[英]Calculating Value at Risk with performanceanalytics package
I tried to calculate the Value at Risk for a list auf Stock Returns. 我试图计算auf股票收益清单的风险价值。 There are 1000 observations, but i wanted to calculate like the following: 有1000个观测值,但我想计算如下:
VaR for observation:
1 to 500
2 to 501
3 to 502
4 to 503
and 500 to 999
as you can see the result would be 500 calculations. 如您所见,结果将是500次计算。
To solve the problem I tried to use a if
condition with a for
loop. 为了解决该问题,我尝试将if
条件与for
循环一起使用。
like this: 像这样:
if(x < 501 & y < 1000){for(i in KO.Returns){VaR(KO.Returns[x: y], p = 0.95, method = "historical")}}
If I use the mentioned code I get the following error code: 如果使用上述代码,则会得到以下错误代码:
VaR calculation produces unreliable result (inverse risk) for column 1: VaR计算得出第1列的结果不可靠(逆风险):
I think the problem is in your data. 我认为问题出在您的数据中。 When you specify your window, the calculation of historical VaR sorts the data and picks out 95th percentile. 当您指定窗口时,历史VaR的计算将对数据进行排序,并选择第95个百分点。 Sometimes your data will not have a negative value in that percentile, thus historical VaR is meaningless (your losses cannot be a positive value, loss is always negative). 有时,您的数据在该百分比中不会具有负值,因此历史VaR毫无意义(您的损失不能为正值,损失始终为负)。 Hence the error. 因此,错误。
I have been trying to reproduce similar errors using the following code: 我一直在尝试使用以下代码重现类似的错误:
library(PerformanceAnalytics)
data("edhec")
data = edhec[, 5]
valat = rollapply(data = data, width = 20,
FUN = function(x) VaR(x, p = 0.95, method = "historical"),
by.column = TRUE)
valat
But when I change the confidence level to p = 0.99
, I stop getting the error. 但是,当我将置信度级别更改为p = 0.99
,我停止得到该错误。 So, maybe you can try to change your confidence level and see. 因此,也许您可以尝试更改您的置信度并查看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.