Curvature, Newton's method, and convexity
The whole book is also in the PDF.
We now reverse our perspective and employ eigendecomposition as a powerful analytical tool to dissect the geometry of optimization problems. This chapter introduces the Hessian matrix, which captures a function’s local curvature. Because the Hessian is a symmetric matrix, its eigendecomposition gives a blueprint of the optimization landscape. The eigenvalues of the Hessian often reveal the nature of critical points, distinguishing between true minima, maxima, and saddle points. This analysis provides the foundation for the crucial concept of convexity, the property that guarantees a local minimum is also a global one. Armed with this understanding of curvature, we move beyond first-order methods to derive Newton’s Method, an algorithm that leaps towards the solution by leveraging a full second-order model of the function. We then prove how fast gradient descent converges: the largest eigenvalue of the Hessian sets a safe step size, and its ratio to the smallest sets the speed. We conclude by showing how this second-order perspective inspires practical approximations, such as diagonal preconditioning, that form the basis of modern adaptive optimizers used throughout AI.
Keywords. Quadratic Form | Hessian Matrix | Second-Order Taylor Approximation | Curvature | Convexity | Positive Definite Matrix | Newton’s Method | Diagonal Preconditioning | Step Size | Convergence Rate | Condition Number
6.1 Eigendecomposition for optimization
Thus far, we demonstrated how the search for a matrix’s principal eigenvector could be framed as an optimization problem. We saw that a simple iterative algorithm, the Power Method, could “climb the hill” of a quadratic form to find the direction of maximum stretch. Now, we reverse our viewpoint.
Having established how to find eigenpairs, we now ask the opposite question: What can an eigendecomposition tell us about optimization?
The answer lies in using eigendecomposition not as a problem to be solved, but as a powerful analytical lens through which to view the geometry of complex functions. Specifically, we will apply it to the Hessian matrix: the matrix of second derivatives that describes the local curvature of our loss function.
6.2 Reminder of second-order derivatives: The Hessian
First-order methods, like gradient descent, operate by assuming the function is locally linear. They ask, “If I take a small step, which direction is steepest downhill?” and follow the negative gradient. This is effective but shortsighted. It fails to account for how the slope itself is changing. To build more sophisticated and faster algorithms, we must ask a more nuanced question: “How does the landscape curve beneath our feet?” The mathematical object that answers this question is the Hessian matrix.
6.2.1 Defining Curvature
Just as the gradient is a vector containing all first partial derivatives, the Hessian is a matrix containing all second partial derivatives. It captures the rate of change of the gradient, and thus, the function’s curvature.
Definition 6.1 (Hessian Matrix). For a twice-differentiable multivariate function \(f: \mathbb{R}^p \rightarrow \mathbb{R}\), the Hessian matrix \(\nabla^2 f(\mathbf{x})\) is the \(p \times p\) matrix of its second-order partial derivatives:
\[ \begin{aligned} \nabla^2 f(\mathbf{x}) = \begin{bmatrix} \tfrac{\partial^2 f}{\partial x_1^2}&\tfrac{\partial^2 f}{\partial x_1\partial x_2}&\cdots&\tfrac{\partial^2 f}{\partial x_1\partial x_p}\\ \vdots&\vdots& \ddots &\vdots\\ \tfrac{\partial^2 f}{\partial x_p\partial x_1}&\tfrac{\partial^2 f}{\partial x_p\partial x_2}&\cdots&\tfrac{\partial^2 f}{\partial x_p^2}\\ \end{bmatrix}. \end{aligned} \]For functions with continuous second derivatives (which includes virtually all functions of interest in AI), Clairaut’s theorem guarantees that the order of differentiation does not matter (\(\frac{\partial^2 f}{\partial x_i \partial x_j} = \frac{\partial^2 f}{\partial x_j \partial x_i}\)), which means the Hessian matrix is always symmetric.1
Example 6.1 (A Simple Affine Function). An affine function represents a flat plane or hyperplane. As such, it has no curvature, and its second derivatives should all be zero.
Let \(f: \mathbb{R}^p \rightarrow \mathbb{R}\) be an affine function defined by:
\[ f(\mathbf{x}) = \mathbf{a}^\top \mathbf{x} + b = \sum_{i=1}^p a_i x_i + b, \]where \(\mathbf{a} \in \mathbb{R}^p\) is a constant vector and \(b \in \mathbb{R}\) is a constant scalar. We compute the partial derivative with respect to each \(x_k\) of the vector \(\mathbf{x}\):
\[ \frac{\partial f}{\partial x_k} = \frac{\partial}{\partial x_k} \left( \sum_{i=1}^p a_i x_i + b \right) = a_k \]Since this is true for all \(k = 1, \dots, p\), the gradient vector is constant:
\[ \nabla f(\mathbf{x}) = \begin{bmatrix} a_1 \\ a_2 \\ \vdots \\ a_p \end{bmatrix} = \mathbf{a} \]Now, we compute the second partial derivatives, which are the elements \(H_{kj} = \frac{\partial^2 f}{\partial x_k \partial x_j}\). This involves differentiating the components of the gradient:
\[ H_{kj} = \frac{\partial}{\partial x_j} \left( \frac{\partial f}{\partial x_k} \right) = \frac{\partial}{\partial x_j} (a_k). \]Since \(a_k\) is a constant, its derivative with respect to any variable \(x_j\) is zero.
\[ H_{kj} = 0. \]This is true for all combinations of \(j\) and \(k\). Thus, the Hessian matrix is the \(p \times p\) zero matrix:
\[ \nabla^2 f(\mathbf{x}) = \mathbf{0} = \begin{bmatrix} 0 & 0 & \cdots & 0 \\ 0 & 0 & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & 0 \end{bmatrix}. \]Example 6.2 (A General Quadratic Function). Let \(f: \mathbb{R}^p \rightarrow \mathbb{R}\) be defined by the quadratic form:
\[ f(\mathbf{x}) = \frac{1}{2}\mathbf{x}^\top \mathbf{Q} \mathbf{x} - \mathbf{b}^\top \mathbf{x}, \]where \(\mathbf{Q} \in \mathbb{R}^{p \times p}\) is a symmetric matrix and \(\mathbf{b} \in \mathbb{R}^p\) is a vector. We can write the function in summation notation:
\[ f(\mathbf{x}) = \frac{1}{2} \sum_{i=1}^p \sum_{j=1}^p Q_{ij}x_i x_j - \sum_{i=1}^p b_i x_i. \]Taking the partial derivative with respect to \(x_k\):
\[ \frac{\partial f}{\partial x_k} = \frac{1}{2} \sum_{i=1}^p \sum_{j=1}^p Q_{ij} \frac{\partial}{\partial x_k}(x_i x_j) - b_k. \]The term \(\frac{\partial}{\partial x_k}(x_i x_j)\) is non-zero only if \(i=k\) or \(j=k\). Using the product rule, this becomes \(x_j\) if \(i=k\) and \(x_i\) if \(j=k\). So
\[ \begin{aligned} \frac{\partial f}{\partial x_k} = \frac{1}{2}\sum_{j=1}^p Q_{kj}x_j + \frac{1}{2}\sum_{i=1}^p Q_{ik}x_i - b_k = \frac{1}{2}(\mathbf{Q}\mathbf{x})_k + \frac{1}{2}(\mathbf{Q}^\top\mathbf{x})_k - b_k . \end{aligned} \]Due to the symmetry of \(\mathbf{Q}\) (\(Q_{kj} = Q_{jk}\)), the two halves are equal, and the derivative simplifies to the \(k\)-th row of \(\mathbf{Q}\) multiplied by \(\mathbf{x}\) (without symmetry, the gradient would be \(\frac{1}{2}(\mathbf{Q} + \mathbf{Q}^\top)\mathbf{x} - \mathbf{b}\)):
\[ \frac{\partial f}{\partial x_k} = \sum_{j=1}^p Q_{kj} x_j - b_k. \]In vector form, this is:
\[ \nabla f(\mathbf{x}) = \mathbf{Q}\mathbf{x} - \mathbf{b}. \]Setting the gradient to zero gives the linear system \(\mathbf{Q}\mathbf{x} = \mathbf{b}\): the stationary points of a quadratic are exactly the solutions of a linear system, the problem of Chapter 4. When \(\mathbf{Q}\) is Positive Definite (defined later in this chapter), there is exactly one, \(\mathbf{x}^\star = \mathbf{Q}^{-1}\mathbf{b}\), and it is the minimizer. For least squares (the next example), this system is the normal equations.
We now differentiate the components of the gradient, \((\nabla f(\mathbf{x}))_k = \sum_{j=1}^p Q_{kj} x_j - b_k\), with respect to \(x_l\):
\[ H_{lk} = \frac{\partial}{\partial x_l} \left( \sum_{j=1}^p Q_{kj} x_j - b_k \right) = \sum_{j=1}^p Q_{kj} \frac{\partial x_j}{\partial x_l}. \]The term \(\frac{\partial x_j}{\partial x_l}\) is 1 if \(j=l\) and 0 otherwise. Thus, the only term that survives the sum is when \(j=l\), giving:
\[ H_{lk} = Q_{kl} = Q_{lk} \quad (\text{since } \mathbf{Q} \text{ is symmetric}). \]Thus, the Hessian matrix of the quadratic function is simply the matrix \(\mathbf{Q}\) itself:
\[ \nabla^2 f(\mathbf{x}) = \mathbf{Q}. \]Example 6.3 (Linear Regression). Let the data matrix be \(\mathbf{A} \in \mathbb{R}^{n \times p}\) (\(n\) samples, \(p\) features, where we will assume for now that \(n > p\) and that the columns of \(\mathbf{A}\) are linearly independent) and the target vector be \(\mathbf{y} \in \mathbb{R}^n\). The parameters are \(\mathbf{x} \in \mathbb{R}^p\). The least-squares loss (with a factor \(\frac{1}{2}\) that simplifies the derivatives) is:
\[ f(\mathbf{x}) = \frac{1}{2} \|\mathbf{y} - \mathbf{A}\mathbf{x}\|_2^2 = \frac{1}{2} (\mathbf{y} - \mathbf{A}\mathbf{x})^\top (\mathbf{y} - \mathbf{A}\mathbf{x}). \]Expanding this gives:
\[ f(\mathbf{x}) = \frac{1}{2}(\mathbf{y}^\top\mathbf{y} - 2\mathbf{y}^\top\mathbf{A}\mathbf{x} + \mathbf{x}^\top\mathbf{A}^\top\mathbf{A}\mathbf{x}). \]This function is a direct application of our general quadratic form from Example 6.2, with parameters \(\mathbf{x}\). By matching the terms: the quadratic matrix is \(\mathbf{Q} = \mathbf{A}^\top\mathbf{A}\); the linear vector is \(\mathbf{b} = \mathbf{A}^\top\mathbf{y}\); the variable is \(\mathbf{x}\).
Using the results from Example 6.2 directly:
- Gradient: \(\nabla f(\mathbf{x}) = \mathbf{Q}\mathbf{x} - \mathbf{b} = \mathbf{A}^\top\mathbf{A}\mathbf{x} - \mathbf{A}^\top\mathbf{y} = \mathbf{A}^\top(\mathbf{A}\mathbf{x} - \mathbf{y}) \in \mathbb{R}^p\).
- Hessian: \(\nabla^2 f(\mathbf{x}) = \mathbf{Q} = \mathbf{A}^\top\mathbf{A} \in \mathbb{R}^{p \times p}\).
Thus, the resulting Hessian is:
\[ \nabla^2 f(\mathbf{x}) = \mathbf{A}^\top\mathbf{A}. \]Example 6.4 (Logistic Regression (Log Loss)). Let \(h_i = \sigma(\mathbf{w}^\top \mathbf{x}_i) = (1 + e^{-\mathbf{w}^\top \mathbf{x}_i})^{-1}\). The negative log-likelihood (log loss) for a dataset of \(n\) samples is:
\[ f(\mathbf{w}) = \sum_{i=1}^n -[y_i \log(h_i) + (1-y_i) \log(1-h_i)]. \]Here \(y_i \in \{0, 1\}\) is the label of sample \(\mathbf{x}_i \in \mathbb{R}^p\), and \(\mathbf{X} \in \mathbb{R}^{n \times p}\) is the data matrix whose \(i\)-th row is \(\mathbf{x}_i^\top\). (Chapter 2 wrote the same loss, for one data point with label \(y = \pm 1\), as \(\log(1 + e^{-y \mathbf{a}^\top\mathbf{w}})\).) Write \(z_i = \mathbf{w}^\top\mathbf{x}_i\), so \(h_i = \sigma(z_i)\). Using \(\sigma'(z) = \sigma(z)(1-\sigma(z))\), the derivative of the \(i\)-th term with respect to \(z_i\) is
\[ \begin{aligned} -y_i\frac{h_i(1-h_i)}{h_i} + (1-y_i)\frac{h_i(1-h_i)}{1-h_i} = -y_i(1-h_i) + (1-y_i)h_i = h_i - y_i . \end{aligned} \]By the chain rule, the gradient of \(z_i\) with respect to \(\mathbf{w}\) is \(\mathbf{x}_i\), so:
\[ \nabla f(\mathbf{w}) = \sum_{i=1}^n (h_i - y_i)\mathbf{x}_i = \mathbf{X}^\top(\mathbf{h} - \mathbf{y}), \]where \(\mathbf{h}\) is the vector of predictions \(h_i\).
We differentiate the gradient again with respect to \(\mathbf{w}\). Then:
\[ \begin{aligned} \nabla^2 f(\mathbf{w}) &= \frac{\partial}{\partial \mathbf{w}^\top} \left( \sum_{i=1}^n (h_i - y_i)\mathbf{x}_i \right) \\ &= \sum_{i=1}^n \mathbf{x}_i \frac{\partial h_i}{\partial \mathbf{w}^\top} \end{aligned} \]In the \(i\)-th term only the number \(h_i\) depends on \(\mathbf{w}\), so its derivative is the column \(\mathbf{x}_i\) times the row vector \(\frac{\partial h_i}{\partial \mathbf{w}^\top}\), a \(p \times p\) matrix. The order matters: the product \(\frac{\partial h_i}{\partial \mathbf{w}^\top}\mathbf{x}_i\), a row times a column, is a single number. The derivative of \(h_i = \sigma(\mathbf{w}^\top \mathbf{x}_i)\) with respect to the vector \(\mathbf{w}\) is:
\[ \frac{\partial h_i}{\partial \mathbf{w}} = \sigma'(\mathbf{w}^\top \mathbf{x}_i) \cdot \mathbf{x}_i = h_i(1-h_i)\mathbf{x}_i. \]Transposing this to get the required row vector \(\frac{\partial h_i}{\partial \mathbf{w}^\top}\):
\[ \frac{\partial h_i}{\partial \mathbf{w}^\top} = h_i(1-h_i)\mathbf{x}_i^\top. \]Substituting this back into the Hessian formula:
\[ \nabla^2 f(\mathbf{w}) = \sum_{i=1}^n \mathbf{x}_i \left( h_i(1-h_i)\mathbf{x}_i^\top \right) = \sum_{i=1}^n h_i(1-h_i) \mathbf{x}_i\mathbf{x}_i^\top. \]The Hessian can be written compactly as \(\mathbf{X}^\top \mathbf{D} \mathbf{X}\), where \(\mathbf{D}\) is a diagonal matrix with diagonal entries \(D_{ii} = h_i(1-h_i)\):
\[ \nabla^2 f(\mathbf{w}) = \mathbf{X}^\top {\rm diag}(h_1(1-h_1), \dots, h_n(1-h_n)) \mathbf{X}. \]Observe that the Hessian depends on the current parameters \(\mathbf{w}\) through the predictions \(h_i\).
Example 6.5 (A Trigonometric Function). Let \(f: \mathbb{R}^2 \rightarrow \mathbb{R}\) be defined as:
\[ f(x, y) = \sin(x) \cos(y). \]We compute the Gradient:
\[ \frac{\partial f}{\partial x} = \cos(x) \cos(y), \quad \frac{\partial f}{\partial y} = -\sin(x) \sin(y) \] \[ \nabla f(x, y) = \begin{bmatrix} \cos(x) \cos(y) \\ -\sin(x) \sin(y) \end{bmatrix} \]Then, we compute the Hessian: We compute the four second partial derivatives:
- \(\frac{\partial^2 f}{\partial x^2} = \frac{\partial}{\partial x}(\cos(x) \cos(y)) = -\sin(x) \cos(y)\);
- \(\frac{\partial^2 f}{\partial y^2} = \frac{\partial}{\partial y}(-\sin(x) \sin(y)) = -\sin(x) \cos(y)\);
- \(\frac{\partial^2 f}{\partial y \partial x} = \frac{\partial}{\partial y}(\cos(x) \cos(y)) = -\cos(x) \sin(y)\);
- \(\frac{\partial^2 f}{\partial x \partial y} = \frac{\partial}{\partial x}(-\sin(x) \sin(y)) = -\cos(x) \sin(y)\).
The resulting Hessian takes the form:
\[ \nabla^2 f(x,y) = \begin{bmatrix} -\sin(x) \cos(y) & -\cos(x) \sin(y) \\ -\cos(x) \sin(y) & -\sin(x) \cos(y) \end{bmatrix} \]Observe that the Hessian clearly depends on the point \((x, y)\).
Example 6.6 (Log-Determinant Function (Advanced Material)). This is an advanced example from matrix calculus, crucial in areas like Gaussian processes and information theory. The function is defined over the space of positive definite matrices (to be defined later in the text, but accept the “vagueness” of this example for now).
Let \(f: S_{++}^p \rightarrow \mathbb{R}\) be the log-determinant function, where \(S_{++}^p\) is the set of \(p \times p\) symmetric positive definite matrices, i.e., matrices that have all their eigenvalues strictly positive:
\[ f(\mathbf{X}) = \log(\det(\mathbf{X})) \]To compute the gradient, we use a result from matrix calculus. The gradient of \(f(\mathbf{X})\) with respect to the matrix \(\mathbf{X}\) is its inverse transpose. Since \(\mathbf{X}\) is symmetric, this is simply its inverse.
\[ \nabla_{\mathbf{X}} f(\mathbf{X}) = \mathbf{X}^{-1} \]Then, the Hessian of a function over matrices is a fourth-order tensor, which is complex to represent.
Advanced material: A more intuitive way to analyze its curvature is to compute the second-order directional derivative. We want to see how the function curves when moving from \(\mathbf{X}\) in the direction of another symmetric matrix \(\mathbf{V}\). The second-order term of the Taylor expansion reveals the action of the Hessian. The expansion is:
\[ f(\mathbf{X} + \mathbf{V}) \approx f(\mathbf{X}) + {\rm Tr}(\mathbf{X}^{-1}\mathbf{V}) - \frac{1}{2}{\rm Tr}((\mathbf{X}^{-1}\mathbf{V})^2) \]The quadratic term, \(-\frac{1}{2}{\rm Tr}((\mathbf{X}^{-1}\mathbf{V})^2)\), encapsulates the curvature. So \(-{\rm Tr}((\mathbf{X}^{-1}\mathbf{V})^2)\) is the second derivative of \(f\) in the direction \(\mathbf{V}\).
Let’s analyze the sign of this quadratic term. Write \(\mathbf{X} = \mathbf{Q}\boldsymbol{\Lambda}\mathbf{Q}^\top\) (Chapter 4) and \(\mathbf{X}^{-1/2} = \mathbf{Q}\boldsymbol{\Lambda}^{-1/2}\mathbf{Q}^\top\), where \(\boldsymbol{\Lambda}^{-1/2}\) holds the numbers \(1/\sqrt{\lambda_i}\); it is symmetric, and \(\mathbf{X}^{-1/2}\mathbf{X}^{-1/2} = \mathbf{X}^{-1}\). Let \(\mathbf{M} = \mathbf{X}^{-1/2}\mathbf{V}\mathbf{X}^{-1/2}\). Since \(\mathbf{X}^{-1/2}\) and \(\mathbf{V}\) are symmetric, \(\mathbf{M}\) is also symmetric and has real eigenvalues, \(\lambda_i(\mathbf{M})\). Using the cyclic property of the trace:
\[ \begin{aligned} {\rm Tr}((\mathbf{X}^{-1}\mathbf{V})^2) &= {\rm Tr}(\mathbf{X}^{-1}\mathbf{V}\mathbf{X}^{-1}\mathbf{V}) \\ &= {\rm Tr}(\mathbf{X}^{-1/2}\mathbf{X}^{-1/2}\mathbf{V}\mathbf{X}^{-1/2}\mathbf{X}^{-1/2}\mathbf{V}) \\ &= {\rm Tr}(\mathbf{X}^{-1/2}\mathbf{V}\mathbf{X}^{-1/2}\mathbf{X}^{-1/2}\mathbf{V}\mathbf{X}^{-1/2}) \\ &= {\rm Tr}(\mathbf{M}^2) \end{aligned} \]The second line writes each \(\mathbf{X}^{-1}\) as \(\mathbf{X}^{-1/2}\mathbf{X}^{-1/2}\); the third moves the first factor to the end, which the cyclic property allows. The trace of a matrix is the sum of its eigenvalues. Therefore:
\[ {\rm Tr}(\mathbf{M}^2) = \sum_{i=1}^p \lambda_i(\mathbf{M}^2) = \sum_{i=1}^p (\lambda_i(\mathbf{M}))^2 \ge 0 \]The term is a sum of squares, so it is always non-negative.
The quadratic term in the Taylor expansion is \(-\frac{1}{2} \sum (\lambda_i(\mathbf{M}))^2\), which is always less than or equal to zero. Since \(\mathbf{X}^{-1/2}\) is invertible, \(\mathbf{M} = \mathbf{0}\) only when \(\mathbf{V} = \mathbf{0}\). So the quadratic term is strictly negative in every direction \(\mathbf{V} \neq \mathbf{0}\): the function curves downwards in every direction. In the language of the convexity section, \(\log\det\) is concave (its negative is convex).
Before we move on, let us comment on the computational complexity of computing the Hessian, which is in addition to computing the gradient of a given function.
Example 6.7 (Complexity for Affine Function). The Hessian of an affine function is the zero matrix. The “computation” here is purely symbolic; we know the result is a \(p \times p\) zero matrix without performing any derivative calculations on the input data.
Example 6.8 (Complexity for Quadratic Function). Similar to the linear case, the Hessian of a general quadratic function \(f(\mathbf{x}) = \frac{1}{2}\mathbf{x}^\top \mathbf{Q} \mathbf{x} - \mathbf{b}^\top \mathbf{x}\) is simply the matrix \(\mathbf{Q}\). The Hessian is provided as part of the function’s definition. Therefore, no computations are needed to derive its value. The cost is \(O(1)\) to identify the matrix \(\mathbf{Q}\), with an associated memory cost of \(O(p^2)\) to store it. This is a critical property: for functions that are already quadratic, the curvature information is obtained for free. (Newton’s method, later in this chapter, solves these problems in a single step when \(\mathbf{Q}\) is Positive Definite, but for a different reason: its quadratic model of a quadratic function is exact.)
Example 6.9 (Complexity for Linear Regression). This is the first example where the Hessian computation is non-trivial and data-dependent. The Hessian is given by the formula \(\mathbf{H} = \mathbf{A}^\top\mathbf{A}\). This requires a matrix multiplication of a \(p \times n\) matrix (\(\mathbf{A}^\top\)) with an \(n \times p\) matrix (\(\mathbf{A}\)).
The resulting Hessian is a \(p \times p\) matrix. To compute each of the \(p^2\) entries in the Hessian, we must perform a dot product between two vectors of length \(n\) (a row of \(\mathbf{A}^\top\) and a column of \(\mathbf{A}\)). This dot product requires \(n\) multiplications and \(n-1\) additions, which is \(O(n)\) work.
The total computational complexity is therefore \(O(np^2)\). This cost is significant. It scales linearly with the number of data points but quadratically with the number of features. For datasets common in AI with thousands or millions of features (both \(n\) and \(p\) are large), computing the Hessian, even once, can be a major computational bottleneck.
Example 6.10 (Complexity for Logistic Regression). The Hessian for logistic regression, \(\mathbf{H} = \mathbf{X}^\top \mathbf{D} \mathbf{X}\), is even more expensive to compute within an iterative optimization algorithm because it must be re-calculated at every step. The breakdown is as follows:
First, we must compute the predictions \(\mathbf{h} = \sigma(\mathbf{X}\mathbf{w})\) for the current parameters \(\mathbf{w}\). This involves a matrix-vector product, costing \(O(np)\). We then calculate the \(n\) diagonal entries \(h_i(1-h_i)\), which costs \(O(n)\). We compute the product \(\mathbf{X}' = \mathbf{D}\mathbf{X}\). Since \(\mathbf{D}\) is diagonal, this is an element-wise scaling of the rows of \(\mathbf{X}\), costing \(O(np)\). Finally, we compute the product \(\mathbf{X}^\top\mathbf{X}'\), which, as in the linear regression case, costs \(O(np^2)\).
The total complexity is dominated by the final step, resulting in a cost of \(O(np^2)\) per iteration. The fact that this expensive computation must be performed at every single step of Newton’s method makes the pure second-order approach impractical for high-dimensional logistic regression.
Example 6.11 (Complexity for Log-Determinant Function (Advanced Material)). The log-determinant function acts on a matrix \(\mathbf{X} \in S_{++}^p\). Here, the Hessian is not a simple matrix but a fourth-order tensor whose action is more complex. However, even computing the gradient, \(\nabla f(\mathbf{X}) = \mathbf{X}^{-1}\), is computationally expensive. The standard algorithm for matrix inversion is Gaussian elimination, which has a complexity of \(O(p^3)\).
Any second-order method would require either forming and storing this tensor or, more practically, computing its product with a direction matrix (a Hessian-vector product). That product is \(-\mathbf{X}^{-1}\mathbf{V}\mathbf{X}^{-1}\), the change of the gradient \(\mathbf{X}^{-1}\) in the direction \(\mathbf{V}\). It needs the inverse and two matrix products, each costing \(O(p^3)\). Therefore, any computation involving the Hessian of the log-det function will have a complexity of at least \(O(p^3)\). This cubic scaling makes full second-order methods feasible only for problems where the matrix dimension \(p\) is relatively small (e.g., up to a few thousand).
6.3 Why the Hessian? Uncovering Function Geometry
To design more powerful optimization algorithms, we need to understand the local curvature: Does the function form a bowl, a dome, a flat valley, or a Pringles-like saddle? The Hessian matrix is the tool that unlocks this geometric insight. To build our intuition, let’s examine four simple quadratic functions, each with a distinct shape centered at the origin.
6.3.1 Four Illustrative Landscapes
Consider the following functions and their associated Hessian matrices. Since these functions are quadratic, their Hessians are constant and describe the curvature across the entire domain.
The Bowl: \(f_1(x_1, x_2) = x_1^2 + x_2^2\): This function describes a perfect circular paraboloid. From the origin, the function curves upwards in every possible direction. Its Hessian is:
\[ \nabla^2 f_1 = \begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix}. \]The Dome: \(f_2(x_1, x_2) = -x_1^2 - x_2^2\): This is an inverted paraboloid. The function curves downwards in every direction from the origin, forming a dome shape. Its Hessian is:
\[ \nabla^2 f_2 = \begin{bmatrix} -2 & 0 \\ 0 & -2 \end{bmatrix}. \]The Valley or Trough: \(f_3(x_1, x_2) = x_1^2\): This function depends only on \(x_1\). It forms a parabolic “trough” or valley. Along the \(x_1\)-axis, it curves upwards. However, along the \(x_2\)-axis, the function is completely flat; it has zero curvature. Its Hessian is:
\[ \nabla^2 f_3 = \begin{bmatrix} 2 & 0 \\ 0 & 0 \end{bmatrix}. \]The Saddle: \(f_4(x_1, x_2) = x_1^2 - x_2^2\): This function has the iconic shape of a saddle. Along the \(x_1\)-axis, it curves upwards, but along the \(x_2\)-axis, it curves downwards. This is a classic example of mixed curvature. Its Hessian is:
\[ \nabla^2 f_4 = \begin{bmatrix} 2 & 0 \\ 0 & -2 \end{bmatrix}. \]
See also Figure 6.1. These four shapes (upward curvature in all directions, downward in all, a mix of upward and flat, and a mix of upward and downward) represent the fundamental geometries that an optimization algorithm will encounter. The goal is to create a formal mathematical language to describe them.
6.3.2 From Intuition to Formalism: Definite Matrices
The mathematical property that captures this notion of curvature is the definiteness of the Hessian matrix. The definiteness of a symmetric matrix \(\mathbf{H}\) is determined by the sign of the quadratic form \(\mathbf{x}^\top\mathbf{H}\mathbf{x}\) for all possible non-zero directions \(\mathbf{x}\). This quadratic form can be thought of as a “curvature probe”: it measures the curvature of the function in the direction of the vector \(\mathbf{x}\) (strictly, for a unit vector \(\mathbf{x}\): a longer \(\mathbf{x}\) scales the number by \(\|\mathbf{x}\|_2^2\), which does not change its sign).
Definition 6.2 (Positive Definite Matrices). A symmetric \(\mathbf{H} \in \mathbb{R}^{p \times p}\) is:
- Positive Definite (\(\mathbf{H} \succ 0\)) if \(\mathbf{x}^\top\mathbf{H}\mathbf{x} > 0\) for all non-zero vectors \(\mathbf{x} \in \mathbb{R}^p\).
- Positive Semidefinite (\(\mathbf{H} \succeq 0\)) if \(\mathbf{x}^\top\mathbf{H}\mathbf{x} \ge 0\) for all \(\mathbf{x} \in \mathbb{R}^p\).
- Negative Definite (\(\mathbf{H} \prec 0\)) if \(\mathbf{x}^\top\mathbf{H}\mathbf{x} < 0\) for all non-zero vectors \(\mathbf{x} \in \mathbb{R}^p\).
- Negative Semidefinite (\(\mathbf{H} \preceq 0\)) if \(\mathbf{x}^\top\mathbf{H}\mathbf{x} \le 0\) for all \(\mathbf{x} \in \mathbb{R}^p\).
- Indefinite if it is none of the above (i.e., \(\mathbf{x}^\top\mathbf{H}\mathbf{x}\) can be both positive and negative).
6.3.3 The Eigenvalue Test: A Practical Tool for Curvature
Checking the sign of \(\mathbf{x}^\top\mathbf{H}\mathbf{x}\) for an infinite number of directions \(\mathbf{x}\) is impossible. Fortunately, the eigendecomposition of the Hessian provides a simple and powerful shortcut. The eigenvalues of a symmetric matrix give us the exact curvature along its principal axes (the eigenvectors). By simply checking the signs of these eigenvalues, we can determine the curvature in all directions.
Theorem 6.1 (Eigenvalue Test for Definiteness). A symmetric \(\mathbf{H}\) is:
- Positive Definite \(\iff\) all of its eigenvalues are strictly positive (\(\lambda_i > 0\)).
- Negative Definite \(\iff\) all of its eigenvalues are strictly negative (\(\lambda_i < 0\)).
- Positive Semidefinite \(\iff\) all of its eigenvalues are non-negative (\(\lambda_i \ge 0\)).
- Negative Semidefinite \(\iff\) all of its eigenvalues are non-positive (\(\lambda_i \le 0\)).
- Indefinite \(\iff\) it has both positive and negative eigenvalues.
Proof: By the Spectral Theorem (Chapter 4), \(\mathbf{H}\) has orthonormal eigenvectors \(\mathbf{q}_1, \dots, \mathbf{q}_p\), with eigenvalues \(\lambda_1, \dots, \lambda_p\). The proof of the Rayleigh Quotient theorem in Chapter 5 showed that, in the coordinates \(c_i\) defined by \(\mathbf{x} = \sum_i c_i\mathbf{q}_i\) (because the \(\mathbf{q}_i\) are orthonormal, \(c_i = \mathbf{q}_i^\top\mathbf{x}\)),
\[ \begin{aligned} \mathbf{x}^\top\mathbf{H}\mathbf{x} = \sum_{i=1}^p \lambda_i c_i^2 \quad \text{and} \quad \|\mathbf{x}\|_2^2 = \sum_{i=1}^p c_i^2 . \end{aligned} \]If every \(\lambda_i > 0\) and \(\mathbf{x} \neq \mathbf{0}\), then some \(c_i \neq 0\), so the sum is positive: \(\mathbf{H}\) is Positive Definite. If some \(\lambda_i \le 0\), the direction \(\mathbf{x} = \mathbf{q}_i\) gives \(\mathbf{q}_i^\top\mathbf{H}\mathbf{q}_i = \lambda_i \le 0\), so \(\mathbf{H}\) is not Positive Definite. For the Positive Semidefinite case, repeat both arguments with the inequalities relaxed: if every \(\lambda_i \ge 0\), the sum is at least \(0\); if some \(\lambda_i < 0\), the direction \(\mathbf{q}_i\) gives \(\mathbf{q}_i^\top\mathbf{H}\mathbf{q}_i = \lambda_i < 0\). The two Negative cases follow by applying these to \(-\mathbf{H}\), whose eigenvalues are \(-\lambda_i\). Finally, \(\mathbf{H}\) is Indefinite when it is neither Positive nor Negative Semidefinite, that is, by the cases above, when it has a negative eigenvalue and a positive one. \(\blacksquare\)
The same identity gives a bound that the convergence proofs later in this chapter rely on. Every \(\lambda_i\) lies between the smallest eigenvalue \(\lambda_{\min}\) and the largest \(\lambda_{\max}\), so
We call this the sandwich. In words: along a unit direction \(\mathbf{v}\), the curvature \(\mathbf{v}^\top\mathbf{H}\mathbf{v}\) is a weighted average of the eigenvalues (with weights \(c_i^2\) that add up to \(1\)), so it lies between the smallest and the largest.
Example 6.12 (Revisiting the Landscapes with Eigenvalues). Let’s now formally classify the Hessians from our four examples using the eigenvalue test. Since all four Hessians are diagonal matrices, their eigenvalues are simply their diagonal entries.
The Bowl: \(f_1(x_1, x_2) = x_1^2 + x_2^2\): Its Hessian is: \(\nabla^2 f_1 = \left[\begin{smallmatrix} 2 & 0 \\ 0 & 2 \end{smallmatrix}\right]\). The eigenvalues: \(\lambda_1 = 2\), \(\lambda_2 = 2\). Then, both eigenvalues are strictly positive. According to the test, the Hessian is positive definite. This confirms our geometric intuition of a bowl that curves upwards in all directions.
The Dome: \(f_2(x_1, x_2) = -x_1^2 - x_2^2\): Its Hessian is: \(\nabla^2 f_2 = \left[\begin{smallmatrix} -2 & 0 \\ 0 & -2 \end{smallmatrix}\right]\). The eigenvalues: \(\lambda_1 = -2\), \(\lambda_2 = -2\). Then, both eigenvalues are strictly negative. The Hessian is negative definite. This corresponds perfectly to the shape of a dome, curving downwards in all directions.
The Valley: \(f_3(x_1, x_2) = x_1^2\): Its Hessian is: \(\nabla^2 f_3 = \left[\begin{smallmatrix} 2 & 0 \\ 0 & 0 \end{smallmatrix}\right]\). The eigenvalues: \(\lambda_1 = 2\), \(\lambda_2 = 0\). Then, the eigenvalues are non-negative (one is positive, one is zero). The Hessian is positive semidefinite. This captures the valley shape: positive curvature in the direction of the first eigenvector and zero curvature (flatness) in the direction of the second.
The Saddle: \(f_4(x_1, x_2) = x_1^2 - x_2^2\): Its Hessian is: \(\nabla^2 f_4 = \left[\begin{smallmatrix} 2 & 0 \\ 0 & -2 \end{smallmatrix}\right]\). The eigenvalues: \(\lambda_1 = 2\), \(\lambda_2 = -2\). Then, there is a mix of positive and negative eigenvalues. The Hessian is indefinite. This is the mathematical signature of a saddle, confirming its mixed curvature: upward along one principal axis and downward along another.
6.4 Analyzing the Definiteness of Hessians in Practice
We have established a powerful connection: the eigenvalues of the Hessian matrix reveal the local geometry of a function. Now, we return to our key examples and ask a practical question: for each function, what is its definiteness, and what is the computational cost of determining it? Answering this reveals why analyzing curvature, while insightful, is a luxury we often cannot afford in large-scale AI.
Example 6.13 (Affine Function). We start with some reminders:
- Hessian: \(\nabla^2 f(\mathbf{x}) = \mathbf{0}\)
- Definiteness Analysis: The analysis is trivial. The eigenvalues of the zero matrix are all zero. According to our test, a matrix whose eigenvalues are all non-negative is positive semidefinite (PSD), and one whose eigenvalues are all non-positive is negative semidefinite (NSD). The zero matrix is uniquely both.
Example 6.14 (General Quadratic Function). We start with some reminders:
- Hessian: \(\nabla^2 f(\mathbf{x}) = \mathbf{Q}\)
- Definiteness Analysis: The Hessian is the constant matrix \(\mathbf{Q}\). Therefore, the function’s curvature is entirely determined by the definiteness of \(\mathbf{Q}\). To determine if \(\mathbf{Q}\) is PD, PSD, etc., we must analyze its eigenvalues. In general there is no symbolic shortcut, and a numerical check costs about as much as an eigendecomposition.
- Computational Cost: The cost of determining the definiteness of a general \(p \times p\) matrix \(\mathbf{Q}\) is the cost of performing an eigendecomposition, which is typically \(O(p^3)\) using standard numerical algorithms. While the Hessian is “free” (it’s given), analyzing it is computationally expensive, scaling cubically with the number of dimensions.
Example 6.15 (Linear Regression). We start with some reminders:
- Hessian: \(\nabla^2 f(\mathbf{x}) = \mathbf{A}^\top\mathbf{A}\)
- Definiteness Analysis: Here, we have a choice. We could compute the eigenvalues of \(\mathbf{A}^\top\mathbf{A}\), which would cost \(O(p^3)\). However, a much cheaper symbolic trick exists. We can analyze the quadratic form directly:
\[ \mathbf{v}^\top (\mathbf{A}^\top\mathbf{A}) \mathbf{v} = (\mathbf{A}\mathbf{v})^\top(\mathbf{A}\mathbf{v}) = \|\mathbf{A}\mathbf{v}\|_2^2 \]
Since the squared norm of any vector is always greater than or equal to zero, we have \(\mathbf{v}^\top \mathbf{H} \mathbf{v} \ge 0\) for all \(\mathbf{v}\). This proves that the Hessian is always Positive Semidefinite (PSD) without computing a single eigenvalue. It becomes Positive Definite (PD) if and only if \(\mathbf{A}\mathbf{v} \neq \mathbf{0}\) for all \(\mathbf{v} \neq \mathbf{0}\), which is true exactly when the columns of the data matrix \(\mathbf{A}\) are linearly independent.
- Computational Cost: The symbolic proof of PSD is \(O(1)\). However, to numerically check if it is strictly PD for a given dataset, one would either need to check the rank of \(\mathbf{A}\) (costing \(O(np^2)\) via SVD) or compute the eigenvalues of the Hessian. The full numerical analysis pipeline would be: 1. Form the Hessian: \(O(np^2)\). 2. Compute its eigenvalues: \(O(p^3)\). The total cost is \(O(np^2 + p^3)\).
Example 6.16 (Logistic Regression). We start with some reminders:
- Hessian: \(\nabla^2 f(\mathbf{w}) = \mathbf{X}^\top \mathbf{D} \mathbf{X}\), where \(\mathbf{D}\) is a diagonal matrix with positive entries \(h_i(1-h_i)\).
- Definiteness Analysis: Like linear regression, we can use a symbolic argument. The Hessian is a sum of weighted rank-one matrices: \(\sum_{i=1}^n \underbrace{h_i(1-h_i)}_{>0} \underbrace{\mathbf{x}_i\mathbf{x}_i^\top}_{\text{PSD}}\). Since a sum of PSD matrices weighted by positive scalars is also PSD, the Hessian is always Positive Semidefinite (PSD).
- Computational Cost: Again, the symbolic proof is \(O(1)\). The critical difference from linear regression is that the Hessian depends on the current parameters \(\mathbf{w}\) (via the matrix \(\mathbf{D}\)). Therefore, any numerical analysis of its definiteness must be performed at every iteration of an optimization algorithm. The cost to do so would be \(O(np^2 + p^3)\) per iteration, making any method that relies on this analysis extremely expensive for high-dimensional problems.
Example 6.17 (Trigonometric Function). We start with some reminders:
- Hessian: \(\nabla^2 f(x,y) = \left[\begin{smallmatrix} -\sin(x) \cos(y) & -\cos(x) \sin(y) \\ -\cos(x) \sin(y) & -\sin(x) \cos(y) \end{smallmatrix}\right]\)
- Definiteness Analysis: The definiteness depends on the point \((x,y)\). To determine it at a specific point, we would plug in the values and compute the eigenvalues of the resulting \(2 \times 2\) matrix. For a \(2 \times 2\) matrix, this is extremely cheap. We can check the determinant \(\Delta = \lambda_1\lambda_2\) and trace \(\tau = \lambda_1+\lambda_2\). Both come straight from the entries, with no eigenvalues: for \(\left[\begin{smallmatrix} a & b \\ b & c \end{smallmatrix}\right]\), \(\Delta = ac - b^2\) and \(\tau = a + c\).
- If \(\Delta > 0\) and \(\tau > 0\), it is PD.
- If \(\Delta > 0\) and \(\tau < 0\), it is ND.
- If \(\Delta < 0\), it is Indefinite.
- If \(\Delta = 0\), at least one eigenvalue is zero and the other equals \(\tau\): it is Positive Semidefinite if \(\tau \ge 0\) and Negative Semidefinite if \(\tau \le 0\).
6.4.1 Summary of Analysis Costs
Table 6.1 summarizes the cost of determining the definiteness of the Hessian in our examples.
| Function | Hessian Form | Definiteness | Method of Analysis | Cost to Analyze Definiteness |
|---|---|---|---|---|
| Linear | \(\mathbf{0}\) | PSD & NSD | Symbolic | \(\mathcal{O}(1)\) |
| Quadratic | \(\mathbf{Q}\) | Depends on \(\mathbf{Q}\) | Eigendecomposition | \(\mathcal{O}(p^3)\) |
| Linear Reg. | \(\mathbf{A}^\top\mathbf{A}\) | Always PSD | Symbolic (Quadratic Form) | \(\mathcal{O}(1)\) proof, \(\mathcal{O}(np^2+p^3)\) numerical |
| Logistic Reg. | \(\mathbf{X}^\top\mathbf{D}\mathbf{X}\) | Always PSD | Symbolic (Sum of PSD) | \(\mathcal{O}(1)\) proof, \(\mathcal{O}(np^2+p^3)\) per iter. |
| Trigonometric | \(2 \times 2\) Matrix | Point- dependent | Eigenvalues (small) | \(\mathcal{O}(1)\) |
This analysis makes it clear why second-order information is used sparingly in large-scale AI. While the theoretical insight from the Hessian is important, the cost to compute and analyze it (especially at every iteration) is often prohibitive. This motivates the development of algorithms that can approximate this curvature information cheaply, which we explore at the end of this chapter.
6.5 Classifying Critical Points: The Role of the Hessian
In our study of gradient descent, we established a fundamental rule: to find a potential minimum, we search for points where the gradient is zero. This is the first-order necessary condition. A point where the gradient vanishes is “flat,” suggesting it could be a valley floor, a mountain peak, or a flat pass between mountains. However, the gradient alone cannot tell these apart. To classify these interesting points, we need to examine the local curvature, which brings us to the second-order conditions provided by the Hessian.
6.5.1 From Calculus 101 to Higher Dimensions
Let’s first revisit single-variable calculus. This simple, mechanical procedure is the exact blueprint for how we analyze functions in higher dimensions.
Example 6.18 (The Parabola \(f(x) = x^2 - 4x + 5\)). Let’s analyze the point \(x=2\).
- First Derivative (Slope): \(f'(x) = 2x - 4\). At our point, \(f'(2) = 2(2) - 4 = 0\). The function is flat at \(x=2\).
- Second Derivative (Curvature): \(f''(x) = 2\). At our point, \(f''(2) = 2 > 0\). The function is curving upwards.
- Conclusion: At \(x=2\), the function has a zero slope and is curving upwards. Any small step away from this point, either left or right, will lead to an increase in the function’s value. This is a local minimum.
Example 6.19 (The Inverted Parabola \(g(x) = -x^2 + 4x - 3\)). Let’s analyze the point \(x=2\).
- First Derivative (Slope): \(g'(x) = -2x + 4\). At our point, \(g'(2) = -2(2) + 4 = 0\). The function is flat at \(x=2\).
- Second Derivative (Curvature): \(g''(x) = -2\). At our point, \(g''(2) = -2 < 0\). The function is curving downwards.
- Conclusion: At \(x=2\), the function has a zero slope and is curving downwards. Any small step away will lead to a decrease in the function’s value. This is a local maximum.
6.5.2 The Bridge to Multivariate Functions
This simple procedure extends directly to functions of multiple variables. The key is to establish the correct analogies:
- The condition of zero slope, \(f'(x) = 0\), becomes the condition of zero gradient, \(\nabla f(\mathbf{x}) = \mathbf{0}\).
- The curvature, given by the second derivative \(f''(x)\), is now described by the Hessian matrix, \(\nabla^2 f(\mathbf{x})\).
- The sign of the second derivative, which tells us if the curve is bending up or down, becomes the definiteness of the Hessian, which tells us the curvature in all directions simultaneously.
The connection is deeper than an analogy. For a univariate function, the Hessian is a \(1 \times 1\) matrix: \(\nabla^2 f(x) = [f''(x)]\). Its only eigenvalue is the value \(f''(x)\) itself. The condition \(f''(x) > 0\) is a literal check for whether the Hessian’s only eigenvalue is positive, making it a Positive Definite matrix. The concepts are identical.
6.5.3 Necessary and Sufficient Conditions
With this foundation, we can now formally define the types of points we are looking for and the conditions that identify them.
Definition 6.3 (Definitions of Critical Points). Consider the following:
- Stationary (or Critical) Point: A point \(\mathbf{x}^\star\) is a stationary point if the first-order necessary condition is met: \(\nabla f(\mathbf{x}^\star) = \mathbf{0}\).
- Local Minimum: A stationary point \(\mathbf{x}^\star\) is a local minimum if \(f(\mathbf{x}^\star) \le f(\mathbf{x})\) for all \(\mathbf{x}\) in a neighborhood around \(\mathbf{x}^\star\). It is a strict local minimum if \(f(\mathbf{x}^\star) < f(\mathbf{x})\) for all \(\mathbf{x} \neq \mathbf{x}^\star\) in that neighborhood.
- Local Maximum: A stationary point \(\mathbf{x}^\star\) is a local maximum if \(f(\mathbf{x}^\star) \ge f(\mathbf{x})\) for all \(\mathbf{x}\) in a neighborhood. It is a strict local maximum if \(f(\mathbf{x}^\star) > f(\mathbf{x})\) for all \(\mathbf{x} \neq \mathbf{x}^\star\) in that neighborhood.
- Saddle Point: A stationary point that is neither a local minimum nor a local maximum.
- Global Minimum: A point \(\mathbf{x}^\star\) is a global minimum if \(f(\mathbf{x}^\star) \le f(\mathbf{x})\) for every \(\mathbf{x}\) in the domain of \(f\).
The Hessian often tells these cases apart, but not always.
Theorem 6.2 (Classifying Stationary Points using the Hessian). Let \(\mathbf{x}^\star\) be a stationary point of a twice-differentiable function \(f\).
- If the Hessian \(\nabla^2 f(\mathbf{x}^\star)\) is Positive Definite (all \(\lambda_i > 0\)), then \(\mathbf{x}^\star\) is a strict local minimum.
- If the Hessian \(\nabla^2 f(\mathbf{x}^\star)\) is Negative Definite (all \(\lambda_i < 0\)), then \(\mathbf{x}^\star\) is a strict local maximum.
- If the Hessian \(\nabla^2 f(\mathbf{x}^\star)\) is Indefinite (has both positive and negative eigenvalues), then \(\mathbf{x}^\star\) is a saddle point.
- If the Hessian is semidefinite but not definite (all \(\lambda_i \ge 0\) or all \(\lambda_i \le 0\), with at least one \(\lambda_i = 0\)), the test is inconclusive: the Hessian alone cannot classify \(\mathbf{x}^\star\).
Why can the test fail? A zero eigenvalue means zero curvature in some direction. Then the answer depends on the higher-order terms of \(f\), which the Hessian does not capture. In one dimension, \(x^4\), \(-x^4\) and \(x^3\) all have \(f'(0) = f''(0) = 0\), yet \(x = 0\) is a minimum, a maximum and an inflection point, respectively. An inflection point is neither a minimum nor a maximum, so Definition 6.3 calls it a saddle point.
Going the other way gives a necessary condition, one that every local minimum satisfies: if \(\mathbf{x}^\star\) is a local minimum, then \(\nabla^2 f(\mathbf{x}^\star)\) is Positive Semidefinite. So a Positive Semidefinite Hessian is necessary for a local minimum, but not sufficient. A Positive Definite Hessian is sufficient for a strict local minimum, but not necessary: \(f(x) = x^4\) has a strict minimum at \(x = 0\) although \(f''(0) = 0\). For a local maximum, replace Positive by Negative. For example, a Hessian with a positive eigenvalue rules out a local maximum. Do not confuse this with the convexity section later in this chapter, where the Hessian is Positive Semidefinite at every point of a convex domain such as \(\mathbb{R}^p\), not only at \(\mathbf{x}^\star\): then \(f\) is convex and every stationary point is a global minimum.
6.5.4 Examples of Critical Point Classification
Let’s apply this theorem to classify the stationary point at the origin (\(\mathbf{x}^\star = \mathbf{0}\)) for several functions.
Example 6.20 (Strict Local Minimum (PD Hessian)). Consider the following case:
- Function: \(f(\mathbf{x}) = x_1^2 + 2x_2^2\)
- Gradient: \(\nabla f(\mathbf{x}) = [2x_1, 4x_2]^\top\). At the origin, \(\nabla f(\mathbf{0}) = \mathbf{0}\).
- Hessian: \(\nabla^2 f(\mathbf{x}) = \left[\begin{smallmatrix} 2 & 0 \\ 0 & 4 \end{smallmatrix}\right]\).
- Eigenvalues: \(\lambda_1 = 2\), \(\lambda_2 = 4\).
- Conclusion: Since all eigenvalues are strictly positive, the Hessian is Positive Definite. The origin is a strict local minimum. The function forms an elliptical bowl.
Example 6.21 (Strict Local Maximum (ND Hessian)). Consider the following case:
- Function: \(f(\mathbf{x}) = -x_1^2 - 2x_2^2\)
- Gradient: \(\nabla f(\mathbf{x}) = [-2x_1, -4x_2]^\top\). At the origin, \(\nabla f(\mathbf{0}) = \mathbf{0}\).
- Hessian: \(\nabla^2 f(\mathbf{x}) = \left[\begin{smallmatrix} -2 & 0 \\ 0 & -4 \end{smallmatrix}\right]\).
- Eigenvalues: \(\lambda_1 = -2\), \(\lambda_2 = -4\).
- Conclusion: Since all eigenvalues are strictly negative, the Hessian is Negative Definite. The origin is a strict local maximum.
Example 6.22 (Saddle Point (Indefinite Hessian)). Consider the following case:
- Function: \(f(\mathbf{x}) = x_1^2 - 2x_2^2\)
- Gradient: \(\nabla f(\mathbf{x}) = [2x_1, -4x_2]^\top\). At the origin, \(\nabla f(\mathbf{0}) = \mathbf{0}\).
- Hessian: \(\nabla^2 f(\mathbf{x}) = \left[\begin{smallmatrix} 2 & 0 \\ 0 & -4 \end{smallmatrix}\right]\).
- Eigenvalues: \(\lambda_1 = 2\), \(\lambda_2 = -4\).
- Conclusion: The eigenvalues have mixed signs. The Hessian is Indefinite. The origin is a saddle point.
Example 6.23 (Local Minimum with a Flat Direction). Consider the following case:
- Function: \(f(\mathbf{x}) = 5x_1^2\)
- Gradient: \(\nabla f(\mathbf{x}) = [10x_1, 0]^\top\). At the origin, \(\nabla f(\mathbf{0}) = \mathbf{0}\).
- Hessian: \(\nabla^2 f(\mathbf{x}) = \left[\begin{smallmatrix} 10 & 0 \\ 0 & 0 \end{smallmatrix}\right]\).
- Eigenvalues: \(\lambda_1 = 10\), \(\lambda_2 = 0\).
- Conclusion: The eigenvalues are non-negative, but one is zero. The Hessian is Positive Semidefinite, so the theorem is inconclusive and we look at \(f\) itself. Since \(f(\mathbf{x}) = 5x_1^2 \ge 0 = f(\mathbf{0})\) for every \(\mathbf{x}\), the origin is a local minimum, but it is not strict. Geometrically, the function is a parabolic valley. We can move along the \(x_2\)-axis (the eigenvector corresponding to \(\lambda_2=0\)) without changing the function value from its minimum of 0.
Example 6.24 (Same Hessian, Different Answers). Each function below has a stationary point at the origin with the same Hessian as in the previous example, \(\nabla^2 f(\mathbf{0}) = \left[\begin{smallmatrix} 10 & 0 \\ 0 & 0 \end{smallmatrix}\right]\).
- \(f(\mathbf{x}) = 5x_1^2 + x_2^4\): \(f(\mathbf{x}) > 0 = f(\mathbf{0})\) for every \(\mathbf{x} \neq \mathbf{0}\), so the origin is a strict local minimum.
- \(f(\mathbf{x}) = 5x_1^2 - x_2^4\): \(f(t, 0) = 5t^2 > 0\) but \(f(0, t) = -t^4 < 0\) for every \(t \neq 0\), so the origin is a saddle point.
- \(f(\mathbf{x}) = 5x_1^2 + x_2^3\): \(f(t, 0) = 5t^2 > 0\) for every \(t \neq 0\) but \(f(0, t) = t^3 < 0\) for every \(t < 0\), so the origin is a saddle point.
Together with \(f(\mathbf{x}) = 5x_1^2\) from the previous example, one Positive Semidefinite Hessian fits a strict minimum, a non-strict minimum and two saddle points. The terms \(x_2^4\), \(-x_2^4\) and \(x_2^3\) make the difference, and the Hessian does not see them.
6.6 From Landscape Analysis to Algorithmic Design
Thus far, the Hessian has been an “object” for analysis. By inspecting its eigenvalues, we have learned to characterize the landscape of a function; identifying bowls, domes, and saddles. This is a powerful descriptive capability, but it is not yet algorithmic. The true goal is to use this deeper understanding of curvature to build an optimization algorithm that is more intelligent and efficient than the “myopic” strategy of gradient descent.
Gradient descent feels the slope of the hill and takes a small step in the steepest downhill direction. A second-order method does more: it measures the curvature of the hill in a small area, fits a quadratic model to that area, and, when the model is a bowl, takes a single, decisive leap directly to the bottom of that bowl. This section formalizes this powerful idea, developing it in parallel with the first-order logic of gradient descent.
6.6.1 The Second-Order Taylor Approximation: A Quadratic Model
While gradient descent is often built upon a linear local model of the function, second-order methods are built upon a more accurate quadratic local model. This is formalized by the Second-Order Taylor Approximation.
If we are at a point \(\mathbf{x}_t\) and are considering a step \(\boldsymbol{\delta}\) to a new point \(\mathbf{x}_t + \boldsymbol{\delta}\), we can approximate the new function value using not only the gradient but also the Hessian:
\[ f(\mathbf{x}_t + \boldsymbol{\delta}) \approx f(\mathbf{x}_t) + \langle \nabla f(\mathbf{x}_t), \boldsymbol{\delta} \rangle + \frac{1}{2} \boldsymbol{\delta}^\top \nabla^2 f(\mathbf{x}_t) \boldsymbol{\delta}. \]This is our local quadratic model of the function. It is a richer approximation than the tangent hyperplane used by gradient descent. Our goal is no longer just to find a direction of descent, but to find the exact step \(\boldsymbol{\delta}\) that minimizes this entire quadratic approximation (when it has a minimum; see below).
6.6.2 Deriving a Different Step: The Birth of Newton’s Method
We want to solve the following local optimization problem for the step \(\boldsymbol{\delta}\):
\[ \min_{\boldsymbol{\delta}} \left( f(\mathbf{x}_t) + \langle \nabla f(\mathbf{x}_t), \boldsymbol{\delta} \rangle + \frac{1}{2} \boldsymbol{\delta}^\top \mathbf{H}_t \boldsymbol{\delta} \right) \]where \(\mathbf{H}_t = \nabla^2 f(\mathbf{x}_t)\) is the Hessian at the current point.
Since \(f(\mathbf{x}_t)\) is a constant, we only need to minimize the terms involving \(\boldsymbol{\delta}\). This is a simple quadratic function of the step vector \(\boldsymbol{\delta}\). Its Hessian is \(\mathbf{H}_t\). Assume for now that \(\mathbf{H}_t\) is Positive Definite, so the model is a bowl. Then its minimum is where its gradient is zero: if \(\boldsymbol{\delta}^\star\) makes the gradient zero, any other step \(\boldsymbol{\delta}^\star + \mathbf{e}\) raises the model by \(\frac{1}{2}\mathbf{e}^\top\mathbf{H}_t\mathbf{e} > 0\). We therefore take the gradient of the approximation with respect to \(\boldsymbol{\delta}\) and set it to the zero vector:
\[ \nabla_{\boldsymbol{\delta}} \left( \langle \nabla f(\mathbf{x}_t), \boldsymbol{\delta} \rangle + \frac{1}{2} \boldsymbol{\delta}^\top \mathbf{H}_t \boldsymbol{\delta} \right) = \mathbf{0} \]Using our rules for vector calculus:
- The gradient of the linear term \(\langle \nabla f(\mathbf{x}_t), \boldsymbol{\delta} \rangle\) with respect to \(\boldsymbol{\delta}\) is \(\nabla f(\mathbf{x}_t)\).
- The gradient of the quadratic term \(\frac{1}{2} \boldsymbol{\delta}^\top \mathbf{H}_t \boldsymbol{\delta}\) with respect to \(\boldsymbol{\delta}\) is \(\mathbf{H}_t \boldsymbol{\delta}\).
This gives us the equation:
\[ \nabla f(\mathbf{x}_t) + \mathbf{H}_t \boldsymbol{\delta} = \mathbf{0} \]Solving for the step \(\boldsymbol{\delta}\), which we call the Newton step, requires solving this system of linear equations:
\[ \mathbf{H}_t \boldsymbol{\delta} = -\nabla f(\mathbf{x}_t) \]If the Hessian \(\mathbf{H}_t\) is invertible, we can write the solution as:
\[ \boldsymbol{\delta} = - (\mathbf{H}_t)^{-1} \nabla f(\mathbf{x}_t) \]If \(\mathbf{H}_t\) is not Positive Definite, the model has no unique minimum. When \(\mathbf{H}_t\) is invertible, the formula still gives a point where the model’s gradient is zero, but that point can be a saddle point or a maximum of the model (see Properties and Practicalities below).
This gives rise to Newton’s method. The update rule is a direct leap based on this optimal step.
Definition 6.4 (Newton’s Method). Given a twice-differentiable function \(f(\mathbf{x})\) and a starting point \(\mathbf{x}_0\), Newton’s method generates a sequence of vectors via the update rule, as long as the Hessian \(\nabla^2 f(\mathbf{x}_t)\) is invertible (if it is not, the step is not defined; see Exercise M5):
\[ \mathbf{x}_{t+1} = \mathbf{x}_t - (\nabla^2 f(\mathbf{x}_t))^{-1} \nabla f(\mathbf{x}_t) \]Often, a learning rate \(\eta\) is included, yielding
\[ \begin{aligned} \mathbf{x}_{t+1} = \mathbf{x}_t - \eta(\mathbf{H}_t)^{-1} \nabla f(\mathbf{x}_t). \end{aligned} \]For the “pure” Newton’s method, we assume \(\eta=1\).
Notice the structure: the step is still based on the negative gradient, but it is first “preconditioned” or rescaled by the inverse of the Hessian. This term corrects the gradient’s direction based on the landscape’s curvature, making it a much more informed and powerful update.
6.6.3 Newton’s Method in Action: The One-Step Solution for Quadratics
Let’s revisit the simple quadratic example from our gradient descent discussion, but this time applying Newton’s method.
Example 6.25 (Univariate Quadratic). Consider minimizing \(f(x) = 3x^2 + 4x - 2\). The first derivative is: \(f'(x) = 6x + 4\). The second derivative (Hessian) is: \(f''(x) = 6\). Then, Newton’s update rule becomes \(x_{t+1} = x_t - \frac{f'(x_t)}{f''(x_t)}\).
\[ x_{t+1} = x_t - \frac{6x_t + 4}{6} \]Let’s start from the same guess, \(x_0 = 2\).
\[ x_1 = 2 - \frac{6(2) + 4}{6} = 2 - \frac{16}{6} = 2 - \frac{8}{3} = -\frac{2}{3} \approx -0.667. \]In a single step, Newton’s method jumped directly to the exact analytical minimum.
Why did this happen? Newton’s method works by minimizing a local quadratic model of the function. For a quadratic function, its quadratic model is not an approximation; it is the function itself. Here \(f'' = 6 > 0\), so the model is a bowl. Therefore, minimizing the model is equivalent to minimizing the actual function, and the algorithm finds the global solution in one iteration.
This incredible property extends to the multivariate case.
Example 6.26 (Newton’s Method for Least-Squares). The least-squares objective is a multivariate quadratic.
- Gradient: \(\nabla f(\mathbf{x}) = \mathbf{A}^\top(\mathbf{A}\mathbf{x} - \mathbf{y})\)
- Hessian: \(\nabla^2 f(\mathbf{x}) = \mathbf{A}^\top\mathbf{A}\)
Let’s write down the update rule for an arbitrary starting point \(\mathbf{x}_0\):
\[ \mathbf{x}_1 = \mathbf{x}_0 - (\mathbf{A}^\top\mathbf{A})^{-1} \left( \mathbf{A}^\top(\mathbf{A}\mathbf{x}_0 - \mathbf{y}) \right) \]Distributing the terms:
\[ \begin{aligned} \mathbf{x}_1 &= \mathbf{x}_0 - (\mathbf{A}^\top\mathbf{A})^{-1}(\mathbf{A}^\top\mathbf{A})\mathbf{x}_0 + (\mathbf{A}^\top\mathbf{A})^{-1}(\mathbf{A}^\top\mathbf{y}) \\ &= \mathbf{x}_0 - \mathbf{I}\mathbf{x}_0 + (\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top\mathbf{y} \\ &= (\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top\mathbf{y} \end{aligned} \]The result of the first step is \((\mathbf{A}^\top\mathbf{A})^{-1}\mathbf{A}^\top\mathbf{y}\), which is the exact, closed-form solution for linear regression: the solution of the normal equations \(\mathbf{A}^\top\mathbf{A}\mathbf{x} = \mathbf{A}^\top\mathbf{y}\) (it needs independent columns in \(\mathbf{A}\), so that \(\mathbf{A}^\top\mathbf{A}\) is invertible). Newton’s method for least-squares is not an iterative approximation; it is an algorithm that computes the analytical solution in a single step.
Newton against gradient descent
Newton's method and gradient descent with the safe step 1/L, on a least-squares problem and on logistic regression (100 data points, 3 features), each for up to 1000 steps.
1rng = np.random.default_rng(3)
2A = rng.normal(size=(100, 3))
3w_true = np.array([1.0, -2.0, 0.5])
4# least-squares targets, then labels for logistic regression
5y_ls = A @ w_true + 0.1 * rng.normal(size=100)
6y_lr = np.where(A @ w_true + 0.5 * rng.normal(size=100) > 0, 1.0, -1.0)
7sig = lambda u: 1 / (1 + np.exp(-u))
8problems = {"least squares": (lambda x: A.T @ (A @ x - y_ls), lambda x: A.T @ A),
9 "logistic": (lambda x: -A.T @ (y_lr * sig(-y_lr * (A @ x))),
10 lambda x: (A.T * (sig(A @ x) * (1 - sig(A @ x)))) @ A)}
11runs = {}
12for name, (grad, hess) in problems.items():
13 L = np.linalg.eigvalsh(hess(np.zeros(3))).max() # the largest curvature
14 for method in ("Newton", "gradient descent"):
15 x, norms = np.zeros(3), []
16 for t in range(1000):
17 norms.append(np.linalg.norm(grad(x)))
18 if method == "Newton":
19 step = np.linalg.solve(hess(x), grad(x))
20 else:
21 step = grad(x) / L
22 x = x - step
23 runs[name + ", " + method] = norms
24 k = next((t for t, v in enumerate(norms) if v < 1e-8), None)
25 if k is None:
26 done = f"still {norms[-1]:.2g} after 1000 steps"
27 else:
28 done = f"below 1e-8 after {k} step" + "s" * (k != 1)
29 print(f"{name:13} {method:16}: gradient {done}")
least squares Newton : gradient below 1e-8 after 1 step least squares gradient descent: gradient below 1e-8 after 21 steps logistic Newton : gradient below 1e-8 after 9 steps logistic gradient descent: gradient still 0.019 after 1000 steps
The size of the gradient against step, on a log scale. On least squares, Newton's method lands on the answer in one step, as Example 6.26 shows, and gradient descent needs 21 steps. On logistic regression, Newton needs 9 steps, and in its last steps the number of correct digits roughly doubles each time. Gradient descent is still at 0.019 after 1000 steps: at the answer, the flattest curvature is about 380 times smaller than L, so the step 1/L is tiny there.
Recorded on an Apple M1 Max laptop, macOS 26.6.2, Python 3.13.7, NumPy 2.4.6, 2026-12-01.
6.6.4 The Cost of Power: A Complexity Showdown
Newton’s method converges dramatically faster than gradient descent; in fact, for the problems where we could analyze GD’s convergence, Newton’s method converged instantly. Why, then, is gradient descent the workhorse of modern AI and not Newton’s method? The answer lies in the computational complexity of each step.
Let’s assume our problem has \(n\) data points and \(p\) features or parameters. Cost of one gradient descent step. The only major computation is the gradient. For least-squares, \(\nabla f(\mathbf{x}) = \mathbf{A}^\top(\mathbf{A}\mathbf{x} - \mathbf{y})\). This involves a matrix-vector product (\(\mathbf{Ax}\), costing \(O(np)\)) and another matrix-vector product (\(\mathbf{A}^\top\) times the result, also \(O(np)\)).
Cost of one Newton step. The computation here is far more involved and breaks down into three stages:
- Compute the Gradient: \(\nabla f(\mathbf{x})\). Same as above, this costs \(O(np)\).
- Compute the Hessian: \(\nabla^2 f(\mathbf{x}) = \mathbf{A}^\top\mathbf{A}\). This is a matrix-matrix multiplication of a \(p \times n\) matrix with an \(n \times p\) matrix. This costs \(O(np^2)\).
- Invert the Hessian (or Solve the System): We need to find the step \(\boldsymbol{\delta}\) by solving the \(p \times p\) linear system \(\mathbf{H}\boldsymbol{\delta} = -\nabla f\). Using standard methods like Gaussian elimination, this costs \(O(p^3)\).
So, overall per step we have \(O(np + np^2 + p^3)\). The difference could be huge: While a single gradient descent step is cheap, a single Newton step requires forming a potentially massive Hessian and then solving a large linear system, an operation that scales cubically with the number of features.
| Algorithm | Convergence Speed (Iterations) | Cost per Iteration | Total Cost (Roughly) |
|---|---|---|---|
| Gradient Descent | Many (Linear convergence; Theorem 6.11) | Cheap: \(\mathcal{O}(np)\) | \(T \times \mathcal{O}(np)\) |
| Newton’s Method | Very Few (Quadratic convergence) | Expensive: \(\mathcal{O}(np^2 + p^3)\) | \(1 \times \mathcal{O}(np^2 + p^3)\) for quadratics |
In modern AI, the number of parameters \(p\) can be in the millions. The \(O(p^2)\) term required to even store the Hessian, let alone the \(O(p^3)\) term to invert it, makes the pure Newton’s method completely computationally infeasible. This is the fundamental reason why first-order methods like gradient descent and its variants (Stochastic Gradient Descent, Adam, etc.) are the dominant paradigm in training large-scale models. They trade theoretical convergence speed for massive gains in per-iteration efficiency.
6.6.5 Properties and Practicalities
Newton’s method is the gold standard for optimization in terms of speed, but this speed comes at a significant cost.
- Pros: Fast Convergence. Near a minimum where the Hessian is Positive Definite, Newton’s method exhibits quadratic convergence (for smooth functions, such as those with three continuous derivatives). This means that the number of correct digits in the solution roughly doubles with every iteration. It is incredibly fast. (When the Hessian at the minimum has a zero eigenvalue, the speed drops: for \(x^4\), each Newton step only multiplies \(x\) by \(2/3\).) For a quadratic function with a Positive Definite Hessian, Newton’s method finds the exact minimum in a single step from any starting point.
- Cons: Computational Cost. The method requires three expensive operations at each step:
- Computing the \(p \times p\) Hessian matrix: \(O(p^2)\) derivative calculations.
- Storing the Hessian: \(O(p^2)\) memory.
- Inverting the Hessian (or solving the system \(\mathbf{H}_t \boldsymbol{\delta} = -\nabla f(\mathbf{x}_t)\)): \(O(p^3)\) computational cost.
For high-dimensional problems in AI (where \(p\) can be in the millions), this cost is prohibitive.
- Cons: Convergence Issues. If the Hessian \(\mathbf{H}_t\) is not positive definite (e.g., at a saddle point or in a non-convex region), the Newton step may point uphill or in a wrong direction, causing the algorithm to diverge. It can also be drawn to a saddle point or a maximum: Newton’s method looks for a zero gradient, and on a quadratic it jumps to the stationary point in one step, whatever its type. Even when the Hessian is Positive Definite everywhere, a full step (\(\eta = 1\)) can overshoot from a far start: for the convex function \(f(x) = \sqrt{1 + x^2}\), Newton’s method gives \(x_{t+1} = -x_t^3\), which blows up from every \(|x_0| > 1\). Smaller steps far from the minimum fix this.
6.7 Convexity: The Golden Ticket of Optimization
Thus far, our analysis with the Hessian has been local. We can often classify a single stationary point as a minimum, maximum, or saddle point, but this gives us no information about the function’s global behavior. In practice, we are almost always hunting for a global minimum; the single best set of parameters for our model. In a general, non-convex landscape full of hills and valleys, finding this global minimum is often an intractable problem.
However, there exists a special class of functions for which this problem becomes dramatically simpler: convex functions. A convex function is one that has a “bowl” shape everywhere. For these functions, the landscape has no misleading local minima. There is only one valley floor, and any local minimum is, by necessity, the global minimum. This property is the golden ticket of optimization.
This section formally defines convexity and connects it directly to the properties of the Hessian; the next section shows how this assumption supercharges the performance of gradient descent. It is not within this course’s objectives to delve into convexity in detail, but given our discussion thus far, it is a great point to introduce this important technology in optimization.
6.7.1 Defining the “Bowl”: Three Perspectives on Convexity
What does it mean for a function to be shaped like a bowl? We can define this property from three different but equivalent perspectives.
The Geometric Definition (Secant Line): The most intuitive definition states that for a convex function, the line segment connecting any two points on its graph must lie on or above the graph itself. The definition needs the domain to be a convex set: a set \(\mathcal{D}\) such that, for any two points \(\mathbf{x}, \mathbf{y} \in \mathcal{D}\), the whole segment \(\theta\mathbf{x} + (1-\theta)\mathbf{y}\), \(\theta \in [0, 1]\), stays in \(\mathcal{D}\). For example, \(\mathbb{R}^p\) and the half-line \(x > 0\) are convex sets; the set of all \(x \neq 0\) is not.
Definition 6.5 (Convex Function). A function \(f: \mathcal{D} \rightarrow \mathbb{R}\) is convex if its domain \(\mathcal{D}\) is a convex set and for any two points \(\mathbf{x}, \mathbf{y} \in \mathcal{D}\) and any \(\theta \in [0, 1]\), the following inequality holds:
\[ f(\theta \mathbf{x} + (1-\theta)\mathbf{y}) \le \theta f(\mathbf{x}) + (1-\theta) f(\mathbf{y}). \]The First-Order Condition (Tangent Plane): An alternative definition states that a differentiable function is convex if and only if it lies entirely on or above any of its tangent hyperplanes. The first-order Taylor approximation at any point provides a global underestimator of the function.
Theorem 6.3 (First-Order Condition). Let \(f\) be differentiable on an open convex set \(\mathcal{D}\) (for example, all of \(\mathbb{R}^p\)). Then \(f\) is convex if and only if for all \(\mathbf{x}, \mathbf{y}\) in \(\mathcal{D}\):
\[ f(\mathbf{y}) \ge f(\mathbf{x}) + \langle \nabla f(\mathbf{x}), \mathbf{y} - \mathbf{x} \rangle. \]The Second-Order Condition (Curvature): For twice-differentiable functions, the most practical and powerful definition connects directly to our study of the Hessian. A function is convex if and only if its curvature is non-negative in all directions, at all points.
Theorem 6.4 (Second-Order Condition). Let \(f\) be twice differentiable on an open convex set \(\mathcal{D}\). Then \(f\) is convex if and only if its Hessian matrix, \(\nabla^2 f(\mathbf{x})\), is positive semidefinite for all \(\mathbf{x}\) in \(\mathcal{D}\).
This theorem is the ultimate payoff for our analysis of the Hessian. It gives us a concrete tool: to prove a function is convex, we simply need to show that all eigenvalues of its Hessian are non-negative, everywhere.
6.7.2 The Grand Prize: Local Optima are Global
The reason we care so deeply about convexity is summarized in one profound theorem.
Theorem 6.5. For a convex function \(f\), any stationary point (i.e., a point \(\mathbf{x}^\star\) where \(\nabla f(\mathbf{x}^\star) = \mathbf{0}\)) is a global minimum.
Proof: Let \(\mathbf{x}^\star\) be a stationary point, so \(\nabla f(\mathbf{x}^\star) = \mathbf{0}\). From the first-order condition for convexity, we know that for any other point \(\mathbf{y}\) in the domain:
\[ f(\mathbf{y}) \ge f(\mathbf{x}^\star) + \langle \nabla f(\mathbf{x}^\star), \mathbf{y} - \mathbf{x}^\star \rangle \]Substituting the stationary point condition:
\[ f(\mathbf{y}) \ge f(\mathbf{x}^\star) + \langle \mathbf{0}, \mathbf{y} - \mathbf{x}^\star \rangle = f(\mathbf{x}^\star) \]This inequality, \(f(\mathbf{y}) \ge f(\mathbf{x}^\star)\), holds for every point \(\mathbf{y}\) in the function’s domain. By definition, this means \(\mathbf{x}^\star\) is a global minimum. \(\blacksquare\)
This is a spectacular result. It means that for a convex function, our simple goal of “find a flat spot” is enough: any flat spot we find is a global minimum. (A convex function may have no flat spot at all, as \(e^x\) shows; then it has no minimum either.) We no longer need to worry about getting stuck in a local valley; any valley we find is the lowest valley there is.
Example 6.27 (Examples of Convexity in Machine Learning). Linear Regression: The loss function has a Hessian \(\mathbf{H} = \mathbf{A}^\top\mathbf{A}\). As we proved, this matrix is always positive semidefinite. Therefore, the linear regression objective is a convex function.
Logistic Regression: The loss function has a Hessian \(\mathbf{H} = \mathbf{X}^\top\mathbf{D}\mathbf{X}\), where \(\mathbf{D}\) is diagonal with positive entries. We proved this is also always positive semidefinite. Therefore, the logistic regression objective is a convex function.
6.8 Why Gradient Descent Works: Curvature Sets the Step Size
Chapter 2 ran gradient descent on \(f(x) = 3x^2 + 4x - 2\) and found an exact formula for the error: every step multiplies \(x_t - x^\star\) by \(1 - 6\eta\), so the method converges exactly when \(0 < \eta < 1/3\). The number \(6\) is \(f''(x)\), the curvature. So the curvature decided which step sizes are safe: \(\eta < 2/f''\). This section does the same for functions of many variables, with the Hessian in place of \(f''\). The message fits in one line:
The largest eigenvalue of the Hessian sets a safe step size; its ratio to the smallest sets the speed.
We first see this exactly on quadratic functions. Then we prove three guarantees for gradient descent with step size \(\eta = 1/L\), where \(L\) bounds the largest eigenvalue of the Hessian: one without convexity, one with convexity, and one for functions whose curvature is bounded below by a positive number \(\mu\).
Throughout this section, \(f: \mathbb{R}^p \to \mathbb{R}\) has continuous second derivatives, and gradient descent is the iteration \(\mathbf{x}_{t+1} = \mathbf{x}_t - \eta \nabla f(\mathbf{x}_t)\), started at some \(\mathbf{x}_0\), with a fixed step size \(\eta > 0\).
6.8.1 Warm-Up: Gradient Descent on a Quadratic
Take the quadratic \(f(\mathbf{x}) = \frac{1}{2}\mathbf{x}^\top\mathbf{Q}\mathbf{x} - \mathbf{b}^\top\mathbf{x}\) of Example 6.2, with \(\mathbf{Q}\) Positive Definite. Its gradient is \(\mathbf{Q}\mathbf{x} - \mathbf{b}\), its Hessian is \(\mathbf{Q}\) at every point, and its minimizer \(\mathbf{x}^\star\) solves \(\mathbf{Q}\mathbf{x}^\star = \mathbf{b}\). Subtract \(\mathbf{x}^\star\) from both sides of the update and replace \(\mathbf{b}\) by \(\mathbf{Q}\mathbf{x}^\star\):
\[ \begin{aligned} \mathbf{x}_{t+1} - \mathbf{x}^\star = \mathbf{x}_t - \mathbf{x}^\star - \eta(\mathbf{Q}\mathbf{x}_t - \mathbf{Q}\mathbf{x}^\star) = (\mathbf{I} - \eta\mathbf{Q})(\mathbf{x}_t - \mathbf{x}^\star). \end{aligned} \]So \(\mathbf{x}_t - \mathbf{x}^\star = (\mathbf{I} - \eta\mathbf{Q})^t(\mathbf{x}_0 - \mathbf{x}^\star)\): every step multiplies the error by the same matrix. This is Chapter 2’s formula with the number \(1 - 6\eta\) replaced by the matrix \(\mathbf{I} - \eta\mathbf{Q}\). It is also the Power Method of Chapter 5, without the normalization, run on \(\mathbf{I} - \eta\mathbf{Q}\).
The eigenvectors show what happens. Let \(\mathbf{q}_1, \dots, \mathbf{q}_p\) be orthonormal eigenvectors of \(\mathbf{Q}\) with eigenvalues \(\lambda_1, \dots, \lambda_p\) (the Spectral Theorem of Chapter 4), and write \(\lambda_{\min}\) and \(\lambda_{\max}\) for the smallest and the largest eigenvalue. Each \(\mathbf{q}_i\) is also an eigenvector of \(\mathbf{I} - \eta\mathbf{Q}\), with eigenvalue \(1 - \eta\lambda_i\), because \((\mathbf{I} - \eta\mathbf{Q})\mathbf{q}_i = \mathbf{q}_i - \eta\lambda_i\mathbf{q}_i\). Write the starting error in eigenvector coordinates, \(\mathbf{x}_0 - \mathbf{x}^\star = \sum_i c_i\mathbf{q}_i\) with \(c_i = \mathbf{q}_i^\top(\mathbf{x}_0 - \mathbf{x}^\star)\). Then
\[ \begin{aligned} \mathbf{x}_t - \mathbf{x}^\star = \sum_{i=1}^p (1 - \eta\lambda_i)^t c_i\mathbf{q}_i . \end{aligned} \]Each eigenvector direction is its own one-variable problem, exactly like Chapter 2’s, with curvature \(\lambda_i\) in place of \(6\). Two consequences follow.
- Safe step sizes. The error goes to zero from every starting point exactly when \(|1 - \eta\lambda_i| < 1\) for every \(i\), that is, when \(0 < \eta < 2/\lambda_{\max}\). The steepest direction sets the limit: a step that is safe for \(\lambda_{\max}\) is safe for every other eigenvalue.
- Speed. With the step \(\eta = 1/\lambda_{\max}\), the factors \(1 - \lambda_i/\lambda_{\max}\) all lie between \(0\) and \(1 - \lambda_{\min}/\lambda_{\max}\). The steepest direction is done after one step (its factor is \(0\)). The flattest direction is the slowest; its factor is
\[ \begin{aligned}
1 - \frac{\lambda_{\min}}{\lambda_{\max}} = 1 - \frac{1}{\kappa(\mathbf{Q})}, \quad \text{where} \quad \kappa(\mathbf{Q}) = \frac{\lambda_{\max}}{\lambda_{\min}}
\end{aligned} \]
is the condition number of Chapters 4 and 5 (for a Positive Definite matrix, the singular values are the eigenvalues). With \(\eta = 1/\lambda_{\max}\), the largest eigenvalue of \(\mathbf{I} - \eta\mathbf{Q}\) is \(1 - \lambda_{\min}/\lambda_{\max}\), so, as in the Power Method, the error ends up pointing along the flattest direction (unless the start has no component along it). When \(\kappa\) is large, the bowl is a long, narrow valley, and gradient descent crawls along its floor.
Example 6.28 (Step Sizes on an Elliptical Bowl). Take \(f(x_1, x_2) = 5x_1^2 + 0.5x_2^2\), the elliptical bowl that returns in the next section. Its Hessian is \(\mathbf{Q} = \left[\begin{smallmatrix} 10 & 0 \\ 0 & 1 \end{smallmatrix}\right]\), so \(\lambda_{\max} = 10\), \(\lambda_{\min} = 1\) and \(\kappa = 10\). The eigenvectors are the coordinate axes, so each step multiplies \(x_1\) by \(1 - 10\eta\) and \(x_2\) by \(1 - \eta\).
- \(\eta = 0.1 = 1/\lambda_{\max}\): from \(\mathbf{x}_0 = [1, 10]^\top\) the iterates are \([0, 9]^\top\), \([0, 8.1]^\top\), and \(\mathbf{x}_t = [0, 10 \cdot 0.9^t]^\top\) for every \(t \ge 1\). The steep coordinate is done after one step. The flat one shrinks by \(0.9 = 1 - 1/\kappa\) per step, so shrinking it a million-fold needs \(0.9^t \le 10^{-6}\), that is, \(t \ge \ln(10^6)/\ln(1/0.9) \approx 131.1\): 132 steps.
- \(\eta = 0.2 = 2/\lambda_{\max}\): the factor for \(x_1\) is \(-1\), so \(x_1\) flips between \(1\) and \(-1\) forever.
- \(\eta = 0.21\): the factor for \(x_1\) is \(-1.1\), and after 50 steps \(|x_1| = 1.1^{50} \approx 117\). Gradient descent diverges, although the step is only 5% above the limit.
Steps against the condition number
Gradient descent with the step 1/λmax on bowls like Example 6.28's, whose Hessian is diag(κ, 1), from (1, 10): the steps needed to shrink the flat coordinate a million-fold, for condition numbers κ from 2 to 1000.
1def steps_needed(kappa):
2 x = np.array([1.0, 10.0])
3 h = np.array([kappa, 1.0]) # Hessian diag(kappa, 1); kappa = 10 is Example 6.28
4 t = 0
5 while abs(x[1]) > 1e-5: # the flat coordinate, 10, shrunk a million-fold
6 x = x - (1 / kappa) * h * x # eta = 1 / lambda_max
7 t += 1
8 return t
9kappas = [2, 5, 10, 20, 50, 100, 200, 500, 1000]
10steps = [steps_needed(k) for k in kappas]
11for k, s in zip(kappas, steps):
12 print(f"kappa = {k:4}: {s:5} steps, {s / k:.1f} per unit of kappa")
kappa = 2: 20 steps, 10.0 per unit of kappa kappa = 5: 62 steps, 12.4 per unit of kappa kappa = 10: 132 steps, 13.2 per unit of kappa kappa = 20: 270 steps, 13.5 per unit of kappa kappa = 50: 684 steps, 13.7 per unit of kappa kappa = 100: 1375 steps, 13.8 per unit of kappa kappa = 200: 2757 steps, 13.8 per unit of kappa kappa = 500: 6901 steps, 13.8 per unit of kappa kappa = 1000: 13809 steps, 13.8 per unit of kappa
Steps against the condition number, on log-log axes. κ = 10 is Example 6.28's bowl: 132 steps. The count grows in proportion to κ, about 13.8 steps per unit of κ, because each step removes only the fraction 1/κ of the flat coordinate and ln(10⁶) ≈ 13.8.
Recorded on an Apple M1 Max laptop, macOS 26.6.2, Python 3.13.7, NumPy 2.4.6, 2026-11-30.
6.8.2 The One Assumption: Curvature at Most \(L\)
For a quadratic, the Hessian is the same matrix at every point, and its eigenvalues settle everything. For other functions, the Hessian changes from point to point, and one step size has to be safe wherever gradient descent goes. This leads to the one assumption behind the guarantees that follow.
Definition 6.6 (Curvature at Most \(L\), at Least \(\mu\)). Let \(L > 0\). A function \(f\) has curvature at most \(L\) if, at every point \(\mathbf{x}\), every eigenvalue of the Hessian \(\nabla^2 f(\mathbf{x})\) is at most \(L\). It has curvature at least \(\mu\) if, at every point, every eigenvalue is at least \(\mu\).
For the quadratic above, \(L = \lambda_{\max}(\mathbf{Q})\) and \(\mu = \lambda_{\min}(\mathbf{Q})\). For logistic regression, the Hessian \(\mathbf{X}^\top\mathbf{D}\mathbf{X}\) changes with \(\mathbf{w}\), but every weight \(h_i(1 - h_i)\) in \(\mathbf{D}\) is at most \(1/4\). So, for every \(\mathbf{v}\),
\[ \begin{aligned} \mathbf{v}^\top\mathbf{X}^\top\mathbf{D}\mathbf{X}\mathbf{v} = \sum_{i=1}^n h_i(1 - h_i)(\mathbf{X}\mathbf{v})_i^2 \le \frac{1}{4}\|\mathbf{X}\mathbf{v}\|_2^2 \le \frac{1}{4}\lambda_{\max}(\mathbf{X}^\top\mathbf{X})\|\mathbf{v}\|_2^2 , \end{aligned} \]where the last step is the sandwich (6.1). Taking \(\mathbf{v}\) to be a unit eigenvector of the Hessian shows that each of its eigenvalues is at most \(L = \frac{1}{4}\lambda_{\max}(\mathbf{X}^\top\mathbf{X})\), at every \(\mathbf{w}\). Chapter 5’s Power Method estimates this number cheaply. Its estimate is never too large, so round it up a little before using it as \(L\).
What books call it. A function with curvature at most \(L\) and at least \(-L\) is called \(L\)-smooth. Books usually state this without the Hessian, as a Lipschitz condition on the gradient: \(\|\nabla f(\mathbf{x}) - \nabla f(\mathbf{y})\|_2 \le L\|\mathbf{x} - \mathbf{y}\|_2\) for all \(\mathbf{x}\) and \(\mathbf{y}\). For a function with continuous second derivatives, the two say the same thing. The proofs below use only the upper bound \(L\), never \(-L\).
Why at every point? The bound must hold wherever gradient descent may go, not only near the minimizer. Take \(f(x) = x^4\). It is convex, with its minimum at \(0\), but its curvature \(f''(x) = 12x^2\) has no upper bound, and no fixed step size is safe from every start. The update is \(x_{t+1} = x_t - 4\eta x_t^3 = x_t(1 - 4\eta x_t^2)\). If \(x_t^2 > 1/(2\eta)\), then \(4\eta x_t^2 > 2\), so \(|1 - 4\eta x_t^2| > 1\) and \(|x_{t+1}| > |x_t|\). The same then holds at every later step, with a growth factor that only increases, and the iterates run off to infinity. With \(\eta = 0.1\), the start \(x_0 = 1\) converges (\(0.6\), \(0.5136\), \(0.4594\), and so on, toward \(0\)), but the start \(x_0 = 3\) gives \(-7.8\), then about \(182\), then about \(-2.4 \times 10^6\). So some bound on the curvature, such as the Lipschitz condition, is needed: without one, a step size that works from one start can blow up from another.
Two tools. The proofs need two facts. The first is the sandwich (6.1) from the Eigenvalue Test: for a symmetric matrix \(\mathbf{H}\), the number \(\mathbf{v}^\top\mathbf{H}\mathbf{v}\) lies between \(\lambda_{\min}\|\mathbf{v}\|_2^2\) and \(\lambda_{\max}\|\mathbf{v}\|_2^2\). The second makes the quadratic model of Newton’s method exact.
Lemma 6.6 (Taylor’s Formula with an Exact Remainder). For any two points \(\mathbf{x}\) and \(\mathbf{y}\), there is a point \(\mathbf{z}\) on the segment between them such that
\[ \begin{aligned} f(\mathbf{y}) = f(\mathbf{x}) + \nabla f(\mathbf{x})^\top(\mathbf{y} - \mathbf{x}) + \frac{1}{2}(\mathbf{y} - \mathbf{x})^\top\nabla^2 f(\mathbf{z})(\mathbf{y} - \mathbf{x}). \end{aligned} \]Proof: Let \(\mathbf{d} = \mathbf{y} - \mathbf{x}\), and let \(\varphi(s) = f(\mathbf{x} + s\mathbf{d})\) be the value of \(f\) along the segment, for \(0 \le s \le 1\). Taylor’s theorem from calculus, with the Lagrange form of the remainder, gives \(\varphi(1) = \varphi(0) + \varphi'(0) + \frac{1}{2}\varphi''(\xi)\) for some \(\xi\) between \(0\) and \(1\). By the chain rule, \(\varphi'(s) = \nabla f(\mathbf{x} + s\mathbf{d})^\top\mathbf{d} = \sum_i d_i \frac{\partial f}{\partial x_i}(\mathbf{x} + s\mathbf{d})\). Applying the chain rule again to each term, \(\varphi''(s) = \sum_i \sum_j d_i d_j \frac{\partial^2 f}{\partial x_j \partial x_i}(\mathbf{x} + s\mathbf{d}) = \mathbf{d}^\top\nabla^2 f(\mathbf{x} + s\mathbf{d})\mathbf{d}\). Substituting, with \(\mathbf{z} = \mathbf{x} + \xi\mathbf{d}\), gives the formula. \(\blacksquare\)
Compared with the second-order Taylor approximation behind Newton’s method, the “\(\approx\)” has become “\(=\)”. The price is that the Hessian is evaluated at an unknown point \(\mathbf{z}\), not at \(\mathbf{x}\). That is why we need bounds that hold at every point.
6.8.3 Every Step Goes Downhill
Lemma 6.7 (The Descent Lemma). If \(f\) has curvature at most \(L\), then for all \(\mathbf{x}\) and \(\mathbf{y}\),
\[ \begin{aligned} f(\mathbf{y}) \le f(\mathbf{x}) + \nabla f(\mathbf{x})^\top(\mathbf{y} - \mathbf{x}) + \frac{L}{2}\|\mathbf{y} - \mathbf{x}\|_2^2 . \end{aligned} \]Proof: By Lemma 6.6, \(f(\mathbf{y})\) equals the right side with the last term replaced by \(\frac{1}{2}(\mathbf{y} - \mathbf{x})^\top\nabla^2 f(\mathbf{z})(\mathbf{y} - \mathbf{x})\). Every eigenvalue of \(\nabla^2 f(\mathbf{z})\) is at most \(L\), so by the upper half of the sandwich (6.1) this term is at most \(\frac{L}{2}\|\mathbf{y} - \mathbf{x}\|_2^2\). \(\blacksquare\)
So at every point \(\mathbf{x}\), the graph of \(f\) lies below a bowl that touches it at \(\mathbf{x}\). This bowl is the quadratic model of Newton’s method with the true curvature replaced by the worst case, \(L\), in every direction. Where should we step? To the bottom of the bowl. Setting the gradient of the right side (with respect to \(\mathbf{y}\)) to zero gives \(\nabla f(\mathbf{x}) + L(\mathbf{y} - \mathbf{x}) = \mathbf{0}\), that is,
\[ \begin{aligned} \mathbf{y} = \mathbf{x} - \frac{1}{L}\nabla f(\mathbf{x}). \end{aligned} \]This is a gradient descent step with \(\eta = 1/L\). Newton’s method steps to the bottom of the model built from the true Hessian; gradient descent with \(\eta = 1/L\) steps to the bottom of the model that assumes curvature \(L\) in every direction.
Corollary 6.8 (Every Step Goes Downhill). If \(f\) has curvature at most \(L\), gradient descent with \(\eta = 1/L\) satisfies, at every step,
Proof: Write \(\mathbf{g} = \nabla f(\mathbf{x}_t)\), and put \(\mathbf{x} = \mathbf{x}_t\) and \(\mathbf{y} = \mathbf{x}_{t+1} = \mathbf{x}_t - \frac{1}{L}\mathbf{g}\) in the Descent Lemma. The middle term is \(\mathbf{g}^\top(-\frac{1}{L}\mathbf{g}) = -\frac{1}{L}\|\mathbf{g}\|_2^2\), and the last term is \(\frac{L}{2} \cdot \frac{1}{L^2}\|\mathbf{g}\|_2^2 = \frac{1}{2L}\|\mathbf{g}\|_2^2\). Together they give \(-\frac{1}{2L}\|\mathbf{g}\|_2^2\). \(\blacksquare\)
So no step raises \(f\), and every step lowers it unless the gradient is already zero. It lowers \(f\) by a lot wherever the gradient is large: gradient descent can stall only where the gradient is small. The same computation with any step size \(\eta\) guarantees a decrease of \(\eta(1 - L\eta/2)\|\nabla f(\mathbf{x}_t)\|_2^2\) (Exercise M11). This is positive exactly for \(0 < \eta < 2/L\), the safe range of the warm-up, and it is largest at \(\eta = 1/L\).
6.8.4 Without Convexity: Gradient Descent Finds a Nearly Flat Point
Theorem 6.9 (Gradient Descent without Convexity). Let \(f\) have curvature at most \(L\), and let \(f\) be bounded below: \(f(\mathbf{x}) \ge f_{\text{low}}\) for every \(\mathbf{x}\). Run gradient descent with \(\eta = 1/L\) from any \(\mathbf{x}_0\). Then, for every number of steps \(T \ge 1\),
\[ \begin{aligned} \min_{0 \le t \le T-1} \|\nabla f(\mathbf{x}_t)\|_2^2 \le \frac{2L\left(f(\mathbf{x}_0) - f_{\text{low}}\right)}{T} . \end{aligned} \]Proof: Rearrange (6.2) as \(\frac{1}{2L}\|\nabla f(\mathbf{x}_t)\|_2^2 \le f(\mathbf{x}_t) - f(\mathbf{x}_{t+1})\), and add these inequalities for \(t = 0, 1, \dots, T-1\). On the right, every intermediate value appears once with a plus sign and once with a minus sign, so the sum collapses to \(f(\mathbf{x}_0) - f(\mathbf{x}_T)\), which is at most \(f(\mathbf{x}_0) - f_{\text{low}}\). On the left, a sum of \(T\) terms is at least \(T\) times its smallest term. Hence \(\frac{T}{2L}\min_t \|\nabla f(\mathbf{x}_t)\|_2^2 \le f(\mathbf{x}_0) - f_{\text{low}}\). Multiply both sides by \(2L/T\). \(\blacksquare\)
What the theorem says, and what it does not:
- A nearly flat point, not the lowest point. The guarantee is about the gradient: one of the first \(T\) iterates has a small gradient. It does not promise a point where the gradient is exactly zero, and there may be none: the function \(\log(1 + e^{-w})\) of the next subsection meets both assumptions and has no flat spot, and gradient descent runs off to infinity while the gradient shrinks to zero. Where a flat spot is reached, it can be a minimum, a maximum or a saddle point (Definition 6.3). Take \(f(x) = \cos x\). Its curvature is \(f''(x) = -\cos x \le 1\), so \(L = 1\), and \(f \ge -1\). Gradient descent with \(\eta = 1\) is \(x_{t+1} = x_t + \sin x_t\). From \(x_0 = 0.5\) it visits \(0.9794\), \(1.8096\), \(2.7812\), \(3.1338\), \(3.1416\), and so finds the minimum at \(\pi\). From \(x_0 = 0\), a maximum, it never moves, because \(\sin 0 = 0\). Both runs obey the theorem.
- Bounded below is needed. The function \(f(x) = -x\) has curvature \(0\), but gradient descent walks off to \(+\infty\) while \(f\) falls to \(-\infty\), and the gradient keeps size \(1\) forever.
- Speed. To guarantee a gradient of size at most \(\varepsilon\), it is enough that \(2L(f(\mathbf{x}_0) - f_{\text{low}})/T \le \varepsilon^2\), that is, \(T \ge 2L(f(\mathbf{x}_0) - f_{\text{low}})/\varepsilon^2\) steps. Ten times more accuracy in the guarantee costs a hundred times more steps.
- Neural networks. The loss of a neural network is usually not convex (Exercise H5), so a guarantee of this kind, a nearly flat point and not necessarily a good one, is the best one can hope for. Even this one does not apply as stated: the curvature of the H5 loss has no upper bound (its second derivative in \(w_2\) is \(2w_1^4\)). In practice the curvature bound holds only in parts of the landscape, and \(\eta\) is tuned by trial and error rather than computed.2
6.8.5 With Convexity: Gradient Descent Finds the Minimum
Theorem 6.10 (Gradient Descent on Convex Functions). Let \(f\) be convex with curvature at most \(L\): at every point, every eigenvalue of \(\nabla^2 f(\mathbf{x})\) lies between \(0\) and \(L\). Let \(\mathbf{x}^\star\) be a minimizer of \(f\). Run gradient descent with \(\eta = 1/L\) from any \(\mathbf{x}_0\). Then, for every \(T \ge 1\),
\[ \begin{aligned} f(\mathbf{x}_T) - f(\mathbf{x}^\star) \le \frac{L\|\mathbf{x}_0 - \mathbf{x}^\star\|_2^2}{2T} . \end{aligned} \]Proof: Write \(\mathbf{g}_t = \nabla f(\mathbf{x}_t)\), so that \(\mathbf{x}_{t+1} = \mathbf{x}_t - \frac{1}{L}\mathbf{g}_t\). We combine three facts.
- One step goes downhill, by (6.2): \(f(\mathbf{x}_{t+1}) \le f(\mathbf{x}_t) - \frac{1}{2L}\|\mathbf{g}_t\|_2^2\).
- \(f\) lies above its tangent planes (the First-Order Condition, Theorem 6.3): \(f(\mathbf{x}^\star) \ge f(\mathbf{x}_t) + \mathbf{g}_t^\top(\mathbf{x}^\star - \mathbf{x}_t)\). With the Hessian, this takes one line: in Lemma 6.6 with \(\mathbf{x} = \mathbf{x}_t\) and \(\mathbf{y} = \mathbf{x}^\star\), the last term is at least \(0\), because \(\nabla^2 f(\mathbf{z})\) is Positive Semidefinite.
- An identity, checked by expanding the square on the right: for any vectors \(\mathbf{a}\) and \(\mathbf{g}\), \[ \begin{aligned} \mathbf{a}^\top\mathbf{g} - \frac{1}{2L}\|\mathbf{g}\|_2^2 = \frac{L}{2}\left(\|\mathbf{a}\|_2^2 - \left\|\mathbf{a} - \tfrac{1}{L}\mathbf{g}\right\|_2^2\right). \end{aligned} \]
Fact 2, rearranged, says \(f(\mathbf{x}_t) \le f(\mathbf{x}^\star) + \mathbf{g}_t^\top(\mathbf{x}_t - \mathbf{x}^\star)\). Put this into Fact 1: \(f(\mathbf{x}_{t+1}) - f(\mathbf{x}^\star) \le \mathbf{g}_t^\top(\mathbf{x}_t - \mathbf{x}^\star) - \frac{1}{2L}\|\mathbf{g}_t\|_2^2\). Apply Fact 3 with \(\mathbf{a} = \mathbf{x}_t - \mathbf{x}^\star\) and \(\mathbf{g} = \mathbf{g}_t\). Since \(\mathbf{a} - \frac{1}{L}\mathbf{g}_t = \mathbf{x}_{t+1} - \mathbf{x}^\star\),
Add (6.3) for \(t = 0, 1, \dots, T-1\). As in the previous proof, the right side collapses, to \(\frac{L}{2}(\|\mathbf{x}_0 - \mathbf{x}^\star\|_2^2 - \|\mathbf{x}_T - \mathbf{x}^\star\|_2^2) \le \frac{L}{2}\|\mathbf{x}_0 - \mathbf{x}^\star\|_2^2\). On the left, \(f(\mathbf{x}_t)\) never increases (Fact 1), so each of the \(T\) terms \(f(\mathbf{x}_{t+1}) - f(\mathbf{x}^\star)\) is at least the last one, \(f(\mathbf{x}_T) - f(\mathbf{x}^\star)\). Hence \(T\left(f(\mathbf{x}_T) - f(\mathbf{x}^\star)\right) \le \frac{L}{2}\|\mathbf{x}_0 - \mathbf{x}^\star\|_2^2\). Divide by \(T\). \(\blacksquare\)
- Speed. Call \(f(\mathbf{x}_T) - f(\mathbf{x}^\star)\) the gap. The guaranteed gap shrinks like \(1/T\): to make the guarantee ten times smaller, run ten times more steps.
- Never farther from the minimizer. The left side of (6.3) is at least \(0\), so \(\|\mathbf{x}_{t+1} - \mathbf{x}^\star\|_2 \le \|\mathbf{x}_t - \mathbf{x}^\star\|_2\): every step ends at least as close to \(\mathbf{x}^\star\) as it started.
- A minimizer must exist for the bound to make sense. The logistic loss of a single data point with feature \(1\) and label \(1\), as a function of the weight, is \(f(w) = \log(1 + e^{-w})\). It is convex with curvature at most \(1/4\), but it has no minimizer: it keeps decreasing toward \(0\) as \(w\) grows. Gradient descent still drives \(f(w_t)\) toward \(0\), but \(w_t\) runs off to infinity, slowly (roughly like \(\ln t\)). The same happens to logistic regression whenever a line through the origin separates the two classes perfectly (any line, if one of the features is the constant \(1\)): the loss has no minimizer, and the weights grow without bound.
6.8.6 Curvature between \(\mu\) and \(L\): Gradient Descent Converges Geometrically
Theorem 6.11 (Gradient Descent with Curvature between \(\mu\) and \(L\)). Let \(0 < \mu \le L\), and let \(f\) have curvature at least \(\mu\) and at most \(L\): at every point, every eigenvalue of \(\nabla^2 f(\mathbf{x})\) lies between \(\mu\) and \(L\). Let \(\mathbf{x}^\star\) be the minimizer of \(f\). Run gradient descent with \(\eta = 1/L\) from any \(\mathbf{x}_0\). Then, for every \(t \ge 0\),
\[ \begin{aligned} f(\mathbf{x}_t) - f(\mathbf{x}^\star) \le \left(1 - \frac{\mu}{L}\right)^t \left(f(\mathbf{x}_0) - f(\mathbf{x}^\star)\right). \end{aligned} \]Functions with curvature at least some \(\mu > 0\) are called strongly convex. Such a function has exactly one minimizer. It has at least one: by (6.4) below, \(f\) grows at least as fast as a bowl in every direction, so it has a lowest point (we take this fact from calculus as given: a continuous function has a lowest point on any closed ball, and outside a large enough ball around \(\mathbf{x}_0\), \(f\) is larger than \(f(\mathbf{x}_0)\)). It has at most one: (6.4) at \(\mathbf{x} = \mathbf{x}^\star\), where the gradient is zero, gives \(f(\mathbf{y}) \ge f(\mathbf{x}^\star) + \frac{\mu}{2}\|\mathbf{y} - \mathbf{x}^\star\|_2^2 > f(\mathbf{x}^\star)\) for every \(\mathbf{y} \neq \mathbf{x}^\star\).
Proof: Step 1: a bowl below \(f\). By Lemma 6.6 and the lower half of the sandwich (6.1), for all \(\mathbf{x}\) and \(\mathbf{y}\),
Step 2: far from the minimum, the gradient is large. Write \(\mathbf{g} = \nabla f(\mathbf{x})\) and \(\mathbf{d} = \mathbf{y} - \mathbf{x}\). Completing the square,
\[ \begin{aligned} \mathbf{g}^\top\mathbf{d} + \frac{\mu}{2}\|\mathbf{d}\|_2^2 = \frac{\mu}{2}\left\|\mathbf{d} + \tfrac{1}{\mu}\mathbf{g}\right\|_2^2 - \frac{1}{2\mu}\|\mathbf{g}\|_2^2 \ge -\frac{1}{2\mu}\|\mathbf{g}\|_2^2 . \end{aligned} \]Put \(\mathbf{y} = \mathbf{x}^\star\) in (6.4) and use this bound: \(f(\mathbf{x}^\star) \ge f(\mathbf{x}) - \frac{1}{2\mu}\|\nabla f(\mathbf{x})\|_2^2\). In other words, for every \(\mathbf{x}\),
Step 3: combine. By (6.2) and then (6.5),
\[ \begin{aligned} f(\mathbf{x}_{t+1}) - f(\mathbf{x}^\star) &\le f(\mathbf{x}_t) - f(\mathbf{x}^\star) - \frac{1}{2L}\|\nabla f(\mathbf{x}_t)\|_2^2 \\ &\le f(\mathbf{x}_t) - f(\mathbf{x}^\star) - \frac{\mu}{L}\left(f(\mathbf{x}_t) - f(\mathbf{x}^\star)\right) = \left(1 - \frac{\mu}{L}\right)\left(f(\mathbf{x}_t) - f(\mathbf{x}^\star)\right). \end{aligned} \]Every step multiplies the gap by at most \(1 - \mu/L\), so \(t\) steps multiply it by at most \((1 - \mu/L)^t\). \(\blacksquare\)
- The condition number sets the speed. Write \(\kappa = L/\mu \ge 1\). The guaranteed gap shrinks by the factor \(1 - 1/\kappa\) per step. For the quadratic of the warm-up, \(L = \lambda_{\max}(\mathbf{Q})\) and \(\mu = \lambda_{\min}(\mathbf{Q})\), so \(\kappa = \kappa(\mathbf{Q})\): the condition number that measured how dangerous it is to solve \(\mathbf{Q}\mathbf{x} = \mathbf{b}\) also measures how slowly gradient descent minimizes \(\frac{1}{2}\mathbf{x}^\top\mathbf{Q}\mathbf{x} - \mathbf{b}^\top\mathbf{x}\).
- Steps per digit. Since \(1 - u \le e^{-u}\), after \(t\) steps the gap is at most \(e^{-t/\kappa}\) times its starting value. So \(t \ge \kappa\ln(1/\varepsilon)\) steps are enough to shrink it by the factor \(\varepsilon\): each extra correct digit costs at most about \(\kappa\ln 10 \approx 2.3\kappa\) steps, a fixed number. Compare the convex case, where ten times more accuracy costs ten times more steps. A gap that shrinks by a fixed factor per step converges geometrically; most books call this linear convergence, because the number of correct digits grows linearly with \(t\). This is the “Linear convergence” entry for gradient descent in Table 6.2.
- Least squares pays twice. For \(f(\mathbf{x}) = \frac{1}{2}\|\mathbf{y} - \mathbf{A}\mathbf{x}\|_2^2\), the Hessian is \(\mathbf{A}^\top\mathbf{A}\), so \(\kappa = \kappa(\mathbf{A}^\top\mathbf{A}) = \kappa(\mathbf{A})^2\) (Chapter 5; the same argument works for a tall \(\mathbf{A}\)), assuming the columns of \(\mathbf{A}\) are independent so that \(\mu > 0\). A data matrix with \(\kappa(\mathbf{A}) = 100\) has \(\kappa = 10{,}000\). By the warm-up, the error along the flattest direction shrinks by only \(1 - 1/\kappa = 0.9999\) per step, so each correct digit of \(\mathbf{x}\) costs about \(23{,}000\) steps.
6.8.7 Summary: What Each Assumption Buys
In all three cases the step size is \(1/L\), where \(L\) bounds the largest eigenvalue of the Hessian at every point. Each extra piece of curvature information buys a stronger guarantee (Table 6.3): a nearly flat point; then the minimum, at rate \(1/T\); then geometric convergence, at a speed set by \(\kappa = L/\mu\).
| Eigenvalues of \(\nabla^2 f(\mathbf{x})\) | Guarantee after \(T\) steps |
|---|---|
| At most \(L\), and \(f \ge f_{\text{low}}\) (Theorem 6.9) | \(\min_{t < T}\|\nabla f(\mathbf{x}_t)\|_2^2 \le 2L(f(\mathbf{x}_0) - f_{\text{low}})/T\) |
| Between \(0\) and \(L\), and a minimizer \(\mathbf{x}^\star\) exists (Theorem 6.10) | \(f(\mathbf{x}_T) - f(\mathbf{x}^\star) \le L\|\mathbf{x}_0 - \mathbf{x}^\star\|_2^2/(2T)\) |
| Between \(\mu > 0\) and \(L\) (Theorem 6.11) | \(f(\mathbf{x}_T) - f(\mathbf{x}^\star) \le (1 - \mu/L)^T(f(\mathbf{x}_0) - f(\mathbf{x}^\star))\) |
Newton’s method fits the same picture. Gradient descent with \(\eta = 1/L\) divides every component of the gradient by the largest curvature, \(L\). Newton’s method divides the component along each eigenvector of the Hessian by that eigenvector’s own eigenvalue \(\lambda_i\). On a quadratic with \(\mathbf{Q}\) Positive Definite, this undoes \(\mathbf{Q}\) exactly, since the quadratic model is the function itself, and every direction is finished in one step. Full Newton steps are expensive, so the next section looks for cheap ways to shrink \(\kappa\).
6.9 Approximating Newton and adaptive methods
We have established a clear trade-off: Gradient Descent is computationally cheap but can be slow to converge, especially on poorly conditioned landscapes (large \(\kappa = L/\mu\); Theorem 6.11). Newton’s Method is incredibly fast in terms of iterations but is often computationally infeasible due to the cost of forming and inverting the Hessian matrix. This raises the question: can we find a middle ground? Can we create an algorithm that captures some of the curvature information used by Newton’s method without paying the full \(O(p^3)\) price?
The answer is yes, and the core idea is to approximate the inverse Hessian, \((\mathbf{H}_t)^{-1}\), with a simpler matrix that is both cheap to compute and effective at preconditioning the gradient.
6.9.1 The Diagonal Approximation: Ignoring Cross-Term Interactions
The simplest non-trivial approximation we can make is to assume the Hessian is a diagonal matrix. This means we ignore all the mixed partial derivatives (\(\frac{\partial^2 f}{\partial x_i \partial x_j}\) where \(i \neq j\)) and only consider the direct, “straight-ahead” curvature along each coordinate axis.
Geometrically, this is a strong assumption. It implies that the function is locally separable, meaning there are no interactions between the different dimensions: the curvature along the \(x_i\) axis is independent of our position along the \(x_j\) axis. While rarely true in practice, this simplification is the key to creating a practical algorithm.
If the Hessian \(\mathbf{H}\) were diagonal, its structure would be:
\[ \mathbf{H} = \text{diag}(h_{11}, h_{22}, \dots, h_{pp}), \quad \text{where } h_{ii} = \frac{\partial^2 f}{\partial x_i^2} \]The inverse of a diagonal matrix is trivial to compute; we simply take the reciprocal of each diagonal element:
\[ \mathbf{H}^{-1} = \text{diag}\left(\frac{1}{h_{11}}, \frac{1}{h_{22}}, \dots, \frac{1}{h_{pp}}\right) \]The Newton step, \(\boldsymbol{\delta} = - \mathbf{H}^{-1} \nabla f(\mathbf{x}_t)\), would then simplify to a beautiful, coordinate-wise update:
\[ \delta_i = - \frac{1}{h_{ii}} \nabla f(\mathbf{x}_t)_i \]This leads to an algorithm known as diagonal preconditioning. Instead of a single, global learning rate \(\eta\) for all dimensions, each coordinate \(i\) gets its own effective, adaptive learning rate, \(\eta_i = \eta / h_{ii}\). This needs every \(h_{ii} > 0\). When \(\mathbf{H}\) is Positive Definite this is automatic: \(h_{ii} = \mathbf{e}_i^\top\mathbf{H}\mathbf{e}_i > 0\), where \(\mathbf{e}_i\) is the \(i\)-th standard basis vector. Where some \(h_{ii} \le 0\), the step is undefined or goes uphill in that coordinate.
- Coordinates with high curvature (large \(h_{ii}\)) get smaller effective step sizes, preventing the algorithm from overshooting and oscillating in steep valleys.
- Coordinates with low curvature (small \(h_{ii}\)) get larger effective step sizes, allowing the algorithm to move more quickly along flat plains.
Example 6.29 (The Elliptical Bowl). Consider the simple quadratic function \(f(x_1, x_2) = 5x_1^2 + 0.5x_2^2\). This function describes an elliptical bowl that is very steep along the \(x_1\) direction and very shallow along the \(x_2\) direction. The minimum is at \((0,0)\).
- Gradient: \(\nabla f(\mathbf{x}) = [10x_1, x_2]^\top\).
- Hessian: \(\mathbf{H} = \left[\begin{smallmatrix} 10 & 0 \\ 0 & 1 \end{smallmatrix}\right]\). (In this case, it is already diagonal).
Let’s start at \(\mathbf{x}_0 =[1, 10]^\top\) and analyze one step of different algorithms.
- Gradient Descent (with \(\eta=0.1\)): The gradient at \(\mathbf{x}_0\) is \([10, 10]^\top\).
\[ \mathbf{x}_1 = \begin{bmatrix} 1 \\ 10 \end{bmatrix} - 0.1 \begin{bmatrix} 10 \\ 10 \end{bmatrix} = \begin{bmatrix} 0 \\ 9 \end{bmatrix} \]
The update makes good progress in the steep \(x_1\) direction but crawls very slowly in the shallow \(x_2\) direction. To remain stable, \(\eta\) must be small (less than \(2/L = 2/10 = 0.2\), where \(L = 10\) is the largest eigenvalue of the Hessian; with \(\eta = 0.1\), Example 6.28 needs 132 steps to shrink the error a million-fold), which forces slow progress in the well-behaved directions.
- Newton’s Method: The inverse Hessian is \(\mathbf{H}^{-1} = \left[\begin{smallmatrix} 0.1 & 0 \\ 0 & 1 \end{smallmatrix}\right]\).
\[ \begin{aligned}
\mathbf{x}_1 &= \mathbf{x}_0 - \mathbf{H}^{-1}\nabla f(\mathbf{x}_0) \\ &= \begin{bmatrix} 1 \\ 10 \end{bmatrix} - \begin{bmatrix} 0.1 & 0 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} 10 \\ 10 \end{bmatrix} = \begin{bmatrix} 1 \\ 10 \end{bmatrix} - \begin{bmatrix} 1 \\ 10 \end{bmatrix} \\ &= \begin{bmatrix} 0 \\ 0 \end{bmatrix}
\end{aligned} \]
As expected, it finds the exact minimum in one step.
- Diagonal Preconditioning (with \(\eta = 1\)): Using the diagonal Hessian is identical to the full Newton method here. The update for each coordinate is:
- \(x_{1,1} = x_{1,0} - \eta \frac{1}{h_{11}} \nabla f(\mathbf{x}_0)_1 = 1 - 1 \cdot \frac{1}{10} \cdot 10 = 0\).
- \(x_{2,1} = x_{2,0} - \eta \frac{1}{h_{22}} \nabla f(\mathbf{x}_0)_2 = 10 - 1 \cdot \frac{1}{1} \cdot 10 = 0\).
The coordinate-wise scaling automatically corrects for the different curvatures, allowing both dimensions to converge in a single step.
6.9.2 From Hessian Diagonals to Gradient Magnitudes: The Final Leap
This diagonal approach is a powerful insight, but it still has a practical flaw: computing even just the diagonal elements of the Hessian, \(\frac{\partial^2 f}{\partial x_i^2}\), can be expensive, and this must be done at every iteration.
This is where the final, crucial idea of modern adaptive optimizers comes in. What if we could approximate these diagonal curvature terms using information we already have for free? The only information we compute at every step of a first-order method is the gradient.
The key intuition is this: the magnitude of the gradient itself is a rough proxy for curvature. This is a heuristic, not a theorem. For the quadratic \(\frac{h}{2}x^2\) the gradient is \(hx\): it is large when the curvature \(h\) is large, but also when \(x\) is far from the minimum, and near the minimum it is small whatever \(h\) is. Our elliptical bowl shows the gap: at \(\mathbf{x}_0 = [1, 10]^\top\) the gradient is \([10, 10]^\top\), the same size in both coordinates, while \(h_{11} = 10\) and \(h_{22} = 1\). So the first Adagrad step (below, with \(\eta = 1\) and \(S\) counting the current gradient) lands at \([0, 9]^\top\), the same point as gradient descent, while diagonal Newton lands at the minimum \([0, 0]^\top\). If the gradient with respect to a parameter \(x_i\) has been consistently large, it suggests we are in a steep, narrow valley in that dimension. The curvature is likely high. If the gradient with respect to \(x_i\) has been consistently small, it suggests we are on a flat plateau in that dimension. The curvature is likely low.
Algorithms like Adagrad, RMSProp, and Adam formalize this intuition. Instead of computing \(h_{ii}\), they keep, for each coordinate, a running sum (Adagrad) or a running average (RMSProp, Adam) of the squares of the past gradients; Adam also averages the gradients themselves. Let’s call this number \(S_i\). The update rule then takes the form:
\[ x_{i, t+1} = x_{i, t} - \frac{\eta}{\sqrt{S_{i,t}} + \epsilon} \nabla f(\mathbf{x}_t)_i \]where \(\epsilon\) is a small constant for numerical stability.
This update rule is the practical culmination of our entire second-order analysis. It looks like diagonal Newton’s method:
- The term \(\sqrt{S_{i,t}}\) serves as a cheap stand-in for the local curvature \(h_{ii}\), built from gradients only.
- Each coordinate gets its own adaptive learning rate, which is inversely proportional to this estimated curvature.
- The entire algorithm requires only gradient information; it adds only \(O(p)\) work per iteration on top of computing the gradient, so its cost per iteration is the same as gradient descent’s (\(O(np)\) for least squares).
This approach brilliantly combines the low computational cost of first-order methods with the adaptive, curvature-aware step sizes of second-order methods. It represents one of the most successful practical compromises in the history of optimization and is the reason why optimizers like Adam are the default choice for training the vast majority of deep learning models today.
Exercises
Below is a set of exercises related to the content of Chapter 6. The exercises are partitioned by difficulty. Programming questions should be completed in Python using the NumPy library, assuming knowledge from an introductory algorithms course.
EASY EXERCISES
- E1.Hessian Calculation. Compute the Hessian matrix for the function \(f(x_1, x_2) = 3x_1^2 + 2x_1x_2 + x_2^2 - 4x_1\). Is the Hessian constant or does it depend on the point \((x_1, x_2)\)?
- E2.Eigenvalue Test for Definiteness. The Hessians of four different functions, evaluated at a critical point, are given below. Classify each Hessian as Positive Definite (PD), Negative Definite (ND), Positive Semidefinite (PSD), Negative Semidefinite (NSD), or Indefinite.
- a)\(\mathbf{H}_1 = \begin{bmatrix} 4 & 0 \\ 0 & 3 \end{bmatrix}\)
- b)\(\mathbf{H}_2 = \begin{bmatrix} -2 & 0 \\ 0 & -5 \end{bmatrix}\)
- c)\(\mathbf{H}_3 = \begin{bmatrix} 4 & 0 \\ 0 & -1 \end{bmatrix}\)
- d)\(\mathbf{H}_4 = \begin{bmatrix} 0 & 0 \\ 0 & 3 \end{bmatrix}\)
- a)
- E3.Classifying Critical Points. Based on your classifications in E2, what type of critical point corresponds to each Hessian (Strict Local Minimum, Strict Local Maximum, Saddle Point, or Inconclusive)?
- E4.Convexity and Hessians. A function has a Hessian \(\nabla^2 f(\mathbf{x}) = \begin{bmatrix} 2 & 1 \\ 1 & 3 \end{bmatrix}\) for all \(\mathbf{x}\). The eigenvalues of this matrix are approximately \(1.38\) and \(3.62\). Is this function convex? Explain why or why not, using the second-order condition for convexity.
- E5.Newton’s Method on a 1D Function. Perform one step of Newton’s method for minimizing \(f(x) = x^3 - 2x + 2\), starting at the point \(x_0 = 1\).
- E6.Stationary Point vs. Local Minimum. Is every stationary point a local minimum? Provide a brief explanation and a simple counterexample function in 1D.
- E7.Diagonal Preconditioning Intuition. In diagonal preconditioning, if the second partial derivative with respect to a parameter \(x_i\) is very large (i.e., high curvature), will the algorithm take a larger or smaller step in that direction compared to other directions? Explain why.
- E8.Hessian of a Sum. If \(f(\mathbf{x}) = g(\mathbf{x}) + h(\mathbf{x})\), what is the relationship between the Hessian of \(f\) and the Hessians of \(g\) and \(h\)? Use this property to find the Hessian of \(f(\mathbf{x}) = 5x_1^2 + \sin(x_2)\).
- E9.Computational Cost. For a machine learning model with \(p = 1{,}000{,}000\) parameters, which of the following operations is the primary reason that pure Newton’s method is considered computationally infeasible?
- a)Computing the gradient.
- b)Inverting the \(p \times p\) Hessian matrix.
- c)Storing the gradient vector.
Explain your choice in one sentence.
- a)
- E10.Python: Hessian of a Quadratic. For \(f(\mathbf{x}) = \frac{1}{2}\mathbf{x}^\top\mathbf{Q}\mathbf{x}\) with symmetric \(\mathbf{Q}\), write a Python function
compute_quadratic_hessian(Q)that takes the matrixQand returns the Hessian of \(f\). Test it with a simple \(2 \times 2\) matrix. - E11.Safe Step Sizes. The function \(f(x_1, x_2) = 2x_1^2 + 8x_2^2\) has a constant Hessian.
- a)Find the smallest valid \(L\), the largest valid \(\mu\), and \(\kappa = L/\mu\).
- b)For which fixed step sizes \(\eta\) does gradient descent converge from every starting point?
- c)Run two steps with \(\eta = 1/L\) from \(\mathbf{x}_0 = [1, 1]^\top\). Which coordinate is done after one step, and by what factor does the other one shrink per step?
- a)
- E12.The Condition Number of Least Squares. Let \(\mathbf{A} = \begin{bmatrix} 1 & 0 \\ 0 & 0.1 \end{bmatrix}\) and \(f(\mathbf{x}) = \frac{1}{2}\|\mathbf{y} - \mathbf{A}\mathbf{x}\|_2^2\). Find the Hessian, \(L\), \(\mu\) and \(\kappa\), and compare \(\kappa\) with \(\kappa(\mathbf{A})\). About how many steps of gradient descent with \(\eta = 1/L\) does each extra correct digit of \(\mathbf{x}\) cost? (Use the warm-up.)
MEDIUM EXERCISES
- M1.Find and Classify Critical Points. Find all stationary points of the function \(f(x_1, x_2) = x_1^3 - 3x_1 + x_2^2\). For each stationary point, compute the Hessian and classify it as a local minimum, local maximum, or saddle point.
- M2.Definiteness of Linear Regression Hessian. Prove that the Hessian of the linear regression loss function, \(\mathbf{H} = \mathbf{A}^\top\mathbf{A}\), is always positive semidefinite. Under what condition on the data matrix \(\mathbf{A}\) does it become positive definite? Explain the practical implication of this condition.
- M3.Newton’s Step vs. Gradient Descent Step. Consider the elliptical bowl function \(f(x_1, x_2) = 10x_1^2 + x_2^2\).
- a)Calculate the gradient and Hessian of this function.
- b)At the point \(\mathbf{x}_0 = [1, 1]^\top\), compute the direction of the gradient descent step, \(-\nabla f(\mathbf{x}_0)\).
- c)At the same point, compute the direction of the Newton step, \(-\mathbf{H}^{-1}\nabla f(\mathbf{x}_0)\).
- d)Compare the two directions. Which one points more directly towards the true minimum at \([0, 0]^\top\)?
- a)
- M4.Convexity of a Function. Using the second-order condition (i.e., by analyzing the Hessian), determine if the function \(f(x_1, x_2) = x_1^2 - 2x_1x_2 + 2x_2^2\) is convex.
- M5.Failure of Newton’s Method. Consider trying to find the maximum of the function \(f(x) = \sin(x)\) using Newton’s method on the minimization problem \(\min_x -f(x)\). A maximum occurs at \(x = \pi/2\). What happens if you initialize Newton’s method at \(x_0 = \pi\)? Try to compute the first iterate \(x_1\). What goes wrong, and why?
- M6.Convexity of a Logarithmic Function. Determine if the function \(f(x) = x \log(x)\) is convex for \(x > 0\) by analyzing its second derivative.
- M7.Complexity of Logistic Regression. For a logistic regression problem with \(n = 10{,}000\) data points and \(p = 5{,}000\) features, estimate the approximate number of floating-point operations (multiplications) required for one step of Newton’s method. Use these leading counts: \(np^2\) multiplications to form the Hessian and \(p^3\) to solve the linear system. Which of the three main components (gradient, Hessian computation, Hessian inversion) dominates the cost?
- M8.Python: One Step of Newton’s Method. Write a Python function
newton_step(f_grad, f_hess, x)that takes three arguments: a function that computes the gradient, a function that computes the Hessian, and the current pointx. The function should return the next point according to one step of pure Newton’s method. Test it on the function from M3 starting at \(\mathbf{x}_0 = [1, 1]^\top\). - M9.Python: Classifying a 2x2 Hessian. Write a Python function
classify_\allowbreak hessian(H)that takes a \(2 \times 2\) NumPy arrayH, computes its eigenvalues, and returns a string: “Positive Definite”, “Negative Definite”, “Positive Semidefinite”, “Negative Semidefinite”, or “Indefinite”. Test it on the four matrices from exercise E2. - M10.The Best Fixed Step for a Quadratic. In the warm-up of the gradient-descent section, every step multiplies the error along the eigenvector \(\mathbf{q}_i\) by \(1 - \eta\lambda_i\), and the eigenvalues lie between \(\mu = \lambda_{\min}\) and \(L = \lambda_{\max}\).
- a)Explain why the error along the slowest direction is multiplied by the factor \(\max(|1 - \eta\mu|, |1 - \eta L|)\) per step (it shrinks when this factor is below \(1\)).
- b)Show that this factor is smallest at \(\eta = 2/(L + \mu)\), where it equals \((\kappa - 1)/(\kappa + 1)\) with \(\kappa = L/\mu\).
- c)For \(\kappa = 10\) and \(\kappa = 100\), compare \((\kappa - 1)/(\kappa + 1)\) with \(1 - 1/\kappa\), the factor for \(\eta = 1/L\). How many steps does each need to shrink the error a million-fold?
- d)Chapter 2’s Exercise H4 found the best step for \(f(x) = ax^2 + bx + c\). Check that (b) gives the same answer when \(p = 1\).
- a)
- M11.One Step with Any Step Size. Let \(f\) have curvature at most \(L\).
- a)Repeat the proof of Corollary 6.8 with a general step size \(\eta > 0\), and show that \(f(\mathbf{x}_{t+1}) \le f(\mathbf{x}_t) - \eta(1 - L\eta/2)\|\nabla f(\mathbf{x}_t)\|_2^2\).
- b)For which \(\eta\) is the guaranteed decrease positive?
- c)Which \(\eta\) makes the guaranteed decrease largest, and what is the largest value?
- d)In Example 6.28, the step \(\eta = 0.21\) diverges. Does this contradict (b)?
- a)
HARD EXERCISES
- H1.Proof of Convexity for a Log-Sum-Exp Function. Prove that the log-sum-exp function, \(f(\mathbf{x}) = \log\left(\sum_{i=1}^p e^{x_i}\right)\), is convex. To do this, you will need to compute its Hessian matrix and show that it is positive semidefinite. (Hint: a matrix of the form \(\mathbf{v}\mathbf{v}^\top\) is always PSD. A matrix \(\mathbf{A} = \text{diag}(\mathbf{d}) - \mathbf{v}\mathbf{v}^\top\) with all \(d_i > 0\) is PSD if \(\mathbf{v}^\top \text{diag}(\mathbf{d})^{-1} \mathbf{v} \le 1\)).
- H2.Implementing and Comparing GD and Newton’s Method. For the 2D function from M3, \(f(x_1, x_2) = 10x_1^2 + x_2^2\):
- a)Implement Gradient Descent in Python. Start at \(\mathbf{x}_0 = [1, 10]^\top\) and use a fixed learning rate \(\eta=0.05\). Run for 50 iterations and store the sequence of iterates.
- b)Implement Newton’s Method in Python for the same function. Start at the same point.
- c)Plot the path of both algorithms on a 2D contour plot of the function. Qualitatively describe the difference in their behavior and explain why Newton’s method is so much more effective here.
- a)
- H3.Deriving a Diagonal Quasi-Newton Update. A popular approximation in Quasi-Newton methods is to approximate the Hessian with a simple diagonal matrix, \(\mathbf{H}_t \approx \mathbf{D}_t\). At iteration \(k\) the algorithm uses one data point \((\mathbf{x}_k, y_k)\) with prediction \(h_k = \sigma(\mathbf{w}_k^\top\mathbf{x}_k)\); \(x_{k,j}\) is its \(j\)-th feature.
- a)The gradient for the logistic loss for a single data point \((\mathbf{x}, y)\) is \(\nabla L(\mathbf{w}) = (h-y)\mathbf{x}\).
- b)The diagonal of the true Hessian is given by \(H_{jj} = h(1-h)x_j^2\).
- c)In the Adagrad algorithm, the diagonal preconditioner is approximated by \(D_{jj} = \sqrt{\sum_{k=0}^t (\nabla L(\mathbf{w}_k)_j)^2}\). Write out what this Adagrad preconditioner term \(D_{jj}\) is explicitly in terms of the inputs \(x_{k,j}\), outputs \(y_k\), and predictions \(h_k\) from all past iterations. Explain why this might be a reasonable, if crude, approximation of the true Hessian diagonal.
- a)
- H4.Regularized Newton’s Method. A common way to handle cases where the Hessian is not positive definite is to use a regularized or “damped” Hessian: \(\mathbf{H}_{\text{reg}} = \mathbf{H} + \gamma\mathbf{I}\), where \(\gamma > 0\) is a regularization parameter.
- a)Prove that if the original Hessian \(\mathbf{H}\) has eigenvalues \(\lambda_i\), then the regularized Hessian has eigenvalues \(\lambda_i + \gamma\).
- b)Explain how choosing a sufficiently large \(\gamma\) can guarantee that the regularized Hessian is positive definite, even if the original Hessian was indefinite. How does this relate to the idea of blending Newton’s method with gradient descent?
- a)
- H5.Non-Convexity of a Simple Neural Network. Consider a very simple neural network with one input \(x\), one hidden neuron with activation function \(\phi(z) = z^2\), and one output. The model is \(f(w_1, w_2) = \phi(w_1 x) w_2 = (w_1x)^2 w_2\). Let the loss for a single data point \((x=1, y=1)\) be the squared error \(L(w_1, w_2) = (y - f(w_1, w_2))^2 = (1 - w_1^2 w_2)^2\). Show that this loss function is non-convex by computing its Hessian and showing that it is indefinite at the point \((w_1=1, w_2=0)\). Then compute the Hessian at \((w_1 = 1, w_2 = 1)\), where the loss is \(0\). Is it indefinite there? What does this say about testing convexity at a single point?
- H6.Gradient Descent on a Wavy Function. Let \(f(x) = \cos x\).
- a)Show that \(f\) has curvature at most \(L = 1\) and that \(f \ge -1\), so Theorem 6.9 applies with \(\eta = 1\). Is \(f\) convex?
- b)Run gradient descent with \(\eta = 1\) from \(x_0 = 4\) for three steps. Where does it go, and what kind of point is that?
- c)Repeat from \(x_0 = 0\) and from \(x_0 = -0.5\).
- d)For the run from \(x_0 = 0.5\) with \(T = 4\), compute both sides of the inequality in Theorem 6.9.
- e)Python: run gradient descent with \(\eta = 1\) for 50 steps from 1000 random starts in \([-10, 10]\). Where do the runs end? The theorem allows a maximum; explain why the runs avoid the maxima.
- a)