%% Test Consumption Model for Structural Change
% Conduct cusum tests to assess whether there are structural changes in the equation for food demand around World War II. Implement forward and backward recursive regressions to obtain the test statistics.
% 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.
% Consider a model for consumption as determined by food prices and disposable income, and assess its stability through the economic shock through the war.
%
% 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);

% Assume that 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 indices before World War II. Plot log consumption with respect to the logs of food price and income.

preWarIdx = (dates <= 1941);

figure
scatter3(LP(preWarIdx),LI(preWarIdx),LQ(preWarIdx),[],'ro');
hold on
scatter3(LP(~preWarIdx),LI(~preWarIdx),LQ(~preWarIdx),[],'b*');
legend({'Pre-war observations','Post-war observations'},...
    'Location','Best')
xlabel('Log price')
ylabel('Log income')
zlabel('Log consumption')
title('{\bf Food Consumption Data}')
% Get a better view
h = gca;
h.CameraPosition = [4.3 -12.2 5.3];

%
% Conduct forward and backward cusum tests using a 5% level of significance for each test. Plot the cusums.

cusumtest([LP,LI],LQ,'Direction',{'forward','backward'},'Plot','on');

% The plots and test results at the command line indicate that neither test rejects the null hypothesis that coefficients are stable.
%
% Compare the results of the cusum tests with the results of a Chow test. Unlike cusum tests, Chow tests require a guess for the time point at which the structural break occurs. Specify that the break point is 1941.

bp = find(preWarIdx,1,'last');
chowtest([LP,LI],LQ,bp,'Display','summary');

% The test results reject the null hypothesis that the coefficients are stable.
% The Chow and cusum test results are not consistent. For details on cusum test limitations, see Limitations.
% Copyright 2012 The MathWorks, Inc.