Skip to main content

pynlme: nonlinear mixed-effects models in Python

Fit nonlinear mixed-effects models (population PK/PD, growth curves, systems-biology models) in Python. pynlme reimplements the published algorithms behind MATLAB's nlmefit/nlmefitsa and R's nlme/saemix, reproduces their results, and is validated against all of them.

pynlme is an independent project, not affiliated with MathWorks or the R project. Its code is its own and contains no MATLAB or R code: the replicate modes reproduce the reference programs' computations, worked out from the publications, their documentation, outputs and traces, and their source (see Implementation: Sources).

Pick your starting point

Coming from MATLAB

Same functions, same arguments, same outputs, same option names:

import numpy as np
from pynlme import nlmefit, nlmefitsa

# MATLAB: model = @(phi,t)(phi(1).*exp(-phi(2).*t)+phi(3).*exp(-phi(4).*t));
def model(phi, t):
    return phi[0] * np.exp(-phi[1] * t) + phi[2] * np.exp(-phi[3] * t)

beta, psi, stats, b = nlmefit(time, concentration, subject, [], model, [1, 1, 1, 1],
                              ParamTransform=[0, 1, 0, 1])

Indices in MATLAB-named options are 1-based (REParamsSelect=[2, 3]); b is r-by-m; stats has MATLAB's fields and definitions. nlmefit reproduces MATLAB's iteration step by step with MATLAB's default options, so on most data the result is MATLAB R2023b's to ~1e-10 (checked on thousands of simulated datasets). Start with the tutorial.

Python users

The same estimators with snake_case options and 0-based indices:

import numpy as np
from pynlme import fit_nlme

def model(phi, t):
    return phi[0] * np.exp(-phi[1] * t[:, 0])

beta, psi, stats, b = fit_nlme(t, y, group, None, model, [10.0, 0.5])
beta, psi, stats, b = fit_nlme(t, y, group, None, model, [10.0, 0.5], method="SAEM",
                               random_state=1)

t holds the predictors (one row per observation), group the subject of each row; data can also be given grouped, one row per subject. Options such as param_transform=[0, 1], re_params_select=[0] or error_model="proportional" follow MATLAB's, and vectorization="full" evaluates all subjects in one model call. By default the estimators follow MATLAB's; compat="r" follows R's.

pynlme.fit is a named-parameter interface (dict or Polars data, dict results); its design is not settled yet and may change. See the API reference.

R users (nlme, saemix) who would rather not use R

pynlme.r takes R's nlme() call almost verbatim, with formulas as strings, and prints R's summary():

import polars as pl
from pynlme.r import nlme, pdDiag, summary, fixef, ranef, VarCorr

theo = pl.read_csv("theoph.csv").with_columns(wtc=pl.col("wt") - 70)
fit = nlme("conc ~ SSfol(dose, time, lKe, lKa, lCl)", data=theo,
           fixed=["lKe ~ 1", "lKa ~ 1", "lCl ~ wtc"],
           random=pdDiag("lKa + lCl ~ 1"), groups="~subject",
           start=[-2.5, 0.5, -3.2, 0])
print(summary(fit))      # same layout and numbers as R
fixef(fit)["lCl.wtc"]; ranef(fit)[1]; VarCorr(fit)["Residual"]["StdDev"]

fixef, ranef, VarCorr, vcov, logLik, AIC, BIC, fitted, resid, predict(fit, newdata, level=) work as in R; the fit itself is a dict. R's defaults are kept (method="ML", pdSymm for a plain random formula), and the results match R nlme to ~1e-4 or closer. Not yet supported: weights= (use error_model=), random effects with covariates, and transformed covariates in fixed (compute a column instead). See the API reference.

saemix users write the same three calls as in R, with the model in R's psi[id, k] form:

import numpy as np
import polars as pl
from pynlme.r import saemixData, saemixModel, saemix, summary, coef, psi, predict

theo = pl.read_csv("theoph.csv")

def model(psi, id, xidep):
    dose, t = xidep[:, 0], xidep[:, 1]
    ka, V, CL = psi[id, 0], psi[id, 1], psi[id, 2]
    k = CL / V
    return dose * ka / (V * (ka - k)) * (np.exp(-k * t) - np.exp(-ka * t))

data = saemixData(theo, name_group="subject", name_predictors=["dose", "time"],
                  name_response="conc", name_covariates=["wt"])
smodel = saemixModel(model, psi0={"ka": [1, 0], "V": [20, 0], "CL": [0.5, 0]},
                     transform_par=[1, 1, 1], covariate_model=[[0, 0, 1]],
                     covariance_model=np.diag([1, 0, 1]))
fit = saemix(smodel, data, {"seed": 632545})
print(summary(fit))            # saemix's layout: estimates, SE, CV(%), omega2, -2LL/AIC/BIC
psi(fit)[1]; predict(fit, type="ppred")

saemix's defaults are kept (one chain, nbiter.saemix = c(300, 100), importance sampling, standard errors); results agree with R saemix within Monte Carlo error.

Lower-level equivalents of R's arguments:

R pynlme
nlme(..., method="ML") / "REML" nlmefit(..., ApproximationType="LME") / "RELME", compat="r"
fixed = list(a ~ 1, b ~ wt) fit(..., covariates={"b": ["wt"]}) or FEGroupDesign
random = pdDiag(a ~ 1) / pdSymm(...) REParamsSelect, CovPattern diagonal / full
saemix: transform.par, covariance.model ParamTransform (same codes), CovPattern
saemix(...) nlmefitsa(..., compat="r") (one chain, as saemix)

compat="r" reproduces nlme's iteration step by step, so pynlme returns R's fit even where the model has several local solutions, and R's method="REML" (which for nonlinear models is not a restricted-likelihood fit). The default compat="matlab" RELME is a true REML fit. See docs/troubleshooting.md.

What is validated

Every claim here is a test in tests/validation/; the details and findings are in validation/README.md.

  • nlmefit: matches MATLAB R2023b on every output (estimates, covariance, log-likelihood, AIC/BIC, standard errors, error parameters, all residual types, random effects) for LME and RELME with all four error models; on 3200 simulated fits, 2699 of the 2715 that never had a variance at zero agree with MATLAB to <1e-8. FOCE reaches MATLAB's optimum or a better one; FO reaches far better optima than MATLAB's (whose FO collapses the variances to zero).
  • compat="r": matches R nlme on fixed datasets (ML and nlme's REML), and gives R's fit on 2399 of 2400 freshly simulated datasets (the other has a variance that is not identified).
  • nlmefitsa (SAEM): agrees with MATLAB and R saemix within Monte Carlo error; reaches the exact maximum likelihood on a linear model; removes the linearisation bias of nlmefit in simulation studies.
  • ODE models (SciPy, or compiled with SUND): reproduce MATLAB's ode45-based fits.
  • Accuracy (validation/accuracy/): on 100 simulated datasets per scenario (a basic one, and one with three correlated random effects, a covariate and a proportional error) pynlme's estimates and standard errors match MATLAB's, R's and nlmixr2's: unbiased SAEM and FOCE with 90-99% coverage of the 95% confidence intervals, and LME with the linearisation's small bias that every program shares. MATLAB's FOCE is biased (coverage down to 0% for log CL).

Supported: ApproximationType LME/RELME/FO/FOCE; error models constant, proportional, combined, exponential; ParamTransform; fixed- and random-effects designs including covariate models; diagonal or full covariance; SAEM options and log-likelihoods. Not yet: FEObsDesign/REObsDesign, block CovPattern, LogLikMethod="gq", Replicates. Unsupported options raise errors; none are silently ignored.

Speed

pynlme against the original programs (MATLAB R2023b nlmefit/nlmefitsa, R nlme and saemix), all run on the same machine, on the same data with the same start values and options; times in milliseconds, the best of several runs (MATLAB after a warm-up run). Each pynlme mode runs the iteration of the program next to it. The models are called once for all subjects (Vectorization="Full" in pynlme and MATLAB; R and saemix do this anyway).

LME pynlme MATLAB mode (NumPy) pynlme MATLAB mode (Rust) MATLAB nlmefit pynlme R mode (NumPy) pynlme R mode (Rust) R nlme
indometh 142 18.9 72.2 81.3 18.7 46.0
theoph 28.3 16.2 16.1 108 24.2 39.0
orange 9.6 5.0 3.8 13.2 3.1 7.0
linear 24.5 7.6 20.9 11.1 4.5 7.0
FOCE pynlme (NumPy) pynlme (Rust) MATLAB nlmefit
indometh 2695 693 1447
theoph 1966 617 1722
orange 411 96.5 97.9
linear 336 63.2 137
SAEM pynlme nlmefitsa (NumPy) pynlme nlmefitsa (Rust) MATLAB nlmefitsa pynlme pynlme.r.saemix R saemix
theoph 246 163 875 238 1305
orange 175 110 1538 174 1253
linear 157 85.1 1665 131 466

In short: on these small problems all programs take milliseconds to a second, and the differences rarely matter in practice. With the Rust kernels (the default):

  • LME: pynlme is faster than MATLAB on indometh and linear (2.8-3.8 times), equal on theoph and slower on orange (5.0 vs 3.8 ms), and 1.6-2.5 times faster than R nlme.
  • FOCE: pynlme is 2-3 times faster than MATLAB, and equal on orange. Its FOCE is its own iteration (it reaches MATLAB's optimum or a better one, not MATLAB's path).
  • SAEM: pynlme is 5-20 times faster than MATLAB and 3.5-7 times faster than saemix, with the same iterations and chains as each.

Without the kernels (NumPy only) pynlme is 1.1-4 times slower than MATLAB and R for LME and FOCE, and still faster for SAEM.

Machine: AMD Ryzen 7 7800X3D (8 cores, 16 threads), 32 GB RAM, Linux (Arch, kernel 7.2). pynlme 0.5.0 on Python 3.13, NumPy 2.5, SciPy 1.18; MATLAB R2023b Update 1 (Statistics and Machine Learning Toolbox 23.2, 8 computational threads); R 4.6.1, nlme 3.1-169, saemix 3.5.

We are not experts in tuning MATLAB or R for speed: the original programs run as a typical user would run them (their default settings, an interpreted model function, the system BLAS), and they could likely be made faster. The times also vary by 10-20% between runs, and R's timer resolves 1 ms. These cases are small; larger problems rank differently in places (below).

Larger problems (validation/scaling/: 10, 100 and 1000 simulated subjects, a closed-form, an ODE and a multiple-dose ODE model, also against nlmixr2). At 1000 subjects pynlme is the fastest for LME (19 times faster than MATLAB on the closed-form model; on the ODE model 10 times faster than MATLAB and 44 times faster than R nlme) and for SAEM (2.5-85 times faster than MATLAB, saemix and nlmixr2 on the closed-form model, 5 times faster than nlmixr2 on the ODE model). Its FOCE is 70 times faster than MATLAB's, but nlmixr2's FOCEi is 6 times faster than pynlme on the closed-form model and far faster on the ODE models, where nlmixr2 uses sensitivity equations and pynlme finite differences. All programs reach the same fits.

Verify on your machine: bash validation/run_benchmarks.sh runs every benchmark with what is installed (pynlme always; R and MATLAB when available, otherwise their stored timings are used) and prints these tables. Or step by step: Rscript validation/r/benchmark.R, validation/matlab/benchmark.m in MATLAB, then uv run python validation/benchmark.py --markdown.

The Rust kernels (rust/, built into the package as pynlme._kernels) compile the inner loops of every algorithm: both LME modes, FO/FOCE and SAEM's sampler; the future custom mode will use them too. The model function stays Python and is called back once per step for all subjects. The kernels do the same arithmetic as the NumPy code, which runs instead when they are missing or PYNLME_KERNELS=0 is set. Without Vectorization="Full" the model is called once per subject: same results, several times slower.

ODE models

  • SUND (recommended): compile the model with the SUND toolbox and use pynlme.sund_model("model_name", feature, parameters, v_states=[...]) (pip install "pynlme[sund]"); see examples/sund_ode_model.py.
  • SciPy: pynlme.ode_model(rhs, y0, output) with a vectorised right-hand side.
  • Doses and other inputs: SUND activities, one set per experiment (sund_model(..., activities={1: oral, 2: infusion}), the experiment in a column of V); person-specific doses (e.g. per kg) are written in the model.
  • Several measured features (e.g. concentration and effect): stack them in one response vector with a feature column in X, and pass feature=["conc", "effect"] (SUND) or a multi-column output (SciPy); each subject is simulated once.

Speed with ODE models (the scaling benchmark): at 1000 subjects LME fits are 10 times faster than MATLAB with ode45 and 44 times faster than R nlme with deSolve; SAEM is 5 times faster than nlmixr2's (and 5 times faster than MATLAB's at 10 subjects). SciPy's ode_model, which solves all subjects as one system, is the faster solver for LME with many subjects and for SAEM; SUND for FOCE, for LME with few subjects, and for models with inputs (activities). FOCE is slow with ODE models (finite differences re-solve every subject's random effects); prefer LME or SAEM there. Analytic derivatives (pynlme.with_jacobian) replace finite differences when available.

Installation

Python 3.11-3.14, on NumPy and SciPy, with compiled kernels in Rust. From PyPI (from version 0.5.0) or a release; the wheels (Linux, Windows, macOS) need no compiler:

pip install pynlme                                                # from PyPI
pip install "pynlme[sund]"                                        # with SUND models
pip install git+https://github.com/willov/pynlme                  # the latest source

Installing from git (or the source archive) compiles the kernels and needs a Rust toolchain (rustup.rs).

SUND compiles your model files on sund.install_model("model.txt"), with the system C compiler if there is one and otherwise the Zig compiler it installs with it.

For development:

git clone https://github.com/willov/pynlme && cd pynlme
uv sync --dev --extra sund     # builds the Rust kernels; leave out --extra sund to skip SUND
uv run pytest                  # --runslow adds the simulation studies

uv sync rebuilds the kernels when files in rust/ change, and installs exactly what is requested: running it without --extra sund uninstalls SUND (its folder of compiled models stays behind as an empty sund package; rerun with --extra sund to restore it). The validation tests compare with R when Rscript with nlme and jsonlite is available, and with MATLAB when PYNLME_MATLAB names the MATLAB command (see validation/README.md).

Documentation

Tutorial · API reference · Troubleshooting · Implementation · Validation · Accuracy · Scaling · Changelog

License

MIT License, see LICENSE.

References

  1. Lindstrom, M. J., and D. M. Bates. "Nonlinear mixed-effects models for repeated measures data." Biometrics 46 (1990): 673-687.
  2. Pinheiro, J. C., and D. M. Bates. Mixed-Effects Models in S and S-PLUS. Springer, 2000.
  3. Kuhn, E., and M. Lavielle. "Maximum likelihood estimation in nonlinear mixed effects models." Computational Statistics & Data Analysis 49 (2005): 1020-1038.
  4. Delyon, B., M. Lavielle, and E. Moulines. "Convergence of a stochastic approximation version of the EM algorithm." Annals of Statistics 27 (1999): 94-128.
  5. Comets, E., A. Lavenu, and M. Lavielle. "Parameter estimation in nonlinear mixed effect models using saemix, an R implementation of the SAEM algorithm." Journal of Statistical Software 80 (2017).

Metadata

Release files for pynlme 0.5.0

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

Built distributions (wheels)

Table of built distributions (wheels) for pynlme 0.5.0
File Actions
pynlme-0.5.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for pynlme-0.5.0-cp311-abi3-win_amd64.whl
pynlme-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for pynlme-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
pynlme-0.5.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for pynlme-0.5.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
pynlme-0.5.0-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for pynlme-0.5.0-cp311-abi3-macosx_11_0_arm64.whl
pynlme-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details for pynlme-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl

Total release size: 2.2 MB

Release files / pynlme-0.5.0-cp311-abi3-win_amd64.whl

Download URL pynlme-0.5.0-cp311-abi3-win_amd64.whl
Size 366.5 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
b1a929e01ac2158a4cfdce1e16dc461e350a29562f778d9f4d8c87b29a3f9979
BLAKE2b-256 checksum
How to use checksums
9a8ca66f5011a84768a3b24def75e7a7518e68462f43ec98d7b5f12ad718da58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / pynlme-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL pynlme-0.5.0-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 461.6 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
91623a74c757a458fda13a346a19849368bbcc9775dba251227702b2de78c126
BLAKE2b-256 checksum
How to use checksums
cee41c637f57db700480724a0f9a19a9f3bca189b7fe69fa69722d7ecba2e7ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / pynlme-0.5.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL pynlme-0.5.0-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 449.3 kB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
120571e397ed5d82f96edf0affa19b205b26b843491bdfa048f882fbc11d4840
BLAKE2b-256 checksum
How to use checksums
1e9a3cd4618609db61e258ee30dc1d0817b7ac477c5b9ac8e9705f7c30895b6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / pynlme-0.5.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL pynlme-0.5.0-cp311-abi3-macosx_11_0_arm64.whl
Size 431.5 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f43f0091531befc9423073b7b49025a18f4d712fc1cc39dbf66a89941309eac8
BLAKE2b-256 checksum
How to use checksums
abef05d89fc953de518911c915f88872c06fa686d6d687c13cf90bef39e694a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Release files / pynlme-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl

Download URL pynlme-0.5.0-cp311-abi3-macosx_10_12_x86_64.whl
Size 449.9 kB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
0f1c8d80205d962a9187d22b330f11b582fb666c3966236a7eae10444ce2d82b
BLAKE2b-256 checksum
How to use checksums
55ee407b59b0baaffc0b4b35c39cec85814ef61e5695044f498419d20d442f40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 12, 2026.

Transparency log

Security for pynlme 0.5.0

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.

Full provenance

All 5 files in this release have been uploaded with provenance metadata.

Select from 5 files

Attestation for pynlme-0.5.0-cp311-abi3-win_amd64.whl

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 Oct 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.0 This release

5 release files Full provenance

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