pybocd: Bayesian Online Changepoint Detection in Python

pybocd is a Python library implementing Bayesian Online Changepoint Detection (BOCD) algorithms with multiple observation models for detecting change points in streaming data.

Overview

Changepoint detection is essential for detecting sudden shifts or anomalies in time series data. The pybocd library provides robust, efficient implementations of BOCD with two built-in observation models:

BOCD-NIG

Normal–Inverse–Gamma (NIG) Model — A conjugate Bayesian model for detecting changepoints in univariate data with unknown mean and variance.

  • Efficient conjugate prior-posterior updates
  • Suitable for continuous-valued streaming data
  • Real-time run-length distribution estimation

BOCD-GMM

Gaussian Mixture Model (GMM) — A particle-based model for multimodal data and robust outlier handling.

  • Handles multimodal and non-Gaussian distributions
  • Robust to outliers through mixture components
  • Particle filtering for sequential inference

Quick Start

Installation

pip install pybocd

Basic Usage — NIG Model

from pybocd import BOCDNIG

# Initialize model with prior parameters
model = BOCDNIG(
    m_0=0.0,      # prior mean
    kappa_0=1.0,  # prior precision
    alpha_0=1.0,  # shape parameter
    beta_0=1.0,   # rate parameter
    l=200.0,      # expected run length
    threshold=1e-4  # pruning threshold
)

# Process streaming data
for x in data_stream:
    model.add_data(x)

Basic Usage — GMM Model

from pybocd import BOCDGMM

# Initialize with hyperparameters
model = BOCDGMM(
    alpha_0=2.0, beta_0=2.0,
    m_0=0.0, kappa_0=1.0,
    alpha_p_0=2.0, beta_p_0=2.0,
    mu_p_0=0.0, sigma_p_sq_0=1.0,
    jitter_mu=0.01, jitter_sigma_sq=0.01,
    jitter_tau_sq=0.01, jitter_pi=0.01,
    l=200.0, m=20, n=200, init_particle_n=50
)

for x in data_stream:
    model.add_data(x)