%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fourth chapter: dividend-yield autoregression
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Copyright By PowCoder代写 加微信 powcoder
%Let’s clean the environment
clear variables
%Loading data
d=xlsread(‘../Data/predictability.xls’);
t=datetime(d(:,1),’ConvertFrom’,’excel’);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The dynamics of the dividend yield
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Always plot the data when you can
plot(t,d(:,4))
xlabel(‘Time in years’)
ylabel(‘The dividend yield’)
title(‘Time series plot’)
%Autocorrelation function (manually)
acf = ones(10,1);
for k = 1:10
acf(k) = corr(d(1:end-k,4),d(1+k:end,4));
%Autocorrelation function using commands
[acf1,l]= autocorr(d(:,4),’Numlags’,10);
acf1=acf1(2:end);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Estimating an AR(1) model for the dividend yield
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Table for parameters and inference
table_estimates = table(acf, acf1,’VariableNames’,…
[“autocorrelations (manual)” “autocorrelation with commands” ]);
disp(‘table of 10 autocorrelations’);
disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
disp(table_estimates);
disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
%First-order autoregression from first principles
y = d(2:end,4);
x1=d(1:end-1,4);
N = size(x1);
x=[ones(N(1),1),x1];
beta = inv(x’*x)*x’*y;
u = y-x*beta;
Var_u = (u’*u)/(N(1)-2);
Var_beta = Var_u*inv(x’*x);
se = sqrt(diag(Var_beta));
t_stat = beta./se;
table_estimates1 = table(beta,se,t_stat,’VariableNames’,…
[“Estimate” “SE” “tStat”],…
‘RowNames’,[“Intercept” “slope”]);
disp(‘Table of estimates: manual’);
disp(table_estimates1);
%Using the function “fitlm”
mod = fitlm(x1,y);
table_estimates2 = table(mod.Coefficients.Estimate,mod.Coefficients.SE,mod.Coefficients.tStat,’VariableNames’,…
[“Estimate” “SE” “tStat”],…
‘RowNames’,[“Intercept” “slope”]);
disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
disp(‘Table of estimates with fitlm’);
disp(table_estimates2);
%Using an assumption of Guassianity of the errors (which was not made before)
disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
[Estimation,variance,~,~] = estimate(arima(1,0,0),d(:,4));
Estimates = [Estimation.Constant;Estimation.AR{1}];
se = sqrt(diag(variance(1:2,1:2)));
t_stats = Estimates./se;
table_estimates3 = table(Estimates,se,t_stats,’VariableNames’,…
[“Estimate” “SE” “tStat”],…
‘RowNames’,[“Intercept” “slope”]);
disp(“%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%”)
disp(‘Table of estimates with arima’);
disp(table_estimates3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Diagnostics: plot series with residuals: are the residuals uncorrelated?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t1 = datetime(d(1:end-1,1),’ConvertFrom’,’excel’);
plot(t1,u);
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com