为什么通过Cholesky分解反转正定矩阵比使用numpy的常规反转慢?

时间:2016-09-19 13:53:07

标签: python r numpy scipy matrix-inverse

数学上已知通过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 更快。然而,当我对两者进行实验时,事实证明Cholesky分解的表现更差!

R

后者缩短了。为什么是这样?在chol2inv(chol(X))中,solve(X)通常比 <table id="companies" class="table table-striped table-bordered table-hover"> <thead> <tr> <th class="center"> Id </th> <th>RegNo</th> <th>Name</th> <th class="hidden-480">Industry</th> <th class="hidden-phone"> Size </th> <th class="hidden-480">LineOfDefence</th> <th>Address</th> </tr> </thead> </table> 更快。

3 个答案:

答案 0 :(得分:1)

我在1000x1000矩阵上进行比较,而通过Cholesky的反转速度大约是其两倍。

答案 1 :(得分:0)

也许您的矩阵太小。我只是在Matlab中使用Cholesky分解然后进行LU分解测试了矩阵反转,得到了一个2×2的矩阵。使用Cholesky进行999999次重复需要5秒,而使用LU仅需3.4秒。确实,Cholesky算法及其后向替换算法具有较小的大O结果,但该结果是渐近结果,仅适用于大矩阵。

#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.

答案 2 :(得分:0)

np.linalg.inv使用并行处理。运行两个功能时,请检查您的CPU峰值。如果您使用并行处理来运行多个inv或Cholesky,则会发现Cholesky更快。