Sort stocks into portfolios based on two different stock characteristics
4 views (last 30 days)
Show older comments
Christoph Kemmer
on 29 Sep 2015
Commented: John Mart
on 22 Jun 2018
Hello,
I try to sort stocks into portfolios and calculate their market cap weighted porrfolio returns based on two different stock characteristics. Suppose I have a Matrix A (180,400) with 180 (monthly) return timeseries of 400 different stocks. Matrices B and C (both 15x400) contain two different characteristics of each company, but at different dates (yearly, therefore 15 rows).
All three Matrices contain NaNs.
Firms shall be yearly sorted into portfolios based on the yearly 5 quantiles (0.2:0.2:0.8) of each of the two characteristics. Two different characteristics with 5 quantiles results in 5x5 = 25 portfolios each year.
I would be very delighted if anyone could give me some hints.
Please let me know if anyone of you had to solve the same problem in the past already.
Thank you very much in advance.
3 Comments
John Mart
on 22 Jun 2018
If anyone want help in double sorting code or any other aassignment help then contact me.. +917466888844 Or whatsapp me +917466888844
Accepted Answer
Kirby Fears
on 30 Sep 2015
Edited: Kirby Fears
on 30 Sep 2015
First, you should get rid of the NaN values in B and C by replacing them with something meaningful. If a NaN value in B is considered poor, replace it with -999 (or some other poor statistic) as a placeholder so it is nicely sorted into the worst (middling, or best) quantile based on your replacement value.
Once NaN's are removed from B and C, the code below creates 25 portfolios each year based on B and C quantiles.
% Making data
B=randn(15,400);
C=randn(15,400);
divB=5; % this must divide size(B,2)
divC=5; % this must divide size(C,2)
% These will be useful in loop later
stepB=size(B,2)/divB;
stepC=size(C,2)/divC;
% For each year (dim1), group into percentiles
[~,sortIdxB]=sort(B,2);
[~,sortIdxC]=sort(C,2);
% One collection of portfolios per year
portfolios=cell(size(B,1),1);
for yr=1:size(B,1),
% Each year has divB*divC portfolios
temp=cell(divB,divC);
for prcB=1:divB,
tempB=sortIdxB(yr,(1+(prcB-1)*stepB):prcB*stepB);
for prcC=1:divC,
tempC=sortIdxC(yr,(1+(prcC-1)*stepC):prcC*stepC);
temp{prcB,prcC}=intersect(tempB,tempC);
end,
end,
portfolios{yr}=temp;
end,
Hope this helps.
9 Comments
Walter Roberson
on 14 Feb 2017
Richard Mawulawoe Ahadzie comments,
How come Matrix A is not in the code. Was to double sort A based on B and C.
John Mart
on 22 Jun 2018
anyone want help in double sorting code or any other aassignment help then contact me.. +917466888844 Or whatsapp me +917466888844
More Answers (1)
Sara Elsayed
on 13 Nov 2017
Hi Thanks for very useful discussion I want to ask why when I integrate this command aNanIdx=any(isnan(A(1+(yr-1)*12:yr*12,:)),1); it does not work, I have tried different representation, however, the common message is Index exceeds matrix dimensions. Can you help me with that? Second, how can I get the corresponding monthly returns to each portfolio?
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!