footix.models package
Submodules
footix.models.basic_poisson module
- class footix.models.basic_poisson.PoissonModel(n_teams, n_goals)[source]
Bases:
object- fit(X_train)[source]
Fit the Poisson model to the training data.
This method trains the model by estimating the parameters (gamma, alphas, betas) that maximize the likelihood of the observed goals in the training data. It performs optimization using scipy’s minimize function with constraints on the sum of alpha and beta parameters.
- Parameters:
X_train (DataFrame) – DataFrame containing match data with columns: - home_team: Name of the home team - away_team: Name of the away team - ftr: Full-time result (unused in this implementation) - fthg: Full-time home goals - ftag: Full-time away goals
- Returns:
gamma: Intercept parameter
alphas: Home team strength parameters
betas: Away team strength parameters
dict_teams: Mapping of team names to indices
- Return type:
None. The method updates the instance attributes
- predict(home_team, away_team)[source]
Predict the goal probability matrix for a match between two teams.
Uses the trained model parameters to calculate the expected number of goals for both teams, then computes the probability distribution of goals for each team using the Poisson distribution.
- Parameters:
- Returns:
GoalMatrix containing the probability distributions for home and away team goals.
- Raises:
ValueError – If either team is not in the list of trained teams.
- Return type:
- goal_expectation(home_team_id, away_team_id)[source]
Calculate expected goals for home and away teams.
The expected number of goals for the home team (lamb) and away team (mu) are calculated using the model parameters. The home team’s expectation includes the intercept term gamma, while the away team’s expectation does not.
footix.models.bayes_xg module
footix.models.bayesian module
- class footix.models.bayesian.BayesianModel(n_goals, n_teams=None, calibrate=False, use_stats=False)[source]
Bases:
objectBayesian hierarchical model for football scores using a Negative Binomial likelihood.
Attributes
- n_teamsint
Number of distinct teams in the league.
- n_goalsint
Maximum number of goals considered when computing score probabilities.
- tracearviz.InferenceData | None
Posterior samples after calling fit. None until the model is fitted.
- get_samples(home_team, away_team, **kwargs)[source]
Generates posterior predictive samples for the specified home and away teams based on the model.
home_team (str): The name of the home team. away_team (str): The name of the away team.
- tuple[np.ndarray, np.ndarray]:
- A tuple containing two one-dimensional numpy arrays:
The first array represents the sampled lambda values for the home team.
The second array represents the sampled lambda values for the away team.
Notes
This function transforms the team names into their corresponding indices, retrieves the posterior samples for model parameters from the trace, computes the expected goal rates (lambda values) for both teams, and flattens the arrays to provide a simplified output.
- Parameters:
- Return type:
footix.models.dixon_coles module
footix.models.elo module
- class footix.models.elo.EloDavidson(n_teams, k0, lambd, sigma, agnostic_probs, **kwargs)[source]
Bases:
object- Parameters:
n_teams (int)
k0 (int)
lambd (float)
sigma (int)
agnostic_probs (ProbaResult)
- fit(X_train)[source]
- Parameters:
X_train (DataFrame | EloDataReader)
- static check_probas(agnostic_probs)[source]
- Parameters:
agnostic_probs (ProbaResult)
- Return type:
None
footix.models.score_matrix module
- class footix.models.score_matrix.GoalMatrix(home_goals_probs, away_goals_probs, correlation_matrix=None)[source]
Bases:
objectUtilities for match score probability matrices.
GoalMatrix builds a joint distribution over football scores (home_goals, away_goals) from two marginal probability vectors. Optionally, a non-negative correlation/weight matrix can be applied element-wise to reweight scorelines.
Notes
The input probability vectors are validated and normalized to sum to 1.
- Parameters:
home_goals_probs (Sequence[float] | ndarray[tuple[Any, ...], dtype[floating]]) – 1D array-like of non-negative goal probabilities for the home team.
away_goals_probs (Sequence[float] | ndarray[tuple[Any, ...], dtype[floating]]) – 1D array-like of non-negative goal probabilities for the away team.
correlation_matrix (ndarray | None) – Optional 2D non-negative array of shape (n, n) applied element-wise to the outer product.
- Raises:
ValueError – If inputs are not 1D, contain NaN/Inf, contain negative values, have incompatible lengths, have zero total probability mass, or if the correlation matrix is invalid.
- return_probas()[source]
Return results probabilities in this order: home_win, draw, away_win.
- Returns:
NamedTuple of probabilities
- Return type:
- Raises:
ValueError – If the internal probability matrix has zero mass.
- visualize(n_goals=5)[source]
Visualize the goal matrix.
- Parameters:
n_goals (int) – Number of goals to visualize in the matrix. Defaults to 5.
- Returns:
The generated figure.
- Return type:
matplotlib.figure.Figure
- asian_handicap_results(handicap)[source]
Calculate the probabilities for a home win, draw, and away win after applying an Asian handicap using vectorized operations. The handicap is added to the home team’s goal count.
- Parameters:
handicap (float) – The handicap to be applied to the home team’s score.
- Returns:
home_win, draw, away_win probabilities.
- Return type:
- get_probable_score()[source]
Return the most probable score (home_goals, away_goals) based on the matrix_array.
Returns
- tuple of int
The (home_goals, away_goals) corresponding to the highest probability in matrix_array.
Examples
>>> gm = GoalMatrix(home_goals_probs, away_goals_probs) >>> gm.get_probable_score() (2, 1)
footix.models.team_elo module
footix.models.utils module
- footix.models.utils.compute_goals_home_vectors(data, /, map_teams, nbr_team)[source]
Compute vectors representing home team goals.
- Parameters:
- Returns:
- A tuple containing two NumPy arrays:
goals_vector representing home team goals and tau_home representing binary vectors for each home team.
- Return type:
tuple[np.ndarray, np.ndarray]
- footix.models.utils.compute_goals_away_vectors(data, /, map_teams, nbr_team)[source]
Compute vectors representing away team goals.
- Parameters:
- Returns:
- A tuple containing two NumPy arrays:
goals_vector representing away team goals and tau_away representing binary vectors for each away team.
- Return type:
tuple[np.ndarray, np.ndarray]
- footix.models.utils.to_torch_tensor(*arrays, dtype=torch.float32)[source]
Convert numpy arrays to torch tensors.
- Parameters:
*arrays (ndarray) – Variable number of numpy arrays to convert
dtype (dtype) – Target tensor dtype (default: torch.float32)
- Returns:
Single tensor if one array is provided, tuple of tensors if multiple arrays
- Return type:
Tensor | Tuple[Tensor, …]
Examples
>>> x = np.array([1, 2, 3]) >>> tensor_x = to_tensor(x)
>>> x = np.array([1, 2, 3]) >>> y = np.array([4, 5, 6]) >>> tensor_x, tensor_y = to_tensor(x, y)
- footix.models.utils.poisson_proba(lambda_param, k)[source]
Calculate the probability of achieving up to k goals given a lambda parameter.
- Parameters:
- Returns:
An array containing the probabilities of achieving each possible
- Return type:
np.ndarray
number of goals from 0 to n_goals, inclusive.
- footix.models.utils.implicit_intensities(proba_from_odds, max_iter=200, tol=1e-10)[source]
Calculate implicit scoring intensities from match outcome probabilities.
This function converts betting odds probabilities into implied goal-scoring intensities (lambda parameters) for both teams using numerical optimization. It uses the Skellam distribution to model the difference between two Poisson processes (goal scoring by each team).
- Parameters:
proba_from_odds (np.ndarray) – Array of shape (n_matches, 3) containing probabilities for [win, draw, loss] derived from betting odds.
max_iter (int, optional) – Maximum number of iterations for the optimization algorithm. Defaults to 200.
tol (float, optional) – Tolerance for optimization convergence. Defaults to 1e-10.
- Raises:
ValueError – If proba_from_odds does not have shape (n_matches, 3).
- Returns:
- Array of shape (n_matches, 2) containing the implied scoring
intensities [lambda1, lambda2] for each match, where lambda1 is the home team’s scoring intensity and lambda2 is the away team’s.
- Return type:
np.ndarray
Note
If the primary optimization fails, the function falls back to a grid search over predefined lambda values to find the best approximation.
- footix.models.utils.implied_poisson_goals(bookmaker_proba, *, k_sum=40, nbr_goals=10)[source]
Calculate implied Poisson goal distributions from bookmaker probabilities.
This function uses a system of equations to find the Poisson parameters (lambda) that best match the observed probabilities from bookmakers. It solves for the scoring rates of both teams using modified Bessel functions of the first kind.
- Parameters:
bookmaker_proba (ProbaResult) – Probabilities from bookmaker (draw, home win, away win)
k_sum (int) – Maximum number of goals to consider in summation (default: 40)
nbr_goals (int) – Number of goals to generate probabilities for (default: 10)
- Returns:
GoalMatrix containing probability distributions for home and away goals
- Raises:
ArithmeticError – If the numerical solver fails to converge
- Return type:
Module contents
Predictive models for football match outcomes.
This module contains various statistical and machine learning models for predicting football match results and deriving implied odds.
- Available models:
basic_poisson: Basic Poisson regression model
bayesian: Bayesian hierarchical model
elo: Elo rating system implementation
score_matrix: Score prediction matrix utilities
team_elo: Team-specific Elo support class used by the Elo model