Browse Source

Distributions: immutable in distribution parameters

provider
Christoph Ruegg 12 years ago
parent
commit
f7b8c664a0
  1. 32
      src/Numerics/Distributions/Bernoulli.cs
  2. 39
      src/Numerics/Distributions/Beta.cs
  3. 40
      src/Numerics/Distributions/Binomial.cs
  4. 82
      src/Numerics/Distributions/Categorical.cs
  5. 39
      src/Numerics/Distributions/Cauchy.cs
  6. 32
      src/Numerics/Distributions/Chi.cs
  7. 32
      src/Numerics/Distributions/ChiSquared.cs
  8. 41
      src/Numerics/Distributions/ContinuousUniform.cs
  9. 39
      src/Numerics/Distributions/ConwayMaxwellPoisson.cs
  10. 46
      src/Numerics/Distributions/Dirichlet.cs
  11. 39
      src/Numerics/Distributions/DiscreteUniform.cs
  12. 48
      src/Numerics/Distributions/Erlang.cs
  13. 32
      src/Numerics/Distributions/Exponential.cs
  14. 39
      src/Numerics/Distributions/FisherSnedecor.cs
  15. 48
      src/Numerics/Distributions/Gamma.cs
  16. 32
      src/Numerics/Distributions/Geometric.cs
  17. 76
      src/Numerics/Distributions/Hypergeometric.cs
  18. 39
      src/Numerics/Distributions/InverseGamma.cs
  19. 44
      src/Numerics/Distributions/InverseWishart.cs
  20. 39
      src/Numerics/Distributions/Laplace.cs
  21. 39
      src/Numerics/Distributions/LogNormal.cs
  22. 46
      src/Numerics/Distributions/MatrixNormal.cs
  23. 48
      src/Numerics/Distributions/Multinomial.cs
  24. 39
      src/Numerics/Distributions/NegativeBinomial.cs
  25. 50
      src/Numerics/Distributions/Normal.cs
  26. 55
      src/Numerics/Distributions/NormalGamma.cs
  27. 39
      src/Numerics/Distributions/Pareto.cs
  28. 32
      src/Numerics/Distributions/Poisson.cs
  29. 32
      src/Numerics/Distributions/Rayleigh.cs
  30. 54
      src/Numerics/Distributions/Stable.cs
  31. 52
      src/Numerics/Distributions/StudentT.cs
  32. 48
      src/Numerics/Distributions/Triangular.cs
  33. 44
      src/Numerics/Distributions/Weibull.cs
  34. 44
      src/Numerics/Distributions/Wishart.cs
  35. 39
      src/Numerics/Distributions/Zipf.cs
  36. 56
      src/UnitTests/DistributionTests/Continuous/BetaTests.cs
  37. 52
      src/UnitTests/DistributionTests/Continuous/CauchyTests.cs
  38. 29
      src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs
  39. 29
      src/UnitTests/DistributionTests/Continuous/ChiTests.cs
  40. 52
      src/UnitTests/DistributionTests/Continuous/ContinuousUniformTests.cs
  41. 82
      src/UnitTests/DistributionTests/Continuous/ErlangTests.cs
  42. 28
      src/UnitTests/DistributionTests/Continuous/ExponentialTests.cs
  43. 52
      src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs
  44. 84
      src/UnitTests/DistributionTests/Continuous/GammaTests.cs
  45. 58
      src/UnitTests/DistributionTests/Continuous/InverseGammaTests.cs
  46. 59
      src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs
  47. 48
      src/UnitTests/DistributionTests/Continuous/LogNormalTests.cs
  48. 107
      src/UnitTests/DistributionTests/Continuous/NormalTests.cs
  49. 52
      src/UnitTests/DistributionTests/Continuous/ParetoTests.cs
  50. 31
      src/UnitTests/DistributionTests/Continuous/RayleighTests.cs
  51. 122
      src/UnitTests/DistributionTests/Continuous/StableTests.cs
  52. 78
      src/UnitTests/DistributionTests/Continuous/StudentTTests.cs
  53. 30
      src/UnitTests/DistributionTests/Continuous/TriangularTests.cs
  54. 58
      src/UnitTests/DistributionTests/Continuous/WeibullTests.cs
  55. 28
      src/UnitTests/DistributionTests/Discrete/BernoulliTests.cs
  56. 29
      src/UnitTests/DistributionTests/Discrete/BinomialTests.cs
  57. 22
      src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs
  58. 60
      src/UnitTests/DistributionTests/Discrete/ConwayMaxwellPoissonTests.cs
  59. 68
      src/UnitTests/DistributionTests/Discrete/DiscreteUniformTests.cs
  60. 28
      src/UnitTests/DistributionTests/Discrete/GeometricTests.cs
  61. 83
      src/UnitTests/DistributionTests/Discrete/HypergeometricTests.cs
  62. 56
      src/UnitTests/DistributionTests/Discrete/NegativeBinomialTests.cs
  63. 28
      src/UnitTests/DistributionTests/Discrete/PoissonTests.cs
  64. 55
      src/UnitTests/DistributionTests/Discrete/ZipfTests.cs
  65. 17
      src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs
  66. 27
      src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs
  67. 48
      src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs
  68. 22
      src/UnitTests/DistributionTests/Multivariate/MultinomialTests.cs
  69. 85
      src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs
  70. 27
      src/UnitTests/DistributionTests/Multivariate/WishartTests.cs
  71. 6
      src/UnitTests/InterpolationTests/CubicSplineTest.cs

32
src/Numerics/Distributions/Bernoulli.cs

@ -45,7 +45,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _p;
readonly double _p;
/// <summary>
/// Initializes a new instance of the Bernoulli class.
@ -54,8 +54,13 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If the Bernoulli parameter is not in the range [0,1].</exception>
public Bernoulli(double p)
{
if (!IsValidParameterSet(p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(p);
_p = p;
}
/// <summary>
@ -66,8 +71,13 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If the Bernoulli parameter is not in the range [0,1].</exception>
public Bernoulli(double p, System.Random randomSource)
{
if (!IsValidParameterSet(p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(p);
_p = p;
}
/// <summary>
@ -88,28 +98,12 @@ namespace MathNet.Numerics.Distributions
return p >= 0.0 && p <= 1.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="p">The probability (p) of generating one. Range: 0 ≤ p ≤ 1.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double p)
{
if (!(p >= 0.0 && p <= 1.0))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_p = p;
}
/// <summary>
/// Gets or sets the probability of generating a one. Range: 0 ≤ p ≤ 1.
/// </summary>
public double P
{
get { return _p; }
set { SetParameters(value); }
}
/// <summary>

39
src/Numerics/Distributions/Beta.cs

@ -53,8 +53,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _shapeA;
double _shapeB;
readonly double _shapeA;
readonly double _shapeB;
/// <summary>
/// Initializes a new instance of the Beta class.
@ -63,8 +63,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="b">The β shape parameter of the Beta distribution. Range: β ≥ 0.</param>
public Beta(double a, double b)
{
if (!IsValidParameterSet(a, b))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(a, b);
_shapeA = a;
_shapeB = b;
}
/// <summary>
@ -75,8 +81,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Beta(double a, double b, System.Random randomSource)
{
if (!IsValidParameterSet(a, b))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(a, b);
_shapeA = a;
_shapeB = b;
}
/// <summary>
@ -98,30 +110,12 @@ namespace MathNet.Numerics.Distributions
return a >= 0.0 && b >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="a">The α shape parameter of the Beta distribution. Range: α ≥ 0.</param>
/// <param name="b">The β shape parameter of the Beta distribution. Range: β ≥ 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double a, double b)
{
if (a < 0.0 || b < 0.0 || Double.IsNaN(a) || Double.IsNaN(b))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_shapeA = a;
_shapeB = b;
}
/// <summary>
/// Gets or sets the α shape parameter of the Beta distribution. Range: α ≥ 0.
/// </summary>
public double A
{
get { return _shapeA; }
set { SetParameters(value, _shapeB); }
}
/// <summary>
@ -130,7 +124,6 @@ namespace MathNet.Numerics.Distributions
public double B
{
get { return _shapeB; }
set { SetParameters(_shapeA, value); }
}
/// <summary>

40
src/Numerics/Distributions/Binomial.cs

@ -47,8 +47,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _p;
int _trials;
readonly double _p;
readonly int _trials;
/// <summary>
/// Initializes a new instance of the Binomial class.
@ -59,8 +59,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
public Binomial(double p, int n)
{
if (!IsValidParameterSet(p, n))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(p, n);
_p = p;
_trials = n;
}
/// <summary>
@ -73,8 +79,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
public Binomial(double p, int n, System.Random randomSource)
{
if (!IsValidParameterSet(p, n))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(p, n);
_p = p;
_trials = n;
}
/// <summary>
@ -96,31 +108,12 @@ namespace MathNet.Numerics.Distributions
return p >= 0.0 && p <= 1.0 && n >= 0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="p">The success probability (p) in each trial. Range: 0 ≤ p ≤ 1.</param>
/// <param name="n">The number of trials (n). Range: n ≥ 0.</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="p"/> is not in the interval [0.0,1.0].</exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
void SetParameters(double p, int n)
{
if (!(p >= 0.0 && p <= 1.0 && n >= 0))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_p = p;
_trials = n;
}
/// <summary>
/// Gets or sets the success probability in each trial. Range: 0 ≤ p ≤ 1.
/// </summary>
public double P
{
get { return _p; }
set { SetParameters(value, _trials); }
}
/// <summary>
@ -129,7 +122,6 @@ namespace MathNet.Numerics.Distributions
public int N
{
get { return _trials; }
set { SetParameters(_p, value); }
}
/// <summary>

82
src/Numerics/Distributions/Categorical.cs

@ -55,8 +55,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double[] _pmfNormalized;
double[] _cdfUnnormalized;
readonly double[] _pmfNormalized;
readonly double[] _cdfUnnormalized;
/// <summary>
/// Initializes a new instance of the Categorical class.
@ -65,9 +65,8 @@ namespace MathNet.Numerics.Distributions
/// as this is often impossible using floating point arithmetic.</param>
/// <exception cref="ArgumentException">If any of the probabilities are negative or do not sum to one.</exception>
public Categorical(double[] probabilityMass)
: this(probabilityMass, SystemRandomSource.Default)
{
_random = SystemRandomSource.Default;
SetParameters(probabilityMass);
}
/// <summary>
@ -79,8 +78,28 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If any of the probabilities are negative or do not sum to one.</exception>
public Categorical(double[] probabilityMass, System.Random randomSource)
{
if (Control.CheckDistributionParameters && !IsValidProbabilityMass(probabilityMass))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(probabilityMass);
// Extract unnormalized cumulative distribution
_cdfUnnormalized = new double[probabilityMass.Length];
_cdfUnnormalized[0] = probabilityMass[0];
for (int i = 1; i < probabilityMass.Length; i++)
{
_cdfUnnormalized[i] = _cdfUnnormalized[i - 1] + probabilityMass[i];
}
// Extract normalized probability mass
var sum = _cdfUnnormalized[_cdfUnnormalized.Length - 1];
_pmfNormalized = new double[probabilityMass.Length];
for (int i = 0; i < probabilityMass.Length; i++)
{
_pmfNormalized[i] = probabilityMass[i]/sum;
}
}
/// <summary>
@ -106,7 +125,27 @@ namespace MathNet.Numerics.Distributions
}
_random = SystemRandomSource.Default;
SetParameters(p);
if (Control.CheckDistributionParameters && !IsValidProbabilityMass(p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
// Extract unnormalized cumulative distribution
_cdfUnnormalized = new double[p.Length];
_cdfUnnormalized[0] = p[0];
for (int i1 = 1; i1 < p.Length; i1++)
{
_cdfUnnormalized[i1] = _cdfUnnormalized[i1 - 1] + p[i1];
}
// Extract normalized probability mass
var sum = _cdfUnnormalized[_cdfUnnormalized.Length - 1];
_pmfNormalized = new double[p.Length];
for (int i2 = 0; i2 < p.Length; i2++)
{
_pmfNormalized[i2] = p[i2]/sum;
}
}
/// <summary>
@ -162,36 +201,6 @@ namespace MathNet.Numerics.Distributions
return last > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="p">An array of nonnegative ratios: this array does not need to be normalized
/// as this is often impossible using floating point arithmetic.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double[] p)
{
if (Control.CheckDistributionParameters && !IsValidProbabilityMass(p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
// Extract unnormalized cumulative distribution
_cdfUnnormalized = new double[p.Length];
_cdfUnnormalized[0] = p[0];
for (int i = 1; i < p.Length; i++)
{
_cdfUnnormalized[i] = _cdfUnnormalized[i - 1] + p[i];
}
// Extract normalized probability mass
var sum = _cdfUnnormalized[_cdfUnnormalized.Length - 1];
_pmfNormalized = new double[p.Length];
for (int i = 0; i < p.Length; i++)
{
_pmfNormalized[i] = p[i]/sum;
}
}
/// <summary>
/// Gets or sets the probability mass vector (non-negative ratios) of the multinomial.
/// </summary>
@ -199,7 +208,6 @@ namespace MathNet.Numerics.Distributions
public double[] P
{
get { return (double[])_pmfNormalized.Clone(); }
set { SetParameters(value); }
}
/// <summary>

39
src/Numerics/Distributions/Cauchy.cs

@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _location;
double _scale;
readonly double _location;
readonly double _scale;
/// <summary>
/// Initializes a new instance of the <see cref="Cauchy"/> class with the location parameter set to 0 and the scale parameter set to 1
@ -62,8 +62,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale (γ) of the distribution. Range: γ > 0.</param>
public Cauchy(double location, double scale)
{
if (!IsValidParameterSet(location, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(location, scale);
_location = location;
_scale = scale;
}
/// <summary>
@ -74,8 +80,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Cauchy(double location, double scale, System.Random randomSource)
{
if (!IsValidParameterSet(location, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(location, scale);
_location = location;
_scale = scale;
}
/// <summary>
@ -97,30 +109,12 @@ namespace MathNet.Numerics.Distributions
return scale > 0.0 && !Double.IsNaN(location);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="location">The location (x0) of the distribution.</param>
/// <param name="scale">The scale (γ) of the distribution. Range: γ > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double location, double scale)
{
if (scale <= 0.0 || Double.IsNaN(location) || Double.IsNaN(scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_location = location;
_scale = scale;
}
/// <summary>
/// Gets or sets the location (x0) of the distribution.
/// </summary>
public double Location
{
get { return _location; }
set { SetParameters(value, _scale); }
}
/// <summary>
@ -129,7 +123,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_location, value); }
}
/// <summary>

32
src/Numerics/Distributions/Chi.cs

@ -46,7 +46,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _freedom;
readonly double _freedom;
/// <summary>
/// Initializes a new instance of the <see cref="Chi"/> class.
@ -54,8 +54,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="freedom">The degrees of freedom (k) of the distribution. Range: k > 0.</param>
public Chi(double freedom)
{
if (!IsValidParameterSet(freedom))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(freedom);
_freedom = freedom;
}
/// <summary>
@ -65,8 +70,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Chi(double freedom, System.Random randomSource)
{
if (!IsValidParameterSet(freedom))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(freedom);
_freedom = freedom;
}
/// <summary>
@ -87,28 +97,12 @@ namespace MathNet.Numerics.Distributions
return freedom > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="freedom">The degrees of freedom (k) of the distribution. Range: k > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double freedom)
{
if (freedom <= 0.0 || Double.IsNaN(freedom))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_freedom = freedom;
}
/// <summary>
/// Gets or sets the degrees of freedom (k) of the Chi distribution. Range: k > 0.
/// </summary>
public double DegreesOfFreedom
{
get { return _freedom; }
set { SetParameters(value); }
}
/// <summary>

32
src/Numerics/Distributions/ChiSquared.cs

@ -44,7 +44,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _freedom;
readonly double _freedom;
/// <summary>
/// Initializes a new instance of the <see cref="ChiSquared"/> class.
@ -52,8 +52,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="freedom">The degrees of freedom (k) of the distribution. Range: k > 0.</param>
public ChiSquared(double freedom)
{
if (!IsValidParameterSet(freedom))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(freedom);
_freedom = freedom;
}
/// <summary>
@ -63,8 +68,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public ChiSquared(double freedom, System.Random randomSource)
{
if (!IsValidParameterSet(freedom))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(freedom);
_freedom = freedom;
}
/// <summary>
@ -85,28 +95,12 @@ namespace MathNet.Numerics.Distributions
return freedom > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="freedom">The degrees of freedom (k) of the distribution. Range: k > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double freedom)
{
if (freedom <= 0.0 || Double.IsNaN(freedom))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_freedom = freedom;
}
/// <summary>
/// Gets or sets the degrees of freedom (k) of the Chi-Squared distribution. Range: k > 0.
/// </summary>
public double DegreesOfFreedom
{
get { return _freedom; }
set { SetParameters(value); }
}
/// <summary>

41
src/Numerics/Distributions/ContinuousUniform.cs

@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _lower;
double _upper;
readonly double _lower;
readonly double _upper;
/// <summary>
/// Initializes a new instance of the ContinuousUniform class with lower bound 0 and upper bound 1.
@ -63,8 +63,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If the upper bound is smaller than the lower bound.</exception>
public ContinuousUniform(double lower, double upper)
{
if (!IsValidParameterSet(lower, upper))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(lower, upper);
_lower = lower;
_upper = upper;
}
/// <summary>
@ -76,8 +82,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If the upper bound is smaller than the lower bound.</exception>
public ContinuousUniform(double lower, double upper, System.Random randomSource)
{
if (!IsValidParameterSet(lower, upper))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(lower, upper);
_lower = lower;
_upper = upper;
}
/// <summary>
@ -96,24 +108,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="upper">Upper bound. Range: lower ≤ upper.</param>
public static bool IsValidParameterSet(double lower, double upper)
{
return lower <= upper && !Double.IsInfinity(lower) && !Double.IsInfinity(upper);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="lower">Lower bound. Range: lower ≤ upper.</param>
/// <param name="upper">Upper bound. Range: lower ≤ upper.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double lower, double upper)
{
if (upper < lower || Double.IsNaN(upper) || Double.IsNaN(lower))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_lower = lower;
_upper = upper;
return lower <= upper;
}
/// <summary>
@ -122,7 +117,6 @@ namespace MathNet.Numerics.Distributions
public double LowerBound
{
get { return _lower; }
set { SetParameters(value, _upper); }
}
/// <summary>
@ -131,7 +125,6 @@ namespace MathNet.Numerics.Distributions
public double UpperBound
{
get { return _upper; }
set { SetParameters(_lower, value); }
}
/// <summary>

39
src/Numerics/Distributions/ConwayMaxwellPoisson.cs

@ -51,8 +51,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _lambda;
double _nu;
readonly double _lambda;
readonly double _nu;
/// <summary>
/// The mean of the distribution.
@ -82,8 +82,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The rate of decay (ν) parameter. Range: ν ≥ 0.</param>
public ConwayMaxwellPoisson(double lambda, double nu)
{
if (!IsValidParameterSet(lambda, nu))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(lambda, nu);
_lambda = lambda;
_nu = nu;
}
/// <summary>
@ -94,8 +100,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public ConwayMaxwellPoisson(double lambda, double nu, System.Random randomSource)
{
if (!IsValidParameterSet(lambda, nu))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(lambda, nu);
_lambda = lambda;
_nu = nu;
}
/// <summary>
@ -117,30 +129,12 @@ namespace MathNet.Numerics.Distributions
return lambda > 0.0 && nu >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="lambda">The lambda (λ) parameter. Range: λ > 0.</param>
/// <param name="nu">The rate of decay (ν) parameter. Range: ν ≥ 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double lambda, double nu)
{
if (!(lambda > 0.0 && nu >= 0.0))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_lambda = lambda;
_nu = nu;
}
/// <summary>
/// Gets or sets the lambda (λ) parameter. Range: λ > 0.
/// </summary>
public double Lambda
{
get { return _lambda; }
set { SetParameters(value, _nu); }
}
/// <summary>
@ -149,7 +143,6 @@ namespace MathNet.Numerics.Distributions
public double Nu
{
get { return _nu; }
set { SetParameters(_lambda, value); }
}
/// <summary>

46
src/Numerics/Distributions/Dirichlet.cs

@ -43,7 +43,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double[] _alpha;
readonly double[] _alpha;
/// <summary>
/// Initializes a new instance of the Dirichlet class. The distribution will
@ -52,8 +52,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="alpha">An array with the Dirichlet parameters.</param>
public Dirichlet(double[] alpha)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(alpha);
_alpha = (double[]) alpha.Clone();
}
/// <summary>
@ -64,8 +69,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Dirichlet(double[] alpha, System.Random randomSource)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(alpha);
_alpha = (double[]) alpha.Clone();
}
/// <summary>
@ -83,7 +93,12 @@ namespace MathNet.Numerics.Distributions
}
_random = SystemRandomSource.Default;
SetParameters(parm);
if (Control.CheckDistributionParameters && !IsValidParameterSet(parm))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_alpha = (double[]) parm.Clone();
}
/// <summary>
@ -102,7 +117,12 @@ namespace MathNet.Numerics.Distributions
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(parm);
if (Control.CheckDistributionParameters && !IsValidParameterSet(parm))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_alpha = (double[]) parm.Clone();
}
/// <summary>
@ -135,28 +155,12 @@ namespace MathNet.Numerics.Distributions
return !allzero;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="alpha">The parameters of the Dirichlet distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double[] alpha)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_alpha = (double[]) alpha.Clone();
}
/// <summary>
/// Gets or sets the parameters of the Dirichlet distribution.
/// </summary>
public double[] Alpha
{
get { return _alpha; }
set { SetParameters(value); }
}
/// <summary>

39
src/Numerics/Distributions/DiscreteUniform.cs

@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
int _lower;
int _upper;
readonly int _lower;
readonly int _upper;
/// <summary>
/// Initializes a new instance of the DiscreteUniform class.
@ -55,8 +55,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="upper">Upper bound. Range: lower ≤ upper.</param>
public DiscreteUniform(int lower, int upper)
{
if (!IsValidParameterSet(lower, upper))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(lower, upper);
_lower = lower;
_upper = upper;
}
/// <summary>
@ -67,8 +73,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public DiscreteUniform(int lower, int upper, System.Random randomSource)
{
if (!IsValidParameterSet(lower, upper))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(lower, upper);
_lower = lower;
_upper = upper;
}
/// <summary>
@ -92,30 +104,12 @@ namespace MathNet.Numerics.Distributions
return lower <= upper;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="lower">Lower bound. Range: lower ≤ upper.</param>
/// <param name="upper">Upper bound. Range: lower ≤ upper.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(int lower, int upper)
{
if (!(lower <= upper))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_lower = lower;
_upper = upper;
}
/// <summary>
/// Gets or sets the lower bound of the probability distribution.
/// </summary>
public int LowerBound
{
get { return _lower; }
set { SetParameters(value, _upper); }
}
/// <summary>
@ -124,7 +118,6 @@ namespace MathNet.Numerics.Distributions
public int UpperBound
{
get { return _upper; }
set { SetParameters(_lower, value); }
}
/// <summary>

48
src/Numerics/Distributions/Erlang.cs

@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _shape;
double _rate;
readonly double _shape;
readonly double _rate;
/// <summary>
/// Initializes a new instance of the <see cref="Erlang"/> class.
@ -55,8 +55,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0.</param>
public Erlang(int shape, double rate)
{
if (!IsValidParameterSet(shape, rate))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(shape, rate);
_shape = shape;
_rate = rate;
}
/// <summary>
@ -67,8 +73,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Erlang(int shape, double rate, System.Random randomSource)
{
if (!IsValidParameterSet(shape, rate))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(shape, rate);
_shape = shape;
_rate = rate;
}
/// <summary>
@ -114,30 +126,12 @@ namespace MathNet.Numerics.Distributions
return shape >= 0.0 && rate >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="shape">The shape (k) of the Erlang distribution. Range: k ≥ 0.</param>
/// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double shape, double rate)
{
if (shape < 0.0 || rate < 0.0 || Double.IsNaN(shape) || Double.IsNaN(rate))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_shape = shape;
_rate = rate;
}
/// <summary>
/// Gets or sets the shape (k) of the Erlang distribution. Range: k ≥ 0.
/// </summary>
public int Shape
{
get { return (int)_shape; }
set { SetParameters(value, _rate); }
}
/// <summary>
@ -146,7 +140,6 @@ namespace MathNet.Numerics.Distributions
public double Rate
{
get { return _rate; }
set { SetParameters(_shape, value); }
}
/// <summary>
@ -155,15 +148,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return 1.0 / _rate; }
set
{
var invScale = 1.0 / value;
if (Double.IsNegativeInfinity(invScale))
{
invScale = -invScale;
}
SetParameters(_shape, invScale);
}
}
/// <summary>

32
src/Numerics/Distributions/Exponential.cs

@ -46,7 +46,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _rate;
readonly double _rate;
/// <summary>
/// Initializes a new instance of the <see cref="Exponential"/> class.
@ -54,8 +54,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="rate">The rate (λ) parameter of the distribution. Range: λ ≥ 0.</param>
public Exponential(double rate)
{
if (!IsValidParameterSet(rate))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(rate);
_rate = rate;
}
/// <summary>
@ -65,8 +70,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Exponential(double rate, System.Random randomSource)
{
if (!IsValidParameterSet(rate))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(rate);
_rate = rate;
}
/// <summary>
@ -87,28 +97,12 @@ namespace MathNet.Numerics.Distributions
return rate >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="rate">The rate (λ) parameter of the distribution. Range: λ ≥ 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double rate)
{
if (rate < 0.0 || Double.IsNaN(rate))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_rate = rate;
}
/// <summary>
/// Gets or sets the rate (λ) parameter of the distribution. Range: λ ≥ 0.
/// </summary>
public double Rate
{
get { return _rate; }
set { SetParameters(value); }
}
/// <summary>

39
src/Numerics/Distributions/FisherSnedecor.cs

@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _freedom1;
double _freedom2;
readonly double _freedom1;
readonly double _freedom2;
/// <summary>
/// Initializes a new instance of the <see cref="FisherSnedecor"/> class.
@ -55,8 +55,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="d2">The second degree of freedom (d2) of the distribution. Range: d2 > 0.</param>
public FisherSnedecor(double d1, double d2)
{
if (!IsValidParameterSet(d1, d2))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(d1, d2);
_freedom1 = d1;
_freedom2 = d2;
}
/// <summary>
@ -67,8 +73,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public FisherSnedecor(double d1, double d2, System.Random randomSource)
{
if (!IsValidParameterSet(d1, d2))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(d1, d2);
_freedom1 = d1;
_freedom2 = d2;
}
/// <summary>
@ -90,30 +102,12 @@ namespace MathNet.Numerics.Distributions
return d1 > 0.0 && d2 > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="d1">The first degree of freedom (d1) of the distribution. Range: d1 > 0.</param>
/// <param name="d2">The second degree of freedom (d2) of the distribution. Range: d2 > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double d1, double d2)
{
if (d1 <= 0.0 || d2 <= 0.0 || Double.IsNaN(d1) || Double.IsNaN(d2))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_freedom1 = d1;
_freedom2 = d2;
}
/// <summary>
/// Gets or sets the first degree of freedom (d1) of the distribution. Range: d1 > 0.
/// </summary>
public double DegreesOfFreedom1
{
get { return _freedom1; }
set { SetParameters(value, _freedom2); }
}
/// <summary>
@ -122,7 +116,6 @@ namespace MathNet.Numerics.Distributions
public double DegreesOfFreedom2
{
get { return _freedom2; }
set { SetParameters(_freedom1, value); }
}
/// <summary>

48
src/Numerics/Distributions/Gamma.cs

@ -54,8 +54,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _shape;
double _rate;
readonly double _shape;
readonly double _rate;
/// <summary>
/// Initializes a new instance of the Gamma class.
@ -64,8 +64,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution. Range: β ≥ 0.</param>
public Gamma(double shape, double rate)
{
if (!IsValidParameterSet(shape, rate))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(shape, rate);
_shape = shape;
_rate = rate;
}
/// <summary>
@ -76,8 +82,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Gamma(double shape, double rate, System.Random randomSource)
{
if (!IsValidParameterSet(shape, rate))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(shape, rate);
_shape = shape;
_rate = rate;
}
/// <summary>
@ -123,30 +135,12 @@ namespace MathNet.Numerics.Distributions
return shape >= 0.0 && rate >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="shape">The shape (k, α) of the Gamma distribution. Range: α ≥ 0.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution. Range: β ≥ 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double shape, double rate)
{
if (shape < 0.0 || rate < 0.0 || Double.IsNaN(shape) || Double.IsNaN(rate))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_shape = shape;
_rate = rate;
}
/// <summary>
/// Gets or sets the shape (k, α) of the Gamma distribution. Range: α ≥ 0.
/// </summary>
public double Shape
{
get { return _shape; }
set { SetParameters(value, _rate); }
}
/// <summary>
@ -155,7 +149,6 @@ namespace MathNet.Numerics.Distributions
public double Rate
{
get { return _rate; }
set { SetParameters(_shape, value); }
}
/// <summary>
@ -164,15 +157,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return 1.0 / _rate; }
set
{
var rate = 1.0 / value;
if (Double.IsNegativeInfinity(rate))
{
rate = -rate;
}
SetParameters(_shape, rate);
}
}
/// <summary>

32
src/Numerics/Distributions/Geometric.cs

@ -45,7 +45,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _p;
readonly double _p;
/// <summary>
/// Initializes a new instance of the Geometric class.
@ -53,8 +53,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="p">The probability (p) of generating one. Range: 0 ≤ p ≤ 1.</param>
public Geometric(double p)
{
if (!IsValidParameterSet(p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(p);
_p = p;
}
/// <summary>
@ -64,8 +69,13 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Geometric(double p, System.Random randomSource)
{
if (!IsValidParameterSet(p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(p);
_p = p;
}
/// <summary>
@ -86,28 +96,12 @@ namespace MathNet.Numerics.Distributions
return p >= 0.0 && p <= 1.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="p">The probability (p) of generating one. Range: 0 ≤ p ≤ 1.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double p)
{
if (!(p >= 0.0 && p <= 1.0))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_p = p;
}
/// <summary>
/// Gets or sets the probability of generating a one. Range: 0 ≤ p ≤ 1.
/// </summary>
public double P
{
get { return _p; }
set { SetParameters(value); }
}
/// <summary>

76
src/Numerics/Distributions/Hypergeometric.cs

@ -46,9 +46,9 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
int _population;
int _success;
int _draws;
readonly int _population;
readonly int _success;
readonly int _draws;
/// <summary>
/// Initializes a new instance of the Hypergeometric class.
@ -58,8 +58,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="draws">The number of draws without replacement (n).</param>
public Hypergeometric(int population, int success, int draws)
{
if (!IsValidParameterSet(population, success, draws))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(population, success, draws);
_population = population;
_success = success;
_draws = draws;
}
/// <summary>
@ -71,8 +78,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Hypergeometric(int population, int success, int draws, System.Random randomSource)
{
if (!IsValidParameterSet(population, success, draws))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(population, success, draws);
_population = population;
_success = success;
_draws = draws;
}
/// <summary>
@ -97,25 +111,6 @@ namespace MathNet.Numerics.Distributions
return population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="population">The size of the population (N).</param>
/// <param name="success">The number successes within the population (K, M).</param>
/// <param name="draws">The number of draws without replacement (n).</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(int population, int success, int draws)
{
if (!(population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_population = population;
_success = success;
_draws = draws;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
@ -131,7 +126,6 @@ namespace MathNet.Numerics.Distributions
public int Population
{
get { return _population; }
set { SetParameters(value, _success, _draws); }
}
/// <summary>
@ -140,7 +134,6 @@ namespace MathNet.Numerics.Distributions
public int Draws
{
get { return _draws; }
set { SetParameters(_population, value, _draws); }
}
/// <summary>
@ -149,37 +142,6 @@ namespace MathNet.Numerics.Distributions
public int Success
{
get { return _success; }
set { SetParameters(_population, _success, value); }
}
/// <summary>
/// Gets or sets the size of the population (N).
/// </summary>
[Obsolete("Use Population instead. Scheduled for removal in v3.0.")]
public int PopulationSize
{
get { return _population; }
set { SetParameters(value, _success, _draws); }
}
/// <summary>
/// Gets or sets the number of draws without replacement (n).
/// </summary>
[Obsolete("Use Draws instead. Scheduled for removal in v3.0.")]
public int N
{
get { return _draws; }
set { SetParameters(_population, value, _draws); }
}
/// <summary>
/// Gets or sets the number successes within the population (K, M).
/// </summary>
[Obsolete("Use Success instead. Scheduled for removal in v3.0.")]
public int M
{
get { return _success; }
set { SetParameters(_population, _success, value); }
}
/// <summary>

39
src/Numerics/Distributions/InverseGamma.cs

@ -46,8 +46,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _shape;
double _scale;
readonly double _shape;
readonly double _scale;
/// <summary>
/// Initializes a new instance of the <see cref="InverseGamma"/> class.
@ -56,8 +56,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale (β) of the distribution. Range: β > 0.</param>
public InverseGamma(double shape, double scale)
{
if (!IsValidParameterSet(shape, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(shape, scale);
_shape = shape;
_scale = scale;
}
/// <summary>
@ -68,8 +74,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public InverseGamma(double shape, double scale, System.Random randomSource)
{
if (!IsValidParameterSet(shape, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(shape, scale);
_shape = shape;
_scale = scale;
}
/// <summary>
@ -91,30 +103,12 @@ namespace MathNet.Numerics.Distributions
return shape > 0.0 && scale > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="shape">The shape (α) of the distribution. Range: α > 0.</param>
/// <param name="scale">The scale (β) of the distribution. Range: β > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double shape, double scale)
{
if (shape <= 0.0 || scale <= 0.0 || Double.IsNaN(shape) || Double.IsNaN(scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_shape = shape;
_scale = scale;
}
/// <summary>
/// Gets or sets the shape (α) parameter. Range: α > 0.
/// </summary>
public double Shape
{
get { return _shape; }
set { SetParameters(value, _scale); }
}
/// <summary>
@ -123,7 +117,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_shape, value); }
}
/// <summary>

44
src/Numerics/Distributions/InverseWishart.cs

@ -46,13 +46,13 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _freedom;
Matrix<double> _scale;
readonly double _freedom;
readonly Matrix<double> _scale;
/// <summary>
/// Caches the Cholesky factorization of the scale matrix.
/// </summary>
Cholesky<double> _chol;
readonly Cholesky<double> _chol;
/// <summary>
/// Initializes a new instance of the <see cref="InverseWishart"/> class.
@ -61,8 +61,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
public InverseWishart(double degreesOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(degreesOfFreedom, scale);
_freedom = degreesOfFreedom;
_scale = scale;
_chol = _scale.Cholesky();
}
/// <summary>
@ -73,8 +80,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public InverseWishart(double degreesOfFreedom, Matrix<double> scale, System.Random randomSource)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(degreesOfFreedom, scale);
_freedom = degreesOfFreedom;
_scale = scale;
_chol = _scale.Cholesky();
}
/// <summary>
@ -109,31 +123,12 @@ namespace MathNet.Numerics.Distributions
return degreesOfFreedom > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="degreesOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double degreesOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_freedom = degreesOfFreedom;
_scale = scale;
_chol = _scale.Cholesky();
}
/// <summary>
/// Gets or sets the degree of freedom (ν) for the inverse Wishart distribution.
/// </summary>
public double DegreesOfFreedom
{
get { return _freedom; }
set { SetParameters(value, _scale); }
}
/// <summary>
@ -142,7 +137,6 @@ namespace MathNet.Numerics.Distributions
public Matrix<double> Scale
{
get { return _scale; }
set { SetParameters(_freedom, value); }
}
/// <summary>

39
src/Numerics/Distributions/Laplace.cs

@ -46,8 +46,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _location;
double _scale;
readonly double _location;
readonly double _scale;
/// <summary>
/// Initializes a new instance of the <see cref="Laplace"/> class (location = 0, scale = 1).
@ -65,8 +65,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Laplace(double location, double scale)
{
if (!IsValidParameterSet(location, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(location, scale);
_location = location;
_scale = scale;
}
/// <summary>
@ -78,8 +84,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Laplace(double location, double scale, System.Random randomSource)
{
if (!IsValidParameterSet(location, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(location, scale);
_location = location;
_scale = scale;
}
/// <summary>
@ -101,30 +113,12 @@ namespace MathNet.Numerics.Distributions
return scale > 0.0 && !Double.IsNaN(location);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (b) of the distribution. Range: b > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double location, double scale)
{
if (scale <= 0.0 || Double.IsNaN(location) || Double.IsNaN(scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_location = location;
_scale = scale;
}
/// <summary>
/// Gets or sets the location (μ) of the Laplace distribution.
/// </summary>
public double Location
{
get { return _location; }
set { SetParameters(value, _scale); }
}
/// <summary>
@ -133,7 +127,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_location, value); }
}
/// <summary>

39
src/Numerics/Distributions/LogNormal.cs

@ -47,8 +47,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _mu;
double _sigma;
readonly double _mu;
readonly double _sigma;
/// <summary>
/// Initializes a new instance of the <see cref="LogNormal"/> class.
@ -59,8 +59,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="sigma">The shape (σ) of the logarithm of the distribution. Range: σ ≥ 0.</param>
public LogNormal(double mu, double sigma)
{
if (!IsValidParameterSet(mu, sigma))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(mu, sigma);
_mu = mu;
_sigma = sigma;
}
/// <summary>
@ -73,8 +79,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public LogNormal(double mu, double sigma, System.Random randomSource)
{
if (!IsValidParameterSet(mu, sigma))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(mu, sigma);
_mu = mu;
_sigma = sigma;
}
/// <summary>
@ -134,30 +146,12 @@ namespace MathNet.Numerics.Distributions
return sigma >= 0.0 && !Double.IsNaN(mu);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="mu">The log-scale (μ) of the distribution.</param>
/// <param name="sigma">The shape (σ) of the distribution. Range: σ ≥ 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double mu, double sigma)
{
if (sigma < 0.0 || Double.IsNaN(mu) || Double.IsNaN(sigma))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_mu = mu;
_sigma = sigma;
}
/// <summary>
/// Gets or sets the log-scale (μ) (mean of the logarithm) of the distribution.
/// </summary>
public double Mu
{
get { return _mu; }
set { SetParameters(value, _sigma); }
}
/// <summary>
@ -166,7 +160,6 @@ namespace MathNet.Numerics.Distributions
public double Sigma
{
get { return _sigma; }
set { SetParameters(_mu, value); }
}
/// <summary>

46
src/Numerics/Distributions/MatrixNormal.cs

@ -49,17 +49,17 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The mean of the matrix normal distribution.
/// </summary>
Matrix<double> _m;
readonly Matrix<double> _m;
/// <summary>
/// The covariance matrix for the rows.
/// </summary>
Matrix<double> _v;
readonly Matrix<double> _v;
/// <summary>
/// The covariance matrix for the columns.
/// </summary>
Matrix<double> _k;
readonly Matrix<double> _k;
/// <summary>
/// Initializes a new instance of the <see cref="MatrixNormal"/> class.
@ -70,8 +70,15 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If the dimensions of the mean and two covariance matrices don't match.</exception>
public MatrixNormal(Matrix<double> m, Matrix<double> v, Matrix<double> k)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(m, v, k);
_m = m;
_v = v;
_k = k;
}
/// <summary>
@ -84,8 +91,15 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If the dimensions of the mean and two covariance matrices don't match.</exception>
public MatrixNormal(Matrix<double> m, Matrix<double> v, Matrix<double> k, System.Random randomSource)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(m, v, k);
_m = m;
_v = v;
_k = k;
}
/// <summary>
@ -138,25 +152,6 @@ namespace MathNet.Numerics.Distributions
return true;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="m">The mean of the matrix normal.</param>
/// <param name="v">The covariance matrix for the rows.</param>
/// <param name="k">The covariance matrix for the columns.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(Matrix<double> m, Matrix<double> v, Matrix<double> k)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_m = m;
_v = v;
_k = k;
}
/// <summary>
/// Gets or sets the mean. (M)
/// </summary>
@ -164,7 +159,6 @@ namespace MathNet.Numerics.Distributions
public Matrix<double> Mean
{
get { return _m; }
set { SetParameters(value, _v, _k); }
}
/// <summary>
@ -174,7 +168,6 @@ namespace MathNet.Numerics.Distributions
public Matrix<double> RowCovariance
{
get { return _v; }
set { SetParameters(_m, value, _k); }
}
/// <summary>
@ -184,7 +177,6 @@ namespace MathNet.Numerics.Distributions
public Matrix<double> ColumnCovariance
{
get { return _k; }
set { SetParameters(_m, _v, value); }
}
/// <summary>

48
src/Numerics/Distributions/Multinomial.cs

@ -55,12 +55,12 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Stores the normalized multinomial probabilities.
/// </summary>
double[] _p;
readonly double[] _p;
/// <summary>
/// The number of trials.
/// </summary>
int _trials;
readonly int _trials;
/// <summary>
/// Initializes a new instance of the Multinomial class.
@ -72,8 +72,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
public Multinomial(double[] p, int n)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(p, n);
_p = (double[]) p.Clone();
_trials = n;
}
/// <summary>
@ -87,8 +93,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
public Multinomial(double[] p, int n, System.Random randomSource)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(p, n);
_p = (double[]) p.Clone();
_trials = n;
}
/// <summary>
@ -115,7 +127,13 @@ namespace MathNet.Numerics.Distributions
p[i] = h[i].Count;
}
SetParameters(p, n);
if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_p = (double[]) p.Clone();
_trials = n;
RandomSource = SystemRandomSource.Default;
}
@ -157,31 +175,12 @@ namespace MathNet.Numerics.Distributions
return n >= 0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="p">An array of nonnegative ratios: this array does not need to be normalized
/// as this is often impossible using floating point arithmetic.</param>
/// <param name="n">The number of trials.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double[] p, int n)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_p = (double[]) p.Clone();
_trials = n;
}
/// <summary>
/// Gets or sets the proportion of ratios.
/// </summary>
public double[] P
{
get { return (double[])_p.Clone(); }
set { SetParameters(value, _trials); }
}
/// <summary>
@ -190,7 +189,6 @@ namespace MathNet.Numerics.Distributions
public int N
{
get { return _trials; }
set { SetParameters(_p, value); }
}
/// <summary>

39
src/Numerics/Distributions/NegativeBinomial.cs

@ -46,8 +46,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _trials;
double _p;
readonly double _trials;
readonly double _p;
/// <summary>
/// Initializes a new instance of the <see cref="NegativeBinomial"/> class.
@ -56,8 +56,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="p">The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1.</param>
public NegativeBinomial(double r, double p)
{
if (!IsValidParameterSet(r,p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(r, p);
_p = p;
_trials = r;
}
/// <summary>
@ -68,8 +74,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public NegativeBinomial(double r, double p, System.Random randomSource)
{
if (!IsValidParameterSet(r,p))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(r, p);
_p = p;
_trials = r;
}
/// <summary>
@ -93,30 +105,12 @@ namespace MathNet.Numerics.Distributions
return r >= 0.0 && p >= 0.0 && p <= 1.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="r">The number of failures (r) until the experiment stopped. Range: r ≥ 0.</param>
/// <param name="p">The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double r, double p)
{
if (!(r >= 0.0 && p >= 0.0 && p <= 1.0))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_p = p;
_trials = r;
}
/// <summary>
/// Gets or sets the number of trials. Range: r ≥ 0.
/// </summary>
public double R
{
get { return _trials; }
set { SetParameters(value, _p); }
}
/// <summary>
@ -125,7 +119,6 @@ namespace MathNet.Numerics.Distributions
public double P
{
get { return _p; }
set { SetParameters(_trials, value); }
}
/// <summary>

50
src/Numerics/Distributions/Normal.cs

@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _mean;
double _stdDev;
readonly double _mean;
readonly double _stdDev;
/// <summary>
/// Initializes a new instance of the Normal class. This is a normal distribution with mean 0.0
@ -77,8 +77,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="stddev">The standard deviation (σ) of the normal distribution. Range: σ ≥ 0.</param>
public Normal(double mean, double stddev)
{
if (!IsValidParameterSet(mean, stddev))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(mean, stddev);
_mean = mean;
_stdDev = stddev;
}
/// <summary>
@ -90,8 +96,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Normal(double mean, double stddev, System.Random randomSource)
{
if (!IsValidParameterSet(mean, stddev))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(mean, stddev);
_mean = mean;
_stdDev = stddev;
}
/// <summary>
@ -162,30 +174,12 @@ namespace MathNet.Numerics.Distributions
return stddev >= 0.0 && !Double.IsNaN(mean);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution. Range: σ ≥ 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double mean, double stddev)
{
if (stddev < 0.0 || Double.IsNaN(mean) || Double.IsNaN(stddev))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_mean = mean;
_stdDev = stddev;
}
/// <summary>
/// Gets or sets the mean (μ) of the normal distribution.
/// </summary>
public double Mean
{
get { return _mean; }
set { SetParameters(value, _stdDev); }
}
/// <summary>
@ -194,7 +188,6 @@ namespace MathNet.Numerics.Distributions
public double StdDev
{
get { return _stdDev; }
set { SetParameters(_mean, value); }
}
/// <summary>
@ -203,7 +196,6 @@ namespace MathNet.Numerics.Distributions
public double Variance
{
get { return _stdDev*_stdDev; }
set { SetParameters(_mean, Math.Sqrt(value)); }
}
/// <summary>
@ -212,16 +204,6 @@ namespace MathNet.Numerics.Distributions
public double Precision
{
get { return 1.0/(_stdDev*_stdDev); }
set
{
var sdev = 1.0/Math.Sqrt(value);
// Handle the case when the precision is -0.
if (Double.IsInfinity(sdev))
{
sdev = Double.PositiveInfinity;
}
SetParameters(_mean, sdev);
}
}
/// <summary>

55
src/Numerics/Distributions/NormalGamma.cs

@ -68,7 +68,6 @@ namespace MathNet.Numerics.Distributions
public double Mean
{
get { return _mean; }
set { _mean = value; }
}
@ -78,7 +77,6 @@ namespace MathNet.Numerics.Distributions
public double Precision
{
get { return _precision; }
set { _precision = value; }
}
}
@ -100,10 +98,10 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _meanLocation;
double _meanScale;
double _precisionShape;
double _precisionInvScale;
readonly double _meanLocation;
readonly double _meanScale;
readonly double _precisionShape;
readonly double _precisionInvScale;
/// <summary>
/// Initializes a new instance of the <see cref="NormalGamma"/> class.
@ -114,8 +112,16 @@ namespace MathNet.Numerics.Distributions
/// <param name="precisionInverseScale">The inverse scale of the precision.</param>
public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInverseScale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale);
_meanLocation = meanLocation;
_meanScale = meanScale;
_precisionShape = precisionShape;
_precisionInvScale = precisionInverseScale;
}
/// <summary>
@ -128,8 +134,16 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale, System.Random randomSource)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInverseScale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale);
_meanLocation = meanLocation;
_meanScale = meanScale;
_precisionShape = precisionShape;
_precisionInvScale = precisionInverseScale;
}
/// <summary>
@ -154,34 +168,12 @@ namespace MathNet.Numerics.Distributions
return meanScale > 0.0 && precShape > 0.0 && precInvScale > 0.0 && !Double.IsNaN(meanLocation);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="meanLocation">The location of the mean.</param>
/// <param name="meanScale">The scale of the mean.</param>
/// <param name="precShape">The shape of the precision.</param>
/// <param name="precInvScale">The inverse scale of the precision.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double meanLocation, double meanScale, double precShape, double precInvScale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precShape, precInvScale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_meanLocation = meanLocation;
_meanScale = meanScale;
_precisionShape = precShape;
_precisionInvScale = precInvScale;
}
/// <summary>
/// Gets or sets the location of the mean.
/// </summary>
public double MeanLocation
{
get { return _meanLocation; }
set { SetParameters(value, _meanScale, _precisionShape, _precisionInvScale); }
}
/// <summary>
@ -190,7 +182,6 @@ namespace MathNet.Numerics.Distributions
public double MeanScale
{
get { return _meanScale; }
set { SetParameters(_meanLocation, value, _precisionShape, _precisionInvScale); }
}
/// <summary>
@ -199,7 +190,6 @@ namespace MathNet.Numerics.Distributions
public double PrecisionShape
{
get { return _precisionShape; }
set { SetParameters(_meanLocation, _meanScale, value, _precisionInvScale); }
}
/// <summary>
@ -208,7 +198,6 @@ namespace MathNet.Numerics.Distributions
public double PrecisionInverseScale
{
get { return _precisionInvScale; }
set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); }
}
/// <summary>

39
src/Numerics/Distributions/Pareto.cs

@ -48,8 +48,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _scale;
double _shape;
readonly double _scale;
readonly double _shape;
/// <summary>
/// Initializes a new instance of the <see cref="Pareto"/> class.
@ -59,8 +59,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> or <paramref name="shape"/> are negative.</exception>
public Pareto(double scale, double shape)
{
if (!IsValidParameterSet(scale, shape))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(scale, shape);
_scale = scale;
_shape = shape;
}
/// <summary>
@ -72,8 +78,14 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> or <paramref name="shape"/> are negative.</exception>
public Pareto(double scale, double shape, System.Random randomSource)
{
if (!IsValidParameterSet(scale, shape))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(scale, shape);
_scale = scale;
_shape = shape;
}
/// <summary>
@ -95,30 +107,12 @@ namespace MathNet.Numerics.Distributions
return scale > 0.0 && shape > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="scale">The scale (xm) of the distribution. Range: xm > 0.</param>
/// <param name="shape">The shape (α) of the distribution. Range: α > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double scale, double shape)
{
if (scale <= 0.0 || shape <= 0.0 || Double.IsNaN(scale) || Double.IsNaN(shape))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_scale = scale;
_shape = shape;
}
/// <summary>
/// Gets or sets the scale (xm) of the distribution. Range: xm > 0.
/// </summary>
public double Scale
{
get { return _scale; }
set { SetParameters(value, _shape); }
}
/// <summary>
@ -127,7 +121,6 @@ namespace MathNet.Numerics.Distributions
public double Shape
{
get { return _shape; }
set { SetParameters(_scale, value); }
}
/// <summary>

32
src/Numerics/Distributions/Poisson.cs

@ -47,7 +47,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _lambda;
readonly double _lambda;
/// <summary>
/// Initializes a new instance of the <see cref="Poisson"/> class.
@ -56,8 +56,13 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="System.ArgumentOutOfRangeException">If <paramref name="lambda"/> is equal or less then 0.0.</exception>
public Poisson(double lambda)
{
if (!IsValidParameterSet(lambda))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(lambda);
_lambda = lambda;
}
/// <summary>
@ -68,8 +73,13 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="System.ArgumentOutOfRangeException">If <paramref name="lambda"/> is equal or less then 0.0.</exception>
public Poisson(double lambda, System.Random randomSource)
{
if (!IsValidParameterSet(lambda))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(lambda);
_lambda = lambda;
}
/// <summary>
@ -92,28 +102,12 @@ namespace MathNet.Numerics.Distributions
return lambda > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="lambda">The lambda (λ) parameter of the Poisson distribution. Range: λ > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double lambda)
{
if (!(lambda > 0.0))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_lambda = lambda;
}
/// <summary>
/// Gets or sets the Poisson distribution parameter λ. Range: λ > 0.
/// </summary>
public double Lambda
{
get { return _lambda; }
set { SetParameters(value); }
}
/// <summary>

32
src/Numerics/Distributions/Rayleigh.cs

@ -49,7 +49,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _scale;
readonly double _scale;
/// <summary>
/// Initializes a new instance of the <see cref="Rayleigh"/> class.
@ -58,8 +58,13 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Rayleigh(double scale)
{
if (!IsValidParameterSet(scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(scale);
_scale = scale;
}
/// <summary>
@ -70,8 +75,13 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Rayleigh(double scale, System.Random randomSource)
{
if (!IsValidParameterSet(scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(scale);
_scale = scale;
}
/// <summary>
@ -92,28 +102,12 @@ namespace MathNet.Numerics.Distributions
return scale > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double scale)
{
if (scale <= 0.0 || Double.IsNaN(scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_scale = scale;
}
/// <summary>
/// Gets or sets the scale (σ) of the distribution. Range: σ > 0.
/// </summary>
public double Scale
{
get { return _scale; }
set { SetParameters(value); }
}
/// <summary>

54
src/Numerics/Distributions/Stable.cs

@ -47,10 +47,10 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _alpha;
double _beta;
double _scale;
double _location;
readonly double _alpha;
readonly double _beta;
readonly double _scale;
readonly double _location;
/// <summary>
/// Initializes a new instance of the <see cref="Stable"/> class.
@ -61,8 +61,16 @@ namespace MathNet.Numerics.Distributions
/// <param name="location">The location (μ) of the distribution.</param>
public Stable(double alpha, double beta, double scale, double location)
{
if (!IsValidParameterSet(alpha, beta, scale, location))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(alpha, beta, scale, location);
_alpha = alpha;
_beta = beta;
_scale = scale;
_location = location;
}
/// <summary>
@ -75,8 +83,16 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Stable(double alpha, double beta, double scale, double location, System.Random randomSource)
{
if (!IsValidParameterSet(alpha, beta, scale, location))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(alpha, beta, scale, location);
_alpha = alpha;
_beta = beta;
_scale = scale;
_location = location;
}
/// <summary>
@ -100,35 +116,12 @@ namespace MathNet.Numerics.Distributions
return alpha > 0.0 && alpha <= 2.0 && beta >= -1.0 && beta <= 1.0 && scale > 0.0 && !Double.IsNaN(location);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="alpha">The stability (α) of the distribution. Range: 2 ≥ α > 0.</param>
/// <param name="beta">The skewness (β) of the distribution. Range: 1 ≥ β ≥ -1.</param>
/// <param name="scale">The scale (c) of the distribution. Range: c > 0.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double alpha, double beta, double scale, double location)
{
if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0
|| Double.IsNaN(alpha) || Double.IsNaN(beta) || Double.IsNaN(scale) || Double.IsNaN(location))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_alpha = alpha;
_beta = beta;
_scale = scale;
_location = location;
}
/// <summary>
/// Gets or sets the stability (α) of the distribution. Range: 2 ≥ α > 0.
/// </summary>
public double Alpha
{
get { return _alpha; }
set { SetParameters(value, _beta, _scale, _location); }
}
/// <summary>
@ -137,7 +130,6 @@ namespace MathNet.Numerics.Distributions
public double Beta
{
get { return _beta; }
set { SetParameters(_alpha, value, _scale, _location); }
}
/// <summary>
@ -146,7 +138,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_alpha, _beta, value, _location); }
}
/// <summary>
@ -155,7 +146,6 @@ namespace MathNet.Numerics.Distributions
public double Location
{
get { return _location; }
set { SetParameters(_alpha, _beta, _scale, value); }
}
/// <summary>

52
src/Numerics/Distributions/StudentT.cs

@ -61,9 +61,9 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _location;
double _scale;
double _freedom;
readonly double _location;
readonly double _scale;
readonly double _freedom;
/// <summary>
/// Initializes a new instance of the StudentT class. This is a Student t-distribution with location 0.0
@ -72,7 +72,9 @@ namespace MathNet.Numerics.Distributions
public StudentT()
{
_random = SystemRandomSource.Default;
SetParameters(0.0, 1.0, 1.0);
_location = 0.0;
_scale = 1.0;
_freedom = 1.0;
}
/// <summary>
@ -84,8 +86,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="freedom">The degrees of freedom (ν) for the distribution. Range: ν > 0.</param>
public StudentT(double location, double scale, double freedom)
{
if (!IsValidParameterSet(location, scale, freedom))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(location, scale, freedom);
_location = location;
_scale = scale;
_freedom = freedom;
}
/// <summary>
@ -98,8 +107,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public StudentT(double location, double scale, double freedom, System.Random randomSource)
{
if (!IsValidParameterSet(location, scale, freedom))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(location, scale, freedom);
_location = location;
_scale = scale;
_freedom = freedom;
}
/// <summary>
@ -119,26 +135,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="freedom">The degrees of freedom (ν) for the distribution. Range: ν > 0.</param>
public static bool IsValidParameterSet(double location, double scale, double freedom)
{
return scale > 0.0 && freedom > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution. Range: σ > 0.</param>
/// <param name="freedom">The degrees of freedom (ν) for the distribution. Range: ν > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double location, double scale, double freedom)
{
if (scale <= 0.0 || freedom <= 0.0 || Double.IsNaN(scale) || Double.IsNaN(location) || Double.IsNaN(freedom))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_location = location;
_scale = scale;
_freedom = freedom;
return scale > 0.0 && freedom > 0.0 && !Double.IsNaN(location);
}
/// <summary>
@ -147,7 +144,6 @@ namespace MathNet.Numerics.Distributions
public double Location
{
get { return _location; }
set { SetParameters(value, _scale, _freedom); }
}
/// <summary>
@ -156,7 +152,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_location, value, _freedom); }
}
/// <summary>
@ -165,7 +160,6 @@ namespace MathNet.Numerics.Distributions
public double DegreesOfFreedom
{
get { return _freedom; }
set { SetParameters(_location, _scale, value); }
}
/// <summary>

48
src/Numerics/Distributions/Triangular.cs

@ -49,9 +49,9 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _lower;
double _upper;
double _mode;
readonly double _lower;
readonly double _upper;
readonly double _mode;
/// <summary>
/// Initializes a new instance of the Triangular class with the given lower bound, upper bound and mode.
@ -62,8 +62,15 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If the upper bound is smaller than the mode or if the mode is smaller than the lower bound.</exception>
public Triangular(double lower, double upper, double mode)
{
if (!IsValidParameterSet(lower, upper, mode))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(lower, upper, mode);
_lower = lower;
_upper = upper;
_mode = mode;
}
/// <summary>
@ -76,8 +83,15 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If the upper bound is smaller than the mode or if the mode is smaller than the lower bound.</exception>
public Triangular(double lower, double upper, double mode, System.Random randomSource)
{
if (!IsValidParameterSet(lower, upper, mode))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(lower, upper, mode);
_lower = lower;
_upper = upper;
_mode = mode;
}
/// <summary>
@ -100,34 +114,12 @@ namespace MathNet.Numerics.Distributions
return upper >= mode && mode >= lower && !Double.IsInfinity(upper) && !Double.IsInfinity(lower) && !Double.IsInfinity(mode);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="lower">Lower bound. Range: lower ≤ mode ≤ upper</param>
/// <param name="upper">Upper bound. Range: lower ≤ mode ≤ upper</param>
/// <param name="mode">Mode (most frequent value). Range: lower ≤ mode ≤ upper</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double lower, double upper, double mode)
{
if (upper < mode || mode < lower || Double.IsNaN(upper) || Double.IsNaN(lower) || Double.IsNaN(mode)
|| Double.IsInfinity(upper) || Double.IsInfinity(lower) || Double.IsInfinity(mode)
)
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_lower = lower;
_upper = upper;
_mode = mode;
}
/// <summary>
/// Gets or sets the lower bound of the distribution.
/// </summary>
public double LowerBound
{
get { return _lower; }
set { SetParameters(value, _upper, _mode); }
}
/// <summary>
@ -136,7 +128,6 @@ namespace MathNet.Numerics.Distributions
public double UpperBound
{
get { return _upper; }
set { SetParameters(_lower, value, _mode); }
}
/// <summary>
@ -209,7 +200,6 @@ namespace MathNet.Numerics.Distributions
public double Mode
{
get { return _mode; }
set { SetParameters(_lower, _upper, value); }
}
/// <summary>

44
src/Numerics/Distributions/Weibull.cs

@ -49,8 +49,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _shape;
double _scale;
readonly double _shape;
readonly double _scale;
/// <summary>
/// Reusable intermediate result 1 / (_scale ^ _shape)
@ -59,7 +59,7 @@ namespace MathNet.Numerics.Distributions
/// By caching this parameter we can get slightly better numerics precision
/// in certain constellations without any additional computations.
/// </remarks>
double _scalePowShapeInv;
readonly double _scalePowShapeInv;
/// <summary>
/// Initializes a new instance of the Weibull class.
@ -68,8 +68,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale (λ) of the Weibull distribution. Range: λ > 0.</param>
public Weibull(double shape, double scale)
{
if (!IsValidParameterSet(shape, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(shape, scale);
_shape = shape;
_scale = scale;
_scalePowShapeInv = Math.Pow(scale, -shape);
}
/// <summary>
@ -80,8 +87,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Weibull(double shape, double scale, System.Random randomSource)
{
if (!IsValidParameterSet(shape, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(shape, scale);
_shape = shape;
_scale = scale;
_scalePowShapeInv = Math.Pow(scale, -shape);
}
/// <summary>
@ -103,31 +117,12 @@ namespace MathNet.Numerics.Distributions
return shape > 0.0 && scale > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="shape">The shape (k) of the Weibull distribution. Range: k > 0.</param>
/// <param name="scale">The scale (λ) of the Weibull distribution. Range: λ > 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double shape, double scale)
{
if (shape <= 0.0 || scale <= 0.0 || Double.IsNaN(shape) || Double.IsNaN(scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_shape = shape;
_scale = scale;
_scalePowShapeInv = Math.Pow(scale, -shape);
}
/// <summary>
/// Gets or sets the shape (k) of the Weibull distribution. Range: k > 0.
/// </summary>
public double Shape
{
get { return _shape; }
set { SetParameters(value, _scale); }
}
/// <summary>
@ -136,7 +131,6 @@ namespace MathNet.Numerics.Distributions
public double Scale
{
get { return _scale; }
set { SetParameters(_shape, value); }
}
/// <summary>

44
src/Numerics/Distributions/Wishart.cs

@ -51,17 +51,17 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The degrees of freedom for the Wishart distribution.
/// </summary>
double _degreesOfFreedom;
readonly double _degreesOfFreedom;
/// <summary>
/// The scale matrix for the Wishart distribution.
/// </summary>
Matrix<double> _scale;
readonly Matrix<double> _scale;
/// <summary>
/// Caches the Cholesky factorization of the scale matrix.
/// </summary>
Cholesky<double> _chol;
readonly Cholesky<double> _chol;
/// <summary>
/// Initializes a new instance of the <see cref="Wishart"/> class.
@ -70,8 +70,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
public Wishart(double degreesOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(degreesOfFreedom, scale);
_degreesOfFreedom = degreesOfFreedom;
_scale = scale;
_chol = _scale.Cholesky();
}
/// <summary>
@ -82,8 +89,15 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Wishart(double degreesOfFreedom, Matrix<double> scale, System.Random randomSource)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(degreesOfFreedom, scale);
_degreesOfFreedom = degreesOfFreedom;
_scale = scale;
_chol = _scale.Cholesky();
}
/// <summary>
@ -114,31 +128,12 @@ namespace MathNet.Numerics.Distributions
return true;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="degreesOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double degreesOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_degreesOfFreedom = degreesOfFreedom;
_scale = scale;
_chol = _scale.Cholesky();
}
/// <summary>
/// Gets or sets the degrees of freedom (n) for the Wishart distribution.
/// </summary>
public double DegreesOfFreedom
{
get { return _degreesOfFreedom; }
set { SetParameters(value, _scale); }
}
/// <summary>
@ -147,7 +142,6 @@ namespace MathNet.Numerics.Distributions
public Matrix<double> Scale
{
get { return _scale; }
set { SetParameters(_degreesOfFreedom, value); }
}
/// <summary>

39
src/Numerics/Distributions/Zipf.cs

@ -50,12 +50,12 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The s parameter of the distribution.
/// </summary>
double _s;
readonly double _s;
/// <summary>
/// The n parameter of the distribution.
/// </summary>
int _n;
readonly int _n;
/// <summary>
/// Initializes a new instance of the <see cref="Zipf"/> class.
@ -64,8 +64,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="n">The n parameter of the distribution.</param>
public Zipf(double s, int n)
{
if (!IsValidParameterSet(s, n))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = SystemRandomSource.Default;
SetParameters(s, n);
_s = s;
_n = n;
}
/// <summary>
@ -76,8 +82,14 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Zipf(double s, int n, System.Random randomSource)
{
if (!IsValidParameterSet(s, n))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_random = randomSource ?? SystemRandomSource.Default;
SetParameters(s, n);
_s = s;
_n = n;
}
/// <summary>
@ -99,30 +111,12 @@ namespace MathNet.Numerics.Distributions
return n > 0 && s > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="s">The s parameter of the distribution.</param>
/// <param name="n">The n parameter of the distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters are out of range.</exception>
void SetParameters(double s, int n)
{
if (!(n > 0 && s > 0.0))
{
throw new ArgumentException(Resources.InvalidDistributionParameters);
}
_s = s;
_n = n;
}
/// <summary>
/// Gets or sets the s parameter of the distribution.
/// </summary>
public double S
{
get { return _s; }
set { SetParameters(value, _n); }
}
/// <summary>
@ -131,7 +125,6 @@ namespace MathNet.Numerics.Distributions
public int N
{
get { return _n; }
set { SetParameters(_s, value); }
}
/// <summary>

56
src/UnitTests/DistributionTests/Continuous/BetaTests.cs

@ -89,62 +89,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Beta(α = 1, β = 2)", n.ToString());
}
/// <summary>
/// Can Set Shape A
/// </summary>
/// <param name="a">New A value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetShapeA(double a)
{
GC.KeepAlive(new Beta(1.0, 1.0)
{
A = a
});
}
/// <summary>
/// Set A fails with negative A.
/// </summary>
[Test]
public void SetShapeAFailsWithNegativeA()
{
var n = new Beta(1.0, 1.0);
Assert.That(() => n.A = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set shape B.
/// </summary>
/// <param name="b">New B value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetShapeB(double b)
{
GC.KeepAlive(new Beta(1.0, 1.0)
{
B = b
});
}
/// <summary>
/// Set shape B fails with negative B.
/// </summary>
[Test]
public void SetShapeBFailsWithNegativeB()
{
var n = new Beta(1.0, 1.0);
Assert.That(() => n.B = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

52
src/UnitTests/DistributionTests/Continuous/CauchyTests.cs

@ -94,58 +94,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Cauchy(x0 = 1, γ = 2)", n.ToString());
}
/// <summary>
/// Can set location.
/// </summary>
/// <param name="location">Location value.</param>
[TestCase(-10.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
public void CanSetLocation(double location)
{
GC.KeepAlive(new Cauchy
{
Location = location
});
}
/// <summary>
/// Set bad location fails.
/// </summary>
[Test]
public void SetBadLocationFail()
{
var n = new Cauchy();
Assert.That(() => n.Location = Double.NaN, Throws.ArgumentException);
}
/// <summary>
/// Can set scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(1.0)]
[TestCase(2.0)]
[TestCase(12.0)]
public void CanSetScale(double scale)
{
GC.KeepAlive(new Cauchy
{
Scale = scale
});
}
/// <summary>
/// Set bad scale fails.
/// </summary>
[Test]
public void SetBadScaleFail()
{
var n = new Cauchy();
Assert.That(() => n.Scale = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy.
/// </summary>

29
src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs

@ -80,35 +80,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("ChiSquared(k = 1)", n.ToString());
}
/// <summary>
/// Can set degrees of freedom.
/// </summary>
/// <param name="dof">Degrees of freedom.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetDoF(double dof)
{
GC.KeepAlive(new ChiSquared(1.0)
{
DegreesOfFreedom = dof
});
}
/// <summary>
/// Set Degrees of freedom fails with non-positive value.
/// </summary>
/// <param name="dof">Degrees of freedom.</param>
[TestCase(-1.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
public void SetDofFailsWithNonPositiveDoF(double dof)
{
var n = new ChiSquared(1.0);
Assert.That(() => n.DegreesOfFreedom = dof, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

29
src/UnitTests/DistributionTests/Continuous/ChiTests.cs

@ -78,35 +78,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Chi(k = 1)", n.ToString());
}
/// <summary>
/// Can set degrees of freedom.
/// </summary>
/// <param name="dof">Degrees of freedom.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetDoF(double dof)
{
GC.KeepAlive(new Chi(1.0)
{
DegreesOfFreedom = dof
});
}
/// <summary>
/// Set Degrees of freedom fails with non-positive value.
/// </summary>
/// <param name="dof">Degrees of freedom.</param>
[TestCase(-1.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
public void SetDofFailsWithNonPositiveDoF(double dof)
{
var n = new Chi(1.0);
Assert.That(() => n.DegreesOfFreedom = dof, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

52
src/UnitTests/DistributionTests/Continuous/ContinuousUniformTests.cs

@ -97,58 +97,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("ContinuousUniform(Lower = 1, Upper = 2)", n.ToString());
}
/// <summary>
/// Can set lower bound.
/// </summary>
/// <param name="lower">Lower bound.</param>
[TestCase(-10.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
public void CanSetLower(double lower)
{
GC.KeepAlive(new ContinuousUniform
{
LowerBound = lower
});
}
/// <summary>
/// Set bad lower bound fails.
/// </summary>
[Test]
public void SetBadLowerFails()
{
var n = new ContinuousUniform();
Assert.That(() => n.LowerBound = 3.0, Throws.ArgumentException);
}
/// <summary>
/// Can set upper bound.
/// </summary>
/// <param name="upper">Upper bound.</param>
[TestCase(1.0)]
[TestCase(2.0)]
[TestCase(12.0)]
public void CanSetUpper(double upper)
{
GC.KeepAlive(new ContinuousUniform
{
UpperBound = upper
});
}
/// <summary>
/// Set bad upper fails.
/// </summary>
[Test]
public void SetBadUpperFails()
{
var n = new ContinuousUniform();
Assert.That(() => n.UpperBound = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy.
/// </summary>

82
src/UnitTests/DistributionTests/Continuous/ErlangTests.cs

@ -120,88 +120,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Erlang(k = 1, λ = 2)", n.ToString());
}
/// <summary>
/// Can set shape.
/// </summary>
/// <param name="shape">New shape value.</param>
[TestCase(-0)]
[TestCase(0)]
[TestCase(1)]
[TestCase(10)]
public void CanSetShape(int shape)
{
GC.KeepAlive(new Erlang(1, 1.0)
{
Shape = shape
});
}
/// <summary>
/// Set shape fails with negative shape.
/// </summary>
[Test]
public void SetShapeFailsWithNegativeShape()
{
var n = new Erlang(1, 1.0);
Assert.That(() => n.Shape = -1, Throws.ArgumentException);
}
/// <summary>
/// Can set scale
/// </summary>
/// <param name="scale">New scale value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetScale(double scale)
{
GC.KeepAlive(new Erlang(1, 1.0)
{
Scale = scale
});
}
/// <summary>
/// Set scale fails with negative scale.
/// </summary>
[Test]
public void SetScaleFailsWithNegativeScale()
{
var n = new Erlang(1, 1.0);
Assert.That(() => n.Scale = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set inverse scale.
/// </summary>
/// <param name="invScale">Inverse scale value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetInvScale(double invScale)
{
GC.KeepAlive(new Erlang(1, 1.0)
{
Rate = invScale
});
}
/// <summary>
/// Set inverse scale fails with negative inverse scale.
/// </summary>
[Test]
public void SetInvScaleFailsWithNegativeInvScale()
{
var n = new Erlang(1, 1.0);
Assert.That(() => n.Rate = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

28
src/UnitTests/DistributionTests/Continuous/ExponentialTests.cs

@ -78,34 +78,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Exponential(λ = 2)", n.ToString());
}
/// <summary>
/// Can set lambda.
/// </summary>
/// <param name="lambda">Lambda value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetLambda(double lambda)
{
GC.KeepAlive(new Exponential(1.0)
{
Rate = lambda
});
}
/// <summary>
/// Set lambda fails with negative lambda.
/// </summary>
[Test]
public void SetLambdaFailsWithNegativeLambda()
{
var n = new Exponential(1.0);
Assert.That(() => n.Rate = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

52
src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs

@ -101,58 +101,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("FisherSnedecor(d1 = 2, d2 = 1)", n.ToString());
}
/// <summary>
/// Can set degree of freedom 1.
/// </summary>
/// <param name="d1">Degrees of freedom 1</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetDegreesOfFreedom1(double d1)
{
GC.KeepAlive(new FisherSnedecor(1.0, 2.0)
{
DegreesOfFreedom1 = d1
});
}
/// <summary>
/// Set degree of freedom 1 fails with negative value.
/// </summary>
[Test]
public void SetDegreesOfFreedom1FailsWithNegativeDegreeOfFreedom()
{
var n = new FisherSnedecor(1.0, 2.0);
Assert.That(() => n.DegreesOfFreedom1 = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set degree of freedom 2.
/// </summary>
/// <param name="d2">Degrees of freedom 2</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetDegreesOfFreedom2(double d2)
{
GC.KeepAlive(new FisherSnedecor(1.0, 2.0)
{
DegreesOfFreedom2 = d2
});
}
/// <summary>
/// Set degree of freedom 2 fails with negative value.
/// </summary>
[Test]
public void SetDegreesOfFreedom2FailsWithNegativeDegreeOfFreedom()
{
var n = new FisherSnedecor(1.0, 2.0);
Assert.That(() => n.DegreesOfFreedom2 = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

84
src/UnitTests/DistributionTests/Continuous/GammaTests.cs

@ -122,90 +122,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Gamma(α = 1, β = 2)", n.ToString());
}
/// <summary>
/// Can set shape.
/// </summary>
/// <param name="shape">Shape value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetShape(double shape)
{
GC.KeepAlive(new Gamma(1.0, 1.0)
{
Shape = shape
});
}
/// <summary>
/// Set shape fails with negative shape.
/// </summary>
[Test]
public void SetShapeFailsWithNegativeShape()
{
var n = new Gamma(1.0, 1.0);
Assert.That(() => n.Shape = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetScale(double scale)
{
GC.KeepAlive(new Gamma(1.0, 1.0)
{
Scale = scale
});
}
/// <summary>
/// Set scale fails with negative scale.
/// </summary>
[Test]
public void SetScaleFailsWithNegativeScale()
{
var n = new Gamma(1.0, 1.0);
Assert.That(() => n.Scale = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set inverse scale.
/// </summary>
/// <param name="invScale">Inverse scale value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetInvScale(double invScale)
{
GC.KeepAlive(new Gamma(1.0, 1.0)
{
Rate = invScale
});
}
/// <summary>
/// Set inverse scale fails with negative value.
/// </summary>
[Test]
public void SetInvScaleFailsWithNegativeInvScale()
{
var n = new Gamma(1.0, 1.0);
Assert.That(() => n.Rate = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

58
src/UnitTests/DistributionTests/Continuous/InverseGammaTests.cs

@ -86,64 +86,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("InverseGamma(α = 1.1, β = 2.1)", n.ToString());
}
/// <summary>
/// Can set A.
/// </summary>
/// <param name="a">A parameter.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetA(double a)
{
GC.KeepAlive(new InverseGamma(1.0, 1.0)
{
Shape = a
});
}
/// <summary>
/// Set A fails with non-positive value.
/// </summary>
/// <param name="a">A parameter.</param>
[TestCase(-1.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
public void SetAFailsWithNonPositiveA(double a)
{
var n = new InverseGamma(1.0, 1.0);
Assert.That(() => n.Shape = a, Throws.ArgumentException);
}
/// <summary>
/// Can set B.
/// </summary>
/// <param name="b">B parameter.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetB(double b)
{
GC.KeepAlive(new InverseGamma(1.0, 1.0)
{
Scale = b
});
}
/// <summary>
/// Set B fails with non-positive value.
/// </summary>
/// <param name="b">B parameter.</param>
[TestCase(-1.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
public void SetBFailsWithNonPositiveB(double b)
{
var n = new InverseGamma(1.0, 1.0);
Assert.That(() => n.Scale = b, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

59
src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs

@ -80,65 +80,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Laplace(μ = -1, b = 2)", n.ToString());
}
/// <summary>
/// Can set location.
/// </summary>
/// <param name="location">Location value.</param>
[TestCase(Double.NegativeInfinity)]
[TestCase(-5.0 - 1.0)]
[TestCase(0.0)]
[TestCase(1.0)]
[TestCase(5.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetLocation(double location)
{
GC.KeepAlive(new Laplace
{
Location = location
});
}
/// <summary>
/// Set location fails with negative value.
/// </summary>
[Test]
public void SetLocationFailsWithNegativeLocation()
{
var n = new Laplace();
Assert.That(() => n.Location = Double.NaN, Throws.ArgumentException);
}
/// <summary>
/// Can set scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetScale(double scale)
{
GC.KeepAlive(new Laplace
{
Scale = scale
});
}
/// <summary>
/// Set scale fails with negative value.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(0.0)]
[TestCase(-1.0)]
[TestCase(-5.0)]
[TestCase(Double.NegativeInfinity)]
[TestCase(Double.NaN)]
public void SetScaleFailsWithNegativeScale(double scale)
{
var n = new Laplace();
Assert.That(() => n.Scale = scale, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

48
src/UnitTests/DistributionTests/Continuous/LogNormalTests.cs

@ -85,54 +85,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("LogNormal(μ = 1, σ = 2)", n.ToString());
}
/// <summary>
/// Can set sigma.
/// </summary>
/// <param name="sigma">Sigma value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetSigma(double sigma)
{
GC.KeepAlive(new LogNormal(1.0, 2.0)
{
Sigma = sigma
});
}
/// <summary>
/// Set sigma fails with negative value.
/// </summary>
[Test]
public void SetSigmaFailsWithNegativeSigma()
{
var n = new LogNormal(1.0, 2.0);
Assert.That(() => n.Sigma = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set mu.
/// </summary>
/// <param name="mu">Mu parameter.</param>
[TestCase(Double.NegativeInfinity)]
[TestCase(-1.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetMu(double mu)
{
GC.KeepAlive(new LogNormal(1.0, 2.0)
{
Mu = mu
});
}
/// <summary>
/// Validate entropy.
/// </summary>

107
src/UnitTests/DistributionTests/Continuous/NormalTests.cs

@ -150,113 +150,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Normal(μ = 1, σ = 2)", n.ToString());
}
/// <summary>
/// Can set precision.
/// </summary>
/// <param name="prec">Precision value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetPrecision(double prec)
{
GC.KeepAlive(new Normal
{
Precision = prec
});
}
/// <summary>
/// Set precision fails with negative value.
/// </summary>
[Test]
public void SetPrecisionFailsWithNegativePrecision()
{
var n = new Normal();
Assert.That(() => n.Precision = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set variance.
/// </summary>
/// <param name="var">Variance value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetVariance(double var)
{
var dist = new Normal
{
Variance = var
};
Assert.AreEqual(var, dist.Variance, 1e-14);
}
/// <summary>
/// Set variance fails with negative value.
/// </summary>
[Test]
public void SetVarianceFailsWithNegativeVariance()
{
var n = new Normal();
Assert.That(() => n.Variance = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set standard deviation.
/// </summary>
/// <param name="sdev">Standard deviation value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetStdDev(double sdev)
{
var dist = new Normal
{
StdDev = sdev
};
Assert.AreEqual(sdev, dist.StdDev, 1e-14);
}
/// <summary>
/// Set standard deviation fails with negative value.
/// </summary>
[Test]
public void SetStdDevFailsWithNegativeStdDev()
{
var n = new Normal();
Assert.That(() => n.StdDev = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set mean.
/// </summary>
/// <param name="mean">Mean value.</param>
[TestCase(Double.NegativeInfinity)]
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetMean(double mean)
{
GC.KeepAlive(new Normal
{
Mean = mean
});
}
/// <summary>
/// Validate entropy.
/// </summary>

52
src/UnitTests/DistributionTests/Continuous/ParetoTests.cs

@ -86,58 +86,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Pareto(xm = 1, α = 2)", n.ToString());
}
/// <summary>
/// Can set scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetScale(double scale)
{
GC.KeepAlive(new Pareto(1.0, 1.0)
{
Scale = scale
});
}
/// <summary>
/// Set scale fails with negative scale.
/// </summary>
[Test]
public void SetScaleFailsWithNegativeScale()
{
var n = new Pareto(1.0, 1.0);
Assert.That(() => n.Scale = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Can set shape.
/// </summary>
/// <param name="shape">Shape value.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetShape(double shape)
{
GC.KeepAlive(new Pareto(1.0, 1.0)
{
Shape = shape
});
}
/// <summary>
/// Set shape fails with negative shape.
/// </summary>
[Test]
public void SetShapeFailsWithNegativeShape()
{
var n = new Pareto(1.0, 1.0);
Assert.That(() => n.Shape = -1.0, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

31
src/UnitTests/DistributionTests/Continuous/RayleighTests.cs

@ -78,37 +78,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Rayleigh(σ = 2)", n.ToString());
}
/// <summary>
/// Can set scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetScale(double scale)
{
GC.KeepAlive(new Rayleigh(1.0)
{
Scale = scale
});
}
/// <summary>
/// Set scale fails with negative scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(-1.0)]
[TestCase(Double.NegativeInfinity)]
[TestCase(Double.NaN)]
public void SetScaleFailsWithNegativeScale(double scale)
{
var n = new Rayleigh(1.0);
Assert.That(() => n.Scale = scale, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

122
src/UnitTests/DistributionTests/Continuous/StableTests.cs

@ -102,128 +102,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Stable(α = 1.2, β = 0.3, c = 1, μ = 2)", n.ToString());
}
/// <summary>
/// Can set alpha.
/// </summary>
/// <param name="alpha">Alpha value.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(2.0)]
public void CanSetAlpha(double alpha)
{
GC.KeepAlive(new Stable(1.0, 1.0, 1.0, 1.0)
{
Alpha = alpha
});
}
/// <summary>
/// Set alpha fails with bad values.
/// </summary>
/// <param name="alpha">Alpha value.</param>
[TestCase(Double.NaN)]
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(2.1)]
[TestCase(Double.NegativeInfinity)]
[TestCase(Double.PositiveInfinity)]
public void SetAlphaFail(double alpha)
{
var n = new Stable(1.0, 1.0, 1.0, 1.0);
Assert.That(() => n.Alpha = alpha, Throws.ArgumentException);
}
/// <summary>
/// Can set beta.
/// </summary>
/// <param name="beta">Beta value.</param>
[TestCase(-1.0)]
[TestCase(-0.1)]
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
public void CanSetBeta(double beta)
{
GC.KeepAlive(new Stable(1.0, 1.0, 1.0, 1.0)
{
Beta = beta
});
}
/// <summary>
/// Set beta fails with bad values.
/// </summary>
/// <param name="beta">Beta value.</param>
[TestCase(Double.NaN)]
[TestCase(-1.1)]
[TestCase(1.1)]
[TestCase(Double.NegativeInfinity)]
[TestCase(Double.PositiveInfinity)]
public void SetBetaFail(double beta)
{
var n = new Stable(1.0, 1.0, 1.0, 1.0);
Assert.That(() => n.Beta = beta, Throws.ArgumentException);
}
/// <summary>
/// Can set scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetScale(double scale)
{
GC.KeepAlive(new Stable(1.0, 1.0, 1.0, 1.0)
{
Scale = scale
});
}
/// <summary>
/// Set scale fails with bad values.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(Double.NaN)]
[TestCase(0.0)]
public void SetScaleFail(double scale)
{
var n = new Stable(1.0, 1.0, 1.0, 1.0);
Assert.That(() => n.Scale = scale, Throws.ArgumentException);
}
/// <summary>
/// Can set location.
/// </summary>
/// <param name="location">Location value.</param>
[TestCase(Double.NegativeInfinity)]
[TestCase(-10.0)]
[TestCase(-1.0)]
[TestCase(-0.1)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetLocation(double location)
{
GC.KeepAlive(new Stable(1.0, 1.0, 1.0, 1.0)
{
Location = location
});
}
/// <summary>
/// Set location fails with bad values.
/// </summary>
[Test]
public void SetLocationFail()
{
var n = new Stable(1.0, 1.0, 1.0, 1.0);
Assert.That(() => n.Location = Double.NaN, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy throws <c>NotSupportedException</c>.
/// </summary>

78
src/UnitTests/DistributionTests/Continuous/StudentTTests.cs

@ -98,84 +98,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("StudentT(μ = 1, σ = 2, ν = 1)", n.ToString());
}
/// <summary>
/// Can set location.
/// </summary>
/// <param name="loc">Location value.</param>
[TestCase(Double.NegativeInfinity)]
[TestCase(-5.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetLocation(double loc)
{
GC.KeepAlive(new StudentT
{
Location = loc
});
}
/// <summary>
/// Can set scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetScale(double scale)
{
GC.KeepAlive(new StudentT
{
Scale = scale
});
}
/// <summary>
/// Set scale fails with non-positive scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(-1.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
public void SetScaleFailsWithNonPositiveScale(double scale)
{
var n = new StudentT();
Assert.That(() => n.Scale = scale, Throws.ArgumentException);
}
/// <summary>
/// Can set degrees of freedom.
/// </summary>
/// <param name="dof">Degrees of freedom.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetDoF(double dof)
{
GC.KeepAlive(new StudentT
{
DegreesOfFreedom = dof
});
}
/// <summary>
/// Set degrees of freedom fails with non-positive value.
/// </summary>
/// <param name="dof">Degrees of freedom.</param>
[TestCase(-1.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
public void SetDofFailsWithNonPositiveDoF(double dof)
{
var n = new StudentT();
Assert.That(() => n.DegreesOfFreedom = dof, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

30
src/UnitTests/DistributionTests/Continuous/TriangularTests.cs

@ -92,36 +92,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
var n = new Triangular(0d, 2d, 1d);
Assert.AreEqual("Triangular(Lower = 0, Upper = 2, Mode = 1)", n.ToString());
}
/// <summary>
/// Can Set Lower Bound
/// </summary>
/// <param name="lower">New Lower Bound.</param>
[TestCase(-0.0)]
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
public void CanSetLowerBound(double lower)
{
GC.KeepAlive(new Triangular(0.0, 20.0, 10.0)
{
LowerBound = lower
});
}
/// <summary>
/// Set LowerBound fails if greater than Mode.
/// </summary>
[TestCase(1.0, 0.5)]
[TestCase(Double.PositiveInfinity, 0.5)]
public void SetLowerBoundFailsIfGreaterThanMode(double lower, double mode)
{
var n = new Triangular(0.0, 100.0, mode);
Assert.That(() => n.LowerBound = lower, Throws.ArgumentException);
}
// Todo: Add tests for setting upper bound and mode values.
// Todo: Add tests for:
// - Mean,

58
src/UnitTests/DistributionTests/Continuous/WeibullTests.cs

@ -88,64 +88,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.AreEqual("Weibull(k = 1, λ = 2)", n.ToString());
}
/// <summary>
/// Can set shape.
/// </summary>
/// <param name="shape">Shape value.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetShape(double shape)
{
GC.KeepAlive(new Weibull(1.0, 1.0)
{
Shape = shape
});
}
/// <summary>
/// Set shape fails with negative shape.
/// </summary>
/// <param name="shape">Shape value.</param>
[TestCase(-1.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
public void SetShapeFailsWithNegativeShape(double shape)
{
var n = new Weibull(1.0, 1.0);
Assert.That(() => n.Shape = shape, Throws.ArgumentException);
}
/// <summary>
/// Can set scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetScale(double scale)
{
GC.KeepAlive(new Weibull(1.0, 1.0)
{
Scale = scale
});
}
/// <summary>
/// Set scale fails with negative scale.
/// </summary>
/// <param name="scale">Scale value.</param>
[TestCase(-1.0)]
[TestCase(-0.0)]
[TestCase(0.0)]
public void SetScaleFailsWithNegativeScale(double scale)
{
var n = new Weibull(1.0, 1.0);
Assert.That(() => n.Scale = scale, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

28
src/UnitTests/DistributionTests/Discrete/BernoulliTests.cs

@ -76,34 +76,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual("Bernoulli(p = 0.3)", b.ToString());
}
/// <summary>
/// Can set probability of one.
/// </summary>
/// <param name="p">Probability of one.</param>
[TestCase(0.0)]
[TestCase(0.3)]
[TestCase(1.0)]
public void CanSetProbabilityOfOne(double p)
{
GC.KeepAlive(new Bernoulli(0.3)
{
P = p
});
}
/// <summary>
/// Set probability of one fails with bad values.
/// </summary>
/// <param name="p">Probability of one.</param>
[TestCase(Double.NaN)]
[TestCase(-1.0)]
[TestCase(2.0)]
public void SetProbabilityOfOneFails(double p)
{
var b = new Bernoulli(0.3);
Assert.That(() => b.P = p, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy.
/// </summary>

29
src/UnitTests/DistributionTests/Discrete/BinomialTests.cs

@ -79,35 +79,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual("Binomial(p = 0.3, n = 2)", b.ToString());
}
/// <summary>
/// Can set success probability.
/// </summary>
/// <param name="p">Success probability.</param>
/// <param name="n">Number of trials.</param>
[TestCase(0.0, 4)]
[TestCase(0.3, 3)]
[TestCase(1.0, 2)]
public void CanSetSuccessProbability(double p, int n)
{
GC.KeepAlive(new Binomial(0.3, n)
{
P = p
});
}
/// <summary>
/// Set success probability fails with bad values.
/// </summary>
/// <param name="p">Success probability.</param>
[TestCase(Double.NaN)]
[TestCase(-1.0)]
[TestCase(2.0)]
public void SetProbabilityOfOneFails(double p)
{
var b = new Binomial(0.3, 1);
Assert.That(() => b.P = p, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy.
/// </summary>

22
src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs

@ -139,28 +139,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual("Categorical(Dimension = 3)", b.ToString());
}
/// <summary>
/// Can set probability.
/// </summary>
[Test]
public void CanSetProbability()
{
GC.KeepAlive(new Categorical(_largeP)
{
P = _smallP
});
}
/// <summary>
/// Set probability with a bad array fails.
/// </summary>
[Test]
public void SetProbabilityFails()
{
var b = new Categorical(_largeP);
Assert.That(() => b.P = _badP, Throws.ArgumentException);
}
/// <summary>
/// Validate mean.
/// </summary>

60
src/UnitTests/DistributionTests/Discrete/ConwayMaxwellPoissonTests.cs

@ -77,66 +77,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual("ConwayMaxwellPoisson(λ = 1, ν = 2)", d.ToString());
}
/// <summary>
/// Can set lambda.
/// </summary>
/// <param name="lambda">Lambda value.</param>
[TestCase(0.1)]
[TestCase(3.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetLambda(double lambda)
{
GC.KeepAlive(new ConwayMaxwellPoisson(1.0, 2.0)
{
Lambda = lambda
});
}
/// <summary>
/// Can set DegreesOfFreedom.
/// </summary>
/// <param name="nu">DegreesOfFreedom parameter.</param>
[TestCase(0.0)]
[TestCase(3.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetNu(double nu)
{
GC.KeepAlive(new ConwayMaxwellPoisson(1.0, 2.0)
{
Nu = nu
});
}
/// <summary>
/// Set lambda with bad values fails.
/// </summary>
/// <param name="lambda">Lambda value.</param>
[TestCase(0.0)]
[TestCase(-0.0)]
[TestCase(-1.0)]
[TestCase(Double.NegativeInfinity)]
public void SetLambdaFails(double lambda)
{
var d = new ConwayMaxwellPoisson(1.0, 2.0);
Assert.That(() => d.Lambda = lambda, Throws.ArgumentException);
}
/// <summary>
/// Set DegreesOfFreedom with bad values fails.
/// </summary>
/// <param name="nu">DegreesOfFreedom parameter.</param>
[TestCase(-0.1)]
[TestCase(-1.0)]
[TestCase(-10.0)]
[TestCase(Double.NegativeInfinity)]
public void SetNuFails(double nu)
{
var d = new ConwayMaxwellPoisson(1.0, 2.0);
Assert.That(() => d.Nu = nu, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy throws <c>NotSupportedException</c>.
/// </summary>

68
src/UnitTests/DistributionTests/Discrete/DiscreteUniformTests.cs

@ -79,66 +79,12 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual("DiscreteUniform(Lower = 0, Upper = 10)", b.ToString());
}
/// <summary>
/// Can set lower bound.
/// </summary>
/// <param name="p">Lower bound.</param>
[TestCase(0)]
[TestCase(3)]
[TestCase(10)]
public void CanSetLowerBound(int p)
{
GC.KeepAlive(new DiscreteUniform(0, 10)
{
LowerBound = p
});
}
/// <summary>
/// Can set upper bound.
/// </summary>
/// <param name="p">Upper bound.</param>
[TestCase(0)]
[TestCase(3)]
[TestCase(10)]
public void CanSetUpperBound(int p)
{
GC.KeepAlive(new DiscreteUniform(0, 10)
{
UpperBound = p
});
}
/// <summary>
/// Set lower bound with bad values fails.
/// </summary>
/// <param name="p">Lower bound.</param>
[TestCase(11)]
[TestCase(20)]
public void SetLowerBoundFails(int p)
{
var b = new DiscreteUniform(0, 10);
Assert.That(() => b.LowerBound = p, Throws.ArgumentException);
}
/// <summary>
/// Set upper bound with bad values fails
/// </summary>
/// <param name="p">Upper bound.</param>
[TestCase(-11)]
[TestCase(-20)]
public void SetUpperBoundFails(int p)
{
var b = new DiscreteUniform(0, 10);
Assert.That(() => b.UpperBound = p, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy.
/// </summary>
/// <param name="l">Lower bound.</param>
/// <param name="u">Upper bound.</param>
/// <param name="e">Expceted value.</param>
/// <param name="e">Expected value.</param>
[TestCase(-10, 10, 3.0445224377234229965005979803657054342845752874046093)]
[TestCase(0, 4, 1.6094379124341003746007593332261876395256013542685181)]
[TestCase(10, 20, 2.3978952727983705440619435779651292998217068539374197)]
@ -169,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// </summary>
/// <param name="l">Lower bound.</param>
/// <param name="u">Upper bound.</param>
/// <param name="m">Expceted value.</param>
/// <param name="m">Expected value.</param>
[TestCase(-10, 10, 0)]
[TestCase(0, 4, 2)]
[TestCase(10, 20, 15)]
@ -185,7 +131,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// </summary>
/// <param name="l">Lower bound.</param>
/// <param name="u">Upper bound.</param>
/// <param name="m">Expceted value.</param>
/// <param name="m">Expected value.</param>
[TestCase(-10, 10, 0)]
[TestCase(0, 4, 2)]
[TestCase(10, 20, 15)]
@ -201,7 +147,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// </summary>
/// <param name="l">Lower bound.</param>
/// <param name="u">Upper bound.</param>
/// <param name="m">Expceted value.</param>
/// <param name="m">Expected value.</param>
[TestCase(-10, 10, 0)]
[TestCase(0, 4, 2)]
[TestCase(10, 20, 15)]
@ -238,7 +184,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// <param name="l">Lower bound.</param>
/// <param name="u">Upper bound.</param>
/// <param name="x">Input X value.</param>
/// <param name="p">Expceted value.</param>
/// <param name="p">Expected value.</param>
[TestCase(-10, 10, -5, 1 / 21.0)]
[TestCase(-10, 10, 1, 1 / 21.0)]
[TestCase(-10, 10, 10, 1 / 21.0)]
@ -256,7 +202,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// <param name="l">Lower bound.</param>
/// <param name="u">Upper bound.</param>
/// <param name="x">Input X value.</param>
/// <param name="dln">Expceted value.</param>
/// <param name="dln">Expected value.</param>
[TestCase(-10, 10, -5, -3.0445224377234229965005979803657054342845752874046093)]
[TestCase(-10, 10, 1, -3.0445224377234229965005979803657054342845752874046093)]
[TestCase(-10, 10, 10, -3.0445224377234229965005979803657054342845752874046093)]
@ -332,7 +278,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// <param name="l">Lower bound.</param>
/// <param name="u">Upper bound.</param>
/// <param name="x">Input X value.</param>
/// <param name="cdf">Expceted value.</param>
/// <param name="cdf">Expected value.</param>
[TestCase(-10, 10, -5, 6.0 / 21.0)]
[TestCase(-10, 10, 1, 12.0 / 21.0)]
[TestCase(-10, 10, 10, 1.0)]

28
src/UnitTests/DistributionTests/Discrete/GeometricTests.cs

@ -76,34 +76,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual("Geometric(p = 0.3)", d.ToString());
}
/// <summary>
/// Can set probability of one.
/// </summary>
/// <param name="p">Probability of generating a one.</param>
[TestCase(0.0)]
[TestCase(0.3)]
[TestCase(1.0)]
public void CanSetProbabilityOfOne(double p)
{
GC.KeepAlive(new Geometric(0.3)
{
P = p
});
}
/// <summary>
/// Set probability of one with a bad value fails.
/// </summary>
/// <param name="p">Probability of generating a one.</param>
[TestCase(Double.NaN)]
[TestCase(-1.0)]
[TestCase(2.0)]
public void SetProbabilityOfOneFails(double p)
{
var d = new Geometric(0.3);
Assert.That(() => d.P = p, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy.
/// </summary>

83
src/UnitTests/DistributionTests/Discrete/HypergeometricTests.cs

@ -86,89 +86,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual("Hypergeometric(N = 10, M = 1, n = 1)", d.ToString());
}
/// <summary>
/// Can set size.
/// </summary>
/// <param name="population">Population size.</param>
[TestCase(5)]
[TestCase(10)]
[TestCase(20)]
public void CanSetPopulation(int population)
{
GC.KeepAlive(new Hypergeometric(10, 1, 1)
{
Population = population
});
}
/// <summary>
/// Set size fails with bad values.
/// </summary>
/// <param name="population">Population size.</param>
[TestCase(-1)]
[TestCase(0)]
public void SetPopulationFails(int population)
{
var d = new Hypergeometric(10, 1, 1);
Assert.That(() => d.Population = population, Throws.ArgumentException);
}
/// <summary>
/// Can set M.
/// </summary>
/// <param name="success">M parameter.</param>
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[TestCase(5)]
public void CanSetSuccess(int success)
{
GC.KeepAlive(new Hypergeometric(10, 1, 1)
{
Success = success
});
}
/// <summary>
/// Set M fails with bad values.
/// </summary>
/// <param name="success">M parameter.</param>
[TestCase(11)]
[TestCase(-1)]
public void SetSuccessFails(int success)
{
var d = new Hypergeometric(10, 1, 1);
Assert.That(() => d.Success = success, Throws.ArgumentException);
}
/// <summary>
/// Can set N.
/// </summary>
/// <param name="draws">N parameter.</param>
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[TestCase(5)]
public void CanSetDraws(int draws)
{
GC.KeepAlive(new Hypergeometric(10, 1, 1)
{
Draws = draws
});
}
/// <summary>
/// Set N fails with bad values.
/// </summary>
/// <param name="draws">N parameter.</param>
[TestCase(11)]
[TestCase(-1)]
public void SetDrawsFails(int draws)
{
var d = new Hypergeometric(10, 1, 1);
Assert.That(() => d.Draws = draws, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy throws <c>NotSupportedException</c>.
/// </summary>

56
src/UnitTests/DistributionTests/Discrete/NegativeBinomialTests.cs

@ -84,62 +84,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual(String.Format("NegativeBinomial(R = {0}, P = {1})", d.R, d.P), d.ToString());
}
/// <summary>
/// Can set R.
/// </summary>
/// <param name="r">Number of trials.</param>
[TestCase(0.0)]
[TestCase(0.1)]
[TestCase(1.0)]
public void CanSetR(double r)
{
GC.KeepAlive(new NegativeBinomial(1.0, 0.5)
{
R = r
});
}
/// <summary>
/// Set R fails with bad values.
/// </summary>
/// <param name="r">Number of trials.</param>
[TestCase(Double.NaN)]
[TestCase(-1.0)]
[TestCase(Double.NegativeInfinity)]
public void SetRFails(double r)
{
var d = new NegativeBinomial(1.0, 0.5);
Assert.That(() => d.R = r, Throws.ArgumentException);
}
/// <summary>
/// Can set probability of one.
/// </summary>
/// <param name="p">Probability of success.</param>
[TestCase(0.0)]
[TestCase(0.3)]
[TestCase(1.0)]
public void CanSetProbabilityOfOne(double p)
{
GC.KeepAlive(new NegativeBinomial(1.0, 0.5)
{
P = p
});
}
/// <summary>
/// Set probability of one fails with bad values.
/// </summary>
/// <param name="p">Probability of success.</param>
[TestCase(Double.NaN)]
[TestCase(-1.0)]
[TestCase(2.0)]
public void SetProbabilityOfOneFails(double p)
{
var d = new NegativeBinomial(1.0, 0.5);
Assert.That(() => d.P = p, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy throws <c>NotSupportedException</c>.
/// </summary>

28
src/UnitTests/DistributionTests/Discrete/PoissonTests.cs

@ -76,34 +76,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual(String.Format("Poisson(λ = {0})", 0.3), d.ToString());
}
/// <summary>
/// Can set probability of one.
/// </summary>
/// <param name="lambda">Lambda value.</param>
[TestCase(1.5)]
[TestCase(5.4)]
[TestCase(10.8)]
public void CanSetProbabilityOfOne(double lambda)
{
GC.KeepAlive(new Poisson(0.3)
{
Lambda = lambda
});
}
/// <summary>
/// Set probability of one fails with bad value.
/// </summary>
/// <param name="lambda">Lambda value.</param>
[TestCase(Double.NaN)]
[TestCase(-1.5)]
[TestCase(0.0)]
public void SetProbabilityOfOneFails(double lambda)
{
var d = new Poisson(0.3);
Assert.That(() => d.Lambda = lambda, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy.
/// </summary>

55
src/UnitTests/DistributionTests/Discrete/ZipfTests.cs

@ -78,61 +78,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual("Zipf(S = 1, N = 5)", d.ToString());
}
/// <summary>
/// Can set S.
/// </summary>
/// <param name="s">S parameter.</param>
[TestCase(0.1)]
[TestCase(1.0)]
[TestCase(5.0)]
public void CanSetS(double s)
{
GC.KeepAlive(new Zipf(1.0, 5)
{
S = s
});
}
/// <summary>
/// Set S fails with bad values.
/// </summary>
/// <param name="s">S parameter.</param>
[TestCase(Double.NaN)]
[TestCase(-1.0)]
[TestCase(Double.NegativeInfinity)]
public void SetSFails(double s)
{
var d = new Zipf(1.0, 5);
Assert.That(() => d.S = s, Throws.ArgumentException);
}
/// <summary>
/// Can set N.
/// </summary>
/// <param name="n">N parameter.</param>
[TestCase(1)]
[TestCase(20)]
[TestCase(50)]
public void CanSetN(int n)
{
GC.KeepAlive(new Zipf(1.0, 5)
{
N = n
});
}
/// <summary>
/// Set N fails with bad values.
/// </summary>
/// <param name="n">N parameter.</param>
[TestCase(-1)]
[TestCase(0)]
public void SetNFails(int n)
{
var d = new Zipf(1.0, 5);
Assert.That(() => d.N = n, Throws.ArgumentException);
}
/// <summary>
/// Validate entropy.
/// </summary>

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

@ -145,23 +145,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
}
}
/// <summary>
/// Can set alpha.
/// </summary>
[Test]
public void CanSetAlpha()
{
var d = new Dirichlet(0.3, 10);
var alpha = new double[10];
for (var i = 0; i < 10; i++)
{
alpha[i] = i;
}
d.Alpha = alpha;
}
/// <summary>
/// Validate mean.
/// </summary>

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

@ -152,21 +152,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
Assert.AreEqual(nu, d.DegreesOfFreedom);
}
/// <summary>
/// Can set DegreesOfFreedom.
/// </summary>
/// <param name="nu">DegreesOfFreedom parameter.</param>
[TestCase(1.0)]
[TestCase(2.0)]
[TestCase(5.0)]
public void CanSetNu(double nu)
{
GC.KeepAlive(new InverseWishart(1.0, Matrix<double>.Build.RandomPositiveDefinite(2, 1))
{
DegreesOfFreedom = nu
});
}
/// <summary>
/// Can get scale matrix.
/// </summary>
@ -186,18 +171,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
}
}
/// <summary>
/// Can set scale matrix.
/// </summary>
[Test]
public void CanSetS()
{
GC.KeepAlive(new InverseWishart(1.0, Matrix<double>.Build.RandomPositiveDefinite(2, 1))
{
Scale = Matrix<double>.Build.RandomPositiveDefinite(2, 1)
});
}
/// <summary>
/// Validate mean.
/// </summary>

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

@ -173,22 +173,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
}
}
/// <summary>
/// Can set M.
/// </summary>
/// <param name="n">Matrix rows count.</param>
/// <param name="p">Matrix columns count.</param>
[TestCase(1, 1)]
[TestCase(3, 3)]
[TestCase(10, 10)]
public void CanSetM(int n, int p)
{
GC.KeepAlive(new MatrixNormal(Matrix<double>.Build.Random(n, p, 1), Matrix<double>.Build.RandomPositiveDefinite(n, 1), Matrix<double>.Build.RandomPositiveDefinite(p, 1))
{
Mean = Matrix<double>.Build.Random(n, p, 1)
});
}
/// <summary>
/// Can get V matrix.
/// </summary>
@ -210,22 +194,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
}
}
/// <summary>
/// Can set V matrix.
/// </summary>
/// <param name="n">Matrix rows count.</param>
/// <param name="p">Matrix columns count.</param>
[TestCase(1, 1)]
[TestCase(3, 3)]
[TestCase(10, 10)]
public void CanSetV(int n, int p)
{
GC.KeepAlive(new MatrixNormal(Matrix<double>.Build.Random(n, p, 1), Matrix<double>.Build.RandomPositiveDefinite(n, 1), Matrix<double>.Build.RandomPositiveDefinite(p, 1))
{
RowCovariance = Matrix<double>.Build.RandomPositiveDefinite(n, 1)
});
}
/// <summary>
/// Can get K matrix.
/// </summary>
@ -247,22 +215,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
}
}
/// <summary>
/// Can set K matrix.
/// </summary>
/// <param name="n">Matrix rows count.</param>
/// <param name="p">Matrix columns count.</param>
[TestCase(1, 1)]
[TestCase(3, 3)]
[TestCase(10, 10)]
public void CanSetK(int n, int p)
{
GC.KeepAlive(new MatrixNormal(Matrix<double>.Build.Random(n, p, 1), Matrix<double>.Build.RandomPositiveDefinite(n, 1), Matrix<double>.Build.RandomPositiveDefinite(p, 1))
{
ColumnCovariance = Matrix<double>.Build.RandomPositiveDefinite(p, 1)
});
}
/// <summary>
/// Validate density.
/// </summary>

22
src/UnitTests/DistributionTests/Multivariate/MultinomialTests.cs

@ -219,28 +219,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
AssertHelpers.AlmostEqualRelative(b.ProbabilityLn(x), Math.Log(b.Probability(x)), 12);
}
/// <summary>
/// Can set probability.
/// </summary>
[Test]
public void CanSetProbability()
{
GC.KeepAlive(new Multinomial(_largeP, 4)
{
P = _smallP
});
}
/// <summary>
/// Set bad values of probability fails.
/// </summary>
[Test]
public void SetProbabilityFails()
{
var b = new Multinomial(_largeP, 4);
Assert.That(() => b.P = _badP, Throws.ArgumentException);
}
/// <summary>
/// Can sample static.
/// </summary>

85
src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs

@ -97,28 +97,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
Assert.AreEqual(meanLocation, ng.MeanLocation);
}
/// <summary>
/// Can set mean location.
/// </summary>
/// <param name="meanLocation">Mean location.</param>
/// <param name="meanScale">Mean scale.</param>
/// <param name="precShape">Precision shape.</param>
/// <param name="precInvScale">Precision inverse scale.</param>
[TestCase(0.0, 1.0, 1.0, 1.0)]
[TestCase(10.0, 2.0, 2.0, 2.0)]
public void CanSetMeanLocation(double meanLocation, double meanScale, double precShape, double precInvScale)
{
var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale)
{
MeanLocation = -5.0
};
Assert.AreEqual(-5.0, ng.MeanLocation);
Assert.AreEqual(meanScale, ng.MeanScale);
Assert.AreEqual(precShape, ng.PrecisionShape);
Assert.AreEqual(precInvScale, ng.PrecisionInverseScale);
}
/// <summary>
/// Can get mean scale.
/// </summary>
@ -134,27 +112,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
Assert.AreEqual(meanScale, ng.MeanScale);
}
/// <summary>
/// Can set mean scale.
/// </summary>
/// <param name="meanLocation">Mean location.</param>
/// <param name="meanScale">Mean scale.</param>
/// <param name="precShape">Precision shape.</param>
/// <param name="precInvScale">Precision inverse scale.</param>
[TestCase(0.0, 1.0, 1.0, 1.0)]
[TestCase(10.0, 2.0, 2.0, 2.0)]
public void CanSetMeanScale(double meanLocation, double meanScale, double precShape, double precInvScale)
{
var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale)
{
MeanScale = 5.0
};
Assert.AreEqual(meanLocation, ng.MeanLocation);
Assert.AreEqual(5.0, ng.MeanScale);
Assert.AreEqual(precShape, ng.PrecisionShape);
Assert.AreEqual(precInvScale, ng.PrecisionInverseScale);
}
/// <summary>
/// Can get precision shape.
/// </summary>
@ -170,27 +127,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
Assert.AreEqual(precShape, ng.PrecisionShape);
}
/// <summary>
/// Can set precision shape.
/// </summary>
/// <param name="meanLocation">Mean location.</param>
/// <param name="meanScale">Mean scale.</param>
/// <param name="precShape">Precision shape.</param>
/// <param name="precInvScale">Precision inverse scale.</param>
[TestCase(0.0, 1.0, 1.0, 1.0)]
[TestCase(10.0, 2.0, 2.0, 2.0)]
public void CanSetPrecisionShape(double meanLocation, double meanScale, double precShape, double precInvScale)
{
var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale)
{
PrecisionShape = 5.0
};
Assert.AreEqual(meanLocation, ng.MeanLocation);
Assert.AreEqual(meanScale, ng.MeanScale);
Assert.AreEqual(5.0, ng.PrecisionShape);
Assert.AreEqual(precInvScale, ng.PrecisionInverseScale);
}
/// <summary>
/// Can get precision inverse scale.
/// </summary>
@ -206,27 +142,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
Assert.AreEqual(precInvScale, ng.PrecisionInverseScale);
}
/// <summary>
/// Can set precision inverse scale.
/// </summary>
/// <param name="meanLocation">Mean location.</param>
/// <param name="meanScale">Mean scale.</param>
/// <param name="precShape">Precision shape.</param>
/// <param name="precInvScale">Precision inverse scale.</param>
[TestCase(0.0, 1.0, 1.0, 1.0)]
[TestCase(10.0, 2.0, 2.0, 2.0)]
public void CanSetPrecisionPrecisionInverseScale(double meanLocation, double meanScale, double precShape, double precInvScale)
{
var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale)
{
PrecisionInverseScale = 5.0
};
Assert.AreEqual(meanLocation, ng.MeanLocation);
Assert.AreEqual(meanScale, ng.MeanScale);
Assert.AreEqual(precShape, ng.PrecisionShape);
Assert.AreEqual(5.0, ng.PrecisionInverseScale);
}
/// <summary>
/// Can get mean marginals.
/// </summary>

27
src/UnitTests/DistributionTests/Multivariate/WishartTests.cs

@ -146,21 +146,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
Assert.AreEqual(nu, d.DegreesOfFreedom);
}
/// <summary>
/// Can set DegreesOfFreedom.
/// </summary>
/// <param name="nu">DegreesOfFreedom parameter.</param>
[TestCase(1.0)]
[TestCase(2.0)]
[TestCase(5.0)]
public void CanSetNu(double nu)
{
GC.KeepAlive(new Wishart(1.0, Matrix<double>.Build.RandomPositiveDefinite(2, 1))
{
DegreesOfFreedom = nu
});
}
/// <summary>
/// Can get scale matrix.
/// </summary>
@ -180,18 +165,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
}
}
/// <summary>
/// Can set scale matrix.
/// </summary>
[Test]
public void CanSetS()
{
GC.KeepAlive(new Wishart(1.0, Matrix<double>.Build.RandomPositiveDefinite(2, 1))
{
Scale = Matrix<double>.Build.RandomPositiveDefinite(2, 1)
});
}
/// <summary>
/// Validate mean.
/// </summary>

6
src/UnitTests/InterpolationTests/CubicSplineTest.cs

@ -30,7 +30,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using MathNet.Numerics.Interpolation;
using NUnit.Framework;
@ -177,6 +176,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
}
}
#if !NET35 && !PORTABLE
[Test]
public void InterpolateAkimaSorted_MustBeThreadSafe_GitHub219([Values(8, 32, 256, 1024)] int samples)
{
@ -188,7 +188,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
}
var yipol = new double[samples];
Parallel.For(0, samples, i =>
System.Threading.Tasks.Parallel.For(0, samples, i =>
{
var spline = CubicSpline.InterpolateAkimaSorted(x, y[i]);
yipol[i] = spline.Interpolate(1.0);
@ -196,5 +196,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests
CollectionAssert.DoesNotContain(yipol, Double.NaN);
}
#endif
}
}

Loading…
Cancel
Save