Created
December 1, 2016 04:02
-
-
Save kylesnowschwartz/4a8e6037a2bf6b67c1e922661d0ff5ae to your computer and use it in GitHub Desktop.
Justins League Simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import numpy as np | |
import copy | |
num_simul = 10000 | |
# get initial standings | |
init_standings, past_perform, playoff_count = dict(), dict(), dict() | |
f = open('2016_week12_standings.txt'); lines = f.readlines(); f.close() | |
for line in lines[1:]: #skips header | |
t = line.rstrip('\n').split('\t') | |
owner = t[0] | |
init_standings[ owner ] = [ int(t[1]), float(t[2]) ] #number of wins and total points | |
past_perform[ owner ] = map(float, t[3:5]) #average score and standard deviation | |
playoff_count[ owner ] = 0 # initialize counts to zero for each owner | |
# get list of matchups | |
matchups = list() | |
f = open('matchups_week13-14.txt'); lines = f.readlines(); f.close() | |
for line in lines: | |
matchups.append( tuple( line.rstrip('\n').split('\t') ) ) | |
# run simulations | |
standings = dict() | |
for sim in range(num_simul): | |
# re-initialize standings | |
standings = copy.deepcopy(init_standings) | |
# play out matchups | |
for m in matchups: | |
# randomly chooses scores based on average and STDev for each person (normal distribution) | |
A = np.random.normal( past_perform[m[0]][0], past_perform[m[0]][1] ) | |
B = np.random.normal( past_perform[m[1]][0], past_perform[m[1]][1] ) | |
# updates total points | |
standings[ m[0] ][1] += A | |
standings[ m[1] ][1] += B | |
# updates standings | |
if A > B: | |
standings[ m[0] ][0] += 1 | |
else: | |
standings[ m[1] ][0] += 1 | |
# get 4 top finishers and add to playoff count | |
for winner in sorted(standings, key = lambda k: (standings[k][0], standings[k][1]), reverse = True)[:4]: | |
playoff_count[ winner ] += 1 | |
# print results | |
for owner, count in sorted(playoff_count.items(), key = lambda k: k[1], reverse = True): | |
print owner + ':\t{:.2%}'.format( float(count) / num_simul ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's not clear why you're traversing a 2D array too though?
[4:32] like if it were ruby you'd have an object
[4:32] be doing
Player.mean
[4:32]
Player.standard_deviation
[4:32]
Player.expected_score
and the matchups
[4:33]
[0] and [1]
have meanings[4:33] are they player_one and player_two?