机器学习实战3.5之预测股票走势

import pandas as pd
import pandas_datareader
import datetime
import matplotlib.pylab as plt
import seaborn as sns
from matplotlib.pylab import style
from statsmodels.tsa.arima_model import ARIMA
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf

style.use('ggplot')    
plt.rcParams['font.sans-serif'] = ['SimHei'] 
plt.rcParams['axes.unicode_minus'] = False  
stockFilePath = './data/T10yr.csv'
stock = pd.read_csv(stockFilePath, index_col=0, parse_dates=[0])
#中间参数表示为时间序列加上对应的索引值,从0开始;
#后一个参数表示将时间序列解析成标准格式
print(stock.head())
print(stock.tail())
             Open   High    Low  Close  Volume  Adj Close
Date                                                     
2000-01-03  6.498  6.603  6.498  6.548       0      6.548
2000-01-04  6.530  6.548  6.485  6.485       0      6.485
2000-01-05  6.521  6.599  6.508  6.599       0      6.599
2000-01-06  6.558  6.585  6.540  6.549       0      6.549
2000-01-07  6.545  6.595  6.504  6.504       0      6.504
             Open   High    Low  Close  Volume  Adj Close
Date                                                     
2016-07-25  1.584  1.584  1.554  1.571       0      1.571
2016-07-26  1.559  1.587  1.549  1.563       0      1.563
2016-07-27  1.570  1.570  1.511  1.515       0      1.515
2016-07-28  1.525  1.535  1.493  1.511       0      1.511
2016-07-29  1.525  1.530  1.458  1.458       0      1.458
stock_week = stock['Close'].resample('W-MON').mean()
stock_train = stock_week['2000':'2015']
stock_train.plot(figsize=(12,8))
plt.legend(bbox_to_anchor=(1.25, 0.5))
plt.title('Stock close')
sns.despine()

机器学习实战3.5之预测股票走势_第1张图片

首先做差分,满足最基本的平稳性

stock_diff = stock_train.diff()
stock_diff = stock_diff.dropna()

plt.figure(figsize=(12,8))
plt.plot(stock_diff)
plt.title('一阶差分')
plt.show()

机器学习实战3.5之预测股票走势_第2张图片

acf = plot_acf(stock_diff, lags=20)
plt.title('ACF')
acf.show()
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
  % get_backend())

机器学习实战3.5之预测股票走势_第3张图片

pacf = plot_pacf(stock_diff, lags=20)
plt.title('PACF')
pacf.show()
C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
  % get_backend())

机器学习实战3.5之预测股票走势_第4张图片

model = ARIMA(stock_train, order=(1,1,1),freq='W-MON')
result = model.fit()
print(result.summary())
                             ARIMA Model Results                              
==============================================================================
Dep. Variable:                D.Close   No. Observations:                  834
Model:                 ARIMA(1, 1, 1)   Log Likelihood                 720.846
Method:                       css-mle   S.D. of innovations              0.102
Date:                Fri, 29 Mar 2019   AIC                          -1433.692
Time:                        15:41:45   BIC                          -1414.787
Sample:                    01-10-2000   HQIC                         -1426.444
                         - 12-28-2015                                         
=================================================================================
                    coef    std err          z      P>|z|      [0.025      0.975]
---------------------------------------------------------------------------------
const            -0.0052      0.005     -1.145      0.252      -0.014       0.004
ar.L1.D.Close     0.2841      0.203      1.397      0.163      -0.115       0.683
ma.L1.D.Close    -0.0869      0.213     -0.408      0.683      -0.504       0.330
                                    Roots                                    
=============================================================================
                  Real          Imaginary           Modulus         Frequency
-----------------------------------------------------------------------------
AR.1            3.5199           +0.0000j            3.5199            0.0000
MA.1           11.5087           +0.0000j           11.5087            0.0000
-----------------------------------------------------------------------------
pred = result.predict('20140609', '20160609',dynamic=True, typ='levels')
print(pred)


你可能感兴趣的:(机器学习实战)