Python Compound InterestCompute compound interest using a formula. Compound on different time schedules.
Compound interest. There is a time value of money. This is interest. In compound interest, an investor earns interest on top of the interest already earned.
The compound interest formula is well-known—it is an exponential function. In Python the pow method is needed. We translate this formula into a Python def.Numbers: pow
The compound_interest method computes the total value of the money (principal) after interest is paid. We specify the rate (the interest rate, expressed as a percentage).
Rate: This is the interest rate. For an interest rate of 4.3%, we can pass 0.043.
Times: The times_per_year argument indicates yearly (1), quarterly (4) or monthly (12) interest.
Years: This final argument to compound_interest tells us how many years we compound interest. Thinking long-term is important.
Python program that computes compound interest
def compound_interest(principal, rate, times_per_year, years):
# (1 + r/n)
body = 1 + (rate / times_per_year)
exponent = times_per_year * years
# P(1 + r/n)^nt
return principal * pow(body, exponent)
# Compute 0.43% quarterly compound interest for 6 years.
result = compound_interest(1500, 0.043, 4, 6)
# Write result.
# Compute 20% compound interest yearly, quarterly and monthly.
print(compound_interest(1000, 0.2, 1, 10))
print(compound_interest(1000, 0.2, 4, 10))
print(compound_interest(1000, 0.2, 12, 10))
I could write all sorts of incorrect code on this website, but this would not do anyone any good. The result to the first call (above) matches Wikipedia's example.Compound interest: Wikipedia
Yearly, quarterly, monthly.
In the last three calls to compound_interest above, we compare how the final amount of money changes based on how often interest is paid.
And: Monthly and quarterly interest accumulates much faster than yearly interest. So this metric is important in an investment.
Tip: This comparison can help us judge certain investments. For example, bonds may pay monthly, but dividend stocks quarterly.
In volatile markets, an investment's interest rate may appear irrelevant. And this is often true. Capital appreciation too is important.
Compounding. The concept of compound interest is behind much of the world's capital markets. It motivates investment. Compound interest is a powerful generator of wealth.
© 2007-2020 sam allen. send bug reports to firstname.lastname@example.org.