Browse Source

Beta distribution may generate NaN when a and b are both very small

pull/894/head
埃博拉酱 5 years ago
parent
commit
2de37bbba1
  1. 8
      src/Numerics.Tests/DistributionTests/Continuous/BetaTests.cs
  2. 2
      src/Numerics/Distributions/Beta.cs

8
src/Numerics.Tests/DistributionTests/Continuous/BetaTests.cs

@ -405,5 +405,13 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
Assert.That(dist.InverseCumulativeDistribution(p), Is.EqualTo(x).Within(1e-6));
Assert.That(Beta.InvCDF(a, b, p), Is.EqualTo(x).Within(1e-6));
}
[TestCase(0.001)]
public void ProbableNaNWhenABBothSmall(double ab)
{
Beta dist = new Beta(ab, ab);
for (byte i = 0; i < 100; ++i)
Assert.That(!double.IsNaN(dist.Sample()),"Generate NaN");
}
}
}

2
src/Numerics/Distributions/Beta.cs

@ -394,7 +394,7 @@ namespace MathNet.Numerics.Distributions
{
var x = Gamma.SampleUnchecked(rnd, a, 1.0);
var y = Gamma.SampleUnchecked(rnd, b, 1.0);
return x/(x + y);
return x == 0 ? 0 : x / (x + y);
}
internal static void SamplesUnchecked(System.Random rnd, double[] values, double a, double b)

Loading…
Cancel
Save