From db4a70cbe4ac4eb5098fbe8b0d9240ced9c4f145 Mon Sep 17 00:00:00 2001 From: Jurgen Van Gael Date: Sun, 20 Sep 2009 17:29:49 +0800 Subject: [PATCH] Enabled Beta entropy test. Signed-off-by: jvangael Signed-off-by: jvangael --- src/Numerics/Distributions/Continuous/Beta.cs | 32 ++++++++++++++++--- .../DistributionTests/Continuous/BetaTests.cs | 10 +++--- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/Numerics/Distributions/Continuous/Beta.cs b/src/Numerics/Distributions/Continuous/Beta.cs index 69be2966..ad48a83a 100644 --- a/src/Numerics/Distributions/Continuous/Beta.cs +++ b/src/Numerics/Distributions/Continuous/Beta.cs @@ -36,7 +36,14 @@ namespace MathNet.Numerics.Distributions /// Implements the Beta distribution. For details about this distribution, see /// Wikipedia - Beta distribution. /// - /// The distribution will use the by default. + /// + /// There are a few special cases for the parameterization of the Beta distribution. When both + /// shape parameters are positive infinity, the Beta distribution degenerates to a point distribution + /// at 0.5. When one of the shape parameters is positive infinity, the distribution degenerates to a point + /// distribution at the positive infinity. When both shape parameters are 0.0, the Beta distribution + /// degenerates to a Bernoulli distribution with parameter 0.5. When one shape parameter is 0.0, the + /// distribution degenerates to a point distribution at the non-zero shape parameter. + /// The distribution will use the by default. /// Users can get/set the random number generator by using the property. /// The statistics classes will check all the incoming parameters whether they are in the allowed /// range. This might involve heavy computation. Optionally, by setting Control.CheckDistributionParameters @@ -217,10 +224,25 @@ namespace MathNet.Numerics.Distributions { get { - return SpecialFunctions.BetaLn(_shapeA, _shapeB) - - ((_shapeA - 1.0) * SpecialFunctions.DiGamma(_shapeA)) - - ((_shapeB - 1.0) * SpecialFunctions.DiGamma(_shapeB)) - + ((_shapeA + _shapeB - 2.0) * SpecialFunctions.DiGamma(_shapeA + _shapeB)); + if (Double.IsPositiveInfinity(_shapeA) || Double.IsPositiveInfinity(_shapeB)) + { + return 0.0; + } + else if (_shapeA == 0.0 && _shapeB == 0.0) + { + return -Math.Log(0.5); + } + else if (_shapeA == 0.0 || _shapeB == 0.0) + { + return 0.0; + } + else + { + return SpecialFunctions.BetaLn(_shapeA, _shapeB) + - ((_shapeA - 1.0) * SpecialFunctions.DiGamma(_shapeA)) + - ((_shapeB - 1.0) * SpecialFunctions.DiGamma(_shapeB)) + + ((_shapeA + _shapeB - 2.0) * SpecialFunctions.DiGamma(_shapeA + _shapeB)); + } } } diff --git a/src/UnitTests/DistributionTests/Continuous/BetaTests.cs b/src/UnitTests/DistributionTests/Continuous/BetaTests.cs index b6585b0d..283fa501 100644 --- a/src/UnitTests/DistributionTests/Continuous/BetaTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/BetaTests.cs @@ -139,10 +139,10 @@ namespace MathNet.Numerics.UnitTests.DistributionTests AssertEx.AreEqual(mean, n.Mean); } - [Test, Ignore("Depending on Special Functions")] - [Row(0.0, 0.0, 0.5)] - [Row(0.0, 0.1, 0.1)] - [Row(1.0, 0.0, 1.0)] + [Test] + [Row(0.0, 0.0, 0.693147180559945309417232121458176568075500134360255)] + [Row(0.0, 0.1, 0.0)] + [Row(1.0, 0.0, 0.0)] [Row(1.0, 1.0, 0.0)] [Row(9.0, 1.0, -1.3083356884473304939016015849561625204060922267565917)] [Row(5.0, 100.0, -2.5201623187602743679459255108827601222133603091753153)] @@ -153,7 +153,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests public void ValidateEntropy(double a, double b, double entropy) { var n = new Beta(a, b); - AssertEx.AreEqual(entropy, n.Entropy); + AssertHelpers.AlmostEqual(entropy, n.Entropy, 14); } [Test]