Mathematics for Machine Learning
1 About
Machine learning uses tools from a variety of mathematical fields. This document is an attempt to provide a summary of the mathematical background needed for an introductory class in machine learning, which at UC Berkeley is known as CS 189/289A.
Our assumption is that the reader is already familiar with the basic concepts of multivariable calculus and linear algebra (at the level of UCB Math 53/54). We emphasize that this document is not a replacement for the prerequisite classes. Most subjects presented here are covered rather minimally; we intend to give an overview and point the interested reader to more comprehensive treatments for further details.
Note that this document concerns math background for machine learning, not machine learning itself. We will not discuss specific machine learning models or algorithms except possibly in passing to highlight the relevance of a mathematical concept.
Earlier versions of this document did not include proofs. We have begun adding in proofs where they are reasonably short and aid in understanding. These proofs are not necessary background for CS 189 but can be used to deepen the reader’s understanding.
You are free to distribute this document as you wish. The latest version can be found at http://gwthomas.github.io/docs/math4ml.pdf. Please report any mistakes to gwthomas@berkeley.edu.
Contents
- 1 About
- 2 Notation
- 3 Linear Algebra
- 4 Calculus and Optimization
- 5 Probability
- References
2 Notation
| Notation | Meaning |
|---|---|
| set of real numbers | |
| set (vector space) of -tuples of real numbers, endowed with the usual inner product | |
| set (vector space) of -by- matrices | |
| Kronecker delta, i.e. if , otherwise | |
| gradient of the function at | |
| Hessian of the function at | |
| transpose of the matrix | |
| sample space | |
| probability of event | |
| distribution of random variable | |
| probability density/mass function evaluated at | |
| complement of event | |
| union of and , with the extra requirement that | |
| expected value of random variable | |
| variance of random variable | |
| covariance of random variables and |
Other notes:
-
•
Vectors and matrices are in bold (e.g. ). This is true for vectors in as well as for vectors in general vector spaces. We generally use Greek letters for scalars and capital Roman letters for matrices and random variables.
-
•
To stay focused at an appropriate level of abstraction, we restrict ourselves to real values. In many places in this document, it is entirely possible to generalize to the complex case, but we will simply state the version that applies to the reals.
-
•
We assume that vectors are column vectors, i.e. that a vector in can be interpreted as an -by- matrix. As such, taking the transpose of a vector is well-defined (and produces a row vector, which is a -by- matrix).
3 Linear Algebra
In this section we present important classes of spaces in which our data will live and our operations will take place: vector spaces, metric spaces, normed spaces, and inner product spaces. Generally speaking, these are defined in such a way as to capture one or more important properties of Euclidean space but in a more general way.
3.1 Vector spaces
Vector spaces are the basic setting in which linear algebra happens. A vector space is a set (the elements of which are called vectors) on which two operations are defined: vectors can be added together, and vectors can be multiplied by real numbers111 More generally, vector spaces can be defined over any field . We take in this document to avoid an unnecessary diversion into abstract algebra. called scalars. must satisfy
-
(i)
There exists an additive identity (written ) in such that for all
-
(ii)
For each , there exists an additive inverse (written ) such that
-
(iii)
There exists a multiplicative identity (written ) in such that for all
-
(iv)
Commutativity: for all
-
(v)
Associativity: and for all and
-
(vi)
Distributivity: and for all and
3.1.1 Euclidean space
The quintessential vector space is Euclidean space, which we denote . The vectors in this space consist of -tuples of real numbers:
For our purposes, it will be useful to think of them as matrices, or column vectors:
Addition and scalar multiplication are defined component-wise on vectors in :
Euclidean space is used to mathematically represent physical space, with notions such as distance, length, and angles. Although it becomes hard to visualize for , these concepts generalize mathematically in obvious ways. Even when you’re working in more general settings than , it is often useful to visualize vector addition and scalar multiplication in terms of 2D vectors in the plane or 3D vectors in space.
3.1.2 Subspaces
Vector spaces can contain other vector spaces. If is a vector space, then is said to be a subspace of if
-
(i)
-
(ii)
is closed under addition: implies
-
(iii)
is closed under scalar multiplication: implies
Note that is always a subspace of , as is the trivial vector space which contains only .
As a concrete example, a line passing through the origin is a subspace of Euclidean space.
Some of the most important subspaces are those induced by linear maps. If is a linear map, we define the nullspace222 It is sometimes called the kernel by algebraists, but we eschew this terminology because the word “kernel” has another meaning in machine learning. of as
and the range (or the columnspace if we are considering the matrix form) of as
It is a good exercise to verify that the nullspace and range of a linear map are always subspaces of its domain and codomain, respectively.
3.2 Metric spaces
Metrics generalize the notion of distance from Euclidean space (although metric spaces need not be vector spaces).
A metric on a set is a function that satisfies
-
(i)
, with equality if and only if
-
(ii)
-
(iii)
(the so-called triangle inequality)
for all .
A key motivation for metrics is that they allow limits to be defined for mathematical objects other than real numbers. We say that a sequence converges to the limit if for any , there exists such that for all . Note that the definition for limits of sequences of real numbers, which you have likely seen in a calculus class, is a special case of this definition when using the metric .
3.3 Normed spaces
Norms generalize the notion of length from Euclidean space.
A norm on a real vector space is a function that satisfies
-
(i)
, with equality if and only if
-
(ii)
-
(iii)
(the triangle inequality again)
for all and all . A vector space endowed with a norm is called a normed vector space, or simply a normed space.
Note that any norm on induces a distance metric on :
One can verify that the axioms for metrics are satisfied under this definition and follow directly from the axioms for norms. Therefore any normed space is also a metric space.333 If a normed space is complete with respect to the distance metric induced by its norm, we say that it is a Banach space.
We will typically only be concerned with a few specific norms on :
Note that the 1- and 2-norms are special cases of the -norm, and the -norm is the limit of the -norm as tends to infinity. We require for the general definition of the -norm because the triangle inequality fails to hold if . (Try to find a counterexample!)
Here’s a fun fact: for any given finite-dimensional vector space , all norms on are equivalent in the sense that for two norms , there exist constants such that
for all . Therefore convergence in one norm implies convergence in any other norm. This rule may not apply in infinite-dimensional vector spaces such as function spaces, though.
3.4 Inner product spaces
An inner product on a real vector space is a function satisfying
-
(i)
, with equality if and only if
-
(ii)
-
(iii)
for all and all . A vector space endowed with an inner product is called an inner product space.
Note that any inner product on induces a norm on :
One can verify that the axioms for norms are satisfied under this definition and follow (almost) directly from the axioms for inner products. Therefore any inner product space is also a normed space (and hence also a metric space).444 If an inner product space is complete with respect to the distance metric induced by its inner product, we say that it is a Hilbert space.
Two vectors and are said to be orthogonal if ; we write for shorthand. Orthogonality generalizes the notion of perpendicularity from Euclidean space. If two orthogonal vectors and additionally have unit length (i.e. ), then they are described as orthonormal.
The standard inner product on is given by
The matrix notation on the righthand side (see the Transposition section if it’s unfamiliar) arises because this inner product is a special case of matrix multiplication where we regard the resulting matrix as a scalar. The inner product on is also often written (hence the alternate name dot product). The reader can verify that the two-norm on is induced by this inner product.
3.4.1 Pythagorean Theorem
The well-known Pythagorean theorem generalizes naturally to arbitrary inner product spaces.
Theorem 1.
If , then
Proof.
Suppose , i.e. . Then
as claimed. ∎
3.4.2 Cauchy-Schwarz inequality
This inequality is sometimes useful in proving bounds:
for all . Equality holds exactly when and are scalar multiples of each other (or equivalently, when they are linearly dependent).
3.5 Transposition
If , its transpose is given by for each . In other words, the columns of become the rows of , and the rows of become the columns of .
The transpose has several nice algebraic properties that can be easily verified from the definition:
-
(i)
-
(ii)
-
(iii)
-
(iv)
3.6 Eigenthings
For a square matrix , there may be vectors which, when is applied to them, are simply scaled by some constant. We say that a nonzero vector is an eigenvector of corresponding to eigenvalue if
The zero vector is excluded from this definition because for every .
We now give some useful results about how eigenvalues change after various manipulations.
Proposition 1.
Let be an eigenvector of with corresponding eigenvalue . Then
-
(i)
For any , is an eigenvector of with eigenvalue .
-
(ii)
If is invertible, then is an eigenvector of with eigenvalue .
-
(iii)
for any (where by definition).
Proof.
(i) follows readily:
(ii) Suppose is invertible. Then
Dividing by , which is valid because the invertibility of implies , gives .
(iii) The case follows immediately by induction on . Then the general case follows by combining the case with (ii). ∎
3.7 Trace
The trace of a square matrix is the sum of its diagonal entries:
The trace has several nice algebraic properties:
-
(i)
-
(ii)
-
(iii)
-
(iv)
The first three properties follow readily from the definition. The last is known as invariance under cyclic permutations. Note that the matrices cannot be reordered arbitrarily, for example in general. Also, there is nothing special about the product of four matrices – analogous rules hold for more or fewer matrices.
Interestingly, the trace of a matrix is equal to the sum of its eigenvalues (repeated according to multiplicity):
3.8 Determinant
The determinant of a square matrix can be defined in several different confusing ways, none of which are particularly important for our purposes; go look at an introductory linear algebra text (or Wikipedia) if you need a definition. But it’s good to know the properties:
-
(i)
-
(ii)
-
(iii)
-
(iv)
-
(v)
Interestingly, the determinant of a matrix is equal to the product of its eigenvalues (repeated according to multiplicity):
3.9 Orthogonal matrices
A matrix is said to be orthogonal if its columns are pairwise orthonormal. This definition implies that
or equivalently, . A nice thing about orthogonal matrices is that they preserve inner products:
A direct result of this fact is that they also preserve 2-norms:
Therefore multiplication by an orthogonal matrix can be considered as a transformation that preserves length, but may rotate or reflect the vector about the origin.
3.10 Symmetric matrices
A matrix is said to be symmetric if it is equal to its own transpose (), meaning that for all . This definition seems harmless enough but turns out to have some strong implications. We summarize the most important of these as
Theorem 2.
(Spectral Theorem) If is symmetric, then there exists an orthonormal basis for consisting of eigenvectors of .
The practical application of this theorem is a particular factorization of symmetric matrices, referred to as the eigendecomposition or spectral decomposition. Denote the orthonormal basis of eigenvectors and their eigenvalues . Let be an orthogonal matrix with as its columns, and . Since by definition for every , the following relationship holds:
Right-multiplying by , we arrive at the decomposition
3.10.1 Rayleigh quotients
Let be a symmetric matrix. The expression is called a quadratic form.
There turns out to be an interesting connection between the quadratic form of a symmetric matrix and its eigenvalues. This connection is provided by the Rayleigh quotient
The Rayleigh quotient has a couple of important properties which the reader can (and should!) easily verify from the definition:
-
(i)
Scale invariance: for any vector and any scalar , .
-
(ii)
If is an eigenvector of with eigenvalue , then .
We can further show that the Rayleigh quotient is bounded by the largest and smallest eigenvalues of . But first we will show a useful special case of the final result.
Proposition 2.
For any such that ,
with equality if and only if is a corresponding eigenvector.
Proof.
We show only the case because the argument for the case is entirely analogous.
Since is symmetric, we can decompose it as . Then use the change of variable , noting that the relationship between and is one-to-one and that since is orthogonal. Hence
Written this way, it is clear that maximizes this expression exactly if and only if it satisfies where and for . That is, contains the index or indices of the largest eigenvalue. In this case, the maximal value of the expression is
Then writing for the columns of , we have
where we have used the matrix-vector product identity.
Recall that are eigenvectors of and form an orthonormal basis for . Therefore by construction, the set forms an orthonormal basis for the eigenspace of . Hence , which is a linear combination of these, lies in that eigenspace and thus is an eigenvector of corresponding to .
We have shown that , from which we have the general inequality for all unit-length . ∎
By the scale invariance of the Rayleigh quotient, we immediately have as a corollary (since for unit )
Theorem 3.
(Min-max theorem) For all ,
with equality if and only if is a corresponding eigenvector.
3.11 Positive (semi-)definite matrices
A symmetric matrix is positive semi-definite if for all , . Sometimes people write to indicate that is positive semi-definite.
A symmetric matrix is positive definite if for all nonzero , . Sometimes people write to indicate that is positive definite. Note that positive definiteness is a strictly stronger property than positive semi-definiteness, in the sense that every positive definite matrix is positive semi-definite but not vice-versa.
These properties are related to eigenvalues in the following way.
Proposition 3.
A symmetric matrix is positive semi-definite if and only if all of its eigenvalues are nonnegative, and positive definite if and only if all of its eigenvalues are positive.
Proof.
Suppose is positive semi-definite, and let be an eigenvector of with eigenvalue . Then
Since (by the assumption that it is an eigenvector), we have , so we can divide both sides by to arrive at . If is positive definite, the inequality above holds strictly, so . This proves one direction.
To simplify the proof of the other direction, we will use the machinery of Rayleigh quotients. Suppose that is symmetric and all its eigenvalues are nonnegative. Then for all ,
Since matches in sign, we conclude that is positive semi-definite. If the eigenvalues of are all strictly positive, then , whence it follows that is positive definite. ∎
As an example of how these matrices arise, consider
Proposition 4.
Suppose . Then is positive semi-definite. If , then is positive definite.
Proof.
For any ,
so is positive semi-definite.
Note that implies , which in turn implies (recall that this is a property of norms). If , implies , so if and only if , and thus is positive definite. ∎
Positive definite matrices are invertible (since their eigenvalues are nonzero), whereas positive semi-definite matrices might not be. However, if you already have a positive semi-definite matrix, it is possible to perturb its diagonal slightly to produce a positive definite matrix.
Proposition 5.
If is positive semi-definite and , then is positive definite.
Proof.
Assuming is positive semi-definite and , we have for any that
as claimed. ∎
An obvious but frequently useful consequence of the two propositions we have just shown is that is positive definite (and in particular, invertible) for any matrix and any .
3.11.1 The geometry of positive definite quadratic forms
A useful way to understand quadratic forms is by the geometry of their level sets. A level set or isocontour of a function is the set of all inputs such that the function applied to those inputs yields a given output. Mathematically, the -isocontour of is .
Let us consider the special case where is a positive definite matrix. Since is positive definite, it has a unique matrix square root , where is the eigendecomposition of and . It is easy to see that this matrix is positive definite (consider its eigenvalues) and satisfies . Fixing a value , the -isocontour of is the set of such that
where we have used the symmetry of . Making the change of variable , we have the condition . That is, the values lie on a sphere of radius . These can be parameterized as where has . Then since , we have
where also satisfies since is orthogonal. Using this parameterization, we see that the solution set is the image of the unit sphere under the invertible linear map .
What we have gained with all these manipulations is a clear algebraic understanding of the -isocontour of in terms of a sequence of linear transformations applied to a well-understood set. We begin with the unit sphere, then scale every axis by , resulting in an axis-aligned ellipsoid. Observe that the axis lengths of the ellipsoid are proportional to the inverse square roots of the eigenvalues of . Hence larger eigenvalues correspond to shorter axis lengths, and vice-versa.
Then this axis-aligned ellipsoid undergoes a rigid transformation (i.e. one that preserves length and angles, such as a rotation/reflection) given by . The result of this transformation is that the axes of the ellipse are no longer along the coordinate axes in general, but rather along the directions given by the corresponding eigenvectors. To see this, consider the unit vector that has . In the pre-transformed space, this vector points along the axis with length proportional to . But after applying the rigid transformation , the resulting vector points in the direction of the corresponding eigenvector , since
where we have used the matrix-vector product identity from earlier.
In summary: the isocontours of are ellipsoids such that the axes point in the directions of the eigenvectors of , and the radii of these axes are proportional to the inverse square roots of the corresponding eigenvalues.
3.12 Singular value decomposition
Singular value decomposition (SVD) is a widely applicable tool in linear algebra. Its strength stems partially from the fact that every matrix has an SVD (even non-square matrices)! The decomposition goes as follows:
where and are orthogonal matrices and is a diagonal matrix with the singular values of (denoted ) on its diagonal.
By convention, the singular values are given in non-increasing order, i.e.
Only the first singular values are nonzero, where is the rank of .
Observe that the SVD factors provide eigendecompositions for and :
It follows immediately that the columns of (the right-singular vectors of ) are eigenvectors of , and the columns of (the left-singular vectors of ) are eigenvectors of .
The matrices and are not necessarily the same size, but both are diagonal with the squared singular values on the diagonal (plus possibly some zeros). Thus the singular values of are the square roots of the eigenvalues of (or equivalently, of )555 Recall that and are positive semi-definite, so their eigenvalues are nonnegative, and thus taking square roots is always well-defined. .
3.13 Some useful matrix identities
3.13.1 Matrix-vector product as linear combination of matrix columns
Proposition 6.
Let be a vector and a matrix with columns . Then
This identity is extremely useful in understanding linear operators in terms of their matrices’ columns. The proof is very simple (consider each element of individually and expand by definitions) but it is a good exercise to convince yourself.
3.13.2 Sum of outer products as matrix-matrix product
An outer product is an expression of the form , where and . By inspection it is not hard to see that such an expression yields an matrix such that
It is not immediately obvious, but the sum of outer products is actually equivalent to an appropriate matrix-matrix product! We formalize this statement as
Proposition 7.
Let and . Then
where
Proof.
For each , we have
This last expression should be recognized as an inner product between the th row of and the th row of , or equivalently the th column of . Hence by the definition of matrix multiplication, it is equal to . ∎
3.13.3 Quadratic forms
Let be a symmetric matrix, and recall that the expression is called a quadratic form of . It is in some cases helpful to rewrite the quadratic form in terms of the individual elements that make up and :
This identity is valid for any square matrix (need not be symmetric), although quadratic forms are usually only discussed in the context of symmetric matrices.
4 Calculus and Optimization
Much of machine learning is about minimizing a cost function (also called an objective function in the optimization community), which is a scalar function of several variables that typically measures how poorly our model fits the data we have.
4.1 Extrema
Optimization is about finding extrema, which depending on the application could be minima or maxima. When defining extrema, it is necessary to consider the set of inputs over which we’re optimizing. This set is called the feasible set. If is the entire domain of the function being optimized (as it often will be for our purposes), we say that the problem is unconstrained. Otherwise the problem is constrained and may be much harder to solve, depending on the nature of the feasible set.
Suppose . A point is said to be a local minimum (resp. local maximum) of in if (resp. ) for all in some neighborhood about .666 A neighborhood about is an open set which contains . Furthermore, if for all , then is a global minimum of in (similarly for global maximum). If the phrase “in ” is unclear from context, assume we are optimizing over the whole domain of the function.
The qualifier strict (as in e.g. a strict local minimum) means that the inequality sign in the definition is actually a or , with equality not allowed. This indicates that the extremum is unique within some neighborhood.
Observe that maximizing a function is equivalent to minimizing , so optimization problems are typically phrased in terms of minimization without loss of generality. This convention (which we follow here) eliminates the need to discuss minimization and maximization separately.
4.2 Gradients
The single most important concept from calculus in the context of machine learning is the gradient. Gradients generalize derivatives to scalar functions of several variables. The gradient of , denoted , is given by
Gradients have the following very important property: points in the direction of steepest ascent from . Similarly, points in the direction of steepest descent from . We will use this fact frequently when iteratively minimizing a function via gradient descent.
4.3 The Jacobian
The Jacobian of is a matrix of first-order partial derivatives:
Note the special case , where .
4.4 The Hessian
The Hessian matrix of is a matrix of second-order partial derivatives:
Recall that if the partial derivatives are continuous, the order of differentiation can be interchanged (Clairaut’s theorem), so the Hessian matrix will be symmetric. This will typically be the case for differentiable functions that we work with.
The Hessian is used in some optimization algorithms such as Newton’s method. It is expensive to calculate but can drastically reduce the number of iterations needed to converge to a local minimum by providing information about the curvature of .
4.5 Matrix calculus
Since a lot of optimization reduces to finding points where the gradient vanishes, it is useful to have differentiation rules for matrix and vector expressions. We give some common rules here. Probably the two most important for our purposes are
Note that this second rule is defined only if is square. Furthermore, if is symmetric, we can simplify the result to .
4.5.1 The chain rule
Most functions that we wish to optimize are not completely arbitrary functions, but rather are composed of simpler functions which we know how to handle. The chain rule gives us a way to calculate derivatives for a composite function in terms of the derivatives of the simpler functions that make it up.
The chain rule from single-variable calculus should be familiar:
where denotes function composition. There is a natural generalization of this rule to multivariate functions.
Proposition 8.
Suppose and . Then and
In the special case we have the following corollary since .
Corollary 1.
Suppose and . Then and
4.6 Taylor’s theorem
Taylor’s theorem has natural generalizations to functions of more than one variable. We give the version presented in [1].
Theorem 4.
(Taylor’s theorem) Suppose is continuously differentiable, and let . Then there exists such that
Furthermore, if is twice continuously differentiable, then
and there exists such that
This theorem is used in proofs about conditions for local minima of unconstrained optimization problems. Some of the most important results are given in the next section.
4.7 Conditions for local minima
Proposition 9.
If is a local minimum of and is continuously differentiable in a neighborhood of , then .
Proof.
Let be a local minimum of , and suppose towards a contradiction that . Let , noting that by the continuity of we have
Hence
Thus there exists such that for all . Now we apply Taylor’s theorem: for any , there exists such that
whence it follows that is not a local minimum, a contradiction. Hence . ∎
The proof shows us why the vanishing gradient is necessary for an extremum: if is nonzero, there always exists a sufficiently small step such that . For this reason, is called a descent direction.
Points where the gradient vanishes are called stationary points. Note that not all stationary points are extrema. Consider given by . We have , but the point is the minimum along the line and the maximum along the line . Thus it is neither a local minimum nor a local maximum of . Points such as these, where the gradient vanishes but there is no local extremum, are called saddle points.
We have seen that first-order information (i.e. the gradient) is insufficient to characterize local minima. But we can say more with second-order information (i.e. the Hessian). First we prove a necessary second-order condition for local minima.
Proposition 10.
If is a local minimum of and is twice continuously differentiable in a neighborhood of , then is positive semi-definite.
Proof.
Let be a local minimum of , and suppose towards a contradiction that is not positive semi-definite. Let be such that , noting that by the continuity of we have
Hence
Thus there exists such that for all . Now we apply Taylor’s theorem: for any , there exists such that
where the middle term vanishes because by the previous result. It follows that is not a local minimum, a contradiction. Hence is positive semi-definite. ∎
Now we give sufficient conditions for local minima.
Proposition 11.
Suppose is twice continuously differentiable with positive semi-definite in a neighborhood of , and that . Then is a local minimum of . Furthermore if is positive definite, then is a strict local minimum.
Proof.
Let be an open ball of radius centered at which is contained in the neighborhood. Applying Taylor’s theorem, we have that for any with , there exists such that
The last inequality holds because is positive semi-definite (since ), so . Since for all directions with , we conclude that is a local minimum.
Now further suppose that is strictly positive definite. Since the Hessian is continuous we can choose another ball with radius centered at such that is positive definite for all . Then following the same argument as above (except with a strict inequality now since the Hessian is positive definite) we have for all with . Hence is a strict local minimum. ∎
Note that, perhaps counterintuitively, the conditions and positive semi-definite are not enough to guarantee a local minimum at ! Consider the function . We have and (so the Hessian, which in this case is the matrix , is positive semi-definite). But has a saddle point at . The function is an even worse offender – it has the same gradient and Hessian at , but is a strict local maximum for this function!
For these reasons we require that the Hessian remains positive semi-definite as long as we are close to . Unfortunately, this condition is not practical to check computationally, but in some cases we can verify it analytically (usually by showing that is p.s.d. for all ). Also, if is strictly positive definite, the continuity assumption on implies this condition, so we don’t have to worry.
4.8 Convexity
Convexity is a term that pertains to both sets and functions. For functions, there are different degrees of convexity, and how convex a function is tells us a lot about its minima: do they exist, are they unique, how quickly can we find them using optimization algorithms, etc. In this section, we present basic results regarding convexity, strict convexity, and strong convexity.
4.8.1 Convex sets
A set is convex if
for all and all .
Geometrically, this means that all the points on the line segment between any two points in are also in . See Figure 1 for a visual.
Why do we care whether or not a set is convex? We will see later that the nature of minima can depend greatly on whether or not the feasible set is convex. Undesirable pathological results can occur when we allow the feasible set to be arbitrary, so for proofs we will need to assume that it is convex. Fortunately, we often want to minimize over all of , which is easily seen to be a convex set.
4.8.2 Basics of convex functions
In the remainder of this section, assume unless otherwise noted. We’ll start with the definitions and then give some results.
A function is convex if
for all and all .
If the inequality holds strictly (i.e. rather than ) for all and , then we say that is strictly convex.
A function is strongly convex with parameter (or -strongly convex) if the function
is convex.
These conditions are given in increasing order of strength; strong convexity implies strict convexity which implies convexity.
Geometrically, convexity means that the line segment between two points on the graph of lies on or above the graph itself. See Figure 2 for a visual.
Strict convexity means that the graph of lies strictly above the line segment, except at the segment endpoints. (So actually the function in the figure appears to be strictly convex.)
4.8.3 Consequences of convexity
Why do we care if a function is (strictly/strongly) convex?
Basically, our various notions of convexity have implications about the nature of minima. It should not be surprising that the stronger conditions tell us more about the minima.
Proposition 12.
Let be a convex set. If is convex, then any local minimum of in is also a global minimum.
Proof.
Suppose is convex, and let be a local minimum of in . Then for some neighborhood about , we have for all . Suppose towards a contradiction that there exists such that .
Consider the line segment , noting that by the convexity of . Then by the convexity of ,
for all .
We can pick to be sufficiently close to that ; then by the definition of , but by the above inequality, a contradiction.
It follows that for all , so is a global minimum of in . ∎
Proposition 13.
Let be a convex set. If is strictly convex, then there exists at most one local minimum of in . Consequently, if it exists it is the unique global minimum of in .
Proof.
The second sentence follows from the first, so all we must show is that if a local minimum exists in then it is unique.
Suppose is a local minimum of in , and suppose towards a contradiction that there exists a local minimum such that .
Since is strictly convex, it is convex, so and are both global minima of in by the previous result. Hence . Consider the line segment , which again must lie entirely in . By the strict convexity of ,
for all . But this contradicts the fact that is a global minimum. Therefore if is a local minimum of in , then , so is the unique minimum in . ∎
It is worthwhile to examine how the feasible set affects the optimization problem. We will see why the assumption that is convex is needed in the results above.
Consider the function , which is a strictly convex function. The unique global minimum of this function in is . But let’s see what happens when we change the feasible set .
-
(i)
: This set is actually convex, so we still have a unique global minimum. But it is not the same as the unconstrained minimum!
-
(ii)
: This set is non-convex, and we can see that has no minima in . For any point , one can find another point such that .
-
(iii)
: This set is non-convex, and we can see that there is a local minimum () which is distinct from the global minimum ().
-
(iv)
: This set is non-convex, and we can see that there are two global minima ().
4.8.4 Showing that a function is convex
Hopefully the previous section has convinced the reader that convexity is an important property. Next we turn to the issue of showing that a function is (strictly/strongly) convex. It is of course possible (in principle) to directly show that the condition in the definition holds, but this is usually not the easiest way.
Proposition 14.
Norms are convex.
Proof.
Let be a norm on a vector space . Then for all and ,
where we have used respectively the triangle inequality, the homogeneity of norms, and the fact that and are nonnegative. Hence is convex. ∎
Proposition 15.
Suppose is differentiable. Then is convex if and only if
for all .
Proof.
To-do. ∎
Proposition 16.
Suppose is twice differentiable. Then
-
(i)
is convex if and only if for all .
-
(ii)
If for all , then is strictly convex.
-
(iii)
is -strongly convex if and only if for all .
Proof.
Omitted. ∎
Proposition 17.
If is convex and , then is convex.
Proof.
Suppose is convex and . Then for all ,
so is convex. ∎
Proposition 18.
If and are convex, then is convex. Furthermore, if is strictly convex, then is strictly convex, and if is -strongly convex, then is -strongly convex.
Proof.
Suppose and are convex. Then for all ,
| convexity of | ||||
| convexity of | ||||
so is convex.
If is strictly convex, the second inequality above holds strictly for and , so is strictly convex.
If is -strongly convex, then the function is convex, so is convex. But
so is -strongly convex. ∎
Proposition 19.
If are convex and , then
is convex.
Proof.
Follows from the previous two propositions by induction. ∎
Proposition 20.
If is convex, then is convex for any appropriately-sized and .
Proof.
Suppose is convex and is defined like so. Then for all ,
| convexity of | ||||
Thus is convex. ∎
Proposition 21.
If and are convex, then is convex.
Proof.
Suppose and are convex and is defined like so. Then for all ,
Note that in the first inequality we have used convexity of and plus the fact that implies . In the second inequality we have used the fact that .
Thus is convex. ∎
4.8.5 Examples
A good way to gain intuition about the distinction between convex, strictly convex, and strongly convex functions is to consider examples where the stronger property fails to hold.
Functions that are convex but not strictly convex:
-
(i)
for any . Such a function is called an affine function, and it is both convex and concave. (In fact, a function is affine if and only if it is both convex and concave.) Note that linear functions and constant functions are special cases of affine functions.
-
(ii)
Functions that are strictly but not strongly convex:
-
(i)
. This example is interesting because it is strictly convex but you cannot show this fact via a second-order argument (since ).
-
(ii)
. This example is interesting because it’s bounded below but has no local minimum.
-
(iii)
. This example is interesting because it’s strictly convex but not bounded below.
Functions that are strongly convex:
-
(i)
4.9 Orthogonal projections
We now consider a particular kind of optimization problem that is particularly well-understood and can often be solved in closed form: given some point in an inner product space , find the closest point to in a subspace of . This process is referred to as projection onto a subspace.
The following diagram should make it geometrically clear that, at least in Euclidean space, the solution is intimately related to orthogonality and the Pythagorean theorem:
Here is an arbitrary element of the subspace , and is the point in such that is perpendicular to . The hypotenuse of a right triangle (in this case ) is always longer than either of the legs (in this case and ), and when there always exists such a triangle between , , and .
Our intuition from Euclidean space suggests that the closest point to in has the perpendicularity property described above, and we now show that this is indeed the case.
Proposition 22.
Suppose and . Then is the unique minimizer of over if and only if .
Proof.
Suppose is the unique minimizer of over . That is, for all , with equality only if . Fix and observe that
must have a minimum at as a consequence of this assumption. Thus
giving . Since was arbitrary in , we have as claimed.
Suppose . Observe that for any , because and is closed under subtraction. Under the hypothesis, , so by the Pythagorean theorem,
and in fact the inequality is strict when since this implies . Thus is the unique minimizer of over . ∎
Since a unique minimizer in can be found for any , we can define an operator
Observe that for any , since has distance zero from itself and every other point in has positive distance from . Thus for any (i.e., ) because . The identity is actually one of the defining properties of a projection, the other being linearity.
An immediate consequence of the previous result is that for any , and conversely that is the unique operator that satisfies this property for all . For this reason, is known as an orthogonal projection.
If we choose an orthonormal basis for the target subspace , it is possible to write down a more specific expression for .
Proposition 23.
If is an orthonormal basis for , then
Proof.
Let be an orthonormal basis for , and suppose . Then for all ,
We have shown that the claimed expression, call it , satisfies for every element of the orthonormal basis for . It follows (by linearity of the inner product) that , so the previous result implies . ∎
The fact that is a linear operator (and thus a proper projection, as earlier we showed ) follows readily from this result.
5 Probability
Probability theory provides powerful tools for modeling and dealing with uncertainty.
5.1 Basics
Suppose we have some sort of randomized experiment (e.g. a coin toss, die roll) that has a fixed set of possible outcomes. This set is called the sample space and denoted .
We would like to define probabilities for some events, which are subsets of . The set of events is denoted .777 is required to be a -algebra for technical reasons; see [3]. The complement of the event is another event, .
Then we can define a probability measure which must satisfy
-
(i)
-
(ii)
Countable additivity: for any countable collection of disjoint sets ,
The triple is called a probability space.888 Note that a probability space is simply a measure space in which the measure of the whole space equals 1.
If , we say that occurs almost surely (often abbreviated a.s.).999 This is a probabilist’s version of the measure-theoretic term almost everywhere. , and conversely occurs almost never if .
From these axioms, a number of useful rules can be derived.
Proposition 24.
Let be an event. Then
-
(i)
.
-
(ii)
If is an event and , then .
-
(iii)
Proof.
(i) Using the countable additivity of , we have
To show (ii), suppose and . Then
as claimed.
For (iii): the middle inequality follows from (ii) since . We also have
by countable additivity, which shows . ∎
Proposition 25.
If and are events, then .
Proof.
The key is to break the events up into their various overlapping and non-overlapping parts.
∎
Proposition 26.
If is a countable set of events, disjoint or not, then
This inequality is sometimes referred to as Boole’s inequality or the union bound.
Proof.
Define and for , noting that for all and the are disjoint. Then
where the last inequality follows by monotonicity since for all . ∎
5.1.1 Conditional probability
The conditional probability of event given that event has occurred is written and defined as
assuming .101010 In some cases it is possible to define conditional probability on events of probability zero, but this is significantly more technical so we omit it.
5.1.2 Chain rule
Another very useful tool, the chain rule, follows immediately from this definition:
5.1.3 Bayes’ rule
Taking the equality from above one step further, we arrive at the simple but crucial Bayes’ rule:
It is sometimes beneficial to omit the normalizing constant and write
Under this formulation, is often referred to as the prior, as the posterior, and as the likelihood.
In the context of machine learning, we can use Bayes’ rule to update our “beliefs” (e.g. values of our model parameters) given some data that we’ve observed.
5.2 Random variables
A random variable is some uncertain quantity with an associated probability distribution over the values it can assume.
Formally, a random variable on a probability space is a function111111 The function must be measurable. .121212 More generally, the codomain can be any measurable space, but is the most common case by far and sufficient for our purposes.
We denote the range of by . To give a concrete example (taken from [2]), suppose is the number of heads in two tosses of a fair coin. The sample space is
and is determined completely by the outcome , i.e. . For example, the event is the set of outcomes .
It is common to talk about the values of a random variable without directly referencing its sample space. The two are related by the following definition: the event that the value of lies in some set is
Note that special cases of this definition include being equal to, less than, or greater than some specified value. For example
A word on notation: we write to denote the entire probability distribution of and for the evaluation of the function at a particular value . Hopefully this (reasonably standard) abuse of notation is not too distracting. If is parameterized by some parameters , we write or , unless we are in a Bayesian setting where the parameters are considered a random variable, in which case we condition on the parameters.
5.2.1 The cumulative distribution function
The cumulative distribution function (c.d.f.) gives the probability that a random variable is at most a certain value:
The c.d.f. can be used to give the probability that a variable lies within a certain range:
5.2.2 Discrete random variables
A discrete random variable is a random variable that has a countable range and assumes each value in this range with positive probability. Discrete random variables are completely specified by their probability mass function (p.m.f.) which satisfies
For a discrete , the probability of a particular value is given exactly by its p.m.f.:
5.2.3 Continuous random variables
A continuous random variable is a random variable that has an uncountable range and assumes each value in this range with probability zero. Most of the continuous random variables that one would encounter in practice are absolutely continuous random variables131313 Random variables that are continuous but not absolutely continuous are called singular random variables. We will not discuss them, assuming rather that all continuous random variables admit a density function. , which means that there exists a function that satisfies
The function is called a probability density function (abbreviated p.d.f.) and must satisfy
The values of this function are not themselves probabilities, since they could exceed 1. However, they do have a couple of reasonable interpretations. One is as relative probabilities; even though the probability of each particular value being picked is technically zero, some points are still in a sense more likely than others.
One can also think of the density as determining the probability that the variable will lie in a small range about a given value. This is because, for small ,
using a midpoint approximation to the integral.
Here are some useful identities that follow from the definitions above:
5.2.4 Other kinds of random variables
There are random variables that are neither discrete nor continuous. For example, consider a random variable determined as follows: flip a fair coin, then the value is zero if it comes up heads, otherwise draw a number uniformly at random from . Such a random variable can take on uncountably many values, but only finitely many of these with positive probability. We will not discuss such random variables because they are rather pathological and require measure theory to analyze.
5.3 Joint distributions
Often we have several random variables and we would like to get a distribution over some combination of them. A joint distribution is exactly this. For some random variables , the joint distribution is written and gives probabilities over entire assignments to all the simultaneously.
5.3.1 Independence of random variables
We say that two variables and are independent if their joint distribution factors into their respective distributions, i.e.
We can also define independence for more than two random variables, although it is more complicated. Let be a collection of random variables indexed by , which may be infinite. Then are independent if for every finite subset of indices we have
For example, in the case of three random variables, , we require that as well as , , and .
It is often convenient (though perhaps questionable) to assume that a bunch of random variables are independent and identically distributed (i.i.d.) so that their joint distribution can be factored entirely:
where all share the same p.m.f./p.d.f.
5.3.2 Marginal distributions
If we have a joint distribution over some set of random variables, it is possible to obtain a distribution for a subset of them by “summing out” (or “integrating out” in the continuous case) the variables we don’t care about:
5.4 Great Expectations
If we have some random variable , we might be interested in knowing what is the “average” value of . This concept is captured by the expected value (or mean) , which is defined as
for discrete and as
for continuous .
In words, we are taking a weighted sum of the values that can take on, where the weights are the probabilities of those respective values. The expected value has a physical interpretation as the “center of mass” of the distribution.
5.4.1 Properties of expected value
A very useful property of expectation is that of linearity:
Note that this holds even if the are not independent!
But if they are independent, the product rule also holds:
5.5 Variance
Expectation provides a measure of the “center” of a distribution, but frequently we are also interested in what the “spread” is about that center. We define the variance of a random variable by
In words, this is the average squared deviation of the values of from the mean of . Using a little algebra and the linearity of expectation, it is straightforward to show that
5.5.1 Properties of variance
Variance is not linear (because of the squaring in the definition), but one can show the following:
Basically, multiplicative constants become squared when they are pulled out, and additive constants disappear (since the variance contributed by a constant is zero).
Furthermore, if are uncorrelated141414 We haven’t defined this yet; see the Correlation section below , then
5.5.2 Standard deviation
Variance is a useful notion, but it suffers from that fact the units of variance are not the same as the units of the random variable (again because of the squaring). To overcome this problem we can use standard deviation, which is defined as . The standard deviation of has the same units as .
5.6 Covariance
Covariance is a measure of the linear relationship between two random variables. We denote the covariance between and as , and it is defined to be
Note that the outer expectation must be taken over the joint distribution of and .
Again, the linearity of expectation allows us to rewrite this as
Comparing these formulas to the ones for variance, it is not hard to see that .
A useful property of covariance is that of bilinearity:
5.6.1 Correlation
Normalizing the covariance gives the correlation:
Correlation also measures the linear relationship between two variables, but unlike covariance always lies between and .
Two variables are said to be uncorrelated if because implies that . If two variables are independent, then they are uncorrelated, but the converse does not hold in general.
5.7 Random vectors
So far we have been talking about univariate distributions, that is, distributions of single variables. But we can also talk about multivariate distributions which give distributions of random vectors:
The summarizing quantities we have discussed for single variables have natural generalizations to the multivariate case.
Expectation of a random vector is simply the expectation applied to each component:
The variance is generalized by the covariance matrix:
That is, . Since covariance is symmetric in its arguments, the covariance matrix is also symmetric. It’s also positive semi-definite: for any ,
The inverse of the covariance matrix, , is sometimes called the precision matrix.
5.8 Estimation of Parameters
Now we get into some basic topics from statistics. We make some assumptions about our problem by prescribing a parametric model (e.g. a distribution that describes how the data were generated), then we fit the parameters of the model to the data. How do we choose the values of the parameters?
5.8.1 Maximum likelihood estimation
A common way to fit parameters is maximum likelihood estimation (MLE). The basic principle of MLE is to choose values that “explain” the data best by maximizing the probability/density of the data we’ve seen as a function of the parameters. Suppose we have random variables and corresponding observations . Then
where is the likelihood function
Often, we assume that are i.i.d. Then we can write
At this point, it is usually convenient to take logs, giving rise to the log-likelihood
This is a valid operation because the probabilities/densities are assumed to be positive, and since log is a monotonically increasing function, it preserves ordering. In other words, any maximizer of will also maximize .
For some distributions, it is possible to analytically solve for the maximum likelihood estimator. If is differentiable, setting the derivatives to zero and trying to solve for is a good place to start.
5.8.2 Maximum a posteriori estimation
A more Bayesian way to fit parameters is through maximum a posteriori estimation (MAP). In this technique we assume that the parameters are a random variable, and we specify a prior distribution . Then we can employ Bayes’ rule to compute the posterior distribution of the parameters given the observed data:
Computing the normalizing constant is often intractable, because it involves integrating over the parameter space, which may be very high-dimensional. Fortunately, if we just want the MAP estimate, we don’t care about the normalizing constant! It does not affect which values of maximize the posterior. So we have
Again, if we assume the observations are i.i.d., then we can express this in the equivalent, and possibly friendlier, form
A particularly nice case is when the prior is chosen carefully such that the posterior comes from the same family as the prior. In this case the prior is called a conjugate prior. For example, if the likelihood is binomial and the prior is beta, the posterior is also beta. There are many conjugate priors; the reader may find this table of conjugate priors useful.
5.9 The Gaussian distribution
There are many distributions, but one of particular importance is the Gaussian distribution, also known as the normal distribution. It is a continuous distribution, parameterized by its mean and positive-definite covariance matrix , with density
Note that in the special case , the density is written in the more recognizable form
We write to denote that is normally distributed with mean and variance .
5.9.1 The geometry of multivariate Gaussians
The geometry of the multivariate Gaussian density is intimately related to the geometry of positive definite quadratic forms, so make sure the material in that section is well-understood before tackling this section.
First observe that the p.d.f. of the multivariate Gaussian can be rewritten as
where and . Writing the density in this way, we see that after shifting by the mean , the density is really just a simple function of its precision matrix’s quadratic form.
Here is a key observation: this function is strictly monotonically decreasing in its argument. That is, whenever . Therefore, small values of (which generally correspond to points where is closer to , i.e. ) have relatively high probability densities, and vice-versa. Furthermore, because is strictly monotonic, it is injective, so the -isocontours of are the -isocontours of the function . That is, for any ,
In words, these functions have the same isocontours but different isovalues.
Recall the executive summary of the geometry of positive definite quadratic forms: the isocontours of are ellipsoids such that the axes point in the directions of the eigenvectors of , and the lengths of these axes are proportional to the inverse square roots of the corresponding eigenvalues. Therefore in this case, the isocontours of the density are ellipsoids (centered at ) with axis lengths proportional to the inverse square roots of the eigenvalues of , or equivalently, the square roots of the eigenvalues of .
Acknowledgements
The author would like to thank Michael Franco for suggested clarifications, and Chinmoy Saayujya for catching a typo.
References
- [1] (2006) Numerical optimization. Springer Science+Business Media, New York. Cited by: §4.6.
- [2] (1993) Probability. Springer-Verlag, New York. Cited by: §5.2.
- [3] (2006) A first look at rigorous probability theory (second edition). World Scientific Publishing, Singapore. Cited by: footnote 7.