Partial Derivatives and Gradients
(adapted from Fieller [1], Section 7.2)
This section covers derivatives of functions of vectors and matrices. Its linear algebra prerequisites, such as vectors, matrices, transposes, dot products, and quadratic forms, are covered in Linear Algebra.
Let \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\) and \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\) be column vectors of length \(p\) (see column vector and dot product).
Definition 1 (Partial derivative) Let \(f : \mathbb{R}^p \to \mathbb{R}\) and \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}= {(x_1, \ldots, x_p)}^{\top} \in \mathbb{R}^p\). The partial derivative [2] of \(f\) with respect to \(x_j\) at \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\) is
\[ \frac{\partial}{\partial x_j} f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}) \stackrel{\text{def}}{=} \lim_{h \to 0} \frac{f(x_1, \ldots, x_j + h, \ldots, x_p) - f(x_1, \ldots, x_p)}{h}, \]
when this limit exists. It is the derivative at \(x_j\) of the one-variable function \(t \mapsto f(x_1, \ldots, t, \ldots, x_p)\), which varies the \(j\)th coordinate and holds the other coordinates fixed.
Example 1 (Partial derivatives of \(x_1^2 x_2 + 3 x_2\)) Let \(f(x_1, x_2) = x_1^2 x_2 + 3 x_2\). At \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}= {(1, 2)}^{\top}\),
\[ \begin{aligned} f(1, 2) &= 1 \cdot 2 + 6 \\ &= 8, \end{aligned} \]
and for \(h \ne 0\)
\[ \begin{aligned} \frac{f(1 + h, 2) - f(1, 2)}{h} &= \frac{(1 + h)^2 \cdot 2 + 6 - 8}{h} && \text{(substitute into } f \text{)} \\ &= \frac{(1 + h)^2 \cdot 2 - 2}{h} && \text{(} 6 - 8 = -2 \text{)} \\ &= \frac{(1 + 2h + h^2) \cdot 2 - 2}{h} && \text{(expand } (1 + h)^2 \text{)} \\ &= \frac{2 + 4h + 2h^2 - 2}{h} && \text{(distributive law)} \\ &= \frac{4h + 2h^2 + 2 - 2}{h} && \text{(reorder the terms)} \\ &= \frac{4h + 2h^2}{h} && \text{(} 2 - 2 = 0 \text{)} \\ &= 4 + 2h, && \text{(divide by } h \ne 0 \text{)} \end{aligned} \]
which tends to \(4\) as \(h \to 0\), so \(\frac{\partial}{\partial x_1} f(1, 2) = 4\). Holding \(x_2\) fixed and differentiating in \(x_1\) gives the same answer: \(\frac{\partial}{\partial x_1} f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}) = 2 x_1 x_2\), which is \(2 \cdot 1 \cdot 2 = 4\) at \({(1, 2)}^{\top}\). Holding \(x_1\) fixed instead gives \(\frac{\partial}{\partial x_2} f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}) = x_1^2 + 3\), which is \(1 + 3 = 4\) at \({(1, 2)}^{\top}\).
Exercise 1 (First-order partial derivatives) Adapted from Miller [3], Question 1.1.42.
Let \(f(x, y) = x^2 y + e^x + \sin(xy)\). Find \(\frac{\partial f}{\partial x}\) and \(\frac{\partial f}{\partial y}\).
Exercise 2 (Partial derivatives of the normal density with respect to parameters) Adapted from Miller [3], Question 1.1.43.
Let
\[f(x; \mu, \sigma) = \frac{1}{\sqrt{2\pi\sigma^2}}{\operatorname{exp}\mathopen{}\left\{-\frac{(x - \mu)^2}{2\sigma^2}\right\}\mathclose{}}\]
Find \(\frac{\partial f}{\partial \mu}\) and \(\frac{\partial f}{\partial \sigma}\).
Theorem 1 (Partial derivatives of the normal density with respect to its parameters) Let \(\mu\in \mathbb{R}\) and \(\sigma> 0\), and let
\[f(x; \mu, \sigma) = \frac{1}{\sqrt{2\pi\sigma^2}}{\operatorname{exp}\mathopen{}\left\{-\frac{(x - \mu)^2}{2\sigma^2}\right\}\mathclose{}}\]
Then for every \(x \in \mathbb{R}\),
\[ \begin{aligned} \frac{\partial f}{\partial \mu} &= \frac{x - \mu}{\sigma^2} f(x; \mu, \sigma) \\ \frac{\partial f}{\partial \sigma} &= \mathopen{}\left[-\frac{1}{\sigma} + \frac{(x - \mu)^2}{\sigma^3}\right]\mathclose{} f(x; \mu, \sigma) \end{aligned} \]
Proof. This is the solution to Exercise 2, worked in Solution 2. The argument has two steps.
- For \(\frac{\partial f}{\partial \mu}\), treat \(\sigma\) as constant. The prefactor is constant, so the chain rule on the exponential gives \(\frac{\partial f}{\partial \mu} = f(x; \mu, \sigma) \cdot\mathopen{}\left[-\frac{2(x - \mu)(-1)}{2\sigma^2}\right]\mathclose{} = \frac{x - \mu}{\sigma^2} f(x; \mu, \sigma)\).
- For \(\frac{\partial f}{\partial \sigma}\), write \(f(x; \mu, \sigma) = \frac{1}{\sqrt{2\pi}}\sigma^{-1}{\operatorname{exp}\mathopen{}\left\{-\frac{(x - \mu)^2}{2\sigma^2}\right\}\mathclose{}}\). The product rule gives one term from differentiating \(\sigma^{-1}\), namely \(-\sigma^{-1} f(x; \mu, \sigma)\), and one from the chain rule on the exponential, namely \(\frac{(x - \mu)^2}{\sigma^3} f(x; \mu, \sigma)\). Adding them gives \(\mathopen{}\left[-\frac{1}{\sigma} + \frac{(x - \mu)^2}{\sigma^3}\right]\mathclose{} f(x; \mu, \sigma)\).
Exercise 3 (Partial derivatives with exponential of quadratic form) Adapted from Miller [3], Question 1.1.44.
Find \(\frac{\partial f}{\partial x}\) and \(\frac{\partial f}{\partial y}\) for
\[f(x, y) = x{\operatorname{exp}\mathopen{}\left\{x^2 + y^2\right\}\mathclose{}}\]
Exercise 4 (Using symmetry to compute partial derivatives) Adapted from Miller [3], Question 1.1.45.
Find \(\frac{\partial f}{\partial x}\) and \(\frac{\partial f}{\partial y}\) for
\[f(x, y) = e^{xy} - \log(x^2 + y^2)\]
on \(\mathbb{R}^2 \setminus \{(0, 0)\}\).
Definition 2 (Vector derivative, gradient) If \(f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}})\) is a scalar-valued function of a \(p \times 1\) vector \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\), such as \(f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) = \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\), then its vector derivative is the column vector of its partial derivatives (Definition 1):
\[ \frac{\partial}{\partial \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}} f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) = \begin{bmatrix} \frac{\partial}{\partial \beta_{1}}f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) \\ \frac{\partial}{\partial \beta_{2}}f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) \\ \vdots \\ \frac{\partial}{\partial \beta_{p}}f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) \end{bmatrix} \]
The vector derivative is also called the gradient [4] of \(f\) with respect to \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\), written \(\nabla_{\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}} f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}})\).
Example 2 (A vector derivative) Let \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}= {(\beta_{1}, \beta_{2})}^{\top}\) and \(f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) = 3 \beta_{1} + 5 \beta_{2}^2\). Then
\[ \begin{aligned} \underbrace{\frac{\partial}{\partial \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}} f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}})}_{2 \times 1} &= \begin{bmatrix} \frac{\partial}{\partial \beta_{1}} (3 \beta_{1} + 5 \beta_{2}^2) \\ \frac{\partial}{\partial \beta_{2}} (3 \beta_{1} + 5 \beta_{2}^2) \end{bmatrix} \\ &= \begin{bmatrix} 3 \\ 10 \beta_{2} \end{bmatrix}, \end{aligned} \]
and at \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}= {(1, 2)}^{\top}\) the vector derivative is \({(3, 20)}^{\top}\).
Hutchinson’s Gradients Refresher (17 min) covers gradients and partial derivatives, the ideas behind this section [5]. The login for the video site is posted on Canvas.
Jon Krohn’s “Calculus for Machine Learning” YouTube playlist has videos on partial derivatives:
1 Checking a gradient by its shape
The gradient always has the same shape as the input it is taken with respect to, because it holds exactly one partial derivative per component of that input:
The table is the cheapest check there is on a gradient just worked out by hand: if the shapes disagree, something went wrong. It is the shape rule of matrix multiplication again, applied to calculus.
Definition 3 (Linear approximation of a function of a vector, first-order Taylor approximation of a function of a vector) Let \(f : \mathbb{R}^p \to \mathbb{R}\) have a gradient at \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\). The linear approximation of \(f\) at \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\) [6] (also called the first-order Taylor approximation or the first-order Taylor expansion) is the function of a step \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\delta}}\vphantom{\delta}}\in \mathbb{R}^p\)
\[ \hat{f}_{\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}}(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\delta}}\vphantom{\delta}}) \stackrel{\text{def}}{=}f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) + \nabla f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\delta}}\vphantom{\delta}}. \tag{1}\]
Example 3 (A linear approximation of a function of two variables) Take \(f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) = 3 \beta_{1} + 5 \beta_{2}^2\) from Example 2, whose gradient at \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}= {(1, 2)}^{\top}\) is \(\nabla f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) = {(3, 20)}^{\top}\). At that point,
\[ \begin{aligned} f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}) &= 3 \cdot 1 + 5 \cdot 2^2 && \text{(substitute } \beta_{1} = 1 \text{ and } \beta_{2} = 2 \text{)} \\ &= 23, && \text{(} 3 + 20 = 23 \text{)} \end{aligned} \]
so the linear approximation at \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\) is
\[ \hat{f}_{\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}}(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\delta}}\vphantom{\delta}}) = 23 + 3 \delta_1 + 20 \delta_2. \]
For the step \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\delta}}\vphantom{\delta}}= {(0.1, 0.1)}^{\top}\), the approximation is
\[ \begin{aligned} \hat{f}_{\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}}(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\delta}}\vphantom{\delta}}) &= 23 + 3 \cdot 0.1 + 20 \cdot 0.1 && \text{(substitute } \delta_1 = \delta_2 = 0.1 \text{)} \\ &= 25.3, && \text{(} 23 + 0.3 + 2 = 25.3 \text{)} \end{aligned} \]
and the value of \(f\) at \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}+ \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\delta}}\vphantom{\delta}}= {(1.1, 2.1)}^{\top}\) is
\[ \begin{aligned} f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}+ \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\delta}}\vphantom{\delta}}) &= 3 \cdot 1.1 + 5 \cdot 2.1^2 && \text{(substitute } \beta_{1} = 1.1 \text{ and } \beta_{2} = 2.1 \text{)} \\ &= 25.35. && \text{(} 3.3 + 22.05 = 25.35 \text{)} \end{aligned} \]
The approximation misses by \(0.05\), which is \(5 \delta_2^2\), the second-order term of \(f\) that the approximation leaves out. That term is never negative, and is zero only when \(\delta_2 = 0\), so the graph of the approximation in Figure 1 is a plane that touches the graph of \(f\) along the line \(\beta_{2} = 2\) and lies below it everywhere else.
Remark 1 (The one-variable linear approximation is the case \(p = 1\)). For \(p = 1\), the gradient of \(f\) is its derivative, so Equation 1 is the one-variable linear approximation, with the step \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\delta}}\vphantom{\delta}}\) in place of \(\varepsilon\).
Definition 4 (Direction) A direction in \(\mathbb{R}^p\) is given by a nonzero vector \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{d}}\vphantom{d}} \in \mathbb{R}^p\): moving from a point \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\) in the direction \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{d}}\vphantom{d}}\) means moving to the points \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}+ t \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{d}}\vphantom{d}}\) with \(t > 0\). For any \(c > 0\), \(c \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{d}}\vphantom{d}}\) reaches the same points (with \(t / c\) in place of \(t\)), so \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{d}}\vphantom{d}}\) and \(c \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{d}}\vphantom{d}}\) give the same direction.
Example 4 (Moving from \({(2, -1)}^{\top}\) in the direction \({(1, 1)}^{\top}\)) From \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}= {(2, -1)}^{\top}\), moving in the direction \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{d}}\vphantom{d}} = {(1, 1)}^{\top}\) reaches the points \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}+ t \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{d}}\vphantom{d}} = {(2 + t,\ -1 + t)}^{\top}\) for \(t > 0\), such as \({(3, 0)}^{\top}\) at \(t = 1\). The vector \({(2, 2)}^{\top}\) gives the same direction: it reaches \({(3, 0)}^{\top}\) at \(t = \tfrac{1}{2}\). The vector \({(-1, -1)}^{\top}\) gives the opposite direction, reaching points such as \({(1, -2)}^{\top}\).
Definition 5 (Level set) Let \(f : \mathbb{R}^p \to \mathbb{R}\), and let \(c\) be a real number. The level set [7] of \(f\) at \(c\) is the preimage of the single value \(c\) under \(f\):
\[ {{{f}^{-1}\mathopen{}\left(\mathopen{}\left\{c\right\}\mathclose{}\right)\mathclose{}}} \stackrel{\text{def}}{=}{{f}^{-1}\mathopen{}\left(\mathopen{}\left\{c\right\}\mathclose{}\right)\mathclose{}}. \]
Theorem 2 (A level set is the set of points where the function takes the value) Let \(f : \mathbb{R}^p \to \mathbb{R}\), and let \(c\) be a real number. The level set of \(f\) at \(c\) is the set of points where \(f\) takes the value \(c\):
\[ {{{f}^{-1}\mathopen{}\left(\mathopen{}\left\{c\right\}\mathclose{}\right)\mathclose{}}} = \mathopen{}\left\{\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\in \mathbb{R}^p : f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}) = c\right\}\mathclose{}. \]
Proof. Unfold the preimage:
\[ \begin{aligned} {{{f}^{-1}\mathopen{}\left(\mathopen{}\left\{c\right\}\mathclose{}\right)\mathclose{}}} &= {{f}^{-1}\mathopen{}\left(\mathopen{}\left\{c\right\}\mathclose{}\right)\mathclose{}} && \text{(}\href{#def-level-set}{\text{Definition~5}}\text{)} \\ &= \mathopen{}\left\{\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\in \mathbb{R}^p : f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}) \in \mathopen{}\left\{c\right\}\mathclose{}\right\}\mathclose{} && \text{(definition of the preimage)} \\ &= \mathopen{}\left\{\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\in \mathbb{R}^p : f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}) = c\right\}\mathclose{}. && \text{(} \mathopen{}\left\{c\right\}\mathclose{} \text{ has the one element } c \text{)} \end{aligned} \]
Example 5 (Level sets of \(x_1^2 + x_2^2\)) Let \(f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}) = x_1^2 + x_2^2\) for \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}= {(x_1, x_2)}^{\top}\). By Theorem 2, each level set of \(f\) holds the points where \(f\) takes that value:
- The level set \({{{f}^{-1}\mathopen{}\left(\mathopen{}\left\{1\right\}\mathclose{}\right)\mathclose{}}}\) is the circle of radius \(1\) around \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{0}}\vphantom{0}}\). For example, \({(0.6, 0.8)}^{\top}\) is on it, since \(0.36 + 0.64 = 1\), and \({(1, 1)}^{\top}\) is not, since \(1 + 1 = 2\).
- The level set \({{{f}^{-1}\mathopen{}\left(\mathopen{}\left\{0\right\}\mathclose{}\right)\mathclose{}}}\) is the single point \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{0}}\vphantom{0}}\).
- The level set \({{{f}^{-1}\mathopen{}\left(\mathopen{}\left\{-1\right\}\mathclose{}\right)\mathclose{}}}\) is empty, since \(x_1^2 + x_2^2 \ge 0\).
Figure 2 draws the first two.
Definition 6 (Level curve) Let \(f : \mathbb{R}^2 \to \mathbb{R}\) and let \(c\) be a real number. The level curve of \(f\) at \(c\) is the level set of \(f\) at \(c\) when it is a curve in the plane. It is the special case \(p = 2\) of a level set. A contour line is a level curve drawn on a plot of \(f\).
Example 6 (A level curve of \(x_1^2 + x_2^2\)) Let \(f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}) = x_1^2 + x_2^2\) for \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}= {(x_1, x_2)}^{\top}\). The level curve at \(4\) is the circle of radius \(2\) around \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{0}}\vphantom{0}}\).
Theorem 3 (Level sets of a linear function) Let \({h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}} = \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}} + \beta_{0}\) with \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\neq \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{0}}\vphantom{0}}\). Every level set \({{{h}^{-1}\mathopen{}\left(\mathopen{}\left\{c\right\}\mathclose{}\right)\mathclose{}}}\) is a hyperplane with normal vector \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\), parallel to the level set \({{{h}^{-1}\mathopen{}\left(\mathopen{}\left\{0\right\}\mathclose{}\right)\mathclose{}}}\). Its dimension is \(p - 1\): a point for \(p = 1\), a line for \(p = 2\), and a two-dimensional plane for \(p = 3\).
Proof. For any \(c\),
\[ \begin{aligned} {{{h}^{-1}\mathopen{}\left(\mathopen{}\left\{c\right\}\mathclose{}\right)\mathclose{}}} &= {\mathopen{}\left\{ \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}: \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}} + \beta_{0}= c \right\}\mathclose{}} \\ &= {\mathopen{}\left\{ \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}: \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}} + (\beta_{0}- c) = 0 \right\}\mathclose{}}. \end{aligned} \]
This is the hyperplane with normal vector \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\) and offset \(\beta_{0}- c\). Changing \(c\) changes only the offset, so the level sets share the normal vector and are parallel. A hyperplane in \(\mathbb{R}^p\) has dimension \(p - 1\) by the hyperplane theorem, which gives the point, line and plane cases for \(p = 1, 2, 3\).
Example 7 (Parallel level lines) Let \({h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}} = 2x_1 + x_2 + 1\), so \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}= {(2, 1)}^{\top}\) and \(\beta_{0}= 1\). The level set at \(c\) is the line \(x_2 = c - 1 - 2x_1\). For \(c = -1, 0, 1\) the lines are \(x_2 = -2 - 2x_1\), \(x_2 = -1 - 2x_1\) and \(x_2 = -2x_1\). They all have slope \(-2\) and differ only in the intercept, so they are parallel.
Theorem 4 (Stepping along the normal vector) Let \({h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}} = \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}} + \beta_{0}\) with \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\neq \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{0}}\vphantom{0}}\). For every \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\in \mathbb{R}^p\) and \(t \in \mathbb{R}\),
\[ {h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}+ t\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right)\mathclose{}} = {h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}} + t{\mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right\rVert\mathclose{}}^2. \]
So a step along \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\) with \(t > 0\) increases \(h\).
Proof. By linearity of the dot product,
\[ \begin{aligned} {h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}+ t\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right)\mathclose{}} &= \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}+ t\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} + \beta_{0}\\ &= \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}} + t\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} + \beta_{0}\\ &= {h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}} + t{\mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right\rVert\mathclose{}}^2. \end{aligned} \]
Example 8 (A step along the normal vector) Let \({h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}} = 2x_1 + x_2 + 1\), so \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}= {(2, 1)}^{\top}\) and \({\mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right\rVert\mathclose{}}^2 = 5\). At \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}= \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{0}}\vphantom{0}}\), \({h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{0}}\vphantom{0}}\right)\mathclose{}} = 1\). A step of \(t = 1\) along \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\) lands at \({(2, 1)}^{\top}\), where \({h\mathopen{}\left({(2, 1)}^{\top}\right)\mathclose{}} = 4 + 1 + 1 = 6 = 1 + 5\).
Theorem 5 (The normal vector is the steepest direction) Let \({h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}} = \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}} + \beta_{0}\) with \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\neq \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{0}}\vphantom{0}}\). Among unit vectors \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}\) (\(\mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}\right\rVert\mathclose{} = 1\)), the change \({h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}+ t\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}\right)\mathclose{}} - {h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}}\) with \(t > 0\) is largest for \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}= \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}/ \mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right\rVert\mathclose{}\).
Proof. For a unit vector \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}\),
\[ {h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}+ t\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}\right)\mathclose{}} - {h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}} = t\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}. \]
Since \(t > 0\), maximizing this change is the same as maximizing \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}\). By the Cauchy-Schwarz inequality, \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}} \le \mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right\rVert\mathclose{}\mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}\right\rVert\mathclose{} = \mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right\rVert\mathclose{}\). Equality holds exactly when \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}= \alpha\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\) for some \(\alpha > 0\). The condition \(\mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}\right\rVert\mathclose{} = 1\) then forces \(\alpha\mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right\rVert\mathclose{} = 1\), so
\[ \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{u}}\vphantom{u}}= \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}/ \mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right\rVert\mathclose{}. \]
Example 9 (Two unit steps compared) Let \({h\mathopen{}\left(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{x}}\vphantom{x}}\right)\mathclose{}} = 2x_1 + x_2 + 1\), so \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}= {(2, 1)}^{\top}\) and \(\mathopen{}\left\lVert\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}\right\rVert\mathclose{} = \sqrt{5}\). A step of size \(1\) along the unit vector \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}}/ \sqrt{5}\) raises \(h\) by \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot \mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} / \sqrt{5} = \sqrt{5} \approx 2.24\). A step of size \(1\) along \({(1, 0)}^{\top}\) raises it by only \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{\beta}}\vphantom{\beta}} \cdot {(1, 0)}^{\top} = 2\).
Turn the blue arrow in Figure 3 and watch the rate: it is largest along the red arrow, zero along the black level curve (Definition 5), and most negative pointing straight back. Then look for a point where the red arrow shrinks to nothing.
Exercise 5 (Take a gradient by hand) Let \(f : \mathbb{R}^2 \to \mathbb{R}\) be
\[f(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{w}}\vphantom{w}}) = 3 w_1 w_2 + w_2^3 - 5 w_1\]
- Compute \(\partial f / \partial w_1\) (Definition 1), treating \(w_2\) as a constant.
- Compute \(\partial f / \partial w_2\), treating \(w_1\) as a constant.
- Assemble \(\nabla_{\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{w}}\vphantom{w}}} f\) and evaluate it at \(\mathord{\smash[b]{\underset{\raise.3ex\hbox{$\scriptstyle\sim$}}{w}}\vphantom{w}} = \begin{bmatrix} 2 & -1 \end{bmatrix}^{\top}\).
- Check the shape of your answer against Table 1.
