In this tutorial, we demonstrate advanced use of the ChangepointInference
package.
Installation instructions are provided here.
First load the package:
To illustrate the software, we generate a synthetic dataset according to
and assume that $\mu_1,\ldots,\mu_T$ is piecewise constant, in the sense that $\mu_{\tau_j+1}=\mu_{\tau_j + 2 } = \ldots = \mu_{\tau_{j+1}}$, $\mu_{\tau_{j+1}} \neq \mu_{\tau_{j+1}+1}$, for $j=0,\ldots,K-1$, where $0 = \tau_{0} < \tau_{1} < \ldots < \tau_{K} < \tau_{K+1} = T$, and where $\tau_1,\ldots,\tau_K$ represent the true changepoints.
To estimate changepoints via $\ell_0$ segmentation, we use functional recursions (Rigaill, 2015; Maidstone, Hocking, Rigaill, & Fearnhead, 2017). In this section, we briefly describe these recursions and illustrate how this information can be extracted from our software.
Let $\mathrm{Cost}(y_{1:s}; u)$ be the cost of segmenting $y_{1:s}$ with $\mu_{s} = u$. Then $\mathrm{Cost}(y_{1:s}; u)$ can be efficiently computed: At the first timepoint, we have $\mathrm{Cost}(y_{1}; u) = \frac12(y_{1} - u)^{2}$; for any $s > 1$ and for all $u$,
For each $u$, this recursion encapsulates two possibilities: (i) there is no changepoint at the $(s-1)$st timepoint, and the optimal cost is equal to the previous cost plus the cost of a new data point, $\mathrm{Cost}(y_{1:(s-1)};u) + \frac12(y_{s} - u)^{2}$; (ii) there is a changepoint at the $(s-1)$st timepoint, and the optimal cost is equal to the optimal cost of segmenting up to $s-1$ plus the penalty for adding a changepoint at $s-1$ plus the cost of a new data point, $\min_{u’}{\mathrm{Cost}(y_{1:(s-1)};u’)} + \lambda + \frac12(y_{s} - u)^{2}$.
Setting functional_pruning_out = TRUE
allows us to examine $\mathrm{Cost}(y_{1:s}; u)$. The following plots $\mathrm{Cost}(y_{1:300}; u)$. Colors represent the most recent changepoint associated with optimal cost at each $u$.
To manually access the cost functions use fit$piecewise_square_losses
. Since $\mathrm{Cost}(y_{1:s}; u)$ is piecewise quadratic, we represent each component through its coefficients (square, linear, constant)
over domain (min_mean, max_mean)
. Furthermore, we store the most recent changepoint data_i
for each region (min_mean, max_mean)
.
For example, the plot above is created by filtering the dataframe to $s = 300$. We see that this cost function is defined over five regions with most recent changepoints at 200, 287, 294, 299
.
The conditioning set $\mathcal{S}$ can be extracted for fixed and adaptive $\nu$s by setting return_conditioning_sets = TRUE
. See Section 3 of our paper (Jewell, Fearnhead, & Witten, 2019) for additional details.
For example,
The conditioning set for each estimated changepoint can be accessed through
Each row is a subset of $\mathbb{R}$ defined as (min_mean, max_mean)
. This region is in $\mathcal{S}$ if contained = 1
.
There are simple plotting tools to visualize these sets:
In the case of inference with $\ell_0$ segmentation, it is also possible to view the cost of segmenting the data as a function of $\phi$, that is,