# Type instability with Dirichlet distribution in Turing

**URL:** https://discourse.julialang.org/t/type-instability-with-dirichlet-distribution-in-turing/39300
**Category:** Probabilistic Programming
**Tags:** question
**Created:** [May 11, 2020, 6:41pm UTC](https://discourse.julialang.org/t/type-instability-with-dirichlet-distribution-in-turing/39300 "2020-05-11T18:41:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [May 11, 2020, 6:41pm UTC](https://discourse.julialang.org/t/type-instability-with-dirichlet-distribution-in-turing/39300/1 "2020-05-11T18:41:00Z")

</div>

I’m fitting a marginalized Gaussian mixture model with Turing, and the sampler is running very slowly. Looking at the output of `@code_warntype`, it appears that drawing the mixture weights from a Dirichlet distribution introduces a type instability (when I make the weight vector constant the sampler runs much faster, so that seems to be the issue). Is this a bug? Any ideas for a workaround? Thanks!

MWE:

```julia
using Turing
@model MarginalizedGMM(x, K, ::Type{T}=Vector{Float64}) where {T} = begin
    N = length(x)
    μ = T(undef, K)
    σ = T(undef, K)
    for i in 1:K
        μ[i] ~ Normal(0, 5)
        σ[i] ~ Gamma()
    end
    w ~ Dirichlet(K, 1.0)
    # w = T([0.75, 0.25]) Way faster with this line instead of ↑
    for i in 1:N
      x[i] ~ Distributions.UnivariateGMM(μ,σ, Categorical(w))
    end
    return (μ::T, σ::T, w::T)
end

x = [randn(150) .- 2; randn(50) .+ 2]
gmm = MarginalizedGMM(x, 2)
varinfo = Turing.VarInfo(gmm)
spl = Turing.SampleFromPrior()
@code_warntype gmm.f(varinfo, spl, Turing.DefaultContext(), gmm)

chn = sample(gmm, NUTS(100, 0.65), 1000)

```

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 12, 2020, 6:49am UTC](https://discourse.julialang.org/t/type-instability-with-dirichlet-distribution-in-turing/39300/2 "2020-05-12T06:49:00Z")

</div>

Please open an issue so it doesn’t get forgotten. I will take a look when I get some time.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 12, 2020, 8:25am UTC](https://discourse.julialang.org/t/type-instability-with-dirichlet-distribution-in-turing/39300/3 "2020-05-12T08:25:35Z")

</div>

And btw it’s natural for it to be faster with that line removed because then you are not accumulating the logpdf from the Dirichlet and not differentiating it wrt to the transformed parameters which that line would do. But the type instability is what I am concerned about.

---

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [May 12, 2020, 4:26pm UTC](https://discourse.julialang.org/t/type-instability-with-dirichlet-distribution-in-turing/39300/4 "2020-05-12T16:26:52Z")

</div>

Issue opened: [Type instability with Dirichlet distribution · Issue #1276 · TuringLang/Turing.jl · GitHub](https://github.com/TuringLang/Turing.jl/issues/1276)

And yeah, I wouldn’t be surprised to see it run a bit slower when it needs to sample the weights as well, but the slowdown with the type instability is ~25x, which did get my attention.
