This year's drops

Null distribution of burn-ins

Decisions at α = 0.05

◆ The Blueprint
The z-test
$$z = \frac{\hat p - p}{\mathrm{SE}}, \qquad \mathrm{SE} = \sqrt{\frac{p(1-p)}{n}}$$

Here \(p\) is the null value and \(\hat p = x/n\) is the proportion observed. The standard error uses \(p\) because the test needs the distribution of \(\hat p\) when H₀ is true. The one-sided p-value is pnorm(-z), the normal curve's area at and above \(z\).

The exact null distribution

When H₀ is true, each drop burns in with probability \(p\), independently of the others, so the number of burn-ins \(X\) in \(n\) drops is Binomial(\(n, p\)).

$$P(X \ge x) = \sum_{k=x}^{n} \binom{n}{k} p^k (1-p)^{n-k}$$

In R this is sum(dbinom(x:n, n, p)), and binom.test(x, n, p, alternative = "greater") reports the same p-value.

Why the curve misses at np = 1

The binomial counts are whole numbers from 0 to \(n\). With \(np = 1\), most of the probability is on 0, 1, and 2, and the distribution is skewed right. The normal curve is continuous, symmetric about \(np\), and extends below 0, so its right tail is thinner than the binomial's and the z-test's p-value is too small. The validity conditions \(np \ge 10\) and \(n(1-p) \ge 10\) mark where the curve is close to the bars.

The decision map

At each \(n\), the z-test rejects H₀ at every count from some threshold up, and so does the exact test. The orange cells are the counts between the two thresholds. The z-test's threshold is never the higher one, so no cell has the exact test rejecting alone.

Past np = 10

Where np ≥ 10, on and right of the dashed line on the decision map, the curve is close to the bars near their center, and the z-test's p-value is smaller than the exact one at most counts. Far in the tail the two can differ by a large factor: at 30 of 120 the exact p-value is 1,942 times the z-test's. Counts whose p-values are near 0.05 can still be orange cells.

The simulation

Each simulated year draws \(n\) marbles with replacement from a Patrol Cap, the cadet's cap used as a marble cup, whose share of red marbles is the null value: 1 red and 11 blue for \(p = 1/12\), and 1,000 marbles for any other \(p\). The slider's 0.083 is not exactly 1/12, so its p-values differ slightly from the presets'. The number of red marbles is that year's burn-ins. The empirical p-value is the share of simulated years with at least \(x\) burn-ins.

pc <- rep(c("red", "blue"), c(1, 11)); set.seed(376); sims <- replicate(10000, sum(sample(pc, 12, replace = TRUE) == "red")); mean(sims >= 3) gives 0.0717, and part 1 of The Script runs it.

The 30 sessions

Gaps from shuffled labels

Null distribution of the gap

◆ The Blueprint
Shuffling the labels

Under H₀ the design has no effect, so each of the 30 checkout times would be the same under either design. A shuffle keeps all 30 times and gives the A label to 12 sessions chosen at random. The gap it produces is one draw from the null distribution of \(\bar y_A - \bar y_B\).

$$\text{gap}^* = \bar y^*_A - \bar y^*_B$$
The p-value
$$p = \frac{\#\{\,|\text{gap}^*| \ge |\bar y_A - \bar y_B|\,\} + 1}{B + 1}$$

\(B\) is the number of shuffles. The +1 counts the real labels as one of the shuffles, so the smallest possible p-value is \(1/(B+1)\). The count uses both tails because Hₐ is two-sided. With 9,999 shuffles, the standard error of a simulated p-value near 0.05 is \(\sqrt{0.05 \times 0.95 / 9{,}999} \approx 0.002\).

The two-sample t-test
$$t = \frac{\bar y_A - \bar y_B}{s_p\sqrt{1/n_A + 1/n_B}}$$
$$s_p = \sqrt{\frac{(n_A - 1)s_A^2 + (n_B - 1)s_B^2}{n_A + n_B - 2}}$$

The pooled t-test assumes the two groups have equal variances and that \(\bar y_A - \bar y_B\) is approximately normal. Its p-value is 2*pt(-abs(t), df) with \(\mathrm{df} = n_A + n_B - 2\), the same as t.test(A, B, var.equal = TRUE). On the plot, the t curve is drawn on the seconds scale: the t density at \(x/\mathrm{SE}\), divided by \(\mathrm{SE}\), the standard error \(s_p\sqrt{1/n_A + 1/n_B}\).

Every relabeling

The small skewed sample's 20 sessions, split 7 and 13, can be labeled in \(\binom{20}{7} = 77{,}520\) ways, few enough to compute every one. The exact p-value is the share of all relabelings with a gap at least as far from 0 as the observed one. It has no +1, because the real labeling is one of the 77,520.

Why the t curve misses

The Central Limit Theorem makes \(\bar y_A - \bar y_B\) approximately normal in large samples, even when the data are skewed. Twenty heavily skewed sessions, one of them 2,616 seconds long, are too few. The exact null distribution has two humps, one for the relabelings that put the long session in group A and one for those that put it in group B, and the single symmetric t curve fits neither.

What shuffling needs

The permutation test assumes the labels are exchangeable under H₀: any 12 sessions could have received the Design A label. Random assignment of the designs guarantees this. Groups that people choose for themselves give no such guarantee, and shuffling their labels does not produce a null distribution.

In R

replicate(9999, { i <- sample(30, 12); mean(times[i]) - mean(times[-i]) }) draws the shuffles, and combn(20, 7) lists every relabeling, and parts 2 and 3 of The Script run both.

The sessions

Null distribution of the gap

This sample in R
◆ The Blueprint
How the samples are drawn

Right-skewed times are round(exp(rnorm(k, 4.6, 0.90))) seconds, bell-shaped times are round(rnorm(k, 150, 45)), and extremely skewed times are round(exp(rnorm(k, 4.6, 1.25))). Design A gets round(n × share) sessions, at least 3, and the true effect is added to each of them, with a floor of 1 second. The seed fixes the sample, and the shuffles always use seed 376.

The simulation error
$$\pm\, 1.96\sqrt{\frac{p(1-p)}{B}}$$

The permutation p-value estimates the share of all relabelings with a gap at least as extreme as the observed one. This is the 95% margin of that estimate. From 999 to 49,999 shuffles it shrinks by a factor of \(\sqrt{50} \approx 7\).

Welch's t-test
$$t = \frac{\bar y_A - \bar y_B}{\sqrt{s_A^2/n_A + s_B^2/n_B}}, \qquad \mathrm{df} = \frac{\left(s_A^2/n_A + s_B^2/n_B\right)^2}{\dfrac{(s_A^2/n_A)^2}{n_A - 1} + \dfrac{(s_B^2/n_B)^2}{n_B - 1}}$$

Welch's t-test estimates each group's variance separately, so its degrees of freedom are usually not a whole number. t.test(A, B) runs Welch's test by default, and var.equal = TRUE runs the pooled test.

False alarms

When there is no true effect, every rejection of H₀ is a false alarm. A test whose p-values are accurate makes false alarms in about 5% of samples at α = 0.05. At the skewed sample's settings (extreme right skew, 20 sessions, share 0.35, no effect), seeds 1 to 1,000 give 46 false alarms for the permutation test, 28 for the pooled t-test, and 34 for Welch's t-test.

In R

In free play, the code under the tiles rebuilds the sample on screen and its p-values. gen_sample(), the function that draws these samples, rebuilds any setting in R, as part 4 of The Script does.

The functions, one by one
CallWhat it returns
pnorm(-z)The one-sided z-test p-value: the normal curve's area at and above z.
binom.test(3, 12, 1/12, alternative = "greater")The exact binomial test. Its p-value equals sum(dbinom(3:12, 12, 1/12)).
sample(pc, 12, replace = TRUE)Twelve marbles drawn with replacement from pc, the Patrol Cap of 1 red (burned-in) marble and 11 blue: one simulated year.
replicate(10000, expr)Runs expr 10,000 times and keeps every result.
sample(30, 12)Twelve of the numbers 1 to 30, without replacement: the sessions that get the A label in one shuffle.
t.test(A, B, var.equal = TRUE)The pooled two-sample t-test. Without var.equal = TRUE, Welch's t-test.
combn(20, 7)Every set of 7 of the 20 sessions, one per column: all 77,520 relabelings.
perm_in_R.R