Portfolio sorting using discretize and accumarray

10 views (last 30 days)
I'm trying to sort equally weighted portfolios using discretize and accummarray. I want to make 3 equally weighted portfolios sorted on the signals 1,2,3. The issue arrises with the number of portfolios, if I set portEW=NaN(T,3); I get the accumarray is unable to perform the assignment because LHS is a 1 by 3 and RHS a 4 by 1.
This can be solved by setting the number of portfolios to 4 and getting 1 portfolio of just zeros and deleting it.
But this seems kind of sloppy, any suggestions on how to do it in a more correct way?
clear all
clc
RET=[1 2 3 4 ; 1 2 3 4 ; 1 2 3 4; 1 2 3 4 ];
signal=[1 2 3 1; 1 2 3 1; 1 2 3 1; 1 2 3 1];
T=length(RET);
breakpoint=NaN(length(T),3);
for i=1:T;
breakpoint(i,1)=1;
breakpoint(i,2)=2;
breakpoint(i,3)=3;
end
portEW=NaN(T,3);
for i=1:T
ind=discretize(signal(i,:),[-inf breakpoint(i,:) inf]);
portEW(i,:)=accumarray(ind(~isnan(ind))',RET(i,~isnan(ind)),[],@nanmean);
end

Answers (0)

Categories

Find more on Portfolio Optimization and Asset Allocation 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!