Browse Source

Verified Distributions, added several properties, fixed several properties.

pull/36/head
Abratiychuk 16 years ago
committed by Marcus Cuda
parent
commit
e21559ac10
  1. 2
      src/Numerics/Distributions/Continuous/Beta.cs
  2. 2
      src/Numerics/Distributions/Continuous/ChiSquare.cs
  3. 34
      src/Numerics/Distributions/Continuous/Exponential.cs
  4. 2
      src/Numerics/Distributions/Continuous/FisherSnedecor.cs
  5. 6
      src/Numerics/Distributions/Continuous/InverseGamma.cs
  6. 7
      src/Numerics/Distributions/Continuous/Pareto.cs
  7. 40
      src/Numerics/Distributions/Continuous/Stable.cs
  8. 3
      src/Numerics/Distributions/Continuous/Weibull.cs
  9. 2
      src/Numerics/Distributions/Discrete/Bernoulli.cs
  10. 2
      src/Numerics/Distributions/Discrete/Binomial.cs
  11. 12
      src/Numerics/Distributions/Discrete/ConwayMaxwellPoisson.cs
  12. 7
      src/Numerics/Distributions/Discrete/Geometric.cs
  13. 28
      src/Numerics/Distributions/Discrete/NegativeBinomial.cs
  14. 26
      src/Numerics/Distributions/Multivariate/Multinomial.cs
  15. 49
      src/Numerics/Distributions/Multivariate/NormalGamma.cs
  16. 2
      src/UnitTests/DistributionTests/Continuous/BetaTests.cs
  17. 2
      src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs
  18. 2
      src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs
  19. 6
      src/UnitTests/DistributionTests/Continuous/InverseGammaTests.cs
  20. 9
      src/UnitTests/DistributionTests/Continuous/ParetoTests.cs
  21. 4
      src/UnitTests/DistributionTests/Continuous/StableTests.cs
  22. 2
      src/UnitTests/DistributionTests/Discrete/BernoulliTests.cs
  23. 2
      src/UnitTests/DistributionTests/Discrete/BinomialTests.cs
  24. 15
      src/UnitTests/DistributionTests/Discrete/GeometricTests.cs
  25. 31
      src/UnitTests/DistributionTests/Discrete/NegativeBinomialTests.cs
  26. 25
      src/UnitTests/DistributionTests/Multivariate/MultinomialTests.cs
  27. 9
      src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs

2
src/Numerics/Distributions/Continuous/Beta.cs

@ -363,7 +363,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotImplementedException();
throw new NotSupportedException();
}
}

2
src/Numerics/Distributions/Continuous/ChiSquare.cs

@ -184,7 +184,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaUpperIncomplete(Mean / 2.0, x / 2.0) / SpecialFunctions.Gamma(Mean / 2.0);
return SpecialFunctions.GammaLowerIncomplete(Mean / 2.0, x / 2.0) / SpecialFunctions.Gamma(Mean / 2.0);
}
#endregion

34
src/Numerics/Distributions/Continuous/Exponential.cs

@ -310,6 +310,40 @@ namespace MathNet.Numerics.Distributions
}
}
/// <summary>
/// Draws a random sample from the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <returns>A random number from this distribution.</returns>
public static double Sample(Random rnd, double lambda)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
return DoSample(rnd, lambda);
}
/// <summary>
/// Generates a sequence of samples from the Exponential distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="lambda">The lambda parameter of the Exponential distribution.</param>
/// <returns>a sequence of samples from the distribution.</returns>
public static IEnumerable<double> Samples(Random rnd, double lambda)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(lambda))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
while (true)
{
yield return DoSample(rnd, lambda);
}
}
#endregion
/// <summary>

2
src/Numerics/Distributions/Continuous/FisherSnedecor.cs

@ -249,7 +249,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.BetaRegularized(_d1 / 2.0, _d2 / 2.0, _d1 * _d2 / (_d1 + (_d1 * _d2)));
return SpecialFunctions.BetaRegularized(_d1 / 2.0, _d2 / 2.0, _d1 * x / ((_d1 * x) + _d2));
}
#endregion

6
src/Numerics/Distributions/Continuous/InverseGamma.cs

@ -259,7 +259,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
return SpecialFunctions.GammaLowerIncomplete(_shape, _scale / x);
return SpecialFunctions.GammaUpperRegularized(_shape, _scale / x);
}
#endregion
@ -280,12 +280,12 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets the median of the distribution.
/// </summary>
/// <remarks>Throws <see cref="NotImplementedException"/>.</remarks>
/// <remarks>Throws <see cref="NotSupportedException"/>.</remarks>
public double Median
{
get
{
throw new NotImplementedException();
throw new NotSupportedException();
}
}

7
src/Numerics/Distributions/Continuous/Pareto.cs

@ -201,7 +201,12 @@ namespace MathNet.Numerics.Distributions
{
get
{
return _scale * _scale * _shape / ((_shape - 1.0) * (_shape - 1.0) * (_scale - 2.0));
if (_shape <= 2.0)
{
return double.PositiveInfinity;
}
return _scale * _scale * _shape / ((_shape - 1.0) * (_shape - 1.0) * (_shape - 2.0));
}
}

40
src/Numerics/Distributions/Continuous/Stable.cs

@ -481,7 +481,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>A random number from this distribution.</returns>
public double Sample()
{
throw new NotImplementedException();
return DoSample(RandomSource, _alpha, _beta);
}
/// <summary>
@ -490,9 +490,45 @@ namespace MathNet.Numerics.Distributions
/// <returns>a sequence of samples from the distribution.</returns>
public IEnumerable<double> Samples()
{
throw new NotImplementedException();
while (true)
{
yield return DoSample(RandomSource, _alpha, _beta);
}
}
/// <summary>
/// Samples the distribution.
/// </summary>
/// <param name="rnd">The random number generator to use.</param>
/// <param name="alpha">The stability parameter of the distribution.</param>
/// <param name="beta">The skewness parameter of the distribution.</param>
/// <returns>a random number from the distribution.</returns>
private static double DoSample(Random rnd, double alpha, double beta)
{
var randTheta = ContinuousUniform.Sample(rnd, -Constants.PiOver2, Constants.PiOver2);
var randW = Exponential.Sample(rnd, 1.0);
if (!1.0.AlmostEqual(alpha))
{
var theta = (1.0 / alpha) * Math.Atan(beta * Math.Tan(Constants.PiOver2 * alpha));
var angle = alpha * (randTheta + theta);
var part1 = beta * Math.Tan(Constants.PiOver2 * alpha);
var factor = Math.Pow(1.0 + (part1 * part1), 1.0 / (2.0 * alpha));
var factor1 = Math.Sin(angle) / Math.Pow(Math.Cos(randTheta), (1.0 / alpha));
var factor2 = Math.Pow(Math.Cos(randTheta - angle) / randW, (1 - alpha) / alpha);
return factor * factor1 * factor2;
}
else
{
var part1 = Constants.PiOver2 + (beta * randTheta);
var summand = part1 * Math.Tan(randTheta);
var subtrahend = beta * Math.Log(Constants.PiOver2 * randW * Math.Cos(randTheta) / part1);
return (2.0 / Math.PI) * (summand - subtrahend);
}
}
#endregion
}
}

3
src/Numerics/Distributions/Continuous/Weibull.cs

@ -216,12 +216,11 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Gets the entropy of the Weibull distribution.
/// </summary>
/// <remarks>Throws a not supported exception.</remarks>
public double Entropy
{
get
{
throw new NotSupportedException();
return (Constants.EulerMascheroni * (1.0 - (1.0 / _shape))) + Math.Log(_scale / _shape) + 1.0;
}
}

2
src/Numerics/Distributions/Discrete/Bernoulli.cs

@ -259,7 +259,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new Exception("The median of the Bernoulli distribution is undefined.");
throw new NotSupportedException("The median of the Bernoulli distribution is undefined.");
}
}

2
src/Numerics/Distributions/Discrete/Binomial.cs

@ -320,7 +320,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotImplementedException();
return (int)Math.Floor(_p * _n);
}
}

12
src/Numerics/Distributions/Discrete/ConwayMaxwellPoisson.cs

@ -346,7 +346,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotImplementedException();
throw new NotSupportedException();
}
}
@ -357,7 +357,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotImplementedException();
throw new NotSupportedException();
}
}
@ -388,7 +388,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotImplementedException();
throw new NotSupportedException();
}
}
@ -399,7 +399,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotImplementedException();
throw new NotSupportedException();
}
}
@ -421,7 +421,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotImplementedException();
throw new NotSupportedException();
}
}
@ -488,7 +488,7 @@ namespace MathNet.Numerics.Distributions
_z = Normalization(_lambda, _nu);
return _z;
}
}
}
/// <summary>

7
src/Numerics/Distributions/Discrete/Geometric.cs

@ -195,7 +195,8 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotSupportedException();
return ((2.0 - _p) / Math.Sqrt(1.0 - _p));
}
}
@ -231,7 +232,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
return (int)Math.Ceiling(-Math.Log(2.0) / Math.Log(1 - _p));
return (int)Math.Ceiling(-Constants.Ln2 / Math.Log(1 - _p));
}
}
@ -253,7 +254,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotSupportedException();
return int.MaxValue;
}
}

28
src/Numerics/Distributions/Discrete/NegativeBinomial.cs

@ -59,7 +59,7 @@ namespace MathNet.Numerics.Distributions
private Random _random;
/// <summary>
/// Gets or sets the r parameter.
/// Gets or sets the number of trials.
/// </summary>
public double R
{
@ -75,7 +75,7 @@ namespace MathNet.Numerics.Distributions
}
/// <summary>
/// Gets or sets the p parameter.
/// Gets or sets the probability of success.
/// </summary>
public double P
{
@ -93,12 +93,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Initializes a new instance of the <see cref="NegativeBinomial"/> class.
/// </summary>
/// <param name="r">
/// The r parameter.
/// </param>
/// <param name="p">
/// The p parameter.
/// </param>
/// <param name="r">The number of trials.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
public NegativeBinomial(double r, double p)
{
SetParameters(r, p);
@ -108,8 +104,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Sets the parameters of the distribution after checking their validity.
/// </summary>
/// <param name="r">The r parameter.</param>
/// <param name="p">The p parameter.</param>
/// <param name="r">The number of trials.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters(double r, double p)
{
@ -125,8 +121,8 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Checks whether the parameters of the distribution are valid.
/// </summary>
/// <param name="r">The r parameter.</param>
/// <param name="p">The p parameter.</param>
/// <param name="r">The number of trials.</param>
/// <param name="p">The probability of a trial resulting in success.</param>
/// <returns><c>true</c> when the parameters are valid, <c>false</c> otherwise.</returns>
private static bool IsValidParameterSet(double r, double p)
{
@ -217,7 +213,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new Exception("Not implemented yet.");
throw new NotSupportedException();
}
}
@ -239,7 +235,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the cumulative density at <paramref name="x"/>.</returns>
public double CumulativeDistribution(double x)
{
throw new NotImplementedException();
return 1 - SpecialFunctions.BetaRegularized(x + 1, _r, 1 - _p);
}
#endregion
@ -264,7 +260,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new Exception("Not implemented yet.");
throw new NotSupportedException();
}
}
@ -286,7 +282,7 @@ namespace MathNet.Numerics.Distributions
{
get
{
throw new NotImplementedException();
return int.MaxValue;
}
}

26
src/Numerics/Distributions/Multivariate/Multinomial.cs

@ -29,6 +29,7 @@ namespace MathNet.Numerics.Distributions
using System;
using System.Collections.Generic;
using Properties;
using Statistics;
/// <summary>
/// Implements the multinomial distribution. For details about this distribution, see
@ -73,24 +74,31 @@ namespace MathNet.Numerics.Distributions
RandomSource = new Random();
}
/* TODO
/// <summary>
/// Generate a multinomial distribution from histogram <paramref name="h"/>. The distribution will
/// Initializes a new instance of the Multinomial class from histogram <paramref name="h"/>. The distribution will
/// not be automatically updated when the histogram changes.
/// </summary>
public Multinomial(Histogram h)
/// <param name="h">Histogram instance</param>
/// <param name="n">The number of trials.</param>
public Multinomial(Histogram h, int n)
{
if (h == null)
{
throw new ArgumentNullException("h");
}
// The probability distribution vector.
_p = new double[h.BinCount];
var p = new double[h.BucketCount];
// Fill in the distribution vector.
for (int i = 0; i < h.BinCount; i++)
for (var i = 0; i < h.BucketCount; i++)
{
_p[i] = h[i];
p[i] = h[i].Count;
}
RandomNumberGenerator = new System.Random();
}*/
SetParameters(p, n);
RandomSource = new Random();
}
/// <summary>
/// A string representation of the distribution.
@ -240,7 +248,7 @@ namespace MathNet.Numerics.Distributions
// The cumulative density of p.
var cp = Categorical.UnnormalizedCdf(p);
// The variable that stores the counts.
// The variable that stores the counts.
var ret = new int[p.Length];
for (var i = 0; i < n; i++)

49
src/Numerics/Distributions/Multivariate/NormalGamma.cs

@ -329,11 +329,11 @@ namespace MathNet.Numerics.Distributions
}
}
/*
/// <summary>
/// Evaluates the probability density function for a NormalGamma distribution.
/// </summary>
/// <param name="mp">The mean/precision pair of the distribution</param>
/// <returns>Density value</returns>
public double Density(MeanPrecisionPair mp)
{
return Density(mp.Mean, mp.Precision);
@ -342,31 +342,38 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Evaluates the probability density function for a NormalGamma distribution.
/// </summary>
/// <param name="mean">The mean of the distribution</param>
/// <param name="prec">The precision of the distribution</param>
/// <returns>Density value</returns>
public double Density(double mean, double prec)
{
if (Double.IsPositiveInfinity(_precisionInvScale) && _meanScale == 0.0)
{
throw new NotImplementedException();
}
else if (Double.IsPositiveInfinity(_precisionInvScale))
if (Double.IsPositiveInfinity(_precisionInvScale))
{
throw new NotImplementedException();
}
else if (_meanScale == 0.0)
if (_meanScale <= 0.0)
{
throw new NotImplementedException();
}
else
{
double e = -0.5 * prec * (mean - _meanLocation) * (mean - _meanLocation) - prec * _precisionInvScale;
return System.Math.Pow(prec * _precisionInvScale, _precisionShape) * System.Math.Exp(e)
/ (Math.Constants.Sqrt2Pi * System.Math.Sqrt(prec) * Math.SpecialFunctions.Gamma(_precisionShape));
}
// double e = -0.5 * prec * (mean - _meanLocation) * (mean - _meanLocation) - prec * _precisionInvScale;
// 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);
return Math.Pow(prec * _precisionInvScale, _precisionShape) * Math.Exp(e) * Math.Sqrt(_meanScale)
/ (Constants.Sqrt2Pi * Math.Sqrt(prec) * SpecialFunctions.Gamma(_precisionShape));
}
/// <summary>
/// Evaluates the log probability density function for a NormalGamma distribution.
/// </summary>
/// <param name="mp">The mean/precision pair of the distribution</param>
/// <returns>The log of the density value</returns>
public double DensityLn(MeanPrecisionPair mp)
{
return DensityLn(mp.Mean, mp.Precision);
@ -375,27 +382,31 @@ namespace MathNet.Numerics.Distributions
/// <summary>
/// Evaluates the log probability density function for a NormalGamma distribution.
/// </summary>
/// <param name="mean">The mean of the distribution</param>
/// <param name="prec">The precision of the distribution</param>
/// <returns>The log of the density value</returns>
public double DensityLn(double mean, double prec)
{
if (Double.IsPositiveInfinity(_precisionInvScale) && _meanScale == 0.0)
{
throw new NotImplementedException();
}
else if (Double.IsPositiveInfinity(_precisionInvScale))
if (Double.IsPositiveInfinity(_precisionInvScale))
{
throw new NotImplementedException();
}
else if (_meanScale == 0.0)
if (_meanScale <= 0.0)
{
throw new NotImplementedException();
}
else
{
double e = -0.5 * prec * (mean - _meanLocation) * (mean - _meanLocation) - prec * _precisionInvScale;
return (_precisionShape - 0.5) * System.Math.Log(prec) + _precisionShape * System.Math.Log(_precisionInvScale) + e
- Math.Constants.LogSqrt2Pi - Math.SpecialFunctions.GammaLn(_precisionShape);
}
}*/
// double e = -0.5 * prec * (mean - _meanLocation) * (mean - _meanLocation) - prec * _precisionInvScale;
// return (_precisionShape - 0.5) * Math.Log(prec) + _precisionShape * Math.Log(_precisionInvScale) + e - Constants.LogSqrt2Pi - SpecialFunctions.GammaLn(_precisionShape);
double e = -(0.5 * prec * _meanScale * (mean - _meanLocation) * (mean - _meanLocation)) - (prec * _precisionInvScale);
return ((_precisionShape - 0.5) * Math.Log(prec)) + (_precisionShape * Math.Log(_precisionInvScale)) - (0.5 * Math.Log(_meanScale)) + e - Constants.LogSqrt2Pi - SpecialFunctions.GammaLn(_precisionShape);
}
/// <summary>
/// Generates a sample from the <c>NormalGamma</c> distribution.

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

@ -193,7 +193,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
}
[Test]
[ExpectedException(typeof(NotImplementedException))]
[ExpectedException(typeof(NotSupportedException))]
[Row(0.0, 0.0)]
[Row(0.0, 0.1)]
[Row(1.0, 0.0)]

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

@ -289,7 +289,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateCumulativeDistribution(double dof, double x)
{
var n = new ChiSquare(dof);
Assert.AreEqual<double>(SpecialFunctions.GammaUpperIncomplete(dof / 2.0, x / 2.0) / SpecialFunctions.Gamma(dof / 2.0), n.CumulativeDistribution(x));
Assert.AreEqual(SpecialFunctions.GammaLowerIncomplete(dof / 2.0, x / 2.0) / SpecialFunctions.Gamma(dof / 2.0), n.CumulativeDistribution(x));
}
}
}

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

@ -365,7 +365,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateCumulativeDistribution(double d1, double d2, double x)
{
var n = new FisherSnedecor(d1, d2);
Assert.AreEqual<double>(SpecialFunctions.BetaRegularized(d1 / 2.0, d2 / 2.0, d1 * d2 / (d1 + d1 * d2)), n.CumulativeDistribution(x));
Assert.AreEqual<double>(SpecialFunctions.BetaRegularized(d1 / 2.0, d2 / 2.0, d1 * x / (d2 + x * d1)), n.CumulativeDistribution(x));
}
}
}

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

@ -82,7 +82,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateToString()
{
var n = new InverseGamma(1.1, 2.1);
Assert.AreEqual<string>("InverseGamma(Shape = 1.1, Inverse Scale = 2.1)", n.ToString());
Assert.AreEqual(String.Format("InverseGamma(Shape = {0}, Inverse Scale = {1})", n.Shape, n.Scale), n.ToString());
}
[Test]
@ -203,7 +203,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
}
[Test]
[ExpectedException(typeof(NotImplementedException))]
[ExpectedException(typeof(NotSupportedException))]
public void ValidateMedian()
{
var n = new InverseGamma(1.0, 1.0);
@ -291,7 +291,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateCumulativeDistribution(double a, double b, double x)
{
var n = new InverseGamma(a, b);
Assert.AreEqual<double>(SpecialFunctions.GammaLowerIncomplete(a, b / x), n.CumulativeDistribution(x));
Assert.AreEqual<double>(SpecialFunctions.GammaUpperRegularized(a, b / x), n.CumulativeDistribution(x));
}
}
}

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

@ -169,7 +169,14 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateVariance(double scale, double shape)
{
var n = new Pareto(scale, shape);
Assert.AreEqual<double>(scale * scale * shape / ((shape - 1.0) * (shape - 1.0) * (scale - 2.0)), n.Variance);
if (shape <= 2.0)
{
Assert.AreEqual(double.PositiveInfinity, n.Variance);
}
else
{
Assert.AreEqual(scale * scale * shape / ((shape - 1.0) * (shape - 1.0) * (shape - 2.0)), n.Variance);
}
}
[Test]

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

@ -108,7 +108,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateToString()
{
var n = new Stable(1.2, 0.3, 1.0, 2.0);
Assert.AreEqual<string>("Stable(Stability = 1.2, Skewness = 0.3, Scale = 1, Location = 2)", n.ToString());
Assert.AreEqual(String.Format("Stable(Stability = {0}, Skewness = {1}, Scale = {2}, Location = {3})",n.Alpha, n.Beta, n.Scale, n.Location) , n.ToString());
}
[Test]
@ -319,7 +319,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
}
[Test]
[ExpectedException(typeof(NotImplementedException))]
public void CanSample()
{
var n = new Stable(1.0, 1.0, 1.0, 1.0);
@ -327,7 +326,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
}
[Test]
[ExpectedException(typeof(NotImplementedException))]
public void CanSampleSequence()
{
var n = new Stable(1.0, 1.0, 1.0, 1.0);

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

@ -68,7 +68,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
public void ValidateToString()
{
var b = new Bernoulli(0.3);
Assert.AreEqual<string>("Bernoulli(P = 0.3)", b.ToString());
Assert.AreEqual(String.Format("Bernoulli(P = {0})", b.P), b.ToString());
}
[Test]

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

@ -69,7 +69,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
public void ValidateToString()
{
var b = new Binomial(0.3, 2);
Assert.AreEqual<string>("Binomial(Success Probability = 0.3, Number of Trials = 2)", b.ToString());
Assert.AreEqual(String.Format("Binomial(Success Probability = {0}, Number of Trials = {1})", b.P, b.N), b.ToString());
}
[Test]

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

@ -68,7 +68,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
public void ValidateToString()
{
var d = new Geometric(0.3);
Assert.AreEqual<string>("Geometric(P = 0.3)", d.ToString());
Assert.AreEqual(String.Format("Geometric(P = {0})",d.P), d.ToString());
}
[Test]
@ -103,11 +103,13 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
}
[Test]
[ExpectedException(typeof(NotSupportedException))]
public void ValidateSkewness()
[Row(0.0)]
[Row(0.3)]
[Row(1.0)]
public void ValidateSkewness(double p)
{
var d = new Geometric(0.3);
double s = d.Skewness;
var d = new Geometric(p);
Assert.AreEqual((2.0 - p) / Math.Sqrt(1.0 - p), d.Skewness);
}
[Test]
@ -138,11 +140,10 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
}
[Test]
[ExpectedException(typeof(NotSupportedException))]
public void ValidateMaximum()
{
var d = new Geometric(0.3);
int max = d.Maximum;
Assert.AreEqual(int.MaxValue, d.Maximum);
}
[Test]

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

@ -80,7 +80,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
public void ValidateToString()
{
var d = new NegativeBinomial(1.0, 0.3);
Assert.AreEqual<string>("NegativeBinomial(R = 1, P = 0.3)", d.ToString());
Assert.AreEqual(String.Format("NegativeBinomial(R = {0}, P = {1})", d.R, d.P), d.ToString());
}
[Test]
@ -178,15 +178,14 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
public void ValidateMinimum()
{
var d = new NegativeBinomial(1.0, 0.5);
Assert.AreEqual<double>(0, d.Minimum);
Assert.AreEqual(0, d.Minimum);
}
[Test]
[ExpectedException(typeof(Exception))]
public void ValidateMaximum()
{
var d = new NegativeBinomial(1.0, 0.3);
int max = d.Maximum;
Assert.AreEqual(int.MaxValue, d.Maximum);
}
[Test]
@ -221,6 +220,22 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
Assert.AreEqual(SpecialFunctions.GammaLn(r + x) - SpecialFunctions.GammaLn(r) - SpecialFunctions.GammaLn(x + 1.0) + r * Math.Log(p) + x * Math.Log(1.0 - p), d.ProbabilityLn(x));
}
[Test]
[Row(0.0, 0.0, 5)]
[Row(0.0, 0.3, 3)]
[Row(0.0, 1.0, 0)]
[Row(0.1, 0.0, 2)]
[Row(0.1, 0.3, 1)]
[Row(0.1, 1.0, 2)]
[Row(1.0, 0.0, 2)]
[Row(1.0, 0.3, 10)]
[Row(1.0, 1.0, 5)]
public void ValidateCumulativeDistribution(double r, double p, int x)
{
var d = new NegativeBinomial(r, p);
Assert.AreApproximatelyEqual(SpecialFunctions.BetaRegularized(r, x + 1.0, p), d.CumulativeDistribution(x), 1e-12);
}
[Test]
public void CanSample()
{
@ -235,13 +250,5 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
var ied = d.Samples();
var e = ied.Take(5).ToArray();
}
[Test]
[ExpectedException(typeof(NotImplementedException))]
public void ValidateCumulativeDistribution()
{
var d = new NegativeBinomial(1.0, 0.5);
var cdx = d.CumulativeDistribution(1.5);
}
}
}

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

@ -33,8 +33,9 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
using System;
using MbUnit.Framework;
using Distributions;
using Statistics;
[TestFixture]
[TestFixture]
public class MultinomialTests
{
double[] badP;
@ -59,6 +60,28 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
Assert.AreEqual<double[]>(largeP, m.P);
}
[Test]
[MultipleAsserts]
public void CanCreateMultinomialFromHistogram()
{
double[] smallDataset = { 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5 };
var hist = new Histogram(smallDataset, 10, 0.0, 10.0);
var m = new Multinomial(hist, 7);
foreach (var t in m.P)
{
Assert.AreEqual(1.0, t);
}
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void MultinomialCreateFailsWithNullHistogram()
{
Histogram h = null;
var m = new Categorical(h);
}
[Test]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void MultinomialCreateFailsWithNegativeRatios()

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

@ -52,6 +52,15 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate
Assert.AreEqual<double>(precInvScale, ng.PrecisionInverseScale);
}
[Test, MultipleAsserts]
[Row(0.0, 1.0, 1.0, 1.0)]
[Row(10.0, 1.0, 2.0, 2.0)]
public void CanGetDensityAndDensityLn(double meanLocation, double meanScale, double precShape, double precInvScale)
{
var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale);
Assert.AreEqual(ng.DensityLn(meanLocation, precShape), Math.Log(ng.Density(meanLocation, precShape)));
}
[Test]
[Row(1.0, -1.3, 2.0, 2.0)]
[Row(1.0, 1.0, -1.0, 1.0)]

Loading…
Cancel
Save