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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""export_dataset.py - generates the portable, DERIVED/synthetic sample dataset.
|
| 2 |
+
|
| 3 |
+
This script does NOT read any live database, and it does NOT copy or
|
| 4 |
+
transform raw rows from football-data.co.uk or any other third-party
|
| 5 |
+
match-data provider. It generates a synthetic-but-realistic set of
|
| 6 |
+
engineered match features and 1X2 outcome
|
| 7 |
+
labels, distributed to match the empirical priors used in production
|
| 8 |
+
(class balance ~46% home win / 27% draw / 27% away win, which mirrors
|
| 9 |
+
publicly documented long-run baselines for the 5 European top-flight
|
| 10 |
+
leagues the production pipeline covers).
|
| 11 |
+
|
| 12 |
+
Why synthetic and not a real-data aggregate:
|
| 13 |
+
- football-data.co.uk's commercial-use / redistribution terms for this
|
| 14 |
+
project were flagged as unverified in the ingestion code itself
|
| 15 |
+
(ml-service/app/ingestion/datasets/football_results_2018_2026.py,
|
| 16 |
+
LICENSE = "Verificare commercial use con football-data.co.uk").
|
| 17 |
+
- The other live-odds third-party source integrated elsewhere in the
|
| 18 |
+
monorepo pipeline is explicitly non-redistributable in any form (its
|
| 19 |
+
ingestion config flags it `is_public_redistribution_allowed=False`)
|
| 20 |
+
and no row from it is ever touched by this export script.
|
| 21 |
+
- Production Postgres (sport_intelligence.fixture) is never read from a
|
| 22 |
+
public repo context.
|
| 23 |
+
- Generating a synthetic sample sidesteps the licensing question
|
| 24 |
+
entirely: no third-party row, raw or aggregated, is shipped. Only the
|
| 25 |
+
feature SCHEMA (column names/semantics) is derived from the real
|
| 26 |
+
pipeline (app/sport_intelligence/features/db_extractor.py
|
| 27 |
+
FEATURE_NAMES_V2), which is this project's own original engineering
|
| 28 |
+
work, not third-party data.
|
| 29 |
+
|
| 30 |
+
The 25 features generated here match FEATURE_NAMES_V2 from
|
| 31 |
+
app/sport_intelligence/features/db_extractor.py:
|
| 32 |
+
dc_p_home, dc_p_draw, dc_p_away, elo_p_home, elo_p_draw, elo_p_away,
|
| 33 |
+
imp_home, imp_draw, imp_away, home_form5_pts, away_form5_pts,
|
| 34 |
+
home_form5_goals_for, away_form5_goals_for, home_form5_goals_against,
|
| 35 |
+
away_form5_goals_against, home_form5_avg_xg, away_form5_avg_xg,
|
| 36 |
+
home_form5_avg_xga, away_form5_avg_xga, home_days_rest, away_days_rest,
|
| 37 |
+
h2h_home_win_rate_5, h2h_avg_total_goals_5, home_lineup_rating,
|
| 38 |
+
away_lineup_rating
|
| 39 |
+
|
| 40 |
+
Usage:
|
| 41 |
+
python export_dataset.py --rows 4000 --seed 42 --output data/derived_sample.csv
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
from __future__ import annotations
|
| 45 |
+
|
| 46 |
+
import argparse
|
| 47 |
+
import csv
|
| 48 |
+
import random
|
| 49 |
+
from pathlib import Path
|
| 50 |
+
|
| 51 |
+
FEATURE_NAMES_V2 = [
|
| 52 |
+
"dc_p_home", "dc_p_draw", "dc_p_away",
|
| 53 |
+
"elo_p_home", "elo_p_draw", "elo_p_away",
|
| 54 |
+
"imp_home", "imp_draw", "imp_away",
|
| 55 |
+
"home_form5_pts", "away_form5_pts",
|
| 56 |
+
"home_form5_goals_for", "away_form5_goals_for",
|
| 57 |
+
"home_form5_goals_against", "away_form5_goals_against",
|
| 58 |
+
"home_form5_avg_xg", "away_form5_avg_xg",
|
| 59 |
+
"home_form5_avg_xga", "away_form5_avg_xga",
|
| 60 |
+
"home_days_rest", "away_days_rest",
|
| 61 |
+
"h2h_home_win_rate_5", "h2h_avg_total_goals_5",
|
| 62 |
+
"home_lineup_rating", "away_lineup_rating",
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
# Empirical long-run class prior for the 5 covered leagues (documented in
|
| 66 |
+
# DATA_PROVENANCE.md). NOT derived from a specific third-party file - used
|
| 67 |
+
# only to shape the synthetic label distribution realistically.
|
| 68 |
+
_HOME_WIN_PRIOR = 0.46
|
| 69 |
+
_DRAW_PRIOR = 0.27
|
| 70 |
+
# away win = remainder
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def _sample_team_strength(rng: random.Random) -> float:
|
| 74 |
+
"""Latent team strength on an Elo-like scale (mean 1500, sd 120)."""
|
| 75 |
+
return rng.gauss(1500.0, 120.0)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _softmax3(a: float, b: float, c: float) -> tuple[float, float, float]:
|
| 79 |
+
import math
|
| 80 |
+
|
| 81 |
+
m = max(a, b, c)
|
| 82 |
+
ea, eb, ec = math.exp(a - m), math.exp(b - m), math.exp(c - m)
|
| 83 |
+
s = ea + eb + ec
|
| 84 |
+
return ea / s, eb / s, ec / s
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def generate_row(rng: random.Random) -> dict[str, float | int | str]:
|
| 88 |
+
"""Generate one synthetic match row: 25 features + outcome label."""
|
| 89 |
+
home_strength = _sample_team_strength(rng)
|
| 90 |
+
away_strength = _sample_team_strength(rng)
|
| 91 |
+
home_advantage = 65.0
|
| 92 |
+
|
| 93 |
+
diff = (home_strength + home_advantage) - away_strength
|
| 94 |
+
|
| 95 |
+
# Dixon-Coles-like probability triple derived from the latent diff,
|
| 96 |
+
# softened toward the empirical draw prior (mirrors the production
|
| 97 |
+
# dixon_coles.py + training.py EloTable.win_probability soft-draw
|
| 98 |
+
# carving logic, re-implemented independently here for synthetic data).
|
| 99 |
+
p_draw = max(0.08, _DRAW_PRIOR - 0.0003 * abs(diff))
|
| 100 |
+
remaining = 1.0 - p_draw
|
| 101 |
+
p_home_raw = 1.0 / (1.0 + 10 ** (-diff / 400))
|
| 102 |
+
p_home = p_home_raw * remaining
|
| 103 |
+
p_away = remaining - p_home
|
| 104 |
+
|
| 105 |
+
# Elo-flavoured triple: same latent diff, independent noise draw so it
|
| 106 |
+
# is correlated with but not identical to the Dixon-Coles triple
|
| 107 |
+
# (mirrors two independently-fit sub-models feeding one meta-learner).
|
| 108 |
+
elo_noise = rng.gauss(0.0, 15.0)
|
| 109 |
+
elo_diff = diff + elo_noise
|
| 110 |
+
elo_p_draw = max(0.08, _DRAW_PRIOR - 0.0003 * abs(elo_diff))
|
| 111 |
+
elo_remaining = 1.0 - elo_p_draw
|
| 112 |
+
elo_p_home_raw = 1.0 / (1.0 + 10 ** (-elo_diff / 400))
|
| 113 |
+
elo_p_home = elo_p_home_raw * elo_remaining
|
| 114 |
+
elo_p_away = elo_remaining - elo_p_home
|
| 115 |
+
|
| 116 |
+
# Market-implied triple: a devigged noisy blend of the two above,
|
| 117 |
+
# standing in for bookmaker consensus.
|
| 118 |
+
imp_home, imp_draw, imp_away = _softmax3(
|
| 119 |
+
(p_home + elo_p_home) / 2 + rng.gauss(0.0, 0.03),
|
| 120 |
+
(p_draw + elo_p_draw) / 2 + rng.gauss(0.0, 0.03),
|
| 121 |
+
(p_away + elo_p_away) / 2 + rng.gauss(0.0, 0.03),
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Rolling-form features, loosely correlated with latent strength.
|
| 125 |
+
home_form5_pts = max(0.0, min(15.0, 1.5 * 5 + (home_strength - 1500) / 60 + rng.gauss(0, 2.0)))
|
| 126 |
+
away_form5_pts = max(0.0, min(15.0, 1.5 * 5 + (away_strength - 1500) / 60 + rng.gauss(0, 2.0)))
|
| 127 |
+
home_form5_goals_for = max(0.0, 6.0 + (home_strength - 1500) / 100 + rng.gauss(0, 1.5))
|
| 128 |
+
away_form5_goals_for = max(0.0, 6.0 + (away_strength - 1500) / 100 + rng.gauss(0, 1.5))
|
| 129 |
+
home_form5_goals_against = max(0.0, 6.0 - (home_strength - 1500) / 100 + rng.gauss(0, 1.5))
|
| 130 |
+
away_form5_goals_against = max(0.0, 6.0 - (away_strength - 1500) / 100 + rng.gauss(0, 1.5))
|
| 131 |
+
home_form5_avg_xg = max(0.0, 1.3 + (home_strength - 1500) / 300 + rng.gauss(0, 0.25))
|
| 132 |
+
away_form5_avg_xg = max(0.0, 1.3 + (away_strength - 1500) / 300 + rng.gauss(0, 0.25))
|
| 133 |
+
home_form5_avg_xga = max(0.0, 1.3 - (home_strength - 1500) / 300 + rng.gauss(0, 0.25))
|
| 134 |
+
away_form5_avg_xga = max(0.0, 1.3 - (away_strength - 1500) / 300 + rng.gauss(0, 0.25))
|
| 135 |
+
|
| 136 |
+
home_days_rest = max(2.0, rng.gauss(7.0, 2.0))
|
| 137 |
+
away_days_rest = max(2.0, rng.gauss(7.0, 2.0))
|
| 138 |
+
|
| 139 |
+
h2h_home_win_rate_5 = min(1.0, max(0.0, 0.5 + (diff / 800) + rng.gauss(0, 0.1)))
|
| 140 |
+
h2h_avg_total_goals_5 = max(0.0, 2.5 + rng.gauss(0, 0.6))
|
| 141 |
+
|
| 142 |
+
home_lineup_rating = home_strength / 1500.0 - 1.0 + rng.gauss(0, 0.05)
|
| 143 |
+
away_lineup_rating = away_strength / 1500.0 - 1.0 + rng.gauss(0, 0.05)
|
| 144 |
+
|
| 145 |
+
# Draw outcome from the Dixon-Coles-like triple (ground truth label).
|
| 146 |
+
u = rng.random()
|
| 147 |
+
if u < p_home:
|
| 148 |
+
outcome = 0 # home
|
| 149 |
+
elif u < p_home + p_draw:
|
| 150 |
+
outcome = 1 # draw
|
| 151 |
+
else:
|
| 152 |
+
outcome = 2 # away
|
| 153 |
+
|
| 154 |
+
return {
|
| 155 |
+
"dc_p_home": p_home, "dc_p_draw": p_draw, "dc_p_away": p_away,
|
| 156 |
+
"elo_p_home": elo_p_home, "elo_p_draw": elo_p_draw, "elo_p_away": elo_p_away,
|
| 157 |
+
"imp_home": imp_home, "imp_draw": imp_draw, "imp_away": imp_away,
|
| 158 |
+
"home_form5_pts": home_form5_pts, "away_form5_pts": away_form5_pts,
|
| 159 |
+
"home_form5_goals_for": home_form5_goals_for, "away_form5_goals_for": away_form5_goals_for,
|
| 160 |
+
"home_form5_goals_against": home_form5_goals_against, "away_form5_goals_against": away_form5_goals_against,
|
| 161 |
+
"home_form5_avg_xg": home_form5_avg_xg, "away_form5_avg_xg": away_form5_avg_xg,
|
| 162 |
+
"home_form5_avg_xga": home_form5_avg_xga, "away_form5_avg_xga": away_form5_avg_xga,
|
| 163 |
+
"home_days_rest": home_days_rest, "away_days_rest": away_days_rest,
|
| 164 |
+
"h2h_home_win_rate_5": h2h_home_win_rate_5, "h2h_avg_total_goals_5": h2h_avg_total_goals_5,
|
| 165 |
+
"home_lineup_rating": home_lineup_rating, "away_lineup_rating": away_lineup_rating,
|
| 166 |
+
"outcome": outcome,
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def export_dataset(rows: int, seed: int, output: Path) -> None:
|
| 171 |
+
rng = random.Random(seed)
|
| 172 |
+
output.parent.mkdir(parents=True, exist_ok=True)
|
| 173 |
+
fieldnames = FEATURE_NAMES_V2 + ["outcome"]
|
| 174 |
+
with output.open("w", newline="", encoding="utf-8") as f:
|
| 175 |
+
writer = csv.DictWriter(f, fieldnames=fieldnames)
|
| 176 |
+
writer.writeheader()
|
| 177 |
+
for _ in range(rows):
|
| 178 |
+
writer.writerow(generate_row(rng))
|
| 179 |
+
print(f"Wrote {rows} synthetic rows to {output}")
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def _parse_args() -> argparse.Namespace:
|
| 183 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 184 |
+
parser.add_argument("--rows", type=int, default=4000)
|
| 185 |
+
parser.add_argument("--seed", type=int, default=42)
|
| 186 |
+
parser.add_argument("--output", type=Path, default=Path("data/derived_sample.csv"))
|
| 187 |
+
return parser.parse_args()
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def main() -> None:
|
| 191 |
+
args = _parse_args()
|
| 192 |
+
export_dataset(args.rows, args.seed, args.output)
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
if __name__ == "__main__":
|
| 196 |
+
main()
|