# Statistical significance: Is my new bot really better than the previous one?

Ethaniel
8,887 views

A test statistic (generically named T) indirectly measures the unlikeliness that some observed result can be explained by random deviation from a simple reference case where nothing special happens (examples: The coin is fair, the dice is fair, the two bots are as good as each other). In the use scope of this paper, the test statistic we’ll calculate is named $\chi_1^2$, however we’ll use a signed version of it.

### Use scope

• The formulas below are for comparing two bots only: Games with more than two bots (1 vs. 1 vs. 1 for instance) are not covered here.
• The formulas allows for draws, and you don’t need to know how the game behaves for a match between two identical bots.

### Formula of statistical significance

$T = \operatorname\left\{sign\right\}\left(W-L\right) \times \left\left( \frac\left\{\left\left[ D + \sqrt\left\{2 \left(W^2 + L^2\right)\right\} \right\right]^2\right\}\left\{N\right\} - N \right\right)$

with:

• W the number of matches won by the new bot,
• D the number of drawn matches,
• L the number of matches lost by the new bot,
• N = W + D + L the number of matches played.
Proof

A test statistic compares actual observation with what we would expect if nothing special happens: That expected outcome is the null hypothesis H₀, and T = 0 if the actual observation exactly fits H₀. As the outcomes to compare are triplets (values being labeled respectively “wins”, “draws” and “losses” for the new bot) for which we increment one of the values at each match result, we’re comparing trinomial distributions. We’ll thus use Pearson’s χ² test of goodness of fit, which is an asymptotic test statistic, not exact but precise enough for our need (calculating an exact test statistic would cost a lot of computing resources for a slight and useless gain in precision).

In our case the null hypothesis is that the two bots are as good as each other (if not identical), so the expected distribution is an equal number of wins and losses; The expected number of draws is undefined because it depends on the game rules and on the bot (especially if it does not play perfectly), this is a free parameter for H₀. So, for a given number N of matches played, the actual observation is a (WDL) triplet while the expected outcome is a (αN−2αα) triplet with α ∈ [0, N/2] (otherwise the triplet would contain negative values). We have 2 degrees of freedom for the actual observation (as N = W + D + L is fixed, we can choose freely only two of the three values) and 1 free parameter for H₀ (namely α), so the remaining number of degrees of freedom for the test statistic is 2 − 1 = 1: Pearson’s χ² test statistic is thus $\chi_1^2$ in our case.

Whatever its degrees of freedom, Pearson’s χ² test statistic is:

$\chi^2 = \sum_\left\{i=1\right\}^N \frac\left\{\left(O_i - E_i\right)^2\right\}\left\{E_i\right\} = \sum_\left\{i=1\right\}^N \frac\left\{O_i^2\right\}\left\{E_i\right\} - N$

with Oᵢ actual observations and Eᵢ expected values. So, in our case:

$\chi_1^2 = \frac\left\{W^2\right\}\left\{\alpha\right\} + \frac\left\{D^2\right\}\left\{N - 2 \alpha\right\} + \frac\left\{L^2\right\}\left\{\alpha\right\} - N = \frac\left\{D^2\right\}\left\{N - 2 \alpha\right\} + \frac\left\{W^2 + L^2\right\}\left\{\alpha\right\} - N$

The free parameter α of H₀ is estimated by minimizing $\chi_1^2$ in the [0, N/2] domain. Its first derivative is:

\begin\left\{align\right\} \frac\left\{\mathrm\left\{d\right\}\chi_1^2\right\}\left\{\mathrm\left\{d\right\}\alpha\right\} & = 2 \frac\left\{D^2\right\}\left\{\left(N - 2 \alpha\right)^2\right\} - \frac\left\{W^2 + L^2\right\}\left\{\alpha^2\right\} \\ & = \frac\left\{W^2 + L^2\right\}\left\{\left(N - 2 \alpha\right)^2\right\} \left\left[ \frac\left\{2 D^2\right\}\left\{W^2 + L^2\right\} - \frac\left\{\left(N - 2 \alpha\right)^2\right\}\left\{\alpha^2\right\} \right\right] \\ & = \frac\left\{W^2 + L^2\right\}\left\{\left(N - 2 \alpha\right)^2\right\} \left\left[ \frac\left\{2 D^2\right\}\left\{W^2 + L^2\right\} - \left\left( \frac\left\{N - 2 \alpha\right\}\left\{\alpha\right\} \right\right)^2 \right\right] \end\left\{align\right\}

We have an extremum when the first derivative vanishes:

$\left. \frac\left\{\mathrm\left\{d\right\}\chi_1^2\right\}\left\{\mathrm\left\{d\right\}\alpha\right\} \right|_\left\{\alpha=\alpha_0\right\} = 0 \Leftrightarrow \frac\left\{N - 2 \alpha_0\right\}\left\{\alpha_0\right\} = \pm \sqrt\left\{\frac\left\{2 D^2\right\}\left\{W^2 + L^2\right\}\right\} = \pm \frac\left\{2 D\right\}\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\}$

As we want α₀ ∈ [0, N/2], N−2α₀ and α₀ are both positive, so we use the positive root:

$\frac\left\{N\right\}\left\{\alpha_0\right\} - 2 = \frac\left\{2 D\right\}\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\} \Rightarrow \begin\left\{cases\right\} \alpha_0 = \frac\left\{N\right\}\left\{\frac\left\{2 D\right\}\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\} + 2\right\} = \frac\left\{N\right\}\left\{2\right\} \frac\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\}\left\{D + \sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\} \\ N - 2 \alpha_0 = \alpha_0 \frac\left\{2 D\right\}\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\} \end\left\{cases\right\}$

As we have taken the positive root, N−2α₀ and α₀ are either both positive or both negative, but that latter case is impossible as N > 0, so α₀ ∈ [0, N/2] as expected. $\chi_1^2$ has a unique extremum in the [0, N/2] domain, at α₀; We then need to check that this extremum is a minimum thanks to the second derivative:

$\frac\left\{\mathrm\left\{d\right\}^2\chi_1^2\right\}\left\{\mathrm\left\{d\right\}\alpha^2\right\} = 8 \frac\left\{D^2\right\}\left\{\left(N - 2 \alpha\right)^3\right\} + 2 \frac\left\{W^2 + L^2\right\}\left\{\alpha^3\right\}$

so

\begin\left\{align\right\} \left. \frac\left\{\mathrm\left\{d\right\}^2\chi_1^2\right\}\left\{\mathrm\left\{d\right\}\alpha^2\right\} \right|_\left\{\alpha=\alpha_0\right\} & = 8 \frac\left\{D^2\right\}\left\{\alpha_0^3\right\} \left\left( \frac\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\}\left\{2 D\right\} \right\right)^3 + 2 \frac\left\{W^2 + L^2\right\}\left\{\alpha_0^3\right\} \\ & = \frac\left\{2 \left(W^2 + L^2\right)\right\}\left\{\alpha_0^3\right\} \frac\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\}\left\{D\right\} + 2 \frac\left\{W^2 + L^2\right\}\left\{\alpha_0^3\right\} \\ & = 2 \frac\left\{W^2 + L^2\right\}\left\{\alpha_0^3\right\} \left\left( \frac\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\}\left\{D\right\} + 1 \right\right) \\ & > 0 \end\left\{align\right\}

$\chi_1^2$ is thus minimal at α₀, and its value is:

\begin\left\{align\right\} \left. \chi_1^2 \right|_\left\{\alpha = \alpha_0\right\} & = \frac\left\{D^2\right\}\left\{\alpha_0\right\} \frac\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\}\left\{2 D\right\} + \frac\left\{W^2 + L^2\right\}\left\{\alpha_0\right\} - N \\ & = \frac\left\{2\right\}\left\{N\right\} \frac\left\{D + \sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\}\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\} \frac\left\{D \sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\}\left\{2\right\} + \frac\left\{2\right\}\left\{N\right\} \left\left[ \frac\left\{D\right\}\left\{\sqrt\left\{2 \left(W^2 + L^2\right)\right\}\right\} + 1 \right\right] \left(W^2 + L^2\right) - N \\ & = \frac\left\{D\right\}\left\{N\right\} \left\left[ D + \sqrt\left\{2 \left(W^2 + L^2\right)\right\} \right\right] + \frac\left\{1\right\}\left\{N\right\} \left\left[ D \sqrt\left\{2 \left(W^2 + L^2\right)\right\} + 2 \left(W^2 + L^2\right) \right\right] - N \\ & = \frac\left\{D^2 + 2 D \sqrt\left\{2 \left(W^2 + L^2\right)\right\} + 2 \left(W^2 + L^2\right)\right\}\left\{N\right\} - N \\ & = \frac\left\{\left\left[ D + \sqrt\left\{2 \left(W^2 + L^2\right)\right\} \right\right]^2\right\}\left\{N\right\} - N \end\left\{align\right\}

A χ² test statistic tells only how far we are from H₀, without a notion of direction: Its value is the same by exchanging W and L. As we have only 1 degree of freedom, we can introduce a direction by adding a sign to the test statistic (Note: It has no meaning for more than 1 d.f.). As we want to evaluate the new bot against the old one, we want the test statistic to be positive if W > L and negative if W < L (given H₀, it is already 0 if W = L):

$T = \operatorname\left\{sign\right\}\left(W-L\right) \times \left. \chi_1^2 \right|_\left\{\alpha = \alpha_0\right\}$

Note: You don’t need to bother about sign(0), as the right-hand multiplier’s value is 0 if W = L. So you can simply implement the sign function as sign(W, L) := W > L ? 1 : -1.

### Numerical interpretation

Below is a table giving the relation between the absolute value of T and the invert of the asymptotic one-tailed p-value, i.e., of the probability p₁ that mere luck could have given the observed result or something more extreme, for some values of T. Graphs above show that p₁ is not about “better than” but about “more extreme than”, i.e., “farther from 0”, hence the flip when W < L. Values for 1/p₁ are rounded to 2 significant digits only: The test statistic is asymptotic so the probability is not exact, and the precise probability is not important, its order of magnitude is enough for a good feeling of what it represents.

|T|1/p
02
101300
20260ᴇ3
3046ᴇ6
407.9ᴇ9
501.3ᴇ12
60210ᴇ12
7034ᴇ15
805.3ᴇ18
90840ᴇ18
100130ᴇ21
Proof

|T| is $\chi_1^2$, Pearson’s χ² test statistic with 1 degree of freedom. The χ² distribution with 1 degree of freedom has PDF:

$\chi_1^2\left(t\right) = \frac\left\{\operatorname\left\{e\right\}^\left\{-t/2\right\}\right\}\left\{\sqrt\left\{2 \pi t\right\}\right\}$

Note: The symbol “$\chi_1^2$” is shared by the test statistic value and the distribution function, even though they are two different mathematical objects: They must not be confused.

By definition of Pearson’s χ² test statistic with 1 d.f. the asymptotic two-tailed p-value p₂ for |T| verifies:

\begin\left\{align\right\} 1 - p_2 & = \int_0^\left\{|T|\right\} \chi_1^2\left(t\right) \,\mathrm\left\{d\right\}t \\ & = \int_0^\left\{|T|\right\} \frac\left\{\operatorname\left\{e\right\}^\left\{-t/2\right\}\right\}\left\{\sqrt\left\{2 \pi t\right\}\right\} \,\mathrm\left\{d\right\}t \\ & \underset\left\{t = x^2\right\}\left\{=\right\} \int_0^\left\{\sqrt\left\{|T|\right\}\right\} \frac\left\{\operatorname\left\{e\right\}^\left\{-\frac\left\{x^2\right\}\left\{2\right\}\right\}\right\}\left\{\sqrt\left\{2 \pi\right\} x\right\} 2 x \,\mathrm\left\{d\right\}x \\ & = 2 \int_0^\left\{\sqrt\left\{|T|\right\}\right\} \frac\left\{1\right\}\left\{\sqrt\left\{2 \pi\right\}\right\} \operatorname\left\{e\right\}^\left\{-\frac\left\{x^2\right\}\left\{2\right\}\right\} \,\mathrm\left\{d\right\}x \\ & = \int_\left\{-\sqrt\left\{|T|\right\}\right\}^\left\{\sqrt\left\{|T|\right\}\right\} \frac\left\{1\right\}\left\{\sqrt\left\{2 \pi\right\}\right\} \operatorname\left\{e\right\}^\left\{-\frac\left\{x^2\right\}\left\{2\right\}\right\} \,\mathrm\left\{d\right\}x \\ & = \operatorname\left\{erf\right\}\left\left( \frac\left\{\sqrt\left\{|T|\right\}\right\}\left\{\sqrt\left\{2\right\}\right\} \right\right) \end\left\{align\right\}

with “erf” the error function. So

$p_2 = 1 - \operatorname\left\{erf\right\}\left\left( \sqrt\left\{\frac\left\{|T|\right\}\left\{2\right\}\right\} \right\right) = \operatorname\left\{erfc\right\}\left\left( \sqrt\left\{\frac\left\{|T|\right\}\left\{2\right\}\right\} \right\right)$

with “erfc” the complementary error function. As p₁ = p₂/2, we finally have 1/p₁ = 2/erfc(√(|T|/2)).

For instance, as the world population will soon reach 8 billion people, we can expect that, at any given time, someone on the planet is reaching a statistical significance of |T| ≈ 40 for some fair random process: Probably someone else, but possibly you while measuring your new bot performance (very unlikely, but still reasonably possible).

### Formula of win rate

There are several ways to define a win rate taking drawn matches into account, the simplest one is to count draws as half wins and half losses:

$\rho = \frac\left\{W + \frac\left\{D\right\}\left\{2\right\}\right\}\left\{N\right\}$

### Practical use

Use a run length limit of 5000 to 10,000 matches (depending on the time it would take) and a threshold for |T| around 50:

• no less than 40, as 1/p₁ would be below the world population,
• no more than 60, as the statistical significance is high enough.

Once the length limit or the threshold is reached, launch a new independent run in order to verify that the result is similar. Note: in case of win rate being precisely 50 % (identical bots) or extremely close to that, T will vary randomly in the [−10, 10] range (with possible temporary pikes outside that range), so the first run may end with T ≈ 10 when the length limit is reached (after a lot of movements in the range) and the second run with T ≈ −10, they will nevertheless be “similar” in the sense that |T| does not increase visibly on average.