From 37eb3adeda68515b9eb37a018f02805e514273dd Mon Sep 17 00:00:00 2001 From: Navodhya Fernando Date: Mon, 13 Oct 2025 10:09:12 +0530 Subject: [PATCH 1/2] =?UTF-8?q?feat(ratings):=C2=A0Add=C2=A0Python=C2=A0im?= =?UTF-8?q?plementation=C2=A0for=C2=A0Elo=C2=A0Expected=C2=A0Score=C2=A0fo?= =?UTF-8?q?rmula?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maths/elo_expected_score.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 maths/elo_expected_score.py diff --git a/maths/elo_expected_score.py b/maths/elo_expected_score.py new file mode 100644 index 000000000000..f3cd6c22c907 --- /dev/null +++ b/maths/elo_expected_score.py @@ -0,0 +1,34 @@ +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(): + # 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}") \ No newline at end of file From dce898a732175a1b802184f412ca26a5d4438ab1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Oct 2025 04:46:31 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/elo_expected_score.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/maths/elo_expected_score.py b/maths/elo_expected_score.py index f3cd6c22c907..b909fd7155ae 100644 --- a/maths/elo_expected_score.py +++ b/maths/elo_expected_score.py @@ -1,10 +1,11 @@ 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: @@ -17,6 +18,7 @@ def calculate_elo_expected_score(rating_a: int, rating_b: int) -> float: exponent = (rating_b - rating_a) / 400 return 1.0 / (1.0 + math.pow(10, exponent)) + def test_calculate_elo_expected_score(): # Player A higher rating assert 0.5 < calculate_elo_expected_score(1600, 1400) < 1.0 @@ -25,10 +27,11 @@ def test_calculate_elo_expected_score(): # 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}") \ No newline at end of file + print(f"Expected Score for Player B: {1.0 - expected_a:.4f}")