Exemplo n.º 1
0
        public void WeightedEmpiricalDistributionConstructorTest()
        {
            double[] original     = { 5, 5, 1, 4, 1, 2, 2, 3, 3, 3, 4, 3, 3, 3, 4, 3, 2, 3 };
            var      distribution = new EmpiricalDistribution(original);

            int[]    weights = { 2, 1, 1, 1, 2, 3, 1, 3, 1, 1, 1, 1 };
            double[] samples = { 5, 1, 4, 1, 2, 3, 4, 3, 4, 3, 2, 3 };
            var      target  = new EmpiricalDistribution(samples, weights);

            Assert.AreEqual(distribution.Entropy, target.Entropy, 1e-10);
            Assert.AreEqual(distribution.Mean, target.Mean);
            Assert.AreEqual(distribution.Median, target.Median);
            Assert.AreEqual(distribution.Mode, target.Mode);
            Assert.AreEqual(distribution.Quartiles.Min, target.Quartiles.Min);
            Assert.AreEqual(distribution.Quartiles.Max, target.Quartiles.Max);
            Assert.AreEqual(distribution.Smoothing, target.Smoothing);
            Assert.AreEqual(distribution.StandardDeviation, target.StandardDeviation);
            Assert.AreEqual(distribution.Support.Min, target.Support.Min);
            Assert.AreEqual(distribution.Support.Max, target.Support.Max);
            Assert.AreEqual(distribution.Variance, target.Variance);
            Assert.IsTrue(target.Weights.IsEqual(weights.Divide(weights.Sum())));
            Assert.AreEqual(target.Samples, samples);

            for (double x = 0; x < 6; x += 0.1)
            {
                double actual, expected;
                expected = distribution.ComplementaryDistributionFunction(x);
                actual   = target.ComplementaryDistributionFunction(x);
                Assert.AreEqual(expected, actual);

                expected = distribution.CumulativeHazardFunction(x);
                actual   = target.CumulativeHazardFunction(x);
                Assert.AreEqual(expected, actual);

                expected = distribution.DistributionFunction(x);
                actual   = target.DistributionFunction(x);
                Assert.AreEqual(expected, actual);

                expected = distribution.HazardFunction(x);
                actual   = target.HazardFunction(x);
                Assert.AreEqual(expected, actual, 1e-15);

                expected = distribution.InverseDistributionFunction(Accord.Math.Tools.Scale(0, 6, 0, 1, x));
                actual   = target.InverseDistributionFunction(Accord.Math.Tools.Scale(0, 6, 0, 1, x));
                Assert.AreEqual(expected, actual);

                expected = distribution.LogProbabilityDensityFunction(x);
                actual   = target.LogProbabilityDensityFunction(x);
                Assert.AreEqual(expected, actual, 1e-15);

                expected = distribution.ProbabilityDensityFunction(x);
                actual   = target.ProbabilityDensityFunction(x);
                Assert.AreEqual(expected, actual, 1e-15);

                expected = distribution.QuantileDensityFunction(Accord.Math.Tools.Scale(0, 6, 0, 1, x));
                actual   = target.QuantileDensityFunction(Accord.Math.Tools.Scale(0, 6, 0, 1, x));
                Assert.AreEqual(expected, actual, 1e-10);
            }
        }