.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_basics/plot_03_pca_models.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_01_basics_plot_03_pca_models.py: PCA models: T², Q and similarity factors ======================================== A PCA model fitted on normal operation describes the correlation of the variables; the Hotelling :math:`T^2` statistic measures the variation inside the model, the :math:`Q` statistic the distance from it. When the correlation structure changes, :math:`Q` exceeds its control limit -- the principle behind PCA-based segmentation and monitoring. .. GENERATED FROM PYTHON SOURCE LINES 11-27 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mvtsa import viz from mvtsa.datasets import make_correlation_switching from mvtsa.representation import ( PCAModel, chordal_distance, krzanowski_similarity, ) X, cps, _ = make_correlation_switching( n_segments=2, length=300, random_state=3 ) .. GENERATED FROM PYTHON SOURCE LINES 28-30 Monitoring statistics of a reference model ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 30-39 .. code-block:: Python model = PCAModel(n_components=1).fit(X[:300]) # first regime as reference t2_lim, q_lim = model.control_limits(alpha=0.01) q = model.q_statistic(X) print( f"Q limit {q_lim:.3f}; exceedance before/after the change: " f"{np.mean(q[:300] > q_lim):.2f} / {np.mean(q[300:] > q_lim):.2f}" ) viz.plot_pca_statistics(X, model) .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_03_pca_models_001.png :alt: plot 03 pca models :srcset: /auto_examples/01_basics/images/sphx_glr_plot_03_pca_models_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Q limit 0.088; exceedance before/after the change: 0.01 / 0.86
.. GENERATED FROM PYTHON SOURCE LINES 40-42 Comparing PCA models: Krzanowski similarity ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-52 .. code-block:: Python m1, m2 = PCAModel(1).fit(X[:300]), PCAModel(1).fit(X[300:]) print( "s_PCA(regime 1, regime 1 half) =", round(m1.similarity(PCAModel(1).fit(X[:150])), 3), ) print("s_PCA(regime 1, regime 2) =", round(m1.similarity(m2), 3)) print( "chordal distance =", round(chordal_distance(m1.components_, m2.components_), 3), ) .. rst-class:: sphx-glr-script-out .. code-block:: none s_PCA(regime 1, regime 1 half) = 1.0 s_PCA(regime 1, regime 2) = 0.11 chordal distance = 0.943 .. GENERATED FROM PYTHON SOURCE LINES 53-57 Parameter study: window length and similarity --------------------------------------------- The similarity of short windows to the reference fluctuates; longer windows estimate the subspace better. .. GENERATED FROM PYTHON SOURCE LINES 57-71 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 3)) for w in (20, 50, 100): s = [ krzanowski_similarity( m1.components_, PCAModel(1).fit(X[a : a + w]).components_ ) for a in range(0, len(X) - w, 5) ] ax.plot(np.arange(0, len(X) - w, 5) + w, s, label=f"window {w}") ax.axvline(300, color="gray", ls=":") ax.set_xlabel("window end") ax.set_ylabel("Krzanowski similarity") ax.legend(frameon=False) plt.show() .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_03_pca_models_002.png :alt: plot 03 pca models :srcset: /auto_examples/01_basics/images/sphx_glr_plot_03_pca_models_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.171 seconds) .. _sphx_glr_download_auto_examples_01_basics_plot_03_pca_models.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/01_basics/plot_03_pca_models.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_pca_models.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_pca_models.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_pca_models.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_