# Type instability of pdf of MixtureModel

**URL:** <https://discourse.julialang.org/t/type-instability-of-pdf-of-mixturemodel/68058>\
**Category:** Statistics\
**Tags:** question\
**Created:** [September 12, 2021, 6:39pm UTC](https://discourse.julialang.org/t/type-instability-of-pdf-of-mixturemodel/68058 "2021-09-12T18:39:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [September 12, 2021, 6:39pm UTC](https://discourse.julialang.org/t/type-instability-of-pdf-of-mixturemodel/68058/1 "2021-09-12T18:39:32Z")

</div>

When I create a Normal one-dimensional mixture as,

```julia
d = MixtureModel([Normal(-1,1), Normal(1, 1)], [0.5,0.5])

```

it turns out that the pdf is not type stable:

> julia\> @code\_warntype pdf(d,0.3)  
> MethodInstance for Distributions.pdf(::MixtureModel{Univariate, Continuous, Normal{Float64}, Float64}, ::Float64)  
> from pdf(d::UnivariateMixture{Continuous}, x::Real) in Distributions at /home/sgaure/.julia/packages/Distributions/Xrm9e/src/mixtures/mixturemodel.jl:419  
> Arguments  
> #self#::Core.Const(Distributions.pdf)  
> d::MixtureModel{Univariate, Continuous, Normal{Float64}, Float64}  
> x::Float64  
> Body::Any  
> 1 ─ %1 = Distributions.\_mixpdf1(d, x)::Any  
> └── return %1

The pdf is used for integration inside a likelihood optimization, and this type instability slows it down by a non-trivial factor. This is of course quite simple to get around by coding the pdf myself for the particular flavours of it I need, but it ought to be handled automatically. Also, the pdf does quite some allocations. This could be avoided, I think.

Is there some simple trick to make type inference work?

---

<div class="post-metadata">

**Author:** ![sethaxen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sethaxen/32/35604_2.png) [@sethaxen](https://discourse.julialang.org/u/sethaxen)\
**Post date:** [September 12, 2021, 7:04pm UTC](https://discourse.julialang.org/t/type-instability-of-pdf-of-mixturemodel/68058/2 "2021-09-12T19:04:14Z")

</div>

I’m not seeing this type-instability. Can you double-check in a clean environment and share the outputs of `Pkg.status()` and `versioninfo()` as done below?

```julia
julia> using Distributions

julia> d = MixtureModel([Normal(-1,1), Normal(1, 1)], [0.5,0.5])
MixtureModel{Normal{Float64}}(K = 2)
components[1] (prior = 0.5000): Normal{Float64}(μ=-1.0, σ=1.0)
components[2] (prior = 0.5000): Normal{Float64}(μ=1.0, σ=1.0)

julia> @code_warntype pdf(d,0.3)
Variables
  #self#::Core.Const(Distributions.pdf)
  d::MixtureModel{Univariate, Continuous, Normal{Float64}, Categorical{Float64, Vector{Float64}}}
  x::Float64

Body::Float64
1 ─ %1 = Distributions._mixpdf1(d, x)::Float64
└── return %1

julia> using Pkg; Pkg.status()
      Status `/private/var/folders/5c/7l7zjpjj4fs6t31xlh4hjggm0000gn/T/jl_FhzLU5/Project.toml`
  [31c24e10] Distributions v0.25.16

julia> versioninfo()
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin18.7.0)
  CPU: Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, haswell)
Environment:

```

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [September 12, 2021, 7:12pm UTC](https://discourse.julialang.org/t/type-instability-of-pdf-of-mixturemodel/68058/3 "2021-09-12T19:12:36Z")

</div>

Ah, thanks, I was in an environment with an old version.
