2

I am trying to calculate APR using Newton Raphson in PHP. I have modified the code from this question into php

Calculating annual percentage rate (need some help with inherited code)

However the formula in this question is based on an initial principal, recurring monthly payments and a total number of payments. I need my APR calculation to include an initial start up fee. This would be added to the first monthly payment meaning the monthly payments are no longer all the the same. I take it that this would alter the original formula and hence its derivative. I have no idea what to use now as the initial formula or how to modify this

Here is the php

$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 %";

Thanks in advance for any help

Community
  • 1
  • 1
user2274191
  • 843
  • 2
  • 14
  • 24

1 Answers1

4

If it's just added to the first monthly payment as a constant, just .. well, add it to the first monthly term from the calculations. After all, the payment schedule and interest should be the same, it's just an added payment in the first term. It does not affect any other payments or the total amount to be paid back over time, as it's just a fee.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • this formula doesnt use a first monthly payment. all payment amounts are the same. Unless I am missing something, where in the code should i add it to the first monthly payment as there is no first monthly payment? – user2274191 Dec 30 '13 at 20:53
  • Then you add it to the total to be paid - APR calculations are based on a yearly approximate rate iirc, including all costs with having the loan. You'll have to consider your local laws for what should be included. – MatsLindh Dec 30 '13 at 21:17
  • I tried adding it to the principal but I do not get the same value as other calculators. For example a 10k loan at 5% interest in uk has apr of 9.5%. When I add an upfront fee of 250 it goes to 14.4%. In my code if I add 250 to the principal the apr remains the same – user2274191 Dec 31 '13 at 11:03
  • Plus if I add to principal it gets interest. It should be an upfront fee and not spread – user2274191 Dec 31 '13 at 11:05