A.3 Matrix calculus: gradients, Jacobians and Hessians
What you will learn
- Use one convention for gradients, Jacobians and Hessians, and the handful of rules that cover every derivative in the estimation chapters.
- Differentiate the log-determinant, the inverse and the quadratic form that appear in the objective function.
- Differentiate a subject's conditional objective by hand, and check it against a finite difference.
Prerequisites: A.2.
Conventions
| Object | Definition | Shape |
|---|---|---|
| Gradient of a scalar \(g(x)\), \(x\in\mathbb R^p\) | \(\nabla g=\bigl(\partial g/\partial x_1,\dots,\partial g/\partial x_p\bigr)^{\mathsf T}\) | \(p\times1\) |
| Jacobian of a vector \(Y(x)\in\mathbb R^n\) | \(\bigl[\partial Y_j/\partial x_k\bigr]\), rows are outputs | \(n\times p\) |
| Hessian of a scalar | \(\bigl[\partial^2g/\partial x_k\partial x_l\bigr]\), symmetric | \(p\times p\) |
The sensitivity matrix \(G_i=\partial Y_i/\partial\eta\) of a subject is the Jacobian of its predictions with respect to its random effects: one row per observation, one column per ETA.
The rules
For constant vectors \(a\) and matrices \(A\) (not depending on \(x\)):
| Function | Derivative |
|---|---|
| \(a^{\mathsf T}x\) | \(a\) |
| \(x^{\mathsf T}Ax\) | \((A+A^{\mathsf T})\,x\), which is \(2Ax\) when \(A\) is symmetric |
| \(\lVert x-a\rVert^2\) | \(2(x-a)\) |
| chain rule, \(g(Y(x))\) | \(\bigl(\partial Y/\partial x\bigr)^{\mathsf T}\,\nabla_Y g\) |
For derivatives with respect to a matrix \(A\) that is symmetric and positive definite, with \(\partial A\) a small change in it:
| Function of \(A\) | Change |
|---|---|
| \(\log\lvert A\rvert\) | \(\partial\log\lvert A\rvert=\operatorname{tr}\bigl(A^{-1}\partial A\bigr)\), so the gradient is \(A^{-1}\) |
| \(A^{-1}\) | \(\partial A^{-1}=-A^{-1}(\partial A)A^{-1}\) |
| \(x^{\mathsf T}A^{-1}x\) | \(\partial\bigl(x^{\mathsf T}A^{-1}x\bigr)=-x^{\mathsf T}A^{-1}(\partial A)A^{-1}x\), so the gradient is \(-A^{-1}xx^{\mathsf T}A^{-1}\) |
| \(\operatorname{tr}(BA)\) | \(B^{\mathsf T}\) |
The first two are the ones that matter most. They explain why the derivative of an objective that contains \(\log\lvert\Omega\rvert+\eta^{\mathsf T}\Omega^{-1}\eta\) with respect to \(\Omega\) is \(\Omega^{-1}-\Omega^{-1}\eta\eta^{\mathsf T}\Omega^{-1}\), which is zero when \(\Omega=\eta\eta^{\mathsf T}\) (a one-subject maximum-likelihood estimate of a variance) and, summed over subjects, leads to the closed-form OMEGA update of SAEM (page A.10).
The subject objective and its gradient
For a subject with the Gaussian record type, the conditional objective is
Differentiating term by term, with \(e_{ij}=y_{ij}-Y_{ij}\) and \(\partial_k\) for \(\partial/\partial\eta_k\):
Two things are visible in this formula and matter later.
- If the residual variance does not depend on \(\eta\) (additive error), \(\partial_kV=0\) and only the middle term remains: the gradient is \(-2G^{\mathsf T}R^{-1}e+2\Omega^{-1}\eta\), the familiar weighted least squares plus prior form.
- If it does (proportional error), the first term is present. It is the reason that FOCE and FOCE-I, which differ only in whether \(V\) is evaluated at \(\eta=0\) or at \(\hat\eta_i\), can give different results (page A.8).
The Hessian is the matrix of second derivatives of this expression. nlmx obtains the gradient of \(J_i\) from exact forward-mode automatic differentiation (page A.9) and the Hessian \(H_i\) by finite differences of those gradients; when \(H_i\) is not positive definite it falls back on a Gauss–Newton curvature. The Gauss–Newton form drops the terms with second derivatives of \(Y\) and is positive semi-definite by construction.
A worked illustration
Gradient at \(\eta=-0.2\) for the appendix subject (defined on page A.6: \(f_j(\eta)=\tfrac{D}{V}\exp\bigl(-\tfrac{\mathrm{CL}\,e^{\eta}}{V}t_j\bigr)\) with \(D=100\), \(V=20\), \(\mathrm{CL}=4\), \(\Omega=0.09\), proportional error \(\sigma^2=0.04\), observations \(4.6,\ 2.9,\ 1.0\) at \(t=1,4,12\) h).
The derivative of the prediction follows from the chain rule, \(\partial f_j/\partial\eta=-\tfrac{\mathrm{CL}\,e^{\eta}t_j}{V}\,f_j\), and \(V_j=\sigma^2f_j^2\) gives \(\partial V_j/\partial\eta=2\sigma^2f_j\,\partial f_j/\partial\eta\).
| \(t\) (h) | \(f\) | \(\partial f/\partial\eta\) | \(V=0.04f^2\) | contribution to \(J\) | contribution to \(\partial J/\partial\eta\) |
|---|---|---|---|---|---|
| 1 | 4.2448 | −0.6951 | 0.72073 | −0.1524 | 0.4150 |
| 4 | 2.5973 | −1.7012 | 0.26983 | −0.9703 | 2.9524 |
| 12 | 0.7008 | −1.3771 | 0.019645 | 0.6265 | 55.9201 |
| prior \(\eta^2/\omega^2\) and \(2\eta/\omega^2\) | 0.4444 | −4.4444 | |||
| total | −0.0517 | 54.8430 |
The central difference \(\bigl(J(\eta+h)-J(\eta-h)\bigr)/2h\) with \(h=10^{-4}(1+\lvert\eta\rvert)\) gives \(54.8430\) (the two values agree to six significant digits). The gradient is positive at \(\eta=-0.2\), so the minimum lies at a smaller \(\eta\): the observations, particularly the 12-hour one (1.0 observed against 0.70 predicted at this \(\eta\)), call for a lower clearance than the typical value. The mode is at \(\hat\eta=-0.334\) (page A.8).
Where the derivatives are used
| Derivative | Used for |
|---|---|
| \(\nabla_\eta J_i\) | the conditional-mode search (empirical Bayes estimates) |
| \(G_i=\partial Y_i/\partial\eta\) | the linearised likelihood of FO, FOCE, FOCE-I; the Fisher information of the power analysis |
| \(H_i=\nabla^2_\eta J_i\) | the Laplace objective; the importance-sampling proposal |
| \(\nabla_\psi\mathrm{OFV}_i\) | the score matrix \(S\) of the covariance step |
| \(\nabla^2_\psi\mathrm{OFV}\) | the curvature matrix \(R\) of the covariance step |
| \(\nabla g\) of an effect | the delta-method standard error of an effect or a curve |
Common mistakes
- Differentiating the log-determinant of \(\Omega\) as if it were \(\log\omega\): for a matrix the gradient is \(\Omega^{-1}\), and for a diagonal element \(\omega_{kk}\) it is \(1/\omega_{kk}\).
- Dropping the \(\partial V/\partial\eta\) term when error is proportional. The mode and the curvature are then those of a different method (FOCE, not FOCE-I).
- Mixing scales: a gradient with respect to \(\log\omega\) is not a gradient with respect to \(\omega\) (page A.9).
Review checklist
- Can you write the gradient of \(J_i\) for additive and for proportional error?
- Do you know which derivatives are exact (automatic differentiation) and which are finite differences?
- Can you check a hand derivative against a central difference of the objective?
Navigation
See the previous page, the next page and the mathematical reference.