Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import collections
- import csv
- chances = {
- 'CLE': 275,
- 'NYG': 200,
- 'TB': 175,
- 'DEN': 150,
- 'NO': 105,
- 'ARI': 90,
- 'CIN': 80,
- 'MIN': 70,
- 'LAC': 60,
- 'SEA': 50,
- 'NE': 45,
- 'HOU': 40,
- 'PIT': 35,
- 'CAR': 30,
- 'NYJ': 25,
- 'LAR': 20,
- 'IND': 17,
- 'TEN': 14,
- 'CHI': 11,
- 'PHI': 8,
- }
- # Dict above does not maintain order so use this instead to fill picks 4-20
- order = ['CLE', 'NYG', 'TB', 'DEN', 'NO', 'ARI', 'CIN',
- 'MIN', 'LAC', 'SEA', 'NE', 'HOU', 'PIT', 'CAR',
- 'NYJ', 'LAR', 'IND', 'TEN', 'CHI', 'PHI']
- def chancesLeft(used):
- total = 1500
- for team in used:
- total -= chances[team]
- return total
- # A node contains a list of teams that won the first n spots and the probability we got to that point.
- # Its child nodes are nodes representing each sequence of n+1 teams starting with the teams we already have.
- class Node:
- def __init__(self, prob, winners):
- self.prob = prob
- self.winners = winners
- self.children = []
- def addChildren(self):
- for key, value in chances.items():
- if value > 0:
- if key not in self.winners:
- nextProb = value / float(chancesLeft(self.winners))
- newProb = self.prob * nextProb
- newWinners = self.winners + [key]
- self.children.append(Node(self.prob * nextProb, self.winners + [key]))
- def addDescendants(self, depth):
- if depth > 0:
- self.addChildren()
- for child in self.children:
- child.addDescendants(depth-1)
- # Returns all leaf nodes at bottom of tree; does not return internal descendants.
- def getAllDescendants(self):
- fullList = []
- if len(self.children) == 0:
- return [self]
- for child in self.children:
- fullList.extend(child.getAllDescendants())
- return fullList
- """
- Print a CSV with each team's odds of each pick.
- Since Philly has many likely scenarios in which they got multiple picks,
- and the likelihood of getting the Lakers pick depends on their own result to some degree,
- break their results out separately to find likelihood of each outcome.
- """
- def main():
- root = Node(1, [])
- root.addDescendants(5)
- probs = {}
- wasOutcomes = collections.defaultdict(int)
- for team in order:
- probs[team] = [0]*20
- for desc in root.getAllDescendants():
- wasPicks = ""
- for idx, val in enumerate(doTransfers(fullList(desc.winners))):
- probs[val][idx] += desc.prob
- if val == "WAS":
- wasPicks += str(idx + 1)
- wasOutcomes[wasPicks] += desc.prob
- printCSV(probs)
- def fullList(winners):
- output = winners
- for team in order:
- if team not in winners:
- output.append(team)
- return output
- def doTransfers(results):
- afterswap = results
- # In all cases, find pick in pre-swap results array, swap location in afterswap.
- return afterswap
- def printCSV(probs):
- with open('lotto_odds.csv', 'wb') as csvfile:
- oddswriter = csv.writer(csvfile, delimiter=',')
- for team in order:
- oddswriter.writerow([team] + probs[team])
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement