Sensitivities and Hessians
Source:vignettes/articles/methods-sensitivities.Rmd
methods-sensitivities.RmdThe derivatives of the objective come from the sensitivities of the ODE solution and the chain rule through every function object, without finite differences. Guide: Sensitivities; integrators: cppDE.
| sweep | returns |
|---|---|
| forward | gradient, Gauss-Newton Hessian |
| forward over forward | exact Hessian |
| reverse | gradient |
| forward over reverse | exact Hessian |
Forward sensitivities
The sensitivities \(S = \partial x / \partial \theta\) of \(\dot x = f(x, \theta)\), \(x(t_0) = x_0(\theta)\), solve
\[\dot S = \frac{\partial f}{\partial x}\, S + \frac{\partial f}{\partial \theta}, \qquad S(t_0) = \frac{\partial x_0}{\partial \theta}, \tag{1}\]
\(n_x n_\theta\) further equations.
cppDE evaluates \(f\) on dual numbers,
one tangent per parameter, and steps states and tangents together. The
step-size control includes the tangents (sensErrCon), so
the sensitivities are as accurate as the states (forward
sensitivities). An event passes the tangents through the saltation
matrix of the jump (events).
Every further function object of g*x*p multiplies by the
Jacobian of that object, e.g. \(g_x S +
g_\theta\) for \(y = g(x,
\theta)\).
Gradient and Hessian of the objective
Each data point adds, up to a constant,
\[\ell_j = r_j^2 + \log \sigma_j^2, \qquad r_j = \frac{g_j - y_j}{\sigma_j}, \tag{2}\]
the summand of the data term. For fixed \(\sigma_j\) and \(J = \partial r / \partial \theta\), the gradient is \(2 J^\top r\) and the Hessian
\[\nabla^2 \sum_j r_j^2 = 2 J^\top J + 2 \sum_j r_j\, \nabla^2 r_j . \tag{3}\]
The Gauss-Newton term \(2 J^\top J\) is positive semidefinite and needs first-order sensitivities; the second term matters for large residuals or strong nonlinearity.
Gauss-Newton with an error model
For \(\sigma_j(\theta)\), the Hessian of \(\ell_j\) in \((g_j, \sigma_j)\) is
\[L_j = \frac{2}{\sigma_j^2} \begin{pmatrix} 1 & -2 r_j \\ -2 r_j & 3 r_j^2 - 1 \end{pmatrix}, \qquad \det L_j = -\frac{4\,(1 + r_j^2)}{\sigma_j^4} < 0. \tag{4}\]
The Gauss-Newton Hessian of normL2() is \(\sum_j J_j^\top L_j J_j\), \(J_j\) the Jacobian of \((g_j, \sigma_j)\): exact curvature of \(\ell_j\), no second derivatives of \(g_j\) and \(\sigma_j\). By (4) \(L_j\) has one negative eigenvalue at every
residual, and the \(\sigma\sigma\)
entry \(3 r_j^2 - 1\) is negative where
residuals are small, so the matrix is in general indefinite along
directions that move a sigma. The exact Hessian adds the second
derivatives of \(g_j\) and \(\sigma_j\), weighted by \(2 r_j / \sigma_j\) and \(2 (1 - r_j^2) / \sigma_j\).
Forward over forward
Differentiating (1) again gives \(n_x n_\theta (n_\theta + 1) / 2\) equations and the exact Hessian, for small models.
Reverse sweep
The gradient of the objective alone follows from one backward sweep with the multipliers \(\lambda(t)\), zero after the last measurement, and the terms \(\ell_j\) of the data at the measurement times \(t_j\),
\[\begin{aligned} \dot\lambda &= -\left(\frac{\partial f}{\partial x}\right)^{\!\top} \lambda, \qquad \lambda(t_j^-) = \lambda(t_j^+) + \left(\frac{\partial \ell_j}{\partial x}\right)^{\!\top}, \\ \frac{\mathrm{d}(-2 \log L)}{\mathrm{d}\theta} &= \int \lambda^\top \frac{\partial f}{\partial \theta}\, \mathrm{d}t + \lambda(t_0)^\top \frac{\partial x_0}{\partial \theta} + \sum_j \frac{\partial \ell_j}{\partial \theta} . \end{aligned} \tag{5}\]
cppDE uses the discrete adjoint of the integrator steps, the
exact derivative of the computed objective, with checkpoints of the
forward trajectory (reverse
mode). The cost is a few times the value, independent of \(n_\theta\). The value pass solves without
sensitivities, and the sweep uses the step grid of that pass.
Pimpl() passes the sweep through the Jacobian of the root
(Steady
states).
Accuracy of a reverse gradient
By default the adjoint follows the step grid of the states without error control.
option of optionsReverse
|
effect |
|---|---|
refine = TRUE |
every step held to the CVODES backward error test under the solve tolerances; a failing step is swept again in substeps |
gradtol |
the share of each step in the gradient added to that test as absolute tolerance |
A trust region converges with a gradient whose error is small
relative to the gradient [1], [2]. gradtol
bounds the error of every step in absolute terms, and the errors of the
steps add up over the sweep; near the optimum, where the gradient
vanishes, a fixed gradtol does not keep the relative error
small (checking
the sweep).
A refined sweep differentiates the trajectory with substeps, not the
computed one. Under refine it therefore returns, per seed,
the first-order change of the seeded functional between the two: over
the steps, the adjoint at the step end times the difference of the end
states, plus the seeds times the difference of the observed values (value
correction). The normL2() seed gives the change of the
objective, a second seed the change of chi2. The reverse
objective adds both, so value and gradient belong to one function, and a
value-only call also sweeps. The corrected value is not necessarily
closer to the exact one; accepted steps get no correction.
Forward over reverse
Forward tangents, one per parameter, ride on the backward sweep, which returns one Hessian column per tangent. Cost grows like a forward solve; the \(n_\theta^2\) Hessian entries are accumulated, but no \(n_x n_\theta^2\) second-order sensitivities are integrated or stored (forward over reverse).
Second derivatives along directions
FOCEI (Nonlinear mixed-effects models) needs traces \(\operatorname{tr}(M\, \partial H_i / \partial \psi)\) for the volume term and the joint Hessian along random-effect directions. Random effects of different groups never share a condition, so the \(k\)-th random effect of all groups shares one tangent \(v_k\), and one forward pass along \(K\) tangents serves all groups. A backward sweep with seed \(w\) on the predictions and sigmas of the data rows, and \(\dot w_k\) along tangent \(k\), returns \(J^\top w\) and
\[\sum_r w_r\, \nabla^2 z_r\, v_k + J^\top \dot w_k \tag{6}\]
for every \(k\), \(z_r\) prediction or sigma of row \(r\), \(J\) their Jacobian. With the gradient of the row loss as seed and \(L_r J_r v_k\) as tangent derivative, \(L_r\) the Hessian (4), the sweep gives \(\Phi_i^{\eta\eta}\), \(\Phi_i^{\eta\psi}\) and the mode shift \(\mathrm{d}\hat\eta / \mathrm{d}\psi = -(\Phi_i^{\eta\eta})^{-1} \Phi_i^{\eta\psi}\). The trace gradients are linear in \(M\): seed \(j\) is \(2 L_r J_{r,j}\), with tangent derivative the derivative of \(L_r\) contracted with \(J_{r,j}\) and \(J_{r,k}\). With \(U_{jk}\) slice \(k\) of the result for seed \(j\),
\[\nabla \operatorname{tr}(M H_i) = \sum_{j,k} M_{jk}\, U_{jk} \tag{7}\]
for every symmetric \(M\): \(K\) seeds for any number of matrices. Hessian rows and trace seeds go back in one sweep, every cotangent with \(K + 1\) blocks, the seed and the derivative of the seed along each tangent.