Expected Shortfall for asset returns
4 次查看(过去 30 天)
显示 更早的评论
Hi everybody!!!
I'm trying to compute the Conditional VaR (Expected Shortfall) for some funds' returns. I have a large matrix (time series) of returns (132x1773) where 132 indicates the number of months and 1773 the number of funds. For those values has been quite easy to compute the Value at Risk for each fund but now I need to compute the conditional one.
As theory suggets, CVaR is the mean of the values above the VaR itself but I'm having lots of trouble ciomputing it.
I already tried 1) storing the value in a matrix with a for loop but since each fund has different number of values below tha VaR, the storage matrix is no more a matrix and 2) using the find function
I'll try to write down some codes to make it easier to understand:
Ret %is my 132x1773 matrix of returns
VaR %is my 1x1773 row vector
[rownum, columnum] = size(Ret);
store = zeros(1,columnum)
for i = 1:columnum
ind(i) = find(Ret(:,i)>VaR(i))
Ret(:,ind(i)) = [];
CVaR(i) = mean(Ret(:,i));
end
Unable to perform assignment because the indices on the left side are not compatible with the size of the
right side.
Thanks a lot in advance!!!
0 个评论
采纳的回答
Star Strider
2022-6-15
If I understand correctly what you want to do, it would likely be easier to create ‘ind’ as a logical vector. There is also no specific need to save it to a matrix (although you can do that if it is absolutely necessary), since it is created and used once in each iteration.
% Ret %is my 132x1773 matrix of returns
% VaR %is my 1x1773 row vector
Ret = randn(5,12);
VaR = randn(1,12);
[rownum, columnum] = size(Ret);
store = zeros(1,columnum);
for i = 1:columnum
ind = Ret(:,i)>VaR(i); % Logical Vector
CVaR(i) = mean(Ret(~ind,i)); % Use Only Those Values That Do Not Meet The 'ind' Criteria
end
.
3 个评论
Star Strider
2022-6-15
As always, my pleasure!
That is certainly possible.
‘‘Can you guess why?’
I am not certain, however there are at least three possibilities that I can think of.
It could be that none of the values met the criteria and ‘ind’ returned a vector of logical 0. This would result in ‘Ret(ind,i)’ being empty.
Comparing any numeric value to NaN results in a logical value of 0 (false). In the context of ‘ind’ that would mean that none of the values matched, creating an empty vector, and the mean of an empty vector would be NaN.
If there are NaN values in the data, then it could be that all the selected rows in any particular column are all NaN.
For example:
v = NaN(5,1);
ind = v <= 2 % In This Instance 'Ret(ind,i)' Would Have No Elements
v = NaN(5,1); % If All The Elements Are 'NaN'
meanv = mean(v, 'omitnan')
So, if all the values were NaN, then mean with 'omitnan' ends up calculating the mean of an empty vector, resulting in (0/0) that is of course NaN.
The mean documentation does not specifically discuss this particular issue (at least that I could find) so this is my best guess as to what is causing the NaN values in ‘CVaR’.
.
更多回答(1 个)
KSSV
2022-6-15
[rownum, columnum] = size(Ret);
store = zeros(1,columnum)
CVaR = cell(columnum,1) ;
for i = 1:columnum
ind(i) = find(Ret(:,i)>VaR(i))
Ret(:,ind(i)) = [];
CVaR{i} = mean(Ret(:,i));
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!