# martineastwood/penaltyblog architecture

> Architecture diagram of the GitHub repository martineastwood/penaltyblog, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/martineastwood/penaltyblog
- Repository: https://github.com/martineastwood/penaltyblog

## Overview

#### 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: [`penaltyblog/matchflow/flow.py`](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/flow.py) (Flow compiler & executor), [`penaltyblog/models/poisson.py`](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/poisson.py) (Frequentist goal models), [`penaltyblog/matchflow/steps/source.py`](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/steps/source.py) (Source & transform boundary), [`penaltyblog/models/gradients.pyx`](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/gradients.pyx) (Cython numerical kernels)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingestion["Ingestion &amp; MatchFlow"]
  node_matchflow_api["MatchFlow API<br/>pipeline API<br/>[__init__.py]"]
  node_flow{{"Flow compiler &amp; executor<br/>lazy pipeline runtime<br/>[flow.py]"}}
  node_stages["Query &amp; aggregation stages<br/>pipeline stages<br/>[query.py]"]
  node_sources["Source &amp; transform boundary<br/>pipeline steps<br/>[source.py]"]
  node_providers["StatsBomb &amp; Opta adapters<br/>provider adapters<br/>[statsbomb.py]"]
  node_opta_client["Opta API client<br/>API transport<br/>[client.py]"]
  node_scrapers["Public-data scrapers<br/>[fbref.py]"]
end

subgraph group_modelling["Outcome Modelling"]
  node_models_api["Models API &amp; contracts<br/>model API<br/>[__init__.py]"]
  node_goal_models["Frequentist goal models<br/>scoreline models<br/>[poisson.py]"]
  node_bayesian_models["Bayesian goal models<br/>Bayesian models"]
  node_native_kernels{{"Cython numerical kernels<br/>compiled extensions<br/>[gradients.pyx]"}}
end

subgraph group_decisions["Decision &amp; Evaluation"]
  node_betting["Betting &amp; implied probabilities<br/>decision utilities<br/>[odds.py]"]
  node_backtest["Backtest &amp; account<br/>strategy evaluation<br/>[backtest.py]"]
  node_ratings["Team ratings<br/>rating algorithms<br/>[elo.py]"]
  node_metrics["Forecast metrics<br/>evaluation metrics<br/>[rps.py]"]
end

subgraph group_analytics["Specialized Analytics"]
  node_xt["Expected Threat<br/>event analytics<br/>[model.py]"]
  node_visualization["Pitch &amp; flow visualization<br/>[pitch.py]"]
  node_fpl["Fantasy optimization<br/>fantasy analytics<br/>[fpl.py]"]
end

subgraph group_delivery["Delivery"]
  node_packaging["Python/Cython packaging<br/>build system<br/>[pyproject.toml]"]
  node_ci["CI &amp; PyPI publishing<br/>automation<br/>[test.yml]"]
  node_docs["Documentation build<br/>Read the Docs<br/>[conf.py]"]
end

node_package["Public package API<br/>Python package<br/>[__init__.py]"]

node_package -->|"exports"| node_matchflow_api
node_package -->|"exports"| node_models_api
node_matchflow_api -->|"constructs"| node_flow
node_flow -->|"compiles stages"| node_stages
node_flow -->|"executes streaming inputs"| node_sources
node_providers -->|"normalizes into"| node_sources
node_opta_client -->|"supplies Opta data"| node_providers
node_scrapers -.->|"provides data"| node_sources
node_models_api -->|"contracts"| node_goal_models
node_models_api -->|"contracts"| node_bayesian_models
node_goal_models -->|"uses"| node_native_kernels
node_bayesian_models -->|"uses sampling kernels"| node_native_kernels
node_goal_models -->|"outcome probabilities"| node_betting
node_bayesian_models -->|"outcome probabilities"| node_betting
node_betting -->|"strategies evaluated by"| node_backtest
node_goal_models -->|"forecasts scored by"| node_metrics
node_native_kernels -->|"accelerates"| node_metrics
node_sources -->|"event data"| node_xt
node_xt -->|"visualized as"| node_visualization
node_flow -.->|"flow output"| node_visualization
node_packaging -->|"compiles"| node_native_kernels
node_ci -->|"tests and publishes"| node_packaging
node_docs -.->|"documents"| node_package

click node_package "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/__init__.py"
click node_matchflow_api "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/__init__.py"
click node_flow "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/flow.py"
click node_stages "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/query.py"
click node_sources "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/steps/source.py"
click node_providers "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/contrib/statsbomb.py"
click node_opta_client "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/steps/opta/client.py"
click node_scrapers "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/scrapers/fbref.py"
click node_models_api "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/__init__.py"
click node_goal_models "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/poisson.py"
click node_bayesian_models "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/bayesian_goal_model.py"
click node_native_kernels "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/gradients.pyx"
click node_betting "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/betting/odds.py"
click node_backtest "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/backtest/backtest.py"
click node_ratings "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/ratings/elo.py"
click node_metrics "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/metrics/rps.py"
click node_xt "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/xt/model.py"
click node_visualization "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/viz/pitch.py"
click node_fpl "https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/fpl/fpl.py"
click node_packaging "https://github.com/martineastwood/penaltyblog/blob/master/pyproject.toml"
click node_ci "https://github.com/martineastwood/penaltyblog/blob/master/.github/workflows/test.yml"
click node_docs "https://github.com/martineastwood/penaltyblog/blob/master/docs/conf.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_matchflow_api,node_flow,node_stages,node_sources,node_providers,node_opta_client,node_scrapers toneBlue
class node_models_api,node_goal_models,node_bayesian_models,node_native_kernels toneAmber
class node_betting,node_backtest,node_ratings,node_metrics toneMint
class node_xt,node_visualization,node_fpl toneRose
class node_packaging,node_ci,node_docs toneIndigo
class node_package toneNeutral
```

## Components

### Ingestion & MatchFlow

Lazy streaming processing for football data.

- [MatchFlow API](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/__init__.py): pipeline API · `penaltyblog/matchflow/__init__.py`
- [Flow compiler & executor](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/flow.py): lazy pipeline runtime · Builds, optimizes, and executes streaming flows. · `penaltyblog/matchflow/flow.py`
- [Query & aggregation stages](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/query.py): pipeline stages · `penaltyblog/matchflow/query.py`
- [Source & transform boundary](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/steps/source.py): pipeline steps · `penaltyblog/matchflow/steps/source.py`
- [StatsBomb & Opta adapters](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/contrib/statsbomb.py): provider adapters · Normalizes provider schemas for MatchFlow. · `penaltyblog/matchflow/contrib/statsbomb.py`
- [Opta API client](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/matchflow/steps/opta/client.py): API transport · Provides endpoints and pagination for Opta data. · `penaltyblog/matchflow/steps/opta/client.py`
- [Public-data scrapers](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/scrapers/fbref.py): scrapers · Separate FBref, Understat, and FootballData ingestion paths. · `penaltyblog/scrapers/fbref.py`

### Outcome Modelling

Scoreline and probability models.

- [Models API & contracts](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/__init__.py): model API · Shared fit/predict contracts and probability grid. · `penaltyblog/models/__init__.py`
- [Frequentist goal models](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/poisson.py): scoreline models · Poisson-family models estimate match score distributions. · `penaltyblog/models/poisson.py`
- [Bayesian goal models](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/bayesian_goal_model.py): Bayesian models · Bayesian and hierarchical scoreline modelling. · `penaltyblog/models/bayesian_goal_model.py`
- [Cython numerical kernels](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/models/gradients.pyx): compiled extensions · Likelihoods, gradients, probabilities, metrics, and sampling kernels. · `penaltyblog/models/gradients.pyx`

### Decision & Evaluation

Probability consumers and result analysis.

- [Betting & implied probabilities](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/betting/odds.py): decision utilities · Converts odds and identifies arbitrage or value. · `penaltyblog/betting/odds.py`
- [Backtest & account](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/backtest/backtest.py): strategy evaluation · Runs lifecycle simulations with account state. · `penaltyblog/backtest/backtest.py`
- [Team ratings](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/ratings/elo.py): rating algorithms · Elo and Massey estimate team strength from results. · `penaltyblog/ratings/elo.py`
- [Forecast metrics](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/metrics/rps.py): evaluation metrics · Scores probabilistic forecasts. · `penaltyblog/metrics/rps.py`

### Specialized Analytics

Event analytics, visualization, and fantasy.

- [Expected Threat](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/xt/model.py): event analytics · Applies an xT model with bundled pretrained artifacts. · `penaltyblog/xt/model.py`
- [Pitch & flow visualization](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/viz/pitch.py): visualization · `penaltyblog/viz/pitch.py`
- [Fantasy optimization](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/fpl/fpl.py): fantasy analytics · Independent FPL capability. · `penaltyblog/fpl/fpl.py`

### Delivery

Packaging, CI, and documentation.

- [Python/Cython packaging](https://github.com/martineastwood/penaltyblog/blob/master/pyproject.toml): build system · Builds Python package and compiled extensions. · `pyproject.toml`
- [CI & PyPI publishing](https://github.com/martineastwood/penaltyblog/blob/master/.github/workflows/test.yml): automation · Validates, covers, and publishes releases. · `.github/workflows/test.yml`
- [Documentation build](https://github.com/martineastwood/penaltyblog/blob/master/docs/conf.py): Read the Docs · `docs/conf.py`

### Other components

- [Public package API](https://github.com/martineastwood/penaltyblog/blob/master/penaltyblog/__init__.py): Python package · Exports domain APIs from the library entry point. · `penaltyblog/__init__.py`

## Connections

- 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
