This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
simulation.py
158 lines (124 loc) · 7.86 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import numpy as np
import pandas as pd
from tqdm import tqdm
from fifa_ratings_predictor.data_methods import read_fixtures_data, normalise_features
from fifa_ratings_predictor.model import NeuralNet
NUMBER_OF_SIMULATIONS2 = 1000
PREDICTED_LINEUPS2 = {'afc-bournemouth': np.array([80, 76, 75, 77, 78, 75, 0, 75, 73, 75, 0, 0, 0, 0, 77, 69, 0, 0]),
'arsenal': np.array([84, 81, 83, 73, 0, 0, 0, 68, 82, 84, 77, 87, 76, 0, 87, 0, 0, 0]),
'brighton-hove-albion': np.array(
[78, 75, 74, 76, 76, 0, 0, 72, 76, 77, 78, 78, 0, 0, 74, 0, 0, 0]),
'burnley': np.array([78, 78, 76, 79, 80, 0, 0, 78, 78, 74, 75, 77, 0, 0, 77, 0, 0, 0]),
'cardiff-city': np.array([72, 74, 75, 75, 71, 0, 0, 72, 74, 74, 0, 0, 0, 0, 72, 75, 75, 0]),
'chelsea': np.array([82, 79, 82, 82, 82, 86, 0, 85, 88, 0, 0, 0, 0, 0, 91, 84, 84, 0]),
'crystal-palace': np.array([73, 75, 74, 78, 72, 65, 0, 78, 78, 76, 0, 0, 0, 0, 79, 71, 0, 0]),
'everton': np.array([80, 81, 80, 79, 80, 0, 0, 74, 81, 80, 79, 77, 0, 0, 80, 0, 0, 0]),
'fulham': np.array([73, 73, 69, 71, 75, 0, 0, 74, 79, 75, 0, 0, 0, 0, 76, 79, 81, 0]),
'huddersfield-town': np.array([75, 73, 76, 75, 76, 0, 0, 71, 75, 73, 70, 0, 0, 0, 69, 76, 0, 0]),
'leicester-city': np.array([82, 75, 73, 79, 76, 0, 0, 80, 84, 81, 75, 0, 0, 0, 82, 77, 0, 0]),
'liverpool': np.array([80, 75, 79, 80, 84, 0, 0, 81, 81, 80, 0, 0, 0, 0, 85, 87, 84, 0]),
'manchester-city': np.array([85, 84, 85, 83, 70, 0, 0, 85, 91, 89, 0, 0, 0, 0, 84, 89, 85, 0]),
'manchester-united': np.array([91, 83, 79, 81, 78, 0, 0, 85, 82, 88, 81, 0, 0, 0, 86, 88, 0, 0]),
'newcastle-united': np.array([75, 76, 74, 78, 78, 0, 0, 78, 76, 74, 75, 0, 0, 0, 75, 75, 0, 0]),
'southampton': np.array([76, 78, 79, 71, 76, 0, 0, 77, 77, 76, 79, 76, 0, 0, 75, 0, 0, 0]),
'tottenham-hotspur': np.array([88, 81, 81, 82, 86, 0, 0, 81, 88, 84, 84, 84, 0, 0, 88, 0, 0, 0]),
'watford': np.array([80, 76, 77, 76, 74, 76, 0, 76, 80, 79, 0, 0, 0, 0, 77, 76, 0, 0]),
'west-ham-united': np.array([78, 76, 74, 75, 78, 0, 0, 76, 76, 81, 81, 0, 0, 0, 79, 81, 0, 0]),
'wolverhampton-wanderers': np.array(
[72, 74, 72, 74, 78, 74, 0, 77, 82, 0, 0, 0, 0, 0, 75, 82, 78, 0])}
class SeasonSimulator:
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
def __init__(self, match_fixtures, predicted_lineups, model_path, write_to_csv=False,
csv_filepath=None):
self.predicted_lineups = predicted_lineups
self.fixtures = match_fixtures
self.write_to_csv = write_to_csv
self.csv_filepath = csv_filepath
self.model_path = model_path
self.total_points = dict.fromkeys(self.predicted_lineups.keys(), 0)
self.wins = dict.fromkeys(self.predicted_lineups.keys(), 0)
self.draws = dict.fromkeys(self.predicted_lineups.keys(), 0)
self.losses = dict.fromkeys(self.predicted_lineups.keys(), 0)
self.league_wins = dict.fromkeys(self.predicted_lineups.keys(), 0)
self.relegation = dict.fromkeys(self.predicted_lineups.keys(), 0)
self.top_4 = dict.fromkeys(self.predicted_lineups.keys(), 0)
def get_match_probabilities(self, match_fixtures, verbose=False):
feature_vectors = []
net = NeuralNet()
for fixture in tqdm(match_fixtures, desc='Getting match probabilities...', disable=not verbose):
home_team, away_team = fixture['home team'], fixture['away team']
feature_vectors.append(np.hstack((self.predicted_lineups[home_team], self.predicted_lineups[
away_team])).reshape(
(1, 36)))
predictions = net.predict(np.vstack((x for x in feature_vectors)), model_name=self.model_path)
match_probabilities = [x for x in predictions]
return match_probabilities
def run_season(self, season_fixtures, match_results):
assert len(season_fixtures) == len(match_results), "Each fixture must have it's '1X2 probabilities"
league_points = dict.fromkeys(self.predicted_lineups.keys(), 0)
for fixture, result in zip(season_fixtures, match_results):
home_team, away_team = fixture['home team'], fixture['away team']
if result == '1':
self.total_points[home_team] += 3
league_points[home_team] += 3
self.wins[home_team] += 1
self.losses[away_team] += 1
elif result == 'X':
self.total_points[home_team] += 1
self.total_points[away_team] += 1
league_points[home_team] += 1
league_points[away_team] += 1
self.draws[home_team] += 1
self.draws[away_team] += 1
elif result == '2':
self.total_points[away_team] += 3
league_points[away_team] += 3
self.wins[away_team] += 1
self.losses[home_team] += 1
league_positions = sorted(league_points.items(), key=lambda x: x[1], reverse=True)
self.league_wins[league_positions[0][0]] += 1
for team, points in league_positions[:4]:
self.top_4[team] += 1
for team, points in league_positions[-3:]:
self.relegation[team] += 1
def normalise_season_values(self, number_of_simulations):
for k in self.total_points.keys():
self.total_points[k] = self.total_points[k] / number_of_simulations
self.wins[k] = self.wins[k] / number_of_simulations
self.draws[k] = self.draws[k] / number_of_simulations
self.losses[k] = self.losses[k] / number_of_simulations
self.league_wins[k] = self.league_wins[k] / number_of_simulations
self.top_4[k] = self.top_4[k] / number_of_simulations
self.relegation[k] = self.relegation[k] / number_of_simulations
def convert_to_pandas(self, write_to_csv=False):
df = pd.DataFrame(
[self.total_points, self.wins, self.draws, self.losses, self.league_wins, self.top_4, self.relegation],
index=['Points', 'Wins',
'Draws', 'Losses',
'1st Place',
'Top 4',
'Relegation']).T
if write_to_csv:
df.sort_values(by='Points', ascending=False).round(decimals=2).to_csv(
self.csv_filepath)
return df.sort_values(by='Points', ascending=False).round(decimals=2)
@staticmethod
def get_match_results_from_probabilities(match_probabilities, number_of_simulations):
return [np.random.choice(['1', 'X', '2'], size=number_of_simulations, p=match_probability) for
match_probability in match_probabilities]
def simulate_monte_carlo(self, number_of_simulations, verbose=False, normalise=True):
for k, v in self.predicted_lineups.items():
self.predicted_lineups[k] = normalise_features(v)
probabilities = self.get_match_probabilities(self.fixtures, verbose=verbose)
results = self.get_match_results_from_probabilities(probabilities, number_of_simulations)
for i in tqdm(range(number_of_simulations), desc='running_simulations', disable=not verbose):
self.run_season(self.fixtures, [x[i] for x in results])
if normalise:
self.normalise_season_values(number_of_simulations)
df = self.convert_to_pandas(write_to_csv=self.write_to_csv)
return df
if __name__ == '__main__':
fixtures2 = read_fixtures_data()
sim = SeasonSimulator(fixtures2, PREDICTED_LINEUPS2, model_path='./deep-models-all/deep')
print(sim.simulate_monte_carlo(1000))