Calculate APR (annual interest rate) programmatically

I am trying to find a way to programmatically calculate APR based on

  • Total loan amount
  • Payment amount
  • Number of Payments
  • Repayment rate

There is no need to take any fees into account.

A fixed interest rate can be assumed, and any remaining amounts may be included in the final payment.

The following formula is based on a loan agreement for a total loan amount of 6,000 euros payable by 24 equal monthly installments of 274.11 euros.

enter image description here

(APR for the above example is 9.4%)

I am looking for an algorithm in any programming language that I can adapt to C.

+6
source share
2 answers

I assume that you want to calculate X from your equation. This equation can be written as

 f(y) = y + y**2 + y**3 + ... + y**N - L/P = 0 

Where

 X = APR L = Loan (6000) P = Individual Payment (274.11) N = Number of payments (24) F = Frequency (12 per year) y = 1 / ((1 + X)**(1/F)) (substitution to simplify the equation) 

Now you need to solve the equation f(y) = 0 to get y . This can be done, for example, using Newton's iteration (pseudocode):

 y = 1 (some plausible initial value) repeat dy = - f(y) / f'(y) y += dy until abs(dy) < eps 

Derivative:

 f'(y) = 1 + 2*y + 3*y**2 + ... + N*y**(N-1) 

You must calculate f(y) and f'(y) using the Horner rule for polynomials to avoid exponentiation. A derivative can probably be approximated in several several terms. After you find y , you get X :

 x = y**(-F) - 1 
+3
source

Here is the Objective-C code snippet I came across (which seems correct) if anyone is interested:

 float x = 1; do{ fx = initialPaymentAmt+paymentAmt *(pow(x, numPayments+1)-x)/(x-1)+0*pow(x,numPayments)-totalLoanAmt; dx = paymentAmt *(numPayments * pow( x , numPayments + 1 ) - ( numPayments + 1 )* pow(x,numPayments)+1) / pow(x-1,2)+numPayments * 0 * pow(x,numPayments-1); z = fx / dx; x=xz; } while (fabs(z)>1e-9 ); apr=100*(pow(1/x,ppa)-1); 
+1
source

Source: https://habr.com/ru/post/898213/


All Articles