Browse Source

Fixed a few special cases in the Gamma distribution densities.

Signed-off-by: Christoph Ruegg <git@cdrnet.ch>
pull/2/head
jvangael 17 years ago
committed by Christoph Ruegg
parent
commit
4c5bbc5cd2
  1. 8
      src/Managed.UnitTests/DistributionTests/Continuous/GammaTests.cs
  2. 25
      src/Managed/Distributions/Continuous/Gamma.cs

8
src/Managed.UnitTests/DistributionTests/Continuous/GammaTests.cs

@ -278,7 +278,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
[Row(0.0, 0.0, 1.0, 0.0)]
[Row(0.0, 0.0, 10.0, 0.0)]
[Row(1.0, 0.1, 0.0, 0.10000000000000000555111512312578270211815834045410156)]
[Row(1.0, 0.1, 1.0, 0.099004983374916810660915381324116279472953858297127391)]
[Row(1.0, 0.1, 1.0, 0.090483741803595961836995913651194571475319347018875963)]
[Row(1.0, 0.1, 10.0, 0.036787944117144234201693506390001264039984687455876246)]
[Row(1.0, 1.0, 0.0, 1.0)]
[Row(1.0, 1.0, 1.0, 0.36787944117144232159552377016146086744581113103176804)]
@ -295,7 +295,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
public void ValidateDensity(double shape, double invScale, double x, double pdf)
{
var n = new Gamma(shape, invScale);
AssertHelpers.AlmostEqual(pdf, n.Density(x), 15);
AssertHelpers.AlmostEqual(pdf, n.Density(x), 14);
}
[Test]
@ -303,7 +303,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
[Row(0.0, 0.0, 1.0, Double.NegativeInfinity)]
[Row(0.0, 0.0, 10.0, Double.NegativeInfinity)]
[Row(1.0, 0.1, 0.0, -2.3025850929940456285068402234265387271634735938763824)]
[Row(1.0, 0.1, 1.0, -2.312585092994045630449730516520562672904829013035318)]
[Row(1.0, 0.1, 1.0, -2.402585092994045634057955346552321429281631934330484)]
[Row(1.0, 0.1, 10.0, -3.3025850929940456285068402234265387271634735938763824)]
[Row(1.0, 1.0, 0.0, 0.0)]
[Row(1.0, 1.0, 1.0, -1.0)]
@ -320,7 +320,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests
public void ValidateDensityLn(double shape, double invScale, double x, double pdfln)
{
var n = new Gamma(shape, invScale);
AssertHelpers.AlmostEqual(pdfln, n.DensityLn(x), 15);
AssertHelpers.AlmostEqual(pdfln, n.DensityLn(x), 14);
}
[Test]

25
src/Managed/Distributions/Continuous/Gamma.cs

@ -170,7 +170,14 @@ namespace MathNet.Numerics.Distributions
set
{
SetParameters(_shape, 1.0/value);
double invScale = 1.0/value;
if(Double.IsNegativeInfinity(invScale))
{
invScale = - invScale;
}
SetParameters(_shape, invScale);
}
}
@ -360,6 +367,14 @@ namespace MathNet.Numerics.Distributions
return 0.0;
}
}
else if (_shape == 0.0 && _invScale == 0.0)
{
return 0.0;
}
else if (_shape == 1.0)
{
return _invScale * Math.Exp(- _invScale * x);
}
else
{
return Math.Pow(_invScale, _shape) * Math.Pow(x, _shape - 1.0) * Math.Exp(-_invScale * x) / SpecialFunctions.Gamma(_shape);
@ -384,6 +399,14 @@ namespace MathNet.Numerics.Distributions
return Double.NegativeInfinity;
}
}
else if(_shape == 0.0 && _invScale == 0.0)
{
return Double.NegativeInfinity;
}
else if(_shape == 1.0)
{
return Math.Log(_invScale) - _invScale*x;
}
else
{
return _shape * Math.Log(_invScale) + (_shape - 1.0) * Math.Log(x) - _invScale * x - SpecialFunctions.GammaLn(_shape);

Loading…
Cancel
Save