@fanxy
2020-05-03T18:16:31.000000Z
字数 6883
阅读 6104
樊潇彦
复旦大学经济学院
金融数据
# 准备工作
setwd("D:\\...\\Ch11")
## 调用
library(fGarch)
library(rugarch)
library(tidyverse)
library(readxl)
library(ggplot2)
library(tseries)
library(zoo)
library(forecast)
考虑到正的和负的资产收益率的非对称效应,Nelson(1991)提出了指数GARCH(m,n)模型:
注:Tsay(2015,P171)第二行:
与GARCH模型相比,EGARCH模型有两点改进:
1. 使用对数条件方差,放松了对模型系数非负性的限制;
2. 股价上升/下跌( 为正/负)对模型的影响不对称。当 时,价格下跌()将使资产波动率上升,存在杠杆效应。
load("AAPL.RData")
colnames(AAPL)
ret.aapl=diff(log(zooreg(AAPL)$AAPL.Close)) # 计算收益率
plot(ret.aapl)
egarch11.spec = ugarchspec(
variance.model = list(model="eGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0))) # 设定EGARCH(1,1)模型
aapl.egarch11.fit = ugarchfit(spec=egarch11.spec, data=ret.aapl)
aapl.egarch11.fit
plot(aapl.egarch11.fit,which=12) # 冲击影响非对称,存在杠杆效应
# 与GARCH(1,1)模型比较
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)
plot(aapl.garch11.fit,which=12) # 冲击影响对称,没有杠杆效应
# P171(4-34)式
source("Egarch.R")
da=read.table("m-ibmsp6709.txt",header=T)
head(da)
ibm=log(da$ibm+1)
Box.test(ibm,lag=12,type='Ljung')
m1=Egarch(ibm)
# 用rugarch包中命令回归
egarch11.spec= ugarchspec(
variance.model =list(model="eGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0),include.mean=T))
ibm.egarch11.fit = ugarchfit(spec=egarch11.spec, data=ibm)
ibm.egarch11.fit
# 欧元兑美元汇率数据,P174:(4-36)式
da=read.table("d-useu9910.txt",header=T)
eu=diff(log(da$rate))*100
source('Tgarch11.R') # 原文件中 filter 命令前加上 stats::
m1=Tgarch11(eu)
# 用rugarch包中命令回归
tgarch11.spec = ugarchspec(
variance.model = list(model="gjrGARCH", garchOrder=c(1,1)), # GJR模型
mean.model = list(armaOrder=c(0,0),include.mean=T))
eu.tgarch11.fit = ugarchfit(spec=tgarch11.spec, data=eu)
eu.tgarch11.fit
# TGARCH杠杆效应作图
plot(eu.tgarch11.fit, which=12)
# P175:汇率数据的APARCH模型
m1=garchFit(~1+aparch(1,1),data=eu,trace=F)
summary(m1)
m2=garchFit(~1+aparch(1,1),data=eu,trace=F,delta=2,include.delta=F) # delta=2, TGARCH(1,1)模型
summary(m2)
# 用rugarch包中命令回归
aparch11.spec = ugarchspec(
variance.model = list(model="apARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0),include.mean=T))
eu.aparch11.fit = ugarchfit(spec=aparch11.spec, data=eu)
# APARCH杠杆效应作图
plot(eu.aparch11.fit, which=12)
# P178:(4-41)式
source("Ngarch.R")
m1=Ngarch(eu)
summary(m1)
# 用rugarch包中命令回归
ngarch11.spec = ugarchspec(
variance.model = list(model="fGARCH",submodel = "NAGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(0,0)))
eu.ngarch11.fit = ugarchfit(spec=ngarch11.spec, data=eu)
# NGARCH杠杆效应作图
plot(eu.ngarch11.fit, which=12)
高频数据包括两种:一是每天同一价格(如收盘价),另一种是每天开盘、最高、最低、收盘(OHLC)四种价格,这两种高频的日度数据都可以用来计算低频的月度(或年度)收益率。用 TTR
包中的 volatility(OHLC, calc = "close", n = 10, N = 260, ...)
命令可以轻松实现,其中 n 为天数,N 为一年的交易日数,calc 为不同的估计方法,具体包括:
close
,Close-to-Close Volatility. parkinson
,Parkinson(1980) High-Low Volatility. garman.klass
,Garman and Klass(1980) assumes Brownian motion with zero drift and no opening jumps (i.e. the opening = close of the previous period). rogers.satchell
,Rogers and Satchell(1991) allows for non-zero drift, but assumed no opening jump. yang.zhang
,Yang and Zhang(2000) has minimum estimation error, and is independent of drift and opening gaps. It can be interpreted as a weighted average of the Rogers and Satchell estimator, the close-open volatility, and the open-close volatility.
da=read.table("d-sp58010.txt",header=T) # 1980-2010年S&P500数据
head(da)
ohlc <- da[,c("Open","High","Low","Close")]
library(TTR)
vClose <- volatility(ohlc, calc="close")
plot(vClose,type="l")
par(mfcol=c(3,1))
tdx=c(1:7737)/253+1980
vYZ63 <- volatility(ohlc, calc="yang.zhang", n=63, N=252) # 窗口为63期
plot(x=tdx,vYZ63,type="l",xlab="")
vYZ32 <- volatility(ohlc, calc="yang.zhang", n=32, N=252) # 窗口为32期
plot(x=tdx,vYZ32,type="l",xlab="")
cl=da[,"Close"]
rtn=diff(log(cl)) # 计算收盘价对数回报率
armagarch= ugarchspec(
variance.model = list(model="sGARCH", garchOrder=c(1,1)),
mean.model = list(armaOrder=c(4,0),include.mean=T))
fitted=ugarchfit(spec=armagarch, data=rtn) # P186:ARMA-GARCH模型
names(fitted@fit) # 查看估计结果的名称
vARMAGARCH=fitted@fit$sigma
plot(x=tdx[-1],vARMAGARCH,type="l",xlab="") # sigma作图
aapl.garch11.fit = ugarchfit(spec=garch11.spec, data=ret.aapl, out.sample=20)
aapl.garch11.fcst = ugarchforecast(aapl.garch11.fit, n.ahead=10, n.roll=10)
plot(aapl.garch11.fcst)
plot(aapl.garch11.fcst, which='all')
纽约大学斯特恩商学院波动研究所的波动实验室提供了大量金融数据的建模结果: