diff --git a/src/Numerics/Distributions/Laplace.cs b/src/Numerics/Distributions/Laplace.cs
index 172cb65e..225f43fb 100644
--- a/src/Numerics/Distributions/Laplace.cs
+++ b/src/Numerics/Distributions/Laplace.cs
@@ -95,17 +95,6 @@ namespace MathNet.Numerics.Distributions
return "Laplace(μ = " + _location + ", b = " + _scale + ")";
}
- ///
- /// Checks whether the parameters of the distribution are valid.
- ///
- /// The location (μ) of the distribution.
- /// The scale (b) of the distribution. Range: b > 0.
- /// true when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(double location, double scale)
- {
- return scale > 0.0 && !Double.IsNaN(location);
- }
-
///
/// Sets the parameters of the distribution after checking their validity.
///
@@ -114,7 +103,7 @@ namespace MathNet.Numerics.Distributions
/// When the parameters are out of range.
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
///
/// The location at which to compute the density.
/// the density at .
+ ///
public double Density(double x)
{
return Math.Exp(-Math.Abs(x - _location)/_scale)/(2.0*_scale);
@@ -237,9 +227,10 @@ namespace MathNet.Numerics.Distributions
///
/// The location at which to compute the log density.
/// the log density at .
+ ///
public double DensityLn(double x)
{
- return Math.Log(Density(x));
+ return -Math.Abs(x - _location)/_scale - Math.Log(2.0*_scale);
}
///
@@ -247,11 +238,33 @@ namespace MathNet.Numerics.Distributions
///
/// The location at which to compute the cumulative distribution function.
/// the cumulative distribution at location .
+ ///
public double CumulativeDistribution(double x)
{
return 0.5*(1.0 + (Math.Sign(x - _location)*(1.0 - Math.Exp(-Math.Abs(x - _location)/_scale))));
}
+ ///
+ /// Samples a Laplace distributed random variable.
+ ///
+ /// a sample from the distribution.
+ public double Sample()
+ {
+ return SampleUnchecked(_random, _location, _scale);
+ }
+
+ ///
+ /// Generates a sample from the Laplace distribution.
+ ///
+ /// a sample from the distribution.
+ public IEnumerable Samples()
+ {
+ while (true)
+ {
+ yield return SampleUnchecked(_random, _location, _scale);
+ }
+ }
+
///
/// Samples the distribution.
///
@@ -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))));
}
///
- /// Samples a Laplace distributed random variable.
+ /// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x.
///
- /// a sample from the distribution.
- public double Sample()
+ /// The location (μ) of the distribution.
+ /// The scale (b) of the distribution. Range: b > 0.
+ /// The location at which to compute the density.
+ /// the density at .
+ ///
+ 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);
}
///
- /// 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).
///
- /// a sample from the distribution.
- public IEnumerable Samples()
+ /// The location (μ) of the distribution.
+ /// The scale (b) of the distribution. Range: b > 0.
+ /// The location at which to compute the density.
+ /// the log density at .
+ ///
+ 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);
+ }
+
+ ///
+ /// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X ≤ x).
+ ///
+ /// The location at which to compute the cumulative distribution function.
+ /// The location (μ) of the distribution.
+ /// The scale (b) of the distribution. Range: b > 0.
+ /// the cumulative distribution at location .
+ ///
+ 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))));
}
///
@@ -295,10 +332,7 @@ namespace MathNet.Numerics.Distributions
/// a sample from the distribution.
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
/// a sequence of samples from the distribution.
public static IEnumerable 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)
{
diff --git a/src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs b/src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs
index 6a06af3a..d26be734 100644
--- a/src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs
+++ b/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));
}
///
@@ -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));
}
///
@@ -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));
}
}
}