Prince Osei Aboagye
Reference

Statistics primer

The tests in code/rl/stats.py and code/scripts/analyze_control.py, in plain words: each one's exact formula as the code computes it, a tiny illustrative example, when to use it, and where the project used it.

Every p-value and interval on this site comes from two files: code/rl/stats.py and code/scripts/analyze_control.py. Both are torch-free and run with a plain Python 3. This page gives each test’s exact formula, as the code computes it, with a tiny worked example.

The worked examples are illustrative: made-up numbers, run through the project’s own functions. The “In the project” lines give real results.

// § 01

Which test for which question

Question Test Section
Did a per-problem score (such as avg@8) change, on the same problems? Paired difference, its standard error and \(t\) 1
Is that change bigger than chance, without assuming a distribution? Sign-flip permutation test 2
What range of changes fits the data? Bootstrap percentile interval 3
Did a yes/no outcome (greedy right, pass@8) change, on the same problems? McNemar’s exact test 4
Did a training metric differ between two blocks of updates? Welch’s \(t\) 5
Is a training metric rising per update? Least-squares slope 6
What is the change over several test sets? Weighted mean of per-set differences 7
What range fits a proportion from a small sample? Wilson interval 8

Why paired. Before and after are evaluated on the same problems. Each problem then serves as its own control, which removes the large spread in difficulty between problems from the comparison.

// § 02

1. The paired difference

For each problem, take the score after minus the score before:

\[d_p = y_p^{\text{after}} - y_p^{\text{before}}, \qquad \bar{d} = \frac{1}{n}\sum_{p=1}^{n} d_p \tag{1}\]

Here \(y_p\) is problem \(p\)’s score (for avg@8, its share of 8 samples right) and \(n\) is the number of problems.

In words: the mean, over problems, of how much each problem’s score changed.

\[s_d = \sqrt{\frac{1}{n - 1}\sum_{p=1}^{n}\left(d_p - \bar{d}\right)^2}, \qquad \mathrm{SE} = \frac{s_d}{\sqrt{n}}, \qquad t = \frac{\bar{d}}{\mathrm{SE}} \tag{2}\]

Here \(s_d\) is the sample standard deviation of the differences.

In words: the standard error is the spread of the per-problem changes shrunk by the square root of the number of problems, and \(t\) says how many standard errors the mean change is from zero.

\[\bar{d} \pm t_{0.975,\,n-1}\,\mathrm{SE} \tag{3}\]

Here \(t_{0.975,\,n-1}\) is the 97.5% quantile of Student’s \(t\) with \(n - 1\) degrees of freedom. The code approximates it by a Cornish–Fisher expansion, with an error below 1e-3 for 10 or more degrees of freedom.

In words: the 95% \(t\)-interval is the mean change plus or minus about two standard errors.

Worked example (illustrative). Six problems, each scored by its share of 8 samples right:

Problem 1 2 3 4 5 6
Before 2/8 3/8 0/8 1/8 5/8 4/8
After 4/8 4/8 0/8 4/8 4/8 5/8
\(d_p\) +0.250 +0.125 0 +0.375 −0.125 +0.125

The mean change is \(\bar{d} = 0.75/6 = 0.125\), or +12.5 points. The spread is \(s_d = 0.177\), so \(\mathrm{SE} = 0.177/\sqrt{6} = 0.072\) and \(t = 1.73\). The code’s quantile for 5 degrees of freedom is 2.568, so the \(t\)-interval is \([-0.060, +0.310]\): it includes 0.

When to use it. Two evaluations of the same problems, with a score per problem. The \(t\)-interval leans on the mean being close to normal, which holds better as \(n\) grows.

In the project. The control run’s MATH-500 avg@8 went from 21.77% to 53.95% at update 100: +32.17 points, SE 1.41, \(t\) 22.9, over 500 problems.

// § 03

2. The sign-flip permutation test

If training changed nothing, each problem’s difference would be as likely to be positive as negative. So flip each sign at random, many times, and see how often the flipped sum is as extreme as the real one:

\[p = \frac{h + 1}{F + 1}, \qquad h = \#\Big\{f \le F : \Big|\sum_{p} \sigma_{f,p}\, d_p\Big| \ge \Big|\sum_{p} d_p\Big|\Big\} \tag{4}\]

Here \(F\) is the number of random flips (20,000, seed 0), \(\sigma_{f,p}\) is \(+1\) or \(-1\) with probability 1/2 each, and the sums run over the nonzero differences only. A zero difference cannot change a sum, so it is left out. The smallest possible p is \(1/(F+1) = 1/20{,}001\), about 0.00005.

In words: the p-value is the share of random sign flips, counting the observed signs as one of them, whose total is at least as large as the observed total.

Worked example (illustrative). Take the five nonzero differences above, in eighths: +2, +1, +3, −1, +1. Their sum is 6. Of the \(2^5 = 32\) sign patterns, 8 give a total of at least 6 in size, so the exact p is \(8/32 = 0.25\). The code’s 20,000 random flips give 0.2498.

When to use it. Paired differences, with no assumption about their distribution. It suits small samples and lumpy scores such as avg@8, which moves in steps of 1/8.

In the project. GSPO-A against SFT on the main run’s six test sets: avg@8 +0.58 points, sign-flip p = 0.002. The control run’s MATH-500 gains report p = 0.00005, the smallest value 20,000 flips can give.

// § 04

3. The bootstrap percentile interval

Resample the problems with replacement, recompute the mean change each time, and read the interval off the sorted means:

\[\left[\ \bar{d}^{*}_{(\lfloor 0.025R \rfloor + 1)},\ \ \bar{d}^{*}_{(\lfloor 0.975R \rfloor)}\ \right] \tag{5}\]

Here \(\bar{d}^{*}_{(j)}\) is the \(j\)-th smallest of \(R\) resampled means, each the mean of \(n\) differences drawn with replacement. With \(R = 10{,}000\) and seed 0, the code reads the 251st and the 9,750th values.

In words: the 95% interval runs from the 2.5th to the 97.5th percentile of the mean change over many resampled sets of problems.

Worked example (illustrative). For the six differences of section 1, the bootstrap interval is \([0.000, +0.250]\), or 0 to +25 points. With six problems it is lumpy: a resampled mean can only take a few values, and it cannot reach past the observed extremes.

When to use it. An interval with no normality assumption. It covers evaluation noise, the choice of problems, but not the randomness of training itself. When items share a cluster, such as conversations with the same first prompt, cluster_paired_ci resamples whole clusters instead, because resampling the items one by one understates the uncertainty.

In the project. Control run, MATH-500 avg@8 at update 100: \([+29.45, +35.00]\) points. On the laptop, Run 3b’s final-answer gain: \([+2.44, +6.50]\).

// § 05

4. McNemar’s exact test

For a yes/no outcome per problem, only the problems that changed carry information. Under no effect, each change is equally likely to go either way:

\[p = \min\left(1,\ \frac{2}{2^{\,n}}\sum_{i=0}^{k}\binom{n}{i}\right), \qquad n = b + c, \quad k = \min(b, c) \tag{6}\]

Here \(b\) counts the problems that went wrong → right, \(c\) those that went right → wrong, and \(p = 1\) when \(n = 0\).

In words: the two-sided chance of a split at least this lopsided, if each changed problem had gone up or down by a coin flip.

Worked example (illustrative). Nine problems went wrong → right and two went right → wrong, so \(n = 11\) and \(k = 2\). The tail is \(\binom{11}{0} + \binom{11}{1} + \binom{11}{2} = 1 + 11 + 55 = 67\), and \(p = 2 \times 67/2{,}048 = 0.065\).

When to use it. Greedy correctness or pass@8 on the same problems before and after. The problems that stayed right or stayed wrong drop out.

In the project. SVAMP pass@8, SFT → GSPO-A: 33 up and 12 down, p = 0.0025. Control run, MATH-500 greedy, base → update 100: 194 up and 18 down, p = 1.86e-38.

// § 06

5. Welch’s t

Two independent groups of values, such as the per-update solve rates of the first and second halves of training:

\[\Delta = \bar{y}_b - \bar{y}_a, \qquad v_a = \frac{s_a^2}{n_a}, \quad v_b = \frac{s_b^2}{n_b}, \qquad t = \frac{\Delta}{\sqrt{v_a + v_b}} \tag{7}\]

In words: the difference of the two means, divided by the standard error of that difference, with no assumption that the two groups are equally noisy.

\[\nu = \frac{\left(v_a + v_b\right)^2}{\dfrac{v_a^2}{n_a - 1} + \dfrac{v_b^2}{n_b - 1}} \tag{8}\]

Here \(\bar{y}_a\), \(s_a^2\) and \(n_a\) are the first group’s mean, sample variance and size, and likewise for \(b\).

In words: the Welch–Satterthwaite degrees of freedom, which fall below \(n_a + n_b - 2\) when the two groups differ in spread or size.

Worked example (illustrative). First block \(a = (0.20, 0.25, 0.22, 0.27)\), second block \(b = (0.30, 0.35, 0.28, 0.33)\). Then \(\Delta = 0.08\), the standard error is 0.022, \(t = 3.64\) and \(\nu = 6.0\): equal spreads and sizes give \(n_a + n_b - 2\).

When to use it. Unpaired samples. In GSPO, each step draws fresh problems from the same pool, so the steps are independent samples.

In the project. The control run’s solve rate rose from 29.27% over updates 1–50 to 48.59% over 51–100: Welch \(t\) 11.95, with 97.4 degrees of freedom. GSPO-A’s halves gave \(t\) 1.68, suggestive but not significant.

// § 07

6. The least-squares slope

Fit a straight line to a metric against the update number:

\[\hat{\beta} = \frac{\sum_{u}\left(x_u - \bar{x}\right)\left(y_u - \bar{y}\right)}{S_{xx}}, \qquad S_{xx} = \sum_{u}\left(x_u - \bar{x}\right)^2 \tag{9}\]

In words: the slope is the change in the metric per update that best fits the points.

\[\mathrm{SE}\left(\hat{\beta}\right) = \sqrt{\frac{\sum_u\left(y_u - \bar{y} - \hat{\beta}\left(x_u - \bar{x}\right)\right)^2}{(n - 2)\, S_{xx}}}, \qquad t = \frac{\hat{\beta}}{\mathrm{SE}\left(\hat{\beta}\right)} \tag{10}\]

Here \(x_u\) is the update number, \(y_u\) the metric at that update, and \(n\) the number of updates (at least 3).

In words: the slope’s standard error comes from how far the points sit from the line, with two degrees of freedom spent on the line itself.

Worked example (illustrative). Updates 1–5 with solve rates 0.20, 0.22, 0.25, 0.24 and 0.29. The slope is 0.02, or 2 points per update, with a standard error of 0.0045 and \(t = 4.47\).

When to use it. To ask whether a metric trends over training, using every update instead of two blocks. It assumes a straight line and independent scatter around it.

In the project. The control run’s solve rate rose +0.361 points per update (SE 0.024, \(t\) 14.81, 100 updates).

// § 08

7. Combining test sets

Each test set gives its own paired difference and standard error. The sets hold different problems, so their errors are independent, and a weighted mean combines them:

\[\Delta = \sum_{s} w_s \Delta_s, \qquad \mathrm{SE}(\Delta) = \sqrt{\sum_{s}\left(w_s\,\mathrm{SE}_s\right)^2} \tag{11}\]

Here \(\Delta_s\) and \(\mathrm{SE}_s\) are set \(s\)’s difference and standard error. PocketRL’s six-set mean uses \(w_s = 1/6\); the pooled mean uses \(w_s = n_s/n\), each set’s share of all problems.

In words: the combined change is a weighted mean of the per-set changes, and its error adds the per-set errors in quadrature.

Example (from the control run). The six per-set standard errors were 1.41, 2.17, 1.35, 4.22, 1.24 and 0.89 points. With \(w_s = 1/6\) they combine to 0.89, so the six-set gain of +13.54 points has \(t\) = 15.2.

// § 09

8. The Wilson interval

For a proportion \(k/n\) from a small sample, the plain normal interval can leave \([0, 1]\). The Wilson interval does not:

\[\tilde{p} = \frac{\hat{p} + \frac{z^2}{2n}}{1 + \frac{z^2}{n}}, \qquad \delta = \frac{z}{1 + \frac{z^2}{n}}\sqrt{\frac{\hat{p}(1 - \hat{p})}{n} + \frac{z^2}{4n^2}}, \qquad \left[\tilde{p} - \delta,\ \tilde{p} + \delta\right] \tag{12}\]

Here \(\hat{p} = k/n\), \(z = 1.96\), \(\tilde{p}\) is the interval’s centre and \(\delta\) its half-width. The bounds are exactly 0 when \(k = 0\) and exactly 1 when \(k = n\).

In words: a 95% interval for a proportion, centred a little toward one half, that stays usable at 0 of \(n\) or \(n\) of \(n\).

In the project. The blind check of the final SFT labels found 0 of 50 wrong. The Wilson interval’s upper bound on the error rate is 7.13%; the notebook’s exact interval gives 7.1%.

// § 10

Reading a result

  • Check the noise floor first. In the control run, the same weights evaluated twice moved MATH-500 avg@8 by −0.33 points (SE 0.86, sign-flip p 0.72).
  • Correct for several tests. GSPO-A’s pooled pass@8 gain had p = 0.038; with two pooled measures, a Bonferroni correction makes it 0.075, borderline.
  • Prefer the metric that uses more of the data. avg@8 uses all 8 samples; pass@8 keeps one bit per problem. The same comparison gave p = 0.002 by avg@8 and 0.038 by pass@8.
  • Know what the interval covers. A bootstrap over problems says nothing about a different training seed. Only the laptop studies had three seeds.