473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

An error of matrix inversion using NumPy

Hi dear all,
I am using Python2.4.2+NumPy1.0.1 to deal with a parameter
estimation problem with the least square methods. During the
calculations, I use NumPy package to deal with matrix operations,
mostly matrix inversion and trasposition. The dimentions of the
matrices used are about 29x19,19x19 and 29x29.

During the calculation, I noticed an apparent error of
inverion of a 19x19 matrix. Denote this matrix as KK, U=KK^ -1, I
found the product of U and KK is not equivalent to unit matrix! This
apparently violate the definition of inversion. The inversion is
through the function linalg.inv().

I have checked that det(KK)=-1.2E+40. At first, I thought the
error may be caused by such a large determinant, so I scaled it as
LL=KK/100, then invert LL. Since det(LL)=11.5 and all its elements are
within -180.0 to 850.0, this seems easier. But the result is still not
correct, the product of LL^-1 thus obtained and LL still not unit
matrix ... At the same time, the inversion results of some 29x19
matrices are correct.

So, can you tell me what goes wrong? Is this a bug in
Numpy.linalg? How to deal with this situation? If you need, I can
post the matrix I used below, but it is so long,so not at the moment.

Thanks in advance!

Apr 4 '07 #1
6 6889
lancered wrote:
Hi dear all,
...........
matrices are correct.

So, can you tell me what goes wrong? Is this a bug in
Numpy.linalg? How to deal with this situation? If you need, I can
post the matrix I used below, but it is so long,so not at the moment.
........

presumably the matrix KK is actually some kind of normal matrix obtained from
the data. So you have say n variables and m observations the data matrix is than
an n x m real valued thing say D then you want the inverse of something like D'D
ie an n by n thing. Typically the data D is de-meaned and normalized by the
column norms so that you end up with a fairly well scaled problem.

A long time ago I used Numeric+python to do exactly this sort of calculation
with excellent results and the matrices were as large or larger eg 100 x 100 and
above. I don't think the underlying numeric routines have changed that much. If
your matrix is symmetric then you should certainly be using

Even if you can't post the matrix, perhaps you should indicate how you proceed
from data to matrix. Another problem is that a large determinant is no guarantee
of stability for the inversion. If the largest eigenvalue is 10**100 and the
smallest 10**-200 I probably have an ill determined problem; surprisingly easy
to achieve :(
--
Robin Becker

Apr 4 '07 #2
Robin Becker wrote:
lancered wrote:
h. If
your matrix is symmetric then you should certainly be using
.....

a qr decomposition I meant to say :)
--
Robin Becker

Apr 4 '07 #3
Here is the eigenvalues of KK I obtained:
>>linalg.eigvals(KK)
array([ 1.11748411e+05, 3.67154458e+04, 3.41580846e+04,
2.75272440e+04, 2.09790868e+04, 1.86242332e+04,
8.68628325e+03, 6.66127732e+03, 6.15547187e+03,
4.68626197e+03, 3.17838339e+03, 2.84888045e+03,
1.88279736e+03, 1.32427574e+03, 1.04946287e+03,
5.79303171e+02, 3.83111876e+02, 4.93826556e-12,
1.50263232e-12])

You are right. The ratio of max/min eigenvalues is 7.4368432669e+016
Maybe this exceed the of precision of my machine?

Is there any tricks for me to be able to deal with this matrix
correctly with
NumPy?


On Apr 4, 3:58 pm, Robin Becker <r...@reportlab.comwrote:
lancered wrote:
Hi dear all,
..........
matrices are correct.
So, can you tell me what goes wrong? Is this a bug in
Numpy.linalg? How to deal with this situation? If you need, I can
post the matrix I used below, but it is so long,so not at the moment.

.......

presumably the matrix KK is actually some kind of normal matrix obtained from
the data. So you have say n variables and m observations the data matrix is than
an n x m real valued thing say D then you want the inverse of something like D'D
ie an n by n thing. Typically the data D is de-meaned and normalized by the
column norms so that you end up with a fairly well scaled problem.

A long time ago I used Numeric+python to do exactly this sort of calculation
with excellent results and the matrices were as large or larger eg 100 x 100 and
above. I don't think the underlying numeric routines have changed that much. If
your matrix is symmetric then you should certainly be using

Even if you can't post the matrix, perhaps you should indicate how you proceed
from data to matrix. Another problem is that a large determinant is no guarantee
of stability for the inversion. If the largest eigenvalue is 10**100 and the
smallest 10**-200 I probably have an ill determined problem; surprisingly easy
to achieve :(
--
Robin Becker

Apr 4 '07 #4
lancered wrote:
Here is the eigenvalues of KK I obtained:
>>linalg.eigvals(KK)
array([ 1.11748411e+05, 3.67154458e+04, 3.41580846e+04,
2.75272440e+04, 2.09790868e+04, 1.86242332e+04,
8.68628325e+03, 6.66127732e+03, 6.15547187e+03,
4.68626197e+03, 3.17838339e+03, 2.84888045e+03,
1.88279736e+03, 1.32427574e+03, 1.04946287e+03,
5.79303171e+02, 3.83111876e+02, 4.93826556e-12,
1.50263232e-12])

You are right. The ratio of max/min eigenvalues is 7.4368432669e+016
Maybe this exceed the of precision of my machine?

Is there any tricks for me to be able to deal with this matrix
correctly with
NumPy?
........

You have to be very careful with a set of eigenvalues like the above. The
implication is that you have a rank deficiency of two ie either you have to
small a number of observations or there's something fishy about the model (two
of the data matrix columns can be written as linear combinations of the others
or two of the columns are structurally zero). If you really believe the data to
be good then normalize to unit column length first ie try using

[d(1)/||d(1)||.....d(n)/||d(n)||]

and see if the eigenvalues are still zero (this will only be of benefit if the
data is vastly different in scale).

I often got bad results, but that was mostly bad programming and or measuring
the voltage of the earth wire by mistake :). Some problems naturally have a zero
at the origin.
--
Robin Becker

Apr 4 '07 #5
In article <11********************@o5g2000hsb.googlegroups.co m>,
"lancered" <wa*****@gmail.comwrote:
Here is the eigenvalues of KK I obtained:
>>linalg.eigvals(KK)
array([ 1.11748411e+05, 3.67154458e+04, 3.41580846e+04,
2.75272440e+04, 2.09790868e+04, 1.86242332e+04,
8.68628325e+03, 6.66127732e+03, 6.15547187e+03,
4.68626197e+03, 3.17838339e+03, 2.84888045e+03,
1.88279736e+03, 1.32427574e+03, 1.04946287e+03,
5.79303171e+02, 3.83111876e+02, 4.93826556e-12,
1.50263232e-12])

You are right. The ratio of max/min eigenvalues is 7.4368432669e+016
Maybe this exceed the of precision of my machine?

Is there any tricks for me to be able to deal with this matrix
correctly with
NumPy?
That sounds large to me, too. Close to the floating point accuracy.
The problem is not with NumPy,but with double precision numbers. No
routine can save you if the condition number is large. However, several
people here have noted that you might be able to solve your problem by
avoiding inverting the matrix in the first place. In other words,
depending on your particular problem, there may be other ways to solve
it beside brute force inversion. Can you Use a QR or SVD approach?

-- Lou Pecora (my views are my own) REMOVE THIS to email me.
Apr 4 '07 #6
lancered wrote:
>

So, can you tell me what goes wrong? Is this a bug in
Numpy.linalg? How to deal with this situation? If you need, I can
post the matrix I used below, but it is so long,so not at the moment.
As you discovered, it is very likely your problem is a very high
condition number.

The easiest thing to do is to use

numpy.linalg.pinv

to perform a pseudo-inverse which will only use the singular-values that
are well-conditioned to compute the inverse.

This will still not give you an exact identity, but at least you will
know you aren't amplifiying low-valued singular vectors.

-Travis

Apr 4 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: vishnu mahendra | last post by:
hello to all, can any one please give me an algorithm to find inverse of a matrix of order n rows and m columns. thank you in advance, vishnu.
10
by: Ing. Carlos Villaseñor M. | last post by:
Hi everybody! I have developed in C# and got in a news group a math class that make matrix operations, eigenvals, eigenvecs, stat functions and much more, but now I trying to develop software in...
14
by: Paul McGuire | last post by:
I've posted a simple Matrix class on my website as a small-footprint package for doing basic calculations on matrices up to about 10x10 in size (no theoretical limit, but performance on inverse is...
5
by: =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?= | last post by:
Hello! I'm trying to find what package I should use if I want to: 1. Create 3d vectors. 2. Normalize those vectors. 3. Create a 3x3 rotation matrix from a unit 3-d vector and an angle in...
3
by: lancered | last post by:
Hi dear all, I am using Python2.4.2+NumPy1.0.1 to deal with a parameter estimation problem with the least square methods. During the calculations, I use NumPy package to deal with matrix...
14
by: James Stroud | last post by:
Hello All, I'm using numpy to calculate determinants of matrices that look like this (13x13):
6
by: amitsoni.1984 | last post by:
Hi, Is there any direct function for matrix multiplication in Python or any of its packages? or do we have to multiply element by element? Thank you, Amit
2
by: devnew | last post by:
hi i am looking for some info about mapping btw values in an array and corresponding columns of a matrix i have an numpy array= and a numpy matrix object= matrix((, , , ))
3
by: PeteJ | last post by:
Hello all.. I wrote code in C to invert n x n matrices, where 1<n<39 and the matrices are guaranteed to be invertible. Prior to this task, I haven't really seen linear algebra since my sophmore...
1
by: dazzler | last post by:
Hi! I have problem with numpy, multiplying with an inversed matrix will crash python :( this works fine: from numpy import matrix A = matrix(,]) B = matrix(,]) print A.I #inverse matrix
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.