2

I'm trying to recreate this app. However, for cases where the frequency of deposits do not match the compounding periods, my fuction's output doesn't match the linked app's output for the total value of investment.

Here's my function...

def compound_interest(principal, pmt, rate, frequency, period, time):

    contribution_frequencies = {'weekly' : 52, 'biweekly' : 26, 'monthly' : 12, 'quarterly' : 4, 'semiannually' : 2, 'yearly' : 1}
    compounding_periods = {'monthly' : 12, 'quarterly' : 4, 'semiannually' : 2, 'yearly' : 1}

    frequency = contribution_frequencies[frequency]
    period = compounding_periods[period]

    rate = rate / 100

    principal_interest = principal * (1 + (rate / period)) ** (period * time)
    fv =  (pmt * frequency) / period  * ((1 + (rate / period)) ** (period * time) - 1) / (rate / period)

    total = principal_interest + fv

    return round(total, 2)

Here's my tests where the frequency of deposits are the same as the compounding periods...

print(compound_interest(5000, 100, 5, 'monthly', 'monthly', 15))
print(compound_interest(5000, 100, 5, 'yearly', 'yearly', 15))
print(compound_interest(5000, 100, 5, 'quarterly', 'quarterly', 15))
print(compound_interest(5000, 100, 5, 'semiannually', 'semiannually', 15))

The returned actual values below from my function are the same as the actual values i get from the linked app's output...

37297.41
12552.5
19393.36
14878.11

For cases other than the above, the actual values of the tests are not the same as that of the linked app. E.g....

print(compound_interest(5000, 100, 5, 'weekly', 'monthly', 15))

returns...

126393.73

whereas the linked app returns...

126579.19

Keep in mind my equation calculates for additional deposits made at the end of a compounding period (or so it it says) which seems to be the same as that of the linked app.

How would I rewrite my function so that its actual values returned are the same as that of the linked app's actual values for all combinations of frequency of deposits and compounding periods?

Thanks!

Acubal
  • 173
  • 1
  • 2
  • 8

1 Answers1

1

I think I see the error in the function. Usually (that is, when compounding and additional contributions take place at the same frequency), the compounding period rate is indeed rate/period. So a nominal annual rate of 5%, compounded quarterly, for example, can be divided by 4 for quarterly compounding (i.e., 1.25% per quarter - which is the rate), resulting in an effective annual rate of 5.095%.

However, when the contribution period is changed to monthly, for example, without an equivalent change in the compounding period, the effective annual rate stays the same. So just dividing the the nominal rate by 12 (so that the rate is 0.41667%) overstates the interest you earn.

To get to the actual monthly rate when the compounding is quarterly, you need to calculate the monthly IRR of the same investment (without contributions) resulting in a future value after one year equal to the future value of the same investment (without contributions) using the effective interest rate of quarterly compounding (at least that's the only way I can think of).

So for example, the future value of $1,000, with a nominal annual 5% interest, compounded quarterly (and no contributions) is $1,050.95 (that reflects the effective annual rate mentioned above). To get the same future value with the same interest rate but with monthly compounding, your monthly rate is 0.41494%, which is less than the rate you get by dividing the nominal annual rate by 12. If you check this scenario on the calculator site, this is the result you'll see.

To do it right, you'll have to develop formulas for converting between each pair of compounding/contribution period (semiannual/monthly, semiannual/quarterly, etc.). These may exist out there, but I couldn't find them. Once you have these formulas, you'll have to incorporate them into your function.

Finally, to make things even more complicated, you'll have to deal with the problem of the weekly calculations. While each year is divisible into exactly 4 quarters and 12 months, it's not divisible into exactly 52 weeks. So, for example, 15 years have 782.143 weeks, not 780. That has to be taken into account separately.

Hopefully - if you're still trying to do it - this won't get you to quit. It is an interesting project, after all.

Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45