__author__ = 'mstock' import numpy import random as rand # Change r declarations (rand.random()) sims = 1 pbata1 = {} pbata2 = {} pbata3 = {} pbata4 = {} pbata5 = {} pbata6 = {} pbata7 = {} pbata8 = {} pbata9 = {} ppitb1 = {} ppitb2 = {} ppitb3 = {} ppitb4 = {} ppitb5 = {} statbata1 = {'H' : 0, '1B' : 0, '2B' : 0, '3B' : 0, 'HR' : 0, 'outs' : 0, 'K' : 0, 'BB' : 0, 'SB' : 0, 'CS': 0, 'R' : 0, 'RBI' : 0} statbata2 = {'H' : 0, '1B' : 0, '2B' : 0, '3B' : 0, 'HR' : 0, 'outs' : 0, 'K' : 0, 'BB' : 0, 'SB' : 0, 'CS': 0, 'R' : 0, 'RBI' : 0} statbata3 = {'H' : 0, '1B' : 0, '2B' : 0, '3B' : 0, 'HR' : 0, 'outs' : 0, 'K' : 0, 'BB' : 0, 'SB' : 0, 'CS': 0, 'R' : 0, 'RBI' : 0} statbata4 = {'H' : 0, '1B' : 0, '2B' : 0, '3B' : 0, 'HR' : 0, 'outs' : 0, 'K' : 0, 'BB' : 0, 'SB' : 0, 'CS': 0, 'R' : 0, 'RBI' : 0} statbata5 = {'H' : 0, '1B' : 0, '2B' : 0, '3B' : 0, 'HR' : 0, 'outs' : 0, 'K' : 0, 'BB' : 0, 'SB' : 0, 'CS': 0, 'R' : 0, 'RBI' : 0} statbata6 = {'H' : 0, '1B' : 0, '2B' : 0, '3B' : 0, 'HR' : 0, 'outs' : 0, 'K' : 0, 'BB' : 0, 'SB' : 0, 'CS': 0, 'R' : 0, 'RBI' : 0} statbata7 = {'H' : 0, '1B' : 0, '2B' : 0, '3B' : 0, 'HR' : 0, 'outs' : 0, 'K' : 0, 'BB' : 0, 'SB' : 0, 'CS': 0, 'R' : 0, 'RBI' : 0} statbata8 = {'H' : 0, '1B' : 0, '2B' : 0, '3B' : 0, 'HR' : 0, 'outs' : 0, 'K' : 0, 'BB' : 0, 'SB' : 0, 'CS': 0, 'R' : 0, 'RBI' : 0} statbata9 = {'H' : 0, '1B' : 0, '2B' : 0, '3B' : 0, 'HR' : 0, 'outs' : 0, 'K' : 0, 'BB' : 0, 'SB' : 0, 'CS': 0, 'R' : 0, 'RBI' : 0} teamABat = [pbata1, pbata2, pbata3, pbata4, pbata5, pbata6, pbata7, pbata8, pbata9] teamAPit = [] teamBBat = [] teamBPit = [ppitb1, ppitb2, ppitb3, ppitb4, ppitb5] teamA = [teamABat,teamAPit] teamB = [teamBBat, teamBPit] teams = [teamA, teamB] statsBattersA = [statbata1] statsBattersB = [0] teamsBatters = [statsBattersA,statsBattersB] teamsScore = [0, 0] global basesFill basesFill = [0, 0, 0] global nOuts nOuts = 0 global t t = 0 def importA(): teams[0][0][0]['name'] = 'McCann' teams[0][0][0]['position'] = 'C' teams[0][0][0]['hitP'] = 0.218 teams[0][0][0]['hitT1B'] = 0.582 teams[0][0][0]['hitT2B'] = 0.154 teams[0][0][0]['hitT3B'] = 0.011 teams[0][0][0]['hitTHR'] = 0.253 teams[0][0][0]['outP'] = 0.676 teams[0][0][0]['outTK'] = 0.280 teams[0][0][0]['outTOO'] = 0.699 teams[0][0][0]['outTSac'] = 0.021 teams[0][0][0]['outDP'] = 0.025 teams[0][0][0]['walkP'] = 0.106 teams[0][0][0]['sbO2'] = 0.000 teams[0][0][0]['sb2'] = 0.000 teams[0][0][0]['sbO3'] = 0.000 teams[0][0][0]['sb3'] = 0.000 teams[0][0][0]['1stS2'] = 0.800 teams[0][0][0]['1stS3'] = 0.200 teams[0][0][0]['1stD3'] = 0.714 teams[0][0][0]['1stDH'] = 0.143 teams[0][0][0]['2ndS3'] = 0.625 teams[0][0][0]['2ndSH'] = 0.375 def importB(): teams[1][1][0]['name'] = 'Evoaldi' teams[1][1][0]['position'] = 'P' teams[1][1][0]['hitP'] = 0.260 teams[1][1][0]['hitT1B'] = 0.790 teams[1][1][0]['hitT2B'] = 0.142 teams[1][1][0]['hitT3B'] = 0.006 teams[1][1][0]['hitTHR'] = 0.062 teams[1][1][0]['outP'] = 0.668 teams[1][1][0]['outTK'] = 0.257 teams[1][1][0]['outTOO'] = 0.728 teams[1][1][0]['outTSac'] = 0.014 teams[1][1][0]['outDP'] = 0.043 teams[1][1][0]['walkP'] = 0.072 def setPlayer(): playerIndex = 0 return playerIndex def getRunner1B(): i1B = 0 return i1B def getRunner2B(): i2B = 0 return i2B def getRunner3B(): i3B = 0 return i3B def setTeam(): teamIndex = 0 global t t = teamIndex return teamIndex def plateAppearance(i): r = rand.random() global basesFill newBases = basesFill global nOuts if r <= hit(): bases = hitType() newBases = basesStatus(bases, basesFill) print bases if bases == "1B": teamsBatters[t][i]['1B'] += 1 elif bases == "2B": teamsBatters[t][i]['2B'] += 1 elif bases == "3B": teamsBatters[t][i]['3B'] += 1 elif bases == "HR": teamsBatters[t][i]['HR'] += 1 teamsBatters[t][i]['H'] += 1 elif r <= hit() + out() and r > hit(): teamsBatters[t][i]['outs'] += 1 outs = outType() print outs if outs == "K": teamsBatters[t][i]['K'] += 1 newBases = basesStatus(outs, basesFill) elif r <= hit() + out() + walk() and r > hit() + out(): teamsBatters[t][i]['BB'] += 1 bb = "Walk" print "Walk" newBases = basesStatus(bb, basesFill) # includes HBP basesFill = newBases print basesFill print def basesStatus(bases, basesFill): global nOuts newBasesFill = basesFill if bases == "1B": firstto2nd = teams[t][0][getRunner1B()]['1stS2'] firstto3rd = teams[t][0][getRunner1B()]['1stS3'] secto3rd = teams[t][0][getRunner2B()]['2ndS3'] sectoH = teams[t][0][getRunner2B()]['2ndSH'] r = rand.random() firston2nd = firston3rd = secon3rd = seconH = False if r <= firstto2nd: firston2nd = True elif r <= firstto2nd + firstto3rd and r > firstto2nd: firston3rd = True r = rand.random() if r <= secto3rd: secon3rd = True elif r <= sectoH + secto3rd and r > secto3rd: seconH = True if basesFill == [0, 0, 0]: newBasesFill = [1, 0, 0] elif basesFill == [1, 0, 0]: if firston2nd: newBasesFill = [1, 1, 0] elif firston3rd: newBasesFill = [1, 0, 1] else: newBasesFill = [1, 0, 0] nOuts += 1 elif basesFill == [0, 1, 0]: if seconH: teamsScore[t] += 1 newBasesFill = [1, 0, 0] elif secon3rd: newBasesFill = [1, 0, 1] else: newBasesFill = [1, 0, 0] nOuts += 1 elif basesFill == [0, 0, 1]: teamsScore[t] += 1 newBasesFill = [1, 0, 0] elif basesFill == [1, 1, 0]: if seconH: teamsScore[t] += 1 if firston3rd: newBasesFill = [1, 0, 1] elif firston2nd: newBasesFill = [1, 1, 0] else: nOuts += 1 newBasesFill = [1, 0, 0] elif secon3rd: newBasesFill = [1, 1, 1] else: nOuts += 1 if firston3rd: newBasesFill = [1, 0, 1] else: newBasesFill = [1, 1, 0] elif basesFill == [1, 0, 1]: teamsScore[t] += 1 if firston3rd: newBasesFill = [1, 0, 1] elif firston2nd: newBasesFill = [1, 1, 0] else: nOuts += 1 newBasesFill = [1, 0, 0] elif basesFill == [0, 1, 1]: teamsScore[t] += 1 if seconH: teamsScore[t] += 1 newBasesFill = [1, 0, 0] elif secon3rd: newBasesFill = [1, 0, 1] else: newBasesFill = [1, 0, 0] elif basesFill == [1, 1, 1]: teamsScore[t] += 1 if seconH: teamsScore[t] += 1 if firston2nd: newBasesFill = [1, 1, 0] elif firston3rd: newBasesFill = [1, 0, 1] else: newBasesFill == [1, 1, 1] elif bases == "2B": firstto3rd = teams[t][0][getRunner1B()]['1stD3'] firsttoH = teams[t][0][getRunner1B()]['1stDH'] r = rand.random() firston3rd = firstonH = False if r <= firstto3rd: firston3rd = True elif r <= firstto3rd + firsttoH and r > firstto3rd: firstonH = True if basesFill == [0, 0, 0]: newBasesFill = [0, 1, 0] elif basesFill == [1, 0, 0]: if firston3rd: newBasesFill = [0, 1, 1] elif firstonH: teamsScore[t] += 1 newBasesFill = [0, 1, 0] else: nOuts += 1 newBasesFill = [0, 1, 0] elif basesFill == [0, 1, 0]: teamsScore[t] += 1 newBasesFill = [0, 1, 0] elif basesFill == [0, 0, 1]: teamsScore[t] += 1 newBasesFill = [0, 1, 0] elif basesFill == [1, 1, 0]: teamsScore[t] += 1 if firston3rd: newBasesFill = [0, 1, 1] elif firstonH: teamsScore[t] += 1 newBasesFill = [0, 1, 0] else: nOuts += 1 newBasesFill = [0, 1, 0] elif basesFill == [1, 0, 1]: teamsScore[t] += 1 if firston3rd: newBasesFill = [0, 1, 1] elif firstonH: teamsScore[t] += 1 newBasesFill = [0, 1, 0] else: nOuts += 1 newBasesFill = [0, 1, 0] elif basesFill == [0, 1, 1]: teamsScore[t] += 2 newBasesFill = [0, 1, 0] elif basesFill == [1, 1, 1]: teamsScore[t] += 2 if firston3rd: newBasesFill = [0, 1, 1] elif firstonH: teamsScore[t] += 1 newBasesFill = [0, 1, 0] else: nOuts += 1 newBasesFill = [0, 1, 0] elif bases == "3B": if basesFill == [0, 0, 0]: newBasesFill = [0, 0, 1] elif basesFill == [1, 0, 0]: teamsScore[t] += 1 newBasesFill = [0, 0, 1] elif basesFill == [0, 1, 0]: teamsScore[t] += 1 newBasesFill == [0, 0, 1] elif basesFill == [0, 0, 1]: teamsScore[t] += 1 newBasesFill == [0, 0, 1] elif basesFill == [1, 1, 0]: teamsScore[t] += 2 newBasesFill = [0, 0, 1] elif basesFill == [1, 0, 1]: teamsScore[t] += 2 newBasesFill = [0, 0, 1] elif basesFill == [0, 1, 1]: teamsScore[t] += 2 newBasesFill = [0, 0, 1] elif basesFill == [1, 1, 1]: teamsScore[t] += 3 newBasesFill = [0, 0, 1] elif bases == "HR": if basesFill == [0, 0, 0]: teamsScore[t] += 1 newBasesFill = [0, 0, 0] elif basesFill == [1, 0, 0]: teamsScore[t] += 2 newBasesFill = [0, 0, 0] elif basesFill == [0, 1, 0]: teamsScore[t] += 2 newBasesFill == [0, 0, 0] elif basesFill == [0, 0, 1]: teamsScore[t] += 2 newBasesFill == [0, 0, 0] elif basesFill == [1, 1, 0]: teamsScore[t] += 3 newBasesFill = [0, 0, 0] elif basesFill == [1, 0, 1]: teamsScore[t] += 3 newBasesFill = [0, 0, 0] elif basesFill == [0, 1, 1]: teamsScore[t] += 3 newBasesFill = [0, 0, 0] elif basesFill == [1, 1, 1]: teamsScore[t] += 4 newBasesFill = [0, 0, 0] elif bases == "OO": if basesFill[0] == 1 and nOuts < 2: bFill2 = basesFill[1] bFill3 = basesFill[2] dpPer = dp() r = rand.random() if r <= dpPer: if bFill3 == 1 and nOuts == 0: teamsScore[t] += 1 newBasesFill = [0, 0, bFill2] nOuts += 2 else: nOuts += 1 newBasesFill[0] = 1 newBasesFill[1] = 0 if bFill3 == 1: teamsScore[t] += 1 if bFill2 == 1: newBasesFill[2] = 1 else: nOuts += 1 elif bases == "Sac": if basesFill[2] == 1 and nOuts < 2: teamsScore[t] += 1 newBasesFill[1] = basesFill[1] newBasesFill[0] = basesFill[0] elif basesFill[0] == 1 or basesFill[1] == 1: newBasesFill[1] = basesFill[0] newBasesFill[2] = basesFill[1] nOuts += 1 elif bases == "K": nOuts += 1 elif bases == "Walk": if basesFill == [1, 0, 0] or basesFill == [1, 0, 1]: newBasesFill[1] = 1 elif basesFill == [1, 1, 0]: newBasesFill = [1, 1, 1] if basesFill == [1, 1, 1]: teamsScore[t] += 1 newBasesFill[0] = 1 return newBasesFill def checkOuts(): endInn = False if nOuts == 3: endInn = True return endInn def steal(): global nOuts if basesFill == [1, 0, 0] or basesFill == [1, 0, 1]: r = rand.random() if r <= teams[t][0][getRunner1B()]['sbO2']: r = rand.random() if r <= teams[t][0][getRunner1B()]['sb2']: teamsBatters[t][getRunner1B()]['SB'] += 1 basesFill[1] = 1 basesFill[0] = 0 else: teamsBatters[t][getRunner1B()]['CS'] += 1 basesFill[0] = 0 nOuts += 1 elif basesFill == [0, 1, 0] or basesFill == [1, 1, 0]: r = rand.random() if r <= teams[t][0][getRunner2B()]['sbO3']: r = rand.random() if r <= teams[t][0][getRunner2B()]['sb3']: teamsBatters[t][getRunner2B()]['SB'] += 1 basesFill[2] = 1 basesFill[1] = 0 else: teamsBatters[t][getRunner2B()]['CS'] += 1 basesFill[1] = 0 nOuts += 1 def hitType(): r = rand.random() type = "none" types = ["1B", "2B", "3B", "HR"] t = setTeam() i = setPlayer() single = (teams[t][0][i]['hitT1B'] + teams[t+1][1][i]['hitT1B']) / 2.0 double = (teams[t][0][i]['hitT2B'] + teams[t+1][1][i]['hitT2B']) / 2.0 triple = (teams[t][0][i]['hitT3B'] + teams[t+1][1][i]['hitT3B']) / 2.0 homer = (teams[t][0][i]['hitTHR'] + teams[t+1][1][i]['hitTHR']) / 2.0 hitProbs = [single, double, triple, homer] index = 0 for i in range(4): if i == 0 and r <= hitProbs[i]: type = types[i] elif r <= index + hitProbs[i] and r > index: type = types[i] index += hitProbs[i] return type def hit(): playerBA = teams[setTeam()][0][setPlayer()]['hitP'] pitcherBA = teams[setTeam()+1][1][setPlayer()]['hitP'] hitSuccess = (playerBA + pitcherBA) / 2.0 return hitSuccess def out(): playerOA = teams[setTeam()][0][setPlayer()]['outP'] pitcherOA = teams[setTeam()+1][1][setPlayer()]['outP'] outSuccess = (playerOA + pitcherOA) / 2.0 return outSuccess def outType(): r = rand.random() type = "OO" types = ["K", "OO", "Sac"] t = setTeam() i = setPlayer() if basesFill == [0,0,0]: sacProb = 0 else: sacProb = teams[t][0][i]['outTSac'] outProbs = [teams[t][0][i]['outTK'],teams[t][0][i]['outTOO'],sacProb] index = 0 for i in range(3): if i == 0 and r <= outProbs[i]: type = types[i] elif r <= index + outProbs[i] and r > index: type = types[i] index += outProbs[i] return type def walk(): playerWA = teams[setTeam()][0][setPlayer()]['walkP'] pitcherWA = teams[setTeam()+1][1][setPlayer()]['walkP'] walkSuccess = (playerWA + pitcherWA) / 2.0 return walkSuccess def dp(): playerDPA = teams[setTeam()][0][setPlayer()]['outDP'] pitcherDPA = teams[setTeam()+1][1][setPlayer()]['outDP'] dpSuccess = (playerDPA + pitcherDPA) / 2.0 return dpSuccess def run(): importA() importB() setTeam() for i in range(sims): for o in range(1,10): global nOuts while not checkOuts(): steal() plateAppearance(setPlayer()) print "Score: ", teamsScore[0] print nOuts nOuts = 0 global basesFill basesFill = [0, 0, 0] print teamsBatters[t][setPlayer()] print float (teamsScore[0]) / sims whip = teamsBatters[t][setPlayer()]['BB'] + teamsBatters[t][setPlayer()]['H'] print float (whip) / (sims * 9) run() # Need to track R, RBI
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question