Financial Algorithms Cookbook | Solving Equations

Algorithm: Calculating the Present Value

Typical Uses

  • todo

Ingredients

To calculate the number of compounding periods you will need the following:

  • todo
tolerence=0.000001
limit=100
def newton_raphson(function, estimate, stop, *values): 
  xk = estimate
  fk = function(xk, *values)
  h=0.0001
  xk = estimate + h
  fkh = function(xk, *values) 
  # Stop since estimate was correct. 
  if fk  == stop:
    return estimate
  # Perform iteration process.
  for k in xrange(lim):
    dk = (fkh - fk)/h
    if fabs(dk) <= tol:
      return 0.0
    fk = function(xk, *values)
    fkh = function(xk+h, *values)
    uk = fk/dk
    xk -= uk
    if fabs(uk) < tol:
      return  xk

Example

You want to solve for i using the future value of an anuity.

FVoa = PMT [((1 + i)n - 1) / i]

Where:

  • PMT= 5,000
  • n = 5
  • FV = 28,185.46
    f = lambda i,FV,PMT,n: PMT*(pow(1+i, n)-1)/i - FV
    i = newton_raphson(f, 0.05, 0.00, 28185.46, 5000, 5)
    print i
    >>> 
    0.0599999147035
    

References

  • Gallager, T; Andrew Jr., J., Financial Management: Principals and Practices, Upper Saddle River, NJ: Prentice Hall, 1996
  • HP-12C Business Calculator Owner's Manual, Hewlett Packard, 1984
  • HP-10B Business Calculator Owner's Manual, Hewlett Packard, 1994