%% Pooled Regression with Cumulative Returns scaled by Volatility (t-stat for Table 6, Panel B) %%

clear;clc
addpath 'E:\RESEARCH\TSMOM\Codes_new';
addpath 'E:\Dropbox\My toolbox';
load Return.mat;

Ns = 1; Ne = 55;
Return = Return(Return(:,6)>=Ns & Return(:,6)<=Ne,:);
Return = Return(Return(:,1)>=198501 & Return(:,1)<=200912,:);
Rs = Return(:,2);
rf = Return(:,4)+1;
Exvol = Return(:,3);
Rcum = Func_GenCum(rf, Rs);

Rcumvol = Rcum(2:end,:)./Exvol(1:end-1);
z = [nan(1,12); Rcumvol];
z = [z, Return(:,1), Return(:,6)];

for n = Ns:Ne
    line1 = find(z(:,14)==n);
    z(line1(1):line1(12),:) = [];
end

res = []; res_sign = [];
for h = 1:12
   yy = [];
   for n = Ns:Ne 
       tt = find(z(:,14) == n);                                                                       %    1 -12             13       14   
       a = z(tt,:);                                                                                             %   xcum/exvol    ym      ID
       yy = [yy; a(2:end,1), a(1:end-1,h), a(2:end,[13, 14])];              %    yy = [y, x,  ym, ID]; 
   end
b = clusterreg(yy(:,1), [yy(:,2), ones(size(yy(:,2)))], yy(:,3));
res = [res; b(1, [1 3]), b(2, [1 3])];
b = clusterreg(yy(:,1), [sign(yy(:,2)), ones(size(yy(:,2)))], yy(:,3));
res_sign = [res_sign; b(1, [1 3]), b(2, [1 3])];
end

save('E:\RESEARCH\TSMOM\Codes_new\Tab6_TstatCum_All2009.mat','res','res_sign')
