Skip to content

@sareon /montecarlo.py
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Monte Carlo for calculating the st.dev of an all skill NHL league
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.