%% Model Structural Change
% Simulate data for a linear model including a structural break in the intercept and one of the predictor coefficients. Then, choose specific coefficients to test for equality across a break point using the Chow test. Adjust parameters to assess the sensitivity of the Chow test.
% Specify four predictors, 50 observations, and a break point at period 44 for the simulated linear model.

numPreds = 4;
numObs = 50;
bp = 44;
rng(1); % For reproducibility

%
% Form the predictor data by specifying means for the predictors, and then adding random, standard Gaussian noise to each of the means.

mu = [0 1 2 3];
X = repmat(mu,numObs,1) + randn(numObs,numPreds);

%
% Add a column of ones to the predictor data.

X = [ones(numObs,1) X];

%
% Specify the true values of the regression coefficients and that the intercept and the coefficient of the second predictor jump by 10%.

beta1 = [1 2 3 4 5]';                                % Initial subsample coefficients
beta2 = beta1 + [beta1(1)*0.1 0 beta1(3)*0.1 0 0 ]'; % Complementary subsample coefficients
X1 = X(1:bp,:);                                      % Initial subsample predictors
X2 = X(bp+1:end,:);                                  % Complementary subsample predictors

%
% Specify a 2-by-5 logical matrix that indicates to first test the intercept and second regression coefficient, and then test all other coefficients.

test1 = [true false true false false]; 
Coeffs = [test1; ~test1]

% The null hypothesis for the first test (Coeffs(1,:)) is equality of the intercepts and the coefficients of the second predictor across subsamples. The null hypothesis for second test (Coeffs(2,:)) is equality of the first, third, and fourth predictors across subsamples.
%
% Simulate data for the linear model
% \texttt{y} = \left[ {\begin{array}{*{20}{c}}
% {X1}&0\\
% 0&{X2}
% \end{array}} \right]\left[ {\begin{array}{*{20}{c}}
% {\texttt{beta1}}\\
% {\texttt{beta2}}
% \end{array}} \right] + \texttt{innov}.
% Create innov as a vector of random Gaussian variates with mean zero and standard deviation 0.2.

sigma = 0.2;
innov = sigma*randn(numObs,1);
y = [X1 zeros(bp,size(X2,2)); zeros(numObs - bp,size(X1,2)) X2]*[beta1; beta2]... 
    + innov;

%
% Conduct the two break point tests indicated in Coeffs. Because there is an intercept in the predictor matrix X already, specify to suppress its inclusion in the linear model that chowtest fits.

chowtest(X,y,bp,'Intercept',false,'Coeffs',Coeffs,'Display','summary');

% At the default significance level:
% The Chow test correctly rejects the null hypothesis that no structural breaks exist at period bp for the intercept and the second coefficient.
% Correctly failed to reject the null hypothesis for the other coefficients.
%
% Compare the break point test results to the results of the forecast test.

chowtest(X,y,bp,'Intercept',false,'Coeffs',Coeffs,'Test','forecast',...
    'Display','summary');

% In this case, the inferences from the tests are equivalent to those for the break point test.
% Copyright 2012 The MathWorks, Inc.