Generation of a normally distributed random vector with a covariance matrix

In Matlab, it is easy to get a normally distributed random vector with mean and standard deviation. From randn help:

Generate values ​​from the normal distribution with mean 1 and standard deviation 2. r = 1 + 2. * randn (100.1);

Now I have the covariance matrix C, and I want to generate N (0, C).

But how can I do this?

From randn help: Generate values ​​from a two-dimensional normal distribution with a given average of the vector and covariance matrices. mu = [1 2]; Sigma = [1.5; .5 2]; R = chol (Sigma); z = repmat (mu, 100.1) + randn (100.2) * R;

But I don’t know exactly what they are doing here.

+4
source share
2 answers

This is a somewhat mathematical question, not a programming question. But I'm a big fan of writing great code that requires both solid mathematical and programming knowledge, so I will write this for posterity.

You need to take the Cholesky decomposition (or any decomposition / square root of the matrix) to create correlated random variables from independent ones. This is due to the fact that if X is a multidimensional norm with an average value of m and covariance D , then Y = AX is a multidimensional norm with an average Am and covariance matrix ADA' , where A' is transposed. If D is the identity matrix, then the covariance matrix is ​​just AA' , which you want to equal the covariance matrix C that you are trying to generate.

Cholesky's decomposition calculates such a matrix A and is the most efficient way to execute it.

For more information see http://web.as.uky.edu/statistics/users/viele/sta601s03/multnorm.pdf

+4
source

You can use the following matlab built-in function to do your job

 mvnrnd(mu,SIGMA) 
+3
source

All Articles