Calculate the Compound Interest

problem-solving
Published

June 5, 2024

Compound interest is the interest earned on both the principal amount and the accumulated interest from previous periods. It’s a powerful concept in finance, allowing your investments to grow exponentially over time. This post will guide you through calculating compound interest using Python, covering different scenarios and providing clear code examples.

Understanding the Formula

The fundamental formula for compound interest is:

\[ A = P \left( 1 + \frac{r}{n} \right)^{nt} \]

Where:

  • A = the future value of the investment/loan, including interest
  • P = the principal investment amount (the initial deposit or loan amount)
  • r = the annual interest rate (decimal)
  • n = the number of times that interest is compounded per year
  • t = the number of years the money is invested or borrowed for

Python Implementation: Basic Compound Interest

Let’s start with a simple Python function to calculate compound interest based on the formula above:

def calculate_compound_interest(principal, rate, time, n):
  """Calculates compound interest.

  Args:
    principal: The principal amount.
    rate: The annual interest rate (decimal).
    time: The number of years.
    n: The number of times interest is compounded per year.

  Returns:
    The future value of the investment/loan.
  """
  amount = principal * (1 + rate / n)**(n * time)
  return amount

principal = 1000  # Initial investment
rate = 0.05      # 5% annual interest rate
time = 5         # 5 years
n = 1            # Compounded annually

future_value = calculate_compound_interest(principal, rate, time, n)
print(f"Future value after {time} years: ${future_value:.2f}")

# Future value after 5 years: $1276.28

This function takes the principal amount, annual interest rate, time period, and compounding frequency as input and returns the future value. The example shows how to use the function with annual compounding.

Handling Different Compounding Frequencies

The n parameter in the formula allows for different compounding frequencies. Let’s modify the example to demonstrate:

principal = 1000
rate = 0.05
time = 5

future_value_annual = calculate_compound_interest(principal, rate, time, 1)
print(f"Future value (annual compounding): ${future_value_annual:.2f}")

future_value_semi_annual = calculate_compound_interest(principal, rate, time, 2)
print(f"Future value (semi-annual compounding): ${future_value_semi_annual:.2f}")

future_value_quarterly = calculate_compound_interest(principal, rate, time, 4)
print(f"Future value (quarterly compounding): ${future_value_quarterly:.2f}")

future_value_monthly = calculate_compound_interest(principal, rate, time, 12)
print(f"Future value (monthly compounding): ${future_value_monthly:.2f}")

# Future value (annual compounding): $1276.28
# Future value (semi-annual compounding): $1280.08
# Future value (quarterly compounding): $1282.04
# Future value (monthly compounding): $1283.36

This code snippet shows how changing the n value affects the final amount. As you can see, more frequent compounding leads to slightly higher returns.

Continuous Compounding

In the case of continuous compounding, the formula changes slightly:

\[ A = P e^{rt} \]

where ‘e’ is the mathematical constant approximately equal to 2.71828. Let’s add a function for this:

import math

def calculate_continuous_compound_interest(principal, rate, time):
  """Calculates compound interest with continuous compounding."""
  amount = principal * math.exp(rate * time)
  return amount

principal = 1000
rate = 0.05
time = 5

future_value_continuous = calculate_continuous_compound_interest(principal, rate, time)
print(f"Future value (continuous compounding): ${future_value_continuous:.2f}")

# Future value (continuous compounding): $1284.03

This demonstrates how to calculate compound interest with continuous compounding using the math.exp() function.