Financial Engineering – Intern Project

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

    \[r_{i}^e = \alpha + \beta_1 r_{m}^e + \beta_2 SMB + \beta_3 HML + \epsilon_{i}\]

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

    \[r_{i}^e = \alpha_{i} + \beta_1 r_{m}^e + \beta_2 SMB + \beta_3 HML+ \beta_4 RMW + \beta_5 CMA + \epsilon_{i}\]

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

    \[r_{i}^e = \alpha + \beta_1 r_m^e + \beta_2 (r_m^e)^2 + \epsilon_i\]

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

    \[r_i^e = \alpha + \beta_1 r_m^e + D\cdot \beta_2 (r_m^e)^2 \; where \; D = \mathbb{I}_{r_m^e > 0}\]

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]]

Information Ratio


Posted