Skip to content

Rician logpdf inaccurate #2035

Description

@aplavin

Minimal example: Rician logpdf for moderate parameter values.
Computing Rician(ν=1, σ=0.12) at x=2.

using Distributions: logpdf, Rician
using SpecialFunctions: besselix

ν, σ, x = 1.0, 0.12, 2.0

# 1. Distributions.jl
println("Distributions.jl:  ", logpdf(Rician(ν, σ), x))

# 2. Exact formula with besselix (Wikipedia Rician PDF)
# same (correct) with besselix from Bessels.jl
σ² = σ^2; z = x*ν/σ²
println("Bessel formula:    ", log(x/σ² * besselix(0, z)) - (x-ν)^2/(2σ²))

# 3. BigFloat series (ground truth)
ν_b, σ_b, x_b = big"1.0", big"0.12", big"2.0"
z_b = x_b*ν_b/σ_b^2
I0 = sum(((z_b/2)^k / factorial(big(k)))^2 for k in 0:300)
println("BigFloat series:   ", Float64(log(x_b/σ_b^2) - (x_b^2+ν_b^2)/(2σ_b^2) + log(I0)))

And scipy:

from scipy.stats import rice
print("scipy:", rice.logpdf(2.0, 1.0/0.12, scale=0.12))

Result:

Distributions.jl:  -33.670645484570354   (WRONG, off by 0.497)
Bessel formula:    -33.17342036436976
BigFloat series:   -33.17342036436976
scipy:             -33.17342036436977

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions