rgarch包是R中用来拟合和检验garch模型的一个包。该包最早在http://rgarch.r-forge.r-project.org上发布,现已发布到CRAN上。简单而言,该包主要包括四个功能:
- 拟合garch族模型
- garch族模型诊断
- garch族模型预测
- 模拟garch序列
- 拟合序列分布
下面分别说一下。
拟合garch族模型
拟合garch族模型分三个步骤:
(1)通过ugarchspec函数设定模型形式
(2)通过ugarchfit函数拟合模型
设定模型形式
一个典型的garch(p,q)模型如下:


该模型由三个部分构成,均值方程对应式(1),分布假设对应(2),方差方程对应式(3),对三个部分进行适当的变形后可以形成egarch模型,egarch-ged模型,egarch-t模型,Igarch模型,garch-m模型和Qgarch模型等。因此,设定模型形式就是分别设定均值方程、方差方程和分布。
rugarch包的优越之处正在于这里。ugarchspec函数的参数也被分解为为三个主要部分,分别是variance.model,对应式(3),mean.model,对应式(1),distribution.model对应式(2)中的$\epsilon$。用户通过对三个部分的参数的分别设定从而构造出自己想用的模型。
举个例子:
variance.model = list(model = "sGARCH", garchOrder = c(1, 1),
submodel = NULL, external.regressors = NULL, variance.targeting = FALSE),
表示拟合的方差模型为sGARCH,方差模型的自回归阶数是(1,1),方差模型中未引入外生变量。
mean.model = list(armaOrder = c(1, 1), include.mean = TRUE, archm = FALSE, archpow = 1, arfima = FALSE, external.regressors = NULL, archex = FALSE)
表示均值方程为arma(1,1)模型,方程自变量中包含均值,未引入外生变量。
distribution.model = "norm"
表示模型分布假设为正态分布。
将三个部分装入ugarchspec的参数中就可以完成一个sgarch(1,1)-norm模型的模型设定。
myspec=ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1), submodel = NULL, external.regressors = NULL, variance.targeting = FALSE), mean.model = list(armaOrder = c(1, 1), include.mean = TRUE, archm = FALSE, archpow = 1, arfima = FALSE, external.regressors = NULL, archex = FALSE), distribution.model = "norm")
拟合模型
拟合模型的函数是ugarchfit。ugarchfit的参数如下:
ugarchfit(spec, data, out.sample = 0, solver = "solnp", solver.control = list(),
fit.control = list(stationarity = 1, fixed.se = 0, scale = 0), ...)
其中,spec为ugarchspec函数的结果,data为数据对象。solver为优化算法。solver.control设定优化参数,fit.control设定拟合参数。
接上面的例子:
myfit=ugarchfit(myspec,data=sp500ret,solver="solnp")
到这里一个garch模型就完成了。
查看结果
键入下列代码查看模型的拟合结果:
> myfit
*---------------------------------*
* GARCH Model Fit *
*---------------------------------*
Conditional Variance Dynamics
-----------------------------------
GARCH Model : sGARCH(1,1)
Mean Model : ARFIMA(1,0,1)
Distribution : norm
Optimal Parameters
------------------------------------
Estimate Std. Error t value Pr(>|t|)
mu 0.000516 0.000090 5.7631 0
ar1 0.835693 0.058412 14.3069 0
ma1 -0.866236 0.053556 -16.1743 0
omega 0.000001 0.000000 5.2751 0
alpha1 0.087706 0.007724 11.3548 0
beta1 0.904994 0.008442 107.2026 0
Robust Standard Errors:
Estimate Std. Error t value Pr(>|t|)
mu 0.000516 0.000101 5.0988 0.000000
ar1 0.835693 0.047779 17.4908 0.000000
ma1 -0.866236 0.044266 -19.5690 0.000000
omega 0.000001 0.000001 2.1703 0.029981
alpha1 0.087706 0.029942 2.9292 0.003398
beta1 0.904994 0.029232 30.9587 0.000000
LogLikelihood : 17901.99
Information Criteria
------------------------------------
Akaike -6.4805
Bayes -6.4733
Shibata -6.4805
Hannan-Quinn -6.4780
Q-Statistics on Standardized Residuals
------------------------------------
statistic p-value
Lag10 14.11 0.078922
Lag15 27.81 0.009623
Lag20 31.04 0.028513
H0 : No serial correlation
Q-Statistics on Standardized Squared Residuals
------------------------------------
statistic p-value
Lag10 3.072 0.9298
Lag15 5.906 0.9495
Lag20 8.425 0.9716
ARCH LM Tests
------------------------------------
Statistic DoF P-Value
ARCH Lag[2] 1.485 2 0.4759
ARCH Lag[5] 1.740 5 0.8838
ARCH Lag[10] 3.021 10 0.9809
Nyblom stability test
------------------------------------
Joint Statistic: 175.7021
Individual Statistics:
mu 0.1997
ar1 0.2214
ma1 0.1695
omega 21.5537
alpha1 0.1341
beta1 0.1121
Asymptotic Critical Values (10% 5% 1%)
Joint Statistic: 1.49 1.68 2.12
Individual Statistic: 0.35 0.47 0.75
Sign Bias Test
------------------------------------
t-value prob sig
Sign Bias 0.3216 7.478e-01
Negative Sign Bias 3.0087 2.635e-03 ***
Positive Sign Bias 2.4529 1.420e-02 **
Joint Effect 29.0986 2.135e-06 ***
Adjusted Pearson Goodness-of-Fit Test:
------------------------------------
group statistic p-value(g-1)
1 20 182.5 9.999e-29
2 30 186.8 5.511e-25
3 40 230.0 6.488e-29
4 50 234.9 4.330e-26
Elapsed time : 6.765625
提取模型结果
rugarch包中模型结果的提取要依靠as.data.frame函数。比如提取模型的拟合值
as.data.frame(myfit,which="fitted")
提取残差序列:
as.data.frame(myfit,which=" residuals")
提取方差序列:
as.data.frame(myfit,which="sigma")
当然,也可以同时查看所有:
as.data.frame(myfit,which=all)
或者
as.data.frame(myfit)
两个语句等价。
模型诊断
通过plot(myfit)可以对模型结果进行图形诊断:
> plot(myfit)
Make a plot selection (or 0 to exit):
1: Series with 2 Conditional SD Superimposed
2: Series with 2.5% VaR Limits (with unconditional mean)
3: Conditional SD
4: ACF of Observations
5: ACF of Squared Observations
6: ACF of Absolute Observations
7: Cross Correlation
8: Empirical Density of Standardized Residuals
9: QQ-Plot of Standardized Residuals
10: ACF of Standardized Residuals
11: ACF of Squared Standardized Residuals
12: News-Impact Curve
Selection: 1
模型预测
如果模型通过检验,可以用ugarchforcast函数对未来进行预测:
forc = ugarchforecast(myfit, n.ahead=20)
> forc=ugarchforecast(myfit, n.ahead=20)
> forc
*------------------------------------*
* GARCH Model Forecast *
*------------------------------------*
Model: sGARCH
Horizon: 20
Roll Steps: 0
Out of Sample: 0
0-roll forecast:
sigma series
2009-02-02 0.02477 0.0016170
2009-02-03 0.02471 0.0014362
2009-02-04 0.02465 0.0012851
2009-02-05 0.02458 0.0011588
2009-02-06 0.02452 0.0010532
2009-02-09 0.02446 0.0009650
2009-02-10 0.02440 0.0008913
2009-02-11 0.02434 0.0008297
2009-02-12 0.02427 0.0007783
2009-02-13 0.02421 0.0007353
2009-02-16 0.02415 0.0006993
2009-02-17 0.02409 0.0006693
2009-02-18 0.02403 0.0006442
2009-02-19 0.02397 0.0006232
2009-02-20 0.02391 0.0006056
2009-02-23 0.02385 0.0005910
2009-02-24 0.02379 0.0005787
2009-02-25 0.02373 0.0005685
2009-02-26 0.02368 0.0005600
2009-02-27 0.02362 0.0005528
可以用fpm或者plot来查看模型的预测结果。比如:
> plot(fore)
Make a plot selection (or 0 to exit):
1: Time Series Prediction (unconditional)
2: Time Series Prediction (rolling)
3: Conditional SD Prediction
Selection: 1