Exact segmentation: Pelt

Description

The method is implemented in ruptures.detection.Pelt.

Because the enumeration of all possible partitions impossible, the algorithm relies on a pruning rule. Many indexes are discarded, greatly reducing the computational cost while retaining the ability to find the optimal segmentation. The implementation follows [BKFE12]. In addition, under certain conditions on the change point repartition, the computational complexity is linear on average.

When calling ruptures.detection.Pelt.__init__(), the minimum distance between change points can be set through the keyword 'min_size'; through the parameter 'jump', only change point indexes multiple of a particular value are considered.

Usage

import numpy as np
import matplotlib.pylab as plt
import ruptures as rpt

# creation of data
n, dim = 500, 3
n_bkps, sigma = 3, 1
signal, b = rpt.pw_constant(n, dim, n_bkps, noise_std=sigma)

# change point detection
model = "l1"  # "l2", "rbf"
algo = rpt.Pelt(model=model, min_size=3, jump=5).fit(signal)
my_bkps = algo.predict(pen=3)

# show results
fig, (ax,) = rpt.display(signal, bkps, my_bkps, figsize=(10, 6))
plt.show()

Code explanation

class ruptures.detection.Pelt(model='l2', custom_cost=None, min_size=2, jump=5, params=None)[source]

Penalized change point detection.

For a given model and penalty level, computes the segmentation which minimizes the constrained sum of approximation errors.

__init__(model='l2', custom_cost=None, min_size=2, jump=5, params=None)[source]

Initialize a Pelt instance.

Parameters
  • model (str, optional) – segment model, [“l1”, “l2”, “rbf”]. Not used if 'custom_cost' is not None.

  • custom_cost (BaseCost, optional) – custom cost function. Defaults to None.

  • min_size (int, optional) – minimum segment length.

  • jump (int, optional) – subsample (one every jump points).

  • params (dict, optional) – a dictionary of parameters for the cost instance.

Returns

self

fit(signal)[source]

Set params.

Parameters

signal (array) – signal to segment. Shape (n_samples, n_features) or (n_samples,).

Returns

self

fit_predict(signal, pen)[source]

Fit to the signal and return the optimal breakpoints.

Helper method to call fit and predict once

Parameters
  • signal (array) – signal. Shape (n_samples, n_features) or (n_samples,).

  • pen (float) – penalty value (>0)

Returns

sorted list of breakpoints

Return type

list

predict(pen)[source]

Return the optimal breakpoints.

Must be called after the fit method. The breakpoints are associated with the signal passed to fit().

Parameters

pen (float) – penalty value (>0)

Returns

sorted list of breakpoints

Return type

list

References

BKFE12

R. Killick, P. Fearnhead, and I. Eckley. Optimal detection of changepoints with a linear computational cost. Journal of the American Statistical Association, 107(500):1590–1598, 2012.