<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>风险 on 格物堂</title><link>https://gewutang.cn/tags/%E9%A3%8E%E9%99%A9/</link><description>Recent content in 风险 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Mon, 27 Jun 2011 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E9%A3%8E%E9%99%A9/index.xml" rel="self" type="application/rss+xml"/><item><title>新书：Financial Risk Forecasting</title><link>https://gewutang.cn/2011/06/27/financial-risk-forcasting/</link><pubDate>Mon, 27 Jun 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/06/27/financial-risk-forcasting/</guid><description>&lt;p&gt;Jon Danielsson撰写的一本金融风险预测的书籍。书的全称是：Financial Risk Forecasting: The Theory and Practice of Forecasting Market Risk with Implementation in R and Matlab (The Wiley Finance Series)。&lt;/p&gt;&#10;&lt;p&gt;我目前还没有细看这本书，但是，依然推荐一下，推荐的理由在于作者为这本书附上了全部的R和matlab代码。详情见&lt;a class="link" href="http://www.financialriskforecasting.com/" target="_blank" rel="noopener"&#10; &gt;这里&lt;/a&gt;。&lt;/p&gt;&#10;</description></item><item><title>基于极值理论的 VaR 计算及实证分析</title><link>https://gewutang.cn/2010/12/29/var-caculation-and-evt/</link><pubDate>Wed, 29 Dec 2010 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2010/12/29/var-caculation-and-evt/</guid><description>&#10;&#10;&#10;&lt;div id="一引言" class="section level1"&gt;&#10;&lt;h1&gt;一、引言&lt;/h1&gt;&#10;&lt;p&gt;风险价值（VaR）作为衡量金融资产极端损失的核心指标，传统方法（如 GARCH 模型）虽能捕捉波动聚类特性，但对尾部风险的刻画仍依赖于整体分布假设。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;极值理论（Extreme Value Theory, EVT）&lt;/strong&gt; 则直接聚焦于收益率序列的尾部行为，通过对极端值建模突破整体分布限制，更精准地估计极端市场条件下的潜在损失。&lt;/p&gt;&#10;&lt;p&gt;本文以特斯拉（TSLA）股票收益率为例，系统介绍极值理论的核心模型（广义极值分布 GEV、广义帕累托分布 GPD）、参数估计及 VaR 计算流程，并通过 R 语言&lt;code&gt;fExtremes&lt;/code&gt;和&lt;code&gt;evd&lt;/code&gt;包实现实证分析。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="二极值理论核心模型" class="section level1"&gt;&#10;&lt;h1&gt;二、极值理论核心模型&lt;/h1&gt;&#10;&lt;p&gt;极值理论通过两类方法刻画极端值分布：&lt;strong&gt;分块最大值法（Block Maxima Method, BMM）&lt;/strong&gt; 基于广义极值分布（GEV），&lt;strong&gt;超限峰值法（Peaks Over Threshold, POT）&lt;/strong&gt; 基于广义帕累托分布（GPD）。其中，POT 因灵活利用数据尾部信息而更常用于 VaR 计算。&lt;/p&gt;&#10;&lt;ol style="list-style-type: decimal"&gt;&#10;&lt;li&gt;广义极值分布（GEV）&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;strong&gt;分块最大值法&lt;/strong&gt;将时间序列划分为若干块（如按日、周），取每块内的极端值（最大值或最小值），其渐进分布可统一为 GEV。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;GEV 分布函数&lt;/strong&gt;：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;G(x; \mu, \sigma, \xi) = \exp\left\{ -\left[1 + \xi \left( \frac{x - \mu}{\sigma} \right) \right]^{-1/\xi} \right\} \tag{1}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;span class="math inline"&gt;\(\mu\)&lt;/span&gt; 为位置参数（中心趋势），决定了分布的中心位置，反映极端值的平均水平；&lt;/li&gt;&#10;&lt;li&gt;&lt;span class="math inline"&gt;\(\sigma &amp;gt; 0\)&lt;/span&gt; 为尺度参数（离散程度）；&lt;/li&gt;&#10;&lt;li&gt;&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 为形状参数（决定尾部特性）：&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;若 &lt;span class="math inline"&gt;\(\xi &amp;gt; 0\)&lt;/span&gt; ，则 GEV 为 Frechet 分布（厚尾，如幂律尾）。Frechet 分布具有显著的厚尾特征，概率密度函数呈渐近幂律形式，尾部衰减速度较慢，能更准确捕捉金融市场中极端事件发生概率较高的尾部风险，在 VaR 计算中可更可靠地拟合金融资产收益率极端值。&lt;/p&gt;&#10;&lt;p&gt;若 &lt;span class="math inline"&gt;\(\xi = 0\)&lt;/span&gt; ，则 GEV 为 Gumbel 分布（轻尾，如正态分布）。该分布下极端事件发生概率随偏离均值程度呈指数衰减，数据在均值附近集中程度高，极端值出现频率低，若资产收益符合此分布，表明极端损失发生可能性较小，风险相对可控。&lt;/p&gt;&#10;&lt;p&gt;若 &lt;span class="math inline"&gt;\(\xi &amp;lt; 0\)&lt;/span&gt; ，则 GEV 为 Weibull 分布（有界尾）。该分布下随机变量存在理论上界，极端事件影响有限，在金融市场中若资产价格波动符合此分布，意味着极端损失幅度有上限，对风险管理者设定止损策略有重要参考价值。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;基于 GEV 的 VaR 计算&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;广义极值分布（GEV）作为统一框架，整合了 Gumbel、Weibull 和 Frechet 三种不同尾部特征的分布类型。基于 GEV 的 VaR 计算，通过极大似然估计或矩估计等方法确定形状参数 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 、位置参数 &lt;span class="math inline"&gt;\(\mu\)&lt;/span&gt; 和尺度参数 &lt;span class="math inline"&gt;\(\sigma\)&lt;/span&gt; ，构建符合数据尾部特征的风险分布模型。&lt;/p&gt;&#10;&lt;p&gt;在给定置信水平 &lt;span class="math inline"&gt;\(1-\alpha\)&lt;/span&gt; 下，VaR 为 GEV 分布的 &lt;span class="math inline"&gt;\(\alpha\)&lt;/span&gt; 分位数：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;VaR_{1-\alpha} = \mu - \frac{\sigma}{\xi} \left[ 1 - (-\ln \alpha)^{-\xi} \right] \quad (\xi \neq 0) \tag{2}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;ol start="2" style="list-style-type: decimal"&gt;&#10;&lt;li&gt;广义帕累托分布（GPD）与 POT 方法&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;&lt;strong&gt;POT 方法&lt;/strong&gt;通过设定阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt;，对所有超过 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 的极端值（超限值）进行建模，其超出量（&lt;span class="math inline"&gt;\(x - u\)&lt;/span&gt;）的分布可由广义帕累托分布（Generalized Pareto Distribution，GPD）逼近。&lt;/p&gt;&#10;&lt;p&gt;这种方法聚焦于极端事件，能更精准刻画金融市场尾部风险，在金融市场剧烈波动时，可有效捕捉资产价格极端变化，为风险管理者提供重要预警信号，在高风险投资领域应用价值显著。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;GPD 分布函数&lt;/strong&gt;：&lt;/p&gt;&#10;&lt;p&gt;广义帕累托分布是刻画超过某一阈值数据分布的重要工具，在金融风险度量中常用于描述极端损失事件概率分布，其概率密度函数为&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;f(x;\xi,\beta)=\frac{1}{\beta}(1 + \frac{\xi x}{\beta})^{-\frac{1}{\xi}-1}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其中 &lt;span class="math inline"&gt;\(x \geq 0\)&lt;/span&gt;，&lt;span class="math inline"&gt;\(\beta &amp;gt; 0\)&lt;/span&gt; 是尺度参数，&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 为形状参数，当 &lt;span class="math inline"&gt;\(\xi = 0\)&lt;/span&gt; 时退化为指数分布。&lt;/p&gt;&#10;&lt;p&gt;GPD 分布函数为：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;F(x;\xi,\beta,\mu)=1-\left(1+\frac{\xi(x - \mu)}{\beta}\right)^{-\frac{1}{\xi}}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;span class="math inline"&gt;\(x\geq\mu，1+\frac{\xi(x - \mu)}{\beta}&amp;gt;0\)&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其分布函数也可表示为针对超出量的形式：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;\begin{cases}&#10;1 - \left(1 + \xi \frac{y}{\sigma}\right)^{-1/\xi} &amp;amp; \xi \neq 0 \\&#10;1 - \exp\left(-\frac{y}{\sigma}\right) &amp;amp; \xi = 0&#10;\end{cases} \tag{3}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;其中：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;span class="math inline"&gt;\(y = x - u \geq 0\)&lt;/span&gt; 为超出量，代表观测值超过阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 的部分，反映极端事件偏离阈值的程度。例如，若阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 设为特斯拉股价单日跌幅 5%，某交易日股价下跌 8% 时，超出量 &lt;span class="math inline"&gt;\(y\)&lt;/span&gt; 为 3%。&lt;/p&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;span class="math inline"&gt;\(\sigma &amp;gt; 0\)&lt;/span&gt; 为尺度参数，决定超出量分布的离散程度。&lt;span class="math inline"&gt;\(\sigma\)&lt;/span&gt; 值越大，超出量波动范围越广，投资组合面临的不确定性越高，且其变化与市场宏观环境相关，经济危机期间通常会显著跃升。&lt;/p&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 为形状参数（与 GEV 中的 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 含义相同），&lt;span class="math inline"&gt;\(\xi &amp;gt; 0\)&lt;/span&gt; 表示厚尾，直接影响分布尾部厚度与极端事件发生概率估计。&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 趋近于 0 时，尾部变薄，极端事件概率降低；&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 增大时，极端事件发生可能性显著提升，且在公司重大事件节点往往会出现明显波动。&lt;/p&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;strong&gt;基于 GPD 的 VaR 计算&lt;/strong&gt;：&lt;/p&gt;&#10;&lt;p&gt;设总样本量为 &lt;span class="math inline"&gt;\(n\)&lt;/span&gt;，超限值数量为 &lt;span class="math inline"&gt;\(N_u\)&lt;/span&gt;（超限概率 &lt;span class="math inline"&gt;\(\hat{p} = N_u / n\)&lt;/span&gt;），则置信水平 &lt;span class="math inline"&gt;\(1-\alpha\)&lt;/span&gt; 的 VaR 满足：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;1 - \alpha = (1 - \hat{p}) + \hat{p} \cdot \left[1 - \left(1 + \xi \frac{VaR_{1-\alpha} - u}{\sigma}\right)^{-1/\xi}\right] \tag{4}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;该公式基于全概率公式推导，将整体风险概率分解为阈值以下和阈值以上两部分。等式左边 &lt;span class="math inline"&gt;\(1 - \alpha\)&lt;/span&gt; 为置信水平，右边 &lt;span class="math inline"&gt;\((1 - \hat{p})\)&lt;/span&gt; 表示未超过阈值的概率，第二项刻画了阈值以上部分在 GPD 分布下对风险的贡献。如在 95% 置信水平（&lt;span class="math inline"&gt;\(\alpha=0.05\)&lt;/span&gt;）下，可据此计算特斯拉股票在极端市场条件下的最大潜在损失。&lt;/p&gt;&#10;&lt;p&gt;经代数变换可得：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;VaR_{1-\alpha} = u + \frac{\sigma}{\xi} \left[ \left( \frac{n}{N_u} (1 - \alpha) \right)^{-\xi} - 1 \right] \quad (\xi \neq 0) \tag{5}&#10;\]&lt;/span&gt;&lt;/p&gt;&#10;&lt;p&gt;在实际应用中，以特斯拉股价实证分析为例，若总样本量 &lt;span class="math inline"&gt;\(n=1000\)&lt;/span&gt; ，超限值数量 &lt;span class="math inline"&gt;\(N_u=50\)&lt;/span&gt;，阈值 &lt;span class="math inline"&gt;\(u = 0.03\)&lt;/span&gt;，尺度参数 &lt;span class="math inline"&gt;\(\sigma = 0.02\)&lt;/span&gt;，形状参数 &lt;span class="math inline"&gt;\(\xi = 0.1\)&lt;/span&gt;，在 99% 置信水平下（ &lt;span class="math inline"&gt;\(\alpha = 0.01\)&lt;/span&gt; ），代入公式 (5) 计算可得 &lt;span class="math inline"&gt;\(VaR_{0.99} \approx 0.068\)&lt;/span&gt;，即特斯拉股票在 99% 置信水平下的单日最大潜在损失约为 6.8%。&lt;/p&gt;&#10;&lt;p&gt;此公式为风险管理者提供了定量工具，结合历史数据计算的参数和预设置信水平，可快速得出风险价值，为投资组合配置和止损策略制定提供支持。需注意，当 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 接近 0 时，需采用特殊数值算法确保计算准确性。&lt;/p&gt;&#10;&lt;ol start="3" style="list-style-type: decimal"&gt;&#10;&lt;li&gt;阈值选择与模型适用性&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;strong&gt;阈值的确定&lt;/strong&gt;：阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 的选择至关重要，需平衡偏差（ &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 过小导致非极端值混入）与方差（ &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 过大导致样本量不足）。常用的确定方法包括：&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;strong&gt;平均超出量函数（Mean Excess Plot）&lt;/strong&gt;：绘制超出量均值与阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 的关系图，寻找超出量均值近似线性的区间。此时的阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 既能有效分离极端值，又保留足够样本量用于可靠估计。例如分析特斯拉股价数据时，可设定一系列候选阈值，计算对应超出量均值并绘制曲线，选择线性区域内的阈值作为最终 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 值。&lt;/p&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hill 图法&lt;/strong&gt;：计算不同阈值下的 Hill 估计量，绘制其与阈值的关系曲线。随着阈值增加，Hill 估计量应趋于稳定，选取稳定区间内的阈值作为合适的 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 值，该方法能有效识别复杂尾部特征数据的稳定尾部参数估计区间，增强阈值选择稳健性。&lt;/p&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&lt;/li&gt;&#10;&lt;li&gt;阈值稳定性检验：确保参数 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 和 &lt;span class="math inline"&gt;\(\sigma\)&lt;/span&gt; 在 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; 增大时趋于稳定。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;div id="三r-语言实证分析tsla-数据" class="section level1"&gt;&#10;&lt;h1&gt;三、R 语言实证分析（TSLA 数据）&#10;&lt;/h1&gt;&#10;&lt;p&gt;以 TSLA 股票收益率为例，采用 POT 方法（GPD）计算极端风险 VaR，步骤如下：&lt;/p&gt;&#10;&lt;div id="数据准备与预处理" class="section level2"&gt;&#10;&lt;h2&gt;1. 数据准备与预处理&lt;/h2&gt;&#10;&lt;p&gt;使用&lt;code&gt;quantmod&lt;/code&gt;获取数据并计算对数收益率（保留损失数据，即负收益率的绝对值）：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 安装并加载包&#10;if (!require(&amp;quot;quantmod&amp;quot;)) install.packages(&amp;quot;quantmod&amp;quot;)&#10;if (!require(&amp;quot;fExtremes&amp;quot;)) install.packages(&amp;quot;fExtremes&amp;quot;)&#10;if (!require(&amp;quot;evd&amp;quot;)) install.packages(&amp;quot;evd&amp;quot;)&#10;library(quantmod)&#10;library(fExtremes)&#10;library(evd)&#10;library(ggplot2)&#10;library(showtext)&#10;font_add(&amp;quot;SimHei&amp;quot;,&amp;quot;SimHei.ttf&amp;quot;)&#10;showtext_auto()&#10;# 获取TSLA数据（2018-2023年）&#10;getSymbols(&amp;quot;TSLA&amp;quot;, from = &amp;quot;2018-01-01&amp;quot;, to = &amp;quot;2023-12-31&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;TSLA&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;tsla_close &amp;lt;- TSLA$TSLA.Close&#10;ret &amp;lt;- diff(log(tsla_close)) # 对数收益率&#10;loss &amp;lt;- -ret # 损失序列（负收益为损失）&#10;loss &amp;lt;- na.omit(loss)&#10;colnames(loss) &amp;lt;- &amp;quot;TSLA_Loss&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="阈值选择mean-excess-plot" class="section level2"&gt;&#10;&lt;h2&gt;2. 阈值选择（Mean Excess Plot）&lt;/h2&gt;&#10;&lt;p&gt;通过平均超出量函数确定阈值 &lt;span class="math inline"&gt;\(u\)&lt;/span&gt; ：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 计算平均超出量&#10;u_candidate &amp;lt;- seq(quantile(loss, 0.7), quantile(loss, 0.95), length.out = 20) # 候选阈值&#10;mean_excess &amp;lt;- sapply(u_candidate, function(u) {&#10;excess &amp;lt;- loss[loss &amp;gt; u] - u # 超出量&#10;mean(excess, na.rm = TRUE)&#10;})&#10;# 可视化平均超出量&#10;me_df &amp;lt;- data.frame(u = u_candidate, mean_excess = mean_excess)&#10;ggplot(me_df, aes(x = u, y = mean_excess)) +&#10;geom_line(color = &amp;quot;blue&amp;quot;) +&#10;geom_point() +&#10;labs(title = &amp;quot;平均超出量函数（确定阈值u）&amp;quot;, x = &amp;quot;阈值u&amp;quot;, y = &amp;quot;平均超出量&amp;quot;) +&#10;theme_minimal()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2010/12/29/var-caculation-and-evt/index_files/figure-html/unnamed-chunk-2-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 选择阈值（如u=0.03，对应约80%分位数）&#10;u &amp;lt;- 0.03&#10;excess &amp;lt;- as.numeric(loss[loss &amp;gt; u] - u) # 超出量序列&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="gpd-参数估计" class="section level2"&gt;&#10;&lt;h2&gt;3. GPD 参数估计&#10;&lt;/h2&gt;&#10;&lt;p&gt;使用&lt;code&gt;evd&lt;/code&gt;包的&lt;code&gt;fpot&lt;/code&gt;函数估计 GPD 的形状参数 &lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt; 和尺度参数 &lt;span class="math inline"&gt;\(\sigma\)&lt;/span&gt; ：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 拟合GPD模型&#10;gpd_fit &amp;lt;- fpot(as.numeric(loss), threshold = u)&#10;&#10;summary(gpd_fit) # 输出参数估计结果&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Length Class Mode &#10;## estimate 2 -none- numeric &#10;## std.err 2 -none- numeric &#10;## fixed 0 -none- NULL &#10;## param 2 -none- numeric &#10;## deviance 1 -none- numeric &#10;## corr 0 -none- NULL &#10;## var.cov 4 -none- numeric &#10;## convergence 1 -none- character&#10;## counts 2 -none- numeric &#10;## message 0 -none- NULL &#10;## threshold 1 -none- numeric &#10;## cmax 1 -none- logical &#10;## r 0 -none- NULL &#10;## ulow 1 -none- numeric &#10;## rlow 1 -none- numeric &#10;## npp 1 -none- numeric &#10;## nhigh 1 -none- numeric &#10;## nat 1 -none- numeric &#10;## pat 1 -none- numeric &#10;## extind 0 -none- NULL &#10;## data 1508 -none- numeric &#10;## exceedances 244 -none- numeric &#10;## mper 0 -none- NULL &#10;## scale 1 -none- numeric &#10;## call 3 -none- call&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 提取参数&#10;&#10;xi &amp;lt;- gpd_fit$estimate[&amp;quot;shape&amp;quot;] # 形状参数ξ&#10;&#10;sigma &amp;lt;- gpd_fit$estimate[&amp;quot;scale&amp;quot;] # 尺度参数σ&#10;&#10;N &amp;lt;- length(loss) # 总样本量&#10;&#10;Nu &amp;lt;- length(excess) # 超限值数量&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="计算-var-与回测" class="section level2"&gt;&#10;&lt;h2&gt;4. 计算 VaR 与回测&lt;/h2&gt;&#10;&lt;p&gt;基于 GPD 参数计算高置信水平（如 99%、99.5%）的 VaR：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 定义VaR计算函数（GPD）&#10;&#10;calc_evt_var &amp;lt;- function(conf_level, u, xi, sigma, N, Nu) {&#10;alpha &amp;lt;- 1 - conf_level&#10;term &amp;lt;- (N / Nu * alpha)^(-xi)&#10;&#10;var &amp;lt;- u + (sigma / xi) * (term - 1)&#10;return(var)&#10;&#10;}&#10;# 计算99%和99.5%置信水平的VaR&#10;var_99 &amp;lt;- calc_evt_var(0.99, u, xi, sigma, N, Nu)&#10;var_995 &amp;lt;- calc_evt_var(0.995, u, xi, sigma, N, Nu)&#10;cat(&amp;quot;99% VaR（EVT-GPD）：&amp;quot;, round(var_99, 4), &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 99% VaR（EVT-GPD）： 0.1115&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;99.5% VaR（EVT-GPD）：&amp;quot;, round(var_995, 4), &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 99.5% VaR（EVT-GPD）： 0.1367&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 回测：极端损失超过VaR的比例&#10;extreme_loss &amp;lt;- as.numeric(loss)[loss &amp;gt; u] # 极端损失序列&#10;fail_99 &amp;lt;- mean(extreme_loss &amp;gt; var_99)&#10;cat(&amp;quot;99% VaR失败率：&amp;quot;, round(fail_99 * 100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 99% VaR失败率： 6.15 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="结果可视化" class="section level2"&gt;&#10;&lt;h2&gt;5. 结果可视化&lt;/h2&gt;&#10;&lt;p&gt;对比实际极端损失与 VaR 阈值：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 可视化极端损失与VaR&#10;extreme_df &amp;lt;- data.frame(&#10;Date = index(loss)[loss &amp;gt; u],&#10;Extreme_Loss = as.numeric(extreme_loss)&#10;)&#10;&#10;ggplot(extreme_df, aes(x = Date, y = Extreme_Loss)) +&#10;geom_point(color = &amp;quot;red&amp;quot;) +&#10;geom_hline(yintercept = var_99, &#10; color = &amp;quot;blue&amp;quot;, &#10; linetype = &amp;quot;dashed&amp;quot;) +&#10;geom_hline(yintercept = var_995, &#10; color = &amp;quot;black&amp;quot;, &#10; linetype = &amp;quot;dashed&amp;quot;) +&#10;&#10;&#10;labs(title = &amp;quot;TSLA极端损失与EVT-GPD VaR&amp;quot;,x = &amp;quot;日期&amp;quot;, y = &amp;quot;极端损失（绝对值）&amp;quot;) +&#10;theme_minimal()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2010/12/29/var-caculation-and-evt/index_files/figure-html/unnamed-chunk-5-1.png" width="672" /&gt;&lt;/p&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;p&gt;极值理论通过直接建模尾部极端值，避免了对整体分布的依赖，在高置信水平下的 VaR 估计更具优势。实证分析显示：&lt;/p&gt;&#10;&lt;ol style="list-style-type: decimal"&gt;&#10;&lt;li&gt;TSLA 股票的极端损失呈现明显厚尾特性（ &lt;span class="math inline"&gt;\(\xi &amp;gt; 0\)&lt;/span&gt; ），适合用 GPD 建模；&lt;/li&gt;&#10;&lt;li&gt;阈值&lt;span class="math inline"&gt;\(u\)&lt;/span&gt;的选择对结果敏感，需结合平均超出量函数和参数稳定性检验确定；&lt;/li&gt;&#10;&lt;li&gt;与传统 GARCH 模型相比，EVT 方法计算的高置信水平 VaR 更保守，能更好应对 “黑天鹅” 事件。&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;未来可结合动态阈值调整或贝叶斯估计，进一步提升 EVT 模型对极端风险的动态跟踪能力。&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;注&lt;/strong&gt;：代码中&lt;code&gt;fpot&lt;/code&gt;函数需确保阈值&lt;span class="math inline"&gt;\(u\)&lt;/span&gt;合理（超限值数量建议占总样本的 5%-10%）；若&lt;span class="math inline"&gt;\(\xi\)&lt;/span&gt;接近 0，可简化为指数分布计算 VaR。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item><item><title>VaR和历史模拟法及R语言</title><link>https://gewutang.cn/2010/12/15/var-calculation-using-historical-simulation/</link><pubDate>Wed, 15 Dec 2010 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2010/12/15/var-calculation-using-historical-simulation/</guid><description>&#10;&#10;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(showtext)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: sysfonts&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: showtextdb&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;font_add(&amp;quot;SimHei&amp;quot;, &amp;quot;SimHei.ttf&amp;quot;)&#10;showtext_auto()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;div id="引言" class="section level2"&gt;&#10;&lt;h2&gt;引言&lt;/h2&gt;&#10;&lt;p&gt;现代金融领域的计量工作分为两大块，即资产定价和风险度量。而资产定价又是以度量风险并为风险定价为基础，由此可见，风险度量工作实际上是现代金融计量领域的核心。金融领域度量风险的方法有很多，其中应用最多的是方差、最大回撤和 VaR 等。&lt;/p&gt;&#10;&lt;p&gt;在业界，VaR 不仅单纯作为风险管理的工具，在进行投资组合优化时，也会将 VaR 作为重要的过滤指标之一。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="var-的定义" class="section level2"&gt;&#10;&lt;h2&gt;VaR 的定义&lt;/h2&gt;&#10;&lt;p&gt;VaR 是 Value at Risk 的简称，中文译作“在险价值”。它是指在市场正常波动情况下，投资者所持有的投资组合在未来一定时期 &lt;span class="math inline"&gt;\(T\)&lt;/span&gt; 内，在一定置信水平 &lt;span class="math inline"&gt;\(\alpha\)&lt;/span&gt; 下所可能产生的最大可能损失。&lt;/p&gt;&#10;&lt;p&gt;假设投资者持有的投资组合的初始价值为 &lt;span class="math inline"&gt;\(P_{0}\)&lt;/span&gt; ，&lt;span class="math inline"&gt;\(r\)&lt;/span&gt; 为未来一定时期 &lt;span class="math inline"&gt;\(T\)&lt;/span&gt; 对应的收益率。那么，该组合的期末价值为：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;P_{T}=P_{0}(1+r_{T})&#10;\]&lt;/span&gt;&#10;&lt;span class="math inline"&gt;\(r\)&lt;/span&gt; 可视作随机变量，若假设在某一置信水平下，该投资组合的最低价值为 &lt;span class="math inline"&gt;\(P^*\)&lt;/span&gt; ，则：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;P_{T}^*=P_{0}(1+r_{T}^*)&#10;\]&lt;/span&gt;&#10;显然，根据VaR的定义可知，其等于投资组合的最低价值和初始价值之差：&lt;/p&gt;&#10;&lt;p&gt;&lt;span class="math display"&gt;\[&#10;VaR_{\alpha}=-P_{0}r_{\alpha}^*&#10;\]&lt;/span&gt;&#10;可见，计算 VaR 值等价于计算一定置信水平下的投资组合的最低价值 &lt;span class="math inline"&gt;\(P^*\)&lt;/span&gt; 或最低收益率 &lt;span class="math inline"&gt;\(r^*\)&lt;/span&gt; 。而计算最低价值或最低收益率则需依赖于价格序列或收益率序列的分布或概率密度函数。换句话说，计算金融资产序列的分布函数或概率密度函数正是计算 VaR 值的核心之处。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="var-的算法" class="section level1"&gt;&#10;&lt;h1&gt;VaR 的算法&lt;/h1&gt;&#10;&lt;p&gt;清楚了 VaR 的定义，计算 VaR 的方法也就呼之欲出了。目前，常用的 VaR 算法可以分为参数法即方差-协方差法、蒙特卡洛模拟法以及非参数方法即历史模拟法。在 VaR 的应用过程中，人们也对 VaR 的上述算法进行了扩展。比如，在方差-协方差方法下人们提出了指数级权平均模型计算方差或者结合 Garch 类模型计算方差进而计算 VaR 的方法。&lt;/p&gt;&#10;&lt;div id="历史模拟法" class="section level2"&gt;&#10;&lt;h2&gt;历史模拟法&lt;/h2&gt;&#10;&lt;p&gt;历史模拟法是最简单的计算 VaR 的方法。它假设投资组合未来的波动与投资组合的历史数据完全一致，因此，计算投资组合的未来 VaR 就等同于计算历史数据的既有 VaR 。&lt;/p&gt;&#10;&lt;p&gt;历史模拟法的步骤为：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;搜集历史数据&lt;/li&gt;&#10;&lt;li&gt;计算历史数据的几何收益率&lt;/li&gt;&#10;&lt;li&gt;对收益率进行排序并计算给定置信水平的下分位数&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/div&gt;&#10;&lt;div id="单资产-var-的计算及其r实现" class="section level2"&gt;&#10;&lt;h2&gt;单资产 VaR 的计算及其R实现&lt;/h2&gt;&#10;&lt;p&gt;下面以计算单个资产的VaR为例：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载必要包&#10;if (!require(&amp;quot;quantmod&amp;quot;)) install.packages(&amp;quot;quantmod&amp;quot;)&#10;library(quantmod)&#10;&#10;# 获取苹果公司股票数据&#10;getSymbols(&amp;quot;AAPL&amp;quot;, src = &amp;quot;yahoo&amp;quot;, from = Sys.Date() - 365, to = Sys.Date())&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 计算对数收益率&#10;returns &amp;lt;- na.omit(periodReturn(AAPL, period = &amp;#39;daily&amp;#39;, type = &amp;#39;log&amp;#39;))&#10;&#10;# 设置置信水平 (95%)&#10;confidence_level &amp;lt;- 0.95&#10;&#10;# 历史模拟法计算VaR&#10;historical_var &amp;lt;- -quantile(returns, probs = 1 - confidence_level)&#10;&#10;# 打印结果&#10;cat(&amp;quot;历史模拟法计算的每日VaR（95%置信水平）:&amp;quot;, round(historical_var*100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 历史模拟法计算的每日VaR（95%置信水平）: 3.22 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 可视化结果&#10;hist(returns, breaks = 50, main = &amp;quot;日收益率分布与VaR（95%）&amp;quot;,&#10; xlab = &amp;quot;日收益率&amp;quot;, col = &amp;quot;lightblue&amp;quot;, border = &amp;quot;white&amp;quot;)&#10;abline(v = -historical_var, col = &amp;quot;red&amp;quot;, lwd = 2)&#10;text(x = -historical_var, y = par(&amp;quot;usr&amp;quot;)[4]*0.9, &#10; labels = paste(&amp;quot;VaR =&amp;quot;, round(historical_var*100, 2), &amp;quot;%&amp;quot;), &#10; pos = 2, col = &amp;quot;red&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2010/12/15/var-calculation-using-historical-simulation/index_files/figure-html/unnamed-chunk-1-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;也可以直接用PerformanceAnalytics包中的VaR函数计算历史模拟法下的VaR值：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载必要包&#10;if (!require(&amp;quot;quantmod&amp;quot;)) install.packages(&amp;quot;quantmod&amp;quot;)&#10;if (!require(&amp;quot;PerformanceAnalytics&amp;quot;)) install.packages(&amp;quot;PerformanceAnalytics&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Loading required package: PerformanceAnalytics&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;PerformanceAnalytics&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:graphics&amp;#39;:&#10;## &#10;## legend&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(quantmod)&#10;library(PerformanceAnalytics)&#10;&#10;# 获取苹果公司股票数据（过去250个交易日）&#10;getSymbols(&amp;quot;AAPL&amp;quot;, src = &amp;quot;yahoo&amp;quot;, from = Sys.Date() - 365, to = Sys.Date())&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 计算对数收益率&#10;returns &amp;lt;- na.omit(ROC(Cl(AAPL))) # 等效于 log(Cl(AAPL)/lag(Cl(AAPL)))&#10;&#10;# 使用PerformanceAnalytics包计算历史模拟法VaR（95%置信水平）&#10;var_result &amp;lt;- VaR(&#10; R = returns,&#10; p = 0.95,&#10; method = &amp;quot;historical&amp;quot;,&#10; portfolio_method = &amp;quot;single&amp;quot;&#10;)&#10;&#10;# 提取并格式化结果&#10;historical_var &amp;lt;- abs(var_result[1,]) # VaR值通常取正数表示损失&#10;confidence_level &amp;lt;- attr(var_result, &amp;quot;cl&amp;quot;)&#10;&#10;# 输出结果&#10;cat(&amp;quot;PerformanceAnalytics计算的每日VaR（&amp;quot;, confidence_level*100, &amp;quot;%置信水平）:&amp;quot;,&#10; round(historical_var*100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## PerformanceAnalytics计算的每日VaR（ %置信水平）: 3.22 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 可视化收益率分布及VaR&#10;hist(returns, breaks = 50, main = &amp;quot;日收益率分布与VaR（历史模拟法）&amp;quot;,&#10; xlab = &amp;quot;日收益率&amp;quot;, col = &amp;quot;lightblue&amp;quot;, border = &amp;quot;white&amp;quot;)&#10;abline(v = -historical_var, col = &amp;quot;red&amp;quot;, lwd = 2)&#10;text(x = -historical_var, y = par(&amp;quot;usr&amp;quot;)[4]*0.9,&#10; labels = paste(&amp;quot;VaR =&amp;quot;, round(historical_var*100, 2), &amp;quot;%&amp;quot;),&#10; pos = 2, col = &amp;quot;red&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2010/12/15/var-calculation-using-historical-simulation/index_files/figure-html/unnamed-chunk-2-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="多资产投资组合的-var-计算及其-r-实现" class="section level2"&gt;&#10;&lt;h2&gt;多资产投资组合的 VaR 计算及其 R 实现&lt;/h2&gt;&#10;&lt;p&gt;用历史模拟法计算多资产投资组合的 VaR 与单资产思路类似，唯一不同的是要按照当前投资组合中各资产的权重计算投资组合的历史收益率。&lt;/p&gt;&#10;&lt;p&gt;具体R代码如下:&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 加载必要包&#10;library(quantmod) # 金融数据获取&#10;library(dplyr) # 数据处理&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ######################### Warning from &amp;#39;xts&amp;#39; package ##########################&#10;## # #&#10;## # The dplyr lag() function breaks how base R&amp;#39;s lag() function is supposed to #&#10;## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #&#10;## # source() into this session won&amp;#39;t work correctly. #&#10;## # #&#10;## # Use stats::lag() to make sure you&amp;#39;re not using dplyr::lag(), or you can add #&#10;## # conflictRules(&amp;#39;dplyr&amp;#39;, exclude = &amp;#39;lag&amp;#39;) to your .Rprofile to stop #&#10;## # dplyr from breaking base R&amp;#39;s lag() function. #&#10;## # #&#10;## # Code in packages is not affected. It&amp;#39;s protected by R&amp;#39;s namespace mechanism #&#10;## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #&#10;## # #&#10;## ###############################################################################&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Attaching package: &amp;#39;dplyr&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:xts&amp;#39;:&#10;## &#10;## first, last&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:stats&amp;#39;:&#10;## &#10;## filter, lag&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#10;## &#10;## intersect, setdiff, setequal, union&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(ggplot2) # 可视化&#10;&#10;# ========== 参数设置 ==========&#10;symbols &amp;lt;- c(&amp;quot;AAPL&amp;quot;, &amp;quot;GOOG&amp;quot;, &amp;quot;MSFT&amp;quot;) # 资产代码（苹果、谷歌、微软）&#10;weights &amp;lt;- c(0.5, 0.3, 0.2) # 资产权重（总和为1）&#10;portfolio_value &amp;lt;- 1e7 # 投资组合当前价值（假设1千万元）&#10;confidence_level &amp;lt;- 0.95 # 置信水平&#10;start_date &amp;lt;- &amp;quot;2020-01-01&amp;quot; # 数据开始日期&#10;end_date &amp;lt;- &amp;quot;2023-12-31&amp;quot; # 数据结束日期&#10;&#10;# ========== 1. 获取历史价格数据 ==========&#10;getSymbols(symbols, src = &amp;quot;yahoo&amp;quot;, from = start_date, to = end_date)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot; &amp;quot;GOOG&amp;quot; &amp;quot;MSFT&amp;quot;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 提取收盘价并合并&#10;prices &amp;lt;- merge(Cl(get(symbols[1])), Cl(get(symbols[2])), Cl(get(symbols[3])))&#10;colnames(prices) &amp;lt;- symbols&#10;&#10;# 清理缺失值（确保日期对齐）&#10;prices_clean &amp;lt;- na.omit(prices)&#10;&#10;# ========== 2. 计算各资产对数收益率 ==========&#10;returns &amp;lt;- data.frame(&#10; AAPL = as.numeric(Delt(prices_clean$AAPL)),&#10; GOOG = as.numeric(Delt(prices_clean$GOOG)),&#10; MSFT = as.numeric(Delt(prices_clean$MSFT))&#10;) %&amp;gt;% na.omit() # 去除首行NA&#10;&#10;# ========== 3. 计算投资组合每日收益率 ==========&#10;# 将收益率转换为矩阵进行加权计算&#10;returns_matrix &amp;lt;- as.matrix(returns)&#10;portfolio_returns &amp;lt;- returns_matrix %*% weights&#10;&#10;# ========== 4. 历史模拟法计算VaR ==========&#10;# 按升序排列组合收益率（损失为负值）&#10;sorted_returns &amp;lt;- sort(portfolio_returns)&#10;&#10;# 确定分位数位置&#10;n &amp;lt;- length(sorted_returns)&#10;var_index &amp;lt;- floor((1 - confidence_level) * n)&#10;var_quantile &amp;lt;- sorted_returns[var_index]&#10;&#10;# 计算VaR绝对值&#10;VaR &amp;lt;- abs(var_quantile) * portfolio_value&#10;&#10;# ========== 5. 输出结果 ==========&#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;cat(&amp;quot;置信水平:&amp;quot;, confidence_level * 100, &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 置信水平: 95 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;历史数据天数:&amp;quot;, n, &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 历史数据天数: 1005&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;VaR分位数对应收益率:&amp;quot;, round(var_quantile * 100, 2), &amp;quot;%\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## VaR分位数对应收益率: -2.94 %&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;组合VaR值:&amp;quot;, round(VaR, 2), &amp;quot;元\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 组合VaR值: 294069.7 元&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# ========== 6. 可视化== ==========&#10;# 绘制组合收益率分布直方图&#10;ggplot(data.frame(Return = portfolio_returns), aes(x = Return)) +&#10; geom_histogram(bins = 50, fill = &amp;quot;lightblue&amp;quot;, color = &amp;quot;black&amp;quot;) +&#10; geom_vline(xintercept = var_quantile, color = &amp;quot;red&amp;quot;, linewidth = 1) +&#10; labs(title = &amp;quot;投资组合历史收益率分布与VaR&amp;quot;,&#10; x = &amp;quot;收益率&amp;quot;, y = &amp;quot;频数&amp;quot;) +&#10; annotate(&amp;quot;text&amp;quot;, x = var_quantile, y = Inf, &#10; label = paste0(confidence_level * 100, &amp;quot;% VaR: &amp;quot;, round(VaR, 2), &amp;quot;元&amp;quot;),&#10; color = &amp;quot;red&amp;quot;, vjust = 1.5, hjust = -0.1) +&#10; theme_minimal()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2010/12/15/var-calculation-using-historical-simulation/index_files/figure-html/unnamed-chunk-3-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;</description></item></channel></rss>