load AAPL.mat;
dd = AAPL;
n=size(dd,1);

% Selecting column 5
sp = dd(:,5);

% Generating lag values of prices
sp_0 = sp(2:n,1);
sp_1 = sp(1:n-1,1);

% Calculating returns
r = log (sp_0./sp_1);

% Defining regressors 
r_l1 = r(1:n-2);

% Defining regressands
r = r(2:n-1);

bfone = ones(size(r,1),1);
Reg1 = [bfone r_l1];

% Least squares estimator for alpha
phi_hat1 = (Reg1'*Reg1)\(Reg1'*r);
phi_hat1

% Variance estimnator
sigma_hat21 = (1/size(r,1))*(r-Reg1*phi_hat1)'*(r-Reg1*phi_hat1);
sigma_hat21

%COvariance matrix estoimation
var_phi1 = inv( (1/size(r,1))*(Reg1'*Reg1));
var_phi = sigma_hat21*var_phi1;


% Test 1.2 

R1 = [1 0];
g1 = 0;

aux_1=(R1*var_phi*R1')\(R1*phi_hat1-g1);

% Value of Test Statistic
test_st1=size(r,1)*(R1*phi_hat1-g1)'*aux_1;

% Critical value
critical1 = chi2inv(0.99,1);

[test_st1 critical1]

% Test 1.3

RS = [0 1];
gS = 0;
aux_S=(RS*var_phi*RS')\(RS*phi_hat1-gS);

% Value of Test Statistic
test_stS=size(r,1)*(RS*phi_hat1-gS)'*aux_S;

% Critical value
critical1 = chi2inv(0.99,1);

[test_stS critical1]

% Covariance estimates in item d

C_hat = sigma_hat21*(phi_hat1(2).^[1;2;3]);

C_hat






