It's mathematically known that inverting a positive definite matrix via Cholesky decomposition is faster than just using np.linalg.inv(X)
. However, when I experimented with both and it turns out Cholesky decomposition's performance is worse!
# Inversion through Cholesky
p = X.shape[0]
Ip = np.eye(p)
%timeit scipy.linalg.cho_solve(scipy.linalg.cho_factor(X,lower=True), Ip)
The slowest run took 17.96 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 107 µs per loop
# Simple inversion
%timeit np.linalg.inv(X)
The slowest run took 58.81 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 25.9 µs per loop
The latter took shorter. Why is this? In R
, chol2inv(chol(X))
is usually faster than solve(X)
.
In linear algebra, the Cholesky decomposition or Cholesky factorization (pronounced /ʃəˈlɛski/ shə-LES-kee) is a decomposition of a Hermitian, positive-definite matrix into the product of a lower triangular matrix and its conjugate transpose, which is useful for efficient numerical solutions, e.g., Monte Carlo ...
Cholesky decomposition is approximately 2x faster than LU Decomposition, where it applies. The SciPy implementation and the pure Python implementation both agree, although we haven't calculated the upper version for the pure Python implementation.
Cholesky decomposition or factorization is a powerful numerical optimization technique that is widely used in linear algebra. It decomposes an Hermitian, positive definite matrix into a lower triangular and its conjugate component. These can later be used for optimally performing algebraic operations.
Every Hermitian positive-definite matrix (and thus also every real-valued symmetric positive-definite matrix) has a unique Cholesky decomposition.
I run a comparison on 1000x1000 matrices, and the Inversion through Cholesky was roughly twice as fast.
Perhaps your matrix is too small. I just tested matrix inversion for a $2\times2$ matrix in Matlab using Cholesky decomposition followed by LU decomposition. 999999 repeats take 5 seconds using Cholesky and only takes 3.4 seconds using LU. Indeed the algorithm of Cholesky followed by back substitution has a smaller big O result, but the result is an asymptotic result and only applies for big matrices.
#LU decomposition
tic
for i=1:999999
(V_i(:,:,2)+[0 1e-10;0 0])\eye(2);
end
toc
Elapsed time is 3.430676 seconds.
#Cholesky
tic
for i=1:999999
(V_i(:,:,2))\eye(2);
end
toc
Elapsed time is 4.824175 seconds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With