'Calculating interest rates in numpy

Hopefully this is a quick and easy question that is not a repeat. I am looking for a built in numpy function (though it could also be a part of another library) which, given an original loan amount, monthly payment amount, and number of payments, can calculate the interest rate. I see numpy has the following function:

np.pmt(Interest_Rate/Payments_Year, Years*Payments_Year, Principal)

This is close to what I am looking for but would instead prefer a function which would provide the interest rate when given the three parameters I listed above. Thank you.



Solution 1:[1]

You want numpy.rate from numpy_financial.

An example usage: suppose I'm making 10 monthly payments of $200 to pay off an initial loan of $1500. Then the monthly interest rate is approximately 5.6%:

>>> import numpy as np
>>> monthly_payment = 200.0
>>> number_of_payments = 10
>>> initial_loan_amount = 1500.0
>>> np.rate(number_of_payments, -monthly_payment, initial_loan_amount, 0.0)
0.056044636451588969

Note the sign convention here: the payment is negative (it's money leaving my account), while the initial loan amount is positive.

You should also take a look at the when parameter: depending on whether interest is accrued after each payment or before, you'll want to select the value of when accordingly. The above example models the situation where the first round of interest is added before the first payment is made (when='end'). If instead the payment is made at the beginning of each month, and interest accrued at the end of the month (when='begin'), the effective interest rate ends up higher, a touch over 7%.

>>> np.rate(number_of_payments, -monthly_payment, initial_loan_amount, 0.0, when='begin')
0.070550580696092852

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Valgur