Skip to content

Commit 0194041

Browse files
committed
时间序列模型预测股价,未完成。
1 parent c394130 commit 0194041

15 files changed

+151
-0
lines changed

Diff for: output/PSO_svm.png

-16 KB
Binary file not shown.
Binary file not shown.

Diff for: output/SVR(kernel='linear')_test.png

-40.9 KB
Binary file not shown.

Diff for: output/SVR_test.png

-42.8 KB
Binary file not shown.

Diff for: output/acf.png

12.1 KB
Loading

Diff for: output/backtest_returns.png

-24.1 KB
Binary file not shown.

Diff for: output/backtest_value.png

-16.9 KB
Binary file not shown.

Diff for: output/experiment.png

-43.9 KB
Binary file not shown.

Diff for: output/ga_results.png

-26.4 KB
Binary file not shown.

Diff for: output/pacf.png

11.7 KB
Loading

Diff for: output/predict_arma.png

64.1 KB
Loading

Diff for: output/rawdata.png

50.1 KB
Loading

Diff for: output/test.png

-37.1 KB
Binary file not shown.

Diff for: output/test_bt.png

-14.7 KB
Binary file not shown.

Diff for: ts.py

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# coding:utf-8
2+
# 时间序列模型ARMA预测股价
3+
4+
5+
import numpy as np
6+
import pandas as pd
7+
import matplotlib.pyplot as plt
8+
from sklearn.preprocessing import MinMaxScaler
9+
import tools
10+
import run
11+
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
12+
from statsmodels.tsa.arima_model import ARMA
13+
from statsmodels.tsa.arima_model import _arma_predict_out_of_sample as out_predict
14+
import warnings
15+
warnings.filterwarnings('ignore', 'statsmodels.tsa.arima_model.ARMA', FutureWarning)
16+
17+
18+
# 准备数据
19+
def readData(code="sh000300", start="2018-01-01", end="2018-12-31", refresh = False):
20+
# 加载数据
21+
tools.initOutput()
22+
data = tools.loadData(code=code, start=start, end=end, refresh=refresh)
23+
# 筛选特征
24+
feature_cols = ["close"]
25+
features_data = data.loc[:, feature_cols]
26+
scaler = MinMaxScaler(feature_range=(1, 2))
27+
features = scaler.fit_transform(features_data.values)
28+
x = []
29+
for i in range(len(features)):
30+
x.append(features[i][0])
31+
new_data = pd.DataFrame({"close":x})
32+
new_data["date"] = data.index
33+
new_data.set_index("date", inplace = True)
34+
print(new_data.head())
35+
return new_data
36+
37+
38+
# 画数据
39+
@run.change_dir
40+
def draw(data):
41+
plt.figure()
42+
plt.plot(data.close)
43+
plt.savefig("./output/rawdata.png")
44+
# 画自相关函数
45+
plot_acf(data.close, lags=25, title="acf")
46+
plt.savefig("./output/acf.png")
47+
# 画自相关函数
48+
plot_pacf(data.close, lags=25, title="pacf")
49+
plt.savefig("./output/pacf.png")
50+
plt.close()
51+
52+
53+
# 将数据差分
54+
def diff_data(data):
55+
diff = data.diff(1).dropna()
56+
return diff
57+
58+
59+
# 寻找参数p和q
60+
def findPQ(data):
61+
# N = len(data)
62+
best = float("inf")
63+
bestp, bestq = -1, -1
64+
N = 20
65+
for p in range(1, N):
66+
for q in range(1, N):
67+
try:
68+
arma = ARMA(data, (p, q)).fit(disp = -1)
69+
aic = arma.aic
70+
print(p, q, aic)
71+
if aic < best:
72+
best = aic
73+
bestp = p
74+
bestq = q
75+
except:
76+
print("出现异常")
77+
return p, q
78+
79+
80+
# 用ARMA模型建模并预测
81+
@run.change_dir
82+
@run.timethis
83+
def arma_model(train_data, p, q):
84+
data = readData(start = "2019-01-01", end = "2019-12-31")
85+
data = diff_data(data)
86+
model = ARMA(train_data, (p, q)).fit()
87+
print(model.summary)
88+
print(model.conf_int())
89+
history_p = train_data
90+
pred_p = []
91+
print("测试", history_p)
92+
for i in range(len(data)):
93+
model_p = ARMA(train_data, p, q)
94+
model_fit_p = model_p.fit(disp = -1)
95+
yhat_p = model_fit_p.predict(start = len(history_p), end = len(history_p))[0]
96+
pred_p.append(yhat_p)
97+
history_p.append(yhat_p)
98+
print("测试", yhat_p)
99+
results = pd.DataFrame()
100+
results["pred"] = pred_p
101+
results["real"] = data
102+
results["date"] = data.index
103+
results.set_index("date", inplace = True)
104+
print("预测结果")
105+
print(results.head())
106+
# # predict = model.forecast()
107+
# print("预测")
108+
# plt.figure()
109+
# model.plot_predict(start = 10, end = 200)
110+
# plt.savefig("./output/predict_arma.png")
111+
# plt.close()
112+
# # print(model.forecast(5))
113+
# pred = model.predict()
114+
# # 提取参数
115+
# params = model.params
116+
# residuals = model.resid
117+
# p = model.k_ar
118+
# q = model.k_ma
119+
# k_exog = model.k_exog
120+
# k_trend = model.k_trend
121+
# steps = 1
122+
123+
# # 样本内数据验证
124+
# in_data = train_data.iloc[:11, :]
125+
# in_resid = residuals.iloc[0:11]
126+
# a = out_predict(params, steps, in_resid, p, q, k_trend, k_exog, endog = in_data["close"], exog = None, start = len(in_data))
127+
# test_pred = pred[8:13]
128+
# print(test_pred, a[0])
129+
# # 样本外预测
130+
# new_resid = residuals.tail(9).copy()
131+
# new_data = train_data.tail(9).copy()
132+
# for i in range(len(data)):
133+
# print(i)
134+
# a = out_predict(params, steps, in_resid, p, q, k_trend, k_exog, endog = new_data["close"], exog = None, start = len(new_data))
135+
# tomorrow_index = data.index[i]
136+
# temp_resid = data.loc[tomorrow_index,'close'] - a[0]
137+
# new_resid[tomorrow_index] = temp_resid
138+
# new_resid.drop(new_resid.index[0],inplace=True)
139+
# new_data = new_data.append(data.iloc[i,:])
140+
# # new_data.drop(data.index[0], inplace=True)
141+
# pred[tomorrow_index] = a[0]
142+
143+
144+
if __name__ == "__main__":
145+
data = readData()
146+
draw(data)
147+
data = diff_data(data)
148+
draw(data)
149+
# p, q = findPQ(data)
150+
arma_model(data, 9, 9)
151+

0 commit comments

Comments
 (0)