Is there any way to create a random symmetric positive definite matrix ?
-
4$\begingroup$ Define "random." $\endgroup$– David G. StorkDec 14, 2018 at 4:59
-
1$\begingroup$ @David G. Stork sir, thanks for the reply,actually i want to create a positive definite matrix randomly in the range from (-1 ,1) $\endgroup$– revanth royDec 14, 2018 at 5:29
-
3$\begingroup$ @revanthroy That does not define "random". You have to specify a probability distribution. $\endgroup$– Henrik SchumacherDec 14, 2018 at 7:05
2 Answers
dim = 10;
RandomVariate[GaussianOrthogonalMatrixDistribution[dim]]
or
With[{U = RandomVariate[CircularRealMatrixDistribution[dim]]},
U.(RandomReal[{-1, 1}, dim] U\[Transpose])
]
-
2$\begingroup$ Since OP did not specify a precise probability distribution, this question cannot be answered precisely. $\endgroup$ Dec 14, 2018 at 7:09
-
1$\begingroup$ Looks like the first option does not produce an SPD matrix. Try
SeedRandom[0]; dim = 2; m = RandomVariate[GaussianOrthogonalMatrixDistribution[dim]]; Eigenvalues[m]
with output{-1.21836, -0.0868584}
. Both eigenvalues are negative. $\endgroup$– A.G.May 20, 2021 at 15:17
See a-simple-algorithm-for-generating-positive-semidefinite-matrices
Translation to Mathematica:
n = 5; (*size of matrix, change as needed*)
q = Table[RandomReal[{-1, 1}], {n}, {n}];
mat = Transpose[q].q;
PositiveDefiniteMatrixQ[mat]
(*true*)
See also how-to-generate-random-symmetric-positive-definite-matrices-using-matlab
The matrix symmetric positive definite matrix A can be written as , A = Q'DQ , where Q is a random matrix and D is a diagonal matrix with positive diagonal elements. The elements of Q and D can be randomly chosen to make a random A.
Here is the translation of the code to Mathematica
n = 5; (*size of matrix. Change as needed *)
q = Table[RandomReal[{-1, 1}], {n}, {n}];
eigenMean = 2; (*see link above *)
mat = Transpose[q].DiagonalMatrix[eigenMean + Table[RandomReal[{-1, 1}], {n}]].q;
PositiveDefiniteMatrixQ[mat]
(*true*)