文档库 最新最全的文档下载
当前位置:文档库 › 用R做GARCH建模

用R做GARCH建模

#安装需要的包
install.packages(fBasics)#金融基础
install.packages(fGarch)#金融GARCH
install.packages("rgarch", repos="https://www.wendangku.net/doc/1c7182457.html,")#用于IGARCH拟合

#读取所有数据
rate.all=read.table("D:\\paper_data\\rate.dat",1,colClasses=c("character","numeric","numeric","numeric"))
#取出USD数据
USD=rate.all$USD
#差分做直方图
################################################################
https://www.wendangku.net/doc/1c7182457.html,D=diff(rate.all$USD)
par(mfrow = c(1, 1))
hist(https://www.wendangku.net/doc/1c7182457.html,D,prob=T,col=0)
lines(density(https://www.wendangku.net/doc/1c7182457.html,D),lty=3)
x=seq(-1,1,0.001)
lines(x,dnorm(x,mean(https://www.wendangku.net/doc/1c7182457.html,D),sqrt(var(https://www.wendangku.net/doc/1c7182457.html,D))),lty=1)
title(main=("https://www.wendangku.net/doc/1c7182457.html,D直方图"),line=0.5)
legend(-0.06,35,c("样本密度","正态密度"),lty=c(3,1))
################################################################

##################https://www.wendangku.net/doc/1c7182457.html,D的单位根检验#############
library(fUnitRoots)
https://www.wendangku.net/doc/1c7182457.html,d=adfTest(https://www.wendangku.net/doc/1c7182457.html,D)
https://www.wendangku.net/doc/1c7182457.html,d=unitrootTest(https://www.wendangku.net/doc/1c7182457.html,D)
##########################################


##################正态性检验###############################
library(fBasics)
skewness(https://www.wendangku.net/doc/1c7182457.html,D)
kurtosis(https://www.wendangku.net/doc/1c7182457.html,D)
jarqueberaTest(https://www.wendangku.net/doc/1c7182457.html,D)
##########################################################




#######################画时间序列图,ACF图####################
par(mfrow = c(3, 2))
ts.plot(USD)
acf(USD)
ts.plot(https://www.wendangku.net/doc/1c7182457.html,D)
acf(https://www.wendangku.net/doc/1c7182457.html,D)
ts.plot(https://www.wendangku.net/doc/1c7182457.html,D^2)
acf(https://www.wendangku.net/doc/1c7182457.html,D^2)
############################################################################


######xi为x的i阶滞后#######################################################
x1=lag(https://www.wendangku.net/doc/1c7182457.html,D)
x1=c(0,x1[1:311])
x2=lag(x1)
x2=c(0,x2[1:311])
x3=lag(x2)
x3=c(0,x3[1:311])
x4=lag(x3)
x4=c(0,x4[1:311])
##############################################################################

######################X^2的LM检验#######################################
par(mfrow = c(1, 1))
https://www.wendangku.net/doc/1c7182457.html,D1=x1*x1
https://www.wendangku.net/doc/1c7182457.html,D2=x2*x2
https://www.wendangku.net/doc/1c7182457.html,D3=x2*x3
https://www.wendangku.net/doc/1c7182457.html,D4=x4*x4
#回归
https://www.wendangku.net/doc/1c7182457.html,d=lm(https://www.wendangku.net/doc/1c7182457.html,D^2~https://www.wendangku.net/doc/1c7182457.html,D1+https://www.wendangku.net/doc/1c7182457.html,D2+https://www.wendangku.net/doc/1c7182457.html,D3+https://www.wendangku.net/doc/1c7182457.html,D4)
summary(https://www.wendangku.net/doc/1c7182457.html,d)
t=summary(https://www.wendangku.net/doc/1c7182457.html,d)[[9]] *312
#卡方检验
1-pchisq(t,4)
par(mfrow = c(2,2))
#plot(https://www.wendangku.net/doc/1c7182457.html,d)
############################################################################


#####GARCH拟合###################################################
#加载fGarch
library(fGarch);
#正态的GARCH(1,1)
usd.gar=garchFit(~garch(1,1),data = https://www.wendangku.net/doc/1c7182457.html,D,cond.dist="norm");
summary(usd.gar)
plot(usd.gar)

#偏态的GARCH(1,1)
usd.gar01=garchFit(~garch(1,1),data = https://www.wendangku.net/doc/1c7182457.html,D,cond.dist="snorm");
summary(usd.gar01)
plot(usd.gar01)
##预测
predict(usd.gar01,10)

#################################################################


#######iGARCH拟合#############################################
#加载rgarch
library(rgarch);

#方差模型
variance.model=list(model="iGARCH",garchOrder=c(1,1),submodel=NULL,external.regressors=NULL);
#均值模型
mean.model=list(armaOrder=c(1,1),include.mean=T,garchInMean=F,inMeanType=1,arfima=F,exteranl.regressors=NULL);
#模型说明
spec=ugarchspec(variance.model=variance.model,mean.mode=mean.

model,distribution.model="norm");
#igarch拟合
usd.igar=ugarchfit(data=https://www.wendangku.net/doc/1c7182457.html,D,spec=spec,out.sample=0,solver="solnp")
#igarch结果
usd.igar
#预测
https://www.wendangku.net/doc/1c7182457.html,d=ugarchforecast(usd.igar,n.ahead=100)
################################################################

相关文档