计算年百分比(需要继承代码的一些帮助)

发布于 2024-09-06 10:17:49 字数 1178 浏览 3 评论 0原文

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

他们可能正在使用这种方法,但我不能真正告诉: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜警司 2024-09-13 10:17:49

该代码确实使用了牛顿拉夫森方法,尽管我不知道它到底在计算什么;您可能复制了错误的部分。事实上,如果您想计算给定贷款金额、每月还款额和月数的年利率,那么您几乎已经完全解决了这个问题,只是您可能不知道该函数是什么 > 正在寻找其根源,这是可以理解的,这是一个绊脚石。

正在搜索的值称为内部回报率( 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 方法,我们需要第一个 F 对 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) )

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 "

我没有使用提供的代码,因为它有点模糊(加上它不适合我)。我从牛顿-拉普森和每月抵押​​贷款支付方程的定义中得出了这一点。近似值收敛得非常快(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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文