From d105a429e8b49de175df77f6df172b4ed717af25 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 19 Jul 2015 11:22:39 +0200 Subject: [PATCH] Distributions: Erlang and Gamma PDF more robust for large shape parameter --- src/Numerics/Distributions/Erlang.cs | 5 +++++ src/Numerics/Distributions/Gamma.cs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/Numerics/Distributions/Erlang.cs b/src/Numerics/Distributions/Erlang.cs index f4253cdb..ca533188 100644 --- a/src/Numerics/Distributions/Erlang.cs +++ b/src/Numerics/Distributions/Erlang.cs @@ -412,6 +412,11 @@ namespace MathNet.Numerics.Distributions return rate*Math.Exp(-rate*x); } + if (shape > 160.0) + { + return Math.Exp(PDFLn(shape, rate, x)); + } + return Math.Pow(rate, shape)*Math.Pow(x, shape - 1.0)*Math.Exp(-rate*x)/SpecialFunctions.Gamma(shape); } diff --git a/src/Numerics/Distributions/Gamma.cs b/src/Numerics/Distributions/Gamma.cs index 28eadb5a..9791daf5 100644 --- a/src/Numerics/Distributions/Gamma.cs +++ b/src/Numerics/Distributions/Gamma.cs @@ -489,6 +489,11 @@ namespace MathNet.Numerics.Distributions return rate*Math.Exp(-rate*x); } + if (shape > 160.0) + { + return Math.Exp(PDFLn(shape, rate, x)); + } + return Math.Pow(rate, shape)*Math.Pow(x, shape - 1.0)*Math.Exp(-rate*x)/SpecialFunctions.Gamma(shape); }