%% Descriptive statistics for size variable
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Desriptive_stat_for_size','A2:AQ2519');
stringVectors = string(raw(:,[4,5,6,8,11,12,13,14,15,16,18,20,21,22]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,17,19,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43]);
% Create output variable
data = reshape([raw{:}],size(raw));
% Create table
ALL_SIZE = table;
% Allocate imported array to column variable names
ALL_SIZE.number = data(:,1);
ALL_SIZE.FUNDID = data(:,2);
ALL_SIZE.FIRMID = data(:,3);
ALL_SIZE.teamID = stringVectors(:,1);
ALL_SIZE.teamIDvintage = stringVectors(:,2);
ALL_SIZE.teamIDvintagenumber = stringVectors(:,3);
ALL_SIZE.VINTAGEYEAR = data(:,4);
ALL_SIZE.STRATEGY = categorical(stringVectors(:,4));
ALL_SIZE.BuyoutDummy = data(:,5);
ALL_SIZE.QUARTILE = data(:,6);
ALL_SIZE.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
ALL_SIZE.COUNTRIES = categorical(stringVectors(:,6));
ALL_SIZE.REGIONS = stringVectors(:,7);
ALL_SIZE.STATUS = categorical(stringVectors(:,8));
ALL_SIZE.FUNDMANAGER = stringVectors(:,9);
ALL_SIZE.DOMICILE = stringVectors(:,10);
ALL_SIZE.FINALCLOSESIZE = data(:,7);
ALL_SIZE.COREINDUSTRIES = categorical(stringVectors(:,11));
ALL_SIZE.INDDUMMY = data(:,8);
ALL_SIZE.INDUSTRIES = categorical(stringVectors(:,12));
ALL_SIZE.MANAGERREGION = categorical(stringVectors(:,13));
ALL_SIZE.MANAGERCOUNTRY = categorical(stringVectors(:,14));
ALL_SIZE.vint90 = data(:,9);
ALL_SIZE.vint91 = data(:,10);
ALL_SIZE.vint92 = data(:,11);
ALL_SIZE.vint93 = data(:,12);
ALL_SIZE.vint94 = data(:,13);
ALL_SIZE.vint95 = data(:,14);
ALL_SIZE.vint96 = data(:,15);
ALL_SIZE.vint97 = data(:,16);
ALL_SIZE.vint98 = data(:,17);
ALL_SIZE.vint99 = data(:,18);
ALL_SIZE.vint00 = data(:,19);
ALL_SIZE.vint01 = data(:,20);
ALL_SIZE.vint02 = data(:,21);
ALL_SIZE.vint03 = data(:,22);
ALL_SIZE.vint04 = data(:,23);
ALL_SIZE.vint05 = data(:,24);
ALL_SIZE.vint06 = data(:,25);
ALL_SIZE.vint07 = data(:,26);
ALL_SIZE.vint08 = data(:,27);
ALL_SIZE.vint09 = data(:,28);
ALL_SIZE.vint10 = data(:,29);
% Clear temporary variables
clearvars data raw stringVectors;
%
ALL_BUYOUT = ALL_SIZE;
ALL_BUYOUT(ALL_BUYOUT.BuyoutDummy < 1,:)=[];
size_buyout_year=varfun(@mean,ALL_BUYOUT,'InputVariables','FINALCLOSESIZE',...
       'GroupingVariables','VINTAGEYEAR')
ALL_VENTURE = ALL_SIZE;
ALL_VENTURE(ALL_VENTURE.BuyoutDummy > 0,:)=[];
size_venture_year=varfun(@mean,ALL_VENTURE,'InputVariables','FINALCLOSESIZE',...
       'GroupingVariables','VINTAGEYEAR')  
%% Descriptive statistics for multiple
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Descriptive_stat_for_multiple','A2:AQ2513');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[4,5,6,8,12,13,14,15,16,17,18,20,21,22]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,19,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43]);
% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
% Create output variable
data = reshape([raw{:}],size(raw));
% Create table
ALL_MULT = table;
% Allocate imported array to column variable names
ALL_MULT.number = data(:,1);
ALL_MULT.FUNDID = data(:,2);
ALL_MULT.FIRMID = data(:,3);
ALL_MULT.teamID = stringVectors(:,1);
ALL_MULT.teamIDvintage = stringVectors(:,2);
ALL_MULT.teamIDvintagenumber = stringVectors(:,3);
ALL_MULT.VINTAGEYEAR = data(:,4);
ALL_MULT.STRATEGY = categorical(stringVectors(:,4));
ALL_MULT.BuyoutDummy = data(:,5);
ALL_MULT.NETMULTIPLEX = data(:,6);
ALL_MULT.QUARTILE = data(:,7);
ALL_MULT.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
ALL_MULT.COUNTRIES = categorical(stringVectors(:,6));
ALL_MULT.REGIONS = stringVectors(:,7);
ALL_MULT.STATUS = categorical(stringVectors(:,8));
ALL_MULT.FUNDMANAGER = stringVectors(:,9);
ALL_MULT.DOMICILE = stringVectors(:,10);
ALL_MULT.COREINDUSTRIES = categorical(stringVectors(:,11));
ALL_MULT.INDDUMMY = data(:,8);
ALL_MULT.INDUSTRIES = categorical(stringVectors(:,12));
ALL_MULT.MANAGERREGION = categorical(stringVectors(:,13));
ALL_MULT.MANAGERCOUNTRY = categorical(stringVectors(:,14));
ALL_MULT.vint90 = data(:,9);
ALL_MULT.vint91 = data(:,10);
ALL_MULT.vint92 = data(:,11);
ALL_MULT.vint93 = data(:,12);
ALL_MULT.vint94 = data(:,13);
ALL_MULT.vint95 = data(:,14);
ALL_MULT.vint96 = data(:,15);
ALL_MULT.vint97 = data(:,16);
ALL_MULT.vint98 = data(:,17);
ALL_MULT.vint99 = data(:,18);
ALL_MULT.vint00 = data(:,19);
ALL_MULT.vint01 = data(:,20);
ALL_MULT.vint02 = data(:,21);
ALL_MULT.vint03 = data(:,22);
ALL_MULT.vint04 = data(:,23);
ALL_MULT.vint05 = data(:,24);
ALL_MULT.vint06 = data(:,25);
ALL_MULT.vint07 = data(:,26);
ALL_MULT.vint08 = data(:,27);
ALL_MULT.vint09 = data(:,28);
ALL_MULT.vint10 = data(:,29);
% Clear temporary variables
clearvars data raw stringVectors R;
%
ALL_BUYOUT = ALL_MULT;
ALL_BUYOUT(ALL_BUYOUT.BuyoutDummy < 1,:)=[];
multiple_buyout_year=varfun(@mean,ALL_BUYOUT,'InputVariables','NETMULTIPLEX',...
       'GroupingVariables','VINTAGEYEAR')
   
multiple_buyout_ind=varfun(@mean,ALL_BUYOUT,'InputVariables','NETMULTIPLEX',...
       'GroupingVariables','COREINDUSTRIES')  
   
multiple_buyout_indd=varfun(@mean,ALL_BUYOUT,'InputVariables','NETMULTIPLEX',...
       'GroupingVariables','INDDUMMY')    

ALL_VENTURE = ALL_MULT;
ALL_VENTURE(ALL_VENTURE.BuyoutDummy > 0,:)=[];
multiple_venture_year=varfun(@mean,ALL_VENTURE,'InputVariables','NETMULTIPLEX',...
       'GroupingVariables','VINTAGEYEAR')  
   
multiple_venture_ind=varfun(@mean,ALL_VENTURE,'InputVariables','NETMULTIPLEX',...
       'GroupingVariables','COREINDUSTRIES')  
   
multiple_venture_indd=varfun(@mean,ALL_VENTURE,'InputVariables','NETMULTIPLEX',...
       'GroupingVariables','INDDUMMY')  

%% Descriptive statistics for irr
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Descriptive_stat_for_irr','A2:AQ2262');
stringVectors = string(raw(:,[4,5,6,8,12,13,14,15,16,17,18,20,21,22]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,19,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43]);
% Create output variable
data = reshape([raw{:}],size(raw));
% Create table
ALL_IRR = table;
% Allocate imported array to column variable names
ALL_IRR.number = data(:,1);
ALL_IRR.FUNDID = data(:,2);
ALL_IRR.FIRMID = data(:,3);
ALL_IRR.teamID = categorical(stringVectors(:,1));
ALL_IRR.teamIDvintage = stringVectors(:,2);
ALL_IRR.teamIDvintagenumber = stringVectors(:,3);
ALL_IRR.VINTAGEYEAR = data(:,4);
ALL_IRR.STRATEGY = categorical(stringVectors(:,4));
ALL_IRR.BuyoutDummy = data(:,5);
ALL_IRR.NETIRR = data(:,6);
ALL_IRR.QUARTILE = data(:,7);
ALL_IRR.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
ALL_IRR.COUNTRIES = categorical(stringVectors(:,6));
ALL_IRR.REGIONS = categorical(stringVectors(:,7));
ALL_IRR.STATUS = categorical(stringVectors(:,8));
ALL_IRR.FUNDMANAGER = categorical(stringVectors(:,9));
ALL_IRR.DOMICILE = categorical(stringVectors(:,10));
ALL_IRR.COREINDUSTRIES = categorical(stringVectors(:,11));
ALL_IRR.INDDUMMY = data(:,8);
ALL_IRR.INDUSTRIES = stringVectors(:,12);
ALL_IRR.MANAGERREGION = categorical(stringVectors(:,13));
ALL_IRR.MANAGERCOUNTRY = categorical(stringVectors(:,14));
ALL_IRR.vint90 = data(:,9);
ALL_IRR.vint91 = data(:,10);
ALL_IRR.vint92 = data(:,11);
ALL_IRR.vint93 = data(:,12);
ALL_IRR.vint94 = data(:,13);
ALL_IRR.vint95 = data(:,14);
ALL_IRR.vint96 = data(:,15);
ALL_IRR.vint97 = data(:,16);
ALL_IRR.vint98 = data(:,17);
ALL_IRR.vint99 = data(:,18);
ALL_IRR.vint00 = data(:,19);
ALL_IRR.vint01 = data(:,20);
ALL_IRR.vint02 = data(:,21);
ALL_IRR.vint03 = data(:,22);
ALL_IRR.vint04 = data(:,23);
ALL_IRR.vint05 = data(:,24);
ALL_IRR.vint06 = data(:,25);
ALL_IRR.vint07 = data(:,26);
ALL_IRR.vint08 = data(:,27);
ALL_IRR.vint09 = data(:,28);
ALL_IRR.vint10 = data(:,29);
% Clear temporary variables
clearvars data raw stringVectors;
%
ALL_BUYOUT = ALL_IRR;
ALL_BUYOUT(ALL_BUYOUT.BuyoutDummy < 1,:)=[];
irr_buyout_year=varfun(@mean,ALL_BUYOUT,'InputVariables','NETIRR',...
       'GroupingVariables','VINTAGEYEAR')
   
irr_buyout_ind=varfun(@mean,ALL_BUYOUT,'InputVariables','NETIRR',...
       'GroupingVariables','COREINDUSTRIES')  
   
irr_buyout_indd=varfun(@mean,ALL_BUYOUT,'InputVariables','NETIRR',...
       'GroupingVariables','INDDUMMY')
   
   irr_buyout_quart=varfun(@mean,ALL_BUYOUT,'InputVariables','NETIRR',...
       'GroupingVariables','QUARTILE') 

ALL_VENTURE = ALL_IRR;
ALL_VENTURE(ALL_VENTURE.BuyoutDummy > 0,:)=[];
irr_venture_year=varfun(@mean,ALL_VENTURE,'InputVariables','NETIRR',...
       'GroupingVariables','VINTAGEYEAR')  
   
irr_venture_ind=varfun(@mean,ALL_VENTURE,'InputVariables','NETIRR',...
       'GroupingVariables','COREINDUSTRIES')  
   
irr_venture_indd=varfun(@mean,ALL_VENTURE,'InputVariables','NETIRR',...
       'GroupingVariables','INDDUMMY')   

   
%% Section 5.1 

%% Section 5.1 all funds multiple t-1 - t
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','All funds for t-1 - t, mult','A2:AS1330');
stringVectors = string(raw(:,[4,5,6,8,12,13,14,15,16,17,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,18,19,20,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]);
% Create output variable
data = reshape([raw{:}],size(raw));
% Create table   
ALL_MULT = table;
% Allocate imported array to column variable names
ALL_MULT.number = data(:,1);
ALL_MULT.FUNDID = data(:,2);
ALL_MULT.FIRMID = data(:,3);
ALL_MULT.teamID = stringVectors(:,1);
ALL_MULT.teamIDvintage = stringVectors(:,2);
ALL_MULT.teamIDvintagenumber = stringVectors(:,3);
ALL_MULT.VINTAGEYEAR = data(:,4);
ALL_MULT.STRATEGY = categorical(stringVectors(:,4));
ALL_MULT.BuyoutDummy = data(:,5);
ALL_MULT.mult_t = data(:,6);
ALL_MULT.mult_t1 = data(:,7);
ALL_MULT.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
ALL_MULT.COUNTRIES = categorical(stringVectors(:,6));
ALL_MULT.REGIONS = stringVectors(:,7);
ALL_MULT.STATUS = categorical(stringVectors(:,8));
ALL_MULT.FUNDMANAGER = stringVectors(:,9);
ALL_MULT.DOMICILE = stringVectors(:,10);
ALL_MULT.FINALCLOSESIZE = data(:,8);
ALL_MULT.size_ = data(:,9);
ALL_MULT.seq_num_t = data(:,10);
ALL_MULT.COREINDUSTRIES = categorical(stringVectors(:,11));
ALL_MULT.INDUSTRIES = stringVectors(:,12);
ALL_MULT.MANAGERREGION = categorical(stringVectors(:,13));
ALL_MULT.MANAGERCOUNTRY = categorical(stringVectors(:,14));
ALL_MULT.vint90 = data(:,11);
ALL_MULT.vint91 = data(:,12);
ALL_MULT.vint92 = data(:,13);
ALL_MULT.vint93 = data(:,14);
ALL_MULT.vint94 = data(:,15);
ALL_MULT.vint95 = data(:,16);
ALL_MULT.vint96 = data(:,17);
ALL_MULT.vint97 = data(:,18);
ALL_MULT.vint98 = data(:,19);
ALL_MULT.vint99 = data(:,20);
ALL_MULT.vint00 = data(:,21);
ALL_MULT.vint01 = data(:,22);
ALL_MULT.vint02 = data(:,23);
ALL_MULT.vint03 = data(:,24);
ALL_MULT.vint04 = data(:,25);
ALL_MULT.vint05 = data(:,26);
ALL_MULT.vint06 = data(:,27);
ALL_MULT.vint07 = data(:,28);
ALL_MULT.vint08 = data(:,29);
ALL_MULT.vint09 = data(:,30);
ALL_MULT.vint10 = data(:,31);
% Clear temporary variables
clearvars data raw stringVectors R;
% Log size
ALL_MULT.size_t=log(ALL_MULT.size_);

% Linear regression reg1mult w/o Buyout Dummy

regmult1 = fitlm(ALL_MULT, 'mult_t~seq_num_t + size_t + mult_t1 +  vint91 + vint92 + vint93 +  vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')

% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE         tStat        pValue  
%                    __________    ________    _________    __________
% 
%     (Intercept)        1.6415      1.1729       1.3995       0.16189
%     mult_t1           0.15981    0.021094       7.5763    6.7082e-14
%     seq_num_t       -0.021847    0.024527     -0.89074       0.37323
%     vint91            0.19493      1.5023      0.12975       0.89678
%     vint92           -0.12285      1.3011    -0.094421       0.92479
%     vint93            0.20904      1.3437      0.15558       0.87639
%     vint94             3.3642      1.2269       2.7421     0.0061881
%     vint95             1.5534      1.2101       1.2837       0.19948
%     vint96             1.4372      1.2038       1.1939       0.23272
%     vint97            0.12818      1.1815      0.10849       0.91362
%     vint98           -0.34231      1.1791     -0.29032       0.77162
%     vint99           -0.82109      1.1773     -0.69745       0.48564
%     vint00           -0.48919      1.1734      -0.4169       0.67682
%     vint01           -0.22028      1.1802     -0.18664       0.85197
%     vint02           -0.23624      1.1864     -0.19912        0.8422
%     vint03           -0.23392       1.186     -0.19723       0.84367
%     vint04           -0.18536      1.1843     -0.15651       0.87566
%     vint05           -0.19558      1.1748     -0.16648        0.8678
%     vint06           -0.23065      1.1745     -0.19639       0.84434
%     vint07            -0.1087       1.174    -0.092587       0.92625
%     vint08            0.11148      1.1753     0.094857       0.92444
%     vint09          -0.033146      1.1844    -0.027984       0.97768
%     vint10          -0.062224      1.1818    -0.052652       0.95802
%     size_t         -0.0045654    0.032346     -0.14114       0.88778
% 
% 
% Number of observations: 1329, Error degrees of freedom: 1305
% Root Mean Squared Error: 1.65
% R-squared: 0.158,  Adjusted R-Squared 0.143
% F-statistic vs. constant model: 10.6, p-value = 3.32e-35

%DIAGNOSTICS of CLRM assumptions

T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -4.6018e-14


% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9618

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.


%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

ALL_MULT.uRaw = regmult1.Residuals.Raw; 
ALL_MULT.uRaw2 = u.^2;

% Create a table with the constant and squared terms
mult1White = table(ALL_MULT.mult_t, ALL_MULT.seq_num_t, ALL_MULT.size_t, ALL_MULT.mult_t1, ALL_MULT.vint91, ALL_MULT.vint92, ALL_MULT.vint93, ALL_MULT.vint94, ...
    ALL_MULT.vint95, ALL_MULT.vint96, ALL_MULT.vint97, ALL_MULT.vint98, ALL_MULT.vint99,ALL_MULT.vint00, ALL_MULT.vint01, ALL_MULT.vint02, ALL_MULT.vint03, ALL_MULT.vint04,...
    ALL_MULT.vint05, ALL_MULT.vint06, ALL_MULT.vint07, ALL_MULT.vint08, ALL_MULT.vint09, ALL_MULT.vint10, ALL_MULT.size_t.^2, ALL_MULT.mult_t1.^2, ALL_MULT.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};

% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + mult_t1 + vint91 + vint92 + vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
% 
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)      -1.2957       18.92    -0.068479       0.94541
%     seq_num_t       0.015013     0.37515      0.04002       0.96808
%     size_t          -0.72649      2.4774     -0.29325       0.76938
%     mult_t1           2.7416     0.61969       4.4242    1.0488e-05
%     vint91           0.42882      22.687     0.018902       0.98492
%     vint92          -0.63964      19.648    -0.032555       0.97403
%     vint93          -0.38933      20.296    -0.019183        0.9847
%     vint94            47.362      18.529       2.5561      0.010698
%     vint95            14.333      18.284      0.78393       0.43322
%     vint96            20.343      18.189       1.1184       0.26358
%     vint97            1.1018      17.842     0.061752       0.95077
%     vint98            2.4533      17.805      0.13778       0.89043
%     vint99           -0.3349      17.779    -0.018837       0.98497
%     vint00            1.3286       17.72     0.074974       0.94025
%     vint01            2.6424      17.824      0.14825       0.88217
%     vint02            1.5877      17.916     0.088619        0.9294
%     vint03            1.4206       17.91     0.079319       0.93679
%     vint04           0.93778      17.884     0.052435       0.95819
%     vint05            1.5115       17.74     0.085202       0.93211
%     vint06            1.7192      17.736     0.096936       0.92279
%     vint07            1.3285      17.728     0.074933       0.94028
%     vint08            3.8871      17.749      0.21901       0.82668
%     vint09            1.5745      17.886     0.088027       0.92987
%     vint10            1.2677      17.847     0.071033       0.94338
%     size_sq        0.0019173     0.21452    0.0089376       0.99287
%     mult_t1sq      -0.046751    0.021285      -2.1964      0.028238
% 
% 
% Number of observations: 1329, Error degrees of freedom: 1303
% Root Mean Squared Error: 24.8
% R-squared: 0.0917,  Adjusted R-Squared 0.0742
% F-statistic vs. constant model: 5.26, p-value = 1.65e-15

% % Langrange Multiplier (LM) test
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
%output: test_stat_e =  121.8126, Crit_Val_e =  35.1725, P_e = 1.9984e-15, reject_e = 1
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
% Next step is to use heteroscedasticity-consistent standard errors.

x = [ALL_MULT.seq_num_t ALL_MULT.size_t ALL_MULT.mult_t1 ...
    ALL_MULT.vint91 ALL_MULT.vint92 ALL_MULT.vint93 ALL_MULT.vint94 ALL_MULT.vint95 ALL_MULT.vint96 ALL_MULT.vint97...
    ALL_MULT.vint98 ALL_MULT.vint99 ALL_MULT.vint00 ALL_MULT.vint01 ALL_MULT.vint02...
    ALL_MULT.vint03 ALL_MULT.vint04 ALL_MULT.vint05 ALL_MULT.vint06 ALL_MULT.vint07 ALL_MULT.vint08...
    ALL_MULT.vint09 ALL_MULT.vint10];

y = ALL_MULT.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', ...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White
% % 
% White =
% 
%   24?4 table
% 
%                       beta          SE         tstat          p     
%                    __________    ________    _________    __________
% 
%     (Intercept)        1.6415     0.55985       2.9321     0.0034246
%     seq_num_t       -0.021847     0.01214      -1.7995      0.072162
%     size_t         -0.0045654    0.026225     -0.17408       0.86183
%     mult_t1           0.15981    0.042275       3.7804    0.00016355
%     vint91            0.19493     0.64759      0.30101       0.76345
%     vint92           -0.12285     0.56126     -0.21888       0.82678
%     vint93            0.20904     0.75715      0.27609       0.78252
%     vint94             3.3642      1.6369       2.0553      0.040048
%     vint95             1.5534     0.98397       1.5787       0.11464
%     vint96             1.4372      1.0089       1.4246       0.15451
%     vint97            0.12818     0.57558       0.2227        0.8238
%     vint98           -0.34231     0.57435       -0.596       0.55128
%     vint99           -0.82109     0.54888      -1.4959       0.13491
%     vint00           -0.48919     0.54103     -0.90419       0.36606
%     vint01           -0.22028      0.5438     -0.40507       0.68549
%     vint02           -0.23624     0.54535     -0.43319       0.66495
%     vint03           -0.23392     0.55951     -0.41807       0.67596
%     vint04           -0.18536      0.5409     -0.34268       0.73189
%     vint05           -0.19558      0.5503      -0.3554       0.72234
%     vint06           -0.23065     0.54914     -0.42002       0.67454
%     vint07            -0.1087     0.54812     -0.19831       0.84284
%     vint08            0.11148     0.56276       0.1981         0.843
%     vint09          -0.033146     0.54583    -0.060725       0.95159
%     vint10          -0.062224     0.55151     -0.11282       0.91019
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags

%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_seq_num_u = corr(ALL_MULT.seq_num_t,u)
corr_size_t_u = corr(ALL_MULT.size_t,u)
corr_mult_t1_u = corr(ALL_MULT.mult_t1,u)
%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest

% output: 

% skew_jbtest = 7.3708
% kurt_jbtest = 95.1318
% test_stat_jbtest = 4.8207e+05
% crit_val_jbtest = 38.0756
% p_jbtest = 0 
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 


% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_MULT(regmult1.Diagnostics.CooksDistance >=0.05,:);

% Linear regression reg1mult w/ Buyout Dummy

regmult1 = fitlm(ALL_MULT, 'mult_t~seq_num_t + BuyoutDummy + size_t + mult_t1 +  vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')

% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)       1.6959      1.1753       1.4429       0.14928
%     BuyoutDummy      0.08185     0.10804      0.75758       0.44884
%     mult_t1          0.16054    0.021119       7.6016    5.5665e-14
%     seq_num_t      -0.018408    0.024947     -0.73787       0.46073
%     vint91           0.16678       1.503      0.11096       0.91166
%     vint92          -0.15198      1.3019     -0.11674       0.90709
%     vint93           0.16658      1.3451      0.12385       0.90146
%     vint94             3.343      1.2274       2.7237     0.0065419
%     vint95            1.5132      1.2115        1.249       0.21189
%     vint96            1.4051      1.2047       1.1663        0.2437
%     vint97           0.10142      1.1822      0.08579       0.93165
%     vint98          -0.36819      1.1798     -0.31209       0.75502
%     vint99          -0.83964      1.1777     -0.71294       0.47601
%     vint00          -0.50558      1.1738     -0.43072       0.66674
%     vint01          -0.23682      1.1806     -0.20059       0.84105
%     vint02          -0.26679      1.1873     -0.22471       0.82224
%     vint03          -0.26891      1.1871     -0.22653       0.82083
%     vint04          -0.22272      1.1856     -0.18786       0.85101
%     vint05          -0.23417      1.1761     -0.19911       0.84221
%     vint06          -0.25632      1.1751     -0.21811       0.82737
%     vint07          -0.13971      1.1749     -0.11891       0.90537
%     vint08          0.084858       1.176     0.072159       0.94249
%     vint09         -0.068403      1.1855    -0.057698         0.954
%     vint10         -0.098313       1.183    -0.083108       0.93378
%     size_t         -0.018938    0.037503     -0.50496       0.61367
% 
% 
% Number of observations: 1329, Error degrees of freedom: 1304
% Root Mean Squared Error: 1.65
% R-squared: 0.158,  Adjusted R-Squared 0.143
% F-statistic vs. constant model: 10.2, p-value = 8.47e-35

%DIAGNOSTICS of CLRM assumptions

T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -4.6028e-14


% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9618

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test
ALL_MULT.uRaw = regmult1.Residuals.Raw; 
ALL_MULT.uRaw2 = u.^2;

% Create a table with the constant and squared terms
mult1White = table(ALL_MULT.mult_t, ALL_MULT.seq_num_t, ALL_MULT.BuyoutDummy, ALL_MULT.size_t, ALL_MULT.mult_t1, ALL_MULT.vint91, ALL_MULT.vint92, ALL_MULT.vint93, ALL_MULT.vint94, ...
    ALL_MULT.vint95, ALL_MULT.vint96, ALL_MULT.vint97, ALL_MULT.vint98, ALL_MULT.vint99,ALL_MULT.vint00, ALL_MULT.vint01, ALL_MULT.vint02, ALL_MULT.vint03, ALL_MULT.vint04,...
    ALL_MULT.vint05, ALL_MULT.vint06, ALL_MULT.vint07, ALL_MULT.vint08, ALL_MULT.vint09, ALL_MULT.vint10, ALL_MULT.size_t.^2, ALL_MULT.mult_t1.^2, ALL_MULT.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','BuyoutDummy', 'size_t','mult_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};

% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + mult_t1 + vint91 +vint92+vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')

% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 27 terms in 26 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)      -2.8633      18.951    -0.15109       0.87993
%     seq_num_t       -0.16238     0.38223    -0.42482       0.67104
%     BuyoutDummy      -4.1056      1.6422        -2.5       0.01254
%     size_t          -0.49854      2.4817    -0.20089       0.84082
%     mult_t1           2.8033     0.62077      4.5158    6.8781e-06
%     vint91            1.8115      22.718    0.079737       0.93646
%     vint92           0.78366      19.677    0.039826       0.96824
%     vint93            1.5711      20.333    0.077267       0.93842
%     vint94            48.579      18.554      2.6183     0.0089399
%     vint95            16.422      18.319     0.89643       0.37019
%     vint96            21.939      18.219      1.2042       0.22873
%     vint97            2.4692      17.868     0.13819       0.89011
%     vint98            3.7203      17.832     0.20864       0.83477
%     vint99           0.56307      17.801     0.03163       0.97477
%     vint00            2.1557      17.742      0.1215       0.90331
%     vint01            3.5086      17.847      0.1966       0.84417
%     vint02            3.1178      17.946     0.17373        0.8621
%     vint03             3.178      17.943     0.17712       0.85944
%     vint04            2.8042      17.919     0.15649       0.87567
%     vint05            3.4584      17.776     0.19456       0.84577
%     vint06            3.0013      17.762     0.16897       0.86585
%     vint07            2.8702      17.758     0.16163       0.87162
%     vint08            5.2274      17.775     0.29408       0.76874
%     vint09            3.3687       17.92     0.18799       0.85091
%     vint10            3.1109      17.881     0.17397       0.86192
%     size_sq         0.045165     0.21544     0.20964       0.83398
%     mult_t1sq      -0.050605    0.021359     -2.3693      0.017969
% 
% 
% Number of observations: 1329, Error degrees of freedom: 1302
% Root Mean Squared Error: 24.9
% R-squared: 0.096,  Adjusted R-Squared 0.078
% F-statistic vs. constant model: 5.32, p-value = 2.9e-16

% % Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  127.6178, Crit_Val_e =  36.4150, P_e = 4.4409-16, reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [ALL_MULT.seq_num_t ALL_MULT.size_t ALL_MULT.mult_t1 ALL_MULT.BuyoutDummy...
    ALL_MULT.vint91 ALL_MULT.vint92 ALL_MULT.vint93 ALL_MULT.vint94 ALL_MULT.vint95 ALL_MULT.vint96 ALL_MULT.vint97...
    ALL_MULT.vint98 ALL_MULT.vint99 ALL_MULT.vint00 ALL_MULT.vint01 ALL_MULT.vint02...
    ALL_MULT.vint03 ALL_MULT.vint04 ALL_MULT.vint05 ALL_MULT.vint06 ALL_MULT.vint07 ALL_MULT.vint08...
    ALL_MULT.vint09 ALL_MULT.vint10];

y = ALL_MULT.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'BuyoutDummy', ...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White
% White =
% 
%   25?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       1.6959     0.56455      3.0039      0.002715
%     seq_num_t      -0.018408    0.013441     -1.3696       0.17106
%     size_t         -0.018938    0.030078    -0.62962       0.52905
%     mult_t1          0.16054    0.041664      3.8532    0.00012216
%     BuyoutDummy      0.08185     0.10516     0.77834       0.43651
%     vint91           0.16678     0.63554     0.26242       0.79304
%     vint92          -0.15198     0.56101     -0.2709       0.78651
%     vint93           0.16658     0.75071      0.2219       0.82443
%     vint94             3.343      1.6479      2.0286      0.042695
%     vint95            1.5132      1.0049      1.5058       0.13235
%     vint96            1.4051      1.0225      1.3742       0.16961
%     vint97           0.10142      0.5782     0.17541       0.86079
%     vint98          -0.36819     0.57198     -0.6437       0.51988
%     vint99          -0.83964      0.5459     -1.5381       0.12427
%     vint00          -0.50558     0.53897    -0.93805       0.34839
%     vint01          -0.23682     0.54218     -0.4368       0.66233
%     vint02          -0.26679     0.54487    -0.48963       0.62448
%     vint03          -0.26891     0.55962    -0.48052       0.63094
%     vint04          -0.22272     0.53971    -0.41267       0.67992
%     vint05          -0.23417     0.55247    -0.42385       0.67174
%     vint06          -0.25632     0.54893    -0.46694       0.64062
%     vint07          -0.13971     0.54803    -0.25493       0.79882
%     vint08          0.084858     0.56301     0.15072       0.88022
%     vint09         -0.068403     0.54679     -0.1251       0.90046
%     vint10         -0.098313     0.55089    -0.17846       0.85839
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags

%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
corr_seq_num_u = corr(ALL_MULT.seq_num_t,u)
corr_size_t_u = corr(ALL_MULT.size_t,u)
corr_mult_t1_u = corr(ALL_MULT.mult_t1,u)
%no correlation
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest

% output: 

% skew_jbtest = 7.4132
% kurt_jbtest = 95.7988
% test_stat_jbtest = 4.8905e+05
% crit_val_jbtest = 39.3641
% p_jbtest = 0 
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 

% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_MULT(regmult1.Diagnostics.CooksDistance >=0.05,:);

% Linear regression reg1mult w/ Buyout Dummy*mult_t1

ALL_MULT.interact=ALL_MULT.BuyoutDummy.*ALL_MULT.mult_t1
 
regmult1 = fitlm(ALL_MULT, 'mult_t~seq_num_t + BuyoutDummy + interact+ size_t + mult_t1 + vint91 + vint92 + vint93  + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
 

% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)       1.6961       1.175       1.4435       0.14911
%     BuyoutDummy      0.28728     0.19011       1.5111       0.13101
%     mult_t1          0.16779    0.021824       7.6885    2.9186e-14
%     seq_num_t       -0.02029    0.024981     -0.81219       0.41683
%     vint91            0.2248      1.5033      0.14954       0.88115
%     vint92          -0.14242      1.3015     -0.10942       0.91288
%     vint93           0.25341      1.3463      0.18822       0.85073
%     vint94            3.3606      1.2271       2.7386     0.0062535
%     vint95             1.567      1.2118       1.2931       0.19621
%     vint96            1.4124      1.2044       1.1727       0.24113
%     vint97           0.11658      1.1819     0.098634       0.92144
%     vint98           -0.3698      1.1794     -0.31354       0.75392
%     vint99          -0.85528      1.1774     -0.72639       0.46773
%     vint00          -0.50633      1.1735     -0.43148       0.66619
%     vint01           -0.2374      1.1803     -0.20114       0.84062
%     vint02          -0.27062      1.1869     -0.22799       0.81969
%     vint03           -0.2681      1.1867     -0.22591        0.8213
%     vint04          -0.21348      1.1853     -0.18012       0.85709
%     vint05          -0.22592      1.1757     -0.19215       0.84765
%     vint06          -0.24698      1.1748     -0.21023       0.83352
%     vint07          -0.13483      1.1746     -0.11479       0.90863
%     vint08          0.079872      1.1757     0.067937       0.94585
%     vint09         -0.081808      1.1853    -0.069021       0.94498
%     vint10         -0.097035      1.1826     -0.08205       0.93462
%     size_t         -0.021271    0.037535     -0.56671       0.57101
%     interact        -0.10296    0.078415      -1.3131        0.1894
% 
% 
% Number of observations: 1329, Error degrees of freedom: 1303
% Root Mean Squared Error: 1.65
% R-squared: 0.159,  Adjusted R-Squared 0.143
% F-statistic vs. constant model: 9.86, p-value = 1.26e-34

%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -3.7010e-14
  
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9618
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
ALL_MULTuRaw = regmult1.Residuals.Raw; 
ALL_MULT.uRaw2 = u.^2;
 
 % Create a table with the constant and squared terms
mult1White = table(ALL_MULT.mult_t, ALL_MULT.seq_num_t, ALL_MULT.BuyoutDummy, ALL_MULT.interact, ALL_MULT.size_t, ALL_MULT.mult_t1, ALL_MULT.vint91, ALL_MULT.vint92, ALL_MULT.vint93, ALL_MULT.vint94, ...
    ALL_MULT.vint95, ALL_MULT.vint96, ALL_MULT.vint97, ALL_MULT.vint98, ALL_MULT.vint99,ALL_MULT.vint00, ALL_MULT.vint01, ALL_MULT.vint02, ALL_MULT.vint03, ALL_MULT.vint04,...
    ALL_MULT.vint05, ALL_MULT.vint06, ALL_MULT.vint07, ALL_MULT.vint08, ALL_MULT.vint09, ALL_MULT.vint10, ALL_MULT.size_t.^2, ALL_MULT.mult_t1.^2, ALL_MULT.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','BuyoutDummy', 'interact',  'size_t','mult_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact + mult_t1 + vint91 + vint92 + vint93+vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)      -4.4856      18.805     -0.23854        0.8115
%     seq_num_t       -0.20503     0.37937     -0.54044       0.58899
%     BuyoutDummy       4.2296      2.9606       1.4286       0.15336
%     interact         -4.2386      1.2596       -3.365    0.00078782
%     size_t          -0.43696      2.4618      -0.1775       0.85915
%     mult_t1           3.7619     0.68113        5.523    4.0208e-08
%     vint91            4.3522      22.547      0.19303       0.84697
%     vint92             1.091      19.519     0.055894       0.95544
%     vint93             5.087      20.194      0.25191       0.80115
%     vint94            48.918      18.405       2.6579       0.00796
%     vint95            17.689      18.177      0.97312       0.33067
%     vint96            21.448      18.072       1.1868       0.23554
%     vint97             2.951      17.725      0.16648        0.8678
%     vint98            3.5217      17.688       0.1991       0.84222
%     vint99          -0.22388       17.66    -0.012677       0.98989
%     vint00            2.3672        17.6       0.1345       0.89303
%     vint01            3.8658      17.704      0.21837       0.82718
%     vint02            3.2038      17.802      0.17997        0.8572
%     vint03            3.4111      17.798      0.19165       0.84804
%     vint04            3.2526      17.775      0.18298       0.85484
%     vint05            3.8492      17.633      0.21829       0.82723
%     vint06            3.5216       17.62      0.19986       0.84162
%     vint07            3.1646      17.615      0.17965       0.85746
%     vint08            5.2568      17.632      0.29813       0.76565
%     vint09            3.0233      17.776      0.17008       0.86497
%     vint10            3.2094      17.738      0.18094       0.85644
%     size_sq         0.031054     0.21374      0.14528       0.88451
%     mult_t1sq      -0.076628    0.022691       -3.377    0.00075458
% 
% 
% Number of observations: 1329, Error degrees of freedom: 1301
% Root Mean Squared Error: 24.7
% R-squared: 0.104,  Adjusted R-Squared 0.0854
% F-statistic vs. constant model: 5.59, p-value = 5.68e-18

% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  138.2197, Crit_Val_e =  37.6525, P_e = 0, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_MULT.seq_num_t ALL_MULT.size_t ALL_MULT.mult_t1 ALL_MULT.BuyoutDummy ALL_MULT.interact ...
    ALL_MULT.vint91 ALL_MULT.vint92 ALL_MULT.vint93 ALL_MULT.vint94 ALL_MULT.vint95 ALL_MULT.vint96 ALL_MULT.vint97...
    ALL_MULT.vint98 ALL_MULT.vint99 ALL_MULT.vint00 ALL_MULT.vint01 ALL_MULT.vint02...
    ALL_MULT.vint03 ALL_MULT.vint04 ALL_MULT.vint05 ALL_MULT.vint06 ALL_MULT.vint07 ALL_MULT.vint08...
    ALL_MULT.vint09 ALL_MULT.vint10];
 
y = ALL_MULT.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'BuyoutDummy', 'interact' ...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
% 
% White =
% 
%   26?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       1.6961     0.56066      3.0252     0.0025327
%     seq_num_t       -0.02029     0.01387     -1.4629       0.14374
%     size_t         -0.021271    0.029562    -0.71955       0.47193
%     mult_t1          0.16779    0.048598      3.4527    0.00057257
%     BuyoutDummy      0.28728     0.12652      2.2706      0.023331
%     interact        -0.10296    0.077035     -1.3366       0.18159
%     vint91            0.2248      0.6603     0.34045       0.73357
%     vint92          -0.14242     0.55481     -0.2567       0.79745
%     vint93           0.25341     0.76556     0.33101       0.74069
%     vint94            3.3606      1.6505      2.0362      0.041931
%     vint95             1.567      1.0171      1.5407       0.12363
%     vint96            1.4124      1.0179      1.3875       0.16551
%     vint97           0.11658     0.57356     0.20325       0.83897
%     vint98           -0.3698     0.56823     -0.6508       0.51529
%     vint99          -0.85528     0.54184     -1.5785       0.11469
%     vint00          -0.50633     0.53421    -0.94781        0.3434
%     vint01           -0.2374     0.53709    -0.44202       0.65855
%     vint02          -0.27062     0.53928    -0.50182       0.61588
%     vint03           -0.2681      0.5545    -0.48351       0.62881
%     vint04          -0.21348      0.5338    -0.39993       0.68928
%     vint05          -0.22592     0.54731    -0.41279       0.67983
%     vint06          -0.24698      0.5448    -0.45334       0.65038
%     vint07          -0.13483     0.54288    -0.24835        0.8039
%     vint08          0.079872     0.55836     0.14305       0.88628
%     vint09         -0.081808     0.54219    -0.15088       0.88009
%     vint10         -0.097035     0.54578    -0.17779       0.85891
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_MULT.seq_num_t,u)
corr_size_t_u = corr(ALL_MULT.size_t,u)
corr_mult_t1_u = corr(ALL_MULT.mult_t1,u)
%no correlation
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 7.3640
% kurt_jbtest = 95.2867
% test_stat_jbtest = 4.8363e+05
% crit_val_jbtest = 40.6465
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_MULT(regmult1.Diagnostics.CooksDistance >=0.05,:);

%% Section 5.1 all funds multiple t-2 - t-1 - t
clc 
clear all
close all
 
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','All funds for t-2-t-1-t, mult','A2:AT744');
stringVectors = string(raw(:,[4,5,6,8,13,14,15,16,17,18,22,23,24,25]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,19,20,21,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
ALL_MULT = table;
 
% Allocate imported array to column variable names
ALL_MULT.number = data(:,1);
ALL_MULT.FUNDID = data(:,2);
ALL_MULT.FIRMID = data(:,3);
ALL_MULT.teamID = stringVectors(:,1);
ALL_MULT.teamIDvintage = stringVectors(:,2);
ALL_MULT.teamIDvintagenumber = stringVectors(:,3);
ALL_MULT.VINTAGEYEAR = data(:,4);
ALL_MULT.STRATEGY = categorical(stringVectors(:,4));
ALL_MULT.BuyoutDummy = data(:,5);
ALL_MULT.mult_t = data(:,6);
ALL_MULT.mult_t1 = data(:,7);
ALL_MULT.mult_t2 = data(:,8);
ALL_MULT.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
ALL_MULT.COUNTRIES = categorical(stringVectors(:,6));
ALL_MULT.REGIONS = categorical(stringVectors(:,7));
ALL_MULT.STATUS = categorical(stringVectors(:,8));
ALL_MULT.FUNDMANAGER = stringVectors(:,9);
ALL_MULT.DOMICILE = stringVectors(:,10);
ALL_MULT.FINALCLOSESIZE = data(:,9);
ALL_MULT.size_ = data(:,10);
ALL_MULT.seq_num_t = data(:,11);
ALL_MULT.COREINDUSTRIES = categorical(stringVectors(:,11));
ALL_MULT.INDUSTRIES = stringVectors(:,12);
ALL_MULT.MANAGERREGION = categorical(stringVectors(:,13));
ALL_MULT.MANAGERCOUNTRY = categorical(stringVectors(:,14));
ALL_MULT.vint90 = data(:,12);
ALL_MULT.vint91 = data(:,13);
ALL_MULT.vint92 = data(:,14);
ALL_MULT.vint93 = data(:,15);
ALL_MULT.vint94 = data(:,16);
ALL_MULT.vint95 = data(:,17);
ALL_MULT.vint96 = data(:,18);
ALL_MULT.vint97 = data(:,19);
ALL_MULT.vint98 = data(:,20);
ALL_MULT.vint99 = data(:,21);
ALL_MULT.vint00 = data(:,22);
ALL_MULT.vint01 = data(:,23);
ALL_MULT.vint02 = data(:,24);
ALL_MULT.vint03 = data(:,25);
ALL_MULT.vint04 = data(:,26);
ALL_MULT.vint05 = data(:,27);
ALL_MULT.vint06 = data(:,28);
ALL_MULT.vint07 = data(:,29);
ALL_MULT.vint08 = data(:,30);
ALL_MULT.vint09 = data(:,31);
ALL_MULT.vint10 = data(:,32);
 
% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
ALL_MULT.size_t=log(ALL_MULT.size_);
 
% Linear regression reg1mult w/o Buyout Dummy
 
regmult2 = fitlm(ALL_MULT, 'mult_t~seq_num_t + size_t + mult_t1 + mult_t2+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult2 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat       pValue  
%                    _________    ________    _______    __________
% 
%     (Intercept)       5.8255      0.8633     6.7479    3.0906e-11
%     mult_t1          0.12996    0.021805     5.9604    3.9445e-09
%     mult_t2         0.011416    0.018995      0.601       0.54803
%     seq_num_t      -0.032092    0.022222    -1.4442       0.14912
%     vint91           -4.7001      1.1946    -3.9344    9.1498e-05
%     vint92           -4.5954      1.1931    -3.8516    0.00012783
%     vint93           -3.6854      1.1956    -3.0825     0.0021315
%     vint94                 0           0        NaN           NaN
%     vint95           -4.0947     0.95726    -4.2775    2.1463e-05
%     vint96           -3.6244     0.95952    -3.7772    0.00017171
%     vint97           -3.2623     0.88301    -3.6946    0.00023702
%     vint98           -4.4199     0.87044    -5.0777    4.8722e-07
%     vint99           -5.1768     0.86361    -5.9943    3.2343e-09
%     vint00           -4.8042     0.85657    -5.6087    2.9099e-08
%     vint01           -4.5616     0.86366    -5.2817    1.6982e-07
%     vint02           -4.5834     0.87442    -5.2417    2.0942e-07
%     vint03           -4.5061     0.86804    -5.1911    2.7251e-07
%     vint04           -4.4441     0.86945    -5.1114    4.1052e-07
%     vint05           -4.5352     0.85948    -5.2767    1.7433e-07
%     vint06           -4.4671     0.85789    -5.2071    2.5071e-07
%     vint07           -4.3177     0.85778    -5.0336    6.0921e-07
%     vint08           -4.0874     0.86099    -4.7474     2.488e-06
%     vint09           -4.2909     0.86497    -4.9608    8.7714e-07
%     vint10           -4.2621     0.86438    -4.9308    1.0182e-06
%     size_t          0.019402    0.030152    0.64348       0.52012
% 
% 
% Number of observations: 743, Error degrees of freedom: 719
% Root Mean Squared Error: 1.19
% R-squared: 0.158,  Adjusted R-Squared 0.131
% F-statistic vs. constant model: 5.88, p-value = 3.76e-16

%DIAGNOSTICS of CLRM assumptions
 
T = regmult2.NumObservations;
k = regmult2.NumEstimatedCoefficients;
u = regmult2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult2.Residuals.Raw);
Stdev_u = std(regmult2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.0903e-13
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9633
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test


ALL_MULT.uRaw = regmult2.Residuals.Raw; 
ALL_MULT.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult2White = table(ALL_MULT.mult_t, ALL_MULT.seq_num_t, ALL_MULT.size_t, ALL_MULT.mult_t1, ALL_MULT.mult_t2, ALL_MULT.vint91, ALL_MULT.vint92, ALL_MULT.vint93, ALL_MULT.vint94, ...
    ALL_MULT.vint95, ALL_MULT.vint96, ALL_MULT.vint97, ALL_MULT.vint98, ALL_MULT.vint99,ALL_MULT.vint00, ALL_MULT.vint01, ALL_MULT.vint02, ALL_MULT.vint03, ALL_MULT.vint04,...
    ALL_MULT.vint05, ALL_MULT.vint06, ALL_MULT.vint07, ALL_MULT.vint08, ALL_MULT.vint09, ALL_MULT.vint10, ALL_MULT.size_t.^2, ALL_MULT.mult_t1.^2, ALL_MULT.mult_t2.^2,  ALL_MULT.uRaw2);
 
mult2White.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'mult_t2','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq', 'mult_t2sq', 'uRaw2'};
 
% Unrestricted regression
regmult2White=fitlm(mult2White,'uRaw2 ~ seq_num_t + size_t + mult_t1 + mult_t2+ vint91 +vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq + mult_t2sq')
 
% regmult2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    __________    _________
% 
%     (Intercept)       5.2271      8.8072        0.5935      0.55304
%     seq_num_t      -0.041404     0.20846      -0.19862      0.84262
%     size_t           -1.9864        1.37       -1.4499      0.14752
%     mult_t1          0.80799     0.46239        1.7474     0.080997
%     mult_t2          0.96537     0.34044        2.8356    0.0047028
%     vint91                 0           0           NaN          NaN
%     vint92          -0.94986      11.096     -0.085602      0.93181
%     vint93           -2.0523       11.14      -0.18422      0.85389
%     vint94          -0.40487      11.109     -0.036446      0.97094
%     vint95           -2.5823      8.9352        -0.289      0.77266
%     vint96          -0.37827      8.9425     -0.042301      0.96627
%     vint97            4.4071      8.2317       0.53538      0.59256
%     vint98            3.9722      8.0999        0.4904        0.624
%     vint99           -1.5688       8.028      -0.19541      0.84513
%     vint00           -1.1876      7.9562      -0.14927      0.88138
%     vint01           0.65497      8.0193      0.081675      0.93493
%     vint02          -0.74352      8.1291     -0.091464      0.92715
%     vint03         -0.065624      8.0595    -0.0081425      0.99351
%     vint04           0.58838      8.0853      0.072772      0.94201
%     vint05           0.87628      7.9764       0.10986      0.91255
%     vint06             1.126      7.9604       0.14145      0.88755
%     vint07            0.6804       7.963      0.085444      0.93193
%     vint08            3.6204      7.9911       0.45305      0.65065
%     vint09           0.10889      8.0321      0.013556      0.98919
%     vint10           0.75283      8.0336       0.09371      0.92537
%     size_sq          0.10014     0.11509       0.87014      0.38452
%     mult_t1sq       0.039133    0.019011        2.0584      0.03991
%     mult_t2sq      -0.027421     0.01091       -2.5134     0.012176
% 
% 
% Number of observations: 743, Error degrees of freedom: 716
% Root Mean Squared Error: 11.1
% R-squared: 0.145,  Adjusted R-Squared 0.114
% F-statistic vs. constant model: 4.67, p-value = 5.3e-13
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  107.7808, Crit_Val_e =  35.1725, P_e = 6.2073e-13, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_MULT.seq_num_t ALL_MULT.size_t ALL_MULT.mult_t1 ALL_MULT.mult_t2...
      ALL_MULT.vint92  ALL_MULT.vint93 ALL_MULT.vint94 ALL_MULT.vint95  ALL_MULT.vint96  ALL_MULT.vint97...
    ALL_MULT.vint98 ALL_MULT.vint99 ALL_MULT.vint00 ALL_MULT.vint01 ALL_MULT.vint02...
    ALL_MULT.vint03 ALL_MULT.vint04 ALL_MULT.vint05 ALL_MULT.vint06 ALL_MULT.vint07 ALL_MULT.vint08...
    ALL_MULT.vint09 ALL_MULT.vint10];
 
y = ALL_MULT.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'mult_t2' ...
    'vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
% 
% White =
% 
%   24?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       1.1254     0.20439      5.5062    5.0588e-08
%     seq_num_t      -0.032092    0.013102     -2.4495      0.014538
%     size_t          0.019402    0.038888     0.49892       0.61798
%     mult_t1          0.12996    0.089993      1.4442       0.14912
%     mult_t2         0.011416    0.025446     0.44863       0.65383
%     vint92           0.10468     0.14525     0.72065       0.47135
%     vint93            1.0147     0.79769       1.272       0.20376
%     vint94            4.7001      0.8308      5.6573    2.1966e-08
%     vint95           0.60546      0.3973      1.5239       0.12795
%     vint96            1.0758     0.67183      1.6012       0.10975
%     vint97            1.4378     0.63937      2.2488      0.024821
%     vint98           0.28026     0.26225      1.0687       0.28556
%     vint99          -0.47664     0.18874     -2.5254      0.011765
%     vint00          -0.10413     0.11999    -0.86778        0.3858
%     vint01           0.13848     0.14032     0.98689       0.32402
%     vint02           0.11669     0.10297      1.1332       0.25749
%     vint03           0.19405     0.10869      1.7854       0.07461
%     vint04           0.25605     0.13311      1.9235      0.054797
%     vint05           0.16487     0.13046      1.2638       0.20671
%     vint06           0.23299     0.12923      1.8028      0.071819
%     vint07           0.38243     0.11382      3.3599    0.00081968
%     vint08           0.61268     0.30059      2.0383      0.041878
%     vint09           0.40918     0.11293      3.6233    0.00031073
%     vint10           0.43805     0.17581      2.4916      0.012935
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_MULT.seq_num_t,u)
corr_size_t_u = corr(ALL_MULT.size_t,u)
corr_mult_t1_u = corr(ALL_MULT.mult_t1,u)
corr_mult_t2_u = corr(ALL_MULT.mult_t2,u)

%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 6.1041
% kurt_jbtest = 73.8395
% test_stat_jbtest = 1.5997e+05
% crit_val_jbtest = 38.0756
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult2.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_MULT(regmult2.Diagnostics.CooksDistance >=0.05,:);
 
% Linear regression reg2mult w/ Buyout Dummy
 
regmult2 = fitlm(ALL_MULT, 'mult_t~ seq_num_t + BuyoutDummy + size_t + mult_t1 + mult_t2 + vint91+ vint92 + vint93  + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult2 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       1.2512     0.85781      1.4586       0.14512
%     BuyoutDummy      0.20808     0.10755      1.9347      0.053421
%     mult_t1          0.13092    0.021769      6.0142    2.8797e-09
%     mult_t2         0.014935    0.019046     0.78415       0.43321
%     seq_num_t      -0.024255    0.022546     -1.0758       0.28238
%     vint91                 0           0         NaN           NaN
%     vint92          0.069977      1.1917    0.058718       0.95319
%     vint93           0.83627       1.196     0.69921       0.48465
%     vint94            4.5623      1.1945      3.8196    0.00014527
%     vint95           0.47705     0.95897     0.49747       0.61901
%     vint96            1.0148     0.95728        1.06       0.28948
%     vint97            1.3835     0.88151      1.5695       0.11698
%     vint98           0.21873     0.86814     0.25195       0.80115
%     vint99          -0.52731     0.86177     -0.6119        0.5408
%     vint00          -0.14453     0.85407    -0.16922       0.86567
%     vint01           0.11103     0.86105     0.12895       0.89743
%     vint02          0.055712     0.87298    0.063817       0.94913
%     vint03           0.10783     0.86622     0.12449       0.90096
%     vint04           0.16583     0.86928     0.19077       0.84876
%     vint05           0.07083     0.85774    0.082577       0.93421
%     vint06           0.17545      0.8552     0.20516       0.83751
%     vint07           0.31791     0.85556     0.37158       0.71032
%     vint08           0.54367     0.85856     0.63324       0.52678
%     vint09           0.32209     0.86339     0.37305       0.70922
%     vint10           0.34479     0.86364     0.39923       0.68984
%     size_t         -0.016049    0.035234     -0.4555       0.64889
% 
% 
% Number of observations: 743, Error degrees of freedom: 718
% Root Mean Squared Error: 1.19
% R-squared: 0.163,  Adjusted R-Squared 0.135
% F-statistic vs. constant model: 5.82, p-value = 1.93e-16

%DIAGNOSTICS of CLRM assumptions
 
T = regmult2.NumObservations;
k = regmult2.NumEstimatedCoefficients;
u = regmult2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult2.Residuals.Raw);
Stdev_u = std(regmult2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 1.8738e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9633
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
ALL_MULT.uRaw = regmult2.Residuals.Raw; 
ALL_MULT.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult2White = table(ALL_MULT.mult_t, ALL_MULT.seq_num_t, ALL_MULT.BuyoutDummy, ALL_MULT.size_t, ALL_MULT.mult_t1, ALL_MULT.mult_t2,  ALL_MULT.vint91, ALL_MULT.vint92, ALL_MULT.vint93, ALL_MULT.vint94, ...
    ALL_MULT.vint95, ALL_MULT.vint96, ALL_MULT.vint97, ALL_MULT.vint98, ALL_MULT.vint99,ALL_MULT.vint00, ALL_MULT.vint01, ALL_MULT.vint02, ALL_MULT.vint03, ALL_MULT.vint04,...
    ALL_MULT.vint05, ALL_MULT.vint06, ALL_MULT.vint07, ALL_MULT.vint08, ALL_MULT.vint09, ALL_MULT.vint10, ALL_MULT.size_t.^2, ALL_MULT.mult_t1.^2, ALL_MULT.mult_t2.^2,  ALL_MULT.uRaw2);
 
mult2White.Properties.VariableNames={'mult_t','seq_num_t', 'BuyoutDummy','size_t','mult_t1', 'mult_t2', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq', 'mult_t2sq', 'uRaw2'};
 
% Unrestricted regression
regmult2White=fitlm(mult2White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + mult_t1 + mult_t2+vint91+vint92 +vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq + mult_t2sq')
%  
% regmult2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 29 terms in 28 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat       pValue  
%                    _________    ________    _________    _________
% 
%     (Intercept)       5.1561      8.8614      0.58186      0.56085
%     seq_num_t      -0.071596     0.21317     -0.33586      0.73707
%     BuyoutDummy     -0.77299      1.0247     -0.75438      0.45087
%     size_t           -2.0133      1.3781       -1.461      0.14445
%     mult_t1          0.85337     0.46777       1.8243     0.068523
%     mult_t2          0.96024     0.34249       2.8037    0.0051888
%     vint91                 0           0          NaN          NaN
%     vint92          -0.86146      11.162    -0.077176      0.93851
%     vint93           -1.4533      11.233     -0.12938       0.8971
%     vint94           0.32116      11.192     0.028695      0.97712
%     vint95            -2.103      9.0031     -0.23359      0.81537
%     vint96          -0.11969      8.9976    -0.013303      0.98939
%     vint97             4.685      8.2821      0.56568      0.57179
%     vint98            4.1869      8.1513      0.51365      0.60766
%     vint99           -1.4831      8.0779      -0.1836      0.85438
%     vint00            -1.114      8.0048     -0.13917      0.88936
%     vint01           0.71463      8.0674     0.088582      0.92944
%     vint02          -0.55574      8.1809    -0.067932      0.94586
%     vint03            0.2169      8.1163     0.026725      0.97869
%     vint04            0.9078      8.1435      0.11147      0.91127
%     vint05            1.2137       8.035      0.15105      0.87998
%     vint06            1.3259       8.011      0.16551      0.86859
%     vint07            0.8983      8.0146      0.11208      0.91079
%     vint08            3.8623      8.0435      0.48018      0.63125
%     vint09           0.40524      8.0886       0.0501      0.96006
%     vint10            1.0298      8.0923      0.12725      0.89878
%     size_sq          0.11313     0.11665      0.96981      0.33247
%     mult_t1sq       0.036731    0.019287       1.9044     0.057255
%     mult_t2sq      -0.027698    0.010985      -2.5214     0.011907
% 
% 
% Number of observations: 743, Error degrees of freedom: 715
% Root Mean Squared Error: 11.1
% R-squared: 0.145,  Adjusted R-Squared 0.112
% F-statistic vs. constant model: 4.48, p-value = 1.35e-12
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  107.4597, Crit_Val_e =  36.4150, P_e = 1.5620e-12, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_MULT.seq_num_t ALL_MULT.size_t ALL_MULT.mult_t1 ALL_MULT.mult_t2 ALL_MULT.BuyoutDummy...
     ALL_MULT.vint92 ALL_MULT.vint93 ALL_MULT.vint94 ALL_MULT.vint95 ALL_MULT.vint96 ALL_MULT.vint97...
    ALL_MULT.vint98 ALL_MULT.vint99 ALL_MULT.vint00 ALL_MULT.vint01 ALL_MULT.vint02...
    ALL_MULT.vint03 ALL_MULT.vint04 ALL_MULT.vint05 ALL_MULT.vint06 ALL_MULT.vint07 ALL_MULT.vint08...
    ALL_MULT.vint09 ALL_MULT.vint10];
 
y = ALL_MULT.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'mult_t2', 'BuyoutDummy',...
    'vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08',...
    'vint09','vint10'};
White
% % 
% White =
% 
%   25?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       1.2512     0.21902      5.7127    1.6109e-08
%     seq_num_t      -0.024255    0.013566      -1.788      0.074188
%     size_t         -0.016049    0.041982    -0.38228       0.70236
%     mult_t1          0.13092    0.089671        1.46        0.1447
%     mult_t2         0.014935    0.025459      0.5866       0.55765
%     BuyoutDummy      0.20808    0.080067      2.5988     0.0095414
%     vint92          0.069977     0.13699      0.5108       0.60964
%     vint93           0.83627     0.79199      1.0559       0.29135
%     vint94            4.5623     0.91166      5.0045    7.0038e-07
%     vint95           0.47705     0.40657      1.1734       0.24102
%     vint96            1.0148     0.68238      1.4871       0.13742
%     vint97            1.3835     0.64254      2.1532      0.031625
%     vint98           0.21873     0.26681     0.81981       0.41259
%     vint99          -0.52731     0.17763     -2.9686      0.003088
%     vint00          -0.14453     0.11047     -1.3083       0.19119
%     vint01           0.11103     0.13617     0.81541        0.4151
%     vint02          0.055712     0.10318     0.53995       0.58939
%     vint03           0.10783     0.10716      1.0062       0.31462
%     vint04           0.16583     0.13792      1.2023       0.22962
%     vint05           0.07083     0.13201     0.53654       0.59175
%     vint06           0.17545     0.13267      1.3225       0.18641
%     vint07           0.31791      0.1163      2.7335     0.0064165
%     vint08           0.54367     0.29975      1.8138      0.070119
%     vint09           0.32209     0.11282      2.8549     0.0044252
%     vint10           0.34479     0.16696      2.0652      0.039256
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_MULT.seq_num_t,u)
corr_size_t_u = corr(ALL_MULT.size_t,u)
corr_mult_t1_u = corr(ALL_MULT.mult_t1,u)
corr_mult_t2_u = corr(ALL_MULT.mult_t2,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 6.2043
% kurt_jbtest = 75.3281
% test_stat_jbtest = 1.6672e+05
% crit_val_jbtest = 39.3641
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult2.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_MULT(regmult2.Diagnostics.CooksDistance >=0.05,:);

% Linear regression reg2mult w/ Buyout Dummy*mult

ALL_MULT.interact1=ALL_MULT.BuyoutDummy.*ALL_MULT.mult_t1
ALL_MULT.interact2=ALL_MULT.BuyoutDummy.*ALL_MULT.mult_t2

 
regmult2 = fitlm(ALL_MULT, 'mult_t~ seq_num_t + BuyoutDummy + interact1 + interact2 + size_t + mult_t1 + mult_t2+ vint91 + vint92 +vint93+  vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult2 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       1.2431     0.85844      1.4481       0.14801
%     BuyoutDummy     0.047858     0.21863      0.2189       0.82679
%     mult_t1          0.13134    0.022687       5.789    1.0601e-08
%     mult_t2         0.010156    0.019662     0.51656       0.60563
%     seq_num_t      -0.023566      0.0226     -1.0428       0.29741
%     vint91                 0           0         NaN           NaN
%     vint92          0.070909      1.1925    0.059461        0.9526
%     vint93           0.77528      1.2036     0.64412        0.5197
%     vint94            4.5213      1.1962      3.7796    0.00017018
%     vint95           0.49753     0.96014     0.51818       0.60449
%     vint96            1.0042     0.95797      1.0483       0.29487
%     vint97            1.3796     0.88211       1.564       0.11825
%     vint98           0.21003     0.86877     0.24176       0.80903
%     vint99          -0.52889     0.86267    -0.61308       0.54002
%     vint00          -0.14538     0.85465     -0.1701       0.86498
%     vint01            0.1196     0.86166      0.1388       0.88965
%     vint02          0.065241     0.87361     0.07468       0.94049
%     vint03           0.10276     0.86681     0.11855       0.90566
%     vint04           0.17687     0.86992     0.20331       0.83895
%     vint05          0.092627     0.85871     0.10787       0.91413
%     vint06           0.18562     0.85588     0.21688       0.82837
%     vint07           0.32934      0.8562     0.38465       0.70061
%     vint08            0.5465     0.85913      0.6361       0.52491
%     vint09           0.32527     0.86401     0.37647       0.70668
%     vint10           0.35033     0.86423     0.40537       0.68533
%     size_t         -0.013828    0.035361    -0.39105       0.69587
%     interact1       0.005279    0.081573    0.064715       0.94842
%     interact2       0.068783    0.070083     0.98145        0.3267
% 
% 
% Number of observations: 743, Error degrees of freedom: 716
% Root Mean Squared Error: 1.19
% R-squared: 0.164,  Adjusted R-Squared 0.134
% F-statistic vs. constant model: 5.4, p-value = 7.38e-16

%DIAGNOSTICS of CLRM assumptions
 
T = regmult2.NumObservations;
k = regmult2.NumEstimatedCoefficients;
u = regmult2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult2.Residuals.Raw);
Stdev_u = std(regmult2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.6155e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9633
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
ALL_MULT.uRaw = regmult2.Residuals.Raw; 
ALL_MULT.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult2White = table(ALL_MULT.mult_t, ALL_MULT.seq_num_t, ALL_MULT.BuyoutDummy, ALL_MULT.interact1, ALL_MULT.interact2, ALL_MULT.size_t, ALL_MULT.mult_t1, ALL_MULT.mult_t2, ALL_MULT.vint91, ALL_MULT.vint92, ALL_MULT.vint93, ALL_MULT.vint94, ...
    ALL_MULT.vint95, ALL_MULT.vint96, ALL_MULT.vint97, ALL_MULT.vint98, ALL_MULT.vint99,ALL_MULT.vint00, ALL_MULT.vint01, ALL_MULT.vint02, ALL_MULT.vint03, ALL_MULT.vint04,...
    ALL_MULT.vint05, ALL_MULT.vint06, ALL_MULT.vint07, ALL_MULT.vint08, ALL_MULT.vint09, ALL_MULT.vint10, ALL_MULT.size_t.^2, ALL_MULT.mult_t1.^2, ALL_MULT.mult_t2.^2,  ALL_MULT.uRaw2);
 
mult2White.Properties.VariableNames={'mult_t','seq_num_t', 'BuyoutDummy', 'interact1', 'interact2', 'size_t','mult_t1', 'mult_t2','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq', 'mult_t2sq', 'uRaw2'};
 
% Unrestricted regression
regmult2White=fitlm(mult2White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact1+ interact2 + mult_t1 + mult_t2+ vint91 + vint92 +vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq + mult_t2sq')
%  regmult2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 31 terms in 30 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat       pValue  
%                    _________    ________    _________    _________
% 
%     (Intercept)       5.1416      8.8624      0.58016      0.56199
%     seq_num_t      -0.093185     0.21337     -0.43673      0.66244
%     BuyoutDummy       4.2338      2.1522       1.9672      0.04955
%     interact1        -1.9648     0.85213      -2.3058     0.021409
%     interact2       -0.70193     0.69794      -1.0057      0.31489
%     size_t           -2.2319      1.3804      -1.6168      0.10636
%     mult_t1           1.5136     0.54253         2.79    0.0054125
%     mult_t2           1.1571     0.37584       3.0787    0.0021589
%     vint91                 0           0          NaN          NaN
%     vint92            -1.076      11.163    -0.096393      0.92324
%     vint93            1.0438      11.274     0.092583      0.92626
%     vint94           0.18034      11.199     0.016102      0.98716
%     vint95           -2.7301      9.0117     -0.30295      0.76201
%     vint96          -0.96658      9.0034     -0.10736      0.91454
%     vint97            3.9802      8.2868      0.48031      0.63115
%     vint98            3.6427      8.1537      0.44676      0.65519
%     vint99           -2.4679      8.0861     -0.30521       0.7603
%     vint00           -1.4053      8.0059     -0.17554      0.86071
%     vint01           0.51028      8.0693     0.063237       0.9496
%     vint02          -0.91841      8.1835     -0.11223      0.91067
%     vint03           0.05523      8.1167    0.0068044      0.99457
%     vint04           0.58525      8.1459     0.071846      0.94274
%     vint05           0.95166      8.0399      0.11837      0.90581
%     vint06            1.1569      8.0129      0.14438      0.88524
%     vint07           0.50829      8.0168     0.063403      0.94946
%     vint08            3.5622      8.0447       0.4428      0.65805
%     vint09          -0.09253      8.0908    -0.011437      0.99088
%     vint10           0.68728      8.0938     0.084914      0.93235
%     size_sq          0.12417     0.11673       1.0638      0.28779
%     mult_t1sq       0.012614    0.021567      0.58489      0.55881
%     mult_t2sq      -0.034087    0.011703      -2.9126    0.0036968
% 
% 
% Number of observations: 743, Error degrees of freedom: 713
% Root Mean Squared Error: 11.1
% R-squared: 0.154,  Adjusted R-Squared 0.119
% F-statistic vs. constant model: 4.46, p-value = 3.22e-13
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  114.1017, Crit_Val_e =  38.8851, P_e = 5.2247e-13, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_MULT.seq_num_t ALL_MULT.size_t ALL_MULT.mult_t1 ALL_MULT.mult_t2 ALL_MULT.BuyoutDummy ALL_MULT.interact1 ALL_MULT.interact2...
    ALL_MULT.vint91 ALL_MULT.vint92  ALL_MULT.vint94 ALL_MULT.vint95 ALL_MULT.vint96 ALL_MULT.vint97...
    ALL_MULT.vint98 ALL_MULT.vint99 ALL_MULT.vint00 ALL_MULT.vint01 ALL_MULT.vint02...
    ALL_MULT.vint03 ALL_MULT.vint04 ALL_MULT.vint05 ALL_MULT.vint06 ALL_MULT.vint07 ALL_MULT.vint08...
    ALL_MULT.vint09 ALL_MULT.vint10];
 
y = ALL_MULT.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'mult_t2', 'BuyoutDummy', 'interact1', 'interact2',...
    'vint91','vint92','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07',...
    'vint08','vint09','vint10'};
White
% % 
% White =
% 
%   27?4 table
% 
%                      beta          SE        tstat         p    
%                    _________    ________    ________    ________
% 
%     (Intercept)       2.0184     0.79379      2.5428      0.0112
%     seq_num_t      -0.023566    0.013312     -1.7703    0.077087
%     size_t         -0.013828    0.042434    -0.32587     0.74461
%     mult_t1          0.13134    0.097344      1.3492     0.17769
%     mult_t2         0.010156    0.025298     0.40147     0.68819
%     BuyoutDummy     0.047858     0.21433     0.22329     0.82337
%     interact1       0.005279     0.11373    0.046415     0.96299
%     interact2       0.068783    0.061958      1.1102     0.26729
%     vint91          -0.77528     0.75014     -1.0335      0.3017
%     vint92          -0.70437      0.7589    -0.92815     0.35363
%     vint94             3.746      1.4114       2.654    0.008124
%     vint95          -0.27776      0.8309    -0.33428     0.73826
%     vint96           0.22894     0.96036     0.23839     0.81165
%     vint97           0.60435     0.98792     0.61174     0.54089
%     vint98          -0.56525     0.80527    -0.70194     0.48294
%     vint99           -1.3042      0.7349     -1.7746    0.076372
%     vint00          -0.92066     0.75094      -1.226     0.22058
%     vint01          -0.65569     0.74319    -0.88226     0.37792
%     vint02          -0.71004     0.75538    -0.93998     0.34754
%     vint03          -0.67252      0.7583    -0.88687     0.37543
%     vint04          -0.59842     0.75841    -0.78904     0.43034
%     vint05          -0.68266     0.75199     -0.9078     0.36428
%     vint06          -0.58966     0.75003    -0.78619     0.43201
%     vint07          -0.44594     0.75498    -0.59067     0.55492
%     vint08          -0.22879     0.78249    -0.29238     0.77008
%     vint09          -0.45001     0.75901    -0.59289     0.55344
%     vint10          -0.42495     0.75418    -0.56346     0.57329    

%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_MULT.seq_num_t,u)
corr_size_t_u = corr(ALL_MULT.size_t,u)
corr_mult_t1_u = corr(ALL_MULT.mult_t1,u)
corr_mult_t2_u = corr(ALL_MULT.mult_t2,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 6.2473
% kurt_jbtest = 76.1259
% test_stat_jbtest = 1.7038e+05
% crit_val_jbtest = 41.9232
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult2.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_MULT(regmult2.Diagnostics.CooksDistance >=0.05,:);
 

%% Section 5.1 all funds irr t-1 - t
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','All funds for t-1 - t, irr','A2:AS1144');
stringVectors = string(raw(:,[4,5,6,8,12,13,14,15,16,17,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,18,19,20,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]);
% Create output variable
data = reshape([raw{:}],size(raw));
% Create table
ALL_IRR = table;
% Allocate imported array to column variable names
ALL_IRR.number = data(:,1);
ALL_IRR.FUNDID = data(:,2);
ALL_IRR.FIRMID = data(:,3);
ALL_IRR.teamID = stringVectors(:,1);
ALL_IRR.teamIDvintage = stringVectors(:,2);
ALL_IRR.teamIDvintagenumber = stringVectors(:,3);
ALL_IRR.VINTAGEYEAR = data(:,4);
ALL_IRR.STRATEGY = categorical(stringVectors(:,4));
ALL_IRR.BuyoutDummy = data(:,5);
ALL_IRR.irr_t = data(:,6);
ALL_IRR.irr_t1 = data(:,7);
ALL_IRR.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
ALL_IRR.COUNTRIES = stringVectors(:,6);
ALL_IRR.REGIONS = stringVectors(:,7);
ALL_IRR.STATUS = categorical(stringVectors(:,8));
ALL_IRR.FUNDMANAGER = stringVectors(:,9);
ALL_IRR.DOMICILE = categorical(stringVectors(:,10));
ALL_IRR.FINALCLOSESIZE = data(:,8);
ALL_IRR.size_ = data(:,9);
ALL_IRR.seq_num_t = data(:,10);
ALL_IRR.COREINDUSTRIES = categorical(stringVectors(:,11));
ALL_IRR.INDUSTRIES = stringVectors(:,12);
ALL_IRR.MANAGERREGION = categorical(stringVectors(:,13));
ALL_IRR.MANAGERCOUNTRY = categorical(stringVectors(:,14));
ALL_IRR.vint90 = data(:,11);
ALL_IRR.vint91 = data(:,12);
ALL_IRR.vint92 = data(:,13);
ALL_IRR.vint93 = data(:,14);
ALL_IRR.vint94 = data(:,15);
ALL_IRR.vint95 = data(:,16);
ALL_IRR.vint96 = data(:,17);
ALL_IRR.vint97 = data(:,18);
ALL_IRR.vint98 = data(:,19);
ALL_IRR.vint99 = data(:,20);
ALL_IRR.vint00 = data(:,21);
ALL_IRR.vint01 = data(:,22);
ALL_IRR.vint02 = data(:,23);
ALL_IRR.vint03 = data(:,24);
ALL_IRR.vint04 = data(:,25);
ALL_IRR.vint05 = data(:,26);
ALL_IRR.vint06 = data(:,27);
ALL_IRR.vint07 = data(:,28);
ALL_IRR.vint08 = data(:,29);
ALL_IRR.vint09 = data(:,30);
ALL_IRR.vint10 = data(:,31);
 
% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
ALL_IRR.size_t=log(ALL_IRR.size_);
 
% Linear regression reg1irr w/o Buyout Dummy
 
regirr1 = fitlm(ALL_IRR, 'irr_t~seq_num_t + size_t + irr_t1 +  vint91 + vint92 + vint93  + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
 
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat        pValue  
%                    ________    ________    _________    __________
% 
%     (Intercept)      26.549      27.432      0.96779       0.33336
%     irr_t1          0.13958    0.024391       5.7224    1.3474e-08
%     seq_num_t      -0.40531      0.4923      -0.8233       0.41051
%     vint91           1.0175      33.458     0.030412       0.97574
%     vint92          -12.882      29.206     -0.44108       0.65924
%     vint93           10.741      30.547      0.35162       0.72519
%     vint94           10.622       28.07      0.37843       0.70519
%     vint95           11.517      27.849      0.41354       0.67929
%     vint96           3.7874      27.824      0.13612       0.89175
%     vint97         -0.73468      27.563    -0.026655       0.97874
%     vint98          -12.725      27.515     -0.46248       0.64382
%     vint99          -25.663      27.504     -0.93307       0.35098
%     vint00          -19.184      27.469     -0.69839       0.48508
%     vint01           -12.35      27.549     -0.44828       0.65404
%     vint02          -13.562      27.614     -0.49114       0.62342
%     vint03          -13.527      27.623     -0.48971       0.62444
%     vint04           -16.26      27.599     -0.58917       0.55587
%     vint05           -14.96      27.474     -0.54453       0.58618
%     vint06          -18.263      27.476     -0.66469       0.50638
%     vint07          -15.184      27.469     -0.55279       0.58052
%     vint08          -14.154      27.482     -0.51505       0.60662
%     vint09          -9.1519      27.663     -0.33083       0.74083
%     vint10          -11.883      27.571       -0.431       0.66655
%     size_t         -0.16657     0.59187     -0.28143       0.77843
% 
% 
% Number of observations: 1143, Error degrees of freedom: 1119
% Root Mean Squared Error: 27.3
% R-squared: 0.111,  Adjusted R-Squared 0.0923
% F-statistic vs. constant model: 6.05, p-value = 2.32e-17
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -3.7233e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9621
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
ALL_IRR.uRaw = regirr1.Residuals.Raw; 
ALL_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(ALL_IRR.irr_t, ALL_IRR.seq_num_t, ALL_IRR.size_t, ALL_IRR.irr_t1, ALL_IRR.vint91, ALL_IRR.vint92, ALL_IRR.vint93, ALL_IRR.vint94, ...
    ALL_IRR.vint95, ALL_IRR.vint96, ALL_IRR.vint97, ALL_IRR.vint98, ALL_IRR.vint99,ALL_IRR.vint00, ALL_IRR.vint01, ALL_IRR.vint02, ALL_IRR.vint03, ALL_IRR.vint04,...
    ALL_IRR.vint05, ALL_IRR.vint06, ALL_IRR.vint07, ALL_IRR.vint08, ALL_IRR.vint09, ALL_IRR.vint10, ALL_IRR.size_t.^2, ALL_IRR.irr_t1.^2, ALL_IRR.uRaw2);
 
mult1White.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + irr_t1 + vint91 +vint92+vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
% % 
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue 
%                    ________    ________    ________    ________
% 
%     (Intercept)      1509.3      7462.7     0.20225     0.83976
%     seq_num_t        38.374      130.57     0.29391     0.76888
%     size_t          -688.97      785.84    -0.87673     0.38082
%     irr_t1           25.754        10.8      2.3846    0.017263
%     vint91           516.41      8747.8    0.059034     0.95294
%     vint92           256.28      7635.6    0.033563     0.97323
%     vint93           646.34      7987.1    0.080923     0.93552
%     vint94           1477.7      7339.4     0.20134     0.84047
%     vint95           2198.1        7281      0.3019     0.76279
%     vint96           2135.8      7275.1     0.29358     0.76913
%     vint97           2122.5      7206.3     0.29453     0.76841
%     vint98           4861.6      7193.8      0.6758     0.49931
%     vint99           764.08      7191.3     0.10625      0.9154
%     vint00           758.27      7183.7     0.10555     0.91596
%     vint01           1368.3      7204.8     0.18992     0.84941
%     vint02           1010.1      7220.5     0.13989     0.88877
%     vint03           1035.9      7222.7     0.14342     0.88599
%     vint04           956.31      7216.2     0.13252     0.89459
%     vint05           1079.1      7183.4     0.15023     0.88061
%     vint06           1010.7      7184.1     0.14069     0.88814
%     vint07           938.83      7182.3     0.13071     0.89603
%     vint08           1208.8      7186.6      0.1682     0.86646
%     vint09           1037.8      7233.5     0.14347     0.88595
%     vint10           985.13      7209.3     0.13665     0.89133
%     size_sq           33.84      67.998     0.49766     0.61882
%     irr_t1sq       0.032658    0.033207     0.98347     0.32559
% 
% 
% Number of observations: 1143, Error degrees of freedom: 1117
% Root Mean Squared Error: 7.14e+03
% R-squared: 0.0551,  Adjusted R-Squared 0.034
% F-statistic vs. constant model: 2.61, p-value = 2.87e-05 

% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  63.0234, Crit_Val_e =  35.1725, P_e = 1.3805e-05, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_IRR.seq_num_t ALL_IRR.size_t ALL_IRR.irr_t1 ...
    ALL_IRR.vint91 ALL_IRR.vint92 ALL_IRR.vint93 ALL_IRR.vint94 ALL_IRR.vint95 ALL_IRR.vint96 ALL_IRR.vint97...
    ALL_IRR.vint98 ALL_IRR.vint99 ALL_IRR.vint00 ALL_IRR.vint01 ALL_IRR.vint02...
    ALL_IRR.vint03 ALL_IRR.vint04 ALL_IRR.vint05 ALL_IRR.vint06 ALL_IRR.vint07 ALL_IRR.vint08...
    ALL_IRR.vint09 ALL_IRR.vint10];
 
y = ALL_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', ...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White
% White =
% 
%   24?4 table
% 
%                      beta         SE        tstat          p     
%                    ________    ________    ________    __________
% 
%     (Intercept)      26.549       2.679      9.9101             0
%     seq_num_t      -0.40531     0.31454     -1.2886       0.19781
%     size_t         -0.16657     0.54536    -0.30543       0.76009
%     irr_t1          0.13958    0.049261      2.8334      0.004686
%     vint91           1.0175      14.972    0.067962       0.94583
%     vint92          -12.882      2.3556     -5.4688    5.5648e-08
%     vint93           10.741      14.286     0.75186       0.45229
%     vint94           10.622      7.6813      1.3829       0.16696
%     vint95           11.517      9.1251      1.2621       0.20718
%     vint96           3.7874      8.6578     0.43746       0.66186
%     vint97         -0.73468      6.9504     -0.1057       0.91584
%     vint98          -12.725      7.8682     -1.6173       0.10608
%     vint99          -25.663      3.1811     -8.0673    1.7764e-15
%     vint00          -19.184      2.1673     -8.8516             0
%     vint01           -12.35      2.7958     -4.4172    1.0948e-05
%     vint02          -13.562      2.6302     -5.1564     2.966e-07
%     vint03          -13.527      2.4687     -5.4795    5.2459e-08
%     vint04           -16.26      2.4963     -6.5138    1.0965e-10
%     vint05           -14.96      2.1263     -7.0358    3.4097e-12
%     vint06          -18.263       1.606     -11.372             0
%     vint07          -15.184      1.5219     -9.9775             0
%     vint08          -14.154      1.7214     -8.2228    4.4409e-16
%     vint09          -9.1519      2.2505     -4.0665    5.0995e-05
%     vint10          -11.883      1.9257     -6.1709    9.4179e-10
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_IRR.seq_num_t,u)
corr_size_t_u = corr(ALL_IRR.size_t,u)
corr_irr_t1_u = corr(ALL_IRR.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 6.7359
% kurt_jbtest = 99.6824
% test_stat_jbtest = 4.5382e+05
% crit_val_jbtest = 38.0756
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
  
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_IRR(regirr1.Diagnostics.CooksDistance >=0.05,:);

% Linear regression reg1irr w/ Buyout Dummy
 
regirr1 = fitlm(ALL_IRR, 'irr_t~seq_num_t + BuyoutDummy + size_t + irr_t1 +  vint91 + vint92 + vint93 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
 
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat        pValue  
%                    ________    ________    _________    __________
% 
%     (Intercept)      28.794       27.43       1.0497       0.29406
%     BuyoutDummy       3.558      1.9267       1.8467       0.06506
%     irr_t1          0.13822    0.024376       5.6701    1.8161e-08
%     seq_num_t      -0.23085     0.50076     -0.46099        0.6449
%     vint91         -0.61281      33.434    -0.018329       0.98538
%     vint92           -13.74      29.178     -0.47089       0.63781
%     vint93           8.4961      30.538      0.27821        0.7809
%     vint94           9.8164      28.043      0.35004       0.72637
%     vint95           10.042       27.83      0.36083        0.7183
%     vint96           2.6364      27.801     0.094832       0.92447
%     vint97          -1.8102       27.54     -0.06573        0.9476
%     vint98          -13.655       27.49     -0.49673       0.61947
%     vint99          -26.324      27.477     -0.95805       0.33825
%     vint00          -19.837      27.442     -0.72287       0.46991
%     vint01          -13.006      27.521     -0.47259        0.6366
%     vint02           -14.83      27.593     -0.53745       0.59106
%     vint03          -15.059      27.606      -0.5455       0.58552
%     vint04          -17.654       27.58      -0.6401       0.52224
%     vint05           -16.65       27.46     -0.60634       0.54441
%     vint06          -19.312      27.452     -0.70349        0.4819
%     vint07          -16.331      27.446     -0.59502       0.55195
%     vint08          -15.121      27.457     -0.55071       0.58194
%     vint09          -10.845      27.649     -0.39223       0.69496
%     vint10          -13.427      27.554      -0.4873       0.62614
%     size_t         -0.79205     0.68138      -1.1624       0.24532
% 
% 
% Number of observations: 1143, Error degrees of freedom: 1118
% Root Mean Squared Error: 27.3
% R-squared: 0.113,  Adjusted R-Squared 0.0943
% F-statistic vs. constant model: 5.95, p-value = 1.38e-17
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 3.0556e-16
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9621
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
ALL_IRR.uRaw = regirr1.Residuals.Raw; 
ALL_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(ALL_IRR.irr_t, ALL_IRR.seq_num_t, ALL_IRR.BuyoutDummy, ALL_IRR.size_t, ALL_IRR.irr_t1, ALL_IRR.vint91, ALL_IRR.vint92, ALL_IRR.vint93, ALL_IRR.vint94, ...
    ALL_IRR.vint95, ALL_IRR.vint96, ALL_IRR.vint97, ALL_IRR.vint98, ALL_IRR.vint99,ALL_IRR.vint00, ALL_IRR.vint01, ALL_IRR.vint02, ALL_IRR.vint03, ALL_IRR.vint04,...
    ALL_IRR.vint05, ALL_IRR.vint06, ALL_IRR.vint07, ALL_IRR.vint08, ALL_IRR.vint09, ALL_IRR.vint10, ALL_IRR.size_t.^2, ALL_IRR.irr_t1.^2, ALL_IRR.uRaw2);
 
mult1White.Properties.VariableNames={'irr_t','seq_num_t','BuyoutDummy', 'size_t','irr_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + irr_t1 + vint91 +vint92+vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
 
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 27 terms in 26 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat        pValue  
%                    ________    ________    __________    _________
% 
%     (Intercept)      1184.7      7513.1       0.15768      0.87474
%     seq_num_t      -0.58923      133.75    -0.0044055      0.99649
%     BuyoutDummy     -809.03      513.43       -1.5757      0.11537
%     size_t          -636.58      791.56      -0.80421      0.42145
%     irr_t1           28.408      10.955        2.5931    0.0096368
%     vint91           838.66      8807.1      0.095225      0.92415
%     vint92           461.36      7685.5       0.06003      0.95214
%     vint93           1123.6      8044.7       0.13967      0.88894
%     vint94           1709.5      7387.5        0.2314      0.81705
%     vint95           2613.4      7330.8       0.35649      0.72154
%     vint96           2406.6      7323.5       0.32862      0.74251
%     vint97           2445.1      7254.3       0.33705      0.73614
%     vint98           5110.7      7241.3       0.70577      0.48048
%     vint99           915.65      7238.2        0.1265      0.89936
%     vint00           927.61      7230.7       0.12829      0.89794
%     vint01           1543.6        7252       0.21285      0.83148
%     vint02           1316.9      7269.6       0.18115      0.85628
%     vint03           1398.6      7272.9       0.19231      0.84754
%     vint04           1286.1      7265.7       0.17701      0.85954
%     vint05           1485.4        7234       0.20534      0.83735
%     vint06           1277.5        7232       0.17665      0.85982
%     vint07           1235.8      7230.6       0.17092      0.86432
%     vint08           1474.4      7234.4        0.2038      0.83855
%     vint09           1460.1      7284.7       0.20044      0.84117
%     vint10           1376.3      7259.6       0.18959      0.84967
%     size_sq          41.434      68.603       0.60396      0.54599
%     irr_t1sq        0.02361    0.033727       0.70002      0.48406
% 
% 
% Number of observations: 1143, Error degrees of freedom: 1116
% Root Mean Squared Error: 7.19e+03
% R-squared: 0.057,  Adjusted R-Squared 0.035
% F-statistic vs. constant model: 2.6, p-value = 2.33e-05

% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  65.1636, Crit_Val_e =  36.4150, P_e = 1.1518e-05, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_IRR.seq_num_t ALL_IRR.size_t ALL_IRR.irr_t1 ALL_IRR.BuyoutDummy...
    ALL_IRR.vint91 ALL_IRR.vint92 ALL_IRR.vint93 ALL_IRR.vint94 ALL_IRR.vint95 ALL_IRR.vint96 ALL_IRR.vint97...
    ALL_IRR.vint98 ALL_IRR.vint99 ALL_IRR.vint00 ALL_IRR.vint01 ALL_IRR.vint02...
    ALL_IRR.vint03 ALL_IRR.vint04 ALL_IRR.vint05 ALL_IRR.vint06 ALL_IRR.vint07 ALL_IRR.vint08...
    ALL_IRR.vint09 ALL_IRR.vint10];
 
y = ALL_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'BuyoutDummy', ...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White
% White =
% 
%   25?4 table
% 
%                      beta         SE         tstat          p     
%                    ________    ________    _________    __________
% 
%     (Intercept)      28.794      3.2494       8.8615             0
%     seq_num_t      -0.23085     0.32987     -0.69982       0.48418
%     size_t         -0.79205     0.69016      -1.1476       0.25137
%     irr_t1          0.13822    0.049205        2.809     0.0050547
%     BuyoutDummy       3.558       1.977       1.7997      0.072177
%     vint91         -0.61281      14.008    -0.043747       0.96511
%     vint92           -13.74      2.4438      -5.6221    2.3705e-08
%     vint93           8.4961      14.206      0.59805       0.54992
%     vint94           9.8164      7.7901       1.2601       0.20789
%     vint95           10.042      9.5967       1.0464        0.2956
%     vint96           2.6364       8.864      0.29743       0.76619
%     vint97          -1.8102      7.3229     -0.24719        0.8048
%     vint98          -13.655      7.9276      -1.7225      0.085248
%     vint99          -26.324      3.0256      -8.7003             0
%     vint00          -19.837      1.9921      -9.9579             0
%     vint01          -13.006      2.6893      -4.8364    1.5028e-06
%     vint02           -14.83       2.651      -5.5939    2.7773e-08
%     vint03          -15.059      2.5236      -5.9671    3.2189e-09
%     vint04          -17.654      2.3395      -7.5459    9.1482e-14
%     vint05           -16.65      2.1505      -7.7423    2.1316e-14
%     vint06          -19.312      1.6677       -11.58             0
%     vint07          -16.331      1.6152      -10.111             0
%     vint08          -15.121      1.8846      -8.0233    2.4425e-15
%     vint09          -10.845      2.4901      -4.3551    1.4497e-05
%     vint10          -13.427      2.1895      -6.1322    1.1923e-09
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_IRR.seq_num_t,u)
corr_size_t_u = corr(ALL_IRR.size_t,u)
corr_irr_t1_u = corr(ALL_IRR.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 6.8459
% kurt_jbtest = 101.6703
% test_stat_jbtest = 4.7260e+05
% crit_val_jbtest = 39.3641
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
  
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_IRR(regirr1.Diagnostics.CooksDistance >=0.05,:);
  
% Linear regression reg1mult w/ Buyout Dummy*irr_t1
 
ALL_IRR.interact=ALL_IRR.BuyoutDummy.*ALL_IRR.irr_t1
 
regirr1 = fitlm(ALL_IRR, 'irr_t~seq_num_t + BuyoutDummy + interact+ size_t + irr_t1 + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)       28.661      27.427        1.045       0.29625
%     BuyoutDummy       4.9129      2.2671       2.1671      0.030441
%     irr_t1           0.15111    0.026896       5.6183    2.4336e-08
%     seq_num_t       -0.24246     0.50081     -0.48415       0.62838
%     vint91           0.58268      33.446     0.017421        0.9861
%     vint92            -13.25      29.178      -0.4541       0.64984
%     vint93            10.178       30.57      0.33295       0.73923
%     vint94            10.042      28.041      0.35814       0.72031
%     vint95             10.42      27.829      0.37444       0.70815
%     vint96             2.793      27.798      0.10047       0.91999
%     vint97           -1.4919      27.537    -0.054176        0.9568
%     vint98           -13.579      27.487       -0.494        0.6214
%     vint99            -26.53      27.474     -0.96564       0.33444
%     vint00           -19.723      27.439     -0.71882        0.4724
%     vint01           -12.874      27.518     -0.46784       0.63999
%     vint02           -14.778      27.589     -0.53563       0.59232
%     vint03           -14.904      27.602     -0.53994       0.58935
%     vint04           -17.339      27.578     -0.62875       0.52964
%     vint05           -16.448      27.457     -0.59904       0.54927
%     vint06           -18.957      27.451     -0.69059       0.48997
%     vint07           -16.029      27.444     -0.58405        0.5593
%     vint08           -14.968      27.454     -0.54521       0.58572
%     vint09           -10.949      27.645     -0.39607       0.69213
%     vint10           -13.396       27.55     -0.48625       0.62688
%     size_t          -0.83276     0.68224      -1.2206       0.22248
%     interact       -0.069153    0.060999      -1.1337       0.25718
% 
% 
% Number of observations: 1143, Error degrees of freedom: 1117
% Root Mean Squared Error: 27.3
% R-squared: 0.114,  Adjusted R-Squared 0.0945
% F-statistic vs. constant model: 5.77, p-value = 1.99e-17 

%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 1.1649e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9621
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
ALL_IRR.uRaw = regirr1.Residuals.Raw; 
ALL_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(ALL_IRR.irr_t, ALL_IRR.seq_num_t, ALL_IRR.BuyoutDummy, ALL_IRR.interact, ALL_IRR.size_t, ALL_IRR.irr_t1, ALL_IRR.vint91, ALL_IRR.vint92, ALL_IRR.vint93, ALL_IRR.vint94, ...
    ALL_IRR.vint95, ALL_IRR.vint96, ALL_IRR.vint97, ALL_IRR.vint98, ALL_IRR.vint99,ALL_IRR.vint00, ALL_IRR.vint01, ALL_IRR.vint02, ALL_IRR.vint03, ALL_IRR.vint04,...
    ALL_IRR.vint05, ALL_IRR.vint06, ALL_IRR.vint07, ALL_IRR.vint08, ALL_IRR.vint09, ALL_IRR.vint10, ALL_IRR.size_t.^2, ALL_IRR.irr_t1.^2, ALL_IRR.uRaw2);
 
mult1White.Properties.VariableNames={'irr_t','seq_num_t','BuyoutDummy', 'interact',  'size_t','irr_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact + irr_t1 + vint91 + vint92+vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)       829.16      7421.4      0.11173       0.91106
%     seq_num_t        0.97709       132.1    0.0073968        0.9941
%     BuyoutDummy       123.09      593.51       0.2074       0.83574
%     interact         -51.011      17.129       -2.978     0.0029637
%     size_t           -654.63      781.76     -0.83738       0.40256
%     irr_t1            47.465      12.755       3.7214    0.00020804
%     vint91            1784.4      8703.6      0.20502       0.83759
%     vint92            884.98      7591.8      0.11657       0.90722
%     vint93            2373.2      7955.4      0.29832       0.76552
%     vint94            1913.5      7296.4      0.26225       0.79318
%     vint95            2861.2      7240.6      0.39516        0.6928
%     vint96            2482.3      7232.9      0.34319       0.73152
%     vint97            2721.6      7165.2      0.37983       0.70414
%     vint98            5214.8      7151.7      0.72916       0.46606
%     vint99            858.12      7148.6      0.12004       0.90447
%     vint00            1219.8      7141.9      0.17079       0.86442
%     vint01            1848.1        7163        0.258       0.79645
%     vint02              1522        7180      0.21198       0.83216
%     vint03            1675.5      7183.5      0.23323       0.81562
%     vint04            1650.8      7176.9      0.23001       0.81812
%     vint05            1764.3      7145.1      0.24693       0.80501
%     vint06            1684.6      7143.9      0.23581       0.81363
%     vint07            1601.8      7142.3      0.22427       0.82259
%     vint08            1781.5      7145.7      0.24931       0.80317
%     vint09            1558.7      7194.6      0.21665       0.82852
%     vint10            1541.2        7170      0.21494       0.82985
%     size_sq           39.637      67.761      0.58495        0.5587
%     irr_t1sq       -0.011249    0.035949     -0.31291       0.75441
% 
% 
% Number of observations: 1143, Error degrees of freedom: 1115
% Root Mean Squared Error: 7.1e+03
% R-squared: 0.0659,  Adjusted R-Squared 0.0433
% F-statistic vs. constant model: 2.91, p-value = 1.11e-06
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  75.3580, Crit_Val_e =  37.6525, P_e = 5.9803e-07, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_IRR.seq_num_t ALL_IRR.size_t ALL_IRR.irr_t1 ALL_IRR.BuyoutDummy ALL_IRR.interact ...
    ALL_IRR.vint91 ALL_IRR.vint92 ALL_IRR.vint93 ALL_IRR.vint94 ALL_IRR.vint95 ALL_IRR.vint96 ALL_IRR.vint97...
    ALL_IRR.vint98 ALL_IRR.vint99 ALL_IRR.vint00 ALL_IRR.vint01 ALL_IRR.vint02...
    ALL_IRR.vint03 ALL_IRR.vint04 ALL_IRR.vint05 ALL_IRR.vint06 ALL_IRR.vint07 ALL_IRR.vint08...
    ALL_IRR.vint09 ALL_IRR.vint10];
 
y = ALL_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'BuyoutDummy', 'interact' ...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
% White =
% 
%   26?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       28.661      3.3306      8.6054             0
%     seq_num_t       -0.24246     0.33146    -0.73149       0.46463
%     size_t          -0.83276     0.68415     -1.2172       0.22377
%     irr_t1           0.15111    0.062012      2.4368      0.014972
%     BuyoutDummy       4.9129      2.0374      2.4113      0.016053
%     interact       -0.069153    0.078717     -0.8785       0.37986
%     vint91           0.58268       14.71     0.03961       0.96841
%     vint92            -13.25       2.519     -5.2598    1.7211e-07
%     vint93            10.178      14.791     0.68813       0.49151
%     vint94            10.042      7.7887      1.2894       0.19753
%     vint95             10.42      9.6314      1.0819       0.27953
%     vint96             2.793      8.8765     0.31465       0.75309
%     vint97           -1.4919      7.3357    -0.20337       0.83888
%     vint98           -13.579      7.9269      -1.713      0.086988
%     vint99            -26.53      3.1085     -8.5344             0
%     vint00           -19.723      2.1142     -9.3292             0
%     vint01           -12.874      2.7354     -4.7066    2.8281e-06
%     vint02           -14.778       2.671     -5.5327    3.9087e-08
%     vint03           -14.904      2.5902     -5.7539    1.1198e-08
%     vint04           -17.339      2.4626     -7.0409    3.2911e-12
%     vint05           -16.448       2.181     -7.5414    9.4591e-14
%     vint06           -18.957      1.8445     -10.277             0
%     vint07           -16.029      1.7458     -9.1813             0
%     vint08           -14.968      1.9726     -7.5879     6.728e-14
%     vint09           -10.949      2.4716     -4.4301    1.0323e-05
%     vint10           -13.396      2.1932     -6.1083    1.3792e-09
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_IRR.seq_num_t,u)
corr_size_t_u = corr(ALL_IRR.size_t,u)
corr_irr_t1_u = corr(ALL_IRR.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 6.7682
% kurt_jbtest = 100.2697
% test_stat_jbtest = 4.5933e+05
% crit_val_jbtest = 40.6465
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_IRR(regirr1.Diagnostics.CooksDistance >=0.05,:);
 
%% Section 5.1 all funds irr t-2 - t-1 - t

clc 
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','All funds for t-2-t-1-t, irr','A2:AT606');
stringVectors = string(raw(:,[4,5,6,8,13,14,15,16,17,18,22,23,24,25]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,19,20,21,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46]);
% Create output variable
data = reshape([raw{:}],size(raw));
% Create table
ALL_IRR = table;
% Allocate imported array to column variable names
ALL_IRR.number = data(:,1);
ALL_IRR.FUNDID = data(:,2);
ALL_IRR.FIRMID = data(:,3);
ALL_IRR.teamID = stringVectors(:,1);
ALL_IRR.teamIDvintage = stringVectors(:,2);
ALL_IRR.teamIDvintagenumber = stringVectors(:,3);
ALL_IRR.VINTAGEYEAR = data(:,4);
ALL_IRR.STRATEGY = categorical(stringVectors(:,4));
ALL_IRR.BuyoutDummy = data(:,5);
ALL_IRR.irr_t = data(:,6);
ALL_IRR.irr_t1 = data(:,7);
ALL_IRR.irr_t2 = data(:,8);
ALL_IRR.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
ALL_IRR.COUNTRIES = categorical(stringVectors(:,6));
ALL_IRR.REGIONS = stringVectors(:,7);
ALL_IRR.STATUS = categorical(stringVectors(:,8));
ALL_IRR.FUNDMANAGER = stringVectors(:,9);
ALL_IRR.DOMICILE = stringVectors(:,10);
ALL_IRR.FINALCLOSESIZE = data(:,9);
ALL_IRR.size_ = data(:,10);
ALL_IRR.seq_num_t = data(:,11);
ALL_IRR.COREINDUSTRIES = categorical(stringVectors(:,11));
ALL_IRR.INDUSTRIES = stringVectors(:,12);
ALL_IRR.MANAGERREGION = categorical(stringVectors(:,13));
ALL_IRR.MANAGERCOUNTRY = categorical(stringVectors(:,14));
ALL_IRR.vint90 = data(:,12);
ALL_IRR.vint91 = data(:,13);
ALL_IRR.vint92 = data(:,14);
ALL_IRR.vint93 = data(:,15);
ALL_IRR.vint94 = data(:,16);
ALL_IRR.vint95 = data(:,17);
ALL_IRR.vint96 = data(:,18);
ALL_IRR.vint97 = data(:,19);
ALL_IRR.vint98 = data(:,20);
ALL_IRR.vint99 = data(:,21);
ALL_IRR.vint00 = data(:,22);
ALL_IRR.vint01 = data(:,23);
ALL_IRR.vint02 = data(:,24);
ALL_IRR.vint03 = data(:,25);
ALL_IRR.vint04 = data(:,26);
ALL_IRR.vint05 = data(:,27);
ALL_IRR.vint06 = data(:,28);
ALL_IRR.vint07 = data(:,29);
ALL_IRR.vint08 = data(:,30);
ALL_IRR.vint09 = data(:,31);
ALL_IRR.vint10 = data(:,32);
% Clear temporary variables
clearvars data raw stringVectors R;
% Log size
ALL_IRR.size_t=log(ALL_IRR.size_);
% Linear regression reg2irr w/o Buyout Dummy
 
regirr2 = fitlm(ALL_IRR, 'irr_t~seq_num_t + size_t + irr_t1 + irr_t2+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr2 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       17.628      19.161     0.91999       0.35796
%     irr_t1           0.21202    0.032485      6.5266    1.4686e-10
%     irr_t2         -0.017545    0.029091     -0.6031       0.54668
%     seq_num_t       -0.43954     0.66076     -0.6652       0.50618
%     vint91                 0           0         NaN           NaN
%     vint92           -17.323      26.317    -0.65824       0.51065
%     vint93                 0           0         NaN           NaN
%     vint94            37.868      26.315       1.439       0.15069
%     vint95           -8.5657      21.061     -0.4067       0.68438
%     vint96            1.7734       21.02    0.084367       0.93279
%     vint97            13.386      19.554     0.68457       0.49389
%     vint98           -9.5413      19.094     -0.4997       0.61748
%     vint99           -29.832      18.999     -1.5702       0.11691
%     vint00           -21.243      18.836     -1.1278       0.25988
%     vint01           -11.513      19.073    -0.60366       0.54631
%     vint02           -15.547       19.34    -0.80387        0.4218
%     vint03           -14.272      19.162    -0.74479        0.4567
%     vint04           -17.728      19.224    -0.92219       0.35681
%     vint05           -18.693      18.921    -0.98796       0.32359
%     vint06           -19.616      18.862       -1.04       0.29878
%     vint07           -15.014      18.883    -0.79513       0.42686
%     vint08            -13.06      18.956    -0.68897       0.49112
%     vint09           -9.2507      19.191    -0.48204       0.62996
%     vint10           -10.315       19.12     -0.5395       0.58975
%     size_t            1.3122     0.75417      1.7399      0.082403
% 
% 
% Number of observations: 605, Error degrees of freedom: 582
% Root Mean Squared Error: 26.2
% R-squared: 0.156,  Adjusted R-Squared 0.124
% F-statistic vs. constant model: 4.88, p-value = 5.82e-12
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr2.NumObservations;
k = regirr2.NumEstimatedCoefficients;
u = regirr2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr2.Residuals.Raw);
Stdev_u = std(regirr2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 6.9192e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9640
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
ALL_IRR.uRaw = regirr2.Residuals.Raw; 
ALL_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr2White = table(ALL_IRR.irr_t, ALL_IRR.seq_num_t, ALL_IRR.size_t, ALL_IRR.irr_t1, ALL_IRR.irr_t2, ALL_IRR.vint91, ALL_IRR.vint92, ALL_IRR.vint93, ALL_IRR.vint94, ...
    ALL_IRR.vint95, ALL_IRR.vint96, ALL_IRR.vint97, ALL_IRR.vint98, ALL_IRR.vint99,ALL_IRR.vint00, ALL_IRR.vint01, ALL_IRR.vint02, ALL_IRR.vint03, ALL_IRR.vint04,...
    ALL_IRR.vint05, ALL_IRR.vint06, ALL_IRR.vint07, ALL_IRR.vint08, ALL_IRR.vint09, ALL_IRR.vint10, ALL_IRR.size_t.^2, ALL_IRR.irr_t1.^2, ALL_IRR.irr_t2.^2,  ALL_IRR.uRaw2);
 
irr2White.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'irr_t2','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq', 'irr_t2sq', 'uRaw2'};
 
% Unrestricted regression
regirr2White=fitlm(irr2White,'uRaw2 ~ seq_num_t + size_t + irr_t1 + irr_t2 +vint92+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq + irr_t2sq')
 
% regirr2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)      -4453.4      6938.6    -0.64184       0.52123
%     seq_num_t         39.938      209.94     0.19023       0.84919
%     size_t            629.22      1172.6     0.53662       0.59173
%     irr_t1            70.359      17.747      3.9646    8.2703e-05
%     irr_t2            9.0867      16.831     0.53989       0.58948
%     vint92            2641.9      8299.1     0.31834       0.75034
%     vint94            2266.8      8289.5     0.27345        0.7846
%     vint95            1195.1      6631.6     0.18022       0.85704
%     vint96              1227        6613     0.18555       0.85287
%     vint97            3679.6      6158.9     0.59744       0.55044
%     vint98            6873.5        6011      1.1435       0.25331
%     vint99            1128.1      5986.1     0.18845       0.85059
%     vint00            2128.2      5954.1     0.35743        0.7209
%     vint01            3532.8      6032.6     0.58561       0.55836
%     vint02            2751.4      6114.4     0.44999       0.65289
%     vint03            2637.4      6048.9     0.43602       0.66299
%     vint04            2519.8      6064.8     0.41548       0.67794
%     vint05            2356.9      5972.5     0.39463       0.69326
%     vint06            2614.7      5960.9     0.43864       0.66108
%     vint07              2903      5968.4     0.48639       0.62687
%     vint08            2891.1        5991     0.48257       0.62958
%     vint09            2632.2      6058.5     0.43446       0.66412
%     vint10              2462      6032.5     0.40811       0.68334
%     size_sq          -75.014      97.726     -0.7676       0.44304
%     irr_t1sq       -0.020276     0.05094    -0.39804       0.69074
%     irr_t2sq       -0.022736    0.045151    -0.50355       0.61477
% 
% 
% Number of observations: 605, Error degrees of freedom: 579
% Root Mean Squared Error: 8.24e+03
% R-squared: 0.101,  Adjusted R-Squared 0.0625
% F-statistic vs. constant model: 2.61, p-value = 3.9e-05
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  61.2972, Crit_Val_e =  33.9244, P_e = 1.4336e-05, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_IRR.seq_num_t ALL_IRR.size_t ALL_IRR.irr_t1 ALL_IRR.irr_t2...
    ALL_IRR.vint92  ALL_IRR.vint94  ALL_IRR.vint95  ALL_IRR.vint96  ALL_IRR.vint97...
    ALL_IRR.vint98 ALL_IRR.vint99 ALL_IRR.vint00 ALL_IRR.vint01 ALL_IRR.vint02...
    ALL_IRR.vint03 ALL_IRR.vint04 ALL_IRR.vint05 ALL_IRR.vint06 ALL_IRR.vint07 ALL_IRR.vint08...
    ALL_IRR.vint09 ALL_IRR.vint10];
 
y = ALL_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'irr_t2' ...
    'vint92','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
% 
% White =
% 
%   23?4 table
% 
%                      beta          SE        tstat          p    
%                    _________    ________    ________    _________
% 
%     (Intercept)       17.628      16.047      1.0985      0.27242
%     seq_num_t       -0.43954     0.42867     -1.0254       0.3056
%     size_t            1.3122     0.44097      2.9757    0.0030401
%     irr_t1           0.21202    0.063126      3.3587    0.0008325
%     irr_t2         -0.017545    0.017121     -1.0247       0.3059
%     vint92           -17.323      16.059     -1.0787      0.28117
%     vint94            37.868      16.373      2.3128     0.021071
%     vint95           -8.5657      17.409    -0.49202      0.62288
%     vint96            1.7734      20.422    0.086834      0.93083
%     vint97            13.386      20.776     0.64431      0.51962
%     vint98           -9.5413      20.617     -0.4628      0.64368
%     vint99           -29.832      16.454     -1.8131     0.070316
%     vint00           -21.243      16.083     -1.3208      0.18706
%     vint01           -11.513      16.458    -0.69955      0.48448
%     vint02           -15.547      16.124    -0.96424      0.33531
%     vint03           -14.272      16.294    -0.87589      0.38144
%     vint04           -17.728       16.25     -1.0909      0.27573
%     vint05           -18.693      15.983     -1.1695      0.24266
%     vint06           -19.616      16.138     -1.2156      0.22463
%     vint07           -15.014      16.172    -0.92839      0.35358
%     vint08            -13.06      16.171    -0.80765      0.41961
%     vint09           -9.2507      16.134    -0.57338       0.5666
%     vint10           -10.315      16.051    -0.64266      0.52069
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_IRR.seq_num_t,u)
corr_size_t_u = corr(ALL_IRR.size_t,u)
corr_irr_t1_u = corr(ALL_IRR.irr_t1,u)
corr_irr_t2_u = corr(ALL_IRR.irr_t2,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 9.4961
% kurt_jbtest = 166.1443
% test_stat_jbtest = 6.8004e+05
% crit_val_jbtest = 36.7807
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr2.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_IRR(regirr2.Diagnostics.CooksDistance >=0.05,:);
 
% Linear regression reg2irr w/ Buyout Dummy
 
regirr2 = fitlm(ALL_IRR, 'irr_t~ seq_num_t + BuyoutDummy + size_t + irr_t1 + irr_t2 + vint91+ vint92 + vint93 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% regirr2 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)       2.0834      19.075      0.10922       0.91307
%     BuyoutDummy       4.1094      2.6475       1.5522       0.12116
%     irr_t1           0.20825    0.032537       6.4005    3.2007e-10
%     irr_t2         -0.015128    0.029097     -0.51991       0.60332
%     seq_num_t       -0.24167     0.67216     -0.35954       0.71932
%     vint91                 0           0          NaN           NaN
%     vint92                 0           0          NaN           NaN
%     vint93            14.858      26.333      0.56425        0.5728
%     vint94            53.185      26.219       2.0285      0.042965
%     vint95            7.1136      21.047      0.33798        0.7355
%     vint96            18.902      21.088      0.89633       0.37045
%     vint97            30.529      19.608        1.557       0.12003
%     vint98            7.8101      19.248      0.40575       0.68507
%     vint99           -12.388      19.093      -0.6488       0.51672
%     vint00           -3.7882      18.918     -0.20024       0.84136
%     vint01            5.9885      19.067      0.31407       0.75358
%     vint02              1.33      19.344     0.068756       0.94521
%     vint03            2.3264      19.202      0.12116       0.90361
%     vint04           -1.1267      19.252    -0.058524       0.95335
%     vint05           -2.5677      18.973     -0.13533       0.89239
%     vint06           -2.6804      18.908     -0.14176       0.88732
%     vint07            1.9198      18.881      0.10168       0.91905
%     vint08            3.7647      18.999      0.19815         0.843
%     vint09            6.4913      19.207      0.33797       0.73551
%     vint10            5.9252      19.153      0.30936       0.75716
%     size_t           0.58733     0.88627       0.6627       0.50779
% 
% 
% Number of observations: 605, Error degrees of freedom: 581
% Root Mean Squared Error: 26.2
% R-squared: 0.159,  Adjusted R-Squared 0.126
% F-statistic vs. constant model: 4.79, p-value = 4.97e-12
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr2.NumObservations;
k = regirr2.NumEstimatedCoefficients;
u = regirr2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr2.Residuals.Raw);
Stdev_u = std(regirr2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 5.9195e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9641
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
ALL_IRR.uRaw = regirr2.Residuals.Raw; 
ALL_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr2White = table(ALL_IRR.irr_t, ALL_IRR.seq_num_t, ALL_IRR.BuyoutDummy, ALL_IRR.size_t, ALL_IRR.irr_t1, ALL_IRR.irr_t2,  ALL_IRR.vint91, ALL_IRR.vint92, ALL_IRR.vint93, ALL_IRR.vint94, ...
    ALL_IRR.vint95, ALL_IRR.vint96, ALL_IRR.vint97, ALL_IRR.vint98, ALL_IRR.vint99,ALL_IRR.vint00, ALL_IRR.vint01, ALL_IRR.vint02, ALL_IRR.vint03, ALL_IRR.vint04,...
    ALL_IRR.vint05, ALL_IRR.vint06, ALL_IRR.vint07, ALL_IRR.vint08, ALL_IRR.vint09, ALL_IRR.vint10, ALL_IRR.size_t.^2, ALL_IRR.irr_t1.^2, ALL_IRR.irr_t2.^2,  ALL_IRR.uRaw2);
 
irr2White.Properties.VariableNames={'irr_t','seq_num_t', 'BuyoutDummy','size_t','irr_t1', 'irr_t2', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq', 'irr_t2sq', 'uRaw2'};
 
% Unrestricted regression
regirr2White=fitlm(irr2White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + irr_t1 + irr_t2+vint91 + vint92 +vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq + irr_t2sq')
 
% regirr2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 29 terms in 28 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)      -3711.6      7014.3     -0.52915        0.5969
%     seq_num_t        -21.206       215.4    -0.098446       0.92161
%     BuyoutDummy      -1277.1       859.5      -1.4858       0.13788
%     size_t            617.28      1182.8      0.52189       0.60195
%     irr_t1            74.846      18.088       4.1378    4.0302e-05
%     irr_t2            10.821      17.019      0.63581       0.52515
%     vint91                 0           0          NaN           NaN
%     vint92            1965.9      8384.1      0.23449       0.81469
%     vint93                 0           0          NaN           NaN
%     vint94            2207.6      8361.9        0.264       0.79187
%     vint95            965.74      6691.5      0.14432        0.8853
%     vint96            611.11      6685.8     0.091404        0.9272
%     vint97            3093.1      6228.9      0.49658       0.61968
%     vint98            6191.1      6084.1       1.0176        0.3093
%     vint99            343.47      6059.5     0.056683       0.95482
%     vint00            1390.2      6025.1      0.23073       0.81761
%     vint01            2813.6      6103.4      0.46099       0.64498
%     vint02            2224.5        6178      0.36007       0.71893
%     vint03              2157      6109.9      0.35304       0.72419
%     vint04            2048.2      6125.8      0.33436       0.73823
%     vint05            2066.9      6027.8      0.34289       0.73181
%     vint06            2071.7      6024.2      0.34389       0.73105
%     vint07            2368.1      6031.7      0.39261       0.69475
%     vint08            2392.6      6053.2      0.39526        0.6928
%     vint09            2443.4      6112.7      0.39973        0.6895
%     vint10            2142.5        6089      0.35186       0.72508
%     size_sq          -56.081      99.287     -0.56484        0.5724
%     irr_t1sq        -0.03346    0.051898     -0.64473       0.51936
%     irr_t2sq       -0.029463    0.045771     -0.64371       0.52002
% 
% 
% Number of observations: 605, Error degrees of freedom: 578
% Root Mean Squared Error: 8.32e+03
% R-squared: 0.104,  Adjusted R-Squared 0.0634
% F-statistic vs. constant model: 2.57, p-value = 3.9e-05
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  62.7731, Crit_Val_e =  35.1725, P_e = 1.5032e-05, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_IRR.seq_num_t ALL_IRR.size_t ALL_IRR.irr_t1 ALL_IRR.irr_t2 ALL_IRR.BuyoutDummy...
     ALL_IRR.vint93  ALL_IRR.vint94 ALL_IRR.vint95 ALL_IRR.vint96 ALL_IRR.vint97...
    ALL_IRR.vint98 ALL_IRR.vint99 ALL_IRR.vint00 ALL_IRR.vint01 ALL_IRR.vint02...
    ALL_IRR.vint03 ALL_IRR.vint04 ALL_IRR.vint05 ALL_IRR.vint06 ALL_IRR.vint07 ALL_IRR.vint08...
    ALL_IRR.vint09 ALL_IRR.vint10];
 
y = ALL_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'irr_t2', 'BuyoutDummy',...
    'vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08',...
    'vint09','vint10'};
White
% 
% White =
% 
%   24?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       2.0834      2.7077     0.76941       0.44195
%     seq_num_t       -0.24167     0.46088    -0.52436       0.60022
%     size_t           0.58733     0.58119      1.0106       0.31263
%     irr_t1           0.20825    0.063791      3.2646     0.0011582
%     irr_t2         -0.015128     0.01745    -0.86697        0.3863
%     BuyoutDummy       4.1094      2.5961      1.5829       0.11397
%     vint93            14.858      16.101     0.92283       0.35646
%     vint94            53.185      5.1408      10.346             0
%     vint95            7.1136      7.0722      1.0058       0.31489
%     vint96            18.902      13.501         1.4       0.16202
%     vint97            30.529      12.682      2.4073      0.016371
%     vint98            7.8101      12.291     0.63543       0.52539
%     vint99           -12.388      3.6597      -3.385    0.00075807
%     vint00           -3.7882      2.5716     -1.4731       0.14124
%     vint01            5.9885      3.3849      1.7692      0.077368
%     vint02              1.33       2.485     0.53523       0.59269
%     vint03            2.3264      2.4989     0.93098       0.35223
%     vint04           -1.1267      3.2087    -0.35115        0.7256
%     vint05           -2.5677      2.2458     -1.1433       0.25335
%     vint06           -2.6804      1.8464     -1.4517       0.14712
%     vint07            1.9198       2.224     0.86319       0.38837
%     vint08            3.7647       2.069      1.8196      0.069316
%     vint09            6.4913      2.6444      2.4547      0.014382
%     vint10            5.9252      2.7081       2.188      0.029054
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_IRR.seq_num_t,u)
corr_size_t_u = corr(ALL_IRR.size_t,u)
corr_irr_t1_u = corr(ALL_IRR.irr_t1,u)
corr_irr_t2_u = corr(ALL_IRR.irr_t2,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 9.6954
% kurt_jbtest = 170.5978
% test_stat_jbtest = 7.1756e+05
% crit_val_jbtest = 38.0756
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr2.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_IRR(regirr2.Diagnostics.CooksDistance >=0.05,:);
 
% Linear regression reg2irr w/ Buyout Dummy*irr
 
ALL_IRR.interact1=ALL_IRR.BuyoutDummy.*ALL_IRR.irr_t1
ALL_IRR.interact2=ALL_IRR.BuyoutDummy.*ALL_IRR.irr_t2
 
 
regirr2 = fitlm(ALL_IRR, 'irr_t~ seq_num_t + BuyoutDummy + interact1 + interact2 + size_t + irr_t1 + irr_t2+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% regirr2 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       20.171      19.261      1.0473       0.29542
%     BuyoutDummy       5.9321      3.4625      1.7133      0.087204
%     irr_t1           0.23236    0.035713      6.5065    1.6705e-10
%     irr_t2         -0.018826    0.031182    -0.60376       0.54624
%     seq_num_t       -0.26846     0.67208    -0.39945       0.68971
%     vint91                 0           0         NaN           NaN
%     vint92           -17.663      26.389    -0.66932       0.50356
%     vint93                 0           0         NaN           NaN
%     vint94            34.722      26.366      1.3169        0.1884
%     vint95           -10.274      21.118    -0.48648       0.62681
%     vint96            1.1779      21.122    0.055768       0.95555
%     vint97            13.154      19.662     0.66903       0.50374
%     vint98            -9.935      19.227    -0.51672       0.60555
%     vint99           -30.892      19.194     -1.6095       0.10806
%     vint00           -21.691      18.985     -1.1425        0.2537
%     vint01            -11.76      19.231    -0.61151        0.5411
%     vint02           -16.416       19.48    -0.84271       0.39974
%     vint03            -15.49      19.261     -0.8042       0.42161
%     vint04           -18.411      19.292    -0.95435       0.34031
%     vint05           -19.808      19.025     -1.0412       0.29823
%     vint06           -19.933      18.972     -1.0507       0.29385
%     vint07           -15.877       19.02    -0.83479       0.40418
%     vint08           -13.998      19.071    -0.73397       0.46326
%     vint09           -11.871       19.31    -0.61474       0.53897
%     vint10           -12.063      19.235    -0.62716        0.5308
%     size_t           0.49687     0.88905     0.55888       0.57646
%     interact1       -0.13528    0.082804     -1.6337       0.10287
%     interact2       0.026077    0.080753     0.32292       0.74687
% 
% 
% Number of observations: 605, Error degrees of freedom: 579
% Root Mean Squared Error: 26.2
% R-squared: 0.163,  Adjusted R-Squared 0.127
% F-statistic vs. constant model: 4.52, p-value = 8.39e-12

 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr2.NumObservations;
k = regirr2.NumEstimatedCoefficients;
u = regirr2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr2.Residuals.Raw);
Stdev_u = std(regirr2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -6.9361e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9641
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
ALL_IRR.uRaw = regirr2.Residuals.Raw; 
ALL_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr2White = table(ALL_IRR.irr_t, ALL_IRR.seq_num_t, ALL_IRR.BuyoutDummy, ALL_IRR.interact1, ALL_IRR.interact2, ALL_IRR.size_t, ALL_IRR.irr_t1, ALL_IRR.irr_t2, ALL_IRR.vint91, ALL_IRR.vint92, ALL_IRR.vint93, ALL_IRR.vint94, ...
    ALL_IRR.vint95, ALL_IRR.vint96, ALL_IRR.vint97, ALL_IRR.vint98, ALL_IRR.vint99,ALL_IRR.vint00, ALL_IRR.vint01, ALL_IRR.vint02, ALL_IRR.vint03, ALL_IRR.vint04,...
    ALL_IRR.vint05, ALL_IRR.vint06, ALL_IRR.vint07, ALL_IRR.vint08, ALL_IRR.vint09, ALL_IRR.vint10, ALL_IRR.size_t.^2, ALL_IRR.irr_t1.^2, ALL_IRR.irr_t2.^2,  ALL_IRR.uRaw2);
 
irr2White.Properties.VariableNames={'irr_t','seq_num_t', 'BuyoutDummy', 'interact1', 'interact2', 'size_t','irr_t1', 'irr_t2','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq', 'irr_t2sq', 'uRaw2'};
 
% Unrestricted regression
regirr2White=fitlm(irr2White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact1+ interact2 + irr_t1 + irr_t2+ vint91 + vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq + irr_t2sq')
%  
% regirr2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 31 terms in 30 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat        pValue  
%                    ________    ________    _________    __________
% 
%     (Intercept)       -1589      6785.3     -0.23418       0.81493
%     seq_num_t       -7.0918      207.91    -0.034109        0.9728
%     BuyoutDummy       923.4      1082.6      0.85295       0.39404
%     interact1       -118.48      28.558      -4.1488    3.8479e-05
%     interact2        -12.49      27.472     -0.45465       0.64953
%     size_t           532.98      1145.6      0.46526       0.64192
%     irr_t1           124.67      21.457       5.8103    1.0345e-08
%     irr_t2           17.889       19.48      0.91832       0.35884
%     vint91                0           0          NaN           NaN
%     vint92          -32.923      8095.2    -0.004067       0.99676
%     vint93                0           0          NaN           NaN
%     vint94          -341.23        8082    -0.042221       0.96634
%     vint95          -1429.8      6478.2     -0.22071       0.82539
%     vint96          -2110.3        6476     -0.32587       0.74464
%     vint97           489.11      6033.1     0.081072       0.93541
%     vint98             3471      5896.8      0.58863       0.55634
%     vint99          -2770.3      5886.5     -0.47061        0.6381
%     vint00          -629.08      5826.5     -0.10797       0.91406
%     vint01           853.46      5902.7      0.14459       0.88509
%     vint02          -56.848      5980.2    -0.009506       0.99242
%     vint03           62.212      5909.1     0.010528        0.9916
%     vint04           404.36      5917.4     0.068335       0.94554
%     vint05           84.123        5834     0.014419        0.9885
%     vint06           297.19      5823.5     0.051034       0.95932
%     vint07           189.92      5837.1     0.032538       0.97405
%     vint08           378.55      5853.3     0.064673       0.94846
%     vint09           -171.7      5922.6     -0.02899       0.97688
%     vint10           -278.2        5896    -0.047186       0.96238
%     size_sq         -60.617      96.018     -0.63131       0.52809
%     irr_t1sq       -0.13351     0.05648      -2.3639      0.018417
%     irr_t2sq       -0.03922    0.048953     -0.80119       0.42335
% 
% 
% Number of observations: 605, Error degrees of freedom: 576
% Root Mean Squared Error: 8.02e+03
% R-squared: 0.136,  Adjusted R-Squared 0.0937
% F-statistic vs. constant model: 3.23, p-value = 8.67e-08
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  82.0954, Crit_Val_e =  37.6525, P_e = 3.3377e-08, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [ALL_IRR.seq_num_t ALL_IRR.size_t ALL_IRR.irr_t1 ALL_IRR.irr_t2 ALL_IRR.BuyoutDummy ALL_IRR.interact1 ALL_IRR.interact2...
     ALL_IRR.vint92  ALL_IRR.vint94 ALL_IRR.vint95 ALL_IRR.vint96 ALL_IRR.vint97...
    ALL_IRR.vint98 ALL_IRR.vint99 ALL_IRR.vint00 ALL_IRR.vint01 ALL_IRR.vint02...
    ALL_IRR.vint03 ALL_IRR.vint04 ALL_IRR.vint05 ALL_IRR.vint06 ALL_IRR.vint07 ALL_IRR.vint08...
    ALL_IRR.vint09 ALL_IRR.vint10];
 
y = ALL_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'irr_t2', 'BuyoutDummy', 'interact1', 'interact2',...
    'vint92','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07',...
    'vint08','vint09','vint10'};
White
% 
% White =
% 
%   26?4 table
% 
%                      beta          SE        tstat          p    
%                    _________    ________    ________    _________
% 
%     (Intercept)       20.171      16.572      1.2171      0.22403
%     seq_num_t       -0.26846     0.47417    -0.56617      0.57149
%     size_t           0.49687     0.59409     0.83635      0.40329
%     irr_t1           0.23236    0.078708      2.9522    0.0032774
%     irr_t2         -0.018826    0.019007     -0.9905      0.32233
%     BuyoutDummy       5.9321      2.8983      2.0468     0.041115
%     interact1       -0.13528     0.11204     -1.2074      0.22777
%     interact2       0.026077    0.046151     0.56504      0.57226
%     vint92           -17.663      16.492      -1.071       0.2846
%     vint94            34.722      17.314      2.0054     0.045362
%     vint95           -10.274      17.935    -0.57284      0.56697
%     vint96            1.1779      21.202    0.055557      0.95571
%     vint97            13.154      21.136     0.62236      0.53394
%     vint98            -9.935      19.807     -0.5016      0.61613
%     vint99           -30.892      17.439     -1.7714     0.076993
%     vint00           -21.691      16.679     -1.3005      0.19393
%     vint01            -11.76      16.879    -0.69673      0.48624
%     vint02           -16.416      16.566    -0.99093      0.32212
%     vint03            -15.49      16.654    -0.93007      0.35271
%     vint04           -18.411      16.639     -1.1065      0.26895
%     vint05           -19.808      16.472     -1.2026      0.22961
%     vint06           -19.933      16.567     -1.2032      0.22937
%     vint07           -15.877      16.602    -0.95633      0.33929
%     vint08           -13.998      16.562     -0.8452      0.39834
%     vint09           -11.871        16.6    -0.71513       0.4748
%     vint10           -12.063      16.521    -0.73018      0.46557
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(ALL_IRR.seq_num_t,u)
corr_size_t_u = corr(ALL_IRR.size_t,u)
corr_irr_t1_u = corr(ALL_IRR.irr_t1,u)
corr_irr_t2_u = corr(ALL_IRR.irr_t2,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 9.4161
% kurt_jbtest = 165.3967
% test_stat_jbtest = 6.7375e+05
% crit_val_jbtest = 40.6465
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not  normally distributed. 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr2.Diagnostics.CooksDistance >=0.05);
Outliers = ALL_IRR(regirr2.Diagnostics.CooksDistance >=0.05,:);
 

%% Section 5.1 Venture funds, multiple, t-1 - t

clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','VC funds for t-1 - t, mult','A2:AS610');
stringVectors = string(raw(:,[4,5,6,8,12,13,14,15,16,17,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,18,19,20,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
VENTURE_MULT1 = table;
% Allocate imported array to column variable names
VENTURE_MULT1.number = data(:,1);
VENTURE_MULT1.FUNDID = data(:,2);
VENTURE_MULT1.FIRMID = data(:,3);
VENTURE_MULT1.teamID = stringVectors(:,1);
VENTURE_MULT1.teamIDvintage = stringVectors(:,2);
VENTURE_MULT1.teamIDvintagenumber = stringVectors(:,3);
VENTURE_MULT1.VINTAGEYEAR = data(:,4);
VENTURE_MULT1.STRATEGY = categorical(stringVectors(:,4));
VENTURE_MULT1.BuyoutDummy = data(:,5);
VENTURE_MULT1.mult_t = data(:,6);
VENTURE_MULT1.mult_t1 = data(:,7);
VENTURE_MULT1.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
VENTURE_MULT1.COUNTRIES = categorical(stringVectors(:,6));
VENTURE_MULT1.REGIONS = stringVectors(:,7);
VENTURE_MULT1.STATUS = categorical(stringVectors(:,8));
VENTURE_MULT1.FUNDMANAGER = stringVectors(:,9);
VENTURE_MULT1.DOMICILE = stringVectors(:,10);
VENTURE_MULT1.FINALCLOSESIZE = data(:,8);
VENTURE_MULT1.size_ = data(:,9);
VENTURE_MULT1.seq_num_t = data(:,10);
VENTURE_MULT1.COREINDUSTRIES = categorical(stringVectors(:,11));
VENTURE_MULT1.INDUSTRIES = stringVectors(:,12);
VENTURE_MULT1.MANAGERREGION = categorical(stringVectors(:,13));
VENTURE_MULT1.MANAGERCOUNTRY = categorical(stringVectors(:,14));
VENTURE_MULT1.vint90 = data(:,11);
VENTURE_MULT1.vint91 = data(:,12);
VENTURE_MULT1.vint92 = data(:,13);
VENTURE_MULT1.vint93 = data(:,14);
VENTURE_MULT1.vint94 = data(:,15);
VENTURE_MULT1.vint95 = data(:,16);
VENTURE_MULT1.vint96 = data(:,17);
VENTURE_MULT1.vint97 = data(:,18);
VENTURE_MULT1.vint98 = data(:,19);
VENTURE_MULT1.vint99 = data(:,20);
VENTURE_MULT1.vint00 = data(:,21);
VENTURE_MULT1.vint01 = data(:,22);
VENTURE_MULT1.vint02 = data(:,23);
VENTURE_MULT1.vint03 = data(:,24);
VENTURE_MULT1.vint04 = data(:,25);
VENTURE_MULT1.vint05 = data(:,26);
VENTURE_MULT1.vint06 = data(:,27);
VENTURE_MULT1.vint07 = data(:,28);
VENTURE_MULT1.vint08 = data(:,29);
VENTURE_MULT1.vint09 = data(:,30);
VENTURE_MULT1.vint10 = data(:,31);

% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
VENTURE_MULT1.size_t=log(VENTURE_MULT1.size_);

% Linear regression reg1mult 
 
regmult1 = fitlm(VENTURE_MULT1, 'mult_t~seq_num_t + size_t + mult_t1 +  vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
 
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE         tStat         pValue  
%                    __________    ________    __________    __________
% 
%     (Intercept)        1.5809      1.5929       0.99248       0.32138
%     mult_t1           0.14041    0.030101        4.6646    3.8354e-06
%     seq_num_t       -0.030526    0.042266      -0.72223       0.47044
%     vint91           -0.47116      2.2039      -0.21378       0.83079
%     vint92         -0.0037044      1.8447    -0.0020081        0.9984
%     vint93           -0.70276      2.0121      -0.34926       0.72702
%     vint94             4.8746      1.6938         2.878     0.0041487
%     vint95             4.2408      1.7088        2.4817      0.013354
%     vint96               2.57      1.6609        1.5474       0.12232
%     vint97             0.7386      1.6013       0.46125       0.64479
%     vint98           -0.30884      1.5982      -0.19324       0.84683
%     vint99            -1.2738        1.59      -0.80115       0.42337
%     vint00           -0.91046      1.5792      -0.57653       0.56448
%     vint01           -0.54709       1.593      -0.34342        0.7314
%     vint02           -0.53288      1.6163       -0.3297       0.74175
%     vint03           -0.55059       1.629        -0.338       0.73548
%     vint04           -0.52055      1.6272      -0.31991       0.74915
%     vint05           -0.24673      1.6051      -0.15372       0.87789
%     vint06           -0.26082      1.5911      -0.16392       0.86985
%     vint07           -0.16872      1.5899      -0.10612       0.91552
%     vint08            0.28778       1.592       0.18077       0.85661
%     vint09           -0.14558      1.6302     -0.089299       0.92888
%     vint10           -0.21009      1.6176      -0.12987       0.89671
%     size_t           0.021757    0.076153       0.28571        0.7752
% 
% 
% Number of observations: 609, Error degrees of freedom: 585
% Root Mean Squared Error: 2.2
% R-squared: 0.25,  Adjusted R-Squared 0.22
% F-statistic vs. constant model: 8.48, p-value = 1.26e-24
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -2.3160e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9640
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
VENTURE_MULT1.uRaw = regmult1.Residuals.Raw; 
VENTURE_MULT1.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(VENTURE_MULT1.mult_t, VENTURE_MULT1.seq_num_t, VENTURE_MULT1.size_t, VENTURE_MULT1.mult_t1, VENTURE_MULT1.vint91, VENTURE_MULT1.vint92, VENTURE_MULT1.vint93, VENTURE_MULT1.vint94, ...
    VENTURE_MULT1.vint95, VENTURE_MULT1.vint96, VENTURE_MULT1.vint97, VENTURE_MULT1.vint98, VENTURE_MULT1.vint99,VENTURE_MULT1.vint00, VENTURE_MULT1.vint01, VENTURE_MULT1.vint02, VENTURE_MULT1.vint03, VENTURE_MULT1.vint04,...
    VENTURE_MULT1.vint05, VENTURE_MULT1.vint06, VENTURE_MULT1.vint07, VENTURE_MULT1.vint08, VENTURE_MULT1.vint09, VENTURE_MULT1.vint10, VENTURE_MULT1.size_t.^2, VENTURE_MULT1.mult_t1.^2, VENTURE_MULT1.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + mult_t1 + vint91 +vint92 + vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
% 
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat       pValue  
%                    _________    ________    _________    _________
% 
%     (Intercept)      -4.1361       26.73     -0.15474      0.87708
%     seq_num_t       -0.15909     0.61827     -0.25731      0.79703
%     size_t          0.060309      5.9445     0.010145      0.99191
%     mult_t1           2.9601     0.92983       3.1834    0.0015329
%     vint91            1.9967      31.932     0.062529      0.95016
%     vint92          -0.55497      26.725    -0.020766      0.98344
%     vint93            1.1336       29.23     0.038783      0.96908
%     vint94            70.594      24.539       2.8768    0.0041643
%     vint95            26.741       24.79       1.0787      0.28116
%     vint96            34.075      24.118       1.4128      0.15824
%     vint97            1.7727      23.199     0.076413      0.93912
%     vint98            3.9327      23.169      0.16974      0.86528
%     vint99           -3.0033      23.071     -0.13018      0.89647
%     vint00           0.76898      22.913      0.03356      0.97324
%     vint01            3.0274      23.121      0.13094      0.89587
%     vint02            1.8617      23.444      0.07941      0.93673
%     vint03            2.1444      23.612     0.090815      0.92767
%     vint04            1.2105      23.592      0.05131       0.9591
%     vint05            2.5551      23.263      0.10984      0.91258
%     vint06            2.3504       23.08      0.10184      0.91892
%     vint07            1.5252      23.045     0.066183      0.94725
%     vint08            6.9207      23.089      0.29974      0.76448
%     vint09            1.4936      23.653     0.063144      0.94967
%     vint10           0.84463      23.442      0.03603      0.97127
%     size_sq        -0.027902     0.63978    -0.043612      0.96523
%     mult_t1sq      -0.057021    0.030312      -1.8811      0.06045
% 
% 
% Number of observations: 609, Error degrees of freedom: 583
% Root Mean Squared Error: 31.9
% R-squared: 0.145,  Adjusted R-Squared 0.108
% F-statistic vs. constant model: 3.96, p-value = 8.61e-10
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  88.3743, Crit_Val_e =  35.1725, P_e = 1.3303e-09, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [VENTURE_MULT1.seq_num_t VENTURE_MULT1.size_t VENTURE_MULT1.mult_t1 ...
    VENTURE_MULT1.vint91 VENTURE_MULT1.vint92 VENTURE_MULT1.vint93 VENTURE_MULT1.vint94 VENTURE_MULT1.vint95 VENTURE_MULT1.vint96 VENTURE_MULT1.vint97...
    VENTURE_MULT1.vint98 VENTURE_MULT1.vint99 VENTURE_MULT1.vint00 VENTURE_MULT1.vint01 VENTURE_MULT1.vint02...
    VENTURE_MULT1.vint03 VENTURE_MULT1.vint04 VENTURE_MULT1.vint05 VENTURE_MULT1.vint06 VENTURE_MULT1.vint07 VENTURE_MULT1.vint08...
    VENTURE_MULT1.vint09 VENTURE_MULT1.vint10];
 
y = VENTURE_MULT1.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', ...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White
% % 
% White =
% 
%   24?4 table
% 
%                       beta          SE         tstat           p     
%                    __________    ________    __________    __________
% 
%     (Intercept)        1.5809     0.60187        2.6267     0.0088403
%     seq_num_t       -0.030526    0.022827       -1.3373       0.18164
%     size_t           0.021757    0.056583       0.38452       0.70073
%     mult_t1           0.14041    0.026955         5.209    2.6039e-07
%     vint91           -0.47116     0.55054      -0.85581       0.39244
%     vint92         -0.0037044     0.53943    -0.0068672       0.99452
%     vint93           -0.70276     0.60546       -1.1607       0.24622
%     vint94             4.8746      2.4172        2.0166      0.044174
%     vint95             4.2408      1.8574        2.2832      0.022764
%     vint96               2.57      1.6825        1.5275       0.12716
%     vint97             0.7386     0.67447        1.0951       0.27391
%     vint98           -0.30884     0.65545      -0.47119       0.63767
%     vint99            -1.2738     0.57587        -2.212      0.027335
%     vint00           -0.91046     0.55619        -1.637       0.10215
%     vint01           -0.54709     0.57594       -0.9499       0.34254
%     vint02           -0.53288     0.57015      -0.93464       0.35035
%     vint03           -0.55059     0.59504      -0.92531       0.35517
%     vint04           -0.52055      0.5796      -0.89811       0.36948
%     vint05           -0.24673     0.62166      -0.39689       0.69158
%     vint06           -0.26082     0.57728      -0.45181       0.65157
%     vint07           -0.16872     0.58178      -0.29001       0.77191
%     vint08            0.28778     0.66578       0.43225       0.66572
%     vint09           -0.14558     0.56605      -0.25718       0.79713
%     vint10           -0.21009     0.57797      -0.36349       0.71637
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(VENTURE_MULT1.seq_num_t,u)
corr_size_t_u = corr(VENTURE_MULT1.size_t,u)
corr_mult_t1_u = corr(VENTURE_MULT1.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 5.5499
% kurt_jbtest = 53.3039
% test_stat_jbtest = 6.7337e+04
% crit_val_jbtest = 38.0756
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = VENTURE_MULT1(regmult1.Diagnostics.CooksDistance >=0.05,:);

%% Section 5.1 buyout, multiple, t-1 - t
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Buyout funds for t-1 - t, mult','A2:AS721');
stringVectors = string(raw(:,[4,5,6,8,12,13,14,15,16,17,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,18,19,20,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
BUYOUT_MULT1 = table;

% Allocate imported array to column variable names
BUYOUT_MULT1.number = data(:,1);
BUYOUT_MULT1.FUNDID = data(:,2);
BUYOUT_MULT1.FIRMID = data(:,3);
BUYOUT_MULT1.teamID = stringVectors(:,1);
BUYOUT_MULT1.teamIDvintage = stringVectors(:,2);
BUYOUT_MULT1.teamIDvintagenumber = stringVectors(:,3);
BUYOUT_MULT1.VINTAGEYEAR = data(:,4);
BUYOUT_MULT1.STRATEGY = categorical(stringVectors(:,4));
BUYOUT_MULT1.BuyoutDummy = data(:,5);
BUYOUT_MULT1.mult_t = data(:,6);
BUYOUT_MULT1.mult_t1 = data(:,7);
BUYOUT_MULT1.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
BUYOUT_MULT1.COUNTRIES = categorical(stringVectors(:,6));
BUYOUT_MULT1.REGIONS = categorical(stringVectors(:,7));
BUYOUT_MULT1.STATUS = categorical(stringVectors(:,8));
BUYOUT_MULT1.FUNDMANAGER = stringVectors(:,9);
BUYOUT_MULT1.DOMICILE = stringVectors(:,10);
BUYOUT_MULT1.FINALCLOSESIZE = data(:,8);
BUYOUT_MULT1.size_ = data(:,9);
BUYOUT_MULT1.seq_num_t = data(:,10);
BUYOUT_MULT1.COREINDUSTRIES = categorical(stringVectors(:,11));
BUYOUT_MULT1.INDUSTRIES = categorical(stringVectors(:,12));
BUYOUT_MULT1.MANAGERREGION = categorical(stringVectors(:,13));
BUYOUT_MULT1.MANAGERCOUNTRY = categorical(stringVectors(:,14));
BUYOUT_MULT1.vint90 = data(:,11);
BUYOUT_MULT1.vint91 = data(:,12);
BUYOUT_MULT1.vint92 = data(:,13);
BUYOUT_MULT1.vint93 = data(:,14);
BUYOUT_MULT1.vint94 = data(:,15);
BUYOUT_MULT1.vint95 = data(:,16);
BUYOUT_MULT1.vint96 = data(:,17);
BUYOUT_MULT1.vint97 = data(:,18);
BUYOUT_MULT1.vint98 = data(:,19);
BUYOUT_MULT1.vint99 = data(:,20);
BUYOUT_MULT1.vint00 = data(:,21);
BUYOUT_MULT1.vint01 = data(:,22);
BUYOUT_MULT1.vint02 = data(:,23);
BUYOUT_MULT1.vint03 = data(:,24);
BUYOUT_MULT1.vint04 = data(:,25);
BUYOUT_MULT1.vint05 = data(:,26);
BUYOUT_MULT1.vint06 = data(:,27);
BUYOUT_MULT1.vint07 = data(:,28);
BUYOUT_MULT1.vint08 = data(:,29);
BUYOUT_MULT1.vint09 = data(:,30);
BUYOUT_MULT1.vint10 = data(:,31);
% Clear temporary variables
clearvars data raw stringVectors R;
% Log size
BUYOUT_MULT1.size_t=log(BUYOUT_MULT1.size_);
% Linear regression reg1mult 
 
regmult1 = fitlm(BUYOUT_MULT1, 'mult_t~seq_num_t + size_t + mult_t1 +  vint91 + vint92 + vint93 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
 
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       1.4738     0.43611      3.3794    0.00076692
%     mult_t1          0.19604    0.033732      5.8116    9.4201e-09
%     seq_num_t      0.0024338    0.017922      0.1358       0.89202
%     vint91            1.6844     0.81812      2.0588      0.039884
%     vint92                 0           0         NaN           NaN
%     vint93            1.2963      0.5803      2.2338       0.02581
%     vint94            1.2432     0.48795      2.5478      0.011054
%     vint95         0.0085425     0.44814    0.019062        0.9848
%     vint96           0.51118     0.45012      1.1357       0.25649
%     vint97           -0.2877     0.42761     -0.6728        0.5013
%     vint98         -0.068352     0.42338    -0.16144       0.87179
%     vint99          0.063727     0.42378     0.15038       0.88051
%     vint00           0.43742     0.42076      1.0396       0.29888
%     vint01           0.58848     0.43069      1.3663       0.17227
%     vint02           0.35625      0.4323     0.82408       0.41018
%     vint03           0.23915     0.42742     0.55953       0.57598
%     vint04           0.27409     0.42534     0.64439       0.51953
%     vint05          0.090183     0.41566     0.21696        0.8283
%     vint06           0.05453     0.41646     0.13094       0.89586
%     vint07           0.19821     0.41651     0.47589        0.6343
%     vint08           0.26248     0.41797     0.62799       0.53021
%     vint09           0.28996     0.42511     0.68208       0.49542
%     vint10           0.28125     0.42346     0.66417        0.5068
%     size_t         -0.043035    0.022133     -1.9444      0.052255
% 
% 
% Number of observations: 720, Error degrees of freedom: 697
% Root Mean Squared Error: 0.707
% R-squared: 0.149,  Adjusted R-Squared 0.122
% F-statistic vs. constant model: 5.55, p-value = 2.03e-14 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -8.0519e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9634
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
BUYOUT_MULT1.uRaw = regmult1.Residuals.Raw; 
BUYOUT_MULT1.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(BUYOUT_MULT1.mult_t, BUYOUT_MULT1.seq_num_t, BUYOUT_MULT1.size_t, BUYOUT_MULT1.mult_t1, BUYOUT_MULT1.vint91, BUYOUT_MULT1.vint92, BUYOUT_MULT1.vint93, BUYOUT_MULT1.vint94, ...
    BUYOUT_MULT1.vint95, BUYOUT_MULT1.vint96, BUYOUT_MULT1.vint97, BUYOUT_MULT1.vint98, BUYOUT_MULT1.vint99,BUYOUT_MULT1.vint00, BUYOUT_MULT1.vint01, BUYOUT_MULT1.vint02, BUYOUT_MULT1.vint03, BUYOUT_MULT1.vint04,...
    BUYOUT_MULT1.vint05, BUYOUT_MULT1.vint06, BUYOUT_MULT1.vint07, BUYOUT_MULT1.vint08, BUYOUT_MULT1.vint09, BUYOUT_MULT1.vint10, BUYOUT_MULT1.size_t.^2, BUYOUT_MULT1.mult_t1.^2, BUYOUT_MULT1.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + mult_t1 + vint91 +vint92+vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat       pValue  
%                    _________    ________    _______    __________
% 
%     (Intercept)       6.9762      2.0838     3.3479     0.0008583
%     seq_num_t      -0.009555    0.044712    -0.2137       0.83084
%     size_t           -2.0468     0.36503    -5.6072    2.9693e-08
%     mult_t1         -0.22487     0.21536    -1.0442       0.29677
%     vint91                 0           0        NaN           NaN
%     vint92           0.72019       1.985    0.36281       0.71686
%     vint93            1.9578      1.9811    0.98819        0.3234
%     vint94             2.658      1.8372     1.4468       0.14841
%     vint95           0.87099      1.7723    0.49145       0.62326
%     vint96            3.7792       1.778     2.1255      0.033897
%     vint97            0.7874      1.7462    0.45091       0.65219
%     vint98           0.94499      1.7438    0.54192       0.58804
%     vint99           0.97778      1.7457     0.5601       0.57559
%     vint00            0.6538      1.7397    0.37582       0.70717
%     vint01           0.91274      1.7549    0.52009       0.60316
%     vint02             1.013      1.7563    0.57682       0.56425
%     vint03           0.65059      1.7483    0.37214       0.70991
%     vint04           0.88773      1.7448    0.50877       0.61107
%     vint05            1.1517       1.732    0.66499       0.50628
%     vint06           0.84731      1.7336    0.48874       0.62518
%     vint07           0.84286       1.733    0.48636       0.62687
%     vint08           0.78399      1.7368    0.45141       0.65183
%     vint09            0.6998      1.7465    0.40069       0.68877
%     vint10            1.3785      1.7431    0.79087       0.42929
%     size_sq          0.14027    0.028229     4.9691    8.4812e-07
%     mult_t1sq       0.029399    0.038072     0.7722       0.44026
% 
% 
% Number of observations: 720, Error degrees of freedom: 695
% Root Mean Squared Error: 1.71
% R-squared: 0.138,  Adjusted R-Squared 0.108
% F-statistic vs. constant model: 4.62, p-value = 5.2e-12
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  99.0288, Crit_Val_e =  33.9244, P_e = 9.5297e-12, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [BUYOUT_MULT1.seq_num_t BUYOUT_MULT1.size_t BUYOUT_MULT1.mult_t1 ...
    BUYOUT_MULT1.vint91 BUYOUT_MULT1.vint93 BUYOUT_MULT1.vint94 BUYOUT_MULT1.vint95 BUYOUT_MULT1.vint96 BUYOUT_MULT1.vint97...
    BUYOUT_MULT1.vint98 BUYOUT_MULT1.vint99 BUYOUT_MULT1.vint00 BUYOUT_MULT1.vint01 BUYOUT_MULT1.vint02...
    BUYOUT_MULT1.vint03 BUYOUT_MULT1.vint04 BUYOUT_MULT1.vint05 BUYOUT_MULT1.vint06 BUYOUT_MULT1.vint07 BUYOUT_MULT1.vint08...
    BUYOUT_MULT1.vint09];
 
y = BUYOUT_MULT1.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', ...
    'vint91','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09'};
White
% % 
% White =
% 
%   22?4 table
% 
%                       beta          SE         tstat           p     
%                    __________    ________    __________    __________
% 
%     (Intercept)        1.7358     0.24395        7.1154    2.7083e-12
%     seq_num_t        0.003208    0.016531       0.19406       0.84618
%     size_t          -0.043283    0.026823       -1.6137       0.10704
%     mult_t1           0.19566    0.039266         4.983     7.851e-07
%     vint91             1.4233     0.14892        9.5576             0
%     vint93             1.0347     0.64775        1.5974       0.11063
%     vint94            0.98171     0.55494         1.769      0.077314
%     vint95           -0.25292     0.25557      -0.98964       0.32268
%     vint96             0.2496     0.51836       0.48152       0.63029
%     vint97           -0.54919     0.16403       -3.3481    0.00085614
%     vint98            -0.3301     0.16187       -2.0393      0.041785
%     vint99            -0.1984     0.17847       -1.1117       0.26665
%     vint00            0.17519     0.16347        1.0717       0.28421
%     vint01            0.32646     0.17782        1.8359      0.066789
%     vint02           0.094317     0.20122       0.46873        0.6394
%     vint03          -0.023113     0.15353      -0.15055       0.88038
%     vint04           0.011528     0.18166      0.063458       0.94942
%     vint05           -0.17192     0.15099       -1.1386       0.25525
%     vint06           -0.20768     0.14164       -1.4663       0.14302
%     vint07          -0.063981     0.15024      -0.42585       0.67034
%     vint08         9.4526e-05     0.13825    0.00068373       0.99945
%     vint09           0.027319     0.14352       0.19035       0.84909
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(BUYOUT_MULT1.seq_num_t,u)
corr_size_t_u = corr(BUYOUT_MULT1.size_t,u)
corr_mult_t1_u = corr(BUYOUT_MULT1.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 1.8191
% kurt_jbtest = 15.0197
% test_stat_jbtest = 4.7313e+03
% crit_val_jbtest = 36.7807
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = BUYOUT_MULT1(regmult1.Diagnostics.CooksDistance >=0.05,:);

%% Section 5.1 venture capital, multiple, t-2 - t-1 - t
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','VC funds for t-2-t-1-t, mult','A2:AT344');
stringVectors = string(raw(:,[4,5,6,8,13,14,15,16,17,18,22,23,24,25]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,19,20,21,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46]);
% Create output variable
data = reshape([raw{:}],size(raw));
% Create table
VENTURE_MULT2 = table;

% Allocate imported array to column variable names
VENTURE_MULT2.number = data(:,1);
VENTURE_MULT2.FUNDID = data(:,2);
VENTURE_MULT2.FIRMID = data(:,3);
VENTURE_MULT2.teamID = stringVectors(:,1);
VENTURE_MULT2.teamIDvintage = stringVectors(:,2);
VENTURE_MULT2.teamIDvintagenumber = stringVectors(:,3);
VENTURE_MULT2.VINTAGEYEAR = data(:,4);
VENTURE_MULT2.STRATEGY = categorical(stringVectors(:,4));
VENTURE_MULT2.BuyoutDummy = data(:,5);
VENTURE_MULT2.mult_t = data(:,6);
VENTURE_MULT2.mult_t1 = data(:,7);
VENTURE_MULT2.mult_t2 = data(:,8);
VENTURE_MULT2.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
VENTURE_MULT2.COUNTRIES = categorical(stringVectors(:,6));
VENTURE_MULT2.REGIONS = stringVectors(:,7);
VENTURE_MULT2.STATUS = categorical(stringVectors(:,8));
VENTURE_MULT2.FUNDMANAGER = stringVectors(:,9);
VENTURE_MULT2.DOMICILE = categorical(stringVectors(:,10));
VENTURE_MULT2.FINALCLOSESIZE = data(:,9);
VENTURE_MULT2.size_ = data(:,10);
VENTURE_MULT2.seq_num_t = data(:,11);
VENTURE_MULT2.COREINDUSTRIES = categorical(stringVectors(:,11));
VENTURE_MULT2.INDUSTRIES = stringVectors(:,12);
VENTURE_MULT2.MANAGERREGION = categorical(stringVectors(:,13));
VENTURE_MULT2.MANAGERCOUNTRY = categorical(stringVectors(:,14));
VENTURE_MULT2.vint90 = data(:,12);
VENTURE_MULT2.vint91 = data(:,13);
VENTURE_MULT2.vint92 = data(:,14);
VENTURE_MULT2.vint93 = data(:,15);
VENTURE_MULT2.vint94 = data(:,16);
VENTURE_MULT2.vint95 = data(:,17);
VENTURE_MULT2.vint96 = data(:,18);
VENTURE_MULT2.vint97 = data(:,19);
VENTURE_MULT2.vint98 = data(:,20);
VENTURE_MULT2.vint99 = data(:,21);
VENTURE_MULT2.vint00 = data(:,22);
VENTURE_MULT2.vint01 = data(:,23);
VENTURE_MULT2.vint02 = data(:,24);
VENTURE_MULT2.vint03 = data(:,25);
VENTURE_MULT2.vint04 = data(:,26);
VENTURE_MULT2.vint05 = data(:,27);
VENTURE_MULT2.vint06 = data(:,28);
VENTURE_MULT2.vint07 = data(:,29);
VENTURE_MULT2.vint08 = data(:,30);
VENTURE_MULT2.vint09 = data(:,31);
VENTURE_MULT2.vint10 = data(:,32);
% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
VENTURE_MULT2.size_t=log(VENTURE_MULT2.size_);
 
% Linear regression reg2mult 
 
regmult2 = fitlm(VENTURE_MULT2, 'mult_t~seq_num_t + size_t + mult_t1 + mult_t2+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult2 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       7.2979      1.5958      4.5732    6.8891e-06
%     mult_t1          0.12253      0.0303       4.044    6.5991e-05
%     mult_t2         0.019724    0.026337     0.74891       0.45446
%     seq_num_t      -0.033781      0.0365    -0.92551        0.3554
%     vint91           -5.8685      1.9063     -3.0784     0.0022623
%     vint92           -5.7947      1.9027     -3.0455     0.0025168
%     vint93                 0           0         NaN           NaN
%     vint94                 0           0         NaN           NaN
%     vint95           -4.6113      1.7956     -2.5681      0.010681
%     vint96           -4.2127      1.7428     -2.4172      0.016205
%     vint97           -3.6946      1.6087     -2.2966       0.02229
%     vint98           -5.2368      1.6056     -3.2615     0.0012285
%     vint99            -6.713       1.589     -4.2246    3.1298e-05
%     vint00           -6.3228      1.5742     -4.0165     7.375e-05
%     vint01           -5.9256      1.5815     -3.7467     0.0002127
%     vint02           -5.8981      1.5981     -3.6906    0.00026311
%     vint03           -5.8721      1.6068     -3.6545    0.00030133
%     vint04            -5.682      1.6136     -3.5214    0.00049198
%     vint05           -5.7658       1.596     -3.6127    0.00035207
%     vint06           -5.5564      1.5823     -3.5116    0.00050992
%     vint07           -5.4476      1.5797     -3.4484    0.00063991
%     vint08            -4.717      1.5945     -2.9583     0.0033258
%     vint09            -5.488      1.6012     -3.4274    0.00068955
%     vint10           -5.6962      1.6026     -3.5543    0.00043653
%     size_t         -0.047735    0.068573    -0.69612       0.48686
% 
% 
% Number of observations: 343, Error degrees of freedom: 320
% Root Mean Squared Error: 1.55
% R-squared: 0.239,  Adjusted R-Squared 0.186
% F-statistic vs. constant model: 4.56, p-value = 3.65e-10
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult2.NumObservations;
k = regmult2.NumEstimatedCoefficients;
u = regmult2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult2.Residuals.Raw);
Stdev_u = std(regmult2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 4.6383e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9674
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
VENTURE_MULT2.uRaw = regmult2.Residuals.Raw; 
VENTURE_MULT2.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult2White = table(VENTURE_MULT2.mult_t, VENTURE_MULT2.seq_num_t, VENTURE_MULT2.size_t, VENTURE_MULT2.mult_t1, VENTURE_MULT2.mult_t2, VENTURE_MULT2.vint91, VENTURE_MULT2.vint92, VENTURE_MULT2.vint93, VENTURE_MULT2.vint94, ...
    VENTURE_MULT2.vint95, VENTURE_MULT2.vint96, VENTURE_MULT2.vint97, VENTURE_MULT2.vint98, VENTURE_MULT2.vint99,VENTURE_MULT2.vint00, VENTURE_MULT2.vint01, VENTURE_MULT2.vint02, VENTURE_MULT2.vint03, VENTURE_MULT2.vint04,...
    VENTURE_MULT2.vint05, VENTURE_MULT2.vint06, VENTURE_MULT2.vint07, VENTURE_MULT2.vint08, VENTURE_MULT2.vint09, VENTURE_MULT2.vint10, VENTURE_MULT2.size_t.^2, VENTURE_MULT2.mult_t1.^2, VENTURE_MULT2.mult_t2.^2,  VENTURE_MULT2.uRaw2);
 
mult2White.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'mult_t2','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq', 'mult_t2sq', 'uRaw2'};
 
% Unrestricted regression
regmult2White=fitlm(mult2White,'uRaw2 ~ seq_num_t + size_t + mult_t1 + mult_t2+ vint91+vint92+vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq + mult_t2sq')
 
% regmult2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE          tStat        pValue 
%                    __________    ________    ___________    ________
% 
%     (Intercept)        1.9817      13.936        0.14221     0.88701
%     seq_num_t       -0.053078     0.36195       -0.14665     0.88351
%     size_t           -0.70752      3.5918       -0.19698     0.84397
%     mult_t1            1.6389     0.79572         2.0596     0.04026
%     mult_t2            1.1209     0.54077         2.0728    0.039002
%     vint91                  0           0            NaN         NaN
%     vint92            -1.2169      15.336      -0.079345     0.93681
%     vint93                  0           0            NaN         NaN
%     vint94           -0.98265      18.799      -0.052272     0.95835
%     vint95            -6.8469      14.197       -0.48228     0.62995
%     vint96            -2.8493      13.436       -0.21207     0.83219
%     vint97             3.6424      11.643        0.31284     0.75461
%     vint98             7.2353      11.593        0.62411     0.53301
%     vint99            -4.1737      11.333       -0.36829     0.71291
%     vint00            -2.0593      11.121       -0.18518     0.85321
%     vint01             1.1895      11.245        0.10578     0.91583
%     vint02            -1.1462      11.481      -0.099839     0.92054
%     vint03         -0.0093288      11.556    -0.00080729     0.99936
%     vint04             1.2094      11.716        0.10323     0.91785
%     vint05             1.1918      11.419        0.10437     0.91694
%     vint06             2.3999      11.216        0.21397     0.83071
%     vint07            0.89862      11.189        0.08031     0.93604
%     vint08             9.1962      11.365        0.80916     0.41903
%     vint09             0.1199      11.517       0.010411      0.9917
%     vint10            0.24793      11.541       0.021482     0.98287
%     size_sq         -0.070174      0.3831       -0.18317     0.85478
%     mult_t1sq       0.0030312    0.031135       0.097354     0.92251
%     mult_t2sq       -0.031265    0.016636        -1.8794    0.061116
% 
% 
% Number of observations: 343, Error degrees of freedom: 317
% Root Mean Squared Error: 15.3
% R-squared: 0.173,  Adjusted R-Squared 0.108
% F-statistic vs. constant model: 2.66, p-value = 4.77e-05
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  59.4244, Crit_Val_e =  33.9244, P_e = 2.7179e-05, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [VENTURE_MULT2.seq_num_t VENTURE_MULT2.size_t VENTURE_MULT2.mult_t1 VENTURE_MULT2.mult_t2...
    VENTURE_MULT2.vint92 VENTURE_MULT2.vint94  VENTURE_MULT2.vint95  VENTURE_MULT2.vint96  VENTURE_MULT2.vint97...
    VENTURE_MULT2.vint98 VENTURE_MULT2.vint99 VENTURE_MULT2.vint00 VENTURE_MULT2.vint01 VENTURE_MULT2.vint02...
    VENTURE_MULT2.vint03 VENTURE_MULT2.vint04 VENTURE_MULT2.vint05 VENTURE_MULT2.vint06 VENTURE_MULT2.vint07 VENTURE_MULT2.vint08...
    VENTURE_MULT2.vint09 VENTURE_MULT2.vint10];
 
y = VENTURE_MULT2.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'mult_t2' ...
    'vint92','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
% White =
% 
%   23?4 table
% 
%                       beta          SE         tstat          p     
%                    __________    ________    _________    __________
% 
%     (Intercept)        1.4295     0.33878       4.2195    3.1429e-05
%     seq_num_t       -0.033781    0.018117      -1.8646      0.063091
%     size_t          -0.047735    0.073763     -0.64714       0.51797
%     mult_t1           0.12253     0.09278       1.3207       0.18749
%     mult_t2          0.019724    0.027377      0.72047       0.47173
%     vint92           0.073799     0.14682      0.50266       0.61553
%     vint94             5.8685    0.089187         65.8             0
%     vint95             1.2572     0.58407       2.1524      0.032065
%     vint96             1.6558      1.0121       1.6361       0.10275
%     vint97             2.1739     0.81294       2.6742     0.0078519
%     vint98            0.63167       0.513       1.2313       0.21905
%     vint99           -0.84452     0.23795      -3.5492    0.00044062
%     vint00           -0.45426     0.11808      -3.8472    0.00014261
%     vint01           -0.05709     0.15979     -0.35727       0.72111
%     vint02          -0.029627     0.12313     -0.24061          0.81
%     vint03         -0.0036072     0.17157    -0.021024       0.98324
%     vint04            0.18647     0.22379       0.8332       0.40531
%     vint05            0.10267     0.19592      0.52403        0.6006
%     vint06            0.31204     0.20166       1.5473       0.12271
%     vint07            0.42093     0.14895        2.826     0.0049909
%     vint08             1.1515     0.69308       1.6614      0.097554
%     vint09            0.38048     0.15466       2.4601      0.014386
%     vint10            0.17233     0.23983      0.71855       0.47291
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(VENTURE_MULT2.seq_num_t,u)
corr_size_t_u = corr(VENTURE_MULT2.size_t,u)
corr_mult_t1_u = corr(VENTURE_MULT2.mult_t1,u)
corr_mult_t2_u = corr(VENTURE_MULT2.mult_t2,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 5.5096
% kurt_jbtest = 52.4699
% test_stat_jbtest = 3.6711e+04
% crit_val_jbtest = 36.7807
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult2.Diagnostics.CooksDistance >=0.05);
Outliers = VENTURE_MULT2(regmult2.Diagnostics.CooksDistance >=0.05,:);


%% Section 5.1 buyout funds, multiple, t-2 - t-1 - t

clc
clear all
close all
 
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Buyout funds for t-2-t-1-t,mult','A2:AT401');
stringVectors = string(raw(:,[4,5,6,8,13,14,15,16,17,18,22,23,24,25]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,19,20,21,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46]);

% Create output variable
data = reshape([raw{:}],size(raw)); 
% Create table
BUYOUT_MULT2 = table;
 
% Allocate imported array to column variable names
BUYOUT_MULT2.number = data(:,1);
BUYOUT_MULT2.FUNDID = data(:,2);
BUYOUT_MULT2.FIRMID = data(:,3);
BUYOUT_MULT2.teamID = stringVectors(:,1);
BUYOUT_MULT2.teamIDvintage = stringVectors(:,2);
BUYOUT_MULT2.teamIDvintagenumber = stringVectors(:,3);
BUYOUT_MULT2.VINTAGEYEAR = data(:,4);
BUYOUT_MULT2.STRATEGY = categorical(stringVectors(:,4));
BUYOUT_MULT2.BuyoutDummy = data(:,5);
BUYOUT_MULT2.mult_t = data(:,6);
BUYOUT_MULT2.mult_t1 = data(:,7);
BUYOUT_MULT2.mult_t2 = data(:,8);
BUYOUT_MULT2.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
BUYOUT_MULT2.COUNTRIES = categorical(stringVectors(:,6));
BUYOUT_MULT2.REGIONS = categorical(stringVectors(:,7));
BUYOUT_MULT2.STATUS = categorical(stringVectors(:,8));
BUYOUT_MULT2.FUNDMANAGER = stringVectors(:,9);
BUYOUT_MULT2.DOMICILE = stringVectors(:,10);
BUYOUT_MULT2.FINALCLOSESIZE = data(:,9);
BUYOUT_MULT2.size_ = data(:,10);
BUYOUT_MULT2.seq_num_t = data(:,11);
BUYOUT_MULT2.COREINDUSTRIES = categorical(stringVectors(:,11));
BUYOUT_MULT2.INDUSTRIES = stringVectors(:,12);
BUYOUT_MULT2.MANAGERREGION = categorical(stringVectors(:,13));
BUYOUT_MULT2.MANAGERCOUNTRY = categorical(stringVectors(:,14));
BUYOUT_MULT2.vint90 = data(:,12);
BUYOUT_MULT2.vint91 = data(:,13);
BUYOUT_MULT2.vint92 = data(:,14);
BUYOUT_MULT2.vint93 = data(:,15);
BUYOUT_MULT2.vint94 = data(:,16);
BUYOUT_MULT2.vint95 = data(:,17);
BUYOUT_MULT2.vint96 = data(:,18);
BUYOUT_MULT2.vint97 = data(:,19);
BUYOUT_MULT2.vint98 = data(:,20);
BUYOUT_MULT2.vint99 = data(:,21);
BUYOUT_MULT2.vint00 = data(:,22);
BUYOUT_MULT2.vint01 = data(:,23);
BUYOUT_MULT2.vint02 = data(:,24);
BUYOUT_MULT2.vint03 = data(:,25);
BUYOUT_MULT2.vint04 = data(:,26);
BUYOUT_MULT2.vint05 = data(:,27);
BUYOUT_MULT2.vint06 = data(:,28);
BUYOUT_MULT2.vint07 = data(:,29);
BUYOUT_MULT2.vint08 = data(:,30);
BUYOUT_MULT2.vint09 = data(:,31);
BUYOUT_MULT2.vint10 = data(:,32);
 
% Clear temporary variables
clearvars data raw stringVectors R;
 
% Log size
BUYOUT_MULT2.size_t=log(BUYOUT_MULT2.size_);
 
% Linear regression reg2mult 
 
regmult2 = fitlm(BUYOUT_MULT2, 'mult_t~seq_num_t + size_t + mult_t1 + mult_t2+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% regmult2 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       1.4866     0.51545      2.8841     0.0041519
%     mult_t1          0.21464    0.043337      4.9528    1.1088e-06
%     mult_t2         0.076126    0.037956      2.0056      0.045614
%     seq_num_t      0.0024455    0.021373     0.11442       0.90896
%     vint91                 0           0         NaN           NaN
%     vint92                 0           0         NaN           NaN
%     vint93                 0           0         NaN           NaN
%     vint94             2.611     0.79722      3.2751     0.0011547
%     vint95          -0.75214     0.56159     -1.3393       0.18128
%     vint96           -0.4933     0.59114    -0.83449       0.40453
%     vint97           -1.0115     0.51954     -1.9469      0.052293
%     vint98           -0.8642      0.4842     -1.7848      0.075104
%     vint99          -0.71099     0.48625     -1.4622       0.14453
%     vint00          -0.31336     0.47672    -0.65732       0.51138
%     vint01         -0.079954     0.49551    -0.16136        0.8719
%     vint02          -0.44061     0.50553    -0.87157         0.384
%     vint03          -0.49977     0.48135     -1.0383       0.29982
%     vint04          -0.51019     0.48324     -1.0558       0.29176
%     vint05          -0.60613     0.47197     -1.2843       0.19984
%     vint06           -0.6089     0.47239      -1.289        0.1982
%     vint07          -0.42759     0.47449    -0.90116       0.36808
%     vint08           -0.4574     0.47477    -0.96342       0.33596
%     vint09          -0.36382     0.47969    -0.75844       0.44867
%     vint10          -0.22205     0.47765    -0.46489       0.64228
%     size_t          0.024879    0.026341     0.94452       0.34551
% 
% 
% Number of observations: 400, Error degrees of freedom: 378
% Root Mean Squared Error: 0.645
% R-squared: 0.189,  Adjusted R-Squared 0.144
% F-statistic vs. constant model: 4.2, p-value = 5.03e-09
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult2.NumObservations;
k = regmult2.NumEstimatedCoefficients;
u = regmult2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult2.Residuals.Raw);
Stdev_u = std(regmult2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 1.6926e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9663
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
BUYOUT_MULT2.uRaw = regmult2.Residuals.Raw; 
BUYOUT_MULT2.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult2White = table(BUYOUT_MULT2.mult_t, BUYOUT_MULT2.seq_num_t, BUYOUT_MULT2.size_t, BUYOUT_MULT2.mult_t1, BUYOUT_MULT2.mult_t2, BUYOUT_MULT2.vint91, BUYOUT_MULT2.vint92, BUYOUT_MULT2.vint93, BUYOUT_MULT2.vint94, ...
    BUYOUT_MULT2.vint95, BUYOUT_MULT2.vint96, BUYOUT_MULT2.vint97, BUYOUT_MULT2.vint98, BUYOUT_MULT2.vint99,BUYOUT_MULT2.vint00, BUYOUT_MULT2.vint01, BUYOUT_MULT2.vint02, BUYOUT_MULT2.vint03, BUYOUT_MULT2.vint04,...
    BUYOUT_MULT2.vint05, BUYOUT_MULT2.vint06, BUYOUT_MULT2.vint07, BUYOUT_MULT2.vint08, BUYOUT_MULT2.vint09, BUYOUT_MULT2.vint10, BUYOUT_MULT2.size_t.^2, BUYOUT_MULT2.mult_t1.^2, BUYOUT_MULT2.mult_t2.^2,  BUYOUT_MULT2.uRaw2);
 
mult2White.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'mult_t2','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq', 'mult_t2sq', 'uRaw2'};
 
% Unrestricted regression
regmult2White=fitlm(mult2White,'uRaw2 ~ seq_num_t + size_t + mult_t1 + mult_t2+ vint91 + vint92+vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq + mult_t2sq')
 
% regmult2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE        tStat       pValue  
%                    __________    ________    ________    _________
% 
%     (Intercept)             0           0         NaN          NaN
%     seq_num_t       -0.013284    0.042563    -0.31211      0.75513
%     size_t           -0.61808     0.35079      -1.762     0.078898
%     mult_t1          -0.35407     0.19348       -1.83     0.068046
%     mult_t2           0.11526     0.19579     0.58869      0.55642
%     vint91                  0           0         NaN          NaN
%     vint92                  0           0         NaN          NaN
%     vint93             3.7198        1.55      2.3999     0.016889
%     vint94             2.0012      1.6854      1.1874      0.23583
%     vint95             2.5099      1.3133      1.9112     0.056751
%     vint96             3.2951      1.4429      2.2837     0.022955
%     vint97             3.3354      1.3285      2.5107     0.012473
%     vint98             3.1218      1.2873       2.425     0.015784
%     vint99             3.0672      1.2745      2.4067     0.016586
%     vint00             2.8884      1.2544      2.3026     0.021852
%     vint01             3.1384      1.2724      2.4665     0.014092
%     vint02             2.9053      1.2793       2.271     0.023719
%     vint03             2.9071      1.2458      2.3335     0.020154
%     vint04             3.0353      1.2684       2.393     0.017204
%     vint05              3.404      1.2511      2.7208    0.0068183
%     vint06             3.0081      1.2449      2.4163      0.01616
%     vint07             3.0103      1.2222       2.463      0.01423
%     vint08             3.0043      1.2484      2.4065     0.016594
%     vint09             2.9466      1.2407      2.3749      0.01806
%     vint10             3.6866      1.2725      2.8971    0.0039888
%     size_sq          0.036383    0.026059      1.3961       0.1635
%     mult_t1sq          0.0527    0.032047      1.6445      0.10092
%     mult_t2sq      0.00046308    0.030643    0.015112      0.98795
% 
% 
% Number of observations: 400, Error degrees of freedom: 375
% Root Mean Squared Error: 1.24
% R-squared: 0.0745,  Adjusted R-Squared 0.0152
% F-statistic vs. constant model: 1.26, p-value = 0.189
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  29.7891, Crit_Val_e =  32.6706, P_e = 0.0963,
%reject_e = 0
 
% Cannot reject H0 at 5%, so there is no heteroscedasticity
% No, the second assumption of CLRM is not violated.
 
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(BUYOUT_MULT2.seq_num_t,u)
corr_size_t_u = corr(BUYOUT_MULT2.size_t,u)
corr_mult_t1_u = corr(BUYOUT_MULT2.mult_t1,u)
corr_mult_t2_u = corr(BUYOUT_MULT2.mult_t2,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 1.6131
% kurt_jbtest = 11.1006
% test_stat_jbtest = 1.2671e+03
% crit_val_jbtest = 35.4789
% p_jbtest = 0 
% reject_jbstest = 1
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 

% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult2.Diagnostics.CooksDistance >=0.05);
Outliers = BUYOUT_MULT2(regmult2.Diagnostics.CooksDistance >=0.05,:);
 
 
%% Section 5.1 venture capital, irr, t-1 - t
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','VC funds for t-1 - t, irr','A2:AS506');
stringVectors = string(raw(:,[4,5,6,8,12,13,14,15,16,17,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,18,19,20,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
VENTURE_IRR1 = table;

% Allocate imported array to column variable names
VENTURE_IRR1.number = data(:,1);
VENTURE_IRR1.FUNDID = data(:,2);
VENTURE_IRR1.FIRMID = data(:,3);
VENTURE_IRR1.teamID = stringVectors(:,1);
VENTURE_IRR1.teamIDvintage = stringVectors(:,2);
VENTURE_IRR1.teamIDvintagenumber = stringVectors(:,3);
VENTURE_IRR1.VINTAGEYEAR = data(:,4);
VENTURE_IRR1.STRATEGY = categorical(stringVectors(:,4));
VENTURE_IRR1.BuyoutDummy = data(:,5);
VENTURE_IRR1.irr_t = data(:,6);
VENTURE_IRR1.irr_t1 = data(:,7);
VENTURE_IRR1.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
VENTURE_IRR1.COUNTRIES = categorical(stringVectors(:,6));
VENTURE_IRR1.REGIONS = categorical(stringVectors(:,7));
VENTURE_IRR1.STATUS = categorical(stringVectors(:,8));
VENTURE_IRR1.FUNDMANAGER = stringVectors(:,9);
VENTURE_IRR1.DOMICILE = categorical(stringVectors(:,10));
VENTURE_IRR1.FINALCLOSESIZE = data(:,8);
VENTURE_IRR1.size_ = data(:,9);
VENTURE_IRR1.seq_num_t = data(:,10);
VENTURE_IRR1.COREINDUSTRIES = categorical(stringVectors(:,11));
VENTURE_IRR1.INDUSTRIES = stringVectors(:,12);
VENTURE_IRR1.MANAGERREGION = categorical(stringVectors(:,13));
VENTURE_IRR1.MANAGERCOUNTRY = categorical(stringVectors(:,14));
VENTURE_IRR1.vint90 = data(:,11);
VENTURE_IRR1.vint91 = data(:,12);
VENTURE_IRR1.vint92 = data(:,13);
VENTURE_IRR1.vint93 = data(:,14);
VENTURE_IRR1.vint94 = data(:,15);
VENTURE_IRR1.vint95 = data(:,16);
VENTURE_IRR1.vint96 = data(:,17);
VENTURE_IRR1.vint97 = data(:,18);
VENTURE_IRR1.vint98 = data(:,19);
VENTURE_IRR1.vint99 = data(:,20);
VENTURE_IRR1.vint00 = data(:,21);
VENTURE_IRR1.vint01 = data(:,22);
VENTURE_IRR1.vint02 = data(:,23);
VENTURE_IRR1.vint03 = data(:,24);
VENTURE_IRR1.vint04 = data(:,25);
VENTURE_IRR1.vint05 = data(:,26);
VENTURE_IRR1.vint06 = data(:,27);
VENTURE_IRR1.vint07 = data(:,28);
VENTURE_IRR1.vint08 = data(:,29);
VENTURE_IRR1.vint09 = data(:,30);
VENTURE_IRR1.vint10 = data(:,31);

% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
VENTURE_IRR1.size_t=log(VENTURE_IRR1.size_);
 
% Linear regression reg1irr
 
regirr1 = fitlm(VENTURE_IRR1, 'irr_t~seq_num_t + size_t + irr_t1 +  vint91 + vint92 + vint93  + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
 
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat       pValue  
%                    ________    ________    _________    _________
% 
%     (Intercept)      25.823      35.984      0.71763      0.47334
%     irr_t1          0.10473    0.036584       2.8627    0.0043838
%     seq_num_t      -0.83328     0.83079       -1.003      0.31636
%     vint91          -20.366      50.301     -0.40489      0.68574
%     vint92          -13.048      38.959     -0.33492      0.73783
%     vint93          -12.321      50.279     -0.24505      0.80652
%     vint94           17.216      37.136      0.46359      0.64315
%     vint95           42.779      37.306       1.1467      0.25207
%     vint96            10.82      36.814      0.29391      0.76895
%     vint97            20.23      36.173      0.55925      0.57625
%     vint98         -0.88373      36.052    -0.024513      0.98045
%     vint99          -32.763      35.985     -0.91047      0.36303
%     vint00          -28.609      35.904     -0.79682      0.42595
%     vint01          -23.189      36.072     -0.64284      0.52063
%     vint02          -22.492      36.343     -0.61889      0.53628
%     vint03          -25.044      36.551     -0.68518      0.49356
%     vint04          -24.776      36.391     -0.68083      0.49631
%     vint05          -19.291      36.289      -0.5316      0.59525
%     vint06          -20.885      36.045     -0.57941      0.56258
%     vint07          -16.401      35.991     -0.45569      0.64882
%     vint08           -17.27      35.984     -0.47994      0.63149
%     vint09          -10.406      36.863     -0.28229      0.77784
%     vint10          -12.361      36.426     -0.33934       0.7345
%     size_t          0.43364       1.364      0.31791      0.75069
% 
% 
% Number of observations: 505, Error degrees of freedom: 481
% Root Mean Squared Error: 35.5
% R-squared: 0.209,  Adjusted R-Squared 0.171
% F-statistic vs. constant model: 5.52, p-value = 3.17e-14
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -8.7053e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9649
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
VENTURE_IRR1.uRaw = regirr1.Residuals.Raw; 
VENTURE_IRR1.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(VENTURE_IRR1.irr_t, VENTURE_IRR1.seq_num_t, VENTURE_IRR1.size_t, VENTURE_IRR1.irr_t1, VENTURE_IRR1.vint91, VENTURE_IRR1.vint92, VENTURE_IRR1.vint93, VENTURE_IRR1.vint94, ...
    VENTURE_IRR1.vint95, VENTURE_IRR1.vint96, VENTURE_IRR1.vint97, VENTURE_IRR1.vint98, VENTURE_IRR1.vint99,VENTURE_IRR1.vint00, VENTURE_IRR1.vint01, VENTURE_IRR1.vint02, VENTURE_IRR1.vint03, VENTURE_IRR1.vint04,...
    VENTURE_IRR1.vint05, VENTURE_IRR1.vint06, VENTURE_IRR1.vint07, VENTURE_IRR1.vint08, VENTURE_IRR1.vint09, VENTURE_IRR1.vint10, VENTURE_IRR1.size_t.^2, VENTURE_IRR1.irr_t1.^2, VENTURE_IRR1.uRaw2);
 
mult1White.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + irr_t1 + vint91 +vint92+vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
% 
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat       pValue 
%                    ________    ________    _________    ________
% 
%     (Intercept)     308.01        11480      0.02683     0.97861
%     seq_num_t       16.659       244.46     0.068145      0.9457
%     size_t         -362.76       2094.7     -0.17318     0.86258
%     irr_t1          37.586       20.319       1.8498    0.064965
%     vint91          970.71        14616     0.066412     0.94708
%     vint92          397.58        11319     0.035126     0.97199
%     vint93          998.28        14613     0.068316     0.94556
%     vint94          1669.3        10788      0.15474     0.87709
%     vint95          2951.9        10838      0.27236     0.78546
%     vint96          1865.1        10698      0.17435     0.86167
%     vint97          2558.6        10508      0.24349     0.80773
%     vint98          8488.1        10475      0.81032     0.41816
%     vint99          132.54        10460     0.012672     0.98989
%     vint00          573.14        10448     0.054857     0.95628
%     vint01          1318.4        10497       0.1256      0.9001
%     vint02           920.8        10570     0.087113     0.93062
%     vint03          1162.2        10629      0.10934     0.91298
%     vint04          1014.2        10580      0.09586     0.92367
%     vint05          1212.1        10547      0.11493     0.90855
%     vint06          1276.4        10485      0.12174     0.90316
%     vint07            1212        10463      0.11584     0.90783
%     vint08          1575.8        10470      0.15051     0.88042
%     vint09            1189        10723      0.11088     0.91176
%     vint10          1002.1        10590     0.094623     0.92465
%     size_sq         11.929       226.11     0.052757     0.95795
%     irr_t1sq       0.00036     0.055612    0.0064735     0.99484
% 
% 
% Number of observations: 505, Error degrees of freedom: 479
% Root Mean Squared Error: 1.03e+04
% R-squared: 0.0744,  Adjusted R-Squared 0.0261
% F-statistic vs. constant model: 1.54, p-value = 0.0471
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  37.5733, Crit_Val_e =  35.1725, P_e = 0.0283, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [VENTURE_IRR1.seq_num_t VENTURE_IRR1.size_t VENTURE_IRR1.irr_t1 ...
    VENTURE_IRR1.vint91 VENTURE_IRR1.vint92 VENTURE_IRR1.vint93 VENTURE_IRR1.vint94 VENTURE_IRR1.vint95 VENTURE_IRR1.vint96 VENTURE_IRR1.vint97...
    VENTURE_IRR1.vint98 VENTURE_IRR1.vint99 VENTURE_IRR1.vint00 VENTURE_IRR1.vint01 VENTURE_IRR1.vint02...
    VENTURE_IRR1.vint03 VENTURE_IRR1.vint04 VENTURE_IRR1.vint05 VENTURE_IRR1.vint06 VENTURE_IRR1.vint07 VENTURE_IRR1.vint08...
    VENTURE_IRR1.vint09 VENTURE_IRR1.vint10];
 
y = VENTURE_IRR1.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', ...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White
% White =
% 
%   24?4 table
% 
%                      beta         SE         tstat          p     
%                    ________    ________    _________    __________
% 
%     (Intercept)      25.823      5.7934       4.4573    1.0241e-05
%     seq_num_t      -0.83328      0.5456      -1.5273       0.12732
%     size_t          0.43364      1.2494      0.34707       0.72869
%     irr_t1          0.10473    0.065981       1.5873       0.11308
%     vint91          -20.366      1.6832        -12.1             0
%     vint92          -13.048       3.328      -3.9207    0.00010053
%     vint93          -12.321       2.004      -6.1479    1.6002e-09
%     vint94           17.216      12.294       1.4004       0.16201
%     vint95           42.779      17.863       2.3948      0.016993
%     vint96            10.82      12.511      0.86489       0.38751
%     vint97            20.23      10.268       1.9702      0.049363
%     vint98         -0.88373      14.991    -0.058952       0.95301
%     vint99          -32.763      4.8955      -6.6925    5.8685e-11
%     vint00          -28.609      2.5654      -11.152             0
%     vint01          -23.189      2.5202      -9.2012             0
%     vint02          -22.492      2.2916      -9.8151             0
%     vint03          -25.044      3.2506      -7.7043     7.061e-14
%     vint04          -24.776      2.7437      -9.0302             0
%     vint05          -19.291      4.9235      -3.9182    0.00010155
%     vint06          -20.885      2.4274      -8.6037             0
%     vint07          -16.401      2.5052      -6.5465    1.4551e-10
%     vint08           -17.27      3.1846      -5.4229    9.1201e-08
%     vint09          -10.406      4.6836      -2.2218      0.026743
%     vint10          -12.361      3.6346      -3.4009    0.00072507
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(VENTURE_IRR1.seq_num_t,u)
corr_size_t_u = corr(VENTURE_IRR1.size_t,u)
corr_irr_t1_u = corr(VENTURE_IRR1.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 6.3775
% kurt_jbtest = 76.1455
% test_stat_jbtest = 1.1600e+05
% crit_val_jbtest = 38.0756
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = VENTURE_IRR1(regirr1.Diagnostics.CooksDistance >=0.05,:);


%% Section 5.1 buyout, irr, t-1 - t
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Buyout funds for t-1 - t, irr','A2:AS639');
stringVectors = string(raw(:,[4,5,6,8,12,13,14,15,16,17,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,18,19,20,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
BUYOUT_IRR1 = table;

% Allocate imported array to column variable names
BUYOUT_IRR1.number = data(:,1);
BUYOUT_IRR1.FUNDID = data(:,2);
BUYOUT_IRR1.FIRMID = data(:,3);
BUYOUT_IRR1.teamID = stringVectors(:,1);
BUYOUT_IRR1.teamIDvintage = stringVectors(:,2);
BUYOUT_IRR1.teamIDvintagenumber = stringVectors(:,3);
BUYOUT_IRR1.VINTAGEYEAR = data(:,4);
BUYOUT_IRR1.STRATEGY = categorical(stringVectors(:,4));
BUYOUT_IRR1.BuyoutDummy = data(:,5);
BUYOUT_IRR1.irr_t = data(:,6);
BUYOUT_IRR1.irr_t1 = data(:,7);
BUYOUT_IRR1.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
BUYOUT_IRR1.COUNTRIES = stringVectors(:,6);
BUYOUT_IRR1.REGIONS = stringVectors(:,7);
BUYOUT_IRR1.STATUS = categorical(stringVectors(:,8));
BUYOUT_IRR1.FUNDMANAGER = stringVectors(:,9);
BUYOUT_IRR1.DOMICILE = categorical(stringVectors(:,10));
BUYOUT_IRR1.FINALCLOSESIZE = data(:,8);
BUYOUT_IRR1.size_ = data(:,9);
BUYOUT_IRR1.seq_num_t = data(:,10);
BUYOUT_IRR1.COREINDUSTRIES = categorical(stringVectors(:,11));
BUYOUT_IRR1.INDUSTRIES = stringVectors(:,12);
BUYOUT_IRR1.MANAGERREGION = categorical(stringVectors(:,13));
BUYOUT_IRR1.MANAGERCOUNTRY = categorical(stringVectors(:,14));
BUYOUT_IRR1.vint90 = data(:,11);
BUYOUT_IRR1.vint91 = data(:,12);
BUYOUT_IRR1.vint92 = data(:,13);
BUYOUT_IRR1.vint93 = data(:,14);
BUYOUT_IRR1.vint94 = data(:,15);
BUYOUT_IRR1.vint95 = data(:,16);
BUYOUT_IRR1.vint96 = data(:,17);
BUYOUT_IRR1.vint97 = data(:,18);
BUYOUT_IRR1.vint98 = data(:,19);
BUYOUT_IRR1.vint99 = data(:,20);
BUYOUT_IRR1.vint00 = data(:,21);
BUYOUT_IRR1.vint01 = data(:,22);
BUYOUT_IRR1.vint02 = data(:,23);
BUYOUT_IRR1.vint03 = data(:,24);
BUYOUT_IRR1.vint04 = data(:,25);
BUYOUT_IRR1.vint05 = data(:,26);
BUYOUT_IRR1.vint06 = data(:,27);
BUYOUT_IRR1.vint07 = data(:,28);
BUYOUT_IRR1.vint08 = data(:,29);
BUYOUT_IRR1.vint09 = data(:,30);
BUYOUT_IRR1.vint10 = data(:,31);

% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
BUYOUT_IRR1.size_t=log(BUYOUT_IRR1.size_);
 
% Linear regression reg1irr
 
regirr1 = fitlm(BUYOUT_IRR1, 'irr_t~seq_num_t + size_t + irr_t1 +  vint91 + vint92 + vint93  + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
 
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat        pValue  
%                    ________    ________    ________    __________
% 
%     (Intercept)     50.175       15.149      3.3121    0.00098027
%     irr_t1         0.15939     0.030824      5.1709    3.1579e-07
%     seq_num_t      0.66542      0.44963      1.4799        0.1394
%     vint91               0            0         NaN           NaN
%     vint92         -31.568       18.234     -1.7313      0.083894
%     vint93         -4.5039       17.168    -0.26234       0.79315
%     vint94         -21.267       15.919     -1.3359       0.18207
%     vint95         -28.532       15.348     -1.8591      0.063498
%     vint96         -24.362       15.446     -1.5772       0.11526
%     vint97          -41.72       15.146     -2.7545     0.0060522
%     vint98         -43.989       15.111      -2.911     0.0037336
%     vint99         -36.733        15.14     -2.4263      0.015541
%     vint00         -28.718       15.083      -1.904      0.057383
%     vint01         -18.553       15.207       -1.22       0.22292
%     vint02         -26.401       15.218     -1.7349      0.083266
%     vint03         -28.073       15.171     -1.8504      0.064733
%     vint04         -31.785       15.157      -2.097      0.036398
%     vint05         -34.541       15.013     -2.3007      0.021742
%     vint06         -38.026       15.036     -2.5291      0.011686
%     vint07         -35.667       15.037      -2.372      0.017999
%     vint08         -33.276       15.074     -2.2075      0.027649
%     vint09         -30.579       15.195     -2.0125      0.044606
%     vint10         -33.233       15.121     -2.1978      0.028338
%     size_t         -1.1561      0.50508      -2.289      0.022418
% 
% 
% Number of observations: 638, Error degrees of freedom: 615
% Root Mean Squared Error: 14.9
% R-squared: 0.194,  Adjusted R-Squared 0.165
% F-statistic vs. constant model: 6.71, p-value = 3.94e-18
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -2.4902e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9638
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
BUYOUT_IRR1.uRaw = regirr1.Residuals.Raw; 
BUYOUT_IRR1.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(BUYOUT_IRR1.irr_t, BUYOUT_IRR1.seq_num_t, BUYOUT_IRR1.size_t, BUYOUT_IRR1.irr_t1, BUYOUT_IRR1.vint91, BUYOUT_IRR1.vint92, BUYOUT_IRR1.vint93, BUYOUT_IRR1.vint94, ...
    BUYOUT_IRR1.vint95, BUYOUT_IRR1.vint96, BUYOUT_IRR1.vint97, BUYOUT_IRR1.vint98, BUYOUT_IRR1.vint99,BUYOUT_IRR1.vint00, BUYOUT_IRR1.vint01, BUYOUT_IRR1.vint02, BUYOUT_IRR1.vint03, BUYOUT_IRR1.vint04,...
    BUYOUT_IRR1.vint05, BUYOUT_IRR1.vint06, BUYOUT_IRR1.vint07, BUYOUT_IRR1.vint08, BUYOUT_IRR1.vint09, BUYOUT_IRR1.vint10, BUYOUT_IRR1.size_t.^2, BUYOUT_IRR1.irr_t1.^2, BUYOUT_IRR1.uRaw2);
 
mult1White.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + irr_t1 + vint91 +vint92 + vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
% 
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat      pValue  
%                    ________    ________    _______    _________
% 
%     (Intercept)      1923.3      945.52     2.0341     0.042371
%     seq_num_t        39.473      23.611     1.6718     0.095073
%     size_t           -584.3      180.49    -3.2373    0.0012721
%     irr_t1          -2.7471      2.5545    -1.0754      0.28263
%     vint91                0           0        NaN          NaN
%     vint92           287.32      924.81    0.31068      0.75615
%     vint93           975.99       870.5     1.1212      0.26265
%     vint94           482.53      807.31     0.5977      0.55026
%     vint95           347.68      778.11    0.44683      0.65515
%     vint96           1731.7      783.13     2.2112      0.02739
%     vint97           446.58       768.1    0.58142      0.56117
%     vint98           571.93      767.11    0.74557      0.45621
%     vint99           329.29      768.73    0.42836      0.66854
%     vint00           164.06       765.6    0.21428       0.8304
%     vint01            468.3       771.9    0.60668      0.54429
%     vint02           377.99      772.19     0.4895      0.62466
%     vint03           307.02       769.5    0.39898      0.69005
%     vint04           392.87      768.86    0.51098      0.60955
%     vint05           384.59      761.74    0.50488      0.61382
%     vint06           230.94      762.65    0.30282      0.76213
%     vint07           182.77      763.13     0.2395       0.8108
%     vint08           229.89      764.86    0.30057      0.76385
%     vint09           178.93      771.17    0.23202       0.8166
%     vint10           244.93      767.73    0.31904      0.74981
%     size_sq          37.699      13.994     2.6939    0.0072565
%     irr_t1sq       0.021196    0.018106     1.1706       0.2422
% 
% 
% Number of observations: 638, Error degrees of freedom: 613
% Root Mean Squared Error: 753
% R-squared: 0.132,  Adjusted R-Squared 0.0976
% F-statistic vs. constant model: 3.87, p-value = 3.06e-09
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  83.9554, Crit_Val_e =  33.9244, P_e = 3.5809e-09, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [BUYOUT_IRR1.seq_num_t BUYOUT_IRR1.size_t BUYOUT_IRR1.irr_t1 ...
    BUYOUT_IRR1.vint92 BUYOUT_IRR1.vint93 BUYOUT_IRR1.vint94 BUYOUT_IRR1.vint95 BUYOUT_IRR1.vint96 BUYOUT_IRR1.vint97...
    BUYOUT_IRR1.vint98 BUYOUT_IRR1.vint99 BUYOUT_IRR1.vint00 BUYOUT_IRR1.vint01 BUYOUT_IRR1.vint02...
    BUYOUT_IRR1.vint03 BUYOUT_IRR1.vint04 BUYOUT_IRR1.vint05 BUYOUT_IRR1.vint06 BUYOUT_IRR1.vint07 BUYOUT_IRR1.vint08...
    BUYOUT_IRR1.vint09 BUYOUT_IRR1.vint10];
 
y = BUYOUT_IRR1.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', ...
    'vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White
% White =
% 
%   23?4 table
% 
%                     beta         SE        tstat          p     
%                    _______    ________    ________    __________
% 
%     (Intercept)     50.175      4.0865      12.278             0
%     seq_num_t      0.66542     0.51681      1.2876       0.19837
%     size_t         -1.1561      0.5533     -2.0895      0.037057
%     irr_t1         0.15939    0.059846      2.6633     0.0079342
%     vint92         -31.568       2.502     -12.617             0
%     vint93         -4.5039      17.179    -0.26218       0.79327
%     vint94         -21.267      7.3963     -2.8753     0.0041709
%     vint95         -28.532      4.5333     -6.2939    5.7677e-10
%     vint96         -24.362      11.844     -2.0569      0.040107
%     vint97          -41.72      3.8813     -10.749             0
%     vint98         -43.989      3.4656     -12.693             0
%     vint99         -36.733      3.6634     -10.027             0
%     vint00         -28.718      2.9462     -9.7474             0
%     vint01         -18.553      4.5476     -4.0798    5.0798e-05
%     vint02         -26.401      4.2002     -6.2857    6.0611e-10
%     vint03         -28.073      3.4895      -8.045    4.2188e-15
%     vint04         -31.785      3.6439     -8.7228             0
%     vint05         -34.541      2.8574     -12.088             0
%     vint06         -38.026       2.477     -15.352             0
%     vint07         -35.667      2.6017     -13.709             0
%     vint08         -33.276      2.8077     -11.852             0
%     vint09         -30.579      3.3437     -9.1453             0
%     vint10         -33.233      3.1517     -10.545             0    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(BUYOUT_IRR1.seq_num_t,u)
corr_size_t_u = corr(BUYOUT_IRR1.size_t,u)
corr_irr_t1_u = corr(BUYOUT_IRR1.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 0.9763
% kurt_jbtest = 14.7981
% test_stat_jbtest = 3.8016e+03
% crit_val_jbtest = 36.7807
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = BUYOUT_IRR1(regirr1.Diagnostics.CooksDistance >=0.05,:);



%% Section 5.1 venture capital, irr, t-2 - t-1 - t
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','VC funds for t-2-t-1-t, irr','A2:AT269');
stringVectors = string(raw(:,[4,5,6,8,13,14,15,16,17,18,22,23,24,25]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,19,20,21,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
VENTURE_IRR2 = table;

% Allocate imported array to column variable names
VENTURE_IRR2.number = data(:,1);
VENTURE_IRR2.FUNDID = data(:,2);
VENTURE_IRR2.FIRMID = data(:,3);
VENTURE_IRR2.teamID = stringVectors(:,1);
VENTURE_IRR2.teamIDvintage = stringVectors(:,2);
VENTURE_IRR2.teamIDvintagenumber = stringVectors(:,3);
VENTURE_IRR2.VINTAGEYEAR = data(:,4);
VENTURE_IRR2.STRATEGY = categorical(stringVectors(:,4));
VENTURE_IRR2.BuyoutDummy = data(:,5);
VENTURE_IRR2.irr_t = data(:,6);
VENTURE_IRR2.irr_t1 = data(:,7);
VENTURE_IRR2.irr_t2 = data(:,8);
VENTURE_IRR2.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
VENTURE_IRR2.COUNTRIES = categorical(stringVectors(:,6));
VENTURE_IRR2.REGIONS = stringVectors(:,7);
VENTURE_IRR2.STATUS = categorical(stringVectors(:,8));
VENTURE_IRR2.FUNDMANAGER = stringVectors(:,9);
VENTURE_IRR2.DOMICILE = categorical(stringVectors(:,10));
VENTURE_IRR2.FINALCLOSESIZE = data(:,9);
VENTURE_IRR2.size_ = data(:,10);
VENTURE_IRR2.seq_num_t = data(:,11);
VENTURE_IRR2.COREINDUSTRIES = categorical(stringVectors(:,11));
VENTURE_IRR2.INDUSTRIES = stringVectors(:,12);
VENTURE_IRR2.MANAGERREGION = categorical(stringVectors(:,13));
VENTURE_IRR2.MANAGERCOUNTRY = categorical(stringVectors(:,14));
VENTURE_IRR2.vint90 = data(:,12);
VENTURE_IRR2.vint91 = data(:,13);
VENTURE_IRR2.vint92 = data(:,14);
VENTURE_IRR2.vint93 = data(:,15);
VENTURE_IRR2.vint94 = data(:,16);
VENTURE_IRR2.vint95 = data(:,17);
VENTURE_IRR2.vint96 = data(:,18);
VENTURE_IRR2.vint97 = data(:,19);
VENTURE_IRR2.vint98 = data(:,20);
VENTURE_IRR2.vint99 = data(:,21);
VENTURE_IRR2.vint00 = data(:,22);
VENTURE_IRR2.vint01 = data(:,23);
VENTURE_IRR2.vint02 = data(:,24);
VENTURE_IRR2.vint03 = data(:,25);
VENTURE_IRR2.vint04 = data(:,26);
VENTURE_IRR2.vint05 = data(:,27);
VENTURE_IRR2.vint06 = data(:,28);
VENTURE_IRR2.vint07 = data(:,29);
VENTURE_IRR2.vint08 = data(:,30);
VENTURE_IRR2.vint09 = data(:,31);
VENTURE_IRR2.vint10 = data(:,32);

% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
VENTURE_IRR2.size_t=log(VENTURE_IRR2.size_);
 
% Linear regression reg2irr 
 
regirr2 = fitlm(VENTURE_IRR2, 'irr_t~seq_num_t + size_t + irr_t1 + irr_t2+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr2 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)       1.3593      26.289     0.051706       0.95881
%     irr_t1           0.20614    0.049887       4.1321    4.9496e-05
%     irr_t2         -0.030035    0.043112     -0.69667       0.48667
%     seq_num_t        -1.0415      1.1629     -0.89561       0.37135
%     vint91                 0           0          NaN           NaN
%     vint92                 0           0          NaN           NaN
%     vint93                 0           0          NaN           NaN
%     vint94            60.836      42.905       1.4179       0.15749
%     vint95            18.938      32.029      0.59127       0.55489
%     vint96            29.782      30.523      0.97572       0.33017
%     vint97            46.114      26.918       1.7131      0.087967
%     vint98            24.238      26.701      0.90775       0.36491
%     vint99           -23.194      26.107     -0.88843       0.37519
%     vint00           -14.438      25.701     -0.56179       0.57478
%     vint01           -4.5083      25.924     -0.17391       0.86208
%     vint02           -2.2218      26.599    -0.083528        0.9335
%     vint03           -4.7403      26.718     -0.17742       0.85933
%     vint04           -6.5599      26.909     -0.24378       0.80761
%     vint05           -4.9354      26.813     -0.18407       0.85411
%     vint06           -4.4502      25.896     -0.17185        0.8637
%     vint07           0.79257      25.654     0.030895       0.97538
%     vint08            5.0241      26.254      0.19137        0.8484
%     vint09            10.689      27.713      0.38572       0.70004
%     vint10            7.9503      27.274       0.2915       0.77092
%     size_t            1.8987      1.8253       1.0402       0.29928
% 
% 
% Number of observations: 268, Error degrees of freedom: 246
% Root Mean Squared Error: 35
% R-squared: 0.245,  Adjusted R-Squared 0.181
% F-statistic vs. constant model: 3.8, p-value = 1.84e-07
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr2.NumObservations;
k = regirr2.NumEstimatedCoefficients;
u = regirr2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr2.Residuals.Raw);
Stdev_u = std(regirr2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 6.8630e-16
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9697
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
VENTURE_IRR2.uRaw = regirr2.Residuals.Raw; 
VENTURE_IRR2.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr2White = table(VENTURE_IRR2.irr_t, VENTURE_IRR2.seq_num_t, VENTURE_IRR2.size_t, VENTURE_IRR2.irr_t1, VENTURE_IRR2.irr_t2, VENTURE_IRR2.vint91, VENTURE_IRR2.vint92, VENTURE_IRR2.vint93, VENTURE_IRR2.vint94, ...
    VENTURE_IRR2.vint95, VENTURE_IRR2.vint96, VENTURE_IRR2.vint97, VENTURE_IRR2.vint98, VENTURE_IRR2.vint99,VENTURE_IRR2.vint00, VENTURE_IRR2.vint01, VENTURE_IRR2.vint02, VENTURE_IRR2.vint03, VENTURE_IRR2.vint04,...
    VENTURE_IRR2.vint05, VENTURE_IRR2.vint06, VENTURE_IRR2.vint07, VENTURE_IRR2.vint08, VENTURE_IRR2.vint09, VENTURE_IRR2.vint10, VENTURE_IRR2.size_t.^2, VENTURE_IRR2.irr_t1.^2, VENTURE_IRR2.irr_t2.^2,  VENTURE_IRR2.uRaw2);
 
irr2White.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'irr_t2','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq', 'irr_t2sq', 'uRaw2'};
 
% Unrestricted regression
regirr2White=fitlm(irr2White,'uRaw2 ~ seq_num_t + size_t + irr_t1 + irr_t2 +vint91+vint92+vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq + irr_t2sq')
 
% regirr2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat         pValue  
%                    _________    ________    __________    __________
% 
%     (Intercept)      -6648.7       11054      -0.60148       0.54809
%     seq_num_t        -13.531      382.25     -0.035399       0.97179
%     size_t            2955.8      3092.9       0.95569       0.34019
%     irr_t1            126.39       34.54        3.6592    0.00031101
%     irr_t2            16.252      29.344       0.55383       0.58021
%     vint91                 0           0           NaN           NaN
%     vint92                 0           0           NaN           NaN
%     vint93                 0           0           NaN           NaN
%     vint94           -136.34       13988    -0.0097473       0.99223
%     vint95           -3052.1       10488      -0.29102       0.77129
%     vint96           -3679.4      9997.4      -0.36804       0.71317
%     vint97           -599.18      8819.2      -0.06794       0.94589
%     vint98            7288.6      8784.6       0.82971       0.40753
%     vint99             -3818      8571.2      -0.44544        0.6564
%     vint00           -186.22        8421     -0.022114       0.98238
%     vint01            1989.3      8502.1       0.23397        0.8152
%     vint02             684.2      8701.2      0.078633       0.93739
%     vint03            749.43      8729.4      0.085851       0.93166
%     vint04            1587.8      8804.8       0.18034       0.85704
%     vint05            613.31      8746.7       0.07012       0.94416
%     vint06              1584      8477.1       0.18686       0.85193
%     vint07            1086.3      8379.9       0.12964       0.89696
%     vint08            1512.1      8574.4       0.17635       0.86017
%     vint09            699.03        9058      0.077173       0.93855
%     vint10            220.38      8912.4      0.024728       0.98029
%     size_sq          -348.08      331.81       -1.0491       0.29521
%     irr_t1sq        -0.15804    0.087848       -1.7991      0.073265
%     irr_t2sq       -0.029368    0.072658      -0.40419       0.68643
% 
% 
% Number of observations: 268, Error degrees of freedom: 243
% Root Mean Squared Error: 1.14e+04
% R-squared: 0.153,  Adjusted R-Squared 0.0694
% F-statistic vs. constant model: 1.83, p-value = 0.0124
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  41.0292, Crit_Val_e =  32.6706, P_e = 0.0056, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [VENTURE_IRR2.seq_num_t VENTURE_IRR2.size_t VENTURE_IRR2.irr_t1 VENTURE_IRR2.irr_t2...
      VENTURE_IRR2.vint94  VENTURE_IRR2.vint95  VENTURE_IRR2.vint96  VENTURE_IRR2.vint97...
    VENTURE_IRR2.vint98 VENTURE_IRR2.vint99 VENTURE_IRR2.vint00 VENTURE_IRR2.vint01 VENTURE_IRR2.vint02...
    VENTURE_IRR2.vint03 VENTURE_IRR2.vint04 VENTURE_IRR2.vint05 VENTURE_IRR2.vint06 VENTURE_IRR2.vint07 VENTURE_IRR2.vint08...
    VENTURE_IRR2.vint09 VENTURE_IRR2.vint10];
 
y = VENTURE_IRR2.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'irr_t2' ...
    'vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
% 
% White =
% 
%   22?4 table
% 
%                      beta          SE        tstat         p     
%                    _________    ________    _______    __________
% 
%     (Intercept)       1.3593      4.5278    0.30021       0.76425
%     seq_num_t        -1.0415      0.7109    -1.4651       0.14408
%     size_t            1.8987     0.91021      2.086      0.037933
%     irr_t1           0.20614    0.075985     2.7129     0.0071059
%     irr_t2         -0.030035    0.026153    -1.1484       0.25182
%     vint94            60.836      1.5737     38.658             0
%     vint95            18.938       13.07     1.4489       0.14854
%     vint96            29.782      15.239     1.9544      0.051704
%     vint97            46.114      17.178     2.6844     0.0077212
%     vint98            24.238      22.931      1.057       0.29147
%     vint99           -23.194       5.731    -4.0471    6.8018e-05
%     vint00           -14.438      4.5575     -3.168     0.0017139
%     vint01           -4.5083       2.626    -1.7168      0.087184
%     vint02           -2.2218      2.6268    -0.8458       0.39843
%     vint03           -4.7403      4.2342    -1.1195       0.26393
%     vint04           -6.5599      4.1588    -1.5773        0.1159
%     vint05           -4.9354      3.3482    -1.4741       0.14165
%     vint06           -4.4502      3.2558    -1.3669       0.17282
%     vint07           0.79257      3.6167    0.21914       0.82671
%     vint08            5.0241      3.3402     1.5041       0.13374
%     vint09            10.689      7.3289     1.4585       0.14588
%     vint10            7.9503      5.3999     1.4723       0.14212
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(VENTURE_IRR2.seq_num_t,u)
corr_size_t_u = corr(VENTURE_IRR2.size_t,u)
corr_irr_t1_u = corr(VENTURE_IRR2.irr_t1,u)
corr_irr_t2_u = corr(VENTURE_IRR2.irr_t2,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 8.3523
% kurt_jbtest = 110.3896
% test_stat_jbtest = 1.3190e+05
% crit_val_jbtest = 35.4789
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr2.Diagnostics.CooksDistance >=0.05);
Outliers = VENTURE_IRR2(regirr2.Diagnostics.CooksDistance >=0.05,:);

    
%% Section 5.1 buyout, irr, t-2 - t-1 - t
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Buyout funds for t-2-t-1-t, irr','A2:AT338');
stringVectors = string(raw(:,[4,5,6,8,13,14,15,16,17,18,22,23,24,25]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,19,20,21,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
BUYOUT_IRR2 = table;

% Allocate imported array to column variable names
BUYOUT_IRR2.number = data(:,1);
BUYOUT_IRR2.FUNDID = data(:,2);
BUYOUT_IRR2.FIRMID = data(:,3);
BUYOUT_IRR2.teamID = stringVectors(:,1);
BUYOUT_IRR2.teamIDvintage = stringVectors(:,2);
BUYOUT_IRR2.teamIDvintagenumber = stringVectors(:,3);
BUYOUT_IRR2.VINTAGEYEAR = data(:,4);
BUYOUT_IRR2.STRATEGY = categorical(stringVectors(:,4));
BUYOUT_IRR2.BuyoutDummy = data(:,5);
BUYOUT_IRR2.irr_t = data(:,6);
BUYOUT_IRR2.irr_t1 = data(:,7);
BUYOUT_IRR2.irr_t2 = data(:,8);
BUYOUT_IRR2.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
BUYOUT_IRR2.COUNTRIES = stringVectors(:,6);
BUYOUT_IRR2.REGIONS = stringVectors(:,7);
BUYOUT_IRR2.STATUS = categorical(stringVectors(:,8));
BUYOUT_IRR2.FUNDMANAGER = stringVectors(:,9);
BUYOUT_IRR2.DOMICILE = stringVectors(:,10);
BUYOUT_IRR2.FINALCLOSESIZE = data(:,9);
BUYOUT_IRR2.size_ = data(:,10);
BUYOUT_IRR2.seq_num_t = data(:,11);
BUYOUT_IRR2.COREINDUSTRIES = categorical(stringVectors(:,11));
BUYOUT_IRR2.INDUSTRIES = categorical(stringVectors(:,12));
BUYOUT_IRR2.MANAGERREGION = categorical(stringVectors(:,13));
BUYOUT_IRR2.MANAGERCOUNTRY = categorical(stringVectors(:,14));
BUYOUT_IRR2.vint90 = data(:,12);
BUYOUT_IRR2.vint91 = data(:,13);
BUYOUT_IRR2.vint92 = data(:,14);
BUYOUT_IRR2.vint93 = data(:,15);
BUYOUT_IRR2.vint94 = data(:,16);
BUYOUT_IRR2.vint95 = data(:,17);
BUYOUT_IRR2.vint96 = data(:,18);
BUYOUT_IRR2.vint97 = data(:,19);
BUYOUT_IRR2.vint98 = data(:,20);
BUYOUT_IRR2.vint99 = data(:,21);
BUYOUT_IRR2.vint00 = data(:,22);
BUYOUT_IRR2.vint01 = data(:,23);
BUYOUT_IRR2.vint02 = data(:,24);
BUYOUT_IRR2.vint03 = data(:,25);
BUYOUT_IRR2.vint04 = data(:,26);
BUYOUT_IRR2.vint05 = data(:,27);
BUYOUT_IRR2.vint06 = data(:,28);
BUYOUT_IRR2.vint07 = data(:,29);
BUYOUT_IRR2.vint08 = data(:,30);
BUYOUT_IRR2.vint09 = data(:,31);
BUYOUT_IRR2.vint10 = data(:,32);

% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
BUYOUT_IRR2.size_t=log(BUYOUT_IRR2.size_);
 
% Linear regression reg2irr 
 
regirr2 = fitlm(BUYOUT_IRR2, 'irr_t~seq_num_t + size_t + irr_t1 + irr_t2+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr2 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue  
%                    ________    ________    _______    __________
% 
%     (Intercept)      48.998      13.806      3.549    0.00044616
%     irr_t1          0.17787    0.039285     4.5276    8.4982e-06
%     irr_t2         0.015601    0.039307    0.39689       0.69172
%     seq_num_t       0.93162     0.56047     1.6622      0.097471
%     vint91                0           0        NaN           NaN
%     vint92                0           0        NaN           NaN
%     vint93          -27.318      16.399    -1.6658      0.096755
%     vint94                0           0        NaN           NaN
%     vint95          -44.003      14.925    -2.9482     0.0034374
%     vint96          -40.818      15.453    -2.6414     0.0086716
%     vint97          -45.731      14.479    -3.1584     0.0017416
%     vint98          -51.619       13.87    -3.7217    0.00023472
%     vint99          -42.964      13.813    -3.1103     0.0020415
%     vint00          -36.062      13.672    -2.6376     0.0087674
%     vint01          -21.834      13.914    -1.5693        0.1176
%     vint02          -37.367      14.065    -2.6567     0.0082963
%     vint03          -36.462      13.774    -2.6471     0.0085295
%     vint04          -41.956      13.796    -3.0412     0.0025563
%     vint05          -44.033      13.627    -3.2313     0.0013638
%     vint06          -45.174      13.646    -3.3105     0.0010403
%     vint07          -41.267      13.648    -3.0236     0.0027053
%     vint08           -40.36      13.669    -2.9526       0.00339
%     vint09          -38.933      13.665    -2.8492     0.0046746
%     vint10          -38.556       13.68    -2.8184     0.0051344
%     size_t         -0.21664     0.60598    -0.3575       0.72096
% 
% 
% Number of observations: 337, Error degrees of freedom: 315
% Root Mean Squared Error: 13.3
% R-squared: 0.205,  Adjusted R-Squared 0.152
% F-statistic vs. constant model: 3.88, p-value = 6.51e-08
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr2.NumObservations;
k = regirr2.NumEstimatedCoefficients;
u = regirr2.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr2.Residuals.Raw);
Stdev_u = std(regirr2.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 7.7195e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9675
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
BUYOUT_IRR2.uRaw = regirr2.Residuals.Raw; 
BUYOUT_IRR2.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr2White = table(BUYOUT_IRR2.irr_t, BUYOUT_IRR2.seq_num_t, BUYOUT_IRR2.size_t, BUYOUT_IRR2.irr_t1, BUYOUT_IRR2.irr_t2, BUYOUT_IRR2.vint91, BUYOUT_IRR2.vint92, BUYOUT_IRR2.vint93, BUYOUT_IRR2.vint94, ...
    BUYOUT_IRR2.vint95, BUYOUT_IRR2.vint96, BUYOUT_IRR2.vint97, BUYOUT_IRR2.vint98, BUYOUT_IRR2.vint99,BUYOUT_IRR2.vint00, BUYOUT_IRR2.vint01, BUYOUT_IRR2.vint02, BUYOUT_IRR2.vint03, BUYOUT_IRR2.vint04,...
    BUYOUT_IRR2.vint05, BUYOUT_IRR2.vint06, BUYOUT_IRR2.vint07, BUYOUT_IRR2.vint08, BUYOUT_IRR2.vint09, BUYOUT_IRR2.vint10, BUYOUT_IRR2.size_t.^2, BUYOUT_IRR2.irr_t1.^2, BUYOUT_IRR2.irr_t2.^2,  BUYOUT_IRR2.uRaw2);
 
irr2White.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'irr_t2','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq', 'irr_t2sq', 'uRaw2'};
 
% Unrestricted regression
% regirr2White=fitlm(irr2White,'uRaw2 ~ seq_num_t + size_t + irr_t1 + irr_t2 +vint91 +vint92+vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq + irr_t2sq')
%  
% regirr2White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)            0           0         NaN           NaN
%     seq_num_t         73.244      19.455      3.7647    0.00019953
%     size_t            87.568      141.76     0.61773       0.53721
%     irr_t1           -1.2951      2.4906    -0.51999       0.60345
%     irr_t2           0.71369      2.5871     0.27587       0.78283
%     vint91                 0           0         NaN           NaN
%     vint92                 0           0         NaN           NaN
%     vint93            148.19      587.46     0.25225       0.80102
%     vint94           -537.96      626.02    -0.85934       0.39082
%     vint95           -373.02      509.38    -0.73231       0.46453
%     vint96           -107.38      555.92    -0.19316       0.84696
%     vint97            847.86       532.5      1.5922       0.11236
%     vint98           -114.98      511.26     -0.2249       0.82221
%     vint99           -193.87      512.05    -0.37862       0.70523
%     vint00           -226.96      495.72    -0.45784       0.64739
%     vint01            177.17      501.09     0.35356       0.72391
%     vint02           -192.46      502.75    -0.38281       0.70212
%     vint03           -150.76      496.48    -0.30366       0.76159
%     vint04            -67.01      502.16    -0.13344       0.89393
%     vint05           -97.526      497.38    -0.19608       0.84468
%     vint06           -260.17       496.5    -0.52401       0.60065
%     vint07           -289.11      489.95    -0.59009       0.55556
%     vint08           -219.12      499.53    -0.43865       0.66122
%     vint09           -310.35      490.55    -0.63266       0.52742
%     vint10           -236.15      503.58    -0.46895       0.63944
%     size_sq          -11.585      10.562     -1.0969       0.27355
%     irr_t1sq        0.015618    0.024785     0.63016       0.52905
%     irr_t2sq       -0.006612    0.026228     -0.2521       0.80113
% 
% 
% Number of observations: 337, Error degrees of freedom: 312
% Root Mean Squared Error: 443
% R-squared: 0.203,  Adjusted R-Squared 0.141
% F-statistic vs. constant model: 3.3, p-value = 7.51e-07
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr2White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  68.2546, Crit_Val_e =  32.6706, P_e = 6.6744e-07, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [BUYOUT_IRR2.seq_num_t BUYOUT_IRR2.size_t BUYOUT_IRR2.irr_t1 BUYOUT_IRR2.irr_t2...
    BUYOUT_IRR2.vint93  BUYOUT_IRR2.vint95  BUYOUT_IRR2.vint96  BUYOUT_IRR2.vint97...
    BUYOUT_IRR2.vint98 BUYOUT_IRR2.vint99 BUYOUT_IRR2.vint00 BUYOUT_IRR2.vint01 BUYOUT_IRR2.vint02...
    BUYOUT_IRR2.vint03 BUYOUT_IRR2.vint04 BUYOUT_IRR2.vint05 BUYOUT_IRR2.vint06 BUYOUT_IRR2.vint07 BUYOUT_IRR2.vint08...
    BUYOUT_IRR2.vint09 BUYOUT_IRR2.vint10];
 
y = BUYOUT_IRR2.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'irr_t2' ...
    'vint93','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
% White =
% 
%   22?4 table
% 
%                      beta         SE        tstat         p     
%                    ________    ________    _______    __________
% 
%     (Intercept)      48.998      3.3855     14.473             0
%     seq_num_t       0.93162     0.67318     1.3839       0.16731
%     size_t         -0.21664     0.57738    -0.3752       0.70775
%     irr_t1          0.17787    0.061369     2.8983     0.0039993
%     irr_t2         0.015601    0.043888    0.35546       0.72247
%     vint93          -27.318      16.452    -1.6605       0.09775
%     vint95          -44.003      3.0177    -14.582             0
%     vint96          -40.818      8.9012    -4.5857     6.399e-06
%     vint97          -45.731       14.86    -3.0774     0.0022601
%     vint98          -51.619       4.091    -12.618             0
%     vint99          -42.964      3.9277    -10.939             0
%     vint00          -36.062        2.99    -12.061             0
%     vint01          -21.834      7.1443    -3.0562     0.0024218
%     vint02          -37.367      4.3312    -8.6273    2.2204e-16
%     vint03          -36.462      3.9975    -9.1212             0
%     vint04          -41.956      4.8927    -8.5753    4.4409e-16
%     vint05          -44.033       3.908    -11.267             0
%     vint06          -45.174      2.9643     -15.24             0
%     vint07          -41.267      2.9066    -14.197             0
%     vint08           -40.36       3.091    -13.057             0
%     vint09          -38.933      2.2642    -17.195             0
%     vint10          -38.556      3.4045    -11.325             0
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(BUYOUT_IRR2.seq_num_t,u)
corr_size_t_u = corr(BUYOUT_IRR2.size_t,u)
corr_irr_t1_u = corr(BUYOUT_IRR2.irr_t1,u)
corr_irr_t2_u = corr(BUYOUT_IRR2.irr_t2,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 1.2981
% kurt_jbtest = 9.3185
% test_stat_jbtest = 655.2458
% crit_val_jbtest = 35.4789
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr2.Diagnostics.CooksDistance >=0.05);
Outliers = BUYOUT_IRR2(regirr2.Diagnostics.CooksDistance >=0.05,:);
 
%% Section 5.2.1 venture, mult

clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MSM Venture MULT t-1','A2:AH223');
stringVectors = string(raw(:,[4,5,6,9,13,14,15,16,17,18,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,10,11,12,19,20,25,26,27,28,29,30,31,32,33,34]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MSM_MULT = table;

% Allocate imported array to column variable names
MSM_MULT.number = data(:,1);
MSM_MULT.FUNDID = data(:,2);
MSM_MULT.FIRMID = data(:,3);
MSM_MULT.teamID = stringVectors(:,1);
MSM_MULT.teamIDvintage = stringVectors(:,2);
MSM_MULT.teamIDvintagenumber = stringVectors(:,3);
MSM_MULT.VINTAGEYEAR = data(:,4);
MSM_MULT.MSM = data(:,5);
MSM_MULT.STRATEGY = categorical(stringVectors(:,4));
MSM_MULT.BuyoutDummy = data(:,6);
MSM_MULT.mult_t = data(:,7);
MSM_MULT.mult_t1 = data(:,8);
MSM_MULT.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
MSM_MULT.COUNTRIES = categorical(stringVectors(:,6));
MSM_MULT.REGIONS = stringVectors(:,7);
MSM_MULT.STATUS = categorical(stringVectors(:,8));
MSM_MULT.FUNDMANAGER = stringVectors(:,9);
MSM_MULT.DOMICILE = categorical(stringVectors(:,10));
MSM_MULT.size_ = data(:,9);
MSM_MULT.seq_num_t = data(:,10);
MSM_MULT.COREINDUSTRIES = categorical(stringVectors(:,11));
MSM_MULT.INDUSTRIES = stringVectors(:,12);
MSM_MULT.MANAGERREGION = categorical(stringVectors(:,13));
MSM_MULT.MANAGERCOUNTRY = categorical(stringVectors(:,14));
MSM_MULT.vint01 = data(:,11);
MSM_MULT.vint02 = data(:,12);
MSM_MULT.vint03 = data(:,13);
MSM_MULT.vint04 = data(:,14);
MSM_MULT.vint05 = data(:,15);
MSM_MULT.vint06 = data(:,16);
MSM_MULT.vint07 = data(:,17);
MSM_MULT.vint08 = data(:,18);
MSM_MULT.vint09 = data(:,19);
MSM_MULT.vint10 = data(:,20);

% Clear temporary variables
clearvars data raw stringVectors;


% Log size
MSM_MULT.size_t=log(MSM_MULT.size_);
MSM_MULT.interact=MSM_MULT.MSM.*MSM_MULT.mult_t;
 
% Linear regression regmult venture
 
regmult = fitlm(MSM_MULT, 'mult_t~seq_num_t + size_t + mult_t1 +MSM +interact  ')
%  
% regmult = 
% 
% 
% Linear regression model:
%     mult_t ~ 1 + MSM + mult_t1 + seq_num_t + size_t + interact
% 
% Estimated Coefficients:
%                    Estimate         SE         tStat        pValue  
%                    _________    __________    ________    __________
% 
%     (Intercept)       1.9725        0.3199      6.1658     3.402e-09
%     MSM             -0.01088     0.0011726     -9.2784    1.8554e-17
%     mult_t1         0.052872      0.060265     0.87732       0.38129
%     seq_num_t      -0.011148      0.025578    -0.43586       0.66337
%     size_t         -0.042448      0.057357    -0.74006       0.46007
%     interact       0.0064277    0.00058108      11.062    7.9357e-23
% 
% 
% Number of observations: 222, Error degrees of freedom: 216
% Root Mean Squared Error: 1.08
% R-squared: 0.384,  Adjusted R-Squared 0.369
% F-statistic vs. constant model: 26.9, p-value = 4.26e-21 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult.NumObservations;
k = regmult.NumEstimatedCoefficients;
u = regmult.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult.Residuals.Raw);
Stdev_u = std(regmult.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 7.8052e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9710
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
MSM_MULT.uRaw = regmult.Residuals.Raw; 
MSM_MULT.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
multWhite = table(MSM_MULT.mult_t, MSM_MULT.seq_num_t, MSM_MULT.size_t, MSM_MULT.mult_t1, MSM_MULT.MSM, MSM_MULT.interact, MSM_MULT.size_t.^2, MSM_MULT.mult_t1.^2, MSM_MULT.MSM.^2,MSM_MULT.interact.^2, MSM_MULT.uRaw2);
 
multWhite.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'MSM','interact','size_sq','mult_t1sq', 'MSMsq','interactsq','uRaw2'};
 
% Unrestricted regression
regmultWhite=fitlm(multWhite,'uRaw2 ~ seq_num_t + size_t + mult_t1 +MSM+ interact +size_sq + mult_t1sq + MSMsq + interactsq')
%  
% regmultWhite = 
% 
% 
% Linear regression model:
%     uRaw2 ~ 1 + seq_num_t + size_t + mult_t1 + MSM + interact + size_sq + mult_t1sq + MSMsq + interactsq
% 
% Estimated Coefficients:
%                     Estimate          SE          tStat       pValue  
%                    ___________    __________    _________    _________
% 
%     (Intercept)         11.826        7.2571       1.6296      0.10468
%     seq_num_t        0.0099918       0.24514      0.04076      0.96753
%     size_t            -0.82615        3.0659     -0.26946      0.78784
%     mult_t1            -2.3225         1.173        -1.98        0.049
%     MSM               -0.11517      0.036837      -3.1264    0.0020181
%     interact          0.036855       0.01166       3.1609     0.001803
%     size_sq           -0.02468         0.327    -0.075474      0.93991
%     mult_t1sq          0.15575       0.10625       1.4658      0.14418
%     MSMsq            0.0002753    0.00012183       2.2597     0.024857
%     interactsq     -2.8885e-05    1.1886e-05      -2.4302      0.01592
% 
% 
% Number of observations: 222, Error degrees of freedom: 212
% Root Mean Squared Error: 10.2
% R-squared: 0.0944,  Adjusted R-Squared 0.056
% F-statistic vs. constant model: 2.46, p-value = 0.0111
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmultWhite.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  20.9645, Crit_Val_e =  11.0705, P_e = 8.2265e-04, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MSM_MULT.seq_num_t MSM_MULT.size_t MSM_MULT.mult_t1 MSM_MULT.interact MSM_MULT.MSM];
 
y = MSM_MULT.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'interact','MSM'};
White
 
% White =
% 
%   6?4 table
% 
%                      beta           SE         tstat          p     
%                    _________    __________    ________    __________
% 
%     (Intercept)       1.9725       0.56839      3.4703    0.00062553
%     seq_num_t      -0.011148      0.010958     -1.0174        0.3101
%     size_t         -0.042448       0.07381    -0.57509       0.56582
%     mult_t1         0.052872      0.067683     0.78117       0.43554
%     interact       0.0064277    0.00088131      7.2934     5.403e-12
%     MSM             -0.01088     0.0018626     -5.8413     1.847e-08
 
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MSM_MULT.seq_num_t,u)
corr_size_t_u = corr(MSM_MULT.size_t,u)
corr_mult_t1_u = corr(MSM_MULT.mult_t1,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 7.9001
% kurt_jbtest = 84.9746
% test_stat_jbtest = 6.4468e+04
% crit_val_jbtest = 12.8325
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult.Diagnostics.CooksDistance >=0.05);
Outliers = MSM_MULT(regmult.Diagnostics.CooksDistance >=0.05,:);
 
%% Section 5.2.1 venture, irr

clc
close all
clear all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MSM Venture IRR t-1','A2:AH187');
stringVectors = string(raw(:,[4,5,6,9,13,14,15,16,17,18,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,10,11,12,19,20,25,26,27,28,29,30,31,32,33,34]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MSM_IRR = table;

% Allocate imported array to column variable names
MSM_IRR.number = data(:,1);
MSM_IRR.FUNDID = data(:,2);
MSM_IRR.FIRMID = data(:,3);
MSM_IRR.teamID = stringVectors(:,1);
MSM_IRR.teamIDvintage = stringVectors(:,2);
MSM_IRR.teamIDvintagenumber = stringVectors(:,3);
MSM_IRR.VINTAGEYEAR = data(:,4);
MSM_IRR.MSM = data(:,5);
MSM_IRR.STRATEGY = categorical(stringVectors(:,4));
MSM_IRR.BuyoutDummy = data(:,6);
MSM_IRR.irr_t = data(:,7);
MSM_IRR.irr_t1 = data(:,8);
MSM_IRR.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
MSM_IRR.COUNTRIES = categorical(stringVectors(:,6));
MSM_IRR.REGIONS = categorical(stringVectors(:,7));
MSM_IRR.STATUS = categorical(stringVectors(:,8));
MSM_IRR.FUNDMANAGER = stringVectors(:,9);
MSM_IRR.DOMICILE = categorical(stringVectors(:,10));
MSM_IRR.size_ = data(:,9);
MSM_IRR.seq_num_t = data(:,10);
MSM_IRR.COREINDUSTRIES = categorical(stringVectors(:,11));
MSM_IRR.INDUSTRIES = stringVectors(:,12);
MSM_IRR.MANAGERREGION = categorical(stringVectors(:,13));
MSM_IRR.MANAGERCOUNTRY = categorical(stringVectors(:,14));
MSM_IRR.vint01 = data(:,11);
MSM_IRR.vint02 = data(:,12);
MSM_IRR.vint03 = data(:,13);
MSM_IRR.vint04 = data(:,14);
MSM_IRR.vint05 = data(:,15);
MSM_IRR.vint06 = data(:,16);
MSM_IRR.vint07 = data(:,17);
MSM_IRR.vint08 = data(:,18);
MSM_IRR.vint09 = data(:,19);
MSM_IRR.vint10 = data(:,20);

% Clear temporary variables
clearvars data raw stringVectors;

% Log size
MSM_IRR.size_t=log(MSM_IRR.size_);
MSM_IRR.interact=MSM_IRR.MSM.*MSM_IRR.irr_t;
 
% Linear regression regirr venture

regirr = fitlm(MSM_IRR, 'irr_t~seq_num_t + size_t + irr_t1 +MSM +interact  ')
%  
% regirr = 
% 
% 
% Linear regression model:
%     irr_t ~ 1 + MSM + irr_t1 + seq_num_t + size_t + interact
% 
% Estimated Coefficients:
%                    Estimate         SE         tStat        pValue  
%                    _________    __________    ________    __________
% 
%     (Intercept)       6.8478        3.0699      2.2306      0.026942
%     MSM             -0.04414     0.0084063     -5.2509    4.2371e-07
%     irr_t1           0.26041      0.042517       6.125    5.5597e-09
%     seq_num_t      0.0069881       0.26783    0.026092       0.97921
%     size_t           0.16112       0.56094     0.28723       0.77427
%     interact       0.0060424    0.00039851      15.162    5.3843e-34
% 
% 
% Number of observations: 186, Error degrees of freedom: 180
% Root Mean Squared Error: 9.42
% R-squared: 0.649,  Adjusted R-Squared 0.639
% F-statistic vs. constant model: 66.5, p-value = 4.53e-39

%DIAGNOSTICS of CLRM assumptions
 
T = regirr.NumObservations;
k = regirr.NumEstimatedCoefficients;
u = regirr.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr.Residuals.Raw);
Stdev_u = std(regirr.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 4.2599e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9732
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test


MSM_IRR.uRaw = regirr.Residuals.Raw; 
MSM_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irrWhite = table(MSM_IRR.irr_t, MSM_IRR.seq_num_t, MSM_IRR.size_t, MSM_IRR.irr_t1, MSM_IRR.MSM, MSM_IRR.interact, MSM_IRR.size_t.^2, MSM_IRR.irr_t1.^2, MSM_IRR.MSM.^2,MSM_IRR.interact.^2, MSM_IRR.uRaw2);
 
irrWhite.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'MSM','interact','size_sq','irr_t1sq', 'MSMsq','interactsq','uRaw2'};
 
% Unrestricted regression
regirrWhite=fitlm(irrWhite,'uRaw2 ~ seq_num_t + size_t + irr_t1 +MSM+ interact +size_sq + irr_t1sq + MSMsq + interactsq')

% regirrWhite = 
% 
% 
% Linear regression model:
%     uRaw2 ~ 1 + seq_num_t + size_t + irr_t1 + MSM + interact + size_sq + irr_t1sq + MSMsq + interactsq
% 
% Estimated Coefficients:
%                     Estimate          SE          tStat        pValue  
%                    ___________    __________    _________    __________
% 
%     (Intercept)         247.82        282.68      0.87667       0.38186
%     seq_num_t          -4.4277        10.322     -0.42897       0.66847
%     size_t             -3.1464        124.17    -0.025339       0.97981
%     irr_t1             -9.9232        1.7767      -5.5852    8.7228e-08
%     MSM                -2.2112        1.2274      -1.8015      0.073333
%     interact          0.022361       0.01748       1.2792       0.20252
%     size_sq            -1.1269        13.373    -0.084264       0.93294
%     irr_t1sq           0.17642      0.028773       6.1315    5.5737e-09
%     MSMsq            0.0063259     0.0044859       1.4102       0.16025
%     interactsq     -5.8023e-07    4.4491e-06     -0.13042       0.89639
% 
% 
% Number of observations: 186, Error degrees of freedom: 176
% Root Mean Squared Error: 358
% R-squared: 0.27,  Adjusted R-Squared 0.232
% F-statistic vs. constant model: 7.22, p-value = 6.66e-09

% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirrWhite.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  50.1567, Crit_Val_e =  11.0705, P_e = 1.2872e-09, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MSM_IRR.seq_num_t MSM_IRR.size_t MSM_IRR.irr_t1 MSM_IRR.interact MSM_IRR.MSM];
 
y = MSM_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'interact','MSM'};
White

% White =
% 
%   6?4 table
% 
%                      beta           SE         tstat          p    
%                    _________    __________    ________    _________
% 
%     (Intercept)       6.8478        3.7108      1.8454      0.06659
%     seq_num_t      0.0069881       0.15309    0.045648      0.96364
%     size_t           0.16112       0.47157     0.34166        0.733
%     irr_t1           0.26041       0.11299      2.3047     0.022301
%     interact       0.0060424    0.00045209      13.365            0
%     MSM             -0.04414      0.014604     -3.0225    0.0028643

%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MSM_IRR.seq_num_t,u)
corr_size_t_u = corr(MSM_IRR.size_t,u)
corr_irr_t1_u = corr(MSM_IRR.irr_t1,u)

%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = -2.0407
% kurt_jbtest = 23.2069
% test_stat_jbtest = 3.2936e+03
% crit_val_jbtest = 12.8325
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 

% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr.Diagnostics.CooksDistance >=0.05);
Outliers = MSM_IRR(regirr.Diagnostics.CooksDistance >=0.05,:);

%% Section 5.2.1 buyout, multiple

clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MSM Buyout MULT t-1','A2:AH321');
stringVectors = string(raw(:,[4,5,6,9,13,14,15,16,17,18,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,10,11,12,19,20,25,26,27,28,29,30,31,32,33,34]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MSM_MULT = table;

% Allocate imported array to column variable names
MSM_MULT.number = data(:,1);
MSM_MULT.FUNDID = data(:,2);
MSM_MULT.FIRMID = data(:,3);
MSM_MULT.teamID = stringVectors(:,1);
MSM_MULT.teamIDvintage = stringVectors(:,2);
MSM_MULT.teamIDvintagenumber = stringVectors(:,3);
MSM_MULT.VINTAGEYEAR = data(:,4);
MSM_MULT.MSM = data(:,5);
MSM_MULT.STRATEGY = categorical(stringVectors(:,4));
MSM_MULT.BuyoutDummy = data(:,6);
MSM_MULT.mult_t = data(:,7);
MSM_MULT.mult_t1 = data(:,8);
MSM_MULT.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
MSM_MULT.COUNTRIES = stringVectors(:,6);
MSM_MULT.REGIONS = stringVectors(:,7);
MSM_MULT.STATUS = categorical(stringVectors(:,8));
MSM_MULT.FUNDMANAGER = stringVectors(:,9);
MSM_MULT.DOMICILE = categorical(stringVectors(:,10));
MSM_MULT.size_ = data(:,9);
MSM_MULT.seq_num_t = data(:,10);
MSM_MULT.COREINDUSTRIES = categorical(stringVectors(:,11));
MSM_MULT.INDUSTRIES = stringVectors(:,12);
MSM_MULT.MANAGERREGION = categorical(stringVectors(:,13));
MSM_MULT.MANAGERCOUNTRY = categorical(stringVectors(:,14));
MSM_MULT.vint01 = data(:,11);
MSM_MULT.vint02 = data(:,12);
MSM_MULT.vint03 = data(:,13);
MSM_MULT.vint04 = data(:,14);
MSM_MULT.vint05 = data(:,15);
MSM_MULT.vint06 = data(:,16);
MSM_MULT.vint07 = data(:,17);
MSM_MULT.vint08 = data(:,18);
MSM_MULT.vint09 = data(:,19);
MSM_MULT.vint10 = data(:,20);

% Clear temporary variables
clearvars data raw stringVectors;

% Log size
MSM_MULT.size_t=log(MSM_MULT.size_);
MSM_MULT.interact=MSM_MULT.MSM.*MSM_MULT.mult_t;
 
% Linear regression regmult buyout
 
regmult = fitlm(MSM_MULT, 'mult_t~seq_num_t + size_t + mult_t1 +MSM +interact  ')
%  
% regmult = 
% 
% 
% Linear regression model:
%     mult_t ~ 1 + MSM + mult_t1 + seq_num_t + size_t + interact
% 
% Estimated Coefficients:
%                     Estimate         SE         tStat        pValue  
%                    __________    __________    ________    __________
% 
%     (Intercept)        1.6955       0.09955      17.032    1.5725e-46
%     MSM             -0.010467    0.00038161     -27.429    2.3216e-85
%     mult_t1          0.050728      0.025913      1.9577      0.051153
%     seq_num_t       0.0040297      0.010067     0.40031        0.6892
%     size_t         -0.0075712      0.013445    -0.56312       0.57376
%     interact        0.0061042     0.0002087      29.249    1.1559e-91
% 
% 
% Number of observations: 320, Error degrees of freedom: 314
% Root Mean Squared Error: 0.307
% R-squared: 0.76,  Adjusted R-Squared 0.756
% F-statistic vs. constant model: 198, p-value = 6.75e-95
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult.NumObservations;
k = regmult.NumEstimatedCoefficients;
u = regmult.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult.Residuals.Raw);
Stdev_u = std(regmult.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.8382e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9675
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
MSM_MULT.uRaw = regmult.Residuals.Raw; 
MSM_MULT.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
multWhite = table(MSM_MULT.mult_t, MSM_MULT.seq_num_t, MSM_MULT.size_t, MSM_MULT.mult_t1, MSM_MULT.MSM, MSM_MULT.interact, MSM_MULT.size_t.^2, MSM_MULT.mult_t1.^2, MSM_MULT.MSM.^2,MSM_MULT.interact.^2, MSM_MULT.uRaw2);
 
multWhite.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'MSM','interact','size_sq','mult_t1sq', 'MSMsq','interactsq','uRaw2'};
 
% Unrestricted regression
regmultWhite=fitlm(multWhite,'uRaw2 ~ seq_num_t + size_t + mult_t1 +MSM+ interact +size_sq + mult_t1sq + MSMsq + interactsq')
 
% regmultWhite = 
% 
% 
% Linear regression model:
%     uRaw2 ~ 1 + seq_num_t + size_t + mult_t1 + MSM + interact + size_sq + mult_t1sq + MSMsq + interactsq
% 
% Estimated Coefficients:
%                     Estimate          SE         tStat       pValue  
%                    ___________    __________    ________    _________
% 
%     (Intercept)        0.67032       0.22382      2.9949    0.0029669
%     seq_num_t       0.00049293     0.0074729    0.065963      0.94745
%     size_t           -0.058741      0.063608    -0.92348      0.35648
%     mult_t1           -0.14911      0.067727     -2.2016     0.028428
%     MSM             -0.0024608    0.00099041     -2.4847     0.013495
%     interact       -0.00050733    0.00049628     -1.0223      0.30746
%     size_sq          0.0024133     0.0047601     0.50697      0.61253
%     mult_t1sq         0.042642      0.014508      2.9392    0.0035381
%     MSMsq           6.4573e-06    2.8088e-06      2.2989     0.022174
%     interactsq       1.172e-06    6.8338e-07       1.715     0.087343
% 
% 
% Number of observations: 320, Error degrees of freedom: 310
% Root Mean Squared Error: 0.222
% R-squared: 0.192,  Adjusted R-Squared 0.169
% F-statistic vs. constant model: 8.19, p-value = 6.31e-11
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmultWhite.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  61.4596, Crit_Val_e =  11.0705, P_e = 6.0667e-12, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MSM_MULT.seq_num_t MSM_MULT.size_t MSM_MULT.mult_t1 MSM_MULT.interact MSM_MULT.MSM];
 
y = MSM_MULT.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'interact','MSM'};
White
 
% White =
% 
%   6?4 table
% 
%                       beta           SE         tstat         p   
%                    __________    __________    ________    _______
% 
%     (Intercept)        1.6955       0.13294      12.754          0
%     seq_num_t       0.0040297       0.00849     0.47464    0.63537
%     size_t         -0.0075712      0.014838    -0.51025    0.61023
%     mult_t1          0.050728      0.044628      1.1367    0.25653
%     interact        0.0061042     0.0003465      17.617          0
%     MSM             -0.010467    0.00053595      -19.53          0
 
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MSM_MULT.seq_num_t,u)
corr_size_t_u = corr(MSM_MULT.size_t,u)
corr_mult_t1_u = corr(MSM_MULT.mult_t1,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 0.7188
% kurt_jbtest = 7.8496
% test_stat_jbtest = 341.1410
% crit_val_jbtest = 12.8325
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult.Diagnostics.CooksDistance >=0.05);
Outliers = MSM_MULT(regmult.Diagnostics.CooksDistance >=0.05,:);




%% Section 5.2.1 buyout, irr
clc
close all
clear all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MSM Buyout IRR t-1','A2:AH276');
stringVectors = string(raw(:,[4,5,6,9,13,14,15,16,17,18,21,22,23,24]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,10,11,12,19,20,25,26,27,28,29,30,31,32,33,34]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MSM_IRR = table;

% Allocate imported array to column variable names
MSM_IRR.number = data(:,1);
MSM_IRR.FUNDID = data(:,2);
MSM_IRR.FIRMID = data(:,3);
MSM_IRR.teamID = stringVectors(:,1);
MSM_IRR.teamIDvintage = stringVectors(:,2);
MSM_IRR.teamIDvintagenumber = stringVectors(:,3);
MSM_IRR.VINTAGEYEAR = data(:,4);
MSM_IRR.MSM = data(:,5);
MSM_IRR.STRATEGY = categorical(stringVectors(:,4));
MSM_IRR.BuyoutDummy = data(:,6);
MSM_IRR.irr_t = data(:,7);
MSM_IRR.irr_t1 = data(:,8);
MSM_IRR.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
MSM_IRR.COUNTRIES = categorical(stringVectors(:,6));
MSM_IRR.REGIONS = categorical(stringVectors(:,7));
MSM_IRR.STATUS = categorical(stringVectors(:,8));
MSM_IRR.FUNDMANAGER = stringVectors(:,9);
MSM_IRR.DOMICILE = categorical(stringVectors(:,10));
MSM_IRR.size_= data(:,9);
MSM_IRR.seq_num_t = data(:,10);
MSM_IRR.COREINDUSTRIES = categorical(stringVectors(:,11));
MSM_IRR.INDUSTRIES = stringVectors(:,12);
MSM_IRR.MANAGERREGION = categorical(stringVectors(:,13));
MSM_IRR.MANAGERCOUNTRY = categorical(stringVectors(:,14));
MSM_IRR.vint01 = data(:,11);
MSM_IRR.vint02 = data(:,12);
MSM_IRR.vint03 = data(:,13);
MSM_IRR.vint04 = data(:,14);
MSM_IRR.vint05 = data(:,15);
MSM_IRR.vint06 = data(:,16);
MSM_IRR.vint07 = data(:,17);
MSM_IRR.vint08 = data(:,18);
MSM_IRR.vint09 = data(:,19);
MSM_IRR.vint10 = data(:,20);

% Clear temporary variables
clearvars data raw stringVectors;

% Log size
MSM_IRR.size_t=log(MSM_IRR.size_);
MSM_IRR.interact=MSM_IRR.MSM.*MSM_IRR.irr_t;

% Linear regression regirr buyout
 
regirr = fitlm(MSM_IRR, 'irr_t~seq_num_t + size_t + irr_t1 +MSM +interact + vint08  ')
%  
% regirr = 
% 
% 
% Linear regression model:
%     irr_t ~ 1 + MSM + irr_t1 + seq_num_t + vint08 + size_t + interact
% 
% Estimated Coefficients:
%                    Estimate         SE         tStat        pValue  
%                    _________    __________    ________    __________
% 
%     (Intercept)       13.297        1.8403      7.2255     5.195e-12
%     MSM            -0.060897     0.0057455     -10.599    3.7655e-22
%     irr_t1         0.0046519      0.017265     0.26944        0.7878
%     seq_num_t        0.18232       0.22687     0.80365       0.42231
%     vint08           0.89423        1.1477     0.77917       0.43657
%     size_t          -0.18503         0.268    -0.69042       0.49052
%     interact       0.0048828    0.00017046      28.645    1.5229e-83
% 
% 
% Number of observations: 275, Error degrees of freedom: 268
% Root Mean Squared Error: 5.43
% R-squared: 0.775,  Adjusted R-Squared 0.77
% F-statistic vs. constant model: 154, p-value = 8.74e-84

%DIAGNOSTICS of CLRM assumptions
 
T = regirr.NumObservations;
k = regirr.NumEstimatedCoefficients;
u = regirr.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr.Residuals.Raw);
Stdev_u = std(regirr.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 6.5816e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9689
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test


MSM_IRR.uRaw = regirr.Residuals.Raw; 
MSM_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irrWhite = table(MSM_IRR.irr_t, MSM_IRR.seq_num_t, MSM_IRR.size_t, MSM_IRR.irr_t1, MSM_IRR.MSM, MSM_IRR.interact, MSM_IRR.size_t.^2, MSM_IRR.irr_t1.^2, MSM_IRR.MSM.^2,MSM_IRR.interact.^2, MSM_IRR.uRaw2);
 
irrWhite.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'MSM','interact','size_sq','irr_t1sq', 'MSMsq','interactsq','uRaw2'};
 
% Unrestricted regression
regirrWhite=fitlm(irrWhite,'uRaw2 ~ seq_num_t + size_t + irr_t1 +MSM+ interact +size_sq + irr_t1sq + MSMsq + interactsq')
% regirrWhite = 
% 
% 
% Linear regression model:
%     uRaw2 ~ 1 + seq_num_t + size_t + irr_t1 + MSM + interact + size_sq + irr_t1sq + MSMsq + interactsq
% 
% Estimated Coefficients:
%                     Estimate          SE          tStat        pValue  
%                    ___________    __________    _________    __________
% 
%     (Intercept)         107.68        87.709       1.2277       0.22064
%     seq_num_t         -0.55474        3.3641      -0.1649       0.86915
%     size_t             0.16666        25.694    0.0064864       0.99483
%     irr_t1             -0.1547       0.42151     -0.36702       0.71389
%     MSM               -0.81314       0.21697      -3.7476    0.00021913
%     interact       -0.00036029     0.0039902    -0.090294       0.92812
%     size_sq           -0.43766        1.9316     -0.22658       0.82092
%     irr_t1sq        0.00010235     0.0022909     0.044674        0.9644
%     MSMsq            0.0018698    0.00075226       2.4856      0.013551
%     interactsq      8.6587e-07    2.7078e-07       3.1977     0.0015535
% 
% 
% Number of observations: 275, Error degrees of freedom: 265
% Root Mean Squared Error: 76.3
% R-squared: 0.186,  Adjusted R-Squared 0.158
% F-statistic vs. constant model: 6.72, p-value = 1.12e-08
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirrWhite.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  51.1028, Crit_Val_e =  12.5916, P_e = 2.8244e-09, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MSM_IRR.seq_num_t MSM_IRR.size_t MSM_IRR.irr_t1 MSM_IRR.interact MSM_IRR.MSM];
 
y = MSM_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'interact','MSM'};
White

% White =
% 
%   6?4 table
% 
%                      beta           SE         tstat         p     
%                    _________    __________    _______    __________
% 
%     (Intercept)       13.654        2.8105     4.8584     1.997e-06
%     seq_num_t        0.17173       0.25744    0.66705       0.50531
%     size_t          -0.14989       0.29628    -0.5059       0.61333
%     irr_t1          0.005355       0.01234    0.43395       0.66467
%     interact       0.0048788    0.00031783      15.35             0
%     MSM            -0.063844     0.0074548    -8.5642    8.8818e-16

%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MSM_IRR.seq_num_t,u)
corr_size_t_u = corr(MSM_IRR.size_t,u)
corr_irr_t1_u = corr(MSM_IRR.irr_t1,u)

%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 0.1973
% kurt_jbtest = 9.2689
% test_stat_jbtest = 452.0813
% crit_val_jbtest = 14.4494
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 



%% Section 5.2.2 high spread, irr and multiple
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','SPREAD_HIGH_ALL','A2:AW316');
stringVectors = string(raw(:,[4,5,6,9,10,16,17,18,19,20,21,25,26,27,28]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,11,12,13,14,15,22,23,24,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
HIGH_SPREAD = table;

% Allocate imported array to column variable names
HIGH_SPREAD.number = data(:,1);
HIGH_SPREAD.FUNDID = data(:,2);
HIGH_SPREAD.FIRMID = data(:,3);
HIGH_SPREAD.teamID = stringVectors(:,1);
HIGH_SPREAD.teamIDvintage = stringVectors(:,2);
HIGH_SPREAD.teamIDvintagenumber = stringVectors(:,3);
HIGH_SPREAD.VINTAGEYEAR = data(:,4);
HIGH_SPREAD.spread = data(:,5);
HIGH_SPREAD.spread_cat = categorical(stringVectors(:,4));
HIGH_SPREAD.STRATEGY = categorical(stringVectors(:,5));
HIGH_SPREAD.BuyoutDummy = data(:,6);
HIGH_SPREAD.irr_t = data(:,7);
HIGH_SPREAD.irr_t1 = data(:,8);
HIGH_SPREAD.mult_t = data(:,9);
HIGH_SPREAD.mult_t1 = data(:,10);
HIGH_SPREAD.PRIMARYFUNDFOCUS = categorical(stringVectors(:,6));
HIGH_SPREAD.COUNTRIES = categorical(stringVectors(:,7));
HIGH_SPREAD.REGIONS = stringVectors(:,8);
HIGH_SPREAD.STATUS = categorical(stringVectors(:,9));
HIGH_SPREAD.FUNDMANAGER = stringVectors(:,10);
HIGH_SPREAD.DOMICILE = stringVectors(:,11);
HIGH_SPREAD.FINALCLOSESIZE = data(:,11);
HIGH_SPREAD.size_ = data(:,12);
HIGH_SPREAD.seq_num_t = data(:,13);
HIGH_SPREAD.COREINDUSTRIES = categorical(stringVectors(:,12));
HIGH_SPREAD.INDUSTRIES = stringVectors(:,13);
HIGH_SPREAD.MANAGERREGION = categorical(stringVectors(:,14));
HIGH_SPREAD.MANAGERCOUNTRY = categorical(stringVectors(:,15));
HIGH_SPREAD.vint90 = data(:,14);
HIGH_SPREAD.vint91 = data(:,15);
HIGH_SPREAD.vint92 = data(:,16);
HIGH_SPREAD.vint93 = data(:,17);
HIGH_SPREAD.vint94 = data(:,18);
HIGH_SPREAD.vint95 = data(:,19);
HIGH_SPREAD.vint96 = data(:,20);
HIGH_SPREAD.vint97 = data(:,21);
HIGH_SPREAD.vint98 = data(:,22);
HIGH_SPREAD.vint99 = data(:,23);
HIGH_SPREAD.vint00 = data(:,24);
HIGH_SPREAD.vint01 = data(:,25);
HIGH_SPREAD.vint02 = data(:,26);
HIGH_SPREAD.vint03 = data(:,27);
HIGH_SPREAD.vint04 = data(:,28);
HIGH_SPREAD.vint05 = data(:,29);
HIGH_SPREAD.vint06 = data(:,30);
HIGH_SPREAD.vint07 = data(:,31);
HIGH_SPREAD.vint08 = data(:,32);
HIGH_SPREAD.vint09 = data(:,33);
HIGH_SPREAD.vint10 = data(:,34);

% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
HIGH_SPREAD.size_t=log(HIGH_SPREAD.size_);

% Linear regression reg1irr w/o Buyout Dummy
 
regirr1 = fitlm(HIGH_SPREAD, 'irr_t~seq_num_t + size_t + irr_t1  + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate      SE        tStat       pValue  
%                    ________    _______    ________    _________
% 
%     (Intercept)       57.18     17.443      3.2782    0.0011715
%     irr_t1         0.021168    0.05375     0.39382        0.694
%     seq_num_t       -1.1362     1.0837     -1.0484      0.29531
%     vint91          -11.947     27.058    -0.44153      0.65916
%     vint92          -26.582     19.608     -1.3557      0.17626
%     vint93                0          0         NaN          NaN
%     vint94           2.4178      17.49     0.13824      0.89014
%     vint95           5.3832     17.592     0.30601      0.75982
%     vint96          -4.8231     17.187    -0.28062       0.7792
%     vint97          -4.3911     16.519    -0.26583      0.79056
%     vint98          -22.765     16.271     -1.3991      0.16284
%     vint99          -32.568     16.309     -1.9969     0.046765
%     vint00          -25.343     16.274     -1.5573      0.12049
%     vint01          -19.842     16.676     -1.1899      0.23506
%     vint02          -24.635     17.917     -1.3749      0.17022
%     vint03          -19.249     18.573     -1.0364      0.30086
%     vint04          -19.964     19.863     -1.0051       0.3157
%     vint05                0          0         NaN          NaN
%     vint06                0          0         NaN          NaN
%     vint07                0          0         NaN          NaN
%     vint08                0          0         NaN          NaN
%     vint09                0          0         NaN          NaN
%     vint10                0          0         NaN          NaN
%     size_t          -2.8152     1.3634     -2.0649     0.039823
% 
% 
% Number of observations: 315, Error degrees of freedom: 298
% Root Mean Squared Error: 31.2
% R-squared: 0.152,  Adjusted R-Squared 0.106
% F-statistic vs. constant model: 3.33, p-value = 2.16e-05
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -2.0859e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9680
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Cannot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
HIGH_SPREAD.uRaw = regirr1.Residuals.Raw; 
HIGH_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr1White = table(HIGH_SPREAD.irr_t, HIGH_SPREAD.seq_num_t, HIGH_SPREAD.size_t, HIGH_SPREAD.irr_t1,  HIGH_SPREAD.vint91, HIGH_SPREAD.vint92, HIGH_SPREAD.vint93, HIGH_SPREAD.vint94, ...
    HIGH_SPREAD.vint95, HIGH_SPREAD.vint96, HIGH_SPREAD.vint97, HIGH_SPREAD.vint98, HIGH_SPREAD.vint99,HIGH_SPREAD.vint00, HIGH_SPREAD.vint01, HIGH_SPREAD.vint02, HIGH_SPREAD.vint03, HIGH_SPREAD.vint04,...
    HIGH_SPREAD.vint05, HIGH_SPREAD.vint06, HIGH_SPREAD.vint07, HIGH_SPREAD.vint08, HIGH_SPREAD.vint09, HIGH_SPREAD.vint10, HIGH_SPREAD.size_t.^2, HIGH_SPREAD.irr_t1.^2, HIGH_SPREAD.uRaw2);
 
irr1White.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(irr1White,'uRaw2 ~ seq_num_t + size_t + irr_t1  + vint91+vint92 +vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
 
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue  
%                    ________    ________    ________    _________
% 
%     (Intercept)       12683      4368.8      2.9032    0.0039787
%     seq_num_t       -150.22      153.78     -0.9768      0.32948
%     size_t          -3637.8      1097.2     -3.3154     0.001032
%     irr_t1          -24.892      14.297      -1.741     0.082748
%     vint91                0           0         NaN          NaN
%     vint92          -1112.9      3549.7    -0.31353       0.7541
%     vint93           868.97      3831.9     0.22677      0.82076
%     vint94           773.15      3319.8     0.23289      0.81601
%     vint95           2708.2      3335.5     0.81195      0.41749
%     vint96             1600      3290.8     0.48619       0.6272
%     vint97           1973.8        3224     0.61221      0.54088
%     vint98           1546.6      3200.2     0.48328      0.62926
%     vint99            237.2      3206.7    0.073972      0.94108
%     vint00           97.104      3201.4    0.030332      0.97582
%     vint01           277.33      3239.4    0.085613      0.93183
%     vint02          -522.15      3368.2    -0.15502      0.87691
%     vint03           376.62      3437.3     0.10957      0.91283
%     vint04           406.63      3574.4     0.11376       0.9095
%     vint05                0           0         NaN          NaN
%     vint06                0           0         NaN          NaN
%     vint07                0           0         NaN          NaN
%     vint08                0           0         NaN          NaN
%     vint09                0           0         NaN          NaN
%     vint10                0           0         NaN          NaN
%     size_sq          267.93      96.742      2.7696    0.0059763
%     irr_t1sq       0.062311    0.079606     0.78275      0.43442
% 
% 
% Number of observations: 315, Error degrees of freedom: 296
% Root Mean Squared Error: 4.42e+03
% R-squared: 0.115,  Adjusted R-Squared 0.0609
% F-statistic vs. constant model: 2.13, p-value = 0.00519

 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 35.6620, Crit_Val_e =  26.2962, P_e = 0.0032,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is  violated.

% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [HIGH_SPREAD.seq_num_t HIGH_SPREAD.size_t HIGH_SPREAD.irr_t1 HIGH_SPREAD.vint91 HIGH_SPREAD.vint92...
      HIGH_SPREAD.vint94  HIGH_SPREAD.vint95  HIGH_SPREAD.vint96  HIGH_SPREAD.vint97...
    HIGH_SPREAD.vint98 HIGH_SPREAD.vint99 HIGH_SPREAD.vint00 HIGH_SPREAD.vint01 HIGH_SPREAD.vint02...
    HIGH_SPREAD.vint03 HIGH_SPREAD.vint04 ];
 
y = HIGH_SPREAD.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'vint91' ...
    'vint92','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04'};
White
    
% 
% White =
% 
%   17?4 table
% 
%                      beta         SE        tstat          p    
%                    ________    ________    ________    _________
% 
%     (Intercept)       57.18      20.405      2.8023    0.0053905
%     seq_num_t       -1.1362     0.79886     -1.4223      0.15593
%     size_t          -2.8152      2.3175     -1.2148      0.22536
%     irr_t1         0.021168    0.070531     0.30012      0.76428
%     vint91          -11.947      22.337    -0.53485      0.59313
%     vint92          -26.582      15.698     -1.6934      0.09138
%     vint94           2.4178      17.611     0.13729      0.89089
%     vint95           5.3832      21.121     0.25488      0.79899
%     vint96          -4.8231      17.533    -0.27509      0.78343
%     vint97          -4.3911      15.781    -0.27826        0.781
%     vint98          -22.765      16.143     -1.4103      0.15946
%     vint99          -32.568      15.649     -2.0811     0.038233
%     vint00          -25.343      15.154     -1.6723      0.09546
%     vint01          -19.842      15.419     -1.2869       0.1991
%     vint02          -24.635      15.986      -1.541      0.12433
%     vint03          -19.249      16.092     -1.1962      0.23252
%     vint04          -19.964      15.776     -1.2655      0.20665
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(HIGH_SPREAD.seq_num_t,u)
corr_size_t_u = corr(HIGH_SPREAD.size_t,u)
corr_irr_t1_u = corr(HIGH_SPREAD.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 3.4334
% kurt_jbtest = 25.2251
% test_stat_jbtest = 7.1020e+03
% crit_val_jbtest = 28.8454
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = HIGH_SPREAD(regirr1.Diagnostics.CooksDistance >=0.05,:);

 
% Linear regression reg1irr w/ Buyout Dummy
 
regirr1 = fitlm(HIGH_SPREAD, 'irr_t~seq_num_t + BuyoutDummy + size_t + irr_t1 + vint91 + vint92 + vint93 +  vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue 
%                    ________    ________    ________    ________
% 
%     (Intercept)      56.888      17.459      3.2583    0.001254
%     BuyoutDummy      3.1145      4.1045      0.7588     0.44859
%     irr_t1         0.024805    0.054001     0.45933     0.64634
%     seq_num_t      -0.98492      1.1027    -0.89319      0.3725
%     vint91          -11.341      27.089    -0.41864     0.67579
%     vint92          -25.267      19.699     -1.2826     0.20064
%     vint93                0           0         NaN         NaN
%     vint94           3.8435      17.603     0.21835     0.82731
%     vint95           5.8152      17.613     0.33016     0.74152
%     vint96          -4.0868      17.227    -0.23723     0.81264
%     vint97          -3.5193       16.57    -0.21239     0.83195
%     vint98          -21.716      16.341     -1.3289     0.18492
%     vint99          -31.399      16.393     -1.9154    0.056423
%     vint00          -24.238       16.35     -1.4824     0.13932
%     vint01          -18.432      16.791     -1.0977     0.27324
%     vint02          -23.973      17.951     -1.3355     0.18277
%     vint03            -18.6      18.606    -0.99972     0.31828
%     vint04          -19.318      19.896    -0.97096     0.33238
%     vint05                0           0         NaN         NaN
%     vint06                0           0         NaN         NaN
%     vint07                0           0         NaN         NaN
%     vint08                0           0         NaN         NaN
%     vint09                0           0         NaN         NaN
%     vint10                0           0         NaN         NaN
%     size_t          -3.3422      1.5309     -2.1831    0.029832
% 
% 
% Number of observations: 315, Error degrees of freedom: 297
% Root Mean Squared Error: 31.2
% R-squared: 0.153,  Adjusted R-Squared 0.105
% F-statistic vs. constant model: 3.16, p-value = 3.33e-05

%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.2185e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9680
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
HIGH_SPREAD.uRaw = regirr1.Residuals.Raw; 
HIGH_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr1White = table(HIGH_SPREAD.irr_t, HIGH_SPREAD.seq_num_t, HIGH_SPREAD.BuyoutDummy, HIGH_SPREAD.size_t, HIGH_SPREAD.irr_t1, HIGH_SPREAD.vint91, HIGH_SPREAD.vint92, HIGH_SPREAD.vint93, HIGH_SPREAD.vint94, ...
    HIGH_SPREAD.vint95, HIGH_SPREAD.vint96, HIGH_SPREAD.vint97, HIGH_SPREAD.vint98, HIGH_SPREAD.vint99,HIGH_SPREAD.vint00, HIGH_SPREAD.vint01, HIGH_SPREAD.vint02, HIGH_SPREAD.vint03, HIGH_SPREAD.vint04,...
    HIGH_SPREAD.vint05, HIGH_SPREAD.vint06, HIGH_SPREAD.vint07, HIGH_SPREAD.vint08, HIGH_SPREAD.vint09, HIGH_SPREAD.vint10, HIGH_SPREAD.size_t.^2, HIGH_SPREAD.irr_t1.^2, HIGH_SPREAD.uRaw2);
 
irr1White.Properties.VariableNames={'irr_t','seq_num_t','BuyoutDummy', 'size_t','irr_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(irr1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + irr_t1 + vint91 +vint92 + vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
%  
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 27 terms in 26 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat        pValue  
%                    ________    ________    _________    __________
% 
%     (Intercept)       13020      4396.4       2.9616     0.0033155
%     seq_num_t        -192.4      157.03      -1.2252       0.22149
%     BuyoutDummy     -789.81      588.24      -1.3427       0.18044
%     size_t          -3687.5      1102.2      -3.3455    0.00093018
%     irr_t1          -24.502      14.353      -1.7071      0.088888
%     vint91                0           0          NaN           NaN
%     vint92          -1256.8      3566.5     -0.35238       0.72481
%     vint93           1039.6        3848      0.27017       0.78722
%     vint94           656.09      3335.9      0.19668       0.84422
%     vint95           2885.4      3348.2      0.86177       0.38953
%     vint96           1624.2      3303.4      0.49167       0.62333
%     vint97           2017.4      3236.8      0.62328       0.53359
%     vint98           1483.4      3213.8      0.46159       0.64473
%     vint99           141.45      3220.5     0.043921         0.965
%     vint00           6.5754        3215    0.0020452       0.99837
%     vint01           107.29      3255.1      0.03296       0.97373
%     vint02          -513.15      3381.1     -0.15177       0.87948
%     vint03           413.05      3450.3      0.11971       0.90479
%     vint04           463.37      3587.9      0.12915       0.89733
%     vint05                0           0          NaN           NaN
%     vint06                0           0          NaN           NaN
%     vint07                0           0          NaN           NaN
%     vint08                0           0          NaN           NaN
%     vint09                0           0          NaN           NaN
%     vint10                0           0          NaN           NaN
%     size_sq          284.32      97.919       2.9037     0.0039737
%     irr_t1sq       0.054166    0.080184      0.67552       0.49989
% 
% 
% Number of observations: 315, Error degrees of freedom: 295
% Root Mean Squared Error: 4.44e+03
% R-squared: 0.121,  Adjusted R-Squared 0.0643
% F-statistic vs. constant model: 2.14, p-value = 0.0043
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  37.4543, Crit_Val_e =  27.5871, P_e = 0.0029,
%reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is  violated.


% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [HIGH_SPREAD.seq_num_t HIGH_SPREAD.size_t HIGH_SPREAD.irr_t1 HIGH_SPREAD.BuyoutDummy...
    HIGH_SPREAD.vint91 HIGH_SPREAD.vint92...
      HIGH_SPREAD.vint94  HIGH_SPREAD.vint95  HIGH_SPREAD.vint96  HIGH_SPREAD.vint97...
    HIGH_SPREAD.vint98 HIGH_SPREAD.vint99 HIGH_SPREAD.vint00 HIGH_SPREAD.vint01 HIGH_SPREAD.vint02...
    HIGH_SPREAD.vint03 HIGH_SPREAD.vint04 ];
 
y = HIGH_SPREAD.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'BuyoutDummy' ...
    'vint91','vint92','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04'};
White

% White =
% 
%   18?4 table
% 
%                      beta         SE        tstat          p    
%                    ________    ________    ________    _________
% 
%     (Intercept)      56.888      20.154      2.8227    0.0050665
%     seq_num_t      -0.98492     0.89116     -1.1052      0.26992
%     size_t          -3.3422      2.4511     -1.3635       0.1737
%     irr_t1         0.024805    0.071372     0.34754      0.72842
%     BuyoutDummy      3.1145      4.6028     0.67665      0.49913
%     vint91          -11.341      21.464    -0.52837      0.59762
%     vint92          -25.267      15.499     -1.6302      0.10406
%     vint94           3.8435      17.493     0.21971      0.82624
%     vint95           5.8152      21.027     0.27656       0.7823
%     vint96          -4.0868      17.313    -0.23606      0.81354
%     vint97          -3.5193      15.382     -0.2288      0.81918
%     vint98          -21.716       15.96     -1.3607      0.17459
%     vint99          -31.399      15.541     -2.0204     0.044191
%     vint00          -24.238      14.994     -1.6165        0.107
%     vint01          -18.432      15.364     -1.1997      0.23118
%     vint02          -23.973      15.717     -1.5253       0.1282
%     vint03            -18.6      15.705     -1.1843      0.23717
%     vint04          -19.318      15.411     -1.2535      0.21095
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(HIGH_SPREAD.seq_num_t,u)
corr_size_t_u = corr(HIGH_SPREAD.size_t,u)
corr_irr_t1_u = corr(HIGH_SPREAD.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 3.4805
% kurt_jbtest = 25.5924
% test_stat_jbtest = 7.3352e+03
% crit_val_jbtest = 30.1910
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = HIGH_SPREAD(regirr1.Diagnostics.CooksDistance >=0.05,:);
   
% Linear regression reg1irr w/ Buyout Dummy*irr_t1
 
HIGH_SPREAD.interact=HIGH_SPREAD.BuyoutDummy.*HIGH_SPREAD.irr_t1
 
regirr1 = fitlm(HIGH_SPREAD, 'irr_t~seq_num_t + BuyoutDummy + interact+ size_t + irr_t1 + vint91 + vint92 + vint93 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% 
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue 
%                    _________    _______    ________    ________
% 
%     (Intercept)       46.001     23.329      1.9718    0.049584
%     BuyoutDummy        4.306     5.2457     0.82087      0.4124
%     irr_t1          0.035364    0.06131     0.57681     0.56452
%     seq_num_t       -0.98331     1.1043    -0.89042     0.37398
%     vint91                 0          0         NaN         NaN
%     vint92           -14.347     25.138    -0.57072     0.56863
%     vint93            11.588     27.137     0.42702     0.66968
%     vint94            14.525     23.589     0.61572     0.53856
%     vint95            16.644     23.635      0.7042     0.48187
%     vint96            6.6558     23.347     0.28509     0.77578
%     vint97            7.3194     22.851     0.32031     0.74897
%     vint98           -11.021     22.707    -0.48536     0.62779
%     vint99            -21.11     22.859    -0.92349     0.35652
%     vint00           -13.685     22.732    -0.60203     0.54763
%     vint01           -7.8437     23.003    -0.34099     0.73336
%     vint02           -13.423     23.897    -0.56172     0.57474
%     vint03           -8.0162     24.401    -0.32852     0.74276
%     vint04           -8.5492      25.34    -0.33738     0.73607
%     vint05                 0          0         NaN         NaN
%     vint06                 0          0         NaN         NaN
%     vint07                 0          0         NaN         NaN
%     vint08                 0          0         NaN         NaN
%     vint09                 0          0         NaN         NaN
%     vint10                 0          0         NaN         NaN
%     size_t           -3.3538     1.5335      -2.187    0.029544
%     interact       -0.047727    0.13054    -0.36561     0.71493
% 
% 
% Number of observations: 315, Error degrees of freedom: 296
% Root Mean Squared Error: 31.3
% R-squared: 0.154,  Adjusted R-Squared 0.102
% F-statistic vs. constant model: 2.99, p-value = 5.88e-05
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -4.8356e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9680
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
HIGH_SPREAD.uRaw = regirr1.Residuals.Raw; 
HIGH_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr1White = table(HIGH_SPREAD.irr_t, HIGH_SPREAD.seq_num_t, HIGH_SPREAD.BuyoutDummy, HIGH_SPREAD.interact, HIGH_SPREAD.size_t, HIGH_SPREAD.irr_t1,  HIGH_SPREAD.vint91, HIGH_SPREAD.vint92, HIGH_SPREAD.vint93, HIGH_SPREAD.vint94, ...
    HIGH_SPREAD.vint95, HIGH_SPREAD.vint96, HIGH_SPREAD.vint97, HIGH_SPREAD.vint98, HIGH_SPREAD.vint99,HIGH_SPREAD.vint00, HIGH_SPREAD.vint01, HIGH_SPREAD.vint02, HIGH_SPREAD.vint03, HIGH_SPREAD.vint04,...
    HIGH_SPREAD.vint05, HIGH_SPREAD.vint06, HIGH_SPREAD.vint07, HIGH_SPREAD.vint08, HIGH_SPREAD.vint09, HIGH_SPREAD.vint10, HIGH_SPREAD.size_t.^2, HIGH_SPREAD.irr_t1.^2, HIGH_SPREAD.uRaw2);
 
irr1White.Properties.VariableNames={'irr_t','seq_num_t','BuyoutDummy', 'interact',  'size_t','irr_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(irr1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact + irr_t1  + vint91 + vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat       pValue  
%                    ________    ________    _________    _________
% 
%     (Intercept)       12724      4438.1        2.867    0.0044504
%     seq_num_t       -193.67      157.57      -1.2291      0.22005
%     BuyoutDummy     -1152.6      765.44      -1.5058      0.13322
%     interact          15.58      20.228      0.77024      0.44179
%     size_t          -3601.4      1114.8      -3.2306    0.0013789
%     irr_t1          -32.253      17.373      -1.8566     0.064397
%     vint91                0           0          NaN          NaN
%     vint92          -1130.9      3582.9     -0.31564       0.7525
%     vint93           990.76      3861.3      0.25659      0.79768
%     vint94           848.77      3357.8      0.25278      0.80062
%     vint95           3040.2      3368.5      0.90253      0.36753
%     vint96           1831.2      3325.5      0.55064      0.58231
%     vint97           2163.5      3255.5      0.66457      0.50686
%     vint98           1678.1      3234.7      0.51877      0.60432
%     vint99           420.83      3253.6      0.12934      0.89718
%     vint00           200.92      3237.2     0.062066      0.95055
%     vint01           275.16        3275     0.084017       0.9331
%     vint02          -298.04      3406.3    -0.087496      0.93034
%     vint03           600.88        3472      0.17307      0.86272
%     vint04           602.18      3605.5      0.16702      0.86747
%     vint05                0           0          NaN          NaN
%     vint06                0           0          NaN          NaN
%     vint07                0           0          NaN          NaN
%     vint08                0           0          NaN          NaN
%     vint09                0           0          NaN          NaN
%     vint10                0           0          NaN          NaN
%     size_sq          277.47      98.853       2.8069    0.0053442
%     irr_t1sq       0.082533    0.087494      0.94329      0.34632
% 
% 
% Number of observations: 315, Error degrees of freedom: 294
% Root Mean Squared Error: 4.45e+03
% R-squared: 0.123,  Adjusted R-Squared 0.063
% F-statistic vs. constant model: 2.06, p-value = 0.00554 
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 38.1218, Crit_Val_e =  28.8693, P_e = 0.0037,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [HIGH_SPREAD.seq_num_t HIGH_SPREAD.size_t HIGH_SPREAD.irr_t1 HIGH_SPREAD.BuyoutDummy HIGH_SPREAD.interact...
    HIGH_SPREAD.vint92 HIGH_SPREAD.vint93  HIGH_SPREAD.vint94  HIGH_SPREAD.vint95  HIGH_SPREAD.vint96  HIGH_SPREAD.vint97...
    HIGH_SPREAD.vint98 HIGH_SPREAD.vint99 HIGH_SPREAD.vint00 HIGH_SPREAD.vint01 HIGH_SPREAD.vint02...
    HIGH_SPREAD.vint03 HIGH_SPREAD.vint04];
 
y = HIGH_SPREAD.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'BuyoutDummy', 'interact' ...
    'vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04'};
White
    
% White =
% 
%   19?4 table
% 
%                      beta          SE        tstat         p   
%                    _________    ________    ________    _______
% 
%     (Intercept)       46.001      19.342      2.3784    0.01799
%     seq_num_t       -0.98331     0.89097     -1.1036     0.2706
%     size_t           -3.3538      2.4395     -1.3748    0.17017
%     irr_t1          0.035364    0.085562     0.41332    0.67966
%     BuyoutDummy        4.306      4.4713     0.96302    0.33628
%     interact       -0.047727    0.099901    -0.47774    0.63317
%     vint92           -14.347      16.621     -0.8632    0.38869
%     vint93            11.588      21.852     0.53029    0.59629
%     vint94            14.525      19.751     0.73537    0.46267
%     vint95            16.644       18.61     0.89434    0.37183
%     vint96            6.6558      19.666     0.33844    0.73526
%     vint97            7.3194      18.768        0.39    0.69681
%     vint98           -11.021      18.816    -0.58573    0.55848
%     vint99            -21.11      17.453     -1.2095    0.22737
%     vint00           -13.685      17.135    -0.79868    0.42508
%     vint01           -7.8437      17.939    -0.43723    0.66225
%     vint02           -13.423      17.398    -0.77153    0.44097
%     vint03           -8.0162      18.237    -0.43956    0.66056
%     vint04           -8.5492      17.121    -0.49934    0.61789

%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(HIGH_SPREAD.seq_num_t,u)
corr_size_t_u = corr(HIGH_SPREAD.size_t,u)
corr_irr_t1_u = corr(HIGH_SPREAD.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 3.4874
% kurt_jbtest = 25.7416
% test_stat_jbtest = 7.4265e+03
% crit_val_jbtest = 31.5264
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = HIGH_SPREAD(regirr1.Diagnostics.CooksDistance >=0.05,:);

% Linear regression reg1mult w/o Buyout Dummy
 
regmult1 = fitlm(HIGH_SPREAD, 'mult_t~seq_num_t + size_t + mult_t1  + vint91 + vint92 + vint93 +vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat       pValue  
%                    _________    ________    _________    _________
% 
%     (Intercept)       2.2267       1.243       1.7913      0.07428
%     mult_t1          0.27658    0.092238       2.9985    0.0029471
%     seq_num_t      -0.047321    0.075629      -0.6257        0.532
%     vint91          -0.14189       1.884    -0.075313      0.94002
%     vint92           -0.8943      1.3653     -0.65502      0.51298
%     vint93                 0           0          NaN          NaN
%     vint94            3.0616      1.2166       2.5165     0.012392
%     vint95           0.84277       1.227      0.68687      0.49271
%     vint96          -0.31038      1.1965     -0.25941       0.7955
%     vint97          -0.65489      1.1498     -0.56958       0.5694
%     vint98           -1.0777      1.1335     -0.95082      0.34249
%     vint99           -1.2898      1.1368      -1.1347      0.25745
%     vint00          -0.82908      1.1352     -0.73033      0.46577
%     vint01          -0.69212      1.1634     -0.59489      0.55238
%     vint02          -0.94502      1.2469     -0.75788      0.44914
%     vint03           -0.7995      1.2942     -0.61775      0.53722
%     vint04          -0.52678      1.3817     -0.38126      0.70329
%     vint05                 0           0          NaN          NaN
%     vint06                 0           0          NaN          NaN
%     vint07                 0           0          NaN          NaN
%     vint08                 0           0          NaN          NaN
%     vint09                 0           0          NaN          NaN
%     vint10                 0           0          NaN          NaN
%     size_t         -0.014243    0.094914     -0.15006      0.88082
% 
% 
% Number of observations: 315, Error degrees of freedom: 298
% Root Mean Squared Error: 2.17
% R-squared: 0.212,  Adjusted R-Squared 0.17
% F-statistic vs. constant model: 5.02, p-value = 3.25e-09
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -6.0406e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9680
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Cannot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
HIGH_SPREAD.uRaw = regmult1.Residuals.Raw; 
HIGH_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(HIGH_SPREAD.mult_t, HIGH_SPREAD.seq_num_t, HIGH_SPREAD.size_t, HIGH_SPREAD.mult_t1,  HIGH_SPREAD.vint91, HIGH_SPREAD.vint92, HIGH_SPREAD.vint93, HIGH_SPREAD.vint94, ...
    HIGH_SPREAD.vint95, HIGH_SPREAD.vint96, HIGH_SPREAD.vint97, HIGH_SPREAD.vint98, HIGH_SPREAD.vint99,HIGH_SPREAD.vint00, HIGH_SPREAD.vint01, HIGH_SPREAD.vint02, HIGH_SPREAD.vint03, HIGH_SPREAD.vint04,...
    HIGH_SPREAD.vint05, HIGH_SPREAD.vint06, HIGH_SPREAD.vint07, HIGH_SPREAD.vint08, HIGH_SPREAD.vint09, HIGH_SPREAD.vint10, HIGH_SPREAD.size_t.^2, HIGH_SPREAD.mult_t1.^2, HIGH_SPREAD.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + mult_t1  + vint91 +vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
 
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate      SE          tStat        pValue 
%                    ________    _______    ___________    ________
% 
%     (Intercept)     -3.2582     39.165      -0.083191     0.93376
%     seq_num_t      -0.82985      1.429       -0.58071     0.56189
%     size_t            2.212     9.1887        0.24073     0.80994
%     mult_t1         -2.0333     5.1178       -0.39729     0.69144
%     vint91                0          0            NaN         NaN
%     vint92          0.65749     32.609       0.020163     0.98393
%     vint93         -0.69147     35.217      -0.019635     0.98435
%     vint94           52.074      30.51         1.7068    0.088933
%     vint95           14.844     30.739        0.48291     0.62953
%     vint96          0.35978     30.269       0.011886     0.99052
%     vint97         0.037335     29.638      0.0012597       0.999
%     vint98         -0.99598     29.423      -0.033851     0.97302
%     vint99         -0.01704     29.432    -0.00057896     0.99954
%     vint00          0.92098     29.414       0.031311     0.97504
%     vint01           1.4489     29.779       0.048655     0.96123
%     vint02           1.4319     30.931       0.046294     0.96311
%     vint03           1.1662     31.574       0.036935     0.97056
%     vint04           1.7097     32.832       0.052074     0.95851
%     vint05                0          0            NaN         NaN
%     vint06                0          0            NaN         NaN
%     vint07                0          0            NaN         NaN
%     vint08                0          0            NaN         NaN
%     vint09                0          0            NaN         NaN
%     vint10                0          0            NaN         NaN
%     size_sq        -0.19042    0.82147       -0.23181     0.81685
%     mult_t1sq       0.69257    0.66218         1.0459     0.29648
% 
% 
% Number of observations: 315, Error degrees of freedom: 296
% Root Mean Squared Error: 40.6
% R-squared: 0.0955,  Adjusted R-Squared 0.0405
% F-statistic vs. constant model: 1.74, p-value = 0.0329
 
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 38.0564, Crit_Val_e =  26.2962, P_e = 0.0015,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [HIGH_SPREAD.seq_num_t HIGH_SPREAD.size_t HIGH_SPREAD.mult_t1  HIGH_SPREAD.vint91 HIGH_SPREAD.vint92...
      HIGH_SPREAD.vint94  HIGH_SPREAD.vint95  HIGH_SPREAD.vint96  HIGH_SPREAD.vint97...
    HIGH_SPREAD.vint98 HIGH_SPREAD.vint99 HIGH_SPREAD.vint00 HIGH_SPREAD.vint01 HIGH_SPREAD.vint02...
    HIGH_SPREAD.vint03 HIGH_SPREAD.vint04 ];
 
y = HIGH_SPREAD.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', ...
    'vint91','vint92','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04'};
White
    
% White =
% 
%   17?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       2.2267     0.64257      3.4653    0.00060355
%     seq_num_t      -0.047321    0.050249    -0.94174       0.34705
%     size_t         -0.014243    0.057945     -0.2458         0.806
%     mult_t1          0.27658    0.088847       3.113      0.002023
%     vint91          -0.14189     0.74382    -0.19075       0.84884
%     vint92           -0.8943        0.61     -1.4661       0.14364
%     vint94            3.0616      1.8373      1.6664       0.09664
%     vint95           0.84277      1.2614     0.66811       0.50455
%     vint96          -0.31038     0.67719    -0.45833       0.64703
%     vint97          -0.65489     0.54478     -1.2021       0.23023
%     vint98           -1.0777     0.53805      -2.003      0.046043
%     vint99           -1.2898     0.56849     -2.2688      0.023958
%     vint00          -0.82908     0.54892     -1.5104       0.13195
%     vint01          -0.69212     0.55509     -1.2468       0.21339
%     vint02          -0.94502     0.56418      -1.675      0.094928
%     vint03           -0.7995     0.54441     -1.4686       0.14295
%     vint04          -0.52678     0.60547    -0.87002       0.38496
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(HIGH_SPREAD.seq_num_t,u)
corr_size_t_u = corr(HIGH_SPREAD.size_t,u)
corr_mult_t1_u = corr(HIGH_SPREAD.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 7.3203
% kurt_jbtest = 86.2487
% test_stat_jbtest = 9.3774e+04
% crit_val_jbtest = 28.8454
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = HIGH_SPREAD(regmult1.Diagnostics.CooksDistance >=0.05,:);
 
% Linear regression reg1mult w/ Buyout Dummy
 
regmult1 = fitlm(HIGH_SPREAD, 'mult_t~seq_num_t + BuyoutDummy + size_t + mult_t1 + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat       pValue  
%                    _________    ________    ________    _________
% 
%     (Intercept)       2.0861      1.6364      1.2749      0.20338
%     BuyoutDummy     0.030443     0.28658     0.10623      0.91548
%     mult_t1          0.27766    0.092946      2.9873    0.0030551
%     seq_num_t      -0.045803    0.077092    -0.59413      0.55289
%     vint91                 0           0         NaN          NaN
%     vint92           -0.7455      1.7472     -0.4267      0.66992
%     vint93           0.13571       1.888     0.07188      0.94275
%     vint94            3.2112      1.6363      1.9624     0.050668
%     vint95           0.98177      1.6469     0.59613      0.55155
%     vint96          -0.16792      1.6223    -0.10351      0.91763
%     vint97          -0.51078      1.5878     -0.3217      0.74791
%     vint98          -0.93143      1.5751    -0.59134      0.55475
%     vint99            -1.142      1.5773    -0.72405      0.46962
%     vint00          -0.68205      1.5749    -0.43306      0.66529
%     vint01          -0.54209      1.5947    -0.33993      0.73415
%     vint02          -0.80268      1.6554    -0.48488      0.62813
%     vint03          -0.65706      1.6918    -0.38837      0.69803
%     vint04          -0.38487      1.7596    -0.21873      0.82701
%     vint05                 0           0         NaN          NaN
%     vint06                 0           0         NaN          NaN
%     vint07                 0           0         NaN          NaN
%     vint08                 0           0         NaN          NaN
%     vint09                 0           0         NaN          NaN
%     vint10                 0           0         NaN          NaN
%     size_t         -0.019379     0.10666    -0.18169      0.85595
% 
% 
% Number of observations: 315, Error degrees of freedom: 297
% Root Mean Squared Error: 2.18
% R-squared: 0.212,  Adjusted R-Squared 0.167
% F-statistic vs. constant model: 4.71, p-value = 7.48e-09
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -2.9096e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9680
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
HIGH_SPREAD.uRaw = regmult1.Residuals.Raw; 
HIGH_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(HIGH_SPREAD.mult_t, HIGH_SPREAD.seq_num_t, HIGH_SPREAD.BuyoutDummy, HIGH_SPREAD.size_t, HIGH_SPREAD.mult_t1, HIGH_SPREAD.vint91, HIGH_SPREAD.vint92, HIGH_SPREAD.vint93, HIGH_SPREAD.vint94, ...
    HIGH_SPREAD.vint95, HIGH_SPREAD.vint96, HIGH_SPREAD.vint97, HIGH_SPREAD.vint98, HIGH_SPREAD.vint99,HIGH_SPREAD.vint00, HIGH_SPREAD.vint01, HIGH_SPREAD.vint02, HIGH_SPREAD.vint03, HIGH_SPREAD.vint04,...
    HIGH_SPREAD.vint05, HIGH_SPREAD.vint06, HIGH_SPREAD.vint07, HIGH_SPREAD.vint08, HIGH_SPREAD.vint09, HIGH_SPREAD.vint10, HIGH_SPREAD.size_t.^2, HIGH_SPREAD.mult_t1.^2, HIGH_SPREAD.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','BuyoutDummy', 'size_t','mult_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + mult_t1 + vint91+vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
%  
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 27 terms in 26 predictors]
% 
% Estimated Coefficients:
%                    Estimate      SE         tStat      pValue 
%                    ________    _______    _________    _______
% 
%     (Intercept)     -2.7738     39.203    -0.070755    0.94364
%     seq_num_t       -1.0678     1.4487     -0.73707    0.46168
%     BuyoutDummy     -5.6838     5.5309      -1.0277    0.30497
%     size_t           2.2372     9.1969      0.24326    0.80798
%     mult_t1         -1.0106     5.2188     -0.19364    0.84659
%     vint91                0          0          NaN        NaN
%     vint92         -0.74828     32.667    -0.022906    0.98174
%     vint93          0.29385     35.261    0.0083338    0.99336
%     vint94           50.534     30.578       1.6527    0.09949
%     vint95           15.399      30.77      0.50044    0.61714
%     vint96          0.23977     30.296    0.0079143    0.99369
%     vint97         -0.40643     29.668    -0.013699    0.98908
%     vint98          -1.6561     29.456    -0.056224     0.9552
%     vint99           -1.029     29.474    -0.034912    0.97217
%     vint00          0.11948      29.45    0.0040569    0.99677
%     vint01           0.3031     29.826     0.010162     0.9919
%     vint02            1.259     30.959     0.040666    0.96759
%     vint03           1.2499     31.602     0.039551    0.96848
%     vint04           1.7856     32.861     0.054338     0.9567
%     vint05                0          0          NaN        NaN
%     vint06                0          0          NaN        NaN
%     vint07                0          0          NaN        NaN
%     vint08                0          0          NaN        NaN
%     vint09                0          0          NaN        NaN
%     vint10                0          0          NaN        NaN
%     size_sq        -0.10987    0.82593     -0.13303    0.89426
%     mult_t1sq       0.52659     0.6824      0.77168    0.44094
% 
% 
% Number of observations: 315, Error degrees of freedom: 295
% Root Mean Squared Error: 40.7
% R-squared: 0.0988,  Adjusted R-Squared 0.0407
% F-statistic vs. constant model: 1.7, p-value = 0.0351
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  31.1157, Crit_Val_e =  27.5871, P_e = 0.0193,
%reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [HIGH_SPREAD.seq_num_t HIGH_SPREAD.size_t HIGH_SPREAD.mult_t1 HIGH_SPREAD.BuyoutDummy...
    HIGH_SPREAD.vint92 HIGH_SPREAD.vint93...
      HIGH_SPREAD.vint94  HIGH_SPREAD.vint95  HIGH_SPREAD.vint96  HIGH_SPREAD.vint97...
    HIGH_SPREAD.vint98 HIGH_SPREAD.vint99 HIGH_SPREAD.vint00 HIGH_SPREAD.vint01 HIGH_SPREAD.vint02...
    HIGH_SPREAD.vint03 HIGH_SPREAD.vint04 ];
 
y = HIGH_SPREAD.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'BuyoutDummy' ...
    'vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04'};
White
 
% White =
% 
%   18?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       2.0861      0.5881      3.5472    0.00044887
%     seq_num_t      -0.045803    0.056132    -0.81598       0.41513
%     size_t         -0.019379      0.0778    -0.24909       0.80346
%     mult_t1          0.27766    0.084614      3.2814     0.0011495
%     BuyoutDummy     0.030443     0.27567     0.11043       0.91214
%     vint92           -0.7455     0.57791       -1.29         0.198
%     vint93           0.13571      0.7311     0.18563       0.85286
%     vint94            3.2112      1.8042      1.7799       0.07607
%     vint95           0.98177      1.0928     0.89836       0.36968
%     vint96          -0.16792     0.73886    -0.22727       0.82036
%     vint97          -0.51078     0.60667    -0.84195       0.40046
%     vint98          -0.93143     0.55876      -1.667      0.096523
%     vint99            -1.142     0.56717     -2.0135      0.044913
%     vint00          -0.68205     0.56714     -1.2026       0.23003
%     vint01          -0.54209      0.6079    -0.89174       0.37322
%     vint02          -0.80268     0.56836     -1.4123       0.15886
%     vint03          -0.65706     0.58897     -1.1156       0.26545
%     vint04          -0.38487       0.591    -0.65122       0.51538
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(HIGH_SPREAD.seq_num_t,u)
corr_size_t_u = corr(HIGH_SPREAD.size_t,u)
corr_mult_t1_u = corr(HIGH_SPREAD.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 7.3326
% kurt_jbtest = 86.4307
% test_stat_jbtest = 9.4182e+04
% crit_val_jbtest = 30.1910
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = HIGH_SPREAD(regmult1.Diagnostics.CooksDistance >=0.05,:);

% Linear regression reg1mult w/ Buyout Dummy*mult_t1
 
HIGH_SPREAD.interact=HIGH_SPREAD.BuyoutDummy.*HIGH_SPREAD.mult_t1
 
regmult1 = fitlm(HIGH_SPREAD, 'mult_t~seq_num_t + BuyoutDummy + interact+ size_t + mult_t1 + vint91 + vint92  + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       2.6269       1.243      2.1133      0.035431
%     BuyoutDummy       1.2695     0.54768      2.3179      0.021153
%     mult_t1          0.40136     0.10322      3.8882    0.00012535
%     seq_num_t      -0.061415    0.076553    -0.80225       0.42306
%     vint91          -0.29062      1.8701     -0.1554       0.87662
%     vint92            -1.327      1.3699    -0.96871        0.3335
%     vint93                 0           0         NaN           NaN
%     vint94            2.6068      1.2263      2.1257      0.034377
%     vint95           0.57292      1.2215     0.46901       0.63941
%     vint96          -0.83863      1.2053    -0.69579       0.48712
%     vint97           -1.0809      1.1546    -0.93611          0.35
%     vint98           -1.6169      1.1473     -1.4093       0.15982
%     vint99           -1.8955      1.1568     -1.6386       0.10238
%     vint00           -1.3432       1.148       -1.17       0.24298
%     vint01           -1.1746      1.1765    -0.99831       0.31896
%     vint02           -1.4491      1.2531     -1.1564       0.24847
%     vint03           -1.3404      1.3016     -1.0298       0.30396
%     vint04          -0.74422       1.374    -0.54164       0.58848
%     vint05                 0           0         NaN           NaN
%     vint06                 0           0         NaN           NaN
%     vint07                 0           0         NaN           NaN
%     vint08                 0           0         NaN           NaN
%     vint09                 0           0         NaN           NaN
%     vint10                 0           0         NaN           NaN
%     size_t         -0.050111     0.10624     -0.4717        0.6375
%     interact        -0.53772      0.2033     -2.6449     0.0086178
% 
% 
% Number of observations: 315, Error degrees of freedom: 296
% Root Mean Squared Error: 2.16
% R-squared: 0.231,  Adjusted R-Squared 0.184
% F-statistic vs. constant model: 4.93, p-value = 9.96e-10
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.0406e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9680
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
HIGH_SPREAD.uRaw = regmult1.Residuals.Raw; 
HIGH_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(HIGH_SPREAD.mult_t, HIGH_SPREAD.seq_num_t, HIGH_SPREAD.BuyoutDummy, HIGH_SPREAD.interact, HIGH_SPREAD.size_t, HIGH_SPREAD.mult_t1,  HIGH_SPREAD.vint91, HIGH_SPREAD.vint92, HIGH_SPREAD.vint93, HIGH_SPREAD.vint94, ...
    HIGH_SPREAD.vint95, HIGH_SPREAD.vint96, HIGH_SPREAD.vint97, HIGH_SPREAD.vint98, HIGH_SPREAD.vint99,HIGH_SPREAD.vint00, HIGH_SPREAD.vint01, HIGH_SPREAD.vint02, HIGH_SPREAD.vint03, HIGH_SPREAD.vint04,...
    HIGH_SPREAD.vint05, HIGH_SPREAD.vint06, HIGH_SPREAD.vint07, HIGH_SPREAD.vint08, HIGH_SPREAD.vint09, HIGH_SPREAD.vint10, HIGH_SPREAD.size_t.^2, HIGH_SPREAD.mult_t1.^2, HIGH_SPREAD.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','BuyoutDummy', 'interact',  'size_t','mult_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact + mult_t1  + vint91 + vint92+vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')

% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat      pValue 
%                    _________    _______    _________    _______
% 
%     (Intercept)      -3.3843     38.337    -0.088278    0.92972
%     seq_num_t        -1.0118     1.4176     -0.71374    0.47597
%     BuyoutDummy       5.7477     10.479      0.54852    0.58376
%     interact         -5.2791     4.1665       -1.267    0.20617
%     size_t            1.4362     9.0052      0.15948     0.8734
%     mult_t1           3.5458     6.0713      0.58403    0.55966
%     vint91                 0          0          NaN        NaN
%     vint92           -4.1499     32.035     -0.12954    0.89702
%     vint93            1.5558     34.493     0.045105    0.96406
%     vint94            45.844     30.043       1.5259    0.12813
%     vint95            11.648     30.099      0.38701    0.69904
%     vint96           -3.7179     29.772     -0.12488    0.90071
%     vint97           -3.5028     29.091     -0.12041    0.90425
%     vint98           -4.8821     28.925     -0.16878    0.86609
%     vint99           -5.5206      29.02     -0.19023    0.84926
%     vint00           -3.2425     28.904     -0.11218    0.91076
%     vint01           -2.4664     29.232    -0.084371    0.93282
%     vint02           -2.5631     30.392    -0.084337    0.93285
%     vint03            -2.615      31.02    -0.084301    0.93288
%     vint04           0.77085     32.134     0.023989    0.98088
%     vint05                 0          0          NaN        NaN
%     vint06                 0          0          NaN        NaN
%     vint07                 0          0          NaN        NaN
%     vint08                 0          0          NaN        NaN
%     vint09                 0          0          NaN        NaN
%     vint10                 0          0          NaN        NaN
%     size_sq        -0.067842    0.80789    -0.083974    0.93314
%     mult_t1sq        0.05587    0.74143     0.075355    0.93998
% 
% 
% Number of observations: 315, Error degrees of freedom: 294
% Root Mean Squared Error: 39.8
% R-squared: 0.103,  Adjusted R-Squared 0.0415
% F-statistic vs. constant model: 1.68, p-value = 0.0356
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 32.6116, Crit_Val_e =  28.8693, P_e = 0.0186,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [HIGH_SPREAD.seq_num_t HIGH_SPREAD.size_t HIGH_SPREAD.mult_t1 HIGH_SPREAD.BuyoutDummy HIGH_SPREAD.interact...
    HIGH_SPREAD.vint91 HIGH_SPREAD.vint92  HIGH_SPREAD.vint94  HIGH_SPREAD.vint95  HIGH_SPREAD.vint96  HIGH_SPREAD.vint97...
    HIGH_SPREAD.vint98 HIGH_SPREAD.vint99 HIGH_SPREAD.vint00 HIGH_SPREAD.vint01 HIGH_SPREAD.vint02...
    HIGH_SPREAD.vint03 HIGH_SPREAD.vint04];
 
y = HIGH_SPREAD.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'BuyoutDummy', 'interact' ...
    'vint91','vint92','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04'};
White
    
% White =
% 
%   19?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       2.6269     0.69262      3.7927    0.00017881
%     seq_num_t      -0.061415    0.061854    -0.99291       0.32152
%     size_t         -0.050111    0.073736     -0.6796       0.49726
%     mult_t1          0.40136      0.1066       3.765    0.00019891
%     BuyoutDummy       1.2695     0.31993      3.9679    8.9938e-05
%     interact        -0.53772     0.17896     -3.0047      0.002873
%     vint91          -0.29062     0.85521    -0.33982       0.73422
%     vint92            -1.327     0.69306     -1.9147      0.056442
%     vint94            2.6068      1.7494      1.4901       0.13721
%     vint95           0.57292      1.1612      0.4934       0.62208
%     vint96          -0.83863     0.74798     -1.1212       0.26306
%     vint97           -1.0809        0.62     -1.7433      0.082257
%     vint98           -1.6169     0.62226     -2.5985     0.0098069
%     vint99           -1.8955     0.66005     -2.8717     0.0043606
%     vint00           -1.3432     0.58609     -2.2918      0.022584
%     vint01           -1.1746     0.60264      -1.949      0.052188
%     vint02           -1.4491     0.60953     -2.3774      0.018037
%     vint03           -1.3404     0.59634     -2.2477      0.025293
%     vint04          -0.74422     0.64776     -1.1489       0.25147
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(HIGH_SPREAD.seq_num_t,u)
corr_size_t_u = corr(HIGH_SPREAD.size_t,u)
corr_mult_t1_u = corr(HIGH_SPREAD.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 7.2477
% kurt_jbtest = 86.6606
% test_stat_jbtest = 9.4621e+04
% crit_val_jbtest = 31.5264
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = HIGH_SPREAD(regmult1.Diagnostics.CooksDistance >=0.05,:);
   

%% Section 5.2.2 medium spread, irr and multiple

clc 
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','SPREAD_MED_ALL','A2:AW214');
stringVectors = string(raw(:,[4,5,6,9,10,16,17,18,19,20,21,25,26,27,28]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,11,12,13,14,15,22,23,24,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MED_SPREAD = table;

% Allocate imported array to column variable names
MED_SPREAD.number = data(:,1);
MED_SPREAD.FUNDID = data(:,2);
MED_SPREAD.FIRMID = data(:,3);
MED_SPREAD.teamID = stringVectors(:,1);
MED_SPREAD.teamIDvintage = stringVectors(:,2);
MED_SPREAD.teamIDvintagenumber = stringVectors(:,3);
MED_SPREAD.VINTAGEYEAR = data(:,4);
MED_SPREAD.spread = data(:,5);
MED_SPREAD.spread_cat = categorical(stringVectors(:,4));
MED_SPREAD.STRATEGY = categorical(stringVectors(:,5));
MED_SPREAD.BuyoutDummy = data(:,6);
MED_SPREAD.irr_t = data(:,7);
MED_SPREAD.irr_t1 = data(:,8);
MED_SPREAD.mult_t = data(:,9);
MED_SPREAD.mult_t1 = data(:,10);
MED_SPREAD.PRIMARYFUNDFOCUS = categorical(stringVectors(:,6));
MED_SPREAD.COUNTRIES = categorical(stringVectors(:,7));
MED_SPREAD.REGIONS = stringVectors(:,8);
MED_SPREAD.STATUS = categorical(stringVectors(:,9));
MED_SPREAD.FUNDMANAGER = stringVectors(:,10);
MED_SPREAD.DOMICILE = stringVectors(:,11);
MED_SPREAD.FINALCLOSESIZE = data(:,11);
MED_SPREAD.size_ = data(:,12);
MED_SPREAD.seq_num_t = data(:,13);
MED_SPREAD.COREINDUSTRIES = categorical(stringVectors(:,12));
MED_SPREAD.INDUSTRIES = stringVectors(:,13);
MED_SPREAD.MANAGERREGION = categorical(stringVectors(:,14));
MED_SPREAD.MANAGERCOUNTRY = categorical(stringVectors(:,15));
MED_SPREAD.vint90 = data(:,14);
MED_SPREAD.vint91 = data(:,15);
MED_SPREAD.vint92 = data(:,16);
MED_SPREAD.vint93 = data(:,17);
MED_SPREAD.vint94 = data(:,18);
MED_SPREAD.vint95 = data(:,19);
MED_SPREAD.vint96 = data(:,20);
MED_SPREAD.vint97 = data(:,21);
MED_SPREAD.vint98 = data(:,22);
MED_SPREAD.vint99 = data(:,23);
MED_SPREAD.vint00 = data(:,24);
MED_SPREAD.vint01 = data(:,25);
MED_SPREAD.vint02 = data(:,26);
MED_SPREAD.vint03 = data(:,27);
MED_SPREAD.vint04 = data(:,28);
MED_SPREAD.vint05 = data(:,29);
MED_SPREAD.vint06 = data(:,30);
MED_SPREAD.vint07 = data(:,31);
MED_SPREAD.vint08 = data(:,32);
MED_SPREAD.vint09 = data(:,33);
MED_SPREAD.vint10 = data(:,34);

% Clear temporary variables
clearvars data raw stringVectors R;

% Log size
MED_SPREAD.size_t=log(MED_SPREAD.size_);
 
% Linear regression reg1irr w/o Buyout Dummy
 
regirr1 = fitlm(MED_SPREAD, 'irr_t~seq_num_t + size_t + irr_t1  + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat        pValue  
%                    ________    ________    ________    __________
% 
%     (Intercept)      78.257      22.356      3.5005    0.00057927
%     irr_t1          0.22816    0.077098      2.9593     0.0034777
%     seq_num_t      -0.73886      1.4432    -0.51196       0.60928
%     vint91                0           0         NaN           NaN
%     vint92                0           0         NaN           NaN
%     vint93                0           0         NaN           NaN
%     vint94                0           0         NaN           NaN
%     vint95           -48.13      24.993     -1.9257      0.055637
%     vint96          -48.405      25.672     -1.8855      0.060892
%     vint97          -69.925      21.611     -3.2356     0.0014332
%     vint98                0           0         NaN           NaN
%     vint99           -63.93      25.002      -2.557      0.011343
%     vint00          -77.275      21.598     -3.5779    0.00044011
%     vint01          -74.512      22.073     -3.3756    0.00089378
%     vint02          -68.734      21.386      -3.214     0.0015392
%     vint03            -72.4      21.031     -3.4425    0.00070972
%     vint04          -73.569       21.29     -3.4555    0.00067822
%     vint05           -74.34      20.142     -3.6908    0.00029236
%     vint06          -77.302      20.928     -3.6938    0.00028913
%     vint07          -73.192      22.155     -3.3036     0.0011418
%     vint08                0           0         NaN           NaN
%     vint09                0           0         NaN           NaN
%     vint10                0           0         NaN           NaN
%     size_t           1.1082      2.2183     0.49955       0.61797
% 
% 
% Number of observations: 213, Error degrees of freedom: 197
% Root Mean Squared Error: 40.3
% R-squared: 0.179,  Adjusted R-Squared 0.116
% F-statistic vs. constant model: 2.86, p-value = 0.000443
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -8.0010e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9721
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Cannot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
MED_SPREAD.uRaw = regirr1.Residuals.Raw; 
MED_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr1White = table(MED_SPREAD.irr_t, MED_SPREAD.seq_num_t, MED_SPREAD.size_t, MED_SPREAD.irr_t1,  MED_SPREAD.vint91, MED_SPREAD.vint92, MED_SPREAD.vint93, MED_SPREAD.vint94, ...
    MED_SPREAD.vint95, MED_SPREAD.vint96, MED_SPREAD.vint97, MED_SPREAD.vint98, MED_SPREAD.vint99,MED_SPREAD.vint00, MED_SPREAD.vint01, MED_SPREAD.vint02, MED_SPREAD.vint03, MED_SPREAD.vint04,...
    MED_SPREAD.vint05, MED_SPREAD.vint06, MED_SPREAD.vint07, MED_SPREAD.vint08, MED_SPREAD.vint09, MED_SPREAD.vint10, MED_SPREAD.size_t.^2, MED_SPREAD.irr_t1.^2, MED_SPREAD.uRaw2);
 
irr1White.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(irr1White,'uRaw2 ~ seq_num_t + size_t + irr_t1  + vint91+vint92 +vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
 
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat        pValue  
%                    ________    ________    ________    __________
% 
%     (Intercept)           0           0         NaN           NaN
%     seq_num_t        195.94       311.8     0.62841        0.5305
%     size_t           1450.4        2888     0.50224       0.61609
%     irr_t1           118.78      34.992      3.3945    0.00083955
%     vint91                0           0         NaN           NaN
%     vint92                0           0         NaN           NaN
%     vint93                0           0         NaN           NaN
%     vint94                0           0         NaN           NaN
%     vint95          -6863.6      9201.4    -0.74593       0.45664
%     vint96          -6286.6      9446.3    -0.66551       0.50655
%     vint97          -5848.6      8612.3     -0.6791       0.49791
%     vint98            23173      9271.6      2.4993      0.013305
%     vint99          -2903.4      8934.8    -0.32496       0.74558
%     vint00          -6130.7      8722.8    -0.70284       0.48303
%     vint01          -4118.5      8811.2    -0.46742       0.64074
%     vint02          -4945.4      8572.8    -0.57687       0.56472
%     vint03          -5406.5        8563    -0.63138       0.52857
%     vint04          -5596.5      8431.6    -0.66375       0.50767
%     vint05          -6034.4      8560.1    -0.70494       0.48172
%     vint06          -6095.8      8574.3    -0.71094       0.47801
%     vint07          -5694.7      8778.9    -0.64868       0.51734
%     vint08                0           0         NaN           NaN
%     vint09                0           0         NaN           NaN
%     vint10                0           0         NaN           NaN
%     size_sq         -135.27      245.08    -0.55193       0.58165
%     irr_t1sq       -0.11123    0.092606     -1.2011       0.23124
% 
% 
% Number of observations: 213, Error degrees of freedom: 195
% Root Mean Squared Error: 8.64e+03
% R-squared: 0.376,  Adjusted R-Squared 0.322
% F-statistic vs. constant model: 6.91, p-value = 6.44e-13
 
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 80.0943, Crit_Val_e =  24.9958, P_e = 6.7127e-11,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MED_SPREAD.seq_num_t MED_SPREAD.size_t MED_SPREAD.irr_t1 MED_SPREAD.vint95  MED_SPREAD.vint96  MED_SPREAD.vint97...
    MED_SPREAD.vint99 MED_SPREAD.vint00 MED_SPREAD.vint01 MED_SPREAD.vint02 MED_SPREAD.vint03 MED_SPREAD.vint04...
    MED_SPREAD.vint05 MED_SPREAD.vint06 MED_SPREAD.vint07];
 
y = MED_SPREAD.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1','vint95','vint96','vint97',...
    'vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07'};
White
    
% 
% White =
% 
%   16?4 table
% 
%                      beta        SE        tstat         p   
%                    ________    _______    ________    _______
% 
%     (Intercept)      78.257     58.837      1.3301    0.18493
%     seq_num_t      -0.73886    0.53825     -1.3727     0.1713
%     size_t           1.1082     1.0086      1.0988    0.27313
%     irr_t1          0.22816    0.18836      1.2113    0.22715
%     vint95           -48.13     60.201    -0.79948    0.42491
%     vint96          -48.405     65.938    -0.73409    0.46371
%     vint97          -69.925     65.566     -1.0665    0.28743
%     vint99           -63.93      63.36      -1.009    0.31413
%     vint00          -77.275     59.771     -1.2928    0.19748
%     vint01          -74.512     59.166     -1.2594    0.20929
%     vint02          -68.734     59.009     -1.1648    0.24541
%     vint03            -72.4     59.466     -1.2175    0.22478
%     vint04          -73.569     59.318     -1.2402    0.21626
%     vint05           -74.34      59.44     -1.2507    0.21244
%     vint06          -77.302     59.418      -1.301    0.19468
%     vint07          -73.192     59.632     -1.2274    0.22104
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MED_SPREAD.seq_num_t,u)
corr_size_t_u = corr(MED_SPREAD.size_t,u)
corr_irr_t1_u = corr(MED_SPREAD.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 4.8569
% kurt_jbtest = 49.0126
% test_stat_jbtest = 1.9627e+04
% crit_val_jbtest = 27.4884
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = MED_SPREAD(regirr1.Diagnostics.CooksDistance >=0.05,:);

 
% Linear regression reg1irr w/ Buyout Dummy
 
regirr1 = fitlm(MED_SPREAD, 'irr_t~seq_num_t + BuyoutDummy + size_t + irr_t1 + vint91 + vint92 + vint93 +  vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat        pValue  
%                    ________    ________    _________    __________
% 
%     (Intercept)      84.468      23.047        3.665    0.00032163
%     BuyoutDummy      7.8656      7.1533       1.0996       0.27292
%     irr_t1          0.21838    0.077568       2.8153      0.005392
%     seq_num_t      -0.32908      1.4898     -0.22089       0.82542
%     vint91                0           0          NaN           NaN
%     vint92                0           0          NaN           NaN
%     vint93                0           0          NaN           NaN
%     vint94                0           0          NaN           NaN
%     vint95          -52.005      25.227      -2.0615      0.040631
%     vint96          -49.748      25.687      -1.9367      0.054282
%     vint97          -71.805      21.667       -3.314     0.0011034
%     vint98                0           0          NaN           NaN
%     vint99          -65.148      25.013      -2.6045     0.0099354
%     vint00          -81.605      21.943       -3.719    0.00026392
%     vint01          -77.567      22.236      -3.4884    0.00060521
%     vint02          -74.728      22.058      -3.3877    0.00085839
%     vint03           -77.21      21.471      -3.5961     0.0004128
%     vint04          -79.502      21.952      -3.6215    0.00037662
%     vint05          -80.099      20.802      -3.8506    0.00016144
%     vint06          -80.725      21.147      -3.8174      0.000183
%     vint07          -77.115      22.429      -3.4382    0.00072116
%     vint08                0           0          NaN           NaN
%     vint09                0           0          NaN           NaN
%     vint10                0           0          NaN           NaN
%     size_t         -0.16377      2.5008    -0.065488       0.94785
% 
% 
% Number of observations: 213, Error degrees of freedom: 196
% Root Mean Squared Error: 40.3
% R-squared: 0.184,  Adjusted R-Squared 0.117
% F-statistic vs. constant model: 2.76, p-value = 0.000517
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -9.1223e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9721
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
MED_SPREAD.uRaw = regirr1.Residuals.Raw; 
MED_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr1White = table(MED_SPREAD.irr_t, MED_SPREAD.seq_num_t, MED_SPREAD.BuyoutDummy, MED_SPREAD.size_t, MED_SPREAD.irr_t1, MED_SPREAD.vint91, MED_SPREAD.vint92, MED_SPREAD.vint93, MED_SPREAD.vint94, ...
    MED_SPREAD.vint95, MED_SPREAD.vint96, MED_SPREAD.vint97, MED_SPREAD.vint98, MED_SPREAD.vint99,MED_SPREAD.vint00, MED_SPREAD.vint01, MED_SPREAD.vint02, MED_SPREAD.vint03, MED_SPREAD.vint04,...
    MED_SPREAD.vint05, MED_SPREAD.vint06, MED_SPREAD.vint07, MED_SPREAD.vint08, MED_SPREAD.vint09, MED_SPREAD.vint10, MED_SPREAD.size_t.^2, MED_SPREAD.irr_t1.^2, MED_SPREAD.uRaw2);
 
irr1White.Properties.VariableNames={'irr_t','seq_num_t','BuyoutDummy', 'size_t','irr_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(irr1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + irr_t1 + vint91 +vint92 + vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 27 terms in 26 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat        pValue  
%                    ________    ________    ________    __________
% 
%     (Intercept)           0           0         NaN           NaN
%     seq_num_t        106.66      323.19     0.33001       0.74176
%     BuyoutDummy     -2030.8      1603.2     -1.2668       0.20682
%     size_t           1666.6      2918.6     0.57102       0.56867
%     irr_t1           133.48      36.814      3.6258    0.00037183
%     vint91                0           0         NaN           NaN
%     vint92                0           0         NaN           NaN
%     vint93                0           0         NaN           NaN
%     vint94                0           0         NaN           NaN
%     vint95          -7271.6      9290.8    -0.78267       0.43482
%     vint96          -7718.3      9599.4    -0.80405        0.4224
%     vint97          -6709.8      8718.7    -0.76959       0.44252
%     vint98            21649      9459.1      2.2887      0.023219
%     vint99          -4185.5        9063    -0.46182       0.64475
%     vint00          -6549.3      8800.8    -0.74416       0.45772
%     vint01          -4665.7      8895.3    -0.52451       0.60055
%     vint02            -4811      8645.4    -0.55648       0.57855
%     vint03          -5646.8      8636.5    -0.65382       0.51403
%     vint04          -5583.7      8502.5     -0.6567       0.51218
%     vint05          -6042.1        8632    -0.69996       0.48483
%     vint06          -6684.6      8659.8    -0.77192       0.44114
%     vint07          -6112.9      8859.6    -0.68997       0.49107
%     vint08                0           0         NaN           NaN
%     vint09                0           0         NaN           NaN
%     vint10                0           0         NaN           NaN
%     size_sq         -126.87      247.18    -0.51328       0.60837
%     irr_t1sq       -0.15254    0.096808     -1.5757       0.11678
% 
% 
% Number of observations: 213, Error degrees of freedom: 194
% Root Mean Squared Error: 8.71e+03
% R-squared: 0.378,  Adjusted R-Squared 0.32
% F-statistic vs. constant model: 6.54, p-value = 1.41e-12
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  80.4232, Crit_Val_e =  26.2962 P_e = 1.3959e-10,
%reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MED_SPREAD.seq_num_t MED_SPREAD.size_t MED_SPREAD.irr_t1 MED_SPREAD.BuyoutDummy...
    MED_SPREAD.vint95  MED_SPREAD.vint96  MED_SPREAD.vint97...
     MED_SPREAD.vint99 MED_SPREAD.vint00 MED_SPREAD.vint01 MED_SPREAD.vint02...
    MED_SPREAD.vint03 MED_SPREAD.vint04 MED_SPREAD.vint05 MED_SPREAD.vint06 MED_SPREAD.vint07];
 
y = MED_SPREAD.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'BuyoutDummy', ...
    'vint95','vint96','vint97','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07'};
White
 
% White =
% 
%   17?4 table
% 
%                      beta        SE        tstat         p    
%                    ________    _______    ________    ________
% 
%     (Intercept)      84.468      58.98      1.4321     0.15358
%     seq_num_t      -0.32908    0.45101    -0.72965     0.46641
%     size_t         -0.16377     1.2191    -0.13434     0.89326
%     irr_t1          0.21838    0.18648       1.171      0.2429
%     BuyoutDummy      7.8656     3.4491      2.2805    0.023577
%     vint95          -52.005     60.391    -0.86115     0.39013
%     vint96          -49.748     66.367     -0.7496     0.45433
%     vint97          -71.805     65.922     -1.0892     0.27729
%     vint99          -65.148     63.522     -1.0256     0.30625
%     vint00          -81.605     60.044     -1.3591     0.17557
%     vint01          -77.567     59.247     -1.3092     0.19188
%     vint02          -74.728     59.098     -1.2645     0.20745
%     vint03           -77.21     59.652     -1.2943     0.19696
%     vint04          -79.502     59.382     -1.3388     0.18207
%     vint05          -80.099     59.516     -1.3458      0.1798
%     vint06          -80.725      59.69     -1.3524     0.17769
%     vint07          -77.115     59.731      -1.291      0.1981
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MED_SPREAD.seq_num_t,u)
corr_size_t_u = corr(MED_SPREAD.size_t,u)
corr_irr_t1_u = corr(MED_SPREAD.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 4.9862
% kurt_jbtest = 50.2956
% test_stat_jbtest = 2.0735e+04
% crit_val_jbtest = 28.8454
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = MED_SPREAD(regirr1.Diagnostics.CooksDistance >=0.05,:);

 
% Linear regression reg1irr w/ Buyout Dummy*irr_t1
 
MED_SPREAD.interact=MED_SPREAD.BuyoutDummy.*MED_SPREAD.irr_t1
 
regirr1 = fitlm(MED_SPREAD, 'irr_t~seq_num_t + BuyoutDummy + interact+ size_t + irr_t1 + vint91 + vint92 + vint93 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% %  
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat        pValue  
%                    ________    ________    ________    __________
% 
%     (Intercept)      83.278      23.114       3.603    0.00040321
%     BuyoutDummy      11.601      8.5158      1.3623       0.17474
%     irr_t1          0.24165    0.082781      2.9192     0.0039402
%     seq_num_t      -0.32746      1.4911    -0.21961       0.82642
%     vint91                0           0         NaN           NaN
%     vint92                0           0         NaN           NaN
%     vint93                0           0         NaN           NaN
%     vint94                0           0         NaN           NaN
%     vint95          -50.569      25.311     -1.9979      0.047176
%     vint96           -48.43      25.761       -1.88      0.061669
%     vint97          -71.037      21.707     -3.2726     0.0012694
%     vint98                0           0         NaN           NaN
%     vint99          -63.557      25.112     -2.5309      0.012201
%     vint00           -78.98        22.2     -3.5577    0.00047417
%     vint01          -75.823      22.359     -3.3911    0.00084935
%     vint02          -73.469      22.132     -3.3195      0.001084
%     vint03          -75.355      21.611     -3.4868    0.00060912
%     vint04          -77.276      22.143     -3.4899    0.00060261
%     vint05          -77.932      20.991     -3.7127    0.00027049
%     vint06          -77.932      21.444     -3.6342    0.00036025
%     vint07          -75.131      22.582      -3.327     0.0010568
%     vint08                0           0         NaN           NaN
%     vint09                0           0         NaN           NaN
%     vint10                0           0         NaN           NaN
%     size_t         -0.37789      2.5169    -0.15014       0.88081
%     interact       -0.18673     0.23049    -0.81017       0.41887
% 
% 
% Number of observations: 213, Error degrees of freedom: 195
% Root Mean Squared Error: 40.3
% R-squared: 0.187,  Adjusted R-Squared 0.116
% F-statistic vs. constant model: 2.63, p-value = 0.00072
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.0652e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9722
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
MED_SPREAD.uRaw = regirr1.Residuals.Raw; 
MED_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr1White = table(MED_SPREAD.irr_t, MED_SPREAD.seq_num_t, MED_SPREAD.BuyoutDummy, MED_SPREAD.interact, MED_SPREAD.size_t, MED_SPREAD.irr_t1,  MED_SPREAD.vint91, MED_SPREAD.vint92, MED_SPREAD.vint93, MED_SPREAD.vint94, ...
    MED_SPREAD.vint95, MED_SPREAD.vint96, MED_SPREAD.vint97, MED_SPREAD.vint98, MED_SPREAD.vint99,MED_SPREAD.vint00, MED_SPREAD.vint01, MED_SPREAD.vint02, MED_SPREAD.vint03, MED_SPREAD.vint04,...
    MED_SPREAD.vint05, MED_SPREAD.vint06, MED_SPREAD.vint07, MED_SPREAD.vint08, MED_SPREAD.vint09, MED_SPREAD.vint10, MED_SPREAD.size_t.^2, MED_SPREAD.irr_t1.^2, MED_SPREAD.uRaw2);
 
irr1White.Properties.VariableNames={'irr_t','seq_num_t','BuyoutDummy', 'interact',  'size_t','irr_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(irr1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact + irr_t1  + vint91 + vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate      SE        tStat        pValue  
%                    ________    _______    ________    __________
% 
%     (Intercept)          0           0         NaN           NaN
%     seq_num_t       122.97      311.36     0.39493       0.69335
%     BuyoutDummy     675.41      1773.8     0.38076       0.70381
%     interact       -170.98      55.335     -3.0899     0.0023111
%     size_t          877.78      2823.1     0.31093        0.7562
%     irr_t1           208.4      43.407      4.8011    3.2458e-06
%     vint91               0           0         NaN           NaN
%     vint92               0           0         NaN           NaN
%     vint93               0           0         NaN           NaN
%     vint94               0           0         NaN           NaN
%     vint95         -6426.9      8953.2    -0.71784       0.47376
%     vint96         -8492.1      9249.9    -0.91808       0.35977
%     vint97         -5932.8      8401.5    -0.70617       0.48097
%     vint98           19151      9133.8      2.0967      0.037376
%     vint99         -3172.3        8735    -0.36317       0.71689
%     vint00         -4046.6      8516.8    -0.47513       0.63525
%     vint01         -2268.3      8604.5    -0.26362       0.79236
%     vint02         -3045.7      8347.6    -0.36486       0.71563
%     vint03         -3616.4      8345.1    -0.43335       0.66526
%     vint04         -3299.3      8223.2    -0.40121       0.68873
%     vint05         -3868.6      8344.5    -0.46362       0.64347
%     vint06         -4131.2      8382.1    -0.49286       0.62269
%     vint07           -4068      8559.5    -0.47527       0.63516
%     vint08               0           0         NaN           NaN
%     vint09               0           0         NaN           NaN
%     vint10               0           0         NaN           NaN
%     size_sq        -75.619      238.68    -0.31683       0.75173
%     irr_t1sq       -0.3032     0.10749     -2.8206      0.005317
% 
% 
% Number of observations: 213, Error degrees of freedom: 193
% Root Mean Squared Error: 8.39e+03
% R-squared: 0.412,  Adjusted R-Squared 0.354
% F-statistic vs. constant model: 7.12, p-value = 3.06e-14
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =87.7906, Crit_Val_e =  27.5871, P_e = 1.5365e-11,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MED_SPREAD.seq_num_t MED_SPREAD.size_t MED_SPREAD.irr_t1 MED_SPREAD.BuyoutDummy MED_SPREAD.interact...
    MED_SPREAD.vint95  MED_SPREAD.vint96  MED_SPREAD.vint97...
    MED_SPREAD.vint99 MED_SPREAD.vint00 MED_SPREAD.vint01 MED_SPREAD.vint02...
    MED_SPREAD.vint03 MED_SPREAD.vint04  MED_SPREAD.vint05  MED_SPREAD.vint06  MED_SPREAD.vint07];
 
y = MED_SPREAD.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'BuyoutDummy', 'interact' ...
    'vint95','vint96','vint97','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07'};
White
    
% White =
% 
%   18?4 table
% 
%                      beta        SE        tstat          p    
%                    ________    _______    ________    _________
% 
%     (Intercept)      83.278     58.666      1.4195      0.15722
%     seq_num_t      -0.32746    0.44991    -0.72783      0.46752
%     size_t         -0.37789     1.2079    -0.31286       0.7547
%     irr_t1          0.24165     0.2173      1.1121      0.26736
%     BuyoutDummy      11.601     3.9052      2.9707    0.0033159
%     interact       -0.18673    0.22073    -0.84597      0.39853
%     vint95          -50.569     59.462    -0.85045      0.39604
%     vint96           -48.43      64.82    -0.74714      0.45581
%     vint97          -71.037     65.301     -1.0878      0.27791
%     vint99          -63.557      62.67     -1.0141      0.31168
%     vint00           -78.98     58.573     -1.3484      0.17898
%     vint01          -75.823     58.472     -1.2967      0.19614
%     vint02          -73.469     58.414     -1.2577      0.20988
%     vint03          -75.355     58.865     -1.2801      0.20191
%     vint04          -77.276     58.625     -1.3182      0.18888
%     vint05          -77.932      58.72     -1.3272      0.18588
%     vint06          -77.932     58.777     -1.3259      0.18631
%     vint07          -75.131     58.873     -1.2762       0.2033
 
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MED_SPREAD.seq_num_t,u)
corr_size_t_u = corr(MED_SPREAD.size_t,u)
corr_irr_t1_u = corr(MED_SPREAD.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 4.8820
% kurt_jbtest = 49.5035
% test_stat_jbtest = 2.0039e+04
% crit_val_jbtest = 30.1910
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = MED_SPREAD(regirr1.Diagnostics.CooksDistance >=0.05,:);

% Linear regression reg1mult w/o Buyout Dummy
 
regmult1 = fitlm(MED_SPREAD, 'mult_t~seq_num_t + size_t + mult_t1  + vint91 + vint92 + vint93 +vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)      0.96083      1.0981      0.87502       0.38267
%     mult_t1          0.40243     0.06777       5.9381    1.3575e-08
%     seq_num_t      -0.016236    0.061704     -0.26313       0.79274
%     vint91                 0           0          NaN           NaN
%     vint92                 0           0          NaN           NaN
%     vint93                 0           0          NaN           NaN
%     vint94                 0           0          NaN           NaN
%     vint95            0.5736      1.1338      0.50591       0.61351
%     vint96            1.7037      1.1143       1.5289       0.12796
%     vint97          -0.53342      1.0104     -0.52794       0.59816
%     vint98                 0           0          NaN           NaN
%     vint99          -0.93264      1.1419     -0.81672       0.41511
%     vint00           -0.4733      1.0161     -0.46579        0.6419
%     vint01          -0.18669      1.0493     -0.17792       0.85897
%     vint02         -0.012642      1.0173    -0.012428        0.9901
%     vint03          -0.21197     0.99889     -0.21221       0.83218
%     vint04          -0.03868      1.0094     -0.03832       0.96947
%     vint05          -0.23014     0.96049     -0.23961       0.81089
%     vint06          -0.15097     0.99066     -0.15239       0.87904
%     vint07          0.025348        1.04     0.024372       0.98058
%     vint08                 0           0          NaN           NaN
%     vint09                 0           0          NaN           NaN
%     vint10                 0           0          NaN           NaN
%     size_t          0.040748      0.0947      0.43029       0.66748
% 
% 
% Number of observations: 213, Error degrees of freedom: 197
% Root Mean Squared Error: 1.72
% R-squared: 0.282,  Adjusted R-Squared 0.227
% F-statistic vs. constant model: 5.15, p-value = 1.36e-08
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -4.1260e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9721
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Cannot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
MED_SPREAD.uRaw = regmult1.Residuals.Raw; 
MED_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(MED_SPREAD.mult_t, MED_SPREAD.seq_num_t, MED_SPREAD.size_t, MED_SPREAD.mult_t1,  MED_SPREAD.vint91, MED_SPREAD.vint92, MED_SPREAD.vint93, MED_SPREAD.vint94, ...
    MED_SPREAD.vint95, MED_SPREAD.vint96, MED_SPREAD.vint97, MED_SPREAD.vint98, MED_SPREAD.vint99,MED_SPREAD.vint00, MED_SPREAD.vint01, MED_SPREAD.vint02, MED_SPREAD.vint03, MED_SPREAD.vint04,...
    MED_SPREAD.vint05, MED_SPREAD.vint06, MED_SPREAD.vint07, MED_SPREAD.vint08, MED_SPREAD.vint09, MED_SPREAD.vint10, MED_SPREAD.size_t.^2, MED_SPREAD.mult_t1.^2, MED_SPREAD.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + mult_t1  + vint91 +vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
 
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat         pValue  
%                    _________    ________    __________    __________
% 
%     (Intercept)            0           0           NaN           NaN
%     seq_num_t       0.056738     0.39685       0.14297       0.88647
%     size_t           0.16554      3.6436      0.045433       0.96381
%     mult_t1         -0.16444      1.0458      -0.15724       0.87522
%     vint91                 0           0           NaN           NaN
%     vint92                 0           0           NaN           NaN
%     vint93                 0           0           NaN           NaN
%     vint94                 0           0           NaN           NaN
%     vint95           -1.2299      11.812      -0.10412       0.91718
%     vint96            37.563      12.397        3.0299     0.0027925
%     vint97          -0.23226       11.08     -0.020962        0.9833
%     vint98           -2.5616      12.035      -0.21284       0.83168
%     vint99            0.1941      11.431      0.016981       0.98647
%     vint00          -0.60174      11.121     -0.054109       0.95691
%     vint01         -0.018367      11.134    -0.0016496       0.99869
%     vint02           0.12641      10.887      0.011611       0.99075
%     vint03          -0.16576      10.904     -0.015201       0.98789
%     vint04         -0.066619      10.751    -0.0061966       0.99506
%     vint05          -0.31938      10.934      -0.02921       0.97673
%     vint06           0.32296       10.95      0.029495        0.9765
%     vint07          -0.18248      11.189     -0.016309       0.98701
%     vint08                 0           0           NaN           NaN
%     vint09                 0           0           NaN           NaN
%     vint10                 0           0           NaN           NaN
%     size_sq        -0.030418     0.30951     -0.098278       0.92182
%     mult_t1sq        0.24902    0.058237        4.2759    3.0315e-05
% 
% 
% Number of observations: 213, Error degrees of freedom: 195
% Root Mean Squared Error: 10.9
% R-squared: 0.556,  Adjusted R-Squared 0.517
% F-statistic vs. constant model: 14.4, p-value = 4.39e-26
 
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 118.4305, Crit_Val_e =  24.9958, P_e = 0,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MED_SPREAD.seq_num_t MED_SPREAD.size_t MED_SPREAD.mult_t1 MED_SPREAD.vint95  MED_SPREAD.vint96  MED_SPREAD.vint97...
     MED_SPREAD.vint99 MED_SPREAD.vint00 MED_SPREAD.vint01 MED_SPREAD.vint02...
    MED_SPREAD.vint03 MED_SPREAD.vint04 MED_SPREAD.vint05 MED_SPREAD.vint06 MED_SPREAD.vint07 ];
 
y = MED_SPREAD.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', ...
    'vint95','vint96','vint97',...
    'vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07'};
White
    
% White =
% 
%   16?4 table
% 
%                      beta          SE         tstat          p     
%                    _________    ________    _________    __________
% 
%     (Intercept)      0.96083     0.60253       1.5947       0.11228
%     seq_num_t      -0.016236      0.0185      -0.8776       0.38116
%     size_t          0.040748    0.044719       0.9112       0.36323
%     mult_t1          0.40243     0.11111        3.622    0.00036616
%     vint95            0.5736      0.8051      0.71246       0.47697
%     vint96            1.7037      2.5222      0.67546       0.50012
%     vint97          -0.53342     0.88274     -0.60428       0.54631
%     vint99          -0.93264      0.9454      -0.9865       0.32502
%     vint00           -0.4733     0.72792     -0.65021       0.51627
%     vint01          -0.18669     0.63776     -0.29273       0.77001
%     vint02         -0.012642     0.63406    -0.019939       0.98411
%     vint03          -0.21197     0.65565      -0.3233       0.74679
%     vint04          -0.03868     0.64744    -0.059742       0.95242
%     vint05          -0.23014     0.64445     -0.35712       0.72136
%     vint06          -0.15097     0.66389      -0.2274       0.82033
%     vint07          0.025348     0.65518     0.038688       0.96918
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MED_SPREAD.seq_num_t,u)
corr_size_t_u = corr(MED_SPREAD.size_t,u)
corr_mult_t1_u = corr(MED_SPREAD.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 2.9922
% kurt_jbtest = 33.1623
% test_stat_jbtest = 8.3920e+03
% crit_val_jbtest = 27.4884
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = MED_SPREAD(regmult1.Diagnostics.CooksDistance >=0.05,:);
 
% Linear regression reg1mult w/ Buyout Dummy
 
regmult1 = fitlm(MED_SPREAD, 'mult_t~seq_num_t + BuyoutDummy + size_t + mult_t1 + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% %  
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat       pValue  
%                    _________    ________    ________    _________
% 
%     (Intercept)       0.1047     0.88645     0.11811      0.90611
%     BuyoutDummy      0.11676     0.30588     0.38172       0.7031
%     mult_t1           0.4001    0.068189      5.8676    1.962e-08
%     seq_num_t      -0.010106     0.06389    -0.15818      0.87448
%     vint91                 0           0         NaN          NaN
%     vint92                 0           0         NaN          NaN
%     vint93                 0           0         NaN          NaN
%     vint94                 0           0         NaN          NaN
%     vint95            1.4674      1.0062      1.4584      0.14641
%     vint96            2.6378      1.0756      2.4525     0.015101
%     vint97           0.38546     0.86638     0.44491      0.65689
%     vint98            0.9565      1.1461     0.83456      0.40503
%     vint99                 0           0         NaN          NaN
%     vint00           0.41333     0.84198     0.49091      0.62407
%     vint01           0.71947     0.84167     0.85481      0.39374
%     vint02           0.84958     0.83363      1.0191      0.30945
%     vint03           0.66782     0.81079     0.82367      0.41117
%     vint04            0.8241     0.83537     0.98651      0.32515
%     vint05           0.63543     0.78068     0.81394      0.41671
%     vint06           0.74931     0.80269      0.9335      0.35176
%     vint07            0.9184     0.85649      1.0723      0.28496
%     vint08                 0           0         NaN          NaN
%     vint09                 0           0         NaN          NaN
%     vint10                 0           0         NaN          NaN
%     size_t           0.02172      0.1072     0.20261      0.83966
% 
% 
% Number of observations: 213, Error degrees of freedom: 196
% Root Mean Squared Error: 1.73
% R-squared: 0.282,  Adjusted R-Squared 0.224
% F-statistic vs. constant model: 4.82, p-value = 3.02e-08
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -2.2401e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9721
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
MED_SPREAD.uRaw = regmult1.Residuals.Raw; 
MED_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(MED_SPREAD.mult_t, MED_SPREAD.seq_num_t, MED_SPREAD.BuyoutDummy, MED_SPREAD.size_t, MED_SPREAD.mult_t1, MED_SPREAD.vint91, MED_SPREAD.vint92, MED_SPREAD.vint93, MED_SPREAD.vint94, ...
    MED_SPREAD.vint95, MED_SPREAD.vint96, MED_SPREAD.vint97, MED_SPREAD.vint98, MED_SPREAD.vint99,MED_SPREAD.vint00, MED_SPREAD.vint01, MED_SPREAD.vint02, MED_SPREAD.vint03, MED_SPREAD.vint04,...
    MED_SPREAD.vint05, MED_SPREAD.vint06, MED_SPREAD.vint07, MED_SPREAD.vint08, MED_SPREAD.vint09, MED_SPREAD.vint10, MED_SPREAD.size_t.^2, MED_SPREAD.mult_t1.^2, MED_SPREAD.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','BuyoutDummy', 'size_t','mult_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + mult_t1 + vint91+vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
% %  
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 27 terms in 26 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat        pValue  
%                    _________    _______    _________    __________
% 
%     (Intercept)            0          0          NaN           NaN
%     seq_num_t      -0.024079    0.40915    -0.058851       0.95313
%     BuyoutDummy      -1.8197     1.9841     -0.91715       0.36025
%     size_t           0.42035     3.6749      0.11439       0.90905
%     mult_t1         0.054159     1.0791     0.050189       0.96003
%     vint91                 0          0          NaN           NaN
%     vint92                 0          0          NaN           NaN
%     vint93                 0          0          NaN           NaN
%     vint94                 0          0          NaN           NaN
%     vint95           -2.0235     11.918     -0.16979       0.86536
%     vint96            35.913      12.61        2.848     0.0048941
%     vint97           -1.5211     11.233     -0.13541       0.89243
%     vint98           -3.9055     12.191     -0.32036       0.74905
%     vint99           -1.3016     11.604     -0.11217       0.91081
%     vint00           -1.3258     11.207      -0.1183       0.90596
%     vint01          -0.91794     11.237     -0.08169       0.93498
%     vint02          -0.14712     10.951    -0.013435        0.9893
%     vint03          -0.75708     10.982    -0.068939       0.94511
%     vint04          -0.39669     10.815    -0.036678       0.97078
%     vint05          -0.68554     11.002    -0.062312       0.95038
%     vint06          -0.58213     11.056    -0.052652       0.95807
%     vint07          -0.95313     11.284    -0.084468       0.93278
%     vint08                 0          0          NaN           NaN
%     vint09                 0          0          NaN           NaN
%     vint10                 0          0          NaN           NaN
%     size_sq        -0.027039    0.31121    -0.086882       0.93086
%     mult_t1sq        0.23798    0.05985       3.9763    0.00010012
% 
% 
% Number of observations: 213, Error degrees of freedom: 194
% Root Mean Squared Error: 11
% R-squared: 0.556,  Adjusted R-Squared 0.515
% F-statistic vs. constant model: 13.5, p-value = 1.73e-25
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  118.4118, Crit_Val_e =  26.2962, P_e = 0,
%reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MED_SPREAD.seq_num_t MED_SPREAD.size_t MED_SPREAD.mult_t1 MED_SPREAD.BuyoutDummy...
    MED_SPREAD.vint95  MED_SPREAD.vint96  MED_SPREAD.vint97...
    MED_SPREAD.vint98  MED_SPREAD.vint00 MED_SPREAD.vint01 MED_SPREAD.vint02...
    MED_SPREAD.vint03 MED_SPREAD.vint04 MED_SPREAD.vint05 MED_SPREAD.vint06 MED_SPREAD.vint07];
 
y = MED_SPREAD.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'BuyoutDummy' ...
    'vint95','vint96','vint97','vint98','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07'};
White
 
% White =
% 
%   17?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       0.1047     0.85688     0.12218       0.90287
%     seq_num_t      -0.010106    0.018834    -0.53659       0.59212
%     size_t           0.02172    0.050769     0.42782       0.66922
%     mult_t1           0.4001     0.11179       3.579    0.00042788
%     BuyoutDummy      0.11676     0.15624     0.74732        0.4557
%     vint95            1.4674     0.94288      1.5563       0.12114
%     vint96            2.6378      2.4721       1.067       0.28718
%     vint97           0.38546     0.85935     0.44855       0.65422
%     vint98            0.9565     0.93624      1.0216       0.30812
%     vint00           0.41333     0.83046     0.49772        0.6192
%     vint01           0.71947     0.85368     0.84279        0.4003
%     vint02           0.84958      0.8362       1.016        0.3108
%     vint03           0.66782     0.82693     0.80759       0.42024
%     vint04            0.8241     0.79781       1.033       0.30281
%     vint05           0.63543     0.80162     0.79268       0.42885
%     vint06           0.74931     0.83308     0.89945       0.36944
%     vint07            0.9184     0.81052      1.1331       0.25846
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MED_SPREAD.seq_num_t,u)
corr_size_t_u = corr(MED_SPREAD.size_t,u)
corr_mult_t1_u = corr(MED_SPREAD.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 3.0351
% kurt_jbtest = 33.3902
% test_stat_jbtest = 8.5237e+03
% crit_val_jbtest = 28.8454
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = MED_SPREAD(regmult1.Diagnostics.CooksDistance >=0.05,:);
    
% Linear regression reg1mult w/ Buyout Dummy*mult_t1
 
MED_SPREAD.interact=MED_SPREAD.BuyoutDummy.*MED_SPREAD.mult_t1
 
regmult1 = fitlm(MED_SPREAD, 'mult_t~seq_num_t + BuyoutDummy + interact+ size_t + mult_t1 + vint91 + vint92  + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)       1.5958     0.90016       1.7728      0.077885
%     BuyoutDummy      0.84609     0.51098       1.6558      0.099432
%     mult_t1          0.44472    0.072319       6.1495    4.6041e-09
%     seq_num_t      -0.011729    0.063548     -0.18457       0.85377
%     vint91                 0           0          NaN           NaN
%     vint92                 0           0          NaN           NaN
%     vint93                 0           0          NaN           NaN
%     vint94                 0           0          NaN           NaN
%     vint95                 0           0          NaN           NaN
%     vint96            1.0451      1.0698       0.9769       0.32988
%     vint97           -1.1519     0.86545       -1.331        0.1848
%     vint98          -0.82139      1.1551     -0.71107       0.47792
%     vint99           -1.4761      1.0007      -1.4751       0.14188
%     vint00          -0.92971     0.82808      -1.1227       0.26299
%     vint01          -0.73105     0.83291     -0.87771       0.38123
%     vint02          -0.65487     0.81072     -0.80777       0.42025
%     vint03          -0.77245     0.79185      -0.9755       0.33057
%     vint04          -0.61345     0.81276     -0.75478       0.45133
%     vint05          -0.75769     0.75666      -1.0014       0.31794
%     vint06          -0.63097     0.79123     -0.79745        0.4262
%     vint07           -0.5194     0.84539     -0.61439        0.5397
%     vint08                 0           0          NaN           NaN
%     vint09                 0           0          NaN           NaN
%     vint10                 0           0          NaN           NaN
%     size_t         -0.003187     0.10753    -0.029637       0.97639
%     interact        -0.36181     0.20367      -1.7765      0.077282
% 
% 
% Number of observations: 213, Error degrees of freedom: 195
% Root Mean Squared Error: 1.72
% R-squared: 0.294,  Adjusted R-Squared 0.232
% F-statistic vs. constant model: 4.77, p-value = 1.9e-08
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.3069e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9722
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
MED_SPREAD.uRaw = regmult1.Residuals.Raw; 
MED_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(MED_SPREAD.mult_t, MED_SPREAD.seq_num_t, MED_SPREAD.BuyoutDummy, MED_SPREAD.interact, MED_SPREAD.size_t, MED_SPREAD.mult_t1,  MED_SPREAD.vint91, MED_SPREAD.vint92, MED_SPREAD.vint93, MED_SPREAD.vint94, ...
    MED_SPREAD.vint95, MED_SPREAD.vint96, MED_SPREAD.vint97, MED_SPREAD.vint98, MED_SPREAD.vint99,MED_SPREAD.vint00, MED_SPREAD.vint01, MED_SPREAD.vint02, MED_SPREAD.vint03, MED_SPREAD.vint04,...
    MED_SPREAD.vint05, MED_SPREAD.vint06, MED_SPREAD.vint07, MED_SPREAD.vint08, MED_SPREAD.vint09, MED_SPREAD.vint10, MED_SPREAD.size_t.^2, MED_SPREAD.mult_t1.^2, MED_SPREAD.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','BuyoutDummy', 'interact',  'size_t','mult_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact + mult_t1  + vint91 + vint92+vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
%  
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat       pValue  
%                    _________    ________    _________    _________
% 
%     (Intercept)            0           0          NaN          NaN
%     seq_num_t      0.0013365     0.39513    0.0033824       0.9973
%     BuyoutDummy       3.5852      3.2612       1.0994      0.27303
%     interact         -3.0004      1.4108      -2.1267     0.034772
%     size_t            0.1997      3.5538     0.056194      0.95525
%     mult_t1           1.4968      1.2353       1.2116       0.2272
%     vint91                 0           0          NaN          NaN
%     vint92                 0           0          NaN          NaN
%     vint93                 0           0          NaN          NaN
%     vint94                 0           0          NaN          NaN
%     vint95           -4.1779      11.516     -0.36279      0.71718
%     vint96            31.114      12.285       2.5327     0.012151
%     vint97           -4.2208      10.882     -0.38788      0.69855
%     vint98           -5.8717      11.776     -0.49861      0.61864
%     vint99           -3.1178      11.212     -0.27809      0.78125
%     vint00           -2.2016      10.817     -0.20353      0.83894
%     vint01            -1.802      10.844     -0.16618       0.8682
%     vint02            -1.589      10.571     -0.15031      0.88069
%     vint03           -1.9427      10.599     -0.18329      0.85477
%     vint04           -1.4943      10.438     -0.14315      0.88632
%     vint05           -1.6398      10.617     -0.15445      0.87742
%     vint06           -1.5475       10.67     -0.14504      0.88484
%     vint07           -2.2477      10.891     -0.20638      0.83672
%     vint08                 0           0          NaN          NaN
%     vint09                 0           0          NaN          NaN
%     vint10                 0           0          NaN          NaN
%     size_sq        -0.016501      0.3006    -0.054893      0.95628
%     mult_t1sq        0.16927    0.064918       2.6074    0.0098669
% 
% 
% Number of observations: 213, Error degrees of freedom: 193
% Root Mean Squared Error: 10.6
% R-squared: 0.573,  Adjusted R-Squared 0.531
% F-statistic vs. constant model: 13.6, p-value = 2.02e-26
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 121.9870, Crit_Val_e =  27.5871, P_e = 0,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [MED_SPREAD.seq_num_t MED_SPREAD.size_t MED_SPREAD.mult_t1 MED_SPREAD.BuyoutDummy MED_SPREAD.interact...
    MED_SPREAD.vint96  MED_SPREAD.vint97...
    MED_SPREAD.vint98 MED_SPREAD.vint99 MED_SPREAD.vint00 MED_SPREAD.vint01 MED_SPREAD.vint02...
    MED_SPREAD.vint03 MED_SPREAD.vint04 MED_SPREAD.vint05 MED_SPREAD.vint06 MED_SPREAD.vint07];
 
y = MED_SPREAD.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'BuyoutDummy', 'interact', ...
    'vint96','vint97','vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07'};
White
    
% White =
% 
%   18?4 table
% 
%                      beta          SE         tstat          p     
%                    _________    ________    _________    __________
% 
%     (Intercept)       1.5958     0.69369       2.3005      0.022397
%     seq_num_t      -0.011729    0.017138      -0.6844       0.49447
%     size_t         -0.003187     0.04801    -0.066382       0.94714
%     mult_t1          0.44472     0.11795       3.7706    0.00021146
%     BuyoutDummy      0.84609     0.27054       3.1274     0.0020122
%     interact        -0.36181     0.12773      -2.8325     0.0050654
%     vint96            1.0451      2.1612      0.48355       0.62921
%     vint97           -1.1519     0.76451      -1.5068       0.13336
%     vint98          -0.82139     0.72575      -1.1318       0.25902
%     vint99           -1.4761     0.99212      -1.4878       0.13829
%     vint00          -0.92971     0.57506      -1.6167       0.10743
%     vint01          -0.73105     0.57518       -1.271       0.20513
%     vint02          -0.65487     0.60578       -1.081       0.28091
%     vint03          -0.77245     0.58424      -1.3221       0.18755
%     vint04          -0.61345     0.59587      -1.0295       0.30442
%     vint05          -0.75769     0.56818      -1.3335       0.18379
%     vint06          -0.63097      0.6018      -1.0485       0.29562
%     vint07           -0.5194     0.55146     -0.94185       0.34735
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(MED_SPREAD.seq_num_t,u)
corr_size_t_u = corr(MED_SPREAD.size_t,u)
corr_mult_t1_u = corr(MED_SPREAD.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 2.7396
% kurt_jbtest = 33.2051
% test_stat_jbtest = 8.3636e+03
% crit_val_jbtest = 30.1910
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = MED_SPREAD(regmult1.Diagnostics.CooksDistance >=0.05,:);

    
%% Section 5.2.2 low spread, irr and multiple

clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','SPREAD_LOW_ALL','A2:AW89');
stringVectors = string(raw(:,[4,5,6,9,10,16,17,18,19,20,21,25,26,27,28]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,11,12,13,14,15,22,23,24,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
LOW_SPREAD = table;

% Allocate imported array to column variable names
LOW_SPREAD.number = data(:,1);
LOW_SPREAD.FUNDID = data(:,2);
LOW_SPREAD.FIRMID = data(:,3);
LOW_SPREAD.teamID = stringVectors(:,1);
LOW_SPREAD.teamIDvintage = stringVectors(:,2);
LOW_SPREAD.teamIDvintagenumber = stringVectors(:,3);
LOW_SPREAD.VINTAGEYEAR = data(:,4);
LOW_SPREAD.spread = data(:,5);
LOW_SPREAD.spread_cat = categorical(stringVectors(:,4));
LOW_SPREAD.STRATEGY = categorical(stringVectors(:,5));
LOW_SPREAD.BuyoutDummy = data(:,6);
LOW_SPREAD.irr_t = data(:,7);
LOW_SPREAD.irr_t1 = data(:,8);
LOW_SPREAD.mult_t = data(:,9);
LOW_SPREAD.mult_t1 = data(:,10);
LOW_SPREAD.PRIMARYFUNDFOCUS = categorical(stringVectors(:,6));
LOW_SPREAD.COUNTRIES = categorical(stringVectors(:,7));
LOW_SPREAD.REGIONS = stringVectors(:,8);
LOW_SPREAD.STATUS = categorical(stringVectors(:,9));
LOW_SPREAD.FUNDMANAGER = stringVectors(:,10);
LOW_SPREAD.DOMICILE = categorical(stringVectors(:,11));
LOW_SPREAD.FINALCLOSESIZE = data(:,11);
LOW_SPREAD.size_ = data(:,12);
LOW_SPREAD.seq_num_t = data(:,13);
LOW_SPREAD.COREINDUSTRIES = categorical(stringVectors(:,12));
LOW_SPREAD.INDUSTRIES = stringVectors(:,13);
LOW_SPREAD.MANAGERREGION = categorical(stringVectors(:,14));
LOW_SPREAD.MANAGERCOUNTRY = categorical(stringVectors(:,15));
LOW_SPREAD.vint90 = data(:,14);
LOW_SPREAD.vint91 = data(:,15);
LOW_SPREAD.vint92 = data(:,16);
LOW_SPREAD.vint93 = data(:,17);
LOW_SPREAD.vint94 = data(:,18);
LOW_SPREAD.vint95 = data(:,19);
LOW_SPREAD.vint96 = data(:,20);
LOW_SPREAD.vint97 = data(:,21);
LOW_SPREAD.vint98 = data(:,22);
LOW_SPREAD.vint99 = data(:,23);
LOW_SPREAD.vint00 = data(:,24);
LOW_SPREAD.vint01 = data(:,25);
LOW_SPREAD.vint02 = data(:,26);
LOW_SPREAD.vint03 = data(:,27);
LOW_SPREAD.vint04 = data(:,28);
LOW_SPREAD.vint05 = data(:,29);
LOW_SPREAD.vint06 = data(:,30);
LOW_SPREAD.vint07 = data(:,31);
LOW_SPREAD.vint08 = data(:,32);
LOW_SPREAD.vint09 = data(:,33);
LOW_SPREAD.vint10 = data(:,34);

% Clear temporary variables
clearvars data raw stringVectors R;


% Log size
LOW_SPREAD.size_t=log(LOW_SPREAD.size_);
 
% Linear regression reg1irr w/o Buyout Dummy
 
regirr1 = fitlm(LOW_SPREAD, 'irr_t~seq_num_t + size_t + irr_t1  + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat      pValue 
%                    ________    ________    ________    _______
% 
%     (Intercept)      2.9516      23.223      0.1271    0.89926
%     irr_t1         0.033354    0.039649     0.84122    0.40336
%     seq_num_t      -0.51994      1.6642    -0.31243    0.75573
%     vint91                0           0         NaN        NaN
%     vint92                0           0         NaN        NaN
%     vint93                0           0         NaN        NaN
%     vint94                0           0         NaN        NaN
%     vint95                0           0         NaN        NaN
%     vint96           6.3032      26.389     0.23886    0.81198
%     vint97            36.38      24.762      1.4692    0.14669
%     vint98          -7.6334      23.916    -0.31918    0.75063
%     vint99          -13.178       23.69    -0.55629    0.57995
%     vint00          -7.4358      22.771    -0.32654    0.74508
%     vint01          -3.8904      24.397    -0.15946    0.87381
%     vint02           8.4622      29.612     0.28577    0.77598
%     vint03            9.398      29.202     0.32183    0.74863
%     vint04           10.672       22.99     0.46419    0.64409
%     vint05           2.3278      23.291    0.099945     0.9207
%     vint06          0.59284      23.028    0.025744    0.97954
%     vint07            5.356      22.019     0.24325    0.80859
%     vint08            3.447      23.303     0.14792    0.88287
%     vint09                0           0         NaN        NaN
%     vint10                0           0         NaN        NaN
%     size_t          0.73917      1.5982     0.46251    0.64528
% 
% 
% Number of observations: 88, Error degrees of freedom: 71
% Root Mean Squared Error: 19.9
% R-squared: 0.24,  Adjusted R-Squared 0.0687
% F-statistic vs. constant model: 1.4, p-value = 0.167
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.6533e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9939
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Cannot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
LOW_SPREAD.uRaw = regirr1.Residuals.Raw; 
LOW_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr1White = table(LOW_SPREAD.irr_t, LOW_SPREAD.seq_num_t, LOW_SPREAD.size_t, LOW_SPREAD.irr_t1,  LOW_SPREAD.vint91, LOW_SPREAD.vint92, LOW_SPREAD.vint93, LOW_SPREAD.vint94, ...
    LOW_SPREAD.vint95, LOW_SPREAD.vint96, LOW_SPREAD.vint97, LOW_SPREAD.vint98, LOW_SPREAD.vint99,LOW_SPREAD.vint00, LOW_SPREAD.vint01, LOW_SPREAD.vint02, LOW_SPREAD.vint03, LOW_SPREAD.vint04,...
    LOW_SPREAD.vint05, LOW_SPREAD.vint06, LOW_SPREAD.vint07, LOW_SPREAD.vint08, LOW_SPREAD.vint09, LOW_SPREAD.vint10, LOW_SPREAD.size_t.^2, LOW_SPREAD.irr_t1.^2, LOW_SPREAD.uRaw2);
 
irr1White.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(irr1White,'uRaw2 ~ seq_num_t + size_t + irr_t1  + vint91+vint92 +vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
 
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE          tStat        pValue  
%                    __________    _________    _________    __________
% 
%     (Intercept)       -60.312       726.42    -0.083026        0.9341
%     seq_num_t          31.834       54.367      0.58553       0.56031
%     size_t             13.206       216.73     0.060934       0.95161
%     irr_t1            0.01202        4.402    0.0027306       0.99783
%     vint91                  0            0          NaN           NaN
%     vint92                  0            0          NaN           NaN
%     vint93                  0            0          NaN           NaN
%     vint94                  0            0          NaN           NaN
%     vint95                  0            0          NaN           NaN
%     vint96             57.947       808.62     0.071661        0.9431
%     vint97             3442.8       764.57        4.503    3.0192e-05
%     vint98             865.09       739.47       1.1699       0.24653
%     vint99              286.9       736.85      0.38936       0.69834
%     vint00             114.86       723.84      0.15869       0.87443
%     vint01             -141.1       832.49     -0.16949       0.86597
%     vint02                  0            0          NaN           NaN
%     vint03            -23.525       890.14    -0.026429         0.979
%     vint04             1034.1       737.39       1.4024       0.16578
%     vint05             223.85       717.62      0.31193       0.75614
%     vint06             318.74       701.72      0.45423       0.65125
%     vint07             193.32       697.23      0.27726        0.7825
%     vint08             222.78       711.77      0.31299       0.75534
%     vint09            -147.73       962.02     -0.15356       0.87845
%     vint10                  0            0          NaN           NaN
%     size_sq           -7.9276       19.434     -0.40793       0.68473
%     irr_t1sq       0.00063178    0.0090506     0.069805       0.94457
% 
% 
% Number of observations: 88, Error degrees of freedom: 69
% Root Mean Squared Error: 627
% R-squared: 0.611,  Adjusted R-Squared 0.509
% F-statistic vs. constant model: 6.02, p-value = 2.13e-08
 
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 53.7570, Crit_Val_e =  26.2962, P_e = 5.6814e-06,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [LOW_SPREAD.seq_num_t LOW_SPREAD.size_t LOW_SPREAD.irr_t1 LOW_SPREAD.vint96  LOW_SPREAD.vint97...
    LOW_SPREAD.vint98 LOW_SPREAD.vint99 LOW_SPREAD.vint00 LOW_SPREAD.vint01 LOW_SPREAD.vint02...
    LOW_SPREAD.vint03 LOW_SPREAD.vint04 LOW_SPREAD.vint05 LOW_SPREAD.vint06 LOW_SPREAD.vint07 LOW_SPREAD.vint08];
 
y = LOW_SPREAD.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'};
White
%     
% White =
% 
%   17?4 table
% 
%                      beta         SE        tstat         p    
%                    ________    ________    ________    ________
% 
%     (Intercept)      2.9516      9.3978     0.31408     0.75422
%     seq_num_t      -0.51994      1.1773    -0.44164     0.65986
%     size_t          0.73917      1.2343     0.59884     0.55085
%     irr_t1         0.033354    0.026661       1.251     0.21432
%     vint96           6.3032      7.3235     0.86069      0.3918
%     vint97            36.38      31.236      1.1647     0.24737
%     vint98          -7.6334      13.558    -0.56304     0.57487
%     vint99          -13.178      7.0263     -1.8756     0.06411
%     vint00          -7.4358      5.5492       -1.34     0.18379
%     vint01          -3.8904      1.6891     -2.3033    0.023676
%     vint02           8.4622      6.8829      1.2294     0.22226
%     vint03            9.398      5.6959        1.65      0.1026
%     vint04           10.672      14.139     0.75478     0.45244
%     vint05           2.3278      7.3732     0.31571     0.75299
%     vint06          0.59284      6.9217     0.08565     0.93194
%     vint07            5.356      5.6423     0.94927     0.34514
%     vint08            3.447      6.9617     0.49513     0.62177
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(LOW_SPREAD.seq_num_t,u)
corr_size_t_u = corr(LOW_SPREAD.size_t,u)
corr_irr_t1_u = corr(LOW_SPREAD.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 0.5737
% kurt_jbtest = 8.5490
% test_stat_jbtest = 117.7304
% crit_val_jbtest = 28.8454
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = LOW_SPREAD(regirr1.Diagnostics.CooksDistance >=0.05,:);
 
% Linear regression reg1irr w/ Buyout Dummy
 
regirr1 = fitlm(LOW_SPREAD, 'irr_t~seq_num_t + BuyoutDummy + size_t + irr_t1 + vint91 + vint92 + vint93 +  vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat      pValue 
%                    ________    ________    _________    _______
% 
%     (Intercept)      11.427        20.4      0.56016    0.57735
%     BuyoutDummy    0.048871      6.1685    0.0079227     0.9937
%     irr_t1         0.033356    0.039932      0.83531     0.4067
%     seq_num_t      -0.51934      1.6778     -0.30954    0.75793
%     vint91                0           0          NaN        NaN
%     vint92                0           0          NaN        NaN
%     vint93                0           0          NaN        NaN
%     vint94                0           0          NaN        NaN
%     vint95                0           0          NaN        NaN
%     vint96          -2.1459       24.81    -0.086493    0.93135
%     vint97           27.925      23.266       1.2002    0.23454
%     vint98          -16.078      22.329     -0.72006    0.47415
%     vint99          -21.619      22.288     -0.96997    0.33577
%     vint00          -15.876      22.207     -0.71492     0.4773
%     vint01          -12.347      26.008     -0.47475    0.63661
%     vint02                0           0          NaN        NaN
%     vint03          0.93833      28.393     0.033048    0.97374
%     vint04            2.202      22.753     0.096776    0.92321
%     vint05          -6.1385      21.801     -0.28157     0.7792
%     vint06          -7.8643      21.603     -0.36404    0.71705
%     vint07          -3.1025      21.507     -0.14425    0.88576
%     vint08          -5.0161      22.135     -0.22662    0.82146
%     vint09          -8.5031      30.268     -0.28093    0.77969
%     vint10                0           0          NaN        NaN
%     size_t          0.73151      1.8777      0.38958    0.69816
% 
% 
% Number of observations: 88, Error degrees of freedom: 70
% Root Mean Squared Error: 20
% R-squared: 0.24,  Adjusted R-Squared 0.0554
% F-statistic vs. constant model: 1.3, p-value = 0.219
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.0847e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9944
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
LOW_SPREAD.uRaw = regirr1.Residuals.Raw; 
LOW_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr1White = table(LOW_SPREAD.irr_t, LOW_SPREAD.seq_num_t, LOW_SPREAD.BuyoutDummy, LOW_SPREAD.size_t, LOW_SPREAD.irr_t1, LOW_SPREAD.vint91, LOW_SPREAD.vint92, LOW_SPREAD.vint93, LOW_SPREAD.vint94, ...
    LOW_SPREAD.vint95, LOW_SPREAD.vint96, LOW_SPREAD.vint97, LOW_SPREAD.vint98, LOW_SPREAD.vint99,LOW_SPREAD.vint00, LOW_SPREAD.vint01, LOW_SPREAD.vint02, LOW_SPREAD.vint03, LOW_SPREAD.vint04,...
    LOW_SPREAD.vint05, LOW_SPREAD.vint06, LOW_SPREAD.vint07, LOW_SPREAD.vint08, LOW_SPREAD.vint09, LOW_SPREAD.vint10, LOW_SPREAD.size_t.^2, LOW_SPREAD.irr_t1.^2, LOW_SPREAD.uRaw2);
 
irr1White.Properties.VariableNames={'irr_t','seq_num_t','BuyoutDummy', 'size_t','irr_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(irr1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + irr_t1 + vint91 +vint92 + vint93+ vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
%  
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 27 terms in 26 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE          tStat        pValue  
%                    __________    _________    _________    __________
% 
%     (Intercept)       -55.941       731.75    -0.076448       0.93931
%     seq_num_t            32.3       54.792       0.5895        0.5577
%     BuyoutDummy        44.319       197.09      0.22487       0.82283
%     size_t             11.516       218.36     0.052738       0.95811
%     irr_t1           -0.10563       4.4623    -0.023671       0.98119
%     vint91                  0            0          NaN           NaN
%     vint92                  0            0          NaN           NaN
%     vint93                  0            0          NaN           NaN
%     vint94                  0            0          NaN           NaN
%     vint95                  0            0          NaN           NaN
%     vint96             70.485       816.13     0.086364       0.93146
%     vint97               3448       770.06       4.4776    3.3704e-05
%     vint98             876.88       746.39       1.1748       0.24463
%     vint99             300.76       744.63      0.40391       0.68769
%     vint00             127.71       731.11      0.17468       0.86191
%     vint01            -140.13       838.34     -0.16715       0.86781
%     vint02                  0            0          NaN           NaN
%     vint03            -23.487       896.39    -0.026202       0.97918
%     vint04             1024.9       743.71       1.3781        0.1732
%     vint05             215.38       723.69      0.29761       0.76701
%     vint06             319.48       706.65      0.45211        0.6528
%     vint07             193.27       702.13      0.27526       0.78405
%     vint08              219.1       716.97      0.30559       0.76096
%     vint09            -187.99       985.41     -0.19078       0.84933
%     vint10                  0            0          NaN           NaN
%     size_sq           -8.3907       19.687     -0.42621       0.67145
%     irr_t1sq       0.00088487    0.0091807     0.096383       0.92353
% 
% 
% Number of observations: 88, Error degrees of freedom: 68
% Root Mean Squared Error: 631
% R-squared: 0.611,  Adjusted R-Squared 0.503
% F-statistic vs. constant model: 5.63, p-value = 5.18e-08
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  53.8018, Crit_Val_e =  27.5871, P_e = 1.0654e-05,
%reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [LOW_SPREAD.seq_num_t LOW_SPREAD.size_t LOW_SPREAD.irr_t1 LOW_SPREAD.BuyoutDummy...
    LOW_SPREAD.vint96  LOW_SPREAD.vint97 LOW_SPREAD.vint98 LOW_SPREAD.vint99 LOW_SPREAD.vint00 LOW_SPREAD.vint01...
    LOW_SPREAD.vint03 LOW_SPREAD.vint04 LOW_SPREAD.vint05 LOW_SPREAD.vint06 LOW_SPREAD.vint07 LOW_SPREAD.vint08 LOW_SPREAD.vint09];
 
y = LOW_SPREAD.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'BuyoutDummy' ...
    'vint96','vint97','vint98','vint99','vint00','vint01','vint03','vint04',...
    'vint05','vint06','vint07','vint08','vint09'};
White
 
% White =
% 
%   18?4 table
% 
%                      beta         SE         tstat          p     
%                    ________    ________    _________    __________
% 
%     (Intercept)      11.427      3.2165       3.5527    0.00062185
%     seq_num_t      -0.51934      1.1695     -0.44407       0.65811
%     size_t          0.73151      1.3536      0.54042        0.5903
%     irr_t1         0.033356    0.026583       1.2548       0.21296
%     BuyoutDummy    0.048871      5.3509    0.0091332       0.99273
%     vint96          -2.1459      2.5373     -0.84575       0.40004
%     vint97           27.925      27.933       0.9997       0.32026
%     vint98          -16.078      12.772      -1.2589       0.21148
%     vint99          -21.619      6.9945      -3.0909     0.0026891
%     vint00          -15.876      4.6895      -3.3855     0.0010726
%     vint01          -12.347      6.6372      -1.8603      0.066257
%     vint03          0.93833      1.2836      0.73103       0.46675
%     vint04            2.202       14.94      0.14738       0.88317
%     vint05          -6.1385      5.2671      -1.1654       0.24706
%     vint06          -7.8643      5.4794      -1.4352       0.15484
%     vint07          -3.1025      5.5208     -0.56196        0.5756
%     vint08          -5.0161      5.6088     -0.89433       0.37364
%     vint09          -8.5031      7.8188      -1.0875       0.27985
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(LOW_SPREAD.seq_num_t,u)
corr_size_t_u = corr(LOW_SPREAD.size_t,u)
corr_irr_t1_u = corr(LOW_SPREAD.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 0.5736
% kurt_jbtest = 8.5544
% test_stat_jbtest = 117.9481
% crit_val_jbtest = 30.1910
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = LOW_SPREAD(regirr1.Diagnostics.CooksDistance >=0.05,:);
    
% Linear regression reg1irr w/ Buyout Dummy*irr_t1
 
LOW_SPREAD.interact=LOW_SPREAD.BuyoutDummy.*LOW_SPREAD.irr_t1
 
regirr1 = fitlm(LOW_SPREAD, 'irr_t~seq_num_t + BuyoutDummy + interact+ size_t + irr_t1 + vint91 + vint92 + vint93 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regirr1 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat      pValue 
%                    _________    ________    _________    _______
% 
%     (Intercept)       3.2141      23.543      0.13652    0.89185
%     BuyoutDummy      -5.9178      7.7162     -0.76692    0.44604
%     irr_t1           0.02393    0.040433      0.59185     0.5561
%     seq_num_t      -0.058153      1.7089     -0.03403    0.97296
%     vint91                 0           0          NaN        NaN
%     vint92                 0           0          NaN        NaN
%     vint93                 0           0          NaN        NaN
%     vint94                 0           0          NaN        NaN
%     vint95                 0           0          NaN        NaN
%     vint96            6.4514      27.315      0.23619    0.81406
%     vint97            38.748      25.614       1.5128    0.13541
%     vint98           -7.7296      25.067     -0.30836    0.75884
%     vint99           -13.249      25.006     -0.52986     0.5981
%     vint00           -7.7166      24.152     -0.31951    0.75042
%     vint01           -6.9494      25.256     -0.27516    0.78411
%     vint02            7.7558      30.138      0.25734    0.79777
%     vint03            8.2471      29.799      0.27676    0.78288
%     vint04            10.948      23.431      0.46726    0.64195
%     vint05            2.7561      23.807      0.11577    0.90821
%     vint06           -1.2623      23.847    -0.052934    0.95795
%     vint07            4.3856      22.792      0.19242    0.84804
%     vint08            3.5506      23.899      0.14857    0.88238
%     vint09                 0           0          NaN        NaN
%     vint10                 0           0          NaN        NaN
%     size_t           0.50469      1.8777      0.26878    0.78899
%     interact         0.31222     0.24448        1.277    0.20635
% 
% 
% Number of observations: 88, Error degrees of freedom: 69
% Root Mean Squared Error: 20
% R-squared: 0.257,  Adjusted R-Squared 0.0638
% F-statistic vs. constant model: 1.33, p-value = 0.198
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr1.NumObservations;
k = regirr1.NumEstimatedCoefficients;
u = regirr1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr1.Residuals.Raw);
Stdev_u = std(regirr1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 4.6879e-16
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9949
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
LOW_SPREAD.uRaw = regirr1.Residuals.Raw; 
LOW_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irr1White = table(LOW_SPREAD.irr_t, LOW_SPREAD.seq_num_t, LOW_SPREAD.BuyoutDummy, LOW_SPREAD.interact, LOW_SPREAD.size_t, LOW_SPREAD.irr_t1,  LOW_SPREAD.vint91, LOW_SPREAD.vint92, LOW_SPREAD.vint93, LOW_SPREAD.vint94, ...
    LOW_SPREAD.vint95, LOW_SPREAD.vint96, LOW_SPREAD.vint97, LOW_SPREAD.vint98, LOW_SPREAD.vint99,LOW_SPREAD.vint00, LOW_SPREAD.vint01, LOW_SPREAD.vint02, LOW_SPREAD.vint03, LOW_SPREAD.vint04,...
    LOW_SPREAD.vint05, LOW_SPREAD.vint06, LOW_SPREAD.vint07, LOW_SPREAD.vint08, LOW_SPREAD.vint09, LOW_SPREAD.vint10, LOW_SPREAD.size_t.^2, LOW_SPREAD.irr_t1.^2, LOW_SPREAD.uRaw2);
 
irr1White.Properties.VariableNames={'irr_t','seq_num_t','BuyoutDummy', 'interact',  'size_t','irr_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq','uRaw2'};
 
% Unrestricted regression
regirr1White=fitlm(irr1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact + irr_t1  + vint91 + vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq')
% regirr1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE          tStat         pValue  
%                    _________    ________    ___________    __________
% 
%     (Intercept)      -46.431      730.16       -0.06359       0.94951
%     seq_num_t         42.519      55.117        0.77143       0.44348
%     BuyoutDummy      -0.1496      254.73    -0.00058728       0.99953
%     interact           2.702      9.4427        0.28615       0.77575
%     size_t           -9.3379      217.88      -0.042857       0.96596
%     irr_t1           0.91827      5.5309        0.16603        0.8687
%     vint91                 0           0            NaN           NaN
%     vint92                 0           0            NaN           NaN
%     vint93                 0           0            NaN           NaN
%     vint94                 0           0            NaN           NaN
%     vint95                 0           0            NaN           NaN
%     vint96            48.521      826.23       0.058726       0.95337
%     vint97            3264.2      781.88         4.1748    9.7902e-05
%     vint98            875.63      747.53         1.1714       0.24608
%     vint99            319.91      744.28        0.42983       0.66886
%     vint00            142.75      729.91        0.19558        0.8456
%     vint01           -172.81      838.79       -0.20602       0.83747
%     vint02                 0           0            NaN           NaN
%     vint03           -19.323      895.09      -0.021588       0.98285
%     vint04             898.9      745.66         1.2055       0.23274
%     vint05            234.76      723.44         0.3245       0.74669
%     vint06            334.13      705.33        0.47373       0.63741
%     vint07            187.78      700.75        0.26796       0.78965
%     vint08            229.24      716.92        0.31976       0.75026
%     vint09           -215.92      984.76       -0.21926       0.82719
%     vint10                 0           0            NaN           NaN
%     size_sq          -6.8077      19.648       -0.34647        0.7302
%     irr_t1sq       -0.001427    0.011184       -0.12759        0.8989
% 
% 
% Number of observations: 88, Error degrees of freedom: 67
% Root Mean Squared Error: 630
% R-squared: 0.583,  Adjusted R-Squared 0.458
% F-statistic vs. constant model: 4.68, p-value = 9.2e-07 
 
% % Langrange Irriplier (LM) test
 
m = k - 1;
test_stat_e = regirr1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 51.2707, Crit_Val_e =  28.8693, P_e = 4.8389e-05,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [LOW_SPREAD.seq_num_t LOW_SPREAD.size_t LOW_SPREAD.irr_t1 LOW_SPREAD.BuyoutDummy LOW_SPREAD.interact...
    LOW_SPREAD.vint96  LOW_SPREAD.vint97 LOW_SPREAD.vint98 LOW_SPREAD.vint99 LOW_SPREAD.vint00 LOW_SPREAD.vint01 LOW_SPREAD.vint02...
    LOW_SPREAD.vint03 LOW_SPREAD.vint04 LOW_SPREAD.vint05 LOW_SPREAD.vint06 LOW_SPREAD.vint07 LOW_SPREAD.vint08];
 
y = LOW_SPREAD.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'BuyoutDummy', 'interact', ...
    'vint96','vint97','vint98','vint99','vint00','vint01','vint02','vint03','vint04',...
    'vint05', 'vint06', 'vint07','vint08'};
White
    
% White =
% 
%   19?4 table
% 
%                      beta          SE         tstat         p   
%                    _________    ________    _________    _______
% 
%     (Intercept)       3.2141      9.6095      0.33447    0.73884
%     seq_num_t      -0.058153       1.178    -0.049365    0.96074
%     size_t           0.50469      1.3361      0.37774    0.70656
%     irr_t1           0.02393    0.020144       1.1879    0.23813
%     BuyoutDummy      -5.9178      7.3091     -0.80964    0.42038
%     interact         0.31222     0.20963       1.4893    0.14006
%     vint96            6.4514      8.8166      0.73174    0.46632
%     vint97            38.748      29.708       1.3043     0.1956
%     vint98           -7.7296       14.04     -0.55054    0.58338
%     vint99           -13.249       10.25      -1.2926    0.19961
%     vint00           -7.7166      8.3563     -0.92345    0.35836
%     vint01           -6.9494      6.0661      -1.1456    0.25513
%     vint02            7.7558      7.8896      0.98304    0.32834
%     vint03            8.2471      7.1617       1.1516    0.25269
%     vint04            10.948      15.014      0.72922    0.46785
%     vint05            2.7561      7.8421      0.35145    0.72611
%     vint06           -1.2623      8.4694     -0.14904    0.88187
%     vint07            4.3856      7.0129      0.62536    0.53339
%     vint08            3.5506      8.5799      0.41383    0.68003
 
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(LOW_SPREAD.seq_num_t,u)
corr_size_t_u = corr(LOW_SPREAD.size_t,u)
corr_irr_t1_u = corr(LOW_SPREAD.irr_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 0.5982
% kurt_jbtest = 8.2295
% test_stat_jbtest = 105.5235
% crit_val_jbtest = 31.5264
% p_jbtest = 2.1316e-14
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr1.Diagnostics.CooksDistance >=0.05);
Outliers = LOW_SPREAD(regirr1.Diagnostics.CooksDistance >=0.05,:);

% Linear regression reg1mult w/o Buyout Dummy
 
regmult1 = fitlm(LOW_SPREAD, 'mult_t~seq_num_t + size_t + mult_t1  + vint91 + vint92 + vint93 +vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% %  
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat      pValue 
%                    _________    ________    ________    _______
% 
%     (Intercept)       1.5722     0.97151      1.6183    0.11051
%     mult_t1         0.024044    0.041306      0.5821    0.56255
%     seq_num_t      -0.097625    0.069338      -1.408    0.16398
%     vint91                 0           0         NaN        NaN
%     vint92                 0           0         NaN        NaN
%     vint93                 0           0         NaN        NaN
%     vint94                 0           0         NaN        NaN
%     vint95                 0           0         NaN        NaN
%     vint96         -0.098593      1.0952    -0.09002    0.92855
%     vint97           0.72996       1.028      0.7101    0.48022
%     vint98          -0.89181     0.99271    -0.89836    0.37236
%     vint99          -0.99716     0.98444     -1.0129    0.31491
%     vint00          -0.64358     0.94346    -0.68215    0.49761
%     vint01          -0.29525      1.0133    -0.29137    0.77171
%     vint02           0.50504      1.2303     0.41051     0.6828
%     vint03            1.2359      1.2138      1.0182    0.31241
%     vint04           0.10898     0.95394     0.11425     0.9094
%     vint05          -0.24416     0.96728    -0.25242    0.80152
%     vint06          -0.11917     0.95635    -0.12461    0.90122
%     vint07            0.2277     0.91417     0.24908     0.8041
%     vint08          -0.18522     0.96704    -0.19153    0.84872
%     vint09                 0           0         NaN        NaN
%     vint10                 0           0         NaN        NaN
%     size_t          0.072603     0.06603      1.0995    0.27565
% 
% 
% Number of observations: 88, Error degrees of freedom: 71
% Root Mean Squared Error: 0.826
% R-squared: 0.282,  Adjusted R-Squared 0.12
% F-statistic vs. constant model: 1.74, p-value = 0.0587
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.0149e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9939
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Cannot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
LOW_SPREAD.uRaw = regmult1.Residuals.Raw; 
LOW_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(LOW_SPREAD.mult_t, LOW_SPREAD.seq_num_t, LOW_SPREAD.size_t, LOW_SPREAD.mult_t1,  LOW_SPREAD.vint91, LOW_SPREAD.vint92, LOW_SPREAD.vint93, LOW_SPREAD.vint94, ...
    LOW_SPREAD.vint95, LOW_SPREAD.vint96, LOW_SPREAD.vint97, LOW_SPREAD.vint98, LOW_SPREAD.vint99,LOW_SPREAD.vint00, LOW_SPREAD.vint01, LOW_SPREAD.vint02, LOW_SPREAD.vint03, LOW_SPREAD.vint04,...
    LOW_SPREAD.vint05, LOW_SPREAD.vint06, LOW_SPREAD.vint07, LOW_SPREAD.vint08, LOW_SPREAD.vint09, LOW_SPREAD.vint10, LOW_SPREAD.size_t.^2, LOW_SPREAD.mult_t1.^2, LOW_SPREAD.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + mult_t1  + vint91 +vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
 
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE          tStat        pValue  
%                    __________    _________    _________    __________
% 
%     (Intercept)        0.4731       1.1821      0.40021       0.69037
%     seq_num_t       -0.019683     0.066408     -0.29639       0.76792
%     size_t           -0.08699      0.26577     -0.32731       0.74453
%     mult_t1          0.025628      0.11689      0.21925       0.82717
%     vint91                  0            0          NaN           NaN
%     vint92                  0            0          NaN           NaN
%     vint93                  0            0          NaN           NaN
%     vint94                  0            0          NaN           NaN
%     vint95                  0            0          NaN           NaN
%     vint96           -0.10052       1.0198    -0.098568        0.9218
%     vint97             4.2787      0.95481       4.4812    3.2617e-05
%     vint98            0.49611       0.9223       0.5379       0.59257
%     vint99            0.75094      0.91764      0.81833        0.4163
%     vint00            0.29508      0.89549      0.32952       0.74287
%     vint01          -0.015832      0.94842    -0.016693       0.98673
%     vint02           -0.28101       1.1805     -0.23805       0.81263
%     vint03           -0.20379       1.1553      -0.1764       0.86055
%     vint04             1.4051      0.88733       1.5836       0.11838
%     vint05            0.40918      0.90037      0.45446       0.65108
%     vint06            0.93038       0.8913       1.0439       0.30061
%     vint07            0.64149      0.85192      0.75299       0.45431
%     vint08             0.3573       0.9013      0.39642       0.69315
%     vint09                  0            0          NaN           NaN
%     vint10                  0            0          NaN           NaN
%     size_sq        -0.0053109     0.023816       -0.223       0.82427
%     mult_t1sq      -0.0009676    0.0063248     -0.15299       0.87891
% 
% 
% Number of observations: 88, Error degrees of freedom: 69
% Root Mean Squared Error: 0.767
% R-squared: 0.601,  Adjusted R-Squared 0.497
% F-statistic vs. constant model: 5.77, p-value = 4.54e-08
 
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 52.8697, Crit_Val_e =  26.2962, P_e = 7.9217e-06,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [LOW_SPREAD.seq_num_t LOW_SPREAD.size_t LOW_SPREAD.mult_t1  LOW_SPREAD.vint96 LOW_SPREAD.vint97...
    LOW_SPREAD.vint98 LOW_SPREAD.vint99 LOW_SPREAD.vint00 LOW_SPREAD.vint01 LOW_SPREAD.vint02...
    LOW_SPREAD.vint03   LOW_SPREAD.vint04 LOW_SPREAD.vint05  LOW_SPREAD.vint06...
      LOW_SPREAD.vint07  LOW_SPREAD.vint08];
 
y = LOW_SPREAD.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', ...
    'vint96','vint97','vint98','vint99','vint00','vint01','vint02','vint03','vint04',...
    'vint05','vint06','vint07','vint08'};
White
    
% White =
% 
%   17?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       1.5722     0.40561      3.8762     0.0002068
%     seq_num_t      -0.097625    0.053817      -1.814      0.073164
%     size_t          0.072603    0.057143      1.2706       0.20732
%     mult_t1         0.024044    0.026962     0.89177         0.375
%     vint96         -0.098593      0.3276    -0.30095       0.76418
%     vint97           0.72996      1.1179     0.65295       0.51553
%     vint98          -0.89181     0.44967     -1.9832      0.050531
%     vint99          -0.99716     0.36677     -2.7187     0.0079258
%     vint00          -0.64358     0.25752     -2.4991       0.01435
%     vint01          -0.29525    0.095926     -3.0779     0.0027967
%     vint02           0.50504     0.30444      1.6589       0.10077
%     vint03            1.2359     0.25542      4.8388    5.6883e-06
%     vint04           0.10898       0.534     0.20409       0.83877
%     vint05          -0.24416     0.36145    -0.67551       0.50116
%     vint06          -0.11917     0.31713    -0.37578         0.708
%     vint07            0.2277     0.29076     0.78312       0.43571
%     vint08          -0.18522     0.33289    -0.55638       0.57939
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(LOW_SPREAD.seq_num_t,u)
corr_size_t_u = corr(LOW_SPREAD.size_t,u)
corr_mult_t1_u = corr(LOW_SPREAD.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 0.3143
% kurt_jbtest = 4.7036
% test_stat_jbtest = 12.0905
% crit_val_jbtest = 28.8454
% p_jbtest = 0.7377
% reject_jbstest = 0
 
%Cannot reject H0 at 5%.
% No, the fifth assumption of the CLRM is not violated. Residuals are normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
% Linear regression reg1mult w/ Buyout Dummy
 
regmult1 = fitlm(LOW_SPREAD, 'mult_t~seq_num_t + BuyoutDummy + size_t + mult_t1 + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat       pValue 
%                    _________    ________    ________    ________
% 
%     (Intercept)       2.1184     0.84676      2.5018    0.014966
%     BuyoutDummy      0.15213     0.25537     0.59571      0.5535
%     mult_t1         0.024181    0.041495     0.58275     0.56214
%     seq_num_t      -0.095738    0.069727      -1.373     0.17461
%     vint91                 0           0         NaN         NaN
%     vint92                 0           0         NaN         NaN
%     vint93                 0           0         NaN         NaN
%     vint94                 0           0         NaN         NaN
%     vint95                 0           0         NaN         NaN
%     vint96          -0.56292      1.0259    -0.54872     0.58514
%     vint97           0.24682     0.96373     0.25611      0.7987
%     vint98           -1.3435     0.92516     -1.4522     0.15142
%     vint99            -1.436     0.92273     -1.5563     0.12465
%     vint00            -1.081     0.91902     -1.1763     0.24392
%     vint01          -0.78419      1.0768    -0.72824     0.46917
%     vint02                 0           0         NaN         NaN
%     vint03           0.73883      1.1755     0.62852     0.53194
%     vint04           -0.4196     0.94426    -0.44437      0.6583
%     vint05          -0.76219     0.90254    -0.84449     0.40159
%     vint06          -0.60869     0.89441    -0.68055     0.49865
%     vint07          -0.26595     0.89076    -0.29856     0.76626
%     vint08          -0.69324     0.91707    -0.75593     0.45251
%     vint09          -0.63262      1.2543    -0.50435     0.61578
%     vint10                 0           0         NaN         NaN
%     size_t          0.048772    0.077462     0.62963     0.53122
% 
% 
% Number of observations: 88, Error degrees of freedom: 70
% Root Mean Squared Error: 0.83
% R-squared: 0.285,  Adjusted R-Squared 0.112
% F-statistic vs. constant model: 1.64, p-value = 0.0764
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 5.8823e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9944
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
LOW_SPREAD.uRaw = regmult1.Residuals.Raw; 
LOW_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(LOW_SPREAD.mult_t, LOW_SPREAD.seq_num_t, LOW_SPREAD.BuyoutDummy, LOW_SPREAD.size_t, LOW_SPREAD.mult_t1, LOW_SPREAD.vint91, LOW_SPREAD.vint92, LOW_SPREAD.vint93, LOW_SPREAD.vint94, ...
    LOW_SPREAD.vint95, LOW_SPREAD.vint96, LOW_SPREAD.vint97, LOW_SPREAD.vint98, LOW_SPREAD.vint99,LOW_SPREAD.vint00, LOW_SPREAD.vint01, LOW_SPREAD.vint02, LOW_SPREAD.vint03, LOW_SPREAD.vint04,...
    LOW_SPREAD.vint05, LOW_SPREAD.vint06, LOW_SPREAD.vint07, LOW_SPREAD.vint08, LOW_SPREAD.vint09, LOW_SPREAD.vint10, LOW_SPREAD.size_t.^2, LOW_SPREAD.mult_t1.^2, LOW_SPREAD.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','BuyoutDummy', 'size_t','mult_t1','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + mult_t1 + vint91+vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
%  
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 27 terms in 26 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE          tStat        pValue  
%                    __________    _________    _________    __________
% 
%     (Intercept)       0.58131       1.1982      0.48513       0.62932
%     seq_num_t       -0.024641     0.066616      -0.3699       0.71274
%     BuyoutDummy      0.054301      0.24023      0.22604       0.82193
%     size_t           -0.12118      0.26653     -0.45466       0.65097
%     mult_t1         0.0031932      0.11796      0.02707       0.97849
%     vint91                  0            0          NaN           NaN
%     vint92                  0            0          NaN           NaN
%     vint93                  0            0          NaN           NaN
%     vint94                  0            0          NaN           NaN
%     vint95                  0            0          NaN           NaN
%     vint96          -0.059676       1.0579    -0.056408        0.9552
%     vint97             4.5183      0.98506       4.5868    2.2899e-05
%     vint98            0.56239       0.9661      0.58212       0.56263
%     vint99            0.72072      0.96507      0.74681       0.45805
%     vint00            0.32608      0.94137      0.34639       0.73024
%     vint01           0.010703      0.97551     0.010972       0.99128
%     vint02           -0.29689       1.2028     -0.24683       0.80587
%     vint03           -0.21595       1.1778     -0.18336       0.85513
%     vint04              1.423      0.90544       1.5716       0.12121
%     vint05            0.43655      0.91873      0.47517       0.63636
%     vint06            0.92589      0.92059       1.0058       0.31851
%     vint07              0.647      0.88141      0.73405       0.46573
%     vint08            0.37534      0.92573      0.40546       0.68656
%     vint09                  0            0          NaN           NaN
%     vint10                  0            0          NaN           NaN
%     size_sq        -0.0027926     0.024004     -0.11634       0.90777
%     mult_t1sq      0.00014295    0.0063892     0.022374       0.98222
% 
% 
% Number of observations: 88, Error degrees of freedom: 68
% Root Mean Squared Error: 0.769
% R-squared: 0.624,  Adjusted R-Squared 0.519
% F-statistic vs. constant model: 5.95, p-value = 1.95e-08
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  54.9375, Crit_Val_e =  27.5871, P_e = 7.0125e-06,
%reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is  violated.
 
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [LOW_SPREAD.seq_num_t LOW_SPREAD.size_t LOW_SPREAD.mult_t1 LOW_SPREAD.BuyoutDummy...
    LOW_SPREAD.vint96  LOW_SPREAD.vint97 LOW_SPREAD.vint98 LOW_SPREAD.vint99 LOW_SPREAD.vint00 LOW_SPREAD.vint01 ...
    LOW_SPREAD.vint03 LOW_SPREAD.vint04 LOW_SPREAD.vint05 LOW_SPREAD.vint06...
    LOW_SPREAD.vint07  LOW_SPREAD.vint08  LOW_SPREAD.vint09];
 
y = LOW_SPREAD.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'BuyoutDummy', ...
    'vint96','vint97','vint98','vint99','vint00','vint01','vint03','vint04',...
    'vint05','vint06','vint07','vint08','vint09'};
White
 
% White =
% 
%   18?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       2.1184     0.14921      14.198             0
%     seq_num_t      -0.095738    0.053528     -1.7886      0.077205
%     size_t          0.048772    0.070637     0.69046       0.49176
%     mult_t1         0.024181    0.026393     0.91621       0.36212
%     BuyoutDummy      0.15213      0.2824     0.53869        0.5915
%     vint96          -0.56292     0.11081     -5.0799    2.1689e-06
%     vint97           0.24682      1.0072     0.24506         0.807
%     vint98           -1.3435     0.35751     -3.7578    0.00031149
%     vint99            -1.436     0.39307     -3.6533    0.00044431
%     vint00            -1.081     0.21886     -4.9393    3.8155e-06
%     vint01          -0.78419     0.31312     -2.5044      0.014151
%     vint03           0.73883    0.056228       13.14             0
%     vint04           -0.4196     0.55525     -0.7557       0.45189
%     vint05          -0.76219     0.24513     -3.1093      0.002543
%     vint06          -0.60869     0.27879     -2.1833      0.031737
%     vint07          -0.26595     0.27182    -0.97839       0.33063
%     vint08          -0.69324     0.26141     -2.6519     0.0095282
%     vint09          -0.63262     0.36077     -1.7535      0.083077
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(LOW_SPREAD.seq_num_t,u)
corr_size_t_u = corr(LOW_SPREAD.size_t,u)
corr_mult_t1_u = corr(LOW_SPREAD.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 0.3073
% kurt_jbtest = 4.9364
% test_stat_jbtest = 15.1338
% crit_val_jbtest = 30.1910
% p_jbtest = 0.5858 
% reject_jbstest = 0

%Cannot reject H0 at 5%.
% No, the fifth assumption of the CLRM is not violated. Residuals are normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = LOW_SPREAD(regmult1.Diagnostics.CooksDistance >=0.05,:);
     
% Linear regression reg1mult w/ Buyout Dummy*mult_t1
 
LOW_SPREAD.interact=LOW_SPREAD.BuyoutDummy.*LOW_SPREAD.mult_t1
 
regmult1 = fitlm(LOW_SPREAD, 'mult_t~seq_num_t + BuyoutDummy + interact+ size_t + mult_t1 + vint91 + vint92  + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
%  
% regmult1 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat       pValue 
%                    _________    ________    ________    ________
% 
%     (Intercept)       2.1221      0.8458      2.5089    0.014737
%     BuyoutDummy     -0.28814     0.48179    -0.59806     0.55198
%     mult_t1         0.012856    0.042761     0.30066     0.76468
%     seq_num_t      -0.085109    0.070343     -1.2099      0.2309
%     vint91                 0           0         NaN         NaN
%     vint92                 0           0         NaN         NaN
%     vint93                 0           0         NaN         NaN
%     vint94                 0           0         NaN         NaN
%     vint95                 0           0         NaN         NaN
%     vint96           -0.5397      1.0249    -0.52656     0.60038
%     vint97           0.37138     0.96954     0.38304       0.703
%     vint98           -1.3333     0.92415     -1.4427     0.15413
%     vint99           -1.4224     0.92176     -1.5432     0.12788
%     vint00           -1.0595     0.91819     -1.1539     0.25298
%     vint01          -0.84273       1.077     -0.7825      0.4369
%     vint02                 0           0         NaN         NaN
%     vint03           0.72435      1.1742     0.61687     0.53958
%     vint04          -0.34733     0.94557    -0.36733     0.71462
%     vint05          -0.70767     0.90293    -0.78374     0.43617
%     vint06          -0.63153     0.89364    -0.70669     0.48241
%     vint07            -0.275     0.88978    -0.30907     0.75831
%     vint08          -0.68502     0.91605    -0.74779     0.45741
%     vint09          -0.62313      1.2529    -0.49734     0.62071
%     vint10                 0           0         NaN         NaN
%     size_t          0.043223    0.077545     0.55739     0.57927
%     interact         0.21879     0.20312      1.0772     0.28558
% 
% 
% Number of observations: 88, Error degrees of freedom: 69
% Root Mean Squared Error: 0.829
% R-squared: 0.297,  Adjusted R-Squared 0.114
% F-statistic vs. constant model: 1.62, p-value = 0.0792
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult1.NumObservations;
k = regmult1.NumEstimatedCoefficients;
u = regmult1.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult1.Residuals.Raw);
Stdev_u = std(regmult1.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 6.0918e-16
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9949
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
LOW_SPREAD.uRaw = regmult1.Residuals.Raw; 
LOW_SPREAD.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
mult1White = table(LOW_SPREAD.mult_t, LOW_SPREAD.seq_num_t, LOW_SPREAD.BuyoutDummy, LOW_SPREAD.interact, LOW_SPREAD.size_t, LOW_SPREAD.mult_t1,  LOW_SPREAD.vint91, LOW_SPREAD.vint92, LOW_SPREAD.vint93, LOW_SPREAD.vint94, ...
    LOW_SPREAD.vint95, LOW_SPREAD.vint96, LOW_SPREAD.vint97, LOW_SPREAD.vint98, LOW_SPREAD.vint99,LOW_SPREAD.vint00, LOW_SPREAD.vint01, LOW_SPREAD.vint02, LOW_SPREAD.vint03, LOW_SPREAD.vint04,...
    LOW_SPREAD.vint05, LOW_SPREAD.vint06, LOW_SPREAD.vint07, LOW_SPREAD.vint08, LOW_SPREAD.vint09, LOW_SPREAD.vint10, LOW_SPREAD.size_t.^2, LOW_SPREAD.mult_t1.^2, LOW_SPREAD.uRaw2);
 
mult1White.Properties.VariableNames={'mult_t','seq_num_t','BuyoutDummy', 'interact',  'size_t','mult_t1', 'vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq','uRaw2'};
 
% Unrestricted regression
regmult1White=fitlm(mult1White,'uRaw2 ~ seq_num_t + size_t + BuyoutDummy + interact + mult_t1  + vint91 + vint92+vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq')
 
% regmult1White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 28 terms in 27 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE          tStat        pValue  
%                    __________    _________    _________    __________
% 
%     (Intercept)       0.37866       1.2351      0.30658       0.76022
%     seq_num_t       -0.010454       0.0673     -0.15534       0.87708
%     BuyoutDummy     -0.081351      0.49949     -0.16287       0.87117
%     interact         0.092562      0.22588      0.40978       0.68342
%     size_t           -0.11331      0.27178      -0.4169       0.67824
%     mult_t1           0.04282      0.14546      0.29437       0.76949
%     vint91                  0            0          NaN           NaN
%     vint92                  0            0          NaN           NaN
%     vint93                  0            0          NaN           NaN
%     vint94                  0            0          NaN           NaN
%     vint95                  0            0          NaN           NaN
%     vint96           0.033873        1.068     0.031716        0.9748
%     vint97             4.3511       1.0019       4.3429    5.5006e-05
%     vint98            0.66726      0.97519      0.68424       0.49646
%     vint99            0.85144      0.97419      0.87399       0.38561
%     vint00            0.46066      0.95393      0.48291       0.63092
%     vint01           0.077636      0.99256     0.078217       0.93792
%     vint02           -0.17661       1.2177     -0.14504       0.88516
%     vint03          -0.089942       1.1955    -0.075232       0.94028
%     vint04             1.3427      0.91677       1.4646       0.14826
%     vint05            0.57255      0.92749      0.61731       0.53937
%     vint06             1.0639      0.93111       1.1427       0.25772
%     vint07            0.74044      0.89047      0.83152       0.40898
%     vint08            0.45359      0.93436      0.48545       0.62912
%     vint09                  0            0          NaN           NaN
%     vint10                  0            0          NaN           NaN
%     size_sq        -0.0050439     0.024407     -0.20666       0.83698
%     mult_t1sq      -0.0018196    0.0076523     -0.23778       0.81286
% 
% 
% Number of observations: 88, Error degrees of freedom: 67
% Root Mean Squared Error: 0.776
% R-squared: 0.589,  Adjusted R-Squared 0.467
% F-statistic vs. constant model: 4.81, p-value = 5.92e-07
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmult1White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e = 51.8549, Crit_Val_e =  28.8693, P_e = 3.9383e-05,
%reject_e = 1
 
% Reject H0 at 5%, so there is  heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [LOW_SPREAD.seq_num_t LOW_SPREAD.size_t LOW_SPREAD.mult_t1 LOW_SPREAD.BuyoutDummy LOW_SPREAD.interact...
     LOW_SPREAD.vint96  LOW_SPREAD.vint97 LOW_SPREAD.vint98 LOW_SPREAD.vint99 LOW_SPREAD.vint00 LOW_SPREAD.vint01...
    LOW_SPREAD.vint03 LOW_SPREAD.vint04 LOW_SPREAD.vint05 LOW_SPREAD.vint06 LOW_SPREAD.vint07 LOW_SPREAD.vint08 LOW_SPREAD.vint09];
 
y = LOW_SPREAD.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'BuyoutDummy', 'interact', ...
    'vint96','vint97','vint98','vint99','vint00','vint01','vint03','vint04','vint05','vint06','vint07','vint08','vint09'};
White
    
% White =
% 
%   19?4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       2.1221     0.14835      14.304             0
%     seq_num_t      -0.085109    0.052464     -1.6223       0.10841
%     size_t          0.043223    0.068889     0.62743       0.53204
%     mult_t1         0.012856    0.021713      0.5921       0.55533
%     BuyoutDummy     -0.28814     0.49785    -0.57877       0.56426
%     interact         0.21879     0.20171      1.0847       0.28109
%     vint96           -0.5397     0.10804     -4.9954    3.0487e-06
%     vint97           0.37138     0.90662     0.40963        0.6831
%     vint98           -1.3333     0.35509     -3.7548    0.00031474
%     vint99           -1.4224     0.38983     -3.6488    0.00045113
%     vint00           -1.0595     0.21845       -4.85     5.441e-06
%     vint01          -0.84273     0.31187     -2.7022     0.0082981
%     vint03           0.72435    0.053992      13.416             0
%     vint04          -0.34733     0.52031    -0.66755       0.50621
%     vint05          -0.70767     0.24156     -2.9295     0.0043458
%     vint06          -0.63153     0.28076     -2.2493      0.027046
%     vint07            -0.275     0.26988      -1.019       0.31108
%     vint08          -0.68502     0.25438     -2.6929     0.0085138
%     vint09          -0.62313     0.36636     -1.7009      0.092582
    
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(LOW_SPREAD.seq_num_t,u)
corr_size_t_u = corr(LOW_SPREAD.size_t,u)
corr_mult_t1_u = corr(LOW_SPREAD.mult_t1,u)
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 0.3120
% kurt_jbtest = 4.7355
% test_stat_jbtest = 12.4717
% crit_val_jbtest = 31.5264
% p_jbtest = 0.8220 
% reject_jbstest = 0
 
%Cannot reject H0 at 5%.
% No, the fifth assumption of the CLRM is notviolated. Residuals are  normally distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult1.Diagnostics.CooksDistance >=0.05);
Outliers = LOW_SPREAD(regmult1.Diagnostics.CooksDistance >=0.05,:);
 
    

 

%% Section 5.3.1 
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (A) venture_t_t-1 MULT','A2:Q603');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17]);

% Replace non-numeric cells with 0.0
R = cellfun(@(x) (~isnumeric(x) && ~islogical(x)) || isnan(x),raw); % Find non-numeric cells
raw(R) = {0.0}; % Replace non-numeric cells

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_A_Venture_MULT = table;

% Allocate imported array to column variable names
FOF_A_Venture_MULT.number = data(:,1);
FOF_A_Venture_MULT.FUNDID = data(:,2);
FOF_A_Venture_MULT.FIRMID = data(:,3);
FOF_A_Venture_MULT.teamID = categorical(stringVectors(:,1));
FOF_A_Venture_MULT.teamIDvintage = stringVectors(:,2);
FOF_A_Venture_MULT.teamIDvintagenumber = stringVectors(:,3);
FOF_A_Venture_MULT.vint_t = data(:,4);
FOF_A_Venture_MULT.vint_t1 = data(:,5);
FOF_A_Venture_MULT.mult_t = data(:,6);
FOF_A_Venture_MULT.mult_t1 = data(:,7);
FOF_A_Venture_MULT.STATUS = categorical(stringVectors(:,4));
FOF_A_Venture_MULT.FUNDMANAGER = categorical(stringVectors(:,5));
FOF_A_Venture_MULT.size_t = data(:,8);
FOF_A_Venture_MULT.size_t1 = data(:,9);
FOF_A_Venture_MULT.seq_num_t = data(:,10);
FOF_A_Venture_MULT.seq_num_t1 = data(:,11);
FOF_A_Venture_MULT.fundgrowtht1t = data(:,12);

% Clear temporary variables
clearvars data raw stringVectors R;

% log(size).


FOF_A_Venture_MULT.log_size_t1 = log(FOF_A_Venture_MULT.size_t1);


% Linear regression reg_A_Venture_MULT

regFOF_A_Venture_MULT = fitlm(FOF_A_Venture_MULT, 'fundgrowtht1t ~ mult_t1 + log_size_t1 + seq_num_t1')

% output 
% 
% 
% Linear regression model:
%     fundgrowtht1t ~ 1 + mult_t1 + seq_num_t1 + log_size_t1
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue  
%                    ________    ________    _______    __________
% 
%     (Intercept)      5.1166     0.40512      12.63    1.5536e-32
%     mult_t1        0.036499    0.038483    0.94844       0.34329
%     seq_num_t1     0.079068    0.047325     1.6707      0.095305
%     log_size_t1    -0.90996    0.082015    -11.095    4.1334e-26
% 
% 
% Number of observations: 594, Error degrees of freedom: 590
% Root Mean Squared Error: 2.52
% R-squared: 0.176,  Adjusted R-Squared 0.172
% F-statistic vs. constant model: 42.1, p-value = 1.12e-24\

% p-Values for mult_t1 =  0.34329, so insignificant. Fundgrowth does not
% respond to past performance when controlled for size and sequence number.
% (??). Maybe results are not correct because assumptions are violated.


%DIAGNOSTICS of CLRM assumptions

T = regFOF_A_Venture_MULT.NumObservations;
k = regFOF_A_Venture_MULT.NumEstimatedCoefficients;
u = regFOF_A_Venture_MULT.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_A_Venture_MULT.Residuals.Raw);
Stdev_u = std(regFOF_A_Venture_MULT.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -3.0084e-14

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9640

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_A_Venture_MULT.uRaw = u; 
FOF_A_Venture_MULT.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_A_Venture_MULT_White = table(FOF_A_Venture_MULT.fundgrowtht1t, FOF_A_Venture_MULT.mult_t1, FOF_A_Venture_MULT.seq_num_t1, FOF_A_Venture_MULT.log_size_t1 ,...
    FOF_A_Venture_MULT.mult_t1.^2, FOF_A_Venture_MULT.log_size_t1.^2, FOF_A_Venture_MULT.uRaw2);
 
FOF_A_Venture_MULT_White.Properties.VariableNames={'fundgrowtht1t','mult_t1','seq_num_t1',...
    'log_size_t1','mult_t1sq','log_size_t1sq','uRaw2'};

% Unrestricted regression
regFOF_A_Venture_MULT_White=fitlm(FOF_A_Venture_MULT_White,'uRaw2 ~ mult_t1 + seq_num_t1 + log_size_t1 + mult_t1sq + log_size_t1sq')

% regFOF_A_Venture_MULT_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ 1 + mult_t1 + seq_num_t1 + log_size_t1 + mult_t1sq + log_size_t1sq
% 
% Estimated Coefficients:
%                      Estimate       SE        tStat       pValue  
%                      ________    ________    _______    __________
% 
%     (Intercept)        111.52      15.689     7.1084    3.4166e-12
%     mult_t1           -1.7697      1.4083    -1.2567       0.20936
%     seq_num_t1         1.4691     0.83255     1.7646      0.078147
%     log_size_t1        -40.18      6.7838    -5.9229    5.3836e-09
%     mult_t1sq        0.073781    0.065141     1.1326       0.25783
%     log_size_t1sq      3.4868     0.73788     4.7255    2.8757e-06
% 
% 
% Number of observations: 594, Error degrees of freedom: 588
% Root Mean Squared Error: 43.8
% R-squared: 0.0966,  Adjusted R-Squared 0.089
% F-statistic vs. constant model: 12.6, p-value = 1.27e-11


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_A_Venture_MULT_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e = 57.4031, Crit_Val_e = 7.8147, P_e = 2.1079e-12,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_A_Venture_MULT.mult_t1 FOF_A_Venture_MULT.seq_num_t1 FOF_A_Venture_MULT.log_size_t1];

y = FOF_A_Venture_MULT.fundgrowtht1t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','seq_num_t1','log_size_t1'};
White


% White =
% 
%   4×4 table
% 
%                      beta         SE        tstat         p     
%                    ________    ________    _______    __________
% 
%     (Intercept)      5.1166     0.81022      6.315    5.3066e-10
%     mult_t1        0.036499    0.032091     1.1374       0.25584
%     seq_num_t1     0.079068    0.076669     1.0313       0.30283
%     log_size_t1    -0.90996     0.16458    -5.5289    4.8384e-08

% Ok nothing changed much
% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(FOF_A_Venture_MULT.mult_t1,u)
corr_fseq_num_t1_u = corr(FOF_A_Venture_MULT.seq_num_t1,u)
corr_log_size_t1_u = corr(FOF_A_Venture_MULT.log_size_t1,u)

%output:
% corr_mult_t1_u = 1.5842e-16
% corr_fseq_num_t1_u = 1.1079e-17
% corr_log_size_t1_u = -5.5937e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 5.4080
% kurt_jbtest = 53.5367
% test_stat_jbtest = 6.6106e+04
% crit_val_jbtest = 9.3484
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_A_Venture_MULT.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_A_Venture_MULT(regFOF_A_Venture_MULT.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 7 funds that drastically increased the fund size (15-30
% times). 
%Exclude these observations:

DrasticallyIncFund = FOF_A_Venture_MULT(FOF_A_Venture_MULT.fundgrowtht1t > 15,:)

FOF_A_Venture_MULT_excluding_outliers = FOF_A_Venture_MULT;
FOF_A_Venture_MULT_excluding_outliers(FOF_A_Venture_MULT_excluding_outliers.fundgrowtht1t > 14.9,:)=[];

regFOF_A_Venture_MULT_excluding_outliers = fitlm(FOF_A_Venture_MULT_excluding_outliers, 'fundgrowtht1t ~ mult_t1 + log_size_t1 + seq_num_t1')

% regFOF_A_Venture_MULT_excluding_outliers = 
% 
% 
% Linear regression model:
%     fundgrowtht1t ~ 1 + mult_t1 + seq_num_t1 + log_size_t1
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       3.2451     0.26308      12.335    3.1723e-31
%     mult_t1         0.081483    0.024207       3.366    0.00081273
%     seq_num_t1     -0.015449    0.029921    -0.51632       0.60583
%     log_size_t1     -0.52715    0.053242     -9.9011    1.8181e-21
% 
% 
% Number of observations: 587, Error degrees of freedom: 583
% Root Mean Squared Error: 1.58
% R-squared: 0.178,  Adjusted R-Squared 0.173
% F-statistic vs. constant model: 41.9, p-value = 1.48e-24

% Ok so looks better now. Seems like previous performance is really
% significant and positively related to fund growth over the next
% period. However the  stimate is low.

u_exc = regFOF_A_Venture_MULT_excluding_outliers.Residuals.Raw;
T_exc = regFOF_A_Venture_MULT_excluding_outliers.NumObservations;
k_exc = regFOF_A_Venture_MULT_excluding_outliers.NumEstimatedCoefficients;
m_exc = k_exc - 1;

alpha = 0.05;
 
figure(3)
histogram(u_exc)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(4)
plot(u_exc, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

skew_jbtest_exc = mean(u_exc.^3) / (std(u_exc)^2)^(3/2)
kurt_jbtest_exc = mean(u_exc.^4) / (std(u_exc)^2)^2
test_stat_jbtest_exc = T_exc * ( skew_jbtest_exc^2 / 6 + (kurt_jbtest_exc - 3)^2 / 24) 
crit_val_jbtest_exc = chi2inv(1-1/2*alpha, m_exc)
p_jbtest_exc = 1 - cdf('Chisquare', test_stat_jbtest_exc, m_exc)
reject_jbstest_exc = test_stat_jbtest_exc > crit_val_jbtest_exc

%Reject = 1. The residuals are still not normally distributed, buth who
%cares

% % Use heteroscedasticity-consistent standard errors for new subsample

x_exc = [FOF_A_Venture_MULT_excluding_outliers.mult_t1 FOF_A_Venture_MULT_excluding_outliers.seq_num_t1 FOF_A_Venture_MULT_excluding_outliers.log_size_t1];

y_exc = FOF_A_Venture_MULT_excluding_outliers.fundgrowtht1t;

% White's estimation correction 

White_exc = table();
const_exc = 1; % 1: constant is included 
lags_exc = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y_exc, x_exc);

White_exc.beta = beta;
White_exc.SE = sqrt(diag(vcvm));
White_exc.tstat = tstat;
White_exc.p = 2*(1-tcdf(abs(tstat),T-2));
White_exc.Properties.RowNames = {'(Intercept)','mult_t1','seq_num_t1','log_size_t1'};
White_exc



% White_exc =
% 
%   4×4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       3.1099     0.33846      9.1882             0
%     mult_t1         0.067181    0.025758      2.6082     0.0093317
%     seq_num_t1     -0.015473    0.029392    -0.52643       0.59879
%     log_size_t1     -0.49683    0.064414     -7.7131    5.2403e-14
 

% FOF A Venture IRR
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (A) venture_t_t-1 IRR','A2:Q603');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17]);
% Replace non-numeric cells with 0.0
R = cellfun(@(x) (~isnumeric(x) && ~islogical(x)) || isnan(x),raw); % Find non-numeric cells
raw(R) = {0.0}; % Replace non-numeric cells

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_A_Venture_IRR = table;

% Allocate imported array to column variable names
FOF_A_Venture_IRR.number = data(:,1);
FOF_A_Venture_IRR.FUNDID = data(:,2);
FOF_A_Venture_IRR.FIRMID = data(:,3);
FOF_A_Venture_IRR.teamID = categorical(stringVectors(:,1));
FOF_A_Venture_IRR.teamIDvintage = stringVectors(:,2);
FOF_A_Venture_IRR.teamIDvintagenumber = stringVectors(:,3);
FOF_A_Venture_IRR.vint_t = data(:,4);
FOF_A_Venture_IRR.vint_t1 = data(:,5);
FOF_A_Venture_IRR.irr_t = data(:,6);
FOF_A_Venture_IRR.irr_t1 = data(:,7);
FOF_A_Venture_IRR.STATUS = categorical(stringVectors(:,4));
FOF_A_Venture_IRR.FUNDMANAGER = categorical(stringVectors(:,5));
FOF_A_Venture_IRR.size_t = data(:,8);
FOF_A_Venture_IRR.size_t1 = data(:,9);
FOF_A_Venture_IRR.seq_num_t = data(:,10);
FOF_A_Venture_IRR.seq_num_t1 = data(:,11);
FOF_A_Venture_IRR.fundgrowtht1t = data(:,12);

% Clear temporary variables
clearvars data raw stringVectors R;

% log(size).


FOF_A_Venture_IRR.log_size_t1 = log(FOF_A_Venture_IRR.size_t1);


% Linear regression reg_A_Venture_IRR

regFOF_A_Venture_IRR = fitlm(FOF_A_Venture_IRR, 'fundgrowtht1t ~ irr_t1 + log_size_t1 + seq_num_t1')

% output
% regFOF_A_Venture_IRR = 
% 
% 
% Linear regression model:
%     fundgrowtht1t ~ 1 + irr_t1 + seq_num_t1 + log_size_t1
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat       pValue  
%                    _________    _________    _______    __________
% 
%     (Intercept)       5.0305      0.39745     12.657    5.3411e-32
%     irr_t1         0.0036651    0.0021456     1.7082      0.088219
%     seq_num_t1       0.09712     0.052484     1.8505      0.064846
%     log_size_t1     -0.89483     0.082774    -10.811    1.3552e-24
% 
% 
% Number of observations: 498, Error degrees of freedom: 494
% Root Mean Squared Error: 2.24
% R-squared: 0.201,  Adjusted R-Squared 0.196
% F-statistic vs. constant model: 41.3, p-value = 7.48e-24


% p-Values for irr_t1 =  0.088219, so significant. However estimate for
% irr_t1 = 0.0036651, superlow. 

%DIAGNOSTICS of CLRM assumptions

T = regFOF_A_Venture_IRR.NumObservations;
k = regFOF_A_Venture_IRR.NumEstimatedCoefficients;
u = regFOF_A_Venture_IRR.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_A_Venture_IRR.Residuals.Raw);
Stdev_u = std(regFOF_A_Venture_IRR.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 5.9299e-15


% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9648

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_A_Venture_IRR.uRaw = u; 
FOF_A_Venture_IRR.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_A_Venture_IRR_White = table(FOF_A_Venture_IRR.fundgrowtht1t, FOF_A_Venture_IRR.irr_t1, FOF_A_Venture_IRR.seq_num_t1, FOF_A_Venture_IRR.log_size_t1 ,...
    FOF_A_Venture_IRR.irr_t1.^2, FOF_A_Venture_IRR.log_size_t1.^2, FOF_A_Venture_IRR.uRaw2);
 
FOF_A_Venture_IRR_White.Properties.VariableNames={'fundgrowtht1t','irr_t1','seq_num_t1',...
    'log_size_t1','irr_t1sq','log_size_t1sq','uRaw2'};

% Unrestricted regression
regFOF_A_Venture_IRR_White=fitlm(FOF_A_Venture_IRR_White,'uRaw2 ~ irr_t1 + seq_num_t1 + log_size_t1 + irr_t1sq + log_size_t1sq')

% Linear regression model:
%     uRaw2 ~ 1 + irr_t1 + seq_num_t1 + log_size_t1 + irr_t1sq + log_size_t1sq
% 
% Estimated Coefficients:
%                       Estimate         SE         tStat       pValue  
%                      __________    __________    _______    __________
% 
%     (Intercept)          73.218        8.3922     8.7246    4.1507e-17
%     irr_t1             -0.03827      0.035939    -1.0649       0.28746
%     seq_num_t1           1.2477       0.49342     2.5286      0.011763
%     log_size_t1         -26.028        3.6775    -7.0776    5.0825e-12
%     irr_t1sq         0.00010526    0.00010427     1.0095       0.31321
%     log_size_t1sq        2.1876       0.40267     5.4326    8.7481e-08
% 
% 
% Number of observations: 498, Error degrees of freedom: 492
% Root Mean Squared Error: 20.6
% R-squared: 0.178,  Adjusted R-Squared 0.17
% F-statistic vs. constant model: 21.3, p-value = 2.49e-19


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_A_Venture_IRR_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  88.7064, Crit_Val_e = 7.8147, P_e = 0
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_A_Venture_IRR.irr_t1 FOF_A_Venture_IRR.seq_num_t1 FOF_A_Venture_IRR.log_size_t1];

y = FOF_A_Venture_IRR.fundgrowtht1t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','seq_num_t1','log_size_t1'};
White


% White =
% 
%   4×4 table
% 
%                      beta          SE         tstat         p     
%                    _________    _________    _______    __________
% 
%     (Intercept)       5.0305      0.84119     5.9802    4.2667e-09
%     irr_t1         0.0036651    0.0027839     1.3166        0.1886
%     seq_num_t1       0.09712     0.088879     1.0927       0.27505
%     log_size_t1     -0.89483      0.17632    -5.0751    5.4868e-07
%
% So koefficients became insignificant ..


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(FOF_A_Venture_IRR.irr_t1,u)
corr_seq_num_t1_u = corr(FOF_A_Venture_IRR.seq_num_t1,u)
corr_log_size_t1_u = corr(FOF_A_Venture_IRR.log_size_t1,u)

%output:
% corr_irr_t1_u =  -5.4245e-17
% corr_seq_num_t1_u =  -8.3638e-17
% corr_log_size_t1_u = -2.4644e-16

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 3.5159
% kurt_jbtest = 21.4337
% test_stat_jbtest = 8.0769e+03
% crit_val_jbtest = 9.3484
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_A_Venture_IRR.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_A_Venture_IRR(regFOF_A_Venture_IRR.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 11 funds that  either drastically increased the fund
% size (15-20 times) or reported IRRt-1 >300%.
%Exclude these observations:

DrasticallyIncFund = FOF_A_Venture_IRR(FOF_A_Venture_IRR.fundgrowtht1t > 14.9,:)
ReportedCrazyIRR  = FOF_A_Venture_IRR(FOF_A_Venture_IRR.irr_t1 > 290,:)

FOF_A_Venture_IRR_excluding_outliers = FOF_A_Venture_IRR;
FOF_A_Venture_IRR_excluding_outliers(FOF_A_Venture_IRR_excluding_outliers.fundgrowtht1t > 14.9 | FOF_A_Venture_IRR_excluding_outliers.irr_t1 > 290 ,:)=[];

regFOF_A_Venture_IRR_excluding_outliers = fitlm(FOF_A_Venture_IRR_excluding_outliers, 'fundgrowtht1t ~ irr_t1 + log_size_t1 + seq_num_t1')

% regFOF_A_Venture_IRR_excluding_outliers = 
% 
% Linear regression model:
%     fundgrowtht1t ~ 1 + irr_t1 + seq_num_t1 + log_size_t1
% 
% Estimated Coefficients:
%                     Estimate        SE         tStat        pValue  
%                    __________    ________    _________    __________
% 
%     (Intercept)        3.1469     0.27314       11.521    2.6168e-27
%     irr_t1          0.0078825    0.001922       4.1012    4.8204e-05
%     seq_num_t1     -0.0023147    0.034861    -0.066398       0.94709
%     log_size_t1      -0.50519    0.056846      -8.8869    1.2423e-17
% 
% 
% Number of observations: 488, Error degrees of freedom: 484
% Root Mean Squared Error: 1.47
% R-squared: 0.183,  Adjusted R-Squared 0.178
% F-statistic vs. constant model: 36.2, p-value = 4e-21

% P-value for irr_t1 decreased, estimate for irr_t1 increased (still
% small). Seq_num_t1 became insignificant.

u_exc = regFOF_A_Venture_IRR_excluding_outliers.Residuals.Raw;
T_exc = regFOF_A_Venture_IRR_excluding_outliers.NumObservations;
k_exc = regFOF_A_Venture_IRR_excluding_outliers.NumEstimatedCoefficients;
m_exc = k_exc - 1;

alpha = 0.05;
 
figure(3)
histogram(u_exc)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(4)
plot(u_exc, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


skew_jbtest_exc = mean(u_exc.^3) / (std(u_exc)^2)^(3/2)
kurt_jbtest_exc = mean(u_exc.^4) / (std(u_exc)^2)^2
test_stat_jbtest_exc = T_exc * ( skew_jbtest_exc^2 / 6 + (kurt_jbtest_exc - 3)^2 / 24) 
crit_val_jbtest_exc = chi2inv(1-1/2*alpha, m_exc)
p_jbtest_exc = 1 - cdf('Chisquare', test_stat_jbtest_exc, m_exc)
reject_jbstest_exc = test_stat_jbtest_exc > crit_val_jbtest_exc

%Reject = 1. The residuals are still not normally distributed, buth who
%cares

% % Use heteroscedasticity-consistent standard errors for new subsample

x_exc = [FOF_A_Venture_IRR_excluding_outliers.irr_t1 FOF_A_Venture_IRR_excluding_outliers.seq_num_t1 FOF_A_Venture_IRR_excluding_outliers.log_size_t1];

y_exc = FOF_A_Venture_IRR_excluding_outliers.fundgrowtht1t;

% White's estimation correction 

White_exc = table();
const_exc = 1; % 1: constant is included 
lags_exc = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y_exc, x_exc);

White_exc.beta = beta;
White_exc.SE = sqrt(diag(vcvm));
White_exc.tstat = tstat;
White_exc.p = 2*(1-tcdf(abs(tstat),T-2));
White_exc.Properties.RowNames = {'(Intercept)','irr_t1','seq_num_t1','log_size_t1'};
White_exc


% 
% White_exc =
% 
%   4×4 table
% 
%                       beta          SE          tstat          p     
%                    __________    _________    _________    __________
% 
%     (Intercept)        3.1469      0.39501       7.9668    1.1324e-14
%     irr_t1          0.0078825    0.0031045        2.539       0.01142
%     seq_num_t1     -0.0023147     0.039917    -0.057988       0.95378
%     log_size_t1      -0.50519     0.075947      -6.6518    7.6672e-11

% FOF A BUYOUT MULT

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (A) buyout_t_t-1 MULT','A2:Q717');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_A_Buyout_MULT = table;

% Allocate imported array to column variable names
FOF_A_Buyout_MULT.number = data(:,1);
FOF_A_Buyout_MULT.FUNDID = data(:,2);
FOF_A_Buyout_MULT.FIRMID = data(:,3);
FOF_A_Buyout_MULT.teamID = stringVectors(:,1);
FOF_A_Buyout_MULT.teamIDvintage = stringVectors(:,2);
FOF_A_Buyout_MULT.teamIDvintagenumber = stringVectors(:,3);
FOF_A_Buyout_MULT.vint_t = data(:,4);
FOF_A_Buyout_MULT.vint_t1 = data(:,5);
FOF_A_Buyout_MULT.mult_t = data(:,6);
FOF_A_Buyout_MULT.mult_t1 = data(:,7);
FOF_A_Buyout_MULT.STATUS = categorical(stringVectors(:,4));
FOF_A_Buyout_MULT.FUNDMANAGER = stringVectors(:,5);
FOF_A_Buyout_MULT.size_t = data(:,8);
FOF_A_Buyout_MULT.size_t1 = data(:,9);
FOF_A_Buyout_MULT.seq_num_t = data(:,10);
FOF_A_Buyout_MULT.seq_num_t1 = data(:,11);
FOF_A_Buyout_MULT.fundgrowtht1t = data(:,12);

% Clear temporary variables
clearvars data raw stringVectors;

% log(size).


FOF_A_Buyout_MULT.log_size_t1 = log(FOF_A_Buyout_MULT.size_t1);


% Linear regression reg_A_Venture_MULT

regFOF_A_Buyout_MULT = fitlm(FOF_A_Buyout_MULT, 'fundgrowtht1t ~ mult_t1 + log_size_t1 + seq_num_t1')

% output 
% regFOF_A_Buyout_MULT = 
% 
% 
% Linear regression model:
%     fundgrowtht1t ~ 1 + mult_t1 + seq_num_t1 + log_size_t1
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue  
%                    ________    ________    _______    __________
% 
%     (Intercept)      5.0714     0.53091     9.5522    1.9966e-20
%     mult_t1         0.23224     0.11782     1.9712      0.049091
%     seq_num_t1      0.41802    0.065054     6.4258    2.4011e-10
%     log_size_t1    -0.84967    0.077207    -11.005    4.0144e-26
% 
% 
% Number of observations: 716, Error degrees of freedom: 712
% Root Mean Squared Error: 2.58
% R-squared: 0.163,  Adjusted R-Squared 0.159
% F-statistic vs. constant model: 46.1, p-value = 2.99e-27



%DIAGNOSTICS of CLRM assumptions

T = regFOF_A_Buyout_MULT.NumObservations;
k = regFOF_A_Buyout_MULT.NumEstimatedCoefficients;
u = regFOF_A_Buyout_MULT.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_A_Buyout_MULT.Residuals.Raw);
Stdev_u = std(regFOF_A_Buyout_MULT.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 2.0155e-14

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9633

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_A_Buyout_MULT.uRaw = u; 
FOF_A_Buyout_MULT.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_A_Buyout_MULT_White = table(FOF_A_Buyout_MULT.fundgrowtht1t, FOF_A_Buyout_MULT.mult_t1, FOF_A_Buyout_MULT.seq_num_t1, FOF_A_Buyout_MULT.log_size_t1 ,...
    FOF_A_Buyout_MULT.mult_t1.^2, FOF_A_Buyout_MULT.log_size_t1.^2, FOF_A_Buyout_MULT.uRaw2);
 
FOF_A_Buyout_MULT_White.Properties.VariableNames={'fundgrowtht1t','mult_t1','seq_num_t1',...
    'log_size_t1','mult_t1sq','log_size_t1sq','uRaw2'};

% Unrestricted regression
regFOF_A_Buyout_MULT_White=fitlm(FOF_A_Buyout_MULT_White,'uRaw2 ~ mult_t1 + seq_num_t1 + log_size_t1 + mult_t1sq + log_size_t1sq')

%regFOF_A_Buyout_MULT_White = 
% Linear regression model:
%     uRaw2 ~ 1 + mult_t1 + seq_num_t1 + log_size_t1 + mult_t1sq + log_size_t1sq
% 
% Estimated Coefficients:
%                      Estimate      SE        tStat        pValue  
%                      ________    _______    ________    __________
% 
%     (Intercept)         72.58     23.625      3.0722     0.0022064
%     mult_t1            2.6071     5.3067     0.49129       0.62337
%     seq_num_t1         7.2016     1.1452      6.2884    5.6024e-10
%     log_size_t1       -21.635     7.6586      -2.825     0.0048615
%     mult_t1sq        -0.34026    0.95401    -0.35666       0.72145
%     log_size_t1sq      1.1605    0.64665      1.7946      0.073135
% 
% 
% Number of observations: 716, Error degrees of freedom: 710
% Root Mean Squared Error: 44.5
% R-squared: 0.0826,  Adjusted R-Squared 0.0761
% F-statistic vs. constant model: 12.8, p-value = 6.53e-12


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_A_Buyout_MULT_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  59.1103, Crit_Val_e = 7.8147, P_e =  9.1060e-13,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_A_Buyout_MULT.mult_t1 FOF_A_Buyout_MULT.seq_num_t1 FOF_A_Buyout_MULT.log_size_t1];

y = FOF_A_Buyout_MULT.fundgrowtht1t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','seq_num_t1','log_size_t1'};
White


% %
% White =
% 
%   4×4 table
% 
%                      beta        SE        tstat         p     
%                    ________    _______    _______    __________
% 
%     (Intercept)      5.0714    0.79703     6.3628    3.5385e-10
%     mult_t1         0.23224    0.13555     1.7133      0.087094
%     seq_num_t1      0.41802    0.19244     2.1722       0.03017
%     log_size_t1    -0.84967    0.17352    -4.8967    1.2063e-06




%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(FOF_A_Buyout_MULT.mult_t1,u)
corr_fseq_num_t1_u = corr(FOF_A_Buyout_MULT.seq_num_t1,u)
corr_log_size_t1_u = corr(FOF_A_Buyout_MULT.log_size_t1,u)

%output:
% corr_mult_t1_u = -1.9975e-16
% corr_fseq_num_t1_u = 1.0249e-16
% corr_log_size_t1_u = 3.3040e-16

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest =    5.2530
% kurt_jbtest = 50.1080
% test_stat_jbtest = 6.9498e+04
% crit_val_jbtest = 9.3484
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_A_Buyout_MULT.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_A_Buyout_MULT(regFOF_A_Buyout_MULT.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 7 funds that drastically increased the fund size (14-40
% times). 
%Exclude these observations:

DrasticallyIncFund = FOF_A_Buyout_MULT(FOF_A_Buyout_MULT.fundgrowtht1t > 14,:)

FOF_A_Buyout_MULT_excluding_outliers = FOF_A_Buyout_MULT;
FOF_A_Buyout_MULT_excluding_outliers(FOF_A_Buyout_MULT_excluding_outliers.fundgrowtht1t > 14,:)=[];

regFOF_A_Buyout_MULT_excluding_outliers = fitlm(FOF_A_Buyout_MULT_excluding_outliers, 'fundgrowtht1t ~ mult_t1 + log_size_t1 + seq_num_t1')

% Linear regression model:
%     fundgrowtht1t ~ 1 + mult_t1 + seq_num_t1 + log_size_t1
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue  
%                    ________    ________    _______    __________
% 
%     (Intercept)      3.9544     0.35105     11.264    3.5181e-27
%     mult_t1         0.11477    0.077932     1.4727       0.14128
%     seq_num_t1      0.13285     0.04395     3.0228     0.0025954
%     log_size_t1    -0.54843    0.051795    -10.588    2.0514e-24
% 
% 
% Number of observations: 709, Error degrees of freedom: 705
% Root Mean Squared Error: 1.69
% R-squared: 0.148,  Adjusted R-Squared 0.144
% F-statistic vs. constant model: 40.7, p-value = 2.78e-24


% Now the mult_t1 coefficient is not significant 
% 
% 



u_exc = regFOF_A_Buyout_MULT_excluding_outliers.Residuals.Raw;
T_exc = regFOF_A_Buyout_MULT_excluding_outliers.NumObservations;
k_exc = regFOF_A_Buyout_MULT_excluding_outliers.NumEstimatedCoefficients;
m_exc = k_exc - 1;

alpha = 0.05;
 
figure(3)
histogram(u_exc)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(4)
plot(u_exc, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

skew_jbtest_exc = mean(u_exc.^3) / (std(u_exc)^2)^(3/2)
kurt_jbtest_exc = mean(u_exc.^4) / (std(u_exc)^2)^2
test_stat_jbtest_exc = T_exc * ( skew_jbtest_exc^2 / 6 + (kurt_jbtest_exc - 3)^2 / 24) 
crit_val_jbtest_exc = chi2inv(1-1/2*alpha, m_exc)
p_jbtest_exc = 1 - cdf('Chisquare', test_stat_jbtest_exc, m_exc)
reject_jbstest_exc = test_stat_jbtest_exc > crit_val_jbtest_exc

%Reject = 1. The residuals are still not normally distributed, buth who
%cares

% % Use heteroscedasticity-consistent standard errors for new subsample
% (I think we shoudnt do it)

x_exc = [FOF_A_Buyout_MULT_excluding_outliers.mult_t1 FOF_A_Buyout_MULT_excluding_outliers.seq_num_t1 FOF_A_Buyout_MULT_excluding_outliers.log_size_t1];

y_exc = FOF_A_Buyout_MULT_excluding_outliers.fundgrowtht1t;

% White's estimation correction 

White_exc = table();
const_exc = 1; % 1: constant is included 
lags_exc = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y_exc, x_exc);

White_exc.beta = beta;
White_exc.SE = sqrt(diag(vcvm));
White_exc.tstat = tstat;
White_exc.p = 2*(1-tcdf(abs(tstat),T-2));
White_exc.Properties.RowNames = {'(Intercept)','mult_t1','seq_num_t1','log_size_t1'};
White_exc


% White_exc =
% 
%   4×4 table
% 
%                      beta         SE        tstat         p     
%                    ________    ________    _______    __________
% 
%     (Intercept)      3.9544     0.44592     8.8678             0
%     mult_t1         0.11477     0.07936     1.4462       0.14856
%     seq_num_t1      0.13285    0.051919     2.5588      0.010707
%     log_size_t1    -0.54843    0.066742    -8.2172    8.8818e-16
 
% FOF A BUYOUT IRR


% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (A) buyout_t_t-1 IRR','A2:Q631');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17]);

% Replace non-numeric cells with 0.0
R = cellfun(@(x) (~isnumeric(x) && ~islogical(x)) || isnan(x),raw); % Find non-numeric cells
raw(R) = {0.0}; % Replace non-numeric cells

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_A_Buyout_IRR = table;

% Allocate imported array to column variable names
FOF_A_Buyout_IRR.number = data(:,1);
FOF_A_Buyout_IRR.FUNDID = data(:,2);
FOF_A_Buyout_IRR.FIRMID = data(:,3);
FOF_A_Buyout_IRR.teamID = categorical(stringVectors(:,1));
FOF_A_Buyout_IRR.teamIDvintage = stringVectors(:,2);
FOF_A_Buyout_IRR.teamIDvintagenumber = stringVectors(:,3);
FOF_A_Buyout_IRR.vint_t = data(:,4);
FOF_A_Buyout_IRR.vint_t1 = data(:,5);
FOF_A_Buyout_IRR.irr_t = data(:,6);
FOF_A_Buyout_IRR.irr_t1 = data(:,7);
FOF_A_Buyout_IRR.STATUS = categorical(stringVectors(:,4));
FOF_A_Buyout_IRR.FUNDMANAGER = categorical(stringVectors(:,5));
FOF_A_Buyout_IRR.size_t = data(:,8);
FOF_A_Buyout_IRR.size_t1 = data(:,9);
FOF_A_Buyout_IRR.seq_num_t = data(:,10);
FOF_A_Buyout_IRR.seq_num_t1 = data(:,11);
FOF_A_Buyout_IRR.fundgrowtht1t = data(:,12);

% Clear temporary variables
clearvars data raw stringVectors R;
% log(size).
FOF_A_Buyout_IRR.log_size_t1 = log(FOF_A_Buyout_IRR.size_t1);

% Linear regression reg_A_Buyout_IRR

regFOF_A_Buyout_IRR = fitlm(FOF_A_Buyout_IRR, 'fundgrowtht1t ~ irr_t1 + log_size_t1 + seq_num_t1')

% output
% 
% Linear regression model:
%     fundgrowtht1t ~ 1 + irr_t1 + seq_num_t1 + log_size_t1
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat       pValue  
%                    _________    _________    _______    __________
% 
%     (Intercept)       5.3192      0.51033     10.423     1.448e-23
%     irr_t1         0.0060981    0.0055489      1.099        0.2722
%     seq_num_t1       0.50583     0.079183     6.3881    3.2784e-10
%     log_size_t1     -0.86557     0.085931    -10.073    3.2236e-22
% 
% 
% Number of observations: 630, Error degrees of freedom: 626
% Root Mean Squared Error: 2.6
% R-squared: 0.154,  Adjusted R-Squared 0.15
% F-statistic vs. constant model: 38.1, p-value = 1.22e-22


% p-Values for irr_t1 =   0.2722 , so insignificant.


%DIAGNOSTICS of CLRM assumptions

T = regFOF_A_Buyout_IRR.NumObservations;
k = regFOF_A_Buyout_IRR.NumEstimatedCoefficients;
u = regFOF_A_Buyout_IRR.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_A_Buyout_IRR.Residuals.Raw);
Stdev_u = std(regFOF_A_Buyout_IRR.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =  -5.7219e-15



% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9638

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_A_Buyout_IRR.uRaw = u; 
FOF_A_Buyout_IRR.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_A_Buyout_IRR_White = table(FOF_A_Buyout_IRR.fundgrowtht1t, FOF_A_Buyout_IRR.irr_t1, FOF_A_Buyout_IRR.seq_num_t1, FOF_A_Buyout_IRR.log_size_t1 ,...
    FOF_A_Buyout_IRR.irr_t1.^2, FOF_A_Buyout_IRR.log_size_t1.^2, FOF_A_Buyout_IRR.uRaw2);
 
FOF_A_Buyout_IRR_White.Properties.VariableNames={'fundgrowtht1t','irr_t1','seq_num_t1',...
    'log_size_t1','irr_t1sq','log_size_t1sq','uRaw2'};

% Unrestricted regression
regFOF_A_Buyout_IRR_White=fitlm(FOF_A_Buyout_IRR_White,'uRaw2 ~ irr_t1 + seq_num_t1 + log_size_t1 + irr_t1sq + log_size_t1sq')

% Linear regression model:
%     uRaw2 ~ 1 + irr_t1 + seq_num_t1 + log_size_t1 + irr_t1sq + log_size_t1sq
% 
% Estimated Coefficients:
%                       Estimate         SE         tStat        pValue  
%                      ___________    _________    ________    __________
% 
%     (Intercept)           86.727        27.28      3.1792       0.00155
%     irr_t1              0.024965      0.14934     0.16717       0.86729
%     seq_num_t1            9.2317       1.4165      6.5175    1.4752e-10
%     log_size_t1          -25.749       9.1666      -2.809     0.0051253
%     irr_t1sq         -0.00027873    0.0010817    -0.25768       0.79674
%     log_size_t1sq         1.4274      0.77688      1.8373      0.066639
% 
% 
% Number of observations: 630, Error degrees of freedom: 624
% Root Mean Squared Error: 45.5
% R-squared: 0.0961,  Adjusted R-Squared 0.0888
% F-statistic vs. constant model: 13.3, p-value = 2.68e-12


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_A_Buyout_IRR_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =     60.5244, Crit_Val_e = 7.8147, P_e = 4.5419e-13
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_A_Buyout_IRR.irr_t1 FOF_A_Buyout_IRR.seq_num_t1 FOF_A_Buyout_IRR.log_size_t1];

y = FOF_A_Buyout_IRR.fundgrowtht1t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','seq_num_t1','log_size_t1'};
White

% 
% White =
% 
%   4×4 table
% 
%                      beta          SE         tstat         p     
%                    _________    _________    _______    __________
% 
%     (Intercept)       5.3192      0.88819     5.9888    3.5585e-09
%     irr_t1         0.0060981    0.0067497    0.90346       0.36663
%     seq_num_t1       0.50583      0.24655     2.0516      0.040623
%     log_size_t1     -0.86557       0.2079    -4.1634    3.5733e-05
%
% So koefficient still insignificant ..


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(FOF_A_Buyout_IRR.irr_t1,u)
corr_seq_num_t1_u = corr(FOF_A_Buyout_IRR.seq_num_t1,u)
corr_log_size_t1_u = corr(FOF_A_Buyout_IRR.log_size_t1,u)

%output:
% corr_irr_t1_u =  6.5526e-16
% corr_seq_num_t1_u =  -1.5435e-16
% corr_log_size_t1_u = -2.8090e-16

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 5.4408
% kurt_jbtest =  51.3005
% test_stat_jbtest = 6.4348e+04
% crit_val_jbtest =  9.3484
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_A_Buyout_IRR.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_A_Buyout_IRR(regFOF_A_Buyout_IRR.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 6 funds that   drastically increased the fund
% size (14-40 times).
%Exclude these observations:

DrasticallyIncFund = FOF_A_Buyout_IRR(FOF_A_Buyout_IRR.fundgrowtht1t > 14,:)

FOF_A_Buyout_IRR_excluding_outliers = FOF_A_Buyout_IRR;
FOF_A_Buyout_IRR_excluding_outliers(FOF_A_Buyout_IRR_excluding_outliers.fundgrowtht1t > 14,:)=[];

regFOF_A_Buyout_IRR_excluding_outliers = fitlm(FOF_A_Buyout_IRR_excluding_outliers, 'fundgrowtht1t ~ irr_t1 + log_size_t1 + seq_num_t1')

% regFOF_A_Buyout_IRR_excluding_outliers = 
% 
% 
% Linear regression model:
%     fundgrowtht1t ~ 1 + irr_t1 + seq_num_t1 + log_size_t1
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat       pValue  
%                    _________    _________    _______    __________
% 
%     (Intercept)       3.7135      0.32779     11.329    3.5914e-27
%     irr_t1         0.0043357    0.0035125     1.2344       0.21753
%     seq_num_t1       0.12263     0.051976     2.3594      0.018612
%     log_size_t1      -0.4836     0.055884    -8.6538    4.2815e-17
% 
% 
% Number of observations: 624, Error degrees of freedom: 620
% Root Mean Squared Error: 1.64
% R-squared: 0.118,  Adjusted R-Squared 0.114
% F-statistic vs. constant model: 27.7, p-value = 8.04e-17

u_exc = regFOF_A_Buyout_IRR_excluding_outliers.Residuals.Raw;
T_exc = regFOF_A_Buyout_IRR_excluding_outliers.NumObservations;
k_exc = regFOF_A_Buyout_IRR_excluding_outliers.NumEstimatedCoefficients;
m_exc = k_exc - 1;

alpha = 0.05;
 
figure(3)
histogram(u_exc)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(4)
plot(u_exc, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


skew_jbtest_exc = mean(u_exc.^3) / (std(u_exc)^2)^(3/2)
kurt_jbtest_exc = mean(u_exc.^4) / (std(u_exc)^2)^2
test_stat_jbtest_exc = T_exc * ( skew_jbtest_exc^2 / 6 + (kurt_jbtest_exc - 3)^2 / 24) 
crit_val_jbtest_exc = chi2inv(1-1/2*alpha, m_exc)
p_jbtest_exc = 1 - cdf('Chisquare', test_stat_jbtest_exc, m_exc)
reject_jbstest_exc = test_stat_jbtest_exc > crit_val_jbtest_exc

%Reject = 1. The residuals are still not normally distributed, buth who
%cares

% % Use heteroscedasticity-consistent standard errors for new subsample

x_exc = [FOF_A_Buyout_IRR_excluding_outliers.irr_t1 FOF_A_Buyout_IRR_excluding_outliers.seq_num_t1 FOF_A_Buyout_IRR_excluding_outliers.log_size_t1];

y_exc = FOF_A_Buyout_IRR_excluding_outliers.fundgrowtht1t;

% White's estimation correction 

White_exc = table();
const_exc = 1; % 1: constant is included 
lags_exc = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y_exc, x_exc);

White_exc.beta = beta;
White_exc.SE = sqrt(diag(vcvm));
White_exc.tstat = tstat;
White_exc.p = 2*(1-tcdf(abs(tstat),T-2));
White_exc.Properties.RowNames = {'(Intercept)','irr_t1','seq_num_t1','log_size_t1'};
White_exc

% 
% 
% White_exc =
% 
%   4×4 table
% 
%                      beta          SE         tstat        p    
%                    _________    _________    _______    ________
% 
%     (Intercept)       3.7135      0.35912     10.341           0
%     irr_t1         0.0043357    0.0034434     1.2591     0.20845
%     seq_num_t1       0.12263     0.052471     2.3372    0.019742
%     log_size_t1      -0.4836     0.056251    -8.5973           0

% FOF A ALL MULT
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (A) all_t_t-1 MULT','A2:W1329');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[7,8,9,12,16,17]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,4,5,6,10,11,13,14,15,18,19,20,21,22,23]);

% Replace non-numeric cells with 0.0
R = cellfun(@(x) (~isnumeric(x) && ~islogical(x)) || isnan(x),raw); % Find non-numeric cells
raw(R) = {0.0}; % Replace non-numeric cells

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_A_All_MULT = table;

% Allocate imported array to column variable names
FOF_A_All_MULT.number = data(:,1);
FOF_A_All_MULT.FUNDID = data(:,2);
FOF_A_All_MULT.FIRMID = data(:,3);
FOF_A_All_MULT.teamID = categorical(stringVectors(:,1));
FOF_A_All_MULT.teamIDvintage = stringVectors(:,2);
FOF_A_All_MULT.teamIDvintagenumber = stringVectors(:,3);
FOF_A_All_MULT.vint_t = data(:,4);
FOF_A_All_MULT.vint_t1 = data(:,5);
FOF_A_All_MULT.STRATEGY = categorical(stringVectors(:,4));
FOF_A_All_MULT.BuyoutDummy = data(:,6);
FOF_A_All_MULT.mult_t = data(:,7);
FOF_A_All_MULT.mult_t1 = data(:,8);
FOF_A_All_MULT.STATUS = categorical(stringVectors(:,5));
FOF_A_All_MULT.FUNDMANAGER = categorical(stringVectors(:,6));
FOF_A_All_MULT.size_t = data(:,9);
FOF_A_All_MULT.size_t1 = data(:,10);
FOF_A_All_MULT.seq_num_t = data(:,11);
FOF_A_All_MULT.seq_num_t1 = data(:,12);
FOF_A_All_MULT.fundgrowtht1t = data(:,13);
FOF_A_All_MULT.PerformanceBuyout = data(:,14);

% Clear temporary variables
clearvars data raw stringVectors R;

% Log(size)

FOF_A_All_MULT.log_size_t1 = log(FOF_A_All_MULT.size_t1);


% Linear regression regFOF_A_All_MULT

regFOF_A_All_MULT = fitlm(FOF_A_All_MULT, 'fundgrowtht1t ~ mult_t1 + PerformanceBuyout + BuyoutDummy + log_size_t1 + seq_num_t1')

% 
% regFOF_A_All_MULT = 
% 
% 
% Linear regression model:
%     fundgrowtht1t ~ 1 + BuyoutDummy + mult_t1 + seq_num_t1 + PerformanceBuyout + log_size_t1
% 
% Estimated Coefficients:
%                          Estimate       SE        tStat       pValue  
%                          ________    ________    _______    __________
% 
%     (Intercept)            4.4874     0.28597     15.692    5.1046e-51
%     BuyoutDummy            1.1487     0.28983     3.9636    7.7791e-05
%     mult_t1              0.038303    0.033184     1.1543        0.2486
%     seq_num_t1            0.19719    0.038479     5.1246    3.4242e-07
%     PerformanceBuyout      0.1194     0.11992    0.99567       0.31959
%     log_size_t1          -0.83925    0.055831    -15.032    2.8808e-47
% 
% 
% Number of observations: 1328, Error degrees of freedom: 1322
% Root Mean Squared Error: 2.56
% R-squared: 0.155,  Adjusted R-Squared 0.152
% F-statistic vs. constant model: 48.7, p-value = 2.53e-46
% 



%DIAGNOSTICS of CLRM assumptions

T = regFOF_A_All_MULT.NumObservations;
k = regFOF_A_All_MULT.NumEstimatedCoefficients;
u = regFOF_A_All_MULT.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_A_All_MULT.Residuals.Raw);
Stdev_u = std(regFOF_A_All_MULT.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =  -1.5166e-14

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =    1.9618

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_A_All_MULT.uRaw = u; 
FOF_A_All_MULT.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_A_All_MULT_White = table(FOF_A_All_MULT.fundgrowtht1t, FOF_A_All_MULT.mult_t1, FOF_A_All_MULT.seq_num_t1, FOF_A_All_MULT.log_size_t1 ,...
    FOF_A_All_MULT.PerformanceBuyout, FOF_A_All_MULT.BuyoutDummy, FOF_A_All_MULT.mult_t1.^2, FOF_A_All_MULT.log_size_t1.^2,FOF_A_All_MULT.PerformanceBuyout.^2, FOF_A_All_MULT.uRaw2);
 
FOF_A_All_MULT_White.Properties.VariableNames={'fundgrowtht1t','mult_t1','seq_num_t1','log_size_t1',...
    'PerformanceBuyout', 'BuyoutDummy','mult_t1sq','log_size_t1sq', 'PerformanceBuyoutsq', 'uRaw2'};

% Unrestricted regression
regFOF_A_All_MULT_White = fitlm(FOF_A_All_MULT_White,'uRaw2 ~ mult_t1 + seq_num_t1 + log_size_t1 + PerformanceBuyout + BuyoutDummy + mult_t1sq + log_size_t1sq + PerformanceBuyoutsq')


% Linear regression model:
%     uRaw2 ~ [Linear formula with 9 terms in 8 predictors]

% Estimated Coefficients:
%                            Estimate       SE        tStat        pValue  
%                            ________    ________    ________    __________
% 
%     (Intercept)               82.89      12.066      6.8698    9.8796e-12
%     mult_t1                 -1.0623      1.2198    -0.87088       0.38398
%     seq_num_t1               3.8103     0.69723      5.4648    5.5339e-08
%     log_size_t1             -27.009      4.4192     -6.1118     1.295e-09
%     PerformanceBuyout        1.9123      5.5714     0.34323       0.73148
%     BuyoutDummy              7.6311      7.5804      1.0067       0.31427
%     mult_t1sq              0.034294    0.041347     0.82943       0.40701
%     log_size_t1sq            1.8089     0.41095      4.4019    1.1596e-05
%     PerformanceBuyoutsq    -0.17375     0.97799    -0.17766       0.85902
% 
% 
% Number of observations: 1328, Error degrees of freedom: 1319
% Root Mean Squared Error: 45.9
% R-squared: 0.0716,  Adjusted R-Squared 0.0659
% F-statistic vs. constant model: 12.7, p-value = 1.01e-17



% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_A_All_MULT_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  95.0339, Crit_Val_e = 11.0705, P_e =  0
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_A_All_MULT.mult_t1 FOF_A_All_MULT.seq_num_t1 FOF_A_All_MULT.log_size_t1 FOF_A_All_MULT.BuyoutDummy  FOF_A_All_MULT.PerformanceBuyout];

y = FOF_A_All_MULT.fundgrowtht1t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','seq_num_t1','log_size_t1', 'BuyoutDummy','PerformanceBuyout'};
White


% 
% White =
% 
%   6×4 table
% 
%                            beta         SE        tstat         p     
%                          ________    ________    _______    __________
% 
%     (Intercept)            4.4874     0.50048     8.9662             0
%     mult_t1              0.038303    0.025886     1.4797        0.1392
%     seq_num_t1            0.19719     0.11535     1.7094      0.087607
%     log_size_t1          -0.83925     0.11978    -7.0068    3.8671e-12
%     BuyoutDummy            1.1487     0.34699     3.3106    0.00095609
%     PerformanceBuyout      0.1194     0.14147    0.84397       0.39884
% 



%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(FOF_A_All_MULT.mult_t1,u)
corr_fseq_num_t1_u = corr(FOF_A_All_MULT.seq_num_t1,u)
corr_log_size_t1_u = corr(FOF_A_All_MULT.log_size_t1,u)
corr_buyout_dummy_u = corr(FOF_A_All_MULT.BuyoutDummy,u)
corr_performance_buyout_u = corr(FOF_A_All_MULT.PerformanceBuyout,u)

%output:
% corr_mult_t1_u = -1.1311e-16
% corr_fseq_num_t1_u = -2.4343e-16
% corr_log_size_t1_u =  -8.0949e-17
% corr_buyout_dummy_u = -1.5630e-16
% corr_performance_buyout_u = -1.6426e-16
%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest =    5.4716
% kurt_jbtest = 53.5101
% test_stat_jbtest = 1.4780e+05
% crit_val_jbtest = 12.8325
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_A_All_MULT.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_A_All_MULT(regFOF_A_All_MULT.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 5 funds that drastically increased the fund size (20-40
% times). 
%Exclude these observations:

DrasticallyIncFund = FOF_A_All_MULT(FOF_A_All_MULT.fundgrowtht1t > 18.9,:)

FOF_A_All_MULT_excluding_outliers = FOF_A_All_MULT;
FOF_A_All_MULT_excluding_outliers(FOF_A_All_MULT_excluding_outliers.fundgrowtht1t > 18.9,:)=[];

regFOF_A_All_MULT_excluding_outliers = fitlm(FOF_A_All_MULT_excluding_outliers, 'fundgrowtht1t ~ mult_t1 + log_size_t1 + seq_num_t1 + BuyoutDummy + PerformanceBuyout')


% Linear regression model:
%     fundgrowtht1t ~ 1 + BuyoutDummy + mult_t1 + seq_num_t1 + PerformanceBuyout + log_size_t1
% 
% Estimated Coefficients:
%                          Estimate       SE        tStat       pValue  
%                          ________    ________    _______    __________
% 
%     (Intercept)            3.8606     0.22638     17.054    4.4667e-59
%     BuyoutDummy           0.85502     0.22847     3.7425    0.00019008
%     mult_t1              0.049034    0.026085     1.8797      0.060364
%     seq_num_t1           0.096623    0.030483     3.1697     0.0015611
%     PerformanceBuyout     0.14057    0.094474      1.488         0.137
%     log_size_t1          -0.67565    0.044319    -15.245    1.8816e-48
% 
% 
% Number of observations: 1323, Error degrees of freedom: 1317
% Root Mean Squared Error: 2.02
% R-squared: 0.166,  Adjusted R-Squared 0.163
% F-statistic vs. constant model: 52.5, p-value = 8.42e-50
% 
% 



u_exc = regFOF_A_All_MULT_excluding_outliers.Residuals.Raw;
T_exc = regFOF_A_All_MULT_excluding_outliers.NumObservations;
k_exc = regFOF_A_All_MULT_excluding_outliers.NumEstimatedCoefficients;
m_exc = k_exc - 1;

alpha = 0.05;
 
figure(3)
histogram(u_exc)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(4)
plot(u_exc, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

skew_jbtest_exc = mean(u_exc.^3) / (std(u_exc)^2)^(3/2)
kurt_jbtest_exc = mean(u_exc.^4) / (std(u_exc)^2)^2
test_stat_jbtest_exc = T_exc * ( skew_jbtest_exc^2 / 6 + (kurt_jbtest_exc - 3)^2 / 24) 
crit_val_jbtest_exc = chi2inv(1-1/2*alpha, m_exc)
p_jbtest_exc = 1 - cdf('Chisquare', test_stat_jbtest_exc, m_exc)
reject_jbstest_exc = test_stat_jbtest_exc > crit_val_jbtest_exc

%Reject = 1. The residuals are still not normally distributed, buth who
%cares

% % Use heteroscedasticity-consistent standard errors for new subsample

x_exc = [FOF_A_All_MULT_excluding_outliers.mult_t1 FOF_A_All_MULT_excluding_outliers.seq_num_t1 FOF_A_All_MULT_excluding_outliers.log_size_t1 FOF_A_All_MULT_excluding_outliers.BuyoutDummy FOF_A_All_MULT_excluding_outliers.PerformanceBuyout];

y_exc = FOF_A_All_MULT_excluding_outliers.fundgrowtht1t;

% White's estimation correction 

White_exc = table();
const_exc = 1; % 1: constant is included 
lags_exc = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y_exc, x_exc);

White_exc.beta = beta;
White_exc.SE = sqrt(diag(vcvm));
White_exc.tstat = tstat;
White_exc.p = 2*(1-tcdf(abs(tstat),T-2));
White_exc.Properties.RowNames = {'(Intercept)','mult_t1','seq_num_t1','log_size_t1', 'BuyoutDummy', 'PerformanceBuyout'};
White_exc


% White_exc =
% 
%   6×4 table
% 
%                            beta         SE       tstat         p     
%                          ________    ________    ______    __________
% 
%     (Intercept)            3.8606     0.37755    10.225             0
%     mult_t1              0.049034    0.025536    1.9202      0.055053
%     seq_num_t1           0.096623    0.063259    1.5274        0.1269
%     log_size_t1          -0.67565    0.076396    -8.844             0
%     BuyoutDummy           0.85502     0.22149    3.8603    0.00011868
%     PerformanceBuyout     0.14057     0.11549    1.2172       0.22374
 
% FOF A ALL IRR

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (A) all_t_t-1 IRR','A2:T1220');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[4,5,6,9,13,14]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,10,11,12,15,16,17,18,19,20]);

% Replace non-numeric cells with 0.0
R = cellfun(@(x) (~isnumeric(x) && ~islogical(x)) || isnan(x),raw); % Find non-numeric cells
raw(R) = {0.0}; % Replace non-numeric cells

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_A_All_IRR = table;

% Allocate imported array to column variable names
FOF_A_All_IRR.number = data(:,1);
FOF_A_All_IRR.FUNDID = data(:,2);
FOF_A_All_IRR.FIRMID = data(:,3);
FOF_A_All_IRR.teamID = categorical(stringVectors(:,1));
FOF_A_All_IRR.teamIDvintage = stringVectors(:,2);
FOF_A_All_IRR.teamIDvintagenumber = stringVectors(:,3);
FOF_A_All_IRR.vint_t = data(:,4);
FOF_A_All_IRR.vint_t1 = data(:,5);
FOF_A_All_IRR.STRATEGY = categorical(stringVectors(:,4));
FOF_A_All_IRR.BuyoutDummy = data(:,6);
FOF_A_All_IRR.irr_t = data(:,7);
FOF_A_All_IRR.irr_t1 = data(:,8);
FOF_A_All_IRR.STATUS = categorical(stringVectors(:,5));
FOF_A_All_IRR.FUNDMANAGER = categorical(stringVectors(:,6));
FOF_A_All_IRR.size_t = data(:,9);
FOF_A_All_IRR.size_t1 = data(:,10);
FOF_A_All_IRR.seq_num_t = data(:,11);
FOF_A_All_IRR.seq_num_t1 = data(:,12);
FOF_A_All_IRR.fundgrowtht1t = data(:,13);
FOF_A_All_IRR.PerformanceBuyout = data(:,14);

% Clear temporary variables
clearvars data raw stringVectors R;

% Log(size)

FOF_A_All_IRR.log_size_t1 = log(FOF_A_All_IRR.size_t1);


% Linear regression regFOF_A_All_IRR

regFOF_A_All_IRR = fitlm(FOF_A_All_IRR, 'fundgrowtht1t ~ irr_t1 + PerformanceBuyout + BuyoutDummy + log_size_t1 + seq_num_t1')

% regFOF_A_All_IRR = 


% Linear regression model:
%     fundgrowtht1t ~ 1 + BuyoutDummy + irr_t1 + seq_num_t1 + PerformanceBuyout + log_size_t1
% 
% Estimated Coefficients:
%                          Estimate        SE         tStat       pValue  
%                          _________    _________    _______    __________
% 
%     (Intercept)             4.6544       0.3004     15.494    1.5282e-49
%     BuyoutDummy             1.2626      0.20568     6.1387    1.1255e-09
%     irr_t1               0.0038638    0.0023935     1.6142       0.10673
%     seq_num_t1             0.23014     0.043214     5.3256     1.198e-07
%     PerformanceBuyout    0.0041558    0.0058778    0.70703       0.47968
%     log_size_t1           -0.87222     0.060225    -14.483    5.7926e-44
% 
% 
% Number of observations: 1219, Error degrees of freedom: 1213
% Root Mean Squared Error: 2.62
% R-squared: 0.158,  Adjusted R-Squared 0.155
% F-statistic vs. constant model: 45.5, p-value = 3.65e-43
% % 
% %


%DIAGNOSTICS of CLRM assumptions

T = regFOF_A_All_IRR.NumObservations;
k = regFOF_A_All_IRR.NumEstimatedCoefficients;
u = regFOF_A_All_IRR.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_A_All_IRR.Residuals.Raw);
Stdev_u = std(regFOF_A_All_IRR.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -4.1706e-14

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =     1.9619

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_A_All_IRR.uRaw = u; 
FOF_A_All_IRR.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_A_All_IRR_White = table(FOF_A_All_IRR.fundgrowtht1t, FOF_A_All_IRR.irr_t1, FOF_A_All_IRR.seq_num_t1, FOF_A_All_IRR.log_size_t1 ,...
    FOF_A_All_IRR.PerformanceBuyout, FOF_A_All_IRR.BuyoutDummy, FOF_A_All_IRR.irr_t1.^2, FOF_A_All_IRR.log_size_t1.^2,FOF_A_All_IRR.PerformanceBuyout.^2, FOF_A_All_IRR.uRaw2);
 
FOF_A_All_IRR_White.Properties.VariableNames={'fundgrowtht1t','irr_t1','seq_num_t1','log_size_t1',...
    'PerformanceBuyout', 'BuyoutDummy','irr_t1sq','log_size_t1sq', 'PerformanceBuyoutsq', 'uRaw2'};

% Unrestricted regression
regFOF_A_All_IRR_White = fitlm(FOF_A_All_IRR_White,'uRaw2 ~ irr_t1 + seq_num_t1 + log_size_t1 + PerformanceBuyout + BuyoutDummy + irr_t1sq + log_size_t1sq + PerformanceBuyoutsq')


% Linear regression model:
%     uRaw2 ~ [Linear formula with 9 terms in 8 predictors]
% 
% Estimated Coefficients:
%                             Estimate         SE          tStat        pValue  
%                            __________    __________    _________    __________
% 
%     (Intercept)                88.407        12.857       6.8761     9.834e-12
%     irr_t1                  -0.083909      0.076952      -1.0904       0.27576
%     seq_num_t1                 4.5989        0.7858       5.8525    6.2272e-09
%     log_size_t1               -29.296        4.7814      -6.1272    1.2076e-09
%     PerformanceBuyout        0.086194       0.16638      0.51806       0.60451
%     BuyoutDummy                9.9718        3.9656       2.5146      0.012046
%     irr_t1sq                0.0001953    0.00023118      0.84481       0.39838
%     log_size_t1sq              1.9519         0.443       4.4061    1.1458e-05
%     PerformanceBuyoutsq    -5.899e-05     0.0011238    -0.052491       0.95815
% 
% 
% Number of observations: 1219, Error degrees of freedom: 1210
% Root Mean Squared Error: 47
% R-squared: 0.0809,  Adjusted R-Squared 0.0748
% F-statistic vs. constant model: 13.3, p-value = 1.47e-18



% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_A_All_IRR_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  98.5568, Crit_Val_e = 11.0705, P_e =  0
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_A_All_IRR.irr_t1 FOF_A_All_IRR.seq_num_t1 FOF_A_All_IRR.log_size_t1 FOF_A_All_IRR.BuyoutDummy  FOF_A_All_IRR.PerformanceBuyout];

y = FOF_A_All_IRR.fundgrowtht1t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','seq_num_t1','log_size_t1', 'BuyoutDummy','PerformanceBuyout'};
White


%  
%   6×4 table
% 
%                            beta          SE         tstat         p     
%                          _________    _________    _______    __________
% 
%     (Intercept)             4.6544      0.52403     8.8819             0
%     irr_t1               0.0038638    0.0027639     1.3979       0.16239
%     seq_num_t1             0.23014      0.13575     1.6953      0.090281
%     log_size_t1           -0.87222      0.13235      -6.59    6.5329e-11
%     BuyoutDummy             1.2626      0.29091     4.3401    1.5422e-05
%     PerformanceBuyout    0.0041558    0.0082554    0.50341       0.61477
% 
% 



%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(FOF_A_All_IRR.irr_t1,u)
corr_fseq_num_t1_u = corr(FOF_A_All_IRR.seq_num_t1,u)
corr_log_size_t1_u = corr(FOF_A_All_IRR.log_size_t1,u)
corr_buyout_dummy_u = corr(FOF_A_All_IRR.BuyoutDummy,u)
corr_performance_buyout_u = corr(FOF_A_All_IRR.PerformanceBuyout,u)

%output:
% corr_mult_t1_u = -4.2299e-16
% corr_fseq_num_t1_u = -4.6139e-17
% corr_log_size_t1_u =  -7.3251e-17
% corr_buyout_dummy_u = 3.9270e-16
% corr_performance_buyout_u = 5.9616e-16
%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest =    5.4576
% kurt_jbtest = 52.2762
% test_stat_jbtest = 1.2938e+05
% crit_val_jbtest = 12.8325
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_A_All_IRR.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_A_All_IRR(regFOF_A_All_IRR.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 7 funds that drastically increased the fund size (14-40
% times). 
%Exclude these observations:

DrasticallyIncFund = FOF_A_All_IRR(FOF_A_All_IRR.fundgrowtht1t > 14,:)

FOF_A_All_IRR_excluding_outliers = FOF_A_All_IRR;
FOF_A_All_IRR_excluding_outliers(FOF_A_All_IRR_excluding_outliers.fundgrowtht1t > 14,:)=[];

regFOF_A_All_IRR_excluding_outliers = fitlm(FOF_A_All_IRR_excluding_outliers, 'fundgrowtht1t ~ irr_t1 + log_size_t1 + seq_num_t1 + BuyoutDummy + PerformanceBuyout')


% 
% regFOF_A_All_IRR_excluding_outliers = 


% Linear regression model:
%     fundgrowtht1t ~ 1 + BuyoutDummy + irr_t1 + seq_num_t1 + PerformanceBuyout + log_size_t1
% 
% Estimated Coefficients:
%                           Estimate        SE         tStat        pValue  
%                          __________    _________    ________    __________
% 
%     (Intercept)              3.0414      0.18851      16.134    3.8667e-53
%     BuyoutDummy             0.92052      0.12631      7.2878    5.7023e-13
%     irr_t1                 0.006271    0.0014628      4.2871    1.9556e-05
%     seq_num_t1             0.027905     0.026848      1.0394       0.29883
%     PerformanceBuyout    -0.0019987    0.0036352    -0.54982       0.58254
%     log_size_t1            -0.49073     0.037904     -12.947    5.6125e-36
% 
% 
% Number of observations: 1204, Error degrees of freedom: 1198
% Root Mean Squared Error: 1.59
% R-squared: 0.158,  Adjusted R-Squared 0.155
% F-statistic vs. constant model: 45.1, p-value = 1.05e-42
% 
% 



u_exc = regFOF_A_All_IRR_excluding_outliers.Residuals.Raw;
T_exc = regFOF_A_All_IRR_excluding_outliers.NumObservations;
k_exc = regFOF_A_All_IRR_excluding_outliers.NumEstimatedCoefficients;
m_exc = k_exc - 1;

alpha = 0.05;
 
figure(3)
histogram(u_exc)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(4)
plot(u_exc, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

skew_jbtest_exc = mean(u_exc.^3) / (std(u_exc)^2)^(3/2)
kurt_jbtest_exc = mean(u_exc.^4) / (std(u_exc)^2)^2
test_stat_jbtest_exc = T_exc * ( skew_jbtest_exc^2 / 6 + (kurt_jbtest_exc - 3)^2 / 24) 
crit_val_jbtest_exc = chi2inv(1-1/2*alpha, m_exc)
p_jbtest_exc = 1 - cdf('Chisquare', test_stat_jbtest_exc, m_exc)
reject_jbstest_exc = test_stat_jbtest_exc > crit_val_jbtest_exc

%Reject = 1. The residuals are still not normally distributed, buth who
%cares

% % Use heteroscedasticity-consistent standard errors for new subsample

x_exc = [FOF_A_All_IRR_excluding_outliers.irr_t1 FOF_A_All_IRR_excluding_outliers.seq_num_t1 FOF_A_All_IRR_excluding_outliers.log_size_t1 FOF_A_All_IRR_excluding_outliers.BuyoutDummy FOF_A_All_IRR_excluding_outliers.PerformanceBuyout];

y_exc = FOF_A_All_IRR_excluding_outliers.fundgrowtht1t;

% White's estimation correction 

White_exc = table();
const_exc = 1; % 1: constant is included 
lags_exc = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y_exc, x_exc);

White_exc.beta = beta;
White_exc.SE = sqrt(diag(vcvm));
White_exc.tstat = tstat;
White_exc.p = 2*(1-tcdf(abs(tstat),T-2));
White_exc.Properties.RowNames = {'(Intercept)','irr_t1','seq_num_t1','log_size_t1', 'BuyoutDummy', 'PerformanceBuyout'};
White_exc


%White_exc =
% 
%   6×4 table
% 
%                             beta          SE         tstat          p     
%                          __________    _________    ________    __________
% 
%     (Intercept)              3.0414      0.23843      12.756             0
%     irr_t1                 0.006271    0.0028747      2.1814      0.029341
%     seq_num_t1             0.027905     0.036337     0.76795       0.44267
%     log_size_t1            -0.49073     0.044541     -11.018             0
%     BuyoutDummy             0.92052      0.13873      6.6354    4.8593e-11
%     PerformanceBuyout    -0.0019987    0.0043515    -0.45933       0.64608
% 

%% Section 5.3.2
% FOF B VENTURE MULTIPLE
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (B) venture_t_t-1 MULT ','A2:Q603');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_B_Venture_MULT = table;

% Allocate imported array to column variable names
FOF_B_Venture_MULT.number = data(:,1);
FOF_B_Venture_MULT.FUNDID = data(:,2);
FOF_B_Venture_MULT.FIRMID = data(:,3);
FOF_B_Venture_MULT.teamID = categorical(stringVectors(:,1));
FOF_B_Venture_MULT.teamIDvintage = stringVectors(:,2);
FOF_B_Venture_MULT.teamIDvintagenumber = stringVectors(:,3);
FOF_B_Venture_MULT.vint_t = data(:,4);
FOF_B_Venture_MULT.vint_t1 = data(:,5);
FOF_B_Venture_MULT.mult_t = data(:,6);
FOF_B_Venture_MULT.mult_t1 = data(:,7);
FOF_B_Venture_MULT.STATUS = categorical(stringVectors(:,4));
FOF_B_Venture_MULT.FUNDMANAGER = categorical(stringVectors(:,5));
FOF_B_Venture_MULT.size_t = data(:,8);
FOF_B_Venture_MULT.size_t1 = data(:,9);
FOF_B_Venture_MULT.seq_num_t = data(:,10);
FOF_B_Venture_MULT.seq_num_t1 = data(:,11);
FOF_B_Venture_MULT.fundgrowtht1t = data(:,12);

% Clear temporary variables
clearvars data raw stringVectors;


% Log (size)

FOF_B_Venture_MULT.log_size_t = log(FOF_B_Venture_MULT.size_t);


% Linear regression reg_B_Venture_MULT

regFOF_B_Venture_MULT = fitlm(FOF_B_Venture_MULT, 'mult_t ~ mult_t1 + log_size_t + seq_num_t + fundgrowtht1t')

% output 
% 
%  Linear regression model:
%     mult_t ~ 1 + mult_t1 + seq_num_t + fundgrowtht1t + log_size_t
% 
% Estimated Coefficients:
%                      Estimate        SE        tStat        pValue  
%                      _________    ________    ________    __________
% 
%     (Intercept)         1.8667     0.43111      4.3299    1.7501e-05
%     mult_t1            0.16927    0.031235      5.4194    8.6945e-08
%     seq_num_t        -0.057635    0.044377     -1.2988       0.19453
%     fundgrowtht1t    -0.073415    0.035738     -2.0543      0.040386
%     log_size_t       -0.042441    0.081591    -0.52017       0.60314
% 
% 
% Number of observations: 601, Error degrees of freedom: 596
% Root Mean Squared Error: 2.4
% R-squared: 0.0578,  Adjusted R-Squared 0.0514
% F-statistic vs. constant model: 9.13, p-value = 3.63e-07
% % 

% p-Values for fundgrowtht1t =  0.040386, so significant. Performance in t depends on fundgrowth 
% between t and t-1 (negative koefficient = -2.0543).


%DIAGNOSTICS of CLRM assumptions

T = regFOF_B_Venture_MULT.NumObservations;
k = regFOF_B_Venture_MULT.NumEstimatedCoefficients;
u = regFOF_B_Venture_MULT.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_B_Venture_MULT.Residuals.Raw);
Stdev_u = std(regFOF_B_Venture_MULT.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -1.1285e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9640

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_B_Venture_MULT.uRaw = u; 
FOF_B_Venture_MULT.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_B_Venture_MULT_White = table(FOF_B_Venture_MULT.mult_t, FOF_B_Venture_MULT.mult_t1, FOF_B_Venture_MULT.log_size_t,...
    FOF_B_Venture_MULT.seq_num_t, FOF_B_Venture_MULT.fundgrowtht1t, ...
    FOF_B_Venture_MULT.mult_t1.^2, FOF_B_Venture_MULT.log_size_t.^2, FOF_B_Venture_MULT.fundgrowtht1t.^2, FOF_B_Venture_MULT.uRaw2);
 
FOF_B_Venture_MULT_White.Properties.VariableNames={'mult_t','mult_t1','log_size_t','seq_num_t','fundgrowtht1t'...
    'mult_t1sq','log_size_tsq','fundgrowtht1tsq','uRaw2'};

% Unrestricted regression
regFOF_B_Venture_MULT_White=fitlm(FOF_B_Venture_MULT_White,'uRaw2 ~ mult_t1 + log_size_t + seq_num_t + fundgrowtht1t + mult_t1sq + log_size_tsq + fundgrowtht1tsq')

% regFOF_B_Venture_MULT_White = 
% 
% 
% 
% Linear regression model:
%     uRaw2 ~ 1 + mult_t1 + log_size_t + seq_num_t + fundgrowtht1t + mult_t1sq + log_size_tsq + fundgrowtht1tsq
% 
% Estimated Coefficients:
%                        Estimate       SE        tStat        pValue  
%                        ________    ________    ________    __________
% 
%     (Intercept)         -16.418      19.998    -0.82097         0.412
%     mult_t1              4.8842      1.2345      3.9563    8.5337e-05
%     log_size_t           8.1368      8.5753     0.94887       0.34308
%     seq_num_t          -0.70025     0.86191    -0.81244       0.41687
%     fundgrowtht1t       -1.8605      1.2921     -1.4399       0.15042
%     mult_t1sq          -0.10976    0.041543     -2.6421     0.0084581
%     log_size_tsq       -0.88507     0.91503    -0.96726       0.33381
%     fundgrowtht1tsq    0.063674    0.057957      1.0986       0.27237
% 
% 
% Number of observations: 601, Error degrees of freedom: 593
% Root Mean Squared Error: 45.8
% R-squared: 0.0367,  Adjusted R-Squared 0.0253
% F-statistic vs. constant model: 3.22, p-value = 0.00231

% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_B_Venture_MULT_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e = 22.0340, Crit_Val_e = 9.4877, P_e = 1.9733e-04,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_B_Venture_MULT.mult_t1 FOF_B_Venture_MULT.log_size_t FOF_B_Venture_MULT.seq_num_t FOF_B_Venture_MULT.fundgrowtht1t];

y = FOF_B_Venture_MULT.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','log_size_t','seq_num_t','fundgrowtht1t'};
White


% 
% White =
% 
%   5×4 table
% 
%                        beta          SE        tstat          p     
%                      _________    ________    ________    __________
% 
%     (Intercept)         1.8667     0.35678       5.232    2.3226e-07
%     mult_t1            0.16927    0.063058      2.6844     0.0074671
%     log_size_t       -0.042441    0.056658    -0.74907       0.45411
%     seq_num_t        -0.057635    0.029067     -1.9828      0.047843
%     fundgrowtht1t    -0.073415    0.022799     -3.2201     0.0013507
% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(FOF_B_Venture_MULT.mult_t1,u)
corr_log_size_t_u = corr(FOF_B_Venture_MULT.log_size_t,u)
corr_seq_num_t_u = corr(FOF_B_Venture_MULT.seq_num_t,u)
corr_fundgrowtht1t_u = corr(FOF_B_Venture_MULT.fundgrowtht1t,u)

%output:
% corr_mult_t1_u = 5.7104e-17
% corr_log_size_t_u = -3.9778e-18
% corr_seq_num_t_u = -5.0987e-17
% corr_fundgrowtht1t_u = 1.7528e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 6.7088
% kurt_jbtest = 66.1201
% test_stat_jbtest = 1.0428e+05
% crit_val_jbtest = 11.1433
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_B_Venture_MULT.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_B_Venture_MULT(regFOF_B_Venture_MULT.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 6 funds. No creteria to exclude them (very high
% multiples either in t or t-1)

% FOF B VENTURE IRR
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (B) venture_t_t-1 IRR','A2:Q603');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_B_Venture_IRR = table;

% Allocate imported array to column variable names
FOF_B_Venture_IRR.number = data(:,1);
FOF_B_Venture_IRR.FUNDID = data(:,2);
FOF_B_Venture_IRR.FIRMID = data(:,3);
FOF_B_Venture_IRR.teamID = categorical(stringVectors(:,1));
FOF_B_Venture_IRR.teamIDvintage = stringVectors(:,2);
FOF_B_Venture_IRR.teamIDvintagenumber = stringVectors(:,3);
FOF_B_Venture_IRR.vint_t = data(:,4);
FOF_B_Venture_IRR.vint_t1 = data(:,5);
FOF_B_Venture_IRR.irr_t = data(:,6);
FOF_B_Venture_IRR.irr_t1 = data(:,7);
FOF_B_Venture_IRR.STATUS = categorical(stringVectors(:,4));
FOF_B_Venture_IRR.FUNDMANAGER = categorical(stringVectors(:,5));
FOF_B_Venture_IRR.size_t = data(:,8);
FOF_B_Venture_IRR.size_t1 = data(:,9);
FOF_B_Venture_IRR.seq_num_t = data(:,10);
FOF_B_Venture_IRR.seq_num_t1 = data(:,11);
FOF_B_Venture_IRR.fundgrowtht1t = data(:,12);

% Clear temporary variables
clearvars data raw stringVectors;


% Log (size)

FOF_B_Venture_IRR.log_size_t = log(FOF_B_Venture_IRR.size_t);


% Linear regression reg_B_Venture_IRR

regFOF_B_Venture_IRR = fitlm(FOF_B_Venture_IRR, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + fundgrowtht1t')

% output 
% regFOF_B_Venture_IRR = 
% 
% 
% Linear regression model:
%     irr_t ~ 1 + irr_t1 + seq_num_t + fundgrowtht1t + log_size_t
% 
% Estimated Coefficients:
%                      Estimate       SE        tStat        pValue  
%                      ________    ________    ________    __________
% 
%     (Intercept)       20.399       7.5623      2.6974     0.0072283
%     irr_t1           0.16414     0.036775      4.4633    1.0019e-05
%     seq_num_t        -1.2159      0.88048     -1.3809       0.16793
%     fundgrowtht1t    -1.4391      0.69662     -2.0659      0.039365
%     log_size_t       -1.1097       1.4361    -0.77273       0.44006
% 
% 
% Number of observations: 496, Error degrees of freedom: 491
% Root Mean Squared Error: 38.3
% R-squared: 0.0521,  Adjusted R-Squared 0.0444
% F-statistic vs. constant model: 6.75, p-value = 2.69e-05
%  

% p-Values for fundgrowtht1t =  0.039365, so significant. Performance in t depends on fundgrowth 
% between t and t-1 (negative koefficient =  -2.0659).


%DIAGNOSTICS of CLRM assumptions

T = regFOF_B_Venture_IRR.NumObservations;
k = regFOF_B_Venture_IRR.NumEstimatedCoefficients;
u = regFOF_B_Venture_IRR.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_B_Venture_IRR.Residuals.Raw);
Stdev_u = std(regFOF_B_Venture_IRR.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -3.8859e-16

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9648

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_B_Venture_IRR.uRaw = u; 
FOF_B_Venture_IRR.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_B_Venture_IRR_White = table(FOF_B_Venture_IRR.irr_t, FOF_B_Venture_IRR.irr_t1, FOF_B_Venture_IRR.log_size_t,...
    FOF_B_Venture_IRR.seq_num_t, FOF_B_Venture_IRR.fundgrowtht1t, ...
    FOF_B_Venture_IRR.irr_t1.^2, FOF_B_Venture_IRR.log_size_t.^2, FOF_B_Venture_IRR.fundgrowtht1t.^2, FOF_B_Venture_IRR.uRaw2);
 
FOF_B_Venture_IRR_White.Properties.VariableNames={'irr_t','irr_t1','log_size_t','seq_num_t','fundgrowtht1t'...
    'irr_t1sq','log_size_tsq','fundgrowtht1tsq','uRaw2'};

% Unrestricted regression
regFOF_B_Venture_IRR_White=fitlm(FOF_B_Venture_IRR_White,'uRaw2 ~ irr_t1 + log_size_t + seq_num_t + fundgrowtht1t + irr_t1sq + log_size_tsq + fundgrowtht1tsq')

% regFOF_B_Venture_IRR_White = 
% 
% Linear regression model:
%     uRaw2 ~ 1 + irr_t1 + log_size_t + seq_num_t + fundgrowtht1t + irr_t1sq + log_size_tsq + fundgrowtht1tsq
% 
% Estimated Coefficients:
%                        Estimate        SE        tStat        pValue  
%                        _________    ________    ________    __________
% 
%     (Intercept)            485.5      5067.1    0.095813       0.92371
%     irr_t1                65.442      18.817      3.4778    0.00055082
%     log_size_t            708.25      2168.5     0.32661       0.74411
%     seq_num_t            -129.03      255.19    -0.50562       0.61335
%     fundgrowtht1t        -694.46      484.14     -1.4344       0.15209
%     irr_t1sq           -0.045447    0.054192    -0.83863       0.40209
%     log_size_tsq         -102.14       231.5    -0.44122       0.65925
%     fundgrowtht1tsq       39.358      33.333      1.1808       0.23828
% 
% 
% Number of observations: 496, Error degrees of freedom: 488
% Root Mean Squared Error: 1.07e+04
% R-squared: 0.0537,  Adjusted R-Squared 0.0401
% F-statistic vs. constant model: 3.96, p-value = 0.000322

% 
% 

% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_B_Venture_IRR_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e = 26.6442, Crit_Val_e = 9.4877, P_e = 2.3458e-05,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_B_Venture_IRR.irr_t1 FOF_B_Venture_IRR.log_size_t FOF_B_Venture_IRR.seq_num_t FOF_B_Venture_IRR.fundgrowtht1t];

y = FOF_B_Venture_IRR.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','log_size_t','seq_num_t','fundgrowtht1t'};
White


% 
% White =
% 
%   5×4 table
% 
%                       beta         SE        tstat          p    
%                      _______    ________    ________    _________
% 
%     (Intercept)       20.399      7.6957      2.6507    0.0082912
%     irr_t1           0.16414    0.077794      2.1099     0.035368
%     log_size_t       -1.1097      1.1738    -0.94542      0.34491
%     seq_num_t        -1.2159     0.67268     -1.8075     0.071291
%     fundgrowtht1t    -1.4391     0.51916      -2.772    0.0057812
% 
% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(FOF_B_Venture_IRR.irr_t1,u)
corr_log_size_t_u = corr(FOF_B_Venture_IRR.log_size_t,u)
corr_seq_num_t_u = corr(FOF_B_Venture_IRR.seq_num_t,u)
corr_fundgrowtht1t_u = corr(FOF_B_Venture_IRR.fundgrowtht1t,u)

%output:
% corr_irr_t1_u = -8.1368e-18
% corr_log_size_t_u = -5.4016e-17
% corr_seq_num_t_u = -4.8206e-17
% corr_fundgrowtht1t_u =  -2.4002e-18

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 5.6656
% kurt_jbtest = 57.0692
% test_stat_jbtest = 6.3072e+04
% crit_val_jbtest = 11.1433
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_B_Venture_IRR.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_B_Venture_IRR(regFOF_B_Venture_IRR.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 5 funds with very high IRR either in t or t-1 (>300%). No creteria to exclude them (very high
% multiples either in t or t-1)
%Try excuding them:

FOF_B_Venture_IRR_excluding_outliers = FOF_B_Venture_IRR;
FOF_B_Venture_IRR_excluding_outliers(FOF_B_Venture_IRR_excluding_outliers.irr_t > 200 | FOF_B_Venture_IRR_excluding_outliers.irr_t1 > 200,:)=[];

regFOF_B_Venture_IRR_excluding_outliers = fitlm(FOF_B_Venture_IRR_excluding_outliers, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + fundgrowtht1t')

% 
% Linear regression model:
%     irr_t ~ 1 + irr_t1 + seq_num_t + fundgrowtht1t + log_size_t
% 
% Estimated Coefficients:
%                      Estimate       SE        tStat        pValue  
%                      ________    ________    ________    __________
% 
%     (Intercept)        12.862      5.5109      2.3339       0.02001
%     irr_t1            0.21633    0.041272      5.2416    2.3838e-07
%     seq_num_t        -0.81343     0.63031     -1.2905       0.19749
%     fundgrowtht1t      -1.022     0.50131     -2.0386      0.042032
%     log_size_t       -0.37851       1.047    -0.36153       0.71786
% 
% 
% Number of observations: 488, Error degrees of freedom: 483
% Root Mean Squared Error: 27.3
% R-squared: 0.0677,  Adjusted R-Squared 0.06
% F-statistic vs. constant model: 8.77, p-value = 7.64e-07

 

u_exc = regFOF_B_Venture_IRR_excluding_outliers.Residuals.Raw;
T_exc = regFOF_B_Venture_IRR_excluding_outliers.NumObservations;
k_exc = regFOF_B_Venture_IRR_excluding_outliers.NumEstimatedCoefficients;
m_exc = k_exc - 1;

alpha = 0.05;
 
figure(3)
histogram(u_exc)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(4)
plot(u_exc, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

skew_jbtest_exc = mean(u_exc.^3) / (std(u_exc)^2)^(3/2)
kurt_jbtest_exc = mean(u_exc.^4) / (std(u_exc)^2)^2
test_stat_jbtest_exc = T_exc * ( skew_jbtest_exc^2 / 6 + (kurt_jbtest_exc - 3)^2 / 24) 
crit_val_jbtest_exc = chi2inv(1-1/2*alpha, m_exc)
p_jbtest_exc = 1 - cdf('Chisquare', test_stat_jbtest_exc, m_exc)
reject_jbstest_exc = test_stat_jbtest_exc > crit_val_jbtest_exc

%Reject = 1. The residuals are still not normally distributed, buth who
%cares

% FOF B BUYOUT MULT
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (B) buyout_t_t-1 MULT','A2:Q717');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_B_Buyout_MULT = table;

% Allocate imported array to column variable names
FOF_B_Buyout_MULT.number = data(:,1);
FOF_B_Buyout_MULT.FUNDID = data(:,2);
FOF_B_Buyout_MULT.FIRMID = data(:,3);
FOF_B_Buyout_MULT.teamID = stringVectors(:,1);
FOF_B_Buyout_MULT.teamIDvintage = stringVectors(:,2);
FOF_B_Buyout_MULT.teamIDvintagenumber = stringVectors(:,3);
FOF_B_Buyout_MULT.vint_t = data(:,4);
FOF_B_Buyout_MULT.vint_t1 = data(:,5);
FOF_B_Buyout_MULT.mult_t = data(:,6);
FOF_B_Buyout_MULT.mult_t1 = data(:,7);
FOF_B_Buyout_MULT.STATUS = categorical(stringVectors(:,4));
FOF_B_Buyout_MULT.FUNDMANAGER = stringVectors(:,5);
FOF_B_Buyout_MULT.size_t = data(:,8);
FOF_B_Buyout_MULT.size_t1 = data(:,9);
FOF_B_Buyout_MULT.seq_num_t = data(:,10);
FOF_B_Buyout_MULT.seq_num_t1 = data(:,11);
FOF_B_Buyout_MULT.fundgrowtht1t = data(:,12);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

FOF_B_Buyout_MULT.log_size_t = log(FOF_B_Buyout_MULT.size_t);


% Linear regression reg_B_Venture_MULT

regFOF_B_Buyput_MULT = fitlm(FOF_B_Buyout_MULT, 'mult_t ~ mult_t1 + log_size_t + seq_num_t + fundgrowtht1t')

% output 
% 

% regFOF_B_Buyput_MULT = 
% 
% 
% Linear regression model:
%     mult_t ~ 1 + mult_t1 + seq_num_t + fundgrowtht1t + log_size_t
% 
% Estimated Coefficients:
%                       Estimate         SE          tStat        pValue  
%                      ___________    _________    _________    __________
% 
%     (Intercept)           1.7369      0.16114       10.779    3.3891e-25
%     mult_t1              0.21155     0.033738       6.2703    6.2503e-10
%     seq_num_t          0.0097663     0.018414      0.53037       0.59602
%     fundgrowtht1t    -0.00075418    0.0099857    -0.075526       0.93982
%     log_size_t          -0.06071     0.022459      -2.7032     0.0070321
% 
% 
% Number of observations: 716, Error degrees of freedom: 711
% Root Mean Squared Error: 0.738
% R-squared: 0.0665,  Adjusted R-Squared 0.0613
% F-statistic vs. constant model: 12.7, p-value = 5.84e-10
% %  


% p-Values for fundgrowtht1t =  0.93982, so insignificant. 


%DIAGNOSTICS of CLRM assumptions

T = regFOF_B_Buyput_MULT.NumObservations;
k = regFOF_B_Buyput_MULT.NumEstimatedCoefficients;
u = regFOF_B_Buyput_MULT.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_B_Buyput_MULT.Residuals.Raw);
Stdev_u = std(regFOF_B_Buyput_MULT.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   7.3260e-15


% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9633

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_B_Buyout_MULT.uRaw = u; 
FOF_B_Buyout_MULT.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_B_Buyout_MULT_White = table(FOF_B_Buyout_MULT.mult_t, FOF_B_Buyout_MULT.mult_t1, FOF_B_Buyout_MULT.log_size_t,...
    FOF_B_Buyout_MULT.seq_num_t, FOF_B_Buyout_MULT.fundgrowtht1t, ...
    FOF_B_Buyout_MULT.mult_t1.^2, FOF_B_Buyout_MULT.log_size_t.^2, FOF_B_Buyout_MULT.fundgrowtht1t.^2, FOF_B_Buyout_MULT.uRaw2);
 
FOF_B_Buyout_MULT_White.Properties.VariableNames={'mult_t','mult_t1','log_size_t','seq_num_t','fundgrowtht1t'...
    'mult_t1sq','log_size_tsq','fundgrowtht1tsq','uRaw2'};

% Unrestricted regression
regFOF_B_Buyout_MULT_White=fitlm(FOF_B_Buyout_MULT_White,'uRaw2 ~ mult_t1 + log_size_t + seq_num_t + fundgrowtht1t + mult_t1sq + log_size_tsq + fundgrowtht1tsq')

% % output
% % Linear regression model:
%     uRaw2 ~ 1 + mult_t1 + log_size_t + seq_num_t + fundgrowtht1t + mult_t1sq + log_size_tsq + fundgrowtht1tsq
% 
% Estimated Coefficients:
%                         Estimate        SE         tStat        pValue  
%                        __________    _________    ________    __________
% 
%     (Intercept)            8.5978       1.3182      6.5225    1.3158e-10
%     mult_t1              -0.11926      0.22884    -0.52114       0.60243
%     log_size_t            -2.2151       0.3971     -5.5782    3.4607e-08
%     seq_num_t          -0.0087134     0.049668    -0.17543       0.86079
%     fundgrowtht1t         0.10969     0.049069      2.2354        0.0257
%     mult_t1sq            0.014007     0.040934     0.34218       0.73232
%     log_size_tsq          0.14572     0.030689      4.7483    2.4835e-06
%     fundgrowtht1tsq    -0.0031918    0.0020993     -1.5204       0.12885
% 
% 
% Number of observations: 716, Error degrees of freedom: 708
% Root Mean Squared Error: 1.91
% R-squared: 0.0817,  Adjusted R-Squared 0.0726
% F-statistic vs. constant model: 9, p-value = 1.16e-10
% 
% 
%

% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_B_Buyout_MULT_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e = 58.5104, Crit_Val_e = 9.4877, P_e = 5.9625e-12,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_B_Buyout_MULT.mult_t1 FOF_B_Buyout_MULT.log_size_t FOF_B_Buyout_MULT.seq_num_t FOF_B_Buyout_MULT.fundgrowtht1t];

y = FOF_B_Buyout_MULT.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','log_size_t','seq_num_t','fundgrowtht1t'};
White


% 
% White =
% 
%   5×4 table
% % 
%                         beta           SE         tstat          p     
%                      ___________    ________    _________    __________
% 
%     (Intercept)           1.7369     0.24018       7.2316    1.2319e-12
%     mult_t1              0.21155     0.04819       4.3899    1.3054e-05
%     log_size_t          -0.06071     0.02962      -2.0497      0.040761
%     seq_num_t          0.0097663    0.018806      0.51931        0.6037
%     fundgrowtht1t    -0.00075418    0.012845    -0.058712        0.9532
% % 
% % 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(FOF_B_Buyout_MULT.mult_t1,u)
corr_log_size_t_u = corr(FOF_B_Buyout_MULT.log_size_t,u)
corr_seq_num_t_u = corr(FOF_B_Buyout_MULT.seq_num_t,u)
corr_fundgrowtht1t_u = corr(FOF_B_Buyout_MULT.fundgrowtht1t,u)

%output:
% corr_mult_t1_u = 9.2145e-17
% corr_log_size_t_u = 1.1097e-16
% corr_seq_num_t_u = -1.3681e-17
% corr_fundgrowtht1t_u = 5.2555e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 1.8388
% kurt_jbtest = 14.2993
% test_stat_jbtest = 4.2124e+03
% crit_val_jbtest = 11.1433
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_B_Buyput_MULT.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_B_Buyout_MULT(regFOF_B_Buyput_MULT.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 4 funds. But not really outliers. No criteria to exclude
% them. 


 % FOF B BUYOUT IRR
 clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (B) buyout_t_t-1 IRR','A2:Q629');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_B_Buyout_IRR = table;

% Allocate imported array to column variable names
FOF_B_Buyout_IRR.number = data(:,1);
FOF_B_Buyout_IRR.FUNDID = data(:,2);
FOF_B_Buyout_IRR.FIRMID = data(:,3);
FOF_B_Buyout_IRR.teamID = categorical(stringVectors(:,1));
FOF_B_Buyout_IRR.teamIDvintage = stringVectors(:,2);
FOF_B_Buyout_IRR.teamIDvintagenumber = stringVectors(:,3);
FOF_B_Buyout_IRR.vint_t = data(:,4);
FOF_B_Buyout_IRR.vint_t1 = data(:,5);
FOF_B_Buyout_IRR.irr_t = data(:,6);
FOF_B_Buyout_IRR.irr_t1 = data(:,7);
FOF_B_Buyout_IRR.STATUS = categorical(stringVectors(:,4));
FOF_B_Buyout_IRR.FUNDMANAGER = categorical(stringVectors(:,5));
FOF_B_Buyout_IRR.size_t = data(:,8);
FOF_B_Buyout_IRR.size_t1 = data(:,9);
FOF_B_Buyout_IRR.seq_num_t = data(:,10);
FOF_B_Buyout_IRR.seq_num_t1 = data(:,11);
FOF_B_Buyout_IRR.fundgrowtht1t = data(:,12);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

FOF_B_Buyout_IRR.log_size_t = log(FOF_B_Buyout_IRR.size_t);


% Linear regression reg_B_Venture_IRR

regFOF_B_Buyout_IRR = fitlm(FOF_B_Buyout_IRR, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + fundgrowtht1t')

% output 
% %
% %    regFOF_B_Buyout_IRR = fitlm(FOF_B_Buyout_IRR, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + fundgrowtht1t')
% 
% regFOF_B_Buyout_IRR = 
% 
% 
% Linear regression model:
%     irr_t ~ 1 + irr_t1 + seq_num_t + fundgrowtht1t + log_size_t
% 
% Estimated Coefficients:
%                      Estimate       SE        tStat        pValue  
%                      ________    ________    ________    __________
% 
%     (Intercept)        22.141      3.4033      6.5058    1.5881e-10
%     irr_t1            0.17437    0.033613      5.1874    2.8878e-07
%     seq_num_t         0.83193     0.47072      1.7674      0.077659
%     fundgrowtht1t    0.021683     0.22692    0.095553       0.92391
%     log_size_t        -2.0472     0.52429     -3.9048    0.00010462
% 
% 
% Number of observations: 628, Error degrees of freedom: 623
% Root Mean Squared Error: 15.8
% R-squared: 0.067,  Adjusted R-Squared 0.061
% F-statistic vs. constant model: 11.2, p-value = 9e-09 
%  

% p-Values for fundgrowtht1t =   0.92391, so insignificant.

%DIAGNOSTICS of CLRM assumptions

T = regFOF_B_Buyout_IRR.NumObservations;
k = regFOF_B_Buyout_IRR.NumEstimatedCoefficients;
u = regFOF_B_Buyout_IRR.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_B_Buyout_IRR.Residuals.Raw);
Stdev_u = std(regFOF_B_Buyout_IRR.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =    1.7892e-14

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9638

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_B_Buyout_IRR.uRaw = u; 
FOF_B_Buyout_IRR.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_B_Buyout_IRR_White = table(FOF_B_Buyout_IRR.irr_t, FOF_B_Buyout_IRR.irr_t1, FOF_B_Buyout_IRR.log_size_t,...
    FOF_B_Buyout_IRR.seq_num_t, FOF_B_Buyout_IRR.fundgrowtht1t, ...
    FOF_B_Buyout_IRR.irr_t1.^2, FOF_B_Buyout_IRR.log_size_t.^2, FOF_B_Buyout_IRR.fundgrowtht1t.^2, FOF_B_Buyout_IRR.uRaw2);
 
FOF_B_Buyout_IRR_White.Properties.VariableNames={'irr_t','irr_t1','log_size_t','seq_num_t','fundgrowtht1t'...
    'irr_t1sq','log_size_tsq','fundgrowtht1tsq','uRaw2'};

% Unrestricted regression
regFOF_B_Buyout_IRR_White=fitlm(FOF_B_Buyout_IRR_White,'uRaw2 ~ irr_t1 + log_size_t + seq_num_t + fundgrowtht1t + irr_t1sq + log_size_tsq + fundgrowtht1tsq')

% output
% 
% regFOF_B_Buyout_IRR_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ 1 + irr_t1 + log_size_t + seq_num_t + fundgrowtht1t + irr_t1sq + log_size_tsq + fundgrowtht1tsq
% 
% Estimated Coefficients:
%                        Estimate       SE        tStat        pValue  
%                        ________    ________    ________    __________
% 
%     (Intercept)         2804.1       686.06      4.0873    4.9389e-05
%     irr_t1             -1.9117       2.9075    -0.65751        0.5111
%     log_size_t         -714.39       208.53     -3.4258    0.00065353
%     seq_num_t           21.892        27.93     0.78381       0.43345
%     fundgrowtht1t       70.067       25.038      2.7984     0.0052956
%     irr_t1sq           0.02148     0.021004      1.0227       0.30685
%     log_size_tsq        44.565       16.165      2.7568     0.0060085
%     fundgrowtht1tsq    -2.2371       1.0258     -2.1807      0.029577
% 
% 
% Number of observations: 628, Error degrees of freedom: 620
% Root Mean Squared Error: 881
% R-squared: 0.0573,  Adjusted R-Squared 0.0466
% F-statistic vs. constant model: 5.38, p-value = 5.33e-06

% 
% 

% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_B_Buyout_IRR_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e = 35.9629, Crit_Val_e = 9.4877, P_e = 2.9450e-07,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_B_Buyout_IRR.irr_t1 FOF_B_Buyout_IRR.log_size_t FOF_B_Buyout_IRR.seq_num_t FOF_B_Buyout_IRR.fundgrowtht1t];

y = FOF_B_Buyout_IRR.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','log_size_t','seq_num_t','fundgrowtht1t'};
White


% 
% 
% White =
% 
%   5×4 table
% 
%                        beta         SE        tstat         p     
%                      ________    ________    _______    __________
% 
%     (Intercept)        22.141      4.1087     5.3889    1.0053e-07
%     irr_t1            0.17437    0.051646     3.3762    0.00078034
%     log_size_t        -2.0472     0.65611    -3.1202     0.0018904
%     seq_num_t         0.83193     0.61043     1.3629       0.17342
%     fundgrowtht1t    0.021683     0.26244    0.08262       0.93418


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(FOF_B_Buyout_IRR.irr_t1,u)
corr_log_size_t_u = corr(FOF_B_Buyout_IRR.log_size_t,u)
corr_seq_num_t_u = corr(FOF_B_Buyout_IRR.seq_num_t,u)
corr_fundgrowtht1t_u = corr(FOF_B_Buyout_IRR.fundgrowtht1t,u)

%output:
% corr_irr_t1_u = 1.1085e-16
% corr_log_size_t_u = 1.6021e-16
% corr_seq_num_t_u = 5.2900e-17
% corr_fundgrowtht1t_u =   8.1712e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 1.0073
% kurt_jbtest = 14.2217
% test_stat_jbtest =  3.4013e+03
% crit_val_jbtest = 11.1433
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_B_Buyout_IRR.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_B_Buyout_IRR(regFOF_B_Buyout_IRR.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 6 funds with no clear criteria to exclude. The
% distribution looks like normal.


% FOF B ALL MULT
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (B) all_t_t-1 MULT','A2:T1318');
stringVectors = string(raw(:,[4,5,6,9,13,14]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,10,11,12,15,16,17,18,19,20]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_B_All_MULT = table;

% Allocate imported array to column variable names
FOF_B_All_MULT.number = data(:,1);
FOF_B_All_MULT.FUNDID = data(:,2);
FOF_B_All_MULT.FIRMID = data(:,3);
FOF_B_All_MULT.teamID = categorical(stringVectors(:,1));
FOF_B_All_MULT.teamIDvintage = stringVectors(:,2);
FOF_B_All_MULT.teamIDvintagenumber = stringVectors(:,3);
FOF_B_All_MULT.vint_t = data(:,4);
FOF_B_All_MULT.vint_t1 = data(:,5);
FOF_B_All_MULT.STRATEGY = categorical(stringVectors(:,4));
FOF_B_All_MULT.BuyoutDummy = data(:,6);
FOF_B_All_MULT.mult_t = data(:,7);
FOF_B_All_MULT.mult_t1 = data(:,8);
FOF_B_All_MULT.STATUS = categorical(stringVectors(:,5));
FOF_B_All_MULT.FUNDMANAGER = categorical(stringVectors(:,6));
FOF_B_All_MULT.size_t = data(:,9);
FOF_B_All_MULT.size_t1 = data(:,10);
FOF_B_All_MULT.seq_num_t = data(:,11);
FOF_B_All_MULT.seq_num_t1 = data(:,12);
FOF_B_All_MULT.fundgrowtht1t = data(:,13);
FOF_B_All_MULT.FundGrowthBuyout = data(:,14);

% Clear temporary variables
clearvars data raw stringVectors;

% Log(size)

FOF_B_All_MULT.log_size_t = log(FOF_B_All_MULT.size_t);


% Linear regression regFOF_B_All_MULT

regFOF_B_All_MULT = fitlm(FOF_B_All_MULT, 'mult_t ~ mult_t1 + FundGrowthBuyout + BuyoutDummy + log_size_t + seq_num_t + fundgrowtht1t')

% 
% regFOF_A_All_MULT = 
% 
% 
% Linear regression model:
%     mult_t ~ 1 + BuyoutDummy + mult_t1 + seq_num_t + fundgrowtht1t + FundGrowthBuyout + log_size_t
% 
% Estimated Coefficients:
%                         Estimate        SE        tStat       pValue  
%                         _________    ________    _______    __________
% 
%     (Intercept)            1.8009     0.21184     8.5011     5.047e-17
%     BuyoutDummy          0.060719     0.11841    0.51277        0.6082
%     mult_t1               0.17317    0.021371     8.1029    1.2191e-15
%     seq_num_t           -0.034289    0.025326    -1.3539         0.176
%     fundgrowtht1t       -0.072694    0.025345    -2.8681     0.0041951
%     FundGrowthBuyout      0.07431    0.034041     2.1829      0.029217
%     log_size_t          -0.047153    0.038362    -1.2292       0.21923
% 
% 
% Number of observations: 1317, Error degrees of freedom: 1310
% Root Mean Squared Error: 1.71
% R-squared: 0.0576,  Adjusted R-Squared 0.0533
% F-statistic vs. constant model: 13.3, p-value = 9.97e-15

% 



%DIAGNOSTICS of CLRM assumptions

T = regFOF_B_All_MULT.NumObservations;
k = regFOF_B_All_MULT.NumEstimatedCoefficients;
u = regFOF_B_All_MULT.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_B_All_MULT.Residuals.Raw);
Stdev_u = std(regFOF_B_All_MULT.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =  1.7058e-14

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9618

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_B_All_MULT.uRaw = u; 
FOF_B_All_MULT.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_B_All_MULT_White = table(FOF_B_All_MULT.mult_t, FOF_B_All_MULT.mult_t1, FOF_B_All_MULT.seq_num_t, FOF_B_All_MULT.log_size_t ,...
    FOF_B_All_MULT.fundgrowtht1t, FOF_B_All_MULT.FundGrowthBuyout, FOF_B_All_MULT.BuyoutDummy,...
    FOF_B_All_MULT.mult_t1.^2, FOF_B_All_MULT.log_size_t.^2, FOF_B_All_MULT.fundgrowtht1t.^2, FOF_B_All_MULT.FundGrowthBuyout.^2, FOF_B_All_MULT.uRaw2);

FOF_B_All_MULT_White.Properties.VariableNames = {'mult_t','mult_t1','seq_num_t','log_size_t', 'fundgrowtht1t','FundGrowthBuyout',...
    'BuyoutDummy','mult_t1sq','log_size_tsq','fundgrowtht1tsq','FundGrowthBuyoutsq','uRaw2'};

% Unrestricted regression
regFOF_B_All_MULT_White = fitlm(FOF_B_All_MULT_White,'uRaw2 ~ mult_t1 + seq_num_t + log_size_t + fundgrowtht1t + FundGrowthBuyout + BuyoutDummy + mult_t1sq + log_size_tsq + fundgrowtht1tsq + FundGrowthBuyoutsq')


% regFOF_B_All_MULT_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 11 terms in 10 predictors]
% 
% Estimated Coefficients:
%                           Estimate        SE        tStat       pValue  
%                           _________    ________    ________    _________
% 
%     (Intercept)             -1.4014      9.1966    -0.15238      0.87891
%     mult_t1                  3.9152     0.74904       5.227    2.004e-07
%     seq_num_t               -0.4449     0.47132    -0.94394      0.34537
%     log_size_t               1.0633      3.1402      0.3386      0.73496
%     fundgrowtht1t           -1.6217     0.86861      -1.867     0.062119
%     FundGrowthBuyout         1.3543      1.1572      1.1704      0.24207
%     BuyoutDummy             -6.3649      2.2826     -2.7884    0.0053737
%     mult_t1sq             -0.081905    0.026177     -3.1289    0.0017937
%     log_size_tsq          -0.094954     0.27124    -0.35008      0.72634
%     fundgrowtht1tsq        0.054573    0.039022      1.3985       0.1622
%     FundGrowthBuyoutsq     -0.04391    0.050966    -0.86155      0.38909
% 
% 
% Number of observations: 1317, Error degrees of freedom: 1306
% Root Mean Squared Error: 31.1
% R-squared: 0.0351,  Adjusted R-Squared 0.0277
% F-statistic vs. constant model: 4.75, p-value = 1.05e-06
% 



% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_B_All_MULT_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  46.1722, Crit_Val_e = 12.5916, P_e = 2.7358e-08
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_B_All_MULT.mult_t1 FOF_B_All_MULT.BuyoutDummy FOF_B_All_MULT.FundGrowthBuyout FOF_B_All_MULT.seq_num_t FOF_B_All_MULT.log_size_t FOF_B_All_MULT.fundgrowtht1t];

y = FOF_B_All_MULT.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','BuyoutDummy','FundGrowthBuyout','seq_num_t','log_size_t','fundgrowtht1t'};
White


% 
% White =
% 
%   7×4 table
% 
%                           beta          SE        tstat         p     
%                         _________    ________    _______    __________
% 
%     (Intercept)            1.8009     0.22472      8.014    2.4425e-15
%     mult_t1               0.17317    0.057822     2.9948     0.0027976
%     BuyoutDummy          0.060719     0.11618    0.52264       0.60131
%     FundGrowthBuyout      0.07431    0.024958     2.9775     0.0029597
%     seq_num_t           -0.034289    0.019638    -1.7461      0.081029
%     log_size_t          -0.047153    0.028594     -1.649      0.099379
%     fundgrowtht1t       -0.072694    0.022575    -3.2201     0.0013129
% 
% 



%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(FOF_B_All_MULT.mult_t1,u)
corr_seq_num_t_u = corr(FOF_B_All_MULT.seq_num_t,u)
corr_log_size_t_u = corr(FOF_B_All_MULT.log_size_t,u)
corr_buyout_dummy_u = corr(FOF_B_All_MULT.BuyoutDummy,u)
corr_fund_growth_buyout_u = corr(FOF_B_All_MULT.FundGrowthBuyout,u)
corr_fundgrowtht1t_u =  corr(FOF_B_All_MULT.fundgrowtht1t,u)

% output:
% corr_mult_t1_u = 6.8505e-17
% corr_seq_num_t_u = 4.9989e-18
% corr_log_size_t_u = 1.8299e-16
% corr_buyout_dummy_u = 4.2294e-17
% corr_fund_growth_buyout_u = 3.3350e-19
% corr_fundgrowtht1t_u = -5.1552e-18
%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest =    8.5537
% kurt_jbtest = 117.6133
% test_stat_jbtest = 7.3691e+05
% crit_val_jbtest = 14.4494
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_B_All_MULT.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_B_All_MULT(regFOF_B_All_MULT.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 10 with no exclusion criteria

% FOF B ALL IRR
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (B) all_t_t-1 IRR','A2:T1125');
stringVectors = string(raw(:,[4,5,6,9,13,14]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,10,11,12,15,16,17,18,19,20]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_B_All_IRR = table;

% Allocate imported array to column variable names
FOF_B_All_IRR.number = data(:,1);
FOF_B_All_IRR.FUNDID = data(:,2);
FOF_B_All_IRR.FIRMID = data(:,3);
FOF_B_All_IRR.teamID = categorical(stringVectors(:,1));
FOF_B_All_IRR.teamIDvintage = stringVectors(:,2);
FOF_B_All_IRR.teamIDvintagenumber = stringVectors(:,3);
FOF_B_All_IRR.vint_t = data(:,4);
FOF_B_All_IRR.vint_t1 = data(:,5);
FOF_B_All_IRR.STRATEGY = categorical(stringVectors(:,4));
FOF_B_All_IRR.BuyoutDummy = data(:,6);
FOF_B_All_IRR.irr_t = data(:,7);
FOF_B_All_IRR.irr_t1 = data(:,8);
FOF_B_All_IRR.STATUS = categorical(stringVectors(:,5));
FOF_B_All_IRR.FUNDMANAGER = categorical(stringVectors(:,6));
FOF_B_All_IRR.size_t = data(:,9);
FOF_B_All_IRR.size_t1 = data(:,10);
FOF_B_All_IRR.seq_num_t = data(:,11);
FOF_B_All_IRR.seq_num_t1 = data(:,12);
FOF_B_All_IRR.fundgrowtht1t = data(:,13);
FOF_B_All_IRR.FundGrowthBuyout = data(:,14);
FOF_B_All_IRR.VarName21 = stringVectors(:,7);

% Clear temporary variables
clearvars data raw stringVectors;

% Log(size)

FOF_B_All_IRR.log_size_t = log(FOF_B_All_IRR.size_t);


% Linear regression regFOF_B_All_IRR

regFOF_B_All_IRR = fitlm(FOF_B_All_IRR, 'irr_t ~ irr_t1 + FundGrowthBuyout + BuyoutDummy + log_size_t + seq_num_t + fundgrowtht1t')

% % regFOF_B_All_IRR = 
% 
% 
% Linear regression model:
%     irr_t ~ 1 + BuyoutDummy + irr_t1 + seq_num_t + fundgrowtht1t + FundGrowthBuyout + log_size_t
% 
% Estimated Coefficients:
%                         Estimate       SE        tStat       pValue  
%                         ________    ________    _______    __________
% 
%     (Intercept)          19.672       3.7879     5.1932    2.4551e-07
%     BuyoutDummy          2.9647       2.1067     1.4073       0.15963
%     irr_t1                0.167     0.024532     6.8075    1.6174e-11
%     seq_num_t           -0.4548      0.51026    -0.8913       0.37296
%     fundgrowtht1t       -1.3926      0.50752    -2.7438     0.0061697
%     FundGrowthBuyout     1.4448      0.64313     2.2465      0.024867
%     log_size_t          -1.4887       0.6915    -2.1529      0.031543
% 
% 
% Number of observations: 1124, Error degrees of freedom: 1117
% Root Mean Squared Error: 28.1
% R-squared: 0.0538,  Adjusted R-Squared 0.0488
% F-statistic vs. constant model: 10.6, p-value = 1.82e-11
% 
% 

%DIAGNOSTICS of CLRM assumptions

T = regFOF_B_All_IRR.NumObservations;
k = regFOF_B_All_IRR.NumEstimatedCoefficients;
u = regFOF_B_All_IRR.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_B_All_IRR.Residuals.Raw);
Stdev_u = std(regFOF_B_All_IRR.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =  2.4871e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9621

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_B_All_IRR.uRaw = u; 
FOF_B_All_IRR.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_B_All_IRR_White = table(FOF_B_All_IRR.irr_t, FOF_B_All_IRR.irr_t1, FOF_B_All_IRR.seq_num_t, FOF_B_All_IRR.log_size_t ,...
    FOF_B_All_IRR.fundgrowtht1t, FOF_B_All_IRR.FundGrowthBuyout, FOF_B_All_IRR.BuyoutDummy,...
    FOF_B_All_IRR.irr_t1.^2, FOF_B_All_IRR.log_size_t.^2, FOF_B_All_IRR.fundgrowtht1t.^2, FOF_B_All_IRR.FundGrowthBuyout.^2, FOF_B_All_IRR.uRaw2);

FOF_B_All_IRR_White.Properties.VariableNames = {'irr_t','irr_t1','seq_num_t','log_size_t', 'fundgrowtht1t','FundGrowthBuyout',...
    'BuyoutDummy','irr_t1sq','log_size_tsq','fundgrowtht1tsq','FundGrowthBuyoutsq','uRaw2'};

% Unrestricted regression
regFOF_B_All_IRR_White = fitlm(FOF_B_All_IRR_White,'uRaw2 ~ irr_t1 + seq_num_t + log_size_t + fundgrowtht1t + FundGrowthBuyout + BuyoutDummy + irr_t1sq + log_size_tsq + fundgrowtht1tsq + FundGrowthBuyoutsq')


% Linear regression model:
%     uRaw2 ~ [Linear formula with 11 terms in 10 predictors]
% 
% Estimated Coefficients:
%                            Estimate         SE         tStat        pValue  
%                           ___________    ________    _________    __________
% 
%     (Intercept)                2677.6        2332       1.1482       0.25114
%     irr_t1                     43.462      10.615       4.0944    4.5379e-05
%     seq_num_t                 -82.262       135.1      -0.6089       0.54272
%     log_size_t                -381.36      802.85       -0.475       0.63488
%     fundgrowtht1t             -561.19      317.33      -1.7685      0.077253
%     FundGrowthBuyout           540.45      368.79       1.4655       0.14308
%     BuyoutDummy               -1475.1      586.84      -2.5137      0.012087
%     irr_t1sq              -0.00051444    0.033035    -0.015573       0.98758
%     log_size_tsq               22.017        69.2      0.31817       0.75042
%     fundgrowtht1tsq            30.391      21.892       1.3882       0.16536
%     FundGrowthBuyoutsq        -29.341      23.082      -1.2712       0.20393
% 
% 
% Number of observations: 1124, Error degrees of freedom: 1113
% Root Mean Squared Error: 7.16e+03
% R-squared: 0.0497,  Adjusted R-Squared 0.0412
% F-statistic vs. constant model: 5.82, p-value = 1.37e-08

% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_B_All_IRR_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  55.8560, Crit_Val_e = 12.5916, P_e =  3.1127e-10
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_B_All_IRR.irr_t1 FOF_B_All_IRR.BuyoutDummy FOF_B_All_IRR.FundGrowthBuyout FOF_B_All_IRR.seq_num_t FOF_B_All_IRR.log_size_t FOF_B_All_IRR.fundgrowtht1t];

y = FOF_B_All_IRR.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','BuyoutDummy','FundGrowthBuyout','seq_num_t','log_size_t','fundgrowtht1t'};
White



% White =
% 
%   7×4 table
% 
%                          beta         SE        tstat         p     
%                         _______    ________    _______    __________
% 
%     (Intercept)          19.672      4.3071     4.5673    5.4872e-06
%     irr_t1                0.167    0.065697      2.542      0.011155
%     BuyoutDummy          2.9647      2.6284      1.128       0.25958
%     FundGrowthBuyout     1.4448       0.533     2.7106     0.0068178
%     seq_num_t           -0.4548     0.42393    -1.0728       0.28358
%     log_size_t          -1.4887     0.60426    -2.4637      0.013902
%     fundgrowtht1t       -1.3926     0.48562    -2.8676     0.0042137




%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(FOF_B_All_IRR.irr_t1,u)
corr_seq_num_t_u = corr(FOF_B_All_IRR.seq_num_t,u)
corr_log_size_t_u = corr(FOF_B_All_IRR.log_size_t,u)
corr_buyout_dummy_u = corr(FOF_B_All_IRR.BuyoutDummy,u)
corr_fund_growth_buyout_u = corr(FOF_B_All_IRR.FundGrowthBuyout,u)
corr_fundgrowtht1t_u =  corr(FOF_B_All_IRR.fundgrowtht1t,u)

% output:
% corr_irr_t1_u =  -2.0287e-16
% corr_seq_num_t_u =  7.1115e-17
% corr_log_size_t_u = 1.8014e-17
% corr_buyout_dummy_u = -2.2779e-17
% corr_fund_growth_buyout_u = -2.8334e-17
% corr_fundgrowtht1t_u = 4.8282e-19
%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest =    6.4654
% kurt_jbtest = 88.0135
% test_stat_jbtest = 3.4631e+05
% crit_val_jbtest = 14.4494
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_B_All_IRR.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_B_All_IRR(regFOF_B_All_IRR.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 6 funds with crazy IRRs and no exclusion criteria

%% Section 5.3.3
% FOF C VENTURE MULT
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (C) venture_t_t-1 MULT','A2:AM603');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_C_Venture_MULT = table;

% Allocate imported array to column variable names
FOF_C_Venture_MULT.number = data(:,1);
FOF_C_Venture_MULT.FUNDID = data(:,2);
FOF_C_Venture_MULT.FIRMID = data(:,3);
FOF_C_Venture_MULT.teamID = categorical(stringVectors(:,1));
FOF_C_Venture_MULT.teamIDvintage = stringVectors(:,2);
FOF_C_Venture_MULT.teamIDvintagenumber = stringVectors(:,3);
FOF_C_Venture_MULT.vint_t = data(:,4);
FOF_C_Venture_MULT.vint_t1 = data(:,5);
FOF_C_Venture_MULT.mult_t = data(:,6);
FOF_C_Venture_MULT.mult_t1 = data(:,7);
FOF_C_Venture_MULT.STATUS = categorical(stringVectors(:,4));
FOF_C_Venture_MULT.FUNDMANAGER = categorical(stringVectors(:,5));
FOF_C_Venture_MULT.size_t = data(:,8);
FOF_C_Venture_MULT.size_t1 = data(:,9);
FOF_C_Venture_MULT.seq_num_t = data(:,10);
FOF_C_Venture_MULT.seq_num_t1 = data(:,11);
FOF_C_Venture_MULT.fundgrowtht1t = data(:,12);
FOF_C_Venture_MULT.FundGrowthPerformance = data(:,13);
FOF_C_Venture_MULT.vint90 = data(:,14);
FOF_C_Venture_MULT.vint91 = data(:,15);
FOF_C_Venture_MULT.vint92 = data(:,16);
FOF_C_Venture_MULT.vint93 = data(:,17);
FOF_C_Venture_MULT.vint94 = data(:,18);
FOF_C_Venture_MULT.vint95 = data(:,19);
FOF_C_Venture_MULT.vint96 = data(:,20);
FOF_C_Venture_MULT.vint97 = data(:,21);
FOF_C_Venture_MULT.vint98 = data(:,22);
FOF_C_Venture_MULT.vint99 = data(:,23);
FOF_C_Venture_MULT.vint00 = data(:,24);
FOF_C_Venture_MULT.vint01 = data(:,25);
FOF_C_Venture_MULT.vint02 = data(:,26);
FOF_C_Venture_MULT.vint03 = data(:,27);
FOF_C_Venture_MULT.vint04 = data(:,28);
FOF_C_Venture_MULT.vint05 = data(:,29);
FOF_C_Venture_MULT.vint06 = data(:,30);
FOF_C_Venture_MULT.vint07 = data(:,31);
FOF_C_Venture_MULT.vint08 = data(:,32);
FOF_C_Venture_MULT.vint09 = data(:,33);
FOF_C_Venture_MULT.vint10 = data(:,34);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

FOF_C_Venture_MULT.log_size_t = log(FOF_C_Venture_MULT.size_t);

% Normalize  independent variables 
% ("Independent variables are standardized with a mean 0 and a standard deviation 1 
% to reduce the possible multi- collinearity problem and to ease the
% interpretation of the interaction term")
% normalize function does not exist in the current matlab licence
% FOF_C_Venture_MULT.norm_fundgrowtht1t = normalize(FOF_C_Venture_MULT,'zscore','std','DataVariables','fundgrowtht1t');
% FOF_C_Venture_MULT.norm_FundGrowthPerformance = normalize(FOF_C_Venture_MULT,'zscore','std','DataVariables','FundGrowthPerformance');
% FOF_C_Venture_MULT.norm_mult_t1 = normalize(FOF_C_Venture_MULT,'zscore','std','DataVariables','mult_t1');

FOF_C_Venture_MULT.norm_fundgrowtht1t = zscore(FOF_C_Venture_MULT.fundgrowtht1t);
FOF_C_Venture_MULT.norm_FundGrowthPerformance = zscore(FOF_C_Venture_MULT.FundGrowthPerformance);
FOF_C_Venture_MULT.norm_mult_t1 = zscore(FOF_C_Venture_MULT.mult_t1);

% Linear regression reg_C_Venture_MULT
% 
regFOF_C_Venture_MULT = fitlm(FOF_C_Venture_MULT, 'mult_t ~ norm_mult_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_FundGrowthPerformance + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')


% regFOF_C_Venture_MULT = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                                   Estimate        SE         tStat        pValue  
%                                   _________    ________    _________    __________
% 
%     (Intercept)                      1.6877      1.5721       1.0735       0.28348
%     seq_num_t                     -0.038113    0.041157     -0.92603       0.35482
%     vint91                         -0.41643      2.1727     -0.19166       0.84807
%     vint92                         0.072351      1.8186     0.039783       0.96828
%     vint93                         -0.33401       2.174     -0.15364       0.87795
%     vint94                           4.9077      1.6698       2.9392     0.0034231
%     vint95                           4.3233      1.6848       2.5661      0.010537
%     vint96                           2.6143      1.6379       1.5962         0.111
%     vint97                          0.55504      1.5812      0.35102        0.7257
%     vint98                         -0.25768      1.5757     -0.16354       0.87015
%     vint99                          -1.0581      1.5695     -0.67417       0.50047
%     vint00                         -0.76229      1.5581     -0.48925       0.62485
%     vint01                         -0.45693      1.5708     -0.29089       0.77124
%     vint02                          -0.4321      1.5958     -0.27078       0.78666
%     vint03                          -0.4419      1.6065     -0.27507       0.78336
%     vint04                         -0.46641      1.6041     -0.29076       0.77134
%     vint05                          -0.1622      1.5826     -0.10249       0.91841
%     vint06                         -0.20239      1.5687     -0.12902       0.89739
%     vint07                        -0.062595       1.569    -0.039893       0.96819
%     vint08                          0.39813      1.5707      0.25348       0.79999
%     vint09                        -0.088887      1.6073    -0.055302       0.95592
%     vint10                         -0.11837      1.5974    -0.074097       0.94096
%     log_size_t                     0.045824     0.07621      0.60129       0.54789
%     norm_fundgrowtht1t            -0.046712     0.10756     -0.43427       0.66426
%     norm_FundGrowthPerformance     -0.24239      0.1259      -1.9253      0.054691
%     norm_mult_t1                     0.5424     0.11198       4.8436    1.6412e-06
% 
% 
% Number of observations: 601, Error degrees of freedom: 575
% Root Mean Squared Error: 2.17
% R-squared: 0.258,  Adjusted R-Squared 0.226
% F-statistic vs. constant model: 8, p-value = 1.83e-24
% 
% 


% DIAGNOSTICS of CLRM assumptions

T = regFOF_C_Venture_MULT.NumObservations;
k = regFOF_C_Venture_MULT.NumEstimatedCoefficients;
u = regFOF_C_Venture_MULT.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_C_Venture_MULT.Residuals.Raw);
Stdev_u = std(regFOF_C_Venture_MULT.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -4.1541e-16

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9641

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_C_Venture_MULT.uRaw = u; 
FOF_C_Venture_MULT.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_C_Venture_MULT_White = table(FOF_C_Venture_MULT.mult_t, FOF_C_Venture_MULT.norm_mult_t1, FOF_C_Venture_MULT.log_size_t,...
    FOF_C_Venture_MULT.seq_num_t, FOF_C_Venture_MULT.norm_fundgrowtht1t, FOF_C_Venture_MULT.norm_FundGrowthPerformance, ...
    FOF_C_Venture_MULT.vint91, FOF_C_Venture_MULT.vint92, FOF_C_Venture_MULT.vint93, FOF_C_Venture_MULT.vint94,...
    FOF_C_Venture_MULT.vint95, FOF_C_Venture_MULT.vint96, FOF_C_Venture_MULT.vint97, FOF_C_Venture_MULT.vint98, FOF_C_Venture_MULT.vint99,...
    FOF_C_Venture_MULT.vint00, FOF_C_Venture_MULT.vint01, FOF_C_Venture_MULT.vint02, FOF_C_Venture_MULT.vint03, FOF_C_Venture_MULT.vint04,...
    FOF_C_Venture_MULT.vint05, FOF_C_Venture_MULT.vint06, FOF_C_Venture_MULT.vint07, FOF_C_Venture_MULT.vint08, FOF_C_Venture_MULT.vint09, FOF_C_Venture_MULT.vint10,...
    FOF_C_Venture_MULT.norm_mult_t1.^2, FOF_C_Venture_MULT.log_size_t.^2, FOF_C_Venture_MULT.norm_fundgrowtht1t.^2, FOF_C_Venture_MULT.norm_FundGrowthPerformance.^2, FOF_C_Venture_MULT.uRaw2);

 
FOF_C_Venture_MULT_White.Properties.VariableNames={'mult_t','norm_mult_t1','log_size_t','seq_num_t','norm_fundgrowtht1t','norm_FundGrowthPerformance',...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'norm_mult_t1sq','log_size_tsq','norm_fundgrowtht1tsq','norm_FundGrowthPerformancesq','uRaw2'};

% Unrestricted regression
regFOF_C_Venture_MULT_White=fitlm(FOF_C_Venture_MULT_White,'uRaw2 ~ norm_mult_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_FundGrowthPerformance + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + norm_mult_t1sq + log_size_tsq + norm_fundgrowtht1tsq + norm_FundGrowthPerformancesq')

% 
% regFOF_C_Venture_MULT_White = 

% Linear regression model:
%     uRaw2 ~ [Linear formula with 30 terms in 29 predictors]
% 
% Estimated Coefficients:
%                                     Estimate       SE         tStat       pValue  
%                                     _________    _______    _________    _________
% 
%     (Intercept)                      -0.72273     26.929    -0.026838       0.9786
%     norm_mult_t1                       10.565     3.2941       3.2072    0.0014156
%     log_size_t                        0.38739     6.1269     0.063228      0.94961
%     seq_num_t                        -0.22045    0.60983     -0.36149      0.71787
%     norm_fundgrowtht1t                0.15363     3.4275     0.044822      0.96426
%     norm_FundGrowthPerformance        -4.4708     4.9573     -0.90186      0.36751
%     vint91                             3.0549     31.791     0.096094      0.92348
%     vint92                            0.54002      26.61     0.020294      0.98382
%     vint93                             3.0775      31.88     0.096533      0.92313
%     vint94                             71.012     24.428        2.907    0.0037907
%     vint95                             26.897     24.691       1.0893      0.27646
%     vint96                             33.791     24.039       1.4057      0.16037
%     vint97                            0.97375     23.152     0.042059      0.96647
%     vint98                             4.6195      23.08      0.20015      0.84143
%     vint99                           -0.42762     23.019    -0.018577      0.98519
%     vint00                             2.8984     22.875      0.12671      0.89922
%     vint01                             4.5567     23.044      0.19774      0.84332
%     vint02                             3.0101     23.389       0.1287      0.89764
%     vint03                             3.5433     23.527      0.15061      0.88034
%     vint04                             2.1533     23.489     0.091673      0.92699
%     vint05                             3.7561     23.173      0.16209      0.87129
%     vint06                             3.4522     22.986      0.15019      0.88067
%     vint07                             3.2272      22.97       0.1405      0.88832
%     vint08                             7.8612     23.008      0.34167      0.73272
%     vint09                             2.1224     23.552     0.090114      0.92823
%     vint10                            0.93008     23.395     0.039755       0.9683
%     norm_mult_t1sq                   -0.66498    0.32545      -2.0433     0.041484
%     log_size_tsq                    -0.041167    0.65881    -0.062487       0.9502
%     norm_fundgrowtht1tsq             0.098179    0.36556      0.26857      0.78836
%     norm_FundGrowthPerformancesq      0.19573     0.4417      0.44313      0.65784
% 
% 
% Number of observations: 601, Error degrees of freedom: 571
% Root Mean Squared Error: 31.8
% R-squared: 0.147,  Adjusted R-Squared 0.104
% F-statistic vs. constant model: 3.41, p-value = 1.16e-08



% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_C_Venture_MULT_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  88.6145, Crit_Val_e = 37.6525, P_e = 4.8090e-09,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_C_Venture_MULT.norm_mult_t1 FOF_C_Venture_MULT.log_size_t FOF_C_Venture_MULT.seq_num_t FOF_C_Venture_MULT.norm_fundgrowtht1t FOF_C_Venture_MULT.norm_FundGrowthPerformance ...
    FOF_C_Venture_MULT.vint91 FOF_C_Venture_MULT.vint92 FOF_C_Venture_MULT.vint93 FOF_C_Venture_MULT.vint94 ...
    FOF_C_Venture_MULT.vint95 FOF_C_Venture_MULT.vint96 FOF_C_Venture_MULT.vint97 FOF_C_Venture_MULT.vint98 FOF_C_Venture_MULT.vint99 ...
    FOF_C_Venture_MULT.vint00 FOF_C_Venture_MULT.vint01 FOF_C_Venture_MULT.vint02 FOF_C_Venture_MULT.vint03 FOF_C_Venture_MULT.vint04 ...
    FOF_C_Venture_MULT.vint05 FOF_C_Venture_MULT.vint06 FOF_C_Venture_MULT.vint07 FOF_C_Venture_MULT.vint08 FOF_C_Venture_MULT.vint09 FOF_C_Venture_MULT.vint10];

y = FOF_C_Venture_MULT.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','norm_mult_t1','log_size_t','seq_num_t','norm_fundgrowtht1t','norm_FundGrowthPerformance',...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White


% 
% White =
% 
%   26×4 table
% 
%                                     beta          SE        tstat          p    
%                                   _________    ________    ________    _________
% 
%     (Intercept)                      1.6877     0.56996       2.961    0.0031874
%     norm_mult_t1                     0.5424     0.22565      2.4037     0.016531
%     log_size_t                     0.045824    0.056949     0.80465      0.42134
%     seq_num_t                     -0.038113    0.017023     -2.2389     0.025529
%     norm_fundgrowtht1t            -0.046712    0.058992    -0.79183      0.42877
%     norm_FundGrowthPerformance     -0.24239     0.15081     -1.6073      0.10852
%     vint91                         -0.41643     0.51175    -0.81374      0.41612
%     vint92                         0.072351     0.48938     0.14784      0.88252
%     vint93                         -0.33401     0.57883    -0.57704      0.56413
%     vint94                           4.9077      2.6189      1.8739     0.061425
%     vint95                           4.3233       1.717      2.5179     0.012065
%     vint96                           2.6143      1.5907      1.6435      0.10081
%     vint97                          0.55504     0.55653     0.99732      0.31901
%     vint98                         -0.25768     0.60417    -0.42651      0.66989
%     vint99                          -1.0581     0.55405     -1.9098      0.05664
%     vint00                         -0.76229     0.52987     -1.4386      0.15077
%     vint01                         -0.45693     0.52522    -0.86998      0.38466
%     vint02                          -0.4321     0.53035    -0.81475      0.41554
%     vint03                          -0.4419     0.53397    -0.82758      0.40824
%     vint04                         -0.46641     0.51093    -0.91286      0.36168
%     vint05                          -0.1622     0.56549    -0.28683      0.77434
%     vint06                         -0.20239     0.53125    -0.38097      0.70336
%     vint07                        -0.062595     0.53492    -0.11702      0.90689
%     vint08                          0.39813     0.61788     0.64435       0.5196
%     vint09                        -0.088887     0.55596    -0.15988      0.87303
%     vint10                         -0.11837     0.52842      -0.224      0.82283

% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_norm_mult_t1_u = corr(FOF_C_Venture_MULT.norm_mult_t1,u)
corr_log_size_t_u = corr(FOF_C_Venture_MULT.log_size_t,u)
corr_seq_num_t_u = corr(FOF_C_Venture_MULT.seq_num_t,u)
corr_norm_fundgrowtht1t_u = corr(FOF_C_Venture_MULT.norm_fundgrowtht1t,u)

%output:
% corr_mult_t1_u = 2.5829e-17
% corr_log_size_t_u = 3.1236e-17
% corr_seq_num_t_u = 2.4238e-17
% corr_fundgrowtht1t_u = 6.9742e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 5.7013
% kurt_jbtest = 56.0322
% test_stat_jbtest = 7.3683e+04
% crit_val_jbtest = 40.6465
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_C_Venture_MULT.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_C_Venture_MULT(regFOF_C_Venture_MULT.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 7 funds. No creteria to exclude them 


 
% FOF C VENTURE IRR
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (C) venture_t_t-1 IRR','A2:AM603');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_C_Venture_IRR = table;

% Allocate imported array to column variable names
FOF_C_Venture_IRR.number = data(:,1);
FOF_C_Venture_IRR.FUNDID = data(:,2);
FOF_C_Venture_IRR.FIRMID = data(:,3);
FOF_C_Venture_IRR.teamID = categorical(stringVectors(:,1));
FOF_C_Venture_IRR.teamIDvintage = stringVectors(:,2);
FOF_C_Venture_IRR.teamIDvintagenumber = stringVectors(:,3);
FOF_C_Venture_IRR.vint_t = data(:,4);
FOF_C_Venture_IRR.vint_t1 = data(:,5);
FOF_C_Venture_IRR.irr_t = data(:,6);
FOF_C_Venture_IRR.irr_t1 = data(:,7);
FOF_C_Venture_IRR.STATUS = categorical(stringVectors(:,4));
FOF_C_Venture_IRR.FUNDMANAGER = categorical(stringVectors(:,5));
FOF_C_Venture_IRR.size_t = data(:,8);
FOF_C_Venture_IRR.size_t1 = data(:,9);
FOF_C_Venture_IRR.seq_num_t = data(:,10);
FOF_C_Venture_IRR.seq_num_t1 = data(:,11);
FOF_C_Venture_IRR.fundgrowtht1t = data(:,12);
FOF_C_Venture_IRR.FundGrowthPerformance = data(:,13);
FOF_C_Venture_IRR.vint90 = data(:,14);
FOF_C_Venture_IRR.vint91 = data(:,15);
FOF_C_Venture_IRR.vint92 = data(:,16);
FOF_C_Venture_IRR.vint93 = data(:,17);
FOF_C_Venture_IRR.vint94 = data(:,18);
FOF_C_Venture_IRR.vint95 = data(:,19);
FOF_C_Venture_IRR.vint96 = data(:,20);
FOF_C_Venture_IRR.vint97 = data(:,21);
FOF_C_Venture_IRR.vint98 = data(:,22);
FOF_C_Venture_IRR.vint99 = data(:,23);
FOF_C_Venture_IRR.vint00 = data(:,24);
FOF_C_Venture_IRR.vint01 = data(:,25);
FOF_C_Venture_IRR.vint02 = data(:,26);
FOF_C_Venture_IRR.vint03 = data(:,27);
FOF_C_Venture_IRR.vint04 = data(:,28);
FOF_C_Venture_IRR.vint05 = data(:,29);
FOF_C_Venture_IRR.vint06 = data(:,30);
FOF_C_Venture_IRR.vint07 = data(:,31);
FOF_C_Venture_IRR.vint08 = data(:,32);
FOF_C_Venture_IRR.vint09 = data(:,33);
FOF_C_Venture_IRR.vint10 = data(:,34);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

FOF_C_Venture_IRR.log_size_t = log(FOF_C_Venture_IRR.size_t);

% Normalize  independent variables 
% ("Independent variables are standardized with a mean 0 and a standard deviation 1 
% to reduce the possible multi- collinearity problem and to ease the
% interpretation of the interaction term")
% normalize function does not exist in the current matlab licence
% FOF_C_Venture_MULT.norm_fundgrowtht1t = normalize(FOF_C_Venture_MULT,'zscore','std','DataVariables','fundgrowtht1t');
% FOF_C_Venture_MULT.norm_FundGrowthPerformance = normalize(FOF_C_Venture_MULT,'zscore','std','DataVariables','FundGrowthPerformance');
% FOF_C_Venture_MULT.norm_mult_t1 = normalize(FOF_C_Venture_MULT,'zscore','std','DataVariables','mult_t1');

FOF_C_Venture_IRR.norm_fundgrowtht1t = zscore(FOF_C_Venture_IRR.fundgrowtht1t);
FOF_C_Venture_IRR.norm_FundGrowthPerformance = zscore(FOF_C_Venture_IRR.FundGrowthPerformance);
FOF_C_Venture_IRR.norm_irr_t1 = zscore(FOF_C_Venture_IRR.irr_t1);

% Linear regression reg_C_Venture_IRR
% 
regFOF_C_Venture_IRR = fitlm(FOF_C_Venture_IRR, 'irr_t ~ norm_irr_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_FundGrowthPerformance + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')


% regFOF_C_Venture_IRR = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 26 terms in 25 predictors]
% 
% Estimated Coefficients:
%                                   Estimate      SE         tStat        pValue  
%                                   ________    _______    _________    __________
% 
%     (Intercept)                    22.479      35.844      0.62715       0.53087
%     seq_num_t                     -1.0619     0.83336      -1.2743       0.20319
%     vint91                        -16.855      50.052     -0.33674       0.73646
%     vint92                        -10.022       38.77      -0.2585       0.79613
%     vint93                         -6.711      50.079     -0.13401       0.89345
%     vint94                         24.108      37.108      0.64967       0.51622
%     vint95                         44.307      37.126       1.1934       0.23331
%     vint96                         11.853      36.645      0.32345        0.7465
%     vint97                         23.546      36.041      0.65331       0.51388
%     vint98                        0.34495      35.879    0.0096142       0.99233
%     vint99                        -29.661      35.824     -0.82795       0.40812
%     vint00                        -24.965      35.755     -0.69823       0.48538
%     vint01                        -19.053      35.912     -0.53056       0.59597
%     vint02                        -18.863      36.204     -0.52102        0.6026
%     vint03                        -20.532      36.403     -0.56403         0.573
%     vint04                        -22.558      36.283     -0.62173       0.53442
%     vint05                        -15.411      36.129     -0.42657       0.66989
%     vint06                         -17.49      35.877     -0.48751       0.62613
%     vint07                         -12.39      35.845     -0.34567       0.72975
%     vint08                        -12.984      35.836     -0.36231       0.71728
%     vint09                        -7.1512      36.699     -0.19486       0.84559
%     vint10                        -9.4805      36.286     -0.26127         0.794
%     log_size_t                    0.96477      1.3779      0.70016       0.48418
%     norm_fundgrowtht1t            -1.9551      1.6775      -1.1655        0.2444
%     norm_FundGrowthPerformance    -6.1232       2.112      -2.8992     0.0039164
%     norm_irr_t1                     9.107       2.189       4.1602    3.7804e-05
% 
% 
% Number of observations: 496, Error degrees of freedom: 470
% Root Mean Squared Error: 35.4
% R-squared: 0.228,  Adjusted R-Squared 0.187
% F-statistic vs. constant model: 5.55, p-value = 3.56e-15
% 
% % 


% DIAGNOSTICS of CLRM assumptions

T = regFOF_C_Venture_IRR.NumObservations;
k = regFOF_C_Venture_IRR.NumEstimatedCoefficients;
u = regFOF_C_Venture_IRR.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_C_Venture_IRR.Residuals.Raw);
Stdev_u = std(regFOF_C_Venture_IRR.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   6.9376e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9650

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_C_Venture_IRR.uRaw = u; 
FOF_C_Venture_IRR.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_C_Venture_IRR_White = table(FOF_C_Venture_IRR.irr_t, FOF_C_Venture_IRR.norm_irr_t1, FOF_C_Venture_IRR.log_size_t,...
    FOF_C_Venture_IRR.seq_num_t, FOF_C_Venture_IRR.norm_fundgrowtht1t, FOF_C_Venture_IRR.norm_FundGrowthPerformance, ...
    FOF_C_Venture_IRR.vint91, FOF_C_Venture_IRR.vint92, FOF_C_Venture_IRR.vint93, FOF_C_Venture_IRR.vint94,...
    FOF_C_Venture_IRR.vint95, FOF_C_Venture_IRR.vint96, FOF_C_Venture_IRR.vint97, FOF_C_Venture_IRR.vint98, FOF_C_Venture_IRR.vint99,...
    FOF_C_Venture_IRR.vint00, FOF_C_Venture_IRR.vint01, FOF_C_Venture_IRR.vint02, FOF_C_Venture_IRR.vint03, FOF_C_Venture_IRR.vint04,...
    FOF_C_Venture_IRR.vint05, FOF_C_Venture_IRR.vint06, FOF_C_Venture_IRR.vint07, FOF_C_Venture_IRR.vint08, FOF_C_Venture_IRR.vint09, FOF_C_Venture_IRR.vint10,...
    FOF_C_Venture_IRR.norm_irr_t1.^2, FOF_C_Venture_IRR.log_size_t.^2, FOF_C_Venture_IRR.norm_fundgrowtht1t.^2, FOF_C_Venture_IRR.norm_FundGrowthPerformance.^2, FOF_C_Venture_IRR.uRaw2);

 
FOF_C_Venture_IRR_White.Properties.VariableNames={'irr_t','norm_irr_t1','log_size_t','seq_num_t','norm_fundgrowtht1t','norm_FundGrowthPerformance',...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'norm_irr_t1sq','log_size_tsq','norm_fundgrowtht1tsq','norm_FundGrowthPerformancesq','uRaw2'};

% Unrestricted regression
regFOF_C_Venture_IRR_White=fitlm(FOF_C_Venture_IRR_White,'uRaw2 ~ norm_irr_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_FundGrowthPerformance + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + norm_irr_t1sq + log_size_tsq + norm_fundgrowtht1tsq + norm_FundGrowthPerformancesq')

% regFOF_C_Venture_IRR_White = 

% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 30 terms in 29 predictors]
% 
% Estimated Coefficients:
%                                     Estimate      SE        tStat       pValue 
%                                     ________    ______    __________    _______
% 
%     (Intercept)                      -335.59     11215     -0.029922    0.97614
%     norm_irr_t1                       1956.7      1031        1.8979    0.05833
%     log_size_t                       -178.05    2090.6     -0.085165    0.93217
%     seq_num_t                        -30.508    241.04      -0.12657    0.89934
%     norm_fundgrowtht1t               -851.91    1105.6      -0.77052    0.44138
%     norm_FundGrowthPerformance       -525.68    1279.8      -0.41075    0.68144
%     vint91                            1497.4     14092       0.10626    0.91542
%     vint92                            941.98     10920      0.086264    0.93129
%     vint93                            2327.2     14166       0.16428    0.86958
%     vint94                            2021.4     10450       0.19344     0.8467
%     vint95                            3354.8     10462       0.32065    0.74862
%     vint96                              2363     10333       0.22868    0.81922
%     vint97                            3475.9     10161       0.34207    0.73245
%     vint98                            8816.6     10119       0.87132    0.38403
%     vint99                            1125.1     10117       0.11121     0.9115
%     vint00                              1442     10123       0.14245    0.88678
%     vint01                            2019.9     10145       0.19911    0.84226
%     vint02                            1439.8     10214       0.14097    0.88795
%     vint03                            1708.8     10264       0.16649    0.86784
%     vint04                            1403.3     10226       0.13723    0.89091
%     vint05                            1839.9     10185       0.18064    0.85673
%     vint06                            1795.5     10119       0.17743    0.85924
%     vint07                            1927.2     10109       0.19065    0.84888
%     vint08                            2028.4     10109       0.20064    0.84106
%     vint09                            1466.7     10344       0.14179     0.8873
%     vint10                            1315.6     10224       0.12869    0.89766
%     norm_irr_t1sq                      84.87    140.56       0.60379    0.54628
%     log_size_tsq                    0.048433    225.65    0.00021464    0.99983
%     norm_fundgrowtht1tsq              165.72    216.21       0.76646    0.44379
%     norm_FundGrowthPerformancesq     -55.691    85.337      -0.65261    0.51433
% 
% 
% Number of observations: 496, Error degrees of freedom: 466
% Root Mean Squared Error: 9.95e+03
% R-squared: 0.0863,  Adjusted R-Squared 0.0294
% F-statistic vs. constant model: 1.52, p-value = 0.0431

% 
% 

% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_C_Venture_IRR_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  42.7975, Crit_Val_e = 37.6525, P_e = 0.0147,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_C_Venture_IRR.norm_irr_t1 FOF_C_Venture_IRR.log_size_t FOF_C_Venture_IRR.seq_num_t FOF_C_Venture_IRR.norm_fundgrowtht1t FOF_C_Venture_IRR.norm_FundGrowthPerformance ...
    FOF_C_Venture_IRR.vint91 FOF_C_Venture_IRR.vint92 FOF_C_Venture_IRR.vint93 FOF_C_Venture_IRR.vint94 ...
    FOF_C_Venture_IRR.vint95 FOF_C_Venture_IRR.vint96 FOF_C_Venture_IRR.vint97 FOF_C_Venture_IRR.vint98 FOF_C_Venture_IRR.vint99 ...
    FOF_C_Venture_IRR.vint00 FOF_C_Venture_IRR.vint01 FOF_C_Venture_IRR.vint02 FOF_C_Venture_IRR.vint03 FOF_C_Venture_IRR.vint04 ...
    FOF_C_Venture_IRR.vint05 FOF_C_Venture_IRR.vint06 FOF_C_Venture_IRR.vint07 FOF_C_Venture_IRR.vint08 FOF_C_Venture_IRR.vint09 FOF_C_Venture_IRR.vint10];

y = FOF_C_Venture_IRR.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','norm_irr_t1','log_size_t','seq_num_t','norm_fundgrowtht1t','norm_FundGrowthPerformance',...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White


% 
% White =
% 26×4 table
% 
%                                    beta        SE        tstat          p     
%                                   _______    _______    ________    __________
% 
%     (Intercept)                    22.479     5.6993      3.9442    9.1634e-05
%     norm_irr_t1                     9.107     4.7919      1.9005      0.057952
%     log_size_t                    0.96477     1.1674     0.82639       0.40898
%     seq_num_t                     -1.0619    0.40075     -2.6499     0.0083107
%     norm_fundgrowtht1t            -1.9551     1.0917      -1.791      0.073912
%     norm_FundGrowthPerformance    -6.1232     3.0349     -2.0176       0.04417
%     vint91                        -16.855     2.5793     -6.5345    1.5917e-10
%     vint92                        -10.022     3.5753     -2.8031     0.0052605
%     vint93                         -6.711     3.4041     -1.9714      0.049233
%     vint94                         24.108     11.097      2.1724      0.030303
%     vint95                         44.307     17.473      2.5357      0.011529
%     vint96                         11.853     12.305     0.96325       0.33589
%     vint97                         23.546     11.722      2.0087      0.045113
%     vint98                        0.34495     15.817    0.021809       0.98261
%     vint99                        -29.661     4.9142     -6.0358    3.1087e-09
%     vint00                        -24.965     2.6319     -9.4858             0
%     vint01                        -19.053     3.1324     -6.0827    2.3701e-09
%     vint02                        -18.863     2.9232      -6.453    2.6197e-10
%     vint03                        -20.532     4.0473      -5.073    5.5505e-07
%     vint04                        -22.558     3.0902        -7.3    1.1582e-12
%     vint05                        -15.411      5.956     -2.5876     0.0099499
%     vint06                         -17.49     2.9171     -5.9957    3.9145e-09
%     vint07                         -12.39     3.5016     -3.5385    0.00044042
%     vint08                        -12.984     3.6824     -3.5259    0.00046141
%     vint09                        -7.1512     4.9314     -1.4501       0.14766
%     vint10                        -9.4805     3.7388     -2.5357      0.011528
% 
% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_norm_irr_t1_u = corr(FOF_C_Venture_IRR.norm_irr_t1,u)
corr_log_size_t_u = corr(FOF_C_Venture_IRR.log_size_t,u)
corr_seq_num_t_u = corr(FOF_C_Venture_IRR.seq_num_t,u)
corr_norm_fundgrowtht1t_u = corr(FOF_C_Venture_IRR.norm_fundgrowtht1t,u)

%output:
% corr_norm_irr_t1_u = 7.9986e-17
% corr_log_size_t_u =  7.7534e-17
% corr_seq_num_t_u =  -4.6737e-17
% corr_norm_fundgrowtht1t_u = 3.6660e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 6.2985
% kurt_jbtest = 73.1681
% test_stat_jbtest = 1.0503e+05
% crit_val_jbtest = 40.6465
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_C_Venture_IRR.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_C_Venture_IRR(regFOF_C_Venture_IRR.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 8 funds with crazy IRRs. No creteria to exclude them 


 
% FOF C BUYOUT MULT
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (C) buyout_t_t-1 MULT','A2:AM717');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_C_Buyout_MULT = table;

% Allocate imported array to column variable names
FOF_C_Buyout_MULT.number = data(:,1);
FOF_C_Buyout_MULT.FUNDID = data(:,2);
FOF_C_Buyout_MULT.FIRMID = data(:,3);
FOF_C_Buyout_MULT.teamID = stringVectors(:,1);
FOF_C_Buyout_MULT.teamIDvintage = stringVectors(:,2);
FOF_C_Buyout_MULT.teamIDvintagenumber = stringVectors(:,3);
FOF_C_Buyout_MULT.vint_t = data(:,4);
FOF_C_Buyout_MULT.vint_t1 = data(:,5);
FOF_C_Buyout_MULT.mult_t = data(:,6);
FOF_C_Buyout_MULT.mult_t1 = data(:,7);
FOF_C_Buyout_MULT.STATUS = categorical(stringVectors(:,4));
FOF_C_Buyout_MULT.FUNDMANAGER = stringVectors(:,5);
FOF_C_Buyout_MULT.size_t = data(:,8);
FOF_C_Buyout_MULT.size_t1 = data(:,9);
FOF_C_Buyout_MULT.seq_num_t = data(:,10);
FOF_C_Buyout_MULT.seq_num_t1 = data(:,11);
FOF_C_Buyout_MULT.fundgrowtht1t = data(:,12);
FOF_C_Buyout_MULT.FundGrowthPerformance = data(:,13);
FOF_C_Buyout_MULT.vint90 = data(:,14);
FOF_C_Buyout_MULT.vint91 = data(:,15);
FOF_C_Buyout_MULT.vint92 = data(:,16);
FOF_C_Buyout_MULT.vint93 = data(:,17);
FOF_C_Buyout_MULT.vint94 = data(:,18);
FOF_C_Buyout_MULT.vint95 = data(:,19);
FOF_C_Buyout_MULT.vint96 = data(:,20);
FOF_C_Buyout_MULT.vint97 = data(:,21);
FOF_C_Buyout_MULT.vint98 = data(:,22);
FOF_C_Buyout_MULT.vint99 = data(:,23);
FOF_C_Buyout_MULT.vint00 = data(:,24);
FOF_C_Buyout_MULT.vint01 = data(:,25);
FOF_C_Buyout_MULT.vint02 = data(:,26);
FOF_C_Buyout_MULT.vint03 = data(:,27);
FOF_C_Buyout_MULT.vint04 = data(:,28);
FOF_C_Buyout_MULT.vint05 = data(:,29);
FOF_C_Buyout_MULT.vint06 = data(:,30);
FOF_C_Buyout_MULT.vint07 = data(:,31);
FOF_C_Buyout_MULT.vint08 = data(:,32);
FOF_C_Buyout_MULT.vint09 = data(:,33);
FOF_C_Buyout_MULT.vint10 = data(:,34);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

FOF_C_Buyout_MULT.log_size_t = log(FOF_C_Buyout_MULT.size_t);

% Normalize  independent variables 
% ("Independent variables are standardized with a mean 0 and a standard deviation 1 
% to reduce the possible multi- collinearity problem and to ease the
% interpretation of the interaction term")

FOF_C_Buyout_MULT.norm_fundgrowtht1t = zscore(FOF_C_Buyout_MULT.fundgrowtht1t);
FOF_C_Buyout_MULT.norm_FundGrowthPerformance = zscore(FOF_C_Buyout_MULT.FundGrowthPerformance);
FOF_C_Buyout_MULT.norm_mult_t1 = zscore(FOF_C_Buyout_MULT.mult_t1);

% Linear regression reg_C_Buyout_MULT
% 
regFOF_C_Buyout_MULT = fitlm(FOF_C_Buyout_MULT, 'mult_t ~ norm_mult_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_FundGrowthPerformance + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')

% Linear regression model:
%     mult_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                                   Estimate        SE        tStat        pValue  
%                                   _________    ________    ________    __________
% 
%     (Intercept)                      3.4951     0.71747      4.8715    1.3746e-06
%     seq_num_t                     0.0048222    0.018124     0.26607       0.79026
%     vint92                          -1.6612     0.81909     -2.0281      0.042933
%     vint93                          0.48295     0.86683     0.55715       0.57761
%     vint94                         -0.42657     0.75783    -0.56289        0.5737
%     vint95                          -1.6835     0.73296     -2.2968       0.02193
%     vint96                          -1.1529     0.73375     -1.5712       0.11659
%     vint97                          -1.9122     0.72152     -2.6503     0.0082265
%     vint98                          -1.7534     0.71955     -2.4368      0.015068
%     vint99                          -1.6691      0.7198     -2.3189      0.020691
%     vint00                          -1.2285     0.71758      -1.712      0.087345
%     vint01                          -1.0773     0.72476     -1.4864       0.13764
%     vint02                          -1.3125     0.72474      -1.811      0.070575
%     vint03                          -1.4343     0.72151     -1.9879       0.04722
%     vint04                          -1.4005     0.71986     -1.9455      0.052122
%     vint05                          -1.5796     0.71454     -2.2107      0.027385
%     vint06                          -1.6254     0.71545     -2.2718      0.023402
%     vint07                          -1.4737     0.71522     -2.0605      0.039722
%     vint08                          -1.4092      0.7167     -1.9663      0.049668
%     vint09                          -1.3625     0.72061     -1.8908      0.059066
%     vint10                          -1.4006     0.71906     -1.9478      0.051839
%     log_size_t                    -0.039234    0.022238     -1.7643      0.078121
%     norm_fundgrowtht1t            -0.075294    0.061712     -1.2201       0.22284
%     norm_FundGrowthPerformance     0.091065    0.064206      1.4183       0.15655
%     norm_mult_t1                    0.15688    0.031626      4.9604    8.8646e-07
% 
% 
% Number of observations: 716, Error degrees of freedom: 691
% Root Mean Squared Error: 0.708
% R-squared: 0.167,  Adjusted R-Squared 0.138
% F-statistic vs. constant model: 5.76, p-value = 3.5e-16
% 
% 

% DIAGNOSTICS of CLRM assumptions

T = regFOF_C_Buyout_MULT.NumObservations;
k = regFOF_C_Buyout_MULT.NumEstimatedCoefficients;
u = regFOF_C_Buyout_MULT.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_C_Buyout_MULT.Residuals.Raw);
Stdev_u = std(regFOF_C_Buyout_MULT.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -8.3507e-17

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9634

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_C_Buyout_MULT.uRaw = u; 
FOF_C_Buyout_MULT.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_C_Buyout_MULT_White = table(FOF_C_Buyout_MULT.mult_t, FOF_C_Buyout_MULT.norm_mult_t1, FOF_C_Buyout_MULT.log_size_t,...
    FOF_C_Buyout_MULT.seq_num_t, FOF_C_Buyout_MULT.norm_fundgrowtht1t, FOF_C_Buyout_MULT.norm_FundGrowthPerformance, ...
    FOF_C_Buyout_MULT.vint92, FOF_C_Buyout_MULT.vint93, FOF_C_Buyout_MULT.vint94,...
    FOF_C_Buyout_MULT.vint95, FOF_C_Buyout_MULT.vint96, FOF_C_Buyout_MULT.vint97, FOF_C_Buyout_MULT.vint98, FOF_C_Buyout_MULT.vint99,...
    FOF_C_Buyout_MULT.vint00, FOF_C_Buyout_MULT.vint01, FOF_C_Buyout_MULT.vint02, FOF_C_Buyout_MULT.vint03, FOF_C_Buyout_MULT.vint04,...
    FOF_C_Buyout_MULT.vint05, FOF_C_Buyout_MULT.vint06, FOF_C_Buyout_MULT.vint07, FOF_C_Buyout_MULT.vint08, FOF_C_Buyout_MULT.vint09, FOF_C_Buyout_MULT.vint10,...
    FOF_C_Buyout_MULT.norm_mult_t1.^2, FOF_C_Buyout_MULT.log_size_t.^2, FOF_C_Buyout_MULT.norm_fundgrowtht1t.^2, FOF_C_Buyout_MULT.norm_FundGrowthPerformance.^2, FOF_C_Buyout_MULT.uRaw2);

 
FOF_C_Buyout_MULT_White.Properties.VariableNames={'mult_t','norm_mult_t1','log_size_t','seq_num_t','norm_fundgrowtht1t','norm_FundGrowthPerformance',...
    'vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'norm_mult_t1sq','log_size_tsq','norm_fundgrowtht1tsq','norm_FundGrowthPerformancesq','uRaw2'};

% Unrestricted regression
regFOF_C_Buyout_MULT_White=fitlm(FOF_C_Buyout_MULT_White,'uRaw2 ~ norm_mult_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_FundGrowthPerformance + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + norm_mult_t1sq + log_size_tsq + norm_fundgrowtht1tsq + norm_FundGrowthPerformancesq')

% regFOF_C_Buyout_MULT_White = 
% 
% 
% Linear regression model:
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 29 terms in 28 predictors]
% 
% Estimated Coefficients:
%                                      Estimate        SE         tStat        pValue  
%                                     __________    ________    _________    __________
% 
%     (Intercept)                           6.79      2.0467       3.3176    0.00095593
%     norm_mult_t1                      -0.26202    0.097879       -2.677     0.0076056
%     log_size_t                         -2.0992     0.36303      -5.7825    1.1173e-08
%     seq_num_t                       -0.0039328     0.04547    -0.086491        0.9311
%     norm_fundgrowtht1t                -0.39811     0.28879      -1.3786       0.16847
%     norm_FundGrowthPerformance         0.81748     0.32501       2.5153      0.012121
%     vint92                              1.0135      1.9764      0.51279       0.60826
%     vint93                             0.67481      2.0899       0.3229       0.74687
%     vint94                              2.7485      1.8273       1.5041       0.13301
%     vint95                              1.0298      1.7669       0.5828       0.56022
%     vint96                              3.8777      1.7687       2.1923       0.02869
%     vint97                             0.81885      1.7392      0.47081       0.63793
%     vint98                              1.0509      1.7351      0.60571       0.54491
%     vint99                              1.0578      1.7359      0.60938       0.54247
%     vint00                             0.74018      1.7305      0.42773       0.66898
%     vint01                              1.0556      1.7477      0.60404       0.54602
%     vint02                              1.1338      1.7473      0.64888       0.51663
%     vint03                             0.72815      1.7391      0.41868       0.67558
%     vint04                             0.98643      1.7357      0.56832          0.57
%     vint05                              1.2712       1.723      0.73781       0.46088
%     vint06                             0.95272       1.725      0.55229       0.58093
%     vint07                             0.90423      1.7238      0.52457       0.60005
%     vint08                             0.88555      1.7278      0.51254       0.60844
%     vint09                             0.84162      1.7381      0.48423       0.62838
%     vint10                              1.4471      1.7344      0.83433       0.40439
%     norm_mult_t1sq                    0.025972    0.026311       0.9871       0.32394
%     log_size_tsq                       0.14297    0.028094       5.0892    4.6472e-07
%     norm_fundgrowtht1tsq              0.012388    0.021075      0.58781       0.55685
%     norm_FundGrowthPerformancesq     -0.050297    0.022023      -2.2839      0.022683
% 
% 
% Number of observations: 716, Error degrees of freedom: 687
% Root Mean Squared Error: 1.7
% R-squared: 0.152,  Adjusted R-Squared 0.117
% F-statistic vs. constant model: 4.39, p-value = 1.58e-12
% 


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_C_Buyout_MULT_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  108.6098, Crit_Val_e = 36.4150, P_e = 9.8543e-13,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_C_Buyout_MULT.norm_mult_t1 FOF_C_Buyout_MULT.log_size_t FOF_C_Buyout_MULT.seq_num_t FOF_C_Buyout_MULT.norm_fundgrowtht1t FOF_C_Buyout_MULT.norm_FundGrowthPerformance ...
    FOF_C_Buyout_MULT.vint92 FOF_C_Buyout_MULT.vint93 FOF_C_Buyout_MULT.vint94 ...
    FOF_C_Buyout_MULT.vint95 FOF_C_Buyout_MULT.vint96 FOF_C_Buyout_MULT.vint97 FOF_C_Buyout_MULT.vint98 FOF_C_Buyout_MULT.vint99 ...
    FOF_C_Buyout_MULT.vint00 FOF_C_Buyout_MULT.vint01 FOF_C_Buyout_MULT.vint02 FOF_C_Buyout_MULT.vint03 FOF_C_Buyout_MULT.vint04 ...
    FOF_C_Buyout_MULT.vint05 FOF_C_Buyout_MULT.vint06 FOF_C_Buyout_MULT.vint07 FOF_C_Buyout_MULT.vint08 FOF_C_Buyout_MULT.vint09 FOF_C_Buyout_MULT.vint10];

y = FOF_C_Buyout_MULT.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','norm_mult_t1','log_size_t','seq_num_t','norm_fundgrowtht1t','norm_FundGrowthPerformance',...
    'vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White


% White =

%   25×4 table
% 
%                                     beta          SE        tstat          p     
%                                   _________    ________    ________    __________
% 
%     (Intercept)                      3.4951     0.16977      20.588             0
%     norm_mult_t1                    0.15688    0.042697      3.6742    0.00025647
%     log_size_t                    -0.039234    0.027648     -1.4191       0.15632
%     seq_num_t                     0.0048222    0.018177     0.26529       0.79086
%     norm_fundgrowtht1t            -0.075294    0.044964     -1.6745      0.094462
%     norm_FundGrowthPerformance     0.091065    0.057597      1.5811       0.11431
%     vint92                          -1.6612      0.2283     -7.2763    9.0572e-13
%     vint93                          0.48295     0.26142      1.8474      0.065104
%     vint94                         -0.42657     0.54188    -0.78721       0.43142
%     vint95                          -1.6835     0.22058      -7.632    7.4163e-14
%     vint96                          -1.1529     0.42575     -2.7079     0.0069336
%     vint97                          -1.9122     0.11202     -17.071             0
%     vint98                          -1.7534     0.11191     -15.669             0
%     vint99                          -1.6691       0.122     -13.682             0
%     vint00                          -1.2285     0.10909     -11.262             0
%     vint01                          -1.0773      0.1585     -6.7967    2.2603e-11
%     vint02                          -1.3125     0.16566     -7.9229    8.8818e-15
%     vint03                          -1.4343     0.11962      -11.99             0
%     vint04                          -1.4005     0.12573     -11.139             0
%     vint05                          -1.5796    0.093127     -16.962             0
%     vint06                          -1.6254    0.088601     -18.345             0
%     vint07                          -1.4737    0.095831     -15.378             0
%     vint08                          -1.4092    0.098145     -14.359             0
%     vint09                          -1.3625     0.11503     -11.845             0
%     vint10                          -1.4006     0.14263     -9.8199             0
% 

% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_norm_mult_t1_u = corr(FOF_C_Buyout_MULT.norm_mult_t1,u)
corr_log_size_t_u = corr(FOF_C_Buyout_MULT.log_size_t,u)
corr_seq_num_t_u = corr(FOF_C_Buyout_MULT.seq_num_t,u)
corr_norm_fundgrowtht1t_u = corr(FOF_C_Buyout_MULT.norm_fundgrowtht1t,u)

%output:
% corr_norm_mult_t1_u = 2.9689e-16
% corr_log_size_t_u = 4.2133e-16
% corr_seq_num_t_u = 2.6622e-16
% corr_norm_fundgrowtht1t_u = 1.3550e-16

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 1.8245
% kurt_jbtest = 15.0410
% test_stat_jbtest = 4.7227e+03
% crit_val_jbtest = 39.3641
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_C_Buyout_MULT.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_C_Buyout_MULT(regFOF_C_Buyout_MULT.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 4 funds. No criteria to exclude them 


 % FOF C BUYOUT IRR
 clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (C) buyout_t_t-1 IRR','A2:AM629');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]);
% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_C_Buyout_IRR = table;

% Allocate imported array to column variable names
FOF_C_Buyout_IRR.number = data(:,1);
FOF_C_Buyout_IRR.FUNDID = data(:,2);
FOF_C_Buyout_IRR.FIRMID = data(:,3);
FOF_C_Buyout_IRR.teamID = categorical(stringVectors(:,1));
FOF_C_Buyout_IRR.teamIDvintage = stringVectors(:,2);
FOF_C_Buyout_IRR.teamIDvintagenumber = stringVectors(:,3);
FOF_C_Buyout_IRR.vint_t = data(:,4);
FOF_C_Buyout_IRR.vint_t1 = data(:,5);
FOF_C_Buyout_IRR.irr_t = data(:,6);
FOF_C_Buyout_IRR.irr_t1 = data(:,7);
FOF_C_Buyout_IRR.STATUS = categorical(stringVectors(:,4));
FOF_C_Buyout_IRR.FUNDMANAGER = categorical(stringVectors(:,5));
FOF_C_Buyout_IRR.size_t = data(:,8);
FOF_C_Buyout_IRR.size_t1 = data(:,9);
FOF_C_Buyout_IRR.seq_num_t = data(:,10);
FOF_C_Buyout_IRR.seq_num_t1 = data(:,11);
FOF_C_Buyout_IRR.fundgrowtht1t = data(:,12);
FOF_C_Buyout_IRR.FundGrowthPerformance = data(:,13);
FOF_C_Buyout_IRR.vint90 = data(:,14);
FOF_C_Buyout_IRR.vint91 = data(:,15);
FOF_C_Buyout_IRR.vint92 = data(:,16);
FOF_C_Buyout_IRR.vint93 = data(:,17);
FOF_C_Buyout_IRR.vint94 = data(:,18);
FOF_C_Buyout_IRR.vint95 = data(:,19);
FOF_C_Buyout_IRR.vint96 = data(:,20);
FOF_C_Buyout_IRR.vint97 = data(:,21);
FOF_C_Buyout_IRR.vint98 = data(:,22);
FOF_C_Buyout_IRR.vint99 = data(:,23);
FOF_C_Buyout_IRR.vint00 = data(:,24);
FOF_C_Buyout_IRR.vint01 = data(:,25);
FOF_C_Buyout_IRR.vint02 = data(:,26);
FOF_C_Buyout_IRR.vint03 = data(:,27);
FOF_C_Buyout_IRR.vint04 = data(:,28);
FOF_C_Buyout_IRR.vint05 = data(:,29);
FOF_C_Buyout_IRR.vint06 = data(:,30);
FOF_C_Buyout_IRR.vint07 = data(:,31);
FOF_C_Buyout_IRR.vint08 = data(:,32);
FOF_C_Buyout_IRR.vint09 = data(:,33);
FOF_C_Buyout_IRR.vint10 = data(:,34);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

FOF_C_Buyout_IRR.log_size_t = log(FOF_C_Buyout_IRR.size_t);

% Normalize  independent variables 
% ("Independent variables are standardized with a mean 0 and a standard deviation 1 
% to reduce the possible multi- collinearity problem and to ease the
% interpretation of the interaction term")

FOF_C_Buyout_IRR.norm_fundgrowtht1t = zscore(FOF_C_Buyout_IRR.fundgrowtht1t);
FOF_C_Buyout_IRR.norm_FundGrowthPerformance = zscore(FOF_C_Buyout_IRR.FundGrowthPerformance);
FOF_C_Buyout_IRR.norm_irr_t1 = zscore(FOF_C_Buyout_IRR.irr_t1);

% Linear regression reg_C_Buyout_IRR
% 
regFOF_C_Buyout_IRR = fitlm(FOF_C_Buyout_IRR, 'irr_t ~ norm_irr_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_FundGrowthPerformance + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')


% regFOF_C_Buyout_IRR = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                                   Estimate       SE         tStat        pValue  
%                                   _________    _______    _________    __________
% 
%     (Intercept)                      52.688     14.982       3.5168    0.00046963
%     seq_num_t                       0.77978    0.45389        1.718      0.086309
%     vint92                          -30.472       18.1      -1.6836      0.092782
%     vint93                           15.432     18.066       0.8542       0.39334
%     vint94                          -20.466     15.797      -1.2955       0.19564
%     vint95                           -27.37     15.295      -1.7895      0.074037
%     vint96                          -23.762     15.327      -1.5503       0.12158
%     vint97                          -39.703     15.054      -2.6374     0.0085691
%     vint98                          -42.668     15.012      -2.8423     0.0046304
%     vint99                           -35.27     15.033      -2.3462      0.019289
%     vint00                          -27.822     14.969      -1.8586      0.063568
%     vint01                          -17.136      15.11      -1.1341       0.25721
%     vint02                          -25.365     15.106      -1.6792      0.093641
%     vint03                          -27.189     15.057      -1.8057      0.071456
%     vint04                          -30.194     15.055      -2.0056       0.04535
%     vint05                          -33.693       14.9      -2.2613      0.024095
%     vint06                          -37.359     14.922      -2.5036      0.012555
%     vint07                          -34.939     14.922      -2.3415      0.019533
%     vint08                          -32.279     14.962      -2.1574      0.031366
%     vint09                          -29.583     15.081      -1.9615      0.050277
%     vint10                          -32.408      15.01       -2.159      0.031241
%     log_size_t                      -1.2604     0.5073      -2.4844      0.013245
%     norm_fundgrowtht1t            -0.036801    0.82838    -0.044425       0.96458
%     norm_FundGrowthPerformance       0.7091    0.89459      0.79266       0.42829
%     norm_irr_t1                      3.1824    0.69703       4.5657    6.0366e-06
% 
% 
% Number of observations: 628, Error degrees of freedom: 603
% Root Mean Squared Error: 14.7
% R-squared: 0.213,  Adjusted R-Squared 0.182
% F-statistic vs. constant model: 6.81, p-value = 1.09e-19

% DIAGNOSTICS of CLRM assumptions

T = regFOF_C_Buyout_IRR.NumObservations;
k = regFOF_C_Buyout_IRR.NumEstimatedCoefficients;
u = regFOF_C_Buyout_IRR.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_C_Buyout_IRR.Residuals.Raw);
Stdev_u = std(regFOF_C_Buyout_IRR.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =  -4.1568e-14
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9639

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_C_Buyout_IRR.uRaw = u; 
FOF_C_Buyout_IRR.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_C_Buyout_IRR_White = table(FOF_C_Buyout_IRR.irr_t, FOF_C_Buyout_IRR.norm_irr_t1, FOF_C_Buyout_IRR.log_size_t,...
    FOF_C_Buyout_IRR.seq_num_t, FOF_C_Buyout_IRR.norm_fundgrowtht1t, FOF_C_Buyout_IRR.norm_FundGrowthPerformance, ...
    FOF_C_Buyout_IRR.vint92, FOF_C_Buyout_IRR.vint93, FOF_C_Buyout_IRR.vint94,...
    FOF_C_Buyout_IRR.vint95, FOF_C_Buyout_IRR.vint96, FOF_C_Buyout_IRR.vint97, FOF_C_Buyout_IRR.vint98, FOF_C_Buyout_IRR.vint99,...
    FOF_C_Buyout_IRR.vint00, FOF_C_Buyout_IRR.vint01, FOF_C_Buyout_IRR.vint02, FOF_C_Buyout_IRR.vint03, FOF_C_Buyout_IRR.vint04,...
    FOF_C_Buyout_IRR.vint05, FOF_C_Buyout_IRR.vint06, FOF_C_Buyout_IRR.vint07, FOF_C_Buyout_IRR.vint08, FOF_C_Buyout_IRR.vint09, FOF_C_Buyout_IRR.vint10,...
    FOF_C_Buyout_IRR.norm_irr_t1.^2, FOF_C_Buyout_IRR.log_size_t.^2, FOF_C_Buyout_IRR.norm_fundgrowtht1t.^2, FOF_C_Buyout_IRR.norm_FundGrowthPerformance.^2, FOF_C_Buyout_IRR.uRaw2);

 
FOF_C_Buyout_IRR_White.Properties.VariableNames={'irr_t','norm_irr_t1','log_size_t','seq_num_t','norm_fundgrowtht1t','norm_FundGrowthPerformance',...
    'vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'norm_irr_t1sq','log_size_tsq','norm_fundgrowtht1tsq','norm_FundGrowthPerformancesq','uRaw2'};

% Unrestricted regression
regFOF_C_Buyout_IRR_White=fitlm(FOF_C_Buyout_IRR_White,'uRaw2 ~ norm_irr_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_FundGrowthPerformance + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + norm_irr_t1sq + log_size_tsq + norm_fundgrowtht1tsq + norm_FundGrowthPerformancesq')

% regFOF_C_Buyout_IRR_White = 
%
% Linear regression model:
%     uRaw2 ~ [Linear formula with 29 terms in 28 predictors]
% 
% Estimated Coefficients:
%                                     Estimate      SE       tStat       pValue  
%                                     ________    ______    ________    _________
% 
%     (Intercept)                      1874.1        915      2.0482      0.04098
%     norm_irr_t1                     -130.62     49.708     -2.6278    0.0088129
%     log_size_t                      -586.57     176.02     -3.3325    0.0009137
%     seq_num_t                        46.686     23.747       1.966     0.049764
%     norm_fundgrowtht1t               11.884     80.871     0.14695      0.88322
%     norm_FundGrowthPerformance       239.87     95.988      2.4989     0.012724
%     vint92                           514.92     898.83     0.57288      0.56694
%     vint93                           359.74     895.46     0.40174      0.68802
%     vint94                           536.27     783.09     0.68482      0.49372
%     vint95                            465.8     758.09     0.61443      0.53916
%     vint96                           1699.2     759.52      2.2373     0.025636
%     vint97                           439.43     746.09     0.58898       0.5561
%     vint98                           597.51     744.63     0.80243      0.42262
%     vint99                           345.88     745.99     0.46365      0.64307
%     vint00                           204.78     742.67     0.27573      0.78285
%     vint01                           533.33     749.78     0.71131      0.47717
%     vint02                           429.54     749.28     0.57327      0.56668
%     vint03                           346.94     746.47     0.46477      0.64226
%     vint04                           450.89     746.54     0.60396       0.5461
%     vint05                           418.59      738.9      0.5665      0.57127
%     vint06                           273.94     739.97     0.37021      0.71136
%     vint07                           188.93     740.14     0.25527       0.7986
%     vint08                           280.86     742.05      0.3785      0.70519
%     vint09                            254.3     748.73     0.33964      0.73424
%     vint10                           265.38     744.94     0.35624      0.72179
%     norm_irr_t1sq                    12.708     6.5136      1.9509     0.051531
%     log_size_tsq                      37.07     13.644       2.717    0.0067778
%     norm_fundgrowtht1tsq            -5.5958     8.3589    -0.66944      0.50348
%     norm_FundGrowthPerformancesq    -10.839     5.2679     -2.0576     0.040059
% 
% 
% Number of observations: 628, Error degrees of freedom: 599
% Root Mean Squared Error: 730
% R-squared: 0.153,  Adjusted R-Squared 0.113
% F-statistic vs. constant model: 3.85, p-value = 3.12e-10

% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_C_Buyout_IRR_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  95.8273, Crit_Val_e = 36.4150, P_e = 1.5320e-10,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_C_Buyout_IRR.norm_irr_t1 FOF_C_Buyout_IRR.log_size_t FOF_C_Buyout_IRR.seq_num_t FOF_C_Buyout_IRR.norm_fundgrowtht1t FOF_C_Buyout_IRR.norm_FundGrowthPerformance ...
    FOF_C_Buyout_IRR.vint92 FOF_C_Buyout_IRR.vint93 FOF_C_Buyout_IRR.vint94 ...
    FOF_C_Buyout_IRR.vint95 FOF_C_Buyout_IRR.vint96 FOF_C_Buyout_IRR.vint97 FOF_C_Buyout_IRR.vint98 FOF_C_Buyout_IRR.vint99 ...
    FOF_C_Buyout_IRR.vint00 FOF_C_Buyout_IRR.vint01 FOF_C_Buyout_IRR.vint02 FOF_C_Buyout_IRR.vint03 FOF_C_Buyout_IRR.vint04 ...
    FOF_C_Buyout_IRR.vint05 FOF_C_Buyout_IRR.vint06 FOF_C_Buyout_IRR.vint07 FOF_C_Buyout_IRR.vint08 FOF_C_Buyout_IRR.vint09 FOF_C_Buyout_IRR.vint10];

y = FOF_C_Buyout_IRR.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','norm_irr_t1','log_size_t','seq_num_t','norm_fundgrowtht1t','norm_FundGrowthPerformance',...
    'vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White


% % 
% White =
% 
%   25×4 table
% 
%                                     beta         SE         tstat          p     
%                                   _________    _______    _________    __________
% 
%     (Intercept)                      52.688     3.0149       17.476             0
%     norm_irr_t1                      3.1824     1.0016       3.1774     0.0015594
%     log_size_t                      -1.2604    0.58728      -2.1461      0.032249
%     seq_num_t                       0.77978    0.61042       1.2774       0.20192
%     norm_fundgrowtht1t            -0.036801    0.73034    -0.050389       0.95983
%     norm_FundGrowthPerformance       0.7091    0.98654      0.71878       0.47255
%     vint92                          -30.472     2.6087      -11.681             0
%     vint93                           15.432     9.2406         1.67      0.095415
%     vint94                          -20.466     7.1803      -2.8502     0.0045125
%     vint95                           -27.37      4.833      -5.6632     2.265e-08
%     vint96                          -23.762      9.589       -2.478      0.013474
%     vint97                          -39.703     3.2967      -12.043             0
%     vint98                          -42.668      3.527      -12.097             0
%     vint99                           -35.27     2.8299      -12.463             0
%     vint00                          -27.822     2.2662      -12.277             0
%     vint01                          -17.136     4.3812      -3.9113    0.00010183
%     vint02                          -25.365     3.6373      -6.9738     7.863e-12
%     vint03                          -27.189     3.3635      -8.0836    3.3307e-15
%     vint04                          -30.194     3.8639      -7.8143    2.3537e-14
%     vint05                          -33.693     2.4735      -13.621             0
%     vint06                          -37.359     2.0504       -18.22             0
%     vint07                          -34.939     2.2569      -15.481             0
%     vint08                          -32.279     2.2504      -14.344             0
%     vint09                          -29.583     3.0797      -9.6056             0
%     vint10                          -32.408     2.6698      -12.139             0


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_norm_irr_t1_u = corr(FOF_C_Buyout_IRR.norm_irr_t1,u)
corr_log_size_t_u = corr(FOF_C_Buyout_IRR.log_size_t,u)
corr_seq_num_t_u = corr(FOF_C_Buyout_IRR.seq_num_t,u)
corr_norm_fundgrowtht1t_u = corr(FOF_C_Buyout_IRR.norm_fundgrowtht1t,u)

%output:
% corr_norm_irr_t1_u = -2.3428e-16
% corr_log_size_t_u =  -5.4391e-16
% corr_seq_num_t_u =  -1.4474e-16
% corr_norm_fundgrowtht1t_u = 1.0939e-16

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 0.9509
% kurt_jbtest =  14.7303
% test_stat_jbtest =  3.6952e+03
% crit_val_jbtest = 39.3641
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_C_Buyout_IRR.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_C_Buyout_IRR(regFOF_C_Buyout_IRR.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 9 funds. No creteria to exclude them 


 

%FOF C ALL MULT
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (C) all_t_t-1 MULT','A2:AQ1318');
stringVectors = string(raw(:,[4,5,6,9,13,14]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,10,11,12,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_C_All_MULT = table;

% Allocate imported array to column variable names
FOF_C_All_MULT.number = data(:,1);
FOF_C_All_MULT.FUNDID = data(:,2);
FOF_C_All_MULT.FIRMID = data(:,3);
FOF_C_All_MULT.teamID = categorical(stringVectors(:,1));
FOF_C_All_MULT.teamIDvintage = stringVectors(:,2);
FOF_C_All_MULT.teamIDvintagenumber = stringVectors(:,3);
FOF_C_All_MULT.vint_t = data(:,4);
FOF_C_All_MULT.vint_t1 = data(:,5);
FOF_C_All_MULT.STRATEGY = categorical(stringVectors(:,4));
FOF_C_All_MULT.BuyoutDummy = data(:,6);
FOF_C_All_MULT.mult_t = data(:,7);
FOF_C_All_MULT.mult_t1 = data(:,8);
FOF_C_All_MULT.STATUS = categorical(stringVectors(:,5));
FOF_C_All_MULT.FUNDMANAGER = categorical(stringVectors(:,6));
FOF_C_All_MULT.size_t = data(:,9);
FOF_C_All_MULT.size_t1 = data(:,10);
FOF_C_All_MULT.seq_num_t = data(:,11);
FOF_C_All_MULT.seq_num_t1 = data(:,12);
FOF_C_All_MULT.fundgrowtht1t = data(:,13);
FOF_C_All_MULT.PerformanceBuyout = data(:,14);
FOF_C_All_MULT.FundGrowthBuyout = data(:,15);
FOF_C_All_MULT.PerformanceFundGrowthBuyout = data(:,16);
FOF_C_All_MULT.FundGrowthPerformance = data(:,17);
FOF_C_All_MULT.vint90 = data(:,18);
FOF_C_All_MULT.vint91 = data(:,19);
FOF_C_All_MULT.vint92 = data(:,20);
FOF_C_All_MULT.vint93 = data(:,21);
FOF_C_All_MULT.vint94 = data(:,22);
FOF_C_All_MULT.vint95 = data(:,23);
FOF_C_All_MULT.vint96 = data(:,24);
FOF_C_All_MULT.vint97 = data(:,25);
FOF_C_All_MULT.vint98 = data(:,26);
FOF_C_All_MULT.vint99 = data(:,27);
FOF_C_All_MULT.vint00 = data(:,28);
FOF_C_All_MULT.vint01 = data(:,29);
FOF_C_All_MULT.vint02 = data(:,30);
FOF_C_All_MULT.vint03 = data(:,31);
FOF_C_All_MULT.vint04 = data(:,32);
FOF_C_All_MULT.vint05 = data(:,33);
FOF_C_All_MULT.vint06 = data(:,34);
FOF_C_All_MULT.vint07 = data(:,35);
FOF_C_All_MULT.vint08 = data(:,36);
FOF_C_All_MULT.vint09 = data(:,37);
FOF_C_All_MULT.vint10 = data(:,38);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)
FOF_C_All_MULT.log_size_t = log(FOF_C_All_MULT.size_t);
% Normalize  independent variables 
% ("Independent variables are standardized with a mean 0 and a standard deviation 1 
% to reduce the possible multi- collinearity problem and to ease the
% interpretation of the interaction term")

FOF_C_All_MULT.norm_fundgrowtht1t = zscore(FOF_C_All_MULT.fundgrowtht1t);
FOF_C_All_MULT.norm_FundGrowthPerformance = zscore(FOF_C_All_MULT.FundGrowthPerformance);
FOF_C_All_MULT.norm_mult_t1 = zscore(FOF_C_All_MULT.mult_t1);
FOF_C_All_MULT.norm_PerformanceBuyout = zscore(FOF_C_All_MULT.PerformanceBuyout);
FOF_C_All_MULT.norm_FundGrowthBuyout = zscore(FOF_C_All_MULT.FundGrowthBuyout);
FOF_C_All_MULT.norm_PerformanceFundGrowthBuyout = zscore(FOF_C_All_MULT.PerformanceFundGrowthBuyout);

% Linear regression reg_C_All_MULT
% 
regFOF_C_All_MULT = fitlm(FOF_C_All_MULT, 'mult_t ~ BuyoutDummy + norm_mult_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_PerformanceBuyout + norm_FundGrowthBuyout + norm_PerformanceFundGrowthBuyout + norm_FundGrowthPerformance + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')


% regFOF_C_All_MULT = 


% Linear regression model:
%     mult_t ~ [Linear formula with 30 terms in 29 predictors]
% 
% Estimated Coefficients:
%                                          Estimate        SE         tStat         pValue  
%                                         __________    ________    __________    __________
% 
%     (Intercept)                             1.7653      1.1588        1.5233       0.12792
%     BuyoutDummy                            0.26937     0.20278        1.3284       0.18428
%     seq_num_t                            -0.026231     0.02449       -1.0711       0.28433
%     vint91                                  0.2975      1.4785       0.20122       0.84056
%     vint92                               -0.054931      1.2802     -0.042909       0.96578
%     vint93                                 0.94659       1.405       0.67374        0.5006
%     vint94                                  3.4249      1.2069        2.8377     0.0046159
%     vint95                                  1.7258      1.1939        1.4455       0.14856
%     vint96                                  1.4908      1.1848        1.2582       0.20853
%     vint97                                0.088347      1.1637       0.07592       0.93949
%     vint98                                 -0.2991      1.1603      -0.25779       0.79661
%     vint99                                 -0.6778      1.1586        -0.585       0.55865
%     vint00                                -0.35334      1.1548      -0.30598       0.75967
%     vint01                                -0.13634      1.1613       -0.1174       0.90656
%     vint02                                -0.15925      1.1681      -0.13634       0.89158
%     vint03                                -0.15651      1.1676      -0.13405       0.89339
%     vint04                                -0.12575      1.1659      -0.10786       0.91413
%     vint05                                -0.12419      1.1567      -0.10737       0.91451
%     vint06                                -0.16107      1.1557      -0.13937       0.88918
%     vint07                               -0.020764      1.1557     -0.017966       0.98567
%     vint08                                 0.18571      1.1567       0.16055       0.87248
%     vint09                                0.024887       1.166      0.021343       0.98298
%     vint10                               0.0002795      1.1639    0.00024014       0.99981
%     log_size_t                          -0.0070283    0.037258      -0.18864        0.8504
%     norm_fundgrowtht1t                   -0.032423     0.08024      -0.40407       0.68623
%     norm_FundGrowthPerformance            -0.26315    0.079091       -3.3272     0.0009019
%     norm_mult_t1                           0.46154    0.057322        8.0516    1.8444e-15
%     norm_PerformanceBuyout                -0.16757     0.10073       -1.6635      0.096462
%     norm_FundGrowthBuyout                -0.042227     0.12541      -0.33672       0.73639
%     norm_PerformanceFundGrowthBuyout       0.24167     0.12293        1.9659      0.049525
% 
% 
% Number of observations: 1317, Error degrees of freedom: 1287
% Root Mean Squared Error: 1.62
% R-squared: 0.172,  Adjusted R-Squared 0.154
% F-statistic vs. constant model: 9.24, p-value = 3.16e-36
% 
% 

% DIAGNOSTICS of CLRM assumptions

T = regFOF_C_All_MULT.NumObservations;
k = regFOF_C_All_MULT.NumEstimatedCoefficients;
u = regFOF_C_All_MULT.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_C_All_MULT.Residuals.Raw);
Stdev_u = std(regFOF_C_All_MULT.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   2.7891e-14

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9618

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_C_All_MULT.uRaw = u; 
FOF_C_All_MULT.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_C_All_MULT_White = table(FOF_C_All_MULT.mult_t, FOF_C_All_MULT.BuyoutDummy, FOF_C_All_MULT.norm_mult_t1, FOF_C_All_MULT.log_size_t,...
    FOF_C_All_MULT.seq_num_t, FOF_C_All_MULT.norm_fundgrowtht1t,FOF_C_All_MULT.norm_PerformanceBuyout, FOF_C_All_MULT.norm_FundGrowthBuyout,...
    FOF_C_All_MULT.norm_PerformanceFundGrowthBuyout, FOF_C_All_MULT.norm_FundGrowthPerformance, ...
    FOF_C_All_MULT.vint91, FOF_C_All_MULT.vint92, FOF_C_All_MULT.vint93, FOF_C_All_MULT.vint94,...
    FOF_C_All_MULT.vint95, FOF_C_All_MULT.vint96, FOF_C_All_MULT.vint97, FOF_C_All_MULT.vint98, FOF_C_All_MULT.vint99,...
    FOF_C_All_MULT.vint00, FOF_C_All_MULT.vint01, FOF_C_All_MULT.vint02, FOF_C_All_MULT.vint03, FOF_C_All_MULT.vint04,...
    FOF_C_All_MULT.vint05, FOF_C_All_MULT.vint06, FOF_C_All_MULT.vint07, FOF_C_All_MULT.vint08, FOF_C_All_MULT.vint09, FOF_C_All_MULT.vint10,...
    FOF_C_All_MULT.norm_mult_t1.^2, FOF_C_All_MULT.log_size_t.^2, FOF_C_All_MULT.norm_fundgrowtht1t.^2, FOF_C_All_MULT.norm_PerformanceBuyout.^2,...
    FOF_C_All_MULT.norm_FundGrowthBuyout.^2, FOF_C_All_MULT.norm_PerformanceFundGrowthBuyout.^2, FOF_C_All_MULT.norm_FundGrowthPerformance.^2, FOF_C_All_MULT.uRaw2);

 
FOF_C_All_MULT_White.Properties.VariableNames={'mult_t','BuyoutDummy','norm_mult_t1','log_size_t','seq_num_t',...
    'norm_fundgrowtht1t','norm_PerformanceBuyout','norm_FundGrowthBuyout','norm_PerformanceFundGrowthBuyout','norm_FundGrowthPerformance',...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'norm_mult_t1sq','log_size_tsq','norm_fundgrowtht1tsq','norm_PerformanceBuyoutsq','norm_FundGrowthBuyoutsq',...
    'norm_PerformanceFundGrowthBuyoutsq','norm_FundGrowthPerformancesq','uRaw2'};

% Unrestricted regression
regFOF_C_All_MULT_White=fitlm(FOF_C_All_MULT_White,'uRaw2 ~ BuyoutDummy + norm_mult_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_PerformanceBuyout + norm_FundGrowthBuyout + norm_PerformanceFundGrowthBuyout + norm_FundGrowthPerformance + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + norm_mult_t1sq + log_size_tsq + norm_fundgrowtht1tsq + norm_PerformanceBuyoutsq + norm_FundGrowthBuyoutsq + norm_PerformanceFundGrowthBuyoutsq + norm_FundGrowthPerformancesq')

% regFOF_C_All_MULT_White = 


% Linear regression model:
%     uRaw2 ~ [Linear formula with 37 terms in 36 predictors]
% 
% Estimated Coefficients:
%                                           Estimate       SE         tStat         pValue  
%                                           _________    _______    __________    __________
% 
%     (Intercept)                              -6.585     18.881      -0.34876       0.72733
%     BuyoutDummy                              6.6045      4.588        1.4395       0.15025
%     norm_mult_t1                             9.7854     1.7094        5.7246    1.2905e-08
%     log_size_t                            0.0018027     2.4921    0.00072336       0.99942
%     seq_num_t                              -0.26906    0.37698      -0.71371       0.47554
%     norm_fundgrowtht1t                       1.1112     2.5911       0.42887       0.66809
%     norm_PerformanceBuyout                  -7.0743     2.7092       -2.6112     0.0091279
%     norm_FundGrowthBuyout                   -1.8085     3.8835       -0.4657       0.64151
%     norm_PerformanceFundGrowthBuyout         4.9802     4.2115        1.1825       0.23722
%     norm_FundGrowthPerformance              -6.1863     3.2126       -1.9256      0.054372
%     vint91                                   6.0061     22.323       0.26905       0.78793
%     vint92                                   2.7295      19.33       0.14121       0.88773
%     vint93                                   7.1024     21.234       0.33448       0.73807
%     vint94                                   49.935     18.222        2.7404     0.0062225
%     vint95                                   19.272     18.031        1.0689       0.28534
%     vint96                                    22.13     17.902        1.2362       0.21661
%     vint97                                   3.4457     17.575       0.19606        0.8446
%     vint98                                   4.8253     17.521        0.2754       0.78306
%     vint99                                   2.4753     17.501       0.14143       0.88755
%     vint00                                   4.7253     17.453       0.27075       0.78663
%     vint01                                   5.8254     17.542       0.33208       0.73988
%     vint02                                   4.8519      17.64       0.27506       0.78332
%     vint03                                   5.2343     17.632       0.29687       0.76661
%     vint04                                   4.8072     17.603       0.27309       0.78483
%     vint05                                   5.5851     17.466       0.31977       0.74919
%     vint06                                   5.1702     17.451       0.29627       0.76708
%     vint07                                   5.1527     17.449        0.2953       0.76781
%     vint08                                   6.7221     17.467       0.38485       0.70041
%     vint09                                   4.4111     17.606       0.25054       0.80221
%     vint10                                   4.3781     17.572       0.24916       0.80328
%     norm_mult_t1sq                         -0.45024    0.11971       -3.7611    0.00017678
%     log_size_tsq                          0.0058865    0.21604      0.027247       0.97827
%     norm_fundgrowtht1tsq                   0.042477    0.27978       0.15183       0.87935
%     norm_PerformanceBuyoutsq                0.32103    0.70046       0.45831       0.64681
%     norm_FundGrowthBuyoutsq                 0.01829    0.24694      0.074069       0.94097
%     norm_PerformanceFundGrowthBuyoutsq     -0.14136    0.21167      -0.66784       0.50435
%     norm_FundGrowthPerformancesq            0.26836    0.24548        1.0932        0.2745
% 
% 
% Number of observations: 1317, Error degrees of freedom: 1280
% Root Mean Squared Error: 24.4
% R-squared: 0.107,  Adjusted R-Squared 0.0823
% F-statistic vs. constant model: 4.28, p-value = 2.06e-15

% 

% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_C_All_MULT_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  141.4292, Crit_Val_e = 42.5570, P_e = 1.1102e-16,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_C_All_MULT.BuyoutDummy FOF_C_All_MULT.norm_mult_t1 FOF_C_All_MULT.log_size_t FOF_C_All_MULT.seq_num_t FOF_C_All_MULT.norm_fundgrowtht1t...
    FOF_C_All_MULT.norm_PerformanceBuyout FOF_C_All_MULT.norm_FundGrowthBuyout FOF_C_All_MULT.norm_PerformanceFundGrowthBuyout FOF_C_All_MULT.norm_FundGrowthPerformance ...
    FOF_C_All_MULT.vint91 FOF_C_All_MULT.vint92 FOF_C_All_MULT.vint93 FOF_C_All_MULT.vint94 ...
    FOF_C_All_MULT.vint95 FOF_C_All_MULT.vint96 FOF_C_All_MULT.vint97 FOF_C_All_MULT.vint98 FOF_C_All_MULT.vint99 ...
    FOF_C_All_MULT.vint00 FOF_C_All_MULT.vint01 FOF_C_All_MULT.vint02 FOF_C_All_MULT.vint03 FOF_C_All_MULT.vint04 ...
    FOF_C_All_MULT.vint05 FOF_C_All_MULT.vint06 FOF_C_All_MULT.vint07 FOF_C_All_MULT.vint08 FOF_C_All_MULT.vint09 FOF_C_All_MULT.vint10];

y = FOF_C_All_MULT.mult_t;



% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','BuyoutDummy','norm_mult_t1','log_size_t','seq_num_t','norm_fundgrowtht1t',...
    'norm_PerformanceBuyout','norm_FundGrowthBuyout','norm_PerformanceFundGrowthBuyout','norm_FundGrowthPerformance',...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White


% White =
% 
%   30×4 table
% 
%                                            beta          SE         tstat           p     
%                                         __________    ________    __________    __________
% 
%     (Intercept)                             1.7653     0.49888        3.5385    0.00041644
%     BuyoutDummy                            0.26937     0.17512        1.5382       0.12424
%     norm_mult_t1                           0.46154      0.1675        2.7555     0.0059419
%     log_size_t                          -0.0070283    0.029861      -0.23537       0.81396
%     seq_num_t                            -0.026231    0.012788       -2.0512      0.040441
%     norm_fundgrowtht1t                   -0.032423    0.059163      -0.54802       0.58377
%     norm_PerformanceBuyout                -0.16757     0.11583       -1.4466       0.14825
%     norm_FundGrowthBuyout                -0.042227    0.058568        -0.721       0.47104
%     norm_PerformanceFundGrowthBuyout       0.24167    0.093633        2.5811     0.0099572
%     norm_FundGrowthPerformance            -0.26315     0.13987       -1.8814      0.060141
%     vint91                                  0.2975     0.60725       0.48991       0.62428
%     vint92                               -0.054931     0.46167      -0.11898       0.90531
%     vint93                                 0.94659     0.79506        1.1906       0.23403
%     vint94                                  3.4249      1.7076        2.0057      0.045095
%     vint95                                  1.7258     0.97227         1.775      0.076133
%     vint96                                  1.4908      1.0014        1.4887        0.1368
%     vint97                                0.088347     0.50326       0.17555       0.86067
%     vint98                                 -0.2991     0.49681      -0.60205       0.54724
%     vint99                                 -0.6778     0.49467       -1.3702       0.17085
%     vint00                                -0.35334     0.48562      -0.72761       0.46698
%     vint01                                -0.13634     0.48787      -0.27945       0.77994
%     vint02                                -0.15925     0.48295      -0.32975       0.74164
%     vint03                                -0.15651     0.47584      -0.32892       0.74227
%     vint04                                -0.12575     0.48073      -0.26158       0.79369
%     vint05                                -0.12419     0.48411      -0.25653       0.79758
%     vint06                                -0.16107     0.48166      -0.33441       0.73812
%     vint07                               -0.020764     0.48413     -0.042889        0.9658
%     vint08                                 0.18571     0.50921        0.3647       0.71539
%     vint09                                0.024887     0.48965      0.050826       0.95947
%     vint10                               0.0002795     0.48857    0.00057207       0.99954
% 

    
   
%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_BuyoutDummy_u = corr(FOF_C_All_MULT.BuyoutDummy,u)
corr_norm_mult_t1_u = corr(FOF_C_All_MULT.norm_mult_t1,u)
corr_log_size_t_u = corr(FOF_C_All_MULT.log_size_t,u)
corr_seq_num_t_u = corr(FOF_C_All_MULT.seq_num_t,u)
corr_norm_fundgrowtht1t_u = corr(FOF_C_All_MULT.norm_fundgrowtht1t,u)
corr_norm_PerformanceBuyout_u = corr(FOF_C_All_MULT.norm_PerformanceBuyout,u)
corr_norm_FundGrowthBuyout_u = corr(FOF_C_All_MULT.norm_FundGrowthBuyout,u)
corr_norm_PerformanceFundGrowthBuyout_u = corr(FOF_C_All_MULT.norm_PerformanceFundGrowthBuyout,u)
corr_norm_FundGrowthPerformance_u = corr(FOF_C_All_MULT.norm_FundGrowthPerformance,u)

%output:
% 
% corr_BuyoutDummy_u = 2.9432e-16
% corr_norm_mult_t1_u = -1.4334e-17
% corr_log_size_t_u = 4.9537e-16
% corr_seq_num_t_u = 3.9280e-17
% corr_norm_fundgrowtht1t_u = 1.8823e-17
% corr_norm_PerformanceBuyout_u = 1.6735e-16
% corr_norm_FundGrowthBuyout_u = 9.1872e-17
% corr_norm_PerformanceFundGrowthBuyout_u = 1.3261e-16
% corr_norm_FundGrowthPerformance_u = 2.3515e-17
%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 7.5359
% kurt_jbtest = 100.0418
% test_stat_jbtest =45.7223
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_C_All_MULT.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_C_All_MULT(regFOF_C_All_MULT.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 9 funds. No creteria to exclude them 


 
% FOF C ALL IRR
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','FOF (C) all_t_t-1 IRR','A2:AR1125');
stringVectors = string(raw(:,[4,5,6,9,13,14]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,10,11,12,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
FOF_C_All_IRR = table;

% Allocate imported array to column variable names
FOF_C_All_IRR.number = data(:,1);
FOF_C_All_IRR.FUNDID = data(:,2);
FOF_C_All_IRR.FIRMID = data(:,3);
FOF_C_All_IRR.teamID = categorical(stringVectors(:,1));
FOF_C_All_IRR.teamIDvintage = stringVectors(:,2);
FOF_C_All_IRR.teamIDvintagenumber = stringVectors(:,3);
FOF_C_All_IRR.vint_t = data(:,4);
FOF_C_All_IRR.vint_t1 = data(:,5);
FOF_C_All_IRR.STRATEGY = categorical(stringVectors(:,4));
FOF_C_All_IRR.BuyoutDummy = data(:,6);
FOF_C_All_IRR.irr_t = data(:,7);
FOF_C_All_IRR.irr_t1 = data(:,8);
FOF_C_All_IRR.STATUS = categorical(stringVectors(:,5));
FOF_C_All_IRR.FUNDMANAGER = categorical(stringVectors(:,6));
FOF_C_All_IRR.size_t = data(:,9);
FOF_C_All_IRR.size_t1 = data(:,10);
FOF_C_All_IRR.seq_num_t = data(:,11);
FOF_C_All_IRR.seq_num_t1 = data(:,12);
FOF_C_All_IRR.fundgrowtht1t = data(:,13);
FOF_C_All_IRR.PerformanceBuyout = data(:,14);
FOF_C_All_IRR.FundGrowthBuyout = data(:,15);
FOF_C_All_IRR.PerformanceFundGrowthBuyout = data(:,16);
FOF_C_All_IRR.FundGrowthPerformance = data(:,17);
FOF_C_All_IRR.vint90 = data(:,18);
FOF_C_All_IRR.vint91 = data(:,19);
FOF_C_All_IRR.vint92 = data(:,20);
FOF_C_All_IRR.vint93 = data(:,21);
FOF_C_All_IRR.vint94 = data(:,22);
FOF_C_All_IRR.vint95 = data(:,23);
FOF_C_All_IRR.vint96 = data(:,24);
FOF_C_All_IRR.vint97 = data(:,25);
FOF_C_All_IRR.vint98 = data(:,26);
FOF_C_All_IRR.vint99 = data(:,27);
FOF_C_All_IRR.vint00 = data(:,28);
FOF_C_All_IRR.vint01 = data(:,29);
FOF_C_All_IRR.vint02 = data(:,30);
FOF_C_All_IRR.vint03 = data(:,31);
FOF_C_All_IRR.vint04 = data(:,32);
FOF_C_All_IRR.vint05 = data(:,33);
FOF_C_All_IRR.vint06 = data(:,34);
FOF_C_All_IRR.vint07 = data(:,35);
FOF_C_All_IRR.vint08 = data(:,36);
FOF_C_All_IRR.vint09 = data(:,37);
FOF_C_All_IRR.vint10 = data(:,38);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

FOF_C_All_IRR.log_size_t = log(FOF_C_All_IRR.size_t);

% Normalize  independent variables 
% ("Independent variables are standardized with a mean 0 and a standard deviation 1 
% to reduce the possible multi- collinearity problem and to ease the
% interpretation of the interaction term")

FOF_C_All_IRR.norm_fundgrowtht1t = zscore(FOF_C_All_IRR.fundgrowtht1t);
FOF_C_All_IRR.norm_FundGrowthPerformance = zscore(FOF_C_All_IRR.FundGrowthPerformance);
FOF_C_All_IRR.norm_irr_t1 = zscore(FOF_C_All_IRR.irr_t1);
FOF_C_All_IRR.norm_PerformanceBuyout = zscore(FOF_C_All_IRR.PerformanceBuyout);
FOF_C_All_IRR.norm_FundGrowthBuyout = zscore(FOF_C_All_IRR.FundGrowthBuyout);
FOF_C_All_IRR.norm_PerformanceFundGrowthBuyout = zscore(FOF_C_All_IRR.PerformanceFundGrowthBuyout);

% Linear regression reg_C_All_MULT
% 
% regFOF_C_All_IRR = fitlm(FOF_C_All_IRR, 'irr_t ~ BuyoutDummy + norm_irr_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_PerformanceBuyout + norm_FundGrowthBuyout + norm_PerformanceFundGrowthBuyout + norm_FundGrowthPerformance + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')
% 
% 
% regFOF_C_All_IRR = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 30 terms in 29 predictors]
% 
% Estimated Coefficients:
%                                         Estimate      SE        tStat        pValue  
%                                         ________    _______    ________    __________
% 
%     (Intercept)                           26.685     27.237     0.97972       0.32744
%     BuyoutDummy                           4.1793      2.455      1.7024      0.088969
%     seq_num_t                           -0.32671    0.50293    -0.64961       0.51608
%     vint91                                3.6542     33.197     0.11008       0.91237
%     vint92                               -10.361     28.959    -0.35778       0.72057
%     vint93                                24.634     31.351     0.78574       0.43219
%     vint94                                 15.22     27.875     0.54603       0.58516
%     vint95                                14.861     27.666     0.53716       0.59126
%     vint96                                 5.066     27.595     0.18358       0.85438
%     vint97                                2.8893     27.353     0.10563       0.91589
%     vint98                               -10.733     27.287    -0.39335       0.69414
%     vint99                               -22.733     27.279    -0.83336       0.40482
%     vint00                               -15.809     27.247    -0.58021       0.56189
%     vint01                               -8.7912     27.324    -0.32174       0.74771
%     vint02                               -11.035     27.393    -0.40284       0.68714
%     vint03                               -10.897     27.408    -0.39757       0.69102
%     vint04                               -13.875     27.389     -0.5066       0.61254
%     vint05                               -12.907     27.259    -0.47348       0.63597
%     vint06                               -15.547     27.251    -0.57051       0.56845
%     vint07                               -12.328     27.248    -0.45245       0.65104
%     vint08                               -11.015      27.26    -0.40408       0.68623
%     vint09                               -7.2345     27.447    -0.26358       0.79215
%     vint10                               -10.136     27.352    -0.37056       0.71103
%     log_size_t                          -0.62648    0.68684    -0.91211       0.36191
%     norm_fundgrowtht1t                    -2.295     1.3599     -1.6876      0.091769
%     norm_FundGrowthPerformance           -4.8587     1.1801     -4.1173    4.1226e-05
%     norm_irr_t1                           8.3559      1.176      7.1056    2.1559e-12
%     norm_PerformanceBuyout               -2.3674     1.2631     -1.8743      0.061153
%     norm_FundGrowthBuyout                 1.6662     1.6248      1.0255       0.30536
%     norm_PerformanceFundGrowthBuyout      2.4903     1.3556      1.8371      0.066472
% 
% 
% Number of observations: 1124, Error degrees of freedom: 1094
% Root Mean Squared Error: 27.1
% R-squared: 0.138,  Adjusted R-Squared 0.115
% F-statistic vs. constant model: 6.05, p-value = 6.49e-21

% 
 

% DIAGNOSTICS of CLRM assumptions

T = regFOF_C_All_IRR.NumObservations;
k = regFOF_C_All_IRR.NumEstimatedCoefficients;
u = regFOF_C_All_IRR.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regFOF_C_All_IRR.Residuals.Raw);
Stdev_u = std(regFOF_C_All_IRR.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   2.3419e-14

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =    1.9621

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

FOF_C_All_IRR.uRaw = u; 
FOF_C_All_IRR.uRaw2 = u.^2;


% Create a table with the constant and squared terms
FOF_C_All_IRR_White = table(FOF_C_All_IRR.irr_t, FOF_C_All_IRR.BuyoutDummy, FOF_C_All_IRR.norm_irr_t1, FOF_C_All_IRR.log_size_t,...
    FOF_C_All_IRR.seq_num_t, FOF_C_All_IRR.norm_fundgrowtht1t,FOF_C_All_IRR.norm_PerformanceBuyout, FOF_C_All_IRR.norm_FundGrowthBuyout,...
    FOF_C_All_IRR.norm_PerformanceFundGrowthBuyout, FOF_C_All_IRR.norm_FundGrowthPerformance, ...
    FOF_C_All_IRR.vint91, FOF_C_All_IRR.vint92, FOF_C_All_IRR.vint93, FOF_C_All_IRR.vint94,...
    FOF_C_All_IRR.vint95, FOF_C_All_IRR.vint96, FOF_C_All_IRR.vint97, FOF_C_All_IRR.vint98, FOF_C_All_IRR.vint99,...
    FOF_C_All_IRR.vint00, FOF_C_All_IRR.vint01, FOF_C_All_IRR.vint02, FOF_C_All_IRR.vint03, FOF_C_All_IRR.vint04,...
    FOF_C_All_IRR.vint05, FOF_C_All_IRR.vint06, FOF_C_All_IRR.vint07, FOF_C_All_IRR.vint08, FOF_C_All_IRR.vint09, FOF_C_All_IRR.vint10,...
    FOF_C_All_IRR.norm_irr_t1.^2, FOF_C_All_IRR.log_size_t.^2, FOF_C_All_IRR.norm_fundgrowtht1t.^2, FOF_C_All_IRR.norm_PerformanceBuyout.^2,...
    FOF_C_All_IRR.norm_FundGrowthBuyout.^2, FOF_C_All_IRR.norm_PerformanceFundGrowthBuyout.^2, FOF_C_All_IRR.norm_FundGrowthPerformance.^2, FOF_C_All_IRR.uRaw2);

 
FOF_C_All_IRR_White.Properties.VariableNames={'irr_t','BuyoutDummy','norm_irr_t1','log_size_t','seq_num_t',...
    'norm_fundgrowtht1t','norm_PerformanceBuyout','norm_FundGrowthBuyout','norm_PerformanceFundGrowthBuyout','norm_FundGrowthPerformance',...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'norm_irr_t1sq','log_size_tsq','norm_fundgrowtht1tsq','norm_PerformanceBuyoutsq','norm_FundGrowthBuyoutsq',...
    'norm_PerformanceFundGrowthBuyoutsq','norm_FundGrowthPerformancesq','uRaw2'};

% Unrestricted regression
regFOF_C_All_IRR_White=fitlm(FOF_C_All_IRR_White,'uRaw2 ~ BuyoutDummy + norm_irr_t1 + log_size_t + seq_num_t + norm_fundgrowtht1t + norm_PerformanceBuyout + norm_FundGrowthBuyout + norm_PerformanceFundGrowthBuyout + norm_FundGrowthPerformance + vint91 + vint92 + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + norm_irr_t1sq + log_size_tsq + norm_fundgrowtht1tsq + norm_PerformanceBuyoutsq + norm_FundGrowthBuyoutsq + norm_PerformanceFundGrowthBuyoutsq + norm_FundGrowthPerformancesq')


% regFOF_C_All_IRR_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 37 terms in 36 predictors]
% 
% Estimated Coefficients:
%                                           Estimate      SE        tStat        pValue  
%                                           ________    ______    _________    __________
% 
%     (Intercept)                            17.181     7128.4    0.0024102       0.99808
%     BuyoutDummy                            30.784     704.86     0.043674       0.96517
%     norm_irr_t1                            1847.7     470.78       3.9248    9.2272e-05
%     log_size_t                            -495.85     764.27     -0.64879       0.51661
%     seq_num_t                             -28.346     129.48     -0.21892       0.82676
%     norm_fundgrowtht1t                    -815.36     748.39      -1.0895       0.27618
%     norm_PerformanceBuyout                -1059.8     489.41      -2.1655      0.030569
%     norm_FundGrowthBuyout                  604.62     879.82      0.68721        0.4921
%     norm_PerformanceFundGrowthBuyout       447.94     743.38      0.60257       0.54692
%     norm_FundGrowthPerformance            -679.68     631.29      -1.0766       0.28188
%     vint91                                 2375.3     8314.6      0.28568       0.77518
%     vint92                                 1478.8     7252.9      0.20389       0.83848
%     vint93                                 2976.6     7865.7      0.37842       0.70519
%     vint94                                 2440.4     6981.7      0.34955       0.72675
%     vint95                                 3449.9     6930.7      0.49778       0.61874
%     vint96                                 3002.6     6914.4      0.43425       0.66419
%     vint97                                 3460.7     6853.4      0.50496       0.61369
%     vint98                                 5707.3     6837.4      0.83471       0.40406
%     vint99                                 1699.2     6838.5      0.24848       0.80381
%     vint00                                 1971.9       6837      0.28841       0.77308
%     vint01                                 2534.3       6850      0.36997       0.71147
%     vint02                                 2094.7     6863.3       0.3052       0.76027
%     vint03                                 2292.6     6866.6      0.33387       0.73854
%     vint04                                 2199.8     6861.2      0.32061       0.74857
%     vint05                                 2387.1     6829.9       0.3495       0.72678
%     vint06                                 2287.2     6827.4      0.33501       0.73768
%     vint07                                 2284.7     6827.9      0.33461       0.73799
%     vint08                                 2338.8     6830.4      0.34241       0.73211
%     vint09                                 2059.6     6875.5      0.29955       0.76458
%     vint10                                 2046.1     6851.5      0.29863       0.76528
%     norm_irr_t1sq                           47.86     48.609      0.98458       0.32505
%     log_size_tsq                           29.286     66.061      0.44332       0.65763
%     norm_fundgrowtht1tsq                   166.58     161.65       1.0305       0.30299
%     norm_PerformanceBuyoutsq              -4.3165     49.317    -0.087525       0.93027
%     norm_FundGrowthBuyoutsq               -104.72     117.75     -0.88935       0.37401
%     norm_PerformanceFundGrowthBuyoutsq    0.88692      29.16     0.030415       0.97574
%     norm_FundGrowthPerformancesq          -28.979     30.985     -0.93528       0.34985
% 
% 
% Number of observations: 1124, Error degrees of freedom: 1087
% Root Mean Squared Error: 6.77e+03
% R-squared: 0.0852,  Adjusted R-Squared 0.0549
% F-statistic vs. constant model: 2.81, p-value = 1.1e-07
% 

% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = regFOF_C_All_IRR_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  95.7324, Crit_Val_e = 42.5570, P_e = 4.6581e-09,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [FOF_C_All_IRR.BuyoutDummy FOF_C_All_IRR.norm_irr_t1 FOF_C_All_IRR.log_size_t FOF_C_All_IRR.seq_num_t FOF_C_All_IRR.norm_fundgrowtht1t...
    FOF_C_All_IRR.norm_PerformanceBuyout FOF_C_All_IRR.norm_FundGrowthBuyout FOF_C_All_IRR.norm_PerformanceFundGrowthBuyout FOF_C_All_IRR.norm_FundGrowthPerformance ...
    FOF_C_All_IRR.vint91 FOF_C_All_IRR.vint92 FOF_C_All_IRR.vint93 FOF_C_All_IRR.vint94 ...
    FOF_C_All_IRR.vint95 FOF_C_All_IRR.vint96 FOF_C_All_IRR.vint97 FOF_C_All_IRR.vint98 FOF_C_All_IRR.vint99 ...
    FOF_C_All_IRR.vint00 FOF_C_All_IRR.vint01 FOF_C_All_IRR.vint02 FOF_C_All_IRR.vint03 FOF_C_All_IRR.vint04 ...
    FOF_C_All_IRR.vint05 FOF_C_All_IRR.vint06 FOF_C_All_IRR.vint07 FOF_C_All_IRR.vint08 FOF_C_All_IRR.vint09 FOF_C_All_IRR.vint10];

y = FOF_C_All_IRR.irr_t;


% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','BuyoutDummy','norm_irr_t1','log_size_t','seq_num_t','norm_fundgrowtht1t',...
    'norm_PerformanceBuyout','norm_FundGrowthBuyout','norm_PerformanceFundGrowthBuyout','norm_FundGrowthPerformance',...
    'vint91','vint92','vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White


% White =
% 
%  30×4 table
% 
%                                           beta        SE        tstat          p     
%                                         ________    _______    ________    __________
% 
%     (Intercept)                           26.685     3.9398      6.7731    2.0293e-11
%     BuyoutDummy                           4.1793     2.2258      1.8776      0.060689
%     norm_irr_t1                           8.3559     3.3957      2.4607      0.014016
%     log_size_t                          -0.62648    0.58893     -1.0638       0.28767
%     seq_num_t                           -0.32671    0.33452    -0.97666       0.32895
%     norm_fundgrowtht1t                    -2.295     1.1157     -2.0569      0.039923
%     norm_PerformanceBuyout               -2.3674     1.8887     -1.2534       0.21031
%     norm_FundGrowthBuyout                 1.6662     1.0296      1.6183       0.10588
%     norm_PerformanceFundGrowthBuyout      2.4903     1.1657      2.1363       0.03287
%     norm_FundGrowthPerformance           -4.8587     2.1717     -2.2372      0.025466
%     vint91                                3.6542     14.428     0.25326       0.80011
%     vint92                               -10.361     3.2524     -3.1857     0.0014838
%     vint93                                24.634     14.252      1.7284      0.084192
%     vint94                                 15.22     7.8269      1.9446      0.052069
%     vint95                                14.861     10.575      1.4052       0.16023
%     vint96                                 5.066     8.2818      0.6117       0.54086
%     vint97                                2.8893     6.9263     0.41716       0.67664
%     vint98                               -10.733     8.9341     -1.2014       0.22985
%     vint99                               -22.733     3.2957     -6.8979    8.8012e-12
%     vint00                               -15.809     2.0744     -7.6211    5.3291e-14
%     vint01                               -8.7912     3.5025       -2.51      0.012215
%     vint02                               -11.035     3.1771     -3.4734     0.0005335
%     vint03                               -10.897     3.5072      -3.107     0.0019375
%     vint04                               -13.875     3.4212     -4.0558    5.3425e-05
%     vint05                               -12.907     3.4374     -3.7548     0.0001824
%     vint06                               -15.547     2.7138     -5.7289    1.2977e-08
%     vint07                               -12.328     2.8561     -4.3165    1.7248e-05
%     vint08                               -11.015     2.9453       -3.74    0.00019331
%     vint09                               -7.2345     3.1076      -2.328       0.02009
%     vint10                               -10.136     2.9368     -3.4513    0.00057875
% 


    
   
%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_BuyoutDummy_u = corr(FOF_C_All_IRR.BuyoutDummy,u)
corr_norm_irr_t1_u = corr(FOF_C_All_IRR.norm_irr_t1,u)
corr_log_size_t_u = corr(FOF_C_All_IRR.log_size_t,u)
corr_seq_num_t_u = corr(FOF_C_All_IRR.seq_num_t,u)
corr_norm_fundgrowtht1t_u = corr(FOF_C_All_IRR.norm_fundgrowtht1t,u)
corr_norm_PerformanceBuyout_u = corr(FOF_C_All_IRR.norm_PerformanceBuyout,u)
corr_norm_FundGrowthBuyout_u = corr(FOF_C_All_IRR.norm_FundGrowthBuyout,u)
corr_norm_PerformanceFundGrowthBuyout_u = corr(FOF_C_All_IRR.norm_PerformanceFundGrowthBuyout,u)
corr_norm_FundGrowthPerformance_u = corr(FOF_C_All_IRR.norm_FundGrowthPerformance,u)

%output:
% 
% corr_BuyoutDummy_u = 1.1813e-16
% corr_norm_irr_t1_u = 2.7528e-17
% corr_log_size_t_u =   3.0030e-16
% corr_seq_num_t_u = 1.1313e-16
% corr_norm_fundgrowtht1t_u = 8.5838e-17
% corr_norm_PerformanceBuyout_u = 2.0131e-17
% corr_norm_FundGrowthBuyout_u = 1.0926e-16
% corr_norm_PerformanceFundGrowthBuyout_u = 6.8560e-17
% corr_norm_FundGrowthPerformance_u = 7.5880e-17
%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest =  6.5883
% kurt_jbtest =  96.1167
% test_stat_jbtest = 4.1421e+05
% crit_val_jbtest = 45.7223
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

sum(regFOF_C_All_IRR.Diagnostics.CooksDistance >=0.05);
Outliers = FOF_C_All_IRR(regFOF_C_All_IRR.Diagnostics.CooksDistance >=0.05,:);

% The outliers are 5 funds. No creteria to exclude them 


 
%% Section 5.3.3 3 categories of fund size increase

% IRR VENTURE LESS 0
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','IRR_Venture_less_0','A2:AP127');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
IRR_Venture_less_0 = table;

% Allocate imported array to column variable names
IRR_Venture_less_0.number = data(:,1);
IRR_Venture_less_0.FUNDID = data(:,2);
IRR_Venture_less_0.FIRMID = data(:,3);
IRR_Venture_less_0.teamID = stringVectors(:,1);
IRR_Venture_less_0.teamIDvintage = stringVectors(:,2);
IRR_Venture_less_0.teamIDvintagenumber = stringVectors(:,3);
IRR_Venture_less_0.vint_t = data(:,4);
IRR_Venture_less_0.vint_t1 = data(:,5);
IRR_Venture_less_0.irr_t = data(:,6);
IRR_Venture_less_0.irr_t1 = data(:,7);
IRR_Venture_less_0.STATUS = categorical(stringVectors(:,4));
IRR_Venture_less_0.FUNDMANAGER = stringVectors(:,5);
IRR_Venture_less_0.size_t = data(:,8);
IRR_Venture_less_0.size_t1 = data(:,9);
IRR_Venture_less_0.seq_num_t = data(:,10);
IRR_Venture_less_0.seq_num_t1 = data(:,11);
IRR_Venture_less_0.fundgrowtht1t = data(:,12);
IRR_Venture_less_0.Dummy_less_0 = data(:,13);
IRR_Venture_less_0.Dummy_0_100 = data(:,14);
IRR_Venture_less_0.Dummy_more_100 = data(:,15);
IRR_Venture_less_0.FundGrowthPerformance = data(:,16);
IRR_Venture_less_0.vint90 = data(:,17);
IRR_Venture_less_0.vint91 = data(:,18);
IRR_Venture_less_0.vint92 = data(:,19);
IRR_Venture_less_0.vint93 = data(:,20);
IRR_Venture_less_0.vint94 = data(:,21);
IRR_Venture_less_0.vint95 = data(:,22);
IRR_Venture_less_0.vint96 = data(:,23);
IRR_Venture_less_0.vint97 = data(:,24);
IRR_Venture_less_0.vint98 = data(:,25);
IRR_Venture_less_0.vint99 = data(:,26);
IRR_Venture_less_0.vint00 = data(:,27);
IRR_Venture_less_0.vint01 = data(:,28);
IRR_Venture_less_0.vint02 = data(:,29);
IRR_Venture_less_0.vint03 = data(:,30);
IRR_Venture_less_0.vint04 = data(:,31);
IRR_Venture_less_0.vint05 = data(:,32);
IRR_Venture_less_0.vint06 = data(:,33);
IRR_Venture_less_0.vint07 = data(:,34);
IRR_Venture_less_0.vint08 = data(:,35);
IRR_Venture_less_0.vint09 = data(:,36);
IRR_Venture_less_0.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;


% Log (size)

IRR_Venture_less_0.log_size_t = log(IRR_Venture_less_0.size_t);

% Linear regression 

reg_IRR_Venture_less_0 = fitlm(IRR_Venture_less_0, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')


% reg_IRR_Venture_less_0 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 22 terms in 21 predictors]
% 
% Estimated Coefficients:
%                    Estimate      SE        tStat       pValue 
%                    ________    _______    ________    ________
% 
%     (Intercept)      47.954     32.342      1.4827     0.14118
%     irr_t1         -0.27044    0.10618     -2.5469    0.012333
%     seq_num_t      -0.22752     1.4093    -0.16144     0.87206
%     vint92          -22.406     35.887    -0.62434     0.53377
%     vint94          -11.104     43.682    -0.25421     0.79984
%     vint95           9.1156      38.18     0.23875     0.81177
%     vint96          -22.325     43.806    -0.50964     0.61138
%     vint97          -8.5623     35.979    -0.23798     0.81236
%     vint98           44.195      34.73      1.2726     0.20601
%     vint99           23.514     34.982     0.67218     0.50296
%     vint00          -33.825     33.311     -1.0154     0.31226
%     vint01          -31.166     32.762    -0.95128     0.34367
%     vint02          -25.945     32.962    -0.78712       0.433
%     vint03          -36.113     32.688     -1.1048      0.2718
%     vint04          -29.022     33.042    -0.87833     0.38179
%     vint05          -15.704      32.87    -0.47776     0.63382
%     vint06           -29.23     31.937    -0.91524     0.36218
%     vint07          -38.604      32.62     -1.1834     0.23933
%     vint08          -26.655      32.57    -0.81839       0.415
%     vint09          -7.7834     32.932    -0.23635     0.81363
%     vint10           -16.62     32.483    -0.51166     0.60998
%     log_size_t      -2.9687     2.1469     -1.3828     0.16969
% 
% 
% Number of observations: 126, Error degrees of freedom: 104
% Root Mean Squared Error: 30.9
% R-squared: 0.289,  Adjusted R-Squared 0.145
% F-statistic vs. constant model: 2.01, p-value = 0.0111


% DIAGNOSTICS of CLRM assumptions

T = reg_IRR_Venture_less_0.NumObservations;
k = reg_IRR_Venture_less_0.NumEstimatedCoefficients;
u = reg_IRR_Venture_less_0.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_IRR_Venture_less_0.Residuals.Raw);
Stdev_u = std(reg_IRR_Venture_less_0.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -1.9200e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9830

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

IRR_Venture_less_0.uRaw = u; 
IRR_Venture_less_0.uRaw2 = u.^2;


% Create a table with the constant and squared terms
IRR_Venture_less_0_White = table(IRR_Venture_less_0.irr_t, IRR_Venture_less_0.irr_t1, IRR_Venture_less_0.log_size_t,...
    IRR_Venture_less_0.seq_num_t, IRR_Venture_less_0.vint92,  IRR_Venture_less_0.vint94,...
    IRR_Venture_less_0.vint95, IRR_Venture_less_0.vint96, IRR_Venture_less_0.vint97, IRR_Venture_less_0.vint98, IRR_Venture_less_0.vint99,...
    IRR_Venture_less_0.vint00, IRR_Venture_less_0.vint01, IRR_Venture_less_0.vint02, IRR_Venture_less_0.vint03, IRR_Venture_less_0.vint04,...
    IRR_Venture_less_0.vint05, IRR_Venture_less_0.vint06, IRR_Venture_less_0.vint07, IRR_Venture_less_0.vint08, IRR_Venture_less_0.vint09, IRR_Venture_less_0.vint10,...
    IRR_Venture_less_0.irr_t1.^2, IRR_Venture_less_0.log_size_t.^2, IRR_Venture_less_0.uRaw2);

 
IRR_Venture_less_0_White.Properties.VariableNames={'irr_t','irr_t1','log_size_t','seq_num_t',...
    'vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'irr_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_IRR_Venture_less_0_White =fitlm(IRR_Venture_less_0_White,'uRaw2 ~ irr_t1 + log_size_t + seq_num_t + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + irr_t1sq + log_size_tsq')


% reg_IRR_Venture_less_0_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                     Estimate       SE         tStat       pValue  
%                     ________    ________    _________    _________
% 
%     (Intercept)        7525       3551.7       2.1187     0.036546
%     irr_t1          -81.492       17.891      -4.5548    1.456e-05
%     log_size_t      -2269.3       1007.1      -2.2532     0.026385
%     seq_num_t       -26.292       123.73      -0.2125      0.83214
%     vint92          -1041.9       3135.3     -0.33232      0.74033
%     vint94          -226.66       3812.3    -0.059455      0.95271
%     vint95           949.32       3334.5      0.28469      0.77646
%     vint96          -1278.8       3836.4     -0.33332      0.73958
%     vint97          -364.61       3150.4     -0.11574      0.90809
%     vint98           9757.5       3069.7       3.1787    0.0019591
%     vint99           2930.7       3143.5       0.9323      0.35338
%     vint00          -2672.4       2943.9      -0.9078      0.36612
%     vint01          -2255.6       2889.9     -0.78053      0.43689
%     vint02          -1614.2       2897.9       -0.557      0.57875
%     vint03            -2034       2875.8     -0.70726      0.48102
%     vint04          -1644.1       2910.2     -0.56494      0.57335
%     vint05          -734.51         2893     -0.25389      0.80009
%     vint06          -1738.3       2826.1     -0.61509      0.53986
%     vint07          -1438.1       2873.8     -0.50042      0.61786
%     vint08          -1924.2       2873.2      -0.6697      0.50456
%     vint09          -1466.9       2920.6     -0.50225      0.61657
%     vint10          -1118.8       2852.5     -0.39223      0.69571
%     irr_t1sq        0.18387     0.074339       2.4734     0.015036
%     log_size_tsq     223.56       117.07       1.9097     0.058985
% 
% 
% Number of observations: 126, Error degrees of freedom: 102
% Root Mean Squared Error: 2.7e+03
% R-squared: 0.536,  Adjusted R-Squared 0.432
% F-statistic vs. constant model: 5.13, p-value = 3.54e-09


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_IRR_Venture_less_0_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  67.5848, Crit_Val_e = 32.6706, P_e = 8.5246e-07,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [IRR_Venture_less_0.irr_t1 IRR_Venture_less_0.log_size_t IRR_Venture_less_0.seq_num_t ...
    IRR_Venture_less_0.vint92 IRR_Venture_less_0.vint94 IRR_Venture_less_0.vint95 IRR_Venture_less_0.vint96 IRR_Venture_less_0.vint97 IRR_Venture_less_0.vint98 IRR_Venture_less_0.vint99 ...
    IRR_Venture_less_0.vint00 IRR_Venture_less_0.vint01 IRR_Venture_less_0.vint02 IRR_Venture_less_0.vint03 IRR_Venture_less_0.vint04 ...
    IRR_Venture_less_0.vint05 IRR_Venture_less_0.vint06 IRR_Venture_less_0.vint07 IRR_Venture_less_0.vint08 IRR_Venture_less_0.vint09 IRR_Venture_less_0.vint10];

y = IRR_Venture_less_0.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','log_size_t','seq_num_t',...
    'vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White


% White =
% 
%   22×4 table
% 
%                      beta        SE        tstat          p     
%                    ________    _______    ________    __________
% 
%     (Intercept)      47.954     12.559      3.8184     0.0002111
%     irr_t1         -0.27044    0.15063     -1.7954      0.075033
%     log_size_t      -2.9687     2.4168     -1.2283       0.22165
%     seq_num_t      -0.22752     0.7677    -0.29637       0.76745
%     vint92          -22.406     8.7128     -2.5716      0.011304
%     vint94          -11.104    0.46999     -23.627             0
%     vint95           9.1156     11.164     0.81654       0.41576
%     vint96          -22.325     3.7994     -5.8759    3.6272e-08
%     vint97          -8.5623     17.904    -0.47824       0.63332
%     vint98           44.195     56.643     0.78023       0.43674
%     vint99           23.514     36.382      0.6463       0.51928
%     vint00          -33.825     8.0734     -4.1898    5.2594e-05
%     vint01          -31.166     5.1878     -6.0076    1.9409e-08
%     vint02          -25.945     4.9086     -5.2857    5.4534e-07
%     vint03          -36.113     3.7847     -9.5419    2.2204e-16
%     vint04          -29.022     2.3636     -12.279             0
%     vint05          -15.704     11.694     -1.3429       0.18176
%     vint06           -29.23     4.3157      -6.773    4.5096e-10
%     vint07          -38.604     8.8485     -4.3627    2.6741e-05
%     vint08          -26.655     5.8379     -4.5658    1.1817e-05
%     vint09          -7.7834     5.3242     -1.4619        0.1463
%     vint10           -16.62     4.2012     -3.9561    0.00012738
% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(IRR_Venture_less_0.irr_t1,u)
corr_log_size_t_u = corr(IRR_Venture_less_0.log_size_t,u)
corr_seq_num_t_u = corr(IRR_Venture_less_0.seq_num_t,u)

% corr_irr_t1_u = 1.9550e-16
% corr_log_size_t_u = 2.0942e-16
% corr_seq_num_t_u = -1.7433e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 2.5242
% kurt_jbtest = 21.1214
% test_stat_jbtest = 1.8578e+03
% crit_val_jbtest = 35.4789
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% there is one outlier with 300% IRR

sum(reg_IRR_Venture_less_0.Diagnostics.CooksDistance >=0.05);
Outliers = IRR_Venture_less_0(reg_IRR_Venture_less_0.Diagnostics.CooksDistance >=0.05,:);

% IRR VENTURE 0-100
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','IRR_Venture_0_100','A2:AP218');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
IRR_Venture_0_100 = table;

% Allocate imported array to column variable names
IRR_Venture_0_100.number = data(:,1);
IRR_Venture_0_100.FUNDID = data(:,2);
IRR_Venture_0_100.FIRMID = data(:,3);
IRR_Venture_0_100.teamID = stringVectors(:,1);
IRR_Venture_0_100.teamIDvintage = stringVectors(:,2);
IRR_Venture_0_100.teamIDvintagenumber = stringVectors(:,3);
IRR_Venture_0_100.vint_t = data(:,4);
IRR_Venture_0_100.vint_t1 = data(:,5);
IRR_Venture_0_100.irr_t = data(:,6);
IRR_Venture_0_100.irr_t1 = data(:,7);
IRR_Venture_0_100.STATUS = categorical(stringVectors(:,4));
IRR_Venture_0_100.FUNDMANAGER = stringVectors(:,5);
IRR_Venture_0_100.size_t = data(:,8);
IRR_Venture_0_100.size_t1 = data(:,9);
IRR_Venture_0_100.seq_num_t = data(:,10);
IRR_Venture_0_100.seq_num_t1 = data(:,11);
IRR_Venture_0_100.fundgrowtht1t = data(:,12);
IRR_Venture_0_100.Dummy_less_0 = data(:,13);
IRR_Venture_0_100.Dummy_0_100 = data(:,14);
IRR_Venture_0_100.Dummy_more_100 = data(:,15);
IRR_Venture_0_100.FundGrowthPerformance = data(:,16);
IRR_Venture_0_100.vint90 = data(:,17);
IRR_Venture_0_100.vint91 = data(:,18);
IRR_Venture_0_100.vint92 = data(:,19);
IRR_Venture_0_100.vint93 = data(:,20);
IRR_Venture_0_100.vint94 = data(:,21);
IRR_Venture_0_100.vint95 = data(:,22);
IRR_Venture_0_100.vint96 = data(:,23);
IRR_Venture_0_100.vint97 = data(:,24);
IRR_Venture_0_100.vint98 = data(:,25);
IRR_Venture_0_100.vint99 = data(:,26);
IRR_Venture_0_100.vint00 = data(:,27);
IRR_Venture_0_100.vint01 = data(:,28);
IRR_Venture_0_100.vint02 = data(:,29);
IRR_Venture_0_100.vint03 = data(:,30);
IRR_Venture_0_100.vint04 = data(:,31);
IRR_Venture_0_100.vint05 = data(:,32);
IRR_Venture_0_100.vint06 = data(:,33);
IRR_Venture_0_100.vint07 = data(:,34);
IRR_Venture_0_100.vint08 = data(:,35);
IRR_Venture_0_100.vint09 = data(:,36);
IRR_Venture_0_100.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

IRR_Venture_0_100.log_size_t = log(IRR_Venture_0_100.size_t);

% Linear regression 

reg_IRR_Venture_0_100 = fitlm(IRR_Venture_0_100, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')

% reg_IRR_Venture_0_100 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 22 terms in 21 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat        pValue  
%                    ________    ________    _________    __________
% 
%     (Intercept)     -7.8613      44.991     -0.17473       0.86147
%     irr_t1          0.28447    0.068764       4.1369    5.2308e-05
%     seq_num_t      -0.68539       1.453     -0.47171       0.63766
%     vint92           18.984      60.344      0.31459       0.75341
%     vint94            41.81      45.017      0.92877       0.35416
%     vint95           88.939      46.867       1.8977      0.059211
%     vint96           39.738      45.156      0.88002       0.37993
%     vint97           50.775      44.132       1.1505       0.25133
%     vint98           7.8249       43.79      0.17869       0.85837
%     vint99          -22.129      44.149     -0.50125       0.61676
%     vint00          -23.255      45.039     -0.51633       0.60621
%     vint01          -6.1008       43.94     -0.13884       0.88972
%     vint02          -3.5432      44.517    -0.079591       0.93664
%     vint03          -7.1724      46.813     -0.15321       0.87839
%     vint04          -6.4142      44.572     -0.14391       0.88572
%     vint05           -1.148       45.23    -0.025381       0.97978
%     vint06          -3.5735      44.185    -0.080876       0.93562
%     vint07           3.6559      43.629     0.083794       0.93331
%     vint08           2.4768      43.645     0.056748        0.9548
%     vint09             -1.8      49.307    -0.036506       0.97092
%     vint10           11.356      45.336      0.25049       0.80247
%     log_size_t       3.2148      3.1861        1.009       0.31422
% 
% 
% Number of observations: 217, Error degrees of freedom: 195
% Root Mean Squared Error: 42.6
% R-squared: 0.33,  Adjusted R-Squared 0.258
% F-statistic vs. constant model: 4.58, p-value = 4.01e-09
% 

% DIAGNOSTICS of CLRM assumptions

T = reg_IRR_Venture_0_100.NumObservations;
k = reg_IRR_Venture_0_100.NumEstimatedCoefficients;
u = reg_IRR_Venture_0_100.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_IRR_Venture_0_100.Residuals.Raw);
Stdev_u = std(reg_IRR_Venture_0_100.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   9.2323e-16

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9722

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

IRR_Venture_0_100.uRaw = u; 
IRR_Venture_0_100.uRaw2 = u.^2;


% Create a table with the constant and squared terms
IRR_Venture_0_100_White = table(IRR_Venture_0_100.irr_t, IRR_Venture_0_100.irr_t1, IRR_Venture_0_100.log_size_t,...
    IRR_Venture_0_100.seq_num_t, IRR_Venture_0_100.vint92,  IRR_Venture_0_100.vint94,...
    IRR_Venture_0_100.vint95, IRR_Venture_0_100.vint96, IRR_Venture_0_100.vint97, IRR_Venture_0_100.vint98, IRR_Venture_0_100.vint99,...
    IRR_Venture_0_100.vint00, IRR_Venture_0_100.vint01, IRR_Venture_0_100.vint02, IRR_Venture_0_100.vint03, IRR_Venture_0_100.vint04,...
    IRR_Venture_0_100.vint05, IRR_Venture_0_100.vint06, IRR_Venture_0_100.vint07, IRR_Venture_0_100.vint08, IRR_Venture_0_100.vint09, IRR_Venture_0_100.vint10,...
    IRR_Venture_0_100.irr_t1.^2, IRR_Venture_0_100.log_size_t.^2, IRR_Venture_0_100.uRaw2);

 
IRR_Venture_0_100_White.Properties.VariableNames={'irr_t','irr_t1','log_size_t','seq_num_t',...
    'vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'irr_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_IRR_Venture_0_100_White =fitlm(IRR_Venture_0_100_White,'uRaw2 ~ irr_t1 + log_size_t + seq_num_t + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + irr_t1sq + log_size_tsq')

% reg_IRR_Venture_0_100_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                     Estimate      SE         tStat         pValue  
%                     ________    _______    __________    __________
% 
%     (Intercept)      -7869.5      17913      -0.43931       0.66093
%     irr_t1            178.73     43.376        4.1204    5.6081e-05
%     log_size_t        3545.1     5179.9        0.6844       0.49454
%     seq_num_t         85.894     430.96       0.19931       0.84223
%     vint92            974.82      17780      0.054828       0.95633
%     vint94           -1974.5      13277      -0.14872       0.88193
%     vint95           -3393.8      13862      -0.24483       0.80685
%     vint96           -5286.2      13395      -0.39463       0.69356
%     vint97           -3114.8      13061      -0.23848       0.81176
%     vint98            2402.1      12974       0.18516        0.8533
%     vint99           -4113.2      13070      -0.31471       0.75332
%     vint00           -738.16      13347     -0.055306       0.95595
%     vint01            3092.9      13027       0.23741       0.81259
%     vint02            604.09      13174      0.045855       0.96347
%     vint03            1286.9      13809      0.093195       0.92585
%     vint04            68.211      13146     0.0051889       0.99587
%     vint05           -52.964      13354    -0.0039662       0.99684
%     vint06            1407.7      13048       0.10788        0.9142
%     vint07             884.3      12872        0.0687        0.9453
%     vint08            1912.3      12908       0.14815       0.88238
%     vint09            128.34      14529     0.0088331       0.99296
%     vint10           -1684.5      13388      -0.12582           0.9
%     irr_t1sq        -0.20092    0.10419       -1.9285      0.055263
%     log_size_tsq     -426.75     547.04      -0.78011       0.43628
% 
% 
% Number of observations: 217, Error degrees of freedom: 193
% Root Mean Squared Error: 1.26e+04
% R-squared: 0.18,  Adjusted R-Squared 0.0822
% F-statistic vs. constant model: 1.84, p-value = 0.0142


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_IRR_Venture_0_100_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  39.0430, Crit_Val_e = 32.6706, P_e =  0.0097,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [IRR_Venture_0_100.irr_t1 IRR_Venture_0_100.log_size_t IRR_Venture_0_100.seq_num_t ...
    IRR_Venture_0_100.vint92 IRR_Venture_0_100.vint94 IRR_Venture_0_100.vint95 IRR_Venture_0_100.vint96 IRR_Venture_0_100.vint97 IRR_Venture_0_100.vint98 IRR_Venture_0_100.vint99 ...
    IRR_Venture_0_100.vint00 IRR_Venture_0_100.vint01 IRR_Venture_0_100.vint02 IRR_Venture_0_100.vint03 IRR_Venture_0_100.vint04 ...
    IRR_Venture_0_100.vint05 IRR_Venture_0_100.vint06 IRR_Venture_0_100.vint07 IRR_Venture_0_100.vint08 IRR_Venture_0_100.vint09 IRR_Venture_0_100.vint10];

y = IRR_Venture_0_100.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','log_size_t','seq_num_t',...
    'vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White


% White =
% 
%   22×4 table
% 
%                      beta        SE        tstat          p     
%                    ________    _______    ________    __________
% 
%     (Intercept)     -7.8613     7.3525     -1.0692       0.28617
%     irr_t1          0.28447    0.10487      2.7126     0.0072167
%     log_size_t       3.2148      1.634      1.9675       0.05041
%     seq_num_t      -0.68539    0.47739     -1.4357       0.15254
%     vint92           18.984     1.3435       14.13             0
%     vint94            41.81     11.138      3.7537    0.00022424
%     vint95           88.939      16.84      5.2813    3.1336e-07
%     vint96           39.738     19.426      2.0456       0.04201
%     vint97           50.775     16.334      3.1086      0.002134
%     vint98           7.8249     18.282       0.428       0.66908
%     vint99          -22.129     6.9886     -3.1665     0.0017667
%     vint00          -23.255     16.823     -1.3823       0.16831
%     vint01          -6.1008     2.8011      -2.178      0.030496
%     vint02          -3.5432     2.4744      -1.432       0.15361
%     vint03          -7.1724     2.8144     -2.5484      0.011519
%     vint04          -6.4142     3.0757     -2.0855      0.038207
%     vint05           -1.148     3.4549    -0.33228          0.74
%     vint06          -3.5735     3.2472     -1.1005       0.27234
%     vint07           3.6559     3.0512      1.1982       0.23217
%     vint08           2.4768     4.6547      0.5321        0.5952
%     vint09             -1.8      2.186    -0.82342       0.41118
%     vint10           11.356     6.8477      1.6584      0.098691
% 




%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(IRR_Venture_0_100.irr_t1,u)
corr_log_size_t_u = corr(IRR_Venture_0_100.log_size_t,u)
corr_seq_num_t_u = corr(IRR_Venture_0_100.seq_num_t,u)

% corr_irr_t1_u = -1.5736e-16
% corr_log_size_t_u = 9.1702e-17
% corr_seq_num_t_u = 1.5071e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 5.8379
% kurt_jbtest = 64.5755
% test_stat_jbtest = 3.5514e+04
% crit_val_jbtest = 35.4789
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% there is one outlier with 514% IRR


sum(reg_IRR_Venture_0_100.Diagnostics.CooksDistance >=0.05);
Outliers = IRR_Venture_0_100(reg_IRR_Venture_0_100.Diagnostics.CooksDistance >=0.05,:);

% IRR VENTURE MORE 100
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','IRR_Venture_more_100','A2:AP151');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
IRR_Venture_more_100 = table;

% Allocate imported array to column variable names
IRR_Venture_more_100.number = data(:,1);
IRR_Venture_more_100.FUNDID = data(:,2);
IRR_Venture_more_100.FIRMID = data(:,3);
IRR_Venture_more_100.teamID = stringVectors(:,1);
IRR_Venture_more_100.teamIDvintage = stringVectors(:,2);
IRR_Venture_more_100.teamIDvintagenumber = stringVectors(:,3);
IRR_Venture_more_100.vint_t = data(:,4);
IRR_Venture_more_100.vint_t1 = data(:,5);
IRR_Venture_more_100.irr_t = data(:,6);
IRR_Venture_more_100.irr_t1 = data(:,7);
IRR_Venture_more_100.STATUS = categorical(stringVectors(:,4));
IRR_Venture_more_100.FUNDMANAGER = stringVectors(:,5);
IRR_Venture_more_100.size_t = data(:,8);
IRR_Venture_more_100.size_t1 = data(:,9);
IRR_Venture_more_100.seq_num_t = data(:,10);
IRR_Venture_more_100.seq_num_t1 = data(:,11);
IRR_Venture_more_100.fundgrowtht1t = data(:,12);
IRR_Venture_more_100.Dummy_less_0 = data(:,13);
IRR_Venture_more_100.Dummy_0_100 = data(:,14);
IRR_Venture_more_100.Dummy_more_100 = data(:,15);
IRR_Venture_more_100.FundGrowthPerformance = data(:,16);
IRR_Venture_more_100.vint90 = data(:,17);
IRR_Venture_more_100.vint91 = data(:,18);
IRR_Venture_more_100.vint92 = data(:,19);
IRR_Venture_more_100.vint93 = data(:,20);
IRR_Venture_more_100.vint94 = data(:,21);
IRR_Venture_more_100.vint95 = data(:,22);
IRR_Venture_more_100.vint96 = data(:,23);
IRR_Venture_more_100.vint97 = data(:,24);
IRR_Venture_more_100.vint98 = data(:,25);
IRR_Venture_more_100.vint99 = data(:,26);
IRR_Venture_more_100.vint00 = data(:,27);
IRR_Venture_more_100.vint01 = data(:,28);
IRR_Venture_more_100.vint02 = data(:,29);
IRR_Venture_more_100.vint03 = data(:,30);
IRR_Venture_more_100.vint04 = data(:,31);
IRR_Venture_more_100.vint05 = data(:,32);
IRR_Venture_more_100.vint06 = data(:,33);
IRR_Venture_more_100.vint07 = data(:,34);
IRR_Venture_more_100.vint08 = data(:,35);
IRR_Venture_more_100.vint09 = data(:,36);
IRR_Venture_more_100.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

IRR_Venture_more_100.log_size_t = log(IRR_Venture_more_100.size_t);

% Linear regression 

reg_IRR_Venture_more_100 = fitlm(IRR_Venture_more_100, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + vint92 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')

% reg_IRR_Venture_more_100 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 21 terms in 20 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE         tStat      pValue 
%                    ________    ________    _________    _______
% 
%     (Intercept)      12.814      20.965      0.61123    0.54212
%     irr_t1         0.013672    0.031299      0.43681    0.66298
%     seq_num_t       -1.6524      1.4387      -1.1485    0.25288
%     vint92          -1.3318      27.958    -0.047636    0.96208
%     vint95           28.214       23.15       1.2188    0.22516
%     vint96           -7.843      22.271     -0.35216    0.72529
%     vint97           14.876      20.888      0.71221    0.47762
%     vint98          -6.1777      20.819     -0.29673    0.76715
%     vint99           -20.88      20.435      -1.0218    0.30879
%     vint00          -15.529      20.316     -0.76435    0.44605
%     vint01          -12.394      21.018     -0.58967    0.55644
%     vint02          -12.492      23.153     -0.53953    0.59045
%     vint03          -9.1715      22.248     -0.41224    0.68085
%     vint04          -35.631      28.132      -1.2666     0.2076
%     vint05          -14.468      21.119     -0.68506    0.49454
%     vint06          -2.0863      21.244    -0.098209    0.92192
%     vint07           1.6273      20.932     0.077743    0.93815
%     vint08          -5.9094       20.89     -0.28288    0.77772
%     vint09          -15.341      24.331     -0.63053    0.52946
%     vint10          -46.888      29.328      -1.5987    0.11233
%     log_size_t      0.96107      1.8934      0.50758    0.61261
% 
% 
% Number of observations: 150, Error degrees of freedom: 129
% Root Mean Squared Error: 19.7
% R-squared: 0.29,  Adjusted R-Squared 0.18
% F-statistic vs. constant model: 2.64, p-value = 0.000553


% DIAGNOSTICS of CLRM assumptions

T = reg_IRR_Venture_more_100.NumObservations;
k = reg_IRR_Venture_more_100.NumEstimatedCoefficients;
u = reg_IRR_Venture_more_100.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_IRR_Venture_more_100.Residuals.Raw);
Stdev_u = std(reg_IRR_Venture_more_100.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =    4.3808e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9785

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

IRR_Venture_more_100.uRaw = u; 
IRR_Venture_more_100.uRaw2 = u.^2;


% Create a table with the constant and squared terms
IRR_Venture_more_100_White = table(IRR_Venture_more_100.irr_t, IRR_Venture_more_100.irr_t1, IRR_Venture_more_100.log_size_t,...
    IRR_Venture_more_100.seq_num_t, IRR_Venture_more_100.vint92,...
    IRR_Venture_more_100.vint95, IRR_Venture_more_100.vint96, IRR_Venture_more_100.vint97, IRR_Venture_more_100.vint98, IRR_Venture_more_100.vint99,...
    IRR_Venture_more_100.vint00, IRR_Venture_more_100.vint01, IRR_Venture_more_100.vint02, IRR_Venture_more_100.vint03, IRR_Venture_more_100.vint04,...
    IRR_Venture_more_100.vint05, IRR_Venture_more_100.vint06, IRR_Venture_more_100.vint07, IRR_Venture_more_100.vint08, IRR_Venture_more_100.vint09, IRR_Venture_more_100.vint10,...
    IRR_Venture_more_100.irr_t1.^2, IRR_Venture_more_100.log_size_t.^2, IRR_Venture_more_100.uRaw2);

 
IRR_Venture_more_100_White.Properties.VariableNames={'irr_t','irr_t1','log_size_t','seq_num_t',...
    'vint92','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'irr_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_IRR_Venture_more_100_White =fitlm(IRR_Venture_more_100_White,'uRaw2 ~ irr_t1 + log_size_t + seq_num_t + vint92 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + irr_t1sq + log_size_tsq')

% reg_IRR_Venture_more_100_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 23 terms in 22 predictors]
% 
% Estimated Coefficients:
%                      Estimate        SE         tStat       pValue 
%                     __________    ________    __________    _______
% 
%     (Intercept)        -880.36        2857      -0.30814    0.75848
%     irr_t1              0.7698      4.3265       0.17793    0.85906
%     log_size_t           370.7      1003.5       0.36939    0.71245
%     seq_num_t          -115.02      104.76       -1.0979    0.27432
%     vint92             -120.07      2012.5     -0.059663    0.95252
%     vint95              1817.4        1676        1.0844    0.28025
%     vint96             -8.5653      1617.6    -0.0052951    0.99578
%     vint97              2282.7      1503.1        1.5186    0.13134
%     vint98              617.97      1500.3       0.41191     0.6811
%     vint99             -126.58        1476     -0.085761    0.93179
%     vint00             -127.66      1461.9     -0.087329    0.93055
%     vint01             -54.443      1510.5     -0.036043    0.97131
%     vint02             -187.78      1666.4      -0.11269    0.91045
%     vint03              426.95      1598.4       0.26711    0.78982
%     vint04              344.37      2025.3       0.17003    0.86526
%     vint05              27.043      1526.7      0.017714    0.98589
%     vint06             -26.973        1527     -0.017664    0.98593
%     vint07              -27.46      1513.7     -0.018141    0.98556
%     vint08              78.592      1504.6      0.052233    0.95842
%     vint09             -189.66      1753.4      -0.10817    0.91403
%     vint10              896.16      2115.4       0.42363    0.67255
%     irr_t1sq        -0.0056812    0.013023      -0.43625     0.6634
%     log_size_tsq       -20.564      95.672      -0.21494    0.83016
% 
% 
% Number of observations: 150, Error degrees of freedom: 127
% Root Mean Squared Error: 1.42e+03
% R-squared: 0.189,  Adjusted R-Squared 0.049
% F-statistic vs. constant model: 1.35, p-value = 0.154


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_IRR_Venture_more_100_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  28.4132, Crit_Val_e = 31.4104, P_e =  0.1000,
%reject_e = 0

% Cannot reject H0 at 5%, so there is no heteroscedasticity
% No, the second assumption of CLRM is not violated.



%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(IRR_Venture_more_100.irr_t1,u)
corr_log_size_t_u = corr(IRR_Venture_more_100.log_size_t,u)
corr_seq_num_t_u = corr(IRR_Venture_more_100.seq_num_t,u)

% corr_irr_t1_u = -1.6034e-16
% corr_log_size_t_u = 3.6527e-17
% corr_seq_num_t_u = -4.9628e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 2.4538
% kurt_jbtest = 19.3945
% test_stat_jbtest = 1.8304e+03
% crit_val_jbtest = 34.1696
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% there is one outlier with 160% IRR


sum(reg_IRR_Venture_more_100.Diagnostics.CooksDistance >=0.05);
Outliers = IRR_Venture_more_100(reg_IRR_Venture_more_100.Diagnostics.CooksDistance >=0.05,:);

% IRR BUYOUT LESS 0
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','IRR_Buyout_less_0','A2:AP103');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
IRR_Buyout_less_0 = table;

% Allocate imported array to column variable names
IRR_Buyout_less_0.number = data(:,1);
IRR_Buyout_less_0.FUNDID = data(:,2);
IRR_Buyout_less_0.FIRMID = data(:,3);
IRR_Buyout_less_0.teamID = stringVectors(:,1);
IRR_Buyout_less_0.teamIDvintage = stringVectors(:,2);
IRR_Buyout_less_0.teamIDvintagenumber = stringVectors(:,3);
IRR_Buyout_less_0.vint_t = data(:,4);
IRR_Buyout_less_0.vint_t1 = data(:,5);
IRR_Buyout_less_0.irr_t = data(:,6);
IRR_Buyout_less_0.irr_t1 = data(:,7);
IRR_Buyout_less_0.STATUS = categorical(stringVectors(:,4));
IRR_Buyout_less_0.FUNDMANAGER = stringVectors(:,5);
IRR_Buyout_less_0.size_t = data(:,8);
IRR_Buyout_less_0.size_t1 = data(:,9);
IRR_Buyout_less_0.seq_num_t = data(:,10);
IRR_Buyout_less_0.seq_num_t1 = data(:,11);
IRR_Buyout_less_0.fundgrowtht1t = data(:,12);
IRR_Buyout_less_0.Dummy_less_0 = data(:,13);
IRR_Buyout_less_0.Dummy_0_100 = data(:,14);
IRR_Buyout_less_0.Dummy_more_100 = data(:,15);
IRR_Buyout_less_0.FundGrowthPerformance = data(:,16);
IRR_Buyout_less_0.vint90 = data(:,17);
IRR_Buyout_less_0.vint91 = data(:,18);
IRR_Buyout_less_0.vint92 = data(:,19);
IRR_Buyout_less_0.vint93 = data(:,20);
IRR_Buyout_less_0.vint94 = data(:,21);
IRR_Buyout_less_0.vint95 = data(:,22);
IRR_Buyout_less_0.vint96 = data(:,23);
IRR_Buyout_less_0.vint97 = data(:,24);
IRR_Buyout_less_0.vint98 = data(:,25);
IRR_Buyout_less_0.vint99 = data(:,26);
IRR_Buyout_less_0.vint00 = data(:,27);
IRR_Buyout_less_0.vint01 = data(:,28);
IRR_Buyout_less_0.vint02 = data(:,29);
IRR_Buyout_less_0.vint03 = data(:,30);
IRR_Buyout_less_0.vint04 = data(:,31);
IRR_Buyout_less_0.vint05 = data(:,32);
IRR_Buyout_less_0.vint06 = data(:,33);
IRR_Buyout_less_0.vint07 = data(:,34);
IRR_Buyout_less_0.vint08 = data(:,35);
IRR_Buyout_less_0.vint09 = data(:,36);
IRR_Buyout_less_0.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;


% Log (size)

IRR_Buyout_less_0.log_size_t = log(IRR_Buyout_less_0.size_t);

% Linear regression 

reg_IRR_Buyout_less_0 = fitlm(IRR_Buyout_less_0, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')


% reg_IRR_Buyout_less_0 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 21 terms in 20 predictors]
% 
% Estimated Coefficients:
%                    Estimate      SE         tStat       pValue 
%                    ________    _______    _________    ________
% 
%     (Intercept)     -1.5115      21.74    -0.069526     0.94474
%     irr_t1          0.34533    0.14682       2.3521    0.021094
%     seq_num_t       0.21181     1.2166      0.17411     0.86221
%     vint94           53.267     24.127       2.2078    0.030086
%     vint95          0.37905      18.52     0.020467     0.98372
%     vint96           25.701     22.177       1.1589      0.2499
%     vint97          0.27392     19.585     0.013987     0.98888
%     vint98          -20.588     22.035     -0.93433     0.35291
%     vint99           20.929     19.311       1.0838     0.28166
%     vint00           13.686     18.143      0.75434     0.45283
%     vint01           30.573     18.735       1.6319     0.10658
%     vint02           29.298     19.195       1.5263     0.13083
%     vint03           13.913     17.944      0.77538     0.44037
%     vint04           8.0868     18.463      0.43801     0.66255
%     vint05           2.5006       18.2      0.13739     0.89106
%     vint06           9.0637     18.397      0.49268     0.62357
%     vint07           13.044     18.814      0.69329     0.49011
%     vint08           8.1886     17.678      0.46321     0.64446
%     vint09           14.542     18.392      0.79067     0.43144
%     vint10            18.76     18.388       1.0202     0.31066
%     log_size_t     -0.22287     1.4168     -0.15731      0.8754
% 
% 
% Number of observations: 102, Error degrees of freedom: 81
% Root Mean Squared Error: 15.4
% R-squared: 0.401,  Adjusted R-Squared 0.253
% F-statistic vs. constant model: 2.71, p-value = 0.000857


% DIAGNOSTICS of CLRM assumptions

T = reg_IRR_Buyout_less_0.NumObservations;
k = reg_IRR_Buyout_less_0.NumEstimatedCoefficients;
u = reg_IRR_Buyout_less_0.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_IRR_Buyout_less_0.Residuals.Raw);
Stdev_u = std(reg_IRR_Buyout_less_0.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -2.6367e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =     1.9897

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

IRR_Buyout_less_0.uRaw = u; 
IRR_Buyout_less_0.uRaw2 = u.^2;


% Create a table with the constant and squared terms
IRR_Buyout_less_0_White = table(IRR_Buyout_less_0.irr_t, IRR_Buyout_less_0.irr_t1, IRR_Buyout_less_0.log_size_t,...
    IRR_Buyout_less_0.seq_num_t, IRR_Buyout_less_0.vint94,...
    IRR_Buyout_less_0.vint95, IRR_Buyout_less_0.vint96, IRR_Buyout_less_0.vint97, IRR_Buyout_less_0.vint98, IRR_Buyout_less_0.vint99,...
    IRR_Buyout_less_0.vint00, IRR_Buyout_less_0.vint01, IRR_Buyout_less_0.vint02, IRR_Buyout_less_0.vint03, IRR_Buyout_less_0.vint04,...
    IRR_Buyout_less_0.vint05, IRR_Buyout_less_0.vint06, IRR_Buyout_less_0.vint07, IRR_Buyout_less_0.vint08, IRR_Buyout_less_0.vint09, IRR_Buyout_less_0.vint10,...
    IRR_Buyout_less_0.irr_t1.^2, IRR_Buyout_less_0.log_size_t.^2, IRR_Buyout_less_0.uRaw2);

 
IRR_Buyout_less_0_White.Properties.VariableNames={'irr_t','irr_t1','log_size_t','seq_num_t',...
    'vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'irr_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_IRR_Buyout_less_0_White =fitlm(IRR_Buyout_less_0_White,'uRaw2 ~ irr_t1 + log_size_t + seq_num_t + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + irr_t1sq + log_size_tsq')

% reg_IRR_Buyout_less_0_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 23 terms in 22 predictors]
% 
% Estimated Coefficients:
%                     Estimate      SE        tStat       pValue 
%                     ________    _______    ________    ________
% 
%     (Intercept)      -820.63     879.65    -0.93291     0.35371
%     irr_t1            1.0515     5.5633     0.18901     0.85057
%     log_size_t         174.5     223.33     0.78135     0.43693
%     seq_num_t         41.516     29.508       1.407     0.16336
%     vint94            160.09     654.92     0.24444     0.80752
%     vint95            111.44     490.42     0.22723     0.82083
%     vint96            -99.96     535.92    -0.18652     0.85251
%     vint97            330.41     555.39     0.59491      0.5536
%     vint98            202.36     557.23     0.36316     0.71746
%     vint99            318.49     536.99      0.5931     0.55481
%     vint00            269.65     500.28       0.539      0.5914
%     vint01            1115.9     524.73      2.1266    0.036572
%     vint02            1033.4     547.38      1.8878    0.062721
%     vint03             320.4     516.38     0.62048     0.53673
%     vint04            555.95     522.56      1.0639     0.29062
%     vint05            311.34     517.67     0.60143     0.54928
%     vint06            249.52     520.58      0.4793     0.63305
%     vint07            265.88     540.84      0.4916     0.62436
%     vint08             319.2     500.86     0.63731     0.52576
%     vint09            337.82     528.22     0.63956     0.52431
%     vint10            359.38     513.85     0.69939     0.48636
%     irr_t1sq        0.073966    0.13994     0.52856      0.5986
%     log_size_tsq     -16.619     19.598    -0.84801       0.399
% 
% 
% Number of observations: 102, Error degrees of freedom: 79
% Root Mean Squared Error: 369
% R-squared: 0.365,  Adjusted R-Squared 0.188
% F-statistic vs. constant model: 2.06, p-value = 0.0106


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_IRR_Buyout_less_0_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e = 37.1886, Crit_Val_e = 31.4104, P_e =  0.0111,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [IRR_Buyout_less_0.irr_t1 IRR_Buyout_less_0.log_size_t IRR_Buyout_less_0.seq_num_t ...
    IRR_Buyout_less_0.vint94 IRR_Buyout_less_0.vint95 IRR_Buyout_less_0.vint96 IRR_Buyout_less_0.vint97 IRR_Buyout_less_0.vint98 IRR_Buyout_less_0.vint99 ...
    IRR_Buyout_less_0.vint00 IRR_Buyout_less_0.vint01 IRR_Buyout_less_0.vint02 IRR_Buyout_less_0.vint03 IRR_Buyout_less_0.vint04 ...
    IRR_Buyout_less_0.vint05 IRR_Buyout_less_0.vint06 IRR_Buyout_less_0.vint07 IRR_Buyout_less_0.vint08 IRR_Buyout_less_0.vint09 IRR_Buyout_less_0.vint10];

y = IRR_Buyout_less_0.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','log_size_t','seq_num_t',...
    'vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White


% White =
% 
%   21×4 table
% 
%                      beta        SE         tstat          p     
%                    ________    _______    _________    __________
% 
%     (Intercept)     -1.5115     16.282    -0.092831       0.92622
%     irr_t1          0.34533    0.15687       2.2014      0.030004
%     log_size_t     -0.22287     1.2473     -0.17869       0.85855
%     seq_num_t       0.21181     1.2324      0.17187       0.86389
%     vint94           53.267     9.4253       5.6514    1.5059e-07
%     vint95          0.37905     4.5697     0.082949       0.93406
%     vint96           25.701     3.4743       7.3974    4.3818e-11
%     vint97          0.27392     10.355     0.026454       0.97895
%     vint98          -20.588     10.676      -1.9285      0.056626
%     vint99           20.929     10.199       2.0522      0.042767
%     vint00           13.686     8.0608       1.6978      0.092649
%     vint01           30.573     17.352       1.7619      0.081134
%     vint02           29.298     15.701        1.866      0.064969
%     vint03           13.913     7.5701       1.8379      0.069045
%     vint04           8.0868     11.222      0.72063       0.47282
%     vint05           2.5006     10.234      0.24434       0.80747
%     vint06           9.0637     8.5247       1.0632       0.29024
%     vint07           13.044     9.2962       1.4031       0.16368
%     vint08           8.1886     7.4546       1.0985       0.27464
%     vint09           14.542     8.9037       1.6333       0.10556
%     vint10            18.76     8.6341       2.1728      0.032161
% 



%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(IRR_Buyout_less_0.irr_t1,u)
corr_log_size_t_u = corr(IRR_Buyout_less_0.log_size_t,u)
corr_seq_num_t_u = corr(IRR_Buyout_less_0.seq_num_t,u)

% corr_irr_t1_u = -2.3067e-16
% corr_log_size_t_u =  1.1056e-16
% corr_seq_num_t_u = -9.3842e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 0.2899
% kurt_jbtest = 5.5380
% test_stat_jbtest = 28.8057
% crit_val_jbtest = 34.1696
% p_jbtest = 0.0917
% reject_jbstest = 0

%Cannot reject H0 at 5%.
% No, the fifth assumption of the CLRM is not violated. Residuals are 
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 

% IRR BUYOUT 0 to 100
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','IRR_Buyout_0_100','A2:AP265');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
IRR_Buyout_0_100 = table;

% Allocate imported array to column variable names
IRR_Buyout_0_100.number = data(:,1);
IRR_Buyout_0_100.FUNDID = data(:,2);
IRR_Buyout_0_100.FIRMID = data(:,3);
IRR_Buyout_0_100.teamID = stringVectors(:,1);
IRR_Buyout_0_100.teamIDvintage = stringVectors(:,2);
IRR_Buyout_0_100.teamIDvintagenumber = stringVectors(:,3);
IRR_Buyout_0_100.vint_t = data(:,4);
IRR_Buyout_0_100.vint_t1 = data(:,5);
IRR_Buyout_0_100.irr_t = data(:,6);
IRR_Buyout_0_100.irr_t1 = data(:,7);
IRR_Buyout_0_100.STATUS = categorical(stringVectors(:,4));
IRR_Buyout_0_100.FUNDMANAGER = stringVectors(:,5);
IRR_Buyout_0_100.size_t = data(:,8);
IRR_Buyout_0_100.size_t1 = data(:,9);
IRR_Buyout_0_100.seq_num_t = data(:,10);
IRR_Buyout_0_100.seq_num_t1 = data(:,11);
IRR_Buyout_0_100.fundgrowtht1t = data(:,12);
IRR_Buyout_0_100.Dummy_less_0 = data(:,13);
IRR_Buyout_0_100.Dummy_0_100 = data(:,14);
IRR_Buyout_0_100.Dummy_more_100 = data(:,15);
IRR_Buyout_0_100.FundGrowthPerformance = data(:,16);
IRR_Buyout_0_100.vint90 = data(:,17);
IRR_Buyout_0_100.vint91 = data(:,18);
IRR_Buyout_0_100.vint92 = data(:,19);
IRR_Buyout_0_100.vint93 = data(:,20);
IRR_Buyout_0_100.vint94 = data(:,21);
IRR_Buyout_0_100.vint95 = data(:,22);
IRR_Buyout_0_100.vint96 = data(:,23);
IRR_Buyout_0_100.vint97 = data(:,24);
IRR_Buyout_0_100.vint98 = data(:,25);
IRR_Buyout_0_100.vint99 = data(:,26);
IRR_Buyout_0_100.vint00 = data(:,27);
IRR_Buyout_0_100.vint01 = data(:,28);
IRR_Buyout_0_100.vint02 = data(:,29);
IRR_Buyout_0_100.vint03 = data(:,30);
IRR_Buyout_0_100.vint04 = data(:,31);
IRR_Buyout_0_100.vint05 = data(:,32);
IRR_Buyout_0_100.vint06 = data(:,33);
IRR_Buyout_0_100.vint07 = data(:,34);
IRR_Buyout_0_100.vint08 = data(:,35);
IRR_Buyout_0_100.vint09 = data(:,36);
IRR_Buyout_0_100.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

IRR_Buyout_0_100.log_size_t = log(IRR_Buyout_0_100.size_t);

% Linear regression 

reg_IRR_Buyout_0_100 = fitlm(IRR_Buyout_0_100, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')


% reg_IRR_Buyout_0_100 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 22 terms in 21 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue  
%                    ________    ________    _______    __________
% 
%     (Intercept)     76.962        13.17     5.8437    1.6403e-08
%     irr_t1           0.202     0.050517     3.9987    8.4642e-05
%     seq_num_t       1.3253      0.67536     1.9623       0.05087
%     vint92         -53.684       17.377    -3.0894     0.0022396
%     vint94         -59.879       14.208    -4.2145    3.5369e-05
%     vint95         -48.292       13.441    -3.5928    0.00039618
%     vint96         -70.599       13.355    -5.2864    2.7856e-07
%     vint97         -65.573       13.237    -4.9539    1.3667e-06
%     vint98         -65.354       12.973    -5.0377    9.2207e-07
%     vint99         -63.901       13.072    -4.8885    1.8516e-06
%     vint00         -53.589       12.763    -4.1987    3.7745e-05
%     vint01         -42.882        12.96    -3.3088     0.0010797
%     vint02         -55.962       12.907    -4.3357    2.1333e-05
%     vint03         -54.879       13.044    -4.2072    3.6444e-05
%     vint04           -53.6       12.743    -4.2061    3.6606e-05
%     vint05         -57.385       12.605    -4.5524    8.4029e-06
%     vint06         -64.826        12.59     -5.149    5.4252e-07
%     vint07          -57.79        12.79    -4.5185    9.7422e-06
%     vint08         -57.273       12.708     -4.507    1.0246e-05
%     vint09         -55.613       12.984    -4.2833    2.6582e-05
%     vint10          -60.94       12.751    -4.7793    3.0535e-06
%     log_size_t     -1.7435      0.73788    -2.3629      0.018925
% 
% 
% Number of observations: 264, Error degrees of freedom: 242
% Root Mean Squared Error: 12.1
% R-squared: 0.319,  Adjusted R-Squared 0.26
% F-statistic vs. constant model: 5.41, p-value = 1.15e-11



% DIAGNOSTICS of CLRM assumptions

T = reg_IRR_Buyout_0_100.NumObservations;
k = reg_IRR_Buyout_0_100.NumEstimatedCoefficients;
u = reg_IRR_Buyout_0_100.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_IRR_Buyout_0_100.Residuals.Raw);
Stdev_u = std(reg_IRR_Buyout_0_100.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =     5.6058e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9698

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

IRR_Buyout_0_100.uRaw = u; 
IRR_Buyout_0_100.uRaw2 = u.^2;


% Create a table with the constant and squared terms
IRR_Buyout_0_100_White = table(IRR_Buyout_0_100.irr_t, IRR_Buyout_0_100.irr_t1, IRR_Buyout_0_100.log_size_t,...
    IRR_Buyout_0_100.seq_num_t, IRR_Buyout_0_100.vint92,  IRR_Buyout_0_100.vint94,...
    IRR_Buyout_0_100.vint95, IRR_Buyout_0_100.vint96, IRR_Buyout_0_100.vint97, IRR_Buyout_0_100.vint98, IRR_Buyout_0_100.vint99,...
    IRR_Buyout_0_100.vint00, IRR_Buyout_0_100.vint01, IRR_Buyout_0_100.vint02, IRR_Buyout_0_100.vint03, IRR_Buyout_0_100.vint04,...
    IRR_Buyout_0_100.vint05, IRR_Buyout_0_100.vint06, IRR_Buyout_0_100.vint07, IRR_Buyout_0_100.vint08, IRR_Buyout_0_100.vint09, IRR_Buyout_0_100.vint10,...
    IRR_Buyout_0_100.irr_t1.^2, IRR_Buyout_0_100.log_size_t.^2, IRR_Buyout_0_100.uRaw2);

 
IRR_Buyout_0_100_White.Properties.VariableNames={'irr_t','irr_t1','log_size_t','seq_num_t',...
    'vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'irr_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_IRR_Buyout_0_100_White =fitlm(IRR_Buyout_0_100_White,'uRaw2 ~ irr_t1 + log_size_t + seq_num_t + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + irr_t1sq + log_size_tsq')

% reg_IRR_Buyout_0_100_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE        tStat      pValue 
%                     _________    ________    _______    ________
% 
%     (Intercept)        716.49      542.22     1.3214     0.18763
%     irr_t1             1.3096      2.2149    0.59126      0.5549
%     log_size_t        -248.16      139.86    -1.7743    0.077282
%     seq_num_t          43.581      16.909     2.5773    0.010554
%     vint92             235.94      423.62    0.55696     0.57808
%     vint94              295.4      346.63    0.85222     0.39494
%     vint95             390.76      327.61     1.1928     0.23414
%     vint96             264.58      325.41    0.81308     0.41698
%     vint97             209.89      322.39    0.65104     0.51564
%     vint98             268.41      315.97    0.84948     0.39646
%     vint99              420.9      318.65     1.3209      0.1878
%     vint00             278.65      311.54    0.89443     0.37199
%     vint01             413.25      315.77     1.3087     0.19189
%     vint02             392.92      314.77     1.2483     0.21315
%     vint03             383.78      317.61     1.2083     0.22811
%     vint04             413.06      310.38     1.3308      0.1845
%     vint05             467.03      307.16     1.5205     0.12971
%     vint06             281.13      306.72    0.91659     0.36028
%     vint07             240.86      311.57    0.77303     0.44026
%     vint08             333.71      309.51     1.0782     0.28203
%     vint09             235.04      316.62    0.74233     0.45861
%     vint10             305.58      310.99    0.98258      0.3268
%     irr_t1sq        0.0040435    0.022898    0.17659     0.85998
%     log_size_tsq         12.4      10.605     1.1692     0.24348
% 
% 
% Number of observations: 264, Error degrees of freedom: 240
% Root Mean Squared Error: 296
% R-squared: 0.178,  Adjusted R-Squared 0.0989
% F-statistic vs. constant model: 2.26, p-value = 0.00126


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_IRR_Buyout_0_100_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e = 46.9193, Crit_Val_e = 32.6706, P_e = 9.6289e-04,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [IRR_Buyout_0_100.irr_t1 IRR_Buyout_0_100.log_size_t IRR_Buyout_0_100.seq_num_t ...
    IRR_Buyout_0_100.vint92 IRR_Buyout_0_100.vint94 IRR_Buyout_0_100.vint95 IRR_Buyout_0_100.vint96 IRR_Buyout_0_100.vint97 IRR_Buyout_0_100.vint98 IRR_Buyout_0_100.vint99 ...
    IRR_Buyout_0_100.vint00 IRR_Buyout_0_100.vint01 IRR_Buyout_0_100.vint02 IRR_Buyout_0_100.vint03 IRR_Buyout_0_100.vint04 ...
    IRR_Buyout_0_100.vint05 IRR_Buyout_0_100.vint06 IRR_Buyout_0_100.vint07 IRR_Buyout_0_100.vint08 IRR_Buyout_0_100.vint09 IRR_Buyout_0_100.vint10];

y = IRR_Buyout_0_100.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','log_size_t','seq_num_t',...
    'vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White

% White =
% 
%   22×4 table
% 
%                     beta         SE        tstat         p     
%                    _______    ________    _______    __________
% 
%     (Intercept)     76.962      5.3797     14.306             0
%     irr_t1           0.202    0.069723     2.8972     0.0040832
%     log_size_t     -1.7435     0.96081    -1.8146      0.070722
%     seq_num_t       1.3253     0.96947      1.367        0.1728
%     vint92         -53.684      4.2181    -12.727             0
%     vint94         -59.879       5.116    -11.704             0
%     vint95         -48.292      6.9773    -6.9213    3.4341e-11
%     vint96         -70.599      5.3788    -13.126             0
%     vint97         -65.573      4.8581    -13.498             0
%     vint98         -65.354      5.0978     -12.82             0
%     vint99         -63.901      6.6667    -9.5851             0
%     vint00         -53.589      5.3848    -9.9519             0
%     vint01         -42.882      5.8279    -7.3581    2.4043e-12
%     vint02         -55.962      5.5345    -10.111             0
%     vint03         -54.879      7.4478    -7.3685    2.2538e-12
%     vint04           -53.6      5.7378    -9.3415             0
%     vint05         -57.385      5.1661    -11.108             0
%     vint06         -64.826      4.0836    -15.875             0
%     vint07          -57.79      4.2201    -13.694             0
%     vint08         -57.273      4.7231    -12.126             0
%     vint09         -55.613      4.1559    -13.382             0
%     vint10          -60.94      4.7661    -12.786             0





%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(IRR_Buyout_0_100.irr_t1,u)
corr_log_size_t_u = corr(IRR_Buyout_0_100.log_size_t,u)
corr_seq_num_t_u = corr(IRR_Buyout_0_100.seq_num_t,u)

% corr_irr_t1_u = -9.2805e-18
% corr_log_size_t_u = 0
% corr_seq_num_t_u =  -3.5758e-16

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 0.8914
% kurt_jbtest = 6.2376
% test_stat_jbtest = 150.2672
% crit_val_jbtest = 35.4789
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% almost normal

% IRR BUYOUT MORE 100
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','IRR_Buyout_more_100','A2:AP260');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
IRR_Buyout_more_100 = table;

% Allocate imported array to column variable names
IRR_Buyout_more_100.number = data(:,1);
IRR_Buyout_more_100.FUNDID = data(:,2);
IRR_Buyout_more_100.FIRMID = data(:,3);
IRR_Buyout_more_100.teamID = stringVectors(:,1);
IRR_Buyout_more_100.teamIDvintage = stringVectors(:,2);
IRR_Buyout_more_100.teamIDvintagenumber = stringVectors(:,3);
IRR_Buyout_more_100.vint_t = data(:,4);
IRR_Buyout_more_100.vint_t1 = data(:,5);
IRR_Buyout_more_100.irr_t = data(:,6);
IRR_Buyout_more_100.irr_t1 = data(:,7);
IRR_Buyout_more_100.STATUS = categorical(stringVectors(:,4));
IRR_Buyout_more_100.FUNDMANAGER = stringVectors(:,5);
IRR_Buyout_more_100.size_t = data(:,8);
IRR_Buyout_more_100.size_t1 = data(:,9);
IRR_Buyout_more_100.seq_num_t = data(:,10);
IRR_Buyout_more_100.seq_num_t1 = data(:,11);
IRR_Buyout_more_100.fundgrowtht1t = data(:,12);
IRR_Buyout_more_100.Dummy_less_0 = data(:,13);
IRR_Buyout_more_100.Dummy_0_100 = data(:,14);
IRR_Buyout_more_100.Dummy_more_100 = data(:,15);
IRR_Buyout_more_100.FundGrowthPerformance = data(:,16);
IRR_Buyout_more_100.vint90 = data(:,17);
IRR_Buyout_more_100.vint91 = data(:,18);
IRR_Buyout_more_100.vint92 = data(:,19);
IRR_Buyout_more_100.vint93 = data(:,20);
IRR_Buyout_more_100.vint94 = data(:,21);
IRR_Buyout_more_100.vint95 = data(:,22);
IRR_Buyout_more_100.vint96 = data(:,23);
IRR_Buyout_more_100.vint97 = data(:,24);
IRR_Buyout_more_100.vint98 = data(:,25);
IRR_Buyout_more_100.vint99 = data(:,26);
IRR_Buyout_more_100.vint00 = data(:,27);
IRR_Buyout_more_100.vint01 = data(:,28);
IRR_Buyout_more_100.vint02 = data(:,29);
IRR_Buyout_more_100.vint03 = data(:,30);
IRR_Buyout_more_100.vint04 = data(:,31);
IRR_Buyout_more_100.vint05 = data(:,32);
IRR_Buyout_more_100.vint06 = data(:,33);
IRR_Buyout_more_100.vint07 = data(:,34);
IRR_Buyout_more_100.vint08 = data(:,35);
IRR_Buyout_more_100.vint09 = data(:,36);
IRR_Buyout_more_100.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

IRR_Buyout_more_100.log_size_t = log(IRR_Buyout_more_100.size_t);

% Linear regression 

reg_IRR_Buyout_more_100 = fitlm(IRR_Buyout_more_100, 'irr_t ~ irr_t1 + log_size_t + seq_num_t + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')


% reg_IRR_Buyout_more_100 = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 22 terms in 21 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat       pValue  
%                    ________    ________    ________    _________
% 
%     (Intercept)      48.725      16.948       2.875    0.0044085
%     irr_t1          0.14565    0.047099      3.0925    0.0022229
%     seq_num_t       0.67732     0.76367     0.88692      0.37602
%     vint93           1.9411      22.928     0.08466       0.9326
%     vint94          -17.868       18.77    -0.95195      0.34209
%     vint95          -27.505      17.609      -1.562      0.11963
%     vint96          -6.3357       17.53    -0.36142       0.7181
%     vint97          -40.835      16.789     -2.4323     0.015746
%     vint98          -44.302      16.693      -2.654    0.0084928
%     vint99          -38.688      16.754     -2.3092     0.021794
%     vint00          -29.803      16.728     -1.7816     0.076099
%     vint01          -23.424      17.546      -1.335      0.18315
%     vint02          -25.652      17.621     -1.4557      0.14679
%     vint03          -27.782      16.934     -1.6406       0.1022
%     vint04          -39.096      17.828      -2.193      0.02928
%     vint05          -35.575      16.565     -2.1476     0.032762
%     vint06          -38.479      16.646     -2.3116     0.021656
%     vint07          -38.783      16.553     -2.3429     0.019961
%     vint08          -35.157      16.824     -2.0897     0.037712
%     vint09          -31.505      17.486     -1.8017     0.072857
%     vint10            -38.8      17.572     -2.2081     0.028199
%     log_size_t     -0.69521      0.9381    -0.74109      0.45938
% 
% 
% Number of observations: 259, Error degrees of freedom: 237
% Root Mean Squared Error: 16.2
% R-squared: 0.236,  Adjusted R-Squared 0.169
% F-statistic vs. constant model: 3.49, p-value = 1.34e-06


% DIAGNOSTICS of CLRM assumptions

T = reg_IRR_Buyout_more_100.NumObservations;
k = reg_IRR_Buyout_more_100.NumEstimatedCoefficients;
u = reg_IRR_Buyout_more_100.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_IRR_Buyout_more_100.Residuals.Raw);
Stdev_u = std(reg_IRR_Buyout_more_100.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -2.5096e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =    1.9700

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

IRR_Buyout_more_100.uRaw = u; 
IRR_Buyout_more_100.uRaw2 = u.^2;


% Create a table with the constant and squared terms
IRR_Buyout_more_100_White = table(IRR_Buyout_more_100.irr_t, IRR_Buyout_more_100.irr_t1, IRR_Buyout_more_100.log_size_t,...
    IRR_Buyout_more_100.seq_num_t, IRR_Buyout_more_100.vint93,  IRR_Buyout_more_100.vint94,...
    IRR_Buyout_more_100.vint95, IRR_Buyout_more_100.vint96, IRR_Buyout_more_100.vint97, IRR_Buyout_more_100.vint98, IRR_Buyout_more_100.vint99,...
    IRR_Buyout_more_100.vint00, IRR_Buyout_more_100.vint01, IRR_Buyout_more_100.vint02, IRR_Buyout_more_100.vint03, IRR_Buyout_more_100.vint04,...
    IRR_Buyout_more_100.vint05, IRR_Buyout_more_100.vint06, IRR_Buyout_more_100.vint07, IRR_Buyout_more_100.vint08, IRR_Buyout_more_100.vint09, IRR_Buyout_more_100.vint10,...
    IRR_Buyout_more_100.irr_t1.^2, IRR_Buyout_more_100.log_size_t.^2, IRR_Buyout_more_100.uRaw2);

 
IRR_Buyout_more_100_White.Properties.VariableNames={'irr_t','irr_t1','log_size_t','seq_num_t',...
    'vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'irr_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_IRR_Buyout_more_100_White =fitlm(IRR_Buyout_more_100_White,'uRaw2 ~ irr_t1 + log_size_t + seq_num_t + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + irr_t1sq + log_size_tsq')

% reg_IRR_Buyout_more_100_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                     Estimate       SE        tStat       pValue  
%                     ________    ________    _______    __________
% 
%     (Intercept)       4661.2      1542.8     3.0212     0.0027958
%     irr_t1           -9.0466      4.3115    -2.0982      0.036952
%     log_size_t       -1356.8      393.81    -3.4455    0.00067505
%     seq_num_t         46.493      42.951     1.0824       0.28016
%     vint93            362.91      1270.1    0.28573       0.77534
%     vint94            609.37      1040.3    0.58579       0.55858
%     vint95            595.49      977.57    0.60915       0.54301
%     vint96            2692.3      969.18     2.7779     0.0059136
%     vint97            823.91      932.57    0.88349       0.37788
%     vint98            834.64      929.08    0.89835       0.36992
%     vint99            395.16      933.74     0.4232       0.67254
%     vint00            177.69      929.37    0.19119       0.84854
%     vint01             394.5      978.17    0.40331       0.68709
%     vint02            236.15      979.03    0.24121       0.80961
%     vint03            418.05      940.43    0.44453       0.65707
%     vint04            247.15      989.77    0.24971       0.80303
%     vint05            427.49      919.67    0.46483       0.64248
%     vint06            304.18      923.72     0.3293       0.74222
%     vint07            248.41      920.23    0.26995       0.78744
%     vint08            224.72      935.41    0.24023       0.81036
%     vint09            285.63       971.6    0.29398       0.76903
%     vint10            348.58      976.08    0.35713       0.72132
%     irr_t1sq        0.036744    0.025416     1.4457       0.14958
%     log_size_tsq          91      28.974     3.1408      0.001901
% 
% 
% Number of observations: 259, Error degrees of freedom: 235
% Root Mean Squared Error: 895
% R-squared: 0.251,  Adjusted R-Squared 0.178
% F-statistic vs. constant model: 3.43, p-value = 8.82e-07


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_IRR_Buyout_more_100_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  65.0459, Crit_Val_e = 32.6706, P_e =   2.1381e-06,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [IRR_Buyout_more_100.irr_t1 IRR_Buyout_more_100.log_size_t IRR_Buyout_more_100.seq_num_t ...
    IRR_Buyout_more_100.vint93 IRR_Buyout_more_100.vint94 IRR_Buyout_more_100.vint95 IRR_Buyout_more_100.vint96 IRR_Buyout_more_100.vint97 IRR_Buyout_more_100.vint98 IRR_Buyout_more_100.vint99 ...
    IRR_Buyout_more_100.vint00 IRR_Buyout_more_100.vint01 IRR_Buyout_more_100.vint02 IRR_Buyout_more_100.vint03 IRR_Buyout_more_100.vint04 ...
    IRR_Buyout_more_100.vint05 IRR_Buyout_more_100.vint06 IRR_Buyout_more_100.vint07 IRR_Buyout_more_100.vint08 IRR_Buyout_more_100.vint09 IRR_Buyout_more_100.vint10];

y = IRR_Buyout_more_100.irr_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','irr_t1','log_size_t','seq_num_t',...
    'vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White

% 
% White =
% 
%   22×4 table
% 
%                      beta         SE        tstat          p     
%                    ________    ________    ________    __________
% 
%     (Intercept)      48.725      4.8086      10.133             0
%     irr_t1          0.14565    0.062453      2.3322      0.020464
%     log_size_t     -0.69521     0.91073    -0.76335       0.44595
%     seq_num_t       0.67732     0.84278     0.80367       0.42233
%     vint93           1.9411     0.73916      2.6261     0.0091561
%     vint94          -17.868      10.885     -1.6416        0.1019
%     vint95          -27.505      7.8768     -3.4919    0.00056442
%     vint96          -6.3357      13.606    -0.46566       0.64185
%     vint97          -40.835      5.0159     -8.1411    1.7097e-14
%     vint98          -44.302      5.3861     -8.2253      9.77e-15
%     vint99          -38.688      3.3345     -11.602             0
%     vint00          -29.803      3.2463     -9.1806             0
%     vint01          -23.424      5.0601     -4.6291    5.8332e-06
%     vint02          -25.652      3.1572     -8.1249    1.8874e-14
%     vint03          -27.782      5.5369     -5.0177    9.7723e-07
%     vint04          -39.096      2.6754     -14.613             0
%     vint05          -35.575      3.4481     -10.317             0
%     vint06          -38.479       2.986     -12.887             0
%     vint07          -38.783      3.0079     -12.894             0
%     vint08          -35.157        3.24     -10.851             0
%     vint09          -31.505      6.6515     -4.7366    3.5983e-06
%     vint10            -38.8      4.9279     -7.8736    9.6811e-14
% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_irr_t1_u = corr(IRR_Buyout_more_100.irr_t1,u)
corr_log_size_t_u = corr(IRR_Buyout_more_100.log_size_t,u)
corr_seq_num_t_u = corr(IRR_Buyout_more_100.seq_num_t,u)

% corr_irr_t1_u = -8.4397e-16
% corr_log_size_t_u = -2.8032e-17
% corr_seq_num_t_u =  1.6105e-16

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest =  0.3426
% kurt_jbtest =  17.6904
% test_stat_jbtest = 2.3340e+03
% crit_val_jbtest =  35.4789
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% there are a couple of outliers


sum(reg_IRR_Buyout_more_100.Diagnostics.CooksDistance >=0.05);
Outliers = IRR_Buyout_more_100(reg_IRR_Buyout_more_100.Diagnostics.CooksDistance >=0.05,:);

% MULT VENTURE LESS 0
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MULT_Venture_less_0','A2:AP165');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MULT_Venture_less_0 = table;

% Allocate imported array to column variable names
MULT_Venture_less_0.number = data(:,1);
MULT_Venture_less_0.FUNDID = data(:,2);
MULT_Venture_less_0.FIRMID = data(:,3);
MULT_Venture_less_0.teamID = stringVectors(:,1);
MULT_Venture_less_0.teamIDvintage = stringVectors(:,2);
MULT_Venture_less_0.teamIDvintagenumber = stringVectors(:,3);
MULT_Venture_less_0.vint_t = data(:,4);
MULT_Venture_less_0.vint_t1 = data(:,5);
MULT_Venture_less_0.mult_t = data(:,6);
MULT_Venture_less_0.mult_t1 = data(:,7);
MULT_Venture_less_0.STATUS = categorical(stringVectors(:,4));
MULT_Venture_less_0.FUNDMANAGER = stringVectors(:,5);
MULT_Venture_less_0.size_t = data(:,8);
MULT_Venture_less_0.size_t1 = data(:,9);
MULT_Venture_less_0.seq_num_t = data(:,10);
MULT_Venture_less_0.seq_num_t1 = data(:,11);
MULT_Venture_less_0.fundgrowtht1t = data(:,12);
MULT_Venture_less_0.Dummy_less_0 = data(:,13);
MULT_Venture_less_0.Dummy_0_100 = data(:,14);
MULT_Venture_less_0.Dummy_more_100 = data(:,15);
MULT_Venture_less_0.FundGrowthPerformance = data(:,16);
MULT_Venture_less_0.vint90 = data(:,17);
MULT_Venture_less_0.vint91 = data(:,18);
MULT_Venture_less_0.vint92 = data(:,19);
MULT_Venture_less_0.vint93 = data(:,20);
MULT_Venture_less_0.vint94 = data(:,21);
MULT_Venture_less_0.vint95 = data(:,22);
MULT_Venture_less_0.vint96 = data(:,23);
MULT_Venture_less_0.vint97 = data(:,24);
MULT_Venture_less_0.vint98 = data(:,25);
MULT_Venture_less_0.vint99 = data(:,26);
MULT_Venture_less_0.vint00 = data(:,27);
MULT_Venture_less_0.vint01 = data(:,28);
MULT_Venture_less_0.vint02 = data(:,29);
MULT_Venture_less_0.vint03 = data(:,30);
MULT_Venture_less_0.vint04 = data(:,31);
MULT_Venture_less_0.vint05 = data(:,32);
MULT_Venture_less_0.vint06 = data(:,33);
MULT_Venture_less_0.vint07 = data(:,34);
MULT_Venture_less_0.vint08 = data(:,35);
MULT_Venture_less_0.vint09 = data(:,36);
MULT_Venture_less_0.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;


% Log (size)

MULT_Venture_less_0.log_size_t = log(MULT_Venture_less_0.size_t);

% Linear regression 

reg_MULT_Venture_less_0 = fitlm(MULT_Venture_less_0, 'mult_t ~ mult_t1 + log_size_t + seq_num_t + vint91 + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')

% 
% reg_MULT_Venture_less_0 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 23 terms in 22 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE         tStat       pValue 
%                    __________    ________    _________    ________
% 
%     (Intercept)         3.045      1.4238       2.1386    0.034192
%     mult_t1         -0.077327     0.11152     -0.69336     0.48922
%     seq_num_t       -0.020813    0.041387      -0.5029     0.61582
%     vint91            -1.8109      1.9153     -0.94552     0.34601
%     vint92            -1.1289      1.5621     -0.72269     0.47106
%     vint94           -0.86237      1.9042     -0.45287     0.65134
%     vint95            0.54792      1.6664      0.32881     0.74279
%     vint96            -1.3822      1.9105     -0.72349     0.47058
%     vint97           -0.48197      1.5134     -0.31847      0.7506
%     vint98            -1.3415      1.4699     -0.91259     0.36302
%     vint99            -1.6965      1.4822      -1.1446     0.25432
%     vint00            -1.8596      1.4121      -1.3168     0.19003
%     vint01            -1.4997      1.4123      -1.0619      0.2901
%     vint02            -1.4578      1.4264       -1.022     0.30853
%     vint03            -1.6937      1.4153      -1.1967     0.23344
%     vint04            -1.4069      1.4352     -0.98024     0.32865
%     vint05             -1.421      1.4077      -1.0095     0.31448
%     vint06            -1.5905      1.3955      -1.1397     0.25635
%     vint07            -1.8096      1.4048      -1.2881     0.19981
%     vint08            0.16599      1.4308      0.11601     0.90781
%     vint09           -0.90545      1.4081     -0.64303     0.52125
%     vint10            -1.2674       1.403     -0.90338     0.36787
%     log_size_t     0.00076318    0.081382    0.0093778     0.99253
% 
% 
% Number of observations: 164, Error degrees of freedom: 141
% Root Mean Squared Error: 1.35
% R-squared: 0.149,  Adjusted R-Squared 0.0157
% F-statistic vs. constant model: 1.12, p-value = 0.335


% DIAGNOSTICS of CLRM assumptions

T = reg_MULT_Venture_less_0.NumObservations;
k = reg_MULT_Venture_less_0.NumEstimatedCoefficients;
u = reg_MULT_Venture_less_0.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_MULT_Venture_less_0.Residuals.Raw);
Stdev_u = std(reg_MULT_Venture_less_0.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -9.8443e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9769

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

MULT_Venture_less_0.uRaw = u; 
MULT_Venture_less_0.uRaw2 = u.^2;


% Create a table with the constant and squared terms
MULT_Venture_less_0_White = table(MULT_Venture_less_0.mult_t, MULT_Venture_less_0.mult_t1, MULT_Venture_less_0.log_size_t,...
    MULT_Venture_less_0.seq_num_t, MULT_Venture_less_0.vint91, MULT_Venture_less_0.vint92,  MULT_Venture_less_0.vint94,...
    MULT_Venture_less_0.vint95, MULT_Venture_less_0.vint96, MULT_Venture_less_0.vint97, MULT_Venture_less_0.vint98, MULT_Venture_less_0.vint99,...
    MULT_Venture_less_0.vint00, MULT_Venture_less_0.vint01, MULT_Venture_less_0.vint02, MULT_Venture_less_0.vint03, MULT_Venture_less_0.vint04,...
    MULT_Venture_less_0.vint05, MULT_Venture_less_0.vint06, MULT_Venture_less_0.vint07, MULT_Venture_less_0.vint08, MULT_Venture_less_0.vint09, MULT_Venture_less_0.vint10,...
    MULT_Venture_less_0.mult_t1.^2, MULT_Venture_less_0.log_size_t.^2, MULT_Venture_less_0.uRaw2);

 
MULT_Venture_less_0_White.Properties.VariableNames={'mult_t','mult_t1','log_size_t','seq_num_t',...
    'vint91','vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'mult_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_MULT_Venture_less_0_White =fitlm(MULT_Venture_less_0_White,'uRaw2 ~ mult_t1 + log_size_t + seq_num_t + vint91 + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + mult_t1sq + log_size_tsq')


% reg_MULT_Venture_less_0_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                      Estimate       SE         tStat        pValue 
%                     __________    _______    __________    ________
% 
%     (Intercept)         11.921      14.73       0.80933     0.41971
%     mult_t1            -4.5677     2.2184        -2.059    0.041358
%     log_size_t         -1.1573     3.5932      -0.32209     0.74786
%     seq_num_t       0.00013223    0.35208    0.00037555      0.9997
%     vint91             -4.2828     16.313      -0.26253      0.7933
%     vint92             -2.2231     13.275      -0.16746     0.86725
%     vint94            -0.23934     16.161      -0.01481     0.98821
%     vint95              -3.074     14.455      -0.21266     0.83191
%     vint96             -2.5803     16.258      -0.15871     0.87413
%     vint97              1.2333     12.867      0.095854     0.92377
%     vint98             -4.4396      12.59      -0.35265     0.72489
%     vint99             -2.6601     12.802      -0.20778      0.8357
%     vint00             -3.6547     12.098      -0.30208     0.76305
%     vint01             -3.9206     12.111      -0.32373     0.74663
%     vint02              -3.711     12.203       -0.3041      0.7615
%     vint03             -3.9452      12.11      -0.32579     0.74508
%     vint04              -4.341     12.453       -0.3486     0.72792
%     vint05             -3.0529     12.067        -0.253     0.80064
%     vint06             -2.8677     11.983      -0.23932     0.81121
%     vint07             -2.8348         12      -0.23623      0.8136
%     vint08              14.961     12.241        1.2223     0.22368
%     vint09             -1.9876      12.09       -0.1644     0.86965
%     vint10             -2.5738     12.004      -0.21441     0.83054
%     mult_t1sq          0.56604    0.33454         1.692    0.092884
%     log_size_tsq      0.069684    0.42183       0.16519     0.86903
% 
% 
% Number of observations: 164, Error degrees of freedom: 139
% Root Mean Squared Error: 11.4
% R-squared: 0.169,  Adjusted R-Squared 0.025
% F-statistic vs. constant model: 1.17, p-value = 0.276
% 


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_MULT_Venture_less_0_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  27.6429, Crit_Val_e = 33.9244, P_e =  0.1878,
%reject_e = 0

% Cannot reject H0 at 5%, so there is no heteroscedasticity
% No, the second assumption of CLRM is not violated.





%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(MULT_Venture_less_0.mult_t1,u)
corr_log_size_t_u = corr(MULT_Venture_less_0.log_size_t,u)
corr_seq_num_t_u = corr(MULT_Venture_less_0.seq_num_t,u)

% corr_mult_t1_u =  -1.6448e-17
% corr_log_size_t_u =  1.4404e-16
% corr_seq_num_t_u =  -6.5868e-16

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 5.6379
% kurt_jbtest = 55.1113
% test_stat_jbtest = 1.9425e+04
% crit_val_jbtest = 36.7807
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% there is one outlier with multiple = 15. Rather than that normal
% distribution.


sum(reg_MULT_Venture_less_0.Diagnostics.CooksDistance >=0.05);
Outliers = MULT_Venture_less_0(reg_MULT_Venture_less_0.Diagnostics.CooksDistance >=0.05,:);

% MULT VENTURE 0-100
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MULT_Venture_0_100','A2:AP257');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MULT_Venture_0_100 = table;

% Allocate imported array to column variable names
MULT_Venture_0_100.number = data(:,1);
MULT_Venture_0_100.FUNDID = data(:,2);
MULT_Venture_0_100.FIRMID = data(:,3);
MULT_Venture_0_100.teamID = stringVectors(:,1);
MULT_Venture_0_100.teamIDvintage = stringVectors(:,2);
MULT_Venture_0_100.teamIDvintagenumber = stringVectors(:,3);
MULT_Venture_0_100.vint_t = data(:,4);
MULT_Venture_0_100.vint_t1 = data(:,5);
MULT_Venture_0_100.mult_t = data(:,6);
MULT_Venture_0_100.mult_t1 = data(:,7);
MULT_Venture_0_100.STATUS = categorical(stringVectors(:,4));
MULT_Venture_0_100.FUNDMANAGER = stringVectors(:,5);
MULT_Venture_0_100.size_t = data(:,8);
MULT_Venture_0_100.size_t1 = data(:,9);
MULT_Venture_0_100.seq_num_t = data(:,10);
MULT_Venture_0_100.seq_num_t1 = data(:,11);
MULT_Venture_0_100.fundgrowtht1t = data(:,12);
MULT_Venture_0_100.Dummy_less_0 = data(:,13);
MULT_Venture_0_100.Dummy_0_100 = data(:,14);
MULT_Venture_0_100.Dummy_more_100 = data(:,15);
MULT_Venture_0_100.FundGrowthPerformance = data(:,16);
MULT_Venture_0_100.vint90 = data(:,17);
MULT_Venture_0_100.vint91 = data(:,18);
MULT_Venture_0_100.vint92 = data(:,19);
MULT_Venture_0_100.vint93 = data(:,20);
MULT_Venture_0_100.vint94 = data(:,21);
MULT_Venture_0_100.vint95 = data(:,22);
MULT_Venture_0_100.vint96 = data(:,23);
MULT_Venture_0_100.vint97 = data(:,24);
MULT_Venture_0_100.vint98 = data(:,25);
MULT_Venture_0_100.vint99 = data(:,26);
MULT_Venture_0_100.vint00 = data(:,27);
MULT_Venture_0_100.vint01 = data(:,28);
MULT_Venture_0_100.vint02 = data(:,29);
MULT_Venture_0_100.vint03 = data(:,30);
MULT_Venture_0_100.vint04 = data(:,31);
MULT_Venture_0_100.vint05 = data(:,32);
MULT_Venture_0_100.vint06 = data(:,33);
MULT_Venture_0_100.vint07 = data(:,34);
MULT_Venture_0_100.vint08 = data(:,35);
MULT_Venture_0_100.vint09 = data(:,36);
MULT_Venture_0_100.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

MULT_Venture_0_100.log_size_t = log(MULT_Venture_0_100.size_t);

% Linear regression 

reg_MULT_Venture_0_100 = fitlm(MULT_Venture_0_100, 'mult_t ~ mult_t1 + log_size_t + seq_num_t + vint91 + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')

% reg_MULT_Venture_0_100 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 23 terms in 22 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)       0.6236      3.1008      0.20111       0.84079
%     mult_t1          0.19726    0.053812       3.6656    0.00030558
%     seq_num_t      -0.029048     0.09105     -0.31904       0.74998
%     vint91           0.45988      4.2161      0.10908       0.91323
%     vint92            1.4523      4.2136      0.34467       0.73065
%     vint94            6.0727       3.125       1.9433      0.053187
%     vint95            7.7481      3.2668       2.3717      0.018518
%     vint96            4.6949      3.1348       1.4977       0.13557
%     vint97            1.3469      3.0566      0.44065       0.65988
%     vint98           0.42534      3.0495      0.13948       0.88919
%     vint99          -0.41037      3.0695     -0.13369       0.89376
%     vint00         -0.042374       3.079    -0.013762       0.98903
%     vint01           0.15799      3.0518     0.051768       0.95876
%     vint02           0.18121      3.1157     0.058159       0.95367
%     vint03           0.17007      3.2253      0.05273       0.95799
%     vint04           0.12006      3.1012     0.038714       0.96915
%     vint05           0.82304      3.1316      0.26282       0.79292
%     vint06           0.45075      3.0572      0.14744       0.88291
%     vint07           0.83769      3.0477      0.27486       0.78366
%     vint08           0.95416       3.039      0.31397       0.75382
%     vint09           0.22616      3.2247     0.070134       0.94415
%     vint10            0.6612      3.1443      0.21029       0.83363
%     log_size_t      0.045735     0.18888      0.24214       0.80888
% 
% 
% Number of observations: 256, Error degrees of freedom: 233
% Root Mean Squared Error: 2.98
% R-squared: 0.32,  Adjusted R-Squared 0.256
% F-statistic vs. constant model: 4.99, p-value = 8.16e-11
% 

% DIAGNOSTICS of CLRM assumptions

T = reg_MULT_Venture_0_100.NumObservations;
k = reg_MULT_Venture_0_100.NumEstimatedCoefficients;
u = reg_MULT_Venture_0_100.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_MULT_Venture_0_100.Residuals.Raw);
Stdev_u = std(reg_MULT_Venture_0_100.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -2.4764e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9702

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

MULT_Venture_0_100.uRaw = u; 
MULT_Venture_0_100.uRaw2 = u.^2;


% Create a table with the constant and squared terms
MULT_Venture_0_100_White = table(MULT_Venture_0_100.mult_t, MULT_Venture_0_100.mult_t1, MULT_Venture_0_100.log_size_t,...
    MULT_Venture_0_100.seq_num_t, MULT_Venture_0_100.vint91, MULT_Venture_0_100.vint92,  MULT_Venture_0_100.vint94,...
    MULT_Venture_0_100.vint95, MULT_Venture_0_100.vint96, MULT_Venture_0_100.vint97, MULT_Venture_0_100.vint98, MULT_Venture_0_100.vint99,...
    MULT_Venture_0_100.vint00, MULT_Venture_0_100.vint01, MULT_Venture_0_100.vint02, MULT_Venture_0_100.vint03, MULT_Venture_0_100.vint04,...
    MULT_Venture_0_100.vint05, MULT_Venture_0_100.vint06, MULT_Venture_0_100.vint07, MULT_Venture_0_100.vint08, MULT_Venture_0_100.vint09, MULT_Venture_0_100.vint10,...
    MULT_Venture_0_100.mult_t1.^2, MULT_Venture_0_100.log_size_t.^2, MULT_Venture_0_100.uRaw2);

 
MULT_Venture_0_100_White.Properties.VariableNames={'mult_t','mult_t1','log_size_t','seq_num_t',...
    'vint91','vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'mult_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_MULT_Venture_0_100_White =fitlm(MULT_Venture_0_100_White,'uRaw2 ~ mult_t1 + log_size_t + seq_num_t + vint91 + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + mult_t1sq + log_size_tsq')

% 
% reg_MULT_Venture_0_100_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 25 terms in 24 predictors]
% 
% Estimated Coefficients:
%                      Estimate        SE         tStat        pValue 
%                     __________    ________    __________    ________
% 
%     (Intercept)         -7.485      57.433      -0.13033     0.89642
%     mult_t1              4.243      1.7429        2.4344    0.015676
%     log_size_t         0.94208        15.4      0.061174     0.95127
%     seq_num_t          -0.3959      1.3376      -0.29598     0.76751
%     vint91               0.407      61.189     0.0066515      0.9947
%     vint92              1.2499      61.156      0.020439     0.98371
%     vint94              71.218       45.39         1.569     0.11801
%     vint95              30.709      47.557       0.64575     0.51908
%     vint96              36.816      45.838       0.80319     0.42269
%     vint97             -4.5692      44.388      -0.10294      0.9181
%     vint98            -0.52888      44.423     -0.011905     0.99051
%     vint99             -6.1369      44.683      -0.13734     0.89088
%     vint00             -2.8148      44.777     -0.062863     0.94993
%     vint01              1.1801      44.451      0.026549     0.97884
%     vint02            -0.38622      45.369    -0.0085128     0.99322
%     vint03             -1.0169       46.84      -0.02171      0.9827
%     vint04             -1.4658      45.038     -0.032546     0.97406
%     vint05             -0.1582      45.521    -0.0034752     0.99723
%     vint06             -1.2722      44.466     -0.028612      0.9772
%     vint07            -0.29055      44.267    -0.0065636     0.99477
%     vint08            -0.19248      44.206    -0.0043542     0.99653
%     vint09             -1.8434      46.855     -0.039342     0.96865
%     vint10             -4.3782      45.702     -0.095799     0.92376
%     mult_t1sq         -0.07984    0.051252       -1.5578     0.12065
%     log_size_tsq    -0.0072578      1.6298    -0.0044533     0.99645
% 
% 
% Number of observations: 256, Error degrees of freedom: 231
% Root Mean Squared Error: 43.2
% R-squared: 0.187,  Adjusted R-Squared 0.102
% F-statistic vs. constant model: 2.21, p-value = 0.00146



% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_MULT_Venture_0_100_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =   47.7493, Crit_Val_e = 33.9244, P_e = 0.0012,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [MULT_Venture_0_100.mult_t1 MULT_Venture_0_100.log_size_t MULT_Venture_0_100.seq_num_t ...
    MULT_Venture_0_100.vint91 MULT_Venture_0_100.vint92 MULT_Venture_0_100.vint94 MULT_Venture_0_100.vint95 MULT_Venture_0_100.vint96 MULT_Venture_0_100.vint97 MULT_Venture_0_100.vint98 MULT_Venture_0_100.vint99 ...
    MULT_Venture_0_100.vint00 MULT_Venture_0_100.vint01 MULT_Venture_0_100.vint02 MULT_Venture_0_100.vint03 MULT_Venture_0_100.vint04 ...
    MULT_Venture_0_100.vint05 MULT_Venture_0_100.vint06 MULT_Venture_0_100.vint07 MULT_Venture_0_100.vint08 MULT_Venture_0_100.vint09 MULT_Venture_0_100.vint10];

y = MULT_Venture_0_100.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','log_size_t','seq_num_t',...
    'vint91','vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White

% White =
% 
%   23×4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       0.6236     0.58008       1.075       0.28339
%     mult_t1          0.19726    0.087212      2.2618      0.024555
%     log_size_t      0.045735      0.1229     0.37213       0.71011
%     seq_num_t      -0.029048    0.027062     -1.0734       0.28411
%     vint91           0.45988    0.060537      7.5967    5.8309e-13
%     vint92            1.4523    0.081395      17.842             0
%     vint94            6.0727      2.7352      2.2202      0.027286
%     vint95            7.7481      2.2579      3.4315    0.00070049
%     vint96            4.6949      2.4073      1.9503      0.052242
%     vint97            1.3469     0.49248      2.7349     0.0066793
%     vint98           0.42534     0.47068     0.90367       0.36703
%     vint99          -0.41037     0.30497     -1.3456       0.17963
%     vint00         -0.042374     0.38006    -0.11149       0.91131
%     vint01           0.15799     0.13759      1.1483       0.25194
%     vint02           0.18121     0.17546      1.0328        0.3027
%     vint03           0.17007     0.16828      1.0107       0.31314
%     vint04           0.12006     0.17381     0.69074       0.49036
%     vint05           0.82304     0.65153      1.2633       0.20766
%     vint06           0.45075     0.22464      2.0065      0.045863
%     vint07           0.83769     0.24206      3.4607    0.00063185
%     vint08           0.95416     0.37121      2.5704       0.01073
%     vint09           0.22616      0.1491      1.5168       0.13056
%     vint10            0.6612     0.30266      2.1846       0.02983
% 



%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(MULT_Venture_0_100.mult_t1,u)
corr_log_size_t_u = corr(MULT_Venture_0_100.log_size_t,u)
corr_seq_num_t_u = corr(MULT_Venture_0_100.seq_num_t,u)

% corr_mult_t1_u =  1.0408e-17
% corr_log_size_t_u =  2.2515e-17
% corr_seq_num_t_u =  4.8839e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 4.1367
% kurt_jbtest = 32.5409
% test_stat_jbtest = 1.0039e+04
% crit_val_jbtest = 36.7807
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% there are several outliers with big multiples


sum(reg_MULT_Venture_0_100.Diagnostics.CooksDistance >=0.05);
Outliers = MULT_Venture_0_100(reg_MULT_Venture_0_100.Diagnostics.CooksDistance >=0.05,:);

% MULT VENTURE MORE 100
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MULT_Venture_more_100','A2:AP182');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MULT_Venture_more_100 = table;

% Allocate imported array to column variable names
MULT_Venture_more_100.number = data(:,1);
MULT_Venture_more_100.FUNDID = data(:,2);
MULT_Venture_more_100.FIRMID = data(:,3);
MULT_Venture_more_100.teamID = stringVectors(:,1);
MULT_Venture_more_100.teamIDvintage = stringVectors(:,2);
MULT_Venture_more_100.teamIDvintagenumber = stringVectors(:,3);
MULT_Venture_more_100.vint_t = data(:,4);
MULT_Venture_more_100.vint_t1 = data(:,5);
MULT_Venture_more_100.mult_t = data(:,6);
MULT_Venture_more_100.mult_t1 = data(:,7);
MULT_Venture_more_100.STATUS = categorical(stringVectors(:,4));
MULT_Venture_more_100.FUNDMANAGER = stringVectors(:,5);
MULT_Venture_more_100.size_t = data(:,8);
MULT_Venture_more_100.size_t1 = data(:,9);
MULT_Venture_more_100.seq_num_t = data(:,10);
MULT_Venture_more_100.seq_num_t1 = data(:,11);
MULT_Venture_more_100.fundgrowtht1t = data(:,12);
MULT_Venture_more_100.Dummy_less_0 = data(:,13);
MULT_Venture_more_100.Dummy_0_100 = data(:,14);
MULT_Venture_more_100.Dummy_more_100 = data(:,15);
MULT_Venture_more_100.FundGrowthPerformance = data(:,16);
MULT_Venture_more_100.vint90 = data(:,17);
MULT_Venture_more_100.vint91 = data(:,18);
MULT_Venture_more_100.vint92 = data(:,19);
MULT_Venture_more_100.vint93 = data(:,20);
MULT_Venture_more_100.vint94 = data(:,21);
MULT_Venture_more_100.vint95 = data(:,22);
MULT_Venture_more_100.vint96 = data(:,23);
MULT_Venture_more_100.vint97 = data(:,24);
MULT_Venture_more_100.vint98 = data(:,25);
MULT_Venture_more_100.vint99 = data(:,26);
MULT_Venture_more_100.vint00 = data(:,27);
MULT_Venture_more_100.vint01 = data(:,28);
MULT_Venture_more_100.vint02 = data(:,29);
MULT_Venture_more_100.vint03 = data(:,30);
MULT_Venture_more_100.vint04 = data(:,31);
MULT_Venture_more_100.vint05 = data(:,32);
MULT_Venture_more_100.vint06 = data(:,33);
MULT_Venture_more_100.vint07 = data(:,34);
MULT_Venture_more_100.vint08 = data(:,35);
MULT_Venture_more_100.vint09 = data(:,36);
MULT_Venture_more_100.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;


% Log (size)

MULT_Venture_more_100.log_size_t = log(MULT_Venture_more_100.size_t);

% Linear regression 

reg_MULT_Venture_more_100 = fitlm(MULT_Venture_more_100, 'mult_t ~ mult_t1 + log_size_t + seq_num_t + vint92 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')

% reg_MULT_Venture_more_100 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 21 terms in 20 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat       pValue  
%                    _________    ________    _________    _________
% 
%     (Intercept)       1.0126     0.72707       1.3927      0.16563
%     mult_t1         0.015026    0.021376      0.70293      0.48312
%     seq_num_t      -0.068451    0.050489      -1.3558      0.17708
%     vint92           0.44896      1.1322      0.39653      0.69224
%     vint95             2.413     0.85261       2.8301    0.0052503
%     vint96         -0.026472       0.803    -0.032966      0.97374
%     vint97           0.57375     0.71741      0.79975      0.42504
%     vint98          -0.19204     0.71629      -0.2681      0.78897
%     vint99          -0.67918     0.68141     -0.99674       0.3204
%     vint00          -0.44198     0.67129      -0.6584      0.51123
%     vint01          -0.25488     0.71849     -0.35474      0.72325
%     vint02           -0.3053     0.77572     -0.39357      0.69442
%     vint03          -0.07624     0.80357    -0.094877      0.92453
%     vint04           0.40324     0.91639      0.44002      0.66051
%     vint05         -0.027427      0.7204    -0.038071      0.96968
%     vint06           0.61875     0.71384      0.86679      0.38736
%     vint07           0.41534     0.71567      0.58036      0.56249
%     vint08           0.19577       0.714      0.27418       0.7843
%     vint09          -0.24827      0.8444     -0.29402      0.76912
%     vint10          -0.18713     0.92726     -0.20181      0.84032
%     log_size_t      0.090721    0.074281       1.2213      0.22376
% 
% 
% Number of observations: 181, Error degrees of freedom: 160
% Root Mean Squared Error: 0.924
% R-squared: 0.257,  Adjusted R-Squared 0.164
% F-statistic vs. constant model: 2.76, p-value = 0.00022



% DIAGNOSTICS of CLRM assumptions

T = reg_MULT_Venture_more_100.NumObservations;
k = reg_MULT_Venture_more_100.NumEstimatedCoefficients;
u = reg_MULT_Venture_more_100.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_MULT_Venture_more_100.Residuals.Raw);
Stdev_u = std(reg_MULT_Venture_more_100.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -4.5960e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =    1.9749

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

MULT_Venture_more_100.uRaw = u; 
MULT_Venture_more_100.uRaw2 = u.^2;


% Create a table with the constant and squared terms
MULT_Venture_more_100_White = table(MULT_Venture_more_100.mult_t, MULT_Venture_more_100.mult_t1, MULT_Venture_more_100.log_size_t,...
    MULT_Venture_more_100.seq_num_t, MULT_Venture_more_100.vint92,...
    MULT_Venture_more_100.vint95, MULT_Venture_more_100.vint96, MULT_Venture_more_100.vint97, MULT_Venture_more_100.vint98, MULT_Venture_more_100.vint99,...
    MULT_Venture_more_100.vint00, MULT_Venture_more_100.vint01, MULT_Venture_more_100.vint02, MULT_Venture_more_100.vint03, MULT_Venture_more_100.vint04,...
    MULT_Venture_more_100.vint05, MULT_Venture_more_100.vint06, MULT_Venture_more_100.vint07, MULT_Venture_more_100.vint08, MULT_Venture_more_100.vint09, MULT_Venture_more_100.vint10,...
    MULT_Venture_more_100.mult_t1.^2, MULT_Venture_more_100.log_size_t.^2, MULT_Venture_more_100.uRaw2);

 
MULT_Venture_more_100_White.Properties.VariableNames={'mult_t','mult_t1','log_size_t','seq_num_t',...
    'vint92','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'mult_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_MULT_Venture_more_100_White =fitlm(MULT_Venture_more_100_White,'uRaw2 ~ mult_t1 + log_size_t + seq_num_t + vint92 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + mult_t1sq + log_size_tsq')

% reg_MULT_Venture_more_100_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 23 terms in 22 predictors]
% 
% Estimated Coefficients:
%                      Estimate         SE          tStat        pValue  
%                     ___________    _________    __________    _________
% 
%     (Intercept)         -1.6654       3.7095      -0.44895      0.65409
%     mult_t1            0.029516      0.14273       0.20679      0.83644
%     log_size_t          0.69509       1.2883       0.53955      0.59027
%     seq_num_t          -0.21007      0.13393       -1.5685      0.11876
%     vint92             -0.15041       2.9522     -0.050949      0.95943
%     vint95               7.3872       2.2328        3.3084    0.0011619
%     vint96              0.14307        2.097      0.068226      0.94569
%     vint97               3.2079       1.8781        1.7081     0.089587
%     vint98              0.53098       1.8637        0.2849      0.77609
%     vint99             -0.12336       1.7786     -0.069356      0.94479
%     vint00             0.052791       1.7463      0.030231      0.97592
%     vint01              0.24394       1.8707        0.1304      0.89642
%     vint02              0.20908       2.0175       0.10363      0.91759
%     vint03               1.2288         2.09       0.58796       0.5574
%     vint04               1.9919       2.3838       0.83557      0.40466
%     vint05              0.94316       1.8756       0.50286      0.61576
%     vint06               2.3315       1.8571        1.2555      0.21115
%     vint07              0.37172       1.8618       0.19966      0.84201
%     vint08              0.44467       1.8577       0.23937      0.81113
%     vint09           -0.0098886       2.1983    -0.0044982      0.99642
%     vint10              0.59772       2.4128       0.24773      0.80467
%     mult_t1sq       -0.00085215    0.0052554      -0.16215       0.8714
%     log_size_tsq      -0.045931      0.12499      -0.36747      0.71376
% 
% 
% Number of observations: 181, Error degrees of freedom: 158
% Root Mean Squared Error: 2.4
% R-squared: 0.235,  Adjusted R-Squared 0.128
% F-statistic vs. constant model: 2.2, p-value = 0.00276


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_MULT_Venture_more_100_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =    42.5006, Crit_Val_e = 31.4104, P_e =  0.0024,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [MULT_Venture_more_100.mult_t1 MULT_Venture_more_100.log_size_t MULT_Venture_more_100.seq_num_t ...
    MULT_Venture_more_100.vint92 MULT_Venture_more_100.vint95 MULT_Venture_more_100.vint96 MULT_Venture_more_100.vint97 MULT_Venture_more_100.vint98 MULT_Venture_more_100.vint99 ...
    MULT_Venture_more_100.vint00 MULT_Venture_more_100.vint01 MULT_Venture_more_100.vint02 MULT_Venture_more_100.vint03 MULT_Venture_more_100.vint04 ...
    MULT_Venture_more_100.vint05 MULT_Venture_more_100.vint06 MULT_Venture_more_100.vint07 MULT_Venture_more_100.vint08 MULT_Venture_more_100.vint09 MULT_Venture_more_100.vint10];

y = MULT_Venture_more_100.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','log_size_t','seq_num_t',...
    'vint92','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White

% White =
% 
%   21×4 table
% 
%                      beta          SE         tstat         p    
%                    _________    ________    _________    ________
% 
%     (Intercept)       1.0126     0.40905       2.4755    0.014234
%     mult_t1         0.015026    0.016233      0.92563     0.35588
%     log_size_t      0.090721    0.078118       1.1613     0.24706
%     seq_num_t      -0.068451    0.052615       -1.301     0.19493
%     vint92           0.44896     0.26159       1.7163    0.087842
%     vint95             2.413      1.4509       1.6631    0.098048
%     vint96         -0.026472     0.34075    -0.077688     0.93816
%     vint97           0.57375     0.70176      0.81759     0.41468
%     vint98          -0.19204     0.35947     -0.53422     0.59385
%     vint99          -0.67918      0.2706      -2.5099    0.012964
%     vint00          -0.44198     0.26187      -1.6878    0.093196
%     vint01          -0.25488     0.29376     -0.86762     0.38676
%     vint02           -0.3053     0.34749      -0.8786      0.3808
%     vint03          -0.07624      0.6014     -0.12677     0.89926
%     vint04           0.40324     0.72937      0.55285     0.58105
%     vint05         -0.027427     0.40593    -0.067565     0.94621
%     vint06           0.61875     0.54353       1.1384     0.25648
%     vint07           0.41534     0.33607       1.2359     0.21813
%     vint08           0.19577     0.27968      0.69997     0.48485
%     vint09          -0.24827     0.29598     -0.83881      0.4027
%     vint10          -0.18713     0.47217     -0.39632     0.69234
% 
% 



%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(MULT_Venture_more_100.mult_t1,u)
corr_log_size_t_u = corr(MULT_Venture_more_100.log_size_t,u)
corr_seq_num_t_u = corr(MULT_Venture_more_100.seq_num_t,u)

% corr_mult_t1_u =  -1.9101e-17
% corr_log_size_t_u =  4.3256e-16
% corr_seq_num_t_u = 4.0396e-18

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 1.9911
% kurt_jbtest = 12.4319
% test_stat_jbtest = 790.5102
% crit_val_jbtest = 34.1696
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% there are several outliers with big multiples


sum(reg_MULT_Venture_more_100.Diagnostics.CooksDistance >=0.05);
Outliers = MULT_Venture_more_100(reg_MULT_Venture_more_100.Diagnostics.CooksDistance >=0.05,:);

% MULT BUYOUT LESS 0
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MULT_Buyout_less_0','A2:AP125');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MULT_Buyout_less_0 = table;

% Allocate imported array to column variable names
MULT_Buyout_less_0.number = data(:,1);
MULT_Buyout_less_0.FUNDID = data(:,2);
MULT_Buyout_less_0.FIRMID = data(:,3);
MULT_Buyout_less_0.teamID = stringVectors(:,1);
MULT_Buyout_less_0.teamIDvintage = stringVectors(:,2);
MULT_Buyout_less_0.teamIDvintagenumber = stringVectors(:,3);
MULT_Buyout_less_0.vint_t = data(:,4);
MULT_Buyout_less_0.vint_t1 = data(:,5);
MULT_Buyout_less_0.mult_t = data(:,6);
MULT_Buyout_less_0.mult_t1 = data(:,7);
MULT_Buyout_less_0.STATUS = categorical(stringVectors(:,4));
MULT_Buyout_less_0.FUNDMANAGER = stringVectors(:,5);
MULT_Buyout_less_0.size_t = data(:,8);
MULT_Buyout_less_0.size_t1 = data(:,9);
MULT_Buyout_less_0.seq_num_t = data(:,10);
MULT_Buyout_less_0.seq_num_t1 = data(:,11);
MULT_Buyout_less_0.fundgrowtht1t = data(:,12);
MULT_Buyout_less_0.Dummy_less_0 = data(:,13);
MULT_Buyout_less_0.Dummy_0_100 = data(:,14);
MULT_Buyout_less_0.Dummy_more_100 = data(:,15);
MULT_Buyout_less_0.FundGrowthPerformance = data(:,16);
MULT_Buyout_less_0.vint90 = data(:,17);
MULT_Buyout_less_0.vint91 = data(:,18);
MULT_Buyout_less_0.vint92 = data(:,19);
MULT_Buyout_less_0.vint93 = data(:,20);
MULT_Buyout_less_0.vint94 = data(:,21);
MULT_Buyout_less_0.vint95 = data(:,22);
MULT_Buyout_less_0.vint96 = data(:,23);
MULT_Buyout_less_0.vint97 = data(:,24);
MULT_Buyout_less_0.vint98 = data(:,25);
MULT_Buyout_less_0.vint99 = data(:,26);
MULT_Buyout_less_0.vint00 = data(:,27);
MULT_Buyout_less_0.vint01 = data(:,28);
MULT_Buyout_less_0.vint02 = data(:,29);
MULT_Buyout_less_0.vint03 = data(:,30);
MULT_Buyout_less_0.vint04 = data(:,31);
MULT_Buyout_less_0.vint05 = data(:,32);
MULT_Buyout_less_0.vint06 = data(:,33);
MULT_Buyout_less_0.vint07 = data(:,34);
MULT_Buyout_less_0.vint08 = data(:,35);
MULT_Buyout_less_0.vint09 = data(:,36);
MULT_Buyout_less_0.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;


% Log (size)

MULT_Buyout_less_0.log_size_t = log(MULT_Buyout_less_0.size_t);

% Linear regression 

reg_MULT_Buyout_less_0 = fitlm(MULT_Buyout_less_0, 'mult_t ~ mult_t1 + log_size_t + seq_num_t + vint92 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')

% reg_MULT_Buyout_less_0 = 


% Linear regression model:
%     mult_t ~ [Linear formula with 21 terms in 20 predictors]
% 
% Estimated Coefficients:
%                    Estimate       SE        tStat        pValue  
%                    ________    ________    ________    __________
% 
%     (Intercept)       5.161     0.94714       5.449    3.4742e-07
%     mult_t1         0.18346     0.13438      1.3653       0.17514
%     seq_num_t      -0.01289    0.054945    -0.23461       0.81498
%     vint92          -3.0333      1.0722     -2.8291     0.0056124
%     vint95          -3.5193      1.0633     -3.3098     0.0012872
%     vint96          -1.7146      1.2447     -1.3775       0.17135
%     vint97          -3.4243     0.99676     -3.4355    0.00085387
%     vint98          -3.5388     0.99046     -3.5729    0.00053872
%     vint99          -2.6777     0.92507     -2.8946      0.004635
%     vint00          -2.7296      0.9091     -3.0026     0.0033599
%     vint01          -2.5273     0.93053      -2.716     0.0077502
%     vint02           -2.321     0.94461     -2.4571      0.015677
%     vint03          -2.8811     0.93082     -3.0952     0.0025328
%     vint04          -2.7037     0.93691     -2.8857     0.0047577
%     vint05          -3.1845     0.89782      -3.547    0.00058818
%     vint06          -2.9276     0.90291     -3.2424     0.0015977
%     vint07          -2.6922     0.89045     -3.0235     0.0031542
%     vint08           -2.928     0.89595     -3.2681      0.001472
%     vint09          -2.8901      0.9077     -3.1839     0.0019219
%     vint10          -2.2823     0.89158     -2.5598      0.011923
%     log_size_t     -0.13665    0.065886      -2.074       0.04057
% 
% 
% Number of observations: 124, Error degrees of freedom: 103
% Root Mean Squared Error: 0.85
% R-squared: 0.277,  Adjusted R-Squared 0.136
% F-statistic vs. constant model: 1.97, p-value = 0.0148
% 


% DIAGNOSTICS of CLRM assumptions

T = reg_MULT_Buyout_less_0.NumObservations;
k = reg_MULT_Buyout_less_0.NumEstimatedCoefficients;
u = reg_MULT_Buyout_less_0.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_MULT_Buyout_less_0.Residuals.Raw);
Stdev_u = std(reg_MULT_Buyout_less_0.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   -8.8956e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9833

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

MULT_Buyout_less_0.uRaw = u; 
MULT_Buyout_less_0.uRaw2 = u.^2;


% Create a table with the constant and squared terms
MULT_Buyout_less_0_White = table(MULT_Buyout_less_0.mult_t, MULT_Buyout_less_0.mult_t1, MULT_Buyout_less_0.log_size_t,...
    MULT_Buyout_less_0.seq_num_t, MULT_Buyout_less_0.vint92,...
    MULT_Buyout_less_0.vint95, MULT_Buyout_less_0.vint96, MULT_Buyout_less_0.vint97, MULT_Buyout_less_0.vint98, MULT_Buyout_less_0.vint99,...
    MULT_Buyout_less_0.vint00, MULT_Buyout_less_0.vint01, MULT_Buyout_less_0.vint02, MULT_Buyout_less_0.vint03, MULT_Buyout_less_0.vint04,...
    MULT_Buyout_less_0.vint05, MULT_Buyout_less_0.vint06, MULT_Buyout_less_0.vint07, MULT_Buyout_less_0.vint08, MULT_Buyout_less_0.vint09, MULT_Buyout_less_0.vint10,...
    MULT_Buyout_less_0.mult_t1.^2, MULT_Buyout_less_0.log_size_t.^2, MULT_Buyout_less_0.uRaw2);

 
MULT_Buyout_less_0_White.Properties.VariableNames={'mult_t','mult_t1','log_size_t','seq_num_t',...
    'vint92','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'mult_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_MULT_Buyout_less_0_White =fitlm(MULT_Buyout_less_0_White,'uRaw2 ~ mult_t1 + log_size_t + seq_num_t + vint92 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + mult_t1sq + log_size_tsq')

% reg_MULT_Buyout_less_0_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 23 terms in 22 predictors]
% 
% Estimated Coefficients:
%                     Estimate       SE        tStat       pValue 
%                     ________    ________    ________    ________
% 
%     (Intercept)       3.9681      1.8023      2.2017    0.029963
%     mult_t1         -0.53283     0.53945    -0.98772     0.32565
%     log_size_t       -1.1586     0.52624     -2.2017    0.029965
%     seq_num_t        0.02594    0.065933     0.39344     0.69483
%     vint92           0.99476       1.275      0.7802      0.4371
%     vint95           0.20421      1.3887     0.14705     0.88338
%     vint96           0.72982      1.5557     0.46914     0.63998
%     vint97           0.55622       1.185     0.46939      0.6398
%     vint98           0.60394      1.1845     0.50988     0.61125
%     vint99            1.5104      1.1029      1.3695     0.17388
%     vint00           0.82789        1.08     0.76657     0.44513
%     vint01            1.2544      1.1064      1.1338     0.25957
%     vint02            1.8347       1.119      1.6396      0.1042
%     vint03           0.82299      1.1057     0.74428     0.45843
%     vint04            1.5769      1.1122      1.4179     0.15931
%     vint05            1.0246      1.0656     0.96153     0.33858
%     vint06            1.0048      1.0703     0.93879     0.35008
%     vint07             1.153      1.0552      1.0927     0.27713
%     vint08           0.95779      1.0621     0.90182      0.3693
%     vint09           0.90144      1.0746     0.83884     0.40354
%     vint10            1.9794      1.0594      1.8684    0.064604
%     mult_t1sq       0.074282     0.13649     0.54422     0.58749
%     log_size_tsq    0.077947    0.046085      1.6914    0.093851
% 
% 
% Number of observations: 124, Error degrees of freedom: 101
% Root Mean Squared Error: 1.01
% R-squared: 0.292,  Adjusted R-Squared 0.138
% F-statistic vs. constant model: 1.89, p-value = 0.0176


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_MULT_Buyout_less_0_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =  36.1996, Crit_Val_e = 31.4104, P_e =  0.0146,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [MULT_Buyout_less_0.mult_t1 MULT_Buyout_less_0.log_size_t MULT_Buyout_less_0.seq_num_t ...
    MULT_Buyout_less_0.vint92 MULT_Buyout_less_0.vint95 MULT_Buyout_less_0.vint96 MULT_Buyout_less_0.vint97 MULT_Buyout_less_0.vint98 MULT_Buyout_less_0.vint99 ...
    MULT_Buyout_less_0.vint00 MULT_Buyout_less_0.vint01 MULT_Buyout_less_0.vint02 MULT_Buyout_less_0.vint03 MULT_Buyout_less_0.vint04 ...
    MULT_Buyout_less_0.vint05 MULT_Buyout_less_0.vint06 MULT_Buyout_less_0.vint07 MULT_Buyout_less_0.vint08 MULT_Buyout_less_0.vint09 MULT_Buyout_less_0.vint10];

y = MULT_Buyout_less_0.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','log_size_t','seq_num_t',...
    'vint92','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White

% 
% White =
% 
%   21×4 table
% 
%                      beta         SE        tstat         p     
%                    ________    ________    _______    __________
% 
%     (Intercept)       5.161     0.52555     9.8202             0
%     mult_t1         0.18346     0.12955     1.4162       0.15927
%     log_size_t     -0.13665    0.070933    -1.9264      0.056374
%     seq_num_t      -0.01289    0.053957    -0.2389       0.81158
%     vint92          -3.0333     0.32617    -9.2997    6.6613e-16
%     vint95          -3.5193     0.29057    -12.112             0
%     vint96          -1.7146     0.31943    -5.3676    3.8637e-07
%     vint97          -3.4243     0.37712    -9.0802    2.4425e-15
%     vint98          -3.5388     0.28713    -12.325             0
%     vint99          -2.6777     0.38967    -6.8717    2.8765e-10
%     vint00          -2.7296      0.2778    -9.8259             0
%     vint01          -2.5273     0.41745    -6.0542    1.6052e-08
%     vint02           -2.321     0.62408    -3.7191    0.00030343
%     vint03          -2.8811     0.22149    -13.008             0
%     vint04          -2.7037     0.43487    -6.2172    7.3342e-09
%     vint05          -3.1845     0.20612     -15.45             0
%     vint06          -2.9276     0.24099    -12.148             0
%     vint07          -2.6922     0.27427     -9.816             0
%     vint08           -2.928     0.21057    -13.905             0
%     vint09          -2.8901     0.27281    -10.594             0
%     vint10          -2.2823     0.35365    -6.4537    2.3141e-09
% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(MULT_Buyout_less_0.mult_t1,u)
corr_log_size_t_u = corr(MULT_Buyout_less_0.log_size_t,u)
corr_seq_num_t_u = corr(MULT_Buyout_less_0.seq_num_t,u)

% corr_mult_t1_u =  -3.2084e-16
% corr_log_size_t_u = 2.0775e-16
% corr_seq_num_t_u =  -7.5876e-17

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest =  0.6099
% kurt_jbtest =  4.1655
% test_stat_jbtest = 14.7065
% crit_val_jbtest =  34.1696
% p_jbtest = 0.7930
% reject_jbstest = 0

% Cannot reject H0 at 5%.
% No, the fifth assumption of the CLRM is not violated. Residuals are
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% MULT BUYOUT 0-100
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MULT_Buyout_0_100','A2:AP296');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MULT_Buyout_0_100 = table;

% Allocate imported array to column variable names
MULT_Buyout_0_100.number = data(:,1);
MULT_Buyout_0_100.FUNDID = data(:,2);
MULT_Buyout_0_100.FIRMID = data(:,3);
MULT_Buyout_0_100.teamID = stringVectors(:,1);
MULT_Buyout_0_100.teamIDvintage = stringVectors(:,2);
MULT_Buyout_0_100.teamIDvintagenumber = stringVectors(:,3);
MULT_Buyout_0_100.vint_t = data(:,4);
MULT_Buyout_0_100.vint_t1 = data(:,5);
MULT_Buyout_0_100.mult_t = data(:,6);
MULT_Buyout_0_100.mult_t1 = data(:,7);
MULT_Buyout_0_100.STATUS = categorical(stringVectors(:,4));
MULT_Buyout_0_100.FUNDMANAGER = stringVectors(:,5);
MULT_Buyout_0_100.size_t = data(:,8);
MULT_Buyout_0_100.size_t1 = data(:,9);
MULT_Buyout_0_100.seq_num_t = data(:,10);
MULT_Buyout_0_100.seq_num_t1 = data(:,11);
MULT_Buyout_0_100.fundgrowtht1t = data(:,12);
MULT_Buyout_0_100.Dummy_less_0 = data(:,13);
MULT_Buyout_0_100.Dummy_0_100 = data(:,14);
MULT_Buyout_0_100.Dummy_more_100 = data(:,15);
MULT_Buyout_0_100.FundGrowthPerformance = data(:,16);
MULT_Buyout_0_100.vint90 = data(:,17);
MULT_Buyout_0_100.vint91 = data(:,18);
MULT_Buyout_0_100.vint92 = data(:,19);
MULT_Buyout_0_100.vint93 = data(:,20);
MULT_Buyout_0_100.vint94 = data(:,21);
MULT_Buyout_0_100.vint95 = data(:,22);
MULT_Buyout_0_100.vint96 = data(:,23);
MULT_Buyout_0_100.vint97 = data(:,24);
MULT_Buyout_0_100.vint98 = data(:,25);
MULT_Buyout_0_100.vint99 = data(:,26);
MULT_Buyout_0_100.vint00 = data(:,27);
MULT_Buyout_0_100.vint01 = data(:,28);
MULT_Buyout_0_100.vint02 = data(:,29);
MULT_Buyout_0_100.vint03 = data(:,30);
MULT_Buyout_0_100.vint04 = data(:,31);
MULT_Buyout_0_100.vint05 = data(:,32);
MULT_Buyout_0_100.vint06 = data(:,33);
MULT_Buyout_0_100.vint07 = data(:,34);
MULT_Buyout_0_100.vint08 = data(:,35);
MULT_Buyout_0_100.vint09 = data(:,36);
MULT_Buyout_0_100.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

MULT_Buyout_0_100.log_size_t = log(MULT_Buyout_0_100.size_t);

% Linear regression 

reg_MULT_Buyout_0_100 = fitlm(MULT_Buyout_0_100, 'mult_t ~ mult_t1 + log_size_t + seq_num_t + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')

% reg_MULT_Buyout_0_100 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 22 terms in 21 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)        3.719     0.67856      5.4807     9.636e-08
%     mult_t1          0.21345    0.048611       4.391     1.615e-05
%     seq_num_t      -0.018112    0.026232    -0.69046       0.49049
%     vint92            -2.602     0.89097     -2.9204     0.0037872
%     vint94            -2.184     0.72629      -3.007     0.0028841
%     vint95           -2.5219     0.67731     -3.7234    0.00023873
%     vint96           -2.8609     0.68253     -4.1916    3.7447e-05
%     vint97           -2.6741     0.67203     -3.9792    8.8661e-05
%     vint98           -2.6065     0.66406      -3.925    0.00010985
%     vint99           -2.6395     0.66355     -3.9778    8.9155e-05
%     vint00           -1.9967     0.65158     -3.0644     0.0023991
%     vint01           -1.8866     0.66104     -2.8539     0.0046489
%     vint02            -2.373     0.65827     -3.6049    0.00037107
%     vint03           -2.2991     0.66124      -3.477    0.00058985
%     vint04           -2.1038     0.65023     -3.2355     0.0013639
%     vint05           -2.3188     0.64316     -3.6053    0.00037057
%     vint06           -2.5768     0.64461     -3.9975    8.2432e-05
%     vint07           -2.2577     0.65219     -3.4617    0.00062279
%     vint08           -2.1519     0.64738      -3.324     0.0010088
%     vint09           -2.1132     0.65485      -3.227     0.0014034
%     vint10           -2.5025     0.64849      -3.859    0.00014217
%     log_size_t     0.0059933    0.034324     0.17461       0.86151
% 
% 
% Number of observations: 295, Error degrees of freedom: 273
% Root Mean Squared Error: 0.626
% R-squared: 0.214,  Adjusted R-Squared 0.154
% F-statistic vs. constant model: 3.55, p-value = 6.95e-07



% DIAGNOSTICS of CLRM assumptions

T = reg_MULT_Buyout_0_100.NumObservations;
k = reg_MULT_Buyout_0_100.NumEstimatedCoefficients;
u = reg_MULT_Buyout_0_100.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_MULT_Buyout_0_100.Residuals.Raw);
Stdev_u = std(reg_MULT_Buyout_0_100.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =  6.9890e-15

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =   1.9687

% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

MULT_Buyout_0_100.uRaw = u; 
MULT_Buyout_0_100.uRaw2 = u.^2;


% Create a table with the constant and squared terms
MULT_Buyout_0_100_White = table(MULT_Buyout_0_100.mult_t, MULT_Buyout_0_100.mult_t1, MULT_Buyout_0_100.log_size_t,...
    MULT_Buyout_0_100.seq_num_t, MULT_Buyout_0_100.vint92, MULT_Buyout_0_100.vint94,...
    MULT_Buyout_0_100.vint95, MULT_Buyout_0_100.vint96, MULT_Buyout_0_100.vint97, MULT_Buyout_0_100.vint98, MULT_Buyout_0_100.vint99,...
    MULT_Buyout_0_100.vint00, MULT_Buyout_0_100.vint01, MULT_Buyout_0_100.vint02, MULT_Buyout_0_100.vint03, MULT_Buyout_0_100.vint04,...
    MULT_Buyout_0_100.vint05, MULT_Buyout_0_100.vint06, MULT_Buyout_0_100.vint07, MULT_Buyout_0_100.vint08, MULT_Buyout_0_100.vint09, MULT_Buyout_0_100.vint10,...
    MULT_Buyout_0_100.mult_t1.^2, MULT_Buyout_0_100.log_size_t.^2, MULT_Buyout_0_100.uRaw2);

 
MULT_Buyout_0_100_White.Properties.VariableNames={'mult_t','mult_t1','log_size_t','seq_num_t',...
    'vint92','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'mult_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_MULT_Buyout_0_100_White =fitlm(MULT_Buyout_0_100_White,'uRaw2 ~ mult_t1 + log_size_t + seq_num_t + vint92 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + mult_t1sq + log_size_tsq')

% reg_MULT_Buyout_0_100_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                      Estimate        SE        tStat      pValue 
%                     __________    ________    ________    _______
% 
%     (Intercept)          1.102      1.9325     0.57025    0.56898
%     mult_t1           -0.20857     0.21601    -0.96556    0.33512
%     log_size_t        -0.24357     0.47408    -0.51378    0.60782
%     seq_num_t       -0.0074112    0.049456    -0.14986    0.88099
%     vint92             0.32699      1.6448      0.1988    0.84257
%     vint94              0.8364      1.3415     0.62346     0.5335
%     vint95              1.0582      1.2488     0.84741    0.39752
%     vint96             0.64082      1.2593     0.50886    0.61126
%     vint97             0.30418      1.2401     0.24528    0.80642
%     vint98             0.54754      1.2251     0.44694    0.65528
%     vint99             0.49044       1.226     0.40005    0.68944
%     vint00             0.63342      1.2063     0.52507    0.59996
%     vint01             0.70715      1.2208     0.57928    0.56288
%     vint02             0.61788      1.2163     0.50801    0.61186
%     vint03             0.56997      1.2199     0.46722    0.64072
%     vint04             0.62322      1.1996     0.51952    0.60382
%     vint05              1.1932      1.1874      1.0049    0.31584
%     vint06             0.64668      1.1897     0.54357    0.58718
%     vint07             0.57291      1.2039     0.47587    0.63455
%     vint08             0.67044       1.195     0.56105    0.57522
%     vint09             0.57393      1.2102     0.47423    0.63572
%     vint10             0.56561      1.1979     0.47217    0.63719
%     mult_t1sq         0.044935    0.032768      1.3713    0.17142
%     log_size_tsq     0.0088734    0.035714     0.24846    0.80397
% 
% 
% Number of observations: 295, Error degrees of freedom: 271
% Root Mean Squared Error: 1.15
% R-squared: 0.0672,  Adjusted R-Squared -0.012
% F-statistic vs. constant model: 0.849, p-value = 0.667


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_MULT_Buyout_0_100_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e =    19.8267, Crit_Val_e =  32.6706, P_e =   0.5323,
%reject_e = 0

% Cannot reject H0 at 5%, so there is no heteroscedasticity
% No, the second assumption of CLRM is not violated.


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(MULT_Buyout_0_100.mult_t1,u)
corr_log_size_t_u = corr(MULT_Buyout_0_100.log_size_t,u)
corr_seq_num_t_u = corr(MULT_Buyout_0_100.seq_num_t,u)

% corr_mult_t1_u =  -1.7450e-18
% corr_log_size_t_u =  0
% corr_seq_num_t_u =  1.1176e-16

%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest =  1.4637
% kurt_jbtest =  10.9058
% test_stat_jbtest =  873.5838
% crit_val_jbtest = 35.4789
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% The distribution is normal with a couple of outliers

% MULT BUYOUT MORE 100
clc
clear all
close all
% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','MULT_Buyout_more_100','A2:AP294');
stringVectors = string(raw(:,[4,5,6,11,12]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,8,9,10,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
MULT_Buyout_more_100 = table;

% Allocate imported array to column variable names
MULT_Buyout_more_100.number = data(:,1);
MULT_Buyout_more_100.FUNDID = data(:,2);
MULT_Buyout_more_100.FIRMID = data(:,3);
MULT_Buyout_more_100.teamID = stringVectors(:,1);
MULT_Buyout_more_100.teamIDvintage = stringVectors(:,2);
MULT_Buyout_more_100.teamIDvintagenumber = stringVectors(:,3);
MULT_Buyout_more_100.vint_t = data(:,4);
MULT_Buyout_more_100.vint_t1 = data(:,5);
MULT_Buyout_more_100.mult_t = data(:,6);
MULT_Buyout_more_100.mult_t1 = data(:,7);
MULT_Buyout_more_100.STATUS = categorical(stringVectors(:,4));
MULT_Buyout_more_100.FUNDMANAGER = stringVectors(:,5);
MULT_Buyout_more_100.size_t = data(:,8);
MULT_Buyout_more_100.size_t1 = data(:,9);
MULT_Buyout_more_100.seq_num_t = data(:,10);
MULT_Buyout_more_100.seq_num_t1 = data(:,11);
MULT_Buyout_more_100.fundgrowtht1t = data(:,12);
MULT_Buyout_more_100.Dummy_less_0 = data(:,13);
MULT_Buyout_more_100.Dummy_0_100 = data(:,14);
MULT_Buyout_more_100.Dummy_more_100 = data(:,15);
MULT_Buyout_more_100.FundGrowthPerformance = data(:,16);
MULT_Buyout_more_100.vint90 = data(:,17);
MULT_Buyout_more_100.vint91 = data(:,18);
MULT_Buyout_more_100.vint92 = data(:,19);
MULT_Buyout_more_100.vint93 = data(:,20);
MULT_Buyout_more_100.vint94 = data(:,21);
MULT_Buyout_more_100.vint95 = data(:,22);
MULT_Buyout_more_100.vint96 = data(:,23);
MULT_Buyout_more_100.vint97 = data(:,24);
MULT_Buyout_more_100.vint98 = data(:,25);
MULT_Buyout_more_100.vint99 = data(:,26);
MULT_Buyout_more_100.vint00 = data(:,27);
MULT_Buyout_more_100.vint01 = data(:,28);
MULT_Buyout_more_100.vint02 = data(:,29);
MULT_Buyout_more_100.vint03 = data(:,30);
MULT_Buyout_more_100.vint04 = data(:,31);
MULT_Buyout_more_100.vint05 = data(:,32);
MULT_Buyout_more_100.vint06 = data(:,33);
MULT_Buyout_more_100.vint07 = data(:,34);
MULT_Buyout_more_100.vint08 = data(:,35);
MULT_Buyout_more_100.vint09 = data(:,36);
MULT_Buyout_more_100.vint10 = data(:,37);

% Clear temporary variables
clearvars data raw stringVectors;

% Log (size)

MULT_Buyout_more_100.log_size_t = log(MULT_Buyout_more_100.size_t);

% Linear regression 

reg_MULT_Buyout_more_100 = fitlm(MULT_Buyout_more_100, 'mult_t ~ mult_t1 + log_size_t + seq_num_t + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10')

% reg_MULT_Buyout_more_100 = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 22 terms in 21 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE        tStat        pValue  
%                    _________    ________    ________    __________
% 
%     (Intercept)       2.8813     0.72702      3.9632    9.4652e-05
%     mult_t1          0.22989    0.049539      4.6406    5.4157e-06
%     seq_num_t       0.013036    0.028818     0.45237       0.65136
%     vint93          0.080162     0.96425    0.083134       0.93381
%     vint94          -0.22924      0.7909    -0.28985       0.77215
%     vint95           -1.5917     0.73885     -2.1543      0.032099
%     vint96          -0.53421     0.73119    -0.73061       0.46565
%     vint97           -1.9822     0.70476     -2.8125     0.0052745
%     vint98           -1.6896     0.70161     -2.4081      0.016702
%     vint99            -1.637     0.70476     -2.3228      0.020929
%     vint00           -1.2704     0.70274     -1.8078      0.071749
%     vint01           -1.2265     0.73724     -1.6636      0.097348
%     vint02           -1.3181      0.7416     -1.7774       0.07662
%     vint03           -1.3859     0.71319     -1.9433       0.05302
%     vint04            -1.806     0.72126      -2.504      0.012868
%     vint05           -1.6881     0.69683     -2.4226      0.016067
%     vint06           -1.6061     0.69889     -2.2981      0.022319
%     vint07           -1.6111     0.69567     -2.3158      0.021314
%     vint08           -1.4976     0.70386     -2.1276      0.034271
%     vint09           -1.5694     0.71935     -2.1817       0.02999
%     vint10           -1.5144     0.73247     -2.0675      0.039633
%     log_size_t     -0.014778    0.036777    -0.40181       0.68814
% 
% 
% Number of observations: 293, Error degrees of freedom: 271
% Root Mean Squared Error: 0.681
% R-squared: 0.233,  Adjusted R-Squared 0.174
% F-statistic vs. constant model: 3.93, p-value = 6.73e-08


% DIAGNOSTICS of CLRM assumptions

T = reg_MULT_Buyout_more_100.NumObservations;
k = reg_MULT_Buyout_more_100.NumEstimatedCoefficients;
u = reg_MULT_Buyout_more_100.Residuals.Raw;
alpha = 0.05;

% 1) expected value (mean) of the residuals is equal to zero, E[u]=0

% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(reg_MULT_Buyout_more_100.Residuals.Raw);
Stdev_u = std(reg_MULT_Buyout_more_100.Residuals.Raw);

% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d =   1.4130e-14

% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =     1.9688


% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000

% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0

% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.

%2) homoscedasticity, Var[u,t] = sigma^2 < infinity

% White's test

MULT_Buyout_more_100.uRaw = u; 
MULT_Buyout_more_100.uRaw2 = u.^2;


% Create a table with the constant and squared terms
MULT_Buyout_more_100_White = table(MULT_Buyout_more_100.mult_t, MULT_Buyout_more_100.mult_t1, MULT_Buyout_more_100.log_size_t,...
    MULT_Buyout_more_100.seq_num_t, MULT_Buyout_more_100.vint93, MULT_Buyout_more_100.vint94,...
    MULT_Buyout_more_100.vint95, MULT_Buyout_more_100.vint96, MULT_Buyout_more_100.vint97, MULT_Buyout_more_100.vint98, MULT_Buyout_more_100.vint99,...
    MULT_Buyout_more_100.vint00, MULT_Buyout_more_100.vint01, MULT_Buyout_more_100.vint02, MULT_Buyout_more_100.vint03, MULT_Buyout_more_100.vint04,...
    MULT_Buyout_more_100.vint05, MULT_Buyout_more_100.vint06, MULT_Buyout_more_100.vint07, MULT_Buyout_more_100.vint08, MULT_Buyout_more_100.vint09, MULT_Buyout_more_100.vint10,...
    MULT_Buyout_more_100.mult_t1.^2, MULT_Buyout_more_100.log_size_t.^2, MULT_Buyout_more_100.uRaw2);

 
MULT_Buyout_more_100_White.Properties.VariableNames={'mult_t','mult_t1','log_size_t','seq_num_t',...
    'vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10',...
    'mult_t1sq','log_size_tsq','uRaw2'};

% Unrestricted regression
reg_MULT_Buyout_more_100_White =fitlm(MULT_Buyout_more_100_White,'uRaw2 ~ mult_t1 + log_size_t + seq_num_t + vint93 + vint94 + vint95 + vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 + mult_t1sq + log_size_tsq')
% 
% reg_MULT_Buyout_more_100_White = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 24 terms in 23 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE        tStat        pValue  
%                     _________    ________    ________    __________
% 
%     (Intercept)        11.782      2.8679      4.1082    5.2971e-05
%     mult_t1            0.3851     0.46057     0.83615       0.40381
%     log_size_t        -3.7675     0.70001      -5.382    1.6041e-07
%     seq_num_t       -0.011831    0.075793    -0.15609       0.87608
%     vint93             1.0674      2.4958     0.42768       0.66922
%     vint94              3.592      2.0522      1.7503      0.081204
%     vint95             1.5654      1.9135     0.81809       0.41403
%     vint96              5.765      1.8882      3.0531     0.0024918
%     vint97             1.5856       1.827     0.86788       0.38623
%     vint98             1.6764      1.8193     0.92146       0.35764
%     vint99             1.4991       1.828      0.8201       0.41288
%     vint00            0.88958      1.8174     0.48948        0.6249
%     vint01             1.6352       1.913     0.85474       0.39345
%     vint02              1.071      1.9211      0.5575       0.57765
%     vint03             1.1745      1.8456     0.63637       0.52508
%     vint04             1.4004      1.8721     0.74803        0.4551
%     vint05             1.2735      1.8046     0.70567         0.481
%     vint06             1.1699      1.8084     0.64696       0.51821
%     vint07             1.2641      1.7997     0.70239       0.48305
%     vint08             1.0652      1.8205     0.58511       0.55897
%     vint09             1.0499      1.8629     0.56357       0.57352
%     vint10             1.7868      1.8994     0.94074       0.34768
%     mult_t1sq       -0.091783    0.090245      -1.017       0.31005
%     log_size_tsq      0.26121    0.051505      5.0715    7.3587e-07
% 
% 
% Number of observations: 293, Error degrees of freedom: 269
% Root Mean Squared Error: 1.76
% R-squared: 0.279,  Adjusted R-Squared 0.218
% F-statistic vs. constant model: 4.53, p-value = 4.13e-10


% Langrange Multiplier (LM) test

m = k - 1;
test_stat_e = reg_MULT_Buyout_more_100_White.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e

%output: test_stat_e = 81.8154, Crit_Val_e = 32.6706, P_e =  4.0061e-09,
%reject_e = 1

% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.

% Next step is to use heteroscedasticity-consistent standard errors.

x = [MULT_Buyout_more_100.mult_t1 MULT_Buyout_more_100.log_size_t MULT_Buyout_more_100.seq_num_t ...
    MULT_Buyout_more_100.vint93 MULT_Buyout_more_100.vint94 MULT_Buyout_more_100.vint95 MULT_Buyout_more_100.vint96 MULT_Buyout_more_100.vint97 MULT_Buyout_more_100.vint98 MULT_Buyout_more_100.vint99 ...
    MULT_Buyout_more_100.vint00 MULT_Buyout_more_100.vint01 MULT_Buyout_more_100.vint02 MULT_Buyout_more_100.vint03 MULT_Buyout_more_100.vint04 ...
    MULT_Buyout_more_100.vint05 MULT_Buyout_more_100.vint06 MULT_Buyout_more_100.vint07 MULT_Buyout_more_100.vint08 MULT_Buyout_more_100.vint09 MULT_Buyout_more_100.vint10];

y = MULT_Buyout_more_100.mult_t;

% White's estimation correction 

White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);

White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','mult_t1','log_size_t','seq_num_t',...
    'vint93','vint94','vint95','vint96','vint97','vint98','vint99',...
    'vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08','vint09','vint10'};
White
% 
% 
% White =
% 
%   22×4 table
% 
%                      beta          SE        tstat          p     
%                    _________    ________    ________    __________
% 
%     (Intercept)       2.8813     0.21807      13.213             0
%     mult_t1          0.22989    0.046987      4.8927    1.6494e-06
%     log_size_t     -0.014778    0.043514    -0.33961        0.7344
%     seq_num_t       0.013036    0.022313     0.58426        0.5595
%     vint93          0.080162    0.033221       2.413      0.016441
%     vint94          -0.22924     0.88088    -0.26024       0.79486
%     vint95           -1.5917     0.29548     -5.3868    1.4813e-07
%     vint96          -0.53421     0.65209    -0.81923       0.41332
%     vint97           -1.9822     0.15728     -12.603             0
%     vint98           -1.6896     0.16214      -10.42             0
%     vint99            -1.637     0.17763     -9.2161             0
%     vint00           -1.2704     0.12791      -9.932             0
%     vint01           -1.2265     0.22827     -5.3728    1.5898e-07
%     vint02           -1.3181     0.16998     -7.7546    1.4988e-13
%     vint03           -1.3859      0.1738     -7.9743    3.5305e-14
%     vint04            -1.806     0.17583     -10.271             0
%     vint05           -1.6881     0.14095     -11.976             0
%     vint06           -1.6061     0.17831     -9.0073             0
%     vint07           -1.6111     0.16239     -9.9209             0
%     vint08           -1.4976     0.15389     -9.7315             0
%     vint09           -1.5694     0.15734     -9.9746             0
%     vint10           -1.5144     0.35611     -4.2526    2.8501e-05
% 
% 


%3) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 

corr_mult_t1_u = corr(MULT_Buyout_more_100.mult_t1,u)
corr_log_size_t_u = corr(MULT_Buyout_more_100.log_size_t,u)
corr_seq_num_t_u = corr(MULT_Buyout_more_100.seq_num_t,u)

% corr_mult_t1_u =   1.8302e-16
% corr_log_size_t_u =  -1.6066e-16
% corr_seq_num_t_u =  7.5676e-17


%no correlation

%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test

skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest


% output: 

% skew_jbtest = 2.2529
% kurt_jbtest = 22.2326
% test_stat_jbtest =  4.7636e+03
% crit_val_jbtest = 35.4789
% p_jbtest = 0
% reject_jbstest = 1

%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 



% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');

figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 


% there are several outliers 


sum(reg_MULT_Buyout_more_100.Diagnostics.CooksDistance >=0.05);
Outliers = MULT_Buyout_more_100(reg_MULT_Buyout_more_100.Diagnostics.CooksDistance >=0.05,:);


%% Section 5.4.3
% IRR VENTURE
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Venture IRR quart','A2:AZ502');
stringVectors = string(raw(:,[4,5,6,8,17,18,19,20,21,22,23,24,28,29,30,31]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,13,14,15,16,25,26,27,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
QUART_VENT_IRR = table;

% Allocate imported array to column variable names
QUART_VENT_IRR.number = data(:,1);
QUART_VENT_IRR.FUNDID = data(:,2);
QUART_VENT_IRR.FIRMID = data(:,3);
QUART_VENT_IRR.teamID = stringVectors(:,1);
QUART_VENT_IRR.teamIDvintage = stringVectors(:,2);
QUART_VENT_IRR.teamIDvintagenumber = stringVectors(:,3);
QUART_VENT_IRR.VINTAGEYEAR = data(:,4);
QUART_VENT_IRR.STRATEGY = categorical(stringVectors(:,4));
QUART_VENT_IRR.BuyoutDummy = data(:,5);
QUART_VENT_IRR.irr_t = data(:,6);
QUART_VENT_IRR.irr_t1 = data(:,7);
QUART_VENT_IRR.quart_t1 = data(:,8);
QUART_VENT_IRR.q1 = data(:,9);
QUART_VENT_IRR.q2 = data(:,10);
QUART_VENT_IRR.q3 = data(:,11);
QUART_VENT_IRR.q4 = data(:,12);
QUART_VENT_IRR.prev_quart_t1 = categorical(stringVectors(:,5));
QUART_VENT_IRR.quart_change_t = categorical(stringVectors(:,6));
QUART_VENT_IRR.PRIMARYFUNDFOCUS = categorical(stringVectors(:,7));
QUART_VENT_IRR.COUNTRIES = categorical(stringVectors(:,8));
QUART_VENT_IRR.REGIONS = stringVectors(:,9);
QUART_VENT_IRR.STATUS = categorical(stringVectors(:,10));
QUART_VENT_IRR.FUNDMANAGER = stringVectors(:,11);
QUART_VENT_IRR.DOMICILE = stringVectors(:,12);
QUART_VENT_IRR.FINALCLOSESIZE = data(:,13);
QUART_VENT_IRR.size_ = data(:,14);
QUART_VENT_IRR.seq_num_t = data(:,15);
QUART_VENT_IRR.COREINDUSTRIES = categorical(stringVectors(:,13));
QUART_VENT_IRR.INDUSTRIES = categorical(stringVectors(:,14));
QUART_VENT_IRR.MANAGERREGION = categorical(stringVectors(:,15));
QUART_VENT_IRR.MANAGERCOUNTRY = categorical(stringVectors(:,16));
QUART_VENT_IRR.vint90 = data(:,16);
QUART_VENT_IRR.vint91 = data(:,17);
QUART_VENT_IRR.vint92 = data(:,18);
QUART_VENT_IRR.vint93 = data(:,19);
QUART_VENT_IRR.vint94 = data(:,20);
QUART_VENT_IRR.vint95 = data(:,21);
QUART_VENT_IRR.vint96 = data(:,22);
QUART_VENT_IRR.vint97 = data(:,23);
QUART_VENT_IRR.vint98 = data(:,24);
QUART_VENT_IRR.vint99 = data(:,25);
QUART_VENT_IRR.vint00 = data(:,26);
QUART_VENT_IRR.vint01 = data(:,27);
QUART_VENT_IRR.vint02 = data(:,28);
QUART_VENT_IRR.vint03 = data(:,29);
QUART_VENT_IRR.vint04 = data(:,30);
QUART_VENT_IRR.vint05 = data(:,31);
QUART_VENT_IRR.vint06 = data(:,32);
QUART_VENT_IRR.vint07 = data(:,33);
QUART_VENT_IRR.vint08 = data(:,34);
QUART_VENT_IRR.vint09 = data(:,35);
QUART_VENT_IRR.vint10 = data(:,36);

% Clear temporary variables
clearvars data raw stringVectors;

% Log size and interaction terms
QUART_VENT_IRR.size_t=log(QUART_VENT_IRR.size_);
QUART_VENT_IRR.quart1=QUART_VENT_IRR.q1.*QUART_VENT_IRR.irr_t1;
QUART_VENT_IRR.quart2=QUART_VENT_IRR.q2.*QUART_VENT_IRR.irr_t1;
QUART_VENT_IRR.quart3=QUART_VENT_IRR.q3.*QUART_VENT_IRR.irr_t1;

% Linear regression regirr 
 
regirr = fitlm(QUART_VENT_IRR, 'irr_t~seq_num_t + size_t + irr_t1 + quart1 + quart2 + quart3 + q1 + q2 + q3+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% % 
% regirr = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 30 terms in 29 predictors]
% 
% Estimated Coefficients:
%                    Estimate      SE         tStat       pValue 
%                    ________    _______    _________    ________
% 
%     (Intercept)      6.8317       33.9      0.20153     0.84037
%     irr_t1          0.49189    0.24549       2.0037    0.045674
%     q1               6.8497     5.8384       1.1732      0.2413
%     q2               2.7631     5.9966      0.46078     0.64517
%     q3                2.226     5.6032      0.39728     0.69134
%     seq_num_t      -0.61723    0.85381     -0.72291     0.47009
%     vint91          -13.678     46.711     -0.29283     0.76978
%     vint92            -2.05     36.044    -0.056876     0.95467
%     vint93           -15.16     46.712     -0.32454     0.74567
%     vint94           25.992     34.296      0.75787     0.44891
%     vint95           51.157     34.442       1.4853     0.13813
%     vint96           18.744     33.942      0.55224     0.58105
%     vint97            29.95     33.493      0.89422     0.37166
%     vint98         -0.74759     33.338    -0.022425     0.98212
%     vint99          -26.179     33.213     -0.78822     0.43097
%     vint00          -19.954     33.106     -0.60273     0.54698
%     vint01          -14.303     33.291     -0.42964     0.66766
%     vint02           -14.19     33.517     -0.42336     0.67223
%     vint03          -15.842     33.744     -0.46948     0.63895
%     vint04          -15.205      33.57     -0.45295     0.65079
%     vint05          -11.982       33.5     -0.35768     0.72074
%     vint06          -12.844     33.231      -0.3865      0.6993
%     vint07          -9.1832      33.16     -0.27693     0.78195
%     vint08          -7.3364     33.168     -0.22119     0.82504
%     vint09          -2.6662     34.008    -0.078401     0.93754
%     vint10          -3.6782     33.613     -0.10943     0.91291
%     size_t           1.9224     1.2907       1.4894     0.13706
%     quart1          -0.4092    0.24721      -1.6553    0.098539
%     quart2         -0.43818    0.27687      -1.5826     0.11418
%     quart3         -0.56356    0.40972      -1.3755     0.16964
% 
% 
% Number of observations: 501, Error degrees of freedom: 471
% Root Mean Squared Error: 32.7
% R-squared: 0.257,  Adjusted R-Squared 0.211
% F-statistic vs. constant model: 5.61, p-value = 3.99e-17

%DIAGNOSTICS of CLRM assumptions
 
T = regirr.NumObservations;
k = regirr.NumEstimatedCoefficients;
u = regirr.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr.Residuals.Raw);
Stdev_u = std(regirr.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -2.0325e-15
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9650
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test


QUART_VENT_IRR.uRaw = regirr.Residuals.Raw; 
QUART_VENT_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irrWhite = table(QUART_VENT_IRR.irr_t, QUART_VENT_IRR.seq_num_t, QUART_VENT_IRR.size_t, QUART_VENT_IRR.irr_t1, QUART_VENT_IRR.quart1,QUART_VENT_IRR.quart2,QUART_VENT_IRR.quart3,QUART_VENT_IRR.q1,QUART_VENT_IRR.q2,QUART_VENT_IRR.q3,...
QUART_VENT_IRR.vint91, QUART_VENT_IRR.vint92, QUART_VENT_IRR.vint93, QUART_VENT_IRR.vint94, ...
    QUART_VENT_IRR.vint95, QUART_VENT_IRR.vint96, QUART_VENT_IRR.vint97, QUART_VENT_IRR.vint98, QUART_VENT_IRR.vint99,QUART_VENT_IRR.vint00, QUART_VENT_IRR.vint01, QUART_VENT_IRR.vint02, QUART_VENT_IRR.vint03, QUART_VENT_IRR.vint04,...
    QUART_VENT_IRR.vint05, QUART_VENT_IRR.vint06, QUART_VENT_IRR.vint07, QUART_VENT_IRR.vint08, QUART_VENT_IRR.vint09, QUART_VENT_IRR.vint10, QUART_VENT_IRR.size_t.^2, QUART_VENT_IRR.irr_t1.^2, QUART_VENT_IRR.quart1.^2,  QUART_VENT_IRR.quart2.^2,QUART_VENT_IRR.quart3.^2,QUART_VENT_IRR.uRaw2);
 
irrWhite.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'quart1','quart2','quart3','q1','q2','q3','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq', 'quart1sq', 'quart2sq','quart3sq','uRaw2'};
 
% Unrestricted regression
regirrWhite=fitlm(irrWhite,'uRaw2 ~ seq_num_t + size_t + irr_t1 + quart1 + quart2 + quart3 + q1 + q2 + q3+ vint91 +vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq + quart1sq + quart2sq + quart3sq ')
 
% regirrWhite = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 35 terms in 34 predictors]
% 
% Estimated Coefficients:
%                    Estimate      SE        tStat       pValue 
%                    ________    ______    __________    _______
% 
%     (Intercept)     -4155.4     11507      -0.36112    0.71817
%     seq_num_t        107.48    266.57        0.4032    0.68698
%     size_t           1259.4    2103.1       0.59883    0.54958
%     irr_t1          -25.564    167.58      -0.15255    0.87882
%     quart1           175.38    166.81        1.0514    0.29363
%     quart2           59.958    186.18       0.32204    0.74757
%     quart3           22.976    207.13       0.11092    0.91172
%     q1              -2823.3    2179.6       -1.2953    0.19584
%     q2               248.78    2200.7       0.11305    0.91004
%     q3               516.71    2021.3       0.25563    0.79835
%     vint91           1021.4     14419      0.070838    0.94356
%     vint92            193.8     11073      0.017502    0.98604
%     vint93           1264.1     14377      0.087929    0.92997
%     vint94           1666.5     10548       0.15799    0.87453
%     vint95           1396.8     10600       0.13177    0.89522
%     vint96          -77.003     10441    -0.0073751    0.99412
%     vint97           2187.7     10298       0.21244    0.83185
%     vint98           4875.2     10255       0.47539    0.63473
%     vint99          -1658.6     10213       -0.1624    0.87106
%     vint00           370.74     10170      0.036452    0.97094
%     vint01           1051.4     10230       0.10277    0.91819
%     vint02             1371     10295       0.13317    0.89412
%     vint03           1319.3     10361       0.12733    0.89874
%     vint04            956.9     10310      0.092815    0.92609
%     vint05           1299.5     10286       0.12633    0.89952
%     vint06           1375.1     10206       0.13473    0.89289
%     vint07           1307.3     10182        0.1284    0.89789
%     vint08           1230.6     10185       0.12082    0.90388
%     vint09           1338.6     10448       0.12813     0.8981
%     vint10           519.69     10322      0.050348    0.95987
%     size_sq         -141.34    227.88      -0.62023    0.53541
%     irr_t1sq        0.36759    2.8716       0.12801     0.8982
%     quart1sq       -0.62073    2.8786      -0.21564    0.82936
%     quart2sq       -0.53168     3.184      -0.16699    0.86745
%     quart3sq       -0.73476    8.2846     -0.088689    0.92937
% 
% 
% Number of observations: 501, Error degrees of freedom: 466
% Root Mean Squared Error: 1e+04
% R-squared: 0.103,  Adjusted R-Squared 0.0378
% F-statistic vs. constant model: 1.58, p-value = 0.0223

% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirrWhite.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  51.7190, Crit_Val_e =  42.5570, P_e = 0.0059, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.

x = [QUART_VENT_IRR.seq_num_t QUART_VENT_IRR.size_t QUART_VENT_IRR.irr_t1 QUART_VENT_IRR.quart1...
    QUART_VENT_IRR.quart2 QUART_VENT_IRR.quart3 QUART_VENT_IRR.q1 QUART_VENT_IRR.q2 QUART_VENT_IRR.q3 QUART_VENT_IRR.vint91...
      QUART_VENT_IRR.vint92  QUART_VENT_IRR.vint93 QUART_VENT_IRR.vint94 QUART_VENT_IRR.vint95  QUART_VENT_IRR.vint96  QUART_VENT_IRR.vint97...
    QUART_VENT_IRR.vint98 QUART_VENT_IRR.vint99 QUART_VENT_IRR.vint00 QUART_VENT_IRR.vint01 QUART_VENT_IRR.vint02...
    QUART_VENT_IRR.vint03 QUART_VENT_IRR.vint04 QUART_VENT_IRR.vint05 QUART_VENT_IRR.vint06 QUART_VENT_IRR.vint07 QUART_VENT_IRR.vint08...
    QUART_VENT_IRR.vint09 QUART_VENT_IRR.vint10];
y = QUART_VENT_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'quart1', 'quart2' , 'quart3', ...
    'q1','q2','q3','vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White

% White =
% 
%   30?4 table
% 
%                      beta        SE         tstat          p     
%                    ________    _______    _________    __________
% 
%     (Intercept)      6.8317     6.5682       1.0401       0.29879
%     seq_num_t      -0.61723    0.50796      -1.2151        0.2249
%     size_t           1.9224    0.72247       2.6608     0.0080462
%     irr_t1          0.49189    0.26488        1.857      0.063897
%     quart1          -0.4092    0.31631      -1.2937       0.19638
%     quart2         -0.43818    0.28417       -1.542       0.12372
%     quart3         -0.56356    0.31969      -1.7628      0.078543
%     q1               6.8497     4.1567       1.6479       0.10001
%     q2               2.7631     3.9707      0.69588       0.48683
%     q3                2.226      3.645      0.61072       0.54166
%     vint91          -13.678     5.5197      -2.4781      0.013538
%     vint92            -2.05     4.7163     -0.43467       0.66399
%     vint93           -15.16     5.8345      -2.5983     0.0096457
%     vint94           25.992     11.903       2.1835      0.029461
%     vint95           51.157     18.383       2.7828     0.0055942
%     vint96           18.744     14.972        1.252       0.21117
%     vint97            29.95     10.651       2.8119     0.0051186
%     vint98         -0.74759     13.145    -0.056872       0.95467
%     vint99          -26.179     4.0146      -6.5209     1.715e-10
%     vint00          -19.954     2.7671      -7.2111    2.0706e-12
%     vint01          -14.303     2.4986      -5.7245    1.7923e-08
%     vint02           -14.19     2.6665      -5.3216    1.5571e-07
%     vint03          -15.842     3.8117      -4.1561    3.8098e-05
%     vint04          -15.205     3.4526      -4.4041    1.3008e-05
%     vint05          -11.982     5.0256      -2.3842      0.017487
%     vint06          -12.844     2.4944       -5.149    3.7748e-07
%     vint07          -9.1832     2.8047      -3.2741     0.0011334
%     vint08          -7.3364      2.693      -2.7243     0.0066705
%     vint09          -2.6662     4.9191     -0.54202       0.58805
%     vint10          -3.6782     3.4495      -1.0663        0.2868

%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(QUART_VENT_IRR.seq_num_t,u)
corr_size_t_u = corr(QUART_VENT_IRR.size_t,u)
corr_irr_t1_u = corr(QUART_VENT_IRR.irr_t1,u)
corr_quart1_u = corr(QUART_VENT_IRR.quart1,u)
corr_quart2_u = corr(QUART_VENT_IRR.quart2,u)
corr_quart3_u = corr(QUART_VENT_IRR.quart3,u)

%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 7.2819
% kurt_jbtest = 103.9577
% test_stat_jbtest = 2.1720e+05
% crit_val_jbtest = 45.7223
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr.Diagnostics.CooksDistance >=0.05);
Outliers = QUART_VENT_IRR(regirr.Diagnostics.CooksDistance >=0.05,:);

% IRR BUYOUT
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Buyout IRR quart','A2:AZ637');
stringVectors = string(raw(:,[4,5,6,8,17,18,19,20,21,22,23,24,28,29,30,31]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,13,14,15,16,25,26,27,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
QUART_BUYOUT_IRR = table;

% Allocate imported array to column variable names
QUART_BUYOUT_IRR.number = data(:,1);
QUART_BUYOUT_IRR.FUNDID = data(:,2);
QUART_BUYOUT_IRR.FIRMID = data(:,3);
QUART_BUYOUT_IRR.teamID = stringVectors(:,1);
QUART_BUYOUT_IRR.teamIDvintage = stringVectors(:,2);
QUART_BUYOUT_IRR.teamIDvintagenumber = stringVectors(:,3);
QUART_BUYOUT_IRR.VINTAGEYEAR = data(:,4);
QUART_BUYOUT_IRR.STRATEGY = categorical(stringVectors(:,4));
QUART_BUYOUT_IRR.BuyoutDummy = data(:,5);
QUART_BUYOUT_IRR.irr_t = data(:,6);
QUART_BUYOUT_IRR.irr_t1 = data(:,7);
QUART_BUYOUT_IRR.quart_t1 = data(:,8);
QUART_BUYOUT_IRR.q1 = data(:,9);
QUART_BUYOUT_IRR.q2 = data(:,10);
QUART_BUYOUT_IRR.q3 = data(:,11);
QUART_BUYOUT_IRR.q4 = data(:,12);
QUART_BUYOUT_IRR.prev_quart_t1 = categorical(stringVectors(:,5));
QUART_BUYOUT_IRR.quart_change_t = categorical(stringVectors(:,6));
QUART_BUYOUT_IRR.PRIMARYFUNDFOCUS = categorical(stringVectors(:,7));
QUART_BUYOUT_IRR.COUNTRIES = categorical(stringVectors(:,8));
QUART_BUYOUT_IRR.REGIONS = categorical(stringVectors(:,9));
QUART_BUYOUT_IRR.STATUS = categorical(stringVectors(:,10));
QUART_BUYOUT_IRR.FUNDMANAGER = stringVectors(:,11);
QUART_BUYOUT_IRR.DOMICILE = categorical(stringVectors(:,12));
QUART_BUYOUT_IRR.FINALCLOSESIZE = data(:,13);
QUART_BUYOUT_IRR.size_ = data(:,14);
QUART_BUYOUT_IRR.seq_num_t = data(:,15);
QUART_BUYOUT_IRR.COREINDUSTRIES = categorical(stringVectors(:,13));
QUART_BUYOUT_IRR.INDUSTRIES = categorical(stringVectors(:,14));
QUART_BUYOUT_IRR.MANAGERREGION = categorical(stringVectors(:,15));
QUART_BUYOUT_IRR.MANAGERCOUNTRY = categorical(stringVectors(:,16));
QUART_BUYOUT_IRR.vint90 = data(:,16);
QUART_BUYOUT_IRR.vint91 = data(:,17);
QUART_BUYOUT_IRR.vint92 = data(:,18);
QUART_BUYOUT_IRR.vint93 = data(:,19);
QUART_BUYOUT_IRR.vint94 = data(:,20);
QUART_BUYOUT_IRR.vint95 = data(:,21);
QUART_BUYOUT_IRR.vint96 = data(:,22);
QUART_BUYOUT_IRR.vint97 = data(:,23);
QUART_BUYOUT_IRR.vint98 = data(:,24);
QUART_BUYOUT_IRR.vint99 = data(:,25);
QUART_BUYOUT_IRR.vint00 = data(:,26);
QUART_BUYOUT_IRR.vint01 = data(:,27);
QUART_BUYOUT_IRR.vint02 = data(:,28);
QUART_BUYOUT_IRR.vint03 = data(:,29);
QUART_BUYOUT_IRR.vint04 = data(:,30);
QUART_BUYOUT_IRR.vint05 = data(:,31);
QUART_BUYOUT_IRR.vint06 = data(:,32);
QUART_BUYOUT_IRR.vint07 = data(:,33);
QUART_BUYOUT_IRR.vint08 = data(:,34);
QUART_BUYOUT_IRR.vint09 = data(:,35);
QUART_BUYOUT_IRR.vint10 = data(:,36);

% Clear temporary variables
clearvars data raw stringVectors;

% Log size and interaction terms
QUART_BUYOUT_IRR.size_t=log(QUART_BUYOUT_IRR.size_);
QUART_BUYOUT_IRR.quart1=QUART_BUYOUT_IRR.q1.*QUART_BUYOUT_IRR.irr_t1;
QUART_BUYOUT_IRR.quart2=QUART_BUYOUT_IRR.q2.*QUART_BUYOUT_IRR.irr_t1;
QUART_BUYOUT_IRR.quart3=QUART_BUYOUT_IRR.q3.*QUART_BUYOUT_IRR.irr_t1;
 
% Linear regression regirr 
 
regirr = fitlm(QUART_BUYOUT_IRR, 'irr_t~seq_num_t + size_t + irr_t1 + quart1 + quart2 + quart3 + q1 + q2 + q3+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% % 
% regirr = 
% 
% 
% Linear regression model:
%     irr_t ~ [Linear formula with 30 terms in 29 predictors]
% 
% Estimated Coefficients:
%                    Estimate      SE        tStat        pValue  
%                    ________    _______    ________    __________
% 
%     (Intercept)      44.435     11.344      3.9172    9.9787e-05
%     irr_t1          0.51052    0.16514      3.0914     0.0020837
%     q1               7.2129     2.4675      2.9232      0.003594
%     q2               4.3853     2.7201      1.6122       0.10744
%     q3             0.060587      2.772    0.021856       0.98257
%     seq_num_t       0.55872    0.42347      1.3194       0.18753
%     vint91           5.1459     18.418     0.27939       0.78004
%     vint92                0          0         NaN           NaN
%     vint93          -7.9307     13.002    -0.60997       0.54211
%     vint94          -15.515     12.047     -1.2879       0.19826
%     vint95          -23.256       11.3     -2.0582      0.040002
%     vint96          -18.542      11.46      -1.618       0.10618
%     vint97          -35.263     11.049     -3.1915     0.0014886
%     vint98          -37.832     10.991     -3.4422     0.0006167
%     vint99          -31.911     11.006     -2.8994     0.0038745
%     vint00           -23.01     10.966     -2.0982      0.036303
%     vint01          -14.191     11.116     -1.2766       0.20222
%     vint02           -22.48     11.131     -2.0196      0.043868
%     vint03          -23.539     11.072     -2.1261      0.033898
%     vint04          -27.408     11.043     -2.4819      0.013338
%     vint05          -29.258      10.83     -2.7017     0.0070923
%     vint06          -32.797      10.81      -3.034     0.0025168
%     vint07          -30.452     10.862     -2.8035     0.0052169
%     vint08          -28.083     10.882     -2.5807      0.010093
%     vint09          -25.522      11.11     -2.2971      0.021953
%     vint10          -28.446         11      -2.586      0.009941
%     size_t          -1.3088    0.50722     -2.5804      0.010103
%     quart1         -0.45605    0.17138      -2.661     0.0079964
%     quart2         -0.46314    0.19155     -2.4179      0.015903
%     quart3          -0.3386    0.24171     -1.4008       0.16177
% 
% 
% Number of observations: 636, Error degrees of freedom: 607
% Root Mean Squared Error: 14.9
% R-squared: 0.209,  Adjusted R-Squared 0.172
% F-statistic vs. constant model: 5.72, p-value = 9.06e-18
 
%DIAGNOSTICS of CLRM assumptions
 
T = regirr.NumObservations;
k = regirr.NumEstimatedCoefficients;
u = regirr.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regirr.Residuals.Raw);
Stdev_u = std(regirr.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = 1.0446e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9639
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
QUART_BUYOUT_IRR.uRaw = regirr.Residuals.Raw; 
QUART_BUYOUT_IRR.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
irrWhite = table(QUART_BUYOUT_IRR.irr_t, QUART_BUYOUT_IRR.seq_num_t, QUART_BUYOUT_IRR.size_t, QUART_BUYOUT_IRR.irr_t1, QUART_BUYOUT_IRR.quart1,QUART_BUYOUT_IRR.quart2,QUART_BUYOUT_IRR.quart3,QUART_BUYOUT_IRR.q1,QUART_BUYOUT_IRR.q2,QUART_BUYOUT_IRR.q3,...
QUART_BUYOUT_IRR.vint91, QUART_BUYOUT_IRR.vint92, QUART_BUYOUT_IRR.vint93, QUART_BUYOUT_IRR.vint94, ...
    QUART_BUYOUT_IRR.vint95, QUART_BUYOUT_IRR.vint96, QUART_BUYOUT_IRR.vint97, QUART_BUYOUT_IRR.vint98, QUART_BUYOUT_IRR.vint99,QUART_BUYOUT_IRR.vint00, QUART_BUYOUT_IRR.vint01, QUART_BUYOUT_IRR.vint02, QUART_BUYOUT_IRR.vint03, QUART_BUYOUT_IRR.vint04,...
    QUART_BUYOUT_IRR.vint05, QUART_BUYOUT_IRR.vint06, QUART_BUYOUT_IRR.vint07, QUART_BUYOUT_IRR.vint08, QUART_BUYOUT_IRR.vint09, QUART_BUYOUT_IRR.vint10, QUART_BUYOUT_IRR.size_t.^2, QUART_BUYOUT_IRR.irr_t1.^2, QUART_BUYOUT_IRR.quart1.^2,  QUART_BUYOUT_IRR.quart2.^2,QUART_BUYOUT_IRR.quart3.^2,QUART_BUYOUT_IRR.uRaw2);
 
irrWhite.Properties.VariableNames={'irr_t','seq_num_t','size_t','irr_t1', 'quart1','quart2','quart3','q1','q2','q3','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','irr_t1sq', 'quart1sq', 'quart2sq','quart3sq','uRaw2'};
 
% Unrestricted regression
regirrWhite=fitlm(irrWhite,'uRaw2 ~ seq_num_t + size_t + irr_t1 + quart1 + quart2 + quart3 + q1 + q2 + q3+ vint91 +vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + irr_t1sq + quart1sq + quart2sq + quart3sq ')
 
% regirrWhite = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 35 terms in 34 predictors]
% 
% Estimated Coefficients:
%                     Estimate       SE         tStat        pValue  
%                    __________    _______    _________    __________
% 
%     (Intercept)             0          0          NaN           NaN
%     seq_num_t          34.438     21.785       1.5808       0.11445
%     size_t            -606.81     179.27      -3.3849    0.00075853
%     irr_t1            -9.3621     10.184     -0.91927       0.35832
%     quart1             7.4381      11.33      0.65649       0.51176
%     quart2             25.814     15.282       1.6891      0.091715
%     quart3             13.825     24.923       0.5547       0.57931
%     q1                -88.262     169.89     -0.51953       0.60358
%     q2                -434.43     186.18      -2.3334      0.019954
%     q3                -150.02      169.1     -0.88717       0.37534
%     vint91             2080.5     936.09       2.2226      0.026615
%     vint92             2808.8     802.45       3.5002    0.00049927
%     vint93             3113.1     701.09       4.4405    1.0684e-05
%     vint94             2460.3     657.52       3.7418    0.00020019
%     vint95             2398.6     611.79       3.9205    9.8522e-05
%     vint96             3744.1      613.1       6.1068    1.8254e-09
%     vint97             2449.6     602.29       4.0672    5.3932e-05
%     vint98             2576.7     605.01       4.2589    2.3838e-05
%     vint99               2409     608.54       3.9586    8.4403e-05
%     vint00             2230.5      593.2       3.7601    0.00018642
%     vint01               2572      604.5       4.2548    2.4271e-05
%     vint02             2475.4      603.6       4.1011    4.6778e-05
%     vint03             2388.6     597.43       3.9981    7.1787e-05
%     vint04               2419     607.56       3.9814    7.6896e-05
%     vint05             2446.9     599.66       4.0805    5.1011e-05
%     vint06             2274.1     599.29       3.7947     0.0001628
%     vint07             2247.2     592.53       3.7925    0.00016418
%     vint08             2337.2     598.12       3.9075    0.00010386
%     vint09             2246.8     601.94       3.7326    0.00020754
%     vint10             2324.7     611.01       3.8047    0.00015653
%     size_sq            40.173     13.892       2.8918     0.0039693
%     irr_t1sq         0.010278    0.48941     0.021001       0.98325
%     quart1sq       -0.0069021    0.49032    -0.014077       0.98877
%     quart2sq        -0.087642    0.51762     -0.16932        0.8656
%     quart3sq         -0.23264     0.9778     -0.23792       0.81202
% 
% 
% Number of observations: 636, Error degrees of freedom: 602
% Root Mean Squared Error: 744
% R-squared: 0.147,  Adjusted R-Squared 0.0998
% F-statistic vs. constant model: 3.13, p-value = 2.44e-08
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regirrWhite.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  93.2204, Crit_Val_e =  41.3371, P_e =6.1776e-09, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [QUART_BUYOUT_IRR.seq_num_t QUART_BUYOUT_IRR.size_t QUART_BUYOUT_IRR.irr_t1 QUART_BUYOUT_IRR.quart1...
    QUART_BUYOUT_IRR.quart2 QUART_BUYOUT_IRR.quart3 QUART_BUYOUT_IRR.q1 QUART_BUYOUT_IRR.q2 QUART_BUYOUT_IRR.q3 ...
      QUART_BUYOUT_IRR.vint92  QUART_BUYOUT_IRR.vint93 QUART_BUYOUT_IRR.vint94 QUART_BUYOUT_IRR.vint95  QUART_BUYOUT_IRR.vint96  QUART_BUYOUT_IRR.vint97...
    QUART_BUYOUT_IRR.vint98 QUART_BUYOUT_IRR.vint99 QUART_BUYOUT_IRR.vint00 QUART_BUYOUT_IRR.vint01 QUART_BUYOUT_IRR.vint02...
    QUART_BUYOUT_IRR.vint03 QUART_BUYOUT_IRR.vint04 QUART_BUYOUT_IRR.vint05 QUART_BUYOUT_IRR.vint06 QUART_BUYOUT_IRR.vint07 QUART_BUYOUT_IRR.vint08...
    QUART_BUYOUT_IRR.vint09 QUART_BUYOUT_IRR.vint10];
y = QUART_BUYOUT_IRR.irr_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','irr_t1', 'quart1', 'quart2' , 'quart3', ...
    'q1','q2','q3','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
%  
% White =
% 
%   29?4 table
% 
%                      beta        SE        tstat          p     
%                    ________    _______    ________    __________
% 
%     (Intercept)      49.581     3.1452      15.764             0
%     seq_num_t       0.55872    0.56296     0.99247       0.32135
%     size_t          -1.3088    0.46678     -2.8039     0.0052027
%     irr_t1          0.51052    0.24681      2.0685      0.039002
%     quart1         -0.45605    0.24066      -1.895      0.058553
%     quart2         -0.46314    0.28076     -1.6496      0.099521
%     quart3          -0.3386    0.27524     -1.2302       0.21908
%     q1               7.2129      2.682      2.6894     0.0073472
%     q2               4.3853     2.7169      1.6141         0.107
%     q3             0.060587     2.6011    0.023293       0.98142
%     vint92          -5.1459     15.614    -0.32956       0.74184
%     vint93          -13.077     15.328    -0.85309       0.39393
%     vint94          -20.661     7.2141      -2.864     0.0043214
%     vint95          -28.402     3.5464     -8.0088    5.5511e-15
%     vint96          -23.688     11.275     -2.1009      0.036041
%     vint97          -40.409     3.1842     -12.691             0
%     vint98          -42.978     2.9932     -14.358             0
%     vint99          -37.056     2.5735     -14.399             0
%     vint00          -28.155     1.6561     -17.001             0
%     vint01          -19.337     3.8025     -5.0853    4.8396e-07
%     vint02          -27.625      3.438     -8.0353    4.6629e-15
%     vint03          -28.685     3.0408     -9.4335             0
%     vint04          -32.554     2.9626     -10.988             0
%     vint05          -34.404     2.4562     -14.007             0
%     vint06          -37.943     1.7944     -21.144             0
%     vint07          -35.598     1.7582     -20.247             0
%     vint08          -33.229     2.1286     -15.611             0
%     vint09          -30.667     2.4119     -12.715             0
%     vint10          -33.592     2.4605     -13.652             0
 
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(QUART_BUYOUT_IRR.seq_num_t,u)
corr_size_t_u = corr(QUART_BUYOUT_IRR.size_t,u)
corr_irr_t1_u = corr(QUART_BUYOUT_IRR.irr_t1,u)
corr_quart1_u = corr(QUART_BUYOUT_IRR.quart1,u)
corr_quart2_u = corr(QUART_BUYOUT_IRR.quart2,u)
corr_quart3_u = corr(QUART_BUYOUT_IRR.quart3,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 1.1298
% kurt_jbtest = 14.6807
% test_stat_jbtest = 3.7510e+03
% crit_val_jbtest = 44.4608
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regirr.Diagnostics.CooksDistance >=0.05);
Outliers = QUART_BUYOUT_IRR(regirr.Diagnostics.CooksDistance >=0.05,:);
 
% MULT VENTURE
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Venture MULT quart','A2:AX611');
stringVectors = string(raw(:,[4,5,6,8,17,18,19,20,21,22,26,27,28,29]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,13,14,15,16,23,24,25,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
QUART_VENTURE_MULT = table;

% Allocate imported array to column variable names
QUART_VENTURE_MULT.number = data(:,1);
QUART_VENTURE_MULT.FUNDID = data(:,2);
QUART_VENTURE_MULT.FIRMID = data(:,3);
QUART_VENTURE_MULT.teamID = stringVectors(:,1);
QUART_VENTURE_MULT.teamIDvintage = stringVectors(:,2);
QUART_VENTURE_MULT.teamIDvintagenumber = stringVectors(:,3);
QUART_VENTURE_MULT.VINTAGEYEAR = data(:,4);
QUART_VENTURE_MULT.STRATEGY = categorical(stringVectors(:,4));
QUART_VENTURE_MULT.BuyoutDummy = data(:,5);
QUART_VENTURE_MULT.mult_t = data(:,6);
QUART_VENTURE_MULT.mult_t1 = data(:,7);
QUART_VENTURE_MULT.quart_t1 = data(:,8);
QUART_VENTURE_MULT.q1 = data(:,9);
QUART_VENTURE_MULT.q2 = data(:,10);
QUART_VENTURE_MULT.q3 = data(:,11);
QUART_VENTURE_MULT.q4 = data(:,12);
QUART_VENTURE_MULT.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
QUART_VENTURE_MULT.COUNTRIES = categorical(stringVectors(:,6));
QUART_VENTURE_MULT.REGIONS = stringVectors(:,7);
QUART_VENTURE_MULT.STATUS = categorical(stringVectors(:,8));
QUART_VENTURE_MULT.FUNDMANAGER = stringVectors(:,9);
QUART_VENTURE_MULT.DOMICILE = stringVectors(:,10);
QUART_VENTURE_MULT.FINALCLOSESIZE = data(:,13);
QUART_VENTURE_MULT.size_ = data(:,14);
QUART_VENTURE_MULT.seq_num_t = data(:,15);
QUART_VENTURE_MULT.COREINDUSTRIES = categorical(stringVectors(:,11));
QUART_VENTURE_MULT.INDUSTRIES = categorical(stringVectors(:,12));
QUART_VENTURE_MULT.MANAGERREGION = categorical(stringVectors(:,13));
QUART_VENTURE_MULT.MANAGERCOUNTRY = categorical(stringVectors(:,14));
QUART_VENTURE_MULT.vint90 = data(:,16);
QUART_VENTURE_MULT.vint91 = data(:,17);
QUART_VENTURE_MULT.vint92 = data(:,18);
QUART_VENTURE_MULT.vint93 = data(:,19);
QUART_VENTURE_MULT.vint94 = data(:,20);
QUART_VENTURE_MULT.vint95 = data(:,21);
QUART_VENTURE_MULT.vint96 = data(:,22);
QUART_VENTURE_MULT.vint97 = data(:,23);
QUART_VENTURE_MULT.vint98 = data(:,24);
QUART_VENTURE_MULT.vint99 = data(:,25);
QUART_VENTURE_MULT.vint00 = data(:,26);
QUART_VENTURE_MULT.vint01 = data(:,27);
QUART_VENTURE_MULT.vint02 = data(:,28);
QUART_VENTURE_MULT.vint03 = data(:,29);
QUART_VENTURE_MULT.vint04 = data(:,30);
QUART_VENTURE_MULT.vint05 = data(:,31);
QUART_VENTURE_MULT.vint06 = data(:,32);
QUART_VENTURE_MULT.vint07 = data(:,33);
QUART_VENTURE_MULT.vint08 = data(:,34);
QUART_VENTURE_MULT.vint09 = data(:,35);
QUART_VENTURE_MULT.vint10 = data(:,36);

% Clear temporary variables
clearvars data raw stringVectors;

% Log size and interaction terms
QUART_VENTURE_MULT.size_t=log(QUART_VENTURE_MULT.size_);
QUART_VENTURE_MULT.quart1=QUART_VENTURE_MULT.q1.*QUART_VENTURE_MULT.mult_t1;
QUART_VENTURE_MULT.quart2=QUART_VENTURE_MULT.q2.*QUART_VENTURE_MULT.mult_t1;
QUART_VENTURE_MULT.quart3=QUART_VENTURE_MULT.q3.*QUART_VENTURE_MULT.mult_t1;
 
% Linear regression regmult 
 
regmult = fitlm(QUART_VENTURE_MULT, 'mult_t~seq_num_t + size_t + mult_t1 + quart1 + quart2 + quart3 + q1 + q2 + q3+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% % 
% regmult = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 30 terms in 29 predictors]
% 
% Estimated Coefficients:
%                     Estimate        SE         tStat        pValue  
%                    __________    ________    _________    __________
% 
%     (Intercept)        2.0344      1.6406       1.2401       0.21545
%     mult_t1          -0.99248     0.60075      -1.6521      0.099062
%     q1               -0.21245     0.45946     -0.46239       0.64398
%     q2               -0.18163     0.53092     -0.34211       0.73239
%     q3                -1.6791     0.54268       -3.094     0.0020699
%     seq_num_t       -0.021785    0.042306     -0.51492        0.6068
%     vint91            0.17598       2.185     0.080543       0.93583
%     vint92         -0.0091234      1.8185    -0.005017         0.996
%     vint93            -1.8169      1.9956     -0.91043       0.36297
%     vint94             4.6808      1.6689       2.8047     0.0052052
%     vint95             3.9136      1.6883       2.3181      0.020793
%     vint96             1.6885       1.647       1.0252       0.30569
%     vint97             0.4929      1.5873      0.31052       0.75628
%     vint98           -0.67371      1.5777     -0.42701       0.66953
%     vint99            -1.6481      1.5701      -1.0497       0.29429
%     vint00            -1.1663      1.5609     -0.74722       0.45524
%     vint01           -0.69882      1.5767     -0.44321       0.65778
%     vint02           -0.84455      1.6008     -0.52757         0.598
%     vint03           -0.72063      1.6103     -0.44751       0.65467
%     vint04           -0.77806      1.6073     -0.48408       0.62851
%     vint05            -0.4961      1.5875      -0.3125       0.75478
%     vint06           -0.50222      1.5733     -0.31922       0.74968
%     vint07           -0.48256      1.5717     -0.30703       0.75893
%     vint08          0.0024836      1.5745    0.0015774       0.99874
%     vint09           -0.36748      1.6121     -0.22795       0.81977
%     vint10           -0.45858      1.5974     -0.28708       0.77415
%     size_t           0.064531    0.075783      0.85152       0.39483
%     quart1             1.1205     0.60008       1.8672      0.062372
%     quart2            0.93618     0.61382       1.5252       0.12776
%     quart3             2.3236     0.65185       3.5646    0.00039443
% 
% 
% Number of observations: 610, Error degrees of freedom: 580
% Root Mean Squared Error: 2.17
% R-squared: 0.282,  Adjusted R-Squared 0.246
% F-statistic vs. constant model: 7.87, p-value = 7.52e-27
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult.NumObservations;
k = regmult.NumEstimatedCoefficients;
u = regmult.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult.Residuals.Raw);
Stdev_u = std(regmult.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -1.4130e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9641
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
QUART_VENTURE_MULT.uRaw = regmult.Residuals.Raw; 
QUART_VENTURE_MULT.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
multWhite = table(QUART_VENTURE_MULT.mult_t, QUART_VENTURE_MULT.seq_num_t, QUART_VENTURE_MULT.size_t, QUART_VENTURE_MULT.mult_t1, QUART_VENTURE_MULT.quart1,QUART_VENTURE_MULT.quart2,QUART_VENTURE_MULT.quart3,QUART_VENTURE_MULT.q1,QUART_VENTURE_MULT.q2,QUART_VENTURE_MULT.q3,...
QUART_VENTURE_MULT.vint91, QUART_VENTURE_MULT.vint92, QUART_VENTURE_MULT.vint93, QUART_VENTURE_MULT.vint94, ...
    QUART_VENTURE_MULT.vint95, QUART_VENTURE_MULT.vint96, QUART_VENTURE_MULT.vint97, QUART_VENTURE_MULT.vint98, QUART_VENTURE_MULT.vint99,QUART_VENTURE_MULT.vint00, QUART_VENTURE_MULT.vint01, QUART_VENTURE_MULT.vint02, QUART_VENTURE_MULT.vint03, QUART_VENTURE_MULT.vint04,...
    QUART_VENTURE_MULT.vint05, QUART_VENTURE_MULT.vint06, QUART_VENTURE_MULT.vint07, QUART_VENTURE_MULT.vint08, QUART_VENTURE_MULT.vint09, QUART_VENTURE_MULT.vint10, QUART_VENTURE_MULT.size_t.^2, QUART_VENTURE_MULT.mult_t1.^2, QUART_VENTURE_MULT.quart1.^2,  QUART_VENTURE_MULT.quart2.^2,QUART_VENTURE_MULT.quart3.^2,QUART_VENTURE_MULT.uRaw2);
 
multWhite.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'quart1','quart2','quart3','q1','q2','q3','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq', 'quart1sq', 'quart2sq','quart3sq','uRaw2'};
 
% Unrestricted regression
regmultWhite=fitlm(multWhite,'uRaw2 ~ seq_num_t + size_t + mult_t1 + quart1 + quart2 + quart3 + q1 + q2 + q3+ vint91 +vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq + quart1sq + quart2sq + quart3sq ')
 
% regmultWhite = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 35 terms in 34 predictors]
% 
% Estimated Coefficients:
%                     Estimate       SE         tStat       pValue  
%                    __________    _______    _________    _________
% 
%     (Intercept)        8.3223     26.231      0.31726      0.75116
%     seq_num_t      -0.0082717    0.58206    -0.014211      0.98867
%     size_t             1.1946     5.5818      0.21402      0.83061
%     mult_t1            -11.39     24.482     -0.46522      0.64195
%     quart1             15.152     24.499      0.61849       0.5365
%     quart2             5.4551     25.919      0.21047      0.83338
%     quart3            -17.244     26.292     -0.65587      0.51217
%     q1                -15.322     9.0634      -1.6905     0.091467
%     q2                -3.2306       12.5     -0.25845      0.79615
%     q3                 8.9289     11.417      0.78205      0.43451
%     vint91             10.624     29.861      0.35577      0.72214
%     vint92             2.0693     24.791     0.083473       0.9335
%     vint93            0.11339      27.38    0.0041414       0.9967
%     vint94             72.029     22.886       3.1473    0.0017331
%     vint95             28.212     23.145       1.2189      0.22337
%     vint96             14.501     22.544      0.64321      0.52034
%     vint97             4.3537     21.653      0.20107      0.84071
%     vint98             3.2321     21.541      0.15004      0.88079
%     vint99             -3.379     21.464     -0.15742      0.87497
%     vint00            -2.9902     21.309     -0.14033      0.88845
%     vint01            -2.6879     21.523     -0.12488      0.90066
%     vint02            -2.2888     21.861      -0.1047      0.91665
%     vint03            -2.3285     21.959     -0.10604      0.91559
%     vint04            -2.1712      21.92    -0.099049      0.92113
%     vint05           -0.93963     21.654    -0.043392       0.9654
%     vint06            -1.8405     21.466    -0.085738       0.9317
%     vint07            -2.0164     21.437    -0.094062      0.92509
%     vint08             1.8603     21.491     0.086561      0.93105
%     vint09           -0.97016     22.004    -0.044089      0.96485
%     vint10             -1.837     21.784    -0.084326      0.93283
%     size_sq          -0.12958      0.601     -0.21561      0.82937
%     mult_t1sq         -5.6453     15.777     -0.35782      0.72061
%     quart1sq           5.5634     15.778      0.35262      0.72451
%     quart2sq           6.4741     15.868        0.408      0.68343
%     quart3sq           14.837     15.953      0.93006      0.35273
% 
% 
% Number of observations: 610, Error degrees of freedom: 575
% Root Mean Squared Error: 29.5
% R-squared: 0.187,  Adjusted R-Squared 0.139
% F-statistic vs. constant model: 3.89, p-value = 7.15e-12
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmultWhite.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  113.9797, Crit_Val_e =  42.5570, P_e = 5.0572e-12, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [QUART_VENTURE_MULT.seq_num_t QUART_VENTURE_MULT.size_t QUART_VENTURE_MULT.mult_t1 QUART_VENTURE_MULT.quart1...
    QUART_VENTURE_MULT.quart2 QUART_VENTURE_MULT.quart3 QUART_VENTURE_MULT.q1 QUART_VENTURE_MULT.q2 QUART_VENTURE_MULT.q3 QUART_VENTURE_MULT.vint91...
      QUART_VENTURE_MULT.vint92  QUART_VENTURE_MULT.vint93 QUART_VENTURE_MULT.vint94 QUART_VENTURE_MULT.vint95  QUART_VENTURE_MULT.vint96  QUART_VENTURE_MULT.vint97...
    QUART_VENTURE_MULT.vint98 QUART_VENTURE_MULT.vint99 QUART_VENTURE_MULT.vint00 QUART_VENTURE_MULT.vint01 QUART_VENTURE_MULT.vint02...
    QUART_VENTURE_MULT.vint03 QUART_VENTURE_MULT.vint04 QUART_VENTURE_MULT.vint05 QUART_VENTURE_MULT.vint06 QUART_VENTURE_MULT.vint07 QUART_VENTURE_MULT.vint08...
    QUART_VENTURE_MULT.vint09 QUART_VENTURE_MULT.vint10];
y = QUART_VENTURE_MULT.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'quart1', 'quart2' , 'quart3', ...
    'q1','q2','q3','vint91','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
 
% White =
% 
%   30?4 table
% 
%                       beta          SE         tstat          p     
%                    __________    ________    _________    __________
% 
%     (Intercept)        2.0344      0.7552       2.6939      0.007258
%     seq_num_t       -0.021785    0.024137     -0.90254       0.36713
%     size_t           0.064531    0.050428       1.2797       0.20116
%     mult_t1          -0.99248      0.8665      -1.1454        0.2525
%     quart1             1.1205     0.86423       1.2965       0.19529
%     quart2            0.93618     0.79574       1.1765       0.23986
%     quart3             2.3236      1.0684       2.1749      0.030026
%     q1               -0.21245     0.51304     -0.41409       0.67895
%     q2               -0.18163     0.51293     -0.35411       0.72338
%     q3                -1.6791      1.0217      -1.6434       0.10083
%     vint91            0.17598     0.47593      0.36976       0.71169
%     vint92         -0.0091234     0.35866    -0.025438       0.97971
%     vint93            -1.8169     0.85286      -2.1303      0.033545
%     vint94             4.6808      2.6972       1.7354      0.083178
%     vint95             3.9136      1.6762       2.3348      0.019877
%     vint96             1.6885      1.3902       1.2145       0.22501
%     vint97             0.4929     0.60025      0.82116       0.41188
%     vint98           -0.67371     0.41204      -1.6351       0.10255
%     vint99            -1.6481      0.3343      -4.9301    1.0616e-06
%     vint00            -1.1663     0.30762      -3.7914    0.00016474
%     vint01           -0.69882     0.34635      -2.0177      0.044061
%     vint02           -0.84455     0.35213      -2.3984      0.016768
%     vint03           -0.72063      0.3395      -2.1226      0.034189
%     vint04           -0.77806     0.33113      -2.3497      0.019105
%     vint05            -0.4961     0.37652      -1.3176       0.18814
%     vint06           -0.50222     0.31737      -1.5824       0.11408
%     vint07           -0.48256     0.33693      -1.4322        0.1526
%     vint08          0.0024836      0.3971    0.0062544       0.99501
%     vint09           -0.36748     0.30626      -1.1999       0.23065
%     vint10           -0.45858      0.3119      -1.4703         0.142
 
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(QUART_VENTURE_MULT.seq_num_t,u)
corr_size_t_u = corr(QUART_VENTURE_MULT.size_t,u)
corr_mult_t1_u = corr(QUART_VENTURE_MULT.mult_t1,u)
corr_quart1_u = corr(QUART_VENTURE_MULT.quart1,u)
corr_quart2_u = corr(QUART_VENTURE_MULT.quart2,u)
corr_quart3_u = corr(QUART_VENTURE_MULT.quart3,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 5.3347
% kurt_jbtest = 51.5906
% test_stat_jbtest = 6.2903e+04
% crit_val_jbtest = 45.7223
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult.Diagnostics.CooksDistance >=0.05);
Outliers = QUART_VENTURE_MULT(regmult.Diagnostics.CooksDistance >=0.05,:);
 
% MULT BUYOUT
clc
clear all
close all

% Import the data
[~, ~, raw] = xlsread('final_tables.xlsx','Buyout MULT quart','A2:AX719');
stringVectors = string(raw(:,[4,5,6,8,17,18,19,20,21,22,26,27,28,29]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[1,2,3,7,9,10,11,12,13,14,15,16,23,24,25,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]);

% Create output variable
data = reshape([raw{:}],size(raw));

% Create table
QUART_BUYOUT_MULT = table;

% Allocate imported array to column variable names
QUART_BUYOUT_MULT.number = data(:,1);
QUART_BUYOUT_MULT.FUNDID = data(:,2);
QUART_BUYOUT_MULT.FIRMID = data(:,3);
QUART_BUYOUT_MULT.teamID = stringVectors(:,1);
QUART_BUYOUT_MULT.teamIDvintage = stringVectors(:,2);
QUART_BUYOUT_MULT.teamIDvintagenumber = stringVectors(:,3);
QUART_BUYOUT_MULT.VINTAGEYEAR = data(:,4);
QUART_BUYOUT_MULT.STRATEGY = categorical(stringVectors(:,4));
QUART_BUYOUT_MULT.BuyoutDummy = data(:,5);
QUART_BUYOUT_MULT.mult_t = data(:,6);
QUART_BUYOUT_MULT.mult_t1 = data(:,7);
QUART_BUYOUT_MULT.quart_t1 = data(:,8);
QUART_BUYOUT_MULT.q1 = data(:,9);
QUART_BUYOUT_MULT.q2 = data(:,10);
QUART_BUYOUT_MULT.q3 = data(:,11);
QUART_BUYOUT_MULT.q4 = data(:,12);
QUART_BUYOUT_MULT.PRIMARYFUNDFOCUS = categorical(stringVectors(:,5));
QUART_BUYOUT_MULT.COUNTRIES = categorical(stringVectors(:,6));
QUART_BUYOUT_MULT.REGIONS = stringVectors(:,7);
QUART_BUYOUT_MULT.STATUS = categorical(stringVectors(:,8));
QUART_BUYOUT_MULT.FUNDMANAGER = stringVectors(:,9);
QUART_BUYOUT_MULT.DOMICILE = stringVectors(:,10);
QUART_BUYOUT_MULT.FINALCLOSESIZE = data(:,13);
QUART_BUYOUT_MULT.size_ = data(:,14);
QUART_BUYOUT_MULT.seq_num_t = data(:,15);
QUART_BUYOUT_MULT.COREINDUSTRIES = categorical(stringVectors(:,11));
QUART_BUYOUT_MULT.INDUSTRIES = categorical(stringVectors(:,12));
QUART_BUYOUT_MULT.MANAGERREGION = categorical(stringVectors(:,13));
QUART_BUYOUT_MULT.MANAGERCOUNTRY = categorical(stringVectors(:,14));
QUART_BUYOUT_MULT.vint90 = data(:,16);
QUART_BUYOUT_MULT.vint91 = data(:,17);
QUART_BUYOUT_MULT.vint92 = data(:,18);
QUART_BUYOUT_MULT.vint93 = data(:,19);
QUART_BUYOUT_MULT.vint94 = data(:,20);
QUART_BUYOUT_MULT.vint95 = data(:,21);
QUART_BUYOUT_MULT.vint96 = data(:,22);
QUART_BUYOUT_MULT.vint97 = data(:,23);
QUART_BUYOUT_MULT.vint98 = data(:,24);
QUART_BUYOUT_MULT.vint99 = data(:,25);
QUART_BUYOUT_MULT.vint00 = data(:,26);
QUART_BUYOUT_MULT.vint01 = data(:,27);
QUART_BUYOUT_MULT.vint02 = data(:,28);
QUART_BUYOUT_MULT.vint03 = data(:,29);
QUART_BUYOUT_MULT.vint04 = data(:,30);
QUART_BUYOUT_MULT.vint05 = data(:,31);
QUART_BUYOUT_MULT.vint06 = data(:,32);
QUART_BUYOUT_MULT.vint07 = data(:,33);
QUART_BUYOUT_MULT.vint08 = data(:,34);
QUART_BUYOUT_MULT.vint09 = data(:,35);
QUART_BUYOUT_MULT.vint10 = data(:,36);

% Clear temporary variables
clearvars data raw stringVectors;

% Log size and interaction terms
QUART_BUYOUT_MULT.size_t=log(QUART_BUYOUT_MULT.size_);
QUART_BUYOUT_MULT.quart1=QUART_BUYOUT_MULT.q1.*QUART_BUYOUT_MULT.mult_t1;
QUART_BUYOUT_MULT.quart2=QUART_BUYOUT_MULT.q2.*QUART_BUYOUT_MULT.mult_t1;
QUART_BUYOUT_MULT.quart3=QUART_BUYOUT_MULT.q3.*QUART_BUYOUT_MULT.mult_t1;
 
% Linear regression regmult 
 
regmult = fitlm(QUART_BUYOUT_MULT, 'mult_t~seq_num_t + size_t + mult_t1 + quart1 + quart2 + quart3 + q1 + q2 + q3+ vint91 + vint92 + vint93 + vint94 + vint95 + vint96 +vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 ')
% % 
% regmult = 
% 
% 
% Linear regression model:
%     mult_t ~ [Linear formula with 30 terms in 29 predictors]
% 
% Estimated Coefficients:
%                     Estimate         SE         tStat         pValue  
%                    ___________    ________    __________    __________
% 
%     (Intercept)         3.1359     0.75391        4.1595    3.5934e-05
%     mult_t1            0.32785     0.18825        1.7416      0.082031
%     q1                 0.34716     0.26773        1.2967       0.19517
%     q2                 0.51529     0.28856        1.7857      0.074584
%     q3                -0.14225     0.31654      -0.44937        0.6533
%     seq_num_t      -3.4832e-05    0.018201    -0.0019137       0.99847
%     vint91                   0           0           NaN           NaN
%     vint92             -1.0022     0.82863       -1.2095        0.2269
%     vint93            -0.80997     0.79877        -1.014       0.31093
%     vint94            -0.48518     0.76922      -0.63074       0.52842
%     vint95             -1.6665     0.73805        -2.258      0.024259
%     vint96             -1.2172     0.74017       -1.6444       0.10054
%     vint97             -1.9732     0.72773       -2.7115     0.0068661
%     vint98             -1.8219     0.72515       -2.5124      0.012218
%     vint99             -1.7046     0.72552       -2.3495      0.019081
%     vint00             -1.2663     0.72345       -1.7503       0.08051
%     vint01             -1.2055     0.72996       -1.6514      0.099112
%     vint02             -1.4422     0.73096        -1.973      0.048899
%     vint03             -1.5453     0.72773       -2.1234      0.034078
%     vint04             -1.4897     0.72584       -2.0524      0.040506
%     vint05             -1.6712     0.72058       -2.3192      0.020677
%     vint06             -1.7142     0.72178        -2.375      0.017822
%     vint07             -1.5615     0.72143       -2.1645      0.030769
%     vint08             -1.4743     0.72267       -2.0401      0.041725
%     vint09             -1.4846     0.72646       -2.0436      0.041375
%     vint10             -1.4695     0.72483       -2.0274      0.043008
%     size_t           -0.045405    0.022585       -2.0104      0.044776
%     quart1            -0.21711     0.19698       -1.1022       0.27076
%     quart2            -0.31206     0.20968       -1.4883       0.13713
%     quart3           -0.012956     0.23773     -0.054501       0.95655
% 
% 
% Number of observations: 718, Error degrees of freedom: 689
% Root Mean Squared Error: 0.713
% R-squared: 0.144,  Adjusted R-Squared 0.109
% F-statistic vs. constant model: 4.13, p-value = 1.71e-11
 
%DIAGNOSTICS of CLRM assumptions
 
T = regmult.NumObservations;
k = regmult.NumEstimatedCoefficients;
u = regmult.Residuals.Raw;
alpha = 0.05;
 
% 1) expected value (mean) of the residuals is equal to zero, E[u]=0
 
% H0: meanresidual = 0 vs. H1: meanresidual unequal 0 (two-sided test) 
Mean_u = mean(regmult.Residuals.Raw);
Stdev_u = std(regmult.Residuals.Raw);
 
% Test Statistic
tStat_d = (Mean_u - 0)/(Stdev_u/sqrt(T))
%output: tStat_d = -2.3552e-14
 
 
% Critical Value
Crit_Val_d = tinv(1-1/2*alpha, T-k)
%output: Crit_Val_d =  1.9634
 
% P-value
P_d = 2*(1-tcdf(abs(tStat_d),T-k))
%output: P_d =  1.0000
 
% Rejection
reject_d = abs(tStat_d) > abs(Crit_Val_d)
%output: reject_d = 0
 
% Canot reject H0 at 5%, so the expected value of u is zero. 
% No, the first assumption of CLRM is not violated.
 
%2) homoscedasticity, Var[u,t] = sigma^2 < infinity
 
% White's test
 
 
QUART_BUYOUT_MULT.uRaw = regmult.Residuals.Raw; 
QUART_BUYOUT_MULT.uRaw2 = u.^2;
 
 
% Create a table with the constant and squared terms
multWhite = table(QUART_BUYOUT_MULT.mult_t, QUART_BUYOUT_MULT.seq_num_t, QUART_BUYOUT_MULT.size_t, QUART_BUYOUT_MULT.mult_t1, QUART_BUYOUT_MULT.quart1,QUART_BUYOUT_MULT.quart2,QUART_BUYOUT_MULT.quart3,QUART_BUYOUT_MULT.q1,QUART_BUYOUT_MULT.q2,QUART_BUYOUT_MULT.q3,...
QUART_BUYOUT_MULT.vint91, QUART_BUYOUT_MULT.vint92, QUART_BUYOUT_MULT.vint93, QUART_BUYOUT_MULT.vint94, ...
    QUART_BUYOUT_MULT.vint95, QUART_BUYOUT_MULT.vint96, QUART_BUYOUT_MULT.vint97, QUART_BUYOUT_MULT.vint98, QUART_BUYOUT_MULT.vint99,QUART_BUYOUT_MULT.vint00, QUART_BUYOUT_MULT.vint01, QUART_BUYOUT_MULT.vint02, QUART_BUYOUT_MULT.vint03, QUART_BUYOUT_MULT.vint04,...
    QUART_BUYOUT_MULT.vint05, QUART_BUYOUT_MULT.vint06, QUART_BUYOUT_MULT.vint07, QUART_BUYOUT_MULT.vint08, QUART_BUYOUT_MULT.vint09, QUART_BUYOUT_MULT.vint10, QUART_BUYOUT_MULT.size_t.^2, QUART_BUYOUT_MULT.mult_t1.^2, QUART_BUYOUT_MULT.quart1.^2,  QUART_BUYOUT_MULT.quart2.^2,QUART_BUYOUT_MULT.quart3.^2,QUART_BUYOUT_MULT.uRaw2);
 
multWhite.Properties.VariableNames={'mult_t','seq_num_t','size_t','mult_t1', 'quart1','quart2','quart3','q1','q2','q3','vint91','vint92','vint93','vint94', ...
    'vint95', 'vint96', 'vint97', 'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10','size_sq','mult_t1sq', 'quart1sq', 'quart2sq','quart3sq','uRaw2'};
 
% Unrestricted regression
regmultWhite=fitlm(multWhite,'uRaw2 ~ seq_num_t + size_t + mult_t1 + quart1 + quart2 + quart3 + q1 + q2 + q3+ vint91 +vint92 + vint93 + vint94 + vint95+vint96 + vint97 + vint98 + vint99 + vint00 + vint01 + vint02 + vint03 + vint04 + vint05 + vint06 + vint07 + vint08 + vint09 + vint10 +size_sq + mult_t1sq + quart1sq + quart2sq + quart3sq ')
 
% regmultWhite = 
% 
% 
% Linear regression model:
%     uRaw2 ~ [Linear formula with 35 terms in 34 predictors]
% 
% Estimated Coefficients:
%                    Estimate        SE         tStat        pValue  
%                    _________    ________    _________    __________
% 
%     (Intercept)            0           0          NaN           NaN
%     seq_num_t      -0.011029    0.044547     -0.24757       0.80454
%     size_t           -1.9974     0.36544      -5.4658    6.4663e-08
%     mult_t1         -0.65622      1.9934      -0.3292       0.74211
%     quart1           0.41053      2.0522      0.20004       0.84151
%     quart2           0.87913      2.1307       0.4126       0.68003
%     quart3           0.57515      2.6045      0.22083       0.82529
%     q1              -0.09719      1.3127    -0.074037         0.941
%     q2              -0.84969      1.4028      -0.6057       0.54491
%     q3              -0.63531      1.7756     -0.35781       0.72059
%     vint91            7.0802      2.2909       3.0906     0.0020786
%     vint92            8.1369      1.8603       4.3738    1.4113e-05
%     vint93            9.1194      1.7934        5.085    4.7539e-07
%     vint94            9.3796      1.6769       5.5935    3.2209e-08
%     vint95            7.8768      1.6224       4.8549    1.4939e-06
%     vint96             10.79      1.6228        6.649    6.0436e-11
%     vint97            7.7734      1.6039       4.8464    1.5574e-06
%     vint98            7.9503      1.5941       4.9872    7.7789e-07
%     vint99            7.9986       1.616       4.9496    9.3788e-07
%     vint00            7.6719      1.5737       4.8749    1.3548e-06
%     vint01            7.9289       1.602       4.9494    9.3883e-07
%     vint02            8.0058       1.618       4.9478    9.4604e-07
%     vint03            7.7139      1.5975       4.8287    1.6974e-06
%     vint04            7.8829      1.5968       4.9366    1.0002e-06
%     vint05            8.1648      1.5945       5.1206     3.965e-07
%     vint06             7.836      1.5901        4.928    1.0434e-06
%     vint07            7.8555      1.5783       4.9771    8.1802e-07
%     vint08            7.8428       1.588       4.9388    9.8934e-07
%     vint09            7.7158      1.6122       4.7858    2.0889e-06
%     vint10            8.3835      1.6159       5.1883    2.8015e-07
%     size_sq          0.13722    0.028251       4.8571     1.478e-06
%     mult_t1sq        0.31247     0.94714      0.32991       0.74157
%     quart1sq        -0.29021     0.94944     -0.30567       0.75995
%     quart2sq        -0.34009      0.9561      -0.3557       0.72217
%     quart3sq        -0.25288      1.0553     -0.23963       0.81069
% 
% 
% Number of observations: 718, Error degrees of freedom: 684
% Root Mean Squared Error: 1.7
% R-squared: 0.141,  Adjusted R-Squared 0.0992
% F-statistic vs. constant model: 3.39, p-value = 1.37e-09
 
% % Langrange Multiplier (LM) test
 
m = k - 1;
test_stat_e = regmultWhite.Rsquared.Ordinary * T
Crit_Val_e = chi2inv(1-alpha, m)
P_e = 1 - cdf('Chisquare', test_stat_e, m) 
reject_e = test_stat_e > Crit_Val_e
 
%output: test_stat_e =  101.0141, Crit_Val_e =  41.3371, P_e = 3.4663e-10, reject_e = 1
 
% Reject H0 at 5%, so there is heteroscedasticity
% Yes, the second assumption of CLRM is violated.
 
% Next step is to use heteroscedasticity-consistent standard errors.
 
x = [QUART_BUYOUT_MULT.seq_num_t QUART_BUYOUT_MULT.size_t QUART_BUYOUT_MULT.mult_t1 QUART_BUYOUT_MULT.quart1...
    QUART_BUYOUT_MULT.quart2 QUART_BUYOUT_MULT.quart3 QUART_BUYOUT_MULT.q1 QUART_BUYOUT_MULT.q2 QUART_BUYOUT_MULT.q3 ...
      QUART_BUYOUT_MULT.vint92  QUART_BUYOUT_MULT.vint93 QUART_BUYOUT_MULT.vint94 QUART_BUYOUT_MULT.vint95  QUART_BUYOUT_MULT.vint96  QUART_BUYOUT_MULT.vint97...
    QUART_BUYOUT_MULT.vint98 QUART_BUYOUT_MULT.vint99 QUART_BUYOUT_MULT.vint00 QUART_BUYOUT_MULT.vint01 QUART_BUYOUT_MULT.vint02...
    QUART_BUYOUT_MULT.vint03 QUART_BUYOUT_MULT.vint04 QUART_BUYOUT_MULT.vint05 QUART_BUYOUT_MULT.vint06 QUART_BUYOUT_MULT.vint07 QUART_BUYOUT_MULT.vint08...
    QUART_BUYOUT_MULT.vint09 QUART_BUYOUT_MULT.vint10];
y = QUART_BUYOUT_MULT.mult_t;
 
% White's estimation correction 
 
White = table();
const = 1; % 1: constant is included 
lags = 0; % No lags
[beta, tstat, ~, vcvm, ~, Rbar , ~ ] = olsnw(y, x);
 
White.beta = beta;
White.SE = sqrt(diag(vcvm));
White.tstat = tstat;
White.p = 2*(1-tcdf(abs(tstat),T-2));
White.Properties.RowNames = {'(Intercept)','seq_num_t','size_t','mult_t1', 'quart1', 'quart2' , 'quart3', ...
    'q1','q2','q3','vint92','vint93','vint94','vint95','vint96','vint97',...
    'vint98','vint99','vint00','vint01','vint02','vint03','vint04','vint05','vint06','vint07','vint08'...
    'vint09','vint10'};
White
 
% White =
% 
%   29?4 table
% 
%                       beta           SE         tstat           p     
%                    ___________    ________    __________    __________
% 
%     (Intercept)         3.1359     0.36355        8.6257             0
%     seq_num_t      -3.4832e-05    0.017679    -0.0019702       0.99843
%     size_t           -0.045405    0.023061       -1.9689      0.049347
%     mult_t1            0.32785     0.24622        1.3315       0.18343
%     quart1            -0.21711     0.23337      -0.93032       0.35252
%     quart2            -0.31206     0.25116       -1.2425       0.21445
%     quart3           -0.012956      0.2933     -0.044174       0.96478
%     q1                 0.34716      0.2771        1.2529       0.21067
%     q2                 0.51529      0.3098        1.6633      0.096687
%     q3                -0.14225     0.36472      -0.39001       0.69664
%     vint92             -1.0022     0.42567       -2.3544      0.018821
%     vint93            -0.80997     0.66212       -1.2233       0.22162
%     vint94            -0.48518     0.55696      -0.87113       0.38398
%     vint95             -1.6665     0.18419       -9.0477             0
%     vint96             -1.2172      0.5008       -2.4304      0.015325
%     vint97             -1.9732     0.11703       -16.861             0
%     vint98             -1.8219     0.13171       -13.832             0
%     vint99             -1.7046     0.12448       -13.694             0
%     vint00             -1.2663    0.090784       -13.948             0
%     vint01             -1.2055     0.13902       -8.6711             0
%     vint02             -1.4422     0.17752       -8.1239    1.9984e-15
%     vint03             -1.5453     0.13352       -11.573             0
%     vint04             -1.4897     0.12293       -12.119             0
%     vint05             -1.6712     0.11489       -14.546             0
%     vint06             -1.7142     0.11148       -15.378             0
%     vint07             -1.5615     0.11667       -13.384             0
%     vint08             -1.4743     0.11891       -12.398             0
%     vint09             -1.4846     0.11705       -12.683             0
%     vint10             -1.4695     0.15692       -9.3649             0
 
%3) covariance between the residuals over time is zero, i.e., Cov(ui,uj) =0 for i /= j. 
% We cant do it since we have no lags
 
%4) there is no relation between the residuals and the independent variables, i.e., Cov(u, x) = 0 for all i = 2, . . . , k 
 
corr_seq_num_u = corr(QUART_BUYOUT_MULT.seq_num_t,u)
corr_size_t_u = corr(QUART_BUYOUT_MULT.size_t,u)
corr_mult_t1_u = corr(QUART_BUYOUT_MULT.mult_t1,u)
corr_quart1_u = corr(QUART_BUYOUT_MULT.quart1,u)
corr_quart2_u = corr(QUART_BUYOUT_MULT.quart2,u)
corr_quart3_u = corr(QUART_BUYOUT_MULT.quart3,u)
 
%no correlation
 
%5) the residuals are normally distributed with mean zero and variance sigma^2, i.e., u distributed N (0, sigma^2)
% Jarque-Bera test
 
skew_jbtest = mean(u.^3) / (std(u)^2)^(3/2)
kurt_jbtest = mean(u.^4) / (std(u)^2)^2
test_stat_jbtest = T * ( skew_jbtest^2 / 6 + (kurt_jbtest - 3)^2 / 24) 
crit_val_jbtest = chi2inv(1-1/2*alpha, m)
p_jbtest = 1 - cdf('Chisquare', test_stat_jbtest, m)
reject_jbstest = test_stat_jbtest > crit_val_jbtest
 
% output: 
 
% skew_jbtest = 1.7979
% kurt_jbtest = 14.4052
% test_stat_jbtest = 4.2783e+03
% crit_val_jbtest = 44.4608
% p_jbtest = 0 
% reject_jbstest = 1
 
%Reject H0 at 5%.
% Yes, the fifth assumption of the CLRM is violated. Residuals are not
% normlly distributed. 
 
 
% Histogram and time series plot of the estimated residuals
figure(1)
histogram(u)
xlabel('Residual');
ylabel('Number of residuals'); 
title('Histogram of residuals');
 
figure(2)
plot(u, 'b'); 
legend({'u'});
ylabel('Estimated Residuals'); 
 
sum(regmult.Diagnostics.CooksDistance >=0.05);
Outliers = QUART_BUYOUT_MULT(regmult.Diagnostics.CooksDistance >=0.05,:);
