%% Inspect Consumption Model Coefficients for Structural Change
% Check coefficient estimates for instability in a model of food demand around World War II. Implement forward and backward recursive regressions in a rolling window.
% Load the U.S. food consumption data set, which contains annual measurements from 1927 through 1962 with missing data due to the war.

load Data_Consumption

% For more details on the data, enter Description at the command prompt.
%
% Plot the series.

P = Data(:,1); % Food price index
I = Data(:,2); % Disposable income index
Q = Data(:,3); % Food consumption index 

figure;
plot(dates,[P I Q],'o-')
axis tight
grid on
xlabel('Year')
ylabel('Index')
title('{\bf Time Series Plot of All Series}')
legend({'Price','Income','Consumption'},'Location','SE')

% Measurements are missing from 1942 through 1947, which correspond to World War II.
%
% To examine elasticities, apply the log transformation to each series.

LP = log(P);
LI = log(I);
LQ = log(Q);

% Consider a model in which log consumption is a linear function of the logs of food price and income. In other words,
% \texttt{LQ}_t=\beta_0+\beta_1\texttt{LI}_t+\beta_2\texttt{LP}+\varepsilon_t.
% \varepsilon_t is a Gaussian random variable with mean 0 and standard deviation \sigma^2.
%
% Identify the breakpoint index at the end of the war, 1945. Ignore missing years with missing data.

numCoeff = 4; % Three predictors and an intercept
T = numel(dates(~isnan(P))); % Sample size
bpIdx = find(dates(~isnan(P)) >= 1945,1) - numCoeff

% The 12th iteration corresponds to the end of the war.
%
% Plot forward recursive-regression coefficient estimates using a rolling window of size 1/4 the sample size. Indicate to plot the coefficients of LP and LI only in the same figure.

X = [LP LI];
y = LQ;
window = ceil(T*1/4);
recreg(X,y,'Window',window,'Plot','combined','PlotVars',[0 1 1],...
    'VarNames',{'Log-price' 'Log-income'});

%
% Plot forward recursive-regression coefficient estimates using a rolling window of size 1/3 the sample size.

window = ceil(T*1/3);
recreg(X,y,'Window',window,'Plot','combined','PlotVars',[0 1 1],...
    'VarNames',{'Log-price' 'Log-income'});

%
% Plot forward recursive-regression coefficient estimates using a rolling window of size 1/2 the sample size.

window = ceil(T*1/2);
recreg(X,y,'Window',window,'Plot','combined','PlotVars',[0 1 1],...
    'VarNames',{'Log-price' 'Log-income'});

% As the window size increases, the lines show less volatility, but the coefficients do exhibit instability.
% Copyright 2012 The MathWorks, Inc.