Пример #1
0
        public void Test()
        {
            // Make a random list.
            RandomHelper.Random = new Random(77);
            List <VectorF> points = new List <VectorF>();

            for (int i = 0; i < 10; i++)
            {
                var vector = new VectorF(4);
                RandomHelper.Random.NextVectorF(vector, -1, 10);
                points.Add(vector);
            }

            PrincipalComponentAnalysisF pca = new PrincipalComponentAnalysisF(points);

            Assert.Greater(pca.Variances[0], pca.Variances[1]);
            Assert.Greater(pca.Variances[1], pca.Variances[2]);
            Assert.Greater(pca.Variances[2], pca.Variances[3]);
            Assert.Greater(pca.Variances[3], 0);

            Assert.IsTrue(pca.V.GetColumn(0).IsNumericallyNormalized);
            Assert.IsTrue(pca.V.GetColumn(1).IsNumericallyNormalized);
            Assert.IsTrue(pca.V.GetColumn(2).IsNumericallyNormalized);
            Assert.IsTrue(pca.V.GetColumn(3).IsNumericallyNormalized);

            // Compute covariance matrix and check if it is diagonal in the transformed space.
            MatrixF cov            = StatisticsHelper.ComputeCovarianceMatrix(points);
            MatrixF transformedCov = pca.V.Transposed * cov * pca.V;

            for (int row = 0; row < transformedCov.NumberOfRows; row++)
            {
                for (int column = 0; column < transformedCov.NumberOfColumns; column++)
                {
                    if (row != column)
                    {
                        Assert.IsTrue(Numeric.IsZero(transformedCov[row, column]));
                    }
                }
            }

            // The principal components must be Eigenvectors which means that multiplying with the covariance
            // matrix does only change the length!
            VectorF v0       = pca.V.GetColumn(0);
            VectorF v0Result = cov * v0;

            Assert.IsTrue(VectorF.AreNumericallyEqual(v0.Normalized, v0Result.Normalized));
            VectorF v1       = pca.V.GetColumn(1);
            VectorF v1Result = cov * v1;

            Assert.IsTrue(VectorF.AreNumericallyEqual(v1.Normalized, v1Result.Normalized));
            VectorF v2       = pca.V.GetColumn(2);
            VectorF v2Result = cov * v2;

            Assert.IsTrue(VectorF.AreNumericallyEqual(v2.Normalized, v2Result.Normalized));
            VectorF v3       = pca.V.GetColumn(3);
            VectorF v3Result = cov * v3;

            Assert.IsTrue(VectorF.AreNumericallyEqual(v3.Normalized, v3Result.Normalized));
        }
Пример #2
0
        public void ComputeCovarianceMatrix3DWithEmptyList()
        {
            var result = StatisticsHelper.ComputeCovarianceMatrix(new List <Vector3D>());

            foreach (var element in result.ToList(MatrixOrder.RowMajor))
            {
                Assert.IsNaN(element);
            }
        }
Пример #3
0
 public void ComputeCovarianceMatrixDWithVectorsOfDifferentLength()
 {
     List <VectorD> points = new List <VectorD>(new[]
     {
         new VectorD(new double[] { -1, -2, 1 }),
         new VectorD(new double[] { 2, -1, 3, 1 }),
         new VectorD(new double[] { 2, -1, 2 }),
     });
     var result = StatisticsHelper.ComputeCovarianceMatrix(points);
 }
Пример #4
0
 public void ComputeCovarianceMatrixFWithVectorsOfDifferentLength()
 {
     List <VectorF> points = new List <VectorF>(new[]
     {
         new VectorF(new float[] { -1, -2, 1 }),
         new VectorF(new float[] { 2, -1, 3 }),
         new VectorF(new float[] { 2, -1, 2, 3 }),
     });
     var result = StatisticsHelper.ComputeCovarianceMatrix(points);
 }
        // Computes the covariance matrix for a list of points.
        private static MatrixF ComputeCovarianceMatrixFromPoints(IList <Vector3> points)
        {
            // Convert IList<Vector3> to IList<VectorF> which is required for PCA.
            int            numberOfPoints = points.Count;
            List <VectorF> pointsCopy     = new List <VectorF>(numberOfPoints);

            for (int i = 0; i < numberOfPoints; i++)
            {
                var point = points[i];
                pointsCopy.Add(point.ToVectorF());
            }

            return(StatisticsHelper.ComputeCovarianceMatrix(pointsCopy));
        }
Пример #6
0
 public void ComputeCovarianceMatrixDWithEmptyList()
 {
     var result = StatisticsHelper.ComputeCovarianceMatrix(new List <VectorD>());
 }
Пример #7
0
 public void ComputeCovarianceMatrixDWithArgumentNull()
 {
     StatisticsHelper.ComputeCovarianceMatrix((List <VectorD>)null);
 }
Пример #8
0
        public void ComputeCovarianceMatrix3F()
        {
            // Make a random list.
            List <Vector3F> points3F = new List <Vector3F>(new[]
            {
                new Vector3F(-1, -2, 1),
                new Vector3F(1, 0, 2),
                new Vector3F(2, -1, 3),
                new Vector3F(2, -1, 2),
            });

            Matrix33F cov3F = StatisticsHelper.ComputeCovarianceMatrix(points3F);

            Assert.AreEqual(3f / 2, cov3F[0, 0]);
            Assert.AreEqual(1f / 2, cov3F[0, 1]);
            Assert.AreEqual(3f / 4, cov3F[0, 2]);
            Assert.AreEqual(1f / 2, cov3F[1, 0]);
            Assert.AreEqual(1f / 2, cov3F[1, 1]);
            Assert.AreEqual(1f / 4, cov3F[1, 2]);
            Assert.AreEqual(3f / 4, cov3F[2, 0]);
            Assert.AreEqual(1f / 4, cov3F[2, 1]);
            Assert.AreEqual(1f / 2, cov3F[2, 2]);

            // Compare with Vector3D version.
            List <Vector3D> points3D = new List <Vector3D>();

            foreach (var point in points3F)
            {
                points3D.Add(point.ToVector3D());
            }
            Matrix33D cov3D = StatisticsHelper.ComputeCovarianceMatrix(points3D);

            for (int i = 0; i < cov3F.ToArray1D(MatrixOrder.RowMajor).Length; i++)
            {
                var item3F = cov3F.ToArray1D(MatrixOrder.RowMajor)[i];
                var item3D = cov3D.ToArray1D(MatrixOrder.RowMajor)[i];
                Assert.AreEqual(item3F, item3D);
            }

            // Compare with VectorF version.
            List <VectorF> pointsF = new List <VectorF>();

            foreach (var point in points3F)
            {
                pointsF.Add(point.ToVectorF());
            }
            MatrixF covF = StatisticsHelper.ComputeCovarianceMatrix(pointsF);

            for (int i = 0; i < cov3F.ToArray1D(MatrixOrder.RowMajor).Length; i++)
            {
                var item3F = cov3F.ToArray1D(MatrixOrder.RowMajor)[i];
                var itemF  = covF.ToArray1D(MatrixOrder.RowMajor)[i];
                Assert.AreEqual(item3F, itemF);
            }

            // Compare with VectorF version.
            List <VectorD> pointsD = new List <VectorD>();

            foreach (var point in points3D)
            {
                pointsD.Add(point.ToVectorD());
            }
            MatrixD covD = StatisticsHelper.ComputeCovarianceMatrix(pointsD);

            for (int i = 0; i < cov3F.ToArray1D(MatrixOrder.RowMajor).Length; i++)
            {
                var item3F = cov3F.ToArray1D(MatrixOrder.RowMajor)[i];
                var itemD  = covD.ToArray1D(MatrixOrder.RowMajor)[i];
                Assert.AreEqual(item3F, itemD);
            }
        }