BOCD-NIG: Normal-Inverse-Gamma Model
Overview
The BOCD-NIG model is a conjugate Bayesian approach for detecting changepoints in univariate data with unknown mean and variance. It uses the Normal-Inverse-Gamma (NIG) distribution as the conjugate prior for efficient sequential inference.
When to Use BOCD-NIG
Best suited for:
- Univariate continuous data streams
- Data with unknown mean and variance
- Fast real-time processing requirements
- Applications requiring simple interpretability
- When data is approximately normally distributed
Advantages:
- Computationally efficient (conjugate updates)
- Closed-form posterior updates
- Low memory footprint
- Fast execution
Limitations:
- Assumes univariate normal data
- Less robust to outliers and multimodal distributions
- May underperform on non-Gaussian data
Parameters
Initialization
from pybocd import BOCDNIG
model = BOCDNIG(
m_0=0.0, # Prior mean
kappa_0=1.0, # Prior precision (inverse variance scale)
alpha_0=1.0, # Prior shape for inverse-gamma distribution
beta_0=1.0, # Prior rate for inverse-gamma distribution
l=200.0, # Expected run length (transition probability)
threshold=1e-4 # Pruning threshold for negligible weights
)
Parameter Descriptions
| Parameter | Description |
|---|---|
m_0 | Prior mean of the normal distribution |
kappa_0 | Prior precision scaling factor (higher = stronger prior belief) |
alpha_0 | Shape parameter of the inverse-gamma distribution for variance |
beta_0 | Rate parameter of the inverse-gamma distribution for variance |
l | Expected run length; probability of changepoint = 1/l at each time step |
threshold | Minimum weight threshold for maintaining run-length hypotheses |
Parameter Tuning Guidelines
m_0: Set based on prior knowledge of the data’s mean. If unknown, use 0 or the first few observations.kappa_0: Higher values indicate stronger confidence inm_0. Use smaller values for more flexibility.alpha_0andbeta_0:44 Set based on prior knowledge of variance. Inverse-gamma distribution’s mean isbeta_0 / (alpha_0 - 1)foralpha_0 > 1. Use small values for weak priors.l: Choose based on expected frequency of changepoints. Smaller values make the model more sensitive to changes.threshold: Adjust to balance computational efficiency and accuracy. Lower values retain more hypotheses but increase computation.
Usage Example
import numpy as np
from pybocd import BOCDNIG
# Generate synthetic data with a changepoint
np.random.seed(42)
data = np.concatenate(
[
np.random.normal(0, 1, 100), # Segment 1: mean=0, std=1
np.random.normal(3, 1, 100), # Segment 2: mean=3, std=1
]
)
# Initialize model
model = BOCDNIG(m_0=0.0, kappa_0=1.0, alpha_0=1.0, beta_0=1.0, l=200.0, threshold=1e-4)
# Process data
for t, x in enumerate(data):
model.add_data(x)
# Get the most probable run length (MAP estimate)
run_length_dist = model.run_length_dist
run_length = model.run_length
max_idx = np.argmax(run_length_dist)
max_path = run_length[max_idx]
print(f"Time {t}: MAP = {max_path}")
Tuning the Model
Expected Run Length (l)
A higher l means changepoints are less likely. Choose based on domain knowledge:
l = 50: Expect a changepoint every ~50 observationsl = 200: Expect a changepoint every ~200 observationsl = 1000: Expect a changepoint every ~1000 observations
Pruning Threshold
The threshold parameter removes run-length hypotheses with negligible posterior weight, reducing computation:
model = BOCDNIG(..., threshold=1e-2) # More aggressive pruning (faster)
model = BOCDNIG(..., threshold=1e-6) # Less pruning (more accurate)
Prior Parameters
For weak priors (less informative):
BOCDNIG(m_0=0.0, kappa_0=0.1, alpha_0=0.5, beta_0=0.5)
For strong priors (more informative):
BOCDNIG(m_0=5.0, kappa_0=10.0, alpha_0=10.0, beta_0=10.0)
References
For theoretical details, see the original BOCD paper and the pybocd documentation.