How to broadcast pdf function for normal distribution with vector of means and sigmas

I’m trying to calculate probabilities using a Normal distribution for a vector of values. When I have the same sigma for all values, it is easy, just like this:

sigma = 3
vals  = [-1,0,1]
pdf.(Normal(0, sigma), vals)

But what if I have a different sigma value for each value in the vector?
This is what I want:

vals    = [-1, 0, 1]
sigma_v = [1 ,2 ,3]

pdf.(Normal(0, sigma_v[1]), vals[1])
pdf.(Normal(0, sigma_v[2]), vals[2])
pdf.(Normal(0, sigma_v[3]), vals[3])

But I can’t get it by doing something like this:

pdf.(Normal(0, sigma_v), vals)

How is this done in Julia?

Does it work if tyou add a dot after Normal?

pdf.(Normal.(0, sigma), vals)

That way you are broadcasting also the Normal function to the elements of sigma.

It does!!.. so simple.

I guess I’m still confused with the broadcasting. Thanks!

1 Like