torch_openreml.methods.CForm

class torch_openreml.methods.CForm[source]

Bases: object

C-form REML score and AI for one random-effect block.

The class is stateless. ai_step() is the model-aware entry point: it extracts \(\symbf{Z}\), \(\symbf{G}\), \(\symbf{R}\) and their derivatives from the model, applies the residual jitter rule, and evaluates the score, AI matrix and optional restricted log-likelihood.

preprocess_input() exposes that extraction separately and returns a dictionary of tensors. ai_step_from_tensors() is the standalone tensor-level kernel; it builds \(\symbf{W}\), \(\symbf{C}\) and \(\symbf{P}\) and evaluates the score and AI matrix directly from x, y, z, g, r and their derivatives.

c_inverse_blocks() and c_inverse_blocks_from_tensors() return the blocks of the inverse coefficient matrix \(\symbf{C}^{-1}\), which give the fixed-effect covariance and the random-effect prediction-error covariance.

The implementation forms the \(n \times n\) matrices explicitly, so it is intended for problems where they fit in memory; there is no sparse factorisation or matrix-free path.

Methods

ai_step(model, y, x, free_params[, ...])

Evaluate one C-form AI step for model.

ai_step_from_tensors(x, y, z, g, r, dz, dg, dr)

Compute the REML score and average information for one AI step.

c_inverse_blocks(model, x, free_params)

Compute the blocks of the inverse coefficient matrix for model.

c_inverse_blocks_from_tensors(x, z, g, r[, ...])

Compute the blocks of \(\symbf{C}^{-1}\) from tensors.

eligible(model)

Whether model can use the C-form method.

loglik(model, y, x, free_params)

Evaluate the restricted log-likelihood for model.

loglik_from_tensors(x, y, z, g, r[, g_inv, ...])

Evaluate the C-form restricted log-likelihood from tensors.

preprocess_input(model, y, x, free_params[, ...])

Prepare the tensors needed by ai_step().

eligible(model)[source]

Whether model can use the C-form method.

loglik(model, y, x, free_params)[source]

Evaluate the restricted log-likelihood for model.

This is the C-form log-likelihood

\[\ell = -\frac{1}{2}\left( \log|\symbf{C}| + \log|\symbf{R}| + \log|\symbf{G}| + \symbf{y}^\top\symbf{P}\symbf{y} \right),\]

evaluated without forming \(\symbf{V}\) or \(\symbf{P}\).

Parameters:
  • model (MixedModelREML) – Model supplying Z, G and R.

  • y (torch.Tensor) – Response vector of shape (n,).

  • x (torch.Tensor) – Fixed-effect design matrix of shape (n, p).

  • free_params (torch.Tensor) – Free parameters of the model, ordered as [free_params_Z, free_params_G, free_params_R].

Returns:

Scalar restricted log-likelihood.

Return type:

torch.Tensor

Raises:

RuntimeError – If model has no random-effect term or no free parameters.

loglik_from_tensors(x, y, z, g, r, g_inv=None, r_inv=None)[source]

Evaluate the C-form restricted log-likelihood from tensors.

Parameters:
  • x – Fixed-effect design matrix of shape (n, p).

  • y – Response vector of shape (n,).

  • z – Random-effect incidence matrix of shape (n, q).

  • g – Random-effect covariance matrix of shape (q, q).

  • r – Residual covariance matrix of shape (n, n).

  • g_inv – Optional precomputed inverse of g. When supplied it is used for the C-form block instead of forming the inverse from a Cholesky factorisation.

  • r_inv – Optional precomputed inverse of r. When supplied it is used for the C-form block instead of forming the inverse from a Cholesky factorisation.

Returns:

Scalar restricted log-likelihood.

Return type:

torch.Tensor

c_inverse_blocks(model, x, free_params)[source]

Compute the blocks of the inverse coefficient matrix for model.

The coefficient matrix is

\[\begin{split}\symbf{C} = \begin{pmatrix} \symbf{X}^\top\symbf{R}^{-1}\symbf{X} & \symbf{X}^\top\symbf{R}^{-1}\symbf{Z} \\ \symbf{Z}^\top\symbf{R}^{-1}\symbf{X} & \symbf{Z}^\top\symbf{R}^{-1}\symbf{Z} + \symbf{G}^{-1} \end{pmatrix}.\end{split}\]

This is the model-aware wrapper around c_inverse_blocks_from_tensors(). It extracts \(\symbf{Z}\), \(\symbf{G}\) and \(\symbf{R}\) from the model and applies the same residual jitter rule as preprocess_input(), so that the blocks are consistent with the matrices used by ai_step().

Parameters:
  • model (MixedModelREML) – Model supplying Z, G and R.

  • x (torch.Tensor) – Fixed-effect design matrix of shape (n, p).

  • free_params (torch.Tensor) – Free parameters of the model, ordered as [free_params_Z, free_params_G, free_params_R].

Returns:

(c11, c12, c21, c22), the blocks of \(\symbf{C}^{-1}\), with shapes (p, p), (p, q), (q, p) and (q, q).

Return type:

tuple

Raises:

RuntimeError – If model has no random-effect term or no free parameters.

c_inverse_blocks_from_tensors(x, z, g, r, g_inv=None, r_inv=None)[source]

Compute the blocks of \(\symbf{C}^{-1}\) from tensors.

The coefficient matrix is

\[\begin{split}\symbf{C} = \begin{pmatrix} \symbf{X}^\top\symbf{R}^{-1}\symbf{X} & \symbf{X}^\top\symbf{R}^{-1}\symbf{Z} \\ \symbf{Z}^\top\symbf{R}^{-1}\symbf{X} & \symbf{Z}^\top\symbf{R}^{-1}\symbf{Z} + \symbf{G}^{-1} \end{pmatrix},\end{split}\]

and the returned blocks satisfy

\[\begin{split}\symbf{C}^{-1} = \begin{pmatrix} \symbf{C}_{11} & \symbf{C}_{12} \\ \symbf{C}_{21} & \symbf{C}_{22} \end{pmatrix}.\end{split}\]
Parameters:
  • x – Fixed-effect design matrix of shape (n, p).

  • z – Random-effect incidence matrix of shape (n, q).

  • g – Random-effect covariance matrix of shape (q, q).

  • r – Residual covariance matrix of shape (n, n).

  • g_inv – Optional precomputed inverse of g. If None, it is computed from a Cholesky factorisation. A supplied value is used as given.

  • r_inv – Optional precomputed inverse of r. If None, it is computed from a Cholesky factorisation. A supplied value is used as given.

Returns:

(c11, c12, c21, c22), the blocks of \(\symbf{C}^{-1}\), with shapes (p, p), (p, q), (q, p) and (q, q).

Return type:

tuple

preprocess_input(model, y, x, free_params, grad=True)[source]

Prepare the tensors needed by ai_step().

The model supplies \(\symbf{Z}\), \(\symbf{G}\) and \(\symbf{R}\) together with their free-parameter counts. The parameter vector free_params is ordered as [free_params_Z, free_params_G, free_params_R].

Parameters:
  • model (MixedModelREML) – Model supplying Z, G and R.

  • y (torch.Tensor) – Response vector of shape (n,).

  • x (torch.Tensor) – Fixed-effect design matrix of shape (n, p).

  • free_params (torch.Tensor) – Free parameters of the model, ordered as [free_params_Z, free_params_G, free_params_R].

  • grad (bool, optional) – Whether to compute dz, dg and dr. Set to False for loglik-only evaluation. Default: True.

Returns:

Prepared tensors with keys x, y, z, g, r, g_inv and r_inv. When grad is True the derivative keys dz, dg and dr are included as well; when it is False they are omitted.

Return type:

dict

Raises:

RuntimeError – If model has no random-effect term or no free parameters.

ai_step(model, y, x, free_params, require_loglik=True)[source]

Evaluate one C-form AI step for model.

This is the model-aware wrapper around ai_step_from_tensors(). The score and AI matrix are

\[s_k = \frac{1}{2}\left[ \symbf{q}_k^\top\symbf{P}\symbf{y} - \operatorname{tr}\left(\symbf{P}\dot{\symbf{V}}_k\right) \right], \qquad \symbf{I}_{kl} = \frac{1}{2}\symbf{q}_k^\top\symbf{P}\symbf{q}_l,\]

with \(\symbf{q}_k = \dot{\symbf{V}}_k\symbf{P}\symbf{y}\).

Parameters:
  • model (MixedModelREML) – Model supplying Z, G and R.

  • y (torch.Tensor) – Response vector of shape (n,).

  • x (torch.Tensor) – Fixed-effect design matrix of shape (n, p).

  • free_params (torch.Tensor) – Flat covariance-parameter tensor.

  • require_loglik (bool, optional) – Whether to evaluate the REML log-likelihood. Default: True.

Returns:

(score, ai, loglik). loglik is torch.nan when require_loglik is False.

Return type:

tuple

Raises:

RuntimeError – If model has no random-effect term or no free parameters.

ai_step_from_tensors(x, y, z, g, r, dz, dg, dr, g_inv=None, r_inv=None, require_loglik=True)[source]

Compute the REML score and average information for one AI step.

The parameters are ordered as [Z, G, R]. With \(\symbf{z} = \symbf{P}\symbf{y}\), \(\symbf{b} = \symbf{Z}^\top\symbf{z}\) and \(\symbf{A} = \symbf{Z}^\top\symbf{P}\symbf{Z}\), the score entries are

\[\begin{split}s_{Z,k} &= \left(\dot{\symbf{Z}}_k^\top\symbf{z}\right)^\top \symbf{G}\symbf{b} - \operatorname{tr} \left(\symbf{Z}^\top\symbf{P} \dot{\symbf{Z}}_k\symbf{G}\right), \\ s_{G,k} &= \frac{1}{2}\left[ \symbf{b}^\top\dot{\symbf{G}}_k\symbf{b} - \operatorname{tr} \left(\symbf{A}\dot{\symbf{G}}_k\right) \right], \\ s_{R,k} &= \frac{1}{2}\left[ \symbf{z}^\top\dot{\symbf{R}}_k\symbf{z} - \operatorname{tr} \left(\symbf{P}\dot{\symbf{R}}_k\right) \right].\end{split}\]

The corresponding working columns are

\[\begin{split}\symbf{q}_{Z,k} &= \dot{\symbf{Z}}_k\symbf{G}\symbf{b} + \symbf{Z}\symbf{G} \dot{\symbf{Z}}_k^\top\symbf{z}, \\ \symbf{q}_{G,k} &= \symbf{Z}\dot{\symbf{G}}_k\symbf{b}, \\ \symbf{q}_{R,k} &= \dot{\symbf{R}}_k\symbf{z},\end{split}\]

and the AI matrix is

\[\symbf{I}_{kl} = \frac{1}{2}\symbf{q}_k^\top \symbf{P}\symbf{q}_l.\]
Parameters:
  • x – Fixed-effect design matrix of shape (n, p).

  • y – Response vector of shape (n,).

  • z – Random-effect incidence matrix of shape (n, q).

  • g – Random-effect covariance matrix of shape (q, q).

  • r – Residual covariance matrix of shape (n, n).

  • dz – Derivatives of z with shape (kz, n, q). The first dimension is the parameter dimension.

  • dg – Derivatives of g with shape (kg, q, q).

  • dr – Derivatives of r with shape (kr, n, n).

  • g_inv – Optional precomputed inverse of g. If None, it is computed from a Cholesky factorisation. A supplied value is used as given.

  • r_inv – Optional precomputed inverse of r. If None, it is computed from a Cholesky factorisation. A supplied value is used as given.

  • require_loglik – If True, return the restricted log-likelihood. If False, return torch.nan and skip the log-determinant calculations.

Raises:

ValueError – If dz, dg and dr all have a zero-length parameter dimension.

Returns:

(score, ai, loglik), where score has shape (kz + kg + kr,) in [Z, G, R] order, ai has shape (kz + kg + kr, kz + kg + kr), and loglik is a scalar tensor, or torch.nan when require_loglik is False.

Individual empty parameter blocks are supported by passing a zero-length first dimension, for example dz.shape == (0, n, q), provided at least one of dz, dg and dr is non-empty.

Return type:

tuple