Why Derivative Checks Matter
Likelihood optimization depends on derivatives, not just on the value of the likelihood. A gradient that is coded with a sign error can send an update in the wrong direction; a Hessian with a small but consequential error can change the Newton step or the estimated standard errors. Software that returns derivatives through automatic differentiation can reduce algebraic mistakes, but it differentiates the function as implemented. It does not establish that the implementation matches the intended likelihood.
Derivative verification compares a proposed analytic or automatic derivative with a numerical approximation calculated from likelihood values or nearby gradients. The comparison is a diagnostic, not a replacement for deriving the score and Hessian. A numerical approximation also has error, so the step size and the scale of the parameters matter.
Definitions and Numerical Approximations
For a small nonzero step \(h\), the forward-difference approximation to a gradient component is
where \(e_j\) is the vector whose \(j\)th entry is one and whose other entries are zero. A central difference uses evaluations on both sides:
For a sufficiently smooth function, the forward difference has leading truncation error of order \(h\), while the central difference has leading truncation error of order \(h^2\). Central differences are therefore often more accurate for a comparable step size, though they require more function evaluations.
A Hessian can be approximated by differencing gradient vectors, one column at a time:
Alternatively, second differences of the log-likelihood approximate diagonal Hessian entries:
Mixed entries can also be computed from four function evaluations. For distinct coordinates \(j\) and \(k\), a central approximation is
When \(\ell\) is twice continuously differentiable, its exact Hessian is symmetric: \(H_{jk}=H_{kj}\). A numerically estimated Hessian may not be exactly symmetric because of approximation error. A large asymmetry is a warning to investigate, not a reason to conceal the discrepancy by symmetrizing the matrix immediately.
Worked Example: Checking a Scalar Gradient and Hessian
Consider the hypothetical log-likelihood \(\ell(\theta)=-\tfrac12(\theta-2)^2\), evaluated at \(\theta=1.2\). Its analytic derivatives are \(g(\theta)=-(\theta-2)\) and \(H(\theta)=-1\), so \(g(1.2)=0.8\) and \(H(1.2)=-1\). Check them with central differences using \(h=0.01\).
The three log-likelihood values are \(\ell(1.20)=-0.32000\), \(\ell(1.21)=-0.31205\), and \(\ell(1.19)=-0.32805\). The central gradient approximation is
This agrees with the analytic gradient. The second-difference approximation is
It agrees with the analytic Hessian as well. The agreement is exact here because the log-likelihood is quadratic, so the central-difference formulas have no higher-order truncation error for these derivatives.
Directional Checks
Coordinate-by-coordinate checks are useful, but a multivariate derivative can also be tested along a direction that changes several parameters at once. For a direction \(v\), the directional derivative of \(\ell\) at \(\theta\) is \(v'g(\theta)\). If \(v\) is a unit vector, this is the rate of change per unit distance in that direction. A central numerical check is
The second directional derivative is \(v'H(\theta)v\). It can be checked using
Directional checks can reveal errors that are less obvious in individual components, particularly when a parameter vector has many coordinates. Several directions can be tried, including unit directions and directions with mixed signs. A successful check in one direction is not proof that every component is correct, so directional checks complement rather than replace coordinate checks.
Worked Example: Checking Gradient and Curvature Along a Direction
Use the hypothetical two-parameter log-likelihood \(\ell(\theta_1,\theta_2)=-\theta_1^2-\theta_1\theta_2-1.5\theta_2^2+4\theta_1-\theta_2\). At \(\theta=(1,2)'\), its value is \(-7\), its gradient is \(g(\theta)=(0,-8)'\), and its Hessian is \(H(\theta)=\begin{pmatrix}-2&-1\\-1&-3\end{pmatrix}\). Take the unit direction \(v=(0.6,0.8)'\) and \(h=0.01\).
The analytic directional derivative is \(v'g(\theta)=0.6(0)+0.8(-8)=-6.4\). Since this log-likelihood is quadratic, its change at displacement \(h v\) is \(h v'g(\theta)+\tfrac12h^2v'Hv\). Here \(v'Hv=-3.6\), so \(\ell(\theta+h v)=-7+0.01(-6.4)+\tfrac12(0.01)^2(-3.6)=-7.06418\). Likewise, \(\ell(\theta-h v)=-7+0.01(6.4)+\tfrac12(0.01)^2(-3.6)=-6.93618\).
The numerical directional derivative is \[ \frac{-7.06418-(-6.93618)}{0.02}=-6.4, \] matching the analytic result. The numerical directional curvature is \[ \frac{-7.06418-2(-7)-6.93618}{0.0001} = \frac{-0.00036}{0.0001} = -3.6, \] which also matches \(v'Hv\). Both checks use the same direction and are consistent with the proposed gradient and Hessian.
Choosing a Step Size
A step that is too large creates truncation error: the difference quotient averages behavior over a region where curvature may matter. A step that is too small can also be inaccurate. In floating-point arithmetic, two nearly equal log-likelihood values are subtracted; rounding error or numerical noise in the likelihood evaluation can then overwhelm their small difference. For a first difference, this error often grows roughly as the evaluation error divided by \(h\). For a second difference, it can grow roughly as the evaluation error divided by \(h^2\).
These competing errors explain why reducing \(h\) indefinitely does not make a numerical derivative more accurate. In practice, compare results over a sensible range of step sizes. A central-difference gradient should often become more stable as \(h\) is reduced initially, then become less stable when cancellation or evaluation noise dominates. Hessian approximations can be especially sensitive because they divide by \(h^2\).
Parameter scaling matters. If one parameter is naturally near \(1000\) and another near \(0.01\), using the same absolute step for both may perturb one too little and the other too much. Choose steps relative to parameter magnitudes or meaningful parameter scales, and check that the perturbed values remain in a region where the likelihood can be evaluated. For likelihoods with numerical integration, simulation, or other noisy calculations, the evaluation noise must also be considered; an extremely small step may make the approximation unusable.
A useful diagnostic reports both absolute and relative discrepancies. For a proposed gradient \(g\) and numerical approximation \(\tilde g\), one possible componentwise comparison is \(\lvert g_j-\tilde g_j\rvert/\max(1,\lvert g_j\rvert,\lvert\tilde g_j\rvert)\). The maximum with one avoids an unstable relative error when both values are close to zero. The acceptable discrepancy depends on the scale, precision, and downstream use; there is no universal threshold that certifies correctness.
Worked Example: Choosing Scaled Steps for Two Parameters
Suppose a hypothetical model is evaluated at \(\theta=(120,0.03)'\), and these parameters have meaningful scales of roughly \(100\) and \(0.01\), respectively. A proposed central-difference rule uses a step equal to one-thousandth of each scale. The coordinate steps are therefore \(h_1=0.1\) and \(h_2=0.00001\).
For the first parameter, the gradient check compares \(\ell(120.1,0.03)\) and \(\ell(119.9,0.03)\), dividing their difference by \(0.2\). For the second, it compares \(\ell(120,0.03001)\) and \(\ell(120,0.02999)\), dividing by \(0.00002\). Each perturbation is the same relative size on its parameter's meaningful scale.
This rule is a starting point, not a guarantee. Repeat the checks with somewhat smaller and larger relative steps. If the second-coordinate estimate changes sharply as \(h_2\) decreases, subtraction or likelihood-evaluation noise may dominate. If it changes as \(h_2\) increases, truncation error may be important. The data in this example specify a step-size strategy, not numerical derivative values; those values require evaluating the particular likelihood.
What Derivative Errors Do to Estimation
For a log-likelihood maximization problem, a Newton–Raphson update uses the gradient and Hessian, conventionally in the form \(\theta_{\text{new}}=\theta-H(\theta)^{-1}g(\theta)\) when the inverse and update are well-defined. An incorrect gradient changes the proposed direction and size of the step. An incorrect Hessian changes the curvature adjustment. A line search or trust-region procedure can limit a poor step, but it cannot make incorrect derivatives correct.
At a converged interior maximum, the observed-information estimate is \(J(\hat\theta)=-H(\hat\theta)\). Under the usual likelihood regularity conditions, an inverse of this matrix is used as an estimated covariance matrix, and its diagonal entries determine estimated standard errors. Thus, an erroneous Hessian can affect both the optimizer and reported uncertainty. The effect can be amplified when the information matrix is poorly conditioned: a small perturbation to its entries can produce a much larger change in its inverse.
Automatic differentiation is valuable because it applies the chain rule to the computational operations defining the likelihood, often more accurately than finite differences. It is still important to compare its output with independent checks at representative parameter values. Such comparisons can identify incorrect indexing, omitted likelihood contributions, inconsistent parameter transformations, or mistakes in the function being differentiated. Finite differences cannot tell which implementation is substantively correct, but disagreement is a reason to investigate before trusting the fit.
Common Pitfalls and a Verification Workflow
- Making the step arbitrarily tiny. Smaller steps reduce truncation error only up to a point; cancellation and evaluation noise can then dominate. Check stability across several scaled step sizes.
- Checking only at a stationary point. A gradient near zero can make some errors hard to see. Verify derivatives at more than one valid parameter value, including points away from the optimum.
- Comparing only one directional derivative. Agreement in one direction does not verify all gradient components or Hessian entries. Combine coordinate checks with multiple directional checks.
- Assuming automatic differentiation validates the model. It differentiates the implemented computation. Independently confirm that the likelihood code represents the intended model.
- Treating a numerical Hessian's asymmetry as harmless. Small asymmetry can result from numerical error, but substantial asymmetry calls for investigation of the derivative calculations, step sizes, or likelihood evaluations.
A practical sequence is to evaluate the likelihood at the test point, compare analytic or automatic gradient components with central differences, compare Hessian columns with differences of gradients, and check symmetry and several directional derivatives. Repeat at different step sizes and parameter values. Investigate any discrepancy before interpreting an optimizer's convergence or its standard errors.
Check Your Understanding
Answer each question using the definitions and diagnostics in this tutorial.
- Why can a forward-difference gradient be less accurate than a central-difference gradient at the same step size?
- What two competing sources of error make it unhelpful to shrink a finite-difference step indefinitely?
- For a direction \(v\), state the formulas for the first and second directional derivatives in terms of the gradient and Hessian.
- Why can a small Hessian error have a large effect on estimated standard errors?
- An automatic gradient agrees with a finite-difference check at one parameter vector but disagrees at another. Give two reasons to investigate before trusting either result.