Skip to main content

torch-openreml

torch-openreml

Version 1.0.0 PyPI version GPL-3.0 Python >=3.10 PyTorch >=2.0

torch-openreml is a PyTorch-based library for AI-REML estimation of linear mixed models.

Author & Maintainer: Weihao (Patrick) Li — patrick.li@anu.edu.au

Overview

torch-openreml fits linear mixed-effects models using the Average Information REML (AI-REML) algorithm on a PyTorch backend. It supports flexible specification of covariance structures through a modular system of matrices and operators, along with automatic or manual gradients and optional parameter transformations for constrained estimation.

Unlike traditional mixed-model software, it does not provide a formula interface. Instead, users define the fixed- and random-effects design matrices and covariance structures directly in code. The library is focused purely on the computational and optimisation backend rather than model specification syntax.

Features

  • Torch-based backend — built on PyTorch, running on CPU, GPU, and other available accelerators.
  • AI-REML estimation engine — variance component estimation using average information restricted maximum likelihood, with a Newton-type scoring method, Armijo backtracking, and a gradient fallback.
  • Extensible covariance structure — composable covariance structures and operators from built-in and user-defined components.
  • Hybrid differentiation support — automatic differentiation and manually specified gradients.
  • Composable parameter transformations — configurable, chainable transformation pipelines for flexible parameterisation.

Installation

Install the released version from PyPI:

pip install torch-openreml

Or install the latest development version directly from GitHub:

pip install git+https://github.com/anu-aagi/torch-openreml.git

Dependencies: torch>=2.0, pandas, tqdm — Python 3.10 or later.

Model

The library assumes the linear mixed-effects model

$$ \mathbf{y} = \mathbf{X}\boldsymbol{\beta} + \mathbf{Z}(\boldsymbol{\theta})\mathbf{b} + \boldsymbol{\varepsilon} $$

with

$$ \mathbf{b} \sim \mathcal{N}(\mathbf{0}, \mathbf{G}(\boldsymbol{\theta})), \qquad \boldsymbol{\varepsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{R}(\boldsymbol{\theta})), $$

so that the marginal covariance of $\mathbf{y}$ is

$$ \mathbf{V}(\boldsymbol{\theta}) = \mathbf{Z}(\boldsymbol{\theta})\mathbf{G}(\boldsymbol{\theta})\mathbf{Z}(\boldsymbol{\theta})^\top + \mathbf{R}(\boldsymbol{\theta}). $$

A design may be a fixed tensor, as in the example below, or a parameterised Matrix whose free parameters are part of $\boldsymbol{\theta}$. Each random-effect term contributes its own design block and covariance, and $\mathbf{Z}(\boldsymbol{\theta})\mathbf{G}(\boldsymbol{\theta})\mathbf{Z}(\boldsymbol{\theta})^\top$ is assembled from those blocks.

Quick start

The example below fits yield from the bundled john_alpha dataset — a resolvable alpha lattice trial — with an intercept, a fixed effect for rep, a random intercept for gen, and a random rep:block interaction.

import torch
from torch_openreml import MixedModelREML
from torch_openreml.covariance import (
    Augment, DummyMatrix, IdentityMatrix, KroneckerProduct, ScalarMatrix,
)
from torch_openreml.example_data import john_alpha

rep = john_alpha["rep"]
block = john_alpha["block"]
gen = john_alpha["gen"]
n = len(john_alpha)

y = torch.tensor(john_alpha["yield"].values)

# Fixed effects X = [1 | rep], with the first replicate absorbed by the intercept.
X = Augment(torch.ones(n, 1), DummyMatrix(rep, drop_first=True))()

# Each random-effect term is a (design, covariance) pair; the residual
# covariance is supplied with R=. G_rep = I is fixed for identifiability.
reml = MixedModelREML(
    genotype=(DummyMatrix(gen), ScalarMatrix(gen)),
    replicate_block=(DummyMatrix(rep, block), KroneckerProduct(
        G_rep=IdentityMatrix(rep),
        G_block=ScalarMatrix(block),
    )),
    R=ScalarMatrix(n),
)

print(reml)  # the marginal covariance assembled from those terms

# Optimise on the transformed scale (zeros mean unit variances here), then
# map the estimates back to variance components.
free_param_names, free_params = reml.optimize(y, X, torch.zeros(3), verbose=1)
print(free_param_names)
print(reml.build_params(free_params))

# Post-estimation: BLUE of the fixed effects, BLUPs of the random effects.
beta_hat = reml.blue()
b_hat = reml.blup()
gen_blup = reml.blup_per_term()["genotype"]

Fitting is performed on the transformed parameter scale. build_params maps the estimates back to the original variance-component scale, with names given by the model's free-parameter names (['G/genotype/sigma^2', 'G/replicate_block/G_block/sigma^2', 'R/sigma^2'] in this example).

The response and design are stored on the model by optimize, so the post-estimation methods can be called without repeating them. blue returns the fixed-effect estimates, blup returns one prediction per random-effect level (the 24 genotypes followed by the 18 replicate-by-block combinations), and blup_per_term splits the same predictions into a dict keyed by the term names supplied to the constructor. cov_blue, var_blue, cov_blup, var_blup and their per-term variants give the corresponding sampling covariances and variances.

Documentation

Full documentation: https://torch-openreml.patrickli.org

Citing

@software{torch_openreml,
  author = {Weihao Li},
  title  = {torch-openreml},
  year   = {2026},
  url    = {https://github.com/anu-aagi/torch-openreml/}
}

Metadata

Release files for torch-openreml 1.0.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 torch-openreml 1.0.0
File Size Uploaded Provenance Actions
torch_openreml-1.0.0.tar.gz 125.4 kB Without provenance Details for torch_openreml-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for torch-openreml 1.0.0
File Interpreter ABI Platform Actions
torch_openreml-1.0.0-py3-none-any.whl Python 3 none any Details for torch_openreml-1.0.0-py3-none-any.whl

Total release size: 269.5 kB

Release files / torch_openreml-1.0.0.tar.gz

Download URL torch_openreml-1.0.0.tar.gz
Size 125.4 kB
Tags Source
SHA-256 checksum
How to use checksums
74c436e06541f857c756ee52d75d1e99bc5dd1722b4d1c7a7e91d64deccd4840
BLAKE2b-256 checksum
How to use checksums
a70b9cf360bf808e3f2958924490111b57e857e9c67d89c9edf029385cfa5c12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Release files / torch_openreml-1.0.0-py3-none-any.whl

Download URL torch_openreml-1.0.0-py3-none-any.whl
Size 144.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3682324cdf1e1ae9659eb4adf0eeb94fe603802ed2b04274e654ba28ccba9595
BLAKE2b-256 checksum
How to use checksums
8f0b8f7d0ee0e6f9e3a758edc8418997b3143b4b34359921f93072e1b33e9a31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Security for torch-openreml 1.0.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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