同花顺Supermind量化交易 金融时间序列2--GARCH模型 附源代码

从金融时间序列的角度,以 沪深 300的收益作为研究标的,使用GARCH等衍生模型进行探讨和对比,并尝试对价格和波动率进行一定的拟合和预测。

金融时间序列(二)

作者:邱吉尔

1. 输入库包

In [1]:

from scipy import stats
import statsmodels.api as sm
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn')
import arch

2. 取出数据

In [2]:

HS300_close=get_price(['000300.SH'], '20151125', '20190125', '1d', ['close'], True, None,is_panel=1)['close']
HS300_close.columns=['HS300_close']
HS300_close['return']=HS300_close['HS300_close'].pct_change()
HS300_close.dropna(inplace=True)
datetime=pd.to_datetime(HS300_close.index)
fig,ax=plt.subplots(figsize=(20,8))
rect1=ax.plot(datetime,HS300_close['return'],color='IndianRed')
ax.set_ylabel('HS300 Return',size=15)
ax=ax.twinx()
rect2=ax.plot(datetime,HS300_close['HS300_close'])
ax.set_ylabel('HS300 Close',size=15)
plt.title('HS300',size=25)

Out[2]:

<matplotlib.text.Text at 0x7fca33a75f60>

3. GARCH 模型

对于一个对数收益率序列rt,令:


如果at满足以下式子:
 


 


 


则称at服从GARCH模型,其中,{ϵt} 是均值为0,方差为1的独立同分布随机变量序列.

1. 数据集分类,GARCH模型拟合

In [142]:

train_GARCH =HS300_close['return'][:-30]
test_GARCH = HS300_close['return'][-30:]
am_GARCH = arch.arch_model(train_GARCH, mean='AR', lags=14, vol='GARCH') 
res_GARCH = am_GARCH.fit()
Iteration:      1,   Func. Count:     20,   Neg. LLF: -2322.719676744772
Iteration:      2,   Func. Count:     48,   Neg. LLF: -2324.118636827964
Iteration:      3,   Func. Count:     78,   Neg. LLF: -2323.6843401398555
Iteration:      4,   Func. Count:    108,   Neg. LLF: -2324.1246848237706
Iteration:      5,   Func. Count:    133,   Neg. LLF: -2324.1264594013755
Iteration:      6,   Func. Count:    163,   Neg. LLF: -2316.849320551835
Iteration:      7,   Func. Count:    193,   Neg. LLF: -2328.0083395660868
Iteration:      8,   Func. Count:    223,   Neg. LLF: -2327.4637810024988
Iteration:      9,   Func. Count:    251,   Neg. LLF: -2328.0231532432426
Iteration:     10,   Func. Count:    281,   Neg. LLF: -2325.763855473391
Iteration:     11,   Func. Count:    311,   Neg. LLF: -2328.0188277867
Iteration:     12,   Func. Count:    338,   Neg. LLF: -2328.024075604725
Positive directional derivative for linesearch    (Exit mode 8)
            Current function value: -2328.024084172148
            Iterations: 16
            Function evaluations: 338
            Gradient evaluations: 12
/opt/conda/lib/python3.5/site-packages/arch/univariate/base.py:517: ConvergenceWarning: 
The optimizer returned code 8. The message is:
Positive directional derivative for linesearch
See scipy.optimize.fmin_slsqp for code meaning.

  ConvergenceWarning)

In [15]:

res_GARCH.summary()

Out[15]:

AR - GARCH Model Results
Dep. Variable:returnR-squared:0.071
Mean Model:ARAdj. R-squared:0.053
Vol Model:GARCHLog-Likelihood:2328.02
Distribution:NormalAIC:-4620.05
Method:Maximum LikelihoodBIC:-4537.35
No. Observations:731
Date:Tue, Feb 12 2019Df Residuals:713
Time:10:13:35Df Model:18
Mean Model
coefstd errtP>|t|95.0% Conf. Int.
Const4.3118e-043.001e-041.4370.151[-1.570e-04,1.019e-03]
return[1]-0.04263.569e-02-1.1930.233[ -0.113,2.737e-02]
return[2]0.02993.594e-020.8320.406[-4.054e-02, 0.100]
return[3]0.06524.003e-021.6280.103[-1.327e-02, 0.144]
return[4]-0.05823.710e-02-1.5680.117[ -0.131,1.454e-02]
return[5]-0.05753.989e-02-1.4410.150[ -0.136,2.072e-02]
return[6]-0.05283.239e-02-1.6310.103[ -0.116,1.066e-02]
return[7]0.03933.921e-021.0020.316[-3.756e-02, 0.116]
return[8]-0.03063.370e-02-0.9080.364[-9.665e-02,3.545e-02]
return[9]0.08743.520e-022.4841.298e-02[1.845e-02, 0.156]
return[10]-7.3447e-033.654e-02-0.2010.841[-7.897e-02,6.428e-02]
return[11]0.02283.433e-020.6640.507[-4.448e-02,9.008e-02]
return[12]-0.05163.641e-02-1.4180.156[ -0.123,1.972e-02]
return[13]0.06463.598e-021.7967.250e-02[-5.901e-03, 0.135]
return[14]-0.14053.224e-02-4.3561.323e-05[ -0.204,-7.727e-02]
Volatility Model
coefstd errtP>|t|95.0% Conf. Int.
omega2.6238e-064.121e-126.367e+050.000[2.624e-06,2.624e-06]
alpha[1]0.05001.510e-023.3129.261e-04[2.042e-02,7.960e-02]
beta[1]0.93001.063e-0287.4770.000[ 0.909, 0.951]

In [16]:

res_GARCH.params

Out[16]:

Const         0.000431
return[1]    -0.042592
return[2]     0.029893
return[3]     0.065190
return[4]    -0.058172
return[5]    -0.057475
return[6]    -0.052824
return[7]     0.039286
return[8]    -0.030600
return[9]     0.087443
return[10]   -0.007345
return[11]    0.022798
return[12]   -0.051641
return[13]    0.064623
return[14]   -0.140464
omega         0.000003
alpha[1]      0.050010
beta[1]       0.930000
Name: params, dtype: float64

2. GARCH模型预测
前面ARCH模型,我们用来预测了收益率,然而直接预测收益率准确度并不是很高,因此很多时候我们主要用来预测波动率.

In [22]:

resid=res_GARCH.resid[-14:]                 #取出过去14天的残差at
params=np.array(res_GARCH.params[1:15])               #取出均值方程所有系数    
w_params=params[::-1]                       #倒转系数方向
w_params.dot(resid[-14:])

for i in range(30):
    resid_new=test_GARCH.iloc[i]-(res_GARCH.params[0]+w_params.dot(resid[-14:]))
    resid=np.append(resid,resid_new)
resid_final=resid[-30:]
resid_final_square=resid_final**2
resid_final_square

Out[22]:

array([1.31265182e-04, 4.24223468e-04, 2.34277150e-05, 8.61572516e-05,
       7.61609044e-05, 9.93121008e-05, 6.07929980e-05, 9.97465719e-08,
       3.17377608e-05, 1.50034013e-04, 7.63315088e-06, 1.93504633e-05,
       1.31637564e-04, 1.60983709e-05, 6.62189736e-04, 2.51275329e-05,
       1.20477057e-05, 3.81599502e-05, 3.29924979e-06, 6.78092403e-05,
       8.81824346e-05, 3.51298854e-04, 1.00565226e-06, 6.70353037e-05,
       2.03613211e-04, 6.43136485e-05, 1.87956839e-04, 1.15415026e-05,
       4.33123654e-05, 1.57135491e-04])

In [54]:

volatility=res_GARCH.conditional_volatility[-1:]
for i in range(30):
    new_volatility=0.000003+0.05001*resid_final_square[i]+0.93*volatility[-1]
    volatility=np.append(volatility,new_volatility)
    
pre_volatility=volatility[-30:]
pre_volatility

Out[54]:

array([0.01227059, 0.01143586, 0.01063952, 0.00990207, 0.00921573,
       0.0085786 , 0.00798413, 0.00742825, 0.00691286, 0.00643946,
       0.00599208, 0.0055766 , 0.00519582, 0.00483592, 0.00453352,
       0.00422043, 0.00392861, 0.00365851, 0.00340558, 0.00317358,
       0.00295884, 0.00277229, 0.00258128, 0.00240694, 0.00225164,
       0.00210024, 0.00196562, 0.00183161, 0.00170856, 0.00159982])

In [55]:

test_GARCH

Out[55]:

2018-12-13    0.015481
2018-12-14   -0.016704
2018-12-17   -0.001489
2018-12-18   -0.010366
2018-12-19   -0.011923
2018-12-20   -0.007670
2018-12-21   -0.012395
2018-12-24    0.002906
2018-12-25   -0.006885
2018-12-26   -0.005054
2018-12-27   -0.003840
2018-12-28    0.006737
2019-01-02   -0.013658
2019-01-03   -0.001580
2019-01-04    0.023958
2019-01-07    0.006070
2019-01-08   -0.002161
2019-01-09    0.010097
2019-01-10   -0.001881
2019-01-11    0.007190
2019-01-14   -0.008722
2019-01-15    0.019625
2019-01-16    0.000211
2019-01-17   -0.005509
2019-01-18    0.018242
2019-01-21    0.005512
2019-01-22   -0.013284
2019-01-23   -0.000720
2019-01-24    0.005644
2019-01-25    0.008132
Name: return, dtype: float64

In [93]:

HS300_close.index=pd.to_datetime(HS300_close.index)
res_GARCH.conditional_volatility.index=pd.to_datetime(res_GARCH.conditional_volatility.index)

plt.figure(figsize=(20,8))
plt.plot(HS300_close['return'],label='train_return')
plt.plot(res_GARCH.conditional_volatility,label='conditional_volatility')
x=test_GARCH.index
plt.plot(test_GARCH,'black',label='test_return')
plt.plot(x,pre_volatility,'.r',label='predict_volatility')
plt.title('Predict Volatility',size=25)
plt.legend()

Out[93]:

<matplotlib.legend.Legend at 0x7fc9cd798358>

In [ ]:

查看以上策略详细请 到 supermind量化交易官网查看:金融时间序列2--GARCH模型附源代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值