Functionals and functional derivatives
The calculus of variations is a field of mathematics that deals with the optimization of functions of functions, called functionals. This topic was not taught to me in my computer science education, but it lies at the foundation of a number of important concepts and algorithms in the data sciences such as gradient boosting and variational inference. In this post, I will provide an explanation of the functional derivative and show how it relates to the gradient of an ordinary multivariate function.
Introduction
Multivariate calculus concerns itself with infitesimal changes of numerical functions – that is, functions that accept a vector of real-numbers and output a real number:
In this blog post, we discuss the calculus of variations, a field of mathematics that generalizes the ideas in multivariate calculus relating to infinitesimal changes of traditional numeric functions to functions of functions, called functionals. Specifically, given a set of functions, , a functional is a mapping between and the real-numbers:
Functionals are quite prevalent in machine learning and statistical inference. For example, information entropy can be considered a functional on probability mass functions. For a given discrete random variable, , entropy can be thought about as a function that accepts as input ’s probability mass function, , and outputs a real number:
where is the support of .
Another example of a functional is the evidence lower bound (ELBO): a function that, like entropy, operates on probability distributions. The ELBO is a foundational quantity used in the popular EM algorithm and variational inference used for performing statistical inference with probabilistic models.
In this blog post, we will review some concepts in traditional calculus such as partial derivatives, directional derivatives, and gradients in order to introduce the definition of the functional derivative, which is simply the generalization of the gradient of numeric functions to functionals.
A review of derivatives and gradients
In this section, we will introduce a few important concepts in multivariate calculus: derivatives, partial derivatives, directional derivatives, and gradients.
Derivatives
Before going further, let’s quickly review the basic definition of the derivative for a univariate function that maps real numbers to real numbers. That is,
The derivative of at input , denoted , describes the rate of change of at . It is defined rigorously as
Geometrically, is the slope of the line that is tangential to at as depicted below:

In this schematic, we depict the value of getting smaller and smaller. As it does, the slope of the line approaches that of the line that is tangential to at x. This slope is the derivative .
Partial derivatives
We will now consider a continous multivariate function that maps real-valued vectors to real-numbers. That is,
Given , the partial derivative of with respect to the th component of , denoted is simply the derivative of if we hold all the components of fixed, except for the th component. Said differently, it tells us the rate of change of with respect to the th dimension of the vector space in which resides! This can be visualized below for a function :

As seen above, the partial derivative is simply the derivative of the function when holding as fixed. That is, it is the slope of the line tangent to the function of when fixed.
Directional derivatives
We can see that the partial derivative of with respect to the th dimension of the vector space can be expressed as
where is the th standard basis vector – that is, the vector of all zeroes except for a one in the th position.
Geometrically, we can view the th partial derivative of as ’s rate of change along the direction of the th standard basis vector of the vector space.
Thinking along these lines, there is nothing stopping us from generalizing this idea to any unit vector rather than just the standard basis vectors. Given some unit vector , we define the directional derivative of along the direction of as
Geometrically, this is simply the rate of change of along the direction at which is pointing! This can be viewed schematically below:

For a given vector , we can derive a formula for . That is, we can show that:
See Theorem 1 in the Appendix of this post for a proof of this equation. Now, if we define the vector of all partial derivatives as
Then we can represent the directional derivative as simply the dot product between and :
This vector , is called the gradient vector of at .
Gradients
As described above, the gradient vector, is the vector constructed by taking the partial derivative of at along each basis vector. It turns out that the gradient vector points in the direction of steepest ascent along ’s surface at . This can be shown schematically below:

We prove this property of the gradient vector in Theorem 2 of the Appendix to this post.
Functional derivatives
Now, we will seek to generalize the notion of gradients to functionals. We’ll let be some set of functions, and for simplicity, we’ll let each be a continuous real-valued function. That is, for each , we have . Then, we’ll consider a functional that maps each to a number. That is,
Now, we’re going to spoil the punchline with the definition for the functional derivative:
Definition 1 (Functional derivative): Given a function , the functional derivative of at , denoted , is defined to be the function for which:
\begin{align*}\int \frac{\partial F}{\partial f}(x) \eta(x) \ dx &= \lim_{h \rightarrow 0}\frac{F(f + h \eta) - F(f)}{h} \\ &= \frac{d F(f + h\eta)}{dh}\bigg\rvert_{h=0}\end{align*}
where is a scalar and is an arbitrary function in .
Woah. What is going on here? How on earth does this define the functional derivative? And why is the functional derivative, buried inside such a seemingly complicated equation?
Let’s break it down.
First, notice the similarity of the right-hand side of the equation of Definition 1 to the definition of the directional gradient:

Indeed, the equation in Definition 1 describes the analogy of the directional derivative for functionals! That is, it describes the rate of change of at in the direction of the function !
How does this work? As we shrink down to an infinitesimaly small number, will become arbitrarily close to . In the illustration below, we see an example function (red) and another function (blue). As gets smaller, the function (purple) becomes more similar to :

Thus, we see that is the “infinitesimal” change to that is analogous to the infinitesimal change to that we describe by in the definition of the directional derivative. The quantity is called a variation of (hence the word “variational” in the name “calculus of variations”).
Now, so far we have only shown that the equation in Definition 1 describes something analogous to the directional derivative for multivariate numerical functions. We showed this by comparing the right-hand side of the equation in Definition 1 to the definition of the directional gradient. However, as Definition 1 states, the functional derivative itself is defined to be the function within the integral on the left-hand side of the equation. What is going on here? Why is this the functional derivative?
Now, it is time to recall the gradient for traditional multivariate functions. Specifically, notice the similarity between the alternative formulation of the directional derivative, which uses the gradient, and the left-hand side of the equation in Definition 1:

Notice, that these equations have similar forms. Instead of a summation in the definition of the directional derivative, we have an integral in the eqation for Definition 1. Moreover, instead of summing over elements of the vector , we “sum” (using an integral) each value of . Lastly, instead of each partial derivative of , we now have each value of the function for each . This function, , is analogous to the gradient! It is thus called the functional derivative!
To drive this home further, recall that we can represent the directional derivative as the dot product between the gradient vector and :
To make this relationship clearer, we note that the dot product is an inner product. Thus, we can write this definition in a more general way as
We also recall that a valid inner product between continuous functions and is
Thus, we see that

Said differently, the functional gradient of a functional, , at a function , denoted is the function for which given any arbitrary function , the inner product between and is the directional derivative of in the direction of !
An example: the functional derivative of entropy
As a toy example, let’s derive the functional derivative of information entropy. Recall at the beginning of this post, the entropy of a discrete random variable can be viewed as a function on ’s probability mass function . More specifically, is defined as
where is the support of .
Let’s derive it’s functional derivative. Let’s start with an arbitrary probability mass function . Then, we can write out the equation that defines the functional derivative:
Let’s simplify this equation:
Now we see that and thus, this is the functional derivative!
Appendix
Theorem 1: Given a differentiable function , vectors , where is a unit vector, then .
Proof:
Consider and to be fixed and let us define the function . Then,
Evaluating this derivative at , we see that
We can also express another way by applying the multivariate chain rule. Doing so, we see that
where is the partial derivative of with respect to it’s th argument when evaluated at . Now, we again evaluate this derivative at and see that
So we have now derived two equivalent forms of . Putting them together we see that
Theorem 2: Given a differentiable function and vector , ’s direction of steepest ascent is the direction pointed to by the gradient .
Proof:
As shown in Theorem 1, given an arbitrary unit vector , the directional derivative can be calculated by taking the dot product of the gradient vector with :
The dot product can be computed as
where is the angle between the two vectors. The function is maximized (and equals 1) when and thus, directional derivative is maximized when . Thus, the unit vector that maximizes the directional derivative is the vector pointing in the same direction as the gradient thus proving that the gradient points in the direction of steepest ascent.