-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiFactorModel.py
381 lines (310 loc) · 14.9 KB
/
MultiFactorModel.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 23 10:51:42 2016
@author: chongwee
"""
import csv
from yahoo_finance import Share
import os
from sys import exit
import datetime as dt
import calendar
import requests
import matplotlib.pyplot as plt
import statsmodels.formula.api as sm
import statsmodels.api as sma
import pandas as pd
from sklearn.metrics import mean_squared_error
def readSymbolsCSV(filepath):
symbols = []
with open(filepath, 'r', newline='\n', encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
symbols.append(row[0])
f.close()
return symbols
def readDatesCSV(filepath):
dates = []
with open(filepath, 'r', newline='\n', encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
dates.append(row[0])
f.close()
return dates
def findLastTradingDayInPeriods(start,end,allTradingDates,frequency):
startDate = dt.datetime.strptime(start, '%Y-%m-%d')
endDate = dt.datetime.strptime(end, '%Y-%m-%d')
lastTradingDays = []
date = startDate
allDatesInPeriod = []
while date <= endDate:
allDatesInPeriod.append(date.strftime('%Y-%m-%d'))
if meetsDateRequirements(date,frequency):
tradingDaysInPeriod = list(set(allTradingDates).intersection(allDatesInPeriod))
if len(tradingDaysInPeriod) > 0:
lastTradingDays.append(max(tradingDaysInPeriod)) #append last/max trading day in period
allDatesInPeriod = []
date += dt.timedelta(days=1)
return lastTradingDays
def meetsDateRequirements(date,frequency):
if frequency == "daily":
return True
elif frequency == "weekly":
if date.isoweekday() == 7:
return True
else:
return False
elif frequency == "monthly":
lastDayInMonth = calendar.monthrange(date.year,date.month)[1]
if date.day == lastDayInMonth:
return True
else:
return False
elif frequency == "yearly":
if date.day == 31 and date.month == 12:
return True
else:
return False
else:
print("Invalid frequency parameter passes to meetsDateRequirements")
exit(0); #exit program due to invalid input (neither daily, weekly, monthly or yearly)
def retrieveQuoteFromGoogle(symbol,start_date,end_date):
start = dt.date(int(start_date[0:4]),int(start_date[5:7]),int(start_date[8:10]))
end = dt.date(int(end_date[0:4]),int(end_date[5:7]),int(end_date[8:10]))
url_string = "http://www.google.com/finance/historical?q={0}".format(symbol)
url_string += "&startdate={0}&enddate={1}&output=csv".format(start.strftime('%b %d, %Y'),end.strftime('%b %d, %Y'))
response = requests.get(url_string)
quoteDict = {}
if response.status_code == 200:
open('temp.csv', 'wb').write(response.content)
with open('temp.csv', 'r', newline='\n', encoding="utf-8") as f:
reader = csv.reader(f)
reader.next()
for row in reader:
date = dt.datetime.strptime(row[0], '%d-%b-%y')
dateStr = date.strftime('%Y-%m-%d')
quoteDict[dateStr] = float(row[4])
f.close()
else:
raise Exception('Unable to find quote on Google Finance')
print(quoteDict)
return quoteDict #return close price from last trading day of week since it might not be friday
def retrieveQuoteFromYahoo(symbol,start,end):
share = Share(symbol)
quoteList = share.get_historical(start,end)
quoteDict = {}
for quote in quoteList:
quoteDict[quote['Date']] = float(quote['Adj_Close'])
return quoteDict
def retrieveHistoricalQuotes(symbol,start,end):
print("Retrieving historical prices for {0}...".format(symbol))
if checkFileExists(symbol,start,end):
return readQuotesFromCSV(symbol,start,end)
else:
quoteDict = {}
try:
quoteDict = retrieveQuoteFromGoogle(symbol,start,end)
except:
quoteDict = retrieveQuoteFromYahoo(symbol,start,end)
writeQuotesToCSV(symbol,start,end,quoteDict)
return quoteDict
def readQuotesFromCSV(symbol,start,end):
quotes = {}
directory = "quotes"
filename = "{0}_{1}_{2}.csv".format(symbol,start,end)
with open(os.path.join(directory,filename), 'r', newline='\n', encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
quotes[row[0]] = float(row[1])
f.close()
return quotes
def writeQuotesToCSV(symbol,start,end,quotes):
directory = "quotes"
if not os.path.exists(directory):
os.makedirs(directory)
filename = "{0}_{1}_{2}.csv".format(symbol,start,end)
with open(os.path.join(directory,filename), 'w', newline="\n", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
dates = quotes.keys()
for date in sorted(dates):
cells = [date,quotes[date]]
writer.writerow(cells)
csvfile.close()
def checkFileExists(symbol,start,end):
directory = "quotes"
filename = "{0}_{1}_{2}.csv".format(symbol,start,end)
if not os.path.exists(os.path.join(directory,filename)):
return False
else:
return True
def readNameSectorCSV(filepath):
stockDictionary = {}
with open(filepath, 'r', newline='\n', encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
nameSectorDictionary = {}
nameSectorDictionary['Name'] = row[1]
nameSectorDictionary['Sector'] = row[2]
stockDictionary[row[0]] = nameSectorDictionary
f.close()
return stockDictionary
def retrieveSectorSymbol(sectorName):
switcher = {
"Consumer Discretionary" : "XLY",
"Consumer Staples": "XLP",
"Energy": "XLE",
"Financial Services": "XLFS",
"Financials": "XLF",
"Health Care": "XLV",
"Industrials": "XLI",
"Materials": "XLB",
"Real Estate": "XLRE",
"Information Technology": "XLK", #both info tech and telecomms point to XLK
"Telecommunications Services": "XLK", #both info tech and telecomms point to XLK
"Utilities": "XLU"
}
return switcher[sectorName]
def writeToFile(filename, text):
outputFile = open(filename, "w", encoding="utf-8")
outputFile.write(text)
outputFile.close()
def performAnalysis(symbolsFilename,startDate,endDate,analysisStartDate,analysisEndDate,analysisPeriod,benchmarkSymbol,outputFilename):
quotes = retrieveHistoricalQuotes("^DJI",startDate,endDate)
allTradingDays = sorted(quotes.keys())
dates = findLastTradingDayInPeriods(analysisStartDate,analysisEndDate,allTradingDays,analysisPeriod)
names_sectors = readNameSectorCSV('sectors.csv')
benchmarkQuotes = retrieveHistoricalQuotes(benchmarkSymbol,startDate,endDate)
sectorQuotes = {}
sectors = ['XLY','XLP','XLE','XLF','XLV','XLI','XLB','XLK','XLU']
for sector in sectors:
sectorQuotes[sector] = retrieveHistoricalQuotes(sector,startDate,endDate)
#start by getting the list of symbols to be considered for shortlisting
symbols = sorted(readSymbolsCSV(symbolsFilename))
#retrieve historical prices and calculate returns
equitiesReturns = {}
benchmarkReturns = []
sectorReturns = {}
df = pd.DataFrame({"Dates":dates[1:]})
for symbol in symbols:
quotes = retrieveHistoricalQuotes(symbol,startDate,endDate)
sector = names_sectors[symbol]['Sector']
sectorSymbol = retrieveSectorSymbol(sector)
stockReturns = []
sectorReturn = []
previousTime = dates[0]
for currentTime in dates[1:]:
try:
prev = quotes[previousTime]
curr = quotes[currentTime]
stockReturn = (curr-prev)/prev
stockReturns.append(stockReturn) #multiply by 100 if you work using percent
benchmarkPrev = benchmarkQuotes[previousTime]
benchmarkCurr = benchmarkQuotes[currentTime]
benchmarkReturn = (benchmarkCurr-benchmarkPrev)/benchmarkPrev
if len(equitiesReturns) == 0:
benchmarkReturns.append(benchmarkReturn) #multiply by 100 if you work using percent
sectorPrev = sectorQuotes[sectorSymbol][previousTime]
sectorCurr = sectorQuotes[sectorSymbol][currentTime]
sectorExcessReturn = (sectorCurr-sectorPrev)/sectorPrev
sectorExcessReturn = sectorExcessReturn - benchmarkReturn
sectorReturn.append(sectorExcessReturn) #multiply by 100 if you work using percent
except KeyError:
raise ValueError("Missing quotes for {0} between {1} and {2}".format(symbol,previousTime,currentTime))
previousTime = currentTime
equitiesReturns[symbol] = stockReturns
sectorReturns[sectorSymbol] = sectorReturn
df[symbol] = stockReturns
df[sectorSymbol] = sectorReturns[sectorSymbol]
df["Benchmark"] = benchmarkReturns
print(df.head())
analysisComponents = [1,2,3,4]
if 1 in analysisComponents:
print("\n\n========== Single Factor Model (Market) ==========\n\n")
summaries = ""
adjrsquaredValues = {}
for symbol in symbols:
name = names_sectors[symbol]['Name']
print("\n===== Regression Results ({} / {}) =====\n".format(name,"DJI"))
result = sm.ols(formula='{} ~ Benchmark'.format(symbol), data=df).fit()
print("Adj R-squared:\t{}".format(result.rsquared_adj))
print("Coefficient:\n{}".format(result.params))
print("P-Value:\n{}".format(result.pvalues))
summaries += (name + "\n\n" + result.summary().as_text() + "\n\n")
plt.plot(df["Benchmark"], df[symbol], 'ro')
plt.plot(df["Benchmark"], result.fittedvalues, 'b')
plt.ylim(-0.4, 0.4)
plt.legend(['Data', 'Fitted model'])
plt.xlabel("Benchmark Returns (DJI)")
plt.ylabel("Stock Returns ({})".format(symbol))
plt.title("Linear Regression ({} / DJI)".format(name))
plt.savefig("{}.png".format(symbol))
plt.show()
adjrsquaredValues[symbol] = result.rsquared_adj
keys = sorted(adjrsquaredValues.keys())
for symbol in keys:
name = names_sectors[symbol]['Name']
print("{}|{}|{}".format(symbol,name,adjrsquaredValues[symbol]))
writeToFile("lm.txt",summaries)
if 2 in analysisComponents:
print("\n\n========== Two Factor Model (Market, Sector) - 5 years ==========\n\n")
adjrsquaredValues = {}
summaries = ""
for symbol in symbols:
sector = names_sectors[symbol]['Sector']
sectorSymbol = retrieveSectorSymbol(sector)
name = names_sectors[symbol]['Name']
print("\n===== Regression Results ({} / {}) =====\n".format(name,sector))
result = sm.ols(formula='{} ~ Benchmark + {}'.format(symbol,sectorSymbol), data=df).fit()
print("Adj R-squared:\t{}".format(result.rsquared_adj))
print("Coefficients:\n{}".format(result.params))
print("P-Values:\n{}".format(result.pvalues))
summaries += (name + "\n\n" + result.summary().as_text() + "\n\n")
adjrsquaredValues[symbol] = result.rsquared_adj
keys = sorted(adjrsquaredValues.keys())
for symbol in keys:
name = names_sectors[symbol]['Name']
sector = names_sectors[symbol]['Sector']
print("{}|{}|{}|{}".format(symbol,name,sector,adjrsquaredValues[symbol]))
writeToFile("lm2.txt",summaries)
if 3 in analysisComponents:
print("\n\n========== Two Factor Model (Market, Sector) - Out-of-Sample Predictions ==========\n\n")
mseValues = {}
for symbol in symbols:
sector = names_sectors[symbol]['Sector']
sectorSymbol = retrieveSectorSymbol(sector)
name = names_sectors[symbol]['Name']
print("\n===== Regression Results ({} / {}) =====\n".format(name,sector))
predictions = []
actual = []
for i in range(48,60):
result = sm.ols(formula='{} ~ Benchmark + {}'.format(symbol,sectorSymbol), data=df.head(i)).fit()
testdf = df[['Benchmark',sectorSymbol]]
prediction = result.predict(testdf[i:i+1])
predictions.append(prediction[0])
actual.append(df.iloc[i][symbol])
print("Actual values:\t{}".format(actual))
print("Predicted values:\t{}".format(predictions))
print("MSE Out-of-sample:\t{}".format(mean_squared_error(actual, predictions)))
mseValues[symbol] = mean_squared_error(actual, predictions)
keys = sorted(mseValues.keys())
for symbol in keys:
name = names_sectors[symbol]['Name']
sector = names_sectors[symbol]['Sector']
print("{}|{}|{}|{}".format(symbol,name,sector,mseValues[symbol]))
if 4 in analysisComponents:
print("\n\n========== ARMA Model ==========\n\n")
bicValues = {}
for symbol in symbols:
AR_lag = 1
MA_lag = 1
data = df[symbol].values
model = sma.tsa.ARMA(data, (AR_lag, MA_lag)).fit(transparams=False)
print(model.summary())
bicValues[symbol] = model.bic
keys = sorted(bicValues.keys())
for symbol in keys:
bic = bicValues[symbol]
name = names_sectors[symbol]['Name']
sector = names_sectors[symbol]['Sector']
print("{}|{}|{}|{}".format(symbol,name,sector,bic))
print("Analysis Complete.")