Portfolio sorting using discretize and accumarray

조회 수: 15 (최근 30일)
Gus Abraham
Gus Abraham 2021년 3월 9일
편집: Gus Abraham 2021년 3월 9일
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개)

카테고리

Help CenterFile 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!

Translated by