I have a code that successfully calculates VaR based on the theory of extreme values ββusing historical data. I am trying to run the same code on several simulated price paths (i.e., Calculating the VaR for each path), and then accepting the middle or middle of these VaRs.
Every example that I could find on the Internet had a simulation function that returned the price at the end of the period, and then they copied the X function many times. This makes sense to me, except that I essentially need to calculate the risk value for each simulated path . Below is the code that I have. I can say that the code works when using historical data (i.e., the "evt" function works fine, and the datatable fills correctly when the lines lossOnly, u and evtVar are not in the function). However, I tried to implement the simulation in the second function and try various combinations that failed.
library('RODBC') library('nor1mix') library('fExtremes') library('QRM') library('fGarch') #function for computing the EVT VaR evt <- function(data,u){ #fit excess returns to gpd to get estimates gpdfit = tryCatch({ gpdfit <- gpdFit(data,u,type="mle") }, warning = function(w) { gpdfit <- gpdFit(data,u,type="mle",optfunc="nlminb") return(gpdfit) }, error = function(e) { gpdfit <- gpdFit(data,u,type="pwm",optfunc="nlminb") return(gpdfit) }, finally = {}) #now calculate VaRs xi <- gpdfit@fit $par.ests["xi"] beta <- gpdfit@fit $par.ests["beta"] Nu <- length( gpdfit@data $exceedances) n <- length(data) evtVar95 <- (u+((beta/xi)*(((n/Nu)*.05)^(-xi) - 1.)))*100 evtVar99 <- (u+((beta/xi)*(((n/Nu)*.01)^(-xi) - 1.)))*100 evtVar997 <- (u+((beta/xi)*(((n/Nu)*.003)^(-xi) - 1.)))*100 evtVar999 <- (u+((beta/xi)*(((n/Nu)*.001)^(-xi) - 1.)))*100 #return calculations return(cbind(evtVar95,evtVar99,evtVar997,evtVar999,u,xi,beta,Nu,n)) } #data <- read.table("pricedata.txt") prices <- data$V1 returns <- diff(log(prices)) #or returns <- log(prices[-1]/prices[-n]) xi <- mean(returns) std <- sd(returns) N <- length(prices) lstval <- prices[N] options(scipen = 999) p <- c(lstval, rep(NA, N-1)) gen.path <- function(){ N <- length(prices) for(i in 2:N) p[i] <- p[i-1] * exp(rnorm(1, xi, std)) # plot(p, type = "l", col = "brown", main = "Simulated Price") #evt calculation #first get only the losses and then make them absolute lossOnly <- abs(p[p<0]) #get threshold u <- quantile(lossOnly, probs = 0.9, names=FALSE) evtVar <- evt(lossOnly,u) return(evtVar) } runs <- 10 sim.evtVar <- replicate(runs, gen.path()) evtVar <- mean(sim.evtVar) #add data to total table VaR <- c(evtVar[1],evtVar[2],evtVar[3],evtVar[4],evtVar[5],evtVar[6],evtVar[7],evtVar[8],evtVar[9]) DF <- data.frame(VaR, row.names=c("evtVar95","evtVaR_99","evtVaR_997","evtVaR_999","u","xi","beta","Nu","n"))
In short, I'm trying to run a value-at-risk function (first function) in a monte carlo function (second function) and trying to put the average simulated values ββin data tables. I know that the first function works, but this is the second function that drives me crazy. There are errors that I get:
> sim.evtVar <- replicate(runs, gen.path()) Error in if (xi > 0.5) { : missing value where TRUE/FALSE needed Called from: .gpdpwmFit(x, u) Browse[1]> evtVar <- mean(sim.evtVar) Error during wrapup: object 'sim.evtVar' not found Browse[1]> > #add data to total table > VaR <- c(evtVar[1],evtVar[2],evtVar[3],evtVar[4],evtVar[5],evtVar[6],evtVar[7],evtVar[8],evtVar[9]) Error: object 'evtVar' not found > DF <- data.frame(VaR, row.names=c("evtVar95","evtVaR_99","evtVaR_997","evtVaR_999","u","xi","beta","Nu","n")) Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ""function"" to a data.frame
Any help you can provide is greatly appreciated! Thank you in advance!