%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Chapter 3: return predictabiliyt and HAC standard errors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Copyright By PowCoder代写 加微信 powcoder
%Let’s clean the environment
clear variables
%Let’s load the data
d=xlsread(‘../Data/predictability.xls’);
ret_m = log(1+d(:,2));
ret_f = log(1+d(:,5));
ex_ret = ret_m – ret_f; %This is defining excess continuously-compounded market returns
N = length(ex_ret); %The sample size
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% We need to predict market excess returns
% We predict 1 month ahead (agg=1), 1-year ahead (agg=11) or 5-years ahead (agg=59)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
agg = 1; % This is a variable that you need to CHOOSE:
% You need to choose agg=1, agg=11 or agg=59 depending on the prediction horizon
dy = d(:,4); % The dividend series
y = ex_ret;
% If predicting 1-month ahead, the code will choose the following y and x
% (notice that dividends are lagged by one period)
y = ex_ret(2:N);
x1 = dy(1:N-1);
x = [ones(N-1,1),x1];
% If predicting 1-year or 5-year ahead, the code will choose the following y and x
% (notice that y is summed forward)
elseif agg == 11||59
y = movsum(y,[agg 0]);
y = y(2+agg:N-1);
x1 = dy(1:N-2-agg);
x = [ones(N-2-agg,1),x1];
% Estimating from first principles: We estimate the coefficients using LS
beta_LS = inv(x’*x)*(x’*y);
%From first principles: t-statistics
u = y – x*beta_LS;
N_u = length(u);
var_u = (u’*u)/(N_u-2);
var_beta = var_u*inv(x’*x);
tstat_inter1 = beta_LS(1,1)/sqrt(var_beta(1,1));
tstat_slope1 = beta_LS(2,1)/sqrt(var_beta(2,2));
tstat1 = [tstat_inter1;tstat_slope1];
% Running regressions using the function “fitlm” in Matlab: t-statistics
out = fitlm(x1,y);
tstat_inter2 = out.Coefficients.tStat(1);
tstat_slope2 = out.Coefficients.tStat(2);
tstat2 = [tstat_inter2;tstat_slope2];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Robust inference:
% Notice that the variance was computed under an assumption of homoskedasticity and zero autocorrelation of the errors
% We will now compute the robust (to homoskedasticity and autocorrelation) version
% This is what we called HAC variance
% As you will see, going from the standard variance to the HAC variance is really easy
% One only has to add k autocovariances (backward and forward) of u(i).*x(i) to the variance of u(i).*x(i) – this is done in 1 loop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
c = length(x1);
x2 = u.*x;
sum = (1/c)*(x2’*x2); % The sum in the loop below begins with the variance
% We then add (k) autocovariances forward and backward
k=agg; % Need to choose the number of lags (I choose the same number of lags as
% the level of aggregation)
sum = sum + ((k-i)/k)*(1/(c-i))*x2(1:c-i,:)’*x2(i+1:c,:) + ((k-i)/k)*(1/(c-i))*x2(i+1:c,:)’*x2(1:c-i,:);
var_betaHAC = c*inv(x’*x)*sum*inv(x’*x); % This is the “sandwich form” of the HAC variance
tstat_interHAC1 = beta_LS(1,1)/sqrt(var_betaHAC(1,1));
tstat_slopeHAC1 = beta_LS(2,1)/sqrt(var_betaHAC(2,2));
tstatHAC1 = [tstat_interHAC1;tstat_slopeHAC1];
% HAC standard errors using the function “hac” in Matlab
[~,seHAC]=hac(x1,y,’bandwidth’,agg,’display’,’off’);
tstat_interHAC2 = beta_LS(1,1)/seHAC(1,1);
tstat_slopeHAC2 = beta_LS(2,1)/seHAC(2,1);
tstatHAC2 = [tstat_interHAC2;tstat_slopeHAC2];
%Table for parameters and inference
table_estimates = table(beta_LS, tstat1,tstat2,tstatHAC1,tstatHAC2,’VariableNames’,…
[“Estimates_LS” “t_stats (manual)” “t_stats (function fitlm)” “t_stats (manual HAC)” “t_stats (function HAC)”],…
‘RowNames’,[“intercept” “slope”]);
disp(‘table of estimates’);
disp(table_estimates);
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com