.. 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_01_costs_and_search.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_01_costs_and_search.py: Costs and search strategies =========================== Any cost can be combined with any search strategy. Here a piecewise stationary series with changes of the mean, the variance and the correlation is segmented with five costs and five strategies. .. GENERATED FROM PYTHON SOURCE LINES 9-27 .. code-block:: Python import matplotlib.pyplot as plt import pandas as pd from mvtsa import segmentation as sg from mvtsa import viz from mvtsa.datasets import make_piecewise_stationary from mvtsa.evaluation import evaluate_segmentation X, cps, _ = make_piecewise_stationary( n_segments=5, length=150, n_variables=3, change="all", vary=0.3, random_state=4, ) .. GENERATED FROM PYTHON SOURCE LINES 28-30 Five costs, bottom-up search ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-37 .. code-block:: Python rows = [] for cost in ("l2", "normal", "linear", "kernel", "pca"): seg = sg.BottomUp(cost, n_segments=5, initial_size=10).segment(X) rows.append( {"cost": cost, **evaluate_segmentation(cps, seg, len(X), margin=10)} ) print(pd.DataFrame(rows)[["cost", "f1", "covering", "hausdorff"]]) .. rst-class:: sphx-glr-script-out .. code-block:: none cost f1 covering hausdorff 0 l2 0.75 0.768717 143.0 1 normal 1.00 0.963732 7.0 2 linear 0.75 0.768717 143.0 3 kernel 1.00 0.973701 3.0 4 pca 1.00 0.973701 3.0 .. GENERATED FROM PYTHON SOURCE LINES 38-44 The Gaussian and kernel costs see every kind of change; the L2 and linear costs only see the changes of the mean. The PCA cost -- whose reconstruction error is computed around the segment mean with the segment's own principal subspace -- reacts to the changes of the correlation structure, and finds every boundary here because each of them changes the correlations as well. .. GENERATED FROM PYTHON SOURCE LINES 46-48 Five search strategies, Gaussian cost ------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-69 .. code-block:: Python searches = { "bottom-up": sg.BottomUp("normal", n_segments=5, initial_size=10), "top-down": sg.TopDown("normal", n_segments=5, jump=5), "optimal": sg.OptimalPartition("normal", n_segments=5, jump=10), "PELT": sg.PELT("normal", penalty=250, jump=5, min_size=20), "window": sg.WindowDiscrepancy("normal", width=40, n_segments=5), } for name, s in searches.items(): seg = s.segment(X) print( f"{name:10s}", seg.change_points, round(evaluate_segmentation(cps, seg, len(X), 10)["covering"], 3), ) viz.plot_segmentation( X, searches["PELT"].segment(X), truth=cps, title="PELT with the Gaussian cost", ) .. image-sg:: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_01_costs_and_search_001.png :alt: PELT with the Gaussian cost :srcset: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_01_costs_and_search_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none bottom-up [180 320 500 610] 0.964 top-down [179 319 498 604] 0.989 optimal [184 314 494 604] 0.958 PELT [180 320 495 605] 0.974 window [177 318 498 603] 1.0 .. GENERATED FROM PYTHON SOURCE LINES 70-75 Parameter study: the PELT penalty --------------------------------- The BIC default (about 60 here) over-segments this series because the variance changes make the Gaussian likelihood very sensitive; the penalty path shows how the number of change points settles as the penalty grows. .. GENERATED FROM PYTHON SOURCE LINES 75-84 .. code-block:: Python path = sg.penalty_path( X, "normal", penalties=[10, 20, 40, 80, 160, 250, 320, 640, 1280] ) print(path) fig, ax = plt.subplots(figsize=(5, 3)) ax.semilogx(list(path), list(path.values()), "o-") ax.set_xlabel("penalty") ax.set_ylabel("number of change points") plt.show() .. image-sg:: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_01_costs_and_search_002.png :alt: plot 01 costs and search :srcset: /auto_examples/02_cost_segmentation/images/sphx_glr_plot_01_costs_and_search_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none {10.0: 101, 20.0: 15, 40.0: 7, 80.0: 6, 160.0: 5, 250.0: 5, 320.0: 4, 640.0: 3, 1280.0: 0} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.580 seconds) .. _sphx_glr_download_auto_examples_02_cost_segmentation_plot_01_costs_and_search.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_01_costs_and_search.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_costs_and_search.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_costs_and_search.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_costs_and_search.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_