Browse Source

Distributions: adapt Laplace

optimization-1
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 + ")"; 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
@ -114,7 +103,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception> /// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double location, double scale) 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); throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
} }
@ -227,6 +216,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns> /// <returns>the density at <paramref name="x"/>.</returns>
/// <seealso cref="PDF"/>
public double Density(double x) public double Density(double x)
{ {
return Math.Exp(-Math.Abs(x - _location)/_scale)/(2.0*_scale); return Math.Exp(-Math.Abs(x - _location)/_scale)/(2.0*_scale);
@ -237,9 +227,10 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
/// <seealso cref="PDFLn"/>
public double DensityLn(double x) public double DensityLn(double x)
{ {
return Math.Log(Density(x)); return -Math.Abs(x - _location)/_scale - Math.Log(2.0*_scale);
} }
/// <summary> /// <summary>
@ -247,11 +238,33 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
/// <seealso cref="CDF"/>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
return 0.5*(1.0 + (Math.Sign(x - _location)*(1.0 - Math.Exp(-Math.Abs(x - _location)/_scale)))); 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> /// <summary>
/// Samples the distribution. /// Samples the distribution.
/// </summary> /// </summary>
@ -262,28 +275,52 @@ namespace MathNet.Numerics.Distributions
static double SampleUnchecked(System.Random rnd, double location, double scale) static double SampleUnchecked(System.Random rnd, double location, double scale)
{ {
var u = rnd.NextDouble() - 0.5; 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> /// <summary>
/// Samples a Laplace distributed random variable. /// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x.
/// </summary> /// </summary>
/// <returns>a sample from the distribution.</returns> /// <param name="location">The location (μ) of the distribution.</param>
public double Sample() /// <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> /// <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> /// </summary>
/// <returns>a sample from the distribution.</returns> /// <param name="location">The location (μ) of the distribution.</param>
public IEnumerable<double> Samples() /// <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) if (scale <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
{
yield return SampleUnchecked(_random, _location, _scale); 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> /// <summary>
@ -295,10 +332,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double location, double scale) public static double Sample(System.Random rnd, double location, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale)) if (scale <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return SampleUnchecked(rnd, location, scale); return SampleUnchecked(rnd, location, scale);
} }
@ -312,10 +346,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double location, double scale) public static IEnumerable<double> Samples(System.Random rnd, double location, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale)) if (scale <= 0.0) throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true) 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) public void ValidateDensity(double location, double scale, double x)
{ {
var n = new Laplace(location, scale); 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> /// <summary>
@ -346,7 +348,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateDensityLn(double location, double scale, double x) public void ValidateDensityLn(double location, double scale, double x)
{ {
var n = new Laplace(location, scale); 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> /// <summary>
@ -400,7 +404,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateCumulativeDistribution(double location, double scale, double x) public void ValidateCumulativeDistribution(double location, double scale, double x)
{ {
var n = new Laplace(location, scale); 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