BinSeg#

Binary Segmentation (BinSeg) – fast sequential change point detection.

Description#

Binary segmentation is a greedy, top-down procedure: it first detects a single change point in the full signal, then splits the series at that point and repeats the operation on each sub-signal until a stopping criterion is met.

The benefits include low complexity – \(O(C\,n\log n)\) where n is the number of samples and C the cost of evaluating the cost function on one sub-signal – and the ability to work whether the number of regimes is known beforehand or not.

Type: change point detection
Supervision: unsupervised or semi-supervised
Scope: univariate and multivariate
Complexity: \(O(C\,n\log n)\)

Parameters#

Name

Type

Default

Description

n_cps

int / None

None

Number of change points. When None, uses penalty or epsilon.

model

str

"l2"

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

min_size

int

2

Minimum segment length.

jump

int

5

Sub-sampling factor for candidate breakpoints.

penalty

float / None

None

Penalty value (BIC-style). Mutually exclusive with n_cps / epsilon. None means 10 when neither n_cps nor epsilon is given.

pen_scale

str / None

None

"bic": penalty is a coefficient on \(\log(n)\,u\), u the cost of one sample of the whole signal: d for "l2" on unit-variance channels (the BIC penalty of one more segment), the mean absolute deviation for "l1", 1 for the kernel costs, \(d(d+3)/2\) for "normal" (its BIC parameter count). One value then applies across lengths, dimensions and costs.

epsilon

float / None

None

Reconstruction-error tolerance. Mutually exclusive with n_cps / penalty.

custom_cost

BaseCost / None

None

Pre-instantiated ruptures cost object.

cost_params

dict / None

None

Additional keyword arguments forwarded to the cost factory.

backend

str

"auto"

Backend of the "l1", "l2", "rbf" and "cosine" costs: "auto" uses numba when it is installed (the kernel costs then in O(n) memory, without the \(n \times n\) Gram matrix); "numba" and "python" force either. Same segmentation on both paths.

axis

int

0

Axis representing time.

Usage#

from tsseg.algorithms import BinSegDetector

detector = BinSegDetector(model="l2", n_cps=3)
change_points = detector.fit_predict(X)

# Unknown number of change points – use a penalty
detector = BinSegDetector(model="l2", penalty=10)
change_points = detector.fit_predict(X)

Implementation: Vendored from ruptures v1.1.8. BSD 2-Clause. With the "l1", "l2", "rbf" or "cosine" cost and numba installed, the best split of a segment comes from one sweep from each end, in \(O(n)\) ("l2"), \(O(n \log n)\) ("l1") or \(O(n^2)\) (kernels) instead of two cost evaluations per candidate, and the kernel costs without the \(n \times n\) Gram matrix. Gains closer than \(10^{-9}\) of the segments’ costs are tied: the last change point of a segment and the first segment win, on both backends.

Reference: Bai (1997), Econometric Theory; Fryzlewicz (2014), The Annals of Statistics.

API reference#

BinSeg detector aligned with the tsseg BaseSegmenter API.

class BinSegDetector(*, n_cps=None, model='l2', min_size=2, jump=5, penalty=None, pen_scale=None, epsilon=None, custom_cost=None, cost_params=None, backend='auto', axis=0)[source]#

Bases: BaseSegmenter

Binary Segmentation change-point detector using ruptures’ implementation.

This wrapper fits the single-change Binseg solver on the provided data and repeatedly splits the series to obtain multiple change points.

Parameters:
  • n_cps (int | None) – Number of change points to return. When given, penalty and epsilon are ignored, with a UserWarning for each one that is also given. When None, splitting stops on penalty or epsilon.

  • model (str) – Cost model passed to ruptures. Examples include “l2”, “l1”, “rbf”.

  • min_size (int) – Minimum segment length enforced during the binary search.

  • jump (int) – Sub-sampling factor for candidate breakpoints.

  • penalty (float | None) – Penalty per change point, used when n_cps is None: splitting continues while the best split lowers the cost by more than penalty. Must be strictly positive. None means 10 when neither n_cps nor epsilon is given, and no penalty otherwise. At most one of penalty and epsilon may be set.

  • pen_scale (str | None) – "bic": penalty is a coefficient on log(n) * u, u the cost of one sample of the whole signal (d for "l2" on unit-variance channels, where it is the BIC penalty; 1 for the kernel costs; d(d+3)/2, the BIC parameter count, for "normal").

  • epsilon (float | None) – Reconstruction budget, used when n_cps and penalty are None: splitting continues while the total cost of the segmentation exceeds epsilon. Must be strictly positive.

  • custom_cost (BaseCost | None) – Pre-instantiated ruptures cost object.

  • cost_params (dict | None) – Additional keyword arguments forwarded to cost_factory when building the cost from model.

  • backend (str) – Backend of the costs l1, l2, rbf and cosine: numba when it is installed under "auto", the kernel costs then without the n x n Gram matrix. Same change points either way.

  • axis (int) – Axis representing time in the input array.

Raises:

ValueError – If both penalty and epsilon are set without n_cps, or if penalty or epsilon is not strictly positive.

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:

BinSegDetector

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:

BinSegDetector