public void CanCreateHypergeometric(int population, int success, int draws)
 {
     var d = new Hypergeometric(population, success, draws);
     Assert.AreEqual(population, d.Population);
     Assert.AreEqual(success, d.Success);
     Assert.AreEqual(draws, d.Draws);
 }
        /// <summary>
        /// Run example
        /// </summary>
        /// <a href="http://en.wikipedia.org/wiki/Hypergeometric_distribution">Hypergeometric distribution</a>
        public void Run()
        {
            // 1. Initialize the new instance of the Hypergeometric distribution class with parameters PopulationSize = 10, M = 2, N = 8
            var hypergeometric = new Hypergeometric(30, 15, 10);
            Console.WriteLine(@"1. Initialize the new instance of the Hypergeometric distribution class with parameters PopulationSize = {0}, M = {1}, N = {2}", hypergeometric.PopulationSize, hypergeometric.M, hypergeometric.N);
            Console.WriteLine();

            // 2. Distributuion properties:
            Console.WriteLine(@"2. {0} distributuion properties:", hypergeometric);

            // Cumulative distribution function
            Console.WriteLine(@"{0} - Сumulative distribution at location '3'", hypergeometric.CumulativeDistribution(3).ToString(" #0.00000;-#0.00000"));

            // Probability density
            Console.WriteLine(@"{0} - Probability mass at location '3'", hypergeometric.Probability(3).ToString(" #0.00000;-#0.00000"));

            // Log probability density
            Console.WriteLine(@"{0} - Log probability mass at location '3'", hypergeometric.ProbabilityLn(3).ToString(" #0.00000;-#0.00000"));

            // Largest element in the domain
            Console.WriteLine(@"{0} - Largest element in the domain", hypergeometric.Maximum.ToString(" #0.00000;-#0.00000"));

            // Smallest element in the domain
            Console.WriteLine(@"{0} - Smallest element in the domain", hypergeometric.Minimum.ToString(" #0.00000;-#0.00000"));

            // Mean
            Console.WriteLine(@"{0} - Mean", hypergeometric.Mean.ToString(" #0.00000;-#0.00000"));

            // Mode
            Console.WriteLine(@"{0} - Mode", hypergeometric.Mode.ToString(" #0.00000;-#0.00000"));

            // Variance
            Console.WriteLine(@"{0} - Variance", hypergeometric.Variance.ToString(" #0.00000;-#0.00000"));

            // Standard deviation
            Console.WriteLine(@"{0} - Standard deviation", hypergeometric.StdDev.ToString(" #0.00000;-#0.00000"));

            // Skewness
            Console.WriteLine(@"{0} - Skewness", hypergeometric.Skewness.ToString(" #0.00000;-#0.00000"));
            Console.WriteLine();

            // 3. Generate 10 samples of the Hypergeometric distribution
            Console.WriteLine(@"3. Generate 10 samples of the Hypergeometric distribution");
            for (var i = 0; i < 10; i++)
            {
                Console.Write(hypergeometric.Sample().ToString("N05") + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 4. Generate 100000 samples of the Hypergeometric(30, 15, 10) distribution and display histogram
            Console.WriteLine(@"4. Generate 100000 samples of the Hypergeometric(30, 15, 10) distribution and display histogram");
            var data = new double[100000];
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = hypergeometric.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
            Console.WriteLine();

            // 5. Generate 100000 samples of the Hypergeometric(52, 13, 5) distribution and display histogram
            Console.WriteLine(@"5. Generate 100000 samples of the Hypergeometric(52, 13, 5) distribution and display histogram");
            hypergeometric.PopulationSize = 52;
            hypergeometric.M = 13;
            hypergeometric.N = 5;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = hypergeometric.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
        }
 public void ValidateEntropyThrowsNotSupportedException()
 {
     var d = new Hypergeometric(10, 1, 1);
     Assert.Throws<NotSupportedException>(() => { var e = d.Entropy; });
 }
 public void ValidateToString()
 {
     var d = new Hypergeometric(10, 1, 1);
     Assert.AreEqual("Hypergeometric(N = 10, M = 1, n = 1)", d.ToString());
 }
 public void CumulativeDistributionMustNotOverflow_CodePlexIssue5729()
 {
     var d = new Hypergeometric(10000, 2, 9800);
     Assert.That(d.CumulativeDistribution(0.0), Is.Not.NaN);
     Assert.That(d.CumulativeDistribution(0.1), Is.Not.NaN);
 }
 public void CanSampleSequence()
 {
     var d = new Hypergeometric(10, 1, 1);
     var ied = d.Samples();
     GC.KeepAlive(ied.Take(5).ToArray());
 }
 public void ValidateCumulativeDistribution(int population, int success, int draws, double x, double cdf)
 {
     var d = new Hypergeometric(population, success, draws);
     AssertHelpers.AlmostEqualRelative(cdf, d.CumulativeDistribution(x), 9);
 }
 public void ValidateProbabilityLn(int population, int success, int draws, int x)
 {
     var d = new Hypergeometric(population, success, draws);
     Assert.That(d.ProbabilityLn(x), Is.EqualTo(Math.Log(d.Probability(x))).Within(1e-14));
 }
 public void CanSample()
 {
     var d = new Hypergeometric(10, 1, 1);
     d.Sample();
 }
 public void ValidateProbability(int population, int success, int draws, int x)
 {
     var d = new Hypergeometric(population, success, draws);
     Assert.That(d.Probability(x), Is.EqualTo(SpecialFunctions.Binomial(success, x)*SpecialFunctions.Binomial(population - success, draws - x)/SpecialFunctions.Binomial(population, draws)));
 }
 public void ValidateMaximum(int population, int success, int draws)
 {
     var d = new Hypergeometric(population, success, draws);
     Assert.AreEqual(Math.Min(success, draws), d.Maximum);
 }
 public void ValidateMedianThrowsNotSupportedException()
 {
     var d = new Hypergeometric(10, 1, 1);
     Assert.Throws<NotSupportedException>(() => { var m = d.Median; });
 }
 public void ValidateMode(int population, int success, int draws)
 {
     var d = new Hypergeometric(population, success, draws);
     Assert.AreEqual((draws + 1)*(success + 1)/(population + 2), d.Mode);
 }
 public void ValidateSkewness(int population, int success, int draws)
 {
     var d = new Hypergeometric(population, success, draws);
     Assert.AreEqual((Math.Sqrt(population - 1.0)*(population - (2*draws))*(population - (2*success)))/(Math.Sqrt(draws*success*(population - success)*(population - draws))*(population - 2.0)), d.Skewness);
 }