The Elo system looks mysterious from the outside, but the maths fits in a few lines of code. In this post we will build a small, dependency-free JavaScript module that computes expected scores and rating changes the way FIDE does for chess, and then test it against a worked example.
I built a browser calculator that does the same thing, so I will mention it once near the end, but everything here runs on its own in Node or the browser console.
The core idea: expected score
Every Elo calculation starts with the expected score of a player. If player A has rating Ra and player B has rating Rb, A's expected score (a number between 0 and 1, where a win is 1, a draw is 0.5 and a loss is 0) is:
E = 1 / (1 + 10^((Rb - Ra) / 400))
Two equally rated players each have an expected score of 0.5. A player rated 400 points higher has an expected score of about 0.91, which means you would expect them to collect roughly 91% of the available points over many games.
function expectedScore(ra, rb) {
return 1 / (1 + Math.pow(10, (rb - ra) / 400));
}
console.log(expectedScore(1500, 1500).toFixed(3)); // 0.500
console.log(expectedScore(1900, 1500).toFixed(3)); // 0.909
console.log(expectedScore(1500, 1900).toFixed(3)); // 0.091
Notice the two expected scores for a pair of players always add up to 1.
Turning a result into a rating change
After the game, the new rating is the old rating plus a correction:
new = old + K * (S - E)
S is the actual score (1, 0.5 or 0) and K is the development coefficient, which controls how fast ratings move. Beating a much stronger opponent gives a big positive surprise (S - E close to 1), while beating a much weaker one barely moves your rating.
FIDE K-factors
FIDE uses three values of K:
- K = 40 for players with fewer than 30 rated games, and for juniors under 18 whose rating is below 2300.
- K = 20 for players rated under 2400.
- K = 10 once a player has reached 2400 at any time.
FIDE also applies the 400-point rule: when two players are more than 400 points apart, the difference is treated as exactly 400 for the expected score calculation. Here is a function that picks K and a function that applies the change:
function kFactor({ rating, games = 30, age = 99, everReached2400 = false }) {
if (everReached2400 || rating >= 2400) return 10;
if (games < 30) return 40;
if (age < 18 && rating < 2300) return 40;
return 20;
}
function cappedDiff(ra, rb) {
// FIDE 400-point rule: never use a difference larger than 400
return Math.max(-400, Math.min(400, rb - ra));
}
function expectedFide(ra, rb) {
return 1 / (1 + Math.pow(10, cappedDiff(ra, rb) / 400));
}
function newRating(player, opponentRating, score) {
const e = expectedFide(player.rating, opponentRating);
const k = kFactor(player);
return Math.round((player.rating + k * (score - e)) * 10) / 10;
}
(Real FIDE lists round to whole points, so you may want Math.round(x) rather than one decimal place.)
A worked example
Take a player rated 1600 with more than 30 games who beats a 1800-rated opponent:
const me = { rating: 1600, games: 120, age: 30 };
const e = expectedFide(1600, 1800);
console.log(e.toFixed(3)); // 0.240
console.log(kFactor(me)); // 20
console.log(newRating(me, 1800, 1)); // 1615.2
console.log(newRating(me, 1800, 0.5)); // 1605.2
console.log(newRating(me, 1800, 0)); // 1595.2
The expected score is 0.24, so a win is worth 20 * (1 - 0.24) = 15.2 points, a draw 20 * (0.5 - 0.24) = 5.2 points, and a loss costs 20 * 0.24 = 4.8 points. The win/loss swing is always K, split according to how surprising the result was.
Performance rating for a tournament
You can also estimate a tournament performance rating with the simple FIDE-style method: average opponent rating plus a lookup of the rating difference for your percentage score. A compact approximation uses the inverse of the expected score formula:
function performanceRating(opponents, points) {
const avg = opponents.reduce((a, b) => a + b, 0) / opponents.length;
const p = Math.min(Math.max(points / opponents.length, 0.01), 0.99);
const diff = -400 * Math.log10(1 / p - 1);
return Math.round(avg + diff);
}
console.log(performanceRating([1500, 1600, 1700, 1650, 1550], 3.5)); // 1747
This is the logistic version; official tables are slightly different, so treat it as an estimate.
Edge cases worth testing
- Equal ratings and a draw should give a change of exactly 0.
- The sum of rating changes for both players is zero when both use the same K.
- A gap of 600 points should behave the same as a gap of 400 for the FIDE function.
- A new player (under 30 games) moves twice as fast as an established one.
const a = { rating: 1500, games: 100, age: 30 };
const b = { rating: 1500, games: 100, age: 30 };
console.assert(newRating(a, 1500, 0.5) === 1500);
console.assert(
expectedFide(2200, 1500) === expectedFide(1900, 1500) // both capped at 400
);
Try it without writing code
If you just want the numbers for a real game, I built a free Elo rating calculator that applies the FIDE K-factors for you. I am the maker, so take that as a disclosure, but the code above is all you need if you prefer to roll your own.
Elo is a lovely example of a tiny formula doing real work, and it generalises well beyond chess: online games, sports leagues and even A/B ranking systems use close relatives of it.
How big is a rating swing? A lookup table
Because the expected score depends only on the rating gap, you can tabulate it once. The table below is for a player with K = 20 who is the higher-rated side (gap = own rating minus opponent rating):
| Gap | Expected score | Win gains | Draw gains | Loss costs |
|---|---|---|---|---|
| 0 | 0.500 | +10.0 | 0.0 | -10.0 |
| 100 | 0.640 | +7.2 | -2.8 | -12.8 |
| 200 | 0.760 | +4.8 | -5.2 | -15.2 |
| 300 | 0.849 | +3.0 | -7.0 | -17.0 |
| 400 | 0.909 | +1.8 | -8.2 | -18.2 |
Read it as a lesson in risk: a 400-point favourite risks 18.2 points to win 1.8, so one upset undoes about ten wins. The underdog sees the mirror image of the same numbers.
Common mistakes
- Rounding too early. Round only the final rating, never the expected score.
- Using the wrong K for one side. Each player has their own K, so the two rating changes are only equal and opposite when both K values match. A 1500 player with 12 games (K = 40) who beats a 1500 veteran (K = 20) gains 20 while the veteran loses 10.
- Forgetting the 400-point cap. Without it, a 2200 player beating a 1500 player is computed with a gap of 700 and the expected score is wrongly 0.98 instead of 0.91.
- Mixing rating systems. Lichess uses Glicko-2 and many sites use their own variants. Their numbers will not match what this FIDE-style function produces.
FAQ
Does Elo know who played White? No. The standard formula ignores colour, even though White scores slightly above 50% at most levels.
Why 400? It is a scale constant. A 400-point gap is defined to mean the stronger player is expected to score about 10 times as many points as the weaker one (0.909 versus 0.091).
Can ratings go below a starting value? In FIDE lists the minimum published rating is 1400 at the time of writing, but the formula itself has no floor, so a plain implementation can go lower.
Is this exactly what FIDE publishes? FIDE rounds and applies a few extra rules (rating periods, floors, tournament-level calculations), so treat this as the core of the system rather than a certified rating calculator.
Takeaway
Elo is three small functions: an expected score, a K-factor lookup and an update line. Once you have a table like the one above, most rating questions ("is this draw worth it?", "how risky is this pairing?") answer themselves.
Top comments (0)