ratingslib.application module

Module for the outcome of application

class Outcome(name: str, outcomes_list: list, columns_dict=None)

Bases: ABC

A base class for the outcome of application

get_outcomes() dict

Creates and returns a dictionary with outcomes. The keys are the possible outcomes that defined by the property of outcomes_list and the values of the dictionary are positive integers. For example for three possible outcomes then the values are 1,2,3.

abstract set_col_indices(pairs_data_df: DataFrame)

To be overridden in subclasses.

_abc_impl = <_abc_data object>
class SportOutcome(name: str, outcomes_list: list, columns_dict=None)

Bases: Outcome

An abstract class for Outcome. The term of outcome is related with application type. For example in sports like soccer for every matchup between two teams there are three possible final outcomes: Home Win, Away Win and Draw. In other applications like a backgammon game, there are two possible outcomes. Another example with two possible outcomes is the under 2.5 goals or over 2.5 goals in soccer. Under 2.5 means that the total number of goals scored during the match will be under or 2 and over means 3 or above. Also in soccer if the outcome is defined by the final score this means that there are many possible results e.g. (0-0, 0-1, 1-0, 1-1, etc.).

Extend this class and override abstract methods to define the application outcomes.

Parameters
  • name (str) – The name of outcome. In soccer possible outcomes names are ‘FT’ (from Full Time) or ‘FTR’ (from Full Time Result).

  • outcomes_list (list) – The list of outcome names.

  • columns_dict (dict, default=None) – A dictionary mapping the column names of the dataset. See the module ratingslib.datasets.parameters for more details.

outcomes_dict

Outcome names mapped to their values

Type

dict

outcomes_values_list

list of outcome values

Type

list

abstract outcome_value(row)

To be overridden in subclasses.

abstract fit_and_predict(train_X, train_Y, test_X, method: Literal['MLE', 'RANK'] = 'RANK') tuple

To be overridden in subclasses.

_abc_impl = <_abc_data object>
class SoccerOutcome(columns_dict=None)

Bases: SportOutcome

In soccer the final outcome during a matchup between two teams there are three possible outcomes:

  • H : Home Win (1)

  • A : Away Win (2)

  • D : Draw (3)

name

Name of outcome. In soccer possible outcome names can be set to ‘FT’ (from Full Time) or ‘FTR’ (from Full Time Result)

Type

str

columns_odds_dict
dictionary mapping the odd columns from dataset
  • dictionary keys:
    • H for home odds,

    • D for draw odds,

    • A for away odds

  • dictionary values:

    list of column names of betting odds from selected bookmakers

Type

dict

home_points_col_index

the index number of column for the goals of home team

Type

int

away_points_col_index

the index number of column for the goals of away team

Type

int

Notes

The odds of the final outcome are offered from the following bookmaker companies:

  • Bet365

  • Bet&Win

  • Interwetten

outcomes_list = ['H', 'A', 'D']
set_col_indices(pairs_data_df: DataFrame)

Set the indices for home and away goals

Parameters

pairs_data_df (pd.DataFrame) – the games data of teams

outcome_value(row) int

This method returns the outcome by given match. The decision is based on the goals scored by each team.

Returns

integer value of outcome (1 = ‘H’, 2 = ‘A’, 3 = ‘D’)

Return type

int

static _fit(train_X: ndarray, train_Y: ndarray, outcomes_dict: dict)

Find a, h parameters

static _fit_and_predict(train_X: ndarray, train_Y: ndarray, test_X: ndarray, outcomes_dict: dict, method: Literal['MLE', 'RANK'] = 'RANK')
fit_and_predict(train_X, train_Y, test_X, method: Literal['MLE', 'RANK'] = 'RANK')

Fit and predict the final outcome

Parameters
  • train_X (numpy.ndarray) – The training set that includes only the features

  • train_Y (numpy.ndarray) – The outcome labels of training set

  • test_X (numpy.ndarray) – The outcome labels of test set

  • method (Literal['MLE', 'RANK'], default='RANK') – Two available methods for predictions: ‘RANK’ or ‘MLE’

Returns

The predictions for the target outcome and the predictions distribution

Return type

tuple

static predict_from_ratings_mle(rating_home: float, rating_away: float, a: float, h: float, outcomes_dict: dict) Tuple[int, list]

The ratings can be turned into predictions by selecting the highest probability from outcomes. The probabilities can be computed by applying the modified logistic function proposed by 1

Parameters
  • rating_home (float) – the rating value of home-team

  • rating_away (float) – the rating value of away-team

  • a (float) – parameter in probability equation

  • h (float) – home advantage parameter

Returns

  • result (int) – The value of predicted result

  • rating_prob (list) – Prediction probabilities for each outcome

References

1

Lasek, J., Szlávik, Z., & Bhulai, S. (2013). The predictive power of ranking systems in association football. International Journal of Applied Pattern Recognition, 1, 27–46.

static predict_from_ratings_logic(rating_home: float, rating_away: float, outcomes_dict: dict, calc_prob: bool = True) Tuple[int, list]

Prediction of the final outcome in a game between two teams is based on their ratings. The logic of prediction is that a higher rating is preferred over the lower rating. For example in a match between teamA and teamB with ratingA and ratingB respectively,

  • if ratingA > ratingB then prediction of the winner is teamA

  • if ratingA < ratingB then prediction of the winner is teamB

  • if ratingA = ratingB then prediction is the Draw.

Parameters
  • rating_home (float) – the rating value of home-team

  • rating_away (float) – the rating value of away-team

  • calc_prob (bool, default=True) – If True calculate the probability of winning. Probabilities are calculated after ratings normalization.

Returns

  • result (int) – The value of predicted result

  • rating_prob (list) – Prediction probabilities for each outcome

Examples

_abc_impl = <_abc_data object>