计算年百分比(需要继承代码的一些帮助)
我正在制作一个应用程序,为客户提供大致的贷款报价(稍后由其他后台系统计算)。 我从我们为其制作计算器的金融公司收到了一些代码。 我的问题是我不理解计算年百分比率(包括启动费和月费)的代码部分。
他们可能正在使用这种方法,但我不能真正告诉: http://www.efunda.com/math/num_rootfinding/num_rootfinding.cfm# Newton_Raphson
代码工作正常,但我真的很讨厌在我不完全理解和/或信任的代码上构建应用程序。 最终的答复将是执行相同操作的源代码,但带有注释和易于理解的变量名称(我并不是真的例外:-) 欢迎所有想法 - 也许有人有解释它的文章的链接。
(请注意,我绝不是数学或金融奇才)
[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]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码确实使用了牛顿拉夫森方法,尽管我不知道它到底在计算什么;您可能复制了错误的部分。事实上,如果您想计算给定贷款金额、每月还款额和月数的年利率,那么您几乎已经完全解决了这个问题,只是您可能不知道该函数是什么 > 正在寻找其根源,这是可以理解的,这是一个绊脚石。
正在搜索的值称为内部回报率( IRR),没有封闭形式;你必须以困难的方式计算它或使用数值方法。计算年百分比率是 IRR 的一个特例,其中所有付款均等且贷款按期限偿还。这意味着等式如下:
P 是本金/贷款金额,m 是每月还款额,i 是利率,N 是月数
我们必须求解 i。上面的等式等价于:
有一些公式可以得到右侧总和的封闭形式,从而得出以下等式,该等式将我们已知的所有数量(期限、贷款和每月还款额)联系起来这更容易处理:
为了减少输入,让:
因此我们必须找到根的方程是:
要使用 Newton-Rhapson 方法,我们需要第一个 F 对 x 的导数
: Groovy 中的以下代码进行了正确的计算:
我没有使用提供的代码,因为它有点模糊(加上它不适合我)。我从牛顿-拉普森和每月抵押贷款支付方程的定义中得出了这一点。近似值收敛得非常快(2 或 3 次迭代内为 10^-5)
注意:我无法为首次提到一阶导数的文本正确插入此链接:
http://www. Wolframalpha.com/input/?i=d/dx(+x+*+((1+%2B+x)^n)/(((1+%2B+x)^n)+-+1)+- m+)
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
And we have to solve for i. The above equation is equivalent to:
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:
To reduce typing let:
So the equation whose roots we have to find is:
To use the Newton-Rhapson method we need the first derivative of F with respect to x:
The following code in Groovy does the proper calculation:
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+)