Loop for rolling window

2 ビュー (過去 30 日間)
Alessandro
Alessandro 2016 年 5 月 3 日
編集済み: Image Analyst 2016 年 5 月 3 日
Hi, I need to divide a time series of returns into portfolios based on stocks that showed the lowest turnover. The problem is that I need to do this over a period of 20 years for each month. Here a little part of the code only for two months. I'm wondering whether there is a much shorter way to write this, for example using a loop which allows me to sort the stocks every month and compute the mean of returns. I was thinking about a "while" loop but I actually do not really know how to implement this. Any little hint??
Thank you!
%%Year 2011 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
R_T_Y11=[R_T(:,1) R_T(:,242:254) R_T(:,542:554)];
%%Month 1 Year 11
R_T_Y11_M1=[R_T_Y11(:,1) R_T_Y11(:,2) R_T_Y11(:,14) R_T_Y11(:,3)];
R_T_Y11_M1_S=sortrows(R_T_Y11_M1,3);
%%Dividing into portfolios
data.ptf1_Y11_M1=R_T_Y11_M1_S(1:43,:);
data.ptf2_Y11_M1=R_T_Y11_M1_S(44:86,:);
data.ptf3_Y11_M1=R_T_Y11_M1_S(87:128,:);
data.ptf4_Y11_M1=R_T_Y11_M1_S(129:170,:);
data.ptf5_Y11_M1=R_T_Y11_M1_S(171:212,:);
data.ptf6_Y11_M1=R_T_Y11_M1_S(212:254,:);
data.ptf7_Y11_M1=R_T_Y11_M1_S(255:297,:);
data.ptf8_Y11_M1=R_T_Y11_M1_S(339:383,:);
data.ptf9_Y11_M1=R_T_Y11_M1_S(384:428,:);
%%Computing mean for each portfolio month 1
MediaM1=zeros(9,3);
MediaM1(1,:)=mean(data.ptf1_Y11_M1(:,2:4),1);
MediaM1(2,:)=mean(data.ptf2_Y11_M1(:,2:4),1);
MediaM1(3,:)=mean(data.ptf3_Y11_M1(:,2:4),1);
MediaM1(4,:)=mean(data.ptf4_Y11_M1(:,2:4),1);
MediaM1(5,:)=mean(data.ptf5_Y11_M1(:,2:4),1);
MediaM1(6,:)=mean(data.ptf6_Y11_M1(:,2:4),1);
MediaM1(7,:)=mean(data.ptf7_Y11_M1(:,2:4),1);
MediaM1(8,:)=mean(data.ptf8_Y11_M1(:,2:4),1);
MediaM1(9,:)=mean(data.ptf9_Y11_M1(:,2:4),1);
%%Month 2 Year 11
R_T_Y11_M2=[R_T_Y11(:,1) R_T_Y11(:,3) R_T_Y11(:,15) R_T_Y11(:,4)];
R_T_Y11_M2_S=sortrows(R_T_Y11_M2,3);
%%Dividing into portfolios
data.ptf1_Y11_M2=R_T_Y11_M2_S(1:43,:);
data.ptf2_Y11_M2=R_T_Y11_M2_S(44:86,:);
data.ptf3_Y11_M2=R_T_Y11_M2_S(87:128,:);
data.ptf4_Y11_M2=R_T_Y11_M2_S(129:170,:);
data.ptf5_Y11_M2=R_T_Y11_M2_S(171:212,:);
data.ptf6_Y11_M2=R_T_Y11_M2_S(212:254,:);
data.ptf7_Y11_M2=R_T_Y11_M2_S(255:297,:);
data.ptf8_Y11_M2=R_T_Y11_M2_S(339:383,:);
data.ptf9_Y11_M2=R_T_Y11_M2_S(384:428,:);
%%Computing mean for each portfolio month 2
...as above!

回答 (0 件)

カテゴリ

Help Center および File ExchangePortfolio Optimization and Asset Allocation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by