Search code examples
pythonrstatisticscovariance

How to generate a random covariance matrix in Python?


So I would like to generate a 50 X 50 covariance matrix for a random variable X given the following conditions:

  1. one variance is 10 times larger than the others
  2. the parameters of X are only slightly correlated

Is there a way of doing this in Python/R etc? Or is there a covariance matrix that you can think of that might satisfy these requirements?

Thank you for your help!


Solution

  • OK, you only need one matrix and randomness isn't important. Here's a way to construct a matrix according to your description. Start with an identity matrix 50 by 50. Assign 10 to the first (upper left) element. Assign a small number (I don't know what's appropriate for your problem, maybe 0.1? 0.01? It's up to you) to all the other elements. Now take that matrix and square it (i.e. compute transpose(X) . X where X is your matrix). Presto! You've squared the eigenvalues so now you have a covariance matrix.

    If the small element is small enough, X is already positive definite. But squaring guarantees it (assuming there are no zero eigenvalues, which you can verify by computing the determinant -- if the determinant is nonzero then there are no zero eigenvalues).

    I assume you can find Python functions for these operations.