A Formula 1 prediction engine combining Monte Carlo simulation with Machine Learning for Grand Prix outcomes.
flowchart LR
subgraph Data["1. Telemetry & Regulations"]
A["FastF1 API Ingestion"] --> B["Clean Flying Laps\n(pick_quicklaps + IsAccurate)"]
B --> C["10-Feature Matrix\n(Grid, Quali Gap, Practice, Form, Track)"]
end
subgraph Model["2. Machine Learning & Simulation"]
C --> D["Random Forest Regressor\n(300 Trees, Max Depth 6, Classified Only)"]
D --> E["Vectorized Monte Carlo Engine\n(Circuit, Wet-Weather & Team DNF Calibration)"]
E --> F["Points Withholding Rule\n(DNFs Excluded from Top-10 Points)"]
end
subgraph Serving["3. Dashboard & Explainability"]
F --> G["Streamlit Mission Control\n(Overview, Backtest & Dynamic Model Spec)"]
F --> H["TreeSHAP Explainability\n(Global Impact & Driver Waterfall)"]
end
- Grand Prix Centric: Organizes session data by Season and Round across Free Practice, Qualifying, Sprint Shootout, Sprint Race, and Grand Prix, plus circuit technical metadata (
length_km,corners,downforce,overtaking_difficulty,tire_wear,type). - 10-Feature ML & Vectorized Monte Carlo Engine: Random Forest Regressor trained across 10 pre-race features (
grid_norm,quali_gap_norm,pace_norm,pace_consistency, lagged pre-race EWMAdriver_formandconstructor_form,overtaking_difficulty,is_street_circuit,has_practice_data,is_sprint) paired with a seeded, vectorized NumPy simulator calibrating variance by circuit overtaking difficulty andweather.rainfall, and DNF rates by constructor reliability. - Non-Finisher Points Withholding: Simulated DNFs are strictly excluded from top-10 points allocation, advancing classified finishers into vacated championship points positions.
- SHAP Explainability & Backtesting: Global feature sensitivity charts, per-driver Plotly SHAP waterfall charts (
Base Value-> feature pushes ->Predicted Pos), and an automatic Predicted vs. Actual backtest view when official race results exist. - Regulatory Rules Support: Full support for Sprint Weekends, dynamically handling 2022 sprint rules (Saturday sprint sets Sunday grid) versus 2023+ regulations (qualifying sets Sunday grid; shootout sets sprint grid), and 20 vs. 22 car knockout thresholds.
- Decoupled Architecture & Live Metadata Sync: Framework-agnostic domain and modeling logic separated from Streamlit presentation/caching adapters, with
scripts/train_historical_model.pyexportingdata/models/model_metadata.jsonso dashboard model specification cards stay synchronized with the latest training run.
The core ML engine (RandomForestRegressor, max_depth=6, min_samples_leaf=4, max_features=0.8) is trained on historical telemetry, qualifying gaps, rolling form, and circuit metadata from 2022 to the present across temporal out-of-time splits (Train: 2022–2024 [1,165 samples], Validation: 2025 [419 samples], Test: 2026 [348 samples]):
| Benchmark Metric | 2025 Validation (419 Samples) | 2026 Out-of-Time Test (348 Samples) | 2026 Baseline Comparison |
|---|---|---|---|
| Points Finish Accuracy (Top 10) | 79.7% (F1: 0.815) | 81.3% (F1: 0.836) | — |
| Podium Finish Accuracy (Top 3) | 91.4% (F1: 0.705) | 88.5% (F1: 0.643) | 82.8% Dummy Prior (+5.7%) |
| Mean Absolute Error (MAE) | 2.30 positions | 2.21 positions | 2.34 positions Grid-Only (-0.13 pos) |
| Root Mean Squared Error (RMSE) | 3.01 positions | 2.84 positions | — |
| Explained Variance ( |
0.662 | 0.703 | — |
Target labels during training are strictly restricted to classified race finishers to prevent mechanical retirement label corruption. Running scripts/train_historical_model.py exports these hyperparameters, partition counts, and evaluation metrics to data/models/model_metadata.json.
git clone https://github.com/BLShaw/f1-predictor.git
cd f1-predictorpip install -r requirements.txt
# Or editable install with development tools (pytest, ruff, bandit):
pip install -e ".[dev]"ruff check .
pytest -vstreamlit run app.py# Ingest historical session telemetry (2022 to present)
python scripts/download_historical_data.py
# Retrain model and evaluate temporal out-of-time splits
python scripts/train_historical_model.py
MIT License. See LICENSE for details.