1

Is there a function (preferably in python ecosystem) to sample covariance matrices?

I’m aware of an LKJ prior, so I might use this. (I believe this is a prior in correlation not covariance but the conversion is known and tractable.)

For context, I’m working on some randomization algorithms and want to battle test them for robustness. So having the ability to randomly generate covariance matrices would be helpful. I could use these matrices and mean vectors to sample from multivariate Gaussian then use this data in randomization function.

This benchmark performance would be used to make claims about randomization algorithm efficacy.

jbuddy_13
  • 3,000
  • 2
    A simple trick I use as a first pass when checking new code is to just sample a matrix $\mathbf{A}$ from any distribution with iid entries, and then $\Sigma=\mathbf{A}\mathbf{A}^\top$ is a covariance matrix. As an added bonus, its eigenvalues will not be super well behaved (the square of whatever that iid dist woulda given you in terms of condition number). In Python, that could look like: – John Madden Oct 20 '23 at 21:46
  • 2
    A = np.random.normal(size=[M,M]); SIGMA = A.T@A. – John Madden Oct 20 '23 at 21:46

1 Answers1

1

You can refer to Supplementary Note 8 of the following paper:

Sani, O. G., Abbaspourazad, H., Wong, Y. T., Pesaran, B., & Shanechi, M. M. (2021). Modeling behaviorally relevant neural dynamics enabled by preferential subspace identification. Nature Neuroscience, 24(1), 140-149.

The authors provide a generic pseudo code for sampling covariance matrices:

enter image description here

You may also be interested in this answer.

Camille Gontier
  • 2,616
  • 6
  • 13