Build on CAPM by incorporating additional independent variables.
import numpy as np
import pandas as pd
import edhec_risk_kit as erk
import statsmodels.api as sm
ind_rets = erk.get_ind_returns(n_inds=49)
fff = erk.get_fff_returns()
ind_excess = ind_rets['1970':'2015'] - fff.loc['1970':'2015', ['RF']].values
mkt_excess = fff.loc['1970':'2015', ['Mkt-RF']]
exp_var = mkt_excess.copy()
exp_var['SMB'] = fff.loc['1970':'2015', ['SMB']]
exp_var['HML'] = fff.loc['1970':'2015', ['HML']]
exp_var['Constant'] = 1
lm = sm.OLS(ind_excess, exp_var).fit()
result = erk.regress(ind_excess, exp_var)
smb_idx_max = result.params.loc['SMB', :].idxmax()
ind_rets.columns[smb_idx_max]
smb_idx_min = result.params.loc['SMB', :].idxmin()
ind_rets.columns[smb_idx_min]
hml_idx_max = result.params.loc['HML', :].idxmax()
ind_rets.columns[hml_idx_max]
hml_idx_min = result.params.loc['HML', :].idxmin()
ind_rets.columns[hml_idx_min]
ind_rets = erk.get_ind_returns(n_inds=49)['1980':'2018']
fff = erk.get_fff_returns()['1980':'2018']
softw_rets = ind_rets.loc[:, ['Softw']]
erk.ff_analysis(r=softw_rets, factors=fff[['Mkt-RF', 'SMB', 'HML']])
hfi_rets = erk.get_hfi_returns()['1980':'2018']
emr_market_rets = hfi_rets['Emerging Markets']
erk.ff_analysis(r=emr_market_rets, factors=fff[['Mkt-RF', 'SMB', 'HML']])