项目作者: georgedouzas

项目描述 :
Collection of sports betting AI tools.
高级语言: Python
项目地址: git://github.com/georgedouzas/sports-betting.git
创建时间: 2019-01-08T00:52:28Z
项目社区:https://github.com/georgedouzas/sports-betting

开源协议:MIT License

下载


[scikit-learn]: http://scikit-learn.org/stable

sports-betting

ci doc

Category Tools
Development black ruff mypy docformatter
Package version pythonversion downloads
Documentation mkdocs
Communication gitter discussions

Introduction

The sports-betting package is a handy set of tools for creating, testing, and using sports betting models. It comes
with a Python API, a CLI, and even a GUI built with Reflex to keep things simple:


The main components of sports-betting are dataloaders and bettors objects:

  • Dataloaders download and prepare data suitable for predictive modelling.
  • Bettors provide an easy way to backtest betting strategies and predict the value bets of future events.

Quick start

GUI

sports-betting comes with a GUI that provides a intuitive way to interact with the library. It supports the following
functionalitites:

  • Easily upload, create, or update dataloaders to handle historical and fixtures data.
  • Develop and test betting models with tools for backtesting and identifying value bets.

To launch the GUI, simply run the command sportsbet-gui. Once started, you’ll see the initial screen:


Explore the functionality with guidance from the built-in bot, which streams helpful messages along the way.

API

The sports-betting package makes it easy to download sports betting data:

  1. from sportsbet.datasets import SoccerDataLoader
  2. dataloader = SoccerDataLoader(param_grid={'league': ['Italy'], 'year': [2020]})
  3. X_train, Y_train, O_train = dataloader.extract_train_data(odds_type='market_maximum')
  4. X_fix, Y_fix, O_fix = dataloader.extract_fixtures_data()

X_train are the historical/training data and X_fix are the test/fixtures data. The historical data can be used to backtest the
performance of a bettor model:

  1. from sportsbet.evaluation import ClassifierBettor, backtest
  2. from sklearn.dummy import DummyClassifier
  3. bettor = ClassifierBettor(DummyClassifier())
  4. backtest(bettor, X_train, Y_train, O_train)

We can use the trained bettor model to predict the value bets using the fixtures data:

  1. bettor.fit(X_train, Y_train)
  2. bettor.bet(X_fix, O_fix)

Sports betting in practice

You can think of any sports betting event as a random experiment with unknown probabilities for the various outcomes. Even for the
most unlikely outcome, for example scoring more than 10 goals in a soccer match, a small probability is still assigned. The
bookmaker estimates this probability P and offers the corresponding odds O. In theory, if the bookmaker offers the so-called fair
odds O = 1 / P in the long run, neither the bettor nor the bookmaker would make any money.

The bookmaker’s strategy is to adjust the odds in their favor using the over-round of probabilities. In practice, it offers odds
less than the estimated fair odds. The important point here is that the bookmaker still has to estimate the probabilities of
outcomes and provide odds that guarantee them long-term profit.

On the other hand, the bettor can also estimate the probabilities and compare them to the odds the bookmaker offers. If the
estimated probability of an outcome is higher than the implied probability from the provided odds, then the bet is called a value
bet.

The only long-term betting strategy that makes sense is to select value bets. However, you have to remember that neither the
bettor nor the bookmaker can access the actual probabilities of outcomes. Therefore, identifying a value bet from the side of the
bettor is still an estimation. The bettor or the bookmaker might be wrong, or both of them.

Another essential point is that bookmakers can access resources that the typical bettor is rare to access. For instance, they have
more data, computational power, and teams of experts working on predictive models. You may assume that trying to beat them is
pointless, but this is not necessarily correct. The bookmakers have multiple factors to consider when they offer their adjusted
odds. This is the reason there is a considerable variation among the offered odds. The bettor should aim to systematically
estimate the value bets, backtest their performance, and not create arbitrarily accurate predictive models. This is a realistic
goal, and sports-betting can help by providing appropriate tools.

Installation

For user installation, sports-betting is currently available on the PyPi’s repository, and you can install it via pip:

  1. pip install sports-betting

If you have Node.js version v22.0.0 or higher, you can optionally install the GUI:

  1. pip install sports-betting[gui]

Development installation requires to clone the repository and then use PDM to install the
project as well as the main and development dependencies:

  1. git clone https://github.com/georgedouzas/sports-betting.git
  2. cd sports-betting
  3. pdm install

Usage

You can access sports-betting through the GUI application, the Python API, or the CLI. However, it’s a good idea to
get familiar with the Python API since you’ll need it to create configuration files for the CLI or load custom betting
models into the GUI. sports-betting supports all common sports betting needs i.e. fetching historical and fixtures
data as well as backtesting of betting strategies and prediction of value bets.

GUI

Launch the GUI app with the command sportsbet-gui.

Here are a few things you can do with the GUI:

  • Configure the dataloader:


  • Create a new betting model:


  • Run the model to get either backtesting results or value bets:


API

Assume we would like to backtest the following scenario and use the bettor object
to predict value bets:

  • Selection of data
    • First and second division of German, Italian and French leagues for the years 2021-2024
    • Maximum odds of the market in order to backtest our betting strategy
  • Configuration of betting strategy
    • 5-fold time ordered cross-validation
    • Initial cash of 10000 euros
    • Stake of 50 euros for each bet
    • Use match odds (home win, away win and draw) as betting markets
    • Logistic regression classifier to predict probabilities and value bets
  1. # Selection of data
  2. from sportsbet.datasets import SoccerDataLoader
  3. leagues = ['Germany', 'Italy', 'France']
  4. divisions = [1, 2]
  5. years = [2021, 2022, 2023, 2024]
  6. odds_type = 'market_maximum'
  7. dataloader = SoccerDataLoader({'league': leagues, 'year': years, 'division': divisions})
  8. X_train, Y_train, O_train = dataloader.extract_train_data(odds_type=odds_type)
  9. X_fix, _, O_fix = dataloader.extract_fixtures_data()
  10. # Configuration of betting strategy
  11. from sklearn.model_selection import TimeSeriesSplit
  12. from sklearn.compose import make_column_transformer
  13. from sklearn.linear_model import LogisticRegression
  14. from sklearn.impute import SimpleImputer
  15. from sklearn.pipeline import make_pipeline
  16. from sklearn.preprocessing import OneHotEncoder
  17. from sklearn.multioutput import MultiOutputClassifier
  18. from sportsbet.evaluation import ClassifierBettor, backtest
  19. tscv = TimeSeriesSplit(5)
  20. init_cash = 10000.0
  21. stake = 50.0
  22. betting_markets = ['home_win__full_time_goals', 'draw__full_time_goals', 'away_win__full_time_goals']
  23. classifier = make_pipeline(
  24. make_column_transformer(
  25. (OneHotEncoder(handle_unknown='ignore'), ['league', 'home_team', 'away_team']), remainder='passthrough'
  26. ),
  27. SimpleImputer(),
  28. MultiOutputClassifier(LogisticRegression(solver='liblinear', random_state=7, class_weight='balanced', C=50)),
  29. )
  30. bettor = ClassifierBettor(classifier, betting_markets=betting_markets, stake=stake, init_cash=init_cash)
  31. # Apply backtesting and get results
  32. backtesting_results = backtest(bettor, X_train, Y_train, O_train, cv=tscv)
  33. # Get value bets for upcoming betting events
  34. bettor.fit(X_train, Y_train)
  35. bettor.bet(X_fix, O_fix)

CLI

The command sportsbet provides various sub-commands to download data and predict the value bets. For any sub-command you may
add the --help flag to get more information about its usage.

Configuration

In order to use the commands, a configuration file is required. You can find examples of such configuration files in
sports-betting/configs/. The configuration file should have a Python file extension and contain a few variables. The variables
DATALOADER_CLASS and PARAM_GRID are mandatory while the rest are optional.

The following variables configure the data extraction:

  • DATALOADER_CLASS: The dataloader class to use.

  • PARAM_GRID: The parameters grid to select the type of information that the data includes.

  • DROP_NA_THRES: The parameter drop_na_thres of the dataloader’s extract_train_data.

  • ODDS_TYPE: The parameter odds_type of the dataloader’s extract_train_data.

The following variables configure the betting process:

  • BETTOR: A bettor object.

  • CV: The parameter cv of the function backtest.

  • N_JOBS: The parameter n_jobs of the function backtest.

  • VERBOSE: The parameter verbose of the function backtest.

Commands

Once these variables are provided, we can select the appropriate commands to select any of the sports-betting‘s functionalities.

Dataloader

Show available parameters for dataloaders:

  1. sportsbet dataloader params -c config.py

Show available odds types:

  1. sportsbet dataloader odds-types -c config.py

Extract training data and save them as CSV files:

  1. sportsbet dataloader training -c config.py -d /path/to/directory

Extract fixtures data and save them as CSV files:

  1. sportsbet dataloader fixtures -c config.py -d /path/to/directory
Bettor

Backtest the bettor and save the results as CSV file:

  1. sportsbet bettor backtest -c config.py -d /path/to/directory

Get the value bets and save them as CSV file:

  1. sportsbet bettor bet -c config.py -d /path/to/directory