qr decompostion gives negative q, r ?

classic Classic list List threaded Threaded
46 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Re: qr decompostion gives negative q, r ?

Pauli Virtanen-3
Virgil Stokes <vs <at> it.uu.se> writes:
[clip]
> Of course you are correct Jason, and I have not checked every QR decomposition
> in MATLAB and numpy. However, for my particular application I do know that the
> R returned from MATLAB always has diagonal elements > 0, while this is not the
> case for my installation of numpy.

It is incorrect to assume this. Also Matlab does not guarantee
that R is PD. That you get it is just due to lucky choice
of matrices and the MATLAB version/platform.

Compare (these are on the same machine!):

MATLAB(R) Version 7.10.0.499 (R2010a) 64-bit (glnxa64) February 5, 2010

>> A=[707.106781, 0.000549702852, 1.66675481e-19;-35.3553391, 70.7104659,
1.66675481e-19;0.0, -3.97555166, 0.00707106781;-707.106781, -0.000648214647,
1.66675481e-19;35.3553391, -70.7104226, 1.66675481e-19;0.0, 3.97560687,
-0.00707106781;0.0, 0.0, 0.0;0.0, 0.0, 0.0;0.0, 0.0, 0.0]; [Q,R]=qr(A,0); R

R =

   1.0e+03 *

    1.0012   -0.0050    0.0000
         0    0.1000   -0.0000
         0         0    0.0000


MATLAB(R) R2012a (7.14.0.739) 64-bit (glnxa64) February 9, 2012

>> A=[707.106781, 0.000549702852, 1.66675481e-19;-35.3553391, 70.7104659,
1.66675481e-19;0.0, -3.97555166, 0.00707106781;-707.106781, -0.000648214647,
1.66675481e-19;35.3553391, -70.7104226, 1.66675481e-19;0.0, 3.97560687,
-0.00707106781;0.0, 0.0, 0.0;0.0, 0.0, 0.0;0.0, 0.0, 0.0]; [Q,R]=qr(A,0); R

R =

   1.0e+03 *

   -1.0012    0.0050    0.0000
         0   -0.1000    0.0000
         0         0   -0.0000


These, and the results returned by Numpy/Scipy are all correct QR
decompositions. There are several signs in QR that can be freely
chosen, and in general there is no particular reason to stick with
a given choice.

Cheers,
Pauli Virtanen


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: qr decompostion gives negative q, r ?

Charles R Harris


On Thu, Nov 22, 2012 at 8:10 AM, Pauli Virtanen <[hidden email]> wrote:
Virgil Stokes <vs <at> it.uu.se> writes:
[clip]
> Of course you are correct Jason, and I have not checked every QR decomposition
> in MATLAB and numpy. However, for my particular application I do know that the
> R returned from MATLAB always has diagonal elements > 0, while this is not the
> case for my installation of numpy.

It is incorrect to assume this. Also Matlab does not guarantee
that R is PD. That you get it is just due to lucky choice
of matrices and the MATLAB version/platform.

Compare (these are on the same machine!):

MATLAB(R) Version 7.10.0.499 (R2010a) 64-bit (glnxa64) February 5, 2010

>> A=[707.106781, 0.000549702852, 1.66675481e-19;-35.3553391, 70.7104659,
1.66675481e-19;0.0, -3.97555166, 0.00707106781;-707.106781, -0.000648214647,
1.66675481e-19;35.3553391, -70.7104226, 1.66675481e-19;0.0, 3.97560687,
-0.00707106781;0.0, 0.0, 0.0;0.0, 0.0, 0.0;0.0, 0.0, 0.0]; [Q,R]=qr(A,0); R

R =

   1.0e+03 *

    1.0012   -0.0050    0.0000
         0    0.1000   -0.0000
         0         0    0.0000


MATLAB(R) R2012a (7.14.0.739) 64-bit (glnxa64) February 9, 2012

>> A=[707.106781, 0.000549702852, 1.66675481e-19;-35.3553391, 70.7104659,
1.66675481e-19;0.0, -3.97555166, 0.00707106781;-707.106781, -0.000648214647,
1.66675481e-19;35.3553391, -70.7104226, 1.66675481e-19;0.0, 3.97560687,
-0.00707106781;0.0, 0.0, 0.0;0.0, 0.0, 0.0;0.0, 0.0, 0.0]; [Q,R]=qr(A,0); R

R =

   1.0e+03 *

   -1.0012    0.0050    0.0000
         0   -0.1000    0.0000
         0         0   -0.0000


These, and the results returned by Numpy/Scipy are all correct QR
decompositions. There are several signs in QR that can be freely
chosen, and in general there is no particular reason to stick with
a given choice.


The term 'positive definite' doesn't really apply either. Just making the diagonals of r come positive in the qr factorization of a covariance doesn't make the quadratic form associated with r PD:

In [17]: m[0]
Out[17]:
array([[ 1. , -0.9],
       [-0.9,  1. ]])

In [18]: eigvalsh(m[0])
Out[18]: array([ 0.1,  1.9])

In [19]: rp[0]
Out[19]:
array([[ 1.3453624 , -1.33792946],
       [ 0.        ,  0.14122589]])

In [20]: eigvalsh(.5*(rp[0].T + rp[0]))
Out[20]: array([-0.15670585,  1.64329415])


Chuck


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Final (summary) on: qr decompostion gives negative q, r ?

Virgil Stokes
After studying the qr decomposition using both numpy and MATLAB, I now realize
that what I thought to be true about the results of the qr decomposition was
incorrect. That is, as many of the those who replied to my emails on this
pointed out:

1) The "sign" of the returned 'r' matrix really doesn't matter.
2) The same input to MATLAB and numpy  (for a qr decomposition) can give
different results (sign differences), and again this really doesn't matter (at
least in my application).

Thanks:  Charles R. Harris, Pauli Virtanen, Sturla Molden, Alejandro Weinstein,
Matthieu Brucher, Alex Leach, Jason Grout, Skipper Seabold, Robert Kern, and
David Menhur for forcing me to think a little more about this.

I am very grateful for your help. The final results (smoothed states and
covariances) obtained from my Python/numpy implementation are the same as those
returned from the original MATLAB implementation :-)

Best regards,
--V

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Final (summary) on: qr decompostion gives negative q, r ?

Daπid
Glad we were useful.

On Sat, Nov 24, 2012 at 3:55 PM, Virgil Stokes <[hidden email]> wrote:
> I am very grateful for your help. The final results (smoothed states and
> covariances) obtained from my Python/numpy implementation are the same as those
> returned from the original MATLAB implementation :-)

I am curious: what about timing? Is any of them significantly faster
than the other?
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Final (summary) on: qr decompostion gives negative q, r ?

Charles R Harris
In reply to this post by Virgil Stokes


On Sat, Nov 24, 2012 at 7:55 AM, Virgil Stokes <[hidden email]> wrote:
After studying the qr decomposition using both numpy and MATLAB, I now realize
that what I thought to be true about the results of the qr decomposition was
incorrect. That is, as many of the those who replied to my emails on this
pointed out:

1) The "sign" of the returned 'r' matrix really doesn't matter.
2) The same input to MATLAB and numpy  (for a qr decomposition) can give
different results (sign differences), and again this really doesn't matter (at
least in my application).

Thanks:  Charles R. Harris, Pauli Virtanen, Sturla Molden, Alejandro Weinstein,
Matthieu Brucher, Alex Leach, Jason Grout, Skipper Seabold, Robert Kern, and
David Menhur for forcing me to think a little more about this.

I am very grateful for your help. The final results (smoothed states and
covariances) obtained from my Python/numpy implementation are the same as those
returned from the original MATLAB implementation :-)


That's a very graceful end to the discussion. Thanks for the update.

Chuck


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Final (summary) on: qr decompostion gives negative q, r ?

Virgil Stokes
In reply to this post by Daπid
On 24-Nov-2012 16:13, Daπid wrote:
> Glad we were useful.
>
> On Sat, Nov 24, 2012 at 3:55 PM, Virgil Stokes <[hidden email]> wrote:
>> I am very grateful for your help. The final results (smoothed states and
>> covariances) obtained from my Python/numpy implementation are the same as those
>> returned from the original MATLAB implementation :-)
> I am curious: what about timing? Is any of them significantly faster
> than the other?
Good question,
MATLAB is faster (how significantly I have not quantified).

--V
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
123