關於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))

 

 

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