Skip to content

Type instability of mixture distributions #1868

@mauricelanghinrichs

Description

@mauricelanghinrichs

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

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