#install.packages("fGarch") library(fGarch) # GARCH Data Generation # Simple GARCH(1,1) Generation and ACF and PACF set.seed(2) a0 <- 0.2 a1 <- 0.5 b1 <- 0.3 w <- rnorm(10000) e <- rep(0, 10000) h <- rep(0, 10000) for (i in 2:10000) { h[i] <- a0 + a1 * (e[i-1]^2) + b1 * h[i-1] e[i] <- w[i]*sqrt(h[i]) } par(mfrow = c(2, 1)) acf(e) pacf(e^2) # GARCH simulation and Log Likelihood Function set.seed(1234) garch11<-garchSpec(model = list(omega = 1e-6, alpha = 0.2, beta = 0.6)) y<-garchSim(garch11, n = 1000) n<-length(y) sigma2<- vector(length = n) #estimating the parameters manually by mle assuming normal distribution ll <- function(theta) { sigma2[1]<-theta[1]/(1-theta[2]-theta[3]) #sigma2[1]<-var(y) for(i in 1:n) { sigma2[i+1]<-theta[1]+theta[2]*y[i]^2+theta[3]*sigma2[i] } R=dnorm(y[1:n], 0, sqrt(sigma2[1:n]), log = TRUE) L=-sum(R) return(L) } fit1 <- optim(theta<-c(0.0001,0.01,0.1),ll) fit1 fit2 <- garchFit(~garch(1,1), y, trace = FALSE) fit2 plot(fit2)