Sort stocks into portolios based on stock characteristics
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Oleg Komarov
am 13 Mär. 2011
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 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Portfolio Optimization and Asset Allocation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!