Spaces:
Sleeping
Sleeping
File size: 1,611 Bytes
822fdea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import math
import random
import itertools
from data_loader import teams, team_map
def elo_probability(teamA, teamB):
teamA = team_map.get(teamA.upper(), teamA.upper())
teamB = team_map.get(teamB.upper(), teamB.upper())
teamA_row = teams[teams['team'] == teamA]
teamB_row = teams[teams['team'] == teamB]
if teamA_row.empty:
return f"Error: Team {teamA} not found"
if teamB_row.empty:
return f"Error: Team {teamB} not found"
eloA = teamA_row['elo_rating'].values[0]
eloB = teamB_row['elo_rating'].values[0]
probA = 1 / (1 + 10 ** ((eloB - eloA) / 550))
return {
"teamA": teamA,
"teamB": teamB,
"probA": probA,
"probB": 1 - probA
}
def simulate_match(teamA, teamB):
probs = elo_probability(teamA, teamB)
r = random.random()
if r < probs["probA"]:
return teamA
else:
return teamB
def monte_carlo_group(group_name, simulations=3000):
group_teams = teams[teams["group"] == group_name]["team"].tolist()
qualify_counts = {team:0 for team in group_teams}
for _ in range(simulations):
results = {team:0 for team in group_teams}
for a,b in itertools.combinations(group_teams,2):
winner = simulate_match(a,b)
results[winner] += 2
standings = sorted(results.items(), key=lambda x:x[1], reverse=True)
qualify_counts[standings[0][0]] += 1
qualify_counts[standings[1][0]] += 1
output = {}
for team in qualify_counts:
output[team] = qualify_counts[team]/simulations
return output |