Computing equivalent vector of random variables from covarience matrix - MathOverflow most recent 30 from http://mathoverflow.net 2013-06-19T11:00:57Z http://mathoverflow.net/feeds/question/37682 http://www.creativecommons.org/licenses/by-nc/2.5/rdf http://mathoverflow.net/questions/37682/computing-equivalent-vector-of-random-variables-from-covarience-matrix Computing equivalent vector of random variables from covarience matrix Forrest 2010-09-04T02:49:47Z 2010-09-28T21:38:55Z <p>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?</p> <p>EDIT: All variables are normally distributed.</p> <p>I have an algorithm that gets the covariances correct, but not the variances on the diagonal:</p> <pre><code>a = [0]*len(r) for x, row in enumerate(cov_matrix(r)): for y, item in enumerate(row): if x &gt; y: continue v = noise(math.sqrt(abs(item))) a[x] += v if item &gt; 0: a[y] += v else: a[y] -= v </code></pre> <p>I feel like this should be simple ...</p> http://mathoverflow.net/questions/37682/computing-equivalent-vector-of-random-variables-from-covarience-matrix/37692#37692 Answer by Robby McKilliam for Computing equivalent vector of random variables from covarience matrix Robby McKilliam 2010-09-04T04:34:25Z 2010-09-04T04:34:25Z <p>This question is perhaps more suited to <a href="http://stats.stackexchange.com/" rel="nofollow">stats exhange</a>. 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 <a href="http://www.mynl.com/pptp/Correlation_CAS_052004.pdf" rel="nofollow">Iman-Conover method</a> and the methods based on <a href="http://www.springerlink.com/content/r20lu6631v34j876/" rel="nofollow">Copulas</a>.</p> http://mathoverflow.net/questions/37682/computing-equivalent-vector-of-random-variables-from-covarience-matrix/37764#37764 Answer by Darsh Ranjan for Computing equivalent vector of random variables from covarience matrix Darsh Ranjan 2010-09-04T23:39:56Z 2010-09-04T23:39:56Z <p>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)$.)</p> <p>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 <em>uncorrelated</em> 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.) </p>