.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_generated/examples/plot_basic_fit.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. .. rst-class:: sphx-glr-example-title .. _sphx_glr__generated_examples_plot_basic_fit.py: Basic AMICA Fit =============== Fit a single-model AMICA (equivalent to Infomax ICA) on synthetic data. Inspects the raw input, convergence via the log-likelihood curve, and confirms that the sphering matrix correctly whitens the data. .. GENERATED FROM PYTHON SOURCE LINES 9-16 .. code-block:: Python import numpy as np import torch import matplotlib.pyplot as plt from pyamica import AMICA .. GENERATED FROM PYTHON SOURCE LINES 17-20 Generate Synthetic Data ----------------------- Mix 8 independent Laplacian sources with a random matrix. .. GENERATED FROM PYTHON SOURCE LINES 20-29 .. code-block:: Python rng = np.random.default_rng(0) n_ch, T = 8, 4000 sources = rng.laplace(0, 1, (n_ch, T)) A_true = rng.standard_normal((n_ch, n_ch)) data = (A_true @ sources).T.astype("float64") # (T, n_ch) X = torch.from_numpy(data) .. GENERATED FROM PYTHON SOURCE LINES 30-34 Raw Data -------- A look at the first 4 channels. Because the sources are Laplacian, the mixture has occasional large spikes. .. GENERATED FROM PYTHON SOURCE LINES 34-46 .. code-block:: Python fig, axes = plt.subplots(4, 1, figsize=(10, 5), sharex=True) t = np.arange(T) for i, ax in enumerate(axes): ax.plot(t, data[:, i], lw=0.5, color="steelblue") ax.set_ylabel(f"Ch {i}") ax.set_yticks([]) axes[-1].set_xlabel("Sample") fig.suptitle("Raw input data (4 of 8 channels)") fig.tight_layout() plt.show() .. image-sg:: /_generated/examples/images/sphx_glr_plot_basic_fit_001.png :alt: Raw input data (4 of 8 channels) :srcset: /_generated/examples/images/sphx_glr_plot_basic_fit_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 47-49 Fit AMICA (M=1) --------------- .. GENERATED FROM PYTHON SOURCE LINES 49-56 .. code-block:: Python model = AMICA(n_models=1, max_iter=100, verbose=False) model.fit(X) print(f"Iterations: {model.n_iter_}") print(f"Final log-likelihood: {model.ll_history()[-1]:.6f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Iterations: 100 Final log-likelihood: -2.081794 .. GENERATED FROM PYTHON SOURCE LINES 57-60 Log-Likelihood Curve -------------------- The LL should increase monotonically and flatten as the model converges. .. GENERATED FROM PYTHON SOURCE LINES 60-69 .. code-block:: Python fig, ax = plt.subplots(figsize=(7, 3)) ax.plot(model.ll_history().numpy(), color="steelblue") ax.set_xlabel("Iteration") ax.set_ylabel("Log-likelihood") ax.set_title("AMICA M=1: convergence") fig.tight_layout() plt.show() .. image-sg:: /_generated/examples/images/sphx_glr_plot_basic_fit_002.png :alt: AMICA M=1: convergence :srcset: /_generated/examples/images/sphx_glr_plot_basic_fit_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 70-78 Sphering Matrix --------------- AMICA pre-whitens the data with a ZCA (symmetric) sphering matrix ``S = V D^{-1/2} V^T`` (eigendecomposition of the sample covariance). The defining property of a whitening matrix is that the sphered data has identity covariance: :math:`S \cdot \text{Cov}(X) \cdot S^T = I`. Verified directly below. .. GENERATED FROM PYTHON SOURCE LINES 78-93 .. code-block:: Python S = model.sphere_.numpy() # (n_ch, n_ch) X_c = data - data.mean(axis=0) # centre Xs = X_c @ S.T # (T, n_ch) - sphered data cov_sphered = Xs.T @ Xs / T # should be identity fig, ax = plt.subplots(figsize=(4, 4)) im = ax.imshow(cov_sphered, vmin=-0.05, vmax=1.05, cmap="RdBu_r") fig.colorbar(im, ax=ax, shrink=0.8) ax.set_title("Covariance of sphered data\n(should be identity)") fig.tight_layout() plt.show() residual = np.abs(cov_sphered - np.eye(n_ch)).max() print(f"Max deviation from identity: {residual:.2e} (expect < 1e-12)") .. image-sg:: /_generated/examples/images/sphx_glr_plot_basic_fit_003.png :alt: Covariance of sphered data (should be identity) :srcset: /_generated/examples/images/sphx_glr_plot_basic_fit_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Max deviation from identity: 1.38e-14 (expect < 1e-12) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.029 seconds) .. _sphx_glr_download__generated_examples_plot_basic_fit.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_basic_fit.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_basic_fit.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_basic_fit.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_