7 Multivariable calculus
7.1 Functions of multiple variables
You will often have occasion to work with mappings of multiple variables at once. As a simple statistical example, think of a linear regression model of y on two distinct variables, x_1 and x_2. The formula for the conditional expectation of y given x_1 and x_2 is \mathbb{E}[y \mid x_1, x_2] = \beta_0 + \beta_1 x_1 + \beta_2 x_2, \tag{7.1} where each \beta is a real-valued coefficient.
To make this a bit more concrete, imagine y is an American adult’s opinion of Donald Trump on a 0–100 scale, x_1 is the number of years of education they have, and x_2 is the percentile of their household income (0–100, from poorest to richest). A plausible-enough formula here might be \mathbb{E}[y \mid x_1, x_2] = 42 + (-1) x_1 + 0.2 x_2. \tag{7.2} With this formula in hand, you can plug in different hypothetical levels of education and income to predict opinions about Trump.
| x_1 | x_2 | \mathbb{E}[y \mid x_1, x_2] |
|---|---|---|
| 12 | 25 | 35 |
| 12 | 50 | 40 |
| 12 | 75 | 45 |
| 16 | 25 | 31 |
| 16 | 50 | 36 |
| 16 | 75 | 41 |
| 20 | 25 | 27 |
| 20 | 50 | 32 |
| 20 | 75 | 37 |
Think about what we’re doing here. We’re taking pairs of numbers x_1 and x_2, plugging them into a formula, and getting a single number back. Another way to think of a “pair of numbers x_1 and x_2” is as a two-dimensional vector, (x_1, x_2) \in \mathbb{R}^2. And thus another way to think about Equation 7.2 (or Equation 7.1 for that matter) is as a function that maps from \mathbb{R}^2 into \mathbb{R}.
We’re now going to work with this type of function, mapping some set of vectors into the set of real numbers. Specifically, for the rest of this chapter, we’ll be considering functions of the form f : X \to \mathbb{R}, where X \subseteq \mathbb{R}^k. In line with our typical notation for vectors, we’ll generally write the set of arguments/inputs to such a function as x_1, x_2, \ldots, x_k.
In the case of a two-dimensional domain, X \subseteq \mathbb{R}^2, this looks like a function whose graph lives in 3-dimensional space, with the x-y coordinate plane as the domain. For example, if we take the domain of Equation 7.2 to be X = \{(x_1, x_2) \in \mathbb{R}^2 \mid x_1 \in [8, 24] \; \text{and} \; x_2 \in [0, 100]\}, which we can denote more compactly as X = [8, 24] \times [0, 100], its graph looks something like the following figure. (I personally hate 3d plots, so I’m only going to give you one of these.)
7.2 Partial derivatives
7.2.1 Defining the partial derivative
Look again at Figure 7.1, and imagine taking a “slice” along the x_1 axis. To give a specific example, imagine fixing x_1 = 16, and then seeing the value just as a function of x_2.
This looks like an ordinary type of function mapping reals into reals that we know very well how to deal with. In fact, it looks like a linear function with a slope of 0.2. From this angle, then, it looks like “the” derivative of f is 0.2.
The tricky thing is that we’d get a different answer if we sliced from a different direction. Now imagine slicing the original function along the other axis instead, fixing x_2 = 50 and examining how the value changes just with x_1.
Once again we see something that looks like a regular old boring function mapping reals into reals. But from this angle it looks like the derivative is -1.
By now you might have already guessed that there is no “the” derivative of a multivariate function. The marginal change in the value of the function depends on which direction we’re moving in. For example, with the function at hand, f(x_1, x_2) = 42 - x_1 + 0.2 x_2, \tag{7.3} a small increase in x_1 reduces the value of the function, whereas a small increase in x_2 increases the value of the function.
The partial derivativeThe derivative of a multivariate function with respect to one of its inputs, calculated by taking the derivative while treating the other inputs as if they were constants. formalizes this notion: taking the derivative with respect to one argument, while holding the other(s) fixed. The formal definition involves applying the familiar rise-over-run formula to a single input of the function, while holding all others at a fixed value.
Definition 7.1 (Partial derivative) Consider a function f : X \to \mathbb{R}, where X \subseteq \mathbb{R}^k, a point \mathbf{c} \in X, and the input numbered m \in \{1, \ldots, k\}. The partial derivativeThe derivative of a multivariate function with respect to one of its inputs, calculated by taking the derivative while treating the other inputs as if they were constants. of f with respect to x_m at \mathbf{c}, denoted \partial f(\mathbf{c}) / \partial x_m, is defined by the limit \begin{aligned} \frac{\partial f(\mathbf{c})}{\partial x_m} &= \lim_{x_m \to c_m} \frac{f(c_1, \ldots, x_m, \ldots, c_k) - f(\mathbf{c})}{x_m - c_m} \\ &= \lim_{h \to 0} \frac{f(c_1, \ldots, c_m + h, \ldots, c_k) - f(\mathbf{c})}{h}, \end{aligned} provided that this limit exists and is finite.
This definition looks, well, cumbersome. In practice, though, it’s no harder to calculate a partial derivative of a multivariate function than it is to calculate an ordinary derivative of a single-variable function. To see why, let’s first briefly push through the pain of using the ordinary definition.
Let’s go back to our Trump-rating-prediction function, Equation 7.3. Take any point (c_1, c_2) in the domain, and let’s use the ugly limit formula to calculate the partial derivative with respect to x_1—i.e., the marginal change in predicted Trump rating with an increase in the person’s years of education. The formula gives us the same “slope” we saw in Figure 7.3: \begin{aligned} \frac{\partial f(c_1, c_2)}{\partial x_1} &= \lim_{h \to 0} \frac{f(c_1 + h, c_2) - f(c_1, c_2)}{h} \\ &= \lim_{h \to 0} \frac{[42 - (c_1 + h) + 0.2 c_2] - [42 - c_1 + 0.2 c_2]}{h} \\ &= \lim_{h \to 0} \frac{-(c_1 + h) - (-c_1)}{h} \\ &= \lim_{h \to 0} \frac{-c_1 - h + c_1}{h} \\ &= \lim_{h \to 0} \frac{-h}{h} \\ &= -1. \end{aligned}
Exercise 7.1 (The formal definition of the partial derivative) Following the same steps as I just used for the partial derivative with respect to x_1, confirm that \partial f(c_1, c_2) / \partial x_2 = 0.2 at all domain points (c_1, c_2).
Answer
For any point (c_1, c_2) in the domain, we have \begin{aligned} \frac{\partial f(c_1, c_2)}{\partial x_2} &= \lim_{h \to 0} \frac{f(c_1, c_2 + h) - f(c_1, c_2)}{h} \\ &= \lim_{h \to 0} \frac{[42 - c_1 + 0.2 (c_2 + h)] - [42 - c_1 + 0.2 c_2]}{h} \\ &= \lim_{h \to 0} \frac{0.2 (c_2 + h) - 0.2 c_2}{h} \\ &= \lim_{h \to 0} \frac{0.2 c_2 + 0.2 h - 0.2 c_2}{h} \\ &= \lim_{h \to 0} \frac{0.2 h}{h} \\ &= 0.2. \end{aligned}
This method of calculating a partial derivative works, but we’ve made it way harder than it needs to be. We’d get the same exact answer if we just took the formula for f(x_1, x_2) and did our ordinary derivative stuff, pretending x_2 was a constant: \begin{aligned} \frac{\partial f(x_1, x_2)}{\partial x_1} &= \frac{d}{d x_1} \left[42 - x_1 + 0.2 x_2\right] \\ &= \frac{d}{d x_1} [42] - \frac{d}{d x_1} [x_1] + 0.2 \frac{d}{d x_1} [x_2] \\ &= 0 - 1 + 0.2 \cdot 0 \\ &= -1. \end{aligned}
You can probably eyeball the function and see that you’d end up with \frac{\partial f(x_1, x_2)}{\partial x_2} = 0.2 if you did the usual derivative stuff with x_2, pretending x_1 was a constant. If that doesn’t seem obvious, work through it explicitly to confirm.
The technique of “just pretend the other variables are constant” doesn’t just work in this singular example. It’s a general property of partial derivatives. The whole point is that you’re holding everything else constant while varying the function along just one dimension.
Exercise 7.2 (More practice with partial derivatives) Consider the function f : \mathbb{R}^2 \to \mathbb{R} defined by f(x_1, x_2) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_3 (x_1 \cdot x_2), where each \beta coefficient is a constant. Using both the formal definition of a partial derivative and the “treat x_2 like it’s a constant and do normal derivative stuff” method, confirm that \frac{\partial f(x_1, x_2)}{\partial x_1} = \beta_1 + \beta_3 x_2.
Answer
Limit method: \begin{aligned} \frac{\partial f(x_1, x_2)}{\partial x_1} &= \lim_{h \to 0} \frac{f(x_1 + h, x_2) - f(x_1, x_2)}{h} \\ &= \lim_{h \to 0} \frac{[\beta_0 + \beta_1 (x_1 + h) + \beta_2 x_2 + \beta_3 (x_1 + h) x_2] - [\beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_3 x_1 x_2]}{h} \\ &= \lim_{h \to 0} \frac{[\beta_1 (x_1 + h) - \beta_1 x_1] + [\beta_3 (x_1 + h) x_2 - \beta_3 x_1 x_2]}{h} \\ &= \lim_{h \to 0} \frac{\beta_1 h + \beta_3 h x_2}{h} \\ &= \lim_{h \to 0} (\beta_1 + \beta_3 x_2) \\ &= \beta_1 + \beta_3 x_2. \end{aligned}
Normal derivative stuff method: \begin{aligned} \frac{\partial f(x_1, x_2)}{\partial x_1} &= \frac{d}{d x_1} \left[\beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_3 (x_1 \cdot x_2)\right] \\ &= \frac{d}{d x_1} [\beta_0] + \beta_1 \frac{d}{d x_1} [x_1] + \beta_2 \frac{d}{d x_1} [x_2] + \beta_3 x_2 \frac{d}{d x_1} [x_1] \\ &= 0 + \beta_1 \cdot 1 + \beta_2 \cdot 0 + \beta_3 x_2 \cdot 1 \\ &= \beta_1 + \beta_3 x_2. \end{aligned}
7.2.2 The gradient
From time to time, it is convenient to talk about all of the partial derivatives of a function at a particular point at once. In particular, we collect them all together into a single vector called the gradientA vector consisting of all of the partial derivatives of f at the domain point \mathbf{c}, denoted \nabla f(\mathbf{c})..
Definition 7.2 (Gradient) Consider a function f : X \to \mathbb{R}, where X \subseteq \mathbb{R}^k, and a point \mathbf{c} \in X. Suppose the partial derivative of f with respect to x_m at \mathbf{c} exists for all m \in \{1, \ldots, k\}. Then the gradientA vector consisting of all of the partial derivatives of f at the domain point \mathbf{c}, denoted \nabla f(\mathbf{c}). of f at \mathbf{c}, denoted \nabla f(\mathbf{c}), is the k-dimensional vector whose elements are the corresponding partial derivatives: \nabla f(\mathbf{c}) = \begin{pmatrix} \partial f(\mathbf{c}) / \partial x_1 \\ \partial f(\mathbf{c}) / \partial x_2 \\ \cdots \\ \partial f(\mathbf{c}) / \partial x_k \end{pmatrix}.
Here’s a fun fact about the gradient that sometimes comes up in computational statistics and machine learning: the gradient points in the “direction of steepest ascent.” To illustrate what I mean by this, think about the function f : \mathbb{R}^2 \to \mathbb{R} defined by f(x_1, x_2) = -(x_1 - 1)^2 - (x_2 + 2)^2, which has a local (and indeed global) maximizer at (1, -2). The gradient of this function is \nabla f(x_1, x_2) = \begin{pmatrix} -2 (x_1 - 1) \\ -2 (x_2 + 2) \end{pmatrix}. If you start at some other point \mathbf{c}, you’ll see that \mathbf{c} + \nabla f(\mathbf{c}) points toward the maximum. For example, the contour plot below shows what happens when you start at \mathbf{c} = (-1, -1) and move slightly in the direction of \nabla f(\mathbf{c}) = (4, -2).
Exercise 7.3 (Gradient of a quadratic function) Again consider the function f(x_1, x_2) = -(x_1 - 1)^2 - (x_2 + 2)^2. Show that for any point \mathbf{c} \in \mathbb{R}^2, \mathbf{c} + \frac{1}{2} \nabla f(\mathbf{c}) =\begin{pmatrix}1\\-2\end{pmatrix}. In other words, no matter where you start, the gradient there points right toward the maximum.
Answer
Using the formula for the gradient from above, we have \begin{aligned} \mathbf{c} + \frac{1}{2} \nabla f(\mathbf{c}) &= \begin{pmatrix} c_1 \\ c_2 \end{pmatrix} + \frac{1}{2} \begin{pmatrix} -2 (c_1 - 1) \\ -2 (c_2 + 2) \end{pmatrix} \\ &= \begin{pmatrix} c_1 \\ c_2 \end{pmatrix} + \begin{pmatrix} -(c_1 - 1) \\ -(c_2 + 2) \end{pmatrix} \\ &= \begin{pmatrix} c_1 - c_1 + 1 \\ c_2 - c_2 - 2 \end{pmatrix} \\ &= \begin{pmatrix} 1 \\ -2 \end{pmatrix}. \end{aligned}
The gradient doesn’t always point right at the local max like it does here. That’s a bit of a special property of quadratic functions like the one in my example. However, it is generally true that if you’re planning to move some small distance h > 0 away from \mathbf{c} in the domain, you’ll get the steepest increase moving h units in the direction of \nabla f(\mathbf{c}). This is why various techniques for numerical optimization rely on the gradients of functions, as you’ll see firsthand if you go deep enough into computational statistics or data science.
7.2.3 Higher-order partial derivatives
Just as with ordinary derivatives (Section 3.5), you can take the partial derivative of the partial derivative, resulting in a second partialThe partial derivative of a partial derivative. The second partial of \frac{\partial f}{\partial x_m} with respect to the same argument, x_m, is denoted \frac{\partial^2 f}{\partial x_m^2}. The second partial with respect to a different argument, x_n (where n \neq m), is denoted \frac{\partial^2 f}{\partial x_m \partial x_n}, and is called a cross-partial..
Definition 7.3 (Second partial derivatives and cross-partials) Consider a function f : X \to \mathbb{R}, where X \subseteq \mathbb{R}^k. Assume that all of the partial derivatives of f exist, and are themselves continuous functions, throughout the domain X. Consider a point \mathbf{c} \in X and the input numbered m \in \{1, \ldots, k\}.
The second partial derivativeThe partial derivative of a partial derivative. The second partial of \frac{\partial f}{\partial x_m} with respect to the same argument, x_m, is denoted \frac{\partial^2 f}{\partial x_m^2}. The second partial with respect to a different argument, x_n (where n \neq m), is denoted \frac{\partial^2 f}{\partial x_m \partial x_n}, and is called a cross-partial. of f with respect to x_m at \mathbf{c}, denoted \frac{\partial^2 f(\mathbf{c})}{\partial x_m^2}, is defined by \begin{aligned} \frac{\partial^2 f(\mathbf{c})}{\partial x_m^2} &= \lim_{x_m \to c_m} \frac{\frac{\partial f(c_1, \ldots, x_m, \ldots, c_k)}{\partial x_m} - \frac{\partial f(\mathbf{c})}{\partial x_m}}{x_m - c_m} \\ &= \lim_{h \to 0} \frac{\frac{\partial f(c_1, \ldots, c_m + h, \ldots, c_k)}{\partial x_m} - \frac{\partial f(\mathbf{c})}{\partial x_m}}{h} \\ &= \frac{\partial}{\partial x_m} \left[\frac{\partial f(\mathbf{x})}{\partial x_m}\right] \quad \text{evaluated at $\mathbf{x} = \mathbf{c}$}, \end{aligned} provided that this limit exists and is finite.
For a distinct input n \in \{1, \ldots, k\} (where n \neq m), the cross-partialThe partial derivative of a partial derivative. The second partial of \frac{\partial f}{\partial x_m} with respect to the same argument, x_m, is denoted \frac{\partial^2 f}{\partial x_m^2}. The second partial with respect to a different argument, x_n (where n \neq m), is denoted \frac{\partial^2 f}{\partial x_m \partial x_n}, and is called a cross-partial. of f with respect to x_m and x_n at \mathbf{c}, denoted \frac{\partial^2 f(\mathbf{c})}{\partial x_m \partial x_n}, is defined by \begin{aligned} \frac{\partial^2 f(\mathbf{c})}{\partial x_m \partial x_n} &= \lim_{x_n \to c_n} \frac{\frac{\partial f(c_1, \ldots, x_n, \ldots, c_k)}{\partial x_m} - \frac{\partial f(\mathbf{c})}{\partial x_m}}{x_n - c_n} \\ &= \lim_{h \to 0} \frac{\frac{\partial f(c_1, \ldots, c_n + h, \ldots, c_k)}{\partial x_m} - \frac{\partial f(\mathbf{c})}{\partial x_m}}{h} \\ &= \frac{\partial}{\partial x_n} \left[\frac{\partial f(\mathbf{x})}{\partial x_m}\right] \quad \text{evaluated at $\mathbf{x} = \mathbf{c}$}, \end{aligned} provided that this limit exists and is finite.
If you’ve already wrapped your head around second derivatives, then it’s not too problematic to interpret the second partial with respect to the same input. To illustrate, consider the two-variable function f : [1, 3]^2 \to \mathbb{R} defined by f(x_1, x_2) = \frac{x_1}{x_1 + x_2}. Let’s slice this function along the x_2 axis at x_2 = 2, looking at how the value varies with x_1 when we fix x_2.
This looks like a strictly concave curve, which would lead us to expect a negative second derivative. And indeed, you can confirm that the second partial with respect to x_1 is negative throughout the domain. First compute the first partial: \begin{aligned} \frac{\partial f(x_1, x_2)}{\partial x_1} &= \frac{d}{d x_1} \left[\frac{x_1}{x_1 + x_2}\right] \\ &= \frac{1 \cdot (x_1 + x_2) - 1 \cdot x_1}{(x_1 + x_2)^2} \quad \text{(quotient rule)} \\ &= \frac{x_2}{(x_1 + x_2)^2}. \end{aligned} Then take the derivative again to get the second partial: \begin{aligned} \frac{\partial^2 f(x_1, x_2)}{\partial x_1^2} &= \frac{\partial}{\partial x_1} \left[\frac{\partial f(x_1, x_2)}{\partial x_1}\right] \\ &= \frac{d}{d x_1} \left[\frac{x_2}{(x_1 + x_2)^2}\right] \\ &= \frac{d}{d x_1} \left[x_2 (x_1 + x_2)^{-2}\right] \\ &= x_2 \cdot \left[(-2) \cdot (x_1 + x_2)^{-3} \cdot 1\right] \quad \text{(chain rule)} \\ &= \frac{-2 x_2}{(x_1 + x_2)^3}. \end{aligned} Because x_2 > 0 and x_1 + x_2 > 0 throughout the domain of the function, the second partial with respect to x_1 is strictly negative throughout the domain.
Exercise 7.4 (Taking a second partial) Continue working with f(x_1, x_2) = \frac{x_1}{x_1 + x_2} for x_1 \in [1, 3] and x_2 \in [1, 3].
Graph how f varies with x_2 on the x_1 = 2 slice of the function. What does this lead you to expect about the sign of \partial^2 f(\mathbf{x}) / \partial x_2^2?
Calculate \partial f(\mathbf{x}) / \partial x_2.
Calculate \partial^2 f(\mathbf{x}) / \partial x_2^2. How do your findings compare to what you saw in the graph in part a?
Answer
On the x_1 = 2 slice, the function is f(2, x_2) = 2 / (2 + x_2).

The curve is decreasing and strictly convex, so we should expect the second partial with respect to x_2 to be positive.
Treating x_1 as a constant, we have \begin{aligned} \frac{\partial f(x_1, x_2)}{\partial x_2} &= \frac{d}{d x_2} \left[x_1 (x_1 + x_2)^{-1}\right] \\ &= x_1 \cdot \left[(-1) \cdot (x_1 + x_2)^{-2} \cdot 1\right] \quad \text{(chain rule)} \\ &= \frac{-x_1}{(x_1 + x_2)^2}. \end{aligned}
Differentiating again with respect to x_2, \begin{aligned} \frac{\partial^2 f(x_1, x_2)}{\partial x_2^2} &= \frac{d}{d x_2} \left[-x_1 (x_1 + x_2)^{-2}\right] \\ &= -x_1 \cdot \left[(-2) \cdot (x_1 + x_2)^{-3} \cdot 1\right] \quad \text{(chain rule)} \\ &= \frac{2 x_1}{(x_1 + x_2)^3}. \end{aligned} Because x_1 > 0 and x_1 + x_2 > 0 throughout the domain, the second partial with respect to x_2 is strictly positive throughout the domain. This matches the convex shape of the graph in part (a).
The cross-partial is a bit different. It tells you a kind of “interaction effect.” (In fact, interaction effects in regression analysis are very closely related to cross-partials, as you’ll see in an upcoming exercise.) For example, if \frac{\partial^2 f(\mathbf{c})}{\partial x_m \partial x_n} > 0, that tells you the marginal increase in the function value with an increase in x_m is greater near \mathbf{c} when x_n is higher. In other words, x_n strengthens the effect of x_m if \partial f(\mathbf{c}) / \partial x_m is positive, or dampens it if the first partial is negative.
Back to our running example, we already calculated the first partial with respect to x_1 as \frac{\partial f(x_1, x_2)}{\partial x_1} = \frac{x_2}{(x_1 + x_2)^2}. Let’s fix x_1 = 2, and see how the first partial with respect to x_1 varies with x_2.
Remember that the y-axis here gives us the derivative of the function with respect to x_1, not the value of the function. So what we see here is that the effect of x_1 on f(x_1, x_2) increases with x_2 until x_2 = 2, then decreases afterward. Or to say this in terms of the cross partial, what we’re seeing tells us that \begin{aligned} \frac{\partial^2 f(2, x_2)}{\partial x_1 \partial x_2} > 0 &\quad \text{if $x_2 < 2$}, \\ \frac{\partial^2 f(2, x_2)}{\partial x_1 \partial x_2} < 0 &\quad \text{if $x_2 > 2$}. \end{aligned}
To confirm this, let’s explicitly calculate the cross-partial. We have \begin{aligned} \frac{\partial^2 f(x_1, x_2)}{\partial x_1 \partial x_2} &= \frac{\partial}{\partial x_2} \left[\frac{\partial f(x_1, x_2)}{\partial x_1}\right] \\ &= \frac{d}{d x_2} \left[\frac{x_2}{(x_1 + x_2)^2}\right] \\ &= \frac{1 \cdot (x_1 + x_2)^2 - 2 (x_1 + x_2) \cdot x_2}{(x_1 + x_2)^4} \quad \text{(quotient rule; chain rule)} \\ &= \frac{(x_1 + x_2)^2 - 2 x_1 x_2 - 2 x_2^2}{(x_1 + x_2)^4} \\ &= \frac{x_1^2 + 2 x_1 x_2 + x_2^2 - 2 x_1 x_2 - 2 x_2^2}{(x_1 + x_2)^4} \\ &= \frac{x_1^2 - x_2^2}{(x_1 + x_2)^4} \\ &= \frac{(x_1 + x_2) (x_1 - x_2)}{(x_1 + x_2)^4} \\ &= \frac{x_1 - x_2}{(x_1 + x_2)^3}. \end{aligned} The denominator here is always positive because x_1 > 0 and x_2 > 0. So we can see here that the cross-partial is positive exactly when x_2 < x_1, and negative exactly when x_2 > x_1.
Exercise 7.5 (Equality of cross-partials) Again working with the function f(x_1, x_2) = x_1 / (x_1 + x_2), show that it doesn’t matter which order you take derivatives to arrive at the cross-partial, i.e., \frac{\partial^2 f(x_1, x_2)}{\partial x_2 \partial x_1} = \frac{\partial^2 f(x_1, x_2)}{\partial x_1 \partial x_2}.
Answer
We already calculated the first partial with respect to x_2 in Exercise 7.4: \frac{\partial f(x_1, x_2)}{\partial x_2} = \frac{-x_1}{(x_1 + x_2)^2}. Now take the derivative of this with respect to x_1: \begin{aligned} \frac{\partial^2 f(x_1, x_2)}{\partial x_2 \partial x_1} &= \frac{\partial}{\partial x_1} \left[\frac{\partial f(x_1, x_2)}{\partial x_2}\right] \\ &= \frac{d}{d x_1} \left[\frac{-x_1}{(x_1 + x_2)^2}\right] \\ &= \frac{(-1) \cdot (x_1 + x_2)^2 - 2 (x_1 + x_2) \cdot (-x_1)}{(x_1 + x_2)^4} \\ &= \frac{-(x_1 + x_2)^2 + 2 x_1^2 + 2 x_1 x_2}{(x_1 + x_2)^4} \\ &= \frac{-x_1^2 - 2 x_1 x_2 - x_2^2 + 2 x_1^2 + 2 x_1 x_2}{(x_1 + x_2)^4} \\ &= \frac{x_1^2 - x_2^2}{(x_1 + x_2)^4} \\ &= \frac{(x_1 + x_2) (x_1 - x_2)}{(x_1 + x_2)^4} \\ &= \frac{x_1 - x_2}{(x_1 + x_2)^3}. \end{aligned} This is the same as the cross-partial we calculated above when we took the derivative with respect to x_1 first.
Exercise 7.6 (An interaction term is a cross-partial) Consider the function f : \mathbb{R}^2 \to \mathbb{R} defined by f(x_1, x_2) = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_3 (x_1 \cdot x_2), where each \beta coefficient is a constant. Confirm that \frac{\partial^2 f(x_1, x_2)}{\partial x_1 \partial x_2} = \frac{\partial^2 f(x_1, x_2)}{\partial x_2 \partial x_1} = \beta_3.
Answer
First calculate the two first partials, treating the other input as a constant: \begin{aligned} \frac{\partial f(x_1, x_2)}{\partial x_1} &= \beta_1 + \beta_3 x_2, \\ \frac{\partial f(x_1, x_2)}{\partial x_2} &= \beta_2 + \beta_3 x_1. \end{aligned} Taking the derivative of the first of these with respect to x_2 gives \frac{\partial^2 f(x_1, x_2)}{\partial x_1 \partial x_2} = \frac{\partial}{\partial x_2} \left[\beta_1 + \beta_3 x_2\right] = \beta_3. Taking the derivative of the second with respect to x_1 gives \frac{\partial^2 f(x_1, x_2)}{\partial x_2 \partial x_1} = \frac{\partial}{\partial x_1} \left[\beta_2 + \beta_3 x_1\right] = \beta_3.
In both of the exercises just now, you saw that you ended up with the same cross-partial regardless of which first partial you started with. Is this a general property, or a fluke? In particular, is it generally going to be true that \frac{\partial^2 f(\mathbf{c})}{\partial x_m \partial x_n} = \frac{\partial^2 f(\mathbf{c})}{\partial x_n \partial x_m}, or do you need to think about the order in which you take each partial derivative?
It turns out that there are some functions where the order matters. However, a mathematical result called Young’s theorem tells us that if all of the second partial derivatives of f are themselves continuous functions, then the \partial x_m \partial x_n cross-partial is equal to the \partial x_n \partial x_m one. Luckily, basically all of the functions we encounter in statistics and formal theory satisfy the conditions for Young’s theorem to hold, so in practice I never think about the order in which I’m computing a cross-partial. A full-fledged multivariable calculus class would go through the counterexamples. For our purposes, it’s enough to know that if you’re not working with some kind of exotic or unusual function, you can proceed as if the order doesn’t matter.
Finally, you may have wondered (though tbh you probably didn’t) whether there is an equivalent of the gradient for second partial derivatives. There is—but now it’s a matrix instead of a vector. The Hessian matrixFor a twice differentiable function mapping from \mathbb{R}^k into \mathbb{R}, \mathbf{H} f(\mathbf{c}) is the k \times k matrix whose diagonal entries are the corresponding second partials at \mathbf{c} and whose off-diagonal entries are the corresponding cross-partials at \mathbf{c}. collects all of the second partials and cross-partials into a k \times k matrix.
Definition 7.4 (Hessian matrix) Consider a function f : X \to \mathbb{R}, where X \subseteq \mathbb{R}_k, and a point \mathbf{c} \in X. Suppose all of the second partial derivatives and cross-partials of f at \mathbf{c} exist. Then the HessianFor a twice differentiable function mapping from \mathbb{R}^k into \mathbb{R}, \mathbf{H} f(\mathbf{c}) is the k \times k matrix whose diagonal entries are the corresponding second partials at \mathbf{c} and whose off-diagonal entries are the corresponding cross-partials at \mathbf{c}. of f at \mathbf{c}, denoted \mathbf{H} f(\mathbf{c}), is the k \times k matrix whose diagonal entries are H_{m, m} = \frac{\partial^2 f(\mathbf{c})}{\partial x_m^2} \quad \text{for $m = 1, \ldots, k$}, and whose off-diagonal entries are H_{m, n} = \frac{\partial^2 f(\mathbf{c})}{\partial x_m \partial x_n} \quad \text{for $m, n = 1, \ldots, k$ ($m \neq n$)}.
7.3 Optimization
TBA.
7.4 Multiple integrals
TBA.
7.5 Concept review
- Partial derivative
- The derivative of a multivariate function with respect to one of its inputs, calculated by taking the derivative while treating the other inputs as if they were constants.
- Gradient
- A vector consisting of all of the partial derivatives of f at the domain point \mathbf{c}, denoted \nabla f(\mathbf{c}).
- Second partial
- The partial derivative of a partial derivative. The second partial of \frac{\partial f}{\partial x_m} with respect to the same argument, x_m, is denoted \frac{\partial^2 f}{\partial x_m^2}. The second partial with respect to a different argument, x_n (where n \neq m), is denoted \frac{\partial^2 f}{\partial x_m \partial x_n}, and is called a cross-partial.
- Hessian matrix
- For a twice differentiable function mapping from \mathbb{R}^k into \mathbb{R}, \mathbf{H} f(\mathbf{c}) is the k \times k matrix whose diagonal entries are the corresponding second partials at \mathbf{c} and whose off-diagonal entries are the corresponding cross-partials at \mathbf{c}.
- Gradient
- A vector consisting of all of the partial derivatives of f at the domain point \mathbf{c}, denoted \nabla f(\mathbf{c}).
- Hessian matrix
- For a twice differentiable function mapping from \mathbb{R}^k into \mathbb{R}, \mathbf{H} f(\mathbf{c}) is the k \times k matrix whose diagonal entries are the corresponding second partials at \mathbf{c} and whose off-diagonal entries are the corresponding cross-partials at \mathbf{c}.
- Partial derivative
- The derivative of a multivariate function with respect to one of its inputs, calculated by taking the derivative while treating the other inputs as if they were constants.
- Second partial
- The partial derivative of a partial derivative. The second partial of \frac{\partial f}{\partial x_m} with respect to the same argument, x_m, is denoted \frac{\partial^2 f}{\partial x_m^2}. The second partial with respect to a different argument, x_n (where n \neq m), is denoted \frac{\partial^2 f}{\partial x_m \partial x_n}, and is called a cross-partial.