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

Re: st: Mata question: -matrix languages evaluate matrix expressions-?

From (Vince Wiggins, StataCorp)
Subject   Re: st: Mata question: -matrix languages evaluate matrix expressions-?
Date   Tue, 12 Apr 2005 16:17:15 -0500

Renzo Comolli <> asks whether a comment at concerns precision in Mata's
computations.  (Mata is the new matrix programming language in Stata 9.)  The
comment from the website is

> "Everybody knows that matrix languages evaluate matrix expressions,
> such as b=invsym(X'X)*X'y, and Mata is no exception. Because of
> Mata's design, however, it is fast enough to work at the element
> level".

And Renzo asks

> This looks like a disclaimer of some sort about some loss of
> precision, but I got quite understand the implications of this.  Can
> anybody clarify? [...]

Not a disclaimer at all.  The comment concerns speed alone.  

The point is that vectorized computations, such as X'X, when done in internal
C code are fast whether done in Stata or Mata.  If, however, we write the
triple loop required to multiply two matrices by hand, the loop will be about
2,000 times faster when programmed in Mata than when programmed in Stata
ado-code.  While we recommend that you try to avoid looping through a dataset
in Stata ado-code, we make no such recommendation for Mata.  Internal
computations are still faster, but Mata is very fast at executing
instructions and, therefore, at looping.

Mata is what computer scientists call a p-coded (or byte-coded) language,
meaning that what you type in Mata is first compiled into a series of byte
codes (actually short integers). Then these codes are executed, and
executed fast.  For those familiar with Java, it is also a byte-coded
language.  For computing history buffs, Niklaus Wirth's Pascal compiled
to p-code (pseudo-code).  P-code and byte-code mean the same thing.

Whether implemented internally or in Mata itself, good algorithms will
produce good numeric results, at least to the precision computers afford.
Because numerics can be important and problems differ, Mata provides
four different linear solvers: Cholesky decomposition, LU decomposition, QR
decomposition, and singular value decomposition.  What's more, because the
computation of cross products can be particularly sensitive to scale, Mata
provides a quad-precision accumulator.  You can see the online help for the
latter using the help server on the Stata website by pointing your web
browser at

At this point, I have to admit that the two best experts on Mata are in
Europe. They attended the German User Group meeting and are now attending
another conference in Spain.

-- Vince

*   For searches and help try:

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