2012us Posted April 14, 2012 Share Posted April 14, 2012 Hi, I am new here and have a problem with a math formula. I am trying to obtain an Annual Percentage Rate for a mortgage that has a 5% Interest Rate compounded monthly. The calculations for the original loan amount (payment, interest rate, term) are complete. But those numbers do not include the loan charges such as closing costs, points, etc. The APR, through the Truth in Lending Act, is used by lenders to disclose the increase. It is my understanding that you have to calculate a new monthly payment including the loan amount charges (points, fees, etc), added onto the original loan amount. That number is used to solve for APR in further calculations. I have already done this and have a good number to start. The problem is that an iterative process must be used to calculate the APR based on the original loan amount and calculated payment for the extra costs; that is where I am at a stumbling block. I got the formula here ... http://www.efunda.com/formulae/finance/loan_calculator.cfm I need to duplicate the formulas in scenario #2 to PHP if possible so I can solve for the APR. The formulas make use of the Newton_Raphson method. The Newton-Raphson method finds the slope (the tangent line) of the function at the current point and uses the zero of the tangent line as the next reference point. The process is repeated until the root is found (as described on efunda.com). In this case the root would be the APR. And I am lost down the rabbit hole of voodoo math. Help would be much appreciated... Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260904-solving-for-annual-percentage-rate/ Share on other sites More sharing options...
ignace Posted April 14, 2012 Share Posted April 14, 2012 What do you have so far? Quote Link to comment https://forums.phpfreaks.com/topic/260904-solving-for-annual-percentage-rate/#findComment-1337264 Share on other sites More sharing options...
2012us Posted April 14, 2012 Author Share Posted April 14, 2012 I have the adjusted payment amount that is used in the final calculation (payment). I have already completed the basic mortgage part of the process, but calculating the APR using the Newton-Raphson equation is where I'm stuck. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260904-solving-for-annual-percentage-rate/#findComment-1337303 Share on other sites More sharing options...
2012us Posted April 14, 2012 Author Share Posted April 14, 2012 This is the code I used for the educated guess part of the equation "r-guess", ... $r = 2 * ($loan_months * ($apr_pmt - $amount)) / ($loan_months * $amount); Legend.... C = $amount = loan amount P = $apr_pmt = monthly payment N = $loan_months = loan term in months Quote Link to comment https://forums.phpfreaks.com/topic/260904-solving-for-annual-percentage-rate/#findComment-1337305 Share on other sites More sharing options...
litebearer Posted April 15, 2012 Share Posted April 15, 2012 Take a look at this (it may point you in the right direction)... <?PHP session_start(); /* script to calculate annual percentage rate of a loan using monthly compounding fixed term fixed rate fixed payments */ /* gather data */ $payment = ((int) ($_POST['payment']*100))/100; $term = (int) $_POST['term']; $loan = ((int) ($_POST['loan']*100))/100; /* validate data */ if($payment<=0 || $term<=0 || $loan<=0) { $my_result = "You MUST enter all three values - try again"; $_SESSION['myresult'] = $my_result; header ("Location: roi00.php"); exit(); } if($payment >= $loan) { $my_result = "Payment must be less than Loan Amount - try again"; $_SESSION['myresult'] = $my_result; header ("Location: roi00.php"); exit(); } if($payment * $term <= $loan) { $my_result = "Total Payments are less than Loan Amount - try again"; $_SESSION['myresult'] = $my_result; header ("Location: roi00.php"); exit(); } if($loan !=){ } /* calculate addon rate, low rate, high rate */ /* DO NOT EDIT THIS SECTION */ $fv = $term * $payment; $interest = $fv - $loan; $years = $term/12; $addon = $interest / $loan / $years; $lo_i = $addon; $hi_i = $addon *100; /* create function DO NOT EDIT */ /* function returns the annual APR as a decimal */ function WhatRate($lo_i, $hi_i, $payment, $term, $loan) { $done = 0; while($done != 1) { $i = ($lo_i + $hi_i)/2/12; $x =1 + $i; $y =pow($x, $term); $z = 1/$y; $w = 1-$z; $u = $w/$i; $pv = $payment * $u; if($pv >$loan+.01) { $lo_i = ($lo_i + $hi_i) /2; }elseif($pv <$loan - .01) { $hi_i = ($lo_i + $hi_i) /2; }else{ $done=1; } } return $i *12; } $apr = WhatRate($lo_i, $hi_i, $payment, $term, $loan) * 100; setlocale(LC_MONETARY, 'en_US'); $my_result = " <table> <tr><td>Loan Amount: </td><td align=right>" . money_format('%i', $loan) . "</td></tr> <tr><td>Monthly Payment: </td><td align=right>" . money_format('%i', $payment) . "</td></tr> <tr><td>Number of Payments: </td><td align=right>" . $term . "</td></tr> <tr><td>Total Payback: </td><td align=right>" . money_format('%i', $fv) . "</td></tr> <tr><td>Total Interest Paid: </td><td align=right>" . money_format('%i', $interest) . "</td></tr> <tr><td>Addon Rate: </td><td align=right>" . number_format($addon*100, 4) . "%</td></tr> <tr><td>APY (simple interest rate): </td><td align=right>" . number_format($apr, 4) . "%</td></tr> </table>"; $_SESSION['myresult'] = $my_result; header ("Location: roi00.php"); ?> BTW if all the extras are added to the loan (ie NOT paid by borrower) then the rate does not change only the payment changes. IF, on the other hand, the extras are DEDUCTED from the loan at closing, THEN you subtract those values from the loan amount to reach a 'net pv' WHICH then would be used to calculate the new interest rate Quote Link to comment https://forums.phpfreaks.com/topic/260904-solving-for-annual-percentage-rate/#findComment-1337521 Share on other sites More sharing options...
2012us Posted April 15, 2012 Author Share Posted April 15, 2012 Thanks litebearer! I will analyze this great-looking code. Will reply after. Grateful Quote Link to comment https://forums.phpfreaks.com/topic/260904-solving-for-annual-percentage-rate/#findComment-1337551 Share on other sites More sharing options...
2012us Posted April 18, 2012 Author Share Posted April 18, 2012 I incorporated the code into my project, it works like a charm. Thank you, thank you for your help! 2012 Quote Link to comment https://forums.phpfreaks.com/topic/260904-solving-for-annual-percentage-rate/#findComment-1338426 Share on other sites More sharing options...
litebearer Posted April 18, 2012 Share Posted April 18, 2012 My pleasure! Glad I could be of some help. Quote Link to comment https://forums.phpfreaks.com/topic/260904-solving-for-annual-percentage-rate/#findComment-1338475 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.