How py-econometrics/duckreg works
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: estimators.py Fit core · __init__.py Public API · duckreg.py DuckRegression · demean.py Transforms
Components 20
Python package
Core library code and public API
- Public API · Thin export surface for the package
duckreg/__init__.py - DuckRegression · General path that compresses unique regressor combinations and fits on aggregated observations
duckreg/duckreg.py - Mundlak · Panel-data estimator using cohort/time structure and transformed regressors
duckreg/duckreg.py - Event study · Event-study variant built on the Mundlak-style compression path
duckreg/duckreg.py - Double demean · Removes unit and time effects via double demeaning before aggregation
duckreg/demean.py - Fit core · Shared fitting logic and weighted least squares style estimation
duckreg/estimators.py - Transforms · Fixed-effect removal and data transformation helpers
duckreg/demean.py - Regularized · Specialized regularized or penalized regression path
duckreg/regularized.py
Documentation
Generated docs and notebooks
Tests
Regression and numerical validation
- DML tests · Coverage for decompressed and compressed estimator behavior
tests/test_dml.py - Fitter tests · Checks fit internals and numerical behavior
tests/test_fitter.py - Analytic tests · Analytic comparisons for inference and estimator outputs
tests/test_dml_analytic.py
Benchmarks
Performance measurement and summaries
- Benchmarks · Benchmark runners and performance summaries
benchmarks/bench.py
CI / release
Automation workflows
- CI workflows · Automated testing and docs publication
.github/workflows/ci.yml
Other components
- DuckDB · Embedded analytical database used for compression and aggregation
- Compression · Reduces raw rows to grouped summary tables and frequency weights
- Weighted fit · Fits models on compressed observations using weights
- Robust SE · Derives robust standard errors from sufficient statistics
- Cluster bootstrap · Clustered inference via resampling rather than full covariance computation
Connections 26
- 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