Pure-Python port of two-sample Mendelian randomization — the MendelianRandomization estimator suite plus the TwoSampleMR harmonisation / diagnostics workflow.
Project description
py-twosamplemr
Pure-Python two-sample Mendelian randomization — a faithful port of
the statistical-methods core of the R/CRAN package
MendelianRandomization
(Yavorska & Burgess, Int. J. Epidemiol. 2017) together with the
harmonisation / diagnostics workflow of
TwoSampleMR
(Hemani et al., eLife 2018).
- PyPI dist name:
pytwosamplemr| import name:pytwosamplemr - Pure Python —
numpy/scipy/pandas/matplotlib. No rpy2. - Numerical parity with R is the design goal. Closed-form estimators
reproduce
MendelianRandomization0.10.0 bit-for-bit (relative difference< 1e-5); bootstrap-based estimators agree to within Monte-Carlo error.
Installation
pip install pytwosamplemr
# or, from a checkout:
pip install -e .
Quick start
import pytwosamplemr as mr
from pytwosamplemr.datasets import ldl_chd_input
# the MendelianRandomization bundled lipid GWAS example (28 SNPs)
obj = ldl_chd_input()
mr.mr_ivw(obj) # inverse-variance weighted
mr.mr_egger(obj) # MR-Egger regression (+ pleiotropy intercept)
mr.mr_maxlik(obj) # maximum likelihood
mr.mr_divw(obj) # debiased IVW
mr.mr_conmix(obj) # contamination mixture
mr.mr_lasso(obj) # MR-Lasso post-selection
mr.mr_cml(obj, n=17723) # constrained maximum likelihood
# panel + diagnostics
mr.mr_allmethods(obj, method="main")
mr.mr_heterogeneity(obj)
mr.mr_pleiotropy_test(obj)
mr.mr_singlesnp(obj)
mr.mr_leaveoneout(obj)
# plots
mr.mr_scatter(obj) # SNP effects + fitted method lines
mr.mr_forest(obj)
mr.mr_funnel(obj)
mr.mr_loo(obj)
What is implemented
Input
| Function | Description |
|---|---|
mr_input / MRInput |
harmonised SNP-exposure / SNP-outcome effect container |
Estimators (full coverage)
| Function | Method | R parity |
|---|---|---|
mr_ivw |
inverse-variance weighted (fixed / random, penalized, correlated SNPs, Cochran's Q) | bit-exact |
mr_egger |
MR-Egger regression: causal estimate + pleiotropy intercept + I²_GX | bit-exact |
mr_median |
simple / weighted / penalized weighted median | estimate exact, bootstrap SE ≈ |
mr_mbe |
mode-based estimate | estimate exact, bootstrap SE ≈ |
mr_maxlik |
maximum likelihood (R Nelder-Mead optimiser ported) | bit-exact |
mr_divw |
debiased IVW | bit-exact |
mr_conmix |
contamination mixture | bit-exact |
mr_lasso |
MR-Lasso post-selection IVW | post-estimate exact |
mr_cml |
constrained maximum likelihood (cML-MA / cML-BIC) | cML-BIC bit-exact; DP ≈ |
mr_allmethods |
run the standard panel and tabulate | — |
Diagnostics / workflow (TwoSampleMR side)
harmonise_data · mr_heterogeneity · mr_pleiotropy_test ·
mr_steiger / directionality_test · mr_singlesnp · mr_leaveoneout
Plots
mr_plot / mr_scatter · mr_forest · mr_funnel · mr_loo
R parity
py-twosamplemr was validated against MendelianRandomization 0.10.0
on the package's own bundled lipid GWAS example data. Closed-form
estimators agree with R to machine precision:
| Method | R estimate | Python estimate | rel. diff |
|---|---|---|---|
| IVW (random) | 2.834214 | 2.834214 | 6e-16 |
| IVW (fixed) | 2.834214 | 2.834214 | 6e-16 |
| MR-Egger | 3.252890 | 3.252890 | 3e-16 |
| Maximum likelihood | 3.224944 | 3.224944 | 6e-12 |
| Debiased IVW | 2.939646 | 2.939646 | 1e-15 |
| Contamination mix | 2.730000 | 2.730000 | 2e-16 |
| MR-Lasso | 2.670896 | 2.670896 | 2e-15 |
| cML-MA-BIC | 2.894808 | 2.894808 | 1e-15 |
A standout detail: mr_maxlik calls R's optim(method="Nelder-Mead"),
which on this rough 29-dimensional likelihood stops at a non-global
local point — so reproducing R requires a line-for-line port of R's
nmmin C routine (pytwosamplemr/_nmmin.py). With that port the
maximum-likelihood estimate matches R to 12 significant figures.
Bootstrap-based estimators (mr_median, mr_mbe, mr_cml with data
perturbation) cannot be bit-exact across RNGs: their point estimates
still match R exactly (the median / mode location does not depend on the
bootstrap), and the bootstrap SE agrees to within ~10%.
The R-parity test suite (tests/test_r_parity.py) runs the R reference
driver live and asserts these tolerances; it skips gracefully when R is
unavailable.
Testing
python -m pytest tests/ -q
License
GPL-3, matching the upstream MendelianRandomization package
(licensed GPL-2 | GPL-3).
References
- Yavorska OO, Burgess S. MendelianRandomization: an R package for performing Mendelian randomization analyses using summarized data. Int J Epidemiol. 2017;46(6):1734-1739.
- Hemani G, et al. The MR-Base platform supports systematic causal inference across the human phenome. eLife. 2018;7:e34408.
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 pytwosamplemr-0.1.0.tar.gz.
File metadata
- Download URL: pytwosamplemr-0.1.0.tar.gz
- Upload date:
- Size: 70.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0eda3a9ba0bde5684099db6cd9c8a6cd325f7bcecc07c0037a36449d9393545
|
|
| MD5 |
b4519f04825ddf21ed67e0336390604f
|
|
| BLAKE2b-256 |
77a71688b9ca1bac6e2afc217695ee7a00cd4a3fea883af0f79dc535f6b15960
|
Provenance
The following attestation bundles were made for pytwosamplemr-0.1.0.tar.gz:
Publisher:
publish.yml on omicverse/py-twosamplemr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytwosamplemr-0.1.0.tar.gz -
Subject digest:
f0eda3a9ba0bde5684099db6cd9c8a6cd325f7bcecc07c0037a36449d9393545 - Sigstore transparency entry: 1582831561
- Sigstore integration time:
-
Permalink:
omicverse/py-twosamplemr@24eca57cc9d22c664de1347c1559dffc76fa327b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/omicverse
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@24eca57cc9d22c664de1347c1559dffc76fa327b -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pytwosamplemr-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytwosamplemr-0.1.0-py3-none-any.whl
- Upload date:
- Size: 56.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
431ea3574354fbd03099c5776cf1129d933bf9fbce7c360409c48168e9e68e81
|
|
| MD5 |
5043dba87cbd6a8c1948a49b32bceaf3
|
|
| BLAKE2b-256 |
0325b20ad572d2ca3a64765df3de8ad67c57995bee0f9f880f9c95cb38fde2b6
|
Provenance
The following attestation bundles were made for pytwosamplemr-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on omicverse/py-twosamplemr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytwosamplemr-0.1.0-py3-none-any.whl -
Subject digest:
431ea3574354fbd03099c5776cf1129d933bf9fbce7c360409c48168e9e68e81 - Sigstore transparency entry: 1582831629
- Sigstore integration time:
-
Permalink:
omicverse/py-twosamplemr@24eca57cc9d22c664de1347c1559dffc76fa327b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/omicverse
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@24eca57cc9d22c664de1347c1559dffc76fa327b -
Trigger Event:
workflow_dispatch
-
Statement type: