Window#

Window – sliding-window discrepancy change point detection.

Description#

Window-based detection slides two adjacent windows along the signal and computes a discrepancy measure \(d(\cdot,\cdot)\) derived from the cost function:

\[d(y_{u..v},\,y_{v..w}) = c(y_{u..w}) - c(y_{u..v}) - c(y_{v..w})\]

When both windows fall inside the same segment their statistical properties are similar and the discrepancy is low. When they straddle a change point the discrepancy spikes. A sequential peak search on the discrepancy curve produces the final change points.

Complexity is \(O(n\,w)\) where w is the window width – making Window one of the fastest methods. The jump parameter further speeds up prediction at the expense of positional precision.

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

Parameters#

Name

Type

Default

Description

width

int

100

Width of each sliding window (in samples).

n_cps

int / None

None

Number of change points.

pen

float / None

None

Penalty threshold.

pen_scale

str / None

None

"bic": pen 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.

model

str

"l2"

Ruptures cost model.

min_size

int

2

Minimum segment length.

jump

int

5

Sub-sampling factor for candidates.

cost_params

dict / None

None

Extra keyword arguments for 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

Time axis.

Usage#

from tsseg.algorithms import WindowDetector

detector = WindowDetector(width=50, model="l2", n_cps=3)
change_points = detector.fit_predict(X)

# Penalty-based stopping
import numpy as np
detector = WindowDetector(width=50, model="l2", pen=np.log(n) * d * sigma**2)
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 window scores and segment costs are computed in numba: the kernel costs without the \(n \times n\) Gram matrix, "l1" and "l2" bit for bit as numpy computes them. Scores closer than \(10^{-9}\) of the windows’ costs are tied: a peak must exceed its neighbours by more than that, and the latest of tied peaks is taken first, on both backends. Peaks are searched as upstream searches them (scipy.signal.argrelmax with mode="wrap"); until 0.2.0 the neighbourhood of a score was cut at the ends of the signal instead, which could add a change point next to the first or last window.

Reference: Basseville & Nikiforov (1993), Detection of Abrupt Changes, Prentice Hall.

API reference#

Window-based change point detector using the vendored ruptures implementation.

class WindowDetector(*, width=100, n_cps=None, pen=None, pen_scale=None, epsilon=None, model='l2', min_size=2, jump=5, cost_params=None, backend='auto', axis=0)[source]#

Bases: BaseSegmenter

Sliding-window change point detector using the vendored ruptures implementation.

A window of width samples slides over the series; at each candidate point, the score is the cost of the whole window minus the costs of its two halves. The local maxima of this score are then added as change points, highest score first, until the stopping criterion is met.

Parameters:
  • width (int) – Window length, rounded down to an even number. Candidates within width // 2 samples of either end of the series are not scored.

  • n_cps (int | None) – Number of change points to return; fewer are returned when the score has fewer local maxima. Takes precedence over pen and epsilon (with a UserWarning if either is also set).

  • pen (float | None) – Penalty per change point: a peak is added while it lowers the cost of the segmentation of the whole series by more than pen. Takes precedence over epsilon (with a UserWarning if both are set).

  • pen_scale (str | None) – None: pen is used as is. "bic": pen 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: peaks are added while the total cost of the segmentation exceeds epsilon.

  • model (str) – Cost model: least squares ("l2"), least absolute deviation ("l1"), Gaussian kernel ("rbf"), linear regression ("linear"), Gaussian likelihood ("normal") or cosine kernel ("cosine").

  • min_size (int) – Minimum segment length. Must not exceed half the series length.

  • jump (int) – Sub-sampling factor for candidate breakpoints: only multiples of jump are scored.

  • cost_params (dict | None) – Extra keyword arguments forwarded to the cost (for instance {"gamma": 0.1} for "rbf").

  • 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 of X that represents time.

Raises:

ValueError – If n_cps, pen and epsilon are all None, which is the default: one stopping criterion must be given.

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:

WindowDetector

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:

WindowDetector