复制固定值年金方程式的代码未给出正确答案

Code which replicates an equation for a fixed value annuity not giving the correct answer

我正在构建一个金融计算器,我正在开发的一部分是寻找固定年金的未来价值。定期年金(即期末付款)的等式为:

PMT/i * ((1+i)^n - 1) * (1+iT)

其中 PMT 是每期付款 ti = r/m 是增长率 r 除以每期复合次数 mn是复利期数m*tT01值变量,表示是普通年金还是到期年金。

具体功能我的方法是:

public static decimal FutureFixedAnnuityValue(decimal annuity, float time, float interest, int nCompPeriods = 1, int startImmediately = 0)
{
    return annuity / (decimal)interest * (decimal)(Math.Pow(1 + interest / nCompPeriods, time * nCompPeriods) - 1) * (decimal)(1 + interest / nCompPeriods * startImmediately);
}

这边,PMT = annuity, t = time, r = interest, m = nCompPeriods, T = startImmediately.

为了收集参数,我使用了这个按钮点击方法:

private void btnCalculate_Click(object sender, EventArgs e)
{
    decimal payment = decimal.Parse(txtAnnuity.Text); //PMT
    int periods = int.Parse(txtPeriods.Text); //t
    float gain = float.Parse(txtEnterInterest.Text) / 100; //r
    float length = float.Parse(txtEnterYears.Text); //m
    int immediately = chkPaymentAt.Checked ? 1 : 0; //due or ordinary

    decimal afv = FinanceCalculations.FutureFixedAnnuityValue(payment, length, gain, periods, immediately);
    lblAnnuityFixed.Text = afv.ToString("C2");
}

为了测试我的方法是否有效,我使用了这个在线计算器:https://www.calculatorsoup.com/calculators/financial/future-value-annuity-calculator.php

使用参数 PMT = 1000, t = 10, r = 1, m = 12, T = 0,我的应用程序给出 512.49,但计算器给出 464.39。但是,如果我更改 m = 1,我的应用程序和计算器都会给出 462.21.

我不知道是什么导致了这种差异。我曾尝试将 annuity / (decimal)interest 更改为 annuity / (decimal)(interest/nCompPeriods),但这在直觉上没有意义,因为进一步降低分母会进一步增加输出,测试它证实了我的怀疑,因为我的输出变为 6 149.88

所以这是怎么回事?我不知道是什么造成了差异。好像跟annuity / (decimal)interest的分母有关,但我就是不知道是什么。

我已经解决了我的问题。这与支付频率与复利期数不一致有关。当发生这种情况时,必须重新计算 r 以找到 equation found here 的有效比率。然后,付款频率(称为 q)代替 m,并且 in 重新计算为 q 而不是 [=12] =].

完成后,错误就修复了。我在我的函数中添加了一个 paymentFrequency 整数参数,用于检查 nCompPeriods 是否与 paymentFrequency 相同。如果它们匹配,那么原来的计算就足够了,否则interest必须变成另一个有效率并且paymentFrequency代替nCompPeriods