Tutorials › Pharmacometric Workflows and Reproducible Modeling › Finite-Difference Gradient Noise

Debugging Model Failures · Tutorial 156 of 399

Finite-Difference Gradient Noise

Compare finite-difference gradients across perturbation sizes to distinguish truncation error from numerical evaluation noise and choose scale-aware diagnostic steps.

Advanced 12 min read

What You'll Learn

  • Define forward and central finite-difference estimates and their smoothness assumptions.
  • Explain how objective-evaluation error is amplified when the perturbation becomes too small.
  • Compare estimates across perturbation sizes to identify truncation-dominated and noise-dominated behavior.
  • Convert perturbations and gradients consistently when parameters are rescaled.
  • Distinguish finite-difference instability from evidence about the underlying model or likelihood.
  • Use numerical examples to assess solver accuracy and select practical gradient diagnostics.

Why Finite-Difference Gradients Become Noisy

An optimizer may report a gradient, but that number can be an estimate formed by subtracting two nearly equal objective values. If the objective is a numerically evaluated pharmacometric likelihood, small integration or floating-point errors in those values can become large relative to their difference. The symptom is a gradient that changes markedly when the perturbation is changed, even though the parameter and model have not changed.

The central diagnostic is to calculate a derivative estimate at several perturbation sizes, using the same parameter coordinate and a controlled evaluation procedure. Large steps may show truncation error because the local slope changes across the step. Very small steps may amplify errors in objective evaluation. A useful estimate usually lies between these regimes, if such a range exists.

Definition: For a scalar objective \(F(\theta)\), the forward finite-difference estimate at step \(h>0\) is \([F(\theta+h)-F(\theta)]/h\). The central finite-difference estimate is \([F(\theta+h)-F(\theta-h)]/(2h)\). Both approximate \(F'(\theta)\), but their accuracy depends on the smoothness of \(F\), the step size, and the accuracy of each objective evaluation.

In model fitting, \(F\) might be the negative log-likelihood and \(\theta\) one component of a parameter vector. The formulas estimate a partial derivative while holding the other parameters fixed. For a vector parameter, apply the same calculation separately to each coordinate; the resulting components form an approximate gradient. The step \(h\) has the units of the parameter being perturbed, while the derivative has units of objective per parameter unit.

Truncation Error and Evaluation Error

First consider an exact, smoothly evaluated objective. If \(F\) has a continuous second derivative near \(\theta\), Taylor expansion gives \(F(\theta+h)=F(\theta)+hF'(\theta)+\frac{h^2}{2}F''(\theta)+o(h^2)\). Dividing the difference by \(h\) shows that the forward estimate is \(F'(\theta)+\frac{h}{2}F''(\theta)+o(h)\). Its leading truncation error is generally proportional to \(h\).

For the central estimate, assume \(F\) has a continuous third derivative near \(\theta\). The expansions on the two sides have remainders \(o(h^3)\), not necessarily \(O(h^4)\):

$$ F(\theta\pm h)=F(\theta)\pm hF'(\theta)+\frac{h^2}{2}F''(\theta)\pm\frac{h^3}{6}F'''(\theta)+o(h^3). $$

Subtracting the minus expansion from the plus expansion cancels the even-power terms. Dividing by \(2h\) gives the central estimate \(F'(\theta)+\frac{h^2}{6}F'''(\theta)+o(h^2)\). Thus its leading truncation error is generally proportional to \(h^2\), under the stated smoothness condition. The stronger claim of an \(O(h^4)\) remainder for the quotient requires stronger smoothness assumptions; continuous third derivative alone does not justify it.

Actual objective evaluations are not exact. Write the computed value as \(\widetilde F(x)=F(x)+e(x)\), where \(e(x)\) is evaluation error. If \(|e(x)|\leq\varepsilon\) at each perturbed point, the error contribution to a central difference is bounded by

$$ \left|\frac{e(\theta+h)-e(\theta-h)}{2h}\right| \leq \frac{2\varepsilon}{2h} =\frac{\varepsilon}{h}. $$

The analogous bound for the forward difference is \(2\varepsilon/h\), since it uses errors at \(\theta+h\) and \(\theta\). These bounds explain the basic tradeoff: truncation tends to decrease as \(h\) shrinks, while evaluation error can grow like \(1/h\). If errors at the two central evaluations are independent with standard deviation \(\sigma\), the standard deviation of their difference divided by \(2h\) is \(\sigma/(\sqrt{2}h)\). This stochastic calculation is a model for variability, not a guarantee that errors from a deterministic solver are independent.

Diagnostic principle: A derivative estimate that changes systematically as \(h\) decreases may first be converging away from truncation error, then becoming dominated by evaluation noise. A stable range across several \(h\) values is more informative than a single estimate. Stability is a diagnostic, not proof that the gradient is correct.

Worked Example: Detecting a Noise-Dominated Step

Suppose a hypothetical objective near \(\theta=2\) is \(F(\theta)=12+3(\theta-2)+0.4(\theta-2)^2\). Its exact derivative at 2 is 3 objective units per parameter unit. The quadratic term is symmetric about 2, so an exact central difference equals 3 for every step. A numerical evaluator gives the following hypothetical values:

Step \(h\)Computed \(\widetilde F(2+h)\)Computed \(\widetilde F(2-h)\)Central estimate
0.112.304111.70393.001
0.0112.0300511.969953.005
0.00112.003000511.99699953.0005
0.000112.0005511.999455.5

For \(h=0.01\), the arithmetic is \((12.03005-11.96995)/(2(0.01))=0.06010/0.02=3.005\). For \(h=0.0001\), it is \((12.00055-11.99945)/0.0002=0.00110/0.0002=5.5\). The true objective difference at the smallest step is only \(0.0006\); the computed difference is \(0.0011\). Small evaluation errors have therefore had a large effect on the quotient. The jump to 5.5 is evidence that this step is unreliable, not evidence that the exact derivative suddenly changed.

A practical follow-up is to repeat evaluations at the same perturbed points, inspect solver tolerances and objective precision, and compare intermediate step sizes. If repeated values vary, quantify that variability rather than treating the last displayed digits as exact.

Parameter Scale Changes the Meaning of a Step

A fixed numerical step is not equally informative for every parameter. A perturbation of 0.001 may be tiny for a parameter near 100 and substantial for one near 0.002. The earlier material on parameter scaling and ill-conditioning motivates making the coordinate scale explicit here: gradient diagnostics should use perturbations that are meaningful relative to each parameter’s scale, while retaining the units of the coordinate used in the calculation.

If \(u=\theta/s\), where \(s\) is a positive scale with the same units as \(\theta\), then the chain rule gives \(dF/du=s\,dF/d\theta\). A step \(h_u\) in \(u\) corresponds exactly to a step \(h_\theta=s h_u\) in \(\theta\). Rescaling does not remove objective-evaluation error for the same physical perturbation. It does make step selection and comparisons across parameters more interpretable, and prevents confusing gradients expressed in different units.

Worked Example: Translating a Step Through Parameter Scaling

Let \(\theta\) be a hypothetical clearance in L/h, with current value 5 L/h, and let \(F(\theta)=30+2(\theta-5)+0.4(\theta-5)^2\). The exact derivative at 5 is 2 objective units per L/h. Use \(u=\theta/5\), so the current value is \(u=1\) and the exact derivative with respect to \(u\) is \(5(2)=10\) objective units.

A central step \(h_\theta=0.01\) L/h corresponds to \(h_u=0.01/5=0.002\). The exact objective values are \(F(5.01)=30.02004\) and \(F(4.99)=29.98004\). Suppose the computed values are 30.020041 and 29.980039. Then the estimate in the \(\theta\) coordinate is \((30.020041-29.980039)/0.02=0.040002/0.02=2.0001\) objective units per L/h.

The same objective difference in the \(u\) coordinate gives \(0.040002/(2(0.002))=10.0005\) objective units per unit \(u\). The factor-of-five difference is required by the change of coordinates, not disagreement between the calculations. A diagnostic report should state both the perturbation and the coordinate, and should not compare raw gradient magnitudes across differently scaled parameters without conversion.

Testing Smoothness and Evaluation Accuracy

Finite-difference formulas rely on local smoothness. A central estimate can be especially useful when the objective behaves smoothly over both sides of the perturbation, but its nominal second-order truncation behavior does not apply if the function lacks the required derivatives. Integration error, changes in solver behavior, and event handling can also make computed objective values depart from a smooth local approximation. A set of estimates that fails to settle as \(h\) is reduced may reflect such evaluation behavior, rather than simply an unfortunate step size.

For a central difference with per-evaluation error bounded by \(\varepsilon\), a useful conservative accounting is truncation of approximately \(|F'''(\theta)|h^2/6\) plus evaluation error of at most \(\varepsilon/h\). If a local bound \(M_3\) on \(|F'''|\) is available, balancing these two terms suggests \(M_3h^2/6\approx\varepsilon/h\), or \(h\approx(6\varepsilon/M_3)^{1/3}\). This is a guide, not a universal prescription: in model fitting, \(M_3\) and \(\varepsilon\) are rarely known accurately, and parameter scaling affects what \(h\) means.

Worked Example: Solver Error Can Defeat a Smaller Step

Consider the hypothetical smooth objective \(F(\theta)=10+4(\theta-1)+2(\theta-1)^2+5(\theta-1)^3\), evaluated at \(\theta=1\). Its derivative is 4, and its exact central estimate is \(4+5h^2\). Suppose each computed objective value has absolute numerical error no greater than \(2\times10^{-6}\).

At \(h=0.01\), the exact values are \(F(1.01)=10.040205\) and \(F(0.99)=9.960195\). Suppose numerical integration produces 10.040206 and 9.960194. The estimate is \((10.040206-9.960194)/0.02=0.080012/0.02=4.0006\). The exact estimate is 4.0005, and the observed error is 0.0001. The evaluation-error bound is \(\varepsilon/h=0.0002\), so this result is consistent with the stated bound.

At \(h=0.001\), the exact estimate is \(4+5(0.001)^2=4.000005\). The possible evaluation-error contribution is now bounded by \(2\times10^{-6}/0.001=0.002\), ten times the bound at \(h=0.01\). For example, computed objective values 10.0040038 and 9.9960002 give \((0.0080036)/0.002=4.0018\), a deviation of 0.001795 from the exact estimate, still within the 0.002 evaluation-error bound. Here the smaller step reduced truncation error but made the estimate less accurate because solver error dominated.

A Practical Diagnostic Workflow

1
Specify the coordinate and units.
Name the parameter, its current value, its units, and the scale used to choose perturbations. Keep other parameters fixed during a coordinate-wise check.
2
Compute a sequence of estimates.
Use several decreasing steps, preferably spaced by a modest factor such as 2 or 10. Record the objective values as well as the resulting derivatives, because the differences reveal whether subtraction is acting on nearly identical values.
3
Look for regimes, not a magic step.
Estimates that move steadily toward a stable range may indicate decreasing truncation error. Erratic changes at the smallest steps suggest evaluation noise, precision loss, or local irregularity. If no stable range appears, investigate objective evaluation before trusting the gradient.
4
Check evaluation repeatability and accuracy.
Re-evaluate the same points and assess solver tolerance, integration accuracy, and numerical precision. Tightening tolerances is informative only if the objective values and gradient sequence respond consistently.
5
Compare methods when available.
Compare central differences with forward differences and, where implemented, analytic or automatic derivatives. Agreement across independent methods and across a step-size plateau is stronger evidence than agreement between two steps using the same inaccurate objective evaluations.

Forward differences require fewer evaluations if \(F(\theta)\) is already available, but have first-order truncation error and use a baseline evaluation. Central differences require two perturbed evaluations and have second-order truncation error under greater smoothness. Analytic derivatives avoid finite-difference step selection but can contain implementation errors. Automatic differentiation can provide accurate derivatives of the operations it differentiates, but does not automatically eliminate errors from approximate numerical solvers or unsupported operations. None of these alternatives makes an invalid or inaccurate objective evaluation reliable.

Finite-difference noise is a diagnostic about derivative calculation, not by itself a diagnosis of the model. A noisy gradient does not prove that a parameter is unidentified, at a boundary, or governed by a nonsmooth likelihood. Conversely, an apparently stable finite difference does not prove that a solver is accurate over the full parameter space. When the function does not satisfy the local smoothness conditions, the Taylor-based error analysis above no longer applies; the immediate task is to identify and understand the evaluation behavior rather than force a step-size rule to fit.

Common Mistakes and Limitations

  • Making \(h\) as small as possible. This can magnify objective-evaluation error by a factor proportional to \(1/h\). Compare a range of steps instead.
  • Using one absolute step for all parameters. The same increment can be negligible for one coordinate and enormous for another. Choose steps with reference to parameter scale and units.
  • Calling every changing estimate “noise.” A large-step discrepancy may be truncation error, while a small-step discrepancy may be evaluation noise. The direction and pattern across steps matter.
  • Interpreting a rescaled gradient without conversion. If \(u=\theta/s\), then \(dF/du=s\,dF/d\theta\). The numerical values differ because their units differ.
  • Assuming smoothness from a successful fit. An optimizer stopping successfully does not establish that the objective has the derivatives required for the finite-difference accuracy claim.

These checks are local and coordinate-wise. They do not establish that the optimizer will behave well in every direction, nor do they replace investigation of integration failures or model behavior over a broader parameter region. Their value is narrower: they show whether a reported numerical gradient is plausibly stable to reasonable changes in its finite-difference step.

Key takeaway: Diagnose a finite-difference gradient by recording estimates over several scale-aware perturbations, checking objective repeatability, and separating truncation behavior from evaluation error. A stable step-size range is useful evidence; a single gradient value is not.

Check Your Understanding

Answer each question, then compare with the solutions below.

  1. For a central difference, what happens to a fixed bound on objective-evaluation error as \(h\) is reduced by a factor of 10?
  2. A parameter is measured in mg/L and is near 0.02 mg/L. Why might a fixed step of 0.1 mg/L be a poor diagnostic choice?
  3. If \(u=\theta/4\), and \(dF/d\theta=3\) objective units per unit of \(\theta\), what is \(dF/du\)?
  4. At successively smaller steps, estimates first approach 2.4 and then jump substantially at the smallest step. Give two plausible explanations for the different parts of this pattern.

Solutions. (1) The bound is \(\varepsilon/h\), so reducing \(h\) tenfold increases the bound tenfold. (2) The step is five times the parameter’s current magnitude, so it does not probe a small local change; it may incur substantial truncation error and may not represent the scale of plausible variation. (3) By the chain rule, \(dF/du=4(dF/d\theta)=12\) objective units per unit of \(u\). (4) The approach toward 2.4 can reflect decreasing truncation error; the final jump can reflect amplified evaluation error, loss of precision, or irregular objective evaluation at very small perturbations. Check repeated objective evaluations and solver accuracy before choosing among these explanations.

Clinical Biostats