Sort stocks into portolios based on stock characteristics
10 次查看(过去 30 天)
显示 更早的评论
Hello,
Suppose i have a matrix A(m,n) of stock returns, where m is time and n is stocks (So in each column i have the time serie of each stock). Suppose that i have another matrix B(m,n) containing trading volume data for the same stocks and dates. The indexing of each stock-each date is the same in the 2 matrices (So that e.g A(1,200) contains the return of the 200th stock for the 1st date of the sample and B(1,200), contains the volume of the same stock the same date). Missing data are NaN's. Is there any way to sort each row or fracions of matrix A based on the ranking of each row of B? Or even better is there a matlab built in function to split a matrix into specified fractiles based on criterias( such as rankings on stock attributes-characteristics) ? Thanks a lot in advance, ~Conrad
0 个评论
采纳的回答
Oleg Komarov
2011-3-13
I had to write some time ago a function to generate the Carhart (1997) model for assessing mutual fund performance.
Here I give you an example how to create equally weighted and cap weighted portfolios rebalanced with maximum frequency:
% Generate 100 time points and 1000 stocks: returns and volumes
A = randn(100,1000);
B = abs(randn(100,1000)*1e4);
% Random NaNs
A(randi(100000,[10000,1])) = NaN(10000,1);
B(isnan(A)) = NaN;
% Form 9 edges for quantile ptf
q = quantile(B,.1:.1:.9,2);
% sizes
szA = size(A);
szq = size(q);
% Preallocate
EW = zeros(szA(1),szq(2)+1);
CW = EW;
% sort volumes into quantiles
for r = 1:szA(1)
% Index stocks into 10 quantile ptf (11 edges)
[c,idx] = histc(B(r,:).',[0 q(r,:) inf]);
nonNaN = idx ~= 0;
% Form equally weighted ptf
EW(r,:) = accumarray(idx(nonNaN),A(r,nonNaN),[],@mean);
% Form cap weighted ptf
totV = accumarray(idx(nonNaN),B(r,nonNaN));
CW(r,:) = accumarray(idx(nonNaN),A(r,nonNaN).*B(r,nonNaN)./totV(idx(nonNaN)).');
end
If you want to rebalance it with a lower freq. you need to put an if statement inside the loop to skip the histc call.
Oleg
0 个评论
更多回答(1 个)
另请参阅
类别
在 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!