How martineastwood/penaltyblog works
Purpose and Package Surface
penaltyblog is a Python library for football analytics: ingesting event/match data, modelling scorelines and outcomes, deriving betting/ratings outputs, and visualization. The public package entry point is penaltyblog/__init__.py; domain APIs are exposed through module-level __init__.py files, notably penaltyblog/models/__init__.py, penaltyblog/matchflow/__init__.py, and penaltyblog/betting/__init__.py.
Data Ingestion and MatchFlow
MatchFlow is the core data-processing subsystem for nested football JSON. It represents lazy pipelines, compiles/optimizes them, then executes streaming transforms rather than requiring full datasets in memory.
• Flow construction and orchestration: penaltyblog/matchflow/flow.py, penaltyblog/matchflow/executor.py
• Query/filter/group/aggregate stages: penaltyblog/matchflow/query.py, penaltyblog/matchflow/group.py, penaltyblog/matchflow/aggregates.py
• Source and transform boundaries: penaltyblog/matchflow/steps/source.py, penaltyblog/matchflow/steps/transform.py
Provider Integrations
Provider-specific adapters isolate external schemas and transport from the generic MatchFlow pipeline. StatsBomb and Opta data can be sourced directly, normalized through provider mappings/parsers, and then passed into generic operations.
• StatsBomb and Opta adapters: penaltyblog/matchflow/contrib/statsbomb.py, penaltyblog/matchflow/contrib/opta.py
• Opta API client, endpoint definitions, and pagination: penaltyblog/matchflow/steps/opta/client.py, penaltyblog/matchflow/steps/opta/endpoints.py, penaltyblog/matchflow/steps/opta/paginator.py
• Public-data scraping is separate from pipeline providers: penaltyblog/scrapers/fbref.py, penaltyblog/scrapers/understat.py, penaltyblog/scrapers/footballdata.py
Outcome and Bayesian Modelling
Goal models fit match results and produce score/outcome probability structures for downstream prediction and betting. Shared model abstractions separate fitting/prediction contracts from distribution-specific implementations.
• Base contracts and probability grid: penaltyblog/models/base_model.py, penaltyblog/models/football_probability_grid.py
• Frequentist goal models: penaltyblog/models/poisson.py, penaltyblog/models/dixon_coles.py, penaltyblog/models/bivariate_poisson.py
• Bayesian and hierarchical paths: penaltyblog/models/bayesian_goal_model.py, penaltyblog/models/hierarchical_bayesian_goal_model.py, penaltyblog/bayes/sampler_api.py
High-Performance Numerical Layer
Performance-critical likelihoods, gradients, probability calculations, metrics, and Bayesian sampling are implemented in Cython, with Python-facing modules and type stubs around them. This is a compiled-extension boundary, not an external compute service.
• Model kernels: penaltyblog/models/gradients.pyx, penaltyblog/models/probabilities.pyx, penaltyblog/models/loss.pyx
• Bayesian sampler/likelihood kernels: penaltyblog/bayes/sampler.pyx, penaltyblog/bayes/likelihood.pyx
• Metric kernel: penaltyblog/metrics/metrics.pyx
Decision, Evaluation, and Ratings
Probabilities feed betting utilities for odds conversion, implied probabilities, arbitrage/value detection, Kelly sizing, and account-based backtests. Independent rating algorithms estimate team strength from results; scoring metrics evaluate probabilistic forecasts.
• Betting and implied-probability layers: penaltyblog/betting/odds.py, penaltyblog/betting/arbitrage.py, penaltyblog/implied/implied.py
• Backtest lifecycle/account state: penaltyblog/backtest/backtest.py, penaltyblog/backtest/account.py, penaltyblog/backtest/context.py
• Ratings and forecast metrics: penaltyblog/ratings/elo.py, penaltyblog/ratings/massey.py, penaltyblog/metrics/rps.py
Specialized Analytics and Presentation
Expected Threat consumes event data with a bundled pretrained artifact; visualization supports pitch and flow-oriented output. FPL is a separate fantasy optimization capability.
• xT model, schema, and artifact IO: penaltyblog/xt/model.py, penaltyblog/xt/schema.py, penaltyblog/xt/pretrained.py
• Visualization: penaltyblog/viz/pitch.py, penaltyblog/viz/flow_support.py
• Fantasy subsystem: penaltyblog/fpl/fpl.py
Delivery and Documentation
The project packages Python plus Cython extensions through pyproject.toml and setup.py, publishes to PyPI, and builds documentation with Read the Docs configuration. CI validates tests, coverage, and publication.
• Build/package definitions: pyproject.toml, setup.py
• CI and publishing: .github/workflows/test.yml, .github/workflows/publish.yml
• Documentation configuration: docs/conf.py, .readthedocs.yml
Main entry points: flow.py Flow compiler & executor · poisson.py Frequentist goal models · source.py Source & transform boundary · gradients.pyx Cython numerical kernels
Components 22
Ingestion & MatchFlow
Lazy streaming processing for football data.
- MatchFlow API
penaltyblog/matchflow/__init__.py - Flow compiler & executor · Builds, optimizes, and executes streaming flows.
penaltyblog/matchflow/flow.py - Query & aggregation stages
penaltyblog/matchflow/query.py - Source & transform boundary
penaltyblog/matchflow/steps/source.py - StatsBomb & Opta adapters · Normalizes provider schemas for MatchFlow.
penaltyblog/matchflow/contrib/statsbomb.py - Opta API client · Provides endpoints and pagination for Opta data.
penaltyblog/matchflow/steps/opta/client.py - Public-data scrapers · Separate FBref, Understat, and FootballData ingestion paths.
penaltyblog/scrapers/fbref.py
Outcome Modelling
Scoreline and probability models.
- Models API & contracts · Shared fit/predict contracts and probability grid.
penaltyblog/models/__init__.py - Frequentist goal models · Poisson-family models estimate match score distributions.
penaltyblog/models/poisson.py - Bayesian goal models · Bayesian and hierarchical scoreline modelling.
penaltyblog/models/bayesian_goal_model.py - Cython numerical kernels · Likelihoods, gradients, probabilities, metrics, and sampling kernels.
penaltyblog/models/gradients.pyx
Decision & Evaluation
Probability consumers and result analysis.
- Betting & implied probabilities · Converts odds and identifies arbitrage or value.
penaltyblog/betting/odds.py - Backtest & account · Runs lifecycle simulations with account state.
penaltyblog/backtest/backtest.py - Team ratings · Elo and Massey estimate team strength from results.
penaltyblog/ratings/elo.py - Forecast metrics · Scores probabilistic forecasts.
penaltyblog/metrics/rps.py
Specialized Analytics
Event analytics, visualization, and fantasy.
- Expected Threat · Applies an xT model with bundled pretrained artifacts.
penaltyblog/xt/model.py - Pitch & flow visualization
penaltyblog/viz/pitch.py - Fantasy optimization · Independent FPL capability.
penaltyblog/fpl/fpl.py
Delivery
Packaging, CI, and documentation.
- Python/Cython packaging · Builds Python package and compiled extensions.
pyproject.toml - CI & PyPI publishing · Validates, covers, and publishes releases.
.github/workflows/test.yml - Documentation build
docs/conf.py
Other components
- Public package API · Exports domain APIs from the library entry point.
penaltyblog/__init__.py
Connections 23
- Public package API → MatchFlow API: exports
- Public package API → Models API & contracts: exports
- MatchFlow API → Flow compiler & executor: constructs
- Flow compiler & executor → Query & aggregation stages: compiles stages
- Flow compiler & executor → Source & transform boundary: executes streaming inputs
- StatsBomb & Opta adapters → Source & transform boundary: normalizes into
- Opta API client → StatsBomb & Opta adapters: supplies Opta data
- Public-data scrapers → Source & transform boundary: provides data
- Models API & contracts → Frequentist goal models: contracts
- Models API & contracts → Bayesian goal models: contracts
- Frequentist goal models → Cython numerical kernels: uses
- Bayesian goal models → Cython numerical kernels: uses sampling kernels
- Frequentist goal models → Betting & implied probabilities: outcome probabilities
- Bayesian goal models → Betting & implied probabilities: outcome probabilities
- Betting & implied probabilities → Backtest & account: strategies evaluated by
- Frequentist goal models → Forecast metrics: forecasts scored by
- Cython numerical kernels → Forecast metrics: accelerates
- Source & transform boundary → Expected Threat: event data
- Expected Threat → Pitch & flow visualization: visualized as
- Flow compiler & executor → Pitch & flow visualization: flow output
- Python/Cython packaging → Cython numerical kernels: compiles
- CI & PyPI publishing → Python/Cython packaging: tests and publishes
- Documentation build → Public package API: documents