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/36/head
Jurgen Van Gael 17 years ago
parent
commit
0a6abf73e7
  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.
// </copyright>
namespace dnAnalytics.Statistics.Distributions
namespace MathNet.Numerics.Distributions
{
using System;
using Properties;
@ -43,7 +43,7 @@ namespace dnAnalytics.Statistics.Distributions
public class Dirichlet
{
// The Dirichlet distribution parameters.
private readonly double[] _alpha;
private double[] _alpha;
/// <summary>
/// The distribution's random number generator.
@ -106,6 +106,8 @@ namespace dnAnalytics.Statistics.Distributions
{
return false;
}
return true;
}
/// <summary>
@ -115,11 +117,12 @@ namespace dnAnalytics.Statistics.Distributions
/// <exception cref="ArgumentOutOfRangeException">When the parameters don't pass the <see cref="IsValidParameterSet"/> function.</exception>
private void SetParameters(double[] alpha)
{
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha) && ! alpha.Length == _alpha.Length)
if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha))
{
throw new ArgumentOutOfRangeException(Resources.InvalidDistributionParameters);
}
_alpha = new double[alpha.Length];
for (int i = 0; i < alpha.Length; i++)
{
_alpha[i] = alpha[i];
@ -143,11 +146,18 @@ namespace dnAnalytics.Statistics.Distributions
}
/// <summary>
/// The parameters of the Dirichlet distribution.
/// Gets or sets the parameters of the Dirichlet distribution.
/// </summary>
public double[] Alpha
{
get { return _alpha; }
get
{
return _alpha;
}
set
{
SetParameters(value);
}
}
/// <summary>
@ -171,7 +181,17 @@ namespace dnAnalytics.Statistics.Distributions
/// </summary>
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>

2
src/UnitTests/DistributionTests/CommonDistributionTests.cs

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

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

@ -26,37 +26,149 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace dnAnalytics.Tests.Statistics.Distributions
namespace MathNet.Numerics.UnitTests.DistributionTests
{
using dnAnalytics.Statistics.Distributions;
using NUnit.Framework;
using System;
using System.Linq;
using MbUnit.Framework;
using MathNet.Numerics.Distributions;
[TestFixture]
public class DirichletTests
{
private const double mAcceptableError = 1e-12;
[SetUp]
public void SetUp()
{
Control.CheckDistributionParameters = true;
}
[Test]
public void SymmetricDirichlet()
public void CanCreateSymmetricDirichlet()
{
Dirichlet d = new Dirichlet(0.3, 5);
for (int i = 0; i < 5; i++)
{
Assert.AreEqual(0.3, d.Mean[i], mAcceptableError);
Assert.AreEqual(0.3 * (1.5 - 0.3) / (1.5 * 1.5 * 2.5), d.Variance[i], mAcceptableError);
Assert.AreEqual(0.3, d.Alpha[i]);
}
}
[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]
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);
// Try getting the random number generator.
System.Random rnd = d.RandomNumberGenerator;
// Try setting the random number generator.
d.RandomNumberGenerator = new System.Random();
for (int i = 0; i < 5; i++)
{
AssertHelpers.AlmostEqual(0.3/1.5, d.Mean[i], 15);
}
}
[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