Describe the CAGR and the code to calculate the CAGR.
For the price of a stock, let S_0 be the stock price on the base date and S_n be the stock price n business days after the start date. At this time, CAGR (compound average growth rate) is calculated by the following formula.
CAGR = \biggl({\frac{S_n}{S_0}} \biggl)^{\frac{1}{years}}-1
However, years = n / (total number of trading days in one year).
CAGR (Compound Annual Growth Rate) is the geometric mean per year calculated from the growth rate over multiple years. For example, consider the average growth rate for three years when sales of 100 million yen grow to 160 million yen in three years. In response to this problem, 160 ÷ 100 = 1.6, that is, a 60% increase in three years, so the idea that the average annual growth rate is 60/3 = 20, which is 20%, is incorrect. In general business thinking, the compound annual growth rate is based on the idea of compound interest, and we have to find x such that 100 x (1 + x) x (1 + x) x (1 + x) = 160. Therefore, 3√1.6 = 1.1696 ... That is, 17.0% is the correct answer.
Source: https://mba.globis.ac.jp/about_mba/glossary/detail-11621.html
test.py
#CAGR(Annual growth rate)To calculate
def CAGR(DF):
df = DF.copy()
df["daily_ret"] = DF["Close"].pct_change() #Calculate the rate of change from the day before the closing price of the stock price.
df["cum_return"] = (1 + df["daily_ret"]).cumprod() #cumprod(Returns the cumulative product of all elements in a scalar y.
n = len(df)/252
CAGR = (df["cum_return"][-1])**(1/n) - 1
return CAGR
Recommended Posts