fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Bayesian Change-Point Detection

Install and import#

bash
npm install fintech-algorithms
ts
import { runFilter } from "fintech-algorithms/statistical-time-series/state-and-regime-models/bayesian-change-point-detection";

Signature#

runFilter(observations, config)

Online detection of structural breaks by tracking the posterior over run length — how long since the last change. Unlike a regime model it does not need the number of regimes specified in advance.

Parameters#

NameTypeNotes
observationsnumber[]Observations in chronological order.
config{ hazard: number; observation_variance: number; prior_mean: number; prior_variance: number }hazard is the prior probability of a change at any step — its reciprocal is the expected run length, which is the more intuitive way to set it. The priors describe beliefs about a segment's mean before seeing data.

Returns#

{ …per-step run-length posterior }[]

The run-length distribution per observation. Being online, an apparent change point can be revised by later data — the posterior shows that, a hard list of breaks would not.

Errors#

  • When hazard falls outside 0…1, or a variance is not positive — throws

Complexity: time O(n²), space O(n).

Worked example#

verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

Input#

observations
[-0.3275053489, 0.2452664583, 0.224748597]
config
{
  "hazard": 0.025,
  "observation_variance": 0.36,
  "prior_mean": 0,
  "prior_variance": 4
}

Call#

runFilter(observations, config)

Returns#

object with 1 field: 2

{
  "2": {
    "index": 2,
    "change_probability": 0.00942784148480789,
    "map_run_length": 3,
    "expected_run_length": 2.9241612404581354,
    "active_hypotheses": 4,
    "map_mean": 0.04611964608414239,
    "log_predictive_density": -0.6857585614486436
  }
}

Diagrams#

Bayesian Change-Point Detection — diagnostic scorecard
Bayesian Change-Point Detection — failure boundary
Bayesian Change-Point Detection — family handoff
Bayesian Change-Point Detection — scenario comparison
Bayesian Change-Point Detection — state update
Bayesian Change-Point Detection — uncertainty ledger

Calculation flow#

Bayesian Change-Point Detection Causal Update Flow
flowchart LR
    A["Filtered state at t-1"] --> B["Predict state at t"]
    B --> C["Read observation available at t"]
    C --> D["Compute evidence or innovation"]
    D --> E["Normalize or gain-weight update"]
    E --> F["Filtered state at t"]
    F --> G["Publish diagnostics"]
    F --> A

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

The rest of the State and Regime Models family#