[英]Estimation of rolling Value at Risk (VaR) using R
我需要對每日股票收益進行滾動VaR估計。 起初我做了以下事情:
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)
它執行計算並返回一個zoo對象,但給出了一系列警告,如下所示:
VaR calculation produces unreliable result (inverse risk) for column: 1 : -0.00030977098532231
然后,我嘗試了同樣的數據樣本,如下所示:
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)
但是不會返回任何動物園對象並給出以下警告和錯誤:
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
從之前的文章( 使用rollapply函數進行使用R的VaR計算 )我理解,如果缺少完整的滾動窗口,則無法執行滾動估計,但在我的數據(sample2.dta)中沒有缺失值。
sample2.dta可以從https://drive.google.com/file/d/0B8usDJAPeV85WDdDQTFEbGQwaUU/edit?usp=sharing下載
有誰可以幫我解決和理解這個問題?
問題是有時60周期窗口的數據沒有變化。
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
我已經在R-Forge(r3525)上為PerformanceAnalytics提供了一個補丁,允許NaN
通過reaonableness檢查。
1)我們可以僅使用VaR
重現警告,如下所示:
> 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
嘗試使用不同的method=
。
> VaR(R = edhec[seq(25, length=60), 5], p = .95, method = "gaussian", invert = TRUE)
Equity Market Neutral
VaR -0.001499347
2)對於"gaussian"
我仍然在實際數據集上收到警告但沒有錯誤。 嘗試嘗試其他可用的"method"
參數值。 見?VaR
。
3)注意by.column = TRUE
可以省略,因為它是默認值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.