Expert-mode marginal effects for Python with session-level analytical pre-commitment.
Project description
pymargins
Expert-mode marginal effects for Python. Session-level analytical pre-commitment, JAX-native autodiff, κ-driven simulation fallback.
Status
Alpha. End-to-end usable with statsmodels GLM and OLS/WLS/GLS through auto-detection. Additional adapters (sklearn, linearmodels, mixed models), cluster/block bootstrap, and reporting polish remain on the roadmap.
Quick example
import statsmodels.formula.api as smf
import statsmodels.api as sm
from pymargins import Margins
# Fit a model
fit = smf.glm(
"outcome ~ treatment + age + sex",
data=df,
family=sm.families.Binomial(),
).fit()
# Wrap in a session, committing to log-scale analysis
m = Margins.log_scale(fit, vcov="HC3", level=0.95)
print(m.summary()) # methods-section paragraph
# Pre-flight diagnostic: is delta reliable here?
print(m.diagnose().summary())
# Compute a relative risk contrast
rr = m.contrasts(
scenarios=[
{"atexog": {"treatment": 1}, "label": "treated"},
{"atexog": {"treatment": 0}, "label": "control"},
],
contrasts=[+1, -1],
)
print(rr.summary()) # estimate, asymmetric CI, κ, etc.
Performance notes
-
Bootstrap with
n_jobs > 1: Parallel bootstrap uses aThreadPoolExecutorfor model refitting, but JAX evaluation is always serial in the main thread. This avoids XLA compilation race conditions. BLAS threads are limited to 1 per worker to prevent oversubscription. -
Large scenario grids:
expand_scenariocreates one block of rows per grid point. For a 10-point grid over a 1M-row dataset this materialises 10M rows. Use smaller representative samples (e.g.at="typical") or pass explicitdata=overrides when exploring high-dimensional counterfactuals. -
Memory retention:
MarginsResultobjects hold references to the parent session, design matrices, and gradients. Callresult.materialize()promptly on results you intend to store long-term; this drops the heavy machinery while preserving estimates, standard errors, and confidence intervals.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pymargins-0.0.1.tar.gz.
File metadata
- Download URL: pymargins-0.0.1.tar.gz
- Upload date:
- Size: 392.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2788d6b1c492b1145428782a71f9fe04a2974ac8b56fcfab62ac6d093b0c0f63
|
|
| MD5 |
6211aed085e48ab659a94bc110a62e12
|
|
| BLAKE2b-256 |
c95ee0a66e7a28d4415990851f2e61575d2246ef1fea4e0c326c1ca0ef4997e3
|
File details
Details for the file pymargins-0.0.1-py3-none-any.whl.
File metadata
- Download URL: pymargins-0.0.1-py3-none-any.whl
- Upload date:
- Size: 208.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f42bb8d98eddeb0f3adeb5c1667ff5e3ec91f2b7b8ff4ea7606b0626815797c3
|
|
| MD5 |
d466dfd3eee3af9c49112df698b457e0
|
|
| BLAKE2b-256 |
3167efeba7cb7f36d3e0f5e041f73353ed69b4d68ccf0dcd49df2e5ada844e05
|