# [SciPy-user] How to generate random positive definite matrix

Dominique Orban dominique.orban@gmail....
Mon Jun 11 10:09:01 CDT 2007

```David Cournapeau wrote:
> Hi there,
>
>     I need to generate random positive definite matrix, mainly for
> testing purpose. Before, I was generating them using a random matrix A
> given by randn, and computing A'A. Unfortunately, if A is singular, so
> is A'A. Is there a better way to do than testing whether A is singular ?
> They do not need to follow a specific distribution (but I would like to
> avoid them to follow a really special "pattern").

Strictly diagonally dominant matrices are positive definite. So you could generate a random A,
compute AA= A'A and then increase the elements on the diagonal to make sure that

AA[i,i] > sum( abs(AA[i,j]), j != i ),

e.g., compute the sum on the right-hand side and then add 1 to it and assign the result to AA[i,i].

More simply, you could compute A'A + alpha*I for some alpha > 0 of your choice and where I is the
identity matrix. All eigenvalues of this matrix are >= alpha, which make it "safely" positive definite.

Dominique

```