Skip to content

Commit 37eb3ad

Browse files
feat(ratings): Add Python implementation for Elo Expected Score formula
1 parent 788d95b commit 37eb3ad

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

maths/elo_expected_score.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import math
2+
3+
def calculate_elo_expected_score(rating_a: int, rating_b: int) -> float:
4+
"""
5+
Calculate the expected score (probability of winning) for Player A
6+
against Player B using Elo ratings.
7+
8+
The formula is: E_A = 1 / (1 + 10^((R_B - R_A) / 400)).
9+
10+
Args:
11+
rating_a (int): Elo rating of Player A.
12+
rating_b (int): Elo rating of Player B.
13+
14+
Returns:
15+
float: Expected score for Player A (between 0.0 and 1.0).
16+
"""
17+
exponent = (rating_b - rating_a) / 400
18+
return 1.0 / (1.0 + math.pow(10, exponent))
19+
20+
def test_calculate_elo_expected_score():
21+
# Player A higher rating
22+
assert 0.5 < calculate_elo_expected_score(1600, 1400) < 1.0
23+
# Player B higher rating
24+
assert 0.0 < calculate_elo_expected_score(1400, 1600) < 0.5
25+
# Equal ratings
26+
assert calculate_elo_expected_score(1500, 1500) == 0.5
27+
28+
if __name__ == "__main__":
29+
ra, rb = 1600, 1400
30+
expected_a = calculate_elo_expected_score(ra, rb)
31+
print(f"Player A Rating: {ra}")
32+
print(f"Player B Rating: {rb}")
33+
print(f"Expected Score for Player A: {expected_a:.4f}")
34+
print(f"Expected Score for Player B: {1.0 - expected_a:.4f}")

0 commit comments

Comments
 (0)