【发布时间】:2016-10-24 02:41:46
【问题描述】:
我正在尝试预测以下内容:
-
list( [close price (current day)-open price (current day)] )
使用以下内容作为输入:
-
list( [open price (current day)-close price (yesterday)] )
但是,我的test_prediction 结果是对错误事物的预测。
sklearn 和 statsmodels 线性回归模型的预测显示输入数据 (test_data) 和 prediction results 之间的相关性约为 100%,而 prediction results 应该与 test_result 相关。
我做错了什么(或遗漏),我该如何解决? 代码将生成 4 个图表,显示不同列表之间的相关性。
###### Working usable example and code below ######
import numpy as np
from plotly.offline import plot
import plotly.graph_objs as go
from sklearn import linear_model
import statsmodels.api as sm
def xy_corr( x, y, fname ):
trace1 = go.Scatter( x = x,
y = y,
mode = 'markers',
marker = dict( size = 6,
color = 'black',
),
showlegend = False
)
layout = go.Layout( title = fname )
fig = go.Figure( data = [trace1],
layout = layout
)
plot( fig, filename = fname + '.html' )
open_p = [23215, 23659, 23770, 23659, 23659, 23993, 23987, 23935, 24380, 24271, 24314, 24018, 23928, 23240, 24193, 23708, 23525, 23640, 23494, 23333, 23451, 23395, 23395, 23925, 23936, 24036, 24008, 24248, 24249, 24599, 24683, 24708, 24510, 24483, 24570, 24946, 25008, 24880, 24478, 24421, 24630, 24540, 24823, 25090, 24610, 24866, 24578, 24686, 24465, 24225, 24526, 24645, 24780, 24538, 24895, 24921, 24743, 25163, 25163, 25316, 25320, 25158, 25375, 25430, 25466, 25231, 25103, 25138, 25138, 25496, 25502, 25610, 25625, 25810, 25789, 25533, 25785, 25698, 25373, 25558, 25594, 25026, 24630, 24509, 24535, 24205, 24465, 23847, 24165, 23840, 24216, 24355, 24158, 23203, 23285, 23423, 23786, 23729, 23944, 23637]
close_p = [23656, 23758, 23663, 23659, 23989, 23978, 24142, 24152, 24279, 24271, 24393, 23942, 23640, 24102, 23710, 23708, 23705, 23693, 23561, 23441, 23395, 23395, 23990, 23900, 24158, 24188, 24241, 24248, 24699, 24678, 24715, 24523, 24486, 24483, 24947, 24904, 24923, 24478, 24434, 24421, 24409, 24705, 25047, 24642, 24875, 24866, 24698, 24463, 24262, 24396, 24633, 24645, 24528, 24895, 24895, 24839, 25178, 25163, 25315, 25323, 25149, 25387, 25375, 25469, 25231, 25073, 25138, 25138, 25448, 25611, 25705, 25623, 25813, 25798, 25560, 25518, 25743, 25305, 25654, 25579, 25315, 24783, 24508, 24532, 24208, 24176, 24047, 24148, 24165, 24159, 24286, 24249, 23635, 23128, 23438, 23869, 23420, 23756, 23705, 24018]
open_prev_close_diff = np.array( [ open_p[i] - close_p[i-1] for i in range( 1, len( open_p ) )] )[np.newaxis].T
open_current_close_diff = np.array( [close_p[i] - open_p[i] for i in range( 1, len( open_p ) )] )
train_data = open_prev_close_diff[ :80]
test_data = open_prev_close_diff[80:]
train_result = open_current_close_diff[ :80]
test_result = open_current_close_diff[80:]
regressor = linear_model.LinearRegression()
regressor.fit( train_data, train_result )
test_prediction = np.array( [int(i) for i in regressor.predict( test_data )] )
xy_corr( [int(i) for i in test_result], test_prediction, 'known_result_and_prediction_result_sklearn')
xy_corr( [int(i) for i in test_data], test_prediction, 'input_data_and_prediction_result_sklearn' )
olsmod = sm.OLS( train_result, train_data )
olsres = olsmod.fit()
test_prediction = np.array( [int(i) for i in olsres.predict( test_data )] )
xy_corr( [int(i) for i in test_result], test_prediction, 'known_result_and_prediction_result_smOLS')
xy_corr( [int(i) for i in test_data], test_prediction, 'input_data_and_prediction_result_smOLS' )
【问题讨论】:
-
您介意发布指向您的四边形图的链接吗?
标签: python statistics scikit-learn statsmodels algorithmic-trading