# How to draw samples from a multivariate Gaussian distribution without having access to a function that does the job? [duplicate]

I am using the programming language Lua which does not have any built-in function for drawing samples from a multivariate Gaussian distribution. So I wonder, how can one implement a function that does the sampling for a multivariate Gaussian distribution? Note that I do have access to a function which can draw samples from a single-variable Gaussian distribution. Can I somehow use that function to draw samples for the multivariate case given a desired covariance matrix?

Also, if I'm not mistaken, I remember I read somewhere that one can draw samples from a single-variable Gaussian distribution as follow:

$\text{sample } = \mu + \sigma^2 \epsilon$

where $\mu$ is a mean, $\sigma^2$ is a variance and $\epsilon \sim U(0,1)$. Therefore, a generalization of the above equation for a multivariate Gaussian distribution is to have mean and [diagonal] covariance matrix. However, if not sure if this equation is valid by itself and whether it gives valid samples for a non-diagonal covariance matrix.

So I wonder, can you guys give me some insights on how one should go about generating samples for a multivariate Gaussian distribution given a desired non0diagonal covariance matrix and without having having access to a built-in function in a programming language? Also, I would appreciate your help to verify/prove the equation I wrote above.

## marked as duplicate by shadowtalker, Greenparker, Glen_b♦May 16 '16 at 23:54

• If you chose $\epsilon$ to be a vector, wouldn't $\sigma^2$ be the 2 by 2 covariance matrix? – sodiumnitrate May 16 '16 at 22:14
• @sodiumnitrate You're right, but I'm just not sure whether those samples are going to be valid. So can someone give some proofs or reasoning on why those samples are valid? – Amir May 16 '16 at 22:18
• Your equation is wrong; that would give samples with variance $\sigma^4$. There are numerous posts already on algorithms for generating from a multivariate Gaussian distribution, for example, via a Choleski decomposition. – Glen_b May 16 '16 at 23:16
• An example is discussed here in the paragraph beginning "In the first case...". Also see here (but which doesn't mention the mean-shift). – Glen_b May 16 '16 at 23:33
• Also here and here – Glen_b May 16 '16 at 23:49