/// <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 ValidateProbability(double r, double p, int x) { var d = new NegativeBinomial(r, p); Assert.AreEqual(Math.Exp(SpecialFunctions.GammaLn(r + x) - SpecialFunctions.GammaLn(r) - SpecialFunctions.GammaLn(x + 1.0) + (r * Math.Log(p)) + (x * Math.Log(1.0 - p))), d.Probability(x)); }