计算R中分布的预期缺口?

时间:2017-08-10 15:40:34

标签: r packages risk-management risk-analysis

我试图计算修改后的分布函数的预期缺陷。  我使用了“PerformanceAnalytics”包。

Fx=c(0.02469009, 0.07225651, 0.11750310, 0.16054298, 0.20148378, 0.24042788, 
0.27747265, 0.31271072, 0.34623021, 0.37811494, 0.40844464, 0.43729513, 
0.46473857, 0.49084358, 0.51567543, 0.53929622, 0.56176501, 0.58313798, 0.60346858, 0.62280765, 0.64120353, 0.65870224, 0.67534753, 0.69118102,  0.70624230, 0.72056903, 0.73419704, 0.74716040, 0.75949154, 0.77122127, 0.78237894, 0.79299245, 0.80308832, 0.81269182, 0.82182695,0.83051655, 0.83878236, 0.84664503, 0.85412424, 0.86123869, 0.86800616, 0.87444357,
0.88056703, 0.88639185, 0.89193258, 0.89720309, 0.90221656, 0.90698551, 0.91152188,0.91583701, 0.91994169, 0.92384618, 0.92756024, 0.93109317, 0.93445380, 0.93765052,0.94069134, 0.94358386, 0.94633531, 0.94895257, 0.99840417, 0.99842013, 0.99843531,0.99844975, 0.99846348, 0.99847654 ,0.99848897 ,0.99850079, 0.99851204, 0.99852273, 0.99853291,1)

library(PerformanceAnalytics)
ES(Fx,p=0.95)

ES calculation produces unreliable result (inverse risk) for column: 1 : -0.169916309516024
   [,1]
ES   NA

我不仅尝试了分布函数,还尝试了离散概率分布,但它给出了相同的警告。 我还看了一下VaRES包。但我的发行版不属于任何特定的参数分布。

这是计算预期差额的一种方式还是一种不同的方案?

1 个答案:

答案 0 :(得分:0)

我相信您会收到警告,因为您的数据中只有正值。 您可以简单地将返回值转换为零平均值,方法是从序列中减去平均值,然后将平均值加回到ES结果中。确保您实际上在Fx中有一个收益系列。

此外,您需要指定“历史”方法来从实际/经验分布中获取ES,而不是从参数分布中获取ES。

有关性能分析软件包的说明,请参阅第96页的ETL。

https://cran.r-project.org/web/packages/PerformanceAnalytics/PerformanceAnalytics.pdf

我已修复您的代码(假设Fx是返回序列),并将其添加到下面。

library(PerformanceAnalytics)

#Data
Fx=c(0.02469009, 0.07225651, 0.11750310, 0.16054298, 0.20148378, 0.24042788, 
     0.27747265, 0.31271072, 0.34623021, 0.37811494, 0.40844464, 0.43729513, 
     0.46473857, 0.49084358, 0.51567543, 0.53929622, 0.56176501, 0.58313798, 0.60346858, 0.62280765, 0.64120353, 0.65870224, 0.67534753, 0.69118102,  0.70624230, 0.72056903, 0.73419704, 0.74716040, 0.75949154, 0.77122127, 0.78237894, 0.79299245, 0.80308832, 0.81269182, 0.82182695,0.83051655, 0.83878236, 0.84664503, 0.85412424, 0.86123869, 0.86800616, 0.87444357,
     0.88056703, 0.88639185, 0.89193258, 0.89720309, 0.90221656, 0.90698551, 0.91152188,0.91583701, 0.91994169, 0.92384618, 0.92756024, 0.93109317, 0.93445380, 0.93765052,0.94069134, 0.94358386, 0.94633531, 0.94895257, 0.99840417, 0.99842013, 0.99843531,0.99844975, 0.99846348, 0.99847654 ,0.99848897 ,0.99850079, 0.99851204, 0.99852273, 0.99853291,1)

#Calculate reference value to check result - ES is the average of the worst X returns.
print(paste('reference value for 87.5% :' , mean(Fx[1:9])))

#Standardizing the data to zero mean
m = mean(Fx)
Fx_s = Fx-m

#Calculating ES
ES = ETL(Fx_s,p=0.875,method = "historical") + m # the old mean has to be added to the result

print(paste('calculated ES for 87.5%: ', ES)