-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_sklearn.py
148 lines (115 loc) · 3.65 KB
/
test_sklearn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import pandas as pd
import numpy as np
from pandas import DataFrame
from sklearn import linear_model
# read data
def read_data():
test = pd.read_excel('Fund of Funds-US Equity.xlsx')
fffactor= pd.read_excel('FactorReturn.xlsx')
newfactor= pd.read_excel('Russell Factor Returns 84 to 16.xlsx')
raw_cpi = pd.read_csv('CPI.csv')
return test, fffactor, newfactor, raw_cpi
# create Fama-French factor
def create_ff(fffactor, newfactor):
fffactor = fffactor[fffactor.columns[:-1]]
newfactor['yymm'] = newfactor['yymm'] // 100
newfactor[newfactor.columns[1:3]] = newfactor[newfactor.columns[1:3]] * 100
merged = pd.merge(fffactor, newfactor)
return merged
# create cpi
def create_cpi(raw_cpi):
raw_cpi = raw_cpi[raw_cpi.columns[:-2]]
cpi = DataFrame(columns=('yymm', 'cpi'))
for i in range(0, raw_cpi.shape[0]):
year = raw_cpi.iloc[i,0]
for j in range(1,13):
cpi_value = raw_cpi.iloc[i,j]
month = j
yymm = year * 100 + month
cpi.loc[i*12 + (j-1)] = [yymm, cpi_value]
cpi['yymm'] = cpi['yymm'].astype(np.int32)
cpi['cpi'] = cpi['cpi'] / 202.6
cpi = cpi.dropna()
return cpi
# create test data
def make_yymm(start_year,start_month,end_year,end_month):
yymm = []
year = start_year
month = start_month
while (year < end_year) or (year == end_year and month <= end_month):
yymm.append(year * 100 + month)
if month == 12:
year = year + 1
month = 1
else:
month += 1
return yymm
def extract_data(row, yymm):
fundname = row[0]
monthly_return = row[34:347]
d = {'yymm': yymm, 'mret':monthly_return}
fund = pd.DataFrame(d)
return fundname, fund
def create_test_data(fund, merged):
test_data = pd.merge(merged,fund)
test_data = test_data.dropna()
test_data['mret'] = test_data['mret'].astype(np.float64)
return test_data
def fit_ff3(data):
lm = linear_model.LinearRegression()
X = data[['Rm3-Rf','SMB3','HML3']]
y = data['mret']
lm.fit(X,y)
intercept = lm.intercept_
r2 = lm.score(X,y)
return intercept, r2
def fit_ff4(data):
lm = linear_model.LinearRegression()
X = data[['Rm3-Rf','Small-Mid','Mid-Large', 'HML3']]
y = data['mret']
lm.fit(X,y)
intercept = lm.intercept_
r2 = lm.score(X,y)
return intercept, r2
def fit_ff5(data):
lm = linear_model.LinearRegression()
X = data[['Mkt5-RF','SMB5','HML5','RMW5','CMA5']]
y = data['mret']
lm.fit(X,y)
intercept = lm.intercept_
r2 = lm.score(X,y)
return intercept, r2
def fit_ff6(data):
lm = linear_model.LinearRegression()
X = data[['Mkt5-RF','Small-Mid','Mid-Large','HML5','RMW5','CMA5']]
y = data['mret']
lm.fit(X,y)
intercept = lm.intercept_
r2 = lm.score(X,y)
return intercept, r2
def output(test, merged):
yymm = make_yymm(1990,6,2016,6)
w = open('result.csv', 'w+')
for i in range(0, test.shape[0]):
fundname, fund = extract_data(test.loc[i], yymm)
w.write(fundname.encode('utf-8'))
w.write('\n')
test_data = create_test_data(fund, merged)
intercept,r2 = fit_ff3(test_data)
w.write('Three Factor, %s, %s\n' %(intercept, r2))
intercept, r2 = fit_ff4(test_data)
w.write('Four Factor, %s, %s\n' %(intercept, r2))
intercept, r2 = fit_ff5(test_data)
w.write('Five Factor, %s, %s\n' %(intercept, r2))
intercept, r2 = fit_ff6(test_data)
w.write('Six Factor, %s, %s\n' %(intercept, r2))
w.write('\n')
w.close()
def main():
test, fffactor, newfactor, raw_cpi = read_data()
merged = create_ff(fffactor, newfactor)
merged = rename(merged)
output(test, merged)
if __name__ == '__main__':
main()