import random
from collections import Counter
# Settings for each game
games = {
"Apr 28 @ WSN": {"Mets_strength": 0.53, "Opp_strength": 0.47, "total_runs_mean": 8.2},
"Apr 29 vs ARI": {"Mets_strength": 0.46, "Opp_strength": 0.54, "total_runs_mean": 7.1},
"Apr 30 vs ARI": {"Mets_strength": 0.48, "Opp_strength": 0.52, "total_runs_mean": 9.6},
"May 1 vs ARI": {"Mets_strength": 0.55, "Opp_strength": 0.45, "total_runs_mean": 9.2}
}
# Simulate
simulations_per_game = 10000
results = {}
for game, data in games.items():
mets_wins = 0
opp_wins = 0
total_runs = []
scorelines = []
for _ in range(simulations_per_game):
# Determine winner
winner = "Mets" if random.random() < data["Mets_strength"] else "Opponent"
if winner == "Mets":
mets_wins += 1
else:
opp_wins += 1
# Generate a total runs number
runs = random.gauss(data["total_runs_mean"], 2.5)
runs = max(0, round(runs)) # Ensure no negative runs
# Randomly assign runs based on winner
if winner == "Mets":
mets_score = random.randint(runs//2, runs)
opp_score = runs - mets_score
else:
opp_score = random.randint(runs//2, runs)
mets_score = runs - opp_score
scorelines.append((mets_score, opp_score))
total_runs.append(mets_score + opp_score)
# Aggregate results
most_common_score = Counter(scorelines).most_common(1)[0][0]
avg_total_runs = sum(total_runs) / len(total_runs)
results[game] = {
"Mets_win_pct": round(mets_wins / simulations_per_game * 100, 1),
"Opponent_win_pct": round(opp_wins / simulations_per_game * 100, 1),
"Most_common_score": most_common_score,
"Avg_total_runs": round(avg_total_runs, 2)
}
results
Result
{'Apr 28 @ WSN': {'Mets_win_pct': 52.5,
'Opponent_win_pct': 47.5,
'Most_common_score': (4, 3),
'Avg_total_runs': 8.18},
'Apr 29 vs ARI': {'Mets_win_pct': 45.7,
'Opponent_win_pct': 54.3,
'Most_common_score': (3, 3),
'Avg_total_runs': 7.08},
'Apr 30 vs ARI': {'Mets_win_pct': 47.8,
'Opponent_win_pct': 52.2,
'Most_common_score': (5, 4),
'Avg_total_runs': 9.61},
'May 1 vs ARI': {'Mets_win_pct': 54.9,
'Opponent_win_pct': 45.1,
'Most_common_score': (5, 4),
'Avg_total_runs': 9.23}}