<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>CVaR on 格物堂</title><link>https://gewutang.cn/tags/cvar/</link><description>Recent content in CVaR on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Sat, 13 Jul 2019 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/cvar/index.xml" rel="self" type="application/rss+xml"/><item><title>CVaR 和 ES 补充 VaR 的风险度量方法解析（含 R 包应用）</title><link>https://gewutang.cn/2019/07/13/cvar-es/</link><pubDate>Sat, 13 Jul 2019 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2019/07/13/cvar-es/</guid><description>&#10;&#10;&#10;&lt;div id="理论基础概述" class="section level1"&gt;&#10;&lt;h1&gt;理论基础概述&lt;/h1&gt;&#10;&lt;p&gt;在金融风险管理领域，风险价值（VaR）是一种广泛使用的风险度量工具，它衡量在一定置信水平下，某一金融资产或投资组合在未来特定时期内可能遭受的最大损失。然而，VaR 存在一些局限性，主要体现在以下几个方面：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;缺乏次可加性：在某些情况下，投资组合的 VaR 可能大于其各组成部分 VaR 之和，这与分散投资降低风险的直觉相悖。&lt;/li&gt;&#10;&lt;li&gt;对尾部风险的刻画不足：VaR 只关注损失分布的分位数点，而不考虑超过该点的损失程度。&lt;/li&gt;&#10;&lt;li&gt;不满足一致性风险度量的要求：VaR 不满足一致性风险度量公理中的次可加性、正齐次性、单调性和平移不变性。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;为了克服 VaR 的这些局限性，条件风险价值（CVaR）和预期短缺（ES）作为更高级的风险度量方法被提出。CVaR 与 ES 的基本概念条件风险价值（Conditional Value at Risk, CVaR），也称为平均短缺（Average Shortfall, AS）或预期短缺（Expected Shortfall, ES），是指在给定置信水平下，超过 VaR 的损失的期望值。&lt;/p&gt;&#10;&lt;p&gt;它不仅考虑了损失超过 VaR 的概率，还考虑了超过部分的大小，因此能更全面地反映尾部风险。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="数学公式解析" class="section level1"&gt;&#10;&lt;h1&gt;数学公式解析&lt;/h1&gt;&#10;&lt;div id="var-的数学定义" class="section level2"&gt;&#10;&lt;h2&gt;VaR 的数学定义&lt;/h2&gt;&#10;&lt;p&gt;对于给定的投资组合收益率 R 和置信水平 &lt;span class="math inline"&gt;\(\alpha\)&lt;/span&gt;，VaR 可以定义为：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;\text{VaR}_\alpha = -\inf \{ x \in \mathbb{R} : F_R(x) \geq \alpha \}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其中，&lt;span class="math inline"&gt;\(F_R(x)\)&lt;/span&gt; 是收益率 &lt;span class="math inline"&gt;\(R\)&lt;/span&gt; 的累积分布函数。&lt;/p&gt;&#10;&lt;p&gt;在实际应用中，VaR 通常表示为：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;P(R \leq -\text{VaR}_\alpha) = 1 - \alpha&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="cvares-的数学定义" class="section level2"&gt;&#10;&lt;h2&gt;CVaR/ES 的数学定义&lt;/h2&gt;&#10;&lt;p&gt;CVaR/ES 在置信水平 &lt;span class="math inline"&gt;\(\alpha\)&lt;/span&gt; 下的定义为：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;\text{CVaR}_\alpha = \text{ES}_\alpha = \mathbb{E}[-R | R \leq -\text{VaR}_\alpha]&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;当收益率分布连续时，CVaR/ES 可以表示为：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;\text{ES}_\alpha = \frac{1}{1-\alpha} \int_{0}^{1-\alpha} \text{VaR}_p \, dp&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;这个公式表明，ES 是所有低于 &lt;span class="math inline"&gt;\(\alpha\)&lt;/span&gt; 置信水平的 VaR 的平均值。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="r-语言实现示例" class="section level1"&gt;&#10;&lt;h1&gt;R 语言实现示例&lt;/h1&gt;&#10;&lt;p&gt;下面通过 R 语言实现基于纳斯达克数据的 VaR、CVaR 和 ES 计算，使用 quantmod 包获取数据，并对比不同 R 包的计算方法。&lt;/p&gt;&#10;&lt;div id="使用-quantmod-获取数据" class="section level2"&gt;&#10;&lt;h2&gt;使用 quantmod 获取数据&lt;/h2&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载必要的包&#10;library(quantmod)&#10;library(PerformanceAnalytics)&#10;library(xts)&#10;library(tibble)&#10;library(dplyr)&#10;library(ggplot2)&#10;library(showtext) # 用于设置中文字体&#10;&#10;# 设置中文字体&#10;font_add(&amp;quot;SimHei&amp;quot;, &amp;quot;SimHei.ttf&amp;quot;) # 如果字体已安装&#10;# 如果字体未安装，可以尝试系统字体路径&#10;# font_add(&amp;quot;SimHei&amp;quot;, &amp;quot;/path/to/SimHei.ttf&amp;quot;) # Linux/Mac&#10;# font_add(&amp;quot;SimHei&amp;quot;, &amp;quot;C:/Windows/Fonts/simhei.ttf&amp;quot;) # Windows&#10;&#10;showtext_auto() # 自动启用showtext&#10;&#10;# 获取纳斯达克综合指数数据&#10;getSymbols(&amp;quot;^IXIC&amp;quot;, from = &amp;quot;2020-01-01&amp;quot;, to = Sys.Date())&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;IXIC&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 计算日对数收益率&#10;returns &amp;lt;- diff(log(Cl(IXIC)))&#10;returns &amp;lt;- na.omit(returns)&#10;&#10;# 设置置信水平&#10;alpha &amp;lt;- 0.95&#10;risk_results &amp;lt;- list()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="使用不同-r-包计算-escvar" class="section level2"&gt;&#10;&lt;h2&gt;使用不同 R 包计算 ES/CVaR&lt;/h2&gt;&#10;&lt;div id="performanceanalytics-包" class="section level3"&gt;&#10;&lt;h3&gt;PerformanceAnalytics 包&lt;/h3&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 使用PerformanceAnalytics包计算ES&#10;&#10;VaR_historical &amp;lt;- PerformanceAnalytics::VaR(returns, p = alpha , method = &amp;quot;historical&amp;quot;)&#10;VaR_gaussian &amp;lt;- PerformanceAnalytics::VaR(returns, p = alpha , method = &amp;quot;gaussian&amp;quot;)&#10;es_historical &amp;lt;- PerformanceAnalytics::ES(returns, p = alpha, method = &amp;quot;historical&amp;quot;)&#10;es_gaussian &amp;lt;- PerformanceAnalytics::ES(returns, p = alpha, method = &amp;quot;gaussian&amp;quot;)&#10;&#10;cat(&amp;quot;PerformanceAnalytics包计算结果:\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## PerformanceAnalytics包计算结果:&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;历史模拟法 VaR:&amp;quot;, round(VaR_historical * 100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 历史模拟法 VaR: -2.58 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;正态分布假设 VaR:&amp;quot;, round(VaR_gaussian * 100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 正态分布假设 VaR: -2.64 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;历史模拟法 ES:&amp;quot;, round(es_historical * 100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 历史模拟法 ES: -3.91 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;正态分布假设 ES:&amp;quot;, round(es_gaussian * 100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 正态分布假设 ES: -3.32 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;risk_results[[&amp;quot;performance&amp;quot;]] &amp;lt;- list(&#10; VaR_historical = VaR_historical,&#10; VaR_gaussian = VaR_gaussian,&#10; es_historical = es_historical,&#10; es_gaussian = es_gaussian&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="fgarch-包" class="section level3"&gt;&#10;&lt;h3&gt;fGarch 包&lt;/h3&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 使用fGarch包计算ES&#10;library(fGarch)&#10;# 拟合GARCH模型并预测&#10;garch_model &amp;lt;- garchFit(~garch(1,1), data = returns, trace = FALSE)&#10;forecast &amp;lt;- predict(garch_model, n.ahead = 1)&#10;es_garch &amp;lt;- tail(fGarch::ES(garch_model), n = 1)&#10;var_garch &amp;lt;- tail(fGarch::VaR(garch_model), n = 1)&#10;cat(&amp;quot;\nfGarch包计算结果:\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## fGarch包计算结果:&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;GARCH模型预测 VaR:&amp;quot;, round(var_garch * 100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## GARCH模型预测 VaR: 1.47 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;GARCH模型预测 ES:&amp;quot;, round(es_garch * 100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## GARCH模型预测 ES: 1.86 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;risk_results[[&amp;quot;fgarch&amp;quot;]] &amp;lt;- list(&#10; var_garch = var_garch,&#10; es_garch = es_garch&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="cvar包针对连续分布的vares计算" class="section level3"&gt;&#10;&lt;h3&gt;cvar包（针对连续分布的VaR/ES计算）&lt;/h3&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;if (!require(&amp;quot;cvar&amp;quot;)) install.packages(&amp;quot;cvar&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: cvar&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;cvar&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:PerformanceAnalytics&amp;#39;:&#10;## &#10;## ES, VaR&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(cvar)&#10;&#10;# 标准正态分布计算&#10;gaussian_es &amp;lt;- list(&#10; qf = cvar::ES(qnorm, dist.type = &amp;quot;qf&amp;quot;), # 使用分位数函数&#10; cdf = cvar::ES(pnorm, dist.type = &amp;quot;cdf&amp;quot;) # 使用累积分布函数&#10;)&#10;&#10;# t分布计算（df=4）&#10;t_dist_es &amp;lt;- list(&#10; qf = cvar::ES(qt, dist.type = &amp;quot;qf&amp;quot;, df = 4),&#10; cdf = cvar::ES(pt, dist.type = &amp;quot;cdf&amp;quot;, df = 4)&#10;)&#10;&#10;risk_results[[&amp;quot;cvar&amp;quot;]] &amp;lt;- list(&#10; gaussian_es = gaussian_es,&#10; t_dist_es = t_dist_es&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;ol start="5" style="list-style-type: decimal"&gt;&#10;&lt;li&gt;rugarch 包&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载必要的包&#10;library(rugarch)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: parallel&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(xts) # 处理时间序列数据&#10;&#10;# 定义GARCH模型（使用偏态t分布）&#10;spec &amp;lt;- ugarchspec(&#10; variance.model = list(model = &amp;quot;sGARCH&amp;quot;, garchOrder = c(1, 1)),&#10; mean.model = list(armaOrder = c(0, 0), include.mean = TRUE),&#10; distribution.model = &amp;quot;sstd&amp;quot; # 偏态t分布&#10;)&#10;&#10;# 拟合模型&#10;fit &amp;lt;- ugarchfit(spec, data = returns, solver = &amp;quot;hybrid&amp;quot;)&#10;&#10;# 使用固定参数重新过滤数据&#10;spec2 &amp;lt;- spec&#10;setfixed(spec2) &amp;lt;- as.list(coef(fit))&#10;filt &amp;lt;- ugarchfilter(spec2, data = returns, n.old = 1000)&#10;&#10;# 提取拟合值和波动率&#10;mu &amp;lt;- fitted(filt) # 条件均值&#10;sigma_t &amp;lt;- sigma(filt) # 条件波动率&#10;&#10;# 获取分布参数&#10;skew &amp;lt;- coef(fit)[&amp;quot;skew&amp;quot;]&#10;shape &amp;lt;- coef(fit)[&amp;quot;shape&amp;quot;]&#10;&#10;# 计算VaR (使用向量化方法)&#10;p &amp;lt;- 0.05 # 置信水平(1-95%)&#10;VaR &amp;lt;- mu + sigma_t * qdist(&amp;quot;sstd&amp;quot;, p = p, mu = 0, sigma = 1, skew = skew, shape = shape)&#10;&#10;# 计算ES (使用数值积分)&#10;es_integrand &amp;lt;- function(x) {&#10; qdist(&amp;quot;sstd&amp;quot;, p = x, mu = 0, sigma = 1, skew = skew, shape = shape)&#10;}&#10;&#10;# 向量化积分计算（对每个时间点单独计算ES）&#10;ES &amp;lt;- numeric(length(returns))&#10;for (i in 1:length(returns)) {&#10; int_result &amp;lt;- integrate(es_integrand, lower = 0, upper = p, subdivisions = 1000)&#10; ES[i] &amp;lt;- mu[i] + sigma_t[i] * int_result$value / p&#10;}&#10;&#10;# 确保所有向量长度一致（处理可能的长度差异）&#10;min_len &amp;lt;- min(length(returns), length(VaR), length(ES))&#10;actual &amp;lt;- as.numeric(tail(returns, min_len))&#10;VaR &amp;lt;- tail(as.numeric(tail(VaR, min_len)),1)&#10;ES &amp;lt;- tail(as.numeric(tail(ES, min_len)),1)&#10;&#10;risk_results[[&amp;quot;rugarch&amp;quot;]] &amp;lt;- list(&#10; VaR = VaR,&#10; ES = ES&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;ol start="4" style="list-style-type: decimal"&gt;&#10;&lt;li&gt;gets包（基于ARX模型的条件风险度量）&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载必要的包&#10;library(gets)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;gets&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:cvar&amp;#39;:&#10;## &#10;## ES, VaR&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:fGarch&amp;#39;:&#10;## &#10;## ES, VaR&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:PerformanceAnalytics&amp;#39;:&#10;## &#10;## ES, VaR&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;returns &amp;lt;- diff(log(Cl(IXIC))) &#10;returns &amp;lt;- as.vector(na.omit(returns)) # 强制转换为普通向量&#10;&#10;# 设置置信水平&#10;alpha &amp;lt;- 0.95&#10;&#10;# 构建ARX模型（滞后1阶）&#10;# 使用普通向量而非xts对象&#10;arx_model &amp;lt;- arx(returns, mc=TRUE, ar=1) # 直接使用向量格式&#10;&#10;# 计算VaR和ES&#10;risk_results[[&amp;quot;gets&amp;quot;]] &amp;lt;- list(&#10; VaR = quantile(arx_model$residuals, probs=1-alpha),&#10; ES = mean(arx_model$residuals[arx_model$residuals &amp;lt;= quantile(arx_model$residuals, probs=1-alpha)])&#10;)&#10;&#10;# 打印结果&#10;cat(&amp;quot;ARX模型风险度量结果:\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ARX模型风险度量结果:&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;VaR (&amp;quot;, alpha*100, &amp;quot;%):&amp;quot;, round(risk_results[[&amp;quot;gets&amp;quot;]]$VaR*100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## VaR ( 95 %): -2.64 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;ES (&amp;quot;, alpha*100, &amp;quot;%):&amp;quot;, round(risk_results[[&amp;quot;gets&amp;quot;]]$ES*100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ES ( 95 %): -3.93 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;ol start="5" style="list-style-type: decimal"&gt;&#10;&lt;li&gt;MSGARCH包（贝叶斯方法的风险预测）&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;if (!require(&amp;quot;MSGARCH&amp;quot;)) install.packages(&amp;quot;MSGARCH&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: MSGARCH&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(MSGARCH)&#10;spec_msgarch &amp;lt;- CreateSpec()&#10;fit_msgarch &amp;lt;- FitML(spec_msgarch, data = returns)&#10;risk_msgarch &amp;lt;- Risk(fit_msgarch, n.ahead = 1, level = alpha)&#10;risk_results[[&amp;quot;MSGARCH&amp;quot;]] &amp;lt;- list(&#10; VaR = as.numeric(risk_msgarch$VaR),&#10; ES = as.numeric(risk_msgarch$ES)&#10;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;ol start="6" style="list-style-type: decimal"&gt;&#10;&lt;li&gt;VaRES包（多种参数分布的风险度量）&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;if (!require(&amp;quot;VaRES&amp;quot;)) install.packages(&amp;quot;VaRES&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: VaRES&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(VaRES)&#10;&#10;returns &amp;lt;- diff(log(Cl(IXIC))) &#10;returns &amp;lt;- as.vector(na.omit(returns)) # 强制转换为普通向量&#10;&#10;# 手动计算 VaR 和 ES（假设 df = 4）&#10;df &amp;lt;- 4&#10;alpha &amp;lt;- 0.05 # 95% 置信水平&#10;&#10;# 计算 t 分布的 VaR&#10;VaR_t &amp;lt;- -quantile(rt(n = 1e6, df = df), probs = alpha) * sd(returns)&#10;&#10;# 计算 t 分布的 ES（解析解）&#10;ES_t &amp;lt;- (dt(qt(alpha, df = df), df = df) / alpha) * &#10; ((df + qt(alpha, df = df)^2) / (df - 2)) * &#10; sd(returns)&#10;&#10;risk_results[[&amp;quot;VaRES&amp;quot;]] &amp;lt;- list(VaR = VaR_t, ES = ES_t)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="结果对比与可视化" class="section level1"&gt;&#10;&lt;h1&gt;结果对比与可视化&lt;/h1&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 从risk_results中提取各方法的VaR和ES（统一格式：保留数值，处理符号）&#10;returns_vec &amp;lt;- as.vector(returns) # 转换为向量用于部分模型&#10;&#10;results &amp;lt;- tibble(&#10; 方法 = c(&#10; &amp;quot;历史模拟法&amp;quot;, &#10; &amp;quot;正态分布假设&amp;quot;, &#10; &amp;quot;GARCH(fGarch)&amp;quot;, &#10; &amp;quot;GARCH(rugarch)&amp;quot;, &#10; &amp;quot;MSGARCH&amp;quot;, &#10; &amp;quot;ARX模型(gets)&amp;quot;, &#10; &amp;quot;t分布(VaRES)&amp;quot;&#10; ),&#10; # 提取VaR（统一为损失幅度，取绝对值或修正符号）&#10; VaR = c(&#10; as.numeric(risk_results$performance$VaR_historical) %&amp;gt;% abs(), # 历史模拟法&#10; as.numeric(risk_results$performance$VaR_gaussian) %&amp;gt;% abs(), # 正态分布&#10; as.numeric(risk_results$fgarch$var_garch), # fGarch&#10; as.numeric(risk_results$rugarch$VaR) %&amp;gt;% abs(), # rugarch&#10; as.numeric(risk_results$MSGARCH$VaR[1]) %&amp;gt;% abs(), # MSGARCH（取第一个结果）&#10; as.numeric(risk_results$gets$VaR) %&amp;gt;% abs(), # ARX模型&#10; as.numeric(risk_results$VaRES$VaR) # VaRES包&#10; ),&#10; # 提取ES（统一为损失幅度，取绝对值或修正符号）&#10; ES = c(&#10; as.numeric(risk_results$performance$es_historical) %&amp;gt;% abs(), # 历史模拟法&#10; as.numeric(risk_results$performance$es_gaussian) %&amp;gt;% abs(), # 正态分布&#10; as.numeric(risk_results$fgarch$es_garch), # fGarch&#10; as.numeric(risk_results$rugarch$ES) %&amp;gt;% abs(), # rugarch&#10; as.numeric(risk_results$MSGARCH$ES[1]) %&amp;gt;% abs(), # MSGARCH（取第一个结果）&#10; as.numeric(risk_results$gets$ES) %&amp;gt;% abs(), # ARX模型&#10; as.numeric(risk_results$VaRES$ES) # VaRES包&#10; )&#10;)&#10;&#10;# 转换为百分比（乘以100）&#10;results &amp;lt;- results %&amp;gt;% &#10; mutate(&#10; VaR_pct = VaR * 100,&#10; ES_pct = ES * 100&#10; )&#10;&#10;&#10;&#10;# 不同方法的ES对比图&#10;ggplot(results, aes(x = 方法, y = ES_pct, fill = 方法)) +&#10; geom_col(width = 0.7, show.legend = FALSE) + # 柱状图，不显示图例&#10; geom_text(&#10; aes(label = sprintf(&amp;quot;%.2f%%&amp;quot;, ES_pct)), # 显示百分比标签&#10; vjust = -0.5, size = 4, family = &amp;quot;SimHei&amp;quot;&#10; ) +&#10; labs(&#10; title = &amp;quot;不同方法计算的预期损失(ES)对比&amp;quot;,&#10; x = &amp;quot;风险度量方法&amp;quot;,&#10; y = &amp;quot;ES（%）&amp;quot;,&#10; caption = &amp;quot;数据来源：纳斯达克指数(2020-2025)日对数收益率&amp;quot;&#10; ) +&#10; ylim(0, max(results$ES_pct) * 1.2) + # 调整y轴范围，避免标签超出&#10; theme_minimal(base_family = &amp;quot;SimHei&amp;quot;) +&#10; theme(&#10; plot.title = element_text(hjust = 0.5, face = &amp;quot;bold&amp;quot;, size = 14),&#10; axis.text.x = element_text(angle = 45, hjust = 1, size = 10),&#10; plot.caption = element_text(hjust = 0, size = 8, color = &amp;quot;gray50&amp;quot;)&#10; )&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2019/07/13/cvar-es/index_files/figure-html/unnamed-chunk-9-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 不同方法的VaR对比图&#10;ggplot(results, aes(x = 方法, y = VaR_pct, fill = 方法)) +&#10; geom_col(width = 0.7, show.legend = FALSE) +&#10; geom_text(&#10; aes(label = sprintf(&amp;quot;%.2f%%&amp;quot;, VaR_pct)),&#10; vjust = -0.5, size = 4, family = &amp;quot;SimHei&amp;quot;&#10; ) +&#10; labs(&#10; title = &amp;quot;不同方法计算的风险价值(VaR)对比&amp;quot;,&#10; x = &amp;quot;风险度量方法&amp;quot;,&#10; y = &amp;quot;VaR（%）&amp;quot;,&#10; caption = &amp;quot;数据来源：纳斯达克指数(2020-2025)日对数收益率&amp;quot;&#10; ) +&#10; ylim(0, max(results$VaR_pct) * 1.2) +&#10; theme_minimal(base_family = &amp;quot;SimHei&amp;quot;) +&#10; theme(&#10; plot.title = element_text(hjust = 0.5, face = &amp;quot;bold&amp;quot;, size = 14),&#10; axis.text.x = element_text(angle = 45, hjust = 1, size = 10),&#10; plot.caption = element_text(hjust = 0, size = 8, color = &amp;quot;gray50&amp;quot;)&#10; )&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2019/07/13/cvar-es/index_files/figure-html/unnamed-chunk-9-2.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 收益率分布与ES标记&#10;# 绘制收益率直方图，并标记历史模拟法ES&#10;p &amp;lt;- ggplot(data = tibble(returns = returns_vec), aes(x = returns)) +&#10; geom_histogram(&#10; aes(y = after_stat(density)), # 密度直方图&#10; bins = 50, fill = &amp;quot;lightblue&amp;quot;, color = &amp;quot;white&amp;quot;&#10; ) +&#10; # 添加核密度曲线&#10; geom_density(color = &amp;quot;darkblue&amp;quot;, linewidth = 0.8) +&#10; # 标记历史模拟法ES（取绝对值对应的收益率位置）&#10; geom_vline(&#10; xintercept = -results$ES[results$方法 == &amp;quot;历史模拟法&amp;quot;] / 100, # 转换为收益率（负值表示损失）&#10; color = &amp;quot;red&amp;quot;, linewidth = 1.2, linetype = &amp;quot;dashed&amp;quot;&#10; ) +&#10; annotate(&#10; &amp;quot;text&amp;quot;, x = -results$ES[results$方法 == &amp;quot;历史模拟法&amp;quot;] / 100, y = 5,&#10; label = sprintf(&amp;quot;历史模拟法ES: %.2f%%&amp;quot;, results$ES[results$方法 == &amp;quot;历史模拟法&amp;quot;]),&#10; color = &amp;quot;red&amp;quot;, hjust = 1.1, family = &amp;quot;SimHei&amp;quot;&#10; ) +&#10; labs(&#10; title = &amp;quot;纳斯达克指数日对数收益率分布&amp;quot;,&#10; x = &amp;quot;日对数收益率&amp;quot;,&#10; y = &amp;quot;密度&amp;quot;&#10; ) +&#10; theme_minimal(base_family = &amp;quot;SimHei&amp;quot;) +&#10; theme(&#10; plot.title = element_text(hjust = 0.5, face = &amp;quot;bold&amp;quot;)&#10; )&#10;print(p)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2019/07/13/cvar-es/index_files/figure-html/unnamed-chunk-9-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# ----------------------&#10;# 输出数值结果&#10;# ----------------------&#10;cat(&amp;quot;各方法风险度量结果（百分比）：\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 各方法风险度量结果（百分比）：&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;results %&amp;gt;% &#10; select(方法, VaR_pct, ES_pct) %&amp;gt;% &#10; rename(&#10; &amp;quot;风险度量方法&amp;quot; = 方法,&#10; &amp;quot;VaR（%）&amp;quot; = VaR_pct,&#10; &amp;quot;ES（%）&amp;quot; = ES_pct&#10; ) %&amp;gt;% &#10; print(n = Inf)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## # A tibble: 7 × 3&#10;## 风险度量方法 `VaR（%）` `ES（%）`&#10;## &amp;lt;chr&amp;gt; &amp;lt;dbl&amp;gt; &amp;lt;dbl&amp;gt;&#10;## 1 历史模拟法 2.58 3.91&#10;## 2 正态分布假设 2.64 3.32&#10;## 3 GARCH(fGarch) 1.47 1.86&#10;## 4 GARCH(rugarch) 1.48 2.07&#10;## 5 MSGARCH 2.25 2.73&#10;## 6 ARX模型(gets) 2.64 3.93&#10;## 7 t分布(VaRES) 3.47 7.87&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;结论CVaR 和 ES 作为 VaR 的补充工具，能够更全面地度量金融风险，尤其是在处理尾部风险时表现更为出色。通过结合使用 VaR、CVaR 和 ES，风险管理者可以获得更完整的风险图景，从而做出更明智的风险管理决策。在实际应用中，应根据投资组合的特性和市场环境选择合适的风险度量方法。&lt;/p&gt;&#10;&lt;p&gt;不同 R 包提供了多种计算 ES/CVaR 的方法，各有特点：PerformanceAnalytics：简单易用，支持多种分布假设RiskPortfolios：专注投资组合风险分析fGarch/rugarch：适合结合 GARCH 模型处理时变波动率ExpectedShortfall：提供 ES 回测功能建议结合数据特性（如是否存在厚尾、波动聚类等）选择合适的模型，并对比不同方法的结果以确保对极端风险的充分考虑。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item></channel></rss>