What an expected goals model actually measures
Each shot is a Bernoulli trial: it either becomes a goal (1) or not (0). Logistic regression models the probability of the 1 as a function of features:
logit(p) = β₀ + β₁·distance + β₂·angle + β₃·is_header + ...
Fitting the model gives coefficients; applying the logistic function gives a probability per shot. Summing those probabilities over a player's shots gives their xG total.
Two things worth being explicit about, because they get glossed over constantly:
- Accuracy is the wrong metric. A model that predicts 0.08 for every shot is "accurate" (non-goal) most of the time and useless. What matters is calibration (predicted probabilities match observed frequencies) and discrimination (higher-probability shots go in more often than lower-probability ones).
- The model is a population average, not a player rating. It tells you how often shots like this are scored historically, not how likely this specific finisher was to score.
With that framing, here's the project layout:
xg-model/
├── requirements.txt
├── src/
│ ├── fetch_shots.py
│ ├── features.py
│ ├── fit_model.py
│ └── calibration.py
└── tests/
└── test_features.py
Pulling and cleaning shots from StatsBomb open data
StatsBomb releases a substantial body of match data under a non-commercial licence; the open-data repository includes every men's World Cup since 1966, among other competitions. The statsbombpy package wraps the downloads. Versioning note: the code below works with statsbombpy 1.x, which returns pandas DataFrames.
statsbombpy==1.14.1
pandas==2.2.3
numpy==2.1.3
statsmodels==0.14.4
scikit-learn==1.5.2
matplotlib==3.9.2
pyarrow==18.0.0
pytest==8.3.3
"""Download 2018 World Cup shots from StatsBomb open data."""
from pathlib import Path
import pandas as pd
from statsbombpy import sb
DATA_DIR = Path("data")
def main() -> None:
DATA_DIR.mkdir(exist_ok=True)
competitions = sb.competitions()
wc = competitions[
(competitions["competition_name"] == "World Cup")
& (competitions["season_name"] == "2018")
]
if wc.empty:
raise RuntimeError("2018 World Cup not found in competition list")
match_ids = sb.matches(
competition_id=int(wc.iloc[0]["competition_id"]),
season_id=int(wc.iloc[0]["season_id"]),
)["match_id"].tolist()
print(f"Found {len(match_ids)} matches")
frames = []
for mid in match_ids:
events = sb.events(match_id=mid)
shots = events[events["type.name"] == "Shot"].copy()
keep = [
"match_id",
"minute",
"shot.statsbomb_xg",
"location",
"shot.body_part.name",
"shot.type.name",
"shot.outcome.name",
]
frames.append(shots[keep])
df = pd.concat(frames, ignore_index=True)
df = df.dropna(subset=["location"])
df.to_parquet(DATA_DIR / "shots.parquet", index=False)
print(f"Saved {len(df)} shots to data/shots.parquet")
if __name__ == "__main__":
main()
A few practical warnings. First, statsbombpy prints a warning about missing columns per competition; that's noise here. Second, the shot.statsbomb_xg column is StatsBomb's own model output — we keep it purely as a sanity check to compare against later, never as a training feature. Training on someone else's xG and calling it your own is the classic beginner mistake.
Feature engineering for the expected goals model: distance, angle, body part, game state
The two geometric features that do most of the work are distance to goal centre and the angle subtended by the goal mouth (7.32 m wide) from the shot location. StatsBomb pitch coordinates are in metres with origin at the top-left, so the goal centre sits at (105, 34) on a 105 × 68 pitch.
"""Engineer features for the xG model."""
import numpy as np
import pandas as pd
GOAL_CENTRE = np.array([105.0, 34.0])
GOAL_HALF_WIDTH = 7.32 / 2
def shot_angle(loc: np.ndarray) -> float:
"""Angle subtended by the goal mouth from the shot location, in [0, pi]."""
left = np.array([105.0, 34.0 - GOAL_HALF_WIDTH])
right = np.array([105.0, 34.0 + GOAL_HALF_WIDTH])
v1, v2 = left - loc, right - loc
cos_a = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
return float(np.arccos(np.clip(cos_a, -1.0, 1.0)))
def build_features(df: pd.DataFrame) -> pd.DataFrame:
loc = np.vstack(df["location"].to_numpy())
dist = np.linalg.norm(loc - GOAL_CENTRE, axis=1)
angle = np.array([shot_angle(row) for row in loc])
out = pd.DataFrame(
{
"distance": dist,
"angle": angle,
"is_header": (df["shot.body_part.name"] == "Head").astype(int),
"is_set_piece": (
df["shot.type.name"].isin(["Free Kick", "Corner"])
).astype(int),
"goal": (df["shot.outcome.name"] == "Goal").astype(int),
}
)
return out.reset_index(drop=True)
Why these features?
- Distance is the strongest single predictor; its coefficient will be clearly negative.
- Angle captures the difference between a shot from the penalty spot (wide angle, high quality) and one from the same distance but on the byline (narrow angle, poor quality). Distance alone cannot distinguish those.
- is_header flags headers, which convert at a materially lower rate than shots with the foot. StatsBomb's published xG methodology documentation lists body part among their core features, consistent with this.
- is_set_piece is a crude game-state proxy covering direct free kicks and corners, which convert at a low rate despite often being in "decent" locations. Penalties are deliberately excluded: they're near-certain, almost uniform-quality shots, and folding them into the set-piece flag would drag that coefficient toward zero for no informational gain. (If you want to model penalties at all, treat them as their own category.) "Kick Off" shots are effectively absent from the data, so they're excluded too.
Deliberately omitted: goalkeeper position, defensive pressure, and pattern of play beyond the set-piece flag. StatsBomb's production model uses freeze-frame data for the former two. Freeze frames are available in the open repository, but with a caveat: full 360 freeze-frame coverage exists only for the subset of 2018 World Cup matches included in the StatsBomb 360 dataset — check the 360 data directory per match before assuming a shot has one. Honesty about that gap matters more than pretending the model is production-grade.
Fitting the model and reading the coefficients
Fitting and interpretation
"""Fit a logistic regression xG model with statsmodels."""
import sys
from pathlib import Path
import pandas as pd
import statsmodels.api as sm
sys.path.insert(0, str(Path(__file__).resolve().parent))
from features import build_features
DATA_DIR = Path(__file__).resolve().parents[1] / "data"
def main() -> None:
df = pd.read_parquet(DATA_DIR / "shots.parquet")
features = build_features(df)
X = features.drop(columns="goal")
y = features["goal"]
X_const = sm.add_constant(X)
model = sm.Logit(y, X_const).fit(disp=0)
print(model.summary())
preds = model.predict(X_const)
out = df[["match_id", "minute", "shot.statsbomb_xg"]].copy()
out["xg_ours"] = preds.to_numpy()
out.to_parquet(DATA_DIR / "xg_predictions.parquet", index=False)
print(f"\nSaved predictions. Mean xG: {preds.mean():.4f}, "
f"goal rate: {y.mean():.4f}")
if __name__ == "__main__":
main()
Run it and you'll see output shaped like this. These numbers are illustrative — they show the typical sign and magnitude pattern you should expect, not guaranteed values. Run the pipeline yourself and read your own table; don't quote these coefficients as if they were real output:
Coef. Std.Err z P>|z|
const 0.412 0.318 1.296 0.195
distance -0.152 0.011 -13.818 0.000
angle 1.847 0.289 6.390 0.000
is_header -0.894 0.292 -3.062 0.002
is_set_piece -1.102 0.311 -3.543 0.000
Read these as directional facts, not gospel:
- distance ≈ −0.15: each additional metre multiplies the odds of scoring by
exp(−0.152) ≈ 0.86, a 14% drop, holding angle and body part fixed. That's a strong effect, and it's why long-range shots accumulate almost no xG.
- angle ≈ +1.85: wider angles (larger subtended angle) sharply increase scoring odds, exactly as expected.
- is_header ≈ −0.89: headers convert at roughly
exp(−0.89) ≈ 0.41 times the odds of otherwise-identical foot shots.
- is_set_piece ≈ −1.10: direct free kicks and corners are poor-value shots even from plausible locations — a known weakness of naive "shots from zone X" analysis.
The Pseudo R-squared statsmodels prints (McFadden) will look low, around 0.2–0.3. That's normal for a Bernoulli model and not a red flag. Judge the model by calibration, which is the next section's job.
One caveat on interpretation: "holding everything else fixed" is doing real work here. Distance and angle are strongly correlated; if you dropped one, the other's coefficient would absorb some of its effect. Don't quote marginal effects from a correlated-feature model without checking that.
Calibration: reliability curves and Brier score
"""Reliability curve and proper scoring rules for the fitted model."""
import sys
from pathlib import Path
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.calibration import calibration_curve
from sklearn.metrics import brier_score_loss, log_loss
sys.path.insert(0, str(Path(__file__).resolve().parent))
from features import build_features
DATA_DIR = Path(__file__).resolve().parents[1] / "data"
def main() -> None:
preds = pd.read_parquet(DATA_DIR / "xg_predictions.parquet")
df = pd.read_parquet(DATA_DIR / "shots.parquet")
y_true = build_features(df)["goal"].to_numpy()
p = preds["xg_ours"].to_numpy()
prob_true, prob_pred = calibration_curve(y_true, p, n_bins=10, strategy="quantile")
brier = brier_score_loss(y_true, p)
ll = log_loss(y_true, p)
print(f"Brier score: {brier:.4f}")
print(f"Log loss: {ll:.4f}")
fig, ax = plt.subplots(figsize=(6, 6))
ax.plot([0, 1], [0, 1], "k--", label="perfect calibration")
ax.plot(prob_pred, prob_true, "o-", label="our model")
ax.set_xlabel("Mean predicted xG (bin)")
ax.set_ylabel("Observed goal rate (bin)")
ax.set_title("Reliability curve — 2018 WC shots")
ax.legend()
fig.savefig(DATA_DIR / "reliability_curve.png", dpi=150)
print("Saved data/reliability_curve.png")
if __name__ == "__main__":
main()
The reliability curve bins shots by predicted probability and plots the observed goal rate per bin. A well-calibrated model hugs the diagonal: shots predicted at 0.10 go in about 10% of the time. With ~2,000 shots (2018 World Cup had 2,458 shots per StatsBomb's open data) split into deciles, each bin holds roughly 200 shots — enough that bins in the 0.05–0.30 range are reasonably stable, though the highest bins will be thin and noisy. Be skeptical of the top-right of the curve for exactly that reason.
The Brier score is the mean squared error of the probabilities (lower is better, 0 is perfect). For context on this dataset, a labelled illustrative example: a model predicting the base goal rate (≈ 0.115 for this shot set) for every shot scores a Brier of roughly p(1−p) ≈ 0.102. Our fitted model should land meaningfully below that — if it doesn't, the features aren't adding information and something is wrong. Log loss is the same idea in cross-entropy form and punishes confident wrong predictions harder.
Comparing our predictions column-by-column with shot.statsbomb_xg is a useful sanity check: the two should correlate strongly (Spearman ρ well above 0.8) without matching exactly, since StatsBomb's model uses features we don't have.
Known limitations and next steps
Be honest about what this expected goals model is: a baseline, not a scouting tool.
- No positional context. Keeper position and pressure are the two biggest upgrades available, and both live in StatsBomb freeze-frame data (available in the open repository, but full 360 freeze frames only for the subset of 2018 World Cup matches in the 360 dataset). Adding keeper distance to shot alone typically improves calibration noticeably.
- Small, single-competition sample. ~2,500 shots is thin for a 5-parameter model with interactions. Pooling multiple seasons stabilises the fit; the fetch script generalises to any competition ID.
- Non-linearity. Distance's effect isn't linear in the log-odds. Common fixes: splines on distance, a log-distance term, or binning. Check the reliability curve per distance band before trusting the fit.
- Independence assumption. Shots within a match are correlated (same defence, same keeper). A mixed-effects or GEE model handles this; for most analytic uses the bias is modest, but know it's there.
Concrete experiments to run next:
- Add freeze-frame features (keeper distance, number of defenders between ball and goal) and re-check the reliability curve.
- Fit on five seasons, test on 2018, and report out-of-sample Brier — in-sample calibration always flatters.
- Replace raw distance with a spline and inspect whether long-range calibration improves.
- Write the XGBoost baseline and compare on the same bins. Gradient boosting will usually win on discrimination but can be worse calibrated out of the box and much harder to interpret — measure both with the same Brier/log-loss harness before declaring a winner. For a broader menu of datasets to extend this with, see other public football data sources.
The core discipline to keep: never report an accuracy figure for an xG model. Report calibration plots and proper scoring rules, and say clearly what your model does not know — that's the difference between a model and a number.
Finally, the tests. They're deliberately offline: they exercise the geometry and feature logic with a tiny fixture, so they run in CI without downloading anything.
"""Offline unit tests for feature engineering."""
import sys
from pathlib import Path
import numpy as np
import pandas as pd
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from features import build_features, shot_angle
def test_angle_bounds() -> None:
for loc in ([50, 34], [104, 34], [90, 5], [105, 34]):
a = shot_angle(np.array(loc, dtype=float))
assert 0.0 <= a <= np.pi
def test_angle_pen_spot_wider_than_byline() -> None:
pen_spot = shot_angle(np.array([94.0, 34.0]))
byline = shot_angle(np.array([94.0, 60.0]))
assert pen_spot > byline
def test_goal_centre_angle_is_pi() -> None:
assert shot_angle(np.array([105.0, 34.0])) == pytest.approx(np.pi)
@pytest.fixture
def tiny_df() -> pd.DataFrame:
return pd.DataFrame(
{
"match_id": [1, 2],
"minute": [10, 20],
"shot.statsbomb_xg": [0.05, 0.4],
"location": [[94.0, 34.0], [105.0, 34.0]],
"shot.body_part.name": ["Head", "Right Foot"],
"shot.type.name": ["Open Play", "Free Kick"],
"shot.outcome.name": ["Off Target", "Goal"],
}
)
def test_feature_frame_shape_and_no_nans(tiny_df: pd.DataFrame) -> None:
feats = build_features(tiny_df)
assert len(feats) == len(tiny_df)
assert not feats.isna().any().any()
def test_distance_non_negative_and_labels_correct(tiny_df: pd.DataFrame) -> None:
feats = build_features(tiny_df)
assert (feats["distance"] >= 0).all()
assert feats["goal"].tolist() == [0, 1]
assert feats["is_header"].tolist() == [1, 0]
assert feats["is_set_piece"].tolist() == [0, 1]
Run with pytest tests/ -q from the project root. Everything here is reproducible end to end: pinned dependencies, public data, an explicit metric harness, and tests that fail loudly if the geometry breaks.