# Turing: no method matching MvNormal(::Matrix{Float64}, ::Float64)

**URL:** https://discourse.julialang.org/t/turing-no-method-matching-mvnormal-matrix-float64-float64/69207
**Category:** Statistics
**Tags:** question
**Created:** [October 4, 2021, 8:00pm UTC](https://discourse.julialang.org/t/turing-no-method-matching-mvnormal-matrix-float64-float64/69207 "2021-10-04T20:00:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Tarotis](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@Tarotis](https://discourse.julialang.org/u/Tarotis)
#### Post date: [October 4, 2021, 8:00pm UTC](https://discourse.julialang.org/t/turing-no-method-matching-mvnormal-matrix-float64-float64/69207/1 "2021-10-04T20:00:22Z")

</div>

Hello everybody,

as I want to switch from R to Julia for my Bayesian modelling, tonight I run my first models in Turing. However, I encountered some problems, principally when trying to model a random-intercept model.

The model is defined as follows:

```julia
@model my_regression(x, y, idx) = begin
    α ~ Normal(mean(y), 2.5 * std(y))
    β ~ Exponential(2)
    σ ~ Exponential(1 / std(y))

    n_gr = length(unique(idx))

    τ ~ truncated(Cauchy(0, 1), 0, Inf) # group-level SDs intercepts
    αⱼ ~ filldist(Normal(0, τ), n_gr) # group-level intercepts

    ŷ = α .+ αⱼ[idx] .+ x * β 
    y ~ MvNormal(ŷ, σ)
end;

```

However, when running the model I receive the error `no method matching MvNormal(::Matrix{Float64}, ::Float64)`.

A similar model without the group-level intercepts runs without any problems and uses ` @. y ~ Normal(exp(α + β1 * x), σ)` as a linear model. I suspect that a main reason for my problems could be due to a lack of understanding of the role of broadcasting, but I am really not sure.

Looking forward to your comments,

Tarotis

---

<div class="post-metadata">

### Author: ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)
#### Post date: [October 4, 2021, 8:29pm UTC](https://discourse.julialang.org/t/turing-no-method-matching-mvnormal-matrix-float64-float64/69207/2 "2021-10-04T20:29:24Z")

</div>

I could be wrong but i think MvNormal is expecting a vector and not a matrix as you appear to be sending it.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [October 4, 2021, 8:56pm UTC](https://discourse.julialang.org/t/turing-no-method-matching-mvnormal-matrix-float64-float64/69207/3 "2021-10-04T20:56:31Z")

</div>

You are correct. ŷ should be a vector. I’m guessing `x` is a n by 1 array rather than a column vector.

---

<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: [October 4, 2021, 10:01pm UTC](https://discourse.julialang.org/t/turing-no-method-matching-mvnormal-matrix-float64-float64/69207/4 "2021-10-04T22:01:35Z")

</div>

What the others have pointed out is correct. There is a `MatrixNormal`, but that’s likely overkill here. This should be fine:

```julia
y ~ MvNormal(vec(ŷ), σ)

```

Edit: this assumes you also vectorize `y`.  
Also, you’ll get better performance if you precompute constants like `mean(y)`, `std(y)`, and `unique(idx)` outside of the model. And this is faster:

```julia
ŷ = α .+ view(αⱼ, idx) .+ x .* β

```

---

<div class="post-metadata">

### Author: ![Tarotis](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@Tarotis](https://discourse.julialang.org/u/Tarotis)
#### Post date: [October 5, 2021, 8:06am UTC](https://discourse.julialang.org/t/turing-no-method-matching-mvnormal-matrix-float64-float64/69207/5 "2021-10-05T08:06:22Z")

</div>

Thank you all for the feedback and explicit explanations! It works great now. As the models will be quite complex in the end I fear that STAN/brms might be faster still, but I guess I will only find out once I have the corresponding models ready.
