问题描述
我的 R 代码:
library(PerformanceAnalytics)
prices <- c(10.4,11,10.11,9.19,10.63,9.68,12.89,9.8,12.57,8.23,9.27,9.51,10.51,9.66,9.52,10.78,9.47,11.87,11.33,11.38,11.16,8.94)
returns <- diff(prices)
print(returns)
VaR(returns,p=.95,method="historical")
VaR(returns,method="gaussian")
VaR(returns,method="modified")
输出:
print(returns)
[1] 0.60 -0.89 -0.92 1.44 -0.95 3.21 -3.09 2.77 -4.34 1.04 0.24 1.00 -0.85 -0.14 1.26
[16] -1.31 2.40 -0.54 0.05 -0.22 -2.22
VaR(returns,method="historical")
VaR calculation produces unreliable result (risk over 100%) for column: 1 : 3.09
[,1]
VaR -1
VaR(returns,method="gaussian")
VaR calculation produces unreliable result (risk over 100%) for column: 1 : 3.03916083148501
[,method="modified")
VaR calculation produces unreliable result (risk over 100%) for column: 1 : 3.1926697487747
[,1]
VaR -1
第一个参数在帮助中描述为 “资产返回的xts、VECTOR、矩阵、数据框、时间序列或动物园对象”
有什么问题?错误在哪里?
解决方法
尝试计算对数回报,然后计算 VaR
returns <- diff(log(prices))
VaR(returns,p = 0.95,method = "historical")
还要检查 PerformanceAnalytics 库中的 ES(预期短缺)函数。 我个人认为比 VaR 好很多。
,在考虑百分比回报时也有效。
returns <- rep(NA,length(prices) - 1)
for (i in 1:length(returns))
returns[i] <- (prices[i+1]-prices[i])/prices[i]