Browse Source

Distributions: adapt Laplace

pull/163/head
Christoph Ruegg 13 years ago
parent
commit
c710944044
  1. 97
      src/Numerics/Distributions/Laplace.cs
  2. 12
      src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs

97
src/Numerics/Distributions/Laplace.cs

@ -95,17 +95,6 @@ namespace MathNet.Numerics.Distributions
return "Laplace(μ = " + _location + ", b = " + _scale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (b) of the distribution. Range: b > 0.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double location, double scale)
{
return scale > 0.0 && !Double.IsNaN(location);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
@ -114,7 +103,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double location, double scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
if (scale <= 0.0 || Double.IsNaN(location) || Double.IsNaN(scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
@ -227,6 +216,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 Math.Exp(-Math.Abs(x - _location)/_scale)/(2.0*_scale);
@ -237,9 +227,10 @@ 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(Density(x));
return -Math.Abs(x - _location)/_scale - Math.Log(2.0*_scale);
}
/// <summary>
@ -247,11 +238,33 @@ 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 0.5*(1.0 + (Math.Sign(x - _location)*(1.0 - Math.Exp(-Math.Abs(x - _location)/_scale))));
}
/// <summary>
/// Samples a Laplace distributed random variable.
/// </summary>
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(_random, _location, _scale);
}
/// <summary>
/// Generates a sample from the Laplace distribution.
/// </summary>
/// <returns>a sample from the distribution.</returns>
public IEnumerable<double> Samples()
{
while (true)
{
yield return SampleUnchecked(_random, _location, _scale);
}
}
/// <summary>
/// Samples the distribution.
/// </summary>
@ -262,28 +275,52 @@ namespace MathNet.Numerics.Distributions
static double SampleUnchecked(System.Random rnd, double location, double scale)
{
var u = rnd.NextDouble() - 0.5;
return location - (scale*Math.Sign(u)*Math.Log(1.0 - (2.0*Math.Abs(u))));
return location - (scale * Math.Sign(u) * Math.Log(1.0 - (2.0 * Math.Abs(u))));
}
/// <summary>
/// Samples a Laplace distributed random variable.
/// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x.
/// </summary>
/// <returns>a sample from the distribution.</returns>
public double Sample()
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (b) of the distribution. Range: b > 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 location, double scale, double x)
{
return SampleUnchecked(_random, _location, _scale);
if (scale <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
return Math.Exp(-Math.Abs(x - location)/scale)/(2.0*scale);
}
/// <summary>
/// Generates a sample from the Laplace distribution.
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x).
/// </summary>
/// <returns>a sample from the distribution.</returns>
public IEnumerable<double> Samples()
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (b) of the distribution. Range: b > 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 location, double scale, double x)
{
while (true)
{
yield return SampleUnchecked(_random, _location, _scale);
}
if (scale <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
return -Math.Abs(x - location)/scale - Math.Log(2.0*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="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (b) of the distribution. Range: b > 0.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
/// <seealso cref="CumulativeDistribution"/>
public static double CDF(double location, double scale, double x)
{
if (scale <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
return 0.5*(1.0 + (Math.Sign(x - location)*(1.0 - Math.Exp(-Math.Abs(x - location)/scale))));
}
/// <summary>
@ -295,10 +332,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double location, double scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
if (scale <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
return SampleUnchecked(rnd, location, scale);
}
@ -312,10 +346,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double location, double scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
if (scale <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
while (true)
{

12
src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs

@ -313,7 +313,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateDensity(double location, double scale, double x)
{
var n = new Laplace(location, scale);
Assert.AreEqual(Math.Exp(-Math.Abs(x - location) / scale) / (2.0 * scale), n.Density(x));
double expected = Math.Exp(-Math.Abs(x - location)/scale)/(2.0*scale);
Assert.AreEqual(expected, n.Density(x));
Assert.AreEqual(expected, Laplace.PDF(location, scale, x));
}
/// <summary>
@ -346,7 +348,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateDensityLn(double location, double scale, double x)
{
var n = new Laplace(location, scale);
Assert.AreEqual(-Math.Log(2.0 * scale) - (Math.Abs(x - location) / scale), n.DensityLn(x));
double expected = -Math.Log(2.0*scale) - (Math.Abs(x - location)/scale);
Assert.AreEqual(expected, n.DensityLn(x));
Assert.AreEqual(expected, Laplace.PDFLn(location, scale, x));
}
/// <summary>
@ -400,7 +404,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateCumulativeDistribution(double location, double scale, double x)
{
var n = new Laplace(location, scale);
Assert.AreEqual(0.5 * (1.0 + (Math.Sign(x - location) * (1.0 - Math.Exp(-Math.Abs(x - location) / scale)))), n.CumulativeDistribution(x));
double expected = 0.5*(1.0 + (Math.Sign(x - location)*(1.0 - Math.Exp(-Math.Abs(x - location)/scale))));
Assert.AreEqual(expected, n.CumulativeDistribution(x));
Assert.AreEqual(expected, Laplace.CDF(location, scale, x));
}
}
}

Loading…
Cancel
Save