Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions maths/elo_expected_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import math


def calculate_elo_expected_score(rating_a: int, rating_b: int) -> float:
"""
Calculate the expected score (probability of winning) for Player A
against Player B using Elo ratings.

The formula is: E_A = 1 / (1 + 10^((R_B - R_A) / 400)).

Args:
rating_a (int): Elo rating of Player A.
rating_b (int): Elo rating of Player B.

Returns:
float: Expected score for Player A (between 0.0 and 1.0).
"""
exponent = (rating_b - rating_a) / 400
return 1.0 / (1.0 + math.pow(10, exponent))


def test_calculate_elo_expected_score():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: test_calculate_elo_expected_score. If the function does not return a value, please provide the type hint as: def function() -> None:

# Player A higher rating
assert 0.5 < calculate_elo_expected_score(1600, 1400) < 1.0
# Player B higher rating
assert 0.0 < calculate_elo_expected_score(1400, 1600) < 0.5
# Equal ratings
assert calculate_elo_expected_score(1500, 1500) == 0.5


if __name__ == "__main__":
ra, rb = 1600, 1400
expected_a = calculate_elo_expected_score(ra, rb)
print(f"Player A Rating: {ra}")
print(f"Player B Rating: {rb}")
print(f"Expected Score for Player A: {expected_a:.4f}")
print(f"Expected Score for Player B: {1.0 - expected_a:.4f}")