diff --git a/src/Numerics/Distributions/Bernoulli.cs b/src/Numerics/Distributions/Bernoulli.cs
index c94b13b1..95ad2137 100644
--- a/src/Numerics/Distributions/Bernoulli.cs
+++ b/src/Numerics/Distributions/Bernoulli.cs
@@ -47,16 +47,10 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Bernoulli : IDiscreteDistribution
{
- ///
- /// The probability of generating a one.
- ///
- double _p;
-
- ///
- /// The distribution's random number generator.
- ///
System.Random _random;
+ double _p;
+
///
/// Initializes a new instance of the Bernoulli class.
///
@@ -115,29 +109,21 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Gets or sets the probability of generating a one.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- public double P
+ public System.Random RandomSource
{
- get { return _p; }
- set { SetParameters(value); }
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
- /// Gets or sets the random number generator which is used to draw random samples.
+ /// Gets or sets the probability of generating a one.
///
- public System.Random RandomSource
+ public double P
{
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
+ get { return _p; }
+ set { SetParameters(value); }
}
///
diff --git a/src/Numerics/Distributions/Beta.cs b/src/Numerics/Distributions/Beta.cs
index 5e823b49..d4bba871 100644
--- a/src/Numerics/Distributions/Beta.cs
+++ b/src/Numerics/Distributions/Beta.cs
@@ -53,21 +53,11 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Beta : IContinuousDistribution
{
- ///
- /// Beta shape parameter a.
- ///
- double _shapeA;
+ System.Random _random;
- ///
- /// Beta shape parameter b.
- ///
+ double _shapeA;
double _shapeB;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the Beta class.
///
@@ -130,6 +120,15 @@ namespace MathNet.Numerics.Distributions
_shapeB = b;
}
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
+ }
+
///
/// Gets or sets the A shape parameter of the Beta distribution.
///
@@ -148,23 +147,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shapeA, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the Beta distribution.
///
diff --git a/src/Numerics/Distributions/Binomial.cs b/src/Numerics/Distributions/Binomial.cs
index 43131d9a..8721d9f3 100644
--- a/src/Numerics/Distributions/Binomial.cs
+++ b/src/Numerics/Distributions/Binomial.cs
@@ -47,20 +47,17 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Binomial : IDiscreteDistribution
{
+ System.Random _random;
+
///
- /// Stores the normalized binomial probability.
+ /// Success probability in each trial.
///
double _p;
///
/// The number of trials.
///
- int _n;
-
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
+ int _trials;
///
/// Initializes a new instance of the Binomial class.
@@ -95,7 +92,7 @@ namespace MathNet.Numerics.Distributions
/// a string representation of the distribution.
public override string ToString()
{
- return "Binomial(Success Probability = " + _p + ", Number of Trials = " + _n + ")";
+ return "Binomial(Success Probability = " + _p + ", Number of Trials = " + _trials + ")";
}
///
@@ -124,7 +121,16 @@ namespace MathNet.Numerics.Distributions
}
_p = p;
- _n = n;
+ _trials = n;
+ }
+
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -133,7 +139,7 @@ namespace MathNet.Numerics.Distributions
public double P
{
get { return _p; }
- set { SetParameters(value, _n); }
+ set { SetParameters(value, _trials); }
}
///
@@ -141,33 +147,16 @@ namespace MathNet.Numerics.Distributions
///
public int N
{
- get { return _n; }
+ get { return _trials; }
set { SetParameters(_p, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
public double Mean
{
- get { return _p*_n; }
+ get { return _p*_trials; }
}
///
@@ -175,7 +164,7 @@ namespace MathNet.Numerics.Distributions
///
public double StdDev
{
- get { return Math.Sqrt(_p*(1.0 - _p)*_n); }
+ get { return Math.Sqrt(_p*(1.0 - _p)*_trials); }
}
///
@@ -183,7 +172,7 @@ namespace MathNet.Numerics.Distributions
///
public double Variance
{
- get { return _p*(1.0 - _p)*_n; }
+ get { return _p*(1.0 - _p)*_trials; }
}
///
@@ -199,7 +188,7 @@ namespace MathNet.Numerics.Distributions
}
var e = 0.0;
- for (var i = 0; i <= _n; i++)
+ for (var i = 0; i <= _trials; i++)
{
var p = Probability(i);
e -= p*Math.Log(p);
@@ -214,7 +203,7 @@ namespace MathNet.Numerics.Distributions
///
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)); }
}
///
@@ -230,7 +219,7 @@ namespace MathNet.Numerics.Distributions
///
public int Maximum
{
- get { return _n; }
+ get { return _trials; }
}
///
@@ -242,7 +231,7 @@ namespace MathNet.Numerics.Distributions
{
if (_p == 1.0)
{
- return _n;
+ return _trials;
}
if (_p == 0.0)
@@ -250,7 +239,7 @@ namespace MathNet.Numerics.Distributions
return 0;
}
- return (int) Math.Floor((_n + 1)*_p);
+ return (int) Math.Floor((_trials + 1)*_p);
}
}
@@ -259,7 +248,7 @@ namespace MathNet.Numerics.Distributions
///
public int Median
{
- get { return (int) Math.Floor(_p*_n); }
+ get { return (int) Math.Floor(_p*_trials); }
}
///
@@ -274,7 +263,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
- if (k > _n)
+ if (k > _trials)
{
return 0.0;
}
@@ -289,7 +278,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
- if (_p == 1.0 && k == _n)
+ if (_p == 1.0 && k == _trials)
{
return 1.0;
}
@@ -299,7 +288,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
- return SpecialFunctions.Binomial(_n, k)*Math.Pow(_p, k)*Math.Pow(1.0 - _p, _n - k);
+ return SpecialFunctions.Binomial(_trials, k)*Math.Pow(_p, k)*Math.Pow(1.0 - _p, _trials - k);
}
///
@@ -314,7 +303,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
- if (k > _n)
+ if (k > _trials)
{
return Double.NegativeInfinity;
}
@@ -329,7 +318,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
- if (_p == 1.0 && k == _n)
+ if (_p == 1.0 && k == _trials)
{
return 0.0;
}
@@ -339,7 +328,7 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
- return SpecialFunctions.BinomialLn(_n, k) + (k*Math.Log(_p)) + ((_n - k)*Math.Log(1.0 - _p));
+ return SpecialFunctions.BinomialLn(_trials, k) + (k*Math.Log(_p)) + ((_trials - k)*Math.Log(1.0 - _p));
}
///
@@ -354,7 +343,7 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
- if (x > _n)
+ if (x > _trials)
{
return 1.0;
}
@@ -362,7 +351,7 @@ namespace MathNet.Numerics.Distributions
var cdf = 0.0;
for (var i = 0; i <= (int) Math.Floor(x); i++)
{
- cdf += Combinatorics.Combinations(_n, i)*Math.Pow(_p, i)*Math.Pow(1.0 - _p, _n - i);
+ cdf += Combinatorics.Combinations(_trials, i)*Math.Pow(_p, i)*Math.Pow(1.0 - _p, _trials - i);
}
return cdf;
@@ -392,7 +381,7 @@ namespace MathNet.Numerics.Distributions
/// The number of successes in N trials.
public int Sample()
{
- return SampleUnchecked(RandomSource, _p, _n);
+ return SampleUnchecked(RandomSource, _p, _trials);
}
///
@@ -403,7 +392,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
- yield return SampleUnchecked(RandomSource, _p, _n);
+ yield return SampleUnchecked(RandomSource, _p, _trials);
}
}
diff --git a/src/Numerics/Distributions/Categorical.cs b/src/Numerics/Distributions/Categorical.cs
index 281584e1..5b9617dc 100644
--- a/src/Numerics/Distributions/Categorical.cs
+++ b/src/Numerics/Distributions/Categorical.cs
@@ -53,6 +53,7 @@ namespace MathNet.Numerics.Distributions
public class Categorical : IDiscreteDistribution
{
System.Random _random;
+
double[] _pmfNormalized;
double[] _cdfUnnormalized;
@@ -188,7 +189,15 @@ namespace MathNet.Numerics.Distributions
{
_pmfNormalized[i] = p[i]/sum;
}
+ }
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -202,23 +211,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/Cauchy.cs b/src/Numerics/Distributions/Cauchy.cs
index bfdc0404..28689843 100644
--- a/src/Numerics/Distributions/Cauchy.cs
+++ b/src/Numerics/Distributions/Cauchy.cs
@@ -42,16 +42,10 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Cauchy : IContinuousDistribution
{
- ///
- /// The scale of the Cauchy distribution.
- ///
- double _scale;
-
- ///
- /// The distribution's random number generator.
- ///
System.Random _random;
+ double _scale;
+
///
/// Initializes a new instance of the class with the location parameter set to 0 and the scale parameter set to 1
///
@@ -84,6 +78,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(location, scale);
}
+ ///
+ /// A string representation of the distribution.
+ ///
+ /// a string representation of the distribution.
+ public override string ToString()
+ {
+ return "Cauchy(Location = " + Median + ", Scale = " + _scale + ")";
+ }
+
+ ///
+ /// Checks whether the parameters of the distribution are valid.
+ ///
+ /// Location parameter.
+ /// Scale parameter. Must be greater than 0.
+ /// True when the parameters are valid, false otherwise.
+ static bool IsValidParameterSet(double location, double scale)
+ {
+ return scale > 0.0 && !Double.IsNaN(location);
+ }
+
///
/// Sets the parameters of the distribution after checking their validity.
///
@@ -102,14 +116,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Checks whether the parameters of the distribution are valid.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// Location parameter.
- /// Scale parameter. Must be greater than 0.
- /// True when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(double location, double scale)
+ public System.Random RandomSource
{
- return scale > 0.0 && !Double.IsNaN(location);
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -130,31 +142,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(Median, value); }
}
- ///
- /// A string representation of the distribution.
- ///
- /// a string representation of the distribution.
- public override string ToString()
- {
- return "Cauchy(Location = " + Median + ", Scale = " + _scale + ")";
- }
-
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
///
/// Gets the mean of the distribution.
diff --git a/src/Numerics/Distributions/Chi.cs b/src/Numerics/Distributions/Chi.cs
index fcbfb7c9..98123961 100644
--- a/src/Numerics/Distributions/Chi.cs
+++ b/src/Numerics/Distributions/Chi.cs
@@ -48,16 +48,13 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Chi : IContinuousDistribution
{
+ System.Random _random;
+
///
/// Keeps track of the degrees of freedom for the Chi distribution.
///
double _dof;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -80,18 +77,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Sets the parameters of the distribution after checking their validity.
+ /// A string representation of the distribution.
///
- /// The degrees of freedom for the Chi distribution.
- /// When the parameters don't pass the function.
- void SetParameters(double dof)
+ /// a string representation of the distribution.
+ public override string ToString()
{
- if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- _dof = dof;
+ return "Chi(DoF = " + _dof + ")";
}
///
@@ -105,38 +96,36 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Gets or sets the degrees of freedom of the Chi distribution.
+ /// Sets the parameters of the distribution after checking their validity.
///
- public double DegreesOfFreedom
+ /// The degrees of freedom for the Chi distribution.
+ /// When the parameters don't pass the function.
+ void SetParameters(double dof)
{
- get { return _dof; }
- set { SetParameters(value); }
+ if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
+ {
+ throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
+ }
+
+ _dof = dof;
}
///
- /// A string representation of the distribution.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// a string representation of the distribution.
- public override string ToString()
+ public System.Random RandomSource
{
- return "Chi(DoF = " + _dof + ")";
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
- /// Gets or sets the distribution's random number generator.
+ /// Gets or sets the degrees of freedom of the Chi distribution.
///
- public System.Random RandomSource
+ public double DegreesOfFreedom
{
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
+ get { return _dof; }
+ set { SetParameters(value); }
}
///
diff --git a/src/Numerics/Distributions/ChiSquare.cs b/src/Numerics/Distributions/ChiSquare.cs
index 560867dc..a52670cf 100644
--- a/src/Numerics/Distributions/ChiSquare.cs
+++ b/src/Numerics/Distributions/ChiSquare.cs
@@ -46,9 +46,6 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class ChiSquare : IContinuousDistribution
{
- ///
- /// The distribution's random number generator.
- ///
System.Random _random;
///
@@ -73,18 +70,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Sets the parameters of the distribution after checking their validity.
+ /// A string representation of the distribution.
///
- /// The degrees of freedom for the ChiSquare distribution.
- /// When the parameters don't pass the function.
- void SetParameters(double dof)
+ /// a string representation of the distribution.
+ public override string ToString()
{
- if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- Mean = dof;
+ return "ChiSquare(DoF = " + Mean + ")";
}
///
@@ -98,38 +89,36 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Gets or sets the degrees of freedom of the ChiSquare distribution.
+ /// Sets the parameters of the distribution after checking their validity.
///
- public double DegreesOfFreedom
+ /// The degrees of freedom for the ChiSquare distribution.
+ /// When the parameters don't pass the function.
+ void SetParameters(double dof)
{
- get { return Mean; }
- set { SetParameters(value); }
+ if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
+ {
+ throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
+ }
+
+ Mean = dof;
}
///
- /// A string representation of the distribution.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// a string representation of the distribution.
- public override string ToString()
+ public System.Random RandomSource
{
- return "ChiSquare(DoF = " + Mean + ")";
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
- /// Gets or sets the distribution's random number generator.
+ /// Gets or sets the degrees of freedom of the ChiSquare distribution.
///
- public System.Random RandomSource
+ public double DegreesOfFreedom
{
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
+ get { return Mean; }
+ set { SetParameters(value); }
}
///
diff --git a/src/Numerics/Distributions/ContinuousUniform.cs b/src/Numerics/Distributions/ContinuousUniform.cs
index 7f3285d6..bc587a89 100644
--- a/src/Numerics/Distributions/ContinuousUniform.cs
+++ b/src/Numerics/Distributions/ContinuousUniform.cs
@@ -46,6 +46,8 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class ContinuousUniform : IContinuousDistribution
{
+ System.Random _random;
+
///
/// The distribution's lower bound.
///
@@ -56,11 +58,6 @@ namespace MathNet.Numerics.Distributions
///
double _upper;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the ContinuousUniform class with lower bound 0 and upper bound 1.
///
@@ -130,6 +127,15 @@ namespace MathNet.Numerics.Distributions
_upper = upper;
}
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
+ }
+
///
/// Gets or sets the lower bound of the distribution.
///
@@ -148,23 +154,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lower, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs
index e5a9dec2..237b85fb 100644
--- a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs
+++ b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs
@@ -53,6 +53,8 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class ConwayMaxwellPoisson : IDiscreteDistribution
{
+ System.Random _random;
+
///
/// Since many properties of the distribution can only be computed approximately, the tolerance
/// level specifies how much error we accept.
@@ -84,11 +86,6 @@ namespace MathNet.Numerics.Distributions
///
double _nu;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -112,6 +109,28 @@ namespace MathNet.Numerics.Distributions
SetParameters(lambda, nu);
}
+ ///
+ /// Returns a that represents this instance.
+ ///
+ ///
+ /// A that represents this instance.
+ ///
+ public override string ToString()
+ {
+ return "ConwayMaxwellPoisson(Lambda = " + _lambda + ", Nu = " + _nu + ")";
+ }
+
+ ///
+ /// Checks whether the parameters of the distribution are valid.
+ ///
+ /// The lambda parameter.
+ /// The nu parameter.
+ /// true when the parameters are valid, false otherwise.
+ static bool IsValidParameterSet(double lambda, double nu)
+ {
+ return lambda > 0.0 && nu >= 0.0;
+ }
+
///
/// Sets the parameters of the distribution after checking their validity.
///
@@ -130,14 +149,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Checks whether the parameters of the distribution are valid.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// The lambda parameter.
- /// The nu parameter.
- /// true when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(double lambda, double nu)
+ public System.Random RandomSource
{
- return lambda > 0.0 && nu >= 0.0;
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -160,34 +177,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lambda, value); }
}
- ///
- /// Returns a that represents this instance.
- ///
- ///
- /// A that represents this instance.
- ///
- public override string ToString()
- {
- return "ConwayMaxwellPoisson(Lambda = " + _lambda + ", Nu = " + _nu + ")";
- }
-
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/Dirichlet.cs b/src/Numerics/Distributions/Dirichlet.cs
index 19cbefed..bd178dd7 100644
--- a/src/Numerics/Distributions/Dirichlet.cs
+++ b/src/Numerics/Distributions/Dirichlet.cs
@@ -43,18 +43,15 @@ namespace MathNet.Numerics.Distributions
/// The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to false, all parameter checks can be turned off.
- public class Dirichlet
+ public class Dirichlet : IDistribution
{
+ System.Random _random;
+
///
/// The Dirichlet distribution parameters.
///
double[] _alpha;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the Dirichlet class. The distribution will
/// be initialized with the default random number generator.
@@ -115,6 +112,17 @@ namespace MathNet.Numerics.Distributions
SetParameters(parm);
}
+ ///
+ /// Returns a that represents this instance.
+ ///
+ ///
+ /// A that represents this instance.
+ ///
+ public override string ToString()
+ {
+ return "Dirichlet(Dimension = " + Dimension + ")";
+ }
+
///
/// Checks whether the parameters of the distribution are valid: no
/// parameter can be less than zero and at least one parameter should be
@@ -160,14 +168,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Returns a that represents this instance.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- ///
- /// A that represents this instance.
- ///
- public override string ToString()
+ public System.Random RandomSource
{
- return "Dirichlet(Dimension = " + Dimension + ")";
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -308,23 +314,6 @@ namespace MathNet.Numerics.Distributions
return term + SpecialFunctions.GammaLn(sumalpha);
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Samples a Dirichlet distributed random vector.
///
diff --git a/src/Numerics/Distributions/DiscreteUniform.cs b/src/Numerics/Distributions/DiscreteUniform.cs
index b66b1926..dbe5ad44 100644
--- a/src/Numerics/Distributions/DiscreteUniform.cs
+++ b/src/Numerics/Distributions/DiscreteUniform.cs
@@ -47,6 +47,8 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class DiscreteUniform : IDiscreteDistribution
{
+ System.Random _random;
+
///
/// The distribution's lower bound.
///
@@ -57,11 +59,6 @@ namespace MathNet.Numerics.Distributions
///
int _upper;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the DiscreteUniform class.
///
@@ -124,6 +121,15 @@ namespace MathNet.Numerics.Distributions
_upper = upper;
}
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
+ }
+
///
/// Gets or sets the lower bound of the probability distribution.
///
@@ -142,23 +148,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lower, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/Erlang.cs b/src/Numerics/Distributions/Erlang.cs
index 074d1b31..a59f8a8c 100644
--- a/src/Numerics/Distributions/Erlang.cs
+++ b/src/Numerics/Distributions/Erlang.cs
@@ -47,21 +47,11 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Erlang : IContinuousDistribution
{
- ///
- /// Erlang shape parameter.
- ///
- double _shape;
+ System.Random _random;
- ///
- /// Erlang inverse scale parameter.
- ///
+ double _shape;
double _invScale;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -109,6 +99,26 @@ namespace MathNet.Numerics.Distributions
return new Erlang(shape, invScale);
}
+ ///
+ /// A string representation of the distribution.
+ ///
+ /// a string representation of the distribution.
+ public override string ToString()
+ {
+ return "Erlang(Shape = " + _shape + ", Inverse Scale = " + _invScale + ")";
+ }
+
+ ///
+ /// Checks whether the parameters of the distribution are valid.
+ ///
+ /// The shape of the Erlang distribution.
+ /// The inverse scale of the Erlang distribution.
+ /// true when the parameters are valid, false otherwise.
+ static bool IsValidParameterSet(double shape, double invScale)
+ {
+ return shape >= 0.0 && invScale >= 0.0;
+ }
+
///
/// Sets the parameters of the distribution after checking their validity.
///
@@ -126,14 +136,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Checks whether the parameters of the distribution are valid.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// The shape of the Erlang distribution.
- /// The inverse scale of the Erlang distribution.
- /// true when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(double shape, double invScale)
+ public System.Random RandomSource
{
- return shape >= 0.0 && invScale >= 0.0;
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -173,32 +181,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
- ///
- /// A string representation of the distribution.
- ///
- /// a string representation of the distribution.
- public override string ToString()
- {
- return "Erlang(Shape = " + _shape + ", Inverse Scale = " + _invScale + ")";
- }
-
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/Exponential.cs b/src/Numerics/Distributions/Exponential.cs
index 5b5bbf6a..df1ea23c 100644
--- a/src/Numerics/Distributions/Exponential.cs
+++ b/src/Numerics/Distributions/Exponential.cs
@@ -46,16 +46,10 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Exponential : IContinuousDistribution
{
- ///
- /// The lambda parameter of the Exponential distribution.
- ///
- double _lambda;
-
- ///
- /// The distribution's random number generator.
- ///
System.Random _random;
+ double _lambda;
+
///
/// Initializes a new instance of the class.
///
@@ -78,18 +72,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Sets the parameters of the distribution after checking their validity.
+ /// A string representation of the distribution.
///
- /// Lambda parameter.
- /// When the parameters don't pass the function.
- void SetParameters(double lambda)
+ /// a string representation of the distribution.
+ public override string ToString()
{
- if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- _lambda = lambda;
+ return "Exponential(Lambda = " + _lambda + ")";
}
///
@@ -103,21 +91,18 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Gets or sets the lambda parameter of the distribution.
+ /// Sets the parameters of the distribution after checking their validity.
///
- public double Lambda
+ /// Lambda parameter.
+ /// When the parameters don't pass the function.
+ void SetParameters(double lambda)
{
- get { return _lambda; }
- set { SetParameters(value); }
- }
+ if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
+ {
+ throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
+ }
- ///
- /// A string representation of the distribution.
- ///
- /// a string representation of the distribution.
- public override string ToString()
- {
- return "Exponential(Lambda = " + _lambda + ")";
+ _lambda = lambda;
}
///
@@ -126,15 +111,16 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource
{
get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
+ set { _random = value ?? new System.Random(); }
+ }
- _random = value;
- }
+ ///
+ /// Gets or sets the lambda parameter of the distribution.
+ ///
+ public double Lambda
+ {
+ get { return _lambda; }
+ set { SetParameters(value); }
}
///
diff --git a/src/Numerics/Distributions/FisherSnedecor.cs b/src/Numerics/Distributions/FisherSnedecor.cs
index 9e7f4ee4..8b681afd 100644
--- a/src/Numerics/Distributions/FisherSnedecor.cs
+++ b/src/Numerics/Distributions/FisherSnedecor.cs
@@ -46,6 +46,8 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class FisherSnedecor : IContinuousDistribution
{
+ System.Random _random;
+
///
/// The first parameter - degree of freedom.
///
@@ -56,11 +58,6 @@ namespace MathNet.Numerics.Distributions
///
double _d2;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -84,6 +81,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(d1, d2);
}
+ ///
+ /// A string representation of the distribution.
+ ///
+ /// a string representation of the distribution.
+ public override string ToString()
+ {
+ return "FisherSnedecor(DegreeOfFreedom1 = " + _d1 + ", DegreeOfFreedom2 = " + _d2 + ")";
+ }
+
+ ///
+ /// Checks whether the parameters of the distribution are valid.
+ ///
+ /// The first parameter - degree of freedom.
+ /// The second parameter - degree of freedom.
+ /// true when the parameters are valid, false otherwise.
+ static bool IsValidParameterSet(double d1, double d2)
+ {
+ return d1 > 0.0 && d2 > 0.0;
+ }
+
///
/// Sets the parameters of the distribution after checking their validity.
///
@@ -101,14 +118,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Checks whether the parameters of the distribution are valid.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// The first parameter - degree of freedom.
- /// The second parameter - degree of freedom.
- /// true when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(double d1, double d2)
+ public System.Random RandomSource
{
- return d1 > 0.0 && d2 > 0.0;
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -129,32 +144,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_d1, value); }
}
- ///
- /// A string representation of the distribution.
- ///
- /// a string representation of the distribution.
- public override string ToString()
- {
- return "FisherSnedecor(DegreeOfFreedom1 = " + _d1 + ", DegreeOfFreedom2 = " + _d2 + ")";
- }
-
- ///
- /// Gets or sets the distribution's random number generator.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/Gamma.cs b/src/Numerics/Distributions/Gamma.cs
index c8c944fd..72348ed0 100644
--- a/src/Numerics/Distributions/Gamma.cs
+++ b/src/Numerics/Distributions/Gamma.cs
@@ -54,21 +54,11 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Gamma : IContinuousDistribution
{
- ///
- /// Gamma shape parameter.
- ///
- double _shape;
+ System.Random _random;
- ///
- /// Gamma inverse scale parameter.
- ///
+ double _shape;
double _invScale;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the Gamma class.
///
@@ -153,6 +143,15 @@ namespace MathNet.Numerics.Distributions
_invScale = invScale;
}
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
+ }
+
///
/// Gets or sets the shape of the Gamma distribution.
///
@@ -190,23 +189,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the Gamma distribution.
///
diff --git a/src/Numerics/Distributions/Geometric.cs b/src/Numerics/Distributions/Geometric.cs
index 7c5a10d1..bb21ef65 100644
--- a/src/Numerics/Distributions/Geometric.cs
+++ b/src/Numerics/Distributions/Geometric.cs
@@ -47,16 +47,10 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Geometric : IDiscreteDistribution
{
- ///
- /// The geometric distribution parameter.
- ///
- double _p;
-
- ///
- /// The distribution's random number generator.
- ///
System.Random _random;
+ double _p;
+
///
/// Initializes a new instance of the Geometric class.
///
@@ -81,18 +75,14 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Sets the parameters of the distribution after checking their validity.
+ /// Returns a that represents this instance.
///
- /// The probability of generating a one.
- /// When the parameters don't pass the function.
- void SetParameters(double p)
+ ///
+ /// A that represents this instance.
+ ///
+ public override string ToString()
{
- if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- _p = p;
+ return "Geometric(P = " + _p + ")";
}
///
@@ -106,41 +96,37 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Gets or sets the probability of generating a one.
+ /// Sets the parameters of the distribution after checking their validity.
///
- public double P
+ /// The probability of generating a one.
+ /// When the parameters don't pass the function.
+ void SetParameters(double p)
{
- get { return _p; }
+ if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
+ {
+ throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
+ }
- set { SetParameters(value); }
+ _p = p;
}
///
- /// Returns a that represents this instance.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- ///
- /// A that represents this instance.
- ///
- public override string ToString()
+ public System.Random RandomSource
{
- return "Geometric(P = " + _p + ")";
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
- /// Gets or sets the random number generator which is used to draw random samples.
+ /// Gets or sets the probability of generating a one.
///
- public System.Random RandomSource
+ public double P
{
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
+ get { return _p; }
- _random = value;
- }
+ set { SetParameters(value); }
}
///
diff --git a/src/Numerics/Distributions/Hypergeometric.cs b/src/Numerics/Distributions/Hypergeometric.cs
index d038039d..45fd6c06 100644
--- a/src/Numerics/Distributions/Hypergeometric.cs
+++ b/src/Numerics/Distributions/Hypergeometric.cs
@@ -49,6 +49,8 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Hypergeometric : IDiscreteDistribution
{
+ System.Random _random;
+
///
/// The size of the population (N).
///
@@ -64,11 +66,6 @@ namespace MathNet.Numerics.Distributions
///
int _draws;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the Hypergeometric class.
///
@@ -94,6 +91,29 @@ namespace MathNet.Numerics.Distributions
SetParameters(population, success, draws);
}
+ ///
+ /// Returns a that represents this instance.
+ ///
+ ///
+ /// A that represents this instance.
+ ///
+ public override string ToString()
+ {
+ return "Hypergeometric(N = " + _population + ", M = " + _success + ", n = " + _draws + ")";
+ }
+
+ ///
+ /// Checks whether the parameters of the distribution are valid.
+ ///
+ /// The size of the population (N).
+ /// The number successes within the population (K, M).
+ /// The number of draws without replacement (n).
+ /// true when the parameters are valid, false otherwise.
+ static bool IsValidParameterSet(int population, int success, int draws)
+ {
+ return population >= 0 && success >= 0 && draws >= 0 && (success <= population && draws <= population);
+ }
+
///
/// Sets the parameters of the distribution after checking their validity.
///
@@ -113,15 +133,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Checks whether the parameters of the distribution are valid.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// The size of the population (N).
- /// The number successes within the population (K, M).
- /// The number of draws without replacement (n).
- /// true when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(int population, int success, int draws)
+ public System.Random RandomSource
{
- return population >= 0 && success >= 0 && draws >= 0 && (success <= population && draws <= population);
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -181,34 +198,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_population, _success, value); }
}
- ///
- /// Returns a that represents this instance.
- ///
- ///
- /// A that represents this instance.
- ///
- public override string ToString()
- {
- return "Hypergeometric(N = " + _population + ", M = " + _success + ", n = " + _draws + ")";
- }
-
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/IDistribution.cs b/src/Numerics/Distributions/IDistribution.cs
new file mode 100644
index 00000000..5a06100a
--- /dev/null
+++ b/src/Numerics/Distributions/IDistribution.cs
@@ -0,0 +1,43 @@
+//
+// 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.
+//
+
+namespace MathNet.Numerics.Distributions
+{
+ ///
+ /// The common interface for all distributions.
+ ///
+ public interface IDistribution
+ {
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ System.Random RandomSource { get; set; }
+ }
+}
diff --git a/src/Numerics/Distributions/IUnivariateDistribution.cs b/src/Numerics/Distributions/IUnivariateDistribution.cs
index 6244044e..294b5bda 100644
--- a/src/Numerics/Distributions/IUnivariateDistribution.cs
+++ b/src/Numerics/Distributions/IUnivariateDistribution.cs
@@ -30,18 +30,11 @@
namespace MathNet.Numerics.Distributions
{
- using System;
-
///
/// The interface for univariate distributions.
///
- public interface IUnivariateDistribution
+ public interface IUnivariateDistribution : IDistribution
{
- ///
- /// Gets or sets the random number generator which is used to generate random samples from the distribution.
- ///
- Random RandomSource { get; set; }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/InverseGamma.cs b/src/Numerics/Distributions/InverseGamma.cs
index d7213eb3..eab245d9 100644
--- a/src/Numerics/Distributions/InverseGamma.cs
+++ b/src/Numerics/Distributions/InverseGamma.cs
@@ -47,21 +47,11 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class InverseGamma : IContinuousDistribution
{
- ///
- /// Inverse Gamma shape parameter.
- ///
- double _shape;
+ System.Random _random;
- ///
- /// Inverse Gamma scale parameter scale.
- ///
+ double _shape;
double _scale;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -85,15 +75,31 @@ namespace MathNet.Numerics.Distributions
SetParameters(shape, scale);
}
+ ///
+ /// A string representation of the distribution.
+ ///
+ /// a string representation of the distribution.
+ public override string ToString()
+ {
+ return "InverseGamma(Shape = " + _shape + ", Inverse Scale = " + _scale + ")";
+ }
+
+ ///
+ /// Checks whether the parameters of the distribution are valid.
+ ///
+ /// The shape (alpha) parameter of the inverse Gamma distribution.
+ /// The scale (beta) parameter of the inverse Gamma distribution.
+ /// true when the parameters are valid, false otherwise.
+ static bool IsValidParameterSet(double shape, double scale)
+ {
+ return shape > 0.0 && scale > 0.0;
+ }
+
///
/// Sets the parameters of the distribution after checking their validity.
///
- ///
- /// The shape (alpha) parameter of the inverse Gamma distribution.
- ///
- ///
- /// The scale (beta) parameter of the inverse Gamma distribution.
- ///
+ /// The shape (alpha) parameter of the inverse Gamma distribution.
+ /// The scale (beta) parameter of the inverse Gamma distribution.
/// When the parameters don't pass the function.
void SetParameters(double shape, double scale)
{
@@ -107,14 +113,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Checks whether the parameters of the distribution are valid.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// The shape (alpha) parameter of the inverse Gamma distribution.
- /// The scale (beta) parameter of the inverse Gamma distribution.
- /// true when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(double shape, double scale)
+ public System.Random RandomSource
{
- return shape > 0.0 && scale > 0.0;
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -135,32 +139,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
- ///
- /// A string representation of the distribution.
- ///
- /// a string representation of the distribution.
- public override string ToString()
- {
- return "InverseGamma(Shape = " + _shape + ", Inverse Scale = " + _scale + ")";
- }
-
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/InverseWishart.cs b/src/Numerics/Distributions/InverseWishart.cs
index 902b94a5..3e24fbb1 100644
--- a/src/Numerics/Distributions/InverseWishart.cs
+++ b/src/Numerics/Distributions/InverseWishart.cs
@@ -46,8 +46,10 @@ namespace MathNet.Numerics.Distributions
/// The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to false, all parameter checks can be turned off.
- public class InverseWishart
+ public class InverseWishart : IDistribution
{
+ System.Random _random;
+
///
/// The degrees of freedom for the inverse Wishart distribution.
///
@@ -63,11 +65,6 @@ namespace MathNet.Numerics.Distributions
///
Cholesky _chol;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -100,24 +97,6 @@ namespace MathNet.Numerics.Distributions
return "InverseWishart(Nu = " + _nu + ", Rows = " + _s.RowCount + ", Columns = " + _s.ColumnCount + ")";
}
- ///
- /// Sets the parameters of the distribution after checking their validity.
- ///
- /// The degrees of freedom for the Wishart distribution.
- /// The scale matrix for the Wishart distribution.
- /// When the parameters don't pass the function.
- void SetParameters(double nu, Matrix s)
- {
- if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- _nu = nu;
- _s = s;
- _chol = Cholesky.Create(_s);
- }
-
///
/// Checks whether the parameters of the distribution are valid.
///
@@ -142,6 +121,33 @@ namespace MathNet.Numerics.Distributions
return nu > 0.0;
}
+ ///
+ /// Sets the parameters of the distribution after checking their validity.
+ ///
+ /// The degrees of freedom for the Wishart distribution.
+ /// The scale matrix for the Wishart distribution.
+ /// When the parameters don't pass the function.
+ void SetParameters(double nu, Matrix s)
+ {
+ if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
+ {
+ throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
+ }
+
+ _nu = nu;
+ _s = s;
+ _chol = Cholesky.Create(_s);
+ }
+
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
+ }
+
///
/// Gets or sets the degrees of freedom for the inverse Wishart distribution.
///
@@ -160,23 +166,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_nu, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean.
///
diff --git a/src/Numerics/Distributions/Laplace.cs b/src/Numerics/Distributions/Laplace.cs
index 0607a1f3..4c6b263b 100644
--- a/src/Numerics/Distributions/Laplace.cs
+++ b/src/Numerics/Distributions/Laplace.cs
@@ -48,16 +48,10 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Laplace : IContinuousDistribution
{
- ///
- /// The scale of the Laplace distribution.
- ///
- double _scale;
-
- ///
- /// The distribution's random number generator.
- ///
System.Random _random;
+ double _scale;
+
///
/// Gets or sets the location of the Laplace distribution.
///
@@ -110,20 +104,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Sets the parameters of the distribution after checking their validity.
+ /// A string representation of the distribution.
///
- /// The location for the Laplace distribution.
- /// The scale for the Laplace distribution.
- /// When the parameters don't pass the function.
- void SetParameters(double location, double scale)
+ /// a string representation of the distribution.
+ public override string ToString()
{
- if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- Mean = location;
- _scale = scale;
+ return "Laplace(Location = " + Mean + ", Scale = " + _scale + ")";
}
///
@@ -138,12 +124,20 @@ namespace MathNet.Numerics.Distributions
}
///
- /// A string representation of the distribution.
+ /// Sets the parameters of the distribution after checking their validity.
///
- /// a string representation of the distribution.
- public override string ToString()
+ /// The location for the Laplace distribution.
+ /// The scale for the Laplace distribution.
+ /// When the parameters don't pass the function.
+ 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;
}
///
@@ -152,15 +146,7 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource
{
get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
+ set { _random = value ?? new System.Random(); }
}
///
diff --git a/src/Numerics/Distributions/LogNormal.cs b/src/Numerics/Distributions/LogNormal.cs
index 3e08ed6f..0a0eaf37 100644
--- a/src/Numerics/Distributions/LogNormal.cs
+++ b/src/Numerics/Distributions/LogNormal.cs
@@ -48,21 +48,11 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class LogNormal : IContinuousDistribution
{
- ///
- /// Keeps track of the mu of the logarithm of the log-log-normal distribution.
- ///
- double _mu;
+ System.Random _random;
- ///
- /// Keeps track of the standard deviation of the logarithm of the log-log-normal distribution.
- ///
+ double _mu;
double _sigma;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
/// The distribution will be initialized with the default
@@ -149,6 +139,15 @@ namespace MathNet.Numerics.Distributions
_sigma = sigma;
}
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
+ }
+
///
/// Gets or sets the mean of the logarithm of the log-normal.
///
@@ -167,23 +166,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_mu, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mu of the log-normal distribution.
///
diff --git a/src/Numerics/Distributions/MatrixNormal.cs b/src/Numerics/Distributions/MatrixNormal.cs
index 68243253..bcdccb41 100644
--- a/src/Numerics/Distributions/MatrixNormal.cs
+++ b/src/Numerics/Distributions/MatrixNormal.cs
@@ -47,8 +47,10 @@ namespace MathNet.Numerics.Distributions
/// The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to false, all parameter checks can be turned off.
- public class MatrixNormal
+ public class MatrixNormal : IDistribution
{
+ System.Random _random;
+
///
/// The mean of the matrix normal distribution.
///
@@ -64,11 +66,6 @@ namespace MathNet.Numerics.Distributions
///
Matrix _k;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -107,55 +104,6 @@ namespace MathNet.Numerics.Distributions
return "MatrixNormal(Rows = " + _m.RowCount + ", Columns = " + _m.ColumnCount + ")";
}
- ///
- /// Gets or sets the mean. (M)
- ///
- /// The mean of the distribution.
- public Matrix Mean
- {
- get { return _m; }
- set { SetParameters(value, _v, _k); }
- }
-
- ///
- /// Gets or sets the row covariance. (V)
- ///
- /// The row covariance.
- public Matrix RowCovariance
- {
- get { return _v; }
- set { SetParameters(_m, value, _k); }
- }
-
- ///
- /// Gets or sets the column covariance. (K)
- ///
- /// The column covariance.
- public Matrix ColumnCovariance
- {
- get { return _k; }
- set { SetParameters(_m, _v, value); }
- }
-
- ///
- /// Sets the parameters of the distribution after checking their validity.
- ///
- /// The mean of the matrix normal.
- /// The covariance matrix for the rows.
- /// The covariance matrix for the columns.
- /// When the parameters don't pass the function.
- void SetParameters(Matrix m, Matrix v, Matrix k)
- {
- if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- _m = m;
- _v = v;
- _k = k;
- }
-
///
/// Checks whether the parameters of the distribution are valid.
///
@@ -196,21 +144,62 @@ namespace MathNet.Numerics.Distributions
return true;
}
+ ///
+ /// Sets the parameters of the distribution after checking their validity.
+ ///
+ /// The mean of the matrix normal.
+ /// The covariance matrix for the rows.
+ /// The covariance matrix for the columns.
+ /// When the parameters don't pass the function.
+ void SetParameters(Matrix m, Matrix v, Matrix k)
+ {
+ if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k))
+ {
+ throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
+ }
+
+ _m = m;
+ _v = v;
+ _k = k;
+ }
+
///
/// Gets or sets the random number generator which is used to draw random samples.
///
public System.Random RandomSource
{
get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
+ set { _random = value ?? new System.Random(); }
+ }
- _random = value;
- }
+ ///
+ /// Gets or sets the mean. (M)
+ ///
+ /// The mean of the distribution.
+ public Matrix Mean
+ {
+ get { return _m; }
+ set { SetParameters(value, _v, _k); }
+ }
+
+ ///
+ /// Gets or sets the row covariance. (V)
+ ///
+ /// The row covariance.
+ public Matrix RowCovariance
+ {
+ get { return _v; }
+ set { SetParameters(_m, value, _k); }
+ }
+
+ ///
+ /// Gets or sets the column covariance. (K)
+ ///
+ /// The column covariance.
+ public Matrix ColumnCovariance
+ {
+ get { return _k; }
+ set { SetParameters(_m, _v, value); }
}
///
diff --git a/src/Numerics/Distributions/Multinomial.cs b/src/Numerics/Distributions/Multinomial.cs
index 05f1a713..70bb9016 100644
--- a/src/Numerics/Distributions/Multinomial.cs
+++ b/src/Numerics/Distributions/Multinomial.cs
@@ -50,8 +50,10 @@ namespace MathNet.Numerics.Distributions
/// The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to false, all parameter checks can be turned off.
- public class Multinomial
+ public class Multinomial : IDistribution
{
+ System.Random _random;
+
///
/// Stores the normalized multinomial probabilities.
///
@@ -60,12 +62,7 @@ namespace MathNet.Numerics.Distributions
///
/// The number of trials.
///
- int _n;
-
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
+ int _trials;
///
/// Initializes a new instance of the Multinomial class.
@@ -130,7 +127,7 @@ namespace MathNet.Numerics.Distributions
/// a string representation of the distribution.
public override string ToString()
{
- return "Multinomial(Dimension = " + _p.Length + ", Number of Trails = " + _n + ")";
+ return "Multinomial(Dimension = " + _p.Length + ", Number of Trails = " + _trials + ")";
}
///
@@ -177,7 +174,16 @@ namespace MathNet.Numerics.Distributions
}
_p = (double[]) p.Clone();
- _n = n;
+ _trials = n;
+ }
+
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -186,7 +192,7 @@ namespace MathNet.Numerics.Distributions
public double[] P
{
get { return (double[]) _p.Clone(); }
- set { SetParameters(value, _n); }
+ set { SetParameters(value, _trials); }
}
///
@@ -194,33 +200,16 @@ namespace MathNet.Numerics.Distributions
///
public int N
{
- get { return _n; }
+ get { return _trials; }
set { SetParameters(_p, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
public Vector Mean
{
- get { return _n*(DenseVector) P; }
+ get { return _trials*(DenseVector) P; }
}
///
@@ -234,7 +223,7 @@ namespace MathNet.Numerics.Distributions
var res = (DenseVector) P;
for (var i = 0; i < res.Count; i++)
{
- res[i] *= _n*(1 - res[i]);
+ res[i] *= _trials*(1 - res[i]);
}
return res;
@@ -252,7 +241,7 @@ namespace MathNet.Numerics.Distributions
var res = (DenseVector) P;
for (var i = 0; i < res.Count; i++)
{
- res[i] = (1.0 - (2.0*res[i]))/Math.Sqrt(_n*(1.0 - res[i])*res[i]);
+ res[i] = (1.0 - (2.0*res[i]))/Math.Sqrt(_trials*(1.0 - res[i])*res[i]);
}
return res;
@@ -278,9 +267,9 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "x");
}
- if (x.Sum() == _n)
+ if (x.Sum() == _trials)
{
- var coef = SpecialFunctions.Multinomial(_n, x);
+ var coef = SpecialFunctions.Multinomial(_trials, x);
var num = 1.0;
for (var i = 0; i < x.Length; i++)
{
@@ -312,9 +301,9 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "x");
}
- if (x.Sum() == _n)
+ if (x.Sum() == _trials)
{
- var coef = Math.Log(SpecialFunctions.Multinomial(_n, x));
+ var coef = Math.Log(SpecialFunctions.Multinomial(_trials, x));
var num = x.Select((t, i) => t*Math.Log(_p[i])).Sum();
return coef + num;
}
@@ -328,7 +317,7 @@ namespace MathNet.Numerics.Distributions
/// the counts for each of the different possible values.
public int[] Sample()
{
- return Sample(RandomSource, _p, _n);
+ return Sample(RandomSource, _p, _trials);
}
///
@@ -339,7 +328,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
- yield return Sample(RandomSource, _p, _n);
+ yield return Sample(RandomSource, _p, _trials);
}
}
diff --git a/src/Numerics/Distributions/NegativeBinomial.cs b/src/Numerics/Distributions/NegativeBinomial.cs
index a657b7ed..e9cea310 100644
--- a/src/Numerics/Distributions/NegativeBinomial.cs
+++ b/src/Numerics/Distributions/NegativeBinomial.cs
@@ -48,27 +48,21 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class NegativeBinomial : IDiscreteDistribution
{
- ///
- /// The r parameter of the distribution.
- ///
- double _r;
+ System.Random _random;
+
+ double _trials;
///
/// The p parameter of the distribution.
///
double _p;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Gets or sets the number of trials.
///
public double R
{
- get { return _r; }
+ get { return _trials; }
set { SetParameters(value, _p); }
}
@@ -78,7 +72,7 @@ namespace MathNet.Numerics.Distributions
public double P
{
get { return _p; }
- set { SetParameters(_r, value); }
+ set { SetParameters(_trials, value); }
}
///
@@ -105,20 +99,14 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Sets the parameters of the distribution after checking their validity.
+ /// Returns a that represents this instance.
///
- /// The number of trials.
- /// The probability of a trial resulting in success.
- /// When the parameters don't pass the function.
- void SetParameters(double r, double p)
+ ///
+ /// A that represents this instance.
+ ///
+ public override string ToString()
{
- if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- _p = p;
- _r = r;
+ return "NegativeBinomial(R = " + _trials + ", P = " + _p + ")";
}
///
@@ -133,39 +121,36 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Returns a that represents this instance.
+ /// Sets the parameters of the distribution after checking their validity.
///
- ///
- /// A that represents this instance.
- ///
- public override string ToString()
+ /// The number of trials.
+ /// The probability of a trial resulting in success.
+ /// When the parameters don't pass the function.
+ 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;
}
///
- /// Gets or sets the distribution's random number generator.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
public System.Random RandomSource
{
get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
+ set { _random = value ?? new System.Random(); }
}
-
///
/// Gets the mean of the distribution.
///
public double Mean
{
- get { return _r*(1.0 - _p)/_p; }
+ get { return _trials*(1.0 - _p)/_p; }
}
///
@@ -173,7 +158,7 @@ namespace MathNet.Numerics.Distributions
///
public double Variance
{
- get { return _r*(1.0 - _p)/(_p*_p); }
+ get { return _trials*(1.0 - _p)/(_p*_p); }
}
///
@@ -181,7 +166,7 @@ namespace MathNet.Numerics.Distributions
///
public double StdDev
{
- get { return Math.Sqrt(_r*(1.0 - _p))/_p; }
+ get { return Math.Sqrt(_trials*(1.0 - _p))/_p; }
}
///
@@ -197,7 +182,7 @@ namespace MathNet.Numerics.Distributions
///
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)); }
}
///
@@ -205,7 +190,7 @@ namespace MathNet.Numerics.Distributions
///
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; }
}
///
@@ -239,10 +224,10 @@ namespace MathNet.Numerics.Distributions
/// the probability mass at location .
public double Probability(int k)
{
- var ln = SpecialFunctions.GammaLn(_r + k)
- - SpecialFunctions.GammaLn(_r)
+ var ln = SpecialFunctions.GammaLn(_trials + k)
+ - SpecialFunctions.GammaLn(_trials)
- SpecialFunctions.GammaLn(k + 1.0)
- + (_r*Math.Log(_p))
+ + (_trials*Math.Log(_p))
+ (k*Math.Log(1.0 - _p));
return Math.Exp(ln);
}
@@ -254,10 +239,10 @@ namespace MathNet.Numerics.Distributions
/// the log probability mass at location .
public double ProbabilityLn(int k)
{
- var ln = SpecialFunctions.GammaLn(_r + k)
- - SpecialFunctions.GammaLn(_r)
+ var ln = SpecialFunctions.GammaLn(_trials + k)
+ - SpecialFunctions.GammaLn(_trials)
- SpecialFunctions.GammaLn(k + 1.0)
- + (_r*Math.Log(_p))
+ + (_trials*Math.Log(_p))
+ (k*Math.Log(1.0 - _p));
return ln;
}
@@ -269,7 +254,7 @@ namespace MathNet.Numerics.Distributions
/// the cumulative distribution at location .
public double CumulativeDistribution(double x)
{
- return 1 - SpecialFunctions.BetaRegularized(x + 1, _r, 1 - _p);
+ return 1 - SpecialFunctions.BetaRegularized(x + 1, _trials, 1 - _p);
}
///
@@ -299,7 +284,7 @@ namespace MathNet.Numerics.Distributions
/// a sample from the distribution.
public int Sample()
{
- return SampleUnchecked(RandomSource, _r, _p);
+ return SampleUnchecked(RandomSource, _trials, _p);
}
///
@@ -310,7 +295,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
- yield return SampleUnchecked(RandomSource, _r, _p);
+ yield return SampleUnchecked(RandomSource, _trials, _p);
}
}
diff --git a/src/Numerics/Distributions/Normal.cs b/src/Numerics/Distributions/Normal.cs
index 046a26b6..b239f011 100644
--- a/src/Numerics/Distributions/Normal.cs
+++ b/src/Numerics/Distributions/Normal.cs
@@ -47,21 +47,11 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Normal : IContinuousDistribution
{
- ///
- /// Keeps track of the mean of the normal distribution.
- ///
- double _mean;
+ System.Random _random;
- ///
- /// Keeps track of the standard deviation of the normal distribution.
- ///
+ double _mean;
double _stdDev;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the Normal class. This is a normal distribution with mean 0.0
/// and standard deviation 1.0. The distribution will
@@ -190,6 +180,15 @@ namespace MathNet.Numerics.Distributions
_stdDev = stddev;
}
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
+ }
+
///
/// Gets or sets the precision of the normal distribution.
///
@@ -211,23 +210,6 @@ namespace MathNet.Numerics.Distributions
}
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets or sets the mean of the normal distribution.
///
diff --git a/src/Numerics/Distributions/NormalGamma.cs b/src/Numerics/Distributions/NormalGamma.cs
index 1f13c115..52866905 100644
--- a/src/Numerics/Distributions/NormalGamma.cs
+++ b/src/Numerics/Distributions/NormalGamma.cs
@@ -100,33 +100,15 @@ namespace MathNet.Numerics.Distributions
/// The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to false, all parameter checks can be turned off.
- public class NormalGamma
+ public class NormalGamma : IDistribution
{
- ///
- /// The location of the mean.
- ///
- double _meanLocation;
+ System.Random _random;
- ///
- /// The scale of the mean.
- ///
+ double _meanLocation;
double _meanScale;
-
- ///
- /// The shape of the precision.
- ///
double _precisionShape;
-
- ///
- /// The inverse scale of the precision.
- ///
double _precisionInvScale;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -154,6 +136,16 @@ namespace MathNet.Numerics.Distributions
SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale);
}
+ ///
+ /// A string representation of the distribution.
+ ///
+ /// a string representation of the distribution.
+ public override string ToString()
+ {
+ return "NormalGamma(Mean Location = " + _meanLocation + ", Mean Scale = " + _meanScale +
+ ", Precision Shape = " + _precisionShape + ", Precision Inverse Scale = " + _precisionInvScale + ")";
+ }
+
///
/// Checks whether the parameters of the distribution are valid.
///
@@ -189,13 +181,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// A string representation of the distribution.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// a string representation of the distribution.
- public override string ToString()
+ public System.Random RandomSource
{
- return "NormalGamma(Mean Location = " + _meanLocation + ", Mean Scale = " + _meanScale +
- ", Precision Shape = " + _precisionShape + ", Precision Inverse Scale = " + _precisionInvScale + ")";
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -234,23 +225,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Returns the marginal distribution for the mean of the NormalGamma distribution.
///
diff --git a/src/Numerics/Distributions/Pareto.cs b/src/Numerics/Distributions/Pareto.cs
index 4d514356..2fc0d794 100644
--- a/src/Numerics/Distributions/Pareto.cs
+++ b/src/Numerics/Distributions/Pareto.cs
@@ -48,21 +48,11 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Pareto : IContinuousDistribution
{
- ///
- /// The scale parameter of the distribution.
- ///
- double _scale;
+ System.Random _random;
- ///
- /// The shape parameter of the distribution.
- ///
+ double _scale;
double _shape;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -88,6 +78,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(scale, shape);
}
+ ///
+ /// A string representation of the distribution.
+ ///
+ /// a string representation of the distribution.
+ public override string ToString()
+ {
+ return "Pareto(Scale = " + _scale + ", Shape = " + _shape + ")";
+ }
+
+ ///
+ /// Checks whether the parameters of the distribution are valid.
+ ///
+ /// The scale parameter of the distribution.
+ /// The shape parameter of the distribution.
+ /// true when the parameters are valid, false otherwise.
+ static bool IsValidParameterSet(double scale, double shape)
+ {
+ return scale > 0.0 && shape > 0.0;
+ }
+
///
/// Sets the parameters of the distribution after checking their validity.
///
@@ -106,14 +116,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Checks whether the parameters of the distribution are valid.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// The scale parameter of the distribution.
- /// The shape parameter of the distribution.
- /// true when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(double scale, double shape)
+ public System.Random RandomSource
{
- return scale > 0.0 && shape > 0.0;
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -134,32 +142,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_scale, value); }
}
- ///
- /// A string representation of the distribution.
- ///
- /// a string representation of the distribution.
- public override string ToString()
- {
- return "Pareto(Scale = " + _scale + ", Shape = " + _shape + ")";
- }
-
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/Poisson.cs b/src/Numerics/Distributions/Poisson.cs
index 6237fcbf..2b6e41d2 100644
--- a/src/Numerics/Distributions/Poisson.cs
+++ b/src/Numerics/Distributions/Poisson.cs
@@ -44,16 +44,10 @@ namespace MathNet.Numerics.Distributions
///
public class Poisson : IDiscreteDistribution
{
- ///
- /// The Poisson distribution parameter λ.
- ///
- double _lambda;
-
- ///
- /// The distribution's random number generator.
- ///
System.Random _random;
+ double _lambda;
+
///
/// Gets or sets the Poisson distribution parameter λ.
///
@@ -87,18 +81,14 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Sets the parameters of the distribution after checking their validity.
+ /// Returns a that represents this instance.
///
- /// The mean (λ) of the distribution.
- /// When the parameters don't pass the function.
- void SetParameters(double lambda)
+ ///
+ /// A that represents this instance.
+ ///
+ public override string ToString()
{
- if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- _lambda = lambda;
+ return "Poisson(λ = " + _lambda + ")";
}
///
@@ -112,14 +102,18 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Returns a that represents this instance.
+ /// Sets the parameters of the distribution after checking their validity.
///
- ///
- /// A that represents this instance.
- ///
- public override string ToString()
+ /// The mean (λ) of the distribution.
+ /// When the parameters don't pass the function.
+ void SetParameters(double lambda)
{
- return "Poisson(λ = " + _lambda + ")";
+ if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
+ {
+ throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
+ }
+
+ _lambda = lambda;
}
///
@@ -128,15 +122,7 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource
{
get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
+ set { _random = value ?? new System.Random(); }
}
///
diff --git a/src/Numerics/Distributions/Rayleigh.cs b/src/Numerics/Distributions/Rayleigh.cs
index 2a79a5c2..204cbc83 100644
--- a/src/Numerics/Distributions/Rayleigh.cs
+++ b/src/Numerics/Distributions/Rayleigh.cs
@@ -49,16 +49,10 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Rayleigh : IContinuousDistribution
{
- ///
- /// The scale parameter of the distribution.
- ///
- double _scale;
-
- ///
- /// The distribution's random number generator.
- ///
System.Random _random;
+ double _scale;
+
///
/// Initializes a new instance of the class.
///
@@ -83,18 +77,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Sets the parameters of the distribution after checking their validity.
+ /// A string representation of the distribution.
///
- /// The scale parameter of the distribution.
- /// When the parameters don't pass the function.
- void SetParameters(double scale)
+ /// a string representation of the distribution.
+ public override string ToString()
{
- if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
- {
- throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
- }
-
- _scale = scale;
+ return "Rayleigh(Scale = " + _scale + ")";
}
///
@@ -108,21 +96,18 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Gets or sets the scale parameter of the distribution.
+ /// Sets the parameters of the distribution after checking their validity.
///
- public double Scale
+ /// The scale parameter of the distribution.
+ /// When the parameters don't pass the function.
+ void SetParameters(double scale)
{
- get { return _scale; }
- set { SetParameters(value); }
- }
+ if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
+ {
+ throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
+ }
- ///
- /// A string representation of the distribution.
- ///
- /// a string representation of the distribution.
- public override string ToString()
- {
- return "Rayleigh(Scale = " + _scale + ")";
+ _scale = scale;
}
///
@@ -131,15 +116,16 @@ namespace MathNet.Numerics.Distributions
public System.Random RandomSource
{
get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
+ set { _random = value ?? new System.Random(); }
+ }
- _random = value;
- }
+ ///
+ /// Gets or sets the scale parameter of the distribution.
+ ///
+ public double Scale
+ {
+ get { return _scale; }
+ set { SetParameters(value); }
}
///
diff --git a/src/Numerics/Distributions/Stable.cs b/src/Numerics/Distributions/Stable.cs
index e3f8855f..583b80a4 100644
--- a/src/Numerics/Distributions/Stable.cs
+++ b/src/Numerics/Distributions/Stable.cs
@@ -49,6 +49,8 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Stable : IContinuousDistribution
{
+ System.Random _random;
+
///
/// The stability parameter of the distribution.
///
@@ -59,21 +61,9 @@ namespace MathNet.Numerics.Distributions
///
double _beta;
- ///
- /// The scale parameter of the distribution.
- ///
double _scale;
-
- ///
- /// The location parameter of the distribution.
- ///
double _location;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -101,6 +91,28 @@ namespace MathNet.Numerics.Distributions
SetParameters(alpha, beta, scale, location);
}
+ ///
+ /// A string representation of the distribution.
+ ///
+ /// a string representation of the distribution.
+ public override string ToString()
+ {
+ return "Stable(" + "Stability = " + _alpha + ", Skewness = " + _beta + ", Scale = " + _scale + ", Location = " + _location + ")";
+ }
+
+ ///
+ /// Checks whether the parameters of the distribution are valid.
+ ///
+ /// The stability parameter of the distribution.
+ /// The skewness parameter of the distribution.
+ /// The scale parameter of the distribution.
+ /// The location parameter of the distribution.
+ /// true when the parameters are valid, false otherwise.
+ 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);
+ }
+
///
/// Sets the parameters of the distribution after checking their validity.
///
@@ -122,16 +134,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Checks whether the parameters of the distribution are valid.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// The stability parameter of the distribution.
- /// The skewness parameter of the distribution.
- /// The scale parameter of the distribution.
- /// The location parameter of the distribution.
- /// true when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(double alpha, double beta, double scale, double location)
+ public System.Random RandomSource
{
- return alpha > 0.0 && alpha <= 2.0 && beta >= -1.0 && beta <= 1.0 && scale > 0.0 && !Double.IsNaN(location);
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -170,32 +178,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_alpha, _beta, _scale, value); }
}
- ///
- /// A string representation of the distribution.
- ///
- /// a string representation of the distribution.
- public override string ToString()
- {
- return "Stable(" + "Stability = " + _alpha + ", Skewness = " + _beta + ", Scale = " + _scale + ", Location = " + _location + ")";
- }
-
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Distributions/StudentT.cs b/src/Numerics/Distributions/StudentT.cs
index f5955a44..9b7f7782 100644
--- a/src/Numerics/Distributions/StudentT.cs
+++ b/src/Numerics/Distributions/StudentT.cs
@@ -57,25 +57,11 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class StudentT : IContinuousDistribution
{
- ///
- /// Keeps track of the location of the Student t-distribution.
- ///
- double _location;
-
- ///
- /// Keeps track of the degrees of freedom for the Student t-distribution.
- ///
- double _dof;
+ System.Random _random;
- ///
- /// Keeps track of the scale for the Student t-distribution.
- ///
+ double _location;
double _scale;
-
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
+ double _freedom;
///
/// 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
/// a string representation of the distribution.
public override string ToString()
{
- return "StudentT(Location = " + _location + ", Scale = " + _scale + ", DoF = " + _dof + ")";
+ return "StudentT(Location = " + _location + ", Scale = " + _scale + ", DoF = " + _freedom + ")";
}
///
@@ -153,7 +139,16 @@ namespace MathNet.Numerics.Distributions
_location = location;
_scale = scale;
- _dof = dof;
+ _freedom = dof;
+ }
+
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -162,7 +157,7 @@ namespace MathNet.Numerics.Distributions
public double Location
{
get { return _location; }
- set { SetParameters(value, _scale, _dof); }
+ set { SetParameters(value, _scale, _freedom); }
}
///
@@ -171,7 +166,7 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
- set { SetParameters(_location, value, _dof); }
+ set { SetParameters(_location, value, _freedom); }
}
///
@@ -179,33 +174,16 @@ namespace MathNet.Numerics.Distributions
///
public double DegreesOfFreedom
{
- get { return _dof; }
+ get { return _freedom; }
set { SetParameters(_location, _scale, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the Student t-distribution.
///
public double Mean
{
- get { return _dof > 1.0 ? _location : Double.NaN; }
+ get { return _freedom > 1.0 ? _location : Double.NaN; }
}
///
@@ -215,17 +193,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
- if (Double.IsPositiveInfinity(_dof))
+ if (Double.IsPositiveInfinity(_freedom))
{
return _scale*_scale;
}
- if (_dof > 2.0)
+ if (_freedom > 2.0)
{
- return _dof*_scale*_scale/(_dof - 2.0);
+ return _freedom*_scale*_scale/(_freedom - 2.0);
}
- return _dof > 1.0 ? Double.PositiveInfinity : Double.NaN;
+ return _freedom > 1.0 ? Double.PositiveInfinity : Double.NaN;
}
}
@@ -236,17 +214,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
- if (Double.IsPositiveInfinity(_dof))
+ if (Double.IsPositiveInfinity(_freedom))
{
return Math.Sqrt(_scale*_scale);
}
- if (_dof > 2.0)
+ if (_freedom > 2.0)
{
- return Math.Sqrt(_dof*_scale*_scale/(_dof - 2.0));
+ return Math.Sqrt(_freedom*_scale*_scale/(_freedom - 2.0));
}
- return _dof > 1.0 ? Double.PositiveInfinity : Double.NaN;
+ return _freedom > 1.0 ? Double.PositiveInfinity : Double.NaN;
}
}
@@ -262,7 +240,7 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
- return (((_dof + 1.0)/2.0)*(SpecialFunctions.DiGamma((1.0 + _dof)/2.0) - SpecialFunctions.DiGamma(_dof/2.0))) + Math.Log(Math.Sqrt(_dof)*SpecialFunctions.Beta(_dof/2.0, 1.0/2.0));
+ return (((_freedom + 1.0)/2.0)*(SpecialFunctions.DiGamma((1.0 + _freedom)/2.0) - SpecialFunctions.DiGamma(_freedom/2.0))) + Math.Log(Math.Sqrt(_freedom)*SpecialFunctions.Beta(_freedom/2.0, 1.0/2.0));
}
}
@@ -273,7 +251,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
- if (_dof <= 3)
+ if (_freedom <= 3)
{
throw new NotSupportedException();
}
@@ -322,15 +300,15 @@ namespace MathNet.Numerics.Distributions
public double Density(double x)
{
// TODO JVG we can probably do a better job for Cauchy special case
- if (_dof >= 1e+8d)
+ if (_freedom >= 1e+8d)
{
return Normal.Density(_location, _scale, x);
}
var d = (x - _location)/_scale;
- return Math.Exp(SpecialFunctions.GammaLn((_dof + 1.0)/2.0) - SpecialFunctions.GammaLn(_dof/2.0))
- *Math.Pow(1.0 + (d*d/_dof), -0.5*(_dof + 1.0))
- /Math.Sqrt(_dof*Math.PI)
+ return Math.Exp(SpecialFunctions.GammaLn((_freedom + 1.0)/2.0) - SpecialFunctions.GammaLn(_freedom/2.0))
+ *Math.Pow(1.0 + (d*d/_freedom), -0.5*(_freedom + 1.0))
+ /Math.Sqrt(_freedom*Math.PI)
/_scale;
}
@@ -342,16 +320,16 @@ namespace MathNet.Numerics.Distributions
public double DensityLn(double x)
{
// TODO JVG we can probably do a better job for Cauchy special case
- if (_dof >= 1e+8d)
+ if (_freedom >= 1e+8d)
{
return Normal.DensityLn(_location, _scale, x);
}
var d = (x - _location)/_scale;
- return SpecialFunctions.GammaLn((_dof + 1.0)/2.0)
- - (0.5*((_dof + 1.0)*Math.Log(1.0 + (d*d/_dof))))
- - SpecialFunctions.GammaLn(_dof/2.0)
- - (0.5*Math.Log(_dof*Math.PI)) - Math.Log(_scale);
+ return SpecialFunctions.GammaLn((_freedom + 1.0)/2.0)
+ - (0.5*((_freedom + 1.0)*Math.Log(1.0 + (d*d/_freedom))))
+ - SpecialFunctions.GammaLn(_freedom/2.0)
+ - (0.5*Math.Log(_freedom*Math.PI)) - Math.Log(_scale);
}
///
@@ -362,14 +340,14 @@ namespace MathNet.Numerics.Distributions
public double CumulativeDistribution(double x)
{
// TODO JVG we can probably do a better job for Cauchy special case
- if (Double.IsPositiveInfinity(_dof))
+ if (Double.IsPositiveInfinity(_freedom))
{
return Normal.CumulativeDistribution(_location, _scale, x);
}
var k = (x - _location)/_scale;
- var h = _dof/(_dof + (k*k));
- var ib = 0.5*SpecialFunctions.BetaRegularized(_dof/2.0, 0.5, h);
+ var h = _freedom/(_freedom + (k*k));
+ var ib = 0.5*SpecialFunctions.BetaRegularized(_freedom/2.0, 0.5, h);
return x <= _location ? ib : 1.0 - ib;
}
@@ -396,7 +374,7 @@ namespace MathNet.Numerics.Distributions
/// a sample from the distribution.
public double Sample()
{
- return SampleUnchecked(RandomSource, _location, _scale, _dof);
+ return SampleUnchecked(RandomSource, _location, _scale, _freedom);
}
///
@@ -407,7 +385,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
- yield return SampleUnchecked(RandomSource, _location, _scale, _dof);
+ yield return SampleUnchecked(RandomSource, _location, _scale, _freedom);
}
}
diff --git a/src/Numerics/Distributions/Weibull.cs b/src/Numerics/Distributions/Weibull.cs
index 085f5db3..90846d2b 100644
--- a/src/Numerics/Distributions/Weibull.cs
+++ b/src/Numerics/Distributions/Weibull.cs
@@ -48,18 +48,13 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Weibull : IContinuousDistribution
{
- ///
- /// Weibull shape parameter.
- ///
- double _shape;
+ System.Random _random;
- ///
- /// Weibull inverse scale parameter.
- ///
+ double _shape;
double _scale;
///
- /// Reusable intermediate result 1 / ( ^ )
+ /// Reusable intermediate result 1 / (_scale ^ _shape)
///
///
/// By caching this parameter we can get slightly better numerics precision
@@ -67,11 +62,6 @@ namespace MathNet.Numerics.Distributions
///
double _scalePowShapeInv;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the Weibull class.
///
@@ -133,6 +123,15 @@ namespace MathNet.Numerics.Distributions
_scalePowShapeInv = Math.Pow(scale, -shape);
}
+ ///
+ /// Gets or sets the random number generator which is used to draw random samples.
+ ///
+ public System.Random RandomSource
+ {
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
+ }
+
///
/// Gets or sets the shape of the Weibull distribution.
///
@@ -151,23 +150,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the Weibull distribution.
///
diff --git a/src/Numerics/Distributions/Wishart.cs b/src/Numerics/Distributions/Wishart.cs
index 6b3ed40f..9e88d661 100644
--- a/src/Numerics/Distributions/Wishart.cs
+++ b/src/Numerics/Distributions/Wishart.cs
@@ -50,6 +50,8 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Wishart
{
+ System.Random _random;
+
///
/// The degrees of freedom for the Wishart distribution.
///
@@ -65,11 +67,6 @@ namespace MathNet.Numerics.Distributions
///
Cholesky _chol;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
diff --git a/src/Numerics/Distributions/Zipf.cs b/src/Numerics/Distributions/Zipf.cs
index 41c85a79..6ca4a08f 100644
--- a/src/Numerics/Distributions/Zipf.cs
+++ b/src/Numerics/Distributions/Zipf.cs
@@ -49,6 +49,8 @@ namespace MathNet.Numerics.Distributions
/// to false, all parameter checks can be turned off.
public class Zipf : IDiscreteDistribution
{
+ System.Random _random;
+
///
/// The s parameter of the distribution.
///
@@ -59,11 +61,6 @@ namespace MathNet.Numerics.Distributions
///
int _n;
- ///
- /// The distribution's random number generator.
- ///
- System.Random _random;
-
///
/// Initializes a new instance of the class.
///
@@ -87,6 +84,26 @@ namespace MathNet.Numerics.Distributions
SetParameters(s, n);
}
+ ///
+ /// A string representation of the distribution.
+ ///
+ /// a string representation of the distribution.
+ public override string ToString()
+ {
+ return "Zipf(S = " + _s + ", N = " + _n + ")";
+ }
+
+ ///
+ /// Checks whether the parameters of the distribution are valid.
+ ///
+ /// The s parameter of the distribution.
+ /// The n parameter of the distribution.
+ /// true when the parameters are valid, false otherwise.
+ static bool IsValidParameterSet(double s, int n)
+ {
+ return n > 0 && s > 0.0;
+ }
+
///
/// Sets the parameters of the distribution after checking their validity.
///
@@ -104,14 +121,12 @@ namespace MathNet.Numerics.Distributions
}
///
- /// Checks whether the parameters of the distribution are valid.
+ /// Gets or sets the random number generator which is used to draw random samples.
///
- /// The s parameter of the distribution.
- /// The n parameter of the distribution.
- /// true when the parameters are valid, false otherwise.
- static bool IsValidParameterSet(double s, int n)
+ public System.Random RandomSource
{
- return n > 0 && s > 0.0;
+ get { return _random; }
+ set { _random = value ?? new System.Random(); }
}
///
@@ -132,32 +147,6 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_s, value); }
}
- ///
- /// A string representation of the distribution.
- ///
- /// a string representation of the distribution.
- public override string ToString()
- {
- return "Zipf(S = " + _s + ", N = " + _n + ")";
- }
-
- ///
- /// Gets or sets the random number generator which is used to draw random samples.
- ///
- public System.Random RandomSource
- {
- get { return _random; }
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException();
- }
-
- _random = value;
- }
- }
-
///
/// Gets the mean of the distribution.
///
diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj
index f2c191a8..0adb9ca3 100644
--- a/src/Numerics/Numerics.csproj
+++ b/src/Numerics/Numerics.csproj
@@ -95,6 +95,7 @@
+
diff --git a/src/UnitTests/DistributionTests/CommonDistributionTests.cs b/src/UnitTests/DistributionTests/CommonDistributionTests.cs
index a964c45e..ca28005e 100644
--- a/src/UnitTests/DistributionTests/CommonDistributionTests.cs
+++ b/src/UnitTests/DistributionTests/CommonDistributionTests.cs
@@ -160,22 +160,19 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
}
}
- ///
- /// Fail set random source with null reference.
- ///
[Test]
- public void FailSetRandomSourceWithNullReference()
+ public void HasRandomSourceEvenAfterSetToNull()
{
foreach (var dd in _discreteDistributions)
{
- var dd1 = dd;
- Assert.Throws(() => dd1.RandomSource = null);
+ Assert.DoesNotThrow(() => dd.RandomSource = null);
+ Assert.IsNotNull(dd.RandomSource);
}
foreach (var cd in _continuousDistributions)
{
- var cd1 = cd;
- Assert.Throws(() => cd1.RandomSource = null);
+ Assert.DoesNotThrow(() => cd.RandomSource = null);
+ Assert.IsNotNull(cd.RandomSource);
}
}
diff --git a/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs b/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs
index 009360bc..a03f1622 100644
--- a/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs
+++ b/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs
@@ -114,14 +114,12 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
};
}
- ///
- /// Fail set random source with null reference.
- ///
[Test]
- public void FailSetRandomSourceWithNullReference()
+ public void HasRandomSourceEvenAfterSetToNull()
{
var d = new Dirichlet(0.3, 5);
- Assert.Throws(() => d.RandomSource = null);
+ Assert.DoesNotThrow(() => d.RandomSource = null);
+ Assert.IsNotNull(d.RandomSource);
}
///
diff --git a/src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs b/src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs
index 5725ee11..24a90a34 100644
--- a/src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs
+++ b/src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs
@@ -121,14 +121,12 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
};
}
- ///
- /// Fail set random source with null reference.
- ///
[Test]
- public void FailSetRandomSourceWithNullReference()
+ public void HasRandomSourceEvenAfterSetToNull()
{
var d = new InverseWishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2));
- Assert.Throws(() => d.RandomSource = null);
+ Assert.DoesNotThrow(() => d.RandomSource = null);
+ Assert.IsNotNull(d.RandomSource);
}
///
diff --git a/src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs b/src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs
index f8145ebe..8353faa1 100644
--- a/src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs
+++ b/src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs
@@ -130,16 +130,14 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
};
}
- ///
- /// Fail set random source with null reference.
- ///
[Test]
- public void FailSetRandomSourceWithNullReference()
+ public void HasRandomSourceEvenAfterSetToNull()
{
const int N = 2;
const int P = 3;
var d = new MatrixNormal(MatrixLoader.GenerateRandomDenseMatrix(N, P), MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(N), MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(P));
- Assert.Throws(() => d.RandomSource = null);
+ Assert.DoesNotThrow(() => d.RandomSource = null);
+ Assert.IsNotNull(d.RandomSource);
}
///