从数学上讲,通过Cholesky分解求正定矩阵比使用np.linalg.inv(X)
要快然而,当我同时使用这两种方法时,结果发现cholesky分解的性能更差!
# 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
后者变短了。这是为什么?在
R
中,chol2inv(chol(X))
通常比solve(X)
快。 最佳答案
我对1000x1000个矩阵进行了比较,通过cholesky的反演速度大约是前者的两倍。