# Variational Inference of Mixture models?

**URL:** <https://discourse.julialang.org/t/variational-inference-of-mixture-models/40031>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [May 23, 2020, 4:33pm UTC](https://discourse.julialang.org/t/variational-inference-of-mixture-models/40031 "2020-05-23T16:33:34Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [May 23, 2020, 6:12pm UTC](https://discourse.julialang.org/t/variational-inference-of-mixture-models/40031/2 "2020-05-23T18:12:55Z")

</div>

Hi, you can use variational inference only for continuous parameters. In variational inference, the goal is to minimise the KL-divergence from the variational distribution to the true posterior. For this, we use gradient based optimisation in Turing, which requires the computation of gradients.

However, you can reformulate a mixture model such that it contains only continuous parameters.

The following model runs using ADVI.

```julia
@model function gmm(x, K)
    N = length(x)
    μ ~ filldist(Normal(0,1), K)
    w ~ Dirichlet(K, 1)
    for i in 1:N
        x[i] ~ MixtureModel(Normal, μ, w)
    end
end

```

Alternatively, you could also use the `arraydist` function, i.e.

```julia
@model function gmm(x, K)
    N = length(x)
    μ ~ filldist(Normal(0,1), K)
    w ~ Dirichlet(K, 1)
    x ~ arraydist(map(i -> MixtureModel(Normal, μ, w), 1:N))
end

```

Note that you might want to change the AD backend if you have a model with a lot of parameters. In this case it is probably not necessary, but for more involved models you might want to keep this in mind and possibly switch to a reverse mode AD. You can find details about this in the documentation of Turing.

---

_[View the full topic](https://discourse.julialang.org/t/variational-inference-of-mixture-models/40031)._
