Open PortfolioOpen Portfolio.
โ† Back to Blog

How to Calculate CAGR Programmatically

February 2, 2026at 3:40 PM UTCBy Pocket Portfolio Teamtechnical
How to Calculate CAGR Programmatically
#calculate#cagr#technical#tutorial#guide

Calculating the Compound Annual Growth Rate (CAGR) is essential for assessing the performance of investments over time. This short guide will provide a straightforward method to compute CAGR programmatically.

Direct Solution with Code

n
def calculate_cagr(final_value, initial_value, periods):
    return (final_value / initial_value) ** (1 / periods) - 1

To use this function, simply pass the final investment value, the initial investment value, and the number of periods (years) over which the growth occurred:

n
cagr = calculate_cagr(final_value=1500, initial_value=1000, periods=3)
print(f"The CAGR is {cagr:.2%}")

Explanation of Key Concepts

The Compound Annual Growth Rate (CAGR) represents the mean annual growth rate of an investment over a specified time period longer than one year. It assumes the investment grows at a steady rate over that period. The formula to calculate CAGR is:

CAGR = (V_f / V_i)^(1/n) - 1

Where:

  • V_f is the final value of the investment,
  • V_i is the initial value of the investment, and
  • n is the number of periods (years).

This formula is derived from the future value formula of a financial investment at compound interest.

Quick Tip

Remember that CAGR does not account for the volatility of the investment's growth over the periods. It smooths out the rate of growth as if it were constant. When using CAGR in real-world scenarios, it's important to consider the investment's performance fluctuations throughout the period.

Gotcha

Beware of negative or zero values for initial or final investment values. The CAGR formula involves taking the nth root of a number, which can lead to complex numbers or math errors if the base of the exponent is negative or zero. Always validate your input values to avoid unexpected results.

This concise guide provides a straightforward method to calculate CAGR programmatically, essential for evaluating investment growth over time. Remember, while CAGR is a useful metric, it's important to consider alongside other factors and metrics for a comprehensive investment analysis.

How to Calculate CAGR Programmatically | Open Portfolio Blog | Open Portfolio