给一个任意矩阵 X,一般情况下它的协方差矩阵并不是对角矩阵。矩阵白化就是用一个白化矩阵 A,使 Y = A * X 的协方差矩阵转化为对角矩阵。
这里首先指出 Y = A * X 成立的前提是 X 中的元素是按列排列的,如果 X 按行排列,Y = X * A。
clear; clc;
%% data are stored as rows.
X = randi(10,2,3);
[rows, cols] = size(X);
%% zero-mean.
meanMatrix = mean(X);
X = X - ones(rows, 1) * meanMatrix;
%%
% covM is symmetric, so svd is the same as eig.
% 1. U = V
% 2. svd is more stable than eig.
covM = cov(X);
[U, D, V] = svd(covM)
% add a small number eps to avoid singular Matrix situations.
A = U * inv(diag(sqrt(diag(D) + eps))); % use inv
% A = U * diag(1 ./ (sqrt(diag(D) + eps))); % or use ./
Y = X * A
cov(Y)