Main fallacy was in operation that converts points to new basis:
y = (W*x')';
Wikipedia says:
The projected vectors are the columns of the matrix
Y = W*·Z,
where
Y is L×N, W is M×L, Z is M×N
,
but pca()
returns W
of size L×M
and Y
of size NxL
so, correct equation in Matlab is:
y = x*W
Below is the corrected code:
[W, Y] = pca(data, 'VariableWeights', 'variance', 'Centered', true);
W = diag(std(data))\W;
%# Getting mean and weights of data (for future data)
[~, mu, we] = zscore(data);
we(we==0) = 1;
%# New point in original 9dim system
%# For example, it is the first point of our input data
x = data(1,:);
x = bsxfun(@minus,x, mu);
x = bsxfun(@rdivide, x, we);
%# New coordinates as principal components
y = x*W;
y0 = Y(1,:);
sum(abs(y0 - y)) %# 4.1883e-14 ~= 0