public void learn_whiten_success()
{
double[,] data =
{
{ 2.5, 2.4 },
{ 0.5, 0.7 },
{ 2.2, 2.9 },
{ 1.9, 2.2 },
{ 3.1, 3.0 },
{ 2.3, 2.7 },
{ 2.0, 1.6 },
{ 1.0, 1.1 },
{ 1.5, 1.6 },
{ 1.1, 0.9 }
};
var method = PrincipalComponentMethod.Center; // PrincipalComponentMethod.Standardize
var pca = new KernelPrincipalComponentAnalysis(new Linear(), method, whiten: true);
pca.Learn(data);
double[] eigenvalues = { 1.28402771, 0.0490833989 };
double[] proportion = eigenvalues.Divide(eigenvalues.Sum());
double[,] eigenvectors =
{
{ 0.19940687993951403, -1.1061252858739095 },
{ 0.21626410214440508, 1.0199057073792104 }
};
// Everything is alright (up to the 9 decimal places shown in the tutorial)
// Assert.IsTrue(eigenvectors.IsEqual(pca.ComponentMatrix, rtol: 1e-9));
Assert.IsTrue(proportion.IsEqual(pca.ComponentProportions, rtol: 1e-9));
Assert.IsTrue(eigenvalues.IsEqual(pca.Eigenvalues.Divide(data.GetLength(0) - 1), rtol: 1e-5));
double[,] actual = pca.Transform(data);
double[][] expected = new double[][]
{
new double[] { 0.243560157209023, -0.263472650637184 },
new double[] { -0.522902576315494, 0.214938218565977 },
new double[] { 0.291870144299372, 0.578317788814594 },
new double[] { 0.0806632088164338, 0.19622137941132 },
new double[] { 0.492962746459375, -0.315204397734004 },
new double[] { 0.268558011864442, 0.263724118751361 },
new double[] { -0.0291545644762578, -0.526334573603598 },
new double[] { -0.336693495487974, 0.0698378585807067 },
new double[] { -0.128858004446015, 0.0267280693333571 },
new double[] { -0.360005627922904, -0.244755811482527 }
}.Multiply(-1);
// Everything is correct (up to 8 decimal places)
Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));
}
}