[SciPy-User] fun with debugging: np.linalg.inv with singular matrix

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Apr 29 17:06:38 EDT 2015


reordering an array changes results by 1021173647741.1094


It's obvious if I calculate 1 / floating_point_noise and
floating_point_noise depends on mathematically irrelevant details.

But it took me a while to remember that noise doesn't follow the simple
math.

inv with nonsingular matrix - great

>>> xm = np.corrcoef(x[:,::-2], rowvar=0)
>>> np.linalg.inv(xm)[:, -3:]
array([[ 0.38794217,  0.40358464,  0.12232308],
       [ 1.21001936,  0.39702137, -0.10164338],
       [ 0.39702137,  1.20088551,  0.03021732],
       [-0.10164338,  0.03021732,  1.03114181]])

>>> np.linalg.inv(xm[::-1,::-1])[::-1,::-1][:, -3:]
array([[ 0.38794217,  0.40358464,  0.12232308],
       [ 1.21001936,  0.39702137, -0.10164338],
       [ 0.39702137,  1.20088551,  0.03021732],
       [-0.10164338,  0.03021732,  1.03114181]])

>>> np.max(np.abs(np.linalg.inv(xm[::-1,::-1])[::-1,::-1] -
np.linalg.inv(xm)))
1.1102230246251565e-16
>>>

inv with singular matrix - blowing up path dependent numerical noise

>>> xm = np.corrcoef(x, rowvar=0)
>>> np.linalg.inv(xm)[:, -3:]
array([[  4.68288729e-02,  -1.86395743e-01,   1.47198710e-02],
       [ -1.35971574e-01,   2.90029902e-01,   3.41148182e-01],
       [  9.77422379e-03,  -1.17494713e-01,  -2.43669915e-01],
       [ -1.00183312e+14,  -1.00183312e+14,  -1.00183312e+14],
       [ -1.00183312e+14,  -1.00183312e+14,  -1.00183312e+14],
       [ -1.00183312e+14,  -1.00183312e+14,  -1.00183312e+14],
       [ -1.00183312e+14,  -1.00183312e+14,  -1.00183312e+14],
       [ -1.00183312e+14,  -1.00183312e+14,  -1.00183312e+14]])

>>> np.linalg.inv(xm[::-1,::-1])[::-1,::-1][:, -3:]
array([[  5.01007835e-02,  -1.84819296e-01,   1.57372647e-02],
       [ -1.46454280e-01,   2.88649950e-01,   3.38451911e-01],
       [  1.13948216e-02,  -1.18632618e-01,  -2.43817631e-01],
       [ -1.01204486e+14,  -1.01204486e+14,  -1.01204486e+14],
       [ -1.01204486e+14,  -1.01204486e+14,  -1.01204486e+14],
       [ -1.01204486e+14,  -1.01204486e+14,  -1.01204486e+14],
       [ -1.01204486e+14,  -1.01204486e+14,  -1.01204486e+14],
       [ -1.01204486e+14,  -1.01204486e+14,  -1.01204486e+14]])

>>> np.max(np.abs(np.linalg.inv(xm[::-1,::-1])[::-1,::-1] -
np.linalg.inv(xm)))
1021173647741.1094
>>>

Josef
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150429/ed50cfb1/attachment.html>


More information about the SciPy-User mailing list