Mata is a full-blown programming language that compiles what you type into bytecode, optimizes it, and executes it fast. Behind the scenes, many of Stata’s commands are implemented using Mata. You can use Mata to implement big systems, or you can use it interactively. Read about all of Mata's features.
To enter Mata, type mata at Stata’s dot prompt. To exit, type end at Mata’s colon prompt:
. mata | ||
mata (type end to exit) | ||
: sqrt(-4) | ||
. | ||
: sqrt(-4+0i) | ||
2i | ||
: end | ||
Mata supports real and complex numbers, binary and text strings (up to 2,147,483,647 characters long), and, for serious programming problems, even pointers! Mata also supports object–oriented programming, or class programming, with classes, methods, inheritance, constructors and destructors, and more.
Mata uses LAPACK routines from the Intel® Math Kernel Library (MKL) for its advanced matrix features, such as Cholesky decomposition, LU decomposition, QR decomposition, SV decomposition, eigenvalues and eigenvectors, and solvers and inverters.
Mata supports matrices that are views onto, not copies of, the data. Say you have loaded a dataset of 200,000 observations and 150 variables, and you need a matrix of 80 of those variables in 180,000 of the observations. Rather than requiring 110 megabytes, Mata needs only 640 bytes.
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. Here is Mata’s polynomial solver:
numeric rowvector polysolve(numeric vector y, numeric vector x) { numeric rowvector res, c, empty real scalar i, j, n if (cols(y) != cols(x) | rows(y) != rows(x)) _error(3200) if ((n=length(x)) == 0) _error(3200) res = (iscomplex(y) | iscomplex(x) ? 0i : 0) for (j=1; j<=n; j++) { c = (1) for (i=1; i<=n; i++) { if (i != j) { c = polymult(c, (-x[i],1) :/ (x[j]-x[i])) } } res = polyadd(res, y[j] :* c) } while (res[cols(res)]==0) res = res[|1,1 \ 1,cols(res)-1|] return(res) }
Much of Mata is written in Mata.
We recommend users begin reading Introduction and first session in Mata Reference Manual.
We also recommend the Stata Press book The Mata Book: A Book for Serious Programmers and Those Who Want to Be.
Learn
Free webinars
NetCourses
Classroom and web training
Organizational training
Video tutorials
Third-party courses
Web resources
Teaching with Stata
© Copyright 1996–2024 StataCorp LLC. All rights reserved.
×
We use cookies to ensure that we give you the best experience on our website—to enhance site navigation, to analyze usage, and to assist in our marketing efforts. By continuing to use our site, you consent to the storing of cookies on your device and agree to delivery of content, including web fonts and JavaScript, from third party web services.
Cookie Settings
Last updated: 16 November 2022
StataCorp LLC (StataCorp) strives to provide our users with exceptional products and services. To do so, we must collect personal information from you. This information is necessary to conduct business with our existing and potential customers. We collect and use this information only where we may legally do so. This policy explains what personal information we collect, how we use it, and what rights you have to that information.
These cookies are essential for our website to function and do not store any personally identifiable information. These cookies cannot be disabled.
This website uses cookies to provide you with a better user experience. A cookie is a small piece of data our website stores on a site visitor's hard drive and accesses each time you visit so we can improve your access to our site, better understand how you use our site, and serve you content that may be of interest to you. For instance, we store a cookie when you log in to our shopping cart so that we can maintain your shopping cart should you not complete checkout. These cookies do not directly store your personal information, but they do support the ability to uniquely identify your internet browser and device.
Please note: Clearing your browser cookies at any time will undo preferences saved here. The option selected here will apply only to the device you are currently using.