From 74b42a6840d65d34f3525d103586b5e6355071e9 Mon Sep 17 00:00:00 2001 From: David Widmann Date: Fri, 12 May 2023 03:16:16 +0200 Subject: [PATCH] Prepare handling of truncated distribution for changes in Distributions --- src/distributions_ext.jl | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/distributions_ext.jl b/src/distributions_ext.jl index 35df9c7..8c124ba 100644 --- a/src/distributions_ext.jl +++ b/src/distributions_ext.jl @@ -439,19 +439,16 @@ Thanks, dude! """ function truncmean(Dist::Truncated) F(x) = x*pdf(Dist,x) - y = 0.0; - + lower, upper = extrema(Dist) if typeof(Dist) <: ContinuousDistribution # Continuous distribution - y = quadgk(F, Dist.lower, Dist.upper)[1] - + y = quadgk(F, lower, upper)[1] else # Discrete distriubtion - x = ceil(Dist.lower) + x = ceil(lower) q_max = 1 - 1E-9; - x_max = min(Dist.upper, quantile(Dist.untruncated, q_max)) - - while x < x_max + x_max = min(upper, quantile(Dist.untruncated, q_max)) + y = F(x) + while (x += 1) <= x_max y += F(x) - x += 1 end end return y