Compute Compound Annual
Growth Rate (CAGR)#
The compute_cagr
function calculates the compound annual growth rate (CAGR) of a given pandas DataFrame of prices. CAGR is a useful metric for evaluating the growth of an investment over multiple years, as it provides an annualized return that smooths out the effects of volatility.
To use the compute_cagr
function, you need to provide a pandas DataFrame containing price data with a datetime index. The function works with time series data.
The function takes the input DataFrame, an optional years parameter, and an optional decimals parameter to control the precision of the output. It returns the CAGR in percentage for each column in the input DataFrame.
compute_cagr
() function
Function#
def compute_cagr(dataframe, years='', decimals=2):
"""
Function to calculate CAGR given a dataframe of prices
Parameters
----------
dataframe : pandas.DataFrame
DataFrame containing price data. Index should be of datetime type.
years : int or 'ytd', optional
Number of years to filter the data before computing the CAGR, or 'ytd'
for year-to-date.
decimals : int, optional
Number of decimal places to round the result. Default is 2.
Returns
-------
float or pandas.Series
The CAGR for each column in the input DataFrame, rounded to the specified
number of decimals.
"""
if years == 'ytd':
last_year_end = dataframe.loc[str(last_year)].iloc[-1].name
dataframe = dataframe[last_year_end:]
hpr = dataframe.iloc[-1][0] / dataframe.iloc[0][0] - 1
ytd_return = hpr * 100
return round(ytd_return, decimals)
elif isinstance(years, int):
d1 = dataframe.index[0]
d2 = dataframe.index[-1]
delta = d2 - d1
days = delta.days
years = years
if days > years * 364:
dataframe = filter_by_date(dataframe, years=years)
value = (dataframe.iloc[-1].div(dataframe.iloc[0])).pow(1 / years).sub(1).mul(100)
return round(value, decimals)
else:
return str('-')
else:
years = len(pd.date_range(dataframe.index[0], dataframe.index[-1], freq='D')) / 365.25
return (dataframe.iloc[-1].div(dataframe.iloc[0])).pow(1 / years).sub(1).mul(100)
Description#
The compute_cagr function computes the compound annual growth rate (CAGR) of a given pandas DataFrame of prices.
Parameters#
dataframe (pandas.DataFrame): DataFrame containing price data. The index should be of datetime type. years (int or ‘ytd’, optional): The number of years to include in the calculation or ‘ytd’ for year-to-date. decimals (int, optional): The number of decimal places to round the result. Default is 2.
Returns#
float or pandas.Series: The CAGR for each column in the input DataFrame, rounded to the specified number of decimals.
Example#
import ds4finance as dsf
import yfinance as yf
import pandas as pd
def download_data(ticker, start, end):
data = yf.download(ticker, start=start, end=end, progress=False)
adj_close = data['Adj Close']
return adj_close
ticker = 'SPY'
start_date = '2015-12-31'
end_date = '2022-12-31'
# Load your price data into a pandas DataFrame
spy = pd.DataFrame(download_data(ticker, start_date, end_date))
spy.columns = ['spy']
# Calculate the CAGR for the last 5 years
cagr_5_years = dsf.compute_cagr(spy, years=5)
cagr_5_years
spy 9.16
dtype: float64
# Calculate the CAGR for the entire dataset
total_cagr = dsf.compute_cagr(spy)
total_cagr
spy 11.387325
dtype: float64
# Calculate the year-to-date CAGR
ytd_cagr = dsf.compute_cagr(spy, years='ytd')
ytd_cagr
0.0