From aad2fd809bcc7d6b87be19ad9955469433a8ce29 Mon Sep 17 00:00:00 2001 From: Orion Yeung <11580988+orionyeung001@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:03:00 -0500 Subject: [PATCH] fix: mode for gamma distribution is 0 for shape<=1 --- src/distribution/gamma.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/distribution/gamma.rs b/src/distribution/gamma.rs index 7a36a30f..c905089f 100644 --- a/src/distribution/gamma.rs +++ b/src/distribution/gamma.rs @@ -133,17 +133,13 @@ impl ContinuousCDF for Gamma { fn sf(&self, x: f64) -> f64 { if x <= 0.0 { 1.0 - } - else if ulps_eq!(x, self.shape) && self.rate.is_infinite() { + } else if ulps_eq!(x, self.shape) && self.rate.is_infinite() { 0.0 - } - else if self.rate.is_infinite() { + } else if self.rate.is_infinite() { 1.0 - } - else if x.is_infinite() { + } else if x.is_infinite() { 0.0 - } - else { + } else { gamma::gamma_ur(self.shape, x * self.rate) } } @@ -239,13 +235,17 @@ impl Mode> for Gamma { /// /// # Formula /// - /// ```ignore - /// (α - 1) / β + /// ```text + /// max{(α - 1) / β, 0} /// ``` /// /// where `α` is the shape and `β` is the rate fn mode(&self) -> Option { - Some((self.shape - 1.0) / self.rate) + if self.shape < 1.0 { + Some((self.shape - 1.0) / self.rate) + } else { + Some(0.0) + } } }