Skip to content

Instantly share code, notes, and snippets.

@tngzng
Last active October 20, 2023 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tngzng/1282b92a288681e8309c943cbb156a4e to your computer and use it in GitHub Desktop.
Save tngzng/1282b92a288681e8309c943cbb156a4e to your computer and use it in GitHub Desktop.
Tennis Scoring: Relationship b/w Points Won + Games Won
class Game:
def __init__(self):
self.p1_points = 0
self.p2_points = 0
def who_won(self):
'''
0, 1, 2, 3, 4
0, 15, 30, 40, win (need 4 points to win, and to win by 2)
'''
if self.p1_points >= 4 and self.p1_points - self.p2_points >= 2:
return 'p1'
if self.p2_points >= 4 and self.p2_points - self.p1_points >= 2:
return 'p2'
import random
P1_CHANCES = range(50, 100, 1)
def p1_wins(percent):
return random.randrange(100) < percent
from collections import Counter
ROUNDS = 10_000
p1_results = []
for p1_chance in P1_CHANCES:
wins = Counter()
for _ in range(ROUNDS):
game = Game()
while not game.who_won():
if p1_wins(p1_chance):
game.p1_points += 1
else:
game.p2_points += 1
wins[game.who_won()] += 1
p1_result = (wins["p1"]/ROUNDS) * 100
p1_results.append(p1_result)
print(f'when p1 has a {p1_chance}% chance to win each point, p1 wins {p1_result}% of games\n')
relationships = [rel for rel in zip(P1_CHANCES, p1_results)]
print(f'relationships:\n {relationships}')
@tngzng
Copy link
Author

tngzng commented Oct 20, 2023

output from local run shows a non-linear relationship b/w the odds of winning a point and the odds of winning a game, with the odds of winning a game increasing faster as the odds of winning a point increase until an inflection point when the chance to win each point is ~70%:

when p1 has a 50% chance to win each point, p1 wins 50.28% of games

when p1 has a 55% chance to win each point, p1 wins 62.5% of games

when p1 has a 60% chance to win each point, p1 wins 73.57000000000001% of games

when p1 has a 65% chance to win each point, p1 wins 83.44% of games

when p1 has a 70% chance to win each point, p1 wins 90.23% of games

when p1 has a 75% chance to win each point, p1 wins 95.0% of games

when p1 has a 80% chance to win each point, p1 wins 98.08% of games

when p1 has a 85% chance to win each point, p1 wins 99.33999999999999% of games

when p1 has a 90% chance to win each point, p1 wins 99.82% of games

@tngzng
Copy link
Author

tngzng commented Oct 20, 2023

plotted in colab:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment