public
Last active

Monte Carlo for calculating the st.dev of an all skill NHL league

  • Download Gist
montecarlo.py
Python
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
import csv, random, math
 
# STuff do once
 
schedule = []
n = 10000
avgsd = 0.0
pskill = 24 #percent skill
 
# store the schedule
with open('schedule.csv', 'rU') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in spamreader:
schedule.append(row)
 
strengths = range(1,31)
 
for i in range(n):
# stuff do on each loop
 
wins = [0]*30
 
startScore = 0 # strength, and score
 
teams = {'PITTSBURGH' : startScore, 'PHILADELPHIA' : startScore, 'OTTAWA' : startScore, 'WINNIPEG' : startScore, 'CHICAGO' : startScore,
'LOS ANGELES' : startScore, 'BOSTON' : startScore, 'NY RANGERS' : startScore, 'TORONTO' : startScore, 'MONTREAL' : startScore,
'NEW JERSEY' : startScore, 'NY ISLANDERS' : startScore, 'TAMPA BAY' : startScore, 'WASHINGTON' : startScore, 'FLORIDA' : startScore,
'CAROLINA' : startScore, 'ST. LOUIS' : startScore, 'DETROIT' : startScore, 'COLUMBUS' : startScore, 'NASHVILLE' : startScore,
'DALLAS' : startScore, 'PHOENIX' : startScore, 'MINNESOTA' : startScore, 'COLORADO' : startScore, 'ANAHEIM' : startScore,
'VANCOUVER' : startScore, 'BUFFALO' : startScore, 'SAN JOSE' : startScore, 'CALGARY' : startScore, 'EDMONTON' : startScore}
 
# assign strengths randomly
random.shuffle(strengths)
 
sIndex = 0
for t in teams:
teams[t] = strengths[sIndex]
sIndex += 1
 
# "compete" the teams
for game in schedule:
if random.randint(1,100) <= (100-pskill):
if random.randint(1,2) == 1:
wins[teams[game[0]]-1] += 1
else:
wins[teams[game[1]]-1] += 1
else:
if teams[game[0]] < teams[game[1]]:
wins[teams[game[0]]-1] += 1
else:
wins[teams[game[1]]-1] += 1
 
wins = [x/82.0 for x in wins]
mean = sum(wins, 0.0) / len(wins)
d = [ (i - mean) ** 2 for i in wins]
std_dev = math.sqrt(sum(d) / len(d))
 
avgsd += std_dev
print std_dev
 
print "Average St.Dev: %s" % (avgsd/n)
print "Mean: %s" % (mean)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.