Skip to main content

duckreg: very fast out-of-memory regressions on SQL backends

python package to run stratified/saturated regressions out-of-memory through Ibis. DuckDB remains the default local backend for backwards compatibility, and the same estimators can run against any Ibis SQL backend that supports the generated aggregation queries. R users, check out Grant McDermott's port of this package.

The package provides a simple interface to run regressions on very large datasets that do not fit in memory by reducing the data inside the database to a set of summary statistics and then running weighted least squares with frequency weights. Robust standard errors are computed from sufficient statistics, while clustered standard errors are computed using the cluster bootstrap. Methodological details and benchmarks are provided in this paper. See examples in notebooks/introduction.ipynb.

  • install
pip install duckreg
  • dev install (preferably in a venv) with
uv pip install -e '.[test]'

or install from git with uv pip install git+https://github.com/py-econometrics/duckreg.git.

By default, legacy DuckDB paths still work:

from duckreg import DuckRegression

model = DuckRegression(
    db_name="large_dataset.db",
    table_name="data",
    formula="Y ~ D + X",
    cluster_col="cluster_id",
    seed=42,
)
model.fit()

For a remote database, create an Ibis backend and pass it through connection. For example, with Databricks:

import ibis
from duckreg import DuckRegression

con = ibis.databricks.connect(
    server_hostname="...",
    http_path="...",
    access_token="...",
    catalog="main",
    schema="analytics",
)

model = DuckRegression(
    db_name=None,
    connection=con,
    table_name="large_experiment_table",
    formula="Y ~ D + X",
    cluster_col="cluster_id",
    seed=42,
    n_bootstraps=0,
)
model.fit()

Currently supports the following regression specifications:

  1. DuckRegression: general linear regression, which compresses the data to y averages stratified by all unique values of the x variables
  2. DuckMundlak: One- or Two-Way Mundlak regression, which compresses the data to the following RHS and avoids the need to incorporate unit (and time FEs)

$$ y \sim 1, w, \bar{w}_{i, .}, \bar{w}_{., t} $$

  1. DuckDoubleDemeaning: Double demeaning regression, which compresses the data to y averages by all values of $w$ after demeaning. This also eliminates unit and time FEs

$$ y \sim (W_{it} - \bar{w}_{i, .} - \bar{w}_{., t} + \bar{w}_{., .}) $$

  1. DuckMundlakEventStudy: Two-way mundlak with dynamic treatment effects. This incorporates treatment-cohort FEs ($\psi_i$), time-period FEs ($\gamma_t$) and dynamic treatment effects $\tau_k$ given by cohort X time interactions.

$$ y \sim \psi_i + \gamma_t + \sum_{k=1}^{T} \tau_{k} D_i 1(t = k) $$

All the above regressions are run in compressed fashion through the configured Ibis backend. Formula-level fixed effects are not part of DuckRegression; use the panel-specific DuckMundlak or DuckDoubleDemeaning estimators for fixed-effect style designs.

Please cite the following paper if you use duckreg in your research:

@misc{lal2024largescalelongitudinalexperiments,
      title={Large Scale Longitudinal Experiments: Estimation and Inference}, 
      author={Apoorva Lal and Alexander Fischer and Matthew Wardrop},
      year={2024},
      eprint={2410.09952},
      archivePrefix={arXiv},
      primaryClass={econ.EM},
      url={https://arxiv.org/abs/2410.09952}, 
}

references:

methods:

libraries:

Metadata

Release files for duckreg 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for duckreg 0.4.1
File Size Uploaded
duckreg-0.4.1.tar.gz 35.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for duckreg 0.4.1
File Interpreter ABI Platform
duckreg-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 63.9 kB

Release files / duckreg-0.4.1.tar.gz

Download URL duckreg-0.4.1.tar.gz
Size 35.6 kB
Tags Source
SHA-256 checksum
How to use checksums
00e27fa42a6feca50ef385f7ede26ef9181a707682ece5953d2fb7a07fb8add0
BLAKE2b-256 checksum
How to use checksums
32e3637d794eb2aa13d3c04ce17a36cb631ed58c71b2bdeb0935b3370687908b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Release files / duckreg-0.4.1-py3-none-any.whl

Download URL duckreg-0.4.1-py3-none-any.whl
Size 28.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bd2b4d05142a9355383d734ad124b6efa17843b4476d2cdca25cb632c300aaa
BLAKE2b-256 checksum
How to use checksums
ae1da7d6f0dd2cc73d9279bea42d21f002b4defd43b608580f9e9499f34a4a12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page