-
Notifications
You must be signed in to change notification settings - Fork 1
/
probs_of_finishing_each_place.py
155 lines (114 loc) · 5.07 KB
/
probs_of_finishing_each_place.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
import epl
import predictions_tensorflow
import random
import numpy as np
def calculateProbsOfEachGameInASeason(csv_file_name, predict):
data, indexToTeam, teamToIndex, indexToGamesPlayed = epl.getData(csv_file_name)
game_data = []
for i in range(len(data["Home Team"])):
homeTeam = data["Home Team"][i]
awayTeam = data["Away Team"][i]
homeIndex = teamToIndex[homeTeam]
awayIndex = teamToIndex[awayTeam]
winnerProp, tieProb, loserProb = predict(homeIndex, awayIndex, i)
total = winnerProp + tieProb + loserProb
winnerProp = winnerProp / total
tieProb = tieProb / total
loserProb = loserProb / total
try:
result = data["Result"][i].split("-")
homeGoals = int(result[0])
awayGoals = int(result[1])
except:
homeGoals = None
awayGoals = None
game_data.append({
'Home Team': homeTeam,
'Away Team': awayTeam,
'Round Number': int(data['Round Number'][i]),
'Date': data['Date'][i],
'Location': data['Location'][i],
'home_win': float(winnerProp),
'tie': float(tieProb),
'away_win': float(loserProb),
'home_goals': homeGoals,
'away_goals': awayGoals,
})
return game_data
def calculateProbs(predictionCount, csv_file_name, predict):
data, indexToTeam, teamToIndex, indexToGamesPlayed = epl.getData(csv_file_name)
indexToPlaceFinishedToTimesFinished = [[0 for _ in range(len(indexToTeam))] for _ in range(len(indexToTeam))]
indexToPoints = [0 for _ in range(len(indexToTeam))]
indexToExpectedPoints = np.array([0.0 for _ in range(len(indexToTeam))])
gameIndexLeftToPlay = []
for i in range(len(data["Home Team"])):
homeTeam = data["Home Team"][i]
awayTeam = data["Away Team"][i]
homeIndex = teamToIndex[homeTeam]
awayIndex = teamToIndex[awayTeam]
if not(type(data["Result"][i]) is str):
gameIndexLeftToPlay.append(i)
winnerProp, tieProb, loserProb = predict(homeIndex, awayIndex, i)
total = winnerProp + tieProb + loserProb
winnerProp = winnerProp / total
tieProb = tieProb / total
loserProb = loserProb / total
indexToExpectedPoints[homeIndex] += winnerProp * 3 + tieProb
indexToExpectedPoints[awayIndex] += loserProb * 3 + tieProb
continue
result = data["Result"][i].split("-")
if len(result) != 2:
gameIndexLeftToPlay.append(i)
winnerProp, tieProb, loserProb = predict(homeIndex, awayIndex, i)
total = winnerProp + tieProb + loserProb
winnerProp = winnerProp / total
tieProb = tieProb / total
loserProb = loserProb / total
indexToExpectedPoints[homeIndex] += winnerProp * 3 + tieProb
indexToExpectedPoints[awayIndex] += loserProb * 3 + tieProb
continue
homeScore = int(result[0].strip())
awayScore = int(result[1].strip())
if homeScore > awayScore:
indexToPoints[homeIndex] += 3
indexToExpectedPoints[homeIndex] += 3
elif homeScore < awayScore:
indexToPoints[awayIndex] += 3
indexToExpectedPoints[awayIndex] += 3
else:
indexToPoints[homeIndex] += 1
indexToPoints[awayIndex] += 1
indexToExpectedPoints[homeIndex] += 1
indexToExpectedPoints[awayIndex] += 1
for rounds in range(predictionCount):
if rounds % 1000 == 0:
print(rounds, (rounds / predictionCount))
roundIndexToPoints = indexToPoints.copy()
for i in gameIndexLeftToPlay:
homeTeam = data["Home Team"][i]
awayTeam = data["Away Team"][i]
homeIndex = teamToIndex[homeTeam]
awayIndex = teamToIndex[awayTeam]
winnerProp, tieProb, loserProb = predict(homeIndex, awayIndex, i)
total = winnerProp + tieProb + loserProb
winnerProp = winnerProp / total
tieProb = tieProb / total
if random.random() <= winnerProp:
roundIndexToPoints[homeIndex] += 3
elif random.random() <= winnerProp + tieProb:
roundIndexToPoints[homeIndex] += 1
roundIndexToPoints[homeIndex] += 1
else:
roundIndexToPoints[awayIndex] += 3
rankings = [(roundIndexToPoints[i], i) for i in range(len(roundIndexToPoints))]
rankings.sort(reverse=True)
for i in range(len(rankings)):
indexToPlaceFinishedToTimesFinished[rankings[i][1]][i] += 1
return np.array(indexToPlaceFinishedToTimesFinished) / predictionCount, indexToExpectedPoints, indexToTeam
def main():
predictor = predictions_tensorflow.createPredictGameFunction('epl.csv')
indexToPlaceFinishedToTimesFinished, indexToTeam = calculateProbs(10**3, 'epl.csv', predictor)
print(indexToPlaceFinishedToTimesFinished)
print(indexToTeam)
if __name__ == '__main__':
main()