FastICA Blind Source Separation from Scratch

Quick Overview

Implement parallel FastICA with the logcosh contrast in NumPy: whiten the data, run symmetric fixed-point updates, and return the recovered sources with the mixing and unmixing matrices. It tests eigendecomposition-based whitening, symmetric decorrelation, and why ICA only identifies sources up to permutation and sign.

FastICA Blind Source Separation from Scratch

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement parallel FastICA with the `logcosh` contrast in NumPy. Given observations `X` of shape `(n, d)`, whiten them, iterate symmetric fixed-point updates, and return the recovered sources together with the mixing and unmixing matrices. The implementation has four functions: - `whiten(X, n_components=None)` returns `(Z, K, mean)`. Use the covariance `Xc.T @ Xc / n` and its top `k` eigenvectors sorted by descending eigenvalue, with each sign flipped so its largest-magnitude entry is positive. Set `K = (V / sqrt(vals)).T` and `Z = Xc @ K.T`. - `symmetric_decorrelation(W)` returns `(W Wᵀ)^(-1/2) W`. - `fastica_step(W, Z)`: with `g = tanh(Z Wᵀ)`, compute `W1 = gᵀZ/n - diag(mean(1 - g²)) W`, then return `symmetric_decorrelation(W1)`. - `fast_ica(X, n_components=None, w_init=None, max_iter=200, tol=1e-6)`: `w_init` defaults to the identity and is decorrelated first. Stop after `max_iter` steps or once `max|abs(diag(W_new Wᵀ)) - 1| < tol`. Return `(S, A, W, mean)` with `S = Z Wᵀ` of shape `(n, k)`, `A = pinv(W K)` of shape `(d, k)`, and `W` of shape `(k, k)`. For example, when `X` holds three mixed signals: ```python S, A, W, mean = fast_ica(X, w_init=np.eye(3), max_iter=500, tol=1e-8) # W @ W.T ≈ I, S.T @ S / n ≈ I # S @ A.T + mean ≈ X (full rank) # S matches true sources up to permutation and sign ``` ```hint Eigenvalue order `np.linalg.eigh` returns eigenvalues in ascending order, so reorder them before keeping the top `k`. A quick test of the whitening step: `Z.T @ Z / n` should be the identity up to rounding. ``` ```hint Square roots and the diagonal term `W Wᵀ` is symmetric positive definite, so one eigendecomposition gives its inverse square root. The `diag(...) W` term only rescales the rows of `W`, which broadcasting handles without building a diagonal matrix. ``` ### Constraints & Assumptions - `X` is a real array of shape `(n, d)`, and `k = n_components` satisfies `1 <= k <= d` (default `k = d`). - The covariance uses the `1/n` normalization. - `w_init`, when given, has shape `(k, k)`. - You may assume the top `k` eigenvalues of the covariance are strictly positive. - Only NumPy is needed. Do not use `sklearn`. ### What a Strong Answer Covers - Whitening that follows the spec exactly: centering, the `1/n` covariance, the descending top `k` eigenpairs, the sign convention, and the shapes of `K` and `Z`. - A correct `(W Wᵀ)^(-1/2) W` and an explanation of why all rows of `W` are decorrelated together at every step. - The fixed-point update with `tanh` and its derivative, written without loops, plus the convergence test on the diagonal of `W_new Wᵀ`. - The output shapes, and an argument for why `S` is white and why `S Aᵀ + mean` reconstructs `X` at full rank. - Failure cases: tiny or zero eigenvalues, a singular starting matrix, and running out of iterations. ### Follow-up Questions - Why can ICA recover sources only up to permutation and scale or sign? - How does deflation FastICA differ from the symmetric version? - Why can ICA not separate more than one Gaussian source?

Overview: Implement parallel FastICA with the logcosh contrast in NumPy: whiten the data, run symmetric fixed-point updates, and return the recovered sources with the mixing and unmixing matrices. It tests eigendecomposition-based whitening, symmetric decorrelation, and why ICA only identifies sources up to permutation and sign.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement parallel FastICA with the logcosh contrast in NumPy. Given observations X of shape (n, d), whiten them, iterate symmetric fixed-point updates, and return the recovered sources together with the mixing and unmixing matrices.

The implementation has four functions:

  • whiten(X, n_components=None) returns (Z, K, mean) . Use the covariance Xc.T @ Xc / n and its top k eigenvectors sorted by descending eigenvalue, with each sign flipped so its largest-magnitude entry is positive. Set K = (V / sqrt(vals)).T and Z = Xc @ K.T .
  • symmetric_decorrelation(W) returns (W Wᵀ)^(-1/2) W .
  • fastica_step(W, Z) : with g = tanh(Z Wᵀ) , compute W1 = gᵀZ/n - diag(mean(1 - g²)) W , then return symmetric_decorrelation(W1) .
  • fast_ica(X, n_components=None, w_init=None, max_iter=200, tol=1e-6) : w_init defaults to the identity and is decorrelated first. Stop after max_iter steps or once max|abs(diag(W_new Wᵀ)) - 1| < tol . Return (S, A, W, mean) with S = Z Wᵀ of shape (n, k) , A = pinv(W K) of shape (d, k) , and W of shape (k, k) .

For example, when X holds three mixed signals:

S, A, W, mean = fast_ica(X, w_init=np.eye(3), max_iter=500, tol=1e-8)
# W @ W.T ≈ I,  S.T @ S / n ≈ I
# S @ A.T + mean ≈ X   (full rank)
# S matches true sources up to permutation and sign

Constraints & Assumptions

  • X is a real array of shape (n, d) , and k = n_components satisfies 1 <= k <= d (default k = d ).
  • The covariance uses the 1/n normalization.
  • w_init , when given, has shape (k, k) .
  • You may assume the top k eigenvalues of the covariance are strictly positive.
  • Only NumPy is needed. Do not use sklearn .

What a Strong Answer Covers Guidance

  • Whitening that follows the spec exactly: centering, the 1/n covariance, the descending top k eigenpairs, the sign convention, and the shapes of K and Z .
  • A correct (W Wᵀ)^(-1/2) W and an explanation of why all rows of W are decorrelated together at every step.
  • The fixed-point update with tanh and its derivative, written without loops, plus the convergence test on the diagonal of W_new Wᵀ .
  • The output shapes, and an argument for why S is white and why S Aᵀ + mean reconstructs X at full rank.
  • Failure cases: tiny or zero eigenvalues, a singular starting matrix, and running out of iterations.

Follow-up Questions Guidance

  • Why can ICA recover sources only up to permutation and scale or sign?
  • How does deflation FastICA differ from the symmetric version?
  • Why can ICA not separate more than one Gaussian source?