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

pdf for ChineseRestaurantProcess gives a stack overflow #1397

Closed
phipsgabler opened this issue Sep 1, 2020 · 8 comments
Closed

pdf for ChineseRestaurantProcess gives a stack overflow #1397

phipsgabler opened this issue Sep 1, 2020 · 8 comments

Comments

@phipsgabler
Copy link
Member

phipsgabler commented Sep 1, 2020

julia> ChineseRestaurantProcess(DirichletProcess(1.0), Int[])
ChineseRestaurantProcess{DirichletProcess{Float64},Array{Int64,1}}(
rpm: DirichletProcess{Float64}(1.0)
m: Int64[]
)

julia> pdf(ChineseRestaurantProcess(DirichletProcess(1.0), Int[]), 1)
ERROR: StackOverflowError:
Stacktrace:
 [1] pdf(::ChineseRestaurantProcess{DirichletProcess{Float64},Array{Int64,1}}, ::Int64) at /home/philipp/.julia/packages/Distributions/dTXqn/src/univariates.jl:333 (repeats 80000 times)

Appearently this is because Distributions contains a recursive definition of pdf for discrete univariate distributions that does not fall back on logpdf:

pdf(d::DiscreteUnivariateDistribution, x::Real) = isinteger(x) ? pdf(d, round(Int, x)) : zero(partype(d))

(logpdf works as it should.)

CC @trappmartin

@phipsgabler phipsgabler changed the title pdf for CRP gives a stack overflow pdf for ChineseRestaurantProcess gives a stack overflow Sep 1, 2020
@devmotion
Copy link
Member

This already came up with other discrete distributions (see e.g. #1089). I really think one should change the definition in Distributions if possible.

@torfjelde
Copy link
Member

This also caught me by surprise when implementing TransformedDistribution in Bijectors.jl. For multivariate distributions it's sufficient to implement logpdf because pdf falls back to it. But in the case of Univariate distributions, it's the reverse: https://github.com/JuliaStats/Distributions.jl/blob/09f2bbdf9faca686d72ff8d30d0299137d601842/src/univariates.jl#L342-L343

@trappmartin
Copy link
Member

This is completely insane. I think we should open a PR on the Distributions.jl side.

@phipsgabler
Copy link
Member Author

Done.

@trappmartin
Copy link
Member

Thanks!

@devmotion
Copy link
Member

@devmotion
Copy link
Member

The PR was merged into Distributions and released as part of Distributions 0.24.

@devmotion
Copy link
Member

As mentioned in the last comment, this is fixed in recent versions of Distributions.

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

No branches or pull requests

4 participants