计算年百分比(需要一些有关继承代码的帮助) [英] Calculating annual percentage rate (need some help with inherited code)

查看:92
本文介绍了计算年百分比(需要一些有关继承代码的帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个可以为客户提供和近似贷款报价的应用程序(它们稍后由其他后台系统计算). 我从金融公司收到了一些我们要为其制作计算器的代码. 我的问题是我不理解代码中计算年度百分比(包括启动费和月费)的部分.

他们正在使用的可能是这种方法,但我不能真正说出: http://www.efunda.com/math/num_rootfinding/num_rootfinding.cfm# Newton_Raphson

代码可以正常工作,但是我真的很讨厌在我不完全理解和/或信任的代码上构建应用程序. 最终的答复将是具有相同功能的源代码,但是带有注释和易于理解的变量名(我并不是唯一的例外::-)欢迎所有想法-也许有人链接到解释它的文章. /p>

(请注意,我绝不是数学或金融专家)

[snip]
int n = numberOfPayments;
double a = (amount / (monthlyPayment * Math.Pow(n, 2)) - (monthlyPayment / amount));
double d = 0;
if (a == 0)
{
    d = 0;
}
else
{
    for (int qq = 0; qq < 20; qq++)
    {
        double b = amount + (monthlyPayment / a) * (1 - (Math.Pow((1 + a), -n)));
        double c = amount + (monthlyPayment / a) * ((n * (Math.Pow((1 + a), (-n - 1)))) - ((1 - (Math.Pow((1 + a), -n))) / a));
        d = a - (b / c);
        double aa = a;
        double dd = d;
        a = d;
        if (Math.Abs(aa - dd) < Math.Pow(10, -5)) { break; }
    }
}
double apr = ((Math.Pow((1 + d), 12)) - 1) * 100;
apr = Math.Round(apr * 100) / 100;
[/snip]

解决方案

尽管我不知道它到底在计算什么,但是代码确实在使用Newton-Raphson方法.您可能复制了错误的部分.如果确实要计算给定贷款金额,每月还款额和月数的年利率,那么您几乎可以完全解决此问题,只是您可能不知道函数是什么正在寻找其根源,这是可以理解的绊脚石.

正在搜索的值称为 内部收益率 (IRR),无封闭格式;您必须用困难的方式进行计算或使用数值方法.计算年利率是IRR的一种特殊情况,即所有付款都相等且贷款到期.这意味着等式如下:

P是本金/贷款金额,m是每月还款,i是利率,N是月数

0 = P - Sum[k=1..N](m*(1+i)^(-k))

我们必须解决i.上式等于:

P = Sum[k=1..N](m*(1+i)^(-k))
P = m * Sum[k=1..N]((1+i)^(-k))  // monthly payments all the same
P/m = Sum[k=1..N]((1+i)^(-k))

有一些公式可以得出右侧总和的封闭式,从而产生以下公式,该公式将我们已经知道的所有数量(期限,贷款和每月还款额)联系起来,并且远远超过此数.易处理的:

monthlyPayment = loanAmount * interestRate * ((1 + interestRate)^numberOfPayments)/(((1 + interestRate)^numberOfPayments) - 1)

为减少打字量,让我们:

  • P是本金/贷款额
  • m是定期付款金额
  • N是付款总数

所以我们要寻找其根的方程是:

F(x) = P * x * ((1 + x)^N)/(((1 + x)^N) - 1) - m 

要使用Newton-Rhapson方法,我们需要 Groovy 中的以下代码进行了正确的计算:

numPay = 360
payment = 1153.7
amount = 165000
double error = Math.pow(10,-5)
double approx = 0.05/12 // let's start with a guess that the APR is 5% 
double prev_approx

def F(x) {
  return amount * x * Math.pow(1 + x,numPay)/(Math.pow(1 + x,numPay) - 1) - payment
}

def F_1(x) {
  return amount * ( Math.pow(1 + x,numPay)/(-1 + Math.pow(1 + x,numPay)) - numPay * x * Math.pow(1 + x,-1 + 2*numPay)/Math.pow(-1 + Math.pow(1 + x,numPay),2) + numPay * x * Math.pow(1 + x,-1 + numPay)/(-1 + Math.pow(1 + x,numPay))) 
}


println "initial guess $approx"
for (k=0;k<20;++k) {
       prev_approx = approx
       approx = prev_approx - F(prev_approx)/F_1(prev_approx)
       diff = Math.abs(approx-prev_approx)
       println "new guess $approx diff is $diff"
       if (diff < error) break
}

apr = Math.round(approx * 12 * 10000)/100 // this way we get APRs like 7.5% or 6.55%
println "APR is ${apr}% final approx $approx "

我没有使用提供的代码,因为它有点模糊(加上它对我不起作用).我是从牛顿-拉普森(Newton-Rhapson)的定义和每月按揭支付方程式得出的.近似值收敛非常快(在2或3次迭代中为10 ^ -5)

注意:我无法正确插入首次提到一阶导数的文本的链接:http://www.wolframalpha.com/input/?i=d/dx(+x+*+((1+%2B+x)^n)/(((1+%2B+x)^n)+-+1)+-m+)

I'm making an application that gives clients and approximate loan offer (they are later calculated by other back-office systems). I have received some code from the financial firm that we are making the calculator for. My problem is that I do not understand the part of the code that calculates the annual percentage rate (including startup and monthly fees).

It might be this method they are using, but I can't really tell: http://www.efunda.com/math/num_rootfinding/num_rootfinding.cfm#Newton_Raphson

The code works correctly, but I really hate building an application on code that I don't fully understand and/or trust. The ultimate reply would be source-code which does the same thing, but with comments and understandable variable names (I'm not really excepting that :-) All ideas are welcome - maybe someone has a link to an article that explains it.

(please note that I'm by no means a math or financial wiz)

[snip]
int n = numberOfPayments;
double a = (amount / (monthlyPayment * Math.Pow(n, 2)) - (monthlyPayment / amount));
double d = 0;
if (a == 0)
{
    d = 0;
}
else
{
    for (int qq = 0; qq < 20; qq++)
    {
        double b = amount + (monthlyPayment / a) * (1 - (Math.Pow((1 + a), -n)));
        double c = amount + (monthlyPayment / a) * ((n * (Math.Pow((1 + a), (-n - 1)))) - ((1 - (Math.Pow((1 + a), -n))) / a));
        d = a - (b / c);
        double aa = a;
        double dd = d;
        a = d;
        if (Math.Abs(aa - dd) < Math.Pow(10, -5)) { break; }
    }
}
double apr = ((Math.Pow((1 + d), 12)) - 1) * 100;
apr = Math.Round(apr * 100) / 100;
[/snip]

解决方案

The code is indeed using the Newton-Raphson method although I have no idea what exactly it is calculating; you may have copied from the wrong section. If, indeed, you want to calculate the annual percentage rate given the loan amount, the monthly payment and the number of months then you have nearly completely solved this except that you probably don't know what the function is whose roots are being searched for and this is, understandably, a stumbling block.

The value that is being searched is called the internal rate of return (IRR) for which there is no closed form; you have to calculate it the hard way or use numerical methods. Calculating the annual percentage rate is a special case of the IRR where all the payments are equal and the loan runs to term. That means that the equation is the following:

P is the principal/loan amount, m is monthly payment, i is the interest rate, N is number of months

0 = P - Sum[k=1..N](m*(1+i)^(-k))

And we have to solve for i. The above equation is equivalent to:

P = Sum[k=1..N](m*(1+i)^(-k))
P = m * Sum[k=1..N]((1+i)^(-k))  // monthly payments all the same
P/m = Sum[k=1..N]((1+i)^(-k))

There are some formulas to get the closed form for the sum on the right hand side which result in the following equation which relates all the quantities that we know already (term, loan, and monthly payment amount) and which is far more tractable:

monthlyPayment = loanAmount * interestRate * ((1 + interestRate)^numberOfPayments)/(((1 + interestRate)^numberOfPayments) - 1)

To reduce typing let:

  • P is the principal/loan amount
  • m is recurring payment amount
  • N is total number of payments

So the equation whose roots we have to find is:

F(x) = P * x * ((1 + x)^N)/(((1 + x)^N) - 1) - m 

To use the Newton-Rhapson method we need the first derivative of F with respect to x:

F_1(x) = P * ( (1 + x)^N/(-1 + (1 + x)^N) - ((N * x * (1 + x)^(-1 + 2*N))/(-1 + (1 + x)^N)^2) + (N * x * (1 + x)^(-1 + N))/(-1 + (1 + x)^N) )

The following code in Groovy does the proper calculation:

numPay = 360
payment = 1153.7
amount = 165000
double error = Math.pow(10,-5)
double approx = 0.05/12 // let's start with a guess that the APR is 5% 
double prev_approx

def F(x) {
  return amount * x * Math.pow(1 + x,numPay)/(Math.pow(1 + x,numPay) - 1) - payment
}

def F_1(x) {
  return amount * ( Math.pow(1 + x,numPay)/(-1 + Math.pow(1 + x,numPay)) - numPay * x * Math.pow(1 + x,-1 + 2*numPay)/Math.pow(-1 + Math.pow(1 + x,numPay),2) + numPay * x * Math.pow(1 + x,-1 + numPay)/(-1 + Math.pow(1 + x,numPay))) 
}


println "initial guess $approx"
for (k=0;k<20;++k) {
       prev_approx = approx
       approx = prev_approx - F(prev_approx)/F_1(prev_approx)
       diff = Math.abs(approx-prev_approx)
       println "new guess $approx diff is $diff"
       if (diff < error) break
}

apr = Math.round(approx * 12 * 10000)/100 // this way we get APRs like 7.5% or 6.55%
println "APR is ${apr}% final approx $approx "

I did not use the provided code since it was a bit murky (plus it did not work for me). I derived this from the definitions of Newton-Rhapson and monthly mortage payments equation. The approximation converges very quickly (10^-5 within 2 or 3 iterations)

NOTE: I am not able to get this link to be properly inserted for the text where the first derivative is first mentioned: http://www.wolframalpha.com/input/?i=d/dx(+x+*+((1+%2B+x)^n)/(((1+%2B+x)^n)+-+1)+-m+)

这篇关于计算年百分比(需要一些有关继承代码的帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆