Sort stocks into portfolios based on two different stock characteristics

4 views (last 30 days)
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
Christoph Kemmer
Christoph Kemmer on 29 Sep 2015
Hi Kirby,
than you very much for your reply. B and C are not derived from A.
A contains returns of 400 stocks, B and C values (=characteristics) from the same 400 stocks, but independent from A. For the moment, only B and C are important.
I try to sort each stock each year to a portfolio based on the 0.2, 0.4, 0.6 and 0.8 quantiles of the Matrices B and C.
Example: If stock X's value in Matrix B at time t is in the 0.2 quantil of all values of Matrix B at time t, and at the same time its value in Matrix C at time t in the 0.8 quantil of all values of Matrix C at time t, then stock X shall at time t be assigned to the portfolio "Lowest quantile characteristic 1 / Highest quantile characteristic 2".
I have currently troubles in finding a smart way to solving this task. Until now I used only if/ifelse commands, but this looks very inconvenient to me.
Again, I appreciate your help. Thank you very much.
John Mart
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

Sign in to comment.

Accepted Answer

Kirby Fears
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
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
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

Sign in to comment.

More Answers (1)

Sara Elsayed
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?

Community Treasure Hunt

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

Start Hunting!