Split data into quintiles
18 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Good afternoon
I recently wrote a code (bottom of this question) for splitting a time series of return data (a different column for different stocks) into 5 portfolios depending on their betaCSD values. This worked perfectly. (To run the data, I copy the first line of the code into the command window).
I now want to repeat this but only for dates where the cross-sectional dispersion (CSD) is negative.
I split the CSD with its dates into two seperate matrices (where CSD is negative/positive), and then adapted all of the inputs to, also, only include values with respect to a negative CSD.
The inputs are: negcsdCSDdiff, betasdispnegcsdforcsddiff, NQ, thedatanegcsdforcsddiff, weightnegcsdCSDdiff, FFnegcsdCSDdiff
However, now, when I run the code, the outputs only appear to work for one row (the 5th) (e.g. outputRetnegcsdforcsddiff, attached).
Please could anyone see where I am going wrong?
Betasdispnegcsdforcsddiff and thedatanegcsdforcsddiff were over 3MB so I can’t attach them, so I have attached samples of them instead.
Thank you for your help
Emily
%% Commputes the returns of sorted portfolios
% Portfolios are rebalanced MONTHLY
%
% INPUT
% 'data' is a Nx(M+2) matrix of spot returns, structured as [datenum retMkt ret(1) ret(2) ... ret(M)]
% 'conData' is a Nx(M+2) matrix of data on which returns will be conditioned, structured as [datenum conMkt con(1) con(2) ... con(M)]
% 'weightData' is a Nx(M+2) matrix of values for constructing weights, structured as [datenum weightTOTAL weight(1) weight(2) ... weight(M)]
% 'NQ' is the number of quartiles at which data will be assigned
% 'CSAD' is a Nx2 matrix, structured as [datenum CSAD]
% 'FF' is a Nx3 matrix, structured as [datenum FF(1) FF(2)]
%
% OUTPUT
% 'outputRet' is a Nx(NQ+4) matrix of portfolio returns (mktCap-weighted), structured as [month year ret(1) ret(2) ... ret(NQ) ret(>0) ret(<0)]
% 'outputBetaDisp' is a Nx(NQ+4) matrix of portfolio CSAD betas (PRE-formation), structured as [month year beta(1) beta(2) ... beta(NQ) beta(>0) beta(<0)]
% 'outputBetaPost' is a Nx(NQ+4) matrix of portfolio CSAD betas (POST-formation), structured as [month year beta(1) beta(2) ... beta(NQ) beta(>0) beta(<0)]
% 'outputRetEW' is a Nx(NQ+4) matrix of portfolio returns(equally-weighted), structured as [month year ret(1) ret(2) ... ret(NQ) ret(>0) ret(<0)]
% 'alphasCAPM' is a Nx(NQ+4) matrix of portfolio alphas (POST-formation) based on the CAPM, structured as [month year alpha(1) alpha(2) ... alpha(NQ) alpha(>0) alpha(<0)]
% 'alphasFF' is a Nx(NQ+4) matrix of portfolio alphas (POST-formation) based on the FF model, structured as [month year alpha(1) alpha(2) ... alpha(NQ) alpha(>0) alpha(<0)]
% 'mktCapT' is a Nx(NQ+4) matrix of portfolio total mkt Cap, structured as [month year mktCapT(1) mktCapT(2) ... mktCapT(NQ) mktCapT(>0) mktCapT(<0)]
function [outputRetnegcsdforcsddiff, outputBetaPrenegcsdforcsddiff, outputBetaPostnegcsdforcsddiff, outputRetEW, alphasCAPM, alphasFF, mktCapT, FCSAD] = CSD_portfolioReturnsMONTHLY1forrobustness(thedatanegcsdCSDdiff, betasdispnegcsdforcsddiff, weightnegcsdCSDdiff, NQ, negcsdCSDdiff, FFnegcsdCSDdiff)
thedatanegcsdCSDdiff = sortrows(thedatanegcsdCSDdiff,1); betasdispnegcsdforcsddiff = sortrows(betasdispnegcsdforcsddiff,1); weightnegcsdCSDdiff = sortrows(weightnegcsdCSDdiff,1);
years = unique(year(thedatanegcsdCSDdiff(:,1))); template = []; for i = 1:size(years,1); a = [1:1:12]'; a(:,2) = years(i,1); template = [template; a]; end;
outputRetnegcsdforcsddiff = template; outputRetnegcsdforcsddiff(:,3:NQ+4) = NaN; % initialize output matrix for portfolio Returns
outputBetaPrenegcsdforcsddiff = template; outputBetaPrenegcsdforcsddiff(:,3:NQ+4) = NaN; % initialize output matrix for PRE-formation portfolio CSAD_betas
outputBetaPostnegcsdforcsddiff = template; outputBetaPostnegcsdforcsddiff(:,3:NQ+4) = NaN; % initialize output matrix for POST-formation portfolio CSAD_betas
outputRetEW = template; outputRetEW(:,3:NQ+4) = NaN; % initialize output matrix for portfolio Returns (equally-weighted)
alphasCAPM = template; alphasCAPM(:,3:NQ+4) = NaN; % initialize output matrix for CAPM alphas
alphasFF = template; alphasFF(:,3:NQ+4) = NaN; % initialize output matrix for FF alphas
mktCapT = template; mktCapT(:,3:NQ+4) = NaN; % initialize output matrix for Total Mkt Cap
betasFCSAD = template; betasFCSAD(:,3:NQ+2) = NaN;
for i = 2:size(outputRetnegcsdforcsddiff,1);
a = thedatanegcsdCSDdiff(year(thedatanegcsdCSDdiff(:,1)) == outputRetnegcsdforcsddiff(i,2),:); % find 'spotRet' for a given year
a = a(month(a(:,1)) == outputRetnegcsdforcsddiff(i,1),:); % find 'spotRet' for a given month within that year
aa = a(1,3:end)'; % keep all assets' return for that month in the first column of 'aa'
locX = find(thedatanegcsdCSDdiff(:,1) == a(1,1));
aa(:,2) = betasdispnegcsdforcsddiff(locX,3:end); % keep assets' conditioning data for that month (2nd column)
aa(:,3) = weightnegcsdCSDdiff(locX,3:end); % keep assets' weighting data for that month (3rd column)
aa(:,4) = [1:1:size(aa,1)];
aa = aa(~isnan(aa(:,1)),:); aa = aa(~isnan(aa(:,2)),:); aa = aa(~isnan(aa(:,3)),:); % drop NaN observations
aa = sortrows(aa,2); % sort all data according to conditioning data (i.e. according to their CSAD beats)
cc = negcsdCSDdiff(year(negcsdCSDdiff(:,1)) == outputRetnegcsdforcsddiff(i,2),:); % find CSAD observations for that month
cc = cc(month(cc(:,1)) == outputRetnegcsdforcsddiff(i,1),:);
ff = FFnegcsdCSDdiff(year(FFnegcsdCSDdiff(:,1)) == outputRetnegcsdforcsddiff(i,2),:); % find pricing factors observations for that month
ff = ff(month(ff(:,1)) == outputRetnegcsdforcsddiff(i,1),:);
if ~isempty(aa)
dataX = thedatanegcsdCSDdiff(year(thedatanegcsdCSDdiff(:,1)) == outputRetnegcsdforcsddiff(i,2),:); % find 'data' obs for a given year
dataX = dataX(month(dataX(:,1)) == outputRetnegcsdforcsddiff(i,1),:); % find 'data' obs for a given month within that year
if size(dataX,1) >= 15 % check that at least 15 returns are available for a given month
Nrows = floor((size(aa,1)-2)./NQ);
for j = 1:NQ+2
if j < NQ
b = aa((j-1)*Nrows+1 : j*Nrows,:); % get b = [ret beta mktCap A/A] for portfolio j
elseif j == NQ
b = aa((NQ-1)*Nrows+1 : end,:); % get b = [ret beta mktCap A/A] for last portfolio
elseif j == NQ +1
b = aa(aa(:,2) > 0,:); % get b = [ret beta mktCap A/A] for portfolio with positive betaDisp
elseif j == NQ+2
b = aa(aa(:,2) <= 0,:); % get b = [ret beta mktCap A/A] for portfolio with negative betaDisp
end
w = b(:,3)./sum(b(:,3)); % compute (percentage) weights for each stock
outputBetaPrenegcsdforcsddiff(i,j+2) = sum(w.*b(:,2)); % compute PRE-formation beta as the weighted average of stocks' betas
mktCapT(i,j+2) = sum(w.*b(:,3)); % compute Total Mkt Cap as the weighted average of stocks' mkt caps's
bb = NaN(size(dataX,1),size(b,1)+2); bb(:,1) = dataX(:,1);
for k = 1:size(b,1)
bb(:,k+2) = dataX(:,b(k,4)+2); % identify returns' time-series for asset k within given month
end
bb(isnan(bb)) = 0;
bb(:,2) = bb(:,3:end)*w;
% bbret = bb(:,2); % avoid error in geomean 1/3
% bbret(bbret<=-1) = -0.9999; % avoid error in geomean 2/3
% bb(:,2) = bbret; % avoid error in geomean 3/3
outputRetnegcsdforcsddiff(i,j+2) = mean(1+bb(:,2))^size(bb,1)-1;
slopes = regress(bb(:,2), [ones(size(bb,1),1) a(:,2) cc(:,2)]);
slopesCAPM = regress(bb(:,2), [ones(size(bb,1),1) a(:,2)]);
slopesFF = regress(bb(:,2), [ones(size(bb,1),1) a(:,2) ff(:,2) ff(:,3)]);
slopesFCSAD = regress(bb(:,2), [ones(size(bb,1),1) cc(:,2)]);
outputBetaPostnegcsdforcsddiff(i,j+2) = slopes(2,1);
alphasCAPM(i,j+2) = slopesCAPM(1,1);
alphasFF(i,j+2) = slopesFF(1,1);
outputRetEW(i,j+2) = mean(bb(:,2));
end
b = aa((NQ-1)*Nrows+1 : end,:);
w = b(:,3)./sum(b(:,3));
end
end
end
FCSAD = template;
for i = 1:size(FCSAD,1)
FCSAD(i,3) = sum(betasFCSAD(i,3:7) .* outputRetnegcsdforcsddiff(i,3:7));
end
0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Data Transformation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!