6

I need to perform rolling VaR estimation of daily stock returns. At first I did the following:

library(PerformanceAnalytics)
data(edhec)
sample<-edhec[,1:5]
var605<-rollapply(as.zoo(sample),width=60,FUN=function(x) VaR(R=x,p=.95,method="modified",invert=T),by.column=TRUE,fill=NA)

It performs the computation and returns a zoo object but gives a series of warnings as follows:

VaR calculation produces unreliable result (inverse risk) for column: 1 : -0.00030977098532231 

Then, I tried the same with sample of my data as follows:

library(foreign)
sample2 <- read.dta("sample2.dta")
sample2.xts <- xts(sample2[,-1],order.by=as.Date(sample2$datadate,format= "%Y-%m-%d"))
any(is.na(sample2.xts))
var605<-rollapply(as.zoo(sample2.xts),width=60,FUN=function(x) VaR(R=x,p=.95,method="modified",invert=T),by.column=TRUE,fill=NA)

But is does not return any zoo object and gives the following warnings and error:

VaR calculation produces unreliable result (inverse risk) for column: 1 : -0.0077322590200255
Error in if (eval(tmp < 0)) { : missing value where TRUE/FALSE needed
Called from: top level

From an earlier post (Using rollapply function for VaR calculation using R) I understand that rolling estimation cannot be performed if complete rolling window is missing, but in my data (sample2.dta) there are no missing values.

sample2.dta can be downloaded from https://drive.google.com/file/d/0B8usDJAPeV85WDdDQTFEbGQwaUU/edit?usp=sharing

Can anyone please help me to resolve and understand this issue?

Community
  • 1
  • 1
Jairaj Gupta
  • 347
  • 4
  • 16

2 Answers2

1

The problem is that sometimes there is no variation in your data for the 60-period window.

R> no_var <- rollapply(sample2.xts, 60, sd, by.column=TRUE)
R> any(no_var==0)
[1] TRUE
R> head(no_var[-(1:60),])
                  001034        001038 001055        001066 001109
1984-03-26 -0.0003322471 -0.0001498238      0 -0.0111818465      0
1984-03-27 -0.0003322471 -0.0001498238      0  0.0002076288      0
1984-03-28 -0.0003322471 -0.0545102488      0  0.0092900768      0
1984-03-29 -0.0199407074 -0.0565552432      0 -0.0183491390      0
1984-03-30  0.0192762133 -0.0023488011      0  0.0000000000      0
1984-04-02 -0.0003322471  0.0000000000      0  0.0560894683      0

I've committed a patch to PerformanceAnalytics on R-Forge (r3525) to allow the NaN to pass through the reaonableness check.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
0

1) We can reproduce the warning using only VaR as follows:

> VaR(R = edhec[seq(25, length=60), 5], p = .95, method = "modified", invert = TRUE)
VaR calculation produces unreliable result (inverse risk) for column: 1 : -0.000203691774704274
    Equity Market Neutral
VaR                    NA

Try using a different method=.

> VaR(R = edhec[seq(25, length=60), 5], p = .95, method = "gaussian", invert = TRUE)
    Equity Market Neutral
VaR          -0.001499347

2) With "gaussian" I still got warnings on the real data set but no errors. Try experimenting with the other "method" argument values that are available as well. See ?VaR .

3) Note that by.column = TRUE can be omitted as it is the default.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • I need to use modified VaR as it considers skewness and kurtosis as additional parameters while estimating the VaR. The real problem emerges when rolling estimation is done, as explained in the question. – Jairaj Gupta Aug 31 '14 at 16:47
  • Try replacing `function(x) VaR(...)` with `function(x) { out <- try(VaR(...)); if (inherits(out, "try-error")) NA else out }` to avoid having it stop with an error. – G. Grothendieck Aug 31 '14 at 17:38
  • It gives the error message but it does not stop and instead gives an answer. You can use `try(..., silent = TRUE)` if you don't want to see the error message. – G. Grothendieck Sep 02 '14 at 11:41
  • Thanks it does the job, and it also solves the problem of this post: http://stackoverflow.com/questions/25109672/var-calculation-with-complete-missing-column – Jairaj Gupta Sep 06 '14 at 18:12
  • But it take about 40 seconds per column; I have about 11,000 columns and the computation time shoots above 100 hours. Can you think of any faster alternative to this? – Jairaj Gupta Sep 07 '14 at 01:47