Linear and Quadratic Kernel Models

A kernel model treats the genetic values as a random effect whose covariance is a kernel matrix. The simplest kernel is the linear kernel, which is equivalent to an additive marker-effect model. A quadratic kernel adds pairwise marker products, so it can capture non-additive interaction effects.

Both models can be written as

\[\boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta} + \boldsymbol{Z}\boldsymbol{g} + \boldsymbol{e},\]

where \(\boldsymbol{g}\) is a vector of genetic values and

\[\boldsymbol{G} = \sigma_A^2 \boldsymbol{K}_1 + \sigma_Q^2 \boldsymbol{K}_2, \qquad \boldsymbol{R} = \sigma_e^2 \boldsymbol{I}.\]

The linear kernel is the standard VanRaden genomic relationship matrix. Starting from the raw marker matrix \(\boldsymbol{M}\), convert the allele scores to dosages and centre them:

\[\boldsymbol{D} = \boldsymbol{M} + 1, \qquad p_j = \frac{1}{2n}\sum_{i=1}^{n} D_{ij}, \qquad \boldsymbol{Z} = \boldsymbol{D} - 2\boldsymbol{p}^\top.\]

The standard denominator is

\[d = 2\sum_{j=1}^{m} p_j(1-p_j), \qquad \boldsymbol{K}_1 = \frac{\boldsymbol{Z}\boldsymbol{Z}^\top}{d},\]

where \(n\) is the number of genotypes and \(m\) is the number of markers. This is the usual VanRaden method 1. The mean diagonal of \(\boldsymbol{K}_1\) is not forced to one; its scale is part of the definition.

The quadratic kernel is the elementwise square

\[\boldsymbol{K}_2 = \boldsymbol{K}_1 \odot \boldsymbol{K}_1.\]

The quadratic kernel includes the squared marker terms and all pairwise products between markers. It is a single variance component: the model does not estimate one parameter per marker pair. No normalization of \(\boldsymbol{K}_2\) is needed; the variance component absorbs its scale.

Simulate the data

The example uses 100 genotypes from the Roseworthy 2014 field trial. The distributed file contains the raw marker scores, so the allele frequencies and the standard VanRaden denominator can be computed directly:

import pandas as pd
import torch

from torch_openreml import MixedModelREML
from torch_openreml.covariance import DummyMatrix, Product, ScalarMatrix, Sum
from torch_openreml.example_data import load_wheat_marker_100

torch.set_default_dtype(torch.float64)
torch.set_printoptions(precision=3, sci_mode=False)
pd.set_option("display.precision", 3)

data = load_wheat_marker_100()
data.shape
(100, 17115)

The file has one row per genotype and 17,114 marker columns. The marker values are the raw allele scores: (AA, AB, BB) = (1, 0, -1), with some continuous imputed values. The conversion to dosage and the centring are done below.

Note

Source and licence. The example data are derived from Taylor, Fruzangohar, and Walter, Roseworthy 2014 Field trial phenotype data and matching 17K+ genotype data, Adelaide University, https://doi.org/10.25909/23949333.v1. The source is licensed under the Creative Commons Attribution 4.0 International licence (CC BY 4.0), https://creativecommons.org/licenses/by/4.0/.

This modified subset is redistributed under CC BY 4.0. The original authors do not endorse this derived work. The docstring of load_wheat_marker_100 records the attribution and the list of modifications.

The raw marker scores are converted to dosages, centred, and divided by the standard VanRaden denominator.

genotype = data["genotype"].tolist()
M = torch.tensor(data.drop(columns="genotype").values)

D = M + 1.0
p = D.mean(dim=0) / 2.0
Z = D - 2.0 * p

d = 2.0 * (p * (1.0 - p)).sum()
K1 = Z @ Z.T / d
K2 = K1 * K1

print("genotypes:", len(genotype))
print("markers:", M.shape[1])
print("VanRaden d:", d.item())
print("K1 shape:", tuple(K1.shape))
print("K1 diagonal mean:", K1.diag().mean().item())
print("K2 diagonal mean:", K2.diag().mean().item())
genotypes: 100
markers: 17114
VanRaden d: 4439.819101759122
K1 shape: (100, 100)
K1 diagonal mean: 1.8992584147962766
K2 diagonal mean: 3.8230945906131226

The mean diagonal of K1 is approximately 1.9 rather than 1. This is expected for these mostly homozygous lines, as the standard VanRaden denominator does not guarantee a mean diagonal of one. K2 is used on its natural scale, with its fitted variance component absorbing the difference in scale.

The two kernels are correlated because K2 is constructed from the same markers as K1. Their upper-triangle correlation is approximately 0.66, allowing the additive and quadratic variance components to partially trade off during estimation.

i, j = torch.triu_indices(len(genotype), len(genotype), offset=1)
print("K1/K2 correlation:", torch.corrcoef(torch.stack([K1[i, j], K2[i, j]]))[0, 1])
K1/K2 correlation: tensor(0.662)

With K1 and K2 constructed, simulate the response with three observations per genotype. The genetic effects are shared across all records of the same genotype, while residual errors are independently generated for each observation. The true variance components are 0.3 for the additive kernel, 0.2 for the quadratic kernel, and 0.5 for the residual, all on the VanRaden kernel scales.

n_rep = 3
seed = 2027
generator = torch.Generator().manual_seed(seed)

genotype_index = torch.arange(len(genotype)).repeat_interleave(n_rep)
obs_genotype = [genotype[i] for i in genotype_index.tolist()]
n_obs = len(obs_genotype)

G_true = 0.3 * K1 + 0.2 * K2
L = torch.linalg.cholesky(G_true + 1e-8 * torch.eye(len(genotype)))
g = L @ torch.randn(len(genotype), generator=generator)
e = torch.sqrt(torch.tensor(0.5)) * torch.randn(n_obs, generator=generator)
y = 1.0 + g[genotype_index] + e

sim = pd.DataFrame({"genotype": obs_genotype, "y": y.numpy()})
sim.head()
genotype y
0 AYT-O-0002 -0.620
1 AYT-O-0002 1.182
2 AYT-O-0002 2.571
3 AYT-O-0003 0.942
4 AYT-O-0003 2.414

The random-effects design has one indicator column per genotype. Each row of Z_obs selects the genetic value of the genotype observed on that record.

Z_obs = DummyMatrix(obs_genotype)()
X = torch.ones(n_obs, 1)

print("Z_obs shape:", tuple(Z_obs.shape))
print("observations:", n_obs)
Z_obs shape: (300, 100)
observations: 300

Fit the linear kernel only

The linear model has one genetic variance component. ScalarMatrix takes the genotype labels directly, so its dimension matches the number of genotype columns in Z_obs.

G_linear = Product(
    scale=ScalarMatrix(obs_genotype),
    known=K1,
)

reml_linear = MixedModelREML(random=(Z_obs, G_linear))
reml_linear.optimize(y, X)

reml_linear.build_params(out_format="dict")
{'G/scale/sigma^2': tensor([1.153]), 'R/sigma^2': tensor([0.438])}

The fitted additive variance is about 1.15, much larger than the true additive value of 0.3. The linear model has no quadratic component, so it absorbs the interaction variance into the additive component.

Fit the linear and quadratic kernels

The joint model has two genetic variance components. Use Sum to add two (Z, G) blocks, each with its own scale.

G_joint = Sum(
    additive=Product(
        scale=ScalarMatrix(obs_genotype),
        known=K1,
    ),
    quadratic=Product(
        scale=ScalarMatrix(obs_genotype),
        known=K2,
    ),
)

reml_joint = MixedModelREML(random=(Z_obs, G_joint))
reml_joint.optimize(y, X)

reml_joint.build_params(out_format="dict")
{'G/additive/scale/sigma^2': tensor([0.391]),
 'G/quadratic/scale/sigma^2': tensor([0.243]),
 'R/sigma^2': tensor([0.432])}

The fitted additive and quadratic variances are about 0.39 and 0.24. The residual variance is about 0.43, close to the true value of 0.5. The parameter names are G/additive/scale/sigma^2, G/quadratic/scale/sigma^2, and R/sigma^2.

Compare the fits

ll_linear = reml_linear.loglik()[0]
ll_joint = reml_joint.loglik()[0]

print("linear loglik:", ll_linear)
print("joint loglik:", ll_joint)
print("gain:", ll_joint - ll_linear)
linear loglik: tensor(-128.683)
joint loglik: tensor(-126.004)
gain: tensor(2.679)

The joint model improves the log-likelihood by about 2.68 in this simulated sample. Because the two kernels are correlated and the quadratic component is weakly identified, one data set is not enough to estimate the interaction variance precisely. The important distinction is that the linear-only model has no parameter for the quadratic kernel.