# generate positive definite matrix with identical diagonal elements

I want to generate a covariance matrix, with the constraint that all the diagonal elements are equal to 1:

Cov[i,i] = 1 for i = 1...dim

The ways I've seen so far to generate a covariance matrix is either use a Wishart distribution, or generate a random matrix and multiply it by itself:

X = rand(dim,dim)
Cov = X.T*X


I can't think of a way to force either of these solutions to have 1s in their diagonal

EDIT:

If I want my covariance matrix to be (d x d), then I only have d*(d-1)/2 parameters to generate. Because the diagonal is 1 and the matrix is symmetric. What I'm 'really' trying to do is to generate a
d*(d-1)/2 vector so that when I fill the covariance matrix with these values, the resulting matrix is positive-definite.

• What distribution do you want these matrices to have? If you don't care, then fix any one matrix you can find and use it forever! – whuber Aug 8 '17 at 20:32

Every correlation matrix is a valid covariance matrix with ones on the diagonal. In R, you could just do cor(X) for any matrix X. In Python, numpy.corrcoef(X).