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!