Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Matrix inversion "bug"

From   "FEIVESON, ALAN H. (AL) (JSC-SK) (NASA)" <>
To   "''" <>
Subject   st: RE: Matrix inversion "bug"
Date   Tue, 10 Jun 2003 08:08:50 -0500

Mark - I too have been bitten by this one - and I agree - it is a nuisance
to have to "symmetrize" the matrix by hand before calling -syminv- or to use

I like your idea for a syminv2 function that always returns [(A + A')/2 ]-1
no matter what A is as long as A is square and A+A' is nonsingular.

Al Feiveson

-----Original Message-----
From: Mark Schaffer []
Sent: Monday, June 09, 2003 6:19 PM
To: Statalist
Cc: Mark Schaffer
Subject: st: Matrix inversion "bug"

Dear Statalisters:

Every now and then I encounter a matrix inversion "bug" in Stata.  It 
cropped up yet again last week when an ivreg2 user wrote to me, and I 
finally decided to see what my fellow Statalisters think of it.

Say we have a matrix M which is calculated as X*S*X'.  S is symmetric and 
so is M.  We want to invert M and so the best thing to do is to use
-syminv-.  The Stata programming manual tells us to use -syminv- instead 
of -inv- wherever possible.

The problem is this.  Although M is guaranteed to be symmetric in 
principle, it is not guaranteed to be symmetric in practice.  Tiny rounding 
errors can arise when Stata multiplies matrices.  Once in a while, M will 
be very slightly non-symmetric, but when it is, -syminv- will exit with an 

When I first encountered this, I wrote to Stata Technical Support, and I 
received a very helpful and simple fix: prior to the call to syminv, simply 
do the following:

mat M = (M+M')/2

This makes the matrix symmetric.  Works, no problem.  But...

Arguably, this is just a fix for what is really a Stata bug.  Ought it be 
possible for Stata's code to recognise when the result of a matrix 
multiplication is supposed to be a symmetric matrix?

Whether or not this should be called a "bug", it may still be the case that 
it's not possible for our friends at Stata Corp to fix it.  If so, then 
there are several possibilities:

(1) Prior to using -syminv-, we should always (?) symmetrize the matrix we 
want to invert using the code fragment above.

(2) Any (?) time we call -syminv-, we should use -capture-.  If the 
inversion using -syminv- fails, we should call -inv-.

(3) Neither of the above is very pretty.  An alternative is to ask our 
Stata Corp friends to, say, add a variant of the -syminv- function, say
-syminv2-.  This function would automatically symmetrize a square matrix 
before inverting it.

What do you think?




This e-mail and any files transmitted with it are confidential
and intended solely for the use of the individual or entity to
whom it is addressed.  If you are not the intended recipient
you are prohibited from using any of the information contained
in this e-mail.  In such a case, please destroy all copies in
your possession and notify the sender by reply e-mail.  Heriot
Watt University does not accept liability or responsibility
for changes made to this e-mail after it was sent, or for
viruses transmitted through this e-mail.  Opinions, comments,
conclusions and other information in this e-mail that do not
relate to the official business of Heriot Watt University are
not endorsed by it.
*   For searches and help try:
*   For searches and help try:

© Copyright 1996–2019 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index