Verify a matrix is positive semi-definite

I am trying to numerically verify that

A symmetric matrix \mathbf{A} is positive semidefinite if and only if it is a covariance matrix.

Then I need to verify in both directions, i.e.

  1. Given a positive semidefinite matrix \mathbf{A}, show that it is a covariance matrix.
  2. Given a covariance matrix, show that it is positive semidefinite.

However, I am not sure

  1. What properties should a matrix have to be a covariance matrix.
  2. I know I could generate a covariance matrix using the following and I know that cov is positive semidefinite if and only if all of its eigenvalues are non-negative. But it turns out that minimum(eigvals(cov)) is a negative number close to 0 (on the order \sim 10^{-15}), I am not sure if I could conclude that cov is positive semidefinite since numerical reasons.
n = 100
u = randn(n);
cov = u * u'

Any input will be appreciated.

The issue is that what you are creating is not a covariance matrix. Consider this

julia> n = 100

julia> x = randn(n,3); # 3 random variables with sample size n

julia> demean_x = x .- mean(x,dims=1)

julia> cov_x = demean_x'*demean_x./(n-1) # or simply use cov function from Statistics

julia> minimum(eigvals(cov_x))>=0
true

Julia also has a function isposdef under LinearAlgebra, it checks if the matrix is positive definite.

julia> using LinearAlgebra

julia> isposdef(cov_x)
true
1 Like

I am not sure I understand the whole context, but this is a well-known property (covariance matrices are psd, and psd matrices are covariance matrices). I don’t know how you could verify a statement about an uncountably infinite set numerically.

It is actually a homework question. I think the point of this question is that professor wants us to get familiar with basic Julia programming.

To verify numerically 1, you may find https://github.com/mcreel/Econometrics/blob/master/Examples/GLS/cholesky.jl useful.

To verify 2, just check the eigenvalues. Your code is missing the step of averaging to compute the sample covariance, and is not keeping the dimensionality fixed.

1 Like

A Cholesky decomposition requires positive definiteness though; semidefiniteness is not enough.