Set of statistics functions
This class represents collection of functions used in statistics. Every Matrix function assumes data is organized in a table-like model, where Columns represents variables and Rows represents a observation of each variable.
        public void SimplsComputeTest()
        {
            double[,] inputs;
            double[,] outputs;

            var pls = CreateWineExample(out inputs, out outputs);

            MultivariateLinearRegression regression = pls.CreateRegression();


            // test factor proportions
            double[] expectedX = { 0.86, 0.12, 0.00, 0.86 };
            double[] actualX   = pls.Predictors.FactorProportions;
            Assert.IsTrue(expectedX.IsEqual(actualX, atol: 0.01));

            double[] expectedY = { 0.67, 0.13, 0.17, 0.00 };
            double[] actualY   = pls.Dependents.FactorProportions;
            Assert.IsTrue(expectedY.IsEqual(actualY, atol: 0.01));


            // Test Properties
            double[][] weights = pls.Weights;
            double[,] actual = pls.Predictors.Result;

            double[,] X0 = (double[, ])pls.Source.Clone(); Tools.Center(X0, inPlace: true);
            double[,] Y0 = (double[, ])pls.Output.Clone(); Tools.Center(Y0, inPlace: true);

            // XSCORES = X0*W
            double[][] expected = X0.Dot(weights);
            Assert.IsTrue(expected.IsEqual(actual, 0.01));
        }
Exemplo n.º 2
0
        public void SkewnessMatrixTest()
        {
            double[][] matrix =
            {
                new double[] { 180 },
                new double[] { 182 },
                new double[] { 169 },
                new double[] { 175 },
                new double[] { 178 },
                new double[] { 189 },
                new double[] { 174 },
                new double[] { 174 },
                new double[] { 171 },
                new double[] { 168 }
            };

            double expected = 0.77771377;
            {
                double[] actual = Tools.Skewness(matrix);
                Assert.AreEqual(expected, actual[0], 1e-7);
            }
            {
                double actual = Tools.Skewness(matrix.GetColumn(0));
                Assert.AreEqual(expected, actual, 1e-7);
            }
        }
Exemplo n.º 3
0
        public void CovarianceTest()
        {
            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 }
            };


            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));
        }
Exemplo n.º 4
0
        public void CovarianceTest6()
        {
            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[] weights = { 1, 1, 1, 1, 1 };

            double[,] actual = Tools.WeightedCovariance(matrix, weights, 0);
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0001));

            matrix = matrix.Transpose();

            actual = Tools.WeightedCovariance(matrix, weights, 1);
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0001));
        }
Exemplo n.º 5
0
        public void VarianceTest7()
        {
            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[] weights = { 0.9, 0.9, 0.9, 0.9, 0.9 };

            double[] expected =
            {
                Tools.WeightedVariance(matrix.GetColumn(0), weights),
                Tools.WeightedVariance(matrix.GetColumn(1), weights),
                Tools.WeightedVariance(matrix.GetColumn(2), weights),
            };


            double[] actual = Tools.WeightedVariance(matrix, weights);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
        }
Exemplo n.º 6
0
        public void VarianceTest8()
        {
            double[,] matrix =
            {
                { 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 }
            };

            double[] weights = { 0.9, 0.9, 0.9, 0.9, 0.9 };

            double[] expected =
            {
                Tools.WeightedVariance(matrix.GetColumn(0), weights, WeightType.Automatic),
                Tools.WeightedVariance(matrix.GetColumn(1), weights, WeightType.Automatic),
                Tools.WeightedVariance(matrix.GetColumn(2), weights, WeightType.Automatic),
            };


            double[] actual = Tools.WeightedVariance(matrix, weights, WeightType.Automatic);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 1e-10));
        }
Exemplo n.º 7
0
        public void ExtendTest2()
        {
            int[][] data =
            {
                new int[] { 0, 2, 4 },
                new int[] { 1, 1, 2 },
            };

            int labelColumn    = 0;
            int positiveColumn = 1;
            int negativeColumn = 2;

            int[][] expected =
            {
                // For label 0
                new int[] { 0, 1 }, // Two positive cases
                new int[] { 0, 1 },

                new int[] { 0, 0 }, // Four negative cases
                new int[] { 0, 0 },
                new int[] { 0, 0 },
                new int[] { 0, 0 },

                // For label 1
                new int[] { 1, 1 }, // One positive case
                new int[] { 1, 0 }, // Three negative cases
                new int[] { 1, 0 },
            };

            int[][] actual = Tools.Expand(data, labelColumn, positiveColumn, negativeColumn);

            Assert.IsTrue(expected.IsEqual(actual));
        }
Exemplo n.º 8
0
        public void CorrelationTest()
        {
            // http://www.solvemymath.com/online_math_calculator/statistics/descriptive/correlation.php

            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 }
            };


            double[,] expected = new double[, ]
            {
                { 1.000000, 0.5669467, 0.533745 },
                { 0.566946, 1.0000000, 0.778127 },
                { 0.533745, 0.7781271, 1.000000 }
            };


            double[,] actual = Tools.Correlation(matrix);

            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.001));
        }
Exemplo n.º 9
0
        public void GroupTest()
        {
            int[][] data =
            {
                new int[] { 1, 1 },
                new int[] { 1, 1 },
                new int[] { 1, 1 },
                new int[] { 1, 1 },
                new int[] { 1, 0 },
                new int[] { 1, 0 },
                new int[] { 1, 0 },
                new int[] { 1, 0 },
                new int[] { 1, 0 },
                new int[] { 2, 1 },
                new int[] { 2, 0 },
                new int[] { 2, 0 },
                new int[] { 2, 0 },
            };

            int[][] expected =
            {
                new int[] { 1, 4, 5 },
                new int[] { 2, 1, 3 },
            };


            int[][] actual;
            actual = Tools.Group(data, 0, 1);

            Assert.IsTrue(Matrix.IsEqual(expected, actual));
        }
Exemplo n.º 10
0
        public void KurtosisMatrixTest2()
        {
            double[][] matrix =
            {
                new double[] { 1987 },
                new double[] { 1987 },
                new double[] { 1991 },
                new double[] { 1992 },
                new double[] { 1992 },
                new double[] { 1992 },
                new double[] { 1992 },
                new double[] { 1993 },
                new double[] { 1994 },
                new double[] { 1994 },
                new double[] { 1995 },
            };

            double expected = 0.4466489;
            {
                double[] actual = Tools.Kurtosis(matrix);
                Assert.AreEqual(expected, actual[0], 1e-7);
            }
            {
                double actual = Tools.Kurtosis(matrix.GetColumn(0));
                Assert.AreEqual(expected, actual, 1e-7);
            }
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        public void ExtendTest()
        {
            int[,] summary =
            {
                { 1, 4, 5 },
                { 2, 1, 3 },
            };

            int[] group     = summary.GetColumn(0);
            int[] positives = summary.GetColumn(1);
            int[] negatives = summary.GetColumn(2);

            int[][] expected =
            {
                new int[] { 1, 1 },
                new int[] { 1, 1 },
                new int[] { 1, 1 },
                new int[] { 1, 1 },
                new int[] { 1, 0 },
                new int[] { 1, 0 },
                new int[] { 1, 0 },
                new int[] { 1, 0 },
                new int[] { 1, 0 },
                new int[] { 2, 1 },
                new int[] { 2, 0 },
                new int[] { 2, 0 },
                new int[] { 2, 0 },
            };

            int[][] actual;
            actual = Tools.Expand(group, positives, negatives);
            Assert.IsTrue(Matrix.IsEqual(expected, actual));
        }
Exemplo n.º 13
0
        public void EntropyTest()
        {
            {
                int[] values  = { 0, 1 };
                int   classes = 2;

                double expected = 1;
                double actual   = Tools.Entropy(values, classes);

                Assert.AreEqual(expected, actual);
            }
            {
                int[] values  = { 0, 0 };
                int   classes = 2;

                double expected = 0;
                double actual   = Tools.Entropy(values, classes);

                Assert.AreEqual(expected, actual);
            }
            {
                int[] values  = { 1, 1 };
                int   classes = 2;

                double expected = 0;
                double actual   = Tools.Entropy(values, classes);

                Assert.AreEqual(expected, actual);
            }
        }
Exemplo n.º 14
0
        public void ModeTest1()
        {
            double[] values   = { 3, 3, 1, 4 };
            double   expected = 3;
            double   actual   = Tools.Mode(values);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 15
0
        public void SkewnessTest()
        {
            double[] values   = { 3.4, 7.1, 1.5, 8.6, 4.9 };
            double   expected = -0.0061643641512110154;
            double   actual   = Tools.Skewness(values);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 16
0
        public void MeanTest3()
        {
            double[] values   = { -5, 0.2, 2 };
            double   expected = -0.93333333333333324;
            double   actual   = Tools.Mean(values);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 17
0
        public void SkewnessTest()
        {
            double[] values   = { 3.4, 7.1, 1.5, 8.6, 4.9 };
            double   expected = -0.00861496080010664;
            double   actual   = Tools.Skewness(values, false);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 18
0
        public void ModeTest4()
        {
            int[] values   = { 2, 5, 1, 6, 4, 1, 2, 6, 2, 6, 8, 2, 6, 2, 2 };
            int   expected = 2;
            int   actual   = Tools.Mode(values);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 19
0
        public void SkewnessTest2()
        {
            Accord.Math.Random.Generator.Seed = 0;
            double[] values = Accord.Math.Vector.Random(10000000);
            double   actual = Tools.Skewness(values, true);

            Assert.AreEqual(0.00046129386638486271, actual);
        }
Exemplo n.º 20
0
        public void KurtosisTest2()
        {
            Accord.Math.Random.Generator.Seed = 0;
            double[] values = Accord.Math.Vector.Random(10000000);
            double   actual = Tools.Kurtosis(values, true);

            Assert.AreEqual(-1.1997826610738631, actual);
        }
Exemplo n.º 21
0
        public void StandardDeviationTest2()
        {
            double[] values   = { -5, 0.2, 2 };
            double   expected = 3.6350149013908224;
            double   actual   = Tools.StandardDeviation(values);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 22
0
        public void KurtosisTest()
        {
            double[] values   = { 3.4, 7.1, 1.5, 8.6, 4.9 };
            double   expected = 1.6572340828415202; // definition used by STATA
            double   actual   = Tools.Kurtosis(values, false) + 3;

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 23
0
        public void KurtosisTest()
        {
            double[] values   = { 3.4, 7.1, 1.5, 8.6, 4.9 };
            double   expected = -1.939370186981427;
            double   actual   = Tools.Kurtosis(values);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 24
0
        public void RankTest1()
        {
            double[] values = { 2, 3, 4, 4, 5, 6, 8, 10, 10, 14, 16, 20, 32, 40 };

            double[] expected = { 1, 2, 3.5, 3.5, 5, 6, 7, 8.5, 8.5, 10, 11, 12, 13, 14 };
            double[] actual   = Tools.Rank(values);

            Assert.IsTrue(expected.IsEqual(actual));
        }
Exemplo n.º 25
0
        public void ModeTest3()
        {
            double[] values = { 0, 1, 2, 2 };

            double expected = 2;
            double actual   = Tools.Mode(values);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 26
0
        public void QuartileTest2()
        {
            double[] values = new double[] { 1, 3, 3, 4, 5, 6, 6, 7, 8, 8 };
            double   q1, q3, actual;

            actual = Tools.Quartiles(values, out q1, out q3, false);
            Assert.AreEqual(3, q1);
            Assert.AreEqual(5.5, actual);
            Assert.AreEqual(7, q3);
        }
Exemplo n.º 27
0
        public void QuartileTest1()
        {
            double[] values = new double[] { 3, 4, 8 };
            double   q1, q3, actual;

            actual = Tools.Quartiles(values, out q1, out q3, false);
            Assert.AreEqual(3, q1);
            Assert.AreEqual(4, actual);
            Assert.AreEqual(8, q3);
        }
Exemplo n.º 28
0
        public void QuartileTest3()
        {
            double[] values = new double[] { 102, 104, 105, 107, 108, 109, 110, 112, 115, 116, 118 };
            double   q1, q3, actual;

            actual = Tools.Quartiles(values, out q1, out q3, false);
            Assert.AreEqual(105, q1);
            Assert.AreEqual(109, actual);
            Assert.AreEqual(115, q3);
        }
Exemplo n.º 29
0
        public void WeightedModeTest1()
        {
            double[] values  = { 0, 1, 1, 2 };
            double[] weights = { 50, 1, 1, 2 };

            double expected = 0;
            double actual   = Tools.WeightedMode(values, weights);

            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 30
0
        public void WeightedCountsModeTest2()
        {
            double[] values  = { 0, 1, 1, 2 };
            int[]    weights = { 0, 1, 1, 50 };

            double expected = 2;
            double actual   = Tools.WeightedMode(values, weights);

            Assert.AreEqual(expected, actual);
        }