Loop examples

Monte Carlo example

A simple example of a Monte Carlo loop in "progressive" mode is shown in Example 9-1.

Example 9-1. Simple Monte Carlo loop


	  nulldata 50
	  seed 547
	  genr x = 100 * uniform()
	  # open a "progressive" loop, to be repeated 100 times
	  loop 100 --progressive
	     genr u = 10 * normal()
	     # construct the dependent variable
	     genr y = 10*x + u
	     # run OLS regression
	     ols y const x
	     # grab the coefficient estimates and R-squared
	     genr a = coeff(const)
	     genr b = coeff(x)
	     genr r2 = $rsq
	     # arrange for printing of stats on these
	     print a b r2
	     # and save the coefficients to file
	     store coeffs.gdt a b
	  endloop

This loop will print out summary statistics for the `a' and `b' estimates and R2 across the 100 repetitions. After running the loop, coeffs.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.

The command nulldata is useful for Monte Carlo work. Instead of opening a "real" data set, nulldata 50 (for instance) opens a dummy data set, containing just a constant and an index variable, with a series length of 50. Constructed variables can then be added using the genr command.

See the seed command in Chapter 12 for information on generating repeatable pseudo-random series.

Iterated least squares

Example 9-2 uses a "while" loop to replicate the estimation of a nonlinear consumption function of the form as presented in Greene (2000, Example 11.3). This script is included in the gretl distribution under the name greene11_3.inp; you can find it in gretl under the menu item "File, Open command file, practice file, Greene...".

The option --print-final for the ols command arranges matters so that the regression results will not be printed each time round the loop, but the results from the regression on the last iteration will be printed when the loop terminates.

Example 9-2. Nonlinear consumption function


	  open greene11_3.gdt
	  # run initial OLS
	  ols C 0 Y
	  genr essbak = $ess
	  genr essdiff = 1
	  genr beta = coeff(Y)
	  genr gamma = 1
	  # iterate OLS till the error sum of squares converges
	  loop while essdiff > .00001
	     # form the linearized variables
	     genr C0 = C + gamma * beta * Y^gamma * log(Y)
	     genr x1 = Y^gamma
	     genr x2 = beta * Y^gamma * log(Y)
	     # run OLS 
	     ols C0 0 x1 x2 --print-final --no-df-corr --vcv
	     genr beta = coeff(x1)
	     genr gamma = coeff(x2)
	     genr ess = $ess
	     genr essdiff = abs(ess - essbak)/essbak
	     genr essbak = ess
	  endloop 
	  # print parameter estimates using their "proper names"
	  noecho
	  printf "alpha = %g\n", coeff(0)
	  printf "beta  = %g\n", beta
	  printf "gamma = %g\n", gamma

Example 9-3 (kindly contributed by Riccardo "Jack" Lucchetti of Ancona University) shows how a loop can be used to estimate an ARMA model, exploiting the "outer product of the gradient" (OPG) regression discussed by Davidson and MacKinnon in their Estimation and Inference in Econometrics.

Example 9-3. ARMA 1, 1


	open armaloop.gdt

	genr c = 0
	genr a = 0.1
	genr m = 0.1

	genr e = const * 0.0
	genr de_c = e
	genr de_a = e
	genr de_m = e

	genr crit = 1
	loop while crit > 1.0e-9

	   # one-step forecast errors
	   genr e = y - c - a*y(-1) - m*e(-1)  

	   # log-likelihood 
	   genr loglik = -0.5 * sum(e^2)
	   print loglik

	   # partials of forecast errors wrt c, a, and m
	   genr de_c = -1 - m * de_c(-1) 
	   genr de_a = -y(-1) -m * de_a(-1)
	   genr de_m = -e(-1) -m * de_m(-1)
   
	   # partials of l wrt c, a and m
	   genr sc_c = -de_c * e
	   genr sc_a = -de_a * e
	   genr sc_m = -de_m * e
   
	   # OPG regression
	   ols const sc_c sc_a sc_m --print-final --no-df-corr --vcv

	   # Update the parameters
	   genr dc = coeff(sc_c) 
	   genr c = c + dc
	   genr da = coeff(sc_a) 
	   genr a = a + da
	   genr dm = coeff(sc_m) 
	   genr m = m + dm

	   printf "  constant        = %.8g (gradient = %#.6g)\n", c, dc
	   printf "  ar1 coefficient = %.8g (gradient = %#.6g)\n", a, da
	   printf "  ma1 coefficient = %.8g (gradient = %#.6g)\n", m, dm

	   genr crit = $T - $ess
	   print crit
	endloop

	genr se_c = stderr(sc_c)
	genr se_a = stderr(sc_a)
	genr se_m = stderr(sc_m)

	noecho
	print "
	printf "constant = %.8g (se = %#.6g, t = %.4f)\n", c, se_c, c/se_c
	printf "ar1 term = %.8g (se = %#.6g, t = %.4f)\n", a, se_a, a/se_a
	printf "ma1 term = %.8g (se = %#.6g, t = %.4f)\n", m, se_m, m/se_m
	

Indexed loop examples

Example 9-4 shows an indexed loop in which the smpl is keyed to the index variable i. Suppose we have a panel dataset with observations on a number of hospitals for the years 1991 to 2000 (where the year of the observation is indicated by a variable named year). We restrict the sample to each of these years in turn and print cross-sectional summary statistics for variables 1 through 4.

Example 9-4. Panel statistics


	  open hospitals.gdt
	  loop i=1991..2000
	    smpl (year=i) --restrict --replace
	    summary 1 2 3 4
	  endloop

Example 9-5 illustrates string substitution in an indexed loop.

Example 9-5. String substitution


	open bea.dat
	loop i=1987..2001
	  genr V = COMP$i
	  genr TC = GOC$i - PBT$i
	  genr C = TC - V
	  ols PBT$i const TC V
	  endloop

The first time round this loop the variable V will be set to equal COMP1987 and the dependent variable for the ols will be PBT1987. The next time round V will be redefined as equal to COMP1988 and the dependent variable in the regression will be PBT1988. And so on.