gretl offers (limited) support for Monte Carlo simulations. To do such work you should either use the GUI client program in "script mode" (see the Section called Command scripts in Chapter 3 above), or use the command-line client. The command loop opens a special mode in which the program accepts commands to be repeated a specified number of times. Within such a loop, only 8 commands can be used: genr, ols, print, printf, sim, smpl, store and summary. With genr and ols it is possible to do quite a lot. You exit the mode of entering loop commands with endloop: at this point the stacked commands are executed. Loops cannot be nested.
The ols command gives special output in a loop context: the results from each individual regression are not printed, but rather you get a printout of (a) the mean value of each estimated coefficient across all the repetitions, (b) the standard deviation of those coefficient estimates, (c) the mean value of the estimated standard error for each coefficient, and (d) the standard deviation of the estimated standard errors. This makes sense only if there is some random input at each step.
The print command also behaves differently in the context of a loop. It prints the mean and standard deviation of the variable, across the repetitions of the loop. It is intended for use with variables that have a single value at each iteration, for example the error sum of squares from a regression.
The store command (use only one of these per loop) writes out the values of the specified variables, from each time round the loop, to the specified file. Thus it keeps a complete record of the variables. This data file can then be read into the program and analysed.
A simple example of Monte Carlo loop code is shown in Example 9-1.
Example 9-1. Simple Monte Carlo loop
(* create a blank data set with series length 50 *) nulldata 50 genr x = 100 * uniform() (* open a loop, to be repeated 100 times *) loop 100 genr u = normal() (* construct the dependent variable *) genr y = 10*x + 20*u (* run OLS regression *) ols y const x (* grab the R-squared value from the regression *) genr r2 = $rsq (* arrange for statistics on R-squared to be printed *) print r2 (* save the individual coefficient estimates *) genr a = coeff(const) genr b = coeff(x) (* and print them to file *) store foo.gdt a b endloop
This loop will print out summary statistics for the `a' and `b' estimates across the 100 repetitions, and also for the R2 values for the 100 regressions. After running the loop, foo.gdt, which contains the individual coefficient estimates from all the runs, can be opened in gretl to examine the frequency distribution of the estimates in detail. Please note that while comment lines are permitted in a loop (as shown in the example), they cannot run over more than one line.
The command nulldata is useful for Monte Carlo work. Instead of opening a "real" data set, nulldata 50 (for instance) opens an empty data set, with only a constant, with a series length of 50. Constructed variables can then be added using the genr command.
See the seed command in Chapter 11 for information on generating repeatable pseudo-random series.