DynP#

Dynamic Programming (DynP) – exact optimal segmentation.

Description#

Dynamic programming finds the exact minimum of the sum of segment costs by enumerating all admissible partitions. The user must specify the number of change points in advance (consider PELT or BinSeg when this is unknown).

Complexity is \(O(C\,K\,n^{2})\) where K is the number of change points, n the sample count and C the cost-function complexity. To reduce runtime, increase min_size and jump:

  • min_size controls the minimum distance between change points.

  • jump controls the grid of admissible positions (only multiples of jump).

Type: change point detection
Supervision: semi-supervised (n_cps required)
Scope: univariate and multivariate
Complexity: \(O(C\,K\,n^{2})\)

Parameters#

Name

Type

Default

Description

n_cps

int

1

Number of change points to detect (>= 0).

model

str

"l2"

Ruptures cost model.

min_size

int

2

Minimum segment length.

jump

int

5

Grid step for candidate positions.

cost_params

dict / None

None

Extra arguments for the cost function.

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 DynpDetector

detector = DynpDetector(n_cps=3, model="l2")
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 dynamic programme runs in numba in \(O(K n)\) memory, where the Python path memoises one sub-problem per pair of grid points, \(O((n / \text{jump})^2)\) whatever the cost. For the kernel costs (kernel change point detection, ruptures’ KernelCPD) it transcribes ruptures’ C code, which updates the kernel sums sample by sample instead of storing the \(n \times n\) Gram matrix. Candidates closer than \(10^{-9}\) of the costs involved are tied, and the earliest change point wins, on both backends.

Reference: Auger & Lawrence (1989), Bulletin of Mathematical Biology.

API reference#

Dynamic programming detector built on the vendored ruptures implementation.

class DynpDetector(*, n_cps=1, model='l2', min_size=2, jump=5, cost_params=None, semi_supervised=True, backend='auto', axis=0)[source]#

Bases: BaseSegmenter

Exact dynamic programming change point detector (vendored ruptures Dynp).

Returns the n_cps change points that minimise the sum of the segment costs, over the candidates allowed by min_size and jump. The number of change points must be known: the detector is semi-supervised only.

Parameters:
  • n_cps (int) – Number of change points to return. 0 returns no change point, and so does a series shorter than min_size * (n_cps + 1); None raises a ValueError.

  • 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 considered.

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

  • semi_supervised (bool) – Stored but not used: it has no effect on the result.

  • 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.

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:

DynpDetector

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:

DynpDetector