Browse Source

Distributions: parameter clarification & unicode tweaks

pull/163/head
Christoph Ruegg 13 years ago
parent
commit
87aed9dad2
  1. 6
      src/Examples/ContinuousDistributions/ContinuousUniformDistribution.cs
  2. 6
      src/Examples/ContinuousDistributions/ExponentialDistribution.cs
  3. 16
      src/Numerics/Distributions/Bernoulli.cs
  4. 54
      src/Numerics/Distributions/Beta.cs
  5. 31
      src/Numerics/Distributions/Binomial.cs
  6. 19
      src/Numerics/Distributions/Categorical.cs
  7. 77
      src/Numerics/Distributions/Cauchy.cs
  8. 45
      src/Numerics/Distributions/Chi.cs
  9. 49
      src/Numerics/Distributions/ChiSquare.cs
  10. 29
      src/Numerics/Distributions/ContinuousUniform.cs
  11. 75
      src/Numerics/Distributions/ConwayMaxwellPoisson.cs
  12. 21
      src/Numerics/Distributions/Dirichlet.cs
  13. 25
      src/Numerics/Distributions/DiscreteUniform.cs
  14. 140
      src/Numerics/Distributions/Erlang.cs
  15. 90
      src/Numerics/Distributions/Exponential.cs
  16. 65
      src/Numerics/Distributions/FisherSnedecor.cs
  17. 188
      src/Numerics/Distributions/Gamma.cs
  18. 17
      src/Numerics/Distributions/Geometric.cs
  19. 11
      src/Numerics/Distributions/Hypergeometric.cs
  20. 54
      src/Numerics/Distributions/InverseGamma.cs
  21. 115
      src/Numerics/Distributions/InverseWishart.cs
  22. 78
      src/Numerics/Distributions/Laplace.cs
  23. 50
      src/Numerics/Distributions/LogNormal.cs
  24. 18
      src/Numerics/Distributions/MatrixNormal.cs
  25. 20
      src/Numerics/Distributions/Multinomial.cs
  26. 40
      src/Numerics/Distributions/NegativeBinomial.cs
  27. 127
      src/Numerics/Distributions/Normal.cs
  28. 18
      src/Numerics/Distributions/NormalGamma.cs
  29. 52
      src/Numerics/Distributions/Pareto.cs
  30. 18
      src/Numerics/Distributions/Poisson.cs
  31. 32
      src/Numerics/Distributions/Rayleigh.cs
  32. 100
      src/Numerics/Distributions/Stable.cs
  33. 18
      src/Numerics/Distributions/StudentT.cs
  34. 54
      src/Numerics/Distributions/Weibull.cs
  35. 108
      src/Numerics/Distributions/Wishart.cs
  36. 18
      src/Numerics/Distributions/Zipf.cs
  37. 6
      src/UnitTests/DistributionTests/Continuous/BetaTests.cs
  38. 6
      src/UnitTests/DistributionTests/Continuous/CauchyTests.cs
  39. 16
      src/UnitTests/DistributionTests/Continuous/ContinuousUniformTests.cs
  40. 16
      src/UnitTests/DistributionTests/Continuous/ErlangTests.cs
  41. 12
      src/UnitTests/DistributionTests/Continuous/ExponentialTests.cs
  42. 16
      src/UnitTests/DistributionTests/Continuous/GammaTests.cs
  43. 6
      src/UnitTests/DistributionTests/Continuous/InverseGammaTests.cs
  44. 6
      src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs
  45. 6
      src/UnitTests/DistributionTests/Continuous/LogNormalTests.cs
  46. 6
      src/UnitTests/DistributionTests/Continuous/NormalTests.cs
  47. 6
      src/UnitTests/DistributionTests/Continuous/ParetoTests.cs
  48. 6
      src/UnitTests/DistributionTests/Continuous/RayleighTests.cs
  49. 6
      src/UnitTests/DistributionTests/Continuous/StableTests.cs
  50. 6
      src/UnitTests/DistributionTests/Continuous/WeibullTests.cs
  51. 24
      src/UnitTests/DistributionTests/Discrete/ConwayMaxwellPoissonTests.cs
  52. 60
      src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs
  53. 2
      src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs
  54. 58
      src/UnitTests/DistributionTests/Multivariate/WishartTests.cs

6
src/Examples/ContinuousDistributions/ContinuousUniformDistribution.cs

@ -65,7 +65,7 @@ namespace Examples.ContinuousDistributionsExamples
{
// 1. Initialize the new instance of the ContinuousUniform distribution class with default parameters.
var continuousUniform = new ContinuousUniform();
Console.WriteLine(@"1. Initialize the new instance of the ContinuousUniform distribution class with parameters Lower = {0}, Upper = {1}", continuousUniform.Lower, continuousUniform.Upper);
Console.WriteLine(@"1. Initialize the new instance of the ContinuousUniform distribution class with parameters Lower = {0}, Upper = {1}", continuousUniform.LowerBound, continuousUniform.UpperBound);
Console.WriteLine();
// 2. Distributuion properties:
@ -131,8 +131,8 @@ namespace Examples.ContinuousDistributionsExamples
// 5. Generate 100000 samples of the ContinuousUniform(2, 10) distribution and display histogram
Console.WriteLine(@"5. Generate 100000 samples of the ContinuousUniform(2, 10) distribution and display histogram");
continuousUniform.Upper = 10;
continuousUniform.Lower = 2;
continuousUniform.UpperBound = 10;
continuousUniform.LowerBound = 2;
for (var i = 0; i < data.Length; i++)
{
data[i] = continuousUniform.Sample();

6
src/Examples/ContinuousDistributions/ExponentialDistribution.cs

@ -65,7 +65,7 @@ namespace Examples.ContinuousDistributionsExamples
{
// 1. Initialize the new instance of the Exponential distribution class with parameter Lambda = 1.
var exponential = new Exponential(1);
Console.WriteLine(@"1. Initialize the new instance of the Exponential distribution class with parameter Lambda = {0}", exponential.Lambda);
Console.WriteLine(@"1. Initialize the new instance of the Exponential distribution class with parameter Lambda = {0}", exponential.Rate);
Console.WriteLine();
// 2. Distributuion properties:
@ -131,7 +131,7 @@ namespace Examples.ContinuousDistributionsExamples
// 5. Generate 100000 samples of the Exponential(9) distribution and display histogram
Console.WriteLine(@"5. Generate 100000 samples of the Exponential(9) distribution and display histogram");
exponential.Lambda = 9;
exponential.Rate = 9;
for (var i = 0; i < data.Length; i++)
{
data[i] = exponential.Sample();
@ -142,7 +142,7 @@ namespace Examples.ContinuousDistributionsExamples
// 6. Generate 100000 samples of the Exponential(0.01) distribution and display histogram
Console.WriteLine(@"6. Generate 100000 samples of the Exponential(0.01) distribution and display histogram");
exponential.Lambda = 0.01;
exponential.Rate = 0.01;
for (var i = 0; i < data.Length; i++)
{
data[i] = exponential.Sample();

16
src/Numerics/Distributions/Bernoulli.cs

@ -109,21 +109,21 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the probability of generating a one.
/// </summary>
public System.Random RandomSource
public double P
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _p; }
set { SetParameters(value); }
}
/// <summary>
/// Gets or sets the probability of generating a one.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double P
public System.Random RandomSource
{
get { return _p; }
set { SetParameters(value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>

54
src/Numerics/Distributions/Beta.cs

@ -1,4 +1,4 @@
// <copyright file="Beta.cs" company="Math.NET">
// <copyright file="Beta.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -61,8 +61,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the Beta class.
/// </summary>
/// <param name="a">The a shape parameter of the Beta distribution.</param>
/// <param name="b">The b shape parameter of the Beta distribution.</param>
/// <param name="a">The α shape parameter of the Beta distribution.</param>
/// <param name="b">The β shape parameter of the Beta distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">If any of the Beta parameters are negative.</exception>
public Beta(double a, double b)
{
@ -73,8 +73,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the Beta class.
/// </summary>
/// <param name="a">The a shape parameter of the Beta distribution.</param>
/// <param name="b">The b shape parameter of the Beta distribution.</param>
/// <param name="a">The α shape parameter of the Beta distribution.</param>
/// <param name="b">The β shape parameter of the Beta distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentOutOfRangeException">If any of the Beta parameters are negative.</exception>
public Beta(double a, double b, System.Random randomSource)
@ -89,14 +89,14 @@ namespace MathNet.Numerics.Distributions
/// <returns>A string representation of the Beta distribution.</returns>
public override string ToString()
{
return "Beta(A = " + _shapeA + ", B = " + _shapeB + ")";
return "Beta(α = " + _shapeA + ", β = " + _shapeB + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="a">The a shape parameter of the Beta distribution.</param>
/// <param name="b">The b shape parameter of the Beta distribution.</param>
/// <param name="a">The α shape parameter of the Beta distribution.</param>
/// <param name="b">The β shape parameter of the Beta distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double a, double b)
{
@ -106,8 +106,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="a">The a shape parameter of the Beta distribution.</param>
/// <param name="b">The b shape parameter of the Beta distribution.</param>
/// <param name="a">The α shape parameter of the Beta distribution.</param>
/// <param name="b">The β shape parameter of the Beta distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double a, double b)
{
@ -121,16 +121,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the A shape parameter of the Beta distribution.
/// Gets or sets the α shape parameter of the Beta distribution.
/// </summary>
public double A
{
@ -139,7 +130,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the B shape parameter of the Beta distribution.
/// Gets or sets the β shape parameter of the Beta distribution.
/// </summary>
public double B
{
@ -147,6 +138,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shapeA, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the Beta distribution.
/// </summary>
@ -524,8 +524,8 @@ namespace MathNet.Numerics.Distributions
/// Samples Beta distributed random variables by sampling two Gamma variables and normalizing.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="a">The A shape parameter.</param>
/// <param name="b">The B shape parameter.</param>
/// <param name="a">The α shape parameter of the Beta distribution.</param>
/// <param name="b">The β shape parameter of the Beta distribution.</param>
/// <returns>a random number from the Beta distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double a, double b)
{
@ -559,8 +559,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="a">The a shape parameter of the Beta distribution.</param>
/// <param name="b">The b shape parameter of the Beta distribution.</param>
/// <param name="a">The α shape parameter of the Beta distribution.</param>
/// <param name="b">The β shape parameter of the Beta distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double a, double b)
{
@ -576,8 +576,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="a">The a shape parameter of the Beta distribution.</param>
/// <param name="b">The b shape parameter of the Beta distribution.</param>
/// <param name="a">The α shape parameter of the Beta distribution.</param>
/// <param name="b">The β shape parameter of the Beta distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double a, double b)
{

31
src/Numerics/Distributions/Binomial.cs

@ -49,20 +49,13 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// Success probability in each trial.
/// </summary>
double _p;
/// <summary>
/// The number of trials.
/// </summary>
int _trials;
/// <summary>
/// Initializes a new instance of the Binomial class.
/// </summary>
/// <param name="p">The success probability of a trial.</param>
/// <param name="p">The success probability in each trial.</param>
/// <param name="n">The number of trials.</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>
@ -75,7 +68,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the Binomial class.
/// </summary>
/// <param name="p">The success probability of a trial.</param>
/// <param name="p">The success probability in each trial.</param>
/// <param name="n">The number of trials.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="p"/> is not in the interval [0.0,1.0].</exception>
@ -125,16 +118,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the success probability.
/// Gets or sets the success probability in each trial.
/// </summary>
public double P
{
@ -151,6 +135,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_p, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

19
src/Numerics/Distributions/Categorical.cs

@ -192,23 +192,22 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the probability mass vector (non-negative ratios) of the multinomial.
/// </summary>
public System.Random RandomSource
/// <remarks>Sometimes the normalized probability vector cannot be represented exactly in a floating point representation.</remarks>
public double[] P
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return (double[])_pmfNormalized.Clone(); }
set { SetParameters(value); }
}
/// <summary>
/// Gets or sets the normalized probability vector of the multinomial.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
/// <remarks>Sometimes the normalized probability vector cannot be represented
/// exactly in a floating point representation.</remarks>
public double[] P
public System.Random RandomSource
{
get { return (double[]) _pmfNormalized.Clone(); }
set { SetParameters(value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>

77
src/Numerics/Distributions/Cauchy.cs

@ -33,7 +33,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Continuous Univariate Cauchy distribution.
/// The Cauchy distribution is a symmetric continuous probability distribution. For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/cauchy_distribution">Wikipedia - Cauchy distribution</a>.
/// <a href="http://en.wikipedia.org/wiki/Cauchy_distribution">Wikipedia - Cauchy distribution</a>.
/// </summary>
/// <remarks><para>The distribution will use the <see cref="System.Random"/> by default.
/// Users can get/set the random number generator by using the <see cref="RandomSource"/> property.</para>
@ -44,6 +44,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _location;
double _scale;
/// <summary>
@ -56,8 +57,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Cauchy"/> class.
/// </summary>
/// <param name="location">The location parameter for the distribution.</param>
/// <param name="scale">The scale parameter for the distribution.</param>
/// <param name="location">The location (x0) of the distribution.</param>
/// <param name="scale">The scale (γ) of the distribution.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Cauchy(double location, double scale)
{
@ -68,8 +69,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Cauchy"/> class.
/// </summary>
/// <param name="location">The location parameter for the distribution.</param>
/// <param name="scale">The scale parameter for the distribution.</param>
/// <param name="location">The location (x0) of the distribution.</param>
/// <param name="scale">The scale (γ) of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Cauchy(double location, double scale, System.Random randomSource)
@ -84,14 +85,14 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Cauchy(Location = " + Median + ", Scale = " + _scale + ")";
return "Cauchy(x0 = " + _location + ", γ = " + _scale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="location">Location parameter.</param>
/// <param name="scale">Scale parameter. Must be greater than 0.</param>
/// <param name="location">The location (x0) of the distribution.</param>
/// <param name="scale">The scale (γ) of the distribution. Must be greater than 0.</param>
/// <returns>True when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double location, double scale)
{
@ -101,8 +102,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="location">Location parameter.</param>
/// <param name="scale">Scale parameter. Must be greater than 0.</param>
/// <param name="location">The location (x0) of the distribution.</param>
/// <param name="scale">The scale (γ) of the distribution. Must be greater than 0.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double location, double scale)
{
@ -111,37 +112,36 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Median = location;
_location = location;
_scale = scale;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the location parameter of the distribution.
/// Gets or sets the location (x0) of the distribution.
/// </summary>
public double Location
{
get { return Median; }
get { return _location; }
set { SetParameters(value, _scale); }
}
/// <summary>
/// Gets or sets the scale parameter of the distribution.
/// Gets or sets the scale (γ) of the distribution.
/// </summary>
public double Scale
{
get { return _scale; }
set { SetParameters(Median, value); }
set { SetParameters(_location, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
@ -188,13 +188,16 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mode
{
get { return Median; }
get { return _location; }
}
/// <summary>
/// Gets the median of the distribution.
/// </summary>
public double Median { get; private set; }
public double Median
{
get { return _location; }
}
/// <summary>
/// Gets the minimum of the distribution.
@ -219,7 +222,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return 1.0/(Constants.Pi*_scale*(1.0 + (((x - Median)/_scale)*((x - Median)/_scale))));
return 1.0/(Constants.Pi*_scale*(1.0 + (((x - _location)/_scale)*((x - _location)/_scale))));
}
/// <summary>
@ -229,7 +232,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
return -Math.Log(Constants.Pi*_scale*(1.0 + (((x - Median)/_scale)*((x - Median)/_scale))));
return -Math.Log(Constants.Pi*_scale*(1.0 + (((x - _location)/_scale)*((x - _location)/_scale))));
}
/// <summary>
@ -239,15 +242,15 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return ((1.0/Constants.Pi)*Math.Atan((x - Median)/_scale)) + 0.5;
return ((1.0/Constants.Pi)*Math.Atan((x - _location)/_scale)) + 0.5;
}
/// <summary>
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="location">The location (x0) of the distribution.</param>
/// <param name="scale">The scale (γ) of the distribution.</param>
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double location, double scale)
{
@ -261,7 +264,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, Median, _scale);
return SampleUnchecked(RandomSource, _location, _scale);
}
/// <summary>
@ -272,7 +275,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, Median, _scale);
yield return SampleUnchecked(RandomSource, _location, _scale);
}
}
@ -280,8 +283,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="location">The location (x0) of the distribution.</param>
/// <param name="scale">The scale (γ) of the distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double location, double scale)
{
@ -297,8 +300,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="location">The location (x0) of the distribution.</param>
/// <param name="scale">The scale (γ) of the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double location, double scale)
{

45
src/Numerics/Distributions/Chi.cs

@ -50,10 +50,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// Keeps track of the degrees of freedom for the Chi distribution.
/// </summary>
double _dof;
double _freedom;
/// <summary>
/// Initializes a new instance of the <see cref="Chi"/> class.
@ -82,7 +79,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Chi(DoF = " + _dof + ")";
return "Chi(DoF = " + _freedom + ")";
}
/// <summary>
@ -107,25 +104,25 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_dof = dof;
_freedom = dof;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the degrees of freedom of the Chi distribution.
/// </summary>
public System.Random RandomSource
public double DegreesOfFreedom
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _freedom; }
set { SetParameters(value); }
}
/// <summary>
/// Gets or sets the degrees of freedom of the Chi distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double DegreesOfFreedom
public System.Random RandomSource
{
get { return _dof; }
set { SetParameters(value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -133,7 +130,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return Constants.Sqrt2*(SpecialFunctions.Gamma((_dof + 1.0)/2.0)/SpecialFunctions.Gamma(_dof/2.0)); }
get { return Constants.Sqrt2*(SpecialFunctions.Gamma((_freedom + 1.0)/2.0)/SpecialFunctions.Gamma(_freedom/2.0)); }
}
/// <summary>
@ -141,7 +138,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return _dof - (Mean*Mean); }
get { return _freedom - (Mean*Mean); }
}
/// <summary>
@ -157,7 +154,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return SpecialFunctions.GammaLn(_dof/2.0) + ((_dof - Math.Log(2) - ((_dof - 1.0)*SpecialFunctions.DiGamma(_dof/2.0)))/2.0); }
get { return SpecialFunctions.GammaLn(_freedom/2.0) + ((_freedom - Math.Log(2) - ((_freedom - 1.0)*SpecialFunctions.DiGamma(_freedom/2.0)))/2.0); }
}
/// <summary>
@ -179,12 +176,12 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (_dof < 1)
if (_freedom < 1)
{
throw new NotSupportedException();
}
return Math.Sqrt(_dof - 1.0);
return Math.Sqrt(_freedom - 1.0);
}
}
@ -219,7 +216,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return (Math.Pow(2.0, 1.0 - (_dof/2.0))*Math.Pow(x, _dof - 1.0)*Math.Exp(-x*x/2.0))/SpecialFunctions.Gamma(_dof/2.0);
return (Math.Pow(2.0, 1.0 - (_freedom/2.0))*Math.Pow(x, _freedom - 1.0)*Math.Exp(-x*x/2.0))/SpecialFunctions.Gamma(_freedom/2.0);
}
/// <summary>
@ -229,7 +226,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
return ((1.0 - (_dof/2.0))*Math.Log(2.0)) + ((_dof - 1.0)*Math.Log(x)) - (x*x/2.0) - SpecialFunctions.GammaLn(_dof/2.0);
return ((1.0 - (_freedom/2.0))*Math.Log(2.0)) + ((_freedom - 1.0)*Math.Log(x)) - (x*x/2.0) - SpecialFunctions.GammaLn(_freedom/2.0);
}
/// <summary>
@ -239,7 +236,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaLowerIncomplete(_dof/2.0, x*x/2.0)/SpecialFunctions.Gamma(_dof/2.0);
return SpecialFunctions.GammaLowerIncomplete(_freedom/2.0, x*x/2.0)/SpecialFunctions.Gamma(_freedom/2.0);
}
/// <summary>
@ -265,7 +262,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, (int) _dof);
return SampleUnchecked(RandomSource, (int) _freedom);
}
/// <summary>
@ -274,7 +271,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public IEnumerable<double> Samples()
{
var dof = (int) _dof;
var dof = (int) _freedom;
while (true)
{
yield return SampleUnchecked(RandomSource, dof);

49
src/Numerics/Distributions/ChiSquare.cs

@ -48,6 +48,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _freedom;
/// <summary>
/// Initializes a new instance of the <see cref="ChiSquare"/> class.
/// </summary>
@ -75,7 +77,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "ChiSquare(DoF = " + Mean + ")";
return "ChiSquare(DoF = " + _freedom + ")";
}
/// <summary>
@ -100,38 +102,41 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Mean = dof;
_freedom = dof;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the degrees of freedom of the <c>ChiSquare</c> distribution.
/// </summary>
public System.Random RandomSource
public double DegreesOfFreedom
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _freedom; }
set { SetParameters(value); }
}
/// <summary>
/// Gets or sets the degrees of freedom of the <c>ChiSquare</c> distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double DegreesOfFreedom
public System.Random RandomSource
{
get { return Mean; }
set { SetParameters(value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
public double Mean { get; private set; }
public double Mean
{
get { return _freedom; }
}
/// <summary>
/// Gets the variance of the distribution.
/// </summary>
public double Variance
{
get { return 2.0*Mean; }
get { return 2.0*_freedom; }
}
/// <summary>
@ -139,7 +144,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return Math.Sqrt(2.0*Mean); }
get { return Math.Sqrt(2.0 * _freedom); }
}
/// <summary>
@ -147,7 +152,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return (Mean/2.0) + Math.Log(2.0*SpecialFunctions.Gamma(Mean/2.0)) + ((1.0 - (Mean/2.0))*SpecialFunctions.DiGamma(Mean/2.0)); }
get { return (_freedom/2.0) + Math.Log(2.0*SpecialFunctions.Gamma(_freedom/2.0)) + ((1.0 - (_freedom/2.0))*SpecialFunctions.DiGamma(_freedom/2.0)); }
}
/// <summary>
@ -155,7 +160,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Skewness
{
get { return Math.Sqrt(8.0/Mean); }
get { return Math.Sqrt(8.0 / _freedom); }
}
/// <summary>
@ -163,7 +168,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mode
{
get { return Mean - 2.0; }
get { return _freedom - 2.0; }
}
/// <summary>
@ -171,7 +176,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Median
{
get { return Mean - (2.0/3.0); }
get { return _freedom - (2.0 / 3.0); }
}
/// <summary>
@ -197,7 +202,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return (Math.Pow(x, (Mean/2.0) - 1.0)*Math.Exp(-x/2.0))/(Math.Pow(2.0, Mean/2.0)*SpecialFunctions.Gamma(Mean/2.0));
return (Math.Pow(x, (_freedom / 2.0) - 1.0) * Math.Exp(-x / 2.0)) / (Math.Pow(2.0, _freedom / 2.0) * SpecialFunctions.Gamma(_freedom / 2.0));
}
/// <summary>
@ -207,7 +212,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
return (-x/2.0) + (((Mean/2.0) - 1.0)*Math.Log(x)) - ((Mean/2.0)*Math.Log(2)) - SpecialFunctions.GammaLn(Mean/2.0);
return (-x / 2.0) + (((_freedom / 2.0) - 1.0) * Math.Log(x)) - ((_freedom / 2.0) * Math.Log(2)) - SpecialFunctions.GammaLn(_freedom / 2.0);
}
/// <summary>
@ -217,7 +222,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaLowerIncomplete(Mean/2.0, x/2.0)/SpecialFunctions.Gamma(Mean/2.0);
return SpecialFunctions.GammaLowerIncomplete(_freedom / 2.0, x / 2.0) / SpecialFunctions.Gamma(_freedom / 2.0);
}
/// <summary>
@ -250,7 +255,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, Mean);
return SampleUnchecked(RandomSource, _freedom);
}
/// <summary>
@ -261,7 +266,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, Mean);
yield return SampleUnchecked(RandomSource, _freedom);
}
}

29
src/Numerics/Distributions/ContinuousUniform.cs

@ -48,14 +48,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// The distribution's lower bound.
/// </summary>
double _lower;
/// <summary>
/// The distribution's upper bound.
/// </summary>
double _upper;
/// <summary>
@ -127,19 +120,10 @@ namespace MathNet.Numerics.Distributions
_upper = upper;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the lower bound of the distribution.
/// </summary>
public double Lower
public double LowerBound
{
get { return _lower; }
set { SetParameters(value, _upper); }
@ -148,12 +132,21 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets or sets the upper bound of the distribution.
/// </summary>
public double Upper
public double UpperBound
{
get { return _upper; }
set { SetParameters(_lower, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

75
src/Numerics/Distributions/ConwayMaxwellPoisson.cs

@ -55,11 +55,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// Since many properties of the distribution can only be computed approximately, the tolerance
/// level specifies how much error we accept.
/// </summary>
const double Tolerance = 1e-12;
double _lambda;
double _nu;
/// <summary>
/// The mean of the distribution.
@ -77,20 +74,16 @@ namespace MathNet.Numerics.Distributions
double _z = double.MinValue;
/// <summary>
/// The lambda parameter.
/// </summary>
double _lambda;
/// <summary>
/// The nu parameter.
/// Since many properties of the distribution can only be computed approximately, the tolerance
/// level specifies how much error we accept.
/// </summary>
double _nu;
const double Tolerance = 1e-12;
/// <summary>
/// Initializes a new instance of the <see cref="ConwayMaxwellPoisson"/> class.
/// </summary>
/// <param name="lambda">The lambda parameter.</param>
/// <param name="nu">The nu parameter.</param>
/// <param name="lambda">The lambda (λ) parameter.</param>
/// <param name="nu">The nu (ν) parameter.</param>
public ConwayMaxwellPoisson(double lambda, double nu)
{
_random = new System.Random();
@ -100,8 +93,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="ConwayMaxwellPoisson"/> class.
/// </summary>
/// <param name="lambda">The lambda parameter.</param>
/// <param name="nu">The nu parameter.</param>
/// <param name="lambda">The lambda (λ) parameter.</param>
/// <param name="nu">The nu (ν) parameter.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public ConwayMaxwellPoisson(double lambda, double nu, System.Random randomSource)
{
@ -112,19 +105,17 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
public override string ToString()
{
return "ConwayMaxwellPoisson(Lambda = " + _lambda + ", Nu = " + _nu + ")";
return "ConwayMaxwellPoisson(λ = " + _lambda + ", ν = " + _nu + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="lambda">The lambda parameter.</param>
/// <param name="nu">The nu parameter.</param>
/// <param name="lambda">The lambda (λ) parameter.</param>
/// <param name="nu">The nu (ν) parameter.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double lambda, double nu)
{
@ -134,8 +125,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="lambda">The lambda parameter.</param>
/// <param name="nu">The nu parameter.</param>
/// <param name="lambda">The lambda (λ) parameter.</param>
/// <param name="nu">The nu (ν) parameter.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double lambda, double nu)
{
@ -149,16 +140,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the lambda parameter.
/// Gets or sets the lambda (λ) parameter.
/// </summary>
/// <value>The value of the lambda parameter.</value>
public double Lambda
@ -168,15 +150,24 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the Nu parameter.
/// Gets or sets the DegreeOfFreedom (ν) parameter.
/// </summary>
/// <value>The value of the Nu parameter.</value>
/// <value>The value of the DegreeOfFreedom parameter.</value>
public double Nu
{
get { return _nu; }
set { SetParameters(_lambda, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
@ -454,8 +445,8 @@ namespace MathNet.Numerics.Distributions
/// Returns one trials from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter</param>
/// <param name="nu">The nu parameter.</param>
/// <param name="lambda">The lambda (λ) parameter.</param>
/// <param name="nu">The nu (ν) parameter.</param>
/// <param name="z">The z parameter.</param>
/// <returns>
/// One sample from the distribution implied by <paramref name="lambda"/>, <paramref name="nu"/>, and <paramref name="z"/>.
@ -504,8 +495,8 @@ namespace MathNet.Numerics.Distributions
/// Samples a random variable.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter</param>
/// <param name="nu">The nu parameter.</param>
/// <param name="lambda">The lambda (λ) parameter.</param>
/// <param name="nu">The nu (ν) parameter.</param>
public static int Sample(System.Random rnd, double lambda, double nu)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda, nu))
@ -521,8 +512,8 @@ namespace MathNet.Numerics.Distributions
/// Samples a sequence of this random variable.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter</param>
/// <param name="nu">The nu parameter.</param>
/// <param name="lambda">The lambda (λ) parameter.</param>
/// <param name="nu">The nu (ν) parameter.</param>
public static IEnumerable<int> Samples(System.Random rnd, double lambda, double nu)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda, nu))

21
src/Numerics/Distributions/Dirichlet.cs

@ -47,9 +47,6 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// The Dirichlet distribution parameters.
/// </summary>
double[] _alpha;
/// <summary>
@ -167,6 +164,15 @@ namespace MathNet.Numerics.Distributions
_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>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
@ -184,15 +190,6 @@ namespace MathNet.Numerics.Distributions
get { return _alpha.Length; }
}
/// <summary>
/// Gets or sets the parameters of the Dirichlet distribution.
/// </summary>
public double[] Alpha
{
get { return _alpha; }
set { SetParameters(value); }
}
/// <summary>
/// Gets the sum of the Dirichlet parameters.
/// </summary>

25
src/Numerics/Distributions/DiscreteUniform.cs

@ -49,14 +49,7 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// The distribution's lower bound.
/// </summary>
int _lower;
/// <summary>
/// The distribution's upper bound.
/// </summary>
int _upper;
/// <summary>
@ -121,15 +114,6 @@ namespace MathNet.Numerics.Distributions
_upper = upper;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the lower bound of the probability distribution.
/// </summary>
@ -148,6 +132,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lower, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

140
src/Numerics/Distributions/Erlang.cs

@ -50,37 +50,37 @@ namespace MathNet.Numerics.Distributions
System.Random _random;
double _shape;
double _invScale;
double _rate;
/// <summary>
/// Initializes a new instance of the <see cref="Erlang"/> class.
/// </summary>
/// <param name="shape">The shape of the Erlang distribution.</param>
/// <param name="invScale">The inverse scale of the Erlang distribution.</param>
public Erlang(int shape, double invScale)
/// <param name="shape">The shape (k) of the Erlang distribution.</param>
/// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution.</param>
public Erlang(int shape, double rate)
{
_random = new System.Random();
SetParameters(shape, invScale);
SetParameters(shape, rate);
}
/// <summary>
/// Initializes a new instance of the <see cref="Erlang"/> class.
/// </summary>
/// <param name="shape">The shape of the Erlang distribution.</param>
/// <param name="invScale">The inverse scale of the Erlang distribution.</param>
/// <param name="shape">The shape (k) of the Erlang distribution.</param>
/// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Erlang(int shape, double invScale, System.Random randomSource)
public Erlang(int shape, double rate, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(shape, invScale);
SetParameters(shape, rate);
}
/// <summary>
/// Constructs a Erlang distribution from a shape and scale parameter. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="shape">The shape of the Erlang distribution.</param>
/// <param name="scale">The scale of the Erlang distribution.</param>
/// <param name="shape">The shape (k) of the Erlang distribution.</param>
/// <param name="scale">The scale (mu) of the Erlang distribution.</param>
/// <returns>a normal distribution.</returns>
public static Erlang WithShapeScale(int shape, double scale)
{
@ -91,12 +91,12 @@ namespace MathNet.Numerics.Distributions
/// Constructs a Erlang distribution from a shape and inverse scale parameter. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="shape">The shape of the Erlang distribution.</param>
/// <param name="invScale">The inverse scale of the Erlang distribution.</param>
/// <param name="shape">The shape (k) of the Erlang distribution.</param>
/// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution.</param>
/// <returns>a normal distribution.</returns>
public static Erlang WithShapeInvScale(int shape, double invScale)
public static Erlang WithShapeRate(int shape, double rate)
{
return new Erlang(shape, invScale);
return new Erlang(shape, rate);
}
/// <summary>
@ -105,52 +105,52 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Erlang(Shape = " + _shape + ", Inverse Scale = " + _invScale + ")";
return "Erlang(Shape = " + _shape + ", λ = " + _rate + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="shape">The shape of the Erlang distribution.</param>
/// <param name="invScale">The inverse scale of the Erlang distribution.</param>
/// <param name="shape">The shape (k) of the Erlang distribution.</param>
/// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double shape, double invScale)
static bool IsValidParameterSet(double shape, double rate)
{
return shape >= 0.0 && invScale >= 0.0;
return shape >= 0.0 && rate >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="shape">The shape of the Erlang distribution.</param>
/// <param name="invScale">The inverse scale of the Erlang distribution.</param>
void SetParameters(double shape, double invScale)
/// <param name="shape">The shape (k) of the Erlang distribution.</param>
/// <param name="rate">The rate or inverse scale (λ) of the Erlang distribution.</param>
void SetParameters(double shape, double rate)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale))
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, rate))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_shape = shape;
_invScale = invScale;
_rate = rate;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the shape (k) of the Erlang distribution.
/// </summary>
public System.Random RandomSource
public int Shape
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return (int)_shape; }
set { SetParameters(value, _rate); }
}
/// <summary>
/// Gets or sets the shape of the Erlang distribution.
/// Gets or sets the rate or inverse scale (λ) of the Erlang distribution.
/// </summary>
public int Shape
public double Rate
{
get { return (int) _shape; }
set { SetParameters(value, _invScale); }
get { return _rate; }
set { SetParameters(_shape, value); }
}
/// <summary>
@ -158,27 +158,25 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Scale
{
get { return 1.0/_invScale; }
get { return 1.0 / _rate; }
set
{
var invScale = 1.0/value;
var invScale = 1.0 / value;
if (Double.IsNegativeInfinity(invScale))
{
invScale = -invScale;
}
SetParameters(_shape, invScale);
}
}
/// <summary>
/// Gets or sets the inverse scale of the Erlang distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double InvScale
public System.Random RandomSource
{
get { return _invScale; }
set { SetParameters(_shape, value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -188,17 +186,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return _shape;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return _shape/_invScale;
return _shape/_rate;
}
}
@ -209,17 +207,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return 0.0;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return _shape/(_invScale*_invScale);
return _shape/(_rate*_rate);
}
}
@ -230,17 +228,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return 0.0;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return Math.Sqrt(_shape)/_invScale;
return Math.Sqrt(_shape)/_rate;
}
}
@ -251,17 +249,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return 0.0;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return _shape - Math.Log(_invScale) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape)*SpecialFunctions.DiGamma(_shape));
return _shape - Math.Log(_rate) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape)*SpecialFunctions.DiGamma(_shape));
}
}
@ -272,12 +270,12 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return 0.0;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
@ -298,17 +296,17 @@ namespace MathNet.Numerics.Distributions
throw new NotSupportedException();
}
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return _shape;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return (_shape - 1.0)/_invScale;
return (_shape - 1.0)/_rate;
}
}
@ -343,22 +341,22 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return x == _shape ? Double.PositiveInfinity : 0.0;
}
if (_shape == 0.0 && _invScale == 0.0)
if (_shape == 0.0 && _rate == 0.0)
{
return 0.0;
}
if (_shape == 1.0)
{
return _invScale*Math.Exp(-_invScale*x);
return _rate*Math.Exp(-_rate*x);
}
return Math.Pow(_invScale, _shape)*Math.Pow(x, _shape - 1.0)*Math.Exp(-_invScale*x)/SpecialFunctions.Gamma(_shape);
return Math.Pow(_rate, _shape)*Math.Pow(x, _shape - 1.0)*Math.Exp(-_rate*x)/SpecialFunctions.Gamma(_shape);
}
/// <summary>
@ -368,22 +366,22 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return x == _shape ? Double.PositiveInfinity : Double.NegativeInfinity;
}
if (_shape == 0.0 && _invScale == 0.0)
if (_shape == 0.0 && _rate == 0.0)
{
return Double.NegativeInfinity;
}
if (_shape == 1.0)
{
return Math.Log(_invScale) - (_invScale*x);
return Math.Log(_rate) - (_rate*x);
}
return (_shape*Math.Log(_invScale)) + ((_shape - 1.0)*Math.Log(x)) - (_invScale*x) - SpecialFunctions.GammaLn(_shape);
return (_shape*Math.Log(_rate)) + ((_shape - 1.0)*Math.Log(x)) - (_rate*x) - SpecialFunctions.GammaLn(_shape);
}
/// <summary>
@ -393,17 +391,17 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return x >= _shape ? 1.0 : 0.0;
}
if (_shape == 0.0 && _invScale == 0.0)
if (_shape == 0.0 && _rate == 0.0)
{
return 0.0;
}
return SpecialFunctions.GammaLowerRegularized(_shape, x*_invScale);
return SpecialFunctions.GammaLowerRegularized(_shape, x*_rate);
}
/// <summary>
@ -466,7 +464,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _shape, _invScale);
return SampleUnchecked(RandomSource, _shape, _rate);
}
/// <summary>
@ -477,7 +475,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _shape, _invScale);
yield return SampleUnchecked(RandomSource, _shape, _rate);
}
}

90
src/Numerics/Distributions/Exponential.cs

@ -37,7 +37,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Continuous Univariate Exponential distribution.
/// The exponential distribution is a distribution over the real numbers parameterized by one non-negative parameter.
/// <a href="http://en.wikipedia.org/wiki/exponential_distribution">Wikipedia - exponential distribution</a>.
/// <a href="http://en.wikipedia.org/wiki/Exponential_distribution">Wikipedia - exponential distribution</a>.
/// </summary>
/// <remarks>The distribution will use the <see cref="System.Random"/> by default.
/// <para>Users can set the random number generator by using the <see cref="RandomSource"/> property.</para>
@ -48,27 +48,27 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _lambda;
double _rate;
/// <summary>
/// Initializes a new instance of the <see cref="Exponential"/> class.
/// </summary>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
public Exponential(double lambda)
/// <param name="rate">The rate (λ) parameter of the Exponential distribution.</param>
public Exponential(double rate)
{
_random = new System.Random();
SetParameters(lambda);
SetParameters(rate);
}
/// <summary>
/// Initializes a new instance of the <see cref="Exponential"/> class.
/// </summary>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <param name="rate">The rate (λ) parameter of the Exponential distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Exponential(double lambda, System.Random randomSource)
public Exponential(double rate, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(lambda);
SetParameters(rate);
}
/// <summary>
@ -77,50 +77,50 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Exponential(Lambda = " + _lambda + ")";
return "Exponential(λ = " + _rate + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="lambda">Lambda parameter.</param>
/// <param name="rate">The rate (λ) parameter of the Exponential distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double lambda)
static bool IsValidParameterSet(double rate)
{
return lambda >= 0.0;
return rate >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="lambda">Lambda parameter.</param>
/// <param name="rate">The rate (λ) parameter of the Exponential distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double lambda)
void SetParameters(double rate)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
if (Control.CheckDistributionParameters && !IsValidParameterSet(rate))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_lambda = lambda;
_rate = rate;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the rate (λ) parameter of the distribution.
/// </summary>
public System.Random RandomSource
public double Rate
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _rate; }
set { SetParameters(value); }
}
/// <summary>
/// Gets or sets the lambda parameter of the distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double Lambda
public System.Random RandomSource
{
get { return _lambda; }
set { SetParameters(value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -128,7 +128,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mean
{
get { return 1.0/_lambda; }
get { return 1.0/_rate; }
}
/// <summary>
@ -136,7 +136,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Variance
{
get { return 1.0/(_lambda*_lambda); }
get { return 1.0/(_rate*_rate); }
}
/// <summary>
@ -144,7 +144,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double StdDev
{
get { return 1.0/_lambda; }
get { return 1.0/_rate; }
}
/// <summary>
@ -152,7 +152,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Entropy
{
get { return 1.0 - Math.Log(_lambda); }
get { return 1.0 - Math.Log(_rate); }
}
/// <summary>
@ -176,7 +176,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Median
{
get { return Math.Log(2.0)/_lambda; }
get { return Math.Log(2.0)/_rate; }
}
/// <summary>
@ -204,7 +204,7 @@ namespace MathNet.Numerics.Distributions
{
if (x >= 0.0)
{
return _lambda*Math.Exp(-_lambda*x);
return _rate*Math.Exp(-_rate*x);
}
return 0.0;
@ -217,7 +217,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
return Math.Log(_lambda) - (_lambda*x);
return Math.Log(_rate) - (_rate*x);
}
/// <summary>
@ -229,7 +229,7 @@ namespace MathNet.Numerics.Distributions
{
if (x >= 0.0)
{
return 1.0 - Math.Exp(-_lambda*x);
return 1.0 - Math.Exp(-_rate*x);
}
return 0.0;
@ -239,9 +239,9 @@ namespace MathNet.Numerics.Distributions
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <param name="rate">The rate (λ) parameter of the Exponential distribution.</param>
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double lambda)
internal static double SampleUnchecked(System.Random rnd, double rate)
{
var r = rnd.NextDouble();
while (r == 0.0)
@ -249,7 +249,7 @@ namespace MathNet.Numerics.Distributions
r = rnd.NextDouble();
}
return -Math.Log(r)/lambda;
return -Math.Log(r)/rate;
}
/// <summary>
@ -258,7 +258,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _lambda);
return SampleUnchecked(RandomSource, _rate);
}
/// <summary>
@ -269,7 +269,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _lambda);
yield return SampleUnchecked(RandomSource, _rate);
}
}
@ -277,34 +277,34 @@ namespace MathNet.Numerics.Distributions
/// Draws a random sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <param name="rate">The rate (λ) parameter of the Exponential distribution.</param>
/// <returns>A random number from this distribution.</returns>
public static double Sample(System.Random rnd, double lambda)
public static double Sample(System.Random rnd, double rate)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
if (Control.CheckDistributionParameters && !IsValidParameterSet(rate))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return SampleUnchecked(rnd, lambda);
return SampleUnchecked(rnd, rate);
}
/// <summary>
/// Generates a sequence of samples from the Exponential distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <param name="rate">The rate (λ) parameter of the Exponential distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double lambda)
public static IEnumerable<double> Samples(System.Random rnd, double rate)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
if (Control.CheckDistributionParameters && !IsValidParameterSet(rate))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return SampleUnchecked(rnd, lambda);
yield return SampleUnchecked(rnd, rate);
}
}
}

65
src/Numerics/Distributions/FisherSnedecor.cs

@ -48,15 +48,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// The first parameter - degree of freedom.
/// </summary>
double _d1;
/// <summary>
/// The second parameter - degree of freedom.
/// </summary>
double _d2;
double _freedom1;
double _freedom2;
/// <summary>
/// Initializes a new instance of the <see cref="FisherSnedecor"/> class.
@ -87,7 +80,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "FisherSnedecor(DegreeOfFreedom1 = " + _d1 + ", DegreeOfFreedom2 = " + _d2 + ")";
return "FisherSnedecor(DegreeOfFreedom1 = " + _freedom1 + ", DegreeOfFreedom2 = " + _freedom2 + ")";
}
/// <summary>
@ -113,35 +106,35 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_d1 = d1;
_d2 = d2;
_freedom1 = d1;
_freedom2 = d2;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the first parameter - degree of freedom.
/// </summary>
public System.Random RandomSource
public double DegreeOfFreedom1
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _freedom1; }
set { SetParameters(value, _freedom2); }
}
/// <summary>
/// Gets or sets the first parameter - degree of freedom.
/// Gets or sets the second parameter - degree of freedom.
/// </summary>
public double DegreeOfFreedom1
public double DegreeOfFreedom2
{
get { return _d1; }
set { SetParameters(value, _d2); }
get { return _freedom2; }
set { SetParameters(_freedom1, value); }
}
/// <summary>
/// Gets or sets the second parameter - degree of freedom.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double DegreeOfFreedom2
public System.Random RandomSource
{
get { return _d2; }
set { SetParameters(_d1, value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -151,12 +144,12 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (_d2 <= 2)
if (_freedom2 <= 2)
{
throw new NotSupportedException();
}
return _d2/(_d2 - 2.0);
return _freedom2/(_freedom2 - 2.0);
}
}
@ -167,12 +160,12 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (_d2 <= 4)
if (_freedom2 <= 4)
{
throw new NotSupportedException();
}
return (2.0*_d2*_d2*(_d1 + _d2 - 2.0))/(_d1*(_d2 - 2.0)*(_d2 - 2.0)*(_d2 - 4.0));
return (2.0*_freedom2*_freedom2*(_freedom1 + _freedom2 - 2.0))/(_freedom1*(_freedom2 - 2.0)*(_freedom2 - 2.0)*(_freedom2 - 4.0));
}
}
@ -199,12 +192,12 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (_d2 <= 6)
if (_freedom2 <= 6)
{
throw new NotSupportedException();
}
return (((2.0*_d1) + _d2 - 2.0)*Math.Sqrt(8.0*(_d2 - 4.0)))/((_d2 - 6.0)*Math.Sqrt(_d1*(_d1 + _d2 - 2.0)));
return (((2.0*_freedom1) + _freedom2 - 2.0)*Math.Sqrt(8.0*(_freedom2 - 4.0)))/((_freedom2 - 6.0)*Math.Sqrt(_freedom1*(_freedom1 + _freedom2 - 2.0)));
}
}
@ -215,12 +208,12 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (_d1 <= 2)
if (_freedom1 <= 2)
{
throw new NotSupportedException();
}
return (_d2*(_d1 - 2.0))/(_d1*(_d2 + 2.0));
return (_freedom2*(_freedom1 - 2.0))/(_freedom1*(_freedom2 + 2.0));
}
}
@ -255,7 +248,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return Math.Sqrt(Math.Pow(_d1*x, _d1)*Math.Pow(_d2, _d2)/Math.Pow((_d1*x) + _d2, _d1 + _d2))/(x*SpecialFunctions.Beta(_d1/2.0, _d2/2.0));
return Math.Sqrt(Math.Pow(_freedom1*x, _freedom1)*Math.Pow(_freedom2, _freedom2)/Math.Pow((_freedom1*x) + _freedom2, _freedom1 + _freedom2))/(x*SpecialFunctions.Beta(_freedom1/2.0, _freedom2/2.0));
}
/// <summary>
@ -275,7 +268,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.BetaRegularized(_d1/2.0, _d2/2.0, _d1*x/((_d1*x) + _d2));
return SpecialFunctions.BetaRegularized(_freedom1/2.0, _freedom2/2.0, _freedom1*x/((_freedom1*x) + _freedom2));
}
/// <summary>
@ -296,7 +289,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _d1, _d2);
return SampleUnchecked(RandomSource, _freedom1, _freedom2);
}
/// <summary>
@ -307,7 +300,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _d1, _d2);
yield return SampleUnchecked(RandomSource, _freedom1, _freedom2);
}
}

188
src/Numerics/Distributions/Gamma.cs

@ -1,4 +1,4 @@
// <copyright file="Gamma.cs" company="Math.NET">
// <copyright file="Gamma.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -46,7 +46,7 @@ namespace MathNet.Numerics.Distributions
/// with shape and inverse scale both zero is undefined.</para>
/// <para> Random number generation for the Gamma distribution is based on the algorithm in:
/// "A Simple Method for Generating Gamma Variables" - Marsaglia &amp; Tsang
/// ACM Transactions on Mathematical Software, Vol. 26, No. 3, September 2000, Pages 363–372.</para>
/// ACM Transactions on Mathematical Software, Vol. 26, No. 3, September 2000, Pages 363–372.</para>
/// <para>The distribution will use the <see cref="System.Random"/> by default.
/// Users can get/set the random number generator by using the <see cref="RandomSource"/> property.</para>
/// <para>The statistics classes will check all the incoming parameters whether they are in the allowed
@ -57,38 +57,37 @@ namespace MathNet.Numerics.Distributions
System.Random _random;
double _shape;
double _invScale;
double _rate;
/// <summary>
/// Initializes a new instance of the Gamma class.
/// </summary>
/// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param>
public Gamma(double shape, double invScale)
/// <param name="shape">The shape (k, α) of the Gamma distribution.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution.</param>
public Gamma(double shape, double rate)
{
_random = new System.Random();
SetParameters(shape, invScale);
SetParameters(shape, rate);
}
/// <summary>
/// Initializes a new instance of the Gamma class.
/// </summary>
/// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <param name="shape">The shape (k, α) of the Gamma distribution.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Gamma(double shape, double invScale, System.Random randomSource)
public Gamma(double shape, double rate, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(shape, invScale);
SetParameters(shape, rate);
}
/// <summary>
/// Constructs a Gamma distribution from a shape and scale parameter. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="scale">The scale of the Gamma distribution.</param>
/// <returns>a normal distribution.</returns>
/// <param name="shape">The shape (k) of the Gamma distribution.</param>
/// <param name="scale">The scale (θ) of the Gamma distribution.</param>
public static Gamma WithShapeScale(double shape, double scale)
{
return new Gamma(shape, 1.0/scale);
@ -98,12 +97,11 @@ namespace MathNet.Numerics.Distributions
/// Constructs a Gamma distribution from a shape and inverse scale parameter. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <returns>a normal distribution.</returns>
public static Gamma WithShapeInvScale(double shape, double invScale)
/// <param name="shape">The shape (α) of the Gamma distribution.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution.</param>
public static Gamma WithShapeRate(double shape, double rate)
{
return new Gamma(shape, invScale);
return new Gamma(shape, rate);
}
/// <summary>
@ -112,81 +110,79 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Gamma(Shape = " + _shape + ", Inverse Scale = " + _invScale + ")";
return "Gamma(α = " + _shape + ", β = " + _rate + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <param name="shape">The shape (k, α) of the Gamma distribution.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double shape, double invScale)
static bool IsValidParameterSet(double shape, double rate)
{
return shape >= 0.0 && invScale >= 0.0;
return shape >= 0.0 && rate >= 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <param name="shape">The shape (k, α) of the Gamma distribution.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double shape, double invScale)
void SetParameters(double shape, double rate)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale))
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, rate))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_shape = shape;
_invScale = invScale;
_rate = rate;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the shape (k, α) of the Gamma distribution.
/// </summary>
public System.Random RandomSource
public double Shape
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _shape; }
set { SetParameters(value, _rate); }
}
/// <summary>
/// Gets or sets the shape of the Gamma distribution.
/// Gets or sets the rate or inverse scale (β) of the Gamma distribution.
/// </summary>
public double Shape
public double Rate
{
get { return _shape; }
set { SetParameters(value, _invScale); }
get { return _rate; }
set { SetParameters(_shape, value); }
}
/// <summary>
/// Gets or sets the scale of the Gamma distribution.
/// Gets or sets the scale (θ) of the Gamma distribution.
/// </summary>
public double Scale
{
get { return 1.0/_invScale; }
get { return 1.0 / _rate; }
set
{
var invScale = 1.0/value;
if (Double.IsNegativeInfinity(invScale))
var rate = 1.0 / value;
if (Double.IsNegativeInfinity(rate))
{
invScale = -invScale;
rate = -rate;
}
SetParameters(_shape, invScale);
SetParameters(_shape, rate);
}
}
/// <summary>
/// Gets or sets the inverse scale of the Gamma distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double InvScale
public System.Random RandomSource
{
get { return _invScale; }
set { SetParameters(_shape, value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -196,17 +192,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return _shape;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return _shape/_invScale;
return _shape/_rate;
}
}
@ -217,17 +213,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return 0.0;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return _shape/(_invScale*_invScale);
return _shape/(_rate*_rate);
}
}
@ -238,17 +234,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return 0.0;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return Math.Sqrt(_shape/(_invScale*_invScale));
return Math.Sqrt(_shape/(_rate*_rate));
}
}
@ -259,17 +255,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return 0.0;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return _shape - Math.Log(_invScale) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape)*SpecialFunctions.DiGamma(_shape));
return _shape - Math.Log(_rate) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape)*SpecialFunctions.DiGamma(_shape));
}
}
@ -280,12 +276,12 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return 0.0;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
@ -301,17 +297,17 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return _shape;
}
if (_invScale == 0.0 && _shape == 0.0)
if (_rate == 0.0 && _shape == 0.0)
{
return Double.NaN;
}
return (_shape - 1.0)/_invScale;
return (_shape - 1.0)/_rate;
}
}
@ -346,22 +342,22 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return x == _shape ? Double.PositiveInfinity : 0.0;
}
if (_shape == 0.0 && _invScale == 0.0)
if (_shape == 0.0 && _rate == 0.0)
{
return 0.0;
}
if (_shape == 1.0)
{
return _invScale*Math.Exp(-_invScale*x);
return _rate*Math.Exp(-_rate*x);
}
return Math.Pow(_invScale, _shape)*Math.Pow(x, _shape - 1.0)*Math.Exp(-_invScale*x)/SpecialFunctions.Gamma(_shape);
return Math.Pow(_rate, _shape)*Math.Pow(x, _shape - 1.0)*Math.Exp(-_rate*x)/SpecialFunctions.Gamma(_shape);
}
/// <summary>
@ -371,22 +367,22 @@ namespace MathNet.Numerics.Distributions
/// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x)
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return x == _shape ? Double.PositiveInfinity : Double.NegativeInfinity;
}
if (_shape == 0.0 && _invScale == 0.0)
if (_shape == 0.0 && _rate == 0.0)
{
return Double.NegativeInfinity;
}
if (_shape == 1.0)
{
return Math.Log(_invScale) - (_invScale*x);
return Math.Log(_rate) - (_rate*x);
}
return (_shape*Math.Log(_invScale)) + ((_shape - 1.0)*Math.Log(x)) - (_invScale*x) - SpecialFunctions.GammaLn(_shape);
return (_shape*Math.Log(_rate)) + ((_shape - 1.0)*Math.Log(x)) - (_rate*x) - SpecialFunctions.GammaLn(_shape);
}
/// <summary>
@ -396,32 +392,32 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
if (Double.IsPositiveInfinity(_invScale))
if (Double.IsPositiveInfinity(_rate))
{
return x >= _shape ? 1.0 : 0.0;
}
if (_shape == 0.0 && _invScale == 0.0)
if (_shape == 0.0 && _rate == 0.0)
{
return 0.0;
}
return SpecialFunctions.GammaLowerRegularized(_shape, x*_invScale);
return SpecialFunctions.GammaLowerRegularized(_shape, x*_rate);
}
/// <summary>
/// <para>Sampling implementation based on:
/// "A Simple Method for Generating Gamma Variables" - Marsaglia &amp; Tsang
/// ACM Transactions on Mathematical Software, Vol. 26, No. 3, September 2000, Pages 363–372.</para>
/// ACM Transactions on Mathematical Software, Vol. 26, No. 3, September 2000, Pages 363–372.</para>
/// <para>This method performs no parameter checks.</para>
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <param name="shape">The shape (k, α) of the Gamma distribution.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution.</param>
/// <returns>A sample from a Gamma distributed random variable.</returns>
internal static double SampleUnchecked(System.Random rnd, double shape, double invScale)
internal static double SampleUnchecked(System.Random rnd, double shape, double rate)
{
if (Double.IsPositiveInfinity(invScale))
if (Double.IsPositiveInfinity(rate))
{
return shape;
}
@ -453,12 +449,12 @@ namespace MathNet.Numerics.Distributions
x = x*x;
if (u < 1.0 - (0.0331*x*x))
{
return alphafix*d*v/invScale;
return alphafix*d*v/rate;
}
if (Math.Log(u) < (0.5*x) + (d*(1.0 - v + Math.Log(v))))
{
return alphafix*d*v/invScale;
return alphafix*d*v/rate;
}
}
}
@ -469,7 +465,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _shape, _invScale);
return SampleUnchecked(RandomSource, _shape, _rate);
}
/// <summary>
@ -480,7 +476,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _shape, _invScale);
yield return SampleUnchecked(RandomSource, _shape, _rate);
}
}
@ -488,36 +484,36 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the Gamma distribution.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="shape">The shape of the Gamma distribution from which to generate samples.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution from which to generate samples.</param>
/// <param name="shape">The shape (k, α) of the Gamma distribution.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rng, double shape, double invScale)
public static double Sample(System.Random rng, double shape, double rate)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale))
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, rate))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return SampleUnchecked(rng, shape, invScale);
return SampleUnchecked(rng, shape, rate);
}
/// <summary>
/// Generates a sequence of samples from the Gamma distribution.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="shape">The shape of the Gamma distribution from which to generate samples.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution from which to generate samples.</param>
/// <param name="shape">The shape (k, α) of the Gamma distribution.</param>
/// <param name="rate">The rate or inverse scale (β) of the Gamma distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rng, double shape, double invScale)
public static IEnumerable<double> Samples(System.Random rng, double shape, double rate)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale))
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, rate))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return SampleUnchecked(rng, shape, invScale);
yield return SampleUnchecked(rng, shape, rate);
}
}
}

17
src/Numerics/Distributions/Geometric.cs

@ -111,22 +111,21 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the probability of generating a one.
/// </summary>
public System.Random RandomSource
public double P
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _p; }
set { SetParameters(value); }
}
/// <summary>
/// Gets or sets the probability of generating a one.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double P
public System.Random RandomSource
{
get { return _p; }
set { SetParameters(value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>

11
src/Numerics/Distributions/Hypergeometric.cs

@ -51,19 +51,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// The size of the population (N).
/// </summary>
int _population;
/// <summary>
/// The number successes within the population (K, M).
/// </summary>
int _success;
/// <summary>
/// The number of draws without replacement (n).
/// </summary>
int _draws;
/// <summary>

54
src/Numerics/Distributions/InverseGamma.cs

@ -38,7 +38,7 @@ namespace MathNet.Numerics.Distributions
/// Continuous Univariate Inverse Gamma distribution.
/// The inverse Gamma distribution is a distribution over the positive real numbers parameterized by
/// two positive parameters.
/// <a href="http://en.wikipedia.org/wiki/inverse-gamma_distribution">Wikipedia - InverseGamma distribution</a>.
/// <a href="http://en.wikipedia.org/wiki/Inverse-gamma_distribution">Wikipedia - InverseGamma distribution</a>.
/// </summary>
/// <remarks><para>The distribution will use the <see cref="System.Random"/> by default.
/// Users can set the random number generator by using the <see cref="RandomSource"/> property.</para>
@ -55,8 +55,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="InverseGamma"/> class.
/// </summary>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <param name="shape">The shape (α) of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (β) of the inverse Gamma distribution.</param>
public InverseGamma(double shape, double scale)
{
_random = new System.Random();
@ -66,8 +66,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="InverseGamma"/> class.
/// </summary>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <param name="shape">The shape (α) of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (β) of the inverse Gamma distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public InverseGamma(double shape, double scale, System.Random randomSource)
{
@ -81,14 +81,14 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "InverseGamma(Shape = " + _shape + ", Inverse Scale = " + _scale + ")";
return "InverseGamma(α = " + _shape + ", β = " + _scale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <param name="shape">The shape (α) of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (β) of the inverse Gamma distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double shape, double scale)
{
@ -98,8 +98,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <param name="shape">The shape (α) of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (β) of the inverse Gamma distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double shape, double scale)
{
@ -113,16 +113,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the shape (alpha) parameter.
/// Gets or sets the shape (α) parameter.
/// </summary>
public double Shape
{
@ -131,7 +122,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets The scale (beta) parameter.
/// Gets or sets The scale (β) parameter.
/// </summary>
public double Scale
{
@ -139,6 +130,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
@ -275,8 +275,8 @@ namespace MathNet.Numerics.Distributions
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <param name="shape">The shape (α) of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (β) of the inverse Gamma distribution.</param>
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double shape, double scale)
{
@ -308,8 +308,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <param name="shape">The shape (α) of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (β) of the inverse Gamma distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double shape, double scale)
{
@ -325,8 +325,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <param name="shape">The shape (α) of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (β) of the inverse Gamma distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double shape, double scale)
{

115
src/Numerics/Distributions/InverseWishart.cs

@ -50,15 +50,8 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// The degrees of freedom for the inverse Wishart distribution.
/// </summary>
double _nu;
/// <summary>
/// The scale matrix for the inverse Wishart distribution.
/// </summary>
Matrix<double> _s;
double _freedom;
Matrix<double> _scale;
/// <summary>
/// Caches the Cholesky factorization of the scale matrix.
@ -68,24 +61,24 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="InverseWishart"/> class.
/// </summary>
/// <param name="nu">The degrees of freedom for the inverse Wishart distribution.</param>
/// <param name="s">The scale matrix for the inverse Wishart distribution.</param>
public InverseWishart(double nu, Matrix<double> s)
/// <param name="degreeOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
public InverseWishart(double degreeOfFreedom, Matrix<double> scale)
{
_random = new System.Random();
SetParameters(nu, s);
SetParameters(degreeOfFreedom, scale);
}
/// <summary>
/// Initializes a new instance of the <see cref="InverseWishart"/> class.
/// </summary>
/// <param name="nu">The degrees of freedom for the inverse Wishart distribution.</param>
/// <param name="s">The scale matrix for the inverse Wishart distribution.</param>
/// <param name="degreeOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public InverseWishart(double nu, Matrix<double> s, System.Random randomSource)
public InverseWishart(double degreeOfFreedom, Matrix<double> scale, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(nu, s);
SetParameters(degreeOfFreedom, scale);
}
/// <summary>
@ -94,76 +87,76 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "InverseWishart(Nu = " + _nu + ", Rows = " + _s.RowCount + ", Columns = " + _s.ColumnCount + ")";
return "InverseWishart(ν = " + _freedom + ", Rows = " + _scale.RowCount + ", Columns = " + _scale.ColumnCount + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <param name="degreeOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double nu, Matrix<double> s)
static bool IsValidParameterSet(double degreeOfFreedom, Matrix<double> scale)
{
if (s.RowCount != s.ColumnCount)
if (scale.RowCount != scale.ColumnCount)
{
return false;
}
for (var i = 0; i < s.RowCount; i++)
for (var i = 0; i < scale.RowCount; i++)
{
if (s.At(i, i) <= 0.0)
if (scale.At(i, i) <= 0.0)
{
return false;
}
}
return nu > 0.0;
return degreeOfFreedom > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <param name="degreeOfFreedom">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 don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double nu, Matrix<double> s)
void SetParameters(double degreeOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreeOfFreedom, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_nu = nu;
_s = s;
_chol = Cholesky<double>.Create(_s);
_freedom = degreeOfFreedom;
_scale = scale;
_chol = Cholesky<double>.Create(_scale);
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the degree of freedom (ν) for the inverse Wishart distribution.
/// </summary>
public System.Random RandomSource
public double DegreeOfFreedom
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _freedom; }
set { SetParameters(value, _scale); }
}
/// <summary>
/// Gets or sets the degrees of freedom for the inverse Wishart distribution.
/// Gets or sets the scale matrix (Ψ) for the inverse Wishart distribution.
/// </summary>
public double Nu
public Matrix<double> Scale
{
get { return _nu; }
set { SetParameters(value, _s); }
get { return _scale; }
set { SetParameters(_freedom, value); }
}
/// <summary>
/// Gets or sets the scale matrix for the inverse Wishart distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public Matrix<double> S
public System.Random RandomSource
{
get { return _s; }
set { SetParameters(_nu, value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -172,7 +165,7 @@ namespace MathNet.Numerics.Distributions
/// <value>The mean of the distribution.</value>
public Matrix<double> Mean
{
get { return _s*(1.0/(_nu - _s.RowCount - 1.0)); }
get { return _scale*(1.0/(_freedom - _scale.RowCount - 1.0)); }
}
/// <summary>
@ -182,7 +175,7 @@ namespace MathNet.Numerics.Distributions
/// <remarks>A. O'Hagan, and J. J. Forster (2004). Kendall's Advanced Theory of Statistics: Bayesian Inference. 2B (2 ed.). Arnold. ISBN 0-340-80752-0.</remarks>
public Matrix<double> Mode
{
get { return _s*(1.0/(_nu + _s.RowCount + 1.0)); }
get { return _scale*(1.0/(_freedom + _scale.RowCount + 1.0)); }
}
/// <summary>
@ -194,13 +187,13 @@ namespace MathNet.Numerics.Distributions
{
get
{
var res = _s.CreateMatrix(_s.RowCount, _s.ColumnCount);
var res = _scale.CreateMatrix(_scale.RowCount, _scale.ColumnCount);
for (var i = 0; i < res.RowCount; i++)
{
for (var j = 0; j < res.ColumnCount; j++)
{
var num1 = ((_nu - _s.RowCount + 1)*_s.At(i, j)*_s.At(i, j)) + ((_nu - _s.RowCount - 1)*_s.At(i, i)*_s.At(j, j));
var num2 = (_nu - _s.RowCount)*(_nu - _s.RowCount - 1)*(_nu - _s.RowCount - 1)*(_nu - _s.RowCount - 3);
var num1 = ((_freedom - _scale.RowCount + 1)*_scale.At(i, j)*_scale.At(i, j)) + ((_freedom - _scale.RowCount - 1)*_scale.At(i, i)*_scale.At(j, j));
var num2 = (_freedom - _scale.RowCount)*(_freedom - _scale.RowCount - 1)*(_freedom - _scale.RowCount - 1)*(_freedom - _scale.RowCount - 3);
res.At(i, j, num1/num2);
}
}
@ -217,7 +210,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(Matrix<double> x)
{
var p = _s.RowCount;
var p = _scale.RowCount;
if (x.RowCount != p || x.ColumnCount != p)
{
@ -226,19 +219,19 @@ namespace MathNet.Numerics.Distributions
var chol = Cholesky<double>.Create(x);
var dX = chol.Determinant;
var sXi = chol.Solve(S);
var sXi = chol.Solve(Scale);
// Compute the multivariate Gamma function.
var gp = Math.Pow(Constants.Pi, p*(p - 1.0)/4.0);
for (var j = 1; j <= p; j++)
{
gp *= SpecialFunctions.Gamma((_nu + 1.0 - j)/2.0);
gp *= SpecialFunctions.Gamma((_freedom + 1.0 - j)/2.0);
}
return Math.Pow(dX, -(_nu + p + 1.0)/2.0)
return Math.Pow(dX, -(_freedom + p + 1.0)/2.0)
*Math.Exp(-0.5*sXi.Trace())
*Math.Pow(_chol.Determinant, _nu/2.0)
/Math.Pow(2.0, _nu*p/2.0)
*Math.Pow(_chol.Determinant, _freedom/2.0)
/Math.Pow(2.0, _freedom*p/2.0)
/gp;
}
@ -249,7 +242,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public Matrix<double> Sample()
{
return Sample(RandomSource, _nu, _s);
return Sample(RandomSource, _freedom, _scale);
}
/// <summary>
@ -257,17 +250,17 @@ namespace MathNet.Numerics.Distributions
/// a Wishart random variable and inverting the matrix.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="nu">The degrees of freedom.</param>
/// <param name="s">The scale matrix.</param>
/// <param name="degreeOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static Matrix<double> Sample(System.Random rnd, double nu, Matrix<double> s)
public static Matrix<double> Sample(System.Random rnd, double degreeOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreeOfFreedom, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
var r = Wishart.Sample(rnd, nu, s.Inverse());
var r = Wishart.Sample(rnd, degreeOfFreedom, scale.Inverse());
return r.Inverse();
}
}

78
src/Numerics/Distributions/Laplace.cs

@ -50,26 +50,9 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
double _location;
double _scale;
/// <summary>
/// Gets or sets the location of the Laplace distribution.
/// </summary>
public double Location
{
get { return Mean; }
set { SetParameters(value, _scale); }
}
/// <summary>
/// Gets or sets the scale of the Laplace distribution.
/// </summary>
public double Scale
{
get { return _scale; }
set { SetParameters(Mean, value); }
}
/// <summary>
/// Initializes a new instance of the <see cref="Laplace"/> class (location = 0, scale = 1).
/// </summary>
@ -109,14 +92,14 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Laplace(Location = " + Mean + ", Scale = " + _scale + ")";
return "Laplace(μ = " + _location + ", b = " + _scale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="location">The location for the Laplace distribution.</param>
/// <param name="scale">The scale for the Laplace distribution.</param>
/// <param name="location">The location (μ) of the Laplace distribution.</param>
/// <param name="scale">The scale (b) of the Laplace distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double location, double scale)
{
@ -126,8 +109,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="location">The location for the Laplace distribution.</param>
/// <param name="scale">The scale for the Laplace distribution.</param>
/// <param name="location">The location (μ) of the Laplace distribution.</param>
/// <param name="scale">The scale (b) of the Laplace distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double location, double scale)
{
@ -136,10 +119,28 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
Mean = location;
_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>
/// Gets or sets the scale (b) of the Laplace distribution.
/// </summary>
public double Scale
{
get { return _scale; }
set { SetParameters(_location, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
@ -152,7 +153,10 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
public double Mean { get; private set; }
public double Mean
{
get { return _location; }
}
/// <summary>
/// Gets the variance of the distribution.
@ -191,7 +195,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Mode
{
get { return Mean; }
get { return _location; }
}
/// <summary>
@ -199,7 +203,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Median
{
get { return Mean; }
get { return _location; }
}
/// <summary>
@ -225,7 +229,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return Math.Exp(-Math.Abs(x - Mean)/_scale)/(2.0*_scale);
return Math.Exp(-Math.Abs(x - _location)/_scale)/(2.0*_scale);
}
/// <summary>
@ -245,15 +249,15 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 0.5*(1.0 + (Math.Sign(x - Mean)*(1.0 - Math.Exp(-Math.Abs(x - Mean)/_scale))));
return 0.5*(1.0 + (Math.Sign(x - _location)*(1.0 - Math.Exp(-Math.Abs(x - _location)/_scale))));
}
/// <summary>
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="location">The location (μ) of the Laplace distribution.</param>
/// <param name="scale">The scale (b) of the Laplace distribution.</param>
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double location, double scale)
{
@ -267,7 +271,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, Mean, _scale);
return SampleUnchecked(RandomSource, _location, _scale);
}
/// <summary>
@ -278,7 +282,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, Mean, _scale);
yield return SampleUnchecked(RandomSource, _location, _scale);
}
}
@ -286,8 +290,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="location">The location (μ) of the Laplace distribution.</param>
/// <param name="scale">The scale (b) of the Laplace distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double location, double scale)
{
@ -303,8 +307,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="location">The location (μ) of the Laplace distribution.</param>
/// <param name="scale">The scale (b) of the Laplace distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double location, double scale)
{

50
src/Numerics/Distributions/LogNormal.cs

@ -1,4 +1,4 @@
// <copyright file="LogNormal.cs" company="Math.NET">
// <copyright file="LogNormal.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -58,8 +58,8 @@ namespace MathNet.Numerics.Distributions
/// The distribution will be initialized with the default <seealso cref="System.Random"/>
/// random number generator.
/// </summary>
/// <param name="mu">The mu of the logarithm of the distribution.</param>
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
/// <param name="mu">The log-scale (μ) of the logarithm of the distribution.</param>
/// <param name="sigma">The shape (σ) of the logarithm of the distribution.</param>
public LogNormal(double mu, double sigma)
{
_random = new System.Random();
@ -71,8 +71,8 @@ namespace MathNet.Numerics.Distributions
/// The distribution will be initialized with the default <seealso cref="System.Random"/>
/// random number generator.
/// </summary>
/// <param name="mu">The mu of the logarithm of the distribution.</param>
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
/// <param name="mu">The log-scale (μ) of the distribution.</param>
/// <param name="sigma">The shape (σ) of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public LogNormal(double mu, double sigma, System.Random randomSource)
{
@ -108,14 +108,14 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "LogNormal(Mu = " + _mu + ", Sigma = " + _sigma + ")";
return "LogNormal(μ = " + _mu + ", σ = " + _sigma + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="mu">The mu of the logarithm of the distribution.</param>
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
/// <param name="mu">The log-scale (μ) of the distribution.</param>
/// <param name="sigma">The shape (σ) of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double mu, double sigma)
{
@ -125,8 +125,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="mu">The mu of the logarithm of the distribution.</param>
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
/// <param name="mu">The log-scale (μ) of the distribution.</param>
/// <param name="sigma">The shape (σ) of the distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double mu, double sigma)
{
@ -140,16 +140,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the mean of the logarithm of the log-normal.
/// Gets or sets the log-scale (μ) (mean of the logarithm) of the distribution.
/// </summary>
public double Mu
{
@ -158,7 +149,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the standard deviation of the logarithm of the log-normal.
/// Gets or sets the shape (σ) (standard deviation of the logarithm) of the distribution.
/// </summary>
public double Sigma
{
@ -166,6 +157,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_mu, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mu of the log-normal distribution.
/// </summary>
@ -324,8 +324,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the log-normal distribution using the <i>Box-Muller</i> algorithm.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="mu">The mu of the logarithm of the distribution.</param>
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
/// <param name="mu">The log-scale (μ) of the distribution.</param>
/// <param name="sigma">The shape (σ) of the distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rng, double mu, double sigma)
{
@ -341,8 +341,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the log-normal distribution using the <i>Box-Muller</i> algorithm.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="mu">The mu of the logarithm of the distribution.</param>
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
/// <param name="mu">The log-scale (μ) of the distribution.</param>
/// <param name="sigma">The shape (σ) of the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rng, double mu, double sigma)
{

18
src/Numerics/Distributions/MatrixNormal.cs

@ -163,15 +163,6 @@ namespace MathNet.Numerics.Distributions
_k = k;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the mean. (M)
/// </summary>
@ -202,6 +193,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_m, _v, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Evaluates the probability density function for the matrix normal distribution.
/// </summary>

20
src/Numerics/Distributions/Multinomial.cs

@ -177,21 +177,12 @@ namespace MathNet.Numerics.Distributions
_trials = n;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the proportion of ratios.
/// </summary>
public double[] P
{
get { return (double[]) _p.Clone(); }
get { return (double[])_p.Clone(); }
set { SetParameters(value, _trials); }
}
@ -204,6 +195,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_p, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

40
src/Numerics/Distributions/NegativeBinomial.cs

@ -51,30 +51,8 @@ namespace MathNet.Numerics.Distributions
System.Random _random;
double _trials;
/// <summary>
/// The p parameter of the distribution.
/// </summary>
double _p;
/// <summary>
/// Gets or sets the number of trials.
/// </summary>
public double R
{
get { return _trials; }
set { SetParameters(value, _p); }
}
/// <summary>
/// Gets or sets the probability of success.
/// </summary>
public double P
{
get { return _p; }
set { SetParameters(_trials, value); }
}
/// <summary>
/// Initializes a new instance of the <see cref="NegativeBinomial"/> class.
/// </summary>
@ -137,6 +115,24 @@ namespace MathNet.Numerics.Distributions
_trials = r;
}
/// <summary>
/// Gets or sets the number of trials.
/// </summary>
public double R
{
get { return _trials; }
set { SetParameters(value, _p); }
}
/// <summary>
/// Gets or sets the probability of success.
/// </summary>
public double P
{
get { return _p; }
set { SetParameters(_trials, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>

127
src/Numerics/Distributions/Normal.cs

@ -1,4 +1,4 @@
// <copyright file="Normal.cs" company="Math.NET">
// <copyright file="Normal.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -77,8 +77,8 @@ namespace MathNet.Numerics.Distributions
/// Initializes a new instance of the Normal class with a particular mean and standard deviation. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="mean">The mean of the normal distribution.</param>
/// <param name="stddev">The standard deviation of the normal distribution.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
public Normal(double mean, double stddev)
{
_random = new System.Random();
@ -89,8 +89,8 @@ namespace MathNet.Numerics.Distributions
/// Initializes a new instance of the Normal class with a particular mean and standard deviation. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="mean">The mean of the normal distribution.</param>
/// <param name="stddev">The standard deviation of the normal distribution.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Normal(double mean, double stddev, System.Random randomSource)
{
@ -102,8 +102,8 @@ namespace MathNet.Numerics.Distributions
/// Constructs a normal distribution from a mean and standard deviation. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="mean">The mean of the normal distribution.</param>
/// <param name="stddev">The standard deviation of the normal distribution.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <returns>a normal distribution.</returns>
public static Normal WithMeanStdDev(double mean, double stddev)
{
@ -114,7 +114,7 @@ namespace MathNet.Numerics.Distributions
/// Constructs a normal distribution from a mean and variance. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="mean">The mean of the normal distribution.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="var">The variance of the normal distribution.</param>
/// <returns>a normal distribution.</returns>
public static Normal WithMeanVariance(double mean, double var)
@ -126,7 +126,7 @@ namespace MathNet.Numerics.Distributions
/// Constructs a normal distribution from a mean and precision. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="mean">The mean of the normal distribution.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="precision">The precision of the normal distribution.</param>
/// <returns>a normal distribution.</returns>
public static Normal WithMeanPrecision(double mean, double precision)
@ -149,14 +149,14 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Normal(Mean = " + _mean + ", StdDev = " + _stdDev + ")";
return "Normal(μ = " + _mean + ", σ = " + _stdDev + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="mean">The mean of the normal distribution.</param>
/// <param name="stddev">The standard deviation of the normal distribution.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double mean, double stddev)
{
@ -166,8 +166,8 @@ namespace MathNet.Numerics.Distributions
/// <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.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double mean, double stddev)
{
@ -181,12 +181,30 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the mean (μ) of the normal distribution.
/// </summary>
public System.Random RandomSource
public double Mean
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _mean; }
set { SetParameters(value, _stdDev); }
}
/// <summary>
/// Gets or sets the standard deviation (σ) of the normal distribution.
/// </summary>
public double StdDev
{
get { return _stdDev; }
set { SetParameters(_mean, value); }
}
/// <summary>
/// Gets or sets the variance of the normal distribution.
/// </summary>
public double Variance
{
get { return _stdDev * _stdDev; }
set { SetParameters(_mean, Math.Sqrt(value)); }
}
/// <summary>
@ -194,47 +212,26 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public double Precision
{
get { return 1.0/(_stdDev*_stdDev); }
get { return 1.0 / (_stdDev * _stdDev); }
set
{
var sdev = 1.0/Math.Sqrt(value);
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>
/// Gets or sets the mean of the normal distribution.
/// </summary>
public double Mean
{
get { return _mean; }
set { SetParameters(value, _stdDev); }
}
/// <summary>
/// Gets or sets the variance of the normal distribution.
/// </summary>
public double Variance
{
get { return _stdDev*_stdDev; }
set { SetParameters(_mean, Math.Sqrt(value)); }
}
/// <summary>
/// Gets or sets the standard deviation of the normal distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double StdDev
public System.Random RandomSource
{
get { return _stdDev; }
set { SetParameters(_mean, value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -288,27 +285,27 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Computes the density of the normal distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="mean">The mean of the normal distribution.</param>
/// <param name="sdev">The standard deviation of the normal distribution.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
internal static double Density(double mean, double sdev, double x)
internal static double Density(double mean, double stddev, double x)
{
var d = (x - mean)/sdev;
return Math.Exp(-0.5*d*d)/(Constants.Sqrt2Pi*sdev);
var d = (x - mean)/stddev;
return Math.Exp(-0.5*d*d)/(Constants.Sqrt2Pi*stddev);
}
/// <summary>
/// Computes the log density of the normal distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="mean">The mean of the normal distribution.</param>
/// <param name="sdev">The standard deviation of the normal distribution.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
internal static double DensityLn(double mean, double sdev, double x)
internal static double DensityLn(double mean, double stddev, double x)
{
var d = (x - mean)/sdev;
return (-0.5*d*d) - Math.Log(sdev) - Constants.LogSqrt2Pi;
var d = (x - mean)/stddev;
return (-0.5*d*d) - Math.Log(stddev) - Constants.LogSqrt2Pi;
}
/// <summary>
@ -334,13 +331,13 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Computes the cumulative distribution function (CDF) of the normal distribution, i.e. P(X &lt;= x).
/// </summary>
/// <param name="mean">The mean of the normal distribution.</param>
/// <param name="sdev">The standard deviation of the normal distribution.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
internal static double CumulativeDistribution(double mean, double sdev, double x)
internal static double CumulativeDistribution(double mean, double stddev, double x)
{
return 0.5*(1.0 + SpecialFunctions.Erf((x - mean)/(sdev*Constants.Sqrt2)));
return 0.5*(1.0 + SpecialFunctions.Erf((x - mean)/(stddev*Constants.Sqrt2)));
}
/// <summary>
@ -388,8 +385,8 @@ namespace MathNet.Numerics.Distributions
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="mean">The mean of the normal distribution from which to generate samples.</param>
/// <param name="stddev">The standard deviation of the normal distribution from which to generate samples.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double mean, double stddev)
{
@ -423,8 +420,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the normal distribution using the <i>Box-Muller</i> algorithm.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="mean">The mean of the normal distribution from which to generate samples.</param>
/// <param name="stddev">The standard deviation of the normal distribution from which to generate samples.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double mean, double stddev)
{
@ -440,8 +437,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the normal distribution using the <i>Box-Muller</i> algorithm.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="mean">The mean of the normal distribution from which to generate samples.</param>
/// <param name="stddev">The standard deviation of the normal distribution from which to generate samples.</param>
/// <param name="mean">The mean (μ) of the normal distribution.</param>
/// <param name="stddev">The standard deviation (σ) of the normal distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double mean, double stddev)
{

18
src/Numerics/Distributions/NormalGamma.cs

@ -180,15 +180,6 @@ namespace MathNet.Numerics.Distributions
_precisionInvScale = precInvScale;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the location of the mean.
/// </summary>
@ -225,6 +216,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Returns the marginal distribution for the mean of the <c>NormalGamma</c> distribution.
/// </summary>

52
src/Numerics/Distributions/Pareto.cs

@ -56,8 +56,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Pareto"/> class.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="shape">The shape parameter of the distribution.</param>
/// <param name="scale">The scale (xm) of the distribution.</param>
/// <param name="shape">The shape (α) of the distribution.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> or <paramref name="shape"/> are negative.</exception>
public Pareto(double scale, double shape)
{
@ -68,8 +68,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Pareto"/> class.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="shape">The shape parameter of the distribution.</param>
/// <param name="scale">The scale (xm) of the distribution.</param>
/// <param name="shape">The shape (α) of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> or <paramref name="shape"/> are negative.</exception>
public Pareto(double scale, double shape, System.Random randomSource)
@ -84,14 +84,14 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Pareto(Scale = " + _scale + ", Shape = " + _shape + ")";
return "Pareto(xm = " + _scale + ", α = " + _shape + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="shape">The shape parameter of the distribution.</param>
/// <param name="scale">The scale (xm) of the distribution.</param>
/// <param name="shape">The shape (α) of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double scale, double shape)
{
@ -101,8 +101,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="shape">The shape parameter of the distribution.</param>
/// <param name="scale">The scale (xm) of the distribution.</param>
/// <param name="shape">The shape (α) of the distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double scale, double shape)
{
@ -116,16 +116,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the scale parameter of the distribution.
/// Gets or sets the scale (xm) of the distribution.
/// </summary>
public double Scale
{
@ -134,7 +125,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the shape parameter of the distribution.
/// Gets or sets the shape (α) of the distribution.
/// </summary>
public double Shape
{
@ -142,6 +133,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_scale, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
@ -264,8 +264,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the Pareto distribution without doing parameter checking.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="shape">The shape parameter.</param>
/// <param name="scale">The scale (xm) of the distribution.</param>
/// <param name="shape">The shape (α) of the distribution.</param>
/// <returns>a random number from the Pareto distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double scale, double shape)
{
@ -297,8 +297,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="shape">The shape parameter.</param>
/// <param name="scale">The scale (xm) of the distribution.</param>
/// <param name="shape">The shape (α) of the distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double scale, double shape)
{
@ -314,8 +314,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="shape">The shape parameter.</param>
/// <param name="scale">The scale (xm) of the distribution.</param>
/// <param name="shape">The shape (α) of the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double scale, double shape)
{

18
src/Numerics/Distributions/Poisson.cs

@ -48,15 +48,6 @@ namespace MathNet.Numerics.Distributions
double _lambda;
/// <summary>
/// Gets or sets the Poisson distribution parameter λ.
/// </summary>
public double Lambda
{
get { return _lambda; }
set { SetParameters(value); }
}
/// <summary>
/// Initializes a new instance of the <see cref="Poisson"/> class.
/// </summary>
@ -116,6 +107,15 @@ namespace MathNet.Numerics.Distributions
_lambda = lambda;
}
/// <summary>
/// Gets or sets the Poisson distribution parameter λ.
/// </summary>
public double Lambda
{
get { return _lambda; }
set { SetParameters(value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>

32
src/Numerics/Distributions/Rayleigh.cs

@ -56,7 +56,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Rayleigh"/> class.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Rayleigh(double scale)
{
@ -67,7 +67,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Rayleigh"/> class.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Rayleigh(double scale, System.Random randomSource)
@ -82,13 +82,13 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Rayleigh(Scale = " + _scale + ")";
return "Rayleigh(σ = " + _scale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double scale)
{
@ -98,7 +98,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double scale)
{
@ -111,21 +111,21 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// Gets or sets the scale (σ) of the distribution.
/// </summary>
public System.Random RandomSource
public double Scale
{
get { return _random; }
set { _random = value ?? new System.Random(); }
get { return _scale; }
set { SetParameters(value); }
}
/// <summary>
/// Gets or sets the scale parameter of the distribution.
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public double Scale
public System.Random RandomSource
{
get { return _scale; }
set { SetParameters(value); }
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
@ -234,7 +234,7 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the Rayleigh distribution without doing parameter checking.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <returns>a random number from the Rayleigh distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double scale)
{
@ -266,7 +266,7 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double scale)
{
@ -282,7 +282,7 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="scale">The scale parameter.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double scale)
{

100
src/Numerics/Distributions/Stable.cs

@ -51,26 +51,18 @@ namespace MathNet.Numerics.Distributions
{
System.Random _random;
/// <summary>
/// The stability parameter of the distribution.
/// </summary>
double _alpha;
/// <summary>
/// The skewness parameter of the distribution.
/// </summary>
double _beta;
double _scale;
double _location;
/// <summary>
/// Initializes a new instance of the <see cref="Stable"/> class.
/// </summary>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <param name="alpha">The stability (α) of the distribution.</param>
/// <param name="beta">The skewness (β) of the distribution.</param>
/// <param name="scale">The scale (c) of the distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
public Stable(double alpha, double beta, double scale, double location)
{
_random = new System.Random();
@ -80,10 +72,10 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Stable"/> class.
/// </summary>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <param name="alpha">The stability (α) of the distribution.</param>
/// <param name="beta">The skewness (β) of the distribution.</param>
/// <param name="scale">The scale (c) of the distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <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)
{
@ -97,16 +89,16 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Stable(" + "Stability = " + _alpha + ", Skewness = " + _beta + ", Scale = " + _scale + ", Location = " + _location + ")";
return "Stable(α = " + _alpha + ", β = " + _beta + ", c = " + _scale + ", μ = " + _location + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <param name="alpha">The stability (α) of the distribution.</param>
/// <param name="beta">The skewness (β) of the distribution.</param>
/// <param name="scale">The scale (c) of the distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double alpha, double beta, double scale, double location)
{
@ -116,10 +108,10 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <param name="alpha">The stability (α) of the distribution.</param>
/// <param name="beta">The skewness (β) of the distribution.</param>
/// <param name="scale">The scale (c) of the distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
void SetParameters(double alpha, double beta, double scale, double location)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha, beta, scale, location))
@ -134,16 +126,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the stability parameter of the distribution.
/// Gets or sets the stability (α) of the distribution.
/// </summary>
public double Alpha
{
@ -152,7 +135,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets The skewness parameter of the distribution.
/// Gets or sets The skewness (β) of the distribution.
/// </summary>
public double Beta
{
@ -161,7 +144,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the scale parameter of the distribution.
/// Gets or sets the scale (c) of the distribution.
/// </summary>
public double Scale
{
@ -170,7 +153,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the location parameter of the distribution.
/// Gets or sets the location (μ) of the distribution.
/// </summary>
public double Location
{
@ -178,6 +161,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_alpha, _beta, _scale, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
@ -338,8 +330,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Computes the density of the Levy distribution.
/// </summary>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <param name="scale">The scale (c) of the distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
static double LevyDensity(double scale, double location, double x)
@ -392,8 +384,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Computes the cumulative distribution function of the Levy distribution.
/// </summary>
/// <param name="scale">The scale parameter.</param>
/// <param name="location">The location parameter.</param>
/// <param name="scale">The scale (c) of the distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
static double LevyCumulativeDistribution(double scale, double location, double x)
@ -406,10 +398,10 @@ namespace MathNet.Numerics.Distributions
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <param name="alpha">The stability (α) of the distribution.</param>
/// <param name="beta">The skewness (β) of the distribution.</param>
/// <param name="scale">The scale (c) of the distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double alpha, double beta, double scale, double location)
{
@ -464,10 +456,10 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <param name="alpha">The stability (α) of the distribution.</param>
/// <param name="beta">The skewness (β) of the distribution.</param>
/// <param name="scale">The scale (c) of the distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double alpha, double beta, double scale, double location)
{
@ -483,10 +475,10 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param>
/// <param name="alpha">The stability (α) of the distribution.</param>
/// <param name="beta">The skewness (β) of the distribution.</param>
/// <param name="scale">The scale (c) of the distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double alpha, double beta, double scale, double location)
{

18
src/Numerics/Distributions/StudentT.cs

@ -142,15 +142,6 @@ namespace MathNet.Numerics.Distributions
_freedom = dof;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the location of the Student t-distribution.
/// </summary>
@ -178,6 +169,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_location, _scale, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the Student t-distribution.
/// </summary>

54
src/Numerics/Distributions/Weibull.cs

@ -1,4 +1,4 @@
// <copyright file="Weibull.cs" company="Math.NET">
// <copyright file="Weibull.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -65,8 +65,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the Weibull class.
/// </summary>
/// <param name="shape">The shape of the Weibull distribution.</param>
/// <param name="scale">The inverse scale of the Weibull distribution.</param>
/// <param name="shape">The shape (k) of the Weibull distribution.</param>
/// <param name="scale">The scale (λ) of the Weibull distribution.</param>
public Weibull(double shape, double scale)
{
_random = new System.Random();
@ -76,8 +76,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the Weibull class.
/// </summary>
/// <param name="shape">The shape of the Weibull distribution.</param>
/// <param name="scale">The inverse scale of the Weibull distribution.</param>
/// <param name="shape">The shape (k) of the Weibull distribution.</param>
/// <param name="scale">The scale (λ) of the Weibull distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Weibull(double shape, double scale, System.Random randomSource)
{
@ -91,14 +91,14 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Weibull(Shape = " + _shape + ", Scale = " + _scale + ")";
return "Weibull(k = " + _shape + ", λ = " + _scale + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="shape">The shape of the Weibull distribution.</param>
/// <param name="scale">The scale of the Weibull distribution.</param>
/// <param name="shape">The shape (k) of the Weibull distribution.</param>
/// <param name="scale">The scale (λ) of the Weibull distribution.</param>
/// <returns><c>true</c> when the parameters positive valid floating point numbers, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double shape, double scale)
{
@ -108,8 +108,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="shape">The shape of the Weibull distribution.</param>
/// <param name="scale">The inverse scale of the Weibull distribution.</param>
/// <param name="shape">The shape (k) of the Weibull distribution.</param>
/// <param name="scale">The scale (λ) of the Weibull distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double shape, double scale)
{
@ -124,16 +124,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the shape of the Weibull distribution.
/// Gets or sets the shape (k) of the Weibull distribution.
/// </summary>
public double Shape
{
@ -142,7 +133,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the scale of the Weibull distribution.
/// Gets or sets the scale (λ) of the Weibull distribution.
/// </summary>
public double Scale
{
@ -150,6 +141,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the Weibull distribution.
/// </summary>
@ -297,8 +297,8 @@ namespace MathNet.Numerics.Distributions
/// any parameter checks.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="shape">The shape of the Weibull distribution.</param>
/// <param name="scale">The scale of the Weibull distribution.</param>
/// <param name="shape">The shape (k) of the Weibull distribution.</param>
/// <param name="scale">The scale (λ) of the Weibull distribution.</param>
/// <returns>A sample from a Weibull distributed random variable.</returns>
internal static double SampleUnchecked(System.Random rnd, double shape, double scale)
{
@ -331,8 +331,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the Weibull distribution.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="shape">The shape of the Weibull distribution from which to generate samples.</param>
/// <param name="scale">The scale of the Weibull distribution from which to generate samples.</param>
/// <param name="shape">The shape (k) of the Weibull distribution.</param>
/// <param name="scale">The scale (λ) of the Weibull distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rng, double shape, double scale)
{
@ -348,8 +348,8 @@ namespace MathNet.Numerics.Distributions
/// Generates a sequence of samples from the Weibull distribution.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="shape">The shape of the Weibull distribution from which to generate samples.</param>
/// <param name="scale">The scale of the Weibull distribution from which to generate samples.</param>
/// <param name="shape">The shape (k) of the Weibull distribution.</param>
/// <param name="scale">The scale (λ) of the Weibull distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rng, double shape, double scale)
{

108
src/Numerics/Distributions/Wishart.cs

@ -55,12 +55,12 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The degrees of freedom for the Wishart distribution.
/// </summary>
double _nu;
double _degreeOfFreedom;
/// <summary>
/// The scale matrix for the Wishart distribution.
/// </summary>
Matrix<double> _s;
Matrix<double> _scale;
/// <summary>
/// Caches the Cholesky factorization of the scale matrix.
@ -70,66 +70,66 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Wishart"/> class.
/// </summary>
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
public Wishart(double nu, Matrix<double> s)
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
public Wishart(double degreeOfFreedom, Matrix<double> scale)
{
_random = new System.Random();
SetParameters(nu, s);
SetParameters(degreeOfFreedom, scale);
}
/// <summary>
/// Initializes a new instance of the <see cref="Wishart"/> class.
/// </summary>
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Wishart(double nu, Matrix<double> s, System.Random randomSource)
public Wishart(double degreeOfFreedom, Matrix<double> scale, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(nu, s);
SetParameters(degreeOfFreedom, scale);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <param name="degreeOfFreedom">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 don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double nu, Matrix<double> s)
void SetParameters(double degreeOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreeOfFreedom, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_nu = nu;
_s = s;
_chol = Cholesky<double>.Create(_s);
_degreeOfFreedom = degreeOfFreedom;
_scale = scale;
_chol = Cholesky<double>.Create(_scale);
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double nu, Matrix<double> s)
static bool IsValidParameterSet(double degreeOfFreedom, Matrix<double> scale)
{
if (s.RowCount != s.ColumnCount)
if (scale.RowCount != scale.ColumnCount)
{
return false;
}
for (var i = 0; i < s.RowCount; i++)
for (var i = 0; i < scale.RowCount; i++)
{
if (s.At(i, i) <= 0.0)
if (scale.At(i, i) <= 0.0)
{
return false;
}
}
if (nu <= 0.0 || Double.IsNaN(nu))
if (degreeOfFreedom <= 0.0 || Double.IsNaN(degreeOfFreedom))
{
return false;
}
@ -138,21 +138,21 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the degrees of freedom for the Wishart distribution.
/// Gets or sets the degrees of freedom (n) for the Wishart distribution.
/// </summary>
public double Nu
public double DegreeOfFreedom
{
get { return _nu; }
set { SetParameters(value, _s); }
get { return _degreeOfFreedom; }
set { SetParameters(value, _scale); }
}
/// <summary>
/// Gets or sets the scale matrix for the Wishart distribution.
/// Gets or sets the scale matrix (V) for the Wishart distribution.
/// </summary>
public Matrix<double> S
public Matrix<double> Scale
{
get { return _s; }
set { SetParameters(_nu, value); }
get { return _scale; }
set { SetParameters(_degreeOfFreedom, value); }
}
/// <summary>
@ -161,7 +161,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Wishart(Nu = " + _nu + ", Rows = " + _s.RowCount + ", Columns = " + _s.ColumnCount + ")";
return "Wishart(DegreeOfFreedom = " + _degreeOfFreedom + ", Rows = " + _scale.RowCount + ", Columns = " + _scale.ColumnCount + ")";
}
/// <summary>
@ -187,7 +187,7 @@ namespace MathNet.Numerics.Distributions
/// <value>The mean of the distribution.</value>
public Matrix<double> Mean
{
get { return _nu*_s; }
get { return _degreeOfFreedom*_scale; }
}
/// <summary>
@ -196,7 +196,7 @@ namespace MathNet.Numerics.Distributions
/// <value>The mode of the distribution.</value>
public Matrix<double> Mode
{
get { return (_nu - _s.RowCount - 1.0)*_s; }
get { return (_degreeOfFreedom - _scale.RowCount - 1.0)*_scale; }
}
/// <summary>
@ -207,12 +207,12 @@ namespace MathNet.Numerics.Distributions
{
get
{
var res = _s.CreateMatrix(_s.RowCount, _s.ColumnCount);
var res = _scale.CreateMatrix(_scale.RowCount, _scale.ColumnCount);
for (var i = 0; i < res.RowCount; i++)
{
for (var j = 0; j < res.ColumnCount; j++)
{
res.At(i, j, _nu*((_s.At(i, j)*_s.At(i, j)) + (_s.At(i, i)*_s.At(j, j))));
res.At(i, j, _degreeOfFreedom*((_scale.At(i, j)*_scale.At(i, j)) + (_scale.At(i, i)*_scale.At(j, j))));
}
}
@ -228,11 +228,11 @@ namespace MathNet.Numerics.Distributions
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(Matrix<double> x)
{
var p = _s.RowCount;
var p = _scale.RowCount;
if (x.RowCount != p || x.ColumnCount != p)
{
throw Matrix.DimensionsDontMatch<ArgumentOutOfRangeException>(x, _s, "x");
throw Matrix.DimensionsDontMatch<ArgumentOutOfRangeException>(x, _scale, "x");
}
var dX = x.Determinant();
@ -242,13 +242,13 @@ namespace MathNet.Numerics.Distributions
var gp = Math.Pow(Constants.Pi, p*(p - 1.0)/4.0);
for (var j = 1; j <= p; j++)
{
gp *= SpecialFunctions.Gamma((_nu + 1.0 - j)/2.0);
gp *= SpecialFunctions.Gamma((_degreeOfFreedom + 1.0 - j)/2.0);
}
return Math.Pow(dX, (_nu - p - 1.0)/2.0)
return Math.Pow(dX, (_degreeOfFreedom - p - 1.0)/2.0)
*Math.Exp(-0.5*siX.Trace())
/Math.Pow(2.0, _nu*p/2.0)
/Math.Pow(_chol.Determinant, _nu/2.0)
/Math.Pow(2.0, _degreeOfFreedom*p/2.0)
/Math.Pow(_chol.Determinant, _degreeOfFreedom/2.0)
/gp;
}
@ -261,7 +261,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public Matrix<double> Sample()
{
return DoSample(RandomSource, _nu, _s, _chol);
return DoSample(RandomSource, _degreeOfFreedom, _scale, _chol);
}
/// <summary>
@ -271,37 +271,37 @@ namespace MathNet.Numerics.Distributions
/// Applied Statistics, Vol. 21, No. 3 (1972), pp. 341-345
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="nu">The degrees of freedom.</param>
/// <param name="s">The scale matrix.</param>
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static Matrix<double> Sample(System.Random rnd, double nu, Matrix<double> s)
public static Matrix<double> Sample(System.Random rnd, double degreeOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreeOfFreedom, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return DoSample(rnd, nu, s, Cholesky<double>.Create(s));
return DoSample(rnd, degreeOfFreedom, scale, Cholesky<double>.Create(scale));
}
/// <summary>
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="nu">The nu parameter to use.</param>
/// <param name="s">The S parameter to use.</param>
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <param name="chol">The cholesky decomposition to use.</param>
/// <returns>a random number from the distribution.</returns>
static Matrix<double> DoSample(System.Random rnd, double nu, Matrix<double> s, Cholesky<double> chol)
static Matrix<double> DoSample(System.Random rnd, double degreeOfFreedom, Matrix<double> scale, Cholesky<double> chol)
{
var count = s.RowCount;
var count = scale.RowCount;
// First generate a lower triangular matrix with Sqrt(Chi-Squares) on the diagonal
// and normal distributed variables in the lower triangle.
var a = new DenseMatrix(count, count);
for (var d = 0; d < count; d++)
{
a.At(d, d, Math.Sqrt(Gamma.Sample(rnd, (nu - d)/2.0, 0.5)));
a.At(d, d, Math.Sqrt(Gamma.Sample(rnd, (degreeOfFreedom - d)/2.0, 0.5)));
}
for (var i = 1; i < count; i++)

18
src/Numerics/Distributions/Zipf.cs

@ -120,15 +120,6 @@ namespace MathNet.Numerics.Distributions
_n = n;
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets or sets the s parameter of the distribution.
/// </summary>
@ -147,6 +138,15 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_s, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? new System.Random(); }
}
/// <summary>
/// Gets the mean of the distribution.
/// </summary>

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

@ -1,4 +1,4 @@
// <copyright file="BetaTests.cs" company="Math.NET">
// <copyright file="BetaTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -90,8 +90,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Beta(1.0, 2.0);
Assert.AreEqual("Beta(A = 1, B = 2)", n.ToString());
var n = new Beta(1d, 2d);
Assert.AreEqual("Beta(α = 1, β = 2)", n.ToString());
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="CauchyTests.cs" company="Math.NET">
// <copyright file="CauchyTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -95,8 +95,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Cauchy(1.0, 2.0);
Assert.AreEqual("Cauchy(Location = 1, Scale = 2)", n.ToString());
var n = new Cauchy(1d, 2d);
Assert.AreEqual("Cauchy(x0 = 1, γ = 2)", n.ToString());
}
/// <summary>

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

@ -55,8 +55,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void CanCreateContinuousUniform()
{
var n = new ContinuousUniform();
Assert.AreEqual(0.0, n.Lower);
Assert.AreEqual(1.0, n.Upper);
Assert.AreEqual(0.0, n.LowerBound);
Assert.AreEqual(1.0, n.UpperBound);
}
/// <summary>
@ -74,8 +74,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void CanCreateContinuousUniform(double lower, double upper)
{
var n = new ContinuousUniform(lower, upper);
Assert.AreEqual(lower, n.Lower);
Assert.AreEqual(upper, n.Upper);
Assert.AreEqual(lower, n.LowerBound);
Assert.AreEqual(upper, n.UpperBound);
}
/// <summary>
@ -115,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{
new ContinuousUniform
{
Lower = lower
LowerBound = lower
};
}
@ -126,7 +126,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void SetBadLowerFails()
{
var n = new ContinuousUniform();
Assert.Throws<ArgumentOutOfRangeException>(() => n.Lower = 3.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.LowerBound = 3.0);
}
/// <summary>
@ -140,7 +140,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{
new ContinuousUniform
{
Upper = upper
UpperBound = upper
};
}
@ -151,7 +151,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void SetBadUpperFails()
{
var n = new ContinuousUniform();
Assert.Throws<ArgumentOutOfRangeException>(() => n.Upper = -1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.UpperBound = -1.0);
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="ErlangTests.cs" company="Math.NET">
// <copyright file="ErlangTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -61,7 +61,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{
var n = new Erlang(shape, invScale);
Assert.AreEqual(shape, n.Shape);
Assert.AreEqual(invScale, n.InvScale);
Assert.AreEqual(invScale, n.Rate);
}
/// <summary>
@ -92,9 +92,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(10, Double.PositiveInfinity)]
public void CanCreateErlangWithShapeInvScale(int shape, double invScale)
{
var n = Erlang.WithShapeInvScale(shape, invScale);
var n = Erlang.WithShapeRate(shape, invScale);
Assert.AreEqual(shape, n.Shape);
Assert.AreEqual(invScale, n.InvScale);
Assert.AreEqual(invScale, n.Rate);
}
/// <summary>
@ -121,8 +121,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Erlang(1, 2.0);
Assert.AreEqual("Erlang(Shape = 1, Inverse Scale = 2)", n.ToString());
var n = new Erlang(1, 2d);
Assert.AreEqual("Erlang(Shape = 1, λ = 2)", n.ToString());
}
/// <summary>
@ -193,7 +193,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{
new Erlang(1, 1.0)
{
InvScale = invScale
Rate = invScale
};
}
@ -204,7 +204,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void SetInvScaleFailsWithNegativeInvScale()
{
var n = new Erlang(1, 1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.InvScale = -1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.Rate = -1.0);
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="ExponentialTests.cs" company="Math.NET">
// <copyright file="ExponentialTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -58,7 +58,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void CanCreateExponential(double lambda)
{
var n = new Exponential(lambda);
Assert.AreEqual(lambda, n.Lambda);
Assert.AreEqual(lambda, n.Rate);
}
/// <summary>
@ -79,8 +79,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Exponential(2.0);
Assert.AreEqual("Exponential(Lambda = 2)", n.ToString());
var n = new Exponential(2d);
Assert.AreEqual("Exponential(λ = 2)", n.ToString());
}
/// <summary>
@ -97,7 +97,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{
new Exponential(1.0)
{
Lambda = lambda
Rate = lambda
};
}
@ -108,7 +108,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void SetLambdaFailsWithNegativeLambda()
{
var n = new Exponential(1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.Lambda = -1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.Rate = -1.0);
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="GammaTests.cs" company="Math.NET">
// <copyright file="GammaTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -63,7 +63,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{
var n = new Gamma(shape, invScale);
Assert.AreEqual(shape, n.Shape);
Assert.AreEqual(invScale, n.InvScale);
Assert.AreEqual(invScale, n.Rate);
}
/// <summary>
@ -94,9 +94,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(10.0, Double.PositiveInfinity)]
public void CanCreateGammaWithShapeInvScale(double shape, double invScale)
{
var n = Gamma.WithShapeInvScale(shape, invScale);
var n = Gamma.WithShapeRate(shape, invScale);
Assert.AreEqual(shape, n.Shape);
Assert.AreEqual(invScale, n.InvScale);
Assert.AreEqual(invScale, n.Rate);
}
/// <summary>
@ -123,8 +123,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Gamma(1.0, 2.0);
Assert.AreEqual("Gamma(Shape = 1, Inverse Scale = 2)", n.ToString());
var n = new Gamma(1d, 2d);
Assert.AreEqual("Gamma(α = 1, β = 2)", n.ToString());
}
/// <summary>
@ -197,7 +197,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{
new Gamma(1.0, 1.0)
{
InvScale = invScale
Rate = invScale
};
}
@ -208,7 +208,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void SetInvScaleFailsWithNegativeInvScale()
{
var n = new Gamma(1.0, 1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.InvScale = -1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.Rate = -1.0);
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="InverseGammaTests.cs" company="Math.NET">
// <copyright file="InverseGammaTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -87,8 +87,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new InverseGamma(1.1, 2.1);
Assert.AreEqual(String.Format("InverseGamma(Shape = {0}, Inverse Scale = {1})", n.Shape, n.Scale), n.ToString());
var n = new InverseGamma(1.1d, 2.1d);
Assert.AreEqual("InverseGamma(α = 1.1, β = 2.1)", n.ToString());
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="LaplaceTests.cs" company="Math.NET">
// <copyright file="LaplaceTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -81,8 +81,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Laplace(-1.0, 2.0);
Assert.AreEqual("Laplace(Location = -1, Scale = 2)", n.ToString());
var n = new Laplace(-1d, 2d);
Assert.AreEqual("Laplace(μ = -1, b = 2)", n.ToString());
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="LogNormalTests.cs" company="Math.NET">
// <copyright file="LogNormalTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -86,8 +86,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new LogNormal(1.0, 2.0);
Assert.AreEqual("LogNormal(Mu = 1, Sigma = 2)", n.ToString());
var n = new LogNormal(1d, 2d);
Assert.AreEqual("LogNormal(μ = 1, σ = 2)", n.ToString());
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="NormalTests.cs" company="Math.NET">
// <copyright file="NormalTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -151,8 +151,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Normal(1.0, 2.0);
Assert.AreEqual("Normal(Mean = 1, StdDev = 2)", n.ToString());
var n = new Normal(1d, 2d);
Assert.AreEqual("Normal(μ = 1, σ = 2)", n.ToString());
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="ParetoTests.cs" company="Math.NET">
// <copyright file="ParetoTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -87,8 +87,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Pareto(1.0, 2.0);
Assert.AreEqual("Pareto(Scale = 1, Shape = 2)", n.ToString());
var n = new Pareto(1d, 2d);
Assert.AreEqual("Pareto(xm = 1, α = 2)", n.ToString());
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="RayleighTests.cs" company="Math.NET">
// <copyright file="RayleighTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -79,8 +79,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Rayleigh(2.0);
Assert.AreEqual("Rayleigh(Scale = 2)", n.ToString());
var n = new Rayleigh(2d);
Assert.AreEqual("Rayleigh(σ = 2)", n.ToString());
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="StableTests.cs" company="Math.NET">
// <copyright file="StableTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -103,8 +103,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Stable(1.2, 0.3, 1.0, 2.0);
Assert.AreEqual(String.Format("Stable(Stability = {0}, Skewness = {1}, Scale = {2}, Location = {3})", n.Alpha, n.Beta, n.Scale, n.Location), n.ToString());
var n = new Stable(1.2d, 0.3d, 1d, 2d);
Assert.AreEqual("Stable(α = 1.2, β = 0.3, c = 1, μ = 2)", n.ToString());
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="WeibullTests.cs" company="Math.NET">
// <copyright file="WeibullTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -89,8 +89,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new Weibull(1.0, 2.0);
Assert.AreEqual("Weibull(Shape = 1, Scale = 2)", n.ToString());
var n = new Weibull(1d, 2d);
Assert.AreEqual("Weibull(k = 1, λ = 2)", n.ToString());
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="ConwayMaxwellPoissonTests.cs" company="Math.NET">
// <copyright file="ConwayMaxwellPoissonTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -50,7 +50,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// Can create <c>ConwayMaxwellPoisson</c>.
/// </summary>
/// <param name="lambda">Lambda value.</param>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
[TestCase(0.1, 0.0)]
[TestCase(1.0, 2.5)]
[TestCase(2.5, 3.0)]
@ -78,8 +78,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
[Test]
public void ValidateToString()
{
var d = new ConwayMaxwellPoisson(1.0, 2.0);
Assert.AreEqual("ConwayMaxwellPoisson(Lambda = 1, Nu = 2)", d.ToString());
var d = new ConwayMaxwellPoisson(1d, 2d);
Assert.AreEqual("ConwayMaxwellPoisson(λ = 1, ν = 2)", d.ToString());
}
/// <summary>
@ -99,9 +99,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
}
/// <summary>
/// Can set Nu.
/// Can set DegreeOfFreedom.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
[TestCase(0.0)]
[TestCase(3.0)]
[TestCase(10.0)]
@ -129,9 +129,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
}
/// <summary>
/// Set Nu with bad values fails.
/// Set DegreeOfFreedom with bad values fails.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
[TestCase(-0.1)]
[TestCase(-1.0)]
[TestCase(-10.0)]
@ -186,7 +186,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// Validate mean.
/// </summary>
/// <param name="lambda">Lambda value.</param>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="mean">Expected value.</param>
[TestCase(1, 1, 1.0)]
[TestCase(2, 1, 2.0)]
@ -224,7 +224,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// Validate probability.
/// </summary>
/// <param name="lambda">Lambda value.</param>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="x">Input X value.</param>
/// <param name="p">Expected value.</param>
[TestCase(1.0, 1.0, 1, 0.367879441171442)]
@ -243,7 +243,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// Validate probability log.
/// </summary>
/// <param name="lambda">Lambda value.</param>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="x">Input X value.</param>
/// <param name="pln">Expected value.</param>
[TestCase(1.0, 1.0, 1, -1.0)]
@ -283,7 +283,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
/// Validate cumulative distribution.
/// </summary>
/// <param name="lambda">Lambda value.</param>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="x">Input X value.</param>
/// <param name="cdf">Expected value.</param>
[TestCase(1.0, 1.0, 1, 0.735758882342885)]

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

@ -1,4 +1,4 @@
// <copyright file="InverseWishartTests.cs" company="Math.NET">
// <copyright file="InverseWishartTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -50,7 +50,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Can create inverse Wishart.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -60,12 +60,12 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var matrix = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
var d = new InverseWishart(nu, matrix);
Assert.AreEqual(nu, d.Nu);
for (var i = 0; i < d.S.RowCount; i++)
Assert.AreEqual(nu, d.DegreeOfFreedom);
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.S.ColumnCount; j++)
for (var j = 0; j < d.Scale.ColumnCount; j++)
{
Assert.AreEqual(matrix[i, j], d.S[i, j]);
Assert.AreEqual(matrix[i, j], d.Scale[i, j]);
}
}
}
@ -73,7 +73,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Fail create inverse Wishart with bad parameters.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -89,7 +89,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Fail create inverse Wishart with bad parameters.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(-1.0, 2)]
[TestCase(Double.NaN, 5)]
@ -135,8 +135,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
[Test]
public void ValidateToString()
{
var d = new InverseWishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2));
Assert.AreEqual("InverseWishart(Nu = 1, Rows = 2, Columns = 2)", d.ToString());
var d = new InverseWishart(1d, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2));
Assert.AreEqual("InverseWishart(ν = 1, Rows = 2, Columns = 2)", d.ToString());
}
/// <summary>
@ -149,13 +149,13 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
public void CanGetNu(double nu)
{
var d = new InverseWishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2));
Assert.AreEqual(nu, d.Nu);
Assert.AreEqual(nu, d.DegreeOfFreedom);
}
/// <summary>
/// Can set Nu.
/// Can set DegreeOfFreedom.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
[TestCase(1.0)]
[TestCase(2.0)]
[TestCase(5.0)]
@ -163,7 +163,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
{
new InverseWishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2))
{
Nu = nu
DegreeOfFreedom = nu
};
}
@ -181,7 +181,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
{
for (var j = 0; j < Order; j++)
{
Assert.AreEqual(matrix[i, j], d.S[i, j]);
Assert.AreEqual(matrix[i, j], d.Scale[i, j]);
}
}
}
@ -194,14 +194,14 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
{
new InverseWishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2))
{
S = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)
Scale = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)
};
}
/// <summary>
/// Validate mean.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -211,11 +211,11 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var d = new InverseWishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order));
var mean = d.Mean;
for (var i = 0; i < d.S.RowCount; i++)
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.S.ColumnCount; j++)
for (var j = 0; j < d.Scale.ColumnCount; j++)
{
Assert.AreEqual(d.S[i, j] * (1.0 / (nu - d.S.RowCount - 1.0)), mean[i, j]);
Assert.AreEqual(d.Scale[i, j] * (1.0 / (nu - d.Scale.RowCount - 1.0)), mean[i, j]);
}
}
}
@ -223,7 +223,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate mode.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -233,11 +233,11 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var d = new InverseWishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order));
var mode = d.Mode;
for (var i = 0; i < d.S.RowCount; i++)
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.S.ColumnCount; j++)
for (var j = 0; j < d.Scale.ColumnCount; j++)
{
Assert.AreEqual(d.S[i, j] * (1.0 / (nu + d.S.RowCount + 1.0)), mode[i, j]);
Assert.AreEqual(d.Scale[i, j] * (1.0 / (nu + d.Scale.RowCount + 1.0)), mode[i, j]);
}
}
}
@ -245,7 +245,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate variance.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -255,12 +255,12 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var d = new InverseWishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order));
var variance = d.Variance;
for (var i = 0; i < d.S.RowCount; i++)
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.S.ColumnCount; j++)
for (var j = 0; j < d.Scale.ColumnCount; j++)
{
var num1 = ((nu - d.S.RowCount + 1) * d.S[i, j] * d.S[i, j]) + ((nu - d.S.RowCount - 1) * d.S[i, i] * d.S[j, j]);
var num2 = (nu - d.S.RowCount) * (nu - d.S.RowCount - 1) * (nu - d.S.RowCount - 1) * (nu - d.S.RowCount - 3);
var num1 = ((nu - d.Scale.RowCount + 1) * d.Scale[i, j] * d.Scale[i, j]) + ((nu - d.Scale.RowCount - 1) * d.Scale[i, i] * d.Scale[j, j]);
var num2 = (nu - d.Scale.RowCount) * (nu - d.Scale.RowCount - 1) * (nu - d.Scale.RowCount - 1) * (nu - d.Scale.RowCount - 3);
Assert.AreEqual(num1 / num2, variance[i, j]);
}
}
@ -269,7 +269,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate density.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="density">Expected value.</param>
[TestCase(1.0, 0.03228684517430723)]
[TestCase(2.0, 0.018096748360719193)]

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

@ -264,7 +264,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale);
var pm = ng.PrecisionMarginal();
Assert.AreEqual(precShape, pm.Shape);
Assert.AreEqual(precInvScale, pm.InvScale);
Assert.AreEqual(precInvScale, pm.Rate);
}
/// <summary>

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

@ -50,7 +50,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Can create wishart.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -61,12 +61,12 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var d = new Wishart(nu, matrix);
Assert.AreEqual(nu, d.Nu);
for (var i = 0; i < d.S.RowCount; i++)
Assert.AreEqual(nu, d.DegreeOfFreedom);
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.S.ColumnCount; j++)
for (var j = 0; j < d.Scale.ColumnCount; j++)
{
Assert.AreEqual(matrix[i, j], d.S[i, j]);
Assert.AreEqual(matrix[i, j], d.Scale[i, j]);
}
}
}
@ -74,7 +74,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Fail create Wishart with bad parameters.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.0, 2)]
[TestCase(0.1, 5)]
@ -91,7 +91,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Fail create Wishart with bad parameters.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(-1.0, 2)]
[TestCase(Double.NaN, 5)]
@ -140,26 +140,26 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
public void ValidateToString()
{
var d = new Wishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2));
Assert.AreEqual("Wishart(Nu = 1, Rows = 2, Columns = 2)", d.ToString());
Assert.AreEqual("Wishart(DegreeOfFreedom = 1, Rows = 2, Columns = 2)", d.ToString());
}
/// <summary>
/// Can get Nu.
/// Can get DegreeOfFreedom.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
[TestCase(1.0)]
[TestCase(2.0)]
[TestCase(5.0)]
public void CanGetNu(double nu)
{
var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2));
Assert.AreEqual(nu, d.Nu);
Assert.AreEqual(nu, d.DegreeOfFreedom);
}
/// <summary>
/// Can set Nu.
/// Can set DegreeOfFreedom.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
[TestCase(1.0)]
[TestCase(2.0)]
[TestCase(5.0)]
@ -167,7 +167,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
{
new Wishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2))
{
Nu = nu
DegreeOfFreedom = nu
};
}
@ -185,7 +185,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
{
for (var j = 0; j < Order; j++)
{
Assert.AreEqual(matrix[i, j], d.S[i, j]);
Assert.AreEqual(matrix[i, j], d.Scale[i, j]);
}
}
}
@ -198,14 +198,14 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
{
new Wishart(1.0, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2))
{
S = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)
Scale = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(2)
};
}
/// <summary>
/// Validate mean.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -215,11 +215,11 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order));
var mean = d.Mean;
for (var i = 0; i < d.S.RowCount; i++)
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.S.ColumnCount; j++)
for (var j = 0; j < d.Scale.ColumnCount; j++)
{
Assert.AreEqual(nu * d.S[i, j], mean[i, j]);
Assert.AreEqual(nu * d.Scale[i, j], mean[i, j]);
}
}
}
@ -227,7 +227,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate mode.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -237,11 +237,11 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order));
var mode = d.Mode;
for (var i = 0; i < d.S.RowCount; i++)
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.S.ColumnCount; j++)
for (var j = 0; j < d.Scale.ColumnCount; j++)
{
Assert.AreEqual((nu - d.S.RowCount - 1.0) * d.S[i, j], mode[i, j]);
Assert.AreEqual((nu - d.Scale.RowCount - 1.0) * d.Scale[i, j], mode[i, j]);
}
}
}
@ -249,7 +249,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate variance.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -259,11 +259,11 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var d = new Wishart(nu, MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order));
var variance = d.Variance;
for (var i = 0; i < d.S.RowCount; i++)
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.S.ColumnCount; j++)
for (var j = 0; j < d.Scale.ColumnCount; j++)
{
Assert.AreEqual(nu * ((d.S[i, j] * d.S[i, j]) + (d.S[i, i] * d.S[j, j])), variance[i, j]);
Assert.AreEqual(nu * ((d.Scale[i, j] * d.Scale[i, j]) + (d.Scale[i, i] * d.Scale[j, j])), variance[i, j]);
}
}
}
@ -271,7 +271,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate density.
/// </summary>
/// <param name="nu">Nu parameter.</param>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="density">Expected value.</param>
[TestCase(1.0, 0.014644982561926487)]
[TestCase(2.0, 0.041042499311949421)]

Loading…
Cancel
Save