From 1a95253055adac5e40671ea6b8c881b156ed928b Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 8 May 2024 21:33:41 -0400 Subject: [PATCH] Use Pentanomial error bars for tests with use_penta These elo error bars, and the elo estimate itself, are still WRONG for SPRT tests, as there is no accounting for the bias introduced by the SPRT cutoff condition. Refering to elo values, or their error bars, from SPRT tests, is flawed at best. This change does however provide proper error bars for fixed-game and datagen workloads --- OpenBench/models.py | 12 +++++++++++ OpenBench/stats.py | 35 ++++++++++++-------------------- OpenBench/templatetags/mytags.py | 14 ++++++------- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/OpenBench/models.py b/OpenBench/models.py index c7ec611..ded4005 100644 --- a/OpenBench/models.py +++ b/OpenBench/models.py @@ -171,6 +171,18 @@ class Test(Model): def __str__(self): return '{0} vs {1} @ {2}'.format(self.dev.name, self.base.name, self.dev_time_control) + def results(self): + return self.as_tri() if self.use_tri else self.as_penta() + + def as_tri(self): + return (self.losses, self.draws, self.wins) + + def as_penta(self): + return (self.LL, self.LD, self.DD, self.DW, self.WW) + + def as_nwld(self): + return (self.games, self.wins, self.losses, self.draws) + class LogEvent(Model): author = CharField(max_length=128) # Username for the OpenBench Profile diff --git a/OpenBench/stats.py b/OpenBench/stats.py index 72d8570..1dc4916 100644 --- a/OpenBench/stats.py +++ b/OpenBench/stats.py @@ -21,11 +21,10 @@ # The implementation for PentanomialSPRT is taken directly from Fishtest. # The implementation for TrinomialSPRT was derived directory from Fishtest. # -# # Only three functions should be used externally from this Module. # 1. llr = TrinomialSPRT([losses, draws, wins], elo0, elo1) # 2. llr = PentanomialSPRT([ll, ld, dd, dw, ww], elo0, elo1) -# 3. elo = ELO([losses, draws, wins]) +# 3. lower, elo, upper = Elo((L, D, W) or (LL, LD, DD/WL, DW, WW)) import math import scipy @@ -71,27 +70,20 @@ def PentanomialSPRT(results, elo0, elo1): return N * stats(mle_pdf)[0] -def ELO(results): +def Elo(results): - # Need at least one result to do the computation - if all(not x for x in results): + # Cannot compute elo without any games + if not (N := sum(results)): return (0.00, 0.00, 0.00) - N = sum(results) - l, d, w = [x / N for x in results] + div = len(results) - 1 # Converts index to the points outcome + mu = sum((f / div) * results[f] for f in range(len(results))) / N + var = sum(((f / div) - mu)**2 * results[f] for f in range(len(results))) / N - # mu is the empirical mean of the variables (Xi), assumed i.i.d. - mu = w + d / 2 - - # stdev is the empirical standard deviation of the random variable (X1+...+X_N)/N - stdev = math.sqrt(w*(1-mu)**2 + l*(0-mu)**2 + d*(0.5-mu)**2) / math.sqrt(N) - - # 95% confidence interval for mu - mu_min = mu + scipy.stats.norm.ppf(0.025) * stdev - mu_max = mu + scipy.stats.norm.ppf(0.975) * stdev - - return (logistic_elo(mu_min), logistic_elo(mu), logistic_elo(mu_max)) + mu_min = mu + scipy.stats.norm.ppf(0.025) * math.sqrt(var) / math.sqrt(N) + mu_max = mu + scipy.stats.norm.ppf(0.975) * math.sqrt(var) / math.sqrt(N) + return logistic_elo(mu_min), logistic_elo(mu), logistic_elo(mu_max) def bayeselo_to_proba(elo, draw_elo): @@ -164,10 +156,9 @@ def MLE_tvalue(pdfhat, ref, s): return pdf_MLE -def logistic_elo(mu): - if mu <= 0 or mu >= 1: - return 0.0 - return -400 * math.log10(1 / mu - 1) +def logistic_elo(x): + x = min(max(x, 1e-3), 1-1e-3) + return -400 * math.log10(1 / x - 1) if __name__ == '__main__': diff --git a/OpenBench/templatetags/mytags.py b/OpenBench/templatetags/mytags.py index 50a82da..bd9ef1d 100644 --- a/OpenBench/templatetags/mytags.py +++ b/OpenBench/templatetags/mytags.py @@ -60,8 +60,8 @@ def gitDiffLink(test): def shortStatBlock(test): - tri_line = 'Games: %d W: %d L: %d D: %d' % (test.games, test.wins, test.losses, test.draws) - penta_line = 'Pntml(0-2): %d, %d, %d, %d, %d' % (test.LL, test.LD, test.DD, test.DW, test.WW) + tri_line = 'Games: %d W: %d L: %d D: %d' % test.as_nwld() + penta_line = 'Pntml(0-2): %d, %d, %d, %d, %d' % test.as_penta() if test.test_mode == 'SPSA': statlines = [ @@ -75,13 +75,13 @@ def shortStatBlock(test): statlines = [llr_line, tri_line, penta_line] if test.use_penta else [llr_line, tri_line] elif test.test_mode == 'GAMES': - lower, elo, upper = OpenBench.stats.ELO([test.losses, test.draws, test.wins]) + lower, elo, upper = OpenBench.stats.Elo(test.results()) elo_line = 'Elo: %0.2f +- %0.2f (95%%) [N=%d]' % (elo, max(upper - elo, elo - lower), test.max_games) statlines = [elo_line, tri_line, penta_line] if test.use_penta else [elo_line, tri_line] elif test.test_mode == 'DATAGEN': status_line = 'Generated %d/%d Games' % (test.games, test.max_games) - lower, elo, upper = OpenBench.stats.ELO([test.losses, test.draws, test.wins]) + lower, elo, upper = OpenBench.stats.Elo(test.results()) elo_line = 'Elo: %0.2f +- %0.2f (95%%) [N=%d]' % (elo, max(upper - elo, elo - lower), test.max_games) statlines = [status_line, elo_line, penta_line] if test.use_penta else [status_line, elo_line, tri_line] @@ -96,7 +96,7 @@ def longStatBlock(test): timecontrol = test.dev_time_control + ['s', '']['=' in test.dev_time_control] type_text = 'SPRT' if test.test_mode == 'SPRT' else 'Conf' - lower, elo, upper = OpenBench.stats.ELO([test.losses, test.draws, test.wins]) + lower, elo, upper = OpenBench.stats.Elo(test.results()) lines = [ 'Elo | %0.2f +- %0.2f (95%%)' % (elo, max(upper - elo, elo - lower)), @@ -107,10 +107,10 @@ def longStatBlock(test): lines.append('LLR | %0.2f (%0.2f, %0.2f) [%0.2f, %0.2f]' % ( test.currentllr, test.lowerllr, test.upperllr, test.elolower, test.eloupper)) - lines.append('Games | N: %d W: %d L: %d D: %d' % (test.games, test.wins, test.losses, test.draws)) + lines.append('Games | N: %d W: %d L: %d D: %d' % test.as_nwld()) if test.use_penta: - lines.append('Penta | [%d, %d, %d, %d, %d]' % (test.LL, test.LD, test.DD, test.DW, test.WW)) + lines.append('Penta | [%d, %d, %d, %d, %d]' % test.as_penta()) return '\n'.join(lines)