Skip to content

Use Bessel.jl for performance? #1979

@aplavin

Description

@aplavin

I noticed that the implementations of Bessel functions used here are quite slow, and replacing them with Bessel.jl can give major speedups.
For examples, replacing

function logpdf(d::Rician, x::Real)
ν, σ = params(d)
result = log(2 * abs(x) / σ^2) + logpdf(NoncentralChisq(2, (ν / σ)^2), (x / σ)^2)
return x < 0 || isinf(x) ? oftype(result, -Inf) : result
end
with

julia> import Bessels as B

julia> @eval Distributions function logpdf(d::Rician, x::Real)
           ν, σ = params(d); σ² = σ^2
           result = log(x/σ² * $B.besseli0(x*ν/σ²)) - (x^2 + ν^2)/2σ²
           return x < 0 || isinf(x) ? oftype(result, -Inf) : result
       end

leads to ~x3-4 performance improvement immediately. Presumably, there are much more cases where this is possible.

Would adding a Bessels.jl dep be considered a reasonable thing by Distributions.jl devs?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions