Why Check the Gradient?
An optimizer can report convergence even when its final parameter vector is not close enough to a stationary point for reliable inference. A gradient check asks whether the objective still has a meaningful local direction of decrease. In a GLMM, this is a numerical diagnostic of the fitted optimization problem: it does not by itself establish that the model is appropriate, that its parameters are identifiable, or that the optimizer found the best solution.
The raw gradient is informative but depends on the scale used to express each parameter. A component can be numerically large because its parameter is measured on a small scale, or numerically small because its parameter is measured on a large scale. Scaling the gradient by local curvature provides a way to assess components on a more comparable basis, provided that the curvature calculation is reliable and the solution is locally interior.
A raw gradient component has units of objective per unit of its parameter. Thus, comparing \(|g_j|\) directly across parameters with different units or scales can be misleading. A scaled gradient adjusts for the local curvature of the objective. For the definitions below, let \(\mathbf{H}=\nabla^2Q(\boldsymbol{\theta})\) be the Hessian at the candidate solution. When \(\mathbf{H}\) is positive definite, the symmetric curvature-scaled gradient is \(\mathbf{H}^{-1/2}\mathbf{g}\), and its length is \(\sqrt{\mathbf{g}^{\mathsf T}\mathbf{H}^{-1}\mathbf{g}}\). These are local diagnostics, not universal pass-or-fail tests: software and optimization methods may use different scaling conventions or tolerances.
What Curvature Scaling Measures
Curvature indicates how rapidly the objective changes as a parameter moves locally. A direction with high curvature is steep even for a small displacement; a direction with low curvature may require a larger displacement to produce a comparable change. A curvature-scaled gradient expresses the gradient in relation to this local geometry. The scaling should not be confused with simply dividing each component by an arbitrary constant or by the parameter estimate.
An implementation may instead use a Cholesky factor. If \(\mathbf{H}=\mathbf{R}^{\mathsf T}\mathbf{R}\), with \(\mathbf{R}\) upper triangular, define \(\mathbf{z}_{\mathrm{chol}}\) by solving \(\mathbf{R}^{\mathsf T}\mathbf{z}_{\mathrm{chol}}=\mathbf{g}\). This gives \(\mathbf{z}_{\mathrm{chol}}=\mathbf{R}^{-\mathsf T}\mathbf{g}\). It generally does not give the same component vector as \(\mathbf{H}^{-1/2}\mathbf{g}\) when \(\mathbf{H}\) is non-diagonal. However, the two vectors have the same squared norm:
This distinction matters when interpreting individual scaled components. The norm summarizes the size of the gradient in the local curvature metric, but the component values depend on the scaling transformation and parameterization. When reporting or interpreting a component-wise check, state the convention used if it is known. A small norm can support a conclusion that the local gradient is small; it does not guarantee that each component under every possible scaling convention will look identical.
Worked Example: Raw Gradients for Differently Scaled Coefficients
Consider a hypothetical smooth objective for two coefficients, \(\theta_1\) and \(\theta_2\), near a candidate solution \((\hat\theta_1,\hat\theta_2)\). Suppose its local quadratic form is exactly \(Q-Q_{\min}=\tfrac12[10^{-4}(\theta_1-\hat\theta_1)^2+10^4(\theta_2-\hat\theta_2)^2]\). At the reported point, let \(\theta_1-\hat\theta_1=10\) and \(\theta_2-\hat\theta_2=0.001\). This hypothetical example isolates the effect of different curvature scales.
The gradient is \(\mathbf{g}=(10^{-4}(\theta_1-\hat\theta_1),\,10^4(\theta_2-\hat\theta_2))^{\mathsf T}\), so substitution gives \(\mathbf{g}=(10^{-4}\times10,\,10^4\times0.001)^{\mathsf T}=(0.001,10)^{\mathsf T}\). The second raw component is much larger numerically. But the Hessian is \(\mathbf{H}=\operatorname{diag}(10^{-4},10^4)\), so the symmetric scaled gradient is \(\mathbf{H}^{-1/2}\mathbf{g}=(0.001/0.01,\,10/100)^{\mathsf T}=(0.1,0.1)^{\mathsf T}\).
The raw components differ by a factor of 10,000, while the scaled components are equal. The associated norm is \(\sqrt{0.1^2+0.1^2}=\sqrt{0.02}\approx0.1414\). The raw comparison alone would overstate how different the two coordinates are in the local curvature scale. Whether a scaled value of this size meets a particular software tolerance depends on the convention and tolerance being used; no universal cutoff follows from this calculation.
Derivation: Link to the Local Newton Step
The curvature-scaled gradient is motivated by the local quadratic approximation. Assume \(Q\) has continuous second derivatives near the candidate point and that the Hessian there is positive definite. For a small displacement \(\boldsymbol{\delta}\), the second-order Taylor approximation is
The quadratic approximation is minimized by setting its gradient with respect to \(\boldsymbol{\delta}\) to zero. Differentiating the linear and quadratic terms gives \(\mathbf{g}+\mathbf{H}\boldsymbol{\delta}=\mathbf{0}\), hence the Newton displacement is \(\boldsymbol{\delta}_{N}=-\mathbf{H}^{-1}\mathbf{g}\). Substituting this displacement into the quadratic approximation gives the predicted decrease:
Thus the scaled-gradient norm is related to the amount by which a local quadratic approximation predicts the objective could decrease. This is a justification for checking it, not a promise that the actual objective will decrease by that amount: the approximation may be poor, numerical derivatives may be inaccurate, and a proposed displacement may not be feasible. At an unconstrained interior minimum, the gradient is zero. At a constrained minimum on a boundary, the ordinary gradient need not be zero; feasibility and the relevant constrained optimality conditions also matter.
Worked Example: Computing a Scaled Gradient Norm
Suppose a hypothetical fit has raw gradient \(\mathbf{g}=(0.02,-0.03)^{\mathsf T}\) and a positive-definite diagonal Hessian \(\mathbf{H}=\operatorname{diag}(4,9)\). Assume these values use the same parameter coordinates and objective scale. The symmetric inverse square root is \(\mathbf{H}^{-1/2}=\operatorname{diag}(1/2,1/3)\). Therefore, \(\mathbf{z}_{\mathrm{sym}}=(0.02/2,-0.03/3)^{\mathsf T}=(0.0100,-0.0100)^{\mathsf T}\).
The norm is \(\|\mathbf{z}_{\mathrm{sym}}\|=\sqrt{0.0100^2+(-0.0100)^2}=\sqrt{0.000200}=0.01414\). For comparison, the raw gradient norm is \(\sqrt{0.02^2+(-0.03)^2}=\sqrt{0.0013}\approx0.03606\). The scaled norm is smaller because the Hessian indicates appreciable curvature in both coordinates. These numbers describe the local objective geometry; neither one is a p-value or a probability, and neither alone establishes that the fit is acceptable.
Checking Components in Practice
A useful check begins by confirming what the reported gradient refers to: the objective being minimized, the parameterization, the parameter ordering, and whether the components are raw or scaled. Then inspect both the component pattern and an overall norm if available. A single large scaled component may indicate that the corresponding coordinate has not settled, but a component can also be inflated by a noisy derivative or an unreliable curvature estimate. Conversely, small components do not rule out problems elsewhere in the fit.
The Cholesky convention illustrates why a component check needs care. With a non-diagonal Hessian, the Cholesky-scaled components can mix information across coordinates. The symmetric and Cholesky vectors have equal lengths, but an individual entry in one vector is not generally the same as the corresponding entry in the other.
Worked Example: Cholesky Components Need Not Match
Let a hypothetical positive-definite Hessian and raw gradient be \(\mathbf{H}=\begin{pmatrix}4&2\\2&4\end{pmatrix}\) and \(\mathbf{g}=(1,0)^{\mathsf T}\). The inverse is \(\mathbf{H}^{-1}=\tfrac1{12}\begin{pmatrix}4&-2\\-2&4\end{pmatrix}\). Thus the squared scaled norm is \(\mathbf{g}^{\mathsf T}\mathbf{H}^{-1}\mathbf{g}=4/12=1/3\), and the norm is \(1/\sqrt{3}\approx0.5774\).
For the Cholesky convention, take \(\mathbf{R}=\begin{pmatrix}2&1\\0&\sqrt{3}\end{pmatrix}\), for which \(\mathbf{R}^{\mathsf T}\mathbf{R}=\mathbf{H}\). Solve \(\mathbf{R}^{\mathsf T}\mathbf{z}_{\mathrm{chol}}=\mathbf{g}\): \(2z_1=1\), so \(z_1=0.5\); then \(z_1+\sqrt{3}z_2=0\), so \(z_2=-1/(2\sqrt{3})\approx-0.2887\). The vector is approximately \((0.5,-0.2887)^{\mathsf T}\), with squared norm \(0.25+1/12=1/3\).
The symmetric inverse-square-root vector is different. The eigenvectors of \(\mathbf{H}\) are along \((1,1)^{\mathsf T}\) and \((1,-1)^{\mathsf T}\), with eigenvalues 6 and 2. Applying \(\mathbf{H}^{-1/2}\) to \(\mathbf{g}\) gives approximately \((0.5577,-0.1494)^{\mathsf T}\), again with squared norm \(1/3\). The components differ, but the norm agrees. Treating the Cholesky result as though it were the symmetric inverse-square-root result would therefore give incorrect component interpretations.
Assumptions, Diagnostics, and Failure Modes
The calculations above presume a smooth objective and derivatives evaluated at the reported parameter vector. Curvature scaling as written also requires a positive-definite Hessian. If the Hessian is indefinite, singular, or numerically unreliable, its inverse square root or Cholesky factor may not exist or may be unstable. In that situation, a scaled-gradient result may be unavailable or misleading; it is not a reason to declare convergence. Checking the Hessian and its numerical properties is a separate diagnostic.
In practice, gradients may be analytic, automatic-differentiation, or finite-difference approximations. Finite differences can be sensitive to step size and rounding, especially when parameters have very different scales. If a gradient looks suspicious, useful follow-up checks include evaluating it again with a suitable derivative method or step size, checking that the objective is stable near the estimate, and considering a refit from a nearby starting point. Optimizer convergence messages provide context, but a success message is not a substitute for checking the returned parameter values and numerical diagnostics.
Boundary constraints require additional care. Variance parameters in GLMMs may be represented using constrained scales or transformed coordinates. A raw gradient in one parameterization is not directly comparable to a gradient in another: under a change of coordinates, gradient components change. At a boundary, the unconstrained condition \(\mathbf{g}=\mathbf{0}\) need not characterize a constrained optimum. Interpret the check in the parameterization actually optimized, and do not use an unconstrained scaled-gradient calculation as a stand-in for constrained optimality conditions.
Confirm whether the fit minimizes a negative log likelihood or another objective, and establish which parameter each component represents.
Raw magnitudes depend on parameter units. For scaled components, determine the scaling convention and whether the curvature calculation is usable.
Check derivative reliability, local objective behavior, and the optimizer output. A concerning component is a diagnostic signal, not a proof of a particular failure.
Check whether the candidate lies at a feasible boundary, where ordinary zero-gradient reasoning may not apply.
Common Pitfall and Practical Comparison
Wrong reasoning: “One raw gradient component is much larger than the others, so that parameter must be the one that failed to converge.” This conclusion ignores parameter units and curvature. Correction: examine the scaled diagnostic and the underlying numerical calculation, while remembering that even scaled components depend on parameterization and scaling convention. A large raw component can be compatible with modest curvature-scaled movement; a small raw component can still matter in a low-curvature direction.
Raw-gradient checks are simple and can help reveal gross derivative problems, but their components are difficult to compare across differently scaled parameters. Curvature-scaled checks adjust for local curvature and are often more useful for judging local stationarity, but require a reliable curvature estimate and a stated convention. Optimizer convergence messages summarize the optimizer's own stopping rules; they are complementary evidence, not alternatives that make gradient inspection unnecessary. No single check diagnoses every issue, and a favorable gradient check does not imply that the model is identifiable or that its estimates are substantively trustworthy.
Check Your Understanding
Use the definitions and calculations above to answer the questions. Brief solutions follow.
- If the raw gradient is \((0.001,10)^{\mathsf T}\) and the Hessian is \(\operatorname{diag}(10^{-4},10^4)\), calculate the symmetric scaled gradient.
- For a positive-definite Hessian \(\mathbf{H}=\mathbf{R}^{\mathsf T}\mathbf{R}\), does solving \(\mathbf{R}^{\mathsf T}\mathbf{z}=\mathbf{g}\) generally give the symmetric inverse-square-root scaled vector? What property do the two vectors share?
- Why can a large raw gradient component be a poor basis for identifying which parameter is least stationary?
- What is a key limitation of curvature scaling when the Hessian is not positive definite or is numerically unreliable?
Solutions. (1) The inverse square-root is \(\operatorname{diag}(100,0.01)\), so the scaled gradient is \((0.1,0.1)^{\mathsf T}\). (2) No: the Cholesky solve gives \(\mathbf{R}^{-\mathsf T}\mathbf{g}\), which generally has different components from \(\mathbf{H}^{-1/2}\mathbf{g}\); their squared norms both equal \(\mathbf{g}^{\mathsf T}\mathbf{H}^{-1}\mathbf{g}\). (3) Raw components have units of objective per parameter unit, so their magnitudes depend on parameter scales and units. (4) The inverse-square-root scaling may not exist or may be unstable, so the resulting diagnostic cannot be trusted as a convergence check.