# Type stability in MixtureModels

**URL:** <https://discourse.julialang.org/t/type-stability-in-mixturemodels/83932>\
**Category:** Performance\
**Tags:** distributions\
**Created:** [July 8, 2022, 7:43am UTC](https://discourse.julialang.org/t/type-stability-in-mixturemodels/83932 "2022-07-08T07:43:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeeviL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leevil/32/32586_2.png) [@LeeviL](https://discourse.julialang.org/u/LeeviL)\
**Post date:** [July 8, 2022, 7:43am UTC](https://discourse.julialang.org/t/type-stability-in-mixturemodels/83932/1 "2022-07-08T07:43:36Z")

</div>

Hi! I want to define a `MixtureModel` in which the component distributions might be from different families. While this piece of code works, the code is type unstable:

```julia
using Distributions
mixture = MixtureModel([Normal(-2, 1), TDist(2), Normal(2, 1)])
@code_warntype logpdf(mixture, 1.0)

```

and the output is

```julia
MethodInstance for Distributions.logpdf(::MixtureModel{Univariate, Continuous, Distribution{Univariate, Continuous}, Categorical{Float64, Vector{Float64}}}, ::Float64)
  from logpdf(d::UnivariateMixture, x::Real) in Distributions at /Users/leevi/.julia/packages/Distributions/O5xl5/src/mixtures/mixturemodel.jl:363
Arguments
  #self#::Core.Const(Distributions.logpdf)
  d::MixtureModel{Univariate, Continuous, Distribution{Univariate, Continuous}, Categorical{Float64, Vector{Float64}}}
  x::Float64
Body::Any
1 ─ %1 = Distributions._mixlogpdf1(d, x)::Any
└── return %1

```

When the components are from the same family, there is no problem:

```julia
mixture = MixtureModel([Normal(-2, 1), Normal(0, 1), Normal(2, 1)])
@code_warntype logpdf(mixture, 1.0)

```

Are there any workarounds for this?
