Browse Source

Distributions: align head members, smarter RandomSource setter

optimization-1
Christoph Ruegg 13 years ago
parent
commit
6ae4b6bd5a
  1. 34
      src/Numerics/Distributions/Bernoulli.cs
  2. 40
      src/Numerics/Distributions/Beta.cs
  3. 83
      src/Numerics/Distributions/Binomial.cs
  4. 26
      src/Numerics/Distributions/Categorical.cs
  5. 65
      src/Numerics/Distributions/Cauchy.cs
  6. 59
      src/Numerics/Distributions/Chi.cs
  7. 55
      src/Numerics/Distributions/ChiSquare.cs
  8. 33
      src/Numerics/Distributions/ContinuousUniform.cs
  9. 67
      src/Numerics/Distributions/ConwayMaxwellPoisson.cs
  10. 47
      src/Numerics/Distributions/Dirichlet.cs
  11. 33
      src/Numerics/Distributions/DiscreteUniform.cs
  12. 70
      src/Numerics/Distributions/Erlang.cs
  13. 62
      src/Numerics/Distributions/Exponential.cs
  14. 63
      src/Numerics/Distributions/FisherSnedecor.cs
  15. 40
      src/Numerics/Distributions/Gamma.cs
  16. 64
      src/Numerics/Distributions/Geometric.cs
  17. 69
      src/Numerics/Distributions/Hypergeometric.cs
  18. 43
      src/Numerics/Distributions/IDistribution.cs
  19. 9
      src/Numerics/Distributions/IUnivariateDistribution.cs
  20. 78
      src/Numerics/Distributions/InverseGamma.cs
  21. 71
      src/Numerics/Distributions/InverseWishart.cs
  22. 52
      src/Numerics/Distributions/Laplace.cs
  23. 40
      src/Numerics/Distributions/LogNormal.cs
  24. 115
      src/Numerics/Distributions/MatrixNormal.cs
  25. 63
      src/Numerics/Distributions/Multinomial.cs
  26. 93
      src/Numerics/Distributions/NegativeBinomial.cs
  27. 40
      src/Numerics/Distributions/Normal.cs
  28. 60
      src/Numerics/Distributions/NormalGamma.cs
  29. 70
      src/Numerics/Distributions/Pareto.cs
  30. 52
      src/Numerics/Distributions/Poisson.cs
  31. 62
      src/Numerics/Distributions/Rayleigh.cs
  32. 74
      src/Numerics/Distributions/Stable.cs
  33. 106
      src/Numerics/Distributions/StudentT.cs
  34. 42
      src/Numerics/Distributions/Weibull.cs
  35. 7
      src/Numerics/Distributions/Wishart.cs
  36. 63
      src/Numerics/Distributions/Zipf.cs
  37. 1
      src/Numerics/Numerics.csproj
  38. 13
      src/UnitTests/DistributionTests/CommonDistributionTests.cs
  39. 8
      src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs
  40. 8
      src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs
  41. 8
      src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs

34
src/Numerics/Distributions/Bernoulli.cs

@ -47,16 +47,10 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Bernoulli : IDiscreteDistribution
{
/// <summary>
/// The probability of generating a one.
/// </summary>
double _p;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
double _p;
/// <summary>
/// Initializes a new instance of the Bernoulli class.
/// </summary>
@ -115,29 +109,21 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the probability of generating a one.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double P
public System.Random RandomSource
{
get { return _p; }
set { SetParameters(value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the probability of generating a one.
/// </summary>
public System.Random RandomSource
public double P
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
get { return _p; }
set { SetParameters(value); }
}
/// <summary>

40
src/Numerics/Distributions/Beta.cs

@ -53,21 +53,11 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Beta : IContinuousDistribution
{
/// <summary>
/// Beta shape parameter a.
/// </summary>
double _shapeA;
System.Random _random;
/// <summary>
/// Beta shape parameter b.
/// </summary>
double _shapeA;
double _shapeB;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the Beta class.
/// </summary>
@ -130,6 +120,15 @@ namespace MathNet.Numerics.Distributions
_shapeB = b;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the A shape parameter of the Beta distribution.
/// </summary>
@ -148,23 +147,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shapeA, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the Beta distribution.
/// </summary>

83
src/Numerics/Distributions/Binomial.cs

@ -47,20 +47,17 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Binomial : IDiscreteDistribution
{
System.Random _random;
/// <summary>
/// Stores the normalized binomial probability.
/// Success probability in each trial.
/// </summary>
double _p;
/// <summary>
/// The number of trials.
/// </summary>
int _n;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
int _trials;
/// <summary>
/// Initializes a new instance of the Binomial class.
@ -95,7 +92,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Binomial(Success Probability = " + _p + ", Number of Trials = " + _n + ")";
return "Binomial(Success Probability = " + _p + ", Number of Trials = " + _trials + ")";
}
/// <summary>
@ -124,7 +121,16 @@ namespace MathNet.Numerics.Distributions
}
_p = p;
_n = n;
_trials = n;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -133,7 +139,7 @@ namespace MathNet.Numerics.Distributions
public double P
{
get { return _p; }
set { SetParameters(value, _n); }
set { SetParameters(value, _trials); }
}
/// <summary>
@ -141,33 +147,16 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int N
{
get { return _n; }
get { return _trials; }
set { SetParameters(_p, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
public double Mean
{
get { return _p*_n; }
get { return _p*_trials; }
}
/// <summary>
@ -175,7 +164,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(_p*(1.0 - _p)*_n); }
get { return Math.Sqrt(_p*(1.0 - _p)*_trials); }
}
/// <summary>
@ -183,7 +172,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return _p*(1.0 - _p)*_n; }
get { return _p*(1.0 - _p)*_trials; }
}
/// <summary>
@ -199,7 +188,7 @@ namespace MathNet.Numerics.Distributions
}
var e = 0.0;
for (var i = 0; i <= _n; i++)
for (var i = 0; i <= _trials; i++)
{
var p = Probability(i);
e -= p*Math.Log(p);
@ -214,7 +203,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return (1.0 - (2.0*_p))/Math.Sqrt(_n*_p*(1.0 - _p)); }
get { return (1.0 - (2.0*_p))/Math.Sqrt(_trials*_p*(1.0 - _p)); }
}
/// <summary>
@ -230,7 +219,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Maximum
{
get { return _n; }
get { return _trials; }
}
/// <summary>
@ -242,7 +231,7 @@ namespace MathNet.Numerics.Distributions
{
if (_p == 1.0)
{
return _n;
return _trials;
}
if (_p == 0.0)
@ -250,7 +239,7 @@ namespace MathNet.Numerics.Distributions
return 0;
}
return (int) Math.Floor((_n + 1)*_p);
return (int) Math.Floor((_trials + 1)*_p);
}
}
@ -259,7 +248,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Median
{
get { return (int) Math.Floor(_p*_n); }
get { return (int) Math.Floor(_p*_trials); }
}
/// <summary>
@ -274,7 +263,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
if (k > _n)
if (k > _trials)
{
return 0.0;
}
@ -289,7 +278,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
if (_p == 1.0 && k == _n)
if (_p == 1.0 && k == _trials)
{
return 1.0;
}
@ -299,7 +288,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
return SpecialFunctions.Binomial(_n, k)*Math.Pow(_p, k)*Math.Pow(1.0 - _p, _n - k);
return SpecialFunctions.Binomial(_trials, k)*Math.Pow(_p, k)*Math.Pow(1.0 - _p, _trials - k);
}
/// <summary>
@ -314,7 +303,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
if (k > _n)
if (k > _trials)
{
return Double.NegativeInfinity;
}
@ -329,7 +318,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
if (_p == 1.0 && k == _n)
if (_p == 1.0 && k == _trials)
{
return 0.0;
}
@ -339,7 +328,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
return SpecialFunctions.BinomialLn(_n, k) + (k*Math.Log(_p)) + ((_n - k)*Math.Log(1.0 - _p));
return SpecialFunctions.BinomialLn(_trials, k) + (k*Math.Log(_p)) + ((_trials - k)*Math.Log(1.0 - _p));
}
/// <summary>
@ -354,7 +343,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
if (x > _n)
if (x > _trials)
{
return 1.0;
}
@ -362,7 +351,7 @@ namespace MathNet.Numerics.Distributions
var cdf = 0.0;
for (var i = 0; i <= (int) Math.Floor(x); i++)
{
cdf += Combinatorics.Combinations(_n, i)*Math.Pow(_p, i)*Math.Pow(1.0 - _p, _n - i);
cdf += Combinatorics.Combinations(_trials, i)*Math.Pow(_p, i)*Math.Pow(1.0 - _p, _trials - i);
}
return cdf;
@ -392,7 +381,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>The number of successes in N trials.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _p, _n);
return SampleUnchecked(RandomSource, _p, _trials);
}
/// <summary>
@ -403,7 +392,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _p, _n);
yield return SampleUnchecked(RandomSource, _p, _trials);
}
}

26
src/Numerics/Distributions/Categorical.cs

@ -53,6 +53,7 @@ namespace MathNet.Numerics.Distributions
public class Categorical : IDiscreteDistribution
{
System.Random _random;
double[] _pmfNormalized;
double[] _cdfUnnormalized;
@ -188,7 +189,15 @@ namespace MathNet.Numerics.Distributions
{
_pmfNormalized[i] = p[i]/sum;
}
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -202,23 +211,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

65
src/Numerics/Distributions/Cauchy.cs

@ -42,16 +42,10 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Cauchy : IContinuousDistribution
{
/// <summary>
/// The scale of the Cauchy distribution.
/// </summary>
double _scale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
double _scale;
/// <summary>
/// Initializes a new instance of the <see cref="Cauchy"/> class with the location parameter set to 0 and the scale parameter set to 1
/// </summary>
@ -84,6 +78,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(location, scale);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Cauchy(Location = " + Median + ", Scale = " + _scale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="location">Location parameter.</param>
/// <param name="scale">Scale parameter. Must be greater than 0.</param>
/// <returns>True 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>
@ -102,14 +116,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <param name="location">Location parameter.</param>
/// <param name="scale">Scale parameter. Must be greater than 0.</param>
/// <returns>True when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double location, double scale)
public System.Random RandomSource
{
return scale > 0.0 && !Double.IsNaN(location);
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -130,31 +142,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(Median, value); }
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Cauchy(Location = " + Median + ", Scale = " + _scale + ")";
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.

59
src/Numerics/Distributions/Chi.cs

@ -48,16 +48,13 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Chi : IContinuousDistribution
{
System.Random _random;
/// <summary>
/// Keeps track of the degrees of freedom for the Chi distribution.
/// </summary>
double _dof;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="Chi"/> class.
/// </summary>
@ -80,18 +77,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// A string representation of the distribution.
/// </summary>
/// <param name="dof">The degrees of freedom for the Chi distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double dof)
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_dof = dof;
return "Chi(DoF = " + _dof + ")";
}
/// <summary>
@ -105,38 +96,36 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the degrees of freedom of the Chi distribution.
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
public double DegreesOfFreedom
/// <param name="dof">The degrees of freedom for the Chi distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double dof)
{
get { return _dof; }
set { SetParameters(value); }
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_dof = dof;
}
/// <summary>
/// A string representation of the distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
public System.Random RandomSource
{
return "Chi(DoF = " + _dof + ")";
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the distribution's random number generator.
/// Gets or sets the degrees of freedom of the Chi distribution.
/// </summary>
public System.Random RandomSource
public double DegreesOfFreedom
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
get { return _dof; }
set { SetParameters(value); }
}
/// <summary>

55
src/Numerics/Distributions/ChiSquare.cs

@ -46,9 +46,6 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class ChiSquare : IContinuousDistribution
{
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
@ -73,18 +70,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// A string representation of the distribution.
/// </summary>
/// <param name="dof">The degrees of freedom for the <c>ChiSquare</c> distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double dof)
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Mean = dof;
return "ChiSquare(DoF = " + Mean + ")";
}
/// <summary>
@ -98,38 +89,36 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the degrees of freedom of the <c>ChiSquare</c> distribution.
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
public double DegreesOfFreedom
/// <param name="dof">The degrees of freedom for the <c>ChiSquare</c> distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double dof)
{
get { return Mean; }
set { SetParameters(value); }
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Mean = dof;
}
/// <summary>
/// A string representation of the distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
public System.Random RandomSource
{
return "ChiSquare(DoF = " + Mean + ")";
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the distribution's random number generator.
/// Gets or sets the degrees of freedom of the <c>ChiSquare</c> distribution.
/// </summary>
public System.Random RandomSource
public double DegreesOfFreedom
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
get { return Mean; }
set { SetParameters(value); }
}
/// <summary>

33
src/Numerics/Distributions/ContinuousUniform.cs

@ -46,6 +46,8 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class ContinuousUniform : IContinuousDistribution
{
System.Random _random;
/// <summary>
/// The distribution's lower bound.
/// </summary>
@ -56,11 +58,6 @@ namespace MathNet.Numerics.Distributions
/// </summary>
double _upper;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the ContinuousUniform class with lower bound 0 and upper bound 1.
/// </summary>
@ -130,6 +127,15 @@ namespace MathNet.Numerics.Distributions
_upper = upper;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the lower bound of the distribution.
/// </summary>
@ -148,23 +154,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lower, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

67
src/Numerics/Distributions/ConwayMaxwellPoisson.cs

@ -53,6 +53,8 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class ConwayMaxwellPoisson : IDiscreteDistribution
{
System.Random _random;
/// <summary>
/// Since many properties of the distribution can only be computed approximately, the tolerance
/// level specifies how much error we accept.
@ -84,11 +86,6 @@ namespace MathNet.Numerics.Distributions
/// </summary>
double _nu;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="ConwayMaxwellPoisson"/> class.
/// </summary>
@ -112,6 +109,28 @@ namespace MathNet.Numerics.Distributions
SetParameters(lambda, nu);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return "ConwayMaxwellPoisson(Lambda = " + _lambda + ", Nu = " + _nu + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="lambda">The lambda parameter.</param>
/// <param name="nu">The nu parameter.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double lambda, double nu)
{
return lambda > 0.0 && nu >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
@ -130,14 +149,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <param name="lambda">The lambda parameter.</param>
/// <param name="nu">The nu parameter.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double lambda, double nu)
public System.Random RandomSource
{
return lambda > 0.0 && nu >= 0.0;
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -160,34 +177,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lambda, value); }
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return "ConwayMaxwellPoisson(Lambda = " + _lambda + ", Nu = " + _nu + ")";
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

47
src/Numerics/Distributions/Dirichlet.cs

@ -43,18 +43,15 @@ namespace MathNet.Numerics.Distributions
/// <para>The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Dirichlet
public class Dirichlet : IDistribution
{
System.Random _random;
/// <summary>
/// The Dirichlet distribution parameters.
/// </summary>
double[] _alpha;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the Dirichlet class. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
@ -115,6 +112,17 @@ namespace MathNet.Numerics.Distributions
SetParameters(parm);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return "Dirichlet(Dimension = " + Dimension + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid: no
/// parameter can be less than zero and at least one parameter should be
@ -160,14 +168,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
public System.Random RandomSource
{
return "Dirichlet(Dimension = " + Dimension + ")";
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -308,23 +314,6 @@ namespace MathNet.Numerics.Distributions
return term + SpecialFunctions.GammaLn(sumalpha);
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Samples a Dirichlet distributed random vector.
/// </summary>

33
src/Numerics/Distributions/DiscreteUniform.cs

@ -47,6 +47,8 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class DiscreteUniform : IDiscreteDistribution
{
System.Random _random;
/// <summary>
/// The distribution's lower bound.
/// </summary>
@ -57,11 +59,6 @@ namespace MathNet.Numerics.Distributions
/// </summary>
int _upper;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the DiscreteUniform class.
/// </summary>
@ -124,6 +121,15 @@ namespace MathNet.Numerics.Distributions
_upper = upper;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the lower bound of the probability distribution.
/// </summary>
@ -142,23 +148,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lower, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

70
src/Numerics/Distributions/Erlang.cs

@ -47,21 +47,11 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Erlang : IContinuousDistribution
{
/// <summary>
/// Erlang shape parameter.
/// </summary>
double _shape;
System.Random _random;
/// <summary>
/// Erlang inverse scale parameter.
/// </summary>
double _shape;
double _invScale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="Erlang"/> class.
/// </summary>
@ -109,6 +99,26 @@ namespace MathNet.Numerics.Distributions
return new Erlang(shape, invScale);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Erlang(Shape = " + _shape + ", Inverse Scale = " + _invScale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="shape">The shape of the Erlang distribution.</param>
/// <param name="invScale">The inverse scale of the Erlang distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double shape, double invScale)
{
return shape >= 0.0 && invScale >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
@ -126,14 +136,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <param name="shape">The shape of the Erlang distribution.</param>
/// <param name="invScale">The inverse scale of the Erlang distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double shape, double invScale)
public System.Random RandomSource
{
return shape >= 0.0 && invScale >= 0.0;
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -173,32 +181,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Erlang(Shape = " + _shape + ", Inverse Scale = " + _invScale + ")";
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

62
src/Numerics/Distributions/Exponential.cs

@ -46,16 +46,10 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Exponential : IContinuousDistribution
{
/// <summary>
/// The lambda parameter of the Exponential distribution.
/// </summary>
double _lambda;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
double _lambda;
/// <summary>
/// Initializes a new instance of the <see cref="Exponential"/> class.
/// </summary>
@ -78,18 +72,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// A string representation of the distribution.
/// </summary>
/// <param name="lambda">Lambda parameter.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double lambda)
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_lambda = lambda;
return "Exponential(Lambda = " + _lambda + ")";
}
/// <summary>
@ -103,21 +91,18 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the lambda parameter of the distribution.
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
public double Lambda
/// <param name="lambda">Lambda parameter.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double lambda)
{
get { return _lambda; }
set { SetParameters(value); }
}
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Exponential(Lambda = " + _lambda + ")";
_lambda = lambda;
}
/// <summary>
@ -126,15 +111,16 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
set { _random = value ?? new System.Random(); }
}
_random = value;
}
/// <summary>
/// Gets or sets the lambda parameter of the distribution.
/// </summary>
public double Lambda
{
get { return _lambda; }
set { SetParameters(value); }
}
/// <summary>

63
src/Numerics/Distributions/FisherSnedecor.cs

@ -46,6 +46,8 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class FisherSnedecor : IContinuousDistribution
{
System.Random _random;
/// <summary>
/// The first parameter - degree of freedom.
/// </summary>
@ -56,11 +58,6 @@ namespace MathNet.Numerics.Distributions
/// </summary>
double _d2;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="FisherSnedecor"/> class.
/// </summary>
@ -84,6 +81,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(d1, d2);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "FisherSnedecor(DegreeOfFreedom1 = " + _d1 + ", DegreeOfFreedom2 = " + _d2 + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double d1, double d2)
{
return d1 > 0.0 && d2 > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
@ -101,14 +118,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double d1, double d2)
public System.Random RandomSource
{
return d1 > 0.0 && d2 > 0.0;
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -129,32 +144,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_d1, value); }
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "FisherSnedecor(DegreeOfFreedom1 = " + _d1 + ", DegreeOfFreedom2 = " + _d2 + ")";
}
/// <summary>
/// Gets or sets the distribution's random number generator.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

40
src/Numerics/Distributions/Gamma.cs

@ -54,21 +54,11 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Gamma : IContinuousDistribution
{
/// <summary>
/// Gamma shape parameter.
/// </summary>
double _shape;
System.Random _random;
/// <summary>
/// Gamma inverse scale parameter.
/// </summary>
double _shape;
double _invScale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the Gamma class.
/// </summary>
@ -153,6 +143,15 @@ namespace MathNet.Numerics.Distributions
_invScale = invScale;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the shape of the Gamma distribution.
/// </summary>
@ -190,23 +189,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the Gamma distribution.
/// </summary>

64
src/Numerics/Distributions/Geometric.cs

@ -47,16 +47,10 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Geometric : IDiscreteDistribution
{
/// <summary>
/// The geometric distribution parameter.
/// </summary>
double _p;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
double _p;
/// <summary>
/// Initializes a new instance of the Geometric class.
/// </summary>
@ -81,18 +75,14 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="p">The probability of generating a one.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double p)
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_p = p;
return "Geometric(P = " + _p + ")";
}
/// <summary>
@ -106,41 +96,37 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the probability of generating a one.
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
public double P
/// <param name="p">The probability of generating a one.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double p)
{
get { return _p; }
if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
set { SetParameters(value); }
_p = p;
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
public System.Random RandomSource
{
return "Geometric(P = " + _p + ")";
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the probability of generating a one.
/// </summary>
public System.Random RandomSource
public double P
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
get { return _p; }
_random = value;
}
set { SetParameters(value); }
}
/// <summary>

69
src/Numerics/Distributions/Hypergeometric.cs

@ -49,6 +49,8 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Hypergeometric : IDiscreteDistribution
{
System.Random _random;
/// <summary>
/// The size of the population (N).
/// </summary>
@ -64,11 +66,6 @@ namespace MathNet.Numerics.Distributions
/// </summary>
int _draws;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the Hypergeometric class.
/// </summary>
@ -94,6 +91,29 @@ namespace MathNet.Numerics.Distributions
SetParameters(population, success, draws);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return "Hypergeometric(N = " + _population + ", M = " + _success + ", n = " + _draws + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="population">The size of the population (N).</param>
/// <param name="success">The number successes within the population (K, M).</param>
/// <param name="draws">The number of draws without replacement (n).</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(int population, int success, int draws)
{
return population >= 0 && success >= 0 && draws >= 0 && (success <= population && draws <= population);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
@ -113,15 +133,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <param name="population">The size of the population (N).</param>
/// <param name="success">The number successes within the population (K, M).</param>
/// <param name="draws">The number of draws without replacement (n).</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(int population, int success, int draws)
public System.Random RandomSource
{
return population >= 0 && success >= 0 && draws >= 0 && (success <= population && draws <= population);
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -181,34 +198,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_population, _success, value); }
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return "Hypergeometric(N = " + _population + ", M = " + _success + ", n = " + _draws + ")";
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

43
src/Numerics/Distributions/IDistribution.cs

@ -0,0 +1,43 @@
// <copyright file="IUnivariateDistribution.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.Distributions
{
/// <summary>
/// The common interface for all distributions.
/// </summary>
public interface IDistribution
{
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
System.Random RandomSource { get; set; }
}
}

9
src/Numerics/Distributions/IUnivariateDistribution.cs

@ -30,18 +30,11 @@
namespace MathNet.Numerics.Distributions
{
using System;
/// <summary>
/// The interface for univariate distributions.
/// </summary>
public interface IUnivariateDistribution
public interface IUnivariateDistribution : IDistribution
{
/// <summary>
/// Gets or sets the random number generator which is used to generate random samples from the distribution.
/// </summary>
Random RandomSource { get; set; }
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

78
src/Numerics/Distributions/InverseGamma.cs

@ -47,21 +47,11 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class InverseGamma : IContinuousDistribution
{
/// <summary>
/// Inverse Gamma shape parameter.
/// </summary>
double _shape;
System.Random _random;
/// <summary>
/// Inverse Gamma scale parameter scale.
/// </summary>
double _shape;
double _scale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="InverseGamma"/> class.
/// </summary>
@ -85,15 +75,31 @@ namespace MathNet.Numerics.Distributions
SetParameters(shape, scale);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "InverseGamma(Shape = " + _shape + ", Inverse Scale = " + _scale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double shape, double scale)
{
return shape > 0.0 && scale > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="shape">
/// The shape (alpha) parameter of the inverse Gamma distribution.
/// </param>
/// <param name="scale">
/// The scale (beta) parameter of the inverse Gamma distribution.
/// </param>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double shape, double scale)
{
@ -107,14 +113,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double shape, double scale)
public System.Random RandomSource
{
return shape > 0.0 && scale > 0.0;
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -135,32 +139,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "InverseGamma(Shape = " + _shape + ", Inverse Scale = " + _scale + ")";
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

71
src/Numerics/Distributions/InverseWishart.cs

@ -46,8 +46,10 @@ namespace MathNet.Numerics.Distributions
/// <para>The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class InverseWishart
public class InverseWishart : IDistribution
{
System.Random _random;
/// <summary>
/// The degrees of freedom for the inverse Wishart distribution.
/// </summary>
@ -63,11 +65,6 @@ namespace MathNet.Numerics.Distributions
/// </summary>
Cholesky<double> _chol;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="InverseWishart"/> class.
/// </summary>
@ -100,24 +97,6 @@ namespace MathNet.Numerics.Distributions
return "InverseWishart(Nu = " + _nu + ", Rows = " + _s.RowCount + ", Columns = " + _s.ColumnCount + ")";
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double nu, Matrix<double> s)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_nu = nu;
_s = s;
_chol = Cholesky<double>.Create(_s);
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
@ -142,6 +121,33 @@ namespace MathNet.Numerics.Distributions
return nu > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double nu, Matrix<double> s)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_nu = nu;
_s = s;
_chol = Cholesky<double>.Create(_s);
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the degrees of freedom for the inverse Wishart distribution.
/// </summary>
@ -160,23 +166,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_nu, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean.
/// </summary>

52
src/Numerics/Distributions/Laplace.cs

@ -48,16 +48,10 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Laplace : IContinuousDistribution
{
/// <summary>
/// The scale of the Laplace distribution.
/// </summary>
double _scale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
double _scale;
/// <summary>
/// Gets or sets the location of the Laplace distribution.
/// </summary>
@ -110,20 +104,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// A string representation of the distribution.
/// </summary>
/// <param name="location">The location for the Laplace distribution.</param>
/// <param name="scale">The scale for the Laplace distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double location, double scale)
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Mean = location;
_scale = scale;
return "Laplace(Location = " + Mean + ", Scale = " + _scale + ")";
}
/// <summary>
@ -138,12 +124,20 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// A string representation of the distribution.
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
/// <param name="location">The location for the Laplace distribution.</param>
/// <param name="scale">The scale for the Laplace distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double location, double scale)
{
return "Laplace(Location = " + Mean + ", Scale = " + _scale + ")";
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Mean = location;
_scale = scale;
}
/// <summary>
@ -152,15 +146,7 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
set { _random = value ?? new System.Random(); }
}
/// <summary>

40
src/Numerics/Distributions/LogNormal.cs

@ -48,21 +48,11 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class LogNormal : IContinuousDistribution
{
/// <summary>
/// Keeps track of the mu of the logarithm of the log-log-normal distribution.
/// </summary>
double _mu;
System.Random _random;
/// <summary>
/// Keeps track of the standard deviation of the logarithm of the log-log-normal distribution.
/// </summary>
double _mu;
double _sigma;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="LogNormal"/> class.
/// The distribution will be initialized with the default <seealso cref="System.Random"/>
@ -149,6 +139,15 @@ namespace MathNet.Numerics.Distributions
_sigma = sigma;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the mean of the logarithm of the log-normal.
/// </summary>
@ -167,23 +166,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_mu, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mu of the log-normal distribution.
/// </summary>

115
src/Numerics/Distributions/MatrixNormal.cs

@ -47,8 +47,10 @@ namespace MathNet.Numerics.Distributions
/// <para>The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class MatrixNormal
public class MatrixNormal : IDistribution
{
System.Random _random;
/// <summary>
/// The mean of the matrix normal distribution.
/// </summary>
@ -64,11 +66,6 @@ namespace MathNet.Numerics.Distributions
/// </summary>
Matrix<double> _k;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="MatrixNormal"/> class.
/// </summary>
@ -107,55 +104,6 @@ namespace MathNet.Numerics.Distributions
return "MatrixNormal(Rows = " + _m.RowCount + ", Columns = " + _m.ColumnCount + ")";
}
/// <summary>
/// Gets or sets the mean. (M)
/// </summary>
/// <value>The mean of the distribution.</value>
public Matrix<double> Mean
{
get { return _m; }
set { SetParameters(value, _v, _k); }
}
/// <summary>
/// Gets or sets the row covariance. (V)
/// </summary>
/// <value>The row covariance.</value>
public Matrix<double> RowCovariance
{
get { return _v; }
set { SetParameters(_m, value, _k); }
}
/// <summary>
/// Gets or sets the column covariance. (K)
/// </summary>
/// <value>The column covariance.</value>
public Matrix<double> ColumnCovariance
{
get { return _k; }
set { SetParameters(_m, _v, value); }
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="m">The mean of the matrix normal.</param>
/// <param name="v">The covariance matrix for the rows.</param>
/// <param name="k">The covariance matrix for the columns.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(Matrix<double> m, Matrix<double> v, Matrix<double> k)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_m = m;
_v = v;
_k = k;
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
@ -196,21 +144,62 @@ namespace MathNet.Numerics.Distributions
return true;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="m">The mean of the matrix normal.</param>
/// <param name="v">The covariance matrix for the rows.</param>
/// <param name="k">The covariance matrix for the columns.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(Matrix<double> m, Matrix<double> v, Matrix<double> k)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_m = m;
_v = v;
_k = k;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
set { _random = value ?? new System.Random(); }
}
_random = value;
}
/// <summary>
/// Gets or sets the mean. (M)
/// </summary>
/// <value>The mean of the distribution.</value>
public Matrix<double> Mean
{
get { return _m; }
set { SetParameters(value, _v, _k); }
}
/// <summary>
/// Gets or sets the row covariance. (V)
/// </summary>
/// <value>The row covariance.</value>
public Matrix<double> RowCovariance
{
get { return _v; }
set { SetParameters(_m, value, _k); }
}
/// <summary>
/// Gets or sets the column covariance. (K)
/// </summary>
/// <value>The column covariance.</value>
public Matrix<double> ColumnCovariance
{
get { return _k; }
set { SetParameters(_m, _v, value); }
}
/// <summary>

63
src/Numerics/Distributions/Multinomial.cs

@ -50,8 +50,10 @@ namespace MathNet.Numerics.Distributions
/// <para>The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Multinomial
public class Multinomial : IDistribution
{
System.Random _random;
/// <summary>
/// Stores the normalized multinomial probabilities.
/// </summary>
@ -60,12 +62,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The number of trials.
/// </summary>
int _n;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
int _trials;
/// <summary>
/// Initializes a new instance of the Multinomial class.
@ -130,7 +127,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Multinomial(Dimension = " + _p.Length + ", Number of Trails = " + _n + ")";
return "Multinomial(Dimension = " + _p.Length + ", Number of Trails = " + _trials + ")";
}
/// <summary>
@ -177,7 +174,16 @@ namespace MathNet.Numerics.Distributions
}
_p = (double[]) p.Clone();
_n = n;
_trials = n;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -186,7 +192,7 @@ namespace MathNet.Numerics.Distributions
public double[] P
{
get { return (double[]) _p.Clone(); }
set { SetParameters(value, _n); }
set { SetParameters(value, _trials); }
}
/// <summary>
@ -194,33 +200,16 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int N
{
get { return _n; }
get { return _trials; }
set { SetParameters(_p, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
public Vector<double> Mean
{
get { return _n*(DenseVector) P; }
get { return _trials*(DenseVector) P; }
}
/// <summary>
@ -234,7 +223,7 @@ namespace MathNet.Numerics.Distributions
var res = (DenseVector) P;
for (var i = 0; i < res.Count; i++)
{
res[i] *= _n*(1 - res[i]);
res[i] *= _trials*(1 - res[i]);
}
return res;
@ -252,7 +241,7 @@ namespace MathNet.Numerics.Distributions
var res = (DenseVector) P;
for (var i = 0; i < res.Count; i++)
{
res[i] = (1.0 - (2.0*res[i]))/Math.Sqrt(_n*(1.0 - res[i])*res[i]);
res[i] = (1.0 - (2.0*res[i]))/Math.Sqrt(_trials*(1.0 - res[i])*res[i]);
}
return res;
@ -278,9 +267,9 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "x");
}
if (x.Sum() == _n)
if (x.Sum() == _trials)
{
var coef = SpecialFunctions.Multinomial(_n, x);
var coef = SpecialFunctions.Multinomial(_trials, x);
var num = 1.0;
for (var i = 0; i < x.Length; i++)
{
@ -312,9 +301,9 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "x");
}
if (x.Sum() == _n)
if (x.Sum() == _trials)
{
var coef = Math.Log(SpecialFunctions.Multinomial(_n, x));
var coef = Math.Log(SpecialFunctions.Multinomial(_trials, x));
var num = x.Select((t, i) => t*Math.Log(_p[i])).Sum();
return coef + num;
}
@ -328,7 +317,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the counts for each of the different possible values.</returns>
public int[] Sample()
{
return Sample(RandomSource, _p, _n);
return Sample(RandomSource, _p, _trials);
}
/// <summary>
@ -339,7 +328,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return Sample(RandomSource, _p, _n);
yield return Sample(RandomSource, _p, _trials);
}
}

93
src/Numerics/Distributions/NegativeBinomial.cs

@ -48,27 +48,21 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class NegativeBinomial : IDiscreteDistribution
{
/// <summary>
/// The r parameter of the distribution.
/// </summary>
double _r;
System.Random _random;
double _trials;
/// <summary>
/// The p parameter of the distribution.
/// </summary>
double _p;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Gets or sets the number of trials.
/// </summary>
public double R
{
get { return _r; }
get { return _trials; }
set { SetParameters(value, _p); }
}
@ -78,7 +72,7 @@ namespace MathNet.Numerics.Distributions
public double P
{
get { return _p; }
set { SetParameters(_r, value); }
set { SetParameters(_trials, value); }
}
/// <summary>
@ -105,20 +99,14 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="r">The number of trials.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double r, double p)
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_p = p;
_r = r;
return "NegativeBinomial(R = " + _trials + ", P = " + _p + ")";
}
/// <summary>
@ -133,39 +121,36 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
/// <param name="r">The number of trials.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double r, double p)
{
return "NegativeBinomial(R = " + _r + ", P = " + _p + ")";
if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_p = p;
_trials = r;
}
/// <summary>
/// Gets or sets the distribution's random number generator.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
public double Mean
{
get { return _r*(1.0 - _p)/_p; }
get { return _trials*(1.0 - _p)/_p; }
}
/// <summary>
@ -173,7 +158,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return _r*(1.0 - _p)/(_p*_p); }
get { return _trials*(1.0 - _p)/(_p*_p); }
}
/// <summary>
@ -181,7 +166,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(_r*(1.0 - _p))/_p; }
get { return Math.Sqrt(_trials*(1.0 - _p))/_p; }
}
/// <summary>
@ -197,7 +182,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return (2.0 - _p)/Math.Sqrt(_r*(1.0 - _p)); }
get { return (2.0 - _p)/Math.Sqrt(_trials*(1.0 - _p)); }
}
/// <summary>
@ -205,7 +190,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public int Mode
{
get { return _r > 1.0 ? (int) Math.Floor((_r - 1.0)*(1.0 - _p)/_p) : 0; }
get { return _trials > 1.0 ? (int) Math.Floor((_trials - 1.0)*(1.0 - _p)/_p) : 0; }
}
/// <summary>
@ -239,10 +224,10 @@ namespace MathNet.Numerics.Distributions
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
public double Probability(int k)
{
var ln = SpecialFunctions.GammaLn(_r + k)
- SpecialFunctions.GammaLn(_r)
var ln = SpecialFunctions.GammaLn(_trials + k)
- SpecialFunctions.GammaLn(_trials)
- SpecialFunctions.GammaLn(k + 1.0)
+ (_r*Math.Log(_p))
+ (_trials*Math.Log(_p))
+ (k*Math.Log(1.0 - _p));
return Math.Exp(ln);
}
@ -254,10 +239,10 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
public double ProbabilityLn(int k)
{
var ln = SpecialFunctions.GammaLn(_r + k)
- SpecialFunctions.GammaLn(_r)
var ln = SpecialFunctions.GammaLn(_trials + k)
- SpecialFunctions.GammaLn(_trials)
- SpecialFunctions.GammaLn(k + 1.0)
+ (_r*Math.Log(_p))
+ (_trials*Math.Log(_p))
+ (k*Math.Log(1.0 - _p));
return ln;
}
@ -269,7 +254,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 1 - SpecialFunctions.BetaRegularized(x + 1, _r, 1 - _p);
return 1 - SpecialFunctions.BetaRegularized(x + 1, _trials, 1 - _p);
}
/// <summary>
@ -299,7 +284,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _r, _p);
return SampleUnchecked(RandomSource, _trials, _p);
}
/// <summary>
@ -310,7 +295,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _r, _p);
yield return SampleUnchecked(RandomSource, _trials, _p);
}
}

40
src/Numerics/Distributions/Normal.cs

@ -47,21 +47,11 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Normal : IContinuousDistribution
{
/// <summary>
/// Keeps track of the mean of the normal distribution.
/// </summary>
double _mean;
System.Random _random;
/// <summary>
/// Keeps track of the standard deviation of the normal distribution.
/// </summary>
double _mean;
double _stdDev;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the Normal class. This is a normal distribution with mean 0.0
/// and standard deviation 1.0. The distribution will
@ -190,6 +180,15 @@ namespace MathNet.Numerics.Distributions
_stdDev = stddev;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the precision of the normal distribution.
/// </summary>
@ -211,23 +210,6 @@ namespace MathNet.Numerics.Distributions
}
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets or sets the mean of the normal distribution.
/// </summary>

60
src/Numerics/Distributions/NormalGamma.cs

@ -100,33 +100,15 @@ namespace MathNet.Numerics.Distributions
/// <para>The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class NormalGamma
public class NormalGamma : IDistribution
{
/// <summary>
/// The location of the mean.
/// </summary>
double _meanLocation;
System.Random _random;
/// <summary>
/// The scale of the mean.
/// </summary>
double _meanLocation;
double _meanScale;
/// <summary>
/// The shape of the precision.
/// </summary>
double _precisionShape;
/// <summary>
/// The inverse scale of the precision.
/// </summary>
double _precisionInvScale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="NormalGamma"/> class.
/// </summary>
@ -154,6 +136,16 @@ namespace MathNet.Numerics.Distributions
SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "NormalGamma(Mean Location = " + _meanLocation + ", Mean Scale = " + _meanScale +
", Precision Shape = " + _precisionShape + ", Precision Inverse Scale = " + _precisionInvScale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
@ -189,13 +181,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// A string representation of the distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
public System.Random RandomSource
{
return "NormalGamma(Mean Location = " + _meanLocation + ", Mean Scale = " + _meanScale +
", Precision Shape = " + _precisionShape + ", Precision Inverse Scale = " + _precisionInvScale + ")";
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -234,23 +225,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Returns the marginal distribution for the mean of the <c>NormalGamma</c> distribution.
/// </summary>

70
src/Numerics/Distributions/Pareto.cs

@ -48,21 +48,11 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Pareto : IContinuousDistribution
{
/// <summary>
/// The scale parameter of the distribution.
/// </summary>
double _scale;
System.Random _random;
/// <summary>
/// The shape parameter of the distribution.
/// </summary>
double _scale;
double _shape;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="Pareto"/> class.
/// </summary>
@ -88,6 +78,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(scale, shape);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Pareto(Scale = " + _scale + ", Shape = " + _shape + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="shape">The shape parameter of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double scale, double shape)
{
return scale > 0.0 && shape > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
@ -106,14 +116,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="shape">The shape parameter of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double scale, double shape)
public System.Random RandomSource
{
return scale > 0.0 && shape > 0.0;
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -134,32 +142,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_scale, value); }
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Pareto(Scale = " + _scale + ", Shape = " + _shape + ")";
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

52
src/Numerics/Distributions/Poisson.cs

@ -44,16 +44,10 @@ namespace MathNet.Numerics.Distributions
/// </remarks>
public class Poisson : IDiscreteDistribution
{
/// <summary>
/// The Poisson distribution parameter λ.
/// </summary>
double _lambda;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
double _lambda;
/// <summary>
/// Gets or sets the Poisson distribution parameter λ.
/// </summary>
@ -87,18 +81,14 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <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>
void SetParameters(double lambda)
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_lambda = lambda;
return "Poisson(λ = " + _lambda + ")";
}
/// <summary>
@ -112,14 +102,18 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
/// <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>
void SetParameters(double lambda)
{
return "Poisson(λ = " + _lambda + ")";
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_lambda = lambda;
}
/// <summary>
@ -128,15 +122,7 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
set { _random = value ?? new System.Random(); }
}
/// <summary>

62
src/Numerics/Distributions/Rayleigh.cs

@ -49,16 +49,10 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Rayleigh : IContinuousDistribution
{
/// <summary>
/// The scale parameter of the distribution.
/// </summary>
double _scale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
double _scale;
/// <summary>
/// Initializes a new instance of the <see cref="Rayleigh"/> class.
/// </summary>
@ -83,18 +77,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// A string representation of the distribution.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double scale)
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_scale = scale;
return "Rayleigh(Scale = " + _scale + ")";
}
/// <summary>
@ -108,21 +96,18 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the scale parameter of the distribution.
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
public double Scale
/// <param name="scale">The scale parameter of the distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double scale)
{
get { return _scale; }
set { SetParameters(value); }
}
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Rayleigh(Scale = " + _scale + ")";
_scale = scale;
}
/// <summary>
@ -131,15 +116,16 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
set { _random = value ?? new System.Random(); }
}
_random = value;
}
/// <summary>
/// Gets or sets the scale parameter of the distribution.
/// </summary>
public double Scale
{
get { return _scale; }
set { SetParameters(value); }
}
/// <summary>

74
src/Numerics/Distributions/Stable.cs

@ -49,6 +49,8 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Stable : IContinuousDistribution
{
System.Random _random;
/// <summary>
/// The stability parameter of the distribution.
/// </summary>
@ -59,21 +61,9 @@ namespace MathNet.Numerics.Distributions
/// </summary>
double _beta;
/// <summary>
/// The scale parameter of the distribution.
/// </summary>
double _scale;
/// <summary>
/// The location parameter of the distribution.
/// </summary>
double _location;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="Stable"/> class.
/// </summary>
@ -101,6 +91,28 @@ namespace MathNet.Numerics.Distributions
SetParameters(alpha, beta, scale, location);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Stable(" + "Stability = " + _alpha + ", Skewness = " + _beta + ", Scale = " + _scale + ", Location = " + _location + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double alpha, double beta, double scale, double location)
{
return alpha > 0.0 && alpha <= 2.0 && beta >= -1.0 && beta <= 1.0 && scale > 0.0 && !Double.IsNaN(location);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
@ -122,16 +134,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double alpha, double beta, double scale, double location)
public System.Random RandomSource
{
return alpha > 0.0 && alpha <= 2.0 && beta >= -1.0 && beta <= 1.0 && scale > 0.0 && !Double.IsNaN(location);
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -170,32 +178,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_alpha, _beta, _scale, value); }
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Stable(" + "Stability = " + _alpha + ", Skewness = " + _beta + ", Scale = " + _scale + ", Location = " + _location + ")";
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

106
src/Numerics/Distributions/StudentT.cs

@ -57,25 +57,11 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class StudentT : IContinuousDistribution
{
/// <summary>
/// Keeps track of the location of the Student t-distribution.
/// </summary>
double _location;
/// <summary>
/// Keeps track of the degrees of freedom for the Student t-distribution.
/// </summary>
double _dof;
System.Random _random;
/// <summary>
/// Keeps track of the scale for the Student t-distribution.
/// </summary>
double _location;
double _scale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
double _freedom;
/// <summary>
/// Initializes a new instance of the StudentT class. This is a Student t-distribution with location 0.0
@ -122,7 +108,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "StudentT(Location = " + _location + ", Scale = " + _scale + ", DoF = " + _dof + ")";
return "StudentT(Location = " + _location + ", Scale = " + _scale + ", DoF = " + _freedom + ")";
}
/// <summary>
@ -153,7 +139,16 @@ namespace MathNet.Numerics.Distributions
_location = location;
_scale = scale;
_dof = dof;
_freedom = dof;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -162,7 +157,7 @@ namespace MathNet.Numerics.Distributions
public double Location
{
get { return _location; }
set { SetParameters(value, _scale, _dof); }
set { SetParameters(value, _scale, _freedom); }
}
/// <summary>
@ -171,7 +166,7 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_location, value, _dof); }
set { SetParameters(_location, value, _freedom); }
}
/// <summary>
@ -179,33 +174,16 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double DegreesOfFreedom
{
get { return _dof; }
get { return _freedom; }
set { SetParameters(_location, _scale, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the Student t-distribution.
/// </summary>
public double Mean
{
get { return _dof > 1.0 ? _location : Double.NaN; }
get { return _freedom > 1.0 ? _location : Double.NaN; }
}
/// <summary>
@ -215,17 +193,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_dof))
if (Double.IsPositiveInfinity(_freedom))
{
return _scale*_scale;
}
if (_dof > 2.0)
if (_freedom > 2.0)
{
return _dof*_scale*_scale/(_dof - 2.0);
return _freedom*_scale*_scale/(_freedom - 2.0);
}
return _dof > 1.0 ? Double.PositiveInfinity : Double.NaN;
return _freedom > 1.0 ? Double.PositiveInfinity : Double.NaN;
}
}
@ -236,17 +214,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_dof))
if (Double.IsPositiveInfinity(_freedom))
{
return Math.Sqrt(_scale*_scale);
}
if (_dof > 2.0)
if (_freedom > 2.0)
{
return Math.Sqrt(_dof*_scale*_scale/(_dof - 2.0));
return Math.Sqrt(_freedom*_scale*_scale/(_freedom - 2.0));
}
return _dof > 1.0 ? Double.PositiveInfinity : Double.NaN;
return _freedom > 1.0 ? Double.PositiveInfinity : Double.NaN;
}
}
@ -262,7 +240,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
return (((_dof + 1.0)/2.0)*(SpecialFunctions.DiGamma((1.0 + _dof)/2.0) - SpecialFunctions.DiGamma(_dof/2.0))) + Math.Log(Math.Sqrt(_dof)*SpecialFunctions.Beta(_dof/2.0, 1.0/2.0));
return (((_freedom + 1.0)/2.0)*(SpecialFunctions.DiGamma((1.0 + _freedom)/2.0) - SpecialFunctions.DiGamma(_freedom/2.0))) + Math.Log(Math.Sqrt(_freedom)*SpecialFunctions.Beta(_freedom/2.0, 1.0/2.0));
}
}
@ -273,7 +251,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (_dof <= 3)
if (_freedom <= 3)
{
throw new NotSupportedException();
}
@ -322,15 +300,15 @@ namespace MathNet.Numerics.Distributions
public double Density(double x)
{
// TODO JVG we can probably do a better job for Cauchy special case
if (_dof >= 1e+8d)
if (_freedom >= 1e+8d)
{
return Normal.Density(_location, _scale, x);
}
var d = (x - _location)/_scale;
return Math.Exp(SpecialFunctions.GammaLn((_dof + 1.0)/2.0) - SpecialFunctions.GammaLn(_dof/2.0))
*Math.Pow(1.0 + (d*d/_dof), -0.5*(_dof + 1.0))
/Math.Sqrt(_dof*Math.PI)
return Math.Exp(SpecialFunctions.GammaLn((_freedom + 1.0)/2.0) - SpecialFunctions.GammaLn(_freedom/2.0))
*Math.Pow(1.0 + (d*d/_freedom), -0.5*(_freedom + 1.0))
/Math.Sqrt(_freedom*Math.PI)
/_scale;
}
@ -342,16 +320,16 @@ namespace MathNet.Numerics.Distributions
public double DensityLn(double x)
{
// TODO JVG we can probably do a better job for Cauchy special case
if (_dof >= 1e+8d)
if (_freedom >= 1e+8d)
{
return Normal.DensityLn(_location, _scale, x);
}
var d = (x - _location)/_scale;
return SpecialFunctions.GammaLn((_dof + 1.0)/2.0)
- (0.5*((_dof + 1.0)*Math.Log(1.0 + (d*d/_dof))))
- SpecialFunctions.GammaLn(_dof/2.0)
- (0.5*Math.Log(_dof*Math.PI)) - Math.Log(_scale);
return SpecialFunctions.GammaLn((_freedom + 1.0)/2.0)
- (0.5*((_freedom + 1.0)*Math.Log(1.0 + (d*d/_freedom))))
- SpecialFunctions.GammaLn(_freedom/2.0)
- (0.5*Math.Log(_freedom*Math.PI)) - Math.Log(_scale);
}
/// <summary>
@ -362,14 +340,14 @@ namespace MathNet.Numerics.Distributions
public double CumulativeDistribution(double x)
{
// TODO JVG we can probably do a better job for Cauchy special case
if (Double.IsPositiveInfinity(_dof))
if (Double.IsPositiveInfinity(_freedom))
{
return Normal.CumulativeDistribution(_location, _scale, x);
}
var k = (x - _location)/_scale;
var h = _dof/(_dof + (k*k));
var ib = 0.5*SpecialFunctions.BetaRegularized(_dof/2.0, 0.5, h);
var h = _freedom/(_freedom + (k*k));
var ib = 0.5*SpecialFunctions.BetaRegularized(_freedom/2.0, 0.5, h);
return x <= _location ? ib : 1.0 - ib;
}
@ -396,7 +374,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _location, _scale, _dof);
return SampleUnchecked(RandomSource, _location, _scale, _freedom);
}
/// <summary>
@ -407,7 +385,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _location, _scale, _dof);
yield return SampleUnchecked(RandomSource, _location, _scale, _freedom);
}
}

42
src/Numerics/Distributions/Weibull.cs

@ -48,18 +48,13 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Weibull : IContinuousDistribution
{
/// <summary>
/// Weibull shape parameter.
/// </summary>
double _shape;
System.Random _random;
/// <summary>
/// Weibull inverse scale parameter.
/// </summary>
double _shape;
double _scale;
/// <summary>
/// Reusable intermediate result 1 / (<see cref="_scale"/> ^ <see cref="_shape"/>)
/// Reusable intermediate result 1 / (_scale ^ _shape)
/// </summary>
/// <remarks>
/// By caching this parameter we can get slightly better numerics precision
@ -67,11 +62,6 @@ namespace MathNet.Numerics.Distributions
/// </remarks>
double _scalePowShapeInv;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the Weibull class.
/// </summary>
@ -133,6 +123,15 @@ namespace MathNet.Numerics.Distributions
_scalePowShapeInv = Math.Pow(scale, -shape);
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the shape of the Weibull distribution.
/// </summary>
@ -151,23 +150,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the Weibull distribution.
/// </summary>

7
src/Numerics/Distributions/Wishart.cs

@ -50,6 +50,8 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Wishart
{
System.Random _random;
/// <summary>
/// The degrees of freedom for the Wishart distribution.
/// </summary>
@ -65,11 +67,6 @@ namespace MathNet.Numerics.Distributions
/// </summary>
Cholesky<double> _chol;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="Wishart"/> class.
/// </summary>

63
src/Numerics/Distributions/Zipf.cs

@ -49,6 +49,8 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Zipf : IDiscreteDistribution
{
System.Random _random;
/// <summary>
/// The s parameter of the distribution.
/// </summary>
@ -59,11 +61,6 @@ namespace MathNet.Numerics.Distributions
/// </summary>
int _n;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary>
/// Initializes a new instance of the <see cref="Zipf"/> class.
/// </summary>
@ -87,6 +84,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(s, n);
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Zipf(S = " + _s + ", N = " + _n + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="s">The s parameter of the distribution.</param>
/// <param name="n">The n parameter of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double s, int n)
{
return n > 0 && s > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
@ -104,14 +121,12 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <param name="s">The s parameter of the distribution.</param>
/// <param name="n">The n parameter of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double s, int n)
public System.Random RandomSource
{
return n > 0 && s > 0.0;
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -132,32 +147,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_s, value); }
}
/// <summary>
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Zipf(S = " + _s + ", N = " + _n + ")";
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

1
src/Numerics/Numerics.csproj

@ -95,6 +95,7 @@
<Compile Include="Distributions\ChiSquare.cs" />
<Compile Include="Distributions\ContinuousUniform.cs" />
<Compile Include="Distributions\ConwayMaxwellPoisson.cs" />
<Compile Include="Distributions\IDistribution.cs" />
<Compile Include="Distributions\Dirichlet.cs" />
<Compile Include="Distributions\DiscreteUniform.cs" />
<Compile Include="Distributions\Erlang.cs" />

13
src/UnitTests/DistributionTests/CommonDistributionTests.cs

@ -160,22 +160,19 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
}
}
/// <summary>
/// Fail set random source with <c>null</c> reference.
/// </summary>
[Test]
public void FailSetRandomSourceWithNullReference()
public void HasRandomSourceEvenAfterSetToNull()
{
foreach (var dd in _discreteDistributions)
{
var dd1 = dd;
Assert.Throws<ArgumentNullException>(() => dd1.RandomSource = null);
Assert.DoesNotThrow(() => dd.RandomSource = null);
Assert.IsNotNull(dd.RandomSource);
}
foreach (var cd in _continuousDistributions)
{
var cd1 = cd;
Assert.Throws<ArgumentNullException>(() => cd1.RandomSource = null);
Assert.DoesNotThrow(() => cd.RandomSource = null);
Assert.IsNotNull(cd.RandomSource);
}
}

8
src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs

@ -114,14 +114,12 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
};
}
/// <summary>
/// Fail set random source with <c>null</c> reference.
/// </summary>
[Test]
public void FailSetRandomSourceWithNullReference()
public void HasRandomSourceEvenAfterSetToNull()
{
var d = new Dirichlet(0.3, 5);
Assert.Throws<ArgumentNullException>(() => d.RandomSource = null);
Assert.DoesNotThrow(() => d.RandomSource = null);
Assert.IsNotNull(d.RandomSource);
}
/// <summary>

8
src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs

@ -121,14 +121,12 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
};
}
/// <summary>
/// Fail set random source with <c>null</c> reference.
/// </summary>
[Test]
public void FailSetRandomSourceWithNullReference()
public void HasRandomSourceEvenAfterSetToNull()
{
var d = new InverseWishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2));
Assert.Throws<ArgumentNullException>(() => d.RandomSource = null);
Assert.DoesNotThrow(() => d.RandomSource = null);
Assert.IsNotNull(d.RandomSource);
}
/// <summary>

8
src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs

@ -130,16 +130,14 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
};
}
/// <summary>
/// Fail set random source with <c>null</c> reference.
/// </summary>
[Test]
public void FailSetRandomSourceWithNullReference()
public void HasRandomSourceEvenAfterSetToNull()
{
const int N = 2;
const int P = 3;
var d = new MatrixNormal(MatrixLoader.GenerateRandomDenseMatrix(N, P), MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(N), MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(P));
Assert.Throws<ArgumentNullException>(() => d.RandomSource = null);
Assert.DoesNotThrow(() => d.RandomSource = null);
Assert.IsNotNull(d.RandomSource);
}
/// <summary>

Loading…
Cancel
Save