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.
Parameters#
Name |
Type |
Default |
Description |
|---|---|---|---|
|
int / None |
|
Number of change points (guided mode). Takes precedence over
|
|
float / None |
|
Threshold on the gain. |
|
str |
|
|
|
float |
\(2^{-1/2}\) |
Decay of the seeded interval lengths, in \([1/2, 1)\); closer to 1: more intervals, slower and more accurate. |
|
int |
|
Number of random intervals ( |
|
str |
|
|
|
bool |
|
With |
|
float |
|
Exponent of the sSIC penalty \(k\,(d+1)/2\,\log(n)^\alpha\). |
|
int |
|
Largest number of change points the sSIC considers (the criterion
needs a bound); a series with more gets |
|
str |
|
Ruptures cost ( |
|
int |
|
Minimum number of points on each side of a split; shorter intervals
are dropped ( |
|
dict / None |
|
Keyword arguments of the ruptures cost. |
|
str |
|
Backend of the best split, as for BinSeg. |
|
int / None |
|
Seed of the random intervals. With |
|
int |
|
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:
BaseSegmenterSeeded 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 firstn_cpsof the greedy path, or, withselection="narrowest", the first solution of at leastn_cpschange points met while the threshold decreases, cut to then_cpslargest gains (the papers have no guided mode: this cut is a choice of this implementation). Takes precedence overpenalty.penalty (
float|None) – Threshold on the gain: a change point is kept when the gain of the interval that selects it exceedspenalty(for"l2"and noise of variancesigma^2,(C sigma)^2 2 log nis the thresholdC sigma sqrt(2 log n)of Fryzlewicz, 2014, Sec. 4, on the CUSUM).None(withn_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) orn_intervalsrandom ones (WBS).decay (
float) – Decayain[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 ofintervals="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 than2 * min_sizeare 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) – Withselection="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 penaltyk (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 (Kof 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 thanmax_cpsgetsmax_cpsat most. NOT evaluates every solution of its path with at mostq_max = 25change points (Baranowski et al., 2019, Sec. 3.6.2); here the path ofselection="narrowest", whose solutions do not grow monotonically, stops at the first one with more thanmax_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 than2 * min_sizehave no admissible split and are dropped.1is them = 2of the experiments of Kovács et al. (2023). Above 1, the seeded intervals of the layers whose length is close to2 * min_sizeare 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 forBinSegDetector. Same result.random_state (
int|None) – Seed of the random intervals (intervals="wild"only). Withintervals="wild"and no seed, the estimator carries thenon_deterministictag.axis (
int) – Time axis.
Notes
Indices follow tsseg: a change point is the first point of a new segment, the
sof the papers’ split(l, s] | (s, r]. Withmin_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"withselection="greedy"is WBS with the sSIC, the whole series being always a candidate interval;augment=Trueadds 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)^alphaper parameter, halved here) to Gaussian channels with their own variances, a change point counting its location anddmeans; it reduces to the univariate criterion ford = 1. This multivariate model is not in the papers. So that the gain weighs the channels as this criterion does, withmodel="l2"and the sSIC orn_cpseach channel of a multivariate series is first divided by its noise levelmedian(|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 ofdiff(x)oversqrt(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. Withpenalty, 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 inO(1)per point for"l2"; the greedy selection then takesO(n log n). The narrowest selection with the sSIC recomputes its solution whenever a lower threshold changes it, up tomax_cpschange 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
fitmethod.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(seesklearn.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 tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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.
- set_predict_request(*, axis='$UNCHANGED$')#
Configure whether metadata should be requested to be passed to the
predictmethod.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(seesklearn.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 topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.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.