Activity: Generating Chi-Squared Variates
Modeling Randomness Activities
We write this program together: the instructor types the code, and you type along. Before each run, write down a prediction; then run and check it.
The question. Let \(Z_1, Z_2, \ldots, Z_k\) represent independent standard normal random variables, each with mean 0 and variance 1. A theorem in statistics says that
\[\chi^2_k = \sum_{i=1}^{k} Z_i^2\]
has a chi-squared distribution with \(k\) degrees of freedom, with \(E[\chi^2_k] = k\) and \(\mathrm{Var}[\chi^2_k] = 2k\). The chi-squared distribution is a gamma distribution with shape \(k/2\) and scale 2. We test the theorem by experiment, for \(k = 5\): generate 1000 chi-squared variates by convolution, and compare them with the theory.
Setup. Get the starter file ChiSquared.kt from the code/ folder beside this activity. Put it in src/main/kotlin/student/activities in your KSL project, the package the file declares; create the folder if you do not have it. Run its main. It compiles and runs, but the statistics are NaN: the two functions marked TODO are what we write.
1. Before any code
What are the mean, the variance, and the smallest possible value of \(\chi^2_5\)?
Sketch the shape you expect for the histogram of 1000 variates.
How many standard normal variates does the program draw in all?
2. Generate and summarize
We write generateChiSquared (TODO 1) and sampleChiSquared (TODO 2) together.
Run. Compare the sample mean and variance with the theory. Does the 95% confidence interval for the mean contain 5?
The program uses stream 1 for the normal variates. If you run it again, do you get the same numbers? Why is that useful?
3. Look at the data
The program prints a histogram and a box plot summary, and shows six plots: a histogram, a box plot, an observation plot, an autocorrelation (ACF) plot, and P-P and Q-Q plots against the Gamma(2.5, 2) distribution.
Describe the shape of the histogram. Does it match your sketch?
The box plot summary lists many “outliers” above the upper fence. Are they errors in the data?
What should the observation plot and the ACF plot show for independent variates? Do they?
Do the P-P and Q-Q plots support the theorem?
4. Save and fit
The program writes the data to kslOutput/ChiSquared5.txt and fits the KSL’s continuous distributions to it with a PDFModeler.
Predict the distribution the
PDFModelerwill rank first, and its parameters. Then look at the ranking. Which distribution comes second, and why might it score well?The fit turns off automatic shifting. Why is that the right choice here?
If you finish early
Change the degrees of freedom to 30. Predict the shape of the histogram and the top distribution, then run and check.