Beispiel #1
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));
        }
Beispiel #2
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));
        }
Beispiel #3
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),
                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));
        }
Beispiel #4
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));
        }
Beispiel #5
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));
        }
Beispiel #6
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);
            }
        }
Beispiel #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));
        }
Beispiel #8
0
        public void KurtosisTest2()
        {
            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);
            }
        }
Beispiel #9
0
        public void SkewnessTest2()
        {
            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);
            }
        }
Beispiel #10
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));
        }
        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, 0.01));

            double[] expectedY = { 0.67, 0.13, 0.17, 0.00 };
            double[] actualY   = pls.Dependents.FactorProportions;
            Assert.IsTrue(expectedY.IsEqual(actualY, 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.Multiply(weights);
            Assert.IsTrue(expected.IsEqual(actual, 0.01));
        }
Beispiel #12
0
        public void ModeTest1()
        {
            double[] values   = { 3, 3, 1, 4 };
            double   expected = 3;
            double   actual   = Tools.Mode(values);

            Assert.AreEqual(expected, actual);
        }
Beispiel #13
0
        public void MeanTest3()
        {
            double[] values   = { -5, 0.2, 2 };
            double   expected = -0.93333333333333324;
            double   actual   = Tools.Mean(values);

            Assert.AreEqual(expected, actual);
        }
Beispiel #14
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);
        }
Beispiel #15
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);
        }
Beispiel #16
0
        public void StandardDeviationTest2()
        {
            double[] values   = { -5, 0.2, 2 };
            double   expected = 3.6350149013908224;
            double   actual   = Tools.StandardDeviation(values);

            Assert.AreEqual(expected, actual);
        }
Beispiel #17
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);
        }
Beispiel #18
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);
        }
Beispiel #19
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);
        }
Beispiel #20
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);
        }
Beispiel #21
0
        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);
        }
Beispiel #22
0
        public void ShuffleTest()
        {
            int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

            Tools.Shuffle(array);

            for (int i = 0; i < 10; i++)
            {
                Assert.IsTrue(array.Contains(i));
            }
        }
        /// <summary>
        ///   Gets the Silverman's rule. estimative of the smoothing parameter.
        ///   This is the default smoothing rule applied used when estimating
        ///   <see cref="MultivariateEmpiricalDistribution"/>s.
        /// </summary>
        ///
        /// <remarks>
        ///   This method is described on Wikipedia, at
        ///   http://en.wikipedia.org/wiki/Multivariate_kernel_density_estimation
        /// </remarks>
        ///
        /// <param name="observations">The observations for the empirical distribution.</param>
        /// <param name="weights">The number of times each sample should be repeated.</param>
        ///
        /// <returns>An estimative of the smoothing parameter.</returns>
        ///
        public static double[,] SilvermanRule(double[][] observations, int[] weights)
        {
            // Silverman's rule
            //  - http://en.wikipedia.org/wiki/Multivariate_kernel_density_estimation

            double[] sigma = Tools.WeightedStandardDeviation(observations, weights);

            double d = sigma.Length;
            double n = weights.Sum();

            return(silverman(sigma, d, n));
        }
Beispiel #24
0
        public void ModeTest()
        {
            double[,] matrix =
            {
                { 3, 3, 1, 4 },
                { 0, 1, 1, 1 },
                { 0, 1, 2, 4 },
            };


            double[] expected = { 0, 1, 1, 4 };
            double[] actual   = Tools.Mode(matrix);
            Assert.IsTrue(expected.IsEqual(actual));
        }
Beispiel #25
0
        public void MeanTest4()
        {
            double[,] matrix =
            {
                { 2, -1.0, 5 },
                { 7,  0.5, 9 },
            };

            double[] sums = Matrix.Sum(matrix);
            Assert.IsTrue(sums.IsEqual(9.0, -0.5, 14.0));

            double[] expected = { 4.5000, -0.2500, 7.0000 };
            double[] actual   = Tools.Mean(matrix, sums);

            Assert.IsTrue(expected.IsEqual(actual));
        }
Beispiel #26
0
        public void MedianTest1()
        {
            double[] values;
            double   expected;
            double   actual;

            values   = new double[] { -5, 0.2, 2, 5, -0.7 };
            expected = 0.2;
            actual   = Tools.Median(values, false);
            Assert.AreEqual(expected, actual);

            values   = new double[] { -5, 0.2, 2, 5 };
            expected = 1.1;
            actual   = Tools.Median(values, false);
            Assert.AreEqual(expected, actual);
        }
Beispiel #27
0
        public void PooledVarianceTest()
        {
            double[][] samples =
            {
                new double[] { 31, 30, 29 },
                new double[] { 42, 41,40, 39 },
                new double[] { 31,28 },
                new double[] { 23, 22,21, 19, 18 },
                new double[] { 21, 20,19, 18, 17 },
            };

            double expected = 2.7642857142857147;
            double actual   = Tools.PooledVariance(samples);

            Assert.AreEqual(expected, actual);
        }
Beispiel #28
0
        public void PooledStandardDeviationTest()
        {
            double[][] samples =
            {
                new double[] { 31, 30, 29 },
                new double[] { 42, 41,40, 39 },
                new double[] { 31,28 },
                new double[] { 23, 22,21, 19, 18 },
                new double[] { 21, 20,19, 18, 17 },
            };

            double expected = System.Math.Sqrt(2.7642857142857147);
            double actual   = Tools.PooledStandardDeviation(samples);

            Assert.AreEqual(expected, actual);
        }
Beispiel #29
0
        public void ModeTest3()
        {
            double[][] matrix =
            {
                new double[] { 6, 4, 9 },
                new double[] { 3, 1, 3 },
                new double[] { 1, 3, 8 },
                new double[] { 1, 5, 4 },
                new double[] { 7, 4, 1 },
                new double[] { 4, 4, 3 },
            };

            double[] expected = { 1, 4, 3 };
            double[] actual   = Tools.Mode(matrix);

            Assert.AreEqual(expected[0], actual[0]);
            Assert.AreEqual(expected[1], actual[1]);
            Assert.AreEqual(expected[2], actual[2]);
        }
Beispiel #30
0
        public void ProportionsTest()
        {
            int[,] summary =
            {
                { 1, 4, 5 },
                { 2, 1, 3 },
            };

            double[,] probabilities =
            {
                { 1, 4.0 / (4 + 5) },
                { 2, 1.0 / (1 + 3) },
            };

            int[]    positives = summary.GetColumn(1);
            int[]    negatives = summary.GetColumn(2);
            double[] expected  = probabilities.GetColumn(1);
            double[] actual;

            actual = Tools.Proportions(positives, negatives);
            Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.000000001));
        }