Browse Source

Distributions: adapt Rayleigh, add InvCDF

optimization-1
Christoph Ruegg 13 years ago
parent
commit
0d385148c0
  1. 103
      src/Numerics/Distributions/Rayleigh.cs
  2. 44
      src/UnitTests/DistributionTests/Continuous/RayleighTests.cs

103
src/Numerics/Distributions/Rayleigh.cs

@ -85,16 +85,6 @@ namespace MathNet.Numerics.Distributions
return "Rayleigh(σ = " + _scale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double scale)
{
return scale > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
@ -102,7 +92,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
if (scale <= 0.0 || Double.IsNaN(scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
@ -205,6 +195,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
/// <seealso cref="PDF"/>
public double Density(double x)
{
return (x/(_scale*_scale))*Math.Exp(-x*x/(2.0*_scale*_scale));
@ -215,6 +206,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
/// <seealso cref="PDFLn"/>
public double DensityLn(double x)
{
return Math.Log(x/(_scale*_scale)) - (x*x/(2.0*_scale*_scale));
@ -225,20 +217,22 @@ namespace MathNet.Numerics.Distributions
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
/// <seealso cref="CDF"/>
public double CumulativeDistribution(double x)
{
return 1.0 - Math.Exp(-x*x/(2.0*_scale*_scale));
}
/// <summary>
/// Generates a sample from the Rayleigh distribution without doing parameter checking.
/// 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="rnd">The random number generator to use.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <returns>a random number from the Rayleigh distribution.</returns>
static double SampleUnchecked(System.Random rnd, double scale)
/// <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 scale*Math.Sqrt(-2.0*Math.Log(rnd.NextDouble()));
return _scale*Math.Sqrt(-2*Math.Log(1 - p));
}
/// <summary>
@ -247,7 +241,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
return SampleUnchecked(_random, _scale);
return _scale*Math.Sqrt(-2.0*Math.Log(_random.NextDouble()));
}
/// <summary>
@ -258,10 +252,67 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(_random, _scale);
yield return _scale*Math.Sqrt(-2.0*Math.Log(_random.NextDouble()));
}
}
/// <summary>
/// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x.
/// </summary>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
/// <seealso cref="Density"/>
public static double PDF(double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
return (x/(scale*scale))*Math.Exp(-x*x/(2.0*scale*scale));
}
/// <summary>
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x).
/// </summary>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
/// <seealso cref="DensityLn"/>
public static double PDFLn(double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
return Math.Log(x/(scale*scale)) - (x*x/(2.0*scale*scale));
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X ≤ x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
/// <seealso cref="CumulativeDistribution"/>
public static double CDF(double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
return 1.0 - Math.Exp(-x*x/(2.0*scale*scale));
}
/// <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="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <returns>the inverse cumulative density at <paramref name="p"/>.</returns>
/// <seealso cref="InverseCumulativeDistribution"/>
public static double InvCDF(double scale, double p)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
return scale*Math.Sqrt(-2*Math.Log(1 - p));
}
/// <summary>
/// Generates a sample from the distribution.
/// </summary>
@ -270,12 +321,9 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
return SampleUnchecked(rnd, scale);
return scale*Math.Sqrt(-2.0*Math.Log(rnd.NextDouble()));
}
/// <summary>
@ -286,14 +334,11 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
if (scale <= 0.0) throw new ArgumentOutOfRangeException("scale", Resources.InvalidDistributionParameters);
while (true)
{
yield return SampleUnchecked(rnd, scale);
yield return scale*Math.Sqrt(-2.0*Math.Log(rnd.NextDouble()));
}
}
}

44
src/UnitTests/DistributionTests/Continuous/RayleighTests.cs

@ -241,11 +241,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual(Double.PositiveInfinity, n.Maximum);
}
/// <summary>
/// Validate density.
/// </summary>
/// <param name="scale">Scale value.</param>
/// <param name="x">Input X value.</param>
[TestCase(0.1, 0.1)]
[TestCase(1.0, 1.0)]
[TestCase(10.0, 10.0)]
@ -253,14 +248,11 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateDensity(double scale, double x)
{
var n = new Rayleigh(scale);
Assert.AreEqual((x / (scale * scale)) * Math.Exp(-x * x / (2.0 * scale * scale)), n.Density(x));
double expected = (x/(scale*scale))*Math.Exp(-x*x/(2.0*scale*scale));
Assert.AreEqual(expected, n.Density(x));
Assert.AreEqual(expected, Rayleigh.PDF(scale, x));
}
/// <summary>
/// Validate density log.
/// </summary>
/// <param name="scale">Scale value.</param>
/// <param name="x">Input X value.</param>
[TestCase(0.1, 0.1)]
[TestCase(1.0, 1.0)]
[TestCase(10.0, 10.0)]
@ -268,7 +260,20 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateDensityLn(double scale, double x)
{
var n = new Rayleigh(scale);
Assert.AreEqual(Math.Log(x / (scale * scale)) - (x * (x / (2.0 * (scale * scale)))), n.DensityLn(x));
double expected = Math.Log(x/(scale*scale)) - (x*(x/(2.0*(scale*scale))));
Assert.AreEqual(expected, n.DensityLn(x));
Assert.AreEqual(expected, Rayleigh.PDFLn(scale, x));
}
[TestCase(0.1, 0.1)]
[TestCase(1.0, 1.0)]
[TestCase(10.0, 10.0)]
public void ValidateInverseCumulativeDistribution(double scale, double x)
{
var n = new Rayleigh(scale);
double cdf = 1.0 - Math.Exp(-x*x/(2.0*scale*scale));
Assert.AreEqual(x, n.InverseCumulativeDistribution(cdf));
Assert.AreEqual(x, Rayleigh.InvCDF(scale, cdf));
}
/// <summary>
@ -291,20 +296,5 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
var ied = n.Samples();
ied.Take(5).ToArray();
}
/// <summary>
/// Validate cumulative distribution.
/// </summary>
/// <param name="scale">Scale value.</param>
/// <param name="x">Input X value.</param>
[TestCase(0.1, 0.1)]
[TestCase(1.0, 1.0)]
[TestCase(10.0, 10.0)]
[TestCase(Double.PositiveInfinity, Double.PositiveInfinity)]
public void ValidateCumulativeDistribution(double scale, double x)
{
var n = new Rayleigh(scale);
Assert.AreEqual(1.0 - Math.Exp(-x * x / (2.0 * scale * scale)), n.CumulativeDistribution(x));
}
}
}

Loading…
Cancel
Save