Whitening transforms data so that its covariance becomes the identity. Implement the two classic whitening matrices from Murphy's book.
whitening_matrix(X, method="zca", eps=0.0) where X has shape (N, D):
- Compute the column means μ and the covariance of the centered data with the 1/N convention, Σ=N1(X−μ)⊤(X−μ).
- Eigendecompose Σ=EDE⊤ with the eigenvalues sorted in decreasing order, and flip each eigenvector (column of E) so that its entry of largest absolute value is positive (first such index on ties).
method="pca": W=(D+ϵI)−1/2E⊤.
method="zca": W=E(D+ϵI)−1/2E⊤.
- Return
(mu, W) with mu of shape (D,) and W of shape (D, D). Raise ValueError for any other method.
whiten(X, mu, W): return Y whose row n is W(xn−μ), shape (N, D).
With eps=0 the whitened training data has sample covariance (with 1/N) exactly I.