Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get pseudo-determinant of a square matrix with python

Tags:

I have a matrix which fails the singular test in which I am calculating for naive bayes classifier. I am handling the ln(det(sigma)) portion of the equation.

if np.linalg.cond(covarianceMatrix) < 1/sys.float_info.epsilon:
    return np.log(np.linalg.det(covarianceMatrix))
else:
    return a pseudo determinant

When the covariance matrix is singular, I must find the pseudo determinant. How can I do that?

like image 891
kenny Avatar asked Mar 20 '16 16:03

kenny


1 Answers

  1. First compute the eigenvalues of your matrix

    eig_values = np.linalg.eig(covarianceMatrix)

  1. Then compute the product of the non-zero eigenvalues (this equals the pseudo-determinant value of the matrix),

    pseudo_determinent = np.product(eig_values[eig_values > 1e-12])

like image 140
IssamLaradji Avatar answered Oct 11 '22 17:10

IssamLaradji