# py-econometrics/duckreg architecture

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

- Interactive diagram: https://gitdiagram.com/py-econometrics/duckreg
- Repository: https://github.com/py-econometrics/duckreg

## Overview

1. This repository is a Python package centered on one architectural idea: run large regressions out of memory by pushing the heavy aggregation work into DuckDB, then fitting on compressed sufficient statistics instead of the full dataset.

2. The public surface is small and appears to be exposed through `duckreg/__init__.py`, with the core estimator types implemented in `duckreg/duckreg.py`, `duckreg/demean.py`, `duckreg/estimators.py`, and `duckreg/regularized.py`.

3. The main data flow is: user data in a tabular form enters a regression class, DuckDB is used to group/compress the data by the relevant strata or transformations, and the resulting summary table is handed to a weighted least squares-style fitting step.

4. `DuckRegression` is the generic path: it compresses by the unique combinations of regressors and fits a linear model on aggregated observations with frequency weights. This is the baseline “general linear regression” subsystem.

5. `DuckMundlak` and `DuckMundlakEventStudy` represent panel-data workflows. They use within/cohort/time structure to absorb unit and time fixed effects via compression and transformed regressors rather than explicit dummy expansion.

6. `DuckDoubleDemeaning` is a separate panel transformation path that removes unit and time effects through double demeaning before aggregation, again reducing the problem to a smaller compressed regression.

7. Inference is split by type: robust standard errors are derived from sufficient statistics, while clustered standard errors are handled through a cluster bootstrap. That boundary matters because the package avoids full-sample covariance computation except where the bootstrap requires resampling.

8. `duckreg/estimators.py` likely contains the lower-level estimator and fit logic shared across the public regression classes, while `duckreg/demean.py` encapsulates the transformation logic for fixed-effect removal. `duckreg/regularized.py` appears to hold a specialized or penalized regression variant.

9. The package is designed around DuckDB as the computational engine, so the main external runtime dependency is not a traditional model-fitting backend but an embedded analytical database used for fast aggregation over large datasets.

10. Testing is organized around regression correctness and numerical behavior: `tests/test_dml.py`, `tests/test_dml_analytic.py`, and `tests/test_fitter.py` suggest coverage for estimator outputs, analytic comparisons, and fitting internals. `tests/conftest.py` and `tests/utils.py` support shared fixtures and helpers.

11. The repo also has a strong validation and performance layer. `benchmarks/` contains benchmark scripts and summary tooling, indicating the authors measure the compression-plus-fit approach against alternatives and track performance regressions.

12. Documentation and examples are split between generated docs in `docs/` and executable notebooks in `notebooks/`. The notebooks appear to be the main place where methodological examples, variance estimation notes, and event-study examples are demonstrated.

13. CI and release hygiene are present via `.github/workflows/ci.yml` and `.github/workflows/docs.yml`, which suggests automated test runs and documentation publication are part of the maintenance pipeline.

14. Overall architecture: thin Python API at the top, estimator-specific transformation/compression modules in the middle, DuckDB as the query/compression engine, and a compact fitting/inference layer at the bottom. The key boundary is that raw data never needs to be fully materialized into a traditional in-memory regression design matrix.

Main entry points: [`duckreg/estimators.py`](https://github.com/py-econometrics/duckreg/blob/master/duckreg/estimators.py) (Fit core), [`duckreg/__init__.py`](https://github.com/py-econometrics/duckreg/blob/master/duckreg/__init__.py) (Public API), [`duckreg/duckreg.py`](https://github.com/py-econometrics/duckreg/blob/master/duckreg/duckreg.py) (DuckRegression), [`duckreg/demean.py`](https://github.com/py-econometrics/duckreg/blob/master/duckreg/demean.py) (Transforms)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_package["Python package"]
  node_api["Public API<br/>package entry<br/>[__init__.py]"]
  node_core_regression["DuckRegression<br/>linear reg<br/>[duckreg.py]"]
  node_panel_mundlak["Mundlak<br/>panel FE<br/>[duckreg.py]"]
  node_panel_event["Event study<br/>panel FE<br/>[duckreg.py]"]
  node_double_demean["Double demean<br/>panel transform<br/>[demean.py]"]
  node_estimator_core["Fit core<br/>estimator internals<br/>[estimators.py]"]
  node_transform_utils["Transforms<br/>demean helpers<br/>[demean.py]"]
  node_regularized["Regularized<br/>penalized reg<br/>[regularized.py]"]
end

subgraph group_docs["Documentation"]
  node_docs_site["Docs site<br/>documentation<br/>[index.html]"]
  node_notebooks["Notebooks<br/>examples<br/>[introduction.ipynb]"]
end

subgraph group_tests["Tests"]
  node_test_dml["DML tests<br/>test suite<br/>[test_dml.py]"]
  node_test_fitter["Fitter tests<br/>test suite<br/>[test_fitter.py]"]
  node_test_analytic["Analytic tests<br/>test suite"]
end

subgraph group_benchmarks["Benchmarks"]
  node_bench_suite["Benchmarks<br/>perf tools<br/>[bench.py]"]
end

subgraph group_ci["CI / release"]
  node_ci_workflows["CI workflows<br/>automation<br/>[ci.yml]"]
end

node_duckdb_engine[("DuckDB<br/>query engine")]
node_compression["Compression<br/>sufficient stats"]
node_wls_fit["Weighted fit<br/>least squares"]
node_robust_se{{"Robust SE<br/>inference"}}
node_cluster_bootstrap{{"Cluster bootstrap<br/>inference"}}

node_api -->|"exports"| node_core_regression
node_api -->|"exports"| node_double_demean
node_api -->|"exports"| node_regularized
node_core_regression -->|"aggregates via"| node_duckdb_engine
node_core_regression -->|"produces"| node_compression
node_compression -->|"feeds"| node_wls_fit
node_wls_fit -->|"implemented by"| node_estimator_core
node_panel_mundlak -->|"uses"| node_transform_utils
node_panel_event -->|"uses"| node_transform_utils
node_double_demean -->|"uses"| node_transform_utils
node_transform_utils -->|"queries via"| node_duckdb_engine
node_panel_mundlak -->|"compresses"| node_compression
node_panel_event -->|"compresses"| node_compression
node_regularized -->|"extends"| node_estimator_core
node_estimator_core -->|"computes"| node_robust_se
node_estimator_core -->|"falls back to"| node_cluster_bootstrap
node_bench_suite -->|"measures"| node_core_regression
node_bench_suite -->|"measures"| node_regularized
node_docs_site -->|"documents"| node_api
node_notebooks -->|"demonstrates"| node_panel_event
node_notebooks -->|"explains"| node_robust_se
node_test_dml -->|"verifies"| node_panel_mundlak
node_test_analytic -->|"verifies"| node_robust_se
node_test_fitter -->|"verifies"| node_estimator_core
node_ci_workflows -->|"runs"| node_test_dml
node_ci_workflows -->|"publishes"| node_docs_site

click node_api "https://github.com/py-econometrics/duckreg/blob/master/duckreg/__init__.py"
click node_core_regression "https://github.com/py-econometrics/duckreg/blob/master/duckreg/duckreg.py"
click node_panel_mundlak "https://github.com/py-econometrics/duckreg/blob/master/duckreg/duckreg.py"
click node_panel_event "https://github.com/py-econometrics/duckreg/blob/master/duckreg/duckreg.py"
click node_double_demean "https://github.com/py-econometrics/duckreg/blob/master/duckreg/demean.py"
click node_estimator_core "https://github.com/py-econometrics/duckreg/blob/master/duckreg/estimators.py"
click node_transform_utils "https://github.com/py-econometrics/duckreg/blob/master/duckreg/demean.py"
click node_regularized "https://github.com/py-econometrics/duckreg/blob/master/duckreg/regularized.py"
click node_bench_suite "https://github.com/py-econometrics/duckreg/blob/master/benchmarks/bench.py"
click node_docs_site "https://github.com/py-econometrics/duckreg/blob/master/docs/index.html"
click node_notebooks "https://github.com/py-econometrics/duckreg/blob/master/notebooks/introduction.ipynb"
click node_test_dml "https://github.com/py-econometrics/duckreg/blob/master/tests/test_dml.py"
click node_test_fitter "https://github.com/py-econometrics/duckreg/blob/master/tests/test_fitter.py"
click node_test_analytic "https://github.com/py-econometrics/duckreg/blob/master/tests/test_dml_analytic.py"
click node_ci_workflows "https://github.com/py-econometrics/duckreg/blob/master/.github/workflows/ci.yml"

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_api,node_core_regression,node_panel_mundlak,node_panel_event,node_double_demean,node_estimator_core,node_transform_utils,node_regularized toneBlue
class node_docs_site,node_notebooks toneAmber
class node_test_dml,node_test_fitter,node_test_analytic toneMint
class node_bench_suite toneRose
class node_ci_workflows toneIndigo
class node_duckdb_engine,node_compression,node_wls_fit,node_robust_se,node_cluster_bootstrap toneNeutral
```

## Components

### Python package

Core library code and public API

- [Public API](https://github.com/py-econometrics/duckreg/blob/master/duckreg/__init__.py): package entry · Thin export surface for the package · `duckreg/__init__.py`
- [DuckRegression](https://github.com/py-econometrics/duckreg/blob/master/duckreg/duckreg.py): linear reg · General path that compresses unique regressor combinations and fits on aggregated observations · `duckreg/duckreg.py`
- [Mundlak](https://github.com/py-econometrics/duckreg/blob/master/duckreg/duckreg.py): panel FE · Panel-data estimator using cohort/time structure and transformed regressors · `duckreg/duckreg.py`
- [Event study](https://github.com/py-econometrics/duckreg/blob/master/duckreg/duckreg.py): panel FE · Event-study variant built on the Mundlak-style compression path · `duckreg/duckreg.py`
- [Double demean](https://github.com/py-econometrics/duckreg/blob/master/duckreg/demean.py): panel transform · Removes unit and time effects via double demeaning before aggregation · `duckreg/demean.py`
- [Fit core](https://github.com/py-econometrics/duckreg/blob/master/duckreg/estimators.py): estimator internals · Shared fitting logic and weighted least squares style estimation · `duckreg/estimators.py`
- [Transforms](https://github.com/py-econometrics/duckreg/blob/master/duckreg/demean.py): demean helpers · Fixed-effect removal and data transformation helpers · `duckreg/demean.py`
- [Regularized](https://github.com/py-econometrics/duckreg/blob/master/duckreg/regularized.py): penalized reg · Specialized regularized or penalized regression path · `duckreg/regularized.py`

### Documentation

Generated docs and notebooks

- [Docs site](https://github.com/py-econometrics/duckreg/blob/master/docs/index.html): documentation · Generated API docs and site pages · `docs/index.html`
- [Notebooks](https://github.com/py-econometrics/duckreg/blob/master/notebooks/introduction.ipynb): examples · Executable examples and methodological notes · `notebooks/introduction.ipynb`

### Tests

Regression and numerical validation

- [DML tests](https://github.com/py-econometrics/duckreg/blob/master/tests/test_dml.py): test suite · Coverage for decompressed and compressed estimator behavior · `tests/test_dml.py`
- [Fitter tests](https://github.com/py-econometrics/duckreg/blob/master/tests/test_fitter.py): test suite · Checks fit internals and numerical behavior · `tests/test_fitter.py`
- [Analytic tests](https://github.com/py-econometrics/duckreg/blob/master/tests/test_dml_analytic.py): test suite · Analytic comparisons for inference and estimator outputs · `tests/test_dml_analytic.py`

### Benchmarks

Performance measurement and summaries

- [Benchmarks](https://github.com/py-econometrics/duckreg/blob/master/benchmarks/bench.py): perf tools · Benchmark runners and performance summaries · `benchmarks/bench.py`

### CI / release

Automation workflows

- [CI workflows](https://github.com/py-econometrics/duckreg/blob/master/.github/workflows/ci.yml): automation · Automated testing and docs publication · `.github/workflows/ci.yml`

### Other components

- **DuckDB**: query engine · Embedded analytical database used for compression and aggregation
- **Compression**: sufficient stats · Reduces raw rows to grouped summary tables and frequency weights
- **Weighted fit**: least squares · Fits models on compressed observations using weights
- **Robust SE**: inference · Derives robust standard errors from sufficient statistics
- **Cluster bootstrap**: inference · Clustered inference via resampling rather than full covariance computation

## Connections

- Public API → DuckRegression: exports · Public package surface exposes the main regression classes
- Public API → Double demean: exports · Public API also exposes panel transformation entry points
- Public API → Regularized: exports · Public API includes the specialized regularized variant
- DuckRegression → DuckDB: aggregates via · Uses DuckDB to group and compress raw tabular data
- DuckRegression → Compression: produces · Turns rows into grouped sufficient statistics
- Compression → Weighted fit: feeds · Compressed summaries are passed to the weighted fit step
- Weighted fit → Fit core: implemented by · Shared fitting logic lives in estimator internals
- Mundlak → Transforms: uses · Panel path relies on transformation helpers
- Event study → Transforms: uses · Event-study path reuses the same transformation machinery
- Double demean → Transforms: uses · Double demeaning is implemented through shared transform helpers
- Transforms → DuckDB: queries via · Transformations are computed through DuckDB-backed aggregation
- Mundlak → Compression: compresses · Mundlak-style workflows reduce panel structure before fitting
- Event study → Compression: compresses · Event-study workflows also fit on compressed summaries
- Regularized → Fit core: extends · Regularized regression builds on shared estimator logic
- Fit core → Robust SE: computes · Robust uncertainty estimates come from sufficient statistics
- Fit core → Cluster bootstrap: falls back to · Clustered standard errors are obtained by bootstrap resampling
- Benchmarks → DuckRegression: measures · Benchmarks compare compression-plus-fit performance
- Benchmarks → Regularized: measures · Benchmarking includes specialized regression variants
- Docs site → Public API: documents · Generated docs cover the public API and module internals
- Notebooks → Event study: demonstrates · Notebooks showcase methodological examples and event studies
- Notebooks → Robust SE: explains · Examples include variance and inference notes
- DML tests → Mundlak: verifies · Checks compressed panel estimator behavior
- Analytic tests → Robust SE: verifies · Compares analytic inference behavior
- Fitter tests → Fit core: verifies · Exercises fitting internals and numerical stability
- CI workflows → DML tests: runs · CI executes the test suite
- CI workflows → Docs site: publishes · Docs workflow builds and publishes documentation
