From a7c7b67647e83db13f53a8847025e62fcf7a42cb Mon Sep 17 00:00:00 2001 From: David Prince Date: Sun, 29 Dec 2013 22:46:00 +0800 Subject: [PATCH] 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. --- .../Discrete/CategoricalTests.cs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs b/src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs index 67d2e9e4..b13e0c20 100644 --- a/src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs @@ -171,6 +171,59 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete AssertHelpers.AlmostEqual(mean, n.Mean, 14); } + /// + /// Validate standard deviation. + /// + /// An array of nonnegative ratios. + /// Standard deviation. + [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); + } + + /// + /// Validate variance. + /// + /// An array of nonnegative ratios. + /// Variance. + [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); + } + + /// + /// Validate median. + /// + /// An array of nonnegative ratios. + /// Median. + [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); + } + /// /// Can sample static. ///