Before performing shift-based methods, because row order matters, it is important to ensure the rows are sorted in the proper order (usually in ascending order by date).
# sorting by year for good measure:gdp_df.sort_values(by=["year"], ascending=True, inplace=True)gdp_df.head()
year
gdp
0
1990
100
1
1991
105
2
1992
110
3
1993
115
4
1994
110
We can use the dataframe’s shift method to reference a corresponding value in another row above or below by specifying the number of rows above or below via the periods parameter.
We use positive numbers to reference rows above, and negative numbers to reference cells below:
gdp_df["gdp"].shift(periods=1) # 1 or -1 depending on order
Even though we are able to perform this growth calculation ourselves, we should know the DataFrame has a dedicated pct_change method for this purpose, which allows us to skip the intermediate steps:
# equivalent, leveraging the pct_change method:gdp_df["gdp_pct_change"] = gdp_df["gdp"].pct_change(periods=1)gdp_df[["year", "gdp", "gdp_pct_change"]]
year
gdp
gdp_pct_change
0
1990
100
NaN
1
1991
105
0.050000
2
1992
110
0.047619
3
1993
115
0.045455
4
1994
110
-0.043478
11.2 Cumulative Growth
Alright, we have studied how to calculate growth from one period to another, but what about calculating cumulative growth over the entire time period?
To calculate cumulative growth for a particular period, we can use thecumprod method (or sometimes the product method, depending on the use case). When calculating the cumulative product, each value gets multiplied by the values that follow, in succession.
Before we calculate a product, to make the multiplication work, we’ll first need to express the growth numbers relative to 1, instead of 0. We’ll also need to fill in the initial null value with a 1, so the first period represents 100%.
Let’s break this down one step at a time, to illustrate each method, before putting them all together at the end.
First we overwrite the initial null value that results from there being no previous row for the first row:
gdp_df.loc[0, "gdp_pct_change"] =0gdp_df
year
gdp
gdp_prev
gdp_change
gdp_pct_change
0
1990
100
NaN
NaN
0.000000
1
1991
105
100.0
5.0
0.050000
2
1992
110
105.0
5.0
0.047619
3
1993
115
110.0
5.0
0.045455
4
1994
110
115.0
-5.0
-0.043478
Then we express growth relative to one instead of zero (so we can calculate cumulative product later):
Each company’s stock prices are expressed using a different scale (i.e. some trade around $10 a share, while others trade around $100 a share). In order to compare their performance, we need to normalize their prices. We can do this by calculating the percent change for each stock. We calculate percent change relative to that stock’s initial price. So a growth of 1 means 100% (i.e. the stocks’ initial value).
import warningswarnings.filterwarnings("ignore") # suppress warning messagesall_symbols = prices_pivot.columns.tolist()growth = prices_pivot.copy()for symbol in all_symbols: growth_col =f"{symbol}_growth"# calculate relative growth: growth[growth_col] = (growth[symbol].pct_change(periods=1) +1).cumprod()# fill in null value resulting from there being no previous period: growth[growth_col].iloc[0] =1growth_cols = [f"{symbol}_growth"for symbol in all_symbols]growth = growth[growth_cols]growth = growth.round(2)growth.head()
AAPL_growth
AMZN_growth
BAC_growth
GOOGL_growth
JPM_growth
META_growth
MSFT_growth
NFLX_growth
NVDA_growth
SPY_growth
date
2023-01-03
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
1.00
2023-01-04
1.01
0.99
1.02
0.99
1.01
1.02
0.96
1.05
1.03
1.01
2023-01-05
1.00
0.97
1.02
0.97
1.01
1.02
0.93
1.05
1.00
1.00
2023-01-06
1.04
1.00
1.03
0.98
1.03
1.04
0.94
1.07
1.04
1.02
2023-01-09
1.04
1.02
1.01
0.99
1.02
1.04
0.95
1.07
1.09
1.02
Display a line chart of growth for each company, for visual comparison:
import plotly.express as pxpx.line(growth, y=growth_cols, title=f"Stock Returns vs Market", height=500)
Analysis Questions:
Which company experienced the highest growth? The lowest?
Which companies out-performed the market (i.e. grew more than the S&P)? Underperformed the market?