Example #1
0
        public void AndersonDarlingConstructorTest2()
        {
            // Test against a Normal distribution

            // This time, let's see if the same sample from the previous example
            // could have originated from a standard Normal (Gaussian) distribution.
            //
            double[] sample =
            {
                0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382
            };

            NormalDistribution distribution = NormalDistribution.Estimate(sample);

            var adtest = new AndersonDarlingTest(sample, distribution);

            double statistic = adtest.Statistic;   // 0.1796
            double pvalue    = adtest.PValue;      // 0.8884

            bool significant = adtest.Significant; // false



            Assert.AreEqual(distribution, adtest.TheoreticalDistribution);
            Assert.AreEqual(DistributionTail.TwoTail, adtest.Tail);

            Assert.AreEqual(0.1796, adtest.Statistic, 1e-4);
            Assert.AreEqual(0.8884, adtest.PValue, 1e-4);
            Assert.IsFalse(Double.IsNaN(adtest.Statistic));

            Assert.IsFalse(adtest.Significant);
        }
Example #2
0
        public void GenerateRandomGeneValueDoesNotDepartFromUniformDistributionInLogSpace()
        {
            // Build up logarithmically spaced domain from e to e^10.
            double minimumExponent = 1;
            double maximumExponent = 10;
            var    domain          = new LogDomain(Math.Exp(minimumExponent), Math.Exp(maximumExponent));

            // Collect results of value generation.
            int numberRuns = 10000;

            double[] generatedValues = new double[numberRuns];
            for (int i = 0; i < numberRuns; i++)
            {
                Allele <double> generated = domain.GenerateRandomGeneValue();
                generatedValues[i] = Math.Log(generated.GetValue());
            }

            // Apply the Anderson-Darling test.
            AndersonDarlingTest uniformTest = new AndersonDarlingTest(
                generatedValues,
                new UniformContinuousDistribution(minimumExponent, maximumExponent));

            Assert.False(
                uniformTest.Significant,
                $"Random generation was found to be not uniform in log space by the Anderson-Darling test with significance level of {uniformTest.Size}.");
        }
        public void AndersonDarlingConstructorTest2()
        {
            #region doc_test_normal
            // Test against a Normal distribution

            // This time, let's see if the same sample from the previous example
            // could have originated from a standard Normal (Gaussian) distribution.
            //
            double[] sample =
            {
                0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382
            };

            // Let's estimate a new Normal distribution using the sample
            NormalDistribution distribution = NormalDistribution.Estimate(sample);

            // Now, we can create a new Anderson-Darling's test:
            var ad = new AndersonDarlingTest(sample, distribution);

            // We can then compute the test statistic,
            // the test p-value and its significance:
            double statistic   = ad.Statistic;   // 0.1796
            double pvalue      = ad.PValue;      // 0.8884
            bool   significant = ad.Significant; // false
            #endregion

            Assert.AreEqual(distribution, ad.TheoreticalDistribution);
            Assert.AreEqual(DistributionTail.TwoTail, ad.Tail);

            Assert.AreEqual(0.1796, ad.Statistic, 1e-4);
            Assert.AreEqual(0.8884, ad.PValue, 1e-4);
            Assert.IsFalse(Double.IsNaN(ad.Statistic));

            Assert.IsFalse(ad.Significant);
        }
        public void AndersonDarlingConstructorTest()
        {
            #region doc_test_uniform
            // Test against a standard Uniform distribution
            // References: http://www.math.nsysu.edu.tw/~lomn/homepage/class/92/kstest/kolmogorov.pdf

            // Suppose we got a new sample, and we would like to test whether this
            // sample seems to have originated from a uniform continuous distribution.
            //
            double[] sample =
            {
                0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382
            };

            // First, we create the distribution we would like to test against:
            //
            var distribution = UniformContinuousDistribution.Standard;

            // Now we can define our hypothesis. The null hypothesis is that the sample
            // comes from a standard uniform distribution, while the alternate is that
            // the sample is not from a standard uniform distribution.
            //
            var adtest = new AndersonDarlingTest(sample, distribution);

            double statistic = adtest.Statistic;   //  1.3891622091168489561
            double pvalue    = adtest.PValue;      // 0.2052

            bool significant = adtest.Significant; // false

            // Since the null hypothesis could not be rejected, then the sample
            // can perhaps be from a uniform distribution. However, please note
            // that this doesn't means that the sample *is* from the uniform, it
            // only means that we could not rule out the possibility.
            #endregion

            Assert.AreEqual(distribution, adtest.TheoreticalDistribution);
            Assert.AreEqual(DistributionTail.TwoTail, adtest.Tail);

            Assert.AreEqual(1.3891622091168489561, statistic, 1e-10);
            Assert.AreEqual(0.2052039626840637121, pvalue, 1e-10);
            Assert.IsFalse(Double.IsNaN(pvalue));

            // Tested against R's package ADGofTest

            /*
             *  X <- c( 0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382)
             *  ad.test(X)
             *
             *  Anderson-Darling GoF Test
             *
             *  data:  X
             *  AD = 1.3891999999999999904, p-value = 0.2052
             *  alternative hypothesis: NA
             */

            Assert.IsFalse(adtest.Significant);
        }
        public void AndersonDarlingConstructorTest()
        {
            // Test against a standard Uniform distribution
            // References: http://www.math.nsysu.edu.tw/~lomn/homepage/class/92/kstest/kolmogorov.pdf


            // Suppose we got a new sample, and we would like to test whether this
            // sample seems to have originated from a uniform continuous distribution.
            //
            double[] sample = 
            { 
                0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382
            };

            // First, we create the distribution we would like to test against:
            //
            var distribution = UniformContinuousDistribution.Standard;

            // Now we can define our hypothesis. The null hypothesis is that the sample
            // comes from a standard uniform distribution, while the alternate is that
            // the sample is not from a standard uniform distribution.
            //
            var adtest = new AndersonDarlingTest(sample, distribution);

            double statistic = adtest.Statistic; //  1.3891622091168489561
            double pvalue = adtest.PValue; // 0.2052

            bool significant = adtest.Significant; // false

            // Since the null hypothesis could not be rejected, then the sample
            // can perhaps be from a uniform distribution. However, please note
            // that this doesn't means that the sample *is* from the uniform, it
            // only means that we could not rule out the possibility.

            Assert.AreEqual(distribution, adtest.TheoreticalDistribution);
            Assert.AreEqual(DistributionTail.TwoTail, adtest.Tail);

            Assert.AreEqual(1.3891622091168489561, statistic, 1e-10);
            Assert.AreEqual(0.2052039626840637121, pvalue, 1e-10);
            Assert.IsFalse(Double.IsNaN(pvalue));

            // Tested against R's package ADGofTest

            /* 
                X <- c( 0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382)
                ad.test(X)

                Anderson-Darling GoF Test

                data:  X 
                AD = 1.3891999999999999904, p-value = 0.2052
                alternative hypothesis: NA 
            */

            Assert.IsFalse(adtest.Significant);
        }
        public void GenerateRandomGeneValueDoesNotDepartFromUniformDistribution()
        {
            // Create domain.
            ContinuousDomain domain = new ContinuousDomain(ContinuousDomainTest.minimum, ContinuousDomainTest.maximum);

            // Collect results of value generation.
            double[] generatedValues = new double[ContinuousDomainTest.TriesForRandomTests];
            for (int i = 0; i < ContinuousDomainTest.TriesForRandomTests; i++)
            {
                Allele <double> generated = domain.GenerateRandomGeneValue();
                generatedValues[i] = generated.GetValue();
            }

            // Apply the Anderson-Darling test.
            AndersonDarlingTest uniformTest = new AndersonDarlingTest(generatedValues, new UniformContinuousDistribution(ContinuousDomainTest.minimum, ContinuousDomainTest.maximum));

            Assert.False(
                uniformTest.Significant,
                $"Random generation was found to be not uniform by the Anderson-Darling test with significance level of {uniformTest.Size}.");
        }
Example #7
0
        public void SampleFromCauchyDistributionDoesNotDepartFromCauchyDistribution()
        {
            // Fix the location and scale.
            double location = -3.3;
            double scale    = 0.22;

            // Collect a large set of samples.
            int numberRuns = 1000;

            double[] results = new double[numberRuns];
            for (int i = 0; i < numberRuns; i++)
            {
                results[i] = Randomizer.Instance.SampleFromCauchyDistribution(location, scale);
            }

            // Apply the Anderson-Darling test.
            AndersonDarlingTest cauchyTest = new AndersonDarlingTest(results, new CauchyDistribution(location, scale));

            Assert.False(
                cauchyTest.Significant,
                $"Cauchy sampling was found not to be correctly distributed by the Anderson-Darling test with significance level of {cauchyTest.Size}.");
        }
Example #8
0
        public void SampleFromUniformDistributionDoesNotDepartFromUniformDistribution()
        {
            // Fix the minimum and maximum values.
            double minimum = -3.3;
            double maximum = double.MaxValue;

            // Collect a large set of samples.
            int numberRuns = 1000;

            double[] results = new double[numberRuns];
            for (int i = 0; i < numberRuns; i++)
            {
                results[i] = Randomizer.Instance.SampleFromUniformDistribution(minimum, maximum);
            }

            // Apply the Anderson-Darling test.
            AndersonDarlingTest uniformTest = new AndersonDarlingTest(results, new UniformContinuousDistribution(minimum, maximum));

            Assert.False(
                uniformTest.Significant,
                $"Random uniform sampling was found to be not uniform by the Anderson-Darling test with significance level of {uniformTest.Size}.");
        }
        public void AndersonDarlingConstructorTest2()
        {
            // Test against a Normal distribution

            // This time, let's see if the same sample from the previous example
            // could have originated from a standard Normal (Gaussian) distribution.
            //
            double[] sample =
            { 
                0.621, 0.503, 0.203, 0.477, 0.710, 0.581, 0.329, 0.480, 0.554, 0.382
            };

            NormalDistribution distribution = NormalDistribution.Estimate(sample);

            var adtest = new AndersonDarlingTest(sample, distribution);

            double statistic = adtest.Statistic; // 0.1796
            double pvalue = adtest.PValue; // 0.8884

            bool significant = adtest.Significant; // false

         

            Assert.AreEqual(distribution, adtest.TheoreticalDistribution);
            Assert.AreEqual(DistributionTail.TwoTail, adtest.Tail);

            Assert.AreEqual(0.1796, adtest.Statistic, 1e-4);
            Assert.AreEqual(0.8884, adtest.PValue, 1e-4);
            Assert.IsFalse(Double.IsNaN(adtest.Statistic));

            Assert.IsFalse(adtest.Significant);
        }
Example #10
0
        /// <summary>
        ///   Computes the analysis.
        /// </summary>
        ///
        public void Compute()
        {
            bool[] fail = new bool[Distributions.Length];

            // Step 1. Fit all candidate distributions to the data.
            for (int i = 0; i < Distributions.Length; i++)
            {
                var distribution = Distributions[i];

                try
                {
                    distribution.Fit(data);
                }
                catch
                {
                    // TODO: Maybe revisit the decision to swallow exceptions here.
                    fail[i] = true;
                }
            }

            // Step 2. Use statistical tests to see how well each
            //         distribution was able to model the data.

            KolmogorovSmirnov = new KolmogorovSmirnovTest[Distributions.Length];
            ChiSquare         = new ChiSquareTest[Distributions.Length];
            AndersonDarling   = new AndersonDarlingTest[Distributions.Length];
            DistributionNames = new string[Distributions.Length];

            double[] ks = new double[Distributions.Length];
            double[] cs = new double[Distributions.Length];
            double[] ad = new double[Distributions.Length];

            var measures = new List <GoodnessOfFit>();

            for (int i = 0; i < Distributions.Length; i++)
            {
                ks[i] = Double.NegativeInfinity;
                cs[i] = Double.NegativeInfinity;
                ad[i] = Double.NegativeInfinity;

                var d = this.Distributions[i] as IUnivariateDistribution;

                if (d == null || fail[i])
                {
                    continue;
                }

                this.DistributionNames[i] = GetName(d.GetType());

                int ms = 5000;

                run(() =>
                {
                    this.KolmogorovSmirnov[i] = new KolmogorovSmirnovTest(data, d);
                    ks[i] = -KolmogorovSmirnov[i].Statistic;
                }, ms);

                run(() =>
                {
                    this.ChiSquare[i] = new ChiSquareTest(data, d);
                    cs[i]             = -ChiSquare[i].Statistic;
                }, ms);

                run(() =>
                {
                    this.AndersonDarling[i] = new AndersonDarlingTest(data, d);
                    ad[i] = AndersonDarling[i].Statistic;
                }, ms);

                if (Double.IsNaN(ks[i]))
                {
                    ks[i] = Double.NegativeInfinity;
                }

                if (Double.IsNaN(cs[i]))
                {
                    cs[i] = Double.NegativeInfinity;
                }

                if (Double.IsNaN(ad[i]))
                {
                    ad[i] = Double.NegativeInfinity;
                }

                measures.Add(new GoodnessOfFit(this, i));
            }

            this.KolmogorovSmirnovRank = getRank(ks);
            this.ChiSquareRank         = getRank(cs);
            this.AndersonDarlingRank   = getRank(ad);

            measures.Sort();

            this.GoodnessOfFit = new GoodnessOfFitCollection(measures);
        }