Distributions.jl icon indicating copy to clipboard operation
Distributions.jl copied to clipboard

Why can't this package use numerical methods for mean of Truncated Distributions?

Open Deduction42 opened this issue 5 years ago • 2 comments

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?

Deduction42 avatar Sep 09 '19 14:09 Deduction42

Numerical methods can be complex to implement right and will have a lot of tricky corner cases.

matbesancon avatar Sep 26 '19 09:09 matbesancon

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.

cossio avatar Dec 23 '21 10:12 cossio