Generating a pseudo-random positive definite matrix

I wanted to test the simple Cholesky code that I wrote in C ++. So I create a random lower triangular L and multiply its transpose by creating A.

A = L * Lt;

But my code does not take A. into account. So I tried this in Matlab:

N=200; L=tril(rand(N, N)); A=L*L'; [lc,p]=chol(A,'lower'); p

This outputs a nonzero p value, which means that Matlab also does not take A. into account. I assume randomness generates rank-resistant matrices. I'm right?

Update:

I forgot to mention that the following Matlab code works as indicated by the Malifer below:

N=200; L=rand(N, N); A=L*L'; [lc,p]=chol(A,'lower'); p

The difference is that L is the lower triangle in the first code, not the second. Why is it important?

I also tried the following with scipy after reading A simple algorithm for generating positive semidefinite matrices :

from scipy import random, linalg
A = random.rand(100, 100)
B = A*A.transpose()
linalg.cholesky(B)

But he is wrong:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/scipy/linalg/decomp_cholesky.py", line 66, in cholesky
    c, lower = _cholesky(a, lower=lower, overwrite_a=overwrite_a, clean=True)
  File "/usr/lib/python2.7/dist-packages/scipy/linalg/decomp_cholesky.py", line 24, in _cholesky
    raise LinAlgError("%d-th leading minor not positive definite" % info)
numpy.linalg.linalg.LinAlgError: 2-th leading minor not positive definite

, scipy. ?

,
Nilesh.

+5
4

. L. rand(N,N) , tril(rand(N,N)). , cond(rand(N,N)) cond(tril(rand(N,N))). - 1e3 1e19 , , . - eig(), .

rand(N,N) .

, , , :

http://epubs.siam.org/doi/abs/10.1137/S0895479896312869

+6

, . ,

L=tril(rand(n))

, eig (L) . L * L ', , .

L=L+n*eye(n)

L * L ' :

> cond(L*L')

ans =

1.8400
+1

MATLAB, :

N=200;
L=rand(N, N); 
A=L*transpose(L); 
[lc,p]=chol(A,'lower');
eig(A)
p

p .

0

. , , . , .

5x5

L = tril(rand(5))
L =
      0.72194            0            0            0            0
     0.027804      0.78422            0            0            0
      0.26607     0.097189      0.77554            0            0
      0.96157      0.71437      0.98738      0.66828            0
     0.024571     0.046486      0.94515      0.38009     0.087634

eig(L)
ans =
     0.087634
      0.66828
      0.77554
      0.78422
      0.72194

, . , rand, 0 1, 1/2. , L . log (det (L)). , log . (, , - , log (det (L)) , , .)

Ah, , lambda = 1. n (0,1) . -n. nxn, , exp (-n). n 200, MATLAB ,

exp(-200)
ans =
   1.3839e-87

, , . , L * L ', . . , 20x20 ., . , L * L ', , .

L = tril(rand(20));

cond(L)
ans =
   1.9066e+07

cond(L*L')
ans =
   3.6325e+14

See how much better for the full matrix.

A = rand(20);

cond(A)
ans =
       253.74

cond(A*A')
ans =
        64384
0
source

All Articles