Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/260904-solving-for-annual-percentage-rate/
Share on other sites

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.