.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/08_evaluation/plot_02_stability.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_08_evaluation_plot_02_stability.py: Is the segmentation stable? =========================== Without ground truth, a segmentation can still be validated: change points that persist when the data are perturbed -- noise added, a random block segmented -- are trustworthy; those that come and go are not. The boundary frequency over the perturbed runs measures the support of every change point. .. 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 segmentation_stability X, cps, _ = mv.datasets.make_piecewise_stationary( n_segments=5, length=150, n_variables=3, change="all", vary=0.3, random_state=3, ) .. GENERATED FROM PYTHON SOURCE LINES 28-30 Stable and unstable settings ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-64 .. code-block:: Python good = segmentation_stability( sg.BottomUp("normal", n_segments=5, initial_size=10), X, n_runs=20, random_state=0, ) over = segmentation_stability( sg.BottomUp("normal", n_segments=9, initial_size=10), X, n_runs=20, random_state=0, ) print( "5 segments: support", good["support"].round(2), "stability", round(good["stability"], 2), ) print( "9 segments: support", over["support"].round(2), "stability", round(over["stability"], 2), ) print( "change points with 9 segments:", over["reference"].change_points, "| true:", cps, ) fig, axes = plt.subplots(2, 1, figsize=(7, 4.4), sharex=True) mv.viz.plot_stability(good, ax=axes[0]) mv.viz.plot_stability(over, ax=axes[1]) .. image-sg:: /auto_examples/08_evaluation/images/sphx_glr_plot_02_stability_001.png :alt: stability = 0.86, stability = 0.89 :srcset: /auto_examples/08_evaluation/images/sphx_glr_plot_02_stability_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 5 segments: support [0.85 0.92 0.83 0.83] stability 0.86 9 segments: support [0.92 0.85 0.92 0.83 0.92 0.86 0.9 0.92] stability 0.89 change points with 9 segments: [120 130 260 270 450 460 620 630] | true: [123 261 454 626] .. GENERATED FROM PYTHON SOURCE LINES 65-72 Parameter study: the number of segments --------------------------------------- With too few segments the boundaries wander between runs; once every true change is found, the support saturates. The extra change points of the over-segmentation are stable as well, because they sit right next to the true ones (one initial segment away), so stability complements the rules for the number of segments rather than replacing them. .. GENERATED FROM PYTHON SOURCE LINES 72-81 .. code-block:: Python for k in range(3, 10): r = segmentation_stability( sg.BottomUp("normal", n_segments=k, initial_size=10), X, n_runs=15, random_state=0, ) print(f"{k} segments: stability {r['stability']:.2f}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none 3 segments: stability 0.46 4 segments: stability 0.57 5 segments: stability 0.83 6 segments: stability 0.82 7 segments: stability 0.83 8 segments: stability 0.85 9 segments: stability 0.87 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.453 seconds) .. _sphx_glr_download_auto_examples_08_evaluation_plot_02_stability.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/08_evaluation/plot_02_stability.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_stability.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_stability.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_stability.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_