consistently treat Bernoulli as a distribution on Bools#2022
consistently treat Bernoulli as a distribution on Bools#2022aplavin wants to merge 1 commit intoJuliaStats:masterfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2022 +/- ##
=======================================
Coverage 86.36% 86.36%
=======================================
Files 146 146
Lines 8789 8789
=======================================
Hits 7591 7591
Misses 1198 1198 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
|
||
| function quantile(d::Bernoulli{T}, p::Real) where T<:Real | ||
| 0 <= p <= 1 ? (p <= failprob(d) ? zero(T) : one(T)) : T(NaN) | ||
| function quantile(d::Bernoulli, p::Real) |
There was a problem hiding this comment.
Can you revert the changes to quantile and cquantile? The current API (probably historically based on Rmath) is to never error but to return NaN for invalid arguments. I think this should be changed, but that requires a more general change of the API of these functions: #1805
There was a problem hiding this comment.
reverting quantile implies also reverting median for consistency, and then basically we lose the point of this PR
any hope of #1805 in the foreseeable future?
There was a problem hiding this comment.
btw, quantile throws sometimes already:
julia> quantile(Bernoulli{Int}(1), 2)
ERROR: InexactError: Int64(NaN)
There was a problem hiding this comment.
any hope of #1805 in the foreseeable future?
Has been ready since > 2 years, just needs a review.
reverting quantile implies also reverting median for consistency, and then basically we lose the point of this PR
The current implementation of quantile is wrong (IMO) but it's consistent. It would be worse if some distributions would behave differently, we can't start erroring just for Bernoulli. You can still change mode, modes and support without touching quantile/cquantile.
There was a problem hiding this comment.
we can't start erroring just for Bernoulli
It already errors sometimes (and with a different exception). In addition to the example above:
quantile(Bernoulli(1//3), 2)There was a problem hiding this comment.
Any references to that? Where do you get that "The current API is to return NaN for invalid inputs"?
Currently, the situation is that basically all discrete distributions return integers from quantile. And naturally, they throw an exception when p not in 0..1.
Documentation doesn't seem to contradict this in the slightest.
Even Bernoulli often behaves like that. It is Bernoulli{Float}() that is an exception (resolved by this PR).
There was a problem hiding this comment.
As I just noted at #1805, NaN is allowed for many continuous distributions (Normal, Geometric, Exponential, Cauchy, Bernoulli...). And discrete distributions don't throw DomainErrors, they just get an error during calculations, without any indication that it's intentional. Let's get that PR merged.
There was a problem hiding this comment.
discrete distributions don't throw DomainErrors
Technically, this doesn't throw a DomainError indeed:
But only because there's no zero-argument constructor for DomainError. Seems pretty much intentional :)
continuous distributions (<...>, Bernoulli...)
??
There was a problem hiding this comment.
But only because there's no zero-argument constructor for DomainError. Seems pretty much intentional :)
I was referring to
julia> quantile(Binomial(10, 0.2), 2)
ERROR: InexactError: Int64(NaN)Though the DomainError error is funny.
continuous distributions (<...>, Bernoulli...)
Sorry, I was talking only about the special case of passing NaN: quantile(Bernoulli(), NaN).
There was a problem hiding this comment.
julia> quantile(Binomial(10, 0.2), 2)
ERROR: InexactError: Int64(NaN)
It's an error still, just a less understandable one. If preferred, this PR can be changed into throwing the same one for Bernoulli, although I think DomainError is cleaner.
Anyway, from experience with this and my previous PR attempts to Distributions over the years, I find it super hard to add/change anything in this package. I had barely any success with that, in stark contrast to other Julia packages. So I'll stop here, and just hope that some improvements do happen at some point...
Currently, it's quite inconsistent: eg,
eltype === Bool,rand()::Bool,minimum()::Bool, butmodeandmedianareInt, andquantileisT(often, float).With this PR
BernoullireturnsBoolwhenever possible and makes sense.This is both self-consistent between functions on
Bernoulli, and also makes it consistent with other discrete distributions likeBinomial/Categorical/DiscreteUniform/DiscreteNonParametricthat return the minimal type (oftenInt) for theirquantileand other functions.