Skip to main content

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 MendelianRandomization 0.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.

Metadata

Release files for pytwosamplemr 0.1.0

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

Source distribution (sdist)

Source distribution for pytwosamplemr 0.1.0
File Size Uploaded
pytwosamplemr-0.1.0.tar.gz 70.5 kB Details

Built distribution (wheel)

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

Total release size: 126.5 kB

Release files / pytwosamplemr-0.1.0.tar.gz

Download URL pytwosamplemr-0.1.0.tar.gz
Size 70.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f0eda3a9ba0bde5684099db6cd9c8a6cd325f7bcecc07c0037a36449d9393545
BLAKE2b-256 checksum
How to use checksums
77a71688b9ca1bac6e2afc217695ee7a00cd4a3fea883af0f79dc535f6b15960
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 20, 2026.

Transparency log

Release files / pytwosamplemr-0.1.0-py3-none-any.whl

Download URL pytwosamplemr-0.1.0-py3-none-any.whl
Size 56.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
431ea3574354fbd03099c5776cf1129d933bf9fbce7c360409c48168e9e68e81
BLAKE2b-256 checksum
How to use checksums
0325b20ad572d2ca3a64765df3de8ad67c57995bee0f9f880f9c95cb38fde2b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 20, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

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