-
Notifications
You must be signed in to change notification settings - Fork 0
/
rolling_test.py
33 lines (31 loc) · 1.45 KB
/
rolling_test.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
import trade_model
import pandas as pd
import numpy as np
from sklearn import svm
from sklearn import linear_model
monthData = trade_model.monthlyModel(5, 2009, 5, 2016, 5, 2014, 5, 2016)
# monthData.monthlyDataDownload()
# monthData.trainFeaturePre()
monthData.trainFeaturePreHd()
trainSpan = len(monthData.xTrain[:,0,0]) - monthData.testSpan
reshapeXtrain = np.zeros((trainSpan*monthData.stockNum, monthData.featureNum))
reshapeYtrain = np.zeros(trainSpan*monthData.stockNum)
for i in range(0, monthData.stockNum):
reshapeXtrain[i*trainSpan:(i+1)*trainSpan, :] = monthData.xTrain[0:trainSpan, :, i]
reshapeYtrain[i*trainSpan:(i+1)*trainSpan] = monthData.yTrain[0:trainSpan, 0, i]
# clf = svm.SVR()
clf = linear_model.LinearRegression()
yearReturn = 1
predictedReturn = np.zeros(monthData.stockNum)
monthlyReturn = np.zeros(monthData.testSpan)
for j in range(1, monthData.testSpan):
clf.fit(reshapeXtrain, reshapeYtrain)
for i in range(0, monthData.stockNum):
# predictedReturn[i] = np.dot(coefficient, monthData.xTest[j, :, i])
predictedReturn[i] = clf.predict(monthData.xTest[j, :, i])
reshapeXtrain[i * trainSpan:(i + 1) * trainSpan, :] = monthData.xTrain[j:trainSpan+j, :, i]
reshapeYtrain[i * trainSpan:(i + 1) * trainSpan] = monthData.yTrain[j:trainSpan+j,0, i]
monthlyReturn[j] = monthData.por10Returns(j, predictedReturn)
yearReturn = yearReturn * (monthlyReturn[j]+1)
print monthlyReturn
print 'overall:', yearReturn