# MvNorm for several vectors at once

**URL:** <https://discourse.julialang.org/t/mvnorm-for-several-vectors-at-once/87673>\
**Category:** New to Julia\
**Tags:** distributions\
**Created:** [September 22, 2022, 8:41pm UTC](https://discourse.julialang.org/t/mvnorm-for-several-vectors-at-once/87673 "2022-09-22T20:41:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurar1891](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurar1891/32/38443_2.png) [@laurar1891](https://discourse.julialang.org/u/laurar1891)\
**Post date:** [September 22, 2022, 8:41pm UTC](https://discourse.julialang.org/t/mvnorm-for-several-vectors-at-once/87673/1 "2022-09-22T20:41:30Z")

</div>

I’m trying to calculate some probabilities using MvNorm.

For a univariate case I would do the following to get the probabilities for a vector, all at once:

```julia
Norm_uni = Normal(2,1) #Creates the Normal dist
xvect = [2,3,4,5]
pdf.(Norm_uni, xvect) # Resutls in a vector of length 4

```

I want to do the same but for a multivariate case. I have the following:

```julia
mu = [2,4]
sigma = [1 0
         0 2]

Norm_def = MvNormal(mu,sigma)

x = [2.5,2]
pdf(Norm_def, x) #This works just fine for a single vector

```

But what if I want the values for two vectors?

```julia
y = [1,1]

pdf.(Norm_def, [x,y]) #This doesn't work

```

I get an error if I try to do just what I did with the univariate case. I also tried to use a matrix structure instead but it also didn’t work.

Thanks!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 22, 2022, 8:46pm UTC](https://discourse.julialang.org/t/mvnorm-for-several-vectors-at-once/87673/2 "2022-09-22T20:46:16Z")

</div>

You don’t say what the error message is, but I’m guessing that this is because currently only univariate distributions are broadcastable ([make univariate distributions broadcastable by simonbyrne · Pull Request #760 · JuliaStats/Distributions.jl · GitHub](https://github.com/JuliaStats/Distributions.jl/pull/760)). The workaround is simply to wrap the distribution in a `Ref` (or a 1-component tuple), like:

```julia
pdf.(Ref(Norm_def), [x,y]) 

```

---

<div class="post-metadata">

**Author:** ![laurar1891](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurar1891/32/38443_2.png) [@laurar1891](https://discourse.julialang.org/u/laurar1891)\
**Post date:** [September 22, 2022, 8:55pm UTC](https://discourse.julialang.org/t/mvnorm-for-several-vectors-at-once/87673/3 "2022-09-22T20:55:14Z")

</div>

Great. This works. What is the Ref doing here? I don’t quite follow the description of Ref in the help.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 22, 2022, 8:58pm UTC](https://discourse.julialang.org/t/mvnorm-for-several-vectors-at-once/87673/4 "2022-09-22T20:58:50Z")

</div>

> [@laurar1891](#):
>
> What is the Ref doing here?

From the [manual](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting):

> Sometimes, you want a container (like an array) that would normally participate in broadcast to be “protected” from broadcast’s behavior of iterating over all of its elements. By placing it inside another container (like a single element [`Tuple`](https://docs.julialang.org/en/v1/base/base/#Core.Tuple)) broadcast will treat it as a single value.

`Ref(x)` acts like a (cheap) zero-dimensional array of `x`.
