Ejemplo n.º 1
0
        protected override void EndProcessing()
        {
            var dist = new HypergeometricDistribution(PopulationSize, Successes, Samples);
            var obj  = DistributionHelper.AddConvinienceMethods(dist);

            WriteObject(obj);
        }
Ejemplo n.º 2
0
        public void HypergeometricDistributionSymmetry()
        {
            foreach (int nPopulation in TestUtilities.GenerateIntegerValues(1, 1000, 4))
            {
                foreach (int nSuccess in TestUtilities.GenerateIntegerValues(1, nPopulation, 2))
                {
                    foreach (int nDraws in TestUtilities.GenerateIntegerValues(1, nPopulation, 2))
                    {
                        HypergeometricDistribution d = new HypergeometricDistribution(nPopulation, nSuccess, nDraws);

                        HypergeometricDistribution d1 = new HypergeometricDistribution(nPopulation, nPopulation - nSuccess, nDraws);
                        HypergeometricDistribution d2 = new HypergeometricDistribution(nPopulation, nSuccess, nPopulation - nDraws);
                        HypergeometricDistribution d3 = new HypergeometricDistribution(nPopulation, nDraws, nSuccess);

                        int kMin = d.Support.LeftEndpoint;
                        int kMax = d.Support.RightEndpoint;
                        foreach (int k in TestUtilities.GenerateUniformIntegerValues(kMin, kMin, 4))
                        {
                            Assert.IsTrue(TestUtilities.IsNearlyEqual(d.ProbabilityMass(k), d1.ProbabilityMass(nDraws - k)));
                            Assert.IsTrue(TestUtilities.IsNearlyEqual(d.ProbabilityMass(k), d2.ProbabilityMass(nSuccess - k)));
                            Assert.IsTrue(TestUtilities.IsNearlyEqual(d.ProbabilityMass(k), d3.ProbabilityMass(k)));
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void MedianTest()
        {
            int populationSize = 15;
            int draws          = 7;
            int success        = 8;
            var target         = new HypergeometricDistribution(populationSize, success, draws);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5));
        }
Ejemplo n.º 4
0
        public void ProbabilityMassFunctionTest()
        {
            int N      = 50;
            int n      = 10;
            int m      = 5;
            var target = new HypergeometricDistribution(N, m, n);

            int    k        = 4;
            double expected = 0.0039645830580150657;
            double actual   = target.ProbabilityMassFunction(k);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public void DistributionFunctionTest()
        {
            int populationSize = 15;
            int draws          = 7;
            int success        = 8;
            var target         = new HypergeometricDistribution(populationSize, success, draws);

            int    k        = 5;
            double expected = 0.96829836829836835;
            double actual   = target.DistributionFunction(k);

            Assert.AreEqual(expected, actual, 1e-10);
        }
        public void ConstructorTest()
        {

            int populationSize = 15; // population size N
            int success = 7;         // number of successes in the sample  
            int samples = 8;         // number of samples drawn from N

            // Create a new Hypergeometric distribution with N = 15, n = 8, and s = 7
            var dist = new HypergeometricDistribution(populationSize, success, samples);

            double mean = dist.Mean;     // 1.3809523809523812
            double median = dist.Median; // 4.0
            double var = dist.Variance;  // 3.2879818594104315
            double mode = dist.Mode;     // 4.0

            double cdf = dist.DistributionFunction(k: 2);               // 0.80488799999999994
            double ccdf = dist.ComplementaryDistributionFunction(k: 2); // 0.19511200000000006

            double pdf1 = dist.ProbabilityMassFunction(k: 4); // 0.38073038073038074
            double pdf2 = dist.ProbabilityMassFunction(k: 5); // 0.18275058275058276
            double pdf3 = dist.ProbabilityMassFunction(k: 6); // 0.030458430458430458

            double lpdf = dist.LogProbabilityMassFunction(k: 2); // -2.3927801721315989

            int icdf1 = dist.InverseDistributionFunction(p: 0.17); // 4
            int icdf2 = dist.InverseDistributionFunction(p: 0.46); // 4
            int icdf3 = dist.InverseDistributionFunction(p: 0.87); // 5
            int icdf4 = dist.InverseDistributionFunction(p: 0.50);

            double hf = dist.HazardFunction(x: 4); // 1.7753623188405792
            double chf = dist.CumulativeHazardFunction(x: 4); // 1.5396683418789763

            string str = dist.ToString(CultureInfo.InvariantCulture); // "HyperGeometric(x; N = 15, m = 7, n = 8)"

            Assert.AreEqual(3.7333333333333334, mean);
            Assert.AreEqual(4.0, median);
            Assert.AreEqual(0.99555555555555553, var);
            Assert.AreEqual(4, mode);
            Assert.AreEqual(1.5396683418789763, chf, 1e-10);
            Assert.AreEqual(0.10023310023310024, cdf);
            Assert.AreEqual(0.38073038073038074, pdf1);
            Assert.AreEqual(0.18275058275058276, pdf2);
            Assert.AreEqual(0.030458430458430458, pdf3);
            Assert.AreEqual(-2.3927801721315989, lpdf);
            Assert.AreEqual(1.7753623188405792, hf);
            Assert.AreEqual(0.89976689976689972, ccdf);
            Assert.AreEqual(3, icdf1);
            Assert.AreEqual(4, icdf2);
            Assert.AreEqual(5, icdf3);
            Assert.AreEqual("HyperGeometric(x; N = 15, m = 7, n = 8)", str);
        }
Ejemplo n.º 7
0
        public void ConstructorTest()
        {
            int populationSize = 15; // population size N
            int success        = 7;  // number of successes in the sample
            int samples        = 8;  // number of samples drawn from N

            // Create a new Hypergeometric distribution with N = 15, n = 8, and s = 7
            var dist = new HypergeometricDistribution(populationSize, success, samples);

            double mean   = dist.Mean;                                  // 1.3809523809523812
            double median = dist.Median;                                // 4.0
            double var    = dist.Variance;                              // 3.2879818594104315
            double mode   = dist.Mode;                                  // 4.0

            double cdf  = dist.DistributionFunction(k: 2);              // 0.80488799999999994
            double ccdf = dist.ComplementaryDistributionFunction(k: 2); // 0.19511200000000006

            double pdf1 = dist.ProbabilityMassFunction(k: 4);           // 0.38073038073038074
            double pdf2 = dist.ProbabilityMassFunction(k: 5);           // 0.18275058275058276
            double pdf3 = dist.ProbabilityMassFunction(k: 6);           // 0.030458430458430458

            double lpdf = dist.LogProbabilityMassFunction(k: 2);        // -2.3927801721315989

            int icdf1 = dist.InverseDistributionFunction(p: 0.17);      // 4
            int icdf2 = dist.InverseDistributionFunction(p: 0.46);      // 4
            int icdf3 = dist.InverseDistributionFunction(p: 0.87);      // 5
            int icdf4 = dist.InverseDistributionFunction(p: 0.50);

            double hf  = dist.HazardFunction(x: 4);                   // 1.7753623188405792
            double chf = dist.CumulativeHazardFunction(x: 4);         // 1.5396683418789763

            string str = dist.ToString(CultureInfo.InvariantCulture); // "HyperGeometric(x; N = 15, m = 7, n = 8)"

            Assert.AreEqual(3.7333333333333334, mean);
            Assert.AreEqual(4.0, median);
            Assert.AreEqual(0.99555555555555553, var);
            Assert.AreEqual(4, mode);
            Assert.AreEqual(1.5396683418789763, chf, 1e-10);
            Assert.AreEqual(0.10023310023310024, cdf);
            Assert.AreEqual(0.38073038073038074, pdf1);
            Assert.AreEqual(0.18275058275058276, pdf2);
            Assert.AreEqual(0.030458430458430458, pdf3);
            Assert.AreEqual(-2.3927801721315989, lpdf);
            Assert.AreEqual(1.7753623188405792, hf);
            Assert.AreEqual(0.89976689976689972, ccdf);
            Assert.AreEqual(3, icdf1);
            Assert.AreEqual(4, icdf2);
            Assert.AreEqual(5, icdf3);
            Assert.AreEqual("HyperGeometric(x; N = 15, m = 7, n = 8)", str);
        }
Ejemplo n.º 8
0
        public void ProbabilityMassFunctionTest2()
        {
            int populationSize = 15;
            int draws          = 7;
            int success        = 8;
            var target         = new HypergeometricDistribution(populationSize, success, draws);

            int    k        = 5;
            double expected = 0.182750582750583;
            double actual   = target.ProbabilityMassFunction(k);

            Assert.AreEqual(expected, actual, 1e-10);
            Assert.IsFalse(Double.IsNaN(actual));
        }
Ejemplo n.º 9
0
        public void DistributionFunctionTest2()
        {
            // Verified against http://stattrek.com/online-calculator/hypergeometric.aspx

            int population        = 20;
            int populationSuccess = 8;
            int sample            = 6;

            double[] pmf          = { 0.0238390092879257, 0.163467492260062, 0.357585139318886, 0.317853457172343, 0.119195046439628, 0.0173374613003096, 0.000722394220846233 };
            double[] less         = { 0.0000000000000000, 0.023839009287926, 0.187306501547988, 0.544891640866874, 0.862745098039217, 0.981940144478844, 0.999277605779154 };
            double[] lessEqual    = { 0.0238390092879257, 0.187306501547988, 0.544891640866874, 0.862745098039217, 0.981940144478845, 0.999277605779154, 1 };
            double[] greater      = { 0.976160990712074, 0.812693498452012, 0.455108359133126, 0.137254901960783, 0.018059855521155, 0.000722394220845968, 0 };
            double[] greaterEqual = { 1, 0.976160990712074, 0.812693498452012, 0.455108359133126, 0.137254901960783, 0.0180598555211555, 0.00072239422084619 };

            var target = new HypergeometricDistribution(population, populationSuccess, sample);

            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(lessEqual[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

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

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

                {   // P(X >= i)
                    double actual = target.ComplementaryDistributionFunction(i, inclusive: true);
                    Assert.AreEqual(greaterEqual[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }
            }
        }
Ejemplo n.º 10
0
        public void ConstructorTest3()
        {
            var h = new HypergeometricDistribution(9, 6, 1);

            var support = h.Support;
            var range1  = h.GetRange(0.99);
            var range2  = h.GetRange(0.999);
            var range3  = h.GetRange(0.9999);

            Assert.AreEqual(0, support.Min);
            Assert.AreEqual(1, support.Max);
            Assert.AreEqual(support, range1);
            Assert.AreEqual(support, range2);
            Assert.AreEqual(support, range3);
        }
Ejemplo n.º 11
0
        public void CloneTest()
        {
            int populationSize = 12;
            int draws          = 5;
            int success        = 4;
            var target         = new HypergeometricDistribution(populationSize, success, draws);

            var actual = (HypergeometricDistribution)target.Clone();

            Assert.AreNotSame(target, actual);
            Assert.AreNotEqual(target, actual);

            Assert.AreEqual(target.PopulationSize, actual.PopulationSize);
            Assert.AreEqual(target.SampleSize, actual.SampleSize);
            Assert.AreEqual(target.PopulationSuccess, actual.PopulationSuccess);
        }
Ejemplo n.º 12
0
        public void HypergeometricDistributionConstructorTest()
        {
            bool thrown;

            thrown = false;
            try { new HypergeometricDistribution(10, 0, 11); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { new HypergeometricDistribution(10, 11, 9); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { new HypergeometricDistribution(0, 0, 1); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { new HypergeometricDistribution(1, 0, 0); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { new HypergeometricDistribution(1, -1, 1); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            int N = 10;
            int n = 8;
            int m = 9;

            var target = new HypergeometricDistribution(N, m, n);

            Assert.AreEqual(N, target.PopulationSize);
            Assert.AreEqual(n, target.SampleSize);
            Assert.AreEqual(m, target.PopulationSuccess);

            double dN = N;
            double dn = n;
            double dm = m;

            Assert.AreEqual(dn * (dm / dN), target.Mean);
            Assert.AreEqual(dn * dm * (dN - dm) * (dN - dn) / (dN * dN * (dN - 1.0)), target.Variance);
        }
Ejemplo n.º 13
0
        public void icdf()
        {
            var dist = HypergeometricDistribution.FromSuccessCounts(successes: 10, failures: 5, samples: 8);

            {
                double pdf = dist.ProbabilityMassFunction(3);
                Assert.AreEqual(0.018648018648018648, pdf); // matches R

                double cdf = dist.DistributionFunction(3);
                Assert.AreEqual(0.018648018648018648, cdf); // matches R
            }

            {
                double x    = 0.3;
                int    icdf = dist.InverseDistributionFunction(x);
                Assert.AreEqual(5, icdf);

                double cdf = dist.DistributionFunction(icdf);
                Assert.AreEqual(0.57342657342657344, cdf); // matches R
            }

            double[] percentiles = Vector.Range(0.0, 1.0, stepSize: 0.1);
            for (int i = 0; i < percentiles.Length; i++)
            {
                double x      = percentiles[i];
                int    icdf   = dist.InverseDistributionFunction(x);
                double cdf    = dist.DistributionFunction(icdf);
                int    iicdf  = dist.InverseDistributionFunction(cdf);
                double iiicdf = dist.DistributionFunction(iicdf);

                Assert.AreEqual(iicdf, icdf, 1e-5);
                double rx = Math.Floor(x);
                double rc = Math.Floor(cdf);
                Assert.AreEqual(rx, rc);
                Assert.AreEqual(iiicdf, cdf, 1e-5);
            }
        }
        public void DistributionFunctionTest2()
        {
            // Verified against http://stattrek.com/online-calculator/hypergeometric.aspx

            int population = 20;
            int populationSuccess = 8;
            int sample = 6;

            double[] pmf = { 0.0238390092879257, 0.163467492260062, 0.357585139318886, 0.317853457172343, 0.119195046439628, 0.0173374613003096, 0.000722394220846233 };
            double[] less = { 0.0000000000000000, 0.023839009287926, 0.187306501547988, 0.544891640866874, 0.862745098039217, 0.981940144478844, 0.999277605779154 };
            double[] lessEqual = { 0.0238390092879257, 0.187306501547988, 0.544891640866874, 0.862745098039217, 0.981940144478845, 0.999277605779154, 1 };
            double[] greater = { 0.976160990712074, 0.812693498452012, 0.455108359133126, 0.137254901960783, 0.018059855521155, 0.000722394220845968, 0 };
            double[] greaterEqual = { 1, 0.976160990712074, 0.812693498452012, 0.455108359133126, 0.137254901960783, 0.0180598555211555, 0.00072239422084619 };

            var target = new HypergeometricDistribution(population, populationSuccess, sample);

            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(lessEqual[i], actual, 1e-4);
                    Assert.IsFalse(Double.IsNaN(actual));
                }

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

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

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

        }
        public void ProbabilityMassFunctionTest()
        {
            int N = 50;
            int n = 10;
            int m = 5;
            var target = new HypergeometricDistribution(N, m, n);

            int k = 4;
            double expected = 0.0039645830580150657;
            double actual = target.ProbabilityMassFunction(k);
            Assert.AreEqual(expected, actual);
        }
        public void ProbabilityMassFunctionTest2()
        {
            int populationSize = 15;
            int draws = 7;
            int success = 8;
            var target = new HypergeometricDistribution(populationSize, success, draws);

            int k = 5;
            double expected = 0.182750582750583;
            double actual = target.ProbabilityMassFunction(k);
            Assert.AreEqual(expected, actual, 1e-10);
            Assert.IsFalse(Double.IsNaN(actual));
        }
        public void DistributionFunctionTest()
        {
            int populationSize = 15;
            int draws = 7;
            int success = 8;
            var target = new HypergeometricDistribution(populationSize, success, draws);

            int k = 5;
            double expected = 0.96829836829836835;
            double actual = target.DistributionFunction(k);
            Assert.AreEqual(expected, actual, 1e-10);
        }
        public void MedianTest()
        {
            int populationSize = 15;
            int draws = 7;
            int success = 8;
            var target = new HypergeometricDistribution(populationSize, success, draws);

            Assert.AreEqual(target.Median, target.InverseDistributionFunction(0.5));
        }
        public void CloneTest()
        {
            int populationSize = 12;
            int draws = 5;
            int success = 4;
            var target = new HypergeometricDistribution(populationSize, success, draws);

            var actual = (HypergeometricDistribution)target.Clone();

            Assert.AreNotSame(target, actual);
            Assert.AreNotEqual(target, actual);

            Assert.AreEqual(target.PopulationSize, actual.PopulationSize);
            Assert.AreEqual(target.SampleSize, actual.SampleSize);
            Assert.AreEqual(target.PopulationSuccess, actual.PopulationSuccess);
        }
        public void HypergeometricDistributionConstructorTest()
        {
            bool thrown;

            thrown = false;
            try { new HypergeometricDistribution(10, 0, 11); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { new HypergeometricDistribution(10, 11, 9); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { new HypergeometricDistribution(0, 0, 1); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { new HypergeometricDistribution(1, 0, 0); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            thrown = false;
            try { new HypergeometricDistribution(1, -1, 1); }
            catch (ArgumentException) { thrown = true; }
            Assert.IsTrue(thrown);

            int N = 10;
            int n = 8;
            int m = 9;

            var target = new HypergeometricDistribution(N, m, n);
            Assert.AreEqual(N, target.PopulationSize);
            Assert.AreEqual(n, target.SampleSize);
            Assert.AreEqual(m, target.PopulationSuccess);

            double dN = N;
            double dn = n;
            double dm = m;

            Assert.AreEqual(dn * (dm / dN), target.Mean);
            Assert.AreEqual(dn * dm * (dN - dm) * (dN - dn) / (dN * dN * (dN - 1.0)), target.Variance);
        }
 public void HypergeometricDistributionStandardDeviation()
 {
     Assert.AreEqual(HypergeometricDistribution.StandardDeviation(14, 10, 8), 0.868062314643143m);
 }
 public void HypergeometricDistributionVariance()
 {
     Assert.AreEqual(HypergeometricDistribution.Variance(14, 10, 8), 0.7535321821036106750392464673m);
 }
        public void ConstructorTest3()
        {
            var h = new HypergeometricDistribution(9, 6, 1);

            var support = h.Support;
            var range1 = h.GetRange(0.99);
            var range2 = h.GetRange(0.999);
            var range3 = h.GetRange(0.9999);

            Assert.AreEqual(0, support.Min);
            Assert.AreEqual(1, support.Max);
            Assert.AreEqual(support, range1);
            Assert.AreEqual(support, range2);
            Assert.AreEqual(support, range3);
        }
 public void HypergeometricDistributionPMF()
 {
     Assert.AreEqual(HypergeometricDistribution.ProbabilityMassFunction(14, 10, 8, 6), 0.41958041958042m);
 }
 public void HypergemoetricDistributionMean()
 {
     Assert.AreEqual(HypergeometricDistribution.Mean(14, 10, 8), 5.7142857142857142857142857143m);
 }