-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulateOdds.py
154 lines (125 loc) · 3.91 KB
/
simulateOdds.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
#!/usr/bin/python
import http.client
import urllib.parse
import re
import random
import math
from loadPicks import Group
oddsTable = {}
resultsTable = {}
# Method to get the odds of team1 beating team2 from dolphinsim.com
def getOdds(team1, team2, loc=0) :
params = urllib.parse.urlencode({'dir' : 'ncaa_mbb', 'tn1' : team1, 'loc' : loc, 'tn2' : team2, 'submit':'Calculate'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain", 'Referer':'http://dolphinsim.com/ratings/nhl/'}
conn = http.client.HTTPConnection("dolphinsim.com")
conn.request("POST", "/cgi-bin/del/ratings/wpredict", params, headers)
response = conn.getresponse()
lines = response.readlines()
conn.close()
for line in lines :
l = line.decode('utf-8')
if "WIN ODDS" in l :
#print(l)
odd= re.search('WIN ODDS = (.*)\%', l, re.IGNORECASE).group(1)
break
chance = float(odd)/100.0
return chance
# Get the teams in bracket order
def loadBracket(fileName = "bracket.csv") :
games = []
team1 = None
team2 = None
try :
f = open(fileName, 'r')
for line in f:
line = line.strip()
if team1 == None :
team1 = line
else :
team2 = line
games.append((team1, team2))
team1 = None
team2 = None
finally :
f.close()
return(games)
def playRound(games) :
winners = []
for aGame in games :
team1 = aGame[0]
team2 = aGame[1]
gameKey = "%s-%s"%(team1, team2)
if (gameKey in oddsTable) :
odds = oddsTable[gameKey]
else :
odds = getOdds(team1, team2)
oddsTable[gameKey] = odds
#print("%s beats %s: %5.3f" % (team1, team2, odds))
if random.random() < odds :
winners.append(team1)
else :
winners.append(team2)
return winners
def makeGames(winners) :
games = []
team1 = None
team2 = None
for t in winners :
if team1 is None :
team1 = t
else :
team2 = t
games.append((team1, team2))
team1 = None
team2 = None
return games
def oneSimulation(games, callback=None) :
index = 0
while len(games) >= 1 :
winners = playRound(games)
if callable(callback) :
callback(winners, index)
for w in winners :
resultsTable[w][index] += 1
#print(winners)
games = []
if len(winners) > 1 :
games = makeGames(winners)
index += 1
#print(resultsTable)
return winners[0]
def setUpResults(games) :
numGames = len(games)
numRounds = int(math.log(numGames)/math.log(2) + 1)
for g in games :
resultsTable[g[0]] = [0] * numRounds
resultsTable[g[1]] = [0] * numRounds
def simplePrint(winners) :
print(winners)
# Main Method
if __name__ == "__main__" :
# Get the teams in bracket order
firstGames = loadBracket()
setUpResults(firstGames)
games = firstGames
#print(games)
g = Group("GetWell Madness", 4)
a = g.simplePrint
# Simulate the appropriate number of times
numSim = 1000
for x in range(0, numSim) :
print("\rSimulation %d of %d" % (x+1, numSim), end='')
champ = oneSimulation(games, a)
g.endSimulation()
#print(champ)
#print(resultsTable)
print()
print()
sortedResultsTable = list(resultsTable)
sortedResultsTable.sort(key=lambda x: (resultsTable[x][3], resultsTable[x][2], resultsTable[x][1], resultsTable[x][0]), reverse=True)
for team in sortedResultsTable :
results = resultsTable[team]
print("%s\t%5.1f\t%5.1f\t%5.1f\t%5.1f" % (team.ljust(20), results[0]/numSim*100, results[1]/numSim*100, results[2]/numSim*100, results[3]/numSim*100))
print()
print()
print(g)