Mathematics for Machine Learning

Garrett Thomas
Department of Electrical Engineering and Computer Sciences
University of California, Berkeley

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

2 Notation

Notation Meaning
set of real numbers
n set (vector space) of n-tuples of real numbers, endowed with the usual inner product
m×n set (vector space) of m-by-n matrices
δij Kronecker delta, i.e. δij=1 if i=j, 0 otherwise
f(𝐱) gradient of the function f at 𝐱
2f(𝐱) Hessian of the function f at 𝐱
𝐀 transpose of the matrix 𝐀
Ω sample space
(A) probability of event A
p(X) distribution of random variable X
p(x) probability density/mass function evaluated at x
Ac complement of event A
A˙B union of A and B, with the extra requirement that AB=
𝔼[X] expected value of random variable X
Var(X) variance of random variable X
Cov(X,Y) covariance of random variables X and Y

Other notes:

  • Vectors and matrices are in bold (e.g. 𝐱,𝐀). This is true for vectors in n 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 n can be interpreted as an n-by-1 matrix. As such, taking the transpose of a vector is well-defined (and produces a row vector, which is a 1-by-n 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 V 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. V must satisfy

  1. (i)

    There exists an additive identity (written 𝟎) in V such that 𝐱+𝟎=𝐱 for all 𝐱V

  2. (ii)

    For each 𝐱V, there exists an additive inverse (written -𝐱) such that 𝐱+(-𝐱)=𝟎

  3. (iii)

    There exists a multiplicative identity (written 1) in such that 1𝐱=𝐱 for all 𝐱V

  4. (iv)

    Commutativity: 𝐱+𝐲=𝐲+𝐱 for all 𝐱,𝐲V

  5. (v)

    Associativity: (𝐱+𝐲)+𝐳=𝐱+(𝐲+𝐳) and α(β𝐱)=(αβ)𝐱 for all 𝐱,𝐲,𝐳V and α,β

  6. (vi)

    Distributivity: α(𝐱+𝐲)=α𝐱+α𝐲 and (α+β)𝐱=α𝐱+β𝐱 for all 𝐱,𝐲V and α,β

3.1.1 Euclidean space

The quintessential vector space is Euclidean space, which we denote n. The vectors in this space consist of n-tuples of real numbers:

𝐱=(x1,x2,,xn)

For our purposes, it will be useful to think of them as n×1 matrices, or column vectors:

𝐱=[x1x2xn]

Addition and scalar multiplication are defined component-wise on vectors in n:

𝐱+𝐲=[x1+y1xn+yn],α𝐱=[αx1αxn]

Euclidean space is used to mathematically represent physical space, with notions such as distance, length, and angles. Although it becomes hard to visualize for n>3, these concepts generalize mathematically in obvious ways. Even when you’re working in more general settings than n, 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 V is a vector space, then SV is said to be a subspace of V if

  1. (i)

    𝟎S

  2. (ii)

    S is closed under addition: 𝐱,𝐲S implies 𝐱+𝐲S

  3. (iii)

    S is closed under scalar multiplication: 𝐱S,α implies α𝐱S

Note that V is always a subspace of V, 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 T:VW 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 T as

null(T)={𝐱VT𝐱=𝟎}

and the range (or the columnspace if we are considering the matrix form) of T as

range(T)={𝐲W𝐱V such that T𝐱=𝐲}

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 S is a function d:S×S that satisfies

  1. (i)

    d(x,y)0, with equality if and only if x=y

  2. (ii)

    d(x,y)=d(y,x)

  3. (iii)

    d(x,z)d(x,y)+d(y,z) (the so-called triangle inequality)

for all x,y,zS.

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 {xn}S converges to the limit x if for any ϵ>0, there exists N such that d(xn,x)<ϵ for all nN. 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 d(x,y)=|x-y|.

3.3 Normed spaces

Norms generalize the notion of length from Euclidean space.

A norm on a real vector space V is a function :V that satisfies

  1. (i)

    𝐱0, with equality if and only if 𝐱=𝟎

  2. (ii)

    α𝐱=|α|𝐱

  3. (iii)

    𝐱+𝐲𝐱+𝐲 (the triangle inequality again)

for all 𝐱,𝐲V 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 V induces a distance metric on V:

d(𝐱,𝐲)=𝐱-𝐲

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 n:

𝐱1 =i=1n|xi|
𝐱2 =i=1nxi2
𝐱p =(i=1n|xi|p)1p    (p1)
𝐱 =max1in|xi|

Note that the 1- and 2-norms are special cases of the p-norm, and the -norm is the limit of the p-norm as p tends to infinity. We require p1 for the general definition of the p-norm because the triangle inequality fails to hold if p<1. (Try to find a counterexample!)

Here’s a fun fact: for any given finite-dimensional vector space V, all norms on V are equivalent in the sense that for two norms A,B, there exist constants α,β>0 such that

α𝐱A𝐱Bβ𝐱A

for all 𝐱V. 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 V is a function ,:V×V satisfying

  1. (i)

    𝐱,𝐱0, with equality if and only if 𝐱=𝟎

  2. (ii)

    α𝐱+β𝐲,𝐳=α𝐱,𝐳+β𝐲,𝐳

  3. (iii)

    𝐱,𝐲=𝐲,𝐱

for all 𝐱,𝐲,𝐳V and all α,β. A vector space endowed with an inner product is called an inner product space.

Note that any inner product on V induces a norm on V:

𝐱=𝐱,𝐱

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 𝐱,𝐲=0; we write 𝐱𝐲 for shorthand. Orthogonality generalizes the notion of perpendicularity from Euclidean space. If two orthogonal vectors 𝐱 and 𝐲 additionally have unit length (i.e. 𝐱=𝐲=1), then they are described as orthonormal.

The standard inner product on n is given by

𝐱,𝐲=i=1nxiyi=𝐱𝐲

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 1×1 matrix as a scalar. The inner product on n is also often written 𝐱𝐲 (hence the alternate name dot product). The reader can verify that the two-norm 2 on n 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

𝐱+𝐲2=𝐱2+𝐲2
Proof.

Suppose 𝐱𝐲, i.e. 𝐱,𝐲=0. Then

𝐱+𝐲2=𝐱+𝐲,𝐱+𝐲=𝐱,𝐱+𝐲,𝐱+𝐱,𝐲+𝐲,𝐲=𝐱2+𝐲2

as claimed. ∎

3.4.2 Cauchy-Schwarz inequality

This inequality is sometimes useful in proving bounds:

|𝐱,𝐲|𝐱𝐲

for all 𝐱,𝐲V. Equality holds exactly when 𝐱 and 𝐲 are scalar multiples of each other (or equivalently, when they are linearly dependent).

3.5 Transposition

If 𝐀m×n, its transpose 𝐀n×m is given by (𝐀)ij=Aji for each (i,j). 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:

  1. (i)

    (𝐀)=𝐀

  2. (ii)

    (𝐀+𝐁)=𝐀+𝐁

  3. (iii)

    (α𝐀)=α𝐀

  4. (iv)

    (𝐀𝐁)=𝐁𝐀

3.6 Eigenthings

For a square matrix 𝐀n×n, there may be vectors which, when 𝐀 is applied to them, are simply scaled by some constant. We say that a nonzero vector 𝐱n 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

  1. (i)

    For any γ, 𝐱 is an eigenvector of 𝐀+γ𝐈 with eigenvalue λ+γ.

  2. (ii)

    If 𝐀 is invertible, then 𝐱 is an eigenvector of 𝐀-1 with eigenvalue λ-1.

  3. (iii)

    𝐀k𝐱=λk𝐱 for any k (where 𝐀0=𝐈 by definition).

Proof.

(i) follows readily:

(𝐀+γ𝐈)𝐱=𝐀𝐱+γ𝐈𝐱=λ𝐱+γ𝐱=(λ+γ)𝐱

(ii) Suppose 𝐀 is invertible. Then

𝐱=𝐀-1𝐀𝐱=𝐀-1(λ𝐱)=λ𝐀-1𝐱

Dividing by λ, which is valid because the invertibility of 𝐀 implies λ0, gives λ-1𝐱=𝐀-1𝐱.

(iii) The case k0 follows immediately by induction on k. Then the general case k follows by combining the k0 case with (ii). ∎

3.7 Trace

The trace of a square matrix is the sum of its diagonal entries:

tr(𝐀)=i=1nAii

The trace has several nice algebraic properties:

  1. (i)

    tr(𝐀+𝐁)=tr(𝐀)+tr(𝐁)

  2. (ii)

    tr(α𝐀)=αtr(𝐀)

  3. (iii)

    tr(𝐀)=tr(𝐀)

  4. (iv)

    tr(𝐀𝐁𝐂𝐃)=tr(𝐁𝐂𝐃𝐀)=tr(𝐂𝐃𝐀𝐁)=tr(𝐃𝐀𝐁𝐂)

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 tr(𝐀𝐁𝐂𝐃)tr(𝐁𝐀𝐂𝐃) 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):

tr(𝐀)=iλi(𝐀)

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:

  1. (i)

    det(𝐈)=1

  2. (ii)

    det(𝐀)=det(𝐀)

  3. (iii)

    det(𝐀𝐁)=det(𝐀)det(𝐁)

  4. (iv)

    det(𝐀-1)=det(𝐀)-1

  5. (v)

    det(α𝐀)=αndet(𝐀)

Interestingly, the determinant of a matrix is equal to the product of its eigenvalues (repeated according to multiplicity):

det(𝐀)=iλi(𝐀)

3.9 Orthogonal matrices

A matrix 𝐐n×n is said to be orthogonal if its columns are pairwise orthonormal. This definition implies that

𝐐𝐐=𝐐𝐐=𝐈

or equivalently, 𝐐=𝐐-1. 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:

𝐐𝐱2=(𝐐𝐱)(𝐐𝐱)=𝐱𝐱=𝐱2

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 𝐀n×n is said to be symmetric if it is equal to its own transpose (𝐀=𝐀), meaning that Aij=Aji for all (i,j). 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 𝐀n×n is symmetric, then there exists an orthonormal basis for n 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 𝐪1,,𝐪n and their eigenvalues λ1,,λn. Let 𝐐 be an orthogonal matrix with 𝐪1,,𝐪n as its columns, and 𝚲=diag(λ1,,λn). Since by definition 𝐀𝐪i=λi𝐪i for every i, the following relationship holds:

𝐀𝐐=𝐐𝚲

Right-multiplying by 𝐐, we arrive at the decomposition

𝐀=𝐐𝚲𝐐

3.10.1 Rayleigh quotients

Let 𝐀n×n 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

R𝐀(𝐱)=𝐱𝐀𝐱𝐱𝐱

The Rayleigh quotient has a couple of important properties which the reader can (and should!) easily verify from the definition:

  1. (i)

    Scale invariance: for any vector 𝐱𝟎 and any scalar α0, R𝐀(𝐱)=R𝐀(α𝐱).

  2. (ii)

    If 𝐱 is an eigenvector of 𝐀 with eigenvalue λ, then R𝐀(𝐱)=λ.

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 𝐱2=1,

λmin(𝐀)𝐱𝐀𝐱λmax(𝐀)

with equality if and only if 𝐱 is a corresponding eigenvector.

Proof.

We show only the max case because the argument for the min 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 𝐲2=1 since 𝐐 is orthogonal. Hence

max𝐱2=1𝐱𝐀𝐱=max𝐲2=1𝐲𝚲𝐲=maxy12++yn2=1i=1nλiyi2

Written this way, it is clear that 𝐲 maximizes this expression exactly if and only if it satisfies iIyi2=1 where I={i:λi=maxj=1,,nλj=λmax(𝐀)} and yj=0 for jI. That is, I contains the index or indices of the largest eigenvalue. In this case, the maximal value of the expression is

i=1nλiyi2=iIλiyi2=λmax(𝐀)iIyi2=λmax(𝐀)

Then writing 𝐪1,,𝐪n for the columns of 𝐐, we have

𝐱=𝐐𝐐𝐱=𝐐𝐲=i=1nyi𝐪i=iIyi𝐪i

where we have used the matrix-vector product identity.

Recall that 𝐪1,,𝐪n are eigenvectors of 𝐀 and form an orthonormal basis for n. Therefore by construction, the set {𝐪i:iI} forms an orthonormal basis for the eigenspace of λmax(𝐀). Hence 𝐱, which is a linear combination of these, lies in that eigenspace and thus is an eigenvector of 𝐀 corresponding to λmax(𝐀).

We have shown that max𝐱2=1𝐱𝐀𝐱=λmax(𝐀), from which we have the general inequality 𝐱𝐀𝐱λmax(𝐀) for all unit-length 𝐱. ∎

By the scale invariance of the Rayleigh quotient, we immediately have as a corollary (since 𝐱𝐀𝐱=R𝐀(𝐱) for unit 𝐱)

Theorem 3.

(Min-max theorem) For all 𝐱𝟎,

λmin(𝐀)R𝐀(𝐱)λmax(𝐀)

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 𝐱n, 𝐱𝐀𝐱0. Sometimes people write 𝐀0 to indicate that 𝐀 is positive semi-definite.

A symmetric matrix 𝐀 is positive definite if for all nonzero 𝐱n, 𝐱𝐀𝐱>0. Sometimes people write 𝐀0 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 A is positive semi-definite, and let 𝐱 be an eigenvector of 𝐀 with eigenvalue λ. Then

0𝐱𝐀𝐱=𝐱(λ𝐱)=λ𝐱𝐱=λ𝐱22

Since 𝐱𝟎 (by the assumption that it is an eigenvector), we have 𝐱22>0, so we can divide both sides by 𝐱22 to arrive at λ0. If 𝐀 is positive definite, the inequality above holds strictly, so λ>0. 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 𝐱𝟎,

0λmin(𝐀)R𝐀(𝐱)

Since 𝐱𝐀𝐱 matches R𝐀(𝐱) in sign, we conclude that 𝐀 is positive semi-definite. If the eigenvalues of 𝐀 are all strictly positive, then 0<λmin(𝐀), whence it follows that 𝐀 is positive definite. ∎

As an example of how these matrices arise, consider

Proposition 4.

Suppose 𝐀m×n. Then 𝐀𝐀 is positive semi-definite. If null(𝐀)={𝟎}, then 𝐀𝐀 is positive definite.

Proof.

For any 𝐱n,

𝐱(𝐀𝐀)𝐱=(𝐀𝐱)(𝐀𝐱)=𝐀𝐱220

so 𝐀𝐀 is positive semi-definite.

Note that 𝐀𝐱22=0 implies 𝐀𝐱2=0, which in turn implies 𝐀𝐱=𝟎 (recall that this is a property of norms). If null(𝐀)={𝟎}, 𝐀𝐱=𝟎 implies 𝐱=𝟎, so 𝐱(𝐀𝐀)𝐱=0 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 ϵ>0, then 𝐀+ϵ𝐈 is positive definite.

Proof.

Assuming 𝐀 is positive semi-definite and ϵ>0, we have for any 𝐱𝟎 that

𝐱(𝐀+ϵ𝐈)𝐱=𝐱𝐀𝐱+ϵ𝐱𝐈𝐱=𝐱𝐀𝐱0+ϵ𝐱22>0>0

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 ϵ>0.

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 c-isocontour of f is {𝐱domf:f(𝐱)=c}.

Let us consider the special case f(𝐱)=𝐱𝐀𝐱 where 𝐀 is a positive definite matrix. Since 𝐀 is positive definite, it has a unique matrix square root 𝐀12=𝐐𝚲12𝐐, where 𝐐𝚲𝐐 is the eigendecomposition of 𝐀 and 𝚲12=diag(λ1,λn). It is easy to see that this matrix 𝐀12 is positive definite (consider its eigenvalues) and satisfies 𝐀12𝐀12=𝐀. Fixing a value c0, the c-isocontour of f is the set of 𝐱n such that

c=𝐱𝐀𝐱=𝐱𝐀12𝐀12𝐱=𝐀12𝐱22

where we have used the symmetry of 𝐀12. Making the change of variable 𝐳=𝐀12𝐱, we have the condition 𝐳2=c. That is, the values 𝐳 lie on a sphere of radius c. These can be parameterized as 𝐳=c𝐳^ where 𝐳^ has 𝐳^2=1. Then since 𝐀-12=𝐐𝚲-12𝐐, we have

𝐱=𝐀-12𝐳=𝐐𝚲-12𝐐c𝐳^=c𝐐𝚲-12𝐳~

where 𝐳~=𝐐𝐳^ also satisfies 𝐳~2=1 since 𝐐 is orthogonal. Using this parameterization, we see that the solution set {𝐱n:f(𝐱)=c} is the image of the unit sphere {𝐳~n:𝐳~2=1} under the invertible linear map 𝐱=c𝐐𝚲-12𝐳~.

What we have gained with all these manipulations is a clear algebraic understanding of the c-isocontour of f in terms of a sequence of linear transformations applied to a well-understood set. We begin with the unit sphere, then scale every axis i by λi-12, 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 𝐞in that has [𝐞i]j=δij. In the pre-transformed space, this vector points along the axis with length proportional to λi-12. But after applying the rigid transformation 𝐐, the resulting vector points in the direction of the corresponding eigenvector 𝐪i, since

𝐐𝐞i=j=1n[𝐞i]j𝐪j=𝐪i

where we have used the matrix-vector product identity from earlier.

In summary: the isocontours of f(𝐱)=𝐱𝐀𝐱 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 𝐀m×n has an SVD (even non-square matrices)! The decomposition goes as follows:

𝐀=𝐔𝚺𝐕

where 𝐔m×m and 𝐕n×n are orthogonal matrices and 𝚺m×n is a diagonal matrix with the singular values of 𝐀 (denoted σi) on its diagonal.

By convention, the singular values are given in non-increasing order, i.e.

σ1σ2σmin(m,n)0

Only the first r singular values are nonzero, where r 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 σi2 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 𝐱n be a vector and 𝐀m×n a matrix with columns 𝐚1,,𝐚n. Then

𝐀𝐱=i=1nxi𝐚i

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 𝐚m and 𝐛n. By inspection it is not hard to see that such an expression yields an m×n matrix such that

[𝐚𝐛]ij=aibj

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 𝐚1,,𝐚km and 𝐛1,,𝐛kn. Then

=1k𝐚𝐛=𝐀𝐁

where

𝐀=[𝐚1𝐚k],𝐁=[𝐛1𝐛k]
Proof.

For each (i,j), we have

[=1k𝐚𝐛]ij==1k[𝐚𝐛]ij==1k[𝐚]i[𝐛]j==1kAiBj

This last expression should be recognized as an inner product between the ith row of 𝐀 and the jth row of 𝐁, or equivalently the jth column of 𝐁. Hence by the definition of matrix multiplication, it is equal to [𝐀𝐁]ij. ∎

3.13.3 Quadratic forms

Let 𝐀n×n 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 𝐱:

𝐱𝐀𝐱=i=1nj=1nAijxixj

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 𝒳d 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 f:d. A point 𝐱 is said to be a local minimum (resp. local maximum) of f in 𝒳 if f(𝐱)f(𝐲) (resp. f(𝐱)f(𝐲)) for all 𝐲 in some neighborhood N𝒳 about 𝐱.666 A neighborhood about 𝐱 is an open set which contains 𝐱. Furthermore, if f(𝐱)f(𝐲) for all 𝐲𝒳, then 𝐱 is a global minimum of f 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 f is equivalent to minimizing -f, 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 f:d, denoted f, is given by

f=[fx1fxn]  i.e.  [f]i=fxi

Gradients have the following very important property: f(𝐱) points in the direction of steepest ascent from 𝐱. Similarly, -f(𝐱) 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 f:nm is a matrix of first-order partial derivatives:

𝐉f=[f1x1f1xnfmx1fmxn]  i.e.  [𝐉f]ij=fixj

Note the special case m=1, where f=𝐉f.

4.4 The Hessian

The Hessian matrix of f:d is a matrix of second-order partial derivatives:

2f=[2fx122fx1xd2fxdx12fxd2]  i.e.  [2f]ij=2fxixj

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 f.

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 2𝐀𝐱.

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:

(fg)(x)=f(g(x))g(x)

where denotes function composition. There is a natural generalization of this rule to multivariate functions.

Proposition 8.

Suppose f:mk and g:nm. Then fg:nk and

𝐉fg(𝐱)=𝐉f(g(𝐱))𝐉g(𝐱)

In the special case k=1 we have the following corollary since f=𝐉f.

Corollary 1.

Suppose f:m and g:nm. Then fg:n and

(fg)(𝐱)=𝐉g(𝐱)f(g(𝐱))

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 f:d is continuously differentiable, and let 𝐡d. Then there exists t(0,1) such that

f(𝐱+𝐡)=f(𝐱)+f(𝐱+t𝐡)𝐡

Furthermore, if f is twice continuously differentiable, then

f(𝐱+𝐡)=f(𝐱)+012f(𝐱+t𝐡)𝐡dt

and there exists t(0,1) such that

f(𝐱+𝐡)=f(𝐱)+f(𝐱)𝐡+12𝐡2f(𝐱+t𝐡)𝐡

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 f and f is continuously differentiable in a neighborhood of 𝐱*, then f(𝐱*)=𝟎.

Proof.

Let 𝐱* be a local minimum of f, and suppose towards a contradiction that f(𝐱*)𝟎. Let 𝐡=-f(𝐱*), noting that by the continuity of f we have

limt0-f(𝐱*+t𝐡)=-f(𝐱*)=𝐡

Hence

limt0𝐡f(𝐱*+t𝐡)=𝐡f(𝐱*)=-𝐡22<0

Thus there exists T>0 such that 𝐡f(𝐱*+t𝐡)<0 for all t[0,T]. Now we apply Taylor’s theorem: for any t(0,T], there exists t(0,t) such that

f(𝐱*+t𝐡)=f(𝐱*)+t𝐡f(𝐱*+t𝐡)<f(𝐱*)

whence it follows that 𝐱* is not a local minimum, a contradiction. Hence f(𝐱*)=𝟎. ∎

The proof shows us why the vanishing gradient is necessary for an extremum: if f(𝐱) is nonzero, there always exists a sufficiently small step α>0 such that f(𝐱-αf(𝐱)))<f(𝐱). For this reason, -f(𝐱) is called a descent direction.

Points where the gradient vanishes are called stationary points. Note that not all stationary points are extrema. Consider f:2 given by f(x,y)=x2-y2. We have f(𝟎)=𝟎, but the point 𝟎 is the minimum along the line y=0 and the maximum along the line x=0. Thus it is neither a local minimum nor a local maximum of f. 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 f and f is twice continuously differentiable in a neighborhood of 𝐱*, then 2f(𝐱*) is positive semi-definite.

Proof.

Let 𝐱* be a local minimum of f, and suppose towards a contradiction that 2f(𝐱*) is not positive semi-definite. Let 𝐡 be such that 𝐡2f(𝐱*)𝐡<0, noting that by the continuity of 2f we have

limt02f(𝐱*+t𝐡)=2f(𝐱*)

Hence

limt0𝐡2f(𝐱*+t𝐡)𝐡=𝐡2f(𝐱*)𝐡<0

Thus there exists T>0 such that 𝐡2f(𝐱*+t𝐡)𝐡<0 for all t[0,T]. Now we apply Taylor’s theorem: for any t(0,T], there exists t(0,t) such that

f(𝐱*+t𝐡)=f(𝐱*)+t𝐡f(𝐱*)0+12t2𝐡2f(𝐱*+t𝐡)𝐡<f(𝐱*)

where the middle term vanishes because f(𝐱*)=𝟎 by the previous result. It follows that 𝐱* is not a local minimum, a contradiction. Hence 2f(𝐱*) is positive semi-definite. ∎

Now we give sufficient conditions for local minima.

Proposition 11.

Suppose f is twice continuously differentiable with 2f positive semi-definite in a neighborhood of 𝐱*, and that f(𝐱*)=𝟎. Then 𝐱* is a local minimum of f. Furthermore if 2f(𝐱*) is positive definite, then 𝐱* is a strict local minimum.

Proof.

Let B be an open ball of radius r>0 centered at 𝐱* which is contained in the neighborhood. Applying Taylor’s theorem, we have that for any 𝐡 with 𝐡2<r, there exists t(0,1) such that

f(𝐱*+𝐡)=f(𝐱*)+𝐡f(𝐱*)0+12𝐡2f(𝐱*+t𝐡)𝐡f(𝐱*)

The last inequality holds because 2f(𝐱*+t𝐡) is positive semi-definite (since t𝐡2=t𝐡2<𝐡2<r), so 𝐡2f(𝐱*+t𝐡)𝐡0. Since f(𝐱*)f(𝐱*+𝐡) for all directions 𝐡 with 𝐡2<r, we conclude that 𝐱* is a local minimum.

Now further suppose that 2f(𝐱*) is strictly positive definite. Since the Hessian is continuous we can choose another ball B with radius r>0 centered at 𝐱* such that 2f(𝐱) is positive definite for all 𝐱B. Then following the same argument as above (except with a strict inequality now since the Hessian is positive definite) we have f(𝐱*+𝐡)>f(𝐱*) for all 𝐡 with 0<𝐡2<r. Hence 𝐱* is a strict local minimum. ∎

Note that, perhaps counterintuitively, the conditions f(𝐱*)=𝟎 and 2f(𝐱*) positive semi-definite are not enough to guarantee a local minimum at 𝐱*! Consider the function f(x)=x3. We have f(0)=0 and f′′(0)=0 (so the Hessian, which in this case is the 1×1 matrix [0], is positive semi-definite). But f has a saddle point at x=0. The function f(x)=-x4 is an even worse offender – it has the same gradient and Hessian at x=0, but x=0 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 2f(𝐱) is p.s.d. for all 𝐱d). Also, if 2f(𝐱*) is strictly positive definite, the continuity assumption on f 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) A convex set
(b) A non-convex set
Figure 1: What convex sets look like

A set 𝒳d is convex if

t𝐱+(1-t)𝐲𝒳

for all 𝐱,𝐲𝒳 and all t[0,1].

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 d, which is easily seen to be a convex set.

4.8.2 Basics of convex functions

In the remainder of this section, assume f:d unless otherwise noted. We’ll start with the definitions and then give some results.

A function f is convex if

f(t𝐱+(1-t)𝐲)tf(𝐱)+(1-t)f(𝐲)

for all 𝐱,𝐲domf and all t[0,1].

If the inequality holds strictly (i.e. < rather than ) for all t(0,1) and 𝐱𝐲, then we say that f is strictly convex.

A function f is strongly convex with parameter m (or m-strongly convex) if the function

𝐱f(𝐱)-m2𝐱22

is convex.

These conditions are given in increasing order of strength; strong convexity implies strict convexity which implies convexity.

Figure 2: What convex functions look like

Geometrically, convexity means that the line segment between two points on the graph of f lies on or above the graph itself. See Figure 2 for a visual.

Strict convexity means that the graph of f 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 f is convex, then any local minimum of f in 𝒳 is also a global minimum.

Proof.

Suppose f is convex, and let 𝐱* be a local minimum of f in 𝒳. Then for some neighborhood N𝒳 about 𝐱*, we have f(𝐱)f(𝐱*) for all 𝐱N. Suppose towards a contradiction that there exists 𝐱~𝒳 such that f(𝐱~)<f(𝐱*).

Consider the line segment 𝐱(t)=t𝐱*+(1-t)𝐱~,t[0,1], noting that 𝐱(t)𝒳 by the convexity of 𝒳. Then by the convexity of f,

f(𝐱(t))tf(𝐱*)+(1-t)f(𝐱~)<tf(𝐱*)+(1-t)f(𝐱*)=f(𝐱*)

for all t(0,1).

We can pick t to be sufficiently close to 1 that 𝐱(t)N; then f(𝐱(t))f(𝐱*) by the definition of N, but f(𝐱(t))<f(𝐱*) by the above inequality, a contradiction.

It follows that f(𝐱*)f(𝐱) for all 𝐱𝒳, so 𝐱* is a global minimum of f in 𝒳. ∎

Proposition 13.

Let 𝒳 be a convex set. If f is strictly convex, then there exists at most one local minimum of f in 𝒳. Consequently, if it exists it is the unique global minimum of f 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 f in 𝒳, and suppose towards a contradiction that there exists a local minimum 𝐱~𝒳 such that 𝐱~𝐱*.

Since f is strictly convex, it is convex, so 𝐱* and 𝐱~ are both global minima of f in 𝒳 by the previous result. Hence f(𝐱*)=f(𝐱~). Consider the line segment 𝐱(t)=t𝐱*+(1-t)𝐱~,t[0,1], which again must lie entirely in 𝒳. By the strict convexity of f,

f(𝐱(t))<tf(𝐱*)+(1-t)f(𝐱~)=tf(𝐱*)+(1-t)f(𝐱*)=f(𝐱*)

for all t(0,1). But this contradicts the fact that 𝐱* is a global minimum. Therefore if 𝐱~ is a local minimum of f 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 f(x)=x2, which is a strictly convex function. The unique global minimum of this function in is x=0. But let’s see what happens when we change the feasible set 𝒳.

  1. (i)

    𝒳={1}: This set is actually convex, so we still have a unique global minimum. But it is not the same as the unconstrained minimum!

  2. (ii)

    𝒳={0}: This set is non-convex, and we can see that f has no minima in 𝒳. For any point x𝒳, one can find another point y𝒳 such that f(y)<f(x).

  3. (iii)

    𝒳=(-,-1][0,): This set is non-convex, and we can see that there is a local minimum (x=-1) which is distinct from the global minimum (x=0).

  4. (iv)

    𝒳=(-,-1][1,): This set is non-convex, and we can see that there are two global minima (x=±1).

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 V. Then for all 𝐱,𝐲V and t[0,1],

t𝐱+(1-t)𝐲t𝐱+(1-t)𝐲=|t|𝐱+|1-t|𝐲=t𝐱+(1-t)𝐲

where we have used respectively the triangle inequality, the homogeneity of norms, and the fact that t and 1-t are nonnegative. Hence is convex. ∎

Proposition 15.

Suppose f is differentiable. Then f is convex if and only if

f(𝐲)f(𝐱)+f(𝐱),𝐲-𝐱

for all 𝐱,𝐲domf.

Proof.

To-do. ∎

Proposition 16.

Suppose f is twice differentiable. Then

  1. (i)

    f is convex if and only if 2f(𝐱)0 for all 𝐱domf.

  2. (ii)

    If 2f(𝐱)0 for all 𝐱domf, then f is strictly convex.

  3. (iii)

    f is m-strongly convex if and only if 2f(𝐱)mI for all 𝐱domf.

Proof.

Omitted. ∎

Proposition 17.

If f is convex and α0, then αf is convex.

Proof.

Suppose f is convex and α0. Then for all 𝐱,𝐲dom(αf)=domf,

(αf)(t𝐱+(1-t)𝐲) =αf(t𝐱+(1-t)𝐲)
α(tf(𝐱)+(1-t)f(𝐲))
=t(αf(𝐱))+(1-t)(αf(𝐲))
=t(αf)(𝐱)+(1-t)(αf)(𝐲)

so αf is convex. ∎

Proposition 18.

If f and g are convex, then f+g is convex. Furthermore, if g is strictly convex, then f+g is strictly convex, and if g is m-strongly convex, then f+g is m-strongly convex.

Proof.

Suppose f and g are convex. Then for all 𝐱,𝐲dom(f+g)=domfdomg,

(f+g)(t𝐱+(1-t)𝐲) =f(t𝐱+(1-t)𝐲)+g(t𝐱+(1-t)𝐲)
tf(𝐱)+(1-t)f(𝐲)+g(t𝐱+(1-t)𝐲) convexity of f
tf(𝐱)+(1-t)f(𝐲)+tg(𝐱)+(1-t)g(𝐲) convexity of g
=t(f(𝐱)+g(𝐱))+(1-t)(f(𝐲)+g(𝐲))
=t(f+g)(𝐱)+(1-t)(f+g)(𝐲)

so f+g is convex.

If g is strictly convex, the second inequality above holds strictly for 𝐱𝐲 and t(0,1), so f+g is strictly convex.

If g is m-strongly convex, then the function h(𝐱)g(𝐱)-m2𝐱22 is convex, so f+h is convex. But

(f+h)(𝐱)f(𝐱)+h(𝐱)f(𝐱)+g(𝐱)-m2𝐱22(f+g)(𝐱)-m2𝐱22

so f+g is m-strongly convex. ∎

Proposition 19.

If f1,,fn are convex and α1,,αn0, then

i=1nαifi

is convex.

Proof.

Follows from the previous two propositions by induction. ∎

Proposition 20.

If f is convex, then g(𝐱)f(𝐀𝐱+𝐛) is convex for any appropriately-sized 𝐀 and 𝐛.

Proof.

Suppose f is convex and g is defined like so. Then for all 𝐱,𝐲domg,

g(t𝐱+(1-t)𝐲) =f(𝐀(t𝐱+(1-t)𝐲)+𝐛)
=f(t𝐀𝐱+(1-t)𝐀𝐲+𝐛)
=f(t𝐀𝐱+(1-t)𝐀𝐲+t𝐛+(1-t)𝐛)
=f(t(𝐀𝐱+𝐛)+(1-t)(𝐀𝐲+𝐛))
tf(𝐀𝐱+𝐛)+(1-t)f(𝐀𝐲+𝐛) convexity of f
=tg(𝐱)+(1-t)g(𝐲)

Thus g is convex. ∎

Proposition 21.

If f and g are convex, then h(𝐱)max{f(𝐱),g(𝐱)} is convex.

Proof.

Suppose f and g are convex and h is defined like so. Then for all 𝐱,𝐲domh,

h(t𝐱+(1-t)𝐲) =max{f(t𝐱+(1-t)𝐲),g(t𝐱+(1-t)𝐲)}
max{tf(𝐱)+(1-t)f(𝐲),tg(𝐱)+(1-t)g(𝐲)}
max{tf(𝐱),tg(𝐱)}+max{(1-t)f(𝐲),(1-t)g(𝐲)}
=tmax{f(𝐱),g(𝐱)}+(1-t)max{f(𝐲),g(𝐲)}
=th(𝐱)+(1-t)h(𝐲)

Note that in the first inequality we have used convexity of f and g plus the fact that ac,bd implies max{a,b}max{c,d}. In the second inequality we have used the fact that max{a+b,c+d}max{a,c}+max{b,d}.

Thus h 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:

  1. (i)

    f(𝐱)=𝐰𝐱+α for any 𝐰d,α. 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.

  2. (ii)

    f(𝐱)=𝐱1

Functions that are strictly but not strongly convex:

  1. (i)

    f(x)=x4. This example is interesting because it is strictly convex but you cannot show this fact via a second-order argument (since f′′(0)=0).

  2. (ii)

    f(x)=exp(x). This example is interesting because it’s bounded below but has no local minimum.

  3. (iii)

    f(x)=-logx. This example is interesting because it’s strictly convex but not bounded below.

Functions that are strongly convex:

  1. (i)

    f(𝐱)=𝐱22

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 V, find the closest point to 𝐱 in a subspace S of V. 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 S, and 𝐲* is the point in S such that 𝐱-𝐲* is perpendicular to S. 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 S has the perpendicularity property described above, and we now show that this is indeed the case.

Proposition 22.

Suppose 𝐱V and 𝐲S. Then 𝐲* is the unique minimizer of 𝐱-𝐲 over 𝐲S if and only if 𝐱-𝐲*S.

Proof.

() Suppose 𝐲* is the unique minimizer of 𝐱-𝐲 over 𝐲S. That is, 𝐱-𝐲*𝐱-𝐲 for all 𝐲S, with equality only if 𝐲=𝐲*. Fix 𝐯S and observe that

g(t) :=𝐱-𝐲*+t𝐯2
=𝐱-𝐲*+t𝐯,𝐱-𝐲*+t𝐯
=𝐱-𝐲*,𝐱-𝐲*-2t𝐱-𝐲*,𝐯+t2𝐯,𝐯
=𝐱-𝐲*2-2t𝐱-𝐲*,𝐯+t2𝐯2

must have a minimum at t=0 as a consequence of this assumption. Thus

0=g(0)=-2𝐱-𝐲*,𝐯+2t𝐯2|t=0=-2𝐱-𝐲*,𝐯

giving 𝐱-𝐲*𝐯. Since 𝐯 was arbitrary in S, we have 𝐱-𝐲*S as claimed.

() Suppose 𝐱-𝐲*S. Observe that for any 𝐲S, 𝐲*-𝐲S because 𝐲*S and S is closed under subtraction. Under the hypothesis, 𝐱-𝐲*𝐲*-𝐲, so by the Pythagorean theorem,

𝐱-𝐲=𝐱-𝐲*+𝐲*-𝐲=𝐱-𝐲*+𝐲*-𝐲𝐱-𝐲*

and in fact the inequality is strict when 𝐲𝐲* since this implies 𝐲*-𝐲>0. Thus 𝐲* is the unique minimizer of 𝐱-𝐲 over 𝐲S. ∎

Since a unique minimizer in S can be found for any 𝐱V, we can define an operator

P𝐱=argmin𝐲S𝐱-𝐲

Observe that P𝐲=𝐲 for any 𝐲S, since 𝐲 has distance zero from itself and every other point in S has positive distance from 𝐲. Thus P(P𝐱)=P𝐱 for any 𝐱 (i.e., P2=P) because P𝐱S. The identity P2=P is actually one of the defining properties of a projection, the other being linearity.

An immediate consequence of the previous result is that 𝐱-P𝐱S for any 𝐱V, and conversely that P is the unique operator that satisfies this property for all 𝐱V. For this reason, P is known as an orthogonal projection.

If we choose an orthonormal basis for the target subspace S, it is possible to write down a more specific expression for P.

Proposition 23.

If 𝐞1,,𝐞m is an orthonormal basis for S, then

P𝐱=i=1m𝐱,𝐞i𝐞i
Proof.

Let 𝐞1,,𝐞m be an orthonormal basis for S, and suppose 𝐱V. Then for all j=1,,m,

𝐱-i=1m𝐱,𝐞i𝐞i,𝐞j =𝐱,𝐞j-i=1m𝐱,𝐞i𝐞i,𝐞jδij
=𝐱,𝐞j-𝐱,𝐞j
=0

We have shown that the claimed expression, call it P~𝐱, satisfies 𝐱-P~𝐱𝐞j for every element 𝐞j of the orthonormal basis for S. It follows (by linearity of the inner product) that 𝐱-P~𝐱S, so the previous result implies P=P~. ∎

The fact that P is a linear operator (and thus a proper projection, as earlier we showed P2=P) 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 A is another event, Ac=ΩA.

Then we can define a probability measure :[0,1] which must satisfy

  1. (i)

    (Ω)=1

  2. (ii)

    Countable additivity: for any countable collection of disjoint sets {Ai},

    (iAi)=i(Ai)

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 (A)=1, we say that A occurs almost surely (often abbreviated a.s.).999 This is a probabilist’s version of the measure-theoretic term almost everywhere. , and conversely A occurs almost never if (A)=0.

From these axioms, a number of useful rules can be derived.

Proposition 24.

Let A be an event. Then

  1. (i)

    (A𝑐)=1-(A).

  2. (ii)

    If B is an event and BA, then (B)(A).

  3. (iii)

    0=()(A)(Ω)=1

Proof.

(i) Using the countable additivity of , we have

(A)+(Ac)=(A˙Ac)=(Ω)=1

To show (ii), suppose B and BA. Then

(A)=(B˙(AB))=(B)+(AB)(B)

as claimed.

For (iii): the middle inequality follows from (ii) since AΩ. We also have

()=(˙)=()+()

by countable additivity, which shows ()=0. ∎

Proposition 25.

If A and B are events, then (AB)=(A)+(B)-(AB).

Proof.

The key is to break the events up into their various overlapping and non-overlapping parts.

(AB) =((AB)˙(AB)˙(BA))
=(AB)+(AB)+(BA)
=(AB)+(A)-(AB)+(B)-(AB)
=(A)+(B)-(AB)

Proposition 26.

If {Ai} is a countable set of events, disjoint or not, then

(iAi)i(Ai)

This inequality is sometimes referred to as Boole’s inequality or the union bound.

Proof.

Define B1=A1 and Bi=Ai(j<iAj) for i>1, noting that jiBj=jiAj for all i and the Bi are disjoint. Then

(iAi)=(iBi)=i(Bi)i(Ai)

where the last inequality follows by monotonicity since BiAi for all i. ∎

5.1.1 Conditional probability

The conditional probability of event A given that event B has occurred is written (A|B) and defined as

(A|B)=(AB)(B)

assuming (B)>0.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:

(AB)=(A|B)(B)=(B|A)(A)

5.1.3 Bayes’ rule

Taking the equality from above one step further, we arrive at the simple but crucial Bayes’ rule:

(A|B)=(B|A)(A)(B)

It is sometimes beneficial to omit the normalizing constant and write

(A|B)(A)(B|A)

Under this formulation, (A) is often referred to as the prior, (A|B) as the posterior, and (B|A) 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. X:Ω.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 X by X(Ω)={X(ω):ωΩ}. To give a concrete example (taken from [2]), suppose X is the number of heads in two tosses of a fair coin. The sample space is

Ω={hh,tt,ht,th}

and X is determined completely by the outcome ω, i.e. X=X(ω). For example, the event X=1 is the set of outcomes {ht,th}.

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 X lies in some set S is

XS={ωΩ:X(ω)S}

Note that special cases of this definition include X being equal to, less than, or greater than some specified value. For example

(X=x)=({ωΩ:X(ω)=x})

A word on notation: we write p(X) to denote the entire probability distribution of X and p(x) for the evaluation of the function p at a particular value xX(Ω). Hopefully this (reasonably standard) abuse of notation is not too distracting. If p is parameterized by some parameters θ, we write p(X;θ) or p(x;θ), 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:

F(x)=(Xx)

The c.d.f. can be used to give the probability that a variable lies within a certain range:

(a<Xb)=F(b)-F(a)

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.) p:X(Ω)[0,1] which satisfies

xX(Ω)p(x)=1

For a discrete X, the probability of a particular value is given exactly by its p.m.f.:

(X=x)=p(x)

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 p:[0,) that satisfies

F(x)-xp(z)dz

The function p is called a probability density function (abbreviated p.d.f.) and must satisfy

-p(x)dx=1

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 ϵ>0,

(x-ϵXx+ϵ)=x-ϵx+ϵp(z)dz2ϵp(x)

using a midpoint approximation to the integral.

Here are some useful identities that follow from the definitions above:

(aXb) =abp(x)dx
p(x) =F(x)

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 [1,2]. 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 X1,,Xn, the joint distribution is written p(X1,,Xn) and gives probabilities over entire assignments to all the Xi simultaneously.

5.3.1 Independence of random variables

We say that two variables X and Y are independent if their joint distribution factors into their respective distributions, i.e.

p(X,Y)=p(X)p(Y)

We can also define independence for more than two random variables, although it is more complicated. Let {Xi}iI be a collection of random variables indexed by I, which may be infinite. Then {Xi} are independent if for every finite subset of indices i1,,ikI we have

p(Xi1,,Xik)=j=1kp(Xij)

For example, in the case of three random variables, X,Y,Z, we require that p(X,Y,Z)=p(X)p(Y)p(Z) as well as p(X,Y)=p(X)p(Y), p(X,Z)=p(X)p(Z), and p(Y,Z)=p(Y)p(Z).

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:

p(X1,,Xn)=i=1np(Xi)

where X1,,Xn 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:

p(X)=yp(X,y)

5.4 Great Expectations

If we have some random variable X, we might be interested in knowing what is the “average” value of X. This concept is captured by the expected value (or mean) 𝔼[X], which is defined as

𝔼[X]=xX(Ω)xp(x)

for discrete X and as

𝔼[X]=-xp(x)dx

for continuous X.

In words, we are taking a weighted sum of the values that X 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:

𝔼[i=1nαiXi+β]=i=1nαi𝔼[Xi]+β

Note that this holds even if the Xi are not independent!

But if they are independent, the product rule also holds:

𝔼[i=1nXi]=i=1n𝔼[Xi]

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 Var(X) of a random variable X by

Var(X)=𝔼[(X-𝔼[X])2]

In words, this is the average squared deviation of the values of X from the mean of X. Using a little algebra and the linearity of expectation, it is straightforward to show that

Var(X)=𝔼[X2]-𝔼[X]2

5.5.1 Properties of variance

Variance is not linear (because of the squaring in the definition), but one can show the following:

Var(αX+β)=α2Var(X)

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 X1,,Xn are uncorrelated141414 We haven’t defined this yet; see the Correlation section below , then

Var(X1++Xn)=Var(X1)++Var(Xn)

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 Var(X). The standard deviation of X has the same units as X.

5.6 Covariance

Covariance is a measure of the linear relationship between two random variables. We denote the covariance between X and Y as Cov(X,Y), and it is defined to be

Cov(X,Y)=𝔼[(X-𝔼[X])(Y-𝔼[Y])]

Note that the outer expectation must be taken over the joint distribution of X and Y.

Again, the linearity of expectation allows us to rewrite this as

Cov(X,Y)=𝔼[XY]-𝔼[X]𝔼[Y]

Comparing these formulas to the ones for variance, it is not hard to see that Var(X)=Cov(X,X).

A useful property of covariance is that of bilinearity:

Cov(αX+βY,Z) =αCov(X,Z)+βCov(Y,Z)
Cov(X,αY+βZ) =αCov(X,Y)+βCov(X,Z)

5.6.1 Correlation

Normalizing the covariance gives the correlation:

ρ(X,Y)=Cov(X,Y)Var(X)Var(Y)

Correlation also measures the linear relationship between two variables, but unlike covariance always lies between -1 and 1.

Two variables are said to be uncorrelated if Cov(X,Y)=0 because Cov(X,Y)=0 implies that ρ(X,Y)=0. 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:

𝐗=[X1Xn]

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:

𝔼[𝐗]=[𝔼[X1]𝔼[Xn]]

The variance is generalized by the covariance matrix:

𝚺=𝔼[(𝐗-𝔼[𝐗])(𝐗-𝔼[𝐗])]=[Var(X1)Cov(X1,X2)Cov(X1,Xn)Cov(X2,X1)Var(X2)Cov(X2,Xn)Cov(Xn,X1)Cov(Xn,X2)Var(Xn)]

That is, Σij=Cov(Xi,Xj). Since covariance is symmetric in its arguments, the covariance matrix is also symmetric. It’s also positive semi-definite: for any 𝐱,

𝐱𝚺𝐱=𝐱𝔼[(𝐗-𝔼[𝐗])(𝐗-𝔼[𝐗])]𝐱=𝔼[𝐱(𝐗-𝔼[𝐗])(𝐗-𝔼[𝐗])𝐱]=𝔼[((𝐗-𝔼[𝐗])𝐱)2]0

The inverse of the covariance matrix, 𝚺-1, 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 X1,,Xn and corresponding observations x1,,xn. Then

θ^mle=argmaxθ(θ)

where is the likelihood function

(θ)=p(x1,,xn;θ)

Often, we assume that X1,,Xn are i.i.d. Then we can write

p(x1,,xn;θ)=i=1np(xi;θ)

At this point, it is usually convenient to take logs, giving rise to the log-likelihood

log(θ)=i=1nlogp(xi;θ)

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 log will also maximize .

For some distributions, it is possible to analytically solve for the maximum likelihood estimator. If log 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 p(θ). Then we can employ Bayes’ rule to compute the posterior distribution of the parameters given the observed data:

p(θ|x1,,xn)p(θ)p(x1,,xn|θ)

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

θ^map=argmaxθp(θ)p(x1,,xn|θ)

Again, if we assume the observations are i.i.d., then we can express this in the equivalent, and possibly friendlier, form

θ^map=argmaxθ(logp(θ)+i=1nlogp(xi|θ))

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 𝝁d and positive-definite covariance matrix 𝚺d×d, with density

p(𝐱;𝝁,𝚺)=1(2π)ddet(𝚺)exp(-12(𝐱-𝝁)𝚺-1(𝐱-𝝁))

Note that in the special case d=1, the density is written in the more recognizable form

p(x;μ,σ2)=12πσ2exp(-(x-μ)22σ2)

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

p(𝐱;𝝁,𝚺)=g(𝐱~𝚺-1𝐱~)

where 𝐱~=𝐱-𝝁 and g(z)=[(2π)ddet(𝚺)]-12exp(-z2). 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 g is strictly monotonically decreasing in its argument. That is, g(a)>g(b) whenever a<b. Therefore, small values of 𝐱~𝚺-1𝐱~ (which generally correspond to points where 𝐱~ is closer to 𝟎, i.e. 𝐱𝝁) have relatively high probability densities, and vice-versa. Furthermore, because g is strictly monotonic, it is injective, so the c-isocontours of p(𝐱;𝝁,𝚺) are the g-1(c)-isocontours of the function 𝐱𝐱~𝚺-1𝐱~. That is, for any c,

{𝐱d:p(𝐱;𝝁,𝚺)=c}={𝐱d:𝐱~𝚺-1𝐱~=g-1(c)}

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 f(𝐱)=𝐱𝐀𝐱 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 𝚺-1, 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] J. Nocedal and S. J. Wright (2006) Numerical optimization. Springer Science+Business Media, New York. Cited by: §4.6.
  • [2] J. Pitman (1993) Probability. Springer-Verlag, New York. Cited by: §5.2.
  • [3] J. S. Rosenthal (2006) A first look at rigorous probability theory (second edition). World Scientific Publishing, Singapore. Cited by: footnote 7.