%% Change the Bandwidth of a HAC Estimator
% This example shows how to change the bandwidth when estimating a HAC coefficient covariance, and compare estimates over varying bandwidths and kernels.
% How does the bandwidth affect HAC estimators? If you change it, are there large differences in the estimates, and, if so, are the differences practically significant? Explore bandwidth effects by estimating HAC coefficient covariances over a grid of bandwidths.
%% Load and Plot the Data.
% Determine how the cost of living affects the behavior of nominal wages. Load the Nelson Plosser data set to explore their statistical relationship.

load Data_NelsonPlosser
isNaN = any(ismissing(DataTable),2);       % Flag periods containing NaNs
cpi = DataTable.CPI(~isNaN); % Cost of living
wm = DataTable.WN(~isNaN);             % Nominal wages

figure
plot(cpi,wm,'o')
hFit = lsline; % Regression line
xlabel('Consumer Price Index (1967 = 100)')
ylabel('Nominal Wages (current $)')
legend(hFit,'OLS Line','Location','SE')
title('{\bf Cost of Living}')
grid on

% The plot suggests that a linear model might capture the relationship between the two variables.
%
%% Define the Model.
% Model the behavior of nominal wages with respect to CPI as this linear model.
% \texttt{wm}_t = \beta_0 + \beta_1\texttt{cpi}_t + \varepsilon_t

Mdl = fitlm(cpi,wm)
coeffCPI = Mdl.Coefficients.Estimate(2);
seCPI = Mdl.Coefficients.SE(2);

%
%% Plot Residuals.
% Plot the residuals from Mdl against the fitted values to assess heteroscedasticity and autocorrelation.

figure;
stem(Mdl.Residuals.Raw);
xlabel('Observation');
ylabel('Residual');
title('{\bf Linear Model Residuals}');
axis tight;
grid on;

% The residual plot shows varying levels of dispersion, which indicates heteroscedasticity. Neighboring residuals (with respect to observation) tend to have the same sign and magnitude, which indicates the presence of autocorrelation.
%
%% Estimate HAC standard errors.
% Obtain HAC standard errors over varying bandwidths using the Bartlett (for the Newey-West estimate) and quadratic spectral kernels.

numEstimates = 10;
stdErrBT = zeros(numEstimates,1);
stdErrQS = zeros(numEstimates,1);
for bw = 1:numEstimates
    [~,seBT] = hac(cpi,wm,'bandwidth',bw,'display','off'); ...
        % Newey-West
    [~,seQS] = hac(cpi,wm,'weights','QS','bandwidth',bw, ...
        'display','off'); % HAC using quadratic spectral kernel
    stdErrBT(bw) = seBT(2);
    stdErrQS(bw) = seQS(2);
end

% You can increase numEstimates to discover how increasing bandwidths affect the HAC estimates.
%
%% Plot the standard errors.
% Visually compare the Newey-West standard errors of  \hat\beta_1 to those using the quadratic spectral kernel over the bandwidth grid.

figure
hold on
hCoeff = plot(1:numEstimates,repmat(coeffCPI,numEstimates, ...
    1),'LineWidth',2);
hOLS = plot(1:numEstimates,repmat(coeffCPI+seCPI, ...
    numEstimates,1),'g--');
plot(1:numEstimates,repmat(coeffCPI-seCPI,numEstimates,1),'g--')
hBT = plot(1:numEstimates,coeffCPI+stdErrBT,'ro--');
plot(1:numEstimates,coeffCPI-stdErrBT,'ro--')
hQS = plot(1:numEstimates,coeffCPI+stdErrQS,'kp--',...
    'LineWidth',2);
plot(1:numEstimates,coeffCPI-stdErrQS,'kp--','LineWidth',2)
hold off
xlabel('Bandwidth')
ylabel('CPI Coefficient')
legend([hCoeff,hOLS,hBT,hQS],{'OLS Estimate', ...
    'OLS Standard Error','Newey-West SE', ...
    'Quadratic Spectral SE'},'Location','E')
title('{\bf CPI Coefficient Standard Errors}')
grid on

% The plot suggests that, for this data set, accounting for heteroscedasticity and autocorrelation using either HAC estimate results in more conservative intervals than the usual OLS standard error. The precision of the HAC estimates decreases as the bandwidth increases along the defined grid.
% For this data set, the Newey-West estimates are slightly more precise than those using the quadratic spectral kernel. This might be because the latter captures heteroscedasticity and autocorrelation better than the former.
% References:
% Andrews, D. W. K. "Heteroskedasticity and Autocorrelation Consistent Covariance Matrix Estimation." Econometrica. Vol. 59, 1991, pp. 817-858.
% Newey, W. K., and K. D. West. "A Simple, Positive Semi-definite, Heteroskedasticity and Autocorrelation Consistent Covariance Matrix." Econometrica. Vol. 55, No. 3, 1987, pp. 703-708.\
% Newey, W. K., and K. D. West. "Automatic Lag Selection in Covariance Matrix Estimation." The Review of Economic Studies. Vol. 61, No. 4, 1994, pp. 631-653.
% Copyright 2015 The MathWorks, Inc.