|
|
@ -43,27 +43,21 @@ namespace MathNet.Numerics.Distributions |
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// The Poisson distribution parameter λ.
|
|
|
/// The Poisson distribution parameter λ.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
private double _lambda; |
|
|
double _lambda; |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// The distribution's random number generator.
|
|
|
/// The distribution's random number generator.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
private Random _random; |
|
|
Random _random; |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Gets or sets the Poisson distribution parameter λ.
|
|
|
/// Gets or sets the Poisson distribution parameter λ.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public double Lambda |
|
|
public double Lambda |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return _lambda; } |
|
|
{ |
|
|
|
|
|
return _lambda; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
set |
|
|
set { SetParameters(value); } |
|
|
{ |
|
|
|
|
|
SetParameters(value); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -82,7 +76,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <param name="lambda">The mean (λ) of the distribution.</param>
|
|
|
/// <param name="lambda">The mean (λ) of the distribution.</param>
|
|
|
/// <exception cref="System.ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
|
|
|
/// <exception cref="System.ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
|
|
|
private void SetParameters(double lambda) |
|
|
void SetParameters(double lambda) |
|
|
{ |
|
|
{ |
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) |
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) |
|
|
{ |
|
|
{ |
|
|
@ -97,7 +91,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <param name="lambda">The mean (λ) of the distribution.</param>
|
|
|
/// <param name="lambda">The mean (λ) of the distribution.</param>
|
|
|
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
|
|
|
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
|
|
|
private static bool IsValidParameterSet(double lambda) |
|
|
static bool IsValidParameterSet(double lambda) |
|
|
{ |
|
|
{ |
|
|
return lambda > 0.00; |
|
|
return lambda > 0.00; |
|
|
} |
|
|
} |
|
|
@ -120,10 +114,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public Random RandomSource |
|
|
public Random RandomSource |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return _random; } |
|
|
{ |
|
|
|
|
|
return _random; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
set |
|
|
set |
|
|
{ |
|
|
{ |
|
|
@ -141,10 +132,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public double Mean |
|
|
public double Mean |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return _lambda; } |
|
|
{ |
|
|
|
|
|
return _lambda; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -152,10 +140,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public double Variance |
|
|
public double Variance |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return _lambda; } |
|
|
{ |
|
|
|
|
|
return _lambda; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -163,10 +148,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public double StdDev |
|
|
public double StdDev |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return Math.Sqrt(_lambda); } |
|
|
{ |
|
|
|
|
|
return Math.Sqrt(_lambda); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -175,10 +157,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// <remarks>Approximation, see Wikipedia <a href="http://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution</a></remarks>
|
|
|
/// <remarks>Approximation, see Wikipedia <a href="http://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution</a></remarks>
|
|
|
public double Entropy |
|
|
public double Entropy |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return (0.5 * Math.Log(2 * Constants.Pi * Constants.E * _lambda)) - (1.0 / (12.0 * _lambda)) - (1.0 / (24.0 * _lambda * _lambda)) - (19.0 / (360.0 * _lambda * _lambda * _lambda)); } |
|
|
{ |
|
|
|
|
|
return (0.5 * Math.Log(2 * Constants.Pi * Constants.E * _lambda)) - (1.0 / (12.0 * _lambda)) - (1.0 / (24.0 * _lambda * _lambda)) - (19.0 / (360.0 * _lambda * _lambda * _lambda)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -186,10 +165,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public double Skewness |
|
|
public double Skewness |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return 1.0 / Math.Sqrt(_lambda); } |
|
|
{ |
|
|
|
|
|
return 1.0 / Math.Sqrt(_lambda); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -197,10 +173,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public int Minimum |
|
|
public int Minimum |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return 0; } |
|
|
{ |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -208,10 +181,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public int Maximum |
|
|
public int Maximum |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return int.MaxValue; } |
|
|
{ |
|
|
|
|
|
return int.MaxValue; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -233,10 +203,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public int Mode |
|
|
public int Mode |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return (int)Math.Floor(_lambda); } |
|
|
{ |
|
|
|
|
|
return (int)Math.Floor(_lambda); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -245,10 +212,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// <remarks>Approximation, see Wikipedia <a href="http://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution</a></remarks>
|
|
|
/// <remarks>Approximation, see Wikipedia <a href="http://en.wikipedia.org/wiki/Poisson_distribution">Poisson distribution</a></remarks>
|
|
|
public int Median |
|
|
public int Median |
|
|
{ |
|
|
{ |
|
|
get |
|
|
get { return (int)Math.Floor(_lambda + (1.0 / 3.0) - (0.02 / _lambda)); } |
|
|
{ |
|
|
|
|
|
return (int)Math.Floor(_lambda + (1.0 / 3.0) - (0.02 / _lambda)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
@ -271,71 +235,15 @@ namespace MathNet.Numerics.Distributions |
|
|
return -_lambda + (k * Math.Log(_lambda)) - SpecialFunctions.FactorialLn(k); |
|
|
return -_lambda + (k * Math.Log(_lambda)) - SpecialFunctions.FactorialLn(k); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Samples a Poisson distributed random variable.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>A sample from the Poisson distribution.</returns>
|
|
|
|
|
|
public int Sample() |
|
|
|
|
|
{ |
|
|
|
|
|
return DoSample(RandomSource, _lambda); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Samples an array of Poisson distributed random variables.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>a sequence of successes in N trials.</returns>
|
|
|
|
|
|
public IEnumerable<int> Samples() |
|
|
|
|
|
{ |
|
|
|
|
|
while (true) |
|
|
|
|
|
{ |
|
|
|
|
|
yield return DoSample(RandomSource, _lambda); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Samples a Poisson distributed random variable.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="rnd">The random number generator to use.</param>
|
|
|
|
|
|
/// <param name="lambda">The Poisson distribution parameter λ.</param>
|
|
|
|
|
|
/// <returns>A sample from the Poisson distribution.</returns>
|
|
|
|
|
|
public static int Sample(Random rnd, double lambda) |
|
|
|
|
|
{ |
|
|
|
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return DoSample(rnd, lambda); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Samples a sequence of Poisson distributed random variables.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="rnd">The random number generator to use.</param>
|
|
|
|
|
|
/// <param name="lambda">The Poisson distribution parameter λ.</param>
|
|
|
|
|
|
/// <returns>a sequence of samples from the distribution.</returns>
|
|
|
|
|
|
public static IEnumerable<int> Samples(Random rnd, double lambda) |
|
|
|
|
|
{ |
|
|
|
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while (true) |
|
|
|
|
|
{ |
|
|
|
|
|
yield return DoSample(rnd, lambda); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Generates one sample from the Poisson distribution.
|
|
|
/// Generates one sample from the Poisson distribution.
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <param name="rnd">The random source to use.</param>
|
|
|
/// <param name="rnd">The random source to use.</param>
|
|
|
/// <param name="lambda">The Poisson distribution parameter λ.</param>
|
|
|
/// <param name="lambda">The Poisson distribution parameter λ.</param>
|
|
|
/// <returns>A random sample from the Poisson distribution.</returns>
|
|
|
/// <returns>A random sample from the Poisson distribution.</returns>
|
|
|
private static int DoSample(Random rnd, double lambda) |
|
|
internal static int SampleUnchecked(Random rnd, double lambda) |
|
|
{ |
|
|
{ |
|
|
return (lambda < 30.0) ? DoSampleShort(rnd, lambda) : DoSampleLarge(rnd, lambda); |
|
|
return (lambda < 30.0) ? DoSampleShort(rnd, lambda) : DoSampleLarge(rnd, lambda); |
|
|
} |
|
|
} |
|
|
@ -346,7 +254,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// <param name="rnd">The random source to use.</param>
|
|
|
/// <param name="rnd">The random source to use.</param>
|
|
|
/// <param name="lambda">The Poisson distribution parameter λ.</param>
|
|
|
/// <param name="lambda">The Poisson distribution parameter λ.</param>
|
|
|
/// <returns>A random sample from the Poisson distribution.</returns>
|
|
|
/// <returns>A random sample from the Poisson distribution.</returns>
|
|
|
private static int DoSampleShort(Random rnd, double lambda) |
|
|
static int DoSampleShort(Random rnd, double lambda) |
|
|
{ |
|
|
{ |
|
|
var limit = Math.Exp(-lambda); |
|
|
var limit = Math.Exp(-lambda); |
|
|
var count = 0; |
|
|
var count = 0; |
|
|
@ -367,7 +275,7 @@ namespace MathNet.Numerics.Distributions |
|
|
/// <remarks>"Rejection method PA" from "The Computer Generation of Poisson Random Variables" by A. C. Atkinson,
|
|
|
/// <remarks>"Rejection method PA" from "The Computer Generation of Poisson Random Variables" by A. C. Atkinson,
|
|
|
/// Journal of the Royal Statistical Society Series C (Applied Statistics) Vol. 28, No. 1. (1979)
|
|
|
/// Journal of the Royal Statistical Society Series C (Applied Statistics) Vol. 28, No. 1. (1979)
|
|
|
/// The article is on pages 29-35. The algorithm given here is on page 32. </remarks>
|
|
|
/// The article is on pages 29-35. The algorithm given here is on page 32. </remarks>
|
|
|
private static int DoSampleLarge(Random rnd, double lambda) |
|
|
static int DoSampleLarge(Random rnd, double lambda) |
|
|
{ |
|
|
{ |
|
|
var c = 0.767 - (3.36 / lambda); |
|
|
var c = 0.767 - (3.36 / lambda); |
|
|
var beta = Math.PI / Math.Sqrt(3.0 * lambda); |
|
|
var beta = Math.PI / Math.Sqrt(3.0 * lambda); |
|
|
@ -395,5 +303,61 @@ namespace MathNet.Numerics.Distributions |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Samples a Poisson distributed random variable.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>A sample from the Poisson distribution.</returns>
|
|
|
|
|
|
public int Sample() |
|
|
|
|
|
{ |
|
|
|
|
|
return SampleUnchecked(RandomSource, _lambda); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Samples an array of Poisson distributed random variables.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>a sequence of successes in N trials.</returns>
|
|
|
|
|
|
public IEnumerable<int> Samples() |
|
|
|
|
|
{ |
|
|
|
|
|
while (true) |
|
|
|
|
|
{ |
|
|
|
|
|
yield return SampleUnchecked(RandomSource, _lambda); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Samples a Poisson distributed random variable.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="rnd">The random number generator to use.</param>
|
|
|
|
|
|
/// <param name="lambda">The Poisson distribution parameter λ.</param>
|
|
|
|
|
|
/// <returns>A sample from the Poisson distribution.</returns>
|
|
|
|
|
|
public static int Sample(Random rnd, double lambda) |
|
|
|
|
|
{ |
|
|
|
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return SampleUnchecked(rnd, lambda); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Samples a sequence of Poisson distributed random variables.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="rnd">The random number generator to use.</param>
|
|
|
|
|
|
/// <param name="lambda">The Poisson distribution parameter λ.</param>
|
|
|
|
|
|
/// <returns>a sequence of samples from the distribution.</returns>
|
|
|
|
|
|
public static IEnumerable<int> Samples(Random rnd, double lambda) |
|
|
|
|
|
{ |
|
|
|
|
|
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
while (true) |
|
|
|
|
|
{ |
|
|
|
|
|
yield return SampleUnchecked(rnd, lambda); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|