How to calculate APR using Newton Raphson

4.6k Views Asked by At

I'm have a computer program to calculate apr using Newton Rhapson. I imagine most mathletes can code so i dont imagine the coding being an issue.

The solution is based on this initial formula

$$\text{PMT}_{\text{month}}= \text{loan} \times \text{rate}\times \frac{(1+\text{rate})^{\text{#PMTs}}}{(1+ \text{rate})^{\text{#PMTs}}-1}$$

The formula and its derivative for the solution are as below with $P$, the principal/loan amount; $m$, the recurring payment amount; and $N$, the total number of payments:

$$\begin{align} F(x) &= \frac{P\;x\;(1+x)^N}{\left((1+x)^N-1\right)}-m\\[5ex] F'(x) &= \frac{P (x+1)^{n-1} \left(x (x+1)^n+(x+1)^n-n x-x-1\right)}{\left((x+1)^n-1\right)^2}\end{align}$$

I would like to change the solution to involve an upfront arrangement fee. I believe the fee can simply be a fixed amount which is added to the first monthly payment? Assuming this is correct, the f(x) that I am using in the solution doesn't include a term for the first monthly payment - the monthly payments are a function of the interest rate

So how would i calculate the apr if the monthly payments simply a function of interest rate and include this arrangement fee? Can i still use Newton Raphson in the method described?

The full code for the original solution is below for completeness if it helps

Thanks for any help you can give

$numPay = 12;
$payment = 875;
$amount = 10000;
$error = pow(10,-5);
$approx = 0.05/12; // let's start with a guess that the APR is 5% 
$prev_approx;

function f($x) {
    global $numPay;
    global $payment; 
    global $amount;
    global $error;



    return $amount * $x * (pow(1 + $x,$numPay)/(pow(1 + $x, $numPay) - 1)) - $payment;

}



function f_prime($x) {
    global $numPay;
    global $payment; 
    global $amount;
    global $error;

     return $amount * (pow(1 + $x,$numPay)/(-1 + pow(1 + $x,$numPay)) - $numPay * $x * pow(1 + $x,-1 + 2*$numPay)/pow(-1 + pow(1 + $x,$numPay),2) + $numPay * $x * pow(1 + $x,-1 + $numPay)/(-1 + pow(1 + $x,$numPay)));


}
echo f($approx) . "<br/>";

echo f_prime($approx) . "<br/>";
echo  "initial guess $approx" . "<br/>";

for ($k=0;$k<20; $k++) {
       $prev_approx = $approx;
       $approx = $prev_approx - (f($prev_approx)/f_prime($prev_approx));
       $diff = abs($approx-$prev_approx);
       echo "new guess $approx diff is $diff <br/>";
       if ($diff < $error) break;
}

$apr = round($approx * 12 * 10000 /100, 1); // this way we get APRs like 7.5% or 6.55%
echo "apr is $apr %";
1

There are 1 best solutions below

6
On

Let's collect the comments into an answer. I like single letter variables for formulas, so

T -- $amount;     or principal
n -- $numPay;     number of months
R -- $payment;    monthly rate
F -- $fee;
x -- $x;          monthly interest rate

The payment plan, as I understand it, has an initial debt or principal $T$, and additional a fee $F$. After a month $R+F$ are paid, then every month $R$, after the n-th payment, the debt is paid in full.

As stated, the interest on the fee for the one month is not raised, which means that $F$ instead of $F\cdot(1+x_{nominal})$ is paid after a month. In balance, this still increases the effective interest rate.

The balance after $n$ months, seen from the start of the debt, and according to the flow of payments, reads as

\begin{align}T+F&=(R+F)(1+x)^{-1}+R(1+x)^{-2}+\dots+R(1+x)^{-n}\\ &=F(1+x)^{-1}+R\frac{1-(1+x)^{-n}}{x}\\ Tx+F\frac{x^2}{1+x}&=R(1-(1+x)^{-n}) \end{align}

Define

$$f(x)=Tx(1+x)+Fx^2-R(1+x-(1+x)^{1-n})$$

with derivative

$$f'(x)=T(1+2x)+2Fx-R(1-(n-1)(1+x)^{-n})$$

for a Newton iteration with the initial guess

$$x_0=\frac{nR-T}{\frac{n(n+1)}2R+F}$$