Seeded Binary Segmentation#

Seeded binary segmentation (SeedBS) and wild binary segmentation (WBS).

Description#

Binary segmentation searches its best split on the whole series, then on each half: a change point lying between two others of opposite sign can stay hidden. SeedBS computes the best split of every interval of a fixed, deterministic collection, the seeded intervals: layers of evenly spread, overlapping intervals whose length decays by a factor decay from one layer to the next, \(O(n \log n)\) points in all. A selection rule then keeps some of these candidates:

  • "greedy": the largest gain first; every interval containing it is dropped, and so on (as in WBS);

  • "narrowest": among the intervals whose gain exceeds the threshold, the narrowest first (narrowest over threshold), which the consistency theory of the paper covers.

The number of change points comes from n_cps, from a threshold penalty on the gain, or, by default, from the strengthened Schwarz information criterion (sSIC) along the solution path, as in the experiments of the paper. intervals="wild" draws n_intervals random intervals instead (WBS; the whole series is always a candidate). With the greedy selection, augment=True also examines in its entirety each segment between two selected change points, the optional augmentation of WBS.

The gain of a split is the decrease of a ruptures cost: for "l2", the squared CUSUM statistic of the papers, summed over channels.

Type: change point detection
Supervision: unsupervised or semi-supervised
Scope: univariate and multivariate
Complexity: \(O(n \log n)\) cost evaluations (seeded intervals)

Parameters#

Name

Type

Default

Description

n_cps

int / None

None

Number of change points (guided mode). Takes precedence over penalty.

penalty

float / None

None

Threshold on the gain. None: the sSIC picks the model (model="l2" only).

intervals

str

"seeded"

"seeded" (SeedBS) or "wild" (WBS, random intervals).

decay

float

\(2^{-1/2}\)

Decay of the seeded interval lengths, in \([1/2, 1)\); closer to 1: more intervals, slower and more accurate.

n_intervals

int

5000

Number of random intervals (intervals="wild").

selection

str

"greedy"

"greedy" or "narrowest".

augment

bool

False

With selection="greedy": also examine each segment between two selected change points in its entirety (optional augmentation of WBS, Fryzlewicz, 2014, Sec. 3.3); off in the greedy selection of the paper.

alpha

float

1.01

Exponent of the sSIC penalty \(k\,(d+1)/2\,\log(n)^\alpha\).

max_cps

int

50

Largest number of change points the sSIC considers (the criterion needs a bound); a series with more gets max_cps at most. With selection="narrowest", the solution path stops at its first solution with more change points (NOT evaluates all those with at most 25).

model

str

"l2"

Ruptures cost ("l2", "l1", "rbf", "linear", "normal", "cosine").

min_size

int

1

Minimum number of points on each side of a split; shorter intervals are dropped (1: the m = 2 of the paper).

cost_params

dict / None

None

Keyword arguments of the ruptures cost.

backend

str

"auto"

Backend of the best split, as for BinSeg.

random_state

int / None

None

Seed of the random intervals. With intervals="wild" and no seed, the estimator carries the non_deterministic tag.

axis

int

0

Axis representing time.

Usage#

from tsseg.algorithms import SeededBinSegDetector

# Unknown number of change points: sSIC
change_points = SeededBinSegDetector().fit_predict(X)

# Narrowest-over-threshold selection, known number of change points
detector = SeededBinSegDetector(selection="narrowest", n_cps=3)
change_points = detector.fit_predict(X)

# Wild binary segmentation
detector = SeededBinSegDetector(intervals="wild", random_state=0)
change_points = detector.fit_predict(X)

Implementation: written from the papers (the authors’ code has no license; the R packages wbs and not are GPL-2.0-only). The sSIC assumes Gaussian noise; for a multivariate series each channel has its own variance and each change point counts \(d + 1\) parameters, an extension that is not in the papers. So that the gain weighs the channels as the criterion does, with model="l2" and the sSIC or n_cps each channel of a multivariate series is first divided by its noise level, the median absolute deviation estimate \(\operatorname{median}|\Delta x| / (\sqrt{2}\,\Phi^{-1}(3/4))\) of the papers (the standard deviation of \(\Delta x\) over \(\sqrt{2}\) when more than half the differences are 0; a constant channel is left as it is): the result does not depend on the scale of each channel. A univariate series, or a threshold penalty (in the units of the series), uses the series as given. In guided mode with selection="narrowest", the first solution with at least n_cps change points is cut to the n_cps largest gains, a choice of this implementation (the papers have no guided mode).

References: Kovács, Bühlmann, Li and Munk (2023), Biometrika; Fryzlewicz (2014), The Annals of Statistics; Baranowski, Chen and Fryzlewicz (2019), Journal of the Royal Statistical Society B.

API reference#

Seeded binary segmentation (Kovács et al., 2023), and wild binary segmentation (Fryzlewicz, 2014) on the same machinery.

class SeededBinSegDetector(*, n_cps=None, penalty=None, intervals='seeded', decay=0.7071067811865476, n_intervals=5000, selection='greedy', augment=False, alpha=1.01, max_cps=50, model='l2', min_size=1, cost_params=None, backend='auto', random_state=None, axis=0)[source]#

Bases: BaseSegmenter

Seeded binary segmentation (SeedBS), or wild binary segmentation (WBS).

Binary segmentation searches its best split on the whole series, then on each half, so that a change point lying between two others of opposite sign can stay hidden. Seeded binary segmentation instead computes the best split of every interval of a fixed, deterministic collection (the seeded intervals: layers of evenly spread, overlapping intervals whose length decays geometrically, O(n log n) points in all), then selects change points among these candidates (Kovács et al., 2023). Wild binary segmentation does the same on random intervals (Fryzlewicz, 2014).

The gain of a split is the decrease of the cost when the interval is cut in two: for model="l2" the squared CUSUM statistic of the papers (summed over channels).

Parameters:
  • n_cps (int | None) – Number of change points (guided mode): the first n_cps of the greedy path, or, with selection="narrowest", the first solution of at least n_cps change points met while the threshold decreases, cut to the n_cps largest gains (the papers have no guided mode: this cut is a choice of this implementation). Takes precedence over penalty.

  • penalty (float | None) – Threshold on the gain: a change point is kept when the gain of the interval that selects it exceeds penalty (for "l2" and noise of variance sigma^2, (C sigma)^2 2 log n is the threshold C sigma sqrt(2 log n) of Fryzlewicz, 2014, Sec. 4, on the CUSUM). None (with n_cps=None): the strengthened Schwarz information criterion picks the model, as in the experiments of Kovács et al. (2023); model="l2" only.

  • intervals (str) – Seeded intervals (SeedBS) or n_intervals random ones (WBS).

  • decay (float) – Decay a in [1/2, 1) of the seeded interval lengths from one layer to the next; the value recommended by Kovács et al. (2023). Closer to 1: more intervals, slower and more accurate.

  • n_intervals (int) – Number of random intervals of intervals="wild", the value of Fryzlewicz (2014, Sec. 4.1) and of the experiments of Kovács et al. (2023); Baranowski et al. (2019, Sec. 3.6.1) recommend 10 000 for NOT. Both ends of each interval are drawn uniformly from the series, as in WBS (Fryzlewicz, 2014, Sec. 3.3); draws shorter than 2 * min_size are dropped, not redrawn, so slightly fewer intervals are kept (NOT, Sec. 2.2, draws among the long enough ones only).

  • selection (str) – "greedy": the largest gain first, then all intervals containing it are dropped, and so on (as in WBS). "narrowest": among the intervals over the threshold, the narrowest first (narrowest over threshold, Baranowski et al., 2019), which the consistency theory of Kovács et al. (2023, Theorem 3) covers.

  • augment (bool) – With selection="greedy" only: also examine in its entirety each segment between two selected change points, its best split competing with the intervals it contains (the optional augmentation of WBS, Fryzlewicz, 2014, Sec. 3.3). A change point found this way can have a larger gain than the one above it in the recursion, so each change point is ranked by the smallest gain along its recursion, the threshold at which WBS stops before reaching it. Off by default, as in the greedy selection of Kovács et al. (2023, Sec. 2.4); costs one more best split of each half at each selected change point.

  • alpha (float) – Exponent of the sSIC penalty k (d + 1)/2 log(n)^alpha; 1 gives the Schwarz criterion. 1.01 is the value of Fryzlewicz (2014).

  • max_cps (int) – Largest number of change points the sSIC considers (K of Fryzlewicz, 2014, Sec. 4.2, which the criterion needs: along the whole path the residual variance tends to 0 and the largest model wins). The solution path is followed until it holds more; a series with more change points than max_cps gets max_cps at most. NOT evaluates every solution of its path with at most q_max = 25 change points (Baranowski et al., 2019, Sec. 3.6.2); here the path of selection="narrowest", whose solutions do not grow monotonically, stops at the first one with more than max_cps, and smaller solutions further down the path are not evaluated.

  • model (str) – Cost of the vendored ruptures ("l2", "l1", "rbf", "linear", "normal", "cosine").

  • min_size (int) – Minimum number of points on each side of a split; intervals shorter than 2 * min_size have no admissible split and are dropped. 1 is the m = 2 of the experiments of Kovács et al. (2023). Above 1, the seeded intervals of the layers whose length is close to 2 * min_size are kept when rounding makes them long enough; Kovács et al. (2023, Sec. 2.3) suggest instead stopping at an earlier layer when a minimal segment length is wanted.

  • cost_params (dict | None) – Keyword arguments of the ruptures cost.

  • backend (str) – Backend of the best split of the "l1", "l2", "rbf" and "cosine" costs, as for BinSegDetector. Same result.

  • random_state (int | None) – Seed of the random intervals (intervals="wild" only). With intervals="wild" and no seed, the estimator carries the non_deterministic tag.

  • axis (int) – Time axis.

Notes

Indices follow tsseg: a change point is the first point of a new segment, the s of the papers’ split (l, s] | (s, r]. With min_size=1, decay=2 ** -0.5, selection="greedy" or "narrowest" and the sSIC, this is g-SeedBS or n-SeedBS of Kovács et al. (2023, Table 1). intervals="wild" with selection="greedy" is WBS with the sSIC, the whole series being always a candidate interval; augment=True adds the optional augmentation of Fryzlewicz (2014, Sec. 3.3) at every later step.

Section, theorem and table numbers of Kovács et al. (2023) are those of arXiv:2002.06633v1, and those of Baranowski et al. (2019) those of its arXiv version.

The sSIC (Fryzlewicz, 2014, Sec. 3.4, eq. (4)) assumes Gaussian noise of constant variance. For a multivariate series this implementation applies the general form of Baranowski et al. (2019, eq. (3.1): minus twice the log-likelihood plus log(n)^alpha per parameter, halved here) to Gaussian channels with their own variances, a change point counting its location and d means; it reduces to the univariate criterion for d = 1. This multivariate model is not in the papers. So that the gain weighs the channels as this criterion does, with model="l2" and the sSIC or n_cps each channel of a multivariate series is first divided by its noise level median(|diff(x)|) / (sqrt(2) * 0.6745), the estimate of Fryzlewicz (2014, Sec. 4.1) and Baranowski et al. (2019, Sec. 2.1); when more than half the differences of a channel are 0 (a noiseless or quantised channel), by the standard deviation of diff(x) over sqrt(2); a constant channel is left as it is. The result then does not depend on the scale of each channel. A univariate series is left as it is, the sSIC and the order of the gains not depending on its scale. With penalty, the gain is that of the series as given, the threshold being in its units.

Cost: the best split of every interval, O(n log n) points in all for the seeded intervals (O(n_intervals * n) for the random ones), each in O(1) per point for "l2"; the greedy selection then takes O(n log n). The narrowest selection with the sSIC recomputes its solution whenever a lower threshold changes it, up to max_cps change points: quadratic in the number of intervals in the worst case.

References

Examples

>>> import numpy as np
>>> from tsseg.algorithms import SeededBinSegDetector
>>> rng = np.random.default_rng(0)
>>> X = np.concatenate([np.zeros(100), 3 * np.ones(100), np.zeros(100)])
>>> X = X + rng.normal(size=300)
>>> SeededBinSegDetector().fit_predict(X)
array([100, 200])
set_fit_request(*, axis='$UNCHANGED$')#

Configure whether metadata should be requested to be passed to the fit method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:

axis (Union[bool, None, str]) – Metadata routing for axis parameter in fit.

Returns:

self – The updated object.

Return type:

SeededBinSegDetector

set_predict_request(*, axis='$UNCHANGED$')#

Configure whether metadata should be requested to be passed to the predict method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to predict.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:

axis (Union[bool, None, str]) – Metadata routing for axis parameter in predict.

Returns:

self – The updated object.

Return type:

SeededBinSegDetector