Pipeline

data engineering

A polyglot medallion pipeline that builds every model twice — in R and Python — and refuses to publish unless the two agree. This page is the portfolio README for engineers.

Data: 2023–2025 regular seasons · final (through 2025-09-28) · Pythagorean exponent 1.733 (fitted) · seed 777 where simulated

The pipeline

Statcast+ statsapiingest(Python)DuckDBbronzedbtstaging 6silver 2gold 19R + Pythonmodelsparitygatestaticsitebronze / silver / gold = medallion layers · widths not to scale
Statcast is sensor telemetry
Statcast isn't a spreadsheet — it's a distributed measurement system: Hawk-Eye optical arrays in 30 venues emitting 2.18M measurement events. So this is a measurement-system engineering problem, and the pipeline treats it as one: high-volume telemetry ingestion, cross-site normalization (park effects are per-venue sensor calibration differences), a mid-life instrument migration (Trackman radar → Hawk-Eye optical in 2020 — the historical-baseline shift familiar from any sensor swap), and late/missing/corrected readings (untracked pitches, suspended games, post-hoc scoring corrections). It's an analogy to instrumentation engineering, not streaming SCADA — and it's accurate.

Warehouse & testing

Warehouse tables
27
staging 6 · silver 2 · gold 19
dbt data tests
50passing
0 failing, 0 warning.
Parity gate
13/ 13
All three tiers pass.
Grain uniqueness
every fact has a uniqueness test on its declared grain — no silent fan-out.
Referential integrity
facts join to dimensions; failed joins get a placeholder + COALESCE, kept under 0.5%.
RE24 anchors
the run-expectancy matrix is asserted against published Tango reference values.
Volume
Statcast pitch counts reconcile with the official boxscore, directionally (never short).
Determinism
a rebuild-and-hash gate proves the gold feeds are byte-identical across rebuilds.

What the gold aggregates look like

A pipeline is only as good as the numbers that fall out of it. Here is a straight EDA plot of the gold team-season table — no model, just the aggregates the warehouse produces — as a sanity check that the joins, the run totals, and the grain are sound.

Actual vs. expected wins (90 team-seasons)
0.300.300.400.400.500.500.600.600.700.70expected win% (Pythagorean)actual win%
Each dot is one team-season: expected win% from run differential (Pythagorean) on the x-axis, actual win% on the y. Points hug the diagonal — run differential really does predict record — and the vertical scatter around it is the season-to-season luck the models on the science page try to separate from skill. A broken join or a double-counted game would show up here as points flying off the line; they don't.
Why this belongs on the engineering page: it is the cheapest, most honest data-quality test there is — plot the output and look at it. The dbt tests prove the grain and the keys; this shows the values are physically plausible.
Same data feeds baseline B1 (Pythagoras) on the data-science page, where the residual becomes the skill-vs-luck story.

The parity gate

A polyglot MLB analytics pipeline that builds every statistical model twice — once in R, once in Python — and refuses to ship unless the two languages agree.

This is dual-channel redundant validation — the same voting logic used on redundant safety-rated measurement channels: two independent implementations must agree before anything ships. R and Python never share memory — no rpy2, no reticulate. They exchange data only through files (DuckDB tables and flat Parquet feeds). Every model is implemented independently in both, and the build fails unless they agree at one of three tiers. If a single-language build is quietly wrong, the other usually isn't wrong the same way — the gate turns "looks plausible" into "two toolchains independently agree." Each model is also anchored by a known-answer test: parity proves R and Python agree; the KAT proves they agree on something true.
Exact
Coefficients / correlations within 1e-5
Why not exact: Deterministic fits should be identical
Label-invariant
PCA principal-component |corr|, cluster ARI, mixed-model BLUP corr
Why not exact: R and Python label clusters/components in different orders
Distributional
Deterministic inputs exact; simulated outputs within Monte-Carlo error
Why not exact: R's Mersenne-Twister and NumPy's PCG64 diverge even with identical seeds — matching seeds across languages is a known trap, so we never try
What a gate failure looks like
Game parity coef.win_pct_diff: R and Python disagree past tolerance
Game KAT: Brier 0.251 not < Pythag baseline 0.272
PARITY GATE FAILED — build aborts, nothing ships.
A real failure blocks publish. That is the point: a broken model can't reach the site.

War stories

Bugs worth remembering — the full postmortems are in DECISIONS.md.

jsonlite silently truncated to 4 significant figures
and spuriously failed exact-tier parity even though the math agreed. It was a *serialization* artifact, not a numeric one. Fix: write_json(..., digits = 12) in **every** R model.
The warehouse wasn't byte-deterministic
until run values were summed as scaled integers. DuckDB's parallel float summation is non-associative, so SUM(run_value) drifted by ~1e-15 across rebuilds and round() couldn't save boundary flips. Summing micro-runs AS BIGINT))`) makes the addition order-independent. A rebuild-and-hash gate guards it.
Suspended games share one `game_pk` across two dates
(24 cases): a game suspended on day *D* and finished on *D+1* appears under both, so a naive GROUP BY game_pk double-counts. Staging dedupes; the rolling-form window uses game_pk as a final tiebreaker to stay a *total* order (doubleheaders already break date-only ordering).
Spring training pollutes a date window
— and 2024's Seoul Series put regular-season games on the *same dates* as domestic spring games. The scope filter has to be game_type, not the calendar.
statsmodels MixedLM went singular
on the BABIP model (many one-observation groups drive the random-effect variance to zero); Nelder-Mead (gradient-free) converges where the analytic-gradient optimizers crash.

Operational notes

Reproducible & cheap
Fixed seeds; the static site is generated straight from the warehouse; a from-empty clone reproduces every published number to six decimals.
TCO ≈ $0: DuckDB + a static GitHub Pages site means no served database, no uptime burden, no bill. The tradeoff — no live data — is why the next phase adds a nightly refresh.
Source
Every stage runs through one CLI and exits non-zero on failure.