public void ValidateEntropyThrowsNotSupportedException()
 {
     var d = new NegativeBinomial(1.0, 0.5);
     Assert.Throws<NotSupportedException>(() => { var e = d.Entropy; });
 }
        /// <summary>
        /// Run example
        /// </summary>
        /// <a href="http://en.wikipedia.org/wiki/Negative_binomial">NegativeBinomial distribution</a>
        public void Run()
        {
            // 1. Initialize the new instance of the NegativeBinomial distribution class with parameters P = 0.2, R = 20
            var negativeBinomial = new NegativeBinomial(20, 0.2);
            Console.WriteLine(@"1. Initialize the new instance of the NegativeBinomial distribution class with parameters P = {0}, N = {1}", negativeBinomial.P, negativeBinomial.R);
            Console.WriteLine();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            // 5. Generate 100000 samples of the NegativeBinomial(0.7, 20) distribution and display histogram
            Console.WriteLine(@"5. Generate 100000 samples of the NegativeBinomial(0.7, 20) distribution and display histogram");
            negativeBinomial.P = 0.7;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = negativeBinomial.Sample();
            }

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

            // 6. Generate 100000 samples of the NegativeBinomial(0.5, 1) distribution and display histogram
            Console.WriteLine(@"6. Generate 100000 samples of the NegativeBinomial(0.5, 1) distribution and display histogram");
            negativeBinomial.P = 0.5;
            negativeBinomial.R = 1;
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = negativeBinomial.Sample();
            }

            ConsoleHelper.DisplayHistogram(data);
        }
 public void CanCreateNegativeBinomial(double r, double p)
 {
     var d = new NegativeBinomial(r, p);
     Assert.AreEqual(r, d.R);
     Assert.AreEqual(p, d.P);
 }
 public void ValidateToString()
 {
     var d = new NegativeBinomial(1.0, 0.3);
     Assert.AreEqual(String.Format("NegativeBinomial(R = {0}, P = {1})", d.R, d.P), d.ToString());
 }
 public void CanSample()
 {
     var d = new NegativeBinomial(1.0, 0.5);
     d.Sample();
 }
 public void CanSampleSequence()
 {
     var d = new NegativeBinomial(1.0, 0.5);
     var ied = d.Samples();
     GC.KeepAlive(ied.Take(5).ToArray());
 }
 public void ValidateProbabilityLn(double r, double p, int x)
 {
     var d = new NegativeBinomial(r, p);
     Assert.AreEqual(SpecialFunctions.GammaLn(r + x) - SpecialFunctions.GammaLn(r) - SpecialFunctions.GammaLn(x + 1.0) + (r * Math.Log(p)) + (x * Math.Log(1.0 - p)), d.ProbabilityLn(x));
 }
 public void ValidateCumulativeDistribution(double r, double p, int x)
 {
     var d = new NegativeBinomial(r, p);
     Assert.AreEqual(SpecialFunctions.BetaRegularized(r, x + 1.0, p), d.CumulativeDistribution(x), 1e-12);
 }
 public void ValidateMaximum()
 {
     var d = new NegativeBinomial(1.0, 0.3);
     Assert.AreEqual(int.MaxValue, d.Maximum);
 }
 public void ValidateMinimum()
 {
     var d = new NegativeBinomial(1.0, 0.5);
     Assert.AreEqual(0, d.Minimum);
 }
 public void ValidateMedianThrowsNotSupportedException()
 {
     var d = new NegativeBinomial(1.0, 0.5);
     Assert.Throws<NotSupportedException>(() => { var m = d.Median; });
 }
 public void ValidateMode(double r, double p)
 {
     var d = new NegativeBinomial(r, p);
     if (r > 1)
     {
         Assert.AreEqual((int)Math.Floor((r - 1.0) * (1.0 - p) / p), d.Mode);
     }
     else
     {
         Assert.AreEqual(0.0, d.Mode);
     }
 }
 public void ValidateSkewness(double r, double p)
 {
     var b = new NegativeBinomial(r, p);
     Assert.AreEqual((2.0 - p) / Math.Sqrt(r * (1.0 - p)), b.Skewness);
 }