Distributions.jl
Distributions.jl copied to clipboard
Why can't this package use numerical methods for mean of Truncated Distributions?
I noticed that Truncated distributions generally don't support mean, var, etc. But they could easily be calculated using numerical methods. In fact, I wrote my own custom module for them:
function numeric_mean( Dist::ContinuousUnivariateDistribution )
(LB, UB) = extrema(Dist)
F(x) = x*pdf(Dist,x)
return quadgk(F, LB, UB)[1]
end
function numeric_mean( Dist::DiscreteUnivariateDistribution )
(LB, UB) = extrema(Dist)
F(x) = x*pdf(Dist,x)
ϵ = 1E-9
#Ensure loop is finite
if !isfinite(LB)
LB = quantile(Dist, ϵ)
end
if !isfinite(UB)
UB = quantile(Dist, 1-ϵ)
end
#Loop through and numerically calculate the mean
y = 0.0
for x in LB:UB
y += F(x)
end
return y
end
I'm wondering why this sort of method (numeric_mean) isn't used as a fallback for the mean truncated distributions? I tried to extend "mean" myself, but it doesn't seem to work smoothly; I get errors and there's possible issues around type piracy. If performance is an issue, would it not be enough to give a warning that a numerical method is being used as an approximation?
Numerical methods can be complex to implement right and will have a lot of tricky corner cases.
A generic routine doing numerical integration like that is likely to have a lot of issues, for instance if you truncate on the tail of the distribution where the pdf is decaying to zero.
I think we should think about each case separately and handle them when possible. For instance Truncated Uniform is trivial.
And Truncated(Normal(), -1, 1)
is also well handled by Distributions.jl.