Skip to content

Commit

Permalink
Prepare handling of truncated distribution for changes in Distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
devmotion authored May 12, 2023
1 parent 3c8e6eb commit 74b42a6
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/distributions_ext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 74b42a6

Please sign in to comment.