Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error when truncating outside of support #1723

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ararslan
Copy link
Member

Fixes #843 using option 2 as described here. The implementation checks for 0 probability within the truncated region but continues to allow coincident truncation bounds if they're within the support. That avoids breaking cases like that shown in #1712.

Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for AD backends such as Zygote we should allow to disable (and skip automatically) the check by uaing the @check_args macro - in other distributions standard checks (without @check_args) impacted performance severely.

src/truncate.jl Outdated Show resolved Hide resolved
@ararslan
Copy link
Member Author

Looks like this approach doesn't quite do it, as evidenced by the large number of test failures 🙃

@devmotion
Copy link
Member

Some of them at least are due to tests that have to be fixed/removed: https://github.com/JuliaStats/Distributions.jl/actions/runs/5017513840/jobs/8995725428?pr=1723#step:6:67

Some others seem to indicate that logtp = -Inf currently for normal distributions truncated far away from the mean. Even though some of their traits (e.g., mean and variance) are tested they are basically useless:

julia> d = truncated(Normal(0, 1), 100, 115)
Truncated(Normal{Float64}=0.0, σ=1.0); lower=100.0, upper=115.0)

julia> mean(d)
100.00999800099926

julia> var(d)
9.994005086296622e-5

julia> pdf(d, 101)
NaN

julia> logpdf(d, 101)
Inf

julia> d.logtp
-Inf

julia> d.tp
0.0

I think we might be able to fix these existing numerical issues for normal distributions (at least) by improving the numerical accuracy in the calculation of logtp. IMO this should be possible since

julia> SpecialFunctions.logerf(100/sqrt(2), 115/sqrt(2)) - log(2)
-5005.5242086942035

It seems one part could consist of improving logdiffcdf for normal distributions as currently

julia> logdiffcdf(Normal(0, 1), 115, 100)
-Inf

And then maybe logdiffcdf could be used in truncated?

@devmotion
Copy link
Member

I opened #1728 to address the logdiffcdf issue for normal distributions.

src/truncate.jl Outdated Show resolved Hide resolved
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Truncated should catch truncations outside the support
2 participants