Skip to content

Instantly share code, notes, and snippets.

@kenchan0130
Created September 20, 2012 13:45
Show Gist options
  • Select an option

  • Save kenchan0130/3756017 to your computer and use it in GitHub Desktop.

Select an option

Save kenchan0130/3756017 to your computer and use it in GitHub Desktop.
fGarch of garchFit function v.s. tsereis of garch function
library(RFinanceYJ)
library(fGarch)
library(tseries)
topix <- quoteStockXtsData(x="998405.t",since="2001-01-04")
lr.topix <- log(lag(topix$Close)/topix$Close)[-1]
plot(lr.topix)
garch.fit <- garchFit(~garch(1,1),data=lr.topix,trace=F)
garch.fit2 <- garch(lr.topix,order=c(3,1))
garch.fit2$n.likeli
garch.fit@fit$llh
aic <- bic <- matrix(0,9,3)
colnames(aic) <- c("p","q","aic")
colnames(bic) <- c("p","q","bic")
k=1
for(q in 1:3){
for(p in 1:3){
x <- garch(lr.topix,order=c(p,q))$n.likeli
aic[k,] <- c(p,q,2*(-x+q+p)/length(lr.topix))
bic[k,] <- c(p,q,(2*(-x+q+p)/length(lr.topix))+log(length(lr.topix)*(p+q))/length(lr.topix))
k <- k+1
}
}
cbind(aic,bic)
aic <- bic <- matrix(0,9,3)
colnames(aic) <- c("p","q","aic")
colnames(bic) <- c("p","q","bic")
k=1
for(q in 1:3){
for(p in 1:3){
expr <- formula(paste("~garch(",p,",",q,")"))
x <- garchFit(expr,data=lr.topix,trace=F)@fit$llh
aic[k,] <- c(p,q,2*(-x+q+p)/length(lr.topix))
bic[k,] <- c(p,q,(2*(-x+q+p)/length(lr.topix))+log(length(lr.topix)*(p+q))/length(lr.topix))
k <- k+1
}
}
cbind(aic,bic)
myxts <- function(x) xts(x,index(topix)[-1])
plot(scale(xts(garch.fit@residuals,index(topix)[-1])),main="Standardized Residuals(fGarch)")
plot(scale(xts(garch.fit2$residuals,index(topix)[-1])),main="Standardized Residuals(tseries)")
plot(myxts([email protected]),main="Conditional SD(fGarch)")
plot(myxts(garch.fit2$fitted[,1]),main="Conditional SD(tseries)")
library(car)
qqPlot(garch.fit@residuals,main="Normal Q-Q Plot(fGarch)",xlab="Theorical Quantiles",ylab="Sample Quantiles")
qqPlot(garch.fit2$residuals,main="Normal Q-Q Plot(tseries)",xlab="Theorical Quantiles",ylab="Sample Quantiles")
plot(lr.topix,main="Series with Conditional SD(fGarch)")
lines(myxts([email protected]),col=2)
lines(-myxts([email protected]),col=2)
plot(lr.topix,main="Series with Conditional SD(tseries)")
lines(myxts(garch.fit2$fitted[,1]),col=2,type="l")
lines(myxts(garch.fit2$fitted[,2]),col=2,type="l")
par(mfrow=c(2,1))
acf(lr.topix,main="acf of Observations")
acf(lr.topix^2,main="acf of Observations^2")
acf(garch.fit@residuals,na.action = na.pass,main="acf of Residuals(fGarch)")
acf((garch.fit@residuals)^2,na.action = na.pass,main="acf of Residuals^2(fGarch)")
acf(garch.fit2$residuals,na.action = na.pass,main="acf of Residuals(tseries)")
acf((garch.fit2$residuals)^2,na.action = na.pass,main="acf of Residuals^2(tseries)")
@lanealencar
Copy link
Copy Markdown

This library is amazing, allowing to choose dist. t-Student.
Is there a version to include explanatory variables in the mean of y_t?
Thank you
Lane

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment