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

Type instability of mixture distributions #1868

Open
mauricelanghinrichs opened this issue Jun 5, 2024 · 0 comments
Open

Type instability of mixture distributions #1868

mauricelanghinrichs opened this issue Jun 5, 2024 · 0 comments

Comments

@mauricelanghinrichs
Copy link

Hi all,

I noticed type instabilities when dealing with mixtures of distributions of different types. For example, the following mixture of a Dirac and a (shifted) Geometric distribution (both discrete), which I need for my work, has a type instability for evaluating the pdf and sampling a single random variate. Interestingly, sampling multiple random numbers (e.g., rand(d, 10) or even rand(d, 1)) is type stable.

using Distributions

a = 0.2
b = 0.95

d1 = Dirac(0)
d2 = Geometric(1-b) + 1
d = MixtureModel([d1, d2], [a, 1-a])

@code_warntype pdf(d, 2) # type instable
@code_warntype pdf.(d, [2,3,4]) # type instable
@code_warntype rand(d) # type instable
@code_warntype rand(d, 1) # type STABLE (?)

The properties of the mixtures seem correct; the pdfs look fine and also mean numbers make sense. But the type instability currently slows down my code.

The issue is not restricted to this specific setup. Also a mixture of Poisson and (not shifted) Geometric distributions has the same issues. The instability vanishes if d1 and d2 have the same distribution type (e.g., both Geometric) (as discussed on Discourse). Mixtures of continuous distributions (e.g., exponential and normal) also seem to have this issue.

It was unclear to us whether this can be considered a bug or missing feature.

Any help greatly appreciated!

Julia Version 1.10.4
Distributions v0.25.107

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

1 participant