%% Test Consumption Model for Structural Change
% Conduct Chow tests to assess whether there are structural changes in the equation for food demand around World War II.
% 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.
% Suppose that you want to develop 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.
%
% 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];

% Data relationships appear to be affected by the war.
%
% Conduct two break point Chow tests at 5% level of significance. For the first test, set the break point at 1941. Set the break point of the other test at 1948.

bp = find(preWarIdx,1,'last');

h1941 = chowtest([LP LI],LQ,bp)     
h1948 = chowtest([LP LI],LQ,bp + 1)

% h1941 = 1 indicates that there is significant evidence reject the null hypothesis that the coefficients are stable when the break points occur before the war. However, h1948 = 0 indicates that there is not enough evidence to reject coefficient stability if the break point is after the war. This result suggests that the data at 1948 are influential.
%
% Alternatively, you can supply a vector of break points to conduct three Chow tests.

h = chowtest([LP LI],LQ,[bp bp+1]);

% By default, chowtest displays a summary of the test results for each test when you conduct more than one test.
% Copyright 2012 The MathWorks, Inc.