Skip to main content

Fast econometric regressions for Python — OLS, IV, panel, probit/logit with robust and clustered SEs, built on Polars and Rust

Project description

polars_reg

Fast econometric regressions for Python, built on Polars and Rust. Covers OLS, IV, panel, and limited-dependent-variable models with robust and clustered standard errors — validated against Stata and R to 5+ decimal places. Also accepts pandas DataFrames.

The computational backend is written in Rust (via PyO3), with parallel demeaning and sandwich estimators powered by Rayon. On fixed-effects and clustered models, polars_reg matches or beats R/fixest and is 2–8× faster than statsmodels, pyfixest, and linearmodels.

Features

Regression Estimators

  • OLS / WLS — analytic weights (weights=) and frequency weights (fweights=)
  • High-dimensional fixed effects — reghdfe-style absorption via iterative demeaning
  • 2SLS / IV with first-stage F-statistics and weak instrument diagnostics
  • LIML — limited information maximum likelihood
  • GMM-IV — two-step efficient GMM with Hansen J test
  • Panel: fixed effects (within), random effects (Swamy-Arora GLS), first-difference
  • Dynamic panel GMM: Arellano-Bond (difference GMM) and Blundell-Bond (system GMM)
  • Probit / Logit — MLE with marginal effects and odds ratios
  • Quantile regression — median and arbitrary quantiles via IRLS
  • PPML — Poisson pseudo-maximum likelihood for count/gravity models

Standard Errors

  • Robust — HC0, HC1 (Stata's robust), HC2, HC3
  • Clustered — one-way and multi-way (Cameron-Gelbach-Miller)
  • HAC — Newey-West for time series
  • Driscoll-Kraay — robust to cross-sectional dependence in panels
  • Bootstrap — pairs bootstrap and wild cluster bootstrap (Webb 6-point)

Convenience Features

  • GroupBy regression — run any estimator per group (e.g., per stock or industry)
  • regtable — side-by-side regression tables (estout/esttab-style) with LaTeX and HTML export
  • Coefficient plots and added-variable plots via Altair
  • Diagnostics — Wald test, Hausman test (FE vs RE), Kleibergen-Paap, Stock-Yogo weak IV
  • Formula API with interaction terms (x1*x2, x1:x2) and indicator expansion (i.group)

Validation

  • Stata equivalenceto_stata() generates the matching Stata command for any specification
  • R equivalenceto_r() generates the matching fixest/lm call
  • Automated comparisoncompare_stata() and compare_r() run the command and diff coefficients

Performance

Benchmarks

Wall-clock time across dataset sizes (1K–1M rows), compared to statsmodels, pyfixest, linearmodels, R/fixest, and Stata:

  • Plain OLS: statsmodels is faster below ~100K rows due to lower call overhead
  • FE, IV, and clustered models: polars_reg is consistently faster than other Python packages, especially at smaller N
  • At large N: performance converges with R/fixest — the difference between the two is roughly negligible

Reproduce with python benchmarks/generate_chart.py (requires R with fixest; Stata optional).

Installation

pip install polars_reg

Quick Start

import polars as pl
import polars_reg as pr

df = pl.read_csv("data.csv")

# OLS with robust standard errors
result = pr.ols("y ~ x1 + x2 + x3", data=df, vcov="HC1")
print(result.summary())

# OLS with absorbed fixed effects and clustered SEs (like Stata's reghdfe)
result = pr.ols("y ~ x1 + x2 | firm_id + year_id", data=df, cluster=["firm_id"])

# IV / 2SLS
result = pr.iv2sls("y ~ x_exog || x_endog ~ z1 + z2", data=df)

# Panel fixed effects
result = pr.panel_fe("y ~ x1 + x2", data=df, entity="firm_id", time="year_id")

# GroupBy: run regression per industry
grp = pr.groupby_reg(pr.ols, "y ~ x1 + x2", df, group_by="industry")
grp.coef_table()  # stacked Polars DataFrame

# Side-by-side comparison table
pr.regtable(m1, m2, m3, labels=["OLS", "Robust", "FE"])

# Probit / Logit
result = pr.logit("y_binary ~ x1 + x2", data=df, cluster="firm_id")
pr.marginal_effects(result, at="mean")
pr.odds_ratios(result)

# Quantile regression (median)
result = pr.quantreg("y ~ x1 + x2", data=df, tau=0.5)

# Dynamic panel GMM
result = pr.panel_ab("y ~ x1", data=df, entity="firm_id", time="year_id")
result = pr.panel_sys_gmm("y ~ x1", data=df, entity="firm_id", time="year_id")

# PPML (Poisson / gravity model)
result = pr.ppml("count ~ x1 + x2", data=df, cluster=["firm_id"])

# Coefficient plot (interactive Altair chart)
result.coefplot()
pr.coefplot(m1, m2, m3, labels=["OLS", "IV", "FE"])

# Added-variable (partial regression) plot
result.avplot("x1")

# Out-of-sample prediction
preds = result.predict(new_df)
intervals = result.predict_interval(new_df, alpha=0.05)  # fit, se, lower, upper

# Access results
result.coefficients  # coefficient vector
result.se            # standard errors
result.tstat         # t-statistics
result.pvalue        # p-values
result.confint()     # confidence intervals
result.coef_table()  # Polars DataFrame
result.wald_test(R)  # Wald test for linear restrictions
result.predict(new_df)   # out-of-sample predictions
result.coefplot()        # coefficient plot
result.avplot()          # added-variable plots

Formula Syntax

Formula Meaning Stata R
y ~ x1 + x2 OLS reg y x1 x2 lm(y ~ x1 + x2)
y ~ x1 + x2 - 1 No intercept reg y x1 x2, noconstant lm(y ~ x1 + x2 - 1)
y ~ x1 | fe1 + fe2 Absorbed FE reghdfe y x1, absorb(fe1 fe2) feols(y ~ x1 | fe1 + fe2)
y ~ x1 || x_end ~ z1 + z2 IV/2SLS ivregress 2sls y x1 (x_end = z1 z2) feols(y ~ x1 | 0 | x_end ~ z1 + z2)
y ~ x1 | fe1 | x_end ~ z1 IV + FE ivreghdfe y x1 (x_end = z1), absorb(fe1) feols(y ~ x1 | fe1 | x_end ~ z1)
y ~ x1*x2 Full factorial reg y c.x1##c.x2 lm(y ~ x1 * x2)
y ~ x1:x2 Interaction only reg y c.x1#c.x2 lm(y ~ x1:x2)
y ~ i.group + x1 Indicator dummies reg y i.group x1 lm(y ~ factor(group) + x1)
y ~ i.group*x1 Indicator × continuous reg y i.group#c.x1 lm(y ~ factor(group) * x1)

Estimators

Function Description
ols() OLS/WLS with optional FE absorption
iv2sls() Two-stage least squares
liml() Limited information maximum likelihood
gmm_iv() Two-step efficient GMM
panel_fe() Panel fixed effects (within)
panel_re() Panel random effects (Swamy-Arora GLS)
panel_fd() Panel first-difference
panel_ab() Arellano-Bond dynamic panel GMM
panel_sys_gmm() Blundell-Bond system GMM
probit() Probit MLE
logit() Logit MLE
quantreg() Quantile regression (IRLS + bootstrap)
ppml() Poisson pseudo-maximum likelihood
coefplot() Coefficient plot with CIs (Altair)
groupby_reg() Run any estimator per group
regtable() Side-by-side regression table
marginal_effects() Probit/logit marginal effects
odds_ratios() Logit odds ratios with delta-method SEs
hausman_test() Hausman specification test (FE vs RE)

Standard Error Options

  • vcov="iid" — homoskedastic (default)
  • vcov="HC1" — heteroskedasticity-robust (Stata's robust)
  • vcov="HC0", "HC2", "HC3" — other HC variants
  • cluster=["firm_id"] — one-way clustered
  • cluster=["firm_id", "year_id"] — two-way clustered (Cameron-Gelbach-Miller)
  • vcov="NW" — Newey-West HAC (requires time=)
  • vcov="DK" — Driscoll-Kraay (requires time=)
  • vcov="bootstrap" — pairs bootstrap
  • vcov="wildboot" — wild cluster bootstrap (requires cluster=)

Stata / R Equivalence

Generate equivalent code to verify results in Stata or R:

# Stata code
print(pr.to_stata("ols", "y ~ x1 + x2 | firm_id", cluster=["firm_id"]))
# → reghdfe y x1 x2, absorb(firm_id) vce(cluster firm_id)

# R code
print(pr.to_r("ols", "y ~ x1 + x2 | firm_id", cluster=["firm_id"]))
# → library(fixest)
#   model <- feols(y ~ x1 + x2 | firm_id, data=df, vcov=~firm_id)

Documentation

  • Showcase notebook — full tour of all features (rendered PDF)
  • API reference — generate locally with uv run pdoc polars_reg --docformat google (serves at http://localhost:8080), or build static HTML with uv run pdoc polars_reg -o docs/api --docformat google

Requirements

  • Python >= 3.11
  • Polars >= 1.0
  • NumPy >= 1.24
  • SciPy >= 1.10
  • pandas (optional — for pandas DataFrame input)
  • Altair (optional — for plotting)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polars_reg-0.1.3.tar.gz (1.3 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polars_reg-0.1.3-cp313-cp313-win_amd64.whl (405.9 kB view details)

Uploaded CPython 3.13Windows x86-64

polars_reg-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (573.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

polars_reg-0.1.3-cp313-cp313-macosx_11_0_arm64.whl (487.2 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

polars_reg-0.1.3-cp313-cp313-macosx_10_12_x86_64.whl (517.8 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

polars_reg-0.1.3-cp312-cp312-win_amd64.whl (406.4 kB view details)

Uploaded CPython 3.12Windows x86-64

polars_reg-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (573.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

polars_reg-0.1.3-cp312-cp312-macosx_11_0_arm64.whl (487.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

polars_reg-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl (517.9 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

polars_reg-0.1.3-cp311-cp311-win_amd64.whl (410.6 kB view details)

Uploaded CPython 3.11Windows x86-64

polars_reg-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (573.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

polars_reg-0.1.3-cp311-cp311-macosx_11_0_arm64.whl (497.0 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

polars_reg-0.1.3-cp311-cp311-macosx_10_12_x86_64.whl (522.4 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

Details for the file polars_reg-0.1.3.tar.gz.

File metadata

  • Download URL: polars_reg-0.1.3.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_reg-0.1.3.tar.gz
Algorithm Hash digest
SHA256 87b8455d3850403ae384dd74b959e8abca72ad4f43d728f92b2ff92c43454a75
MD5 183b79a0f205a2a0fab7b1a627b9e8cd
BLAKE2b-256 a656c6b7b1486893ca2c4ca8fc0829987a6792ad5313e30b4a523baa8f409545

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3.tar.gz:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: polars_reg-0.1.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 405.9 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_reg-0.1.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e9a77c0a00b3926cdaa34b2a758e04f6d574d27ca51a1c974f0eb90ee78f8348
MD5 015b04aa740a1692221d330699edc5f9
BLAKE2b-256 1b5424e79d3caa21c94d0f27281de5343c0d962e31175a433660d293d12b77b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp313-cp313-win_amd64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 544438b35522c04fdd7ff1399ef63093b02b487992ca0c4252c30385eb8be512
MD5 4cbed269c5a657507f105bcf439cdc79
BLAKE2b-256 6f314346e95bfcf155704a3581e1ce4c07ddcbc9b3d5d0b515eb843fdbb214de

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d32d7fb6622aa6e1b4dad669de9cc9da6724282c3a5ed6853cf7be60c38a41f5
MD5 9bbf947a1a77c08ba464dc1f6544e7b3
BLAKE2b-256 d9c6eac7ee3894951d1623c347d051862a94716aa2ad530f0200f00a5a5aff5e

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.3-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a58603d24fb23894573e5d1074d64a50e43e984a6edcf5772c642c977f3d461e
MD5 ff2d7228a6db32b0a6c3e958ee61b65c
BLAKE2b-256 c8dd2d8eceb9c2522309e999dfd7b268664f51f2a61bee4a78251dd2ad9424bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp313-cp313-macosx_10_12_x86_64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: polars_reg-0.1.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 406.4 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_reg-0.1.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ee2bc53027256c8b81ded3fb27413a34c65663391ca1e4a0f718d0f3f60e4118
MD5 22a58f4aef5c614992ee941d5832d836
BLAKE2b-256 8a5a6ff4c883b6206b994c61bc1ea0acd89c113a47d72878505cfa4ce2b80cad

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp312-cp312-win_amd64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 20b5f459ae47e655509dbe12d57a5279b1ece846823b7d5672b45053aae0d29b
MD5 71bc4f37407f3e340cc81924e1640dab
BLAKE2b-256 ece8a8db2848210fe065f1e58f3679e29ac55cebce16baa6bc330bb193c89beb

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3f1b4e8c36d6e7dcbbcf547597745cf1fbfa609e3d23e7a7fdedd26db0d6d0d5
MD5 ce1c69269c136aaebe767dd4afa54735
BLAKE2b-256 9c1b5dd27225f301b5dfda7e670cce514648b9cf319b5ea42b214334df4d066b

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f14a2e008c5b53da933ff855a7a1f8cfd435dded3ce84036005101940324068a
MD5 f07140421ae9e25892a9a3db9d611092
BLAKE2b-256 cf75241a442be94412622391f4625946f4427ec0b2fa4931c2f31e3329d1b26f

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: polars_reg-0.1.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 410.6 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for polars_reg-0.1.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b57857493e32071e484d79d91e4b6170946724e8ba9e938895be2a6cb485b9d9
MD5 efcbefc94a20658e7324fc3d30e7170c
BLAKE2b-256 3a5eca12f387a56141a127e607bc3f1dd074df4948b6dca5d564b5fb6edd724c

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp311-cp311-win_amd64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 18b2c4a7c7e2549910617ac959f50a9283a1ad38fd3f7db4c8c548d2ed664b58
MD5 5f447c8c67e154e6bdff44f26a9f4198
BLAKE2b-256 7a56f07b9686abb82198d1c401fc1a5647a86344da35020c5d1a74ca9dc3b7ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 97dd31394088df75f7128cbdf808ff5d0067618849068ba4f5d513c77d4c8080
MD5 7506eff810ed16a46ae1063a411bc80f
BLAKE2b-256 70645074077ad9b2ab150a133d2a7bc3b9ba39416c4e688cb36e89cf5753c58e

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_reg-0.1.3-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.3-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ffb29a6702a1fe88b0c8412206dbaf27840ae0c92e72817e4771c95058da5bf2
MD5 1bd335196996c969e5aa02ef7d44b56b
BLAKE2b-256 89d8290491e82bc5de001005808548e6e1af7e24a162fe8fcd5cf0872f859bc9

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.3-cp311-cp311-macosx_10_12_x86_64.whl:

Publisher: ci.yml on AlexSulliMora/polars_reg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page