Solved – how to generate specific random covariance matrices

covariance-matrix

I am trying to create random covariance matrices for three joint gaussian variables. My goal is to sample random covariances matrices that always have correlation between 0.7 and 0.9 (or 0 if there isn’t).

So far I am doing it manually with a repeat until is.positive.definite is true… But I am unable to achieve it, my repeat takes a lot of time because most of my matrices samples return false for the positive.definite.

Is there a library to do this or an simpler approach for this?

On the math side I know I can have correlation between: $X_1$ and $X_2$. $X_2$ and $X_3$. $X_1$ and $X_3$ If I am not mistaken, I can have correlation between the three pair or just one pair, there shouldn’t be any issue. But if there is correlation between two of them, the remaining correlation couldn’t be 0, otherwise the matrix would never be positive definite…

Best Answer

The G-Wishart distribution (Letac & Massam, 2007) is a distribution on positive definite matrices with fixed zeros corresponding to the missing edges of a graph $\mathcal G$ with nodes the indices $(i,j)$ of the associated variates. It has a density of the same form as the Wishart distribution: $$p(\Sigma|\delta,\Xi)\propto|\Sigma|^{(\delta-2)/2}\exp\left\{-\frac{1}{2}\text{tr}(\Sigma^\text{T}\Xi)\right\}$$and enjoys the most useful property that the conditional distributions of the submatrices of $\Sigma$ associated with the cliques of the graph all are standard Wishart, which allows for a Gibbs sampling approach to its simulation.

This distribution is implemented in R via the function rgwish. The graph $\mathcal G$ is described by an adjacency upper-triangular matrix adj that is made of 0's and 1's, with 0's indicating the fixed zeroes of the matrix.

In the current question, this R function can be called until all constraints are satisfied. The matrix $\Xi$ (denoted D in rgwish) can be chosen towards favouring the constraints to be met.