Created
April 29, 2024 17:03
-
-
Save neftlon/bd344a301e6a038f5eed6bd8079d0dcb to your computer and use it in GitHub Desktop.
Elo ranking scheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "4c16a708-8626-41eb-9b59-737ffe419cfe", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import typing, math" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"id": "40f4be54-6cd2-4d7b-94df-cc1cd834ee3b", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Elo(typing.NamedTuple):\n", | |
" ratings: dict[typing.Any, float]\n", | |
" K: float = 30.\n", | |
"\n", | |
" def log(self, winner, loser):\n", | |
" \"log a game returning a new Elo object\"\n", | |
" # compute win/lose probabilities\n", | |
" p1 = (1 / (1 + 10 ** ((self.ratings[winner] - self.ratings[loser]) / 400)))\n", | |
" p2 = (1 / (1 + 10 ** ((self.ratings[loser] - self.ratings[winner]) / 400)))\n", | |
" \n", | |
" # update probabilities\n", | |
" update = {winner: self.ratings[winner] + self.K * (1 - p2),\n", | |
" loser: self.ratings[loser] + self.K * (0 - p1)}\n", | |
"\n", | |
" # construct result\n", | |
" res = dict(self.ratings) # copy existing dict\n", | |
" res.update(update)\n", | |
" return Elo(res, K=self.K)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"id": "1fb02a30-e3d9-4faf-84f7-ebe4431b1738", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Elo(ratings={'A': 1200, 'B': 1000, 'C': 800}, K=30.0)" | |
] | |
}, | |
"execution_count": 77, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"env = Elo({\"A\": 1200, \"B\": 1000, \"C\": 800})\n", | |
"env" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"id": "86ca1bea-a211-4538-84f7-8f5409de4989", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"winner, loser = \"B\", \"A\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 79, | |
"id": "60aad676-3bff-4c7b-862c-1dbedfd4339a", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'B': 1022.7924077994387, 'A': 1177.2075922005613}" | |
] | |
}, | |
"execution_count": 79, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# compute win/lose probabilities\n", | |
"p1 = (1 / (1 + 10 ** ((env.ratings[winner] - env.ratings[loser]) / 400)))\n", | |
"p2 = (1 / (1 + 10 ** ((env.ratings[loser] - env.ratings[winner]) / 400)))\n", | |
"\n", | |
"# update probabilities\n", | |
"{winner: env.ratings[winner] + env.K * (1 - p2),\n", | |
" loser: env.ratings[loser] + env.K * (0 - p1)}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 81, | |
"id": "06eb7747-2010-4355-8ad5-0e035b9ee40e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Elo(ratings={'A': 1110.060653079418, 'B': 1063.7227544923412, 'C': 826.2165924282407}, K=30.0)" | |
] | |
}, | |
"execution_count": 81, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"env.log(winner, loser).log(winner, loser).log(winner, loser).log(\"C\", \"A\")" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.12.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment