Home > other >  Given all values except interest rate, how do I solve EMI formula for interest rate?
Given all values except interest rate, how do I solve EMI formula for interest rate?

Time:10-21

The installment amount is calculated by the formula below.

enter image description here

I have a dataframe where I have the principal amount (P), installment amount and number of payments (n) in different columns and I wish to calculate the interest rate (i) for all rows.

Principal (P) Installment Amount Number of Installments (n) Interest Rate (i)
5.300 187 35 r

CodePudding user response:

Given a dataframe called df

>>> df
    Principal  Installment  Num Payments
0      1000.0         40.0            30
1      3500.0        200.0            20
2  10000000.0    2000000.0            10

and a function interest using some solving method (in below example, Newton-Raphson)

ERROR_TOLERANCE = 1e-6

def interest(principal, installment, num_payments):
    def f(x):
        return principal * x**(num_payments   1) - (principal   installment) * x**num_payments   installment
    def f_prime(x):
        return principal * (num_payments   1) * x**num_payments - (principal   installment)*num_payments * x**(num_payments - 1)

    guess = 1   (((installment * num_payments / principal) - 1)/12)
    intermediate = f(guess)
    while abs(intermediate) > ERROR_TOLERANCE:
        guess = guess - intermediate / f_prime(gues
        intermediate = f(guess)
    return guess

you can calculate the interest rate like

df['Interest'] = df.apply(lambda row: interest(row['Principal'],row['Installment'],row['Num Payments']),axis=1)

giving

>>> df
    Principal  Installment  Num Payments  Interest
0      1000.0         40.0            30  1.012191
1      3500.0        200.0            20  1.013069
2  10000000.0    2000000.0            10  1.150984

Note: tweak ERROR_TOLERANCE as desired to meet requirements.

  • Related