"""export_dataset.py - generates the portable, DERIVED/synthetic sample dataset. This script does NOT read any live database, and it does NOT copy or transform raw rows from football-data.co.uk or any other third-party match-data provider. It generates a synthetic-but-realistic set of engineered match features and 1X2 outcome labels, distributed to match the empirical priors used in production (class balance ~46% home win / 27% draw / 27% away win, which mirrors publicly documented long-run baselines for the 5 European top-flight leagues the production pipeline covers). Why synthetic and not a real-data aggregate: - football-data.co.uk's commercial-use / redistribution terms for this project were flagged as unverified in the ingestion code itself (ml-service/app/ingestion/datasets/football_results_2018_2026.py, LICENSE = "Verificare commercial use con football-data.co.uk"). - The other live-odds third-party source integrated elsewhere in the monorepo pipeline is explicitly non-redistributable in any form (its ingestion config flags it `is_public_redistribution_allowed=False`) and no row from it is ever touched by this export script. - Production Postgres (sport_intelligence.fixture) is never read from a public repo context. - Generating a synthetic sample sidesteps the licensing question entirely: no third-party row, raw or aggregated, is shipped. Only the feature SCHEMA (column names/semantics) is derived from the real pipeline (app/sport_intelligence/features/db_extractor.py FEATURE_NAMES_V2), which is this project's own original engineering work, not third-party data. The 25 features generated here match FEATURE_NAMES_V2 from app/sport_intelligence/features/db_extractor.py: dc_p_home, dc_p_draw, dc_p_away, elo_p_home, elo_p_draw, elo_p_away, imp_home, imp_draw, imp_away, home_form5_pts, away_form5_pts, home_form5_goals_for, away_form5_goals_for, home_form5_goals_against, away_form5_goals_against, home_form5_avg_xg, away_form5_avg_xg, home_form5_avg_xga, away_form5_avg_xga, home_days_rest, away_days_rest, h2h_home_win_rate_5, h2h_avg_total_goals_5, home_lineup_rating, away_lineup_rating Usage: python export_dataset.py --rows 4000 --seed 42 --output data/derived_sample.csv """ from __future__ import annotations import argparse import csv import random from pathlib import Path FEATURE_NAMES_V2 = [ "dc_p_home", "dc_p_draw", "dc_p_away", "elo_p_home", "elo_p_draw", "elo_p_away", "imp_home", "imp_draw", "imp_away", "home_form5_pts", "away_form5_pts", "home_form5_goals_for", "away_form5_goals_for", "home_form5_goals_against", "away_form5_goals_against", "home_form5_avg_xg", "away_form5_avg_xg", "home_form5_avg_xga", "away_form5_avg_xga", "home_days_rest", "away_days_rest", "h2h_home_win_rate_5", "h2h_avg_total_goals_5", "home_lineup_rating", "away_lineup_rating", ] # Empirical long-run class prior for the 5 covered leagues (documented in # DATA_PROVENANCE.md). NOT derived from a specific third-party file - used # only to shape the synthetic label distribution realistically. _HOME_WIN_PRIOR = 0.46 _DRAW_PRIOR = 0.27 # away win = remainder def _sample_team_strength(rng: random.Random) -> float: """Latent team strength on an Elo-like scale (mean 1500, sd 120).""" return rng.gauss(1500.0, 120.0) def _softmax3(a: float, b: float, c: float) -> tuple[float, float, float]: import math m = max(a, b, c) ea, eb, ec = math.exp(a - m), math.exp(b - m), math.exp(c - m) s = ea + eb + ec return ea / s, eb / s, ec / s def generate_row(rng: random.Random) -> dict[str, float | int | str]: """Generate one synthetic match row: 25 features + outcome label.""" home_strength = _sample_team_strength(rng) away_strength = _sample_team_strength(rng) home_advantage = 65.0 diff = (home_strength + home_advantage) - away_strength # Dixon-Coles-like probability triple derived from the latent diff, # softened toward the empirical draw prior (mirrors the production # dixon_coles.py + training.py EloTable.win_probability soft-draw # carving logic, re-implemented independently here for synthetic data). p_draw = max(0.08, _DRAW_PRIOR - 0.0003 * abs(diff)) remaining = 1.0 - p_draw p_home_raw = 1.0 / (1.0 + 10 ** (-diff / 400)) p_home = p_home_raw * remaining p_away = remaining - p_home # Elo-flavoured triple: same latent diff, independent noise draw so it # is correlated with but not identical to the Dixon-Coles triple # (mirrors two independently-fit sub-models feeding one meta-learner). elo_noise = rng.gauss(0.0, 15.0) elo_diff = diff + elo_noise elo_p_draw = max(0.08, _DRAW_PRIOR - 0.0003 * abs(elo_diff)) elo_remaining = 1.0 - elo_p_draw elo_p_home_raw = 1.0 / (1.0 + 10 ** (-elo_diff / 400)) elo_p_home = elo_p_home_raw * elo_remaining elo_p_away = elo_remaining - elo_p_home # Market-implied triple: a devigged noisy blend of the two above, # standing in for bookmaker consensus. imp_home, imp_draw, imp_away = _softmax3( (p_home + elo_p_home) / 2 + rng.gauss(0.0, 0.03), (p_draw + elo_p_draw) / 2 + rng.gauss(0.0, 0.03), (p_away + elo_p_away) / 2 + rng.gauss(0.0, 0.03), ) # Rolling-form features, loosely correlated with latent strength. home_form5_pts = max(0.0, min(15.0, 1.5 * 5 + (home_strength - 1500) / 60 + rng.gauss(0, 2.0))) away_form5_pts = max(0.0, min(15.0, 1.5 * 5 + (away_strength - 1500) / 60 + rng.gauss(0, 2.0))) home_form5_goals_for = max(0.0, 6.0 + (home_strength - 1500) / 100 + rng.gauss(0, 1.5)) away_form5_goals_for = max(0.0, 6.0 + (away_strength - 1500) / 100 + rng.gauss(0, 1.5)) home_form5_goals_against = max(0.0, 6.0 - (home_strength - 1500) / 100 + rng.gauss(0, 1.5)) away_form5_goals_against = max(0.0, 6.0 - (away_strength - 1500) / 100 + rng.gauss(0, 1.5)) home_form5_avg_xg = max(0.0, 1.3 + (home_strength - 1500) / 300 + rng.gauss(0, 0.25)) away_form5_avg_xg = max(0.0, 1.3 + (away_strength - 1500) / 300 + rng.gauss(0, 0.25)) home_form5_avg_xga = max(0.0, 1.3 - (home_strength - 1500) / 300 + rng.gauss(0, 0.25)) away_form5_avg_xga = max(0.0, 1.3 - (away_strength - 1500) / 300 + rng.gauss(0, 0.25)) home_days_rest = max(2.0, rng.gauss(7.0, 2.0)) away_days_rest = max(2.0, rng.gauss(7.0, 2.0)) h2h_home_win_rate_5 = min(1.0, max(0.0, 0.5 + (diff / 800) + rng.gauss(0, 0.1))) h2h_avg_total_goals_5 = max(0.0, 2.5 + rng.gauss(0, 0.6)) home_lineup_rating = home_strength / 1500.0 - 1.0 + rng.gauss(0, 0.05) away_lineup_rating = away_strength / 1500.0 - 1.0 + rng.gauss(0, 0.05) # Draw outcome from the Dixon-Coles-like triple (ground truth label). u = rng.random() if u < p_home: outcome = 0 # home elif u < p_home + p_draw: outcome = 1 # draw else: outcome = 2 # away return { "dc_p_home": p_home, "dc_p_draw": p_draw, "dc_p_away": p_away, "elo_p_home": elo_p_home, "elo_p_draw": elo_p_draw, "elo_p_away": elo_p_away, "imp_home": imp_home, "imp_draw": imp_draw, "imp_away": imp_away, "home_form5_pts": home_form5_pts, "away_form5_pts": away_form5_pts, "home_form5_goals_for": home_form5_goals_for, "away_form5_goals_for": away_form5_goals_for, "home_form5_goals_against": home_form5_goals_against, "away_form5_goals_against": away_form5_goals_against, "home_form5_avg_xg": home_form5_avg_xg, "away_form5_avg_xg": away_form5_avg_xg, "home_form5_avg_xga": home_form5_avg_xga, "away_form5_avg_xga": away_form5_avg_xga, "home_days_rest": home_days_rest, "away_days_rest": away_days_rest, "h2h_home_win_rate_5": h2h_home_win_rate_5, "h2h_avg_total_goals_5": h2h_avg_total_goals_5, "home_lineup_rating": home_lineup_rating, "away_lineup_rating": away_lineup_rating, "outcome": outcome, } def export_dataset(rows: int, seed: int, output: Path) -> None: rng = random.Random(seed) output.parent.mkdir(parents=True, exist_ok=True) fieldnames = FEATURE_NAMES_V2 + ["outcome"] with output.open("w", newline="", encoding="utf-8") as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() for _ in range(rows): writer.writerow(generate_row(rng)) print(f"Wrote {rows} synthetic rows to {output}") def _parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--rows", type=int, default=4000) parser.add_argument("--seed", type=int, default=42) parser.add_argument("--output", type=Path, default=Path("data/derived_sample.csv")) return parser.parse_args() def main() -> None: args = _parse_args() export_dataset(args.rows, args.seed, args.output) if __name__ == "__main__": main()