Browse Source

Distributions: ChiSquared.CDF test fix for more robust implementation #288

cuda
Christoph Ruegg 12 years ago
parent
commit
a6878cc7d1
  1. 2
      src/Numerics/Distributions/ChiSquared.cs
  2. 50
      src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs
  3. 1
      src/UnitTests/UnitTests.csproj

2
src/Numerics/Distributions/ChiSquared.cs

@ -215,7 +215,7 @@ namespace MathNet.Numerics.Distributions
/// <seealso cref="CDF"/>
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);
}
/// <summary>

50
src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs

@ -293,36 +293,30 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
/// </summary>
/// <param name="dof">Degrees of freedom.</param>
/// <param name="x">Input X value.</param>
[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)
/// <param name="expected">N[CDF[ChiSquare[dof], x],20]</param>
[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));
}
}
}

1
src/UnitTests/UnitTests.csproj

@ -343,6 +343,7 @@
<Compile Include="EuclidTests\GcdRelatedTest.cs" />
<Compile Include="EuclidTests\GcdRelatedTestBigInteger.cs" />
<Compile Include="EuclidTests\IntegerTheoryTest.cs" />
<Compile Include="LinearAlgebraTests\MatrixStorageCombinatorsTests.cs" />
<Compile Include="LinearAlgebraTests\VectorStorageCombinatorsTests.cs" />
<Compile Include="Random\SystemRandomSourceTests.cs" />
<Compile Include="RootFindingTests\BisectionTest.cs" />

Loading…
Cancel
Save