|
|
|
@ -80,16 +80,6 @@ namespace MathNet.Numerics.Distributions |
|
|
|
return "Exponential(λ = " + _rate + ")"; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks whether the parameters of the distribution are valid.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rate">The rate (λ) parameter of the distribution. Range: λ ≥ 0.</param>
|
|
|
|
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
|
|
|
|
static bool IsValidParameterSet(double rate) |
|
|
|
{ |
|
|
|
return rate >= 0.0; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the parameters of the distribution after checking their validity.
|
|
|
|
/// </summary>
|
|
|
|
@ -97,7 +87,7 @@ namespace MathNet.Numerics.Distributions |
|
|
|
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
|
|
|
|
void SetParameters(double rate) |
|
|
|
{ |
|
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(rate)) |
|
|
|
if (rate < 0.0 || Double.IsNaN(rate)) |
|
|
|
{ |
|
|
|
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); |
|
|
|
} |
|
|
|
@ -200,14 +190,10 @@ 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) |
|
|
|
{ |
|
|
|
if (x >= 0.0) |
|
|
|
{ |
|
|
|
return _rate*Math.Exp(-_rate*x); |
|
|
|
} |
|
|
|
|
|
|
|
return 0.0; |
|
|
|
return x < 0.0 ? 0.0 : _rate*Math.Exp(-_rate*x); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -215,6 +201,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(_rate) - (_rate*x); |
|
|
|
@ -225,14 +212,43 @@ 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) |
|
|
|
{ |
|
|
|
if (x >= 0.0) |
|
|
|
return x < 0.0 ? 0.0 : 1.0 - Math.Exp(-_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 p >= 1.0 ? double.PositiveInfinity : -Math.Log(1 - p)/_rate; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Draws a random sample from the distribution.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A random number from this distribution.</returns>
|
|
|
|
public double Sample() |
|
|
|
{ |
|
|
|
return SampleUnchecked(_random, _rate); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generates a sequence of samples from the Exponential distribution.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>a sequence of samples from the distribution.</returns>
|
|
|
|
public IEnumerable<double> Samples() |
|
|
|
{ |
|
|
|
while (true) |
|
|
|
{ |
|
|
|
return 1.0 - Math.Exp(-_rate*x); |
|
|
|
yield return SampleUnchecked(_random, _rate); |
|
|
|
} |
|
|
|
|
|
|
|
return 0.0; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -249,28 +265,64 @@ namespace MathNet.Numerics.Distributions |
|
|
|
r = rnd.NextDouble(); |
|
|
|
} |
|
|
|
|
|
|
|
return -Math.Log(r)/rate; |
|
|
|
return -Math.Log(r) / rate; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Draws a random sample from the distribution.
|
|
|
|
/// Computes the probability density of the distribution (PDF) at x, i.e. ∂P(X ≤ x)/∂x.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A random number from this distribution.</returns>
|
|
|
|
public double Sample() |
|
|
|
/// <param name="rate">The rate (λ) parameter 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 rate, double x) |
|
|
|
{ |
|
|
|
return SampleUnchecked(_random, _rate); |
|
|
|
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters); |
|
|
|
|
|
|
|
return x < 0.0 ? 0.0 : rate*Math.Exp(-rate*x); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Generates a sequence of samples from the Exponential distribution.
|
|
|
|
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x).
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>a sequence of samples from the distribution.</returns>
|
|
|
|
public IEnumerable<double> Samples() |
|
|
|
/// <param name="rate">The rate (λ) parameter 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 rate, double x) |
|
|
|
{ |
|
|
|
while (true) |
|
|
|
{ |
|
|
|
yield return SampleUnchecked(_random, _rate); |
|
|
|
} |
|
|
|
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters); |
|
|
|
|
|
|
|
return Math.Log(rate) - (rate*x); |
|
|
|
} |
|
|
|
|
|
|
|
/// <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="rate">The rate (λ) parameter of the distribution. Range: λ ≥ 0.</param>
|
|
|
|
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
|
|
|
|
/// <seealso cref="CumulativeDistribution"/>
|
|
|
|
public static double CDF(double rate, double x) |
|
|
|
{ |
|
|
|
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters); |
|
|
|
|
|
|
|
return x < 0.0 ? 0.0 : 1.0 - Math.Exp(-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>
|
|
|
|
/// <param name="rate">The rate (λ) parameter of the distribution. Range: λ ≥ 0.</param>
|
|
|
|
/// <returns>the inverse cumulative density at <paramref name="p"/>.</returns>
|
|
|
|
/// <seealso cref="InverseCumulativeDistribution"/>
|
|
|
|
public static double InvCDF(double rate, double p) |
|
|
|
{ |
|
|
|
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters); |
|
|
|
|
|
|
|
return p >= 1.0 ? double.PositiveInfinity : -Math.Log(1 - p)/rate; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -281,10 +333,7 @@ namespace MathNet.Numerics.Distributions |
|
|
|
/// <returns>A random number from this distribution.</returns>
|
|
|
|
public static double Sample(System.Random rnd, double rate) |
|
|
|
{ |
|
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(rate)) |
|
|
|
{ |
|
|
|
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); |
|
|
|
} |
|
|
|
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters); |
|
|
|
|
|
|
|
return SampleUnchecked(rnd, rate); |
|
|
|
} |
|
|
|
@ -297,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 rate) |
|
|
|
{ |
|
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(rate)) |
|
|
|
{ |
|
|
|
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); |
|
|
|
} |
|
|
|
if (rate < 0.0) throw new ArgumentOutOfRangeException("rate", Resources.InvalidDistributionParameters); |
|
|
|
|
|
|
|
while (true) |
|
|
|
{ |
|
|
|
|