%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Program        : MatlabCode                                            %    
%  Date           : 08/2020                                               %    
%  Description    : Replicate Fu (2009) JFE                               %  
%                   Calcualte and Etimate Variables                       %
%                   Summary Statistics of Variables                       % 
%                   Regression and Sorting Potfolio Analysis              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% NOTE: Throughout the program, missing values are given a value of -9.9999

%% Section 1
%  Load data generated from SAS Code and from other sources
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ----------------------------- Load Data  -------------------------------%
% 1. Daily Stock Data
%{
clear
clc

dataoutdaily1_allstocks = load('dataoutdaily1_allstocks.txt');
dataoutdaily2_allstocks = load('dataoutdaily2_allstocks.txt');
dataoutdaily3_allstocks = load('dataoutdaily3_allstocks.txt');
dataoutdaily4_allstocks = load('dataoutdaily4_allstocks.txt');
dataoutdaily5_allstocks = load('dataoutdaily5_allstocks.txt');
dataoutdaily6_allstocks = load('dataoutdaily6_allstocks.txt');
dataoutdaily7_allstocks = load('dataoutdaily7_allstocks.txt');
dataoutdaily8_allstocks = load('dataoutdaily8_allstocks.txt');
dataoutdaily9_allstocks = load('dataoutdaily9_allstocks.txt');

save dataoutdaily_allstocks.mat

clear
clc
%}
% Daily Stock Data Description
%{
No.VarName      Description
1  PERMNO	    Permno Number
2  DATE         Date (yyyymmdd)
3  RET	        Daily Holding Period Return
4  mmf3_daily   Market excess return (Fama & French Three Factors) (%)
5  smb3_daily   Small Minus Big Factor (Fama & French Three Factors)(%)
6  hml3_daily   High Minus Low Factor (Fama & French Three Factors)(%)
7  rf3_daily    Risk free rate (Fama & French Three Factors)(%)
8  mmf5_daily   Market excess return (Fama & French Five Factors) (%)
9  smb5_daily   Small Minus Big Factor (Fama & French Five Factors)(%)
10 hml5_daily   High Minus Low Factor (Fama & French Five Factors)(%)
11 rmw5_daily   Robust Minus Weak (Fama & French Five Factors)(%)
12 cma5_daily   Conservative Minus Aggresive (Fama & French Five Factors)(%)
13 rf5_daily    Risk free rate (Fama & French Five Factors)(%)
14 exchcd       Exchange Code
%}

% 2. Monthly Stock Data
%{

clear
clc

dataoutmonthly1_allstocks = load('dataoutmonthly1_allstocks.txt');
save dataoutmonthly1_allstocks.mat dataoutmonthly1_allstocks

clear
clc

%} 
% Monthly Stock Data Description
%{

No.VarName      Description
1  PERMNO       Permno Number 
2  PRC          Stock Price at the end of month (t)
3  VOL          Trading Volume in month (t) (in hundreds)
4  RET          Monthly Holding Period Return for month (t) 
5  RETX         Monthly Holding Period Return (ex-dividend) for month (t)
6  SHROUT       Number of Shares Outstanding at the end of month (t)
                (in thousands)
7  CFACPR       Cumulative Factor to Adjust Price at the end of month (t) 
8  yearmonth    YearMonth (yyyymm) 
9  mmf3_monthly Market excess return (Fama & French Three Factors) (%)
10 smb3_monthly Small Minus Big Factor (Fama & French Three Factors)(%)
11 hml3_monthly High Minus Low Factor (Fama & French Three Factors)(%)
12 rf3_monthly  Risk free rate (Fama & French Three Factors)(%)
13 mmf5_monthly Market excess return (Fama & French Five Factors) (%)
14 smb5_monthly Small Minus Big Factor (Fama & French Five Factors)(%)
15 hml5_monthly High Minus Low Factor (Fama & French Five Factors)(%)
16 rmw5_monthly Robust Minus Weak (Fama & French Five Factors)(%)
17 cma5_monthly Conservative Minus Aggresive (Fama & French Five Factors)(%)
18 rf5_monthly  Risk free rate (Fama & French Five Factors)(%)
19 caldt        Ending date of month (t) (yyyymmdd)
20 VWRETD       Value Weighted Return of CRSP Market Portfolio for 
                month (t)
21 lagVWRETD    Value Weighted Return of CRSP Market Portfolio for 
                month (t-1)
% (Before January 1950, VWRETD and lagVWRETD are market portfolio
   returns obtained from French's Website;
   On and after January 1950, VWRETD and lagVWRETD are market portfolio
   returns obtained from CRSP)
22 ME_dec       Market Capitalization at the end of December in last year 
                (in $thousands)
23 ME_jun       Market Capitalization at the end of June
                as in Fama & French (1992) (in $thousands)
24 BE           Book Value of Stockholders Equity 
                as in Fama & French (1992) (in $millions)
25 prc_last     Stock Price at the end of month (t-1)
26 shrout_last  Number of Shares Outstanding at the end of month (t-1)
                (in thousands)
27 cfacpr_last  Cumulative Factor to Adjust Price at the end of month (t-1) 
28 prc_last2    Stock Price at the end of month (t-2)
29 shrout_last2 Number of Shares Outstanding at the end of month (t-2)
                (in thousands)
30 cfacpr_last2 Cumulative Factor to Adjust Price at the end of month (t-2)
31 exchcd       Exchange Code
32 indsample    Indicator of whether a stock is traded after July 1963
                ( 0 = Not traded after July 1963;
                  1 = Traded after July 1963    ;)
%}

% 3. Daily & Monthly Fama & French Three Factors Data
%{
clear
clc

load data_famafrench3factor_daily.txt
load data_famafrench3factor_monthly.txt
save dataother.mat data_famafrench3factor_daily ...
                   data_famafrench3factor_monthly

clear
clc
%}

% 4. Monthly CRSP market portfolio
%{
clear
clc

load datamarket1.txt
save datamarket1.mat datamarket1

clear
clc
%}

% 5. IBES data
%{
clear
clc
load dataIBES1_UnAdj_allstocks.txt
save dataIBES1_UnAdj_allstocks.mat dataIBES1_UnAdj_allstocks
clear
clc
%}
% IBES Data Description
%{
No.VarName      Description
1  PERMNO       Permno Number 
2  date         Ending date of a month (yyyymmdd)
3  Price        Stock price as of the ending date of a month
4  CFACPR       Cumulative Factor to Adjust Price at the end of month (t) 
5  ANNDATE      Most Recent Earnings Announcement Date (yyyymmddd)
6  EPS          Most Recently Announced EPS
7  FE1          Most Recent Forecast EPS for next first Fiscal Year
8  FE2          Most Recent Forecast EPS for next second  Fiscal Year 
9  LTG          Most Recent Forest Long Term Growth Rate of EPS (%)
10 YearMonth    YearMonth (yyyymm)
11 NP           Net Payout (in $ millions)
12 IB           Income Before Extraordinary Items (in $ millions)
13 FE1_DEC      Earnings Forecast as of December in the last year for
                fiscal year ending in this year
14 SHROUT_DEC   Number of Shares Outstanding as of December in the last
                year (in thousands)
15 SIC1         Compustat SIC codes for the fiscal year ending in calendar 
                year y-1  
16 SIC2         CRSP SIC codes for June of year y
%}

% 6. Fama & French 48 Industry Code
%{
clear
clc

load Siccode48.txt
save dataSiccode48.mat Siccode48
clear
clc
%}
% Fama & French 48 Industry Code Data Description
%{
No.VarName      Description
1  IndustryNo.  Industry Number (from 1 to 48)
2  Code1        Smallest Industry Code within an industry classification
3  Code2        Largest Industry Code within an industry classification
%}

% 7. US Real GDO Growth Rate data
%{
clear
clc

load dataGDP.xlsx
save dataGDP.mat dataGDP
clear
clc
%}
% US Real GDO Growth Rate data Description
%{
No.VarName      Description
1  year         calendar year (yyyy)
2  RealGDP      Annual Real GDP   
3  GDPG         Growth Rate of Annual Real GDP 
%}

% 8. Historical Book Equity Data obtained from French's website
%{
clear
clc

load DFF_BE_With_Nonindust.txt
save data_BE_1926_2001.mat DFF_BE_With_Nonindust
clear
clc
%}
% USHistorical Book Equity data Description
%{
No.     VarName       Description
1       PERMNO        Permno Number 
2       FirstYear     First_Moody_Year
3       LastYear      Last_Moody_Year
4 - 79  BE            Book value of equity (in millions) for year 1926 - 
                      2001
%}



% --------------------------- Load Data End ------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Section 2
%  Calculate variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ------------------------------ Variables -------------------------------%

% --------------------------------- IVOL ---------------------------------%
% Calculate Monthly Idiosyncratic Volatility using daily return in one
% month
%{
clear
clc

% Minimum number of trading days in a month
cut1_1 = 15;

% Count number of trading days in each month
load dataother.mat data_famafrench3factor_daily
datadays    = data_famafrench3factor_daily;
clear data_famafrench3factor_daily
datadays    = [datadays floor(datadays(:,1) / 100)];
monthallall = unique(datadays(:,end));

datadays1=[];
for j = 1:size(monthallall,1)
    tmpdays   = datadays(datadays(:,end) == monthallall(j),:);
    datadays1 = [datadays1;monthallall(j) size(tmpdays,1)];
    clear tmpdays

end
clear j monthallall
clear datadays


for ppp = 1:9

    if ppp == 1
       load dataoutdaily_allstocks.mat dataoutdaily1_allstocks
       data = dataoutdaily1_allstocks;
       clear dataoutdaily1_allstocks
    elseif ppp == 2
       load dataoutdaily_allstocks.mat dataoutdaily2_allstocks
       data = dataoutdaily2_allstocks;
       clear dataoutdaily2_allstocks
    elseif ppp == 3
       load dataoutdaily_allstocks.mat dataoutdaily3_allstocks
       data = dataoutdaily3_allstocks;
       clear dataoutdaily3_allstocks
    elseif ppp == 4
       load dataoutdaily_allstocks.mat dataoutdaily4_allstocks
       data = dataoutdaily4_allstocks;
       clear dataoutdaily4_allstocks
    elseif ppp == 5
       load dataoutdaily_allstocks.mat dataoutdaily5_allstocks
       data = dataoutdaily5_allstocks;
       clear dataoutdaily5_allstocks
    elseif ppp == 6
       load dataoutdaily_allstocks.mat dataoutdaily6_allstocks
       data = dataoutdaily6_allstocks;
       clear dataoutdaily6_allstocks
    elseif ppp == 7
       load dataoutdaily_allstocks.mat dataoutdaily7_allstocks
       data = dataoutdaily7_allstocks;
       clear dataoutdaily7_allstocks
    elseif ppp == 8
       load dataoutdaily_allstocks.mat dataoutdaily8_allstocks
       data = dataoutdaily8_allstocks;
       clear dataoutdaily8_allstocks
    elseif ppp == 9
       load dataoutdaily_allstocks.mat dataoutdaily9_allstocks
       data = dataoutdaily9_allstocks;
       clear dataoutdaily9_allstocks
    end

Aout_resid = [];
% Stocks are traded on NYSE, AMEX, or NASDAQ
data       = data(data(:,14) >=1 & data(:,14) <= 3 ,:);
permno0    = unique(data(:,1));

for i = 1:size(permno0,1)
    
    tmpout  = [];
    tmpdata = data(data(:,1) == permno0(i),:);
    tmpdata = [tmpdata floor(tmpdata(:,2) / 100)];
    date0   = unique(tmpdata(:,end));
    
    for j = 1:size(date0,1)
        
        % Find numder of trading days in a month
        tmptmpdays = datadays1(datadays1(:,1) == date0(j),2);
       
        tmptmpdata = tmpdata(tmpdata(:,end) == date0(j),:);
        
        % Eliminate missing stock return or missing factor
        tmptmpdata(tmptmpdata(:,3) == -9.9999 ...
                 | tmptmpdata(:,4) == -9.9999,:) = [];
        
        % Number of trading days should be greater than the minimum
        % requirement
        if size(tmptmpdata,1) >= cut1_1 
           
           size0 = size(tmptmpdata,1);
           % Eliminate zero return days
           tmptmpdata(tmptmpdata(:,3) == 0,:) = [];
           
           % IVOL Relative to Fama French Three Factor Model
           yyy0   = tmptmpdata(:,3) - tmptmpdata(:,7) / 100;
           % Dependent Variable: Daily Excess Return 
           xxx0   = [ones(size(yyy0,1),1) tmptmpdata(:,[4 5 6]) / 100];
           % Independent Variables: (1) Market Excess Return
           %                        (2) SMB
           %                        (3) HML
           rltff3 = ols1(yyy0,xxx0);
           
           % IVOL Relative to Fama French Five Factor Model
           yyy0   = tmptmpdata(:,3) - tmptmpdata(:,13) / 100;
           % Dependent Variable: Daily Excess Return 
           xxx0   = [ones(size(yyy0,1),1) ...
                     tmptmpdata(:,[8 9 10 11 12]) / 100];
           % Independent Variables: (1) Market Excess Return
           %                        (2) SMB
           %                        (3) HML
           %                        (4) CMA
           %                        (5) RMW
           rltff5 = ols1(yyy0,xxx0);

           tmpout = [tmpout;permno0(i) date0(j)... 
                            std(rltff3.resid,0) * tmptmpdays^0.5...
                            std(rltff5.resid,0) * tmptmpdays^0.5...
                            size0];
           clear yyy0 xxx0 rltff3 rltff5 size0
        end
        
        clear tmptmpdata tmptmpdays
    end
    
    Aout_resid = [Aout_resid;tmpout];
    clear date0 tmpdata tmpout tmpoutstat1 tmpoutstat2 a
    
end

% Save calculated monthly IVOL

if ppp == 1
   save tmpoutresid1_allstocks_nonzero.mat Aout_resid
elseif ppp == 2
   save tmpoutresid2_allstocks_nonzero.mat Aout_resid
elseif ppp == 3
   save tmpoutresid3_allstocks_nonzero.mat Aout_resid
elseif ppp == 4
   save tmpoutresid4_allstocks_nonzero.mat Aout_resid
elseif ppp == 5
   save tmpoutresid5_allstocks_nonzero.mat Aout_resid
elseif ppp == 6
   save tmpoutresid6_allstocks_nonzero.mat Aout_resid
elseif ppp == 7 
   save tmpoutresid7_allstocks_nonzero.mat Aout_resid
elseif ppp == 8
   save tmpoutresid8_allstocks_nonzero.mat Aout_resid
elseif ppp == 9
   save tmpoutresid9_allstocks_nonzero.mat Aout_resid
end

end

%}
% Monthly IVOL calculated using daily return in one month Data Description
%{
No.VarName      Description
1  PERMNO	    Permno Number
2  DATE         YearMonth (yyyymm)
3  IVOLff3	    Monthly IVOL relative to Fama French Three Factor Model
4  IVOLff5      Monthly IVOL relative to Fama French Three Factor Model
5  Num          Number of trading days in a month
%}
% Calculate Monthly Idiosyncratic Volatility using daily return in the
% past three months, past six months, past one year
%{

clear
clc

% Calculate number of trading days within a month
load dataother.mat data_famafrench3factor_daily
datadays    = data_famafrench3factor_daily;
clear data_famafrench3factor_daily
datadays    = [datadays floor(datadays(:,1) / 100)];
monthallall = unique(datadays(:,end));
datadays1   = [];
for j = 1:size(monthallall,1)
    tmpdays   = datadays(datadays(:,end) == monthallall(j),:);
    datadays1 = [datadays1;monthallall(j) size(tmpdays,1)];
    clear tmpdays
end
clear j monthallall
clear datadays

% Minimum number of trading days in a month
cut1_1 = 15;

       
for ppp = 1:9
    
    if ppp == 1
       load dataoutdaily_allstocks.mat dataoutdaily1_allstocks
       data = dataoutdaily1_allstocks;
       clear dataoutdaily1_allstocks
    elseif ppp == 2
       load dataoutdaily_allstocks.mat dataoutdaily2_allstocks
       data = dataoutdaily2_allstocks;
       clear dataoutdaily2_allstocks
    elseif ppp == 3
       load dataoutdaily_allstocks.mat dataoutdaily3_allstocks
       data = dataoutdaily3_allstocks;
       clear dataoutdaily3_allstocks     
    elseif ppp == 4
       load dataoutdaily_allstocks.mat dataoutdaily4_allstocks
       data = dataoutdaily4_allstocks;
       clear dataoutdaily4_allstocks
    elseif ppp == 5
       load dataoutdaily_allstocks.mat dataoutdaily5_allstocks
       data = dataoutdaily5_allstocks;
       clear dataoutdaily5_allstocks
    elseif ppp == 6
       load dataoutdaily_allstocks.mat dataoutdaily6_allstocks
       data = dataoutdaily6_allstocks;
       clear dataoutdaily6_allstocks
    elseif ppp == 7
       load dataoutdaily_allstocks.mat dataoutdaily7_allstocks
       data = dataoutdaily7_allstocks;
       clear dataoutdaily7_allstocks
    elseif ppp == 8
       load dataoutdaily_allstocks.mat dataoutdaily8_allstocks
       data = dataoutdaily8_allstocks;
       clear dataoutdaily8_allstocks
    elseif ppp == 9
       load dataoutdaily_allstocks.mat dataoutdaily9_allstocks
       data = dataoutdaily9_allstocks;
       clear dataoutdaily9_allstocks
    end

Aout_resid = [];
% Stocks are traded on NYSE, AMEX, or NASDAQ
data       = data(data(:,14) >=1 & data(:,14) <= 3 ,:);
permno0    = unique(data(:,1));


for i = 1:size(permno0,1)
    
    tmpout  = [];
    tmpdata = data(data(:,1) == permno0(i),:);
    tmpdata = [tmpdata floor(tmpdata(:,2) / 100)];
    date0   = unique(tmpdata(:,end));
    date0   = date0(date0>=196307,:);
    
    for j = 1:size(date0,1)
        
        ppp0     = find(datadays1(:,1) == date0(j));
        month0   = datadays1(ppp0,1);
        month_2  = datadays1(ppp0 - 2,1);
        month_5  = datadays1(ppp0 - 5,1);
        month_11 = datadays1(ppp0 - 11,1);
        
        tmptmpdata_3month  = tmpdata(tmpdata(:,end) >= month_2 & ...
                             tmpdata(:,end) <= month0,:);
        tmptmpdata_6month  = tmpdata(tmpdata(:,end) >= month_5 & ...
                             tmpdata(:,end) <= month0,:);
        tmptmpdata_12month = tmpdata(tmpdata(:,end) >= month_11 & ...
                             tmpdata(:,end) <= month0,:);
        
        tmptmpdays0 = datadays1(datadays1(:,1) == month0,2);
                         
        % Eliminate missing stock return or missing factor
        tmptmpdata_3month(tmptmpdata_3month(:,3) == -9.9999 | ...
        tmptmpdata_3month(:,4) == -9.9999,:)  = [];
        tmptmpdata_6month(tmptmpdata_6month(:,3) == -9.9999 | ...
        tmptmpdata_6month(:,4) == -9.9999,:)  = [];
        tmptmpdata_12month(tmptmpdata_12month(:,3) == -9.9999 | ...
        tmptmpdata_12month(:,4) == -9.9999,:) = [];
    
        numday_3month    = size(tmptmpdata_3month,1);
        numday_6month    = size(tmptmpdata_6month,1);
        numday_12month   = size(tmptmpdata_12month,1);
        nummonth_3month  = size(unique(tmptmpdata_3month(:,end)),1);
        nummonth_6month  = size(unique(tmptmpdata_6month(:,end)),1);
        nummonth_12month = size(unique(tmptmpdata_12month(:,end)),1);
        
        clear ppp0 month_2 month_5 month_11
        for mmm = 1:3
            if mmm == 1
               tmptmpdata0 = tmptmpdata_3month;
            elseif mmm == 2
               tmptmpdata0 = tmptmpdata_6month;
            elseif mmm == 3
               tmptmpdata0 = tmptmpdata_12month;
            end
            
            if size(tmptmpdata0,1) >= cut1_1            
               yyy0 = tmptmpdata0(:,3) - tmptmpdata0(:,7) / 100;
               % Dependent Variable: Daily Excess Return 
               xxx0 = [ones(size(yyy0,1),1) tmptmpdata0(:,[4 5 6]) / 100];
               % Independent Variables: (1) Market Excess Return
               %                        (2) SMB
               %                        (3) HML
               rltff3 = ols1(yyy0,xxx0);
               IVOL0  = (sum(rltff3.resid.^2) / size(tmptmpdata0,1) ...
                         * tmptmpdays0)^0.5;
               if isnan(IVOL0) == 1 IVOL0 = -9.9999;end
                  clear tmptmpdata0 yyy0 xxx0 rltff3
            else
               IVOL0 = -9.9999;
            end
            
            if mmm == 1
               IVOL_3month  =  IVOL0;
            elseif mmm == 2
               IVOL_6month  =  IVOL0;
            elseif mmm == 3
               IVOL_12month =  IVOL0;
            end
            clear tmptmpdata0 IVOL0    
        end
                     
        tmpout = [tmpout;permno0(i) date0(j) ...
                  nummonth_3month numday_3month IVOL_3month ...
                  nummonth_6month numday_6month IVOL_6month ...
                  nummonth_12month numday_12month IVOL_12month];
        
        clear tmptmpdata_3month tmptmpdata_6month tmptmpdata_12month
        clear nummonth_3month numday_3month IVOL_3month
        clear nummonth_6month numday_6month IVOL_6month
        clear nummonth_12month numday_12month IVOL_12month
        clear tmptmpdays0
    end
    Aout_resid = [Aout_resid;tmpout];
    clear date0 tmpdata tmpout tmpoutstat1 tmpoutstat2 a
    
end
if ppp == 1
   save tmpoutresid1_allstocks_otherIVOL.mat Aout_resid
elseif ppp == 2
   save tmpoutresid2_allstocks_otherIVOL.mat Aout_resid
elseif ppp == 3
   save tmpoutresid3_allstocks_otherIVOL.mat Aout_resid
elseif ppp == 4
   save tmpoutresid4_allstocks_otherIVOL.mat Aout_resid
elseif ppp == 5
   save tmpoutresid5_allstocks_otherIVOL.mat Aout_resid
elseif ppp == 6
   save tmpoutresid6_allstocks_otherIVOL.mat Aout_resid
elseif ppp == 7
   save tmpoutresid7_allstocks_otherIVOL.mat Aout_resid
elseif ppp == 8
   save tmpoutresid8_allstocks_otherIVOL.mat Aout_resid
elseif ppp == 9
   save tmpoutresid9_allstocks_otherIVOL.mat Aout_resid
end
end
%}
% Monthly IVOL calculated using daily return in 3/6/12 month 
% Data Description
%{
No.VarName      Description
1  PERMNO	    Permno Number
2  DATE         YearMonth (yyyymm)
3  NumMonth	    Number of month to calculate IVOL in the previous 3 months
4  NumDay	    Number of day to calculate IVOL in the previous 3 months
5  IVOL  	    Monthly IVOL calculated using daily return in the precious
                3 months
6  NumMonth	    Number of month to calculate IVOL in the previous 6 months
7  NumDay	    Number of day to calculate IVOL in the previous 6 months
8  IVOL  	    Monthly IVOL calculated using daily return in the precious
                6 months
9  NumMonth	    Number of month to calculate IVOL in the previous 12 months
10 NumDay	    Number of day to calculate IVOL in the previous 12 months
11 IVOL  	    Monthly IVOL calculated using daily return in the precious
                12 months
%}
% Calculate Monthly Idiosyncratic Volatility using monthly return in the
% past past 1 year, 3 years, 5 years and all years
%{

clear
clc

load dataoutmonthly1_allstocks.mat
data = dataoutmonthly1_allstocks;
data = data(data(:,31) >= 1 & data(:,31) <= 3 & data(:,32) == 1,:);
clear dataoutmonthly1_allstocks

data(:,[6 7 19 20 21 27 30 31]) = [];
data(:,17:end) = [];

permno0_all = unique(data(:,1));

monthall = [];
for yearall = 1926:2018
    for mmm = 1:12
        monthall = [monthall;yearall*100 + mmm];
    end
end
clear mmm yearall

% Minimum Number of Months to calculate IVOL
cut2 = 4;

for ppp = 1:9
    if ppp == 1
       permno0 = permno0_all(permno0_all >= 10000 & ...
                             permno0_all <= 19999,:);
    elseif ppp == 2
       permno0 = permno0_all(permno0_all >= 20000 & ...
                             permno0_all <= 29999,:);
    elseif ppp == 3
       permno0 = permno0_all(permno0_all >= 30000 & ...
                             permno0_all <= 39999,:);
    elseif ppp == 4
       permno0 = permno0_all(permno0_all >= 40000 & ...
                             permno0_all <= 49999,:);
    elseif ppp == 5
       permno0 = permno0_all(permno0_all >= 50000 & ...
                             permno0_all <= 59999,:);
    elseif ppp == 6
       permno0 = permno0_all(permno0_all >= 60000 & ...
                             permno0_all <= 69999,:);
    elseif ppp == 7
       permno0 = permno0_all(permno0_all >= 70000 & ...
                             permno0_all <= 79999,:);
    elseif ppp == 8
       permno0 = permno0_all(permno0_all >= 80000 & ...
                             permno0_all <= 89999,:);
    elseif ppp == 9
       permno0 = permno0_all(permno0_all >= 90000 & ...
                             permno0_all <= 99999,:);
    end
    
Aout_resid = [];

for i = 1:size(permno0,1)
    
    tmpdata      = data(data(:,1) == permno0(i),:);
    date0        = unique(tmpdata(:,6));
    date0        = date0(date0 >= 196307,:);
    tmpout       = [];
    
    for j = 1:size(date0,1)
        
        ppp0    = find(monthall == date0(j));
        month0  = monthall(ppp0);
        month_1 = monthall(ppp0 - 11);
        month_3 = monthall(ppp0 - 11 - 12*2);
        month_5 = monthall(ppp0 - 11 - 12*4);
        
        tmptmpdata   = tmpdata(tmpdata(:,6) <= month0,:);
        tmptmpdata_1 = tmpdata(tmpdata(:,6) <= month0 & ...
                               tmpdata(:,6) >= month_1 ,:);
        tmptmpdata_3 = tmpdata(tmpdata(:,6) <= month0 & ...
                               tmpdata(:,6) >= month_3 ,:);
        tmptmpdata_5 = tmpdata(tmpdata(:,6) <= month0 & ...
                               tmpdata(:,6) >= month_5 ,:);
        
        nummonth_1 = size(tmptmpdata_1,1);
        nummonth_3 = size(tmptmpdata_3,1);
        nummonth_5 = size(tmptmpdata_5,1);
        nummonth   = size(tmptmpdata,1);
        
        
        for mmm = 1:4
            if mmm == 1
               tmptmpdata1 = tmptmpdata;
            elseif mmm == 2
               tmptmpdata1 = tmptmpdata_1;  
            elseif mmm == 3
               tmptmpdata1 = tmptmpdata_3; 
            elseif mmm == 4
               tmptmpdata1 = tmptmpdata_5;  
            end
            
            if size(tmptmpdata1,1) >= cut2;
               yyy0   = tmptmpdata1(:,4) - tmptmpdata1(:,10) / 100;
               xxx0   = [ones(size(yyy0,1),1) ...
                         tmptmpdata1(:,[7 8 9]) / 100];
               rltff3 = ols1(yyy0,xxx0);
               IVOL0  = std(rltff3.resid);
               if isnan(IVOL0) == 1 IVOL0 = -9.9999;end
            else
               IVOL0  = -9.9999;    
            end
            clear tmptmpdata1 yyy0 xxx0  rltff3
            if mmm == 1
               IVOL   = IVOL0;
            elseif mmm == 2
               IVOL_1 = IVOL0;
            elseif mmm == 3
               IVOL_3 = IVOL0;
            elseif mmm == 4
               IVOL_5 = IVOL0;
            end
            clear IVOL0
        end
        
        
        tmpout = [tmpout;permno0(i) date0(j),...
                  nummonth_1 IVOL_1,...
                  nummonth_3 IVOL_3,...
                  nummonth_5 IVOL_5,...
                  nummonth IVOL];
              
        clear month0 month_1 month_3 month_5
        clear tmptmpdata tmptmpdata_1 tmptmpdata_3 tmptmpdata_5
        clear nummonth_1 IVOL_1
        clear nummonth_3 IVOL_3
        clear nummonth_5 IVOL_5
        clear nummonth IVOL 
    
    end
    
    Aout_resid = [Aout_resid;tmpout];
    clear tmpout
    
end
    if permno0(i) >= 10000 & permno0(i) <= 19999
       save tmpoutresid1_allstocks_otherIVOL_monthly.mat Aout_resid
    elseif permno0(i) >= 20000 & permno0(i) <= 29999
       save tmpoutresid2_allstocks_otherIVOL_monthly.mat Aout_resid
    elseif permno0(i) >= 30000 & permno0(i) <= 39999
       save tmpoutresid3_allstocks_otherIVOL_monthly.mat Aout_resid
    elseif permno0(i) >= 40000 & permno0(i) <= 49999
       save tmpoutresid4_allstocks_otherIVOL_monthly.mat Aout_resid
    elseif permno0(i) >= 50000 & permno0(i) <= 59999
       save tmpoutresid5_allstocks_otherIVOL_monthly.mat Aout_resid
    elseif permno0(i) >= 60000 & permno0(i) <= 69999
       save tmpoutresid6_allstocks_otherIVOL_monthly.mat Aout_resid
    elseif permno0(i) >= 70000 & permno0(i) <= 79999
       save tmpoutresid7_allstocks_otherIVOL_monthly.mat Aout_resid
    elseif permno0(i) >= 80000 & permno0(i) <= 89999
       save tmpoutresid8_allstocks_otherIVOL_monthly.mat Aout_resid
    elseif permno0(i) >= 90000 & permno0(i) <= 99999
       save tmpoutresid9_allstocks_otherIVOL_monthly.mat Aout_resid
    end

end

%}
% Monthly IVOL calculated using monthly return in 1/3/5/all years
%{
No.VarName      Description
1  PERMNO	    Permno Number
2  DATE         YearMonth (yyyymm)
3  NumMonth	    Number of month to calculate IVOL in the previous 1 year
4  IVOL  	    Monthly IVOL calculated using monthly return in the 
                precious 1 year
5  NumMonth	    Number of month to calculate IVOL in the previous 3 year
6  IVOL  	    Monthly IVOL calculated using monthly return in the 
                precious 3 year
7  NumMonth	    Number of month to calculate IVOL in the previous 5 year
8  IVOL  	    Monthly IVOL calculated using monthly return in the 
                precious 5 year
9  NumMonth	    Number of month to calculate IVOL in the previous all year
10 IVOL  	    Monthly IVOL calculated using monthly return in the 
                precious all year
%}

% --------------------------------- BETA ---------------------------------%
% Estimate BETA, calculate past returns, turnover rate for each firm
% Load data
%{

clear
clc

load dataoutmonthly1_allstocks.mat
data1 = dataoutmonthly1_allstocks;
clear dataoutmonthly1_allstocks

% Stocks traded on NYSE, AMEX, or NASDAQ
data1 = data1(data1(:,31) >= 1 & data1(:,31) <= 3 ,:);

%}
%{

permno0  = unique(data1(:,1));
monthall = unique(data1(:,8));
cut5     = 24; % Minimum Number of Months to Calculate Market Beta


for ppp = 1:9
    
    if ppp == 1
       permnoall = permno0(permno0 > 10000 & permno0 <= 19999,:);
    elseif ppp == 2
       permnoall = permno0(permno0 > 20000 & permno0 <= 29999,:);
    elseif ppp == 3
       permnoall = permno0(permno0 > 30000 & permno0 <= 39999,:);
    elseif ppp == 4
       permnoall = permno0(permno0 > 40000 & permno0 <= 49999,:);
    elseif ppp == 5
       permnoall = permno0(permno0 > 50000 & permno0 <= 59999,:);
    elseif ppp == 6
       permnoall = permno0(permno0 > 60000 & permno0 <= 69999,:);
    elseif ppp == 7
       permnoall = permno0(permno0 > 70000 & permno0 <= 79999,:);
    elseif ppp == 8
       permnoall = permno0(permno0 > 80000 & permno0 <= 89999,:);
    elseif ppp == 9
       permnoall = permno0(permno0 > 90000 & permno0 <= 99999,:);
    end
    
     A_out  = [];
     pstart = 1;
    

for i = pstart:size(permnoall,1)

    tmpdata = data1(data1(:,1) == permnoall(i),:);
    month0  = unique(tmpdata(:,8));
    month0  = month0(month0 >= 196307,:);
    tmpout  =[];
    
    
    if isempty(month0) == 0
    
    % Calculate Factor Adjusted Trading Volume in month (t)
    tmpdata(tmpdata(:,3) ~= -9.9999 & tmpdata(:,7) ~= -9.9999,31) =...
    tmpdata(tmpdata(:,3) ~= -9.9999 & tmpdata(:,7) ~= -9.9999,3).*...
    tmpdata(tmpdata(:,3) ~= -9.9999 & tmpdata(:,7) ~= -9.9999,7);
    tmpdata(tmpdata(:,3) == -9.9999 | tmpdata(:,7) == -9.9999,31) =...
    -9.9999;

    % Calculate Factor Adjusted Number of Shares Outstanding in month (t-2)
    tmpdata(tmpdata(:,29) ~= -9.9999 & tmpdata(:,30) ~= -9.9999,32) =...
    tmpdata(tmpdata(:,29) ~= -9.9999 & tmpdata(:,30) ~= -9.9999,29).*...
    tmpdata(tmpdata(:,29) ~= -9.9999 & tmpdata(:,30) ~= -9.9999,30);
    tmpdata(tmpdata(:,29) == -9.9999 | tmpdata(:,30) == -9.9999,32) =...
    -9.9999;

    % Calculate Share Turnover as in Chordia et al. (2001)
    % Share turnover is measured by the number of shares traded divided by 
    % the number of shares outstanding in the second to last month
    tmpdata(tmpdata(:,31) ~= -9.9999 & tmpdata(:,32) ~= -9.9999 & ...
            tmpdata(:,31) > 0 & tmpdata(:,32) > 0,33)...
          =(tmpdata(tmpdata(:,31) ~= -9.9999 & ...
            tmpdata(:,32) ~= -9.9999 & tmpdata(:,31) > 0 & ...
            tmpdata(:,32)>0,31)*100)./...
           (tmpdata(tmpdata(:,31) ~= -9.9999 & ...
            tmpdata(:,32) ~= -9.9999 & tmpdata(:,31) > 0 & ...
            tmpdata(:,32)>0,32)*1000);
    tmpdata(tmpdata(:,31) == -9.9999 | tmpdata(:,32) == -9.9999 | ...
            tmpdata(:,31) == 0 | tmpdata(:,32) == 0,33)=-9.9999;
        
        
    for j = 1:size(month0,1)
        
        % Find data 60 months before month(t)
        p0 = find(monthall == month0(j));
        if p0 > 60
           month_60 = monthall(p0 - 60);
           month_1  = monthall(p0 - 1);
        elseif p0 > 1 & p0 <= 60
           month_60 = monthall(1);
           month_1  = monthall(p0 - 1);
        else
           month_60 = 192601;
           month_1  = 192601;
        end
        
        tmptmpdata = tmpdata(tmpdata(:,8) <= month_1 & ...
                     tmpdata(:,8) >= month_60,:);
        
        if size(tmptmpdata,1) >= cut5
           yyy0 = tmptmpdata(:,4);
           xxx0 = [ones(size(yyy0,1),1) tmptmpdata(:,20)];
           rlt0 = ols1(yyy0,xxx0);
           tmpbeta = rlt0.beta(2);
           tmpbeta_nummonth = size(tmptmpdata,1);
           clear yyy0 xxx0 rlt0
        else
           tmpbeta = -9.9999;
           tmpbeta_nummonth = size(tmptmpdata,1);
        end
        clear month_60 month_1 tmptmpdata
        
        % Find data 7 months to 2 months before month(t)
        if p0 > 7
           month_7 = monthall(p0 - 7);
           month_2 = monthall(p0 - 2);
        elseif p0 > 2 & p0 <= 7
           month_7 = monthall(1);
           month_2 = monthall(p0 - 2);
        else
           month_7 = 192601;
           month_2 = 192601;
        end
        tmptmpdata = tmpdata(tmpdata(:,8) <= month_2 & ...
                     tmpdata(:,8) >= month_7,:);
                 
        if size(tmptmpdata,1) == 6
           tmpret_2_7 = prod(tmptmpdata(:,4) + 1) - 1;
        else
           tmpret_2_7 = -9.9999;
        end
        clear tmptmpdata month_2 month_7
        
        % Calculate TURN and CVTURN
        if p0 > 36
           month_36 = monthall(p0 - 36);
           month_1  = monthall(p0 - 1);
        elseif p0 > 1 & p0 <= 36
           month_36 = monthall(1);
           month_1  = monthall(p0 - 1);
        else
           month_36 = 192601;
           month_1  = 192601;
        end
        tmptmpdata = tmpdata(tmpdata(:,8) <= month_1 & ...
                     tmpdata(:,8) >= month_36 & ...
                     tmpdata(:,33) ~= -9.9999,:); 
        % Number of months for calculation of TURN and CVTURN
        sizeTURN   = size(tmptmpdata,1);
        if size(tmptmpdata,1) >= 1
           tmpmeanTURN = mean(tmptmpdata(:,33));
        else
           tmpmeanTURN = -9.9999;
        end
        if size(tmptmpdata,1) >= 2
           tmpstdTURN  = std(tmptmpdata(:,33));
           if tmpstdTURN > 0
              tmpCVTURN = tmpstdTURN / mean(tmptmpdata(:,33));
           else
              tmpCVTURN = -9.9999; 
           end
        else
           tmpCVTURN = -9.9999;
        end
        clear month_1 month_36 tmptmpdata

        tmpret     = tmpdata(tmpdata(:,8) == month0(j),4);
        tmpretx    = tmpdata(tmpdata(:,8) == month0(j),5);
        tmpcapdec  = tmpdata(tmpdata(:,8) == month0(j),22);
        tmpcapjune = tmpdata(tmpdata(:,8) == month0(j),23);
        tmpbe      = tmpdata(tmpdata(:,8) == month0(j),24);
        
        tmpout=[tmpout;permnoall(i) month0(j) ...
                       tmpbeta tmpbeta_nummonth ...
                       tmpret_2_7 tmpmeanTURN tmpCVTURN ...
                       tmpret tmpretx tmpcapdec tmpcapjune tmpbe ...
                       sizeTURN];
                   
        clear tmpbeta tmpret_2_7 p0
        clear tmpmeanTURN tmpCVTURN tmpstdTURN
        clear tmpret tmpretx tmpcapdec tmpcapjune tmpbe sizeTURN
        
    end
    
    A_out = [A_out;tmpout];

    clear tmpdata tmpout tmpdata_IVOL
    end
end

% Save data
if ppp == 1
   save tmp_BETA_TURN_pastR_allstocks_1963_2017_1.mat A_out
elseif ppp == 2
   save tmp_BETA_TURN_pastR_allstocks_1963_2017_2.mat A_out
elseif ppp == 3
   save tmp_BETA_TURN_pastR_allstocks_1963_2017_3.mat A_out
elseif ppp == 4
   save tmp_BETA_TURN_pastR_allstocks_1963_2017_4.mat A_out
elseif ppp == 5
   save tmp_BETA_TURN_pastR_allstocks_1963_2017_5.mat A_out
elseif ppp == 6
   save tmp_BETA_TURN_pastR_allstocks_1963_2017_6.mat A_out
elseif ppp == 7
   save tmp_BETA_TURN_pastR_allstocks_1963_2017_7.mat A_out
elseif ppp == 8
   save tmp_BETA_TURN_pastR_allstocks_1963_2017_8.mat A_out
elseif ppp == 9
   save tmp_BETA_TURN_pastR_allstocks_1963_2017_9.mat A_out
end

end

%}
% Description of Data on BETA, Lagged RET, TURN, SIZE 
%{
No.VarName      Description
1  PERMNO       Permno Number 
2  Date         YearMonth (yyyymm) (t)
3  MarketBeta   Beta estimated using individual stock returns and market
                returns in the last 60 months 
4  MarketBeta   Number of month to calculate Beta
5  RET_2_7      Compound Return for month (t-2) to month (t-7)
6  TURN         Average Turnover Rate in the last 36 months
7  CVTURN       CV of Turnover Rate in the last 36 months
8  RET          RET (t)
9  RETX         RETX(t)
10 ME_dec       Market Capitalization at the end of December in last year 
                (in $thousands)
11 ME_jun       Market Capitalization at the end of June
                as in Fama & French (1992) (in $thousands)
12 BE           Book Value of Stockholders Equity 
                as in Fama & French (1992) (in $millions)
13 sizeTURN     Number of Month to calculate TURN
%}

% Calcualte Portfolio Beta as in Fama & French (1992)
% Sort stocks by size and market beta
%{

clear
clc

% load pre-raking betas of individual stocks
data = [];
for num0 = 1:9
load(['tmp_BETA_TURN_pastR_allstocks_1963_2017_' num2str(num0) '.mat'])
data = [data;A_out];
clear A_out
end
clear num0
dataBETA = data;
clear data
dataBETA = dataBETA(dataBETA(:,2)<=201712,:);

% load monthly stock data
load dataoutmonthly1_allstocks.mat
data = dataoutmonthly1_allstocks;
clear  dataoutmonthly1_allstocks

% load monthly CRSP market portfolio data
load datamarket1.txt


monthall = unique(dataBETA(:,2));

% Data of mean portfolio returns
A_out_100portfolioret = [];
% Data of stocks included in a portfolio
A_out_portfolio       = [];


for j= 2:size(monthall,1)
    
    tmpBETA = dataBETA(dataBETA(:,2) == monthall(j),:);
    
    tmpdata = data(data(:,8) == monthall(j - 1),:);
    tmpdatanext = data(data(:,8) == monthall(j),:);
    
    permno1 = unique(tmpBETA(:,1));
    permno2 = unique(tmpdata(:,1));
    permno0 = intersect(permno1,permno2);
    clear permno1 permno2
    
    tmpmarketret = datamarket1(datamarket1(:,3) == monthall(j),[2 4]);
    
    tmpall = [];
    
    for i = 1:size(permno0,1)
        % Find individual market beta
        tmptmpBETA = tmpBETA(tmpBETA(:,1) == permno0(i),:);
        if isempty(tmptmpBETA) == 0
           tmptmpBETA = tmptmpBETA(3);
        else tmptmpBETA = -9.9999;
        end
        % Find market capitalization as of June
        tmptmpsize = tmpdata(tmpdata(:,1) == permno0(i),:);
        if isempty(tmptmpsize) == 0
           tmptmpsize = tmptmpsize(1,23);
        else
           tmptmpsize = -9.9999;
        end
        % Find market capitalization as of month(t)
        tmptmpsize1 = tmpdata(tmpdata(:,1) == permno0(i) & ...
                      tmpdata(:,2) ~= -9.9999 & tmpdata(:,6) ~= -9.9999,:);
        if isempty(tmptmpsize1) == 0
           tmptmpsize1 = tmptmpsize1(1,6)*tmptmpsize1(1,2);
        else
           tmptmpsize1 = -9.9999;
        end
        
        tmptmpretnext = tmpdatanext(tmpdatanext(:,1) == permno0(i),:);
        if isempty(tmptmpretnext) == 0
           tmptmpretnext = tmptmpretnext(1,4);
        else
           tmptmpretnext = -9.9999;
        end

        tmpall=[tmpall;permno0(i) monthall(j)...
                tmptmpBETA tmptmpsize tmptmpsize1 tmptmpretnext];
        clear tmptmpBETA tmptmpsize  tmptmpsize1 tmptmpretnext
        
    end
    
    % Sort stocks by market cap at the end of june of last year and beta
    % Eliminate stocks with missing beta or missing market cap
    tmpall1 = tmpall(tmpall(:,3) ~= -9.9999 & tmpall(:,4) ~= -9.9999,:);
    if size(tmpall1,1) >= 500
        % Sort stocks by size
       tmpall1 = sortrows(tmpall1,4);
       interval_1 = floor(size(tmpall1,1) / 10);
       
       for gsize = 1:10
           if gsize == 1
              pstart = 1;pend = interval_1;
           elseif gsize == 2
              pstart = interval_1*1 + 1;pend = interval_1*2;
           elseif gsize == 3
              pstart = interval_1*2 + 1;pend = interval_1*3;
           elseif gsize == 4
              pstart = interval_1*3 + 1;pend = interval_1*4;
           elseif gsize == 5
              pstart = interval_1*4 + 1;pend = interval_1*5;
           elseif gsize == 6
              pstart = interval_1*5 + 1;pend = interval_1*6;
           elseif gsize == 7
              pstart = interval_1*6 + 1;pend = interval_1*7;
           elseif gsize == 8
              pstart = interval_1*7 + 1;pend = interval_1*8;
           elseif gsize == 9
              pstart = interval_1*8 + 1;pend = interval_1*9;
           elseif gsize == 10
              pstart = interval_1*9 + 1;pend = size(tmpall1,1);              
           end
           
           tmpall2 = tmpall1(pstart:pend,:);
           % Sort stocks by market beta
           tmpall2 = sortrows(tmpall2,3);
           interval_2 = floor(size(tmpall2,1) / 10);
           clear pstart pend
           
           for gbeta = 1:10
              if gbeta == 1
                 pstart = 1;pend = interval_2;
              elseif gbeta == 2
                 pstart = interval_2*1 + 1;pend = interval_2*2;
              elseif gbeta == 3
                 pstart = interval_2*2 + 1;pend = interval_2*3;
              elseif gbeta == 4
                 pstart = interval_2*3 + 1;pend = interval_2*4;
              elseif gbeta == 5
                 pstart = interval_2*4 + 1;pend = interval_2*5;
              elseif gbeta == 6
                 pstart = interval_2*5 + 1;pend = interval_2*6;
              elseif gbeta == 7
                 pstart = interval_2*6 + 1;pend = interval_2*7;
              elseif gbeta == 8
                 pstart = interval_2*7 + 1;pend = interval_2*8;
              elseif gbeta == 9
                 pstart = interval_2*8 + 1;pend = interval_2*9;
              elseif gbeta == 10
                 pstart = interval_2*9 + 1;pend = size(tmpall2,1);              
              end
                     
           tmpall3 = tmpall2(pstart:pend,:);
           tmpall3 = tmpall3(tmpall3(:,6) ~= -9.9999,:);
           clear pstart pend 
           % Equal?weighted monthly returns for a portfolio
           tmpmeanret =mean(tmpall3(:,6));
           A_out_100portfolioret = [A_out_100portfolioret;...
                                    monthall(j) monthall(j - 1)...
                                    gsize gbeta...
                                    mean(tmpall3(:,6)) tmpmarketret];
           A_out_portfolio       = [A_out_portfolio;tmpall3(:,1)...
                                    monthall(j - 1)*...
                                    ones(size(tmpall3,1),1)...
                                    gsize*ones(size(tmpall3,1),1)...
                                    gbeta*ones(size(tmpall3,1),1)];
           clear tmpall3 tmpmeanret
           end
           clear tmpall2
       end
    end
    clear tmpall1
    
    clear tmpall1 

    clear tmpall tmpall1 interval_1 interval_2 
    clear tmpBETA tmpdata tmpdatanext tmpmarketret gbeta gsize permno0
end

% Save data
save tmp_portfolio_beta_allstocks_1963_2017.mat...
     A_out_100portfolioret A_out_portfolio 

%}
% 100 portfolio sorted by size and beta Data Description
%{
A_out_100portfolioret
No.VarName      Description
1  YearMonth	Yearmonth(t) (yyyymm)
2  YearMonth	Yearmonth(t-1) (yyyymm)
3  GroupNo      Size Group Number
4  GrouoNo      Market Beta Group Bumber
5  MeanRET      Equal weighited Portfolio Return (t)
6  VRET         CRSP market portfolio value weighted return (t)
7  VRET         CRSP market portfolio value weighted return (t-1)

A_out_portfolio 
No.VarName      Description
1  Permno   	Permno Number
2  YearMonth	Yearmonth(t-1) (yyyymm)
3  GroupNo      Size Group Number
4  GrouoNo      Market Beta Group Bumber

%}
% Calculate Full Sample Post Ranking Market Beta for 100 Portfolios
% Sample Period July 1963 - December 2006
%{
clear
clc

load tmp_portfolio_beta.mat A_out_100portfolioret A_out_portfolio

A_out_100portfoliobeta = [];

for gsize = 1:10
    for gbeta = 1:10
        tmpreg = A_out_100portfolioret...
                (A_out_100portfolioret(:,3) == gsize...
               & A_out_100portfolioret(:,4) == gbeta,:);
        tmpreg = tmpreg(tmpreg(:,2) >= 196307 & tmpreg(:,2) <= 200612,:);
        
        % Dependent Variable: Portfolio RET (t)
        yyy0   = tmpreg(:,5);
        % Independent Variable: Market RET (t) Market RET(t-1)
        xxx0   = [ones(size(yyy0,1),1) tmpreg(:,[6 7])];
        
        rlt0   = ols1(yyy0,xxx0);
        
        A_out_100portfoliobeta = [A_out_100portfoliobeta;...
                                  gsize gbeta rlt0.beta(2) + rlt0.beta(3)];
        clear tmpreg yyy0 xxx0 rlt0
    end
end
save tmp_portfolio_beta_1963_2006.mat...
     A_out_100portfolioret A_out_portfolio A_out_100portfoliobeta

%}
% Sample Period January 2007 - December 2017
%{
clear
clc

load tmp_portfolio_beta.mat A_out_100portfolioret A_out_portfolio

A_out_100portfoliobeta = [];

for gsize = 1:10
    for gbeta = 1:10
        tmpreg = A_out_100portfolioret...
                (A_out_100portfolioret(:,3) == gsize...
               & A_out_100portfolioret(:,4) == gbeta,:);
        tmpreg = tmpreg(tmpreg(:,2) >= 200701 & tmpreg(:,2) <= 201712,:);
        
        % Dependent Variable: Portfolio RET (t)
        yyy0   = tmpreg(:,5);
        % Independent Variable: Market RET (t) Market RET(t-1)
        xxx0   = [ones(size(yyy0,1),1) tmpreg(:,[6 7])];
        
        rlt0   = ols1(yyy0,xxx0);
        
        A_out_100portfoliobeta = [A_out_100portfoliobeta;...
                                  gsize gbeta rlt0.beta(2) + rlt0.beta(3)];
        clear tmpreg yyy0 xxx0 rlt0
    end
end
save tmp_portfolio_beta_2007_2017.mat...
     A_out_100portfolioret A_out_portfolio A_out_100portfoliobeta

%}
% Sample Period July 1963 - December 2017
%{
clear
clc

load tmp_portfolio_beta.mat A_out_100portfolioret A_out_portfolio

A_out_100portfoliobeta = [];

for gsize = 1:10
    for gbeta = 1:10
        tmpreg = A_out_100portfolioret...
                (A_out_100portfolioret(:,3) == gsize...
               & A_out_100portfolioret(:,4) == gbeta,:);
        tmpreg = tmpreg(tmpreg(:,2) >= 196307 & tmpreg(:,2) <= 201712,:);
        
        % Dependent Variable: Portfolio RET (t)
        yyy0   = tmpreg(:,5);
        % Independent Variable: Market RET (t) Market RET(t-1)
        xxx0   = [ones(size(yyy0,1),1) tmpreg(:,[6 7])];
        
        rlt0   = ols1(yyy0,xxx0);
        
        A_out_100portfoliobeta = [A_out_100portfoliobeta;...
                                  gsize gbeta rlt0.beta(2) + rlt0.beta(3)];
        clear tmpreg yyy0 xxx0 rlt0
    end
end
save tmp_portfolio_beta_1963_2017.mat...
     A_out_100portfolioret A_out_portfolio A_out_100portfoliobeta

%}
% Sample Period July 1926 - June 1963
%{
clear
clc

load tmp_portfolio_beta.mat A_out_100portfolioret A_out_portfolio

A_out_100portfoliobeta = [];

for gsize = 1:10
    for gbeta = 1:10
        tmpreg = A_out_100portfolioret...
                (A_out_100portfolioret(:,3) == gsize...
               & A_out_100portfolioret(:,4) == gbeta,:);
        tmpreg = tmpreg(tmpreg(:,2) >= 192607 & tmpreg(:,2) <= 196306,:);
        
        % Dependent Variable: Portfolio RET (t)
        yyy0   = tmpreg(:,5);
        % Independent Variable: Market RET (t) Market RET(t-1)
        xxx0   = [ones(size(yyy0,1),1) tmpreg(:,[6 7])];
        
        rlt0   = ols1(yyy0,xxx0);
        
        A_out_100portfoliobeta = [A_out_100portfoliobeta;...
                                  gsize gbeta rlt0.beta(2) + rlt0.beta(3)];
        clear tmpreg yyy0 xxx0 rlt0
    end
end
save tmp_portfolio_beta_1926_1963.mat...
     A_out_100portfolioret A_out_portfolio A_out_100portfoliobeta

%}

% ---------------------------------EIVOL ---------------------------------%
% Estimate EGARCH E(IVOL), All available information
%{
clear
clc

% Starting month for E(IVOL)
StartMonth = 196307;

load dataoutmonthly1_allstocks.mat
data = dataoutmonthly1_allstocks;
data = data(data(:,31) >= 1 & data(:,31) <= 3 &...
            data(:,32) == 1,:);

data(:,[6 7 19 20 21 27 30 31 32])=[];

ON_method3 = 1;
% whether to save estimated EVIOL
% 1 = save; 0 = not save
IndSave    = 0;
 
clear lastwarn
warning('Off');

permno0_all = unique(data(:,1));

cut2 = 4; % Minimum number of months

% Warning statement when covariance matrix is not a number
warn1 = ['Error in calculation of parameter covariance matrix.'...
         ' Matrix of NaN''s returned.'];

     
monthallall0 = [];
for yyy = 1990:2020
    for mmm = 1:12
        monthallall0 = [monthallall0;yyy*100 + mmm];
    end
end


for ppp = 1

    if ppp == 1
       permno0 = permno0_all(permno0_all >= 10000 &...
                             permno0_all <= 19999,:);
    elseif ppp == 2
       permno0 = permno0_all(permno0_all >= 20000 &...
                             permno0_all <=29999,:);
    elseif ppp == 3
       permno0 = permno0_all(permno0_all >= 30000 &...
                             permno0_all <= 39999,:);
    elseif ppp == 4
       permno0 = permno0_all(permno0_all >= 40000 &...
                             permno0_all <= 49999,:);
    elseif ppp == 5
       permno0 = permno0_all(permno0_all >= 50000 &...
                             permno0_all <= 59999,:);
    elseif ppp == 6
       permno0 = permno0_all(permno0_all >= 60000 &...
                             permno0_all <= 69999,:);
    elseif ppp == 7
       permno0 = permno0_all(permno0_all >= 70000 &...
                             permno0_all <= 79999,:);
    elseif ppp == 8
       permno0 = permno0_all(permno0_all >= 80000 &...
                             permno0_all <= 89999,:);
    elseif ppp == 9
       permno0 = permno0_all(permno0_all >= 90000 &...
                             permno0_all <= 99999,:);
    end
    
    
    Aout_EstIdioV_method3_allstocks     = [];
    Aout_EstIdioV_method3_allstocks_sqp = [];
    
    pstart=1;
    
    
for i = pstart:size(permno0,1)
    
    tmp_full_ff3_method3_allstocks     = [];
    tmp_full_ff3_method3_allstocks_sqp = [];
    
    tmpdata      = data(data(:,1) == permno0(i),:);
    dateall0     = unique(tmpdata(:,6));
    
    for Estimation = 1
    % Eliminate missing monthly return or missing factor
    tmpdata1     = tmpdata(tmpdata(:,4) ~= -9.9999 &...
                   tmpdata(:,7) ~= -9.9999,:);
    date0        = unique(tmpdata1(:,6));
    if size(tmpdata1,1) >= cut2
       % Number of month >= Minimum number of months
       
       for method=3
       if ON_method3 == 1
       if size(tmpdata1,1) >= cut2;
          date1 = date0;
          date1 = date1(date1 >= StartMonth,:);
          for j = 1:size(date1,1)
              [i j]
              % Find data before month (t)
              tmptmpdata1 = tmpdata1(tmpdata1(:,6)<date1(j),:);
              
              yyy0   = tmptmpdata1(:,4) - tmptmpdata1(:,10) / 100;
              xxx0   = [ones(size(yyy0,1),1) tmptmpdata1(:,[7 8 9]) / 100];
              rltff3 = ols1(yyy0,xxx0);
              clear yyy0 xxx0
              tmptmpff3 = [rltff3.resid];
              clear rltff3 yyy0 xxx0 tmptmpdata1

              tmptmp_full_ff3     = [];
              tmptmp_full_ff3_sqp = [];
                  
              a = tmptmpff3(:,1);
                  
              for modelall = 1:9
                  % Algorithm = 'interior-point'
                  for mmm = 1
                          
                      lastwarn('Default');
                      
                      if modelall == 1
                         p = 1;q = 1;
                      elseif modelall == 2
                         p = 1;q = 2;
                      elseif modelall == 3
                         p = 1;q = 3;
                      elseif modelall == 4;
                         p = 2;q = 1;
                      elseif modelall == 5;
                         p = 2;q = 2;
                      elseif modelall == 6;
                         p = 2;q = 3;
                      elseif modelall == 7;
                         p = 3;q = 1;
                      elseif modelall == 8;
                         p = 3;q = 2;
                      elseif modelall == 9;
                         p = 3;q = 3;
                      end
                      
                      % Number of unknown parameters for EGARCH model                   
                      numvar0 = 1 + 1*p + 1*q + 1*q;
                      T       = length(a);
                      Mdl     = egarch(p,q);

                        
                      opts             = optimset('fmincon');
                      opts.Algorithm   = 'interior-point';
                      opts.Maxiter     = 1000;
                      opts.tolfun      = 10^(-6);
                      opts.tolx        = 10^(-6);
                      opts.MaxFunEvals = 1000;

                      try
                      [EstMdl,EstParamCov,logL,exitflag] = ...
                       estimate(Mdl,a,'options',opts,'Display','Off');
                      catch      
                      % If there is warning 'Estimated GARCH model is
                      % invalid'
                       warning('Estimated GARCH model is invalid');
                       indwarning = 1;
                       tmptmp_full_ff3 = [tmptmp_full_ff3 ...
                                          ones(1,numvar0) * (-9.9999) ...
                                          -9.9999 -9.9999 -9.9999 ...
                                          -9.9999 indwarning -9.9999 1111]; 
                     
                       if modelall == 1
                          V2_m1 = -9.9999;
                       elseif modelall == 2
                          V2_m2 = -9.9999;
                       elseif modelall == 3
                          V2_m3 = -9.9999;                            
                       elseif modelall == 4
                          V2_m4 = -9.9999;
                       elseif modelall == 5
                          V2_m5 = -9.9999;
                       elseif modelall == 6
                          V2_m6 = -9.9999;
                       elseif modelall == 7
                          V2_m7 = -9.9999;
                       elseif modelall == 8
                          V2_m8 = -9.9999;
                       elseif modelall == 9
                          V2_m9 = -9.9999;
                       end
                         
                       continue;
                      end
                      
                      % If no warning 'Estimated GARCH model is valid'                     
                      if isnan(EstMdl.Constant) + ...
                         sum(isnan(cell2mat(EstMdl.GARCH))) + ...
                         sum(isnan(cell2mat(EstMdl.ARCH))) + ...
                         sum(isnan(cell2mat(EstMdl.Leverage))) > 0
                         % If at least one of estimated parameters is not
                         % a number      
                         indwarning = 2;
                         tmptmp_full_ff3 = [tmptmp_full_ff3 ...
                                            ones(1,numvar0) * (-9.9999) ...
                                            -9.9999 -9.9999 -9.9999 -9.9999
                                            indwarning -9.9999 1111]; 
                         
                         if modelall == 1
                            V2_m1 = -9.9999;
                         elseif modelall == 2
                            V2_m2 = -9.9999;
                         elseif modelall == 3
                            V2_m3 = -9.9999;                            
                         elseif modelall == 4
                            V2_m4 = -9.9999;
                         elseif modelall == 5
                            V2_m5 = -9.9999;
                         elseif modelall == 6
                            V2_m6 = -9.9999;
                         elseif modelall == 7
                            V2_m7 = -9.9999;
                         elseif modelall == 8
                            V2_m8 = -9.9999;
                         elseif modelall == 9
                            V2_m9 = -9.9999;
                         end
                         
                      % If all estimated parameters are valid number
                      elseif isnan(EstMdl.Constant) + ...
                             sum(isnan(cell2mat(EstMdl.GARCH))) + ...
                             sum(isnan(cell2mat(EstMdl.ARCH))) + ...
                             sum(isnan(cell2mat(EstMdl.Leverage))) == 0
                 
                             Constant0 = EstMdl.Constant;
                             GARCH0    = cell2mat(EstMdl.GARCH);
                             ARCH0     = cell2mat(EstMdl.ARCH);
                             Leverage0 = cell2mat(EstMdl.Leverage);
                             
                             Mdl0 = egarch('Constant',Constant0,...
                                           'GARCH',GARCH0,...
                                           'ARCH',ARCH0,...
                                           'Leverage',Leverage0);
                                       
                             % In-Sample EIVOL(t-1)
                             % Expected Idiosyncratic Variance of 
                             % month (t-1) estimated using information up
                             % to month (t-1)
                             V2 = infer(Mdl0,a);
                             V2 = V2(end);
                             if isnan(V2) == 1 V2 = -9.9999; end
                             if isinf(V2) == 1 V2 = -9.9999; end
                     
                             if modelall == 1
                                V2_m1 = V2;
                             elseif modelall == 2
                                V2_m2 = V2;
                             elseif modelall == 3
                                V2_m3 = V2;                      
                             elseif modelall == 4
                                V2_m4 = V2;
                             elseif modelall == 5
                                V2_m5 = V2;
                             elseif modelall == 6
                                V2_m6 = V2;
                             elseif modelall == 7
                                V2_m7 = V2;
                             elseif modelall == 8
                                V2_m8 = V2;
                             elseif modelall == 9 
                                V2_m9 = V2;
                             end
                     
                             clear Constant0 GARCH0 ARCH0 Leverage0 Mdl0
                             
                             % If number of estimated parameters is smaller
                             % than the number of parameters of the EGARCH
                             % model
                             
                             if length(EstMdl.Constant) + ...
                                length(EstMdl.GARCH) + ...
                                length(EstMdl.ARCH) + ...
                                length(EstMdl.Leverage) ~= numvar0;
                                                                      
                                [msgstr, msgid] = lastwarn;
                                % If estimated covariance matrix is not a
                                % number
                                if length(msgstr)>=50 & ...
                                    sum(msgstr(1:50) == warn1(1:50)) == 50;
                                   indwarning = 3;
                                   aic        = -9.9999;
                                   bic        = -9.9999;
                                   logL       = -9.9999;
                                else
                                % If estimated covariance matrix is a
                                % number
                                   indwarning = 4;
                                   numParams  = sum(any(EstParamCov));
                                   [aic,bic]  = aicbic(logL,numParams,T);
                                end

                                M_Constant = EstMdl.Constant;
                                M_GARCH    = zeros(1,p);
                                if ~isempty(cell2mat(EstMdl.GARCH))
                                for pp = 1:length(cell2mat(EstMdl.GARCH))
                                    tmp = cell2mat(EstMdl.GARCH);
                                    M_GARCH(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                M_ARCH     = zeros(1,q);
                                if ~isempty(cell2mat(EstMdl.ARCH))
                                for pp = 1:length(cell2mat(EstMdl.ARCH))
                                    tmp = cell2mat(EstMdl.ARCH);
                                    M_ARCH(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                M_Leverage = zeros(1,q);
                                if ~isempty(cell2mat(EstMdl.Leverage))
                                for pp =...
                                    1:length(cell2mat(EstMdl.Leverage))
                                    tmp=cell2mat(EstMdl.Leverage);
                                    M_Leverage(pp)=tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                % Out-of-Sample EIVOL(t)
                                % Expected Idiosyncratic Variance of 
                                % month (t) estimated using information up
                                % to month (t-1)
                                V = forecast(EstMdl,1,'Y0',a);
                                if isnan(V) == 1 V = -9.9999;end
                                if isinf(V) == 1 V = -9.9999;end

                                tmptmp_full_ff3 = [tmptmp_full_ff3 ...
                                M_Constant M_GARCH M_ARCH M_Leverage ...
                                V logL aic bic indwarning ...
                                exitflag.exitflag 1111];
                            
                                clear EstMdl indwarning logL aic bic
                                clear exitflag V numParams T
                                clear EstParamCov Mdl msgid msgstr numvar0 
                                clear M_GARCH M_ARCH M_Leverage M_Constant
                                
                             % If number of estimated parameters is equal
                             % to the number of parameters of the EGARCH
                             % model
                             elseif length(EstMdl.Constant) + ...
                                    length(EstMdl.GARCH) + ...
                                    length(EstMdl.ARCH) + ...
                                    length(EstMdl.Leverage) == numvar0;
                                
                                    V = forecast(EstMdl,1,'Y0',a);
                                    if isnan(V) == 1 V = -9.9999;end
                                    if isinf(V) == 1 V = -9.9999;end
                                   
                                   [msgstr, msgid] = lastwarn;
                                   % If estimated covariance matrix is not
                                   % a number
                                   if length(msgstr) >= 50 & ...
                                      sum(msgstr(1:50)==warn1(1:50)) == 50;
                                      indwarning = 5;
                                      aic        = -9.9999;
                                      bic        = -9.9999;
                                      logL       = -9.9999;
                                   % If estimated covariance matrix is a
                                   % number
                                   else
                                       indwarning = 0;
                                       numParams  = sum(any(EstParamCov));
                                       [aic,bic]  = ...
                                        aicbic(logL,numParams,T);
                                   end
                                   tmptmp_full_ff3 = [tmptmp_full_ff3 ...
                                   EstMdl.Constant ...
                                   cell2mat(EstMdl.GARCH) ...
                                   cell2mat(EstMdl.ARCH) ...
                                   cell2mat(EstMdl.Leverage) ...
                                   V logL aic bic indwarning ...
                                   exitflag.exitflag 1111];
                                end
                                clear EstMdl indwarning logL aic bic 
                                clear exitflag V EstParamCov Mdl msgid 
                                clear msgstr numvar0 numParams T
                                clear M_GARCH M_ARCH M_Leverage M_Constant
                      end
                                   
                  end  
                  % Algorithm = 'sqp'
                  for mmm = 2
                          
                      lastwarn('Default');
                      
                      if modelall == 1
                         p = 1;q = 1;
                      elseif modelall == 2
                         p = 1;q = 2;
                      elseif modelall == 3
                         p = 1;q = 3;
                      elseif modelall == 4;
                         p = 2;q = 1;
                      elseif modelall == 5;
                         p = 2;q = 2;
                      elseif modelall == 6;
                         p = 2;q = 3;
                      elseif modelall == 7;
                         p = 3;q = 1;
                      elseif modelall == 8;
                         p = 3;q = 2;
                      elseif modelall == 9;
                         p = 3;q = 3;
                      end
                      
                      % Number of unknown parameters for EGARCH model                   
                      numvar0 = 1 + 1*p + 1*q + 1*q;
                      T       = length(a);
                      Mdl     = egarch(p,q);
                         
                      opts             = optimset('fmincon');
                      opts.Algorithm   = 'sqp';
                      opts.Maxiter     = 1000;
                      opts.tolfun      = 10^(-6);
                      opts.tolx        = 10^(-6);
                      opts.MaxFunEvals = 1000;

                      try
                      [EstMdl,EstParamCov,logL,exitflag] = ...
                       estimate(Mdl,a,'options',opts,'Display','Off');
                      catch      
                      % If there is warning 'Estimated GARCH model is
                      % invalid'
                       warning('Estimated GARCH model is invalid');
                       indwarning = 1;
                       tmptmp_full_ff3_sqp = [tmptmp_full_ff3_sqp ...
                                          ones(1,numvar0) * (-9.9999) ...
                                          -9.9999 -9.9999 -9.9999 ...
                                          -9.9999 indwarning -9.9999 1111]; 
                     
                       if modelall == 1
                          V2_m1_sqp = -9.9999;
                       elseif modelall == 2
                          V2_m2_sqp = -9.9999;
                       elseif modelall == 3
                          V2_m3_sqp = -9.9999;                            
                       elseif modelall == 4
                          V2_m4_sqp = -9.9999;
                       elseif modelall == 5
                          V2_m5_sqp = -9.9999;
                       elseif modelall == 6
                          V2_m6_sqp = -9.9999;
                       elseif modelall == 7
                          V2_m7_sqp = -9.9999;
                       elseif modelall == 8
                          V2_m8_sqp = -9.9999;
                       elseif modelall == 9
                          V2_m9_sqp = -9.9999;
                       end
                         
                       continue;
                      end
                      
                      % If no warning 'Estimated GARCH model is valid'                     
                      if isnan(EstMdl.Constant) + ...
                         sum(isnan(cell2mat(EstMdl.GARCH))) + ...
                         sum(isnan(cell2mat(EstMdl.ARCH))) + ...
                         sum(isnan(cell2mat(EstMdl.Leverage))) > 0
                         % If at least one of estimated parameters is not
                         % a number      
                         indwarning = 2;
                         tmptmp_full_ff3_sqp = [tmptmp_full_ff3_sqp ...
                                            ones(1,numvar0) * (-9.9999) ...
                                            -9.9999 -9.9999 -9.9999 -9.9999
                                            indwarning -9.9999 1111]; 
                         
                         if modelall == 1
                            V2_m1_sqp = -9.9999;
                         elseif modelall == 2
                            V2_m2_sqp = -9.9999;
                         elseif modelall == 3
                            V2_m3_sqp = -9.9999;                            
                         elseif modelall == 4
                            V2_m4_sqp = -9.9999;
                         elseif modelall == 5
                            V2_m5_sqp = -9.9999;
                         elseif modelall == 6
                            V2_m6_sqp = -9.9999;
                         elseif modelall == 7
                            V2_m7_sqp = -9.9999;
                         elseif modelall == 8
                            V2_m8_sqp = -9.9999;
                         elseif modelall == 9
                            V2_m9_sqp = -9.9999;
                         end
                         
                      % If all estimated parameters are valid number
                      elseif isnan(EstMdl.Constant) + ...
                             sum(isnan(cell2mat(EstMdl.GARCH))) + ...
                             sum(isnan(cell2mat(EstMdl.ARCH))) + ...
                             sum(isnan(cell2mat(EstMdl.Leverage))) == 0
                 
                             Constant0 = EstMdl.Constant;
                             GARCH0    = cell2mat(EstMdl.GARCH);
                             ARCH0     = cell2mat(EstMdl.ARCH);
                             Leverage0 = cell2mat(EstMdl.Leverage);
                             
                             Mdl0 = egarch('Constant',Constant0,...
                                           'GARCH',GARCH0,...
                                           'ARCH',ARCH0,...
                                           'Leverage',Leverage0);
                                       
                             % In-Sample EIVOL(t-1)
                             % Expected Idiosyncratic Variance of 
                             % month (t-1) estimated using information up
                             % to month (t-1)
                             V2 = infer(Mdl0,a);
                             V2 = V2(end);
                             if isnan(V2) == 1 V2 = -9.9999; end
                             if isinf(V2) == 1 V2 = -9.9999; end
                     
                             if modelall == 1
                                V2_m1_sqp = V2;
                             elseif modelall == 2
                                V2_m2_sqp = V2;
                             elseif modelall == 3
                                V2_m3_sqp = V2;                      
                             elseif modelall == 4
                                V2_m4_sqp = V2;
                             elseif modelall == 5
                                V2_m5_sqp = V2;
                             elseif modelall == 6
                                V2_m6_sqp = V2;
                             elseif modelall == 7
                                V2_m7_sqp = V2;
                             elseif modelall == 8
                                V2_m8_sqp = V2;
                             elseif modelall == 9
                                V2_m9_sqp = V2;
                             end
                     
                             clear Constant0 GARCH0 ARCH0 Leverage0 Mdl0
                             
                             % If number of estimated parameters is smaller
                             % than the number of parameters of the EGARCH
                             % model
                             
                             if length(EstMdl.Constant) + ...
                                length(EstMdl.GARCH) + ...
                                length(EstMdl.ARCH) + ...
                                length(EstMdl.Leverage) ~= numvar0;
                                                                      
                                [msgstr, msgid] = lastwarn;
                                % If estimated covariance matrix is not a
                                % number
                                if length(msgstr)>=50 & ...
                                    sum(msgstr(1:50) == warn1(1:50)) == 50;
                                   indwarning = 3;
                                   aic        = -9.9999;
                                   bic        = -9.9999;
                                   logL       = -9.9999;
                                else
                                % If estimated covariance matrix is a
                                % number
                                   indwarning = 4;
                                   numParams  = sum(any(EstParamCov));
                                   [aic,bic]  = aicbic(logL,numParams,T);
                                end

                                M_Constant = EstMdl.Constant;
                                M_GARCH    = zeros(1,p);
                                if ~isempty(cell2mat(EstMdl.GARCH))
                                for pp = 1:length(cell2mat(EstMdl.GARCH))
                                    tmp = cell2mat(EstMdl.GARCH);
                                    M_GARCH(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                M_ARCH     = zeros(1,q);
                                if ~isempty(cell2mat(EstMdl.ARCH))
                                for pp = 1:length(cell2mat(EstMdl.ARCH))
                                    tmp = cell2mat(EstMdl.ARCH);
                                    M_ARCH(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                M_Leverage = zeros(1,q);
                                if ~isempty(cell2mat(EstMdl.Leverage))
                                for pp =...
                                    1:length(cell2mat(EstMdl.Leverage))
                                    tmp=cell2mat(EstMdl.Leverage);
                                    M_Leverage(pp)=tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                % Out-of-Sample EIVOL(t)
                                % Expected Idiosyncratic Variance of 
                                % month (t) estimated using information up
                                % to month (t-1)
                                V = forecast(EstMdl,1,'Y0',a);
                                if isnan(V) == 1 V = -9.9999;end
                                if isinf(V) == 1 V = -9.9999;end

                                tmptmp_full_ff3_sqp = ...
                                [tmptmp_full_ff3_sqp ...
                                M_Constant M_GARCH M_ARCH M_Leverage ...
                                V logL aic bic indwarning ...
                                exitflag.exitflag 1111];
                            
                                clear EstMdl indwarning logL aic bic
                                clear exitflag V numParams T
                                clear EstParamCov Mdl msgid msgstr numvar0 
                                clear M_GARCH M_ARCH M_Leverage M_Constant
                                
                             % If number of estimated parameters is equal
                             % to the number of parameters of the EGARCH
                             % model
                             elseif length(EstMdl.Constant) + ...
                                    length(EstMdl.GARCH) + ...
                                    length(EstMdl.ARCH) + ...
                                    length(EstMdl.Leverage) == numvar0;
                                
                                    V = forecast(EstMdl,1,'Y0',a);
                                    if isnan(V) == 1 V = -9.9999;end
                                    if isinf(V) == 1 V = -9.9999;end
                                   
                                   [msgstr, msgid] = lastwarn;
                                   % If estimated covariance matrix is not 
                                   % a number
                                   if length(msgstr) >= 50 & ...
                                      sum(msgstr(1:50)==warn1(1:50)) == 50;
                                      indwarning = 5;
                                      aic        = -9.9999;
                                      bic        = -9.9999;
                                      logL       = -9.9999;
                                   % If estimated covariance matrix is a
                                   % number
                                   else
                                       indwarning = 0;
                                       numParams  = sum(any(EstParamCov));
                                       [aic,bic]  = ...
                                        aicbic(logL,numParams,T);
                                   end
                                   tmptmp_full_ff3_sqp = ...
                                  [tmptmp_full_ff3_sqp ...
                                   EstMdl.Constant ...
                                   cell2mat(EstMdl.GARCH) ...
                                   cell2mat(EstMdl.ARCH) ...
                                   cell2mat(EstMdl.Leverage) ...
                                   V logL aic bic indwarning ...
                                   exitflag.exitflag 1111];
                                end
                                clear EstMdl indwarning logL aic bic 
                                clear exitflag V EstParamCov Mdl msgid 
                                clear msgstr numvar0 numParams T
                                clear M_GARCH M_ARCH M_Leverage M_Constant
                      end
                                   
                  end  
                  clear p q
              end
              
              
              tmpdata_m = tmpdata(tmpdata(:,6) == date1(j),1:16);
              
              tmp_full_ff3_method3_allstocks = ...
             [tmp_full_ff3_method3_allstocks;tmpdata_m(1,:) ...
              permno0(i) date1(j) tmptmp_full_ff3 ...
              V2_m1 V2_m2 V2_m3 V2_m4 V2_m5 V2_m6 V2_m7 V2_m8 V2_m9];
          
              tmp_full_ff3_method3_allstocks_sqp = ...
             [tmp_full_ff3_method3_allstocks_sqp;tmpdata_m(1,:) ...
              permno0(i) date1(j) tmptmp_full_ff3_sqp ...
              V2_m1_sqp V2_m2_sqp V2_m3_sqp V2_m4_sqp ...
              V2_m5_sqp V2_m6_sqp V2_m7_sqp V2_m8_sqp V2_m9_sqp];
              
              clear tmptmp_full_ff3 a tmpdata_m
              clear tmptmpff3 tmptmp_full_ff3_sqp
              clear V2_m1 V2_m2 V2_m3 V2_m4 V2_m5 V2_m6 V2_m7 V2_m8 V2_m9
              clear V2_m1_sqp V2_m2_sqp V2_m3_sqp V2_m4_sqp V2_m5_sqp 
              clear V2_m6_sqp V2_m7_sqp V2_m8 V2_m9_sqp

          end    
          
       end
       end
    
       end
        
        
    end
    
    end
    
    
    
    
    clear tmpdata tmpdata1

    
    Aout_EstIdioV_method3_allstocks     = ...
   [Aout_EstIdioV_method3_allstocks;...
    tmp_full_ff3_method3_allstocks];
    Aout_EstIdioV_method3_allstocks_sqp = ...
   [Aout_EstIdioV_method3_allstocks_sqp;...
   tmp_full_ff3_method3_allstocks_sqp];
    
    clear tmp_full_ff3_method3_allstocks 
    clear tmp_full_ff3_method3_allstocks_sqp 
    
    
    if permno0(i) >= 10000 & permno0(i) <= 19999
       if IndSave == 1
       save tmp_EstIdioV_1_19262017_allstocks_allstocks.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_1_19262017_allstocks_allstocks_sqp.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 20000 & permno0(i) <= 29999
       if IndSave == 1
       save tmp_EstIdioV_2_19262017_allstocks_allstocks.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_2_19262017_allstocks_allstocks_sqp.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 30000 & permno0(i) <= 39999
       if IndSave == 1
       save tmp_EstIdioV_3_19262017_allstocks_allstocks.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_3_19262017_allstocks_allstocks_sqp.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 40000 & permno0(i) <= 49999    
       if IndSave == 1
       save tmp_EstIdioV_4_19262017_allstocks_allstocks.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_4_19262017_allstocks_allstocks_sqp.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 50000 & permno0(i) <= 59999
       if IndSave == 1
       save tmp_EstIdioV_5_19262017_allstocks_allstocks.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_5_19262017_allstocks_allstocks_sqp.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 60000 & permno0(i) <= 69999
       if IndSave == 1
       save tmp_EstIdioV_6_19262017_allstocks_allstocks.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_6_19262017_allstocks_allstocks_sqp.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 70000 & permno0(i) <= 79999
       if IndSave == 1
       save tmp_EstIdioV_7_19262017_allstocks_allstocks.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_7_19262017_allstocks_allstocks_sqp.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 80000 & permno0(i) <= 89999
       if IndSave == 1
       save tmp_EstIdioV_8_19262017_allstocks_allstocks.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_8_19262017_allstocks_allstocks_sqp.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 90000 & permno0(i) <= 99999
       if IndSave == 1
       save tmp_EstIdioV_9_19262017_allstocks_allstocks.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_9_19262017_allstocks_allstocks_sqp.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    end
    
    
end
end

%}
%{
 No.VarName      Description
 17 PERMNO       Permno Number 
 18 Date         YearMonth (t)
 
 %----------------------E(IVOL), All available data-----------------------%
 % EGARCH Model 1
 19 Constant     
 20 GARCH_1
 21 ARCH_1
 22 Leverage_1
 23 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 24 LOG
 25 AIC
 26 BIC
 27 Indicator of Warning
 28 Exitflag
145 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1)
 % EGARCH Model 2
 30 Constant     
 31 GARCH_1
 32 ARCH_1
 33 ARCH_2
 34 Leverage_1
 35 Leverage_2
 36 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 37 LOG
 38 AIC
 39 BIC
 40 Indicator of Warning
 41 Exitflag
146 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 3
 43 Constant     
 44 GARCH_1
 45 ARCH_1
 46 ARCH_2
 47 ARCH_3
 48 Leverage_1
 49 Leverage_2
 50 Leverage_3
 51 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 52 LOG
 53 AIC
 54 BIC
 55 Indicator of Warning
 56 Exitflag
147 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 4
 58 Constant     
 59 GARCH_1
 60 GARCH_2
 61 ARCH_1
 62 Leverage_1
 63 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 64 LOG
 65 AIC
 66 BIC
 67 Indicator of Warning
 68 Exitflag
148 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 5
 70 Constant     
 71 GARCH_1
 72 GARCH_2
 73 ARCH_1
 74 ARCH_2
 75 Leverage_1
 76 Leverage_2
 77 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 78 LOG
 79 AIC
 80 BIC
 81 Indicator of Warning
 82 Exitflag
149 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 
 
 % EGARCH Model 6
 84 Constant     
 85 GARCH_1
 86 GARCH_2
 87 ARCH_1
 88 ARCH_2
 89 ARCH_3
 90 Leverage_1
 91 Leverage_2
 92 Leverage_3
 93 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 94 LOG
 95 AIC
 95 BIC
 97 Indicator of Warning
 98 Exitflag
150 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 7
100 Constant     
101 GARCH_1
102 GARCH_2
103 GARCH_3
104 ARCH_1
105 Leverage_1
106 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
107 LOG
108 AIC
109 BIC
110 Indicator of Warning
111 Exitflag
151 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 8
113 Constant     
114 GARCH_1
115 GARCH_2
116 GARCH_3
117 ARCH_1
118 ARCH_2
119 Leverage_1
120 Leverage_2
121 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
122 LOG
123 AIC
124 BIC
125 Indicator of Warning
126 Exitflag
152 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 9
128 Constant     
129 GARCH_1
130 GARCH_2
131 GARCH_3
132 ARCH_1
133 ARCH_2
134 ARCH_2
135 Leverage_1
136 Leverage_2
137 Leverage_2
138 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
139 LOG
140 AIC
141 BIC
142 Indicator of Warning
143 Exitflag
153 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 





%}
% Estimate EGARCH E(IVOL), Rolling Window of 24 - 60 months
%{
clear
clc

% Starting month for E(IVOL)
StartMonth = 196307;

load dataoutmonthly1_allstocks.mat
data = dataoutmonthly1_allstocks;
data = data(data(:,31) >= 1 & data(:,31) <= 3 & ...
            data(:,32) == 1,:);

data(:,[6 7 19 20 21 27 30 31 32]) = [];

ON_method3 = 1;
% whether to save estimated EVIOL
% 1 = save; 0 = not save
IndSave    = 0;

clear lastwarn
warning('Off');

permno0_all = unique(data(:,1));

cut2 = 4; % Minimum number of months

% Warning statement when covariance matrix is not a number
warn1 = ['Error in calculation of parameter covariance matrix.'...
         ' Matrix of NaN''s returned.'];

     
monthallall0 = [];
for yyy = 1990:2020
    for mmm = 1:12
        monthallall0 = [monthallall0;yyy*100 + mmm];
    end
end


for ppp = 1

    if ppp == 1
       permno0 = permno0_all(permno0_all >= 10000 & ...
                             permno0_all <= 19999,:);
    elseif ppp == 2
       permno0 = permno0_all(permno0_all >= 20000 & ...
                             permno0_all <=29999,:);
    elseif ppp == 3
       permno0 = permno0_all(permno0_all >= 30000 & ...
                             permno0_all <= 39999,:);
    elseif ppp == 4
       permno0 = permno0_all(permno0_all >= 40000 & ...
                             permno0_all <= 49999,:);
    elseif ppp == 5
       permno0 = permno0_all(permno0_all >= 50000 & ...
                             permno0_all <= 59999,:);
    elseif ppp == 6
       permno0 = permno0_all(permno0_all >= 60000 & ...
                             permno0_all <= 69999,:);
    elseif ppp == 7
       permno0 = permno0_all(permno0_all >= 70000 & ...
                             permno0_all <= 79999,:);
    elseif ppp == 8
       permno0 = permno0_all(permno0_all >= 80000 & ...
                             permno0_all <= 89999,:);
    elseif ppp == 9
       permno0 = permno0_all(permno0_all >= 90000 & ...
                             permno0_all <= 99999,:);
    end
    
    
    Aout_EstIdioV_method3_allstocks     = [];
    Aout_EstIdioV_method3_allstocks_sqp = [];
    
    pstart=1;
    
    
for i = pstart:size(permno0,1)
    
    tmp_full_ff3_method3_allstocks     = [];
    tmp_full_ff3_method3_allstocks_sqp = [];
    
    tmpdata      = data(data(:,1) == permno0(i),:);
    dateall0     = unique(tmpdata(:,6));
    
    for Estimation = 1
    % Eliminate missing monthly return or missing factor
    tmpdata1     = tmpdata(tmpdata(:,4) ~= -9.9999 & ...
                           tmpdata(:,7) ~= -9.9999,:);
    date0        = unique(tmpdata1(:,6));
    if size(tmpdata1,1) >= cut2
       % Number of month >= Minimum number of months
       
       for method=3
       if ON_method3 == 1
       if size(tmpdata1,1) >= cut2;
          date1 = date0;
          date1 = date1(date1 >= StartMonth,:);
          p_date_1 = find(monthallall0 == date1(j));
          % find the starting month of rolling window data
          date_start = monthallall0(p_date_1 - 60);
          clear p_date_1
          
          for j = 1:size(date1,1)
              [i j]
              % Find data before month (t) and after month (t-60) 
              tmptmpdata1 = tmpdata1(tmpdata1(:,6) < date1(j) & ...
                                     tmpdata1(:,6) >= date_start,:);
              
          if  size(tmptmpdata1,1) >= 24
              
              yyy0   = tmptmpdata1(:,4) - tmptmpdata1(:,10) / 100;
              xxx0   = [ones(size(yyy0,1),1) tmptmpdata1(:,[7 8 9]) / 100];
              rltff3 = ols1(yyy0,xxx0);
              clear yyy0 xxx0
              tmptmpff3 = [rltff3.resid];
              clear rltff3 yyy0 xxx0 tmptmpdata1

              tmptmp_full_ff3     = [];
              tmptmp_full_ff3_sqp = [];
                  
              a = tmptmpff3(:,1);
                  
              for modelall = 1:9
                  % Algorithm = 'interior-point'
                  for mmm = 1
                          
                      lastwarn('Default');
                      
                      if modelall == 1
                         p = 1;q = 1;
                      elseif modelall == 2
                         p = 1;q = 2;
                      elseif modelall == 3
                         p = 1;q = 3;
                      elseif modelall == 4;
                         p = 2;q = 1;
                      elseif modelall == 5;
                         p = 2;q = 2;
                      elseif modelall == 6;
                         p = 2;q = 3;
                      elseif modelall == 7;
                         p = 3;q = 1;
                      elseif modelall == 8;
                         p = 3;q = 2;
                      elseif modelall == 9;
                         p = 3;q = 3;
                      end
                      
                      % Number of unknown parameters for EGARCH model                   
                      numvar0 = 1 + 1*p + 1*q + 1*q;
                      T       = length(a);
                      Mdl     = egarch(p,q);

                        
                      opts             = optimset('fmincon');
                      opts.Algorithm   = 'interior-point';
                      opts.Maxiter     = 1000;
                      opts.tolfun      = 10^(-6);
                      opts.tolx        = 10^(-6);
                      opts.MaxFunEvals = 1000;

                      try
                      [EstMdl,EstParamCov,logL,exitflag] = ...
                       estimate(Mdl,a,'options',opts,'Display','Off');
                      catch      
                      % If there is warning 'Estimated GARCH model is
                      % invalid'
                       warning('Estimated GARCH model is invalid');
                       indwarning = 1;
                       tmptmp_full_ff3 = [tmptmp_full_ff3 ...
                                          ones(1,numvar0) * (-9.9999) ...
                                          -9.9999 -9.9999 -9.9999 ...
                                          -9.9999 indwarning -9.9999 1111]; 
                     
                       if modelall == 1
                          V2_m1 = -9.9999;
                       elseif modelall == 2
                          V2_m2 = -9.9999;
                       elseif modelall == 3
                          V2_m3 = -9.9999;                            
                       elseif modelall == 4
                          V2_m4 = -9.9999;
                       elseif modelall == 5
                          V2_m5 = -9.9999;
                       elseif modelall == 6
                          V2_m6 = -9.9999;
                       elseif modelall == 7
                          V2_m7 = -9.9999;
                       elseif modelall == 8
                          V2_m8 = -9.9999;
                       elseif modelall == 9
                          V2_m9 = -9.9999;
                       end
                         
                       continue;
                      end
                      
                      % If no warning 'Estimated GARCH model is valid'                     
                      if isnan(EstMdl.Constant) + ...
                         sum(isnan(cell2mat(EstMdl.GARCH))) + ...
                         sum(isnan(cell2mat(EstMdl.ARCH))) + ...
                         sum(isnan(cell2mat(EstMdl.Leverage))) > 0
                         % If at least one of estimated parameters is not
                         % a number      
                         indwarning = 2;
                         tmptmp_full_ff3 = [tmptmp_full_ff3 ...
                                            ones(1,numvar0) * (-9.9999) ...
                                            -9.9999 -9.9999 -9.9999 -9.9999
                                            indwarning -9.9999 1111]; 
                         
                         if modelall == 1
                            V2_m1 = -9.9999;
                         elseif modelall == 2
                            V2_m2 = -9.9999;
                         elseif modelall == 3
                            V2_m3 = -9.9999;                            
                         elseif modelall == 4
                            V2_m4 = -9.9999;
                         elseif modelall == 5
                            V2_m5 = -9.9999;
                         elseif modelall == 6
                            V2_m6 = -9.9999;
                         elseif modelall == 7
                            V2_m7 = -9.9999;
                         elseif modelall == 8
                            V2_m8 = -9.9999;
                         elseif modelall == 9
                            V2_m9 = -9.9999;
                         end
                         
                      % If all estimated parameters are valid number
                      elseif isnan(EstMdl.Constant) + ...
                             sum(isnan(cell2mat(EstMdl.GARCH))) + ...
                             sum(isnan(cell2mat(EstMdl.ARCH))) + ...
                             sum(isnan(cell2mat(EstMdl.Leverage))) == 0
                 
                             Constant0 = EstMdl.Constant;
                             GARCH0    = cell2mat(EstMdl.GARCH);
                             ARCH0     = cell2mat(EstMdl.ARCH);
                             Leverage0 = cell2mat(EstMdl.Leverage);
                             
                             Mdl0 = egarch('Constant',Constant0,...
                                           'GARCH',GARCH0,...
                                           'ARCH',ARCH0,...
                                           'Leverage',Leverage0);
                                       
                             % In-Sample EIVOL(t-1)
                             % Expected Idiosyncratic Variance of 
                             % month (t-1) estimated using information up
                             % to month (t-1)
                             V2 = infer(Mdl0,a);
                             V2 = V2(end);
                             if isnan(V2) == 1 V2 = -9.9999; end
                             if isinf(V2) == 1 V2 = -9.9999; end
                     
                             if modelall == 1
                                V2_m1 = V2;
                             elseif modelall == 2
                                V2_m2 = V2;
                             elseif modelall == 3
                                V2_m3 = V2;                      
                             elseif modelall == 4
                                V2_m4 = V2;
                             elseif modelall == 5
                                V2_m5 = V2;
                             elseif modelall == 6
                                V2_m6 = V2;
                             elseif modelall == 7
                                V2_m7 = V2;
                             elseif modelall == 8
                                V2_m8 = V2;
                             elseif modelall == 9
                                V2_m9 = V2;
                             end
                     
                             clear Constant0 GARCH0 ARCH0 Leverage0 Mdl0
                             
                             % If number of estimated parameters is smaller
                             % than the number of parameters of the EGARCH
                             % model
                             
                             if length(EstMdl.Constant) + ...
                                length(EstMdl.GARCH) + ...
                                length(EstMdl.ARCH) + ...
                                length(EstMdl.Leverage) ~= numvar0;
                                                                      
                                [msgstr, msgid] = lastwarn;
                                % If estimated covariance matrix is not a
                                % number
                                if length(msgstr)>=50 & ...
                                    sum(msgstr(1:50) == warn1(1:50)) == 50;
                                   indwarning = 3;
                                   aic        = -9.9999;
                                   bic        = -9.9999;
                                   logL       = -9.9999;
                                else
                                % If estimated covariance matrix is a
                                % number
                                   indwarning = 4;
                                   numParams  = sum(any(EstParamCov));
                                   [aic,bic]  = aicbic(logL,numParams,T);
                                end

                                M_Constant = EstMdl.Constant;
                                M_GARCH    = zeros(1,p);
                                if ~isempty(cell2mat(EstMdl.GARCH))
                                for pp = 1:length(cell2mat(EstMdl.GARCH))
                                    tmp = cell2mat(EstMdl.GARCH);
                                    M_GARCH(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                M_ARCH     = zeros(1,q);
                                if ~isempty(cell2mat(EstMdl.ARCH))
                                for pp = 1:length(cell2mat(EstMdl.ARCH))
                                    tmp = cell2mat(EstMdl.ARCH);
                                    M_ARCH(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                M_Leverage = zeros(1,q);
                                if ~isempty(cell2mat(EstMdl.Leverage))
                                for pp = ...
                                    1:length(cell2mat(EstMdl.Leverage))
                                    tmp = cell2mat(EstMdl.Leverage);
                                    M_Leverage(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                % Out-of-Sample EIVOL(t)
                                % Expected Idiosyncratic Variance of 
                                % month (t) estimated using information up
                                % to month (t-1)
                                V = forecast(EstMdl,1,'Y0',a);
                                if isnan(V) == 1 V = -9.9999;end
                                if isinf(V) == 1 V = -9.9999;end

                                tmptmp_full_ff3 = [tmptmp_full_ff3 ...
                                M_Constant M_GARCH M_ARCH M_Leverage ...
                                V logL aic bic indwarning ...
                                exitflag.exitflag 1111];
                            
                                clear EstMdl indwarning logL aic bic
                                clear exitflag V numParams T
                                clear EstParamCov Mdl msgid msgstr numvar0 
                                clear M_GARCH M_ARCH M_Leverage M_Constant
                                
                             % If number of estimated parameters is equal
                             % to the number of parameters of the EGARCH
                             % model
                             elseif length(EstMdl.Constant) + ...
                                    length(EstMdl.GARCH) + ...
                                    length(EstMdl.ARCH) +. ..
                                    length(EstMdl.Leverage) == numvar0;
                                
                                    V = forecast(EstMdl,1,'Y0',a);
                                    if isnan(V) == 1 V = -9.9999;end
                                    if isinf(V) == 1 V = -9.9999;end
                                   
                                   [msgstr, msgid] = lastwarn;
                                   % If estimated covariance matrix is not
                                   % a number
                                   if length(msgstr) >= 50 & ...
                                      sum(msgstr(1:50)==warn1(1:50)) == 50;
                                      indwarning = 5;
                                      aic        = -9.9999;
                                      bic        = -9.9999;
                                      logL       = -9.9999;
                                   % If estimated covariance matrix is a
                                   % number
                                   else
                                       indwarning = 0;
                                       numParams  = sum(any(EstParamCov));
                                       [aic,bic]  = ...
                                        aicbic(logL,numParams,T);
                                   end
                                   tmptmp_full_ff3 = [tmptmp_full_ff3 ...
                                   EstMdl.Constant ...
                                   cell2mat(EstMdl.GARCH) ...
                                   cell2mat(EstMdl.ARCH) ...
                                   cell2mat(EstMdl.Leverage) ...
                                   V logL aic bic indwarning ...
                                   exitflag.exitflag 1111];
                                end
                                clear EstMdl indwarning logL aic bic 
                                clear exitflag V EstParamCov Mdl msgid 
                                clear msgstr numvar0 numParams T
                                clear M_GARCH M_ARCH M_Leverage M_Constant
                      end
                                   
                  end  
                  % Algorithm = 'sqp'
                  for mmm = 2
                          
                      lastwarn('Default');
                      
                      if modelall == 1
                         p = 1;q = 1;
                      elseif modelall == 2
                         p = 1;q = 2;
                      elseif modelall == 3
                         p = 1;q = 3;
                      elseif modelall == 4
                         p = 2;q = 1;
                      elseif modelall == 5
                         p = 2;q = 2;
                      elseif modelall == 6
                         p = 2;q = 3;
                      elseif modelall == 7
                         p = 3;q = 1;
                      elseif modelall == 8
                         p = 3;q = 2;
                      elseif modelall == 9
                         p = 3;q = 3;
                      end
                      
                      % Number of unknown parameters for EGARCH model                   
                      numvar0 = 1 + 1*p + 1*q + 1*q;
                      T       = length(a);
                      Mdl     = egarch(p,q);
                         
                      opts             = optimset('fmincon');
                      opts.Algorithm   = 'sqp';
                      opts.Maxiter     = 1000;
                      opts.tolfun      = 10^(-6);
                      opts.tolx        = 10^(-6);
                      opts.MaxFunEvals = 1000;

                      try
                      [EstMdl,EstParamCov,logL,exitflag] =...
                       estimate(Mdl,a,'options',opts,'Display','Off');
                      catch      
                      % If there is warning 'Estimated GARCH model is
                      % invalid'
                       warning('Estimated GARCH model is invalid');
                       indwarning = 1;
                       tmptmp_full_ff3_sqp = [tmptmp_full_ff3_sqp ...
                                          ones(1,numvar0) * (-9.9999) ...
                                          -9.9999 -9.9999 -9.9999 ...
                                          -9.9999 indwarning -9.9999 1111]; 
                     
                       if modelall == 1
                          V2_m1_sqp = -9.9999;
                       elseif modelall == 2
                          V2_m2_sqp = -9.9999;
                       elseif modelall == 3
                          V2_m3_sqp = -9.9999;                            
                       elseif modelall == 4
                          V2_m4_sqp = -9.9999;
                       elseif modelall == 5
                          V2_m5_sqp = -9.9999;
                       elseif modelall == 6
                          V2_m6_sqp = -9.9999;
                       elseif modelall == 7
                          V2_m7_sqp = -9.9999;
                       elseif modelall == 8
                          V2_m8_sqp = -9.9999;
                       elseif modelall == 9
                          V2_m9_sqp = -9.9999;
                       end
                         
                       continue;
                      end
                      
                      % If no warning 'Estimated GARCH model is valid'                     
                      if isnan(EstMdl.Constant) + ...
                         sum(isnan(cell2mat(EstMdl.GARCH))) + ...
                         sum(isnan(cell2mat(EstMdl.ARCH))) + ...
                         sum(isnan(cell2mat(EstMdl.Leverage))) > 0
                         % If at least one of estimated parameters is not
                         % a number      
                         indwarning = 2;
                         tmptmp_full_ff3_sqp = [tmptmp_full_ff3_sqp ...
                                            ones(1,numvar0) * (-9.9999) ...
                                            -9.9999 -9.9999 -9.9999 -9.9999
                                            indwarning -9.9999 1111]; 
                         
                         if modelall == 1
                            V2_m1_sqp = -9.9999;
                         elseif modelall == 2
                            V2_m2_sqp = -9.9999;
                         elseif modelall == 3
                            V2_m3_sqp = -9.9999;                            
                         elseif modelall == 4
                            V2_m4_sqp = -9.9999;
                         elseif modelall == 5
                            V2_m5_sqp = -9.9999;
                         elseif modelall == 6
                            V2_m6_sqp = -9.9999;
                         elseif modelall == 7
                            V2_m7_sqp = -9.9999;
                         elseif modelall == 8
                            V2_m8_sqp = -9.9999;
                         elseif modelall == 9
                            V2_m9_sqp = -9.9999;
                         end
                         
                      % If all estimated parameters are valid number
                      elseif isnan(EstMdl.Constant) + ...
                             sum(isnan(cell2mat(EstMdl.GARCH))) + ...
                             sum(isnan(cell2mat(EstMdl.ARCH))) + ...
                             sum(isnan(cell2mat(EstMdl.Leverage))) == 0
                 
                             Constant0 = EstMdl.Constant;
                             GARCH0    = cell2mat(EstMdl.GARCH);
                             ARCH0     = cell2mat(EstMdl.ARCH);
                             Leverage0 = cell2mat(EstMdl.Leverage);
                             
                             Mdl0 = egarch('Constant',Constant0,...
                                           'GARCH',GARCH0,...
                                           'ARCH',ARCH0,...
                                           'Leverage',Leverage0);
                                       
                             % In-Sample EIVOL(t-1)
                             % Expected Idiosyncratic Variance of 
                             % month (t-1) estimated using information up
                             % to month (t-1)
                             V2 = infer(Mdl0,a);
                             V2 = V2(end);
                             if isnan(V2) == 1 V2 = -9.9999; end
                             if isinf(V2) == 1 V2 = -9.9999; end
                     
                             if modelall == 1
                                V2_m1_sqp = V2;
                             elseif modelall == 2
                                V2_m2_sqp = V2;
                             elseif modelall == 3
                                V2_m3_sqp = V2;                      
                             elseif modelall == 4
                                V2_m4_sqp = V2;
                             elseif modelall == 5
                                V2_m5_sqp = V2;
                             elseif modelall == 6
                                V2_m6_sqp = V2;
                             elseif modelall == 7
                                V2_m7_sqp = V2;
                             elseif modelall == 8
                                V2_m8_sqp = V2;
                             elseif modelall == 9
                                V2_m9_sqp = V2;
                             end
                     
                             clear Constant0 GARCH0 ARCH0 Leverage0 Mdl0
                             
                             % If number of estimated parameters is smaller
                             % than the number of parameters of the EGARCH
                             % model
                             
                             if length(EstMdl.Constant) + ...
                                length(EstMdl.GARCH) + ...
                                length(EstMdl.ARCH) + ...
                                length(EstMdl.Leverage) ~= numvar0;
                                                                      
                                [msgstr, msgid] = lastwarn;
                                % If estimated covariance matrix is not a
                                % number
                                if length(msgstr)>=50 & ...
                                    sum(msgstr(1:50) == warn1(1:50)) == 50;
                                   indwarning = 3;
                                   aic        = -9.9999;
                                   bic        = -9.9999;
                                   logL       = -9.9999;
                                else
                                % If estimated covariance matrix is a
                                % number
                                   indwarning = 4;
                                   numParams  = sum(any(EstParamCov));
                                   [aic,bic]  = aicbic(logL,numParams,T);
                                end

                                M_Constant = EstMdl.Constant;
                                M_GARCH    = zeros(1,p);
                                if ~isempty(cell2mat(EstMdl.GARCH))
                                for pp = 1:length(cell2mat(EstMdl.GARCH))
                                    tmp = cell2mat(EstMdl.GARCH);
                                    M_GARCH(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                M_ARCH     = zeros(1,q);
                                if ~isempty(cell2mat(EstMdl.ARCH))
                                for pp = 1:length(cell2mat(EstMdl.ARCH))
                                    tmp = cell2mat(EstMdl.ARCH);
                                    M_ARCH(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                M_Leverage = zeros(1,q);
                                if ~isempty(cell2mat(EstMdl.Leverage))
                                for pp =...
                                    1:length(cell2mat(EstMdl.Leverage))
                                    tmp = cell2mat(EstMdl.Leverage);
                                    M_Leverage(pp) = tmp(pp);
                                    clear tmp
                                end
                                    clear pp
                                end
                                % Out-of-Sample EIVOL(t)
                                % Expected Idiosyncratic Variance of 
                                % month (t) estimated using information up
                                % to month (t-1)
                                V = forecast(EstMdl,1,'Y0',a);
                                if isnan(V) == 1 V = -9.9999;end
                                if isinf(V) == 1 V = -9.9999;end

                                tmptmp_full_ff3_sqp = ...
                                [tmptmp_full_ff3_sqp ...
                                M_Constant M_GARCH M_ARCH M_Leverage ...
                                V logL aic bic indwarning ...
                                exitflag.exitflag 1111];
                            
                                clear EstMdl indwarning logL aic bic
                                clear exitflag V numParams T
                                clear EstParamCov Mdl msgid msgstr numvar0 
                                clear M_GARCH M_ARCH M_Leverage M_Constant
                                
                             % If number of estimated parameters is equal
                             % to the number of parameters of the EGARCH
                             % model
                             elseif length(EstMdl.Constant) + ...
                                    length(EstMdl.GARCH) + ...
                                    length(EstMdl.ARCH) + ...
                                    length(EstMdl.Leverage) == numvar0;
                                
                                    V = forecast(EstMdl,1,'Y0',a);
                                    if isnan(V) == 1 V = -9.9999;end
                                    if isinf(V) == 1 V = -9.9999;end
                                   
                                   [msgstr, msgid] = lastwarn;
                                   % If estimated covariance matrix is not 
                                   % a number
                                   if length(msgstr) >= 50 & ...
                                      sum(msgstr(1:50)==warn1(1:50)) == 50;
                                      indwarning = 5;
                                      aic        = -9.9999;
                                      bic        = -9.9999;
                                      logL       = -9.9999;
                                   % If estimated covariance matrix is a
                                   % number
                                   else
                                       indwarning = 0;
                                       numParams  = sum(any(EstParamCov));
                                       [aic,bic]  = ...
                                        aicbic(logL,numParams,T);
                                   end
                                   tmptmp_full_ff3_sqp = ...
                                  [tmptmp_full_ff3_sqp ...
                                   EstMdl.Constant ...
                                   cell2mat(EstMdl.GARCH) ...
                                   cell2mat(EstMdl.ARCH) ...
                                   cell2mat(EstMdl.Leverage) ...
                                   V logL aic bic indwarning ...
                                   exitflag.exitflag 1111];
                                end
                                clear EstMdl indwarning logL aic bic 
                                clear exitflag V EstParamCov Mdl msgid 
                                clear msgstr numvar0 numParams T
                                clear M_GARCH M_ARCH M_Leverage M_Constant
                      end
                                   
                  end  
                  clear p q
              end
              
              
              tmpdata_m = tmpdata(tmpdata(:,6) == date1(j),1:16);
              
              tmp_full_ff3_method3_allstocks = ...
             [tmp_full_ff3_method3_allstocks;tmpdata_m(1,:) ...
              permno0(i) date1(j) tmptmp_full_ff3 ...
              V2_m1 V2_m2 V2_m3 V2_m4 V2_m5 V2_m6 V2_m7 V2_m8 V2_m9];
          
              tmp_full_ff3_method3_allstocks_sqp = ...
             [tmp_full_ff3_method3_allstocks_sqp;tmpdata_m(1,:) ...
              permno0(i) date1(j) tmptmp_full_ff3_sqp ...
              V2_m1_sqp V2_m2_sqp V2_m3_sqp V2_m4_sqp ...
              V2_m5_sqp V2_m6_sqp V2_m7_sqp V2_m8_sqp V2_m9_sqp];
              
              clear tmptmp_full_ff3 a tmpdata_m
              clear tmptmpff3 tmptmp_full_ff3_sqp
              clear V2_m1 V2_m2 V2_m3 V2_m4 V2_m5 V2_m6 V2_m7 V2_m8 V2_m9
              clear V2_m1_sqp V2_m2_sqp V2_m3_sqp V2_m4_sqp V2_m5_sqp 
              clear V2_m6_sqp V2_m7_sqp V2_m8 V2_m9_sqp
          end
          end    
          
       end
       end
    
       end
       
       
        
        
    end
    
    end
    
    
    
    
    clear tmpdata tmpdata1

    
    Aout_EstIdioV_method3_allstocks     = ...
   [Aout_EstIdioV_method3_allstocks;...
    tmp_full_ff3_method3_allstocks];
    Aout_EstIdioV_method3_allstocks_sqp = ...
   [Aout_EstIdioV_method3_allstocks_sqp;...
   tmp_full_ff3_method3_allstocks_sqp];
    
    clear tmp_full_ff3_method3_allstocks 
    clear tmp_full_ff3_method3_allstocks_sqp 
    
    
    if permno0(i) >= 10000 & permno0(i) <= 19999
       if IndSave == 1
       save tmp_EstIdioV_1_19262017_allstocks_allstocks_Roll.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_1_19262017_allstocks_allstocks_sqp_Roll.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 20000 & permno0(i) <= 29999
       if IndSave == 1
       save tmp_EstIdioV_2_19262017_allstocks_allstocks_Roll.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_2_19262017_allstocks_allstocks_sqp_Roll.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 30000 & permno0(i) <= 39999
       if IndSave == 1
       save tmp_EstIdioV_3_19262017_allstocks_allstocks_Roll.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_3_19262017_allstocks_allstocks_sqp_Roll.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 40000 & permno0(i) <= 49999    
       if IndSave == 1
       save tmp_EstIdioV_4_19262017_allstocks_allstocks_Roll.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_4_19262017_allstocks_allstocks_sqp_Roll.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 50000 & permno0(i) <= 59999
       if IndSave == 1
       save tmp_EstIdioV_5_19262017_allstocks_allstocks_Roll.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_5_19262017_allstocks_allstocks_sqp_Roll.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 60000 & permno0(i) <= 69999
       if IndSave == 1
       save tmp_EstIdioV_6_19262017_allstocks_allstocks_Roll.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_6_19262017_allstocks_allstocks_sqp_Roll.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 70000 & permno0(i) <= 79999
       if IndSave == 1
       save tmp_EstIdioV_7_19262017_allstocks_allstocks_Roll.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_7_19262017_allstocks_allstocks_sqp_Roll.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 80000 & permno0(i) <= 89999
       if IndSave == 1
       save tmp_EstIdioV_8_19262017_allstocks_allstocks_Roll.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_8_19262017_allstocks_allstocks_sqp_Roll.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    elseif permno0(i) >= 90000 & permno0(i) <= 99999
       if IndSave == 1
       save tmp_EstIdioV_9_19262017_allstocks_allstocks_Roll.mat...
            Aout_EstIdioV_method3_allstocks
       save tmp_EstIdioV_9_19262017_allstocks_allstocks_sqp_Roll.mat...
            Aout_EstIdioV_method3_allstocks_sqp
       end
    end
    
    
end
end

%}
%{
 No.VarName      Description
 17 PERMNO       Permno Number 
 18 Date         YearMonth (t)
 
 %----------------------E(IVOL), Rolling Window---------------------------%
 % EGARCH Model 1
 19 Constant     
 20 GARCH_1
 21 ARCH_1
 22 Leverage_1
 23 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 24 LOG
 25 AIC
 26 BIC
 27 Indicator of Warning
 28 Exitflag
145 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1)
 % EGARCH Model 2
 30 Constant     
 31 GARCH_1
 32 ARCH_1
 33 ARCH_2
 34 Leverage_1
 35 Leverage_2
 36 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 37 LOG
 38 AIC
 39 BIC
 40 Indicator of Warning
 41 Exitflag
146 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 3
 43 Constant     
 44 GARCH_1
 45 ARCH_1
 46 ARCH_2
 47 ARCH_3
 48 Leverage_1
 49 Leverage_2
 50 Leverage_3
 51 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 52 LOG
 53 AIC
 54 BIC
 55 Indicator of Warning
 56 Exitflag
147 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 4
 58 Constant     
 59 GARCH_1
 60 GARCH_2
 61 ARCH_1
 62 Leverage_1
 63 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 64 LOG
 65 AIC
 66 BIC
 67 Indicator of Warning
 68 Exitflag
148 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 5
 70 Constant     
 71 GARCH_1
 72 GARCH_2
 73 ARCH_1
 74 ARCH_2
 75 Leverage_1
 76 Leverage_2
 77 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 78 LOG
 79 AIC
 80 BIC
 81 Indicator of Warning
 82 Exitflag
149 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 
 
 % EGARCH Model 6
 84 Constant     
 85 GARCH_1
 86 GARCH_2
 87 ARCH_1
 88 ARCH_2
 89 ARCH_3
 90 Leverage_1
 91 Leverage_2
 92 Leverage_3
 93 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
 94 LOG
 95 AIC
 95 BIC
 97 Indicator of Warning
 98 Exitflag
150 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 7
100 Constant     
101 GARCH_1
102 GARCH_2
103 GARCH_3
104 ARCH_1
105 Leverage_1
106 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
107 LOG
108 AIC
109 BIC
110 Indicator of Warning
111 Exitflag
151 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 8
113 Constant     
114 GARCH_1
115 GARCH_2
116 GARCH_3
117 ARCH_1
118 ARCH_2
119 Leverage_1
120 Leverage_2
121 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
122 LOG
123 AIC
124 BIC
125 Indicator of Warning
126 Exitflag
152 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 

 % EGARCH Model 9
128 Constant     
129 GARCH_1
130 GARCH_2
131 GARCH_3
132 ARCH_1
133 ARCH_2
134 ARCH_2
135 Leverage_1
136 Leverage_2
137 Leverage_2
138 EIVOL(t): Expected Idiosyncratic Variance for month (t) estimated using 
    information up to month (t-1)
139 LOG
140 AIC
141 BIC
142 Indicator of Warning
143 Exitflag
153 EIVOL(t-1): Expected Idiosyncratic Variance for month (t-1) estimated 
    using information up to month (t-1) 





%}

% ---------------------------- Variables End -----------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Section 3
% Tables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% -------------------------------- Tables --------------------------------%
%  Table 1 and Table 2 Time series characteristics of IVOL for each firm
% ------------------------------------------------------------------------%
% Calculate mean, std and skewness of IVOL and LOGIVOL
% Do test of random walk proocess of IVOL and LOGIVOL

% Load data on calculated monthly IVOL
%{
clear
clc
data = [];
load tmpoutresid1_allstocks_nonzero.mat
data = [data;Aout_resid];
clear Aout_resid
load tmpoutresid2_allstocks_nonzero.mat
data = [data;Aout_resid];
clear Aout_resid
load tmpoutresid3_allstocks_nonzero.mat
data = [data;Aout_resid];
clear Aout_resid
load tmpoutresid4_allstocks_nonzero.mat
data = [data;Aout_resid];
clear Aout_resid
load tmpoutresid5_allstocks_nonzero.mat
data = [data;Aout_resid];
clear Aout_resid
load tmpoutresid6_allstocks_nonzero.mat
data = [data;Aout_resid];
clear Aout_resid
load tmpoutresid7_allstocks_nonzero.mat
data = [data;Aout_resid];
clear Aout_resid
load tmpoutresid8_allstocks_nonzero.mat
data = [data;Aout_resid];
clear Aout_resid
load tmpoutresid9_allstocks_nonzero.mat
data = [data;Aout_resid];
clear Aout_resid

%}
% Sample period 1963 - 2006
%{

% Sample Period of Fu (2009)
data_fu2009  = data(data(:,2) <= 200612 & ...
                    data(:,2) >= 196307 & data(:,5) >= 15,:);
data0        = data_fu2009;

ppp          = isnan(data0(:,3));
data0(ppp,:) = [];

permnoall    = unique(data0(:,1));
monthall     = unique(data0(:,2));


cut3=5;  % Minimum number of months to calculate autocorrelations
cut4=30; % Minimum number of months to test random walk process


DFTable = [ 25  49 -3.75;
            50  99 -3.59;
           100 249 -3.50;
           250 499 -3.45;
           500 999 -3.44];

     
A_out_Table1      = [];
A_out_Table1_LNG  = [];
A_out_Table1_LNG1 = [];
A_out_Table2      = [];

for i = 1:size(permnoall,1)
    
    tmpdata         = data0(data0(:,1) == permnoall(i),:);
    meanIVOL_fama3  = mean(tmpdata(:,3));
    
    if size(tmpdata,1) >= 2
    stdIVOL_fama3   = std(tmpdata(:,3));
    else
    stdIVOL_fama3   = -9.9999;  
    end
    
    if size(tmpdata,1)>= 3 & meanIVOL_fama3 > 0
    skewIVOL_fama3  = skewness(tmpdata(:,3),0);
    else
    skewIVOL_fama3  = -9.9999;    
    end
    
    month0          = unique(tmpdata(:,2));
    nummonth        = size(month0,1);
    
    % Stocks are traded on and after July 1963
    if max(month0) >= 196307
        
      tmpout = [];
    % Find lagged idiosyncratic volatility
    for j = 1:size(month0,1)
        tmptmpdata = tmpdata(tmpdata(:,2) == month0(j),[1 2 3]);
        
        p0 = find(monthall == month0(j));
        % Find IVOL (t-1)
        if p0 > 1
           month_1         = monthall(p0 - 1);
           tmpIVOL_fama3_1 = tmpdata(tmpdata(:,2) == month_1,3);
           if isempty(tmpIVOL_fama3_1) == 1
              tmpIVOL_fama3_1 = -9.9999;
           end
           clear month_1
        else
           tmpIVOL_fama3_1 = -9.9999; 
        end
        % Find IVOL (t-2)
        if p0 > 2
           month_2         = monthall(p0 - 2);
           tmpIVOL_fama3_2 = tmpdata(tmpdata(:,2) == month_2,3);
           if isempty(tmpIVOL_fama3_2) == 1
              tmpIVOL_fama3_2 = -9.9999;
           end
           clear month_2
        else
           tmpIVOL_fama3_2 = -9.9999; 
        end
        % Find IVOL (t-3)
        if p0 > 3
           month_3         = monthall(p0 - 3);
           tmpIVOL_fama3_3 = tmpdata(tmpdata(:,2) == month_3,3);
           if isempty(tmpIVOL_fama3_3) == 1
              tmpIVOL_fama3_3 = -9.9999;
           end
           clear month_3
        else
           tmpIVOL_fama3_3 =-9.9999; 
        end
        % Find IVOL (t-4)
        if p0 > 4
           month_4         = monthall(p0 - 4);
           tmpIVOL_fama3_4 = tmpdata(tmpdata(:,2) == month_4,3);
           if isempty(tmpIVOL_fama3_4) == 1
              tmpIVOL_fama3_4 = -9.9999;
           end
           clear month_4
        else
           tmpIVOL_fama3_4 = -9.9999; 
        end
        % Find IVOL (t-5)
        if p0 > 5
           month_5         = monthall(p0 - 5);
           tmpIVOL_fama3_5 = tmpdata(tmpdata(:,2) == month_5,3);
           if isempty(tmpIVOL_fama3_5) == 1
              tmpIVOL_fama3_5 = -9.9999;
           end
           clear month_5
        else
           tmpIVOL_fama3_5 = -9.9999; 
        end
        % Find IVOL (t-6)
        if p0 > 6
           month_6         = monthall(p0 - 6);
           tmpIVOL_fama3_6 = tmpdata(tmpdata(:,2) == month_6,3);
           if isempty(tmpIVOL_fama3_6) == 1
              tmpIVOL_fama3_6 = -9.9999;
           end
           clear month_6
        else
           tmpIVOL_fama3_6 = -9.9999; 
        end
        % Find IVOL (t-11)  
        if p0 > 11
           month_11         = monthall(p0 - 11);
           tmpIVOL_fama3_11 = tmpdata(tmpdata(:,2) == month_11,3);
           if isempty(tmpIVOL_fama3_11) == 1
              tmpIVOL_fama3_11 = -9.9999;
           end
           clear month_11
        else
           tmpIVOL_fama3_11 = -9.9999; 
        end
        % Find IVOL (t-12)  
        if p0 > 12
           month_12         =monthall(p0 - 12);
           tmpIVOL_fama3_12 = tmpdata(tmpdata(:,2) == month_12,3);
           if isempty(tmpIVOL_fama3_12) == 1
              tmpIVOL_fama3_12 = -9.9999;
           end
           clear month_12
        else
           tmpIVOL_fama3_12 = -9.9999; 
        end
        % Find IVOL (t-13)  
        if p0 > 13
           month_13         = monthall(p0 - 13);
           tmpIVOL_fama3_13 = tmpdata(tmpdata(:,2) == month_13,3);
           if isempty(tmpIVOL_fama3_13) == 1
              tmpIVOL_fama3_13 = -9.9999;
           end
           clear month_13
        else
           tmpIVOL_fama3_13 = -9.9999; 
        end  
        
        tmpout=[tmpout;tmptmpdata ...
                       tmpIVOL_fama3_1 tmpIVOL_fama3_2 tmpIVOL_fama3_3 ...
                       tmpIVOL_fama3_4 tmpIVOL_fama3_5 tmpIVOL_fama3_6 ...
                       tmpIVOL_fama3_11 tmpIVOL_fama3_12 tmpIVOL_fama3_13];
                   
        clear tmpIVOL_fama3_1 tmpIVOL_fama3_2 tmpIVOL_fama3_3 
        clear tmpIVOL_fama3_4 tmpIVOL_fama3_5 tmpIVOL_fama3_6
        clear tmpIVOL_fama3_11 tmpIVOL_fama3_12 tmpIVOL_fama3_13 p0
        clear tmptmpdata
    end
    
    tmp0 = tmpout(:,[2 3 4]);
    % Eliminate months with missing IVOL (t) or missing IVOL (t-1)
    tmp0 = tmp0(tmp0(:,2) ~= -9.9999 & tmp0(:,3) ~= -9.9999,:);
    if size(tmp0,1)>0
       tmp0(tmp0(:,2) == 0 | tmp0(:,3) == 0,4)   = -9.9999;
       tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,4) = ...
            log(tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,2). / ...
            tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,3));
       tmpLNG = tmp0;
    else
       tmpLNG = [-9.9999 -9.9999 -9.9999 -9.9999];
    end
    clear tmp0
           

    tmpautocorrpara  = [];
    tmpDFtest        = [];
    tmpDFtest_LN     = [];
    
    lagc0 = 4;
    for lag = [1 2 3 4 5 6 11 12 13]
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                  tmpout(:,lagc0) ~= -9.9999,:);
              
        if size(regdata,1) >= cut3;
           yyy0 = regdata(:,3);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           rlt0 = ols1(yyy0,xxx0);
           rho0 = corrcoef(yyy0,xxx0(:,2));
           rho0 = rho0(1,2);
           tmpautocorrpara = [tmpautocorrpara rho0];
        else
           tmpautocorrpara = [tmpautocorrpara -9.9999];
        end
        
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                  tmpout(:,lagc0) ~= -9.9999,:);
        if lag == 1 & size(regdata,1) >= cut4;
           yyy0 = regdata(:,3) - regdata(:,lagc0);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           if std(yyy0) > 0 & std(xxx0(:,2)) > 0
              rlt0 = ols1(yyy0,xxx0);
           if size(regdata,1) >= DFTable(1,1) & ...
              size(regdata,1) <= DFTable(1,2)
            ind0 = (rlt0.tstat(2) <= DFTable(1,3));
           elseif size(regdata,1) >= DFTable(2,1) & ...
                  size(regdata,1) <= DFTable(2,2)
            ind0 = (rlt0.tstat(2) <= DFTable(2,3));
           elseif size(regdata,1) >= DFTable(3,1) & ...
                  size(regdata,1) <= DFTable(3,2)
            ind0 = (rlt0.tstat(2) <= DFTable(3,3));
           elseif size(regdata,1) >= DFTable(4,1) & ...
                  size(regdata,1) <= DFTable(4,2)
            ind0 = (rlt0.tstat(2) <= DFTable(4,3));
           elseif size(regdata,1) >= DFTable(5,1) & ...
                  size(regdata,1) <= DFTable(5,2)
            ind0 = (rlt0.tstat(2) <= DFTable(5,3));
           elseif size(regdata,1) <  DFTable(1,1)
            ind0 = -9.9999;          
           end
           
           tmpDFtest     = [tmpDFtest rlt0.beta(2) rlt0.tstat(2) ind0];
           else
           tmpDFtest     = [-9.9999 -9.9999 -9.9999];
           end
           
        elseif lag == 1 & size(regdata,1) < cut4
           tmpDFtest     = [-9.9999 -9.9999 -9.9999];
        end
        clear yyy0 xxx0 regdata rlt0 ind0 sizeDFtest
        
        
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                         tmpout(:,lagc0) ~= -9.9999 & ...
                         tmpout(:,3)>0 & tmpout(:,lagc0) > 0,:);
        
        
        if lag == 1 & size(regdata,1) >= cut4;
           sizeDFtest_LN_2 = size(regdata,1);
           if size(regdata,1) >= cut4;
           yyy0 = log(regdata(:,3)) - log(regdata(:,lagc0));
           xxx0 = [ones(size(yyy0,1),1) log(regdata(:,lagc0))];
           if std(yyy0) > 0 & std(xxx0(:,2)) > 0
           rlt0 = ols1(yyy0,xxx0);
           if size(regdata,1) >= DFTable(1,1) & ...
              size(regdata,1) <= DFTable(1,2)
            ind0 = (rlt0.tstat(2) <= DFTable(1,3));
           elseif size(regdata,1) >= DFTable(2,1) & ...
                  size(regdata,1) <= DFTable(2,2)
            ind0 = (rlt0.tstat(2) <= DFTable(2,3));
           elseif size(regdata,1) >= DFTable(3,1) & ...
                  size(regdata,1) <= DFTable(3,2)
            ind0 = (rlt0.tstat(2) <= DFTable(3,3));
           elseif size(regdata,1) >= DFTable(4,1) & ...
                  size(regdata,1) <= DFTable(4,2)
            ind0 = (rlt0.tstat(2) <= DFTable(4,3));
           elseif size(regdata,1) >= DFTable(5,1) & ...
                  size(regdata,1) <= DFTable(5,2)
            ind0 = (rlt0.tstat(2) <= DFTable(5,3));
           elseif size(regdata,1) <  DFTable(1,1)
            ind0 = -9.9999;
           end
           
           tmpDFtest_LN = [tmpDFtest_LN rlt0.beta(2) rlt0.tstat(2) ind0];
           else
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
           end
           else
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
           end
        elseif lag == 1 & size(regdata,1) < cut4
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
        end
        clear yyy0 xxx0 regdata rlt0 ind0 sizeDFtest_LN 
        
        lagc0 = lagc0 + 1;
    end
    clear lagc0 lag
    
        
    tmp0 = tmpLNG(tmpLNG(:,4) ~= -9.9999,:);
    if size(tmp0,1) >= 1
       meanLNGIVOL_fama3 = mean(tmp0(:,4));
    else
       meanLNGIVOL_fama3 = -9.9999;  
    end
    if size(tmp0,1) >= 2
       stdLNGIVOL_fama3 = std(tmp0(:,4));
    else
       stdLNGIVOL_fama3 = -9.9999;  
    end
    if size(tmp0,1) >= 3 & stdLNGIVOL_fama3 > 0
       skewLNGIVOL_fama3 = skewness(tmp0(:,4),0);
    else
       skewLNGIVOL_fama3 = -9.9999;  
    end
    clear tmp0
           
    
    % Find lagged log idiosyncratic volatility growth rate
    tmpdata = tmpLNG;
    month0  = unique(tmpdata(:,1));
    
    tmpout_LNG = [];
    for j = 1:size(month0,1)
        tmptmpdata = tmpdata(tmpdata(:,1) == month0(j),[1 4]);
        p0 = find(monthall == month0(j));
        % Find LOGIVOL (t-1)
        if p0 > 1
           month_1 = monthall(p0 - 1);
           tmpLNGIVOL_fama3_1 = tmpdata(tmpdata(:,1) == month_1,4);
           if isempty(tmpLNGIVOL_fama3_1) == 1
              tmpLNGIVOL_fama3_1 = -9.9999;
           end
           clear month_1
        else
           tmpLNGIVOL_fama3_1 = -9.9999; 
        end
        % Find LOGIVOL (t-2)
        if p0 > 2
           month_2 = monthall(p0 - 2);
           tmpLNGIVOL_fama3_2 = tmpdata(tmpdata(:,1) == month_2,4);
           if isempty(tmpLNGIVOL_fama3_2) == 1
              tmpLNGIVOL_fama3_2 = -9.9999;
           end
           clear month_2
        else
           tmpLNGIVOL_fama3_2 = -9.9999; 
        end
        % Find LOGIVOL (t-3)
        if p0 > 3
           month_3 = monthall(p0 - 3);
           tmpLNGIVOL_fama3_3 = tmpdata(tmpdata(:,1) == month_3,4);
           if isempty(tmpLNGIVOL_fama3_3) == 1
              tmpLNGIVOL_fama3_3 = -9.9999;
           end
           clear month_3
        else
           tmpLNGIVOL_fama3_3 = -9.9999; 
        end
        % Find LOGIVOL (t-4)
        if p0 > 4
           month_4 = monthall(p0 - 4);
           tmpLNGIVOL_fama3_4 = tmpdata(tmpdata(:,1) == month_4,4);
           if isempty(tmpLNGIVOL_fama3_4) == 1
              tmpLNGIVOL_fama3_4 = -9.9999;
           end
           clear month_4
        else
           tmpLNGIVOL_fama3_4 = -9.9999; 
        end
        % Find LOGIVOL (t-5)
        if p0 > 5
           month_5 = monthall(p0 - 5);
           tmpLNGIVOL_fama3_5 = tmpdata(tmpdata(:,1) == month_5,4);
           if isempty(tmpLNGIVOL_fama3_5) == 1
              tmpLNGIVOL_fama3_5 = -9.9999;
           end
           clear month_5
        else
           tmpLNGIVOL_fama3_5 = -9.9999; 
        end
        % Find LOGIVOL (t-6)
        if p0 > 6
           month_6 = monthall(p0 - 6);
           tmpLNGIVOL_fama3_6 = tmpdata(tmpdata(:,1) == month_6,4);
           if isempty(tmpLNGIVOL_fama3_6) == 1
              tmpLNGIVOL_fama3_6 = -9.9999;
           end
           clear month_6
        else
           tmpLNGIVOL_fama3_6 = -9.9999; 
        end
        % Find LOGIVOL (t-11)
        if p0 > 11
           month_11 = monthall(p0 - 11);
           tmpLNGIVOL_fama3_11 = tmpdata(tmpdata(:,1) == month_11,4);
           if isempty(tmpLNGIVOL_fama3_11) == 1
              tmpLNGIVOL_fama3_11 = -9.9999;
           end
           clear month_11
        else
           tmpLNGIVOL_fama3_11 = -9.9999; 
        end
        % Find LOGIVOL (t-12)
        if p0 > 12
           month_12 = monthall(p0 - 12);
           tmpLNGIVOL_fama3_12 = tmpdata(tmpdata(:,1) == month_12,4);
           if isempty(tmpLNGIVOL_fama3_12) == 1
              tmpLNGIVOL_fama3_12 = -9.9999;
           end
           clear month_12
        else
           tmpLNGIVOL_fama3_12 = -9.9999; 
        end
        % Find LOGIVOL (t-13)
        if p0 > 13
           month_13 = monthall(p0 - 13);
           tmpLNGIVOL_fama3_13=tmpdata(tmpdata(:,1) == month_13,4);
           if isempty(tmpLNGIVOL_fama3_13) == 1
              tmpLNGIVOL_fama3_13 = -9.9999;
           end
           clear month_13
        else
           tmpLNGIVOL_fama3_13 = -9.9999; 
        end   
        
        tmpout_LNG = [tmpout_LNG;tmptmpdata ...
                      tmpLNGIVOL_fama3_1 tmpLNGIVOL_fama3_2 ...
                      tmpLNGIVOL_fama3_3 tmpLNGIVOL_fama3_4 ...
                      tmpLNGIVOL_fama3_5 tmpLNGIVOL_fama3_6 ...
                      tmpLNGIVOL_fama3_11 tmpLNGIVOL_fama3_12 ...
                      tmpLNGIVOL_fama3_13];
                  
        clear tmpLNGIVOL_fama3_1 tmpLNGIVOL_fama3_2 tmpLNGIVOL_fama3_3
        clear tmpLNGIVOL_fama3_4 tmpLNGIVOL_fama3_5 tmpLNGIVOL_fama3_6
        clear tmpLNGIVOL_fama3_11 tmpLNGIVOL_fama3_12 tmpLNGIVOL_fama3_13 
        clear tmptmpdata p0
    end
    clear tmpdata month0
    
    clear tmpLNG
    
    tmpout_LNG = [permnoall(i) * ones(size(tmpout_LNG,1),1) tmpout_LNG];
    
    tmpautocorrpara_LNG = [];
    lagc0 = 4;
    for lag = [1 2 3 4 5 6 11 12 13]
        regdata = tmpout_LNG(tmpout_LNG(:,3) ~= -9.9999 & ...
                  tmpout_LNG(:,lagc0) ~= -9.9999,:);
              
        if size(regdata,1) >= cut3
           yyy0 = regdata(:,3);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           rlt0 = ols1(yyy0,xxx0);
           rho0 = corrcoef(yyy0,xxx0(:,2));
           rho0 = rho0(1,2);
           
           tmpautocorrpara_LNG = [tmpautocorrpara_LNG rho0];
        else
           tmpautocorrpara_LNG = [tmpautocorrpara_LNG -9.9999];
        end
        
        clear yyy0 xxx0 rlt0 rho0
        lagc0 = lagc0 + 1;
        
    end
    clear lagc0 lag 

    if meanIVOL_fama3 > 0 & stdIVOL_fama3 ~= -9.9999
       CVIVOL_fama3 = stdIVOL_fama3 / meanIVOL_fama3;
    else
       CVIVOL_fama3 = -9.9999;
    end
    
    if meanLNGIVOL_fama3 > 0 & stdLNGIVOL_fama3 ~= -9.9999
       CVLNGIVOL_fama3 = stdLNGIVOL_fama3 / meanLNGIVOL_fama3;
    else
       CVLNGIVOL_fama3 = -9.9999;
    end
    
    A_out_Table1     = [A_out_Table1;permnoall(i) nummonth ...
                        meanIVOL_fama3 stdIVOL_fama3 CVIVOL_fama3 ...
                        skewIVOL_fama3 tmpautocorrpara];
    A_out_Table1_LNG = [A_out_Table1_LNG;permnoall(i) nummonth ...
                        meanLNGIVOL_fama3 stdLNGIVOL_fama3 ...
                        CVLNGIVOL_fama3 skewLNGIVOL_fama3 ...
                        tmpautocorrpara_LNG];
    A_out_Table2     = [A_out_Table2;permnoall(i) nummonth tmpDFtest ...
                        tmpDFtest_LN];
    
    clear meanIVOL_fama3 stdIVOL_fama3 skewIVOL_fama3 tmpautocorrpara 
    clear CVIVOL_fama3 CVLNGIVOL_fama3
    clear meanLNGIVOL_fama3 stdLNGIVOL_fama3 skewLNGIVOL_fama3 
    clear tmpautocorrpara_LNG
    clear tmpDFtest 
    clear tmpout tmpout_LNG nummonth tmpDFtest_LN 
    end
end

% Save Data
    save Results_Table1_allstock_nonzero_1963_2006.mat...
         A_out_Table1 A_out_Table1_LNG A_out_Table1_LNG1
    save Results_Table2_allstock_nonzero_1963_2006.mat A_out_Table2

%}
% Sample period 2007 - 2017
%{

% Sample Peiord after that of Fu (2009)
data_fu2009  = data(data(:,2) <= 201712 & ...
              data(:,2) >= 200701 & data(:,5) >= 15,:);
data0        = data_fu2009;

ppp          = isnan(data0(:,3));
data0(ppp,:) = [];

permnoall    = unique(data0(:,1));
monthall     = unique(data0(:,2));


cut3=5;  % Minimum number of months to calculate autocorrelations
cut4=30; % Minimum number of months to test random walk process


DFTable = [ 25  49 -3.75;
            50  99 -3.59;
           100 249 -3.50;
           250 499 -3.45;
           500 999 -3.44];

     
A_out_Table1      = [];
A_out_Table1_LNG  = [];
A_out_Table1_LNG1 = [];
A_out_Table2      = [];

for i = 1:size(permnoall,1)
    
    tmpdata         = data0(data0(:,1) == permnoall(i),:);
    meanIVOL_fama3  = mean(tmpdata(:,3));
    
    if size(tmpdata,1) >= 2
    stdIVOL_fama3   = std(tmpdata(:,3));
    else
    stdIVOL_fama3   = -9.9999;  
    end
    
    if size(tmpdata,1)>= 3 & meanIVOL_fama3 > 0
    skewIVOL_fama3  = skewness(tmpdata(:,3),0);
    else
    skewIVOL_fama3  = -9.9999;    
    end
    
    month0          = unique(tmpdata(:,2));
    nummonth        = size(month0,1);
    
    % Stocks are traded on and after July 1963
    if max(month0) >= 196307
    tmpout = [];
    
    % Find lagged idiosyncratic volatility
    for j = 1:size(month0,1)
        tmptmpdata = tmpdata(tmpdata(:,2) == month0(j),[1 2 3]);
        
        p0 = find(monthall == month0(j));
        % Find IVOL (t-1)
        if p0 > 1
           month_1         = monthall(p0 - 1);
           tmpIVOL_fama3_1 = tmpdata(tmpdata(:,2) == month_1,3);
           if isempty(tmpIVOL_fama3_1) == 1
              tmpIVOL_fama3_1 = -9.9999;
           end
           clear month_1
        else
           tmpIVOL_fama3_1 = -9.9999; 
        end
        % Find IVOL (t-2)
        if p0 > 2
           month_2         = monthall(p0 - 2);
           tmpIVOL_fama3_2 = tmpdata(tmpdata(:,2) == month_2,3);
           if isempty(tmpIVOL_fama3_2) == 1
              tmpIVOL_fama3_2 = -9.9999;
           end
           clear month_2
        else
           tmpIVOL_fama3_2 = -9.9999; 
        end
        % Find IVOL (t-3)
        if p0 > 3
           month_3         = monthall(p0 - 3);
           tmpIVOL_fama3_3 = tmpdata(tmpdata(:,2) == month_3,3);
           if isempty(tmpIVOL_fama3_3) == 1
              tmpIVOL_fama3_3 = -9.9999;
           end
           clear month_3
        else
           tmpIVOL_fama3_3 =-9.9999; 
        end
        % Find IVOL (t-4)
        if p0 > 4
           month_4         = monthall(p0 - 4);
           tmpIVOL_fama3_4 = tmpdata(tmpdata(:,2) == month_4,3);
           if isempty(tmpIVOL_fama3_4) == 1
              tmpIVOL_fama3_4 = -9.9999;
           end
           clear month_4
        else
           tmpIVOL_fama3_4 = -9.9999; 
        end
        % Find IVOL (t-5)
        if p0 > 5
           month_5         = monthall(p0 - 5);
           tmpIVOL_fama3_5 = tmpdata(tmpdata(:,2) == month_5,3);
           if isempty(tmpIVOL_fama3_5) == 1
              tmpIVOL_fama3_5 = -9.9999;
           end
           clear month_5
        else
           tmpIVOL_fama3_5 = -9.9999; 
        end
        % Find IVOL (t-6)
        if p0 > 6
           month_6         = monthall(p0 - 6);
           tmpIVOL_fama3_6 = tmpdata(tmpdata(:,2) == month_6,3);
           if isempty(tmpIVOL_fama3_6) == 1
              tmpIVOL_fama3_6 = -9.9999;
           end
           clear month_6
        else
           tmpIVOL_fama3_6 = -9.9999; 
        end
        % Find IVOL (t-11)  
        if p0 > 11
           month_11         = monthall(p0 - 11);
           tmpIVOL_fama3_11 = tmpdata(tmpdata(:,2) == month_11,3);
           if isempty(tmpIVOL_fama3_11) == 1
              tmpIVOL_fama3_11 = -9.9999;
           end
           clear month_11
        else
           tmpIVOL_fama3_11 = -9.9999; 
        end
        % Find IVOL (t-12)  
        if p0 > 12
           month_12         =monthall(p0 - 12);
           tmpIVOL_fama3_12 = tmpdata(tmpdata(:,2) == month_12,3);
           if isempty(tmpIVOL_fama3_12) == 1
              tmpIVOL_fama3_12 = -9.9999;
           end
           clear month_12
        else
           tmpIVOL_fama3_12 = -9.9999; 
        end
        % Find IVOL (t-13)  
        if p0 > 13
           month_13         = monthall(p0 - 13);
           tmpIVOL_fama3_13 = tmpdata(tmpdata(:,2) == month_13,3);
           if isempty(tmpIVOL_fama3_13) == 1
              tmpIVOL_fama3_13 = -9.9999;
           end
           clear month_13
        else
           tmpIVOL_fama3_13 = -9.9999; 
        end  
        
        tmpout=[tmpout;tmptmpdata ...
                       tmpIVOL_fama3_1 tmpIVOL_fama3_2 tmpIVOL_fama3_3 ...
                       tmpIVOL_fama3_4 tmpIVOL_fama3_5 tmpIVOL_fama3_6 ...
                       tmpIVOL_fama3_11 tmpIVOL_fama3_12 tmpIVOL_fama3_13];
                   
        clear tmpIVOL_fama3_1 tmpIVOL_fama3_2 tmpIVOL_fama3_3 
        clear tmpIVOL_fama3_4 tmpIVOL_fama3_5 tmpIVOL_fama3_6
        clear tmpIVOL_fama3_11 tmpIVOL_fama3_12 tmpIVOL_fama3_13 p0
        clear tmptmpdata
    end
    
    tmp0 = tmpout(:,[2 3 4]);
    % Eliminate months with missing IVOL (t) or missing IVOL (t-1)
    tmp0 = tmp0(tmp0(:,2) ~= -9.9999 & tmp0(:,3) ~= -9.9999,:);
    if size(tmp0,1) > 0
       tmp0(tmp0(:,2) == 0 | tmp0(:,3) == 0,4) = -9.9999;
       tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,4) = ...
            log(tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,2). / ...
            tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,3));
       tmpLNG = tmp0;
    else
       tmpLNG = [-9.9999 -9.9999 -9.9999 -9.9999];
    end
    clear tmp0
           

    tmpautocorrpara  = [];
    tmpDFtest        = [];
    tmpDFtest_LN     = [];
    sizeDFtestall    = [];
    sizeDFtest_LNall = [];
    
    lagc0 = 4;
    for lag = [1 2 3 4 5 6 11 12 13]
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                  tmpout(:,lagc0) ~= -9.9999,:);
              
        if size(regdata,1) >= cut3;
           yyy0 = regdata(:,3);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           rlt0 = ols1(yyy0,xxx0);
           rho0 = corrcoef(yyy0,xxx0(:,2));
           rho0 = rho0(1,2);
           tmpautocorrpara = [tmpautocorrpara rho0];
        else
           tmpautocorrpara = [tmpautocorrpara -9.9999];
        end
        
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                  tmpout(:,lagc0) ~= -9.9999,:);
        if lag == 1 & size(regdata,1) >= cut4;
           yyy0 = regdata(:,3) - regdata(:,lagc0);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           if std(yyy0) > 0 & std(xxx0(:,2)) > 0
              rlt0 = ols1(yyy0,xxx0);
           if size(regdata,1) >= DFTable(1,1) & ...
              size(regdata,1) <= DFTable(1,2)
            ind0 = (rlt0.tstat(2) <= DFTable(1,3));
           elseif size(regdata,1) >= DFTable(2,1) & ...
                  size(regdata,1) <= DFTable(2,2)
            ind0 = (rlt0.tstat(2) <= DFTable(2,3));
           elseif size(regdata,1) >= DFTable(3,1) & ...
                  size(regdata,1) <= DFTable(3,2)
            ind0 = (rlt0.tstat(2) <= DFTable(3,3));
           elseif size(regdata,1) >= DFTable(4,1) & ...
                  size(regdata,1) <= DFTable(4,2)
            ind0 = (rlt0.tstat(2) <= DFTable(4,3));
           elseif size(regdata,1) >= DFTable(5,1) & ...
                  size(regdata,1) <= DFTable(5,2)
            ind0 = (rlt0.tstat(2) <= DFTable(5,3));
           elseif size(regdata,1) <  DFTable(1,1)
            ind0 = -9.9999;          
           end
           
           tmpDFtest     = [tmpDFtest rlt0.beta(2) rlt0.tstat(2) ind0];
           else
           tmpDFtest     = [-9.9999 -9.9999 -9.9999];
           end
           
        elseif lag == 1 & size(regdata,1) < cut4
           tmpDFtest     = [-9.9999 -9.9999 -9.9999];
        end
        clear yyy0 xxx0 regdata rlt0 ind0 sizeDFtest
        
        
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                         tmpout(:,lagc0) ~= -9.9999 & ...
                         tmpout(:,3) > 0 & tmpout(:,lagc0) > 0,:);
        
        
        if lag == 1 & size(regdata,1) >= cut4;
           sizeDFtest_LN_2 = size(regdata,1);
           if size(regdata,1) >= cut4;
           yyy0 = log(regdata(:,3)) - log(regdata(:,lagc0));
           xxx0 = [ones(size(yyy0,1),1) log(regdata(:,lagc0))];
           if std(yyy0) > 0 & std(xxx0(:,2)) > 0
           rlt0 = ols1(yyy0,xxx0);
           if size(regdata,1) >= DFTable(1,1) & ...
              size(regdata,1) <= DFTable(1,2)
            ind0 = (rlt0.tstat(2) <= DFTable(1,3));
           elseif size(regdata,1) >= DFTable(2,1) & ...
                  size(regdata,1) <= DFTable(2,2)
            ind0 = (rlt0.tstat(2) <= DFTable(2,3));
           elseif size(regdata,1) >= DFTable(3,1) & ...
                  size(regdata,1) <= DFTable(3,2)
            ind0 = (rlt0.tstat(2) <= DFTable(3,3));
           elseif size(regdata,1) >= DFTable(4,1) & ...
                  size(regdata,1) <= DFTable(4,2)
            ind0 = (rlt0.tstat(2) <= DFTable(4,3));
           elseif size(regdata,1) >= DFTable(5,1) & ...
                  size(regdata,1) <= DFTable(5,2)
            ind0 = (rlt0.tstat(2) <= DFTable(5,3));
           elseif size(regdata,1) <  DFTable(1,1)
            ind0 = -9.9999;
           end
           
           tmpDFtest_LN = [tmpDFtest_LN rlt0.beta(2) rlt0.tstat(2) ind0];
           else
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
           end
           else
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
           end
        elseif lag == 1 & size(regdata,1) < cut4
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
        end
        clear yyy0 xxx0 regdata rlt0 ind0 sizeDFtest_LN 
        
        lagc0 = lagc0 + 1;
    end
    clear lagc0 lag
    
        
    tmp0 = tmpLNG(tmpLNG(:,4) ~= -9.9999,:);
    if size(tmp0,1) >= 1
       meanLNGIVOL_fama3 = mean(tmp0(:,4));
    else
       meanLNGIVOL_fama3 = -9.9999;  
    end
    if size(tmp0,1) >= 2
       stdLNGIVOL_fama3 = std(tmp0(:,4));
    else
       stdLNGIVOL_fama3 = -9.9999;  
    end
    if size(tmp0,1) >= 3 & stdLNGIVOL_fama3 > 0
       skewLNGIVOL_fama3 = skewness(tmp0(:,4),0);
    else
       skewLNGIVOL_fama3 = -9.9999;  
    end
    clear tmp0
           
    
    % Find lagged log idiosyncratic volatility growth rate
    tmpdata = tmpLNG;
    month0  = unique(tmpdata(:,1));
    
    tmpout_LNG = [];
    for j = 1:size(month0,1)
        tmptmpdata = tmpdata(tmpdata(:,1) == month0(j),[1 4]);
        p0 = find(monthall == month0(j));
        % Find LOGIVOL (t-1)
        if p0 > 1
           month_1 = monthall(p0 - 1);
           tmpLNGIVOL_fama3_1 = tmpdata(tmpdata(:,1) == month_1,4);
           if isempty(tmpLNGIVOL_fama3_1) == 1
              tmpLNGIVOL_fama3_1 = -9.9999;
           end
           clear month_1
        else
           tmpLNGIVOL_fama3_1 = -9.9999; 
        end
        % Find LOGIVOL (t-2)
        if p0 > 2
           month_2 = monthall(p0 - 2);
           tmpLNGIVOL_fama3_2 = tmpdata(tmpdata(:,1) == month_2,4);
           if isempty(tmpLNGIVOL_fama3_2) == 1
              tmpLNGIVOL_fama3_2 = -9.9999;
           end
           clear month_2
        else
           tmpLNGIVOL_fama3_2 = -9.9999; 
        end
        % Find LOGIVOL (t-3)
        if p0 > 3
           month_3 = monthall(p0 - 3);
           tmpLNGIVOL_fama3_3 = tmpdata(tmpdata(:,1) == month_3,4);
           if isempty(tmpLNGIVOL_fama3_3) == 1
              tmpLNGIVOL_fama3_3 = -9.9999;
           end
           clear month_3
        else
           tmpLNGIVOL_fama3_3 = -9.9999; 
        end
        % Find LOGIVOL (t-4)
        if p0 > 4
           month_4 = monthall(p0 - 4);
           tmpLNGIVOL_fama3_4 = tmpdata(tmpdata(:,1) == month_4,4);
           if isempty(tmpLNGIVOL_fama3_4) == 1
              tmpLNGIVOL_fama3_4 = -9.9999;
           end
           clear month_4
        else
           tmpLNGIVOL_fama3_4 = -9.9999; 
        end
        % Find LOGIVOL (t-5)
        if p0 > 5
           month_5 = monthall(p0 - 5);
           tmpLNGIVOL_fama3_5 = tmpdata(tmpdata(:,1) == month_5,4);
           if isempty(tmpLNGIVOL_fama3_5) == 1
              tmpLNGIVOL_fama3_5 = -9.9999;
           end
           clear month_5
        else
           tmpLNGIVOL_fama3_5 = -9.9999; 
        end
        % Find LOGIVOL (t-6)
        if p0 > 6
           month_6 = monthall(p0 - 6);
           tmpLNGIVOL_fama3_6 = tmpdata(tmpdata(:,1) == month_6,4);
           if isempty(tmpLNGIVOL_fama3_6) == 1
              tmpLNGIVOL_fama3_6 = -9.9999;
           end
           clear month_6
        else
           tmpLNGIVOL_fama3_6 = -9.9999; 
        end
        % Find LOGIVOL (t-11)
        if p0 > 11
           month_11 = monthall(p0 - 11);
           tmpLNGIVOL_fama3_11 = tmpdata(tmpdata(:,1) == month_11,4);
           if isempty(tmpLNGIVOL_fama3_11) == 1
              tmpLNGIVOL_fama3_11 = -9.9999;
           end
           clear month_11
        else
           tmpLNGIVOL_fama3_11 = -9.9999; 
        end
        % Find LOGIVOL (t-12)
        if p0 > 12
           month_12 = monthall(p0 - 12);
           tmpLNGIVOL_fama3_12 = tmpdata(tmpdata(:,1) == month_12,4);
           if isempty(tmpLNGIVOL_fama3_12) == 1
              tmpLNGIVOL_fama3_12 = -9.9999;
           end
           clear month_12
        else
           tmpLNGIVOL_fama3_12 = -9.9999; 
        end
        % Find LOGIVOL (t-13)
        if p0 > 13
           month_13 = monthall(p0 - 13);
           tmpLNGIVOL_fama3_13=tmpdata(tmpdata(:,1) == month_13,4);
           if isempty(tmpLNGIVOL_fama3_13) == 1
              tmpLNGIVOL_fama3_13 = -9.9999;
           end
           clear month_13
        else
           tmpLNGIVOL_fama3_13 = -9.9999; 
        end   
        
        tmpout_LNG = [tmpout_LNG;tmptmpdata ...
                      tmpLNGIVOL_fama3_1 tmpLNGIVOL_fama3_2 ...
                      tmpLNGIVOL_fama3_3 tmpLNGIVOL_fama3_4 ...
                      tmpLNGIVOL_fama3_5 tmpLNGIVOL_fama3_6 ...
                      tmpLNGIVOL_fama3_11 tmpLNGIVOL_fama3_12 ...
                      tmpLNGIVOL_fama3_13];
                  
        clear tmpLNGIVOL_fama3_1 tmpLNGIVOL_fama3_2 tmpLNGIVOL_fama3_3
        clear tmpLNGIVOL_fama3_4 tmpLNGIVOL_fama3_5 tmpLNGIVOL_fama3_6
        clear tmpLNGIVOL_fama3_11 tmpLNGIVOL_fama3_12 tmpLNGIVOL_fama3_13 
        clear tmptmpdata p0
    end
    clear tmpdata month0
    
    clear tmpLNG
    
    tmpout_LNG = [permnoall(i) * ones(size(tmpout_LNG,1),1) tmpout_LNG];
    
    tmpautocorrpara_LNG = [];
    lagc0 = 4;
    for lag = [1 2 3 4 5 6 11 12 13]
        regdata = tmpout_LNG(tmpout_LNG(:,3) ~= -9.9999 & ...
                  tmpout_LNG(:,lagc0) ~= -9.9999,:);
              
        if size(regdata,1) >= cut3;
           yyy0 = regdata(:,3);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           rlt0 = ols1(yyy0,xxx0);
           rho0 = corrcoef(yyy0,xxx0(:,2));
           rho0 = rho0(1,2);
           
           tmpautocorrpara_LNG = [tmpautocorrpara_LNG rho0];
        else
           tmpautocorrpara_LNG = [tmpautocorrpara_LNG -9.9999];
        end
        
        clear yyy0 xxx0 rlt0 rho0
        lagc0 = lagc0 + 1;
        
    end
    clear lagc0 lag 

    if meanIVOL_fama3 >0 & stdIVOL_fama3 ~= -9.9999
       CVIVOL_fama3 = stdIVOL_fama3 / meanIVOL_fama3;
    else
       CVIVOL_fama3 = -9.9999;
    end
    
    if meanLNGIVOL_fama3 >0 & stdLNGIVOL_fama3 ~= -9.9999
       CVLNGIVOL_fama3 = stdLNGIVOL_fama3 / meanLNGIVOL_fama3;
    else
       CVLNGIVOL_fama3 = -9.9999;
    end
    
    A_out_Table1     = [A_out_Table1;permnoall(i) nummonth ...
                        meanIVOL_fama3 stdIVOL_fama3 CVIVOL_fama3 ...
                        skewIVOL_fama3 tmpautocorrpara];
    A_out_Table1_LNG = [A_out_Table1_LNG;permnoall(i) nummonth ...
                        meanLNGIVOL_fama3 stdLNGIVOL_fama3 ...
                        CVLNGIVOL_fama3 skewLNGIVOL_fama3 ...
                        tmpautocorrpara_LNG];
    A_out_Table2     = [A_out_Table2;permnoall(i) nummonth tmpDFtest ...
                        tmpDFtest_LN];
    
    clear meanIVOL_fama3 stdIVOL_fama3 skewIVOL_fama3 tmpautocorrpara 
    clear CVIVOL_fama3 CVLNGIVOL_fama3
    clear meanLNGIVOL_fama3 stdLNGIVOL_fama3 skewLNGIVOL_fama3 
    clear tmpautocorrpara_LNG
    clear tmpDFtest 
    clear tmpout tmpout_LNG nummonth tmpDFtest_LN 
    
    end
end

% Save Data
    save Results_Table1_allstock_nonzero_2007_2017.mat...
         A_out_Table1 A_out_Table1_LNG A_out_Table1_LNG1
    save Results_Table2_allstock_nonzero_1963_2017.mat A_out_Table2

%}
% Sample period 1963 - 2017
%{

% Sample Peiord Extended to Dec 2017
data_fu2009  = data(data(:,2) <= 201712 & ...
              data(:,2) >= 196307 & data(:,5) >= 15,:);
data0        = data_fu2009;

ppp          = isnan(data0(:,3));
data0(ppp,:) = [];

permnoall    = unique(data0(:,1));
monthall     = unique(data0(:,2));


cut3=5;  % Minimum number of months to calculate autocorrelations
cut4=30; % Minimum number of months to test random walk process


DFTable = [ 25  49 -3.75;
            50  99 -3.59;
           100 249 -3.50;
           250 499 -3.45;
           500 999 -3.44];

     
A_out_Table1      = [];
A_out_Table1_LNG  = [];
A_out_Table1_LNG1 = [];
A_out_Table2      = [];

for i = 1:size(permnoall,1)
    
    tmpdata         = data0(data0(:,1) == permnoall(i),:);
    meanIVOL_fama3  = mean(tmpdata(:,3));
    
    if size(tmpdata,1) >= 2
    stdIVOL_fama3   = std(tmpdata(:,3));
    else
    stdIVOL_fama3   = -9.9999;  
    end
    
    if size(tmpdata,1)>= 3 & meanIVOL_fama3 > 0
    skewIVOL_fama3  = skewness(tmpdata(:,3),0);
    else
    skewIVOL_fama3  = -9.9999;    
    end
    
    month0          = unique(tmpdata(:,2));
    nummonth        = size(month0,1);
    
    % Stocks are traded on and after July 1963
    if max(month0) >= 196307
    tmpout = [];
    
    % Find lagged idiosyncratic volatility
    for j = 1:size(month0,1)
        tmptmpdata = tmpdata(tmpdata(:,2) == month0(j),[1 2 3]);
        
        p0 = find(monthall==month0(j));
        % Find IVOL (t-1)
        if p0 > 1
           month_1         = monthall(p0 - 1);
           tmpIVOL_fama3_1 = tmpdata(tmpdata(:,2) == month_1,3);
           if isempty(tmpIVOL_fama3_1) == 1
              tmpIVOL_fama3_1 = -9.9999;
           end
           clear month_1
        else
           tmpIVOL_fama3_1 = -9.9999; 
        end
        % Find IVOL (t-2)
        if p0 > 2
           month_2         = monthall(p0 - 2);
           tmpIVOL_fama3_2 = tmpdata(tmpdata(:,2) == month_2,3);
           if isempty(tmpIVOL_fama3_2) == 1
              tmpIVOL_fama3_2 = -9.9999;
           end
           clear month_2
        else
           tmpIVOL_fama3_2 = -9.9999; 
        end
        % Find IVOL (t-3)
        if p0 > 3
           month_3         = monthall(p0 - 3);
           tmpIVOL_fama3_3 = tmpdata(tmpdata(:,2) == month_3,3);
           if isempty(tmpIVOL_fama3_3) == 1
              tmpIVOL_fama3_3 = -9.9999;
           end
           clear month_3
        else
           tmpIVOL_fama3_3 =-9.9999; 
        end
        % Find IVOL (t-4)
        if p0 > 4
           month_4         = monthall(p0 - 4);
           tmpIVOL_fama3_4 = tmpdata(tmpdata(:,2) == month_4,3);
           if isempty(tmpIVOL_fama3_4) == 1
              tmpIVOL_fama3_4 = -9.9999;
           end
           clear month_4
        else
           tmpIVOL_fama3_4 = -9.9999; 
        end
        % Find IVOL (t-5)
        if p0 > 5
           month_5         = monthall(p0 - 5);
           tmpIVOL_fama3_5 = tmpdata(tmpdata(:,2) == month_5,3);
           if isempty(tmpIVOL_fama3_5) == 1
              tmpIVOL_fama3_5 = -9.9999;
           end
           clear month_5
        else
           tmpIVOL_fama3_5 = -9.9999; 
        end
        % Find IVOL (t-6)
        if p0 > 6
           month_6         = monthall(p0 - 6);
           tmpIVOL_fama3_6 = tmpdata(tmpdata(:,2) == month_6,3);
           if isempty(tmpIVOL_fama3_6) == 1
              tmpIVOL_fama3_6 = -9.9999;
           end
           clear month_6
        else
           tmpIVOL_fama3_6 = -9.9999; 
        end
        % Find IVOL (t-11)  
        if p0 > 11
           month_11         = monthall(p0 - 11);
           tmpIVOL_fama3_11 = tmpdata(tmpdata(:,2) == month_11,3);
           if isempty(tmpIVOL_fama3_11) == 1
              tmpIVOL_fama3_11 = -9.9999;
           end
           clear month_11
        else
           tmpIVOL_fama3_11 = -9.9999; 
        end
        % Find IVOL (t-12)  
        if p0 > 12
           month_12         =monthall(p0 - 12);
           tmpIVOL_fama3_12 = tmpdata(tmpdata(:,2) == month_12,3);
           if isempty(tmpIVOL_fama3_12) == 1
              tmpIVOL_fama3_12 = -9.9999;
           end
           clear month_12
        else
           tmpIVOL_fama3_12 = -9.9999; 
        end
        % Find IVOL (t-13)  
        if p0 > 13
           month_13         = monthall(p0 - 13);
           tmpIVOL_fama3_13 = tmpdata(tmpdata(:,2) == month_13,3);
           if isempty(tmpIVOL_fama3_13) == 1
              tmpIVOL_fama3_13 = -9.9999;
           end
           clear month_13
        else
           tmpIVOL_fama3_13 = -9.9999; 
        end  
        
        tmpout=[tmpout;tmptmpdata ...
                       tmpIVOL_fama3_1 tmpIVOL_fama3_2 tmpIVOL_fama3_3 ...
                       tmpIVOL_fama3_4 tmpIVOL_fama3_5 tmpIVOL_fama3_6 ...
                       tmpIVOL_fama3_11 tmpIVOL_fama3_12 tmpIVOL_fama3_13];
                   
        clear tmpIVOL_fama3_1 tmpIVOL_fama3_2 tmpIVOL_fama3_3 
        clear tmpIVOL_fama3_4 tmpIVOL_fama3_5 tmpIVOL_fama3_6
        clear tmpIVOL_fama3_11 tmpIVOL_fama3_12 tmpIVOL_fama3_13 p0
        clear tmptmpdata
    end
    
    tmp0 = tmpout(:,[2 3 4]);
    % Eliminate months with missing IVOL (t) or missing IVOL (t-1)
    tmp0 = tmp0(tmp0(:,2) ~= -9.9999 & tmp0(:,3) ~= -9.9999,:);
    if size(tmp0,1) > 0
       tmp0(tmp0(:,2) == 0 | tmp0(:,3) == 0,4) = -9.9999;
       tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,4) =...
            log(tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,2). / ...
            tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,3));
       tmpLNG = tmp0;
    else
       tmpLNG = [-9.9999 -9.9999 -9.9999 -9.9999];
    end
    clear tmp0
           

    tmpautocorrpara  = [];
    tmpDFtest        = [];
    tmpDFtest_LN     = [];
    sizeDFtestall    = [];
    sizeDFtest_LNall = [];
    
    lagc0 = 4;
    for lag = [1 2 3 4 5 6 11 12 13]
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                  tmpout(:,lagc0) ~= -9.9999,:);
              
        if size(regdata,1) >= cut3;
           yyy0 = regdata(:,3);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           rlt0 = ols1(yyy0,xxx0);
           rho0 = corrcoef(yyy0,xxx0(:,2));
           rho0 = rho0(1,2);
           tmpautocorrpara = [tmpautocorrpara rho0];
        else
           tmpautocorrpara = [tmpautocorrpara -9.9999];
        end
        
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                  tmpout(:,lagc0) ~= -9.9999,:);
        if lag == 1 & size(regdata,1) >= cut4;
           yyy0 = regdata(:,3) - regdata(:,lagc0);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           if std(yyy0) > 0 & std(xxx0(:,2)) > 0
              rlt0 = ols1(yyy0,xxx0);
           if size(regdata,1) >= DFTable(1,1) & ...
              size(regdata,1) <= DFTable(1,2)
            ind0 = (rlt0.tstat(2) <= DFTable(1,3));
           elseif size(regdata,1) >= DFTable(2,1) & ...
                  size(regdata,1) <= DFTable(2,2)
            ind0 = (rlt0.tstat(2) <= DFTable(2,3));
           elseif size(regdata,1) >= DFTable(3,1) & ...
                  size(regdata,1) <= DFTable(3,2)
            ind0 = (rlt0.tstat(2) <= DFTable(3,3));
           elseif size(regdata,1) >= DFTable(4,1) & ...
                  size(regdata,1) <= DFTable(4,2)
            ind0 = (rlt0.tstat(2) <= DFTable(4,3));
           elseif size(regdata,1) >= DFTable(5,1) & ...
                  size(regdata,1) <= DFTable(5,2)
            ind0 = (rlt0.tstat(2) <= DFTable(5,3));
           elseif size(regdata,1) <  DFTable(1,1)
            ind0 = -9.9999;          
           end
           
           tmpDFtest     = [tmpDFtest rlt0.beta(2) rlt0.tstat(2) ind0];
           else
           tmpDFtest     = [-9.9999 -9.9999 -9.9999];
           end
           
        elseif lag == 1 & size(regdata,1) < cut4
           tmpDFtest     = [-9.9999 -9.9999 -9.9999];
        end
        clear yyy0 xxx0 regdata rlt0 ind0 sizeDFtest
        
        
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                         tmpout(:,lagc0) ~= -9.9999 & ...
                         tmpout(:,3) > 0 & tmpout(:,lagc0) > 0,:);
        
        
        if lag == 1 & size(regdata,1) >= cut4;
           sizeDFtest_LN_2 = size(regdata,1);
           if size(regdata,1) >= cut4;
           yyy0 = log(regdata(:,3)) - log(regdata(:,lagc0));
           xxx0 = [ones(size(yyy0,1),1) log(regdata(:,lagc0))];
           if std(yyy0) > 0 & std(xxx0(:,2)) > 0
           rlt0 = ols1(yyy0,xxx0);
           if size(regdata,1) >= DFTable(1,1) & ...
              size(regdata,1) <= DFTable(1,2)
            ind0 = (rlt0.tstat(2) <= DFTable(1,3));
           elseif size(regdata,1) >= DFTable(2,1) & ...
                  size(regdata,1) <= DFTable(2,2)
            ind0 = (rlt0.tstat(2) <= DFTable(2,3));
           elseif size(regdata,1) >= DFTable(3,1) & ...
                  size(regdata,1) <= DFTable(3,2)
            ind0 = (rlt0.tstat(2) <= DFTable(3,3));
           elseif size(regdata,1) >= DFTable(4,1) & ...
                  size(regdata,1) <= DFTable(4,2)
            ind0 = (rlt0.tstat(2) <= DFTable(4,3));
           elseif size(regdata,1) >= DFTable(5,1) & ...
                  size(regdata,1) <= DFTable(5,2)
            ind0 = (rlt0.tstat(2) <= DFTable(5,3));
           elseif size(regdata,1) <  DFTable(1,1)
            ind0 = -9.9999;
           end
           
           tmpDFtest_LN = [tmpDFtest_LN rlt0.beta(2) rlt0.tstat(2) ind0];
           else
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
           end
           else
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
           end
        elseif lag == 1 & size(regdata,1) < cut4
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
        end
        clear yyy0 xxx0 regdata rlt0 ind0 sizeDFtest_LN 
        
        lagc0 = lagc0 + 1;
    end
    clear lagc0 lag
    
        
    tmp0 = tmpLNG(tmpLNG(:,4) ~= -9.9999,:);
    if size(tmp0,1) >= 1
       meanLNGIVOL_fama3 = mean(tmp0(:,4));
    else
       meanLNGIVOL_fama3 = -9.9999;  
    end
    if size(tmp0,1) >= 2
       stdLNGIVOL_fama3 = std(tmp0(:,4));
    else
       stdLNGIVOL_fama3 = -9.9999;  
    end
    if size(tmp0,1) >= 3 & stdLNGIVOL_fama3 > 0
       skewLNGIVOL_fama3 = skewness(tmp0(:,4),0);
    else
       skewLNGIVOL_fama3 = -9.9999;  
    end
    clear tmp0
           
    
    % Find lagged log idiosyncratic volatility growth rate
    tmpdata = tmpLNG;
    month0  = unique(tmpdata(:,1));
    
    tmpout_LNG = [];
    for j = 1:size(month0,1)
        tmptmpdata = tmpdata(tmpdata(:,1) == month0(j),[1 4]);
        p0 = find(monthall == month0(j));
        % Find LOGIVOL (t-1)
        if p0 > 1
           month_1 = monthall(p0 - 1);
           tmpLNGIVOL_fama3_1 = tmpdata(tmpdata(:,1) == month_1,4);
           if isempty(tmpLNGIVOL_fama3_1) == 1
              tmpLNGIVOL_fama3_1 = -9.9999;
           end
           clear month_1
        else
           tmpLNGIVOL_fama3_1 = -9.9999; 
        end
        % Find LOGIVOL (t-2)
        if p0 > 2
           month_2 = monthall(p0 - 2);
           tmpLNGIVOL_fama3_2 = tmpdata(tmpdata(:,1) == month_2,4);
           if isempty(tmpLNGIVOL_fama3_2) == 1
              tmpLNGIVOL_fama3_2 = -9.9999;
           end
           clear month_2
        else
           tmpLNGIVOL_fama3_2 = -9.9999; 
        end
        % Find LOGIVOL (t-3)
        if p0 > 3
           month_3 = monthall(p0 - 3);
           tmpLNGIVOL_fama3_3 = tmpdata(tmpdata(:,1) == month_3,4);
           if isempty(tmpLNGIVOL_fama3_3) == 1
              tmpLNGIVOL_fama3_3 = -9.9999;
           end
           clear month_3
        else
           tmpLNGIVOL_fama3_3 = -9.9999; 
        end
        % Find LOGIVOL (t-4)
        if p0 > 4
           month_4 = monthall(p0 - 4);
           tmpLNGIVOL_fama3_4 = tmpdata(tmpdata(:,1) == month_4,4);
           if isempty(tmpLNGIVOL_fama3_4) == 1
              tmpLNGIVOL_fama3_4 = -9.9999;
           end
           clear month_4
        else
           tmpLNGIVOL_fama3_4 = -9.9999; 
        end
        % Find LOGIVOL (t-5)
        if p0 > 5
           month_5 = monthall(p0 - 5);
           tmpLNGIVOL_fama3_5 = tmpdata(tmpdata(:,1) == month_5,4);
           if isempty(tmpLNGIVOL_fama3_5) == 1
              tmpLNGIVOL_fama3_5 = -9.9999;
           end
           clear month_5
        else
           tmpLNGIVOL_fama3_5 = -9.9999; 
        end
        % Find LOGIVOL (t-6)
        if p0 > 6
           month_6 = monthall(p0 - 6);
           tmpLNGIVOL_fama3_6 = tmpdata(tmpdata(:,1) == month_6,4);
           if isempty(tmpLNGIVOL_fama3_6) == 1
              tmpLNGIVOL_fama3_6 = -9.9999;
           end
           clear month_6
        else
           tmpLNGIVOL_fama3_6 = -9.9999; 
        end
        % Find LOGIVOL (t-11)
        if p0 > 11
           month_11 = monthall(p0 - 11);
           tmpLNGIVOL_fama3_11 = tmpdata(tmpdata(:,1) == month_11,4);
           if isempty(tmpLNGIVOL_fama3_11) == 1
              tmpLNGIVOL_fama3_11 = -9.9999;
           end
           clear month_11
        else
           tmpLNGIVOL_fama3_11 = -9.9999; 
        end
        % Find LOGIVOL (t-12)
        if p0 > 12
           month_12 = monthall(p0 - 12);
           tmpLNGIVOL_fama3_12 = tmpdata(tmpdata(:,1) == month_12,4);
           if isempty(tmpLNGIVOL_fama3_12) == 1
              tmpLNGIVOL_fama3_12 = -9.9999;
           end
           clear month_12
        else
           tmpLNGIVOL_fama3_12 = -9.9999; 
        end
        % Find LOGIVOL (t-13)
        if p0 > 13
           month_13 = monthall(p0 - 13);
           tmpLNGIVOL_fama3_13=tmpdata(tmpdata(:,1) == month_13,4);
           if isempty(tmpLNGIVOL_fama3_13) == 1
              tmpLNGIVOL_fama3_13 = -9.9999;
           end
           clear month_13
        else
           tmpLNGIVOL_fama3_13 = -9.9999; 
        end   
        
        tmpout_LNG = [tmpout_LNG;tmptmpdata ...
                      tmpLNGIVOL_fama3_1 tmpLNGIVOL_fama3_2 ...
                      tmpLNGIVOL_fama3_3 tmpLNGIVOL_fama3_4 ...
                      tmpLNGIVOL_fama3_5 tmpLNGIVOL_fama3_6 ...
                      tmpLNGIVOL_fama3_11 tmpLNGIVOL_fama3_12 ...
                      tmpLNGIVOL_fama3_13];
                  
        clear tmpLNGIVOL_fama3_1 tmpLNGIVOL_fama3_2 tmpLNGIVOL_fama3_3
        clear tmpLNGIVOL_fama3_4 tmpLNGIVOL_fama3_5 tmpLNGIVOL_fama3_6
        clear tmpLNGIVOL_fama3_11 tmpLNGIVOL_fama3_12 tmpLNGIVOL_fama3_13 
        clear tmptmpdata p0
    end
    clear tmpdata month0
    
    clear tmpLNG
    
    tmpout_LNG = [permnoall(i) * ones(size(tmpout_LNG,1),1) tmpout_LNG];
    
    tmpautocorrpara_LNG = [];
    lagc0 = 4;
    for lag = [1 2 3 4 5 6 11 12 13]
        regdata = tmpout_LNG(tmpout_LNG(:,3) ~= -9.9999 & ...
                  tmpout_LNG(:,lagc0) ~= -9.9999,:);
              
        if size(regdata,1) >= cut3;
           yyy0 = regdata(:,3);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           rlt0 = ols1(yyy0,xxx0);
           rho0 = corrcoef(yyy0,xxx0(:,2));
           rho0 = rho0(1,2);
           
           tmpautocorrpara_LNG = [tmpautocorrpara_LNG rho0];
        else
           tmpautocorrpara_LNG = [tmpautocorrpara_LNG -9.9999];
        end
        
        clear yyy0 xxx0 rlt0 rho0
        lagc0 = lagc0 + 1;
        
    end
    clear lagc0 lag 

    if meanIVOL_fama3 > 0 & stdIVOL_fama3 ~= -9.9999
       CVIVOL_fama3 = stdIVOL_fama3 / meanIVOL_fama3;
    else
       CVIVOL_fama3 = -9.9999;
    end
    
    if meanLNGIVOL_fama3 > 0 & stdLNGIVOL_fama3 ~= -9.9999
       CVLNGIVOL_fama3 = stdLNGIVOL_fama3 / meanLNGIVOL_fama3;
    else
       CVLNGIVOL_fama3 = -9.9999;
    end
    
    A_out_Table1     = [A_out_Table1;permnoall(i) nummonth ...
                        meanIVOL_fama3 stdIVOL_fama3 CVIVOL_fama3 ...
                        skewIVOL_fama3 tmpautocorrpara];
    A_out_Table1_LNG = [A_out_Table1_LNG;permnoall(i) nummonth ...
                        meanLNGIVOL_fama3 stdLNGIVOL_fama3 ...
                        CVLNGIVOL_fama3 skewLNGIVOL_fama3 ...
                        tmpautocorrpara_LNG];
    A_out_Table2     = [A_out_Table2;permnoall(i) nummonth tmpDFtest ...
                        tmpDFtest_LN];
    
    clear meanIVOL_fama3 stdIVOL_fama3 skewIVOL_fama3 tmpautocorrpara 
    clear CVIVOL_fama3 CVLNGIVOL_fama3
    clear meanLNGIVOL_fama3 stdLNGIVOL_fama3 skewLNGIVOL_fama3 
    clear tmpautocorrpara_LNG
    clear tmpDFtest 
    clear tmpout tmpout_LNG nummonth tmpDFtest_LN 
    
    end
end

% Save Data
    save Results_Table1_allstock_nonzero_1963_2017.mat...
         A_out_Table1 A_out_Table1_LNG A_out_Table1_LNG1
    save Results_Table2_allstock_nonzero_1963_2017.mat A_out_Table2

%}
% Sample period 1926 - 1963
%{

% Sample Period before that of Fu (2009)
data_fu2009  = data(data(:,2) <= 196306 & ...
              data(:,2) >= 192607 & data(:,5) >= 15,:);
data0        = data_fu2009;

ppp          = isnan(data0(:,3));
data0(ppp,:) = [];

permnoall    = unique(data0(:,1));
monthall     = unique(data0(:,2));


cut3=5;  % Minimum number of months to calculate autocorrelations
cut4=30; % Minimum number of months to test random walk process


DFTable = [ 25  49 -3.75;
            50  99 -3.59;
           100 249 -3.50;
           250 499 -3.45;
           500 999 -3.44];

     
A_out_Table1      = [];
A_out_Table1_LNG  = [];
A_out_Table1_LNG1 = [];
A_out_Table2      = [];

for i = 1:size(permnoall,1)
    
    tmpdata         = data0(data0(:,1) == permnoall(i),:);
    meanIVOL_fama3  = mean(tmpdata(:,3));
    
    if size(tmpdata,1) >= 2
    stdIVOL_fama3   = std(tmpdata(:,3));
    else
    stdIVOL_fama3   = -9.9999;  
    end
    
    if size(tmpdata,1)>= 3 & meanIVOL_fama3 > 0
    skewIVOL_fama3  = skewness(tmpdata(:,3),0);
    else
    skewIVOL_fama3  = -9.9999;    
    end
    
    month0          = unique(tmpdata(:,2));
    nummonth        = size(month0,1);
    
    % Stocks are traded on and after July 1963
    if max(month0) >= 196307
        
    tmpout = [];
    % Find lagged idiosyncratic volatility
    for j = 1:size(month0,1)
        tmptmpdata = tmpdata(tmpdata(:,2) == month0(j),[1 2 3]);
        
        p0 = find(monthall == month0(j));
        % Find IVOL (t-1)
        if p0 > 1
           month_1         = monthall(p0 - 1);
           tmpIVOL_fama3_1 = tmpdata(tmpdata(:,2) == month_1,3);
           if isempty(tmpIVOL_fama3_1) == 1
              tmpIVOL_fama3_1 = -9.9999;
           end
           clear month_1
        else
           tmpIVOL_fama3_1 = -9.9999; 
        end
        % Find IVOL (t-2)
        if p0 > 2
           month_2         = monthall(p0 - 2);
           tmpIVOL_fama3_2 = tmpdata(tmpdata(:,2) == month_2,3);
           if isempty(tmpIVOL_fama3_2) == 1
              tmpIVOL_fama3_2 = -9.9999;
           end
           clear month_2
        else
           tmpIVOL_fama3_2 = -9.9999; 
        end
        % Find IVOL (t-3)
        if p0 > 3
           month_3         = monthall(p0 - 3);
           tmpIVOL_fama3_3 = tmpdata(tmpdata(:,2) == month_3,3);
           if isempty(tmpIVOL_fama3_3) == 1
              tmpIVOL_fama3_3 = -9.9999;
           end
           clear month_3
        else
           tmpIVOL_fama3_3 =-9.9999; 
        end
        % Find IVOL (t-4)
        if p0 > 4
           month_4         = monthall(p0 - 4);
           tmpIVOL_fama3_4 = tmpdata(tmpdata(:,2) == month_4,3);
           if isempty(tmpIVOL_fama3_4) == 1
              tmpIVOL_fama3_4 = -9.9999;
           end
           clear month_4
        else
           tmpIVOL_fama3_4 = -9.9999; 
        end
        % Find IVOL (t-5)
        if p0 > 5
           month_5         = monthall(p0 - 5);
           tmpIVOL_fama3_5 = tmpdata(tmpdata(:,2) == month_5,3);
           if isempty(tmpIVOL_fama3_5) == 1
              tmpIVOL_fama3_5 = -9.9999;
           end
           clear month_5
        else
           tmpIVOL_fama3_5 = -9.9999; 
        end
        % Find IVOL (t-6)
        if p0 > 6
           month_6         = monthall(p0 - 6);
           tmpIVOL_fama3_6 = tmpdata(tmpdata(:,2) == month_6,3);
           if isempty(tmpIVOL_fama3_6) == 1
              tmpIVOL_fama3_6 = -9.9999;
           end
           clear month_6
        else
           tmpIVOL_fama3_6 = -9.9999; 
        end
        % Find IVOL (t-11)  
        if p0 > 11
           month_11         = monthall(p0 - 11);
           tmpIVOL_fama3_11 = tmpdata(tmpdata(:,2) == month_11,3);
           if isempty(tmpIVOL_fama3_11) == 1
              tmpIVOL_fama3_11 = -9.9999;
           end
           clear month_11
        else
           tmpIVOL_fama3_11 = -9.9999; 
        end
        % Find IVOL (t-12)  
        if p0 > 12
           month_12         = monthall(p0 - 12);
           tmpIVOL_fama3_12 = tmpdata(tmpdata(:,2) == month_12,3);
           if isempty(tmpIVOL_fama3_12) == 1
              tmpIVOL_fama3_12 = -9.9999;
           end
           clear month_12
        else
           tmpIVOL_fama3_12 = -9.9999; 
        end
        % Find IVOL (t-13)  
        if p0 > 13
           month_13         = monthall(p0 - 13);
           tmpIVOL_fama3_13 = tmpdata(tmpdata(:,2) == month_13,3);
           if isempty(tmpIVOL_fama3_13) == 1
              tmpIVOL_fama3_13 = -9.9999;
           end
           clear month_13
        else
           tmpIVOL_fama3_13 = -9.9999; 
        end  
        
        tmpout=[tmpout;tmptmpdata ...
                       tmpIVOL_fama3_1 tmpIVOL_fama3_2 tmpIVOL_fama3_3 ...
                       tmpIVOL_fama3_4 tmpIVOL_fama3_5 tmpIVOL_fama3_6 ...
                       tmpIVOL_fama3_11 tmpIVOL_fama3_12 tmpIVOL_fama3_13];
                   
        clear tmpIVOL_fama3_1 tmpIVOL_fama3_2 tmpIVOL_fama3_3 
        clear tmpIVOL_fama3_4 tmpIVOL_fama3_5 tmpIVOL_fama3_6
        clear tmpIVOL_fama3_11 tmpIVOL_fama3_12 tmpIVOL_fama3_13 p0
        clear tmptmpdata
    end
    
    tmp0 = tmpout(:,[2 3 4]);
    % Eliminate months with missing IVOL (t) or missing IVOL (t-1)
    tmp0 = tmp0(tmp0(:,2) ~= -9.9999 & tmp0(:,3) ~= -9.9999,:);
    if size(tmp0,1)>0
       tmp0(tmp0(:,2) == 0 | tmp0(:,3) == 0,4) = -9.9999;
       tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,4) = ...
            log(tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,2). / ...
            tmp0(tmp0(:,2) > 0 & tmp0(:,3) > 0,3));
       tmpLNG = tmp0;
    else
       tmpLNG = [-9.9999 -9.9999 -9.9999 -9.9999];
    end
    clear tmp0
           

    tmpautocorrpara  = [];
    tmpDFtest        = [];
    tmpDFtest_LN     = [];
    
    lagc0 = 4;
    for lag = [1 2 3 4 5 6 11 12 13]
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                  tmpout(:,lagc0) ~= -9.9999,:);
              
        if size(regdata,1) >= cut3
           yyy0 = regdata(:,3);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           rlt0 = ols1(yyy0,xxx0);
           rho0 = corrcoef(yyy0,xxx0(:,2));
           rho0 = rho0(1,2);
           tmpautocorrpara = [tmpautocorrpara rho0];
        else
           tmpautocorrpara = [tmpautocorrpara -9.9999];
        end
        
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                  tmpout(:,lagc0) ~= -9.9999,:);
        if lag == 1 & size(regdata,1) >= cut4;
           yyy0 = regdata(:,3) - regdata(:,lagc0);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           if std(yyy0) > 0 & std(xxx0(:,2)) > 0
              rlt0 = ols1(yyy0,xxx0);
           if size(regdata,1) >= DFTable(1,1) & ...
              size(regdata,1) <= DFTable(1,2)
            ind0 = (rlt0.tstat(2) <= DFTable(1,3));
           elseif size(regdata,1) >= DFTable(2,1) & ...
                  size(regdata,1) <= DFTable(2,2)
            ind0 = (rlt0.tstat(2) <= DFTable(2,3));
           elseif size(regdata,1) >= DFTable(3,1) & ...
                  size(regdata,1) <= DFTable(3,2)
            ind0 = (rlt0.tstat(2) <= DFTable(3,3));
           elseif size(regdata,1) >= DFTable(4,1) & ...
                  size(regdata,1) <= DFTable(4,2)
            ind0 = (rlt0.tstat(2) <= DFTable(4,3));
           elseif size(regdata,1) >= DFTable(5,1) & ...
                  size(regdata,1) <= DFTable(5,2)
            ind0 = (rlt0.tstat(2) <= DFTable(5,3));
           elseif size(regdata,1) <  DFTable(1,1)
            ind0 = -9.9999;          
           end
           
           tmpDFtest     = [tmpDFtest rlt0.beta(2) rlt0.tstat(2) ind0];
           else
           tmpDFtest     = [-9.9999 -9.9999 -9.9999];
           end
           
        elseif lag == 1 & size(regdata,1) < cut4
           tmpDFtest     = [-9.9999 -9.9999 -9.9999];
        end
        clear yyy0 xxx0 regdata rlt0 ind0 sizeDFtest
        
        
        regdata = tmpout(tmpout(:,3) ~= -9.9999 & ...
                         tmpout(:,lagc0) ~= -9.9999 & ...
                         tmpout(:,3) > 0 & tmpout(:,lagc0) > 0,:);
        
        
        if lag == 1 & size(regdata,1) >= cut4;
           sizeDFtest_LN_2 = size(regdata,1);
           if size(regdata,1) >= cut4;
           yyy0 = log(regdata(:,3)) - log(regdata(:,lagc0));
           xxx0 = [ones(size(yyy0,1),1) log(regdata(:,lagc0))];
           if std(yyy0) > 0 & std(xxx0(:,2)) > 0
           rlt0 = ols1(yyy0,xxx0);
           if size(regdata,1) >= DFTable(1,1) & ...
              size(regdata,1) <= DFTable(1,2)
            ind0 = (rlt0.tstat(2) <= DFTable(1,3));
           elseif size(regdata,1) >= DFTable(2,1) & ...
                  size(regdata,1) <= DFTable(2,2)
            ind0 = (rlt0.tstat(2) <= DFTable(2,3));
           elseif size(regdata,1) >= DFTable(3,1) & ...
                  size(regdata,1) <= DFTable(3,2)
            ind0 = (rlt0.tstat(2) <= DFTable(3,3));
           elseif size(regdata,1) >= DFTable(4,1) & ...
                  size(regdata,1) <= DFTable(4,2)
            ind0 = (rlt0.tstat(2) <= DFTable(4,3));
           elseif size(regdata,1) >= DFTable(5,1) & ...
                  size(regdata,1) <= DFTable(5,2)
            ind0 = (rlt0.tstat(2) <= DFTable(5,3));
           elseif size(regdata,1) <  DFTable(1,1)
            ind0 = -9.9999;
           end
           
           tmpDFtest_LN = [tmpDFtest_LN rlt0.beta(2) rlt0.tstat(2) ind0];
           else
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
           end
           else
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
           end
        elseif lag == 1 & size(regdata,1) < cut4
           tmpDFtest_LN = [-9.9999 -9.9999 -9.9999];
        end
        clear yyy0 xxx0 regdata rlt0 ind0 sizeDFtest_LN 
        
        lagc0 = lagc0 + 1;
    end
    clear lagc0 lag
    
        
    tmp0 = tmpLNG(tmpLNG(:,4) ~= -9.9999,:);
    if size(tmp0,1) >= 1
       meanLNGIVOL_fama3 = mean(tmp0(:,4));
    else
       meanLNGIVOL_fama3 = -9.9999;  
    end
    if size(tmp0,1) >= 2
       stdLNGIVOL_fama3 = std(tmp0(:,4));
    else
       stdLNGIVOL_fama3 = -9.9999;  
    end
    if size(tmp0,1) >= 3 & stdLNGIVOL_fama3 > 0
       skewLNGIVOL_fama3 = skewness(tmp0(:,4),0);
    else
       skewLNGIVOL_fama3 = -9.9999;  
    end
    clear tmp0
           
    
    % Find lagged log idiosyncratic volatility growth rate
    tmpdata = tmpLNG;
    month0  = unique(tmpdata(:,1));
    
    tmpout_LNG = [];
    for j = 1:size(month0,1)
        tmptmpdata = tmpdata(tmpdata(:,1) == month0(j),[1 4]);
        p0 = find(monthall == month0(j));
        % Find LOGIVOL (t-1)
        if p0 > 1
           month_1 = monthall(p0 - 1);
           tmpLNGIVOL_fama3_1 = tmpdata(tmpdata(:,1) == month_1,4);
           if isempty(tmpLNGIVOL_fama3_1) == 1
              tmpLNGIVOL_fama3_1 = -9.9999;
           end
           clear month_1
        else
           tmpLNGIVOL_fama3_1 = -9.9999; 
        end
        % Find LOGIVOL (t-2)
        if p0 > 2
           month_2 = monthall(p0 - 2);
           tmpLNGIVOL_fama3_2 = tmpdata(tmpdata(:,1) == month_2,4);
           if isempty(tmpLNGIVOL_fama3_2) == 1
              tmpLNGIVOL_fama3_2 = -9.9999;
           end
           clear month_2
        else
           tmpLNGIVOL_fama3_2 = -9.9999; 
        end
        % Find LOGIVOL (t-3)
        if p0 > 3
           month_3 = monthall(p0 - 3);
           tmpLNGIVOL_fama3_3 = tmpdata(tmpdata(:,1) == month_3,4);
           if isempty(tmpLNGIVOL_fama3_3) == 1
              tmpLNGIVOL_fama3_3 = -9.9999;
           end
           clear month_3
        else
           tmpLNGIVOL_fama3_3 = -9.9999; 
        end
        % Find LOGIVOL (t-4)
        if p0 > 4
           month_4 = monthall(p0 - 4);
           tmpLNGIVOL_fama3_4 = tmpdata(tmpdata(:,1) == month_4,4);
           if isempty(tmpLNGIVOL_fama3_4) == 1
              tmpLNGIVOL_fama3_4 = -9.9999;
           end
           clear month_4
        else
           tmpLNGIVOL_fama3_4 = -9.9999; 
        end
        % Find LOGIVOL (t-5)
        if p0 > 5
           month_5 = monthall(p0 - 5);
           tmpLNGIVOL_fama3_5 = tmpdata(tmpdata(:,1) == month_5,4);
           if isempty(tmpLNGIVOL_fama3_5) == 1
              tmpLNGIVOL_fama3_5 = -9.9999;
           end
           clear month_5
        else
           tmpLNGIVOL_fama3_5 = -9.9999; 
        end
        % Find LOGIVOL (t-6)
        if p0 > 6
           month_6 = monthall(p0 - 6);
           tmpLNGIVOL_fama3_6 = tmpdata(tmpdata(:,1) == month_6,4);
           if isempty(tmpLNGIVOL_fama3_6) == 1
              tmpLNGIVOL_fama3_6 = -9.9999;
           end
           clear month_6
        else
           tmpLNGIVOL_fama3_6 = -9.9999; 
        end
        % Find LOGIVOL (t-11)
        if p0 > 11
           month_11 = monthall(p0 - 11);
           tmpLNGIVOL_fama3_11 = tmpdata(tmpdata(:,1) == month_11,4);
           if isempty(tmpLNGIVOL_fama3_11) == 1
              tmpLNGIVOL_fama3_11 = -9.9999;
           end
           clear month_11
        else
           tmpLNGIVOL_fama3_11 = -9.9999; 
        end
        % Find LOGIVOL (t-12)
        if p0 > 12
           month_12 = monthall(p0 - 12);
           tmpLNGIVOL_fama3_12 = tmpdata(tmpdata(:,1) == month_12,4);
           if isempty(tmpLNGIVOL_fama3_12) == 1
              tmpLNGIVOL_fama3_12 = -9.9999;
           end
           clear month_12
        else
           tmpLNGIVOL_fama3_12 = -9.9999; 
        end
        % Find LOGIVOL (t-13)
        if p0 > 13
           month_13 = monthall(p0 - 13);
           tmpLNGIVOL_fama3_13=tmpdata(tmpdata(:,1) == month_13,4);
           if isempty(tmpLNGIVOL_fama3_13) == 1
              tmpLNGIVOL_fama3_13 = -9.9999;
           end
           clear month_13
        else
           tmpLNGIVOL_fama3_13 = -9.9999; 
        end   
        
        tmpout_LNG = [tmpout_LNG;tmptmpdata ...
                      tmpLNGIVOL_fama3_1 tmpLNGIVOL_fama3_2 ...
                      tmpLNGIVOL_fama3_3 tmpLNGIVOL_fama3_4 ...
                      tmpLNGIVOL_fama3_5 tmpLNGIVOL_fama3_6 ...
                      tmpLNGIVOL_fama3_11 tmpLNGIVOL_fama3_12 ...
                      tmpLNGIVOL_fama3_13];
                  
        clear tmpLNGIVOL_fama3_1 tmpLNGIVOL_fama3_2 tmpLNGIVOL_fama3_3
        clear tmpLNGIVOL_fama3_4 tmpLNGIVOL_fama3_5 tmpLNGIVOL_fama3_6
        clear tmpLNGIVOL_fama3_11 tmpLNGIVOL_fama3_12 tmpLNGIVOL_fama3_13 
        clear tmptmpdata p0
    end
    clear tmpdata month0
    
    clear tmpLNG
    
    tmpout_LNG = [permnoall(i) * ones(size(tmpout_LNG,1),1) tmpout_LNG];
    
    tmpautocorrpara_LNG = [];
    lagc0 = 4;
    for lag = [1 2 3 4 5 6 11 12 13]
        regdata = tmpout_LNG(tmpout_LNG(:,3) ~= -9.9999 & ...
                  tmpout_LNG(:,lagc0) ~= -9.9999,:);
              
        if size(regdata,1) >= cut3;
           yyy0 = regdata(:,3);
           xxx0 = [ones(size(yyy0,1),1) regdata(:,lagc0)];
           rlt0 = ols1(yyy0,xxx0);
           rho0 = corrcoef(yyy0,xxx0(:,2));
           rho0 = rho0(1,2);
           
           tmpautocorrpara_LNG = [tmpautocorrpara_LNG rho0];
        else
           tmpautocorrpara_LNG = [tmpautocorrpara_LNG -9.9999];
        end
        
        clear yyy0 xxx0 rlt0 rho0
        lagc0 = lagc0 + 1;
        
    end
    clear lagc0 lag 

    if meanIVOL_fama3 > 0 & stdIVOL_fama3 ~= -9.9999
       CVIVOL_fama3 = stdIVOL_fama3 / meanIVOL_fama3;
    else
       CVIVOL_fama3 = -9.9999;
    end
    
    if meanLNGIVOL_fama3 > 0 & stdLNGIVOL_fama3 ~= -9.9999
       CVLNGIVOL_fama3 = stdLNGIVOL_fama3 / meanLNGIVOL_fama3;
    else
       CVLNGIVOL_fama3 = -9.9999;
    end
    
    A_out_Table1     = [A_out_Table1;permnoall(i) nummonth ...
                        meanIVOL_fama3 stdIVOL_fama3 CVIVOL_fama3 ...
                        skewIVOL_fama3 tmpautocorrpara];
    A_out_Table1_LNG = [A_out_Table1_LNG;permnoall(i) nummonth ...
                        meanLNGIVOL_fama3 stdLNGIVOL_fama3 ...
                        CVLNGIVOL_fama3 skewLNGIVOL_fama3 ...
                        tmpautocorrpara_LNG];
    A_out_Table2     = [A_out_Table2;permnoall(i) nummonth tmpDFtest ...
                        tmpDFtest_LN];
    
    clear meanIVOL_fama3 stdIVOL_fama3 skewIVOL_fama3 tmpautocorrpara 
    clear CVIVOL_fama3 CVLNGIVOL_fama3
    clear meanLNGIVOL_fama3 stdLNGIVOL_fama3 skewLNGIVOL_fama3 
    clear tmpautocorrpara_LNG
    clear tmpDFtest 
    clear tmpout tmpout_LNG nummonth tmpDFtest_LN 
    end
end

% Save Data
    save Results_Table1_allstock_nonzero_1926_1963.mat...
         A_out_Table1 A_out_Table1_LNG A_out_Table1_LNG1
    save Results_Table2_allstock_nonzero_1926_1963.mat A_out_Table2

%}
% ------------------------------------------------------------------------%


% ------------------------------ Table 1 ---------------------------------%
% Table 1: mean, std, skewness and autocorrelation of IVOL
% ------------------------------ Table 2 ---------------------------------%
% Table 2: test of random walk process of IVOL

% Table 1 and Table 2 Data Description
%{
Data: A_out_Table1
No.VarName      Description
1  PERMNO	    Permno Number
2  nummonth     Number of month
3  meanIVOL     Mean of monthly IVOL
4  stdIVOL      Std of monthly IVOL
5  CVIVOL       CV of monthly IVOL   
6  skewIVOL     Skewness of monthly IVOL
7  autolag1     1 lag autocorrelation of IVOL
8  autolag2     2 lag autocorrelation of IVOL
9  autolag3     3 lag autocorrelation of IVOL
10 autolag4     4 lag autocorrelation of IVOL
11 autolag5     5 lag autocorrelation of IVOL
12 autolag6     6 lag autocorrelation of IVOL
13 autolag11    11 lag autocorrelation of IVOL
14 autolag12    12 lag autocorrelation of IVOL
15 autolag13    13 lag autocorrelation of IVOL

Data: A_out_Table1_LNG
No.VarName      Description
1  PERMNO	    Permno Number
2  nummonth     Number of month
3  meanLOGIVOL  Mean of monthly LOGIVOL
4  stdLOGIVOL   Std of monthly LOGIVOL
5  CVLOGIVOL    CV of monthly LOGIVOL   
6  skewLOGIVOL  Skewness of monthly LOGIVOL
7  autolag1     1 lag autocorrelation of LOGIVOL
8  autolag2     2 lag autocorrelation of LOGIVOL
9  autolag3     3 lag autocorrelation of LOGIVOL
10 autolag4     4 lag autocorrelation of LOGIVOL
11 autolag5     5 lag autocorrelation of LOGIVOL
12 autolag6     6 lag autocorrelation of LOGIVOL
13 autolag11    11 lag autocorrelation of LOGIVOL
14 autolag12    12 lag autocorrelation of LOGIVOL
15 autolag13    13 lag autocorrelation of LOGIVOL

Data: A_out_Table2
No.VarName      Description
1  PERMNO	    Permno Number
2  nummonth     Number of month
3  gamma        coefficient estimate of random walk process (IVOL)
4  tstat        tstat of gamma (IVOL)
5  indreject    Indicator of whether random walk process hypothesis 
                if rejected (IVOL)
                ( 0 = not reject; 1 = reject)
6  gamma        coefficient estimate of random walk process (LOGIVOL)
7  tstat        tstat of gamma (LOGIVOL)
8  indreject    Indicator of whether random walk process hypothesis 
                if rejected (LOGIVOL)
                ( 0 = not reject; 1 = reject)
%}
% Table 1 and Table 2
%{

clear
clc

SamplePeriod = 1;

% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963


if SamplePeriod == 1
   load Results_Table1_allstock_nonzero_1963_2006.mat...
        A_out_Table1 A_out_Table1_LNG
   load Results_Table2_allstock_nonzero_1963_2006.mat...
        A_out_Table2
elseif SamplePeriod == 2
   load Results_Table1_allstock_nonzero_2007_2017.mat...
        A_out_Table1 A_out_Table1_LNG
   load Results_Table2_allstock_nonzero_2007_2017.mat...
        A_out_Table2
elseif SamplePeriod == 3
   load Results_Table1_allstock_nonzero_1963_2017.mat...
        A_out_Table1 A_out_Table1_LNG
   load Results_Table2_allstock_nonzero_1963_2017.mat...
        A_out_Table2
elseif SamplePeriod == 4
   load Results_Table1_allstock_nonzero_1926_1963.mat...
        A_out_Table1 A_out_Table1_LNG
   load Results_Table2_allstock_nonzero_1926_1963.mat...
        A_out_Table2
end


A_Table1        = zeros(2,14);
A_Table2_model1 = zeros(2,6);
A_Table2_model2 = zeros(2,6);

datastat  = A_out_Table1;
datastat1 = datastat(datastat(:,3) ~= -9.9999,:);
permnoall = unique(datastat1(:,1));

% Number of stocks with valid mean IVOL
A_Table1(1,1) = size(permnoall,1);
clear permnoall datastat1

Tcolumn = 2;
for ccc = 3:15
    
    % Eliminate missing value 
    tmp = datastat(datastat(:,ccc) ~= -9.9999,:);
    ppp = isnan(tmp(:,ccc));
    tmp(ppp,:) = [];
    
    % Mean and std of IVOL are reported in percentage
    if ccc == 3 | ccc == 4
       tmp(:,ccc) = tmp(:,ccc) * 100;
    end
    
    A_Table1(1,Tcolumn) = mean(tmp(:,ccc));
    clear tmp
    Tcolumn = Tcolumn + 1;
end
clear datastat 

datastat  = A_out_Table1_LNG;
datastat1 = datastat(datastat(:,3) ~= -9.9999,:);
permnoall = unique(datastat1(:,1));

% Number of stocks with valid mean LOGIVOL
A_Table1(2,1) = size(permnoall,1);
clear permnoall datastat1

Tcolumn = 2;
for ccc = 3:15
    
    % Eliminate missing value 
    tmp = datastat(datastat(:,ccc) ~= -9.9999,:);
    ppp = isnan(tmp(:,ccc));
    tmp(ppp,:) = [];
    
    % Mean and std of IVOL are reported in percentage
    if ccc == 3 | ccc == 4
       tmp(:,ccc) = tmp(:,ccc) * 100;
    end
    
    A_Table1(2,Tcolumn) = mean(tmp(:,ccc));
    clear tmp
    Tcolumn = Tcolumn + 1;
end
clear datastat 

datastat  = A_out_Table2;
datastat1 = datastat(datastat(:,3) ~= -9.9999,:);
permnoall = unique(datastat1(:,1));

% Number of stocks with valid gama
A_Table2_model1(1,1) = size(permnoall,1);
A_Table2_model1(2,1) = size(permnoall,1);

for ccc = [3]
    
    % Eliminate missing value 
    tmp = datastat(datastat(:,ccc) ~= -9.9999,:);
    ppp = isnan(tmp(:,ccc));
    tmp(ppp,:) = [];
    
    A_Table2_model1(1,2) = mean(tmp(:,ccc));
    A_Table2_model1(1,3) = median(tmp(:,ccc));
    A_Table2_model1(1,4) = prctile(tmp(:,ccc),25);
    A_Table2_model1(1,5) = prctile(tmp(:,ccc),75);
    
    tmp = datastat(datastat(:,ccc + 1) ~= -9.9999,:);
    ppp = isnan(tmp(:,ccc + 1));
    tmp(ppp,:) =[];
    
    A_Table2_model1(2,2) = mean(tmp(:,ccc + 1));
    A_Table2_model1(2,3) = prctile(tmp(:,ccc + 1),50);
    A_Table2_model1(2,4) = prctile(tmp(:,ccc + 1),25);
    A_Table2_model1(2,5) = prctile(tmp(:,ccc + 1),75);
    
    tmp = datastat(datastat(:,ccc + 2) ~= -9.9999,:);
    
    A_Table2_model1(1,6) = sum(tmp(:,ccc + 2)) / size(tmp,1) * 100;
    clear tmp
end
clear datastat

datastat  = A_out_Table2;
datastat1 = datastat(datastat(:,6) ~= -9.9999,:);
permnoall = unique(datastat1(:,1));

% Number of stocks with valid gama
A_Table2_model2(1,1) = size(permnoall,1);
A_Table2_model2(2,1) = size(permnoall,1);

for ccc = [6]
    
    % Eliminate missing value 
    tmp = datastat(datastat(:,ccc) ~= -9.9999,:);
    ppp = isnan(tmp(:,ccc));
    tmp(ppp,:) =[];
    
    A_Table2_model2(1,2) = mean(tmp(:,ccc));
    A_Table2_model2(1,3) = prctile(tmp(:,ccc),50);
    A_Table2_model2(1,4) = prctile(tmp(:,ccc),25);
    A_Table2_model2(1,5) = prctile(tmp(:,ccc),75);
    
    tmp = datastat(datastat(:,ccc + 1) ~= -9.9999,:);
    ppp = isnan(tmp(:,ccc + 1));
    tmp(ppp,:) = [];
        
    A_Table2_model2(2,2) = mean(tmp(:,ccc + 1));
    A_Table2_model2(2,3) = prctile(tmp(:,ccc + 1),25);
    A_Table2_model2(2,4) = prctile(tmp(:,ccc + 1),50);
    A_Table2_model2(2,5) = prctile(tmp(:,ccc + 1),75);
    
    tmp = datastat(datastat(:,ccc + 2) ~= -9.9999,:);

    A_Table2_model2(1,6) = sum(tmp(:,ccc + 2)) / size(tmp,1) * 100;
    clear tmp
end

%}
% ------------------------------------------------------------------------%

% Table 3 Summary Statistics - Pooled Data
% ------------------------------- Table 3 --------------------------------%
% Match data
%{

clear
clc

SamplePeriod = 1;
% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963

data = [];
for num0 = 1:9
load(['tmpoutresid' num2str(num0) '_allstocks_nonzero'  '.mat'])
data = [data;Aout_resid];
clear Aout_resid
end
clear num0
dataIVOL = data;
clear data
ppp = isnan(dataIVOL(:,3));
dataIVOL(ppp,:) = [];
clear ppp 
dataIVOL = dataIVOL(:,[1 2 3 4]);

data = [];
for num0 = 1:9
load(['tmp_EstIdioV_' num2str(num0) '_19262017_allstocks_allstocks'  '.mat'])
data = [data;Aout_EstIdioV_method3_allstocks];
clear Aout_EstIdioV_method3_allstocks
end
dataEIVOL_19262017 = data;
clear data

data = [];
for num0 = 1:9
load(['tmp_EstIdioV_' num2str(num0) '_19262017_allstocks_allstocks_sqp'  '.mat'])
data = [data;Aout_EstIdioV_method3_allstocks_sqp];
clear Aout_EstIdioV_method3_allstocks_sqp
end
dataEIVOL_19262017_sqp = data;

data = [];
for num0 = 1:9
load(['tmp_EstIdioV_' num2str(num0) '_19262017_allstocks_allstocks_Roll'  '.mat'])
data = [data;Aout_EstIdioV_method3_allstocks];
clear Aout_EstIdioV_method3_allstocks
end
dataEIVOL_Roll_19262017 = data;
clear data

data = [];
for num0 = 1:9
load(['tmp_EstIdioV_' num2str(num0) '_19262017_allstocks_allstocks_sqp_Roll'  '.mat'])
data = [data;Aout_EstIdioV_method3_allstocks_sqp];
clear Aout_EstIdioV_method3_allstocks_sqp
end
dataEIVOL_Roll_19262017_sqp = data;


data = [];
for num0 = 1:9 
load(['tmp_BETA_TURN_pastR_allstocks_1963_2017_' num2str(num0) '.mat'])
data = [data;A_out];
clear A_out
end
clear num0
dataBETA = data;
clear data


data = [];
for num0 = 1:9
load(['tmp_ICC_' num2str(num0) '_UnAdj_allstocks.mat'])
data = [data;A_outmatch];
clear A_outmatch
end
clear num0
dataICC = data;
clear data

data = [];
for num0 = 1:9
load(['tmpoutresid' num2str(num0) '_allstocks_otherIVOL'  '.mat'])
data = [data;Aout_resid];
clear Aout_resid
end
clear num0
dataIVOL_other_daily = data;
clear data

data = [];
for num0 = 1:9
load(['tmpoutresid' num2str(num0) '_allstocks_otherIVOL_monthly'  '.mat'])
data = [data;Aout_resid];
clear Aout_resid
end
clear num0
dataIVOL_other_monthly = data;
clear data


load dataoutmonthly1_allstocks.mat
data = dataoutmonthly1_allstocks;
clear  dataoutmonthly1_allstocks

if SamplePeriod == 1
data = data(data(:,8) >= 196307 & data(:,8)<=200612,:);
load tmp_portfolio_beta_allstocks_1963_2006.mat...
     A_out_portfolio A_out_100portfoliobeta
elseif SamplePeriod == 2
data = data(data(:,8) >= 200701 & data(:,8)<=201712,:);
load tmp_portfolio_beta_allstocks_1963_2017.mat...
     A_out_portfolio A_out_100portfoliobeta
elseif SamplePeriod == 3
data = data(data(:,8) >= 196307 & data(:,8)<=201712,:);
load tmp_portfolio_beta_allstocks_1963_2017.mat...
     A_out_portfolio A_out_100portfoliobeta
elseif SamplePeriod == 4
data = data(data(:,8) >= 192607 & data(:,8)<=196306,:);
load tmp_portfolio_beta_allstocks_1926_1963.mat...
     A_out_portfolio A_out_100portfoliobeta
end

load data_BE_1926_2001.mat DFF_BE_With_Nonindust
data_BE_1926_2001 = DFF_BE_With_Nonindust;
clear DFF_BE_With_Nonindust

%}
%{

IndSave  = 0;

permno0  = unique(data(:,1));
monthall = unique(data(:,8));

for ppp= 1:9
    A_outmatch = [];
    if ppp == 1
       permnoall = permno0(permno0 >= 10000 & permno0 <= 19999,:);
    elseif ppp == 2
       permnoall = permno0(permno0 >= 20000 & permno0 <= 29999,:);
    elseif ppp == 3
       permnoall = permno0(permno0 >= 30000 & permno0 <= 39999,:);
    elseif ppp == 4
       permnoall = permno0(permno0 >= 40000 & permno0 <= 49999,:);
    elseif ppp == 5
       permnoall = permno0(permno0 >= 50000 & permno0 <= 59999,:);
    elseif ppp == 6
       permnoall = permno0(permno0 >= 60000 & permno0 <= 69999,:);
    elseif ppp == 7
       permnoall = permno0(permno0 >= 70000 & permno0 <= 79999,:);
    elseif ppp == 8
       permnoall = permno0(permno0 >= 80000 & permno0 <= 89999,:);
    elseif ppp == 9
       permnoall = permno0(permno0 >= 90000 & permno0 <= 99999,:);
    end
    
for i = 1:size(permnoall,1)

    tmpdata           = data(data(:,1) == permnoall(i),:);
    tmpIVOL           = dataIVOL(dataIVOL(:,1) == permnoall(i),:);
    tmpEIVOL          = dataEIVOL_19262017(dataEIVOL_19262017(:,1) == ...
                        permnoall(i),:);
    tmpEIVOL_sqp      = dataEIVOL_19262017_sqp ...
                       (dataEIVOL_19262017_sqp(:,1) == ...
                        permnoall(i),:);
    tmpEIVOL_Roll     = dataEIVOL_Roll_19262017 ...
                       (dataEIVOL_Roll_19262017(:,1) == ...
                        permnoall(i),:);
    tmpEIVOL_sqp_Roll = dataEIVOL_Roll_19262017_sqp ...
                       (dataEIVOL_Roll_19262017_sqp(:,1) == ...
                        permnoall(i),:);               
               
    tmpBETA           = dataBETA(dataBETA(:,1) == permnoall(i),:);
    tmpportfolio      = A_out_portfolio...
                       (A_out_portfolio(:,1) == permnoall(i),:);
    tmpICC            = dataICC(dataICC(:,1) == permnoall(i),:);
    
    tmpdata_BE_1926_2001  = data_BE_1926_2001...
                           (data_BE_1926_2001(:,1) == permnoall(i),:);
    
    
    tmpIVOL_other_daily   = dataIVOL_other_daily ...
    (dataIVOL_other_daily(:,1) == permnoall(i),:);
    tmpIVOL_other_monthly = dataIVOL_monthly ...
    (dataIVOL_monthly(:,1) == permnoall(i),:);
    
    % replace missing Compustat BM with BM obtained from French's website
    % (if available)
    if isempty(tmpdata_BE_1926_2001) == 0
       tmpdataacc = tmpdata_BE_1926_2001(1,:);
       tmpdataacc(1:3) = [];
       tmpdataacc = tmpdataacc';
       tmpdataacc(tmpdataacc(:,1) == -99.99,1) = -9.9999;
       tmpdataacc = [[1926:2001]' [1927:2002]' tmpdataacc];
       tmpdataacc(:,1) = tmpdataacc(:,1) * 100 + 7;
       tmpdataacc(:,2) = tmpdataacc(:,2) * 100 + 6;
 
       month0 = unique(tmpdata(:,8));
       for jjj = 1
           tmptmpBE_Compustat = tmpdata(tmpdata(:,8) == month0(jjj),24);
           tmptmpBE_Moody = tmpdataacc(tmpdataacc(:,1) <= month0(jjj) & ...
                            tmpdataacc(:,2) >= month0(jjj),3);
           if isempty(tmptmpBE_Moody)==1
              tmptmpBE_Moody = -9.9999;
           end
           if tmptmpBE_Compustat == -9.9999 & ...
              tmptmpBE_Moody ~= -9.9999
              tmpdata(tmpdata(:,8) == month0(jjj),24) = tmptmpBE_Moody;
           end
           clear tmptmpBE_Moody tmptmpBE_Compustat
       end
       clear month0 tmpdataacc 
    end
    clear tmpdata_BE_1926_2001
    
    month0       = unique(tmpdata(:,8));
    tmpout       = [];
    
    for j = 1:size(month0,1)
        
        if isempty(tmpIVOL_other_daily) == 0
           ppp1 = find(monthall == month0(j));
           
           tmptmpIVOL_other_daily = tmpIVOL_other_daily ...
           (tmpIVOL_other_daily(:,2) == monthall(ppp1),:);
           if isempty(tmptmpIVOL_other_daily) == 0
              tmptmpIVOL_other_daily = ...
              tmptmpIVOL_other_daily(1,3:11);
           else
              tmptmpIVOL_other_daily = ones(1,9) * (-9.9999); 
           end
           if ppp1 > 1
              month_1 = monthall(ppp1-1);
              tmptmpIVOLlag1_other_daily = tmpIVOL_other_daily ...
              (tmpIVOL_other_daily(:,2) == month_1,:);
              if isempty(tmptmpIVOLlag1_other_daily) == 0
                 tmptmpIVOLlag1_other_daily = ...
                 tmptmpIVOLlag1_other_daily(1,3:11);
              else
                 tmptmpIVOLlag1_other_daily = ones(1,9) * (-9.9999);
              end
           else
              tmptmpIVOLlag1_other_daily = ones(1,9) * (-9.9999);
           end
        
        else
           tmptmpIVOLlag1_other_daily = ones(1,9) * (-9.9999); 
        end
        clear ppp1 month_1
        
        if isempty(tmpIVOL_other_monthly) == 0
           ppp1 = find(monthall == month0(j));
           
           tmptmpIVOL_other_monthly = tmpIVOL_other_monthly ...
           (tmpIVOL_other_monthly(:,2) == monthall(ppp1),:);
           if isempty(tmptmpIVOL_other_monthly) == 0
              tmptmpIVOL_other_monthly = ...
              tmptmpIVOL_other_monthly(1,3:10);
           else
              tmptmpIVOL_other_monthly = ones(1,8) * (-9.9999); 
           end
           if ppp1 > 1
              month_1 = monthall(ppp1-1);
              tmptmpIVOLlag1_other_monthly = tmpIVOL_other_monthly ...
              (tmpIVOL_other_monthly(:,2) == month_1,:);
              if isempty(tmptmpIVOLlag1_other_monthly) == 0
                 tmptmpIVOLlag1_other_monthly = ...
                 tmptmpIVOLlag1_other_monthly(1,3:10);
              else
                 tmptmpIVOLlag1_other_monthly = ones(1,8) * (-9.9999);
              end
           else
              tmptmpIVOLlag1_other_monthly = ones(1,8) * (-9.9999);
           end
        
        else
           tmptmpIVOLlag1_other_monthly = ones(1,8) * (-9.9999); 
        end
        clear ppp1 month_1
        
        tmpIVOL_other = [tmptmpIVOL_other_daily ...
                         tmptmpIVOLlag1_other_daily ...
                         tmptmpIVOL_other_monthly ...
                         tmptmpIVOLlag1_other_monthly];
        clear tmptmpIVOL_other_daily tmptmpIVOLlag1_other_daily
        clear tmptmpIVOL_other_monthly tmptmpIVOLlag1_other_monthly
        
        tmptmpdatabefore = tmpdata(tmpdata(:,8)<=month0(j),:);
        % RET (t) RETX (t)
        tmptmpret        = tmpdata(tmpdata(:,8) == month0(j),[4 5]);
        % Price (t-1)
        tmptmpprclag1    = tmpdata(tmpdata(:,8) == month0(j),25);
        % Price (t-2)
        tmptmpprclag2    = tmpdata(tmpdata(:,8) == month0(j),28);
        
        % Find RET (t-1) RETX(t-1)
        ppp1 = find(monthall == month0(j));
        if ppp1 > 1
           month_1 = monthall(ppp1-1);
           tmptmpretlag1 = tmpdata(tmpdata(:,8) == month_1,:);
           if isempty(tmptmpretlag1) == 0
              tmptmpretlag1 = tmptmpretlag1(1,[4 5]);
           else
              tmptmpretlag1 = [-9.9999 -9.9999];
           end
        else
           tmptmpretlag1 = [-9.9999 -9.9999]; 
        end
        clear ppp1 month_1
        % SHROUT (t-1)
        tmptmpshroutlag1 = tmpdata(tmpdata(:,8) == month0(j),26);
        % SHROUT (t-2)
        tmptmpshroutlag2 = tmpdata(tmpdata(:,8) == month0(j),29);
        
        if isempty(tmpIVOL) == 0
           % IVOL (t)
           tmptmpIVOL = tmpIVOL(tmpIVOL(:,2) == month0(j),:);
           if isempty(tmptmpIVOL) == 0
              tmptmpIVOL = tmptmpIVOL(1,3);
           else
              tmptmpIVOL = -9.9999;
           end
           % IVOL (t-1)
           ppp1 = find(monthall == month0(j));
           if ppp1 > 1
              month_1 = monthall(ppp1-1);
              tmptmpIVOLlag1 = tmpIVOL(tmpIVOL(:,2) == month_1,:);
              if isempty(tmptmpIVOLlag1) == 0
                 tmptmpIVOLlag1 = tmptmpIVOLlag1(1,3);
              else
                 tmptmpIVOLlag1 = -9.9999; 
              end
           else
              tmptmpIVOLlag1 = -9.9999;
           end
           clear ppp1 month_1
        else
           tmptmpIVOL     = -9.9999;
           tmptmpIVOLlag1 = -9.9999;
        end
        
        % Find Lagged Returns RET(t-2,t-7),BE,SIZE of December and June
        tmptmpBETAlag1 = tmpBETA(tmpBETA(:,2) == month0(j),:);
        if isempty(tmptmpBETAlag1) == 0
           tmptmpTURNnumlag1 = tmptmpBETAlag1(1,13); 
           tmptmpBETAlag1    = tmptmpBETAlag1(1,[3 4 5 6 7]);
        else
           tmptmpBETAlag1    =[-9.9999 -9.9999 -9.9999 -9.9999 -9.9999];
           tmptmpTURNnumlag1 = -9.9999;
        end
        
        % EVIOL estimated using all available information
        % EIVOL (t-1) using information up to (t-1) and 
        % EIVOL (t) using information up to (t-1)
        for EIVOLmethod = 1
        if isempty(tmpEIVOL) == 0
           tmptmpEIVOLlag1 = tmpEIVOL(tmpEIVOL(:,18) == month0(j),:);
           
           if isempty(tmptmpEIVOLlag1) == 0
              tmpmodelall = [];
              
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
              
              % Find valid EIVOL(t) estimated using information up to
              % (t-1)
              % First select EIVOL with exitflag = 1
              % --If EIVOL with exitflag = 1 exsits, select EIVOL with the
              %   lowest AIC
              % --If EIVOL with exitflag = 1 does not exist, select EVIOL
              %   with exitflag = 2 
              % ----If EIVOL with exitflag = 2 exsits, select EIVOL with 
              %     the lowest AIC
              % ----If EIVOL with exitflag = 2 does not exist, EIVOL is
              %     assigned missing value of -9.9999
              
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_t_outsample_InterP = [tmpmodelall1(1,[1 2])];        
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_t_outsample_InterP = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_t_outsample_InterP = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 
        
              tmpmodelall = [];
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model_new AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
               
              % Find valid EIVOL(t-1) estimated using information up to
              % (t-1)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_lastt_insample_InterP = ...
                 [tmpmodelall1(1,[1 2])];       
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_lastt_insample_InterP = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_lastt_insample_InterP = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 

        
           else
           tmptmpEIVOL_t_outsample_InterP  = [-9.9999 -9.9999];
           tmptmpEIVOL_lastt_insample_InterP = [-9.9999 -9.9999];
           end
                      
        else
           tmptmpEIVOL_t_outsample_InterP  = [-9.9999 -9.9999];
           tmptmpEIVOL_lastt_insample_InterP = [-9.9999 -9.9999];
        end
        end
        for EIVOLmethod = 2
        if isempty(tmpEIVOL_sqp) == 0
           tmptmpEIVOLlag1 = tmpEIVOL_sqp ...
                            (tmpEIVOL_sqp(:,18) == month0(j),:);
           
           if isempty(tmptmpEIVOLlag1) == 0
              tmpmodelall = [];
              
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
              
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_t_outsample_sqp = [tmpmodelall1(1,[1 2])];        
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_t_outsample_sqp = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_t_outsample_sqp = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 
        
              tmpmodelall = [];
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model_new AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
               
              % Find valid EIVOL(t-1) estimated using information up to
              % (t-1)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_lastt_insample_sqp = ...
                 [tmpmodelall1(1,[1 2])];       
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_lastt_insample_sqp = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_lastt_insample_sqp = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 

        
           else
           tmptmpEIVOL_t_outsample_sqp  = [-9.9999 -9.9999];
           tmptmpEIVOL_lastt_insample_sqp = [-9.9999 -9.9999];
           end
                      
        else
           tmptmpEIVOL_t_outsample_sqp  = [-9.9999 -9.9999];
           tmptmpEIVOL_lastt_insample_sqp = [-9.9999 -9.9999];
        end
        end
        % EIVOL (t) using information up to (t) and 
        % EIVOL (t+1) using information up to (t)
        for EIVOLmethod = 1
        if isempty(tmpEIVOL) == 0
           p1 = find(monthall==month0(j)) + 1;
           tmptmpEIVOLlag1 = tmpEIVOL(tmpEIVOL(:,18) == monthall(p1),:);
           clear p1
           if isempty(tmptmpEIVOLlag1) == 0
              tmpmodelall = [];
              
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model AIC...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
              % Find valid EIVOL(t+1) estimated using information up to
              % (t)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_nextt_outsample_InterP = ...
                 [tmpmodelall1(1,[1 2])];        
              else
                 tmpmodelall1 = tmpmodelall ...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_nextt_outsample_InterP = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_nextt_outsample_InterP = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 
        
              tmpmodelall = [];
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model_new AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
               
              % Find valid EIVOL(t) estimated using information up to
              % (t)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_t_insample_InterP = ...
                 [tmpmodelall1(1,[1 2])];       
              else
                 tmpmodelall1 = tmpmodelall ...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_t_insample_InterP = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_t_insample_InterP = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 

        
           else
           tmptmpEIVOL_nextt_outsample_InterP  = [-9.9999 -9.9999];
           tmptmpEIVOL_t_insample_InterP = [-9.9999 -9.9999];
           end
                      
        else
           tmptmpEIVOL_nextt_outsample_InterP  = [-9.9999 -9.9999];
           tmptmpEIVOL_t_insample_InterP = [-9.9999 -9.9999];
        end
        end
        for EIVOLmethod = 2
        if isempty(tmpEIVOL_sqp) == 0
           p1 = find(monthall == month0(j)) + 1;
           tmptmpEIVOLlag1 = tmpEIVOL_sqp ...
                            (tmpEIVOL_sqp(:,18) == month0(j),:);
           clear p1
           
           if isempty(tmptmpEIVOLlag1) == 0
              tmpmodelall = [];
              
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
              
              % Find valid EIVOL(t+1) estimated using information up to
              % (t)
              
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_nextt_outsample_sqp = [tmpmodelall1(1,[1 2])];        
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_nextt_outsample_sqp = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_nextt_outsample_sqp = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 
        
              tmpmodelall = [];
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model_new AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
               
              % Find valid EIVOL(t) estimated using information up to
              % (t)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_t_insample_sqp = ...
                 [tmpmodelall1(1,[1 2])];       
              else
                 tmpmodelall1 = tmpmodelall ...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_t_insample_sqp = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_t_insample_sqp = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 

        
           else
           tmptmpEIVOL_nextt_outsample_sqp  = [-9.9999 -9.9999];
           tmptmpEIVOL_t_insample_sqp = [-9.9999 -9.9999];
           end
                      
        else
           tmptmpEIVOL_nextt_outsample_sqp  = [-9.9999 -9.9999];
           tmptmpEIVOL_t_insample_sqp = [-9.9999 -9.9999];
        end
        end
        
        % EVIOL estimated using rolling window data
        % EIVOL (t-1) using information up to (t-1) and 
        % EIVOL (t) using information up to (t-1)
        for EIVOLmethod = 1
        if isempty(tmpEIVOL_Roll) == 0
           tmptmpEIVOLlag1 = tmpEIVOL_Roll ...
                            (tmpEIVOL_Roll(:,18) == month0(j),:);
           
           if isempty(tmptmpEIVOLlag1) == 0
              tmpmodelall = [];
              
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
              
              % Find valid EIVOL(t) estimated using information up to
              % (t-1)
              % First select EIVOL with exitflag = 1
              % --If EIVOL with exitflag = 1 exsits, select EIVOL with the
              %   lowest AIC
              % --If EIVOL with exitflag = 1 does not exist, select EVIOL
              %   with exitflag = 2 
              % ----If EIVOL with exitflag = 2 exsits, select EIVOL with 
              %     the lowest AIC
              % ----If EIVOL with exitflag = 2 does not exist, EIVOL is
              %     assigned missing value of -9.9999
              
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_Roll_t_outsample_InterP = ...
                [tmpmodelall1(1,[1 2])];        
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_Roll_t_outsample_InterP = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_Roll_t_outsample_InterP = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 
        
              tmpmodelall = [];
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model_new AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
               
              % Find valid EIVOL(t-1) estimated using information up to
              % (t-1)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_Roll_lastt_insample_InterP = ...
                 [tmpmodelall1(1,[1 2])];       
              else
                 tmpmodelall1 = tmpmodelall ...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_Roll_lastt_insample_InterP = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_Roll_lastt_insample_InterP = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 

        
           else
           tmptmpEIVOL_Roll_t_outsample_InterP  = [-9.9999 -9.9999];
           tmptmpEIVOL_Roll_lastt_insample_InterP = [-9.9999 -9.9999];
           end
                      
        else
           tmptmpEIVOL_Roll_t_outsample_InterP  = [-9.9999 -9.9999];
           tmptmpEIVOL_Roll_lastt_insample_InterP = [-9.9999 -9.9999];
        end
        end
        for EIVOLmethod = 2
        if isempty(tmpEIVOL_sqp_Roll) == 0
           tmptmpEIVOLlag1 = tmpEIVOL_sqp_Roll ...
                            (tmpEIVOL_sqp_Roll(:,18) == month0(j),:);
           
           if isempty(tmptmpEIVOLlag1) == 0
              tmpmodelall = [];
              
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
              
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_Roll_t_outsample_sqp = ...
                [tmpmodelall1(1,[1 2])];        
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_Roll_t_outsample_sqp = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_Roll_t_outsample_sqp = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 
        
              tmpmodelall = [];
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model_new AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
               
              % Find valid EIVOL(t-1) estimated using information up to
              % (t-1)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_Roll_lastt_insample_sqp = ...
                 [tmpmodelall1(1,[1 2])];       
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_Roll_lastt_insample_sqp = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_Roll_lastt_insample_sqp = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 

        
           else
           tmptmpEIVOL_Roll_t_outsample_sqp  = [-9.9999 -9.9999];
           tmptmpEIVOL_Roll_lastt_insample_sqp = [-9.9999 -9.9999];
           end
                      
        else
           tmptmpEIVOL_Roll_t_outsample_sqp  = [-9.9999 -9.9999];
           tmptmpEIVOL_Roll_lastt_insample_sqp = [-9.9999 -9.9999];
        end
        end
        % EIVOL (t) using information up to (t) and 
        % EIVOL (t+1) using information up to (t)
        for EIVOLmethod = 1
        if isempty(tmpEIVOL_Roll) == 0
           p1 = find(monthall == month0(j)) + 1;
           tmptmpEIVOLlag1 = tmpEIVOL_Roll ...
                            (tmpEIVOL_Roll(:,18) == monthall(p1),:);
           clear p1
           if isempty(tmptmpEIVOLlag1) == 0
              tmpmodelall = [];
              
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
              % Find valid EIVOL(t+1) estimated using information up to
              % (t)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_Roll_nextt_outsample_InterP = ...
                 [tmpmodelall1(1,[1 2])];        
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_Roll_nextt_outsample_InterP = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_Roll_nextt_outsample_InterP = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 
        
              tmpmodelall = [];
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model_new AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
               
              % Find valid EIVOL(t) estimated using information up to
              % (t)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_Roll_t_insample_InterP = ...
                 [tmpmodelall1(1,[1 2])];       
              else
                 tmpmodelall1 = tmpmodelall...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_Roll_t_insample_InterP = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_Roll_t_insample_InterP = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 

        
           else
           tmptmpEIVOL_Roll_nextt_outsample_InterP  = [-9.9999 -9.9999];
           tmptmpEIVOL_Roll_t_insample_InterP = [-9.9999 -9.9999];
           end
                      
        else
           tmptmpEIVOL_Roll_nextt_outsample_InterP  = [-9.9999 -9.9999];
           tmptmpEIVOL_Roll_t_insample_InterP = [-9.9999 -9.9999];
        end
        end
        for EIVOLmethod = 2
        if isempty(tmpEIVOL_sqp_Roll) == 0
           p1 = find(monthall == month0(j)) + 1;
           tmptmpEIVOLlag1 = tmpEIVOL_sqp_Roll ...
                            (tmpEIVOL_sqp_Roll(:,18) == monthall(p1),:);
           clear p1
           
           if isempty(tmptmpEIVOLlag1) == 0
              tmpmodelall = [];
              
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
              
              % Find valid EIVOL(t+1) estimated using information up to
              % (t)
              
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_Roll_nextt_outsample_sqp = ...
                [tmpmodelall1(1,[1 2])];        
              else
                 tmpmodelall1 = tmpmodelall ...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_Roll_nextt_outsample_sqp = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_Roll_nextt_outsample_sqp = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 
        
              tmpmodelall = [];
              for modelall = 1:9
                  if modelall == 1
                     ExpIdioV_model =  23; ExpIdioV_model_new = 145;
                  elseif modelall == 2
                     ExpIdioV_model =  36; ExpIdioV_model_new = 146;
                  elseif modelall == 3
                     ExpIdioV_model =  51; ExpIdioV_model_new = 147;
                  elseif modelall == 4
                     ExpIdioV_model =  63; ExpIdioV_model_new = 148;
                  elseif modelall == 5
                     ExpIdioV_model =  77; ExpIdioV_model_new = 149;
                  elseif modelall == 6
                     ExpIdioV_model =  93; ExpIdioV_model_new = 150;
                  elseif modelall == 7
                     ExpIdioV_model = 106; ExpIdioV_model_new = 151;
                  elseif modelall == 8
                     ExpIdioV_model = 121; ExpIdioV_model_new = 152;
                  elseif modelall == 9
                     ExpIdioV_model = 138; ExpIdioV_model_new = 153;
                  end
                  
                  AIC         = ExpIdioV_model + 2;
                  IndWarning  = ExpIdioV_model + 4;
                  ExitFlag    = ExpIdioV_model + 5;
                  tmpmodelall = [tmpmodelall;modelall ...
                  tmptmpEIVOLlag1(:,[ExpIdioV_model_new AIC ...
                  IndWarning ExitFlag])];
                  clear ExpIdioV_model AIC IndWarning ExitFlag
              end
               
              % Find valid EIVOL(t) estimated using information up to
              % (t)
              tmpmodelall1 = tmpmodelall(tmpmodelall(:,2) ~= -9.9999 & ...
                             tmpmodelall(:,4) == 0 & ...
                             tmpmodelall(:,5) == 1,:);
              if isempty(tmpmodelall1) == 0
                 tmpmodelall1 = sortrows(tmpmodelall1,3);  
                 tmptmpEIVOL_Roll_t_insample_sqp = ...
                 [tmpmodelall1(1,[1 2])];       
              else
                 tmpmodelall1 = tmpmodelall ...
                               (tmpmodelall(:,2) ~= -9.9999 & ...
                                tmpmodelall(:,4) == 0 & ...
                                tmpmodelall(:,5) >= 1,:);  
                 if isempty(tmpmodelall1) == 0
                    tmpmodelall1 = sortrows(tmpmodelall1,3);  
                    tmptmpEIVOL_Roll_t_insample_sqp = ...
                    [tmpmodelall1(1,[1 2])];
                 else
                    tmptmpEIVOL_Roll_t_insample_sqp = ...
                    [-9.9999 -9.9999];
                 end
              end
              clear tmpmodelall1 tmpmodelall 

        
           else
           tmptmpEIVOL_Roll_nextt_outsample_sqp  = [-9.9999 -9.9999];
           tmptmpEIVOL_Roll_t_insample_sqp = [-9.9999 -9.9999];
           end
                      
        else
           tmptmpEIVOL_Roll_nextt_outsample_sqp  = [-9.9999 -9.9999];
           tmptmpEIVOL_Roll_t_insample_sqp = [-9.9999 -9.9999];
        end
        end
        
        
        % Find Portfolio Beta
        ppp1 = find(monthall == month0(j));
        if ppp1 > 1
           month_1 = monthall(ppp1-1);
        if isempty(tmpportfolio) ==0
           tmptmpportfolio = tmpportfolio(tmpportfolio(:,2) == month_1,:);
           if isempty(tmptmpportfolio) == 0      
              tmptmpportfolioBETAlag1 = A_out_100portfoliobeta ...
              (A_out_100portfoliobeta(:,1) == tmptmpportfolio(1,3) & ...
               A_out_100portfoliobeta(:,2) == tmptmpportfolio(1,4),3);
           else
               tmptmpportfolioBETAlag1 = -9.9999;
           end
        else
           tmptmpportfolioBETAlag1 = -9.9999;  
        end
        clear tmptmpportfolio
        
        else
           tmptmpportfolioBETAlag1 = -9.9999;
        end
        clear ppp1 month_1
        
        % Calculate BM Ratio
        ppp1 = find(monthall == month0(j));
        if ppp1 > 1
           month_1 = monthall(ppp1 - 1);
           tmptmpdata = tmpdata(tmpdata(:,8) == month_1,:);
           if isempty(tmptmpdata) == 0
              tmptmpsize    = tmptmpdata(1,23);
              tmptmpBE      = tmptmpdata(1,24);
              tmptmpsizeDEC = tmptmpdata(1,22);
              if tmptmpBE ~= -9.9999 & tmptmpsizeDEC ~= -9.9999 &...
                 tmptmpBE > 0 & tmptmpsizeDEC > 0
                 tmptmpBM =(tmptmpBE * 1000000) / (tmptmpsizeDEC * 1000);
              else
                 tmptmpBM = -9.9999;
              end
           else
              tmptmpsize = -9.9999;
              tmptmpBM   = -9.9999;
           end
        else
           tmptmpsize = -9.9999;
           tmptmpBM   = -9.9999;
        end
        clear tmptmpdata tmptmpBE tmptmpsizeDEC ppp1 month_1
       
        if isempty(tmpICC) == 0
           ppp1 = find(monthall == month0(j));
           if ppp1 >1
              tmp0 = tmpICC(tmpICC(:,2) == monthall(ppp1 - 1),:);
              if isempty(tmp0) == 0
                 tmptmpICC = tmp0(1,[3 6]); 
              else
                 tmptmpICC = [-9.9999 -9.9999];
              end
           else
              tmptmpICC = [-9.9999 -9.9999];
           end
        else
           tmptmpICC = [-9.9999 -9.9999];
        end
        clear tmp0 ppp1
        
        tmpout = [tmpout;permnoall(i) month0(j) ...
                  tmptmpret tmptmpretlag1 tmptmpsize tmptmpBM ...
                  tmptmpprclag1 tmptmpshroutlag1 ...
                  tmptmpprclag2 tmptmpshroutlag2 ...
                  tmptmpIVOL tmptmpIVOLlag1 ...
                  tmptmpportfolioBETAlag1 ...
                  tmptmpBETAlag1 tmptmpTURNnumlag1 ...
                  tmptmpEIVOL_t_outsample_InterP ...
                  tmptmpEIVOL_t_outsample_sqp ...
                  tmptmpEIVOL_lastt_insample_InterP ...
                  tmptmpEIVOL_lastt_insample_sqp ...
                  tmptmpEIVOL_nextt_outsample_InterP ...
                  tmptmpEIVOL_nextt_outsample_sqp ...
                  tmptmpEIVOL_t_insample_InterP ...
                  tmptmpEIVOL_t_insample_sqp ...
                  size(tmptmpdatabefore,1) ...
                  tmptmpICC ...
                  tmpIVOL_other ...
                  tmptmpEIVOL_Roll_t_outsample_InterP ...
                  tmptmpEIVOL_Roll_t_outsample_sqp ...
                  tmptmpEIVOL_Roll_lastt_insample_InterP ...
                  tmptmpEIVOL_Roll_lastt_insample_sqp ...
                  tmptmpEIVOL_Roll_nextt_outsample_InterP ...
                  tmptmpEIVOL_Roll_nextt_outsample_sqp ...
                  tmptmpEIVOL_Roll_t_insample_InterP ...
                  tmptmpEIVOL_Roll_t_insample_sqp];
              
        clear tmptmpret tmptmpretlag1 tmptmpsize tmptmpBM
        clear tmptmpprclag1 tmptmpshroutlag1
        clear tmptmpprclag2 tmptmpshroutlag2
        clear tmptmpIVOL tmptmpIVOLlag1
        clear tmptmpportfolioBETAlag1
        clear tmptmpBETAlag1 tmptmpTURNnumlag1
        clear tmptmpEIVOL_t_outsample_InterP
        clear tmptmpEIVOL_t_outsample_sqp
        clear tmptmpEIVOL_lastt_insample_InterP
        clear tmptmpEIVOL_lastt_insample_sqp
        clear tmptmpEIVOL_nextt_outsample_InterP
        clear tmptmpEIVOL_nextt_outsample_sqp
        clear tmptmpEIVOL_t_insample_InterP
        clear tmptmpEIVOL_t_insample_sqp
        clear tmptmpdatabefore tmptmpICC tmpIVOL_other
        clear tmptmpEIVOL_Roll_t_outsample_InterP 
        clear tmptmpEIVOL_Roll_t_outsample_sqp
        clear tmptmpEIVOL_Roll_lastt_insample_InterP
        clear tmptmpEIVOL_Roll_lastt_insample_sqp
        clear tmptmpEIVOL_Roll_nextt_outsample_InterP
        clear tmptmpEIVOL_Roll_nextt_outsample_sqp
        clear tmptmpEIVOL_Roll_t_insample_InterP
        clear tmptmpEIVOL_Roll_t_insample_sqp
        
        

    end
    A_outmatch = [A_outmatch;tmpout];
    clear tmpdata tmpIVOL tmpEIVOL tmpICC tmpBETA tmpout tmpportfolio
    clear tmpICC tmpIVOL_other_daily tmpIVOL_other_monthly
    
end

% Save data
if IndSave == 1
if SamplePeriod == 1
if ppp == 1
   save tmpmatch_allstocks_1_1963_2006.mat A_outmatch
elseif ppp == 2
   save tmpmatch_allstocks_2_1963_2006.mat A_outmatch
elseif ppp == 3
   save tmpmatch_allstocks_3_1963_2006.mat A_outmatch
elseif ppp == 4
   save tmpmatch_allstocks_4_1963_2006.mat A_outmatch
elseif ppp == 5
   save tmpmatch_allstocks_5_1963_2006.mat A_outmatch
elseif ppp == 6
   save tmpmatch_allstocks_6_1963_2006.mat A_outmatch
elseif ppp == 7
   save tmpmatch_allstocks_7_1963_2006.mat A_outmatch
elseif ppp == 8
   save tmpmatch_allstocks_8_1963_2006.mat A_outmatch
elseif ppp == 9
   save tmpmatch_allstocks_9_1963_2006.mat A_outmatch
end

elseif SamplePeriod == 2
if ppp == 1
   save tmpmatch_allstocks_1_2007_2017.mat A_outmatch
elseif ppp == 2
   save tmpmatch_allstocks_2_2007_2017.mat A_outmatch
elseif ppp == 3
   save tmpmatch_allstocks_3_2007_2017.mat A_outmatch
elseif ppp == 4
   save tmpmatch_allstocks_4_2007_2017.mat A_outmatch
elseif ppp == 5
   save tmpmatch_allstocks_5_2007_2017.mat A_outmatch
elseif ppp == 6
   save tmpmatch_allstocks_6_2007_2017.mat A_outmatch
elseif ppp == 7
   save tmpmatch_allstocks_7_2007_2017.mat A_outmatch
elseif ppp == 8
   save tmpmatch_allstocks_8_2007_2017.mat A_outmatch
elseif ppp == 9
   save tmpmatch_allstocks_9_2007_2017.mat A_outmatch
end


elseif SamplePeriod == 3
if ppp == 1
   save tmpmatch_allstocks_1_1963_2017.mat A_outmatch
elseif ppp == 2
   save tmpmatch_allstocks_2_1963_2017.mat A_outmatch
elseif ppp == 3
   save tmpmatch_allstocks_3_1963_2017.mat A_outmatch
elseif ppp == 4
   save tmpmatch_allstocks_4_1963_2017.mat A_outmatch
elseif ppp == 5
   save tmpmatch_allstocks_5_1963_2017.mat A_outmatch
elseif ppp == 6
   save tmpmatch_allstocks_6_1963_2017.mat A_outmatch
elseif ppp == 7
   save tmpmatch_allstocks_7_1963_2017.mat A_outmatch
elseif ppp == 8
   save tmpmatch_allstocks_8_1963_2017.mat A_outmatch
elseif ppp == 9
   save tmpmatch_allstocks_9_1963_2017.mat A_outmatch
end

elseif SamplePeriod == 4
if ppp == 1
   save tmpmatch_allstocks_1_1926_1963.mat A_outmatch
elseif ppp == 2
   save tmpmatch_allstocks_2_1926_1963.mat A_outmatch
elseif ppp == 3
   save tmpmatch_allstocks_3_1926_1963.mat A_outmatch
elseif ppp == 4
   save tmpmatch_allstocks_4_1926_1963.mat A_outmatch
elseif ppp == 5
   save tmpmatch_allstocks_5_1926_1963.mat A_outmatch
elseif ppp == 6
   save tmpmatch_allstocks_6_1926_1963.mat A_outmatch
elseif ppp == 7
   save tmpmatch_allstocks_7_1926_1963.mat A_outmatch
elseif ppp == 8
   save tmpmatch_allstocks_8_1926_1963.mat A_outmatch
elseif ppp == 9
   save tmpmatch_allstocks_9_1926_1963.mat A_outmatch
end


end
end
clear permnoall
end


%}
% Matched monthly Data Description
%{
No.VarName      Description
1  PERMNO       Permno Number 
2  Date         YearMonth (yyyymm) (t)
3  RET          RET (t)
4  RETX         RETX(t)
5  LAGRET       RET (t-1)
6  LAGRETX      RETX(t-1)
7  ME_jun       Market Capitalization at the end of June
                as in Fama & French (1992) (in $thousands)
8  BM           Book to Market Ratio
9  Price        Price (t-1)
10 SHROUT       Number of Shares Outstanding at the end of last month (t-1)
                (in thousands)
11 Price        Price (t-2)
12 SHROUT       Number of Shares Outstanding at the end of last month (t-2)
                (in thousands)
13 IVOL         IVOL (t)
14 IVOL         IVOL (t-1)
15 Beta         Portfolio Market Beta
16 Beta         Individual Market Beta
17 Beta         Number of Month to calculate individual market beta
18 RET_2_7      Compound Return for month (t-2) to month (t-7)
19 TURN         Average Turnover Rate in the last 36 months
20 CVTURN       CV of Turnover Rate in the last 36 months
21 sizeTURN     Number of Month to calculate TURN
22              Number of Best Model
23              EIVOL(t) estimated using information up to (t-1)
                (Algorithm = 'interior-point')                  
24              Number of Best Model
25              EIVOL(t) estimated using information up to (t-1)
                (Algorithm = 'sqp')                  
26              Number of Best Model
27              EIVOL(t-1) estimated using information up to (t-1)
                (Algorithm = 'interior-point')                  
28              Number of Best Model
29              EIVOL(t-1) estimated using information up to (t-1)
                (Algorithm = 'sqp')    
   
30              Number of Best Model
31              EIVOL(t+1) estimated using information up to (t)
                (Algorithm = 'interior-point')                  
32              Number of Best Model
33              EIVOL(t+1) estimated using information up to (t)
                (Algorithm = 'sqp')                  
34              Number of Best Model
35              EIVOL(t) estimated using information up to (t)
                (Algorithm = 'interior-point')                  
36              Number of Best Model
37              EIVOL(t) estimated using information up to (t)
                (Algorithm = 'sqp')       
38 sizeTURN     Number of Month before month (t)
39 ICC          Implied Cost of Capital
40 exitflag     exitflag of fsolve for estimating ICC
41 NumMonth(t)  Number of month to calculate IVOL in the previous 3 months
42 NumDay(t)    Number of day to calculate IVOL in the previous 3 months
43 IVOL(t)      Monthly IVOL calculated using daily return in the precious
                3 months
44 NumMonth(t)	Number of month to calculate IVOL in the previous 6 months
45 NumDay(t)	Number of day to calculate IVOL in the previous 6 months
46 IVOL(t)  	Monthly IVOL calculated using daily return in the precious
                6 months
47 NumMonth(t)	Number of month to calculate IVOL in the previous 12 months
48 NumDay(t)	Number of day to calculate IVOL in the previous 12 months
49 IVOL(t)  	Monthly IVOL calculated using daily return in the precious
                12 months   
50 NumMonth(t-1)Number of month to calculate IVOL in the previous 3 months
51 NumDay(t-1)  Number of day to calculate IVOL in the previous 3 months
52 IVOL(t-1)    Monthly IVOL calculated using daily return in the precious
                3 months
53 NumMonth(t-1)Number of month to calculate IVOL in the previous 6 months
54 NumDay(t-1)	Number of day to calculate IVOL in the previous 6 months
55 IVOL(t-1)  	Monthly IVOL calculated using daily return in the precious
                6 months
56 NumMonth(t-1)Number of month to calculate IVOL in the previous 12 months
57 NumDay(t-1)	Number of day to calculate IVOL in the previous 12 months
58 IVOL(t-1)  	Monthly IVOL calculated using daily return in the precious
                12 months     
59 NumMonth(t)	Number of month to calculate IVOL in the previous 1 year
60 IVOL(t)      Monthly IVOL calculated using monthly return in the 
                precious 1 year
61 NumMonth(t)	Number of month to calculate IVOL in the previous 3 year
62 IVOL(t)      Monthly IVOL calculated using monthly return in the 
                precious 3 year
63 NumMonth(t)	Number of month to calculate IVOL in the previous 5 year
64 IVOL(t)  	Monthly IVOL calculated using monthly return in the 
                precious 5 year
65 NumMonth(t)	Number of month to calculate IVOL in the previous all year
66 IVOL(t)      Monthly IVOL calculated using monthly return in the 
                precious all year   
67 NumMonth(t-1)Number of month to calculate IVOL in the previous 1 year
68 IVOL(t-1)    Monthly IVOL calculated using monthly return in the 
                precious 1 year
69 NumMonth(t-1)Number of month to calculate IVOL in the previous 3 year
70 IVOL(t-1)    Monthly IVOL calculated using monthly return in the 
                precious 3 year
71 NumMonth(t-1)Number of month to calculate IVOL in the previous 5 year
72 IVOL(t-1)  	Monthly IVOL calculated using monthly return in the 
                precious 5 year
73 NumMonth(t-1)Number of month to calculate IVOL in the previous all year
74 IVOL(t-1)    Monthly IVOL calculated using monthly return in the 
                precious all year       
75              Number of Best Model
76              EIVOL(t) estimated using information from (t-60) to (t-1)
                (Algorithm = 'interior-point')                  
77              Number of Best Model
78              EIVOL(t) estimated using information from (t-60) to (t-1)
                (Algorithm = 'sqp')                  
79              Number of Best Model
80              EIVOL(t-1) estimated using information from (t-60) to (t-1)
                (Algorithm = 'interior-point')                  
81              Number of Best Model
82              EIVOL(t-1) estimated using information from (t-60) to (t-1)
                (Algorithm = 'sqp')    
   
83              Number of Best Model
84              EIVOL(t+1) estimated using information from (t-59) to (t)
                (Algorithm = 'interior-point')                  
85              Number of Best Model
86              EIVOL(t+1) estimated using information from (t-59) to (t)
                (Algorithm = 'sqp')                  
87              Number of Best Model
88              EIVOL(t) estimated using information from (t-59) to (t)
                (Algorithm = 'interior-point')                  
89              Number of Best Model
90              EIVOL(t) estimated using information from (t-59) to (t)
                (Algorithm = 'sqp')       

%}
% Summary Statistics of Pooled Data
%{

clear
clc

SamplePeriod = 1;
% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963


data = [];
for num0 = 1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0


% Load data in risk-free rate
load dataother.mat data_famafrench3factor_monthly
datarf = data_famafrench3factor_monthly;
clear data_famafrench3factor_monthly
monthall0 = unique(data(:,2));

% Calculate monthly Excess RET
for m = 1:size(monthall0,1)
    tmprf = datarf(datarf(:,1) == monthall0(m),:);
    data(data(:,2) == monthall0(m),31) = tmprf(1,7) / 100;
    clear tmprf
end
data(:,4)  = data(:,3) - data(:,31);
data(:,31) = [];

A_Table3 = zeros(11,7);
% Summary Statistics for RET EXRET and BETA
for ccc=[3 4 15]
    if ccc == 3
       ccc1 = 1;
       ccc2 = 3;
    elseif ccc == 4
       ccc1 = 2;
    elseif ccc == 15;
       ccc1 = 7;
    end
    % Eliminate Missing Value
    data1 = data(data(:,ccc) ~= -9.9999,:);
    
    % Eliminate Monthly RET >3
    if ccc == 3 | ccc == 4
       data1(data1(:,3) >= 3,:) = [];
    end
    
    a = data1(:,ccc);

    if ccc == 3
       aa = log(a + 1);
       a  = a * 100;
       aa = aa * 100;
       A_Table3(ccc1,:) = [mean(a) std(a) prctile(a,50) prctile(a,25) ...
                           prctile(a,75) skewness(a) size(a,1)];
       A_Table3(ccc2,:) = [mean(aa) std(aa) prctile(aa,50)...
                           prctile(aa,25) prctile(aa,75) skewness(aa) ...
                           size(aa,1)];
    elseif ccc == 4 
       a = a * 100;
       A_Table3(ccc1,:) = [mean(a) std(a) prctile(a,50) prctile(a,25) ...
                           prctile(a,75) skewness(a) size(a,1)];
    elseif ccc == 15
       A_Table3(ccc1,:) = [mean(a) std(a) prctile(a,50) prctile(a,25) ...
                           prctile(a,75) skewness(a) size(a,1)];
    end
    
    clear data1 a aa
end


monthall = unique(data(:,2));
tmpout   = [];

% Summary Statistics
% IVOL out-of-sample EIVOL in-sample EIVOL
% SIZE BM RET_2_7 TURN CVTURN
for j = 1:size(monthall,1)
    
    tmpdata=data(data(:,2) == monthall(j),:);
    
    for ccc = [13 23 35 7 8 18 19 20]
        
        % Eliminate Missing Value
        tmptmp = tmpdata(tmpdata(:,ccc) ~= -9.9999,:);
        
        
        if isempty(tmptmp) == 0
           % 36 months of data to calcualte TURN and CVTURN
           if ccc == 19 | ccc == 20
              tmptmp = tmptmp(tmptmp(:,21) >= 36,:);
           end
           
           % The smallest and largest 0.5\% of the observations in each 
           % month for ME, BE/ME, IVOL, E(IVOL), Ret(2,7), TURN, and CVTURN
           % are set equal to the next smallest or largest values
           
           LB = prctile(tmptmp(:,ccc),0.5);
           UB = prctile(tmptmp(:,ccc),99.5);
           
           data(data(:,2) == monthall(j) & data(:,ccc) ~= -9.9999 & ...
                data(:,ccc) <= LB,ccc) = LB;
           data(data(:,2) == monthall(j) & data(:,ccc) ~= -9.9999 & ...
                data(:,ccc) >= UB,ccc) = UB;
           clear LB UB 
        end
        clear tmptmp
    end
    clear tmpdata
    
end

for ccc = [13 23 35 7 8 18 19 20]
    
    data1 = data(data(:,ccc) ~= -9.9999,:);
    a     = data1(:,ccc);
    
    % IVOL
    if ccc == 13
       ccc1 = 4;
       a    = a * 100; 
    % EIVOL (t) estimated using information up to (t-1)   
    elseif ccc == 23
       ccc1 = 5;
       a    = a.^0.5;
       a    = a * 100; 
    % EIVOL (t) estimated using information up to (t)   
    elseif ccc == 35
       ccc1 = 6;
       a    = a.^0.5;
       a    = a * 100; 
    % SIZE
    elseif ccc == 7
       ccc1 = 8;
       a    = log((a / 1000 + 1));
    % BM
    elseif ccc == 8
       ccc1 = 9;
       a    = log(a);
    % RET_2_7   
    elseif ccc ==18
       ccc1 = 10;
       a    = a + 1;
    % TURN
    elseif ccc == 19
       ccc1 = 11;
       a    = log(a * 100);
    % CVTURN
    elseif ccc == 20
       ccc1 = 12;
       a    = log(a * 100);
    end
    
    A_Table3(ccc1,:) = [mean(a) std(a) prctile(a,50) prctile(a,25) ...
    prctile(a,75) skewness(a) size(data1,1)];
    
    clear data1 a ccc1    
end
%}
% ------------------------------------------------------------------------%
% Table 4 Pairwise Correlation Matrix
% ------------------------------- Table 4 --------------------------------%
%{
clear
clc

SamplePeriod = 1;
% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963

% Minimum Number of Stocks within a Month to calculate correlation
cut0 = 20; 
data = [];
for num0 = 1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0


monthall     = unique(data(:,2));
A_out_Table4 = [];


for j = 1:size(monthall,1)
    j
    tmpout  = [];
    tmpdata = data(data(:,2) == monthall(j),:);
    % Calculatev log RET
    tmpdata(tmpdata(:,3) ~= -9.9999,57) = ...
    log(tmpdata(tmpdata(:,3) ~= -9.9999,5) + 1);
    tmpdata(tmpdata(:,3) == -9.9999,57) = -9.9999;
    clear tmpdata1 rho0 ccc
    for ccc1 = [3 57 13 23 35 15 7 8 18 19 20]
        for ccc2 = [3 57 13 23 35 15 7 8 18 19 20]
            
            tmpdata1(:,1) = tmpdata(:,ccc1);
            tmpdata1(:,2) = tmpdata(:,ccc2);
            % Eliminate monthly RET >3 or missing value
            if ccc1 == 5 | ccc1 == 57
               tmpdata1(tmpdata1(:,1) > 3 | tmpdata1(:,1) == -9.9999 | ...
               tmpdata1(:,2) == -9.9999,:) = [];
            elseif ccc2 == 5 | ccc2 == 57
               tmpdata1(tmpdata1(:,2) > 3 | tmpdata1(:,1) == -9.9999 | ...
               tmpdata1(:,2) == -9.9999,:) = [];
            else
               tmpdata1(tmpdata1(:,1) == -9.9999 | ...
               tmpdata1(:,2) == -9.9999,:) = [];
            end
            
            if size(tmpdata1,1) > cut0
               if ccc2 == 7
                  tmpdata1(:,2) = log(tmpdata1(:,2) / 1000 + 1);
               elseif ccc2 == 8
                  tmpdata1(:,2) = log(tmpdata1(:,2));
               elseif ccc2 == 18
                  tmpdata1(:,2) = tmpdata1(:,2) + 1;
               elseif ccc2 == 19
                  tmpdata1(:,2) = log(tmpdata1(:,2) * 100);
               elseif ccc2 == 20
                  tmpdata1(:,2) = log(tmpdata1(:,2) * 100);
               elseif ccc2 == 23 | ccc2 == 35
                  tmpdata1(:,2) = tmpdata1(:,2).^0.5;
               end
               if ccc1 == 7
                  tmpdata1(:,1) = log(tmpdata1(:,1) / 1000 + 1);
               elseif ccc1 == 8
                  tmpdata1(:,1) = log(tmpdata1(:,1));
               elseif ccc1 == 18
                  tmpdata1(:,1) = tmpdata1(:,1) + 1;
               elseif ccc1 == 19
                  tmpdata1(:,1) = log(tmpdata1(:,1) * 100);
               elseif ccc1 == 20
                  tmpdata1(:,1) = log(tmpdata1(:,1) * 100);
               elseif ccc1 == 23 | ccc1 == 35
                  tmpdata1(:,1) = tmpdata1(:,1).^0.5;
               end
               
               rho0   = corrcoef(tmpdata1(:,1),tmpdata1(:,2));
               tmpout = [tmpout;monthall(j) ccc1 ccc2 rho0(1,2)];
                              
            end
            clear tmpdata1 rho0 rho0_trunc tmpdata1_trunc LB1 UB1 LB2 UB2
        end
    end
    
    
    A_out_Table4 = [A_out_Table4;tmpout];   
    clear tmpdata tmpout
end

% Save data
if SamplePeriod == 1
save Results_Table4_allstocks_1963_2006.mat A_out_Table4
elseif SamplePeriod == 2
save Results_Table4_allstocks_2007_2017.mat A_out_Table4
elseif SamplePeriod == 3
save Results_Table4_allstocks_1963_2017.mat A_out_Table4
elseif SamplePeriod == 4
save Results_Table4_allstocks_1926_1963.mat A_out_Table4
end

A_out_correplation       = zeros(10,10);
A_out_correplation_tstat = zeros(10,10);
if SamplePeriod == 1
load Results_Table4_allstocks_1963_2006.mat A_out_Table4
elseif SamplePeriod == 2
load Results_Table4_allstocks_2007_2017.mat A_out_Table4
elseif SamplePeriod == 3
load Results_Table4_allstocks_1963_2017.mat A_out_Table4
elseif SamplePeriod == 4
load Results_Table4_allstocks_1926_1963.mat A_out_Table4
end

IndEIVOLSample = 1;
% ( 1 = Out of Sample; 0 = In Sample)
if IndEIVOLSample == 1
   c_EIVOL = 23;
elseif IndEIVOLSample == 0
   c_EIVOL = 35;
end
    Trow = 1; 
    for ccc1 = [3 57 13 c_EIVOL 15 7 8 18 19 20]
        Tcolumn = 1;
        for ccc2 = [3 57 13 23 35 15 7 8 18 19 20]
           
            tmp  = A_out_Table4(A_out_Table4(:,2) == ccc1 & ...
                   A_out_Table4(:,3) == ccc2,:);
            rlt0 = nwest(tmp(:,4),ones(size(tmp,1),1),0);
            if Trow < Tcolumn
               A_out_correplation(Trow,Tcolumn)       = rlt0.beta;
               A_out_correplation_tstat(Trow,Tcolumn) = rlt0.tstat;
            end
            A_out_correplation(Trow,Tcolumn)       = rlt0.beta;
            A_out_correplation_tstat(Trow,Tcolumn) = rlt0.tstat;
            clear rlt0
            
            Tcolumn = Tcolumn + 1;
        end
        Trow = Trow + 1;
    end
%}
% ------------------------------------------------------------------------%
% Table 5 Fama-Macbeth Cross Sectional 
% ------------------------------- Table 5 --------------------------------%
%{
clear
clc

SamplePeriod = 1;
% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963

% Minimum Number of Stocks within a Month to run a regression
cut0 = 20; 

data = [];
for num0 = 1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0

monthall = unique(data(:,2));


A_out_Table5_reg1   = [];
A_out_Table5_reg2   = [];
A_out_Table5_reg3   = [];
A_out_Table5_reg3_2 = [];
A_out_Table5_reg4   = [];
A_out_Table5_reg4_2 = [];
A_out_Table5_reg5   = [];
A_out_Table5_reg5_2 = [];
A_out_Table5_reg6   = [];
A_out_Table5_reg7   = [];

% Portfolio Beta
betac   = 15;
% SIZE
sizec   = 7;
% BM
bmc     = 8;
% RET(t)
retc    = 3;
% RET(t-2,t-7)
ret27c  = 18;
% TURN
turnc   = 19;
% CVTURN
cvturnc = 20;
% EIVOL(t)|(t-1) EIVOL estimated using information up to (t-1)
eivolc1 = 23;
% EIVOL(t)|(t) EIVOL  estimated using information up to (t)
eivolc2 = 35;
% IVOL(t-1)
ivolc1  = 14;
% IVOL(t)
ivolc2  = 13;

for j = 1:size(monthall,1)
 
    tmpout_reg1   = [];
    tmpout_reg2   = [];
    tmpout_reg3   = [];
    tmpout_reg3_2 = [];
    tmpout_reg4   = [];
    tmpout_reg4_2 = [];
    tmpout_reg5   = [];
    tmpout_reg5_2 = [];
    tmpout_reg6   = [];
    tmpout_reg7   = [];
    
    tmpdata = data(data(:,2) == monthall(j),:);
    
    % For all regressions
    %Observations with monthly return > 3 are eliminated
    % The smallest and largest 0.5% of the explanatory variables
    % (except BETA) are set equal to the next smallest and largest values
    
    % Regression 1: Y = RET(t) X=BETA(t-1) LN(ME)(t-1) LN(B/M)(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,betac) == -9.9999 | tmpdata1(:,sizec) == -9.9999 |...
    tmpdata1(:,bmc) == -9.9999,:) = [];
    
    if size(tmpdata1,1) > cut0
        
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       
       tmpdata1(tmpdata1(:,sizec) <= LB1,sizec) = LB1;
       tmpdata1(tmpdata1(:,sizec) >= UB1,sizec) = UB1;
       tmpdata1(tmpdata1(:,bmc)   <= LB2,bmc)   = LB2;
       tmpdata1(tmpdata1(:,bmc)   >= UB2,bmc)   = UB2;
       
       tmpdata1(:,sizec) = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)   = log(tmpdata1(:,bmc));
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,[betac sizec bmc])];
       rlt0 = ols1(yyy0,xxx0);
       
       tmpout_reg1 = [tmpout_reg1;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 tmpdata1 rlt0
    
    % Regression 2: Y = RET(t) X= BETA(t-1) LN(ME)(t-1) LN(B/M)(t-1)
    %                             RET(-2,-7) Ln(TURN) Ln(CVTURN)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,betac) == -9.9999 |...
             tmpdata1(:,retc) == -9.9999 |...
             tmpdata1(:,sizec) == -9.9999 |...
             tmpdata1(:,bmc) == -9.9999  |...
             tmpdata1(:,ret27c) == -9.9999 |...
             tmpdata1(:,turnc) == -9.9999 |...
             tmpdata1(:,cvturnc) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ret27c),0.5);
       UB3 = prctile(tmpdata1(:,ret27c),99.5);
       LB4 = prctile(tmpdata1(:,turnc),0.5);
       UB4 = prctile(tmpdata1(:,turnc),99.5);
       LB5 = prctile(tmpdata1(:,cvturnc),0.5);
       UB5 = prctile(tmpdata1(:,cvturnc),99.5);
       
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,ret27c)  <= LB3,ret27c)  = LB3;
       tmpdata1(tmpdata1(:,ret27c)  >= UB3,ret27c)  = UB3;
       tmpdata1(tmpdata1(:,turnc)   <= LB4,turnc)   = LB4;
       tmpdata1(tmpdata1(:,turnc)   >= UB4,turnc)   = UB4;
       tmpdata1(tmpdata1(:,cvturnc) <= LB5,cvturnc) = LB5;
       tmpdata1(tmpdata1(:,cvturnc) >= UB5,cvturnc) = UB5;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,ret27c)  = tmpdata1(:,ret27c) + 1;
       tmpdata1(:,turnc)   = log(tmpdata1(:,turnc) * 100);
       tmpdata1(:,cvturnc) = log(tmpdata1(:,cvturnc) * 100);
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1)...
               tmpdata1(:,[betac sizec bmc ret27c turnc cvturnc])];
       rlt0 = ols1(yyy0,xxx0);
       
       tmpout_reg2 = [tmpout_reg2;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 LB4 UB4 LB5 UB5 tmpdata1 rlt0
    
    % Regression 3: Y=RET(t) X=EIVOL(t)|(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
             tmpdata1(:,eivolc1) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,eivolc1),0.5);
       UB1 = prctile(tmpdata1(:,eivolc1),99.5);
       tmpdata1(tmpdata1(:,eivolc1) <= LB1,eivolc1) = LB1;
       tmpdata1(tmpdata1(:,eivolc1) >= UB1,eivolc1) = UB1;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,eivolc1).^0.5 * 100];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg3 = [tmpout_reg3;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 tmpdata1 rlt0
    
    % Regression 3(2): Y=RET(t) X=EIVOL(t)|(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,eivolc2) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,eivolc2),0,5);
       UB1 = prctile(tmpdata1(:,eivolc2),99.5);
       tmpdata1(tmpdata1(:,eivolc2) <= LB1,eivolc2) = LB1;
       tmpdata1(tmpdata1(:,eivolc2) >= UB1,eivolc2) = UB1;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,eivolc2).^0.5 * 100];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg3_2 = [tmpout_reg3_2;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 tmpdata1 rlt0
    
    % Regression 4: Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) EIVOL(t)|(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,eivolc1) == -9.9999 | tmpdata1(:,sizec) == -9.9999 |...
    tmpdata1(:,bmc) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,eivolc1),0.5);
       UB3 = prctile(tmpdata1(:,eivolc1),99.5);
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,eivolc1) <= LB3,eivolc1) = LB3;
       tmpdata1(tmpdata1(:,eivolc1) >= UB3,eivolc1) = UB3;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,eivolc1) = tmpdata1(:,eivolc1).^0.5 * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,[sizec bmc eivolc1])];
       rlt0 = ols1(yyy0,xxx0);
       
       tmpout_reg4 = [tmpout_reg4;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 tmpdata1 rlt0
    % Regression 4(2): Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) EIVOL(t)|(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc)>3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,eivolc2) == -9.9999 | tmpdata1(:,sizec) == -9.9999 |...
    tmpdata1(:,bmc) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,eivolc2),0.5);
       UB3 = prctile(tmpdata1(:,eivolc2),99.5);
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,eivolc2) <= LB3,eivolc2) = LB3;
       tmpdata1(tmpdata1(:,eivolc2) >= UB3,eivolc2) = UB3;
       tmpdata1(:,sizec)  = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)    = log(tmpdata1(:,bmc));
       tmpdata1(:,eivolc2)= tmpdata1(:,eivolc2).^0.5 * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,[sizec bmc eivolc2])];
       rlt0 = ols1(yyy0,xxx0);       
       tmpout_reg4_2=[tmpout_reg4_2;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 tmpdata1 rlt0 rlt1 tmpdata2
    
    % Regression 5: Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) RET(-2,-7)
    %                             Ln(TURN) Ln(CVTURN) EIVOL(t)|(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,sizec) == -9.9999 | tmpdata1(:,bmc) == -9.9999|...
    tmpdata1(:,ret27c) == -9.9999 | tmpdata1(:,turnc) == -9.9999 |...
    tmpdata1(:,cvturnc) == -9.9999 |...
    tmpdata1(:,eivolc1) == -9.9999,:) = [];

    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ret27c),0.5);
       UB3 = prctile(tmpdata1(:,ret27c),99.5);
       LB4 = prctile(tmpdata1(:,turnc),0.5);
       UB4 = prctile(tmpdata1(:,turnc),99.5);
       LB5 = prctile(tmpdata1(:,cvturnc),0.5);
       UB5 = prctile(tmpdata1(:,cvturnc),99.5);
       LB6 = prctile(tmpdata1(:,eivolc1),0.5);
       UB6 = prctile(tmpdata1(:,eivolc1),99.5);
       
       tmpdata1(tmpdata1(:,sizec)   <=LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >=UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <=LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >=UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,ret27c)  <=LB3,ret27c)  = LB3;
       tmpdata1(tmpdata1(:,ret27c)  >=UB3,ret27c)  = UB3;
       tmpdata1(tmpdata1(:,turnc)   <=LB4,turnc)   = LB4;
       tmpdata1(tmpdata1(:,turnc)   >=UB4,turnc)   = UB4;
       tmpdata1(tmpdata1(:,cvturnc) <=LB5,cvturnc) = LB5;
       tmpdata1(tmpdata1(:,cvturnc) >=UB5,cvturnc) = UB5;
       tmpdata1(tmpdata1(:,eivolc1) <=LB6,eivolc1) = LB6;
       tmpdata1(tmpdata1(:,eivolc1) >=UB6,eivolc1) = UB6;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,ret27c)  = tmpdata1(:,ret27c) + 1;
       tmpdata1(:,turnc)   = log(tmpdata1(:,turnc) * 100);
       tmpdata1(:,cvturnc) = log(tmpdata1(:,cvturnc) * 100);
       tmpdata1(:,eivolc1) = tmpdata1(:,eivolc1).^0.5 * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1)...
               tmpdata1(:,[sizec bmc ret27c turnc cvturnc eivolc1])];
       rlt0 = ols1(yyy0,xxx0);
       
       tmpout_reg5 = [tmpout_reg5;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 LB4 UB4 LB5 UB5...
          LB6 UB6 tmpdata1 rlt0   
    % regression 5(2): Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) RET(-2,-7)
    %                                Ln(TURN) Ln(CVTURN) EIVOL(t)|(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,sizec) == -9.9999 | tmpdata1(:,bmc) == -9.9999 |...
    tmpdata1(:,ret27c) == -9.9999 | tmpdata1(:,turnc) == -9.9999 |...
    tmpdata1(:,cvturnc) == -9.9999 |...
    tmpdata1(:,eivolc2) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ret27c),0.5);
       UB3 = prctile(tmpdata1(:,ret27c),99.5);
       LB4 = prctile(tmpdata1(:,turnc),0.5);
       UB4 = prctile(tmpdata1(:,turnc),99.5);
       LB5 = prctile(tmpdata1(:,cvturnc),0.5);
       UB5 = prctile(tmpdata1(:,cvturnc),99.5);
       LB6 = prctile(tmpdata1(:,eivolc2),0.5);
       UB6 = prctile(tmpdata1(:,eivolc2),99.5);
       
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,ret27c)  <= LB3,ret27c)  = LB3;
       tmpdata1(tmpdata1(:,ret27c)  >= UB3,ret27c)  = UB3;
       tmpdata1(tmpdata1(:,turnc)   <= LB4,turnc)   = LB4;
       tmpdata1(tmpdata1(:,turnc)   >= UB4,turnc)   = UB4;
       tmpdata1(tmpdata1(:,cvturnc) <= LB5,cvturnc) = LB5;
       tmpdata1(tmpdata1(:,cvturnc) >= UB5,cvturnc) = UB5;
       tmpdata1(tmpdata1(:,eivolc2) <= LB6,eivolc2) = LB6;
       tmpdata1(tmpdata1(:,eivolc2) >= UB6,eivolc2) = UB6;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,ret27c)  = tmpdata1(:,ret27c) + 1;
       tmpdata1(:,turnc)   = log(tmpdata1(:,turnc) * 100);
       tmpdata1(:,cvturnc) = log(tmpdata1(:,cvturnc) * 100);
       tmpdata1(:,eivolc2) = tmpdata1(:,eivolc2).^0.5 * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1)...
               tmpdata1(:,[sizec bmc ret27c turnc cvturnc eivolc2])];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg5_2=[tmpout_reg5_2;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 LB4 UB4 LB5 UB5 LB6 UB6 tmpdata1 rlt0    tmpdata2 rlt1
   
    % Regression 6: Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) RET(-2,-7)
    %                             Ln(TURN) Ln(CVTURN) IVOL(t-1)
    tmpdata1=tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,sizec) == -9.9999 | tmpdata1(:,bmc) == -9.9999 | ...
    tmpdata1(:,ret27c) == -9.9999 | tmpdata1(:,turnc) == -9.9999 |...
    tmpdata1(:,cvturnc) == -9.9999 | tmpdata1(:,ivolc1) == -9.9999,:) = [];
    
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ret27c),0.5);
       UB3 = prctile(tmpdata1(:,ret27c),99.5);
       LB4 = prctile(tmpdata1(:,turnc),0.5);
       UB4 = prctile(tmpdata1(:,turnc),99.5);
       LB5 = prctile(tmpdata1(:,cvturnc),0.5);
       UB5 = prctile(tmpdata1(:,cvturnc),99.5);
       LB6 = prctile(tmpdata1(:,ivolc1),0.5);
       UB6 = prctile(tmpdata1(:,ivolc1),99.5);
       
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,ret27c)  <= LB3,ret27c)  = LB3;
       tmpdata1(tmpdata1(:,ret27c)  >= UB3,ret27c)  = UB3;
       tmpdata1(tmpdata1(:,turnc)   <= LB4,turnc)   = LB4;
       tmpdata1(tmpdata1(:,turnc)   >= UB4,turnc)   = UB4;
       tmpdata1(tmpdata1(:,cvturnc) <= LB5,cvturnc) = LB5;
       tmpdata1(tmpdata1(:,cvturnc) >= UB5,cvturnc) = UB5;
       tmpdata1(tmpdata1(:,ivolc1)  <= LB6,ivolc1)  = LB6;
       tmpdata1(tmpdata1(:,ivolc1)  >= UB6,ivolc1)  = UB6;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,ret27c)  = tmpdata1(:,ret27c) + 1;
       tmpdata1(:,turnc)   = log(tmpdata1(:,turnc) * 100);
       tmpdata1(:,cvturnc) = log(tmpdata1(:,cvturnc) * 100);
       tmpdata1(:,ivolc1)  = tmpdata1(:,ivolc1) * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1)...
               tmpdata1(:,[sizec bmc ret27c turnc cvturnc ivolc1])];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg6 = [tmpout_reg6;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 LB4 UB4 LB5 UB5...
          LB6 UB6 tmpdata1 rlt0      
      
    % regression 7: Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) RET(-2,-7)
    %                             Ln(TURN) Ln(CVTURN) IVOL(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,sizec) == -9.9999 | tmpdata1(:,bmc) == -9.9999 |...
    tmpdata1(:,ret27c) == -9.9999 | tmpdata1(:,turnc) == -9.9999 |...
    tmpdata1(:,cvturnc) == -9.9999 | tmpdata1(:,ivolc2) == -9.9999,:) = [];
    
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ret27c),0.5);
       UB3 = prctile(tmpdata1(:,ret27c),99.5);
       LB4 = prctile(tmpdata1(:,turnc),0.5);
       UB4 = prctile(tmpdata1(:,turnc),99.5);
       LB5 = prctile(tmpdata1(:,cvturnc),0.5);
       UB5 = prctile(tmpdata1(:,cvturnc),99.5);
       LB6 = prctile(tmpdata1(:,ivolc2),0.5);
       UB6 = prctile(tmpdata1(:,ivolc2),99.5);
       
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,ret27c)  <= LB3,ret27c)  = LB3;
       tmpdata1(tmpdata1(:,ret27c)  >= UB3,ret27c)  = UB3;
       tmpdata1(tmpdata1(:,turnc)   <= LB4,turnc)   = LB4;
       tmpdata1(tmpdata1(:,turnc)   >= UB4,turnc)   = UB4;
       tmpdata1(tmpdata1(:,cvturnc) <= LB5,cvturnc) = LB5;
       tmpdata1(tmpdata1(:,cvturnc) >= UB5,cvturnc) = UB5;
       tmpdata1(tmpdata1(:,ivolc2)  <= LB6,ivolc1)  = LB6;
       tmpdata1(tmpdata1(:,ivolc2)  >= UB6,ivolc1)  = UB6;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,ret27c)  = tmpdata1(:,ret27c) + 1;
       tmpdata1(:,turnc)   = log(tmpdata1(:,turnc) * 100);
       tmpdata1(:,cvturnc) = log(tmpdata1(:,cvturnc) * 100);
       tmpdata1(:,ivolc2)  = tmpdata1(:,ivolc2) * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1)...
               tmpdata1(:,[sizec bmc ret27c turnc cvturnc ivolc2])];
       rlt0 = ols1(yyy0,xxx0);
       
       tmpout_reg7 = [tmpout_reg7;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 LB4 UB4 LB5 UB5...
          LB6 UB6 tmpdata1 rlt0      
     
    A_out_Table5_reg1   = [A_out_Table5_reg1;tmpout_reg1];
    A_out_Table5_reg2   = [A_out_Table5_reg2;tmpout_reg2];
    A_out_Table5_reg3   = [A_out_Table5_reg3;tmpout_reg3];
    A_out_Table5_reg3_2 = [A_out_Table5_reg3_2;tmpout_reg3_2];
    A_out_Table5_reg4   = [A_out_Table5_reg4;tmpout_reg4];
    A_out_Table5_reg4_2 = [A_out_Table5_reg4_2;tmpout_reg4_2];
    A_out_Table5_reg5   = [A_out_Table5_reg5;tmpout_reg5];
    A_out_Table5_reg5_2 = [A_out_Table5_reg5_2;tmpout_reg5_2];
    A_out_Table5_reg6   = [A_out_Table5_reg6;tmpout_reg6];
    A_out_Table5_reg7   = [A_out_Table5_reg7;tmpout_reg7];
    
    clear tmpdata tmpout_reg1 tmpout_reg2 tmpout_reg3 tmpout_reg3_2...
          tmpout_reg4 mpout_reg4_2 tmpout_reg5 tmpout_reg5_2 tmpout_reg6...
          tmpout_reg7
end
if SamplePeriod == 1
save Results_Table5_allstocks_1963_2006.mat A_out_Table5_reg1...
     A_out_Table5_reg2 A_out_Table5_reg3 A_out_Table5_reg3_2...
     A_out_Table5_reg4 A_out_Table5_reg4_2 A_out_Table5_reg5...
     A_out_Table5_reg5_2 A_out_Table5_reg6 A_out_Table5_reg7
elseif SamplePeriod == 2
save Results_Table5_allstocks_2007_2017.mat A_out_Table5_reg1...
     A_out_Table5_reg2 A_out_Table5_reg3 A_out_Table5_reg3_2...
     A_out_Table5_reg4 A_out_Table5_reg4_2 A_out_Table5_reg5...
     A_out_Table5_reg5_2 A_out_Table5_reg6 A_out_Table5_reg7
elseif SamplePeriod == 3
save Results_Table5_allstocks_1963_2017.mat A_out_Table5_reg1...
     A_out_Table5_reg2 A_out_Table5_reg3 A_out_Table5_reg3_2...
     A_out_Table5_reg4 A_out_Table5_reg4_2 A_out_Table5_reg5...
     A_out_Table5_reg5_2 A_out_Table5_reg6 A_out_Table5_reg7
elseif SamplePeriod == 4
save Results_Table5_allstocks_1926_1963.mat A_out_Table5_reg1...
     A_out_Table5_reg2 A_out_Table5_reg3 A_out_Table5_reg3_2...
     A_out_Table5_reg4 A_out_Table5_reg4_2 A_out_Table5_reg5...
     A_out_Table5_reg5_2 A_out_Table5_reg6 A_out_Table5_reg7
end

if SamplePeriod == 1
load Results_Table5_allstocks_1963_2006.mat A_out_Table5_reg1...
     A_out_Table5_reg2 A_out_Table5_reg3 A_out_Table5_reg3_2...
     A_out_Table5_reg4 A_out_Table5_reg4_2 A_out_Table5_reg5...
     A_out_Table5_reg5_2 A_out_Table5_reg6 A_out_Table5_reg7
elseif SamplePeriod == 2
load Results_Table5_allstocks_2007_2017.mat A_out_Table5_reg1...
     A_out_Table5_reg2 A_out_Table5_reg3 A_out_Table5_reg3_2...
     A_out_Table5_reg4 A_out_Table5_reg4_2 A_out_Table5_reg5...
     A_out_Table5_reg5_2 A_out_Table5_reg6 A_out_Table5_reg7
elseif SamplePeriod == 3
load Results_Table5_allstocks_1963_2017.mat A_out_Table5_reg1...
     A_out_Table5_reg2 A_out_Table5_reg3 A_out_Table5_reg3_2...
     A_out_Table5_reg4 A_out_Table5_reg4_2 A_out_Table5_reg5...
     A_out_Table5_reg5_2 A_out_Table5_reg6 A_out_Table5_reg7
elseif SamplePeriod == 4
load Results_Table5_allstocks_1926_1963.mat A_out_Table5_reg1...
     A_out_Table5_reg2 A_out_Table5_reg3 A_out_Table5_reg3_2...
     A_out_Table5_reg4 A_out_Table5_reg4_2 A_out_Table5_reg5...
     A_out_Table5_reg5_2 A_out_Table5_reg6 A_out_Table5_reg7
end

indinclude_Rt = 0;
% (0 = EIVOL (t)|(t-1); 1= EIVOL (t)|(t))
A_out_Table5  = zeros(14,10);
for reg = 1:7
    if reg == 1
       regdata = A_out_Table5_reg1;
       Trow    = [1;2];
       Tcolumn = [1 2 3 10];
    elseif reg == 2
       regdata = A_out_Table5_reg2;
       Trow    = [3;4];
       Tcolumn = [1 2 3 4 5 6 10];
    elseif reg == 3
       if indinclude_Rt==1
       regdata = A_out_Table5_reg3_2;
       else
       regdata = A_out_Table5_reg3;
       end
       Trow    = [5;6];
       Tcolumn = [7 10];
    elseif reg == 4
       if indinclude_Rt==1
       regdata = A_out_Table5_reg4_2;
       else
       regdata = A_out_Table5_reg4;
       end
       Trow    = [7;8];
       Tcolumn = [2 3 7 10];
    elseif reg == 5
       if indinclude_Rt==1
       regdata = A_out_Table5_reg5_2;
       else
       regdata = A_out_Table5_reg5;
       end
       Trow    = [9;10];
       Tcolumn = [2 3 4 5 6 7 10];
    elseif reg == 6
       regdata = A_out_Table5_reg6;
       Trow    = [11;12];
       Tcolumn = [2 3 4 5 6 8 10];
    elseif reg == 7
       regdata = A_out_Table5_reg7;
       Trow    = [13;14];
       Tcolumn = [2 3 4 5 6 9 10];
    end
    
    for ccc = 3:(size(regdata,2) - 1)
        tmp  = regdata(:,ccc);
        rlt0 = nwest(tmp,ones(size(tmp,1),1),0);
        A_out_Table5(Trow(1),Tcolumn(ccc-2)) = rlt0.beta;
        A_out_Table5(Trow(2),Tcolumn(ccc-2)) = rlt0.tstat;
        clear tmp rlt0
    end
    for ccc = size(regdata,2)
        tmp = regdata(:,ccc);
        A_out_Table5(Trow(1),Tcolumn(end))   = mean(tmp) * 100;
        clear tmp
    end
    clear regdata Trow Tcolumn 
end
%}
% ------------------------------------------------------------------------%
% Table 6 Sorting Portfolio Analysis
% ------------------------------- Table 6 --------------------------------%
%{
clear
clc

SamplePeriod = 1;
% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963

IndSave      =0;

data = [];
for num0 = 1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0

monthall = unique(data(:,2));

% RET(t)
retc       = 3;
% Price(t-1)
lagprcc    = 9;
% SHROUT(t-1)
lagshroutc = 10;
% IVOL(t)
ivolc      = 13;
% BETA
betac      = 15;
% SIZE
sizec      = 7;
% BM
bmc        = 8;
% EIVOL(t)|(t-1)
sortc      = 23;
% EIVOL(t)|(t)
sortc_2    = 35;

A_out_Table6        = [];
A_out_Table6_size   = [];
A_out_Table6_bm     = [];

A_out_Table6_2      = [];
A_out_Table6_size_2 = [];
A_out_Table6_bm_2   = [];

% Minimum Number of Stocks within a Month to do sorting analysis
cut1 = 50;

for j = 1:size(monthall,1)
    
    tmpdata = data(data(:,2) == monthall(j),:);
    tmpdata = tmpdata(tmpdata(:,sortc) ~= -9.9999,:);
    if size(tmpdata,1) >= cut1
        
       % Sort by EIVOL(t)|(t-1)
       tmpdata   = sortrows(tmpdata,sortc);
       interval0 = floor(size(tmpdata,1) / 10);
       tmpout = [];
       
       for ggg = 1:10
           tmptmpout = [];
           if ggg == 1
              datagroup = tmpdata(1:interval0,:);
           elseif ggg == 2
              datagroup = tmpdata(interval0 + 1:interval0*2,:);
           elseif ggg == 3
              datagroup = tmpdata(interval0*2 + 1:interval0*3,:);
           elseif ggg == 4
              datagroup = tmpdata(interval0*3 + 1:interval0*4,:);
           elseif ggg == 5
              datagroup = tmpdata(interval0*4 + 1:interval0*5,:);
           elseif ggg == 6
              datagroup = tmpdata(interval0*5 + 1:interval0*6,:);
           elseif ggg == 7
              datagroup = tmpdata(interval0*6 + 1:interval0*7,:);
           elseif ggg == 8
              datagroup = tmpdata(interval0*7 + 1:interval0*8,:);
           elseif ggg == 9
              datagroup = tmpdata(interval0*8 + 1:interval0*9,:);
           elseif ggg == 10
              datagroup = tmpdata(interval0*9 + 1:end,:);
           end
           
           for ccc = [retc sortc ivolc betac sizec bmc]
               % Portfolio Value-weighted RET and Equal-weighted RET
               if ccc == retc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999 & ...
                               datagroup(:,lagprcc) ~= -9.9999 & ...
                               datagroup(:,lagshroutc) ~= -9.9999,:);
                  datagroup1(:,57) = datagroup1(:,lagprcc).* ...
                                     datagroup1(:,lagshroutc);
                  tmp1       = sum(datagroup1(:,ccc).*datagroup1(:,57)/...
                               sum(datagroup1(:,57)));
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp2       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout tmp1 * 100 tmp2 * 100];
               elseif ccc == sortc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc).^0.5);
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1 * 100];
               elseif ccc == ivolc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1 * 100];
               elseif ccc == betac
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1];
               elseif ccc == sizec
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  A_out_Table6_size = [A_out_Table6_size;...
                  ones(size(datagroup1,1),1) * ggg datagroup1(:,ccc)];
               elseif ccc == bmc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  A_out_Table6_bm   = [A_out_Table6_bm;...
                  ones(size(datagroup1,1),1) * ggg datagroup1(:,ccc)];
               end
               clear datagroup1 tmp1 tmp2
           end
           tmpout = [tmpout;monthall(j) ggg tmptmpout];
           clear tmptmpout datagroup
       end
       clear interval0
       A_out_Table6 = [A_out_Table6;tmpout];
    end
    clear tmpout tmpdata
    
    tmpdata = data(data(:,2) == monthall(j),:);
    tmpdata = tmpdata(tmpdata(:,sortc_2) ~= -9.9999,:);
    if size(tmpdata,1) >= cut1
       % Sort by EIVOL(t)|(t)  
       tmpdata   = sortrows(tmpdata,sortc_2);
       interval0 = floor(size(tmpdata,1) / 10);
       tmpout    = [];
       for ggg = 1:10
           tmptmpout = [];
           if ggg == 1
              datagroup = tmpdata(1:interval0,:);
           elseif ggg == 2
              datagroup = tmpdata(interval0 + 1:interval0*2,:);
           elseif ggg == 3
              datagroup = tmpdata(interval0*2 + 1:interval0*3,:);
           elseif ggg == 4
              datagroup = tmpdata(interval0*3 + 1:interval0*4,:);
           elseif ggg == 5
              datagroup = tmpdata(interval0*4 + 1:interval0*5,:);
           elseif ggg == 6
              datagroup = tmpdata(interval0*5 + 1:interval0*6,:);
           elseif ggg == 7
              datagroup = tmpdata(interval0*6 + 1:interval0*7,:);
           elseif ggg == 8
              datagroup = tmpdata(interval0*7 + 1:interval0*8,:);
           elseif ggg == 9
              datagroup = tmpdata(interval0*8 + 1:interval0*9,:);
           elseif ggg == 10
              datagroup = tmpdata(interval0*9 + 1:end,:);
              
           end
           for ccc = [retc sortc_2 ivolc betac sizec bmc]
               
               if ccc == retc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999 & ...
                  datagroup(:,lagprcc) ~= -9.9999 & ...
                  datagroup(:,lagshroutc) ~= -9.9999,:);
                  datagroup1(:,57) = datagroup1(:,lagprcc).*...
                  datagroup1(:,lagshroutc);
                  tmp1       = sum(datagroup1(:,ccc).*datagroup1(:,57)/...
                  sum(datagroup1(:,57)));
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp2       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout tmp1 * 100 tmp2 * 100];
               elseif ccc == sortc_2
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc).^0.5);
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1 * 100];
               elseif ccc == ivolc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1 * 100];
               elseif ccc == betac
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1];
               elseif ccc == sizec
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  A_out_Table6_size_2 = [A_out_Table6_size_2;...
                  ones(size(datagroup1,1),1) * ggg datagroup1(:,ccc)];
               elseif ccc == bmc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  A_out_Table6_bm_2   = [A_out_Table6_bm_2;...
                  ones(size(datagroup1,1),1) * ggg datagroup1(:,ccc)];
               end
               clear datagroup1 tmp1 tmp2
           end
           tmpout = [tmpout;monthall(j) ggg tmptmpout];
           clear tmptmpout datagroup
       end
       clear interval0
       A_out_Table6_2 = [A_out_Table6_2;tmpout];
    end
    clear tmpout tmpdata
    
end
if IndSave == 1
if SamplePeriod == 1
save Results_Table6_allstocks_1963_2006.mat A_out_Table6_2...
     A_out_Table6 A_out_Table6_bm_2 A_out_Table6_bm...
     A_out_Table6_size A_out_Table6_size_2
elseif SamplePeriod == 2
save Results_Table6_allstocks_2007_2017.mat A_out_Table6_2...
     A_out_Table6 A_out_Table6_bm_2 A_out_Table6_bm...
     A_out_Table6_size A_out_Table6_size_2
elseif SamplePeriod == 3
save Results_Table6_allstocks_1963_2017.mat A_out_Table6_2...
     A_out_Table6 A_out_Table6_bm_2 A_out_Table6_bm...
     A_out_Table6_size A_out_Table6_size_2
elseif SamplePeriod == 4
save Results_Table6_allstocks_1926_1963.mat A_out_Table6_2...
     A_out_Table6 A_out_Table6_bm_2 A_out_Table6_bm...
     A_out_Table6_size A_out_Table6_size_2
end
end
%}
% Description of Data on Correlations
%{
No.VarName      Description

A_out_Table6
1  DATE         YearMonth (yyyymm)
2  GroupNo.	    Group Number (from 1 to 10) of stocks 
                sorted by EIVOL(t)|(t-1)
3  VWRETP       Value-weighted Portfolio RET (t)
4  EWRETP       Equal-weighted Portfolio RET (t)
5  NumStock     Number of Stocks with EIVOL(t)|(t-1) within a portfolio
6  MeanEIVOL    Mean EIVOL(t)|(t-1)
7  NumStock     Number of Stocks with IVOL(t) within a portfolio
8  MeanIVOL     Mean IVOL(t)
9  NumStock     Number of Stocks with Beta within a portfolio
10 MeanBeta     Mean Beta

A_out_Table6_2
1  DATE         YearMonth (yyyymm)
2  GroupNo.	    Group Number (from 1 to 10) of stocks 
                sorted by EIVOL(t)|(t)
3  VWRETP       Value-weighted Portfolio RET (t)
4  EWRETP       Equal-weighted Portfolio RET (t)
5  NumStock     Number of Stocks with EIVOL(t)|(t) within a portfolio
6  MeanEIVOL    Mean EIVOL(t)|(t)
7  NumStock     Number of Stocks with IVOL(t) within a portfolio
8  MeanIVOL     Mean IVOL(t)
9  NumStock     Number of Stocks with Beta within a portfolio
10 MeanBeta     Mean Beta

A_out_Table6_size
1  GroupNo.	    Group Number (from 1 to 10) of stocks 
                sorted by EIVOL(t)|(t-1)
2  size         Market Capitalization as of June (in $ thousands)

A_out_Table6_size_2
1  GroupNo.	    Group Number (from 1 to 10) of stocks 
                sorted by EIVOL(t)|(t)
2  size         Market Capitalization as of June (in $ thousands)

A_out_Table6_bm
1  GroupNo.	    Group Number (from 1 to 10) of stocks 
                sorted by EIVOL(t)|(t-1)
2  BM           Book to Market Ratio

A_out_Table6_bm_2
1  GroupNo.	    Group Number (from 1 to 10) of stocks 
                sorted by EIVOL(t)|(t)
2  BM           Book to Market Ratio

%}
%{
clear
clc

indinclude_Rt = 0;
% (0 = EIVOL (t)|(t-1); 1= EIVOL (t)|(t))

A_Table6   = zeros(8,10);

load dataother.mat data_famafrench3factor_monthly
datafama3 = data_famafrench3factor_monthly;
clear data_famafrench3factor_monthly

if SamplePeriod == 1
load Results_Table6_allstocks_1963_2006.mat
elseif SamplePeriod == 3
load Results_Table6_allstocks_2007_2017.mat
elseif SamplePeriod == 3
load Results_Table6_allstocks_1963_2017.mat
elseif SamplePeriod == 4
load Results_Table6_allstocks_1926_1963.mat
end

if indinclude_Rt==1
   data     = A_out_Table6_2;
   datasize = A_out_Table6_size_2;
   databm   = A_out_Table6_bm_2;
else
   data     = A_out_Table6;
   datasize = A_out_Table6_size;
   databm   = A_out_Table6_bm;
end

for ggg = 1:10
    
    tmp     = data(data(:,2) == ggg,:);
    tmpsize = datasize(datasize(:,1) == ggg,:);
    tmpbm   = databm(databm(:,1) == ggg,:);
    
    Trow = 1;
    % VWRET RET
    for ccc = 3:4
        tmptmp = tmp(:,ccc);
        A_Table6(Trow,ggg) = mean(tmptmp);
        Trow = Trow + 1;
        clear tmptmp
    end
    % EIVOL IVOL Beta
    for ccc = [6 8 10]
        tmptmp = sum(tmp(:,ccc).* tmp(:,ccc - 1)) / sum(tmp(:,ccc - 1));
        A_Table6(Trow,ggg) = tmptmp;
        Trow = Trow + 1;
        clear tmptmp
    end
    % Calculate Alpha
    for ccc = 3
        tmptmp = tmp(:,[1 ccc]);
        month0 = unique(tmptmp(:,1));
        for jj = 1:size(month0,1)
            tmpfama3 = datafama3(datafama3(:,1) == month0(jj),:);
            tmptmp(tmptmp(:,1) == month0(jj),[3 4 5 6]) = ...
            tmpfama3([2 3 4 5]);
            clear tmpfama3
        end
        yyy0 = tmptmp(:,2) - tmptmp(:,6);
        xxx0 = [ones(size(yyy0,1),1) tmptmp(:,[3 4 5])];
        rlt0 = ols1(yyy0,xxx0);
        A_Table6(Trow,ggg) = rlt0.beta(1);
        clear tmptmp
    end
    
    A_Table6(6,ggg) = prctile(tmpsize(:,2),50) / 1000;
    A_Table6(7,ggg) = prctile(tmpbm(:,2),50);
     
end

%}
% ------------------------------------------------------------------------%
% Table 7 & 8 Return dispersion of portfolios sorted by IVOL
% ---------------------------- Table 7 & 8--------------------------------%
%{

clear
clc

SamplePeriod = 1;
IndSave      =0;

data = [];
for num0 = 1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0

monthall = unique(data(:,2));

A_out_Table7 = [];
A_out_Table8 = [];


cut1 = 50;

% RET(t)
retc        = 3;
% RET(t-1)
lagretc     = 5;
% Price(t-1)
lagprcc     = 9;
% SHROUT(t-1)
lagshroutc  = 10;
% Price(t-2)
lagprcc2    = 11;
% SHROUT(t-2)
lagshroutc2 = 12;

% Market Cap (t-1)
sizec       = 60;
% Market Cap (t-2)
sizec2      = 61;

% IVOL(t)
ivolc       = 13;
% IVOL(t-1);
c_IVOLlag   = 14;

sortc       = c_IVOLlag;
sortc_2     = lagretc;

for j = 1:size(monthall,1)
    
    tmpdata = data(data(:,2) == monthall(j),:);
    tmpdata = tmpdata(tmpdata(:,sortc) ~= -9.9999,:);
    
    if size(tmpdata,1) >= cut1
       % Sort stocks by IVOL(t-1)
       tmpdata = sortrows(tmpdata,sortc);
       interval0 = floor(size(tmpdata,1) / 5);
       tmpout = [];
       for ggg = 1:5
           tmptmpout = [];
           if ggg == 1
              datagroup = tmpdata(1:interval0,:);
           elseif ggg == 2
              datagroup = tmpdata(interval0 + 1:interval0*2,:);
           elseif ggg == 3
              datagroup = tmpdata(interval0*2 + 1:interval0*3,:);
           elseif ggg == 4
              datagroup = tmpdata(interval0*3 + 1:interval0*4,:);
           elseif ggg == 5
              datagroup = tmpdata(interval0*4 + 1:end,:);              
           end
           
           % Calculate Market Capitalization (t-1) and (t-2)
           datagroup(datagroup(:,lagprcc) ~= -9.9999 & ...
                     datagroup(:,lagshroutc) ~= -9.9999,sizec)= ...
                     datagroup(datagroup(:,lagprcc) ~= -9.9999 & ...
                     datagroup(:,lagshroutc) ~= -9.9999,lagprcc).*...
                     datagroup(datagroup(:,lagprcc) ~= -9.9999 & ...
                     datagroup(:,lagshroutc) ~= -9.9999,lagshroutc);
           datagroup(datagroup(:,lagprcc) == -9.9999 | ...
                     datagroup(:,lagshroutc) == -9.9999,sizec) = -9.9999;
                 
           datagroup(datagroup(:,lagprcc2) ~= -9.9999 & ...
                     datagroup(:,lagshroutc2) ~= -9.9999,sizec2)= ...
                     datagroup(datagroup(:,lagprcc2) ~= -9.9999 & ...
                     datagroup(:,lagshroutc2) ~= -9.9999,lagprcc2).*...
                     datagroup(datagroup(:,lagprcc2) ~= -9.9999 & ...
                     datagroup(:,lagshroutc2) ~= -9.9999,lagshroutc2);
           datagroup(datagroup(:,lagprcc2) == -9.9999 | ...
                     datagroup(:,lagshroutc2) == -9.9999,sizec2) = -9.9999;
                 
       
           size0 = size(datagroup,1);
           for ccc = [sortc sizec retc lagretc]
               % Calculate Value-weighted and Equal-weighted 
               % Portfolio RET (t)
               if ccc == retc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999 & ...
                               datagroup(:,lagprcc) ~= -9.9999 & ...
                               datagroup(:,lagshroutc) ~= -9.9999,:);
                           
                  tmp1 = sum(datagroup1(:,ccc).*datagroup1(:,sizec)/...
                         sum(datagroup1(:,sizec)));
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp2 = mean(datagroup1(:,ccc));
                  tmptmpout = [tmptmpout tmp1 * 100 tmp2 * 100];
                  clear tmp1 tmp2
               % Calculate Value-weighted and Equal-weighted 
               % Portfolio RET (t-1)
               elseif ccc==lagretc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999 & ...
                               datagroup(:,lagprcc2) ~= -9.9999 & ...
                               datagroup(:,lagshroutc2) ~= -9.9999,:);
                           
                  tmp1 = sum(datagroup1(:,ccc).*datagroup1(:,sizec2)/...
                         sum(datagroup1(:,sizec2)));
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp2  = mean(datagroup1(:,ccc));
                  tmptmpout = [tmptmpout tmp1 * 100 tmp2 * 100];
                  clear tmp1 tmp2
               % Mean IVOL (t-1)
               elseif ccc==sortc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1 = mean(datagroup1(:,ccc));
                  tmptmpout = [tmptmpout tmp1 * 100];
                  clear tmp1
               % Mean SIZE (t-1)
               elseif ccc==sizec
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp1 = mean(datagroup1(:,ccc));
                  tmptmpout = [tmptmpout tmp1 / 1000];
                  clear tmp1
               end
               clear datagroup1 
           end
           tmpout = [tmpout;monthall(j) ggg size0 tmptmpout];
           clear tmptmpout datagroup size0
       end
       clear interval0
       A_out_Table7 = [A_out_Table7;tmpout];
    end
    clear tmpout tmpdata
    
    tmpdata = data(data(:,2) == monthall(j),:);
    tmpdata = tmpdata(tmpdata(:,c_IVOLlag) ~= -9.9999,:);
    if size(tmpdata,1) >= cut1
       tmpdata = sortrows(tmpdata,c_IVOLlag);
       interval0 = floor(size(tmpdata,1)/5);
       % Select 40% stocks with high IVOL(t-1)
       tmpdata1 = tmpdata(interval0*3 + 1:end,:);
       tmpdata1 = tmpdata1(tmpdata1(:,sortc_2) ~= -9.9999,:);
       % Sort stocks by RET(t-1)
       tmpdata1 =sortrows(tmpdata1,sortc_2);
       interval0 = floor(size(tmpdata1,1) / 5);
       tmpout = [];
       for ggg= 1:5
           tmptmpout = [];
           if ggg == 1
              datagroup = tmpdata1(1:interval0,:);
           elseif ggg == 2
              datagroup = tmpdata1(interval0 + 1:interval0*2,:);
           elseif ggg == 3
              datagroup = tmpdata1(interval0*2 + 1:interval0*3,:);
           elseif ggg == 4
              datagroup = tmpdata1(interval0*3 + 1:interval0*4,:);
           elseif ggg == 5
              datagroup = tmpdata1(interval0*4 + 1:end,:);
           end

           size0=size(datagroup,1);
           for ccc = [sortc_2 retc sizec ivolc]
               % Calculate Value-weighted and Equal-weighted 
               % Portfolio RET (t)
               if ccc == retc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999 & ...
                               datagroup(:,lagprcc) ~= -9.9999 & ...
                               datagroup(:,lagshroutc) ~= -9.9999,:);
                  tmp1 = sum(datagroup1(:,ccc).*datagroup1(:,sizec)/...
                         sum(datagroup1(:,sizec)));
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp2 = mean(datagroup1(:,ccc));
                  tmptmpout = [tmptmpout tmp1 * 100 tmp2 * 100];
                  clear datagroup1 tmp1 tmp2
               % Mean RET(t-1)
               elseif ccc==sortc_2
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp1 = mean(datagroup1(:,ccc));
                  tmptmpout = [tmptmpout tmp1 * 100];
                  clear datagroup1 tmp1
               % Mean SIZE(t-1)
               elseif ccc==sizec
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp1 = mean(datagroup1(:,ccc));
                  tmptmpout=[tmptmpout tmp1 / 1000];
                  clear datagroup1 tmp1
               % Mean IVOL(t)
               elseif ccc==ivolc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp1 = mean(datagroup1(:,ccc));
                  tmptmpout=[tmptmpout tmp1 * 100];
                  clear datagroup1 tmp1
               end
               clear datagroup1 tmp1 tmp2
           end
           tmpout = [tmpout;monthall(j) ggg size0 tmptmpout];
           clear tmptmpout datagroup size0
       end
       clear interval0
       A_out_Table8 = [A_out_Table8;tmpout];
    end
    clear tmpout tmpdata tmpdata1
    
end
if SamplePeriod == 1
   save Results_Table8_allstocks_1963_2006.mat A_out_Table7   
   save Results_Table9_allstocks_1963_2006.mat A_out_Table8
elseif SamplePeriod == 2
   save Results_Table8_allstocks_2007_2017.mat A_out_Table7   
   save Results_Table9_allstocks_2007_2017.mat A_out_Table8
elseif SamplePeriod == 3
   save Results_Table8_allstocks_1963_2017.mat A_out_Table7   
   save Results_Table9_allstocks_1963_2017.mat A_out_Table8
elseif SamplePeriod == 4
   save Results_Table8_allstocks_1926_1963.mat A_out_Table7   
   save Results_Table9_allstocks_1926_1963.mat A_out_Table8
end
%}
% Data Description for Portfolios sorted by IVOL(t-1)
%{

A_out_Table7   
No.VarName      Description
1  DATE         YearMonth (yyyymm)
2  GroupNo.     Group Number (Sorted by IVOL(t-1))
3  NumStock     Number of Stocks within a portfolio
4  IVOL(t-1)    Mean IVOL(t-1)
5  SIZE(t-1)    Market Capitalization at the end of last month
6  VWRET(t)     Value-weighted Portfolio Return in month (t)
7  EWRET(t)     Equal-wegihted Portfolio Return in month (t)
8  VWRET(t-1)   Value-weighted Portfolio Return in month (t-1)
9  EWRET(t-1)   Equal-wegihted Portfolio Return in month (t-1)

A_out_Table8  
No.VarName      Description
1  DATE         YearMonth (yyyymm)
2  GroupNo.     Group Number (Sorted by RET(t-1))
3  NumStock     Number of Stocks within a portfolio
4  EWRET(t-1)   Equal-wegihted Portfolio Return in month (t-1)
5  VWRET(t)     Value-weighted Portfolio Return in month (t)
6  EWRET(t)     Equal-wegihted Portfolio Return in month (t)
7  SIZE(t-1)    Market Capitalization at the end of last month
8  IVOL(t)      Mean IVOL(t)

%}
%{

clear
clc

SamplePeriod = 1;

if SamplePeriod == 1
   load Results_Table8_allstocks_1963_2006.mat A_out_Table7   
   load Results_Table9_allstocks_1963_2006.mat A_out_Table8
elseif SamplePeriod == 2
   load Results_Table8_allstocks_2007_2017.mat A_out_Table7   
   load Results_Table9_allstocks_2007_2017.mat A_out_Table8
elseif SamplePeriod == 3
   load Results_Table8_allstocks_1963_2017.mat A_out_Table7   
   load Results_Table9_allstocks_1963_2017.mat A_out_Table8
elseif SamplePeriod == 4
   load Results_Table8_allstocks_1926_1963.mat A_out_Table7   
   load Results_Table9_allstocks_1926_1963.mat A_out_Table8
end
load dataother.mat data_famafrench3factor_monthly
datafama3 = data_famafrench3factor_monthly;
clear data_famafrench3factor_monthly

% Match monthly portfolio data with monthly Fama & French Three factors
% Calculate percentage of market share
monthall = unique(A_out_Table7(:,1));
for j= 1:size(monthall,1)
    tmp = A_out_Table7(A_out_Table7(:,1) == monthall(j),:);
    tmpfama3 = datafama3(datafama3(:,1) == monthall(j),:);
    marketcapall = sum(tmp(:,5));
    
    A_out_Table7(A_out_Table7(:,1) == monthall(j),10) = ...
    A_out_Table7(A_out_Table7(:,1) == monthall(j),5) / marketcapall;
    clear marketcapall
    
    A_out_Table7(A_out_Table7(:,1) == monthall(j),11) = tmpfama3(2);
    A_out_Table7(A_out_Table7(:,1) == monthall(j),12) = tmpfama3(3);
    A_out_Table7(A_out_Table7(:,1) == monthall(j),13) = tmpfama3(4);
    A_out_Table7(A_out_Table7(:,1) == monthall(j),14) = tmpfama3(5);
    A_out_Table8(A_out_Table8(:,1) == monthall(j),9)  = tmpfama3(2);
    A_out_Table8(A_out_Table8(:,1) == monthall(j),10) = tmpfama3(3);
    A_out_Table8(A_out_Table8(:,1) == monthall(j),11) = tmpfama3(4);
    A_out_Table8(A_out_Table8(:,1) == monthall(j),12) = tmpfama3(5);
    
    if j == 1
       month0 = monthall(j) - floor(monthall(j) / 100) * 100;
       if month0 == 1
          month_1 = (floor(monthall(j) / 100) - 1) * 100 + 12;
       else
          month_1 = monthall(j) - 1;
       end
       clear momth0
    else
        month_1 = monthall(j - 1);
    end
    
    tmpfama3lag = datafama3(datafama3(:,1) == month_1,:);
    A_out_Table7(A_out_Table7(:,1) == monthall(j),15) = tmpfama3lag(2);
    A_out_Table7(A_out_Table7(:,1) == monthall(j),16) = tmpfama3lag(3);
    A_out_Table7(A_out_Table7(:,1) == monthall(j),17) = tmpfama3lag(4);
    A_out_Table7(A_out_Table7(:,1) == monthall(j),18) = tmpfama3lag(5);
    
    clear marketcapall tmp tmpfama3 tmpfama3lag month_1
end
clear monthall j
    

for ggg = 1:5
    tmp = A_out_Table7( A_out_Table7(:,2) == ggg,:);
    A_Table8(ggg*2 - 1,1) = ggg;

    Tcolumn = 2;
        for ccc = [3 4 5 10 7]
            tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
            % Number of Stock-Month Observations 
            if ccc == 3
               tmptmp1=tmptmp(tmptmp(:,4) ~= -9.9999 & ...
                       tmptmp(:,6) ~= -9.9999,:);
               A_Table8(ggg*2 - 1,Tcolumn) = sum(tmptmp1(:,ccc));
            end
            % Mean IVOL(t-1)
            if ccc == 4
               A_Table8(ggg*2 - 1,Tcolumn) = mean(tmptmp(:,ccc));
            end
            % Mean SIZE(t-1)
            if ccc == 5
               A_Table8(ggg*2 - 1,Tcolumn) = mean(tmptmp(:,ccc));
            end
            % Mean Market Share
            if ccc == 10
               A_Table8(ggg*2 - 1,Tcolumn) = mean(tmptmp(:,ccc));
            end
            % Mean EWRET(t)
            if ccc == 7
               A_Table8(ggg*2 - 1,Tcolumn) = mean(tmptmp(:,ccc));
            end
            Tcolumn = Tcolumn + 1;
            clear tmptmp
        end
        % Mean VWXRET(t)
        % Portfolio VW Alpha(t) relative to Fama & French 
        % Three Factor Model
        for ccc = [6]
            tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
            A_Table8(ggg*2 - 1,Tcolumn) = ...
            mean(tmptmp(:,ccc) - tmptmp(:,14));
            % Calculate Alpha
            yyy0 = tmptmp(:,ccc) - tmptmp(:,14);
            xxx0 = [ones(size(yyy0,1),1) tmptmp(:,[11 12 13])];
            rlt0 = ols1(yyy0,xxx0);
            A_Table8(ggg*2 - 1,Tcolumn + 1) = rlt0.beta(1);
            A_Table8(ggg*2,Tcolumn + 1)     = rlt0.tstat(1);
            if ggg == 1
               ff3alpha_g1 = rlt0.beta(1) + rlt0.resid;
            elseif ggg == 5
               ff3alpha_g5 = rlt0.beta(1) + rlt0.resid;
            end
            clear tmptmp yyy0 xxx0 rlt0
        end
        Tcolumn = Tcolumn + 2;
        % Mean RET(t-1)
        for ccc = [9]
           tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
           A_Table8(ggg*2 - 1,Tcolumn) = mean(tmptmp(:,ccc));
           clear tmptmp
        end
        Tcolumn = Tcolumn + 1;
        % Mean VWXRET(t-1)
        % Portfolio VW Alpha(t-1) relative to Fama & French 
        % Three Factor Model
        for ccc = [8]
            tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
            A_Table8(ggg*2-1,Tcolumn) = ...
            mean(tmptmp(:,ccc) - tmptmp(:,18));

            yyy0 = tmptmp(:,ccc) - tmptmp(:,18);
            xxx0 = [ones(size(yyy0,1),1) tmptmp(:,[15 16 17])];
            rlt0 = ols1(yyy0,xxx0);
           if ggg == 1
              ff3alpha_g1_lag = rlt0.beta(1) + rlt0.resid;
           elseif ggg == 5
              ff3alpha_g5_lag = rlt0.beta(1) + rlt0.resid;
           end
        
           A_Table8(ggg*2 - 1,Tcolumn + 1) = rlt0.beta(1);
           A_Table8(ggg*2,Tcolumn + 1)     = rlt0.tstat(1);
           clear tmptmp yyy0 xxx0 rlt0
        end
end
A_Table8(:,5)= A_Table8(:,5) * 100;

% Calculate spread in Alpha of Portfolio 1 and 5
% Test whether the spread in Aplha is significant
% VWAlpha (t)
yyy0 = ff3alpha_g5 - ff3alpha_g1;
rlt0 = nwest(yyy0,ones(size(yyy0,1),1),0);
A_Table8(11,8) = rlt0.beta;
A_Table8(12,8) = rlt0.tstat;
clear rlt0 yyy0
% VWAlpha (t-1)
yyy0 = ff3alpha_g5_lag - ff3alpha_g1_lag;
rlt0 = nwest(yyy0,ones(size(yyy0,1),1),0);
A_Table8(11,11)=rlt0.beta;
A_Table8(12,11)=rlt0.tstat;
clear rlt0 yyy0

for ggg = 1:5
    tmp = A_out_Table8( A_out_Table8(:,2) == ggg,:);
    A_Table9(ggg*2 - 1,1) = ggg;
 
    %{
A_out_Table8  
No.VarName      Description
1  DATE         YearMonth (yyyymm)
2  GroupNo.     Group Number (Sorted by RET(t-1))
3  NumStock     Number of Stocks within a portfolio
4  EWRET(t-1)   Equal-wegihted Portfolio Return in month (t-1)
5  VWRET(t)     Value-weighted Portfolio Return in month (t)
6  EWRET(t)     Equal-wegihted Portfolio Return in month (t)
7  SIZE(t-1)    Market Capitalization at the end of last month
8  IVOL(t)      Mean IVOL(t)
    %}
    Tcolumn = 2;
    % Number of Stock-Month Observations 
    for ccc = [3]
        tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
        A_Table9(ggg*2 - 1,Tcolumn) = sum(tmptmp(:,ccc));
        clear tmptmp
    end
    Tcolumn = Tcolumn + 1;
    % Mean EWRET(t-1)
    for ccc = [4]
        tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
        A_Table9(ggg*2 - 1,Tcolumn) = mean(tmptmp(:,ccc));
        clear tmptmp
    end
    Tcolumn = Tcolumn + 1;
    % Mean EWRET(t) and EWXRET(t)
    for ccc = [6]
        tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:); 
        A_Table9(ggg*2 - 1,Tcolumn)     = mean(tmptmp(:,ccc));
        A_Table9(ggg*2 - 1,Tcolumn + 1) = ...
        mean(tmptmp(:,ccc) - tmptmp(:,12));
        clear tmptmp
    end
    Tcolumn = Tcolumn + 2; 
    % Mean VWXRET(t)
    for ccc = [5]
        tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
        A_Table9(ggg*2 - 1,Tcolumn) = mean(tmptmp(:,ccc) - tmptmp(:,12));
        clear tmptmp
    end
    Tcolumn = Tcolumn + 1;
    % Mean IVOL(t) and SIZE(t-1)
    for ccc = [8 7]
        tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
        A_Table9(ggg*2 - 1,Tcolumn) = mean(tmptmp(:,ccc));
        Tcolumn = Tcolumn + 1;
        clear tmptmp
    end
    % Calculate EW Alpha relative to Fama & French
    % Three Factor Model
    for ccc = [6]
        tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
        yyy0 = tmptmp(:,ccc) - tmptmp(:,12);
        xxx0 = [ones(size(yyy0,1),1) tmptmp(:,[9 10 11])];
        rlt0 = ols1(yyy0,xxx0);
        A_Table9(ggg*2 - 1,Tcolumn) = rlt0.beta(1);
        A_Table9(ggg*2,Tcolumn)     = rlt0.tstat(1);
        clear tmptmp yyy0 xxx0 rlt0
    end
    Tcolumn = Tcolumn + 1;   
    % Calculate VW Alpha relative to Fama & French
    % Three Factor Model
   for ccc = [5]
        tmptmp = tmp(tmp(:,ccc) ~= -9.9999,:);
        yyy0 = tmptmp(:,ccc) - tmptmp(:,12);
        xxx0 = [ones(size(yyy0,1),1) tmptmp(:,[9 10 11])];
        rlt0 = ols1(yyy0,xxx0);
        
        A_Table9(ggg*2 - 1,Tcolumn) = rlt0.beta(1);
        A_Table9(ggg*2,Tcolumn)     = rlt0.tstat(1);
        clear tmptmp yyy0 xxx0 rlt0
    end
end
%}
% ------------------------------------------------------------------------%
% Table 9 Robustness Test 1: Different Methods to Calculate IVOL
% ------------------------------- Table 9 --------------------------------%
%{

clear
clc

% Minimum number of stocks within a month to run a regression
cut0 = 20;
SamplePeriod = 1;
IndSave      = 0;

data = [];
for num0 = 1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0

monthall = unique(data(:,2));


A_out_Table_reg3   = [];
A_out_Table_reg3_2 = [];
A_out_Table_reg4   = [];
A_out_Table_reg4_2 = [];
A_out_Table_reg5   = [];
A_out_Table_reg5_2 = [];

% Portfolio Beta
betac   = 15;
% SIZE
sizec   = 7;
% BM
bmc     = 8;
% RET(t)
retc    = 3;
% RET(t-2,t-7)
ret27c  = 18;
% TURN
turnc   = 19;
% CVTURN
cvturnc = 20;

% IVOL(t-1) daily data
ivolc1all_daily   = 52:3:58;
% IVOL(t) daily data
ivolc2all_daily   = 43:3:49;
% IVOL(t-1) monthly data
ivolc1all_monthly = 68:2:74;
% IVOL(t) monthly data
ivolc2all_monthly = 60:2:66;


for j = 1:size(monthall,1)
    j
    tmpout_reg3 = [];tmpout_reg3_2 = [];
    tmpout_reg4 = [];tmpout_reg4_2 = [];
    tmpout_reg5 = [];tmpout_reg5_2 = [];
    
    tmpdata = data(data(:,2) == monthall(j),:);
    
    for mmm = 1:7
        if mmm == 1
           ivolc1 = ivolc1all_daily(1);
           ivolc2 = ivolc2all_daily(1);
           nummonth0 = 3; c0 = 2;
        elseif mmm == 2
           ivolc1 = ivolc1all_daily(2);
           ivolc2 = ivolc2all_daily(2);
           nummonth0 = 6; c0 = 2;
        elseif mmm == 3
           ivolc1 = ivolc1all_daily(3);
           ivolc2 = ivolc2all_daily(3);
           nummonth0 = 12;c0 = 2;
        elseif mmm == 4
           ivolc1 = ivolc1all_monthly(1);
           ivolc2 = ivolc2all_monthly(1);
           nummonth0 = 12; c0 = 1;
        elseif mmm == 5
           ivolc1 = ivolc1all_monthly(2);
           ivolc2 = ivolc2all_monthly(2);
           nummonth0 = 12; c0 = 1;
        elseif mmm == 6
           ivolc1 = ivolc1all_monthly(3);
           ivolc2 = ivolc2all_monthly(3);
           nummonth0 = 12; c0 = 1;
        elseif mmm == 7
           ivolc1 = ivolc1all_monthly(4);
           ivolc2 = ivolc2all_monthly(4);
           nummonth0 = 12; c0 = 1;
        end
                   
    % Regression 3: Y = RET(t) X = IVOL(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 | ...
             tmpdata1(:,ivolc1) == -9.9999 | ...
             tmpdata1(:,ivolc1 - c0) < nummonth0,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,ivolc1),0.5);
       UB1 = prctile(tmpdata1(:,ivolc1),99.5);
       tmpdata1(tmpdata1(:,ivolc1) <= LB1,ivolc1) = LB1;
       tmpdata1(tmpdata1(:,ivolc1) >= UB1,ivolc1) = UB1;
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,ivolc1) * 100];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg3 = [tmpout_reg3;monthall(j) rlt0.beta' rlt0.rsqr ivolc1];
    end
    clear yyy0 xxx0 LB1 UB1 tmpdata1 rlt0
    % Regression 3(2): Y = RET(t) X = IVOL(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 | ...
             tmpdata1(:,ivolc2) == -9.9999 | ...
             tmpdata1(:,ivolc2 - c0) < nummonth0,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,ivolc2),1);
       UB1 = prctile(tmpdata1(:,ivolc2),99.5);
       tmpdata1(tmpdata1(:,ivolc2) <= LB1,ivolc2) = LB1;
       tmpdata1(tmpdata1(:,ivolc2) >= UB1,ivolc2) = UB1;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,ivolc2) * 100];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg3_2 = [tmpout_reg3_2;monthall(j)...
                        rlt0.beta' rlt0.rsqr ivolc2];
    end
    clear yyy0 xxx0 LB1 UB1 tmpdata1 rlt0
    % Regression 4: Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) IVOL(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 | ...
             tmpdata1(:,ivolc1) == -9.9999 | ...
             tmpdata1(:,ivolc1 - c0) < nummonth0 | ...
             tmpdata1(:,sizec) == -9.9999 | ...
             tmpdata1(:,bmc) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ivolc1),0.5);
       UB3 = prctile(tmpdata1(:,ivolc1),99.5);
       tmpdata1(tmpdata1(:,sizec)  <= LB1,sizec)  = LB1;
       tmpdata1(tmpdata1(:,sizec)  >= UB1,sizec)  = UB1;
       tmpdata1(tmpdata1(:,bmc)    <= LB2,bmc)    = LB2;
       tmpdata1(tmpdata1(:,bmc)    >= UB2,bmc)    = UB2;
       tmpdata1(tmpdata1(:,ivolc1) <= LB3,ivolc1) = LB3;
       tmpdata1(tmpdata1(:,ivolc1) >= UB3,ivolc1) = UB3;
       tmpdata1(:,sizec)  = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)    = log(tmpdata1(:,bmc));
       tmpdata1(:,ivolc1) = tmpdata1(:,ivolc1) * 100;
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,[sizec bmc ivolc1])];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg4 = [tmpout_reg4;monthall(j) rlt0.beta' rlt0.rsqr ivolc1];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 tmpdata1 rlt0
    % Regression 4(2): Y = RET(t) X = LN(ME)(t-1) LN(B/M)(t-1) IVOL(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 | ...
             tmpdata1(:,ivolc2) == -9.9999 | ...
             tmpdata1(:,ivolc2 - c0) < nummonth0 | ...
             tmpdata1(:,sizec) == -9.9999 | ...
             tmpdata1(:,bmc) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ivolc2),0.5);
       UB3 = prctile(tmpdata1(:,ivolc2),99.5);
       
       tmpdata1(tmpdata1(:,sizec)  <= LB1,sizec)  = LB1;
       tmpdata1(tmpdata1(:,sizec)  >= UB1,sizec)  = UB1;
       tmpdata1(tmpdata1(:,bmc)    <= LB2,bmc)    = LB2;
       tmpdata1(tmpdata1(:,bmc)    >= UB2,bmc)    = UB2;
       tmpdata1(tmpdata1(:,ivolc2) <= LB3,ivolc2) = LB3;
       tmpdata1(tmpdata1(:,ivolc2) >= UB3,ivolc2) = UB3;
       
       tmpdata1(:,sizec)  = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)    = log(tmpdata1(:,bmc));
       tmpdata1(:,ivolc2) = tmpdata1(:,ivolc2) * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,[sizec bmc ivolc2])];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg4_2 = [tmpout_reg4_2;monthall(j)...
                        rlt0.beta' rlt0.rsqr ivolc2];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 tmpdata1 rlt0 rlt1
    
    % Regression 5: Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) RET(-2,-7)
    %                   Ln(TURN) Ln(CVTURN) IVOL(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 | ...
            tmpdata1(:,sizec) == -9.9999 | ...
            tmpdata1(:,bmc) == -9.9999 | ...
            tmpdata1(:,ret27c) == -9.9999 | ... 
            tmpdata1(:,turnc) == -9.9999 |...
            tmpdata1(:,cvturnc) == -9.9999 | ...
            tmpdata1(:,ivolc1) == -9.9999 |...
            tmpdata1(:,ivolc1 - c0) < nummonth0,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ret27c),0.5);
       UB3 = prctile(tmpdata1(:,ret27c),99.5);
       LB4 = prctile(tmpdata1(:,turnc),0.5);
       UB4 = prctile(tmpdata1(:,turnc),99.5);
       LB5 = prctile(tmpdata1(:,cvturnc),0.5);
       UB5 = prctile(tmpdata1(:,cvturnc),99.5);
       LB6 = prctile(tmpdata1(:,ivolc1),0.5);
       UB6 = prctile(tmpdata1(:,ivolc1),99.5);
       
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,ret27c)  <= LB3,ret27c)  = LB3;
       tmpdata1(tmpdata1(:,ret27c)  >= UB3,ret27c)  = UB3;
       tmpdata1(tmpdata1(:,turnc)   <= LB4,turnc)   = LB4;
       tmpdata1(tmpdata1(:,turnc)   >= UB4,turnc)   = UB4;
       tmpdata1(tmpdata1(:,cvturnc) <= LB5,cvturnc) = LB5;
       tmpdata1(tmpdata1(:,cvturnc) >= UB5,cvturnc) = UB5;
       tmpdata1(tmpdata1(:,ivolc1)  <= LB6,ivolc1)  = LB6;
       tmpdata1(tmpdata1(:,ivolc1)  >= UB6,ivolc1)  = UB6;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,ret27c)  = tmpdata1(:,ret27c) + 1;
       tmpdata1(:,turnc)   = log(tmpdata1(:,turnc) * 100);
       tmpdata1(:,cvturnc) = log(tmpdata1(:,cvturnc) * 100);
       tmpdata1(:,ivolc1)   = tmpdata1(:,ivolc1) * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1)...
               tmpdata1(:,[sizec bmc ret27c turnc cvturnc ivolc1])];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg5 = [tmpout_reg5;monthall(j) rlt0.beta' rlt0.rsqr ivolc1];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 LB4 UB4 LB5 UB5...
          LB6 UB6 tmpdata1 rlt0   
    % Regression 5(2): Y = RET(t) X = LN(ME)(t-1) LN(B/M)(t-1) RET(-2,-7)
    %                             Ln(TURN) Ln(CVTURN) IVOL(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 | ...
             tmpdata1(:,sizec) == -9.9999 | ...
             tmpdata1(:,bmc) == -9.9999 | ...
             tmpdata1(:,ret27c) == -9.9999 | ...
             tmpdata1(:,turnc) == -9.9999 | ...
             tmpdata1(:,cvturnc) == -9.9999 | ...
             tmpdata1(:,ivolc2) == -9.9999 | ...
             tmpdata1(:,ivolc2 - c0) < nummonth0,:)=[];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ret27c),0.5);
       UB3 = prctile(tmpdata1(:,ret27c),99.5);
       LB4 = prctile(tmpdata1(:,turnc),0.5);
       UB4 = prctile(tmpdata1(:,turnc),99.5);
       LB5 = prctile(tmpdata1(:,cvturnc),0.5);
       UB5 = prctile(tmpdata1(:,cvturnc),99.5);
       LB6 = prctile(tmpdata1(:,ivolc2),0.5);
       UB6 = prctile(tmpdata1(:,ivolc2),99.5);
       
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,ret27c)  <= LB3,ret27c)  = LB3;
       tmpdata1(tmpdata1(:,ret27c)  >= UB3,ret27c)  = UB3;
       tmpdata1(tmpdata1(:,turnc)   <= LB4,turnc)   = LB4;
       tmpdata1(tmpdata1(:,turnc)   >= UB4,turnc)   = UB4;
       tmpdata1(tmpdata1(:,cvturnc) <= LB5,cvturnc) = LB5;
       tmpdata1(tmpdata1(:,cvturnc) >= UB5,cvturnc) = UB5;
       tmpdata1(tmpdata1(:,ivolc2)  <= LB6,ivolc2)  = LB6;
       tmpdata1(tmpdata1(:,ivolc2)  >= UB6,ivolc2)  = UB6;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,ret27c)  = tmpdata1(:,ret27c) + 1;
       tmpdata1(:,turnc)   = log(tmpdata1(:,turnc) * 100);
       tmpdata1(:,cvturnc) = log(tmpdata1(:,cvturnc) * 100);
       tmpdata1(:,ivolc2)  = tmpdata1(:,ivolc2) * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1)...
               tmpdata1(:,[sizec bmc ret27c turnc cvturnc ivolc2])];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg5_2 = [tmpout_reg5_2;monthall(j)...
                        rlt0.beta' rlt0.rsqr ivolc2 ];
    end
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 LB4 UB4 LB5 UB5...
          LB6 UB6 tmpdata1 rlt0  
    A_out_Table_reg3   = [A_out_Table_reg3;tmpout_reg3];
    A_out_Table_reg3_2 = [A_out_Table_reg3_2;tmpout_reg3_2];
    A_out_Table_reg4   = [A_out_Table_reg4;tmpout_reg4];
    A_out_Table_reg4_2 = [A_out_Table_reg4_2;tmpout_reg4_2];
    A_out_Table_reg5   = [A_out_Table_reg5;tmpout_reg5];
    A_out_Table_reg5_2 = [A_out_Table_reg5_2;tmpout_reg5_2];
    
    clear tmpdata tmpout_reg1 tmpout_reg2 tmpout_reg3 tmpout_reg3_2...
          tmpout_reg4 mpout_reg4_2 tmpout_reg5 tmpout_reg5_2...
          tmpout_reg6 tmpout_reg7
end
if SamplePeriod == 1
   save Results_Tableother_allstocks_1963_2006.mat...
        A_out_Table_reg3 A_out_Table_reg3_2  A_out_Table_reg4...
        A_out_Table_reg4_2 A_out_Table_reg5 A_out_Table_reg5_2
elseif SamplePeriod == 2
   save Results_Tableother_allstocks_2007_2017.mat...
        A_out_Table_reg3 A_out_Table_reg3_2  A_out_Table_reg4...
        A_out_Table_reg4_2 A_out_Table_reg5 A_out_Table_reg5_2
elseif SamplePeriod == 3
   save Results_Tableother_allstocks_1963_2017.mat...
        A_out_Table_reg3 A_out_Table_reg3_2  A_out_Table_reg4...
        A_out_Table_reg4_2 A_out_Table_reg5 A_out_Table_reg5_2
elseif SamplePeriod == 4
   save Results_Tableother_allstocks_1926_1963.mat...
        A_out_Table_reg3 A_out_Table_reg3_2  A_out_Table_reg4...
        A_out_Table_reg4_2 A_out_Table_reg5 A_out_Table_reg5_2
end
%}
%{
clear
clc

SamplePeriod = 1;


if SamplePeriod == 1
   load Results_Tableother_allstocks_1963_2006.mat...
        A_out_Table_reg3 A_out_Table_reg3_2  A_out_Table_reg4...
        A_out_Table_reg4_2 A_out_Table_reg5 A_out_Table_reg5_2
elseif SamplePeriod == 2
   load Results_Tableother_allstocks_2007_2017.mat...
        A_out_Table_reg3 A_out_Table_reg3_2  A_out_Table_reg4...
        A_out_Table_reg4_2 A_out_Table_reg5 A_out_Table_reg5_2
elseif SamplePeriod == 3
   load Results_Tableother_allstocks_1963_2017.mat...
        A_out_Table_reg3 A_out_Table_reg3_2  A_out_Table_reg4...
        A_out_Table_reg4_2 A_out_Table_reg5 A_out_Table_reg5_2
elseif SamplePeriod == 4
   load Results_Tableother_allstocks_1926_1963.mat...
        A_out_Table_reg3 A_out_Table_reg3_2  A_out_Table_reg4...
        A_out_Table_reg4_2 A_out_Table_reg5 A_out_Table_reg5_2
end

A_out_Table_daily = zeros(6,10);

% IVOL(t-1) daily data
ivolc1all_daily   = 52:3:58;
% IVOL(t) daily data
ivolc2all_daily   = 43:3:49;
% IVOL(t-1) monthly data
ivolc1all_monthly = 68:2:74;
% IVOL(t) monthly data
ivolc2all_monthly = 60:2:66;


for dailyivolc = 1:length(ivolc1all_daily)
        ivolc1 = ivolc1all_daily(dailyivolc);
        ivolc2 = ivolc2all_daily(dailyivolc);
    for reg = 1:6
    if reg == 1
       regdata = A_out_Table_reg3_2(A_out_Table_reg3_2(:,end) == ivolc2,:);
       Trow    = [12*dailyivolc - 11;12*dailyivolc - 10];
       Tcolumn = [8 10];
    elseif reg == 2
       regdata = A_out_Table_reg3(A_out_Table_reg3(:,end) == ivolc1,:);
       Trow    = [12*dailyivolc - 9;12*dailyivolc - 8];
       Tcolumn = [9 10];
    elseif reg == 3
       regdata = A_out_Table_reg4_2(A_out_Table_reg4_2(:,end) == ivolc2,:);
       Trow    = [12*dailyivolc - 7;12*dailyivolc - 6];
       Tcolumn = [2 3 8 10];
    elseif reg == 4
       regdata = A_out_Table_reg4(A_out_Table_reg4(:,end) == ivolc1,:);
       Trow    = [12*dailyivolc - 5;12*dailyivolc - 4];
       Tcolumn = [2 3 9 10];
    elseif reg == 5
       regdata = A_out_Table_reg5_2(A_out_Table_reg5_2(:,end) == ivolc2,:);
       Trow    = [12*dailyivolc - 3;12*dailyivolc - 2];
       Tcolumn = [2 3 4 5 6 8 10];
    elseif reg == 6
       regdata = A_out_Table_reg5(A_out_Table_reg5(:,end) == ivolc1,:);
       Trow    = [12*dailyivolc - 1;12*dailyivolc - 0];
       Tcolumn = [2 3 4 5 6 9 10];
    end
    for ccc = 3:(size(regdata,2)-2)
        tmp  = regdata(:,ccc);
        rlt0 = nwest(tmp,ones(size(tmp,1),1),0);
        
        A_out_Table_daily(Trow(1),Tcolumn(ccc - 2)) = rlt0.beta;
        A_out_Table_daily(Trow(2),Tcolumn(ccc - 2)) = rlt0.tstat;
        clear tmp rlt0
    end
    for ccc = size(regdata,2) - 1
        tmp = regdata(:,ccc);
        A_out_Table_daily(Trow(1),Tcolumn(end))   = mean(tmp) * 100;
        clear tmp
    end
    clear regdata Trow Tcolumn 
    end
end

A_out_Table_monthly = zeros(6,10);

for monthlyivolc = 1:length(ivolc1all_monthly)
        ivolc1 = ivolc1all_monthly(monthlyivolc);
        ivolc2 = ivolc2all_monthly(monthlyivolc);
    for reg = 1:6
    if reg == 1
       regdata = A_out_Table_reg3_2(A_out_Table_reg3_2(:,end) == ivolc2,:);
       Trow    = [12*monthlyivolc - 11;12*monthlyivolc - 10];
       Tcolumn = [8 10];
    elseif reg == 2
       regdata = A_out_Table_reg3(A_out_Table_reg3(:,end) == ivolc1,:);
       Trow    = [12*monthlyivolc - 9;12*monthlyivolc - 8];
       Tcolumn = [9 10];
    elseif reg == 3
       regdata =A_out_Table_reg4_2(A_out_Table_reg4_2(:,end) == ivolc2,:);
       Trow    = [12*monthlyivolc - 7;12*monthlyivolc - 6];
       Tcolumn = [2 3 8 10];
    elseif reg == 4
       regdata = A_out_Table_reg4(A_out_Table_reg4(:,end) == ivolc1,:);
       Trow    = [12*monthlyivolc - 5;12*monthlyivolc - 4];
       Tcolumn = [2 3 9 10];
    elseif reg == 5
       regdata = A_out_Table_reg5_2(A_out_Table_reg5_2(:,end) == ivolc2,:);
       Trow    = [12*monthlyivolc - 3;12*monthlyivolc - 2];
       Tcolumn = [2 3 4 5 6 8 10];
    elseif reg == 6
       regdata = A_out_Table_reg5(A_out_Table_reg5(:,end) == ivolc1,:);
       Trow    = [12*monthlyivolc - 1;12*monthlyivolc - 0];
       Tcolumn = [2 3 4 5 6 9 10];
    end
    for ccc = 3:(size(regdata,2) - 2)
        tmp  = regdata(:,ccc);
        rlt0 = nwest(tmp,ones(size(tmp,1),1),0);
        
        A_out_Table_monthly(Trow(1),Tcolumn(ccc - 2)) = rlt0.beta;
        A_out_Table_monthly(Trow(2),Tcolumn(ccc - 2)) = rlt0.tstat;
        clear tmp rlt0
    end
    for ccc = size(regdata,2) - 1
        tmp = regdata(:,ccc);
        A_out_Table_monthly(Trow(1),Tcolumn(end))   = mean(tmp) * 100;
        clear tmp
    end
    clear regdata Trow Tcolumn 
    end
end


%}

% Table 10 Robustness Test 2: EIVOL estimated using rolling window data
% ------------------------------- Table 10 -------------------------------%
% Panel A Summary statistics of EIVOL estimated in a rolling window
%{

clear
clc

SamplePeriod = 1;
% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963


data = [];
for num0=1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0


A_Table10_A = zeros(2,7);

monthall    = unique(data(:,2));
tmpout      = [];

for j = 1:size(monthall,1)
    
    tmpdata = data(data(:,2) == monthall(j),:);
    
    for ccc = [76 88]
        
        % Eliminate Missing Value
        tmptmp = tmpdata(tmpdata(:,ccc) ~= -9.9999,:);
        
        
        if isempty(tmptmp) == 0
           
           % The smallest and largest 0.5\% of the observations in each 
           % month for E(IVOL) are set equal to the next smallest or 
           % largest values
           
           LB = prctile(tmptmp(:,ccc),0.5);
           UB = prctile(tmptmp(:,ccc),99.5);
           
           data(data(:,2) == monthall(j) & data(:,ccc) ~= -9.9999 & ...
                data(:,ccc) <= LB,ccc) = LB;
           data(data(:,2) == monthall(j) & data(:,ccc) ~= -9.9999 & ...
                data(:,ccc) >= UB,ccc) = UB;
           clear LB UB 
        end
        clear tmptmp
    end
    clear tmpdata
    
end

for ccc = [76 88]
    
    data1 = data(data(:,ccc) ~= -9.9999,:);
    a     = data1(:,ccc);
    

    % EIVOL (t) estimated using information from (t-60) to (t-1)
    if ccc == 76
       ccc1 = 1;
       a    = a.^0.5;
       a    = a * 100; 
    % EIVOL (t) estimated using information from (t-59) to (t) 
    elseif ccc == 88
       ccc1 = 2;
       a    = a.^0.5;
       a    = a * 100; 
    end
    
    A_Table10_A(ccc1,:) = [mean(a) std(a) prctile(a,50) prctile(a,25) ...
    prctile(a,75) skewness(a) size(data1,1)];
    
    clear data1 a ccc1    
end
%}
% Panel B Correlation between EIVOL estimated 
% in a rolling window and other variables
%{
clear
clc

SamplePeriod = 1;
% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963

% Minimum Number of Stocks within a Month to calculate correlation
cut0 = 20; 
data = [];
for num0 = 1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0


monthall        = unique(data(:,2));
A_out_Table10_B = [];


for j = 1:size(monthall,1)
    j
    tmpout  = [];
    tmpdata = data(data(:,2) == monthall(j),:);
    % Calculatev log RET
    tmpdata(tmpdata(:,3) ~= -9.9999,57) = ...
    log(tmpdata(tmpdata(:,3) ~= -9.9999,5) + 1);
    tmpdata(tmpdata(:,3) == -9.9999,57) = -9.9999;
    clear tmpdata1 rho0 ccc
    for ccc1 = [76 88]
        for ccc2 = [3 57 13 76 88 15 7 8 18 19 20]
            
            tmpdata1(:,1) = tmpdata(:,ccc1);
            tmpdata1(:,2) = tmpdata(:,ccc2);
            % Eliminate monthly RET >3 or missing value
            if ccc1 == 5 | ccc1 == 57
               tmpdata1(tmpdata1(:,1) > 3 | tmpdata1(:,1) == -9.9999 | ...
               tmpdata1(:,2) == -9.9999,:) = [];
            elseif ccc2 == 5 | ccc2 == 57
               tmpdata1(tmpdata1(:,2) > 3 | tmpdata1(:,1) == -9.9999 | ...
               tmpdata1(:,2) == -9.9999,:) = [];
            else
               tmpdata1(tmpdata1(:,1) == -9.9999 | ...
               tmpdata1(:,2) == -9.9999,:) = [];
            end
            
            if size(tmpdata1,1) > cut0
               if ccc2 == 7
                  tmpdata1(:,2) = log(tmpdata1(:,2) / 1000 + 1);
               elseif ccc2 == 8
                  tmpdata1(:,2) = log(tmpdata1(:,2));
               elseif ccc2 == 18
                  tmpdata1(:,2) = tmpdata1(:,2) + 1;
               elseif ccc2 == 19
                  tmpdata1(:,2) = log(tmpdata1(:,2) * 100);
               elseif ccc2 == 20
                  tmpdata1(:,2) = log(tmpdata1(:,2) * 100);
               elseif ccc2 == 76 | ccc2 == 88
                  tmpdata1(:,2) = tmpdata1(:,2).^0.5;
               end
               if ccc1 == 7
                  tmpdata1(:,1) = log(tmpdata1(:,1) / 1000 + 1);
               elseif ccc1 == 8
                  tmpdata1(:,1) = log(tmpdata1(:,1));
               elseif ccc1 == 18
                  tmpdata1(:,1) = tmpdata1(:,1) + 1;
               elseif ccc1 == 19
                  tmpdata1(:,1) = log(tmpdata1(:,1) * 100);
               elseif ccc1 == 20
                  tmpdata1(:,1) = log(tmpdata1(:,1) * 100);
               elseif ccc1 == 76 | ccc1 == 88
                  tmpdata1(:,1) = tmpdata1(:,1).^0.5;
               end
               
               rho0   = corrcoef(tmpdata1(:,1),tmpdata1(:,2));
               tmpout = [tmpout;monthall(j) ccc1 ccc2 rho0(1,2)];
                              
            end
            clear tmpdata1 rho0 rho0_trunc tmpdata1_trunc LB1 UB1 LB2 UB2
        end
    end
    
    
    A_out_Table10_B = [A_out_Table10_B;tmpout];   
    clear tmpdata tmpout
end

% Save data
if SamplePeriod == 1
save Results_Table10_PanelB_allstocks_1963_2006.mat A_out_Table10_B
elseif SamplePeriod == 2
save Results_Table10_PanelB_allstocks_2007_2017.mat A_out_Table10_B
elseif SamplePeriod == 3
save Results_Table10_PanelB_allstocks_1963_2017.mat A_out_Table10_B
elseif SamplePeriod == 4
save Results_Table10_PanelB_allstocks_1926_1963.mat A_out_Table10_B
end

A_out_correplation       = zeros(1,10);
A_out_correplation_tstat = zeros(1,10);
if SamplePeriod == 1
load Results_Table10_PanelB_allstocks_1963_2006.mat A_out_Table10_B
elseif SamplePeriod == 2
load Results_Table10_PanelB_allstocks_2007_2017.mat A_out_Table10_B
elseif SamplePeriod == 3
load Results_Table10_PanelB_allstocks_1963_2017.mat A_out_Table10_B
elseif SamplePeriod == 4
load Results_Table10_PanelB_allstocks_1926_1963.mat A_out_Table10_B
end

IndEIVOLSample = 1;
% ( 1 = Out of Sample; 0 = In Sample)
if IndEIVOLSample == 1
   c_EIVOL = 76;
elseif IndEIVOLSample == 0
   c_EIVOL = 88;
end
    Trow = 1; 
    for ccc1 = [c_EIVOL]
        Tcolumn = 1;
        for ccc2 = [3 57 13 15 7 8 18 19 20]
           
            tmp  = A_out_Table10_B(A_out_Table10_B(:,2) == ccc1 & ...
                   A_out_Table10_B(:,3) == ccc2,:);
            rlt0 = nwest(tmp(:,4),ones(size(tmp,1),1),0);
            if Trow < Tcolumn
               A_out_correplation(Trow,Tcolumn)       = rlt0.beta;
               A_out_correplation_tstat(Trow,Tcolumn) = rlt0.tstat;
            end
            A_out_correplation(Trow,Tcolumn)       = rlt0.beta;
            A_out_correplation_tstat(Trow,Tcolumn) = rlt0.tstat;
            clear rlt0
            
            Tcolumn = Tcolumn + 1;
        end
        Trow = Trow + 1;
    end
%}
% Panel C Month-to-month cross sectional regressions
%{
clear
clc

SamplePeriod = 1;
% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963

% Minimum Number of Stocks within a Month to run a regression
cut0 = 20; 

data = [];
for num0 = 1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0

monthall = unique(data(:,2));


A_out_Table10_PanelC_reg3   = [];
A_out_Table10_PanelC_reg3_2 = [];
A_out_Table10_PanelC_reg4   = [];
A_out_Table10_PanelC_reg4_2 = [];
A_out_Table10_PanelC_reg5   = [];
A_out_Table10_PanelC_reg5_2 = [];

% Portfolio Beta
betac   = 15;
% SIZE
sizec   = 7;
% BM
bmc     = 8;
% RET(t)
retc    = 3;
% RET(t-2,t-7)
ret27c  = 18;
% TURN
turnc   = 19;
% CVTURN
cvturnc = 20;
% EIVOL(t)|(t-1) EIVOL estimated using information from (t-60) to (t-1)
eivolc1 = 76;
% EIVOL(t)|(t) EIVOL  estimated using information from (t-59? to (t)
eivolc2 = 88;
% IVOL(t-1)
ivolc1  = 14;
% IVOL(t)
ivolc2  = 13;

for j = 1:size(monthall,1)
 
    tmpout_reg3   = [];
    tmpout_reg3_2 = [];
    tmpout_reg4   = [];
    tmpout_reg4_2 = [];
    tmpout_reg5   = [];
    tmpout_reg5_2 = [];
    
    tmpdata = data(data(:,2) == monthall(j),:);
    
    % For all regressions
    % Observations with monthly return > 3 are eliminated
    % The smallest and largest 0.5% of the explanatory variables
    % (except BETA) are set equal to the next smallest and largest values
    
    % Regression 3: Y=RET(t) X=EIVOL(t)|(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
             tmpdata1(:,eivolc1) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,eivolc1),0.5);
       UB1 = prctile(tmpdata1(:,eivolc1),99.5);
       tmpdata1(tmpdata1(:,eivolc1) <= LB1,eivolc1) = LB1;
       tmpdata1(tmpdata1(:,eivolc1) >= UB1,eivolc1) = UB1;
       
       yyy0 = tmpdata1(:,retc)*100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,eivolc1).^0.5 * 100];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg3 = [tmpout_reg3;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 tmpdata1 rlt0
    
    % Regression 3(2): Y=RET(t) X=EIVOL(t)|(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,eivolc2) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,eivolc2),0,5);
       UB1 = prctile(tmpdata1(:,eivolc2),99.5);
       tmpdata1(tmpdata1(:,eivolc2) <= LB1,eivolc2) = LB1;
       tmpdata1(tmpdata1(:,eivolc2) >= UB1,eivolc2) = UB1;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,eivolc2).^0.5 * 100];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg3_2 = [tmpout_reg3_2;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 tmpdata1 rlt0
    
    % Regression 4: Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) EIVOL(t)|(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,eivolc1) == -9.9999 | tmpdata1(:,sizec) == -9.9999 |...
    tmpdata1(:,bmc) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,eivolc1),0.5);
       UB3 = prctile(tmpdata1(:,eivolc1),99.5);
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,eivolc1) <= LB3,eivolc1) = LB3;
       tmpdata1(tmpdata1(:,eivolc1) >= UB3,eivolc1) = UB3;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,eivolc1) = tmpdata1(:,eivolc1).^0.5 * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,[sizec bmc eivolc1])];
       rlt0 = ols1(yyy0,xxx0);
       
       tmpout_reg4 = [tmpout_reg4;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 tmpdata1 rlt0
    % Regression 4(2): Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) EIVOL(t)|(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc)>3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,eivolc2) == -9.9999 | tmpdata1(:,sizec) == -9.9999 |...
    tmpdata1(:,bmc) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,eivolc2),0.5);
       UB3 = prctile(tmpdata1(:,eivolc2),99.5);
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,eivolc2) <= LB3,eivolc2) = LB3;
       tmpdata1(tmpdata1(:,eivolc2) >= UB3,eivolc2) = UB3;
       tmpdata1(:,sizec)  = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)    = log(tmpdata1(:,bmc));
       tmpdata1(:,eivolc2)= tmpdata1(:,eivolc2).^0.5 * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1) tmpdata1(:,[sizec bmc eivolc2])];
       rlt0 = ols1(yyy0,xxx0);       
       tmpout_reg4_2=[tmpout_reg4_2;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 tmpdata1 rlt0 rlt1 tmpdata2
    
    % Regression 5: Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) RET(-2,-7)
    %                             Ln(TURN) Ln(CVTURN) EIVOL(t)|(t-1)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,sizec) == -9.9999 | tmpdata1(:,bmc) == -9.9999|...
    tmpdata1(:,ret27c) == -9.9999 | tmpdata1(:,turnc) == -9.9999 |...
    tmpdata1(:,cvturnc) == -9.9999 |...
    tmpdata1(:,eivolc1) == -9.9999,:) = [];

    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ret27c),0.5);
       UB3 = prctile(tmpdata1(:,ret27c),99.5);
       LB4 = prctile(tmpdata1(:,turnc),0.5);
       UB4 = prctile(tmpdata1(:,turnc),99.5);
       LB5 = prctile(tmpdata1(:,cvturnc),0.5);
       UB5 = prctile(tmpdata1(:,cvturnc),99.5);
       LB6 = prctile(tmpdata1(:,eivolc1),0.5);
       UB6 = prctile(tmpdata1(:,eivolc1),99.5);
       
       tmpdata1(tmpdata1(:,sizec)   <=LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >=UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <=LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >=UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,ret27c)  <=LB3,ret27c)  = LB3;
       tmpdata1(tmpdata1(:,ret27c)  >=UB3,ret27c)  = UB3;
       tmpdata1(tmpdata1(:,turnc)   <=LB4,turnc)   = LB4;
       tmpdata1(tmpdata1(:,turnc)   >=UB4,turnc)   = UB4;
       tmpdata1(tmpdata1(:,cvturnc) <=LB5,cvturnc) = LB5;
       tmpdata1(tmpdata1(:,cvturnc) >=UB5,cvturnc) = UB5;
       tmpdata1(tmpdata1(:,eivolc1) <=LB6,eivolc1) = LB6;
       tmpdata1(tmpdata1(:,eivolc1) >=UB6,eivolc1) = UB6;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,ret27c)  = tmpdata1(:,ret27c) + 1;
       tmpdata1(:,turnc)   = log(tmpdata1(:,turnc) * 100);
       tmpdata1(:,cvturnc) = log(tmpdata1(:,cvturnc) * 100);
       tmpdata1(:,eivolc1) = tmpdata1(:,eivolc1).^0.5 * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1)...
               tmpdata1(:,[sizec bmc ret27c turnc cvturnc eivolc1])];
       rlt0 = ols1(yyy0,xxx0);
       
       tmpout_reg5 = [tmpout_reg5;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 LB4 UB4 LB5 UB5...
          LB6 UB6 tmpdata1 rlt0   
    % regression 5(2): Y = RET(t) X= LN(ME)(t-1) LN(B/M)(t-1) RET(-2,-7)
    %                                Ln(TURN) Ln(CVTURN) EIVOL(t)|(t)
    tmpdata1 = tmpdata;
    tmpdata1(tmpdata1(:,retc) > 3 | tmpdata1(:,retc) == -9.9999 |...
    tmpdata1(:,sizec) == -9.9999 | tmpdata1(:,bmc) == -9.9999 |...
    tmpdata1(:,ret27c) == -9.9999 | tmpdata1(:,turnc) == -9.9999 |...
    tmpdata1(:,cvturnc) == -9.9999 |...
    tmpdata1(:,eivolc2) == -9.9999,:) = [];
    if size(tmpdata1,1) > cut0
       LB1 = prctile(tmpdata1(:,sizec),0.5);
       UB1 = prctile(tmpdata1(:,sizec),99.5);
       LB2 = prctile(tmpdata1(:,bmc),0.5);
       UB2 = prctile(tmpdata1(:,bmc),99.5);
       LB3 = prctile(tmpdata1(:,ret27c),0.5);
       UB3 = prctile(tmpdata1(:,ret27c),99.5);
       LB4 = prctile(tmpdata1(:,turnc),0.5);
       UB4 = prctile(tmpdata1(:,turnc),99.5);
       LB5 = prctile(tmpdata1(:,cvturnc),0.5);
       UB5 = prctile(tmpdata1(:,cvturnc),99.5);
       LB6 = prctile(tmpdata1(:,eivolc2),0.5);
       UB6 = prctile(tmpdata1(:,eivolc2),99.5);
       
       tmpdata1(tmpdata1(:,sizec)   <= LB1,sizec)   = LB1;
       tmpdata1(tmpdata1(:,sizec)   >= UB1,sizec)   = UB1;
       tmpdata1(tmpdata1(:,bmc)     <= LB2,bmc)     = LB2;
       tmpdata1(tmpdata1(:,bmc)     >= UB2,bmc)     = UB2;
       tmpdata1(tmpdata1(:,ret27c)  <= LB3,ret27c)  = LB3;
       tmpdata1(tmpdata1(:,ret27c)  >= UB3,ret27c)  = UB3;
       tmpdata1(tmpdata1(:,turnc)   <= LB4,turnc)   = LB4;
       tmpdata1(tmpdata1(:,turnc)   >= UB4,turnc)   = UB4;
       tmpdata1(tmpdata1(:,cvturnc) <= LB5,cvturnc) = LB5;
       tmpdata1(tmpdata1(:,cvturnc) >= UB5,cvturnc) = UB5;
       tmpdata1(tmpdata1(:,eivolc2) <= LB6,eivolc2) = LB6;
       tmpdata1(tmpdata1(:,eivolc2) >= UB6,eivolc2) = UB6;
       
       tmpdata1(:,sizec)   = log(tmpdata1(:,sizec) / 1000 + 1);
       tmpdata1(:,bmc)     = log(tmpdata1(:,bmc));
       tmpdata1(:,ret27c)  = tmpdata1(:,ret27c) + 1;
       tmpdata1(:,turnc)   = log(tmpdata1(:,turnc) * 100);
       tmpdata1(:,cvturnc) = log(tmpdata1(:,cvturnc) * 100);
       tmpdata1(:,eivolc2) = tmpdata1(:,eivolc2).^0.5 * 100;
       
       yyy0 = tmpdata1(:,retc) * 100;
       xxx0 = [ones(size(yyy0,1),1)...
               tmpdata1(:,[sizec bmc ret27c turnc cvturnc eivolc2])];
       rlt0 = ols1(yyy0,xxx0);
       tmpout_reg5_2=[tmpout_reg5_2;monthall(j) rlt0.beta' rlt0.rsqr];
    end
    clear yyy0 xxx0 LB1 UB1 LB2 UB2 LB3 UB3 LB4 UB4 LB5 UB5 LB6 UB6 tmpdata1 rlt0    tmpdata2 rlt1
?     
    A_out_Table10_PanelC_reg3   = [A_out_Table10_PanelC_reg3;tmpout_reg3];
    A_out_Table10_PanelC_reg3_2 = [A_out_Table10_PanelC_reg3_2; ...
                                   tmpout_reg3_2];
    A_out_Table10_PanelC_reg4   = [A_out_Table10_PanelC_reg4;tmpout_reg4];
    A_out_Table10_PanelC_reg4_2 = [A_out_Table10_PanelC_reg4_2; ...
                                   tmpout_reg4_2];
    A_out_Table10_PanelC_reg5   = [A_out_Table10_PanelC_reg5;tmpout_reg5];
    A_out_Table10_PanelC_reg5_2 = [A_out_Table10_PanelC_reg5_2;...
                                   tmpout_reg5_2];
    
    clear tmpdata tmpout_reg3 tmpout_reg3_2...
          tmpout_reg4 mpout_reg4_2 tmpout_reg5 tmpout_reg5_2 
end

if SamplePeriod == 1
save Results_Table10_PanelC_allstocks_1963_2006.mat...
     A_out_Table10_PanelC_reg3 A_out_Table10_PanelC_reg3_2...
     A_out_Table10_PanelC_reg4 A_out_Table10_PanelC_reg4_2...
     A_out_Table10_PanelC_reg5 A_out_Table10_PanelC_reg5_2
elseif SamplePeriod == 2
save Results_Table10_PanelC_allstocks_2007_2017.mat...
     A_out_Table10_PanelC_reg3 A_out_Table10_PanelC_reg3_2...
     A_out_Table10_PanelC_reg4 A_out_Table10_PanelC_reg4_2...
     A_out_Table10_PanelC_reg5 A_out_Table10_PanelC_reg5_2
elseif SamplePeriod == 3
save Results_Table10_PanelC_allstocks_1963_2017.mat...
     A_out_Table10_PanelC_reg3 A_out_Table10_PanelC_reg3_2...
     A_out_Table10_PanelC_reg4 A_out_Table10_PanelC_reg4_2...
     A_out_Table10_PanelC_reg5 A_out_Table10_PanelC_reg5_2
elseif SamplePeriod == 4
save Results_Table10_PanelC_allstocks_1926_1963.mat...
     A_out_Table10_PanelC_reg3 A_out_Table10_PanelC_reg3_2...
     A_out_Table10_PanelC_reg4 A_out_Table10_PanelC_reg4_2...
     A_out_Table10_PanelC_reg5 A_out_Table10_PanelC_reg5_2
end

if SamplePeriod == 1
load Results_Table10_PanelC_allstocks_1963_2006.mat...
     A_out_Table10_PanelC_reg3 A_out_Table10_PanelC_reg3_2...
     A_out_Table10_PanelC_reg4 A_out_Table10_PanelC_reg4_2...
     A_out_Table10_PanelC_reg5 A_out_Table10_PanelC_reg5_2
elseif SamplePeriod == 2
load Results_Table10_PanelC_allstocks_2007_2017.mat...
     A_out_Table10_PanelC_reg3 A_out_Table10_PanelC_reg3_2...
     A_out_Table10_PanelC_reg4 A_out_Table10_PanelC_reg4_2...
     A_out_Table10_PanelC_reg5 A_out_Table10_PanelC_reg5_2
elseif SamplePeriod == 3
load Results_Table10_PanelC_allstocks_1963_2017.mat...
     A_out_Table10_PanelC_reg3 A_out_Table10_PanelC_reg3_2...
     A_out_Table10_PanelC_reg4 A_out_Table10_PanelC_reg4_2...
     A_out_Table10_PanelC_reg5 A_out_Table10_PanelC_reg5_2
elseif SamplePeriod == 4
load Results_Table10_PanelC_allstocks_1926_1963.mat...
     A_out_Table10_PanelC_reg3 A_out_Table10_PanelC_reg3_2...
     A_out_Table10_PanelC_reg4 A_out_Table10_PanelC_reg4_2...
     A_out_Table10_PanelC_reg5 A_out_Table10_PanelC_reg5_2
end

indinclude_Rt = 0;
% (0 = EIVOL (t)|(t-1); 1= EIVOL (t)|(t))
A_out_Table10_PanelC  = zeros(6,10);
for reg = 3:5
    if reg==3
       if indinclude_Rt==1
       regdata = A_out_Table5_reg3_2;
       else
       regdata = A_out_Table5_reg3;
       end
       Trow    = [1;2];
       Tcolumn = [7 10];
    elseif reg==4
       if indinclude_Rt==1
       regdata = A_out_Table5_reg4_2;
       else
       regdata = A_out_Table5_reg4;
       end
       Trow    = [3;4];
       Tcolumn = [2 3 7 10];
    elseif reg==5
       if indinclude_Rt==1
       regdata = A_out_Table5_reg5_2;
       else
       regdata = A_out_Table5_reg5;
       end
       Trow    = [5;6];
       Tcolumn = [2 3 4 5 6 7 10];
    end
    
    for ccc = 3:(size(regdata,2) - 1)
        tmp  = regdata(:,ccc);
        rlt0 = nwest(tmp,ones(size(tmp,1),1),0);
        A_out_Table10_PanelC(Trow(1),Tcolumn(ccc-2)) = rlt0.beta;
        A_out_Table10_PanelC(Trow(2),Tcolumn(ccc-2)) = rlt0.tstat;
        clear tmp rlt0
    end
    for ccc = size(regdata,2)
        tmp = regdata(:,ccc);
        A_out_Table10_PanelC(Trow(1),Tcolumn(end))   = mean(tmp) * 100;
        clear tmp
    end
    clear regdata Trow Tcolumn 
end
%}
% Panel D Summary statistics for portfolios formed on EIVOL estimated 
% in a rolling window
%{
clear
clc

SamplePeriod = 1;
% NO. Table Panel Sample Period Description
% 1   Panel B Sample Period July 1963 to December 2006
% 2   Panel C Sample Period January 2007 to December 2017
% 3   Panel D Sample Period July 1963 to December 2017
% 4   Panel E Sample Period July 1926 to June 1963

IndSave      =0;

data = [];
for num0 = 1:9
if SamplePeriod == 1
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2006' '.mat'])
elseif SamplePeriod == 2
load(['tmpmatch_allstocks_' num2str(num0) '_2007_2017' '.mat'])
elseif SamplePeriod == 3
load(['tmpmatch_allstocks_' num2str(num0) '_1963_2017' '.mat'])
elseif SamplePeriod == 4
load(['tmpmatch_allstocks_' num2str(num0) '_1926_1963' '.mat'])
end
data = [data;A_outmatch];
clear A_outmatch
end
clear num0

monthall = unique(data(:,2));

% RET(t)
retc       = 3;
% Price(t-1)
lagprcc    = 9;
% SHROUT(t-1)
lagshroutc = 10;
% IVOL(t)
ivolc      = 13;
% BETA
betac      = 15;
% SIZE
sizec      = 7;
% BM
bmc        = 8;
% EIVOL(t)|(t-1)
sortc      = 76;
% EIVOL(t)|(t)
sortc_2    = 88;

A_out_Table10_PanelD        = [];
A_out_Table10_PanelD_size   = [];
A_out_Table10_PanelD_bm     = [];

A_out_Table10_PanelD_2      = [];
A_out_Table10_PanelD_size_2 = [];
A_out_Table10_PanelD_bm_2   = [];

% Minimum Number of Stocks within a Month to do sorting analysis
cut1 = 50;

for j = 1:size(monthall,1)
    
    tmpdata = data(data(:,2) == monthall(j),:);
    tmpdata = tmpdata(tmpdata(:,sortc) ~= -9.9999,:);
    if size(tmpdata,1) >= cut1
        
       % Sort by EIVOL(t)|(t-1)
       tmpdata   = sortrows(tmpdata,sortc);
       interval0 = floor(size(tmpdata,1) / 10);
       tmpout = [];
       
       for ggg = 1:10
           tmptmpout = [];
           if ggg == 1
              datagroup = tmpdata(1:interval0,:);
           elseif ggg == 2
              datagroup = tmpdata(interval0 + 1:interval0*2,:);
           elseif ggg == 3
              datagroup = tmpdata(interval0*2 + 1:interval0*3,:);
           elseif ggg == 4
              datagroup = tmpdata(interval0*3 + 1:interval0*4,:);
           elseif ggg == 5
              datagroup = tmpdata(interval0*4 + 1:interval0*5,:);
           elseif ggg == 6
              datagroup = tmpdata(interval0*5 + 1:interval0*6,:);
           elseif ggg == 7
              datagroup = tmpdata(interval0*6 + 1:interval0*7,:);
           elseif ggg == 8
              datagroup = tmpdata(interval0*7 + 1:interval0*8,:);
           elseif ggg == 9
              datagroup = tmpdata(interval0*8 + 1:interval0*9,:);
           elseif ggg == 10
              datagroup = tmpdata(interval0*9 + 1:end,:);
           end
           
           for ccc = [retc sortc ivolc betac sizec bmc]
               % Portfolio Value-weighted RET and Equal-weighted RET
               if ccc == retc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999 & ...
                               datagroup(:,lagprcc) ~= -9.9999 & ...
                               datagroup(:,lagshroutc) ~= -9.9999,:);
                  datagroup1(:,57) = datagroup1(:,lagprcc).* ...
                                     datagroup1(:,lagshroutc);
                  tmp1       = sum(datagroup1(:,ccc).*datagroup1(:,57)/...
                               sum(datagroup1(:,57)));
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp2       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout tmp1 * 100 tmp2 * 100];
               elseif ccc == sortc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc).^0.5);
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1 * 100];
               elseif ccc == ivolc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1 * 100];
               elseif ccc == betac
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1];
               elseif ccc == sizec
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  A_out_Table10_PanelD_size = ...
                 [A_out_Table10_PanelD_size; ...
                  ones(size(datagroup1,1),1) * ggg datagroup1(:,ccc)];
               elseif ccc == bmc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  A_out_Table10_PanelD_bm   = ...
                 [A_out_Table10_PanelD_bm; ...
                  ones(size(datagroup1,1),1) * ggg datagroup1(:,ccc)];
               end
               clear datagroup1 tmp1 tmp2
           end
           tmpout = [tmpout;monthall(j) ggg tmptmpout];
           clear tmptmpout datagroup
       end
       clear interval0
       A_out_Table10_PanelD = [A_out_Table10_PanelD;tmpout];
    end
    clear tmpout tmpdata
    
    tmpdata = data(data(:,2) == monthall(j),:);
    tmpdata = tmpdata(tmpdata(:,sortc_2) ~= -9.9999,:);
    if size(tmpdata,1) >= cut1
       % Sort by EIVOL(t)|(t)  
       tmpdata   = sortrows(tmpdata,sortc_2);
       interval0 = floor(size(tmpdata,1) / 10);
       tmpout    = [];
       for ggg = 1:10
           tmptmpout = [];
           if ggg == 1
              datagroup = tmpdata(1:interval0,:);
           elseif ggg == 2
              datagroup = tmpdata(interval0 + 1:interval0*2,:);
           elseif ggg == 3
              datagroup = tmpdata(interval0*2 + 1:interval0*3,:);
           elseif ggg == 4
              datagroup = tmpdata(interval0*3 + 1:interval0*4,:);
           elseif ggg == 5
              datagroup = tmpdata(interval0*4 + 1:interval0*5,:);
           elseif ggg == 6
              datagroup = tmpdata(interval0*5 + 1:interval0*6,:);
           elseif ggg == 7
              datagroup = tmpdata(interval0*6 + 1:interval0*7,:);
           elseif ggg == 8
              datagroup = tmpdata(interval0*7 + 1:interval0*8,:);
           elseif ggg == 9
              datagroup = tmpdata(interval0*8 + 1:interval0*9,:);
           elseif ggg == 10
              datagroup = tmpdata(interval0*9 + 1:end,:);
              
           end
           for ccc = [retc sortc_2 ivolc betac sizec bmc]
               
               if ccc == retc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999 & ...
                  datagroup(:,lagprcc) ~= -9.9999 & ...
                  datagroup(:,lagshroutc) ~= -9.9999,:);
                  datagroup1(:,57) = datagroup1(:,lagprcc).*...
                  datagroup1(:,lagshroutc);
                  tmp1       = sum(datagroup1(:,ccc).*datagroup1(:,57)/...
                  sum(datagroup1(:,57)));
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  tmp2       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout tmp1 * 100 tmp2 * 100];
               elseif ccc == sortc_2
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc).^0.5);
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1 * 100];
               elseif ccc == ivolc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1 * 100];
               elseif ccc == betac
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  datagroup1 = sortrows(datagroup1,ccc);
                  tmp1       = mean(datagroup1(:,ccc));
                  tmptmpout  = [tmptmpout size(datagroup1,1) tmp1];
               elseif ccc == sizec
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  A_out_Table10_PanelD_size_2 = ...
                 [A_out_Table10_PanelD_size_2; ...
                  ones(size(datagroup1,1),1) * ggg datagroup1(:,ccc)];
               elseif ccc == bmc
                  datagroup1 = datagroup(datagroup(:,ccc) ~= -9.9999,:);
                  A_out_Table10_PanelD_bm_2   = ...
                 [A_out_Table10_PanelD_bm_2; ...
                  ones(size(datagroup1,1),1) * ggg datagroup1(:,ccc)];
               end
               clear datagroup1 tmp1 tmp2
           end
           tmpout = [tmpout;monthall(j) ggg tmptmpout];
           clear tmptmpout datagroup
       end
       clear interval0
       A_out_Table10_PanelD_2 = [A_out_Table10_PanelD_2;tmpout];
    end
    clear tmpout tmpdata
    
end
if IndSave == 1
if SamplePeriod == 1
save Results_Table10_PanelD_allstocks_1963_2006.mat...
     A_out_Table10_PanelD A_out_Table10_PanelD_size...
     A_out_Table10_PanelD_bm...
     A_out_Table10_PanelD_2 A_out_Table10_PanelD_size_2...
     A_out_Table10_PanelD_bm_2
elseif SamplePeriod == 2
save Results_Table10_PanelD_allstocks_2007_2017.mat...
     A_out_Table10_PanelD A_out_Table10_PanelD_size...
     A_out_Table10_PanelD_bm...
     A_out_Table10_PanelD_2 A_out_Table10_PanelD_size_2...
     A_out_Table10_PanelD_bm_2
elseif SamplePeriod == 3
save Results_Table10_PanelD_allstocks_1963_2017.mat...
     A_out_Table10_PanelD A_out_Table10_PanelD_size...
     A_out_Table10_PanelD_bm...
     A_out_Table10_PanelD_2 A_out_Table10_PanelD_size_2...
     A_out_Table10_PanelD_bm_2
elseif SamplePeriod == 4
save Results_Table10_PanelD_allstocks_1926_1963.mat...
     A_out_Table10_PanelD A_out_Table10_PanelD_size...
     A_out_Table10_PanelD_bm...
     A_out_Table10_PanelD_2 A_out_Table10_PanelD_size_2...
     A_out_Table10_PanelD_bm_2
end
end
%}
%{
clear
clc

indinclude_Rt = 0;
% (0 = EIVOL (t)|(t-1); 1= EIVOL (t)|(t))

A_Table10_PanelD   = zeros(8,10);

load dataother.mat data_famafrench3factor_monthly
datafama3 = data_famafrench3factor_monthly;
clear data_famafrench3factor_monthly

if SamplePeriod == 1
load Results_Table10_PanelD_allstocks_1963_2006.mat
elseif SamplePeriod == 2
load Results_Table10_PanelD_allstocks_2007_2017.mat
elseif SamplePeriod == 3
load Results_Table10_PanelD_allstocks_1963_2017.mat
elseif SamplePeriod == 4
load Results_Table10_PanelD_allstocks_1926_1963.mat
end

if indinclude_Rt==1
   data     = A_out_Table10_PanelD_2;
   datasize = A_out_Table10_PanelD_size_2;
   databm   = A_out_Table10_PanelD_bm_2;
else
   data     = A_out_Table10_PanelD;
   datasize = A_out_Table10_PanelD_size;
   databm   = A_out_Table10_PanelD_bm;
end

for ggg = 1:10
    
    tmp     = data(data(:,2) == ggg,:);
    tmpsize = datasize(datasize(:,1) == ggg,:);
    tmpbm   = databm(databm(:,1) == ggg,:);
    
    Trow = 1;
    % VWRET RET
    for ccc = 3:4
        tmptmp = tmp(:,ccc);
        A_Table10_PanelD(Trow,ggg) = mean(tmptmp);
        Trow = Trow + 1;
        clear tmptmp
    end
    % EIVOL IVOL Beta
    for ccc = [6 8 10]
        tmptmp = sum(tmp(:,ccc).* tmp(:,ccc - 1)) / sum(tmp(:,ccc - 1));
        A_Table10_PanelD(Trow,ggg) = tmptmp;
        Trow = Trow + 1;
        clear tmptmp
    end
    % Calculate Alpha
    for ccc = 3
        tmptmp = tmp(:,[1 ccc]);
        month0 = unique(tmptmp(:,1));
        for jj = 1:size(month0,1)
            tmpfama3 = datafama3(datafama3(:,1) == month0(jj),:);
            tmptmp(tmptmp(:,1) == month0(jj),[3 4 5 6]) = ...
            tmpfama3([2 3 4 5]);
            clear tmpfama3
        end
        yyy0 = tmptmp(:,2) - tmptmp(:,6);
        xxx0 = [ones(size(yyy0,1),1) tmptmp(:,[3 4 5])];
        rlt0 = ols1(yyy0,xxx0);
        A_Table10_PanelD(Trow,ggg) = rlt0.beta(1);
        clear tmptmp
    end
    
    A_Table10_PanelD(6,ggg) = prctile(tmpsize(:,2),50) / 1000;
    A_Table10_PanelD(7,ggg) = prctile(tmpbm(:,2),50);
     
end

%}

% ------------------------------ Tables End ------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


