JavaScript中的简单财务利率函数

4

我正在寻找一个简单的JavaScript金融利率函数,然后我发现了这个。但是它似乎太难理解了。我想简化这个函数,需要你的帮助。 如果有人有一个更简单的函数,请回答。(它相当于Excel中的RATE函数。)

var rate = function(nper, pmt, pv, fv, type, guess) {
  if (guess == null) guess = 0.01;
  if (fv == null) fv = 0;
  if (type == null) type = 0;

  var FINANCIAL_MAX_ITERATIONS = 128;//Bet accuracy with 128
  var FINANCIAL_PRECISION = 0.0000001;//1.0e-8

  var y, y0, y1, x0, x1 = 0, f = 0, i = 0;
  var rate = guess;
  if (Math.abs(rate) < FINANCIAL_PRECISION) {
     y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
  } else {
     f = Math.exp(nper * Math.log(1 + rate));
     y = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;
  }
  y0 = pv + pmt * nper + fv;
  y1 = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;

  // find root by Newton secant method
  i = x0 = 0.0;
  x1 = rate;
  while ((Math.abs(y0 - y1) > FINANCIAL_PRECISION) && (i < FINANCIAL_MAX_ITERATIONS)) {
     rate = (y1 * x0 - y0 * x1) / (y1 - y0);
     x0 = x1;
     x1 = rate;

     if (Math.abs(rate) < FINANCIAL_PRECISION) {
        y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
     } else {
        f = Math.exp(nper * Math.log(1 + rate));
        y = pv * f + pmt * (1 / rate + type) * (f - 1) + fv;
     }

     y0 = y1;
     y1 = y;
     ++i;
  }
  return rate;}

谢谢!


1
你究竟想做什么?计算利率吗?是带复利还是不带复利?你是想尝试计算在z的复合利率下,y债务在x时间内需要支付的确切金额吗?你的目标是什么? - Gareth Parker
嗨,Gareth,我想计算利率,但我想简化这个函数或找到另一个函数。 - rabugento
这是有关费率计算背后正在发生的事情的一些信息:http://www.excelbanter.com/showthread.php?t=123697 - JohnLBevan
如果你能明确解释你想计算什么,我们就可以为你找到一个新的函数或者编写一个函数。但是Excel中的RATE函数非常复杂,因为它似乎是为了解决所有利率问题而设计的。如果你有具体要求,也许我们可以找到一个更简单的函数。 - Gareth Parker
Gareth,我需要一个类似于Excel RATE函数的JavaScript函数。在Excel中,RATE函数为:"=RATE(NPER,PMT,PV)"。 - rabugento
@rabugento也许你应该先理解你要做的数学。如果你不理解数学,那么你如何创建、测试或使用相关的javascript呢? - Hamish
1个回答

3

对于我来说,这个数学问题太过复杂,但是这里有一些更容易理解的内容。为了让内容更加通俗易懂,一些变量已经重命名,并且格式化也更加便于阅读。

function rate(paymentsPerYear, paymentAmount, presentValue, futureValue, dueEndOrBeginning, interest)
{
    //If interest, futureValue, dueEndorBeginning was not set, set now
    if (interest == null)
        interest = 0.01;

    if (futureValue == null)
        futureValue = 0;

    if (dueEndOrBeginning == null)
        dueEndOrBeginning = 0;

    var FINANCIAL_MAX_ITERATIONS = 128;//Bet accuracy with 128
    var FINANCIAL_PRECISION = 0.0000001;//1.0e-8

    var y, y0, y1, x0, x1 = 0, f = 0, i = 0;
    var rate = interest;
    if (Math.abs(rate) < FINANCIAL_PRECISION)
    {
        y = presentValue * (1 + paymentsPerYear * rate) + paymentAmount * (1 + rate * dueEndOrBeginning) * paymentsPerYear + futureValue;
    }
    else
    {
        f = Math.exp(paymentsPerYear * Math.log(1 + rate));
        y = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;
    }
    y0 = presentValue + paymentAmount * paymentsPerYear + futureValue;
    y1 = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;

    // find root by Newton secant method
    i = x0 = 0.0;
    x1 = rate;
    while ((Math.abs(y0 - y1) > FINANCIAL_PRECISION)
        && (i < FINANCIAL_MAX_ITERATIONS))
    {
        rate = (y1 * x0 - y0 * x1) / (y1 - y0);
        x0 = x1;
        x1 = rate;

        if (Math.abs(rate) < FINANCIAL_PRECISION)
        {
            y = presentValue * (1 + paymentsPerYear * rate) + paymentAmount * (1 + rate * dueEndOrBeginning) * paymentsPerYear + futureValue;
        }
        else
        {
            f = Math.exp(paymentsPerYear * Math.log(1 + rate));
            y = presentValue * f + paymentAmount * (1 / rate + dueEndOrBeginning) * (f - 1) + futureValue;
        }

        y0 = y1;
        y1 = y;
        ++i;
    }
    return rate;
}

谢谢Gareth,我正在努力更好地理解这个函数。感谢您的支持! - rabugento
在我的情况下,结果与 Excel 的不同。 - vault

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接