关于Python的ARCH包(三)

1.2 举例

1.2.1 ARCH建模

以下代码需要在 IPython notebook下运行:

In [1]:
import warnings
warnings.simplefilter('ignore')

%matplotlib inline
import seaborn
seaborn.set_style('darkgrid')
In [2]:
seaborn.mpl.rcParams['figure.figsize'] = (10.0, 6.0)
seaborn.mpl.rcParams['savefig.dpi'] = 90
seaborn.mpl.rcParams['font.family'] = 'serif'
seaborn.mpl.rcParams['font.size'] = 14

1.2.2 Setup

本例利用来自Yahoo金融的数据,利用pandas-datareader导入。

import datetime as dt
import pandas_datareader.data as web
st = dt.datetime(1988,1,1)
en = dt.datetime(2018,1,1)
data = web.get_data_famafrench('F-F_Research_Data_Factors_daily', start=st, end=en)
mkt_returns = data[0]['Mkt-RF'] +  data[0]['RF']
returns = mkt_returns
figure = returns.plot()

1.2.3 Specifying Common Models

最简单的模型构建方法是使用模型构建方法 arch.arch_model,它能表示多数通用模型。最简单的arch调用将会使用均值为常数模型的GARCH(1,1)波动过程且误差服从正态分布。

该模型通过调用fit方法进行估计。可选迭代参数iter控制最优化产出的频率,disp控制是否返回收敛信息。结果类直接返回估计参数及相关数值,如果summary方法一样。

 

1.2.4 GARCH (with a Constant Mean)

默认选项会产生基于均值的GARCH(1,1)条件方差以及正态分布误差。

from arch import arch_model
am = arch_model(returns)
res = am.fit(update_freq=5)
print(res.summary())
Iteration:      5,   Func. Count:     41,   Neg. LLF: 9820.63969763662
Iteration:     10,   Func. Count:     74,   Neg. LLF: 9817.274895653107
Optimization terminated successfully.    (Exit mode 0)
            Current function value: 9817.274187120329
            Iterations: 13
            Function evaluations: 92
            Gradient evaluations: 13
                     Constant Mean - GARCH Model Results                      
==============================================================================
Dep. Variable:                   None   R-squared:                      -0.000
Mean Model:             Constant Mean   Adj. R-squared:                 -0.000
Vol Model:                      GARCH   Log-Likelihood:               -9817.27
Distribution:                  Normal   AIC:                           19642.5
Method:            Maximum Likelihood   BIC:                           19670.3
                                        No. Observations:                 7561
Date:                Mon, Jan 21 2019   Df Residuals:                     7557
Time:                        19:24:29   Df Model:                            4
                                 Mean Model                                 
============================================================================
                 coef    std err          t      P>|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
mu             0.0687  9.098e-03      7.555  4.176e-14 [5.091e-02,8.657e-02]
                              Volatility Model                              
============================================================================
                 coef    std err          t      P>|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
omega          0.0151  4.174e-03      3.622  2.923e-04 [6.937e-03,2.330e-02]
alpha[1]       0.0836  1.231e-02      6.788  1.138e-11   [5.943e-02,  0.108]
beta[1]        0.9014  1.436e-02     62.771      0.000     [  0.873,  0.930]
============================================================================

Covariance estimator: robust

plot() 方法可以用来快速将标准化残差和条件波动率显示出来。

fig = res.plot(annualize='D')

1.2.5 GJR-GARCH

其他输入参数可以用来构建有关模型。该举例设定o为1,这表示该模型包括一阶滞后的非对称效应,且使GARCH模型变为GJR-GARCH模型,其方差变化表示如下:

这里I为指标函数,当其参数为真时值为1.

由于引进了非对称项,对数似然率得到很大提升,估计参数高度显著。

 Constant Mean - GJR-GARCH Model Results                    
==============================================================================
Dep. Variable:                   None   R-squared:                      -0.000
Mean Model:             Constant Mean   Adj. R-squared:                 -0.000
Vol Model:                  GJR-GARCH   Log-Likelihood:               -9713.51
Distribution:                  Normal   AIC:                           19437.0
Method:            Maximum Likelihood   BIC:                           19471.7
                                        No. Observations:                 7561
Date:                Mon, Jan 21 2019   Df Residuals:                     7556
Time:                        19:33:32   Df Model:                            5
                                 Mean Model                                 
============================================================================
                 coef    std err          t      P>|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
mu             0.0442  8.759e-03      5.051  4.398e-07 [2.707e-02,6.141e-02]
                               Volatility Model                              
=============================================================================
                 coef    std err          t      P>|t|       95.0% Conf. Int.
-----------------------------------------------------------------------------
omega          0.0186  4.608e-03      4.044  5.252e-05  [9.603e-03,2.766e-02]
alpha[1]   5.9351e-03  5.981e-03      0.992      0.321 [-5.788e-03,1.766e-02]
gamma[1]       0.1355  2.086e-02      6.495  8.291e-11    [9.459e-02,  0.176]
beta[1]        0.9044  1.464e-02     61.778      0.000      [  0.876,  0.933]
=============================================================================

Covariance estimator: robust

1.2.6 TARCH/ZARCH

TARCH (也作 ZARCH)模型使用绝对值来构建波动率模型。该模型的精度参数 power=1.0。其默认精度参数为power=2,且方差过程与平方项线性相关。

TARCH波动率过程表示如下:

更多的模型参数在精度 (κκ)下的波动率表示如下:

 

此处条件方差为  

该TARCH 模型也改进了拟合度,虽然对数似然率下降。

Iteration:      5,   Func. Count:     54,   Neg. LLF: 9701.46915224492
Iteration:     10,   Func. Count:     94,   Neg. LLF: 9685.575185675976
Iteration:     15,   Func. Count:    130,   Neg. LLF: 9683.248100354442
Optimization terminated successfully.    (Exit mode 0)
            Current function value: 9683.248099008848
            Iterations: 16
            Function evaluations: 137
            Gradient evaluations: 16
                  Constant Mean - TARCH/ZARCH Model Results                   
==============================================================================
Dep. Variable:                   None   R-squared:                      -0.000
Mean Model:             Constant Mean   Adj. R-squared:                 -0.000
Vol Model:                TARCH/ZARCH   Log-Likelihood:               -9683.25
Distribution:                  Normal   AIC:                           19376.5
Method:            Maximum Likelihood   BIC:                           19411.1
                                        No. Observations:                 7561
Date:                Mon, Jan 21 2019   Df Residuals:                     7556
Time:                        19:40:04   Df Model:                            5
                                 Mean Model                                 
============================================================================
                 coef    std err          t      P>|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
mu             0.0386  8.899e-03      4.338  1.437e-05 [2.116e-02,5.604e-02]
                              Volatility Model                              
============================================================================
                 coef    std err          t      P>|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
omega          0.0215  4.333e-03      4.961  6.998e-07 [1.301e-02,2.999e-02]
alpha[1]       0.0137  6.461e-03      2.114  3.448e-02 [9.980e-04,2.632e-02]
gamma[1]       0.1195  1.440e-02      8.301  1.031e-16   [9.130e-02,  0.148]
beta[1]        0.9213  1.010e-02     91.219      0.000     [  0.901,  0.941]
============================================================================

Covariance estimator: robust

1.2.7 Student’s T Errors(学生T分布)

金融收益率通常肥尾,学生T分布是一种捕获该特征的简单方法。使用arch方法可以把分布从正态变为学生T分布。

标准化残差在估计自由度10附近呈现肥尾特征,对数似然率也有大幅增加。

am = arch_model(returns, p=1, o=1, q=1, power=1.0, dist='StudentsT')
res = am.fit(update_freq=5)
print(res.summary())
Iteration:      5,   Func. Count:     57,   Neg. LLF: 9535.283529224867
Iteration:     10,   Func. Count:    102,   Neg. LLF: 9506.009581127182
Iteration:     15,   Func. Count:    144,   Neg. LLF: 9505.381448443233
Optimization terminated successfully.    (Exit mode 0)
            Current function value: 9505.38144747584
            Iterations: 15
            Function evaluations: 145
            Gradient evaluations: 15
                     Constant Mean - TARCH/ZARCH Model Results                      
====================================================================================
Dep. Variable:                         None   R-squared:                      -0.000
Mean Model:                   Constant Mean   Adj. R-squared:                 -0.000
Vol Model:                      TARCH/ZARCH   Log-Likelihood:               -9505.38
Distribution:      Standardized Student's t   AIC:                           19022.8
Method:                  Maximum Likelihood   BIC:                           19064.3
                                              No. Observations:                 7561
Date:                      Mon, Jan 21 2019   Df Residuals:                     7555
Time:                              19:47:19   Df Model:                            6
                                 Mean Model                                 
============================================================================
                 coef    std err          t      P>|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
mu             0.0593  8.031e-03      7.382  1.558e-13 [4.355e-02,7.503e-02]
                               Volatility Model                              
=============================================================================
                 coef    std err          t      P>|t|       95.0% Conf. Int.
-----------------------------------------------------------------------------
omega          0.0153  3.142e-03      4.875  1.087e-06  [9.159e-03,2.148e-02]
alpha[1]   7.9883e-03  4.902e-03      1.630      0.103 [-1.619e-03,1.760e-02]
gamma[1]       0.1258  1.335e-02      9.420  4.523e-21    [9.959e-02,  0.152]
beta[1]        0.9291  8.309e-03    111.828      0.000      [  0.913,  0.945]
                              Distribution                              
========================================================================
                 coef    std err          t      P>|t|  95.0% Conf. Int.
------------------------------------------------------------------------
nu             6.8359      0.575     11.896  1.241e-32 [  5.710,  7.962]
========================================================================

Covariance estimator: robust

1.2.8 Fixing Parameters

在一切情况下,固定参数而非估计参数或许更为有趣。可以利用fix()方法生成同模型结果类。 除了判断信息(诸如标准误差,t统计等等)不可用外,该类结果返回值与通常模型结果类一致.

该例中,对前一估计模型的使用对称参数:

fixed_res = am.fix([0.0235, 0.01, 0.06, 0.0, 0.9382, 8.0])
print(fixed_res.summary())
                      Constant Mean - TARCH/ZARCH Model Results                      
=====================================================================================
Dep. Variable:                          None   R-squared:                          --
Mean Model:                    Constant Mean   Adj. R-squared:                     --
Vol Model:                       TARCH/ZARCH   Log-Likelihood:               -9690.73
Distribution:       Standardized Student's t   AIC:                           19393.5
Method:            User-specified Parameters   BIC:                           19435.1
                                               No. Observations:                 7561
Date:                       Mon, Jan 21 2019                                         
Time:                               20:10:34                                         
      Mean Model     
=====================
                 coef
---------------------
mu             0.0235
   Volatility Model  
=====================
                 coef
---------------------
omega          0.0100
alpha[1]       0.0600
gamma[1]       0.0000
beta[1]        0.9382
     Distribution    
=====================
                 coef
---------------------
nu             8.0000
=====================

Results generated with user-specified parameters.
Since the model was not estimated, there are no std. errors.

 

1.2.9 构建模型

整体上,模型可以从三个方面构建:

  • 平均模型(arch.mean)
    • 0均值 (ZeroMean) - 可用,如果单独使用模型残差。
    • 常数均值(ConstantMean) - 适用于多数流动金融资产。
    • 可选外生变量自回归 (ARX)
    • 可选外生变量异方差自回归
    • 仅仅外生回归 (LS)
  • 波动率过程 (arch.volatility)
    • ARCH (ARCH)
    • GARCH (GARCH)
    • GJR-GARCH (GARCH using o argument)
    • TARCH/ZARCH (GARCH using power argument set to 1)
    • Power GARCH and Asymmetric Power GARCH (GARCH using power)
    • 使用估计系数的指数加权平均移动方差 (EWMAVariance)
    • 异方差 ARCH (HARCH)
    • 参数模型
      • 指数加权平均移动方差,即为风险指标 RiskMetrics (EWMAVariance)
      • EWMAs的加权平均数,即为 RiskMetrics 2006 方法风险指标 (RiskMetrics2006)
  • 分布 (arch.distribution)
    • Normal (Normal)
    • Standardized Students’s T (StudentsT)

 

1.2.10 均值模型

第一个选择为均值模型。对于很多流动性金融资产而言,常数均值或0均值模型已经足够。对于其他时间序列数据,比如通胀,则可能需要更为复杂的模型。这些例子使用了美联储数据库的核心CPI数据Federal Reserve Economic Data .

core_cpi = web.DataReader("CPILFESL", "fred", dt.datetime(1957,1,1), dt.datetime(2014,1,1))
ann_inflation = 100 * core_cpi.CPILFESL.pct_change(12).dropna()
fig = ann_inflation.plot()

所有均值模型均由常数方差和正态分布进行初始化.对于 ARX 类模型,模型中的滞后阶数由lags表示。

from arch.univariate import ARX
ar = ARX(ann_inflation, lags = [1, 3, 12])
print(ar.fit().summary())
 AR - Constant Variance Model Results                     
==============================================================================
Dep. Variable:               CPILFESL   R-squared:                       0.991
Mean Model:                        AR   Adj. R-squared:                  0.991
Vol Model:          Constant Variance   Log-Likelihood:               -13.7178
Distribution:                  Normal   AIC:                           37.4356
Method:            Maximum Likelihood   BIC:                           59.9043
                                        No. Observations:                  661
Date:                Mon, Jan 21 2019   Df Residuals:                      656
Time:                        21:23:29   Df Model:                            5
                                   Mean Model                                  
===============================================================================
                   coef    std err          t      P>|t|       95.0% Conf. Int.
-------------------------------------------------------------------------------
Const            0.0424  2.197e-02      1.929  5.372e-02 [-6.781e-04,8.542e-02]
CPILFESL[1]      1.1927  3.513e-02     33.950 1.229e-252      [  1.124,  1.262]
CPILFESL[3]     -0.1803  4.123e-02     -4.374  1.218e-05   [ -0.261,-9.954e-02]
CPILFESL[12]    -0.0235  1.384e-02     -1.695  9.001e-02 [-5.060e-02,3.663e-03]
                              Volatility Model                              
============================================================================
                 coef    std err          t      P>|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
sigma2         0.0610  6.992e-03      8.728  2.590e-18 [4.733e-02,7.474e-02]
============================================================================

Covariance estimator: White's Heteroskedasticity Consistent Estimator

1.2.11 波动率过程

波动率过程可以由volatility属性添加到均值模型中去。该举例使用volatility参数给均值模型添加了一个ARCH(5)的波动率过程。参数iter和disp则在拟合过程fit()中时可用来描述估计结果。

from arch.univariate import ARCH, GARCH
ar.volatility = ARCH(p=5)
res = ar.fit(update_freq=0, disp='off')
print(res.summary())
 AR - ARCH Model Results                            
==============================================================================
Dep. Variable:               CPILFESL   R-squared:                       0.991
Mean Model:                        AR   Adj. R-squared:                  0.991
Vol Model:                       ARCH   Log-Likelihood:                83.8447
Distribution:                  Normal   AIC:                          -147.689
Method:            Maximum Likelihood   BIC:                          -102.752
                                        No. Observations:                  661
Date:                Mon, Jan 21 2019   Df Residuals:                      651
Time:                        21:27:32   Df Model:                           10
                                   Mean Model                                  
===============================================================================
                   coef    std err          t      P>|t|       95.0% Conf. Int.
-------------------------------------------------------------------------------
Const            0.0268  2.207e-02      1.212      0.225 [-1.651e-02,7.002e-02]
CPILFESL[1]      1.0854  3.840e-02     28.266 9.155e-176      [  1.010,  1.161]
CPILFESL[3]     -0.0755  4.148e-02     -1.821  6.854e-02    [ -0.157,5.747e-03]
CPILFESL[12]    -0.0210  1.195e-02     -1.761  7.829e-02 [-4.446e-02,2.381e-03]
                              Volatility Model                              
============================================================================
                 coef    std err          t      P>|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
omega      9.9111e-03  2.195e-03      4.515  6.342e-06 [5.608e-03,1.421e-02]
alpha[1]       0.1291  4.090e-02      3.157  1.594e-03   [4.895e-02,  0.209]
alpha[2]       0.2279  6.468e-02      3.524  4.258e-04     [  0.101,  0.355]
alpha[3]       0.1698  7.121e-02      2.385  1.709e-02   [3.026e-02,  0.309]
alpha[4]       0.2643  8.318e-02      3.177  1.489e-03     [  0.101,  0.427]
alpha[5]       0.1686  7.426e-02      2.271  2.316e-02   [2.309e-02,  0.314]
============================================================================

Covariance estimator: robust

即使经过了标准化处理,标准化的残差和条件波动率仍然显示出更大的误差。

fig = res.plot()

 

1.2.12 分布

最后,分布可以通过distribution属性,把默认的正态分布修改为标准的学生T分布。

学生T分布改进了模型,自由度大约在8个附近。

from arch.univariate import StudentsT
ar.distribution = StudentsT()
res = ar.fit(update_freq=0, disp='off')
print(res.summary())
  AR - ARCH Model Results                               
====================================================================================
Dep. Variable:                     CPILFESL   R-squared:                       0.991
Mean Model:                              AR   Adj. R-squared:                  0.991
Vol Model:                             ARCH   Log-Likelihood:                89.4699
Distribution:      Standardized Student's t   AIC:                          -156.940
Method:                  Maximum Likelihood   BIC:                          -107.509
                                              No. Observations:                  661
Date:                      Mon, Jan 21 2019   Df Residuals:                      650
Time:                              21:33:12   Df Model:                           11
                                   Mean Model                                  
===============================================================================
                   coef    std err          t      P>|t|       95.0% Conf. Int.
-------------------------------------------------------------------------------
Const            0.0281  2.252e-02      1.249      0.212 [-1.600e-02,7.227e-02]
CPILFESL[1]      1.0851  3.920e-02     27.683 1.133e-168      [  1.008,  1.162]
CPILFESL[3]     -0.0697  4.277e-02     -1.629      0.103    [ -0.154,1.415e-02]
CPILFESL[12]    -0.0266  1.525e-02     -1.742  8.157e-02 [-5.643e-02,3.328e-03]
                              Volatility Model                              
============================================================================
                 coef    std err          t      P>|t|      95.0% Conf. Int.
----------------------------------------------------------------------------
omega          0.0117  3.142e-03      3.710  2.071e-04 [5.499e-03,1.782e-02]
alpha[1]       0.1669  5.233e-02      3.189  1.426e-03   [6.433e-02,  0.269]
alpha[2]       0.2185  6.648e-02      3.287  1.012e-03   [8.823e-02,  0.349]
alpha[3]       0.1370  6.928e-02      1.977  4.804e-02   [1.179e-03,  0.273]
alpha[4]       0.2186  7.738e-02      2.825  4.734e-03   [6.691e-02,  0.370]
alpha[5]       0.1596  8.579e-02      1.861  6.275e-02  [-8.495e-03,  0.328]
                              Distribution                              
========================================================================
                 coef    std err          t      P>|t|  95.0% Conf. Int.
------------------------------------------------------------------------
nu             9.1015      3.834      2.374  1.760e-02 [  1.587, 16.616]
========================================================================

Covariance estimator: robust

1.2.13 WTI 原油

下一个例子使用了FRED的西得克萨斯中级原油数据.这些模型使用替代分布假定进行拟合。打印出的结果显示,正态分布比标准学生T分布或标准偏态学生T分布的对数似然率更低。然而,后两者比较接近。T和偏态T分布的密切性表明收益率并没有严重的偏态。

from collections import OrderedDict
crude=web.get_data_fred('DCOILWTICO',dt.datetime(2000, 1, 1),dt.datetime(2015, 1, 1))
crude_ret = 100 * crude.dropna().pct_change().dropna()
res_normal = arch_model(crude_ret).fit(disp='off')
res_t = arch_model(crude_ret, dist='t').fit(disp='off')
res_skewt = arch_model(crude_ret, dist='skewt').fit(disp='off')
lls = pd.Series(OrderedDict((('normal', res_normal.loglikelihood),
                 ('t', res_t.loglikelihood),
                 ('skewt', res_skewt.loglikelihood))))
print(lls)
params = pd.DataFrame(OrderedDict((('normal', res_normal.params),
                 ('t', res_t.params),
                 ('skewt', res_skewt.params))))
print(params)
normal   -8227.359031
t        -8128.534732
skewt    -8126.303934
dtype: float64
            normal         t     skewt
alpha[1]  0.054488  0.046069  0.045908
beta[1]   0.940953  0.949954  0.950364
lambda         NaN       NaN -0.048593
mu        0.065643  0.076392  0.057599
nu             NaN  6.841493  6.889696
omega     0.034733  0.026497  0.025239

标准化的残差可以把残差除以条件波动率后计算得出。上述均同残差一并画出(非标准化,但时间长度不一)。非标准化的残差中间的尖峰更为突出,表明其分布比标准化残差更具有厚尾特征。

 

std_resid = res_normal.resid / res_normal.conditional_volatility
unit_var_resid = res_normal.resid / res_normal.resid.std()
df = pd.concat([std_resid, unit_var_resid],1)
df.columns = ['Std Resids', 'Unit Variance Resids']
subplot = df.plot(kind='kde', xlim=(-4,4))

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章