{ "cells": [ { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "import math\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initial conditions" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "initial_rating = 400\n", "k = 100\n", "\n", "things = ['Malted Milk','Rich Tea','Hobnob','Digestive']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Elo Algos" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [], "source": [ "def expected_win(r1, r2):\n", " \"\"\"\n", " Expected probability of player 1 beating player 2\n", " if player 1 has rating 1 (r1) and player 2 has rating 2 (r2)\n", " \"\"\"\n", " return 1.0 / (1 + math.pow(10, (r2-r1)/400))\n", "\n", "def update_rating(R, k, P, d):\n", " \"\"\"\n", " d = 1 = WIN\n", " d = 0 = LOSS\n", " \"\"\"\n", " return R + k*(d-P)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "def elo(Ra, Rb, k, d):\n", " \"\"\"\n", " d = 1 when player A wins\n", " d = 0 when player B wins\n", " \"\"\"\n", " \n", " Pa = expected_win(Ra, Rb)\n", " Pb = expected_win(Rb, Ra)\n", " \n", " # update if A wins\n", " if d == 1:\n", " Ra = update_rating(Ra, k, Pa, d)\n", " Rb = update_rating(Rb, k, Pb, d-1)\n", " \n", " # update if B wins\n", " elif d == 0:\n", " Ra = update_rating(Ra, k, Pa, d)\n", " Rb = update_rating(Rb, k, Pb, d+1)\n", " \n", " return Pa, Pb, Ra, Rb" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [], "source": [ "def elo_sequence(things, initial_rating, k, results):\n", " \"\"\"\n", " Initialises score dictionary, and runs through sequence of pairwise results, returning final dictionary of Elo rankings\n", " \"\"\"\n", "\n", " dic_scores = {i:initial_rating for i in things}\n", "\n", " for result in results:\n", "\n", " winner, loser = result\n", " Ra, Rb = dic_scores[winner], dic_scores[loser]\n", " _, _, newRa, newRb = elo(Ra, Rb, k, 1)\n", " dic_scores[winner], dic_scores[loser] = newRa, newRb\n", "\n", " return dic_scores" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Mean Elo" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [], "source": [ "def mElo(things, initial_rating, k, results, numEpochs):\n", " \"\"\"\n", " Randomises the sequence of the pairwise comparisons, running the Elo sequence in a random\n", " sequence for a number of epochs\n", " \n", " Returns the mean Elo ratings over the randomised epoch sequences\n", " \"\"\"\n", " \n", " lst_outcomes = []\n", " \n", " for i in np.arange(numEpochs):\n", " np.random.shuffle(results)\n", " lst_outcomes.append(elo_sequence(things, initial_rating, k, results))\n", " \n", " return pd.DataFrame(lst_outcomes).mean().sort_values(ascending=False)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pairwise Outcomes from Christian's Taste Test\n", "\n", "> **Format** (Winner, Loser)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [], "source": [ "results = np.array([('Malted Milk','Rich Tea'),('Malted Milk','Digestive'),('Malted Milk','Hobnob')\\\n", " ,('Hobnob','Rich Tea'),('Hobnob','Digestive'),('Digestive','Rich Tea')])" ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [], "source": [ "jenResults = np.array([('Rich Tea','Malted Milk'),('Digestive','Malted Milk'),('Hobnob','Malted Milk')\\\n", " ,('Hobnob','Rich Tea'),('Hobnob','Digestive'),('Digestive','Rich Tea')])" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Hobnob 529.617095\n", "Digestive 442.809668\n", "Rich Tea 356.925713\n", "Malted Milk 270.647524\n", "dtype: float64" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mElo(things, initial_rating, k, jenResults, 1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "py37", "language": "python", "name": "py37" }, "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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }