Search code examples
pythonannuity

Finding Term in PV formula with negative interest rate


I'm trying to replicate BA II Plus precision in finding term (N) in the Present Value of an Ordinary Annuity Formula. Specifically, with negative interest rates (where I can't use natural logarithms). My code is below, but it is both slow, and marginally less accurate. Is there a better way I can do this?

# code to find term (n) in present value of annuity formula, where the 
# interest rate is negative, i.e. -55.31257% in below example, as the
# general way to solve, i.e natural logarithms don't work with negative 
# interest rates.

pv = 1559606.4
pp = 100
i = -.5531257
# below is the desired answer
# n = 11.251357

# where interest rate is negative
for num in range(1,100000000):
   expn = num/1000000
   temp3 =  pv - pp*(1 - (1+i)**-expn)/i

   #set level of precision, i.e. accurate to within
   if temp3 <= 0.00000001:
       n = num/1000000
       print("The Number of periods is " + str(n))
       n = float(n)
       break   

Solution

  • I would use numpy.nper(), like:

    Code:

    import numpy as np 
    np.nper(i, -pp, pv)
    

    Test Code:

    pv = 1559606.4
    pp = 100
    i = -.5531257
    # below is the desired answer
    # n = 11.251357
    
    import numpy as np
    
    print(np.nper(i, pp, -pv))
    

    Results:

    11.2513570023