MATLAB计算投资组合的cVaR和VaR

计算条件风险价值 (Conditional Value-at-Risk, cVaR) 是一种衡量投资组合风险的方法,它关注的是损失分布的尾部风险。

MATLAB代码如下:

Matlab 复制代码
clc;close all;clear all;warning off;%清除变量
rand('seed', 100);
randn('seed', 100);
format long g;

% 随机产生数据(例如,投资组合的日收益率)
nSamples = 1000; % 设置样本数量
returns = normrnd(0, 0.01, [nSamples, 1]); % 正态分布的随机收益率

% 定义置信水平
confidenceLevel = 0.95; % 95%的置信水平

% 对收益率进行排序
[sortedReturns, sortIndices] = sort(returns);

% 计算VaR(Value-at-Risk)
VaRIndex = round(confidenceLevel * nSamples);
VaR = sortedReturns(VaRIndex);

% 计算cVaR
% cVaR是损失超过VaR的期望值
cVaRIndexStart = VaRIndex + 1;
cVaR = mean(sortedReturns(cVaRIndexStart:end));

% 输出结果
fprintf('VaR at %d%% confidence level is: %.4f\n', round(confidenceLevel*100), VaR);
fprintf('cVaR at %d%% confidence level is: %.4f\n', round(confidenceLevel*100), cVaR);

% 数据可视化
figure;
histogram(returns, 'Normalization', 'pdf', 'BinMethod', 'auto');
hold on;
% 绘制VaR和cVaR线
xlimits = xlim;
plot([VaR, VaR], ylim, 'r--', 'LineWidth', 2);
% text(VaR, ylim(2)*0.7, sprintf('VaR: %.4f', VaR), 'Color', 'r');

% cVaR是一个期望值,所以我们用一个点来表示它在直方图上的位置
plot(cVaR, 0, 'bo', 'MarkerSize', 10, 'MarkerFaceColor', 'b');
% text(cVaR, ylim(2)*0.6, sprintf('cVaR: %.4f', cVaR), 'Color', 'b');

% 设置图表标题和坐标轴标签
title('投资组合收益率分布与VaR、cVaR');
xlabel('收益率');
ylabel('概率密度');

% 释放hold状态
hold off;

程序结果如下:

相关推荐
谁不学习揍谁!12 小时前
大数据可视化看板:基于电子竞技行业数据大数据可视化分析(详细源码文档等资料)
人工智能·python·信息可视化·stylus
YangYang9YangYan17 小时前
2026中专生学习数据分析的发展指南
信息可视化
漂视数字孪生世界18 小时前
数字孪生,真的是展示价值大于实用性吗?
3d·信息可视化·数字孪生·可视化大屏·bi可视化
计算机学姐1 天前
基于SpringBoot的药房管理系统【个性化推荐+数据可视化】
java·spring boot·后端·mysql·spring·信息可视化·java-ee
liliangcsdn1 天前
如何在jupyter中使用typescript基于plotly.js画图
信息可视化
GIS好难学1 天前
【分享】120套开源数据可视化大屏H5模板
信息可视化·webgis
. . . . .2 天前
蚂蚁企业级数据可视化解决方案
信息可视化
星川皆无恙2 天前
豆瓣电影数据爬虫分析:基于 Python 的豆瓣电影数据可视化分析系统
大数据·爬虫·python·算法·机器学习·信息可视化·numpy
LaughingZhu2 天前
Product Hunt 每日热榜 | 2026-02-11
人工智能·经验分享·神经网络·信息可视化·产品运营
岱宗夫up2 天前
Python数据分析:领域专精
大数据·开发语言·python·信息可视化·数据挖掘·数据分析