Esempio n. 1
0
        public void DistributionFunctionTest()
        {
            // http://www.stat.yale.edu/Courses/1997-98/101/binom.htm
            // Verified in http://stattrek.com/online-calculator/binomial.aspx

            double[] pmf             = { 0.0260838446329553, 0.10433562893683, 0.198238170750635, 0.237886375826923, 0.202203904741285 };
            double[] cdfLess         = { 0.0000000000000000, 0.0260838446329553, 0.130419473569785, 0.32865764432042, 0.566544020147343 };
            double[] cdfLessEqual    = { 0.0260838446329553, 0.130419473569785, 0.32865764432042, 0.566544020147343, 0.768747924888628 };
            double[] cdfGreater      = { 0.973916155367045, 0.869580526430215, 0.67134235567958, 0.433455979852657, 0.231252075111372 };
            double[] cdfGreaterEqual = { 1.000000000000000, 0.973916155367045, 0.869580526430215, 0.67134235567958, 0.433455979852657 };

            int    trials               = 20;
            double probability          = 0.166667;
            BinomialDistribution target = new BinomialDistribution(trials, probability);

            for (int i = 0; i < pmf.Length; i++)
            {
                {   // P(X = i)
                    double actual = target.ProbabilityMassFunction(i);
                    Assert.AreEqual(pmf[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

                {   // P(X <= i)
                    double actual = target.DistributionFunction(i);
                    Assert.AreEqual(cdfLessEqual[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

                {   // P(X < i)
                    double actual = target.DistributionFunction(i, inclusive: false);
                    Assert.AreEqual(cdfLess[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

                {   // P(X > i)
                    double actual = target.ComplementaryDistributionFunction(i);
                    Assert.AreEqual(cdfGreater[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

                {   // P(X >= i)
                    double actual = target.ComplementaryDistributionFunction(i, inclusive: true);
                    Assert.AreEqual(cdfGreaterEqual[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }
            }
        }
        public void ConstructorTest()
        {
            var bin = new BinomialDistribution(trials: 16, probability: 0.12);

            double mean   = bin.Mean;                                   // 1.92
            double median = bin.Median;                                 // 2
            double var    = bin.Variance;                               // 1.6896
            double mode   = bin.Mode;                                   // 2

            double cdf  = bin.DistributionFunction(k: 0);               // 0.12933699143209909
            double pdf  = bin.ProbabilityMassFunction(k: 1);            // 0.28218979948821621
            double lpdf = bin.LogProbabilityMassFunction(k: 0);         // -2.0453339441581582

            double ccdf  = bin.ComplementaryDistributionFunction(k: 0); // 0.87066300856790091
            int    icdf0 = bin.InverseDistributionFunction(p: 0.37);    // 1
            int    icdf1 = bin.InverseDistributionFunction(p: 0.50);    // 2
            int    icdf2 = bin.InverseDistributionFunction(p: 0.99);    // 5
            int    icdf3 = bin.InverseDistributionFunction(p: 0.999);   // 7

            double hf  = bin.HazardFunction(x: 0);                      // 1.3809523809523814
            double chf = bin.CumulativeHazardFunction(x: 0);            // 0.86750056770472328

            string str = bin.ToString(CultureInfo.InvariantCulture);    // "Binomial(x; n = 16, p = 0.12)"

            double[] probabilities = new double[10];
            for (int i = 0; i < probabilities.Length; i++)
            {
                probabilities[i] = bin.ProbabilityMassFunction(i);
            }

            Assert.AreEqual(1.92, mean);
            Assert.AreEqual(2, median);
            Assert.AreEqual(2, mode);
            Assert.AreEqual(1.6896, var);
            Assert.AreEqual(0.13850027875444251, chf, 1e-10);
            Assert.AreEqual(0.12933699143209909, cdf, 1e-10);
            Assert.AreEqual(0.28218979948821621, pdf, 1e-10);
            Assert.AreEqual(-2.0453339441581582, lpdf);
            Assert.AreEqual(0.14855000173354949, hf, 1e-10);
            Assert.AreEqual(0.87066300856790091, ccdf, 1e-10);
            Assert.AreEqual(1, icdf0);
            Assert.AreEqual(2, icdf1);
            Assert.AreEqual(5, icdf2);
            Assert.AreEqual(7, icdf3);
            Assert.AreEqual("Binomial(x; n = 16, p = 0.12)", str);



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

            Assert.AreEqual(0.0, range1.Min);
            Assert.AreEqual(4.0, range1.Max);
            Assert.AreEqual(0.0, range2.Min);
            Assert.AreEqual(5.0, range2.Max);
            Assert.AreEqual(0.0, range3.Min);
            Assert.AreEqual(5.0, range3.Max);
        }
        public void ConstructorTest()
        {
            var bin = new BinomialDistribution(trials: 16, probability: 0.12);

            double mean = bin.Mean;     // 1.92
            double median = bin.Median; // 2
            double var = bin.Variance;  // 1.6896
            double mode = bin.Mode;     // 2

            double cdf = bin.DistributionFunction(k: 0);    // 0.12933699143209909
            double pdf = bin.ProbabilityMassFunction(k: 1); // 0.28218979948821621
            double lpdf = bin.LogProbabilityMassFunction(k: 0); // -2.0453339441581582

            double ccdf = bin.ComplementaryDistributionFunction(k: 0); // 0.87066300856790091
            int icdf0 = bin.InverseDistributionFunction(p: 0.37); // 1
            int icdf1 = bin.InverseDistributionFunction(p: 0.50); // 2
            int icdf2 = bin.InverseDistributionFunction(p: 0.99); // 5
            int icdf3 = bin.InverseDistributionFunction(p: 0.999); // 7

            double hf = bin.HazardFunction(x: 0); // 1.3809523809523814
            double chf = bin.CumulativeHazardFunction(x: 0); // 0.86750056770472328

            string str = bin.ToString(CultureInfo.InvariantCulture); // "Binomial(x; n = 16, p = 0.12)"

            double[] probabilities = new double[10];
            for (int i = 0; i < probabilities.Length; i++)
                probabilities[i] = bin.ProbabilityMassFunction(i);

            Assert.AreEqual(1.92, mean);
            Assert.AreEqual(2, median);
            Assert.AreEqual(2, mode);
            Assert.AreEqual(1.6896, var);
            Assert.AreEqual(0.13850027875444251, chf, 1e-10);
            Assert.AreEqual(0.12933699143209909, cdf, 1e-10);
            Assert.AreEqual(0.28218979948821621, pdf, 1e-10);
            Assert.AreEqual(-2.0453339441581582, lpdf);
            Assert.AreEqual(0.14855000173354949, hf, 1e-10);
            Assert.AreEqual(0.87066300856790091, ccdf, 1e-10);
            Assert.AreEqual(1, icdf0);
            Assert.AreEqual(2, icdf1);
            Assert.AreEqual(5, icdf2);
            Assert.AreEqual(7, icdf3);
            Assert.AreEqual("Binomial(x; n = 16, p = 0.12)", str);



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

            Assert.AreEqual(0.0, range1.Min);
            Assert.AreEqual(4.0, range1.Max);
            Assert.AreEqual(0.0, range2.Min);
            Assert.AreEqual(5.0, range2.Max);
            Assert.AreEqual(0.0, range3.Min);
            Assert.AreEqual(5.0, range3.Max);
        }
        public void DistributionFunctionTest()
        {
            // http://www.stat.yale.edu/Courses/1997-98/101/binom.htm
            // Verified in http://stattrek.com/online-calculator/binomial.aspx

            double[] pmf = { 0.0260838446329553, 0.10433562893683, 0.198238170750635, 0.237886375826923, 0.202203904741285 };
            double[] cdfLess = { 0.0000000000000000, 0.0260838446329553, 0.130419473569785, 0.32865764432042, 0.566544020147343 };
            double[] cdfLessEqual = { 0.0260838446329553, 0.130419473569785, 0.32865764432042, 0.566544020147343, 0.768747924888628 };
            double[] cdfGreater = { 0.973916155367045, 0.869580526430215, 0.67134235567958, 0.433455979852657, 0.231252075111372 };
            double[] cdfGreaterEqual = { 1.000000000000000, 0.973916155367045, 0.869580526430215, 0.67134235567958, 0.433455979852657 };

            int trials = 20;
            double probability = 0.166667;
            BinomialDistribution target = new BinomialDistribution(trials, probability);

            for (int i = 0; i < pmf.Length; i++)
            {
                {   // P(X = i)
                    double actual = target.ProbabilityMassFunction(i);
                    Assert.AreEqual(pmf[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

                {   // P(X <= i)
                    double actual = target.DistributionFunction(i);
                    Assert.AreEqual(cdfLessEqual[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

                {   // P(X < i)
                    double actual = target.DistributionFunction(i, inclusive: false);
                    Assert.AreEqual(cdfLess[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

                {   // P(X > i)
                    double actual = target.ComplementaryDistributionFunction(i);
                    Assert.AreEqual(cdfGreater[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

                {   // P(X >= i)
                    double actual = target.ComplementaryDistributionFunction(i, inclusive: true);
                    Assert.AreEqual(cdfGreaterEqual[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }
            }
        }