Spaces:
Sleeping
Sleeping
Download src/probability_engine.py from ajaykumar0712/Streamlit-Testing: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/ajaykumar0712/Streamlit-Testing/resolve/f5237b341eec0f9a4bb5c1917dfea4de9b66a1ea/src/probability_engine.py
- Command line
-
hf download hf://spaces/ajaykumar0712/Streamlit-Testing@f5237b341eec0f9a4bb5c1917dfea4de9b66a1ea/src/probability_engine.py
-
curl -L -o probability_engine.py https://huggingface.co/spaces/ajaykumar0712/Streamlit-Testing/resolve/f5237b341eec0f9a4bb5c1917dfea4de9b66a1ea/src/probability_engine.py
1.61 kB
| 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 |