5  Optimization for Maximum Likelihood Estimation

Author

Longhai Li

Published

October 27, 2026

This chapter develops maximum likelihood estimation and the numerical methods used to find it, first for a scalar parameter and then for a parameter vector. The univariate case, where the negative log-likelihood can be plotted and a root of its derivative can be bracketed, motivates the two ideas that carry over unchanged to higher dimensions: replacing that function locally by a quadratic and solving for its minimum (Newton–Raphson), and using the curvature of that quadratic to report a standard error. The multivariate case, where neither plotting nor bracketing is available, shows what survives of that idea and what has to be added — safeguards against divergence, and a wider menu of derivative-free and gradient-only methods (gradient descent, conjugate gradient, BFGS, stochastic gradient descent, and Nelder–Mead) for when the Hessian is unavailable or too expensive. Several of these methods are illustrated below with live, in-browser animations that let you change the objective function, the starting point, and the algorithm before watching the iterates respond.

5.1 Maximum Likelihood: Theory

5.1.1 Likelihood, Negative Log-Likelihood, and Information

A model specifies the density or probability of the data \(D\) given a parameter \(\theta\), written \(f(D \mid \theta)\). As a function of \(D\) with \(\theta\) fixed it is the sampling distribution; as a function of \(\theta\) with \(D\) observed it is the likelihood. For independent observations \(x_1, \ldots, x_n\), \[ L(\theta) = \prod_{i=1}^{n} f(x_i \mid \theta), \qquad \ell(\theta) = \log L(\theta) = \sum_{i=1}^{n} \log f(x_i \mid \theta). \]

We never work with \(L\) itself: the product underflows to zero for even moderate \(n\), and sums are easier to differentiate. Maximizing \(L\) is the same as maximizing \(\ell\), which in turn is the same as minimizing the negative log-likelihood \[ \mathcal{L}(\theta) = -\ell(\theta) = -\sum_{i=1}^{n}\log f(x_i \mid \theta) = \sum_{i=1}^{n} \mathcal{L}_i(\theta) , \] and it is the minimization form that the rest of this chapter — and every optimizer in R — actually uses. Where \(\mathcal{L}\) is smooth with an interior minimum, \(\hat\theta\) solves, \[ g(\theta) = \mathcal{L}'(\theta) = 0, \qquad g'(\hat\theta) > 0 . \]

Three quantities recur throughout the chapter:

  • the gradient \(g(\theta) = \mathcal{L}'(\theta) = -\ell'(\theta)\), whose root locates the minimum;

  • the observed information \(J(\theta) = g'(\theta) = \mathcal{L}''(\theta) = -\ell''(\theta)\), the curvature of \(\mathcal{L}\) at \(\theta\);

  • the expected information \(n I(\theta) = E_D\left[g'(\theta; D)\right]\), its expectation under the model.

The derivative of the log-likelihood, \(\ell'(\theta) = -g(\theta)\), is what the statistical literature calls the score, and \(g(\theta) = 0\) is the score equation written with the opposite sign. That terminology survives in two standard names used later — the method of scoring, and Fisher information defined as the variance of the score — but the algorithms below are all stated in terms of \(\mathcal{L}\) and \(g\), so the score does not appear as a separate object.

The second derivative plays a double role. It determines how a Newton-type algorithm steps toward the minimum, and it determines how precisely the data pin the parameter down. An optimizer that computes the curvature has computed the standard error at the same time.

5.1.2 Example: iid Bernoulli

Before turning to a model with a continuous parameter and an exactly quadratic negative log-likelihood, it is worth solving \(g(\theta) = 0\) by hand in the simplest possible case. Let \(Y_1, \ldots, Y_n \overset{iid}{\sim} \mathrm{Bernoulli}(\theta)\), so that \(P(y_i \mid \theta) = \theta^{y_i}(1 - \theta)^{1 - y_i}\). Writing \(n_1 = \sum_i y_i\) for the number of successes and \(n_0 = n - n_1\) for the number of failures, \[ L(\theta) = \theta^{n_1}(1 - \theta)^{n_0}, \qquad \mathcal{L}(\theta) = -n_1 \log\theta - n_0 \log(1 - \theta). \]

Its derivative is, \[ g(\theta) = -\frac{n_1}{\theta} + \frac{n_0}{1 - \theta}, \] and setting \(g(\theta) = 0\) gives \(n_1(1-\theta) = n_0\theta\), i.e. \(n_1 = (n_1 + n_0)\theta = n\theta\), so \[ \hat\theta = \frac{n_1}{n} = \overline y. \]

The second derivative \(g'(\theta) = n_1/\theta^{2} + n_0/(1-\theta)^{2}\) is positive wherever it is defined, confirming that \(\overline y\) is a minimum of \(\mathcal{L}\) and not a saddle point. With only three observations, \(y = (1, 1, 0)\), both the likelihood and \(\mathcal{L}\) can be tabulated and plotted directly, which makes \(\hat\theta = 2/3 = \overline y\) visible without any calculus at all — the value that makes the data most probable is exactly the value that makes \(\mathcal{L}\) smallest.

\(\theta\) \(L(\theta) = \theta^{2}(1-\theta)\) \(\mathcal{L}(\theta) = -\log L(\theta)\)
\(0\) \(0\) \(\infty\)
\(1/2\) \(1/8 = 0.1250\) \(2.079\)
\(2/3\) \(4/27 = 0.1481\) (max) \(1.910\) (min)
\(3/4\) \(9/64 = 0.1406\) \(1.962\)
\(1\) \(0\) \(\infty\)
Code
par(mfrow = c(1, 2), mar = c(4, 4.5, 2.5, 1))
curve(x^2 * (1 - x), 0, 1, lwd = 2, xlab = TeX(r"($\theta$)"),
      ylab = TeX(r"($L(\theta)$)"), main = TeX(r"(likelihood)"))
abline(v = 2 / 3, col = 2, lty = 2)

curve(-log(x^2 * (1 - x)), 0.05, 0.98, n = 400, lwd = 2, ylim = c(1.8, 4),
      xlab = TeX(r"($\theta$)"), ylab = TeX(r"($-\log L(\theta)$)"),
      main = TeX(r"(negative log-likelihood)"))
abline(v = 2 / 3, col = 2, lty = 2)
Figure 5.1: Introduces the likelihood and the negative log-likelihood with the simplest possible example, showing that the parameter value making the data most probable is the one that minimizes the function every optimizer in this chapter works with. Likelihood and negative log-likelihood of \(\theta\) for three Bernoulli observations \(y = (1,1,0)\): the maximum of \(L\) and the minimum of \(\mathcal{L}\) both sit at \(\hat\theta = \bar y = 2/3\).

5.1.3 Closed Form: the Normal Mean

Let \(X_1, \ldots, X_n\) be iid \(N(\mu, \sigma_0^{2})\) with \(\sigma_0^{2}\) known. Using the identity \(\sum_i (x_i - \mu)^2 = \sum_i (x_i - \overline x)^2 + n(\overline x - \mu)^2\), \[ \mathcal{L}(\mu) = \frac{n}{2}\log\left(2\pi\sigma_0^{2}\right) + \frac{1}{2\sigma_0^{2}}\sum_{i=1}^{n}(x_i - \mu)^{2} = C + \frac{n}{2\sigma_0^{2}}(\mu - \overline x)^{2} . \]

Differentiating, \[ g(\mu) = \frac{n}{\sigma_0^{2}}\left(\mu - \overline x\right), \qquad g'(\mu) = \frac{n}{\sigma_0^{2}}, \qquad \hat\mu = \overline x . \]

Every feature of the general theory is visible here in exact form. \(\mathcal{L}\) is an upward parabola, so it has a unique minimum. Its derivative \(g\) is linear, so \(g(\mu) = 0\) is solved in one line. The curvature \(J(\mu) = g'(\mu) = n/\sigma_0^{2}\) does not depend on \(\mu\) or on the data, so observed and expected information coincide, \(J = nI\), and both grow linearly with \(n\): doubling the sample doubles the sharpness of the well and halves the variance of \(\hat\mu\).

Code
x20 <- rnorm(20, mean = 1); x50 <- rnorm(50, mean = 1)
mu <- seq(0, 2, length.out = 200)

nll_norm <- function(mu, x) vapply(mu, function(m) -sum(dnorm(x, m, log = TRUE)), numeric(1))

par(mfrow = c(1, 3), mar = c(4, 4.5, 2.5, 1))

matplot(mu, cbind(nll_norm(mu, x20) - min(nll_norm(mu, x20)),
                  nll_norm(mu, x50) - min(nll_norm(mu, x50))),
        type = "l", lty = 1, lwd = 2, col = c(1, 2),
        xlab = TeX(r"($\mu$)"), ylab = TeX(r"($L(\mu) - L(\hat{\mu})$)"),
        main = TeX(r"(negative log-likelihood)"))
legend("top", bty = "n", lty = 1, lwd = 2, col = c(1, 2),
       legend = TeX(c(r"($n = 20$)", r"($n = 50$)")))

matplot(mu, cbind(20 * (mu - mean(x20)), 50 * (mu - mean(x50))),
        type = "l", lty = 1, lwd = 2, col = c(1, 2),
        xlab = TeX(r"($\mu$)"), ylab = TeX(r"($g(\mu)$)"),
        main = TeX(r"(gradient)"))
abline(h = 0, lty = 3)

matplot(mu, cbind(rep(20, 200), rep(50, 200)), type = "l", lty = 1, lwd = 2, col = c(1, 2),
        ylim = c(0, 60), xlab = TeX(r"($\mu$)"), ylab = TeX(r"($g'(\mu) = J(\mu)$)"),
        main = TeX(r"(curvature)"))
Figure 5.2: Relates the three central objects of maximum likelihood, the negative log-likelihood, its gradient, and its curvature, and shows how each changes with sample size. Negative log-likelihood, its gradient \(g\), and its curvature for a normal mean at \(n = 20\) and \(n = 50\): the larger sample gives a sharper well, a steeper \(g\), and larger curvature.

The derivative \(g\) crosses zero exactly where \(\mathcal{L}\) bottoms out, and the larger sample gives a sharper well, a steeper \(g\), and a larger curvature.

5.1.4 Consistency, Asymptotic Normality, and Fisher Information

Under regularity conditions — the support of \(f\) not depending on \(\theta\), the true value interior to the parameter space, and two derivatives passing under the integral sign — the MLE has three properties as \(n \to \infty\), with \(\theta_*\) the true parameter.

Consistency. \(\hat\theta \to \theta_*\) in probability. The reason is that \(\mathcal{L}(\theta)/n\) converges to \(-E_{\theta_*}[\log f(X \mid \theta)]\), which by the information inequality is minimized at \(\theta = \theta_*\): the surface being minimized converges to one whose lowest point is at the truth.

Asymptotic normality.

\[ \hat\theta \;\dot\sim\; N\left(\theta_*,\ \left[n I(\theta_*)\right]^{-1}\right), \qquad I(\theta) = -E_X\left[\frac{\partial^{2}}{\partial\theta^{2}}\log f(X \mid \theta)\right] = E_X\left[\left(\frac{\partial}{\partial\theta}\log f(X\mid\theta)\right)^{2}\right]. \]

A sketch: expand \(g(\hat\theta) = 0\) about \(\theta_*\) to get \(\hat\theta - \theta_* \approx -g(\theta_*)/g'(\theta_*)\). The numerator is a sum of iid mean-zero terms — this is the step in which \(I(\theta)\) appears as the variance of the score — hence asymptotically normal with variance \(nI(\theta_*)\); the denominator converges to \(nI(\theta_*)\) by the law of large numbers. Dividing gives variance \([nI(\theta_*)]^{-1}\).

Asymptotic efficiency. No consistent estimator has smaller asymptotic variance — the Cramér–Rao bound is attained.

Since \(\theta_*\) is unknown, the variance is estimated by substituting \(\hat\theta\). Two versions are available: the expected information \(nI(\hat\theta)\), which requires the expectation analytically, and the observed information \(J(\hat\theta) = g'(\hat\theta)\), which requires only the curvature that a Newton step already computes. By the law of large numbers \(J(\hat\theta)/n \approx I(\hat\theta)\), and both give \[ \widehat{\mathrm{SE}}(\hat\theta) = J(\hat\theta)^{-1/2} \quad\text{or}\quad \left[nI(\hat\theta)\right]^{-1/2}, \qquad \hat\theta \pm 1.96\,\widehat{\mathrm{SE}}(\hat\theta). \]

These are asymptotic statements. A flat \(\mathcal{L}\) — small curvature — gives a large standard error, and an \(\mathcal{L}\) that is far from quadratic gives a standard error that should not be trusted at all; the last section of this chapter shows a case of exactly that.

5.1.5 Demonstration: the Cauchy Location Model

The Cauchy density \(f(x \mid \theta) = 1/\{\pi[1 + (x - \theta)^2]\}\) has no closed-form MLE: \[ \mathcal{L}(\theta) = n\log\pi + \sum_{i=1}^{n}\log\left(1 + (x_i - \theta)^{2}\right), \qquad g(\theta) = -\sum_{i=1}^{n}\frac{2(x_i - \theta)}{1 + (x_i - \theta)^{2}} . \]

Setting \(g(\theta) = 0\) gives a polynomial equation of degree \(2n - 1\). Plotting \(\mathcal{L}\) for samples of increasing size shows the well sharpening around the truth, which is consistency and growing information in one picture.

Code
nll_cauchy_fn <- function(theta, x) -sum(dcauchy(x, theta, log = TRUE))

thetas <- seq(15, 25, length.out = 200)
n_set <- c(50, 100, 200)

nll_cauchy <- sapply(n_set, function(n) {
  d <- rcauchy(n, location = 20)
  sapply(thetas, nll_cauchy_fn, x = d)
})
nll_cauchy <- sweep(nll_cauchy, 2, apply(nll_cauchy, 2, min))   # align minima at 0

par(mar = c(4, 4.5, 2.5, 1))
matplot(thetas, nll_cauchy, type = "l", lty = 1, lwd = 2, col = c(1, 2, 4), ylim = c(0, 20),
        xlab = TeX(r"($\theta$)"), ylab = TeX(r"($L(\theta) - L(\hat{\theta})$)"),
        main = TeX(r"(Cauchy negative log-likelihood, $\theta_* = 20$)"))
abline(v = 20, lty = 3)
legend("topright", bty = "n", lty = 1, lwd = 2, col = c(1, 2, 4),
       legend = TeX(sprintf(r"($n = %d$)", n_set)))
Figure 5.3: Shows how the Cauchy location negative log-likelihood, which is harder to minimize than the normal one, concentrates around the true value as the sample size grows. Cauchy negative log-likelihood for samples of increasing size (\(n = 50, 100, 200\)) from a true location \(\theta_* = 20\): the well sharpens around the truth as \(n\) grows.

The variability of the MLE across repeated samples is the sampling distribution the theory describes. Repeating the experiment 50 times at two sample sizes shows the curves clustering more tightly, and their minima concentrating, as \(n\) grows.

Code
nll_curves <- function(n, nrep = 50)
  replicate(nrep, {
    d <- rcauchy(n, location = 20)
    lc <- sapply(thetas, nll_cauchy_fn, x = d)
    lc - min(lc)
  })

curves_small <- nll_curves(20)
curves_large <- nll_curves(200)

par(mfrow = c(1, 2), mar = c(4, 4.5, 2.5, 1))
for (obj in list(list(m = curves_small, n = 20), list(m = curves_large, n = 200)))
  matplot(thetas, obj$m, type = "l", lty = 1, col = adjustcolor(1, 0.35), ylim = c(0, 10),
          xlab = TeX(r"($\theta$)"), ylab = TeX(r"($L(\theta) - L(\hat{\theta})$)"),
          main = TeX(sprintf(r"(sample size $n = %d$)", obj$n)))

### sampling distribution of the MLE at the two sample sizes
mle_at <- function(m) thetas[apply(m, 2, which.min)]
rbind(`n = 20`  = c(mean = mean(mle_at(curves_small)), sd = sd(mle_at(curves_small))),
      `n = 200` = c(mean = mean(mle_at(curves_large)), sd = sd(mle_at(curves_large))))
            mean        sd
n = 20  20.05930 0.3843737
n = 200 20.01005 0.0920729
Figure 5.4: Shows the sampling variability of the negative log-likelihood itself: different data sets give different curves, and the minima of those curves, the MLEs, become less variable as \(n\) grows. 50 repeated Cauchy negative log-likelihood curves at \(n = 20\) versus \(n = 200\): the curves cluster more tightly, and their minima concentrate, as \(n\) grows.

The standard deviation falls by roughly the factor \(\sqrt{10}\) predicted by \([nI(\theta_*)]^{-1/2}\). Two features of these curves also warn us about computation: for small \(n\) the negative log-likelihood has several local minima, and its shape away from the well is far from quadratic. Numerical optimization is required, and it must be used with care.

5.2 Univariate Optimization Algorithms

Every algorithm in this section minimizes the negative log-likelihood \[ \mathcal{L}(\theta) = -\ell(\theta), \] so that the quantity driving each step is its derivative, \[ g(\theta) = \mathcal{L}'(\theta) = -\ell'(\theta), \] and the quantity measuring curvature is \(g'(\theta) = \mathcal{L}''(\theta) = -\ell''(\theta)\). For iid data \(\mathcal{L}\) is a sum of per-observation terms, \(\mathcal{L}(\theta) = \sum_i \mathcal{L}_i(\theta)\), a decomposition that matters only in the section on stochastic gradients. Maximizing \(\ell\) and minimizing \(-\ell\) are the same problem, but stating it as a minimization matches what optim, nlm and optimize expect, and it is the form the multivariate and gradient-based methods later in the chapter use as well. Note that \(g'\) is exactly the observed information \(J(\theta)\) of the previous section, so the curvature an optimizer computes on its way to the minimum is the same curvature that supplies the standard error.

5.2.1 The Running Example: Zero-Truncated Poisson Counts

All the methods below are illustrated on one model, so that their pictures and their iterate sequences can be compared directly.

Counts are often recorded only when positive: a study of hospital stays sees admitted patients, a survey of litters sees litters that exist. If \(Y \sim \mathrm{Poisson}(\lambda)\) and we observe \(X = Y \mid Y > 0\), \[ P(x \mid \lambda) = \frac{e^{-\lambda}\lambda^{x}}{x!}\cdot\frac{1}{1 - e^{-\lambda}}, \qquad x = 1, 2, \ldots. \]

Dropping the factorial terms, which do not involve \(\lambda\), and dividing by \(n\) gives the negative log-likelihood per observation, \[ \mathcal{L}(\lambda) = -\overline x \log\lambda + \lambda + \log\left(1 - e^{-\lambda}\right), \] whose derivatives are, \[ g(\lambda) = -\ell'(\lambda) = -\frac{\overline x}{\lambda} + \frac{1}{1 - e^{-\lambda}}, \qquad g'(\lambda) = -\ell''(\lambda) = \frac{\overline x}{\lambda^{2}} - \frac{e^{-\lambda}}{\left(1 - e^{-\lambda}\right)^{2}} . \]

Two features of this model make it a convenient running example. First, the data enter only through the sample mean \(\overline x\), so the total negative log-likelihood is exactly \(n\) times the per-observation one and the two have the same minimizer; the figures below therefore plot the per-observation version, which does not grow with \(n\), while the standard error at the end uses the total. Second, since \(E(X) = \lambda/(1 - e^{-\lambda})\), substituting \(E(X)\) for \(\overline x\) in \(g'\) gives the expected information per observation in closed form, \[ I(\lambda) = \frac{1}{\lambda\left(1 - e^{-\lambda}\right)} - \frac{e^{-\lambda}}{\left(1 - e^{-\lambda}\right)^{2}} , \] which is what Fisher scoring needs. The equation \(g(\lambda) = 0\) has no closed-form root, but \(\mathcal{L}\) is unimodal — an ideal problem on which to compare all the methods against a single known answer.

Code
set.seed(2024)          # the same data underlies every figure in this section

## Negative log-likelihood per observation and its derivatives: nll is L(lambda),
## g is the gradient L'(lambda), dg is the curvature L''(lambda). The data enter
## only through xbar, so the total L is length(xp) times these.
nll <- function(l, xb = xbar) -xb * log(l) + l + log(1 - exp(-l))    # L(lambda)
g   <- function(l, xb = xbar) -xb / l + 1 / (1 - exp(-l))            # gradient  L'
dg  <- function(l, xb = xbar)  xb / l^2 - exp(-l) / (1 - exp(-l))^2  # curvature L''

## expected information per observation, and the fixed-point map used below
info <- function(l) 1 / (l * (1 - exp(-l))) - exp(-l) / (1 - exp(-l))^2
fp_map <- function(l, xb = xbar) xb * (1 - exp(-l))

## simulated data: Poisson counts with the zeros discarded
x  <- rpois(100, lambda = 1.5)
xp <- x[x > 0]
xbar <- mean(xp)

lhat <- uniroot(g, c(0.2, 20))$root       # the answer every method must find

c(n_observed = length(xp), sample_mean = xbar, true_lambda = 1.5, mle = lhat)
 n_observed sample_mean true_lambda         mle 
  78.000000    1.897436    1.500000    1.454236 

The sample mean overshoots 1.5, because the zeros that would have pulled it down were never recorded; the MLE corrects for exactly that.

Code
lam <- seq(0.3, 3, length.out = 300)

par(mfrow = c(1, 2), mar = c(4, 4.5, 2.5, 1))
plot(lam, nll(lam), type = "l", lwd = 2,
     xlab = TeX(r"($\lambda$)"), ylab = TeX(r"($L(\lambda)$)"),
     main = TeX(r"(negative log-likelihood)"))
abline(v = lhat, lty = 3)
points(lhat, nll(lhat), pch = 19, cex = 1.2)

plot(lam, g(lam), type = "l", lwd = 2,
     xlab = TeX(r"($\lambda$)"), ylab = TeX(r"($g(\lambda)$)"),
     main = TeX(r"(gradient)"))
abline(h = 0, lty = 3); abline(v = lhat, lty = 3)
points(lhat, 0, pch = 19, cex = 1.2)
Figure 5.5: Sets up the running example of a zero-truncated Poisson model by showing the negative log-likelihood that every method in this section minimizes, together with its gradient, whose root is the minimizer. Negative log-likelihood per observation and its gradient \(g(\lambda)\) for the zero-truncated Poisson model: the minimum of \(\mathcal{L}\) sits exactly where \(g\) crosses zero.

The minimum of \(\mathcal{L}\) and the zero crossing of \(g\) are the same point, \(\hat\lambda \approx 1.454\). Since \(g\) is increasing through that crossing — the mirror image of the decreasing score \(\ell' = -g\) — the curvature \(g'(\hat\lambda)\) is positive, as it must be at a minimum.

5.2.2 Three Equivalent Formulations

The same task can be posed three ways, and the algorithms below attack different versions of it:

  • minimize \(\mathcal{L}(\theta) = -\ell(\theta)\) directly, using function values only;

  • find a root of \(g(\theta) = -\ell'(\theta)\);

  • rearrange that root equation into a fixed point \(\theta = \varphi(\theta)\).

Root-finders are usually faster because they use derivative information, but they locate any stationary point, including maxima and saddle points. Direct minimizers need only function values and always move downhill, but converge more slowly. Which to prefer depends on whether derivatives are available and whether the function is smooth.

Each method below is illustrated with a schematic showing its first few iterations on the truncated-Poisson nll just defined. The minimizer \(\hat\lambda\) is marked in every figure.

5.2.3 Fixed-Point (Simple) Iteration

Rearrange the stationarity equation \(g(\theta) = -\ell'(\theta) = 0\) algebraically into the form \(\theta = \varphi(\theta)\) and iterate \(\theta_{k+1} = \varphi(\theta_k)\). For the truncated Poisson, \(-\overline x/\lambda + 1/(1 - e^{-\lambda}) = 0\) rearranges to \[ \lambda = \overline x\left(1 - e^{-\lambda}\right) = \varphi(\lambda), \qquad \varphi'(\lambda) = \overline x e^{-\lambda}. \]

If \(\varphi\) maps an interval into itself and \(\lvert\varphi'(\theta^{*})\rvert < 1\) at the fixed point, the mean value theorem gives \[ \lvert\theta_{k+1} - \theta^{*}\rvert = \lvert\varphi(\theta_k) - \varphi(\theta^{*})\rvert \le \lvert\varphi'(\xi)\rvert \lvert\theta_k - \theta^{*}\rvert , \] so the error contracts by roughly the constant factor \(\lvert\varphi'(\theta^{*})\rvert\) at each step. This is linear convergence: a fixed number of digits is gained per iteration, and the closer \(|g'|\) is to 1 the slower the progress.

Code
fixed_point <- function(g, x0, tol = 1e-10, maxit = 500) {
  for (k in 1:maxit) {
    x1 <- g(x0)
    if (abs(x1 - x0) < tol) return(c(root = x1, iter = k))
    x0 <- x1
  }
  stop("not converged")
}

The mechanism is easiest to see as a cobweb: from \(\lambda_k\) on the diagonal, move vertically to the curve \(\varphi\) to obtain \(\lambda_{k+1}\), then horizontally back to the diagonal to start the next step. The iterates walk toward the intersection of \(g\) with the \(45^{\circ}\) line, and the shallower \(\varphi\) is at that crossing — the smaller \(\lvert\varphi'\rvert\) — the larger each stride toward it.

Code
par(mar = c(4, 4.5, 2.5, 1))
plot(lam, fp_map(lam), type = "l", lwd = 2, xlim = c(0.3, 3), ylim = c(0.3, 3),
     xlab = TeX(r"($\lambda_k$)"), ylab = TeX(r"($\varphi(\lambda_k) = \lambda_{k+1}$)"),
     main = TeX(r"(fixed-point iteration: the cobweb)"))
abline(0, 1, lty = 2)

x0 <- 2.8
for (k in 1:5) {
  x1 <- fp_map(x0)
  segments(x0, x0, x0, x1, col = 2, lwd = 2)          # up or down to the curve
  segments(x0, x1, x1, x1, col = 2, lwd = 2)          # across to the diagonal
  points(x0, x1, pch = 19, col = 2, cex = 0.7)
  if (k <= 3) text(x0, 0.35, TeX(sprintf(r"($\lambda_%d$)", k - 1)), col = 2, cex = 0.9)
  x0 <- x1
}
points(lhat, lhat, pch = 19, cex = 1.2)
text(lhat, lhat, TeX(r"($\hat{\lambda}$)"), pos = 2, offset = 0.6)
Figure 5.6: Illustrates fixed-point iteration, the simplest scheme for solving the stationarity equation, and how its iterates converge to the solution. Fixed-point iteration as a cobweb diagram: iterates walk toward the intersection of \(\varphi\) with the diagonal.

The method needs no derivatives, but the rearrangement must be found by hand, and different rearrangements of the same equation can converge at different speeds or diverge outright. Geometrically, a rearrangement with \(\lvert\varphi'\rvert > 1\) at the crossing produces a cobweb that spirals outward instead of inward.

5.2.4 Bisection

If \(g\) is continuous and \(g(a)\) and \(g(b)\) have opposite signs, a root lies in \([a, b]\). Evaluate \(g\) at the midpoint and retain whichever half still changes sign. For the truncated Poisson \(-\ell'\) is negative at small \(\lambda\) and positive at large \(\lambda\), so any bracket straddling \(\hat\lambda\) works.

Code
bisection <- function(d1, a, b, tol = 1e-10) {          # d1 is nll'
  if (d1(a) * d1(b) > 0) stop("endpoints do not bracket a root")
  k <- 0
  while (b - a > tol) {
    m <- (a + b) / 2
    if (d1(a) * d1(m) <= 0) b <- m else a <- m
    k <- k + 1
  }
  c(root = (a + b) / 2, iter = k)
}

The schematic below draws the bracket at each iteration as a bar beneath the curve of \(g\). The dot is the midpoint, and the dotted line carries it up to the curve, where its sign decides which half survives.

Code
par(mar = c(4, 4.5, 2.5, 1))
plot(lam, g(lam), type = "l", lwd = 2, ylim = c(-4.0, 0.9), xlim = c(0.12, 3),
     xlab = TeX(r"($\lambda$)"), ylab = TeX(r"($g(\lambda)$)"),
     main = TeX(r"(bisection: each step halves the bracket)"))
abline(h = 0, lty = 3)
points(lhat, 0, pch = 19, cex = 1.2)

a <- 0.3; b <- 3
ylev <- seq(-2.9, -3.8, length.out = 4)
for (k in 1:4) {
  m <- (a + b) / 2
  segments(a, ylev[k], b, ylev[k], col = 4, lwd = 3)
  points(c(a, b), rep(ylev[k], 2), pch = "|", col = 4)
  segments(m, ylev[k], m, g(m), lty = 3, col = 2)
  points(m, ylev[k], pch = 19, col = 2, cex = 0.8)
  text(0.19, ylev[k], k, cex = 0.8, col = 4)
  if (g(a) * g(m) <= 0) b <- m else a <- m
}
Figure 5.7: Illustrates bisection, a slow but robust root-finding scheme that only needs an interval known to contain the root. Bisection on the gradient \(g\) of the negative log-likelihood: each iteration halves the bracket that contains the root.

The bracket halves at every step, so after \(k\) iterations the error is at most \((b - a)/2^{k}\) and the iteration count is known in advance: about \(\log_2\{(b-a)/\text{tol}\}\), regardless of the shape of \(g\). Bisection cannot diverge, which makes it the natural fallback when a faster method misbehaves. R’s uniroot uses Brent’s method, which retains the bracketing guarantee while interpolating for speed.

5.2.6 Newton–Raphson

5.2.6.1 Derivation

Approximate \(g\) by its tangent line at \(\theta_k\) and solve for where the tangent crosses zero: \[ g(\theta) \approx g(\theta_k) + g'(\theta_k)(\theta - \theta_k) = 0 \implies \theta_{k+1} = \theta_k - \frac{g(\theta_k)}{g'(\theta_k)} = \theta_k - \frac{-\ell'(\theta_k)}{-\ell''(\theta_k)} . \]

The two minus signs cancel, so this is numerically the same step a score-based derivation would give; writing it this way keeps the whole section in minimization form. Equivalently, replace \(\mathcal{L}\) by its second-order Taylor expansion at \(\theta_k\) and jump to the minimum of that parabola. For an exactly quadratic \(\mathcal{L}(\theta) = a(\theta - u)^2 + C\) with \(a > 0\) we have \(g = 2a(\theta - u)\) and \(g' = 2a\), so \[ \theta_{k+1} = \theta_k - \frac{2a(\theta_k - u)}{2a} = u, \] in one step from any starting value. The normal-mean example of the previous section is exactly of this form: Newton converges immediately.

The left panel below traces three steps. At each iterate, the tangent to \(g\) is followed to the horizontal axis, and where it lands is the next iterate. As the curve straightens near the root, the tangent becomes an excellent approximation and the steps land almost exactly on the answer.

The right panel contrasts one Newton step with one step of the method of scoring from the same point. Newton uses the actual slope \(g'(\lambda_k)\), the observed information; scoring uses \(I(\lambda_k)\), the same curvature averaged over hypothetical datasets. The two lines differ away from the root and coincide as \(\lambda_k\) approaches it.

Code
par(mfrow = c(1, 2), mar = c(4, 4.5, 2.5, 1))

### three Newton steps
plot(lam, g(lam), type = "l", lwd = 2, ylim = c(-2.6, 0.9),
     xlab = TeX(r"($\lambda$)"), ylab = TeX(r"($g(\lambda)$)"),
     main = TeX(r"(Newton-Raphson: follow the tangent)"))
abline(h = 0, lty = 3)
points(lhat, 0, pch = 19, cex = 1.2)

x0 <- 0.45
for (k in 1:3) {
  x1 <- x0 - g(x0) / dg(x0)
  segments(x0, g(x0), x1, 0, col = 2, lwd = 2)
  segments(x1, 0, x1, g(x1), lty = 3, col = "grey40")
  points(c(x0, x1), c(g(x0), 0), pch = 19, col = 2, cex = 0.7)
  text(x0, g(x0), TeX(sprintf(r"($\lambda_%d$)", k - 1)),
       pos = if (k == 1) 4 else 1, col = 2, cex = 0.9)
  x0 <- x1
}

## Newton step versus scoring step from the same point
plot(lam, g(lam), type = "l", lwd = 2, ylim = c(-2.6, 0.9),
     xlab = TeX(r"($\lambda$)"), ylab = TeX(r"($g(\lambda)$)"),
     main = TeX(r"(observed versus expected curvature)"))
abline(h = 0, lty = 3)
points(lhat, 0, pch = 19, cex = 1.2)

x0 <- 0.45
x_nr <- x0 - g(x0) / dg(x0)     # slope  nll''(lambda) = -l''(lambda)
x_fs <- x0 - g(x0) / info(x0)       # slope  I(lambda)
segments(x0, g(x0), x_nr, 0, col = 2, lwd = 2)
segments(x0, g(x0), x_fs, 0, col = 4, lwd = 2, lty = 5)
points(c(x0, x_nr, x_fs), c(g(x0), 0, 0), pch = 19, cex = 0.7, col = c(1, 2, 4))
legend("bottomright", bty = "n", lwd = 2, lty = c(1, 5), col = c(2, 4),
       legend = TeX(c(r"(Newton: slope $g'(\lambda_k)$)", r"(scoring: slope $I(\lambda_k)$)")))
Figure 5.10: Illustrates Newton-Raphson, which replaces the gradient of the negative log-likelihood by its tangent line at each step, and contrasts it with Fisher scoring, which uses the expected rather than the observed curvature. Newton-Raphson following the tangent to \(-\ell'\) (left), and one Newton step compared with one Fisher-scoring step from the same point (right).

5.2.7 Advantage: quadratic convergence

Let \(e_k = \theta_k - \hat\theta\) and expand \(g(\hat\theta) = 0\) about \(\theta_k\): \[ 0 = g(\theta_k) - g'(\theta_k)e_k + \tfrac12 g''(\xi)e_k^{2} \implies e_{k+1} = e_k - \frac{g(\theta_k)}{g'(\theta_k)} = \frac{g''(\xi)}{2\,g'(\theta_k)}\,e_k^{2} . \]

The error is squared at each step, so the number of correct digits roughly doubles. Two conditions appear in the constant: \(g'(\hat\theta) \ne 0\), meaning the minimum is a genuine bowl and not a flat spot, and \(g''\) bounded near the root. Asymptotic normality says that negative log-likelihoods are approximately quadratic near the MLE, which is why Newton is so effective there.

Code
newton <- function(d1, d2, x0, tol = 1e-10, maxit = 100, trace = FALSE) {
  for (k in 1:maxit) {                                 # d1 = nll', d2 = nll''
    x1 <- x0 - d1(x0) / d2(x0)
    if (trace) cat(sprintf("iter %2d: %.10f\n", k, x1))
    if (!is.finite(x1)) stop("diverged")
    if (abs(x1 - x0) < tol) return(c(root = x1, iter = k))
    x0 <- x1
  }
  stop("not converged")
}

5.2.8 A Classic Illustration: Newton’s Method for \(\sqrt{a}\)

Derivation. To find \(\sqrt{a}\), take \(f(x) = x^2 - a\), whose positive root is \(\sqrt a\). Then \(f'(x) = 2x\), and the Newton step is, \[ x_{k+1} = x_k - \frac{f(x_k)}{f'(x_k)} = x_k - \frac{x_k^{2} - a}{2x_k} = \frac{1}{2}\left(x_k + \frac{a}{x_k}\right). \]

The final form is the Babylonian rule: if \(x_k\) is too small then \(a/x_k\) is too large, and the root lies between them, so average the pair. Newton’s tangent line lands exactly on the midpoint.

The error follows the general quadratic law. Writing \(e_k = x_k - \sqrt a\), \[ e_{k+1} = \frac{1}{2}\left(x_k + \frac{a}{x_k}\right) - \sqrt a = \frac{\left(x_k - \sqrt a\right)^{2}}{2x_k} = \frac{e_k^{2}}{2x_k}, \]

so the number of correct digits roughly doubles each step.

Code
babylonian <- function(a, x0 = 1, iters = 6)
  Reduce(function(x, k) (x + a / x) / 2, 1:iters, init = x0, accumulate = TRUE)

traces <- sapply(c(10, 20, 50), babylonian)
dimnames(traces) <- list(paste("iter", 0:6), sprintf("sqrt(%d)", c(10, 20, 50)))

print(traces, digits = 12)
            sqrt(10)       sqrt(20)       sqrt(50)
iter 0 1.00000000000  1.00000000000  1.00000000000
iter 1 5.50000000000 10.50000000000 25.50000000000
iter 2 3.65909090909  6.20238095238 13.73039215686
iter 3 3.19600508187  4.71347454529  8.68597437190
iter 4 3.16245562280  4.47831444547  7.22119047433
iter 5 3.16227766518  4.47214021707  7.07262827574
iter 6 3.16227766017  4.47213595500  7.07106798401
Code
sqrt(c(10, 20, 50))                       # for comparison
[1] 3.162278 4.472136 7.071068
Code
signif(abs(traces - rep(sqrt(c(10, 20, 50)), each = 7)), 3)    # errors
       sqrt(10) sqrt(20) sqrt(50)
iter 0 2.16e+00 3.47e+00 6.07e+00
iter 1 2.34e+00 6.03e+00 1.84e+01
iter 2 4.97e-01 1.73e+00 6.66e+00
iter 3 3.37e-02 2.41e-01 1.61e+00
iter 4 1.78e-04 6.18e-03 1.50e-01
iter 5 5.01e-09 4.26e-06 1.56e-03
iter 6 4.44e-16 2.03e-12 1.72e-07

Starting from \(x_0 = 1\) in every case, the first two steps do the coarse work — the larger \(a\) is, the further the opening leap — and from the third step on the error squares: roughly \(10^{-1}\), then \(10^{-3}\), then \(10^{-6}\), then machine precision. Six iterations suffice for all three, though \(\sqrt{50}\) spends one more step than \(\sqrt{10}\) recovering from the poor start.

5.2.9 Problem: divergence

Newton uses only local information, so the quadratic model can be badly wrong away from the maximum. Three failures occur:

  • wrong curvature — where \(g' < 0\) the local model is an upside-down bowl, and the step heads toward a maximum or off to infinity;

  • overshooting — where \(g\) is nearly flat, \(g'(\theta_k) \approx 0\) produces an enormous step, possibly outside the parameter space;

  • multiple roots — the iteration converges to whichever stationary point it happens to reach, which need not be the global minimum.

The single-observation Cauchy likelihood is the standard illustration. With one observation at zero, \(\mathcal{L}(\theta) = -\ell(\theta) = \log(1 + \theta^{2})\) and \(g(\theta) = -\ell'(\theta) = 2\theta/(1 + \theta^{2})\), whose only root is \(\theta = 0\). That derivative rises, turns over at \(\theta = 1\), and decays to zero in the tails — exactly the flat region that defeats Newton.

Code
nll_c <- function(t) log(1 + t^2)                  # negative log-likelihood
g_c   <- function(t) 2 * t / (1 + t^2)             # gradient  L'(theta)
dg_c  <- function(t) 2 * (1 - t^2) / (1 + t^2)^2   # curvature L''(theta)

par(mfrow = c(1, 2), mar = c(4, 4.5, 2.5, 1))
curve(nll_c, -5, 5, n = 400, lwd = 2, xlab = TeX(r"($\theta$)"), ylab = TeX(r"($L(\theta)$)"),
      main = TeX(r"(negative log-likelihood)"))
curve(g_c, -5, 5, n = 400, lwd = 2, xlab = TeX(r"($\theta$)"), ylab = TeX(r"($g(\theta)$)"),
      main = TeX(r"(gradient, with the flat tails)"))
abline(h = 0, lty = 3); abline(v = c(-1, 1), col = 2, lty = 2)
Figure 5.11: Explains why Newton-Raphson can fail on the Cauchy likelihood by showing the shape of the contribution of a single observation. Single-observation Cauchy negative log-likelihood and its gradient: the gradient flattens in the tails, the region that defeats Newton-Raphson.

Tracing the iterates from three starting values shows the boundary at \(\lvert\theta_0\rvert = 1\), where \(g\) turns over:

Code
newton_trace <- function(d1, d2, x0, iters = 8) {
  path <- numeric(iters + 1); path[1] <- x0
  for (k in 1:iters) {
    x0 <- x0 - d1(x0) / d2(x0)
    path[k + 1] <- if (is.finite(x0)) x0 else NA
  }
  path
}

starts <- c(0.5, 0.8, 1.2)
traces <- vapply(starts, function(v) newton_trace(g_c, dg_c, v), numeric(9))
colnames(traces) <- sprintf("start = %.1f", starts)
rownames(traces) <- paste("iter", 0:8)
signif(traces, 5)
       start = 0.5 start = 0.8 start = 1.2
iter 0  5.0000e-01      0.8000      1.2000
iter 1 -3.3333e-01     -2.8444      7.8545
iter 2  8.3333e-02     -6.4912     15.9680
iter 3 -1.1655e-03    -13.2980     32.0620
iter 4  3.1664e-09    -26.7470     64.1860
iter 5  0.0000e+00    -53.5690    128.4000
iter 6  0.0000e+00   -107.1800    256.8200
iter 7  0.0000e+00   -214.3700    513.6500
iter 8  0.0000e+00   -428.7500   1027.3000

From \(\theta_0 = 0.5\), inside the region where the quadratic model is adequate, Newton converges quadratically to zero. From \(\theta_0 = 0.8\) the first step already overshoots into the tail, and from \(\theta_0 = 1.2\) — beyond the inflection point of \(-\ell'\) — the iterates run away, each step landing further out where the derivative is flatter still.

Code
## Interactive versions of the same three trajectories (requires the animation
## package and a graphics device that can write animations)
library(animation)
ani.options(interval = 1, nmax = 50)
newton.method(FUN = g_c, init = 0.5, rg = c(-1.5, 1.5))
newton.method(FUN = g_c, init = 0.8, rg = c(-8, 1.5))
newton.method(FUN = g_c, init = 1.2, rg = c(-2, 20))

5.2.9.1 Shinylive App for Newton-Raphson on the Cauchy Likelihood

There is a shinylive app to show how the Newton-Raphson step diverges on the Cauchy likelihood from a starting value chosen by clicking, and how step halving and a robustified Hessian each repair it.

5.2.10 Univariate Fisher Scoring

Newton’s step divides by the observed curvature \(g'(\theta_k) = -\ell''(\theta_k)\), computed from the data at hand, which can be negative — or nearly zero — far from the minimum. Replacing it by the expected curvature \(nI(\theta_k)\) gives the method of scoring: \[ \theta_{k+1} = \theta_k - \frac{g(\theta_k)}{nI(\theta_k)} = \theta_k + \frac{\ell'(\theta_k)}{nI(\theta_k)} . \]

Because \(nI(\theta) > 0\) for every \(\theta\) by construction, the denominator never changes sign: every step of scoring is a descent step on \(\mathcal{L}\), even at a point where Newton’s step would point uphill or blow up. Near the minimum \(nI(\hat\theta) \approx g'(\hat\theta)\), so scoring and Newton coincide and both converge quickly; the two methods differ only in how they behave away from the root.

Code
fisher_scoring <- function(d1, nI, x0, tol = 1e-10, maxit = 100) {
  for (k in 1:maxit) {                                 # d1 = nll', nI = expected info
    x1 <- x0 - d1(x0) / nI(x0)
    if (abs(x1 - x0) < tol) return(c(root = x1, iter = k))
    x0 <- x1
  }
  stop("not converged")
}

## a poor start: at lambda = 3.5 the curvature nll'' is nearly flat
c(newton_step_from_3.5 = 3.5 - g(3.5) / dg(3.5))  # overshoots to an infeasible negative lambda
newton_step_from_3.5 
          -0.4826613 
Code
rbind(newton  = tryCatch(newton(g, dg, 3.5), error = function(e) c(root = NA, iter = NA)),
      scoring = fisher_scoring(g, info, 3.5))
            root iter
newton        NA   NA
scoring 1.454235    5

From this poor start, Newton’s step lands on a negative — and hence infeasible — value of \(\lambda\) and never recovers, while scoring’s positive denominator lands its very first step near \(\lambda \approx 1.64\), already close to the root, and converges normally from there. The price of that safety is that \(nI(\theta)\) must be available in closed form; where it is not, Newton with the safeguards below is the fallback.

5.2.11 Safeguards

Three remedies cover most cases. Start from a robust estimate — the sample median for a location model. Apply step halving: accept the full Newton step only if it decreases \(\mathcal{L}\), otherwise halve it until it does. Or replace the observed curvature by the expected information, giving the method of scoring, \[ \theta_{k+1} = \theta_k - \frac{g(\theta_k)}{n I(\theta_k)} , \] in which the divisor is positive by construction, so every step descends. Near the MLE \(nI(\hat\theta) \approx g'(\hat\theta)\), so scoring inherits Newton’s speed while being far more stable away from the optimum. Its cost is that the expectation must be available in closed form.

Code
safe_newton <- function(f, d1, d2, x0, lower = -Inf, upper = Inf,
                        tol = 1e-10, maxit = 100, max_halve = 40) {
  f0 <- f(x0)                                    # f = nll, d1 = nll', d2 = nll''
  for (k in 1:maxit) {
    step <- -d1(x0) / d2(x0)
    if (d2(x0) < 0) step <- -sign(d1(x0))        # wrong curvature: just move downhill
    for (h in 0:max_halve) {                     # halve until feasible and improving
      x1 <- x0 + step / 2^h
      if (x1 > lower && x1 < upper && is.finite(f(x1)) && f(x1) <= f0) break
    }
    if (abs(x1 - x0) < tol) return(c(root = x1, iter = k, halvings = h))
    x0 <- x1; f0 <- f(x1)
  }
  stop("not converged")
}

5.2.12 Numerical Derivatives

When \(g\) or \(g'\) is tedious or error-prone to derive by hand, both can be approximated by finite differences: \[ g(\theta) \approx \frac{\mathcal{L}(\theta + h) - \mathcal{L}(\theta - h)}{2h}, \qquad g'(\theta) \approx \frac{\mathcal{L}(\theta + h) - 2\,\mathcal{L}(\theta) + \mathcal{L}(\theta - h)}{h^{2}}. \]

The step \(h\) trades off two sources of error: too large and the approximation suffers truncation error from the curvature of \(\mathcal{L}\) itself; too small and it suffers rounding error, because the numerator subtracts two nearly equal floating-point numbers. A step of \(h \approx \sqrt{\varepsilon}\,(1 + \lvert\theta\rvert) \approx 10^{-8}\) balances the two for the first derivative, and \(h \approx \varepsilon^{1/3}\) for the second, where \(\varepsilon \approx 2 \times 10^{-16}\) is double-precision machine epsilon.

Code
h <- 1e-5
c(analytic = dg(lhat),
  numeric  = (nll(lhat + h) - 2 * nll(lhat) + nll(lhat - h)) / h^2)
 analytic   numeric 
0.4995693 0.4995676 

The two agree to several digits, which is reassuring but also the point: nlm() and optim(method = "BFGS") fall back on exactly this kind of finite differencing whenever an analytic gradient is not supplied, and numDeriv::grad() / numDeriv::hessian() implement more accurate variants (Richardson extrapolation) for when a single finite difference is not precise enough.

5.2.13 Summary of the Methods

method needs convergence fails when
fixed point a rearrangement \(\theta = \varphi(\theta)\) linear, factor \(\lvert \varphi'\rvert\) \(\lvert \varphi'\rvert \ge 1\)
bisection \(g\) and a sign-changing bracket linear, factor \(1/2\) no bracket available
Newton–Raphson \(g\) and \(g'\) quadratic near the root flat or wrong curvature
method of scoring \(g\) and \(I(\theta)\) linear to quadratic \(E[\cdot]\) unavailable
golden section \(\mathcal{L}\) only, and a bracket linear, factor \(0.618\) \(\mathcal{L}\) not unimodal

5.2.14 Putting the Methods Together on the Truncated Poisson

Each method above was drawn on this model one step at a time. Running them to convergence side by side shows how the iterate sequences themselves differ.

5.2.14.1 The Three Iterations

Each method below returns the whole sequence of iterates together with the value of \(\mathcal{L}\) attained, so the trajectories can be compared directly. All three use the nll, g, dg and info — that is, \(\mathcal{L}\), \(g\), \(g'\) and \(I\) — defined at the start of this section, with simple iteration using the rearrangement \(\lambda = \overline x(1 - e^{-\lambda}) = \varphi(\lambda)\) derived earlier.

Code
## Each returns the iterate sequence together with the nll attained.
iterate_path <- function(step, n_iter, lambda0) {
  lambda <- numeric(n_iter + 1); lambda[1] <- lambda0
  for (i in 1:n_iter) lambda[i + 1] <- step(lambda[i])
  data.frame(lambda = lambda, nll = nll(lambda))
}

nzp.simple.iteration <- function(n_iter, lambda0)
  iterate_path(function(l) fp_map(l), n_iter, lambda0)

nzp.newton.raphson <- function(n_iter, lambda0)
  iterate_path(function(l) l - g(l) / dg(l), n_iter, lambda0)

nzp.method.of.scoring <- function(n_iter, lambda0)
  iterate_path(function(l) l - g(l) / info(l), n_iter, lambda0)

5.2.14.2 Comparison From Several Starting Values

The three methods are run for 15 iterations from a sensible start, a start far below, and a start far above.

Code
starts <- c(xbar, 0.1, 100)

trace_tab <- function(fun, n_iter = 15)
  vapply(starts, function(s) fun(n_iter, s)$lambda, numeric(n_iter + 1))

paths <- list(simple = trace_tab(nzp.simple.iteration),
              newton = trace_tab(nzp.newton.raphson),
              scoring = trace_tab(nzp.method.of.scoring))
Warning in log(l): NaNs produced
Warning in log(1 - exp(-l)): NaNs produced
Code
### first six iterates from each starting value
for (m in names(paths)) {
  cat("\n", m, "\n")
  tab <- paths[[m]][1:6, ]
  dimnames(tab) <- list(paste("iter", 0:5), sprintf("start = %.4g", starts))
  print(signif(tab, 6))
}

 simple 
       start = 1.897 start = 0.1 start = 100
iter 0       1.89744    0.100000   100.00000
iter 1       1.61291    0.180565     1.89744
iter 2       1.51926    0.313459     1.61291
iter 3       1.48214    0.510573     1.51926
iter 4       1.46643    0.758687     1.48214
iter 5       1.45961    1.008900     1.46643

 newton 
       start = 1.897 start = 0.1 start = 100
iter 0       1.89744    0.100000      100.00
iter 1       1.34531    0.194248    -5070.27
iter 2       1.44706    0.366193         NaN
iter 3       1.45420    0.649619         NaN
iter 4       1.45423    1.024460         NaN
iter 5       1.45423    1.337120         NaN

 scoring 
       start = 1.897 start = 0.1 start = 100
iter 0       1.89744     0.10000   100.00000
iter 1       1.46976     1.73860     1.89744
iter 2       1.45426     1.46102     1.46976
iter 3       1.45423     1.45424     1.45426
iter 4       1.45423     1.45423     1.45423
iter 5       1.45423     1.45423     1.45423

The pattern is the one the theory predicts. Newton–Raphson takes the fewest iterations from a good start, and its digits lock in from the left as the error is squared each step. Simple iteration converges from every start, including the extreme ones, but slowly and at a steady rate. The method of scoring is intermediate: always descending on \(\mathcal{L}\), and nearly as fast as Newton once close.

Code
err <- sapply(paths, function(p) abs(p[, 1] - lhat))   # from the xbar start

par(mar = c(4, 4.5, 2.5, 1))
matplot(0:15, pmax(err, 1e-17), log = "y", type = "b", pch = 19, lty = 1, col = c(4, 1, 2),
        xlab = TeX(r"(iteration $k$)"), ylab = TeX(r"($|\lambda_k - \hat{\lambda}|$)"),
        main = TeX(r"(error decay from $\lambda_0 = \bar{x}$)"))
legend("bottomleft", bty = "n", pch = 19, lty = 1, col = c(4, 1, 2),
       legend = c("simple iteration", "Newton-Raphson", "method of scoring"))
Figure 5.12: Compares the convergence speed of three iterative algorithms on the same problem by showing how quickly each reduces its error. Error decay (log scale) of simple iteration, Newton-Raphson, and the method of scoring for the zero-truncated Poisson MLE: Newton’s error curve bends downward, reflecting quadratic convergence.

On a log scale a linear method is a straight line; Newton’s curve bends downward until it reaches the floor set by machine precision.

Code
c(newton      = unname(newton(g, dg, xbar)["root"]),
  fixed_point = unname(fixed_point(fp_map, xbar)["root"]),
  bisection   = unname(bisection(g, 0.1, 20)["root"]),
  golden      = golden(nll, c(0.1, 20), eps = 1e-8),
  uniroot     = uniroot(g, c(0.1, 20), tol = 1e-10)$root,
  optimize    = optimize(nll, c(0.1, 20), tol = 1e-10)$minimum)
     newton fixed_point   bisection      golden     uniroot    optimize 
   1.454235    1.454235    1.454235    1.454235    1.454235    1.454235 

All six agree, which is the point of running them together: the methods differ in cost and robustness, not in the answer.

5.2.14.3 Standard Error From the Curvature

The optimizer has already produced everything inference needs: the curvature \(g'(\hat\lambda)\) that Newton divided by is the observed information per observation, so the total is \(J(\hat\lambda) = n\,g'(\hat\lambda)\).

Code
n_obs  <- length(xp)
se_obs <- 1 / sqrt(n_obs * dg(lhat))      # observed information
se_exp <- 1 / sqrt(n_obs * info(lhat))        # expected information

c(mle = lhat, se_observed = se_obs, se_expected = se_exp)
        mle se_observed se_expected 
  1.4542358   0.1601972   0.1601971 
Code
lhat + c(-1.96, 1.96) * se_obs                     # 95% Wald interval
[1] 1.140249 1.768222
Code
lam_se <- seq(lhat - 4 * se_obs, lhat + 4 * se_obs, length.out = 300)
quad <- 0.5 * n_obs * dg(lhat) * (lam_se - lhat)^2

par(mar = c(4, 4.5, 2.5, 1))
plot(lam_se, n_obs * (nll(lam_se) - nll(lhat)), type = "l", lwd = 2,
     xlab = TeX(r"($\lambda$)"), ylab = TeX(r"($L(\lambda) - L(\hat{\lambda})$)"),
     main = TeX(r"(negative log-likelihood and its quadratic approximation at $\hat{\lambda}$)"))
lines(lam_se, quad, col = 2, lwd = 2, lty = 2)
abline(h = 1.92, col = 4, lty = 3)
abline(v = lhat + c(-1.96, 1.96) * se_obs, col = 4, lty = 3)
legend("top", bty = "n", lwd = 2, lty = c(1, 2), col = c(1, 2),
       legend = TeX(c(r"($L(\lambda) - L(\hat{\lambda})$)", r"(quadratic from $J(\hat{\lambda})$)")))
Figure 5.13: Shows how the curvature of the negative log-likelihood at the MLE yields a standard error and a Wald confidence interval, by approximating it with a parabola. Zero-truncated Poisson negative log-likelihood versus its quadratic approximation at \(\hat\lambda\), with the resulting Wald 95% interval marked.

The parabola implied by the observed information tracks the negative log-likelihood closely over the width of the interval, so the Wald interval is trustworthy here.

5.2.14.4 Evaluating the Naive Estimator

All of this machinery is worth the effort only if the MLE beats the easy alternative. The naive estimator ignores the truncation and reports \(\overline x\). Simulating many truncated samples gives the sampling distributions of both estimators.

Code
mle_nzp <- function(n)
  uniroot(function(l) g(l, xb = mean(n)), c(1e-6, 50), tol = 1e-10)$root

sim_nzp <- function(lambda, n_gen = 100, B = 500)
  vapply(1:B, function(b) {
    y <- rpois(n_gen, lambda); n <- y[y > 0]
    c(naive = mean(n), mle = mle_nzp(n))
  }, c(naive = 0, mle = 0))

for (lambda_true in c(0.5, 1.5)) {
  est <- sim_nzp(lambda_true)
  tab <- t(apply(est, 1, function(e)
    c(bias = mean(e) - lambda_true, sd = sd(e), rmse = sqrt(mean((e - lambda_true)^2)))))
  cat("\nlambda =", lambda_true, "\n"); print(round(tab, 4))
}

lambda = 0.5 
         bias     sd   rmse
naive  0.7720 0.0906 0.7773
mle   -0.0012 0.1549 0.1547

lambda = 1.5 
         bias     sd   rmse
naive  0.4248 0.1220 0.4420
mle   -0.0107 0.1666 0.1668
Code
est <- sim_nzp(1.5)
par(mfrow = c(1, 2), mar = c(4, 4.5, 2.5, 1))
for (m in c("naive", "mle")) {
  hist(est[m, ], breaks = 25, col = "grey90",
       xlim = range(est), xlab = TeX(r"($\hat{\lambda}$)"),
       main = TeX(sprintf(r"(%s, bias $= %.3f$)", m, mean(est[m, ]) - 1.5)))
  abline(v = 1.5, col = 2, lwd = 2, lty = 2)
}
Figure 5.14: Uses simulation to show why the MLE is worth computing: ignoring the truncation gives a biased estimator, whereas the MLE is centred on the truth. Sampling distributions of the naive estimator \(\bar x\) versus the MLE for a zero-truncated Poisson mean, \(\lambda_{true} = 1.5\): the naive estimator is badly biased upward, the MLE is centred on the truth.

The naive estimator is biased upward, and severely so when \(\lambda\) is small, since that is when most of the probability mass sits on the discarded zero. Its bias does not shrink with sample size: it reflects the wrong model, not too little data. The MLE is centred on the truth with a standard deviation matching the information-based standard error of the previous section. The optimization was the means; the correct estimator was the end.

5.3 Nelder–Mead Method for Multivariate Optimization

Golden section search needs a bracket, which exists only in one dimension. Nelder–Mead generalizes the idea: it keeps \(p+1\) points, a simplex, in \(\mathbb{R}^p\) (a segment in 1D, a triangle in 2D, a tetrahedron in 3D) and, at each iteration, replaces the worst point by a better one, using only function values: no gradient, no Hessian, no bracket. The simplex then crawls downhill, stretching where the objective falls steadily and shrinking around a minimum, so that it adapts its shape to the local contours of \(\mathcal{L}\) without ever computing a derivative.

5.3.1 One iteration

Write \(f = \mathcal{L}\) for the objective. Order the \(p+1\) vertices so that \[ f(v_1) \le f(v_2) \le \cdots \le f(v_{p+1}), \] so that \(v_1\) is the best and \(v_{p+1}\) the worst, and let \(c = \frac1p\sum_{j=1}^{p} v_j\) be the centroid of all vertices except the worst. The worst vertex is reflected through \(c\), and, depending on how good the new point is, the simplex is expanded, contracted or shrunk. The trial points are \[ x_r = c + \alpha\,(c - v_{p+1}), \qquad x_e = c + \gamma\,(x_r - c), \qquad x_c = c + \rho\,(v_{p+1} - c), \] the reflection, the expansion and the (inside) contraction, with coefficients \(\alpha = 1\), \(\gamma = 2\), \(\rho = \tfrac12\) and shrink factor \(\sigma = \tfrac12\), the values used by optim.

Figure 5.15: Shows the trial points of one Nelder-Mead iteration, which are all on the line through the worst vertex and the centroid of the other two. A triangle with vertices \(v_1\) (best), \(v_2\) and \(v_3\) (worst): the reflection \(x_r\), the expansion \(x_e\) beyond it, and the contraction \(x_c\) between \(c\) and the worst vertex.

The decision rule compares \(f(x_r)\) with the current values:

Table 5.1: Lists the actions of one Nelder–Mead iteration according to how good the reflected point is. Decision rule of the Nelder–Mead method.
Outcome of the reflection Action
\(f(x_r) < f(v_1)\), better than the best Try the expansion \(x_e\); keep the better of \(x_e\) and \(x_r\) in place of \(v_{p+1}\)
\(f(v_1) \le f(x_r) < f(v_p)\), better than the second worst Accept \(x_r\) in place of \(v_{p+1}\)
\(f(v_p) \le f(x_r) < f(v_{p+1})\), only slightly better than the worst Outside contraction \(x_c = c + \rho(x_r - c)\); accept it if \(f(x_c) \le f(x_r)\), otherwise shrink
\(f(x_r) \ge f(v_{p+1})\), no better than the worst Inside contraction \(x_c = c + \rho(v_{p+1} - c)\); accept it if \(f(x_c) < f(v_{p+1})\), otherwise shrink
Contraction failed Shrink: \(v_j \leftarrow v_1 + \sigma (v_j - v_1)\) for \(j = 2, \ldots, p+1\)

A typical iteration costs one or two function evaluations; only a shrink costs \(p\) more. The simplex needs a starting point and a size: optim builds the initial simplex from the starting value \(\theta_0\) by moving each coordinate in turn by a step proportional to the scale of the parameter. The iteration stops when the spread of the function values over the simplex, \(|f(v_{p+1}) - f(v_1)|\), falls below a tolerance (reltol, default \(10^{-8}\) relative), which is a criterion on \(f\) rather than on the gradient: it does not certify that a stationary point has been reached.

5.3.2 Implementation

The whole algorithm fits in a few lines. The function below returns the final point and also all the simplices visited, so that the path can be drawn.

Code
## Nelder-Mead for f(theta); x0 is the start, step the size of the initial simplex
nelder_mead <- function(f, x0, step = 0.5, maxit = 500, tol = 1e-10,
                        alpha = 1, gamma = 2, rho = 0.5, sigma = 0.5) {
  p <- length(x0)
  V <- rbind(x0, t(sapply(1:p, function(j) x0 + step * (1:p == j))))   # (p+1) x p simplex
  fv <- apply(V, 1, f); nev <- p + 1; hist <- list(V)
  for (it in 1:maxit) {
    o <- order(fv); V <- V[o, , drop = FALSE]; fv <- fv[o]            # v_1 best, v_{p+1} worst
    if (abs(fv[p + 1] - fv[1]) < tol * (abs(fv[1]) + tol)) break      # spread of f over the simplex
    cen <- colMeans(V[1:p, , drop = FALSE])                           # centroid of all but the worst
    xr <- cen + alpha * (cen - V[p + 1, ]); fr <- f(xr); nev <- nev + 1
    if (fr < fv[1]) {                                                 # better than the best: expand
      xe <- cen + gamma * (xr - cen); fe <- f(xe); nev <- nev + 1
      if (fe < fr) { V[p + 1, ] <- xe; fv[p + 1] <- fe } else { V[p + 1, ] <- xr; fv[p + 1] <- fr }
    } else if (fr < fv[p]) {                                          # better than the second worst
      V[p + 1, ] <- xr; fv[p + 1] <- fr
    } else {                                                          # poor: contract, outside or inside
      xc <- if (fr < fv[p + 1]) cen + rho * (xr - cen) else cen + rho * (V[p + 1, ] - cen)
      fc <- f(xc); nev <- nev + 1
      if (fc < min(fr, fv[p + 1])) {
        V[p + 1, ] <- xc; fv[p + 1] <- fc
      } else {                                                        # contraction failed: shrink
        for (i in 2:(p + 1)) V[i, ] <- V[1, ] + sigma * (V[i, ] - V[1, ])
        fv[2:(p + 1)] <- apply(V[2:(p + 1), , drop = FALSE], 1, f); nev <- nev + p
      }
    }
    hist[[length(hist) + 1]] <- V
  }
  o <- order(fv)
  list(par = V[o[1], ], value = unname(fv[o[1]]), iterations = it, evals = nev, hist = hist)
}

5.3.3 Example: the Rosenbrock banana

The Rosenbrock function \(f(\theta) = (1-\theta_1)^2 + 100(\theta_2 - \theta_1^2)^2\) has its minimum at \((1, 1)\), at the bottom of a long, curved, narrow valley: easy to find, hard to follow. It is the standard test of an optimizer.

Code
rosen <- function(v) (1 - v[1])^2 + 100 * (v[2] - v[1]^2)^2       # Rosenbrock "banana", min at (1, 1)
rosen_gr <- function(v) c(-2 * (1 - v[1]) - 400 * v[1] * (v[2] - v[1]^2), 200 * (v[2] - v[1]^2))

nm <- nelder_mead(rosen, c(-1.2, 1))
c(nm$par, f = nm$value, iterations = nm$iterations, evals = nm$evals)
                                     f   iterations        evals 
1.000000e+00 1.000000e+00 1.238327e-20 1.240000e+02 2.350000e+02 
Code
## the same method in optim, and gradient-based methods on the same problem
cmp <- rbind("Nelder-Mead (optim)" = optim(c(-1.2, 1), rosen, method = "Nelder-Mead")$counts,
             "BFGS, numerical gradient" = optim(c(-1.2, 1), rosen, method = "BFGS")$counts,
             "BFGS, analytic gradient" = optim(c(-1.2, 1), rosen, rosen_gr, method = "BFGS")$counts)
cmp
                         function gradient
Nelder-Mead (optim)           195       NA
BFGS, numerical gradient      120       38
BFGS, analytic gradient       110       43
Code
g1 <- seq(-1.6, 1.6, length.out = 200); g2 <- seq(-0.6, 2.2, length.out = 200)
z <- outer(g1, g2, Vectorize(function(a, b) rosen(c(a, b))))
contour(g1, g2, log10(z + 1e-3), nlevels = 18, col = "grey70", xlab = expression(theta[1]),
        ylab = expression(theta[2]), drawlabels = FALSE)
for (k in seq(1, length(nm$hist), by = 2)) polygon(nm$hist[[k]], border = "grey40", lwd = 0.7)
best <- t(sapply(nm$hist, function(V) V[which.min(apply(V, 1, rosen)), ]))
lines(best, col = 2, lwd = 2); points(best[1, 1], best[1, 2], pch = 19, col = 2)
points(1, 1, pch = 19, col = 4, cex = 1.3)
Figure 5.16: Shows how the Nelder-Mead simplex follows the curved valley of the Rosenbrock function using only function values. Contours of the Rosenbrock function with the successive simplices of Nelder-Mead (grey triangles, every second iteration) and the path of the best vertex (red); the method starts at \((-1.2, 1)\) and ends at the minimum \((1, 1)\) (blue).

Starting from \((-1.2, 1)\) the simplex first stretches and tumbles down into the valley, then follows it round the bend, shrinking as it goes, and finally collapses onto \((1,1)\). The counts in the table compare the cost: Nelder–Mead needs somewhat more function evaluations than BFGS does here (195 against 110 to 120), but no gradient evaluations at all. In two parameters the difference is small; it grows quickly with \(p\), since BFGS needs about the same number of iterations whereas the simplex has more vertices to improve.

5.3.4 Strengths and limits

  • No derivatives. It needs only \(f\), so it applies to noisy, non-differentiable or awkward objectives, such as those from simulations or a likelihood with an integral that has no usable derivative, and when coding a gradient is not worth the effort.
  • Robust to a poor start. The simplex is large at first and does not rely on a quadratic model, so it rarely diverges as Newton–Raphson can.
  • Slow and unreliable in high dimension. The simplex has \(p+1\) vertices to improve, convergence is only sublinear, and beyond about ten parameters it becomes impractical. It can also stall on a ridge or in a valley, giving a point that is not a stationary point; restarting from the returned value, with a new simplex, is the usual remedy.
  • No standard errors. It produces no curvature. After Nelder–Mead the Hessian must be obtained by numerical differentiation at the solution, as optim(..., hessian = TRUE) does.

This makes it, as optim’s default method, a reasonable first look at an unfamiliar objective with a few parameters, but a poor choice for a smooth likelihood that is fitted many times or has many parameters, where the gradient-based methods of the next sections are far more efficient.

5.3.5 Shinylive App for the Nelder–Mead Algorithm

There is a shinylive app to show how the Nelder-Mead method finds a minimum without derivatives, by repeatedly reflecting, expanding, contracting, and shrinking a simplex.

5.4 Multivariate Newton-Raphson Optimzation Methods

5.4.1 Gradient and Hessian: A Geometric Illustration

The two objects entering the multivariate Newton step have a direct geometric reading. The negative gradient \(-g(\theta)\) is the direction of steepest descent and is perpendicular to the contour of \(\mathcal{L}\) through \(\theta\); the Hessian describes how that direction curves, and at a strict local minimum \(J(\theta) = \nabla g(\theta)\) is positive definite. Near \(\hat\theta\), \(\mathcal{L}\) is well approximated by the quadratic \[ \mathcal{L}(\theta) \approx \mathcal{L}(\hat\theta) + \tfrac12(\theta - \hat\theta)^\top J(\hat\theta)(\theta - \hat\theta), \] whose contours are ellipses centred at \(\hat\theta\). Writing the eigendecomposition \(J(\hat\theta) = Q\Lambda Q^\top\), the axes of these ellipses point along the eigenvectors in \(Q\), and the half-length along axis \(j\) is proportional to \(1/\sqrt{\lambda_j}\): a large eigenvalue means sharp curvature and a short, well-determined direction, while \(\lambda_{\max}/\lambda_{\min}\) measures how elongated — and, once the axes are tilted, how correlated the parameters are.

Figure 5.17: Gives a geometric picture of the gradient and the Hessian in two dimensions, explaining why the direction of steepest descent generally does not point at the minimum. Contours of a quadratic negative log-likelihood: steepest-descent arrows (green) run perpendicular to the contours but do not point at the minimum unless the ellipses are circular; the dashed axes are the eigenvectors of \(J(\hat\theta)\), each with half-length \(1/\sqrt{\lambda_j}\).

Every arrow in the figure is perpendicular to the contour it starts on and points generally downhill toward the minimum at the origin, but only an arrow started exactly on a principal axis points straight at it. This is why Newton’s step, which multiplies the gradient by \(J(\hat\theta)^{-1}\) rather than simply following it, converges directly to \(\hat\theta\): it rescales and rotates the descent direction to undo exactly this distortion. The plain gradient-descent methods of a later section have no such correction, and zig-zag across elongated contours like these.

5.4.2 Naive Newton-Raphson Iterations

In the previous section the parameter was a scalar, and the log-likelihood could be inspected by plotting it. Once \(\theta \in \mathbb{R}^p\) neither is true: we cannot see the surface, and the notion of “bracketing a root” that underlies bisection and golden section has no direct multivariate analogue. What survives is the local quadratic approximation, and the Newton–Raphson algorithm is its direct consequence.

This section develops the multivariate Newton–Raphson method and its statistical variant, Fisher scoring, applies both to logistic regression, examines interactively how and why the iteration can diverge, and closes with a survey of the derivative-free and gradient-only alternatives.

Write the log-likelihood as \(\ell(\theta)\) for \(\theta \in \mathbb{R}^p\), and define. \[ g(\theta) = \nabla \mathcal{L}(\theta) = -\frac{\partial \ell(\theta)}{\partial \theta} \quad (p \times 1, \text{ the gradient}), \qquad \nabla g(\theta) = -\frac{\partial^2 \ell(\theta)}{\partial \theta \, \partial \theta^{\top}} \quad (p \times p, \text{ the Hessian of } \mathcal{L}). \]

The univariate \(g = \mathcal{L}'\) becomes the gradient \(g = \nabla\mathcal{L}\) with no change of meaning. As before, the Hessian of \(\mathcal{L}\) is the observed information matrix, so we write \(J(\theta) = \nabla g(\theta)\) throughout.

Expand \(\mathcal{L}\) to second order about the current iterate \(\theta^{(k)}\): \[ \mathcal{L}(\theta) \;\approx\; \mathcal{L}(\theta^{(k)}) + g(\theta^{(k)})^{\top}(\theta - \theta^{(k)}) + \tfrac{1}{2}\,(\theta - \theta^{(k)})^{\top} J(\theta^{(k)}) \,(\theta - \theta^{(k)}). \]

This is a quadratic in \(\theta\). If \(J(\theta^{(k)})\) is positive definite the quadratic has a unique minimum, found by setting its derivative to zero: \[ g(\theta^{(k)}) + J(\theta^{(k)})(\theta - \theta^{(k)}) = 0 . \]

Solving for \(\theta\) gives the Newton–Raphson update: \[ \boxed{\;\theta^{(k+1)} = \theta^{(k)} - J(\theta^{(k)})^{-1} g(\theta^{(k)}) \;}. \]

The algorithm therefore replaces the negative log-likelihood by the quadratic that matches its value, slope and curvature at the current point, and jumps to the minimum of that quadratic. Everything that follows — the fast convergence, and the modes of failure — comes from how good or bad that replacement is.

Three remarks are worth making explicit.

The inverse is never formed. Write the update as \(\theta^{(k+1)} = \theta^{(k)} + \delta^{(k)}\) where \(\delta^{(k)}\) solves the linear system \(J(\theta^{(k)})\,\delta = -g(\theta^{(k)})\). In R this is solve(J, -g), not solve(J) %*% (-g). The former is faster and numerically more stable; the latter computes \(p^2\) numbers in order to use \(p\) of them.

The step is not the steepest-descent direction. \(\delta^{(k)} = -J^{-1}g\) points along \(-g\) only when \(J\) is a multiple of the identity. The information matrix rescales and rotates the descent direction to account for differing curvature and correlation among the parameters, which is exactly why Newton–Raphson is insensitive to the scaling of the parameters while gradient descent is not.

Positive definiteness is an assumption, not a guarantee. Away from the minimum \(J(\theta^{(k)})\) can be indefinite or singular, in which case the “minimum of the quadratic” is a saddle point or does not exist, and \(\delta^{(k)}\) may point uphill. Section 5.4.5 examines this.

5.4.2.1 Fisher scoring

The observed information \(J(\theta)\) depends on the data. Its expectation under the model, \[ I(\theta) = E_{\theta}\{J(\theta)\} = -E_{\theta}\left\{ \frac{\partial^2 \ell(\theta)}{\partial \theta \, \partial \theta^{\top}} \right\}, \]

is the expected (Fisher) information matrix. Replacing \(J\) by \(I\) in the update gives the method of scoring: \[ \theta^{(k+1)} = \theta^{(k)} - I(\theta^{(k)})^{-1} g(\theta^{(k)}). \]

The two algorithms have different iterates but the same fixed point, since both stop where \(g(\theta) = 0\). The practical differences:

Table 5.2: Contrasts Newton-Raphson with Fisher scoring, the two choices of curvature matrix in a second-order update, to help decide between them. Newton–Raphson compared with Fisher scoring
Newton–Raphson Fisher scoring
Curvature matrix Observed information \(J(\theta)\) Expected information \(I(\theta)\)
Derivation Second derivative of \(\mathcal{L}\) Expectation of that second derivative
Positive definite? Only guaranteed near the minimum Always, wherever the model is identified
Algebra Can be messy Often simpler, as data-dependent terms average out
Local convergence Quadratic Linear, but typically with a very small rate constant
Stability from poor starts Less stable More stable

The guaranteed positive definiteness of \(I(\theta)\) is the main practical argument for scoring: the update direction is always a descent direction on \(\mathcal{L}\), so the iteration cannot be sent uphill by an ill-behaved Hessian.

For generalised linear models with the canonical link the two coincide exactly, because the second derivative of the log-likelihood does not involve the response. Logistic regression uses the canonical logit link, so the example below is simultaneously Newton–Raphson, Fisher scoring, and — as we note at the end of the section — iteratively reweighted least squares.

5.4.3 Convergence properties

Near a minimum \(\hat\theta\) of \(\mathcal{L}\) at which \(J(\hat\theta)\) is positive definite, Newton–Raphson converges quadratically: the error satisfies \[ \lVert\theta^{(k+1)} - \hat\theta\rVert \;\le\; C \,\lVert\theta^{(k)} - \hat\theta\rVert^{2}, \]

for some constant \(C\) depending on the third derivatives. Informally, the number of correct digits doubles at each iteration, which is why five or six iterations are usually enough and why a well-started run appears to converge instantly.

The qualification “near” is essential. The theorem is local: it guarantees nothing about a start far from \(\hat\theta\), and nothing at all if \(J\) fails to be positive definite along the way.

5.4.4 Standard errors as a by-product

At convergence the same matrix that drove the iteration supplies the asymptotic variance. Under the usual regularity conditions, \[ \hat\theta \;\dot\sim\; N\!\left(\theta, \; J(\hat\theta)^{-1}\right), \]

so \(\operatorname{\mathrm{SE}}(\hat\theta_j) = \sqrt{[J(\hat\theta)^{-1}]_{jj}}\). This is a genuine practical advantage of Newton-type methods over derivative-free ones: the curvature information needed for inference has already been computed. Methods that never form a Hessian require a separate numerical differentiation step, which is what hessian = TRUE requests from nlm and optim.

5.4.5 Enhancements to Multivariate Newton-Raphson Methods

Newton–Raphson can diverge even on a well-behaved problem. Far from \(\hat\theta\) the quadratic approximation is unreliable, and \(J_k = J(\theta^{(k)})\) may be nearly singular or not positive definite, so the raw step \(\delta^{(k)} = -J_k^{-1}g_k\), with \(g_k = g(\theta^{(k)})\), can be enormous or fail to point downhill at all. The standard enhancements all keep the Newton idea and add a guard.

  • Step halving (line search). Move \(\theta^{(k+1)} = \theta^{(k)} + s\,\delta^{(k)}\), starting at \(s = 1\) and halving until \(\mathcal{L}\) decreases by enough — the Armijo condition \(\mathcal{L}(\theta^{(k)} + s\,\delta^{(k)}) \le \mathcal{L}(\theta^{(k)}) + c_1 s\,g_k^\top\delta^{(k)}\) with a tiny \(c_1\) (say \(10^{-4}\)). Near the minimum \(s = 1\) passes on the first try, so fast local convergence is untouched.

    The condition asks for more than a lower value of \(\mathcal{L}\). Written as \[ \underbrace{\mathcal{L}(\theta^{(k)} + s\,\delta^{(k)}) - \mathcal{L}(\theta^{(k)})}_{\text{actual decrease}} \;\le\; c_1 \cdot \underbrace{s\,g_k^\top\delta^{(k)}}_{\text{decrease predicted by the tangent plane}}, \] it says that the decrease must be at least a fixed fraction of what the slope promises for a step of that length. (When \(J_k\) is positive definite, \(g_k^\top\delta^{(k)} = -g_k^\top J_k^{-1} g_k < 0\), so the right-hand side is a genuine decrease.) Asking only for some decrease is not enough, because the gains can shrink so fast that the iterates stall at a point that is not a minimum. For \(\mathcal{L}(\theta) = \theta^2\), the sequence \(\theta^{(k)} = (-1)^k(1 + 2^{-k})\) lowers \(\mathcal{L}\) at every step and moves downhill each time, yet it bounces towards \(\pm 1\), where the gradient is \(\pm 2\), never \(0\). With \(\delta = -g = -2\theta\) the Armijo condition reduces to \(s \le 1 - c_1\), and the overshooting steps need \(s \to 1\), so they are rejected. Armijo only forbids steps that are too long for the gain they deliver. Starting at \(s = 1\) and halving only when the test fails is what keeps the accepted steps from being needlessly short.

  • Ridging (Levenberg–Marquardt). Replace \(J_k\) by \(J_k + \tau\mathbb{I}\), with \(\tau \ge 0\) just large enough to make it positive definite. This repairs indefiniteness and shortens the step; as \(\tau\) grows the direction rotates from Newton toward plain gradient descent, and \(\tau = 0\) wherever \(J_k\) is already well behaved. This is roughly what nlm does internally.

    A simple rule takes \(\tau = \max\{0,\ \epsilon - \lambda_{\min}(J_k)\}\), which lifts the smallest eigenvalue of \(J_k\) to a small positive \(\epsilon\). A modified Cholesky factorisation achieves the same without an eigendecomposition. Since \(\delta^{(k)}(\tau) = -(J_k + \tau\mathbb{I})^{-1}g_k\) has length decreasing in \(\tau\), ridging also acts as a step-length control, and with a positive definite matrix the direction satisfies \(g_k^\top\delta^{(k)} < 0\), so a line search along it is guaranteed to find a decrease. A related guard for GLMs is Fisher scoring: the expected information \(I(\theta) = X^\top W X\) is positive semi-definite at every \(\theta\), and positive definite whenever \(X\) has full column rank and all weights are positive, so the ridge is rarely needed at all.

  • Trust regions. Minimise the quadratic approximation only within \(\lVert\theta - \theta^{(k)}\rVert \le \Delta_k\), growing or shrinking \(\Delta_k\) according to how well the quadratic predicted the realised change. This is nlminb and the PORT routines.

    The quality of the prediction is measured by the ratio \[ \rho_k = \frac{\mathcal{L}(\theta^{(k)} + \delta) - \mathcal{L}(\theta^{(k)})} {g_k^\top\delta + \tfrac12\,\delta^\top J_k\,\delta}, \] actual decrease over the decrease predicted by the quadratic model. A typical rule rejects the step if \(\rho_k\) is small or negative and shrinks \(\Delta_k\) (say by a factor of four); it accepts the step and keeps \(\Delta_k\) when \(\rho_k\) is moderate; and it doubles \(\Delta_k\) when \(\rho_k \ge 3/4\) and the step reached the boundary. The constrained subproblem has the solution \(\delta = -(J_k + \tau\mathbb{I})^{-1}g_k\) for some \(\tau \ge 0\), so a trust region is ridging with \(\tau\) chosen by the step length rather than the other way round. Unlike a line search, it first fixes how far to go and then chooses the direction. That makes it robust when \(J_k\) is indefinite: at a saddle point, where the Newton step points the wrong way, it still moves downhill.

  • Better parameterisation and starting values. Often the cheapest fix. Centring and scaling the covariates improves the conditioning of \(J\). Parameters with natural bounds are better optimised on an unconstrained scale — \(\log\sigma\) rather than \(\sigma\), \(\operatorname{logit} p\) rather than \(p\) — which removes steps that leave the parameter space and usually makes \(\ell\) closer to quadratic. Good starting values come from a simpler fit: method of moments estimates, a least-squares fit, or the null model with an intercept only. glm sidesteps the choice altogether: it starts from fitted means rather than coefficients, taking \(\mu_i^{(0)} = (y_i + 0.5)/2\) for binary data, which are always inside \((0, 1)\), and it therefore never begins in the flat region where the weights \(p_i(1-p_i)\) vanish.

Combining the first two gives safeguarded Newton: at each iteration form a positive definite \(\tilde J_k\), solve \(\tilde J_k\,\delta^{(k)} = -g_k\), then backtrack on \(s\). Both guards switch themselves off near a well-behaved maximum, so the method keeps Newton’s fast local convergence while still making progress from remote starting values.

5.4.5.1 Shinylive App for Safeguarded Newton-Raphson

There is a shinylive app to show how the full Newton-Raphson step diverges on a logistic-regression likelihood started far from the maximum, and how backtracking on the step length rescues the same starting value.

5.5 Example of Fitting Logistic Regression with Safeguarded Multivariate Newton-Raphson

5.5.1 The model and its derivatives

With a single covariate \(x_i\) and binary response \(y_i\), let \(u_i = \beta_0 + \beta_1 x_i\) and \(p_i = e^{u_i}/(1 + e^{u_i})\). The log-likelihood is \[ \ell(\beta) = \sum_{i=1}^{n}\left\{ y_i u_i - \log\left(1 + e^{u_i}\right) \right\}. \]

Writing \(X\) for the \(n \times 2\) design matrix with rows \((1, x_i)\), the gradient of \(\mathcal{L}(\beta) = -\ell(\beta)\) and the information are, \[ g(\beta) = -X^{\top}(y - p), \qquad J(\beta) = X^{\top} W X, \qquad W = \operatorname{diag}\{p_i(1 - p_i)\}. \]

Note that \(J\) does not involve \(y\). This is the canonical-link property mentioned above, and it means \(J(\beta) = I(\beta)\) identically.

Two numerical points, both carried over from the chapter on computer arithmetic. First, \(\log(1 + e^{u})\) overflows for \(u \gtrsim 710\); the stable form is \(\log(1+e^u) = \max(u,0) + \log(1 + e^{-\vert{}u\vert{}})\). Second, \(p_i(1-p_i) \to 0\) when \(\vert{}u_i\vert{}\) is large, so \(W\) — and hence \(J\) — degenerates toward singularity when the fitted probabilities are extreme. That observation is the whole explanation of the divergence we are about to see.

5.5.1.1 The model as a computation graph

Before differentiating anything it helps to fix the object being differentiated. Write the model as a computation graph: one node for every intermediate quantity — the inputs, each linear unit, each activation, the linear predictor, the loss — and one directed edge \(u \to z\) whenever \(z\) is computed directly from \(u\). The graph is acyclic, values flow forward along its edges, and the whole of backpropagation is two rules stated on it.

The first is the node rule. Write \[\delta_z = \frac{\partial \mathcal{L}_i}{\partial z}\] for the sensitivity of the loss to node \(z\). Since \(z\) can influence the loss only through the nodes it feeds, the multivariate chain rule gives one term per outgoing edge, \[\delta_z \;=\; \sum_{p \,\in\, \mathrm{consumers}(z)} \delta_p \, \frac{\partial p}{\partial z} ,\] and the recursion starts at the loss itself with \(\delta_{\mathcal{L}_i} = 1\).

The second is the edge rule. Every parameter sits on an edge that forms a linear combination, and it reaches \(z\) only through the single term \(\omega\, u\) it contributes, so \(\partial z/\partial \omega = u\) and \[\frac{\partial \mathcal{L}_i}{\partial \omega_{u \to z}} \;=\; \delta_z \cdot u :\] the sensitivity at the arrow’s head times the value at its tail.

That is the entire algorithm. Everything below — logistic regression here, a hidden layer later in this chapter, a hundred layers in a modern network — is these two rules applied to a larger graph. Note that the graph is neither a chain nor a tree: a unit typically feeds several others, so most nodes have more than one consumer, and the sum in the node rule is what gathers those contributions. Section 5.6.5 writes the two rules out for a network of arbitrary depth.

5.5.2 The gradient by the chain rule

The gradient above can be written down directly, but it is worth deriving it the way an automatic-differentiation system would, because the same mechanics carry over unchanged to models with many more layers. Logistic regression is a neural network — the smallest interesting one: two inputs, one linear unit, a sigmoid activation, and a cross-entropy loss.

Take a single observation and follow it forward through the model: \[ u_i = \beta_0 \cdot 1 + \beta_1 x_i, \qquad p_i = \sigma(u_i) = \frac{1}{1 + e^{-u_i}}, \qquad \mathcal{L}_i = -\left\{ y_i \log p_i + (1 - y_i)\log(1 - p_i) \right\} . \]

Each stage is a simple function of the one before it, so \(\partial \mathcal{L}_i / \partial \beta\) is a product of three local derivatives. Working backwards:

Loss with respect to the probability. Differentiating the cross-entropy, \[ \frac{\partial \mathcal{L}_i}{\partial p_i} = -\frac{y_i}{p_i} + \frac{1 - y_i}{1 - p_i} = \frac{p_i - y_i}{p_i(1 - p_i)} . \]

Probability with respect to the linear unit. The sigmoid has the convenient derivative \[ \frac{\partial p_i}{\partial u_i} = \sigma'(u_i) = p_i(1 - p_i) . \]

Linear unit with respect to each parameter. Every weight sees only the input it multiplies, so there is one local derivative per edge entering \(u_i\): \[ \frac{\partial u_i}{\partial \beta_0} = 1 \quad\text{(the edge from the constant node } 1 \text{)}, \qquad \frac{\partial u_i}{\partial \beta_1} = x_i \quad\text{(the edge from } x_i \text{)} . \]

Multiplying the first two gives the quantity that backpropagation carries between layers — the sensitivity of the loss to the linear unit, written \(\delta_i\): \[ \delta_i \;=\; \frac{\partial \mathcal{L}_i}{\partial u_i} = \frac{\partial \mathcal{L}_i}{\partial p_i}\cdot\frac{\partial p_i}{\partial u_i} = \frac{p_i - y_i}{p_i(1 - p_i)} \cdot p_i(1 - p_i) = p_i - y_i . \]

The factor \(p_i(1-p_i)\) cancels exactly. This cancellation is the reason cross-entropy is paired with a sigmoid rather than, say, squared error: the awkward \(\sigma'\) term disappears and the signal sent backwards is simply the prediction error \(p_i - y_i\). Multiplying by the last local derivative and summing over observations, \[ g(\beta) = \nabla \mathcal{L}(\beta) = \sum_{i=1}^{n} \delta_i \begin{pmatrix} \partial u_i/\partial\beta_0 \\ \partial u_i/\partial\beta_1 \end{pmatrix} = \sum_{i=1}^{n} (p_i - y_i)\begin{pmatrix} 1 \\ x_i \end{pmatrix} = X^{\top}(p - y) = -X^{\top}(y - p), \]

which is the expression used above and coded below. Note that \(\delta_i\) is also exactly the per-observation gradient contribution that stochastic gradient descent samples later in this chapter.

The diagram below lays the same computation out as a network, drawn the same way as the two-layer picture later in this chapter: values flow left to right in the top panel (the forward pass), and derivatives accumulate right to left in the bottom panel (the backward pass), each arrow carrying exactly one local derivative from the list above. The intercept is drawn as the weight \(\beta_0\) on an edge from a square constant node holding the value \(1\), placed directly above \(u_i\) so that the space beneath the row is free: there the sensitivity \(\delta\) of each node sits directly under that node, and the two parameter gradients are collected in the box beneath the fan of \(\beta\) arrows. A constant node has no sensitivity of its own, since nothing it holds depends on the parameters, so in the backward panel its edge carries the weight but no arrow.

Figure 5.18: Shows how the gradient of the negative log-likelihood is assembled by the chain rule, by drawing logistic regression as the one-unit neural network it is. Logistic regression as a one-unit network: the forward pass (top) maps inputs to a linear unit, a sigmoid and a loss; the backward pass (bottom) multiplies the local derivatives, and the sigmoid factor cancels so that the signal reaching the linear unit is the prediction error \(p_i - y_i\). As in the two-layer picture, each arrow carries only the local derivative of its head with respect to its tail, the sensitivity \(\delta\) of each node sits beneath that node, and the parameter gradients are collected in the box beneath the fan of weight arrows.

The red box marks the cancellation: whatever the sigmoid contributes on the way forward is undone on the way back, leaving \(\delta_i = p_i - y_i\). In a deeper network the same \(\delta\) would be passed further left and multiplied by the next layer’s local derivative; here there is only one layer, so the recursion stops immediately and \(g(\beta) = \sum_i \delta_i\,(1, x_i)^{\top}\).

5.5.3 Implementation in R

Gradient of Logistic Regression Model

### numerically stable log(1 + exp(u))
log1pexp <- function(u) pmax(u, 0) + log1p(exp(-abs(u)))

### negative log-likelihood, its gradient g, and the information matrix
nll_grad_info <- function(b, y, x) {
    u <- b[1] + b[2] * x
    p <- plogis(u)
    w <- p * (1 - p)
    list(
        neg_loglik = -sum(u * y - log1pexp(u)),
        grad       = -c(sum(y - p), sum(x * (y - p))),   # g(beta)
        inf        = matrix(c(sum(w), sum(x * w),
                              sum(x * w), sum(x * x * w)), 2, 2)
    )
}

neg_logp_logistic <- function(b, x, y) {
    u <- b[1] + b[2] * x
    -sum(u * y - log1pexp(u))
}

Because we minimise the negative log-likelihood, the update \(\beta^{(k+1)} = \beta^{(k)} + J^{-1}S\) becomes \(\beta^{(k+1)} = \beta^{(k)} - J^{-1} g(\beta^{(k)})\), which is the form coded below.

Newton-Raphson Iterations for Logistic Regression

mle_logistic_nr <- function(b0, no_iter, y, x, debug = FALSE) {
    out <- matrix(NA_real_, no_iter + 1, 3,
                  dimnames = list(NULL, c("beta0", "beta1", "neg_loglike")))
    b <- b0
    for (i in seq_len(no_iter + 1)) {
        q <- nll_grad_info(b, y, x)
        out[i, ] <- c(b, q$neg_loglik)
        if (debug) print(out[i, ])
        if (i > no_iter) break
        step <- tryCatch(solve(q$inf, q$grad),
                         error = function(e) rep(NA_real_, 2))
        if (any(!is.finite(step))) break
        b <- b - step
    }
    out
}
Code
gen_logistic_data <- function(b, n) {
    x <- sort(runif(n, -2, 2))
    p <- plogis(b[1] + b[2] * x)
    y <- rbinom(n, 1, p)
    plot(x, p, type = "l", ylim = c(0, 1), ylab = "P(y = 1)")
    points(x, y, col = y + 1)
    list(x = x, y = y)
}

set.seed(1)
data <- gen_logistic_data(c(0, 1.5), 200)
Figure 5.19: Shows the simulated logistic-regression data used as a running example for multivariate optimization. Simulated logistic data: true success probability and observed responses.

A start close to the truth converges in a handful of iterations:

Code
round(mle_logistic_nr(c(0, 3), 8, data$y, data$x), 6)
          beta0     beta1 neg_loglike
 [1,]  0.000000  3.000000   117.03598
 [2,] -0.505055 -0.306054   164.86061
 [3,] -0.040097  1.281039    99.03765
 [4,] -0.251124  1.470987    97.74624
 [5,] -0.272205  1.502838    97.72964
 [6,] -0.272532  1.503424    97.72964
 [7,] -0.272532  1.503424    97.72964
 [8,] -0.272532  1.503424    97.72964
 [9,] -0.272532  1.503424    97.72964

The doubling of correct digits predicted by the quadratic-convergence result is visible in the final rows.

5.5.4 Comparison with nlm and glm

Code
logit_nlm <- nlm(neg_logp_logistic, c(0, 0),
                 x = data$x, y = data$y, hessian = TRUE)
logit_nlm$estimate
[1] -0.2725325  1.5034237
Code
sqrt(diag(solve(logit_nlm$hessian)))   # asymptotic standard errors
[1] 0.1797390 0.2089674
Code
summary(glm(y ~ x, family = binomial(), data = data))$coefficients
              Estimate Std. Error   z value     Pr(>|z|)
(Intercept) -0.2725322  0.1797371 -1.516283 1.294479e-01
x            1.5034244  0.2089473  7.195234 6.235374e-13

The three routes agree to printing accuracy. glm is doing the same arithmetic as mle_logistic_nr: for a GLM the Newton–Raphson step can be rearranged as a weighted least-squares fit of the working response \(u_i + (y_i - p_i)/\{p_i(1-p_i)\}\) on \(X\) with weights \(p_i(1-p_i)\), giving iteratively reweighted least squares (IRLS). This is not a different algorithm but a different arrangement of the same one, and it explains why glm reports “Fisher Scoring iterations” in its output.

5.6 Backpropagation Algorithm for Neural Networks

5.6.1 Example of Poisson Neural Network

Logistic regression was a one-unit network, and its Hessian was small enough to invert. Adding a single hidden layer breaks that comfort, and shows why the methods in this section exist.

Let each observation carry \(p = 3\) covariates \(x_i = (x_{i1}, x_{i2}, x_{i3})\) and a count response \(y_i\). Put \(J = 3\) hidden units \(h_{i1}, h_{i2}, h_{i3}\) between the covariates and the mean, and use a log link so that the mean stays positive: \[a_{ij} = \sum_{k=0}^{p} w_{jk}\, x_{ik}, \qquad h_{ij} = \tanh(a_{ij}), \qquad \eta_i = \sum_{j=0}^{J} v_j\, h_{ij}, \qquad \lambda_i = e^{\eta_i},\] where \(x_{i0} \equiv 1\) and \(h_{i0} \equiv 1\) are constant inputs, so that the intercept of each hidden unit is the weight \(w_{j0}\) and the intercept of the linear predictor is the weight \(v_0\), with \(y_i \mid x_i \sim \mathrm{Poisson}(\lambda_i)\). Dropping the \(\log y_i!\) term, which carries no parameters, the negative log-likelihood is \[\mathcal{L}(\theta) = \sum_{i=1}^{n} \mathcal{L}_i(\theta), \qquad \mathcal{L}_i(\theta) = \lambda_i - y_i \eta_i ,\] where \(\theta\) collects the \(J(p+1) = 12\) input weights \(w_{jk}\), \(k = 0, \ldots, p\), and the \(J + 1 = 4\) output weights \(v_j\), \(j = 0, \ldots, J\) — sixteen parameters in all.

The intercepts matter: without \(w_{j0}\) every hidden unit would pass through the origin, and without \(v_0\) the level of \(\lambda\) could not be shifted freely. Carrying them as ordinary weights on constant inputs means they need no separate symbol and no separate rule: \(w_{j0}\) and \(v_0\) are differentiated exactly like every other weight, and their local derivative happens to be the tail value \(1\).

Three features make this a natural home for gradient-only methods. The score equations have no closed-form solution. \(\mathcal{L}\) is not convex — permuting the hidden units leaves it unchanged, so every minimum is one of \(J!\) equivalent copies, and the Hessian is indefinite over much of the space, which is exactly where a raw Newton step misbehaves. And while a \(12 \times 12\) Hessian is still cheap here, its size grows as \((Jp + J)^2\): widen the layer or the covariate set and the \(O(p^3)\) solve becomes the bottleneck long before the gradient does.

5.6.2 What the model can represent

With a single covariate the model is easy to see. Each tanh unit switches on at a location set by its intercept \(w_{j0}\) and at a rate set by its weight \(w_{j1}\), and enters the linear predictor scaled by its output weight \(v_j\). It is those scaled units \(v_j h_j(x)\) that are worth plotting: they add up, with \(v_0\), to \(\eta(x)\), and exponentiating \(\eta\) gives a \(\lambda(x)\) with a hump — a shape a log-linear Poisson regression, whose \(\log\lambda\) is a straight line, cannot produce at all.

Code
set.seed(7)

w_true  <- c(3.0, 3.0, 1.0)     # input weight of each hidden unit
w0_true <- c(1.5, -1.5, 0.0)    # hidden intercepts: where each unit switches on
v_true  <- c(1.2, -1.2, 0.3)    # output weights
v0_true <- -0.3                 # output intercept

lambda_nn <- function(x, w, w0, v, v0)
  as.vector(exp(v0 + tanh(sweep(outer(x, w), 2, w0, "+")) %*% v))

xg <- seq(-3, 3, length.out = 400)
H  <- tanh(sweep(outer(xg, w_true), 2, w0_true, "+"))
VH <- sweep(H, 2, v_true, "*")             # the scaled units v_j h_j(x)
eg <- v0_true + rowSums(VH)                # eta(x), what they add up to

par(mfrow = c(1, 2), mar = c(4, 4.5, 2.5, 1))

matplot(xg, VH, type = "l", lty = 1, lwd = 2, col = c(1, 2, 4),
        ylim = range(VH, eg) + c(0, 1.3),   # headroom for the legend
        xlab = TeX(r"($x$)"), ylab = TeX(r"($v_j h_j(x)$)"),
        main = TeX(r"(the three scaled hidden units)"))
lines(xg, eg, lwd = 2.5, lty = 2, col = "grey30")
abline(h = 0, lty = 3)
legend("topleft", bty = "n", lty = c(1, 1, 1, 2), lwd = 2,
       col = c(1, 2, 4, "grey30"), cex = 0.8,
       legend = TeX(c(sprintf(r"($v_%d h_%d$: $w = %.1f$, $w_0 = %.1f$, $v = %.1f$)",
                              1:3, 1:3, w_true, w0_true, v_true),
                      r"($\eta(x) = v_0 + \sum_j v_j h_j(x)$)")))

## simulate counts from the curve
n  <- 150
xi <- runif(n, -3, 3)
yi <- rpois(n, lambda_nn(xi, w_true, w0_true, v_true, v0_true))

plot(xg, lambda_nn(xg, w_true, w0_true, v_true, v0_true), type = "l", lwd = 2,
     ylim = c(0, max(yi) + 0.5),
     xlab = TeX(r"($x$)"), ylab = TeX(r"($\lambda(x)$ and $y$)"),
     main = TeX(r"(mean curve and simulated counts)"))
points(xi, yi, pch = 19, col = adjustcolor(2, 0.45), cex = 0.7)
legend("topright", bty = "n", lwd = c(2, NA), pch = c(NA, 19), col = c(1, adjustcolor(2, 0.6)),
       legend = TeX(c(r"($\lambda(x)$)", r"($y_i \sim Poisson(\lambda(x_i))$)")))
Figure 5.20: Shows what a hidden layer buys in a Poisson regression, by displaying the three scaled hidden units and the non-monotone mean curve they combine into, together with counts simulated from it. A one-covariate Poisson network with three tanh units: the scaled activations \(v_j h_j(x)\) and the linear predictor \(\eta(x)\) they sum to (left), and the resulting mean \(\lambda(x) = e^{\eta(x)}\) with simulated counts (right), a shape no log-linear model could produce.

The counts scatter around the curve with variance equal to the mean, so they bunch near zero in the flat regions and spread out under the peak.

5.6.3 The gradient by backpropagation

The chain rule applies exactly as in the logistic case, only with one more layer to traverse. Working backwards from the loss for a single observation:

Loss to the linear predictor. In terms of \(\lambda_i\), the loss of one observation is \[\mathcal{L}_i = \lambda_i - y_i \log \lambda_i , \qquad \lambda_i = e^{\eta_i},\] which is \(\lambda_i - y_i \eta_i\) once \(\log \lambda_i = \eta_i\) is substituted. The two local derivatives at the output are \[\frac{\partial \mathcal{L}_i}{\partial \lambda_i} = 1 - \frac{y_i}{\lambda_i}, \qquad \frac{\partial \lambda_i}{\partial \eta_i} = \lambda_i ,\] whose product is again a bare prediction error, with the \(\lambda_i\) cancelling just as \(p_i(1-p_i)\) did for the sigmoid: \[\delta_i \;=\; \frac{\partial \mathcal{L}_i}{\partial \eta_i} = \left(1 - \frac{y_i}{\lambda_i}\right)\lambda_i = \lambda_i - y_i .\]

Output layer. Since \(\partial \eta_i / \partial v_j = h_{ij}\) and \(\partial \eta_i / \partial v_0 = h_{i0} = 1\), \[\frac{\partial \mathcal{L}_i}{\partial v_j} = \delta_i\, h_{ij}, \qquad \frac{\partial \mathcal{L}_i}{\partial v_0} = \delta_i .\]

Into the hidden layer. The path from \(a_{ij}\) to the loss runs through \(h_{ij}\) and then \(\eta_i\), contributing \(\partial \eta_i/\partial h_{ij} = v_j\) and \(\partial h_{ij}/\partial a_{ij} = 1 - h_{ij}^2\) (the tanh derivative). Taking them one at a time gives a sensitivity at each node on the way, \[\delta_{ij}^{h} \;=\; \frac{\partial \mathcal{L}_i}{\partial h_{ij}} = \delta_i\, v_j , \qquad \delta_{ij}^{a} \;=\; \frac{\partial \mathcal{L}_i}{\partial a_{ij}} = \delta_{ij}^{h}\left(1 - h_{ij}^{2}\right) .\]

Input layer. Finally \(\partial a_{ij}/\partial w_{jk} = x_{ik}\) for every \(k\), including \(\partial a_{ij}/\partial w_{j0} = x_{i0} = 1\), so \[\frac{\partial \mathcal{L}_i}{\partial w_{jk}} = \delta_{ij}^{a}\, x_{ik}, \qquad \frac{\partial \mathcal{L}_i}{\partial w_{j0}} = \delta_{ij}^{a} .\]

Each intercept derivative is the sensitivity at its own unit, unmultiplied. Splitting at \(a_{ij}\) is also what makes the computation efficient: \(\delta_{ij}^{a}\) is formed once and reused for all \(p\) weights entering unit \(j\).

5.6.4 Two kinds of arrow

Every arrow in the network is one of two kinds, and the distinction organises the whole computation.

Arrows that carry a weight, drawn blue below, are the ones forming a linear combination: \(x_{ik} \to a_{ij}\) carries \(w_{jk}\), \(h_{ij} \to \eta_i\) carries \(v_j\), and the edges from the constant nodes holding \(x_{i0} = h_{i0} = 1\) into \(a_{ij}\) and \(\eta_i\) carry the intercepts \(w_{j0}\) and \(v_0\). Every parameter of the model sits on one of these arrows, and these are the only derivatives we ultimately want.

Arrows that carry no weight are the fixed transformations: \(a_{ij} \to h_{ij}\) is tanh, \(\eta_i \to \lambda_i\) is \(\exp\), \(\lambda_i \to \mathcal{L}_i\) is the Poisson loss. Nothing is estimated on them, so they contribute no gradient component of their own; they only pass sensitivity along.

Backpropagation computes one number per node — the sensitivity of the loss to that node, \[\delta_z = \frac{\partial \mathcal{L}_i}{\partial z},\] obtained by multiplying the local derivatives along the arrows from the loss back to \(z\). Once \(\delta\) is known at a node, the edge rule of Section 5.5.1.1 makes the weights feeding it free:

The gradient of the weight on a blue arrow \(u \to z\) is \(\delta_z\) times the value at the arrow’s tail: . \[> \frac{\partial \mathcal{L}_i}{\partial \omega_{u \to z}} = \delta_z \cdot u . >.\]

The reason is immediate: \(z\) is a linear combination, so it depends on \(\omega\) only through the single term \(\omega\, u\), giving \(\partial z/\partial \omega = u\). For this network, with \(\delta_{\eta} = \lambda_i - y_i\), \(\delta_{h_j} = \delta_{\eta}\, v_j\) and \(\delta_{a_j} = \delta_{h_j}(1 - h_{ij}^2)\), \[\frac{\partial \mathcal{L}_i}{\partial v_j} = \delta_{\eta}\, h_{ij}, \qquad \frac{\partial \mathcal{L}_i}{\partial v_0} = \delta_{\eta} \cdot 1, \qquad \frac{\partial \mathcal{L}_i}{\partial w_{jk}} = \delta_{a_j}\, x_{ik}, \qquad \frac{\partial \mathcal{L}_i}{\partial w_{j0}} = \delta_{a_j} \cdot 1 .\]

The intercept gradients are the same expression with a tail value of \(1\), which is the whole content of “an intercept is a weight on a constant input”.

This also explains the one label that might look out of place. Along the blue arrow \(h_{ij} \to \eta_i\) the local derivative is \[\frac{\partial \eta_i}{\partial h_{ij}} = v_j ,\] and it plays no part in \(v_j\)’s own gradient — a weight’s gradient never involves that weight. Its job is to carry \(\delta\) from \(\eta_i\) back to \(h_{ij}\), so \(v_j\) appears in the gradient of every weight behind unit \(j\) and in none at or after it. In a deeper network this is the only kind of factor that accumulates: a path from the loss to an early weight is a long product of such node-to-node derivatives, closed off at the end by one tail value.

Summing over observations gives the full gradient \(g(\theta)\). This is backpropagation in miniature: one forward sweep stores \(h\), \(\eta\) and \(\lambda\); one backward sweep turns \(\delta_i\) into \(\delta_{ij}^{h}\), then \(\delta_{ij}^{a}\), reading off every partial derivative on the way. The cost is a small multiple of one function evaluation, no matter how many parameters there are — which is precisely why first-order methods scale where Newton does not.

The diagram below is the logistic picture with a layer inserted, and it is drawn twice: once for the forward pass and once for the backward pass, so that neither set of arrows has to share an edge with the other. The intercepts are drawn as in the logistic picture: each unit that has an intercept gets its own square constant node holding the value \(1\), directly above it, and the intercept — \(w_{j0}\) for hidden unit \(j\), \(v_0\) for \(\eta\) — is the weight on the edge from that square. Every parameter is then a \(w\) or a \(v\) sitting on an ordinary edge, the intercepts included.

The pre-activation \(a_j\) is drawn as a node of its own, so that every arrow joins two quantities that actually appear in the picture and the tanh step gets an edge to sit on. The backward panel traces one continuous path from the loss to a single weight, \(L \to \lambda \to \eta \to h_j \to a_j \to w_{jk}\), in bold. Multiplying the four local derivatives along it gives that weight’s partial derivative; every other weight is reached by a path of the same shape, drawn faint. Only the highlighted edges are labelled — the rest carry the same formulas with different indices.

Each arrow in the backward panel carries only its local derivative. The sensitivity \(\delta\) of each node is written directly beneath that node (including \(\delta_{x_k}\) at the input layer, which is well defined but never needed, since no parameter hangs off an input). A constant node, by contrast, has no \(\delta\) at all, because the value \(1\) it holds does not depend on the parameters, so its edge carries the intercept but no arrow. The parameter gradients — each one a \(\delta\) at an arrow’s head times the value at its tail — are collected in the boxes beneath the fan of arrows whose weights they belong to.

Figure 5.21: This figure shows the way that backpropagation assembles the gradient of a two-layer Poisson network, by labelling each edge with its local derivative. The network is oriented left-to-right, with the nodes of each layer stacked vertically: the forward pass (top) carries values rightward, and the backward pass (bottom) carries local derivatives leftward. Each arrow carries only the local derivative of its head with respect to its tail; the sensitivity \(\delta\) of each node sits directly beneath that node, and the parameter gradients — obtained by multiplying the \(\delta\) at the arrow’s head by the value at its tail — are collected in the boxes beneath the corresponding fan of weight arrows. Each intercept is the weight on an edge from a square constant node holding the value \(1\); a constant node has no \(\delta\), so in the backward pass its edge carries no arrow.

R function for Computing and Illustrating the Gradient of Single-layer Neural Networks

Code
library(gt)
J <- 3                                      # number of hidden units

## math column header for gt tables
m <- function(tex) html(paste0("\\(", tex, "\\)"))

## theta = (W[J x p, by column], w0, v, v0); X holds x_1..x_p (no constant column)
## workingtable = TRUE also returns three gt tables (first nshow observations):
##   forward values of the nodes, backward deltas, and the gradient as inner products
nn_pois <- function(theta, X, y, workingtable = FALSE, nshow = 6) {
  p <- ncol(X)

  ## ---- extract weights from theta ----
  W  <- matrix(theta[1:(J * p)], J, p)      # input weights w_jk, k = 1..p
  w0 <- theta[J * p + 1:J]                  # hidden intercepts w_j0
  v  <- theta[J * p + J + 1:J]              # output weights v_j
  v0 <- theta[J * p + 2 * J + 1]            # output intercept v_0

  ## ---- forward pass: store a, h, eta, lambda (needed by the backward pass) ----
  A   <- sweep(X %*% t(W), 2, w0, "+")      # n x J  a_ij = w_j0 + sum_k w_jk x_ik
  H   <- tanh(A)                            # n x J  h_ij = tanh(a_ij)
  eta <- as.vector(v0 + H %*% v)            # n      eta_i = v_0 + sum_j v_j h_ij
  lam <- exp(eta)                           # n      lambda_i = exp(eta_i)

  ## ---- loss: Poisson negative log-likelihood ----
  nll <- sum(lam - y * eta)                 # dropping log(y!)

  ## ---- backward pass: sensitivities delta, from the loss back to a ----
  delta_eta <- lam - y                      # n      dL_i/d eta_i  (seed)
  delta_h   <- matrix(0, nrow(X), J)        # n x J  dL_i/d h_ij = delta_eta_i * v_j
  for (j in 1:J) delta_h[, j] <- delta_eta * v[j]
  delta_a   <- delta_h * (1 - H^2)          # n x J  dL_i/d a_ij = delta_h * tanh'(a_ij)

  ## ---- gradients of the weights: delta at the arrow's head x value at its tail,
  ##      summed over observations (the intercepts have tail value 1) ----
  d_v  <- as.vector(t(H) %*% delta_eta)     # J      dL/dv_j  = sum_i delta_eta_i h_ij
  d_v0 <- sum(delta_eta)                    # 1      dL/dv_0
  d_W  <- t(delta_a) %*% X                  # J x p  dL/dw_jk = sum_i delta_a_ij x_ik
  d_w0 <- colSums(delta_a)                  # J      dL/dw_j0

  ## ---- pack the gradient in the same order as theta, with names ----
  grad <- c(as.vector(d_W), d_w0, d_v, d_v0)
  names(grad) <- c(paste0("w", rep(1:J, p), rep(1:p, each = J)),   # w11, w21, ..., w3p
                   paste0("w", 1:J, "0"), paste0("v", 1:J), "v0")
  out <- list(nll = nll, grad = grad)
  if (!workingtable) return(out)

  ## ---- working tables: one row per observation i ----
  wk <- data.frame(i = seq_along(y), y = y, X, A, H, eta = eta, lam = lam,
                   delta_eta = delta_eta, delta_h, delta_a)
  names(wk) <- c("i", "y", paste0("x", 1:p), paste0("a", 1:J), paste0("h", 1:J),
                 "eta", "lam", "delta_eta", paste0("delta_h", 1:J), paste0("delta_a", 1:J))
  top <- head(wk, nshow)

  ## forward values
  forward <- top |>
    gt() |>
    cols_hide(starts_with("delta")) |>
    cols_label(i = m("i"), y = m("y_i"),
               x1 = m("x_i^{(1)}"), x2 = m("x_i^{(2)}"), x3 = m("x_i^{(3)}"),
               a1 = m("a_i^{(1)}"), a2 = m("a_i^{(2)}"), a3 = m("a_i^{(3)}"),
               h1 = m("h_i^{(1)}"), h2 = m("h_i^{(2)}"), h3 = m("h_i^{(3)}"),
               eta = m("\\eta_i"), lam = m("\\lambda_i")) |>
    tab_spanner(m("\\text{covariates}"), c(x1, x2, x3)) |>
    tab_spanner(m("a_i^{(j)} = w_{j0} + \\sum_k w_{jk} x_i^{(k)}"), c(a1, a2, a3)) |>
    tab_spanner(m("h_i^{(j)} = \\tanh(a_i^{(j)})"), c(h1, h2, h3)) |>
    tab_spanner(m("\\eta_i = v_0 + \\sum_j v_j h_i^{(j)}, \\; \\lambda_i = e^{\\eta_i}"), c(eta, lam)) |>
    fmt_number(-c(i, y), decimals = 3) |>
    tab_header(title = "Forward pass: values of the nodes")

  ## backward deltas
  backward <- top |>
    gt() |>
    cols_hide(c(y, starts_with("x"), starts_with("a"), starts_with("h"), eta, lam)) |>
    cols_label(i = m("i"), delta_eta = m("\\delta_{\\eta_i} = \\lambda_i - y_i"),
               delta_h1 = m("\\delta_{h_i^{(1)}}"), delta_h2 = m("\\delta_{h_i^{(2)}}"),
               delta_h3 = m("\\delta_{h_i^{(3)}}"),
               delta_a1 = m("\\delta_{a_i^{(1)}}"), delta_a2 = m("\\delta_{a_i^{(2)}}"),
               delta_a3 = m("\\delta_{a_i^{(3)}}")) |>
    tab_spanner(m("\\delta_{h_i^{(j)}} = \\delta_{\\eta_i}\\, v_j"), starts_with("delta_h")) |>
    tab_spanner(m("\\delta_{a_i^{(j)}} = \\delta_{h_i^{(j)}} \\bigl(1 - (h_i^{(j)})^2\\bigr)"), starts_with("delta_a")) |>
    fmt_number(-i, decimals = 3) |>
    tab_header(title = "Backward pass: sensitivities")

  ## gradient: inner product of two columns of the full working table (all n rows)
  ip <- function(u, w) sum(wk[[u]] * w)
  tex <- val <- character(0)
  for (k in 1:p) for (j in 1:J) {
    tex <- c(tex, sprintf("\\sum_i \\delta_{a_i^{(%d)}}\\, x_i^{(%d)}", j, k))
    val <- c(val, ip(paste0("delta_a", j), wk[[paste0("x", k)]]))
  }
  for (j in 1:J) {
    tex <- c(tex, sprintf("\\sum_i \\delta_{a_i^{(%d)}}\\cdot 1", j))
    val <- c(val, ip(paste0("delta_a", j), 1))
  }
  for (j in 1:J) {
    tex <- c(tex, sprintf("\\sum_i \\delta_{\\eta_i}\\, h_i^{(%d)}", j))
    val <- c(val, ip("delta_eta", wk[[paste0("h", j)]]))
  }
  tex <- c(tex, "\\sum_i \\delta_{\\eta_i}\\cdot 1"); val <- c(val, ip("delta_eta", 1))

  par_tex <- c(sprintf("w_{%d%d}", rep(1:J, p), rep(1:p, each = J)),
               sprintf("w_{%d0}", 1:J), sprintf("v_{%d}", 1:J), "v_0")
  gradient <- data.frame(par = paste0("\\(", par_tex, "\\)"),
                         inner = paste0("\\(", tex, "\\)"),
                         value = as.numeric(val), back = unname(grad)) |>
    gt() |>
    fmt_passthrough(c(par, inner), escape = FALSE) |>
    cols_label(par = m("\\text{weight}"),
               inner = m("\\text{inner product of two columns}"),
               value = m("\\text{value}"), back = m("\\text{gradient}")) |>
    fmt_number(c(value, back), decimals = 4) |>
    tab_header(title = "Gradient: inner products over all observations")

  c(out, list(tables = list(forward = forward, backward = backward, gradient = gradient)))
}

set.seed(3)
n <- 40; p <- 3
X <- matrix(rnorm(n * p), n, p)
theta0 <- rnorm(J * p + 2 * J + 1, sd = 0.5)
## counts simulated from the network at theta0
ytrue <- rpois(n, local({
  W <- matrix(theta0[1:(J * p)], J, p); w0 <- theta0[J * p + 1:J]
  v <- theta0[J * p + J + 1:J];        v0 <- theta0[J * p + 2 * J + 1]
  exp(v0 + tanh(sweep(X %*% t(W), 2, w0, "+")) %*% v)
}))

fit  <- nn_pois(theta0, X, ytrue, workingtable = TRUE)
back <- fit$grad                           # named gradient

## central differences, one coordinate at a time
h <- 1e-6
numeric_grad <- vapply(seq_along(theta0), function(k) {
  e <- numeric(length(theta0)); e[k] <- h
  (nn_pois(theta0 + e, X, ytrue)$nll - nn_pois(theta0 - e, X, ytrue)$nll) / (2 * h)
}, numeric(1))

Check the Gradient Ouput

Code
cbind(backprop = back, numeric = numeric_grad)
         backprop       numeric
w11  0.4737615235  0.4737615296
w21 -1.0509837992 -1.0509837960
w31  0.0001707534  0.0001707647
w12 -1.3588962590 -1.3588962613
w22  4.1853262037  4.1853262047
w32 -0.0881340975 -0.0881340956
w13  0.3705859680  0.3705859690
w23  0.8368418239  0.8368418190
w33  0.0974066121  0.0974066090
w10 -0.9574699509 -0.9574699522
w20  1.3058687313  1.3058687340
w30 -0.0693719994 -0.0693719926
v1  -0.6236882995 -0.6236883063
v2  -1.8296636752 -1.8296636757
v3   0.8358353706  0.8358353725
v0  -1.3255493620 -1.3255493627
Code
max(abs(back - numeric_grad))              # agreement across all 16 coordinates
[1] 1.130205e-08

The working tables. Calling the function with workingtable = TRUE also returns three tables (the first six of the \(n = 40\) observations are shown). The forward pass fills in the first, from the covariates to \(\lambda_i\).

Code
fit$tables$forward
Forward pass: values of the nodes
\(i\) \(y_i\)
\(\text{covariates}\)
\(a_i^{(j)} = w_{j0} + \sum_k w_{jk} x_i^{(k)}\)
\(h_i^{(j)} = \tanh(a_i^{(j)})\)
\(\eta_i = v_0 + \sum_j v_j h_i^{(j)}, \; \lambda_i = e^{\eta_i}\)
\(x_i^{(1)}\) \(x_i^{(2)}\) \(x_i^{(3)}\) \(a_i^{(1)}\) \(a_i^{(2)}\) \(a_i^{(3)}\) \(h_i^{(1)}\) \(h_i^{(2)}\) \(h_i^{(3)}\) \(\eta_i\) \(\lambda_i\)
1 2 −0.962 0.787 1.736 1.442 −0.141 −1.973 0.894 −0.140 −0.962 −0.482 0.618
2 1 −0.293 −0.310 −0.352 0.804 0.305 0.418 0.666 0.296 0.395 −0.921 0.398
3 0 0.259 1.699 0.689 0.106 0.739 −1.103 0.106 0.629 −0.802 −1.687 0.185
4 0 −1.152 −0.795 1.224 1.868 −0.540 −0.979 0.953 −0.493 −0.753 −0.136 0.873
5 0 0.196 0.348 0.794 0.501 0.237 −0.695 0.463 0.232 −0.601 −1.085 0.338
6 0 0.030 −2.265 −0.006 1.103 −0.410 0.949 0.802 −0.388 0.739 −0.239 0.787

The backward pass fills in the second, from \(\delta_{\eta_i}\) back to \(\delta_{a_i^{(j)}}\).

Code
fit$tables$backward
Backward pass: sensitivities
\(i\) \(\delta_{\eta_i} = \lambda_i - y_i\)
\(\delta_{h_i^{(j)}} = \delta_{\eta_i}\, v_j\)
\(\delta_{a_i^{(j)}} = \delta_{h_i^{(j)}} \bigl(1 - (h_i^{(j)})^2\bigr)\)
\(\delta_{h_i^{(1)}}\) \(\delta_{h_i^{(2)}}\) \(\delta_{h_i^{(3)}}\) \(\delta_{a_i^{(1)}}\) \(\delta_{a_i^{(2)}}\) \(\delta_{a_i^{(3)}}\)
1 −1.382 −1.032 1.128 −0.088 −0.207 1.106 −0.007
2 −0.602 −0.449 0.491 −0.038 −0.250 0.448 −0.032
3 0.185 0.138 −0.151 0.012 0.137 −0.091 0.004
4 0.873 0.651 −0.712 0.056 0.059 −0.539 0.024
5 0.338 0.252 −0.276 0.022 0.198 −0.261 0.014
6 0.787 0.588 −0.642 0.050 0.210 −0.545 0.023

Each component of the gradient is then the inner product of two columns of the working table over all \(n\) observations — a \(\delta\) column against the column at the tail of the weight’s arrow, or against a column of ones for an intercept. The last column is the named gradient returned by the function.

Code
fit$tables$gradient
Gradient: inner products over all observations
\(\text{weight}\) \(\text{inner product of two columns}\) \(\text{value}\) \(\text{gradient}\)
\(w_{11}\) \(\sum_i \delta_{a_i^{(1)}}\, x_i^{(1)}\) 0.4738 0.4738
\(w_{21}\) \(\sum_i \delta_{a_i^{(2)}}\, x_i^{(1)}\) −1.0510 −1.0510
\(w_{31}\) \(\sum_i \delta_{a_i^{(3)}}\, x_i^{(1)}\) 0.0002 0.0002
\(w_{12}\) \(\sum_i \delta_{a_i^{(1)}}\, x_i^{(2)}\) −1.3589 −1.3589
\(w_{22}\) \(\sum_i \delta_{a_i^{(2)}}\, x_i^{(2)}\) 4.1853 4.1853
\(w_{32}\) \(\sum_i \delta_{a_i^{(3)}}\, x_i^{(2)}\) −0.0881 −0.0881
\(w_{13}\) \(\sum_i \delta_{a_i^{(1)}}\, x_i^{(3)}\) 0.3706 0.3706
\(w_{23}\) \(\sum_i \delta_{a_i^{(2)}}\, x_i^{(3)}\) 0.8368 0.8368
\(w_{33}\) \(\sum_i \delta_{a_i^{(3)}}\, x_i^{(3)}\) 0.0974 0.0974
\(w_{10}\) \(\sum_i \delta_{a_i^{(1)}}\cdot 1\) −0.9575 −0.9575
\(w_{20}\) \(\sum_i \delta_{a_i^{(2)}}\cdot 1\) 1.3059 1.3059
\(w_{30}\) \(\sum_i \delta_{a_i^{(3)}}\cdot 1\) −0.0694 −0.0694
\(v_{1}\) \(\sum_i \delta_{\eta_i}\, h_i^{(1)}\) −0.6237 −0.6237
\(v_{2}\) \(\sum_i \delta_{\eta_i}\, h_i^{(2)}\) −1.8297 −1.8297
\(v_{3}\) \(\sum_i \delta_{\eta_i}\, h_i^{(3)}\) 0.8358 0.8358
\(v_0\) \(\sum_i \delta_{\eta_i}\cdot 1\) −1.3255 −1.3255

The working graph. The same numbers can be written on the computational graph itself, which is how backpropagation is usually done by hand. In Figure 5.22 each node holds its \(\delta\) for the first three observations (the input nodes hold the covariates, the tail values that the input weights are multiplied by), and each edge shows its weight at \(\theta_0\) together with the gradient of that weight in green. As in Figure 5.21, the intercepts are the weights on the edges from the square constant nodes holding \(1\). The gradients are inner products over all \(n = 40\) observations, so they are the values in the last column of the table above, not sums of the three numbers shown.

Code
local({
  ## forward and backward pass at theta0, as in nn_pois
  W  <- matrix(theta0[1:(J * p)], J, p); w0 <- theta0[J * p + 1:J]
  v  <- theta0[J * p + J + 1:J];         v0 <- theta0[J * p + 2 * J + 1]
  H     <- tanh(sweep(X %*% t(W), 2, w0, "+"))
  lam   <- as.vector(exp(v0 + H %*% v))
  d_lam <- 1 - ytrue / lam                  # delta at lambda
  d_eta <- lam - ytrue                      # delta at eta
  d_h   <- outer(d_eta, v)                  # delta at h_j
  d_a   <- d_h * (1 - H^2)                  # delta at a_j

  ## layout: one column per layer, units stacked top to bottom
  y3     <- c(10, 5.4, 0.8)
  x_xy   <- cbind(1, y3);  a_xy <- cbind(8, y3);  h_xy <- cbind(11.2, y3)
  eta_xy <- c(14.6, 5.4);  lam_xy <- c(17.2, 5.4);  L_xy <- c(19.8, 5.4)
  r  <- 0.9                                 # node radius
  sq <- 0.3                                 # half-width of a constant node
  up <- 2.4                                 # height of a constant node above its unit

  op <- par(mar = c(0, 0, 0, 0)); on.exit(par(op))
  plot(NA, xlim = c(0, 20.8), ylim = c(-0.3, 13.8), asp = 1,
       axes = FALSE, xlab = "", ylab = "")

  ## an edge label: the weight, and its gradient in green, on a white box
  edge_label <- function(xy, lab, g, col) {
    rect(xy[1] - 0.8, xy[2] - 0.4, xy[1] + 0.8, xy[2] + 0.4,
         col = "white", border = "grey70")
    text(xy[1], xy[2] + 0.18, parse(text = lab), col = col, cex = 0.7)
    text(xy[1], xy[2] - 0.2, sprintf("%.4f", g), col = "darkgreen", font = 2, cex = 0.7)
  }
  ## a node: its label and the first three of its values
  node <- function(xy, lab, vals, bg) {
    symbols(xy[1], xy[2], circles = r, inches = FALSE, add = TRUE, bg = bg)
    text(xy[1], xy[2] + 0.55, parse(text = lab), cex = 0.95)
    text(xy[1], xy[2] - 0.25, paste(c(sprintf("%.3f", vals[1:3]), "..."), collapse = "\n"),
         cex = 0.6)
  }
  wlab <- function(nm, w) sprintf("%s == %s", nm, sprintf("%.3f", w))

  ## edges
  for (k in 1:p) for (j in 1:J) segments(x_xy[k, 1], x_xy[k, 2], a_xy[j, 1], a_xy[j, 2])
  for (j in 1:J) {
    segments(a_xy[j, 1], a_xy[j, 2], h_xy[j, 1], h_xy[j, 2])
    segments(h_xy[j, 1], h_xy[j, 2], eta_xy[1], eta_xy[2])
    segments(a_xy[j, 1], a_xy[j, 2] + up, a_xy[j, 1], a_xy[j, 2])
  }
  segments(eta_xy[1], eta_xy[2] + up, eta_xy[1], eta_xy[2])
  segments(eta_xy[1], eta_xy[2], L_xy[1], L_xy[2])

  ## edge labels; each w_jk sits near the a_j end of its edge
  for (k in 1:p) for (j in 1:J)
    edge_label(x_xy[k, ] + 0.74 * (a_xy[j, ] - x_xy[k, ]),
               wlab(sprintf("w[%d%d]", j, k), W[j, k]),
               back[sprintf("w%d%d", j, k)], "steelblue4")
  bias_mid <- (r + up - sq) / 2
  for (j in 1:J) {
    edge_label(a_xy[j, ] + c(0, bias_mid), wlab(sprintf("w[%d*0]", j), w0[j]),
               back[sprintf("w%d0", j)], "steelblue4")
    edge_label((h_xy[j, ] + eta_xy) / 2, wlab(sprintf("v[%d]", j), v[j]),
               back[sprintf("v%d", j)], "firebrick4")
    text(mean(c(a_xy[j, 1], h_xy[j, 1])), a_xy[j, 2] + 0.3, "tanh", cex = 0.75)
  }
  edge_label(eta_xy + c(0, bias_mid), wlab("v[0]", v0), back["v0"], "firebrick4")
  text(mean(c(eta_xy[1], lam_xy[1])), eta_xy[2] + 0.3, "exp", cex = 0.75)
  text(mean(c(lam_xy[1], L_xy[1])), eta_xy[2] + 0.3, "Poisson", cex = 0.75)

  ## constant nodes
  const_xy <- rbind(a_xy, eta_xy) + cbind(0, rep(up, J + 1))
  rect(const_xy[, 1] - sq, const_xy[, 2] - sq, const_xy[, 1] + sq, const_xy[, 2] + sq,
       col = "lightblue1")
  text(const_xy[, 1], const_xy[, 2], "1", cex = 0.85)

  ## nodes: covariates at the inputs, deltas elsewhere
  for (k in 1:p) node(x_xy[k, ], sprintf("x[i]^{(%d)}", k), X[, k], "grey90")
  for (j in 1:J) {
    node(a_xy[j, ], sprintf("delta[a[i]^{(%d)}]", j), d_a[, j], "honeydew")
    node(h_xy[j, ], sprintf("delta[h[i]^{(%d)}]", j), d_h[, j], "lightyellow")
  }
  node(eta_xy, "delta[eta[i]]", d_eta, "grey90")
  node(lam_xy, "delta[lambda[i]]", d_lam, "grey90")
  symbols(L_xy[1], L_xy[2], circles = 0.7, inches = FALSE, add = TRUE, bg = "white")
  text(L_xy[1], L_xy[2], expression(L[i]))

  text(c(1, 9.6, 14.6, 17.2, 19.8), 13.6,
       c("Input", "Hidden (tanh)", "Output", "Mean", "Loss"), font = 2)
})
Figure 5.22: Shows the backward pass of the Poisson network on actual numbers, so that each gradient can be traced to the \(\delta\) at the head of its edge and the value at its tail. The computational graph at \(\theta_0\): each node shows its sensitivity \(\delta\) for observations \(i = 1, 2, 3\) of the \(n = 40\) (the input nodes show the covariates), and each edge shows its weight and, in green, the gradient of the negative log-likelihood with respect to that weight, summed over all \(40\) observations. The squares are constant nodes holding \(1\), whose edges carry the intercepts.

5.6.5 A General Description of Gradient of Neural Networks

Neither rule of Section 5.5.1.1 mentions depth, so the same pair describes a network of any size. Write a feed-forward network of \(L\) layers as \[h^{(0)} = x, \qquad a^{(l)} = W^{(l)} h^{(l-1)}, \qquad h^{(l)} = f\!\left(a^{(l)}\right), \qquad l = 1, \ldots, L ,\] with a constant entry folded into each \(h^{(l-1)}\) so that the intercepts are ordinary columns of \(W^{(l)}\), exactly as the constant nodes holding \(1\) supply the intercepts in Figure 5.21.

The backward pass has the same shape as the forward pass. Applying the node rule to a whole layer at once turns the sum over consumers into a matrix–vector product with the transposed weight matrix: \[\delta_{h^{(l-1)}} = W^{(l)\top} \delta_{a^{(l)}}, \qquad \delta_{a^{(l)}} = \delta_{h^{(l)}} \odot f'\!\left(a^{(l)}\right) ,\] where \(\odot\) is the elementwise product. Set the two sweeps side by side and they are the same two operations:

forward backward
combine \(a^{(l)} = W^{(l)} h^{(l-1)}\) \(\delta_{h^{(l-1)}} = W^{(l)\top} \delta_{a^{(l)}}\)
transform \(h^{(l)} = f\!\left(a^{(l)}\right)\) \(\delta_{a^{(l)}} = \delta_{h^{(l)}} \odot f'\!\left(a^{(l)}\right)\)

A layer of the backward pass takes a linear combination of the quantities in the layer just processed and then rescales each entry — the same two steps the forward pass performs on values. Three things differ. The matrix is transposed, so the linear combination runs across the arrows entering a unit rather than those leaving it. The elementwise step multiplies by \(f'(a^{(l)})\) instead of applying \(f\), which makes the backward pass linear in \(\delta\) once the activations are fixed. And the sweep runs from \(l = L\) down to \(l = 1\).

Starting the recursion. The sweep needs one number to begin with, the sensitivity at the output. For the Poisson network of this section that is \[\delta_{\eta_i} = \frac{\partial \mathcal{L}_i}{\partial \eta_i} = \lambda_i - y_i ,\] and for the logistic model of Section 5.5.1.1 it is \(p_i - y_i\). In both cases the derivative of the output activation cancels against the derivative of the loss, which is why the seed is a bare prediction error.

Reading off the gradients. Once \(\delta_{a^{(l)}}\) is known, the edge rule delivers the whole weight matrix at once as an outer product, \[\frac{\partial \mathcal{L}_i}{\partial W^{(l)}} = \delta_{a^{(l)}} \, h^{(l-1)\top} ,\] one entry \(\delta_{a^{(l)}_j} h^{(l-1)}_k\) per weight \(w^{(l)}_{jk}\): the sensitivity at the arrow’s head times the value at its tail. The label \(\delta_{x_k} = \sum_j \delta_{a_j} w_{jk}\) in Figure 5.21 is the node rule at \(l = 1\). It is marked unneeded there because no parameter hangs off an input, but in a deeper network it is precisely the \(\delta\) handed to the layer below.

The complete gradient for one observation is therefore four steps: sweep forward storing \(a^{(l)}\) and \(h^{(l)}\); seed \(\delta\) at the output; sweep backward through the pair of recursions above; and form one outer product per layer along the way. Summing over observations gives \(g(\theta)\).

Cost. The two sweeps consist of the same \(L\) matrix–vector products, one per layer, with the same matrices — \(W^{(l)}\) going up and \(W^{(l)\top}\) coming down. A gradient therefore costs a small constant multiple of one evaluation of \(\mathcal{L}_i\), about \(O(L W^{2})\) for width \(W\), or one multiply–add per parameter per sweep, independent of how many parameters there are. This is worth contrasting with the form in which the chain rule is usually written. Expanding \(\partial \mathcal{L}_i / \partial w\) as a sum over paths from the loss to \(w\) produces about \(W^{L}\) terms, exponential in depth. The recursion computes the same quantity by visiting each edge once: a node’s \(\delta\) is formed once and reused by every node that feeds it, so shared sub-paths are never recomputed. In the language of algorithms the graph is a DAG and the backward sweep is dynamic programming over it in reverse topological order, which stands to path enumeration as a memoized recursion stands to a naive one.

Memory. The backward sweep consumes what the forward sweep produced — \(h^{(l-1)}\) for the outer product, \(f'(a^{(l)})\) for the elementwise step — so the activations have to be stored, at a cost proportional to the total number of nodes. That is why the diagrams above have a forward panel at all, and it is the cost that gradient checkpointing trades back for recomputation in very deep models.

Two extensions. Weight sharing needs no new machinery: when one parameter sits on many edges, as in a convolutional or recurrent network, its gradient is the sum of the edge rule over all of them, which is the node rule again. And the direction of the sweep is a choice. Propagating \(\partial z / \partial \theta_k\) forward is equally correct but costs one sweep per parameter, whereas sweeping backward costs one sweep per output and there is a single scalar output here. That asymmetry is what makes a full gradient affordable for models with millions of parameters, and it is the property the rest of this chapter relies on.

Backpropagation is easy to get subtly wrong, so it is worth testing against finite differences before handing the gradient to an optimizer — the same comparison made for the univariate case earlier in this chapter.

The two agree to the accuracy of the difference quotient. With a gradient this cheap and this reliable, BFGS or conjugate gradient can fit the model without ever forming a Hessian — which is what the rest of this section is about.

5.7 Gradient-Based Methods

Newton–Raphson is the method of choice when the second derivatives are available and \(p\) is modest. Outside that regime, three considerations push toward alternatives: derivatives may be unavailable or painful to code; the \(O(p^3)\) cost of solving the Newton system and the \(O(p^2)\) storage of \(J\) become prohibitive for large \(p\); and the objective may be non-smooth, noisy or multimodal.

As everywhere in this chapter, the objective is the negative log-likelihood \(\mathcal{L}(\theta) = -\ell(\theta)\), whose minimum is exactly the maximum of \(\ell\). This is also the convention that R’s optim(), nlm(), and nlminb() use internally, so the code below matches what those functions do under the hood. The methods here apply to any smooth objective, not only a likelihood, so this section writes \(f\) for the function being minimized and keeps \(g = \nabla f\) for its gradient — the same \(g\) used everywhere else in the chapter, where \(f = \mathcal{L}\) and \(g = \mathcal{L}'(\theta)\) in the univariate case.

5.7.1 Classification by derivative information

Methods are most usefully organised by what they require of the objective.

Table 5.3: Organizes the optimization methods of this chapter by how much derivative information each one requires. Optimization methods classified by the derivative information required
Order Requires Methods Typical use
0 Function values only Nelder–Mead, simulated annealing, genetic algorithms Non-smooth, discontinuous, or awkward-to-differentiate objectives; small \(p\)
1 Gradient Steepest descent, conjugate gradient, quasi-Newton (BFGS, L-BFGS), BHHH, stochastic gradient descent (with momentum, Adam) The workhorse class; large \(p\); gradients cheap by automatic differentiation
2 Gradient and Hessian Newton–Raphson, Fisher scoring, IRLS, trust-region Newton Statistical models of modest dimension where the Hessian is also wanted for inference

5.7.3 Why Gradient Descent Zig-Zags

With an exact line search, consecutive gradients are orthogonal — \(g_{k+1}'g_k = 0\), since the search stops exactly where the directional derivative along \(-g_k\) vanishes. On the elongated contours of the quadratic from Figure 5.17, that orthogonality forces the path to zig-zag back and forth across the valley instead of running down its length:

Figure 5.23: Illustrates the main weakness of gradient descent: on an elongated objective it makes slow zig-zag progress. Gradient descent with an exact line search zig-zags across an elongated valley: each step is orthogonal to the next.

The number of iterations needed grows with the condition number \(\lambda_{\max}/\lambda_{\min}\) of the Hessian — precisely the elongation of the ellipses in the earlier figure.

5.7.4 Conjugate Gradient

The zig-zag is fixed by choosing each new direction to be conjugate to the previous ones with respect to the Hessian \(Q\): \(d_i'Qd_j = 0\) for \(i \ne j\). For a quadratic \(f(\theta) = \tfrac12\theta'Q\theta - b'\theta\), minimizing along \(p\) such directions in succession reaches the exact minimum in at most \(p\) steps, without ever forming or storing \(Q\) — storage is \(O(p)\), its defining advantage over Newton.

Here a separate symbol is warranted: the search direction \(d_k\) is no longer the negative gradient, but a correction of it. The nonlinear version builds these directions from gradients only: \[ d_0 = -g_0, \qquad d_{k+1} = -g_{k+1} + \beta_k d_k, \] \[ \beta_k^{\mathrm{FR}} = \frac{g_{k+1}'g_{k+1}}{g_k'g_k}\ \text{(Fletcher–Reeves)}, \qquad \beta_k^{\mathrm{PR}} = \frac{g_{k+1}'(g_{k+1} - g_k)}{g_k'g_k}\ \text{(Polak–Ribière)}, \] with \(\theta_{k+1} = \theta_k + \alpha_k d_k\) from a line search and \(g_k = g(\theta_k)\) as before. Fletcher–Reeves and Polak–Ribière differ only in this correction coefficient. The method is sensitive to the accuracy of the line search, and conjugacy degrades on non-quadratic \(f\), so it is usually restarted with \(d = -g\) every \(p\) iterations.

Code
conj_grad <- function(f, g, x0, maxit = 1000, tol = 1e-8, restart = length(x0)) {
  g0 <- g(x0); d <- -g0; path <- x0            # d: a conjugate direction, not -g
  for (k in 1:maxit) {
    a <- optimize(function(a) f(x0 + a * d), c(0, 10))$minimum
    x1 <- x0 + a * d; g1 <- g(x1); path <- rbind(path, x1)
    if (sqrt(sum(g1^2)) < tol) break
    beta <- if (k %% restart == 0) 0 else max(0, sum(g1 * (g1 - g0)) / sum(g0^2))   # Polak-Ribiere+
    d <- -g1 + beta * d; x0 <- x1; g0 <- g1 }
  list(par = x1, iter = k, path = path)
}
cg <- conj_grad(fq, gq, c(2, -1.8))
par(pty = "s", mar = c(4, 4, 2, 1))
contour(s, s, -z, nlevels = 15, main = sprintf("conjugate gradient, %d iterations", cg$iter))
lines(gd$path, type = "b", col = 2, pch = 19, cex = 0.6); lines(cg$path, type = "b", col = 4, pch = 19, lwd = 2)
legend("topleft", c("gradient descent", "conjugate gradient"), col = c(2, 4), lwd = 2, bty = "n")
Figure 5.24: Shows how conjugate gradient repairs the zig-zag behaviour of gradient descent by choosing search directions that do not undo earlier progress. Conjugate gradient (blue) reaches the minimum of the same elongated quadratic in far fewer iterations than gradient descent (red).

5.7.5 Quasi-Newton: BFGS

BFGS sits between the gradient methods above and Newton’s method: it builds an approximation \(B_k \approx J(\theta_k)\) from successive gradient differences \(s_k = \theta_{k+1} - \theta_k\), \(y_k = g_{k+1} - g_k\), via a rank-two update that keeps \(B_k\) positive definite by construction (which is exactly why the Wolfe curvature condition of an earlier section matters: it is what guarantees \(s_k'y_k > 0\), and hence a well-defined update). Steps are \(\theta_{k+1} = \theta_k - \alpha_k B_k^{-1} g_k\) with a line search, giving superlinear convergence from only first derivatives — again a rescaling of \(g_k\) rather than \(g_k\) itself. BFGS itself stores a full \(p \times p\) approximation; L-BFGS keeps only the last few update vectors, reducing storage to \(O(mp)\) and making it the default choice for high-dimensional smooth problems. For most likelihood problems where the Hessian is awkward to derive, BFGS with an analytic gradient is the best practical compromise.

5.7.5.1 Example: Nelder–Mead, CG and BFGS on the Logistic Regression

Returning to the logistic-regression example, optim() can fit it with no Hessian at all — Nelder–Mead needs only the log-likelihood, CG and BFGS also take the analytic gradient already coded for Newton–Raphson:

negll_fn  <- function(b) neg_logp_logistic(b, data$x, data$y)
neggr_fn  <- function(b) nll_grad_info(b, data$y, data$x)$grad

fits <- list(NM   = optim(c(0, 0), negll_fn, method = "Nelder-Mead"),
             CG   = optim(c(0, 0), negll_fn, neggr_fn, method = "CG"),
             BFGS = optim(c(0, 0), negll_fn, neggr_fn, method = "BFGS", hessian = TRUE))
t(sapply(fits, function(f) c(f$par, f_evals = f$counts[1], g_evals = f$counts[2])))
                         f_evals.function g_evals.gradient
NM   -0.2726011 1.503158               65               NA
CG   -0.2725322 1.503424               25                9
BFGS -0.2725326 1.503425               31                9
sqrt(diag(solve(fits$BFGS$hessian)))     # SEs from the numerically computed Hessian at the optimum
[1] 0.1797399 0.2089537

All three land on the same estimate as mle_logistic_nr and glm above, and optim(..., hessian = TRUE) returns a finite-difference Hessian at the solution, so standard errors are available even for a method that never formed one during the iterations.

5.7.6 Stochastic Gradient Descent

5.7.6.1 Motivation: Large \(n\)

For iid data the negative log-likelihood and its gradient are sums over observations: \[ \mathcal{L}(\theta) = \sum_{i=1}^n \mathcal{L}_i(\theta), \qquad g(\theta) = \sum_{i=1}^n g^{(i)}(\theta), \qquad g^{(i)} = \nabla\mathcal{L}_i . \]

Every derivative-based method above needs the full gradient — an \(O(np)\) pass through all the data — at every single iteration, and Newton needs \(O(np^2)\) for the Hessian on top of that. With \(n\) in the millions and \(p\) in the millions, as in a large neural network, even one full gradient evaluation is expensive.

The idea behind stochastic gradient descent is that \(g^{(i)}(\theta)\) for a randomly chosen \(i\) is an unbiased estimate of the average gradient, \[ E_i\big[g^{(i)}(\theta)\big] = \frac{1}{n}g(\theta), \] so a noisy but cheap gradient estimate is often enough to make progress — the same Monte Carlo idea used elsewhere in this book, now applied to optimization rather than integration.

Notation. From here on a superscript in parentheses indexes cases or mini-batches and a subscript indexes iterations, as for \(\theta_k\): \(g^{(i)}\) is the gradient of case \(i\); \(g^{(B)} = \frac{1}{m}\sum_{i\in B} g^{(i)}\) is the average over a mini-batch \(B\) of \(m\) cases; and \(g_k\) is the gradient used at iteration \(k\), either the full gradient \(g(\theta_k)\) or a mini-batch gradient \(g^{(B_k)}(\theta_k)\).

5.7.6.2 The SGD Algorithm

For \(t = 1, 2, \ldots\):

  1. Sample an index \(i_t\) uniformly from \(\{1, \ldots, n\}\) — or shuffle the data once and cycle through it, so that one full pass is called an epoch.

  2. Update in the descent direction of that single observation’s negative log-likelihood: \[ \theta_{t+1} = \theta_t - \gamma_t\,g^{(i_t)}(\theta_t). \]

Mini-batch SGD averages the gradient over a batch \(B_t\) of \(m\) observations (typically \(m = 32\)–\(512\) in machine-learning practice, though far smaller batches suffice for the simple models in this book): \[ \theta_{t+1} = \theta_t - \gamma_t\,g^{(B_t)}(\theta_t), \qquad g^{(B_t)} = \frac{1}{m}\sum_{i\in B_t}g^{(i)}, \] which reduces the variance of the gradient estimate by a factor of \(1/m\) at \(m\) times the per-step cost, and is what lets the computation vectorize efficiently or run on a GPU. The learning rate \(\gamma_t\) plays the role that a line search played for the deterministic methods above — a line search itself would need a full pass over the data to evaluate \(\mathcal{L}\), defeating the purpose.

5.7.6.3 Learning Rate and Convergence

Because each update uses a noisy gradient, a constant learning rate \(\gamma\) makes \(\theta_t\) bounce around the optimum indefinitely, with a variance proportional to \(\gamma\). The classical result of Robbins and Monro (1951) is that \(\theta_t\) converges to the optimum provided \[ \sum_t \gamma_t = \infty \quad\text{and}\quad \sum_t \gamma_t^2 < \infty, \qquad\text{e.g. } \gamma_t = \frac{\gamma_0}{1 + t/t_0}, \] the first condition allowing the iterate to travel arbitrarily far from its start, and the second forcing the noise to die out fast enough for it to settle.

A learning rate that is too large causes divergence or persistent noise; one that is too small is painfully slow. Convergence is only sublinear — far slower than Newton’s quadratic rate per iteration — but each SGD iteration costs \(O(p)\) or \(O(mp)\) rather than \(O(np)\) for a full gradient or \(O(np^2)\) for a Hessian, and early progress is often fast even when final convergence is not. Common refinements are the momentum and Adam modifications of the gradient described next, which were developed for, or are now mostly used with, mini-batch gradients, and Polyak–Ruppert averaging of the iterates, \(\overline\theta_T = \tfrac1T\sum_{t\le T}\theta_t\), which restores the \(\sqrt n\)-efficiency of the MLE that the noisy path alone does not have.

5.7.6.4 Why SGD?

Two different arguments are made for SGD, and they are not equally firm.

Cost per unit of progress. This is the established reason, and it holds for any loss that is a sum over many cases, convex or not. Large data sets are redundant: a mini-batch of a few hundred cases usually points in nearly the same direction as all \(n\), so SGD takes many useful steps in the time a full-gradient method takes one. Since the MLE itself is accurate only to \(O(n^{-1/2})\), there is little value in optimizing far below that statistical error, and for large \(n\) SGD reaches that accuracy with less computation than methods that need the full gradient (Bottou and Bousquet, 2008, “The Tradeoffs of Large Scale Learning”).

The noise itself. The mini-batch gradient is the average gradient plus noise whose covariance, for \(m\) cases drawn with replacement, is \[ \operatorname{Var}\big(g^{(B)}\big) = \frac1m\Big(\frac1n\sum_{i=1}^n g^{(i)}g^{(i)\prime} - \bar g\,\bar g'\Big), \qquad \bar g = \frac1n\, g(\theta). \] Near a minimum \(\bar g \approx 0\), and the covariance is \(\frac{1}{nm}\) times the empirical Fisher information of Section 5.7.6.7, which under a correctly specified model approximates the Hessian. The noise is therefore largest along the directions of highest curvature, and its overall size is set by the ratio \(\gamma/m\) of learning rate to batch size. For non-convex losses this noise has been argued to help in three ways:

  • it moves the iterate away from saddle points, where exact gradient descent can stall (Ge, Huang, Jin and Yuan, 2015; Jin et al., 2017);
  • SGD behaves roughly like gradient descent on a smoothed version of the loss, in which small, sharp local minima disappear (Kleinberg, Li and Yuan, 2018);
  • for neural networks, small-batch training tends to end in wide, flat minima, which generalize better than the sharp minima reached with large batches (Keskar et al., 2017).

The first is proved, under conditions on the saddle points. The other two are supported by analyses under simplifying assumptions and by experiments, but they remain debated: large-batch training with a rescaled learning rate (Goyal et al., 2017), or full-batch gradient descent with explicit regularization (Geiping et al., 2022), can match small-batch SGD, and how flat a minimum is depends on how the network is parameterized (Dinh et al., 2017). The safest summary is that in neural networks SGD’s noise often acts as an implicit regularizer, by a mechanism that is not yet settled.

For a concave log-likelihood such as the logistic regression fitted above by Newton–Raphson, there is a single optimum, the MLE, and only the first argument applies. The noise then adds variance around the MLE rather than helping to find it, and Newton–Raphson or BFGS remain the better choices at such sizes. SGD earns its place on neural networks, where the loss is non-convex and \(n\) and \(p\) are large.

5.7.6.5 Momentum

At iteration \(k\) let \(B_k\) be the mini-batch and \(g^{(B_k)} = g^{(B_k)}(\theta_k)\) its gradient at the current iterate, the average of the case gradients \(g^{(i)}\), \(i \in B_k\) (superscript: which batch; subscript: which iteration). This gradient is noisy, and the iterates also zig-zag across valleys. Momentum (the heavy-ball method) modifies the gradient by averaging it over past iterations, which damps both the noise and the zig-zag: it keeps an exponentially weighted sum \(m_k\) of past gradients and steps along it, \[ m_k = \beta\, m_{k-1} + g^{(B_k)}, \qquad \theta_{k+1} = \theta_k - \alpha\, m_k, \] where \(\beta \in [0, 1)\), commonly \(0.9\), sets the memory. The across-valley components change sign from step to step and cancel in the average, while the along-valley components keep their sign and add up, so the step lengthens along the valley floor. With \(\beta = 0\) it reduces to gradient descent, and the effective step along a persistent direction is \(\alpha/(1-\beta)\), which is why \(\alpha\) is usually reduced when \(\beta\) is raised. Only the gradient is modified; the preconditioner is still the identity and no curvature information is used. On a deterministic full-data problem momentum has little to offer: conjugate gradient is the optimal form of the same idea for a quadratic and needs no \(\beta\), and BFGS or Newton–Raphson do better still. It is a modifier designed for SGD.

5.7.6.6 Adam

Momentum fixes the direction but not the scale: one step size \(\alpha\) serves every coordinate, although the coordinates of a network can have gradients of very different sizes. Adam (adaptive moment estimation; Kingma and Ba, 2014, “A Method for Stochastic Optimization”) adds a step size for each coordinate, estimated from the gradients themselves. Besides the averaged gradient it keeps an exponential average of the elementwise squared gradient, \[ m_k = \beta_1 m_{k-1} + (1-\beta_1)\, g^{(B_k)}, \qquad s_k = \beta_2 s_{k-1} + (1-\beta_2)\, g^{(B_k)} \odot g^{(B_k)}, \] started at \(m_0 = s_0 = 0\). Both averages are corrected for that start, \(\hat m_k = m_k/(1-\beta_1^k)\) and \(\hat s_k = s_k/(1-\beta_2^k)\), and the update is \[ \theta_{k+1} = \theta_k - \alpha\, \frac{\hat m_k}{\sqrt{\hat s_k} + \varepsilon}, \] with the usual defaults \(\beta_1 = 0.9\), \(\beta_2 = 0.999\) and \(\varepsilon = 10^{-8}\).

The update needs only the batch gradient \(g^{(B_k)}\): \(s_k\) is a running average of the squared gradient over iterations. Unrolling the recursion shows why the zero start does no harm: \(\hat s_k = \sum_{j\le k}\beta_2^{k-j} g_j^2 \big/ \sum_{j\le k}\beta_2^{k-j}\) is a proper weighted average of the squared gradients seen so far, with no trace of \(s_0\); in particular the first step is \(\alpha\, g_1/(|g_1| + \varepsilon) \approx \alpha\,\operatorname{sign}(g_1)\) in each coordinate. Dividing by \(\sqrt{\hat s_k}\) makes each coordinate move roughly \(\alpha\) per step whatever the scale of \(f\): coordinates with large gradients take smaller steps and flat ones larger steps, and the iteration becomes far less sensitive to how the parameters are scaled. Adam is momentum with a step size for each coordinate: with \(s_k\) held constant it is exactly momentum, so it contains the averaged gradient and adds the rescaling. It is the default optimizer for neural networks. As with momentum, its value lies in SGD: on a deterministic full-batch problem it rescales coordinates but uses no curvature, and gradient descent with a line search, conjugate gradient, BFGS or Newton–Raphson are better choices.

5.7.6.7 Comments on the Use of Squared Gradients for Rescaling in Adam

AdaGrad, RMSProp and Adam divide the gradient, coordinate by coordinate, by the square root of a running average of its own squared values:

Table 5.4: Adaptive step-size methods built from the history of the batch gradients.
Method Running average \(s_k\) of \(\big(g^{(B_j)}\big)^2\) (elementwise) Step
AdaGrad (Duchi, Hazan and Singer, 2011) cumulative sum \(\sum_{j \le k} \big(g^{(B_j)}\big)^2\) \(\alpha\, g^{(B_k)} / \sqrt{s_k}\)
RMSProp (Hinton, 2012) exponential average \(\beta_2 s_{k-1} + (1-\beta_2)\, \big(g^{(B_k)}\big)^2\) \(\alpha\, g^{(B_k)} / \sqrt{s_k}\)
Adam (Kingma and Ba, 2014) as RMSProp, bias-corrected \(\alpha\, \hat m_k / \sqrt{\hat s_k}\), with momentum

AdaGrad’s cumulative sum grows without bound, so its steps keep shrinking; RMSProp and Adam forget old gradients and do not. Why this rescaling helps has been explained from several perspectives. They are described below, with their limits, and none of them accounts for all of the behaviour seen in practice.

Perspective 1: the empirical Fisher information. Adam’s authors remark that \(\hat s_k\) approximates the diagonal of the Fisher information (Kingma and Ba, 2015, citing Pascanu and Bengio, 2013), which suggests reading the method as a diagonal version of natural gradient. To see what this does and does not mean, recall the empirical Fisher information of this chapter.

For a loss that is a sum over cases, \(\mathcal{L} = \sum_i \mathcal{L}_i\), the per-case gradients \(g^{(i)} = \nabla\mathcal{L}_i\) define the empirical Fisher information \[ \hat F(\theta) = \sum_{i=1}^n g^{(i)}(\theta)\, {g^{(i)}(\theta)}' . \]

Connection to the Fisher information. Each case’s score has mean zero at the true parameter, so the Fisher information of the \(n\) observations is \[ I_n(\theta) = n I(\theta) = \sum_{i=1}^n E\big[g^{(i)}{g^{(i)}}'\big] = E\big[J(\theta)\big] \] by the information equality. \(\hat F\) replaces the expectation by the observed scores: it estimates \(I_n(\theta)\) from first derivatives only, and it is positive semidefinite by construction, whatever \(\theta\).

BHHH. Using \(\hat F\) as the curvature in a Newton step gives the BHHH algorithm (Berndt, Hall, Hall and Hausman, 1974), also called the outer-product-of-gradients method, \[ \theta_{k+1} = \theta_k - \alpha_k\, \hat F(\theta_k)^{-1} g_k, \qquad \alpha_k \text{ from a line search}, \] beside Newton–Raphson, which uses the observed curvature \(J\), and Fisher scoring, which uses \(I_n\). Every step is a descent direction, and \(\hat F(\hat\theta)^{-1}\) at the optimum is the OPG estimate of the variance, which with the Hessian also forms the sandwich estimator \(J^{-1}\hat F J^{-1}\). BHHH is standard in econometrics (maxLik(..., method = "BHHH") in R). It needs per-case gradients, and \(\hat F\) can be a poor stand-in for \(I_n\) far from the MLE or when the model is misspecified.

The diagonal. \(\hat F\) is a \(p \times p\) matrix: \(O(p^2)\) storage and an \(O(p^3)\) solve, impossible for a network with \(p\) in the millions. Its diagonal is only \(p\) numbers, the sum over cases of the squared gradients, \[ \big[\operatorname{diag}\hat F\big]_r = \sum_{i=1}^n \Big(\frac{\partial \mathcal{L}_i}{\partial \theta_r}\Big)^2, \qquad \text{that is,} \quad \operatorname{diag}\hat F = \sum_{i=1}^n g^{(i)} \odot g^{(i)}, \] a curvature scale for each coordinate \(\theta_r\), computed from gradients that are already available, in \(O(p)\) storage.

Adam’s \(\hat s_k\) looks like \(\operatorname{diag}\hat F\), but the resemblance is loose:

  • \(s_k\) squares the batch average \(g^{(B_k)}\); it does not sum the squares of the case gradients. For a batch of \(m\) cases drawn at a fixed \(\theta\), with \(\bar g = \frac1n\sum_i g^{(i)}\), \[ E\big[g^{(B)} \odot g^{(B)}\big] = \bar g^2 + \frac1m\Big(\frac1n\operatorname{diag}\hat F - \bar g^2\Big), \] so it contains the squared mean gradient, which is the signal and not the noise. Only near the optimum, where \(\bar g \approx 0\), and if \(\theta\) changes little over the averaging window of about \(1/(1-\beta_2) = 1000\) iterations, is \(\hat s_k \approx \frac{1}{nm}\operatorname{diag}\hat F\). With full-batch gradients \(s_k\) is just the squared mean gradient, which tends to zero at the optimum.
  • Even where \(\hat s_k\) does estimate \(\operatorname{diag}\hat F\), \(\hat F\) stands for curvature only when the model is correctly specified (\(\hat F \approx I_n \approx J\)). Otherwise, and far from the optimum, it is a poor substitute for curvature (Kunstner, Hennig and Balles, 2019).
  • A Newton or scoring step multiplies by the inverse \(\hat F^{-1}\), a power \(-1\), whereas these methods use the power \(-\tfrac12\). With \(\hat s \propto g^2\) a power of \(-1\) would give the step \(\alpha/g\), which explodes where the gradient is small. Kingma and Ba note this themselves, calling the square root “more conservative” than natural gradient.

Methods that really do approximate the Fisher information of a network include K-FAC (Martens and Grosse, 2015), one Kronecker-factored block per layer, and natural gradient (Amari, 1998).

Perspective 2: a signal-to-noise ratio. Kingma and Ba’s own explanation is that \(\hat m_k/\sqrt{\hat s_k}\) is a signal-to-noise ratio. When the gradient direction is uncertain, \(\hat m_k\) is small relative to \(\sqrt{\hat s_k}\) and the step shrinks, which they call “a form of automatic annealing” near an optimum. The step is also bounded by about \(\alpha\) and is invariant to the scale of the gradient: multiplying \(g\) by a constant \(c\) cancels in \(\hat m_k/\sqrt{\hat s_k}\), so rescaling the parameters or the loss does not change the iteration. This explains the robustness to scale that makes Adam easy to use. It does not by itself show an improvement in the rate of convergence.

Perspective 3: online learning. AdaGrad was derived from a regret bound in online convex optimization (Duchi, Hazan and Singer, 2011): the per-coordinate step \(1/\sqrt{\sum_j g_j^2}\) is the best fixed scaling in hindsight, and it gives rates that adapt to sparse gradients. The bound is for AdaGrad, not for RMSProp or Adam, which forget old gradients. The convergence proof in the original Adam paper was later shown to be wrong, with simple convex problems on which Adam does not converge, and the variant AMSGrad was proposed (Reddi, Kale and Kumar, 2018).

Perspective 4: sign descent. Dividing a gradient by its magnitude leaves its sign. With \(\beta_1 = \beta_2 = 0\), Adam is sign descent, \(\theta_{k+1} = \theta_k - \alpha\operatorname{sign}(g)\), which is steepest descent in the max-norm (Bernstein et al., 2018). Balles and Hennig (2018) split Adam’s update into a sign and a magnitude, with the magnitude being a variance-based damping of the sign. On transformers, sign descent without the averaging recovers much of Adam’s advantage over SGD (Kunstner et al., 2023).

Perspective 5: heterogeneous noise and curvature. Two explanations for why adaptive methods beat SGD on attention models are heavy-tailed gradient noise, where the per-coordinate normalization acts like clipping (Zhang et al., 2020), and a Hessian whose blocks differ strongly from one parameter block to another, which a single learning rate cannot suit (Zhang et al., 2024). Kunstner et al. (2023) find that noise is not the main factor behind the gap between Adam and SGD on transformers, so these accounts compete.

Practical benefits. Whatever the explanation, the benefits are not in doubt. Adam needs little tuning, since its default \(\alpha\), \(\beta_1\), \(\beta_2\) work across a wide range of problems, it is insensitive to the scale of the parameters and the loss, it copes with coordinates whose gradients differ by orders of magnitude, and it costs only \(O(p)\) extra storage. It, or its variant AdamW, is the default for training transformers and many other deep networks.

An open question. There is still no theory that explains why this rescaling helps so much for deep networks, and the Fisher interpretation in particular is not supported. What can be said with some confidence is what the rescaling does (it equalizes the movement of coordinates and is invariant to scale) and that it is effective empirically. Which of the properties above is responsible for its advantage over SGD, and in which problems, remains to be explored.

5.7.7 Shinylive App for Comparing the Optimizers

There is a shinylive app to compare the paths that gradient descent, momentum, Adam, conjugate gradient, BFGS and safeguarded Newton take on the same objective, on six test functions from a quadratic to the Rosenbrock banana and Rastrigin’s many local minima. The app uses exact gradients, a deterministic setting in which momentum and Adam are not at their best, which is the point of the comparison: curvature information (BFGS, Newton) and line searches (CG, BFGS) pay off there, while momentum needs a learning rate suited to each function and diverges when it is too large, and Adam’s single default \(\alpha = 0.1\) works on all six but is slow along curved valleys.

5.8 Examples of Fitting Neural Networks (Shinylive App Illustration)

Both examples below use one input \(x\) and four tanh hidden units, so that \(\theta\) has \(13\) weights and the fit can be drawn as a curve. They differ only in the likelihood, hence in the output link and in the starting value \(\delta_{\eta_i}\) of backpropagation (Section 5.6.5); every other step of the gradient computation is the same. Each app fits the same data with seven optimizers: fixed-rate gradient descent, mini-batch SGD, momentum, Adam, gradient descent with a Wolfe line search, conjugate gradient and BFGS. One SGD iteration is an epoch, so that its progress can be compared with the full-batch methods.

5.8.1 Gaussian Neural Network

For \(y_i = \mu(x_i) + \varepsilon_i\) with \(\varepsilon_i \sim N(0, \sigma^2)\) the mean is the network output itself (identity link), \(\mu_i = \eta_i = \sum_{j=0}^{4} v_j h_{ij}\), and the loss is half the mean squared error, \[ \mathcal{L}(\theta) = \frac{1}{2n}\sum_{i=1}^n (y_i - \mu_i)^2, \qquad \delta_{\eta_i} = \mu_i - y_i. \] The factor \(\tfrac12\) changes neither the minimizer nor the shape of the loss. It is there because it makes the loss the Gaussian negative log-likelihood per observation, \(-\log N(y_i \mid \mu_i, \sigma^2) = \frac{(y_i-\mu_i)^2}{2\sigma^2} + \text{const}\), with \(\sigma^2 = 1\) and the constant dropped, and because it cancels the \(2\) produced by differentiating the square, so that \(\delta_{\eta_i} = \mu_i - y_i\) without a factor. Its minimum is not zero but about \(\sigma^2/2\), the noise floor, which the app draws as a dashed line. The model is non-convex: the four units can be permuted, and several of them can end up doing nearly the same work, so different optimizers and starting weights may reach different, equally good fits, and only some find a good one quickly. The Gaussian app lets you set the noise level \(\sigma\), draws the weights and their gradient as networks, and shows on the data scale how the four scaled hidden units add up to the fitted mean.

5.8.2 Poisson Neural Network

With a log link, \(\lambda_i = e^{\eta_i}\), and counts \(y_i \mid x_i \sim \mathrm{Poisson}(\lambda_i)\), the loss is \(\mathcal{L} = \frac1n\sum_i(\lambda_i - y_i\eta_i)\) and \(\delta_{\eta_i} = \lambda_i - y_i\); this is the model whose gradient is worked out in full in the backpropagation section above. The log link makes the hidden units add on the log scale, so \(\lambda(x)\) is a product of their contributions \(e^{v_j h_j(x)}\), and the loss is much more sensitive to a weight that is too large than in the Gaussian case, so a fixed learning rate is harder to choose. The Poisson app offers the same controls as the Gaussian one, with the sample size going up to \(2000\) so that mini-batch SGD can be compared with the full-batch methods when \(n\) is large.

5.9 Examples of Fitting NN with Tensorflow

The Gaussian network with a linear output (identity link) written in Keras, now for a mean function of two predictors with four modes: \(\mu(x_1, x_2)\) is a sum of four Gaussian bumps of different heights centred at \((\pm 1.5, \pm 1.5)\), \[ \mu(x_1, x_2) = \sum_{k=1}^{4} h_k \exp\!\Big(-\frac{(x_1 - c_{k1})^2 + (x_2 - c_{k2})^2}{2 \cdot 0.8^2}\Big), \] with centres \(c_1 = (-1.5, -1.5)\), \(c_2 = (-1.5, 1.5)\), \(c_3 = (1.5, -1.5)\), \(c_4 = (1.5, 1.5)\) and heights \(h = (2.0, 1.5, 1.0, 1.8)\), and \(y = \mu(x_1, x_2) + \varepsilon\) with \(\varepsilon \sim N(0, 0.2^2)\), so that the smallest attainable loss is the noise floor \(\sigma^2 = 0.2^2 = 0.04\). The network has one hidden layer of 16 tanh units and a linear output, that is two layers of weights and \(2\cdot 16 + 16 + 16 + 1 = 65\) parameters. The data are split into a training and a test set; the network is fitted to the training set only, and the test set shows how well it predicts new observations.

Fitting Neural Networks with Tensorflow

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"          # quiet TensorFlow's start-up messages
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt


tf.keras.utils.set_random_seed(1)                 # numpy, python and TensorFlow seeds

# the true mean: four bumps of different heights, centred at (+-1.5, +-1.5)
centres = np.array([[-1.5, -1.5], [-1.5, 1.5], [1.5, -1.5], [1.5, 1.5]])
heights = np.array([2.0, 1.5, 1.0, 1.8])
def mu_true(x):
    d2 = ((x[:, None, :] - centres[None]) ** 2).sum(axis=-1)       # n x 4 squared distances
    return (heights * np.exp(-d2 / (2 * 0.8 ** 2))).sum(axis=1, keepdims=True)

# data: y = mu(x1, x2) + noise
rng = np.random.default_rng(1)
n, sigma = 1500, 0.2
x = rng.uniform(-3, 3, size=(n, 2)).astype("float32")
y = (mu_true(x) + sigma * rng.normal(size=(n, 1))).astype("float32")

# train / test split: the first 1200 observations fit the network, the last 300 test it
ntr = 1200
x_tr, y_tr, x_te, y_te = x[:ntr], y[:ntr], x[ntr:], y[ntr:]

# the model: eta = v_0 + sum_j v_j tanh(w_j1 x_1 + w_j2 x_2 + w_j0), identity link
model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(2,)),
    tf.keras.layers.Dense(16, activation="tanh"),   # hidden layer: W (2 x 16), w_0 (16)
    tf.keras.layers.Dense(1),                       # output layer: v (16 x 1), v_0
])

# the loss is chosen separately from the model. Keras calls it as loss(y_true, y_pred):
#   y_true = a mini-batch of the targets y_tr passed to fit()
#   y_pred = the output of the last layer of the network for the same rows, here eta = mu;
#            Keras does not check that this suits the loss, so the output layer and the
#            loss have to be chosen together. The names y, mu are free.
# the mean squared error, the Gaussian negative log-likelihood up to a constant
square_loss = lambda y, mu: tf.reduce_mean(tf.square(y - mu))

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01), loss=square_loss)

# fit(x, y): x is fed to the network, y is held back as the targets of the loss; the rows
# of x_tr and y_tr are shuffled together and cut into mini-batches of 50 observations
history = model.fit(x_tr, y_tr, batch_size=50, epochs=600, verbose=0)   # one epoch = 1200/50 mini-batch steps

print("training loss:", history.history["loss"][-1], "  noise floor:", sigma**2)
training loss: 0.04587170109152794   noise floor: 0.04000000000000001
print("number of weights:", model.count_params())
number of weights: 65

Evaluating the Fitting by Tensorflow

Code
# fitted values, and R^2 = 1 - SSE / SST on each set
r2 = lambda y, yhat: 1 - np.sum((y - yhat)**2) / np.sum((y - y.mean())**2)
yhat_tr = model.predict(x_tr, verbose=0)
yhat_te = model.predict(x_te, verbose=0)

# the true and the fitted mean on a grid, drawn with the same contour levels
g = np.linspace(-3, 3, 100, dtype="float32")
G1, G2 = np.meshgrid(g, g)
grid = np.column_stack([G1.ravel(), G2.ravel()])
Z_true = mu_true(grid).reshape(G1.shape)
Z_fit = model.predict(grid, verbose=0).reshape(G1.shape)
levels = np.linspace(-0.2, 2.0, 12)

fig, ax = plt.subplots(1, 3, figsize=(12, 4.4))
for a, Z, title in [(ax[0], Z_true, r"true $\mu(x_1, x_2)$"), (ax[1], Z_fit, r"fitted $\hat\mu(x_1, x_2)$")]:
    cs = a.contourf(G1, G2, Z, levels=levels, cmap="viridis", alpha=0.85)
    a.contour(G1, G2, Z, levels=levels, colors="k", linewidths=0.5)
    a.set_title(title); a.set_xlabel(r"$x_1$"); a.set_ylabel(r"$x_2$")
cbar = fig.colorbar(cs, ax=ax[:2], shrink=0.85, pad=0.02)   # assigned so that the object is not printed

# right: fitted value against observed response
ax[2].scatter(y_tr, yhat_tr, s=8, color="grey", alpha=0.6,
              label=f"training, $R^2$ = {r2(y_tr, yhat_tr):.3f}")
ax[2].scatter(y_te, yhat_te, s=12, color="deepskyblue", alpha=0.8,
              label=f"test, $R^2$ = {r2(y_te, yhat_te):.3f}")
lim = [y.min(), y.max()]
ax[2].plot(lim, lim, "k:", lw=1)
ax[2].set_xlabel("observed y"); ax[2].set_ylabel(r"fitted $\hat{y}$")
ax[2].legend(frameon=False, loc="upper left")
plt.show()

In Keras the model only defines the network, \(x \mapsto \eta(x;\theta)\); the response enters through the loss given to compile. Choosing the squared-error loss \((y-\mu)^2\) is what makes this a Gaussian model with an identity link. For counts one would end the network with the same linear layer and use the loss \(e^{\eta} - y\eta\) of the Poisson model, so the output layer and the loss have to be chosen consistently by the user. Everything the chapter did by hand happens inside fit: the forward pass computes \(\mu_i\), backpropagation produces the gradient of the mini-batch loss, and Adam turns that gradient into the update \(\theta_{k+1} = \theta_k - \alpha_k P_k m_k\) of Table 5.5. The optimizer is the only line to change in order to repeat the comparison of the apps: SGD(learning_rate=0.1), SGD(learning_rate=0.1, momentum=0.9) or Adam(0.01). Setting batch_size=ntr makes each step a full-batch one. The loss is not convex, so different random initial weights can end in different, equally good fits. The \(R^2\) cannot reach \(1\) even for the true mean, because the noise variance \(0.04\) is part of the variance of \(y\); the contour plot is the better check that all four modes have been found. The test \(R^2\) is close to the training one because the network has \(65\) weights for \(1200\) observations; a network with many more weights would show the gap that the test set exists to reveal.

The same network gives a Poisson regression by changing only the loss. The last layer is still a linear Dense(1), whose output is now the log-mean \(\eta_i = \log\lambda_i\), so the loss must exponentiate it:

# Poisson negative log-likelihood per observation, dropping log(y!):  lambda - y * eta,
# with lambda = exp(eta); here the second argument is eta = log(lambda), not lambda
poisson_nll = lambda y, eta: tf.reduce_mean(tf.exp(eta) - y * eta)

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01), loss=poisson_nll)
model.fit(x_tr, y_tr, batch_size=50, epochs=600, verbose=0)    # y_tr now holds counts

lambda_hat = np.exp(model.predict(x_te, verbose=0))            # predictions are on the eta scale

Keras also ships such losses, for example tf.keras.losses.Poisson, but that one expects the mean \(\lambda\) as its second argument and uses \(\lambda - y\log\lambda\), so it would need an exp activation on the last layer. Writing the loss on the \(\eta\) scale as above is numerically safer and matches the loss \(\lambda_i - y_i\eta_i\) of the chapter. The code above is shown, not run.

5.10 Remarks on Implementation of Optimization Algorithms

The chapter has covered a lot of ground, from bisection on a scalar gradient \(g\) to stochastic gradient descent on millions of parameters. This section collects the gradient-based methods, Newton–Raphson included, into one reference: first as a single update rule in which they differ only in the gradient they use and in how it is modified, then by what each needs and costs, then by which R function to reach for and how to choose among them. Derivative-free methods such as Nelder–Mead are left out.

5.10.1 One Form for All the Methods

Every method of this chapter that uses the gradient can be written as the same update, \[ \theta_{k+1} = \theta_k - \alpha_k\, P_k\, m_k , \] where \(g_k = \nabla f(\theta_k)\) is the gradient of the objective \(f = \mathcal{L}\) at the current iterate, and the three remaining ingredients are what distinguishes the methods:

  • \(m_k\) is the gradient used: the gradient itself, a sampled version of it (mini-batches), or a smoothed version of it (a running average of past gradients);
  • \(P_k\) is the modifier or preconditioner, a matrix that rescales or rotates the step, built from curvature, from Fisher information, or from the history of gradients, with \(P_k = I\) when there is none;
  • \(\alpha_k\) is the step size: set by hand, found by a line search, or fixed at \(1\).
Table 5.5: Writes every gradient-based method of this chapter as \(\theta_{k+1} = \theta_k - \alpha_k P_k m_k\), showing the gradient each one uses, how it modifies it, and how the step size is chosen. The common form of the gradient-based methods.
Method Gradient used, \(m_k\) Modifier, \(P_k\) Step size, \(\alpha_k\)
Steepest descent, fixed rate \(g_k\) \(I\) \(\alpha\), set by hand
Steepest descent, line search \(g_k\) \(I\) \(\alpha_k\) from a Wolfe line search
Momentum \(v_k = \beta v_{k-1} + g_k\), a weighted sum of past gradients \(I\) \(\alpha\), set by hand
Adam \(\hat m_k\), a bias-corrected exponential average of past gradients \(\mathrm{diag}\big(1/(\sqrt{\hat s_k} + \varepsilon)\big)\), with \(\hat s_k\) the running average of \(g^{(B_j)} \odot g^{(B_j)}\) \(\alpha\), set by hand
Conjugate gradient \(d_k = -g_k + \beta_k d_{k-1}\), the gradient combined with the previous direction implicit: successive directions conjugate with respect to the Hessian \(\alpha_k\) from a line search
BFGS, L-BFGS \(g_k\) \(B_k \approx J(\theta_k)^{-1}\), built from \(s_k = \theta_{k+1}-\theta_k\) and \(y_k = g_{k+1}-g_k\); L-BFGS keeps only the last \(m\) pairs \(\alpha_k\) from a Wolfe line search
Newton–Raphson \(g_k\) \(J(\theta_k)^{-1}\), the inverse observed information \(1\), or step-halving as a safeguard
Fisher scoring, IRLS \(g_k\) \(I(\theta_k)^{-1}\), the inverse expected information \(1\)
Stochastic gradient descent \(g_k^{B} = \frac1m\sum_{i\in B_k}\nabla\mathcal{L}_i(\theta_k)\) on a random mini-batch, with \(E\,g_k^B = g_k\) \(I\) \(\alpha_k\) decreasing, set by hand

Read down the table and the progression of the chapter appears. Steepest descent uses the gradient as it is. Conjugate gradient changes which direction is used, by combining the gradient with the past direction, which damps the zig-zag across a valley. Momentum does so for SGD by averaging the gradient over the past, and Adam also changes the scale of each coordinate with a diagonal estimate built from the gradients alone. BFGS learns a full inverse-Hessian approximation from gradient differences, and Newton–Raphson and Fisher scoring compute that matrix from second derivatives, which is why they alone take unit steps and converge quadratically near the solution. Stochastic gradient descent leaves \(P_k\) alone and changes the gradient instead, to a cheap and noisy one, and momentum and Adam can be applied to it unchanged. The same table is the one used in the descriptions of the two neural-network apps.

5.10.2 Comparison

Table 5.6: Summarizes the trade-offs among the gradient-based methods of this chapter: what each needs, what it costs, and how fast and robust it is. Comparison of gradient-based optimization methods. \(p\) is the number of parameters and \(m\) the L-BFGS memory length (typically 5–20).
Method Derivatives Storage Cost per iteration Local rate Robust start Hessian for SEs
Steepest descent gradient \(O(p)\) 1 gradient + line search linear (slow) good no
Momentum gradient \(O(p)\) 1 gradient + \(O(p)\) linear (faster along valleys) good no
Adam gradient \(O(p)\) 1 gradient + \(O(p)\) sublinear, noisy near the optimum good no
Conjugate gradient gradient \(O(p)\) 1 gradient + line search linear (fast) moderate no
BFGS gradient \(O(p^2)\) 1 gradient + \(O(p^2)\) superlinear moderate approximate
L-BFGS gradient \(O(mp)\) 1 gradient + \(O(mp)\) superlinear moderate no
Newton–Raphson gradient, Hessian \(O(p^2)\) 1 Hessian + \(O(p^3)\) quadratic poor exact
Fisher scoring / IRLS gradient, expected information \(O(p^2)\) 1 weighted LS fit near-quadratic fair exact
Stochastic gradient minibatch gradient \(O(p)\) \(O(\text{batch})\) sublinear good no

5.10.3 Software Packages for Optimization

The methods of this chapter are rarely coded by hand. The packages below are the commonly used ones, grouped by the job they do: general-purpose numerical optimizers, statistical model fitting, and automatic differentiation with mini-batch optimizers for neural networks.

Table 5.7: Lists the commonly used software for the optimization methods of this chapter, in R and in Python, with the methods each offers. Software packages for optimization
Language Package, function Methods available Notes
R optim (stats) BFGS, CG, L-BFGS-B General workhorse; supply gr or the gradient is differenced; hessian = TRUE for inference; L-BFGS-B allows box constraints
R nlm (stats) Newton-type with ridging Uses the analytic gradient and Hessian if attached as attributes, otherwise numerical; returns $hessian
R nlminb (stats) Trust-region (PORT) Reliable on badly scaled problems; supports box constraints
R optimize, uniroot (stats) Golden section, Brent One dimension only
R glm (stats) IRLS (Fisher scoring) The specialised case for generalized linear models; use start when separation is suspected
R nloptr Large NLopt collection Gradient-based and global methods, with constraints
R torch SGD, momentum, Adam, L-BFGS Automatic differentiation and mini-batch training of neural networks
Python scipy.optimize minimize with BFGS, L-BFGS-B, CG, Newton-CG, trust-region methods; root; minimize_scalar The standard general-purpose library; the counterpart of optim and nlm
Python statsmodels IRLS for GLMs, Newton–Raphson and BFGS for discrete-choice and other likelihood models Returns standard errors from the Hessian
Python scikit-learn LogisticRegression solvers lbfgs, newton-cg, sag, saga; SGDClassifier, SGDRegressor Fitting of standard models; the optimizer is chosen by an argument
Python PyTorch (torch.optim) SGD (with momentum), Adam, AdamW, RMSprop, L-BFGS Gradients by autograd; the training loop is written explicitly
Python TensorFlow / Keras (tf.keras.optimizers) SGD (with momentum), Adam, AdamW, RMSprop, Adagrad Gradients by tf.GradientTape; model.fit runs the mini-batch loop; BFGS and L-BFGS in TensorFlow Probability (tfp.optimizer)
Python JAX with Optax SGD, momentum, Adam, AdamW; jax.scipy.optimize.minimize for BFGS jax.grad differentiates ordinary numerical functions; the optimizers are composable

For small and moderate likelihood problems the first rows are all that is needed: supply the negative log-likelihood and, if possible, its gradient, and let BFGS or a Newton-type method do the rest. Fitting a neural network is different. The gradient is obtained by automatic differentiation, which carries out the backpropagation of Section 5.6.5 for any model written with the library’s operations, and the update is applied to mini-batches by one of the optimizers of this chapter. TensorFlow and PyTorch are the two dominant libraries for this, and the Keras interface of TensorFlow makes the choice of optimizer a single argument. The Poisson network of this chapter, with the loss \(\lambda_i - y_i\eta_i\) and four hidden units, is fitted by Adam as follows.

5.10.4 Optimization in Practice for Deep Learning

Fitting a deep network is the setting for which the methods of this section are used at the largest scale, with \(p\) from millions to hundreds of billions and \(n\) so large that a full pass through the data is rarely made. The practice differs from the small examples of this chapter in four ways.

  • Only mini-batch gradients. The gradient comes from backpropagation (Section 5.6.5) on a mini-batch, and it is the only derivative information used. Full-batch methods, and second-order methods such as Newton–Raphson or BFGS, need a full pass or \(O(p^2)\) storage, which is out of reach. Their place is taken by the diagonal rescaling of the Adam family (Table 5.4), and in research by cheaper approximations of curvature such as Shampoo and K-FAC.
  • Adam-type methods as the default. AdamW, which is Adam with the weight decay applied directly to the weights rather than through the gradient, is the usual choice for transformers and language models, mainly because it needs little tuning and is robust to badly scaled weights. SGD with momentum, usually with a carefully tuned schedule and weight decay, remains common for image classification, where it is often reported to generalize slightly better; this is a rule of thumb that depends on the model and the data, not a theorem.
  • The learning-rate schedule matters as much as the optimizer. A typical recipe is a short warmup followed by a cosine or linear decay of \(\alpha_k\), in the spirit of the Robbins–Monro condition above. Gradient clipping, large batches with a rescaled learning rate, and mixed-precision arithmetic are further standard ingredients.
  • The loss is not the only goal. The objective is highly non-convex, with many equivalent minima, and a loss driven to its exact minimum can overfit. Practitioners stop early, regularize, and judge the fit on held-out data, so convergence to a stationary point is rarely checked in the sense used earlier in this chapter.

The two apps in the next section are deliberately tiny, with one input and four hidden units, so that the effect of each optimizer on the weights, the gradient and the fitted curve can be seen. The same update rules, with the same formulas, are what runs inside the deep learning libraries.