.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/03_latent_segmentation/plot_02_dynamic_pca.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_03_latent_segmentation_plot_02_dynamic_pca.py: Dynamic PCA segmentation ======================== When the segments differ in their *dynamics* -- here a second-order system whose parameters change while the static gain is kept -- a static PCA model sees nothing. Dynamic PCA (Ku et al., 1995) augments every sample with its lagged values, so that the principal subspace of the lag matrix encodes the input--output dynamics; segmenting with it (Dobos and Abonyi, 2012; Bankó, 2015) recovers the changes. The number of lags can be chosen from the eigenvalues of the covariance of the lag matrix: a lag that completes a dynamic relation makes the smallest eigenvalue drop by orders of magnitude. .. GENERATED FROM PYTHON SOURCE LINES 15-28 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import mvtsa as mv from mvtsa import segmentation as sg from mvtsa.evaluation import evaluate_segmentation from mvtsa.representation import select_dpca_lag Y, cps, _ = mv.datasets.make_dynamic_switching( order=2, n_segments=3, length=300, random_state=1 ) .. GENERATED FROM PYTHON SOURCE LINES 29-31 Static versus dynamic PCA ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-43 .. code-block:: Python static = sg.PCASegmenter(n_segments=3, n_components=1).segment(Y) dynamic = sg.DPCASegmenter(n_segments=3, lags=2, n_components=5).segment(Y) for name, s in [("PCA", static), ("DPCA", dynamic)]: print( f"{name:5s}", s.change_points, evaluate_segmentation(cps, s, len(Y), margin=15)["f1"], ) mv.viz.plot_segmentation( Y, dynamic, truth=cps, names=["u", "y"], title="Dynamic PCA, two lags" ) .. image-sg:: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_02_dynamic_pca_001.png :alt: Dynamic PCA, two lags :srcset: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_02_dynamic_pca_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none PCA [360 560] 0.0 DPCA [300 600] 1.0 .. GENERATED FROM PYTHON SOURCE LINES 44-46 Choosing the number of lags --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 46-56 .. code-block:: Python Y1, _, _ = mv.datasets.make_dynamic_switching( order=1, n_segments=3, length=300, random_state=0 ) print("selected lag, first-order system: ", select_dpca_lag(Y1)) print("selected lag, second-order system:", select_dpca_lag(Y)) print( "Ku et al. (1995) count of relations:", select_dpca_lag(Y1, method="ku"), select_dpca_lag(Y, method="ku"), ) .. rst-class:: sphx-glr-script-out .. code-block:: none selected lag, first-order system: 1 selected lag, second-order system: 2 Ku et al. (1995) count of relations: 1 1 .. GENERATED FROM PYTHON SOURCE LINES 57-61 The pH neutralization process ----------------------------- The titration curve makes the process gain change by orders of magnitude, so the local linear dynamics differ between operating ranges. .. GENERATED FROM PYTHON SOURCE LINES 61-68 .. code-block:: Python P = mv.datasets.simulate_ph_cstr(T=720, random_state=0) seg_ph = sg.DPCASegmenter(n_segments=6, lags=2, n_components=0.99).segment(P) print( "pH change points:", seg_ph.change_points, "| input steps every 60 samples" ) mv.viz.plot_segmentation(P, seg_ph, names=["base flow", "pH"]) .. image-sg:: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_02_dynamic_pca_002.png :alt: plot 02 dynamic pca :srcset: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_02_dynamic_pca_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none pH change points: [ 60 120 240 300 660] | input steps every 60 samples .. GENERATED FROM PYTHON SOURCE LINES 69-71 Parameter study: lags --------------------- .. GENERATED FROM PYTHON SOURCE LINES 71-85 .. code-block:: Python f1 = [ evaluate_segmentation( cps, sg.DPCASegmenter(3, lags=L, n_components=0.99).segment(Y), len(Y), 15, )["f1"] for L in range(0, 5) ] fig, ax = plt.subplots(figsize=(4.5, 2.8)) ax.bar(np.arange(5), f1, color="#285780") ax.set_xlabel("number of lags") ax.set_ylabel("F1 (margin 15)") plt.show() .. image-sg:: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_02_dynamic_pca_003.png :alt: plot 02 dynamic pca :srcset: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_02_dynamic_pca_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.139 seconds) .. _sphx_glr_download_auto_examples_03_latent_segmentation_plot_02_dynamic_pca.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/03_latent_segmentation/plot_02_dynamic_pca.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_dynamic_pca.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_dynamic_pca.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_dynamic_pca.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_