Empirical.Exercise <- function(y1, y2, Initial.Date="08/07/1996", Final.Date="24/03/2000") { ################################################################################ # # # Empirical Exercise Function by Breno de Andrade Pinheiro Néri # # # # brenoneri@gmail.com www.fgv.br/aluno/bneri # # # # November, 2005 - Version 1 # # # ################################################################################ # # # This code generates the empirical exercise of the paper "Comparing # # Value-at-Risk Methodologies", by Lima, Luiz Renato and Breno Néri, 2005b. # # # # You enter a matrix (y1), with the dates in the first column and the returns # # in the second one, another matrix (y2), with four columns, one for each of # # the four analysed VaR methodology, a Initial.Date and a Final.Date. # # # # OBS: The code can be easily modified to handle (adapt the graphics format) # # more than four VaR methodologies! # # # # Defaults: Initial.Date="08/07/1996" and Final.Date="24/03/2000" # # # ################################################################################ # # # 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. # # # ################################################################################ plots <- function(ts, text1, text2) { readline(prompt="Please, Press Enter to Continue!") message(text <- paste(text1, text2)) print(summary(ts)) message("Variance: ", var(ts)) message("Skewness: ", skewness(ts)) message("Kurtosis: ", kurtosis(ts)) message("Range: ", max(ts)-min(ts)) message("Length: ", length(ts)) suppressWarnings(print(adf.test(ts))) print(jarque.bera.test(ts)) layout(matrix(1:4, byrow=T, ncol=2)) hist(ts, 100, main=paste("Histrogram:", text), xlab=text2, col="gray") plot(density(ts), main=paste("Empirical Density:", text)) qqnorm(ts, main=paste("Normal Q-Q Plot:", text)) boxplot(ts, main=paste("Box Plot:", text)) } library(fBasics) library(tseries) source("BackTest.R") n1 <- c(1:length(y1[,1]))[y1[,1]==Initial.Date] n2 <- c(1:length(y1[,1]))[y1[,1]==Final.Date] print(BackTest(ts1<-ts(y1[n1:n2,2]), y2[n1:(n2-249),1:4])) ts2 <- ts(y2[n1:(n2-250),1:4], start=251) layout(matrix(1:4, byrow=T, ncol=2)) for(i in 1:4) { plot(ts1, main=paste(switch(i, "RiskMetrics", "Gaussian GARCH(1,1)", "Skewed Student-t APARCH(1,1)", "ARCH(1) Quantile"), "VaR(1%)"), ylab="Ibovespa Daily Return") lines(-ts2[,i], t="l", col="red", lty=2) } plots(ts1, "Ibovespa Daily", "Return") for(i in 1:4) plots(ts2[,i], switch(i, "RiskMetrics", "Gaussian GARCH(1,1)", "Skewed-t APARCH(1,1)", "ARCH(1) Quantile"), "") }