%% Table II.1-3

clear;
Tlist = [120,240,480];
Nlist = 50%[50,100,200];

fulltbl = []; 
for ii = 1:length(Nlist)
    tbls = [];
    disp(ii);
    for jj=1:length(Tlist)        
        disp(jj);
        y = load(['Simulation_Results_T_',num2str(Tlist(jj)),'_N_',num2str(Nlist(ii)),'.mat']);
        tbl= [100*y.Gammatrue,[ 100*(mean(y.GammaFM')' - y.Gammatrue),sqrt((100*(std(y.GammaFM')' )).^2 + (100*(mean(y.GammaFM')' - y.Gammatrue)).^2)]];
        for kk=4:6
            mm  = 100*(mean(reshape(y.Gammahat(:,kk,:),5,y.M)')' - y.Gammatrue);
            ss  = 100*(std(reshape(y.Gammahat(:,kk,:),5,y.M)')');
        
            tbl = [tbl,[mm,sqrt(ss.^2+mm.^2)]]; 
            
        end;
        
        tbls = [tbls;tbl];
        
    end;    
    
    fulltbl =  [fulltbl,tbls];
end;

%% Table II.4

Tlist = [120,240,480];%[50,200,600];
Nlist = [50,100,200];

fulltbl2 = []; 
for ii = 1:length(Nlist)
    tbls = [];
    disp(ii);
    for jj=1:length(Tlist)        
        disp(jj);
        y = load(['Simulation_Results_T_',num2str(Tlist(jj)),'_N_',num2str(Nlist(ii)),'.mat']);
        tbl= [median(y.phat(2,:)),std(y.phat(2,:))];
        
        
        tbls = [tbls;tbl];
        
    end;    
    
    fulltbl2 =  [fulltbl2,tbls];
end;

%% Figure II.1
clear;

load(['Simulation_Results_T_',num2str(240),'_N_',num2str(200),'.mat']);


symlists = {'\gamma_0','RmRf','SMB','HML','IP'};
 nbin = 50; lw=2;
% 
 Tlist = randi([1,T],20);
 TT= length(Tlist);
 ph=5;
figure;

GG = zeros(d+1,M);
AvarGG = zeros(d+1,M);

for j = 1:M

    GG(:,j) = Gammahat(:,phat(2,j),j);

    AvarGG(:,j) = avarhat(:,phat(2,j),j);
    
end

for k=1:d+1
    
    subplot(d+1,2,2*k-1)
    binCtrs = linspace(-5,5,nbin);
    binWidth=binCtrs(2)-binCtrs(1);  
    counts=hist((GG(k,:) - Gammatrue(k))./sqrt(AvarGG(k,:)),binCtrs);
    prob = counts / (M * binWidth);   
    h1=bar(binCtrs,prob,'hist');

    set(h1,'FaceColor',[.6 .6 .6]);
    set(h1,'EdgeColor',[.6 .6 .6]);
            
    xgrid = linspace(-4,4,81);
    pdfReal=pdf('Normal',-4:0.1:4,0,1); 
    line(xgrid,pdfReal,'color','k','linestyle','--','linewidth',lw);    
    xlim([-4,4]);
    ylim([0,0.5]);
    title(['Three-Pass: ', symlists{k}])
    
end



for k=1:d+1
    
    subplot(d+1,2,2*k)
    binCtrs = linspace(-5,5,nbin);
    binWidth=binCtrs(2)-binCtrs(1);  
  
    counts=hist((reshape(GammaFM(k,:),1,M) - Gammatrue(k))./sqrt(reshape(avarhatFM(k,:),1,M)),binCtrs);
    prob = counts / (M * binWidth);   
    h1=bar(binCtrs,prob,'hist');

    set(h1,'FaceColor',[.6 .6 .6]);
    set(h1,'EdgeColor',[.6 .6 .6]);
            
    xgrid = linspace(-4,4,81);
    pdfReal=pdf('Normal',-4:0.1:4,0,1); 
    line(xgrid,pdfReal,'color','k','linestyle','--','linewidth',lw);    
    xlim([-4,4]);
    ylim([0,0.5]);
    title(['Two-Pass: ',symlists{k}])
end

 

