Expected Shortfall for asset returns

3 views (last 30 days)
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!!!

Accepted Answer

Star Strider
Star Strider on 15 Jun 2022
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 Comments
Star Strider
Star Strider on 15 Jun 2022
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’.
.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 15 Jun 2022
[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 Comment
Marco Piazza
Marco Piazza on 15 Jun 2022
Thanks KSSV, but also with your guess I still get the same error from Matlab.

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!