# 10.3 Arch/Garch 模型 · 如何使用优矿进行 GARCH 模型分析

## ARCH 建模示例

``````import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

try:
import seaborn
except ImportError:
pass
``````

## 一、准备工作

``````import datetime as dt
st = dt.datetime(1990,1,1)
en = dt.datetime(2015,6,30)

returns = 100 * data['closePrice'].pct_change().dropna()
figure = returns.plot(figsize=(20,6))
``````

## GARCH (均值恒定)

``````from arch import arch_model
am = arch_model(returns)
res = am.fit(update_freq=5)
print(res.summary())

Iteration:      5,   Func. Count:     38,   Neg. LLF: 5787.7752693
Iteration:     10,   Func. Count:     75,   Neg. LLF: 5785.39088499
Optimization terminated successfully.    (Exit mode 0)
Current function value: 5785.0196556
Iterations: 14
Function evaluations: 101
Constant Mean - GARCH Model Results
==============================================================================
Dep. Variable:             closePrice   R-squared:                      -0.000
Mean Model:             Constant Mean   Adj. R-squared:                 -0.000
Vol Model:                      GARCH   Log-Likelihood:               -5785.02
Distribution:                  Normal   AIC:                           11578.0
Method:            Maximum Likelihood   BIC:                           11601.1
No. Observations:                 2373
Date:                Fri, Oct 16 2015   Df Residuals:                     2369
Time:                        11:08:36   Df Model:                            4
Mean Model
==============================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
------------------------------------------------------------------------------
mu             0.0349  2.825e-03     12.348  4.985e-35   [2.935e-02,4.042e-02]
Volatility Model
==============================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
------------------------------------------------------------------------------
omega          0.0383  2.763e-03     13.861  1.085e-43   [3.288e-02,4.371e-02]
alpha[1]       0.0287  2.439e-04    117.761      0.000   [2.825e-02,2.920e-02]
beta[1]        0.9694  4.516e-04   2146.801      0.000       [  0.969,  0.970]
==============================================================================

Covariance estimator: robust
``````

`plot()` 函数可以快速展示 标的的标准偏差和条件波动率。

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

## GJR-GARCH

`arch_model` 在构建模型时，还可以添加附加参数。在这个例子中，设置`o`为1， 即包含了非对称冲击的一阶滞后项，从而将原GARCH模型转换为一个GJR-GARCH模型。新的模型具有动态方差，由下面公式给出：

``````am = arch_model(returns, p=1, o=1, q=1)
res = am.fit(update_freq=5, disp='off')
print(res.summary())
``````

## TARCH/ZARCH

TARCH模型 (又称为 ZARCH模型) 是对波动率的绝对值进行建模. 使用该模型时，需要在`arch_model`建构函数中，设置`power=1.0`。因为默认的阶数为2，对应的是用平方项表示的方差变化过程。

TARCH model的波动率过程由下面公式给出：

``````am = arch_model(returns, p=1, o=1, q=1, power=1.0)
res = am.fit(update_freq=5)
print(res.summary())

Iteration:      5,   Func. Count:     45,   Neg. LLF: 5765.36462439
Iteration:     10,   Func. Count:     84,   Neg. LLF: 5758.70411096
Iteration:     15,   Func. Count:    121,   Neg. LLF: 5758.63601597
Optimization terminated successfully.    (Exit mode 0)
Current function value: 5758.6360268
Iterations: 15
Function evaluations: 121
Constant Mean - TARCH/ZARCH Model Results
==============================================================================
Dep. Variable:             closePrice   R-squared:                      -0.000
Mean Model:             Constant Mean   Adj. R-squared:                 -0.000
Vol Model:                TARCH/ZARCH   Log-Likelihood:               -5758.64
Distribution:                  Normal   AIC:                           11527.3
Method:            Maximum Likelihood   BIC:                           11556.1
No. Observations:                 2373
Date:                Fri, Oct 16 2015   Df Residuals:                     2368
Time:                        12:50:24   Df Model:                            5
Mean Model
==============================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
------------------------------------------------------------------------------
mu             0.0625  4.323e-03     14.456  2.296e-47   [5.402e-02,7.096e-02]
Volatility Model
==============================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
------------------------------------------------------------------------------
omega          0.0457  4.933e-03      9.257  2.107e-20   [3.599e-02,5.533e-02]
alpha[1]       0.0594  1.012e-03     58.701      0.000   [5.742e-02,6.139e-02]
gamma[1]      -0.0184  5.771e-04    -31.882 4.768e-223 [-1.953e-02,-1.727e-02]
beta[1]        0.9498  2.510e-03    378.466      0.000       [  0.945,  0.955]
==============================================================================

Covariance estimator: robust
``````

## 学生T分布误差

``````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:     48,   Neg. LLF: 5522.16193119
Iteration:     10,   Func. Count:     93,   Neg. LLF: 5475.09377571
Iteration:     15,   Func. Count:    138,   Neg. LLF: 5451.04968458
Iteration:     20,   Func. Count:    179,   Neg. LLF: 5435.39156625
Iteration:     25,   Func. Count:    223,   Neg. LLF: 5434.83467797
Iteration:     30,   Func. Count:    269,   Neg. LLF: 5434.83086355
Optimization terminated successfully.    (Exit mode 0)
Current function value: 5434.83085822
Iterations: 33
Function evaluations: 304
Constant Mean - TARCH/ZARCH Model Results
====================================================================================
Dep. Variable:                   closePrice   R-squared:                      -0.001
Mean Model:                   Constant Mean   Adj. R-squared:                 -0.001
Vol Model:                      TARCH/ZARCH   Log-Likelihood:               -5434.83
Distribution:      Standardized Student's t   AIC:                           10881.7
Method:                  Maximum Likelihood   BIC:                           10916.3
No. Observations:                 2373
Date:                      Fri, Oct 16 2015   Df Residuals:                     2367
Time:                              13:04:54   Df Model:                            6
Mean Model
===============================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
-------------------------------------------------------------------------------
mu         -1.3518e-08  1.493e-10    -90.539      0.000 [-1.381e-08,-1.323e-08]
Volatility Model
===============================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
-------------------------------------------------------------------------------
omega           0.0894      0.304      0.294      0.769       [ -0.507,  0.686]
alpha[1]        0.1185  9.143e-03     12.962  2.004e-38       [  0.101,  0.136]
gamma[1]   -2.9293e-03  4.117e-03     -0.712      0.477  [-1.100e-02,5.139e-03]
beta[1]         0.8829  9.652e-02      9.148  5.803e-20       [  0.694,  1.072]
Distribution
==============================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
------------------------------------------------------------------------------
nu             3.6773      0.142     25.919 4.047e-148       [  3.399,  3.955]
==============================================================================

Covariance estimator: robust
``````

## 使用固定参数

``````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:                    closePrice   R-squared:                          --
Mean Model:                    Constant Mean   Adj. R-squared:                     --
Vol Model:                       TARCH/ZARCH   Log-Likelihood:               -5579.95
Distribution:       Standardized Student's t   AIC:                           11171.9
Method:            User-specified Parameters   BIC:                           11206.5
No. Observations:                 2373
Date:                       Fri, Oct 16 2015
Time:                               13:04:57
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.
``````
``````import pandas as pd
df = pd.concat([res.conditional_volatility,fixed_res.conditional_volatility],1)
df.columns = ['Estimated', 'Fixed']
df.plot()

<matplotlib.axes.AxesSubplot at 0x6b5cd90>
``````

• A mean model (arch.mean)
• Zero mean (ZeroMean) - useful if using residuals from a model estimated separately
• Constant mean (ConstantMean) - common for most liquid financial assets
• Autoregressive (ARX) with optional exogenous regressors
• Heterogeneous (HARX) autoregression with optional exogenous regressors
• Exogenous regressors only (LS)
• A volatility process (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)
• Heterogeneous ARCH (HARCH)
• Parameterless Models
• Exponentially Weighted Moving Average Variance, known as RiskMetrics (EWMAVariance)
• Weighted averages of EWMAs, known as the RiskMetrics 2006 methodology (RiskMetrics2006)
• A distribution (arch.distribution)
• Normal (Normal)
• Standardized Students's T (StudentsT)

## Mean Models 均值模型

``````core_cpi = DataAPI.ChinaDataCPIGet(indicID=u"M030000003",indicName=u"",beginDate=u"20050101",endDate=u"",field=u"",pandas="1")
ann_inflation = 100 * core_cpi.sort(columns='periodDate').dataValue.pct_change(12).dropna()
fig = ann_inflation.plot()
fig

<matplotlib.axes.AxesSubplot at 0x6e6a090>
``````

``````from arch.univariate import ARX
ar = ARX(ann_inflation, lags = [1, 3, 12])
print(ar.fit().summary())

AR - Constant Variance Model Results
==============================================================================
Dep. Variable:              dataValue   R-squared:                       0.264
Mean Model:                        AR   Adj. R-squared:                  0.242
Vol Model:          Constant Variance   Log-Likelihood:               -685.867
Distribution:                  Normal   AIC:                           1381.73
Method:            Maximum Likelihood   BIC:                           1395.00
No. Observations:                  105
Date:                Fri, Oct 16 2015   Df Residuals:                      100
Time:                        13:21:29   Df Model:                            5
Mean Model
=================================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
---------------------------------------------------------------------------------
Const            -1.4076    254.081 -5.540e-03      0.996  [-4.994e+02,4.966e+02]
dataValue[1]      0.4233      0.112      3.777  1.590e-04       [  0.204,  0.643]
dataValue[3]      0.1590  1.423e-02     11.173  5.523e-29       [  0.131,  0.187]
dataValue[12]    -0.0117  1.050e-03    -11.133  8.695e-29 [-1.374e-02,-9.628e-03]
Volatility Model
==============================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
------------------------------------------------------------------------------
sigma2     2.7619e+04  2.259e+08  1.223e-04      1.000  [-4.427e+08,4.427e+08]
==============================================================================

Covariance estimator: White's Heteroskedasticity Consistent Estimator
``````

## Volatility Processes 波动率过程

``````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:              dataValue   R-squared:                       0.104
Mean Model:                        AR   Adj. R-squared:                  0.077
Vol Model:                       ARCH   Log-Likelihood:               -576.003
Distribution:                  Normal   AIC:                           1172.01
Method:            Maximum Likelihood   BIC:                           1198.55
No. Observations:                  105
Date:                Fri, Oct 16 2015   Df Residuals:                       95
Time:                        13:25:52   Df Model:                           10
Mean Model
=================================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
---------------------------------------------------------------------------------
Const           -11.9096     29.775     -0.400      0.689       [-70.268, 46.449]
dataValue[1]      0.8647  2.093e-02     41.313      0.000       [  0.824,  0.906]
dataValue[3]     -0.0296  9.925e-03     -2.978  2.904e-03 [-4.901e-02,-1.010e-02]
dataValue[12]    -0.0154  2.936e-04    -52.419      0.000 [-1.597e-02,-1.482e-02]
Volatility Model
===============================================================================
coef    std err          t      P>|t|        95.0% Conf. Int.
-------------------------------------------------------------------------------
omega         404.9890  1.164e+05  3.480e-03      0.997  [-2.277e+05,2.285e+05]
alpha[1]        0.6348  5.176e-02     12.266  1.383e-34       [  0.533,  0.736]
alpha[2]        0.2690  1.767e-02     15.221  2.552e-52       [  0.234,  0.304]
alpha[3]        0.0962  1.161e-02      8.283  1.196e-16     [7.341e-02,  0.119]
alpha[4]    2.5205e-10  3.554e-04  7.093e-07      1.000  [-6.965e-04,6.965e-04]
alpha[5]   -4.6303e-10  1.178e-05 -3.929e-05      1.000  [-2.310e-05,2.309e-05]
===============================================================================

Covariance estimator: robust
``````

``````fig = res.plot()
``````

## Distributions 概率分布情况

2 文中，多次提到`iter`，但这个疑为原作者笔误，故都改为`update_freq`

3 原文最后一章‘WTI Crude’由于给出的例子和当前的`arch`包版本不兼容，无法正确运行，故没有引入