0

I am trying to calculate portfolio cVaR (conditional value at risk) levels from my simulated data for various portfolios.

I am able to do that for one single portfolio using the following code:

% Without a for-loop for series 1

test2 = test(:,1)

VaR_Calib_EVT   = 100 * quantile(test2, VarLevel_Calib);
help1           = sum(test2(:) <VaR_Calib_EVT/100);
cVaR_Calib_EVT  = sum(test2(test2 <VaR_Calib_EVT/100)/help1); 

However, when putting a for-loop around (see following code), the output values in cVaR_Calib_EVT are wrong except for the value in in cell (1,1).

VarLevel_Calib           = 0.05;
test                     = trnd(3,780,16); 
nIndices                 = 16;

for i=1:nIndices
    VaR_Calib_EVT  (:,i) = 100 * quantile(test(:,i), VarLevel_Calib);
    help1          (:,i) = sum(test(:,i)<(VaR_Calib_EVT(:,i)/100));
    cVaR_Calib_EVT (:,i) = sum(test(test(:,i) <VaR_Calib_EVT(:,i)/100)/help1(:,i)); 
end

What am I doing wrong?

Best, Carolin

Carolin
  • 539
  • 2
  • 7
  • 15
  • 1
    What do you mean by "carried out correctly" ? Please be more specific on what is wrong with your output, what have you checked, etc. – Ratbert Jun 17 '15 at 10:37
  • Hi Ratbert, thanks for trying to help. I have adjusted my question, hope it makes more sense now. – Carolin Jun 17 '15 at 11:29
  • Again, can you explan the meaning of "wrong" in the sentence "... the output values in cVaR_Calib_EVT are wrong except for ..." ? You have to understand that we do _not_ know what you are trying to do and what you expect as a result. To me, your code is perfectly fine since it works without error. – Ratbert Jun 17 '15 at 11:32
  • By "wrong" I mean that the values in cVaR_Calib_EVT are not the ones I would obtain when running the other code extract (without the for-loop) for all 16 portfolios separately by changing the index manually. It is only the very first value in cVaR_Calib_EVT that is equivalent to value generated from the code section without the for-loop. – Carolin Jun 17 '15 at 11:44
  • 1
    Try `cVaR_Calib_EVT (:,i) = sum(test(test(:,i) – schvaba986 Jun 17 '15 at 12:41
  • Many thanks schvaba986, your solution works perfectly! :-) – Carolin Jun 17 '15 at 13:09

1 Answers1

1

Your test variable is a three-dimensional variable, so when you do

test2 = test(:,1);

and then

test2(:) <VaR_Calib_EVT/100

it's not the same as in your second example when you do

test(:,i)<(VaR_Calib_EVT(:,i)/100)

To replicate the results of your first example you could explicitly do the test2 assignment inside the loop, which should perform as expected.

However note that using only two indices to specify parts of a 3-dimensional matrix will probably behave in unexpected ways and the long-term solution would be to explicitly specify exactly which members of the test matrix you want to include. If you do this you will be able to safely expand your single example into the loop.

xenoclast
  • 1,635
  • 15
  • 26