[R-SIG-Finance] Slight Discrepancy between ugarchfit and by hand calculation
alexios ghalanos
alexios at 4dscape.com
Mon Jul 14 21:27:56 CEST 2014
The reason you are getting a discrepancy is because you have not been diligent in either reading the documentation or the c-code which show the GARCH recursion:
1. The specification you use is the default one ARMA(1,1)-GARCH(1,1) but you filter the returns ONLY for a constant.
2. Your code does not include start-up recursion conditions.
Here is the correct way to do it:
#################################
library(rugarch)
data(sp500ret)
calcResiduals<-function(returns, mu){
# pass the correct mu which is estimated jointly in the ML procedure
returns-mu
}
standardGarch<-function(returns, alpha, beta, omega, mu){
residuals<-calcResiduals(returns, mu)
sigmaVect<-rep(0, length(returns))
# recursion initialisation value h0
h0 = mean(residuals^2)
for(i in 1:length(returns)){
if(i==1){
sigmaVect[i]<-h0
} else{
sigmaVect[i]<-omega+alpha*residuals[i-1]^2+beta*sigmaVect[i-1]
}
}
return(sigmaVect)
}
returns<-sp500ret[,1] #leave off the dates for now
# armaOrder=c(0,0) since default is (1,1)
spec<-ugarchspec(mean.model=list(armaOrder=c(0,0)),variance.model=list(model="sGARCH"))
fit<-ugarchfit(spec, sp500ret)
alpha<-coef(fit)["alpha1"]
beta<-coef(fit)["beta1"]
omega<-coef(fit)["omega"]
mu<-coef(fit)["mu"]
vals<-standardGarch(returns,alpha, beta, omega, mu)
# all.equal is the way to compare, NOT visually if you want exact results:
all.equal(as.numeric(sigma(fit)), sqrt(as.numeric(vals)))
################################
-Alexios
On 14 Jul 2014, at 20:02, Tevlin, Dylan <Dylan.Tevlin at kochind.com> wrote:
> Running R 2.1.14 on Windows 7.
>
> My problem is the following:
>
> I'm using the sp500ret data, approximating the parameters using ugarchfit, and then calculating the conditional variances "by hand" and plotting on top of the 3rd graph from plot.ugarchfit. This task may seem silly but bear with me. All the following code is ready to run copy and paste.
>
> The discrepancy I'm finding is consistent over different models, so let's just look at a Garch(1,1)
>
> #start code ****************
> library(rugarch)
> data(sp500ret)
>
> #by hand functions
> calcResiduals<-function(returns){
> #returns a list of the returns residuals
>
> #returns :a vector or time series of floats
>
> returns-mean(returns)
> }
>
> standardGarch<-function(returns, alpha, beta, omega){
> #standard garch per rugarch documentation
>
> #returns :a vector or time series of floats
> #alpha :a float, estimation of the alpha parameter
> #beta :a float, estimation of the beta parameter
> #omega :a float, estimation of the omega parameter
>
> residuals<-calcResiduals(returns)
> #now we have our iterative recursive method
> sigmaVect<-rep(0, length(returns)+1)
>
> for(i in 1:length(returns)){
> sigmaVect[i+1]<-omega+alpha*residuals[i]^2+beta*sigmaVect[i]
> }
> sigmaVect
> }
>
> returns<-sp500ret[,1] #leave off the dates for now
>
> spec<-ugarchspec(variance.model=list(model="sGARCH"))
> fit<-ugarchfit(spec, sp500ret)
> fit
> plot(fit) #here press 3 then enter on the console
> #then press 0 then enter to exit the graph menu
>
> #I got the following as estimates for my parameters from ugarchfit
> #these should be changed to the appropriate ones you get in output
> #they should be near the same
> alpha<-.087817
> beta<-.904886
> omega<-.000001
>
> vals<-standardGarch(returns,alpha, beta, omega )
> vals<-vals[2:(length(vals))]
> vals<-as.data.frame(vals)
> rownames(vals)<-rownames(sp500ret) #attach the dates
>
> #yes, the dates are most likely a bit off, that won't matter to much for the visual
> #representation though. My issue is a vertical shift difference
>
> lines(sqrt(vals), col="purple")
>
> #plot the by hand calculation on top of the ugarchfit graph.
>
> #end code *******************
>
> On my machine, I see a slight discrepancy between my function's calculation of the conditional variances and the ugarchfit calculation. To my understanding I implemented the formula used in ugarchfit for sGarch correctly, so there should be very little difference in the output.
>
> My first thoughts as to why this difference occurs is some difference in how C and R treat numerics and the conversion back and forth (this occurs in ugarchfit but not in mine), or possibly some truncation of the parameter estimates for display purposes, leading me to use parameter values that are slightly off.
>
> Can anyone provide an explanation as to what I am seeing here?
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>
More information about the R-SIG-Finance
mailing list