# How to generate n samples from a peculiar (normally distributed) distribution

**URL:** https://discourse.julialang.org/t/how-to-generate-n-samples-from-a-peculiar-normally-distributed-distribution/51911
**Category:** Probabilistic Programming
**Tags:** distributions
**Created:** [December 16, 2020, 6:07am UTC](https://discourse.julialang.org/t/how-to-generate-n-samples-from-a-peculiar-normally-distributed-distribution/51911 "2020-12-16T06:07:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Jaidy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaidy/32/20310_2.png) [@Jaidy](https://discourse.julialang.org/u/Jaidy)
#### Post date: [December 16, 2020, 6:07am UTC](https://discourse.julialang.org/t/how-to-generate-n-samples-from-a-peculiar-normally-distributed-distribution/51911/1 "2020-12-16T06:07:50Z")

</div>

Hi,  
I am trying to generate n samples from a distribution as follows:  
each sample is of size m. Say x is a sample. Then for each of the components {x\_1,x\_2,…x\_m}, x\_i follows a normal distribution with mean 0 and variance i^2. Furthermore, the correlation between x\_i and x\_j is 0.5^{i != j}.

The covariance matrix is easy to code and pass to say MvNormal() function from Distributions. But how do I ensure the variance of each of the components?

I can sample x\_i ~ N(0, i^2) individually, and stack up these to do so, but I was wondering if there is a shorter way to do it? Especially ensuring the correlation between the components of a sample.

Thanks a lot

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 16, 2020, 4:12pm UTC](https://discourse.julialang.org/t/how-to-generate-n-samples-from-a-peculiar-normally-distributed-distribution/51911/2 "2020-12-16T16:12:58Z")

</div>

> [@Jaidy](#):
>
> The covariance matrix is easy to code and pass to say MvNormal() function from Distributions. But how do I ensure the variance of each of the components?

The variance would be the diagonal of your covariance matrix. If you have a correlation matrix, just pre/postmultiply with diagonal matrices that contain the standard deviations. For details, see any basic statistics text or [wikipedia](https://en.wikipedia.org/wiki/Covariance_matrix#Relation_to_the_correlation_matrix).
