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!!!

采纳的回答

Star Strider
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 个评论
Marco Piazza
Marco Piazza 2022-6-15
Maybe the problem is that for that specific vector (where the NaN appears) the for loop did not find any values smaller than the VaR. I used a standardized VaR, maybe I should look for the historical one (based on realized returns) so that the for loop will always find smaller values.
Star Strider
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
ind = 5×1 logical array
0 0 0 0 0
v = NaN(5,1); % If All The Elements Are 'NaN'
meanv = mean(v, 'omitnan')
meanv = NaN
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
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
  1 个评论
Marco Piazza
Marco Piazza 2022-6-15
Thanks KSSV, but also with your guess I still get the same error from Matlab.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by