Generating symmetric Positive Definite Matrix from random vector multiplication yield singular matrix

cholesky decompositioneigenvalues-eigenvectorspositive definite

This might seem a very naive or ignorant question, but I am not able to give myself a satisfactory explanation.

Suppose i want to generate a random symmetric positive definite matrix. I was under the impression that every matrix that follows,
$$
x^TAx > 0\\ ~\forall x \ne0 \in R
$$

is a positive definite matrix. So if I take a random vector > 0 and multiply it with its transpose I should get a positive definite matrix. However octave disagrees!

x = rand(5,1)*5;
A = x*x'
ans =

    7.6497    3.5375    4.7485    6.9960   13.1888
    3.5375    1.6359    2.1959    3.2352    6.0990
    4.7485    2.1959    2.9476    4.3427    8.1869
    6.9960    3.2352    4.3427    6.3982   12.0618
   13.1888    6.0990    8.1869   12.0618   22.7388

chol(A)
error: chol: input matrix must be positive definite

Should the above matrix not be positive definite?
When i dug up further, i found out that some of its eigenvalues are negative but even adding small diagonal part and making them positive didnt help, it only worked once I add values > 1e-14 to diagonal

eig(A)
ans =

  -8.4114e-16
  -4.2296e-16
   1.1886e-16
   5.4291e-15
   4.1370e+01

eig(A + eye(5)*1e-15)
ans =

   4.4863e-16
   5.0505e-16
   1.1676e-15
   7.9287e-15
   4.1370e+01


chol(A + eye(5)*1e-15)
error: chol: input matrix must be positive definite

chol(x*x' + eye(5)*1e-14)
ans =

   2.76581   1.27901   1.71686   2.52947   4.76852
   0.00000   0.00000   0.00000   0.00000   0.00000
   0.00000   0.00000   0.00000   0.00000   0.00000
   0.00000   0.00000   0.00000   0.00000   0.00000
   0.00000   0.00000   0.00000   0.00000   0.00000

1.) what is the surest way of generating a symmetric positive definite matrix with a random vector?
2.) how much diagonal value I need to add?

I again apologize if this question seem bit juvenile or ignorant. I am trying to grasp linear algebra.

Best Answer

When you multiply vector $x$ by its transpose $x^T$, you will get positive semidefinite matrix with only one nonzero eigenvalue.

Instead you can generate $X=\mathrm{rand}(5,5)$ and set $A=X^TX$. Or you can also generate diagonal matrix with positive diagonal elements and perform unitary transformation, i.e. $UDU^*$, where $D$ is diagonal and $U$ is unitary.

Related Question