BOCD#

Offline Bayesian Gaussian segmentation with joint MAP decoding.

Description#

BOCDDetector fits the Gaussian product-partition model of Fearnhead (2006). A constant hazard \(h=1/\lambda\) gives segment duration mass \(g(l)=h(1-h)^{l-1}\); the final segment contributes its right-censored survival probability. The default returns the single most probable partition (MAP), using the recursion of Fearnhead and Liu (2007). The whole series is used. The historical name does not imply the causal online filter of Adams and MacKay (2007).

The default multivariate_strategy="native" integrates out the mean vector and full covariance under a Normal-inverse-Wishart prior. It can distinguish changes of direction or covariance that a norm reduction loses. For \(d\) channels, the prior parameters are

\[\mu_0 = \mu\mathbf{1}, \qquad \kappa_0=\kappa, \qquad \nu_0 = 2\alpha+d-1, \qquad \Psi_0 = 2\beta I_d.\]

Each marginal variance has an inverse-gamma prior with shape alpha and scale beta. For one channel this is exactly the Normal-Gamma model. The alternatives "l2" and "ensembling" respectively analyse the norm or fit each channel separately and merge the boundaries. Ensembling does not model cross-channel covariance; MAP then applies to each channel separately.

Type: change point detection
Supervision: unsupervised or an upper bound on the number of changes
Scope: univariate and multivariate

Defaults and decision rule#

The defaults use per-channel standardisation, hazard_lambda=1000, alpha=kappa=1, beta=10, a minimum segment length of 25, and joint MAP inference on at most 2,000 interior candidate positions. These values were selected on 15 series from five benchmark datasets, with basic mean, variance, covariance and stationary-noise checks. They are a practical starting point, not a guarantee of suitability for every time scale or observation model.

standardize=True centers and scales the series supplied to predict; prior parameters are in these standardised units. In L2 mode, the norm is computed first and its scalar series is then standardised, so that centering channels does not itself erase a magnitude change. To supply priors in physical units, set standardize=False.

max_cps=K constrains MAP inference to partitions with at most K changes. It optimises their locations jointly; it does not trim a previously fitted partition and does not force exactly K changes. max_cps=0 skips inference. min_distance constrains segment lengths during inference, including both edge segments. Ensembling also spaces its final aggregated boundaries.

For marginal rather than MAP decoding, set decoding="marginal". cp_prob_threshold=0.5 then thresholds peaks of the boundary probabilities, with spacing and an optional count cap. Boundary probabilities are distinct events: their sum is the posterior expected number of changes, not one. The probability threshold and truncate do not affect MAP decoding. truncate=None sums every message term. The optional finite legacy cutoff can discard important later terms; it does not accelerate likelihood evaluation.

Candidate grid and cost#

max_candidates=2000 restricts the possible boundary locations on a regular grid, always including 0 and n as segment endpoints. Every observation still contributes to segment means and covariance matrices. This is a restriction on boundary locations, not downsampling or replacing blocks by their means. Both MAP and marginal inference are conditional on this candidate set and the minimum segment length. Set max_candidates=None to allow every interior position. The geometric prior retains the original sample-time units on this grid; changing the grid changes the conditioned model.

Let \(m\) be the number of candidate vertices, including the two endpoints. For native multivariate inference, worst-case time is \(O(nd^2+m^2d^3)\), with \(O(n+md^2)\) working memory beyond the input. For one channel it is \(O(n+m^2)\) time and \(O(n+m)\) memory. An active MAP count constraint K adds \(O(m^2K)\) time and \(O(mK)\) memory; unconstrained MAP needs no table indexed by the number of changes. Ensembling runs the univariate calculation per channel and stores their candidates.

Block moments are combined using Welford’s recurrence. A determinant lemma keeps large mean offsets out of covariance matrices; Cholesky evaluation falls back to an eigenvalue calculation if a tiny prior ridge is not numerically resolved. No n-by-n segment or ordinal-change matrix is stored.

Usage#

from tsseg.algorithms import BOCDDetector

change_points = BOCDDetector().fit_predict(X)
at_most_three = BOCDDetector(max_cps=3).fit_predict(X)
probabilities_as_peaks = BOCDDetector(
    decoding="marginal", cp_prob_threshold=0.5
).fit_predict(X)

Suitability and earlier results#

The model assumes independent Gaussian observations within a segment. Full covariance detects more multivariate distribution changes than L2 reduction, but does not describe arbitrary temporal dynamics. Oscillations, drift or outliers can still produce extra boundaries; short weak changes can be missed. hazard_lambda and min_distance are in samples and depend on the sampling rate. Check both boundary precision and segmentation quality for the application.

Versions before the inference correction passed a constant duration mass, normalised boundary scores to sum to one, and used cubic-time inference with quadratic memory. The corrected inference first retained the old defaults. The new native likelihood, MAP decision rule, standardisation, candidate grid and calibrated defaults change the outputs again. Earlier BOCD benchmark results must be recomputed for this configuration.

When migrating an experiment configuration, explicit old values still override the constructor defaults. Set hazard_lambda=1000 and beta=10 (or remove their overrides) to use the calibrated prior. In MAP mode, remove probability threshold and truncation axes from tuning grids: they are inactive; vary the duration and Gaussian prior parameters instead. For example:

instance:
  _target_: tsseg.algorithms.bocd.detector.BOCDDetector
  multivariate_strategy: native
  decoding: map
  hazard_lambda: 1000
  beta: 10
  standardize: true
  max_candidates: 2000
  truncate: null

Implementation: the detector uses _offline.py. Legacy generic helpers in bayesian_models.py, originally adapted from hildensia under MIT, remain available for compatibility and are not called by the detector.

References: Fearnhead (2006); Fearnhead and Liu (2007), Section 3.1; Murphy (2007), conjugate Gaussian analysis.

API reference#

class BOCDDetector(*, hazard_lambda=1000, mu=0.0, kappa=1.0, alpha=1.0, beta=10.0, truncate=None, cp_prob_threshold=0.5, min_distance=25, max_cps=None, multivariate_strategy='native', tolerance=0.01, decoding='map', max_candidates=2000, standardize=True, axis=0)[source]#

Bases: BaseSegmenter

Bayesian change-point detector based on offline inference.

Segment parameters are integrated out under a conjugate prior. Native multivariate inference includes the full covariance matrix. The default returns the joint maximum a posteriori partition; thresholded marginal boundary probabilities remain available.

Parameters:
  • hazard_lambda (float) – Expected run length before conditioning on the candidate grid and minimum segment length. The unrestricted sample-time hazard is 1 / hazard_lambda.

  • mu (float) – Prior mean of the segment observations.

  • kappa (float) – Strength of the prior mean (normal precision).

  • alpha (float) – Prior shape of the inverse-gamma distribution over the variance.

  • beta (float) – Prior scale of the inverse-gamma distribution over the variance.

  • truncate (int | None) – Optional legacy natural-log cutoff in marginal decoding. None sums every term on the candidate grid. A finite cutoff can discard important terms and does not accelerate likelihood evaluation. Not used by MAP decoding.

  • cp_prob_threshold (float) – Minimum probability of a peak in marginal decoding. Not used by MAP.

  • min_distance (int) – Minimum segment length, including the first and last segments.

  • max_cps (int | None) – Optional upper bound on the number of changes. MAP jointly optimises over partitions with at most this many boundaries. Marginal decoding caps the selected peaks. 0 returns none.

  • multivariate_strategy (str) –

    Strategy for handling multivariate data:

    • "native" – integrate the mean vector and full covariance.

    • "l2" – reduce to univariate via L2 norm across channels.

    • "ensembling" – run BOCD independently on each channel and aggregate the detected change points.

  • tolerance (int | float) – Tolerance for aggregating change points in the ensembling strategy. If a float in (0, 1) it is interpreted as a fraction of the signal length.

  • axis (int) – Time axis in the input array.

  • decoding (str) – Joint MAP partition or thresholded marginal peaks.

  • max_candidates (int | None) – Maximum interior boundary candidates on a regular grid. All observations contribute to the likelihood. None permits every interior position. Inference is conditional on the candidate grid.

  • standardize (bool) – Center and scale each channel on the whole series. Prior parameters are then in standardised units. Disable to specify priors in the original measurement units.

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:

BOCDDetector

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:

BOCDDetector

offline_changepoint_detection(data, prior_function, log_likelihood_class, truncate=-40)[source]#

Compute the likelihood of changepoints on data.

Parameters: data – the time series data truncate – the cutoff probability 10^truncate to stop computation for that changepoint log likelihood

Outputs:

P – the log-likelihood of a datasequence [t, s], given there is no changepoint between t and s Q – the log-likelihood of data Pcp – the log-likelihood that the i-th changepoint is at time step t. To actually get the probility of a changepoint at time step t sum the probabilities.

online_changepoint_detection(data, hazard_function, log_likelihood_class)[source]#

Use online bayesian changepoint detection https://scientya.com/bayesian-online-change-point-detection-an-intuitive-understanding-b2d2b9dc165b

Parameters: data – the time series data

Outputs:

R – is the probability at time step t that the last sequence is already s time steps long maxes – the argmax on column axis of matrix R (growth probability value) for each time step