Inheritance: UnivariateContinuousDistribution
Beispiel #1
0
        private double complementaryDistributionFunction(double x)
        {
            if (exact)
            {
                // For small samples (< 30) and if there are not very large
                // differences in samples sizes, this distribution is exact.
                return(WilcoxonDistribution.exactComplement(x, table));
            }

            if (Correction == ContinuityCorrection.Midpoint)
            {
                if (x > Mean)
                {
                    x = x - 0.5;
                }
                else
                {
                    x = x + 0.5;
                }
            }
            else if (Correction == ContinuityCorrection.KeepInside)
            {
                x = x - 0.5;
            }

            return(approximation.ComplementaryDistributionFunction(x));
        }
        public void ConstructorTest()
        {
            double[] ranks = { 1, 2, 3, 4, 5.5, 5.5, 7, 8, 9, 10, 11, 12 };

            var W = new WilcoxonDistribution(ranks);

            double mean = W.Mean;     // 39.0
            double median = W.Median; // 38.5
            double var = W.Variance;  // 162.5

            double cdf = W.DistributionFunction(w: 42); // 0.60817384423279575
            double pdf = W.ProbabilityDensityFunction(w: 42); // 0.38418508862319295
            double lpdf = W.LogProbabilityDensityFunction(w: 42); // 0.38418508862319295

            double ccdf = W.ComplementaryDistributionFunction(x: 42); // 0.39182615576720425
            double icdf = W.InverseDistributionFunction(p: cdf); // 42
            double icdf2 = W.InverseDistributionFunction(p: 0.5); // 42

            double hf = W.HazardFunction(x: 42); // 0.98049883339449373
            double chf = W.CumulativeHazardFunction(x: 42); // 0.936937017743799

            string str = W.ToString(); // "W+(x; R)"

            Assert.AreEqual(39.0, mean);
            Assert.AreEqual(38.5, median, 1e-6);
            Assert.AreEqual(162.5, var);
            Assert.AreEqual(0.936937017743799, chf);
            Assert.AreEqual(0.60817384423279575, cdf);
            Assert.AreEqual(0.38418508862319295, pdf);
            Assert.AreEqual(-0.95663084089698047, lpdf);
            Assert.AreEqual(0.98049883339449373, hf);
            Assert.AreEqual(0.39182615576720425, ccdf);
            Assert.AreEqual(42, icdf, 1e-6);
            Assert.AreEqual("W+(x; R)", str);
        }
Beispiel #3
0
        /// <summary>
        ///   Gets the log-probability density function (pdf) for
        ///   this distribution evaluated at point <c>x</c>.
        /// </summary>
        ///
        /// <param name="x">A single point in the distribution range.</param>
        ///
        /// <returns>
        ///   The logarithm of the probability of <c>u</c>
        ///   occurring in the current distribution.
        /// </returns>
        ///
        /// <remarks>
        ///   The Probability Density Function (PDF) describes the
        ///   probability that a given value <c>u</c> will occur.
        /// </remarks>
        ///
        /// <example>
        ///   See <see cref="MannWhitneyDistribution"/>.
        /// </example>
        ///
        protected internal override double InnerLogProbabilityDensityFunction(double x)
        {
            if (exact)
            {
                return(Math.Log(WilcoxonDistribution.count(x, table)) - Math.Log(table.Length));
            }

            return(approximation.ProbabilityDensityFunction(x));
        }
Beispiel #4
0
        /// <summary>
        ///   Gets the probability density function (pdf) for
        ///   this distribution evaluated at point <c>u</c>.
        /// </summary>
        ///
        /// <param name="x">A single point in the distribution range.</param>
        ///
        /// <returns>
        ///   The probability of <c>u</c> occurring
        ///   in the current distribution.
        /// </returns>
        ///
        /// <remarks>
        ///   The Probability Density Function (PDF) describes the
        ///   probability that a given value <c>u</c> will occur.
        /// </remarks>
        ///
        /// <example>
        ///   See <see cref="MannWhitneyDistribution"/>.
        /// </example>
        ///
        protected internal override double InnerProbabilityDensityFunction(double x)
        {
            if (this.exact)
            {
                return(WilcoxonDistribution.count(x, table) / (double)table.Length);
            }

            return(approximation.ProbabilityDensityFunction(x));
        }
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        ///
        public override object Clone()
        {
            var clone = new WilcoxonDistribution(n);

            clone.exact         = exact;
            clone.table         = table;
            clone.n             = n;
            clone.approximation = (NormalDistribution)approximation.Clone();
            return(clone);
        }
        public void ConstructorTest2()
        {
            double[] ranks = { 1, 2, 3, 4, 5.5, 5.5, 7, 8, 9, 10, 11, 12 };

            var W = new WilcoxonDistribution(ranks, forceExact: true);

            double mean = W.Mean;     // 39
            double median = W.Median; // 39
            double var = W.Variance;  // 162.5

            double cdf = W.DistributionFunction(w: 42); // 0.582763671875
            double pdf = W.ProbabilityDensityFunction(w: 42); // 0.014404296875
            double lpdf = W.LogProbabilityDensityFunction(w: 42); // -4.2402287228136233

            double ccdf = W.ComplementaryDistributionFunction(x: 42); // 0.417236328125
            double icdf = W.InverseDistributionFunction(p: cdf); // 41.965447500067114
            double icdf2 = W.InverseDistributionFunction(p: 0.5); // 39.000000487005138

            double hf = W.HazardFunction(x: 42); // 0.03452311293153891
            double chf = W.CumulativeHazardFunction(x: 42); // 0.87410248360375287

            string str = W.ToString(); // "W+(x; R)"

            Assert.AreEqual(39.0, mean);
            Assert.AreEqual(39.0, median, 1e-6);
            Assert.AreEqual(162.5, var);
            Assert.AreEqual(0.87410248360375287, chf);
            Assert.AreEqual(0.582763671875, cdf);
            Assert.AreEqual(0.014404296875, pdf);
            Assert.AreEqual(-4.2402287228136233, lpdf);
            Assert.AreEqual(0.03452311293153891, hf);
            Assert.AreEqual(0.417236328125, ccdf);
            Assert.AreEqual(42, icdf, 0.05);
            Assert.AreEqual("W+(x; R)", str);

            var range1 = W.GetRange(0.95);
            var range2 = W.GetRange(0.99);
            var range3 = W.GetRange(0.01);

            Assert.AreEqual(17.999999736111114, range1.Min);
            Assert.AreEqual(60.000000315408002, range1.Max);
            Assert.AreEqual(10.000000351098127, range2.Min);
            Assert.AreEqual(67.99999981945885, range2.Max);
            Assert.AreEqual(10.000000351098119, range3.Min);
            Assert.AreEqual(67.99999981945885, range3.Max);
        }
        public void MedianTest()
        {
            double[] ranks = { 1, 2, 3, 7 };

            WilcoxonDistribution target = new WilcoxonDistribution(ranks);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5));
        }
        public void CumulativeExactTest()
        {
            // example from https://onlinecourses.science.psu.edu/stat414/node/319

            double[] ranks = 
            {
                22, 2, 13, 24, 16, 15, 25, 10, 9, 11, 5, 
                17, 12, 20, 14, 30, 8, 6, 26, 19, 29, 27, 3, 28,
                7, 21, 23, 1, 18, 4
            };

            WilcoxonDistribution target = new WilcoxonDistribution(ranks);

            Assert.AreEqual(232.5, target.Mean);
            Assert.AreEqual(2363.75, target.Variance);
            Assert.AreEqual(Math.Sqrt(2363.75), target.StandardDeviation);

            double actual = target.DistributionFunction(200);
            double expected = 0.2546;
            Assert.AreEqual(expected, actual, 1e-2);

            double inv = target.InverseDistributionFunction(actual);

            Assert.AreEqual(200, inv);
        }
        public void CumulativeTest()
        {
            // Example from https://onlinecourses.science.psu.edu/stat414/node/319

            double[] ranks = { 1, 2, 3 };

            WilcoxonDistribution target = new WilcoxonDistribution(ranks);

            double[] probabilities = { 0.0, 1 / 8.0, 1 / 8.0, 1 / 8.0, 2 / 8.0, 1 / 8.0, 1 / 8.0, 1 / 8.0 };
            double[] expected = Accord.Math.Matrix.CumulativeSum(probabilities);

            for (int i = 0; i < expected.Length; i++)
            {
                // P(W<=i)
                double actual = target.DistributionFunction(i);
                Assert.AreEqual(expected[i], actual);
            }
        }
        public void ProbabilityTest()
        {
            // Example from https://onlinecourses.science.psu.edu/stat414/node/319

            double[] ranks = { 1, 2, 3 };

            WilcoxonDistribution target = new WilcoxonDistribution(ranks);

            double[] expected = { 1 / 8.0, 1 / 8.0, 1 / 8.0, 2 / 8.0, 1 / 8.0, 1 / 8.0, 1 / 8.0 };

            for (int i = 0; i < expected.Length; i++)
            {
                // P(W=i)
                double actual = target.ProbabilityDensityFunction(i);
                Assert.AreEqual(expected[i], actual);
            }
        }