[SciPy-User] qr decompostion gives negative q, r ?
Virgil Stokes
vs@it.uu...
Tue Nov 20 17:10:19 CST 2012
On 2012-11-20 23:57, Charles R Harris wrote:
>
>
> On Tue, Nov 20, 2012 at 3:49 PM, Virgil Stokes <vs@it.uu.se
> <mailto:vs@it.uu.se>> wrote:
>
> On 2012-11-20 23:13, Skipper Seabold wrote:
> > On Tue, Nov 20, 2012 at 5:03 PM, Virgil Stokes <vs@it.uu.se
> <mailto:vs@it.uu.se>> wrote:
> >> On 2012-11-20 22:33, Da?id wrote:
> >>> The QR descomposition is finding two matrices with certain
> properties such that:
> >>>
> >>> A = Q·R
> >>>
> >>> But, if both Q and R are multiplied by -1, (-Q)·(-R) = Q·R =
> A, still
> >>> the same matrix. If Q is orthogonal, -Q is also. The sign is,
> >>> therefore, arbitrary.
> >>>
> >>> On Tue, Nov 20, 2012 at 12:01 AM, Virgil Stokes <vs@it.uu.se
> <mailto:vs@it.uu.se>> wrote:
> >>>> I am using the latest versions of numpy (from
> >>>> numpy-1.7.0b2-win32-superpack-python2.7.exe) and scipy (from
> >>>> scipy-0.11.0-win32-superpack-python2.7.exe ) on a windows 7
> (32-bit)
> >>>> platform.
> >>>>
> >>>> I have used
> >>>>
> >>>> import numpy as np
> >>>> q,r = np.linalg.qr(A)
> >>>>
> >>>> and compared the results to what I get from MATLAB (R2010B)
> >>>>
> >>>> [q,r] = qr(A)
> >>>>
> >>>> The q,r returned from numpy are both the negative of the q,r
> returned
> >>>> from MATLAB for the same matrix A. I believe that theq,r
> returned from
> >>>> MATLAB are correct. Why am I getting their negative from numpy?
> >>>>
> >>>> Note, I have tried this on several different matrices ---
> numpy always
> >>>> gives the negative of MATLAB's.
> >>>>
> >>>> _______________________________________________
> >>>> SciPy-User mailing list
> >>>> SciPy-User@scipy.org <mailto:SciPy-User@scipy.org>
> >>>> http://mail.scipy.org/mailman/listinfo/scipy-user
> >>> _______________________________________________
> >>> SciPy-User mailing list
> >>> SciPy-User@scipy.org <mailto:SciPy-User@scipy.org>
> >>> http://mail.scipy.org/mailman/listinfo/scipy-user
> >> Thanks David,
> >> I am well aware of this; but, I am using the QR decomposition for a
> >> convariance (PD matrix) and the negative R is not very useful
> in this
> >> case and the numpy result, IMHO should not be the default.
> >>
> > Can't you guard against this in your code?
> >
> >> Why is numpy/Python different from that returned by MATLAB and
> >> MATHEMATICA? This makes translations rather tricky and one
> begins to
> >> wonder if there are other differences.
> > It can often depend on the version of the underlying LAPACK
> functions
> > used (or maybe even where/how it was compiled). In my
> experience, I've
> > seen linear algebra functions in MATLAB give different results up to
> > an arbitrary sign when I know for a fact they were using the same
> > underling LAPACK routine. I later upgraded the LAPACK that I used to
> > build scipy and the signs agreed. I do not know if MATLAB does any
> > kind of normalization after the fact, but you could file an issue or
> > better yet provide a PR for the sign check in scipy if it's
> something
> > you don't want to check for in your code in the future. The
> beauty of
> > scipy is that you can look at the code to see why you're getting the
> > results you're getting. You can find out the LAPACK version and then
> > look at the helper functions that calls these routines to see what's
> > going on. Good luck figuring that out with MATLAB, etc.
> >
> > Skipper
> >
> > Skipper
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User@scipy.org <mailto:SciPy-User@scipy.org>
> > http://mail.scipy.org/mailman/listinfo/scipy-user
> Ok Skipper,
> Unfortunately, things are worse than I had hoped, numpy sometimes
> returns the negative of the q,r and other times the same as MATLAB!
> Thus, as someone has already mentioned in this discussion, the "sign"
> seems to depend on the matrix being decomposed. This could be a
> nightmare to track down.
>
> I hope that I can return to some older versions of numpy/scipy to work
> around this problem until this problem is fixed. Any suggestions
> on how
> to recover earlier versions would be appreciated.
>
>
> But why is it a problem? Why is Matlab "right"? What is the property
> that you
> need to have in the decomposition?
I have already posted an answer to your first question, Chuck.
MATLAB is correct because the results of my application (Kalman filter
using QR factorization of the covariance matrix) are correct -- I have
verified this. The QR factorization is used to propagate the R matrix
and clearly if the "sign" of R changes in an unpredictable manner (at
least I have been unable to predict the "sign" changes that occur in
numpy), then the answer is unlikely to be correct.
I believe that I have answered your questions. If not, then you might
look at a paper/tutorial/book that discusses the "square-root Kalman
filter". Then, if you have any additional questions on my application, I
will try my best to answer them for you.
>
> Chuck
>
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20121121/030bfc7f/attachment-0001.html
More information about the SciPy-User
mailing list