Foresight and Hindsight Predictions

An example of code is provided to illustrate how to turn ratings into predictions of the winner of soccer outcome.

Teams ratings are produced by the following methods:

  • WinLoss

  • Colley

  • Massey

  • Elo (win version)

  • Elo (points version)

  • Keener

  • OffenseDefense

  • AccuRATE

  • GeM

Two type of predictions for the final oucome are made:

  • hindsight - predicting past matches using the ratings of teams (the first 20 matches of EPL 2018-2019)

  • foresight - predicting upcoming matches (the 3rd match week of EPL 2018-2019) using the ratings of teams for previous weeks (the first 20 matches of EPL 2018-2019)

Data (hindsight)

Soccer Data: The first 20 games of EPL (2018-2019 season)

Data (foresight)

Soccer Data: The 3rd match-week of EPL (2018-2019 season)

Python code

 1"""Soccer hindsight and foresight predictions"""
 2
 3import ratingslib.ratings as rl
 4import pandas as pd
 5from ratingslib.app_sports.methods import (Predictions, predict_hindsight,
 6                                           prepare_sport_dataset,
 7                                           show_list_of_accuracy_results)
 8from ratingslib.application import SoccerOutcome
 9from ratingslib.datasets.filenames import (FILENAME_EPL_2018_2019_3RD_WEEK,
10                                           FILENAME_EPL_2018_2019_20_GAMES,
11                                           datasets_paths)
12from ratingslib.datasets.parameters import (COLUMNS_DICT, DATE_COL, WEEK_PERIOD,
13                                            stats)
14from ratingslib.datasets.parse import parse_pairs_data
15from ratingslib.ratings.methods import rating_systems_to_dict
16from ratingslib.utils.enums import ratings
17from ratingslib.utils.methods import print_info
18
19# rating systems
20ratings_list = [
21    rl.Winloss(normalization=False),
22    rl.Colley(),
23    rl.Massey(data_limit=10),
24    rl.Elo(version=ratings.ELOWIN, K=40, ks=400, HA=0,
25           starting_point=0),
26    rl.Elo(version=ratings.ELOPOINT, K=40, ks=400, HA=0,
27           starting_point=0),
28    rl.Keener(normalization=False),
29    rl.OffenseDefense(tol=0.0001),
30    rl.Markov(b=0.85, stats_markov_dict=stats.STATS_MARKOV_EQUAL_DICT),
31    rl.AccuRate()
32]
33# convert list to dictionary
34ratings_dict = rating_systems_to_dict(ratings_list)
35# get train and test filenames
36filename, filename_test = datasets_paths(
37    FILENAME_EPL_2018_2019_20_GAMES, FILENAME_EPL_2018_2019_3RD_WEEK)
38
39# set columns dictionry and outcome
40columns_dict = COLUMNS_DICT
41outcome = SoccerOutcome()
42
43# prediction methods
44pred_methods_list = ['MLE', 'RANK']
45
46# parse train and test data
47data_train, teams_df = parse_pairs_data(filename,
48                                        parse_dates=[DATE_COL],
49                                        frequency=WEEK_PERIOD,
50                                        outcome=outcome,
51                                        columns_dict=columns_dict)
52
53# ==========================================================================
54# HINDSIGHT PREDICTIONS
55# ==========================================================================
56
57print_info("HINDSIGHT RESULTS")
58for pm in pred_methods_list:
59    pred_list = []
60    test_Y_list = []
61    for rs in ratings_list:
62        pred, test_y = predict_hindsight(
63            data_train, rs.rate_from_file(filename), outcome, pm)
64        test_Y_list.append(test_y)
65        pred_list.append(pred)
66    print_info(pm)
67    show_list_of_accuracy_results(
68        list(ratings_dict.keys()), test_Y_list, pred_list, print_predictions=True)
69
70# ==========================================================================
71# FORESIGHT PREDICTIONS
72# ==========================================================================
73
74# the test file start from the 3rd week (start_period = 3)
75data_test, teams_df = parse_pairs_data(filename_test,
76                                       parse_dates=[DATE_COL],
77                                       frequency=WEEK_PERIOD,
78                                       outcome=outcome,
79                                       start_period_from=3,
80                                       columns_dict=columns_dict)
81
82
83# concat 2 files
84data_all = pd.concat([data_train, data_test]).reset_index(drop=True)
85# prepare the dataset
86data = prepare_sport_dataset(data_season=data_all,
87                             teams_df=teams_df,
88                             rating_systems=ratings_dict,
89                             start_week=2,
90                             columns_dict=columns_dict)
91
92print_info("FORESIGHT RESULTS")
93# make foresight predictions for the 3rd week (start_from_week=3)
94results = Predictions(data, outcome, start_from_week=3,
95                      print_accuracy_report=True,
96                      print_predictions=True,
97                      print_classification_report=False).rs_pred_parallel(
98    rating_systems=ratings_dict,
99    pred_methods_list=pred_methods_list)

Results

Below are shown the results of hindsight and foresight predictions:

=====HINDSIGHT RESULTS=====


=====MLE=====


=====Accuracy results=====

                              Accuracy  Correct Games  Wrong Games  Total Games                                                                                           Predictions
Winloss[normalization=False]      0.85             17            3           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 1/3, 1/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]
Colley                            0.85             17            3           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 1/3, 1/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]
Massey[data_limit=10]             0.85             17            3           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 2/3, 1/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]
EloWin[HA=0_K=40_ks=400]          0.85             17            3           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 1/3, 1/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]
EloPoint[HA=0_K=40_ks=400]        0.85             17            3           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 1/3, 1/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]
Keener[normalization=False]       0.85             17            3           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 1/3, 1/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]
OffenseDefense[tol=0.0001]        0.55             11            9           20  [1/1, 1/1, 1/2, 1/2, 1/2, 1/1, 1/3, 1/2, 1/1, 1/3, 1/3, 1/1, 1/1, 1/1, 1/1, 1/2, 1/1, 1/2, 1/1, 2/2]
Markov[b=0.85]                    0.60             12            8           20  [1/1, 1/1, 1/2, 2/2, 2/2, 1/1, 2/3, 1/2, 1/1, 2/3, 2/3, 2/1, 1/1, 1/1, 1/1, 1/2, 2/1, 2/2, 1/1, 2/2]
AccuRATE                          0.85             17            3           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 1/3, 1/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]

*** Predictions columns notation: (Predicted / Actual), 1 = Home Win, 2 = Away Win, 3 = Draw


=====RANK=====


=====Accuracy results=====

                              Accuracy  Correct Games  Wrong Games  Total Games                                                                                           Predictions
Winloss[normalization=False]      0.85             17            3           20  [3/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 3/3, 3/3, 1/1, 1/1, 1/1, 1/1, 2/2, 3/1, 2/2, 1/1, 2/2]
Colley                            0.85             17            3           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 2/3, 1/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]
Massey[data_limit=10]             0.85             17            3           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 2/3, 2/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]
EloWin[HA=0_K=40_ks=400]          0.85             17            3           20  [2/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 2/3, 3/3, 1/1, 1/1, 1/1, 1/1, 2/2, 1/1, 2/2, 1/1, 2/2]
EloPoint[HA=0_K=40_ks=400]        0.75             15            5           20  [2/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 1/3, 2/3, 1/1, 1/1, 1/1, 1/1, 2/2, 2/1, 2/2, 1/1, 2/2]
Keener[normalization=False]       0.75             15            5           20  [2/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 1/3, 2/3, 1/1, 1/1, 1/1, 1/1, 2/2, 2/1, 2/2, 1/1, 2/2]
OffenseDefense[tol=0.0001]        0.70             14            6           20  [2/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 2/3, 1/3, 1/1, 2/1, 1/1, 1/1, 2/2, 2/1, 2/2, 1/1, 2/2]
Markov[b=0.85]                    0.60             12            8           20  [1/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 1/2, 1/1, 2/3, 2/3, 2/1, 1/1, 2/1, 1/1, 1/2, 2/1, 2/2, 1/1, 2/2]
AccuRATE                          0.75             15            5           20  [2/1, 1/1, 2/2, 2/2, 2/2, 1/1, 2/3, 2/2, 1/1, 1/3, 2/3, 1/1, 1/1, 1/1, 1/1, 2/2, 2/1, 2/2, 1/1, 2/2]

*** Predictions columns notation: (Predicted / Actual), 1 = Home Win, 2 = Away Win, 3 = Draw
50.0%100.0%


=====FORESIGHT RESULTS=====


=====MLE=====


=====Accuracy results=====

                           Accuracy Correct Games Wrong Games  Total Games                                         Predictions
Winloss[normalization=False]      0.7             7           3           10  [1/1, 1/3, 1/3, 1/1, 2/2, 2/3, 1/1, 2/2, 1/1, 2/2]
Colley                            0.5             5           5           10  [1/1, 1/3, 1/3, 1/1, 1/2, 2/3, 1/1, 2/2, 1/1, 1/2]
Massey[data_limit=10]              NA            NA          NA            0                                                  NA
EloWin[HA=0_K=40_ks=400]          0.5             5           5           10  [1/1, 1/3, 1/3, 1/1, 1/2, 2/3, 1/1, 2/2, 1/1, 1/2]
EloPoint[HA=0_K=40_ks=400]        0.5             5           5           10  [1/1, 1/3, 1/3, 1/1, 1/2, 2/3, 1/1, 2/2, 1/1, 1/2]
Keener[normalization=False]       0.5             5           5           10  [1/1, 1/3, 1/3, 1/1, 1/2, 2/3, 1/1, 2/2, 1/1, 1/2]
OffenseDefense[tol=0.0001]        0.4             4           6           10  [1/1, 1/3, 1/3, 1/1, 1/2, 1/3, 1/1, 1/2, 1/1, 1/2]
Markov[b=0.85]                    0.4             4           6           10  [1/1, 1/3, 1/3, 1/1, 1/2, 1/3, 1/1, 1/2, 1/1, 1/2]
AccuRATE                          0.4             4           6           10  [1/1, 1/3, 2/3, 1/1, 1/2, 2/3, 1/1, 1/2, 1/1, 1/2]

*** Predictions columns notation: (Predicted / Actual), 1 = Home Win, 2 = Away Win, 3 = Draw


=====RANK=====


=====Accuracy results=====

                              Accuracy  Correct Games  Wrong Games  Total Games                                         Predictions
Winloss[normalization=False]       0.6              6            4           10  [3/1, 1/3, 3/3, 1/1, 2/2, 2/3, 3/1, 2/2, 1/1, 2/2]
Colley                             0.5              5            5           10  [2/1, 1/3, 2/3, 1/1, 2/2, 2/3, 2/1, 2/2, 1/1, 2/2]
Massey[data_limit=10]              0.4              4            6           10  [2/1, 1/3, 2/3, 1/1, 2/2, 2/3, 1/1, 1/2, 2/1, 2/2]
EloWin[HA=0_K=40_ks=400]           0.5              5            5           10  [3/1, 1/3, 2/3, 1/1, 2/2, 2/3, 2/1, 2/2, 1/1, 2/2]
EloPoint[HA=0_K=40_ks=400]         0.6              6            4           10  [1/1, 1/3, 2/3, 1/1, 2/2, 2/3, 2/1, 2/2, 1/1, 2/2]
Keener[normalization=False]        0.6              6            4           10  [1/1, 1/3, 2/3, 1/1, 2/2, 2/3, 2/1, 2/2, 1/1, 2/2]
OffenseDefense[tol=0.0001]         0.4              4            6           10  [2/1, 1/3, 1/3, 1/1, 2/2, 2/3, 2/1, 2/2, 1/1, 1/2]
Markov[b=0.85]                     0.5              5            5           10  [1/1, 2/3, 1/3, 1/1, 2/2, 1/3, 2/1, 2/2, 1/1, 1/2]
AccuRATE                           0.6              6            4           10  [1/1, 1/3, 2/3, 1/1, 2/2, 2/3, 2/1, 2/2, 1/1, 2/2]

*** Predictions columns notation: (Predicted / Actual), 1 = Home Win, 2 = Away Win, 3 = Draw