# How do I get an almost "normal" normal distribution?

**URL:** <https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863>\
**Category:** New to Julia\
**Created:** [August 31, 2020, 8:24pm UTC](https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863 "2020-08-31T20:24:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [August 31, 2020, 8:24pm UTC](https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863/1 "2020-08-31T20:24:36Z")

</div>

Sorry for the “normal” normal 😅. What I mean is that I need a Gaussian distribution with a slight change. I need to include a factor \xi, such as the density distribution below

![Captura de tela de 2020-08-31 17-16-41](https://global.discourse-cdn.com/julialang/original/3X/3/f/3f175759ecf317d428f32d2d57a911a93c3f2145.png)

I don’t need the best performance, just a simple example.

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [August 31, 2020, 8:28pm UTC](https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863/2 "2020-08-31T20:28:40Z")

</div>

You can define your own distribution w Distributions.jl.

For example here is how to define a slew-normal: [Skew normal distribution? - #9 by Albert\_Zevelev](https://discourse.julialang.org/t/skew-normal-distribution/21549/9)

---

<div class="post-metadata">

**Author:** ![stst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stst/32/17252_2.png) [@stst](https://discourse.julialang.org/u/stst)\
**Post date:** [September 1, 2020, 2:41am UTC](https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863/3 "2020-09-01T02:41:11Z")

</div>

The expression you posted looks like an ordinary three-dimensional (i.e. multivariate) normal distribution with zero mean and a diagonal (but not isotropic) covariance matrix.

This is easy with the Distributions package - see [here](https://juliastats.org/Distributions.jl/latest/multivariate/#Distributions.MvNormal).

```julia
using Distributions
rf = 1.2
xi = 0.3
sig = rf^2*[1/xi, 1/xi, xi^2] # diagonal of covariance matrix
d = MvNormal(sig) # get distribution object

rand(d,4) # draw samples

r = [2,3,4]
pdf(d,r) # evaluate probability density function (pdf)

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 1, 2020, 2:48am UTC](https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863/4 "2020-09-01T02:48:46Z")

</div>

student’s T is almost normal.

Heck by CLT `sum(x)` if elements of `x` are all drawn from iid distribution then it is almost normal.

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [September 1, 2020, 3:26am UTC](https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863/5 "2020-09-01T03:26:31Z")

</div>

With this diagonal of covariance matrix you got something new to my life.

At link of the documentation you’ve send, it says :

> vector of type `Vector{T}` : indicating a diagonal covariance as `diagm(abs2(sig))`

Maybe I’m overthinking, but if the `MvNormal` function applies a `abs2`, the input should be the square root. That is, `sig = rf*[1/sqrt(xi), 1/sqrt(xi), xi]`. Or am I just confused ?

---

<div class="post-metadata">

**Author:** ![stst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stst/32/17252_2.png) [@stst](https://discourse.julialang.org/u/stst)\
**Post date:** [September 1, 2020, 3:56am UTC](https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863/6 "2020-09-01T03:56:09Z")

</div>

AFAICT, this looks like a mistake in the documentation. In the [source code](https://github.com/JuliaStats/Distributions.jl/blob/master/src/multivariate/mvnormal.jl), `sig` is taken as the covariance matrix \varSigma, or an abbreviated form thereof.

The parameter would probably better be named `Sig`, to avoid confusion with the standard deviation \sigma. The two are related by \varSigma = \sigma^2 \boldsymbol{I} for an isotropic covariance matrix.

EDIT: This is wrong.

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [September 1, 2020, 1:39pm UTC](https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863/7 "2020-09-01T13:39:51Z")

</div>

After some reading I believe that you’re talking about this line:

```julia
# constructor without mean vector
MvNormal(Σ::AbstractVecOrMat{<:Real}) = MvNormal(Zeros{eltype(Σ)}(size(Σ, 1)), Σ)

```

Indeed, the input should be the covariance matrix diagonal as you wrote before.

---

<div class="post-metadata">

**Author:** ![stst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stst/32/17252_2.png) [@stst](https://discourse.julialang.org/u/stst)\
**Post date:** [September 1, 2020, 6:02pm UTC](https://discourse.julialang.org/t/how-do-i-get-an-almost-normal-normal-distribution/45863/8 "2020-09-01T18:02:33Z")

</div>

Actually, I was wrong in my previous post (I crossed it out). The meaning of `sig` in `MvNormal(sig)` depends on the type of `sig `.

- If `sig` is a scalar, it’s taken as the isotropic standard deviation \sigma.

```julia
julia> using Distributions, LinearAlgebra
julia> mu = [1.,2.,3.]; sig = 2.; MvNormal(mu,sig)
IsoNormal(
dim: 3
μ: [1.0, 2.0, 3.0]
Σ: [4.0 0.0 0.0; 0.0 4.0 0.0; 0.0 0.0 4.0]
)

```

- If `sig` is a 3-element vector, it’s the vector of standard deviations (\sigma\_x,\sigma\_y,\sigma\_z)

```julia
julia> mu = [1.,2.,3.]; sig = [2.,3.,4.]; MvNormal(mu,sig)
DiagNormal(
dim: 3
μ: [1.0, 2.0, 3.0]
Σ: [4.0 0.0 0.0; 0.0 9.0 0.0; 0.0 0.0 16.0]
)

```

- If `sig` is a 3-by-3 matrix, it’s taken as the covariance matrix \varSigma (with the squares of the standard deviations on the diagonal).

```julia
julia> mu = [1.,2.,3.]; sig = diagm([2.,3.,4.]); MvNormal(mu,sig)
FullNormal(
dim: 3     
μ: [1.0, 2.0, 3.0]
Σ: [2.0 0.0 0.0; 0.0 3.0 0.0; 0.0 0.0 4.0]
)

```

One needs to be careful here, since for a vector `sig`, `MvNormal(sig)` and `MvNormal(diagm(sig))` give different distributions. Not sure whether this subtle difference is by design or not.
