Elo Rating Algorithm - GeeksforGeeks

20 Mar, 2018
Elo Rating Algorithm is widely used rating algorithm that is used to rank players in many competitive games.Players with higher ELO rating have a higher probability of winning a game than a player with lower ELO rating. After each game, ELO rating of players is updated. If a player with higher ELO rating wins, only a few points are transferred from the lower rated player. However if lower rated player wins, then transferred points from a higher rated player are far greater.
Approach:P1: Probability of winning of player with rating2P2: Probability of winning of player with rating1.
P1 = (1.0 / (1.0 + pow(10, ((rating1 – rating2) / 400))));P2 = (1.0 / (1.0 + pow(10, ((rating2 – rating1) / 400))));Obviously, P1 + P2 = 1.
The rating of player is updated using the formula given below :-
rating1 = rating1 + K*(Actual Score – Expected score);
In most of the games, “Actual Score” is either 0 or 1 means player either wins or loose. K is a constant. If K is of a lower value, then the rating is changed by a small fraction but if K is of a higher value, then the changes in the rating are significant. Different organizations set a different value of K.
Example:
Suppose there is a live match on chess.com between two playersrating1 = 1200, rating2 = 1000;P1 = (1.0 / (1.0 + pow(10, ((1000-1200) / 400)))) = 0.76P2 = (1.0 / (1.0 + pow(10, ((1200-1000) / 400)))) = 0.24
And Assume constant K=30;
CASE-1 : Suppose Player 1 wins:rating1 = rating1 + k*(actual – expected) = 1200+30(1 – 0.76) = 1207.2;rating2 = rating2 + k*(actual – expected) = 1000+30(0 – 0.24) = 992.8;
Case-2 : Suppose Player 2 wins:rating1 = rating1 + k*(actual – expected) = 1200+30(0 – 0.76) = 1177.2;rating2 = rating2 + k*(actual – expected) = 1000+30(1 – 0.24) = 1022.8;
CPP
Java
Python3
C#
// CPP program for Elo Rating#include <bits/stdc++.h>using namespace std;  // Function to calculate the Probabilityfloat Probability(int rating1, int rating2){    return 1.0 * 1.0 / (1 + 1.0 *            pow(10, 1.0 * (rating1 - rating2) / 400));}  // Function to calculate Elo rating// K is a constant.// d determines whether Player A wins or Player B. void EloRating(float Ra, float Rb, int K, bool d){        // To calculate the Winning    // Probability of Player B    float Pb = Probability(Ra, Rb);      // To calculate the Winning    // Probability of Player A    float Pa = Probability(Rb, Ra);      // Case -1 When Player A wins    // Updating the Elo Ratings    if (d == 1) {        Ra = Ra + K * (1 - Pa);        Rb = Rb + K * (0 - Pb);    }      // Case -2 When Player B wins    // Updating the Elo Ratings    else {        Ra = Ra + K * (0 - Pa);        Rb = Rb + K * (1 - Pb);    }      cout << "Updated Ratings:-\n";    cout << "Ra = " << Ra << " Rb = " << Rb;}  int main(){    // Ra and Rb are current ELO ratings    float Ra = 1200, Rb = 1000;          int K = 30;    bool d = 1;    EloRating(Ra, Rb, K, d);      return 0;}
// Java program to count rotationally // equivalent rectangles with n unit // squaresclass GFG {      // Function to calculate the Probability    static float Probability(float rating1,                                float rating2)    {        return 1.0f * 1.0f / (1 + 1.0f *                 (float)(Math.pow(10, 1.0f *                (rating1 - rating2) / 400)));    }          // Function to calculate Elo rating    // K is a constant.    // d determines whether Player A wins    // or Player B.     static void EloRating(float Ra, float Rb,                            int K, boolean d)    {               // To calculate the Winning        // Probability of Player B        float Pb = Probability(Ra, Rb);              // To calculate the Winning        // Probability of Player A        float Pa = Probability(Rb, Ra);              // Case -1 When Player A wins        // Updating the Elo Ratings        if (d == true) {            Ra = Ra + K * (1 - Pa);            Rb = Rb + K * (0 - Pb);        }              // Case -2 When Player B wins        // Updating the Elo Ratings        else {            Ra = Ra + K * (0 - Pa);            Rb = Rb + K * (1 - Pb);        }              System.out.print("Updated Ratings:-\n");                  System.out.print("Ra = " + (Math.round(                   Ra * 1000000.0) / 1000000.0)                     + " Rb = " + Math.round(Rb                       * 1000000.0) / 1000000.0);    }          //driver code    public static void main (String[] args)    {                  // Ra and Rb are current ELO ratings        float Ra = 1200, Rb = 1000;                  int K = 30;        boolean d = true;                  EloRating(Ra, Rb, K, d);    }}  // This code is contributed by Anant Agarwal.
# Python 3 program for Elo Ratingimport math  # Function to calculate the Probabilitydef Probability(rating1, rating2):      return 1.0 * 1.0 / (1 + 1.0 * math.pow(10, 1.0 * (rating1 - rating2) / 400))    # Function to calculate Elo rating# K is a constant.# d determines whether# Player A wins or Player B. def EloRating(Ra, Rb, K, d):         # To calculate the Winning    # Probability of Player B    Pb = Probability(Ra, Rb)      # To calculate the Winning    # Probability of Player A    Pa = Probability(Rb, Ra)      # Case -1 When Player A wins    # Updating the Elo Ratings    if (d == 1) :        Ra = Ra + K * (1 - Pa)        Rb = Rb + K * (0 - Pb)            # Case -2 When Player B wins    # Updating the Elo Ratings    else :        Ra = Ra + K * (0 - Pa)        Rb = Rb + K * (1 - Pb)            print("Updated Ratings:-")    print("Ra =", round(Ra, 6)," Rb =", round(Rb, 6))  # Driver code  # Ra and Rb are current ELO ratingsRa = 1200Rb = 1000K = 30d = 1EloRating(Ra, Rb, K, d)  # This code is contributed by# Smitha Dinesh Semwal
// C# program to count rotationally equivalent// rectangles with n unit squaresusing System;  class GFG {          // Function to calculate the Probability    static float Probability(float rating1,                                  float rating2)    {        return 1.0f * 1.0f / (1 + 1.0f *                (float)(Math.Pow(10, 1.0f *                 (rating1 - rating2) / 400)));    }           // Function to calculate Elo rating    // K is a constant.    // d determines whether Player A wins or    // Player B.     static void EloRating(float Ra, float Rb,                                int K, bool d)    {                 // To calculate the Winning        // Probability of Player B        float Pb = Probability(Ra, Rb);               // To calculate the Winning        // Probability of Player A        float Pa = Probability(Rb, Ra);               // Case -1 When Player A wins        // Updating the Elo Ratings        if (d == true) {            Ra = Ra + K * (1 - Pa);            Rb = Rb + K * (0 - Pb);        }               // Case -2 When Player B wins        // Updating the Elo Ratings        else {            Ra = Ra + K * (0 - Pa);            Rb = Rb + K * (1 - Pb);        }               Console.Write("Updated Ratings:-\n");                  Console.Write("Ra = " + (Math.Round(Ra                      * 1000000.0) / 1000000.0)                     + " Rb = " + Math.Round(Rb                     * 1000000.0) / 1000000.0);    }          //driver code    public static void Main()    {                  // Ra and Rb are current ELO ratings        float Ra = 1200, Rb = 1000;                   int K = 30;        bool d = true;        EloRating(Ra, Rb, K, d);    }}  // This code is contributed by Anant Agarwal.

Updated Ratings:-
Ra = 1207.207642 Rb = 992.792419

Time ComplexityTime complexity of algorithm depends mostly on the complexity of pow function whosecomplexity is dependent on Computer Architecture.On x86, this is constant time operation:-O(1)
Referenceshttp://www.gautamnarula.com/rating/
Articles
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Time Complexity and Space Complexity
Docker - COPY Instruction
Time complexities of different data structures
SQL | Date functions
Implementation of LinkedList in Javascript
Difference between Min Heap and Max Heap
Difference between Class and Object
Deploy Python Flask App on Heroku
SQL | Functions (Aggregate and Scalar Functions)
How compare() method works in Java