Class 8


Review from previous class

Iteration
The Lisp "do" form is pretty complex, offering a great deal of flexibility. For many problems, "dotimes" is easier to work with and yields a more readible program. Similarly, macros allow you to make your own iterative constructs (like the "while" example).

Speed of calculation
Iteration and tail recursion are equally effective.

Building artificial lists
Its occasionally tempting to build a list rather than use iteration. If you don't really need the list, and only want it to avoid writing the iterative/recursive function, you are likely to get a slower program. On the other hand, use the built-in functions whenever possible; just don't force it upon yourself.


Tools for the Day

We'll see a bit more of Mathematica today, using a small notebook named newton.nb.
Fixed points
Mathematica has this capability built-in.

More graphics
A strength of MM is the huge range of graphics that are available. This notebook shows a color density plot. The plot illustrates the domains of attraction that occur when one uses Newton's method to find the n roots of unity, the solutions of z^n=1 in the complex plane.


Status of Projects


Simulation Methods in Lisp

Random number generators
These are by-and-large built-into LispStat. You can find most of these by using the apropos function, looking for symbols with "-rand" in the name (as in uniform-rand or normal-rand).
		> (apropos '-rand)
		-RAND
		F-RAND
		BINOMIAL-RAND
		MAKE-RANDOM-STATE
		GAMMA-RAND
		T-RAND
		NORMAL-RAND
		CAUCHY-RAND
		CHISQ-RAND
		UNIFORM-RAND
		POISSON-RAND
		BETA-RAND
		

These generators are accompanied by

For example,
		
		> (apropos 'poisson)
		POISSON
		POISSONREG-MODEL
		POISSON-PMF
		POISSON-QUANT
		POISSON-CDF
		POISSON-RAND
		
		

Controlling the random seed
The output of the various -rand functions are lists of "pseudo-random" values. After all, they're generated by an algorithm and cannot be truly random. You can control the values generated by setting the value of a symbol. This is a good thing since that makes your simulations reproducible (as long as you remember to record the seeds that you have used in the experiments).

You will need to be careful in how you save the seed, however. Consider the following output (generated in the new release 3.48 of LispStat on a Macintosh).

		
		> *random-state*
		#$(1 #(2147483562 0 44830 4924))
		> (setf saveSeed *random-state*)
		#$(1 #(2147483562 0 44830 4924))
		> (uniform-rand 5)
		(0.742012 0.638736 0.703512 0.3317438 0.079660)
		> *random-state*
		#$(1 #(2147483562 171069757 327390122 156320365))
		> saveSeed
		#$(1 #(2147483562 171069757 327390122 156320365))
		
		

Can you explain what has happened?

The way to save a copy of the seed that will not change is to use the make-random-state function. This same function can also be used to initialize the seed, typically using some function relying upon the internal clock in the computer you're using.

		> (setf saveSeed (make-random-state *random-state*))
		#$(1 #(2147483562 171069757 327390122 156320365))
		> (uniform-rand 3)
		(0.18446831 0.336059123 0.22523372)
		> *random-state*
		#$(1 #(2147483562 483685715 100858525 1764656372))
		> saveseed
		#$(1 #(2147483562 171069757 327390122 156320365))
		
		> (setf *random-state* (make-random-state t))  ; init from clock
		#$(1 #(2147483562 0 44830 5893))

		
Simulating a statistic via Monte Carlo methods
The most common, the simple algorithm for this chore is

Written informally, here's the code. This code will save all of the values that are generated, not just some summary statistics. This a useful since you can then do some data analysis using the simulated values.

	     (setf stat ())
	     (dotimes (i nReps)
	     	(generate random numbers of chosen distribution and sample size)
	     	(evaluate statistic with this data)
	     	(push the statistic on the list stat)
	     )
	     (analyze results)
	     
This is so general, we can write a function that will write this function for us.

Finally, once the simulation has been run, the simulation values (rather than a few summary statistics) are available for analysis using all of the LispStat statistical procedures (eg compare, scatterplot-matrix, etc).

Bootstrap simulations
The bootstrap is a "rough-and-ready" tool for estimating the standard error and confidence interval for a statistic. You can think of it simply as a method of simulation based on sampling the data or as an approximation to a functional Taylor series expansion. Both ideas are useful.

The key thing that we need to bootstrap a statistic is a way of sampling with replacement from a list of values. Fortunately, LispStat has the sample function we need:

	    
		> (sample (iseq 10) 10)     ; without replacement
		(3 1 0 6 5 7 4 9 2 8)
		
		> (sample (iseq 10) 10 t)   ; WITH replacement
		(6 6 5 6 5 6 2 1 8 0)
	    
	    

This problem fits our prior style of simulation and so we can exploit the same code to bootstrap a statistic, emphasizing the linkage between resampling and simulation.

Lisp script for today's class
class8.lsp


Next time

We will look a bit more carefully at simulation, with particular attention to a Gibbs sampler.