Skip to content

Note

Click here to download the full example code

Saving and loading a bettor

This example shows save_bettor and load_bettor.

# Author: Georgios Douzas <gdouzas@icloud.com>
# Licence: MIT

import tempfile
from pathlib import Path

import matplotlib.pyplot as plt
import pandas as pd

from sportsbet.dataloaders import DataLoader
from sportsbet.evaluation import OddsComparisonBettor, load_bettor, save_bettor
from sportsbet.sources import SampleSoccerOdds, SampleSoccerStats

A fitted bettor comes back fitted

The model you backtested is the model that should place the bets. Saving it makes them the same object. Otherwise you have two models that were fitted separately and are only hopefully the same.

dataloader = DataLoader(
    param_grid={'league': ['England']},
    stats=SampleSoccerStats(),
    odds=SampleSoccerOdds(),
)
X_train, Y_train, O_train = dataloader.extract_train_data(odds_type='market_average')

bettor = OddsComparisonBettor(alpha=0.03, betting_markets=['home_win', 'draw', 'away_win'])
_ = bettor.fit(X_train, Y_train, O_train)

path = str(Path(tempfile.mkdtemp()) / 'bettor.pkl')
save_bettor(bettor, path)

It is ready to bet without being fitted again.

reloaded = load_bettor(path)
reloaded.betting_markets_.tolist()

Out:

['home_win', 'draw', 'away_win']
reloaded.bet(X_train, O_train)

Out:

array([[False, False,  True],
       [ True, False, False],
       [False, False, False],
       ...,
       [False, False, False],
       [ True, False, False],
       [False, False,  True]], shape=(380, 3))

A picture of it

markets = reloaded.betting_markets_.tolist()
counts = pd.DataFrame(reloaded.bet(X_train, O_train), columns=markets).sum()

fig, ax = plt.subplots()
ax.bar(counts.index, counts.to_numpy())
ax.set_title('Value bets found, by market')
ax.set_ylabel('bets')

Value bets found, by market

Out:

Text(46.722222222222214, 0.5, 'bets')

Total running time of the script: ( 0 minutes 0.731 seconds)

Download Python source code: plot_save_load_bettor.py

Download Jupyter notebook: plot_save_load_bettor.ipynb

Gallery generated by mkdocs-gallery