Skip to main content

tennisim

PyPI version shields.io PyPI pyversions Actions Status Actions Status codecov

Simulate tennis points, games, sets and matches.

Small pure python package (no dependencies outside of standard lib) to simulate tennis using points-based modelling i.e. given a probability of a server winning a given point, simulate the outcome of:

  • points
  • games
  • sets
  • tiebreaks
  • matches

with the ability to alter various parameters to gain some intuition behind how the tennis scoring system impacts matches. Using this we can answer various questions like:

  • what effect does removing the second serve have on match duration?
  • if the probability of winning a point on serve is reflective of skill, then how do rule alterations affect the likelihood that the more skillful player will actually end up winning the match (not just a given point)?

Installing

pip install tennisim

Points-based Modelling

Points-based modelling is a popular model for modelling tennis matches where predictions for games, sets and matches depends on modelling every constituent point. This can lead to a wealth of data that can be used to generate in-play match odds as we can output distributions e.g. for a given starting point, if we simulate 1000 outcomes how many show that player 1 wins the next set?

For more background I wrote this article on Towards Data Science.

Example

Suppose we want to simulate a game of tennis. We define the probability that the server will win a given point:

from tennisim.sim import sim_game

p = 0.7
sim_game(p)

This will simulate 1 game of tennis where the probability that the server will win any given point is 0.7. It will return a tuple containing:

  • boolean result for if the server won the game
  • list of tuples for the score progression of the game

We can then take this further and simulate 1,000 groups of 100 games to generate a distribution of results. This can be interesting when looking at how changes in the probability p or the length of a game impacts the servers win probability for the game.

import numpy as np
from tennisim.sim import sim_game

# set params for simulation
games = 100
sims = 1000
probabs = np.linspace(0.5, 0.8, 4)
results = {}

# for each serve win probability
for p in probabs:
   # we now need to generate sims
   means = []
   game_lengths = []
   for i in range(0, sims):
       g_results = [sim_game(p) for x in range(games)]
       # get mean result
       mean_res = np.mean([x[0] for x in g_results])
       # get mean game length
       mean_length = np.mean([len(x[1]) for x in g_results])
       # join to results holders
       means.append(mean_res)
       game_lengths.append(mean_length)
   # add data to probab dict
   results[p] = (means, game_lengths)

Help

For more info see the documentation

License

tennisim is free and open source software, distributed under the terms of the MIT license.

Metadata

Release files for tennisim 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tennisim 0.1.3
File Size Uploaded
tennisim-0.1.3.tar.gz 15.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tennisim 0.1.3
File Interpreter ABI Platform
tennisim-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 31.8 kB

Release files / tennisim-0.1.3.tar.gz

Download URL tennisim-0.1.3.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7e6d8ead04cd75af67af2a44f08431f28603dd0460aed69ec7a4db2a899d0d19
BLAKE2b-256 checksum
How to use checksums
ccfe1c674cfbe1ad416a3f046804814dc0b6bc2141a88f64c6884f9a455fbffe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release files / tennisim-0.1.3-py3-none-any.whl

Download URL tennisim-0.1.3-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
133d3a4e13618b5e62465706b97d8317f66dd4ed5dd39fbd23962f5f32f4bb65
BLAKE2b-256 checksum
How to use checksums
11bc3db75d9ec95eb899d2c2c1821eb76a3056eadd9854746b8e9945d8543408
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page