繁簡切換您正在訪問的是FX168財經網,本網站所提供的內容及信息均遵守中華人民共和國香港特別行政區當地法律法規。

FX168财经网>人物频道>帖子

基于python的时间序列分析学习(二)

作者/lcb173364 2019-05-10 03:13 0 来源: FX168财经网人物频道

本篇利用Python实现了ARCH相关函数的实现,包括ARCH效应检验和模型建立,以及GARCH模型的建立和波动率的预测,还介绍了GARCH的推广,例如GJR-GARCH,TARCH,EGARCH等。

摘要

1 ARCH模型

1.1 ARCH模型简介

1.2 ARCH效应检验

1.3 ARCH模型建立

2. GARCH模型

2.1 GARCH模型简介

2.2 GARCH模型波动率预测

2.2.1 根据均值方程预测atatatatata_t
2.2.2 根据波动率方程预测σ2tσ2tσ2tσt2σ2t\sigma^2_t

3. GARCH模型的推广

3.1 GJR-GARCH

3.2 TARCH

3.3 T学生分布

3.4 EGARCH

后记

from scipy import statsimport statsmodels.api as smfrom jqdata import macroimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltmatplotlib.rcParams['axes.unicode_minus']=False # 解决负号显示异常的问题import arch

1. ARCH模型¶

之前介绍的ARMA相关的模型主要基于同方差的假设,但是在实际情况中,同方差并不是一个很容易满足的条件。因此考虑波动率的变化情况是十分必要的。本篇介绍资产收益波动率的建模和统计方法,着眼于考虑波动率的变化情况,因此称之为条件异方差模型,即ARCH模型。

虽然波动率不可直接观测,但是在一些资产收益率时间序列中却普遍存在以下情况: (1)存在波动率聚集,也就是波动率可能在一些时间段上高,另一些时间段上低。 (2)波动率以连续方式随时间变化,即波动率跳跃很少见。 (3)波动率不发散到无穷,即波动率在固定范围内变化,从统计学角度说,这意味着波动率往往是平稳的。 (4)波动率对价格大幅上升和价格大幅下降的反应不同,这种现象称为杠杆效应。

1.1 ARCH模型简介¶

ARCH模型的基本思想是:资产收益率的扰动$a_t$是序列不相关的,但不是独立的;$a_t$的不独立性可以用其延迟值的简单二次函数来描述。具体来说,一个m阶的ARCH模型是假定:            $$a_t = \sigma_t \epsilon_t \\              \sigma^2_t = \alpha_0 + \alpha_1 a^2_{t-1} + \cdots + \alpha_m a^2_{t-m}$$ 其中,$\{ \epsilon_t \}$是均值为0,方差为1的独立同分布随机变量序列,$\alpha_0 >0, \alpha_1, \cdots, \alpha_m \geq 0$. 从上式中可以看出,过去的平方扰动$\{ a^2_{t-i} \}^m_{i=1}$会影响方差项$\{ \sigma^2_t \}$;也就是,大的扰动会倾向于紧接着出现另一个大的扰动,这很好的刻画了波动率聚集现象。

仍然以上证指数日涨跌幅数据为例:

# 获取股票涨跌幅def get_pct_change(scu,count,date,fqy='1d'):df=get_price(security=scu,count=count+1,end_date=date,fields=['close'],frequency=fqy)return df['close',:,:].pct_change().iloc[1:,:]
XSHG_chgpct = get_pct_change(scu=['000001.XSHG'], count=500, date='2016-12-31',fqy='1d')XSHG_chgpct.plot(figsize = (15,6), grid = True)
<matplotlib.axes._subplots.AxesSubplot at 0x7fbb99656250>
t = sm.tsa.stattools.adfuller(XSHG_chgpct['000001.XSHG'])  # ADF检验print "p-value:   ",t[1]
p-value:    9.41981252875e-07

p<0.05, 认为序列是平稳的,建立AR(p)模型,并判断阶数。

fig = plt.figure(figsize=(15,6))ax1=fig.add_subplot(111)fig = sm.graphics.tsa.plot_pacf(XSHG_chgpct,lags = 10,ax=ax1)
/opt/conda/envs/python2/lib/python2.7/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == str('face'):
#根据上图,建立AR(4)模型XSHG_chgpct_model = sm.tsa.ARMA(XSHG_chgpct, (4,0)).fit()

1.2 ARCH效应检验¶

同上篇文章自相关函数的序列检验方法,计算均值方程的残差: $$a_t = r_t - \mu_t$$并作出残差和残差平方图像:

at = XSHG_chgpct - XSHG_chgpct_model.fittedvaluesat2 = np.square(at)plt.figure(figsize=(10,6))plt.subplot(211)plt.plot(at,label = 'at')plt.legend()plt.subplot(212)plt.plot(at2,label='at^2')plt.legend(loc=0)
/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/frame.py:3200: FutureWarning: TimeSeries broadcasting along DataFrame index by default is deprecated. Please use DataFrame.<op> to explicitly broadcast arithmetic operations along the index
  FutureWarning)
<matplotlib.legend.Legend at 0x7fbb8d616f50>
acf,q,p = sm.tsa.acf(at2,nlags=20,qstat=True)  ## 计算自相关系数及p-valueout = np.c_[range(1,21), acf[1:], q, p]output=pd.DataFrame(out, columns=['lag', "AC", "Q", "P-value"])output = output.set_index('lag')output

ACQP-value
lag


10.19807819.7354618.893571e-06
20.24848350.8553809.055128e-12
30.22296175.9613212.254517e-16
40.20211796.6340705.118884e-20
50.160051109.6234294.921288e-22
60.092943114.0125382.941310e-22
70.127734122.3194652.518106e-23
80.091204126.5630571.457495e-23
90.071239129.1573831.767276e-23
100.109735135.3257703.816124e-24
110.079323138.5555083.203215e-24
120.118699145.8023064.020897e-25
130.185349163.5085633.837089e-28
140.116069170.4663095.512235e-29
150.097599175.3960222.011711e-29
160.186076193.3520041.742687e-32
170.100148198.5640845.564529e-33
180.103919204.1877541.454536e-33
190.094940208.8913355.757735e-34
200.135340218.4694902.412120e-35

p<0.05,认为残差序列具有相关性,符合异方差的假设,因此具有ARCH效应。

1.3 ARCH模型建立¶

用$\{ a^2_t \}$序列偏自相关函数(pACF)确定:

figure = plt.figure(figsize=(20,5))ax1 = figure.add_subplot(111)fig = sm.graphics.tsa.plot_pacf(at2,lags = 20, ax=ax1)

粗略选择ARCH模型阶数为4阶,根据之前的分析,AR模型的阶数为4阶。建立模型如下:

train_ARCH = XSHG_chgpct[:-10]test_ARCH = XSHG_chgpct[-10:]am_ARCH = arch.arch_model(train_ARCH, mean='AR', lags=4, vol='ARCH', p=4) res_ARCH = am_ARCH.fit()
Iteration:      1,   Func. Count:     12,   Neg. LLF: -1265.11156097
Iteration:      2,   Func. Count:     31,   Neg. LLF: -1265.26321446
Iteration:      3,   Func. Count:     46,   Neg. LLF: -1268.23077171
Iteration:      4,   Func. Count:     62,   Neg. LLF: -1268.38085284
Iteration:      5,   Func. Count:     76,   Neg. LLF: -1268.44728035
Iteration:      6,   Func. Count:     90,   Neg. LLF: -1269.15006686
Iteration:      7,   Func. Count:    104,   Neg. LLF: -1269.63810452
Iteration:      8,   Func. Count:    118,   Neg. LLF: -1270.04321783
Iteration:      9,   Func. Count:    131,   Neg. LLF: -1271.23378798
Iteration:     10,   Func. Count:    144,   Neg. LLF: -1271.96804469
Iteration:     11,   Func. Count:    158,   Neg. LLF: -1272.34895828
Iteration:     12,   Func. Count:    171,   Neg. LLF: -1273.07486479
Iteration:     13,   Func. Count:    184,   Neg. LLF: -1273.15219728
Iteration:     14,   Func. Count:    197,   Neg. LLF: -1273.72913292
Iteration:     15,   Func. Count:    209,   Neg. LLF: -1274.69850975
Iteration:     16,   Func. Count:    222,   Neg. LLF: -1274.75050645
Iteration:     17,   Func. Count:    235,   Neg. LLF: -1274.80335829
Iteration:     18,   Func. Count:    247,   Neg. LLF: -1274.81024722
Iteration:     19,   Func. Count:    259,   Neg. LLF: -1274.81159778
Iteration:     20,   Func. Count:    271,   Neg. LLF: -1274.81161492
Optimization terminated successfully.    (Exit mode 0)
            Current function value: -1274.81161529
            Iterations: 20
            Function evaluations: 272
            Gradient evaluations: 20
res_ARCH.summary(), res_ARCH.params
(<class 'statsmodels.iolib.summary.Summary'>
 """
                            AR - ARCH Model Results                            
 ==============================================================================
 Dep. Variable:            000001.XSHG   R-squared:                      -0.001
 Mean Model:                        AR   Adj. R-squared:                 -0.009
 Vol Model:                       ARCH   Log-Likelihood:                1274.81
 Distribution:                  Normal   AIC:                          -2529.62
 Method:            Maximum Likelihood   BIC:                          -2487.76
                                         No. Observations:                  486
 Date:                Tue, Nov 21 2017   Df Residuals:                      476
 Time:                        15:21:19   Df Model:                           10
                                     Mean Model                                   
 =================================================================================
                     coef    std err          t      P>|t|        95.0% Conf. Int.
 -
 Const         7.4239e-04  5.928e-07   1252.297      0.000   [7.412e-04,7.435e-04]
 0000...SHG[1]     0.0755  9.635e-03      7.833  4.753e-15   [5.659e-02,9.436e-02]
 0000...SHG[2]    -0.1017  2.675e-02     -3.801  1.442e-04    [ -0.154,-4.925e-02]
 0000...SHG[3]    -0.0300  6.519e-03     -4.609  4.045e-06 [-4.283e-02,-1.727e-02]
 0000...SHG[4]    -0.0476  1.622e-02     -2.932  3.366e-03 [-7.937e-02,-1.577e-02]
                                 Volatility Model                               
 ===============================================================================
                   coef    std err          t      P>|t|        95.0% Conf. Int.
 -
 omega       1.2184e-04  8.472e-10  1.438e+05      0.000   [1.218e-04,1.218e-04]
 alpha[1]   -1.0180e-14  5.071e-02 -2.007e-13      1.000  [-9.940e-02,9.940e-02]
 alpha[2]        0.3320  3.667e-02      9.055  1.363e-19       [  0.260,  0.404]
 alpha[3]        0.2590  1.479e-02     17.517  1.056e-68       [  0.230,  0.288]
 alpha[4]        0.1972  3.818e-02      5.165  2.400e-07       [  0.122,  0.272]
 ===============================================================================
 
 Covariance estimator: robust
 """, Const            7.423861e-04
 0000...SHG[1]    7.547658e-02
 0000...SHG[2]   -1.016841e-01
 0000...SHG[3]   -3.004857e-02
 0000...SHG[4]   -4.756994e-02
 omega            1.218369e-04
 alpha[1]        -1.018029e-14
 alpha[2]         3.320260e-01
 alpha[3]         2.590258e-01
 alpha[4]         1.971899e-01
 Name: params, dtype: float64)

因此,模型为: $$r_t = 0.00074 + 0.07548a_t - 0.10168a_{t-1} - 0.03005a_{t-2} - 0.04757a_{t-3} \\ \sigma^2_t = 0.00012 - 1.018 × 10^{-14}a^2_{t-1} + 0.33203a^2_{t-2} + 0.25903a^2_{t-3} + 0.19719a^2_{t-4}$$

可以看出,上证指数日收益率期望大约为0.07%,根据AdjR²值,模型拟合效果较差。

res_ARCH.hedgehog_plot()
len(train_ARCH)
490
pre_ARCH = res_ARCH.forecast(horizon=10,start=489).iloc[489]plt.figure(figsize=(10,4))plt.plot(test_ARCH,label='realValue')pre_ARCH.plot(label='predictValue')plt.plot(np.zeros(10),label='zero')plt.legend(loc=0)
<matplotlib.legend.Legend at 0x7fbb8d2c3950>

预测效果实在很差hhhh.

2. GARCH模型¶

ARCH模型在描述波动率的过程中,为了充分描述资产收益的波动情况,往往需要很多参数。下面引入广义的ARCH模型来修正ARCH模型的不足,及GARCH(generalized ARCH)模型。

2.1 GARCH模型简介¶

对于收益率序列$r_t$, 令$a_t = r_t - \mu_t$,称$a_t$服从GARCH(m,s)模型,若$a_t$满足:

$$a_t = \sigma_t \epsilon_t \\ \sigma^2_t = \alpha_0 + \sum^m_{i=1}\alpha_i a^2_{t-i} + \sum^s_{j=1} \beta_j \sigma^2_{t-j}$$

其中 $$\forall i>0, \alpha_0 > 0, \alpha_i \geq 0, \beta_j \geq 0, \sum^{max(m,s)}_{i=1} (\alpha_i + \beta_j) <1$$ 其中,$\{ \epsilon_t \}$是均值为0,方差为1的独立同分布随机变量序列。

ARCH模型的建模方法也可以用来建立GARCH模型,然而GARCH模型定阶较困难,在实际中只用到低阶的GARCH模型,如GARCH(1,1)、GARCH(1,2)和GARCH(2,1)等。

仍以之前的数据为例,构建GARCH模型,均值方程取AR(4)模型,波动率方程取GARCH(1,1)模型:

train_GARCH = XSHG_chgpct[:-10]test_GARCH = XSHG_chgpct[-10:]am_GARCH = arch.arch_model(train_GARCH, mean='AR',lags=4,vol='GARCH') res_GARCH = am_GARCH.fit()
Iteration:      1,   Func. Count:     10,   Neg. LLF: -1291.27825127
Positive directional derivative for linesearch    (Exit mode 8)
            Current function value: -1291.27825128
            Iterations: 5
            Function evaluations: 10
            Gradient evaluations: 1
res_GARCH.summary(), res_GARCH.params
(<class 'statsmodels.iolib.summary.Summary'>
 """
                            AR - GARCH Model Results                           
 ==============================================================================
 Dep. Variable:            000001.XSHG   R-squared:                       0.030
 Mean Model:                        AR   Adj. R-squared:                  0.022
 Vol Model:                      GARCH   Log-Likelihood:                1291.28
 Distribution:                  Normal   AIC:                          -2566.56
 Method:            Maximum Likelihood   BIC:                          -2533.07
                                         No. Observations:                  486
 Date:                Tue, Nov 21 2017   Df Residuals:                      478
 Time:                        15:21:22   Df Model:                            8
                                     Mean Model                                    
 ==================================================================================
                      coef    std err          t      P>|t|        95.0% Conf. Int.
 
 Const          1.7975e-04  2.975e-07    604.215      0.000   [1.792e-04,1.803e-04]
 0000...SHG[1]      0.0825  2.686e-03     30.712 3.927e-207   [7.723e-02,8.776e-02]
 0000...SHG[2]     -0.0868  2.571e-03    -33.775 4.595e-250 [-9.186e-02,-8.178e-02]
 0000...SHG[3] -4.4177e-03  2.903e-03     -1.522      0.128  [-1.011e-02,1.272e-03]
 0000...SHG[4]      0.1229  1.703e-03     72.122      0.000       [  0.120,  0.126]
                                Volatility Model                               
 ==============================================================================
                  coef    std err          t      P>|t|        95.0% Conf. Int.
 
 omega      7.9959e-06  2.697e-21  2.965e+15      0.000   [7.996e-06,7.996e-06]
 alpha[1]       0.1000  4.734e-04    211.216      0.000     [9.907e-02,  0.101]
 beta[1]        0.8800  3.548e-04   2480.279      0.000       [  0.879,  0.881]
 ==============================================================================
 
 Covariance estimator: robust
 """, Const            0.000180
 0000...SHG[1]    0.082499
 0000...SHG[2]   -0.086822
 0000...SHG[3]   -0.004418
 0000...SHG[4]    0.122853
 omega            0.000008
 alpha[1]         0.100000
 beta[1]          0.880000
 Name: params, dtype: float64)

得到模型: $$r_t = 0.00018 + 0.08250a_t - 0.08682a_{t-1} - 0.00442a_{t-2} + 0.12285a_{t-3} \\ \sigma^2_{t} = 0.000008 + 0.1a^2_{t-1} + 0.88\sigma^2_{t-1}$$

res_GARCH.hedgehog_plot()

可以看出,方差的还原还不错。

2.2 GARCH模型波动率预测¶

2.2.1 根据均值方程预测$a_t$

提取均值方程的系数向量,逐个计算$a_t$后面的10个值:

ini = res_GARCH.resid[-4:]a = np.array(res_GARCH.params[1:5])w = a[::-1] # 系数for i in range(10):new = test_GARCH.iloc[i] - (res_GARCH.params[0] + w.dot(ini[-4:]))ini = np.append(ini,new)print len(ini)at_pre = ini[-10:]at_pre2 = at_pre**2at_pre2
14
array([  9.65171549e-06,   1.29592813e-05,   1.43107182e-04,
         1.95905001e-06,   6.44071417e-05,   2.34988199e-05,
         2.79190993e-05,   1.00915325e-05,   1.81691680e-06,
         2.16753512e-06])

2.2.2 根据波动率方程预测$\sigma^2_t$

ini2 = res_GARCH.conditional_volatility[-2:] #上两个条件异方差值for i in range(10):new = res_GARCH.params['omega'] + res_GARCH.params['alpha[1]']*at_pre2[i] + res_GARCH.params['beta[1]']*ini2[-1]ini2 = np.append(ini2,new)vol_pre = ini2[-10:]vol_pre
array([ 0.010515  ,  0.00926249,  0.0081733 ,  0.00720069,  0.00635105,
        0.00559927,  0.00493814,  0.00435457,  0.0038402 ,  0.00338759])

将原始数据、条件异方差拟合数据和预测数据画出来,如图:

plt.figure(figsize=(15,5))plt.plot(XSHG_chgpct,label='origin_data')plt.plot(res_GARCH.conditional_volatility,label='conditional_volatility')x = range(490,500)plt.plot(x,vol_pre,'.r',label='predict_volatility')plt.legend(loc=0)
<matplotlib.legend.Legend at 0x7fbb8d09ff10>

3. GARCH模型的推广¶

3.1 GJR-GARCH¶

在GARCH模型中,正负冲击对条件方差的影响是对称的。为衡量收益率波动的非对称性,Glosten等人提出了GJR—GARCH模型,也称作门限GARCH(或T-GARCH模型),它引入了一个示性函数$\{N_{ {t-k}}\}$,即 $$N_{t-k}= \begin{cases} 1,& \text{若$a_{t-k} < 0$}\\ 0,& \text{若$a_{t-k} \geq 0$} \end{cases}$$ 它的波动率方程可以写为:

$$\sigma^2_t = \alpha_0 + \sum^s_{i=1} \alpha_i a^2_{t-i} + \sum^n_{k=1} \gamma_k N_{t-k} a^2_{t-k} + \sum^m_{j=1} \beta_j \sigma^2_{t-j}$$

在arch函数包中,三个求和项的项数分别为p,o,q,我们仍以上证指数日收益率为例,设定p,o,q的值均为1。

am_GJR = arch.arch_model(XSHG_chgpct, p=1, o=1, q=1)res_GJR = am_GJR.fit(update_freq=5, disp='off')print(res_GJR.summary(), res_GJR.params)
(<class 'statsmodels.iolib.summary.Summary'>
"""
                   Constant Mean - GJR-GARCH Model Results                    
==============================================================================
Dep. Variable:            000001.XSHG   R-squared:                       0.000
Mean Model:             Constant Mean   Adj. R-squared:                  0.000
Vol Model:                  GJR-GARCH   Log-Likelihood:                1335.04
Distribution:                  Normal   AIC:                          -2660.07
Method:            Maximum Likelihood   BIC:                          -2639.00
                                        No. Observations:                  500
Date:                Tue, Nov 21 2017   Df Residuals:                      495
Time:                        15:21:25   Df Model:                            5
                                  Mean Model                                  
==============================================================================
                 coef    std err          t      P>|t|        95.0% Conf. Int.

mu         3.0370e-04  2.199e-09  1.381e+05      0.000   [3.037e-04,3.037e-04]
                               Volatility Model                               
==============================================================================
                 coef    std err          t      P>|t|        95.0% Conf. Int.

omega      8.0647e-06  1.355e-20  5.950e+14      0.000   [8.065e-06,8.065e-06]
alpha[1]       0.1000  1.570e-03     63.697      0.000     [9.692e-02,  0.103]
gamma[1]       0.0100  2.655e-03      3.767  1.653e-04   [4.797e-03,1.520e-02]
beta[1]        0.8750  4.585e-04   1908.285      0.000       [  0.874,  0.876]
==============================================================================

Covariance estimator: robust
""", mu          0.000304
omega       0.000008
alpha[1]    0.100000
gamma[1]    0.010000
beta[1]     0.875000
Name: params, dtype: float64)

由此得到波动率方程为: $$\sigma^2_t = 0.000008 + 0.1a^2_{t-1} + 0.01 N_{t-1} a^2_{t-1} + 0.875 \sigma^2_{t-1}$$

3.2 TARCH¶

TARCH模型(ZARCH)是将波动率的平方变为绝对值,即将默认值power=2改为1,波动率公式为: $$\sigma_t = \alpha_0 + \sum^s_{i=1} \alpha_i \left|a_{t-i}\right| + \sum^n_{k=1} \gamma_k N_{t-k} \left| a_{t-k}\right| + \sum^m_{j=1} \beta_j \sigma_{t-j},$$ 或者,更一般地,可以改为$\kappa$阶。

am_TARCH = arch.arch_model(XSHG_chgpct, p=1, o=1, q=1, power=1.0)res_TARCH = am_TARCH.fit(update_freq=5)print(res_TARCH.summary(), res_TARCH.params)
Iteration:      5,   Func. Count:     49,   Neg. LLF: -1333.05710211
Iteration:     10,   Func. Count:     85,   Neg. LLF: -1340.2415358
Iteration:     15,   Func. Count:    120,   Neg. LLF: -1340.28522071
Optimization terminated successfully.    (Exit mode 0)
            Current function value: -1340.2852807
            Iterations: 17
            Function evaluations: 138
            Gradient evaluations: 16
(<class 'statsmodels.iolib.summary.Summary'>
"""
                  Constant Mean - TARCH/ZARCH Model Results                   
==============================================================================
Dep. Variable:            000001.XSHG   R-squared:                      -0.000
Mean Model:             Constant Mean   Adj. R-squared:                 -0.000
Vol Model:                TARCH/ZARCH   Log-Likelihood:                1340.29
Distribution:                  Normal   AIC:                          -2670.57
Method:            Maximum Likelihood   BIC:                          -2649.50
                                        No. Observations:                  500
Date:                Tue, Nov 21 2017   Df Residuals:                      495
Time:                        15:21:26   Df Model:                            5
                                  Mean Model                                  
==============================================================================
                 coef    std err          t      P>|t|        95.0% Conf. Int.

mu         4.3385e-04  4.075e-07   1064.628      0.000   [4.331e-04,4.347e-04]
                               Volatility Model                               
==============================================================================
                 coef    std err          t      P>|t|        95.0% Conf. Int.

omega      2.6779e-04  2.812e-04      0.952      0.341  [-2.833e-04,8.189e-04]
alpha[1]       0.0600      0.344      0.175      0.861       [ -0.614,  0.734]
gamma[1]       0.0170      1.144  1.488e-02      0.988       [ -2.225,  2.259]
beta[1]        0.9315      3.486      0.267      0.789       [ -5.901,  7.764]
==============================================================================

Covariance estimator: robust
""", mu          0.000434
omega       0.000268
alpha[1]    0.060007
gamma[1]    0.017016
beta[1]     0.931485
Name: params, dtype: float64)

由此得到波动率方程为: $$\sigma_t = 0.00027 + 0.06 \left|a_{t-1}\right| + 0.017 N_{t-1} \left| a_{t-1}\right| + 0.9315 \sigma_{t-1},$$

3.3 T学生分布¶

由于收益率序列经常是厚尾的,有时候用学生分布(t-分布)替代正态分布,如下所示:

am_t = arch.arch_model(XSHG_chgpct, p=1, o=1, q=1, dist = 'StudentsT')res_t = am_t.fit(update_freq=5)print(res_t.summary(), res_t.params)
Iteration:      5,   Func. Count:     60,   Neg. LLF: -1361.92464974
Iteration:     10,   Func. Count:    105,   Neg. LLF: -1368.18523019
Iteration:     15,   Func. Count:    150,   Neg. LLF: -1371.4149911
Iteration:     20,   Func. Count:    192,   Neg. LLF: -1372.78754568
Iteration:     25,   Func. Count:    232,   Neg. LLF: -1372.90852692
Optimization terminated successfully.    (Exit mode 0)
            Current function value: -1372.90852692
            Iterations: 25
            Function evaluations: 232
            Gradient evaluations: 25
(<class 'statsmodels.iolib.summary.Summary'>
"""
                      Constant Mean - GJR-GARCH Model Results                       
====================================================================================
Dep. Variable:                  000001.XSHG   R-squared:                      -0.001
Mean Model:                   Constant Mean   Adj. R-squared:                 -0.001
Vol Model:                        GJR-GARCH   Log-Likelihood:                1372.91
Distribution:      Standardized Student's t   AIC:                          -2733.82
Method:                  Maximum Likelihood   BIC:                          -2708.53
                                              No. Observations:                  500
Date:                      Tue, Nov 21 2017   Df Residuals:                      494
Time:                              15:21:26   Df Model:                            6
                                  Mean Model                                  
==============================================================================
                 coef    std err          t      P>|t|        95.0% Conf. Int.

mu         1.0085e-03  1.752e-07   5756.453      0.000   [1.008e-03,1.009e-03]
                               Volatility Model                               
==============================================================================
                 coef    std err          t      P>|t|        95.0% Conf. Int.

omega      1.2573e-06  2.674e-15  4.703e+08      0.000   [1.257e-06,1.257e-06]
alpha[1]       0.0776  5.121e-04    151.540      0.000   [7.660e-02,7.860e-02]
gamma[1]   6.5835e-03  1.107e-03      5.946  2.746e-09   [4.413e-03,8.754e-03]
beta[1]        0.9191  2.066e-04   4448.758      0.000       [  0.919,  0.920]
                                 Distribution                                 
==============================================================================
                 coef    std err          t      P>|t|        95.0% Conf. Int.

nu             3.9599  7.427e-02     53.317      0.000       [  3.814,  4.105]
==============================================================================

Covariance estimator: robust
""", mu          0.001009
omega       0.000001
alpha[1]    0.077601
gamma[1]    0.006584
beta[1]     0.919107
nu          3.959875
Name: params, dtype: float64)

由此得到波动率方程为: $$\sigma^2_t = 0.000001 + 0.077601a^2_{t-1} + 0.006584 N_{t-1} a^2_{t-1} + 0.919107 \sigma^2_{t-1}$$

3.4 EGARCH¶

为了进一步描述杠杆效应,Nelson提出了指数GARCH(EGARCH模型),体现正负资产收益率的非对称效应。这里我们以GJR-GARCH(1,1,1)为例,即在GARCH模型的基础上,考虑引入的示性函数,写出EGARCH模型的函数如下:

$$ln(\sigma^2_t) = \alpha_0 + \alpha_1 \frac{\left | a_{t-1} \right | + \gamma_1 a_{t-1}}{\sigma_{t-1}} + \beta_1 ln(\sigma^2_{t-1})$$

可以看出,正的$a_{t-1}$对对数波动率的贡献为$ \frac{a_{t-1}\alpha_1 (1 + \gamma_1)}{\sigma_{t-1}}$,负的$a_{t-1}$对对数波动率的贡献为$\frac{a_{t-1}\alpha_1 (1 - \gamma_1)}{\sigma_{t-1}}$.

am_EGARCH = arch.arch_model(XSHG_chgpct, mean='AR',lags=4,vol='EGARCH',p=1,o=1,q=1) res_EGARCH = am_EGARCH.fit()print(res_EGARCH.summary(), res_EGARCH.params)
Iteration:      1,   Func. Count:     11,   Neg. LLF: -1330.07873751
Iteration:      2,   Func. Count:     29,   Neg. LLF: -1330.09727084
Iteration:      3,   Func. Count:     44,   Neg. LLF: -1332.89992713
Iteration:      4,   Func. Count:     59,   Neg. LLF: -1333.52532992
Iteration:      5,   Func. Count:     73,   Neg. LLF: -1333.93506431
Iteration:      6,   Func. Count:     88,   Neg. LLF: -1334.04466384
Iteration:      7,   Func. Count:    102,   Neg. LLF: -1334.06362229
Iteration:      8,   Func. Count:    115,   Neg. LLF: -1334.11448845
Iteration:      9,   Func. Count:    127,   Neg. LLF: -1334.96429337
Iteration:     10,   Func. Count:    140,   Neg. LLF: -1335.35692036
Iteration:     11,   Func. Count:    152,   Neg. LLF: -1336.78394647
Iteration:     12,   Func. Count:    164,   Neg. LLF: -1336.87575367
Iteration:     13,   Func. Count:    176,   Neg. LLF: -1337.05120666
Iteration:     14,   Func. Count:    188,   Neg. LLF: -1337.09318812
Iteration:     15,   Func. Count:    200,   Neg. LLF: -1337.15260228
Iteration:     16,   Func. Count:    211,   Neg. LLF: -1337.15844619
Iteration:     17,   Func. Count:    222,   Neg. LLF: -1337.16598411
Iteration:     18,   Func. Count:    233,   Neg. LLF: -1337.16675658
Iteration:     19,   Func. Count:    244,   Neg. LLF: -1337.16694513
Iteration:     20,   Func. Count:    255,   Neg. LLF: -1337.16696696
Iteration:     21,   Func. Count:    266,   Neg. LLF: -1337.16696867
Optimization terminated successfully.    (Exit mode 0)
            Current function value: -1337.16696867
            Iterations: 21
            Function evaluations: 266
            Gradient evaluations: 21
(<class 'statsmodels.iolib.summary.Summary'>
"""
                          AR - EGARCH Model Results                           
==============================================================================
Dep. Variable:            000001.XSHG   R-squared:                       0.019
Mean Model:                        AR   Adj. R-squared:                  0.011
Vol Model:                     EGARCH   Log-Likelihood:                1337.17
Distribution:                  Normal   AIC:                          -2656.33
Method:            Maximum Likelihood   BIC:                          -2618.47
                                        No. Observations:                  496
Date:                Tue, Nov 21 2017   Df Residuals:                      487
Time:                        16:08:14   Df Model:                            9
                                    Mean Model                                   
=================================================================================
                    coef    std err          t      P>|t|        95.0% Conf. Int.
-
Const         2.2140e-04  4.758e-07    465.341      0.000   [2.205e-04,2.223e-04]
0000...SHG[1]     0.0158  5.955e-03      2.650  8.056e-03   [4.107e-03,2.745e-02]
0000...SHG[2]    -0.0431  2.946e-03    -14.625  1.946e-48 [-4.886e-02,-3.731e-02]
0000...SHG[3]     0.0183  3.158e-03      5.779  7.526e-09   [1.206e-02,2.444e-02]
0000...SHG[4]     0.0603  3.713e-03     16.247  2.329e-59   [5.305e-02,6.760e-02]
                               Volatility Model                               
==============================================================================
                 coef    std err          t      P>|t|        95.0% Conf. Int.

omega         -0.0423  4.726e-03     -8.959  3.266e-19 [-5.160e-02,-3.308e-02]
alpha[1]       0.1250  2.220e-03     56.295      0.000       [  0.121,  0.129]
gamma[1]      -0.0183  1.244e-03    -14.713  5.301e-49 [-2.074e-02,-1.586e-02]
beta[1]        0.9942  7.519e-05  1.322e+04      0.000       [  0.994,  0.994]
==============================================================================

Covariance estimator: robust
""", Const            0.000221
0000...SHG[1]    0.015778
0000...SHG[2]   -0.043086
0000...SHG[3]    0.018251
0000...SHG[4]    0.060325
omega           -0.042338
alpha[1]         0.125001
gamma[1]        -0.018302
beta[1]          0.994160
Name: params, dtype: float64)

由此得到波动率方程为: $$ln(\sigma^2_t) = -0.0423 + 0.125 \frac{\left | a_{t-1} \right | + -0.018 a_{t-1}}{\sigma_{t-1}} + 0.994 ln(\sigma^2_{t-1})$$

后记¶

关于ARCH相关的模型还有很多,例如GARCH-M, HARCH等等,还有各种函数对均值方程和波动方程的参数进行了调整,有兴趣的读者可以参阅Kevin Sheppard写的arch Documentation,这是目前我所见到的关于arch函数包阐述最全面的文档. 另外,还可以参阅qua*pian上的的广义矩估计方法(GMM)对于GARCH模型的阶数进行判断, 最后,有兴趣的读者可以学习一些更加复杂的ARCH相关模型的使用,我发现了一个pyflux函数包,可以实现例如长记忆模型,GAS模型等等,它对于模型的介绍、公式、实现方法及代码都写的非常清晰。由于平台目前无没有此函数包,暂时没有办法进行调试。

分享到:
举报财经168客户端下载

全部回复

0/140

投稿 您想发表你的观点和看法?

更多人气分析师

  • 张亦巧

    人气2152文章4145粉丝45

    暂无个人简介信息

  • 梁孟梵

    人气2152文章3177粉丝39

    qq:2294906466 了解群指导添加微信mfmacd

  • 指导老师

    人气1856文章4423粉丝52

    暂无个人简介信息

  • 李冉晴

    人气2296文章3821粉丝34

    李冉晴,专业现贷实盘分析师。

  • 刘钥钥1

    人气2016文章3119粉丝34

    专业从事现货黄金、现货白银模似实盘操作分析指导

  • 张迎妤

    人气1896文章3305粉丝34

    个人专注于行情技术分析,消息面解读剖析,给予您第一时间方向...

  • 金泰铬J

    人气2320文章3925粉丝51

    投资问答解咨询金泰铬V/信tgtg67即可获取每日的实时资讯、行情...

  • 金算盘

    人气2696文章7761粉丝125

    高级分析师,混过名校,厮杀于股市和期货、证券市场多年,专注...

  • 金帝财神

    人气4736文章8329粉丝118

    本文由资深分析师金帝财神微信:934295330,指导黄金,白银,...