Search code examples
c#finance

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


I am building a finance calculator of sorts and one part I am developing is finding the future value for a fixed annuity. The equation for a regular annuity (i.e. payment at the end of the period) is:

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

Where PMT is the payment per period t, i = r/m is the growth rate r divided by the times compounded per period m, n is the number of compounded periods m*t, and T is a 0 or 1 valued variable which indicated whether it is a ordinary or due annuity.

My method for the specific function is:

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

Over here, PMT = annuity, t = time, r = interest, m = nCompPeriods, T = startImmediately.

To collect the parameters, I use this button click method:

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");
}

To test if my method works I use this online calculator: https://www.calculatorsoup.com/calculators/financial/future-value-annuity-calculator.php.

With the arguments PMT = 1000, t = 10, r = 1, m = 12, T = 0, my application gives $10 512.49, but the calculator gives $10 464.39. However, if I change m = 1 my application and the calculator both give $10 462.21.

I don't know what is causing this disparity. I have tried changing annuity / (decimal)interest to annuity / (decimal)(interest/nCompPeriods) but that intuitively doesn't make sense because lowering the denominator even further increases the output more, and testing it confirms my suspicion since my output becomes $126 149.88.

So what is going on? I can't figure what is causing the difference. It seems like it has to do with something regarding the denominator of the annuity / (decimal)interest, but I just don't know what it can be.


Solution

  • I have figured out my problem. It has to do with the fact that the payment frequencies do not coincide with the number of compounding periods. When that happens r must be recalculated to find an effective rate with the equation found here. Then, the payment frequencies (referred to as q) take the place of m, and i and n are recalculated with q instead of m.

    When that is done, the error is fixed. I have added a paymentFrequency integer parameter to my function that checks if nCompPeriods is the same as paymentFrequency. If they match, then the original calculation is sufficient, otherwise interest must be turned into another effective rate and paymentFrequency takes the place of nCompPeriods.