简体   繁体   English

如何从Wishart分布生成随机协方差矩阵

[英]How to Generate Random Covariance Matrix from Wishart Distrubtion

I need to generate an nxn, positive-definite covariance matrix for a project. 我需要为项目生成一个nxn正定协方差矩阵。 Drawing from the Wishart distribution was recommended. 建议从Wishart发行中提取图纸。 How do I generate a random covariance matrix in R, ideally also using the Wishart Distribution. 如何在R中生成理想的随机协方差矩阵,理想情况下也要使用Wishart分布。 I've tried rwishart() to get values, but need more help. 我已经尝试过rwishart()来获取值,但是需要更多帮助。 Thanks 谢谢

Please see the function documentation by running ?rWishart . 请通过运行?rWishart查看功能文档。

As you can read, you need to supply the number of samples n you want (ie n random matrices), the degrees of freedom df , and the parameter Sigma to the function. 如您所见,您需要向n提供所需的样本数量n (即n随机矩阵),自由度df和参数Sigma In addition, you also need to decide on the dimension of the wanted random matrix. 另外,您还需要确定所需随机矩阵的维数。

# Set parameters
n <- 1  # Number of matrices
p <- 5  # Dimension
df <- 10  # Degrees of freedom
Sigma <- toeplitz((p:1)/p)  # the matrix parameter of the distribution

# Draw n Wishart distributed matrices
rwish <- drop(rWishart(n, df, Sigma))
print(rwish)

The function generates a 1 xpxp array (effectively a matrix) but we drop the unneeded dimension. 该函数生成一个1 xpxp数组(实际上是一个矩阵),但是我们删除了不需要的维。

You can generate a wishart distributed matrix "manually" by 您可以通过以下方式“手动”生成wishart分布式矩阵

library("mvtnorm")
rgaus <- rmvnorm(n = df, mean = rep(0, p), sigma = Sigma)
rwish2 <- crossprod(rgaus) # crossprod is the same as "t(rgaus) %*% rgaus"

which should help you understand better what the Wishart distribution actually is. 这应该可以帮助您更好地了解Wishart发行版的实际含义。 It is the distribution of the so-called scatter matrix df samples from a zero-mean multivariate normal distribution with variance Sigma . 它是方差Sigma为零均值多元正态分布的所谓散射矩阵df样本的分布。

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

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