[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: Matrix inversion "bug"
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: