Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple algorithm for generating positive-semidefinite matrices

I want to generate positive random semi-definite matrices. I am looking for an algorithm or more preferably an simple implementation of the algorithm in C, matlab, java or any language.

like image 747
BHS Avatar asked Mar 06 '09 15:03

BHS


People also ask

What is positive semidefinite matrix example?

Example 1.1. The matrix Jn is positive semidefinite because Jn = J′n, Y′JnY = ≥ 0 for Y = (y1,…, yn)′ and Y′JnY = 0 for Y = (1, −1, 0,…, 0)′.

How do you write a positive definite matrix?

A matrix is positive definite if it's symmetric and all its pivots are positive. where Ak is the upper left k x k submatrix. All the pivots will be pos itive if and only if det(Ak) > 0 for all 1 k n. So, if all upper left k x k determinants of a symmetric matrix are positive, the matrix is positive definite.

What is meant by positive semidefinite matrix?

A positive semidefinite matrix is a Hermitian matrix all of whose eigenvalues are nonnegative. A matrix. may be tested to determine if it is positive semidefinite in the Wolfram Language using PositiveSemidefiniteMatrixQ[m].

Is the sum of positive semidefinite matrices?

– The sum of two positive semidefinite matrices is positive semidefinite. – The product of two positive semidefinite matrices need not be positive semidefinite.


3 Answers

  1. generate random matrix
  2. multiply it by its own transposition
  3. you have obtained a positive semi-definite matrix.

Example code (Python):

import numpy as np
matrixSize = 10 
A = np.random.rand(matrixSize, matrixSize)
B = np.dot(A, A.transpose())
print 'random positive semi-define matrix for today is', B
like image 92
vartec Avatar answered Sep 24 '22 02:09

vartec


You need to be clear on your definition of "random". What are your constraints on the resulting matrix? Do you want the coefficients to be uniformly or normally distributed? Do you want the eigenvalues to have a particular distribution? (etc.)

There are a number of ways to generate positive semidefinite matrices M, including:

  1. Given an arbitrary matrix A, compute M = ATA (constructing a Cholesky decomposition)
  2. Given an arbitrary diagonal matrix S with nonnegative diagonal entries, and an orthonormal matrix Q of the same size, compute M = QSQT (constructing a singular value decomposition)

For numerical reasons I'd probably choose the second approach by generating the diagonal matrix with desired properties, then generating Q as the composition of a number of Householder reflections (generate a random vector v, scale to unit length, H = I - 2vvT); I suspect you'd want to use K * N where N is the size of the matrix M, and K is a number between 1.5-3 (I'm guessing on this) that ensures that it has enough degrees of freedom.

You could also generate an orthonormal matrix Q using Givens rotations: pick 2 distinct values from 1 to N and generate a Givens rotation about that pair of axes, with an angle uniformly distributed from 0 to 2 * pi. Then take K * N of these (same reasoning as above paragraph) and their composition yields Q.

edit: I'd guess (not sure) that if you have coefficients that are independently-generated and normally distributed, then the matrix as a whole would be "normally distributed" (whatever that means). It's true for vectors, at least. (N independently-generated Gaussian random variables, one for each component, gives you a Gaussian random vector) This isn't true for uniformly-distributed components.

like image 22
Jason S Avatar answered Sep 25 '22 02:09

Jason S


If you can generate a random matrix in your chosen language, then by using the property that a matrix multiplied by its transpose is positive semi-definte, you can generate a random positive semi-definite matix

In Matlab it would be as simple as

% Generate a random 3x3 matrix
    A = rand(3,3) 
% Multiply by its tranpose
    PosSemDef = A'*A 
like image 10
chillysapien Avatar answered Sep 24 '22 02:09

chillysapien