1

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 %";
  • Usually, you should have no problems in applying Newtons method. By its nature, the function you consider is monotonically increasing and convex, ideal conditions for convergence of Newton. - Is there any interest paid on the fee? Is the fee rolled into the total amount, the principal of the loan? – Lutz Lehmann Dec 30 '13 at 21:52
  • And please, simplify your formula, $\frac{(1+x)^{num}}{(1+x)^{num}-1}=1+\frac{1}{(1+x)^{num}-1}$ – Lutz Lehmann Dec 30 '13 at 21:55
  • I am presuming the fee is added to the first monthly payment without interest on it. I do not know how to handle this scenario – user2274191 Dec 31 '13 at 10:59
  • Then the payment of the fee does not enter the interest calculation. Or you calculate it like $$T+F=(R+F)q^{-1}+Rq^{-2}+\dots+Rq^{-n}=Fq^{-1}+R\frac{1-q^{-n}}{q-1}$$ with $q=1+x$, $x$ the monthly interest rate. – Lutz Lehmann Dec 31 '13 at 11:10
  • Im ever so sorry but im not from a maths background. I really appreciate your help . What do thr letters represent q f and r. What part of that answer is the f (x) that I need to differentiate. Basic calculus is the limit of my maths! – user2274191 Dec 31 '13 at 13:02
  • As I said, $q=1+x$. $T$ is the total amount or principal, $R$ is the rate or payment. Your initial formula for $f(x)$ thus computes $Tx/(1-(1+x)^{-n})-R$. A numerically better equation would be $$f(x)=Tx-R(1-(1+x)^{-n}).$$ If my formula is financially correct, then this gets modified to $$f(x)=Tx+Fx^2-R(1-(1+x)^{-n})$$ with derivative $$f'(x)=T+2Fx-nR(1+x)^{-n-1}.$$ – Lutz Lehmann Dec 31 '13 at 13:16
  • Initial guess is $x=(R-T/n)/((n+1)R/2+F/n)$, and your last computation line should read $apr = round($approx * 12 * 10000, 1) /100; – Lutz Lehmann Dec 31 '13 at 13:37
  • In my last comment I lost a denominator $(1+x)$ at $F$, corrected version in answer. – Lutz Lehmann Dec 31 '13 at 16:28

1 Answers1

1

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}$$

Lutz Lehmann
  • 126,666
  • I will check this when back at work but your answer is very clear and thorough so I can only assume its spot on. Thanks so much – user2274191 Jan 01 '14 at 14:46
  • Hello. Ive now found out that I need to include a fee in both the first monthly payment and the last monthly payment. Im worried im a little out of my depth as I don't know how to handle this scenario either. I dont know if the fee in the last monthly payment has interest – user2274191 Jan 02 '14 at 21:13
  • That would be a fee for the final documents releasing the mortgage or indebtedness? If they are considered as part of the contract and thus of the repayment plan, they enter the computation of the effective rate.
    So $Fx^2$ gets replaced by $F_1x^2+F_nx(1+x-(1+x)^{n-1})$.
    You may leave out the fee terms in the derivative (but of course not in the function value), it is sufficient to have a good approximation of the derivative, the exact derivative is seldom really necessary.
    – Lutz Lehmann Jan 02 '14 at 21:36
  • Ok thanks. I understand the newton raphson side of things but not the financial side of things. I will have a go at implementing this but ive also been advised to try binary chop to calculate apr to see what is best – user2274191 Jan 02 '14 at 23:44
  • Binary chop is for sorted lists or telephone directories. For continuous functions the method is called bisection. For smooth functions like the polynomials and rational functions here, the guarded secant method, also called regula falsi, is often a better choice. – Lutz Lehmann Jan 03 '14 at 00:41
  • Can you explain the initial guess? – Antoni Parellada Aug 02 '17 at 15:52