Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
run simulations of a single-elimination tournament
Nadal 1 12390
Daniel 564
Sweeting Q 486
Gimeno-Traver 844
Tomic W 239
Chardy 960
Falla 540
Lopez F 31 1310
Isner 20 1850
Serra 711
Stepanek 735
Gremelmayr Q 469
Machado 573
Giraldo 785
Young D Q 435
Cilic 15 2140
Youzhny 10 2920
Ilhan 574
Kavcic Q 552
Anderson K 868
Raonic Q 351
Phau 581
Chela 1070
Llodra 22 1575
Nalbandian 27 1480
Hewitt 870
Berankis 589
Matosevic W 392
Russell 547
Ebden W 288
Nieminen 1062
Ferrer 7 3735
Soderling 4 5785
Starace 945
Muller Q 466
Stadler Q 155
Istomin 1031
Hernych Q 196
Mello 627
Bellucci 30 1355
Gulbis 24 1505
Becker 870
Dolgopolov 928
Kukushkin 815
Seppi 900
Clement 627
Petzschner 839
Tsonga 13 2345
Melzer 11 2785
Millot Q 334
Ball W 344
Riba 672
Sela 568
Del Potro 180
Zemlja Q 376
Baghdatis 21 1785
Garcia-Lopez 32 1300
Berrer 835
Schwank 580
Mayer L 572
Marchenko 624
Ramirez Hidalgo 638
Beck K 543
Murray 5 5760
Berdych 6 3955
Crugnola Q 194
Kohlschreiber 1215
Kamke 724
Harrison W 313
Mannarino 612
Dancevic Q 172
Gasquet 28 1385
Davydenko 23 1555
Mayer F 1073
Fognini 855
Nishikori 599
Zverev 611
Tipsarevic 935
Schuettler 597
Verdasco 9 3240
Almagro 14 2160
Robert Q 460
Andreev 622
Volandri 574
Cipolla Q 190
Paire W 366
Luczak W 400
Ljubicic 17 1965
Troicki 29 1385
Tursunov 263
Dabul 584
Mahut Q 424
Karlovic 670
Dodig 606
Granollers 993
Djokovic 3 6240
Roddick 8 3565
Hajek 560
Przysiezny 590
Kunitsyn 551
Berlocq 725
Haase 803
Benneteau 965
Monaco 26 1480
Wawrinka 19 1855
Gabashvili 626
Dimitrov Q 518
Golubev 1135
Gil 551
Cuevas 790
De Bakker 950
Monfils 12 2560
Fish 16 1996
Hanescu 915
Robredo 915
Devvarman 514
Stakhovsky 925
Brands 541
Kubot 670
Querrey 18 1860
Montanes 25 1495
Brown 573
Andujar 683
Malisse 956
Lu 1141
Simon 1005
Lacko 553
Federer 2 9245
## run simulations of a single-elimination tournament
import random, copy
from createTuple import createTuple ## gist: 778481
from writeMatrixCSV import writeMatrixCSV ## gist: 778484
def calcWp(x, y, e):
x = float(x)
y = float(y)
return (x**e)/((x**e)+(y**e))
def findRoundWinners(roundPlayers):
## get each pair of players, return list of 'winners'
nextRound = []
roundMatches = len(roundPlayers)/2
for i in range(roundMatches):
j = i*2
playerOne = roundPlayers[j]
playerTwo = roundPlayers[j+1]
oneWp = calcWp(pointDict[playerOne], pointDict[playerTwo], e)
if random.random() < oneWp: winner = playerOne
else: winner = playerTwo
nextRound.append(winner)
return nextRound
e = 1.1
sims = 100000
draw = createTuple('aussie_draw_2011.csv')
blankResults = {1: 0,
2: 0,
4: 0,
8: 0
}
for z in [17, 33, 65]:
if len(draw) > z:
dsize = (z-1)
blankResults[dsize] = 0
pointDict = {}
firstRound = []
resultDict = {}
for pl in draw:
pointDict[pl[0]] = int(pl[2])
firstRound.append(pl[0])
resultDict[pl[0]] = copy.deepcopy(blankResults)
## run the simulation
for x in range(sims):
if x % (sims/10) == 0: print x
nextRound = firstRound
while True:
nextRound = findRoundWinners(nextRound)
n = len(nextRound)
for p in nextRound:
resultDict[p][n] += 1
if n == 1: break
rds = []
for z in [65, 33, 17]:
if len(draw) > z:
rdname = 'R' + str(z-1)
rds.append(rdname)
header = ['Player', '', 'points'] + rds + ['QF', 'SF', 'F', 'W']
resultTable = [header]
for pl in draw:
p = pl[0]
row = pl[:3]
for result in [64, 32, 16, 8, 4, 2, 1]:
perc = resultDict[p][result]/float(sims)
row.append(perc)
resultTable.append(row)
writeMatrixCSV(resultTable, 'aussie_sim_results_2011.csv')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment