I have a symmetric positive-definite matrix (e.g. Covariance matrix), and I want to calculate its inverse. In math, I know that it is more efficient to use Cholesky decomposition to invert the matrix, especially if your matrix is big. But I was not sure how does "numpy.lianlg.inv()" works. Say I have the following code:
import numpy as np
X = np.arange(10000).reshape(100,100)
X = X + X.T - np.diag(X.diagonal()) # symmetry
X = np.dot(X,X.T) # positive-definite
# simple inversion:
inverse1 = np.linalg.inv(X)
# Cholesky decomposition inversion:
c = np.linalg.inv(np.linalg.cholesky(X))
inverse2 = np.dot(c.T,c)
Which one is more efficient (inverse1 or inverse2)? If the second one is more efficient, why is numpy.linalg.inv() not using this instead?
invdoes not take advantage of the fact thatcholeskyis triangular — it does not use lapack'sDTRTRIinvis expensive and isn't numerically stable. Usually, you want to multiply the inverse with a vector, i.e., you want to solve a system of equations. In all such cases, it's better to just solve the system using something likelinalg.solve(tellingsolvethat the matrix is symmetric and positive definite will makesolveuse Cholesky). If you want to use the inverse multiple times, compute and store the decomposition for later use.scipy.linalg.lapack.dtrtri