2016-01-08 108 views
1

我需要基于GARCH(1,1),EGARCH(1,1)和TGARCH(1,1)计算欧元/美元对10分钟时间间隔的平均波动率,并显示它们在一个情节。我的数据集存储在csv中。格式和数据频率是1分钟。我确实有下面的代码,但仅仅计算并绘制它就像在附图中一样。历史波动率计算和绘图

enter image description here

# installation of needed packages 
# install.packages("fBasics") 
# install.packages("tseries") 
# install.packages("car") 
# install.packages("FinTS") 
# install.packages("fGarch") 
# install.packages("rugarch") 

# Library 
library(xts) 
library(fBasics) # e.g. basicStats() 
library(tseries)# e.g. jarque.bera.test() 
library(car) # e.g. durbinWatsonTest() 
library(FinTS) # e.g. ArchTest() 
library(fGarch) # e.g. garchFit() 
library(rugarch) # e.g. ugarchfit() 

# Data import 
EUR <- read.csv("Data_Forecast/EURUSD.csv", 
      stringsAsFactors = FALSE) #o import data 

# First and last 6 data 
head(EUR) 
tail(EUR) 

# formatting the date and time 
EUR$Date <- strptime(EUR$Date, format = "%d.%m.%Y %H:%M:%S") 

str(EUR) 

EUR <- EUR[EUR$Date >= strptime("2015-01-01 23:00:00", format = "%Y-%m-%d %H:%M:%S"),] 

# we need just weekdays 
wday_ <- as.POSIXlt(EUR$Date)$wday 
EUR <- EUR[!wday_ %in% c(0,6) ,] 

# lets create vector of hours and minutes based on the existing data 
hour_ <- as.POSIXlt(EUR$Date)$hour 
minute_ <- as.POSIXlt(EUR$Date)$min 

########################################################################### 
# Exclude first and last 5 minutes 
exclude_ <- ((hour_ == 23 & minute_ < 6) | (hour_ == 22 & minute_ > 55))*1 

EUR <- EUR[exclude_ == 0,] 

head(EUR) 
tail(EUR) 


# xts object creation 
EUR.xts<-xts(EUR$Close,EUR$Date) 

# plot of original close pricess 
plot(EUR$Date,EUR$Close,type="l", col="blue", 
main="EUR/USD") 

# log-returns calculation 
EUR$r<-diff.xts(log(EUR$Close)) 

# log returns plot 
plot(EUR$Date,EUR$r,type="l", col="red", 
main="Log-returns of USD/JPY") 
abline(h=0,col="gray",lty=2) #abline functions adds line to the plot 


###################################################################### 
    #GARCH MODEL 
################################################################# 
# remove missing values and zeroes 
data_ <- EUR[!is.na(EUR$r) & EUR$r!=0,] 

k.garch11<-garchFit(~garch(1,1), 
       data = 100*data_$r, 
       include.mean=F, 
       cond.dist= "norm", # conditional distribution of errors 
       trace=FALSE) # if we don't want to see history of iterations 


summary(k.garch11) 
# lets see if the model converges on a shorter sample 
k.garch11<-garchFit(~garch(1,1), 
       data = 100*data_$r[1:15000], 
       include.mean=F, 
       cond.dist= "norm", # conditional distribution of errors 
       trace=FALSE) # if we don't want to see history of iterations 

k.garch11<-garchFit(~garch(1,1), 
       data = 100*data_$r[-c(1:15000)], 
       include.mean=F, 
       cond.dist= "norm", # conditional distribution of errors 
       trace=FALSE) # if we don't want to see history of iterations 

plot(100*data_$r[30000:32000], type="l") 

which(100*data_$r > 0.9) 

k.garch11<-garchFit(~garch(1,1), 
       data = 100*data_$r[-15000], 
       include.mean=F, 
       cond.dist= "norm", # conditional distribution of errors 
       trace=FALSE) # if we don't want to see history of iterations 

summary(k.garch11) 

# Let's assume that the final model is GARCH(1,1) 
str(k.garch11) 

# Plot of conditional variance estimates 
par(mfrow=c(2,1)) 
plot([email protected], # @data = original data values 
type="l",col="red",ylab="r",main="Log-returns of EUR/USD") 
plot([email protected], # @h.t = conditional variance 
type="l",col="blue", 
ylab="cvar",main="Estimated Volatility of EUR/USD") 
par(mfrow=c(1,1)) 


# Do standardized residuals come from normal distribution? 
stdres<[email protected]/sqrt([email protected]) 

hist(stdres,breaks=20,prob=T, 
main="Histogram of standardized residuals \n from GARCH(1,1) for EUR/USD") 

############################################################################  
# The EGARCH model  
######################################################################### 

# Let's examine whether conditional variance reacts asymmetrically 
# to the news arriving to the market. 
# Below estimation of the EGARCH(1,1) model. 

# ugarchfit() from rugarch package 

# lets first define a model specification 
spec = ugarchspec(# variance equation 
variance.model=list(model="eGARCH",garchOrder=c(1,1)), 
# sGARCH would stand for standard GARCH model 
# mean equation 
mean.model=list(armaOrder=c(0,0),include.mean=F), 
# assumed distribution of errors 
distribution.model="norm") 

# function doesn't accept missing values 
k.egarch11 = ugarchfit(spec=spec, data=na.omit(EUR$r)) 

k.egarch11 

# Plot of conditional standard deviation estimates (3) 
# and News-Impact curve (12). 
# ESC to exit 

plot(k.egarch11) 

##########################################################################         
# The TGARCH model 
################################################################### 

# lets first define a model specification 
spec = ugarchspec(# variance equation 
variance.model=list(model="fGARCH",garchOrder=c(1,1), 
        submodel="TGARCH"), 
# model="fGARCH" (family GARCH) together with submodel="TGARCH" 
# mean equation 
mean.model=list(armaOrder=c(0,0),include.mean=F), 
# assumed distribution of errors 
distribution.model="norm") 

# function doesn't accept missing values 
k.tgarch11 = ugarchfit(spec=spec, data=na.omit(EUR$r)) 

k.tgarch11 


# Plot of News-Impact curve (12). 
# ESC to exit 
plot(k.tgarch11) 

回答

1

我想你问的是如何叠加了多个地块。你连接的代码使用R.基本的绘图功能,假设每个地块都还现身在你的例子,这是你使用基本的绘图功能如何叠加多条曲线:

plot(k.garch11, xlim=c(X_min, X_max),ylim=c(Y_min, Y_max)) 
par(new=T) 
plot(k.tgarch11,xlim=c(X_min, X_max),ylim=c(Y_min, Y_max)) 
par(new=T) 
plot(k.egarch11,xlim=c(X_min, X_max),ylim=c(Y_min, Y_max)) 

xlimylim标准化轴限制,以便在数据范围内具有一致的轴。 X_min,X_max,Y_min,Y_max都是数字,由您指定。 par(new=T)告诉R不要创造一个新的全新情节,而是覆盖当前情节的下一个情节。

通过读取?par可获得其他绘图参数(如轴标签)。

?legend?axis将帮助您使图例和轴更漂亮。

+0

嗨,大卫,谢谢你的回答,但我需要不同的东西。我需要10分钟的时间间隔的平均波动率,我的代码没有这样做。我需要添加一些代码来计算它 –