# Turing Model debug info

**URL:** <https://discourse.julialang.org/t/turing-model-debug-info/40440>\
**Category:** Probabilistic Programming\
**Created:** [May 29, 2020, 8:44pm UTC](https://discourse.julialang.org/t/turing-model-debug-info/40440 "2020-05-29T20:44:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsteckel](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@rsteckel](https://discourse.julialang.org/u/rsteckel)\
**Post date:** [May 29, 2020, 8:44pm UTC](https://discourse.julialang.org/t/turing-model-debug-info/40440/1 "2020-05-29T20:44:13Z")

</div>

Is there a way to get more information about when Turing is unable to sample from a model?

For example, I tried a simple (univariate) mixture of gaussians model and when I try to sample from it, I get this error:

```julia
ERROR: ArgumentError: Right-hand side of a ~ must be subtype of Distribution or a vector of Distributions.

```

It would be helpful to know the right-hand side of which “~” is causing the problem. Maybe something in DynamicPPL could give a line number?

Here’s the model:

```julia
@model mm(y) = begin
    N = length(y)

    μ1 ~ Normal()
    μ2 ~ Normal()
    μ3 ~ Normal()

    μ ~ [μ1, μ2, μ3]
    ps ~ Dirichlet(ones(3))

    k = Vector{Int}(undef, N)
    for i in 1:N
        k[i] ~ Categorical(ps)
        y[i] ~ Normal(μ[k[i]])
    end
   return k
end

```

[fce5fe82] Turing v0.13.0  
[31c24e10] Distributions v0.23.2

---

<div class="post-metadata">

**Author:** ![rsteckel](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@rsteckel](https://discourse.julialang.org/u/rsteckel)\
**Post date:** [May 29, 2020, 8:56pm UTC](https://discourse.julialang.org/t/turing-model-debug-info/40440/2 "2020-05-29T20:56:06Z")

</div>

I found the problem, but any debug info or tools would be helpful.

```julia
μ ~ [μ1, μ2, μ3]

```

Should be:

```julia
μ = [μ1, μ2, μ3]

```

---

<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 29, 2020, 9:42pm UTC](https://discourse.julialang.org/t/turing-model-debug-info/40440/3 "2020-05-29T21:42:29Z")

</div>

Could you please open an issue for this on DynamicPPL. Thanks!

---

<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 29, 2020, 9:47pm UTC](https://discourse.julialang.org/t/turing-model-debug-info/40440/4 "2020-05-29T21:47:00Z")

</div>

Just as a remark, in case you aim to sample z using particle Gibbs you will need to use TArrays.

Here is a slightly modified version using tzeros to construct a TArray.

```julia
@model mm(y) = begin
    N = length(y)

    μ ~ filldist(Normal(), 3)
    ps ~ Dirichlet(3, 1.0)
    k = tzeros(Int, N)
    for i in 1:N
        k[i] ~ Categorical(ps)
        y[i] ~ Normal(μ[k[i]])
    end
   return k
end

```

---

<div class="post-metadata">

**Author:** ![rsteckel](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@rsteckel](https://discourse.julialang.org/u/rsteckel)\
**Post date:** [June 2, 2020, 9:01pm UTC](https://discourse.julialang.org/t/turing-model-debug-info/40440/5 "2020-06-02T21:01:22Z")

</div>

Thanks Martin. I stumbled across the Turing Guide document. That helped quite a bit. I should have read that first.

However, using your model and using a dataset consisting of 3 gaussians generated in this way:

```julia
y = Vector{Float32}()
λ = [Normal(2), Normal(8), Normal(-1)]
G = Categorical([.3, .5, .2])
for i in 1:500
    a = rand(G)
    push!(y, rand(λ[a]))
end

```

and using this sample statement (with what seems like very relaxed parameters):

```julia
s = Gibbs(PG(10, :k), NUTS(20, .65, :μ, :ps))
@time chn = sample(mm(y), s, 500);

```

it took ~ 45 mins to complete. Does that seem as expected for 500 data points and 500 iterations?

---

<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:** [August 14, 2020, 4:40pm UTC](https://discourse.julialang.org/t/turing-model-debug-info/40440/6 "2020-08-14T16:40:40Z")

</div>

Hi, sorry for the late reply.

The current implementation for particle Gibbs doesn’t exploit parallelism. Ideally you want to use only HMC or NUTS in this model.  
Meaning you would marginalise out the discrete latent assignments. Does that answer your question?

---

<div class="post-metadata">

**Author:** ![rsteckel](https://avatars.discourse-cdn.com/v4/letter/r/45deac/32.png) [@rsteckel](https://discourse.julialang.org/u/rsteckel)\
**Post date:** [August 17, 2020, 2:55pm UTC](https://discourse.julialang.org/t/turing-model-debug-info/40440/7 "2020-08-17T14:55:26Z")

</div>

Yes, that makes sense. Thanks for the follow up!
