Ejemplo n.º 1
0
        /// <summary>
        ///   Gets the Anderson-Darling statistic for the samples and target distribution.
        /// </summary>
        ///
        /// <param name="sortedSamples">The sorted samples.</param>
        /// <param name="distribution">The target distribution.</param>
        ///
        public static double GetStatistic(double[] sortedSamples, IUnivariateDistribution <double> distribution)
        {
            double N = sortedSamples.Length;
            double S = 0;
            int    n = sortedSamples.Length;

            // Finally, compute the test statistic.
            for (int i = 0; i < sortedSamples.Length; i++)
            {
                double a = 2.0 * (i + 1) - 1;
                double b = distribution.DistributionFunction(sortedSamples[i]);
                double c = distribution.ComplementaryDistributionFunction(sortedSamples[n - i - 1]);

                S += a * (Math.Log(b) + Math.Log(c));
            }

            return(-n - S / n);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Creates a new Anderson-Darling test.
        /// </summary>
        ///
        /// <param name="sample">The sample we would like to test as belonging to the <paramref name="hypothesizedDistribution"/>.</param>
        /// <param name="hypothesizedDistribution">A fully specified distribution.</param>
        ///
        public AndersonDarlingTest(double[] sample, IUnivariateDistribution hypothesizedDistribution)
        {
            double N = sample.Length;

            // Create the test statistic distribution with given degrees of freedom

            this.TheoreticalDistribution = hypothesizedDistribution;
            if (hypothesizedDistribution is UniformContinuousDistribution)
            {
                StatisticDistribution = new AndersonDarlingDistribution(AndersonDarlingDistributionType.Uniform, sample.Length);
            }
            else if (hypothesizedDistribution is NormalDistribution)
            {
                StatisticDistribution = new AndersonDarlingDistribution(AndersonDarlingDistributionType.Normal, sample.Length);
            }

            // Create a copy of the samples to prevent altering the
            // constructor's original arguments in the sorting step
            double[] Y = (double[])sample.Clone();

            // Sort sample
            Array.Sort(Y);

            // Create the theoretical and empirical distributions
            this.TheoreticalDistribution = hypothesizedDistribution;


            double S = 0;
            int    n = Y.Length;

            // Finally, compute the test statistic.
            for (int i = 0; i < Y.Length; i++)
            {
                double a = 2.0 * (i + 1) - 1;
                double b = TheoreticalDistribution.DistributionFunction(Y[i]);
                double c = TheoreticalDistribution.ComplementaryDistributionFunction(Y[n - i - 1]);

                S += a * (Math.Log(b) + Math.Log(c));
            }

            this.Statistic = -n - S / n;
            this.PValue    = StatisticToPValue(Statistic);
        }