Skip to content

Instantly share code, notes, and snippets.

@SreedevSB
Created January 17, 2021 11:53
Show Gist options
  • Save SreedevSB/b0b6b0a31c137fbf9fe7baff6bc88169 to your computer and use it in GitHub Desktop.
Save SreedevSB/b0b6b0a31c137fbf9fe7baff6bc88169 to your computer and use it in GitHub Desktop.
Python function to calculate changed elo rating after a chess match
def calc_new_elo(a,b,winner, k=20):
# a = rating of a
# b = rating of b
# if a wins then winner = 1
# if b wins then winner = 2
# if draw then winner = 0.5
# k factor default value is 20
a_expected = 1/(1+10**((b-a)/400))
b_expected = 1/(1+10**((a-b)/400))
if winner == 1:
a_scored = 1
b_scored = 0
elif winner == 2:
a_scored = 0
b_scored = 1
elif winner == 0:
a_scored = 0.5
b_scored = 0.5
a_new = a + k * (a_scored - a_expected)
b_new = b + k * (b_scored - b_expected)
print(a_new,b_new)
calc_new_elo(2100, 1900, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment