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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Bernoulli : IDiscreteDistribution 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; System.Random _random;
double _p;
/// <summary> /// <summary>
/// Initializes a new instance of the Bernoulli class. /// Initializes a new instance of the Bernoulli class.
/// </summary> /// </summary>
@ -115,29 +109,21 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
public double P public System.Random RandomSource
{ {
get { return _p; } get { return _random; }
set { SetParameters(value); } set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <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> /// </summary>
public System.Random RandomSource public double P
{ {
get { return _random; } get { return _p; }
set set { SetParameters(value); }
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
} }
/// <summary> /// <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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Beta : IContinuousDistribution public class Beta : IContinuousDistribution
{ {
/// <summary> System.Random _random;
/// Beta shape parameter a.
/// </summary>
double _shapeA;
/// <summary> double _shapeA;
/// Beta shape parameter b.
/// </summary>
double _shapeB; double _shapeB;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Beta class. /// Initializes a new instance of the Beta class.
/// </summary> /// </summary>
@ -130,6 +120,15 @@ namespace MathNet.Numerics.Distributions
_shapeB = b; _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> /// <summary>
/// Gets or sets the A shape parameter of the Beta distribution. /// Gets or sets the A shape parameter of the Beta distribution.
/// </summary> /// </summary>
@ -148,23 +147,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shapeA, value); } 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> /// <summary>
/// Gets the mean of the Beta distribution. /// Gets the mean of the Beta distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Binomial : IDiscreteDistribution public class Binomial : IDiscreteDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// Stores the normalized binomial probability. /// Success probability in each trial.
/// </summary> /// </summary>
double _p; double _p;
/// <summary> /// <summary>
/// The number of trials. /// The number of trials.
/// </summary> /// </summary>
int _n; int _trials;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Binomial class. /// Initializes a new instance of the Binomial class.
@ -95,7 +92,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns> /// <returns>a string representation of the distribution.</returns>
public override string ToString() public override string ToString()
{ {
return "Binomial(Success Probability = " + _p + ", Number of Trials = " + _n + ")"; return "Binomial(Success Probability = " + _p + ", Number of Trials = " + _trials + ")";
} }
/// <summary> /// <summary>
@ -124,7 +121,16 @@ namespace MathNet.Numerics.Distributions
} }
_p = p; _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> /// <summary>
@ -133,7 +139,7 @@ namespace MathNet.Numerics.Distributions
public double P public double P
{ {
get { return _p; } get { return _p; }
set { SetParameters(value, _n); } set { SetParameters(value, _trials); }
} }
/// <summary> /// <summary>
@ -141,33 +147,16 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public int N public int N
{ {
get { return _n; } get { return _trials; }
set { SetParameters(_p, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </summary>
public double Mean public double Mean
{ {
get { return _p*_n; } get { return _p*_trials; }
} }
/// <summary> /// <summary>
@ -175,7 +164,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double StdDev public double StdDev
{ {
get { return Math.Sqrt(_p*(1.0 - _p)*_n); } get { return Math.Sqrt(_p*(1.0 - _p)*_trials); }
} }
/// <summary> /// <summary>
@ -183,7 +172,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double Variance public double Variance
{ {
get { return _p*(1.0 - _p)*_n; } get { return _p*(1.0 - _p)*_trials; }
} }
/// <summary> /// <summary>
@ -199,7 +188,7 @@ namespace MathNet.Numerics.Distributions
} }
var e = 0.0; var e = 0.0;
for (var i = 0; i <= _n; i++) for (var i = 0; i <= _trials; i++)
{ {
var p = Probability(i); var p = Probability(i);
e -= p*Math.Log(p); e -= p*Math.Log(p);
@ -214,7 +203,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double Skewness 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> /// <summary>
@ -230,7 +219,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public int Maximum public int Maximum
{ {
get { return _n; } get { return _trials; }
} }
/// <summary> /// <summary>
@ -242,7 +231,7 @@ namespace MathNet.Numerics.Distributions
{ {
if (_p == 1.0) if (_p == 1.0)
{ {
return _n; return _trials;
} }
if (_p == 0.0) if (_p == 0.0)
@ -250,7 +239,7 @@ namespace MathNet.Numerics.Distributions
return 0; 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> /// </summary>
public int Median public int Median
{ {
get { return (int) Math.Floor(_p*_n); } get { return (int) Math.Floor(_p*_trials); }
} }
/// <summary> /// <summary>
@ -274,7 +263,7 @@ namespace MathNet.Numerics.Distributions
return 0.0; return 0.0;
} }
if (k > _n) if (k > _trials)
{ {
return 0.0; return 0.0;
} }
@ -289,7 +278,7 @@ namespace MathNet.Numerics.Distributions
return 0.0; return 0.0;
} }
if (_p == 1.0 && k == _n) if (_p == 1.0 && k == _trials)
{ {
return 1.0; return 1.0;
} }
@ -299,7 +288,7 @@ namespace MathNet.Numerics.Distributions
return 0.0; 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> /// <summary>
@ -314,7 +303,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity; return Double.NegativeInfinity;
} }
if (k > _n) if (k > _trials)
{ {
return Double.NegativeInfinity; return Double.NegativeInfinity;
} }
@ -329,7 +318,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity; return Double.NegativeInfinity;
} }
if (_p == 1.0 && k == _n) if (_p == 1.0 && k == _trials)
{ {
return 0.0; return 0.0;
} }
@ -339,7 +328,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity; 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> /// <summary>
@ -354,7 +343,7 @@ namespace MathNet.Numerics.Distributions
return 0.0; return 0.0;
} }
if (x > _n) if (x > _trials)
{ {
return 1.0; return 1.0;
} }
@ -362,7 +351,7 @@ namespace MathNet.Numerics.Distributions
var cdf = 0.0; var cdf = 0.0;
for (var i = 0; i <= (int) Math.Floor(x); i++) 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; return cdf;
@ -392,7 +381,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>The number of successes in N trials.</returns> /// <returns>The number of successes in N trials.</returns>
public int Sample() public int Sample()
{ {
return SampleUnchecked(RandomSource, _p, _n); return SampleUnchecked(RandomSource, _p, _trials);
} }
/// <summary> /// <summary>
@ -403,7 +392,7 @@ namespace MathNet.Numerics.Distributions
{ {
while (true) 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 public class Categorical : IDiscreteDistribution
{ {
System.Random _random; System.Random _random;
double[] _pmfNormalized; double[] _pmfNormalized;
double[] _cdfUnnormalized; double[] _cdfUnnormalized;
@ -188,7 +189,15 @@ namespace MathNet.Numerics.Distributions
{ {
_pmfNormalized[i] = p[i]/sum; _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> /// <summary>
@ -202,23 +211,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Cauchy : IContinuousDistribution 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; System.Random _random;
double _scale;
/// <summary> /// <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 /// 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> /// </summary>
@ -84,6 +78,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(location, scale); 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
@ -102,14 +116,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <param name="location">Location parameter.</param> public System.Random RandomSource
/// <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); get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -130,31 +142,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(Median, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// 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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Chi : IContinuousDistribution public class Chi : IContinuousDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// Keeps track of the degrees of freedom for the Chi distribution. /// Keeps track of the degrees of freedom for the Chi distribution.
/// </summary> /// </summary>
double _dof; double _dof;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Chi"/> class. /// Initializes a new instance of the <see cref="Chi"/> class.
/// </summary> /// </summary>
@ -80,18 +77,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// A string representation of the distribution.
/// </summary> /// </summary>
/// <param name="dof">The degrees of freedom for the Chi distribution.</param> /// <returns>a string representation of the distribution.</returns>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception> public override string ToString()
void SetParameters(double dof)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof)) return "Chi(DoF = " + _dof + ")";
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_dof = dof;
} }
/// <summary> /// <summary>
@ -105,38 +96,36 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Gets or sets the degrees of freedom of the Chi distribution. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </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; } if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
set { SetParameters(value); } {
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_dof = dof;
} }
/// <summary> /// <summary>
/// A string representation of the distribution. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
/// <returns>a string representation of the distribution.</returns> public System.Random RandomSource
public override string ToString()
{ {
return "Chi(DoF = " + _dof + ")"; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
/// Gets or sets the distribution's random number generator. /// Gets or sets the degrees of freedom of the Chi distribution.
/// </summary> /// </summary>
public System.Random RandomSource public double DegreesOfFreedom
{ {
get { return _random; } get { return _dof; }
set set { SetParameters(value); }
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
} }
/// <summary> /// <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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class ChiSquare : IContinuousDistribution public class ChiSquare : IContinuousDistribution
{ {
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random; System.Random _random;
/// <summary> /// <summary>
@ -73,18 +70,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// A string representation of the distribution.
/// </summary> /// </summary>
/// <param name="dof">The degrees of freedom for the <c>ChiSquare</c> distribution.</param> /// <returns>a string representation of the distribution.</returns>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception> public override string ToString()
void SetParameters(double dof)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof)) return "ChiSquare(DoF = " + Mean + ")";
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Mean = dof;
} }
/// <summary> /// <summary>
@ -98,38 +89,36 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </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; } if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
set { SetParameters(value); } {
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Mean = dof;
} }
/// <summary> /// <summary>
/// A string representation of the distribution. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
/// <returns>a string representation of the distribution.</returns> public System.Random RandomSource
public override string ToString()
{ {
return "ChiSquare(DoF = " + Mean + ")"; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
/// Gets or sets the distribution's random number generator. /// Gets or sets the degrees of freedom of the <c>ChiSquare</c> distribution.
/// </summary> /// </summary>
public System.Random RandomSource public double DegreesOfFreedom
{ {
get { return _random; } get { return Mean; }
set set { SetParameters(value); }
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
} }
/// <summary> /// <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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class ContinuousUniform : IContinuousDistribution public class ContinuousUniform : IContinuousDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// The distribution's lower bound. /// The distribution's lower bound.
/// </summary> /// </summary>
@ -56,11 +58,6 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
double _upper; double _upper;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the ContinuousUniform class with lower bound 0 and upper bound 1. /// Initializes a new instance of the ContinuousUniform class with lower bound 0 and upper bound 1.
/// </summary> /// </summary>
@ -130,6 +127,15 @@ namespace MathNet.Numerics.Distributions
_upper = upper; _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> /// <summary>
/// Gets or sets the lower bound of the distribution. /// Gets or sets the lower bound of the distribution.
/// </summary> /// </summary>
@ -148,23 +154,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lower, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class ConwayMaxwellPoisson : IDiscreteDistribution public class ConwayMaxwellPoisson : IDiscreteDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// Since many properties of the distribution can only be computed approximately, the tolerance /// Since many properties of the distribution can only be computed approximately, the tolerance
/// level specifies how much error we accept. /// level specifies how much error we accept.
@ -84,11 +86,6 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
double _nu; double _nu;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ConwayMaxwellPoisson"/> class. /// Initializes a new instance of the <see cref="ConwayMaxwellPoisson"/> class.
/// </summary> /// </summary>
@ -112,6 +109,28 @@ namespace MathNet.Numerics.Distributions
SetParameters(lambda, nu); 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
@ -130,14 +149,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <param name="lambda">The lambda parameter.</param> public System.Random RandomSource
/// <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; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -160,34 +177,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lambda, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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 /// <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 /// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Dirichlet public class Dirichlet : IDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// The Dirichlet distribution parameters. /// The Dirichlet distribution parameters.
/// </summary> /// </summary>
double[] _alpha; double[] _alpha;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Dirichlet class. The distribution will /// Initializes a new instance of the Dirichlet class. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator. /// be initialized with the default <seealso cref="System.Random"/> random number generator.
@ -115,6 +112,17 @@ namespace MathNet.Numerics.Distributions
SetParameters(parm); 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> /// <summary>
/// Checks whether the parameters of the distribution are valid: no /// Checks whether the parameters of the distribution are valid: no
/// parameter can be less than zero and at least one parameter should be /// parameter can be less than zero and at least one parameter should be
@ -160,14 +168,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <returns> public System.Random RandomSource
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{ {
return "Dirichlet(Dimension = " + Dimension + ")"; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -308,23 +314,6 @@ namespace MathNet.Numerics.Distributions
return term + SpecialFunctions.GammaLn(sumalpha); 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> /// <summary>
/// Samples a Dirichlet distributed random vector. /// Samples a Dirichlet distributed random vector.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class DiscreteUniform : IDiscreteDistribution public class DiscreteUniform : IDiscreteDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// The distribution's lower bound. /// The distribution's lower bound.
/// </summary> /// </summary>
@ -57,11 +59,6 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
int _upper; int _upper;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the DiscreteUniform class. /// Initializes a new instance of the DiscreteUniform class.
/// </summary> /// </summary>
@ -124,6 +121,15 @@ namespace MathNet.Numerics.Distributions
_upper = upper; _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> /// <summary>
/// Gets or sets the lower bound of the probability distribution. /// Gets or sets the lower bound of the probability distribution.
/// </summary> /// </summary>
@ -142,23 +148,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lower, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Erlang : IContinuousDistribution public class Erlang : IContinuousDistribution
{ {
/// <summary> System.Random _random;
/// Erlang shape parameter.
/// </summary>
double _shape;
/// <summary> double _shape;
/// Erlang inverse scale parameter.
/// </summary>
double _invScale; double _invScale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Erlang"/> class. /// Initializes a new instance of the <see cref="Erlang"/> class.
/// </summary> /// </summary>
@ -109,6 +99,26 @@ namespace MathNet.Numerics.Distributions
return new Erlang(shape, invScale); 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
@ -126,14 +136,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <param name="shape">The shape of the Erlang distribution.</param> public System.Random RandomSource
/// <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; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -173,32 +181,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Exponential : IContinuousDistribution 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; System.Random _random;
double _lambda;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Exponential"/> class. /// Initializes a new instance of the <see cref="Exponential"/> class.
/// </summary> /// </summary>
@ -78,18 +72,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// A string representation of the distribution.
/// </summary> /// </summary>
/// <param name="lambda">Lambda parameter.</param> /// <returns>a string representation of the distribution.</returns>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception> public override string ToString()
void SetParameters(double lambda)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) return "Exponential(Lambda = " + _lambda + ")";
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_lambda = lambda;
} }
/// <summary> /// <summary>
@ -103,21 +91,18 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Gets or sets the lambda parameter of the distribution. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </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; } if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
set { SetParameters(value); } {
} throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
/// <summary> _lambda = lambda;
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Exponential(Lambda = " + _lambda + ")";
} }
/// <summary> /// <summary>
@ -126,15 +111,16 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set { _random = value ?? new System.Random(); }
{ }
if (value == null)
{
throw new ArgumentNullException();
}
_random = value; /// <summary>
} /// Gets or sets the lambda parameter of the distribution.
/// </summary>
public double Lambda
{
get { return _lambda; }
set { SetParameters(value); }
} }
/// <summary> /// <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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class FisherSnedecor : IContinuousDistribution public class FisherSnedecor : IContinuousDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// The first parameter - degree of freedom. /// The first parameter - degree of freedom.
/// </summary> /// </summary>
@ -56,11 +58,6 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
double _d2; double _d2;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FisherSnedecor"/> class. /// Initializes a new instance of the <see cref="FisherSnedecor"/> class.
/// </summary> /// </summary>
@ -84,6 +81,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(d1, d2); 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
@ -101,14 +118,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <param name="d1">The first parameter - degree of freedom.</param> public System.Random RandomSource
/// <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; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -129,32 +144,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_d1, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Gamma : IContinuousDistribution public class Gamma : IContinuousDistribution
{ {
/// <summary> System.Random _random;
/// Gamma shape parameter.
/// </summary>
double _shape;
/// <summary> double _shape;
/// Gamma inverse scale parameter.
/// </summary>
double _invScale; double _invScale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Gamma class. /// Initializes a new instance of the Gamma class.
/// </summary> /// </summary>
@ -153,6 +143,15 @@ namespace MathNet.Numerics.Distributions
_invScale = invScale; _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> /// <summary>
/// Gets or sets the shape of the Gamma distribution. /// Gets or sets the shape of the Gamma distribution.
/// </summary> /// </summary>
@ -190,23 +189,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); } 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> /// <summary>
/// Gets the mean of the Gamma distribution. /// Gets the mean of the Gamma distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Geometric : IDiscreteDistribution public class Geometric : IDiscreteDistribution
{ {
/// <summary>
/// The geometric distribution parameter.
/// </summary>
double _p;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random; System.Random _random;
double _p;
/// <summary> /// <summary>
/// Initializes a new instance of the Geometric class. /// Initializes a new instance of the Geometric class.
/// </summary> /// </summary>
@ -81,18 +75,14 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Returns a <see cref="System.String"/> that represents this instance.
/// </summary> /// </summary>
/// <param name="p">The probability of generating a one.</param> /// <returns>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception> /// A <see cref="System.String"/> that represents this instance.
void SetParameters(double p) /// </returns>
public override string ToString()
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(p)) return "Geometric(P = " + _p + ")";
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_p = p;
} }
/// <summary> /// <summary>
@ -106,41 +96,37 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Gets or sets the probability of generating a one. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </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> /// <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> /// </summary>
/// <returns> public System.Random RandomSource
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{ {
return "Geometric(P = " + _p + ")"; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <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> /// </summary>
public System.Random RandomSource public double P
{ {
get { return _random; } get { return _p; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value; set { SetParameters(value); }
}
} }
/// <summary> /// <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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Hypergeometric : IDiscreteDistribution public class Hypergeometric : IDiscreteDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// The size of the population (N). /// The size of the population (N).
/// </summary> /// </summary>
@ -64,11 +66,6 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
int _draws; int _draws;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Hypergeometric class. /// Initializes a new instance of the Hypergeometric class.
/// </summary> /// </summary>
@ -94,6 +91,29 @@ namespace MathNet.Numerics.Distributions
SetParameters(population, success, draws); 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
@ -113,15 +133,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <param name="population">The size of the population (N).</param> public System.Random RandomSource
/// <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); get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -181,34 +198,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_population, _success, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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 namespace MathNet.Numerics.Distributions
{ {
using System;
/// <summary> /// <summary>
/// The interface for univariate distributions. /// The interface for univariate distributions.
/// </summary> /// </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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class InverseGamma : IContinuousDistribution public class InverseGamma : IContinuousDistribution
{ {
/// <summary> System.Random _random;
/// Inverse Gamma shape parameter.
/// </summary>
double _shape;
/// <summary> double _shape;
/// Inverse Gamma scale parameter scale.
/// </summary>
double _scale; double _scale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="InverseGamma"/> class. /// Initializes a new instance of the <see cref="InverseGamma"/> class.
/// </summary> /// </summary>
@ -85,15 +75,31 @@ namespace MathNet.Numerics.Distributions
SetParameters(shape, scale); 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
/// <param name="shape"> /// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// The shape (alpha) parameter of the inverse Gamma distribution. /// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// </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> /// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double shape, double scale) void SetParameters(double shape, double scale)
{ {
@ -107,14 +113,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param> public System.Random RandomSource
/// <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; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -135,32 +139,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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 /// <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 /// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class InverseWishart public class InverseWishart : IDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// The degrees of freedom for the inverse Wishart distribution. /// The degrees of freedom for the inverse Wishart distribution.
/// </summary> /// </summary>
@ -63,11 +65,6 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
Cholesky<double> _chol; Cholesky<double> _chol;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="InverseWishart"/> class. /// Initializes a new instance of the <see cref="InverseWishart"/> class.
/// </summary> /// </summary>
@ -100,24 +97,6 @@ namespace MathNet.Numerics.Distributions
return "InverseWishart(Nu = " + _nu + ", Rows = " + _s.RowCount + ", Columns = " + _s.ColumnCount + ")"; 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> /// <summary>
/// Checks whether the parameters of the distribution are valid. /// Checks whether the parameters of the distribution are valid.
/// </summary> /// </summary>
@ -142,6 +121,33 @@ namespace MathNet.Numerics.Distributions
return nu > 0.0; 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> /// <summary>
/// Gets or sets the degrees of freedom for the inverse Wishart distribution. /// Gets or sets the degrees of freedom for the inverse Wishart distribution.
/// </summary> /// </summary>
@ -160,23 +166,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_nu, value); } 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> /// <summary>
/// Gets the mean. /// Gets the mean.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Laplace : IContinuousDistribution 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; System.Random _random;
double _scale;
/// <summary> /// <summary>
/// Gets or sets the location of the Laplace distribution. /// Gets or sets the location of the Laplace distribution.
/// </summary> /// </summary>
@ -110,20 +104,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// A string representation of the distribution.
/// </summary> /// </summary>
/// <param name="location">The location for the Laplace distribution.</param> /// <returns>a string representation of the distribution.</returns>
/// <param name="scale">The scale for the Laplace distribution.</param> public override string ToString()
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double location, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale)) return "Laplace(Location = " + Mean + ", Scale = " + _scale + ")";
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Mean = location;
_scale = scale;
} }
/// <summary> /// <summary>
@ -138,12 +124,20 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// A string representation of the distribution. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
/// <returns>a string representation of the distribution.</returns> /// <param name="location">The location for the Laplace distribution.</param>
public override string ToString() /// <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> /// <summary>
@ -152,15 +146,7 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set { _random = value ?? new System.Random(); }
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
} }
/// <summary> /// <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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class LogNormal : IContinuousDistribution public class LogNormal : IContinuousDistribution
{ {
/// <summary> System.Random _random;
/// Keeps track of the mu of the logarithm of the log-log-normal distribution.
/// </summary>
double _mu;
/// <summary> double _mu;
/// Keeps track of the standard deviation of the logarithm of the log-log-normal distribution.
/// </summary>
double _sigma; double _sigma;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="LogNormal"/> class. /// Initializes a new instance of the <see cref="LogNormal"/> class.
/// The distribution will be initialized with the default <seealso cref="System.Random"/> /// The distribution will be initialized with the default <seealso cref="System.Random"/>
@ -149,6 +139,15 @@ namespace MathNet.Numerics.Distributions
_sigma = sigma; _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> /// <summary>
/// Gets or sets the mean of the logarithm of the log-normal. /// Gets or sets the mean of the logarithm of the log-normal.
/// </summary> /// </summary>
@ -167,23 +166,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_mu, value); } 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> /// <summary>
/// Gets the mu of the log-normal distribution. /// Gets the mu of the log-normal distribution.
/// </summary> /// </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 /// <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 /// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class MatrixNormal public class MatrixNormal : IDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// The mean of the matrix normal distribution. /// The mean of the matrix normal distribution.
/// </summary> /// </summary>
@ -64,11 +66,6 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
Matrix<double> _k; Matrix<double> _k;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="MatrixNormal"/> class. /// Initializes a new instance of the <see cref="MatrixNormal"/> class.
/// </summary> /// </summary>
@ -107,55 +104,6 @@ namespace MathNet.Numerics.Distributions
return "MatrixNormal(Rows = " + _m.RowCount + ", Columns = " + _m.ColumnCount + ")"; 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> /// <summary>
/// Checks whether the parameters of the distribution are valid. /// Checks whether the parameters of the distribution are valid.
/// </summary> /// </summary>
@ -196,21 +144,62 @@ namespace MathNet.Numerics.Distributions
return true; 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> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public System.Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set { _random = value ?? new System.Random(); }
{ }
if (value == null)
{
throw new ArgumentNullException();
}
_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> /// <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 /// <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 /// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Multinomial public class Multinomial : IDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// Stores the normalized multinomial probabilities. /// Stores the normalized multinomial probabilities.
/// </summary> /// </summary>
@ -60,12 +62,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The number of trials. /// The number of trials.
/// </summary> /// </summary>
int _n; int _trials;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Multinomial class. /// Initializes a new instance of the Multinomial class.
@ -130,7 +127,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns> /// <returns>a string representation of the distribution.</returns>
public override string ToString() public override string ToString()
{ {
return "Multinomial(Dimension = " + _p.Length + ", Number of Trails = " + _n + ")"; return "Multinomial(Dimension = " + _p.Length + ", Number of Trails = " + _trials + ")";
} }
/// <summary> /// <summary>
@ -177,7 +174,16 @@ namespace MathNet.Numerics.Distributions
} }
_p = (double[]) p.Clone(); _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> /// <summary>
@ -186,7 +192,7 @@ namespace MathNet.Numerics.Distributions
public double[] P public double[] P
{ {
get { return (double[]) _p.Clone(); } get { return (double[]) _p.Clone(); }
set { SetParameters(value, _n); } set { SetParameters(value, _trials); }
} }
/// <summary> /// <summary>
@ -194,33 +200,16 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public int N public int N
{ {
get { return _n; } get { return _trials; }
set { SetParameters(_p, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </summary>
public Vector<double> Mean public Vector<double> Mean
{ {
get { return _n*(DenseVector) P; } get { return _trials*(DenseVector) P; }
} }
/// <summary> /// <summary>
@ -234,7 +223,7 @@ namespace MathNet.Numerics.Distributions
var res = (DenseVector) P; var res = (DenseVector) P;
for (var i = 0; i < res.Count; i++) for (var i = 0; i < res.Count; i++)
{ {
res[i] *= _n*(1 - res[i]); res[i] *= _trials*(1 - res[i]);
} }
return res; return res;
@ -252,7 +241,7 @@ namespace MathNet.Numerics.Distributions
var res = (DenseVector) P; var res = (DenseVector) P;
for (var i = 0; i < res.Count; i++) 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; return res;
@ -278,9 +267,9 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "x"); 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; var num = 1.0;
for (var i = 0; i < x.Length; i++) for (var i = 0; i < x.Length; i++)
{ {
@ -312,9 +301,9 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "x"); 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(); var num = x.Select((t, i) => t*Math.Log(_p[i])).Sum();
return coef + num; return coef + num;
} }
@ -328,7 +317,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the counts for each of the different possible values.</returns> /// <returns>the counts for each of the different possible values.</returns>
public int[] Sample() public int[] Sample()
{ {
return Sample(RandomSource, _p, _n); return Sample(RandomSource, _p, _trials);
} }
/// <summary> /// <summary>
@ -339,7 +328,7 @@ namespace MathNet.Numerics.Distributions
{ {
while (true) 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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class NegativeBinomial : IDiscreteDistribution public class NegativeBinomial : IDiscreteDistribution
{ {
/// <summary> System.Random _random;
/// The r parameter of the distribution.
/// </summary> double _trials;
double _r;
/// <summary> /// <summary>
/// The p parameter of the distribution. /// The p parameter of the distribution.
/// </summary> /// </summary>
double _p; double _p;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Gets or sets the number of trials. /// Gets or sets the number of trials.
/// </summary> /// </summary>
public double R public double R
{ {
get { return _r; } get { return _trials; }
set { SetParameters(value, _p); } set { SetParameters(value, _p); }
} }
@ -78,7 +72,7 @@ namespace MathNet.Numerics.Distributions
public double P public double P
{ {
get { return _p; } get { return _p; }
set { SetParameters(_r, value); } set { SetParameters(_trials, value); }
} }
/// <summary> /// <summary>
@ -105,20 +99,14 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Returns a <see cref="System.String"/> that represents this instance.
/// </summary> /// </summary>
/// <param name="r">The number of trials.</param> /// <returns>
/// <param name="p">The probability of a trial resulting in success.</param> /// A <see cref="System.String"/> that represents this instance.
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception> /// </returns>
void SetParameters(double r, double p) public override string ToString()
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p)) return "NegativeBinomial(R = " + _trials + ", P = " + _p + ")";
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_p = p;
_r = r;
} }
/// <summary> /// <summary>
@ -133,39 +121,36 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Returns a <see cref="System.String"/> that represents this instance. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
/// <returns> /// <param name="r">The number of trials.</param>
/// A <see cref="System.String"/> that represents this instance. /// <param name="p">The probability of a trial resulting in success.</param>
/// </returns> /// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
public override string ToString() 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> /// <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> /// </summary>
public System.Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set { _random = value ?? new System.Random(); }
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
} }
/// <summary> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </summary>
public double Mean public double Mean
{ {
get { return _r*(1.0 - _p)/_p; } get { return _trials*(1.0 - _p)/_p; }
} }
/// <summary> /// <summary>
@ -173,7 +158,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double Variance public double Variance
{ {
get { return _r*(1.0 - _p)/(_p*_p); } get { return _trials*(1.0 - _p)/(_p*_p); }
} }
/// <summary> /// <summary>
@ -181,7 +166,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double StdDev public double StdDev
{ {
get { return Math.Sqrt(_r*(1.0 - _p))/_p; } get { return Math.Sqrt(_trials*(1.0 - _p))/_p; }
} }
/// <summary> /// <summary>
@ -197,7 +182,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double Skewness 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> /// <summary>
@ -205,7 +190,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public int Mode 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> /// <summary>
@ -239,10 +224,10 @@ namespace MathNet.Numerics.Distributions
/// <returns>the probability mass at location <paramref name="k"/>.</returns> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
public double Probability(int k) public double Probability(int k)
{ {
var ln = SpecialFunctions.GammaLn(_r + k) var ln = SpecialFunctions.GammaLn(_trials + k)
- SpecialFunctions.GammaLn(_r) - SpecialFunctions.GammaLn(_trials)
- SpecialFunctions.GammaLn(k + 1.0) - SpecialFunctions.GammaLn(k + 1.0)
+ (_r*Math.Log(_p)) + (_trials*Math.Log(_p))
+ (k*Math.Log(1.0 - _p)); + (k*Math.Log(1.0 - _p));
return Math.Exp(ln); return Math.Exp(ln);
} }
@ -254,10 +239,10 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log probability mass at location <paramref name="k"/>.</returns> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
public double ProbabilityLn(int k) public double ProbabilityLn(int k)
{ {
var ln = SpecialFunctions.GammaLn(_r + k) var ln = SpecialFunctions.GammaLn(_trials + k)
- SpecialFunctions.GammaLn(_r) - SpecialFunctions.GammaLn(_trials)
- SpecialFunctions.GammaLn(k + 1.0) - SpecialFunctions.GammaLn(k + 1.0)
+ (_r*Math.Log(_p)) + (_trials*Math.Log(_p))
+ (k*Math.Log(1.0 - _p)); + (k*Math.Log(1.0 - _p));
return ln; return ln;
} }
@ -269,7 +254,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
return 1 - SpecialFunctions.BetaRegularized(x + 1, _r, 1 - _p); return 1 - SpecialFunctions.BetaRegularized(x + 1, _trials, 1 - _p);
} }
/// <summary> /// <summary>
@ -299,7 +284,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public int Sample() public int Sample()
{ {
return SampleUnchecked(RandomSource, _r, _p); return SampleUnchecked(RandomSource, _trials, _p);
} }
/// <summary> /// <summary>
@ -310,7 +295,7 @@ namespace MathNet.Numerics.Distributions
{ {
while (true) 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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Normal : IContinuousDistribution public class Normal : IContinuousDistribution
{ {
/// <summary> System.Random _random;
/// Keeps track of the mean of the normal distribution.
/// </summary>
double _mean;
/// <summary> double _mean;
/// Keeps track of the standard deviation of the normal distribution.
/// </summary>
double _stdDev; double _stdDev;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Normal class. This is a normal distribution with mean 0.0 /// 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 /// and standard deviation 1.0. The distribution will
@ -190,6 +180,15 @@ namespace MathNet.Numerics.Distributions
_stdDev = stddev; _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> /// <summary>
/// Gets or sets the precision of the normal distribution. /// Gets or sets the precision of the normal distribution.
/// </summary> /// </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> /// <summary>
/// Gets or sets the mean of the normal distribution. /// Gets or sets the mean of the normal distribution.
/// </summary> /// </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 /// <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 /// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class NormalGamma public class NormalGamma : IDistribution
{ {
/// <summary> System.Random _random;
/// The location of the mean.
/// </summary>
double _meanLocation;
/// <summary> double _meanLocation;
/// The scale of the mean.
/// </summary>
double _meanScale; double _meanScale;
/// <summary>
/// The shape of the precision.
/// </summary>
double _precisionShape; double _precisionShape;
/// <summary>
/// The inverse scale of the precision.
/// </summary>
double _precisionInvScale; double _precisionInvScale;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="NormalGamma"/> class. /// Initializes a new instance of the <see cref="NormalGamma"/> class.
/// </summary> /// </summary>
@ -154,6 +136,16 @@ namespace MathNet.Numerics.Distributions
SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale); 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> /// <summary>
/// Checks whether the parameters of the distribution are valid. /// Checks whether the parameters of the distribution are valid.
/// </summary> /// </summary>
@ -189,13 +181,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// A string representation of the distribution. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
/// <returns>a string representation of the distribution.</returns> public System.Random RandomSource
public override string ToString()
{ {
return "NormalGamma(Mean Location = " + _meanLocation + ", Mean Scale = " + _meanScale + get { return _random; }
", Precision Shape = " + _precisionShape + ", Precision Inverse Scale = " + _precisionInvScale + ")"; set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -234,23 +225,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); } 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> /// <summary>
/// Returns the marginal distribution for the mean of the <c>NormalGamma</c> distribution. /// Returns the marginal distribution for the mean of the <c>NormalGamma</c> distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Pareto : IContinuousDistribution public class Pareto : IContinuousDistribution
{ {
/// <summary> System.Random _random;
/// The scale parameter of the distribution.
/// </summary>
double _scale;
/// <summary> double _scale;
/// The shape parameter of the distribution.
/// </summary>
double _shape; double _shape;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Pareto"/> class. /// Initializes a new instance of the <see cref="Pareto"/> class.
/// </summary> /// </summary>
@ -88,6 +78,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(scale, shape); 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
@ -106,14 +116,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <param name="scale">The scale parameter of the distribution.</param> public System.Random RandomSource
/// <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; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -134,32 +142,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_scale, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </summary>

52
src/Numerics/Distributions/Poisson.cs

@ -44,16 +44,10 @@ namespace MathNet.Numerics.Distributions
/// </remarks> /// </remarks>
public class Poisson : IDiscreteDistribution public class Poisson : IDiscreteDistribution
{ {
/// <summary>
/// The Poisson distribution parameter λ.
/// </summary>
double _lambda;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random; System.Random _random;
double _lambda;
/// <summary> /// <summary>
/// Gets or sets the Poisson distribution parameter λ. /// Gets or sets the Poisson distribution parameter λ.
/// </summary> /// </summary>
@ -87,18 +81,14 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Returns a <see cref="System.String"/> that represents this instance.
/// </summary> /// </summary>
/// <param name="lambda">The mean (λ) of the distribution.</param> /// <returns>
/// <exception cref="System.ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception> /// A <see cref="System.String"/> that represents this instance.
void SetParameters(double lambda) /// </returns>
public override string ToString()
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) return "Poisson(λ = " + _lambda + ")";
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_lambda = lambda;
} }
/// <summary> /// <summary>
@ -112,14 +102,18 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Returns a <see cref="System.String"/> that represents this instance. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
/// <returns> /// <param name="lambda">The mean (λ) of the distribution.</param>
/// A <see cref="System.String"/> that represents this instance. /// <exception cref="System.ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
/// </returns> void SetParameters(double lambda)
public override string ToString()
{ {
return "Poisson(λ = " + _lambda + ")"; if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_lambda = lambda;
} }
/// <summary> /// <summary>
@ -128,15 +122,7 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set { _random = value ?? new System.Random(); }
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
} }
/// <summary> /// <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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Rayleigh : IContinuousDistribution 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; System.Random _random;
double _scale;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Rayleigh"/> class. /// Initializes a new instance of the <see cref="Rayleigh"/> class.
/// </summary> /// </summary>
@ -83,18 +77,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// A string representation of the distribution.
/// </summary> /// </summary>
/// <param name="scale">The scale parameter of the distribution.</param> /// <returns>a string representation of the distribution.</returns>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception> public override string ToString()
void SetParameters(double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale)) return "Rayleigh(Scale = " + _scale + ")";
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_scale = scale;
} }
/// <summary> /// <summary>
@ -108,21 +96,18 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Gets or sets the scale parameter of the distribution. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </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; } if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
set { SetParameters(value); } {
} throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
/// <summary> _scale = scale;
/// A string representation of the distribution.
/// </summary>
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Rayleigh(Scale = " + _scale + ")";
} }
/// <summary> /// <summary>
@ -131,15 +116,16 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set { _random = value ?? new System.Random(); }
{ }
if (value == null)
{
throw new ArgumentNullException();
}
_random = value; /// <summary>
} /// Gets or sets the scale parameter of the distribution.
/// </summary>
public double Scale
{
get { return _scale; }
set { SetParameters(value); }
} }
/// <summary> /// <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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Stable : IContinuousDistribution public class Stable : IContinuousDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// The stability parameter of the distribution. /// The stability parameter of the distribution.
/// </summary> /// </summary>
@ -59,21 +61,9 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
double _beta; double _beta;
/// <summary>
/// The scale parameter of the distribution.
/// </summary>
double _scale; double _scale;
/// <summary>
/// The location parameter of the distribution.
/// </summary>
double _location; double _location;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Stable"/> class. /// Initializes a new instance of the <see cref="Stable"/> class.
/// </summary> /// </summary>
@ -101,6 +91,28 @@ namespace MathNet.Numerics.Distributions
SetParameters(alpha, beta, scale, location); 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
@ -122,16 +134,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <param name="alpha">The stability parameter of the distribution.</param> public System.Random RandomSource
/// <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); get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -170,32 +178,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_alpha, _beta, _scale, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class StudentT : IContinuousDistribution public class StudentT : IContinuousDistribution
{ {
/// <summary> System.Random _random;
/// 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;
/// <summary> double _location;
/// Keeps track of the scale for the Student t-distribution.
/// </summary>
double _scale; double _scale;
double _freedom;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the StudentT class. This is a Student t-distribution with location 0.0 /// 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> /// <returns>a string representation of the distribution.</returns>
public override string ToString() public override string ToString()
{ {
return "StudentT(Location = " + _location + ", Scale = " + _scale + ", DoF = " + _dof + ")"; return "StudentT(Location = " + _location + ", Scale = " + _scale + ", DoF = " + _freedom + ")";
} }
/// <summary> /// <summary>
@ -153,7 +139,16 @@ namespace MathNet.Numerics.Distributions
_location = location; _location = location;
_scale = scale; _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> /// <summary>
@ -162,7 +157,7 @@ namespace MathNet.Numerics.Distributions
public double Location public double Location
{ {
get { return _location; } get { return _location; }
set { SetParameters(value, _scale, _dof); } set { SetParameters(value, _scale, _freedom); }
} }
/// <summary> /// <summary>
@ -171,7 +166,7 @@ namespace MathNet.Numerics.Distributions
public double Scale public double Scale
{ {
get { return _scale; } get { return _scale; }
set { SetParameters(_location, value, _dof); } set { SetParameters(_location, value, _freedom); }
} }
/// <summary> /// <summary>
@ -179,33 +174,16 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double DegreesOfFreedom public double DegreesOfFreedom
{ {
get { return _dof; } get { return _freedom; }
set { SetParameters(_location, _scale, value); } 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> /// <summary>
/// Gets the mean of the Student t-distribution. /// Gets the mean of the Student t-distribution.
/// </summary> /// </summary>
public double Mean public double Mean
{ {
get { return _dof > 1.0 ? _location : Double.NaN; } get { return _freedom > 1.0 ? _location : Double.NaN; }
} }
/// <summary> /// <summary>
@ -215,17 +193,17 @@ namespace MathNet.Numerics.Distributions
{ {
get get
{ {
if (Double.IsPositiveInfinity(_dof)) if (Double.IsPositiveInfinity(_freedom))
{ {
return _scale*_scale; 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 get
{ {
if (Double.IsPositiveInfinity(_dof)) if (Double.IsPositiveInfinity(_freedom))
{ {
return Math.Sqrt(_scale*_scale); 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(); 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 get
{ {
if (_dof <= 3) if (_freedom <= 3)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
@ -322,15 +300,15 @@ namespace MathNet.Numerics.Distributions
public double Density(double x) public double Density(double x)
{ {
// TODO JVG we can probably do a better job for Cauchy special case // 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); return Normal.Density(_location, _scale, x);
} }
var d = (x - _location)/_scale; var d = (x - _location)/_scale;
return Math.Exp(SpecialFunctions.GammaLn((_dof + 1.0)/2.0) - SpecialFunctions.GammaLn(_dof/2.0)) return Math.Exp(SpecialFunctions.GammaLn((_freedom + 1.0)/2.0) - SpecialFunctions.GammaLn(_freedom/2.0))
*Math.Pow(1.0 + (d*d/_dof), -0.5*(_dof + 1.0)) *Math.Pow(1.0 + (d*d/_freedom), -0.5*(_freedom + 1.0))
/Math.Sqrt(_dof*Math.PI) /Math.Sqrt(_freedom*Math.PI)
/_scale; /_scale;
} }
@ -342,16 +320,16 @@ namespace MathNet.Numerics.Distributions
public double DensityLn(double x) public double DensityLn(double x)
{ {
// TODO JVG we can probably do a better job for Cauchy special case // 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); return Normal.DensityLn(_location, _scale, x);
} }
var d = (x - _location)/_scale; var d = (x - _location)/_scale;
return SpecialFunctions.GammaLn((_dof + 1.0)/2.0) return SpecialFunctions.GammaLn((_freedom + 1.0)/2.0)
- (0.5*((_dof + 1.0)*Math.Log(1.0 + (d*d/_dof)))) - (0.5*((_freedom + 1.0)*Math.Log(1.0 + (d*d/_freedom))))
- SpecialFunctions.GammaLn(_dof/2.0) - SpecialFunctions.GammaLn(_freedom/2.0)
- (0.5*Math.Log(_dof*Math.PI)) - Math.Log(_scale); - (0.5*Math.Log(_freedom*Math.PI)) - Math.Log(_scale);
} }
/// <summary> /// <summary>
@ -362,14 +340,14 @@ namespace MathNet.Numerics.Distributions
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
// TODO JVG we can probably do a better job for Cauchy special case // 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); return Normal.CumulativeDistribution(_location, _scale, x);
} }
var k = (x - _location)/_scale; var k = (x - _location)/_scale;
var h = _dof/(_dof + (k*k)); var h = _freedom/(_freedom + (k*k));
var ib = 0.5*SpecialFunctions.BetaRegularized(_dof/2.0, 0.5, h); var ib = 0.5*SpecialFunctions.BetaRegularized(_freedom/2.0, 0.5, h);
return x <= _location ? ib : 1.0 - ib; return x <= _location ? ib : 1.0 - ib;
} }
@ -396,7 +374,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public double Sample() public double Sample()
{ {
return SampleUnchecked(RandomSource, _location, _scale, _dof); return SampleUnchecked(RandomSource, _location, _scale, _freedom);
} }
/// <summary> /// <summary>
@ -407,7 +385,7 @@ namespace MathNet.Numerics.Distributions
{ {
while (true) 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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Weibull : IContinuousDistribution public class Weibull : IContinuousDistribution
{ {
/// <summary> System.Random _random;
/// Weibull shape parameter.
/// </summary>
double _shape;
/// <summary> double _shape;
/// Weibull inverse scale parameter.
/// </summary>
double _scale; double _scale;
/// <summary> /// <summary>
/// Reusable intermediate result 1 / (<see cref="_scale"/> ^ <see cref="_shape"/>) /// Reusable intermediate result 1 / (_scale ^ _shape)
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// By caching this parameter we can get slightly better numerics precision /// By caching this parameter we can get slightly better numerics precision
@ -67,11 +62,6 @@ namespace MathNet.Numerics.Distributions
/// </remarks> /// </remarks>
double _scalePowShapeInv; double _scalePowShapeInv;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Weibull class. /// Initializes a new instance of the Weibull class.
/// </summary> /// </summary>
@ -133,6 +123,15 @@ namespace MathNet.Numerics.Distributions
_scalePowShapeInv = Math.Pow(scale, -shape); _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> /// <summary>
/// Gets or sets the shape of the Weibull distribution. /// Gets or sets the shape of the Weibull distribution.
/// </summary> /// </summary>
@ -151,23 +150,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); } 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> /// <summary>
/// Gets the mean of the Weibull distribution. /// Gets the mean of the Weibull distribution.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Wishart public class Wishart
{ {
System.Random _random;
/// <summary> /// <summary>
/// The degrees of freedom for the Wishart distribution. /// The degrees of freedom for the Wishart distribution.
/// </summary> /// </summary>
@ -65,11 +67,6 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
Cholesky<double> _chol; Cholesky<double> _chol;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Wishart"/> class. /// Initializes a new instance of the <see cref="Wishart"/> class.
/// </summary> /// </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> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Zipf : IDiscreteDistribution public class Zipf : IDiscreteDistribution
{ {
System.Random _random;
/// <summary> /// <summary>
/// The s parameter of the distribution. /// The s parameter of the distribution.
/// </summary> /// </summary>
@ -59,11 +61,6 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
int _n; int _n;
/// <summary>
/// The distribution's random number generator.
/// </summary>
System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Zipf"/> class. /// Initializes a new instance of the <see cref="Zipf"/> class.
/// </summary> /// </summary>
@ -87,6 +84,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(s, n); 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> /// <summary>
/// Sets the parameters of the distribution after checking their validity. /// Sets the parameters of the distribution after checking their validity.
/// </summary> /// </summary>
@ -104,14 +121,12 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <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> /// </summary>
/// <param name="s">The s parameter of the distribution.</param> public System.Random RandomSource
/// <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; get { return _random; }
set { _random = value ?? new System.Random(); }
} }
/// <summary> /// <summary>
@ -132,32 +147,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_s, value); } 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> /// <summary>
/// Gets the mean of the distribution. /// Gets the mean of the distribution.
/// </summary> /// </summary>

1
src/Numerics/Numerics.csproj

@ -95,6 +95,7 @@
<Compile Include="Distributions\ChiSquare.cs" /> <Compile Include="Distributions\ChiSquare.cs" />
<Compile Include="Distributions\ContinuousUniform.cs" /> <Compile Include="Distributions\ContinuousUniform.cs" />
<Compile Include="Distributions\ConwayMaxwellPoisson.cs" /> <Compile Include="Distributions\ConwayMaxwellPoisson.cs" />
<Compile Include="Distributions\IDistribution.cs" />
<Compile Include="Distributions\Dirichlet.cs" /> <Compile Include="Distributions\Dirichlet.cs" />
<Compile Include="Distributions\DiscreteUniform.cs" /> <Compile Include="Distributions\DiscreteUniform.cs" />
<Compile Include="Distributions\Erlang.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] [Test]
public void FailSetRandomSourceWithNullReference() public void HasRandomSourceEvenAfterSetToNull()
{ {
foreach (var dd in _discreteDistributions) foreach (var dd in _discreteDistributions)
{ {
var dd1 = dd; Assert.DoesNotThrow(() => dd.RandomSource = null);
Assert.Throws<ArgumentNullException>(() => dd1.RandomSource = null); Assert.IsNotNull(dd.RandomSource);
} }
foreach (var cd in _continuousDistributions) foreach (var cd in _continuousDistributions)
{ {
var cd1 = cd; Assert.DoesNotThrow(() => cd.RandomSource = null);
Assert.Throws<ArgumentNullException>(() => cd1.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] [Test]
public void FailSetRandomSourceWithNullReference() public void HasRandomSourceEvenAfterSetToNull()
{ {
var d = new Dirichlet(0.3, 5); var d = new Dirichlet(0.3, 5);
Assert.Throws<ArgumentNullException>(() => d.RandomSource = null); Assert.DoesNotThrow(() => d.RandomSource = null);
Assert.IsNotNull(d.RandomSource);
} }
/// <summary> /// <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] [Test]
public void FailSetRandomSourceWithNullReference() public void HasRandomSourceEvenAfterSetToNull()
{ {
var d = new InverseWishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)); 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> /// <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] [Test]
public void FailSetRandomSourceWithNullReference() public void HasRandomSourceEvenAfterSetToNull()
{ {
const int N = 2; const int N = 2;
const int P = 3; const int P = 3;
var d = new MatrixNormal(MatrixLoader.GenerateRandomDenseMatrix(N, P), MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(N), MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(P)); 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> /// <summary>

Loading…
Cancel
Save