Browse Source

Added Dirichlet unit tests

Fixed typo in CommonDistributionTests.cs

Signed-off-by: jvangael <jurgen.vangael@gmail.com>

Signed-off-by: jvangael <jurgen.vangael@gmail.com>
pull/2/head
jvangael 17 years ago
parent
commit
38d60ddb6a
  1. 32
      src/Numerics/Distributions/Multivariate/Dirichlet.cs
  2. 2
      src/UnitTests/DistributionTests/CommonDistributionTests.cs
  3. 136
      src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs

32
src/Numerics/Distributions/Multivariate/Dirichlet.cs

@ -26,7 +26,7 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
namespace dnAnalytics.Statistics.Distributions namespace MathNet.Numerics.Distributions
{ {
using System; using System;
using Properties; using Properties;
@ -43,7 +43,7 @@ namespace dnAnalytics.Statistics.Distributions
public class Dirichlet public class Dirichlet
{ {
// The Dirichlet distribution parameters. // The Dirichlet distribution parameters.
private readonly double[] _alpha; private double[] _alpha;
/// <summary> /// <summary>
/// The distribution's random number generator. /// The distribution's random number generator.
@ -106,6 +106,8 @@ namespace dnAnalytics.Statistics.Distributions
{ {
return false; return false;
} }
return true;
} }
/// <summary> /// <summary>
@ -115,11 +117,12 @@ namespace dnAnalytics.Statistics.Distributions
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception> /// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters(double[] alpha) private void SetParameters(double[] alpha)
{ {
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha) && ! alpha.Length == _alpha.Length) if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha))
{ {
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters); throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
} }
_alpha = new double[alpha.Length];
for (int i = 0; i < alpha.Length; i++) for (int i = 0; i < alpha.Length; i++)
{ {
_alpha[i] = alpha[i]; _alpha[i] = alpha[i];
@ -143,11 +146,18 @@ namespace dnAnalytics.Statistics.Distributions
} }
/// <summary> /// <summary>
/// The parameters of the Dirichlet distribution. /// Gets or sets the parameters of the Dirichlet distribution.
/// </summary> /// </summary>
public double[] Alpha public double[] Alpha
{ {
get { return _alpha; } get
{
return _alpha;
}
set
{
SetParameters(value);
}
} }
/// <summary> /// <summary>
@ -171,7 +181,17 @@ namespace dnAnalytics.Statistics.Distributions
/// </summary> /// </summary>
public double[] Mean public double[] Mean
{ {
get { return _alpha / AlphaSum; } get
{
double sum = AlphaSum;
double[] parm = new double[Dimension];
for (int i = 0; i < Dimension; i++)
{
parm[i] = _alpha[i] / sum;
}
return parm;
}
} }
/// <summary> /// <summary>

2
src/UnitTests/DistributionTests/CommonDistributionTests.cs

@ -54,7 +54,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
[Row(1)] [Row(1)]
[Row(2)] [Row(2)]
[Row(3)] [Row(3)]
public void CanCreateNormal(int i) public void ValidateThatUnivariateDistributionsHaveRandomSource(int i)
{ {
Assert.IsNotNull(dists[i].RandomSource); Assert.IsNotNull(dists[i].RandomSource);
} }

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

@ -26,37 +26,149 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
namespace dnAnalytics.Tests.Statistics.Distributions namespace MathNet.Numerics.UnitTests.DistributionTests
{ {
using dnAnalytics.Statistics.Distributions; using System;
using NUnit.Framework; using System.Linq;
using MbUnit.Framework;
using MathNet.Numerics.Distributions;
[TestFixture] [TestFixture]
public class DirichletTests public class DirichletTests
{ {
private const double mAcceptableError = 1e-12; [SetUp]
public void SetUp()
{
Control.CheckDistributionParameters = true;
}
[Test] [Test]
public void SymmetricDirichlet() public void CanCreateSymmetricDirichlet()
{ {
Dirichlet d = new Dirichlet(0.3, 5); Dirichlet d = new Dirichlet(0.3, 5);
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
Assert.AreEqual(0.3, d.Mean[i], mAcceptableError); Assert.AreEqual(0.3, d.Alpha[i]);
Assert.AreEqual(0.3 * (1.5 - 0.3) / (1.5 * 1.5 * 2.5), d.Variance[i], mAcceptableError); }
}
[Test]
public void CanCreateDirichlet()
{
double[] alpha = new double[10];
for (int i = 0; i < 10; i++)
{
alpha[i] = i;
}
Dirichlet d = new Dirichlet(alpha);
for (int i = 0; i < 5; i++)
{
Assert.AreEqual(i, d.Alpha[i]);
}
}
[Test]
[Row(0.0)]
[Row(-0.1)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void FailCreateDirichlet(double alpha)
{
Dirichlet d = new Dirichlet(alpha, 5);
}
[Test]
public void HasRandomSource(int i)
{
Dirichlet d = new Dirichlet(0.3, 5);
Assert.IsNotNull(d.RandomSource);
}
[Test]
public void CanSetRandomSource(int i)
{
Dirichlet d = new Dirichlet(0.3, 5);
d.RandomSource = new Random();
}
[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void FailSetRandomSourceWithNullReference(int i)
{
Dirichlet d = new Dirichlet(0.3, 5);
d.RandomSource = null;
}
[Test]
public void CanGetDimension()
{
Dirichlet d = new Dirichlet(0.3, 10);
Assert.AreEqual(10, d.Dimension);
}
[Test]
public void CanGetAlpha()
{
Dirichlet d = new Dirichlet(0.3, 10);
double[] alpha = new double[10];
for (int i = 0; i < 10; i++)
{
Assert.AreEqual(0.3, d.Alpha[i]);
} }
} }
[Test] [Test]
public void GetSetRNG() public void CanSetAlpha()
{
Dirichlet d = new Dirichlet(0.3, 10);
double[] alpha = new double[10];
for (int i = 0; i < 10; i++)
{
alpha[i] = i;
}
d.Alpha = alpha;
}
[Test]
public void ValidateMean()
{ {
Dirichlet d = new Dirichlet(0.3, 5); Dirichlet d = new Dirichlet(0.3, 5);
// Try getting the random number generator. for (int i = 0; i < 5; i++)
System.Random rnd = d.RandomNumberGenerator; {
// Try setting the random number generator. AssertHelpers.AlmostEqual(0.3/1.5, d.Mean[i], 15);
d.RandomNumberGenerator = new System.Random(); }
}
[Test]
public void ValidateVariance()
{
double[] alpha = new double[10];
double sum = 0.0;
for (int i = 0; i < 10; i++)
{
alpha[i] = i;
sum += i;
}
Dirichlet d = new Dirichlet(alpha);
for (int i = 0; i < 10; i++)
{
AssertHelpers.AlmostEqual(i * (sum - i) / (sum * sum * (sum + 1.0)), d.Variance[i], 15);
}
}
[Test]
public void Sample()
{
Dirichlet d = new Dirichlet(1.0, 5);
double[] s = d.Sample();
} }
} }
} }
Loading…
Cancel
Save