torch_openreml.methods.CForm¶
- class torch_openreml.methods.CForm[source]¶
Bases:
objectC-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 fromx,y,z,g,rand their derivatives.c_inverse_blocks()andc_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
modelcan 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().- 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,GandR.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
modelhas 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 aspreprocess_input(), so that the blocks are consistent with the matrices used byai_step().- Parameters:
model (MixedModelREML) – Model supplying
Z,GandR.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
modelhas 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. IfNone, it is computed from a Cholesky factorisation. A supplied value is used as given.r_inv – Optional precomputed inverse of
r. IfNone, 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_paramsis ordered as[free_params_Z, free_params_G, free_params_R].- Parameters:
model (MixedModelREML) – Model supplying
Z,GandR.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,dganddr. Set toFalsefor loglik-only evaluation. Default:True.
- Returns:
Prepared tensors with keys
x,y,z,g,r,g_invandr_inv. WhengradisTruethe derivative keysdz,dganddrare included as well; when it isFalsethey are omitted.- Return type:
dict
- Raises:
RuntimeError – If
modelhas 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,GandR.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).loglikistorch.nanwhenrequire_loglikisFalse.- Return type:
tuple
- Raises:
RuntimeError – If
modelhas 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
zwith shape(kz, n, q). The first dimension is the parameter dimension.dg – Derivatives of
gwith shape(kg, q, q).dr – Derivatives of
rwith shape(kr, n, n).g_inv – Optional precomputed inverse of
g. IfNone, it is computed from a Cholesky factorisation. A supplied value is used as given.r_inv – Optional precomputed inverse of
r. IfNone, it is computed from a Cholesky factorisation. A supplied value is used as given.require_loglik – If
True, return the restricted log-likelihood. IfFalse, returntorch.nanand skip the log-determinant calculations.
- Raises:
ValueError – If
dz,dganddrall have a zero-length parameter dimension.- Returns:
(score, ai, loglik), wherescorehas shape(kz + kg + kr,)in[Z, G, R]order,aihas shape(kz + kg + kr, kz + kg + kr), andloglikis a scalar tensor, ortorch.nanwhenrequire_loglikisFalse.Individual empty parameter blocks are supported by passing a zero-length first dimension, for example
dz.shape == (0, n, q), provided at least one ofdz,dganddris non-empty.- Return type:
tuple