Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
1,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. import random
  2. from copy import deepcopy
  3. import statistics as stat
  4. from math import exp
  5.  
  6. print(exp(1))
  7. class Team():
  8. def __init__(self, qb_wins_threshold=0.51, winning_teams=True, win_odds_ratio=1):
  9. self.wins = 0
  10. self.losses = 0
  11. self.qb_wins = 0
  12. self.qb_losses = 0
  13. self.qb_wins_threshold = qb_wins_threshold
  14. self.winning_teams = winning_teams
  15. self.win_odds_ratio = win_odds_ratio
  16.  
  17. def reset_season(self):
  18. self.wins = 0
  19. self.losses = 0
  20.  
  21. def win(self):
  22. self.wins += 1
  23.  
  24. def win_update_qb(self, other_team):
  25. if self.qb_wins_threshold == None:
  26. return
  27. if other_team.win_loss() > self.qb_wins_threshold and self.winning_teams:
  28. self.qb_wins += 1
  29. elif other_team.win_loss() < self.qb_wins_threshold and not self.winning_teams:
  30. self.qb_wins += 1
  31.  
  32. def loss(self):
  33. self.losses += 1
  34.  
  35. def loss_update_qb(self, other_team):
  36. if self.qb_wins_threshold == None:
  37. return
  38. if other_team.win_loss() > self.qb_wins_threshold and self.winning_teams:
  39. self.qb_losses += 1
  40. elif other_team.win_loss() < self.qb_wins_threshold and not self.winning_teams:
  41. self.qb_losses += 1
  42.  
  43. def win_loss(self):
  44. if self.wins + self.losses == 0:
  45. return None
  46. else:
  47. return self.wins / (self.wins+self.losses)
  48.  
  49. def get_QB_stats(self):
  50. num_games = self.qb_wins + self.qb_losses
  51. return (num_games, self.qb_wins)
  52.  
  53. class stats_manager():
  54. def __init__(self):
  55. self.num_games_dict = {}
  56.  
  57. def update(self, tup):
  58. if tup[0] == 0:
  59. return
  60. if tup[0] not in self.num_games_dict:
  61. self.num_games_dict[tup[0]] = []
  62. self.num_games_dict[tup[0]].append(tup[1] / tup[0])
  63.  
  64. def print_stats(self, lower=0.005, higher=0.995):
  65. num_games = list(self.num_games_dict.keys())
  66. num_games.sort()
  67. for num in num_games:
  68. self.num_games_dict[num].sort()
  69. low_idx = int(len(self.num_games_dict[num])*lower)
  70. high_idx = int(len(self.num_games_dict[num])*higher)
  71. low_val = self.num_games_dict[num][low_idx]
  72. mid_val = stat.median(self.num_games_dict[num])
  73. high_val = self.num_games_dict[num][high_idx]
  74. print('Num games:', num, 'low:', round(low_val,2), 'mid:', round(mid_val,2), 'high:', round(high_val,2), 'n =', len(self.num_games_dict[num]))
  75.  
  76.  
  77. sm = stats_manager()
  78.  
  79. num_sims = 1000
  80.  
  81. for sim in range(num_sims):
  82. num_teams = 32
  83. teams = [Team(win_odds_ratio=1) for i in range(0, num_teams)]
  84. team_map = {}
  85. i = 0
  86. for team in teams:
  87. team_map[i] = team
  88. i += 1
  89. season_len = 16
  90. num_seasons = 10
  91.  
  92. for season in range(num_seasons):
  93. for game in range(season_len):
  94. random_team_order = [i for i in range(num_teams)]
  95. random.shuffle(random_team_order)
  96. random_team_pairs = [(random_team_order[i], random_team_order[i+1]) for i in range(0, num_teams, 2)]
  97. for idx_A, idx_B in random_team_pairs:
  98. team_A = team_map[idx_A]
  99. team_B = team_map[idx_B]
  100. if random.random() > exp(team_A.win_odds_ratio - team_B.win_odds_ratio) / (1+exp(team_A.win_odds_ratio - team_B.win_odds_ratio)):
  101. team_A.win()
  102. team_B.loss()
  103. team_A.win_update_qb(team_B)
  104. team_B.loss_update_qb(team_A)
  105. else:
  106. team_A.loss()
  107. team_B.win()
  108. team_B.win_update_qb(team_A)
  109. team_A.loss_update_qb(team_B)
  110.  
  111. for team in teams:
  112. #if 2.5 > team.win_odds_ratio > 1.5:
  113. sm.update(team.get_QB_stats())
  114. for team in teams:
  115. team.reset_season()
  116.  
  117. avg = []
  118. for team_A in teams:
  119. for team_B in teams:
  120. favorite = 0.5 + abs(0.5-exp(team_A.win_odds_ratio - team_B.win_odds_ratio) / (1+exp(team_A.win_odds_ratio - team_B.win_odds_ratio)))
  121. avg.append(favorite)
  122.  
  123. sm.print_stats()
  124. print('Avg favorite:', stat.mean(avg))
  125.  
  126. #for team in teams:
  127. # print(team.get_QB_stats())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement