24
Stata Technical Bulletin
STB-20
. drop _all
. set obs 100
. gen x = invnorm(uniform())
. gen true.y = l+2*x
. save truth
Our program is
program define hetero
version 3.1
ɪf ∏-1-∏==∏7∏ {
global S_1 ,,a se_a b se.b,'
exit
>
use truth, clear
gen y = true.y + 2*(invnorm(uniform()) + 'lz*x)
regress y x
post -b [_cons] -se[.cons] _b[x] _se[x]
end
Note the use of ` 1 ' in our statement for generating y. ` 1 ' is an argument. If the argument’s value is 2, then the last part of
the statement is equivalent to “2*x”. We can run 10,000 simulations, setting the argument to 2 by typing
. simul hetero, args(2) reps(10000)
We might then analyze that data and try a different experiment, this time setting the argument to 1.5:
. simul hetero, args(1.5) reps(10000)
Our program hetero could, however, be more efficient because it rereads the file truth once every replication. It would
be better if we could read the data just once and, in fact, we can because we can use the query call to initialize ourselves. A
faster version reads:
program define hetero
version 3.1
ɪf ∏-ι-∏==∏7∏ {
use truth, clear (load the data just once)
global S_1 ,,a se_a b se.b,'
exit
>
gen y = true.y + 2*(invnorm(uniform()) + 'lz*x)
regress y x
post _b [_cons] -seE-Cons] _bEx] _seEx]
drop y (because we will recreate it next time)
end
Assume we plan on replicating the experiment 10,000 times. In our original draft, we used the truth data once per replication,
meaning we performed 10,000 uses. The new version does this only once, saving 9,999 unnecessary uses.
Performance
simul is implemented in terms of post (see insert above) and it must, therefore, be slower. The good news is that it is
not much slower:
N post (sec.) simul (sec.)
100 8.13 8.90
500 49.21 43.83
1000 89.19 87.16
These timings were performed on a 25MHz, DOS 486 computer.
Moreover, on the log-normal example, the direct post implementation took 13.5 minutes to perform 10,000 replications;
the simul solution took 14.5 minutes, for a total cost of 1 minute. post provides more flexibility than simul but, unless that
flexibility is necessary, simul provides a more convenient way to perform Monte Carlo simulations.