April 17, 2012 --- Class 27 --- Homework Revisited, Chi-squared Distribution
Activities:
Homework Revisited
We spent the beginning of class going over the homework that was
returned. A histogram of scores was shown. The average score on
each problem was also announced.
For problem 6.4, I showed how much of the work could
be done using Mathematica. Some students used variations of
programs like map.c or fixed_points.c to accomplish the work. That is
fine.
Problem 6.6 focuses on the scaling properties of the map. Part (a)
can easily be accomplished with a one or two line awk script. This
much more efficient that writing a compiled code for the formula
for delta_k. I also described a simple formula for r_infinity
using b_k, b_{k-1} and delta. Full credit for part (b) required using
a number of high precision values of s_m, the values of r that
give superstable trajectories. Although this is project 6.22, we
already did all the hard work to solve for the s_m values. With the
awk script for part (a), it is trivial to make a beautiful plot
of delta_k based on the s_k rather than b_k. We get much closer to
the high precision value in Eq. (6.10).
For problem 11.10, all the answers are random numbers, so how did
I grade your work. Essentially, the answer is by repeating all the
calculations a large number of times so that I have an accurate
knowledge of how much the results should vary. Any results that
seemed too far off, were noted as such. If you can demonstrate that
anything I flagged as being too far off is, indeed, correct,
please return your paper to me for regrading.
Chi-squared distribution
We briefly reviewed a method of getting numbers with a chi-squared
distribution based on Gaussian random numbers and an awk command:
The code for creating gaussian random numbers is in
~sg/src/gausrand.c.
gausrand.c uses an accept-reject algorithm to generate numbers
uniformly in the unit circle and then rescales the radius of
the points to create a gaussian distribution. Like the
Box-Muller method, it produces two numbers at a time.
It comes from Sec. 7.2 of Numerical Recipes.
testgaus.c is a simple driver programs that calls gausrand 250000
times and prints the result to standard output.
The awk script chisq.a looks like this:
{sum += $1*$1; if (NR%n == 0) {print sum; sum=0.} }
testgaus |awk -f chisq.a n=6
will produce a stream of numbers according to the chisq distribution
with N=6. Of course, you can pipe this to hist or autocorr if you
want to see a histogram or verify that the autocorrelation is small.
We interactively produced histograms for different values of N:
foreach i ( 2 4 6 8 10 12 20)
testgaus |awk -f chisq.a n=$i|hist -n 50 -g |axis lt $i|xplot
end
Notice the label in the axis command so we can tell which graph
is which.
Chi-Squared in C
If you want to know what a chi-squared distribution looks like you
may use my chisq program in directory ~sg/src/misc.
The program chisq.c calls gausrand() to produce numbers with a
chi-sq distribution. This runs more quickly than using the awk
script. Of course, writing the C program
takes more time than typing the awk script. So we have yet another
example of trying to compare development time and run time.
If you just type chisq, you will get this message:
Usage: chisq dof num
where dof is the number of degrees of freedom
and num is the number of random variables generated
Now you know how to run the code. For example, try:
chisq 10 10000 |~sg/bin/hist -n 40 -g |axis |xplot
will produce a nice graph of the distribution for 10 degrees
of freedom.
It was easy to interactively produce a series of histograms with
the new command.
So far, we have used an empirical approach to understanding the
chi squared distribution. In the next class, we will take an
analytic approach.