@fanxy
2020-04-26T18:12:32.000000Z
字数 8416
阅读 10238
樊潇彦
复旦大学经济学院
金融数据
下载数据:Ch10_Data.rar
# 准备工作
setwd("D:\\...\\Ch10")
install.packages("fGarch","rugarch")
## 调用
library(fGarch)
library(rugarch)
library(tidyverse)
library(readxl)
library(ggplot2)
library(tseries)
library(zoo)
library(forecast)
我们可以将金融资产的对数收益率分为均值和波动两部分:
如果对数收益率服从ARMA(p,q)过程:
如果 与其他 个前定解释变量 ,以及外生变量 之间存在线性关系(如CAPM模型、协整模型、周末效应检验等):
将所有用于估计均值的信息记为 ,则资产收益率的条件方差:
资产波动率的建模一般分为4个步骤:
两种ARCH效应检验:
对均值方程的残差平方序列 可以进行两种ARCH效应检验:
# P139:Intel月收益率
da=read.table("m-intcsp7309.txt",header=T) # 读取Intel和S&P月涨跌幅数据
head(da)
rtn=ts(log(da$intc+1),frequency=12,start=c(1973,1)) # 对数收益率r
Box.test(rtn,lag=12,type='Ljung') # r自相关系数为零
Box.test(abs(rtn),lag=12,type='Ljung') # |r|自相关系数为零
# P141:ARCH效应检验
error=rtn-mean(rtn)
Box.test(error^2,lag=12,type='Ljung') # Ljung-Box检验
source("archTest.R") # 调用Tsay(2015)程序
archTest(error,12) # Engle(1982)提出的LM检验
# P143:欧元兑美元汇率日涨跌幅的ARCH效应检验
fx=read.table("d-useu9910.txt",header=T)
head(fx)
eu=diff(log(fx$rate))
Box.test(eu,lag=20,type='Ljung')
Box.test(eu^2,lag=20,type='Ljung')
archTest(eu,20)
Tsay(2015)涉及的其他常用模型包括:
学者 | 模型 |
---|---|
Bollerslev(1986) | 广义自回归条件异方差模型:GARCH |
Engle and Bollerslev (1986) | 求和GARCH模型:IGARCH |
Engle et al.(1987) | GARCH均值模型:GARCH-M |
Nelson(1991) | 指数GARCH模型:EGARCH |
Glosten et al.(1993), Zakoian(1994) | 门限GARCH模:TGARCH |
Ding et al.(1993) | 非对称幂ARCH:APARCH |
Engle and Ng(1993), Duan(1995) | 非对称GARCH模型:NGARCH |
在 rugarch
包中,上述模型可以通过设定相应的参数来实现:
1. | 方差模型(variance model) |
---|---|
model | “sGARCH”, “fGARCH”, “eGARCH”, “gjrGARCH”, “apARCH”, “iGARCH”, “csGARCH” |
garchOrder | c(m,n) |
submodel | 对于“fGARCH”模型,可进一步设定 “GARCH”, “TGARCH”, “AVGARCH”, “NGARCH”, “NAGARCH”, “APARCH”, “GJRGARCH”, “ALLGARCH” |
external.regressors | 外部解释变量,默认为 NULL |
2. | 均值模型(mean model) |
armaOrder | c(p,q) |
include.mean | T/F:均值方程中是否有 |
archm | T/F: 均值方程中是否包含波动率 |
archpow | 1/2: archm=T 时设定包含 还是 |
arfima | T/F |
external.regressors | 外部解释变量,默认为 NULL |
3. | 的分布模型(distribution model) |
norm | normal distibution |
snorm | skew-normal distribution |
std | student-t |
sstd | skew-student |
ged | generalized error distribution |
sged | skew-generalized error distribution |
nig | normal inverse gaussian distribution |
ghyp | Generalized Hyperbolic |
jsu | Johnson's SU distribution |
以ARCH(1)模型为例:
相应的均值和方差为:
# P151:Intel收益率ARCH(1)模型
int_arch1=garchFit(~1+garch(1,0),data=rtn,trace=F,
cond.dist="norm") # fGarch包中命令,默认为正态分布,可设定学生分布 cond.dist="std" 等
summary(int_arch1)
plot(int_arch1) # 回归结果作图,13个选项,0可退出
resi=residuals(int_arch1,standardize=T) # 提取残差作图
tdx=c(1:444)/12+1973
par(mfcol=c(3,1))
plot(tdx,resi,xlab='year',ylab='stand-resi',type='l')
acf(resi,lag=20)
pacf(resi^2,lag=20)
library(rugarch) # 用rugarch包中命令复制结果
intel.set=ugarchspec(variance.model = list(model="sGARCH", garchOrder=c(1,0)),
mean.model = list(armaOrder=c(0,0), include.mean=T))
ugarchfit(spec=intel.set, data=rtn)
# P154:欧元兑美元汇率
eu_arch11=garchFit(~1+garch(11,0),data=eu,trace=F)
summary(eu_arch11)
eu.set=ugarchspec(variance.model = list(model="sGARCH", garchOrder=c(11,0)),
mean.model = list(armaOrder=c(0,0), include.mean=T))
ugarchfit(spec=eu.set, data=eu)
Bollerslev(1986)提出的标准GARCH(m,n)模型为:
和 分别称为ARCH参数和GARCH参数。以GARCH(1,1)模型为例,有:
# P159: Intel收益率GARCH(1,1)模型
int_garch11=garchFit(~1+garch(1,1),data=rtn,trace=F) # (4-18)式
summary(int_garch11)
v1=volatility(int_garch11) # sigma_t的估计值
# 预测并作图
par(mfcol=c(1,1))
upp=0.0113+2*v1
low=0.0113-2*v1
tdx=c(1:444)/12+1973
plot(tdx,rtn,xlab='year',ylab='series',type='l',ylim=c(-0.6,0.6))
lines(tdx,upp,lty=2,col='red')
lines(tdx,low,lty=2,col='red')
abline(h=c(0.0113))
int_garch11t=garchFit(~1+garch(1,1),data=rtn,trace=F,
cond.dist="std") # 假定t分布,(4-19)式
summary(int_garch11t)
v2=volatility(int_garch11t)
int_garch11st=garchFit(~1+garch(1,1),data=rtn,trace=F,
cond.dist='sstd') # 假定有偏的t分布,(4-20)式
summary(int_garch11st)
v3=volatility(int_garch11st)
cor(cbind(v1,v2,v3)) # 3种模型得到sigma_t的相关系数
此外,我们也可以用rugarch
包中的命令重新回归方程(4.18)-(4.20):
int_garch.sec= ugarchspec(variance.model = list(model="sGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0),include.mean=T))
ugarchfit(spec=int_garch.sec, data=rtn)
int_garcht.sec= ugarchspec(variance.model = list(model="sGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0),include.mean=T),
distribution.model = "std")
ugarchfit(spec=int_garcht.sec, data=rtn)
int_garchtst.sec= ugarchspec(variance.model = list(model="sGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0),include.mean=T),
distribution.model = "sstd")
ugarchfit(spec=int_garchtst.sec, data=rtn)
以GARCH(1,1)为例,当 时,波动率的方差将趋于无穷大,波动率变成非平稳过程。因此类似于ARIMA模型,IGARCH模型就是单位根GARCH模型,IGARCH(1,1)模型为:
# P165(4-22)式:
int_igarch.sec= ugarchspec(variance.model = list(model="iGARCH"),
mean.model = list(armaOrder=c(0,0),include.mean=T))
ugarchfit(spec=int_igarch.sec, data=rtn)
GARCH-M模型考虑波动率对收益率的直接影响,因此在下述模型中 为风险溢价参数:
# P166(4-25,26)式:
int_garchm.sec= ugarchspec(
variance.model = list(model="sGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0),include.mean=T,archm=T,archpow=2))
ugarchfit(spec=int_garchm.sec, data=rtn*100)
用苹果公司股票数据 AAPL.RData 计算回报率,并建立 GARCH(1,1) 模型:
library(quantmod)
loadSymbols("AAPL", from="2006-01-01") # 获取苹果公司数据
chartSeries(AAPL) # 股价作图
ret.aapl <- dailyReturn(Cl(AAPL), type='log') # 用对数收盘价计算日收益率
chartSeries(ret.aapl) # 收益率作图
garch11.spec = ugarchspec(variance.model = list(model="sGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0)))
aapl.garch11.fit = ugarchfit(spec=garch11.spec, data=ret.aapl)
aapl.garch11.fit
# 结果报告
coef(aapl.garch11.fit) #estimated coefficients
vcov(aapl.garch11.fit) #covariance matrix of parameter estimates
infocriteria(aapl.garch11.fit) #common information criteria list
newsimpact(aapl.garch11.fit) #calculate news impact curve
signbias(aapl.garch11.fit) #Engle - Ng sign bias test
fitted(aapl.garch11.fit) #obtarchin the fitted data series
residuals(aapl.garch11.fit) #obtain the residuals
uncvariance(aapl.garch11.fit) #unconditional (long-run) variance
uncmean(aapl.garch11.fit) #unconditional (long-run) mean
# SGARCH news impact curve
ni.garch11 <- newsimpact(aapl.garch11.fit)
plot(ni.garch11$zx, ni.garch11$zy, type="l", lwd=2, col="blue", main="GARCH(1,1) - News Impact", ylab=ni.garch11$yexpr, xlab=ni.garch11$xexpr)
课堂讨论:假定 服从t分布和有偏的t分布,重新回归 GARCH(1,1) 模型。