Portfolio sorting using discretize and accumarray
13 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Portfolio Optimization and Asset Allocation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!