rm(list=ls()) require(zoo) require(ggplot2) require(rugarch) require(MASS) require(moments) source("LoadFundData.R") # source("LoadWigData.R") par(mfrow=c(2,1), cex = 0.7, bty="l") plot(P, main="Level", xlab="", ylab="") plot(r, main="returns", xlab="", ylab="") ##################################### # VaR model settings # ##################################### T <- length(r) # full sample, nobs N <- 1250 # we shorten the sample to the last 5 years r <- tail(r,N) # log-returns R <- coredata(r) p <- 0.05 # tolerance level H <- 1 # horizon ########################################################## # Historical simulation # ########################################################## # Value at Risk - VaR R0 <- sort(R) N0 <- floor(N*p) # p-th quantile VaR_HS <- R0[N0] # compare to: quantile(R,p) # Expected shortfall - ES ES_HS <- mean(R0[1:N0]) # mean value below VaR # histogram and VaR hist(R, main="Historical simulation", xlab="return", ylab="nobs", nclass = 500, col="black") abline(v=c(VaR_HS,ES_HS) ,lwd=2, col=c("blue","red"),lty=c(2,2)) legend("left", c("VaR for hist. sim.","ES for hist. sim."), lty=c(2,2), col=c("blue","red"), bty="n") # VaR confidence interval d1 <- density(R) x <- d1$x temp <- tail(which(x