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

Fix rand for truncated normal with 0 variance #1721

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

Conversation

ararslan
Copy link
Member

@ararslan ararslan commented May 11, 2023

Fixes #1712
Fixes #1867

if a <= μ <= b
z = 0.0
else
return NaN
Copy link
Member

Choose a reason for hiding this comment

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

I think we would want to catch this case earlier in truncated: A distribution with zero mass is by definition not a distribution. See #843.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe let's throw an error here? I don't think returning NaN is particularly useful.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've opened #1723 as a means of catching this case earlier, but I'd also be fine throwing an error here.

@ararslan
Copy link
Member Author

Seems I was beaten to the punch by about 2 years: #1273 🙂

@codecov-commenter
Copy link

codecov-commenter commented May 11, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.99%. Comparing base (13029c0) to head (5c79db1).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1721   +/-   ##
=======================================
  Coverage   85.99%   85.99%           
=======================================
  Files         144      144           
  Lines        8666     8670    +4     
=======================================
+ Hits         7452     7456    +4     
  Misses       1214     1214           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@HakanBrian
Copy link

This should also resolve #1867

@ararslan ararslan requested a review from devmotion August 15, 2024 17:07
z = randnt(rng, a, b, d.tp)
if iszero(σ)
if a <= μ <= b
z = 0.0
Copy link
Member

Choose a reason for hiding this comment

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

I assume this will cause type instabilities, e.g., if parameters of Normal and Truncated are Float32?

Copy link
Member Author

@ararslan ararslan Aug 16, 2024

Choose a reason for hiding this comment

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

z is always Float64, both here and the branch that calls randnt (that function requires all inputs be Float64 and it returns a Float64). But if µ and the truncation points are wider, say BigFloat, then you'll get a MethodError from randnt when isfinite(µ) && σ > 0 and a BigFloat result otherwise. (That behavior predates this PR.) Float32 works because extrema promotes the truncation points to Float64, so every branch will return Float64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants