.. 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_03_recursive_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_03_recursive_pca.py: On-line segmentation with recursive PCA ======================================= Dobos and Abonyi (2012) segment process data *on line*: a recursive PCA model is updated at every sample with a variable forgetting factor, and the Krzanowski similarity between the running model and the model of the current segment signals a change. Because the recursive model needs some samples to follow a change, the detection comes with a delay; a retrospective search over the samples of the closing segment localizes the change exactly. .. GENERATED FROM PYTHON SOURCE LINES 13-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import mvtsa as mv from mvtsa import segmentation as sg X, cps, _ = mv.datasets.make_correlation_switching( n_segments=4, length=250, n_variables=4, random_state=5 ) .. GENERATED FROM PYTHON SOURCE LINES 25-27 Batch use --------- .. GENERATED FROM PYTHON SOURCE LINES 27-44 .. code-block:: Python rp = sg.RecursivePCASegmenter( threshold=0.7, n_components=1, forgetting=0.97, warmup=60, patience=8 ) seg = rp.segment(X) print("detected:", seg.change_points, " true:", cps) fig, axes = plt.subplots(3, 1, figsize=(7, 5.5), sharex=True) mv.viz.plot_segmentation(X, seg, truth=cps, ax=axes[0]) axes[1].plot(rp.similarity_, color="#285780", lw=1) axes[1].axhline(rp.threshold, color="#D55E00", ls="--", lw=1) axes[1].set_ylabel("similarity") axes[2].plot(rp.forgetting_, color="#009E73", lw=1) axes[2].set_ylabel("forgetting") axes[2].set_xlabel("sample") for ax in axes[1:]: for c in cps: ax.axvline(c, color="gray", ls=":", lw=0.8) .. image-sg:: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_03_recursive_pca_001.png :alt: plot 03 recursive pca :srcset: /auto_examples/03_latent_segmentation/images/sphx_glr_plot_03_recursive_pca_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none detected: [250 501 749] true: [250 500 750] .. GENERATED FROM PYTHON SOURCE LINES 45-49 Stream use ---------- :meth:`partial_fit` processes one sample at a time and returns ``True`` when a change point has been declared. The scaling must be fixed in advance. .. GENERATED FROM PYTHON SOURCE LINES 49-60 .. code-block:: Python stream = sg.RecursivePCASegmenter( threshold=0.7, n_components=1, forgetting=0.97, warmup=60, patience=8, scale=(X[:200].mean(0), X[:200].std(0)), ) for t, x in enumerate(X): if stream.partial_fit(x): print(f"sample {t}: change declared at {stream.change_points_[-1]}") .. rst-class:: sphx-glr-script-out .. code-block:: none sample 299: change declared at 250 sample 522: change declared at 501 sample 786: change declared at 749 .. GENERATED FROM PYTHON SOURCE LINES 61-66 Parameter study: forgetting factor and detection delay ------------------------------------------------------ Without the retrospective localization, the reported change point is the moment of detection; a smaller forgetting factor follows changes faster but makes the similarity noisier. .. GENERATED FROM PYTHON SOURCE LINES 66-83 .. code-block:: Python for lam in (0.95, 0.97, 0.99): s = sg.RecursivePCASegmenter( threshold=0.7, n_components=1, forgetting=lam, warmup=60, patience=8, refine=False, ).segment(X) d = [ int(np.min(np.abs(s.change_points - c))) if len(s.change_points) else -1 for c in cps ] print(f"forgetting {lam}: detections {s.change_points}, delays {d}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none forgetting 0.95: detections [277 515 764], delays [27, 15, 14] forgetting 0.97: detections [292 515 779], delays [42, 15, 29] forgetting 0.99: detections [302 559 806], delays [52, 59, 56] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.329 seconds) .. _sphx_glr_download_auto_examples_03_latent_segmentation_plot_03_recursive_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_03_recursive_pca.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_recursive_pca.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_recursive_pca.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_recursive_pca.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_