Here is very simple python code to implement the SPRT a la cutechess-cli or fishtest
Code: Select all
from __future__ import division
import math
bb=math.log(10)/400
def LL(x):
return 1/(1+math.exp(-bb*x))
def TrivialLLR(W,D,L,elo0,elo1):
"""
This function computes the log likelihood ratio of H0:elo=elo1 versus
H1:elo=elo1 under the trinomial (w/d/l) logistic model. W/D/L are
respectively the Win/Draw/Loss count.
For details see
http://hardy.uhasselt.be/Toga/GSPRT_approximation.pdf
"""
# to avoid division by zero
if W==0 or D==0 or L==0:
return 0.0
N=W+D+L
w,d,l=W/N,D/N,L/N
s=w+d/2
m2=w+d/4
var=m2-s**2
var_s=var/N
s0=LL(elo0)
s1=LL(elo1)
return (s1-s0)*(2*s-s0-s1)/var_s/2.0
def TrivialSPRT(W,D,L,elo0,elo1,alpha,beta):
"""
This function should be called after each game until it returns either
'H0' or 'H1' in which case the test stops.
alpha, beta are the type I,II error probabilities. The other parameters
are as in the description of the function TrivialLLR().
"""
LLR=TrivialLLR(W,D,L,elo0,elo1)
LA=math.log(beta/(1-alpha))
LB=math.log((1-beta)/alpha)
if LLR>LB:
return 'H1'
elif LLR<LA:
return 'H0'
else:
return ''