public void MahalanobisTest() { double[] x = { 1, 0 }; double[,] y = { { 1, 0 }, { 0, 8 }, { 0, 5 } }; // Computing the mean of y double[] meanY = Tools.Mean(y); // Computing the covariance matrix of y double[,] covY = Tools.Covariance(y, meanY); // Inverting the covariance matrix double[,] precision = covY.Inverse(); // Run actual test double expected = 1.33333; double actual = Distance.SquareMahalanobis(x, meanY, precision); Assert.AreEqual(expected, actual, 0.0001); }
public void CovarianceTest2() { double[][] matrix = new double[, ] { { 4.0, 2.0, 0.60 }, { 4.2, 2.1, 0.59 }, { 3.9, 2.0, 0.58 }, { 4.3, 2.1, 0.62 }, { 4.1, 2.2, 0.63 } }.ToArray(); double[,] expected = new double[, ] { { 0.02500, 0.00750, 0.00175 }, { 0.00750, 0.00700, 0.00135 }, { 0.00175, 0.00135, 0.00043 }, }; double[,] actual = Tools.Covariance(matrix); Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0001)); actual = Tools.Covariance(matrix, 0); Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0001)); matrix = matrix.Transpose(); actual = Tools.Covariance(matrix, 1); Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0001)); }
public void CovarianceTest4() { double[] u = { -2, 1, 5 }; double[] v = { 7, 1, 6 }; double expected = -0.8333; double actual = Tools.Covariance(u, v); Assert.AreEqual(expected, actual, 0.0001); }
public void CovarianceTest4() { double[] u = { -2, 1, 5 }; double[] v = { 7, 1, 6 }; double[,] expected = { { 12.3333, -0.8333 }, { -0.8333, 10.3333 } }; double[,] actual = Tools.Covariance(u, v); Assert.IsTrue(expected.IsEqual(actual, 0.0001)); }
public void WhiteningTest() { double[,] value = { { 0.4218, 0.6557, 0.6787, 0.6555 }, { 0.9157, 0.0357, 0.7577, 0.1712 }, { 0.7922, 0.8491, 0.7431, 0.7060 }, { 0.9595, 0.9340, 0.3922, 0.0318 }, }; double[,] I = Matrix.Identity(3); double[,] T = null; // transformation matrix // Perform the whitening transform double[,] actual = Tools.Whitening(value, out T); // Check if covariance matrix has the identity form double[,] cov = Tools.Covariance(actual).Submatrix(0, 2, 0, 2); Assert.IsTrue(cov.IsEqual(I, 1e-10)); // Check if we can transform the data double[,] result = value.Multiply(T); Assert.IsTrue(result.IsEqual(actual)); }
// ------------------------------------------------------------ /// <summary> /// Computes the whitening transform for the given data, making /// its covariance matrix equals the identity matrix. /// </summary> /// <param name="value">A matrix where each column represent a /// variable and each row represent a observation.</param> /// <param name="transformMatrix">The base matrix used in the /// transformation.</param> /// <returns> /// The transformed source data (which now has unit variance). /// </returns> /// public static double[,] Whitening(double[,] value, out double[,] transformMatrix) { if (value == null) { throw new ArgumentNullException("value"); } int cols = value.GetLength(1); double[,] cov = Tools.Covariance(value); // Diagonalizes the covariance matrix SingularValueDecomposition svd = new SingularValueDecomposition(cov, true, // compute left vectors (to become a transformation matrix) false, // do not compute right vectors since they aren't necessary true, // transpose if necessary to avoid erroneous assumptions in SVD true); // perform operation in-place, reducing memory usage // Retrieve the transformation matrix transformMatrix = svd.LeftSingularVectors; // Perform scaling to have unit variance double[] singularValues = svd.Diagonal; for (int i = 0; i < cols; i++) { for (int j = 0; j < singularValues.Length; j++) { transformMatrix[i, j] /= Math.Sqrt(singularValues[j]); } } // Return the transformed data return(value.Multiply(transformMatrix)); }