The Elo Rating algorithm, created by Arpad Elo (a Hungarian-Amerian Physics Professor), is one of the most common algoritms used for rating because of the logic & functionality behind it.
It is used by developers wanting to rank chess players to college students wanting to rank girls (Mark Zucherberg's Facemash).
But how does it work? How can I implement it into my code?
The algorith is based on two key formulas.
- The first formula tells you how to predict the outcome of the game.
- The second formula tells you how to update your rating depending on the outcome of the game.
This is how they look:
Formula A:
Formula B:
Don't get discouraged by how the formulas look! We'll go through this step by step.
When Arpad Elo first invented the algorithm, his idea was to asume that each players ability forms a bell curve.
Image that each player brings a box with them to every game. Before every game, they pull out a number from that box. Whoever has the highest number wins.
That number represent a players ability to win the match.
This means that every player has the potencial of playing at a range of different abilities.
Somedays, a player might be feel great and pick a high number.
Other days, that player might feel sick or unlucky and pick a low number.
Most of the time, however, that player picks a number in the middle of their range.
An elite player will generally be picking from a box of very high numbers. So they are more likely to win but not always...
If you pay attention at the frequency of the numbers, you'll realize that they form bell curves.
Notice how the bell curves are exactly the same. The only difference is the center of the curves.
The center of the curves represents the mean or averge and the averege is that players rating.
To get the probability of a player winning a game, we need to find the frequency of the differences between the two players.
If you plot that, you get a bell curve. It's called a logistic curve.
The idea is that the more of the curve is to the right of 0, the more likely that player is to win.
Basically, the Elo Rating Algorithm is designed so that if a player has a rating that is 400 points more than another player, they are 10 times more likely to win.
So in the curve, the area to the right of 0 would be 10 times the area of the curve to the left of 0.
If you turn that into a formula, this is the formula that you would get:
So, If Player A has a rating of Ra, and Player B has a rating of Rb, the exact formula for the expected score of Player A is:
You can simmilarly look at the probability for Player B to win by:
With this, you can predict who will win.
EXAMPLE: If the probability of winning is 1, your going to win the game. If the probability of winning is 0, your going to loose the game.
IMPORTANT: If the probability of winning is 0.5 (half), then your going to win as many games as you loose. Basically a draw.
Now, lets say that:
Win | Loose | Draw |
---|---|---|
1 | 0 | 0.5 |
Then, the probability can be used as the expected score.
It can be wierd like if you have a probability of 0.75.
This means that you could win 75% of the games and loose the other 25%.
But it also means that you coud win 50% of the games and draw the other 50% of the games.
Either way, we would say the expected score is 0.75
Whenever a player does better than expected, their rating will increse. The more surprising that the win is, the more points they will get. Up to a max of 32 points.
Nothing special about 32, just a choice that they made.
The same way, if a player does worse than expected, their rating will decrease up to 32 poibts.
Throughout a tournament, a players raiting is updated using an update formula.
This is how said formula looks:
For Example: Let's say that player A has a rating of 1656 and Player B has a rating of 1763. Since player A is the weaker player, he's expected to loose. His expexted win is 0.35 . However, he actually wins the game.
If this was the case, we would apply the formula like this:
In the next few paragraphs, I will explain how and why I did this. Keep reading in if you want to:
a) Feel bad for me.
b) Have a couple laughs
c) Be thankfull that you didn't have to do it yourself.
This project originated when my friend (Filippo) asked me to bet on which team was going to with the 2020, NBA title. I said "Sure!", and proceeded to overcomplicate my life since I knew very little (basically nothing) about basketball.
The idea was to create a program that would run every possible scenerio for which a team could win depending on how many games the team had won. Of couse, it would have been 10 times more effective to just do reseach and make an educated guess, but there was one, criticall problem with that take. It was simply not a cool enough option.
I knew I wanted to include the The Social Network's rating algorithm but instead of ranking girls, I would rank teams. So I looked up the formula online and vuala, there it was, The Elo Rating System in all its glory.
I then watched a couple videos, read some medium articles and proceeded to rethink my life decisions. After some close encounters with mental breakdowns, I ran the command which would determain if the last 48 hours of my life were worth it...
ts-node algorithm.ts
The next 5 seconds felt like they would never end. I closed my eyes and of course it didn't work. So I was back to the drawing board. And after many tries and soooo mannyyyyyy erorrrrssssss, it worked.