【代码实践】Garch和Arch的简单应用

Garch和Arch模型的简单应用

1、Problem with Variance

Autoregressive models can be developed for univariate time series data that is stationary (AR), has a trend (ARIMA), and has a seasonal component (SARIMA).
一元时间序列数据的自相关模型:平稳AR,趋势ARIMA,季节成分SARIMA

One aspect of a univariate time series that these autoregressive models do not model is a change in the variance over time.
这些自相关模型假设所有时间序列方差不变。

Classically, a time series with modest changes in variance can sometimes be adjusted using a power transform, such as by taking the Log or using a Box-Cox transform.
当其方差改变较小,可取对数或采用Box-Cox转换。

There are some time series where the variance changes consistently over time. In the context of a time series in the financial domain, this would be called increasing and decreasing volatility.
存在方差持续变化的时间序列。

In time series where the variance is increasing in a systematic way, such as an increasing trend, this property of the series is called heteroskedasticity. It’s a fancy word from statistics that means changing or unequal variance across the series.
方差持续变化意味着异方差性。

If the change in variance can be correlated over time, then it can be modeled using an autoregressive process, such as ARCH.
如果方差的变化存在相关性,可使用自回归模型建模,即ARCH。

2、What Is an ARCH Model?

Autoregressive Conditional Heteroskedasticity, or ARCH, is a method that explicitly models the change in variance over time in a time series.
ARCH通过明确方差变化建模。

Specifically, an ARCH method models the variance at a time step as a function of the residual errors from a mean process (e.g. a zero mean).
即将时间序列方差作为均值残差的函数。

The ARCH process introduced by Engle (1982) explicitly recognizes the difference between the unconditional and the conditional variance allowing the latter to change over time as a function of past errors.

— Generalized autoregressive conditional heteroskedasticity, 1986.

A lag parameter must be specified to define the number of prior residual errors to include in the model. Using the notation of the GARCH model (discussed later), we can refer to this parameter as “q“. Originally, this parameter was called “p“, and is also called “p” in the arch Python package used later in this tutorial.
滞后参数需要被明确以定义残差误差项的数量:q。

q: The number of lag squared residual errors to include in the ARCH model.
A generally accepted notation for an ARCH model is to specify the ARCH() function with the q parameter ARCH(q); for example, ARCH(1) would be a first order ARCH model.
q:ARCH模型中残差项数量。

The approach expects the series is stationary, other than the change in variance, meaning it does not have a trend or seasonal component. An ARCH model is used to predict the variance at future time steps.
ARCH可用于预测未来的方差。

[ARCH] are mean zero, serially uncorrelated processes with nonconstant variances conditional on the past, but constant unconditional variances. For such processes, the recent past gives information about the one-period forecast variance.

– Autoregressive Conditional Heteroscedasticity with Estimates of the Variance of United Kingdom Inflation, 1982.

In practice, this can be used to model the expected variance on the residuals after another autoregressive model has been used, such as an ARMA or similar.
在实践中,可以使用预期方差与残差建模。

The model should only be applied to a prewhitened residual series {e_t} that is uncorrelated and contains no trends or seasonal changes, such as might be obtained after fitting a satisfactory SARIMA model.
— Page 148, Introductory Time Series with R, 2009.

该模型只能应用于prewhitened残差序列,即不包含趋势。

3、What Is a GARCH Model?

Generalized Autoregressive Conditional Heteroskedasticity, or GARCH, is an extension of the ARCH model that incorporates a moving average component together with the autoregressive component.
GARCH是ARCH的扩展版,可以包含一个移动平均成分以及自回归成分。

Specifically, the model includes lag variance terms (e.g. the observations if modeling the white noise residual errors of another process), together with lag residual errors from a mean process.
具体而言,该模型包含一个滞后方差项(对经过其他模型所得白噪声残差项建模)以及一个均值过程所得滞后残差项。

The introduction of a moving average component allows the model to both model the conditional change in variance over time as well as changes in the time-dependent variance. Examples include conditional increases and decreases in variance.
移动平均项:允许模型方差随时间变化,允许方差时间依赖性变化。比如方差的conditional增加与降低。

As such, the model introduces a new parameter “p” that describes the number of lag variance terms:
p: The number of lag variances to include in the GARCH model.
滞后方差项个数。
q: The number of lag residual errors to include in the GARCH model.
滞后残差项个数。
A generally accepted notation for a GARCH model is to specify the GARCH() function with the p and q parameters GARCH(p, q); for example GARCH(1, 1) would be a first order GARCH model.

A GARCH model subsumes ARCH models, where a GARCH(0, q) is equivalent to an ARCH(q) model.
不含滞后方差项的GARCH模型等同于ARCH。

For p = 0 the process reduces to the ARCH(q) process, and for p = q = 0 E(t) is simply white noise.
p=q=0代表简单的白噪音。

In the ARCH(q) process the conditional variance is specified as a linear function of past sample variances only, whereas the GARCH(p, q) process allows lagged conditional variances to enter as well. This corresponds to some sort of adaptive learning mechanism.

— Generalized autoregressive conditional heteroskedasticity, 1986.

As with ARCH, GARCH predicts the future variance and expects that the series is stationary, other than the change in variance, meaning it does not have a trend or seasonal component.
与ARCH相同,GARCH预测未来方差,并且期待时间序列是平稳的。

4、How to Configure ARCH and GARCH Models

The configuration for an ARCH model is best understood in the context of ACF and PACF plots of the variance of the time series.
如何配置ARCH模型。

This can be achieved by subtracting the mean from each observation in the series and squaring the result, or just squaring the observation if you’re already working with white noise residuals from another model.
可以看作:(x-mean)**2。即将白噪声开方。

If a correlogram appears to be white noise […], then volatility ca be detected by looking at the correlogram of the squared values since the squared values are equivalent to the variance (provided the series is adjusted to have a mean of zero).

— Pages 146-147, Introductory Time Series with R, 2009.

The ACF and PACF plots can then be interpreted to estimate values for p and q, in a similar way as is done for the ARMA model.

For more information on how to do this, see the post:

A Gentle Introduction to Autocorrelation and Partial Autocorrelation

5、ARCH and GARCH Models in Python

(1)数据生成

In this section, we will look at how we can develop ARCH and GARCH models in Python using the arch library.
本节将使用arch library建模。

First, let’s prepare a dataset we can use for these examples.

Test Dataset
We can create a dataset with a controlled model of variance.

The simplest case would be a series of random noise where the mean is zero and the variance starts at 0.0 and steadily increases.
准备数据集:随机白噪声,均值为零,方差从0逐步增加。

We can achieve this in Python using the gauss() function that generates a Gaussian random number with the specified mean and standard deviation.

# create a simple white noise with increasing variance
from random import gauss
from random import seed
from matplotlib import pyplot
# seed pseudorandom number generator
seed(1)
# create dataset
data = [gauss(0, i*0.01) for i in range(0,100)]
# plot
pyplot.plot(data)
pyplot.show()

请添加图片描述

(2)自相关性

We know there is an autocorrelation in the variance of the contrived dataset.
已知该数据集存在自相关性。画图。

Nevertheless, we can look at an autocorrelation plot to confirm this expectation. The complete example is listed below.

# check correlations of squared observations
from random import gauss
from random import seed
from matplotlib import pyplot
from statsmodels.graphics.tsaplots import plot_acf
import numpy as np
# seed pseudorandom number generator
seed(1)
# create dataset
data = [gauss(0, i*0.01) for i in range(0,100)]
# square the dataset
squared_data = [x**2 for x in data]
# create acf plot
plot_acf(np.array(squared_data))
pyplot.show()

请添加图片描述

(3)Arch建模

Define the model
Fit the model
Make a forecast.
三步走:定义模型;拟合模型;预测。

Before fitting and forecasting, we can split the dataset into a train and test set so that we can fit the model on the train and evaluate its performance on the test set.
将数据分为train和test。

A model can be defined by calling the arch_model() function. We can specify a model for the mean of the series: in this case mean=’Zero’ is an appropriate model. We can then specify the model for the variance: in this case vol=’ARCH’. We can also specify the lag parameter for the ARCH model: in this case p=15.

通过arch_model()建模。

Note, in the arch library, the names of p and q parameters for ARCH/GARCH have been reversed.

指定均值模型,方差模型。平方项滞后个数为15。

选取10个作为测试集用来预测。

# example of ARCH model
from random import gauss
from random import seed
from matplotlib import pyplot
from arch import arch_model
# seed pseudorandom number generator
seed(1)
# create dataset
data = [gauss(0, i*0.01) for i in range(0,100)]
# split into train/test
n_test = 10
train, test = data[:-n_test], data[-n_test:]
# define model
model = arch_model(train, mean='Zero', vol='ARCH', p=15)
# fit model
model_fit = model.fit()
# forecast the test set
yhat = model_fit.forecast(horizon=n_test)
# plot the actual variance
var = [i*0.01 for i in range(0,100)]
pyplot.plot(var[-n_test:])
# plot forecast variance
pyplot.plot(yhat.variance.values[-1, :])
pyplot.show()
D:\python\envs\TF\lib\site-packages\arch\compat\numba.py:39: PerformanceWarning: 
numba is not available, and this function is being executed without JIT
compilation. Either install numba or reinstalling after installing Cython
is strongly recommended.
  warnings.warn(performance_warning, PerformanceWarning)
D:\python\envs\TF\lib\site-packages\arch\compat\numba.py:39: PerformanceWarning: 
numba is not available, and this function is being executed without JIT
compilation. Either install numba or reinstalling after installing Cython
is strongly recommended.
  warnings.warn(performance_warning, PerformanceWarning)


Iteration:      1,   Func. Count:     18,   Neg. LLF: 88214.15274343832
Iteration:      2,   Func. Count:     36,   Neg. LLF: 145.16327370105464
Iteration:      3,   Func. Count:     54,   Neg. LLF: 128.0887135967567
Iteration:      4,   Func. Count:     72,   Neg. LLF: 109.21731760967693
Iteration:      5,   Func. Count:     90,   Neg. LLF: 36.505726167454
Iteration:      6,   Func. Count:    108,   Neg. LLF: 39.65736210558854
Iteration:      7,   Func. Count:    126,   Neg. LLF: 28.719728253674663
Iteration:      8,   Func. Count:    143,   Neg. LLF: 28.02026266328528
Iteration:      9,   Func. Count:    161,   Neg. LLF: 34.947196664051255
Iteration:     10,   Func. Count:    180,   Neg. LLF: 30.036303168280703
Iteration:     11,   Func. Count:    198,   Neg. LLF: 26.91678180890047
Iteration:     12,   Func. Count:    216,   Neg. LLF: 35.232996325776796
Iteration:     13,   Func. Count:    235,   Neg. LLF: 25.55779436454201
Iteration:     14,   Func. Count:    253,   Neg. LLF: 25.496876439718772
Iteration:     15,   Func. Count:    271,   Neg. LLF: 25.486204106812277
Iteration:     16,   Func. Count:    289,   Neg. LLF: 25.486109755436825
Iteration:     17,   Func. Count:    307,   Neg. LLF: 25.482149625441725
Iteration:     18,   Func. Count:    324,   Neg. LLF: 25.480673980268485
Iteration:     19,   Func. Count:    341,   Neg. LLF: 25.47836302173241
Iteration:     20,   Func. Count:    358,   Neg. LLF: 25.47763916181153
Iteration:     21,   Func. Count:    375,   Neg. LLF: 25.47751060017057
Iteration:     22,   Func. Count:    392,   Neg. LLF: 25.477507641491535
Iteration:     23,   Func. Count:    408,   Neg. LLF: 25.477507607815134
Optimization terminated successfully    (Exit mode 0)
            Current function value: 25.477507641491535
            Iterations: 23
            Function evaluations: 408
            Gradient evaluations: 23


D:\python\envs\TF\lib\site-packages\arch\compat\numba.py:39: PerformanceWarning: 
numba is not available, and this function is being executed without JIT
compilation. Either install numba or reinstalling after installing Cython
is strongly recommended.
  warnings.warn(performance_warning, PerformanceWarning)
D:\python\envs\TF\lib\site-packages\arch\__future__\_utility.py:11: FutureWarning: 
The default for reindex is True. After September 2021 this will change to
False. Set reindex to True or False to silence this message. Alternatively,
you can use the import comment

from arch.__future__ import reindexing

to globally set reindex to True and silence this warning.

  warnings.warn(

请添加图片描述

(4)Garch建模

We can fit a GARCH model just as easily using the arch library.

The arch_model() function can specify a GARCH instead of ARCH model vol=’GARCH’ as well as the lag parameters for both.

The dataset may not be a good fit for a GARCH model given the linearly increasing variance, nevertheless, the complete example is listed below.

使用Garch模型。
p: The number of lag variances to include in the GARCH model.
滞后方差项个数。
q: The number of lag residual errors to include in the GARCH model.
滞后残差项个数。
蓝线:真实方差。

# example of ARCH model
from random import gauss
from random import seed
from matplotlib import pyplot
from arch import arch_model
# seed pseudorandom number generator
seed(1)
# create dataset
data = [gauss(0, i*0.01) for i in range(0,100)]
# split into train/test
n_test = 10
train, test = data[:-n_test], data[-n_test:]
# define model
model = arch_model(train, mean='Zero', vol='GARCH', p=15, q=15)
# fit model
model_fit = model.fit()
# forecast the test set
yhat = model_fit.forecast(horizon=n_test)
# plot the actual variance
var = [i*0.01 for i in range(0,100)]
pyplot.plot(var[-n_test:])
# plot forecast variance
pyplot.plot(yhat.variance.values[-1, :])
pyplot.show()
D:\python\envs\TF\lib\site-packages\arch\compat\numba.py:39: PerformanceWarning: 
numba is not available, and this function is being executed without JIT
compilation. Either install numba or reinstalling after installing Cython
is strongly recommended.
  warnings.warn(performance_warning, PerformanceWarning)


Iteration:      1,   Func. Count:     33,   Neg. LLF: 134.2317665883839
Iteration:      2,   Func. Count:     70,   Neg. LLF: 95088.3398100521
Iteration:      3,   Func. Count:    103,   Neg. LLF: 544.0686542051735
Iteration:      4,   Func. Count:    136,   Neg. LLF: 359.74467197626797
Iteration:      5,   Func. Count:    169,   Neg. LLF: 120.68429190701761
Iteration:      6,   Func. Count:    202,   Neg. LLF: 57.67984556619349
Iteration:      7,   Func. Count:    235,   Neg. LLF: 37.23386320586698
Iteration:      8,   Func. Count:    268,   Neg. LLF: 44.284722021361496
Iteration:      9,   Func. Count:    301,   Neg. LLF: 30.955293097511912
Iteration:     10,   Func. Count:    334,   Neg. LLF: 26.9576030443107
Iteration:     11,   Func. Count:    366,   Neg. LLF: 31.358756978286806
Iteration:     12,   Func. Count:    400,   Neg. LLF: 30.014724862032015
Iteration:     13,   Func. Count:    433,   Neg. LLF: 28.461775455924112
Iteration:     14,   Func. Count:    466,   Neg. LLF: 27.699903204400325
Iteration:     15,   Func. Count:    499,   Neg. LLF: 26.904454755807887
Iteration:     16,   Func. Count:    532,   Neg. LLF: 25.516088236134205
Iteration:     17,   Func. Count:    564,   Neg. LLF: 25.51091583772553
Iteration:     18,   Func. Count:    597,   Neg. LLF: 25.783633830232016
Iteration:     19,   Func. Count:    630,   Neg. LLF: 25.492119474366323
Iteration:     20,   Func. Count:    662,   Neg. LLF: 25.485424782249122
Iteration:     21,   Func. Count:    694,   Neg. LLF: 25.479685299743895
Iteration:     22,   Func. Count:    726,   Neg. LLF: 25.477822341013752
Iteration:     23,   Func. Count:    758,   Neg. LLF: 25.47752598232283
Iteration:     24,   Func. Count:    790,   Neg. LLF: 25.477511692767404
Iteration:     25,   Func. Count:    822,   Neg. LLF: 25.477507957892282
Iteration:     26,   Func. Count:    853,   Neg. LLF: 25.47750792087572
Optimization terminated successfully    (Exit mode 0)
            Current function value: 25.477507957892282
            Iterations: 26
            Function evaluations: 853
            Gradient evaluations: 26


D:\python\envs\TF\lib\site-packages\arch\compat\numba.py:39: PerformanceWarning: 
numba is not available, and this function is being executed without JIT
compilation. Either install numba or reinstalling after installing Cython
is strongly recommended.
  warnings.warn(performance_warning, PerformanceWarning)
D:\python\envs\TF\lib\site-packages\arch\__future__\_utility.py:11: FutureWarning: 
The default for reindex is True. After September 2021 this will change to
False. Set reindex to True or False to silence this message. Alternatively,
you can use the import comment

from arch.__future__ import reindexing

to globally set reindex to True and silence this warning.

  warnings.warn(

请添加图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值