JuliaStats / Distributions.jl

A Julia package for probability distributions and associated functions.
Other
1.1k stars 414 forks source link

Type instability of mixture distributions #1868

Open mauricelanghinrichs opened 3 months ago

mauricelanghinrichs commented 3 months ago

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