Streamlit-Testing / src /probability_engine.py
ajaykumar0712's picture
Created probability engine file
822fdea verified
Raw History Blame
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