Skip to main content

m2n2

Mixed modeling with neural networks, built on pyreml.

The random design of a linear mixed model is the embedding of a neural network, trained jointly with the fixed effects and the variance components on the REML loss.

Installation

Install m2n2 with pip:

pip install m2n2

The model

m2n2 fits a linear mixed model built upon the neural network $\nu$ of parameters $\theta$:

$$ y = X\beta + Zu + r \qquad z_i \mapsto \nu(z_i, \theta) \in \mathbb{R}^d $$

  • $y$: a quantitative continuous label.
  • $X$: the fixed effects design matrix, provided from a patsy formula.
  • $\beta$: the fixed effects.
  • $z_i$: features of individual $i$.
  • $Z = \bigoplus_{k=1}^{q} \nu(z_{(k)}, \theta), Z \in \mathbb{R}^{n \times qd}$: the random effects design matrix, or matrix of embeddings, for all $n$ individuals from $q$ distinct groups.
  • $u \sim N(0, \sigma^2_u I), u \in \mathbb{R}^{qd}$: the embedding effects, with an embedding effect vector $u_k$ for each of the $q$ groups.
  • $r \sim N(0, \sigma^2_R I)$: the residual.

Several random effects based on neural networks can be provided, either referring to the same neural network, the same features, or not.

Additional random effects following the pyreml specification system can also be provided.

Example

from functools import partial
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from meiosim import Arabidopsis
from meiosim.Population import MISSING
from m2n2 import MixedModelNN, Embedding, RandomNN
from pyreml import Random, Residual

pop = Arabidopsis(n_SNPs=10_000, seed=42)
data = pop.phenotypes[["individual", "DTF1"]].merge(
    pop.metadata[["individual", "country"]], on="individual"
)

z = pop.genotypes.astype(np.float32)
z[z == MISSING] = np.nan
z = torch.tensor(np.where(np.isnan(z), np.nanmean(z, axis=0), z))

class Network(nn.Module):
    def __init__(self, z):
        super().__init__()
        self.register_buffer("z", z)
        self.body = nn.Sequential(
            nn.Dropout(0.1),
            nn.Linear(z.shape[1], 256), nn.BatchNorm1d(256), nn.GELU(), nn.Dropout(0.5),
            nn.Linear(256, 128), nn.BatchNorm1d(128), nn.GELU(), nn.Dropout(0.5),
            nn.Linear(128, 64),
        )

    def forward(self, rows=None):
        return self.body(self.z if rows is None else self.z[rows])

torch.manual_seed(42)
network = Network(z) # /!\ z and data must be perfectly aligned

model = MixedModelNN.from_dataframe(
    data,
    response = "DTF1",
    fixed = "1 + country",
    embedding = {
        "geno": Embedding(
            network,
            optimizer = partial(
                torch.optim.AdamW,
                lr=1e-3,
                weight_decay=1e-1
            )
        )
    },
    random = [
        RandomNN(
            embedding = "geno",
        ),
        RandomNN(
            embedding = "geno",
            group = "country",
        )
    ],
    residual = Residual(),
    lr_beta=1e-2,
    lr_var=1e-2,
)

model.fit(
    n_epoch=1000,
    batch_size=256,
    validation=0.2,
    patience=50,
    verbose=False,
)


fig, (ax_loss, ax_score) = plt.subplots(1, 2, figsize=(10, 3.5))
ax_loss.plot(model.loss)
ax_loss.set_xlabel("epoch")
ax_loss.set_ylabel("REML -2logLik")
ax_score.plot(model.score)
ax_score.axvline(np.argmin(model.score), color="grey", linestyle="--")
ax_score.set_xlabel("epoch")
ax_score.set_ylabel("validation MSE")
plt.show()

model.random[0].plot(group_by = "country")
model.random[1].plot()

model.varcomp()

Documentation

The documentation is available at this address.

Code

The code is available at this address.

License

Copyright © 2026 CNRS, University of Montpellier

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

Metadata

Release files for m2n2 0.0.3

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

Source distribution (sdist)

Source distribution for m2n2 0.0.3
File Size Uploaded Provenance Actions
m2n2-0.0.3.tar.gz 25.8 kB With provenance Details for m2n2-0.0.3.tar.gz

Built distribution (wheel)

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

Total release size: 50.5 kB

Release files / m2n2-0.0.3.tar.gz

Download URL m2n2-0.0.3.tar.gz
Size 25.8 kB
Tags Source
SHA-256 checksum
How to use checksums
043fc38ed5c8c26edce32d3b9b951b418eccf54852b8e2a7a6dbbe8ff65b6992
BLAKE2b-256 checksum
How to use checksums
741bf3de3567dfcebd2dc8b27992e21bce1650af386c3f5269cedcfe7f3ad8c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 21, 2026.

Transparency log

Release files / m2n2-0.0.3-py3-none-any.whl

Download URL m2n2-0.0.3-py3-none-any.whl
Size 24.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26eb1e344a0fe86a179c3520035c192fa46b0dde1427798042da4649b4f58f6c
BLAKE2b-256 checksum
How to use checksums
98a0bec226706d13aa1f015a9cf13b98918e3756ade6c5022c16b513e5d1484d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 21, 2026.

Transparency log

Security for m2n2 0.0.3

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

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for m2n2-0.0.3.tar.gz

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 Nov 21, 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.

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