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

Avoid Inf from logpdf(::Beta,...) (issue #350) #359

Merged
merged 2 commits into from
Feb 15, 2021

Conversation

jamesonquinn
Copy link
Contributor

This adds 2 special cases to logpdf(::Beta,...)

  • If x==0 and alpha<1, then effectively use x=eps/e (machine epsilon divided by e)
  • If x==1 and beta<1, then effectively use x=1-eps/e (machine epsilon divided by e)

Adds two special cases for logpdf(::Beta...), when x is 0 or 1. Effectively, uses (machine epsilon / e) or one minus that instead of 0 or 1.
@@ -12,7 +12,15 @@ const beta = Beta()

function logpdf(::Beta, x::Real, alpha::Real, beta::Real)
(x < 0 || x > 1 ? -Inf :
(alpha - 1) * log(x) + (beta - 1) * log1p(-x) - logbeta(alpha, beta) )
((x == 0 && alpha <= 1) ? #Special case to avoid infinite PDF at 0
(alpha - 1) * log(eps(typeof(x))) -
Copy link
Contributor

Choose a reason for hiding this comment

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

If the user uses a constraint like choicemap(:x => 0), there's a chance that x will be of type Int. It looks like this will cause eps(typeof(x)) to error, but that's probably fine (the user can always use 0.0).

Copy link
Contributor

@alex-lew alex-lew left a comment

Choose a reason for hiding this comment

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

Looks good to me -- thanks!

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