.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_cost_segmentation/plot_03_number_of_segments.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_02_cost_segmentation_plot_03_number_of_segments.py: How many segments? ================== Five rules for the number of segments on one series: the knee of the cost curve, the relative reduction rate, BIC, a permutation test (after Vasko and Toivonen, 2002) and the persistence of the change points. .. GENERATED FROM PYTHON SOURCE LINES 9-20 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mvtsa import segmentation as sg from mvtsa.datasets import make_piecewise_stationary X, cps, _ = make_piecewise_stationary( n_segments=6, length=100, n_variables=2, random_state=11 ) .. GENERATED FROM PYTHON SOURCE LINES 21-23 The rules --------- .. GENERATED FROM PYTHON SOURCE LINES 23-35 .. code-block:: Python for method, kw in [ ("knee", {}), ("rr", {"rr_threshold": 0.05}), ("bic", {}), ("persistence", {}), ("permutation", {"n_permutations": 19, "random_state": 0}), ]: n, info = sg.select_n_segments( X, "l2", method, max_segments=12, initial_size=5, **kw ) print(f"{method:12s} -> {n} segments") print("true number of segments:", len(cps) + 1) .. rst-class:: sphx-glr-script-out .. code-block:: none knee -> 5 segments rr -> 6 segments bic -> 6 segments persistence -> 6 segments permutation -> 6 segments true number of segments: 6 .. GENERATED FROM PYTHON SOURCE LINES 36-38 BIC as a function of the number of segments ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-52 .. code-block:: Python n, info = sg.select_n_segments(X, "l2", "bic", max_segments=12, initial_size=5) fig, ax = plt.subplots(figsize=(5, 3)) ks = sorted(info["bic"]) ax.plot(ks, [info["bic"][k] for k in ks], "o-") ax.axvline(n, color="gray", ls=":") ax.set_xlabel("number of segments") ax.set_ylabel("BIC") print( "BIC minimum at", n, "segments; min BIC", round(np.min(list(info["bic"].values())), 1), ) plt.show() .. image-sg:: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_03_number_of_segments_001.png :alt: plot 03 number of segments :srcset: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_03_number_of_segments_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none BIC minimum at 6 segments; min BIC 498.3 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.479 seconds) .. _sphx_glr_download_auto_examples_02_cost_segmentation_plot_03_number_of_segments.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/02_cost_segmentation/plot_03_number_of_segments.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_number_of_segments.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_number_of_segments.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_number_of_segments.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_