Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Consider the following model:

yt=β0+β1I{t>c}+ϵt.y_t = \beta_0 + \beta_1 I\{t > c\} + \epsilon_t.

This is known as a change-point model. The parameter cc is called the change-point. I{t>c}I\{t > c\} is the indicator function which takes the value 1 if t>ct > c and 0 otherwise. The function β0+β1I{t>c}\beta_0 + \beta_1 I\{t > c\} equals β0\beta_0 for times t≤ct \leq c and equals β0+β1\beta_0 + \beta_1 for times t>ct > c. Therefore this model states that the level of the time series equals β0\beta_0 until a time cc at which point it switches to β0+β1\beta_0 + \beta_1. The value of cc is therefore called the changepoint. From the given data y1,…,yny_1, \dots, y_n, we need to infer the parameter cc as well as β0,β1,σ\beta_0, \beta_1, \sigma. The unknown parameter cc makes it a nonlinear model. If cc were known, this will become a linear regression model with XX-matrix given by

Xc=(1I{1>c}1I{2>c}1I{3>c}⋅⋅⋅⋅⋅⋅1I{n>c})X_c = \begin{pmatrix} 1 & I\{1 > c\} \\ 1 & I\{2 > c\} \\ 1 & I\{3 > c\} \\ \cdot & \cdot \\ \cdot & \cdot \\ \cdot & \cdot \\ 1 & I\{n > c\} \end{pmatrix}

For parameter estimation and inference, we first compute RSS(cc):

RSS(c):=min⁡β0,β1∑t=1n(yt−β0−β1I{t>c})2RSS(c) := \min_{\beta_0, \beta_1} \sum_{t=1}^n (y_t - \beta_0 - \beta_1 I\{t > c\})^2

and then minimize over cc to obtain the MLE of c^\hat{c}. After finding c^\hat{c}, we can find the MLEs of the other parameters as in linear regression with known cc.

<Figure size 1500x600 with 1 Axes>

Is the changepoint visible from the plot above?

<Figure size 640x480 with 1 Axes>
5045
[-0.01930042  0.42189817]
[[-0.01930042  0.        ]
 [ 0.42189817  0.4       ]]
[1.00596521 1.00606582 1.        ]
<Figure size 640x480 with 1 Axes>

The Bayesian posterior for cc is:

∣XcTXc∣−1/2(1RSS(c))(n−p)/2|X_c^T X_c|^{-1/2} \left(\frac{1}{RSS(c)} \right)^{(n-p)/2}

where p=2p = 2 and ∣XcTXc∣|X_c^T X_c| denotes the determinant of XcTXcX_c^T X_c.

As before, it is better to compute the logarithm of the posterior (as opposed to the posterior directly) because of numerical issues.

<Figure size 640x480 with 1 Axes>

Let us exponentiate the log-posterior values to get the posterior.

<Figure size 640x480 with 1 Axes>

The following code generates posterior samples.

[[ 5.00800000e+03 -1.41424488e-02  3.84947207e-01  9.91760686e-01]
 [ 5.04800000e+03 -9.85234185e-03  4.18254840e-01  1.01846456e+00]
 [ 5.04500000e+03 -1.83004633e-02  4.20618034e-01  1.00653605e+00]
 ...
 [ 5.04800000e+03 -1.31685587e-02  4.33076689e-01  1.00957027e+00]
 [ 5.02100000e+03 -4.37556261e-02  4.68929293e-01  9.94190708e-01]
 [ 5.03000000e+03 -8.89227825e-03  4.18827031e-01  9.99898227e-01]]
<Figure size 1500x600 with 1 Axes>
<Figure size 1500x600 with 1 Axes>

Example with multiple change-points

We now consider an example where there are three change points and the model we want to use is:

yt=β0+β1I{t>c1}+β2I{t>c2}+β3I{t>c3}+ϵty_t = \beta_0 + \beta_1 I\{t > c_1\} + \beta_2 I\{t > c_2\} + \beta_3 I\{t > c_3\} + \epsilon_t

We work on a simulated dataset that is generated as follows.

<Figure size 640x480 with 1 Axes>
[100 200 300]

The following is the RSS for fitting three change points to the data.

x      104.000000
y      200.000000
z      306.000000
rss    399.896828
Name: 66535, dtype: float64
<Figure size 640x480 with 1 Axes>

The estimates are decent.

To obtain a faster algorithm for estimating c1,c2,c3c_1, c_2, c_3, we can try the following iterative algorithm. First we obtain c^1\hat{c}_1 by solving single change-point RSS minimization:

c^1=minimizer of min⁡β0,β1∑t=1n(yt−β0−β1I{t>c1})2.\hat{c}_1 = \text{minimizer of } \min_{\beta_0, \beta_1} \sum_{t=1}^n (y_t - \beta_0 - \beta_1 I\{t > c_1\})^2.

Then we obtain c^2\hat{c}_2 by two-change point RSS minimization with the first change-point fixed at c^1\hat{c}_1:

c^2=minimizer of min⁡β0,β1,β2∑t=1n(yt−β0−β1I{t>c^1}−β2I{t>c2})2.\hat{c}_2 = \text{minimizer of } \min_{\beta_0, \beta_1, \beta_2} \sum_{t=1}^n (y_t - \beta_0 - \beta_1 I\{t > \hat{c}_1\} - \beta_2 I\{t > c_2\})^2.

Finally we determine c^3\hat{c}_3 by three-change point RSS minimization with the first change-point fixed at c^1\hat{c}_1 and the second change-point fixed at c^2\hat{c}_2:

c^3=minimizer of min⁡β0,β1,β2,β3∑t=1n(yt−β0−β1I{t>c^1}−β2I{t>c^2}−β3I{t>c3})2.\hat{c}_3 = \text{minimizer of } \min_{\beta_0, \beta_1, \beta_2, \beta_3} \sum_{t=1}^n (y_t - \beta_0 - \beta_1 I\{t > \hat{c}_1\} - \beta_2 I\{t > \hat{c}_2\} - \beta_3 I\{t > c_3\})^2.

Here is the algorithm.

200
104
306

In this example, this iterative scheme (which runs much much faster than joint grid minimization) gives the same estimates as the full grid search.