Compute Growth Index#
The compute_growth_index function takes a pandas DataFrame of historical prices, an initial value for the investment, and optional parameters for initial and ending costs. It calculates the percentage change of the price data, adjusts it for the initial and ending costs, and computes the cumulative product. The resulting DataFrame contains the growth index for each column in the input DataFrame.
Function#
def compute_growth_index(dataframe, initial_value=100, initial_cost=0, ending_cost=0):
"""
Compute the growth index of a given DataFrame of prices, accounting for initial and
ending costs.
Parameters
----------
dataframe : pandas.DataFrame
DataFrame containing price data. Index should be of datetime type.
initial_value : float, optional
Initial value of the investment. Default is 100.
initial_cost : float, optional
Initial cost as a percentage of the investment. Default is 0.
ending_cost : float, optional
Ending cost as a percentage of the investment. Default is 0.
Returns
-------
pandas.DataFrame
A DataFrame containing the growth index for each column in the input DataFrame.
"""
if not isinstance(dataframe, pd.DataFrame):
raise TypeError("Input must be a pandas DataFrame")
initial_cost = initial_cost / 100
ending_cost = ending_cost / 100
GR = ((1 + dataframe.pct_change()).cumprod()) * (initial_value * (1 - initial_cost))
GR.iloc[0] = initial_value * (1 - initial_cost)
GR.iloc[-1] = GR.iloc[-1] * (1 * (1 - ending_cost))
return GR
Parameters#
dataframe: A pandas DataFrame containing price data. The index should be of datetime type.
initial_value: A float, optional. The initial value of the investment. Default is 100.
initial_cost: A float, optional. The initial cost as a percentage of the investment. Default is 0.
ending_cost: A float, optional. The ending cost as a percentage of the investment. Default is 0.
Returns#
A pandas DataFrame containing the growth index for each column in the input DataFrame.
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']
dsf.compute_growth_index(spy)
spy | |
---|---|
Date | |
2015-12-31 | 100.000000 |
2016-01-04 | 98.602058 |
2016-01-05 | 98.768794 |
2016-01-06 | 97.522931 |
2016-01-07 | 95.183209 |
... | ... |
2022-12-23 | 213.023382 |
2022-12-27 | 212.183320 |
2022-12-28 | 209.546324 |
2022-12-29 | 213.318236 |
2022-12-30 | 212.756325 |
1763 rows × 1 columns
dsf.compute_growth_index(spy, 200)
spy | |
---|---|
Date | |
2015-12-31 | 200.000000 |
2016-01-04 | 197.204116 |
2016-01-05 | 197.537588 |
2016-01-06 | 195.045863 |
2016-01-07 | 190.366418 |
... | ... |
2022-12-23 | 426.046765 |
2022-12-27 | 424.366641 |
2022-12-28 | 419.092649 |
2022-12-29 | 426.636472 |
2022-12-30 | 425.512651 |
1763 rows × 1 columns