.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/04_modern_segmentation/plot_01_bocpd.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_04_modern_segmentation_plot_01_bocpd.py: Bayesian on-line change point detection ======================================= Adams and MacKay (2007) maintain the posterior distribution of the *run length* -- the time since the last change -- and update it with every new sample using the predictive distribution of a conjugate model. Here a Normal--inverse-Wishart model detects changes of the mean and the covariance of a three-variable series. .. GENERATED FROM PYTHON SOURCE LINES 11-27 .. code-block:: Python import matplotlib.pyplot as plt import mvtsa as mv from mvtsa import segmentation as sg from mvtsa.evaluation import evaluate_segmentation X, cps, _ = mv.datasets.make_piecewise_stationary( n_segments=6, length=120, n_variables=3, change="all", vary=0.3, random_state=8, ) .. GENERATED FROM PYTHON SOURCE LINES 28-30 Run-length posterior and change probability ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-45 .. code-block:: Python bo = sg.BOCPD(hazard=150, model="full") seg = bo.segment(X) print("detected:", seg.change_points, "\ntrue: ", cps) print(evaluate_segmentation(cps, seg, len(X), margin=10)["f1"]) fig, axes = plt.subplots(3, 1, figsize=(7, 6), sharex=True) mv.viz.plot_segmentation(X, seg, truth=cps, ax=axes[0]) mv.viz.plot_run_length(bo, ax=axes[1]) mv.viz.plot_profile( bo.change_probability_, seg.change_points, ax=axes[2], label="P(change)", threshold=bo.threshold, ) .. image-sg:: /auto_examples/04_modern_segmentation/images/sphx_glr_plot_01_bocpd_001.png :alt: plot 01 bocpd :srcset: /auto_examples/04_modern_segmentation/images/sphx_glr_plot_01_bocpd_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none detected: [101 246 346 478 615] true: [101 246 346 478 615] 1.0 .. GENERATED FROM PYTHON SOURCE LINES 46-50 Parameter study: the hazard --------------------------- The hazard is the prior expected segment length. The detector is robust to it over an order of magnitude. .. GENERATED FROM PYTHON SOURCE LINES 50-57 .. code-block:: Python for h in (30, 100, 300, 1000): s = sg.BOCPD(hazard=h).segment(X) print( f"hazard {h:5d}: {len(s.change_points)} change points, " f"F1 {evaluate_segmentation(cps, s, len(X), 10)['f1']:.2f}" ) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none hazard 30: 4 change points, F1 0.89 hazard 100: 4 change points, F1 0.89 hazard 300: 5 change points, F1 1.00 hazard 1000: 5 change points, F1 1.00 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.819 seconds) .. _sphx_glr_download_auto_examples_04_modern_segmentation_plot_01_bocpd.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/mvtsa/main?urlpath=lab/tree/notebooks/auto_examples/04_modern_segmentation/plot_01_bocpd.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_bocpd.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_bocpd.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_bocpd.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_