Advertisement
Romancandle332

NFLHC Lottery Python

Jan 26th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. import collections
  2. import csv
  3.  
  4. chances = {
  5. 'CLE': 275,
  6. 'NYG': 200,
  7. 'TB': 175,
  8. 'DEN': 150,
  9. 'NO': 105,
  10. 'ARI': 90,
  11. 'CIN': 80,
  12. 'MIN': 70,
  13. 'LAC': 60,
  14. 'SEA': 50,
  15. 'NE': 45,
  16. 'HOU': 40,
  17. 'PIT': 35,
  18. 'CAR': 30,
  19. 'NYJ': 25,
  20. 'LAR': 20,
  21. 'IND': 17,
  22. 'TEN': 14,
  23. 'CHI': 11,
  24. 'PHI': 8,
  25. }
  26.  
  27. # Dict above does not maintain order so use this instead to fill picks 4-20
  28. order = ['CLE', 'NYG', 'TB', 'DEN', 'NO', 'ARI', 'CIN',
  29. 'MIN', 'LAC', 'SEA', 'NE', 'HOU', 'PIT', 'CAR',
  30. 'NYJ', 'LAR', 'IND', 'TEN', 'CHI', 'PHI']
  31.  
  32. def chancesLeft(used):
  33. total = 1500
  34. for team in used:
  35. total -= chances[team]
  36. return total
  37.  
  38. # A node contains a list of teams that won the first n spots and the probability we got to that point.
  39. # Its child nodes are nodes representing each sequence of n+1 teams starting with the teams we already have.
  40. class Node:
  41. def __init__(self, prob, winners):
  42. self.prob = prob
  43. self.winners = winners
  44. self.children = []
  45.  
  46. def addChildren(self):
  47. for key, value in chances.items():
  48. if value > 0:
  49. if key not in self.winners:
  50. nextProb = value / float(chancesLeft(self.winners))
  51. newProb = self.prob * nextProb
  52. newWinners = self.winners + [key]
  53. self.children.append(Node(self.prob * nextProb, self.winners + [key]))
  54.  
  55. def addDescendants(self, depth):
  56. if depth > 0:
  57. self.addChildren()
  58. for child in self.children:
  59. child.addDescendants(depth-1)
  60.  
  61. # Returns all leaf nodes at bottom of tree; does not return internal descendants.
  62. def getAllDescendants(self):
  63. fullList = []
  64. if len(self.children) == 0:
  65. return [self]
  66. for child in self.children:
  67. fullList.extend(child.getAllDescendants())
  68. return fullList
  69.  
  70. """
  71. Print a CSV with each team's odds of each pick.
  72. Since Philly has many likely scenarios in which they got multiple picks,
  73. and the likelihood of getting the Lakers pick depends on their own result to some degree,
  74. break their results out separately to find likelihood of each outcome.
  75. """
  76. def main():
  77. root = Node(1, [])
  78. root.addDescendants(5)
  79. probs = {}
  80. wasOutcomes = collections.defaultdict(int)
  81. for team in order:
  82. probs[team] = [0]*20
  83. for desc in root.getAllDescendants():
  84. wasPicks = ""
  85. for idx, val in enumerate(doTransfers(fullList(desc.winners))):
  86. probs[val][idx] += desc.prob
  87. if val == "WAS":
  88. wasPicks += str(idx + 1)
  89. wasOutcomes[wasPicks] += desc.prob
  90. printCSV(probs)
  91.  
  92. def fullList(winners):
  93. output = winners
  94. for team in order:
  95. if team not in winners:
  96. output.append(team)
  97. return output
  98.  
  99. def doTransfers(results):
  100. afterswap = results
  101. # In all cases, find pick in pre-swap results array, swap location in afterswap.
  102. return afterswap
  103.  
  104. def printCSV(probs):
  105. with open('lotto_odds.csv', 'wb') as csvfile:
  106. oddswriter = csv.writer(csvfile, delimiter=',')
  107. for team in order:
  108. oddswriter.writerow([team] + probs[team])
  109.  
  110. if __name__ == '__main__':
  111. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement