2
$\begingroup$

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 ...

$\endgroup$
0

2 Answers 2

3
$\begingroup$

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.)

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ Commented Sep 4, 2010 at 23:47
  • $\begingroup$ 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. $\endgroup$ Commented Sep 5, 2010 at 1:32
2
$\begingroup$

This question is perhaps more suited to stats exchange. 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.

$\endgroup$
3
  • $\begingroup$ "Using [...] Cholesky decomposition [...] only works if the distribution of the random variables you want to generate is Gaussian": No (re-read Darsh's comment). $\endgroup$
    – Did
    Commented Sep 4, 2010 at 6:29
  • $\begingroup$ @Didier: See my comment on Darsh's answer. $\endgroup$ Commented Sep 5, 2010 at 2:06
  • $\begingroup$ The link to springerlink.com is broken. I'm also unable to find any copy saved on the Wayback Machine. $\endgroup$ Commented Aug 11, 2022 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .