public int GetNumber() { double nextSample = Distribution.Sample(); Sum += nextSample; double result = Math.Floor(Sum); Sum -= result; return((int)result); }
public void TestDistribution(double xm, double a) { var expectedMean = a * xm / (a - 1); var expectedVariance = (xm * xm * a) / ((a - 1) * (a - 1) * (a - 2)); var distribution = new Pareto(xm, a); var list = Enumerable.Range(1, 10000000).Select(x => distribution.Sample()).ToList(); var calculatedMean = Statistics.Mean(list); calculatedMean.ShouldBe(expectedMean, 0.05 * expectedMean); Statistics.Variance(list).ShouldBe(expectedVariance, 0.5 * expectedVariance); }
public void CanSample() { var n = new Pareto(1.0, 1.0); n.Sample(); }
/// <summary> /// Run example /// </summary> /// <a href="http://en.wikipedia.org/wiki/Pareto_distribution">Pareto distribution</a> public void Run() { // 1. Initialize the new instance of the Pareto distribution class with parameters Shape = 3, Scale = 1 var pareto = new Pareto(1, 3); Console.WriteLine(@"1. Initialize the new instance of the Pareto distribution class with parameters Shape = {0}, Scale = {1}", pareto.Shape, pareto.Scale); Console.WriteLine(); // 2. Distributuion properties: Console.WriteLine(@"2. {0} distributuion properties:", pareto); // Cumulative distribution function Console.WriteLine(@"{0} - Сumulative distribution at location '0.3'", pareto.CumulativeDistribution(0.3).ToString(" #0.00000;-#0.00000")); // Probability density Console.WriteLine(@"{0} - Probability density at location '0.3'", pareto.Density(0.3).ToString(" #0.00000;-#0.00000")); // Log probability density Console.WriteLine(@"{0} - Log probability density at location '0.3'", pareto.DensityLn(0.3).ToString(" #0.00000;-#0.00000")); // Entropy Console.WriteLine(@"{0} - Entropy", pareto.Entropy.ToString(" #0.00000;-#0.00000")); // Largest element in the domain Console.WriteLine(@"{0} - Largest element in the domain", pareto.Maximum.ToString(" #0.00000;-#0.00000")); // Smallest element in the domain Console.WriteLine(@"{0} - Smallest element in the domain", pareto.Minimum.ToString(" #0.00000;-#0.00000")); // Mean Console.WriteLine(@"{0} - Mean", pareto.Mean.ToString(" #0.00000;-#0.00000")); // Median Console.WriteLine(@"{0} - Median", pareto.Median.ToString(" #0.00000;-#0.00000")); // Mode Console.WriteLine(@"{0} - Mode", pareto.Mode.ToString(" #0.00000;-#0.00000")); // Variance Console.WriteLine(@"{0} - Variance", pareto.Variance.ToString(" #0.00000;-#0.00000")); // Standard deviation Console.WriteLine(@"{0} - Standard deviation", pareto.StdDev.ToString(" #0.00000;-#0.00000")); // Skewness Console.WriteLine(@"{0} - Skewness", pareto.Skewness.ToString(" #0.00000;-#0.00000")); Console.WriteLine(); // 3. Generate 10 samples of the Pareto distribution Console.WriteLine(@"3. Generate 10 samples of the Pareto distribution"); for (var i = 0; i < 10; i++) { Console.Write(pareto.Sample().ToString("N05") + @" "); } Console.WriteLine(); Console.WriteLine(); // 4. Generate 100000 samples of the Pareto(1, 3) distribution and display histogram Console.WriteLine(@"4. Generate 100000 samples of the Pareto(1, 3) distribution and display histogram"); var data = new double[100000]; for (var i = 0; i < data.Length; i++) { data[i] = pareto.Sample(); } ConsoleHelper.DisplayHistogram(data); Console.WriteLine(); // 5. Generate 100000 samples of the Pareto(1, 1) distribution and display histogram Console.WriteLine(@"5. Generate 100000 samples of the Pareto(1, 1) distribution and display histogram"); pareto.Shape = 1; for (var i = 0; i < data.Length; i++) { data[i] = pareto.Sample(); } ConsoleHelper.DisplayHistogram(data); Console.WriteLine(); // 6. Generate 100000 samples of the Pareto(10, 5) distribution and display histogram Console.WriteLine(@"6. Generate 100000 samples of the Pareto(10, 50) distribution and display histogram"); pareto.Shape = 50; pareto.Scale = 10; for (var i = 0; i < data.Length; i++) { data[i] = pareto.Sample(); } ConsoleHelper.DisplayHistogram(data); }