## Computing equivalent vector of random variables from covarience matrix

### Remember to vote up questions/answers you find interesting or helpful (requires 15 reputation points)

Given a covariance matrix, how can I construct a vector of expressions of randomly distributed variables whose covariance matrix is equal to the given one?

EDIT: All variables are normally distributed.

I have an algorithm that gets the covariances correct, but not the variances on the diagonal:

````a = [0]*len(r)
for x, row in enumerate(cov_matrix(r)):
    for y, item in enumerate(row):
        if x > y: continue
        v = noise(math.sqrt(abs(item)))
        a[x] += v
        if item > 0:
            a[y] += v
        else:
            a[y] -= v
````

I feel like this should be simple ...

-

Meta-question: Why did you write that as a comment instead of an answer? – Forrest Sep 4 2010 at 14:59
Good point. I was originally planning to leave a short comment, but I ended up giving more detail than I had thought I would. I deleted the comment and reposted it as an answer (and added a paragraph generalizing the construction slightly).  – Darsh Ranjan Sep 4 2010 at 23:41

## 2 Answers

If $A$ is your target covariance matrix and $LL^T = A$, and $x = (x_1, \ldots, x_n)$ is a vector of independent random variables with mean zero and variance 1, then $y = Lx$ has the required covariance. Here $L$ is a matrix and $L^T$ is its transpose. $L$ can just be the Cholesky factor of $A$. ((Check: $\mathrm{cov}(y) = E[yy^T] = E[(Lx)(Lx)^T] = E[Lxx^TL^T] = LE[xx^T]L^T$ (by linearity of expectation) $= L\mathrm{cov}(x)L^T = LIL^T = LL^T = A$. $\mathrm{cov}(y) = E[yy^T]$ because $y$ has mean 0, and likewise for $\mathrm{cov}(x)$.)

That's not too far from a "complete" solution, actually. If you start with a vector $y$ of random variables with mean zero and covariance matrix $A$, then if $A = LL^T$ and $x = L^{-1}y$, then $\mathrm{cov}(x) = I$. That doesn't necessarily imply that the components of $x$ are independent; it means they are uncorrelated. So the most general construction is to begin with a vector $x$ of uncorrelated random variables with mean zero and variance 1 and let $y=Lx$. (I only mean that every example can theoretically be obtained that way, not that it's necessarily the best or most computationally efficient way to do it.)

-

It depends on what you are doing.  Say you want uniformly distributed random variables with a particular correlation structure.  You can't just generate uncorrelated uniform random variables and apply the Cholesky decomposition because a sum of uniform random variables is no longer uniform. It will have the correct correlation (as you show) but the marginal distributions will not be uniform. – Robby McKilliam Sep 4 2010 at 23:47
The way I understood the problem, the only goal is to produce a random vector with a prescribed covariance matrix. If more information is prescribed, like the marginal distributions of the components, then I don't know if my method can be extended.  – Darsh Ranjan Sep 5 2010 at 1:32

### You can accept an answer to one of your own questions by clicking the check mark next to it. This awards 15 reputation points to the person who answered and 2 reputation points to you.

This question is perhaps more suited to stats exhange. Darsh suggested using the Cholesky decomposition, but this only works if the distribution of the random variables you want to generate is Gaussian. Otherwise there are two techniques that I know of, the Iman-Conover method and the methods based on Copulas.

-

"Using [...] Cholesky decomposition [...] only works if the distribution of the random variables you want to generate is Gaussian": No (re-read Darsh's comment). – Didier Piau Sep 4 2010 at 6:29
@Didier: See my comment on Darsh's answer. – Robby McKilliam Sep 5 2010 at 2:06
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 21, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9100992679595947, "perplexity_flag": "head"}