torch-openreml: a PyTorch-based framework for flexible linear mixed-effects modelling
Australasian Applied Statistics Conference · 2026
Weihao (Patrick) Li · Emi Tanaka
Research School of Finance, Actuarial Studies and Statistics, ANU
Why we model the covariance
Multi-environment trial
“Yield across environments is correlated. I want to predict performance in an environment where this genotype was never tested.”
Field experiment
“Plots close together tend to be more alike. If I ignore spatial correlation, my variety comparisons may be less reliable.”
Repeated measures
“The same plot is measured every season. Measurements one season apart are more alike than those five seasons apart. The correlation decays with the time gap.”
Clustered data
“Between-cluster variance is not constant. It changes with cluster-level covariates.”
From predefined structures to custom covariance models
Predefined structures
Custom covariance structures are harder to implement
lme4 — no simple user-facing interface for adding new covariance structures. Requires extending its covariance implementation.
glmmTMB — implement a new covariance structure in C++ and modify the R interface. Requires building from source.
V is the covariance of the observations, \mathrm{Var}(\boldsymbol{y}). In a mixed model V = ZGZ^{\top} + R. G is the covariance of the random effects, R of the residuals.
model.V() evaluates the graph to obtain the marginal covariance matrix.
\partial V/\partial\theta_k
model.V.grad() computes all parameter derivatives, combining analytical gradients with automatic differentiation (torch.func.jacrev) and propagating them through the graph.
V = model.V()grads, _ = model.V.grad()print("V ", V.shape)print("dV/dtheta", grads.shape)
V torch.Size([1200, 1200])
dV/dtheta torch.Size([18, 1200, 1200])