DEV Community

Academic Quant
Academic Quant

Posted on Originally published at academicquant.co.uk

With three seeds per model a permutation test stops at p = 0.10

Say you've trained the old model with three seeds and the new one with three seeds. The old one got 71.2, 72.8 and 73.1, and the new one got 74.0, 74.9 and 75.3. Every run of the new model beat every run of the old one and the gap is 2.4 points, so it looks settled. If you do an exact permutation test on it you'll get p = 0.10, and there's no result you could've had that would've made it smaller.

Why it stops at 0.10

The test asks how unusual your split would be if the labels "old" and "new" didn't mean anything. There are six numbers and you've got to choose which three to call old, so there are 20 ways of doing it. Yours is the most extreme one, and so is its mirror image where the new model loses every time. That's 2 out of 20.

from itertools import combinations
import numpy as np

def permutation_test(a, b):
    x, n = np.r_[a, b], len(a)
    seen = abs(np.mean(a) - np.mean(b))
    hits = total = 0
    for idx in combinations(range(len(x)), n):
        m = np.zeros(len(x), bool); m[list(idx)] = True
        hits += abs(x[m].mean() - x[~m].mean()) >= seen - 1e-12
        total += 1
    return hits / total

permutation_test([71.2, 72.8, 73.1], [74.0, 74.9, 75.3])   # 0.1
Enter fullscreen mode Exit fullscreen mode

So two identical models would look like this by luck one time in ten. I simulated 100,000 pairs of identical models to check, and all three runs of one were above all three of the other in 10.06% of them.

How many seeds you'd need

Runs per model Ways to split Smallest two-sided p
3 20 0.100
4 70 0.029
5 252 0.008

With four each you can get under 0.05, but only if there's hardly any overlap, and I'd normally go to five. I gave each model two more seeds in the example above (72.0 and 73.6 for the old one, 73.4 and 74.4 for the new). There's now one overlap, since 73.6 beats 73.4, and the difference in means has come down to 1.86 points. But the p-value is 4 out of 252, so about 0.016, which is a lot better than before.

What about the t-test?

Welch's t-test on the first six numbers gives p = 0.036, so you'd be tempted to use that one. It gets there by assuming the runs are normally distributed, and you can't check that with three of them. I'd believe it more if I'd seen twenty seeds of the old model at some point and they looked normal. If you haven't, I'd say what you did and give all six numbers rather than a p-value.

Also, if both models are scored on the same test questions, you can compare them question by question and you'll usually need far fewer runs. I've written the permutation test out in full, with a sampled version for when there are too many splits to list, at https://academicquant.co.uk/ml-permutation-test?utm_source=devto&utm_medium=post&utm_campaign=devto-three-seeds-permutation

Top comments (0)