Skip to content

Instantly share code, notes, and snippets.

@mueslo
Last active August 29, 2015 14:05
Show Gist options
  • Save mueslo/a3bfbf216b602bfd4c3b to your computer and use it in GitHub Desktop.
Save mueslo/a3bfbf216b602bfd4c3b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
import numpy as np
#ELO basics
# Expected result given 2 elo scores:
def expected(r1, r2):
sigma = 400.
return 1./(1.+pow(10., (r2-r1)/sigma))
#(minus) rating difference of r1 (r2) given result
def delta_rating(r1, r2, result):
k_factor = 32
return k_factor*(result-expected(r1,r2))
#How to read:
# player i wins against player i 50% due to symmetry
# player 1 wins against player 2 80% (hence player 2 wins against player 1 20%)
# player 1 wins against player 3 90% (hence player 3 wins against player 1 10%)
# etc.
w_example = np.array([[ 0.5, 0.8, 0.9, 0.8],
[ 0.2, 0.5, 0.6, 0.5],
[ 0.1, 0.4, 0.5, 0.5],
[ 0.2, 0.5, 0.5, 0.5]])
w_simple = np.array([[ 0.5, 0.7],
[ 0.3, 0.5]])
def eig1(M):
eigval, eigvect = np.linalg.eig(M) #calculate eigenvectors and eigenvalues
return eigvect[:,np.isclose(eigval,1.)] #select eigenvector with eigenvalue 1
def elo_scores(w, avg=1200):
assert np.all(w.T + w) == 1 and np.all(w>=0) #r<=1 follows from w.T + w == 1
if np.linalg.det(w) == 0:
print "Warning: win array with determinant zero supplied. (Identical win rates)."
v = eig1(w/np.sum(w,axis=1))
scores = v*avg*len(v)/sum(v)
return scores
@mueslo
Copy link
Author

mueslo commented Aug 13, 2014

Example:

In [107]: elo_scores(w_example)
Out[107]: array([ 1800.+0.j,  1080.+0.j,   900.+0.j,  1020.+0.j])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment