Browse Source

Distributions: move files (ns sync), clean up

optimization-1
Christoph Ruegg 13 years ago
parent
commit
e37498618b
  1. 81
      src/Numerics/Distributions/Bernoulli.cs
  2. 59
      src/Numerics/Distributions/Beta.cs
  3. 95
      src/Numerics/Distributions/Binomial.cs
  4. 87
      src/Numerics/Distributions/Categorical.cs
  5. 55
      src/Numerics/Distributions/Cauchy.cs
  6. 64
      src/Numerics/Distributions/Chi.cs
  7. 60
      src/Numerics/Distributions/ChiSquare.cs
  8. 45
      src/Numerics/Distributions/ContinuousUniform.cs
  9. 81
      src/Numerics/Distributions/ConwayMaxwellPoisson.cs
  10. 28
      src/Numerics/Distributions/Dirichlet.cs
  11. 89
      src/Numerics/Distributions/DiscreteUniform.cs
  12. 88
      src/Numerics/Distributions/Erlang.cs
  13. 71
      src/Numerics/Distributions/Exponential.cs
  14. 63
      src/Numerics/Distributions/FisherSnedecor.cs
  15. 51
      src/Numerics/Distributions/Gamma.cs
  16. 69
      src/Numerics/Distributions/Geometric.cs
  17. 43
      src/Numerics/Distributions/Hypergeometric.cs
  18. 6
      src/Numerics/Distributions/IContinuousDistribution.cs
  19. 6
      src/Numerics/Distributions/IDiscreteDistribution.cs
  20. 4
      src/Numerics/Distributions/IDistribution.cs
  21. 61
      src/Numerics/Distributions/InverseGamma.cs
  22. 31
      src/Numerics/Distributions/InverseWishart.cs
  23. 63
      src/Numerics/Distributions/Laplace.cs
  24. 50
      src/Numerics/Distributions/LogNormal.cs
  25. 33
      src/Numerics/Distributions/MatrixNormal.cs
  26. 40
      src/Numerics/Distributions/Multinomial.cs
  27. 85
      src/Numerics/Distributions/NegativeBinomial.cs
  28. 44
      src/Numerics/Distributions/Normal.cs
  29. 33
      src/Numerics/Distributions/NormalGamma.cs
  30. 61
      src/Numerics/Distributions/Pareto.cs
  31. 66
      src/Numerics/Distributions/Poisson.cs
  32. 61
      src/Numerics/Distributions/Rayleigh.cs
  33. 123
      src/Numerics/Distributions/Stable.cs
  34. 61
      src/Numerics/Distributions/StudentT.cs
  35. 45
      src/Numerics/Distributions/Weibull.cs
  36. 33
      src/Numerics/Distributions/Wishart.cs
  37. 79
      src/Numerics/Distributions/Zipf.cs
  38. 8
      src/Numerics/LinearAlgebra/Complex/Solvers/Iterative/MlkBiCgStab.cs
  39. 8
      src/Numerics/LinearAlgebra/Complex32/Solvers/Iterative/MlkBiCgStab.cs
  40. 8
      src/Numerics/LinearAlgebra/Double/Solvers/Iterative/MlkBiCgStab.cs
  41. 6
      src/Numerics/LinearAlgebra/Single/Solvers/Iterative/MlkBiCgStab.cs
  42. 70
      src/Numerics/Numerics.csproj
  43. 8
      src/Numerics/Statistics/MCMC/HybridMC.cs
  44. 6
      src/Numerics/Statistics/MCMC/UnivariateHybridMC.cs
  45. 16
      src/UnitTests/DistributionTests/CommonDistributionTests.cs
  46. 10
      src/UnitTests/DistributionTests/Continuous/BetaTests.cs
  47. 10
      src/UnitTests/DistributionTests/Continuous/CauchyTests.cs
  48. 10
      src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs
  49. 10
      src/UnitTests/DistributionTests/Continuous/ChiTests.cs
  50. 10
      src/UnitTests/DistributionTests/Continuous/ContinuousUniformTests.cs
  51. 10
      src/UnitTests/DistributionTests/Continuous/ErlangTests.cs
  52. 10
      src/UnitTests/DistributionTests/Continuous/ExponentialTests.cs
  53. 10
      src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs
  54. 10
      src/UnitTests/DistributionTests/Continuous/GammaTests.cs
  55. 10
      src/UnitTests/DistributionTests/Continuous/InverseGammaTests.cs
  56. 10
      src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs
  57. 10
      src/UnitTests/DistributionTests/Continuous/LogNormalTests.cs
  58. 10
      src/UnitTests/DistributionTests/Continuous/NormalTests.cs
  59. 10
      src/UnitTests/DistributionTests/Continuous/ParetoTests.cs
  60. 10
      src/UnitTests/DistributionTests/Continuous/RayleighTests.cs
  61. 10
      src/UnitTests/DistributionTests/Continuous/StableTests.cs
  62. 10
      src/UnitTests/DistributionTests/Continuous/StudentTTests.cs
  63. 10
      src/UnitTests/DistributionTests/Continuous/WeibullTests.cs
  64. 10
      src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs
  65. 16
      src/UnitTests/IntegralTransformsTests/FourierTest.cs
  66. 16
      src/UnitTests/IntegralTransformsTests/HartleyTest.cs
  67. 16
      src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs
  68. 16
      src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs
  69. 19
      src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs
  70. 6
      src/UnitTests/InterpolationTests/LinearInterpolationCase.cs
  71. 4
      src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs
  72. 4
      src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs
  73. 4
      src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs
  74. 4
      src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs
  75. 2
      src/UnitTests/LinearAlgebraTests/Complex/MatrixLoader.cs
  76. 2
      src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs
  77. 2
      src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.Arithmetic.cs
  78. 8
      src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs
  79. 2
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixLoader.cs
  80. 2
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs
  81. 2
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.Arithmetic.cs
  82. 8
      src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs
  83. 2
      src/UnitTests/LinearAlgebraTests/Double/MatrixLoader.cs
  84. 2
      src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs
  85. 2
      src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs
  86. 6
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs
  87. 2
      src/UnitTests/LinearAlgebraTests/Single/MatrixLoader.cs
  88. 2
      src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs
  89. 12
      src/UnitTests/LinearAlgebraTests/Single/MatrixTests.Arithmetic.cs
  90. 6
      src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs
  91. 5
      src/UnitTests/StatisticsTests/MCMCTests/HybridMCTest.cs
  92. 4
      src/UnitTests/StatisticsTests/MCMCTests/MCMCDiagnosticsTest.cs
  93. 14
      src/UnitTests/StatisticsTests/MCMCTests/MetropolisHastingsSamplerTests.cs
  94. 12
      src/UnitTests/StatisticsTests/MCMCTests/MetropolisSamplerTests.cs
  95. 12
      src/UnitTests/StatisticsTests/MCMCTests/RejectionSamplerTests.cs
  96. 5
      src/UnitTests/StatisticsTests/MCMCTests/UnivariateHybridMCTest.cs
  97. 13
      src/UnitTests/StatisticsTests/StatisticsTests.cs

81
src/Numerics/Distributions/Discrete/Bernoulli.cs → src/Numerics/Distributions/Bernoulli.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Discrete Univariate Bernoulli distribution.
/// The Bernoulli distribution is a distribution over bits. The parameter /// The Bernoulli distribution is a distribution over bits. The parameter
/// p specifies the probability that a 1 is generated. /// p specifies the probability that a 1 is generated.
/// <a href="http://en.wikipedia.org/wiki/Bernoulli_distribution">Wikipedia - Bernoulli distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Bernoulli_distribution">Wikipedia - Bernoulli distribution</a>.
@ -50,7 +55,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Bernoulli class. /// Initializes a new instance of the Bernoulli class.
@ -59,7 +64,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If the Bernoulli parameter is not in the range [0,1].</exception> /// <exception cref="ArgumentOutOfRangeException">If the Bernoulli parameter is not in the range [0,1].</exception>
public Bernoulli(double p) public Bernoulli(double p)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(p); SetParameters(p);
} }
@ -69,9 +74,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="p">The probability of generating one.</param> /// <param name="p">The probability of generating one.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentOutOfRangeException">If the Bernoulli parameter is not in the range [0,1].</exception> /// <exception cref="ArgumentOutOfRangeException">If the Bernoulli parameter is not in the range [0,1].</exception>
public Bernoulli(double p, Random randomSource) public Bernoulli(double p, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(p); SetParameters(p);
} }
@ -118,12 +123,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(value); } set { SetParameters(value); }
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -193,30 +196,6 @@ namespace MathNet.Numerics.Distributions
get { return 1; } get { return 1; }
} }
/// <summary>
/// Computes the cumulative distribution function of the Bernoulli distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
if (x < 0.0)
{
return 0.0;
}
if (x < 1.0)
{
return 1.0 - _p;
}
return 1.0;
}
#endregion
#region IDiscreteDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -234,7 +213,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the probability mass function. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <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> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -254,7 +233,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the log probability mass function. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <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> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -268,7 +247,25 @@ namespace MathNet.Numerics.Distributions
return k == 1 ? Math.Log(_p) : Double.NegativeInfinity; return k == 1 ? Math.Log(_p) : Double.NegativeInfinity;
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
if (x < 0.0)
{
return 0.0;
}
if (x < 1.0)
{
return 1.0 - _p;
}
return 1.0;
}
/// <summary> /// <summary>
/// Generates one sample from the Bernoulli distribution. /// Generates one sample from the Bernoulli distribution.
@ -276,7 +273,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random source to use.</param> /// <param name="rnd">The random source to use.</param>
/// <param name="p">The probability of generating a one.</param> /// <param name="p">The probability of generating a one.</param>
/// <returns>A random sample from the Bernoulli distribution.</returns> /// <returns>A random sample from the Bernoulli distribution.</returns>
internal static int SampleUnchecked(Random rnd, double p) internal static int SampleUnchecked(System.Random rnd, double p)
{ {
if (rnd.NextDouble() < p) if (rnd.NextDouble() < p)
{ {
@ -313,7 +310,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="p">The probability of generating a 1.</param> /// <param name="p">The probability of generating a 1.</param>
/// <returns>A sample from the Bernoulli distribution.</returns> /// <returns>A sample from the Bernoulli distribution.</returns>
public static int Sample(Random rnd, double p) public static int Sample(System.Random rnd, double p)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(p)) if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
{ {
@ -329,7 +326,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="p">The probability of generating a 1.</param> /// <param name="p">The probability of generating a 1.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<int> Samples(Random rnd, double p) public static IEnumerable<int> Samples(System.Random rnd, double p)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(p)) if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
{ {

59
src/Numerics/Distributions/Continuous/Beta.cs → src/Numerics/Distributions/Beta.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,14 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Implements the Beta distribution. For details about this distribution, see /// Continuous Univariate Beta distribution.
/// For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Beta_distribution">Wikipedia - Beta distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Beta_distribution">Wikipedia - Beta distribution</a>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
@ -61,7 +66,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Beta class. /// Initializes a new instance of the Beta class.
@ -71,7 +76,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If any of the Beta parameters are negative.</exception> /// <exception cref="ArgumentOutOfRangeException">If any of the Beta parameters are negative.</exception>
public Beta(double a, double b) public Beta(double a, double b)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(a, b); SetParameters(a, b);
} }
@ -82,9 +87,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="b">The b shape parameter of the Beta distribution.</param> /// <param name="b">The b shape parameter of the Beta distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentOutOfRangeException">If any of the Beta parameters are negative.</exception> /// <exception cref="ArgumentOutOfRangeException">If any of the Beta parameters are negative.</exception>
public Beta(double a, double b, Random randomSource) public Beta(double a, double b, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(a, b); SetParameters(a, b);
} }
@ -143,12 +148,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shapeA, value); } set { SetParameters(_shapeA, value); }
} }
#region IDistribution implementation
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -242,9 +245,9 @@ namespace MathNet.Numerics.Distributions
} }
return SpecialFunctions.BetaLn(_shapeA, _shapeB) return SpecialFunctions.BetaLn(_shapeA, _shapeB)
- ((_shapeA - 1.0)*SpecialFunctions.DiGamma(_shapeA)) - ((_shapeA - 1.0)*SpecialFunctions.DiGamma(_shapeA))
- ((_shapeB - 1.0)*SpecialFunctions.DiGamma(_shapeB)) - ((_shapeB - 1.0)*SpecialFunctions.DiGamma(_shapeB))
+ ((_shapeA + _shapeB - 2.0)*SpecialFunctions.DiGamma(_shapeA + _shapeB)); + ((_shapeA + _shapeB - 2.0)*SpecialFunctions.DiGamma(_shapeA + _shapeB));
} }
} }
@ -286,14 +289,10 @@ namespace MathNet.Numerics.Distributions
} }
return 2.0*(_shapeB - _shapeA)*Math.Sqrt(_shapeA + _shapeB + 1.0) return 2.0*(_shapeB - _shapeA)*Math.Sqrt(_shapeA + _shapeB + 1.0)
/((_shapeA + _shapeB + 2.0)*Math.Sqrt(_shapeA*_shapeB)); /((_shapeA + _shapeB + 2.0)*Math.Sqrt(_shapeA*_shapeB));
} }
} }
#endregion
#region IContinuousDistribution implementation
/// <summary> /// <summary>
/// Gets the mode of the Beta distribution; when there are multiple answers, this routine will return 0.5. /// Gets the mode of the Beta distribution; when there are multiple answers, this routine will return 0.5.
/// </summary> /// </summary>
@ -365,7 +364,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the Beta distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -421,7 +420,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the Beta distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -480,10 +479,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the cumulative distribution function of the Beta distribution. /// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
if (x < 0.0) if (x < 0.0)
@ -539,8 +538,6 @@ namespace MathNet.Numerics.Distributions
return SpecialFunctions.BetaRegularized(_shapeA, _shapeB, x); return SpecialFunctions.BetaRegularized(_shapeA, _shapeB, x);
} }
#endregion
/// <summary> /// <summary>
/// Samples Beta distributed random variables by sampling two Gamma variables and normalizing. /// Samples Beta distributed random variables by sampling two Gamma variables and normalizing.
/// </summary> /// </summary>
@ -548,7 +545,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="a">The A shape parameter.</param> /// <param name="a">The A shape parameter.</param>
/// <param name="b">The B shape parameter.</param> /// <param name="b">The B shape parameter.</param>
/// <returns>a random number from the Beta distribution.</returns> /// <returns>a random number from the Beta distribution.</returns>
internal static double SampleUnchecked(Random rnd, double a, double b) internal static double SampleUnchecked(System.Random rnd, double a, double b)
{ {
var x = Gamma.SampleUnchecked(rnd, a, 1.0); var x = Gamma.SampleUnchecked(rnd, a, 1.0);
var y = Gamma.SampleUnchecked(rnd, b, 1.0); var y = Gamma.SampleUnchecked(rnd, b, 1.0);
@ -583,7 +580,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="a">The a shape parameter of the Beta distribution.</param> /// <param name="a">The a shape parameter of the Beta distribution.</param>
/// <param name="b">The b shape parameter of the Beta distribution.</param> /// <param name="b">The b shape parameter of the Beta distribution.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double a, double b) public static double Sample(System.Random rnd, double a, double b)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(a, b)) if (Control.CheckDistributionParameters && !IsValidParameterSet(a, b))
{ {
@ -600,7 +597,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="a">The a shape parameter of the Beta distribution.</param> /// <param name="a">The a shape parameter of the Beta distribution.</param>
/// <param name="b">The b shape parameter of the Beta distribution.</param> /// <param name="b">The b shape parameter of the Beta distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double a, double b) public static IEnumerable<double> Samples(System.Random rnd, double a, double b)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(a, b)) if (Control.CheckDistributionParameters && !IsValidParameterSet(a, b))
{ {

95
src/Numerics/Distributions/Discrete/Binomial.cs → src/Numerics/Distributions/Binomial.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,14 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Implements the binomial distribution. For details about this distribution, see /// Discrete Univariate Binomial distribution.
/// For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Binomial_distribution">Wikipedia - Binomial distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Binomial_distribution">Wikipedia - Binomial distribution</a>.
/// </summary> /// </summary>
/// <remarks><para>The distribution is parameterized by a probability (between 0.0 and 1.0).</para> /// <remarks><para>The distribution is parameterized by a probability (between 0.0 and 1.0).</para>
@ -55,7 +60,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Binomial class. /// Initializes a new instance of the Binomial class.
@ -66,7 +71,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
public Binomial(double p, int n) public Binomial(double p, int n)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(p, n); SetParameters(p, n);
} }
@ -78,9 +83,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="p"/> is not in the interval [0.0,1.0].</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="p"/> is not in the interval [0.0,1.0].</exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
public Binomial(double p, int n, Random randomSource) public Binomial(double p, int n, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(p, n); SetParameters(p, n);
} }
@ -140,12 +145,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_p, value); } set { SetParameters(_p, value); }
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -230,36 +233,6 @@ namespace MathNet.Numerics.Distributions
get { return _n; } get { return _n; }
} }
/// <summary>
/// Computes the cumulative distribution function of the Binomial distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
if (x < 0.0)
{
return 0.0;
}
if (x > _n)
{
return 1.0;
}
var cdf = 0.0;
for (var i = 0; i <= (int) Math.Floor(x); i++)
{
cdf += Combinatorics.Combinations(_n, i)*Math.Pow(_p, i)*Math.Pow(1.0 - _p, _n - i);
}
return cdf;
}
#endregion
#region IDiscreteDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -290,7 +263,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the probability mass function. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <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> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -330,7 +303,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the log probability mass function. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <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> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -369,7 +342,31 @@ namespace MathNet.Numerics.Distributions
return SpecialFunctions.BinomialLn(_n, k) + (k*Math.Log(_p)) + ((_n - k)*Math.Log(1.0 - _p)); return SpecialFunctions.BinomialLn(_n, k) + (k*Math.Log(_p)) + ((_n - k)*Math.Log(1.0 - _p));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
if (x < 0.0)
{
return 0.0;
}
if (x > _n)
{
return 1.0;
}
var cdf = 0.0;
for (var i = 0; i <= (int) Math.Floor(x); i++)
{
cdf += Combinatorics.Combinations(_n, i)*Math.Pow(_p, i)*Math.Pow(1.0 - _p, _n - i);
}
return cdf;
}
/// <summary> /// <summary>
/// Generates a sample from the Binomial distribution without doing parameter checking. /// Generates a sample from the Binomial distribution without doing parameter checking.
@ -378,7 +375,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="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> /// <param name="n">The number of trials; must be positive.</param>
/// <returns>The number of successful trials.</returns> /// <returns>The number of successful trials.</returns>
internal static int SampleUnchecked(Random rnd, double p, int n) internal static int SampleUnchecked(System.Random rnd, double p, int n)
{ {
var k = 0; var k = 0;
for (var i = 0; i < n; i++) for (var i = 0; i < n; i++)
@ -417,7 +414,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="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> /// <param name="n">The number of trials; must be positive.</param>
/// <returns>The number of successes in <paramref name="n"/> trials.</returns> /// <returns>The number of successes in <paramref name="n"/> trials.</returns>
public static int Sample(Random rnd, double p, int n) public static int Sample(System.Random rnd, double p, int n)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n)) if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
{ {
@ -434,7 +431,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="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> /// <param name="n">The number of trials; must be positive.</param>
/// <returns>a sequence of successes in <paramref name="n"/> trials.</returns> /// <returns>a sequence of successes in <paramref name="n"/> trials.</returns>
public static IEnumerable<int> Samples(Random rnd, double p, int n) public static IEnumerable<int> Samples(System.Random rnd, double p, int n)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n)) if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
{ {

87
src/Numerics/Distributions/Discrete/Categorical.cs → src/Numerics/Distributions/Categorical.cs

@ -28,16 +28,17 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using MathNet.Numerics.Properties;
using MathNet.Numerics.Statistics;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using System.Linq;
using Properties;
using Statistics;
/// <summary> /// <summary>
/// Implements the categorical distribution. For details about this distribution, see /// Discrete Univariate Categorical distribution.
/// For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Categorical_distribution">Wikipedia - Categorical distribution</a>. This /// <a href="http://en.wikipedia.org/wiki/Categorical_distribution">Wikipedia - Categorical distribution</a>. This
/// distribution is sometimes called the Discrete distribution. /// distribution is sometimes called the Discrete distribution.
/// </summary> /// </summary>
@ -51,7 +52,7 @@ namespace MathNet.Numerics.Distributions
/// to <c>false</c>, all parameter checks can be turned off.</para></remarks> /// to <c>false</c>, all parameter checks can be turned off.</para></remarks>
public class Categorical : IDiscreteDistribution public class Categorical : IDiscreteDistribution
{ {
Random _random; System.Random _random;
double[] _pmfNormalized; double[] _pmfNormalized;
double[] _cdfUnnormalized; double[] _cdfUnnormalized;
@ -63,7 +64,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If any of the probabilities are negative or do not sum to one.</exception> /// <exception cref="ArgumentException">If any of the probabilities are negative or do not sum to one.</exception>
public Categorical(double[] probabilityMass) public Categorical(double[] probabilityMass)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(probabilityMass); SetParameters(probabilityMass);
} }
@ -74,9 +75,9 @@ namespace MathNet.Numerics.Distributions
/// as this is often impossible using floating point arithmetic.</param> /// as this is often impossible using floating point arithmetic.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentException">If any of the probabilities are negative or do not sum to one.</exception> /// <exception cref="ArgumentException">If any of the probabilities are negative or do not sum to one.</exception>
public Categorical(double[] probabilityMass, Random randomSource) public Categorical(double[] probabilityMass, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(probabilityMass); SetParameters(probabilityMass);
} }
@ -102,7 +103,7 @@ namespace MathNet.Numerics.Distributions
p[i] = histogram[i].Count; p[i] = histogram[i].Count;
} }
_random = new Random(); _random = new System.Random();
SetParameters(p); SetParameters(p);
} }
@ -201,12 +202,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(value); } set { SetParameters(value); }
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -277,30 +276,6 @@ namespace MathNet.Numerics.Distributions
get { return _pmfNormalized.Length - 1; } get { return _pmfNormalized.Length - 1; }
} }
/// <summary>
/// Computes the cumulative distribution function of the Binomial distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
if (x < 0.0)
{
return 0.0;
}
if (x >= _cdfUnnormalized.Length)
{
return 1.0;
}
return _cdfUnnormalized[(int) Math.Floor(x)]/_cdfUnnormalized[_cdfUnnormalized.Length - 1];
}
#endregion
#region IDiscreteDistribution Members
/// <summary> /// <summary>
/// Gets he mode of the distribution. /// Gets he mode of the distribution.
/// </summary> /// </summary>
@ -319,7 +294,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the probability mass function. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <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> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -339,7 +314,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the log probability mass function. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <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> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -358,7 +333,25 @@ namespace MathNet.Numerics.Distributions
return Math.Log(_pmfNormalized[k]); return Math.Log(_pmfNormalized[k]);
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
if (x < 0.0)
{
return 0.0;
}
if (x >= _cdfUnnormalized.Length)
{
return 1.0;
}
return _cdfUnnormalized[(int) Math.Floor(x)]/_cdfUnnormalized[_cdfUnnormalized.Length - 1];
}
/// <summary> /// <summary>
/// Computes the cumulative distribution function. This method performs no parameter checking. /// Computes the cumulative distribution function. This method performs no parameter checking.
@ -385,7 +378,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="cdfUnnormalized">The (unnormalized) cumulative distribution of the probability distribution.</param> /// <param name="cdfUnnormalized">The (unnormalized) cumulative distribution of the probability distribution.</param>
/// <returns>One sample from the categorical distribution implied by <paramref name="cdfUnnormalized"/>.</returns> /// <returns>One sample from the categorical distribution implied by <paramref name="cdfUnnormalized"/>.</returns>
internal static int SampleUnchecked(Random rnd, double[] cdfUnnormalized) internal static int SampleUnchecked(System.Random rnd, double[] cdfUnnormalized)
{ {
// TODO : use binary search to speed up this procedure. // TODO : use binary search to speed up this procedure.
var u = rnd.NextDouble()*cdfUnnormalized[cdfUnnormalized.Length - 1]; var u = rnd.NextDouble()*cdfUnnormalized[cdfUnnormalized.Length - 1];
@ -426,7 +419,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="cdfUnnormalized">An array of the cumulative distribution. Not assumed to be normalized.</param> /// <param name="cdfUnnormalized">An array of the cumulative distribution. Not assumed to be normalized.</param>
/// <returns>One random integer between 0 and the size of the categorical (exclusive).</returns> /// <returns>One random integer between 0 and the size of the categorical (exclusive).</returns>
public static int SampleWithCumulativeDistribution(Random rnd, double[] cdfUnnormalized) public static int SampleWithCumulativeDistribution(System.Random rnd, double[] cdfUnnormalized)
{ {
if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized)) if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized))
{ {
@ -442,7 +435,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="pmfUnnormalized">An array of nonnegative ratios. Not assumed to be normalized.</param> /// <param name="pmfUnnormalized">An array of nonnegative ratios. Not assumed to be normalized.</param>
/// <returns>One random integer between 0 and the size of the categorical (exclusive).</returns> /// <returns>One random integer between 0 and the size of the categorical (exclusive).</returns>
public static int SampleWithProbabilityMass(Random rnd, double[] pmfUnnormalized) public static int SampleWithProbabilityMass(System.Random rnd, double[] pmfUnnormalized)
{ {
if (Control.CheckDistributionParameters && !IsValidProbabilityMass(pmfUnnormalized)) if (Control.CheckDistributionParameters && !IsValidProbabilityMass(pmfUnnormalized))
{ {
@ -459,7 +452,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="cdfUnnormalized">An array of the cumulative distribution. Not assumed to be normalized.</param> /// <param name="cdfUnnormalized">An array of the cumulative distribution. Not assumed to be normalized.</param>
/// <returns>random integers between 0 and the size of the categorical (exclusive).</returns> /// <returns>random integers between 0 and the size of the categorical (exclusive).</returns>
public static IEnumerable<int> SamplesWithCumulativeDistribution(Random rnd, double[] cdfUnnormalized) public static IEnumerable<int> SamplesWithCumulativeDistribution(System.Random rnd, double[] cdfUnnormalized)
{ {
if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized)) if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized))
{ {
@ -478,7 +471,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="pmfUnnormalized">An array of nonnegative ratios. Not assumed to be normalized.</param> /// <param name="pmfUnnormalized">An array of nonnegative ratios. Not assumed to be normalized.</param>
/// <returns>random integers between 0 and the size of the categorical (exclusive).</returns> /// <returns>random integers between 0 and the size of the categorical (exclusive).</returns>
public static IEnumerable<int> SamplesWithProbabilityMass(Random rnd, double[] pmfUnnormalized) public static IEnumerable<int> SamplesWithProbabilityMass(System.Random rnd, double[] pmfUnnormalized)
{ {
if (Control.CheckDistributionParameters && !IsValidProbabilityMass(pmfUnnormalized)) if (Control.CheckDistributionParameters && !IsValidProbabilityMass(pmfUnnormalized))
{ {

55
src/Numerics/Distributions/Continuous/Cauchy.cs → src/Numerics/Distributions/Cauchy.cs

@ -24,13 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Continuous Univariate Cauchy distribution.
/// The Cauchy distribution is a symmetric continuous probability distribution. For details about this distribution, see /// The Cauchy distribution is a symmetric continuous probability distribution. For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/cauchy_distribution">Wikipedia - Cauchy distribution</a>. /// <a href="http://en.wikipedia.org/wiki/cauchy_distribution">Wikipedia - Cauchy distribution</a>.
/// </summary> /// </summary>
@ -49,7 +50,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Cauchy"/> class with the location parameter set to 0 and the scale parameter set to 1 /// Initializes a new instance of the <see cref="Cauchy"/> class with the location parameter set to 0 and the scale parameter set to 1
@ -66,7 +67,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception> /// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Cauchy(double location, double scale) public Cauchy(double location, double scale)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(location, scale); SetParameters(location, scale);
} }
@ -77,9 +78,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale parameter for the distribution.</param> /// <param name="scale">The scale parameter for the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception> /// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Cauchy(double location, double scale, Random randomSource) public Cauchy(double location, double scale, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(location, scale); SetParameters(location, scale);
} }
@ -138,12 +139,10 @@ namespace MathNet.Numerics.Distributions
return "Cauchy(Location = " + Median + ", Scale = " + _scale + ")"; return "Cauchy(Location = " + Median + ", Scale = " + _scale + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -197,20 +196,6 @@ namespace MathNet.Numerics.Distributions
get { throw new NotSupportedException(); } get { throw new NotSupportedException(); }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return ((1.0/Constants.Pi)*Math.Atan((x - Median)/_scale)) + 0.5;
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -241,7 +226,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -251,7 +236,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -260,7 +245,15 @@ namespace MathNet.Numerics.Distributions
return -Math.Log(Constants.Pi*_scale*(1.0 + (((x - Median)/_scale)*((x - Median)/_scale)))); return -Math.Log(Constants.Pi*_scale*(1.0 + (((x - Median)/_scale)*((x - Median)/_scale))));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return ((1.0/Constants.Pi)*Math.Atan((x - Median)/_scale)) + 0.5;
}
/// <summary> /// <summary>
/// Samples the distribution. /// Samples the distribution.
@ -269,7 +262,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="location">The location shape parameter.</param> /// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, double location, double scale) internal static double SampleUnchecked(System.Random rnd, double location, double scale)
{ {
var u = rnd.NextDouble(); var u = rnd.NextDouble();
return location + (scale*Math.Tan(Constants.Pi*(u - 0.5))); return location + (scale*Math.Tan(Constants.Pi*(u - 0.5)));
@ -303,7 +296,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="location">The location shape parameter.</param> /// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double location, double scale) public static double Sample(System.Random rnd, double location, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
{ {
@ -320,7 +313,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="location">The location shape parameter.</param> /// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double location, double scale) public static IEnumerable<double> Samples(System.Random rnd, double location, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
{ {

64
src/Numerics/Distributions/Continuous/Chi.cs → src/Numerics/Distributions/Chi.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,15 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// This class implements functionality for the Chi distribution. This distribution is /// Continuous Univariate Chi distribution.
/// a continuous probability distribution. The distribution usually arises when a k-dimensional vector's orthogonal /// This distribution is a continuous probability distribution. The distribution usually arises when a k-dimensional vector's orthogonal
/// components are independent and each follow a standard normal distribution. The length of the vector will /// components are independent and each follow a standard normal distribution. The length of the vector will
/// then have a chi distribution. /// then have a chi distribution.
/// <a href="http://en.wikipedia.org/wiki/Chi_distribution">Wikipedia - Chi distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Chi_distribution">Wikipedia - Chi distribution</a>.
@ -52,7 +56,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Chi"/> class. /// Initializes a new instance of the <see cref="Chi"/> class.
@ -60,7 +64,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="dof">The degrees of freedom for the Chi distribution.</param> /// <param name="dof">The degrees of freedom for the Chi distribution.</param>
public Chi(double dof) public Chi(double dof)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(dof); SetParameters(dof);
} }
@ -69,9 +73,9 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="dof">The degrees of freedom for the Chi distribution.</param> /// <param name="dof">The degrees of freedom for the Chi distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Chi(double dof, Random randomSource) public Chi(double dof, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(dof); SetParameters(dof);
} }
@ -118,12 +122,10 @@ namespace MathNet.Numerics.Distributions
return "Chi(DoF = " + _dof + ")"; return "Chi(DoF = " + _dof + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the distribution's random number generator. /// Gets or sets the distribution's random number generator.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -181,20 +183,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaLowerIncomplete(_dof/2.0, x*x/2.0)/SpecialFunctions.Gamma(_dof/2.0);
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -236,7 +224,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -246,7 +234,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -255,7 +243,15 @@ namespace MathNet.Numerics.Distributions
return ((1.0 - (_dof/2.0))*Math.Log(2.0)) + ((_dof - 1.0)*Math.Log(x)) - (x*x/2.0) - SpecialFunctions.GammaLn(_dof/2.0); return ((1.0 - (_dof/2.0))*Math.Log(2.0)) + ((_dof - 1.0)*Math.Log(x)) - (x*x/2.0) - SpecialFunctions.GammaLn(_dof/2.0);
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaLowerIncomplete(_dof/2.0, x*x/2.0)/SpecialFunctions.Gamma(_dof/2.0);
}
/// <summary> /// <summary>
/// Samples the distribution. /// Samples the distribution.
@ -263,7 +259,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">Degrees of Freedom</param> /// <param name="dof">Degrees of Freedom</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, int dof) internal static double SampleUnchecked(System.Random rnd, int dof)
{ {
double sum = 0; double sum = 0;
for (var i = 0; i < dof; i++) for (var i = 0; i < dof; i++)
@ -302,7 +298,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">Degrees of Freedom</param> /// <param name="dof">Degrees of Freedom</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, int dof) public static double Sample(System.Random rnd, int dof)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof)) if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
{ {
@ -318,7 +314,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">Degrees of Freedom</param> /// <param name="dof">Degrees of Freedom</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, int dof) public static IEnumerable<double> Samples(System.Random rnd, int dof)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof)) if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
{ {

60
src/Numerics/Distributions/Continuous/ChiSquare.cs → src/Numerics/Distributions/ChiSquare.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,15 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// This class implements functionality for the ChiSquare distribution. This distribution is /// Continuous Univariate ChiSquare distribution.
/// a sum of the squares of k independent standard normal random variables. /// 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>. /// <a href="http://en.wikipedia.org/wiki/Chi-square_distribution">Wikipedia - ChiSquare distribution</a>.
/// </summary> /// </summary>
/// <remarks><para>The distribution will use the <see cref="System.Random"/> by default. /// <remarks><para>The distribution will use the <see cref="System.Random"/> by default.
@ -49,7 +49,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ChiSquare"/> class. /// Initializes a new instance of the <see cref="ChiSquare"/> class.
@ -57,7 +57,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="dof">The degrees of freedom for the ChiSquare distribution.</param> /// <param name="dof">The degrees of freedom for the ChiSquare distribution.</param>
public ChiSquare(double dof) public ChiSquare(double dof)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(dof); SetParameters(dof);
} }
@ -66,9 +66,9 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="dof">The degrees of freedom for the ChiSquare distribution.</param> /// <param name="dof">The degrees of freedom for the ChiSquare distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public ChiSquare(double dof, Random randomSource) public ChiSquare(double dof, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(dof); SetParameters(dof);
} }
@ -115,12 +115,10 @@ namespace MathNet.Numerics.Distributions
return "ChiSquare(DoF = " + Mean + ")"; return "ChiSquare(DoF = " + Mean + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the distribution's random number generator. /// Gets or sets the distribution's random number generator.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -171,20 +169,6 @@ namespace MathNet.Numerics.Distributions
get { return Math.Sqrt(8.0/Mean); } get { return Math.Sqrt(8.0/Mean); }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaLowerIncomplete(Mean/2.0, x/2.0)/SpecialFunctions.Gamma(Mean/2.0);
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -218,7 +202,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -228,7 +212,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -237,7 +221,15 @@ namespace MathNet.Numerics.Distributions
return (-x/2.0) + (((Mean/2.0) - 1.0)*Math.Log(x)) - ((Mean/2.0)*Math.Log(2)) - SpecialFunctions.GammaLn(Mean/2.0); return (-x/2.0) + (((Mean/2.0) - 1.0)*Math.Log(x)) - ((Mean/2.0)*Math.Log(2)) - SpecialFunctions.GammaLn(Mean/2.0);
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaLowerIncomplete(Mean/2.0, x/2.0)/SpecialFunctions.Gamma(Mean/2.0);
}
/// <summary> /// <summary>
/// Samples the distribution. /// Samples the distribution.
@ -245,7 +237,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">The degrees of freedom.</param> /// <param name="dof">The degrees of freedom.</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, double dof) internal static double SampleUnchecked(System.Random rnd, double dof)
{ {
//Use the simple method if the dof is an integer anyway //Use the simple method if the dof is an integer anyway
if (Math.Floor(dof) == dof && dof < Int32.MaxValue) if (Math.Floor(dof) == dof && dof < Int32.MaxValue)
@ -290,7 +282,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">The degrees of freedom.</param> /// <param name="dof">The degrees of freedom.</param>
/// <returns>a sample from the distribution. </returns> /// <returns>a sample from the distribution. </returns>
public static double Sample(Random rnd, double dof) public static double Sample(System.Random rnd, double dof)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof)) if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
{ {
@ -306,7 +298,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="dof">The degrees of freedom.</param> /// <param name="dof">The degrees of freedom.</param>
/// <returns>a sample from the distribution. </returns> /// <returns>a sample from the distribution. </returns>
public static IEnumerable<double> Samples(Random rnd, double dof) public static IEnumerable<double> Samples(System.Random rnd, double dof)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(dof)) if (Control.CheckDistributionParameters && !IsValidParameterSet(dof))
{ {

45
src/Numerics/Distributions/Continuous/ContinuousUniform.cs → src/Numerics/Distributions/ContinuousUniform.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Continuous Univariate Uniform distribution.
/// The continuous uniform distribution is a distribution over real numbers. For details about this distribution, see /// The continuous uniform distribution is a distribution over real numbers. For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29">Wikipedia - Continuous uniform distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29">Wikipedia - Continuous uniform distribution</a>.
/// </summary> /// </summary>
@ -58,7 +59,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the ContinuousUniform class with lower bound 0 and upper bound 1. /// Initializes a new instance of the ContinuousUniform class with lower bound 0 and upper bound 1.
@ -75,7 +76,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If the upper bound is smaller than the lower bound.</exception> /// <exception cref="ArgumentException">If the upper bound is smaller than the lower bound.</exception>
public ContinuousUniform(double lower, double upper) public ContinuousUniform(double lower, double upper)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(lower, upper); SetParameters(lower, upper);
} }
@ -86,9 +87,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="upper">Upper bound; must be at least as large as lower.</param> /// <param name="upper">Upper bound; must be at least as large as lower.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentException">If the upper bound is smaller than the lower bound.</exception> /// <exception cref="ArgumentException">If the upper bound is smaller than the lower bound.</exception>
public ContinuousUniform(double lower, double upper, Random randomSource) public ContinuousUniform(double lower, double upper, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(lower, upper); SetParameters(lower, upper);
} }
@ -147,12 +148,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lower, value); } set { SetParameters(_lower, value); }
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -207,10 +206,6 @@ namespace MathNet.Numerics.Distributions
get { return 0.0; } get { return 0.0; }
} }
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -246,7 +241,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -261,7 +256,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -276,10 +271,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the cumulative distribution function of the distribution. /// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
if (x <= _lower) if (x <= _lower)
@ -295,8 +290,6 @@ namespace MathNet.Numerics.Distributions
return (x - _lower)/(_upper - _lower); return (x - _lower)/(_upper - _lower);
} }
#endregion
/// <summary> /// <summary>
/// Generates one sample from the <c>ContinuousUniform</c> distribution without parameter checking. /// Generates one sample from the <c>ContinuousUniform</c> distribution without parameter checking.
/// </summary> /// </summary>
@ -304,7 +297,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="lower">The lower bound of the uniform random variable.</param> /// <param name="lower">The lower bound of the uniform random variable.</param>
/// <param name="upper">The upper 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> /// <returns>a uniformly distributed random number.</returns>
internal static double SampleUnchecked(Random rnd, double lower, double upper) internal static double SampleUnchecked(System.Random rnd, double lower, double upper)
{ {
return lower + (rnd.NextDouble()*(upper - lower)); return lower + (rnd.NextDouble()*(upper - lower));
} }
@ -337,7 +330,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="lower">The lower bound of the uniform random variable.</param> /// <param name="lower">The lower bound of the uniform random variable.</param>
/// <param name="upper">The upper bound of the uniform random variable.</param> /// <param name="upper">The upper bound of the uniform random variable.</param>
/// <returns>a uniformly distributed sample.</returns> /// <returns>a uniformly distributed sample.</returns>
public static double Sample(Random rnd, double lower, double upper) public static double Sample(System.Random rnd, double lower, double upper)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper))
{ {
@ -354,7 +347,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="lower">The lower bound of the uniform random variable.</param> /// <param name="lower">The lower bound of the uniform random variable.</param>
/// <param name="upper">The upper bound of the uniform random variable.</param> /// <param name="upper">The upper bound of the uniform random variable.</param>
/// <returns>a sequence of uniformly distributed samples.</returns> /// <returns>a sequence of uniformly distributed samples.</returns>
public static IEnumerable<double> Samples(Random rnd, double lower, double upper) public static IEnumerable<double> Samples(System.Random rnd, double lower, double upper)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper))
{ {

81
src/Numerics/Distributions/Discrete/ConwayMaxwellPoisson.cs → src/Numerics/Distributions/ConwayMaxwellPoisson.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Discrete Univariate Conway-Maxwell-Poisson distribution.
/// <para>The Conway-Maxwell-Poisson distribution is a generalization of the Poisson, Geometric and Bernoulli /// <para>The Conway-Maxwell-Poisson distribution is a generalization of the Poisson, Geometric and Bernoulli
/// distributions. It is parameterized by two real numbers "lambda" and "nu". For /// distributions. It is parameterized by two real numbers "lambda" and "nu". For
/// <list> /// <list>
@ -82,7 +87,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ConwayMaxwellPoisson"/> class. /// Initializes a new instance of the <see cref="ConwayMaxwellPoisson"/> class.
@ -91,7 +96,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The nu parameter.</param> /// <param name="nu">The nu parameter.</param>
public ConwayMaxwellPoisson(double lambda, double nu) public ConwayMaxwellPoisson(double lambda, double nu)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(lambda, nu); SetParameters(lambda, nu);
} }
@ -101,9 +106,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="lambda">The lambda parameter.</param> /// <param name="lambda">The lambda parameter.</param>
/// <param name="nu">The nu parameter.</param> /// <param name="nu">The nu parameter.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public ConwayMaxwellPoisson(double lambda, double nu, Random randomSource) public ConwayMaxwellPoisson(double lambda, double nu, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(lambda, nu); SetParameters(lambda, nu);
} }
@ -166,12 +171,10 @@ namespace MathNet.Numerics.Distributions
return "ConwayMaxwellPoisson(Lambda = " + _lambda + ", Nu = " + _nu + ")"; return "ConwayMaxwellPoisson(Lambda = " + _lambda + ", Nu = " + _nu + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -332,26 +335,6 @@ namespace MathNet.Numerics.Distributions
get { throw new NotSupportedException(); } get { throw new NotSupportedException(); }
} }
/// <summary>
/// Computes the cumulative distribution function of the <c>ConwayMaxwellPoisson</c> distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
double sum = 0;
for (var i = 0; i < x + 1; i++)
{
sum += Probability(i);
}
return sum;
}
#endregion
#region IDiscreteDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution /// Gets the mode of the distribution
/// </summary> /// </summary>
@ -385,30 +368,40 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the probability of a specific value. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
/// the probability mass at location <paramref name="k"/>.
/// </returns>
public double Probability(int k) public double Probability(int k)
{ {
return Math.Pow(_lambda, k)/Math.Pow(SpecialFunctions.Factorial(k), _nu)/Z; return Math.Pow(_lambda, k)/Math.Pow(SpecialFunctions.Factorial(k), _nu)/Z;
} }
/// <summary> /// <summary>
/// Computes the log probability of a specific value. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
/// the log probability mass at location <paramref name="k"/>.
/// </returns>
public double ProbabilityLn(int k) public double ProbabilityLn(int k)
{ {
return Math.Log(Probability(k)); return Math.Log(Probability(k));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
double sum = 0;
for (var i = 0; i < x + 1; i++)
{
sum += Probability(i);
}
return sum;
}
/// <summary> /// <summary>
/// Gets the normalization constant of the Conway-Maxwell-Poisson distribution. /// Gets the normalization constant of the Conway-Maxwell-Poisson distribution.
@ -478,7 +471,7 @@ namespace MathNet.Numerics.Distributions
/// <returns> /// <returns>
/// One sample from the distribution implied by <paramref name="lambda"/>, <paramref name="nu"/>, and <paramref name="z"/>. /// One sample from the distribution implied by <paramref name="lambda"/>, <paramref name="nu"/>, and <paramref name="z"/>.
/// </returns> /// </returns>
internal static int SampleUnchecked(Random rnd, double lambda, double nu, double z) internal static int SampleUnchecked(System.Random rnd, double lambda, double nu, double z)
{ {
var u = rnd.NextDouble(); var u = rnd.NextDouble();
var p = 1.0/z; var p = 1.0/z;
@ -524,7 +517,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter</param> /// <param name="lambda">The lambda parameter</param>
/// <param name="nu">The nu parameter.</param> /// <param name="nu">The nu parameter.</param>
public static int Sample(Random rnd, double lambda, double nu) public static int Sample(System.Random rnd, double lambda, double nu)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda, nu)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda, nu))
{ {
@ -541,7 +534,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter</param> /// <param name="lambda">The lambda parameter</param>
/// <param name="nu">The nu parameter.</param> /// <param name="nu">The nu parameter.</param>
public static IEnumerable<int> Samples(Random rnd, double lambda, double nu) public static IEnumerable<int> Samples(System.Random rnd, double lambda, double nu)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda, nu)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda, nu))
{ {

28
src/Numerics/Distributions/Multivariate/Dirichlet.cs → src/Numerics/Distributions/Dirichlet.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Linq;
using Properties;
/// <summary> /// <summary>
/// Implements the multivariate Dirichlet distribution. For details about this distribution, see /// Multivariate Dirichlet distribution. For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Dirichlet_distribution">Wikipedia - Dirichlet distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Dirichlet_distribution">Wikipedia - Dirichlet distribution</a>.
/// </summary> /// </summary>
/// <remarks><para>The distribution will use the <see cref="System.Random"/> by default. /// <remarks><para>The distribution will use the <see cref="System.Random"/> by default.
@ -53,7 +53,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Dirichlet class. The distribution will /// Initializes a new instance of the Dirichlet class. The distribution will
@ -62,7 +62,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="alpha">An array with the Dirichlet parameters.</param> /// <param name="alpha">An array with the Dirichlet parameters.</param>
public Dirichlet(double[] alpha) public Dirichlet(double[] alpha)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(alpha); SetParameters(alpha);
} }
@ -72,9 +72,9 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="alpha">An array with the Dirichlet parameters.</param> /// <param name="alpha">An array with the Dirichlet parameters.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Dirichlet(double[] alpha, Random randomSource) public Dirichlet(double[] alpha, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(alpha); SetParameters(alpha);
} }
@ -92,7 +92,7 @@ namespace MathNet.Numerics.Distributions
parm[i] = alpha; parm[i] = alpha;
} }
_random = new Random(); _random = new System.Random();
SetParameters(parm); SetParameters(parm);
} }
@ -102,7 +102,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="alpha">The value of each parameter of the Dirichlet distribution.</param> /// <param name="alpha">The value of each parameter of the Dirichlet distribution.</param>
/// <param name="k">The dimension of the Dirichlet distribution.</param> /// <param name="k">The dimension of the Dirichlet distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Dirichlet(double alpha, int k, Random randomSource) public Dirichlet(double alpha, int k, System.Random randomSource)
{ {
// Create a parameter structure. // Create a parameter structure.
var parm = new double[k]; var parm = new double[k];
@ -111,7 +111,7 @@ namespace MathNet.Numerics.Distributions
parm[i] = alpha; parm[i] = alpha;
} }
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(parm); SetParameters(parm);
} }
@ -311,7 +311,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -340,7 +340,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="alpha">The Dirichlet distribution parameter.</param> /// <param name="alpha">The Dirichlet distribution parameter.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double[] Sample(Random rnd, double[] alpha) public static double[] Sample(System.Random rnd, double[] alpha)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha)) if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha))
{ {

89
src/Numerics/Distributions/Discrete/DiscreteUniform.cs → src/Numerics/Distributions/DiscreteUniform.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Discrete Univariate Uniform distribution.
/// The discrete uniform distribution is a distribution over integers. The distribution /// The discrete uniform distribution is a distribution over integers. The distribution
/// is parameterized by a lower and upper bound (both inclusive). /// is parameterized by a lower and upper bound (both inclusive).
/// <a href="http://en.wikipedia.org/wiki/Uniform_distribution_%28discrete%29">Wikipedia - Discrete uniform distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Uniform_distribution_%28discrete%29">Wikipedia - Discrete uniform distribution</a>.
@ -55,7 +60,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the DiscreteUniform class. /// Initializes a new instance of the DiscreteUniform class.
@ -64,7 +69,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="upper">Upper bound; must be at least as large as <paramref name="lower"/>.</param> /// <param name="upper">Upper bound; must be at least as large as <paramref name="lower"/>.</param>
public DiscreteUniform(int lower, int upper) public DiscreteUniform(int lower, int upper)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(lower, upper); SetParameters(lower, upper);
} }
@ -74,9 +79,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="lower">Lower bound.</param> /// <param name="lower">Lower bound.</param>
/// <param name="upper">Upper bound; must be at least as large as <paramref name="lower"/>.</param> /// <param name="upper">Upper bound; must be at least as large as <paramref name="lower"/>.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public DiscreteUniform(int lower, int upper, Random randomSource) public DiscreteUniform(int lower, int upper, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(lower, upper); SetParameters(lower, upper);
} }
@ -137,12 +142,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_lower, value); } set { SetParameters(_lower, value); }
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -212,30 +215,6 @@ namespace MathNet.Numerics.Distributions
get { return _upper; } get { return _upper; }
} }
/// <summary>
/// Computes the cumulative distribution function of the Bernoulli distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
if (x < _lower)
{
return 0.0;
}
if (x >= _upper)
{
return 1.0;
}
return Math.Min(1.0, (Math.Floor(x) - _lower + 1)/(_upper - _lower + 1));
}
#endregion
#region IDiscreteDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution; since every element in the domain has the same probability this method returns the middle one. /// Gets the mode of the distribution; since every element in the domain has the same probability this method returns the middle one.
/// </summary> /// </summary>
@ -253,12 +232,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the probability mass function. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
/// the probability mass at location <paramref name="k"/>.
/// </returns>
public double Probability(int k) public double Probability(int k)
{ {
if (k >= _lower && k <= _upper) if (k >= _lower && k <= _upper)
@ -270,12 +247,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the probability of a specific value. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
/// the log probability mass at location <paramref name="k"/>.
/// </returns>
public double ProbabilityLn(int k) public double ProbabilityLn(int k)
{ {
if (k >= _lower && k <= _upper) if (k >= _lower && k <= _upper)
@ -286,7 +261,25 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity; return Double.NegativeInfinity;
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
if (x < _lower)
{
return 0.0;
}
if (x >= _upper)
{
return 1.0;
}
return Math.Min(1.0, (Math.Floor(x) - _lower + 1)/(_upper - _lower + 1));
}
/// <summary> /// <summary>
/// Generates one sample from the discrete uniform distribution. This method does not do any parameter checking. /// Generates one sample from the discrete uniform distribution. This method does not do any parameter checking.
@ -295,7 +288,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="lower">The lower bound of the uniform random variable.</param> /// <param name="lower">The lower bound of the uniform random variable.</param>
/// <param name="upper">The upper 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> /// <returns>A random sample from the discrete uniform distribution.</returns>
internal static int SampleUnchecked(Random rnd, int lower, int upper) internal static int SampleUnchecked(System.Random rnd, int lower, int upper)
{ {
return (rnd.Next()%(upper - lower + 1)) + lower; return (rnd.Next()%(upper - lower + 1)) + lower;
} }
@ -328,7 +321,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="lower">The lower bound of the uniform random variable.</param> /// <param name="lower">The lower bound of the uniform random variable.</param>
/// <param name="upper">The upper bound of the uniform random variable.</param> /// <param name="upper">The upper bound of the uniform random variable.</param>
/// <returns>A sample from the discrete uniform distribution.</returns> /// <returns>A sample from the discrete uniform distribution.</returns>
public static int Sample(Random rnd, int lower, int upper) public static int Sample(System.Random rnd, int lower, int upper)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper))
{ {
@ -345,7 +338,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="lower">The lower bound of the uniform random variable.</param> /// <param name="lower">The lower bound of the uniform random variable.</param>
/// <param name="upper">The upper bound of the uniform random variable.</param> /// <param name="upper">The upper bound of the uniform random variable.</param>
/// <returns>a sequence of samples from the discrete uniform distribution.</returns> /// <returns>a sequence of samples from the discrete uniform distribution.</returns>
public static IEnumerable<int> Samples(Random rnd, int lower, int upper) public static IEnumerable<int> Samples(System.Random rnd, int lower, int upper)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lower, upper))
{ {

88
src/Numerics/Distributions/Continuous/Erlang.cs → src/Numerics/Distributions/Erlang.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,15 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// This class implements functionality for the Erlang distribution. This distribution is /// Continuous Univariate Erlang distribution.
/// is a continuous probability distribution with wide applicability primarily due to its /// This distribution is is a continuous probability distribution with wide applicability primarily due to its
/// relation to the exponential and Gamma distributions. /// relation to the exponential and Gamma distributions.
/// <a href="http://en.wikipedia.org/wiki/Erlang_distribution">Wikipedia - Erlang distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Erlang_distribution">Wikipedia - Erlang distribution</a>.
/// </summary> /// </summary>
@ -56,7 +60,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Erlang"/> class. /// Initializes a new instance of the <see cref="Erlang"/> class.
@ -65,7 +69,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="invScale">The inverse scale of the Erlang distribution.</param> /// <param name="invScale">The inverse scale of the Erlang distribution.</param>
public Erlang(int shape, double invScale) public Erlang(int shape, double invScale)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(shape, invScale); SetParameters(shape, invScale);
} }
@ -75,9 +79,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Erlang distribution.</param> /// <param name="shape">The shape of the Erlang distribution.</param>
/// <param name="invScale">The inverse scale of the Erlang distribution.</param> /// <param name="invScale">The inverse scale of the Erlang distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Erlang(int shape, double invScale, Random randomSource) public Erlang(int shape, double invScale, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(shape, invScale); SetParameters(shape, invScale);
} }
@ -178,12 +182,10 @@ namespace MathNet.Numerics.Distributions
return "Erlang(Shape = " + _shape + ", Inverse Scale = " + _invScale + ")"; return "Erlang(Shape = " + _shape + ", Inverse Scale = " + _invScale + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -302,30 +304,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
/// <summary>
/// Computes the cumulative distribution function of the Erlang distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
if (Double.IsPositiveInfinity(_invScale))
{
return x >= _shape ? 1.0 : 0.0;
}
if (_shape == 0.0 && _invScale == 0.0)
{
return 0.0;
}
return SpecialFunctions.GammaLowerRegularized(_shape, x*_invScale);
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -377,7 +355,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -402,10 +380,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
public double DensityLn(double x) public double DensityLn(double x)
{ {
if (Double.IsPositiveInfinity(_invScale)) if (Double.IsPositiveInfinity(_invScale))
@ -426,7 +404,25 @@ namespace MathNet.Numerics.Distributions
return (_shape*Math.Log(_invScale)) + ((_shape - 1.0)*Math.Log(x)) - (_invScale*x) - SpecialFunctions.GammaLn(_shape); return (_shape*Math.Log(_invScale)) + ((_shape - 1.0)*Math.Log(x)) - (_invScale*x) - SpecialFunctions.GammaLn(_shape);
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
if (Double.IsPositiveInfinity(_invScale))
{
return x >= _shape ? 1.0 : 0.0;
}
if (_shape == 0.0 && _invScale == 0.0)
{
return 0.0;
}
return SpecialFunctions.GammaLowerRegularized(_shape, x*_invScale);
}
/// <summary> /// <summary>
/// <para>Sampling implementation based on: /// <para>Sampling implementation based on:
@ -438,7 +434,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Gamma distribution.</param> /// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale 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> /// <returns>A sample from a Erlang distributed random variable.</returns>
internal static double SampleUnchecked(Random rnd, double shape, double invScale) internal static double SampleUnchecked(System.Random rnd, double shape, double invScale)
{ {
if (Double.IsPositiveInfinity(invScale)) if (Double.IsPositiveInfinity(invScale))
{ {
@ -510,7 +506,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Gamma distribution.</param> /// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param> /// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double shape, double invScale) public static double Sample(System.Random rnd, double shape, double invScale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale))
{ {
@ -527,7 +523,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Gamma distribution.</param> /// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param> /// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double shape, double invScale) public static IEnumerable<double> Samples(System.Random rnd, double shape, double invScale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale))
{ {

71
src/Numerics/Distributions/Continuous/Exponential.cs → src/Numerics/Distributions/Exponential.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Continuous Univariate Exponential distribution.
/// The exponential distribution is a distribution over the real numbers parameterized by one non-negative parameter. /// The exponential distribution is a distribution over the real numbers parameterized by one non-negative parameter.
/// <a href="http://en.wikipedia.org/wiki/exponential_distribution">Wikipedia - exponential distribution</a>. /// <a href="http://en.wikipedia.org/wiki/exponential_distribution">Wikipedia - exponential distribution</a>.
/// </summary> /// </summary>
@ -49,7 +54,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Exponential"/> class. /// Initializes a new instance of the <see cref="Exponential"/> class.
@ -57,7 +62,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param> /// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
public Exponential(double lambda) public Exponential(double lambda)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(lambda); SetParameters(lambda);
} }
@ -66,9 +71,9 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param> /// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Exponential(double lambda, Random randomSource) public Exponential(double lambda, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(lambda); SetParameters(lambda);
} }
@ -115,12 +120,10 @@ namespace MathNet.Numerics.Distributions
return "Exponential(Lambda = " + _lambda + ")"; return "Exponential(Lambda = " + _lambda + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -174,25 +177,6 @@ namespace MathNet.Numerics.Distributions
get { return 2.0; } get { return 2.0; }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
if (x >= 0.0)
{
return 1.0 - Math.Exp(-_lambda*x);
}
return 0.0;
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -226,7 +210,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -241,7 +225,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -250,7 +234,20 @@ namespace MathNet.Numerics.Distributions
return Math.Log(_lambda) - (_lambda*x); return Math.Log(_lambda) - (_lambda*x);
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
if (x >= 0.0)
{
return 1.0 - Math.Exp(-_lambda*x);
}
return 0.0;
}
/// <summary> /// <summary>
/// Samples the distribution. /// Samples the distribution.
@ -258,7 +255,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param> /// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, double lambda) internal static double SampleUnchecked(System.Random rnd, double lambda)
{ {
var r = rnd.NextDouble(); var r = rnd.NextDouble();
while (r == 0.0) while (r == 0.0)
@ -296,7 +293,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param> /// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <returns>A random number from this distribution.</returns> /// <returns>A random number from this distribution.</returns>
public static double Sample(Random rnd, double lambda) public static double Sample(System.Random rnd, double lambda)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{ {
@ -312,7 +309,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param> /// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double lambda) public static IEnumerable<double> Samples(System.Random rnd, double lambda)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{ {

63
src/Numerics/Distributions/Continuous/FisherSnedecor.cs → src/Numerics/Distributions/FisherSnedecor.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,14 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Implements the FisherSnedecor distribution. For details about this distribution, see /// Continuous Univariate FisherSnedecor distribution.
/// For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/F-distribution">Wikipedia - FisherSnedecor distribution</a>. /// <a href="http://en.wikipedia.org/wiki/F-distribution">Wikipedia - FisherSnedecor distribution</a>.
/// </summary> /// </summary>
/// <remarks><para>The distribution will use the <see cref="System.Random"/> by default. /// <remarks><para>The distribution will use the <see cref="System.Random"/> by default.
@ -54,7 +59,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FisherSnedecor"/> class. /// Initializes a new instance of the <see cref="FisherSnedecor"/> class.
@ -63,7 +68,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="d2">The second parameter - degree of freedom.</param> /// <param name="d2">The second parameter - degree of freedom.</param>
public FisherSnedecor(double d1, double d2) public FisherSnedecor(double d1, double d2)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(d1, d2); SetParameters(d1, d2);
} }
@ -73,9 +78,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="d1">The first parameter - degree of freedom.</param> /// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param> /// <param name="d2">The second parameter - degree of freedom.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public FisherSnedecor(double d1, double d2, Random randomSource) public FisherSnedecor(double d1, double d2, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(d1, d2); SetParameters(d1, d2);
} }
@ -133,12 +138,10 @@ namespace MathNet.Numerics.Distributions
return "FisherSnedecor(DegreeOfFreedom1 = " + _d1 + ", DegreeOfFreedom2 = " + _d2 + ")"; return "FisherSnedecor(DegreeOfFreedom1 = " + _d1 + ", DegreeOfFreedom2 = " + _d2 + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the distribution's random number generator. /// Gets or sets the distribution's random number generator.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -216,20 +219,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.BetaRegularized(_d1/2.0, _d2/2.0, _d1*x/((_d1*x) + _d2));
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -271,7 +260,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -281,7 +270,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -290,7 +279,15 @@ namespace MathNet.Numerics.Distributions
return Math.Log(Density(x)); return Math.Log(Density(x));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.BetaRegularized(_d1/2.0, _d2/2.0, _d1*x/((_d1*x) + _d2));
}
/// <summary> /// <summary>
/// Generates one sample from the <c>FisherSnedecor</c> distribution without parameter checking. /// Generates one sample from the <c>FisherSnedecor</c> distribution without parameter checking.
@ -299,7 +296,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="d1">The first parameter - degree of freedom.</param> /// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param> /// <param name="d2">The second parameter - degree of freedom.</param>
/// <returns>a <c>FisherSnedecor</c> distributed random number.</returns> /// <returns>a <c>FisherSnedecor</c> distributed random number.</returns>
internal static double SampleUnchecked(Random rnd, double d1, double d2) internal static double SampleUnchecked(System.Random rnd, double d1, double d2)
{ {
return (ChiSquare.Sample(rnd, d1)/d1)/(ChiSquare.Sample(rnd, d2)/d2); return (ChiSquare.Sample(rnd, d1)/d1)/(ChiSquare.Sample(rnd, d2)/d2);
} }
@ -332,7 +329,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="d1">The first parameter - degree of freedom.</param> /// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param> /// <param name="d2">The second parameter - degree of freedom.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double d1, double d2) public static double Sample(System.Random rnd, double d1, double d2)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(d1, d2)) if (Control.CheckDistributionParameters && !IsValidParameterSet(d1, d2))
{ {
@ -349,7 +346,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="d1">The first parameter - degree of freedom.</param> /// <param name="d1">The first parameter - degree of freedom.</param>
/// <param name="d2">The second parameter - degree of freedom.</param> /// <param name="d2">The second parameter - degree of freedom.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double d1, double d2) public static IEnumerable<double> Samples(System.Random rnd, double d1, double d2)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(d1, d2)) if (Control.CheckDistributionParameters && !IsValidParameterSet(d1, d2))
{ {

51
src/Numerics/Distributions/Continuous/Gamma.cs → src/Numerics/Distributions/Gamma.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,14 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Implements the univariate Gamma distribution. For details about this distribution, see /// Continuous Univariate Gamma distribution.
/// For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Gamma_distribution">Wikipedia - Gamma distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Gamma_distribution">Wikipedia - Gamma distribution</a>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
@ -62,7 +67,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Gamma class. /// Initializes a new instance of the Gamma class.
@ -71,7 +76,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="invScale">The inverse scale of the Gamma distribution.</param> /// <param name="invScale">The inverse scale of the Gamma distribution.</param>
public Gamma(double shape, double invScale) public Gamma(double shape, double invScale)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(shape, invScale); SetParameters(shape, invScale);
} }
@ -81,9 +86,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Gamma distribution.</param> /// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param> /// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Gamma(double shape, double invScale, Random randomSource) public Gamma(double shape, double invScale, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(shape, invScale); SetParameters(shape, invScale);
} }
@ -185,12 +190,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); } set { SetParameters(_shape, value); }
} }
#region IDistribution implementation
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -309,10 +312,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
#endregion
#region IContinuousDistribution implementation
/// <summary> /// <summary>
/// Gets the mode of the Gamma distribution. /// Gets the mode of the Gamma distribution.
/// </summary> /// </summary>
@ -359,7 +358,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the Gamma distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -384,7 +383,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the Gamma distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -409,10 +408,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the cumulative distribution function of the Gamma distribution. /// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
if (Double.IsPositiveInfinity(_invScale)) if (Double.IsPositiveInfinity(_invScale))
@ -428,8 +427,6 @@ namespace MathNet.Numerics.Distributions
return SpecialFunctions.GammaLowerRegularized(_shape, x*_invScale); return SpecialFunctions.GammaLowerRegularized(_shape, x*_invScale);
} }
#endregion
/// <summary> /// <summary>
/// <para>Sampling implementation based on: /// <para>Sampling implementation based on:
/// "A Simple Method for Generating Gamma Variables" - Marsaglia &amp; Tsang /// "A Simple Method for Generating Gamma Variables" - Marsaglia &amp; Tsang
@ -440,7 +437,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Gamma distribution.</param> /// <param name="shape">The shape of the Gamma distribution.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution.</param> /// <param name="invScale">The inverse scale of the Gamma distribution.</param>
/// <returns>A sample from a Gamma distributed random variable.</returns> /// <returns>A sample from a Gamma distributed random variable.</returns>
internal static double SampleUnchecked(Random rnd, double shape, double invScale) internal static double SampleUnchecked(System.Random rnd, double shape, double invScale)
{ {
if (Double.IsPositiveInfinity(invScale)) if (Double.IsPositiveInfinity(invScale))
{ {
@ -512,7 +509,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Gamma distribution from which to generate samples.</param> /// <param name="shape">The shape of the Gamma distribution from which to generate samples.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution from which to generate samples.</param> /// <param name="invScale">The inverse scale of the Gamma distribution from which to generate samples.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rng, double shape, double invScale) public static double Sample(System.Random rng, double shape, double invScale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale))
{ {
@ -529,7 +526,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Gamma distribution from which to generate samples.</param> /// <param name="shape">The shape of the Gamma distribution from which to generate samples.</param>
/// <param name="invScale">The inverse scale of the Gamma distribution from which to generate samples.</param> /// <param name="invScale">The inverse scale of the Gamma distribution from which to generate samples.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rng, double shape, double invScale) public static IEnumerable<double> Samples(System.Random rng, double shape, double invScale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, invScale))
{ {

69
src/Numerics/Distributions/Discrete/Geometric.cs → src/Numerics/Distributions/Geometric.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Discrete Univariate Geometric distribution.
/// The Geometric distribution is a distribution over positive integers parameterized by one positive real number. /// The Geometric distribution is a distribution over positive integers parameterized by one positive real number.
/// This implementation of the Geometric distribution will never generate 0's. /// This implementation of the Geometric distribution will never generate 0's.
/// <a href="http://en.wikipedia.org/wiki/geometric_distribution">Wikipedia - geometric distribution</a>. /// <a href="http://en.wikipedia.org/wiki/geometric_distribution">Wikipedia - geometric distribution</a>.
@ -50,7 +55,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Geometric class. /// Initializes a new instance of the Geometric class.
@ -59,7 +64,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If the Geometric parameter is not in the range [0,1].</exception> /// <exception cref="ArgumentOutOfRangeException">If the Geometric parameter is not in the range [0,1].</exception>
public Geometric(double p) public Geometric(double p)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(p); SetParameters(p);
} }
@ -69,9 +74,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="p">The probability of generating one.</param> /// <param name="p">The probability of generating one.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentOutOfRangeException">If the Geometric parameter is not in the range [0,1].</exception> /// <exception cref="ArgumentOutOfRangeException">If the Geometric parameter is not in the range [0,1].</exception>
public Geometric(double p, Random randomSource) public Geometric(double p, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(p); SetParameters(p);
} }
@ -121,12 +126,10 @@ namespace MathNet.Numerics.Distributions
return "Geometric(P = " + _p + ")"; return "Geometric(P = " + _p + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -181,20 +184,6 @@ namespace MathNet.Numerics.Distributions
get { return (2.0 - _p)/Math.Sqrt(1.0 - _p); } get { return (2.0 - _p)/Math.Sqrt(1.0 - _p); }
} }
/// <summary>
/// Computes the cumulative distribution function of the Bernoulli distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 1.0 - Math.Pow(1.0 - _p, x);
}
#endregion
#region IDiscreteDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -228,12 +217,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the probability mass function. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
/// the probability mass at location <paramref name="k"/>.
/// </returns>
public double Probability(int k) public double Probability(int k)
{ {
if (k <= 0) if (k <= 0)
@ -245,12 +232,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the log probability mass function. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
/// the log probability mass at location <paramref name="k"/>.
/// </returns>
public double ProbabilityLn(int k) public double ProbabilityLn(int k)
{ {
if (k <= 0) if (k <= 0)
@ -261,7 +246,15 @@ namespace MathNet.Numerics.Distributions
return ((k - 1)*Math.Log(1.0 - _p)) + Math.Log(_p); return ((k - 1)*Math.Log(1.0 - _p)) + Math.Log(_p);
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return 1.0 - Math.Pow(1.0 - _p, x);
}
/// <summary> /// <summary>
/// Returns one sample from the distribution. /// Returns one sample from the distribution.
@ -271,7 +264,7 @@ namespace MathNet.Numerics.Distributions
/// <returns> /// <returns>
/// One sample from the distribution implied by <paramref name="p"/>. /// One sample from the distribution implied by <paramref name="p"/>.
/// </returns> /// </returns>
internal static int SampleUnchecked(Random rnd, double p) internal 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)); return p == 1.0 ? 1 : (int) Math.Ceiling(-Math.Log(1.0 - rnd.NextDouble(), 1.0 - p));
} }
@ -302,7 +295,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="p">The p parameter</param> /// <param name="p">The p parameter</param>
public static int Sample(Random rnd, double p) public static int Sample(System.Random rnd, double p)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(p)) if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
{ {
@ -317,7 +310,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="p">The p parameter</param> /// <param name="p">The p parameter</param>
public static IEnumerable<int> Samples(Random rnd, double p) public static IEnumerable<int> Samples(System.Random rnd, double p)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(p)) if (Control.CheckDistributionParameters && !IsValidParameterSet(p))
{ {

43
src/Numerics/Distributions/Discrete/Hypergeometric.cs → src/Numerics/Distributions/Hypergeometric.cs

@ -28,16 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using MathNet.Numerics.Properties; using System;
using System.Collections.Generic; using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
/// <summary> /// <summary>
/// This class implements functionality for the Hypergeometric distribution. This distribution is /// Discrete Univariate Hypergeometric distribution.
/// a discrete probability distribution that describes the number of successes in a sequence /// This distribution is a discrete probability distribution that describes the number of successes in a sequence
/// of n draws from a finite population without replacement, just as the binomial distribution /// of n draws from a finite population without replacement, just as the binomial distribution
/// describes the number of successes for draws with replacement /// describes the number of successes for draws with replacement
/// <a href="http://en.wikipedia.org/wiki/Hypergeometric_distribution">Wikipedia - Hypergeometric distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Hypergeometric_distribution">Wikipedia - Hypergeometric distribution</a>.
@ -68,7 +67,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Hypergeometric class. /// Initializes a new instance of the Hypergeometric class.
@ -78,7 +77,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="draws">The number of draws without replacement (n).</param> /// <param name="draws">The number of draws without replacement (n).</param>
public Hypergeometric(int population, int success, int draws) public Hypergeometric(int population, int success, int draws)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(population, success, draws); SetParameters(population, success, draws);
} }
@ -89,9 +88,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="success">The number successes within the population (K, M).</param> /// <param name="success">The number successes within the population (K, M).</param>
/// <param name="draws">The number of draws without replacement (n).</param> /// <param name="draws">The number of draws without replacement (n).</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Hypergeometric(int population, int success, int draws, Random randomSource) public Hypergeometric(int population, int success, int draws, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(population, success, draws); SetParameters(population, success, draws);
} }
@ -196,7 +195,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -283,34 +282,30 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the probability mass function (PMF), i.e. P(X = x). /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
/// the probability mass at location <paramref name="k"/>.
/// </returns>
public double Probability(int k) public double Probability(int k)
{ {
return SpecialFunctions.Binomial(_success, k)*SpecialFunctions.Binomial(_population - _success, _draws - k)/SpecialFunctions.Binomial(_population, _draws); return SpecialFunctions.Binomial(_success, k)*SpecialFunctions.Binomial(_population - _success, _draws - k)/SpecialFunctions.Binomial(_population, _draws);
} }
/// <summary> /// <summary>
/// Computes values of the log probability mass function (lnPMF), i.e. ln(P(X = x)). /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
/// the log probability mass at location <paramref name="k"/>.
/// </returns>
public double ProbabilityLn(int k) public double ProbabilityLn(int k)
{ {
return Math.Log(Probability(k)); return Math.Log(Probability(k));
} }
/// <summary> /// <summary>
/// Computes the cumulative distribution function (CDF) of the distribution, i.e. P(X &lt;= x). /// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
if (x < Minimum) if (x < Minimum)
@ -340,7 +335,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="success">The number successes within the population (K, M).</param> /// <param name="success">The number successes within the population (K, M).</param>
/// <param name="draws">The n parameter of the distribution.</param> /// <param name="draws">The n parameter of the distribution.</param>
/// <returns>a random number from the Hypergeometric distribution.</returns> /// <returns>a random number from the Hypergeometric distribution.</returns>
internal static int SampleUnchecked(Random rnd, int population, int success, int draws) internal static int SampleUnchecked(System.Random rnd, int population, int success, int draws)
{ {
var x = 0; var x = 0;
@ -389,7 +384,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="population">The size of the population (N).</param> /// <param name="population">The size of the population (N).</param>
/// <param name="success">The number successes within the population (K, M).</param> /// <param name="success">The number successes within the population (K, M).</param>
/// <param name="draws">The number of draws without replacement (n).</param> /// <param name="draws">The number of draws without replacement (n).</param>
public static int Sample(Random rnd, int population, int success, int draws) public static int Sample(System.Random rnd, int population, int success, int draws)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(population, success, draws)) if (Control.CheckDistributionParameters && !IsValidParameterSet(population, success, draws))
{ {
@ -406,7 +401,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="population">The size of the population (N).</param> /// <param name="population">The size of the population (N).</param>
/// <param name="success">The number successes within the population (K, M).</param> /// <param name="success">The number successes within the population (K, M).</param>
/// <param name="draws">The number of draws without replacement (n).</param> /// <param name="draws">The number of draws without replacement (n).</param>
public static IEnumerable<int> Samples(Random rnd, int population, int success, int draws) public static IEnumerable<int> Samples(System.Random rnd, int population, int success, int draws)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(population, success, draws)) if (Control.CheckDistributionParameters && !IsValidParameterSet(population, success, draws))
{ {

6
src/Numerics/Distributions/IContinuousDistribution.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -58,14 +58,14 @@ namespace MathNet.Numerics.Distributions
double Maximum { get; } double Maximum { get; }
/// <summary> /// <summary>
/// The probability density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
double Density(double x); double Density(double x);
/// <summary> /// <summary>
/// The log probability density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>

6
src/Numerics/Distributions/IDiscreteDistribution.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -58,14 +58,14 @@ namespace MathNet.Numerics.Distributions
int Maximum { get; } int Maximum { get; }
/// <summary> /// <summary>
/// Computes values of the probability mass function. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <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> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
double Probability(int k); double Probability(int k);
/// <summary> /// <summary>
/// Computes values of the log probability mass function. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <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> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>

4
src/Numerics/Distributions/IDistribution.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -68,7 +68,7 @@ namespace MathNet.Numerics.Distributions
double Skewness { get; } double Skewness { get; }
/// <summary> /// <summary>
/// Computes the cumulative distribution function (cdf) for this probability distribution. /// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative distribution function.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative distribution at location <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>

61
src/Numerics/Distributions/Continuous/InverseGamma.cs → src/Numerics/Distributions/InverseGamma.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Continuous Univariate Inverse Gamma distribution.
/// The inverse Gamma distribution is a distribution over the positive real numbers parameterized by /// The inverse Gamma distribution is a distribution over the positive real numbers parameterized by
/// two positive parameters. /// two positive parameters.
/// <a href="http://en.wikipedia.org/wiki/inverse-gamma_distribution">Wikipedia - InverseGamma distribution</a>. /// <a href="http://en.wikipedia.org/wiki/inverse-gamma_distribution">Wikipedia - InverseGamma distribution</a>.
@ -55,7 +60,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="InverseGamma"/> class. /// Initializes a new instance of the <see cref="InverseGamma"/> class.
@ -64,7 +69,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param> /// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
public InverseGamma(double shape, double scale) public InverseGamma(double shape, double scale)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(shape, scale); SetParameters(shape, scale);
} }
@ -74,9 +79,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param> /// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param> /// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public InverseGamma(double shape, double scale, Random randomSource) public InverseGamma(double shape, double scale, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(shape, scale); SetParameters(shape, scale);
} }
@ -139,12 +144,10 @@ namespace MathNet.Numerics.Distributions
return "InverseGamma(Shape = " + _shape + ", Inverse Scale = " + _scale + ")"; return "InverseGamma(Shape = " + _shape + ", Inverse Scale = " + _scale + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -222,20 +225,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaUpperRegularized(_shape, _scale/x);
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -270,7 +259,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -285,7 +274,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -294,7 +283,15 @@ namespace MathNet.Numerics.Distributions
return Math.Log(Density(x)); return Math.Log(Density(x));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaUpperRegularized(_shape, _scale/x);
}
/// <summary> /// <summary>
/// Samples the distribution. /// Samples the distribution.
@ -303,7 +300,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param> /// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param> /// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, double shape, double scale) internal static double SampleUnchecked(System.Random rnd, double shape, double scale)
{ {
return 1.0/Gamma.Sample(rnd, shape, scale); return 1.0/Gamma.Sample(rnd, shape, scale);
} }
@ -336,7 +333,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param> /// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param> /// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double shape, double scale) public static double Sample(System.Random rnd, double shape, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale))
{ {
@ -353,7 +350,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param> /// <param name="shape">The shape (alpha) parameter of the inverse Gamma distribution.</param>
/// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param> /// <param name="scale">The scale (beta) parameter of the inverse Gamma distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double shape, double scale) public static IEnumerable<double> Samples(System.Random rnd, double shape, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale))
{ {

31
src/Numerics/Distributions/Multivariate/InverseWishart.cs → src/Numerics/Distributions/InverseWishart.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,16 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
/// <summary> /// <summary>
/// This class implements functionality for the inverse Wishart distribution. This distribution is /// Multivariate Inverse Wishart distribution. This distribution is
/// parameterized by the degrees of freedom nu and the scale matrix S. The inverse Wishart distribution /// parameterized by the degrees of freedom nu and the scale matrix S. The inverse Wishart distribution
/// is the conjugate prior for the covariance matrix of a multivariate normal distribution. /// is the conjugate prior for the covariance matrix of a multivariate normal distribution.
/// <a href="http://en.wikipedia.org/wiki/Inverse-Wishart_distribution">Wikipedia - Inverse-Wishart distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Inverse-Wishart_distribution">Wikipedia - Inverse-Wishart distribution</a>.
@ -63,7 +66,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="InverseWishart"/> class. /// Initializes a new instance of the <see cref="InverseWishart"/> class.
@ -72,7 +75,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="s">The scale matrix for the inverse Wishart distribution.</param> /// <param name="s">The scale matrix for the inverse Wishart distribution.</param>
public InverseWishart(double nu, Matrix<double> s) public InverseWishart(double nu, Matrix<double> s)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(nu, s); SetParameters(nu, s);
} }
@ -82,9 +85,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The degrees of freedom for the inverse Wishart distribution.</param> /// <param name="nu">The degrees of freedom for the inverse Wishart distribution.</param>
/// <param name="s">The scale matrix for the inverse Wishart distribution.</param> /// <param name="s">The scale matrix for the inverse Wishart distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public InverseWishart(double nu, Matrix<double> s, Random randomSource) public InverseWishart(double nu, Matrix<double> s, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(nu, s); SetParameters(nu, s);
} }
@ -160,7 +163,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -244,10 +247,10 @@ namespace MathNet.Numerics.Distributions
} }
return Math.Pow(dX, -(_nu + p + 1.0)/2.0) return Math.Pow(dX, -(_nu + p + 1.0)/2.0)
*Math.Exp(-0.5*sXi.Trace()) *Math.Exp(-0.5*sXi.Trace())
*Math.Pow(_chol.Determinant, _nu/2.0) *Math.Pow(_chol.Determinant, _nu/2.0)
/Math.Pow(2.0, _nu*p/2.0) /Math.Pow(2.0, _nu*p/2.0)
/gp; /gp;
} }
/// <summary> /// <summary>
@ -268,7 +271,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The degrees of freedom.</param> /// <param name="nu">The degrees of freedom.</param>
/// <param name="s">The scale matrix.</param> /// <param name="s">The scale matrix.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static Matrix<double> Sample(Random rnd, double nu, Matrix<double> s) public static Matrix<double> Sample(System.Random rnd, double nu, Matrix<double> s)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s)) if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
{ {

63
src/Numerics/Distributions/Continuous/Laplace.cs → src/Numerics/Distributions/Laplace.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Continuous Univariate Laplace distribution.
/// The Laplace distribution is a distribution over the real numbers parameterized by a mean and /// The Laplace distribution is a distribution over the real numbers parameterized by a mean and
/// scale parameter. The PDF is: /// scale parameter. The PDF is:
/// p(x) = \frac{1}{2 * scale} \exp{- |x - mean| / scale}. /// p(x) = \frac{1}{2 * scale} \exp{- |x - mean| / scale}.
@ -51,7 +56,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Gets or sets the location of the Laplace distribution. /// Gets or sets the location of the Laplace distribution.
@ -87,7 +92,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception> /// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Laplace(double location, double scale) public Laplace(double location, double scale)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(location, scale); SetParameters(location, scale);
} }
@ -98,9 +103,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale for the Laplace distribution.</param> /// <param name="scale">The scale for the Laplace distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception> /// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Laplace(double location, double scale, Random randomSource) public Laplace(double location, double scale, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(location, scale); SetParameters(location, scale);
} }
@ -141,12 +146,10 @@ namespace MathNet.Numerics.Distributions
return "Laplace(Location = " + Mean + ", Scale = " + _scale + ")"; return "Laplace(Location = " + Mean + ", Scale = " + _scale + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -197,20 +200,6 @@ namespace MathNet.Numerics.Distributions
get { return 0.0; } get { return 0.0; }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 0.5*(1.0 + (Math.Sign(x - Mean)*(1.0 - Math.Exp(-Math.Abs(x - Mean)/_scale))));
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -244,17 +233,17 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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) public double Density(double x)
{ {
return Math.Exp(-Math.Abs(x - Mean)/_scale)/(2.0*_scale); return Math.Exp(-Math.Abs(x - Mean)/_scale)/(2.0*_scale);
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -263,7 +252,15 @@ namespace MathNet.Numerics.Distributions
return Math.Log(Density(x)); return Math.Log(Density(x));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return 0.5*(1.0 + (Math.Sign(x - Mean)*(1.0 - Math.Exp(-Math.Abs(x - Mean)/_scale))));
}
/// <summary> /// <summary>
/// Samples the distribution. /// Samples the distribution.
@ -272,7 +269,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="location">The location shape parameter.</param> /// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, double location, double scale) internal static double SampleUnchecked(System.Random rnd, double location, double scale)
{ {
var u = rnd.NextDouble() - 0.5; var u = rnd.NextDouble() - 0.5;
return location - (scale*Math.Sign(u)*Math.Log(1.0 - (2.0*Math.Abs(u)))); return location - (scale*Math.Sign(u)*Math.Log(1.0 - (2.0*Math.Abs(u))));
@ -306,7 +303,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="location">The location shape parameter.</param> /// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double location, double scale) public static double Sample(System.Random rnd, double location, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
{ {
@ -323,7 +320,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="location">The location shape parameter.</param> /// <param name="location">The location shape parameter.</param>
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double location, double scale) public static IEnumerable<double> Samples(System.Random rnd, double location, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale))
{ {

50
src/Numerics/Distributions/Continuous/LogNormal.cs → src/Numerics/Distributions/LogNormal.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,17 +28,17 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Statistics; using MathNet.Numerics.Statistics;
using System.Collections.Generic;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
/// <summary> /// <summary>
/// Implements the univariate Log-Normal distribution. For details about this distribution, see /// Continuous Univariate Log-Normal distribution.
/// For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Log-normal_distribution">Wikipedia - Log-Normal distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Log-normal_distribution">Wikipedia - Log-Normal distribution</a>.
/// </summary> /// </summary>
/// <remarks><para>The distribution will use the <see cref="System.Random"/> by default. /// <remarks><para>The distribution will use the <see cref="System.Random"/> by default.
@ -57,7 +61,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="LogNormal"/> class. /// Initializes a new instance of the <see cref="LogNormal"/> class.
@ -68,7 +72,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param> /// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
public LogNormal(double mu, double sigma) public LogNormal(double mu, double sigma)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(mu, sigma); SetParameters(mu, sigma);
} }
@ -80,9 +84,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="mu">The mu of the logarithm of the distribution.</param> /// <param name="mu">The mu of the logarithm of the distribution.</param>
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param> /// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public LogNormal(double mu, double sigma, Random randomSource) public LogNormal(double mu, double sigma, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(mu, sigma); SetParameters(mu, sigma);
} }
@ -95,8 +99,8 @@ namespace MathNet.Numerics.Distributions
/// <returns>a log-normal distribution.</returns> /// <returns>a log-normal distribution.</returns>
public static LogNormal WithMeanVariance(double mean, double var) public static LogNormal WithMeanVariance(double mean, double var)
{ {
var sigma2 = Math.Log(var / (mean * mean) + 1.0); var sigma2 = Math.Log(var/(mean*mean) + 1.0);
return new LogNormal(Math.Log(mean) - sigma2 / 2.0, Math.Sqrt(sigma2)); return new LogNormal(Math.Log(mean) - sigma2/2.0, Math.Sqrt(sigma2));
} }
/// <summary> /// <summary>
@ -163,12 +167,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_mu, value); } set { SetParameters(_mu, value); }
} }
#region IDistribution implementation
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -234,10 +236,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
#endregion
#region IContinuousDistribution implementation
/// <summary> /// <summary>
/// Gets the mode of the log-normal distribution. /// Gets the mode of the log-normal distribution.
/// </summary> /// </summary>
@ -271,7 +269,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the log-normal distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -287,7 +285,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the log-normal distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -303,10 +301,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the cumulative distribution function of the log-normal distribution. /// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
if (x < 0.0) if (x < 0.0)
@ -317,8 +315,6 @@ namespace MathNet.Numerics.Distributions
return 0.5*(1.0 + SpecialFunctions.Erf((Math.Log(x) - _mu)/(_sigma*Constants.Sqrt2))); return 0.5*(1.0 + SpecialFunctions.Erf((Math.Log(x) - _mu)/(_sigma*Constants.Sqrt2)));
} }
#endregion
/// <summary> /// <summary>
/// Generates a sample from the log-normal distribution using the <i>Box-Muller</i> algorithm. /// Generates a sample from the log-normal distribution using the <i>Box-Muller</i> algorithm.
/// </summary> /// </summary>
@ -349,7 +345,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="mu">The mu of the logarithm of the distribution.</param> /// <param name="mu">The mu of the logarithm of the distribution.</param>
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param> /// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rng, double mu, double sigma) public static double Sample(System.Random rng, double mu, double sigma)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(mu, sigma)) if (Control.CheckDistributionParameters && !IsValidParameterSet(mu, sigma))
{ {
@ -366,7 +362,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="mu">The mu of the logarithm of the distribution.</param> /// <param name="mu">The mu of the logarithm of the distribution.</param>
/// <param name="sigma">The standard deviation of the logarithm of the distribution.</param> /// <param name="sigma">The standard deviation of the logarithm of the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rng, double mu, double sigma) public static IEnumerable<double> Samples(System.Random rng, double mu, double sigma)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(mu, sigma)) if (Control.CheckDistributionParameters && !IsValidParameterSet(mu, sigma))
{ {

33
src/Numerics/Distributions/Multivariate/MatrixNormal.cs → src/Numerics/Distributions/MatrixNormal.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,6 +28,7 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
@ -31,10 +36,8 @@ using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
/// <summary> /// <summary>
/// This class implements functionality for matrix valued normal distributions. The distribution /// Multivariate Matrix-valued Normal distributions. The distribution
/// is parameterized by a mean matrix (M), a covariance matrix for the rows (V) and a covariance matrix /// is parameterized by a mean matrix (M), a covariance matrix for the rows (V) and a covariance matrix
/// for the columns (K). If the dimension of M is d-by-m then V is d-by-d and K is m-by-m. /// for the columns (K). If the dimension of M is d-by-m then V is d-by-d and K is m-by-m.
/// <a href="http://en.wikipedia.org/wiki/Matrix_normal_distribution">Wikipedia - MatrixNormal distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Matrix_normal_distribution">Wikipedia - MatrixNormal distribution</a>.
@ -64,7 +67,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="MatrixNormal"/> class. /// Initializes a new instance of the <see cref="MatrixNormal"/> class.
@ -75,7 +78,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If the dimensions of the mean and two covariance matrices don't match.</exception> /// <exception cref="ArgumentOutOfRangeException">If the dimensions of the mean and two covariance matrices don't match.</exception>
public MatrixNormal(Matrix<double> m, Matrix<double> v, Matrix<double> k) public MatrixNormal(Matrix<double> m, Matrix<double> v, Matrix<double> k)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(m, v, k); SetParameters(m, v, k);
} }
@ -87,9 +90,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="k">The covariance matrix for the columns.</param> /// <param name="k">The covariance matrix for the columns.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentOutOfRangeException">If the dimensions of the mean and two covariance matrices don't match.</exception> /// <exception cref="ArgumentOutOfRangeException">If the dimensions of the mean and two covariance matrices don't match.</exception>
public MatrixNormal(Matrix<double> m, Matrix<double> v, Matrix<double> k, Random randomSource) public MatrixNormal(Matrix<double> m, Matrix<double> v, Matrix<double> k, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(m, v, k); SetParameters(m, v, k);
} }
@ -196,7 +199,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -228,9 +231,9 @@ namespace MathNet.Numerics.Distributions
var cholK = Cholesky<double>.Create(_k); var cholK = Cholesky<double>.Create(_k);
return Math.Exp(-0.5*cholV.Solve(a.Transpose()*cholK.Solve(a)).Trace()) return Math.Exp(-0.5*cholV.Solve(a.Transpose()*cholK.Solve(a)).Trace())
/Math.Pow(2.0*Constants.Pi, x.RowCount*x.ColumnCount/2.0) /Math.Pow(2.0*Constants.Pi, x.RowCount*x.ColumnCount/2.0)
/Math.Pow(cholV.Determinant, x.RowCount/2.0) /Math.Pow(cholV.Determinant, x.RowCount/2.0)
/Math.Pow(cholK.Determinant, x.ColumnCount/2.0); /Math.Pow(cholK.Determinant, x.ColumnCount/2.0);
} }
/// <summary> /// <summary>
@ -251,7 +254,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="k">The covariance matrix for the columns.</param> /// <param name="k">The covariance matrix for the columns.</param>
/// <exception cref="ArgumentOutOfRangeException">If the dimensions of the mean and two covariance matrices don't match.</exception> /// <exception cref="ArgumentOutOfRangeException">If the dimensions of the mean and two covariance matrices don't match.</exception>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static Matrix<double> Sample(Random rnd, Matrix<double> m, Matrix<double> v, Matrix<double> k) public static Matrix<double> Sample(System.Random rnd, Matrix<double> m, Matrix<double> v, Matrix<double> k)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k)) if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k))
{ {
@ -287,7 +290,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="mean">The mean of the vector normal distribution.</param> /// <param name="mean">The mean of the vector normal distribution.</param>
/// <param name="covariance">The covariance matrix of the vector normal distribution.</param> /// <param name="covariance">The covariance matrix of the vector normal distribution.</param>
/// <returns>a sequence of samples from defined distribution.</returns> /// <returns>a sequence of samples from defined distribution.</returns>
static Vector<double> SampleVectorNormal(Random rnd, Vector<double> mean, Matrix<double> covariance) static Vector<double> SampleVectorNormal(System.Random rnd, Vector<double> mean, Matrix<double> covariance)
{ {
var chol = Cholesky<double>.Create(covariance); var chol = Cholesky<double>.Create(covariance);
return SampleVectorNormal(rnd, mean, chol); return SampleVectorNormal(rnd, mean, chol);
@ -300,7 +303,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="mean">The mean of the vector normal distribution.</param> /// <param name="mean">The mean of the vector normal distribution.</param>
/// <param name="cholesky">The Cholesky factorization of the covariance matrix.</param> /// <param name="cholesky">The Cholesky factorization of the covariance matrix.</param>
/// <returns>a sequence of samples from defined distribution.</returns> /// <returns>a sequence of samples from defined distribution.</returns>
static Vector<double> SampleVectorNormal(Random rnd, Vector<double> mean, Cholesky<double> cholesky) static Vector<double> SampleVectorNormal(System.Random rnd, Vector<double> mean, Cholesky<double> cholesky)
{ {
var count = mean.Count; var count = mean.Count;

40
src/Numerics/Distributions/Multivariate/Multinomial.cs → src/Numerics/Distributions/Multinomial.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,18 +28,18 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.Properties;
using MathNet.Numerics.Statistics;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using LinearAlgebra;
using LinearAlgebra.Double;
using Properties;
using Statistics;
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary> /// <summary>
/// Implements the multinomial distribution. For details about this distribution, see /// Multivariate Multinomial distribution. For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Multinomial_distribution">Wikipedia - Multinomial distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Multinomial_distribution">Wikipedia - Multinomial distribution</a>.
/// </summary> /// </summary>
/// <remarks><para>The distribution is parameterized by a vector of ratios: in other words, the parameter /// <remarks><para>The distribution is parameterized by a vector of ratios: in other words, the parameter
@ -61,7 +65,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Multinomial class. /// Initializes a new instance of the Multinomial class.
@ -73,7 +77,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
public Multinomial(double[] p, int n) public Multinomial(double[] p, int n)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(p, n); SetParameters(p, n);
} }
@ -86,9 +90,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentOutOfRangeException">If any of the probabilities are negative or do not sum to one.</exception> /// <exception cref="ArgumentOutOfRangeException">If any of the probabilities are negative or do not sum to one.</exception>
/// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception>
public Multinomial(double[] p, int n, Random randomSource) public Multinomial(double[] p, int n, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(p, n); SetParameters(p, n);
} }
@ -117,7 +121,7 @@ namespace MathNet.Numerics.Distributions
} }
SetParameters(p, n); SetParameters(p, n);
RandomSource = new Random(); RandomSource = new System.Random();
} }
/// <summary> /// <summary>
@ -197,7 +201,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -347,7 +351,7 @@ namespace MathNet.Numerics.Distributions
/// as this is often impossible using floating point arithmetic.</param> /// as this is often impossible using floating point arithmetic.</param>
/// <param name="n">The number of trials.</param> /// <param name="n">The number of trials.</param>
/// <returns>the counts for each of the different possible values.</returns> /// <returns>the counts for each of the different possible values.</returns>
public static int[] Sample(Random rnd, double[] p, int n) public static int[] Sample(System.Random rnd, double[] p, int n)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n)) if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
{ {
@ -376,7 +380,7 @@ namespace MathNet.Numerics.Distributions
/// as this is often impossible using floating point arithmetic.</param> /// as this is often impossible using floating point arithmetic.</param>
/// <param name="n">The number of variables needed.</param> /// <param name="n">The number of variables needed.</param>
/// <returns>a sequence of counts for each of the different possible values.</returns> /// <returns>a sequence of counts for each of the different possible values.</returns>
public static IEnumerable<int[]> Samples(Random rnd, double[] p, int n) public static IEnumerable<int[]> Samples(System.Random rnd, double[] p, int n)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n)) if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n))
{ {

85
src/Numerics/Distributions/Discrete/NegativeBinomial.cs → src/Numerics/Distributions/NegativeBinomial.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Discrete Univariate Negative Binomial distribution.
/// The negative binomial is a distribution over the natural numbers with two parameters r,p. For the special /// The negative binomial is a distribution over the natural numbers with two parameters r,p. For the special
/// case that r is an integer one can interpret the distribution as the number of tails before the r'th head /// case that r is an integer one can interpret the distribution as the number of tails before the r'th head
/// when the probability of head is p. /// when the probability of head is p.
@ -56,7 +61,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Gets or sets the number of trials. /// Gets or sets the number of trials.
@ -83,7 +88,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="p">The probability of a trial resulting in success.</param> /// <param name="p">The probability of a trial resulting in success.</param>
public NegativeBinomial(double r, double p) public NegativeBinomial(double r, double p)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(r, p); SetParameters(r, p);
} }
@ -93,9 +98,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="r">The number of trials.</param> /// <param name="r">The number of trials.</param>
/// <param name="p">The probability of a trial resulting in success.</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> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public NegativeBinomial(double r, double p, Random randomSource) public NegativeBinomial(double r, double p, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(r, p); SetParameters(r, p);
} }
@ -138,12 +143,10 @@ namespace MathNet.Numerics.Distributions
return "NegativeBinomial(R = " + _r + ", P = " + _p + ")"; return "NegativeBinomial(R = " + _r + ", P = " + _p + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the distribution's random number generator. /// Gets or sets the distribution's random number generator.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -197,20 +200,6 @@ namespace MathNet.Numerics.Distributions
get { return (2.0 - _p)/Math.Sqrt(_r*(1.0 - _p)); } get { return (2.0 - _p)/Math.Sqrt(_r*(1.0 - _p)); }
} }
/// <summary>
/// Computes the cumulative distribution function of the <c>NegativeBinomial</c> distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 1 - SpecialFunctions.BetaRegularized(x + 1, _r, 1 - _p);
}
#endregion
#region IDiscreteDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution /// Gets the mode of the distribution
/// </summary> /// </summary>
@ -244,40 +233,44 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the probability mass function. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
/// the probability mass at location <paramref name="k"/>.
/// </returns>
public double Probability(int k) public double Probability(int k)
{ {
var ln = SpecialFunctions.GammaLn(_r + k) var ln = SpecialFunctions.GammaLn(_r + k)
- SpecialFunctions.GammaLn(_r) - SpecialFunctions.GammaLn(_r)
- SpecialFunctions.GammaLn(k + 1.0) - SpecialFunctions.GammaLn(k + 1.0)
+ (_r*Math.Log(_p)) + (_r*Math.Log(_p))
+ (k*Math.Log(1.0 - _p)); + (k*Math.Log(1.0 - _p));
return Math.Exp(ln); return Math.Exp(ln);
} }
/// <summary> /// <summary>
/// Computes values of the log probability mass function. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
/// the log probability mass at location <paramref name="k"/>.
/// </returns>
public double ProbabilityLn(int k) public double ProbabilityLn(int k)
{ {
var ln = SpecialFunctions.GammaLn(_r + k) var ln = SpecialFunctions.GammaLn(_r + k)
- SpecialFunctions.GammaLn(_r) - SpecialFunctions.GammaLn(_r)
- SpecialFunctions.GammaLn(k + 1.0) - SpecialFunctions.GammaLn(k + 1.0)
+ (_r*Math.Log(_p)) + (_r*Math.Log(_p))
+ (k*Math.Log(1.0 - _p)); + (k*Math.Log(1.0 - _p));
return ln; return ln;
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return 1 - SpecialFunctions.BetaRegularized(x + 1, _r, 1 - _p);
}
/// <summary> /// <summary>
/// Samples a negative binomial distributed random variable. /// Samples a negative binomial distributed random variable.
@ -286,7 +279,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="r">The r parameter.</param> /// <param name="r">The r parameter.</param>
/// <param name="p">The p parameter.</param> /// <param name="p">The p parameter.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
internal static int SampleUnchecked(Random rnd, double r, double p) internal static int SampleUnchecked(System.Random rnd, double r, double p)
{ {
var lambda = Gamma.SampleUnchecked(rnd, r, p); var lambda = Gamma.SampleUnchecked(rnd, r, p);
var c = Math.Exp(-lambda); var c = Math.Exp(-lambda);
@ -327,7 +320,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="r">The r parameter.</param> /// <param name="r">The r parameter.</param>
/// <param name="p">The p parameter.</param> /// <param name="p">The p parameter.</param>
public static int Sample(Random rnd, double r, double p) public static int Sample(System.Random rnd, double r, double p)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p)) if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p))
{ {
@ -343,7 +336,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="r">The r parameter.</param> /// <param name="r">The r parameter.</param>
/// <param name="p">The p parameter.</param> /// <param name="p">The p parameter.</param>
public static IEnumerable<int> Samples(Random rnd, double r, double p) public static IEnumerable<int> Samples(System.Random rnd, double r, double p)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p)) if (Control.CheckDistributionParameters && !IsValidParameterSet(r, p))
{ {

44
src/Numerics/Distributions/Continuous/Normal.cs → src/Numerics/Distributions/Normal.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,16 +28,16 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Statistics; using MathNet.Numerics.Statistics;
using System.Collections.Generic;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
/// <summary> /// <summary>
/// Implements the univariate Normal (or Gaussian) distribution. For details about this distribution, see /// Continuous Univariate Normal distribution, also known as Gaussian distribution.
/// For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Normal_distribution">Wikipedia - Normal distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Normal_distribution">Wikipedia - Normal distribution</a>.
/// </summary> /// </summary>
/// <remarks><para>The distribution will use the <see cref="System.Random"/> by default. /// <remarks><para>The distribution will use the <see cref="System.Random"/> by default.
@ -56,7 +60,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Normal class. This is a normal distribution with mean 0.0 /// Initializes a new instance of the Normal class. This is a normal distribution with mean 0.0
@ -74,7 +78,7 @@ namespace MathNet.Numerics.Distributions
/// be initialized with the default <seealso cref="System.Random"/> random number generator. /// be initialized with the default <seealso cref="System.Random"/> random number generator.
/// </summary> /// </summary>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Normal(Random randomSource) public Normal(System.Random randomSource)
: this(0.0, 1.0, randomSource) : this(0.0, 1.0, randomSource)
{ {
} }
@ -87,7 +91,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="stddev">The standard deviation of the normal distribution.</param> /// <param name="stddev">The standard deviation of the normal distribution.</param>
public Normal(double mean, double stddev) public Normal(double mean, double stddev)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(mean, stddev); SetParameters(mean, stddev);
} }
@ -98,9 +102,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="mean">The mean of the normal distribution.</param> /// <param name="mean">The mean of the normal distribution.</param>
/// <param name="stddev">The standard deviation of the normal distribution.</param> /// <param name="stddev">The standard deviation of the normal distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Normal(double mean, double stddev, Random randomSource) public Normal(double mean, double stddev, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(mean, stddev); SetParameters(mean, stddev);
} }
@ -210,7 +214,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -326,7 +330,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the normal distribution (PDF), i.e. dP(X &lt;= x)/dx. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -336,7 +340,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the normal distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx). /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -358,10 +362,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the cumulative distribution function (CDF) of the normal distribution, i.e. P(X &lt;= x). /// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
return CumulativeDistribution(_mean, _stdDev, x); return CumulativeDistribution(_mean, _stdDev, x);
@ -382,7 +386,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <returns>a pair of random numbers from the standard normal distribution.</returns> /// <returns>a pair of random numbers from the standard normal distribution.</returns>
internal static Tuple<double, double> SampleUncheckedBoxMuller(Random rnd) internal static Tuple<double, double> SampleUncheckedBoxMuller(System.Random rnd)
{ {
var v1 = (2.0*rnd.NextDouble()) - 1.0; var v1 = (2.0*rnd.NextDouble()) - 1.0;
var v2 = (2.0*rnd.NextDouble()) - 1.0; var v2 = (2.0*rnd.NextDouble()) - 1.0;
@ -405,7 +409,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="mean">The mean of the normal distribution from which to generate samples.</param> /// <param name="mean">The mean of the normal distribution from which to generate samples.</param>
/// <param name="stddev">The standard deviation of the normal distribution from which to generate samples.</param> /// <param name="stddev">The standard deviation of the normal distribution from which to generate samples.</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, double mean, double stddev) internal static double SampleUnchecked(System.Random rnd, double mean, double stddev)
{ {
return mean + (stddev*SampleUncheckedBoxMuller(rnd).Item1); return mean + (stddev*SampleUncheckedBoxMuller(rnd).Item1);
} }
@ -440,7 +444,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="mean">The mean of the normal distribution from which to generate samples.</param> /// <param name="mean">The mean of the normal distribution from which to generate samples.</param>
/// <param name="stddev">The standard deviation of the normal distribution from which to generate samples.</param> /// <param name="stddev">The standard deviation of the normal distribution from which to generate samples.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double mean, double stddev) public static double Sample(System.Random rnd, double mean, double stddev)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(mean, stddev)) if (Control.CheckDistributionParameters && !IsValidParameterSet(mean, stddev))
{ {
@ -457,7 +461,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="mean">The mean of the normal distribution from which to generate samples.</param> /// <param name="mean">The mean of the normal distribution from which to generate samples.</param>
/// <param name="stddev">The standard deviation of the normal distribution from which to generate samples.</param> /// <param name="stddev">The standard deviation of the normal distribution from which to generate samples.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double mean, double stddev) public static IEnumerable<double> Samples(System.Random rnd, double mean, double stddev)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(mean, stddev)) if (Control.CheckDistributionParameters && !IsValidParameterSet(mean, stddev))
{ {

33
src/Numerics/Distributions/Multivariate/NormalGamma.cs → src/Numerics/Distributions/NormalGamma.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// This structure represents the type over which the <see cref="NormalGamma"/> distribution /// This structure represents the type over which the <see cref="NormalGamma"/> distribution
/// is defined. /// is defined.
@ -79,6 +83,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Multivariate Normal-Gamma Distribution.
/// <para>The <see cref="NormalGamma"/> distribution is the conjugate prior distribution for the <see cref="Normal"/> /// <para>The <see cref="NormalGamma"/> distribution is the conjugate prior distribution for the <see cref="Normal"/>
/// distribution. It specifies a prior over the mean and precision of the <see cref="Normal"/> distribution.</para> /// distribution. It specifies a prior over the mean and precision of the <see cref="Normal"/> distribution.</para>
/// <para>It is parameterized by four numbers: the mean location, the mean scale, the precision shape and the /// <para>It is parameterized by four numbers: the mean location, the mean scale, the precision shape and the
@ -120,7 +125,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="NormalGamma"/> class. /// Initializes a new instance of the <see cref="NormalGamma"/> class.
@ -131,7 +136,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="precisionInverseScale">The inverse scale of the precision.</param> /// <param name="precisionInverseScale">The inverse scale of the precision.</param>
public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale) public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale); SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale);
} }
@ -143,9 +148,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="precisionShape">The shape of the precision.</param> /// <param name="precisionShape">The shape of the precision.</param>
/// <param name="precisionInverseScale">The inverse scale of the precision.</param> /// <param name="precisionInverseScale">The inverse scale of the precision.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale, Random randomSource) public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale); SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale);
} }
@ -190,7 +195,7 @@ namespace MathNet.Numerics.Distributions
public override string ToString() public override string ToString()
{ {
return "NormalGamma(Mean Location = " + _meanLocation + ", Mean Scale = " + _meanScale + return "NormalGamma(Mean Location = " + _meanLocation + ", Mean Scale = " + _meanScale +
", Precision Shape = " + _precisionShape + ", Precision Inverse Scale = " + _precisionInvScale + ")"; ", Precision Shape = " + _precisionShape + ", Precision Inverse Scale = " + _precisionInvScale + ")";
} }
/// <summary> /// <summary>
@ -232,7 +237,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -324,7 +329,7 @@ namespace MathNet.Numerics.Distributions
// return Math.Pow(prec * _precisionInvScale, _precisionShape) * Math.Exp(e) / (Constants.Sqrt2Pi * Math.Sqrt(prec) * SpecialFunctions.Gamma(_precisionShape)); // return Math.Pow(prec * _precisionInvScale, _precisionShape) * Math.Exp(e) / (Constants.Sqrt2Pi * Math.Sqrt(prec) * SpecialFunctions.Gamma(_precisionShape));
double e = -(0.5*prec*_meanScale*(mean - _meanLocation)*(mean - _meanLocation)) - (prec*_precisionInvScale); double e = -(0.5*prec*_meanScale*(mean - _meanLocation)*(mean - _meanLocation)) - (prec*_precisionInvScale);
return Math.Pow(prec*_precisionInvScale, _precisionShape)*Math.Exp(e)*Math.Sqrt(_meanScale) return Math.Pow(prec*_precisionInvScale, _precisionShape)*Math.Exp(e)*Math.Sqrt(_meanScale)
/(Constants.Sqrt2Pi*Math.Sqrt(prec)*SpecialFunctions.Gamma(_precisionShape)); /(Constants.Sqrt2Pi*Math.Sqrt(prec)*SpecialFunctions.Gamma(_precisionShape));
} }
/// <summary> /// <summary>
@ -396,7 +401,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="precisionShape">The shape of the precision.</param> /// <param name="precisionShape">The shape of the precision.</param>
/// <param name="precisionInverseScale">The inverse scale of the precision.</param> /// <param name="precisionInverseScale">The inverse scale of the precision.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static MeanPrecisionPair Sample(Random rnd, double meanLocation, double meanScale, double precisionShape, double precisionInverseScale) public static MeanPrecisionPair Sample(System.Random rnd, double meanLocation, double meanScale, double precisionShape, double precisionInverseScale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInverseScale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInverseScale))
{ {
@ -423,7 +428,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="precisionShape">The shape of the precision.</param> /// <param name="precisionShape">The shape of the precision.</param>
/// <param name="precisionInvScale">The inverse scale of the precision.</param> /// <param name="precisionInvScale">The inverse scale of the precision.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<MeanPrecisionPair> Samples(Random rnd, double meanLocation, double meanScale, double precisionShape, double precisionInvScale) public static IEnumerable<MeanPrecisionPair> Samples(System.Random rnd, double meanLocation, double meanScale, double precisionShape, double precisionInvScale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInvScale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInvScale))
{ {

61
src/Numerics/Distributions/Continuous/Pareto.cs → src/Numerics/Distributions/Pareto.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Continuous Univariate Pareto distribution.
/// The Pareto distribution is a power law probability distribution that coincides with social, /// The Pareto distribution is a power law probability distribution that coincides with social,
/// scientific, geophysical, actuarial, and many other types of observable phenomena. /// scientific, geophysical, actuarial, and many other types of observable phenomena.
/// For details about this distribution, see /// For details about this distribution, see
@ -56,7 +61,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Pareto"/> class. /// Initializes a new instance of the <see cref="Pareto"/> class.
@ -66,7 +71,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> or <paramref name="shape"/> are negative.</exception> /// <exception cref="ArgumentException">If <paramref name="scale"/> or <paramref name="shape"/> are negative.</exception>
public Pareto(double scale, double shape) public Pareto(double scale, double shape)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(scale, shape); SetParameters(scale, shape);
} }
@ -77,9 +82,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape parameter of the distribution.</param> /// <param name="shape">The shape parameter of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> or <paramref name="shape"/> are negative.</exception> /// <exception cref="ArgumentException">If <paramref name="scale"/> or <paramref name="shape"/> are negative.</exception>
public Pareto(double scale, double shape, Random randomSource) public Pareto(double scale, double shape, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(scale, shape); SetParameters(scale, shape);
} }
@ -138,12 +143,10 @@ namespace MathNet.Numerics.Distributions
return "Pareto(Scale = " + _scale + ", Shape = " + _shape + ")"; return "Pareto(Scale = " + _scale + ", Shape = " + _shape + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -213,20 +216,6 @@ namespace MathNet.Numerics.Distributions
get { return (2.0*(_shape + 1.0)/(_shape - 3.0))*Math.Sqrt((_shape - 2.0)/_shape); } get { return (2.0*(_shape + 1.0)/(_shape - 3.0))*Math.Sqrt((_shape - 2.0)/_shape); }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 1.0 - Math.Pow(_scale/x, _shape);
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -260,7 +249,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -270,7 +259,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -279,7 +268,15 @@ namespace MathNet.Numerics.Distributions
return Math.Log(Density(x)); return Math.Log(Density(x));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return 1.0 - Math.Pow(_scale/x, _shape);
}
/// <summary> /// <summary>
/// Generates a sample from the Pareto distribution without doing parameter checking. /// Generates a sample from the Pareto distribution without doing parameter checking.
@ -288,7 +285,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <param name="shape">The shape parameter.</param> /// <param name="shape">The shape parameter.</param>
/// <returns>a random number from the Pareto distribution.</returns> /// <returns>a random number from the Pareto distribution.</returns>
internal static double SampleUnchecked(Random rnd, double scale, double shape) internal static double SampleUnchecked(System.Random rnd, double scale, double shape)
{ {
return scale*Math.Pow(rnd.NextDouble(), -1.0/shape); return scale*Math.Pow(rnd.NextDouble(), -1.0/shape);
} }
@ -321,7 +318,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <param name="shape">The shape parameter.</param> /// <param name="shape">The shape parameter.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double scale, double shape) public static double Sample(System.Random rnd, double scale, double shape)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale, shape)) if (Control.CheckDistributionParameters && !IsValidParameterSet(scale, shape))
{ {
@ -338,7 +335,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <param name="shape">The shape parameter.</param> /// <param name="shape">The shape parameter.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double scale, double shape) public static IEnumerable<double> Samples(System.Random rnd, double scale, double shape)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale, shape)) if (Control.CheckDistributionParameters && !IsValidParameterSet(scale, shape))
{ {

66
src/Numerics/Distributions/Discrete/Poisson.cs → src/Numerics/Distributions/Poisson.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Pseudo-random generation of poisson distributed deviates. /// Discrete Univariate Poisson distribution.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// <para>Distribution is described at <a href="http://en.wikipedia.org/wiki/Poisson_distribution"> Wikipedia - Poisson distribution</a>.</para> /// <para>Distribution is described at <a href="http://en.wikipedia.org/wiki/Poisson_distribution"> Wikipedia - Poisson distribution</a>.</para>
@ -48,7 +52,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Gets or sets the Poisson distribution parameter λ. /// Gets or sets the Poisson distribution parameter λ.
@ -66,7 +70,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="System.ArgumentOutOfRangeException">If <paramref name="lambda"/> is equal or less then 0.0.</exception> /// <exception cref="System.ArgumentOutOfRangeException">If <paramref name="lambda"/> is equal or less then 0.0.</exception>
public Poisson(double lambda) public Poisson(double lambda)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(lambda); SetParameters(lambda);
} }
@ -76,9 +80,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="lambda">The Poisson distribution parameter λ.</param> /// <param name="lambda">The Poisson distribution parameter λ.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="System.ArgumentOutOfRangeException">If <paramref name="lambda"/> is equal or less then 0.0.</exception> /// <exception cref="System.ArgumentOutOfRangeException">If <paramref name="lambda"/> is equal or less then 0.0.</exception>
public Poisson(double lambda, Random randomSource) public Poisson(double lambda, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(lambda); SetParameters(lambda);
} }
@ -118,12 +122,10 @@ namespace MathNet.Numerics.Distributions
return "Poisson(λ = " + _lambda + ")"; return "Poisson(λ = " + _lambda + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -194,20 +196,6 @@ namespace MathNet.Numerics.Distributions
get { return int.MaxValue; } get { return int.MaxValue; }
} }
/// <summary>
/// Computes the cumulative distribution function of the Poisson distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 1.0 - SpecialFunctions.GammaLowerRegularized(x + 1, _lambda);
}
#endregion
#region IDiscreteDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -226,7 +214,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the probability mass function. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <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> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
@ -236,7 +224,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the log probability mass function. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <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> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
@ -245,7 +233,15 @@ namespace MathNet.Numerics.Distributions
return -_lambda + (k*Math.Log(_lambda)) - SpecialFunctions.FactorialLn(k); return -_lambda + (k*Math.Log(_lambda)) - SpecialFunctions.FactorialLn(k);
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return 1.0 - SpecialFunctions.GammaLowerRegularized(x + 1, _lambda);
}
/// <summary> /// <summary>
/// Generates one sample from the Poisson distribution. /// Generates one sample from the Poisson distribution.
@ -253,7 +249,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random source to use.</param> /// <param name="rnd">The random source to use.</param>
/// <param name="lambda">The Poisson distribution parameter λ.</param> /// <param name="lambda">The Poisson distribution parameter λ.</param>
/// <returns>A random sample from the Poisson distribution.</returns> /// <returns>A random sample from the Poisson distribution.</returns>
internal static int SampleUnchecked(Random rnd, double lambda) internal static int SampleUnchecked(System.Random rnd, double lambda)
{ {
return (lambda < 30.0) ? DoSampleShort(rnd, lambda) : DoSampleLarge(rnd, lambda); return (lambda < 30.0) ? DoSampleShort(rnd, lambda) : DoSampleLarge(rnd, lambda);
} }
@ -264,7 +260,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random source to use.</param> /// <param name="rnd">The random source to use.</param>
/// <param name="lambda">The Poisson distribution parameter λ.</param> /// <param name="lambda">The Poisson distribution parameter λ.</param>
/// <returns>A random sample from the Poisson distribution.</returns> /// <returns>A random sample from the Poisson distribution.</returns>
static int DoSampleShort(Random rnd, double lambda) static int DoSampleShort(System.Random rnd, double lambda)
{ {
var limit = Math.Exp(-lambda); var limit = Math.Exp(-lambda);
var count = 0; var count = 0;
@ -285,7 +281,7 @@ namespace MathNet.Numerics.Distributions
/// <remarks>"Rejection method PA" from "The Computer Generation of Poisson Random Variables" by A. C. Atkinson, /// <remarks>"Rejection method PA" from "The Computer Generation of Poisson Random Variables" by A. C. Atkinson,
/// Journal of the Royal Statistical Society Series C (Applied Statistics) Vol. 28, No. 1. (1979) /// Journal of the Royal Statistical Society Series C (Applied Statistics) Vol. 28, No. 1. (1979)
/// The article is on pages 29-35. The algorithm given here is on page 32. </remarks> /// The article is on pages 29-35. The algorithm given here is on page 32. </remarks>
static int DoSampleLarge(Random rnd, double lambda) static int DoSampleLarge(System.Random rnd, double lambda)
{ {
var c = 0.767 - (3.36/lambda); var c = 0.767 - (3.36/lambda);
var beta = Math.PI/Math.Sqrt(3.0*lambda); var beta = Math.PI/Math.Sqrt(3.0*lambda);
@ -341,7 +337,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The Poisson distribution parameter λ.</param> /// <param name="lambda">The Poisson distribution parameter λ.</param>
/// <returns>A sample from the Poisson distribution.</returns> /// <returns>A sample from the Poisson distribution.</returns>
public static int Sample(Random rnd, double lambda) public static int Sample(System.Random rnd, double lambda)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{ {
@ -357,7 +353,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The Poisson distribution parameter λ.</param> /// <param name="lambda">The Poisson distribution parameter λ.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<int> Samples(Random rnd, double lambda) public static IEnumerable<int> Samples(System.Random rnd, double lambda)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda)) if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{ {

61
src/Numerics/Distributions/Continuous/Rayleigh.cs → src/Numerics/Distributions/Rayleigh.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Continuous Univariate Rayleigh distribution.
/// The Rayleigh distribution (pronounced /ˈreɪli/) is a continuous probability distribution. As an /// The Rayleigh distribution (pronounced /ˈreɪli/) is a continuous probability distribution. As an
/// example of how it arises, the wind speed will have a Rayleigh distribution if the components of /// example of how it arises, the wind speed will have a Rayleigh distribution if the components of
/// the two-dimensional wind velocity vector are uncorrelated and normally distributed with equal variance. /// the two-dimensional wind velocity vector are uncorrelated and normally distributed with equal variance.
@ -52,7 +57,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Rayleigh"/> class. /// Initializes a new instance of the <see cref="Rayleigh"/> class.
@ -61,7 +66,7 @@ namespace MathNet.Numerics.Distributions
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception> /// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Rayleigh(double scale) public Rayleigh(double scale)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(scale); SetParameters(scale);
} }
@ -71,9 +76,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale parameter of the distribution.</param> /// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
/// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception> /// <exception cref="ArgumentException">If <paramref name="scale"/> is negative.</exception>
public Rayleigh(double scale, Random randomSource) public Rayleigh(double scale, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(scale); SetParameters(scale);
} }
@ -120,12 +125,10 @@ namespace MathNet.Numerics.Distributions
return "Rayleigh(Scale = " + _scale + ")"; return "Rayleigh(Scale = " + _scale + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -179,20 +182,6 @@ namespace MathNet.Numerics.Distributions
get { return (2.0*Math.Sqrt(Constants.Pi)*(Constants.Pi - 3.0))/Math.Pow(4.0 - Constants.Pi, 1.5); } get { return (2.0*Math.Sqrt(Constants.Pi)*(Constants.Pi - 3.0))/Math.Pow(4.0 - Constants.Pi, 1.5); }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return 1.0 - Math.Exp(-x*x/(2.0*_scale*_scale));
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -226,7 +215,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -236,7 +225,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -245,7 +234,15 @@ namespace MathNet.Numerics.Distributions
return Math.Log(x/(_scale*_scale)) - (x*x/(2.0*_scale*_scale)); return Math.Log(x/(_scale*_scale)) - (x*x/(2.0*_scale*_scale));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
return 1.0 - Math.Exp(-x*x/(2.0*_scale*_scale));
}
/// <summary> /// <summary>
/// Generates a sample from the Rayleigh distribution without doing parameter checking. /// Generates a sample from the Rayleigh distribution without doing parameter checking.
@ -253,7 +250,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <returns>a random number from the Rayleigh distribution.</returns> /// <returns>a random number from the Rayleigh distribution.</returns>
internal static double SampleUnchecked(Random rnd, double scale) internal static double SampleUnchecked(System.Random rnd, double scale)
{ {
return scale*Math.Sqrt(-2.0*Math.Log(rnd.NextDouble())); return scale*Math.Sqrt(-2.0*Math.Log(rnd.NextDouble()));
} }
@ -285,7 +282,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double scale) public static double Sample(System.Random rnd, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
{ {
@ -301,7 +298,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="scale">The scale parameter.</param> /// <param name="scale">The scale parameter.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double scale) public static IEnumerable<double> Samples(System.Random rnd, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(scale))
{ {

123
src/Numerics/Distributions/Continuous/Stable.cs → src/Numerics/Distributions/Stable.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Continuous Univariate Stable distribution.
/// A random variable is said to be stable (or to have a stable distribution) if it has /// A random variable is said to be stable (or to have a stable distribution) if it has
/// the property that a linear combination of two independent copies of the variable has /// the property that a linear combination of two independent copies of the variable has
/// the same distribution, up to location and scale parameters. /// the same distribution, up to location and scale parameters.
@ -67,7 +72,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Stable"/> class. /// Initializes a new instance of the <see cref="Stable"/> class.
@ -78,7 +83,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="location">The location parameter of the distribution.</param> /// <param name="location">The location parameter of the distribution.</param>
public Stable(double alpha, double beta, double scale, double location) public Stable(double alpha, double beta, double scale, double location)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(alpha, beta, scale, location); SetParameters(alpha, beta, scale, location);
} }
@ -90,9 +95,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale parameter of the distribution.</param> /// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param> /// <param name="location">The location parameter of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Stable(double alpha, double beta, double scale, double location, Random randomSource) public Stable(double alpha, double beta, double scale, double location, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(alpha, beta, scale, location); SetParameters(alpha, beta, scale, location);
} }
@ -174,12 +179,10 @@ namespace MathNet.Numerics.Distributions
return "Stable(" + "Stability = " + _alpha + ", Skewness = " + _beta + ", Scale = " + _scale + ", Location = " + _location + ")"; return "Stable(" + "Stability = " + _alpha + ", Skewness = " + _beta + ", Scale = " + _scale + ", Location = " + _location + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -267,51 +270,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
/// <remarks>Throws a not supported exception if <c>Alpha != 2</c>, <c>(Alpha != 1 and Beta !=0)</c>, or <c>(Alpha != 0.5 and Beta != 1)</c></remarks>
public double CumulativeDistribution(double x)
{
if (_alpha == 2)
{
return (new Normal(_location, StdDev)).CumulativeDistribution(x);
}
if (_alpha == 1 && _beta == 0)
{
return (new Cauchy(_location, _scale)).CumulativeDistribution(x);
}
if (_alpha == 0.5 && _beta == 1)
{
return LevyCumulativeDistribution(_scale, _location, x);
}
throw new NotSupportedException();
}
/// <summary>
/// Computes the cumulative distribution function of the Levy distribution.
/// </summary>
/// <param name="scale">The scale parameter.</param>
/// <param name="location">The location parameter.</param>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>
/// the cumulative density at <paramref name="x"/>.
/// </returns>
static double LevyCumulativeDistribution(double scale, double location, double x)
{
// The parameters scale and location must be correct
return SpecialFunctions.Erfc(Math.Sqrt(scale/(2*(x - location))));
}
#endregion
#region IContinuousDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -371,7 +329,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -414,7 +372,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -423,7 +381,44 @@ namespace MathNet.Numerics.Distributions
return Math.Log(Density(x)); return Math.Log(Density(x));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
/// <remarks>Throws a not supported exception if <c>Alpha != 2</c>, <c>(Alpha != 1 and Beta !=0)</c>, or <c>(Alpha != 0.5 and Beta != 1)</c></remarks>
public double CumulativeDistribution(double x)
{
if (_alpha == 2)
{
return (new Normal(_location, StdDev)).CumulativeDistribution(x);
}
if (_alpha == 1 && _beta == 0)
{
return (new Cauchy(_location, _scale)).CumulativeDistribution(x);
}
if (_alpha == 0.5 && _beta == 1)
{
return LevyCumulativeDistribution(_scale, _location, x);
}
throw new NotSupportedException();
}
/// <summary>
/// Computes the cumulative distribution function of the Levy distribution.
/// </summary>
/// <param name="scale">The scale parameter.</param>
/// <param name="location">The location parameter.</param>
/// <param name="x">The location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
static double LevyCumulativeDistribution(double scale, double location, double x)
{
// The parameters scale and location must be correct
return SpecialFunctions.Erfc(Math.Sqrt(scale/(2*(x - location))));
}
/// <summary> /// <summary>
/// Samples the distribution. /// Samples the distribution.
@ -434,7 +429,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale parameter of the distribution.</param> /// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param> /// <param name="location">The location parameter of the distribution.</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
internal static double SampleUnchecked(Random rnd, double alpha, double beta, double scale, double location) internal static double SampleUnchecked(System.Random rnd, double alpha, double beta, double scale, double location)
{ {
var randTheta = ContinuousUniform.Sample(rnd, -Constants.PiOver2, Constants.PiOver2); var randTheta = ContinuousUniform.Sample(rnd, -Constants.PiOver2, Constants.PiOver2);
var randW = Exponential.Sample(rnd, 1.0); var randW = Exponential.Sample(rnd, 1.0);
@ -492,7 +487,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale parameter of the distribution.</param> /// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param> /// <param name="location">The location parameter of the distribution.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rnd, double alpha, double beta, double scale, double location) public static double Sample(System.Random rnd, double alpha, double beta, double scale, double location)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha, beta, scale, location)) if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha, beta, scale, location))
{ {
@ -511,7 +506,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale parameter of the distribution.</param> /// <param name="scale">The scale parameter of the distribution.</param>
/// <param name="location">The location parameter of the distribution.</param> /// <param name="location">The location parameter of the distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double alpha, double beta, double scale, double location) public static IEnumerable<double> Samples(System.Random rnd, double alpha, double beta, double scale, double location)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, scale, location)) if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, scale, location))
{ {
@ -524,4 +519,4 @@ namespace MathNet.Numerics.Distributions
} }
} }
} }
} }

61
src/Numerics/Distributions/Continuous/StudentT.cs → src/Numerics/Distributions/StudentT.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Continuous Univariate Student's T-distribution.
/// Implements the univariate Student t-distribution. For details about this /// Implements the univariate Student t-distribution. For details about this
/// distribution, see /// distribution, see
/// <a href="http://en.wikipedia.org/wiki/Student%27s_t-distribution"> /// <a href="http://en.wikipedia.org/wiki/Student%27s_t-distribution">
@ -70,7 +75,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the StudentT class. This is a Student t-distribution with location 0.0 /// Initializes a new instance of the StudentT class. This is a Student t-distribution with location 0.0
@ -92,7 +97,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="dof">The degrees of freedom for 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) public StudentT(double location, double scale, double dof)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(location, scale, dof); SetParameters(location, scale, dof);
} }
@ -105,9 +110,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale 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="dof">The degrees of freedom for the Student t-distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public StudentT(double location, double scale, double dof, Random randomSource) public StudentT(double location, double scale, double dof, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(location, scale, dof); SetParameters(location, scale, dof);
} }
@ -178,12 +183,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_location, _scale, value); } set { SetParameters(_location, _scale, value); }
} }
#region IDistribution implementation
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -279,10 +282,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
#endregion
#region IContinuousDistribution implementation
/// <summary> /// <summary>
/// Gets the mode of the Student t-distribution. /// Gets the mode of the Student t-distribution.
/// </summary> /// </summary>
@ -316,7 +315,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the Student t-distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -330,13 +329,13 @@ namespace MathNet.Numerics.Distributions
var d = (x - _location)/_scale; var d = (x - _location)/_scale;
return Math.Exp(SpecialFunctions.GammaLn((_dof + 1.0)/2.0) - SpecialFunctions.GammaLn(_dof/2.0)) return Math.Exp(SpecialFunctions.GammaLn((_dof + 1.0)/2.0) - SpecialFunctions.GammaLn(_dof/2.0))
*Math.Pow(1.0 + (d*d/_dof), -0.5*(_dof + 1.0)) *Math.Pow(1.0 + (d*d/_dof), -0.5*(_dof + 1.0))
/Math.Sqrt(_dof*Math.PI) /Math.Sqrt(_dof*Math.PI)
/_scale; /_scale;
} }
/// <summary> /// <summary>
/// Computes the log density of the Student t-distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -350,16 +349,16 @@ namespace MathNet.Numerics.Distributions
var d = (x - _location)/_scale; var d = (x - _location)/_scale;
return SpecialFunctions.GammaLn((_dof + 1.0)/2.0) return SpecialFunctions.GammaLn((_dof + 1.0)/2.0)
- (0.5*((_dof + 1.0)*Math.Log(1.0 + (d*d/_dof)))) - (0.5*((_dof + 1.0)*Math.Log(1.0 + (d*d/_dof))))
- SpecialFunctions.GammaLn(_dof/2.0) - SpecialFunctions.GammaLn(_dof/2.0)
- (0.5*Math.Log(_dof*Math.PI)) - Math.Log(_scale); - (0.5*Math.Log(_dof*Math.PI)) - Math.Log(_scale);
} }
/// <summary> /// <summary>
/// Computes the cumulative distribution function of the Student t-distribution. /// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
// TODO JVG we can probably do a better job for Cauchy special case // TODO JVG we can probably do a better job for Cauchy special case
@ -374,8 +373,6 @@ namespace MathNet.Numerics.Distributions
return x <= _location ? ib : 1.0 - ib; return x <= _location ? ib : 1.0 - ib;
} }
#endregion
/// <summary> /// <summary>
/// Samples student-t distributed random variables. /// Samples student-t distributed random variables.
/// </summary> /// </summary>
@ -386,7 +383,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale 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="dof">The degrees of freedom for the standard student-t distribution.</param>
/// <returns>a random number from the standard student-t distribution.</returns> /// <returns>a random number from the standard student-t distribution.</returns>
internal static double SampleUnchecked(Random rnd, double location, double scale, double dof) internal static double SampleUnchecked(System.Random rnd, double location, double scale, double dof)
{ {
var n = Normal.SampleUncheckedBoxMuller(rnd).Item1; var n = Normal.SampleUncheckedBoxMuller(rnd).Item1;
var g = Gamma.SampleUnchecked(rnd, 0.5*dof, 0.5); var g = Gamma.SampleUnchecked(rnd, 0.5*dof, 0.5);
@ -422,7 +419,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale 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="dof">The degrees of freedom for the Student t-distribution.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rng, double location, double scale, double dof) public static double Sample(System.Random rng, double location, double scale, double dof)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, dof)) if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, dof))
{ {
@ -440,7 +437,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The scale 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="dof">The degrees of freedom for the Student t-distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rng, double location, double scale, double dof) public static IEnumerable<double> Samples(System.Random rng, double location, double scale, double dof)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, dof)) if (Control.CheckDistributionParameters && !IsValidParameterSet(location, scale, dof))
{ {

45
src/Numerics/Distributions/Continuous/Weibull.cs → src/Numerics/Distributions/Weibull.cs

@ -28,14 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Implements the Weibull distribution. For details about this distribution, see /// Continuous Univariate Weibull distribution.
/// For details about this distribution, see
/// <a href="http://en.wikipedia.org/wiki/Weibull_distribution">Wikipedia - Weibull distribution</a>. /// <a href="http://en.wikipedia.org/wiki/Weibull_distribution">Wikipedia - Weibull distribution</a>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
@ -69,7 +70,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the Weibull class. /// Initializes a new instance of the Weibull class.
@ -78,7 +79,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="scale">The inverse scale of the Weibull distribution.</param> /// <param name="scale">The inverse scale of the Weibull distribution.</param>
public Weibull(double shape, double scale) public Weibull(double shape, double scale)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(shape, scale); SetParameters(shape, scale);
} }
@ -88,9 +89,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Weibull distribution.</param> /// <param name="shape">The shape of the Weibull distribution.</param>
/// <param name="scale">The inverse scale of the Weibull distribution.</param> /// <param name="scale">The inverse scale of the Weibull distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Weibull(double shape, double scale, Random randomSource) public Weibull(double shape, double scale, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(shape, scale); SetParameters(shape, scale);
} }
@ -150,12 +151,10 @@ namespace MathNet.Numerics.Distributions
set { SetParameters(_shape, value); } set { SetParameters(_shape, value); }
} }
#region IDistribution implementation
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -216,10 +215,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
#endregion
#region IContinuousDistribution implementation
/// <summary> /// <summary>
/// Gets the mode of the Weibull distribution. /// Gets the mode of the Weibull distribution.
/// </summary> /// </summary>
@ -261,7 +256,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the density of the Weibull distribution. /// Computes the density of the distribution (PDF), i.e. dP(X &lt;= x)/dx.
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the density.</param> /// <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>
@ -281,7 +276,7 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the log density of the Weibull distribution. /// Computes the log density of the distribution (lnPDF), i.e. ln(dP(X &lt;= x)/dx).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the log density.</param> /// <param name="x">The location at which to compute the log density.</param>
/// <returns>the log density at <paramref name="x"/>.</returns> /// <returns>the log density at <paramref name="x"/>.</returns>
@ -301,10 +296,10 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes the cumulative distribution function of the Weibull distribution. /// Computes the cumulative distribution (CDF) of the distribution, i.e. P(X &lt;= x).
/// </summary> /// </summary>
/// <param name="x">The location at which to compute the cumulative density.</param> /// <param name="x">The location at which to compute the cumulative distribution function.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns> /// <returns>the cumulative distribution at location <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x) public double CumulativeDistribution(double x)
{ {
if (x < 0.0) if (x < 0.0)
@ -315,8 +310,6 @@ namespace MathNet.Numerics.Distributions
return -SpecialFunctions.ExponentialMinusOne(-Math.Pow(x, _shape)*_scalePowShapeInv); return -SpecialFunctions.ExponentialMinusOne(-Math.Pow(x, _shape)*_scalePowShapeInv);
} }
#endregion
/// <summary> /// <summary>
/// Generates one sample from the Weibull distribution. This method doesn't perform /// Generates one sample from the Weibull distribution. This method doesn't perform
/// any parameter checks. /// any parameter checks.
@ -325,7 +318,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Weibull distribution.</param> /// <param name="shape">The shape of the Weibull distribution.</param>
/// <param name="scale">The scale 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> /// <returns>A sample from a Weibull distributed random variable.</returns>
internal static double SampleUnchecked(Random rnd, double shape, double scale) internal static double SampleUnchecked(System.Random rnd, double shape, double scale)
{ {
var x = rnd.NextDouble(); var x = rnd.NextDouble();
return scale*Math.Pow(-Math.Log(x), 1.0/shape); return scale*Math.Pow(-Math.Log(x), 1.0/shape);
@ -359,7 +352,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Weibull distribution from which to generate samples.</param> /// <param name="shape">The shape of the Weibull distribution from which to generate samples.</param>
/// <param name="scale">The scale of the Weibull distribution from which to generate samples.</param> /// <param name="scale">The scale of the Weibull distribution from which to generate samples.</param>
/// <returns>a sample from the distribution.</returns> /// <returns>a sample from the distribution.</returns>
public static double Sample(Random rng, double shape, double scale) public static double Sample(System.Random rng, double shape, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale))
{ {
@ -376,7 +369,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="shape">The shape of the Weibull distribution from which to generate samples.</param> /// <param name="shape">The shape of the Weibull distribution from which to generate samples.</param>
/// <param name="scale">The scale of the Weibull distribution from which to generate samples.</param> /// <param name="scale">The scale of the Weibull distribution from which to generate samples.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rng, double shape, double scale) public static IEnumerable<double> Samples(System.Random rng, double shape, double scale)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale)) if (Control.CheckDistributionParameters && !IsValidParameterSet(shape, scale))
{ {

33
src/Numerics/Distributions/Multivariate/Wishart.cs → src/Numerics/Distributions/Wishart.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,6 +28,7 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
@ -31,10 +36,8 @@ using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
/// <summary> /// <summary>
/// This class implements functionality for the Wishart distribution. This distribution is /// Multivariate Wishart distribution. This distribution is
/// parameterized by the degrees of freedom nu and the scale matrix S. The Wishart distribution /// parameterized by the degrees of freedom nu and the scale matrix S. The Wishart distribution
/// is the conjugate prior for the precision (inverse covariance) matrix of the multivariate /// is the conjugate prior for the precision (inverse covariance) matrix of the multivariate
/// normal distribution. /// normal distribution.
@ -65,7 +68,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Wishart"/> class. /// Initializes a new instance of the <see cref="Wishart"/> class.
@ -74,7 +77,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="s">The scale matrix for the Wishart distribution.</param> /// <param name="s">The scale matrix for the Wishart distribution.</param>
public Wishart(double nu, Matrix<double> s) public Wishart(double nu, Matrix<double> s)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(nu, s); SetParameters(nu, s);
} }
@ -84,9 +87,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The degrees of freedom for the Wishart distribution.</param> /// <param name="nu">The degrees of freedom for the Wishart distribution.</param>
/// <param name="s">The scale matrix for the Wishart distribution.</param> /// <param name="s">The scale matrix for the Wishart distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Wishart(double nu, Matrix<double> s, Random randomSource) public Wishart(double nu, Matrix<double> s, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(nu, s); SetParameters(nu, s);
} }
@ -167,7 +170,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -246,10 +249,10 @@ namespace MathNet.Numerics.Distributions
} }
return Math.Pow(dX, (_nu - p - 1.0)/2.0) return Math.Pow(dX, (_nu - p - 1.0)/2.0)
*Math.Exp(-0.5*siX.Trace()) *Math.Exp(-0.5*siX.Trace())
/Math.Pow(2.0, _nu*p/2.0) /Math.Pow(2.0, _nu*p/2.0)
/Math.Pow(_chol.Determinant, _nu/2.0) /Math.Pow(_chol.Determinant, _nu/2.0)
/gp; /gp;
} }
/// <summary> /// <summary>
@ -274,7 +277,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="nu">The degrees of freedom.</param> /// <param name="nu">The degrees of freedom.</param>
/// <param name="s">The scale matrix.</param> /// <param name="s">The scale matrix.</param>
/// <returns>a sequence of samples from the distribution.</returns> /// <returns>a sequence of samples from the distribution.</returns>
public static Matrix<double> Sample(Random rnd, double nu, Matrix<double> s) public static Matrix<double> Sample(System.Random rnd, double nu, Matrix<double> s)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s)) if (Control.CheckDistributionParameters && !IsValidParameterSet(nu, s))
{ {
@ -292,7 +295,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="s">The S parameter to use.</param> /// <param name="s">The S parameter to use.</param>
/// <param name="chol">The cholesky decomposition to use.</param> /// <param name="chol">The cholesky decomposition to use.</param>
/// <returns>a random number from the distribution.</returns> /// <returns>a random number from the distribution.</returns>
static Matrix<double> DoSample(Random rnd, double nu, Matrix<double> s, Cholesky<double> chol) static Matrix<double> DoSample(System.Random rnd, double nu, Matrix<double> s, Cholesky<double> chol)
{ {
var count = s.RowCount; var count = s.RowCount;

79
src/Numerics/Distributions/Discrete/Zipf.cs → src/Numerics/Distributions/Zipf.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System;
using System.Collections.Generic;
using Properties;
/// <summary> /// <summary>
/// Discrete Univariate Zipf distribution.
/// Zipf's law, an empirical law formulated using mathematical statistics, refers to the fact /// Zipf's law, an empirical law formulated using mathematical statistics, refers to the fact
/// that many types of data studied in the physical and social sciences can be approximated with /// that many types of data studied in the physical and social sciences can be approximated with
/// a Zipfian distribution, one of a family of related discrete power law probability distributions. /// a Zipfian distribution, one of a family of related discrete power law probability distributions.
@ -57,7 +62,7 @@ namespace MathNet.Numerics.Distributions
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
/// </summary> /// </summary>
Random _random; System.Random _random;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Zipf"/> class. /// Initializes a new instance of the <see cref="Zipf"/> class.
@ -66,7 +71,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="n">The n parameter of the distribution.</param> /// <param name="n">The n parameter of the distribution.</param>
public Zipf(double s, int n) public Zipf(double s, int n)
{ {
_random = new Random(); _random = new System.Random();
SetParameters(s, n); SetParameters(s, n);
} }
@ -76,9 +81,9 @@ namespace MathNet.Numerics.Distributions
/// <param name="s">The s parameter of the distribution.</param> /// <param name="s">The s parameter of the distribution.</param>
/// <param name="n">The n parameter of the distribution.</param> /// <param name="n">The n parameter of the distribution.</param>
/// <param name="randomSource">The random number generator which is used to draw random samples.</param> /// <param name="randomSource">The random number generator which is used to draw random samples.</param>
public Zipf(double s, int n, Random randomSource) public Zipf(double s, int n, System.Random randomSource)
{ {
_random = randomSource ?? new Random(); _random = randomSource ?? new System.Random();
SetParameters(s, n); SetParameters(s, n);
} }
@ -136,12 +141,10 @@ namespace MathNet.Numerics.Distributions
return "Zipf(S = " + _s + ", N = " + _n + ")"; return "Zipf(S = " + _s + ", N = " + _n + ")";
} }
#region IDistribution Members
/// <summary> /// <summary>
/// Gets or sets the random number generator which is used to draw random samples. /// Gets or sets the random number generator which is used to draw random samples.
/// </summary> /// </summary>
public Random RandomSource public System.Random RandomSource
{ {
get { return _random; } get { return _random; }
set set
@ -222,25 +225,6 @@ namespace MathNet.Numerics.Distributions
} }
} }
/// <summary>
/// Computes the cumulative distribution function of the distribution.
/// </summary>
/// <param name="x">The integer location at which to compute the cumulative density.</param>
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
if (x <= 1)
{
return 0.0;
}
return SpecialFunctions.GeneralHarmonic((int) x, _s)/SpecialFunctions.GeneralHarmonic(_n, _s);
}
#endregion
#region IDiscreteDistribution Members
/// <summary> /// <summary>
/// Gets the mode of the distribution. /// Gets the mode of the distribution.
/// </summary> /// </summary>
@ -274,30 +258,39 @@ namespace MathNet.Numerics.Distributions
} }
/// <summary> /// <summary>
/// Computes values of the probability mass function. /// Computes the probability mass (PMF), i.e. P(X = x).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the probability mass function.</param>
/// <returns> /// <returns>the probability mass at location <paramref name="k"/>.</returns>
/// the probability mass at location <paramref name="k"/>.
/// </returns>
public double Probability(int k) public double Probability(int k)
{ {
return (1.0/Math.Pow(k, _s))/SpecialFunctions.GeneralHarmonic(_n, _s); return (1.0/Math.Pow(k, _s))/SpecialFunctions.GeneralHarmonic(_n, _s);
} }
/// <summary> /// <summary>
/// Computes values of the log probability mass function. /// Computes the log probability mass (lnPMF), i.e. ln(P(X = x)).
/// </summary> /// </summary>
/// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param> /// <param name="k">The location in the domain where we want to evaluate the log probability mass function.</param>
/// <returns> /// <returns>the log probability mass at location <paramref name="k"/>.</returns>
/// the log probability mass at location <paramref name="k"/>.
/// </returns>
public double ProbabilityLn(int k) public double ProbabilityLn(int k)
{ {
return Math.Log(Probability(k)); return Math.Log(Probability(k));
} }
#endregion /// <summary>
/// Computes the cumulative distribution (CDF) of the distribution, 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>
public double CumulativeDistribution(double x)
{
if (x <= 1)
{
return 0.0;
}
return SpecialFunctions.GeneralHarmonic((int) x, _s)/SpecialFunctions.GeneralHarmonic(_n, _s);
}
/// <summary> /// <summary>
/// Generates a sample from the Zipf distribution without doing parameter checking. /// Generates a sample from the Zipf distribution without doing parameter checking.
@ -306,7 +299,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="s">The s parameter of the distribution.</param> /// <param name="s">The s parameter of the distribution.</param>
/// <param name="n">The n parameter of the distribution.</param> /// <param name="n">The n parameter of the distribution.</param>
/// <returns>a random number from the Zipf distribution.</returns> /// <returns>a random number from the Zipf distribution.</returns>
internal static int SampleUnchecked(Random rnd, double s, int n) internal static int SampleUnchecked(System.Random rnd, double s, int n)
{ {
var r = 0.0; var r = 0.0;
while (r == 0.0) while (r == 0.0)
@ -356,7 +349,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="s">The s parameter of the distribution.</param> /// <param name="s">The s parameter of the distribution.</param>
/// <param name="n">The n parameter of the distribution.</param> /// <param name="n">The n parameter of the distribution.</param>
public static int Sample(Random rnd, double s, int n) public static int Sample(System.Random rnd, double s, int n)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(s, n)) if (Control.CheckDistributionParameters && !IsValidParameterSet(s, n))
{ {
@ -372,7 +365,7 @@ namespace MathNet.Numerics.Distributions
/// <param name="rnd">The random number generator to use.</param> /// <param name="rnd">The random number generator to use.</param>
/// <param name="s">The s parameter of the distribution.</param> /// <param name="s">The s parameter of the distribution.</param>
/// <param name="n">The n parameter of the distribution.</param> /// <param name="n">The n parameter of the distribution.</param>
public static IEnumerable<int> Samples(Random rnd, double s, int n) public static IEnumerable<int> Samples(System.Random rnd, double s, int n)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(s, n)) if (Control.CheckDistributionParameters && !IsValidParameterSet(s, n))
{ {

8
src/Numerics/LinearAlgebra/Complex/Solvers/Iterative/MlkBiCgStab.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Iterative namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Iterative
{ {

8
src/Numerics/LinearAlgebra/Complex32/Solvers/Iterative/MlkBiCgStab.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Iterative namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Iterative
{ {

8
src/Numerics/LinearAlgebra/Double/Solvers/Iterative/MlkBiCgStab.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Iterative namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Iterative
{ {

6
src/Numerics/LinearAlgebra/Single/Solvers/Iterative/MlkBiCgStab.cs

@ -28,13 +28,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners; using MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers.Status; using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Iterative namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Iterative
{ {

70
src/Numerics/Numerics.csproj

@ -86,6 +86,40 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Distributions\Bernoulli.cs" />
<Compile Include="Distributions\Beta.cs" />
<Compile Include="Distributions\Binomial.cs" />
<Compile Include="Distributions\Categorical.cs" />
<Compile Include="Distributions\Cauchy.cs" />
<Compile Include="Distributions\Chi.cs" />
<Compile Include="Distributions\ChiSquare.cs" />
<Compile Include="Distributions\ContinuousUniform.cs" />
<Compile Include="Distributions\ConwayMaxwellPoisson.cs" />
<Compile Include="Distributions\Dirichlet.cs" />
<Compile Include="Distributions\DiscreteUniform.cs" />
<Compile Include="Distributions\Erlang.cs" />
<Compile Include="Distributions\Exponential.cs" />
<Compile Include="Distributions\FisherSnedecor.cs" />
<Compile Include="Distributions\Gamma.cs" />
<Compile Include="Distributions\Geometric.cs" />
<Compile Include="Distributions\Hypergeometric.cs" />
<Compile Include="Distributions\InverseGamma.cs" />
<Compile Include="Distributions\InverseWishart.cs" />
<Compile Include="Distributions\Laplace.cs" />
<Compile Include="Distributions\LogNormal.cs" />
<Compile Include="Distributions\MatrixNormal.cs" />
<Compile Include="Distributions\Multinomial.cs" />
<Compile Include="Distributions\NegativeBinomial.cs" />
<Compile Include="Distributions\Normal.cs" />
<Compile Include="Distributions\NormalGamma.cs" />
<Compile Include="Distributions\Pareto.cs" />
<Compile Include="Distributions\Poisson.cs" />
<Compile Include="Distributions\Rayleigh.cs" />
<Compile Include="Distributions\Stable.cs" />
<Compile Include="Distributions\StudentT.cs" />
<Compile Include="Distributions\Weibull.cs" />
<Compile Include="Distributions\Wishart.cs" />
<Compile Include="Distributions\Zipf.cs" />
<Compile Include="Providers\LinearAlgebra\Acml\AcmlLinearAlgebraProvider.Complex.cs" /> <Compile Include="Providers\LinearAlgebra\Acml\AcmlLinearAlgebraProvider.Complex.cs" />
<Compile Include="Providers\LinearAlgebra\Acml\AcmlLinearAlgebraProvider.Complex32.cs" /> <Compile Include="Providers\LinearAlgebra\Acml\AcmlLinearAlgebraProvider.Complex32.cs" />
<Compile Include="Providers\LinearAlgebra\Acml\AcmlLinearAlgebraProvider.Double.cs" /> <Compile Include="Providers\LinearAlgebra\Acml\AcmlLinearAlgebraProvider.Double.cs" />
@ -140,26 +174,6 @@
<Compile Include="Statistics\QuantileDefinition.cs" /> <Compile Include="Statistics\QuantileDefinition.cs" />
<Compile Include="Statistics\StreamingStatistics.cs" /> <Compile Include="Statistics\StreamingStatistics.cs" />
<Compile Include="Statistics\SortedArrayStatistics.cs" /> <Compile Include="Statistics\SortedArrayStatistics.cs" />
<Compile Include="Distributions\Continuous\Cauchy.cs" />
<Compile Include="Distributions\Continuous\Chi.cs" />
<Compile Include="Distributions\Continuous\ChiSquare.cs" />
<Compile Include="Distributions\Continuous\Erlang.cs" />
<Compile Include="Distributions\Continuous\Exponential.cs" />
<Compile Include="Distributions\Continuous\FisherSnedecor.cs" />
<Compile Include="Distributions\Continuous\InverseGamma.cs" />
<Compile Include="Distributions\Continuous\Laplace.cs" />
<Compile Include="Distributions\Continuous\Pareto.cs" />
<Compile Include="Distributions\Continuous\Rayleigh.cs" />
<Compile Include="Distributions\Continuous\Stable.cs" />
<Compile Include="Distributions\Discrete\ConwayMaxwellPoisson.cs" />
<Compile Include="Distributions\Discrete\Geometric.cs" />
<Compile Include="Distributions\Discrete\Hypergeometric.cs" />
<Compile Include="Distributions\Discrete\NegativeBinomial.cs" />
<Compile Include="Distributions\Discrete\Poisson.cs" />
<Compile Include="Distributions\Discrete\Zipf.cs" />
<Compile Include="Distributions\Multivariate\InverseWishart.cs" />
<Compile Include="Distributions\Multivariate\MatrixNormal.cs" />
<Compile Include="Distributions\Multivariate\Wishart.cs" />
<Compile Include="LinearAlgebra\Storage\SparseVectorStorage.cs" /> <Compile Include="LinearAlgebra\Storage\SparseVectorStorage.cs" />
<Compile Include="LinearAlgebra\Storage\DenseVectorStorage.cs" /> <Compile Include="LinearAlgebra\Storage\DenseVectorStorage.cs" />
<Compile Include="LinearAlgebra\Storage\MatrixStorage.Validation.cs" /> <Compile Include="LinearAlgebra\Storage\MatrixStorage.Validation.cs" />
@ -378,21 +392,9 @@
<Compile Include="LinearAlgebra\Storage\VectorStorage.cs" /> <Compile Include="LinearAlgebra\Storage\VectorStorage.cs" />
<Compile Include="LinearAlgebra\Storage\VectorStorage.Validation.cs" /> <Compile Include="LinearAlgebra\Storage\VectorStorage.Validation.cs" />
<Compile Include="Permutation.cs" /> <Compile Include="Permutation.cs" />
<Compile Include="Distributions\Continuous\Beta.cs" />
<Compile Include="Distributions\Continuous\ContinuousUniform.cs" />
<Compile Include="Distributions\Continuous\LogNormal.cs" />
<Compile Include="Distributions\Continuous\Weibull.cs" />
<Compile Include="Distributions\Continuous\Gamma.cs" />
<Compile Include="Distributions\Continuous\Normal.cs" />
<Compile Include="Distributions\Discrete\Bernoulli.cs" />
<Compile Include="Distributions\Discrete\Binomial.cs" />
<Compile Include="Distributions\Discrete\Categorical.cs" />
<Compile Include="Distributions\Discrete\DiscreteUniform.cs" />
<Compile Include="Distributions\IContinuousDistribution.cs" /> <Compile Include="Distributions\IContinuousDistribution.cs" />
<Compile Include="Distributions\IDiscreteDistribution.cs" /> <Compile Include="Distributions\IDiscreteDistribution.cs" />
<Compile Include="Distributions\IDistribution.cs" /> <Compile Include="Distributions\IDistribution.cs" />
<Compile Include="Distributions\Multivariate\Dirichlet.cs" />
<Compile Include="Distributions\Multivariate\Multinomial.cs" />
<Compile Include="IntegralTransforms\Algorithms\DiscreteHartleyTransform.Naive.cs" /> <Compile Include="IntegralTransforms\Algorithms\DiscreteHartleyTransform.Naive.cs" />
<Compile Include="IntegralTransforms\Algorithms\DiscreteHartleyTransform.Options.cs" /> <Compile Include="IntegralTransforms\Algorithms\DiscreteHartleyTransform.Options.cs" />
<Compile Include="IntegralTransforms\HartleyOptions.cs" /> <Compile Include="IntegralTransforms\HartleyOptions.cs" />
@ -475,10 +477,6 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Include="Distributions\Continuous\StudentT.cs" />
<Compile Include="Distributions\Multivariate\NormalGamma.cs" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" /> <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
<Service Include="{B4F97281-0DBD-4835-9ED8-7DFB966E87FF}" /> <Service Include="{B4F97281-0DBD-4835-9ED8-7DFB966E87FF}" />

8
src/Numerics/Statistics/MCMC/HybridMC.cs

@ -28,11 +28,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
namespace MathNet.Numerics.Statistics.Mcmc namespace MathNet.Numerics.Statistics.Mcmc
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
/// <summary> /// <summary>
/// A hybrid Monte Carlo sampler for multivariate distributions. /// A hybrid Monte Carlo sampler for multivariate distributions.

6
src/Numerics/Statistics/MCMC/UnivariateHybridMC.cs

@ -28,10 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.Distributions;
namespace MathNet.Numerics.Statistics.Mcmc namespace MathNet.Numerics.Statistics.Mcmc
{ {
using System; using Random = System.Random;
using Distributions;
/// <summary> /// <summary>
/// A hybrid Monte Carlo sampler for univariate distributions. /// A hybrid Monte Carlo sampler for univariate distributions.

16
src/UnitTests/DistributionTests/CommonDistributionTests.cs

@ -24,15 +24,17 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Random;
using MathNet.Numerics.Statistics;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests namespace MathNet.Numerics.UnitTests.DistributionTests
{ {
using System; using Random = System.Random;
using System.Collections.Generic;
using System.Linq;
using Distributions;
using Numerics.Random;
using NUnit.Framework;
using Statistics;
/// <summary> /// <summary>
/// This class will perform various tests on discrete and continuous univariate distributions. The multivariate distributions /// This class will perform various tests on discrete and continuous univariate distributions. The multivariate distributions

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

@ -24,12 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Beta distribution tests. /// Beta distribution tests.

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Cauchy distribution tests. /// Cauchy distribution tests.
/// </summary> /// </summary>

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

@ -24,12 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Chi square distribution test /// Chi square distribution test

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Chi distribution test /// Chi distribution test
/// </summary> /// </summary>

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

@ -24,12 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Continuous uniform tests. /// Continuous uniform tests.

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Erlang distribution tests. /// Erlang distribution tests.
/// </summary> /// </summary>

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Exponential distribution tests. /// Exponential distribution tests.
/// </summary> /// </summary>

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Fisher-Snedecor distribution tests. /// Fisher-Snedecor distribution tests.
/// </summary> /// </summary>

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

@ -24,12 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Gamma distribution tests. /// Gamma distribution tests.

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Inverse gamma distribution tests. /// Inverse gamma distribution tests.
/// </summary> /// </summary>

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Laplace distribution tests. /// Laplace distribution tests.
/// </summary> /// </summary>

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

@ -24,12 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// <c>LogNormal</c> distribution tests. /// <c>LogNormal</c> distribution tests.

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

@ -24,12 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Normal distribution tests. /// Normal distribution tests.

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Pareto distribution tests. /// Pareto distribution tests.
/// </summary> /// </summary>

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Rayleigh distribution tests. /// Rayleigh distribution tests.
/// </summary> /// </summary>

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

@ -24,13 +24,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Stable distribution tests. /// Stable distribution tests.
/// </summary> /// </summary>

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

@ -24,12 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// <c>StudentT</c> distribution tests. /// <c>StudentT</c> distribution tests.

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

@ -24,12 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Weibull distribution tests. /// Weibull distribution tests.

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

@ -24,12 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.Distributions;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
{ {
using System; using Random = System.Random;
using System.Linq;
using Distributions;
using NUnit.Framework;
/// <summary> /// <summary>
/// Dirichlet distribution tests /// Dirichlet distribution tests

16
src/UnitTests/IntegralTransformsTests/FourierTest.cs

@ -24,15 +24,17 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Numerics;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.IntegralTransforms;
using MathNet.Numerics.IntegralTransforms.Algorithms;
using MathNet.Numerics.Signals;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System; using Random = System.Random;
using System.Numerics;
using Distributions;
using IntegralTransforms;
using IntegralTransforms.Algorithms;
using NUnit.Framework;
using Signals;
/// <summary> /// <summary>
/// Fourier test. /// Fourier test.

16
src/UnitTests/IntegralTransformsTests/HartleyTest.cs

@ -24,15 +24,17 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Numerics;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.IntegralTransforms;
using MathNet.Numerics.IntegralTransforms.Algorithms;
using MathNet.Numerics.Signals;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System; using Random = System.Random;
using System.Numerics;
using Distributions;
using IntegralTransforms;
using IntegralTransforms.Algorithms;
using NUnit.Framework;
using Signals;
/// <summary> /// <summary>
/// Hartley tests. /// Hartley tests.

16
src/UnitTests/IntegralTransformsTests/InverseTransformTest.cs

@ -24,15 +24,17 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Numerics;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.IntegralTransforms;
using MathNet.Numerics.IntegralTransforms.Algorithms;
using MathNet.Numerics.Signals;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System; using Random = System.Random;
using System.Numerics;
using Distributions;
using IntegralTransforms;
using IntegralTransforms.Algorithms;
using NUnit.Framework;
using Signals;
/// <summary> /// <summary>
/// Inverse Transform test. /// Inverse Transform test.

16
src/UnitTests/IntegralTransformsTests/MatchingNaiveTransformTest.cs

@ -24,15 +24,17 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Numerics;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.IntegralTransforms;
using MathNet.Numerics.IntegralTransforms.Algorithms;
using MathNet.Numerics.Signals;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System; using Random = System.Random;
using System.Numerics;
using Distributions;
using IntegralTransforms;
using IntegralTransforms.Algorithms;
using NUnit.Framework;
using Signals;
/// <summary> /// <summary>
/// Matching Naive transform tests. /// Matching Naive transform tests.

19
src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs

@ -24,17 +24,18 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System.Linq;
using System.Numerics;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.IntegralTransforms;
using MathNet.Numerics.IntegralTransforms.Algorithms;
using MathNet.Numerics.Signals;
using MathNet.Numerics.Statistics;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.IntegralTransformsTests namespace MathNet.Numerics.UnitTests.IntegralTransformsTests
{ {
using System; using Random = System.Random;
using System.Linq;
using System.Numerics;
using Distributions;
using IntegralTransforms;
using IntegralTransforms.Algorithms;
using NUnit.Framework;
using Signals;
using Statistics;
/// <summary> /// <summary>
/// Parseval theorem verification tests. /// Parseval theorem verification tests.

6
src/UnitTests/InterpolationTests/LinearInterpolationCase.cs

@ -28,11 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Random;
namespace MathNet.Numerics.UnitTests.InterpolationTests namespace MathNet.Numerics.UnitTests.InterpolationTests
{ {
using Distributions;
using Numerics.Random;
/// <summary> /// <summary>
/// LinearInterpolation test case. /// LinearInterpolation test case.
/// </summary> /// </summary>

4
src/UnitTests/LinearAlgebraProviderTests/Complex/LinearAlgebraProviderTests.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex; using MathNet.Numerics.LinearAlgebra.Complex;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Providers.LinearAlgebra; using MathNet.Numerics.Providers.LinearAlgebra;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex
{ {

4
src/UnitTests/LinearAlgebraProviderTests/Complex32/LinearAlgebraProviderTests.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex32; using MathNet.Numerics.LinearAlgebra.Complex32;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Providers.LinearAlgebra; using MathNet.Numerics.Providers.LinearAlgebra;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex32 namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Complex32
{ {

4
src/UnitTests/LinearAlgebraProviderTests/Double/LinearAlgebraProviderTests.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Providers.LinearAlgebra; using MathNet.Numerics.Providers.LinearAlgebra;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Double namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Double
{ {

4
src/UnitTests/LinearAlgebraProviderTests/Single/LinearAlgebraProviderTests.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.LinearAlgebra.Single; using MathNet.Numerics.LinearAlgebra.Single;
using MathNet.Numerics.Providers.LinearAlgebra; using MathNet.Numerics.Providers.LinearAlgebra;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Single namespace MathNet.Numerics.UnitTests.LinearAlgebraProviderTests.Single
{ {

2
src/UnitTests/LinearAlgebraTests/Complex/MatrixLoader.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex; using MathNet.Numerics.LinearAlgebra.Complex;
using MathNet.Numerics.Random; using MathNet.Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{ {

2
src/UnitTests/LinearAlgebraTests/Complex/MatrixStructureTheory.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System.Linq;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex; using MathNet.Numerics.LinearAlgebra.Complex;
using MathNet.Numerics.Random; using MathNet.Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Linq;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{ {

2
src/UnitTests/LinearAlgebraTests/Complex/MatrixTests.Arithmetic.cs

@ -24,11 +24,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex; using MathNet.Numerics.LinearAlgebra.Complex;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{ {

8
src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex;
using NUnit.Framework;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
{ {

2
src/UnitTests/LinearAlgebraTests/Complex32/MatrixLoader.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex32; using MathNet.Numerics.LinearAlgebra.Complex32;
using MathNet.Numerics.Random; using MathNet.Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ {

2
src/UnitTests/LinearAlgebraTests/Complex32/MatrixStructureTheory.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System.Linq;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex32; using MathNet.Numerics.LinearAlgebra.Complex32;
using MathNet.Numerics.Random; using MathNet.Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Linq;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ {

2
src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.Arithmetic.cs

@ -24,11 +24,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex32; using MathNet.Numerics.LinearAlgebra.Complex32;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ {

8
src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex32;
using NUnit.Framework;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex32;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
{ {

2
src/UnitTests/LinearAlgebraTests/Double/MatrixLoader.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.Random; using MathNet.Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {

2
src/UnitTests/LinearAlgebraTests/Double/MatrixStructureTheory.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System.Linq;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.Random; using MathNet.Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Linq;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {

2
src/UnitTests/LinearAlgebraTests/Double/MatrixTests.Arithmetic.cs

@ -24,11 +24,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {

6
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -28,13 +28,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections;
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{ {

2
src/UnitTests/LinearAlgebraTests/Single/MatrixLoader.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Single; using MathNet.Numerics.LinearAlgebra.Single;
using MathNet.Numerics.Random; using MathNet.Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {

2
src/UnitTests/LinearAlgebraTests/Single/MatrixStructureTheory.cs

@ -28,12 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System.Linq;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Single; using MathNet.Numerics.LinearAlgebra.Single;
using MathNet.Numerics.Random; using MathNet.Numerics.Random;
using NUnit.Framework; using NUnit.Framework;
using System.Linq;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {

12
src/UnitTests/LinearAlgebraTests/Single/MatrixTests.Arithmetic.cs

@ -24,14 +24,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Single;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {
using Distributions;
using LinearAlgebra;
using LinearAlgebra.Single;
using NUnit.Framework;
using System;
/// <summary> /// <summary>
/// Abstract class with the common set of matrix tests /// Abstract class with the common set of matrix tests
/// </summary> /// </summary>

6
src/UnitTests/LinearAlgebraTests/Single/VectorTests.cs

@ -28,13 +28,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections;
using System.Collections.Generic;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Single; using MathNet.Numerics.LinearAlgebra.Single;
using NUnit.Framework; using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
{ {

5
src/UnitTests/StatisticsTests/MCMCTests/HybridMCTest.cs

@ -29,10 +29,11 @@
// </copyright> // </copyright>
using System; using System;
using NUnit.Framework; using MathNet.Numerics.Distributions;
using MathNet.Numerics.Statistics; using MathNet.Numerics.Statistics;
using MathNet.Numerics.Statistics.Mcmc; using MathNet.Numerics.Statistics.Mcmc;
using MathNet.Numerics.Statistics.Mcmc.Diagnostics; using MathNet.Numerics.Statistics.Mcmc.Diagnostics;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
{ {
@ -42,7 +43,7 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
[TestFixture] [TestFixture]
public class HybridMCTest public class HybridMCTest
{ {
private readonly Distributions.Normal _normal = new Distributions.Normal(0, 1); private readonly Normal _normal = new Normal(0, 1);
/// <summary> /// <summary>
/// Testing the constructor to make sure that RandomSource is /// Testing the constructor to make sure that RandomSource is

4
src/UnitTests/StatisticsTests/MCMCTests/MCMCDiagnosticsTest.cs

@ -29,10 +29,10 @@
// </copyright> // </copyright>
using System; using System;
using NUnit.Framework;
using MathNet.Numerics.Statistics;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using MathNet.Numerics.Statistics;
using MathNet.Numerics.Statistics.Mcmc.Diagnostics; using MathNet.Numerics.Statistics.Mcmc.Diagnostics;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
{ {

14
src/UnitTests/StatisticsTests/MCMCTests/MetropolisHastingsSamplerTests.cs

@ -28,13 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Random;
using MathNet.Numerics.Statistics.Mcmc;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
{ {
using System; using Random = System.Random;
using Distributions;
using Numerics.Random;
using NUnit.Framework;
using Statistics.Mcmc;
/// <summary> /// <summary>
/// Metropolis hastings sampler tests. /// Metropolis hastings sampler tests.
@ -57,7 +59,7 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
}; };
Assert.IsNotNull(ms.RandomSource); Assert.IsNotNull(ms.RandomSource);
ms.RandomSource = new Random(); ms.RandomSource = new System.Random();
Assert.IsNotNull(ms.RandomSource); Assert.IsNotNull(ms.RandomSource);
} }

12
src/UnitTests/StatisticsTests/MCMCTests/MetropolisSamplerTests.cs

@ -28,13 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Random;
using MathNet.Numerics.Statistics.Mcmc;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
{ {
using System; using Random = System.Random;
using Distributions;
using Numerics.Random;
using NUnit.Framework;
using Statistics.Mcmc;
/// <summary> /// <summary>
/// Metropolis sampler tests. /// Metropolis sampler tests.

12
src/UnitTests/StatisticsTests/MCMCTests/RejectionSamplerTests.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Random;
using MathNet.Numerics.Statistics.Mcmc;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
{ {
using System;
using Distributions;
using Numerics.Random;
using NUnit.Framework;
using Statistics.Mcmc;
/// <summary> /// <summary>
/// Rejection sampler tests. /// Rejection sampler tests.
/// </summary> /// </summary>

5
src/UnitTests/StatisticsTests/MCMCTests/UnivariateHybridMCTest.cs

@ -30,14 +30,13 @@
using System; using System;
using MathNet.Numerics.Distributions; using MathNet.Numerics.Distributions;
using NUnit.Framework; using MathNet.Numerics.Statistics;
using MathNet.Numerics.Statistics.Mcmc; using MathNet.Numerics.Statistics.Mcmc;
using MathNet.Numerics.Statistics.Mcmc.Diagnostics; using MathNet.Numerics.Statistics.Mcmc.Diagnostics;
using MathNet.Numerics.Statistics; using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
{ {
/// <summary> /// <summary>
/// Test for the UnivariateHybridMC. /// Test for the UnivariateHybridMC.
/// </summary> /// </summary>

13
src/UnitTests/StatisticsTests/StatisticsTests.cs

@ -28,15 +28,16 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Random; using MathNet.Numerics.Random;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.StatisticsTests namespace MathNet.Numerics.UnitTests.StatisticsTests
{ {
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Statistics; using Statistics;
/// <summary> /// <summary>
@ -565,7 +566,7 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests
public void StabilityMeanVariance() public void StabilityMeanVariance()
{ {
// Test around 10^9, potential stability issues // Test around 10^9, potential stability issues
var gaussian = new Distributions.Normal(1e+9, 2, new MersenneTwister(100)); var gaussian = new Normal(1e+9, 2, new MersenneTwister(100));
AssertHelpers.AlmostEqual(1e+9, Statistics.Mean(gaussian.Samples().Take(10000)), 11); AssertHelpers.AlmostEqual(1e+9, Statistics.Mean(gaussian.Samples().Take(10000)), 11);
AssertHelpers.AlmostEqual(4d, Statistics.Variance(gaussian.Samples().Take(10000)), 1); AssertHelpers.AlmostEqual(4d, Statistics.Variance(gaussian.Samples().Take(10000)), 1);

Loading…
Cancel
Save