From a6878cc7d1bfa9befe0a5c4ca39325436ad215a1 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 22 Mar 2015 13:43:59 +0100 Subject: [PATCH] Distributions: ChiSquared.CDF test fix for more robust implementation #288 --- src/Numerics/Distributions/ChiSquared.cs | 2 +- .../Continuous/ChiSquareTests.cs | 50 ++++++++----------- src/UnitTests/UnitTests.csproj | 1 + 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/Numerics/Distributions/ChiSquared.cs b/src/Numerics/Distributions/ChiSquared.cs index 4076a090..90249767 100644 --- a/src/Numerics/Distributions/ChiSquared.cs +++ b/src/Numerics/Distributions/ChiSquared.cs @@ -215,7 +215,7 @@ namespace MathNet.Numerics.Distributions /// public double CumulativeDistribution(double x) { - return SpecialFunctions.GammaLowerIncomplete(_freedom/2.0, x/2.0)/SpecialFunctions.Gamma(_freedom/2.0); + return SpecialFunctions.GammaLowerRegularized(_freedom/2.0, x/2.0); } /// diff --git a/src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs b/src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs index 1047c718..6bedc208 100644 --- a/src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs @@ -293,36 +293,30 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous /// /// Degrees of freedom. /// Input X value. - [TestCase(1.0, 0.0)] - [TestCase(1.0, 0.1)] - [TestCase(1.0, 1.0)] - [TestCase(1.0, 5.5)] - [TestCase(1.0, 110.1)] - [TestCase(1.0, Double.PositiveInfinity)] - [TestCase(2.0, 0.0)] - [TestCase(2.0, 0.1)] - [TestCase(2.0, 1.0)] - [TestCase(2.0, 5.5)] - [TestCase(2.0, 110.1)] - [TestCase(2.0, Double.PositiveInfinity)] - [TestCase(2.5, 0.0)] - [TestCase(2.5, 0.1)] - [TestCase(2.5, 1.0)] - [TestCase(2.5, 5.5)] - [TestCase(2.5, 110.1)] - [TestCase(2.5, Double.PositiveInfinity)] - [TestCase(Double.PositiveInfinity, 0.0)] - [TestCase(Double.PositiveInfinity, 0.1)] - [TestCase(Double.PositiveInfinity, 1.0)] - [TestCase(Double.PositiveInfinity, 5.5)] - [TestCase(Double.PositiveInfinity, 110.1)] - [TestCase(Double.PositiveInfinity, Double.PositiveInfinity)] - public void ValidateCumulativeDistribution(double dof, double x) + /// N[CDF[ChiSquare[dof], x],20] + [TestCase(1.0, 0.0, 0.0)] + [TestCase(1.0, 0.1, 0.24817036595415071751)] + [TestCase(1.0, 1.0, 0.68268949213708589717)] + [TestCase(1.0, 5.5, 0.98098352632769945624)] + [TestCase(1.0, 110.1, 1.0)] + [TestCase(2.0, 0.0, 0.0)] + [TestCase(2.0, 0.1, 0.048770575499285990909)] + [TestCase(2.0, 1.0, 0.39346934028736657640)] + [TestCase(2.0, 5.5, 0.93607213879329242730)] + [TestCase(2.0, 110.1, 1.0)] + [TestCase(2.5, 0.0, 0.0)] + [TestCase(2.5, 0.1, 0.020298266579604156571)] + [TestCase(2.5, 1.0, 0.28378995266531297417)] + [TestCase(2.5, 5.5, 0.90239512593899828629)] + [TestCase(2.5, 110.1, 1.0)] + [TestCase(10000.0, 1.0, 0.0)] + [TestCase(10000.0, 7500.0, 3.3640453687878842514e-84)] + [TestCase(20000.0, 1.0, 0.0)] + public void ValidateCumulativeDistribution(double dof, double x, double expected) { var n = new ChiSquared(dof); - double expected = SpecialFunctions.GammaLowerIncomplete(dof / 2.0, x / 2.0) / SpecialFunctions.Gamma(dof / 2.0); - Assert.AreEqual(expected, n.CumulativeDistribution(x)); - Assert.AreEqual(expected, ChiSquared.CDF(dof, x)); + Assert.That(n.CumulativeDistribution(x), Is.EqualTo(expected).Within(1e-14)); + Assert.That(ChiSquared.CDF(dof, x), Is.EqualTo(expected).Within(1e-14)); } } } diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index 102394a1..3b9505a2 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -343,6 +343,7 @@ +