##------GARCH Model-------- library(tseries) VIX<-get.hist.quote(instrument = "^VIX" ,start="2015-01-01" ,end=Sys.Date() ,quote="AdjClose" ,provider = "yahoo") par(mfrow=c(1,2)) plot(ts(VIX$AdjClose),ylab="VIX") ## Why are we using SPY and not ^GSPC SnP.500<-get.hist.quote(instrument = "SPY" ,start="2015-01-01" ,end=Sys.Date() ,quote="AdjClose" ,provider = "yahoo") plot(ts(SnP.500$AdjClose),ylab="S&P 500") ## log-return ln_rt<-as.numeric(diff(log(SnP.500))) ## Fit GARCH(1,1) fit.garch<-garch(ln_rt,order=c(1,1)) ## Fitted Values sigma_hat<-fit.garch$fitted.values[,"sigt"] vol_hat<-sigma_hat*sqrt(252)*100 ## plot GARCH fitted volatility vs. VIX par(mfrow=c(1,1)) plot(ts(VIX$AdjClose),col="red",lwd=2,ylim=c(0,40)) lines(vol_hat,col="blue",lwd=2) text<-c("VIX","GARCH(1,1)") legend(350,40,text,col=c("red","blue"),lwd=c(2,2)) ##---- VaR------- # Daily volatility vol <- sd(ln_rt) # Daily average return rbar <- mean(ln_rt) ### n<-length(SnP.500) unit <- 1000 # Number of units of S&P 500 Value<-SnP.500[n] # Current Value of S&P 500 Value value_p <- Value*unit # Value of portfolio hp <- 1 # Holding period a <- .95 # Confidence level (5%) # Parametric VaR with Gaussian parvar_Gauss <- abs(value_p*qnorm(1-a,0,1)*vol*sqrt(hp)) # Parametric VaR with t(10) parvar_t <- abs(value_p*qt(1-a,df=10)*vol*sqrt(hp)) # Historical VaR hvar <- abs(quantile(ln_rt,1-a)*value_p) # Vector comparing the two VarS varv <- cbind(value_p,parvar_Gauss,parvar_t,hvar) names(varv)<- c("Portfolio Value","VaR Gaussian","VaR t(10)","Historical VaR") print(varv)