Simulations in Stata

It is fairly easy to do simulations in Stata, but not a whole lot easier than in a compiled language. The user benefits from Stata most when using the estimation functions which converts pages of matrix algebra into a single command. There is no such compression if a simulation is mostly arithmetic asignment statements, which convert nearly one-for-one from one language to another. Mauricio Cáceres Bravo has written an effective blog post on this topic.

If your problm is a long series of -generate- and -replace- statements, you might consider using a traditional compiled language. In brief testing I found that statements such as

set obs 100000000 generate x=_n replace x=x*2+x*x/2

took about 5-10 times as long as equivalent fortran code, even when the compiled code was single threaded, and the Stata had the use of 8 processors. Part of the reason for this is that -generate- and -replace- spend a lot of time (about half for the above statements) in single-threaded processing. Note that the fortran code for the above is only 3 lines longer and fewer characters.

-mata- is another possibility. It will take advantage of multiple cores for all these elementwise calculations, and some others. In my tests, it was faster than single thread fortran for the code above.

Obviously, it isn't worth learning a whole new language to save a few minutes of CPU time, but to save a few months of waiting for results? It might be worthwhile. It depends on your project.