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.4.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.4-cp313-cp313-win_amd64.whl (406.2 kB view details)

Uploaded CPython 3.13Windows x86-64

polars_reg-0.1.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (573.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

polars_reg-0.1.4-cp313-cp313-macosx_11_0_arm64.whl (487.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

polars_reg-0.1.4-cp313-cp313-macosx_10_12_x86_64.whl (518.1 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

polars_reg-0.1.4-cp312-cp312-win_amd64.whl (406.7 kB view details)

Uploaded CPython 3.12Windows x86-64

polars_reg-0.1.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (573.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

polars_reg-0.1.4-cp312-cp312-macosx_11_0_arm64.whl (487.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

polars_reg-0.1.4-cp312-cp312-macosx_10_12_x86_64.whl (518.2 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

polars_reg-0.1.4-cp311-cp311-win_amd64.whl (410.9 kB view details)

Uploaded CPython 3.11Windows x86-64

polars_reg-0.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (573.7 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

polars_reg-0.1.4-cp311-cp311-macosx_11_0_arm64.whl (497.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

polars_reg-0.1.4-cp311-cp311-macosx_10_12_x86_64.whl (522.8 kB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: polars_reg-0.1.4.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.4.tar.gz
Algorithm Hash digest
SHA256 ca7f3715856df86b9896f6f0a7c7a9e7367e51ba83ad8348dfc3b66ba9fa860f
MD5 8329bf5a66bb9e257f4c7226e9cb832e
BLAKE2b-256 1b971c4af9b416fe1187190796f1960e04c32da0b2ec66312caf4d85a17b13cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4.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.4-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: polars_reg-0.1.4-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 406.2 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.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 0cf9075ebf96b2473f1cc2f66a2ac7613bb23e91fcf8fc61258476acc35f6477
MD5 7f9b9025a316223cd0ab1a8edaac18aa
BLAKE2b-256 f39ab3f6cc93755c61eba68ac08b7f8107b546d6ad32eae9704276df50bef1c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8f4e9b6ccdfee195202e0946c37b828ff0fdf4d8dc109c3799fb09e0fbec49c1
MD5 e2c83250881d21b7018c4abf397e5efc
BLAKE2b-256 aeaedc7bde21a14b97faeaee39c3596efeddb159172c6957c56affdc3f61c027

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9cfaf6bd218926f5e038ebb1c38b77b3df22ace8c7bc041d62f99c44b8f104d9
MD5 3a1290bf08fa350007a2c166cc795274
BLAKE2b-256 9c07bf5fd9abe6740e8a2fd68a9739275cc1c31189196da647e4e7dfd612f1cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.4-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 6f2808167c6a1bde223e31022ab6ffa7899437d62d0718967add6babe5b90242
MD5 0d547f2f9999d78bbe657bf5a4dda9be
BLAKE2b-256 bdb5feac4ebb6b338081cbdfd5ba9e47dc06c885c4e37390582c116ac2a7ce89

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: polars_reg-0.1.4-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 406.7 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.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 04177e72fad0fb72283f53b29cc7b3bc12df095f3b32d19552fac704e2da70d7
MD5 57d98622b5a18ccc32c5c19d9c6c795a
BLAKE2b-256 13df72b57fe1bf23808660649b5235a0271f8b5ee44e7dbfa0dda5bf9549bf68

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 baeea2b05e7537acabeef1f0efca5766372dfea472890743c7a81a1896efb601
MD5 610a6fe61fd88d4da1082bab3f16f8a8
BLAKE2b-256 3b33bc482c095e8bc025770c1b63ffcd40f17fa34be75d03e7da2add8f814d0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b86ff3fab0c69ddf9a080cb47b71f32670859f13c55328ba8a8c03488e7f6ee5
MD5 b7ab8799da6fac77309fb2235857afc8
BLAKE2b-256 e57ec34a11372d4cd54ec244b48a1c64303f58dd74eba60a36cb5b42e4436964

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.4-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5cbdb35d7939def737c8d0b5737038a8e3b2b8066c33d8fa2e79a59ff090ca25
MD5 a3f374f1344fd206ef9c367934bb1c55
BLAKE2b-256 101ea84476799c05b66547d5f555060a76fd144218732beb1db820e6f1e3e0c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: polars_reg-0.1.4-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 410.9 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.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 89476c141ee04c42d4021c9e659cf396e68a4f8e7bface63806dcbfbd8dff84e
MD5 f0089c97c2a61af2ea20a5abfc91afbe
BLAKE2b-256 2bcd8b56365cb338a760cd70d98b405d9e5683fa1fe72aaecaa6b07231ee0891

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 10b856cf52deadcca260087c3b0a672aed1fbbbbd293fd7a6c2e44472b803c73
MD5 63ca371b0e3a53e7ee80b3c2096a3ab0
BLAKE2b-256 16c0c026584461923c55f1347e5340ffaca7301edbf320993c029caacd32633b

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8497264b43f51056f6a6195ab35c56155828d77f2383b23c96b2a01cf40d80e6
MD5 6c540f6c0f7f8534504bf395334ce7c9
BLAKE2b-256 4600d4e51093cf1071189cb77189ee84930d63d8ee6669d8885a962d6b30a4b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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.4-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_reg-0.1.4-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 70cac5a6bd594e63065abed1c444b1cb6991d11f1c330f8c8f049e6e1c28bef9
MD5 659b6adb6b8304d257ac1c53834bdd9e
BLAKE2b-256 01a9ca069b9ba49fbb6e374605bf7ceb4ff678b89543d1077ad672b6e2a83783

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_reg-0.1.4-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