Is a DNA sequence of length n consistent with four equally likely nucleotides? Pearson's statistic S = Σ (xi − ei)² / ei compares the observed letter counts with their expected values. The top panel shows its distribution for sequences simulated under the null (teal) and under an alternative (pink), the exact null distribution obtained by enumerating every possible count vector (white outline) and the chi-square density with 3 degrees of freedom (gold). We reject when S exceeds a critical value; the fraction of alternative sequences rejected is the power. The bottom panel shows the power as a function of n: exactly, by the noncentral chi-square approximation and by simulation.
The exact distributions are computed by listing every count vector (xA, xC, xG, xT) with sum n (there are (n+1)(n+2)(n+3)/6 of them) and its multinomial probability; this is done for n ≤ 200. The exact test rejects when S is larger than the smallest value c with P0(S ≤ c) ≥ 0.95, so its size is at most 5%. The Monte Carlo critical value is the 95% sample quantile of the simulated null values, as in the book. Simulated sequences use a seeded generator, so everyone sees the same run.
code/ch01_generative_discrete.R (Figure 1.10).