Browse Source

Removed redundant parameters in Dirichlet distribution unit tests.

Added unit tests for the NormalGamma distribution.
Expect different exception in Beta distribution unit tests.
la-knuth
Jurgen Van Gael 17 years ago
parent
commit
35f24d1a4c
  1. 64
      src/Numerics/Distributions/Multivariate/NormalGamma.cs
  2. 2
      src/UnitTests/DistributionTests/Continuous/BetaTests.cs
  3. 6
      src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs
  4. 96
      src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs

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

@ -201,6 +201,7 @@ namespace MathNet.Numerics.Distributions
public double PrecisionShape
{
get { return _precisionShape; }
set { SetParameters(_meanLocation, _meanScale, value, _precisionInvScale); }
}
/// <summary>
@ -209,6 +210,28 @@ namespace MathNet.Numerics.Distributions
public double PrecisionInverseScale
{
get { return _precisionInvScale; }
set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); }
}
/// <summary>
/// Gets or sets the random number generator which is used to draw random samples.
/// </summary>
public Random RandomSource
{
get
{
return _random;
}
set
{
if (value == null)
{
throw new ArgumentNullException();
}
_random = value;
}
}
/// <summary>
@ -217,7 +240,14 @@ namespace MathNet.Numerics.Distributions
/// <returns></returns>
public StudentT MeanMarginal()
{
return new StudentT(_meanLocation, _meanScale * _precisionShape / _precisionInvScale, 2.0 * _precisionShape);
if (Double.IsPositiveInfinity(_precisionInvScale))
{
return new StudentT(_meanLocation, _meanScale * _precisionShape, Double.PositiveInfinity);
}
else
{
return new StudentT(_meanLocation, _meanScale * _precisionShape / _precisionInvScale, 2.0 * _precisionShape);
}
}
/// <summary>
@ -229,7 +259,6 @@ namespace MathNet.Numerics.Distributions
return new Gamma(_precisionShape, _precisionInvScale);
}
/*
/// <summary>
/// Gets the mean of the distribution.
/// </summary>
@ -249,11 +278,7 @@ namespace MathNet.Numerics.Distributions
}
}
/// <summary>
/// Gets or sets the random number generator.
/// </summary>
/// <value>The random number generator used to generate a random sample.</value>
public System.Random RandomNumberGenerator { get; set; }
/*
/// <summary>
/// The mode of the distribution.
@ -378,31 +403,6 @@ namespace MathNet.Numerics.Distributions
return NormalGamma.Sample(RandomNumberGenerator, size, _meanLocation, _meanScale, _precisionShape, _precisionInvScale);
}
/// <summary>
/// Checks the parameters of a NormalGamma distribution.
/// </summary>
/// <param name="meanScale">The scale of the mean.</param>
/// <param name="precShape">The shape of the precision.</param>
/// <param name="precInvScale">The inverse scale of the precision.</param>
/// <exception cref="ArgumentOutOfRangeException">If the mean scale is negative.</exception>
/// <exception cref="ArgumentOutOfRangeException">If the inverse precision scale is negative.</exception>
/// <exception cref="ArgumentOutOfRangeException">If the precision shape is negative.</exception>
private static void CheckParameters(double meanScale, double precShape, double precInvScale)
{
if (meanScale < 0.0)
{
throw new ArgumentOutOfRangeException("meanScale", Resources.ParameterCannotBeNegative);
}
else if (precShape <= 0.0)
{
throw new ArgumentOutOfRangeException("precShape", Resources.ParameterCannotBeNegative);
}
else if (precInvScale <= 0.0)
{
throw new ArgumentOutOfRangeException("precInvScale", Resources.ParameterCannotBeNegative);
}
}
/// <summary>
/// Samples an array of NormalGamma distributed random variables.
/// </summary>

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

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

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

@ -80,14 +80,14 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
}
[Test]
public void HasRandomSource(int i)
public void HasRandomSource()
{
Dirichlet d = new Dirichlet(0.3, 5);
Assert.IsNotNull(d.RandomSource);
}
[Test]
public void CanSetRandomSource(int i)
public void CanSetRandomSource()
{
Dirichlet d = new Dirichlet(0.3, 5);
d.RandomSource = new Random();
@ -95,7 +95,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void FailSetRandomSourceWithNullReference(int i)
public void FailSetRandomSourceWithNullReference()
{
Dirichlet d = new Dirichlet(0.3, 5);
d.RandomSource = null;

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

@ -103,5 +103,101 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
AssertEx.AreEqual<double>(precShape, ng.PrecisionShape);
AssertEx.AreEqual<double>(precInvScale, ng.PrecisionInverseScale);
}
[Test]
[Row(0.0, 1.0, 1.0, 1.0)]
[Row(10.0, 1.0, 2.0, 2.0)]
public void CanGetPrecisionShape(double meanLocation, double meanScale, double precShape, double precInvScale)
{
NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale);
AssertEx.AreEqual<double>(precShape, ng.PrecisionShape);
}
[Test, MultipleAsserts]
[Row(0.0, 1.0, 1.0, 1.0)]
[Row(10.0, 1.0, 2.0, 2.0)]
public void CanSetPrecisionShape(double meanLocation, double meanScale, double precShape, double precInvScale)
{
NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale);
ng.PrecisionShape = 5.0;
AssertEx.AreEqual<double>(meanLocation, ng.MeanLocation);
AssertEx.AreEqual<double>(meanScale, ng.MeanScale);
AssertEx.AreEqual<double>(5.0, ng.PrecisionShape);
AssertEx.AreEqual<double>(precInvScale, ng.PrecisionInverseScale);
}
[Test]
[Row(0.0, 1.0, 1.0, 1.0)]
[Row(10.0, 1.0, 2.0, 2.0)]
public void CanGetPrecisionInverseScale(double meanLocation, double meanScale, double precShape, double precInvScale)
{
NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale);
AssertEx.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 CanSetPrecisionPrecisionInverseScale(double meanLocation, double meanScale, double precShape, double precInvScale)
{
NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale);
ng.PrecisionInverseScale = 5.0;
AssertEx.AreEqual<double>(meanLocation, ng.MeanLocation);
AssertEx.AreEqual<double>(meanScale, ng.MeanScale);
AssertEx.AreEqual<double>(precShape, ng.PrecisionShape);
AssertEx.AreEqual<double>(5.0, ng.PrecisionInverseScale);
}
[Test, MultipleAsserts]
[Row(0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 2.0)]
[Row(10.0, 1.0, 2.0, 2.0, 10.0, 1.0, 4.0)]
[Row(10.0, 1.0, 2.0, Double.PositiveInfinity, 10.0, 2.0, Double.PositiveInfinity)]
public void CanGetMeanMarginal(double meanLocation, double meanScale, double precShape, double precInvScale,
double meanMarginalMean, double meanMarginalScale, double meanMarginalDoF)
{
NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale);
var mm = ng.MeanMarginal();
AssertEx.AreEqual<double>(meanMarginalMean, mm.Location);
AssertEx.AreEqual<double>(meanMarginalScale, mm.Scale);
AssertEx.AreEqual<double>(meanMarginalDoF, mm.DegreesOfFreedom);
}
[Test, MultipleAsserts]
[Row(0.0, 1.0, 1.0, 1.0)]
[Row(10.0, 1.0, 2.0, 2.0)]
[Row(10.0, 1.0, 2.0, Double.PositiveInfinity)]
public void CanGetPrecisionMarginal(double meanLocation, double meanScale, double precShape, double precInvScale)
{
NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale);
var pm = ng.PrecisionMarginal();
AssertEx.AreEqual<double>(precShape, pm.Shape);
AssertEx.AreEqual<double>(precInvScale, pm.InvScale);
}
[Test, MultipleAsserts]
[Row(0.0, 1.0, 1.0, 1.0, 0.0, 1.0)]
[Row(10.0, 1.0, 2.0, 2.0, 10.0, 1.0)]
[Row(10.0, 1.0, 2.0, Double.PositiveInfinity, 10.0, 2.0)]
public void CanGetMean(double meanLocation, double meanScale, double precShape, double precInvScale,
double meanMean, double meanPrecision)
{
NormalGamma ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale);
AssertEx.AreEqual<double>(meanMean, ng.Mean.Mean);
AssertEx.AreEqual<double>(meanPrecision, ng.Mean.Precision);
}
[Test]
public void HasRandomSource()
{
NormalGamma ng = new NormalGamma(0.0, 1.0, 1.0, 1.0);
Assert.IsNotNull(ng.RandomSource);
}
[Test]
public void CanSetRandomSource()
{
NormalGamma ng = new NormalGamma(0.0, 1.0, 1.0, 1.0);
ng.RandomSource = new Random();
}
}
}
Loading…
Cancel
Save