-
Notifications
You must be signed in to change notification settings - Fork 40
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
Native implementations for distrs #37
Comments
In conversion from |
I am team |
For example, I would write function poislogpdf(λ::T, x::T) where {T <: Real}
λ < 0 && throw(ArgumentError("λ = $λ should be non-negative"))
isinteger(x) && 0 ≤ x || return T(-Inf)
iszero(λ) && return iszero(x) ? zero(T) : T(-Inf)
xlogy(x, λ) - λ - lgamma(x + 1)
end Is the change in behavior justified? |
@Nosferican I think of an assertion as stating a condition that must be true if the algorithm/code is functioning properly. Passing an out-of-bounds parameter value is, to me, pretty much the definition of an Also, a programmer may wish to catch the error and take appropriate action. I don't think you can catch an assertion. |
|
@nalimilan Yes, of course. Thank you. |
However, doing a quick run, I noticed |
@Nosferican I disagree with throwing a I guess it is a matter of how closely you want to follow measure-theoretic probability and the idea that a probability mass function is a density with respect to counting measure. Because we tend to promote types I think of the By the way, in general we should evaluate probabilities on the logarithmic scale first then, if desired, transform back to the probability or probability density scale. And you definitely don't want to call julia> factorial(21)
ERROR: OverflowError()
Stacktrace:
[1] factorial_lookup(::Int64, ::Array{Int64,1}, ::Int64) at ./combinatorics.jl:31
[2] factorial(::Int64) at ./combinatorics.jl:39 I think the general approach should be to use poispdf(λ::Real, x::Real) = exp(poislogpdf(λ, x))
function poislogpdf(λ::T, x::T) where {T <: Real}
λ < 0 && throw(DomainError("λ = $λ must be non-negative"))
isinteger(x) && 0 ≤ x || return T(-Inf)
iszero(λ) && return iszero(x) ? zero(T) : T(-Inf)
xlogy(x, λ) - λ - lgamma(x + 1)
end
poislogpdf(λ::Number, x::Number) = poislogpdf(promote(float(λ), x)...) |
I agree. I was thinking of dispatching on whether the arguments were in the support or not à la what I believe |
@Nosferican I see that you are correct about julia> poislogpdf(-1.f0, 1.f0)
ERROR: DomainError:
Stacktrace:
[1] poislogpdf(::Float32, ::Float32) at /home/bates/.julia/v0.6/StatsFuns/src/distrs/pois.jl:19 and julia> poislogpdf(-1.f0, 1.f0)
ERROR: ArgumentError: λ = -1.0 must be non-negative
Stacktrace:
[1] poislogpdf(::Float32, ::Float32) at /home/bates/.julia/v0.6/StatsFuns/src/distrs/pois.jl:19 |
That's fixed on 0.7 thanks to JuliaLang/julia#22751. But until then |
I'd also like to bring up the discussion on unit tests. Current tests will not hold up. We could at first compare our implementation to the Rmath one but eventually I think we need to write our own tests to get an understanding of the function accuracy and precision and inform users of the same. |
Another thing to keep in mind when typing the function is that it would be nice for them to be compatible with ForwardDiff and such, which I think means This seems too restrictive for example: https://github.com/JuliaStats/StatsFuns.jl/pull/33/files#diff-eb7a8b837bc7832e705af52fca59cc7bR25 |
@Ellipse0934 Keep in mind that the code in I think that once a complete set of libRmath-free "d-p-q-r" functions for a distribution is available, the implementation should be moved to the Thus extensive unit tests on coverage and accuracy should be part of the Testing a native implementation versus a libRmath implementation in |
Any updates on this? Currently the fallback implementation of beta pdf has different behavior than Rmath (returning NaN instead of -Inf for out of domain values) which causes frustrations in some of my code. There are commits in the |
With #113 we are now pretty far here. What remains is mostly the |
Could someone post an updated list? |
|
Making this issue to track the progress in implementing the code for distrs natively
The text was updated successfully, but these errors were encountered: