Browse Source

Distributions: s/ChiSquare/ChiSquared/, cleanup, docs

pull/163/head
Christoph Ruegg 13 years ago
parent
commit
707261922b
  1. 2
      src/Examples/ContinuousDistributions/ChiSquareDistribution.cs
  2. 12
      src/Examples/ContinuousDistributions/FisherSnedecorDistribution.cs
  3. 2
      src/Examples/Signals/Random.cs
  4. 4
      src/Examples/Statistics.cs
  5. 12
      src/Numerics/Distributions/Bernoulli.cs
  6. 12
      src/Numerics/Distributions/Beta.cs
  7. 12
      src/Numerics/Distributions/Binomial.cs
  8. 92
      src/Numerics/Distributions/Categorical.cs
  9. 12
      src/Numerics/Distributions/Cauchy.cs
  10. 64
      src/Numerics/Distributions/Chi.cs
  11. 77
      src/Numerics/Distributions/ChiSquared.cs
  12. 12
      src/Numerics/Distributions/ContinuousUniform.cs
  13. 34
      src/Numerics/Distributions/ConwayMaxwellPoisson.cs
  14. 4
      src/Numerics/Distributions/Dirichlet.cs
  15. 12
      src/Numerics/Distributions/DiscreteUniform.cs
  16. 14
      src/Numerics/Distributions/Erlang.cs
  17. 12
      src/Numerics/Distributions/Exponential.cs
  18. 48
      src/Numerics/Distributions/FisherSnedecor.cs
  19. 22
      src/Numerics/Distributions/Gamma.cs
  20. 12
      src/Numerics/Distributions/Geometric.cs
  21. 12
      src/Numerics/Distributions/Hypergeometric.cs
  22. 13
      src/Numerics/Distributions/IContinuousDistribution.cs
  23. 13
      src/Numerics/Distributions/IDiscreteDistribution.cs
  24. 4
      src/Numerics/Distributions/IDistribution.cs
  25. 6
      src/Numerics/Distributions/IUnivariateDistribution.cs
  26. 12
      src/Numerics/Distributions/InverseGamma.cs
  27. 38
      src/Numerics/Distributions/InverseWishart.cs
  28. 14
      src/Numerics/Distributions/Laplace.cs
  29. 44
      src/Numerics/Distributions/LogNormal.cs
  30. 28
      src/Numerics/Distributions/MatrixNormal.cs
  31. 4
      src/Numerics/Distributions/Multinomial.cs
  32. 32
      src/Numerics/Distributions/NegativeBinomial.cs
  33. 46
      src/Numerics/Distributions/Normal.cs
  34. 4
      src/Numerics/Distributions/NormalGamma.cs
  35. 12
      src/Numerics/Distributions/Pareto.cs
  36. 12
      src/Numerics/Distributions/Poisson.cs
  37. 12
      src/Numerics/Distributions/Rayleigh.cs
  38. 12
      src/Numerics/Distributions/Stable.cs
  39. 116
      src/Numerics/Distributions/StudentT.cs
  40. 24
      src/Numerics/Distributions/Weibull.cs
  41. 66
      src/Numerics/Distributions/Wishart.cs
  42. 12
      src/Numerics/Distributions/Zipf.cs
  43. 2
      src/Numerics/Numerics.csproj
  44. 40
      src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs
  45. 2
      src/UnitTests/DistributionTests/Continuous/ChiTests.cs
  46. 2
      src/UnitTests/DistributionTests/Continuous/ErlangTests.cs
  47. 24
      src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs
  48. 4
      src/UnitTests/DistributionTests/Continuous/StudentTTests.cs
  49. 18
      src/UnitTests/DistributionTests/Discrete/ConwayMaxwellPoissonTests.cs
  50. 24
      src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs
  51. 30
      src/UnitTests/DistributionTests/Multivariate/WishartTests.cs

2
src/Examples/ContinuousDistributions/ChiSquareDistribution.cs

@ -64,7 +64,7 @@ namespace Examples.ContinuousDistributionsExamples
public void Run()
{
// 1. Initialize the new instance of the ChiSquare distribution class with parameter dof = 1.
var chiSquare = new ChiSquare(1);
var chiSquare = new ChiSquared(1);
Console.WriteLine(@"1. Initialize the new instance of the ChiSquare distribution class with parameter DegreesOfFreedom = {0}", chiSquare.DegreesOfFreedom);
Console.WriteLine();

12
src/Examples/ContinuousDistributions/FisherSnedecorDistribution.cs

@ -63,9 +63,9 @@ namespace Examples.ContinuousDistributionsExamples
/// <a href="http://en.wikipedia.org/wiki/F-distribution">FisherSnedecor distribution</a>
public void Run()
{
// 1. Initialize the new instance of the FisherSnedecor distribution class with parameter DegreeOfFreedom1 = 50, DegreeOfFreedom2 = 20.
// 1. Initialize the new instance of the FisherSnedecor distribution class with parameter DegreesOfFreedom1 = 50, DegreesOfFreedom2 = 20.
var fisherSnedecor = new FisherSnedecor(50, 20);
Console.WriteLine(@"1. Initialize the new instance of the FisherSnedecor distribution class with parameters DegreeOfFreedom1 = {0}, DegreeOfFreedom2 = {1}", fisherSnedecor.DegreeOfFreedom1, fisherSnedecor.DegreeOfFreedom2);
Console.WriteLine(@"1. Initialize the new instance of the FisherSnedecor distribution class with parameters DegreesOfFreedom1 = {0}, DegreesOfFreedom2 = {1}", fisherSnedecor.DegreesOfFreedom1, fisherSnedecor.DegreesOfFreedom2);
Console.WriteLine();
// 2. Distributuion properties:
@ -125,8 +125,8 @@ namespace Examples.ContinuousDistributionsExamples
// 5. Generate 100000 samples of the FisherSnedecor(20, 10) distribution and display histogram
Console.WriteLine(@"5. Generate 100000 samples of the FisherSnedecor(20, 10) distribution and display histogram");
fisherSnedecor.DegreeOfFreedom1 = 20;
fisherSnedecor.DegreeOfFreedom2 = 10;
fisherSnedecor.DegreesOfFreedom1 = 20;
fisherSnedecor.DegreesOfFreedom2 = 10;
for (var i = 0; i < data.Length; i++)
{
data[i] = fisherSnedecor.Sample();
@ -137,8 +137,8 @@ namespace Examples.ContinuousDistributionsExamples
// 6. Generate 100000 samples of the FisherSnedecor(100, 100) distribution and display histogram
Console.WriteLine(@"6. Generate 100000 samples of the FisherSnedecor(100, 100) distribution and display histogram");
fisherSnedecor.DegreeOfFreedom1 = 100;
fisherSnedecor.DegreeOfFreedom2 = 100;
fisherSnedecor.DegreesOfFreedom1 = 100;
fisherSnedecor.DegreesOfFreedom2 = 100;
for (var i = 0; i < data.Length; i++)
{
data[i] = fisherSnedecor.Sample();

2
src/Examples/Signals/Random.cs

@ -96,7 +96,7 @@ namespace Examples.SignalsExamples
Console.WriteLine();
// 3. Get 10 random samples of f(x, y) = (x * y) / 2 using ChiSquare(10) distribution
var chiSquare = new ChiSquare(10);
var chiSquare = new ChiSquared(10);
result = SignalGenerator.Random(TwoDomainFunction, chiSquare, 10);
Console.WriteLine(@" 3. Get 10 random samples of f(x, y) = (x * y) / 2 using ChiSquare(10) distribution");
for (var i = 0; i < result.Length; i++)

4
src/Examples/Statistics.cs

@ -65,7 +65,7 @@ namespace Examples
public void Run()
{
// 1. Initialize the new instance of the ChiSquare distribution class with parameter dof = 5.
var chiSquare = new ChiSquare(5);
var chiSquare = new ChiSquared(5);
Console.WriteLine(@"1. Initialize the new instance of the ChiSquare distribution class with parameter DegreesOfFreedom = {0}", chiSquare.DegreesOfFreedom);
Console.WriteLine(@"{0} distributuion properties:", chiSquare);
Console.WriteLine(@"{0} - Largest element", chiSquare.Maximum.ToString(" #0.00000;-#0.00000"));
@ -111,7 +111,7 @@ namespace Examples
Console.WriteLine();
// Generate 1000 samples of the ChiSquare(2.5) distribution
var chiSquareB = new ChiSquare(2);
var chiSquareB = new ChiSquared(2);
var dataB = new double[1000];
for (var i = 0; i < data.Length; i++)
{

12
src/Numerics/Distributions/Bernoulli.cs

@ -199,7 +199,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -219,7 +219,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -234,7 +234,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -259,7 +259,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random source to use.</param>
/// <param name="p">The probability of generating a one.</param>
/// <returns>A random sample from the Bernoulli distribution.</returns>
internal static int SampleUnchecked(System.Random rnd, double p)
static int SampleUnchecked(System.Random rnd, double p)
{
if (rnd.NextDouble() < p)
{
@ -275,7 +275,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A sample from the Bernoulli distribution.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _p);
return SampleUnchecked(_random, _p);
}
/// <summary>
@ -286,7 +286,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _p);
yield return SampleUnchecked(_random, _p);
}
}

12
src/Numerics/Distributions/Beta.cs

@ -346,7 +346,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -402,7 +402,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -461,7 +461,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -527,7 +527,7 @@ namespace MathNet.Numerics.Distributions
/// <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)
static double SampleUnchecked(System.Random rnd, double a, double b)
{
var x = Gamma.SampleUnchecked(rnd, a, 1.0);
var y = Gamma.SampleUnchecked(rnd, b, 1.0);
@ -540,7 +540,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _shapeA, _shapeB);
return SampleUnchecked(_random, _shapeA, _shapeB);
}
/// <summary>
@ -551,7 +551,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _shapeA, _shapeB);
yield return SampleUnchecked(_random, _shapeA, _shapeB);
}
}

12
src/Numerics/Distributions/Binomial.cs

@ -245,7 +245,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -285,7 +285,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -325,7 +325,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -357,7 +357,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="p">The success probability of a trial; must be in the interval [0.0, 1.0].</param>
/// <param name="n">The number of trials; must be positive.</param>
/// <returns>The number of successful trials.</returns>
internal static int SampleUnchecked(System.Random rnd, double p, int n)
static int SampleUnchecked(System.Random rnd, double p, int n)
{
var k = 0;
for (var i = 0; i < n; i++)
@ -374,7 +374,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>The number of successes in N trials.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _p, _trials);
return SampleUnchecked(_random, _p, _trials);
}
/// <summary>
@ -385,7 +385,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _p, _trials);
yield return SampleUnchecked(_random, _p, _trials);
}
}

92
src/Numerics/Distributions/Categorical.cs

@ -285,7 +285,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -305,7 +305,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -325,7 +325,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -344,6 +344,58 @@ namespace MathNet.Numerics.Distributions
return _cdfUnnormalized[(int) Math.Floor(x)]/_cdfUnnormalized[_cdfUnnormalized.Length - 1];
}
/// <summary>
/// Computes the inverse of the cumulative distribution function (InvCDF) for the distribution
/// at the given probability.
/// </summary>
/// <param name="probability">A real number between 0 and 1.</param>
/// <returns>An integer between 0 and the size of the categorical (exclusive), that corresponds to the inverse CDF for the given probability.</returns>
public int InverseCumulativeDistribution(double probability)
{
if (probability < 0.0 || probability > 1.0 || Double.IsNaN(probability))
{
throw new ArgumentOutOfRangeException("probability");
}
var denormalizedProbability = probability * _cdfUnnormalized[_cdfUnnormalized.Length - 1];
int idx = Array.BinarySearch(_cdfUnnormalized, denormalizedProbability);
if (idx < 0)
{
idx = ~idx;
}
return idx;
}
/// <summary>
/// Computes the inverse of the cumulative distribution function (InvCDF) for the distribution
/// at the given probability.
/// </summary>
/// <param name="cdfUnnormalized">An array corresponding to a CDF for a categorical distribution. Not assumed to be normalized.</param>
/// <param name="probability">A real number between 0 and 1.</param>
/// <returns>An integer between 0 and the size of the categorical (exclusive), that corresponds to the inverse CDF for the given probability.</returns>
public static int InverseCumulativeDistribution(double[] cdfUnnormalized, double probability)
{
if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
if (probability < 0.0 || probability > 1.0 || Double.IsNaN(probability))
{
throw new ArgumentOutOfRangeException("probability");
}
var denormalizedProbability = probability * cdfUnnormalized[cdfUnnormalized.Length - 1];
int idx = Array.BinarySearch(cdfUnnormalized, denormalizedProbability);
if (idx < 0)
{
idx = ~idx;
}
return idx;
}
/// <summary>
/// Computes the cumulative distribution function. This method performs no parameter checking.
/// If the probability mass was normalized, the resulting cumulative distribution is normalized as well (up to numerical errors).
@ -389,7 +441,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>The number of successful trials.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _cdfUnnormalized);
return SampleUnchecked(_random, _cdfUnnormalized);
}
/// <summary>
@ -400,7 +452,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _cdfUnnormalized);
yield return SampleUnchecked(_random, _cdfUnnormalized);
}
}
@ -475,35 +527,5 @@ namespace MathNet.Numerics.Distributions
yield return SampleUnchecked(rnd, cdf);
}
}
/// <summary>
/// Returns the inverse of the distribution function for the categorical distribution
/// specified by the given normalized CDF, for the given probability.
/// </summary>
/// <param name="cdfUnnormalized">An array corresponding to a CDF for a categorical distribution. Not assumed to be normalized.</param>
/// <param name="probability">A real number between 0 and 1.</param>
/// <returns>An integer between 0 and the size of the categorical (exclusive),
/// that corresponds to the inverse CDF for the given probability.</returns>
public static int InverseCumulativeDistribution(double[] cdfUnnormalized, double probability)
{
if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
if (probability < 0.0 || probability > 1.0 || Double.IsNaN(probability))
{
throw new ArgumentOutOfRangeException("probability");
}
var denormalizedProbability = probability*cdfUnnormalized[cdfUnnormalized.Length - 1];
int idx = Array.BinarySearch(cdfUnnormalized, denormalizedProbability);
if (idx < 0)
{
idx = ~idx;
}
return idx;
}
}
}

12
src/Numerics/Distributions/Cauchy.cs

@ -216,7 +216,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -226,7 +226,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -236,7 +236,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -252,7 +252,7 @@ namespace MathNet.Numerics.Distributions
/// <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)
static double SampleUnchecked(System.Random rnd, double location, double scale)
{
var u = rnd.NextDouble();
return location + (scale*Math.Tan(Constants.Pi*(u - 0.5)));
@ -264,7 +264,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _location, _scale);
return SampleUnchecked(_random, _location, _scale);
}
/// <summary>
@ -275,7 +275,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _location, _scale);
yield return SampleUnchecked(_random, _location, _scale);
}
}

64
src/Numerics/Distributions/Chi.cs

@ -55,22 +55,22 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Chi"/> class.
/// </summary>
/// <param name="dof">The degrees of freedom for the Chi distribution.</param>
public Chi(double dof)
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
public Chi(double freedom)
{
_random = new System.Random();
SetParameters(dof);
SetParameters(freedom);
}
/// <summary>
/// Initializes a new instance of the <see cref="Chi"/> class.
/// </summary>
/// <param name="dof">The degrees of freedom for the Chi distribution.</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Chi(double dof, System.Random randomSource)
public Chi(double freedom, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(dof);
SetParameters(freedom);
}
/// <summary>
@ -79,36 +79,36 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Chi(DoF = " + _freedom + ")";
return "Chi(k = " + _freedom + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="dof">The degrees of freedom for the Chi distribution.</param>
/// <param name="freedom">The degrees of freedom for the Chi distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double dof)
static bool IsValidParameterSet(double freedom)
{
return dof > 0.0;
return freedom > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="dof">The degrees of freedom for the Chi distribution.</param>
/// <param name="freedom">The degrees of freedom for the Chi distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double dof)
void SetParameters(double freedom)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
if (Control.CheckDistributionParameters && !IsValidParameterSet(freedom))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_freedom = dof;
_freedom = freedom;
}
/// <summary>
/// Gets or sets the degrees of freedom of the Chi distribution.
/// Gets or sets the degrees of freedom (k) of the Chi distribution.
/// </summary>
public double DegreesOfFreedom
{
@ -210,7 +210,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -220,7 +220,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -230,7 +230,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -243,12 +243,12 @@ namespace MathNet.Numerics.Distributions
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">Degrees of Freedom</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, int dof)
static double SampleUnchecked(System.Random rnd, int freedom)
{
double sum = 0;
for (var i = 0; i < dof; i++)
for (var i = 0; i < freedom; i++)
{
sum += Math.Pow(Normal.Sample(rnd, 0.0, 1.0), 2);
}
@ -262,7 +262,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, (int) _freedom);
return SampleUnchecked(_random, (int) _freedom);
}
/// <summary>
@ -271,10 +271,10 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public IEnumerable<double> Samples()
{
var dof = (int) _freedom;
var freedom = (int)_freedom;
while (true)
{
yield return SampleUnchecked(RandomSource, dof);
yield return SampleUnchecked(_random, freedom);
}
}
@ -282,34 +282,34 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">Degrees of Freedom</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, int dof)
public static double Sample(System.Random rnd, int freedom)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
if (Control.CheckDistributionParameters && !IsValidParameterSet(freedom))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return SampleUnchecked(rnd, dof);
return SampleUnchecked(rnd, freedom);
}
/// <summary>
/// Generates a sequence of samples from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">Degrees of Freedom</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, int dof)
public static IEnumerable<double> Samples(System.Random rnd, int freedom)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
if (Control.CheckDistributionParameters && !IsValidParameterSet(freedom))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return SampleUnchecked(rnd, dof);
yield return SampleUnchecked(rnd, freedom);
}
}
}

77
src/Numerics/Distributions/ChiSquare.cs → src/Numerics/Distributions/ChiSquared.cs

@ -35,7 +35,7 @@ using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions
{
/// <summary>
/// Continuous Univariate ChiSquare distribution.
/// Continuous Univariate Chi-Squared distribution.
/// This distribution is a sum of the squares of k independent standard normal random variables.
/// <a href="http://en.wikipedia.org/wiki/Chi-square_distribution">Wikipedia - ChiSquare distribution</a>.
/// </summary>
@ -44,31 +44,31 @@ namespace MathNet.Numerics.Distributions
/// <para>The statistics classes will check all the incoming parameters whether they are in the allowed
/// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class ChiSquare : IContinuousDistribution
public class ChiSquared : IContinuousDistribution
{
System.Random _random;
double _freedom;
/// <summary>
/// Initializes a new instance of the <see cref="ChiSquare"/> class.
/// Initializes a new instance of the <see cref="ChiSquared"/> class.
/// </summary>
/// <param name="dof">The degrees of freedom for the ChiSquare distribution.</param>
public ChiSquare(double dof)
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
public ChiSquared(double freedom)
{
_random = new System.Random();
SetParameters(dof);
SetParameters(freedom);
}
/// <summary>
/// Initializes a new instance of the <see cref="ChiSquare"/> class.
/// Initializes a new instance of the <see cref="ChiSquared"/> class.
/// </summary>
/// <param name="dof">The degrees of freedom for the ChiSquare distribution.</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public ChiSquare(double dof, System.Random randomSource)
public ChiSquared(double freedom, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(dof);
SetParameters(freedom);
}
/// <summary>
@ -77,36 +77,36 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "ChiSquare(DoF = " + _freedom + ")";
return "ChiSquared(k = " + _freedom + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="dof">The degrees of freedom for the <c>ChiSquare</c> distribution.</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double dof)
static bool IsValidParameterSet(double freedom)
{
return dof > 0 && !Double.IsNaN(dof);
return freedom > 0 && !Double.IsNaN(freedom);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="dof">The degrees of freedom for the <c>ChiSquare</c> distribution.</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double dof)
void SetParameters(double freedom)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
if (Control.CheckDistributionParameters && !IsValidParameterSet(freedom))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_freedom = dof;
_freedom = freedom;
}
/// <summary>
/// Gets or sets the degrees of freedom of the <c>ChiSquare</c> distribution.
/// Gets or sets the degrees of freedom (k) of the Chi-Squared distribution.
/// </summary>
public double DegreesOfFreedom
{
@ -196,7 +196,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -206,7 +206,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -216,7 +216,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -229,24 +229,25 @@ namespace MathNet.Numerics.Distributions
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">The degrees of freedom.</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double dof)
static double SampleUnchecked(System.Random rnd, double freedom)
{
//Use the simple method if the dof is an integer anyway
if (Math.Floor(dof) == dof && dof < Int32.MaxValue)
// Use the simple method if the degrees if freedom is an integer anyway
if (Math.Floor(freedom) == freedom && freedom < Int32.MaxValue)
{
double sum = 0;
var n = (int) dof;
var n = (int) freedom;
for (var i = 0; i < n; i++)
{
sum += Math.Pow(Normal.Sample(rnd, 0.0, 1.0), 2);
}
return sum;
}
//Call the gamma function (see http://en.wikipedia.org/wiki/Gamma_distribution#Specializations
//for a justification)
return Gamma.SampleUnchecked(rnd, dof/2.0, .5);
return Gamma.SampleUnchecked(rnd, freedom/2.0, .5);
}
/// <summary>
@ -255,7 +256,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _freedom);
return SampleUnchecked(_random, _freedom);
}
/// <summary>
@ -266,7 +267,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _freedom);
yield return SampleUnchecked(_random, _freedom);
}
}
@ -274,34 +275,34 @@ namespace MathNet.Numerics.Distributions
/// Generates a sample from the <c>ChiSquare</c> distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">The degrees of freedom.</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <returns>a sample from the distribution. </returns>
public static double Sample(System.Random rnd, double dof)
public static double Sample(System.Random rnd, double freedom)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
if (Control.CheckDistributionParameters && !IsValidParameterSet(freedom))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return SampleUnchecked(rnd, dof);
return SampleUnchecked(rnd, freedom);
}
/// <summary>
/// Generates a sequence of samples from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">The degrees of freedom.</param>
/// <param name="freedom">The degrees of freedom (k) of the distribution.</param>
/// <returns>a sample from the distribution. </returns>
public static IEnumerable<double> Samples(System.Random rnd, double dof)
public static IEnumerable<double> Samples(System.Random rnd, double freedom)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
if (Control.CheckDistributionParameters && !IsValidParameterSet(freedom))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return SampleUnchecked(rnd, dof);
yield return SampleUnchecked(rnd, freedom);
}
}
}

12
src/Numerics/Distributions/ContinuousUniform.cs

@ -223,7 +223,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -238,7 +238,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -253,7 +253,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -279,7 +279,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="lower">The lower bound of the uniform random variable.</param>
/// <param name="upper">The upper bound of the uniform random variable.</param>
/// <returns>a uniformly distributed random number.</returns>
internal static double SampleUnchecked(System.Random rnd, double lower, double upper)
static double SampleUnchecked(System.Random rnd, double lower, double upper)
{
return lower + (rnd.NextDouble()*(upper - lower));
}
@ -290,7 +290,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _lower, _upper);
return SampleUnchecked(_random, _lower, _upper);
}
/// <summary>
@ -301,7 +301,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _lower, _upper);
yield return SampleUnchecked(_random, _lower, _upper);
}
}

34
src/Numerics/Distributions/ConwayMaxwellPoisson.cs

@ -83,7 +83,7 @@ namespace MathNet.Numerics.Distributions
/// 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="nu">The rate of decay (ν) parameter.</param>
public ConwayMaxwellPoisson(double lambda, double nu)
{
_random = new System.Random();
@ -94,7 +94,7 @@ namespace MathNet.Numerics.Distributions
/// 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="nu">The rate of decay (ν) 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)
{
@ -115,7 +115,7 @@ namespace MathNet.Numerics.Distributions
/// 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="nu">The rate of decay (ν) parameter.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double lambda, double nu)
{
@ -126,7 +126,7 @@ namespace MathNet.Numerics.Distributions
/// 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="nu">The rate of decay (ν) parameter.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double lambda, double nu)
{
@ -142,7 +142,6 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets or sets the lambda (λ) parameter.
/// </summary>
/// <value>The value of the lambda parameter.</value>
public double Lambda
{
get { return _lambda; }
@ -150,9 +149,8 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the DegreeOfFreedom (ν) parameter.
/// Gets or sets the rate of decay (ν) parameter.
/// </summary>
/// <value>The value of the DegreeOfFreedom parameter.</value>
public double Nu
{
get { return _nu; }
@ -348,7 +346,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -358,7 +356,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -368,7 +366,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -403,8 +401,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Computes an approximate normalization constant for the CMP distribution.
/// </summary>
/// <param name="lambda">The lambda parameter for the CMP distribution.</param>
/// <param name="nu">The nu parameter for the CMP distribution.</param>
/// <param name="lambda">The lambda (λ) parameter for the CMP distribution.</param>
/// <param name="nu">The rate of decay (ν) parameter for the CMP distribution.</param>
/// <returns>
/// an approximate normalization constant for the CMP distribution.
/// </returns>
@ -446,12 +444,12 @@ namespace MathNet.Numerics.Distributions
/// </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="nu">The rate of decay (ν) 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"/>.
/// </returns>
internal static int SampleUnchecked(System.Random rnd, double lambda, double nu, double z)
static int SampleUnchecked(System.Random rnd, double lambda, double nu, double z)
{
var u = rnd.NextDouble();
var p = 1.0/z;
@ -474,7 +472,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _lambda, _nu, Z);
return SampleUnchecked(_random, _lambda, _nu, Z);
}
/// <summary>
@ -487,7 +485,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _lambda, _nu, Z);
yield return SampleUnchecked(_random, _lambda, _nu, Z);
}
}
@ -496,7 +494,7 @@ namespace MathNet.Numerics.Distributions
/// </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="nu">The rate of decay (ν) parameter.</param>
public static int Sample(System.Random rnd, double lambda, double nu)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda, nu))
@ -513,7 +511,7 @@ namespace MathNet.Numerics.Distributions
/// </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="nu">The rate of decay (ν) parameter.</param>
public static IEnumerable<int> Samples(System.Random rnd, double lambda, double nu)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda, nu))

4
src/Numerics/Distributions/Dirichlet.cs

@ -129,7 +129,7 @@ namespace MathNet.Numerics.Distributions
/// </param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c>
/// otherwise.</returns>
public static bool IsValidParameterSet(double[] alpha)
static bool IsValidParameterSet(double[] alpha)
{
var allzero = true;
@ -317,7 +317,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A sample from this distribution.</returns>
public double[] Sample()
{
return Sample(RandomSource, _alpha);
return Sample(_random, _alpha);
}
/// <summary>

12
src/Numerics/Distributions/DiscreteUniform.cs

@ -214,7 +214,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -229,7 +229,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -244,7 +244,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -270,7 +270,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="lower">The lower bound of the uniform random variable.</param>
/// <param name="upper">The upper bound of the uniform random variable.</param>
/// <returns>A random sample from the discrete uniform distribution.</returns>
internal static int SampleUnchecked(System.Random rnd, int lower, int upper)
static int SampleUnchecked(System.Random rnd, int lower, int upper)
{
return (rnd.Next()%(upper - lower + 1)) + lower;
}
@ -281,7 +281,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _lower, _upper);
return SampleUnchecked(_random, _lower, _upper);
}
/// <summary>
@ -292,7 +292,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _lower, _upper);
yield return SampleUnchecked(_random, _lower, _upper);
}
}

14
src/Numerics/Distributions/Erlang.cs

@ -105,7 +105,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Erlang(Shape = " + _shape + ", λ = " + _rate + ")";
return "Erlang(k = " + _shape + ", λ = " + _rate + ")";
}
/// <summary>
@ -335,7 +335,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -360,7 +360,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -385,7 +385,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -414,7 +414,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <returns>A sample from a Erlang distributed random variable.</returns>
internal static double SampleUnchecked(System.Random rnd, double shape, double invScale)
static double SampleUnchecked(System.Random rnd, double shape, double invScale)
{
if (Double.IsPositiveInfinity(invScale))
{
@ -464,7 +464,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _shape, _rate);
return SampleUnchecked(_random, _shape, _rate);
}
/// <summary>
@ -475,7 +475,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _shape, _rate);
yield return SampleUnchecked(_random, _shape, _rate);
}
}

12
src/Numerics/Distributions/Exponential.cs

@ -196,7 +196,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -211,7 +211,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -221,7 +221,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -241,7 +241,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</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 rate)
static double SampleUnchecked(System.Random rnd, double rate)
{
var r = rnd.NextDouble();
while (r == 0.0)
@ -258,7 +258,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _rate);
return SampleUnchecked(_random, _rate);
}
/// <summary>
@ -269,7 +269,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _rate);
yield return SampleUnchecked(_random, _rate);
}
}

48
src/Numerics/Distributions/FisherSnedecor.cs

@ -54,8 +54,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="FisherSnedecor"/> class.
/// </summary>
/// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param>
/// <param name="d1">The first degree of freedom (d1) of the distribution.</param>
/// <param name="d2">The second degree of freedom (d2) of the distribution.</param>
public FisherSnedecor(double d1, double d2)
{
_random = new System.Random();
@ -65,8 +65,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="FisherSnedecor"/> class.
/// </summary>
/// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param>
/// <param name="d1">The first degree of freedom (d1) of the distribution.</param>
/// <param name="d2">The second degree of freedom (d2) of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public FisherSnedecor(double d1, double d2, System.Random randomSource)
{
@ -80,14 +80,14 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "FisherSnedecor(DegreeOfFreedom1 = " + _freedom1 + ", DegreeOfFreedom2 = " + _freedom2 + ")";
return "FisherSnedecor(d1 = " + _freedom1 + ", d2 = " + _freedom2 + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param>
/// <param name="d1">The first degree of freedom (d1) of the distribution.</param>
/// <param name="d2">The second degree of freedom (d2) of the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double d1, double d2)
{
@ -97,8 +97,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param>
/// <param name="d1">The first degree of freedom (d1) of the distribution.</param>
/// <param name="d2">The second degree of freedom (d2) of the distribution.</param>
void SetParameters(double d1, double d2)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(d1, d2))
@ -113,7 +113,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets or sets the first parameter - degree of freedom.
/// </summary>
public double DegreeOfFreedom1
public double DegreesOfFreedom1
{
get { return _freedom1; }
set { SetParameters(value, _freedom2); }
@ -122,7 +122,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets or sets the second parameter - degree of freedom.
/// </summary>
public double DegreeOfFreedom2
public double DegreesOfFreedom2
{
get { return _freedom2; }
set { SetParameters(_freedom1, value); }
@ -242,7 +242,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -252,7 +252,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -262,7 +262,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -275,12 +275,12 @@ namespace MathNet.Numerics.Distributions
/// Generates one sample from the <c>FisherSnedecor</c> distribution without parameter checking.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param>
/// <param name="d1">The first degree of freedom (d1) of the distribution.</param>
/// <param name="d2">The second degree of freedom (d2) of the distribution.</param>
/// <returns>a <c>FisherSnedecor</c> distributed random number.</returns>
internal static double SampleUnchecked(System.Random rnd, double d1, double d2)
static double SampleUnchecked(System.Random rnd, double d1, double d2)
{
return (ChiSquare.Sample(rnd, d1)/d1)/(ChiSquare.Sample(rnd, d2)/d2);
return (ChiSquared.Sample(rnd, d1)/d1)/(ChiSquared.Sample(rnd, d2)/d2);
}
/// <summary>
@ -289,7 +289,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _freedom1, _freedom2);
return SampleUnchecked(_random, _freedom1, _freedom2);
}
/// <summary>
@ -300,7 +300,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _freedom1, _freedom2);
yield return SampleUnchecked(_random, _freedom1, _freedom2);
}
}
@ -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="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param>
/// <param name="d1">The first degree of freedom (d1) of the distribution.</param>
/// <param name="d2">The second degree of freedom (d2) of the distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rnd, double d1, double d2)
{
@ -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="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param>
/// <param name="d1">The first degree of freedom (d1) of the distribution.</param>
/// <param name="d2">The second degree of freedom (d2) of the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rnd, double d1, double d2)
{

22
src/Numerics/Distributions/Gamma.cs

@ -336,7 +336,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -361,7 +361,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -386,7 +386,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -465,7 +465,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _shape, _rate);
return SampleUnchecked(_random, _shape, _rate);
}
/// <summary>
@ -476,35 +476,35 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _shape, _rate);
yield return SampleUnchecked(_random, _shape, _rate);
}
}
/// <summary>
/// Generates a sample from the Gamma distribution.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="rnd">The random number generator to use.</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 rate)
public static double Sample(System.Random rnd, double shape, double rate)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, rate))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return SampleUnchecked(rng, shape, rate);
return SampleUnchecked(rnd, 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="rnd">The random number generator to use.</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 rate)
public static IEnumerable<double> Samples(System.Random rnd, double shape, double rate)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, rate))
{
@ -513,7 +513,7 @@ namespace MathNet.Numerics.Distributions
while (true)
{
yield return SampleUnchecked(rng, shape, rate);
yield return SampleUnchecked(rnd, shape, rate);
}
}
}

12
src/Numerics/Distributions/Geometric.cs

@ -202,7 +202,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -217,7 +217,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -232,7 +232,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -249,7 +249,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>
/// One sample from the distribution implied by <paramref name="p"/>.
/// </returns>
internal static int SampleUnchecked(System.Random rnd, double p)
static int SampleUnchecked(System.Random rnd, double p)
{
return p == 1.0 ? 1 : (int) Math.Ceiling(-Math.Log(1.0 - rnd.NextDouble(), 1.0 - p));
}
@ -260,7 +260,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A sample from the Geometric distribution.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _p);
return SampleUnchecked(_random, _p);
}
/// <summary>
@ -271,7 +271,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _p);
yield return SampleUnchecked(_random, _p);
}
}

12
src/Numerics/Distributions/Hypergeometric.cs

@ -260,7 +260,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -270,7 +270,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -280,7 +280,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -313,7 +313,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="success">The number successes within the population (K, M).</param>
/// <param name="draws">The n parameter of the distribution.</param>
/// <returns>a random number from the Hypergeometric distribution.</returns>
internal static int SampleUnchecked(System.Random rnd, int population, int success, int draws)
static int SampleUnchecked(System.Random rnd, int population, int success, int draws)
{
var x = 0;
@ -340,7 +340,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>The number of successes in n trials.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _population, _success, _draws);
return SampleUnchecked(_random, _population, _success, _draws);
}
/// <summary>
@ -351,7 +351,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _population, _success, _draws);
yield return SampleUnchecked(_random, _population, _success, _draws);
}
}

13
src/Numerics/Distributions/IContinuousDistribution.cs

@ -33,8 +33,9 @@ namespace MathNet.Numerics.Distributions
using System.Collections.Generic;
/// <summary>
/// The interface for continuous univariate distributions.
/// Continuous Univariate Probability Distribution.
/// </summary>
/// <seealso cref="IDiscreteDistribution"/>
public interface IContinuousDistribution : IUnivariateDistribution
{
/// <summary>
@ -48,24 +49,24 @@ namespace MathNet.Numerics.Distributions
double Median { get; }
/// <summary>
/// Gets the smallest element in the domain of the distributions which can be represented by a double.
/// Gets the smallest element in the domain of the distribution which can be represented by a double.
/// </summary>
double Minimum { get; }
/// <summary>
/// Gets the largest element in the domain of the distributions which can be represented by a double.
/// Gets the largest element in the domain of the distribution which can be represented by a double.
/// </summary>
double Maximum { get; }
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
double Density(double x);
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -80,7 +81,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Draws a sequence of random samples from the distribution.
/// </summary>
/// <returns>a sequence of samples from the distribution.</returns>
/// <returns>an infinite sequence of samples from the distribution.</returns>
IEnumerable<double> Samples();
}
}

13
src/Numerics/Distributions/IDiscreteDistribution.cs

@ -33,8 +33,9 @@ namespace MathNet.Numerics.Distributions
using System.Collections.Generic;
/// <summary>
/// The interface for discrete univariate distributions.
/// Discrete Univariate Probability Distribution.
/// </summary>
/// <seealso cref="IContinuousDistribution"/>
public interface IDiscreteDistribution : IUnivariateDistribution
{
/// <summary>
@ -48,24 +49,24 @@ namespace MathNet.Numerics.Distributions
int Median { get; }
/// <summary>
/// Gets the smallest element in the domain of the distributions which can be represented by an integer.
/// Gets the smallest element in the domain of the distribution which can be represented by an integer.
/// </summary>
int Minimum { get; }
/// <summary>
/// Gets the largest element in the domain of the distributions which can be represented by an integer.
/// Gets the largest element in the domain of the distribution which can be represented by an integer.
/// </summary>
int Maximum { get; }
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
double Probability(int k);
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -80,7 +81,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Draws a sequence of random samples from the distribution.
/// </summary>
/// <returns>a sequence of samples from the distribution.</returns>
/// <returns>an infinite sequence of samples from the distribution.</returns>
IEnumerable<int> Samples();
}
}

4
src/Numerics/Distributions/IDistribution.cs

@ -31,8 +31,10 @@
namespace MathNet.Numerics.Distributions
{
/// <summary>
/// The common interface for all distributions.
/// Probability Distribution.
/// </summary>
/// <seealso cref="IContinuousDistribution"/>
/// <seealso cref="IDiscreteDistribution"/>
public interface IDistribution
{
/// <summary>

6
src/Numerics/Distributions/IUnivariateDistribution.cs

@ -31,8 +31,10 @@
namespace MathNet.Numerics.Distributions
{
/// <summary>
/// The interface for univariate distributions.
/// Univariate Probability Distribution.
/// </summary>
/// <seealso cref="IContinuousDistribution"/>
/// <seealso cref="IDiscreteDistribution"/>
public interface IUnivariateDistribution : IDistribution
{
/// <summary>
@ -61,7 +63,7 @@ namespace MathNet.Numerics.Distributions
double Skewness { get; }
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>

12
src/Numerics/Distributions/InverseGamma.cs

@ -237,7 +237,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -252,7 +252,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -262,7 +262,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -278,7 +278,7 @@ namespace MathNet.Numerics.Distributions
/// <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)
static double SampleUnchecked(System.Random rnd, double shape, double scale)
{
return 1.0/Gamma.Sample(rnd, shape, scale);
}
@ -289,7 +289,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _shape, _scale);
return SampleUnchecked(_random, _shape, _scale);
}
/// <summary>
@ -300,7 +300,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _shape, _scale);
yield return SampleUnchecked(_random, _shape, _scale);
}
}

38
src/Numerics/Distributions/InverseWishart.cs

@ -61,24 +61,24 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="InverseWishart"/> class.
/// </summary>
/// <param name="degreeOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
public InverseWishart(double degreeOfFreedom, Matrix<double> scale)
public InverseWishart(double degreesOfFreedom, Matrix<double> scale)
{
_random = new System.Random();
SetParameters(degreeOfFreedom, scale);
SetParameters(degreesOfFreedom, scale);
}
/// <summary>
/// Initializes a new instance of the <see cref="InverseWishart"/> class.
/// </summary>
/// <param name="degreeOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public InverseWishart(double degreeOfFreedom, Matrix<double> scale, System.Random randomSource)
public InverseWishart(double degreesOfFreedom, Matrix<double> scale, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(degreeOfFreedom, scale);
SetParameters(degreesOfFreedom, scale);
}
/// <summary>
@ -93,10 +93,10 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="degreeOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double degreeOfFreedom, Matrix<double> scale)
static bool IsValidParameterSet(double degreesOfFreedom, Matrix<double> scale)
{
if (scale.RowCount != scale.ColumnCount)
{
@ -111,23 +111,23 @@ namespace MathNet.Numerics.Distributions
}
}
return degreeOfFreedom > 0.0;
return degreesOfFreedom > 0.0;
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="degreeOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double degreeOfFreedom, Matrix<double> scale)
void SetParameters(double degreesOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreeOfFreedom, scale))
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_freedom = degreeOfFreedom;
_freedom = degreesOfFreedom;
_scale = scale;
_chol = Cholesky<double>.Create(_scale);
}
@ -135,7 +135,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets or sets the degree of freedom (ν) for the inverse Wishart distribution.
/// </summary>
public double DegreeOfFreedom
public double DegreesOfFreedom
{
get { return _freedom; }
set { SetParameters(value, _scale); }
@ -242,7 +242,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public Matrix<double> Sample()
{
return Sample(RandomSource, _freedom, _scale);
return Sample(_random, _freedom, _scale);
}
/// <summary>
@ -250,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="degreeOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degree of freedom (ν) for the inverse Wishart distribution.</param>
/// <param name="scale">The scale matrix (Ψ) for the inverse Wishart distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static Matrix<double> Sample(System.Random rnd, double degreeOfFreedom, Matrix<double> scale)
public static Matrix<double> Sample(System.Random rnd, double degreesOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreeOfFreedom, scale))
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
var r = Wishart.Sample(rnd, degreeOfFreedom, scale.Inverse());
var r = Wishart.Sample(rnd, degreesOfFreedom, scale.Inverse());
return r.Inverse();
}
}

14
src/Numerics/Distributions/Laplace.cs

@ -223,17 +223,17 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
/// <returns>the density at <paramref name="x"/>.</returns>
public double Density(double x)
{
return Math.Exp(-Math.Abs(x - _location)/_scale)/(2.0*_scale);
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -243,7 +243,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -259,7 +259,7 @@ namespace MathNet.Numerics.Distributions
/// <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)
static double SampleUnchecked(System.Random rnd, double location, double scale)
{
var u = rnd.NextDouble() - 0.5;
return location - (scale*Math.Sign(u)*Math.Log(1.0 - (2.0*Math.Abs(u))));
@ -271,7 +271,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _location, _scale);
return SampleUnchecked(_random, _location, _scale);
}
/// <summary>
@ -282,7 +282,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _location, _scale);
yield return SampleUnchecked(_random, _location, _scale);
}
}

44
src/Numerics/Distributions/LogNormal.cs

@ -119,7 +119,7 @@ namespace MathNet.Numerics.Distributions
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double mu, double sigma)
{
return sigma >= 0.0 && !Double.IsNaN(mu) && !Double.IsNaN(mu);
return sigma >= 0.0 && !Double.IsNaN(mu);
}
/// <summary>
@ -251,7 +251,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -267,7 +267,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -283,7 +283,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -303,7 +303,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return Math.Exp(Normal.SampleUnchecked(RandomSource, _mu, _sigma));
return Math.Exp(Normal.SampleUnchecked(_random, _mu, _sigma));
}
/// <summary>
@ -312,51 +312,31 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public IEnumerable<double> Samples()
{
while (true)
{
var sample = Normal.SampleUncheckedBoxMuller(RandomSource);
yield return Math.Exp(_mu + (_sigma*sample.Item1));
yield return Math.Exp(_mu + (_sigma*sample.Item2));
}
return Normal.SamplesUnchecked(_random, _mu, _sigma).Select(Math.Exp);
}
/// <summary>
/// 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="rnd">The random number generator to use.</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)
public static double Sample(System.Random rnd, double mu, double sigma)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(mu, sigma))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return Math.Exp(Normal.SampleUnchecked(rng, mu, sigma));
return Math.Exp(Normal.Sample(rnd, mu, sigma));
}
/// <summary>
/// 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="rnd">The random number generator to use.</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)
public static IEnumerable<double> Samples(System.Random rnd, double mu, double sigma)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(mu, sigma))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
var sample = Normal.SampleUncheckedBoxMuller(rng);
yield return Math.Exp(mu + (sigma*sample.Item1));
yield return Math.Exp(mu + (sigma*sample.Item2));
}
return Normal.Samples(rnd, mu, sigma).Select(Math.Exp);
}
}
}

28
src/Numerics/Distributions/MatrixNormal.cs

@ -231,7 +231,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public Matrix<double> Sample()
{
return Sample(RandomSource, _m, _v, _k);
return Sample(_random, _m, _v, _k);
}
/// <summary>
@ -282,34 +282,12 @@ namespace MathNet.Numerics.Distributions
static Vector<double> SampleVectorNormal(System.Random rnd, Vector<double> mean, Matrix<double> covariance)
{
var chol = Cholesky<double>.Create(covariance);
return SampleVectorNormal(rnd, mean, chol);
}
/// <summary>
/// Samples a vector normal distributed random variable.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="mean">The mean of the vector normal distribution.</param>
/// <param name="cholesky">The Cholesky factorization of the covariance matrix.</param>
/// <returns>a sequence of samples from defined distribution.</returns>
static Vector<double> SampleVectorNormal(System.Random rnd, Vector<double> mean, Cholesky<double> cholesky)
{
var count = mean.Count;
// Sample a standard normal variable.
var v = new DenseVector(count);
for (var d = 0; d < count; d += 2)
{
var sample = Normal.SampleUncheckedBoxMuller(rnd);
v[d] = sample.Item1;
if (d + 1 < count)
{
v[d + 1] = sample.Item2;
}
}
var v = DenseVector.CreateRandom(mean.Count, new Normal(rnd));
// Return the transformed variable.
return mean + (cholesky.Factor*v);
return mean + (chol.Factor*v);
}
}
}

4
src/Numerics/Distributions/Multinomial.cs

@ -317,7 +317,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the counts for each of the different possible values.</returns>
public int[] Sample()
{
return Sample(RandomSource, _p, _trials);
return Sample(_random, _p, _trials);
}
/// <summary>
@ -328,7 +328,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return Sample(RandomSource, _p, _trials);
yield return Sample(_random, _p, _trials);
}
}

32
src/Numerics/Distributions/NegativeBinomial.cs

@ -56,7 +56,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="NegativeBinomial"/> class.
/// </summary>
/// <param name="r">The number of trials.</param>
/// <param name="r">The number of failures until the experiment stopped.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
public NegativeBinomial(double r, double p)
{
@ -67,7 +67,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="NegativeBinomial"/> class.
/// </summary>
/// <param name="r">The number of trials.</param>
/// <param name="r">The number of failures until the experiment stopped.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public NegativeBinomial(double r, double p, System.Random randomSource)
@ -90,7 +90,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="r">The number of trials.</param>
/// <param name="r">The number of failures until the experiment stopped.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double r, double p)
@ -101,7 +101,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="r">The number of trials.</param>
/// <param name="r">The number of failures until the experiment stopped.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double r, double p)
@ -214,7 +214,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -229,7 +229,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -244,7 +244,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -257,10 +257,10 @@ namespace MathNet.Numerics.Distributions
/// Samples a negative binomial distributed random variable.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="r">The r parameter.</param>
/// <param name="p">The p parameter.</param>
/// <param name="r">The number of failures until the experiment stopped.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
/// <returns>a sample from the distribution.</returns>
internal static int SampleUnchecked(System.Random rnd, double r, double p)
static int SampleUnchecked(System.Random rnd, double r, double p)
{
var lambda = Gamma.SampleUnchecked(rnd, r, p);
var c = Math.Exp(-lambda);
@ -280,7 +280,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _trials, _p);
return SampleUnchecked(_random, _trials, _p);
}
/// <summary>
@ -291,7 +291,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _trials, _p);
yield return SampleUnchecked(_random, _trials, _p);
}
}
@ -299,8 +299,8 @@ namespace MathNet.Numerics.Distributions
/// Samples a random variable.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="r">The r parameter.</param>
/// <param name="p">The p parameter.</param>
/// <param name="r">The number of failures until the experiment stopped.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
public static int Sample(System.Random rnd, double r, double p)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p))
@ -315,8 +315,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="r">The r parameter.</param>
/// <param name="p">The p parameter.</param>
/// <param name="r">The number of failures until the experiment stopped.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
public static IEnumerable<int> Samples(System.Random rnd, double r, double p)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p))

46
src/Numerics/Distributions/Normal.cs

@ -309,7 +309,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -319,7 +319,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -341,7 +341,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -351,7 +351,8 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the inverse cumulative distribution function of the normal distribution.
/// Computes the inverse of the cumulative distribution function (InvCDF) for the distribution
/// at the given probability.
/// </summary>
/// <param name="p">The location at which to compute the inverse cumulative density.</param>
/// <returns>the inverse cumulative density at <paramref name="p"/>.</returns>
@ -365,7 +366,7 @@ namespace MathNet.Numerics.Distributions
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <returns>a pair of random numbers from the standard normal distribution.</returns>
internal static Tuple<double, double> SampleUncheckedBoxMuller(System.Random rnd)
static Tuple<double, double> SampleStandardBoxMuller(System.Random rnd)
{
var v1 = (2.0*rnd.NextDouble()) - 1.0;
var v2 = (2.0*rnd.NextDouble()) - 1.0;
@ -390,7 +391,24 @@ namespace MathNet.Numerics.Distributions
/// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double mean, double stddev)
{
return mean + (stddev*SampleUncheckedBoxMuller(rnd).Item1);
return mean + (stddev*SampleStandardBoxMuller(rnd).Item1);
}
/// <summary>
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</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 IEnumerable<double> SamplesUnchecked(System.Random rnd, double mean, double stddev)
{
while (true)
{
var sample = SampleStandardBoxMuller(rnd);
yield return mean + (stddev*sample.Item1);
yield return mean + (stddev*sample.Item2);
}
}
/// <summary>
@ -399,7 +417,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _mean, _stdDev);
return SampleUnchecked(_random, _mean, _stdDev);
}
/// <summary>
@ -408,12 +426,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public IEnumerable<double> Samples()
{
while (true)
{
var sample = SampleUncheckedBoxMuller(RandomSource);
yield return _mean + (_stdDev*sample.Item1);
yield return _mean + (_stdDev*sample.Item2);
}
return SamplesUnchecked(_random, _mean, _stdDev);
}
/// <summary>
@ -447,12 +460,7 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
var sample = SampleUncheckedBoxMuller(rnd);
yield return mean + (stddev*sample.Item1);
yield return mean + (stddev*sample.Item2);
}
return SamplesUnchecked(rnd, mean, stddev);
}
}
}

4
src/Numerics/Distributions/NormalGamma.cs

@ -351,7 +351,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public MeanPrecisionPair Sample()
{
return Sample(RandomSource, _meanLocation, _meanScale, _precisionShape, _precisionInvScale);
return Sample(_random, _meanLocation, _meanScale, _precisionShape, _precisionInvScale);
}
/// <summary>
@ -362,7 +362,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return Sample(RandomSource, _meanLocation, _meanScale, _precisionShape, _precisionInvScale);
yield return Sample(_random, _meanLocation, _meanScale, _precisionShape, _precisionInvScale);
}
}

12
src/Numerics/Distributions/Pareto.cs

@ -231,7 +231,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -241,7 +241,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -251,7 +251,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -267,7 +267,7 @@ namespace MathNet.Numerics.Distributions
/// <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)
static double SampleUnchecked(System.Random rnd, double scale, double shape)
{
return scale*Math.Pow(rnd.NextDouble(), -1.0/shape);
}
@ -278,7 +278,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _scale, _shape);
return SampleUnchecked(_random, _scale, _shape);
}
/// <summary>
@ -289,7 +289,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _scale, _shape);
yield return SampleUnchecked(_random, _scale, _shape);
}
}

12
src/Numerics/Distributions/Poisson.cs

@ -200,7 +200,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -210,7 +210,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -220,7 +220,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -235,7 +235,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random source to use.</param>
/// <param name="lambda">The Poisson distribution parameter λ.</param>
/// <returns>A random sample from the Poisson distribution.</returns>
internal static int SampleUnchecked(System.Random rnd, double lambda)
static int SampleUnchecked(System.Random rnd, double lambda)
{
return (lambda < 30.0) ? DoSampleShort(rnd, lambda) : DoSampleLarge(rnd, lambda);
}
@ -302,7 +302,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A sample from the Poisson distribution.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _lambda);
return SampleUnchecked(_random, _lambda);
}
/// <summary>
@ -313,7 +313,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _lambda);
yield return SampleUnchecked(_random, _lambda);
}
}

12
src/Numerics/Distributions/Rayleigh.cs

@ -201,7 +201,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -211,7 +211,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -221,7 +221,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -236,7 +236,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</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)
static double SampleUnchecked(System.Random rnd, double scale)
{
return scale*Math.Sqrt(-2.0*Math.Log(rnd.NextDouble()));
}
@ -247,7 +247,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _scale);
return SampleUnchecked(_random, _scale);
}
/// <summary>
@ -258,7 +258,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _scale);
yield return SampleUnchecked(_random, _scale);
}
}

12
src/Numerics/Distributions/Stable.cs

@ -303,7 +303,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -346,7 +346,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -356,7 +356,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -403,7 +403,7 @@ namespace MathNet.Numerics.Distributions
/// <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)
static double SampleUnchecked(System.Random rnd, double alpha, double beta, double scale, double location)
{
var randTheta = ContinuousUniform.Sample(rnd, -Constants.PiOver2, Constants.PiOver2);
var randW = Exponential.Sample(rnd, 1.0);
@ -436,7 +436,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _alpha, _beta, _scale, _location);
return SampleUnchecked(_random, _alpha, _beta, _scale, _location);
}
/// <summary>
@ -447,7 +447,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _alpha, _beta, _scale, _location);
yield return SampleUnchecked(_random, _alpha, _beta, _scale, _location);
}
}

116
src/Numerics/Distributions/StudentT.cs

@ -1,4 +1,4 @@
// <copyright file="StudentT.cs" company="Math.NET">
// <copyright file="StudentT.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -78,13 +78,13 @@ namespace MathNet.Numerics.Distributions
/// freedom. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="location">The location of the Student t-distribution.</param>
/// <param name="scale">The scale of the Student t-distribution.</param>
/// <param name="dof">The degrees of freedom for the Student t-distribution.</param>
public StudentT(double location, double scale, double dof)
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <param name="freedom">The degrees of freedom (ν) for the distribution.</param>
public StudentT(double location, double scale, double freedom)
{
_random = new System.Random();
SetParameters(location, scale, dof);
SetParameters(location, scale, freedom);
}
/// <summary>
@ -92,14 +92,14 @@ namespace MathNet.Numerics.Distributions
/// freedom. The distribution will
/// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary>
/// <param name="location">The location of the Student t-distribution.</param>
/// <param name="scale">The scale of the Student t-distribution.</param>
/// <param name="dof">The degrees of freedom for the Student t-distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <param name="freedom">The degrees of freedom (ν) for the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public StudentT(double location, double scale, double dof, System.Random randomSource)
public StudentT(double location, double scale, double freedom, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(location, scale, dof);
SetParameters(location, scale, freedom);
}
/// <summary>
@ -108,42 +108,42 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "StudentT(Location = " + _location + ", Scale = " + _scale + ", DoF = " + _freedom + ")";
return "StudentT(μ = " + _location + ", σ = " + _scale + ", ν = " + _freedom + ")";
}
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="location">The location of the Student t-distribution.</param>
/// <param name="scale">The scale of the Student t-distribution.</param>
/// <param name="dof">The degrees of freedom for the Student t-distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <param name="freedom">The degrees of freedom (ν) for the distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double location, double scale, double dof)
static bool IsValidParameterSet(double location, double scale, double freedom)
{
return scale > 0.0 && dof > 0.0 && !Double.IsNaN(location);
return scale > 0.0 && freedom > 0.0 && !Double.IsNaN(location);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="location">The location of the Student t-distribution.</param>
/// <param name="scale">The scale of the Student t-distribution.</param>
/// <param name="dof">The degrees of freedom for the Student t-distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <param name="freedom">The degrees of freedom (ν) for the distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double location, double scale, double dof)
void SetParameters(double location, double scale, double freedom)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, dof))
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, freedom))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_location = location;
_scale = scale;
_freedom = dof;
_freedom = freedom;
}
/// <summary>
/// Gets or sets the location of the Student t-distribution.
/// Gets or sets the location (μ) of the Student t-distribution.
/// </summary>
public double Location
{
@ -152,7 +152,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the scale of the Student t-distribution.
/// Gets or sets the scale (σ) of the Student t-distribution.
/// </summary>
public double Scale
{
@ -161,7 +161,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the degrees of freedom of the Student t-distribution.
/// Gets or sets the degrees of freedom (ν) of the Student t-distribution.
/// </summary>
public double DegreesOfFreedom
{
@ -235,12 +235,10 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (_location != 0 || _scale != 1.0)
{
throw new NotSupportedException();
}
if (_location != 0 || _scale != 1.0) throw new NotSupportedException();
return (((_freedom + 1.0)/2.0)*(SpecialFunctions.DiGamma((1.0 + _freedom)/2.0) - SpecialFunctions.DiGamma(_freedom/2.0))) + Math.Log(Math.Sqrt(_freedom)*SpecialFunctions.Beta(_freedom/2.0, 1.0/2.0));
return (((_freedom + 1.0)/2.0)*(SpecialFunctions.DiGamma((1.0 + _freedom)/2.0) - SpecialFunctions.DiGamma(_freedom/2.0)))
+ Math.Log(Math.Sqrt(_freedom)*SpecialFunctions.Beta(_freedom/2.0, 1.0/2.0));
}
}
@ -251,10 +249,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
if (_freedom <= 3)
{
throw new NotSupportedException();
}
if (_freedom <= 3) throw new NotSupportedException();
return 0.0;
}
@ -293,7 +288,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -313,7 +308,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -333,7 +328,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -357,15 +352,14 @@ namespace MathNet.Numerics.Distributions
/// <remarks>The algorithm is method 2 in section 5, chapter 9
/// in L. Devroye's "Non-Uniform Random Variate Generation"</remarks>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="location">The location of the Student t-distribution.</param>
/// <param name="scale">The scale of the Student t-distribution.</param>
/// <param name="dof">The degrees of freedom for the standard student-t distribution.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <param name="freedom">The degrees of freedom (ν) for the distribution.</param>
/// <returns>a random number from the standard student-t distribution.</returns>
internal static double SampleUnchecked(System.Random rnd, double location, double scale, double dof)
static double SampleUnchecked(System.Random rnd, double location, double scale, double freedom)
{
var n = Normal.SampleUncheckedBoxMuller(rnd).Item1;
var g = Gamma.SampleUnchecked(rnd, 0.5*dof, 0.5);
return location + (scale*n*Math.Sqrt(dof/g));
var gamma = Gamma.SampleUnchecked(rnd, 0.5*freedom, 0.5);
return Normal.SampleUnchecked(rnd, location, scale*Math.Sqrt(freedom/gamma));
}
/// <summary>
@ -374,7 +368,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _location, _scale, _freedom);
return SampleUnchecked(_random, _location, _scale, _freedom);
}
/// <summary>
@ -385,46 +379,46 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _location, _scale, _freedom);
yield return SampleUnchecked(_random, _location, _scale, _freedom);
}
}
/// <summary>
/// Generates a sample from the Student t-distribution.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="location">The location of the Student t-distribution.</param>
/// <param name="scale">The scale of the Student t-distribution.</param>
/// <param name="dof">The degrees of freedom for the Student t-distribution.</param>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <param name="freedom">The degrees of freedom (ν) for the distribution.</param>
/// <returns>a sample from the distribution.</returns>
public static double Sample(System.Random rng, double location, double scale, double dof)
public static double Sample(System.Random rnd, double location, double scale, double freedom)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, dof))
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, freedom))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return SampleUnchecked(rng, location, scale, dof);
return SampleUnchecked(rnd, location, scale, freedom);
}
/// <summary>
/// Generates a sequence of samples from the Student t-distribution using the <i>Box-Muller</i> algorithm.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="location">The location of the Student t-distribution.</param>
/// <param name="scale">The scale of the Student t-distribution.</param>
/// <param name="dof">The degrees of freedom for the Student t-distribution.</param>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="location">The location (μ) of the distribution.</param>
/// <param name="scale">The scale (σ) of the distribution.</param>
/// <param name="freedom">The degrees of freedom (ν) for the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(System.Random rng, double location, double scale, double dof)
public static IEnumerable<double> Samples(System.Random rnd, double location, double scale, double freedom)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, dof))
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, freedom))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return SampleUnchecked(rng, location, scale, dof);
yield return SampleUnchecked(rnd, location, scale, freedom);
}
}
}

24
src/Numerics/Distributions/Weibull.cs

@ -238,7 +238,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// Computes the probability density of the distribution (PDF) at x, i.e. dP(X &lt;= x)/dx.
/// </summary>
/// <param name="x">The location at which to compute the density.</param>
/// <returns>the density at <paramref name="x"/>.</returns>
@ -258,7 +258,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(dP(X &lt;= x)/dx).
/// </summary>
/// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns>
@ -278,7 +278,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -300,7 +300,7 @@ namespace MathNet.Numerics.Distributions
/// <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)
static double SampleUnchecked(System.Random rnd, double shape, double scale)
{
var x = rnd.NextDouble();
return scale*Math.Pow(-Math.Log(x), 1.0/shape);
@ -312,7 +312,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public double Sample()
{
return SampleUnchecked(RandomSource, _shape, _scale);
return SampleUnchecked(_random, _shape, _scale);
}
/// <summary>
@ -323,35 +323,35 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _shape, _scale);
yield return SampleUnchecked(_random, _shape, _scale);
}
}
/// <summary>
/// Generates a sample from the Weibull distribution.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="rnd">The random number generator to use.</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)
public static double Sample(System.Random rnd, double shape, double scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return SampleUnchecked(rng, shape, scale);
return SampleUnchecked(rnd, shape, scale);
}
/// <summary>
/// Generates a sequence of samples from the Weibull distribution.
/// </summary>
/// <param name="rng">The random number generator to use.</param>
/// <param name="rnd">The random number generator to use.</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)
public static IEnumerable<double> Samples(System.Random rnd, double shape, double scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale))
{
@ -360,7 +360,7 @@ namespace MathNet.Numerics.Distributions
while (true)
{
yield return SampleUnchecked(rng, shape, scale);
yield return SampleUnchecked(rnd, shape, scale);
}
}
}

66
src/Numerics/Distributions/Wishart.cs

@ -55,7 +55,7 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// The degrees of freedom for the Wishart distribution.
/// </summary>
double _degreeOfFreedom;
double _degreesOfFreedom;
/// <summary>
/// The scale matrix for the Wishart distribution.
@ -70,40 +70,40 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="Wishart"/> class.
/// </summary>
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
public Wishart(double degreeOfFreedom, Matrix<double> scale)
public Wishart(double degreesOfFreedom, Matrix<double> scale)
{
_random = new System.Random();
SetParameters(degreeOfFreedom, scale);
SetParameters(degreesOfFreedom, scale);
}
/// <summary>
/// Initializes a new instance of the <see cref="Wishart"/> class.
/// </summary>
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Wishart(double degreeOfFreedom, Matrix<double> scale, System.Random randomSource)
public Wishart(double degreesOfFreedom, Matrix<double> scale, System.Random randomSource)
{
_random = randomSource ?? new System.Random();
SetParameters(degreeOfFreedom, scale);
SetParameters(degreesOfFreedom, scale);
}
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
void SetParameters(double degreeOfFreedom, Matrix<double> scale)
void SetParameters(double degreesOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreeOfFreedom, scale))
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_degreeOfFreedom = degreeOfFreedom;
_degreesOfFreedom = degreesOfFreedom;
_scale = scale;
_chol = Cholesky<double>.Create(_scale);
}
@ -111,10 +111,10 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
static bool IsValidParameterSet(double degreeOfFreedom, Matrix<double> scale)
static bool IsValidParameterSet(double degreesOfFreedom, Matrix<double> scale)
{
if (scale.RowCount != scale.ColumnCount)
{
@ -129,7 +129,7 @@ namespace MathNet.Numerics.Distributions
}
}
if (degreeOfFreedom <= 0.0 || Double.IsNaN(degreeOfFreedom))
if (degreesOfFreedom <= 0.0 || Double.IsNaN(degreesOfFreedom))
{
return false;
}
@ -140,9 +140,9 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets or sets the degrees of freedom (n) for the Wishart distribution.
/// </summary>
public double DegreeOfFreedom
public double DegreesOfFreedom
{
get { return _degreeOfFreedom; }
get { return _degreesOfFreedom; }
set { SetParameters(value, _scale); }
}
@ -152,7 +152,7 @@ namespace MathNet.Numerics.Distributions
public Matrix<double> Scale
{
get { return _scale; }
set { SetParameters(_degreeOfFreedom, value); }
set { SetParameters(_degreesOfFreedom, value); }
}
/// <summary>
@ -161,7 +161,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a string representation of the distribution.</returns>
public override string ToString()
{
return "Wishart(DegreeOfFreedom = " + _degreeOfFreedom + ", Rows = " + _scale.RowCount + ", Columns = " + _scale.ColumnCount + ")";
return "Wishart(DegreesOfFreedom = " + _degreesOfFreedom + ", 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 _degreeOfFreedom*_scale; }
get { return _degreesOfFreedom*_scale; }
}
/// <summary>
@ -196,7 +196,7 @@ namespace MathNet.Numerics.Distributions
/// <value>The mode of the distribution.</value>
public Matrix<double> Mode
{
get { return (_degreeOfFreedom - _scale.RowCount - 1.0)*_scale; }
get { return (_degreesOfFreedom - _scale.RowCount - 1.0)*_scale; }
}
/// <summary>
@ -212,7 +212,7 @@ namespace MathNet.Numerics.Distributions
{
for (var j = 0; j < res.ColumnCount; j++)
{
res.At(i, j, _degreeOfFreedom*((_scale.At(i, j)*_scale.At(i, j)) + (_scale.At(i, i)*_scale.At(j, j))));
res.At(i, j, _degreesOfFreedom*((_scale.At(i, j)*_scale.At(i, j)) + (_scale.At(i, i)*_scale.At(j, j))));
}
}
@ -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((_degreeOfFreedom + 1.0 - j)/2.0);
gp *= SpecialFunctions.Gamma((_degreesOfFreedom + 1.0 - j)/2.0);
}
return Math.Pow(dX, (_degreeOfFreedom - p - 1.0)/2.0)
return Math.Pow(dX, (_degreesOfFreedom - p - 1.0)/2.0)
*Math.Exp(-0.5*siX.Trace())
/Math.Pow(2.0, _degreeOfFreedom*p/2.0)
/Math.Pow(_chol.Determinant, _degreeOfFreedom/2.0)
/Math.Pow(2.0, _degreesOfFreedom*p/2.0)
/Math.Pow(_chol.Determinant, _degreesOfFreedom/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, _degreeOfFreedom, _scale, _chol);
return DoSample(RandomSource, _degreesOfFreedom, _scale, _chol);
}
/// <summary>
@ -271,28 +271,28 @@ 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="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static Matrix<double> Sample(System.Random rnd, double degreeOfFreedom, Matrix<double> scale)
public static Matrix<double> Sample(System.Random rnd, double degreesOfFreedom, Matrix<double> scale)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreeOfFreedom, scale))
if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return DoSample(rnd, degreeOfFreedom, scale, Cholesky<double>.Create(scale));
return DoSample(rnd, degreesOfFreedom, scale, Cholesky<double>.Create(scale));
}
/// <summary>
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="degreeOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="degreesOfFreedom">The degrees of freedom (n) for the Wishart distribution.</param>
/// <param name="scale">The scale matrix (V) for the Wishart distribution.</param>
/// <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 degreeOfFreedom, Matrix<double> scale, Cholesky<double> chol)
static Matrix<double> DoSample(System.Random rnd, double degreesOfFreedom, Matrix<double> scale, Cholesky<double> chol)
{
var count = scale.RowCount;
@ -301,7 +301,7 @@ namespace MathNet.Numerics.Distributions
var a = new DenseMatrix(count, count);
for (var d = 0; d < count; d++)
{
a.At(d, d, Math.Sqrt(Gamma.Sample(rnd, (degreeOfFreedom - d)/2.0, 0.5)));
a.At(d, d, Math.Sqrt(Gamma.Sample(rnd, (degreesOfFreedom - d)/2.0, 0.5)));
}
for (var i = 1; i < count; i++)

12
src/Numerics/Distributions/Zipf.cs

@ -247,7 +247,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the probability mass (PMF), i.e. P(X = x).
/// Computes the probability mass (PMF) at k, i.e. P(X = k).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -257,7 +257,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// Computes the log probability mass (lnPMF) at k, i.e. ln(P(X = k)).
/// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -267,7 +267,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// Computes the cumulative distribution (CDF) of the distribution at x, i.e. P(X &lt;= x).
/// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
@ -288,7 +288,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="s">The s parameter of the distribution.</param>
/// <param name="n">The n parameter of the distribution.</param>
/// <returns>a random number from the Zipf distribution.</returns>
internal static int SampleUnchecked(System.Random rnd, double s, int n)
static int SampleUnchecked(System.Random rnd, double s, int n)
{
var r = 0.0;
while (r == 0.0)
@ -317,7 +317,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sample from the distribution.</returns>
public int Sample()
{
return SampleUnchecked(RandomSource, _s, _n);
return SampleUnchecked(_random, _s, _n);
}
/// <summary>
@ -328,7 +328,7 @@ namespace MathNet.Numerics.Distributions
{
while (true)
{
yield return SampleUnchecked(RandomSource, _s, _n);
yield return SampleUnchecked(_random, _s, _n);
}
}

2
src/Numerics/Numerics.csproj

@ -92,7 +92,7 @@
<Compile Include="Distributions\Categorical.cs" />
<Compile Include="Distributions\Cauchy.cs" />
<Compile Include="Distributions\Chi.cs" />
<Compile Include="Distributions\ChiSquare.cs" />
<Compile Include="Distributions\ChiSquared.cs" />
<Compile Include="Distributions\ContinuousUniform.cs" />
<Compile Include="Distributions\ConwayMaxwellPoisson.cs" />
<Compile Include="Distributions\IDistribution.cs" />

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

@ -57,7 +57,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity)]
public void CanCreateChiSquare(double dof)
{
var n = new ChiSquare(dof);
var n = new ChiSquared(dof);
Assert.AreEqual(dof, n.DegreesOfFreedom);
}
@ -72,7 +72,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.NaN)]
public void ChiSquareCreateFailsWithBadParameters(double dof)
{
Assert.Throws<ArgumentOutOfRangeException>(() => new ChiSquare(dof));
Assert.Throws<ArgumentOutOfRangeException>(() => new ChiSquared(dof));
}
/// <summary>
@ -81,8 +81,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new ChiSquare(1.0);
Assert.AreEqual("ChiSquare(DoF = 1)", n.ToString());
var n = new ChiSquared(1.0);
Assert.AreEqual("ChiSquared(k = 1)", n.ToString());
}
/// <summary>
@ -95,7 +95,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity)]
public void CanSetDoF(double dof)
{
new ChiSquare(1.0)
new ChiSquared(1.0)
{
DegreesOfFreedom = dof
};
@ -110,7 +110,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(0.0)]
public void SetDofFailsWithNonPositiveDoF(double dof)
{
var n = new ChiSquare(1.0);
var n = new ChiSquared(1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.DegreesOfFreedom = dof);
}
@ -125,7 +125,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity)]
public void ValidateMean(double dof)
{
var n = new ChiSquare(dof);
var n = new ChiSquared(dof);
Assert.AreEqual(dof, n.Mean);
}
@ -140,7 +140,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity)]
public void ValidateVariance(double dof)
{
var n = new ChiSquare(dof);
var n = new ChiSquared(dof);
Assert.AreEqual(2 * dof, n.Variance);
}
@ -155,7 +155,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity)]
public void ValidateStdDev(double dof)
{
var n = new ChiSquare(dof);
var n = new ChiSquared(dof);
Assert.AreEqual(Math.Sqrt(n.Variance), n.StdDev);
}
@ -170,7 +170,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity)]
public void ValidateMode(double dof)
{
var n = new ChiSquare(dof);
var n = new ChiSquared(dof);
Assert.AreEqual(dof - 2, n.Mode);
}
@ -185,7 +185,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity)]
public void ValidateMedian(double dof)
{
var n = new ChiSquare(dof);
var n = new ChiSquared(dof);
Assert.AreEqual(dof - (2.0 / 3.0), n.Median);
}
@ -195,7 +195,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateMinimum()
{
var n = new ChiSquare(1.0);
var n = new ChiSquared(1.0);
Assert.AreEqual(0.0, n.Minimum);
}
@ -205,7 +205,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateMaximum()
{
var n = new ChiSquare(1.0);
var n = new ChiSquared(1.0);
Assert.AreEqual(Double.PositiveInfinity, n.Maximum);
}
@ -240,7 +240,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity, Double.PositiveInfinity)]
public void ValidateDensity(double dof, double x)
{
var n = new ChiSquare(dof);
var n = new ChiSquared(dof);
Assert.AreEqual((Math.Pow(x, (dof / 2.0) - 1.0) * Math.Exp(-x / 2.0)) / (Math.Pow(2.0, dof / 2.0) * SpecialFunctions.Gamma(dof / 2.0)), n.Density(x));
}
@ -275,7 +275,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity, Double.PositiveInfinity)]
public void ValidateDensityLn(double dof, double x)
{
var n = new ChiSquare(dof);
var n = new ChiSquared(dof);
Assert.AreEqual((-x / 2.0) + (((dof / 2.0) - 1.0) * Math.Log(x)) - ((dof / 2.0) * Math.Log(2)) - SpecialFunctions.GammaLn(dof / 2.0), n.DensityLn(x));
}
@ -285,7 +285,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void CanSampleStatic()
{
ChiSquare.Sample(new Random(), 2.0);
ChiSquared.Sample(new Random(), 2.0);
}
/// <summary>
@ -294,7 +294,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void FailSampleStatic()
{
Assert.Throws<ArgumentOutOfRangeException>(() => ChiSquare.Sample(new Random(), -1.0));
Assert.Throws<ArgumentOutOfRangeException>(() => ChiSquared.Sample(new Random(), -1.0));
}
/// <summary>
@ -303,7 +303,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void CanSample()
{
var n = new ChiSquare(1.0);
var n = new ChiSquared(1.0);
n.Sample();
}
@ -313,7 +313,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void CanSampleSequence()
{
var n = new ChiSquare(1.0);
var n = new ChiSquared(1.0);
var ied = n.Samples();
ied.Take(5).ToArray();
}
@ -349,7 +349,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(Double.PositiveInfinity, Double.PositiveInfinity)]
public void ValidateCumulativeDistribution(double dof, double x)
{
var n = new ChiSquare(dof);
var n = new ChiSquared(dof);
Assert.AreEqual(SpecialFunctions.GammaLowerIncomplete(dof / 2.0, x / 2.0) / SpecialFunctions.Gamma(dof / 2.0), n.CumulativeDistribution(x));
}
}

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

@ -80,7 +80,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateToString()
{
var n = new Chi(1.0);
Assert.AreEqual("Chi(DoF = 1)", n.ToString());
Assert.AreEqual("Chi(k = 1)", n.ToString());
}
/// <summary>

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

@ -122,7 +122,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateToString()
{
var n = new Erlang(1, 2d);
Assert.AreEqual("Erlang(Shape = 1, λ = 2)", n.ToString());
Assert.AreEqual("Erlang(k = 1, λ = 2)", n.ToString());
}
/// <summary>

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

@ -66,8 +66,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void CanCreateFisherSnedecor(double d1, double d2)
{
var n = new FisherSnedecor(d1, d2);
Assert.AreEqual(d1, n.DegreeOfFreedom1);
Assert.AreEqual(d2, n.DegreeOfFreedom2);
Assert.AreEqual(d1, n.DegreesOfFreedom1);
Assert.AreEqual(d2, n.DegreesOfFreedom2);
}
/// <summary>
@ -102,8 +102,8 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[Test]
public void ValidateToString()
{
var n = new FisherSnedecor(2.0, 1.0);
Assert.AreEqual("FisherSnedecor(DegreeOfFreedom1 = 2, DegreeOfFreedom2 = 1)", n.ToString());
var n = new FisherSnedecor(2d, 1d);
Assert.AreEqual("FisherSnedecor(d1 = 2, d2 = 1)", n.ToString());
}
/// <summary>
@ -114,11 +114,11 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetDegreeOfFreedom1(double d1)
public void CanSetDegreesOfFreedom1(double d1)
{
new FisherSnedecor(1.0, 2.0)
{
DegreeOfFreedom1 = d1
DegreesOfFreedom1 = d1
};
}
@ -126,10 +126,10 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
/// Set degree of freedom 1 fails with negative value.
/// </summary>
[Test]
public void SetDegreeOfFreedom1FailsWithNegativeDegreeOfFreedom()
public void SetDegreesOfFreedom1FailsWithNegativeDegreeOfFreedom()
{
var n = new FisherSnedecor(1.0, 2.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.DegreeOfFreedom1 = -1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.DegreesOfFreedom1 = -1.0);
}
/// <summary>
@ -140,11 +140,11 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
[TestCase(1.0)]
[TestCase(10.0)]
[TestCase(Double.PositiveInfinity)]
public void CanSetDegreeOfFreedom2(double d2)
public void CanSetDegreesOfFreedom2(double d2)
{
new FisherSnedecor(1.0, 2.0)
{
DegreeOfFreedom2 = d2
DegreesOfFreedom2 = d2
};
}
@ -152,10 +152,10 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
/// Set degree of freedom 2 fails with negative value.
/// </summary>
[Test]
public void SetDegreeOfFreedom2FailsWithNegativeDegreeOfFreedom()
public void SetDegreesOfFreedom2FailsWithNegativeDegreeOfFreedom()
{
var n = new FisherSnedecor(1.0, 2.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.DegreeOfFreedom2 = -1.0);
Assert.Throws<ArgumentOutOfRangeException>(() => n.DegreesOfFreedom2 = -1.0);
}
/// <summary>

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

@ -1,4 +1,4 @@
// <copyright file="StudentTTests.cs" company="Math.NET">
// <copyright file="StudentTTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
@ -100,7 +100,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateToString()
{
var n = new StudentT(1.0, 2.0, 1.0);
Assert.AreEqual("StudentT(Location = 1, Scale = 2, DoF = 1)", n.ToString());
Assert.AreEqual("StudentT(μ = 1, σ = 2, ν = 1)", n.ToString());
}
/// <summary>

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

@ -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">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
[TestCase(0.1, 0.0)]
[TestCase(1.0, 2.5)]
[TestCase(2.5, 3.0)]
@ -99,9 +99,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
}
/// <summary>
/// Can set DegreeOfFreedom.
/// Can set DegreesOfFreedom.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
[TestCase(0.0)]
[TestCase(3.0)]
[TestCase(10.0)]
@ -129,9 +129,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
}
/// <summary>
/// Set DegreeOfFreedom with bad values fails.
/// Set DegreesOfFreedom with bad values fails.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="x">Input X value.</param>
/// <param name="cdf">Expected value.</param>
[TestCase(1.0, 1.0, 1, 0.735758882342885)]

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

@ -50,7 +50,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Can create inverse Wishart.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -60,7 +60,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var matrix = MatrixLoader.GenerateRandomPositiveDefiniteDenseMatrix(order);
var d = new InverseWishart(nu, matrix);
Assert.AreEqual(nu, d.DegreeOfFreedom);
Assert.AreEqual(nu, d.DegreesOfFreedom);
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.Scale.ColumnCount; j++)
@ -73,7 +73,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Fail create inverse Wishart with bad parameters.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(-1.0, 2)]
[TestCase(Double.NaN, 5)]
@ -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.DegreeOfFreedom);
Assert.AreEqual(nu, d.DegreesOfFreedom);
}
/// <summary>
/// Can set DegreeOfFreedom.
/// Can set DegreesOfFreedom.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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))
{
DegreeOfFreedom = nu
DegreesOfFreedom = nu
};
}
@ -201,7 +201,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate mean.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -223,7 +223,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate mode.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -245,7 +245,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate variance.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -269,7 +269,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate density.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="density">Expected value.</param>
[TestCase(1.0, 0.03228684517430723)]
[TestCase(2.0, 0.018096748360719193)]

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

@ -50,7 +50,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Can create wishart.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -61,7 +61,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
var d = new Wishart(nu, matrix);
Assert.AreEqual(nu, d.DegreeOfFreedom);
Assert.AreEqual(nu, d.DegreesOfFreedom);
for (var i = 0; i < d.Scale.RowCount; i++)
{
for (var j = 0; j < d.Scale.ColumnCount; j++)
@ -74,7 +74,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Fail create Wishart with bad parameters.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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(DegreeOfFreedom = 1, Rows = 2, Columns = 2)", d.ToString());
Assert.AreEqual("Wishart(DegreesOfFreedom = 1, Rows = 2, Columns = 2)", d.ToString());
}
/// <summary>
/// Can get DegreeOfFreedom.
/// Can get DegreesOfFreedom.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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.DegreeOfFreedom);
Assert.AreEqual(nu, d.DegreesOfFreedom);
}
/// <summary>
/// Can set DegreeOfFreedom.
/// Can set DegreesOfFreedom.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom 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))
{
DegreeOfFreedom = nu
DegreesOfFreedom = nu
};
}
@ -205,7 +205,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate mean.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -227,7 +227,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate mode.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -249,7 +249,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate variance.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="order">Scale matrix order.</param>
[TestCase(0.1, 2)]
[TestCase(1.0, 5)]
@ -271,7 +271,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
/// <summary>
/// Validate density.
/// </summary>
/// <param name="nu">DegreeOfFreedom parameter.</param>
/// <param name="nu">DegreesOfFreedom parameter.</param>
/// <param name="density">Expected value.</param>
[TestCase(1.0, 0.014644982561926487)]
[TestCase(2.0, 0.041042499311949421)]

Loading…
Cancel
Save