简体   繁体   English

如何在Python中生成随机协方差矩阵?

[英]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: 因此,在以下条件下,我想为随机变量X生成50 X 50协方差矩阵:

  1. one variance is 10 times larger than the others 一个方差是另一个方差的10倍
  2. the parameters of X are only slightly correlated X的参数只是稍微相关

Is there a way of doing this in Python/R etc? 有没有办法在Python / R等中做到这一点? Or is there a covariance matrix that you can think of that might satisfy these requirements? 还是您可以想到可以满足这些要求的协方差矩阵?

Thank you for your help! 谢谢您的帮助!

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. 从50到50的单位矩阵开始。将10分配给第一个(左上方)元素。 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. 为所有其他元素分配一个较小的数字(我不知道什么适合您的问题,也许0.1?0.01?由您决定)。 Now take that matrix and square it (ie compute transpose(X) . X where X is your matrix). 现在取该矩阵并将其平方(即计算transpose(X)。X,其中X是您的矩阵)。 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. 如果小元素足够小,则X已经是正定的。 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. 我假设您可以找到用于这些操作的Python函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM