Table of Contents
Some of the factors I computed during my internship. I list the formula for each factor, and the code computing it. Those codes are only a little portion of the whole project, which contains much more things such as data cleaning, data merging, speed optimization and so on. I will give an example of how the whole project looks like
Fama-French 3-factor
from sklearn.linear_model import LinearRegression
def get_ff3(df):
x = df[['mkt_rf', 'smb', 'hml']]
y = df['excess_return']
reg = LinearRegression().fit(x,y)
return [reg.intercept_, reg.coef_[0], reg.coef_[1], reg.coef_[2]]
Fama-French 5-factor
def get_ff5(df):
x = df[['mkt_rf', 'smb', 'hml', 'rmw', 'cma']]
y = df['excess_return']
if np.isnan(y).any():
return np.nan
else:
reg = LinearRegression().fit(x,y)
return [reg.intercept_, reg.coef_[0], reg.coef_[1], reg.coef_[2], reg.coef_[3], reg.coef_[4]]
Treynor-Mazuy Model
def gen_new_vars_tm(data):
#get excess return
data['excess_return'] = data['return'] - data['rf']
#For TM model
data['mkt_rf_sq'] = data['mkt_rf']**2
return [data['excess_return'], data['mkt_rf_sq']]
def get_tm1(suibian):
x = suibian[['mkt_rf', 'mkt_rf_sq']]
y = suibian['excess_return']
reg = LinearRegression().fit(x,y)
return [reg.intercept_, reg.coef_[0], reg.coef_[1]]
Henriksson-Merton Model
def gen_new_vars_hm(data):
#get excess return
data['excess_return'] = data['return'] - data['rf']
data['mkt_rf_sq'] = data['mkt_rf']**2
data['mkt_rf_dummy'] = np.where(data['mkt_rf'] < 0, 0, 1)
data['mkt_rf_sq_times_dummy'] = data['mkt_rf_sq'] * data['mkt_rf_dummy']
return [data['excess_return'], data['mkt_rf_sq_times_dummy']]
def get_hm1(data):
x = data[['mkt_rf', 'mkt_rf_sq_times_dummy']]
y = data['excess_return']
reg = LinearRegression().fit(x,y)
return [reg.intercept_, reg.coef_[0]]