MC_VaRox <- function(T=1250, N=1000, DGP=1, garch=F) { ################################################################################ # # # Monte Carlo Simulation by Breno Neri (calls Ox during optimizations) # # # # Comparing VaR Methodologies # # # # breno.neri@nyu.edu http://homepages.nyu.edu/~bpn207 # # # # January, 2007 - Version 8 # # # ################################################################################ # # # This code is a Monte Carlo simulation to compare seven different # # methodologies for estimating 1-day-ahead-forecast VaR time series, namely: # # 1- RiskMetrics model developed by J.P. Morgan, 1996; # # 2- GARCH(1, 1) with normal errors; # # 3- APARCH(1, 1) with skewed-t errors; # # 4- ARCH(1) Quantile VaR, developed by Wu and Xiao, 2002; # # 5- ARCH(0) Quantile VaR; # # 6- QAR(1)-VaR (Quantile AutoRegressive VaR), introduced by me and my thesis # # advisor, Luiz Renato Lima (see Lima and Neri, 2006); # # 7- QAR(0)-VaR. # # # # The first three methodology are estimated by an Ox code called by the # # VaRox.R code! # # # # The comparations are peformed by five different backtests, namely: # # 1- Kupiec (1995), Unconditional Coverage; # # 2- Christoffersen (1997), Independence; # # 3- Christoffersen (1997), Conditional Coverage; # # 4- Engle and Manganelli (2002), Dynamic Quantile; # # 5- Lopez (1998), Magnitude Loss Function. # # # # The DGPs analyzed present conditional heteroskedasticity (ARCH Effect): # # Y[t] = alfa*Y[t-1] + u[t], u[t] = sqrt(H[t])*E[t], H[t] = 1 + 0.5*u[t-1]^2, # # E[t] are independents and identically distributed fundamental innovations. # # Optionally, a GARCH Effect can be introduced: # # H[t] = 1 + 0.5*u[t-1]^2 + 0.5*(garch==T)*H[t-1], where (garch==T) is an # # indicator function. It is 1 if garch==T and it is 0 if garch==F. # # # # You enter a sample size (T), a number of replications (N), if there is GARCH # # effect (garch=T), and you can choose from fifteen different DGP # # specifications, namely: # # 1- alfa = 0 and E[t] ~ i.i.d. Standard Gaussian; # # 2- alfa = 0 and E[t] ~ i.i.d. Student-t with 3 degrees of freedom; # # 3- alfa = 0 and E[t] ~ i.i.d. Chi-Squared with 1 degree of freedom - 1; # # 4- alfa = 0 and E[t] ~ i.i.d. 2 - Gamma(2, 1); # # 5- alfa = 0 and E[t] ~ i.i.d. I[v[t]<=0.2]*Chi-Squared(1) + # # I[0.20.8]*Dirac(-4), # # v[t] ~ i.i.d. Uniform[0, 1]; # # 6- alfa = 0.5 and E[t] ~ i.i.d. Standard Gaussian; # # 7- alfa = 0.5 and E[t] ~ i.i.d. Student-t with 3 degrees of freedom; # # 8- alfa = 0.5 and E[t] ~ i.i.d. Chi-Squared with 1 degree of freedom - 1; # # 9- alfa = 0.5 and E[t] ~ i.i.d. 2 - Gamma(2, 1); # # 10- alfa = 0.5 and E[t] ~ i.i.d. I[v[t]<=0.2]*Chi-Squared(1) + # # I[0.20.8]*Dirac(-4), # # v[t] ~ i.i.d. Uniform[0, 1]. # # 11- alfa = S[t] and E[t] ~ i.i.d. Standard Gaussian; # # 12- alfa = S[t] and E[t] ~ i.i.d. Student-t with 3 degrees of freedom; # # 13- alfa = S[t] and E[t] ~ i.i.d. Chi-Squared with 1 degree of freedom - 1; # # 14- alfa = S[t] and E[t] ~ i.i.d. 2 - Gamma(2, 1); # # 15- alfa = S[t] and E[t] ~ i.i.d. I[v[t]<=0.2]*Chi-Squared(1) + # # I[0.20.8]*Dirac(-4), # # v[t] ~ i.i.d. Uniform[0, 1]. # # # # Where S[t] is given by: if(u[t]<0.5) S[t] = -0.5 else S[t] = 0.5 # # # # The code generates N (the number of replications) files that are saved as # # MC_VaR.'DGP'.'i'.R, where DGP and i are numbers indicating the choosen DGP # # and the replication, respectively. Each file contains a list (one kind of R # # object) named "result" with the results of the function BackTest (see # # BackTest.R). To access these results, use the command load "file". These # # results can be analysed by the Result.Generator.R code. # # # # Defaults: tau=0.01 and win=250, due to regulatory obligation (1996 Amendment # # to Basle Capital Accord); Standard Normal DGP (DGP=1); 1000 replications # # (N=1000) and a sample size of 1250 (T=1250). Remember that the first 250 # # (in fact, win) observations are lost in the estimation (see VaR.R). The # # remaining 1000 observations are approximately equivalent to a four years # # time series. garch=F. # # # # OBS.: This Monte Carlo Simulation may take weeks to run, depending on your # # hardware resources! # # # ################################################################################ # # # You may use this code only if you accept the conditions: # # (1) I am not liable for any problem caused by this code (i.e. you use it at # # your own risk); # # (2) You must give me credit in your papers where this code has been used. # # # ################################################################################ source("VaRox.R") source("BackTest.R") i <- 1 repeat { if(DGP==1|DGP==6|DGP==11) e <- rnorm(T) else if(DGP==2|DGP==7|DGP==12) e <- rt(T, df=3) else if(DGP==3|DGP==8|DGP==13) e <- rchisq(T, df=1)-1 else if(DGP==4|DGP==9|DGP==14) e <- 2-rgamma(T, 2, 1) else if(DGP==5|DGP==10|DGP==15) { u <- runif(T) e <- (u<=.8)*rchisq(T, 1) - 4*(u>.2) } else error("DGP must be 1, 2, 3, ..., or 15!") h <- 1 u <- sqrt(h)*e[1] for(j in 2:T) { h[j] <- 1 + .5*u[j-1]^2 + .5*(garch==T)*h[j-1] u[j] <- sqrt(h[j])*e[j] } y <- u if(DGP>5&DGP<11) for(j in 2:T) y[j] <- .5*y[j-1] + u[j] else if(DGP>10) for(j in 2:T) if(u[j]<0) y[j] <- -.5*y[j-1] + u[j] else y[j] <- .5*y[j-1] + u[j] V <- suppressWarnings(VaR(y)) if(is.character(V)) next result <- BackTest(y, V, .01) save(result, file=paste("MC_VaR", DGP, i, "R", sep=".")) if(i==N) break i <- i+1 } }