22
Stata Technical Bulletin
STB-20
Summary
There is no question that the display-and-infile approach is the fastest way to run simulations in Stata. The 10,000-
replication simulation presented above, I estimate, would have taken only 8.5 minutes rather than the 13.5 minutes actually
observed. Nevertheless, I continue to reject using that method because it does not allow me to maintain logs of what I have done.
Moreover, it is easy to misinterpret these timings unless one remembers that they are absolute, not relative. For instance, the
difference in execution time for 10,000 replications is 5 minutes and that difference remains 5 minutes regardless of the complexity
of the simulation. Thus, I recently performed simulations involving bootstrapped quantile regression (10,000 replications of 50
replications, meaning estimation of 500,000 quantile regressions). This simulation took over 8 hours. The difference in execution
time between the logable buffered append and display-and-infile is still only 5 minutes.
More importantly, buffered append is substantially faster than the simple append method—in fact, regressions of time on
number of replications suggest that the buffered append method is nearly twice as fast once the fixed costs of the routines are
eliminated (for buffered append, each added replication is estimated as costing .0847 seconds; for the simple append, .1604
seconds; and the ratio is thus .1604/.0847 ≈ 1.89). As boot and bsqreg are currently implemented in terms of the simple-append
method, a doubling of performance should be possible by reimplementing these routines in terms of post.
References
Hamilton, L. C. 1991. ssi1: Monte Carlo simulation. Stata Technical Bulletin 1: 25-28.
ssi6.1 Simplified Monte Carlo simulations
William Gould, Stata Corporation, FAX 409-696-4601
The syntax of the simul command is
simul progname, reps(#) [ args (whatever) dots ]
simul eases the programming task of performing Monte Carlo type simulations. progname is the name of a program that
performs a single simulation. Typing ‘simul progname, reps(#) ’ iterates progname for # replications and collects the results.
simul calls progname two ways. At the outset, simul issues “progname ?” and expects progname to set the global macro
$S_1 to contain a list of variable names under which results are to be stored. Thereafter, simul issues straight “progname” calls
and expects it to perform a single simulation and to store the results using post. Details of post can be found in the insert
above, but enough information is provided below to use post successfully.
Options
reps(#) is not optional—it specifies the number of replications to be performed.
args (whatever) specifies any arguments to be passed to progname on invocation. The query call is then of the form “progname
w whatever” and subsequent calls of the form “progname whatever”.
dots requests a dot be placed on the screen at the beginning of every call to progname, thus providing entertainment during a
long simulation.
Remarks
progname must have the following outline:
program define progname
if l.'f-..==..?.. {
global S_1 "aaribble names'1
exit
perform single simulation
post results
end