public void TestStarUniformDistribution() { IContinuousDistribution distribution = new Gamma(9, 1/5000.0); Console.WriteLine(distribution.CumulativeDistribution(30000)); //IContinuousDistribution distribution = new Gamma(3, 0.05); for (double d = 0; d < 50000; d = d + 1000) { //Console.WriteLine(Gamma.PDF(3, 0.05, d)); //Console.WriteLine(Gamma.CDF(3, 0.05, d)); Console.WriteLine("" + d + ": " + distribution.CumulativeDistribution(d)); } }
public void SetupDistributions() { dists = new IDistribution[8]; dists[0] = new Beta(1.0, 1.0); dists[1] = new ContinuousUniform(0.0, 1.0); dists[2] = new Gamma(1.0, 1.0); dists[3] = new Normal(0.0, 1.0); dists[4] = new Bernoulli(0.6); dists[5] = new Weibull(1.0, 1.0); dists[6] = new DiscreteUniform(1, 10); dists[7] = new LogNormal(1.0, 1.0); }
public void ValidateInverseCumulativeDistribution(int shape, double invScale, double x, double cdf) { var gamma = new Gamma(shape, invScale); Assert.That(gamma.InverseCumulativeDistribution(cdf), Is.EqualTo(x).Within(10)); Assert.That(Gamma.InvCDF(shape, invScale, cdf), Is.EqualTo(x).Within(10)); }
public void SetShapeFailsWithNegativeShape() { var n = new Gamma(1.0, 1.0); Assert.That(() => n.Shape = -1.0, Throws.ArgumentException); }
public void ValidateDensityLn(int shape, double invScale, double x, double pdfln) { var n = new Gamma(shape, invScale); AssertHelpers.AlmostEqualRelative(pdfln, n.DensityLn(x), 13); AssertHelpers.AlmostEqualRelative(pdfln, Gamma.PDFLn(shape, invScale, x), 13); }
public void SetInvScaleFailsWithNegativeInvScale() { var n = new Gamma(1.0, 1.0); Assert.Throws<ArgumentOutOfRangeException>(() => n.Rate = -1.0); }
/// <summary> /// Run example /// </summary> /// <a href="http://en.wikipedia.org/wiki/Gamma_distribution">Gamma distribution</a> public void Run() { // 1. Initialize the new instance of the Gamma distribution class with parameter Shape = 1, Scale = 0.5. var gamma = new Gamma(1, 2.0); Console.WriteLine(@"1. Initialize the new instance of the Gamma distribution class with parameters Shape = {0}, Scale = {1}", gamma.Shape, gamma.Scale); Console.WriteLine(); // 2. Distributuion properties: Console.WriteLine(@"2. {0} distributuion properties:", gamma); // Cumulative distribution function Console.WriteLine(@"{0} - Сumulative distribution at location '0.3'", gamma.CumulativeDistribution(0.3).ToString(" #0.00000;-#0.00000")); // Probability density Console.WriteLine(@"{0} - Probability density at location '0.3'", gamma.Density(0.3).ToString(" #0.00000;-#0.00000")); // Log probability density Console.WriteLine(@"{0} - Log probability density at location '0.3'", gamma.DensityLn(0.3).ToString(" #0.00000;-#0.00000")); // Entropy Console.WriteLine(@"{0} - Entropy", gamma.Entropy.ToString(" #0.00000;-#0.00000")); // Largest element in the domain Console.WriteLine(@"{0} - Largest element in the domain", gamma.Maximum.ToString(" #0.00000;-#0.00000")); // Smallest element in the domain Console.WriteLine(@"{0} - Smallest element in the domain", gamma.Minimum.ToString(" #0.00000;-#0.00000")); // Mean Console.WriteLine(@"{0} - Mean", gamma.Mean.ToString(" #0.00000;-#0.00000")); // Mode Console.WriteLine(@"{0} - Mode", gamma.Mode.ToString(" #0.00000;-#0.00000")); // Variance Console.WriteLine(@"{0} - Variance", gamma.Variance.ToString(" #0.00000;-#0.00000")); // Standard deviation Console.WriteLine(@"{0} - Standard deviation", gamma.StdDev.ToString(" #0.00000;-#0.00000")); // Skewness Console.WriteLine(@"{0} - Skewness", gamma.Skewness.ToString(" #0.00000;-#0.00000")); Console.WriteLine(); // 3. Generate 10 samples of the Gamma distribution Console.WriteLine(@"3. Generate 10 samples of the Gamma distribution"); for (var i = 0; i < 10; i++) { Console.Write(gamma.Sample().ToString("N05") + @" "); } Console.WriteLine(); Console.WriteLine(); // 4. Generate 100000 samples of the Gamma(1, 2) distribution and display histogram Console.WriteLine(@"4. Generate 100000 samples of the Gamma(1, 2) distribution and display histogram"); var data = new double[100000]; Gamma.Samples(data, 1, 2); ConsoleHelper.DisplayHistogram(data); Console.WriteLine(); // 5. Generate 100000 samples of the Gamma(8) distribution and display histogram Console.WriteLine(@"5. Generate 100000 samples of the Gamma(5, 1) distribution and display histogram"); Gamma.Samples(data, 5, 1); ConsoleHelper.DisplayHistogram(data); }
public void ValidateSkewness(double shape, double invScale, double skewness) { var n = new Gamma(shape, invScale); AssertHelpers.AlmostEqualRelative(skewness, n.Skewness, 15); }
/// <summary> /// Generates a sequence of samples from the distribution. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="shape">The shape (k) of the Erlang distribution. Range: k ≥ 0.</param> /// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0.</param> /// <returns>a sequence of samples from the distribution.</returns> public static IEnumerable <double> Samples(System.Random rnd, int shape, double rate) { return(Gamma.Samples(rnd, shape, rate)); }
static double SampleUnchecked(System.Random rnd, double shape, double scale) { return(1.0 / Gamma.SampleUnchecked(rnd, shape, scale)); }
static IEnumerable <double> SamplesUnchecked(System.Random rnd, double shape, double scale) { return(Gamma.SamplesUnchecked(rnd, shape, scale).Select(z => 1.0 / z)); }
/// <summary> /// Fills an array with samples generated from the distribution. /// </summary> /// <param name="values">The array to fill with the samples.</param> /// <param name="shape">The shape (k) of the Erlang distribution. Range: k ≥ 0.</param> /// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0.</param> /// <returns>a sequence of samples from the distribution.</returns> public static void Samples(double[] values, int shape, double rate) { Gamma.Samples(values, shape, rate); }
/// <summary> /// Generates a sequence of samples from the NormalGamma distribution /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="meanLocation">The location of the mean.</param> /// <param name="meanScale">The scale of the mean.</param> /// <param name="precisionShape">The shape of the precision.</param> /// <param name="precisionInvScale">The inverse scale of the precision.</param> /// <returns>a sequence of samples from the distribution.</returns> public static IEnumerable <MeanPrecisionPair> Samples(System.Random rnd, double meanLocation, double meanScale, double precisionShape, double precisionInvScale) { if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInvScale)) { throw new ArgumentException(Resources.InvalidDistributionParameters); } while (true) { var mp = new MeanPrecisionPair(); // Sample the precision. mp.Precision = double.IsPositiveInfinity(precisionInvScale) ? precisionShape : Gamma.Sample(rnd, precisionShape, precisionInvScale); // Sample the mean. mp.Mean = meanScale == 0.0 ? meanLocation : Normal.Sample(rnd, meanLocation, Math.Sqrt(1.0 / (meanScale * mp.Precision))); yield return(mp); } }
/// <summary> /// Generates a sequence of samples from the distribution. /// </summary> /// <param name="shape">The shape (k) of the Erlang distribution. Range: k ≥ 0.</param> /// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0.</param> /// <returns>a sequence of samples from the distribution.</returns> public static IEnumerable <double> Samples(int shape, double rate) { return(Gamma.Samples(shape, rate)); }
/// <summary> /// Generates a sample from the distribution. /// </summary> /// <param name="shape">The shape (k) of the Erlang distribution. Range: k ≥ 0.</param> /// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0.</param> /// <returns>a sample from the distribution.</returns> public static double Sample(int shape, double rate) { return(Gamma.Sample(shape, rate)); }
/// <summary> /// Fills an array with samples generated from the distribution. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="values">The array to fill with the samples.</param> /// <param name="shape">The shape (k) of the Erlang distribution. Range: k ≥ 0.</param> /// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0.</param> /// <returns>a sequence of samples from the distribution.</returns> public static void Samples(System.Random rnd, double[] values, int shape, double rate) { Gamma.Samples(rnd, values, shape, rate); }
public void ValidateMean(double shape, double invScale, double mean) { var n = new Gamma(shape, invScale); Assert.AreEqual(mean, n.Mean); }
/// <summary> /// Samples student-t distributed random variables. /// </summary> /// <remarks>The algorithm is method 2 in section 5, chapter 9 /// in L. Devroye's "Non-Uniform Random Variate Generation"</remarks> /// <param name="rnd">The random number generator to use.</param> /// <param name="location">The location (μ) of the distribution.</param> /// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param> /// <param name="freedom">The degrees of freedom (ν) for the distribution. Range: ν > 0.</param> /// <returns>a random number from the standard student-t distribution.</returns> static double SampleUnchecked(System.Random rnd, double location, double scale, double freedom) { var gamma = Gamma.SampleUnchecked(rnd, 0.5 * freedom, 0.5); return(Normal.Sample(rnd, location, scale * Math.Sqrt(freedom / gamma))); }
public void ValidateMinimum() { var n = new Gamma(1.0, 1.0); Assert.AreEqual(0.0, n.Minimum); }
/// <summary> /// Draws a random sample from the distribution. /// </summary> /// <returns>A random number from this distribution.</returns> public double Sample() { return(1.0 / Gamma.Sample(_random, _shape, _scale)); }
public void ValidateToString() { var n = new Gamma(1d, 2d); Assert.AreEqual("Gamma(α = 1, β = 2)", n.ToString()); }
/// <summary> /// Generates a sequence of samples from the Cauchy distribution. /// </summary> /// <returns>a sequence of samples from the distribution.</returns> public IEnumerable <double> Samples() { return(Gamma.Samples(_random, _shape, _scale).Select(z => 1.0 / z)); }
public void ValidateDensity(int shape, double invScale, double x, double pdf) { var n = new Gamma(shape, invScale); AssertHelpers.AlmostEqual(pdf, n.Density(x), 14); AssertHelpers.AlmostEqual(pdf, Gamma.PDF(shape, invScale, x), 14); }
public void SetScaleFailsWithNegativeScale() { var n = new Gamma(1.0, 1.0); n.Scale = -1.0; }
public void SetShapeFailsWithNegativeShape() { var n = new Gamma(1.0, 1.0); Assert.Throws<ArgumentOutOfRangeException>(() => n.Shape = -1.0); }
public void SetShapeFailsWithNegativeShape() { var n = new Gamma(1.0, 1.0); n.Shape = -1.0; }
public void SetInvScaleFailsWithNegativeInvScale() { var n = new Gamma(1.0, 1.0); Assert.That(() => n.Rate = -1.0, Throws.ArgumentException); }
public void ValidateDensityLn(double shape, double invScale, double x, double pdfln) { var n = new Gamma(shape, invScale); AssertHelpers.AlmostEqual(pdfln, n.DensityLn(x), 14); }
public void ValidateCumulativeDistribution(int shape, double invScale, double x, double cdf) { var gamma = new Gamma(shape, invScale); Assert.That(gamma.CumulativeDistribution(x), Is.EqualTo(cdf).Within(13)); Assert.That(Gamma.CDF(shape, invScale, x), Is.EqualTo(cdf).Within(13)); }
public void ValidateMaximum() { var n = new Gamma(1.0, 1.0); AssertEx.AreEqual<double>(System.Double.PositiveInfinity, n.Maximum); }
public void ValidateEntropy(double shape, double invScale, double entropy) { var n = new Gamma(shape, invScale); AssertHelpers.AlmostEqualRelative(entropy, n.Entropy, 12); }
public void ValidateMedian(double shape, double invScale) { var n = new Gamma(shape, invScale); var median = n.Median; }
public void ValidateMaximum() { var n = new Gamma(1.0, 1.0); Assert.AreEqual(Double.PositiveInfinity, n.Maximum); }
public void ValidateToString() { var n = new Gamma(1.0, 2.0); AssertEx.AreEqual<string>("Gamma(Shape = 1, Inverse Scale = 2)", n.ToString()); }
public void ValidateMedian() { var n = new Gamma(0.0, 0.0); Assert.Throws<NotSupportedException>(() => { var median = n.Median; }); }
public void CanCreateGamma(double shape, double invScale) { var n = new Gamma(shape, invScale); AssertEx.AreEqual<double>(shape, n.Shape); AssertEx.AreEqual<double>(invScale, n.InvScale); }
public void ValidateMode(double shape, double invScale, double mode) { var n = new Gamma(shape, invScale); Assert.AreEqual(mode, n.Mode); }
public void CanCreateGamma(double shape, double invScale) { var n = new Gamma(shape, invScale); Assert.AreEqual(shape, n.Shape); Assert.AreEqual(invScale, n.Rate); }
public void ValidateStdDev(double shape, double invScale, double sdev) { var n = new Gamma(shape, invScale); AssertHelpers.AlmostEqualRelative(sdev, n.StdDev, 15); }
/// <summary> /// Fills an array with samples generated from the distribution. /// </summary> public void Samples(double[] values) { Gamma.SamplesUnchecked(_random, values, _shape, _rate); }
public void ValidateVariance(double shape, double invScale, double var) { var n = new Gamma(shape, invScale); AssertHelpers.AlmostEqualRelative(var, n.Variance, 15); }
/// <summary> /// Generates a sample from the distribution. /// </summary> /// <param name="rnd">The random number generator to use.</param> /// <param name="shape">The shape (k) of the Erlang distribution. Range: k ≥ 0.</param> /// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0.</param> /// <returns>a sample from the distribution.</returns> public static double Sample(System.Random rnd, int shape, double rate) { return(Gamma.Sample(rnd, shape, rate)); }
public void ValidateCumulativeDistribution(int shape, double invScale, double x, double cdf) { var n = new Gamma(shape, invScale); AssertHelpers.AlmostEqual(cdf, n.CumulativeDistribution(x), 14); AssertHelpers.AlmostEqual(cdf, Gamma.CDF(shape, invScale, x), 14); }
/// <summary> /// Generates a sample from the Erlang distribution. /// </summary> /// <returns>a sample from the distribution.</returns> public double Sample() { return(Gamma.SampleUnchecked(_random, _shape, _rate)); }