Spaces:
Running
Running
Download services/model/elo.py from 2026worldcup/predictor: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/2026worldcup/predictor/resolve/main/services/model/elo.py
- Command line
-
hf download hf://spaces/2026worldcup/predictor/services/model/elo.py
-
curl -L -o elo.py https://huggingface.co/spaces/2026worldcup/predictor/resolve/main/services/model/elo.py
1.14 kB
| """Standard Elo ratings over international match history. | |
| World-Football-Elo style: K scaled by goal difference, home advantage applied | |
| unless the match is at a neutral venue.""" | |
| from __future__ import annotations | |
| import pandas as pd | |
| START = 1500.0 | |
| K = 40.0 | |
| HOME_ADV = 65.0 | |
| def _k_factor(goal_diff: int) -> float: | |
| g = abs(goal_diff) | |
| if g <= 1: | |
| return K | |
| if g == 2: | |
| return K * 1.5 | |
| return K * (1.75 + (g - 3) / 8.0) | |
| def compute_elo(df: pd.DataFrame) -> dict[str, float]: | |
| """df must be sorted-able by date and have scores. Returns final ratings.""" | |
| df = df.sort_values("date") | |
| r: dict[str, float] = {} | |
| for m in df.itertuples(index=False): | |
| h, a = m.home_team, m.away_team | |
| rh = r.get(h, START) | |
| ra = r.get(a, START) | |
| adv = 0.0 if bool(m.neutral) else HOME_ADV | |
| exp_h = 1.0 / (1.0 + 10 ** (-((rh + adv) - ra) / 400.0)) | |
| gd = int(m.home_score) - int(m.away_score) | |
| res_h = 1.0 if gd > 0 else (0.5 if gd == 0 else 0.0) | |
| k = _k_factor(gd) | |
| delta = k * (res_h - exp_h) | |
| r[h] = rh + delta | |
| r[a] = ra - delta | |
| return r | |