.. 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_01_pca_t2_q.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_01_pca_t2_q.py: PCA segmentation with the T² and Q statistics ============================================= Abonyi et al. (2005) segment a multivariate series so that the PCA model of every segment describes its samples well: the cost of a segment is the sum of the reconstruction errors (:math:`Q`) or of the Hotelling :math:`T^2` values of its samples. Bankó (2015, Eq. 3.1) uses the same costs with bottom-up search for correlation-based warping. :math:`Q` reacts to changes of the correlation structure, :math:`T^2` to changes of the variation within the model; their sum is the full squared reconstruction distance. .. GENERATED FROM PYTHON SOURCE LINES 13-25 .. code-block:: Python import matplotlib.pyplot as plt import pandas as pd import mvtsa as mv from mvtsa import segmentation as sg from mvtsa.evaluation import evaluate_segmentation X, cps, _ = mv.datasets.make_correlation_switching( n_segments=5, length=120, n_variables=4, random_state=2 ) .. GENERATED FROM PYTHON SOURCE LINES 26-28 Q, T² and their sum ------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-40 .. code-block:: Python rows = [] for stat in ("Q", "T2", "both"): seg = sg.PCASegmenter( n_segments=5, n_components=1, statistic=stat ).segment(X) rows.append( { "statistic": stat, **evaluate_segmentation(cps, seg, len(X), margin=6), } ) print(pd.DataFrame(rows)[["statistic", "f1", "covering", "hausdorff"]]) .. rst-class:: sphx-glr-script-out .. code-block:: none statistic f1 covering hausdorff 0 Q 1.0 1.000000 0.0 1 T2 0.0 0.198095 440.0 2 both 0.0 0.554853 90.0 .. GENERATED FROM PYTHON SOURCE LINES 41-44 The variances of the variables are the same in every segment, so the :math:`T^2` statistic -- the variation *inside* the model -- has nothing to react to; the changes live entirely in the residual space. .. GENERATED FROM PYTHON SOURCE LINES 46-50 The loadings of the segment models ---------------------------------- The first principal direction of every segment: the correlation structure that defines the segments. .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python seg = sg.PCASegmenter(n_segments=5, n_components=1).segment(X) mv.viz.plot_segmentation(X, seg, truth=cps) mv.viz.plot_segment_loadings(X, seg) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_01_pca_t2_q_001.png :alt: plot 01 pca t2 q :srcset: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_01_pca_t2_q_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_01_pca_t2_q_002.png :alt: plot 01 pca t2 q :srcset: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_01_pca_t2_q_002.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 55-59 Parameter study: the number of principal components --------------------------------------------------- One latent variable generates the data. With more components the :math:`Q` statistic sees less and less of the structure. .. GENERATED FROM PYTHON SOURCE LINES 59-65 .. code-block:: Python for q in (1, 2, 3): s = sg.PCASegmenter(n_segments=5, n_components=q).segment(X) print( f"{q} components: covering {evaluate_segmentation(cps, s, len(X))['covering']:.3f}" ) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none 1 components: covering 1.000 2 components: covering 1.000 3 components: covering 0.717 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.135 seconds) .. _sphx_glr_download_auto_examples_03_latent_segmentation_plot_01_pca_t2_q.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_01_pca_t2_q.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_pca_t2_q.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_pca_t2_q.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_pca_t2_q.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_