Introduction to Portfolio Analysis in Python
Charlotte Werger
Data Scientist
$$
$$
# Inspect the data
portfolio_data.head()
mean_ret var pf_w bm_w GICS Sector
Ticker
A 0.146 0.035 0.002 0.005 Health Care
AAL 0.444 0.094 0.214 0.189 Industrials
AAP 0.242 0.029 0.000 0.000 Consumer Discretionary
AAPL 0.225 0.027 0.324 0.459 Information Technology
ABBV 0.182 0.029 0.026 0.010 Health Care
# Calculate mean portfolio return
total_return_pf = (pf_w*mean_ret).sum()
# Calculate mean benchmark return
total_return_bm = (bm_w*mean_ret).sum()
# Calculate active return
active_return = total_return_pf - total_return_bm
print ("Simple active return: ", active_return)
Simple active return: 6.5764
# Group dataframe by GICS sectors
grouped_df=portfolio_data.groupby('GICS Sector').sum()
# Calculate active weights of portfolio
grouped_df['active_weight']=grouped_df['pf_weights']-
grouped_df['bm_weights']
print (grouped_df['active_weight'])
GICS Sector
Consumer Discretionary 20.257
Financials -2.116
...etc
Introduction to Portfolio Analysis in Python