Skip to content

Conversation

@MrVPlusOne
Copy link
Contributor

The current constructor for Truncated is not numerically stable, for example,

original = Normal(-5.0, 0.2)
trunc = truncated(original, 0.0, 5.0)
test_point = 0.5
logpdf(trunc, test_point)  # returns Inf!

This is because the constructor directly performs the computation using probabilities instead of log probabilities:

function truncated(d::UnivariateDistribution, l::T, u::T) where {T <: Real}
    l < u || error("lower bound should be less than upper bound.")
    T2 = promote_type(T, eltype(d))
    lcdf = isinf(l) ? zero(T2) : T2(cdf(d, l))
    ucdf = isinf(u) ? one(T2) : T2(cdf(d, u))
    tp = ucdf - lcdf
    Truncated(d, promote(l, u, lcdf, ucdf, tp, log(tp))...)
end

This pull request fixes this issue by using logcdf and the numerically stable operation logsubexp.

@codecov-commenter
Copy link

codecov-commenter commented Aug 26, 2021

Codecov Report

Merging #1374 (2d3e216) into master (e49b844) will increase coverage by 7.24%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1374      +/-   ##
==========================================
+ Coverage   75.56%   82.81%   +7.24%     
==========================================
  Files         116      116              
  Lines        4903     6661    +1758     
==========================================
+ Hits         3705     5516    +1811     
+ Misses       1198     1145      -53     
Impacted Files Coverage Δ
src/truncate.jl 85.91% <100.00%> (+7.34%) ⬆️
src/genericfit.jl 66.66% <0.00%> (-8.34%) ⬇️
src/univariate/continuous/studentizedrange.jl 72.72% <0.00%> (-5.06%) ⬇️
src/univariate/continuous/noncentralf.jl 85.00% <0.00%> (-1.67%) ⬇️
src/univariate/continuous/cosine.jl 92.85% <0.00%> (-1.27%) ⬇️
src/univariate/continuous/arcsine.jl 88.57% <0.00%> (-1.09%) ⬇️
src/univariate/continuous/skewnormal.jl 65.62% <0.00%> (-1.05%) ⬇️
src/univariate/continuous/normalinversegaussian.jl 88.46% <0.00%> (-1.02%) ⬇️
src/univariate/discrete/negativebinomial.jl 91.11% <0.00%> (-0.79%) ⬇️
src/show.jl 95.00% <0.00%> (-0.66%) ⬇️
... and 102 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e49b844...2d3e216. Read the comment docs.

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.

Thank you for the PR and sorry for the delay 🙂

@devmotion devmotion merged commit 27f5413 into JuliaStats:master Aug 26, 2021
@MrVPlusOne MrVPlusOne deleted the jiayi/fix-truncated branch August 26, 2021 16:00
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.

3 participants