Skip to content

Commit 5e918f5

Browse files
authored
Fix ranking calculation for ties. (#174)
1 parent 11ac382 commit 5e918f5

File tree

7 files changed

+28
-6
lines changed

7 files changed

+28
-6
lines changed

changes/174.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix rank calculation for ties.

openskill/models/weng_lin/bradley_terry_full.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ def _calculate_rankings(
10711071
if ranks:
10721072
team_scores = []
10731073
for index, _ in enumerate(game):
1074-
team_scores.append(ranks[index] or index)
1074+
team_scores.append(ranks[index] if ranks[index] is not None else index)
10751075
else:
10761076
team_scores = [i for i, _ in enumerate(game)]
10771077

openskill/models/weng_lin/bradley_terry_part.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ def _calculate_rankings(
10671067
if ranks:
10681068
team_scores = []
10691069
for index, _ in enumerate(game):
1070-
team_scores.append(ranks[index] or index)
1070+
team_scores.append(ranks[index] if ranks[index] is not None else index)
10711071
else:
10721072
team_scores = [i for i, _ in enumerate(game)]
10731073

openskill/models/weng_lin/plackett_luce.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ def _calculate_rankings(
11321132
if ranks:
11331133
team_scores = []
11341134
for index, _ in enumerate(game):
1135-
team_scores.append(ranks[index] or index)
1135+
team_scores.append(ranks[index] if ranks[index] is not None else index)
11361136
else:
11371137
team_scores = [i for i, _ in enumerate(game)]
11381138

openskill/models/weng_lin/thurstone_mosteller_full.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ def _calculate_rankings(
11191119
if ranks:
11201120
team_scores = []
11211121
for index, _ in enumerate(game):
1122-
team_scores.append(ranks[index] or index)
1122+
team_scores.append(ranks[index] if ranks[index] is not None else index)
11231123
else:
11241124
team_scores = [i for i, _ in enumerate(game)]
11251125

openskill/models/weng_lin/thurstone_mosteller_part.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ def _calculate_rankings(
11101110
if ranks:
11111111
team_scores = []
11121112
for index, _ in enumerate(game):
1113-
team_scores.append(ranks[index] or index)
1113+
team_scores.append(ranks[index] if ranks[index] is not None else index)
11141114
else:
11151115
team_scores = [i for i, _ in enumerate(game)]
11161116

tests/models/weng_lin/test_common.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_calculate_rankings(model) -> None:
8181
assert model._calculate_rankings([]) == []
8282
assert model._calculate_rankings([], []) == []
8383
assert model._calculate_rankings([a, b, c, d]) == [0, 1, 2, 3]
84-
assert model._calculate_rankings([a, b], [0, 0]) == [0, 1]
84+
assert model._calculate_rankings([a, b], [0, 0]) == [0, 0]
8585
assert model._calculate_rankings([a, b, c, d], [1, 2, 3, 4]) == [0, 1, 2, 3]
8686
assert model._calculate_rankings([a, b, c, d], [1, 1, 3, 4]) == [0, 0, 2, 3]
8787
assert model._calculate_rankings([a, b, c, d], [1, 2, 3, 3]) == [0, 1, 2, 2]
@@ -201,3 +201,24 @@ def test_ladder_pairs():
201201
assert _ladder_pairs([1, 2]) == [[2], [1]]
202202
assert _ladder_pairs([1, 2, 3]) == [[2], [1, 3], [2]]
203203
assert _ladder_pairs([1, 2, 3, 4]) == [[2], [1, 3], [2, 4], [3]]
204+
205+
206+
@pytest.mark.parametrize("model", MODELS)
207+
@pytest.mark.parametrize("tie_score", [-1, 0, 0.1, 10, 13.4])
208+
def test_ties(model, tie_score):
209+
model_instance = model()
210+
211+
player_1 = model_instance.rating()
212+
player_2 = model_instance.rating()
213+
214+
result = model_instance.rate(
215+
[[player_1], [player_2]], scores=[tie_score, tie_score]
216+
)
217+
218+
# Both players should have the same rating change
219+
assert (
220+
result[0][0].mu == result[1][0].mu
221+
), f"Model {model.__name__} with score {tie_score}: Players should have equal mu after tie"
222+
assert (
223+
result[0][0].sigma == result[1][0].sigma
224+
), f"Model {model.__name__} with score {tie_score}: Players should have equal sigma after tie"

0 commit comments

Comments
 (0)