Skip to content

Instantly share code, notes, and snippets.

@lmmx
Last active December 21, 2025 16:16
Show Gist options
  • Select an option

  • Save lmmx/160319a68b69cd28c5b955b8224958ac to your computer and use it in GitHub Desktop.

Select an option

Save lmmx/160319a68b69cd28c5b955b8224958ac to your computer and use it in GitHub Desktop.
[linfa-ica] `GFunc::Exp` computes incorrect g and g' derivatives

In fast_ica.rs, the exp function appears to have a bug in the exponentiation func, where the variable named exp stores $-u^2/2$ rather than $exp(-u^2/2)$

For $G(u) = -\exp(-u^2/2)$, the derivatives should be:

$$g(u) = G'(u) = u \exp\left(-\frac{u^2}{2}\right)$$

$$g'(u) = G''(u) = (1 - u^2) \exp\left(-\frac{u^2}{2}\right)$$

https://github.com/rust-ml/linfa/blob/db3cadee926e546a2809346c73689ce5048a96e9/algorithms/linfa-ica/src/fast_ica.rs#L239-L246

So shouldn't it be instead:

fn exp<A: Float>(x: &Array2<A>) -> (Array2<A>, Array1<A>) {

    let exp = x.mapv(|x| (-x.powi(2) / A::cast(2.)).exp());

    (

        x * &exp,

        (x.mapv(|x| A::cast(1.) - x.powi(2)) * &exp)

            .mean_axis(Axis(1))

            .unwrap(),

    )

}

https://github.com/scikit-learn/scikit-learn/blob/8a98df038ce00a7c016f6ce4abdee1f53e81b811/sklearn/decomposition/_fastica.py#L157-L162

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment