Browse Source

Categorical dist: Tests for Var, SD, and Median

Added unit tests to validate the following properties of the
 Categorical distribution:
- Variance,
- StdDev, and
- Median.

TODO: Find out the expected behaviour of Median in ambiguous cases
 (e.g. where P(X=1)=0.5, and P(X=2)=0.5), then add appropriate unit
 tests.

TODO: Research how to calculate entropy, then add unit tests for the
 Entropy property.
provider
David Prince 13 years ago
parent
commit
a7c7b67647
  1. 53
      src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs

53
src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs

@ -171,6 +171,59 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete
AssertHelpers.AlmostEqual(mean, n.Mean, 14);
}
/// <summary>
/// Validate standard deviation.
/// </summary>
/// <param name="p">An array of nonnegative ratios.</param>
/// <param name="stdDev">Standard deviation.</param>
[TestCase(new double[] { 0, 0.25, 0.5, 0.25 }, 0.70710678118654752440084436210485)]
[TestCase(new double[] { 0, 1, 2, 1 }, 0.70710678118654752440084436210485)]
[TestCase(new double[] { 0, 0.5, 0.5 }, 0.5)]
[TestCase(new double[] { 0.75, 0.25 }, 0.43301270189221932338186158537647)] //Sqrt((0.25*0.25)*.75+(.75*.75)*.25)
[TestCase(new double[] { 1, 0, 1 }, 1)]
public void ValidateStdDev(double[] p, double stdDev)
{
var n = new Categorical(p);
AssertHelpers.AlmostEqual(stdDev, n.StdDev, 14);
}
/// <summary>
/// Validate variance.
/// </summary>
/// <param name="p">An array of nonnegative ratios.</param>
/// <param name="variance">Variance.</param>
[TestCase(new double[] { 0, 0.25, 0.5, 0.25 }, 0.5)]
[TestCase(new double[] { 0, 1, 2, 1 }, 0.5)]
[TestCase(new double[] { 0, 0.5, 0.5 }, 0.25)]
[TestCase(new double[] { 0.75, 0.25 }, 0.1875)] //(0.25*0.25)*.75+(.75*.75)*.25)
[TestCase(new double[] { 1, 0, 1 }, 1)]
public void ValidateVariance(double[] p, double variance)
{
var n = new Categorical(p);
AssertHelpers.AlmostEqual(variance, n.Variance, 14);
}
/// <summary>
/// Validate median.
/// </summary>
/// <param name="p">An array of nonnegative ratios.</param>
/// <param name="median">Median.</param>
[TestCase(new double[] { 0, 0.25, 0.5, 0.25 }, 2)]
[TestCase(new double[] { 0, 1, 2, 1 }, 2)]
[TestCase(new double[] { 0.75, 0.25 }, 0)]
// The following test case has median of 5, because:
// P(X < 5) = (1+2+6+3+2)/29 = 14/29 < 0.5.
// P(X <= 5) = 19/29 > 0.5.
[TestCase(new double[] { 1, 2, 6, 3, 2, 5, 1, 1, 0, 1, 7 }, 5)]
// TODO: Find out the expected behavour of Median in ambiguous cases like the following:
//[TestCase(new double[] { 0, 0.5, 0.5 }, ???)]
//[TestCase(new double[] { 1, 0, 1 }, ???)]
public void ValidateMedian(double[] p, int median)
{
var n = new Categorical(p);
Assert.AreEqual(median, n.Median);
}
/// <summary>
/// Can sample static.
/// </summary>

Loading…
Cancel
Save