English 中文(简体)
计算年薪率(根据继承法计算了一些帮助)
原标题:Calculating annual percentage rate (need some help with inherited code)

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.

(请注意,Im绝非数学或财务上加权)

[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]
最佳回答

尽管我不知道什么是计算方法,但该法典确实使用纽特-拉夫森方法;你可能已经从错误的章节中复制过。 如果你想计算贷款数额、每月付款额和月数的年度百分比,那么你几乎完全解决了这一问题,除非你可能不知道“is<>/em”的职能是什么,其根源正在寻找,这是可以理解的。

正在搜索的价值称为,对此没有封闭形式;您必须计算出硬度或使用数字方法。 计算年度百分比是经常资源支出的一个特殊案例,所有付款均相等,贷款按期进行。 这意味着这一公式如下:

P是本金/卢兰数额,是每月付款,即利率,N是月/月数。

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

我们必须解决。 上述公式相当于:

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 is the principal/loan amount
  • m is recurring payment amount
  • N is total number of payments

因此,我们必须找到的根源是:

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

为使用纽顿-Rhapson方法,我们需要

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) )

http://groovy.codehaus.org“rel=“noretinger”>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 "

我没有使用规定代码,因为它是一块微粒(但我没有工作)。 我从纽顿-Rhapson和月摊付等概念得出了这一定义。 近似值(在2或3个频率内为10^-5)

NOTE:我无法将这一联系适当插入第一种衍生工具首次提及的案文:http://www.wolframalpha.com/input/?i=d/dx(+x+*+(1+%2B+x)^n)/((1+%2B+x)+-1)+-m+>。

问题回答

暂无回答




相关问题
Understanding a software system

I recently became part of a complex embedded project team for which I will be developing a part. For the part which is my responsibility there is only old code and not much documentation. I am keen ...

How to add a new feature to an existing component? [closed]

We have this legacy image viewer component in our project that s working fine but it s source is a real mess. I have to add a new feature to this component so that people can add annotation to images ...

Ruby on Rails: What to do with legacy code?

I ve a portal project built in Rails 1.2.3. I ve finished it at end of 2006. The project are using the following plug-ins: acts_as_attachment acts_as_ferret betternestedset simple_http_auth I know ...

Web application to legacy code interop

I am trying to migrate a simple WinForms app to a ASP.Net web app. WinForms app is basically only a presentation layer for a complex plain C app, and interacts with legacy code through COM interop. I ...

Separating rapid development from refactoring/optimization

I m working in a team of 2 front-end developers on a web-based late-stage startup project. The site works quite well, but there s a lot of room for improvement code-wise, as the code is quite messy ...