Skip to content
Closed
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions maths/elo_expected_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import math

Check failure on line 1 in maths/elo_expected_score.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

maths/elo_expected_score.py:1:1: I001 Import block is un-sorted or un-formatted

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.

Check failure on line 7 in maths/elo_expected_score.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

maths/elo_expected_score.py:7:1: W293 Blank line contains whitespace
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():
Copy link
Copy Markdown

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}")

Check failure on line 34 in maths/elo_expected_score.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

maths/elo_expected_score.py:34:66: W292 No newline at end of file
Loading