Browse Source

Distributions: Gamma.InvCDF

optimization-3
Christoph Ruegg 13 years ago
parent
commit
9d0787dc4d
  1. 28
      src/Numerics/Distributions/Gamma.cs
  2. 27
      src/UnitTests/DistributionTests/Continuous/GammaTests.cs

28
src/Numerics/Distributions/Gamma.cs

@ -360,6 +360,18 @@ namespace MathNet.Numerics.Distributions
return CDF(_shape, _rate, x);
}
/// <summary>
/// Computes the inverse of the cumulative distribution function (InvCDF) for the distribution
/// at the given probability. This is also known as the quantile or percent point function.
/// </summary>
/// <param name="p">The location at which to compute the inverse cumulative density.</param>
/// <returns>the inverse cumulative density at <paramref name="p"/>.</returns>
/// <seealso cref="InvCDF"/>
public double InverseCumulativeDistribution(double p)
{
return InvCDF(_shape, _rate, p);
}
/// <summary>
/// Generates a sample from the Gamma distribution.
/// </summary>
@ -491,6 +503,22 @@ namespace MathNet.Numerics.Distributions
return SpecialFunctions.GammaLowerRegularized(shape, x*rate);
}
/// <summary>
/// Computes the inverse of the cumulative distribution function (InvCDF) for the distribution
/// at the given probability. This is also known as the quantile or percent point function.
/// </summary>
/// <param name="p">The location at which to compute the inverse cumulative density.</param>
/// <param name="shape">The shape (k, α) of the Gamma distribution. Range: α ≥ 0.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution. Range: β ≥ 0.</param>
/// <returns>the inverse cumulative density at <paramref name="p"/>.</returns>
/// <seealso cref="InverseCumulativeDistribution"/>
public static double InvCDF(double shape, double rate, double p)
{
if (shape < 0.0 || rate < 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
return SpecialFunctions.GammaLowerRegularizedInv(shape, p)/rate;
}
/// <summary>
/// Generates a sample from the Gamma distribution.
/// </summary>

27
src/UnitTests/DistributionTests/Continuous/GammaTests.cs

@ -498,9 +498,30 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(10, Double.PositiveInfinity, 10.0, 1.0)]
public void ValidateCumulativeDistribution(int shape, double invScale, double x, double cdf)
{
var n = new Gamma(shape, invScale);
AssertHelpers.AlmostEqualRelative(cdf, n.CumulativeDistribution(x), 13);
AssertHelpers.AlmostEqualRelative(cdf, Gamma.CDF(shape, invScale, x), 13);
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));
}
/// <summary>
/// Validate inverse cumulative distribution.
/// </summary>
/// <param name="shape">Shape value.</param>
/// <param name="invScale">Inverse scale value.</param>
/// <param name="x">Input X value.</param>
/// <param name="cdf">Expected value.</param>
[TestCase(1, 0.1, 1.0, 0.095162581964040431858607615783064404690935346242622848)]
[TestCase(1, 0.1, 10.0, 0.63212055882855767840447622983853913255418886896823196)]
[TestCase(1, 1.0, 1.0, 0.63212055882855767840447622983853913255418886896823196)]
[TestCase(1, 1.0, 10.0, 0.99995460007023751514846440848443944938976208191113396)]
[TestCase(10, 10.0, 1.0, 0.54207028552814779168583514294066541824736464003242184)]
[TestCase(10, 1.0, 1.0, 0.00000011142547833872067735305068724025236288094949815466035)]
[TestCase(10, 1.0, 10.0, 0.54207028552814779168583514294066541824736464003242184)]
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));
}
}
}

Loading…
Cancel
Save