Need some help with a tricky loop

Hi, I'm looking to create a loop. Here's what I have:
SP_returns = 2778x1 array
for i = 2526:2777
q(i-2525) = quantile(SP_returns(i-2525:i),0.09);
end
Therefore q = 1x252 array
Now I need to express the following as a loop:
tail_SP1 = SP_returns(SP_returns(1:2526) < q(1,1)) - q(1,1);
tail_SP2 = SP_returns(SP_returns(2:2527) < q(1,2)) - q(1,2);
tail_SP3 = SP_returns(SP_returns(3:2528) < q(1,3)) - q(1,3);
...
tail_SP252 = SP_returns(SP_returns(252:2777) < q(1,252)) - 1(1,252);
Basically, tail_SP1 will be in column 1, tail_SP2 will be in column 2 in the array if I do the loop. Problem is, they have different number of observations. For example, tail_SP1 and tail_SP2 may have 227, while tail_SP10 may have 226. The loop won't work. At least not the one I know how to do. I don't think it's even possible to create a matrix which would have different number of values in each column. Perhaps there is another way of doing this thing. I really don't want to have to type 252 lines of code for this. I will appreciate all the help I can get. Thanks.

 採用された回答

Walter Roberson
Walter Roberson 2011 年 6 月 19 日

1 投票

Cell arrays can contain vectors of different length.
tail_SP{K} = SP_returns(SP_returns(K:2525+K) < q(1,K)) - q(1,K);

10 件のコメント

Julia
Julia 2011 年 6 月 19 日
This is what I tried:
for K = 1:252
tail_SP{K} = SP_returns(SP_returns(K:2525+K) < q(1,K)) - q(1,K);
end
The values I'm getting for the first vector are fine, they're all negative and less than q = -0.01, but what I'm getting for the second, third and so on vectors are not. My q is the same for the first few vectors (-0.01) and it's impossible, given my data, to have positive values in tail_SP vectors since (SP_returns < q) give values below -0.01 and if we subtract q from them we will still be getting all negative numbers.
Perhaps I misinterpreted what you were trying to tell me?
Walter Roberson
Walter Roberson 2011 年 6 月 19 日
The code I gave replicates what you asked for, but you asked for the wrong thing ;-) Try this:
for K = 1:252
tail_SP{K} = SP_returns(K-1+find(SP_returns(K:2525+K) < q(K))) - q(K);
end
Julia
Julia 2011 年 6 月 19 日
Walter, you're a genius. It worked and it gave me exactly what I wanted. Now, one more thing if you don't mind. The array I'm getting - I've never worked with it before (I'm a new to MATLAB as you may have noticed). This is what I have altogether:
for K = 1:252
tail_SP{K} = SP_returns(K-1+find(SP_returns(K:2525+K) < q(K))) - q(K);
exceedances{K} = -1.*tail_SP{K};
end
I know that I could have just multiplied the top line by -1, but I need the exceedances line to be separate. But that's not what's important.
I now want to use the gpfit function which estimates the parameters of generalized pareto. When I only had one vector for tail_SP I did the following:
paramEsts = gpfit(exceedances);
Now that I have 252 vectors I need a loop which would calculate separate parameters for each vector of exceedances. I have:
for K = 1:252
paramEsts{K} = gpfit(exceedances(K));
end
This doesn't work. So, how can I make it work?
Walter Roberson
Walter Roberson 2011 年 6 月 19 日
Very close
for K = 1:252
paramEsts{K} = gpfit(exceedances{K});
end
Walter Roberson
Walter Roberson 2011 年 6 月 19 日
By the way: is it possible for any of the tails to be empty? If so then do you want any special handling for that case?
Julia
Julia 2011 年 6 月 19 日
No, they can't be empty.
Julia
Julia 2011 年 6 月 19 日
By the way, the code worked perfectly. Thank you very much. Now I just need to get another 3 loops based on the estimates and I'm done. I'll try to do it myself but something tells me I'll probably be back here in a couple of minutes. :)
Julia
Julia 2011 年 6 月 19 日
Actually, I think I can do the loops, I just need to know how to get a specific value within each vector on its own. I mean every vector has two values. What if I want the first value in the fifth vector? If I type paramEsts{5} I get both values. I tried the variations of it like paramEsts{1,5}. Didn't work.
Julia
Julia 2011 年 6 月 19 日
Never mind, I got it.
Walter Roberson
Walter Roberson 2011 年 6 月 19 日
Sorry, it was my turn to cut the front lawn ;-)
Just for completeness, in case someone else reads this and wonders what the solution is, the syntax would be
paramEsts{5}(1)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by