Mathematics For Machine Learning

Download as pdf or txt
Download as pdf or txt
You are on page 1of 298

MATHEMATICS FOR

MACHINE LEARNING

Marc Peter Deisenroth


A. Aldo Faisal
Cheng Soon Ong
Contents

Foreword 1

Part I Mathematical Foundations 9

1 Introduction and Motivation 11


1.1 Finding Words for Intuitions 12
1.2 Two Ways to Read This Book 13
1.3 Exercises and Feedback 16

2 Linear Algebra 17
2.1 Systems of Linear Equations 19
2.2 Matrices 22
2.3 Solving Systems of Linear Equations 27
2.4 Vector Spaces 35
2.5 Linear Independence 40
2.6 Basis and Rank 44
2.7 Linear Mappings 48
2.8 Affine Spaces 61
2.9 Further Reading 63
Exercises 64

3 Analytic Geometry 70
3.1 Norms 71
3.2 Inner Products 72
3.3 Lengths and Distances 75
3.4 Angles and Orthogonality 76
3.5 Orthonormal Basis 78
3.6 Orthogonal Complement 79
3.7 Inner Product of Functions 80
3.8 Orthogonal Projections 81
3.9 Rotations 91
3.10 Further Reading 94
Exercises 96

4 Matrix Decompositions 98
4.1 Determinant and Trace 99

i
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
ii Contents

4.2 Eigenvalues and Eigenvectors 105


4.3 Cholesky Decomposition 114
4.4 Eigendecomposition and Diagonalization 115
4.5 Singular Value Decomposition 119
4.6 Matrix Approximation 129
4.7 Matrix Phylogeny 134
4.8 Further Reading 135
Exercises 137

5 Vector Calculus 139


5.1 Differentiation of Univariate Functions 141
5.2 Partial Differentiation and Gradients 146
5.3 Gradients of Vector-Valued Functions 149
5.4 Gradients of Matrices 155
5.5 Useful Identities for Computing Gradients 158
5.6 Backpropagation and Automatic Differentiation 159
5.7 Higher-Order Derivatives 164
5.8 Linearization and Multivariate Taylor Series 165
5.9 Further Reading 170
Exercises 170

6 Probability and Distributions 172


6.1 Construction of a Probability Space 172
6.2 Discrete and Continuous Probabilities 178
6.3 Sum Rule, Product Rule, and Bayes’ Theorem 183
6.4 Summary Statistics and Independence 186
6.5 Gaussian Distribution 197
6.6 Conjugacy and the Exponential Family 205
6.7 Change of Variables/Inverse Transform 214
6.8 Further Reading 221
Exercises 222

7 Continuous Optimization 225


7.1 Optimization Using Gradient Descent 227
7.2 Constrained Optimization and Lagrange Multipliers 233
7.3 Convex Optimization 236
7.4 Further Reading 246
Exercises 247

Part II Central Machine Learning Problems 249

8 When Models Meet Data 251


8.1 Data, Models, and Learning 251
8.2 Empirical Risk Minimization 258
8.3 Parameter Estimation 265
8.4 Probabilistic Modeling and Inference 272
8.5 Directed Graphical Models 278

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Contents iii

8.6 Model Selection 283

9 Linear Regression 289


9.1 Problem Formulation 291
9.2 Parameter Estimation 292
9.3 Bayesian Linear Regression 303
9.4 Maximum Likelihood as Orthogonal Projection 313
9.5 Further Reading 315

10 Dimensionality Reduction with Principal Component Analysis 317


10.1 Problem Setting 318
10.2 Maximum Variance Perspective 320
10.3 Projection Perspective 325
10.4 Eigenvector Computation and Low-Rank Approximations 333
10.5 PCA in High Dimensions 335
10.6 Key Steps of PCA in Practice 336
10.7 Latent Variable Perspective 339
10.8 Further Reading 343

11 Density Estimation with Gaussian Mixture Models 348


11.1 Gaussian Mixture Model 349
11.2 Parameter Learning via Maximum Likelihood 350
11.3 EM Algorithm 360
11.4 Latent-Variable Perspective 363
11.5 Further Reading 368

12 Classification with Support Vector Machines 370


12.1 Separating Hyperplanes 372
12.2 Primal Support Vector Machine 374
12.3 Dual Support Vector Machine 383
12.4 Kernels 388
12.5 Numerical Solution 390
12.6 Further Reading 392

References 395
Index 407

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


Foreword

Machine learning is the latest in a long line of attempts to distill human


knowledge and reasoning into a form that is suitable for constructing ma-
chines and engineering automated systems. As machine learning becomes
more ubiquitous and its software packages become easier to use, it is nat-
ural and desirable that the low-level technical details are abstracted away
and hidden from the practitioner. However, this brings with it the danger
that a practitioner becomes unaware of the design decisions and, hence,
the limits of machine learning algorithms.
The enthusiastic practitioner who is interested to learn more about the
magic behind successful machine learning algorithms currently faces a
daunting set of pre-requisite knowledge:

Programming languages and data analysis tools


Large-scale computation and the associated frameworks
Mathematics and statistics and how machine learning builds on it

At universities, introductory courses on machine learning tend to spend


early parts of the course covering some of these pre-requisites. For histori-
cal reasons, courses in machine learning tend to be taught in the computer
science department, where students are often trained in the first two areas
of knowledge, but not so much in mathematics and statistics.
Current machine learning textbooks primarily focus on machine learn-
ing algorithms and methodologies and assume that the reader is com-
petent in mathematics and statistics. Therefore, these books only spend
one or two chapters of background mathematics, either at the beginning
of the book or as appendices. We have found many people who want to
delve into the foundations of basic machine learning methods who strug-
gle with the mathematical knowledge required to read a machine learning
textbook. Having taught undergraduate and graduate courses at universi-
ties, we find that the gap between high school mathematics and the math-
ematics level required to read a standard machine learning textbook is too
big for many people.
This book brings the mathematical foundations of basic machine learn-
ing concepts to the fore and collects the information in a single place so
that this skills gap is narrowed or even closed.

1
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
2 Foreword

Why Another Book on Machine Learning?


Machine learning builds upon the language of mathematics to express
concepts that seem intuitively obvious but that are surprisingly difficult
to formalize. Once formalized properly, we can gain insights into the task
we want to solve. One common complaint of students of mathematics
around the globe is that the topics covered seem to have little relevance
to practical problems. We believe that machine learning is an obvious and
direct motivation for people to learn mathematics.
This book is intended to be a guidebook to the vast mathematical lit-
“Math is linked in erature that forms the foundations of modern machine learning. We mo-
the popular mind tivate the need for mathematical concepts by directly pointing out their
with phobia and
usefulness in the context of fundamental machine learning problems. In
anxiety. You’d think
we’re discussing the interest of keeping the book short, many details and more advanced
spiders.” (Strogatz, concepts have been left out. Equipped with the basic concepts presented
2014, page 281) here, and how they fit into the larger context of machine learning, the
reader can find numerous resources for further study, which we provide at
the end of the respective chapters. For readers with a mathematical back-
ground, this book provides a brief but precisely stated glimpse of machine
learning. In contrast to other books that focus on methods and models
of machine learning (MacKay, 2003; Bishop, 2006; Alpaydin, 2010; Bar-
ber, 2012; Murphy, 2012; Shalev-Shwartz and Ben-David, 2014; Rogers
and Girolami, 2016) or programmatic aspects of machine learning (Müller
and Guido, 2016; Raschka and Mirjalili, 2017; Chollet and Allaire, 2018),
we provide only four representative examples of machine learning algo-
rithms. Instead, we focus on the mathematical concepts behind the models
themselves. We hope that readers will be able to gain a deeper understand-
ing of the basic questions in machine learning and connect practical ques-
tions arising from the use of machine learning with fundamental choices
in the mathematical model.
We do not aim to write a classical machine learning book. Instead, our
intention is to provide the mathematical background, applied to four cen-
tral machine learning problems, to make it easier to read other machine
learning textbooks.

Who Is the Target Audience?


As applications of machine learning become widespread in society, we
believe that everybody should have some understanding of its underlying
principles. This book is written in an academic mathematical style, which
enables us to be precise about the concepts behind machine learning. We
encourage readers unfamiliar with this seemingly terse style to persevere
and to keep the goals of each topic in mind. We sprinkle comments and
remarks throughout the text, in the hope that it provides useful guidance
with respect to the big picture.
The book assumes the reader to have mathematical knowledge commonly

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Foreword 3

covered in high school mathematics and physics. For example, the reader
should have seen derivatives and integrals before, and geometric vectors
in two or three dimensions. Starting from there, we generalize these con-
cepts. Therefore, the target audience of the book includes undergraduate
university students, evening learners and learners participating in online
machine learning courses.
In analogy to music, there are three types of interaction that people
have with machine learning:
Astute Listener The democratization of machine learning by the pro-
vision of open-source software, online tutorials and cloud-based tools al-
lows users to not worry about the specifics of pipelines. Users can focus on
extracting insights from data using off-the-shelf tools. This enables non-
tech-savvy domain experts to benefit from machine learning. This is sim-
ilar to listening to music; the user is able to choose and discern between
different types of machine learning, and benefits from it. More experi-
enced users are like music critics, asking important questions about the
application of machine learning in society such as ethics, fairness, and pri-
vacy of the individual. We hope that this book provides a foundation for
thinking about the certification and risk management of machine learning
systems, and allows them to use their domain expertise to build better
machine learning systems.
Experienced Artist Skilled practitioners of machine learning can plug
and play different tools and libraries into an analysis pipeline. The stereo-
typical practitioner would be a data scientist or engineer who understands
machine learning interfaces and their use cases, and is able to perform
wonderful feats of prediction from data. This is similar to a virtuoso play-
ing music, where highly skilled practitioners can bring existing instru-
ments to life and bring enjoyment to their audience. Using the mathe-
matics presented here as a primer, practitioners would be able to under-
stand the benefits and limits of their favorite method, and to extend and
generalize existing machine learning algorithms. We hope that this book
provides the impetus for more rigorous and principled development of
machine learning methods.
Fledgling Composer As machine learning is applied to new domains,
developers of machine learning need to develop new methods and extend
existing algorithms. They are often researchers who need to understand
the mathematical basis of machine learning and uncover relationships be-
tween different tasks. This is similar to composers of music who, within
the rules and structure of musical theory, create new and amazing pieces.
We hope this book provides a high-level overview of other technical books
for people who want to become composers of machine learning. There is
a great need in society for new researchers who are able to propose and
explore novel approaches for attacking the many challenges of learning
from data.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


4 Foreword

Acknowledgments
We are grateful to many people who looked at early drafts of the book and
suffered through painful expositions of concepts. We tried to implement
their ideas that we did not vehemently disagree with. We would like to
especially acknowledge Christfried Webers for his careful reading of many
parts of the book, and his detailed suggestions on structure and presen-
tation. Many friends and colleagues have also been kind enough to pro-
vide their time and energy on different versions of each chapter. We have
been lucky to benefit from the generosity of the online community, who
have suggested improvements via github.com, which greatly improved
the book.
The following people have found bugs, proposed clarifications and sug-
gested relevant literature, either via github.com or personal communica-
tion. Their names are sorted alphabetically.

Abdul-Ganiy Usman Ellen Broad


Adam Gaier Fengkuangtian Zhu
Adele Jackson Fiona Condon
Aditya Menon Georgios Theodorou
Alasdair Tran He Xin
Aleksandar Krnjaic Irene Raissa Kameni
Alexander Makrigiorgos Jakub Nabaglo
Alfredo Canziani James Hensman
Ali Shafti Jamie Liu
Amr Khalifa Jean Kaddour
Andrew Tanggara Jean-Paul Ebejer
Angus Gruen Jerry Qiang
Antal A. Buss Jitesh Sindhare
Antoine Toisoul Le Cann John Lloyd
Areg Sarvazyan Jonas Ngnawe
Artem Artemev Jon Martin
Artyom Stepanov Justin Hsi
Bill Kromydas Kai Arulkumaran
Bob Williamson Kamil Dreczkowski
Boon Ping Lim Lily Wang
Chao Qu Lionel Tondji Ngoupeyou
Cheng Li Lydia Knüfing
Chris Sherlock Mahmoud Aslan
Christopher Gray Mark Hartenstein
Daniel McNamara Mark van der Wilk
Daniel Wood Markus Hegland
Darren Siegel Martin Hewing
David Johnston Matthew Alger
Dawei Chen Matthew Lee

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Foreword 5

Maximus McCann Shakir Mohamed


Mengyan Zhang Shawn Berry
Michael Bennett Sheikh Abdul Raheem Ali
Michael Pedersen Sheng Xue
Minjeong Shin Sridhar Thiagarajan
Mohammad Malekzadeh Syed Nouman Hasany
Naveen Kumar Szymon Brych
Nico Montali Thomas Bühler
Oscar Armas Timur Sharapov
Patrick Henriksen Tom Melamed
Patrick Wieschollek Vincent Adam
Pattarawat Chormai
Vincent Dutordoir
Paul Kelly
Vu Minh
Petros Christodoulou
Wasim Aftab
Piotr Januszewski
Wen Zhi
Pranav Subramani
Wojciech Stokowiec
Quyu Kong
Ragib Zaman Xiaonan Chong
Rui Zhang Xiaowei Zhang
Ryan-Rhys Griffiths Yazhou Hao
Salomon Kabongo Yicheng Luo
Samuel Ogunmola Young Lee
Sandeep Mavadia Yu Lu
Sarvesh Nikumbh Yun Cheng
Sebastian Raschka Yuxiao Huang
Senanayak Sesh Kumar Karri Zac Cranko
Seung-Heon Baek Zijian Cao
Shahbaz Chaudhary Zoe Nolan

Contributors through github, whose real names were not listed on their
github profile, are:

SamDataMad insad empet


bumptiousmonkey HorizonP victorBigand
idoamihai cs-maillist 17SKYE
deepakiim kudo23 jessjing1995

We are also very grateful to Parameswaran Raman and the many anony-
mous reviewers, organized by Cambridge University Press, who read one
or more chapters of earlier versions of the manuscript, and provided con-
structive criticism that led to considerable improvements. A special men-
tion goes to Dinesh Singh Negi, our LATEX support, for detailed and prompt
advice about LATEX-related issues. Last but not least, we are very grateful
to our editor Lauren Cowles, who has been patiently guiding us through
the gestation process of this book.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


6 Foreword

Table of Symbols

Symbol Typical meaning


a, b, c, ↵, , Scalars are lowercase
x, y, z Vectors are bold lowercase
A, B, C Matrices are bold uppercase
x> , A> Transpose of a vector or matrix
A 1 Inverse of a matrix
hx, yi Inner product of x and y
x> y Dot product of x and y
B = (b1 , b2 , b3 ) (Ordered) tuple
B = [b1 , b2 , b3 ] Matrix of column vectors stacked horizontally
B = {b1 , b2 , b3 } Set of vectors (unordered)
Z, N Integers and natural numbers, respectively
R, C Real and complex numbers, respectively
Rn n-dimensional vector space of real numbers
8x Universal quantifier: for all x
9x Existential quantifier: there exists x
a := b a is defined as b
a =: b b is defined as a
a/b a is proportional to b, i.e., a = constant · b
g f Function composition: “g after f ”
() If and only if
=) Implies
A, C Sets
a2A a is an element of the set A
; Empty set
D Number of dimensions; indexed by d = 1, . . . , D
N Number of data points; indexed by n = 1, . . . , N
Im Identity matrix of size m ⇥ m
0m,n Matrix of zeros of size m ⇥ n
1m,n Matrix of ones of size m ⇥ n
ei Standard/canonical vector (where i is the component that is 1)
dim Dimensionality of vector space
rk(A) Rank of matrix A
Im( ) Image of linear mapping
ker( ) Kernel (null space) of a linear mapping
span[b1 ] Span (generating set) of b1
tr(A) Trace of A
det(A) Determinant of A
|·| Absolute value or determinant (depending on context)
k·k Norm; Euclidean unless specified
Eigenvalue or Lagrange multiplier
E Eigenspace corresponding to eigenvalue

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Foreword 7

Symbol Typical meaning


✓ Parameter vector
@f
@x
Partial derivative of f with respect to x
df
dx
Total derivative of f with respect to x
r Gradient
L Lagrangian
L Negative log-likelihood
n
k
Binomial coefficient, n choose k
VX [x] Variance of x with respect to the random variable X
EX [x] Expectation of x with respect to the random variable X
CovX,Y [x, y] Covariance between x and y .
X? ? Y |Z X is conditionally independent of Y given Z
X⇠p Random variable X is distributed according to p
N µ, ⌃ Gaussian distribution with mean µ and covariance ⌃
Ber(µ) Bernoulli distribution with parameter µ
Bin(N, µ) Binomial distribution with parameters N, µ
Beta(↵, ) Beta distribution with parameters ↵,

Table of Abbreviations and Acronyms


Acronym Meaning
e.g. Exempli gratia (Latin: for example)
GMM Gaussian mixture model
i.e. Id est (Latin: this means)
i.i.d. Independent, identically distributed
MAP Maximum a posteriori
MLE Maximum likelihood estimation/estimator
ONB Orthonormal basis
PCA Principal component analysis
PPCA Probabilistic principal component analysis
REF Row-echelon form
SPD Symmetric, positive definite
SVM Support vector machine

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


Part I

Mathematical Foundations

9
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
1

Introduction and Motivation

Machine learning is about designing algorithms that automatically extract


valuable information from data. The emphasis here is on “automatic”, i.e.,
machine learning is concerned about general-purpose methodologies that
can be applied to many datasets, while producing something that is mean-
ingful. There are three concepts that are at the core of machine learning:
data, a model, and learning.

Since machine learning is inherently data driven, data is at the core data
of machine learning. The goal of machine learning is to design general-
purpose methodologies to extract valuable patterns from data, ideally
without much domain-specific expertise. For example, given a large corpus
of documents (e.g., books in many libraries), machine learning methods
can be used to automatically find relevant topics that are shared across
documents (Hoffman et al., 2010). To achieve this goal, we design mod-
els that are typically related to the process that generates data, similar to model
the dataset we are given. For example, in a regression setting, the model
would describe a function that maps inputs to real-valued outputs. To
paraphrase Mitchell (1997): A model is said to learn from data if its per-
formance on a given task improves after the data is taken into account.
The goal is to find good models that generalize well to yet unseen data,
which we may care about in the future. Learning can be understood as a learning
way to automatically find patterns and structure in data by optimizing the
parameters of the model.

While machine learning has seen many success stories, and software is
readily available to design and train rich and flexible machine learning
systems, we believe that the mathematical foundations of machine learn-
ing are important in order to understand fundamental principles upon
which more complicated machine learning systems are built. Understand-
ing these principles can facilitate creating new machine learning solutions,
understanding and debugging existing approaches, and learning about the
inherent assumptions and limitations of the methodologies we are work-
ing with.

11
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
12 Introduction and Motivation

1.1 Finding Words for Intuitions


A challenge we face regularly in machine learning is that concepts and
words are slippery, and a particular component of the machine learning
system can be abstracted to different mathematical concepts. For example,
the word “algorithm” is used in at least two different senses in the con-
text of machine learning. In the first sense, we use the phrase “machine
learning algorithm” to mean a system that makes predictions based on in-
predictor put data. We refer to these algorithms as predictors. In the second sense,
we use the exact same phrase “machine learning algorithm” to mean a
system that adapts some internal parameters of the predictor so that it
performs well on future unseen input data. Here we refer to this adapta-
training tion as training a system.
This book will not resolve the issue of ambiguity, but we want to high-
light upfront that, depending on the context, the same expressions can
mean different things. However, we attempt to make the context suffi-
ciently clear to reduce the level of ambiguity.
The first part of this book introduces the mathematical concepts and
foundations needed to talk about the three main components of a machine
learning system: data, models, and learning. We will briefly outline these
components here, and we will revisit them again in Chapter 8 once we
have discussed the necessary mathematical concepts.
While not all data is numerical, it is often useful to consider data in
a number format. In this book, we assume that data has already been
appropriately converted into a numerical representation suitable for read-
data as vectors ing into a computer program. Therefore, we think of data as vectors. As
another illustration of how subtle words are, there are (at least) three
different ways to think about vectors: a vector as an array of numbers (a
computer science view), a vector as an arrow with a direction and magni-
tude (a physics view), and a vector as an object that obeys addition and
scaling (a mathematical view).
model A model is typically used to describe a process for generating data, sim-
ilar to the dataset at hand. Therefore, good models can also be thought
of as simplified versions of the real (unknown) data-generating process,
capturing aspects that are relevant for modeling the data and extracting
hidden patterns from it. A good model can then be used to predict what
would happen in the real world without performing real-world experi-
ments.
learning We now come to the crux of the matter, the learning component of
machine learning. Assume we are given a dataset and a suitable model.
Training the model means to use the data available to optimize some pa-
rameters of the model with respect to a utility function that evaluates how
well the model predicts the training data. Most training methods can be
thought of as an approach analogous to climbing a hill to reach its peak.
In this analogy, the peak of the hill corresponds to a maximum of some

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


1.2 Two Ways to Read This Book 13

desired performance measure. However, in practice, we are interested in


the model to perform well on unseen data. Performing well on data that
we have already seen (training data) may only mean that we found a
good way to memorize the data. However, this may not generalize well to
unseen data, and, in practical applications, we often need to expose our
machine learning system to situations that it has not encountered before.
Let us summarize the main concepts of machine learning that we cover
in this book:

We represent data as vectors.


We choose an appropriate model, either using the probabilistic or opti-
mization view.
We learn from available data by using numerical optimization methods
with the aim that the model performs well on data not used for training.

1.2 Two Ways to Read This Book


We can consider two strategies for understanding the mathematics for
machine learning:

Bottom-up: Building up the concepts from foundational to more ad-


vanced. This is often the preferred approach in more technical fields,
such as mathematics. This strategy has the advantage that the reader
at all times is able to rely on their previously learned concepts. Unfor-
tunately, for a practitioner many of the foundational concepts are not
particularly interesting by themselves, and the lack of motivation means
that most foundational definitions are quickly forgotten.
Top-down: Drilling down from practical needs to more basic require-
ments. This goal-driven approach has the advantage that the readers
know at all times why they need to work on a particular concept, and
there is a clear path of required knowledge. The downside of this strat-
egy is that the knowledge is built on potentially shaky foundations, and
the readers have to remember a set of words that they do not have any
way of understanding.

We decided to write this book in a modular way to separate foundational


(mathematical) concepts from applications so that this book can be read
in both ways. The book is split into two parts, where Part I lays the math-
ematical foundations and Part II applies the concepts from Part I to a set
of fundamental machine learning problems, which form four pillars of
machine learning as illustrated in Figure 1.1: regression, dimensionality
reduction, density estimation, and classification. Chapters in Part I mostly
build upon the previous ones, but it is possible to skip a chapter and work
backward if necessary. Chapters in Part II are only loosely coupled and
can be read in any order. There are many pointers forward and backward

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


14 Introduction and Motivation

Figure 1.1 The


foundations and
four pillars of Machine Learning
machine learning.

Dimensionality

Classification
Reduction
Regression

Estimation
Density
Vector Calculus Probability & Distributions Optimization
Linear Algebra Analytic Geometry Matrix Decomposition

between the two parts of the book to link mathematical concepts with
machine learning algorithms.
Of course there are more than two ways to read this book. Most readers
learn using a combination of top-down and bottom-up approaches, some-
times building up basic mathematical skills before attempting more com-
plex concepts, but also choosing topics based on applications of machine
learning.

Part I Is about Mathematics


The four pillars of machine learning we cover in this book (see Figure 1.1)
require a solid mathematical foundation, which is laid out in Part I.
We represent numerical data as vectors and represent a table of such
data as a matrix. The study of vectors and matrices is called linear algebra,
linear algebra which we introduce in Chapter 2. The collection of vectors as a matrix is
also described there.
Given two vectors representing two objects in the real world, we want
to make statements about their similarity. The idea is that vectors that
are similar should be predicted to have similar outputs by our machine
learning algorithm (our predictor). To formalize the idea of similarity be-
tween vectors, we need to introduce operations that take two vectors as
input and return a numerical value representing their similarity. The con-
analytic geometry struction of similarity and distances is central to analytic geometry and is
discussed in Chapter 3.
In Chapter 4, we introduce some fundamental concepts about matri-
matrix ces and matrix decomposition. Some operations on matrices are extremely
decomposition useful in machine learning, and they allow for an intuitive interpretation
of the data and more efficient learning.
We often consider data to be noisy observations of some true underly-
ing signal. We hope that by applying machine learning we can identify the
signal from the noise. This requires us to have a language for quantify-
ing what “noise” means. We often would also like to have predictors that

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


1.2 Two Ways to Read This Book 15

allow us to express some sort of uncertainty, e.g., to quantify the confi-


dence we have about the value of the prediction at a particular test data
point. Quantification of uncertainty is the realm of probability theory and probability theory
is covered in Chapter 6.
To train machine learning models, we typically find parameters that
maximize some performance measure. Many optimization techniques re-
quire the concept of a gradient, which tells us the direction in which to
search for a solution. Chapter 5 is about vector calculus and details the vector calculus
concept of gradients, which we subsequently use in Chapter 7, where we
talk about optimization to find maxima/minima of functions. optimization

Part II Is about Machine Learning


The second part of the book introduces four pillars of machine learning
as shown in Figure 1.1. We illustrate how the mathematical concepts in-
troduced in the first part of the book are the foundation for each pillar.
Broadly speaking, chapters are ordered by difficulty (in ascending order).
In Chapter 8, we restate the three components of machine learning
(data, models, and parameter estimation) in a mathematical fashion. In
addition, we provide some guidelines for building experimental set-ups
that guard against overly optimistic evaluations of machine learning sys-
tems. Recall that the goal is to build a predictor that performs well on
unseen data.
In Chapter 9, we will have a close look at linear regression, where our linear regression
objective is to find functions that map inputs x 2 RD to corresponding ob-
served function values y 2 R, which we can interpret as the labels of their
respective inputs. We will discuss classical model fitting (parameter esti-
mation) via maximum likelihood and maximum a posteriori estimation,
as well as Bayesian linear regression, where we integrate the parameters
out instead of optimizing them.
Chapter 10 focuses on dimensionality reduction, the second pillar in Fig- dimensionality
ure 1.1, using principal component analysis. The key objective of dimen- reduction
sionality reduction is to find a compact, lower-dimensional representation
of high-dimensional data x 2 RD , which is often easier to analyze than
the original data. Unlike regression, dimensionality reduction is only con-
cerned about modeling the data – there are no labels associated with a
data point x.
In Chapter 11, we will move to our third pillar: density estimation. The density estimation
objective of density estimation is to find a probability distribution that de-
scribes a given dataset. We will focus on Gaussian mixture models for this
purpose, and we will discuss an iterative scheme to find the parameters of
this model. As in dimensionality reduction, there are no labels associated
with the data points x 2 RD . However, we do not seek a low-dimensional
representation of the data. Instead, we are interested in a density model
that describes the data.
Chapter 12 concludes the book with an in-depth discussion of the fourth

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


16 Introduction and Motivation

classification pillar: classification. We will discuss classification in the context of support


vector machines. Similar to regression (Chapter 9), we have inputs x and
corresponding labels y . However, unlike regression, where the labels were
real-valued, the labels in classification are integers, which requires special
care.

1.3 Exercises and Feedback


We provide some exercises in Part I, which can be done mostly by pen and
paper. For Part II, we provide programming tutorials (jupyter notebooks)
to explore some properties of the machine learning algorithms we discuss
in this book.
We appreciate that Cambridge University Press strongly supports our
aim to democratize education and learning by making this book freely
available for download at
https://mml-book.com
where tutorials, errata, and additional materials can be found. Mistakes
can be reported and feedback provided using the preceding URL.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2

Linear Algebra

When formalizing intuitive concepts, a common approach is to construct a


set of objects (symbols) and a set of rules to manipulate these objects. This
is known as an algebra. Linear algebra is the study of vectors and certain algebra
rules to manipulate vectors. The vectors many of us know from school are
called “geometric vectors”, which are usually denoted by a small arrow
above the letter, e.g., !
x and ! y . In this book, we discuss more general
concepts of vectors and use a bold letter to represent them, e.g., x and y .
In general, vectors are special objects that can be added together and
multiplied by scalars to produce another object of the same kind. From
an abstract mathematical viewpoint, any object that satisfies these two
properties can be considered a vector. Here are some examples of such
vector objects:

1. Geometric vectors. This example of a vector may be familiar from high


school mathematics and physics. Geometric vectors – see Figure 2.1(a)
– are directed segments, which can be drawn (at least in two dimen-
! ! ! ! !
sions). Two geometric vectors x, y can be added, such that x + y = z
is another geometric vector. Furthermore, multiplication by a scalar
!
x , 2 R, is also a geometric vector. In fact, it is the original vector
scaled by . Therefore, geometric vectors are instances of the vector
concepts introduced previously. Interpreting vectors as geometric vec-
tors enables us to use our intuitions about direction and magnitude to
reason about mathematical operations.
2. Polynomials are also vectors; see Figure 2.1(b): Two polynomials can

! ! 4 Figure 2.1
x+y Different types of
2
vectors. Vectors can
0 be surprising
objects, including
y

! 2 (a) geometric
x ! vectors
y 4
and (b) polynomials.
6
2 0 2
x

(a) Geometric vectors. (b) Polynomials.

17
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
18 Linear Algebra

be added together, which results in another polynomial; and they can


be multiplied by a scalar 2 R, and the result is a polynomial as
well. Therefore, polynomials are (rather unusual) instances of vectors.
Note that polynomials are very different from geometric vectors. While
geometric vectors are concrete “drawings”, polynomials are abstract
concepts. However, they are both vectors in the sense previously de-
scribed.
3. Audio signals are vectors. Audio signals are represented as a series of
numbers. We can add audio signals together, and their sum is a new
audio signal. If we scale an audio signal, we also obtain an audio signal.
Therefore, audio signals are a type of vector, too.
4. Elements of Rn (tuples of n real numbers) are vectors. Rn is more
abstract than polynomials, and it is the concept we focus on in this
book. For instance,
2 3
1
a = 425 2 R3 (2.1)
3

is an example of a triplet of numbers. Adding two vectors a, b 2 Rn


component-wise results in another vector: a + b = c 2 Rn . Moreover,
multiplying a 2 Rn by 2 R results in a scaled vector a 2 Rn .
Be careful to check Considering vectors as elements of Rn has an additional benefit that
whether array it loosely corresponds to arrays of real numbers on a computer. Many
operations actually
programming languages support array operations, which allow for con-
perform vector
operations when venient implementation of algorithms that involve vector operations.
implementing on a
computer. Linear algebra focuses on the similarities between these vector concepts.
Pavel Grinfeld’s
We can add them together and multiply them by scalars. We will largely
series on linear focus on vectors in Rn since most algorithms in linear algebra are for-
algebra: mulated in Rn . We will see in Chapter 8 that we often consider data to
http://tinyurl. be represented as vectors in Rn . In this book, we will focus on finite-
com/nahclwm
dimensional vector spaces, in which case there is a 1:1 correspondence
Gilbert Strang’s
course on linear between any kind of vector and Rn . When it is convenient, we will use
algebra: intuitions about geometric vectors and consider array-based algorithms.
http://tinyurl. One major idea in mathematics is the idea of “closure”. This is the ques-
com/29p5q8j
tion: What is the set of all things that can result from my proposed oper-
3Blue1Brown series
ations? In the case of vectors: What is the set of vectors that can result by
on linear algebra:
https://tinyurl. starting with a small set of vectors, and adding them to each other and
com/h5g4kps scaling them? This results in a vector space (Section 2.4). The concept of
a vector space and its properties underlie much of machine learning. The
concepts introduced in this chapter are summarized in Figure 2.2.
This chapter is mostly based on the lecture notes and books by Drumm
and Weil (2001), Strang (2003), Hogben (2013), Liesen and Mehrmann
(2015), as well as Pavel Grinfeld’s Linear Algebra series. Other excellent

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.1 Systems of Linear Equations 19

Vector
Figure 2.2 A mind
map of the concepts
ses pro
po per introduced in this

closure
m ty o
co f chapter, along with
Chapter 5 where they are used
Matrix Abelian
Vector calculus with + in other parts of the
ts Vector space Group Linear
sen independence book.
rep

e
repr
res

maximal set
ent

System of
s

linear equations
Linear/affine
so mapping
lve
solved by

s Basis

Matrix
inverse
Gaussian
elimination

Chapter 3 Chapter 10
Chapter 12
Analytic geometry Dimensionality
Classification
reduction

resources are Gilbert Strang’s Linear Algebra course at MIT and the Linear
Algebra Series by 3Blue1Brown.
Linear algebra plays an important role in machine learning and gen-
eral mathematics. The concepts introduced in this chapter are further ex-
panded to include the idea of geometry in Chapter 3. In Chapter 5, we
will discuss vector calculus, where a principled knowledge of matrix op-
erations is essential. In Chapter 10, we will use projections (to be intro-
duced in Section 3.8) for dimensionality reduction with principal compo-
nent analysis (PCA). In Chapter 9, we will discuss linear regression, where
linear algebra plays a central role for solving least-squares problems.

2.1 Systems of Linear Equations


Systems of linear equations play a central part of linear algebra. Many
problems can be formulated as systems of linear equations, and linear
algebra gives us the tools for solving them.

Example 2.1
A company produces products N1 , . . . , Nn for which resources
R1 , . . . , Rm are required. To produce a unit of product Nj , aij units of
resource Ri are needed, where i = 1, . . . , m and j = 1, . . . , n.
The objective is to find an optimal production plan, i.e., a plan of how
many units xj of product Nj should be produced if a total of bi units of
resource Ri are available and (ideally) no resources are left over.
If we produce x1 , . . . , xn units of the corresponding products, we need

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


20 Linear Algebra

a total of
ai1 x1 + · · · + ain xn (2.2)
many units of resource Ri . An optimal production plan (x1 , . . . , xn ) 2 Rn ,
therefore, has to satisfy the following system of equations:
a11 x1 + · · · + a1n xn = b1
.. , (2.3)
.
am1 x1 + · · · + amn xn = bm
where aij 2 R and bi 2 R.

system of linear Equation (2.3) is the general form of a system of linear equations, and
equations x1 , . . . , xn are the unknowns of this system. Every n-tuple (x1 , . . . , xn ) 2
solution Rn that satisfies (2.3) is a solution of the linear equation system.

Example 2.2
The system of linear equations
x1 + x2 + x3 = 3 (1)
x1 x2 + 2x3 = 2 (2) (2.4)
2x1 + 3x3 = 1 (3)
has no solution: Adding the first two equations yields 2x1 +3x3 = 5, which
contradicts the third equation (3).
Let us have a look at the system of linear equations
x1 + x2 + x3 = 3 (1)
x1 x2 + 2x3 = 2 (2) . (2.5)
x2 + x3 = 2 (3)
From the first and third equation, it follows that x1 = 1. From (1)+(2),
we get 2x1 + 3x3 = 5, i.e., x3 = 1. From (3), we then get that x2 = 1.
Therefore, (1, 1, 1) is the only possible and unique solution (verify that
(1, 1, 1) is a solution by plugging in).
As a third example, we consider
x1 + x2 + x3 = 3 (1)
x1 x2 + 2x3 = 2 (2) . (2.6)
2x1 + 3x3 = 5 (3)
Since (1)+(2)=(3), we can omit the third equation (redundancy). From
(1) and (2), we get 2x1 = 5 3x3 and 2x2 = 1+x3 . We define x3 = a 2 R
as a free variable, such that any triplet
✓ ◆
5 3 1 1
a, + a, a , a 2 R (2.7)
2 2 2 2

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.1 Systems of Linear Equations 21

Figure 2.3 The


x2 solution space of a
system of two linear
equations with two
4x1 + 4x2 = 5
variables can be
geometrically
2x1 4x2 = 1 interpreted as the
intersection of two
lines. Every linear
equation represents
a line.
x1

is a solution of the system of linear equations, i.e., we obtain a solution


set that contains infinitely many solutions.

In general, for a real-valued system of linear equations we obtain either


no, exactly one, or infinitely many solutions. Linear regression (Chapter 9)
solves a version of Example 2.1 when we cannot solve the system of linear
equations.
Remark (Geometric Interpretation of Systems of Linear Equations). In a
system of linear equations with two variables x1 , x2 , each linear equation
defines a line on the x1 x2 -plane. Since a solution to a system of linear
equations must satisfy all equations simultaneously, the solution set is the
intersection of these lines. This intersection set can be a line (if the linear
equations describe the same line), a point, or empty (when the lines are
parallel). An illustration is given in Figure 2.3 for the system

4x1 + 4x2 = 5
(2.8)
2x1 4x2 = 1

where the solution space is the point (x1 , x2 ) = (1, 14 ). Similarly, for three
variables, each linear equation determines a plane in three-dimensional
space. When we intersect these planes, i.e., satisfy all linear equations at
the same time, we can obtain a solution set that is a plane, a line, a point
or empty (when the planes have no common intersection). }
For a systematic approach to solving systems of linear equations, we
will introduce a useful compact notation. We collect the coefficients aij
into vectors and collect the vectors into matrices. In other words, we write
the system from (2.3) in the following form:
2 3 2 3 2 3 2 3
a11 a12 a1n b1
6 .. 7 6 .. 7 6 .. 7 6 .. 7
x1 4 . 5 + x2 4 . 5 + · · · + xn 4 . 5 = 4 . 5 (2.9)
am1 am2 amn bm

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


22 Linear Algebra
2 32 3 2 3
a11 ··· a1n x1 b1
6 .. .. 7 6 .. 7 = 6 .. 7 .
() 4 . . 54 . 5 4 . 5 (2.10)
am1 · · · amn xn bm
In the following, we will have a close look at these matrices and de-
fine computation rules. We will return to solving linear equations in Sec-
tion 2.3.

2.2 Matrices
Matrices play a central role in linear algebra. They can be used to com-
pactly represent systems of linear equations, but they also represent linear
functions (linear mappings) as we will see later in Section 2.7. Before we
discuss some of these interesting topics, let us first define what a matrix
is and what kind of operations we can do with matrices. We will see more
properties of matrices in Chapter 4.
matrix Definition 2.1 (Matrix). With m, n 2 N a real-valued (m, n) matrix A is
an m·n-tuple of elements aij , i = 1, . . . , m, j = 1, . . . , n, which is ordered
according to a rectangular scheme consisting of m rows and n columns:
2 3
a11 a12 · · · a1n
6 a21 a22 · · · a2n 7
6 7
A = 6 .. .. .. 7 , aij 2 R . (2.11)
4 . . . 5
am1 am2 · · · amn
row By convention (1, n)-matrices are called rows and (m, 1)-matrices are called
column columns. These special matrices are also called row/column vectors.
row vector
column vector Rm⇥n is the set of all real-valued (m, n)-matrices. A 2 Rm⇥n can be
Figure 2.4 By equivalently represented as a 2 Rmn by stacking all n columns of the
stacking its matrix into a long vector; see Figure 2.4.
columns, a matrix A
can be represented
as a long vector a.
2.2.1 Matrix Addition and Multiplication
A 2 R4⇥2 a 2 R8
The sum of two matrices A 2 Rm⇥n , B 2 Rm⇥n is defined as the element-
re-shape
wise sum, i.e.,
2 3
a11 + b11 · · · a1n + b1n
6 .. .. 7 m⇥n
(2.12)
A + B := 4 . . 52R .
am1 + bm1 · · · amn + bmn
Note the size of the For matrices A 2 Rm⇥n , B 2 Rn⇥k , the elements cij of the product
matrices. C = AB 2 Rm⇥k are computed as
C =
n
X
np.einsum(’il,
lj’, A, B) cij = ail blj , i = 1, . . . , m, j = 1, . . . , k. (2.13)
l=1

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.2 Matrices 23

This means, to compute element cij we multiply the elements of the ith There are n columns
row of A with the j th column of B and sum them up. Later in Section 3.2, in A and n rows in
B so that we can
we will call this the dot product of the corresponding row and column. In
compute ail blj for
cases, where we need to be explicit that we are performing multiplication, l = 1, . . . , n.
we use the notation A · B to denote multiplication (explicitly showing Commonly, the dot
“·”). product between
two vectors a, b is
Remark. Matrices can only be multiplied if their “neighboring” dimensions denoted by a> b or
match. For instance, an n ⇥ k -matrix A can be multiplied with a k ⇥ m- ha, bi.
matrix B , but only from the left side:
A |{z}
|{z} B = |{z}
C (2.14)
n⇥k k⇥m n⇥m

The product BA is not defined if m 6= n since the neighboring dimensions


do not match. }
Remark. Matrix multiplication is not defined as an element-wise operation
on matrix elements, i.e., cij 6= aij bij (even if the size of A, B was cho-
sen appropriately). This kind of element-wise multiplication often appears
in programming languages when we multiply (multi-dimensional) arrays
with each other, and is called a Hadamard product. } Hadamard product

Example 2.3 2 3
 0 2
1 2 3
For A = 2 R2⇥3 , B = 41 15 2 R3⇥2 , we obtain
3 2 1
0 1
2 3
 0 2 
1 2 3 4 5 2 3
AB = 1 1 = 2 R2⇥2 , (2.15)
3 2 1 2 5
0 1
2 3 2 3
0 2  6 4 2
1 2 3
BA = 41 15 = 4 2 0 25 2 R3⇥3 . (2.16)
3 2 1
0 1 3 2 1

Figure 2.5 Even if


From this example, we can already see that matrix multiplication is not both matrix
multiplications AB
commutative, i.e., AB 6= BA; see also Figure 2.5 for an illustration.
and BA are
defined, the
Definition 2.2 (Identity Matrix). In Rn⇥n , we define the identity matrix
dimensions of the
2 3 results can be
1 0 ··· 0 ··· 0
60 1 · · · 0 · · · 07 different.
6 7
6 .. .. . . .. . . .. 7
6. . . . . .7
I n := 6
60 0 · · · 1 · · · 07 2 R
7 n⇥n
(2.17)
6 7
6. . .
. . ... . . . ... 5
7
4 .. ..
identity matrix
0 0 ··· 0 ··· 1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


24 Linear Algebra

as the n ⇥ n-matrix containing 1 on the diagonal and 0 everywhere else.


Now that we defined matrix multiplication, matrix addition and the
identity matrix, let us have a look at some properties of matrices:
associativity
Associativity:
8A 2 Rm⇥n , B 2 Rn⇥p , C 2 Rp⇥q : (AB)C = A(BC) (2.18)
distributivity
Distributivity:
8A, B 2 Rm⇥n , C, D 2 Rn⇥p : (A + B)C = AC + BC (2.19a)
A(C + D) = AC + AD (2.19b)
Multiplication with the identity matrix:
8A 2 Rm⇥n : I m A = AI n = A (2.20)
Note that I m 6= I n for m 6= n.

2.2.2 Inverse and Transpose


A square matrix Definition 2.3 (Inverse). Consider a square matrix A 2 Rn⇥n . Let matrix
possesses the same B 2 Rn⇥n have the property that AB = I n = BA. B is called the
number of columns
inverse of A and denoted by A 1 .
and rows.
inverse Unfortunately, not every matrix A possesses an inverse A 1 . If this
regular inverse does exist, A is called regular/invertible/nonsingular, otherwise
invertible singular/noninvertible. When the matrix inverse exists, it is unique. In Sec-
nonsingular tion 2.3, we will discuss a general way to compute the inverse of a matrix
singular by solving a system of linear equations.
noninvertible
Remark (Existence of the Inverse of a 2 ⇥ 2-matrix). Consider a matrix

a a
A := 11 12 2 R2⇥2 . (2.21)
a21 a22
If we multiply A with

a22 a12
B := (2.22)
a21 a11
we obtain

a a a12 a21 0
AB = 11 22 = (a11 a22 a12 a21 )I . (2.23)
0 a11 a22 a12 a21
Therefore,

1 1 a22 a12
A = (2.24)
a11 a22 a12 a21 a21 a11
if and only if a11 a22 a12 a21 6= 0. In Section 4.1, we will see that a11 a22
a12 a21 is the determinant of a 2⇥2-matrix. Furthermore, we can generally
use the determinant to check whether a matrix is invertible. }

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.2 Matrices 25

Example 2.4 (Inverse Matrix)


The matrices
2 3 2 3
1 2 1 7 7 6
A = 44 4 55 , B=4 2 1 15 (2.25)
6 7 7 4 5 4
are inverse to each other since AB = I = BA.

Definition 2.4 (Transpose). For A 2 Rm⇥n the matrix B 2 Rn⇥m with


bij = aji is called the transpose of A. We write B = A> . transpose
The main diagonal
In general, A> can be obtained by writing the columns of A as the rows (sometimes called
of A> . The following are important properties of inverses and transposes: “principal diagonal”,
“primary diagonal”,
“leading diagonal”,
1
AA = I = A 1A (2.26) or “major diagonal”)
1 1 of a matrix A is the
(AB) 1
=B A (2.27) collection of entries
(A + B) 1
6= A 1
+B 1
(2.28) Aij where i = j.
> > The scalar case of
(A ) = A (2.29) (2.28) is
1
= 16 6= 12 + 14 .
(A + B)> = A> + B > (2.30) 2+4

> >
>
(AB) = B A (2.31)
Definition 2.5 (Symmetric Matrix). A matrix A 2 Rn⇥n is symmetric if symmetric matrix
A = A> .
Note that only (n, n)-matrices can be symmetric. Generally, we call
(n, n)-matrices also square matrices because they possess the same num- square matrix
ber of rows and columns. Moreover, if A is invertible, then so is A> , and
(A 1 )> = (A> ) 1 =: A > .
Remark (Sum and Product of Symmetric Matrices). The sum of symmet-
ric matrices A, B 2 Rn⇥n is always symmetric. However, although their
product is always defined, it is generally not symmetric:
  
1 0 1 1 1 1
= . (2.32)
0 0 1 1 0 0
}

2.2.3 Multiplication by a Scalar


Let us look at what happens to matrices when they are multiplied by a
scalar 2 R. Let A 2 Rm⇥n and 2 R. Then A = K , Kij = aij .
Practically, scales each element of A. For , 2 R, the following holds:
associativity
Associativity:
( )C = ( C), C 2 Rm⇥n

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


26 Linear Algebra

(BC) = ( B)C = B( C) = (BC) , B 2 Rm⇥n , C 2 Rn⇥k .


Note that this allows us to move scalar values around.

distributivity ( C)> = C > >


= C > = C > since = >
for all 2 R.
Distributivity:
( + )C = C + C, C 2 Rm⇥n
(B + C) = B + C, B, C 2 Rm⇥n

Example 2.5 (Distributivity)


If we define

1 2
C := , (2.33)
3 4
then for any , 2 R we obtain
 
( + )1 ( + )2 + 2 +2
( + )C = = (2.34a)
( + )3 ( + )4 3 +3 4 +4
 
2 2
= + = C+ C. (2.34b)
3 4 3 4

2.2.4 Compact Representations of Systems of Linear Equations


If we consider the system of linear equations

2x1 + 3x2 + 5x3 = 1


4x1 2x2 7x3 = 8 (2.35)
9x1 + 5x2 3x3 = 2

and use the rules for matrix multiplication, we can write this equation
system in a more compact form as
2 32 3 2 3
2 3 5 x1 1
44 2 7 x2 = 85 .
5 4 5 4 (2.36)
9 5 3 x3 2

Note that x1 scales the first column, x2 the second one, and x3 the third
one.
Generally, a system of linear equations can be compactly represented in
their matrix form as Ax = b; see (2.3), and the product Ax is a (linear)
combination of the columns of A. We will discuss linear combinations in
more detail in Section 2.5.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.3 Solving Systems of Linear Equations 27

2.3 Solving Systems of Linear Equations


In (2.3), we introduced the general form of an equation system, i.e.,
a11 x1 + · · · + a1n xn = b1
.. (2.37)
.
am1 x1 + · · · + amn xn = bm ,
where aij 2 R and bi 2 R are known constants and xj are unknowns,
i = 1, . . . , m, j = 1, . . . , n. Thus far, we saw that matrices can be used as
a compact way of formulating systems of linear equations so that we can
write Ax = b, see (2.10). Moreover, we defined basic matrix operations,
such as addition and multiplication of matrices. In the following, we will
focus on solving systems of linear equations and provide an algorithm for
finding the inverse of a matrix.

2.3.1 Particular and General Solution


Before discussing how to generally solve systems of linear equations, let
us have a look at an example. Consider the system of equations
2 3
 x1 
1 0 8 4 6 7
6x2 7 = 42 . (2.38)
0 1 2 12 4x3 5 8
x4
The system has two equations and four unknowns. Therefore, in general
we would expect infinitely many solutions. This system of equations is
in a particularly easy form, where the first two columns consist of a 1
and
P4 a 0. Remember that we want to find scalars x1 , . . . , x4 , such that
i=1 xi ci = b, where we define ci to be the ith column of the matrix and
b the right-hand-side of (2.38). A solution to the problem in (2.38) can
be found immediately by taking 42 times the first column and 8 times the
second column so that
  
42 1 0
b= = 42 +8 . (2.39)
8 0 1
Therefore, a solution is [42, 8, 0, 0]> . This solution is called a particular particular solution
solution or special solution. However, this is not the only solution of this special solution
system of linear equations. To capture all the other solutions, we need
to be creative in generating 0 in a non-trivial way using the columns of
the matrix: Adding 0 to our special solution does not change the special
solution. To do so, we express the third column using the first two columns
(which are of this very simple form)
  
8 1 0
=8 +2 (2.40)
2 0 1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


28 Linear Algebra

so that 0 = 8c1 + 2c2 1c3 + 0c4 and (x1 , x2 , x3 , x4 ) = (8, 2, 1, 0). In


fact, any scaling of this solution by 1 2 R produces the 0 vector, i.e.,
0 2 31
 8
1 0 8 4 B 627
B 6 C
7C = 1 (8c1 + 2c2 c3 ) = 0 .
@ 14 (2.41)
0 1 2 12 1 A
5
0
Following the same line of reasoning, we express the fourth column of the
matrix in (2.38) using the first two columns and generate another set of
non-trivial versions of 0 as
0 2 31
 4
1 0 8 4 B 6 12 7
B 6 C
7C = 2 ( 4c1 + 12c2 c4 ) = 0
@ 24 (2.42)
0 1 2 12 0 A
5
1
for any 2 2 R. Putting everything together, we obtain all solutions of the
general solution equation system in (2.38), which is called the general solution, as the set
8 2 3 2 3 2 3 9
>
> 42 8 4 >
>
< 6 7 6 7 6 7 =
6 8 7 6 2 7 6 12 7
x 2 R : x = 4 5 + 1 4 5 + 2 4 5 , 1 , 2 2 R . (2.43)
4
>
> 0 1 0 >
>
: ;
0 0 1
Remark. The general approach we followed consisted of the following
three steps:
1. Find a particular solution to Ax = b.
2. Find all solutions to Ax = 0.
3. Combine the solutions from steps 1. and 2. to the general solution.
Neither the general nor the particular solution is unique. }
The system of linear equations in the preceding example was easy to
solve because the matrix in (2.38) has this particularly convenient form,
which allowed us to find the particular and the general solution by in-
spection. However, general equation systems are not of this simple form.
Fortunately, there exists a constructive algorithmic way of transforming
any system of linear equations into this particularly simple form: Gaussian
elimination. Key to Gaussian elimination are elementary transformations
of systems of linear equations, which transform the equation system into
a simple form. Then, we can apply the three steps to the simple form that
we just discussed in the context of the example in (2.38).

2.3.2 Elementary Transformations


elementary Key to solving a system of linear equations are elementary transformations
transformations that keep the solution set the same, but that transform the equation system
into a simpler form:

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.3 Solving Systems of Linear Equations 29

Exchange of two equations (rows in the matrix representing the system


of equations)
Multiplication of an equation (row) with a constant 2 R\{0}
Addition of two equations (rows)

Example 2.6
For a 2 R, we seek all solutions of the following system of equations:
2x1 + 4x2 2x3 x4 + 4x5 = 3
4x1 8x2 + 3x3 3x4 + x5 = 2
. (2.44)
x1 2x2 + x3 x4 + x5 = 0
x1 2x2 3x4 + 4x5 = a
We start by converting this system of equations into the compact matrix
notation Ax = b. We no longer mention⇥ the ⇤variables x explicitly and
build the augmented matrix (in the form A | b ) augmented matrix
2 3
2 4 2 1 4 3 Swap with R3
6 4 8 3 3 1 7
2 7
6
4 1 2 1 1 1 0 5 Swap with R1
1 2 0 3 4 a
where we used the vertical line to separate the left-hand side from the
right-hand side in (2.44). We use to indicate a transformation of the
augmented matrix using elementary transformations. The augmented
⇥ ⇤
matrix A | b
Swapping Rows 1 and 3 leads to
compactly
2 3
1 2 1 1 1 0 represents the
6 system of linear
6 4 8 3 3 1 2 77 4R1 equations Ax = b.
4 2 4 2 1 4 3 5 +2R1
1 2 0 3 4 a R1
When we now apply the indicated transformations (e.g., subtract Row 1
four times from Row 2), we obtain
2 3
1 2 1 1 1 0
6 0 0 1 1 3 2 7
6 7
4 0 0 0 3 6 3 5
0 0 1 2 3 a R2 R3
2 3
1 2 1 1 1 0
6 2 7
6 0 0 1 1 3 7 ·( 1)
4 0 0 0 3 6 3 5 ·( 13 )
0 0 0 0 0 a+1
2 3
1 2 1 1 1 0
6 0 0 1 1 3 2 7
6 7
4 0 0 0 1 2 1 5
0 0 0 0 0 a+1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


30 Linear Algebra

row-echelon form This (augmented) matrix is in a convenient form, the row-echelon form
(REF). Reverting this compact notation back into the explicit notation with
the variables we seek, we obtain
x1 2x2 + x3 x4 + x5 = 0
x3 x4 + 3x5 = 2
. (2.45)
x4 2x5 = 1
0 = a+1
particular solution Only for a = 1 this system can be solved. A particular solution is
2 3 2 3
x1 2
6x2 7 6 0 7
6 7 6 7
6 x 3 7 = 6 17 . (2.46)
6 7 6 7
4x4 5 4 1 5
x5 0
general solution The general solution, which captures the set of all possible solutions, is
8 2 3 2 3 2 3 9
>
> 2 2 2 >
>
>
> 6 0 7 617 6 0 7 >
>
< 6 7 6 7 6 7 =
x2R :x=6
5
6 717 + 1 607 + 2 6 17 ,
6 7 6 7 1 , 2 2 R . (2.47)
>
> 4 1 5 405 4 2 5 >
>
>
> >
>
: ;
0 0 1

In the following, we will detail a constructive way to obtain a particular


and general solution of a system of linear equations.
Remark (Pivots and Staircase Structure). The leading coefficient of a row
pivot (first nonzero number from the left) is called the pivot and is always
strictly to the right of the pivot of the row above it. Therefore, any equa-
tion system in row-echelon form always has a “staircase” structure. }
row-echelon form Definition 2.6 (Row-Echelon Form). A matrix is in row-echelon form if

All rows that contain only zeros are at the bottom of the matrix; corre-
spondingly, all rows that contain at least one nonzero element are on
top of rows that contain only zeros.
Looking at nonzero rows only, the first nonzero number from the left
pivot (also called the pivot or the leading coefficient) is always strictly to the
leading coefficient right of the pivot of the row above it.
In other texts, it is
sometimes required Remark (Basic and Free Variables). The variables corresponding to the
that the pivot is 1. pivots in the row-echelon form are called basic variables and the other
basic variable variables are free variables. For example, in (2.45), x1 , x3 , x4 are basic
free variable variables, whereas x2 , x5 are free variables. }
Remark (Obtaining a Particular Solution). The row-echelon form makes

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.3 Solving Systems of Linear Equations 31

our lives easier when we need to determine a particular solution. To do


this, we express the right-hand
PP side of the equation system using the pivot
columns, such that b = i=1 i pi , where pi , i = 1, . . . , P , are the pivot
columns. The i are determined easiest if we start with the rightmost pivot
column and work our way to the left.
In the previous example, we would try to find 1 , 2 , 3 so that
2 3 2 3 2 3 2 3
1 1 1 0
607 617 6 17 6 27
6 7 6 7
14 5+ 24 5+ 34
6 7=6 7. (2.48)
0 0 15 415
0 0 0 0
From here, we find relatively directly that 3 = 1, 2 = 1, 1 = 2. When
we put everything together, we must not forget the non-pivot columns
for which we set the coefficients implicitly to 0. Therefore, we get the
particular solution x = [2, 0, 1, 1, 0]> . }
Remark (Reduced Row Echelon Form). An equation system is in reduced reduced
row-echelon form (also: row-reduced echelon form or row canonical form) if row-echelon form

It is in row-echelon form.
Every pivot is 1.
The pivot is the only nonzero entry in its column.
}
The reduced row-echelon form will play an important role later in Sec-
tion 2.3.3 because it allows us to determine the general solution of a sys-
tem of linear equations in a straightforward way.
Gaussian
Remark (Gaussian Elimination). Gaussian elimination is an algorithm that elimination
performs elementary transformations to bring a system of linear equations
into reduced row-echelon form. }

Example 2.7 (Reduced Row Echelon Form)


Verify that the following matrix is in reduced row-echelon form (the pivots
are in bold):
2 3
1 3 0 0 3
A = 40 0 1 0 9 5 . (2.49)
0 0 0 1 4
The key idea for finding the solutions of Ax = 0 is to look at the non-
pivot columns, which we will need to express as a (linear) combination of
the pivot columns. The reduced row echelon form makes this relatively
straightforward, and we express the non-pivot columns in terms of sums
and multiples of the pivot columns that are on their left: The second col-
umn is 3 times the first column (we can ignore the pivot columns on the
right of the second column). Therefore, to obtain 0, we need to subtract

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


32 Linear Algebra

the second column from three times the first column. Now, we look at the
fifth column, which is our second non-pivot column. The fifth column can
be expressed as 3 times the first pivot column, 9 times the second pivot
column, and 4 times the third pivot column. We need to keep track of
the indices of the pivot columns and translate this into 3 times the first col-
umn, 0 times the second column (which is a non-pivot column), 9 times
the third column (which is our second pivot column), and 4 times the
fourth column (which is the third pivot column). Then we need to subtract
the fifth column to obtain 0. In the end, we are still solving a homogeneous
equation system.
To summarize, all solutions of Ax = 0, x 2 R5 are given by
8 2 3 2 3 9
>
> 3 3 >
>
>
> 6 17 6 0 7 >
>
< 6 7 6 7 =
5 6 7
x 2 R : x = 16 0 7+ 26 9 7 , 6 7 1, 2 2 R . (2.50)
>
> 4 0 5 4 45 >
>
>
> >
>
: ;
0 1

2.3.3 The Minus-1 Trick


In the following, we introduce a practical trick for reading out the solu-
tions x of a homogeneous system of linear equations Ax = 0, where
A 2 Rk⇥n , x 2 Rn .
To start, we assume that A is in reduced row-echelon form without any
rows that just contain zeros, i.e.,
2 3
0 ··· 0 1 ⇤ ··· ⇤ 0 ⇤ ··· ⇤ 0 ⇤ ··· ⇤
6 .. .. . . .. 7
6 .
6 . 0 0 · · · 0 1 ⇤ · · · ⇤ .. .. . 7 7
6 .. . .
.. .. ... .
.. 0 .. . . . .
.. .. .. .. 7 ,
A=6 . . 7
6 7
6 .. .. .. .. .. .. .. .. .. .. 7
4 . . . . . . . . 0 . . 5
0 ··· 0 0 0 ··· 0 0 0 ··· 0 1 ⇤ ··· ⇤
(2.51)
where ⇤ can be an arbitrary real number, with the constraints that the first
nonzero entry per row must be 1 and all other entries in the corresponding
column must be 0. The columns j1 , . . . , jk with the pivots (marked in
bold) are the standard unit vectors e1 , . . . , ek 2 Rk . We extend this matrix
to an n ⇥ n-matrix à by adding n k rows of the form
⇥ ⇤
0 ··· 0 1 0 ··· 0 (2.52)

so that the diagonal of the augmented matrix à contains either 1 or 1.


Then, the columns of à that contain the 1 as pivots are solutions of

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.3 Solving Systems of Linear Equations 33

the homogeneous equation system Ax = 0. To be more precise, these


columns form a basis (Section 2.6.1) of the solution space of Ax = 0,
which we will later call the kernel or null space (see Section 2.7.3). kernel
null space

Example 2.8 (Minus-1 Trick)


Let us revisit the matrix in (2.49), which is already in REF:
2 3
1 3 0 0 3
A = 40 0 1 0 9 5 . (2.53)
0 0 0 1 4
We now augment this matrix to a 5 ⇥ 5 matrix by adding rows of the
form (2.52) at the places where the pivots on the diagonal are missing
and obtain
2 3
1 3 0 0 3
60 1 0 0 0 7
6 7
à = 60 0 1 0 9 7
6
7. (2.54)
40 0 0 1 45
0 0 0 0 1
From this form, we can immediately read out the solutions of Ax = 0 by
taking the columns of Ã, which contain 1 on the diagonal:
8 2 3 2 3 9
>
> 3 3 >
>
>
> 6 7 6 7 >
>
< 6 17 6 0 7 =
5 6 7
x 2 R : x = 16 0 7+ 26 9 7 ,6 7 1, 2 2 R , (2.55)
>
> 4 0 5 4 45 >
>
>
> >
>
: ;
0 1
which is identical to the solution in (2.50) that we obtained by “insight”.

Calculating the Inverse


To compute the inverse A 1 of A 2 Rn⇥n , we need to find a matrix X
that satisfies AX = I n . Then, X = A 1 . We can write this down as
a set of simultaneous linear equations AX = I n , where we solve for
X = [x1 | · · · |xn ]. We use the augmented matrix notation for a compact
representation of this set of systems of linear equations and obtain
⇥ ⇤ ⇥ 1

A|I n ··· I n |A . (2.56)

This means that if we bring the augmented equation system into reduced
row-echelon form, we can read out the inverse on the right-hand side of
the equation system. Hence, determining the inverse of a matrix is equiv-
alent to solving systems of linear equations.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


34 Linear Algebra

Example 2.9 (Calculating an Inverse Matrix by Gaussian Elimination)


To determine the inverse of
2 3
1 0 2 0
61 1 0 07
A=6 41 2 0 15
7 (2.57)
1 1 1 1
we write down the augmented matrix
2 3
1 0 2 0 1 0 0 0
6 1 1 0 0 0 1 0 0 7
6 7
4 1 2 0 1 0 0 1 0 5
1 1 1 1 0 0 0 1
and use Gaussian elimination to bring it into reduced row-echelon form
2 3
1 0 0 0 1 2 2 2
6 0 1 0 0 1 1 2 27
6 7,
4 0 0 1 0 1 1 1 15
0 0 0 1 1 0 1 2
such that the desired inverse is given as its right-hand side:
2 3
1 2 2 2
6 1 1 2 27
A 1=6 4 1
7. (2.58)
1 1 15
1 0 1 2
We can verify that (2.58) is indeed the inverse by performing the multi-
plication AA 1 and observing that we recover I 4 .

2.3.4 Algorithms for Solving a System of Linear Equations


In the following, we briefly discuss approaches to solving a system of lin-
ear equations of the form Ax = b. We make the assumption that a solu-
tion exists. Should there be no solution, we need to resort to approximate
solutions, which we do not cover in this chapter. One way to solve the ap-
proximate problem is using the approach of linear regression, which we
discuss in detail in Chapter 9.
In special cases, we may be able to determine the inverse A 1 , such
that the solution of Ax = b is given as x = A 1 b. However, this is
only possible if A is a square matrix and invertible, which is often not the
case. Otherwise, under mild assumptions (i.e., A needs to have linearly
independent columns) we can use the transformation

Ax = b () A> Ax = A> b () x = (A> A) 1 A> b (2.59)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.4 Vector Spaces 35

and use the Moore-Penrose pseudo-inverse (A> A) 1 A> to determine the Moore-Penrose
solution (2.59) that solves Ax = b, which also corresponds to the mini- pseudo-inverse
mum norm least-squares solution. A disadvantage of this approach is that
it requires many computations for the matrix-matrix product and comput-
ing the inverse of A> A. Moreover, for reasons of numerical precision it
is generally not recommended to compute the inverse or pseudo-inverse.
In the following, we therefore briefly discuss alternative approaches to
solving systems of linear equations.
Gaussian elimination plays an important role when computing deter-
minants (Section 4.1), checking whether a set of vectors is linearly inde-
pendent (Section 2.5), computing the inverse of a matrix (Section 2.2.2),
computing the rank of a matrix (Section 2.6.2), and determining a basis
of a vector space (Section 2.6.1). Gaussian elimination is an intuitive and
constructive way to solve a system of linear equations with thousands of
variables. However, for systems with millions of variables, it is impracti-
cal as the required number of arithmetic operations scales cubically in the
number of simultaneous equations.
In practice, systems of many linear equations are solved indirectly, by ei-
ther stationary iterative methods, such as the Richardson method, the Ja-
cobi method, the Gauß-Seidel method, and the successive over-relaxation
method, or Krylov subspace methods, such as conjugate gradients, gener-
alized minimal residual, or biconjugate gradients. We refer to the books
by Stoer and Burlirsch (2002), Strang (2003), and Liesen and Mehrmann
(2015) for further details.
Let x⇤ be a solution of Ax = b. The key idea of these iterative methods
is to set up an iteration of the form
x(k+1) = Cx(k) + d (2.60)
for suitable C and d that reduces the residual error kx(k+1) x⇤ k in every
iteration and converges to x⇤ . We will introduce norms k · k, which allow
us to compute similarities between vectors, in Section 3.1.

2.4 Vector Spaces


Thus far, we have looked at systems of linear equations and how to solve
them (Section 2.3). We saw that systems of linear equations can be com-
pactly represented using matrix-vector notation (2.10). In the following,
we will have a closer look at vector spaces, i.e., a structured space in which
vectors live.
In the beginning of this chapter, we informally characterized vectors as
objects that can be added together and multiplied by a scalar, and they
remain objects of the same type. Now, we are ready to formalize this,
and we will start by introducing the concept of a group, which is a set
of elements and an operation defined on these elements that keeps some
structure of the set intact.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


36 Linear Algebra

2.4.1 Groups
Groups play an important role in computer science. Besides providing a
fundamental framework for operations on sets, they are heavily used in
cryptography, coding theory, and graphics.

Definition 2.7 (Group). Consider a set G and an operation ⌦ : G ⇥G ! G


group defined on G . Then G := (G, ⌦) is called a group if the following hold:
closure
associativity
1. Closure of G under ⌦: 8x, y 2 G : x ⌦ y 2 G
neutral element 2. Associativity: 8x, y, z 2 G : (x ⌦ y) ⌦ z = x ⌦ (y ⌦ z)
inverse element 3. Neutral element: 9e 2 G 8x 2 G : x ⌦ e = x and e ⌦ x = x
4. Inverse element: 8x 2 G 9y 2 G : x ⌦ y = e and y ⌦ x = e. We often
write x 1 to denote the inverse element of x.

Remark. The inverse element is defined with respect to the operation ⌦


and does not necessarily mean x1 . }
Abelian group If additionally 8x, y 2 G : x ⌦ y = y ⌦ x, then G = (G, ⌦) is an Abelian
group (commutative).

Example 2.10 (Groups)


Let us have a look at some examples of sets with associated operations
and see whether they are groups:
(Z, +) is a group.
N0 := N [ {0} (N0 , +) is not a group: Although (N0 , +) possesses a neutral element
(0), the inverse elements are missing.
(Z, ·) is not a group: Although (Z, ·) contains a neutral element (1), the
inverse elements for any z 2 Z, z 6= ±1, are missing.
(R, ·) is not a group since 0 does not possess an inverse element.
(R\{0}, ·) is Abelian.
(Rn , +), (Zn , +), n 2 N are Abelian if + is defined componentwise, i.e.,
(x1 , · · · , xn ) + (y1 , · · · , yn ) = (x1 + y1 , · · · , xn + yn ). (2.61)
Then, (x1 , · · · , xn ) 1 := ( x1 , · · · , xn ) is the inverse element and
e = (0, · · · , 0) is the neutral element.
(Rm⇥n , +), the set of m ⇥ n-matrices is Abelian (with componentwise
addition as defined in (2.61)).
Let us have a closer look at (Rn⇥n , ·), i.e., the set of n ⇥ n-matrices with
matrix multiplication as defined in (2.13).
– Closure and associativity follow directly from the definition of matrix
multiplication.
– Neutral element: The identity matrix I n is the neutral element with
respect to matrix multiplication “·” in (Rn⇥n , ·).

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.4 Vector Spaces 37

– Inverse element: If the inverse exists (A is regular), then A 1 is the


inverse element of A 2 Rn⇥n , and in exactly this case (Rn⇥n , ·) is a
group, called the general linear group.

Definition 2.8 (General Linear Group). The set of regular (invertible)


matrices A 2 Rn⇥n is a group with respect to matrix multiplication as
defined in (2.13) and is called general linear group GL(n, R). However, general linear group
since matrix multiplication is not commutative, the group is not Abelian.

2.4.2 Vector Spaces


When we discussed groups, we looked at sets G and inner operations on
G , i.e., mappings G ⇥ G ! G that only operate on elements in G . In the
following, we will consider sets that in addition to an inner operation +
also contain an outer operation ·, the multiplication of a vector x 2 G by
a scalar 2 R. We can think of the inner operation as a form of addition,
and the outer operation as a form of scaling. Note that the inner/outer
operations have nothing to do with inner/outer products.
Definition 2.9 (Vector Space). A real-valued vector space V = (V, +, ·) is vector space
a set V with two operations
+: V ⇥V !V (2.62)
·: R⇥V !V (2.63)
where
1. (V, +) is an Abelian group
2. Distributivity:
1. 8 2 R, x, y 2 V : · (x + y) = ·x+ ·y
2. 8 , 2 R, x 2 V : ( + ) · x = ·x+ ·x
3. Associativity (outer operation): 8 , 2 R, x 2 V : ·( ·x) = ( )·x
4. Neutral element with respect to the outer operation: 8x 2 V : 1·x = x
The elements x 2 V are called vectors. The neutral element of (V, +) is vector
the zero vector 0 = [0, . . . , 0]> , and the inner operation + is called vector vector addition
addition. The elements 2 R are called scalars and the outer operation scalar
· is a multiplication by scalars. Note that a scalar product is something multiplication by
different, and we will get to this in Section 3.2. scalars

Remark. A “vector multiplication” ab, a, b 2 Rn , is not defined. Theoret-


ically, we could define an element-wise multiplication, such that c = ab
with cj = aj bj . This “array multiplication” is common to many program-
ming languages but makes mathematically limited sense using the stan-
dard rules for matrix multiplication: By treating vectors as n ⇥ 1 matrices

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


38 Linear Algebra

(which we usually do), we can use the matrix multiplication as defined


in (2.13). However, then the dimensions of the vectors do not match. Only
outer product the following multiplications for vectors are defined: ab> 2 Rn⇥n (outer
product), a> b 2 R (inner/scalar/dot product). }

Example 2.11 (Vector Spaces)


Let us have a look at some important examples:
V = Rn , n 2 N is a vector space with operations defined as follows:
– Addition: x+y = (x1 , . . . , xn )+(y1 , . . . , yn ) = (x1 +y1 , . . . , xn +yn )
for all x, y 2 Rn
– Multiplication by scalars: x = (x1 , . . . , xn ) = ( x1 , . . . , xn ) for
all 2 R, x 2 Rn
V = Rm⇥n , m, n 2 N is a vector space with
2 3
a11 + b11 · · · a1n + b1n
– Addition: A + B = 4
6 .. .. 7
. . 5 is defined ele-
am1 + bm1 · · · amn + bmn
mentwise for all A, B 2 V 2 3
a11 · · · a1n
– Multiplication by scalars: A = 4 ... .. 7 as defined in
6
. 5
am1 · · · amn
Section 2.2. Remember that Rm⇥n is equivalent to Rmn .
V = C, with the standard definition of addition of complex numbers.

Remark. In the following, we will denote a vector space (V, +, ·) by V


when + and · are the standard vector addition and scalar multiplication.
Moreover, we will use the notation x 2 V for vectors in V to simplify
notation. }
Remark. The vector spaces Rn , Rn⇥1 , R1⇥n are only different in the way
we write vectors. In the following, we will not make a distinction between
column vector Rn and Rn⇥1 , which allows us to write n-tuples as column vectors
2 3
x1
6 .. 7
x = 4 . 5. (2.64)
xn

This simplifies the notation regarding vector space operations. However,


row vector we do distinguish between Rn⇥1 and R1⇥n (the row vectors) to avoid con-
fusion with matrix multiplication. By default, we write x to denote a col-
transpose umn vector, and a row vector is denoted by x> , the transpose of x. }

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.4 Vector Spaces 39

2.4.3 Vector Subspaces


In the following, we will introduce vector subspaces. Intuitively, they are
sets contained in the original vector space with the property that when
we perform vector space operations on elements within this subspace, we
will never leave it. In this sense, they are “closed”. Vector subspaces are a
key idea in machine learning. For example, Chapter 10 demonstrates how
to use vector subspaces for dimensionality reduction.
Definition 2.10 (Vector Subspace). Let V = (V, +, ·) be a vector space
and U ✓ V , U 6= ;. Then U = (U , +, ·) is called vector subspace of V (or vector subspace
linear subspace) if U is a vector space with the vector space operations + linear subspace
and · restricted to U ⇥ U and R ⇥ U . We write U ✓ V to denote a subspace
U of V .
If U ✓ V and V is a vector space, then U naturally inherits many prop-
erties directly from V because they hold for all x 2 V , and in particular for
all x 2 U ✓ V . This includes the Abelian group properties, the distribu-
tivity, the associativity and the neutral element. To determine whether
(U , +, ·) is a subspace of V we still do need to show
1. U 6= ;, in particular: 0 2 U
2. Closure of U :
a. With respect to the outer operation: 8 2 R 8x 2 U : x 2 U .
b. With respect to the inner operation: 8x, y 2 U : x + y 2 U .

Example 2.12 (Vector Subspaces)


Let us have a look at some examples:
For every vector space V , the trivial subspaces are V itself and {0}.
Only example D in Figure 2.6 is a subspace of R2 (with the usual inner/
outer operations). In A and C , the closure property is violated; B does
not contain 0.
The solution set of a homogeneous system of linear equations Ax = 0
with n unknowns x = [x1 , . . . , xn ]> is a subspace of Rn .
The solution of an inhomogeneous system of linear equations Ax =
b, b 6= 0 is not a subspace of Rn .
The intersection of arbitrarily many subspaces is a subspace itself.

B Figure 2.6 Not all


A
subsets of R2 are
subspaces. In A and
D
C, the closure
0 0 0 0
C property is violated;
B does not contain
0. Only D is a
subspace.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


40 Linear Algebra

Remark. Every subspace U ✓ (Rn , +, ·) is the solution space of a homo-


geneous system of homogeneous linear equations Ax = 0 for x 2 Rn .
}

2.5 Linear Independence


In the following, we will have a close look at what we can do with vectors
(elements of the vector space). In particular, we can add vectors together
and multiply them with scalars. The closure property guarantees that we
end up with another vector in the same vector space. It is possible to find
a set of vectors with which we can represent every vector in the vector
space by adding them together and scaling them. This set of vectors is
a basis, and we will discuss them in Section 2.6.1. Before we get there,
we will need to introduce the concepts of linear combinations and linear
independence.

Definition 2.11 (Linear Combination). Consider a vector space V and a


finite number of vectors x1 , . . . , xk 2 V . Then, every v 2 V of the form
k
X
v= 1 x1 + · · · + k xk = i xi 2V (2.65)
i=1

linear combination with 1, . . . , k 2 R is a linear combination of the vectors x1 , . . . , xk .


The 0-vector can always be P written as the linear combination of k vec-
k
tors x1 , . . . , xk because 0 = i=1 0xi is always true. In the following,
we are interested in non-trivial linear combinations of a set of vectors to
represent 0, i.e., linear combinations of vectors x1 , . . . , xk , where not all
coefficients i in (2.65) are 0.

Definition 2.12 (Linear (In)dependence). Let us consider a vector space


V with k 2 N and x1 , . P . . , xk 2 V . If there is a non-trivial linear com-
k
bination, such that 0 = i=1 i xi with at least one i 6= 0, the vectors
linearly dependent x1 , . . . , xk are linearly dependent. If only the trivial solution exists, i.e.,
linearly 1 = . . . = k = 0 the vectors x1 , . . . , xk are linearly independent.
independent
Linear independence is one of the most important concepts in linear
algebra. Intuitively, a set of linearly independent vectors consists of vectors
that have no redundancy, i.e., if we remove any of those vectors from
the set, we will lose something. Throughout the next sections, we will
formalize this intuition more.

Example 2.13 (Linearly Dependent Vectors)


A geographic example may help to clarify the concept of linear indepen-
dence. A person in Nairobi (Kenya) describing where Kigali (Rwanda) is

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.5 Linear Independence 41

might say ,“You can get to Kigali by first going 506 km Northwest to Kam-
pala (Uganda) and then 374 km Southwest.”. This is sufficient information
to describe the location of Kigali because the geographic coordinate sys-
tem may be considered a two-dimensional vector space (ignoring altitude
and the Earth’s curved surface). The person may add, “It is about 751 km
West of here.” Although this last statement is true, it is not necessary to
find Kigali given the previous information (see Figure 2.7 for an illus-
tration). In this example, the “506 km Northwest” vector (blue) and the
“374 km Southwest” vector (purple) are linearly independent. This means
the Southwest vector cannot be described in terms of the Northwest vec-
tor, and vice versa. However, the third “751 km West” vector (black) is a
linear combination of the other two vectors, and it makes the set of vec-
tors linearly dependent. Equivalently, given “751 km West” and “374 km
Southwest” can be linearly combined to obtain “506 km Northwest”.

Kampala Figure 2.7


506 Geographic example
t
es km (with crude
w No approximations to
th rth
Sou wes
t cardinal directions)
of linearly
km Nairobi
dependent vectors
3 74 751 km West in a
t two-dimensional
es space (plane).
w
Kigali
uth
So
km
3 74

Remark. The following properties are useful to find out whether vectors
are linearly independent:

k vectors are either linearly dependent or linearly independent. There


is no third option.
If at least one of the vectors x1 , . . . , xk is 0 then they are linearly de-
pendent. The same holds if two vectors are identical.
The vectors {x1 , . . . , xk : xi 6= 0, i = 1, . . . , k}, k > 2, are linearly
dependent if and only if (at least) one of them is a linear combination
of the others. In particular, if one vector is a multiple of another vector,
i.e., xi = xj , 2 R then the set {x1 , . . . , xk : xi 6= 0, i = 1, . . . , k}
is linearly dependent.
A practical way of checking whether vectors x1 , . . . , xk 2 V are linearly
independent is to use Gaussian elimination: Write all vectors as columns

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


42 Linear Algebra

of a matrix A and perform Gaussian elimination until the matrix is in


row echelon form (the reduced row-echelon form is unnecessary here):
– The pivot columns indicate the vectors, which are linearly indepen-
dent of the vectors on the left. Note that there is an ordering of vec-
tors when the matrix is built.
– The non-pivot columns can be expressed as linear combinations of
the pivot columns on their left. For instance, the row-echelon form

1 3 0
(2.66)
0 0 2
tells us that the first and third columns are pivot columns. The sec-
ond column is a non-pivot column because it is three times the first
column.
All column vectors are linearly independent if and only if all columns
are pivot columns. If there is at least one non-pivot column, the columns
(and, therefore, the corresponding vectors) are linearly dependent.
}

Example 2.14
Consider R4 with
2 3 2 3 2
3
1 1 1
6 2 7 617 6 27
x1 = 6 7
4 35 , x2 = 6 7
405 , x3 = 6 7
4 1 5. (2.67)
4 2 1
To check whether they are linearly dependent, we follow the general ap-
proach and solve
2 3 2 3 2 3
1 1 1
6 2 7 617 6 27
1 x1 + 2 x2 + 3 x3 = 1 4
6 7+ 26 7+ 36 7 = 0 (2.68)
35 405 4 1 5
4 2 1
for 1 , . . . , 3 . We write the vectors xi , i = 1, 2, 3, as the columns of a
matrix and apply elementary row operations until we identify the pivot
columns:

2 3 2 3
1 1 1 1 1 1
6 2 1 27 60 1 07
6 7 ··· 6 7. (2.69)
4 3 0 15 40 0 15
4 2 1 0 0 0
Here, every column of the matrix is a pivot column. Therefore, there is no
non-trivial solution, and we require 1 = 0, 2 = 0, 3 = 0 to solve the
equation system. Hence, the vectors x1 , x2 , x3 are linearly independent.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.5 Linear Independence 43

Remark. Consider a vector space V with k linearly independent vectors


b1 , . . . , bk and m linear combinations
k
X
x1 = i1 bi ,
i=1
.. (2.70)
.
k
X
xm = im bi .
i=1

Defining B = [b1 , . . . , bk ] as the matrix whose columns are the linearly


independent vectors b1 , . . . , bk , we can write
2 3
1j

= 4 ... 5 ,
6 7
xj = B j , j j = 1, . . . , m , (2.71)
kj

in a more compact form.


We want to test whether x1 , . . . , xm are linearly independent.
Pm For this
purpose, we follow the general approach of testing when j=1 j xj = 0.
With (2.71), we obtain
m
X m
X m
X
j xj = jB j =B j j . (2.72)
j=1 j=1 j=1

This means that {x1 , . . . , xm } are linearly independent if and only if the
column vectors { 1 , . . . , m } are linearly independent.
}
Remark. In a vector space V , m linear combinations of k vectors x1 , . . . , xk
are linearly dependent if m > k . }

Example 2.15
Consider a set of linearly independent vectors b1 , b2 , b3 , b4 2 Rn and
x1 = b1 2b2 + b3 b4
x2 = 4b1 2b2 + 4b4
. (2.73)
x3 = 2b1 + 3b2 b3 3b4
x4 = 17b1 10b2 + 11b3 + b4
Are the vectors x1 , . . . , x4 2 Rn linearly independent? To answer this
question, we investigate whether the column vectors
82 3 2 3 2 3 2 39
>
> 1 4 2 17 >>
<6 7 6 7 6 7 6 7=
6 27 , 6 27 , 6 3 7 , 6 107 (2.74)
> 4 1 5 4 0 5 4 15 4 11 5>
>
: >
;
1 4 3 1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


44 Linear Algebra

are linearly independent. The reduced row-echelon form of the corre-


sponding linear equation system with coefficient matrix
2 3
1 4 2 17
6 2 2 3 107
A=6 4 1
7 (2.75)
0 1 11 5
1 4 3 1
is given as
2 3
1 0 0 7
60 1 0 157
6 7. (2.76)
40 0 1 185
0 0 0 0
We see that the corresponding linear equation system is non-trivially solv-
able: The last column is not a pivot column, and x4 = 7x1 15x2 18x3 .
Therefore, x1 , . . . , x4 are linearly dependent as x4 can be expressed as a
linear combination of x1 , . . . , x3 .

2.6 Basis and Rank


In a vector space V , we are particularly interested in sets of vectors A that
possess the property that any vector v 2 V can be obtained by a linear
combination of vectors in A. These vectors are special vectors, and in the
following, we will characterize them.

2.6.1 Generating Set and Basis


Definition 2.13 (Generating Set and Span). Consider a vector space V =
(V, +, ·) and set of vectors A = {x1 , . . . , xk } ✓ V . If every vector v 2
V can be expressed as a linear combination of x1 , . . . , xk , A is called a
generating set generating set of V . The set of all linear combinations of vectors in A is
span called the span of A. If A spans the vector space V , we write V = span[A]
or V = span[x1 , . . . , xk ].

Generating sets are sets of vectors that span vector (sub)spaces, i.e.,
every vector can be represented as a linear combination of the vectors
in the generating set. Now, we will be more specific and characterize the
smallest generating set that spans a vector (sub)space.

Definition 2.14 (Basis). Consider a vector space V = (V, +, ·) and A ✓


minimal V . A generating set A of V is called minimal if there exists no smaller set
à ✓ A ✓ V that spans V . Every linearly independent generating set of V
basis is minimal and is called a basis of V .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.6 Basis and Rank 45

Let V = (V, +, ·) be a vector space and B ✓ V, B =


6 ;. Then, the
following statements are equivalent: A basis is a minimal
generating set and a
B is a basis of V . maximal linearly
B is a minimal generating set. independent set of
vectors.
B is a maximal linearly independent set of vectors in V , i.e., adding any
other vector to this set will make it linearly dependent.
Every vector x 2 V is a linear combination of vectors from B , and every
linear combination is unique, i.e., with
k
X k
X
x= i bi = i bi (2.77)
i=1 i=1

and i, i 2 R, bi 2 B it follows that i = i, i = 1, . . . , k .

Example 2.16

In R3 , the canonical/standard basis is canonical basis


8 2 3 2 3 2 39
< 1 0 0 =
B= 4 0 , 1 , 05 .
5 4 5 4 (2.78)
: ;
0 0 1
Different bases in R3 are
8 2 3 2 3 2 39 82 3 2 3 2 39
< 1 1 1 = < 0.5 1.8 2.2 =
B1 = 405 , 415 , 415 , B2 = 40.85 , 40.35 , 4 1.35 . (2.79)
: ; : ;
0 0 1 0.4 0.3 3.5
The set
82 3 2 3 2 39
>
> 1 2 1 > >
<6 7 6 =
27 6 17 6 17
A= 6435 , 4
7,6
5 4
7 (2.80)
>
> 0 0 5> >
: ;
4 2 4
is linearly independent, but not a generating set (and no basis) of R4 :
For instance, the vector [1, 0, 0, 0]> cannot be obtained by a linear com-
bination of elements in A.

Remark. Every vector space V possesses a basis B . The preceding exam-


ples show that there can be many bases of a vector space V , i.e., there is
no unique basis. However, all bases possess the same number of elements,
the basis vectors. } basis vector

We only consider finite-dimensional vector spaces V . In this case, the


dimension of V is the number of basis vectors of V , and we write dim(V ). dimension
If U ✓ V is a subspace of V , then dim(U ) 6 dim(V ) and dim(U ) =

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


46 Linear Algebra

dim(V ) if and only if U = V . Intuitively, the dimension of a vector space


can be thought of as the number of independent directions in this vector
The dimension of a space.
vector space
corresponds to the
Remark. The dimension of a vector space is not necessarily the number

number of its basis 0
of elements in a vector. For instance, the vector space V = span[ ] is
vectors. 1
one-dimensional, although the basis vector possesses two elements. }
Remark. A basis of a subspace U = span[x1 , . . . , xm ] ✓ R can be found
n

by executing the following steps:


1. Write the spanning vectors as columns of a matrix A
2. Determine the row-echelon form of A.
3. The spanning vectors associated with the pivot columns are a basis of
U.
}

Example 2.17 (Determining a Basis)


For a vector subspace U ✓ R5 , spanned by the vectors
2 3 2 3 2 3 2 3
1 2 3 1
6 2 7 6 17 6 47 6 87
6 7 6 7 6 7 6 7
x1 = 6 17 , x2 = 6 1 7 , x3 = 6 3 7 , x4 = 6
6 7 6 7 6 7
6 577 2 R , (2.81)
5

4 15 4 2 5 4 5 5 4 65
1 2 3 1
we are interested in finding out which vectors x1 , . . . , x4 are a basis for U .
For this, we need to check whether x1 , . . . , x4 are linearly independent.
Therefore, we need to solve
4
X
i xi = 0, (2.82)
i=1

which leads to a homogeneous system of equations with matrix


2 3
1 2 3 1
6 4 87
⇥ ⇤ 6 2 1 7
6
x1 , x2 , x3 , x4 = 6 1 1 3 57 (2.83)
7.
4 1 2 5 65
1 2 3 1
With the basic transformation rules for systems of linear equations, we
obtain the row-echelon form
2 3 2 3
1 2 3 1 1 2 3 1
6 2 1 4 8 7 6 0 1 2 27
6 7 6 7
6 1 1 3 5 7 · · · 6 0 0 0 1 7.
6 7 6 7
4 1 2 5 6 5 4 0 0 0 0 5
1 2 3 1 0 0 0 0

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.6 Basis and Rank 47

Since the pivot columns indicate which set of vectors is linearly indepen-
dent, we see from the row-echelon form that x1 , x2 , x4 are linearly inde-
pendent (because the system of linear equations 1 x1 + 2 x2 + 4 x4 = 0
can only be solved with 1 = 2 = 4 = 0). Therefore, {x1 , x2 , x4 } is a
basis of U .

2.6.2 Rank
The number of linearly independent columns of a matrix A 2 Rm⇥n
equals the number of linearly independent rows and is called the rank rank
of A and is denoted by rk(A).
Remark. The rank of a matrix has some important properties:
rk(A) = rk(A> ), i.e., the column rank equals the row rank.
The columns of A 2 Rm⇥n span a subspace U ✓ Rm with dim(U ) =
rk(A). Later we will call this subspace the image or range. A basis of
U can be found by applying Gaussian elimination to A to identify the
pivot columns.
The rows of A 2 Rm⇥n span a subspace W ✓ Rn with dim(W ) =
rk(A). A basis of W can be found by applying Gaussian elimination to
A> .
For all A 2 Rn⇥n it holds that A is regular (invertible) if and only if
rk(A) = n.
For all A 2 Rm⇥n and all b 2 Rm it holds that the linear equation
system Ax = b can be solved if and only if rk(A) = rk(A|b), where
A|b denotes the augmented system.
For A 2 Rm⇥n the subspace of solutions for Ax = 0 possesses dimen-
sion n rk(A). Later, we will call this subspace the kernel or the null kernel
space. null space
A matrix A 2 Rm⇥n has full rank if its rank equals the largest possible full rank
rank for a matrix of the same dimensions. This means that the rank of
a full-rank matrix is the lesser of the number of rows and columns, i.e.,
rk(A) = min(m, n). A matrix is said to be rank deficient if it does not rank deficient
have full rank.
}

Example 2.18 (Rank)


2 3
1 0 1
A = 4 0 1 15 .
0 0 0
A has two linearly independent rows/columns so that rk(A) = 2.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


48 Linear Algebra

2 3
1 2 1
A=4 2 3 15 .
3 5 0
We use Gaussian elimination to determine the rank:
2 3 2 3
1 2 1 1 2 1
4 2 3 15 ··· 4 0 1 35 . (2.84)
3 5 0 0 0 0
Here, we see that the number of linearly independent rows and columns
is 2, such that rk(A) = 2.

2.7 Linear Mappings


In the following, we will study mappings on vector spaces that preserve
their structure, which will allow us to define the concept of a coordinate.
In the beginning of the chapter, we said that vectors are objects that can be
added together and multiplied by a scalar, and the resulting object is still
a vector. We wish to preserve this property when applying the mapping:
Consider two real vector spaces V, W . A mapping : V ! W preserves
the structure of the vector space if
(x + y) = (x) + (y) (2.85)
( x) = (x) (2.86)
for all x, y 2 V and 2 R. We can summarize this in the following
definition:
Definition 2.15 (Linear Mapping). For vector spaces V, W , a mapping
linear mapping : V ! W is called a linear mapping (or vector space homomorphism/
vector space linear transformation) if
homomorphism
linear 8x, y 2 V 8 , 2R: ( x + y) = (x) + (y) . (2.87)
transformation
It turns out that we can represent linear mappings as matrices (Sec-
tion 2.7.1). Recall that we can also collect a set of vectors as columns of a
matrix. When working with matrices, we have to keep in mind what the
matrix represents: a linear mapping or a collection of vectors. We will see
more about linear mappings in Chapter 4. Before we continue, we will
briefly introduce special mappings.
Definition 2.16 (Injective, Surjective, Bijective). Consider a mapping :
V ! W , where V, W can be arbitrary sets. Then is called
injective
surjective Injective if 8x, y 2 V : (x) = (y) =) x = y .
bijective Surjective if (V) = W .
Bijective if it is injective and surjective.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.7 Linear Mappings 49

If is surjective, then every element in W can be “reached” from V


using . A bijective can be “undone”, i.e., there exists a mapping :
W ! V so that (x) = x. This mapping is then called the inverse
of and normally denoted by 1
.
With these definitions, we introduce the following special cases of linear
mappings between vector spaces V and W :
isomorphism
Isomorphism: : V ! W linear and bijective endomorphism
Endomorphism: : V ! V linear automorphism
Automorphism: : V ! V linear and bijective
We define idV : V ! V , x 7! x as the identity mapping or identity identity mapping
automorphism in V . identity
automorphism

Example 2.19 (Homomorphism)


The mapping : R2 ! C, (x) = x1 + ix2 , is a homomorphism:
✓  ◆
x1 y
+ 1 = (x1 + y1 ) + i(x2 + y2 ) = x1 + ix2 + y1 + iy2
x2 y2
✓ ◆ ✓ ◆
x1 y1
= +
x2 y2
✓  ◆ ✓ ◆
x1 x1
= x1 + ix2 = (x1 + ix2 ) = .
x2 x2
(2.88)
This also justifies why complex numbers can be represented as tuples in
R2 : There is a bijective linear mapping that converts the elementwise addi-
tion of tuples in R2 into the set of complex numbers with the correspond-
ing addition. Note that we only showed linearity, but not the bijection.

Theorem 2.17 (Theorem 3.59 in Axler (2015)). Finite-dimensional vector


spaces V and W are isomorphic if and only if dim(V ) = dim(W ).
Theorem 2.17 states that there exists a linear, bijective mapping be-
tween two vector spaces of the same dimension. Intuitively, this means
that vector spaces of the same dimension are kind of the same thing, as
they can be transformed into each other without incurring any loss.
Theorem 2.17 also gives us the justification to treat Rm⇥n (the vector
space of m ⇥ n-matrices) and Rmn (the vector space of vectors of length
mn) the same, as their dimensions are mn, and there exists a linear, bi-
jective mapping that transforms one into the other.
Remark. Consider vector spaces V, W, X . Then:
For linear mappings : V ! W and : W ! X , the mapping
: V ! X is also linear.
If : V ! W is an isomorphism, then 1
: W ! V is an isomor-
phism, too.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


50 Linear Algebra

Figure 2.8 Two


different coordinate
systems defined by
two sets of basis
vectors. A vector x
has different
coordinate
x x
representations b2
depending on which
coordinate system is e2
chosen.
b1
e1

If : V ! W, : V ! W are linear, then + and , 2 R, are


linear, too.

2.7.1 Matrix Representation of Linear Mappings


Any n-dimensional vector space is isomorphic to Rn (Theorem 2.17). We
consider a basis {b1 , . . . , bn } of an n-dimensional vector space V . In the
following, the order of the basis vectors will be important. Therefore, we
write

B = (b1 , . . . , bn ) (2.89)

ordered basis and call this n-tuple an ordered basis of V .


Remark (Notation). We are at the point where notation gets a bit tricky.
Therefore, we summarize some parts here. B = (b1 , . . . , bn ) is an ordered
basis, B = {b1 , . . . , bn } is an (unordered) basis, and B = [b1 , . . . , bn ] is a
matrix whose columns are the vectors b1 , . . . , bn . }
Definition 2.18 (Coordinates). Consider a vector space V and an ordered
basis B = (b1 , . . . , bn ) of V . For any x 2 V we obtain a unique represen-
tation (linear combination)

x = ↵1 b1 + . . . + ↵n bn (2.90)

coordinate of x with respect to B . Then ↵1 , . . . , ↵n are the coordinates of x with


respect to B , and the vector
2 3
↵1
6 .. 7
↵ = 4 . 5 2 Rn (2.91)
↵n
coordinate vector is the coordinate vector/coordinate representation of x with respect to the
coordinate ordered basis B .
representation

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.7 Linear Mappings 51

A basis effectively defines a coordinate system. We are familiar with the


Cartesian coordinate system in two dimensions, which is spanned by the
canonical basis vectors e1 , e2 . In this coordinate system, a vector x 2 R2
has a representation that tells us how to linearly combine e1 and e2 to
obtain x. However, any basis of R2 defines a valid coordinate system,
and the same vector x from before may have a different coordinate rep-
resentation in the (b1 , b2 ) basis. In Figure 2.8, the coordinates of x with
respect to the standard basis (e1 , e2 ) is [2, 2]> . However, with respect to
the basis (b1 , b2 ) the same vector x is represented as [1.09, 0.72]> , i.e.,
x = 1.09b1 + 0.72b2 . In the following sections, we will discover how to
obtain this representation.

Example 2.20
Let us have a look at a geometric vector x 2 R2 with coordinates [2, 3]> Figure 2.9
with respect to the standard basis (e1 , e2 ) of R2 . This means, we can write Different coordinate
representations of a
x = 2e1 + 3e2 . However, we do not have to choose the standard basis to
vector x, depending
represent this vector. If we use the basis vectors b1 = [1, 1]> , b2 = [1, 1]> on the choice of
we will obtain the coordinates 12 [ 1, 5]> to represent the same vector with basis.
respect to (b1 , b2 ) (see Figure 2.9). x = 2e1 + 3e2
1
x= 2 b1 + 52 b2

Remark. For an n-dimensional vector space V and an ordered basis B


of V , the mapping : Rn ! V , (ei ) = bi , i = 1, . . . , n, is linear
(and because of Theorem 2.17 an isomorphism), where (e1 , . . . , en ) is e2
b2
the standard basis of Rn .
} e1
b1
Now we are ready to make an explicit connection between matrices and
linear mappings between finite-dimensional vector spaces.
Definition 2.19 (Transformation Matrix). Consider vector spaces V, W
with corresponding (ordered) bases B = (b1 , . . . , bn ) and C = (c1 , . . . , cm ).
Moreover, we consider a linear mapping : V ! W . For j 2 {1, . . . , n},
m
X
(bj ) = ↵1j c1 + · · · + ↵mj cm = ↵ij ci (2.92)
i=1

is the unique representation of (bj ) with respect to C . Then, we call the


m ⇥ n-matrix A , whose elements are given by
A (i, j) = ↵ij , (2.93)
the transformation matrix of (with respect to the ordered bases B of V transformation
and C of W ). matrix

The coordinates of (bj ) with respect to the ordered basis C of W


are the j -th column of A . Consider (finite-dimensional) vector spaces
V, W with ordered bases B, C and a linear mapping : V ! W with

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


52 Linear Algebra

transformation matrix A . If x̂ is the coordinate vector of x 2 V with


respect to B and ŷ the coordinate vector of y = (x) 2 W with respect
to C , then

ŷ = A x̂ . (2.94)

This means that the transformation matrix can be used to map coordinates
with respect to an ordered basis in V to coordinates with respect to an
ordered basis in W .

Example 2.21 (Transformation Matrix)


Consider a homomorphism : V ! W and ordered bases B =
(b1 , . . . , b3 ) of V and C = (c1 , . . . , c4 ) of W . With
(b1 ) = c1 c2 + 3c3 c4
(b2 ) = 2c1 + c2 + 7c3 + 2c4 (2.95)
(b3 ) = 3c2 + c3 + 4c4
the
P4 transformation matrix A with respect to B and C satisfies (bk ) =
i=1 ↵ik ci for k = 1, . . . , 3 and is given as
2 3
1 2 0
6 1 1 37
A = [↵1 , ↵2 , ↵3 ] = 6 4 3
7, (2.96)
7 15
1 2 4
where the ↵j , j = 1, 2, 3, are the coordinate vectors of (bj ) with respect
to C .

Example 2.22 (Linear Transformations of Vectors)

Figure 2.10 Three


examples of linear
transformations of
the vectors shown
as dots in (a);
(b) Rotation by 45 ;
(c) Stretching of the
horizontal
(a) Original data. (b) Rotation by 45 . (c) Stretch along the (d) General linear
coordinates by 2;
horizontal axis. mapping.
(d) Combination of
reflection, rotation We consider three linear transformations of a set of vectors in R2 with
and stretching.
the transformation matrices
  
cos( ⇡4 ) sin( ⇡4 ) 2 0 1 3 1
A1 = ⇡ ⇡ , A2 = , A3 = . (2.97)
sin( 4 ) cos( 4 ) 0 1 2 1 1

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.7 Linear Mappings 53

Figure 2.10 gives three examples of linear transformations of a set of vec-


tors. Figure 2.10(a) shows 400 vectors in R2 , each of which is represented
by a dot at the corresponding (x1 , x2 )-coordinates. The vectors are ar-
ranged in a square. When we use matrix A1 in (2.97) to linearly transform
each of these vectors, we obtain the rotated square in Figure 2.10(b). If we
apply the linear mapping represented by A2 , we obtain the rectangle in
Figure 2.10(c) where each x1 -coordinate is stretched by 2. Figure 2.10(d)
shows the original square from Figure 2.10(a) when linearly transformed
using A3 , which is a combination of a reflection, a rotation, and a stretch.

2.7.2 Basis Change


In the following, we will have a closer look at how transformation matrices
of a linear mapping : V ! W change if we change the bases in V and
W . Consider two ordered bases
B = (b1 , . . . , bn ), B̃ = (b̃1 , . . . , b̃n ) (2.98)
of V and two ordered bases
C = (c1 , . . . , cm ), C̃ = (c̃1 , . . . , c̃m ) (2.99)
of W . Moreover, A 2 Rm⇥n is the transformation matrix of the linear
mapping : V ! W with respect to the bases B and C , and à 2 Rm⇥n
is the corresponding transformation mapping with respect to B̃ and C̃ .
In the following, we will investigate how A and à are related, i.e., how/
whether we can transform A into à if we choose to perform a basis
change from B, C to B̃, C̃ .
Remark. We effectively get different coordinate representations of the
identity mapping idV . In the context of Figure 2.9, this would mean to
map coordinates with respect to (e1 , e2 ) onto coordinates with respect to
(b1 , b2 ) without changing the vector x. By changing the basis and corre-
spondingly the representation of vectors, the transformation matrix with
respect to this new basis can have a particularly simple form that allows
for straightforward computation. }

Example 2.23 (Basis Change)


Consider a transformation matrix

2 1
A= (2.100)
1 2
with respect to the canonical basis in R2 . If we define a new basis
 
1 1
B=( , ) (2.101)
1 1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


54 Linear Algebra

we obtain a diagonal transformation matrix



3 0
à = (2.102)
0 1
with respect to B , which is easier to work with than A.

In the following, we will look at mappings that transform coordinate


vectors with respect to one basis into coordinate vectors with respect to
a different basis. We will state our main result first and then provide an
explanation.
Theorem 2.20 (Basis Change). For a linear mapping : V ! W , ordered
bases
B = (b1 , . . . , bn ), B̃ = (b̃1 , . . . , b̃n ) (2.103)
of V and
C = (c1 , . . . , cm ), C̃ = (c̃1 , . . . , c̃m ) (2.104)
of W , and a transformation matrix A of with respect to B and C , the
corresponding transformation matrix à with respect to the bases B̃ and C̃
is given as
1
à = T A S. (2.105)
Here, S 2 Rn⇥n is the transformation matrix of idV that maps coordinates
with respect to B̃ onto coordinates with respect to B , and T 2 Rm⇥m is the
transformation matrix of idW that maps coordinates with respect to C̃ onto
coordinates with respect to C .
Proof Following Drumm and Weil (2001), we can write the vectors of
the new basis B̃ of V as a linear combination of the basis vectors of B ,
such that
n
X
b̃j = s1j b1 + · · · + snj bn = sij bi , j = 1, . . . , n . (2.106)
i=1

Similarly, we write the new basis vectors C̃ of W as a linear combination


of the basis vectors of C , which yields
m
X
c̃k = t1k c1 + · · · + tmk cm = tlk cl , k = 1, . . . , m . (2.107)
l=1

We define S = ((sij )) 2 Rn⇥n as the transformation matrix that maps


coordinates with respect to B̃ onto coordinates with respect to B and
T = ((tlk )) 2 Rm⇥m as the transformation matrix that maps coordinates
with respect to C̃ onto coordinates with respect to C . In particular, the j th
column of S is the coordinate representation of b̃j with respect to B and

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.7 Linear Mappings 55

the k th column of T is the coordinate representation of c̃k with respect to


C . Note that both S and T are regular.
We are going to look at (b̃j ) from two perspectives. First, applying the
mapping , we get that for all j = 1, . . . , n
m m m m m
!
X (2.107)
X X X X
(b̃j ) = ãkj c̃k = ãkj tlk cl = tlk ãkj cl , (2.108)
k=1
| {z } k=1 l=1 l=1 k=1
2W

where we first expressed the new basis vectors c̃k 2 W as linear com-
binations of the basis vectors cl 2 W and then swapped the order of
summation.
Alternatively, when we express the b̃j 2 V as linear combinations of
bj 2 V , we arrive at
n
! n n m
(2.106)
X X X X
(b̃j ) = sij bi = sij (bi ) = sij ali cl (2.109a)
i=1 i=1 i=1 l=1
m n
!
X X
= ali sij cl , j = 1, . . . , n , (2.109b)
l=1 i=1

where we exploited the linearity of . Comparing (2.108) and (2.109b),


it follows for all j = 1, . . . , n and l = 1, . . . , m that
m
X n
X
tlk ãkj = ali sij (2.110)
k=1 i=1

and, therefore,

T Ã = A S 2 Rm⇥n , (2.111)

such that
1
à = T A S, (2.112)

which proves Theorem 2.20.

Theorem 2.20 tells us that with a basis change in V (B is replaced with


B̃ ) and W (C is replaced with C̃ ), the transformation matrix A of a
linear mapping : V ! W is replaced by an equivalent matrix à with
1
à = T A S. (2.113)

Figure 2.11 illustrates this relation: Consider a homomorphism : V !


W and ordered bases B, B̃ of V and C, C̃ of W . The mapping CB is an
instantiation of and maps basis vectors of B onto linear combinations
of basis vectors of C . Assume that we know the transformation matrix A
of CB with respect to the ordered bases B, C . When we perform a basis
change from B to B̃ in V and from C to C̃ in W , we can determine the

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


56 Linear Algebra

Figure 2.11 For a


Vector spaces V W V W
homomorphism
: V ! W and B
CB
C B
CB
C
ordered bases B, B̃ A A
of V and C, C̃ of W Ordered bases B B̃ S T ⌅C C̃ B B̃ S T 1 1
⌅C̃C = ⌅C C̃
(marked in blue), Ã Ã
we can express the B̃ C̃ B̃ C̃
C̃ B̃ C̃ B̃
mapping C̃ B̃ with
respect to the bases
B̃, C̃ equivalently as
corresponding transformation matrix à as follows: First, we find the ma-
a composition of the
homomorphisms trix representation of the linear mapping B B̃ : V ! V that maps coordi-
C̃ B̃ =
nates with respect to the new basis B̃ onto the (unique) coordinates with
⌅C̃C CB B B̃ respect to the “old” basis B (in V ). Then, we use the transformation ma-
with respect to the
trix A of CB : V ! W to map these coordinates onto the coordinates
bases in the
subscripts. The with respect to C in W . Finally, we use a linear mapping ⌅C̃C : W ! W
corresponding to map the coordinates with respect to C onto coordinates with respect to
transformation C̃ . Therefore, we can express the linear mapping C̃ B̃ as a composition of
matrices are in red. linear mappings that involve the “old” basis:

C̃ B̃ = ⌅C̃C CB B B̃
1
= ⌅C C̃ CB B B̃ . (2.114)
Concretely, we use B B̃ = idV and ⌅C C̃ = idW , i.e., the identity mappings
that map vectors onto themselves, but with respect to a different basis.

equivalent Definition 2.21 (Equivalence). Two matrices A, Ã 2 Rm⇥n are equivalent


if there exist regular matrices S 2 Rn⇥n and T 2 Rm⇥m , such that
à = T 1 AS .
similar Definition 2.22 (Similarity). Two matrices A, Ã 2 Rn⇥n are similar if
there exists a regular matrix S 2 Rn⇥n with à = S 1 AS

Remark. Similar matrices are always equivalent. However, equivalent ma-


trices are not necessarily similar. }
Remark. Consider vector spaces V, W, X . From the remark that follows
Theorem 2.17, we already know that for linear mappings : V ! W
and : W ! X the mapping : V ! X is also linear. With
transformation matrices A and A of the corresponding mappings, the
overall transformation matrix is A =A A . }
In light of this remark, we can look at basis changes from the perspec-
tive of composing linear mappings:

A is the transformation matrix of a linear mapping CB : V ! W


with respect to the bases B, C .
à is the transformation matrix of the linear mapping C̃ B̃ : V ! W
with respect to the bases B̃, C̃ .
S is the transformation matrix of a linear mapping B B̃ : V ! V
(automorphism) that represents B̃ in terms of B . Normally, = idV is
the identity mapping in V .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.7 Linear Mappings 57

T is the transformation matrix of a linear mapping ⌅C C̃ : W ! W


(automorphism) that represents C̃ in terms of C . Normally, ⌅ = idW is
the identity mapping in W .
If we (informally) write down the transformations just in terms of bases,
then A : B ! C , Ã : B̃ ! C̃ , S : B̃ ! B , T : C̃ ! C and
T 1 : C ! C̃ , and
B̃ ! C̃ = B̃ ! B! C ! C̃ (2.115)
1
à = T A S. (2.116)
Note that the execution order in (2.116) is from right to left because vec-
tors are multiplied at the right-hand side so that x 7! Sx 7! A (Sx) 7!
T 1 A (Sx) = Ã x.

Example 2.24 (Basis Change)


Consider a linear mapping : R3 ! R4 whose transformation matrix is
2 3
1 2 0
6 1 1 37
A =6 4 3 7 15
7 (2.117)
1 2 4
with respect to the standard bases
2 3 2 3 2 3 2 3
2 3 2 3 2 3 1 0 0 0
1 0 0 6 07 6 17 6 07 6 07
B = ( 0 , 1 , 05 ) ,
4 5 4 5 4 C = (6 7 6 7 6 7 6 7
405 , 405 , 415 , 405). (2.118)
0 0 1
0 0 0 1
We seek the transformation matrix à of with respect to the new bases

2 3 2 3 2 3 2 3
2 3 2 3 2 3 1 1 0 1
1 0 1 617 607 617 607
B̃ = (415 , 415 , 405) 2 R3 , C̃ = (6 7 6 7 6 7 6 7
405 , 415 , 415 , 405) . (2.119)
0 1 1
0 0 0 1
Then,
2 3
2 3 1 1 0 1
1 0 1 61 0 1 07
S = 41 1 05 , T =6
40
7, (2.120)
1 1 05
0 1 1
0 0 0 1
where the ith column of S is the coordinate representation of b̃i in
terms of the basis vectors of B . Since B is the standard basis, the co-
ordinate representation is straightforward to find. For a general basis B ,
we would need to solve a linear equation system to find the i such that

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


58 Linear Algebra

P3
i=1 i bi = b̃j , j = 1, . . . , 3. Similarly, the j th column of T is the coordi-
nate representation of c̃j in terms of the basis vectors of C .
Therefore, we obtain
2 32 3
1 1 1 1 3 2 1
16 1 1 1 17 6
7 6 0 4 27
7
à = T 1 A S = 6 (2.121a)
24 1 1 1 1 5410 8 45
0 0 0 2 1 6 3
2 3
4 4 2
6 6 0 0 7
=64 4
7. (2.121b)
8 45
1 6 3

In Chapter 4, we will be able to exploit the concept of a basis change


to find a basis with respect to which the transformation matrix of an en-
domorphism has a particularly simple (diagonal) form. In Chapter 10, we
will look at a data compression problem and find a convenient basis onto
which we can project the data while minimizing the compression loss.

2.7.3 Image and Kernel


The image and kernel of a linear mapping are vector subspaces with cer-
tain important properties. In the following, we will characterize them
more carefully.
Definition 2.23 (Image and Kernel).
kernel For : V ! W , we define the kernel/null space
null space
ker( ) := 1
(0W ) = {v 2 V : (v) = 0W } (2.122)
image and the image/range
range
Im( ) := (V ) = {w 2 W |9v 2 V : (v) = w} . (2.123)
domain We also call V and W also the domain and codomain of , respectively.
codomain
Intuitively, the kernel is the set of vectors in v 2 V that maps onto
the neutral element 0W 2 W . The image is the set of vectors w 2 W that
can be “reached” by from any vector in V . An illustration is given in
Figure 2.12.
Remark. Consider a linear mapping : V ! W , where V, W are vector
spaces.
It always holds that (0V ) = 0W and, therefore, 0V 2 ker( ). In
particular, the null space is never empty.
Im( ) ✓ W is a subspace of W , and ker( ) ✓ V is a subspace of V .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.7 Linear Mappings 59

:V !W Figure 2.12 Kernel


V W and image of a
linear mapping
: V ! W.

ker( ) Im( )

0V 0W

is injective (one-to-one) if and only if ker( ) = {0}.


}
Remark (Null Space and Column Space). Let us consider A 2 R m⇥n
and
a linear mapping : Rn ! Rm , x 7! Ax.
For A = [a1 , . . . , an ], where ai are the columns of A, we obtain
( n )
X
Im( ) = {Ax : x 2 R } = n
xi a i : x1 , . . . , x n 2 R (2.124a)
i=1

= span[a1 , . . . , an ] ✓ R , m
(2.124b)
i.e., the image is the span of the columns of A, also called the column column space
space. Therefore, the column space (image) is a subspace of Rm , where
m is the “height” of the matrix.
rk(A) = dim(Im( )).
The kernel/null space ker( ) is the general solution to the homoge-
neous system of linear equations Ax = 0 and captures all possible
linear combinations of the elements in Rn that produce 0 2 Rm .
The kernel is a subspace of Rn , where n is the “width” of the matrix.
The kernel focuses on the relationship among the columns, and we can
use it to determine whether/how we can express a column as a linear
combination of other columns.
}

Example 2.25 (Image and Kernel of a Linear Mapping)


The mapping
2 3 2 3
x1  x1 
6 x 7 1 2 1 0 6 x2 7 x + 2x2 x3
4 2 6
: R ! R , 4 5 7!2 7 6 7 = 1
x3 1 0 0 1 4x3 5 x1 + x4
x4 x4
(2.125a)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


60 Linear Algebra

   
1 2 1 0
= x1 + x2 + x3 + x4 (2.125b)
1 0 0 1
is linear. To determine Im( ), we can take the span of the columns of the
transformation matrix and obtain
   
1 2 1 0
Im( ) = span[ , , , ]. (2.126)
1 0 0 1
To compute the kernel (null space) of , we need to solve Ax = 0, i.e.,
we need to solve a homogeneous equation system. To do this, we use
Gaussian elimination to transform A into reduced row-echelon form:
 
1 2 1 0 1 0 0 1
··· 1 1 . (2.127)
1 0 0 1 0 1 2 2

This matrix is in reduced row-echelon form, and we can use the Minus-
1 Trick to compute a basis of the kernel (see Section 2.3.3). Alternatively,
we can express the non-pivot columns (columns 3 and 4) as linear com-
binations of the pivot columns (columns 1 and 2). The third column a3 is
equivalent to 12 times the second column a2 . Therefore, 0 = a3 + 12 a2 . In
the same way, we see that a4 = a1 12 a2 and, therefore, 0 = a1 12 a2 a4 .
Overall, this gives us the kernel (null space) as
2 3 2 3
0 1
617 6 1 7
ker( ) = span[6 27 6 2 7
4 1 5 , 4 0 5] . (2.128)
0 1

rank-nullity
theorem Theorem 2.24 (Rank-Nullity Theorem). For vector spaces V, W and a lin-
ear mapping : V ! W it holds that
dim(ker( )) + dim(Im( )) = dim(V ) . (2.129)
fundamental The rank-nullity theorem is also referred to as the fundamental theorem
theorem of linear of linear mappings (Axler, 2015, theorem 3.22). The following are direct
mappings
consequences of Theorem 2.24:
If dim(Im( )) < dim(V ), then ker( ) is non-trivial, i.e., the kernel
contains more than 0V and dim(ker( )) > 1.
If A is the transformation matrix of with respect to an ordered basis
and dim(Im( )) < dim(V ), then the system of linear equations A x =
0 has infinitely many solutions.
If dim(V ) = dim(W ), then the following three-way equivalence holds:
– is injective
– is surjective
– is bijective
since Im( ) ✓ W .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.8 Affine Spaces 61

2.8 Affine Spaces


In the following, we will have a closer look at spaces that are offset from
the origin, i.e., spaces that are no longer vector subspaces. Moreover, we
will briefly discuss properties of mappings between these affine spaces,
which resemble linear mappings.
Remark. In the machine learning literature, the distinction between linear
and affine is sometimes not clear so that we can find references to affine
spaces/mappings as linear spaces/mappings. }

2.8.1 Affine Subspaces


Definition 2.25 (Affine Subspace). Let V be a vector space, x0 2 V and
U ✓ V a subspace. Then the subset
L = x0 + U := {x0 + u : u 2 U } (2.130a)
= {v 2 V |9u 2 U : v = x0 + u} ✓ V (2.130b)
is called affine subspace or linear manifold of V . U is called direction or affine subspace
direction space, and x0 is called support point. In Chapter 12, we refer to linear manifold
such a subspace as a hyperplane. direction
direction space
Note that the definition of an affine subspace excludes 0 if x0 2 / U. support point
Therefore, an affine subspace is not a (linear) subspace (vector subspace) hyperplane
of V for x0 2
/ U.
Examples of affine subspaces are points, lines, and planes in R3 , which
do not (necessarily) go through the origin.
Remark. Consider two affine subspaces L = x0 + U and L̃ = x̃0 + Ũ of a
vector space V . Then, L ✓ L̃ if and only if U ✓ Ũ and x0 x̃0 2 Ũ .
Affine subspaces are often described by parameters: Consider a k -dimen-
sional affine space L = x0 + U of V . If (b1 , . . . , bk ) is an ordered basis of
U , then every element x 2 L can be uniquely described as
x = x0 + 1 b1 + ... + k bk , (2.131)
where 1 , . . . , k 2 R. This representation is called parametric equation parametric equation
of L with directional vectors b1 , . . . , bk and parameters 1 , . . . , k . } parameters

Example 2.26 (Affine Subspaces)

One-dimensional affine subspaces are called lines and can be written line
as y = x0 + x1 , where 2 R, where U = span[x1 ] ✓ Rn is a
one-dimensional subspace of Rn . This means that a line is defined by
a support point x0 and a vector x1 that defines the direction. See Fig-
ure 2.13 for an illustration.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


62 Linear Algebra

plane Two-dimensional affine subspaces of Rn are called planes. The para-


metric equation for planes is y = x0 + 1 x1 + 2 x2 , where 1 , 2 2 R
and U = span[x1 , x2 ] ✓ Rn . This means that a plane is defined by a
support point x0 and two linearly independent vectors x1 , x2 that span
the direction space.
hyperplane In Rn , the (n 1)-dimensional affine subspaces are called hyperplanes,
Pn 1
and the corresponding parametric equation is y = x0 + i=1 i xi ,
where x1 , . . . , xn 1 form a basis of an (n 1)-dimensional subspace
U of Rn . This means that a hyperplane is defined by a support point
x0 and (n 1) linearly independent vectors x1 , . . . , xn 1 that span the
direction space. In R2 , a line is also a hyperplane. In R3 , a plane is also
a hyperplane.

Figure 2.13 Vectors


+ u
y on a line lie in an
L = x0
affine subspace L
with support point y
x0 and direction u.
x0
u
0

Remark (Inhomogeneous systems of linear equations and affine subspaces).


For A 2 Rm⇥n and b 2 Rm , the solution of the linear equation sys-
tem Ax = b is either the empty set or an affine subspace of Rn of
dimension n rk(A). In particular, the solution of the linear equation
1 x1 + . . . + n xn = b, where ( 1 , . . . , n ) 6= (0, . . . , 0), is a hyperplane
in Rn .
In Rn , every k -dimensional affine subspace is the solution of a linear
inhomogeneous equation system Ax = b, where A 2 Rm⇥n , b 2 Rm and
rk(A) = n k . Recall that for homogeneous equation systems Ax = 0
the solution was a vector subspace, which we can also think of as a special
affine space with support point x0 = 0. }

2.8.2 Affine Mappings


Similar to linear mappings between vector spaces, which we discussed
in Section 2.7, we can define affine mappings between two affine spaces.
Linear and affine mappings are closely related. Therefore, many properties
that we already know from linear mappings, e.g., that the composition of
linear mappings is a linear mapping, also hold for affine mappings.

Definition 2.26 (Affine Mapping). For two vector spaces V, W , a linear

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


2.9 Further Reading 63

mapping : V ! W , and a 2 W , the mapping


:V !W (2.132)
x 7! a + (x) (2.133)
is an affine mapping from V to W . The vector a is called the translation affine mapping
vector of . translation vector

Every affine mapping : V ! W is also the composition of a linear


mapping : V ! W and a translation ⌧ : W ! W in W , such that
=⌧ . The mappings and ⌧ are uniquely determined.
The composition 0 of affine mappings : V ! W , 0 : W ! X is
affine.
Affine mappings keep the geometric structure invariant. They also pre-
serve the dimension and parallelism.

2.9 Further Reading


There are many resources for learning linear algebra, including the text-
books by Strang (2003), Golan (2007), Axler (2015), and Liesen and
Mehrmann (2015). There are also several online resources that we men-
tioned in the introduction to this chapter. We only covered Gaussian elim-
ination here, but there are many other approaches for solving systems of
linear equations, and we refer to numerical linear algebra textbooks by
Stoer and Burlirsch (2002), Golub and Van Loan (2012), and Horn and
Johnson (2013) for an in-depth discussion.
In this book, we distinguish between the topics of linear algebra (e.g.,
vectors, matrices, linear independence, basis) and topics related to the
geometry of a vector space. In Chapter 3, we will introduce the inner
product, which induces a norm. These concepts allow us to define angles,
lengths and distances, which we will use for orthogonal projections. Pro-
jections turn out to be key in many machine learning algorithms, such as
linear regression and principal component analysis, both of which we will
cover in Chapters 9 and 10, respectively.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


64 Linear Algebra

Exercises
2.1 We consider (R\{ 1}, ?), where

a ? b := ab + a + b, a, b 2 R\{ 1} (2.134)

a. Show that (R\{ 1}, ?) is an Abelian group.


b. Solve

3 ? x ? x = 15

in the Abelian group (R\{ 1}, ?), where ? is defined in (2.134).


2.2 Let n be in N\{0}. Let k, x be in Z. We define the congruence class k̄ of the
integer k as the set

k = {x 2 Z | x k = 0 (modn)}
= {x 2 Z | (9a 2 Z) : (x k = n · a)} .

We now define Z/nZ (sometimes written Zn ) as the set of all congruence


classes modulo n. Euclidean division implies that this set is a finite set con-
taining n elements:

Zn = {0, 1, . . . , n 1}

For all a, b 2 Zn , we define

a b := a + b

a. Show that (Zn , ) is a group. Is it Abelian?


b. We now define another operation ⌦ for all a and b in Zn as

a ⌦ b = a ⇥ b, (2.135)

where a ⇥ b represents the usual multiplication in Z.


Let n = 5. Draw the times table of the elements of Z5 \{0} under ⌦, i.e.,
calculate the products a ⌦ b for all a and b in Z5 \{0}.
Hence, show that Z5 \{0} is closed under ⌦ and possesses a neutral
element for ⌦. Display the inverse of all elements in Z5 \{0} under ⌦.
Conclude that (Z5 \{0}, ⌦) is an Abelian group.
c. Show that (Z8 \{0}, ⌦) is not a group.
d. We recall that the Bézout theorem states that two integers a and b are
relatively prime (i.e., gcd(a, b) = 1) if and only if there exist two integers
u and v such that au + bv = 1. Show that (Zn \{0}, ⌦) is a group if and
only if n 2 N\{0} is prime.
2.3 Consider the set G of 3 ⇥ 3 matrices defined as follows:
82 3 9
< 1 x z =
G = 40 1 y 5 2 R3⇥3 x, y, z 2 R (2.136)
: ;
0 0 1

We define · as the standard matrix multiplication.


Is (G, ·) a group? If yes, is it Abelian? Justify your answer.
2.4 Compute the following matrix products, if possible:

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Exercises 65

a.
2 32 3
1 2 1 1 0
44 5 5 40 1 15
7 8 1 0 1

b.
2 32 3
1 2 3 1 1 0
44 5 6 5 40 1 15
7 8 9 1 0 1

c.
2 32 3
1 1 0 1 2 3
40 1 1 5 44 5 65
1 0 1 7 8 9

d.
2 3
0 3

1 2 1 2 661 17
7
4 1 1 4 42 1 5
5 2

e.
2 3
0 3

61 17
6 7 1 2 1 2
42 1 5 4 1 1 4
5 2

2.5 Find the set S of all solutions in x of the following inhomogeneous linear
systems Ax = b, where A and b are defined as follows:
a.
2 3 2 3
1 1 1 1 1
62 5 7 57 6 27
A=6
42
7, b=6 7
1 1 3 5 445
5 2 4 2 6

b.
2 3 2 3
1 1 0 0 1 3
61 1 0 3 0 7 667
A=6
42
7, b=6 7
1 0 1 15 455
1 2 0 2 1 1

2.6 Using Gaussian elimination, find all solutions of the inhomogeneous equa-
tion system Ax = b with
2 3 2 3
0 1 0 0 1 0 2
A = 40 0 0 1 1 05 , b = 4 15
0 1 0 0 0 1 1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


66 Linear Algebra
2 3
x1
2.7 Find all solutions in x = 4x2 5 2 R3 of the equation system Ax = 12x,
x3
where
2 3
6 4 3
A = 46 0 95
0 8 0
P
and 3i=1 xi = 1.
2.8 Determine the inverses of the following matrices if possible:
a.
2 3
2 3 4
A = 43 4 55
4 5 6
b.
2 3
1 0 1 0
60 1 1 07
A=6
41
7
1 0 15
1 1 1 0

2.9 Which of the following sets are subspaces of R3 ?


a. A = {( , + µ3 , µ3 ) | , µ 2 R}
b. B = {( ,
2 2
, 0) | 2 R}
c. Let be in R.
C = {(⇠1 , ⇠2 , ⇠3 ) 2 R3 | ⇠1 2⇠2 + 3⇠3 = }
d. D = {(⇠1 , ⇠2 , ⇠3 ) 2 R3 | ⇠2 2 Z}
2.10 Are the following sets of vectors linearly independent?
a.
2 3 2 3 2 3
2 1 3
x1 = 4 1 5 , x2 = 4 1 5 , x3 = 4 3 5
3 2 8
b.
2 3 2 3 2 3
1 1 1
627 61 7 607
6 7 6 7 6 7
x1 = 6 7
617 , x2 = 6 7
60 7 , x3 = 6 7
607
405 41 5 415
0 1 1
2.11 Write
2 3
1
y = 4 25
5
as linear combination of
2 3 2 3 2 3
1 1 2
x1 = 4 1 5 , x2 = 4 2 5 , x3 = 4 1 5
1 3 1

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Exercises 67

2.12 Consider two subspaces of R4 :


2 3 2 3 2 3 2 3 2 3 2 3
1 2 1 1 2 3
6 1 7 6 17 6 1 7 6 27 6 27 6 6 7
U1 = span[6 7 6 7 6 7
4 35 , 4 0 5 , 4 15] , U2 = span[6 7 6 7 6 7
4 2 5 , 4 0 5 , 4 25] .
1 1 1 1 0 1

Determine a basis of U1 \ U2 .
2.13 Consider two subspaces U1 and U2 , where U1 is the solution space of the
homogeneous equation system A1 x = 0 and U2 is the solution space of the
homogeneous equation system A2 x = 0 with
2 3 2 3
1 0 1 3 3 0
61 2 17 61 2 37
A1 = 6
42
7, A2 = 6 7.
1 3 5 47 5 25
1 0 1 3 1 2

a. Determine the dimension of U1 , U2 .


b. Determine bases of U1 and U2 .
c. Determine a basis of U1 \ U2 .
2.14 Consider two subspaces U1 and U2 , where U1 is spanned by the columns of
A1 and U2 is spanned by the columns of A2 with
2 3 2 3
1 0 1 3 3 0
61 2 17 61 2 37
A1 = 6
42
7, A2 = 6 7.
1 3 5 47 5 25
1 0 1 3 1 2

a. Determine the dimension of U1 , U2


b. Determine bases of U1 and U2
c. Determine a basis of U1 \ U2
2.15 Let F = {(x, y, z) 2 R3 | x+y z = 0} and G = {(a b, a+b, a 3b) | a, b 2 R}.
a. Show that F and G are subspaces of R3 .
b. Calculate F \ G without resorting to any basis vector.
c. Find one basis for F and one for G, calculate F \G using the basis vectors
previously found and check your result with the previous question.
2.16 Are the following mappings linear?
a. Let a, b 2 R.
: L1 ([a, b]) ! R
Z b
f 7! (f ) = f (x)dx ,
a

where L1 ([a, b]) denotes the set of integrable functions on [a, b].
b.
: C1 ! C0
f 7! (f ) = f 0 ,

where for k > 1, C k denotes the set of k times continuously differen-


tiable functions, and C 0 denotes the set of continuous functions.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


68 Linear Algebra

c.
:R!R
x 7! (x) = cos(x)

d.
: R3 ! R2

1 2 3
x 7! x
1 4 3

e. Let ✓ be in [0, 2⇡[.


: R2 ! R2

cos(✓) sin(✓)
x 7! x
sin(✓) cos(✓)

2.17 Consider the linear mapping


: R3 ! R4
2 3
02 31 3x1 + 2x2 + x3
x1 6 x1 + x2 + x3 7
@4 x 2 5 A = 6 7
4 x1 3x2 5
x3
2x1 + 3x2 + x3

Find the transformation matrix A .


Determine rk(A ).
Compute the kernel and image of . What are dim(ker( )) and dim(Im( ))?
2.18 Let E be a vector space. Let f and g be two automorphisms on E such that
f g = idE (i.e., f g is the identity mapping idE ). Show that ker(f ) =
ker(g f ), Im(g) = Im(g f ) and that ker(f ) \ Im(g) = {0E }.
2.19 Consider an endomorphism : R3 ! R3 whose transformation matrix
(with respect to the standard basis in R3 ) is
2 3
1 1 0
A = 41 1 05 .
1 1 1

1. Determine ker( ) and Im( ).


2. Determine the transformation matrix à with respect to the basis
2 3 2 3 2 3
1 1 1
B = ( 41 5 , 42 5 , 40 5) ,
1 1 0

i.e., perform a basis change toward the new basis B .


2.20 Let us consider b1 , b2 , b01 , b02 , 4 vectors of R2 expressed in the standard basis
of R2 as
   
2 1 2 1
b1 = , b2 = , b01 = , b02 =
1 1 2 1

and let us define two ordered bases B = (b1 , b2 ) and B 0 = (b01 , b02 ) of R2 .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Exercises 69

1. Show that B and B 0 are two bases of R2 and draw those basis vectors.
2. Compute the matrix P 1 that performs a basis change from B 0 to B .
3. We consider c1 , c2 , c3 , three vectors of R3 defined in the standard basis
of R as
2 3 2 3 2 3
1 0 1
c1 = 4 2 5 , c2 = 4 1 5 , c3 = 4 0 5
1 2 1

and we define C = (c1 , c2 , c3 ).


a. Show that C is a basis of R3 , e.g., by using determinants (see Sec-
tion 4.1).
b. Let us call C 0 = (c01 , c02 , c03 ) the standard basis of R3 . Determine the
matrix P 2 that performs the basis change from C to C 0 .
4. We consider a homomorphism : R2 ! R3 , such that
(b1 + b2 ) = c2 + c3
(b1 b2 ) = 2c1 c2 + 3c3

where B = (b1 , b2 ) and C = (c1 , c2 , c3 ) are ordered bases of R2 and R3 ,


respectively.
Determine the transformation matrix A of with respect to the ordered
bases B and C .
5. Determine A0 , the transformation matrix of with respect to the bases
B 0 and C 0 .
6. Let us consider the vector x 2 R2 whose coordinates in B 0 are [2, 3]> . In
other words, x = 2b01 + 3b02 .
a. Calculate the coordinates of x in B .
b. Based on that, compute the coordinates of (x) expressed in C .
c. Then, write (x) in terms of c01 , c02 , c03 .
d. Use the representation of x in B 0 and the matrix A0 to find this result
directly.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


3

Analytic Geometry

In Chapter 2, we studied vectors, vector spaces, and linear mappings at


a general but abstract level. In this chapter, we will add some geomet-
ric interpretation and intuition to all of these concepts. In particular, we
will look at geometric vectors and compute their lengths and distances
or angles between two vectors. To be able to do this, we equip the vec-
tor space with an inner product that induces the geometry of the vector
space. Inner products and their corresponding norms and metrics capture
the intuitive notions of similarity and distances, which we use to develop
the support vector machine in Chapter 12. We will then use the concepts
of lengths and angles between vectors to discuss orthogonal projections,
which will play a central role when we discuss principal component anal-
ysis in Chapter 10 and regression via maximum likelihood estimation in
Chapter 9. Figure 3.1 gives an overview of how concepts in this chapter
are related and how they are connected to other chapters of the book.

Figure 3.1 A mind


Inner product
map of the concepts
introduced in this
es
chapter, along with uc
when they are used ind
in other parts of the
Chapter 12
book. Norm Classification

Orthogonal
Lengths Angles Rotations
projection

Chapter 9 Chapter 4 Chapter 10


Regression Matrix Dimensionality
decomposition reduction

70
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
3.1 Norms 71

kx k1 = 1 kx k2 = 1 Figure 3.3 For


1 1
different norms, the
red lines indicate
the set of vectors
with norm 1. Left:
1 1 Manhattan norm;
Right: Euclidean
distance.

3.1 Norms
When we think of geometric vectors, i.e., directed line segments that start
at the origin, then intuitively the length of a vector is the distance of the
“end” of this directed line segment from the origin. In the following, we
will discuss the notion of the length of vectors using the concept of a norm.

Definition 3.1 (Norm). A norm on a vector space V is a function norm

k · k : V ! R, (3.1)
x 7! kxk , (3.2)

which assigns each vector x its length kxk 2 R, such that for all 2R length
and x, y 2 V the following hold:
absolutely
Absolutely homogeneous: k xk = | |kxk homogeneous

Triangle inequality: kx + yk 6 kxk + kyk triangle inequality

Positive definite: kxk > 0 and kxk = 0 () x = 0 positive definite

Figure 3.2 Triangle


In geometric terms, the triangle inequality states that for any triangle, inequality.
the sum of the lengths of any two sides must be greater than or equal
a
to the length of the remaining side; see Figure 3.2 for an illustration. b

Definition 3.1 is in terms of a general vector space V (Section 2.4), but ca+b

in this book we will only consider a finite-dimensional vector space Rn .


Recall that for a vector x 2 Rn we denote the elements of the vector using
a subscript, that is, xi is the ith element of the vector x.

Example 3.1 (Manhattan Norm)


The Manhattan norm on Rn is defined for x 2 Rn as Manhattan norm

Xn
kxk1 := |xi | , (3.3)
i=1

where | · | is the absolute value. The left panel of Figure 3.3 shows all
vectors x 2 R2 with kxk1 = 1. The Manhattan norm is also called `1 `1 norm
norm.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


72 Analytic Geometry

Example 3.2 (Euclidean Norm)


Euclidean norm The Euclidean norm of x 2 Rn is defined as
v
u n
uX p
kxk2 := t x2i = x> x (3.4)
i=1

Euclidean distance and computes the Euclidean distance of x from the origin. The right panel
of Figure 3.3 shows all vectors x 2 R2 with kxk2 = 1. The Euclidean
`2 norm norm is also called `2 norm.

Remark. Throughout this book, we will use the Euclidean norm (3.4) by
default if not stated otherwise. }

3.2 Inner Products


Inner products allow for the introduction of intuitive geometrical con-
cepts, such as the length of a vector and the angle or distance between
two vectors. A major purpose of inner products is to determine whether
vectors are orthogonal to each other.

3.2.1 Dot Product


We may already be familiar with a particular type of inner product, the
scalar product scalar product/dot product in Rn , which is given by
dot product n
X
x> y = x i yi . (3.5)
i=1

We will refer to this particular inner product as the dot product in this
book. However, inner products are more general concepts with specific
properties, which we will now introduce.

3.2.2 General Inner Products


Recall the linear mapping from Section 2.7, where we can rearrange the
bilinear mapping mapping with respect to addition and multiplication with a scalar. A bi-
linear mapping ⌦ is a mapping with two arguments, and it is linear in
each argument, i.e., when we look at a vector space V then it holds that
for all x, y, z 2 V, , 2 R that
⌦( x + y, z) = ⌦(x, z) + ⌦(y, z) (3.6)
⌦(x, y + z) = ⌦(x, y) + ⌦(x, z) . (3.7)
Here, (3.6) asserts that ⌦ is linear in the first argument, and (3.7) asserts
that ⌦ is linear in the second argument (see also (2.87)).

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.2 Inner Products 73

Definition 3.2. Let V be a vector space and ⌦ : V ⇥ V ! R be a bilinear


mapping that takes two vectors and maps them onto a real number. Then
⌦ is called symmetric if ⌦(x, y) = ⌦(y, x) for all x, y 2 V , i.e., the symmetric
order of the arguments does not matter.
⌦ is called positive definite if positive definite

8x 2 V \{0} : ⌦(x, x) > 0 , ⌦(0, 0) = 0 . (3.8)


Definition 3.3. Let V be a vector space and ⌦ : V ⇥ V ! R be a bilinear
mapping that takes two vectors and maps them onto a real number. Then
A positive definite, symmetric bilinear mapping ⌦ : V ⇥V ! R is called
an inner product on V . We typically write hx, yi instead of ⌦(x, y). inner product
The pair (V, h·, ·i) is called an inner product space or (real) vector space inner product space
with inner product. If we use the dot product defined in (3.5), we call vector space with
(V, h·, ·i) a Euclidean vector space. inner product
Euclidean vector
We will refer to these spaces as inner product spaces in this book. space

Example 3.3 (Inner Product That Is Not the Dot Product)


Consider V = R2 . If we define
hx, yi := x1 y1 (x1 y2 + x2 y1 ) + 2x2 y2 (3.9)
then h·, ·i is an inner product but different from the dot product. The proof
will be an exercise.

3.2.3 Symmetric, Positive Definite Matrices


Symmetric, positive definite matrices play an important role in machine
learning, and they are defined via the inner product. In Section 4.3, we
will return to symmetric, positive definite matrices in the context of matrix
decompositions. The idea of symmetric positive semidefinite matrices is
key in the definition of kernels (Section 12.4).
Consider an n-dimensional vector space V with an inner product h·, ·i :
V ⇥ V ! R (see Definition 3.3) and an ordered basis B = (b1 , . . . , bn ) of
V . Recall from Section 2.6.1 that any vectors x, y 2 V Pncan be written as
linearPcombinations of the basis vectors so that x = i=1 i bi 2 V and
n
y = j=1 j bj 2 V for suitable i , j 2 R. Due to the bilinearity of the
inner product, it holds for all x, y 2 V that
* n n
+ n X n
X X X >
hx, yi = i bi , j bj = i hbi , bj i j = x̂ Aŷ , (3.10)
i=1 j=1 i=1 j=1

where Aij := hbi , bj i and x̂, ŷ are the coordinates of x and y with respect
to the basis B . This implies that the inner product h·, ·i is uniquely deter-
mined through A. The symmetry of the inner product also means that A

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


74 Analytic Geometry

is symmetric. Furthermore, the positive definiteness of the inner product


implies that

8x 2 V \{0} : x> Ax > 0 . (3.11)

Definition 3.4 (Symmetric, Positive Definite Matrix). A symmetric matrix


symmetric, positive A 2 Rn⇥n that satisfies (3.11) is called symmetric, positive definite, or
definite just positive definite. If only > holds in (3.11), then A is called symmetric,
positive definite
positive semidefinite.
symmetric, positive
semidefinite

Example 3.4 (Symmetric, Positive Definite Matrices)


Consider the matrices
 
9 6 9 6
A1 = , A2 = . (3.12)
6 5 6 3
A1 is positive definite because it is symmetric and
 
⇥ ⇤ 9 6 x1
>
x A1 x = x 1 x 2 (3.13a)
6 5 x2
= 9x21 + 12x1 x2 + 5x22 = (3x1 + 2x2 )2 + x22 > 0 (3.13b)
for all x 2 V \{0}. In contrast, A2 is symmetric but not positive definite
because x> A2 x = 9x21 + 12x1 x2 + 3x22 = (3x1 + 2x2 )2 x22 can be less
than 0, e.g., for x = [2, 3]> .

If A 2 Rn⇥n is symmetric, positive definite, then

hx, yi = x̂> Aŷ (3.14)

defines an inner product with respect to an ordered basis B , where x̂ and


ŷ are the coordinate representations of x, y 2 V with respect to B .

Theorem 3.5. For a real-valued, finite-dimensional vector space V and an


ordered basis B of V , it holds that h·, ·i : V ⇥ V ! R is an inner product if
and only if there exists a symmetric, positive definite matrix A 2 Rn⇥n with

hx, yi = x̂> Aŷ . (3.15)

The following properties hold if A 2 Rn⇥n is symmetric and positive


definite:

The null space (kernel) of A consists only of 0 because x> Ax > 0 for
all x 6= 0. This implies that Ax 6= 0 if x 6= 0.
The diagonal elements aii of A are positive because aii = e>
i Aei > 0,
where ei is the ith vector of the standard basis in Rn .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.3 Lengths and Distances 75

3.3 Lengths and Distances


In Section 3.1, we already discussed norms that we can use to compute
the length of a vector. Inner products and norms are closely related in the
sense that any inner product induces a norm Inner products
q induce norms.
kxk := hx, xi (3.16)

in a natural way, such that we can compute lengths of vectors using the in-
ner product. However, not every norm is induced by an inner product. The
Manhattan norm (3.3) is an example of a norm without a corresponding
inner product. In the following, we will focus on norms that are induced
by inner products and introduce geometric concepts, such as lengths, dis-
tances, and angles.
Remark (Cauchy-Schwarz Inequality). For an inner product vector space
(V, h·, ·i) the induced norm k · k satisfies the Cauchy-Schwarz inequality Cauchy-Schwarz
inequality
| hx, yi | 6 kxkkyk . (3.17)
}

Example 3.5 (Lengths of Vectors Using Inner Products)


In geometry, we are often interested in lengths of vectors. We can now use
an inner product to compute them using (3.16). Let us take x = [1, 1]> 2
R2 . If we use the dot product as the inner product, with (3.16) we obtain
p p p
kxk = x> x = 12 + 12 = 2 (3.18)
as the length of x. Let us now choose a different inner product:
 1
1 1
hx, yi := x >
1
2 y = x y
1 1 (x1 y2 + x2 y1 ) + x2 y2 . (3.19)
2
1 2
If we compute the norm of a vector, then this inner product returns smaller
values than the dot product if x1 and x2 have the same sign (and x1 x2 >
0); otherwise, it returns greater values than the dot product. With this
inner product, we obtain
p
hx, xi = x21 x1 x2 + x22 = 1 1 + 1 = 1 =) kxk = 1 = 1 , (3.20)
such that x is “shorter” with this inner product than with the dot product.

Definition 3.6 (Distance and Metric). Consider an inner product space


(V, h·, ·i). Then
q
d(x, y) := kx yk = hx y, x yi (3.21)

is called the distance between x and y for x, y 2 V . If we use the dot distance
product as the inner product, then the distance is called Euclidean distance. Euclidean distance

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


76 Analytic Geometry

The mapping

d:V ⇥V !R (3.22)
(x, y) 7! d(x, y) (3.23)

metric is called a metric.

Remark. Similar to the length of a vector, the distance between vectors


does not require an inner product: a norm is sufficient. If we have a norm
induced by an inner product, the distance may vary depending on the
choice of the inner product. }
A metric d satisfies the following:

positive definite 1. d is positive definite, i.e., d(x, y) > 0 for all x, y 2 V and d(x, y) =
0 () x = y .
symmetric 2. d is symmetric, i.e., d(x, y) = d(y, x) for all x, y 2 V .
triangle inequality 3. Triangle inequality: d(x, z) 6 d(x, y) + d(y, z) for all x, y, z 2 V .

Remark. At first glance, the lists of properties of inner products and met-
rics look very similar. However, by comparing Definition 3.3 with Defini-
tion 3.6 we observe that hx, yi and d(x, y) behave in opposite directions.
Very similar x and y will result in a large value for the inner product and
a small value for the metric. }

3.4 Angles and Orthogonality


Figure 3.4 When
restricted to [0, ⇡] In addition to enabling the definition of lengths of vectors, as well as the
then f (!) = cos(!) distance between two vectors, inner products also capture the geometry
returns a unique
number in the
of a vector space by defining the angle ! between two vectors. We use
interval [ 1, 1]. the Cauchy-Schwarz inequality (3.17) to define angles ! in inner prod-
uct spaces between two vectors x, y , and this notion coincides with our
1
intuition in R2 and R3 . Assume that x 6= 0, y 6= 0. Then
cos( )

0
hx, yi
16 6 1. (3.24)
kxk kyk
1
0 ⇡/2 ⇡
Therefore, there exists a unique ! 2 [0, ⇡], illustrated in Figure 3.4, with

hx, yi
cos ! = . (3.25)
kxk kyk
angle The number ! is the angle between the vectors x and y . Intuitively, the
angle between two vectors tells us how similar their orientations are. For
example, using the dot product, the angle between x and y = 4x, i.e., y
is a scaled version of x, is 0: Their orientation is the same.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.4 Angles and Orthogonality 77

Example 3.6 (Angle between Vectors)


Let us compute the angle between x = [1, 1]> 2 R2 and y = [1, 2]> 2 R2 ; Figure 3.5 The
see Figure 3.5, where we use the dot product as the inner product. Then angle ! between
two vectors x, y is
we get
computed using the
hx, yi x> y 3 inner product.
cos ! = p =p =p , (3.26)
hx, xi hy, yi x> xy > y 10
y
and the angle between the two vectors is arccos( p310 ) ⇡ 0.32 rad, which
corresponds to about 18 .
1
! x
A key feature of the inner product is that it also allows us to characterize
vectors that are orthogonal.
0 1
Definition 3.7 (Orthogonality). Two vectors x and y are orthogonal if and orthogonal
only if hx, yi = 0, and we write x ? y . If additionally kxk = 1 = kyk,
i.e., the vectors are unit vectors, then x and y are orthonormal. orthonormal

An implication of this definition is that the 0-vector is orthogonal to


every vector in the vector space.
Remark. Orthogonality is the generalization of the concept of perpendic-
ularity to bilinear forms that do not have to be the dot product. In our
context, geometrically, we can think of orthogonal vectors as having a
right angle with respect to a specific inner product. }

Example 3.7 (Orthogonal Vectors)

Figure 3.6 The


1 angle ! between
y x two vectors x, y can
change depending
! on the inner
product.
1 0 1

Consider two vectors x = [1, 1]> , y = [ 1, 1]> 2 R2 ; see Figure 3.6.


We are interested in determining the angle ! between them using two
different inner products. Using the dot product as the inner product yields
an angle ! between x and y of 90 , such that x ? y . However, if we
choose the inner product

> 2 0
hx, yi = x y, (3.27)
0 1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


78 Analytic Geometry

we get that the angle ! between x and y is given by


hx, yi 1
cos ! = = =) ! ⇡ 1.91 rad ⇡ 109.5 , (3.28)
kxkkyk 3
and x and y are not orthogonal. Therefore, vectors that are orthogonal
with respect to one inner product do not have to be orthogonal with re-
spect to a different inner product.

Definition 3.8 (Orthogonal Matrix). A square matrix A 2 Rn⇥n is an


orthogonal matrix orthogonal matrix if and only if its columns are orthonormal so that
AA> = I = A> A , (3.29)
which implies that
A 1
= A> , (3.30)
It is convention to i.e., the inverse is obtained by simply transposing the matrix.
call these matrices
“orthogonal” but a Transformations by orthogonal matrices are special because the length
more precise of a vector x is not changed when transforming it using an orthogonal
description would matrix A. For the dot product, we obtain
be “orthonormal”.
2 2
Transformations kAxk = (Ax)> (Ax) = x> A> Ax = x> Ix = x> x = kxk . (3.31)
with orthogonal
matrices preserve Moreover, the angle between any two vectors x, y , as measured by their
distances and inner product, is also unchanged when transforming both of them using
angles.
an orthogonal matrix A. Assuming the dot product as the inner product,
the angle of the images Ax and Ay is given as
(Ax)> (Ay) x> A> Ay x> y
cos ! = =q = , (3.32)
kAxk kAyk x> A> Axy > A> Ay kxk kyk

which gives exactly the angle between x and y . This means that orthog-
onal matrices A with A> = A 1 preserve both angles and distances. It
turns out that orthogonal matrices define transformations that are rota-
tions (with the possibility of flips). In Section 3.9, we will discuss more
details about rotations.

3.5 Orthonormal Basis


In Section 2.6.1, we characterized properties of basis vectors and found
that in an n-dimensional vector space, we need n basis vectors, i.e., n
vectors that are linearly independent. In Sections 3.3 and 3.4, we used
inner products to compute the length of vectors and the angle between
vectors. In the following, we will discuss the special case where the basis
vectors are orthogonal to each other and where the length of each basis
vector is 1. We will call this basis then an orthonormal basis.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.6 Orthogonal Complement 79

Let us introduce this more formally.

Definition 3.9 (Orthonormal Basis). Consider an n-dimensional vector


space V and a basis {b1 , . . . , bn } of V . If

hbi , bj i = 0 for i 6= j (3.33)


hbi , bi i = 1 (3.34)

for all i, j = 1, . . . , n then the basis is called an orthonormal basis (ONB). orthonormal basis
If only (3.33) is satisfied, then the basis is called an orthogonal basis. Note ONB
orthogonal basis
that (3.34) implies that every basis vector has length/norm 1.

Recall from Section 2.6.1 that we can use Gaussian elimination to find a
basis for a vector space spanned by a set of vectors. Assume we are given
a set {b̃1 , . . . , b̃n } of non-orthogonal and unnormalized basis vectors. We
concatenate them into a matrix B̃ = [b̃1 , . . . , b̃n ] and apply Gaussian elim-
>
ination to the augmented matrix (Section 2.3.2) [B̃ B̃ |B̃] to obtain an
orthonormal basis. This constructive way to iteratively build an orthonor-
mal basis {b1 , . . . , bn } is called the Gram-Schmidt process (Strang, 2003).

Example 3.8 (Orthonormal Basis)


The canonical/standard basis for a Euclidean vector space Rn is an or-
thonormal basis, where the inner product is the dot product of vectors.
In R2 , the vectors
 
1 1 1 1
b1 = p , b2 = p (3.35)
2 1 2 1
form an orthonormal basis since b>
1 b2 = 0 and kb1 k = 1 = kb2 k.

We will exploit the concept of an orthonormal basis in Chapter 12 and


Chapter 10 when we discuss support vector machines and principal com-
ponent analysis.

3.6 Orthogonal Complement


Having defined orthogonality, we will now look at vector spaces that are
orthogonal to each other. This will play an important role in Chapter 10,
when we discuss linear dimensionality reduction from a geometric per-
spective.
Consider a D-dimensional vector space V and an M -dimensional sub-
space U ✓ V . Then its orthogonal complement U ? is a (D M )-dimensional orthogonal
subspace of V and contains all vectors in V that are orthogonal to every complement
vector in U . Furthermore, U \ U ? = {0} so that any vector x 2 V can be

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


80 Analytic Geometry

Figure 3.7 A plane


U in a e3
three-dimensional
vector space can be w
described by its
normal vector,
which spans its e2
orthogonal
complement U ? .

e1
U

uniquely decomposed into


M
X DXM
?
x= m bm + j bj , m, j 2 R, (3.36)
m=1 j=1

where (b1 , . . . , bM ) is a basis of U and (b? ?


1 , . . . , bD M ) is a basis of U .
?

Therefore, the orthogonal complement can also be used to describe a


plane U (two-dimensional subspace) in a three-dimensional vector space.
More specifically, the vector w with kwk = 1, which is orthogonal to the
plane U , is the basis vector of U ? . Figure 3.7 illustrates this setting. All
vectors that are orthogonal to w must (by construction) lie in the plane
normal vector U . The vector w is called the normal vector of U .
Generally, orthogonal complements can be used to describe hyperplanes
in n-dimensional vector and affine spaces.

3.7 Inner Product of Functions


Thus far, we looked at properties of inner products to compute lengths,
angles and distances. We focused on inner products of finite-dimensional
vectors. In the following, we will look at an example of inner products of
a different type of vectors: inner products of functions.
The inner products we discussed so far were defined for vectors with a
finite number of entries. We can think of a vector x 2 Rn as function with
n function values. The concept of an inner product can be generalized to
vectors with an infinite number of entries (countably infinite) and also
continuous-valued functions (uncountably infinite). Then the sum over
individual components of vectors (see Equation (3.5) for example) turns
into an integral.
An inner product of two functions u : R ! R and v : R ! R can be
defined as the definite integral
Z b
hu, vi := u(x)v(x)dx (3.37)
a

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.8 Orthogonal Projections 81

for lower and upper limits a, b < 1, respectively. As with our usual inner
product, we can define norms and orthogonality by looking at the inner
product. If (3.37) evaluates to 0, the functions u and v are orthogonal. To
make the preceding inner product mathematically precise, we need to take
care of measures and the definition of integrals, leading to the definition of
a Hilbert space. Furthermore, unlike inner products on finite-dimensional
vectors, inner products on functions may diverge (have infinite value). All
this requires diving into some more intricate details of real and functional
analysis, which we do not cover in this book.

Example 3.9 (Inner Product of Functions)


If we choose u = sin(x) and v = cos(x), the integrand f (x) = u(x)v(x) Figure 3.8 f (x) =
of (3.37), is shown in Figure 3.8. We see that this function is odd, i.e., sin(x) cos(x).
f ( x) = f (x). Therefore, the integral with limits a = ⇡, b = ⇡ of this 0.5

product evaluates to 0. Therefore, sin and cos are orthogonal functions.

sin(x) cos(x)
0.0

Remark. It also holds that the collection of functions 0.5


2.5 0.0 2.5

{1, cos(x), cos(2x), cos(3x), . . . } (3.38) x

is orthogonal if we integrate from ⇡ to ⇡ , i.e., any pair of functions are


orthogonal to each other. The collection of functions in (3.38) spans a
large subspace of the functions that are even and periodic on [ ⇡, ⇡), and
projecting functions onto this subspace is the fundamental idea behind
Fourier series. }
In Section 6.4.6, we will have a look at a second type of unconventional
inner products: the inner product of random variables.

3.8 Orthogonal Projections


Projections are an important class of linear transformations (besides rota-
tions and reflections) and play an important role in graphics, coding the-
ory, statistics and machine learning. In machine learning, we often deal
with data that is high-dimensional. High-dimensional data is often hard
to analyze or visualize. However, high-dimensional data quite often pos-
sesses the property that only a few dimensions contain most information,
and most other dimensions are not essential to describe key properties
of the data. When we compress or visualize high-dimensional data, we
will lose information. To minimize this compression loss, we ideally find
the most informative dimensions in the data. As discussed in Chapter 1, “Feature” is a
data can be represented as vectors, and in this chapter, we will discuss common expression
for data
some of the fundamental tools for data compression. More specifically, we
representation.
can project the original high-dimensional data onto a lower-dimensional
feature space and work in this lower-dimensional space to learn more
about the dataset and extract relevant patterns. For example, machine

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


82 Analytic Geometry

Figure 3.9
Orthogonal
projection (orange 2
dots) of a
1
two-dimensional
dataset (blue dots)

x2
0
onto a
one-dimensional 1
subspace (straight
line). 2

4 2 0 2 4
x1

learning algorithms, such as principal component analysis (PCA) by Pear-


son (1901) and Hotelling (1933) and deep neural networks (e.g., deep
auto-encoders (Deng et al., 2010)), heavily exploit the idea of dimension-
ality reduction. In the following, we will focus on orthogonal projections,
which we will use in Chapter 10 for linear dimensionality reduction and
in Chapter 12 for classification. Even linear regression, which we discuss
in Chapter 9, can be interpreted using orthogonal projections. For a given
lower-dimensional subspace, orthogonal projections of high-dimensional
data retain as much information as possible and minimize the difference/
error between the original data and the corresponding projection. An il-
lustration of such an orthogonal projection is given in Figure 3.9. Before
we detail how to obtain these projections, let us define what a projection
actually is.

Definition 3.10 (Projection). Let V be a vector space and U ✓ V a


projection subspace of V . A linear mapping ⇡ : V ! U is called a projection if
⇡2 = ⇡ ⇡ = ⇡.
Since linear mappings can be expressed by transformation matrices (see
Section 2.7), the preceding definition applies equally to a special kind
projection matrix of transformation matrices, the projection matrices P ⇡ , which exhibit the
property that P 2⇡ = P ⇡ .
In the following, we will derive orthogonal projections of vectors in the
inner product space (Rn , h·, ·i) onto subspaces. We will start with one-
line dimensional subspaces, which are also called lines. If not mentioned oth-
erwise, we assume the dot product hx, yi = x> y as the inner product.

3.8.1 Projection onto One-Dimensional Subspaces (Lines)


Assume we are given a line (one-dimensional subspace) through the ori-
gin with basis vector b 2 Rn . The line is a one-dimensional subspace
U ✓ Rn spanned by b. When we project x 2 Rn onto U , we seek the
vector ⇡U (x) 2 U that is closest to x. Using geometric arguments, let

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.8 Orthogonal Projections 83
Figure 3.10
x Examples of
projections onto
one-dimensional
subspaces.

b x

⇡U (x)

! sin !
! cos ! b
(a) Projection of x 2 R2 onto a subspace U (b) Projection of a two-dimensional vector
with basis vector b. x with kxk = 1 onto a one-dimensional
subspace spanned by b.

us characterize some properties of the projection ⇡U (x) (Figure 3.10(a)


serves as an illustration):

The projection ⇡U (x) is closest to x, where “closest” implies that the


distance kx ⇡U (x)k is minimal. It follows that the segment ⇡U (x) x
from ⇡U (x) to x is orthogonal to U , and therefore the basis vector b of
U . The orthogonality condition yields h⇡U (x) x, bi = 0 since angles
between vectors are defined via the inner product. is then the
The projection ⇡U (x) of x onto U must be an element of U and, there- coordinate of ⇡U (x)
with respect to b.
fore, a multiple of the basis vector b that spans U . Hence, ⇡U (x) = b,
for some 2 R.

In the following three steps, we determine the coordinate , the projection


⇡U (x) 2 U , and the projection matrix P ⇡ that maps any x 2 Rn onto U :

1. Finding the coordinate . The orthogonality condition yields


⇡U (x)= b
hx ⇡U (x), bi = 0 () hx b, bi = 0 . (3.39)

We can now exploit the bilinearity of the inner product and arrive at With a general inner
product, we get
hx, bi hb, xi = hx, bi if
hx, bi hb, bi = 0 () = = . (3.40) kbk = 1.
hb, bi kbk2
In the last step, we exploited the fact that inner products are symmet-
ric. If we choose h·, ·i to be the dot product, we obtain

b> x b> x
= = . (3.41)
b> b kbk2

If kbk = 1, then the coordinate of the projection is given by b> x.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


84 Analytic Geometry

2. Finding the projection point ⇡U (x) 2 U . Since ⇡U (x) = b, we imme-


diately obtain with (3.40) that

hx, bi b> x
⇡U (x) = b = b = b, (3.42)
kbk2 kbk2
where the last equality holds for the dot product only. We can also
compute the length of ⇡U (x) by means of Definition 3.1 as

k⇡U (x)k = k bk = | | kbk . (3.43)

Hence, our projection is of length | | times the length of b. This also


adds the intuition that is the coordinate of ⇡U (x) with respect to the
basis vector b that spans our one-dimensional subspace U .
If we use the dot product as an inner product, we get

(3.42) |b> x| (3.25) kbk


k⇡U (x)k = kbk = | cos !| kxk kbk = | cos !| kxk .
kbk 2 kbk2
(3.44)

Here, ! is the angle between x and b. This equation should be familiar


from trigonometry: If kxk = 1, then x lies on the unit circle. It follows
The horizontal axis that the projection onto the horizontal axis spanned by b is exactly
is a one-dimensional cos ! , and the length of the corresponding vector ⇡U (x) = |cos !|. An
subspace.
illustration is given in Figure 3.10(b).
3. Finding the projection matrix P ⇡ . We know that a projection is a lin-
ear mapping (see Definition 3.10). Therefore, there exists a projection
matrix P ⇡ , such that ⇡U (x) = P ⇡ x. With the dot product as inner
product and

b> x bb>
⇡U (x) = b = b = b = x, (3.45)
kbk2 kbk2
we immediately see that

bb>
P⇡ = . (3.46)
kbk2

Projection matrices Note that bb> (and, consequently, P ⇡ ) is a symmetric matrix (of rank
are always 1), and kbk2 = hb, bi is a scalar.
symmetric.
The projection matrix P ⇡ projects any vector x 2 Rn onto the line through
the origin with direction b (equivalently, the subspace U spanned by b).
Remark. The projection ⇡U (x) 2 Rn is still an n-dimensional vector and
not a scalar. However, we no longer require n coordinates to represent the
projection, but only a single one if we want to express it with respect to
the basis vector b that spans the subspace U : . }

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.8 Orthogonal Projections 85
x Figure 3.11
Projection onto a
two-dimensional
subspace U with
basis b1 , b2 . The
projection ⇡U (x) of
x ⇡U (x) x 2 R3 onto U can
be expressed as a
linear combination
U of b1 , b2 and the
b2
displacement vector
x ⇡U (x) is
⇡U (x) orthogonal to both
b1 and b2 .
0 b1

Example 3.10 (Projection onto a Line)


Find the projection matrix P ⇡ onto the line through the origin spanned
⇥ ⇤>
by b = 1 2 2 . b is a direction and a basis of the one-dimensional
subspace (line through origin).
With (3.46), we obtain
2 3 2 3
1 1 2 2
bb> 1 4 5⇥ ⇤ 14
P⇡ = > = 2 1 2 2 = 2 4 45 . (3.47)
b b 9 2 9 2 4 4

Let us now choose a particular x and see whether it lies in the subspace
⇥ ⇤>
spanned by b. For x = 1 1 1 , the projection is
2 32 3 2 3 2 3
1 2 2 1 5 1
14 1
⇡U (x) = P ⇡ x = 2 4 45 415 = 4105 2 span[425] . (3.48)
9 2 4 4 1 9 10 2
Note that the application of P ⇡ to ⇡U (x) does not change anything, i.e.,
P ⇡ ⇡U (x) = ⇡U (x). This is expected because according to Definition 3.10,
we know that a projection matrix P ⇡ satisfies P 2⇡ x = P ⇡ x for all x.

Remark. With the results from Chapter 4, we can show that ⇡U (x) is an
eigenvector of P ⇡ , and the corresponding eigenvalue is 1. }

3.8.2 Projection onto General Subspaces


If U is given by a set
In the following, we look at orthogonal projections of vectors x 2 Rn of spanning vectors,
onto lower-dimensional subspaces U ✓ Rn with dim(U ) = m > 1. An which are not a
basis, make sure
illustration is given in Figure 3.11.
you determine a
Assume that (b1 , . . . , bm ) is an ordered basis of U . Any projection ⇡U (x) basis b1 , . . . , bm
onto U is necessarily an element of U . Therefore, they can be represented before proceeding.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


86 Analytic Geometry

as linear Pcombinations of the basis vectors b1 , . . . , bm of U , such that


m
The basis vectors ⇡U (x) = i=1 i bi .
form the columns of As in the 1D case, we follow a three-step procedure to find the projec-
B 2 Rn⇥m , where
tion ⇡U (x) and the projection matrix P ⇡ :
B = [b1 , . . . , bm ].
1. Find the coordinates 1 , . . . , m of the projection (with respect to the
basis of U ), such that the linear combination
m
X
⇡U (x) = i bi =B , (3.49)
i=1

B = [b1 , . . . , bm ] 2 Rn⇥m , = [ 1, . . . , m]
>
2 Rm , (3.50)
is closest to x 2 Rn . As in the 1D case, “closest” means “minimum
distance”, which implies that the vector connecting ⇡U (x) 2 U and
x 2 Rn must be orthogonal to all basis vectors of U . Therefore, we
obtain m simultaneous conditions (assuming the dot product as the
inner product)
hb1 , x ⇡U (x)i = b>1 (x ⇡U (x)) = 0 (3.51)
..
.
hbm , x ⇡U (x)i = b>
m (x ⇡U (x)) = 0 (3.52)
which, with ⇡U (x) = B , can be written as
b>
1 (x B )=0 (3.53)
..
.
b>
m (x B )=0 (3.54)
such that we obtain a homogeneous linear equation system
2 >3 2 3
b1
6 .. 7 4 >
4 . 5 x B 5 = 0 () B (x B ) = 0 (3.55)
b>
m

() B > B = B > x . (3.56)


normal equation The last expression is called normal equation. Since b1 , . . . , bm are a
basis of U and, therefore, linearly independent, B > B 2 Rm⇥m is reg-
ular and can be inverted. This allows us to solve for the coefficients/
coordinates
= (B > B) 1 B > x . (3.57)

pseudo-inverse The matrix (B > B) 1 B > is also called the pseudo-inverse of B , which
can be computed for non-square matrices B . It only requires that B > B
is positive definite, which is the case if B is full rank. In practical ap-
plications (e.g., linear regression), we often add a “jitter term” ✏I to

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.8 Orthogonal Projections 87

B > B to guarantee increased numerical stability and positive definite-


ness. This “ridge” can be rigorously derived using Bayesian inference.
See Chapter 9 for details.
2. Find the projection ⇡U (x) 2 U . We already established that ⇡U (x) =
B . Therefore, with (3.57)
⇡U (x) = B(B > B) 1 B > x . (3.58)

3. Find the projection matrix P ⇡ . From (3.58), we can immediately see


that the projection matrix that solves P ⇡ x = ⇡U (x) must be

P ⇡ = B(B > B) 1 B > . (3.59)

Remark. The solution for projecting onto general subspaces includes the
1D case as a special case: If dim(U ) = 1, then B > B 2 R is a scalar and
we can rewrite the projection matrix in (3.59) P ⇡ = B(B > B) 1 B > as
>
P ⇡ = BB
B> B
, which is exactly the projection matrix in (3.46). }

Example 3.11 (Projection2onto 3 2a Two-dimensional


3 Subspace)
2 3
1 0 6
For a subspace U = span[415 , 415] ✓ R3 and x = 405 2 R3 find the
1 2 0
coordinates of x in terms of the subspace U , the projection point ⇡U (x)
and the projection matrix P ⇡ .
First, we see that the generating set of U is a basis (linear
2 indepen-
3
1 0
dence) and write the basis vectors of U into a matrix B = 41 15.
1 2
Second, we compute the matrix B > B and the vector B > x as
2 3 2 3
 1 0   6 
1 1 1 4 3 3 1 1 1 4 5 6
B>B = 1 15 = , B>x = 0 = .
0 1 2 3 5 0 1 2 0
1 2 0
(3.60)

Third, we solve the normal equation B > B = B > x to find :


   
3 3 1 6 5
= () = . (3.61)
3 5 2 0 3
Fourth, the projection ⇡U (x) of x onto U , i.e., into the column space of
B , can be directly computed via
2 3
5
⇡U (x) = B = 4 2 5 . (3.62)
1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


88 Analytic Geometry

projection error The corresponding projection error is the norm of the difference vector
The projection error between the original vector and its projection onto U , i.e.,
is also called the ⇥ ⇤> p
reconstruction error. kx ⇡U (x)k = 1 2 1 = 6. (3.63)

Fifth, the projection matrix (for any x 2 R3 ) is given by


2 3
5 2 1
1
P ⇡ = B(B > B) 1 B > = 4 2 2 2 5 . (3.64)
6 1 2 5
To verify the results, we can (a) check whether the displacement vector
⇡U (x) x is orthogonal to all basis vectors of U , and (b) verify that
P ⇡ = P 2⇡ (see Definition 3.10).

Remark. The projections ⇡U (x) are still vectors in Rn although they lie in
an m-dimensional subspace U ✓ Rn . However, to represent a projected
vector we only need the m coordinates 1 , . . . , m with respect to the
basis vectors b1 , . . . , bm of U . }
Remark. In vector spaces with general inner products, we have to pay
attention when computing angles and distances, which are defined by
means of the inner product. }
We can find
approximate Projections allow us to look at situations where we have a linear system
solutions to Ax = b without a solution. Recall that this means that b does not lie in
unsolvable linear
equation systems
the span of A, i.e., the vector b does not lie in the subspace spanned by
using projections. the columns of A. Given that the linear equation cannot be solved exactly,
we can find an approximate solution. The idea is to find the vector in the
subspace spanned by the columns of A that is closest to b, i.e., we compute
the orthogonal projection of b onto the subspace spanned by the columns
of A. This problem arises often in practice, and the solution is called the
least-squares least-squares solution (assuming the dot product as the inner product) of
solution an overdetermined system. This is discussed further in Section 9.4. Using
reconstruction errors (3.63) is one possible approach to derive principal
component analysis (Section 10.3).
Remark. We just looked at projections of vectors x onto a subspace U with
basis vectors {b1 , . . . , bk }. If this basis is an ONB, i.e., (3.33) and (3.34)
are satisfied, the projection equation (3.58) simplifies greatly to
⇡U (x) = BB > x (3.65)

since B > B = I with coordinates


= B>x . (3.66)
This means that we no longer have to compute the inverse from (3.58),
which saves computation time. }

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.8 Orthogonal Projections 89

3.8.3 Gram-Schmidt Orthogonalization


Projections are at the core of the Gram-Schmidt method that allows us to
constructively transform any basis (b1 , . . . , bn ) of an n-dimensional vector
space V into an orthogonal/orthonormal basis (u1 , . . . , un ) of V . This
basis always exists (Liesen and Mehrmann, 2015) and span[b1 , . . . , bn ] =
span[u1 , . . . , un ]. The Gram-Schmidt orthogonalization method iteratively Gram-Schmidt
constructs an orthogonal basis (u1 , . . . , un ) from any basis (b1 , . . . , bn ) of orthogonalization
V as follows:
u1 := b1 (3.67)
uk := bk ⇡span[u1 ,...,uk 1]
(bk ) , k = 2, . . . , n . (3.68)
In (3.68), the k th basis vector bk is projected onto the subspace spanned
by the first k 1 constructed orthogonal vectors u1 , . . . , uk 1 ; see Sec-
tion 3.8.2. This projection is then subtracted from bk and yields a vector
uk that is orthogonal to the (k 1)-dimensional subspace spanned by
u1 , . . . , uk 1 . Repeating this procedure for all n basis vectors b1 , . . . , bn
yields an orthogonal basis (u1 , . . . , un ) of V . If we normalize the uk , we
obtain an ONB where kuk k = 1 for k = 1, . . . , n.

Example 3.12 (Gram-Schmidt Orthogonalization)

Figure 3.12
b2 b2 u2 b2 Gram-Schmidt
orthogonalization.
(a) non-orthogonal
basis (b1 , b2 ) of R2 ;
0 b1 0 ⇡span[u1 ] (b2 ) u1 0 ⇡span[u1 ] (b2 ) u1 (b) first constructed
basis vector u1 and
(a) Original non-orthogonal (b) First new basis vector (c) Orthogonal basis vectors u1
orthogonal
basis vectors b1 , b2 . u1 = b1 and projection of b2 and u2 = b2 ⇡span[u1 ] (b2 ).
projection of b2
onto the subspace spanned by
onto span[u1 ];
u1 .
(c) orthogonal basis
Consider a basis (b1 , b2 ) of R2 , where (u1 , u2 ) of R2 .
 
2 1
b1 = , b2 = ; (3.69)
0 1
see also Figure 3.12(a). Using the Gram-Schmidt method, we construct an
orthogonal basis (u1 , u2 ) of R2 as follows (assuming the dot product as
the inner product):

2
u1 := b1 = , (3.70)
0
   
(3.45) u1 u>1 1 1 0 1 0
u2 := b2 ⇡span[u1 ] (b2 ) = b2 b
2 2 = = .
ku1 k 1 0 0 1 1
(3.71)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


90 Analytic Geometry
Figure 3.13 x x
Projection onto an
affine space.
(a) original setting;
(b) setting shifted x x0
L L
by x0 so that ⇡L(x)
x x0 can be x0 x0
projected onto the
b2 b2 U = L x0 b2
direction space U ;
⇡U (x x0)
(c) projection is
translated back to 0 b1 0 b1 0 b1
x0 + ⇡U (x x0 ),
(a) Setting. (b) Reduce problem to pro- (c) Add support point back in
which gives the final
jection ⇡U onto vector sub- to get affine projection ⇡L .
orthogonal
space.
projection ⇡L (x).

These steps are illustrated in Figures 3.12(b) and (c). We immediately see
that u1 and u2 are orthogonal, i.e., u>1 u2 = 0.

3.8.4 Projection onto Affine Subspaces


Thus far, we discussed how to project a vector onto a lower-dimensional
subspace U . In the following, we provide a solution to projecting a vector
onto an affine subspace.
Consider the setting in Figure 3.13(a). We are given an affine space L =
x0 + U , where b1 , b2 are basis vectors of U . To determine the orthogonal
projection ⇡L (x) of x onto L, we transform the problem into a problem
that we know how to solve: the projection onto a vector subspace. In
order to get there, we subtract the support point x0 from x and from L,
so that L x0 = U is exactly the vector subspace U . We can now use the
orthogonal projections onto a subspace we discussed in Section 3.8.2 and
obtain the projection ⇡U (x x0 ), which is illustrated in Figure 3.13(b).
This projection can now be translated back into L by adding x0 , such that
we obtain the orthogonal projection onto an affine space L as
⇡L (x) = x0 + ⇡U (x x0 ) , (3.72)
where ⇡U (·) is the orthogonal projection onto the subspace U , i.e., the
direction space of L; see Figure 3.13(c).
From Figure 3.13, it is also evident that the distance of x from the affine
space L is identical to the distance of x x0 from U , i.e.,
d(x, L) = kx ⇡L (x)k = kx (x0 + ⇡U (x x0 ))k (3.73a)
= d(x x0 , ⇡U (x x0 )) . (3.73b)
We will use projections onto an affine subspace to derive the concept of
a separating hyperplane in Section 12.1.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.9 Rotations 91

Figure 3.14 A
rotation rotates
objects in a plane
about the origin. If
Original
the rotation angle is
Rotated by 112.5
positive, we rotate
counterclockwise.

Figure 3.15 The


robotic arm needs to
rotate its joints in
order to pick up
objects or to place
them correctly.
Figure taken
from (Deisenroth
et al., 2015).

3.9 Rotations
Length and angle preservation, as discussed in Section 3.4, are the two
characteristics of linear mappings with orthogonal transformation matri-
ces. In the following, we will have a closer look at specific orthogonal
transformation matrices, which describe rotations.
A rotation is a linear mapping (more specifically, an automorphism of rotation
a Euclidean vector space) that rotates a plane by an angle ✓ about the
origin, i.e., the origin is a fixed point. For a positive angle ✓ > 0, by com-
mon convention, we rotate in a counterclockwise direction. An example is
shown in Figure 3.14, where the transformation matrix is

0.38 0.92
R= . (3.74)
0.92 0.38
Important application areas of rotations include computer graphics and
robotics. For example, in robotics, it is often important to know how to
rotate the joints of a robotic arm in order to pick up or place an object,
see Figure 3.15.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


92 Analytic Geometry

Figure 3.16 (e2 ) = [ sin ✓, cos ✓]>


Rotation of the cos ✓
standard basis in R2
by an angle ✓. e2

(e1 ) = [cos ✓, sin ✓]>


sin ✓


sin ✓ e1 cos ✓

3.9.1 Rotations in R2
⇢  
1 0
Consider the standard basis e1 = , e2 = of R2 , which defines
0 1
the standard coordinate system in R2 . We aim to rotate this coordinate
system by an angle ✓ as illustrated in Figure 3.16. Note that the rotated
vectors are still linearly independent and, therefore, are a basis of R2 . This
means that the rotation performs a basis change.
Rotations are linear mappings so that we can express them by a
rotation matrix rotation matrix R(✓). Trigonometry (see Figure 3.16) allows us to de-
termine the coordinates of the rotated axes (the image of ) with respect
to the standard basis in R2 . We obtain
 
cos ✓ sin ✓
(e1 ) = , (e2 ) = . (3.75)
sin ✓ cos ✓
Therefore, the rotation matrix that performs the basis change into the
rotated coordinates R(✓) is given as

⇥ ⇤ cos ✓ sin ✓
R(✓) = (e1 ) (e2 ) = . (3.76)
sin ✓ cos ✓

3.9.2 Rotations in R3
In contrast to the R2 case, in R3 we can rotate any two-dimensional plane
about a one-dimensional axis. The easiest way to specify the general rota-
tion matrix is to specify how the images of the standard basis e1 , e2 , e3 are
supposed to be rotated, and making sure these images Re1 , Re2 , Re3 are
orthonormal to each other. We can then obtain a general rotation matrix
R by combining the images of the standard basis.
To have a meaningful rotation angle, we have to define what “coun-
terclockwise” means when we operate in more than two dimensions. We
use the convention that a “counterclockwise” (planar) rotation about an
axis refers to a rotation about an axis when we look at the axis “head on,
from the end toward the origin”. In R3 , there are therefore three (planar)
rotations about the three standard basis vectors (see Figure 3.17):

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.9 Rotations 93

e3 Figure 3.17
Rotation of a vector
(gray) in R3 by an
angle ✓ about the
e3 -axis. The rotated
vector is shown in
blue.
e2

✓ e1

Rotation about the e1 -axis


2 3
⇥ ⇤ 1 0 0
R1 (✓) = (e1 ) (e2 ) (e3 ) = 40 cos ✓ sin ✓5 . (3.77)
0 sin ✓ cos ✓

Here, the e1 coordinate is fixed, and the counterclockwise rotation is


performed in the e2 e3 plane.
Rotation about the e2 -axis
2 3
cos ✓ 0 sin ✓
R2 (✓) = 4 0 1 0 5. (3.78)
sin ✓ 0 cos ✓

If we rotate the e1 e3 plane about the e2 axis, we need to look at the e2


axis from its “tip” toward the origin.
Rotation about the e3 -axis
2 3
cos ✓ sin ✓ 0
R3 (✓) = 4 sin ✓ cos ✓ 05 . (3.79)
0 0 1

Figure 3.17 illustrates this.

3.9.3 Rotations in n Dimensions


The generalization of rotations from 2D and 3D to n-dimensional Eu-
clidean vector spaces can be intuitively described as fixing n 2 dimen-
sions and restrict the rotation to a two-dimensional plane in the n-dimen-
sional space. As in the three-dimensional case, we can rotate any plane
(two-dimensional subspace of Rn ).

Definition 3.11 (Givens Rotation). Let V be an n-dimensional Euclidean


vector space and : V ! V an automorphism with transformation ma-

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


94 Analytic Geometry

trix
2 3
Ii 1 0 ··· ··· 0
6 0 cos ✓ 0 sin ✓ 0 7
6 7
Rij (✓) := 6
6 0 0 I j i 1 0 0 7 2 Rn⇥n ,
7 (3.80)
4 0 sin ✓ 0 cos ✓ 0 5
0 ··· ··· 0 In j

Givens rotation for 1 6 i < j 6 n and ✓ 2 R. Then Rij (✓) is called a Givens rotation.
Essentially, Rij (✓) is the identity matrix I n with
rii = cos ✓ , rij = sin ✓ , rji = sin ✓ , rjj = cos ✓ . (3.81)
In two dimensions (i.e., n = 2), we obtain (3.76) as a special case.

3.9.4 Properties of Rotations


Rotations exhibit a number of useful properties, which can be derived by
considering them as orthogonal matrices (Definition 3.8):
Rotations preserve distances, i.e., kx yk = kR✓ (x) R✓ (y)k. In other
words, rotations leave the distance between any two points unchanged
after the transformation.
Rotations preserve angles, i.e., the angle between R✓ x and R✓ y equals
the angle between x and y .
Rotations in three (or more) dimensions are generally not commuta-
tive. Therefore, the order in which rotations are applied is important,
even if they rotate about the same point. Only in two dimensions vector
rotations are commutative, such that R( )R(✓) = R(✓)R( ) for all
, ✓ 2 [0, 2⇡). They form an Abelian group (with multiplication) only if
they rotate about the same point (e.g., the origin).

3.10 Further Reading


In this chapter, we gave a brief overview of some of the important concepts
of analytic geometry, which we will use in later chapters of the book. For a
broader and more in-depth overview of some the concepts we presented,
we refer to the following excellent books: Axler (2015) and Boyd and
Vandenberghe (2018).
Inner products allow us to determine specific bases of vector (sub)spaces,
where each vector is orthogonal to all others (orthogonal bases) using the
Gram-Schmidt method. These bases are important in optimization and
numerical algorithms for solving linear equation systems. For instance,
Krylov subspace methods, such as conjugate gradients or the generalized
minimal residual method (GMRES), minimize residual errors that are or-
thogonal to each other (Stoer and Burlirsch, 2002).
In machine learning, inner products are important in the context of

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


3.10 Further Reading 95

kernel methods (Schölkopf and Smola, 2002). Kernel methods exploit the
fact that many linear algorithms can be expressed purely by inner prod-
uct computations. Then, the “kernel trick” allows us to compute these
inner products implicitly in a (potentially infinite-dimensional) feature
space, without even knowing this feature space explicitly. This allowed the
“non-linearization” of many algorithms used in machine learning, such as
kernel-PCA (Schölkopf et al., 1997) for dimensionality reduction. Gaus-
sian processes (Rasmussen and Williams, 2006) also fall into the category
of kernel methods and are the current state of the art in probabilistic re-
gression (fitting curves to data points). The idea of kernels is explored
further in Chapter 12.
Projections are often used in computer graphics, e.g., to generate shad-
ows. In optimization, orthogonal projections are often used to (iteratively)
minimize residual errors. This also has applications in machine learning,
e.g., in linear regression where we want to find a (linear) function that
minimizes the residual errors, i.e., the lengths of the orthogonal projec-
tions of the data onto the linear function (Bishop, 2006). We will investi-
gate this further in Chapter 9. PCA (Pearson, 1901; Hotelling, 1933) also
uses projections to reduce the dimensionality of high-dimensional data.
We will discuss this in more detail in Chapter 10.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


96 Analytic Geometry

Exercises
3.1 Show that h·, ·i defined for all x = [x1 , x2 ]> 2 R2 and y = [y1 , y2 ]> 2 R2 by

hx, yi := x1 y1 (x1 y2 + x2 y1 ) + 2(x2 y2 )

is an inner product.
3.2 Consider R2 with h·, ·i defined for all x and y in R2 as

2 0
hx, yi := x> y.
1 2
| {z }
=:A

Is h·, ·i an inner product?


3.3 Compute the distance between
2 3 2 3
1 1
x = 425 , y = 4 15
3 0

using
a. hx, yi := x> y
2 3
2 1 0
b. hx, yi := x> Ay , A := 41 3 15
0 1 2
3.4 Compute the angle between
 
1 1
x= , y=
2 1

using
a. hx, yi := x> y

> 2 1
b. hx, yi := x By , B :=
1 3
3.5 Consider the Euclidean vector space R5 with the dot product. A subspace
U ✓ R5 and x 2 R5 are given by
2 3 2 3 2 3 2 3 2 3
0 1 3 1 1
6 17 6 37 6 4 7 6 37 6 97
6 7 6 7 6 7 6 7 6 7
U = span[6 7 6 7 6
6 2 7, 6 1 7, 6 1
7,
7
6 5 7] ,
6 7 x=6 7
6 17
4 0 5 4 15 4 2 5 405 445
2 2 1 7 1

a. Determine the orthogonal projection ⇡U (x) of x onto U


b. Determine the distance d(x, U )
3.6 Consider R3 with the inner product
2 3
2 1 0
hx, yi := x> 41 2 15 y .
0 1 2

Furthermore, we define e1 , e2 , e3 as the standard/canonical basis in R3 .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Exercises 97

a. Determine the orthogonal projection ⇡U (e2 ) of e2 onto


U = span[e1 , e3 ] .

Hint: Orthogonality is defined through the inner product.


b. Compute the distance d(e2 , U ).
c. Draw the scenario: standard basis vectors and ⇡U (e2 )
3.7 Let V be a vector space and ⇡ an endomorphism of V .
a. Prove that ⇡ is a projection if and only if idV ⇡ is a projection, where
idV is the identity endomorphism on V .
b. Assume now that ⇡ is a projection. Calculate Im(idV ⇡) and ker(idV ⇡)
as a function of Im(⇡) and ker(⇡).

3.8 Using the Gram-Schmidt method, turn the basis B = (b1 , b2 ) of a two-
dimensional subspace U ✓ R3 into an ONB C = (c1 , c2 ) of U , where
2 3 2 3
1 1
b1 := 415 , b2 := 4 2 5 .
1 0

3.9 Let n 2 N⇤ and let x1 , . . . , xn > 0 be n positive real numbers so that x1 +


· · · + xn = 1. Use the Cauchy-Schwarz inequality and show that
Pn
a. x2i > n1
Pi=1
n
b. i=1 xi > n
1 2

Hint: Think about the dot product on Rn . Then, choose specific vectors
x, y 2 Rn and apply the Cauchy-Schwarz inequality.
3.10 Rotate the vectors
 
2 0
x1 := , x2 :=
3 1

by 30 .

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


4

Matrix Decompositions

In Chapters 2 and 3, we studied ways to manipulate and measure vectors,


projections of vectors, and linear mappings. Mappings and transforma-
tions of vectors can be conveniently described as operations performed by
matrices. Moreover, data is often represented in matrix form as well, e.g.,
where the rows of the matrix represent different people and the columns
describe different features of the people, such as weight, height, and socio-
economic status. In this chapter, we present three aspects of matrices: how
to summarize matrices, how matrices can be decomposed, and how these
decompositions can be used for matrix approximations.
We first consider methods that allow us to describe matrices with just
a few numbers that characterize the overall properties of matrices. We
will do this in the sections on determinants (Section 4.1) and eigenval-
ues (Section 4.2) for the important special case of square matrices. These
characteristic numbers have important mathematical consequences and
allow us to quickly grasp what useful properties a matrix has. From here
we will proceed to matrix decomposition methods: An analogy for ma-
trix decomposition is the factoring of numbers, such as the factoring of
21 into prime numbers 7 · 3. For this reason matrix decomposition is also
matrix factorization often referred to as matrix factorization. Matrix decompositions are used
to describe a matrix by means of a different representation using factors
of interpretable matrices.
We will first cover a square-root-like operation for symmetric, positive
definite matrices, the Cholesky decomposition (Section 4.3). From here
we will look at two related methods for factorizing matrices into canoni-
cal forms. The first one is known as matrix diagonalization (Section 4.4),
which allows us to represent the linear mapping using a diagonal trans-
formation matrix if we choose an appropriate basis. The second method,
singular value decomposition (Section 4.5), extends this factorization to
non-square matrices, and it is considered one of the fundamental concepts
in linear algebra. These decompositions are helpful, as matrices represent-
ing numerical data are often very large and hard to analyze. We conclude
the chapter with a systematic overview of the types of matrices and the
characteristic properties that distinguish them in the form of a matrix tax-
onomy (Section 4.7).
The methods that we cover in this chapter will become important in

98
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
4.1 Determinant and Trace 99

tests used in Figure 4.1 A mind


Determinant Invertibility Cholesky map of the concepts
introduced in this
chapter, along with
used in

used in
where they are used
in other parts of the
book.

Eigenvalues Chapter 6
Probability
& distributions
determines

used
in

constructs used in
Eigenvectors Orthogonal matrix Diagonalization

n
di
use
us

in
ed

SVD
ed
us
in

used in

Chapter 10
Dimensionality
reduction

both subsequent mathematical chapters, such as Chapter 6, but also in


applied chapters, such as dimensionality reduction in Chapters 10 or den-
sity estimation in Chapter 11. This chapter’s overall structure is depicted
in the mind map of Figure 4.1.

4.1 Determinant and Trace The determinant


Determinants are important concepts in linear algebra. A determinant is notation |A| must
not be confused
a mathematical object in the analysis and solution of systems of linear
with the absolute
equations. Determinants are only defined for square matrices A 2 Rn⇥n , value.
i.e., matrices with the same number of rows and columns. In this book,
we write the determinant as det(A) or sometimes as |A| so that
a11 a12 . . . a1n
a21 a22 . . . a2n
det(A) = .. .. .. . (4.1)
. . .
an1 an2 . . . ann
The determinant of a square matrix A 2 Rn⇥n is a function that maps A determinant

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


100 Matrix Decompositions

onto a real number. Before providing a definition of the determinant for


general n ⇥ n matrices, let us have a look at some motivating examples,
and define determinants for some special matrices.

Example 4.1 (Testing for Matrix Invertibility)


Let us begin with exploring if a square matrix A is invertible (see Sec-
tion 2.2.2). For the smallest cases, we already know when a matrix
is invertible. If A is a 1 ⇥ 1 matrix, i.e., it is a scalar number, then
A = a =) A 1 = a1 . Thus a a1 = 1 holds, if and only if a 6= 0.
For 2 ⇥ 2 matrices, by the definition of the inverse (Definition 2.3), we
know that AA 1 = I . Then, with (2.24), the inverse of A is

1 a22 a12
A 1= . (4.2)
a11 a22 a12 a21 a21 a11
Hence, A is invertible if and only if
a11 a22 a12 a21 6= 0 . (4.3)
This quantity is the determinant of A 2 R2⇥2 , i.e.,
a11 a12
det(A) = = a11 a22 a12 a21 . (4.4)
a21 a22

Example 4.1 points already at the relationship between determinants


and the existence of inverse matrices. The next theorem states the same
result for n ⇥ n matrices.
Theorem 4.1. For any square matrix A 2 Rn⇥n it holds that A is invertible
if and only if det(A) 6= 0.
We have explicit (closed-form) expressions for determinants of small
matrices in terms of the elements of the matrix. For n = 1,
det(A) = det(a11 ) = a11 . (4.5)
For n = 2,
a11 a12
det(A) = = a11 a22 a12 a21 , (4.6)
a21 a22
which we have observed in the preceding example.
For n = 3 (known as Sarrus’ rule),
a11 a12 a13
a21 a22 a23 = a11 a22 a33 + a21 a32 a13 + a31 a12 a23 (4.7)
a31 a32 a33
a31 a22 a13 a11 a32 a23 a21 a12 a33 .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.1 Determinant and Trace 101

For a memory aid of the product terms in Sarrus’ rule, try tracing the
elements of the triple products in the matrix.
We call a square matrix T an upper-triangular matrix if Tij = 0 for upper-triangular
i > j , i.e., the matrix is zero below its diagonal. Analogously, we define a matrix
lower-triangular matrix as a matrix with zeros above its diagonal. For a tri- lower-triangular
angular matrix T 2 Rn⇥n , the determinant is the product of the diagonal matrix
elements, i.e.,
n
Y
det(T ) = Tii . (4.8)
i=1
The determinant is
the signed volume
of the parallelepiped
Example 4.2 (Determinants as Measures of Volume) formed by the
columns of the
The notion of a determinant is natural when we consider it as a mapping
matrix.
from a set of n vectors spanning an object in Rn . It turns out that the de- Figure 4.2 The area
terminant det(A) is the signed volume of an n-dimensional parallelepiped of the parallelogram
formed by columns of the matrix A. (shaded region)
For n = 2, the columns of the matrix form a parallelogram; see Fig- spanned by the
vectors b and g is
ure 4.2. As the angle between vectors gets smaller, the area of a parallel- |det([b, g])|.
ogram shrinks, too. Consider two vectors b, g that form the columns of a
matrix A = [b, g]. Then, the absolute value of the determinant of A is the
area of the parallelogram with vertices 0, b, g, b + g . In particular, if b, g b
are linearly dependent so that b = g for some 2 R, they no longer
g
form a two-dimensional parallelogram. Therefore, the corresponding area
is 0. On the contrary, if b, g are linearly independent and are multiples of Figure 4.3 The
 volume of the
b
the canonical basis vectors e1 , e2 then they can be written as b = and parallelepiped
0 (shaded volume)

0 b 0 spanned by vectors
g= , and the determinant is = bg 0 = bg . r, b, g is
g 0 g
|det([r, b, g])|.
The sign of the determinant indicates the orientation of the spanning
vectors b, g with respect to the standard basis (e1 , e2 ). In our figure, flip-
ping the order to g, b swaps the columns of A and reverses the orientation
of the shaded area. This becomes the familiar formula: area = height ⇥
length. This intuition extends to higher dimensions. In R3 , we consider b
r
three vectors r, b, g 2 R3 spanning the edges of a parallelepiped, i.e., a g
solid with faces that are parallel parallelograms (see Figure 4.3). The ab- The sign of the
determinant
solute value of the determinant of the 3 ⇥ 3 matrix [r, b, g] is the volume
indicates the
of the solid. Thus, the determinant acts as a function that measures the orientation of the
signed volume formed by column vectors composed in a matrix. spanning vectors.
Consider the three linearly independent vectors r, g, b 2 R3 given as
2 3 2 3 2 3
2 6 1
r = 4 0 5 , g = 4 15 , b = 4 4 5 . (4.9)
8 0 1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


102 Matrix Decompositions

Writing these vectors as the columns of a matrix


2 3
2 6 1
A = [r, g, b] = 4 0 1 4 5 (4.10)
8 0 1
allows us to compute the desired volume as
V = |det(A)| = 186 . (4.11)

Computing the determinant of an n ⇥ n matrix requires a general algo-


rithm to solve the cases for n > 3, which we are going to explore in the fol-
lowing. Theorem 4.2 below reduces the problem of computing the deter-
minant of an n⇥n matrix to computing the determinant of (n 1)⇥(n 1)
matrices. By recursively applying the Laplace expansion (Theorem 4.2),
we can therefore compute determinants of n ⇥ n matrices by ultimately
computing determinants of 2 ⇥ 2 matrices.
Laplace expansion
Theorem 4.2 (Laplace Expansion). Consider a matrix A 2 Rn⇥n . Then,
for all j = 1, . . . , n:
det(Ak,j ) is called 1. Expansion along column j
a minor and n
( 1)k+j det(Ak,j )
X
a cofactor.
det(A) = ( 1)k+j akj det(Ak,j ) . (4.12)
k=1

2. Expansion along row j


n
X
det(A) = ( 1)k+j ajk det(Aj,k ) . (4.13)
k=1

Here Ak,j 2 R(n 1)⇥(n 1) is the submatrix of A that we obtain when delet-
ing row k and column j .

Example 4.3 (Laplace Expansion)


Let us compute the determinant of
2 3
1 2 3
A = 4 3 1 25 (4.14)
0 0 1
using the Laplace expansion along the first row. Applying (4.13) yields
1 2 3
1 2
3 1 2 = ( 1)1+1 · 1
0 1
0 0 1 (4.15)
3 2 3 1
+ ( 1)1+2 · 2 + ( 1)1+3 · 3 .
0 1 0 0

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.1 Determinant and Trace 103

We use (4.6) to compute the determinants of all 2 ⇥ 2 matrices and obtain


det(A) = 1(1 0) 2(3 0) + 3(0 0) = 5. (4.16)
For completeness we can compare this result to computing the determi-
nant using Sarrus’ rule (4.7):
det(A) = 1·1·1+3·0·3+0·2·2 0·1·3 1·0·2 3·2·1 = 1 6 = 5 . (4.17)

For A 2 Rn⇥n the determinant exhibits the following properties:

The determinant of a matrix product is the product of the corresponding


determinants, det(AB) = det(A)det(B).
Determinants are invariant to transposition, i.e., det(A) = det(A> ).
If A is regular (invertible), then det(A 1 ) = det(A)
1
.
Similar matrices (Definition 2.22) possess the same determinant. There-
fore, for a linear mapping : V ! V all transformation matrices A
of have the same determinant. Thus, the determinant is invariant to
the choice of basis of a linear mapping.
Adding a multiple of a column/row to another one does not change
det(A).
Multiplication of a column/row with 2 R scales det(A) by . In
particular, det( A) = n det(A).
Swapping two rows/columns changes the sign of det(A).
Because of the last three properties, we can use Gaussian elimination (see
Section 2.1) to compute det(A) by bringing A into row-echelon form.
We can stop Gaussian elimination when we have A in a triangular form
where the elements below the diagonal are all 0. Recall from (4.8) that the
determinant of a triangular matrix is the product of the diagonal elements.
Theorem 4.3. A square matrix A 2 Rn⇥n has det(A) 6= 0 if and only if
rk(A) = n. In other words, A is invertible if and only if it is full rank.
When mathematics was mainly performed by hand, the determinant
calculation was considered an essential way to analyze matrix invertibil-
ity. However, contemporary approaches in machine learning use direct
numerical methods that superseded the explicit calculation of the deter-
minant. For example, in Chapter 2, we learned that inverse matrices can
be computed by Gaussian elimination. Gaussian elimination can thus be
used to compute the determinant of a matrix.
Determinants will play an important theoretical role for the following
sections, especially when we learn about eigenvalues and eigenvectors
(Section 4.2) through the characteristic polynomial.
Definition 4.4. The trace of a square matrix A 2 Rn⇥n is defined as trace

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


104 Matrix Decompositions
n
X
tr(A) := aii , (4.18)
i=1

i.e. , the trace is the sum of the diagonal elements of A.


The trace satisfies the following properties:
tr(A + B) = tr(A) + tr(B) for A, B 2 Rn⇥n
tr(↵A) = ↵tr(A) , ↵ 2 R for A 2 Rn⇥n
tr(I n ) = n
tr(AB) = tr(BA) for A 2 Rn⇥k , B 2 Rk⇥n
It can be shown that only one function satisfies these four properties to-
gether – the trace (Gohberg et al., 2012).
The properties of the trace of matrix products are more general. Specif-
The trace is ically, the trace is invariant under cyclic permutations, i.e.,
invariant under
cyclic permutations. tr(AKL) = tr(KLA) (4.19)
for matrices A 2 Ra⇥k , K 2 Rk⇥l , L 2 Rl⇥a . This property generalizes to
products of an arbitrary number of matrices. As a special case of (4.19), it
follows that for two vectors x, y 2 Rn
tr(xy > ) = tr(y > x) = y > x 2 R . (4.20)
Given a linear mapping : V ! V , where V is a vector space, we
define the trace of this map by using the trace of matrix representation
of . For a given basis of V , we can describe by means of the transfor-
mation matrix A. Then the trace of is the trace of A. For a different
basis of V , it holds that the corresponding transformation matrix B of
can be obtained by a basis change of the form S 1 AS for suitable S (see
Section 2.7.2). For the corresponding trace of , this means
1 (4.19) 1
tr(B) = tr(S AS) = tr(ASS ) = tr(A) . (4.21)
Hence, while matrix representations of linear mappings are basis depen-
dent the trace of a linear mapping is independent of the basis.
In this section, we covered determinants and traces as functions char-
acterizing a square matrix. Taking together our understanding of determi-
nants and traces we can now define an important equation describing a
matrix A in terms of a polynomial, which we will use extensively in the
following sections.
Definition 4.5 (Characteristic Polynomial). For 2 R and a square ma-
trix A 2 Rn⇥n
pA ( ) := det(A I) (4.22a)
= c0 + c1 + c2 2
+ · · · + cn 1
n 1
+ ( 1) n n
, (4.22b)
characteristic c0 , . . . , cn 1 2 R, is the characteristic polynomial of A. In particular,
polynomial

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.2 Eigenvalues and Eigenvectors 105

c0 = det(A) , (4.23)
cn 1 = ( 1)n 1 tr(A) . (4.24)
The characteristic polynomial (4.22a) will allow us to compute eigen-
values and eigenvectors, covered in the next section.

4.2 Eigenvalues and Eigenvectors


We will now get to know a new way to characterize a matrix and its associ-
ated linear mapping. Recall from Section 2.7.1 that every linear mapping
has a unique transformation matrix given an ordered basis. We can in-
terpret linear mappings and their associated transformation matrices by
performing an “eigen” analysis. As we will see, the eigenvalues of a lin- Eigen is a German
ear mapping will tell us how a special set of vectors, the eigenvectors, is word meaning
“characteristic”,
transformed by the linear mapping.
“self”, or “own”.
Definition 4.6. Let A 2 Rn⇥n be a square matrix. Then 2 R is an
eigenvalue of A and x 2 R \{0} is the corresponding eigenvector of A if
n
eigenvalue
eigenvector
Ax = x . (4.25)
We call (4.25) the eigenvalue equation. eigenvalue equation

Remark. In the linear algebra literature and software, it is often a conven-


tion that eigenvalues are sorted in descending order, so that the largest
eigenvalue and associated eigenvector are called the first eigenvalue and
its associated eigenvector, and the second largest called the second eigen-
value and its associated eigenvector, and so on. However, textbooks and
publications may have different or no notion of orderings. We do not want
to presume an ordering in this book if not stated explicitly. }
The following statements are equivalent:
is an eigenvalue of A 2 Rn⇥n .
There exists an x 2 Rn \{0} with Ax = x, or equivalently, (A
I n )x = 0 can be solved non-trivially, i.e., x 6= 0.
rk(A I n ) < n.
det(A I n ) = 0.
Definition 4.7 (Collinearity and Codirection). Two vectors that point in
the same direction are called codirected. Two vectors are collinear if they codirected
point in the same or the opposite direction. collinear

Remark (Non-uniqueness of eigenvectors). If x is an eigenvector of A


associated with eigenvalue , then for any c 2 R\{0} it holds that cx is
an eigenvector of A with the same eigenvalue since
A(cx) = cAx = c x = (cx) . (4.26)
Thus, all vectors that are collinear to x are also eigenvectors of A.
}

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


106 Matrix Decompositions

Theorem 4.8. 2 R is eigenvalue of A 2 Rn⇥n if and only if is a root of


the characteristic polynomial pA ( ) of A.

algebraic Definition 4.9. Let a square matrix A have an eigenvalue i . The algebraic
multiplicity multiplicity of i is the number of times the root appears in the character-
istic polynomial.

Definition 4.10 (Eigenspace and Eigenspectrum). For A 2 Rn⇥n , the set


of all eigenvectors of A associated with an eigenvalue spans a subspace
eigenspace of Rn , which is called the eigenspace of A with respect to and is denoted
eigenspectrum by E . The set of all eigenvalues of A is called the eigenspectrum, or just
spectrum spectrum, of A.

If is an eigenvalue of A 2 Rn⇥n , then the corresponding eigenspace


E is the solution space of the homogeneous system of linear equations
(A I)x = 0. Geometrically, the eigenvector corresponding to a nonzero
eigenvalue points in a direction that is stretched by the linear mapping.
The eigenvalue is the factor by which it is stretched. If the eigenvalue is
negative, the direction of the stretching is flipped.

Example 4.4 (The Case of the Identity Matrix)


The identity matrix I 2 Rn⇥n has characteristic polynomial pI ( ) =
det(I I) = (1 )n = 0, which has only one eigenvalue = 1 that oc-
curs n times. Moreover, Ix = x = 1x holds for all vectors x 2 Rn \{0}.
Because of this, the sole eigenspace E1 of the identity matrix spans n di-
mensions, and all n standard basis vectors of Rn are eigenvectors of I .

Useful properties regarding eigenvalues and eigenvectors include the


following:

A matrix A and its transpose A> possess the same eigenvalues, but not
necessarily the same eigenvectors.
The eigenspace E is the null space of A I since

Ax = x () Ax x=0 (4.27a)
() (A I)x = 0 () x 2 ker(A I). (4.27b)

Similar matrices (see Definition 2.22) possess the same eigenvalues.


Therefore, a linear mapping has eigenvalues that are independent of
the choice of basis of its transformation matrix. This makes eigenvalues,
together with the determinant and the trace, key characteristic param-
eters of a linear mapping as they are all invariant under basis change.
Symmetric, positive definite matrices always have positive, real eigen-
values.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.2 Eigenvalues and Eigenvectors 107

Example 4.5 (Computing Eigenvalues, Eigenvectors, and


Eigenspaces)
Let us find the eigenvalues and eigenvectors of the 2 ⇥ 2 matrix

4 2
A= . (4.28)
1 3
Step 1: Characteristic Polynomial. From our definition of the eigen-
vector x 6= 0 and eigenvalue of A, there will be a vector such that
Ax = x, i.e., (A I)x = 0. Since x 6= 0, this requires that the kernel
(null space) of A I contains more elements than just 0. This means
that A I is not invertible and therefore det(A I) = 0. Hence, we
need to compute the roots of the characteristic polynomial (4.22a) to find
the eigenvalues.
Step 2: Eigenvalues. The characteristic polynomial is

pA ( ) = det(A I) (4.29a)
✓  ◆
4 2 0 4 2
= det = (4.29b)
1 3 0 1 3
= (4 )(3 ) 2 · 1. (4.29c)

We factorize the characteristic polynomial and obtain

p( ) = (4 )(3 ) 2 · 1 = 10 7 + 2
= (2 )(5 ) (4.30)
giving the roots 1 = 2 and 2 = 5.
Step 3: Eigenvectors and Eigenspaces. We find the eigenvectors that
correspond to these eigenvalues by looking at vectors x such that

4 2
x = 0. (4.31)
1 3
For = 5 we obtain
   
4 5 2 x1 1 2 x1
= = 0. (4.32)
1 3 5 x2 1 2 x2
We solve this homogeneous system and obtain a solution space

2
E5 = span[ ]. (4.33)
1
This eigenspace is one-dimensional as it possesses a single basis vector.
Analogously, we find the eigenvector for = 2 by solving the homoge-
neous system of equations
 
4 2 2 2 2
x= x = 0. (4.34)
1 3 2 1 1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


108 Matrix Decompositions

 
x1 1
This means any vector x = , where x2 = x1 , such as , is an
x2 1
eigenvector with eigenvalue 2. The corresponding eigenspace is given as

1
E2 = span[ ]. (4.35)
1

The two eigenspaces E5 and E2 in Example 4.5 are one-dimensional


as they are each spanned by a single vector. However, in other cases
we may have multiple identical eigenvalues (see Definition 4.9) and the
eigenspace may have more than one dimension.
Definition 4.11. Let i be an eigenvalue of a square matrix A. Then the
geometric geometric multiplicity of i is the number of linearly independent eigen-
multiplicity vectors associated with i . In other words, it is the dimensionality of the
eigenspace spanned by the eigenvectors associated with i .
Remark. A specific eigenvalue’s geometric multiplicity must be at least
one because every eigenvalue has at least one associated eigenvector. An
eigenvalue’s geometric multiplicity cannot exceed its algebraic multiplic-
ity, but it may be lower. }

Example 4.6 
2 1
The matrix A = has two repeated eigenvalues 1 = 2 = 2 and an
0 2
algebraic multiplicity of 2. The eigenvalue has, however, only one distinct
1
unit eigenvector x1 = and, thus, geometric multiplicity 1.
0

Graphical Intuition in Two Dimensions


Let us gain some intuition for determinants, eigenvectors, and eigenval-
ues using different linear mappings. Figure 4.4 depicts five transformation
matrices A1 , . . . , A5 and their impact on a square grid of points, centered
In geometry, the at the origin:
area-preserving 1
properties of this 0
A1 = 2 . The direction of the two eigenvectors correspond to the
type of shearing 0 2
parallel to an axis is canonical basis vectors in R2 , i.e., to two cardinal axes. The vertical axis
also known as
Cavalieri’s principle
is extended by a factor of 2 (eigenvalue 1 = 2), and the horizontal axis
of equal areas for is compressed by factor 12 (eigenvalue 2 = 12 ). The mapping is area
parallelograms preserving (det(A1 ) = 1 = 2 · 12 ).
(Katz, 2004).

1 12
A2 = corresponds to a shearing mapping , i.e., it shears the
0 1
points along the horizontal axis to the right if they are on the positive

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.2 Eigenvalues and Eigenvectors 109

Figure 4.4
Determinants and
eigenspaces.
Overview of five
∏1 = 2.0 linear mappings and
∏2 = 0.5 their associated
det(A) = 1.0
transformation
matrices
Ai 2 R2⇥2
projecting 400
color-coded points
x 2 R2 (left
∏1 = 1.0
∏2 = 1.0 column) onto target
det(A) = 1.0 points Ai x (right
column). The
central column
depicts the first
eigenvector,
stretched by its
∏1 = (0.87-0.5j) associated
∏2 = (0.87+0.5j) eigenvalue 1 , and
det(A) = 1.0
the second
eigenvector
stretched by its
eigenvalue 2 . Each
row depicts the
effect of one of five
∏1 = 0.0
∏2 = 2.0 transformation
det(A) = 0.0 matrices Ai with
respect to the
standard basis .

∏1 = 0.5
∏2 = 1.5
det(A) = 0.75

half of the vertical axis, and to the left vice versa. This mapping is area
preserving (det(A2 ) = 1). The eigenvalue 1 = 1 = 2 is repeated
and the eigenvectors are collinear (drawn here for emphasis in two
opposite directions). This indicates that the mapping acts only along
one direction
 (the horizontal axis).
p
cos( ⇡6 ) sin( ⇡6 ) 3 p1
A3 = 1
= 2 The matrix A3 rotates the
sin( ⇡6 ) cos( ⇡6 ) 1 3
points by ⇡6 rad = 30 counter-clockwise and has only complex eigen-
values, reflecting that the mapping is a rotation (hence, no eigenvectors
are drawn). A rotation has to be volume preserving, and so the deter-
minantis 1. For more details on rotations, we refer to Section 3.9.
1 1
A4 = represents a mapping in the standard basis that col-
1 1
lapses a two-dimensional domain onto one dimension. Since one eigen-

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


110 Matrix Decompositions

value is 0, the space in direction of the (blue) eigenvector corresponding


to 1 = 0 collapses, while the orthogonal (red) eigenvector stretches
space by
 a factor 2 = 2. Therefore, the area of the image is 0.
1 12
A5 = 1 is a shear-and-stretch mapping that scales space by 75%
2
1
since | det(A5 )| = 34 . It stretches space along the (blue) eigenvector
of 2 by a factor 1.5 and compresses it along the orthogonal (blue)
eigenvector by a factor 0.5.

Example 4.7 (Eigenspectrum of a Biological Neural Network)

Figure 4.5
0
Caenorhabditis 25
elegans neural 50 20
network (Kaiser and
15
Hilgetag, 2006). 100
neuron index

eigenvalue

(a) Symmetrized 10
connectivity matrix; 150 5
(b) Eigenspectrum.
0
200
°5

250 °10

0 50 100 150 200 250 0 100 200


neuron index index of sorted eigenvalue

(a) Connectivity matrix. (b) Eigenspectrum.

Methods to analyze and learn from network data are an essential com-
ponent of machine learning methods. The key to understanding networks
is the connectivity between network nodes, especially if two nodes are
connected to each other or not. In data science applications, it is often
useful to study the matrix that captures this connectivity data.
We build a connectivity/adjacency matrix A 2 R277⇥277 of the complete
neural network of the worm C.Elegans. Each row/column represents one
of the 277 neurons of this worm’s brain. The connectivity matrix A has
a value of aij = 1 if neuron i talks to neuron j through a synapse, and
aij = 0 otherwise. The connectivity matrix is not symmetric, which im-
plies that eigenvalues may not be real valued. Therefore, we compute a
symmetrized version of the connectivity matrix as Asym := A + A> . This
new matrix Asym is shown in Figure 4.5(a) and has a nonzero value aij if
and only if two neurons are connected (white pixels), irrespective of the
direction of the connection. In Figure 4.5(b), we show the correspond-
ing eigenspectrum of Asym . The horizontal axis shows the index of the
eigenvalues, sorted in descending order. The vertical axis shows the corre-
sponding eigenvalue. The S -like shape of this eigenspectrum is typical for
many biological neural networks. The underlying mechanism responsible
for this is an area of active neuroscience research.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.2 Eigenvalues and Eigenvectors 111

Theorem 4.12. The eigenvectors x1 , . . . , xn of a matrix A 2 Rn⇥n with n


distinct eigenvalues 1 , . . . , n are linearly independent.
This theorem states that eigenvectors of a matrix with n distinct eigen-
values form a basis of Rn .
Definition 4.13. A square matrix A 2 Rn⇥n is defective if it possesses defective
fewer than n linearly independent eigenvectors.
A non-defective matrix A 2 Rn⇥n does not necessarily require n dis-
tinct eigenvalues, but it does require that the eigenvectors form a basis of
Rn . Looking at the eigenspaces of a defective matrix, it follows that the
sum of the dimensions of the eigenspaces is less than n. Specifically, a de-
fective matrix has at least one eigenvalue i with an algebraic multiplicity
m > 1 and a geometric multiplicity of less than m.
Remark. A defective matrix cannot have n distinct eigenvalues, as distinct
eigenvalues have linearly independent eigenvectors (Theorem 4.12). }
Theorem 4.14. Given a matrix A 2 Rm⇥n , we can always obtain a sym-
metric, positive semidefinite matrix S 2 Rn⇥n by defining
S := A> A . (4.36)
Remark. If rk(A) = n, then S := A> A is symmetric, positive definite.
}
Understanding why Theorem 4.14 holds is insightful for how we can
use symmetrized matrices: Symmetry requires S = S > , and by insert-
ing (4.36) we obtain S = A> A = A> (A> )> = (A> A)> = S > . More-
over, positive semidefiniteness (Section 3.2.3) requires that x> Sx > 0
and inserting (4.36) we obtain x> Sx = x> A> Ax = (x> A> )(Ax) =
(Ax)> (Ax) > 0, because the dot product computes a sum of squares
(which are themselves non-negative).
spectral theorem
Theorem 4.15 (Spectral Theorem). If A 2 Rn⇥n is symmetric, there ex-
ists an orthonormal basis of the corresponding vector space V consisting of
eigenvectors of A, and each eigenvalue is real.
A direct implication of the spectral theorem is that the eigendecompo-
sition of a symmetric matrix A exists (with real eigenvalues), and that
we can find an ONB of eigenvectors so that A = P DP > , where D is
diagonal and the columns of P contain the eigenvectors.

Example 4.8
Consider the matrix
2 3
3 2 2
A = 4 2 3 25 . (4.37)
2 2 3

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


112 Matrix Decompositions

The characteristic polynomial of A is


pA ( ) = ( 1)2 ( 7) , (4.38)
so that we obtain the eigenvalues 1 = 1 and 2 = 7, where 1 is a
repeated eigenvalue. Following our standard procedure for computing
eigenvectors, we obtain the eigenspaces
2 3 2 3 2 3
1 1 1
E1 = span[4 1 5, 4 0 5], E7 = span[415] . (4.39)
0 1 1
| {z } | {z } |{z}
=:x1 =:x2 =:x3

We see that x3 is orthogonal to both x1 and x2 . However, since x> 1 x2 =


1 6= 0, they are not orthogonal. The spectral theorem (Theorem 4.15)
states that there exists an orthogonal basis, but the one we have is not
orthogonal. However, we can construct one.
To construct such a basis, we exploit the fact that x1 , x2 are eigenvec-
tors associated with the same eigenvalue . Therefore, for any ↵, 2 R it
holds that
A(↵x1 + x2 ) = Ax1 ↵ + Ax2 = (↵x1 + x2 ) , (4.40)
i.e., any linear combination of x1 and x2 is also an eigenvector of A as-
sociated with . The Gram-Schmidt algorithm (Section 3.8.3) is a method
for iteratively constructing an orthogonal/orthonormal basis from a set of
basis vectors using such linear combinations. Therefore, even if x1 and x2
are not orthogonal, we can apply the Gram-Schmidt algorithm and find
eigenvectors associated with 1 = 1 that are orthogonal to each other
(and to x3 ). In our example, we will obtain
2 3 2 3
1 1
1
x01 = 4 1 5 , x02 = 4 15 , (4.41)
0 2 2

which are orthogonal to each other, orthogonal to x3 , and eigenvectors of


A associated with 1 = 1.

Before we conclude our considerations of eigenvalues and eigenvectors


it is useful to tie these matrix characteristics together with the concepts of
the determinant and the trace.
Theorem 4.16. The determinant of a matrix A 2 Rn⇥n is the product of
its eigenvalues, i.e.,
n
Y
det(A) = i , (4.42)
i=1

where i are (possibly repeated) eigenvalues of A.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.2 Eigenvalues and Eigenvectors 113

Figure 4.6
Geometric
x2 A interpretation of
eigenvalues. The
v2 eigenvectors of A
x1 v1 get stretched by the
corresponding
eigenvalues. The
area of the unit
Theorem 4.17. The trace of a matrix A 2 Rn⇥n is the sum of its eigenval-
square changes by
ues, i.e., | 1 2 |, the
Xn circumference
tr(A) = i, (4.43) changes by a factor
i=1 2(| 1 | + | 2 |).

where i are (possibly repeated) eigenvalues of A.

Let us provide a geometric intuition of these two theorems. Consider


a matrix A 2 R2⇥2 that possesses two linearly independent eigenvectors
x1 , x2 . For this example, we assume (x1 , x2 ) are an ONB of R2 so that they
are orthogonal and the area of the square they span is 1; see Figure 4.6.
From Section 4.1, we know that the determinant computes the change of
area of unit square under the transformation A. In this example, we can
compute the change of area explicitly: Mapping the eigenvectors using
A gives us vectors v 1 = Ax1 = 1 x1 and v 2 = Ax2 = 2 x2 , i.e., the
new vectors v i are scaled versions of the eigenvectors xi , and the scaling
factors are the corresponding eigenvalues i . v 1 , v 2 are still orthogonal,
and the area of the rectangle they span is | 1 2 |.
Given that x1 , x2 (in our example) are orthonormal, we can directly
compute the circumference of the unit square as 2(1 + 1). Mapping the
eigenvectors using A creates a rectangle whose circumference is 2(| 1 | +
| 2 |). Therefore, the sum of the absolute values of the eigenvalues tells us
how the circumference of the unit square changes under the transforma-
tion matrix A.

Example 4.9 (Google’s PageRank – Webpages as Eigenvectors)


Google uses the eigenvector corresponding to the maximal eigenvalue of
a matrix A to determine the rank of a page for search. The idea for the
PageRank algorithm, developed at Stanford University by Larry Page and
Sergey Brin in 1996, was that the importance of any web page can be ap-
proximated by the importance of pages that link to it. For this, they write
down all web sites as a huge directed graph that shows which page links
to which. PageRank computes the weight (importance) xi > 0 of a web
site ai by counting the number of pages pointing to ai . Moreover, PageR-
ank takes into account the importance of the web sites that link to ai . The
navigation behavior of a user is then modeled by a transition matrix A of
this graph that tells us with what (click) probability somebody will end up

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


114 Matrix Decompositions

on a different web site. The matrix A has the property that for any ini-
tial rank/importance vector x of a web site the sequence x, Ax, A2 x, . . .
PageRank converges to a vector x⇤ . This vector is called the PageRank and satisfies
Ax⇤ = x⇤ , i.e., it is an eigenvector (with corresponding eigenvalue 1) of
A. After normalizing x⇤ , such that kx⇤ k = 1, we can interpret the entries
as probabilities. More details and different perspectives on PageRank can
be found in the original technical report (Page et al., 1999).

4.3 Cholesky Decomposition


There are many ways to factorize special types of matrices that we en-
counter often in machine learning. In the positive real numbers, we have
the square-root operation that gives us a decomposition of the number
into identical components, e.g., 9 = 3 · 3. For matrices, we need to be
careful that we compute a square-root-like operation on positive quanti-
ties. For symmetric, positive definite matrices (see Section 3.2.3), we can
Cholesky choose from a number of square-root equivalent operations. The Cholesky
decomposition decomposition/Cholesky factorization provides a square-root equivalent op-
Cholesky eration on symmetric, positive definite matrices that is useful in practice.
factorization
Theorem 4.18 (Cholesky Decomposition). A symmetric, positive definite
matrix A can be factorized into a product A = LL> , where L is a lower-
triangular matrix with positive diagonal elements:
2 3 2 32 3
a11 · · · a1n l11 · · · 0 l11 · · · ln1
6 .. .. .. 7 = 6 .. . . .. 7 6 .. . . .. 7 .
4 . . . 5 4 . . . 54 . . . 5 (4.44)
an1 · · · ann ln1 · · · lnn 0 · · · lnn

Cholesky factor L is called the Cholesky factor of A, and L is unique.

Example 4.10 (Cholesky Factorization)


Consider a symmetric, positive definite matrix A 2 R3⇥3 . We are inter-
ested in finding its Cholesky factorization A = LL> , i.e.,
2 3 2 32 3
a11 a21 a31 l11 0 0 l11 l21 l31
4 5 > 4 5 4
a a a
A = 21 22 32 = LL = 21 22 l l 0 0 l22 l32 5 . (4.45)
a31 a32 a33 l31 l32 l33 0 0 l33
Multiplying out the right-hand side yields
2 2 3
l11 l21 l11 l31 l11
A = 4l21 l11 2
l21 + l222
l31 l21 + l32 l22 5 . (4.46)
2 2 2
l31 l11 l31 l21 + l32 l22 l31 + l32 + l33

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.4 Eigendecomposition and Diagonalization 115

Comparing the left-hand side of (4.45) and the right-hand side of (4.46)
shows that there is a simple pattern in the diagonal elements lii :
q q
p
l11 = a11 , l22 = a22 l21 2
, l33 = a33 (l31 2 2
+ l32 ) . (4.47)
Similarly for the elements below the diagonal (lij , where i > j ), there is
also a repeating pattern:
1 1 1
l21 = a21 , l31 = a31 , l32 = (a32 l31 l21 ) . (4.48)
l11 l11 l22
Thus, we constructed the Cholesky decomposition for any symmetric, pos-
itive definite 3 ⇥ 3 matrix. The key realization is that we can backward
calculate what the components lij for the L should be, given the values
aij for A and previously computed values of lij .

The Cholesky decomposition is an important tool for the numerical


computations underlying machine learning. Here, symmetric positive def-
inite matrices require frequent manipulation, e.g., the covariance matrix
of a multivariate Gaussian variable (see Section 6.5) is symmetric, positive
definite. The Cholesky factorization of this covariance matrix allows us to
generate samples from a Gaussian distribution. It also allows us to perform
a linear transformation of random variables, which is heavily exploited
when computing gradients in deep stochastic models, such as the varia-
tional auto-encoder (Jimenez Rezende et al., 2014; Kingma and Welling,
2014). The Cholesky decomposition also allows us to compute determi-
nants very efficiently. Given the Cholesky decomposition A = LL> , we
know that det(A) = det(L) det(L> ) = det(L)2 . Since L is a triangular
matrix, the determinant
Q 2 is simply the product of its diagonal entries so
that det(A) = i lii . Thus, many numerical software packages use the
Cholesky decomposition to make computations more efficient.

4.4 Eigendecomposition and Diagonalization


A diagonal matrix is a matrix that has value zero on all off-diagonal ele- diagonal matrix
ments, i.e., they are of the form
2 3
c1 · · · 0
D = 4 ... . . . ... 5 .
6 7
(4.49)
0 · · · cn
They allow fast computation of determinants, powers, and inverses. The
determinant is the product of its diagonal entries, a matrix power D k is
given by each diagonal element raised to the power k , and the inverse
D 1 is the reciprocal of its diagonal elements if all of them are nonzero.
In this section, we will discuss how to transform matrices into diagonal

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


116 Matrix Decompositions

form. This is an important application of the basis change we discussed in


Section 2.7.2 and eigenvalues from Section 4.2.
Recall that two matrices A, D are similar (Definition 2.22) if there ex-
ists an invertible matrix P , such that D = P 1 AP . More specifically, we
will look at matrices A that are similar to diagonal matrices D that con-
tain the eigenvalues of A on the diagonal.
diagonalizable Definition 4.19 (Diagonalizable). A matrix A 2 Rn⇥n is diagonalizable
if it is similar to a diagonal matrix, i.e., if there exists an invertible matrix
P 2 Rn⇥n such that D = P 1 AP .
In the following, we will see that diagonalizing a matrix A 2 Rn⇥n is
a way of expressing the same linear mapping but in another basis (see
Section 2.6.1), which will turn out to be a basis that consists of the eigen-
vectors of A.
Let A 2 Rn⇥n , let 1 , . . . , n be a set of scalars, and let p1 , . . . , pn be a
set of vectors in Rn . We define P := [p1 , . . . , pn ] and let D 2 Rn⇥n be a
diagonal matrix with diagonal entries 1 , . . . , n . Then we can show that
AP = P D (4.50)
if and only if 1 , . . . , n are the eigenvalues of A and p1 , . . . , pn are cor-
responding eigenvectors of A.
We can see that this statement holds because
AP = A[p1 , . . . , pn ] = [Ap1 , . . . , Apn ] , (4.51)
2 3
1 0
6
P D = [p1 , . . . , pn ] 4 .. 7
(4.52)
. 5 = [ 1 p1 , . . . , n pn ] .

0 n

Thus, (4.50) implies that


Ap1 = 1 p1 (4.53)
..
.
Apn = n pn . (4.54)
Therefore, the columns of P must be eigenvectors of A.
Our definition of diagonalization requires that P 2 Rn⇥n is invertible,
i.e., P has full rank (Theorem 4.3). This requires us to have n linearly
independent eigenvectors p1 , . . . , pn , i.e., the pi form a basis of Rn .
Theorem 4.20 (Eigendecomposition). A square matrix A 2 Rn⇥n can be
factored into
1
A = P DP , (4.55)
where P 2 Rn⇥n and D is a diagonal matrix whose diagonal entries are
the eigenvalues of A, if and only if the eigenvectors of A form a basis of Rn .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.4 Eigendecomposition and Diagonalization 117

Figure 4.7 Intuition


behind the
p2 eigendecomposition
p1 2 p2 1 p1 as sequential
A transformations.
Top-left to
bottom-left: P 1
performs a basis
P 1 P
change (here drawn
in R2 and depicted
e2 as a rotation-like
2 e2 operation), mapping
D the eigenvectors
into the standard
e1 1 e1 basis. Bottom-left to
bottom-right: D
performs a scaling
along the remapped
Theorem 4.20 implies that only non-defective matrices can be diagonal- orthogonal
ized and that the columns of P are the n eigenvectors of A. For symmetric eigenvectors,
depicted here by a
matrices we can obtain even stronger outcomes for the eigenvalue decom-
circle being
position. stretched to an
ellipse. Bottom-right
Theorem 4.21. A symmetric matrix S 2 Rn⇥n can always be diagonalized. to top-right: P
undoes the basis
Theorem 4.21 follows directly from the spectral theorem 4.15. More- change (depicted as
over, the spectral theorem states that we can find an ONB of eigenvectors a reverse rotation)
and restores the
of Rn . This makes P an orthogonal matrix so that D = P > AP .
original coordinate
Remark. The Jordan normal form of a matrix offers a decomposition that frame.
works for defective matrices (Lang, 1987) but is beyond the scope of this
book. }

Geometric Intuition for the Eigendecomposition


We can interpret the eigendecomposition of a matrix as follows (see also
Figure 4.7): Let A be the transformation matrix of a linear mapping with
respect to the standard basis. P 1 performs a basis change from the stan-
dard basis into the eigenbasis. This identifies the eigenvectors pi (red and
orange arrows in Figure 4.7) onto the standard basis vectors ei . Then, the
diagonal D scales the vectors along these axes by the eigenvalues i . Fi-
nally, P transforms these scaled vectors back into the standard/canonical
coordinates yielding i pi .

Example 4.11 (Eigendecomposition) 


2 1
Let us compute the eigendecomposition of A = .
1 2
Step 1: Compute eigenvalues and eigenvectors. The characteristic

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


118 Matrix Decompositions

polynomial of A is
✓ ◆
2 1
det(A I) = det (4.56a)
1 2
= (2 )2 1= 2
4 +3=( 3)( 1) . (4.56b)
Therefore, the eigenvalues of A are 1 = 1 and 2 = 3 (the roots of the
characteristic polynomial), and the associated (normalized) eigenvectors
are obtained via
 
2 1 2 1
p1 = 1p1 , p = 3p2 . (4.57)
1 2 1 2 2
This yields
 
1 1 1 1
p1 = p , p2 = p . (4.58)
2 1 2 1
Step 2: Check for existence. The eigenvectors p1 , p2 form a basis of
R2 . Therefore, A can be diagonalized.
Step 3: Construct the matrix P to diagonalize A. We collect the
eigenvectors of A in P so that

1 1 1
P = [p1 , p2 ] = p . (4.59)
2 1 1
We then obtain

1 1 0
P AP = = D. (4.60)
0 3
Equivalently, we get (exploiting that P 1 = P > since the eigenvectors p1
and p2 in this example form an ONB)
   
2 1 1 1 1 1 0 1 1 1
=p p . (4.61)
1 2 2 1 1 0 3 2 1 1
| {z } | {z } | {z } | {z }
A P D P>

Diagonal matrices D can efficiently be raised to a power. Therefore,


we can find a matrix power for a matrix A 2 Rn⇥n via the eigenvalue
decomposition (if it exists) so that

Ak = (P DP 1 k
) = P Dk P 1
. (4.62)

Computing D k is efficient because we apply this operation individually


to any diagonal element.
Assume that the eigendecomposition A = P DP 1 exists. Then,
1 1
det(A) = det(P DP ) = det(P ) det(D) det(P ) (4.63a)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.5 Singular Value Decomposition 119
Y
= det(D) = dii (4.63b)
i

allows for an efficient computation of the determinant of A.

The eigenvalue decomposition requires square matrices. It would be


useful to perform a decomposition on general matrices. In the next sec-
tion, we introduce a more general matrix decomposition technique, the
singular value decomposition.

4.5 Singular Value Decomposition


The singular value decomposition (SVD) of a matrix is a central matrix
decomposition method in linear algebra. It has been referred to as the
“fundamental theorem of linear algebra” (Strang, 1993) because it can be
applied to all matrices, not only to square matrices, and it always exists.
Moreover, as we will explore in the following, the SVD of a matrix A,
which represents a linear mapping : V ! W , quantifies the change
between the underlying geometry of these two vector spaces. We recom-
mend the work by Kalman (1996) and Roy and Banerjee (2014) for a
deeper overview of the mathematics of the SVD.
SVD theorem
Theorem 4.22 (SVD Theorem). Let Am⇥n be a rectangular matrix of rank
r 2 [0, min(m, n)]. The SVD of A is a decomposition of the form SVD
singular value
n m n decomposition
n

A = U ⌃ V>
m

(4.64)

with an orthogonal matrix U 2 Rm⇥m with column vectors ui , i = 1, . . . , m,


and an orthogonal matrix V 2 Rn⇥n with column vectors v j , j = 1, . . . , n.
Moreover, ⌃ is an m ⇥ n matrix with ⌃ii = i > 0 and ⌃ij = 0, i 6= j .

The diagonal entries i , i = 1, . . . , r, of ⌃ are called the singular values, singular values
ui are called the left-singular vectors, and v j are called the right-singular left-singular vectors
vectors. By convention, the singular values are ordered, i.e., 1 > 2 > right-singular
r > 0.
vectors

The singular value matrix ⌃ is unique, but it requires some attention. singular value
Observe that the ⌃ 2 Rm⇥n is rectangular. In particular, ⌃ is of the same matrix
size as A. This means that ⌃ has a diagonal submatrix that contains the
singular values and needs additional zero padding. Specifically, if m > n,
then the matrix ⌃ has diagonal structure up to row n and then consists of

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


120 Matrix Decompositions

Figure 4.8 Intuition


behind the SVD of a
matrix A 2 R3⇥2 V2
as sequential V1 A 2 u2
transformations.
Top-left to
bottom-left: V > 1 u1
performs a basis
change in R2 . V> U
Bottom-left to
bottom-right: ⌃
scales and maps e2
from R2 to R3 . The 2 e2
ellipse in the ⌃
bottom-right lives in e1 1 e1
R3 . The third
dimension is
orthogonal to the
surface of the
elliptical disk.
0> row vectors from n + 1 to m below so that
2 3
Bottom-right to 1 0 0
top-right: U
6 0 ... 0 7
6 7
performs a basis
6 7
change within R3 . 60 0 7
⌃=6 6 0 ... 0 7 .
n7
(4.65)
6 7
6. .. 7
4 .. . 5
0 ... 0
If m < n, the matrix ⌃ has a diagonal structure up to column m and
columns that consist of 0 from m + 1 to n:
2 3
1 0 0 0 ... 0
⌃ = 4 0 ... 0 0
6 7
05 . (4.66)
0 0 m 0 ... 0
Remark. The SVD exists for any matrix A 2 Rm⇥n . }

4.5.1 Geometric Intuitions for the SVD


The SVD offers geometric intuitions to describe a transformation matrix
A. In the following, we will discuss the SVD as sequential linear trans-
formations performed on the bases. In Example 4.12, we will then apply
transformation matrices of the SVD to a set of vectors in R2 , which allows
us to visualize the effect of each transformation more clearly.
The SVD of a matrix can be interpreted as a decomposition of a corre-
sponding linear mapping (recall Section 2.7.1) : Rn ! Rm into three
operations; see Figure 4.8. The SVD intuition follows superficially a simi-
lar structure to our eigendecomposition intuition, see Figure 4.7: Broadly
speaking, the SVD performs a basis change via V > followed by a scal-
ing and augmentation (or reduction) in dimensionality via the singular

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.5 Singular Value Decomposition 121

value matrix ⌃. Finally, it performs a second basis change via U . The SVD
entails a number of important details and caveats, which is why we will
review our intuition in more detail. It is useful to revise
Assume we are given a transformation matrix of a linear mapping : basis changes
(Section 2.7.2),
Rn ! Rm with respect to the standard bases B and C of Rn and Rm ,
orthogonal matrices
respectively. Moreover, assume a second basis B̃ of Rn and C̃ of Rm . Then (Definition 3.8) and
orthonormal bases
1. The matrix V performs a basis change in the domain Rn from B̃ (rep- (Section 3.5).
resented by the red and orange vectors v 1 and v 2 in the top-left of Fig-
ure 4.8) to the standard basis B . V > = V 1 performs a basis change
from B to B̃ . The red and orange vectors are now aligned with the
canonical basis in the bottom-left of Figure 4.8.
2. Having changed the coordinate system to B̃ , ⌃ scales the new coordi-
nates by the singular values i (and adds or deletes dimensions), i.e.,
⌃ is the transformation matrix of with respect to B̃ and C̃ , rep-
resented by the red and orange vectors being stretched and lying in
the e1 -e2 plane, which is now embedded in a third dimension in the
bottom-right of Figure 4.8.
3. U performs a basis change in the codomain Rm from C̃ into the canoni-
cal basis of Rm , represented by a rotation of the red and orange vectors
out of the e1 -e2 plane. This is shown in the top-right of Figure 4.8.

The SVD expresses a change of basis in both the domain and codomain.
This is in contrast with the eigendecomposition that operates within the
same vector space, where the same basis change is applied and then un-
done. What makes the SVD special is that these two different bases are
simultaneously linked by the singular value matrix ⌃.

Example 4.12 (Vectors and the SVD)


Consider a mapping of a square grid of vectors X 2 R2 that fit in a box of
size 2 ⇥ 2 centered at the origin. Using the standard basis, we map these
vectors using
2 3
1 0.8
A = 40 1 5 = U ⌃V > (4.67a)
1 0
2 32 3
0.79 0 0.62 1.62 0 
0.78 0.62
= 4 0.38 0.78 0.495 4 0 1.05 . (4.67b)
0.62 0.78
0.48 0.62 0.62 0 0
We start with a set of vectors X (colored dots; see top-left panel of Fig-
ure 4.9) arranged in a grid. We then apply V > 2 R2⇥2 , which rotates X .
The rotated vectors are shown in the bottom-left panel of Figure 4.9. We
now map these vectors using the singular value matrix ⌃ to the codomain
R3 (see the bottom-right panel in Figure 4.9). Note that all vectors lie in

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


122 Matrix Decompositions

the x1 -x2 plane. The third coordinate is always 0. The vectors in the x1 -x2
plane have been stretched by the singular values.
The direct mapping of the vectors X by A to the codomain R3 equals
the transformation of X by U ⌃V > , where U performs a rotation within
the codomain R3 so that the mapped vectors are no longer restricted to
the x1 -x2 plane; they still are on a plane as shown in the top-right panel
of Figure 4.9.

Figure 4.9 SVD and 1.5


mapping of vectors
(represented by
1.0
discs). The panels 1.0
follow the same
0.5 0.5
anti-clockwise

x3
structure of 0.0
x2

0.0
Figure 4.8.
-0.5
0.5
-1.0
1.5
1.0 0.5
-1.5
1.5 -0.5 -0.5 x2
1.5 1.0 0.5 0.0 0.5 1.0 1.5 0.5
x1 x1 1.5
-1.5
1.5

1.0

0.5

0 x3
x2

0.0

0.5

1.5
1.0
0.5
-1.5
1.5 -0.5 -0.5 x2
1.5 1.0 0.5 0.0 0.5 1.0 1.5 0.5
x1 x1 1.5 -1.5

4.5.2 Construction of the SVD


We will next discuss why the SVD exists and show how to compute it
in detail. The SVD of a general matrix shares some similarities with the
eigendecomposition of a square matrix.
Remark. Compare the eigendecomposition of an SPD matrix

S = S > = P DP > (4.68)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.5 Singular Value Decomposition 123

with the corresponding SVD


S = U ⌃V > . (4.69)
If we set
U =P =V , D = ⌃, (4.70)
we see that the SVD of SPD matrices is their eigendecomposition. }
In the following, we will explore why Theorem 4.22 holds and how
the SVD is constructed. Computing the SVD of A 2 Rm⇥n is equivalent
to finding two sets of orthonormal bases U = (u1 , . . . , um ) and V =
(v 1 , . . . , v n ) of the codomain Rm and the domain Rn , respectively. From
these ordered bases, we will construct the matrices U and V .
Our plan is to start with constructing the orthonormal set of right-
singular vectors v 1 , . . . , v n 2 Rn . We then construct the orthonormal set
of left-singular vectors u1 , . . . , um 2 Rm . Thereafter, we will link the two
and require that the orthogonality of the v i is preserved under the trans-
formation of A. This is important because we know that the images Av i
form a set of orthogonal vectors. We will then normalize these images by
scalar factors, which will turn out to be the singular values.
Let us begin with constructing the right-singular vectors. The spectral
theorem (Theorem 4.15) tells us that a symmetric matrix possesses an
ONB of eigenvectors, which also means it can be diagonalized. More-
over, from Theorem 4.14 we can always construct a symmetric, positive
semidefinite matrix A> A 2 Rn⇥n from any rectangular matrix A 2
Rm⇥n . Thus, we can always diagonalize A> A and obtain
2 3
1 ··· 0
A> A = P DP > = P 4 ... . . . ... 5 P > ,
6 7
(4.71)
0 ··· n
where P is an orthogonal matrix, which is composed of the orthonormal
eigenbasis. The i > 0 are the eigenvalues of A> A. Let us assume the
SVD of A exists and inject (4.64) into (4.71). This yields
A> A = (U ⌃V > )> (U ⌃V > ) = V ⌃> U > U ⌃V > , (4.72)

where U , V are orthogonal matrices. Therefore, with U > U = I we ob-


tain
2 2 3
1 0 0
A> A = V ⌃> ⌃V > = V 4 0 . . . 0 5 V > .
6 7
(4.73)
2
0 0 n

Comparing now (4.71) and (4.73), we identify


V > = P> , (4.74)
2
i = i. (4.75)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


124 Matrix Decompositions

Therefore, the eigenvectors of A> A that compose P are the right-singular


vectors V of A (see (4.74)). The eigenvalues of A> A are the squared
singular values of ⌃ (see (4.75)).
To obtain the left-singular vectors U , we follow a similar procedure.
We start by computing the SVD of the symmetric matrix AA> 2 Rm⇥m
(instead of the previous A> A 2 Rn⇥n ). The SVD of A yields
AA> = (U ⌃V > )(U ⌃V > )> = U ⌃V > V ⌃> U > (4.76a)
2 2 3
1 0 0
= U 4 0 ... 0 5 U> .
6 7
(4.76b)
2
0 0 m

The spectral theorem tells us that AA> = SDS > can be diagonalized
and we can find an ONB of eigenvectors of AA> , which are collected in
S . The orthonormal eigenvectors of AA> are the left-singular vectors U
and form an orthonormal basis set in the codomain of the SVD.
This leaves the question of the structure of the matrix ⌃. Since AA>
and A> A have the same nonzero eigenvalues (see page 106) the nonzero
entries of the ⌃ matrices in the SVD for both cases have to be the same.
The last step is to link up all the parts we touched upon so far. We have
an orthonormal set of right-singular vectors in V . To finish the construc-
tion of the SVD, we connect them with the orthonormal vectors U . To
reach this goal, we use the fact the images of the v i under A have to be
orthogonal, too. We can show this by using the results from Section 3.4.
We require that the inner product between Av i and Av j must be 0 for
i 6= j . For any two orthogonal eigenvectors v i , v j , i 6= j , it holds that
>
(Av i )> (Av j ) = v > >
i (A A)v j = v i ( j v j ) =
>
j vi vj = 0. (4.77)
For the case m > r, it holds that {Av 1 , . . . , Av r } is a basis of an r-
dimensional subspace of Rm .
To complete the SVD construction, we need left-singular vectors that
are orthonormal: We normalize the images of the right-singular vectors
Av i and obtain
Av i 1 1
ui := = p Av i = Av i , (4.78)
kAv i k i i

where the last equality was obtained from (4.75) and (4.76b), showing
us that the eigenvalues of AA> are such that i2 = i .
Therefore, the eigenvectors of A> A, which we know are the right-
singular vectors v i , and their normalized images under A, the left-singular
vectors ui , form two self-consistent ONBs that are connected through the
singular value matrix ⌃.
singular value Let us rearrange (4.78) to obtain the singular value equation
equation
Av i = i ui , i = 1, . . . , r . (4.79)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.5 Singular Value Decomposition 125

This equation closely resembles the eigenvalue equation (4.25), but the
vectors on the left- and the right-hand sides are not the same.
For n > m, (4.79) holds only for i 6 m and (4.79) says nothing about
the ui for i > m. However, we know by construction that they are or-
thonormal. Conversely, for m > n, (4.79) holds only for i 6 n. For i > n,
we have Av i = 0 and we still know that the v i form an orthonormal set.
This means that the SVD also supplies an orthonormal basis of the kernel
(null space) of A, the set of vectors x with Ax = 0 (see Section 2.7.3).
Moreover, concatenating the v i as the columns of V and the ui as the
columns of U yields
AV = U ⌃ , (4.80)

where ⌃ has the same dimensions as A and a diagonal structure for rows
1, . . . , r. Hence, right-multiplying with V > yields A = U ⌃V > , which is
the SVD of A.

Example 4.13 (Computing the SVD)


Let us find the singular value decomposition of

1 0 1
A= . (4.81)
2 1 0
The SVD requires us to compute the right-singular vectors v j , the singular
values k , and the left-singular vectors ui .
Step 1: Right-singular vectors as the eigenbasis of A> A.
We start by computing
2 3 2 3
1 2  5 2 1
1 0 1
A> A = 4 0 1 5 = 4 2 1 05 . (4.82)
2 1 0
1 0 1 0 1
We compute the singular values and right-singular vectors v j through
the eigenvalue decomposition of A> A, which is given as
2 5 32 3 2 p5 3
p 0 p1 6 0 0 p2 p1
6 30 6
7 6 30 p130 p230 7
A> A = 4 p30 2 p1
5
p2 5 40 1 05 4 0
6 5 5 5
= P DP > ,
p1 p2 p1 0 0 0 p1 p2 p1
30 5 6 6 6 6
(4.83)
and we obtain the right-singular vectors as the columns of P so that
2 5 3
p 0 p1
6 302 p1
6
7
p2 5 .
V = P = 4 p30 5 6
(4.84)
p1 p2 p1
30 5 6

Step 2: Singular-value matrix.


As the singular values i are the square roots of the eigenvalues of

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


126 Matrix Decompositions

A> A we obtain them straight from p D . Since rk(A) = 2, there are only
two nonzero singular values: 1 = 6 and 2 = 1. The singular value
matrix must be the same size as A, and we obtain
p
6 0 0
⌃= . (4.85)
0 1 0
Step 3: Left-singular vectors as the normalized image of the right-
singular vectors.
We find the left-singular vectors by computing the image of the right-
singular vectors under A and normalizing them by dividing them by their
corresponding singular value. We obtain
2 5 3
 p " #
1 1 1 0 1 6 p30 p1
2 7
u1 = Av 1 = p 4 30 5 = 5
p2
, (4.86)
1 6 2 1 0 1 5
p
30
2 " # 3
 0
1 1 1 0 1 4 p1 5 p2
u2 = Av 2 = = p1
5
, (4.87)
2 1 2 1 0 p25 5
5

1 1 2
U = [u1 , u2 ] = p . (4.88)
5 2 1
Note that on a computer the approach illustrated here has poor numerical
behavior, and the SVD of A is normally computed without resorting to the
eigenvalue decomposition of A> A.

4.5.3 Eigenvalue Decomposition vs. Singular Value Decomposition


Let us consider the eigendecomposition A = P DP 1 and the SVD A =
U ⌃V > and review the core elements of the past sections.
The SVD always exists for any matrix Rm⇥n . The eigendecomposition is
only defined for square matrices Rn⇥n and only exists if we can find a
basis of eigenvectors of Rn .
The vectors in the eigendecomposition matrix P are not necessarily
orthogonal, i.e., the change of basis is not a simple rotation and scaling.
On the other hand, the vectors in the matrices U and V in the SVD are
orthonormal, so they do represent rotations.
Both the eigendecomposition and the SVD are compositions of three
linear mappings:
1. Change of basis in the domain
2. Independent scaling of each new basis vector and mapping from do-
main to codomain
3. Change of basis in the codomain

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.5 Singular Value Decomposition 127

Figure 4.10 Movie

Chandra
Beatrix
ratings of three
Ali people for four
movies and its SVD
2 3 2 3
Star Wars 5 4 1 0.6710 0.0236 0.4647 0.5774 decomposition.
6 7
Blade Runner 66 5 5 0 7
7=6
6 0.7197 0.2054 0.4759 0.4619 7
7
Amelie 4 0 0 5 5 4 0.0939 0.7705 0.5268 0.3464 5
Delicatessen 1 0 4 0.1515 0.6030 0.5293 0.5774
2 3
9.6438 0 0
6 0 6.3639 0 7
6 7
4 0 0 0.7056 5
0 0 0
2 3
6 0.7367 0.6515 0.1811 7
4 0.0852 0.1762 0.9807 5
0.6708 0.7379 0.0743

A key difference between the eigendecomposition and the SVD is that


in the SVD, domain and codomain can be vector spaces of different
dimensions.
In the SVD, the left- and right-singular vector matrices U and V are
generally not inverse of each other (they perform basis changes in dif-
ferent vector spaces). In the eigendecomposition, the basis change ma-
trices P and P 1 are inverses of each other.
In the SVD, the entries in the diagonal matrix ⌃ are all real and non-
negative, which is not generally true for the diagonal matrix in the
eigendecomposition.
The SVD and the eigendecomposition are closely related through their
projections
– The left-singular vectors of A are eigenvectors of AA>
– The right-singular vectors of A are eigenvectors of A> A.
– The nonzero singular values of A are the square roots of the nonzero
eigenvalues of AA> and are equal to the nonzero eigenvalues of
A> A.
For symmetric matrices A 2 Rn⇥n , the eigenvalue decomposition and
the SVD are one and the same, which follows from the spectral theo-
rem 4.15.

Example 4.14 (Finding Structure in Movie Ratings and Consumers)


Let us add a practical interpretation of the SVD by analyzing data on
people and their preferred movies. Consider three viewers (Ali, Beatrix,
Chandra) rating four different movies (Star Wars, Blade Runner, Amelie,
Delicatessen). Their ratings are values between 0 (worst) and 5 (best) and
encoded in a data matrix A 2 R4⇥3 as shown in Figure 4.10. Each row

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


128 Matrix Decompositions

represents a movie and each column a user. Thus, the column vectors of
movie ratings, one for each viewer, are xAli , xBeatrix , xChandra .
Factoring A using the SVD offers us a way to capture the relationships
of how people rate movies, and especially if there is a structure linking
which people like which movies. Applying the SVD to our data matrix A
makes a number of assumptions:
1. All viewers rate movies consistently using the same linear mapping.
2. There are no errors or noise in the ratings.
3. We interpret the left-singular vectors ui as stereotypical movies and
the right-singular vectors v j as stereotypical viewers.
We then make the assumption that any viewer’s specific movie preferences
can be expressed as a linear combination of the v j . Similarly, any movie’s
like-ability can be expressed as a linear combination of the ui . Therefore,
a vector in the domain of the SVD can be interpreted as a viewer in the
“space” of stereotypical viewers, and a vector in the codomain of the SVD
These two “spaces” correspondingly as a movie in the “space” of stereotypical movies. Let us
are only inspect the SVD of our movie-user matrix. The first left-singular vector u1
meaningfully
spanned by the
has large absolute values for the two science fiction movies and a large
respective viewer first singular value (red shading in Figure 4.10). Thus, this groups a type
and movie data if of users with a specific set of movies (science fiction theme). Similarly, the
the data itself covers
a sufficient diversity
first right-singular v 1 shows large absolute values for Ali and Beatrix, who
of viewers and give high ratings to science fiction movies (green shading in Figure 4.10).
movies. This suggests that v 1 reflects the notion of a science fiction lover.
Similarly, u2 , seems to capture a French art house film theme, and v 2
indicates that Chandra is close to an idealized lover of such movies. An
idealized science fiction lover is a purist and only loves science fiction
movies, so a science fiction lover v 1 gives a rating of zero to everything
but science fiction themed – this logic is implied the diagonal substructure
for the singular value matrix ⌃. A specific movie is therefore represented
by how it decomposes (linearly) into its stereotypical movies. Likewise, a
person would be represented by how they decompose (via linear combi-
nation) into movie themes.

It is worth, to briefly discuss SVD terminology and conventions, as there


are different versions used in the literature. The mathematics remains in-
variant to these differences, but these differences can be confusing.

For convenience in notation and abstraction, we use an SVD notation


where the SVD is described as having two square left- and right-singular
vector matrices, but a non-square singular value matrix. Our defini-
full SVD tion (4.64) for the SVD is sometimes called the full SVD.
Some authors define the SVD a bit differently and focus on square sin-

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.6 Matrix Approximation 129

gular matrices. Then, for A 2 Rm⇥n and m > n,


A = U ⌃ V>. (4.89)
m⇥n m⇥n n⇥n n⇥n

Sometimes this formulation is called the reduced SVD (e.g., Datta (2010)) reduced SVD
or the SVD (e.g., Press et al. (2007)). This alternative format changes
merely how the matrices are constructed but leaves the mathematical
structure of the SVD unchanged. The convenience of this alternative
formulation is that ⌃ is diagonal, as in the eigenvalue decomposition.
In Section 4.6, we will learn about matrix approximation techniques
using the SVD, which is also called the truncated SVD. truncated SVD
It is possible to define the SVD of a rank-r matrix A so that U is an
m ⇥ r matrix, ⌃ a diagonal matrix r ⇥ r, and V an r ⇥ n matrix.
This construction is very similar to our definition, and ensures that the
diagonal matrix ⌃ has only nonzero entries along the diagonal. The
main convenience of this alternative notation is that ⌃ is diagonal, as
in the eigenvalue decomposition.
A restriction that the SVD for A only applies to m ⇥ n matrices with
m > n is practically unnecessary. When m < n, the SVD decomposition
will yield ⌃ with more zero columns than rows and, consequently, the
singular values m+1 , . . . , n are 0.
The SVD is used in a variety of applications in machine learning from
least-squares problems in curve fitting to solving systems of linear equa-
tions. These applications harness various important properties of the SVD,
its relation to the rank of a matrix, and its ability to approximate matrices
of a given rank with lower-rank matrices. Substituting a matrix with its
SVD has often the advantage of making calculation more robust to nu-
merical rounding errors. As we will explore in the next section, the SVD’s
ability to approximate matrices with “simpler” matrices in a principled
manner opens up machine learning applications ranging from dimension-
ality reduction and topic modeling to data compression and clustering.

4.6 Matrix Approximation


We considered the SVD as a way to factorize A = U ⌃V > 2 Rm⇥n into
the product of three matrices, where U 2 Rm⇥m and V 2 Rn⇥n are or-
thogonal and ⌃ contains the singular values on its main diagonal. Instead
of doing the full SVD factorization, we will now investigate how the SVD
allows us to represent a matrix A as a sum of simpler (low-rank) matrices
Ai , which lends itself to a matrix approximation scheme that is cheaper
to compute than the full SVD.
We construct a rank-1 matrix Ai 2 Rm⇥n as
Ai := ui v >
i , (4.90)
which is formed by the outer product of the ith orthogonal column vector

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


130 Matrix Decompositions
Figure 4.11 Image
processing with the
SVD. (a) The
original grayscale
image is a
1, 432 ⇥ 1, 910
matrix of values
between 0 (black) (a) Original image A. (b) A1 , 1 ⇡ 228, 052. (c) A2 , 2 ⇡ 40, 647.
and 1 (white).
(b)–(f) Rank-1
matrices
A1 , . . . , A5 and
their corresponding
singular values
1 , . . . , 5 . The
grid-like structure of
each rank-1 matrix (d) A3 , ⇡ 26, 125. (e) A4 , ⇡ 20, 232. (f) A5 , ⇡ 15, 436.
3 4 5
is imposed by the
outer-product of the
left and
right-singular
vectors. of U and V . Figure 4.11 shows an image of Stonehenge, which can be
represented by a matrix A 2 R1432⇥1910 , and some outer products Ai , as
defined in (4.90).
A matrix A 2 Rm⇥n of rank r can be written as a sum of rank-1 matrices
Ai so that
r
X r
X
A= >
i ui v i = i Ai , (4.91)
i=1 i=1

where the outer-product matrices Ai are weighted by the ith singular


value i . We can see why (4.91) holds: The diagonal structure of the
singular value matrix ⌃ multiplies only matching left- and right-singular
vectors ui v >
i and scales them by the corresponding singular value i . All
terms ⌃ij ui v >
j vanish for i 6= j because ⌃ is a diagonal matrix. Any terms
i > r vanish because the corresponding singular values are 0.
In (4.90), we introduced rank-1 matrices Ai . We summed up the r in-
dividual rank-1 matrices to obtain a rank-r matrix A; see (4.91). If the
sum does not run over all matrices Ai , i = 1, . . . , r, but only up to an
rank-k intermediate value k < r, we obtain a rank-k approximation
approximation
k
X k
X
b
A(k) := >
i ui v i = i Ai (4.92)
i=1 i=1

b
of A with rk(A(k)) = k . Figure 4.12 shows low-rank approximations
b
A(k) of an original image A of Stonehenge. The shape of the rocks be-
comes increasingly visible and clearly recognizable in the rank-5 approx-
imation. While the original image requires 1, 432 · 1, 910 = 2, 735, 120
numbers, the rank-5 approximation requires us only to store the five sin-
gular values and the five left- and right-singular vectors (1, 432 and 1, 910-

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.6 Matrix Approximation 131
Figure 4.12 Image
reconstruction with
the SVD. (a)
Original image.
(b)–(f) Image
reconstruction using
the low-rank
(a) Original image A. b
(b) Rank-1 approximation A(1).(c) b
Rank-2 approximation A(2). approximation of
the SVD, where the
rank-k
approximation is
b
given by A(k) =
Pk
i=1 i Ai .

b
(d) Rank-3 approximation A(3).(e) b
Rank-4 approximation A(4).(f) b
Rank-5 approximation A(5).

dimensional each) for a total of 5 · (1, 432 + 1, 910 + 1) = 16, 715 numbers
– just above 0.6% of the original.
To measure the difference (error) between A and its rank-k approxima-
b
tion A(k) , we need the notion of a norm. In Section 3.1, we already used
norms on vectors that measure the length of a vector. By analogy we can
also define norms on matrices.
Definition 4.23 (Spectral Norm of a Matrix). For x 2 Rn \{0}, the spectral spectral norm
norm of a matrix A 2 Rm⇥n is defined as
kAxk2
kAk2 := max . (4.93)
x kxk2
We introduce the notation of a subscript in the matrix norm (left-hand
side), similar to the Euclidean norm for vectors (right-hand side), which
has subscript 2. The spectral norm (4.93) determines how long any vector
x can at most become when multiplied by A.
Theorem 4.24. The spectral norm of A is its largest singular value 1.

We leave the proof of this theorem as an exercise.


Eckart-Young
Theorem 4.25 (Eckart-Young Theorem (Eckart and Young, 1936)). Con- theorem
sider a matrix A 2 Rm⇥n of rank
Pk r and let>B 2 R
m⇥n
be a matrix of rank
b
k . For any k 6 r with A(k) = i=1 i ui v i it holds that
b
A(k) = argminrk(B)=k kA Bk2 , (4.94)
A b
A(k) = k+1 . (4.95)
2

The Eckart-Young theorem states explicitly how much error we intro-


duce by approximating A using a rank-k approximation. We can inter-
pret the rank-k approximation obtained with the SVD as a projection of

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


132 Matrix Decompositions

the full-rank matrix A onto a lower-dimensional space of rank-at-most-k


matrices. Of all possible projections, the SVD minimizes the error (with
respect to the spectral norm) between A and any rank-k approximation.
We can retrace some of the steps to understand why (4.95) should hold.
We observe that the difference between A A(k) b is a matrix containing
the sum of the remaining rank-1 matrices
r
X
A b
A(k) = >
i ui v i . (4.96)
i=k+1

By Theorem 4.24, we immediately obtain k+1 as the spectral norm of the


difference matrix. Let us have a closer look at (4.94). If we assume that
there is another matrix B with rk(B) 6 k , such that

kA Bk2 < A b
A(k) , (4.97)
2

then there exists an at least (n k )-dimensional null space Z ✓ Rn , such


that x 2 Z implies that Bx = 0. Then it follows that
kAxk2 = k(A B)xk2 , (4.98)
and by using a version of the Cauchy-Schwartz inequality (3.17) that en-
compasses norms of matrices, we obtain
kAxk2 6 kA Bk2 kxk2 < k+1 kxk2 . (4.99)
However, there exists a (k + 1)-dimensional subspace where kAxk2 >
k+1 kxk2 , which is spanned by the right-singular vectors v j , j 6 k + 1 of
A. Adding up dimensions of these two spaces yields a number greater than
n, as there must be a nonzero vector in both spaces. This is a contradiction
of the rank-nullity theorem (Theorem 2.24) in Section 2.7.3.
The Eckart-Young theorem implies that we can use SVD to reduce a
rank-r matrix A to a rank-k matrix A b in a principled, optimal (in the
spectral norm sense) manner. We can interpret the approximation of A by
a rank-k matrix as a form of lossy compression. Therefore, the low-rank
approximation of a matrix appears in many machine learning applications,
e.g., image processing, noise filtering, and regularization of ill-posed prob-
lems. Furthermore, it plays a key role in dimensionality reduction and
principal component analysis, as we will see in Chapter 10.

Example 4.15 (Finding Structure in Movie Ratings and Consumers


(continued))
Coming back to our movie-rating example, we can now apply the con-
cept of low-rank approximations to approximate the original data matrix.
Recall that our first singular value captures the notion of science fiction
theme in movies and science fiction lovers. Thus, by using only the first

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.6 Matrix Approximation 133

singular value term in a rank-1 decomposition of the movie-rating matrix,


we obtain the predicted ratings
2 3
0.6710
6 0.71977 ⇥ ⇤
A 1 = u1 v > 6 7 0.7367 0.6515 0.1811 (4.100a)
1 = 4
0.09395
0.1515
2 3
0.4943 0.4372 0.1215
60.5302 0.4689 0.13037
=640.0692 0.0612 0.01705 .
7 (4.100b)
0.1116 0.0987 0.0274
This first rank-1 approximation A1 is insightful: it tells us that Ali and
Beatrix like science fiction movies, such as Star Wars and Bladerunner
(entries have values > 0.4), but fails to capture the ratings of the other
movies by Chandra. This is not surprising, as Chandra’s type of movies is
not captured by the first singular value. The second singular value gives
us a better rank-1 approximation for those movie-theme lovers:
2 3
0.0236
6 0.2054 7 ⇥ ⇤
A 2 = u2 v > 6 7 0.0852 0.1762 0.9807 (4.101a)
2 = 4 5
0.7705
0.6030
2 3
0.0154 0.0042 0.0174
6 0.1338 0.0362 0.15167
=6 4 0.5019
7. (4.101b)
0.1358 0.5686 5
0.3928 0.1063 0.445
In this second rank-1 approximation A2 , we capture Chandra’s ratings
and movie types well, but not the science fiction movies. This leads us to
b
consider the rank-2 approximation A(2) , where we combine the first two
rank-1 approximations
2 3
4.7801 4.2419 1.0244
65.2252 4.7522 0.02507
b
A(2) = 1 A1 + 2 A2 = 6 7. (4.102)
40.2493 0.2743 4.9724 5
0.7495 0.2756 4.0278
b
A(2) is similar to the original movie ratings table
2 3
5 4 1
6 5 5 07
A=6 4 0 0 55 ,
7 (4.103)
1 0 4
and this suggests that we can ignore the contribution of A3 . We can in-
terpret this so that in the data table there is no evidence of a third movie-

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


134 Matrix Decompositions

Figure 4.13 A
functional Real matrices
9 Pseudo-inverse
phylogeny of 9 SVD
matrices
encountered in
Rn⇥n Rn⇥m
machine learning. Square
9 Determinant Nonsquare
9 Trace

No basis of det =
0
eigenvectors
Singular

de
Basis of
Defective

t
6=
eigenvectors

0
Non-defective
(diagonalizable)

A> A = AA> A> A 6= AA>

Normal Non-normal

A>
A
=
A
A>
9 Inverse Matrix
Symmetric =
eigenvalues 2 R
I Regular
(invertible)

Diagonal Columns are


orthogonal
eigenvectors
Positive definite
Cholesky
Identity eigenvalues > 0 Orthogonal
Rotation
matrix

theme/movie-lovers category. This also means that the entire space of


movie-themes/movie-lovers in our example is a two-dimensional space
spanned by science fiction and French art house movies and lovers.

4.7 Matrix Phylogeny


The word
“phylogenetic” In Chapters 2 and 3, we covered the basics of linear algebra and analytic
describes how we geometry. In this chapter, we looked at fundamental characteristics of ma-
capture the
trices and linear mappings. Figure 4.13 depicts the phylogenetic tree of
relationships among
individuals or relationships between different types of matrices (black arrows indicating
groups and derived “is a subset of”) and the covered operations we can perform on them (in
from the Greek blue). We consider all real matrices A 2 Rn⇥m . For non-square matrices
words for “tribe”
(where n 6= m), the SVD always exists, as we saw in this chapter. Focus-
and “source”.
ing on square matrices A 2 Rn⇥n , the determinant informs us whether a
square matrix possesses an inverse matrix, i.e., whether it belongs to the
class of regular, invertible matrices. If the square n ⇥ n matrix possesses n
linearly independent eigenvectors, then the matrix is non-defective and an

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


4.8 Further Reading 135

eigendecomposition exists (Theorem 4.12). We know that repeated eigen-


values may result in defective matrices, which cannot be diagonalized.
Non-singular and non-defective matrices are not the same. For exam-
ple, a rotation matrix will be invertible (determinant is nonzero) but not
diagonalizable in the real numbers (eigenvalues are not guaranteed to be
real numbers).
We dive further into the branch of non-defective square n ⇥ n matrices.
A is normal if the condition A> A = AA> holds. Moreover, if the more
restrictive condition holds that A> A = AA> = I , then A is called or-
thogonal (see Definition 3.8). The set of orthogonal matrices is a subset of
the regular (invertible) matrices and satisfies A> = A 1 .
Normal matrices have a frequently encountered subset, the symmetric
matrices S 2 Rn⇥n , which satisfy S = S > . Symmetric matrices have only
real eigenvalues. A subset of the symmetric matrices consists of the pos-
itive definite matrices P that satisfy the condition of x> P x > 0 for all
x 2 Rn \{0}. In this case, a unique Cholesky decomposition exists (Theo-
rem 4.18). Positive definite matrices have only positive eigenvalues and
are always invertible (i.e., have a nonzero determinant).
Another subset of symmetric matrices consists of the diagonal matrices
D . Diagonal matrices are closed under multiplication and addition, but do
not necessarily form a group (this is only the case if all diagonal entries
are nonzero so that the matrix is invertible). A special diagonal matrix is
the identity matrix I .

4.8 Further Reading


Most of the content in this chapter establishes underlying mathematics
and connects them to methods for studying mappings, many of which are
at the heart of machine learning at the level of underpinning software so-
lutions and building blocks for almost all machine learning theory. Matrix
characterization using determinants, eigenspectra, and eigenspaces pro-
vides fundamental features and conditions for categorizing and analyzing
matrices. This extends to all forms of representations of data and map-
pings involving data, as well as judging the numerical stability of compu-
tational operations on such matrices (Press et al., 2007).
Determinants are fundamental tools in order to invert matrices and
compute eigenvalues “by hand”. However, for almost all but the smallest
instances, numerical computation by Gaussian elimination outperforms
determinants (Press et al., 2007). Determinants remain nevertheless a
powerful theoretical concept, e.g., to gain intuition about the orientation
of a basis based on the sign of the determinant. Eigenvectors can be used
to perform basis changes to transform data into the coordinates of mean-
ingful orthogonal, feature vectors. Similarly, matrix decomposition meth-
ods, such as the Cholesky decomposition, reappear often when we com-
pute or simulate random events (Rubinstein and Kroese, 2016). Therefore,

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


136 Matrix Decompositions

the Cholesky decomposition enables us to compute the reparametrization


trick where we want to perform continuous differentiation over random
variables, e.g., in variational autoencoders (Jimenez Rezende et al., 2014;
Kingma and Welling, 2014).
Eigendecomposition is fundamental in enabling us to extract mean-
ingful and interpretable information that characterizes linear mappings.
Therefore, the eigendecomposition underlies a general class of machine
learning algorithms called spectral methods that perform eigendecomposi-
tion of a positive-definite kernel. These spectral decomposition methods
encompass classical approaches to statistical data analysis, such as the
following:
principal component
analysis Principal component analysis (PCA (Pearson, 1901), see also Chapter 10),
in which a low-dimensional subspace, which explains most of the vari-
Fisher discriminant ability in the data, is sought.
analysis Fisher discriminant analysis, which aims to determine a separating hy-
multidimensional perplane for data classification (Mika et al., 1999).
scaling Multidimensional scaling (MDS) (Carroll and Chang, 1970).
The computational efficiency of these methods typically comes from find-
ing the best rank-k approximation to a symmetric, positive semidefinite
matrix. More contemporary examples of spectral methods have different
origins, but each of them requires the computation of the eigenvectors
Isomap and eigenvalues of a positive-definite kernel, such as Isomap (Tenenbaum
Laplacian et al., 2000), Laplacian eigenmaps (Belkin and Niyogi, 2003), Hessian
eigenmaps eigenmaps (Donoho and Grimes, 2003), and spectral clustering (Shi and
Hessian eigenmaps Malik, 2000). The core computations of these are generally underpinned
spectral clustering by low-rank matrix approximation techniques (Belabbas and Wolfe, 2009)
as we encountered here via the SVD.
The SVD allows us to discover some of the same kind of information as
the eigendecomposition. However, the SVD is more generally applicable
to non-square matrices and data tables. These matrix factorization meth-
ods become relevant whenever we want to identify heterogeneity in data
when we want to perform data compression by approximation, e.g., in-
stead of storing n⇥m values just storing (n+m)k values, or when we want
to perform data pre-processing, e.g., to decorrelate predictor variables of
a design matrix (Ormoneit et al., 2001). The SVD operates on matrices,
which we can interpret as rectangular arrays with two indices (rows and
columns). The extension of matrix-like structure to higher-dimensional
arrays are called tensors. It turns out that the SVD is the special case of
a more general family of decompositions that operate on such tensors
(Kolda and Bader, 2009). SVD-like operations and low-rank approxima-
Tucker tions on tensors are, for example, the Tucker decomposition (Tucker, 1966)
decomposition or the CP decomposition (Carroll and Chang, 1970).
CP decomposition The SVD low-rank approximation is frequently used in machine learn-
ing for computational efficiency reasons. This is because it reduces the

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Exercises 137

amount of memory and operations with nonzero multiplications we need


to perform on potentially very large matrices of data (Trefethen and Bau III,
1997). Moreover, low-rank approximations are used to operate on ma-
trices that may contain missing values as well as for purposes of lossy
compression and dimensionality reduction (Moonen and De Moor, 1995;
Markovsky, 2011).

Exercises
4.1 Compute the determinant using the Laplace expansion (using the first row)
and the Sarrus Rule for
2 3
1 3 5
A=4 2 4 6 5.
0 2 4
4.2 Compute the following determinant efficiently:
2 3
2 0 1 2 0
62 1 0 1 17
6 7
60 27
6 1 2 1 7.
4 2 0 2 1 25
2 0 0 1 1
 
1 0 2 2
4.3 Compute the eigenspaces of , .
1 1 2 1
4.4 Compute all eigenspaces of
2 3
0 1 1 1
6 1 1 2 37
A=6
42
7.
1 0 05
1 1 1 0
4.5 Diagonalizability of a matrix is unrelated to its invertibility. Determine for
the following four matrices whether they are diagonalizable and/or invert-
ible
   
1 0 1 0 1 1 0 1
, , , .
0 1 0 0 0 1 0 0
4.6 Compute the eigenspaces of the following transformation matrices. Are they
diagonalizable?
a.
2 3
2 3 0
A = 41 4 35
0 0 1
b.
2 3
1 1 0 0
60 0 0 07
A=6
40
7
0 0 05
0 0 0 0

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


138 Matrix Decompositions

4.7 Are the following matrices diagonalizable? If yes, determine their diagonal
form and a basis with respect to which the transformation matrices are di-
agonal. If no, give reasons why they are not diagonalizable.
a.

0 1
A=
8 4

b.
2 3
1 1 1
A = 41 1 15
1 1 1
c.
2 3
5 4 2 1
6 0 1 1 17
A=4 6 7
1 1 3 0 5
1 1 1 2

d.
2 3
5 6 6
A=4 1 4 2 5
3 6 4

4.8 Find the SVD of the matrix



3 2 2
A= .
2 3 2

4.9 Find the singular value decomposition of



2 2
A= .
1 1

4.10 Find the best rank-1 approximation of



3 2 2
A= .
2 3 2

4.11 Show that for any A 2 Rm⇥n the matrices A> A and AA> possess the
same nonzero eigenvalues.
4.12 Show that for x 6= 0 Theorem 4.24 holds, i.e., show that
kAxk2
max = 1,
x kxk2

where 1 is the largest singular value of A 2 Rm⇥n .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5

Vector Calculus

Many algorithms in machine learning optimize an objective function with


respect to a set of desired model parameters that control how well a model
explains the data: Finding good parameters can be phrased as an opti-
mization problem (see Sections 8.2 and 8.3). Examples include: (i) lin-
ear regression (see Chapter 9), where we look at curve-fitting problems
and optimize linear weight parameters to maximize the likelihood; (ii)
neural-network auto-encoders for dimensionality reduction and data com-
pression, where the parameters are the weights and biases of each layer,
and where we minimize a reconstruction error by repeated application of
the chain rule; and (iii) Gaussian mixture models (see Chapter 11) for
modeling data distributions, where we optimize the location and shape
parameters of each mixture component to maximize the likelihood of the
model. Figure 5.1 illustrates some of these problems, which we typically
solve by using optimization algorithms that exploit gradient information
(Section 7.1). Figure 5.2 gives an overview of how concepts in this chap-
ter are related and how they are connected to other chapters of the book.

Central to this chapter is the concept of a function. A function f is


a quantity that relates two quantities to each other. In this book, these
quantities are typically inputs x 2 RD and targets (function values) f (x),
which we assume are real-valued if not stated otherwise. Here RD is the
domain of f , and the function values f (x) are the image/codomain of f . domain
image/codomain
10 Figure 5.1 Vector
4 Training data calculus plays a
MLE
5 central role in (a)
2
regression (curve
0 fitting) and (b)
x2

0
y

density estimation,
2
5
i.e., modeling data
distributions.
4
10
4 2 0 2 4 10 5 0 5 10
x x1

(a) Regression problem: Find parameters, (b) Density estimation with a Gaussian mixture
such that the curve explains the observations model: Find means and covariances, such that
(crosses) well. the data (dots) can be explained well.

139
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
140 Vector Calculus

Figure 5.2 A mind Chapter 9


map of the concepts Difference quotient
Regression
introduced in this
chapter, along with
n

defines
when they are used i
ed
in other parts of the us
book.

Chapter 7 used in used in Chapter 10


Optimization Partial derivatives Dimensionality
reduction

collected in
us
ed
in

us
Chapter 6 used in Jacobian Chapter 11

ed
Probability Hessian Density estimation

in
used in

Taylor series Chapter 12


Classification

Section 2.7.3 provides much more detailed discussion in the context of


linear functions. We often write
f : RD ! R (5.1a)
x 7! f (x) (5.1b)
to specify a function, where (5.1a) specifies that f is a mapping from
RD to R and (5.1b) specifies the explicit assignment of an input x to
a function value f (x). A function f assigns every input x exactly one
function value f (x).

Example 5.1
Recall the dot product as a special case of an inner product (Section 3.2).
In the previous notation, the function f (x) = x> x, x 2 R2 , would be
specified as
f : R2 ! R (5.2a)
x 7! x21 + x22 . (5.2b)

In this chapter, we will discuss how to compute gradients of functions,


which is often essential to facilitate learning in machine learning models
since the gradient points in the direction of steepest ascent. Therefore,

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.1 Differentiation of Univariate Functions 141

Figure 5.3 The


y f (x) average incline of a
function f between
x0 and x0 + x is
the incline of the
secant (blue)
through f (x0 ) and
f (x0 + x) f (x0 + x) and
given by y/ x.
y
f (x0 )
x

vector calculus is one of the fundamental mathematical tools we need in


machine learning. Throughout this book, we assume that functions are
differentiable. With some additional technical definitions, which we do
not cover here, many of the approaches presented can be extended to
sub-differentials (functions that are continuous but not differentiable at
certain points). We will look at an extension to the case of functions with
constraints in Chapter 7.

5.1 Differentiation of Univariate Functions


In the following, we briefly revisit differentiation of a univariate function,
which may be familiar from high school mathematics. We start with the
difference quotient of a univariate function y = f (x), x, y 2 R, which we
will subsequently use to define derivatives.
Definition 5.1 (Difference Quotient). The difference quotient difference quotient

y f (x + x) f (x)
:= (5.3)
x x
computes the slope of the secant line through two points on the graph of
f . In Figure 5.3, these are the points with x-coordinates x0 and x0 + x.
The difference quotient can also be considered the average slope of f
between x and x + x if we assume f to be a linear function. In the limit
for x ! 0, we obtain the tangent of f at x, if f is differentiable. The
tangent is then the derivative of f at x.
Definition 5.2 (Derivative). More formally, for h > 0 the derivative of f derivative
at x is defined as the limit
df f (x + h) f (x)
:= lim , (5.4)
dx h!0 h
and the secant in Figure 5.3 becomes a tangent.
The derivative of f points in the direction of steepest ascent of f .

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


142 Vector Calculus

Example 5.2 (Derivative of a Polynomial)


We want to compute the derivative of f (x) = xn , n 2 N. We may already
know that the answer will be nxn 1 , but we want to derive this result
using the definition of the derivative as the limit of the difference quotient.
Using the definition of the derivative in (5.4), we obtain
df f (x + h) f (x)
= lim (5.5a)
dx h!0 h
(x + h)n xn
= lim (5.5b)
Pn hn n i i
h!0

x h xn
= lim i=0 i . (5.5c)
h!0 h
We see that xn = n
0
xn 0 h0 . By starting the sum at 1, the xn -term cancels,
and we obtain
Pn n
df i=1 x n i hi
= lim i
(5.6a)
dx h!0 h
!
Xn
n n i i 1
= lim x h (5.6b)
h!0
i=1
i
! n
!
n n 1 X n n i i 1
= lim x + x h (5.6c)
h!0 1 i=2
i
| {z }
!0 as h!0
n!
= xn 1
= nxn 1
. (5.6d)
1!(n 1)!

5.1.1 Taylor Series


The Taylor series is a representation of a function f as an infinite sum of
terms. These terms are determined using derivatives of f evaluated at x0 .

Taylor polynomial Definition 5.3 (Taylor Polynomial). The Taylor polynomial of degree n of
We define t0 := 1 f : R ! R at x0 is defined as
for all t 2 R.
n
X f (k) (x0 )
Tn (x) := (x x0 ) k , (5.7)
k=0
k!

where f (k) (x0 ) is the k th derivative of f at x0 (which we assume exists)


(k)
and f k!(x0 ) are the coefficients of the polynomial.

Definition 5.4 (Taylor Series). For a smooth function f 2 C 1 , f : R ! R,


Taylor series the Taylor series of f at x0 is defined as

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.1 Differentiation of Univariate Functions 143
1
X f (k) (x0 )
T1 (x) = (x x0 ) k . (5.8)
k=0
k!

For x0 = 0, we obtain the Maclaurin series as a special instance of the f 2 C 1 means that
Taylor series. If f (x) = T1 (x), then f is called analytic. f is continuously
differentiable
infinitely many
Remark. In general, a Taylor polynomial of degree n is an approximation times.
of a function, which does not need to be a polynomial. The Taylor poly- Maclaurin series
nomial is similar to f in a neighborhood around x0 . However, a Taylor analytic
polynomial of degree n is an exact representation of a polynomial f of
degree k 6 n since all derivatives f (i) , i > k vanish. }

Example 5.3 (Taylor Polynomial)


We consider the polynomial
f (x) = x4 (5.9)
and seek the Taylor polynomial T6 , evaluated at x0 = 1. We start by com-
puting the coefficients f (k) (1) for k = 0, . . . , 6:
f (1) = 1 (5.10)
f 0 (1) = 4 (5.11)
f 00 (1) = 12 (5.12)
f (3) (1) = 24 (5.13)
f (4)
(1) = 24 (5.14)
f (5)
(1) = 0 (5.15)
f (6)
(1) = 0 (5.16)
Therefore, the desired Taylor polynomial is
6
X f (k) (x0 )
T6 (x) = (x x0 ) k (5.17a)
k=0
k!
= 1 + 4(x 1) + 6(x 1)2 + 4(x 1)3 + (x 1)4 + 0 . (5.17b)
Multiplying out and re-arranging yields
T6 (x) = (1 4 + 6 4 + 1) + x(4 12 + 12 4)
+ x2 (6 12 + 6) + x3 (4 4) + x4 (5.18a)
= x4 = f (x) , (5.18b)
i.e., we obtain an exact representation of the original function.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


144 Vector Calculus

Figure 5.4 Taylor


polynomials. The f
original function T0
f (x) = 4
T1
sin(x) + cos(x) T5
(black, solid) is 2 T10
approximated by

y
Taylor polynomials
(dashed) around 0
x0 = 0.
Higher-order Taylor
polynomials 2
approximate the
function f better 4 2 0 2 4
and more globally. x
T10 is already
similar to f in
[ 4, 4].
Example 5.4 (Taylor Series)
Consider the function in Figure 5.4 given by
f (x) = sin(x) + cos(x) 2 C 1 . (5.19)
We seek a Taylor series expansion of f at x0 = 0, which is the Maclaurin
series expansion of f . We obtain the following derivatives:
f (0) = sin(0) + cos(0) = 1 (5.20)
f 0 (0) = cos(0) sin(0) = 1 (5.21)
f 00 (0) = sin(0) cos(0) = 1 (5.22)
f (3)
(0) = cos(0) + sin(0) = 1 (5.23)
f (4)
(0) = sin(0) + cos(0) = f (0) = 1 (5.24)
..
.
We can see a pattern here: The coefficients in our Taylor series are only
±1 (since sin(0) = 0), each of which occurs twice before switching to the
other one. Furthermore, f (k+4) (0) = f (k) (0).
Therefore, the full Taylor series expansion of f at x0 = 0 is given by
1
X f (k) (x0 )
T1 (x) = (x x0 ) k (5.25a)
k=0
k!
1 2 1 3 1 1
=1+x x x + x4 + x5 · · · (5.25b)
2! 3! 4! 5!
1 2 1 1 3 1
=1 x + x4 ⌥ · · · + x x + x5 ⌥ · · · (5.25c)
2! 4! 3! 5!
X1 X1
1 1
= ( 1)k x2k + ( 1)k x2k+1 (5.25d)
k=0
(2k)! k=0
(2k + 1)!
= cos(x) + sin(x) , (5.25e)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.1 Differentiation of Univariate Functions 145

where we used the power series representations power series


representation
1
X 1 2k
cos(x) = ( 1)k x , (5.26)
k=0
(2k)!
X1
1
sin(x) = ( 1)k x2k+1 . (5.27)
k=0
(2k + 1)!
Figure 5.4 shows the corresponding first Taylor polynomials Tn for n =
0, 1, 5, 10.

Remark. A Taylor series is a special case of a power series


1
X
f (x) = ak (x c)k (5.28)
k=0

where ak are coefficients and c is a constant, which has the special form
in Definition 5.4. }

5.1.2 Differentiation Rules


In the following, we briefly state basic differentiation rules, where we
denote the derivative of f by f 0 .

Product rule: (f (x)g(x))0 = f 0 (x)g(x) + f (x)g 0 (x) (5.29)


✓ ◆0
f (x) f 0 (x)g(x) f (x)g 0 (x)
Quotient rule: = (5.30)
g(x) (g(x))2
Sum rule: (f (x) + g(x))0 = f 0 (x) + g 0 (x) (5.31)
0
Chain rule: 0 0
g(f (x)) = (g f ) (x) = g (f (x))f (x) 0
(5.32)
Here, g f denotes function composition x 7! f (x) 7! g(f (x)).

Example 5.5 (Chain rule)


Let us compute the derivative of the function h(x) = (2x + 1)4 using the
chain rule. With
h(x) = (2x + 1)4 = g(f (x)) , (5.33)
f (x) = 2x + 1 , (5.34)
g(f ) = f 4 , (5.35)
we obtain the derivatives of f and g as
f 0 (x) = 2 , (5.36)
g 0 (f ) = 4f 3 , (5.37)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


146 Vector Calculus

such that the derivative of h is given as


(5.34)
h0 (x) = g 0 (f )f 0 (x) = (4f 3 ) · 2 = 4(2x + 1)3 · 2 = 8(2x + 1)3 , (5.38)
where we used the chain rule (5.32) and substituted the definition of f
in (5.34) in g 0 (f ).

5.2 Partial Differentiation and Gradients


Differentiation as discussed in Section 5.1 applies to functions f of a
scalar variable x 2 R. In the following, we consider the general case
where the function f depends on one or more variables x 2 Rn , e.g.,
f (x) = f (x1 , x2 ). The generalization of the derivative to functions of sev-
eral variables is the gradient.
We find the gradient of the function f with respect to x by varying one
variable at a time and keeping the others constant. The gradient is then
the collection of these partial derivatives.
Definition 5.5 (Partial Derivative). For a function f : Rn ! R, x 7!
partial derivative f (x), x 2 Rn of n variables x1 , . . . , xn we define the partial derivatives as
@f f (x1 + h, x2 , . . . , xn ) f (x)
= lim
@x1 h!0 h
.. (5.39)
.
@f f (x1 , . . . , xn 1 , xn + h) f (x)
= lim
@xn h!0 h
and collect them in the row vector

df @f (x) @f (x) @f (x)
rx f = gradf = = ··· 2 R1⇥n , (5.40)
dx @x1 @x2 @xn
where n is the number of variables and 1 is the dimension of the image/
range/codomain of f . Here, we defined the column vector x = [x1 , . . . , xn ]>
gradient 2 Rn . The row vector in (5.40) is called the gradient of f or the Jacobian
Jacobian and is the generalization of the derivative from Section 5.1.
Remark. This definition of the Jacobian is a special case of the general
definition of the Jacobian for vector-valued functions as the collection of
partial derivatives. We will get back to this in Section 5.3. }
We can use results
from scalar
differentiation: Each
partial derivative is
Example 5.6 (Partial Derivatives Using the Chain Rule)
a derivative with For f (x, y) = (x + 2y 3 )2 , we obtain the partial derivatives
respect to a scalar.
@f (x, y) @
= 2(x + 2y 3 ) (x + 2y 3 ) = 2(x + 2y 3 ) , (5.41)
@x @x

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.2 Partial Differentiation and Gradients 147

@f (x, y) @
= 2(x + 2y 3 ) (x + 2y 3 ) = 12(x + 2y 3 )y 2 . (5.42)
@y @y
where we used the chain rule (5.32) to compute the partial derivatives.

Remark (Gradient as a Row Vector). It is not uncommon in the literature


to define the gradient vector as a column vector, following the conven-
tion that vectors are generally column vectors. The reason why we define
the gradient vector as a row vector is twofold: First, we can consistently
generalize the gradient to vector-valued functions f : Rn ! Rm (then
the gradient becomes a matrix). Second, we can immediately apply the
multi-variate chain rule without paying attention to the dimension of the
gradient. We will discuss both points in Section 5.3. }

Example 5.7 (Gradient)


For f (x1 , x2 ) = x21 x2 + x1 x32 2 R, the partial derivatives (i.e., the deriva-
tives of f with respect to x1 and x2 ) are
@f (x1 , x2 )
= 2x1 x2 + x32 (5.43)
@x1
@f (x1 , x2 )
= x21 + 3x1 x22 (5.44)
@x2
and the gradient is then

df @f (x1 , x2 ) @f (x1 , x2 ) ⇥ ⇤
= = 2x1 x2 + x32 x21 + 3x1 x22 2 R1⇥2 .
dx @x1 @x2
(5.45)

5.2.1 Basic Rules of Partial Differentiation


Product rule:
In the multivariate case, where x 2 Rn , the basic differentiation rules that (f g)0 = f 0 g + f g 0 ,
we know from school (e.g., sum rule, product rule, chain rule; see also Sum rule:
(f + g)0 = f 0 + g 0 ,
Section 5.1.2) still apply. However, when we compute derivatives with re- Chain rule:
spect to vectors x 2 Rn we need to pay attention: Our gradients now (g(f ))0 = g 0 (f )f 0
involve vectors and matrices, and matrix multiplication is not commuta-
tive (Section 2.2.1), i.e., the order matters.
Here are the general product rule, sum rule, and chain rule:
@ @f @g
Product rule: f (x)g(x) = g(x) + f (x) (5.46)
@x @x @x
@ @f @g
Sum rule: f (x) + g(x) = + (5.47)
@x @x @x

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


148 Vector Calculus
@ @ @g @f
Chain rule: (g f )(x) = g(f (x)) = (5.48)
@x @x @f @x

This is only an Let us have a closer look at the chain rule. The chain rule (5.48) resem-
intuition, but not bles to some degree the rules for matrix multiplication where we said that
mathematically
neighboring dimensions have to match for matrix multiplication to be de-
correct since the
partial derivative is fined; see Section 2.2.1. If we go from left to right, the chain rule exhibits
not a fraction. similar properties: @f shows up in the “denominator” of the first factor
and in the “numerator” of the second factor. If we multiply the factors to-
gether, multiplication is defined, i.e., the dimensions of @f match, and @f
“cancels”, such that @g/@x remains.

5.2.2 Chain Rule


Consider a function f : R ! R of two variables x1 , x2 . Furthermore,
2

x1 (t) and x2 (t) are themselves functions of t. To compute the gradient of


f with respect to t, we need to apply the chain rule (5.48) for multivariate
functions as
" #
df h i @x1 (t) @f @x1 @f @x2
@f @f
= @x1 @x2 @x2 (t) =
@t + , (5.49)
dt @t
@x1 @t @x2 @t

where d denotes the gradient and @ partial derivatives.

Example 5.8
Consider f (x1 , x2 ) = x21 + 2x2 , where x1 = sin t and x2 = cos t, then
df @f @x1 @f @x2
= + (5.50a)
dt @x1 @t @x2 @t
@ sin t @ cos t
= 2 sin t +2 (5.50b)
@t @t
= 2 sin t cos t 2 sin t = 2 sin t(cos t 1) (5.50c)
is the corresponding derivative of f with respect to t.

If f (x1 , x2 ) is a function of x1 and x2 , where x1 (s, t) and x2 (s, t) are


themselves functions of two variables s and t, the chain rule yields the
partial derivatives

@f @f @x1 @f @x2
= + , (5.51)
@s @x1 @s @x2 @s
@f @f @x1 @f @x2
= + , (5.52)
@t @x1 @t @x2 @t

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.3 Gradients of Vector-Valued Functions 149

and the gradient is obtained by the matrix multiplication


2 3
@x1 @x1
df @f @x h i
= = @f @f 6 @s @t 7 (5.53)
@x @x 4 @x @x 5.
d(s, t) @x @(s, t) | 1 {z 2 } 2 2

@f | @s {z @t }
=
@x @x
=
@(s, t)
This compact way of writing the chain rule as a matrix multiplication only The chain rule can
makes sense if the gradient is defined as a row vector. Otherwise, we will be written as a
matrix
need to start transposing gradients for the matrix dimensions to match.
multiplication.
This may still be straightforward as long as the gradient is a vector or a
matrix; however, when the gradient becomes a tensor (we will discuss this
in the following), the transpose is no longer a triviality.
Remark (Verifying the Correctness of a Gradient Implementation). The
definition of the partial derivatives as the limit of the corresponding dif-
ference quotient (see (5.39)) can be exploited when numerically checking
the correctness of gradients in computer programs: When we compute Gradient checking
gradients and implement them, we can use finite differences to numer-
ically test our computation and implementation: We choose the value h
to be small (e.g., h = 10 4 ) and compare the finite-difference approxima-
tion from (5.39) with our (analytic) implementation of the gradient. If the
error is small, ourqgradient
P
implementation is probably correct. “Small”
(dh df )2
could mean that Pi (dhii +dfii )2 < 10 6 , where dhi is the finite-difference
i
approximation and dfi is the analytic gradient of f with respect to the ith
variable xi . }

5.3 Gradients of Vector-Valued Functions


Thus far, we discussed partial derivatives and gradients of functions f :
Rn ! R mapping to the real numbers. In the following, we will generalize
the concept of the gradient to vector-valued functions (vector fields) f :
Rn ! Rm , where n > 1 and m > 1.
For a function f : Rn ! Rm and a vector x = [x1 , . . . , xn ]> 2 Rn , the
corresponding vector of function values is given as
2 3
f1 (x)
f (x) = 4 ... 5 2 Rm .
6 7
(5.54)
fm (x)
Writing the vector-valued function in this way allows us to view a vector-
valued function f : Rn ! Rm as a vector of functions [f1 , . . . , fm ]> ,
fi : Rn ! R that map onto R. The differentiation rules for every fi are
exactly the ones we discussed in Section 5.2.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


150 Vector Calculus

Therefore, the partial derivative of a vector-valued function f : Rn !


Rm with respect to xi 2 R, i = 1, . . . n, is given as the vector
2 @f1 3 2 3
@xi limh!0 f1 (x1 ,...,xi 1 ,xi +h,x
h
i+1 ,...xn ) f1 (x)

@f
= 4 ... 5 = 4 ..
6 7 6 7 m
. 52R .
@xi @fm
@xi limh!0 fm (x1 ,...,xi 1 ,xi +h,x
h
i+1 ,...xn ) fm (x)

(5.55)
From (5.40), we know that the gradient of f with respect to a vector is
the row vector of the partial derivatives. In (5.55), every partial derivative
@f /@xi is a column vector. Therefore, we obtain the gradient of f : Rn !
Rm with respect to x 2 Rn by collecting these partial derivatives:

df (x) @f (x) @f (x)
= ··· (5.56a)
dx @x1 @xn
2 3
@f1 (x) @f1 (x)
6 @x1 ··· @xn 7
6 7
=6 .. .. 7 2 Rm⇥n . (5.56b)
6 . . 7
4 @fm (x) @fm (x) 5
@x1 ··· @xn

Definition 5.6 (Jacobian). The collection of all first-order partial deriva-


Jacobian tives of a vector-valued function f : Rn ! Rm is called the Jacobian. The
The gradient of a Jacobian J is an m ⇥ n matrix, which we define and arrange as follows:
function 
f : Rn ! Rm is a df (x) @f (x) @f (x)
J = rx f = = ··· (5.57)
matrix of size dx @x1 @xn
m ⇥ n. 2 3
@f1 (x) @f1 (x)
6 @x ···
6 1 @xn 7 7
=6
6 .
.. .. 7
(5.58)
. 7,
6 7
4 @fm (x) @fm (x) 5
···
@x @xn
2 31
x1
6 .. 7 @fi
x = 4 . 5 , J(i, j) = . (5.59)
@xj
xn
As a special case of (5.58), a function Pf : Rn ! R1 , which maps a
n
vector x 2 R onto a scalar (e.g., f (x) = i=1 xi ), possesses a Jacobian
n

that is a row vector (matrix of dimension 1 ⇥ n); see (5.40).


numerator layout Remark. In this book, we use the numerator layout of the derivative, i.e.,
the derivative df /dx of f 2 Rm with respect to x 2 Rn is an m ⇥
n matrix, where the elements of f define the rows and the elements of
x define the columns of the corresponding Jacobian; see (5.58). There

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.3 Gradients of Vector-Valued Functions 151

Figure 5.5 The


determinant of the
f (·) Jacobian of f can
b2 c1 c2 be used to compute
the magnifier
between the blue
b1
and orange area.

exists also the denominator layout, which is the transpose of the numerator denominator layout
layout. In this book, we will use the numerator layout. }
We will see how the Jacobian is used in the change-of-variable method
for probability distributions in Section 6.7. The amount of scaling due to
the transformation of a variable is provided by the determinant.
In Section 4.1, we saw that the determinant can be used to compute
the area of a parallelogram. If we are given two vectors b1 = [1, 0]> ,
b2 = [0, 1]> as the sides of the unit square (blue; see Figure 5.5), the area
of this square is
✓ ◆
1 0
det = 1. (5.60)
0 1
If we take a parallelogram with the sides c1 = [ 2, 1]> , c2 = [1, 1]>
(orange in Figure 5.5), its area is given as the absolute value of the deter-
minant (see Section 4.1)
✓ ◆
2 1
det = | 3| = 3 , (5.61)
1 1
i.e., the area of this is exactly three times the area of the unit square.
We can find this scaling factor by finding a mapping that transforms the
unit square into the other square. In linear algebra terms, we effectively
perform a variable transformation from (b1 , b2 ) to (c1 , c2 ). In our case,
the mapping is linear and the absolute value of the determinant of this
mapping gives us exactly the scaling factor we are looking for.
We will describe two approaches to identify this mapping. First, we ex-
ploit that the mapping is linear so that we can use the tools from Chapter 2
to identify this mapping. Second, we will find the mapping using partial
derivatives using the tools we have been discussing in this chapter.
Approach 1 To get started with the linear algebra approach, we
identify both {b1 , b2 } and {c1 , c2 } as bases of R2 (see Section 2.6.1 for a
recap). What we effectively perform is a change of basis from (b1 , b2 ) to
(c1 , c2 ), and we are looking for the transformation matrix that implements
the basis change. Using results from Section 2.7.2, we identify the desired
basis change matrix as

2 1
J= , (5.62)
1 1
such that J b1 = c1 and J b2 = c2 . The absolute value of the determi-

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


152 Vector Calculus

nant of J , which yields the scaling factor we are looking for, is given as
|det(J )| = 3, i.e., the area of the square spanned by (c1 , c2 ) is three times
greater than the area spanned by (b1 , b2 ).
Approach 2 The linear algebra approach works for linear trans-
formations; for nonlinear transformations (which become relevant in Sec-
tion 6.7), we follow a more general approach using partial derivatives.
For this approach, we consider a function f : R2 ! R2 that performs a
variable transformation. In our example, f maps the coordinate represen-
tation of any vector x 2 R2 with respect to (b1 , b2 ) onto the coordinate
representation y 2 R2 with respect to (c1 , c2 ). We want to identify the
mapping so that we can compute how an area (or volume) changes when
it is being transformed by f . For this, we need to find out how f (x)
changes if we modify x a bit. This question is exactly answered by the
Jacobian matrix df dx
2 R2⇥2 . Since we can write
y1 = 2x1 + x2 (5.63)
y2 = x 1 + x 2 (5.64)
we obtain the functional relationship between x and y , which allows us
to get the partial derivatives
@y1 @y1 @y2 @y2
= 2, = 1, = 1, =1 (5.65)
@x1 @x2 @x1 @x2
and compose the Jacobian as
2 @y @y1 3 
1
6 1 @x2 7 2 1
J = 4 @x
@y @y2 5 = 1 1 . (5.66)
2
@x1 @x2
Geometrically, the The Jacobian represents the coordinate transformation we are looking
Jacobian for. It is exact if the coordinate transformation is linear (as in our case),
determinant gives
and (5.66) recovers exactly the basis change matrix in (5.62). If the co-
the magnification/
scaling factor when ordinate transformation is nonlinear, the Jacobian approximates this non-
we transform an linear transformation locally with a linear one. The absolute value of the
area or volume. Jacobian determinant |det(J )| is the factor by which areas or volumes are
Jacobian
scaled when coordinates are transformed. Our case yields |det(J )| = 3.
determinant
The Jacobian determinant and variable transformations will become
relevant in Section 6.7 when we transform random variables and prob-
Figure 5.6 ability distributions. These transformations are extremely relevant in ma-
Dimensionality of chine learning in the context of training deep neural networks using the
(partial) derivatives.
reparametrization trick, also called infinite perturbation analysis.
x In this chapter, we encountered derivatives of functions. Figure 5.6 sum-
f (x) marizes the dimensions of those derivatives. If f : R ! R the gradient is
@f simply a scalar (top-left entry). For f : RD ! R the gradient is a 1 ⇥ D
@x row vector (top-right entry). For f : R ! RE , the gradient is an E ⇥ 1
column vector, and for f : RD ! RE the gradient is an E ⇥ D matrix.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.3 Gradients of Vector-Valued Functions 153

Example 5.9 (Gradient of a Vector-Valued Function)


We are given
f (x) = Ax , f (x) 2 RM , A 2 RM ⇥N , x 2 RN .
To compute the gradient df /dx we first determine the dimension of
df /dx: Since f : RN ! RM , it follows that df /dx 2 RM ⇥N . Second,
to compute the gradient we determine the partial derivatives of f with
respect to every xj :
N
X @fi
fi (x) = Aij xj =) = Aij (5.67)
j=1
@xj

We collect the partial derivatives in the Jacobian and obtain the gradient
2 @f1 @f1 3 2 3
@x1
· · · @x N
A11 · · · A1N
df
= 4 ... .. 7 = 6 .. .. 7 = A 2 RM ⇥N . (5.68)
6
. 5 4 . . 5
dx @fM @fM
@x 1
· · · @x N
AM 1 · · · AM N

Example 5.10 (Chain Rule)


Consider the function h : R ! R, h(t) = (f g)(t) with
f :R !R2
(5.69)
g : R ! R2 (5.70)
f (x) = exp(x1 x22 ) , (5.71)
 
x t cos t
x = 1 = g(t) = (5.72)
x2 t sin t
and compute the gradient of h with respect to t. Since f : R2 ! R and
g : R ! R2 we note that
@f @g
2 R1⇥2 , 2 R2⇥1 . (5.73)
@x @t
The desired gradient is computed by applying the chain rule:
2 3
 @x1
dh @f @x @f @f 6 @t 7
= = 4 5 (5.74a)
dt @x @t @x1 @x2 @x2
@t 
⇥ ⇤ cos t t sin t
2 2 2
= exp(x1 x2 )x2 2 exp(x1 x2 )x1 x2 (5.74b)
sin t + t cos t
= exp(x1 x22 ) x22 (cos t t sin t) + 2x1 x2 (sin t + t cos t) , (5.74c)
where x1 = t cos t and x2 = t sin t; see (5.72).

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


154 Vector Calculus

Example 5.11 (Gradient of a Least-Squares Loss in a Linear Model)


We will discuss this Let us consider the linear model
model in much
more detail in y= ✓, (5.75)
Chapter 9 in the
context of linear where ✓ 2 RD is a parameter vector, 2 RN ⇥D are input features and
regression, where
we need derivatives
y 2 R are the corresponding observations. We define the functions
N

of the least-squares
loss L with respect
L(e) := kek2 , (5.76)
to the parameters ✓. e(✓) := y ✓. (5.77)
We seek @L@✓
, and we will use the chain rule for this purpose. L is called a
least-squares loss least-squares loss function.
Before we start our calculation, we determine the dimensionality of the
gradient as
@L
2 R1⇥D . (5.78)
@✓
The chain rule allows us to compute the gradient as
@L @L @e
= , (5.79)
@✓ @e @✓
dLdtheta = where the dth element is given by
np.einsum(
’n,nd’, XN
@L @L @e
dLde,dedtheta) [1, d] = [n] [n, d] . (5.80)
@✓ n=1
@e @✓
We know that kek2 = e> e (see Section 3.2) and determine
@L
= 2e> 2 R1⇥N . (5.81)
@e
Furthermore, we obtain
@e
= 2 RN ⇥D , (5.82)
@✓
such that our desired derivative is
@L (5.77)
= 2e> = 2(y > ✓ > > ) |{z} 2 R1⇥D . (5.83)
@✓ | {z }
1⇥N N ⇥D

Remark. We would have obtained the same result without using the chain
rule by immediately looking at the function
L2 (✓) := ky ✓k2 = (y ✓)> (y ✓) . (5.84)
This approach is still practical for simple functions like L2 but becomes
impractical for deep function compositions. }

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.4 Gradients of Matrices 155
A 2 R4⇥2 x 2 R3 Figure 5.7
x1 Visualization of
x2 gradient
x3 computation of a
matrix with respect
to a vector. We are
interested in
Partial derivatives: computing the
gradient of
@A
2 R4⇥2 A 2 R4⇥2 with
@x3 dA
2 R4⇥2⇥3 respect to a vector
@A dx
2 R4⇥2 x 2 R3 . We know
@x2 collate that gradient
@A dA
2 R4⇥2⇥3 . We
2 R4⇥2 dx
@x1 follow two
equivalent
4 approaches to arrive
there: (a) collating
3 partial derivatives
into a Jacobian
2 tensor;
(a) Approach 1: We compute the partial derivative (b) flattening of the
@A @A @A
, , , each of which is a 4 ⇥ 2 matrix, and col- matrix into a vector,
@x1 @x2 @x3
late them in a 4 ⇥ 2 ⇥ 3 tensor. computing the
Jacobian matrix,
re-shaping into a
Jacobian tensor.
A 2 R4⇥2 x 2 R3
x1
x2
x3

dà dA
2 R8⇥3 2 R4⇥2⇥3
A 2 R4⇥2 Ã 2 R8 dx dx

re-shape gradient re-shape

(b) Approach 2: We re-shape (flatten) A 2 R4⇥2 into a vec-


tor à 2 R8 . Then, we compute the gradient ddx Ã
2 R8⇥3 .
We obtain the gradient tensor by re-shaping this gradient as
illustrated above.

5.4 Gradients of Matrices We can think of a


We will encounter situations where we need to take gradients of matrices tensor as a
multidimensional
with respect to vectors (or other matrices), which results in a multidimen-
array.
sional tensor. We can think of this tensor as a multidimensional array that

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


156 Vector Calculus

collects partial derivatives. For example, if we compute the gradient of an


m ⇥ n matrix A with respect to a p ⇥ q matrix B , the resulting Jacobian
would be (m⇥n)⇥(p⇥q), i.e., a four-dimensional tensor J , whose entries
are given as Jijkl = @Aij /@Bkl .
Since matrices represent linear mappings, we can exploit the fact that
there is a vector-space isomorphism (linear, invertible mapping) between
the space Rm⇥n of m ⇥ n matrices and the space Rmn of mn vectors.
Therefore, we can re-shape our matrices into vectors of lengths mn and
pq , respectively. The gradient using these mn vectors results in a Jacobian
Matrices can be of size mn ⇥ pq . Figure 5.7 visualizes both approaches. In practical ap-
transformed into plications, it is often desirable to re-shape the matrix into a vector and
vectors by stacking
continue working with this Jacobian matrix: The chain rule (5.48) boils
the columns of the
matrix down to simple matrix multiplication, whereas in the case of a Jacobian
(“flattening”). tensor, we will need to pay more attention to what dimensions we need
to sum out.

Example 5.12 (Gradient of Vectors with Respect to Matrices)


Let us consider the following example, where
f = Ax , f 2 RM , A 2 RM ⇥N , x 2 RN (5.85)
and where we seek the gradient df /dA. Let us start again by determining
the dimension of the gradient as
df
2 RM ⇥(M ⇥N ) . (5.86)
dA
By definition, the gradient is the collection of the partial derivatives:
2 @f1 3
@A
df @fi
= 4 ... 5 ,
6 7
2 R1⇥(M ⇥N ) . (5.87)
dA @fM
@A
@A

To compute the partial derivatives, it will be helpful to explicitly write out


the matrix vector multiplication:
N
X
fi = Aij xj , i = 1, . . . , M , (5.88)
j=1

and the partial derivatives are then given as


@fi
= xq . (5.89)
@Aiq
This allows us to compute the partial derivatives of fi with respect to a
row of A, which is given as
@fi
= x> 2 R1⇥1⇥N , (5.90)
@Ai,:

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.4 Gradients of Matrices 157

@fi
= 0> 2 R1⇥1⇥N (5.91)
@Ak6=i,:
where we have to pay attention to the correct dimensionality. Since fi
maps onto R and each row of A is of size 1 ⇥ N , we obtain a 1 ⇥ 1 ⇥ N -
sized tensor as the partial derivative of fi with respect to a row of A.
We stack the partial derivatives (5.91) and get the desired gradient
in (5.87) via
2 >3
0
6 .. 7
6 . 7
6 >7
60 7
@fi 6 >7
=6 x 7 72R
1⇥(M ⇥N )
. (5.92)
@A 6 60 7
>
6 7
6 . 7
4 .. 5
0>

Example 5.13 (Gradient of Matrices with Respect to Matrices)


Consider a matrix R 2 RM ⇥N and f : RM ⇥N ! RN ⇥N with
f (R) = R> R =: K 2 RN ⇥N , (5.93)
where we seek the gradient dK/dR.
To solve this hard problem, let us first write down what we already
know: The gradient has the dimensions
dK
2 R(N ⇥N )⇥(M ⇥N ) , (5.94)
dR
which is a tensor. Moreover,
dKpq
2 R1⇥M ⇥N (5.95)
dR
for p, q = 1, . . . , N , where Kpq is the (p, q)th entry of K = f (R). De-
noting the ith column of R by r i , every entry of K is given by the dot
product of two columns of R, i.e.,
M
X
Kpq = r>
p rq = Rmp Rmq . (5.96)
m=1
@Kpq
When we now compute the partial derivative @Rij
we obtain

XM
@Kpq @
= Rmp Rmq = @pqij , (5.97)
@Rij m=1
@R ij

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


158 Vector Calculus

8
>
> Riq if j = p, p 6= q
<
Rip if j = q, p 6= q
@pqij = . (5.98)
>
> 2Riq if j = p, p = q
:
0 otherwise
From (5.94), we know that the desired gradient has the dimension (N ⇥
N ) ⇥ (M ⇥ N ), and every single entry of this tensor is given by @pqij
in (5.98), where p, q, j = 1, . . . , N and i = 1, . . . , M .

5.5 Useful Identities for Computing Gradients


In the following, we list some useful gradients that are frequently required
in a machine learning context (Petersen and Pedersen, 2012). Here, we
use tr(·) as the trace (see Definition 4.4), det(·) as the determinant (see
Section 4.1) and f (X) 1 as the inverse of f (X), assuming it exists.
✓ ◆>
@ @f (X)
f (X)> = (5.99)
@X @X
✓ ◆
@ @f (X)
tr(f (X)) = tr (5.100)
@X @X
✓ ◆
@ @f (X)
det(f (X)) = det(f (X))tr f (X) 1 (5.101)
@X @X
@ @f (X)
f (X) 1 = f (X) 1 f (X) 1 (5.102)
@X @X
@a> X 1 b
= (X 1 )> ab> (X 1 )> (5.103)
@X
@x> a
= a> (5.104)
@x
@a> x
= a> (5.105)
@x
@a> Xb
= ab> (5.106)
@X
@x> Bx
= x> (B + B > ) (5.107)
@x
@
(x As)> W (x As) = 2(x As)> W A for symmetric W
@s
(5.108)
Remark. In this book, we only cover traces and transposes of matrices.
However, we have seen that derivatives can be higher-dimensional ten-
sors, in which case the usual trace and transpose are not defined. In these
cases, the trace of a D ⇥D ⇥E ⇥F tensor would be an E ⇥F -dimensional
matrix. This is a special case of a tensor contraction. Similarly, when we

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.6 Backpropagation and Automatic Differentiation 159

“transpose” a tensor, we mean swapping the first two dimensions. Specif-


ically, in (5.99) through (5.102), we require tensor-related computations
when we work with multivariate functions f (·) and compute derivatives
with respect to matrices (and choose not to vectorize them as discussed in
Section 5.4). }

5.6 Backpropagation and Automatic Differentiation


A good discussion
In many machine learning applications, we find good model parameters about
by performing gradient descent (Section 7.1), which relies on the fact backpropagation
and the chain rule is
that we can compute the gradient of a learning objective with respect
available at a blog
to the parameters of the model. For a given objective function, we can by Tim Viera at
obtain the gradient with respect to the model parameters using calculus https://tinyurl.
and applying the chain rule; see Section 5.2.2. We already had a taste in com/ycfm2yrw.
Section 5.3 when we looked at the gradient of a squared loss with respect
to the parameters of a linear regression model.
Consider the function
q
f (x) = x2 + exp(x2 ) + cos x2 + exp(x2 ) . (5.109)
By application of the chain rule, and noting that differentiation is linear,
we compute the gradient
df 2x + 2x exp(x2 )
= p sin x2 + exp(x2 ) 2x + 2x exp(x2 )
dx 2 x2 + exp(x2 )
!
1
= 2x p sin x2 + exp(x2 ) 1 + exp(x2 ) .
2 x2 + exp(x2 )
(5.110)
Writing out the gradient in this explicit way is often impractical since it
often results in a very lengthy expression for a derivative. In practice,
it means that, if we are not careful, the implementation of the gradient
could be significantly more expensive than computing the function, which
imposes unnecessary overhead. For training deep neural network mod-
els, the backpropagation algorithm (Kelley, 1960; Bryson, 1961; Dreyfus, backpropagation
1962; Rumelhart et al., 1986) is an efficient way to compute the gradient
of an error function with respect to the parameters of the model.

5.6.1 Gradients in a Deep Network


An area where the chain rule is used to an extreme is deep learning, where
the function value y is computed as a many-level function composition
y = (fK fK 1 · · · f1 )(x) = fK (fK 1 (· · · (f1 (x)) · · · )) , (5.111)
where x are the inputs (e.g., images), y are the observations (e.g., class
labels), and every function fi , i = 1, . . . , K , possesses its own parameters.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


160 Vector Calculus

Figure 5.8 Forward


pass in a multi-layer
neural network to x f1 fK 1 fK L
compute the loss L
as a function of the
inputs x and the
parameters Ai , bi . A0 , b 0 A1 , b 1 AK 2 , b K 2 AK 1 , b K 1

We discuss the case, In neural networks with multiple layers, we have functions fi (xi 1 ) =
where the activation (Ai 1 xi 1 + bi 1 ) in the ith layer. Here xi 1 is the output of layer i 1
functions are
and an activation function, such as the logistic sigmoid 1+e1 x , tanh or a
identical in each
layer to unclutter rectified linear unit (ReLU). In order to train these models, we require the
notation. gradient of a loss function L with respect to all model parameters Aj , bj
for j = 1, . . . , K . This also requires us to compute the gradient of L with
respect to the inputs of each layer. For example, if we have inputs x and
observations y and a network structure defined by

f 0 := x (5.112)
f i := i (Ai 1 f i 1 + bi 1 ) , i = 1, . . . , K , (5.113)

see also Figure 5.8 for a visualization, we may be interested in finding


Aj , bj for j = 0, . . . , K 1, such that the squared loss
L(✓) = ky f K (✓, x)k2 (5.114)

is minimized, where ✓ = {A0 , b0 , . . . , AK 1 , bK 1 }.


To obtain the gradients with respect to the parameter set ✓ , we require
the partial derivatives of L with respect to the parameters ✓ j = {Aj , bj }
of each layer j = 0, . . . , K 1. The chain rule allows us to determine the
A more in-depth partial derivatives as
discussion about
gradients of neural @L @L @f K
networks can be
= (5.115)
@✓ K 1 @f K @✓ K 1
found in Justin
Domke’s lecture @L @L @f K @f K 1
notes = (5.116)
@✓ K 2 @f K @f K 1 @✓ K 2
https://tinyurl.
com/yalcxgtv.
@L @L @f K @f K 1 @f K 2
= (5.117)
@✓ K 3 @f K @f K 1 @f K 2 @✓ K 3

@L @L @f K @f i+2 @f i+1
= ··· (5.118)
@✓ i @f K @f K 1 @f i+1 @✓ i

The orange terms are partial derivatives of the output of a layer with
respect to its inputs, whereas the blue terms are partial derivatives of
the output of a layer with respect to its parameters. Assuming, we have
already computed the partial derivatives @L/@✓ i+1 , then most of the com-
putation can be reused to compute @L/@✓ i . The additional terms that we

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.6 Backpropagation and Automatic Differentiation 161

Figure 5.9
Backward pass in a
x f1 fK 1 fK L multi-layer neural
network to compute
the gradients of the
loss function.
A0 , b 0 A1 , b 1 AK 2 , b K 2 AK 1 , b K 1

Figure 5.10 Simple


x a b y
graph illustrating
the flow of data
from x to y via
need to compute are indicated by the boxes. Figure 5.9 visualizes that the some intermediate
gradients are passed backward through the network. variables a, b.

5.6.2 Automatic Differentiation


It turns out that backpropagation is a special case of a general technique
in numerical analysis called automatic differentiation. We can think of au- automatic
tomatic differentation as a set of techniques to numerically (in contrast to differentiation
symbolically) evaluate the exact (up to machine precision) gradient of a
function by working with intermediate variables and applying the chain
rule. Automatic differentiation applies a series of elementary arithmetic Automatic
operations, e.g., addition and multiplication and elementary functions, differentiation is
different from
e.g., sin, cos, exp, log. By applying the chain rule to these operations, the
symbolic
gradient of quite complicated functions can be computed automatically. differentiation and
Automatic differentiation applies to general computer programs and has numerical
forward and reverse modes. Baydin et al. (2018) give a great overview of approximations of
the gradient, e.g., by
automatic differentiation in machine learning.
using finite
Figure 5.10 shows a simple graph representing the data flow from in- differences.
puts x to outputs y via some intermediate variables a, b. If we were to
compute the derivative dy/dx, we would apply the chain rule and obtain
dy dy db da
= . (5.119)
dx db da dx
Intuitively, the forward and reverse mode differ in the order of multipli- In the general case,
cation. Due to the associativity of matrix multiplication, we can choose we work with
Jacobians, which
between
✓ ◆ can be vectors,
dy dy db da matrices, or tensors.
= , (5.120)
dx db da dx
✓ ◆
dy dy db da
= . (5.121)
dx db da dx
Equation (5.120) would be the reverse mode because gradients are prop- reverse mode
agated backward through the graph, i.e., reverse to the data flow. Equa-
tion (5.121) would be the forward mode, where the gradients flow with forward mode
the data from left to right through the graph.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


162 Vector Calculus

In the following, we will focus on reverse mode automatic differentia-


tion, which is backpropagation. In the context of neural networks, where
the input dimensionality is often much higher than the dimensionality of
the labels, the reverse mode is computationally significantly cheaper than
the forward mode. Let us start with an instructive example.

Example 5.14
Consider the function
q
f (x) = x2 + exp(x2 ) + cos x2 + exp(x2 ) (5.122)
from (5.109). If we were to implement a function f on a computer, we
intermediate would be able to save some computation by using intermediate variables:
variables
a = x2 , (5.123)
b = exp(a) , (5.124)
c = a + b, (5.125)
p
d = c, (5.126)
e = cos(c) , (5.127)
f = d + e. (5.128)

p
Figure 5.11 exp(·) b · d
Computation graph
with inputs x,
function values f ,
x (·)2 a + c + f
and intermediate
variables a, b, c, d, e. cos(·) e

This is the same kind of thinking process that occurs when applying
the chain rule. Note that the preceding set of equations requires fewer
operations than a direct implementation of the function f (x) as defined
in (5.109). The corresponding computation graph in Figure 5.11 shows
the flow of data and computations required to obtain the function value
f.
The set of equations that include intermediate variables can be thought
of as a computation graph, a representation that is widely used in imple-
mentations of neural network software libraries. We can directly compute
the derivatives of the intermediate variables with respect to their corre-
sponding inputs by recalling the definition of the derivative of elementary
functions. We obtain the following:
@a
= 2x (5.129)
@x
@b
= exp(a) (5.130)
@a

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.6 Backpropagation and Automatic Differentiation 163

@c @c
=1= (5.131)
@a @b
@d 1
= p (5.132)
@c 2 c
@e
= sin(c) (5.133)
@c
@f @f
=1= . (5.134)
@d @e
By looking at the computation graph in Figure 5.11, we can compute
@f /@x by working backward from the output and obtain
@f @f @d @f @e
= + (5.135)
@c @d @c @e @c
@f @f @c
= (5.136)
@b @c @b
@f @f @b @f @c
= + (5.137)
@a @b @a @c @a
@f @f @a
= . (5.138)
@x @a @x
Note that we implicitly applied the chain rule to obtain @f /@x. By substi-
tuting the results of the derivatives of the elementary functions, we get
@f 1
= 1 · p + 1 · ( sin(c)) (5.139)
@c 2 c
@f @f
= ·1 (5.140)
@b @c
@f @f @f
= exp(a) + ·1 (5.141)
@a @b @c
@f @f
= · 2x . (5.142)
@x @a
By thinking of each of the derivatives above as a variable, we observe
that the computation required for calculating the derivative is of similar
complexity as the computation of the function itself. This is quite counter-
intuitive since the mathematical expression for the derivative @f@x
(5.110)
is significantly more complicated than the mathematical expression of the
function f (x) in (5.109).

Automatic differentiation is a formalization of Example 5.14. Let x1 , . . . , xd


be the input variables to the function, xd+1 , . . . , xD 1 be the intermediate
variables, and xD the output variable. Then the computation graph can be
expressed as follows:

For i = d + 1, . . . , D : xi = gi (xPa(xi ) ) , (5.143)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


164 Vector Calculus

where the gi (·) are elementary functions and xPa(xi ) are the parent nodes
of the variable xi in the graph. Given a function defined in this way, we
can use the chain rule to compute the derivative of the function in a step-
by-step fashion. Recall that by definition f = xD and hence
@f
= 1. (5.144)
@xD
For other variables xi , we apply the chain rule
@f X @f @xj X @f @gj
= = , (5.145)
@xi x @xj @xi @xj @xi
j :xi 2Pa(xj ) x j :xi 2Pa(xj )

where Pa(xj ) is the set of parent nodes of xj in the computation graph.


Auto-differentiation Equation (5.143) is the forward propagation of a function, whereas (5.145)
in reverse mode is the backpropagation of the gradient through the computation graph.
requires a parse
For neural network training, we backpropagate the error of the prediction
tree.
with respect to the label.
The automatic differentiation approach above works whenever we have
a function that can be expressed as a computation graph, where the ele-
mentary functions are differentiable. In fact, the function may not even be
a mathematical function but a computer program. However, not all com-
puter programs can be automatically differentiated, e.g., if we cannot find
differential elementary functions. Programming structures, such as for
loops and if statements, require more care as well.

5.7 Higher-Order Derivatives


So far, we have discussed gradients, i.e., first-order derivatives. Some-
times, we are interested in derivatives of higher order, e.g., when we want
to use Newton’s Method for optimization, which requires second-order
derivatives (Nocedal and Wright, 2006). In Section 5.1.1, we discussed
the Taylor series to approximate functions using polynomials. In the mul-
tivariate case, we can do exactly the same. In the following, we will do
exactly this. But let us start with some notation.
Consider a function f : R2 ! R of two variables x, y . We use the
following notation for higher-order partial derivatives (and for gradients):
@2f
@x2
is the second partial derivative of f with respect to x.
@nf
@xn
is the nth partial derivative of f with respect to x.
@2f
@y@x
@
= @y @f
@x
is the partial derivative obtained by first partial differ-
entiating with respect to x and then with respect to y .
@2f
@x@y
is the partial derivative obtained by first partial differentiating by
y and then x.
Hessian The Hessian is the collection of all second-order partial derivatives.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.8 Linearization and Multivariate Taylor Series 165

Figure 5.12 Linear


approximation of a
function. The
1 f (x) original function f
is linearized at
x0 = 2 using a
0
f (x)

first-order Taylor
series expansion.
1 f (x0) f (x0) + f 0(x0)(x x0 )

4 2 0 2 4
x

If f (x, y) is a twice (continuously) differentiable function, then


@2f @2f
= , (5.146)
@x@y @y@x
i.e., the order of differentiation does not matter, and the corresponding
Hessian matrix Hessian matrix
2 2 3
@ f @2f
6 @x2 @x@y 7
H=6 4 @ f
7 (5.147)
2
@2f 5
@x@y @y 2
is symmetric. The Hessian is denoted as r2x,y f (x, y). Generally, for x 2 Rn
and f : Rn ! R, the Hessian is an n ⇥ n matrix. The Hessian measures
the curvature of the function locally around (x, y).
Remark (Hessian of a Vector Field). If f : Rn ! Rm is a vector field, the
Hessian is an (m ⇥ n ⇥ n)-tensor. }

5.8 Linearization and Multivariate Taylor Series


The gradient rf of a function f is often used for a locally linear approxi-
mation of f around x0 :
f (x) ⇡ f (x0 ) + (rx f )(x0 )(x x0 ) . (5.148)
Here (rx f )(x0 ) is the gradient of f with respect to x, evaluated at x0 .
Figure 5.12 illustrates the linear approximation of a function f at an input
x0 . The original function is approximated by a straight line. This approx-
imation is locally accurate, but the farther we move away from x0 the
worse the approximation gets. Equation (5.148) is a special case of a mul-
tivariate Taylor series expansion of f at x0 , where we consider only the
first two terms. We discuss the more general case in the following, which
will allow for better approximations.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


166 Vector Calculus

Figure 5.13
Visualizing outer
products. Outer
products of vectors
increase the
dimensionality of
the array by 1 per
term. (a) The outer (a) Given a vector 2 R4 , we obtain the outer product 2
:= ⌦ = >
2
product of two R4⇥4 as a matrix.
vectors results in a
matrix; (b) the
outer product of
three vectors yields
a third-order tensor.

(b) An outer product 3 := ⌦ ⌦ 2 R4⇥4⇥4 results in a third-order tensor (“three-


dimensional matrix”), i.e., an array with three indexes.

Definition 5.7 (Multivariate Taylor Series). We consider a function


f : RD ! R (5.149)
x 7! f (x) , x2R , D
(5.150)
that is smooth at x0 . When we define the difference vector := x x0 ,
multivariate Taylor the multivariate Taylor series of f at (x0 ) is defined as
series
1
X Dk f (x0 ) k
f (x) = x
, (5.151)
k=0
k!

where Dxk f (x0 ) is the k -th (total) derivative of f with respect to x, eval-
uated at x0 .

Taylor polynomial Definition 5.8 (Taylor Polynomial). The Taylor polynomial of degree n of
f at x0 contains the first n + 1 components of the series in (5.151) and is
defined as
n
X Dk f (x0 ) k
Tn (x) = x
. (5.152)
k=0
k!

In (5.151) and (5.152), we used the slightly sloppy notation of k ,


which is not defined for vectors x 2 RD , D > 1, and k > 1. Note that
A vector can be both Dxk f and k are k -th order tensors, i.e., k -dimensional arrays. The
implemented as a k times
one-dimensional
z }| {
k
array, a matrix as a k th-order tensor 2R D⇥D⇥...⇥D
is obtained as a k -fold outer product,
two-dimensional denoted by ⌦, of the vector 2 R . For example,
D

array.
2 > 2
:= ⌦ = , [i, j] = [i] [j] (5.153)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.8 Linearization and Multivariate Taylor Series 167
3 3
:= ⌦ ⌦ , [i, j, k] = [i] [j] [k] . (5.154)
Figure 5.13 visualizes two such outer products. In general, we obtain the
terms
D
X D
X
Dxk f (x0 ) k = ··· Dxk f (x0 )[i1 , . . . , ik ] [i1 ] · · · [ik ] (5.155)
i1 =1 ik =1

in the Taylor series, where Dxk f (x0 ) k contains k -th order polynomials.
Now that we defined the Taylor series for vector fields, let us explicitly
write down the first terms Dxk f (x0 ) k of the Taylor series expansion for
k = 0, . . . , 3 and := x x0 :
np.einsum(
0
k = 0 : Dx0 f (x0 ) = f (x0 ) 2 R (5.156) ’i,i’,Df1,d)
D
np.einsum(
X ’ij,i,j’,
k=1: Dx1 f (x0 ) 1 = rx f (x0 ) |{z} = rx f (x0 )[i] [i] 2 R (5.157) Df2,d,d)
| {z } i=1
1⇥D D⇥1 np.einsum(
> > ’ijk,i,j,k’,
k=2: Dx2 f (x0 ) 2 = tr H(x0 ) |{z} |{z} = H(x0 ) (5.158) Df3,d,d,d)
| {z }
D⇥D D⇥1 1⇥D

D X
X D
= H[i, j] [i] [j] 2 R (5.159)
i=1 j=1
D X
X D X
D
3
k = 3 : Dx3 f (x0 ) = Dx3 f (x0 )[i, j, k] [i] [j] [k] 2 R
i=1 j=1 k=1
(5.160)
Here, H(x0 ) is the Hessian of f evaluated at x0 .

Example 5.15 (Taylor Series Expansion of a Function with Two Vari-


ables)
Consider the function
f (x, y) = x2 + 2xy + y 3 . (5.161)
We want to compute the Taylor series expansion of f at (x0 , y0 ) = (1, 2).
Before we start, let us discuss what to expect: The function in (5.161) is
a polynomial of degree 3. We are looking for a Taylor series expansion,
which itself is a linear combination of polynomials. Therefore, we do not
expect the Taylor series expansion to contain terms of fourth or higher
order to express a third-order polynomial. This means that it should be
sufficient to determine the first four terms of (5.151) for an exact alterna-
tive representation of (5.161).
To determine the Taylor series expansion, we start with the constant
term and the first-order derivatives, which are given by
f (1, 2) = 13 (5.162)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


168 Vector Calculus

@f @f
= 2x + 2y =) (1, 2) = 6 (5.163)
@x @x
@f @f
= 2x + 3y 2 =) (1, 2) = 14 . (5.164)
@y @y
Therefore, we obtain
h i ⇥ ⇤
1 @f @f
Dx,y f (1, 2) = rx,y f (1, 2) = @x
(1, 2) @y
(1, 2) = 6 14 2 R1⇥2
(5.165)
such that
1 
Dx,y f (1, 2) ⇥ ⇤ x 1
= 6 14 = 6(x 1) + 14(y 2) . (5.166)
1! y 2
Note that Dx,y
1
f (1, 2) contains only linear terms, i.e., first-order polyno-
mials.
The second-order partial derivatives are given by
@2f @2f
= 2 =) (1, 2) = 2 (5.167)
@x2 @x2
@2f @2f
= 6y =) (1, 2) = 12 (5.168)
@y 2 @y 2
@2f @2f
= 2 =) (1, 2) = 2 (5.169)
@y@x @y@x
@2f @2f
= 2 =) (1, 2) = 2 . (5.170)
@x@y @x@y
When we collect the second-order partial derivatives, we obtain the Hes-
sian
" 2 # 
@ f @2f
2 2
H = @2f@x2 @x@y
@2f
= , (5.171)
2
2 6y
@y@x @y

such that

2 2
H(1, 2) = 2 R2⇥2 . (5.172)
2 12
Therefore, the next term of the Taylor-series expansion is given by
2
Dx,y f (1, 2) 2 1 >
= H(1, 2) (5.173a)
2! 2  
1⇥ ⇤ 2 2 x 1
= x 1 y 2 (5.173b)
2 2 12 y 2
2
= (x 1) + 2(x 1)(y 2) + 6(y 2)2 . (5.173c)
2
Here, Dx,y
2
f (1, 2) contains only quadratic terms, i.e., second-order poly-
nomials.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


5.8 Linearization and Multivariate Taylor Series 169

The third-order derivatives are obtained as


h i
3
Dx,y f = @H @x
@H
@y 2 R2⇥2⇥2 , (5.174)
" 3 #
@ f @3f
@H
3
Dx,y f [:, :, 1] = = @x3
@3f
@x2 @y
@3f
, (5.175)
@x @x@y@x @x@y 2
" 3 #
@ f @3f
@H
3
Dx,y f [:, :, 2] = = @3f@y@x2 @y@x@y
@3f
. (5.176)
@y @y 2 @x @y 3

Since most second-order partial derivatives in the Hessian in (5.171) are


constant, the only nonzero third-order partial derivative is
@3f @3f
= 6 =) (1, 2) = 6 . (5.177)
@y 3 @y 3
Higher-order derivatives and the mixed derivatives of degree 3 (e.g.,
@f 3
@x2 @y
) vanish, such that
 
0 0 0 0
3
Dx,y f [:, :, 1] = 3
, Dx,y f [:, :, 2] = (5.178)
0 0 0 6
and
3
Dx,y f (1, 2) 3
= (y 2)3 , (5.179)
3!
which collects all cubic terms of the Taylor series. Overall, the (exact)
Taylor series expansion of f at (x0 , y0 ) = (1, 2) is
2 3
1
Dx,y f (1, 2) 2 Dx,y f (1, 2) 3
f (x) = f (1, 2) + Dx,y f (1, 2) + +
2! 3!
(5.180a)
@f (1, 2) @f (1, 2)
= f (1, 2) + (x 1) + (y 2)
@x @y
✓ 2
1 @ f (1, 2) 2 @ 2 f (1, 2)
+ (x 1) + (y 2)2
2! @x2 @y 2

@ 2 f (1, 2) 1 @ 3 f (1, 2)
+2 (x 1)(y 2) + (y 2)3 (5.180b)
@x@y 6 @y 3
= 13 + 6(x 1) + 14(y 2)
+ (x 1)2 + 6(y 2)2 + 2(x 1)(y 2) + (y 2)3 . (5.180c)
In this case, we obtained an exact Taylor series expansion of the polyno-
mial in (5.161), i.e., the polynomial in (5.180c) is identical to the original
polynomial in (5.161). In this particular example, this result is not sur-
prising since the original function was a third-order polynomial, which
we expressed through a linear combination of constant terms, first-order,
second-order, and third-order polynomials in (5.180c).

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


170 Vector Calculus

5.9 Further Reading


Further details of matrix differentials, along with a short review of the
required linear algebra, can be found in Magnus and Neudecker (2007).
Automatic differentiation has had a long history, and we refer to Griewank
and Walther (2003), Griewank and Walther (2008), and Elliott (2009)
and the references therein.
In machine learning (and other disciplines), we often need to compute
expectations, i.e., we need to solve integrals of the form
Z
Ex [f (x)] = f (x)p(x)dx . (5.181)

Even if p(x) is in a convenient form (e.g., Gaussian), this integral gen-


erally cannot be solved analytically. The Taylor series expansion of f is
one way of finding an approximate solution: Assuming p(x) = N µ, ⌃
is Gaussian, then the first-order Taylor series expansion around µ locally
linearizes the nonlinear function f . For linear functions, we can compute
the mean (and the covariance) exactly if p(x) is Gaussian distributed (see
extended Kalman Section 6.5). This property is heavily exploited by the extended Kalman
filter filter (Maybeck, 1979) for online state estimation in nonlinear dynami-
cal systems (also called “state-space models”). Other deterministic ways
unscented transform to approximate the integral in (5.181) are the unscented transform (Julier
Laplace and Uhlmann, 1997), which does not require any gradients, or the Laplace
approximation approximation (MacKay, 2003; Bishop, 2006; Murphy, 2012), which uses
a second-order Taylor series expansion (requiring the Hessian) for a local
Gaussian approximation of p(x) around its mode.

Exercises
5.1 Compute the derivative f 0 (x) for
f (x) = log(x4 ) sin(x3 ) .

5.2 Compute the derivative f 0 (x) of the logistic sigmoid


1
f (x) = .
1 + exp( x)
5.3 Compute the derivative f 0 (x) of the function
f (x) = exp( 2
1
2 (x µ)2 ) ,

where µ, 2 R are constants.


5.4 Compute the Taylor polynomials Tn , n = 0, . . . , 5 of f (x) = sin(x) + cos(x)
at x0 = 0.
5.5 Consider the following functions:
f1 (x) = sin(x1 ) cos(x2 ) , x 2 R2
f2 (x, y) = x> y , x, y 2 Rn
f3 (x) = xx> , x 2 Rn

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Exercises 171
@fi
a. What are the dimensions of @x ?
b. Compute the Jacobians.
5.6 Differentiate f with respect to t and g with respect to X , where
f (t) = sin(log(t> t)) , t 2 RD
g(X) = tr(AXB) , A 2 RD⇥E , X 2 RE⇥F , B 2 RF ⇥D ,
where tr denotes the trace.
5.7 Compute the derivatives df /dx of the following functions by using the chain
rule. Provide the dimensions of every single partial derivative. Describe your
steps in detail.
a.
f (z) = log(1 + z) , z = x> x , x 2 RD
b.
f (z) = sin(z) , z = Ax + b , A 2 RE⇥D , x 2 RD , b 2 RE
where sin(·) is applied to every element of z .
5.8 Compute the derivatives df /dx of the following functions. Describe your
steps in detail.
a. Use the chain rule. Provide the dimensions of every single partial deriva-
tive.
1
f (z) = exp( 2 z)
> 1
z = g(y) = y S y
y = h(x) = x µ

where x, µ 2 RD , S 2 RD⇥D .
b.
f (x) = tr(xx> + 2
I) , x 2 RD
Here tr(A) is the trace of A, i.e., the sum of the diagonal elements Aii .
Hint: Explicitly write out the outer product.
c. Use the chain rule. Provide the dimensions of every single partial deriva-
tive. You do not need to compute the product of the partial derivatives
explicitly.
f = tanh(z) 2 RM
z = Ax + b, x 2 RN , A 2 RM ⇥N , b 2 RM .
Here, tanh is applied to every component of z .
5.9 We define
g(z, ⌫) := log p(x, z) log q(z, ⌫)
z := t(✏, ⌫)
for differentiable functions p, q, t. By using the chain rule, compute the gra-
dient
d
g(z, ⌫) .
d⌫

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


6

Probability and Distributions

Probability, loosely speaking, concerns the study of uncertainty. Probabil-


ity can be thought of as the fraction of times an event occurs, or as a degree
of belief about an event. We then would like to use this probability to mea-
sure the chance of something occurring in an experiment. As mentioned
in Chapter 1, we often quantify uncertainty in the data, uncertainty in the
machine learning model, and uncertainty in the predictions produced by
random variable the model. Quantifying uncertainty requires the idea of a random variable,
which is a function that maps outcomes of random experiments to a set of
properties that we are interested in. Associated with the random variable
is a function that measures the probability that a particular outcome (or
probability set of outcomes) will occur; this is called the probability distribution.
distribution Probability distributions are used as a building block for other con-
cepts, such as probabilistic modeling (Section 8.4), graphical models (Sec-
tion 8.5), and model selection (Section 8.6). In the next section, we present
the three concepts that define a probability space (the sample space, the
events, and the probability of an event) and how they are related to a
fourth concept called the random variable. The presentation is deliber-
ately slightly hand wavy since a rigorous presentation may occlude the
intuition behind the concepts. An outline of the concepts presented in this
chapter are shown in Figure 6.1.

6.1 Construction of a Probability Space


The theory of probability aims at defining a mathematical structure to
describe random outcomes of experiments. For example, when tossing a
single coin, we cannot determine the outcome, but by doing a large num-
ber of coin tosses, we can observe a regularity in the average outcome.
Using this mathematical structure of probability, the goal is to perform
automated reasoning, and in this sense, probability generalizes logical
reasoning (Jaynes, 2003).

6.1.1 Philosophical Issues


When constructing automated reasoning systems, classical Boolean logic
does not allow us to express certain forms of plausible reasoning. Consider

172
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
6.1 Construction of a Probability Space 173

Figure 6.1 A mind


Mean Variance Bayes’ Theorem
map of the concepts
related to random
variables and
probability
distributions, as
Chapter 9 described in this
Summary statistics Product rule Sum rule Regression chapter.
Pro
per
ty

Random variable Example


Transformations & distribution Gaussian
Ex Chapter 10
am Dimensionality
ple
Property

reduction
y

Independence
rit

Bernoulli
ila
m

Sufficient statistics
Si

Conjugate

Chapter 11
Finite

Density estimation

Inner product Beta


Exponential family

the following scenario: We observe that A is false. We find B becomes


less plausible, although no conclusion can be drawn from classical logic.
We observe that B is true. It seems A becomes more plausible. We use
this form of reasoning daily. We are waiting for a friend, and consider
three possibilities: H1, she is on time; H2, she has been delayed by traffic;
and H3, she has been abducted by aliens. When we observe our friend
is late, we must logically rule out H1. We also tend to consider H2 to be
more likely, though we are not logically required to do so. Finally, we may
consider H3 to be possible, but we continue to consider it quite unlikely.
How do we conclude H2 is the most plausible answer? Seen in this way, “For plausible
probability theory can be considered a generalization of Boolean logic. In reasoning it is
necessary to extend
the context of machine learning, it is often applied in this way to formalize
the discrete true and
the design of automated reasoning systems. Further arguments about how false values of truth
probability theory is the foundation of reasoning systems can be found to continuous
in Pearl (1988). plausibilities”
(Jaynes, 2003).
The philosophical basis of probability and how it should be somehow
related to what we think should be true (in the logical sense) was studied
by Cox (Jaynes, 2003). Another way to think about it is that if we are
precise about our common sense we end up constructing probabilities.
E. T. Jaynes (1922–1998) identified three mathematical criteria, which
must apply to all plausibilities:
1. The degrees of plausibility are represented by real numbers.
2. These numbers must be based on the rules of common sense.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


174 Probability and Distributions

3. The resulting reasoning must be consistent, with the three following


meanings of the word “consistent”:
(a) Consistency or non-contradiction: When the same result can be
reached through different means, the same plausibility value must
be found in all cases.
(b) Honesty: All available data must be taken into account.
(c) Reproducibility: If our state of knowledge about two problems are
the same, then we must assign the same degree of plausibility to
both of them.
The Cox–Jaynes theorem proves these plausibilities to be sufficient to
define the universal mathematical rules that apply to plausibility p, up to
transformation by an arbitrary monotonic function. Crucially, these rules
are the rules of probability.
Remark. In machine learning and statistics, there are two major interpre-
tations of probability: the Bayesian and frequentist interpretations (Bishop,
2006; Efron and Hastie, 2016). The Bayesian interpretation uses probabil-
ity to specify the degree of uncertainty that the user has about an event. It
is sometimes referred to as “subjective probability” or “degree of belief”.
The frequentist interpretation considers the relative frequencies of events
of interest to the total number of events that occurred. The probability of
an event is defined as the relative frequency of the event in the limit when
one has infinite data. }
Some machine learning texts on probabilistic models use lazy notation
and jargon, which is confusing. This text is no exception. Multiple distinct
concepts are all referred to as “probability distribution”, and the reader
has to often disentangle the meaning from the context. One trick to help
make sense of probability distributions is to check whether we are trying
to model something categorical (a discrete random variable) or some-
thing continuous (a continuous random variable). The kinds of questions
we tackle in machine learning are closely related to whether we are con-
sidering categorical or continuous models.

6.1.2 Probability and Random Variables


There are three distinct ideas that are often confused when discussing
probabilities. First is the idea of a probability space, which allows us to
quantify the idea of a probability. However, we mostly do not work directly
with this basic probability space. Instead, we work with random variables
(the second idea), which transfers the probability to a more convenient
(often numerical) space. The third idea is the idea of a distribution or law
associated with a random variable. We will introduce the first two ideas
in this section and expand on the third idea in Section 6.2.
Modern probability is based on a set of axioms proposed by Kolmogorov

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.1 Construction of a Probability Space 175

(Grinstead and Snell, 1997; Jaynes, 2003) that introduce the three con-
cepts of sample space, event space, and probability measure. The prob-
ability space models a real-world process (referred to as an experiment)
with random outcomes.

The sample space ⌦


The sample space is the set of all possible outcomes of the experiment, sample space
usually denoted by ⌦. For example, two successive coin tosses have
a sample space of {hh, tt, ht, th}, where “h” denotes “heads” and “t”
denotes “tails”.
The event space A
The event space is the space of potential results of the experiment. A event space
subset A of the sample space ⌦ is in the event space A if at the end
of the experiment we can observe whether a particular outcome ! 2 ⌦
is in A. The event space A is obtained by considering the collection of
subsets of ⌦, and for discrete probability distributions (Section 6.2.1)
A is often the power set of ⌦.
The probability P
With each event A 2 A, we associate a number P (A) that measures the
probability or degree of belief that the event will occur. P (A) is called
the probability of A. probability

The probability of a single event must lie in the interval [0, 1], and the
total probability over all outcomes in the sample space ⌦ must be 1, i.e.,
P (⌦) = 1. Given a probability space (⌦, A, P ), we want to use it to model
some real-world phenomenon. In machine learning, we often avoid explic-
itly referring to the probability space, but instead refer to probabilities on
quantities of interest, which we denote by T . In this book, we refer to T
as the target space and refer to elements of T as states. We introduce a target space
function X : ⌦ ! T that takes an element of ⌦ (an outcome) and returns
a particular quantity of interest x, a value in T . This association/mapping
from ⌦ to T is called a random variable. For example, in the case of tossing random variable
two coins and counting the number of heads, a random variable X maps
to the three possible outcomes: X(hh) = 2, X(ht) = 1, X(th) = 1, and
X(tt) = 0. In this particular case, T = {0, 1, 2}, and it is the probabilities
on elements of T that we are interested in. For a finite sample space ⌦ and The name “random
finite T , the function corresponding to a random variable is essentially a variable” is a great
source of
lookup table. For any subset S ✓ T , we associate PX (S) 2 [0, 1] (the
misunderstanding
probability) to a particular event occurring corresponding to the random as it is neither
variable X . Example 6.1 provides a concrete illustration of the terminol- random nor is it a
ogy. variable. It is a
function.
Remark. The aforementioned sample space ⌦ unfortunately is referred
to by different names in different books. Another common name for ⌦
is “state space” (Jacod and Protter, 2004), but state space is sometimes
reserved for referring to states in a dynamical system (Hasselblatt and

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


176 Probability and Distributions

Katok, 2003). Other names sometimes used to describe ⌦ are: “sample


description space”, “possibility space,” and “event space”. }

Example 6.1
This toy example is We assume that the reader is already familiar with computing probabil-
essentially a biased
ities of intersections and unions of sets of events. A gentler introduction
coin flip example.
to probability with many examples can be found in chapter 2 of Walpole
et al. (2011).
Consider a statistical experiment where we model a funfair game con-
sisting of drawing two coins from a bag (with replacement). There are
coins from USA (denoted as $) and UK (denoted as £) in the bag, and
since we draw two coins from the bag, there are four outcomes in total.
The state space or sample space ⌦ of this experiment is then ($, $), ($,
£), (£, $), (£, £). Let us assume that the composition of the bag of coins is
such that a draw returns at random a $ with probability 0.3.
The event we are interested in is the total number of times the repeated
draw returns $. Let us define a random variable X that maps the sample
space ⌦ to T , which denotes the number of times we draw $ out of the
bag. We can see from the preceding sample space we can get zero $, one $,
or two $s, and therefore T = {0, 1, 2}. The random variable X (a function
or lookup table) can be represented as a table like the following:
X(($, $)) = 2 (6.1)
X(($, £)) = 1 (6.2)
X((£, $)) = 1 (6.3)
X((£, £)) = 0 . (6.4)
Since we return the first coin we draw before drawing the second, this
implies that the two draws are independent of each other, which we will
discuss in Section 6.4.5. Note that there are two experimental outcomes,
which map to the same event, where only one of the draws returns $.
Therefore, the probability mass function (Section 6.2.1) of X is given by
P (X = 2) = P (($, $))
= P ($) · P ($)
= 0.3 · 0.3 = 0.09 (6.5)
P (X = 1) = P (($, £) [ (£, $))
= P (($, £)) + P ((£, $))
= 0.3 · (1 0.3) + (1 0.3) · 0.3 = 0.42 (6.6)
P (X = 0) = P ((£, £))
= P (£) · P (£)
= (1 0.3) · (1 0.3) = 0.49 . (6.7)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.1 Construction of a Probability Space 177

In the calculation, we equated two different concepts, the probability


of the output of X and the probability of the samples in ⌦. For example,
in (6.7) we say P (X = 0) = P ((£, £)). Consider the random variable
X : ⌦ ! T and a subset S ✓ T (for example, a single element of T ,
such as the outcome that one head is obtained when tossing two coins).
Let X 1 (S) be the pre-image of S by X , i.e., the set of elements of ⌦ that
map to S under X ; {! 2 ⌦ : X(!) 2 S}. One way to understand the
transformation of probability from events in ⌦ via the random variable
X is to associate it with the probability of the pre-image of S (Jacod and
Protter, 2004). For S ✓ T , we have the notation

PX (S) = P (X 2 S) = P (X 1
(S)) = P ({! 2 ⌦ : X(!) 2 S}) . (6.8)
The left-hand side of (6.8) is the probability of the set of possible outcomes
(e.g., number of $ = 1) that we are interested in. Via the random variable
X , which maps states to outcomes, we see in the right-hand side of (6.8)
that this is the probability of the set of states (in ⌦) that have the property
(e.g., $£, £$). We say that a random variable X is distributed according
to a particular probability distribution PX , which defines the probability
mapping between the event and the probability of the outcome of the
random variable. In other words, the function PX or equivalently P X 1
is the law or distribution of random variable X . law
distribution
Remark. The target space, that is, the range T of the random variable X ,
is used to indicate the kind of probability space, i.e., a T random variable.
When T is finite or countably infinite, this is called a discrete random
variable (Section 6.2.1). For continuous random variables (Section 6.2.2),
we only consider T = R or T = RD . }

6.1.3 Statistics
Probability theory and statistics are often presented together, but they con-
cern different aspects of uncertainty. One way of contrasting them is by the
kinds of problems that are considered. Using probability, we can consider
a model of some process, where the underlying uncertainty is captured
by random variables, and we use the rules of probability to derive what
happens. In statistics, we observe that something has happened and try
to figure out the underlying process that explains the observations. In this
sense, machine learning is close to statistics in its goals to construct a
model that adequately represents the process that generated the data. We
can use the rules of probability to obtain a “best-fitting” model for some
data.
Another aspect of machine learning systems is that we are interested
in generalization error (see Chapter 8). This means that we are actually
interested in the performance of our system on instances that we will
observe in future, which are not identical to the instances that we have

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


178 Probability and Distributions

seen so far. This analysis of future performance relies on probability and


statistics, most of which is beyond what will be presented in this chapter.
The interested reader is encouraged to look at the books by Boucheron
et al. (2013) and Shalev-Shwartz and Ben-David (2014). We will see more
about statistics in Chapter 8.

6.2 Discrete and Continuous Probabilities


Let us focus our attention on ways to describe the probability of an event
as introduced in Section 6.1. Depending on whether the target space is dis-
crete or continuous, the natural way to refer to distributions is different.
When the target space T is discrete, we can specify the probability that a
random variable X takes a particular value x 2 T , denoted as P (X = x).
The expression P (X = x) for a discrete random variable X is known as
probability mass the probability mass function. When the target space T is continuous, e.g.,
function the real line R, it is more natural to specify the probability that a random
variable X is in an interval, denoted by P (a 6 X 6 b) for a < b. By con-
vention, we specify the probability that a random variable X is less than
a particular value x, denoted by P (X 6 x). The expression P (X 6 x) for
cumulative a continuous random variable X is known as the cumulative distribution
distribution function function. We will discuss continuous random variables in Section 6.2.2.
We will revisit the nomenclature and contrast discrete and continuous
random variables in Section 6.2.3.
univariate Remark. We will use the phrase univariate distribution to refer to distribu-
tions of a single random variable (whose states are denoted by non-bold
x). We will refer to distributions of more than one random variable as
multivariate multivariate distributions, and will usually consider a vector of random
variables (whose states are denoted by bold x). }

6.2.1 Discrete Probabilities


When the target space is discrete, we can imagine the probability distri-
bution of multiple random variables as filling out a (multidimensional)
array of numbers. Figure 6.2 shows an example. The target space of the
joint probability is the Cartesian product of the target spaces of each of
joint probability the random variables. We define the joint probability as the entry of both
values jointly
nij
P (X = xi , Y = yj ) = , (6.9)
N
where nij is the number of events with state xi and yj and N the total
number of events. The joint probability is the probability of the intersec-
tion of both events, that is, P (X = xi , Y = yj ) = P (X = xi \ Y = yj ).
probability mass Figure 6.2 illustrates the probability mass function (pmf) of a discrete prob-
function ability distribution. For two random variables X and Y , the probability

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.2 Discrete and Continuous Probabilities 179

ci Figure 6.2
z }|{ Visualization of a
y1 discrete bivariate
o probability mass
Y y2 nij rj function, with
random variables X
y3 and Y . This
x1 x2 x3 x4 x5 diagram is adapted
from Bishop (2006).
X

that X = x and Y = y is (lazily) written as p(x, y) and is called the joint


probability. One can think of a probability as a function that takes state
x and y and returns a real number, which is the reason we write p(x, y).
The marginal probability that X takes the value x irrespective of the value marginal probability
of random variable Y is (lazily) written as p(x). We write X ⇠ p(x) to
denote that the random variable X is distributed according to p(x). If we
consider only the instances where X = x, then the fraction of instances
(the conditional probability) for which Y = y is written (lazily) as p(y | x). conditional
probability

Example 6.2
Consider two random variables X and Y , where X has five possible states
and Y has three possible states, as shown in Figure 6.2. We denote by nij
the number of events with state X = xi and Y = yj , and denote by
N the total number of events. The value ciP is the sum of the individual
3
frequencies for the ith column, that is, ci = j=1 nij . Similarly, the value
P5
rj is the row sum, that is, rj = i=1 nij . Using these definitions, we can
compactly express the distribution of X and Y .
The probability distribution of each random variable, the marginal
probability, can be seen as the sum over a row or column
P3
ci j=1 nij
P (X = xi ) = = (6.10)
N N
and
P5
rj nij
P (Y = yj ) = = i=1 , (6.11)
N N
where ci and rj are the ith column and j th row of the probability table,
respectively. By convention, for discrete random variables with a finite
number of events, we assume that probabilties sum up to one, that is,
5
X 3
X
P (X = xi ) = 1 and P (Y = yj ) = 1 . (6.12)
i=1 j=1

The conditional probability is the fraction of a row or column in a par-

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


180 Probability and Distributions

ticular cell. For example, the conditional probability of Y given X is


nij
P (Y = yj | X = xi ) = , (6.13)
ci
and the conditional probability of X given Y is
nij
P (X = xi | Y = yj ) = . (6.14)
rj

In machine learning, we use discrete probability distributions to model


categorical variable categorical variables, i.e., variables that take a finite set of unordered val-
ues. They could be categorical features, such as the degree taken at uni-
versity when used for predicting the salary of a person, or categorical la-
bels, such as letters of the alphabet when doing handwriting recognition.
Discrete distributions are also often used to construct probabilistic models
that combine a finite number of continuous distributions (Chapter 11).

6.2.2 Continuous Probabilities


We consider real-valued random variables in this section, i.e., we consider
target spaces that are intervals of the real line R. In this book, we pretend
that we can perform operations on real random variables as if we have dis-
crete probability spaces with finite states. However, this simplification is
not precise for two situations: when we repeat something infinitely often,
and when we want to draw a point from an interval. The first situation
arises when we discuss generalization errors in machine learning (Chap-
ter 8). The second situation arises when we want to discuss continuous
distributions, such as the Gaussian (Section 6.5). For our purposes, the
lack of precision allows for a briefer introduction to probability.
Remark. In continuous spaces, there are two additional technicalities,
which are counterintuitive. First, the set of all subsets (used to define
the event space A in Section 6.1) is not well behaved enough. A needs
to be restricted to behave well under set complements, set intersections,
and set unions. Second, the size of a set (which in discrete spaces can be
obtained by counting the elements) turns out to be tricky. The size of a
measure set is called its measure. For example, the cardinality of discrete sets, the
length of an interval in R, and the volume of a region in Rd are all mea-
sures. Sets that behave well under set operations and additionally have
Borel -algebra a topology are called a Borel -algebra. Betancourt details a careful con-
struction of probability spaces from set theory without being bogged down
in technicalities; see https://tinyurl.com/yb3t6mfd. For a more pre-
cise construction, we refer to Billingsley (1995) and Jacod and Protter
(2004).
In this book, we consider real-valued random variables with their cor-

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.2 Discrete and Continuous Probabilities 181

responding Borel -algebra. We consider random variables with values in


RD to be a vector of real-valued random variables. }
Definition 6.1 (Probability Density Function). A function f : RD ! R is
called a probability density function (pdf ) if probability density
function
1. 8x 2 RD : f (x) > 0 pdf
2. Its integral exists and
Z
f (x)dx = 1 . (6.15)
RD

For probability mass functions (pmf) of discrete random variables, the


integral in (6.15) is replaced with a sum (6.12).
Observe that the probability density function is any function f that is
non-negative and integrates to one. We associate a random variable X
with this function f by
Z b
P (a 6 X 6 b) = f (x)dx , (6.16)
a

where a, b 2 R and x 2 R are outcomes of the continuous random vari-


able X . States x 2 RD are defined analogously by considering a vector
of x 2 R. This association (6.16) is called the law or distribution of the law
random variable X . P (X = x) is a set of
Remark. In contrast to discrete random variables, the probability of a con- measure zero.
tinuous random variable X taking a particular value P (X = x) is zero.
This is like trying to specify an interval in (6.16) where a = b. }
Definition 6.2 (Cumulative Distribution Function). A cumulative distribu- cumulative
tion function (cdf) of a multivariate real-valued random variable X with distribution function
states x 2 RD is given by
FX (x) = P (X1 6 x1 , . . . , XD 6 xD ) , (6.17)
where X = [X1 , . . . , XD ]> , x = [x1 , . . . , xD ]> , and the right-hand side
represents the probability that random variable Xi takes the value smaller
than or equal to xi .
There are cdfs,
The cdf can be expressed also as the integral of the probability density which do not have
function f (x) so that corresponding pdfs.
Z x1 Z xD
FX (x) = ··· f (z1 , . . . , zD )dz1 · · · dzD . (6.18)
1 1

Remark. We reiterate that there are in fact two distinct concepts when
talking about distributions. First is the idea of a pdf (denoted by f (x)),
which is a nonnegative function that sums to one. Second is the law of a
random variable X , that is, the association of a random variable X with
the pdf f (x). }

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


182 Probability and Distributions
Figure 6.3 2.0 2.0
Examples of
(a) discrete and 1.5 1.5
(b) continuous P (Z = z)

p(x)
uniform 1.0 1.0
distributions. See
Example 6.3 for 0.5 0.5
details of the
0.0 0.0
distributions. 1 0 1 2 1 0 1 2
z x
(a) Discrete distribution (b) Continuous distribution

For most of this book, we will not use the notation f (x) and FX (x) as
we mostly do not need to distinguish between the pdf and cdf. However,
we will need to be careful about pdfs and cdfs in Section 6.7.

6.2.3 Contrasting Discrete and Continuous Distributions


Recall from Section 6.1.2 that probabilities are positive and the total prob-
ability sums up to one. For discrete random variables (see (6.12)), this
implies that the probability of each state must lie in the interval [0, 1].
However, for continuous random variables the normalization (see (6.15))
does not imply that the value of the density is less than or equal to 1 for
uniform distribution all values. We illustrate this in Figure 6.3 using the uniform distribution
for both discrete and continuous random variables.

Example 6.3
We consider two examples of the uniform distribution, where each state is
equally likely to occur. This example illustrates some differences between
discrete and continuous probability distributions.
Let Z be a discrete uniform random variable with three states {z =
The actual values of 1.1, z = 0.3, z = 1.5}. The probability mass function can be represented
these states are not as a table of probability values:
meaningful here,
and we deliberately z 1.1 0.3 1.5
chose numbers to
drive home the 1 1 1
P (Z = z) 3 3 3
point that we do not
want to use (and
should ignore) the Alternatively, we can think of this as a graph (Figure 6.3(a)), where we
ordering of the use the fact that the states can be located on the x-axis, and the y -axis
states.
represents the probability of a particular state. The y -axis in Figure 6.3(a)
is deliberately extended so that is it the same as in Figure 6.3(b).
Let X be a continuous random variable taking values in the range 0.9 6
X 6 1.6, as represented by Figure 6.3(b). Observe that the height of the

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.3 Sum Rule, Product Rule, and Bayes’ Theorem 183

Type “Point probability” “Interval probability” Table 6.1


Nomenclature for
Discrete P (X = x) Not applicable probability
Probability mass function distributions.
Continuous p(x) P (X 6 x)
Probability density function Cumulative distribution function

density can be greater than 1. However, it needs to hold that


Z 1.6
p(x)dx = 1 . (6.19)
0.9

Remark. There is an additional subtlety with regards to discrete prob-


ability distributions. The states z1 , . . . , zd do not in principle have any
structure, i.e., there is usually no way to compare them, for example
z1 = red, z2 = green, z3 = blue. However, in many machine learning
applications discrete states take numerical values, e.g., z1 = 1.1, z2 =
0.3, z3 = 1.5, where we could say z1 < z2 < z3 . Discrete states that as-
sume numerical values are particularly useful because we often consider
expected values (Section 6.4.1) of random variables. }
Unfortunately, machine learning literature uses notation and nomen-
clature that hides the distinction between the sample space ⌦, the target
space T , and the random variable X . For a value x of the set of possible
outcomes of the random variable X , i.e., x 2 T , p(x) denotes the prob- We think of the
ability that random variable X has the outcome x. For discrete random outcome x as the
argument that
variables, this is written as P (X = x), which is known as the probabil-
results in the
ity mass function. The pmf is often referred to as the “distribution”. For probability p(x).
continuous variables, p(x) is called the probability density function (often
referred to as a density). To muddy things even further, the cumulative
distribution function P (X 6 x) is often also referred to as the “distribu-
tion”. In this chapter, we will use the notation X to refer to both univariate
and multivariate random variables, and denote the states by x and x re-
spectively. We summarize the nomenclature in Table 6.1.
Remark. We will be using the expression “probability distribution” not
only for discrete probability mass functions but also for continuous proba-
bility density functions, although this is technically incorrect. In line with
most machine learning literature, we also rely on context to distinguish
the different uses of the phrase probability distribution. }

6.3 Sum Rule, Product Rule, and Bayes’ Theorem


We think of probability theory as an extension to logical reasoning. As we
discussed in Section 6.1.1, the rules of probability presented here follow

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


184 Probability and Distributions

naturally from fulfilling the desiderata (Jaynes, 2003, chapter 2). Prob-
abilistic modeling (Section 8.4) provides a principled foundation for de-
signing machine learning methods. Once we have defined probability dis-
tributions (Section 6.2) corresponding to the uncertainties of the data and
our problem, it turns out that there are only two fundamental rules, the
sum rule and the product rule.
Recall from (6.9) that p(x, y) is the joint distribution of the two ran-
dom variables x, y . The distributions p(x) and p(y) are the correspond-
ing marginal distributions, and p(y | x) is the conditional distribution of y
given x. Given the definitions of the marginal and conditional probability
for discrete and continuous random variables in Section 6.2, we can now
These two rules present the two fundamental rules in probability theory.
arise The first rule, the sum rule, states that
naturally (Jaynes, 8 X
2003) from the > p(x, y) if y is discrete
requirements we
>
<
discussed in p(x) = Z
y2Y
, (6.20)
>
>
Section 6.1.1. : p(x, y)dy if y is continuous
sum rule Y

where Y are the states of the target space of random variable Y . This
means that we sum out (or integrate out) the set of states y of the random
marginalization variable Y . The sum rule is also known as the marginalization property.
property The sum rule relates the joint distribution to a marginal distribution. In
general, when the joint distribution contains more than two random vari-
ables, the sum rule can be applied to any subset of the random variables,
resulting in a marginal distribution of potentially more than one random
variable. More concretely, if x = [x1 , . . . , xD ]> , we obtain the marginal
Z
p(xi ) = p(x1 , . . . , xD )dx\i (6.21)

by repeated application of the sum rule where we integrate/sum out all


random variables except xi , which is indicated by \i, which reads “all
except i.”
Remark. Many of the computational challenges of probabilistic modeling
are due to the application of the sum rule. When there are many variables
or discrete variables with many states, the sum rule boils down to per-
forming a high-dimensional sum or integral. Performing high-dimensional
sums or integrals is generally computationally hard, in the sense that there
is no known polynomial-time algorithm to calculate them exactly. }
product rule The second rule, known as the product rule, relates the joint distribution
to the conditional distribution via

p(x, y) = p(y | x)p(x) . (6.22)

The product rule can be interpreted as the fact that every joint distribu-
tion of two random variables can be factorized (written as a product)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.3 Sum Rule, Product Rule, and Bayes’ Theorem 185

of two other distributions. The two factors are the marginal distribu-
tion of the first random variable p(x), and the conditional distribution
of the second random variable given the first p(y | x). Since the ordering
of random variables is arbitrary in p(x, y), the product rule also implies
p(x, y) = p(x | y)p(y). To be precise, (6.22) is expressed in terms of the
probability mass functions for discrete random variables. For continuous
random variables, the product rule is expressed in terms of the probability
density functions (Section 6.2.3).
In machine learning and Bayesian statistics, we are often interested in
making inferences of unobserved (latent) random variables given that we
have observed other random variables. Let us assume we have some prior
knowledge p(x) about an unobserved random variable x and some rela-
tionship p(y | x) between x and a second random variable y , which we
can observe. If we observe y , we can use Bayes’ theorem to draw some
conclusions about x given the observed values of y . Bayes’ theorem (also Bayes’ theorem
Bayes’ rule or Bayes’ law) Bayes’ rule

likelihood prior Bayes’ law


z }| { z }| {
p(y | x) p(x)
p(x | y) = (6.23)
| {z } p(y)
posterior |{z}
evidence

is a direct consequence of the product rule in (6.22) since


p(x, y) = p(x | y)p(y) (6.24)
and
p(x, y) = p(y | x)p(x) (6.25)
so that
p(y | x)p(x)
p(x | y)p(y) = p(y | x)p(x) () p(x | y) = . (6.26)
p(y)
In (6.23), p(x) is the prior, which encapsulates our subjective prior prior
knowledge of the unobserved (latent) variable x before observing any
data. We can choose any prior that makes sense to us, but it is critical to
ensure that the prior has a nonzero pdf (or pmf) on all plausible x, even
if they are very rare.
The likelihood p(y | x) describes how x and y are related, and in the likelihood
case of discrete probability distributions, it is the probability of the data y The likelihood is
sometimes also
if we were to know the latent variable x. Note that the likelihood is not a
called the
distribution in x, but only in y . We call p(y | x) either the “likelihood of “measurement
x (given y )” or the “probability of y given x” but never the likelihood of model”.
y (MacKay, 2003).
The posterior p(x | y) is the quantity of interest in Bayesian statistics posterior
because it expresses exactly what we are interested in, i.e., what we know
about x after having observed y .

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


186 Probability and Distributions

The quantity
Z
p(y) := p(y | x)p(x)dx = EX [p(y | x)] (6.27)

marginal likelihood is the marginal likelihood/evidence. The right-hand side of (6.27) uses the
evidence expectation operator which we define in Section 6.4.1. By definition, the
marginal likelihood integrates the numerator of (6.23) with respect to the
latent variable x. Therefore, the marginal likelihood is independent of
x, and it ensures that the posterior p(x | y) is normalized. The marginal
likelihood can also be interpreted as the expected likelihood where we
take the expectation with respect to the prior p(x). Beyond normalization
of the posterior, the marginal likelihood also plays an important role in
Bayesian model selection, as we will discuss in Section 8.6. Due to the
Bayes’ theorem is integration in (8.44), the evidence is often hard to compute.
also called the Bayes’ theorem (6.23) allows us to invert the relationship between x
“probabilistic
and y given by the likelihood. Therefore, Bayes’ theorem is sometimes
inverse.”
probabilistic inverse called the probabilistic inverse. We will discuss Bayes’ theorem further in
Section 8.4.
Remark. In Bayesian statistics, the posterior distribution is the quantity
of interest as it encapsulates all available information from the prior and
the data. Instead of carrying the posterior around, it is possible to focus
on some statistic of the posterior, such as the maximum of the posterior,
which we will discuss in Section 8.3. However, focusing on some statistic
of the posterior leads to loss of information. If we think in a bigger con-
text, then the posterior can be used within a decision-making system, and
having the full posterior can be extremely useful and lead to decisions that
are robust to disturbances. For example, in the context of model-based re-
inforcement learning, Deisenroth et al. (2015) show that using the full
posterior distribution of plausible transition functions leads to very fast
(data/sample efficient) learning, whereas focusing on the maximum of
the posterior leads to consistent failures. Therefore, having the full pos-
terior can be very useful for a downstream task. In Chapter 9, we will
continue this discussion in the context of linear regression. }

6.4 Summary Statistics and Independence


We are often interested in summarizing sets of random variables and com-
paring pairs of random variables. A statistic of a random variable is a de-
terministic function of that random variable. The summary statistics of a
distribution provide one useful view of how a random variable behaves,
and as the name suggests, provide numbers that summarize and charac-
terize the distribution. We describe the mean and the variance, two well-
known summary statistics. Then we discuss two ways to compare a pair
of random variables: first, how to say that two random variables are inde-
pendent; and second, how to compute an inner product between them.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.4 Summary Statistics and Independence 187

6.4.1 Means and Covariances


Mean and (co)variance are often useful to describe properties of probabil-
ity distributions (expected values and spread). We will see in Section 6.6
that there is a useful family of distributions (called the exponential fam-
ily), where the statistics of the random variable capture all possible infor-
mation.
The concept of the expected value is central to machine learning, and
the foundational concepts of probability itself can be derived from the
expected value (Whittle, 2000).

Definition 6.3 (Expected Value). The expected value of a function g : R ! expected value
R of a univariate continuous random variable X ⇠ p(x) is given by
Z
EX [g(x)] = g(x)p(x)dx . (6.28)
X

Correspondingly, the expected value of a function g of a discrete random


variable X ⇠ p(x) is given by
X
EX [g(x)] = g(x)p(x) , (6.29)
x2X

where X is the set of possible outcomes (the target space) of the random
variable X .

In this section, we consider discrete random variables to have numerical


outcomes. This can be seen by observing that the function g takes real
numbers as inputs. The expected value
of a function of a
Remark. We consider multivariate random variables X as a finite vector random variable is
of univariate random variables [X1 , . . . , XD ]> . For multivariate random sometimes referred
variables, we define the expected value element wise to as the law of the
unconscious
2 3 statistician (Casella
EX1 [g(x1 )]
6 .. 7 and Berger, 2002,
EX [g(x)] = 4 . 52R ,
D
(6.30) Section 2.2).
EXD [g(xD )]

where the subscript EXd indicates that we are taking the expected value
with respect to the dth element of the vector x. }
Definition 6.3 defines the meaning of the notation EX as the operator
indicating that we should take the integral with respect to the probabil-
ity density (for continuous distributions) or the sum over all states (for
discrete distributions). The definition of the mean (Definition 6.4), is a
special case of the expected value, obtained by choosing g to be the iden-
tity function.

Definition 6.4 (Mean). The mean of a random variable X with states mean

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


188 Probability and Distributions

x 2 RD is an average and is defined as


2 3
EX1 [x1 ]
EX [x] = 4
6 .. 7 D
(6.31)
. 52R ,
EXD [xD ]
where
8 Z
>
>
< xd p(xd )dxd if X is a continuous random variable
Exd [xd ] := X X
>
>
:
xi p(xd = xi ) if X is a discrete random variable
xi 2X
(6.32)
for d = 1, . . . , D , where the subscript d indicates the corresponding di-
mension of x. The integral and sum are over the states X of the target
space of the random variable X .

In one dimension, there are two other intuitive notions of “average”,


median which are the median and the mode. The median is the “middle” value if
we sort the values, i.e., 50% of the values are greater than the median and
50% are smaller than the median. This idea can be generalized to contin-
uous values by considering the value where the cdf (Definition 6.2) is 0.5.
For distributions, which are asymmetric or have long tails, the median
provides an estimate of a typical value that is closer to human intuition
than the mean value. Furthermore, the median is more robust to outliers
than the mean. The generalization of the median to higher dimensions is
non-trivial as there is no obvious way to “sort” in more than one dimen-
mode sion (Hallin et al., 2010; Kong and Mizera, 2012). The mode is the most
frequently occurring value. For a discrete random variable, the mode is
defined as the value of x having the highest frequency of occurrence. For
a continuous random variable, the mode is defined as a peak in the density
p(x). A particular density p(x) may have more than one mode, and fur-
thermore there may be a very large number of modes in high-dimensional
distributions. Therefore, finding all the modes of a distribution can be
computationally challenging.

Example 6.4
Consider the two-dimensional distribution illustrated in Figure 6.4:
✓   ◆ ✓   ◆
10 1 0 0 8.4 2.0
p(x) = 0.4 N x , + 0.6 N x , .
2 0 1 0 2.0 1.7
(6.33)
We will define the Gaussian distribution N µ, 2
in Section 6.5. Also
shown is its corresponding marginal distribution in each dimension. Ob-
serve that the distribution is bimodal (has two modes), but one of the

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.4 Summary Statistics and Independence 189

marginal distributions is unimodal (has one mode). The horizontal bi-


modal univariate distribution illustrates that the mean and median can
be different from each other. While it is tempting to define the two-
dimensional median to be the concatenation of the medians in each di-
mension, the fact that we cannot define an ordering of two-dimensional
points makes it difficult. When we say “cannot define an ordering”, we
mean
 that
 there is more than one way to define the relation < so that
3 2
< .
0 3

Figure 6.4
Mean Illustration of the
Modes mean, mode, and
Median median for a
two-dimensional
dataset, as well as
its marginal
densities.

Remark. The expected value (Definition 6.3) is a linear operator. For ex-
ample, given a real-valued function f (x) = ag(x) + bh(x) where a, b 2 R
and x 2 RD , we obtain
Z
EX [f (x)] = f (x)p(x)dx (6.34a)
Z
= [ag(x) + bh(x)]p(x)dx (6.34b)
Z Z
= a g(x)p(x)dx + b h(x)p(x)dx (6.34c)

= aEX [g(x)] + bEX [h(x)] . (6.34d)

}
For two random variables, we may wish to characterize their correspon-

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


190 Probability and Distributions

dence to each other. The covariance intuitively represents the notion of


how dependent random variables are to one another.
covariance Definition 6.5 (Covariance (Univariate)). The covariance between two
univariate random variables X, Y 2 R is given by the expected product
of their deviations from their respective means, i.e.,
⇥ ⇤
CovX,Y [x, y] := EX,Y (x EX [x])(y EY [y]) . (6.35)
Terminology: The
covariance of Remark. When the random variable associated with the expectation or
multivariate random covariance is clear by its arguments, the subscript is often suppressed (for
variables Cov[x, y] example, EX [x] is often written as E[x]). }
is sometimes
referred to as By using the linearity of expectations, the expression in Definition 6.5
cross-covariance, can be rewritten as the expected value of the product minus the product
with covariance
of the expected values, i.e.,
referring to
Cov[x, x]. Cov[x, y] = E[xy] E[x]E[y] . (6.36)
variance The covariance of a variable with itself Cov[x, x] is called the variance and
standard deviation is denoted by VX [x]. The square root of the variance is called the standard
deviation and is often denoted by (x). The notion of covariance can be
generalized to multivariate random variables.
Definition 6.6 (Covariance (Multivariate)). If we consider two multivari-
ate random variables X and Y with states x 2 RD and y 2 RE respec-
covariance tively, the covariance between X and Y is defined as
Cov[x, y] = E[xy > ] E[x]E[y]> = Cov[y, x]> 2 RD⇥E . (6.37)
Definition 6.6 can be applied with the same multivariate random vari-
able in both arguments, which results in a useful concept that intuitively
captures the “spread” of a random variable. For a multivariate random
variable, the variance describes the relation between individual dimen-
sions of the random variable.
variance Definition 6.7 (Variance). The variance of a random variable X with
states x 2 RD and a mean vector µ 2 RD is defined as
VX [x] = CovX [x, x] (6.38a)
= EX [(x µ)(x µ) ] = EX [xx > >
]EX [x]EX [x] >
(6.38b)
2 3
Cov[x1 , x1 ] Cov[x1 , x2 ] . . . Cov[x1 , xD ]
6 Cov[x2 , x1 ] Cov[x2 , x2 ] . . . Cov[x2 , xD ] 7
6 7
=6 .. .. .. .. 7. (6.38c)
4 . . . . 5
Cov[xD , x1 ] ... ... Cov[xD , xD ]
covariance matrix The D ⇥ D matrix in (6.38c) is called the covariance matrix of the mul-
tivariate random variable X . The covariance matrix is symmetric and pos-
itive semidefinite and tells us something about the spread of the data. On
marginal its diagonal, the covariance matrix contains the variances of the marginals

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.4 Summary Statistics and Independence 191
Figure 6.5
6 6 Two-dimensional
datasets with
4 4
identical means and
2 2 variances along
y

y
each axis (colored
0 0 lines) but with
different
2 2 covariances.
5 0 5 5 0 5
x x

(a) x and y are negatively correlated. (b) x and y are positively correlated.

Z
p(xi ) = p(x1 , . . . , xD )dx\i , (6.39)

where “\i” denotes “all variables but i”. The off-diagonal entries are the
cross-covariance terms Cov[xi , xj ] for i, j = 1, . . . , D, i 6= j . cross-covariance

Remark. In this book, we generally assume that covariance matrices are


positive definite to enable better intuition. We therefore do not discuss
corner cases that result in positive semidefinite (low-rank) covariance ma-
trices. }
When we want to compare the covariances between different pairs of
random variables, it turns out that the variance of each random variable
affects the value of the covariance. The normalized version of covariance
is called the correlation.
Definition 6.8 (Correlation). The correlation between two random vari- correlation
ables X, Y is given by
Cov[x, y]
corr[x, y] = p 2 [ 1, 1] . (6.40)
V[x]V[y]
The correlation matrix is the covariance matrix of standardized random
variables, x/ (x). In other words, each random variable is divided by its
standard deviation (the square root of the variance) in the correlation
matrix.
The covariance (and correlation) indicate how two random variables
are related; see Figure 6.5. Positive correlation corr[x, y] means that when
x grows, then y is also expected to grow. Negative correlation means that
as x increases, then y decreases.

6.4.2 Empirical Means and Covariances


The definitions in Section 6.4.1 are often also called the population mean population mean
and covariance, as it refers to the true statistics for the population. In ma- and covariance
chine learning, we need to learn from empirical observations of data. Con-
sider a random variable X . There are two conceptual steps to go from

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


192 Probability and Distributions

population statistics to the realization of empirical statistics. First, we use


the fact that we have a finite dataset (of size N ) to construct an empirical
statistic that is a function of a finite number of identical random variables,
X1 , . . . , XN . Second, we observe the data, that is, we look at the realiza-
tion x1 , . . . , xN of each of the random variables and apply the empirical
statistic.
Specifically, for the mean (Definition 6.4), given a particular dataset we
empirical mean can obtain an estimate of the mean, which is called the empirical mean or
sample mean sample mean. The same holds for the empirical covariance.
empirical mean Definition 6.9 (Empirical Mean and Covariance). The empirical mean vec-
tor is the arithmetic average of the observations for each variable, and it
is defined as
N
1 X
x̄ := xn , (6.41)
N n=1

where xn 2 RD .
empirical covariance Similar to the empirical mean, the empirical covariance matrix is a D⇥D
matrix
N
1 X
⌃ := (xn x̄)(xn x̄)> . (6.42)
N n=1
Throughout the
book, we use the To compute the statistics for a particular dataset, we would use the
empirical realizations (observations) x1 , . . . , xN and use (6.41) and (6.42). Em-
covariance, which is pirical covariance matrices are symmetric, positive semidefinite (see Sec-
a biased estimate.
The unbiased
tion 3.2.3).
(sometimes called
corrected)
covariance has the 6.4.3 Three Expressions for the Variance
factor N 1 in the
denominator We now focus on a single random variable X and use the preceding em-
instead of N . pirical formulas to derive three possible expressions for the variance. The
The derivations are following derivation is the same for the population variance, except that
exercises at the end we need to take care of integrals. The standard definition of variance, cor-
of this chapter.
responding to the definition of covariance (Definition 6.5), is the expec-
tation of the squared deviation of a random variable X from its expected
value µ, i.e.,
VX [x] := EX [(x µ)2 ] . (6.43)
The expectation in (6.43) and the mean µ = EX (x) are computed us-
ing (6.32), depending on whether X is a discrete or continuous random
variable. The variance as expressed in (6.43) is the mean of a new random
variable Z := (X µ)2 .
When estimating the variance in (6.43) empirically, we need to resort
to a two-pass algorithm: one pass through the data to calculate the mean
µ using (6.41), and then a second pass using this estimate µ̂ calculate the

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.4 Summary Statistics and Independence 193

variance. It turns out that we can avoid two passes by rearranging the
terms. The formula in (6.43) can be converted to the so-called raw-score raw-score formula
formula for variance: for variance

2
VX [x] = EX [x2 ] (EX [x]) . (6.44)
The expression in (6.44) can be remembered as “the mean of the square
minus the square of the mean”. It can be calculated empirically in one pass
through data since we can accumulate xi (to calculate the mean) and x2i
simultaneously, where xi is the ith observation. Unfortunately, if imple- If the two terms
mented in this way, it can be numerically unstable. The raw-score version in (6.44) are huge
and approximately
of the variance can be useful in machine learning, e.g., when deriving the
equal, we may
bias–variance decomposition (Bishop, 2006). suffer from an
A third way to understand the variance is that it is a sum of pairwise dif- unnecessary loss of
ferences between all pairs of observations. Consider a sample x1 , . . . , xN numerical precision
in floating-point
of realizations of random variable X , and we compute the squared differ-
arithmetic.
ence between pairs of xi and xj . By expanding the square, we can show
that the sum of N 2 pairwise differences is the empirical variance of the
observations:
2 !2 3
N
X XN XN
1 1 1
2
(xi xj )2 = 2 4 x2i xi 5 . (6.45)
N i,j=1 N i=1 N i=1

We see that (6.45) is twice the raw-score expression (6.44). This means
that we can express the sum of pairwise distances (of which there are N 2
of them) as a sum of deviations from the mean (of which there are N ). Ge-
ometrically, this means that there is an equivalence between the pairwise
distances and the distances from the center of the set of points. From a
computational perspective, this means that by computing the mean (N
terms in the summation), and then computing the variance (again N
terms in the summation), we can obtain an expression (left-hand side
of (6.45)) that has N 2 terms.

6.4.4 Sums and Transformations of Random Variables


We may want to model a phenomenon that cannot be well explained by
textbook distributions (we introduce some in Sections 6.5 and 6.6), and
hence may perform simple manipulations of random variables (such as
adding two random variables).
Consider two random variables X, Y with states x, y 2 RD . Then:
E[x + y] = E[x] + E[y] (6.46)
E[x y] = E[x] E[y] (6.47)
V[x + y] = V[x] + V[y] + Cov[x, y] + Cov[y, x] (6.48)
V[x y] = V[x] + V[y] Cov[x, y] Cov[y, x] . (6.49)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


194 Probability and Distributions

Mean and (co)variance exhibit some useful properties when it comes


to affine transformation of random variables. Consider a random variable
X with mean µ and covariance matrix ⌃ and a (deterministic) affine
transformation y = Ax + b of x. Then y is itself a random variable
whose mean vector and covariance matrix are given by
EY [y] = EX [Ax + b] = AEX [x] + b = Aµ + b , (6.50)
> >
VY [y] = VX [Ax + b] = VX [Ax] = AVX [x]A = A⌃A , (6.51)
This can be shown respectively. Furthermore,
directly by using the
definition of the Cov[x, y] = E[x(Ax + b)> ] E[x]E[Ax + b]> (6.52a)
mean and
covariance.
= E[x]b> + E[xx> ]A> µb> µµ> A> (6.52b)
> > >
= µb µb + E[xx ] >
µµ >
A (6.52c)
(6.38b)
= ⌃A> , (6.52d)
where ⌃ = E[xx> ] µµ> is the covariance of X .

6.4.5 Statistical Independence


statistical Definition 6.10 (Independence). Two random variables X, Y are statis-
independence tically independent if and only if
p(x, y) = p(x)p(y) . (6.53)
Intuitively, two random variables X and Y are independent if the value
of y (once known) does not add any additional information about x (and
vice versa). If X, Y are (statistically) independent, then
p(y | x) = p(y)
p(x | y) = p(x)
VX,Y [x + y] = VX [x] + VY [y]
CovX,Y [x, y] = 0
The last point may not hold in converse, i.e., two random variables can
have covariance zero but are not statistically independent. To understand
why, recall that covariance measures only linear dependence. Therefore,
random variables that are nonlinearly dependent could have covariance
zero.

Example 6.5
Consider a random variable X with zero mean (EX [x] = 0) and also
EX [x3 ] = 0. Let y = x2 (hence, Y is dependent on X ) and consider the
covariance (6.36) between X and Y . But this gives
Cov[x, y] = E[xy] E[x]E[y] = E[x3 ] = 0 . (6.54)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.4 Summary Statistics and Independence 195

In machine learning, we often consider problems that can be mod-


eled as independent and identically distributed (i.i.d.) random variables, independent and
X1 , . . . , XN . For more than two random variables, the word “indepen- identically
distributed
dent” (Definition 6.10) usually refers to mutually independent random
i.i.d.
variables, where all subsets are independent (see Pollard (2002, chap-
ter 4) and Jacod and Protter (2004, chapter 3)). The phrase “identically
distributed” means that all the random variables are from the same distri-
bution.
Another concept that is important in machine learning is conditional
independence.
Definition 6.11 (Conditional Independence). Two random variables X
and Y are conditionally independent given Z if and only if conditionally
independent
p(x, y | z) = p(x | z)p(y | z) for all z 2 Z , (6.55)
where Z is the set of states of random variable Z . We write X ?
? Y | Z to
denote that X is conditionally independent of Y given Z .
Definition 6.11 requires that the relation in (6.55) must hold true for
every value of z . The interpretation of (6.55) can be understood as “given
knowledge about z , the distribution of x and y factorizes”. Independence
can be cast as a special case of conditional independence if we write X ? ?
Y | ;. By using the product rule of probability (6.22), we can expand the
left-hand side of (6.55) to obtain
p(x, y | z) = p(x | y, z)p(y | z) . (6.56)
By comparing the right-hand side of (6.55) with (6.56), we see that p(y | z)
appears in both of them so that
p(x | y, z) = p(x | z) . (6.57)
Equation (6.57) provides an alternative definition of conditional indepen-
dence, i.e., X ?? Y | Z . This alternative presentation provides the inter-
pretation “given that we know z , knowledge about y does not change our
knowledge of x”.

6.4.6 Inner Products of Random Variables


Recall the definition of inner products from Section 3.2. We can define an Inner products
inner product between random variables, which we briefly describe in this between
multivariate random
section. If we have two uncorrelated random variables X, Y , then
variables can be
V[x + y] = V[x] + V[y] . (6.58) treated in a similar
fashion
Since variances are measured in squared units, this looks very much like
the Pythagorean theorem for right triangles c2 = a2 + b2 .
In the following, we see whether we can find a geometric interpreta-
tion of the variance relation of uncorrelated random variables in (6.58).

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


196 Probability and Distributions

Figure 6.6
Geometry of
random variables. If
random variables X
and Y are
uncorrelated, they
are orthogonal
vectors in a
corresponding
vector space, and [ y]
var
the Pythagorean [ x]
+
p var
theorem applies. p
] = a var[x]
+y c
p va r[ x
b
p
var[y]

Random variables can be considered vectors in a vector space, and we


can define inner products to obtain geometric properties of random vari-
ables (Eaton, 2007). If we define
hX, Y i := Cov[x, y] (6.59)
for zero mean random variables X and Y , we obtain an inner product. We
Cov[x, x] = 0 () see that the covariance is symmetric, positive definite, and linear in either
x=0 argument. The length of a random variable is
Cov[↵x + z, y] = q q
↵ Cov[x, y] +
kXk = Cov[x, x] = V[x] = [x] , (6.60)
Cov[z, y] for ↵ 2 R.
i.e., its standard deviation. The “longer” the random variable, the more
uncertain it is; and a random variable with length 0 is deterministic.
If we look at the angle ✓ between two random variables X, Y , we get
hX, Y i Cov[x, y]
cos ✓ = =p , (6.61)
kXk kY k V[x]V[y]
which is the correlation (Definition 6.8) between the two random vari-
ables. This means that we can think of correlation as the cosine of the
angle between two random variables when we consider them geometri-
cally. We know from Definition 3.7 that X ? Y () hX, Y i = 0. In our
case, this means that X and Y are orthogonal if and only if Cov[x, y] = 0,
i.e., they are uncorrelated. Figure 6.6 illustrates this relationship.
Remark. While it is tempting to use the Euclidean distance (constructed

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.5 Gaussian Distribution 197

Figure 6.7
Gaussian
distribution of two
random variables x1
0.20 and x2 .

p(x1, x2)
0.15
0.10
0.05
0.00

7.5
5.0
2.5
1 0.0 x 2
0 2.5
x1 1 5.0

from the preceding definition of inner products) to compare probability


distributions, it is unfortunately not the best way to obtain distances be-
tween distributions. Recall that the probability mass (or density) is posi-
tive and needs to add up to 1. These constraints mean that distributions
live on something called a statistical manifold. The study of this space of
probability distributions is called information geometry. Computing dis-
tances between distributions are often done using Kullback-Leibler diver-
gence, which is a generalization of distances that account for properties of
the statistical manifold. Just like the Euclidean distance is a special case of
a metric (Section 3.3), the Kullback-Leibler divergence is a special case of
two more general classes of divergences called Bregman divergences and
f -divergences. The study of divergences is beyond the scope of this book,
and we refer for more details to the recent book by Amari (2016), one of
the founders of the field of information geometry. }

6.5 Gaussian Distribution


The Gaussian distribution is the most well-studied probability distribution
for continuous-valued random variables. It is also referred to as the normal normal distribution
distribution. Its importance originates from the fact that it has many com- The Gaussian
putationally convenient properties, which we will be discussing in the fol- distribution arises
naturally when we
lowing. In particular, we will use it to define the likelihood and prior for
consider sums of
linear regression (Chapter 9), and consider a mixture of Gaussians for independent and
density estimation (Chapter 11). identically
There are many other areas of machine learning that also benefit from distributed random
variables. This is
using a Gaussian distribution, for example Gaussian processes, variational
known as the
inference, and reinforcement learning. It is also widely used in other ap- central limit
plication areas such as signal processing (e.g., Kalman filter), control (e.g., theorem (Grinstead
linear quadratic regulator), and statistics (e.g., hypothesis testing). and Snell, 1997).

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


198 Probability and Distributions
Figure 6.8
8
Gaussian p(x)
Mean
0.20 Sample
distributions Mean 6
overlaid with 100 0.15
Sample
4
2
samples. (a) One-

x2
2
dimensional case; 0.10
(b) two-dimensional 0
0.05
case. 2
0.00
4
5.0 2.5 0.0 2.5 5.0 7.5 1 0 1
x x1

(a) Univariate (one-dimensional) Gaussian; (b) Multivariate (two-dimensional) Gaus-


The red cross shows the mean and the red sian, viewed from top. The red cross shows
line shows the extent of the variance. the mean and the colored lines show the con-
tour lines of the density.

For a univariate random variable, the Gaussian distribution has a den-


sity that is given by
✓ ◆
1 (x µ)2
2
p(x | µ, ) = p exp . (6.62)
2⇡ 2 2 2
multivariate The multivariate Gaussian distribution is fully characterized by a mean
Gaussian vector µ and a covariance matrix ⌃ and defined as
distribution
mean vector D 1
covariance matrix
p(x | µ, ⌃) = (2⇡) 2 |⌃| 2 exp 1
2
(x µ)> ⌃ 1 (x µ) , (6.63)

Also known as a where x 2 RD . We write p(x) = N x | µ, ⌃ or X ⇠ N µ, ⌃ . Fig-


multivariate normal ure 6.7 shows a bivariate Gaussian (mesh), with the corresponding con-
distribution.
tour plot. Figure 6.8 shows a univariate Gaussian and a bivariate Gaussian
with corresponding samples. The special case of the Gaussian with zero
mean and identity covariance, that is, µ = 0 and ⌃ = I , is referred to as
standard normal the standard normal distribution.
distribution Gaussians are widely used in statistical estimation and machine learn-
ing as they have closed-form expressions for marginal and conditional dis-
tributions. In Chapter 9, we use these closed-form expressions extensively
for linear regression. A major advantage of modeling with Gaussian ran-
dom variables is that variable transformations (Section 6.7) are often not
needed. Since the Gaussian distribution is fully specified by its mean and
covariance, we often can obtain the transformed distribution by applying
the transformation to the mean and covariance of the random variable.

6.5.1 Marginals and Conditionals of Gaussians are Gaussians


In the following, we present marginalization and conditioning in the gen-
eral case of multivariate random variables. If this is confusing at first read-
ing, the reader is advised to consider two univariate random variables in-
stead. Let X and Y be two multivariate random variables, that may have

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.5 Gaussian Distribution 199

different dimensions. To consider the effect of applying the sum rule of


probability and the effect of conditioning, we explicitly write the Gaus-
sian distribution in terms of the concatenated states [x, y]> ,
✓  ◆
µx ⌃xx ⌃xy
p(x, y) = N , . (6.64)
µy ⌃yx ⌃yy
where ⌃xx = Cov[x, x] and ⌃yy = Cov[y, y] are the marginal covari-
ance matrices of x and y , respectively, and ⌃xy = Cov[x, y] is the cross-
covariance matrix between x and y .
The conditional distribution p(x | y) is also Gaussian (illustrated in Fig-
ure 6.9(c)) and given by (derived in Section 2.3 of Bishop, 2006)
p(x | y) = N µx | y , ⌃x | y (6.65)
µx | y = µx + ⌃xy ⌃yy1 (y µy ) (6.66)
⌃x | y = ⌃xx ⌃xy ⌃yy1 ⌃yx . (6.67)
Note that in the computation of the mean in (6.66), the y -value is an
observation and no longer random.
Remark. The conditional Gaussian distribution shows up in many places,
where we are interested in posterior distributions:
The Kalman filter (Kalman, 1960), one of the most central algorithms
for state estimation in signal processing, does nothing but computing
Gaussian conditionals of joint distributions (Deisenroth and Ohlsson,
2011; Särkkä, 2013).
Gaussian processes (Rasmussen and Williams, 2006), which are a prac-
tical implementation of a distribution over functions. In a Gaussian pro-
cess, we make assumptions of joint Gaussianity of random variables. By
(Gaussian) conditioning on observed data, we can determine a poste-
rior distribution over functions.
Latent linear Gaussian models (Roweis and Ghahramani, 1999; Mur-
phy, 2012), which include probabilistic principal component analysis
(PPCA) (Tipping and Bishop, 1999). We will look at PPCA in more de-
tail in Section 10.7.
}
The marginal distribution p(x) of a joint Gaussian distribution p(x, y)
(see (6.64)) is itself Gaussian and computed by applying the sum rule
(6.20) and given by
Z
p(x) = p(x, y)dy = N x | µx , ⌃xx . (6.68)

The corresponding result holds for p(y), which is obtained by marginaliz-


ing with respect to x. Intuitively, looking at the joint distribution in (6.64),
we ignore (i.e., integrate out) everything we are not interested in. This is
illustrated in Figure 6.9(b).

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


200 Probability and Distributions

Example 6.6
Figure 6.9
(a) Bivariate 8
Gaussian;
6
(b) marginal of a
joint Gaussian 4
distribution is

x2
2
Gaussian; (c) the
conditional 0 x2 = 1
distribution of a 2
Gaussian is also
Gaussian. 4
1 0 1
x1

(a) Bivariate Gaussian.

p(x1) 1.2 p(x1|x2 = 1)

0.6 Mean Mean


1.0
2 2
0.8
0.4
0.6

0.4
0.2
0.2

0.0 0.0
1.5 1.0 0.5 0.0 0.5 1.0 1.5 1.5 1.0 0.5 0.0 0.5 1.0 1.5
x1 x1

(b) Marginal distribution. (c) Conditional distribution.

Consider the bivariate Gaussian distribution (illustrated in Figure 6.9):


✓  ◆
0 0.3 1
p(x1 , x2 ) = N , . (6.69)
2 1 5
We can compute the parameters of the univariate Gaussian, conditioned
on x2 = 1, by applying (6.66) and (6.67) to obtain the mean and vari-
ance respectively. Numerically, this is
µ x1 | x2 = 1 = 0 + ( 1) · 0.2 · ( 1 2) = 0.6 (6.70)
and
2
x1 | x2 = 1 = 0.3 ( 1) · 0.2 · ( 1) = 0.1 . (6.71)
Therefore, the conditional Gaussian is given by
p(x1 | x2 = 1) = N 0.6, 0.1 . (6.72)
The marginal distribution p(x1 ), in contrast, can be obtained by apply-
ing (6.68), which is essentially using the mean and variance of the random
variable x1 , giving us
p(x1 ) = N 0, 0.3 . (6.73)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.5 Gaussian Distribution 201

6.5.2 Product of Gaussian Densities


For linear regression (Chapter 9), we need to compute a Gaussian likeli-
hood. Furthermore, we may wish to assume a Gaussian prior (Section 9.3).
We apply Bayes’ Theorem to compute the posterior, which results in a mul-
tiplication of the likelihood and the prior, that is, the multiplication of two
Gaussian densities. The product of two Gaussians N x | a, A N x | b, B The derivation is an
is a Gaussian distribution scaled by a c 2 R, given by c N x | c, C with exercise at the end
of this chapter.
1 1
C = (A +B ) 1
(6.74)
1 1
c = C(A a + B b) (6.75)
D 1
c = (2⇡) 2 |A + B| 2 exp 1
2
(a b)> (A + B) 1 (a b) . (6.76)
The scaling constant c itself can be written in the form of a Gaussian
density either in a or in b with an “inflated” covariance matrix A + B ,
i.e., c = N a | b, A + B = N b | a, A + B .
Remark. For notation convenience, we will sometimes use N x | m, S
to describe the functional form of a Gaussian density even if x is not a
random variable. We have just done this in the preceding demonstration
when we wrote
c = N a | b, A + B = N b | a, A + B . (6.77)
Here, neither a nor b are random variables. However, writing c in this way
is more compact than (6.76). }

6.5.3 Sums and Linear Transformations


If X, Y are independent Gaussian random variables (i.e., the joint distri-
bution is given as p(x, y) = p(x)p(y)) with p(x) = N x | µx , ⌃x and
p(y) = N y | µy , ⌃y , then x + y is also Gaussian distributed and given
by
p(x + y) = N µx + µy , ⌃x + ⌃y . (6.78)
Knowing that p(x + y) is Gaussian, the mean and covariance matrix can
be determined immediately using the results from (6.46) through (6.49).
This property will be important when we consider i.i.d. Gaussian noise
acting on random variables, as is the case for linear regression (Chap-
ter 9).

Example 6.7
Since expectations are linear operations, we can obtain the weighted sum
of independent Gaussian random variables
p(ax + by) = N aµx + bµy , a2 ⌃x + b2 ⌃y . (6.79)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


202 Probability and Distributions

Remark. A case that will be useful in Chapter 11 is the weighted sum of


Gaussian densities. This is different from the weighted sum of Gaussian
random variables. }
In Theorem 6.12, the random variable x is from a density that is a
mixture of two densities p1 (x) and p2 (x), weighted by ↵. The theorem can
be generalized to the multivariate random variable case, since linearity of
expectations holds also for multivariate random variables. However, the
idea of a squared random variable needs to be replaced by xx> .

Theorem 6.12. Consider a mixture of two univariate Gaussian densities

p(x) = ↵p1 (x) + (1 ↵)p2 (x) , (6.80)

where the scalar 0 < ↵ < 1 is the mixture weight, and p1 (x) and p2 (x) are
univariate Gaussian densities (Equation (6.62)) with different parameters,
i.e., (µ1 , 12 ) 6= (µ2 , 22 ).
Then the mean of the mixture density p(x) is given by the weighted sum
of the means of each random variable:

E[x] = ↵µ1 + (1 ↵)µ2 . (6.81)

The variance of the mixture density p(x) is given by


⇥ ⇤ ⇣⇥ ⇤ 2

V[x] = ↵ 12 + (1 ↵) 22 + ↵µ21 + (1 ↵)µ22 [↵µ1 + (1 ↵)µ2 ] .
(6.82)

Proof The mean of the mixture density p(x) is given by the weighted
sum of the means of each random variable. We apply the definition of the
mean (Definition 6.4), and plug in our mixture (6.80), which yields
Z 1
E[x] = xp(x)dx (6.83a)
1
Z 1
= ↵xp1 (x) + (1 ↵)xp2 (x)dx (6.83b)
1
Z 1 Z 1
=↵ xp1 (x)dx + (1 ↵) xp2 (x)dx (6.83c)
1 1
= ↵µ1 + (1 ↵)µ2 . (6.83d)

To compute the variance, we can use the raw-score version of the vari-
ance from (6.44), which requires an expression of the expectation of the
squared random variable. Here we use the definition of an expectation of
a function (the square) of a random variable (Definition 6.3),
Z 1
2
E[x ] = x2 p(x)dx (6.84a)
1
Z 1
= ↵x2 p1 (x) + (1 ↵)x2 p2 (x)dx (6.84b)
1

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.5 Gaussian Distribution 203
Z 1 Z 1
=↵ x2 p1 (x)dx + (1 ↵) x2 p2 (x)dx (6.84c)
1 1
= ↵(µ21 + 2
1) + (1 ↵)(µ22 + 2
2) , (6.84d)
where in the last equality, we again used the raw-score version of the
variance (6.44) giving 2 = E[x2 ] µ2 . This is rearranged such that the
expectation of a squared random variable is the sum of the squared mean
and the variance.
Therefore, the variance is given by subtracting (6.83d) from (6.84d),
V[x] = E[x2 ] (E[x])2 (6.85a)
= ↵(µ1 + 1 ) + (1 ↵)(µ2 + 2 ) (↵µ1 + (1 ↵)µ2 ) (6.85b)
2 2 2 2 2
⇥ ⇤
= ↵ 12 + (1 ↵) 22
⇣⇥ ⇤ ⌘
2
+ ↵µ21 + (1 ↵)µ22 [↵µ1 + (1 ↵)µ2 ] . (6.85c)

Remark. The preceding derivation holds for any density, but since the
Gaussian is fully determined by the mean and variance, the mixture den-
sity can be determined in closed form. }
For a mixture density, the individual components can be considered
to be conditional distributions (conditioned on the component identity).
Equation (6.85c) is an example of the conditional variance formula, also
known as the law of total variance, which generally states that for two ran- law of total variance
dom variables X and Y it holds that VX [x] = EY [VX [x|y]]+ VY [EX [x|y]],
i.e., the (total) variance of X is the expected conditional variance plus the
variance of a conditional mean.
We consider in Example 6.17 a bivariate standard Gaussian random
variable X and performed a linear transformation Ax on it. The outcome
is a Gaussian random variable with mean zero and covariance AA> . Ob-
serve that adding a constant vector will change the mean of the distribu-
tion, without affecting its variance, that is, the random variable x + µ is
Gaussian with mean µ and identity covariance. Hence, any linear/affine
transformation of a Gaussian random variable is Gaussian distributed. Any linear/affine
Consider a Gaussian distributed random variable X ⇠ N µ, ⌃ . For transformation of a
Gaussian random
a given matrix A of appropriate shape, let Y be a random variable such
variable is also
that y = Ax is a transformed version of x. We can compute the mean of Gaussian
y by exploiting that the expectation is a linear operator (6.50) as follows: distributed.

E[y] = E[Ax] = AE[x] = Aµ . (6.86)


Similarly the variance of y can be found by using (6.51):
V[y] = V[Ax] = AV[x]A> = A⌃A> . (6.87)
This means that the random variable y is distributed according to
p(y) = N y | Aµ, A⌃A> . (6.88)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


204 Probability and Distributions

Let us now consider the reverse transformation: when we know that a


random variable has a mean that is a linear transformation of another
random variable. For a given full rank matrix A 2 RM ⇥N , where M > N ,
let y 2 RM be a Gaussian random variable with mean Ax, i.e.,

p(y) = N y | Ax, ⌃ . (6.89)

What is the corresponding probability distribution p(x)? If A is invert-


ible, then we can write x = A 1 y and apply the transformation in the
previous paragraph. However, in general A is not invertible, and we use
an approach similar to that of the pseudo-inverse (3.57). That is, we pre-
multiply both sides with A> and then invert A> A, which is symmetric
and positive definite, giving us the relation

y = Ax () (A> A) 1 A> y = x . (6.90)

Hence, x is a linear transformation of y , and we obtain

p(x) = N x | (A> A) 1 A> y, (A> A) 1 A> ⌃A(A> A) 1


. (6.91)

6.5.4 Sampling from Multivariate Gaussian Distributions


We will not explain the subtleties of random sampling on a computer, and
the interested reader is referred to Gentle (2004). In the case of a mul-
tivariate Gaussian, this process consists of three stages: first, we need a
source of pseudo-random numbers that provide a uniform sample in the
interval [0,1]; second, we use a non-linear transformation such as the
Box-Müller transform (Devroye, 1986) to obtain a sample from a univari-
ate Gaussian; and third, we collate a vector of these samples to obtain a
sample from a multivariate standard normal N 0, I .
For a general multivariate Gaussian, that is, where the mean is non
zero and the covariance is not the identity matrix, we use the proper-
ties of linear transformations of a Gaussian random variable. Assume we
are interested in generating samples xi , i = 1, . . . , n, from a multivariate
To compute the Gaussian distribution with mean µ and covariance matrix ⌃. We would
Cholesky like to construct the sample from a sampler that provides samples from
factorization of a
the multivariate standard normal N 0, I .
matrix, it is required
that the matrix is To obtain samples from a multivariate normal N µ, ⌃ , we can use
symmetric and the properties of a linear transformation of a Gaussian random variable:
positive definite If x ⇠ N 0, I , then y = Ax + µ, where AA> = ⌃ is Gaussian dis-
(Section 3.2.3).
Covariance matrices
tributed with mean µ and covariance matrix ⌃. One convenient choice of
possess this A is to use the Cholesky decomposition (Section 4.3) of the covariance
property. matrix ⌃ = AA> . The Cholesky decomposition has the benefit that A is
triangular, leading to efficient computation.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.6 Conjugacy and the Exponential Family 205

6.6 Conjugacy and the Exponential Family


Many of the probability distributions “with names” that we find in statis-
tics textbooks were discovered to model particular types of phenomena.
For example, we have seen the Gaussian distribution in Section 6.5. The
distributions are also related to each other in complex ways (Leemis and
McQueston, 2008). For a beginner in the field, it can be overwhelming to
figure out which distribution to use. In addition, many of these distribu-
tions were discovered at a time that statistics and computation were done “Computers” used to
by pencil and paper. It is natural to ask what are meaningful concepts be a job description.
in the computing age (Efron and Hastie, 2016). In the previous section,
we saw that many of the operations required for inference can be conve-
niently calculated when the distribution is Gaussian. It is worth recalling
at this point the desiderata for manipulating probability distributions in
the machine learning context:

1. There is some “closure property” when applying the rules of probability,


e.g., Bayes’ theorem. By closure, we mean that applying a particular
operation returns an object of the same type.
2. As we collect more data, we do not need more parameters to describe
the distribution.
3. Since we are interested in learning from data, we want parameter es-
timation to behave nicely.

It turns out that the class of distributions called the exponential family exponential family
provides the right balance of generality while retaining favorable compu-
tation and inference properties. Before we introduce the exponential fam-
ily, let us see three more members of “named” probability distributions,
the Bernoulli (Example 6.8), Binomial (Example 6.9), and Beta (Exam-
ple 6.10) distributions.

Example 6.8
The Bernoulli distribution is a distribution for a single binary random Bernoulli
variable X with state x 2 {0, 1}. It is governed by a single continuous pa- distribution
rameter µ 2 [0, 1] that represents the probability of X = 1. The Bernoulli
distribution Ber(µ) is defined as
p(x | µ) = µx (1 µ)1 x
, x 2 {0, 1} , (6.92)
E[x] = µ , (6.93)
V[x] = µ(1 µ) , (6.94)
where E[x] and V[x] are the mean and variance of the binary random
variable X .

An example where the Bernoulli distribution can be used is when we


are interested in modeling the probability of “heads” when flipping a coin.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


206 Probability and Distributions

Figure 6.10
Examples of the µ = 0.1
Binomial 0.3 µ = 0.4
distribution for
µ 2 {0.1, 0.4, 0.75} µ = 0.75
and N = 15.
0.2
p(m)

0.1

0.0
0.0 2.5 5.0 7.5 10.0 12.5 15.0
Number m of observations x = 1 in N = 15 experiments

Remark. The rewriting above of the Bernoulli distribution, where we use


Boolean variables as numerical 0 or 1 and express them in the exponents,
is a trick that is often used in machine learning textbooks. Another oc-
curence of this is when expressing the Multinomial distribution. }

Example 6.9 (Binomial Distribution)


Binomial The Binomial distribution is a generalization of the Bernoulli distribution
distribution to a distribution over integers (illustrated in Figure 6.10). In particular,
the Binomial can be used to describe the probability of observing m oc-
currences of X = 1 in a set of N samples from a Bernoulli distribution
where p(X = 1) = µ 2 [0, 1]. The Binomial distribution Bin(N, µ) is
defined as
!
N m
p(m | N, µ) = µ (1 µ)N m , (6.95)
m
E[m] = N µ , (6.96)
V[m] = N µ(1 µ) , (6.97)
where E[m] and V[m] are the mean and variance of m, respectively.

An example where the Binomial could be used is if we want to describe


the probability of observing m “heads” in N coin-flip experiments if the
probability for observing head in a single experiment is µ.

Example 6.10 (Beta Distribution)


We may wish to model a continuous random variable on a finite interval.
Beta distribution The Beta distribution is a distribution over a continuous random variable
µ 2 [0, 1], which is often used to represent the probability for some binary
event (e.g., the parameter governing the Bernoulli distribution). The Beta

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.6 Conjugacy and the Exponential Family 207

distribution Beta(↵, ) (illustrated in Figure 6.11) itself is governed by


two parameters ↵ > 0, > 0 and is defined as
(↵ + ) ↵ 1
p(µ | ↵, ) = µ (1 µ) 1 (6.98)
(↵) ( )
↵ ↵
E[µ] = , V[µ] = 2
(6.99)
↵+ (↵ + ) (↵ + + 1)
where (·) is the Gamma function defined as
Z 1
(t) := xt 1 exp( x)dx, t > 0. (6.100)
0
(t + 1) = t (t) . (6.101)
Note that the fraction of Gamma functions in (6.98) normalizes the Beta
distribution.

10 Figure 6.11
↵ = 0.5 = Examples of the
8 ↵=1= Beta distribution for
↵ = 2, = 0.3 different values of ↵
and .
p(µ|↵, )

6 ↵ = 4, = 10
↵ = 5, = 1
4

0
0.0 0.2 0.4 0.6 0.8 1.0
µ

Intuitively, ↵ moves probability mass toward 1, whereas moves prob-


ability mass toward 0. There are some special cases (Murphy, 2012):

For ↵ = 1 = , we obtain the uniform distribution U [0, 1].


For ↵, < 1, we get a bimodal distribution with spikes at 0 and 1.
For ↵, > 1, the distribution is unimodal.
For ↵, > 1 and ↵ = , the distribution is unimodal, symmetric, and
centered in the interval [0, 1], i.e., the mode/mean is at 12 .

Remark. There is a whole zoo of distributions with names, and they are
related in different ways to each other (Leemis and McQueston, 2008).
It is worth keeping in mind that each named distribution is created for a
particular reason, but may have other applications. Knowing the reason
behind the creation of a particular distribution often allows insight into
how to best use it. We introduced preceding three distributions to be able

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


208 Probability and Distributions

to illustrate the concepts of conjugacy (Section 6.6.1) and exponential


families (Section 6.6.3). }

6.6.1 Conjugacy
According to Bayes’ theorem (6.23), the posterior is proportional to the
product of the prior and the likelihood. The specification of the prior can
be tricky for two reasons: First, the prior should encapsulate our knowl-
edge about the problem before we see any data. This is often difficult to
describe. Second, it is often not possible to compute the posterior distribu-
tion analytically. However, there are some priors that are computationally
conjugate prior convenient: conjugate priors.
conjugate Definition 6.13 (Conjugate Prior). A prior is conjugate for the likelihood
function if the posterior is of the same form/type as the prior.
Conjugacy is particularly convenient because we can algebraically cal-
culate our posterior distribution by updating the parameters of the prior
distribution.
Remark. When considering the geometry of probability distributions, con-
jugate priors retain the same distance structure as the likelihood (Agarwal
and Daumé III, 2010). }
To introduce a concrete example of conjugate priors, we describe in Ex-
ample 6.11 the Binomial distribution (defined on discrete random vari-
ables) and the Beta distribution (defined on continuous random vari-
ables).

Example 6.11 (Beta-Binomial Conjugacy)


Consider a Binomial random variable x ⇠ Bin(N, µ) where
!
N x
p(x | N, µ) = µ (1 µ)N x , x = 0, 1, . . . , N , (6.102)
x
is the probability of finding x times the outcome “heads” in N coin flips,
where µ is the probability of a “head”. We place a Beta prior on the pa-
rameter µ, that is, µ ⇠ Beta(↵, ), where
(↵ + ) ↵ 1
p(µ | ↵, ) = µ (1 µ) 1
. (6.103)
(↵) ( )
If we now observe some outcome x = h, that is, we see h heads in N coin
flips, we compute the posterior distribution on µ as
p(µ | x = h, N, ↵, ) / p(x | N, µ)p(µ | ↵, ) (6.104a)
h
/ µ (1 µ) (N h)
µ ↵ 1
(1 µ) 1
(6.104b)
= µh+↵ 1 (1 µ)(N h)+ 1
(6.104c)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.6 Conjugacy and the Exponential Family 209
Likelihood Conjugate prior Posterior Table 6.2 Examples
Bernoulli Beta Beta of conjugate priors
Binomial Beta Beta for common
Gaussian Gaussian/inverse Gamma Gaussian/inverse Gamma likelihood functions.
Gaussian Gaussian/inverse Wishart Gaussian/inverse Wishart
Multinomial Dirichlet Dirichlet

/ Beta(h + ↵, N h + ), (6.104d)
i.e., the posterior distribution is a Beta distribution as the prior, i.e., the
Beta prior is conjugate for the parameter µ in the Binomial likelihood
function.

In the following example, we will derive a result that is similar to the


Beta-Binomial conjugacy result. Here we will show that the Beta distribu-
tion is a conjugate prior for the Bernoulli distribution.

Example 6.12 (Beta-Bernoulli Conjugacy)


Let x 2 {0, 1} be distributed according to the Bernoulli distribution with
parameter ✓ 2 [0, 1], that is, p(x = 1 | ✓) = ✓. This can also be expressed
as p(x | ✓) = ✓x (1 ✓)1 x . Let ✓ be distributed according to a Beta distri-
bution with parameters ↵, , that is, p(✓ | ↵, ) / ✓↵ 1 (1 ✓) 1 .
Multiplying the Beta and the Bernoulli distributions, we get
p(✓ | x, ↵, ) = p(x | ✓)p(✓ | ↵, ) (6.105a)
x
/ ✓ (1 ✓) 1 x ↵ 1
✓ (1 ✓) 1
(6.105b)
=✓ ↵+x 1
(1 ✓) +(1 x) 1
(6.105c)
/ p(✓ | ↵ + x, + (1 x)) . (6.105d)
The last line is the Beta distribution with parameters (↵ + x, + (1 x)).

Table 6.2 lists examples for conjugate priors for the parameters of some
standard likelihoods used in probabilistic modeling. Distributions such as The Gamma prior is
Multinomial, inverse Gamma, inverse Wishart, and Dirichlet can be found conjugate for the
precision (inverse
in any statistical text, and are described in Bishop (2006), for example.
variance) in the
The Beta distribution is the conjugate prior for the parameter µ in both univariate Gaussian
the Binomial and the Bernoulli likelihood. For a Gaussian likelihood func- likelihood, and the
tion, we can place a conjugate Gaussian prior on the mean. The reason Wishart prior is
conjugate for the
why the Gaussian likelihood appears twice in the table is that we need
precision matrix
to distinguish the univariate from the multivariate case. In the univariate (inverse covariance
(scalar) case, the inverse Gamma is the conjugate prior for the variance. matrix) in the
In the multivariate case, we use a conjugate inverse Wishart distribution multivariate
Gaussian likelihood.
as a prior on the covariance matrix. The Dirichlet distribution is the conju-

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


210 Probability and Distributions

gate prior for the multinomial likelihood function. For further details, we
refer to Bishop (2006).

6.6.2 Sufficient Statistics


Recall that a statistic of a random variable is a deterministic function of
that random variable. For example, if x = [x1 , . . . , xN ]> is a vector of
univariate Gaussian random variables, that is, xn ⇠ N µ, 2 , then the
sample mean µ̂ = N1 (x1 + · · · + xN ) is a statistic. Sir Ronald Fisher dis-
sufficient statistics covered the notion of sufficient statistics: the idea that there are statistics
that will contain all available information that can be inferred from data
corresponding to the distribution under consideration. In other words, suf-
ficient statistics carry all the information needed to make inference about
the population, that is, they are the statistics that are sufficient to repre-
sent the distribution.
For a set of distributions parametrized by ✓, let X be a random variable
with distribution p(x | ✓0 ) given an unknown ✓0 . A vector (x) of statistics
is called sufficient statistics for ✓0 if they contain all possible informa-
tion about ✓0 . To be more formal about “contain all possible information”,
this means that the probability of x given ✓ can be factored into a part
that does not depend on ✓, and a part that depends on ✓ only via (x).
The Fisher-Neyman factorization theorem formalizes this notion, which
we state in Theorem 6.14 without proof.
Theorem 6.14 (Fisher-Neyman). [Theorem 6.5 in Lehmann and Casella
Fisher-Neyman (1998)] Let X have probability density function p(x | ✓). Then the statistics
theorem (x) are sufficient for ✓ if and only if p(x | ✓) can be written in the form
p(x | ✓) = h(x)g✓ ( (x)) , (6.106)
where h(x) is a distribution independent of ✓ and g✓ captures all the depen-
dence on ✓ via sufficient statistics (x).
If p(x | ✓) does not depend on ✓, then (x) is trivially a sufficient statistic
for any function . The more interesting case is that p(x | ✓) is dependent
only on (x) and not x itself. In this case, (x) is a sufficient statistic for
✓.
In machine learning, we consider a finite number of samples from a
distribution. One could imagine that for simple distributions (such as the
Bernoulli in Example 6.8) we only need a small number of samples to
estimate the parameters of the distributions. We could also consider the
opposite problem: If we have a set of data (a sample from an unknown
distribution), which distribution gives the best fit? A natural question to
ask is, as we observe more data, do we need more parameters ✓ to de-
scribe the distribution? It turns out that the answer is yes in general, and
this is studied in non-parametric statistics (Wasserman, 2007). A converse
question is to consider which class of distributions have finite-dimensional

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.6 Conjugacy and the Exponential Family 211

sufficient statistics, that is the number of parameters needed to describe


them does not increase arbitrarily. The answer is exponential family dis-
tributions, described in the following section.

6.6.3 Exponential Family


There are three possible levels of abstraction we can have when con-
sidering distributions (of discrete or continuous random variables). At
level one (the most concrete end of the spectrum), we have a particu-
lar named distribution with fixed parameters, for example a univariate
Gaussian N 0, 1 with zero mean and unit variance. In machine learning,
we often use the second level of abstraction, that is, we fix the paramet-
ric form (the univariate Gaussian) and infer the parameters from data. For
example, we assume a univariate Gaussian N µ, 2 with unknown mean
µ and unknown variance 2 , and use a maximum likelihood fit to deter-
mine the best parameters (µ, 2 ). We will see an example of this when
considering linear regression in Chapter 9. A third level of abstraction is
to consider families of distributions, and in this book, we consider the ex-
ponential family. The univariate Gaussian is an example of a member of
the exponential family. Many of the widely used statistical models, includ-
ing all the “named” models in Table 6.2, are members of the exponential
family. They can all be unified into one concept (Brown, 1986).
Remark. A brief historical anecdote: Like many concepts in mathemat-
ics and science, exponential families were independently discovered at
the same time by different researchers. In the years 1935–1936, Edwin
Pitman in Tasmania, Georges Darmois in Paris, and Bernard Koopman in
New York independently showed that the exponential families are the only
families that enjoy finite-dimensional sufficient statistics under repeated
independent sampling (Lehmann and Casella, 1998). }
An exponential family is a family of probability distributions, parame- exponential family
terized by ✓ 2 RD , of the form
p(x | ✓) = h(x) exp (h✓, (x)i A(✓)) , (6.107)
where (x) is the vector of sufficient statistics. In general, any inner prod-
uct (Section 3.2) can be used in (6.107), and for concreteness we will use
the standard dot product here (h✓, (x)i = ✓ > (x)). Note that the form
of the exponential family is essentially a particular expression of g✓ ( (x))
in the Fisher-Neyman theorem (Theorem 6.14).
The factor h(x) can be absorbed into the dot product term by adding
another entry (log h(x)) to the vector of sufficient statistics (x), and
constraining the corresponding parameter ✓0 = 1. The term A(✓) is the
normalization constant that ensures that the distribution sums up or inte-
grates to one and is called the log-partition function. A good intuitive no- log-partition
tion of exponential families can be obtained by ignoring these two terms function

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


212 Probability and Distributions

and considering exponential families as distributions of the form


p(x | ✓) / exp ✓ > (x) . (6.108)
natural parameters For this form of parametrization, the parameters ✓ are called the natural
parameters. At first glance, it seems that exponential families are a mun-
dane transformation by adding the exponential function to the result of a
dot product. However, there are many implications that allow for conve-
nient modeling and efficient computation based on the fact that we can
capture information about data in (x).

Example 6.13 (Gaussian as Exponential Family) 


x
Consider the univariate Gaussian distribution N µ, 2
. Let (x) = .
x2
Then by using the definition of the exponential family,
p(x | ✓) / exp(✓1 x + ✓2 x2 ) . (6.109)
Setting
 >
µ 1
✓= 2
, 2
(6.110)
2
and substituting into (6.109), we obtain
✓ ◆ ✓ ◆
µx x2 1
p(x | ✓) / exp 2 2
/ exp (x µ) 2
. (6.111)
2 2 2
Therefore, the univariate Gaussian distribution is a member of the expo-
x
nential family with sufficient statistic (x) = 2 , and natural parame-
x
ters given by ✓ in (6.110).

Example 6.14 (Bernoulli as Exponential Family)


Recall the Bernoulli distribution from Example 6.8
p(x | µ) = µx (1 µ)1 x
, x 2 {0, 1}. (6.112)
This can be written in exponential family form
⇥ ⇤
p(x | µ) = exp log µx (1 µ)1 x (6.113a)
= exp [x log µ + (1 x) log(1 µ)] (6.113b)
= exp [x log µ x log(1 µ) + log(1 µ)] (6.113c)
h i
= exp x log 1 µµ + log(1 µ) . (6.113d)
The last line (6.113d) can be identified as being in exponential family
form (6.107) by observing that
h(x) = 1 (6.114)

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.6 Conjugacy and the Exponential Family 213

✓ = log 1 µµ (6.115)
(x) = x (6.116)
A(✓) = log(1 µ) = log(1 + exp(✓)). (6.117)
The relationship between ✓ and µ is invertible so that
1
µ= . (6.118)
1 + exp( ✓)
The relation (6.118) is used to obtain the right equality of (6.117).

Remark. The relationship between the original Bernoulli parameter µ and


the natural parameter ✓ is known as the sigmoid or logistic function. Ob- sigmoid
serve that µ 2 (0, 1) but ✓ 2 R, and therefore the sigmoid function
squeezes a real value into the range (0, 1). This property is useful in ma-
chine learning, for example it is used in logistic regression (Bishop, 2006,
section 4.3.2), as well as as a nonlinear activation functions in neural net-
works (Goodfellow et al., 2016, chapter 6). }
It is often not obvious how to find the parametric form of the conjugate
distribution of a particular distribution (for example, those in Table 6.2).
Exponential families provide a convenient way to find conjugate pairs of
distributions. Consider the random variable X is a member of the expo-
nential family (6.107):

p(x | ✓) = h(x) exp (h✓, (x)i A(✓)) . (6.119)

Every member of the exponential family has a conjugate prior (Brown,


1986)
✓⌧  ◆

p(✓ | ) = hc (✓) exp 1
, Ac ( ) , (6.120)
2 A(✓)

where = 1
has dimension dim(✓) + 1. The sufficient statistics of
2


the conjugate prior are . By using the knowledge of the general
A(✓)
form of conjugate priors for exponential families, we can derive functional
forms of conjugate priors corresponding to particular distributions.

Example 6.15
Recall the exponential family form of the Bernoulli distribution (6.113d)

µ
p(x | µ) = exp x log + log(1 µ) . (6.121)
1 µ

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


214 Probability and Distributions

The canonical conjugate prior has the form



µ µ
p(µ | ↵, ) = exp ↵ log + ( + ↵) log(1 µ) Ac ( ) ,
1 µ 1 µ
(6.122)
where we defined := [↵, + ↵]> and hc (µ) := µ/(1 µ). Equa-
tion (6.122) then simplifies to
p(µ | ↵, ) = exp [(↵ 1) log µ + ( 1) log(1 µ) Ac (↵, )] .
(6.123)
Putting this in non-exponential family form yields
p(µ | ↵, ) / µ↵ 1 (1 µ) 1
, (6.124)
which we identify as the Beta distribution (6.98). In example 6.12, we
assumed that the Beta distribution is the conjugate prior of the Bernoulli
distribution and showed that it was indeed the conjugate prior. In this
example, we derived the form of the Beta distribution by looking at the
canonical conjugate prior of the Bernoulli distribution in exponential fam-
ily form.

As mentioned in the previous section, the main motivation for expo-


nential families is that they have finite-dimensional sufficient statistics.
Additionally, conjugate distributions are easy to write down, and the con-
jugate distributions also come from an exponential family. From an infer-
ence perspective, maximum likelihood estimation behaves nicely because
empirical estimates of sufficient statistics are optimal estimates of the pop-
ulation values of sufficient statistics (recall the mean and covariance of a
Gaussian). From an optimization perspective, the log-likelihood function
is concave, allowing for efficient optimization approaches to be applied
(Chapter 7).

6.7 Change of Variables/Inverse Transform


It may seem that there are very many known distributions, but in reality
the set of distributions for which we have names is quite limited. There-
fore, it is often useful to understand how transformed random variables
are distributed. For example, assuming that X is a random variable dis-
tributed according to the univariate normal distribution N 0, 1 , what is
the distribution of X 2 ? Another example, which is quite common in ma-
chine learning, is, given that X1 and X2 are univariate standard normal,
what is the distribution of 12 (X1 + X2 )?
One option to work out the distribution of 12 (X1 + X2 ) is to calculate
the mean and variance of X1 and X2 and then combine them. As we saw
in Section 6.4.4, we can calculate the mean and variance of resulting ran-
dom variables when we consider affine transformations of random vari-

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.7 Change of Variables/Inverse Transform 215

ables. However, we may not be able to obtain the functional form of the
distribution under transformations. Furthermore, we may be interested
in nonlinear transformations of random variables for which closed-form
expressions are not readily available.
Remark (Notation). In this section, we will be explicit about random vari-
ables and the values they take. Hence, recall that we use capital letters
X, Y to denote random variables and small letters x, y to denote the val-
ues in the target space T that the random variables take. We will explicitly
write pmfs of discrete random variables X as P (X = x). For continuous
random variables X (Section 6.2.2), the pdf is written as f (x) and the cdf
is written as FX (x). }
We will look at two approaches for obtaining distributions of transfor-
mations of random variables: a direct approach using the definition of a
cumulative distribution function and a change-of-variable approach that
uses the chain rule of calculus (Section 5.2.2). The change-of-variable ap- Moment generating
proach is widely used because it provides a “recipe” for attempting to functions can also
be used to study
compute the resulting distribution due to a transformation. We will ex-
transformations of
plain the techniques for univariate random variables, and will only briefly random
provide the results for the general case of multivariate random variables. variables (Casella
Transformations of discrete random variables can be understood di- and Berger, 2002,
chapter 2).
rectly. Suppose that there is a discrete random variable X with pmf P (X =
x) (Section 6.2.1), and an invertible function U (x). Consider the trans-
formed random variable Y := U (X), with pmf P (Y = y). Then

P (Y = y) = P (U (X) = y) transformation of interest (6.125a)


= P (X = U 1 (y)) inverse (6.125b)

where we can observe that x = U 1 (y). Therefore, for discrete random


variables, transformations directly change the individual events (with the
probabilities appropriately transformed).

6.7.1 Distribution Function Technique


The distribution function technique goes back to first principles, and uses
the definition of a cdf FX (x) = P (X 6 x) and the fact that its differential
is the pdf f (x) (Wasserman, 2004, chapter 2). For a random variable X
and a function U , we find the pdf of the random variable Y := U (X) by

1. Finding the cdf:


FY (y) = P (Y 6 y) (6.126)

2. Differentiating the cdf FY (y) to get the pdf f (y).


d
f (y) = FY (y) . (6.127)
dy

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


216 Probability and Distributions

We also need to keep in mind that the domain of the random variable may
have changed due to the transformation by U .

Example 6.16
Let X be a continuous random variable with probability density function
on 0 6 x 6 1
f (x) = 3x2 . (6.128)
We are interested in finding the pdf of Y = X 2 .
The function f is an increasing function of x, and therefore the resulting
value of y lies in the interval [0, 1]. We obtain
FY (y) = P (Y 6 y) definition of cdf (6.129a)
= P (X 2 6 y) transformation of interest (6.129b)
1
= P (X 6 y ) 2 inverse (6.129c)
1
= FX (y ) 2 definition of cdf (6.129d)
Z y 12
= 3t2 dt cdf as a definite integral (6.129e)
0
⇥ ⇤t=y 12
= t3 t=0 result of integration (6.129f)
3
=y , 2 0 6 y 6 1. (6.129g)
Therefore, the cdf of Y is
3
FY (y) = y 2 (6.130)
for 0 6 y 6 1. To obtain the pdf, we differentiate the cdf
d 3 1
f (y) = FY (y) = y 2 (6.131)
dy 2
for 0 6 y 6 1.

In Example 6.16, we considered a strictly monotonically increasing func-


Functions that have tion f (x) = 3x2 . This means that we could compute an inverse function.
inverses are called In general, we require that the function of interest y = U (x) has an in-
bijective functions
verse x = U 1 (y). A useful result can be obtained by considering the cu-
(Section 2.7).
mulative distribution function FX (x) of a random variable X , and using
it as the transformation U (x). This leads to the following theorem.

Theorem 6.15. [Theorem 2.1.10 in Casella and Berger (2002)] Let X be a


continuous random variable with a strictly monotonic cumulative distribu-
tion function FX (x). Then the random variable Y defined as

Y := FX (x) (6.132)

has a uniform distribution.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.7 Change of Variables/Inverse Transform 217

Theorem 6.15 is known as the probability integral transform, and it is probability integral
used to derive algorithms for sampling from distributions by transforming transform
the result of sampling from a uniform random variable (Bishop, 2006).
The algorithm works by first generating a sample from a uniform distribu-
tion, then transforming it by the inverse cdf (assuming this is available)
to obtain a sample from the desired distribution. The probability integral
transform is also used for hypothesis testing whether a sample comes from
a particular distribution (Lehmann and Romano, 2005). The idea that the
output of a cdf gives a uniform distribution also forms the basis of copu-
las (Nelsen, 2006).

6.7.2 Change of Variables


The distribution function technique in Section 6.7.1 is derived from first
principles, based on the definitions of cdfs and using properties of in-
verses, differentiation, and integration. This argument from first principles
relies on two facts:

1. We can transform the cdf of Y into an expression that is a cdf of X .


2. We can differentiate the cdf to obtain the pdf.

Let us break down the reasoning step by step, with the goal of understand-
ing the more general change-of-variables approach in Theorem 6.16. Change of variables
in probability relies
Remark. The name “change of variables” comes from the idea of chang- on the
ing the variable of integration when faced with a difficult integral. For change-of-variables
univariate functions, we use the substitution rule of integration, method in
Z Z calculus (Tandra,
2014).
f (g(x))g (x)dx = f (u)du , where u = g(x) .
0
(6.133)

The derivation of this rule is based on the chain rule of calculus (5.32) and
by applying twice the fundamental theorem of calculus. The fundamental
theorem of calculus formalizes the fact that integration and differentiation
are somehow “inverses” of each other. An intuitive understanding of the
rule can be obtained by thinking (loosely) about small changes (differen-
tials) to the equation u = g(x), that is by considering u = g 0 (x) x as a
differential of u = g(x). By subsituting u = g(x), the argument inside the
integral on the right-hand side of (6.133) becomes f (g(x)). By pretending
that the term du can be approximated by du ⇡ u = g 0 (x) x, and that
dx ⇡ x, we obtain (6.133). }
Consider a univariate random variable X , and an invertible function
U , which gives us another random variable Y = U (X). We assume that
random variable X has states x 2 [a, b]. By the definition of the cdf, we
have
FY (y) = P (Y 6 y) . (6.134)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


218 Probability and Distributions

We are interested in a function U of the random variable


P (Y 6 y) = P (U (X) 6 y) , (6.135)
where we assume that the function U is invertible. An invertible function
on an interval is either strictly increasing or strictly decreasing. In the case
that U is strictly increasing, then its inverse U 1 is also strictly increasing.
By applying the inverse U 1 to the arguments of P (U (X) 6 y), we obtain
P (U (X) 6 y) = P (U 1
(U (X)) 6 U 1
(y)) = P (X 6 U
(y)) . 1

(6.136)
The right-most term in (6.136) is an expression of the cdf of X . Recall the
definition of the cdf in terms of the pdf
Z U 1 (y)
P (X 6 U 1 (y)) = f (x)dx . (6.137)
a

Now we have an expression of the cdf of Y in terms of x:


Z U 1 (y)
FY (y) = f (x)dx . (6.138)
a

To obtain the pdf, we differentiate (6.138) with respect to y :


Z 1
d d U (y)
f (y) = Fy (y) = f (x)dx . (6.139)
dy dy a
Note that the integral on the right-hand side is with respect to x, but we
need an integral with respect to y because we are differentiating with
respect to y . In particular, we use (6.133) to get the substitution
Z Z
0
f (U 1 (y))U 1 (y)dy = f (x)dx where x = U 1 (y) . (6.140)

Using (6.140) on the right-hand side of (6.139) gives us


Z 1
d U (y) 0
f (y) = fx (U 1 (y))U 1 (y)dy . (6.141)
dy a
We then recall that differentiation is a linear operator and we use the
subscript x to remind ourselves that fx (U 1 (y)) is a function of x and not
y . Invoking the fundamental theorem of calculus again gives us
✓ ◆
d
f (y) = fx (U 1 (y)) · U 1 (y) . (6.142)
dy
Recall that we assumed that U is a strictly increasing function. For decreas-
ing functions, it turns out that we have a negative sign when we follow
the same derivation. We introduce the absolute value of the differential to
have the same expression for both increasing and decreasing U :
d
f (y) = fx (U 1
(y)) · U 1
(y) . (6.143)
dy

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.7 Change of Variables/Inverse Transform 219

This is called the change-of-variable technique. The term dy


d
U 1 (y) in change-of-variable
technique
(6.143) measures how much a unit volume changes when applying U
(see also the definition of the Jacobian in Section 5.3).
Remark. In comparison to the discrete case in (6.125b), we have an addi-
tional factor dy
d
U 1 (y) . The continuous case requires more care because
P (Y = y) = 0 for all y . The probability density function f (y) does not
have a description as a probability of an event involving y . }
So far in this section, we have been studying univariate change of vari-
ables. The case for multivariate random variables is analogous, but com-
plicated by fact that the absolute value cannot be used for multivariate
functions. Instead, we use the determinant of the Jacobian matrix. Recall
from (5.58) that the Jacobian is a matrix of partial derivatives, and that
the existence of a nonzero determinant shows that we can invert the Ja-
cobian. Recall the discussion in Section 4.1 that the determinant arises
because our differentials (cubes of volume) are transformed into paral-
lelepipeds by the Jacobian. Let us summarize preceding the discussion in
the following theorem, which gives us a recipe for multivariate change of
variables.

Theorem 6.16. [Theorem 17.2 in Billingsley (1995)] Let f (x) be the value
of the probability density of the multivariate continuous random variable X .
If the vector-valued function y = U (x) is differentiable and invertible for
all values within the domain of x, then for corresponding values of y , the
probability density of Y = U (X) is given by
✓ ◆
@
f (y) = fx (U 1 (y)) · det U 1 (y) . (6.144)
@y
The theorem looks intimidating at first glance, but the key point is that
a change of variable of a multivariate random variable follows the pro-
cedure of the univariate change of variable. First we need to work out
the inverse transform, and substitute that into the density of x. Then we
calculate the determinant of the Jacobian and multiply the result. The
following example illustrates the case of a bivariate random variable.

Example 6.17 
x1
Consider a bivariate random variable X with states x = and proba-
x2
bility density function
✓ ◆  >  !
x1 1 1 x1 x1
f = exp . (6.145)
x2 2⇡ 2 x2 x2

We use the change-of-variable technique from Theorem 6.16 to derive the

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


220 Probability and Distributions

effect of a linear transformation (Section 2.7) of the random variable.


Consider a matrix A 2 R2⇥2 defined as

a b
A= . (6.146)
c d
We are interested in finding the probability density function of the trans-
formed bivariate random variable Y with states y = Ax.
Recall that for change of variables we require the inverse transformation
of x as a function of y . Since we consider linear transformations, the
inverse transformation is given by the matrix inverse (see Section 2.2.2).
For 2 ⇥ 2 matrices, we can explicitly write out the formula, given by
   
x1 1 y1 1 d b y1
=A = . (6.147)
x2 y2 ad bc c a y2
Observe that ad bc is the determinant (Section 4.1) of A. The corre-
sponding probability density function is given by
1 ⇣ ⌘
>
f (x) = f (A 1 y) = exp 1 >
2
y A A 1
y . (6.148)
2⇡
The partial derivative of a matrix times a vector with respect to the vector
is the matrix itself (Section 5.5), and therefore
@
A 1y = A 1
. (6.149)
@y
Recall from Section 4.1 that the determinant of the inverse is the inverse
of the determinant so that the determinant of the Jacobian matrix is
✓ ◆
@ 1 1
det A y = . (6.150)
@y ad bc
We are now able to apply the change-of-variable formula from Theo-
rem 6.16 by multiplying (6.148) with (6.150), which yields
✓ ◆
@ 1
f (y) = f (x) det A y (6.151a)
@y
1 ⇣ ⌘
> 1
= exp 1 >
2
y A A y |ad bc| 1 . (6.151b)
2⇡

While Example 6.17 is based on a bivariate random variable, which al-


lows us to easily compute the matrix inverse, the preceding relation holds
for higher dimensions.
Remark. We saw in Section 6.5 that the density f (x) in (6.148) is actually
the standard Gaussian distribution, and the transformed density f (y) is a
bivariate Gaussian with covariance ⌃ = AA> . }
We will use the ideas in this chapter to describe probabilistic modeling

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


6.8 Further Reading 221

in Section 8.4, as well as introduce a graphical language in Section 8.5. We


will see direct machine learning applications of these ideas in Chapters 9
and 11.

6.8 Further Reading


This chapter is rather terse at times. Grinstead and Snell (1997) and
Walpole et al. (2011) provide more relaxed presentations that are suit-
able for self-study. Readers interested in more philosophical aspects of
probability should consider Hacking (2001), whereas an approach that
is more related to software engineering is presented by Downey (2014).
An overview of exponential families can be found in Barndorff-Nielsen
(2014). We will see more about how to use probability distributions to
model machine learning tasks in Chapter 8. Ironically, the recent surge
in interest in neural networks has resulted in a broader appreciation of
probabilistic models. For example, the idea of normalizing flows (Jimenez
Rezende and Mohamed, 2015) relies on change of variables for transform-
ing random variables. An overview of methods for variational inference as
applied to neural networks is described in chapters 16 to 20 of the book
by Goodfellow et al. (2016).
We side stepped a large part of the difficulty in continuous random vari-
ables by avoiding measure theoretic questions (Billingsley, 1995; Pollard,
2002), and by assuming without construction that we have real numbers,
and ways of defining sets on real numbers as well as their appropriate fre-
quency of occurrence. These details do matter, for example, in the specifi-
cation of conditional probability p(y | x) for continuous random variables
x, y (Proschan and Presnell, 1998). The lazy notation hides the fact that
we want to specify that X = x (which is a set of measure zero). Fur-
thermore, we are interested in the probability density function of y . A
more precise notation would have to say Ey [f (y) | (x)], where we take
the expectation over y of a test function f conditioned on the -algebra of
x. A more technical audience interested in the details of probability the-
ory have many options (Jaynes, 2003; MacKay, 2003; Jacod and Protter,
2004; Grimmett and Welsh, 2014), including some very technical discus-
sions (Shiryayev, 1984; Lehmann and Casella, 1998; Dudley, 2002; Bickel
and Doksum, 2006; Çinlar, 2011). An alternative way to approach proba-
bility is to start with the concept of expectation, and “work backward” to
derive the necessary properties of a probability space (Whittle, 2000). As
machine learning allows us to model more intricate distributions on ever
more complex types of data, a developer of probabilistic machine learn-
ing models would have to understand these more technical aspects. Ma-
chine learning texts with a probabilistic modeling focus include the books
by MacKay (2003); Bishop (2006); Rasmussen and Williams (2006); Bar-
ber (2012); Murphy (2012).

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


222 Probability and Distributions

Exercises
6.1 Consider the following bivariate distribution p(x, y) of two discrete random
variables X and Y .

y1 0.01 0.02 0.03 0.1 0.1

Y y2 0.05 0.1 0.05 0.07 0.2

y3 0.1 0.05 0.03 0.05 0.04

x1 x2 x3 x4 x5
X

Compute:
a. The marginal distributions p(x) and p(y).
b. The conditional distributions p(x|Y = y1 ) and p(y|X = x3 ).
6.2 Consider a mixture of two Gaussian distributions (illustrated in Figure 6.4),
✓  ◆ ✓  ◆
10 1 0 0 8.4 2.0
0.4 N , + 0.6 N , .
2 0 1 0 2.0 1.7

a. Compute the marginal distributions for each dimension.


b. Compute the mean, mode and median for each marginal distribution.
c. Compute the mean and mode for the two-dimensional distribution.
6.3 You have written a computer program that sometimes compiles and some-
times not (code does not change). You decide to model the apparent stochas-
ticity (success vs. no success) x of the compiler using a Bernoulli distribution
with parameter µ:

p(x | µ) = µx (1 µ)1 x
, x 2 {0, 1} .

Choose a conjugate prior for the Bernoulli likelihood and compute the pos-
terior distribution p(µ | x1 , . . . , xN ).
6.4 There are two bags. The first bag contains four mangos and two apples; the
second bag contains four mangos and four apples.
We also have a biased coin, which shows “heads” with probability 0.6 and
“tails” with probability 0.4. If the coin shows “heads”. we pick a fruit at
random from bag 1; otherwise we pick a fruit at random from bag 2.
Your friend flips the coin (you cannot see the result), picks a fruit at random
from the corresponding bag, and presents you a mango.
What is the probability that the mango was picked from bag 2?
Hint: Use Bayes’ theorem.
6.5 Consider the time-series model

xt+1 = Axt + w , w ⇠ N 0, Q
y t = Cxt + v , v ⇠ N 0, R ,

where w, v are i.i.d. Gaussian noise variables. Further, assume that p(x0 ) =
N µ0 , ⌃ 0 .

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Exercises 223

a. What is the form of p(x0 , x1 , . . . , xT )? Justify your answer (you do not


have to explicitly compute the joint distribution).
b. Assume that p(xt | y 1 , . . . , y t ) = N µt , ⌃t .
1. Compute p(xt+1 | y 1 , . . . , y t ).
2. Compute p(xt+1 , y t+1 | y 1 , . . . , y t ).
3. At time t+1, we observe the value y t+1 = ŷ . Compute the conditional
distribution p(xt+1 | y 1 , . . . , y t+1 ).
6.6 Prove the relationship in (6.44), which relates the standard definition of the
variance to the raw-score expression for the variance.
6.7 Prove the relationship in (6.45), which relates the pairwise difference be-
tween examples in a dataset with the raw-score expression for the variance.
6.8 Express the Bernoulli distribution in the natural parameter form of the ex-
ponential family, see (6.107).
6.9 Express the Binomial distribution as an exponential family distribution. Also
express the Beta distribution is an exponential family distribution. Show that
the product of the Beta and the Binomial distribution is also a member of
the exponential family.
6.10 Derive the relationship in Section 6.5.2 in two ways:
a. By completing the square
b. By expressing the Gaussian in its exponential family form
The product of two Gaussians N x | a, A N x | b, B is an unnormalized
Gaussian distribution c N x | c, C with
1 1 1
C = (A +B )
1 1
c = C(A a+B b)
D 1
c = (2⇡) 2 |A + B| 2 exp 1
2 (a b)> (A + B) 1
(a b) .

Note that the normalizing constant c itself can be considered a (normalized)


Gaussian distribution either in a or in b with an “inflated” covariance matrix
A + B , i.e., c = N a | b, A + B = N b | a, A + B .
6.11 Iterated Expectations.
Consider two random variables x, y with joint distribution p(x, y). Show that
⇥ ⇤
EX [x] = EY EX [x | y] .
Here, EX [x | y] denotes the expected value of x under the conditional distri-
bution p(x | y).
6.12 Manipulation of Gaussian Random Variables.
Consider a Gaussian random variable x ⇠ N x | µx , ⌃x , where x 2 RD .
Furthermore, we have
y = Ax + b + w ,

where y 2 RE , A 2 RE⇥D , b 2 RE , and w ⇠ N w | 0, Q is indepen-


dent Gaussian noise. “Independent” implies that x and w are independent
random variables and that Q is diagonal.
a. Write down the likelihood
R p(y | x).
b. The distribution p(y) = p(y | x)p(x)dx is Gaussian. Compute the mean
µy and the covariance ⌃y . Derive your result in detail.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


224 Probability and Distributions

c. The random variable y is being transformed according to the measure-


ment mapping
z = Cy + v ,

where z 2 RF , C 2 RF ⇥E , and v ⇠ N v | 0, R is independent Gaus-


sian (measurement) noise.
Write down p(z | y).
Compute p(z), i.e., the mean µz and the covariance ⌃z . Derive your
result in detail.
d. Now, a value ŷ is measured. Compute the posterior distribution p(x | ŷ).
Hint for solution: This posterior is also Gaussian, i.e., we need to de-
termine only its mean and covariance matrix. Start by explicitly com-
puting the joint Gaussian p(x, y). This also requires us to compute the
cross-covariances Covx,y [x, y] and Covy,x [y, x]. Then apply the rules
for Gaussian conditioning.
6.13 Probability Integral Transformation
Given a continuous random variable x, with cdf Fx (x), show that the ran-
dom variable y = Fx (x) is uniformly distributed.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7

Continuous Optimization

Since machine learning algorithms are implemented on a computer, the


mathematical formulations are expressed as numerical optimization meth-
ods. This chapter describes the basic numerical methods for training ma-
chine learning models. Training a machine learning model often boils
down to finding a good set of parameters. The notion of “good” is de-
termined by the objective function or the probabilistic model, which we
will see examples of in the second part of this book. Given an objective
function, finding the best value is done using optimization algorithms. Since we consider
This chapter covers two main branches of continuous optimization (Fig- data and models in
RD , the
ure 7.1): unconstrained and constrained optimization. We will assume in
optimization
this chapter that our objective function is differentiable (see Chapter 5), problems we face
hence we have access to a gradient at each location in the space to help us are continuous
find the optimum value. By convention, most objective functions in ma- optimization
problems, as
chine learning are intended to be minimized, that is, the best value is the
opposed to
minimum value. Intuitively finding the best value is like finding the val- combinatorial
leys of the objective function, and the gradients point us uphill. The idea is optimization
to move downhill (opposite to the gradient) and hope to find the deepest problems for
point. For unconstrained optimization, this is the only concept we need, discrete variables.

but there are several design choices, which we discuss in Section 7.1. For
constrained optimization, we need to introduce other concepts to man-
age the constraints (Section 7.2). We will also introduce a special class
of problems (convex optimization problems in Section 7.3) where we can
make statements about reaching the global optimum.
Consider the function in Figure 7.2. The function has a global minimum global minimum
around x = 4.5, with a function value of approximately 47. Since
the function is “smooth,” the gradients can be used to help find the min-
imum by indicating whether we should take a step to the right or left.
This assumes that we are in the correct bowl, as there exists another local local minimum
minimum around x = 0.7. Recall that we can solve for all the stationary
points of a function by calculating its derivative and setting it to zero. For Stationary points
are the real roots of
`(x) = x4 + 7x3 + 5x2 17x + 3 , (7.1) the derivative, that
is, points that have
we obtain the corresponding gradient as zero gradient.
d`(x)
= 4x3 + 21x2 + 10x 17 . (7.2)
dx
225
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
226 Continuous Optimization

Figure 7.1 A mind Continuous


optimization Stepsize
map of the concepts
related to
optimization, as
presented in this
chapter. There are Unconstrained
Momentum
optimization Gradient descent
two main ideas:
gradient descent
and convex
optimization.
Stochastic
gradient
Constrained Chapter 10 descent
optimization Dimension reduc.

Lagrange Chapter 11
multipliers Density estimation
Convex

Convex optimization Linear


& duality programming

Convex conjugate Quadratic Chapter 12


programming Classification

Since this is a cubic equation, it has in general three solutions when set to
zero. In the example, two of them are minimums and one is a maximum
(around x = 1.4). To check whether a stationary point is a minimum
or maximum, we need to take the derivative a second time and check
whether the second derivative is positive or negative at the stationary
point. In our case, the second derivative is

d2 `(x)
= 12x2 + 42x + 10 . (7.3)
dx2
By substituting our visually estimated values of x = 4.5, ⇣1.4, 0.7, we ⌘
2
will observe that as expected the middle point is a maximum d dx `(x)
2 <0
and the other two stationary points are minimums.
Note that we have avoided analytically solving for values of x in the
previous discussion, although for low-order polynomials such as the pre-
ceding we could do so. In general, we are unable to find analytic solu-
tions, and hence we need to start at some value, say x0 = 6, and follow
the negative gradient. The negative gradient indicates that we should go

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.1 Optimization Using Gradient Descent 227

Figure 7.2 Example


objective function.
60 Negative gradients
are indicated by
x4 + 7x3 + 5x2 17x + 3 arrows, and the
40
global minimum is
indicated by the
20 dashed blue line.
Objective

20

40

60
6 5 4 3 2 1 0 1 2
Value of parameter

right, but not how far (this is called the step-size). Furthermore, if we According to the
had started at the right side (e.g., x0 = 0) the negative gradient would Abel–Ruffini
theorem, there is in
have led us to the wrong minimum. Figure 7.2 illustrates the fact that for
general no algebraic
x > 1, the negative gradient points toward the minimum on the right of solution for
the figure, which has a larger objective value. polynomials of
In Section 7.3, we will learn about a class of functions, called convex degree 5 or more
(Abel, 1826).
functions, that do not exhibit this tricky dependency on the starting point
of the optimization algorithm. For convex functions, all local minimums
are global minimum. It turns out that many machine learning objective For convex functions
functions are designed such that they are convex, and we will see an ex- all local minima are
global minimum.
ample in Chapter 12.
The discussion in this chapter so far was about a one-dimensional func-
tion, where we are able to visualize the ideas of gradients, descent direc-
tions, and optimal values. In the rest of this chapter we develop the same
ideas in high dimensions. Unfortunately, we can only visualize the con-
cepts in one dimension, but some concepts do not generalize directly to
higher dimensions, therefore some care needs to be taken when reading.

7.1 Optimization Using Gradient Descent


We now consider the problem of solving for the minimum of a real-valued
function

min f (x) , (7.4)


x

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


228 Continuous Optimization

where f : Rd ! R is an objective function that captures the machine


learning problem at hand. We assume that our function f is differentiable,
and we are unable to analytically find a solution in closed form.
Gradient descent is a first-order optimization algorithm. To find a local
minimum of a function using gradient descent, one takes steps propor-
tional to the negative of the gradient of the function at the current point.
We use the Recall from Section 5.1 that the gradient points in the direction of the
convention of row steepest ascent. Another useful intuition is to consider the set of lines
vectors for
where the function is at a certain value (f (x) = c for some value c 2 R),
gradients.
which are known as the contour lines. The gradient points in a direction
that is orthogonal to the contour lines of the function we wish to optimize.
Let us consider multivariate functions. Imagine a surface (described by
the function f (x)) with a ball starting at a particular location x0 . When
the ball is released, it will move downhill in the direction of steepest de-
scent. Gradient descent exploits the fact that f (x0 ) decreases fastest if one
moves from x0 in the direction of the negative gradient ((rf )(x0 ))> of
f at x0 . We assume in this book that the functions are differentiable, and
refer the reader to more general settings in Section 7.4. Then, if
x1 = x0 ((rf )(x0 ))> (7.5)
for a small step-size > 0, then f (x1 ) 6 f (x0 ). Note that we use the
transpose for the gradient since otherwise the dimensions will not work
out.
This observation allows us to define a simple gradient descent algo-
rithm: If we want to find a local optimum f (x⇤ ) of a function f : Rn !
R, x 7! f (x), we start with an initial guess x0 of the parameters we wish
to optimize and then iterate according to
xi+1 = xi i ((rf )(xi ))
>
. (7.6)
For suitable step-size i, the sequence f (x0 ) > f (x1 ) > . . . converges to
a local minimum.

Example 7.1
Consider a quadratic function in two dimensions
✓ ◆  >   > 
x1 1 x1 2 1 x1 5 x1
f = (7.7)
x2 2 x2 1 20 x2 3 x2
with gradient
✓ ◆  >   >
x1 x1 2 1 5
rf = . (7.8)
x2 x2 1 20 3
Starting at the initial location x0 = [ 3, 1]> , we iteratively apply (7.6)
to obtain a sequence of estimates that converge to the minimum value

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.1 Optimization Using Gradient Descent 229

2 0 90 Figure 7.3 Gradient


50. 40.0
descent on a
75 two-dimensional
quadratic surface
1 60 (shown as a
0.0
heatmap). See
45 Example 7.1 for a
description.
x2

0
30

°1 15
10.0

30.0
20.0
0
70. 60.
80. 0 0
50.0
0 40.0
°2 °15
°4 °2 0 2 4
x1

(illustrated in Figure 7.3). We can see (both from the figure and by plug-
ging x0 into (7.8) with = 0.085) that the negative gradient at x0 points
north and east, leading to x1 = [ 1.98, 1.21]> . Repeating that argument
gives us x2 = [ 1.32, 0.42]> , and so on.

Remark. Gradient descent can be relatively slow close to the minimum:


Its asymptotic rate of convergence is inferior to many other methods. Us-
ing the ball rolling down the hill analogy, when the surface is a long, thin
valley, the problem is poorly conditioned (Trefethen and Bau III, 1997).
For poorly conditioned convex problems, gradient descent increasingly
“zigzags” as the gradients point nearly orthogonally to the shortest di-
rection to a minimum point; see Figure 7.3. }

7.1.1 Step-size
As mentioned earlier, choosing a good step-size is important in gradient
descent. If the step-size is too small, gradient descent can be slow. If the The step-size is also
step-size is chosen too large, gradient descent can overshoot, fail to con- called the learning
rate.
verge, or even diverge. We will discuss the use of momentum in the next
section. It is a method that smoothes out erratic behavior of gradient up-
dates and dampens oscillations.
Adaptive gradient methods rescale the step-size at each iteration, de-
pending on local properties of the function. There are two simple heuris-
tics (Toussaint, 2012):
When the function value increases after a gradient step, the step-size
was too large. Undo the step and decrease the step-size.
When the function value decreases the step could have been larger. Try
to increase the step-size.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


230 Continuous Optimization

Although the “undo” step seems to be a waste of resources, using this


heuristic guarantees monotonic convergence.

Example 7.2 (Solving a Linear Equation System)


When we solve linear equations of the form Ax = b, in practice we solve
Ax b = 0 approximately by finding x⇤ that minimizes the squared error
kAx bk2 = (Ax b)> (Ax b) (7.9)
if we use the Euclidean norm. The gradient of (7.9) with respect to x is
rx = 2(Ax b)> A . (7.10)
We can use this gradient directly in a gradient descent algorithm. How-
ever, for this particular special case, it turns out that there is an analytic
solution, which can be found by setting the gradient to zero. We will see
more on solving squared error problems in Chapter 9.

Remark. When applied to the solution of linear systems of equations Ax =


b, gradient descent may converge slowly. The speed of convergence of gra-
condition number dient descent is dependent on the condition number  = (A) max
(A)min
, which
is the ratio of the maximum to the minimum singular value (Section 4.5)
of A. The condition number essentially measures the ratio of the most
curved direction versus the least curved direction, which corresponds to
our imagery that poorly conditioned problems are long, thin valleys: They
are very curved in one direction, but very flat in the other. Instead of di-
rectly solving Ax = b, one could instead solve P 1 (Ax b) = 0, where
preconditioner P is called the preconditioner. The goal is to design P 1 such that P 1 A
has a better condition number, but at the same time P 1 is easy to com-
pute. For further information on gradient descent, preconditioning, and
convergence we refer to Boyd and Vandenberghe (2004, chapter 9). }

7.1.2 Gradient Descent With Momentum


As illustrated in Figure 7.3, the convergence of gradient descent may be
very slow if the curvature of the optimization surface is such that there
are regions that are poorly scaled. The curvature is such that the gradient
descent steps hops between the walls of the valley and approaches the
optimum in small steps. The proposed tweak to improve convergence is
Goh (2017) wrote to give gradient descent some memory.
an intuitive blog Gradient descent with momentum (Rumelhart et al., 1986) is a method
post on gradient
that introduces an additional term to remember what happened in the
descent with
momentum. previous iteration. This memory dampens oscillations and smoothes out
the gradient updates. Continuing the ball analogy, the momentum term
emulates the phenomenon of a heavy ball that is reluctant to change di-
rections. The idea is to have a gradient update with memory to implement

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.1 Optimization Using Gradient Descent 231

a moving average. The momentum-based method remembers the update


xi at each iteration i and determines the next update as a linear combi-
nation of the current and previous gradients

xi+1 = xi i ((rf )(xi ))


>
+ ↵ xi (7.11)
xi = xi xi 1 = ↵ xi 1 i 1 ((rf )(xi 1 ))
>
, (7.12)

where ↵ 2 [0, 1]. Sometimes we will only know the gradient approxi-
mately. In such cases, the momentum term is useful since it averages out
different noisy estimates of the gradient. One particularly useful way to
obtain an approximate gradient is by using a stochastic approximation,
which we discuss next.

7.1.3 Stochastic Gradient Descent


Computing the gradient can be very time consuming. However, often it is
possible to find a “cheap” approximation of the gradient. Approximating
the gradient is still useful as long as it points in roughly the same direction
as the true gradient. stochastic gradient
Stochastic gradient descent (often shortened as SGD) is a stochastic ap- descent
proximation of the gradient descent method for minimizing an objective
function that is written as a sum of differentiable functions. The word
stochastic here refers to the fact that we acknowledge that we do not
know the gradient precisely, but instead only know a noisy approxima-
tion to it. By constraining the probability distribution of the approximate
gradients, we can still theoretically guarantee that SGD will converge.
In machine learning, given n = 1, . . . , N data points, we often consider
objective functions that are the sum of the losses Ln incurred by each
example n. In mathematical notation, we have the form
N
X
L(✓) = Ln (✓) , (7.13)
n=1

where ✓ is the vector of parameters of interest, i.e., we want to find ✓ that


minimizes L. An example from regression (Chapter 9) is the negative log-
likelihood, which is expressed as a sum over log-likelihoods of individual
examples so that
N
X
L(✓) = log p(yn |xn , ✓) , (7.14)
n=1

where xn 2 RD are the training inputs, yn are the training targets, and ✓
are the parameters of the regression model.
Standard gradient descent, as introduced previously, is a “batch” opti-
mization method, i.e., optimization is performed using the full training set

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


232 Continuous Optimization

by updating the vector of parameters according to


N
X
✓ i+1 = ✓ i i (rL(✓ i ))
>
= ✓i i (rLn (✓ i ))> (7.15)
n=1

for a suitable step-size parameter i . Evaluating the sum gradient may re-
quire expensive evaluations of the gradients from all individual functions
Ln . When the training set is enormous and/or no simple formulas exist,
evaluating the sums P of gradients becomes very expensive.
N
Consider the term n=1 (rLn (✓ i )) in (7.15), we can reduce the amount
of computation by taking a sum over a smaller set of Ln . In contrast to
batch gradient descent, which uses all Ln for n = 1, . . . , N , we randomly
choose a subset of Ln for mini-batch gradient descent. In the extreme
case, we randomly select only a single Ln to estimate the gradient. The
key insight about why taking a subset of data is sensible is to realize that
for gradient descent to converge, we only require that the PN gradient is an
unbiased estimate of the true gradient. In fact the term n=1 (rLn (✓ i ))
in (7.15) is an empirical estimate of the expected value (Section 6.4.1) of
the gradient. Therefore, any other unbiased empirical estimate of the ex-
pected value, for example using any subsample of the data, would suffice
for convergence of gradient descent.
Remark. When the learning rate decreases at an appropriate rate, and sub-
ject to relatively mild assumptions, stochastic gradient descent converges
almost surely to local minimum (Bottou, 1998). }
Why should one consider using an approximate gradient? A major rea-
son is practical implementation constraints, such as the size of central
processing unit (CPU)/graphics processing unit (GPU) memory or limits
on computational time. We can think of the size of the subset used to esti-
mate the gradient in the same way that we thought of the size of a sample
when estimating empirical means (Section 6.4.1). Large mini-batch sizes
will provide accurate estimates of the gradient, reducing the variance in
the parameter update. Furthermore, large mini-batches take advantage of
highly optimized matrix operations in vectorized implementations of the
cost and gradient. The reduction in variance leads to more stable conver-
gence, but each gradient calculation will be more expensive.
In contrast, small mini-batches are quick to estimate. If we keep the
mini-batch size small, the noise in our gradient estimate will allow us to
get out of some bad local optima, which we may otherwise get stuck in.
In machine learning, optimization methods are used for training by min-
imizing an objective function on the training data, but the overall goal
is to improve generalization performance (Chapter 8). Since the goal in
machine learning does not necessarily need a precise estimate of the min-
imum of the objective function, approximate gradients using mini-batch
approaches have been widely used. Stochastic gradient descent is very
effective in large-scale machine learning problems (Bottou et al., 2018),

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.2 Constrained Optimization and Lagrange Multipliers 233

3 Figure 7.4
Illustration of
constrained
optimization. The
2
unconstrained
problem (indicated
by the contour
1 lines) has a
minimum on the
right side (indicated
by the circle). The
x2

0
box constraints
( 1 6 x 6 1 and
1 6 y 6 1) require
1
that the optimal
solution is within
the box, resulting in
2 an optimal value
indicated by the
star.
3
3 2 1 0 1 2 3
x1

such as training deep neural networks on millions of images (Dean et al.,


2012), topic models (Hoffman et al., 2013), reinforcement learning (Mnih
et al., 2015), or training of large-scale Gaussian process models (Hensman
et al., 2013; Gal et al., 2014).

7.2 Constrained Optimization and Lagrange Multipliers


In the previous section, we considered the problem of solving for the min-
imum of a function
min f (x) , (7.16)
x

where f : RD ! R.
In this section, we have additional constraints. That is, for real-valued
functions gi : RD ! R for i = 1, . . . , m, we consider the constrained
optimization problem (see Figure 7.4 for an illustration)
min f (x) (7.17)
x

subject to gi (x) 6 0 for all i = 1, . . . , m .


It is worth pointing out that the functions f and gi could be non-convex
in general, and we will consider the convex case in the next section.
One obvious, but not very practical, way of converting the constrained
problem (7.17) into an unconstrained one is to use an indicator function
m
X
J(x) = f (x) + 1(gi (x)) , (7.18)
i=1

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


234 Continuous Optimization

where 1(z) is an infinite step function


(
0 if z 6 0
1(z) = . (7.19)
1 otherwise

This gives infinite penalty if the constraint is not satisfied, and hence
would provide the same solution. However, this infinite step function is
equally difficult to optimize. We can overcome this difficulty by introduc-
Lagrange multiplier ing Lagrange multipliers. The idea of Lagrange multipliers is to replace the
step function with a linear function.
Lagrangian We associate to problem (7.17) the Lagrangian by introducing the La-
grange multipliers i > 0 corresponding to each inequality constraint re-
spectively (Boyd and Vandenberghe, 2004, chapter 4) so that
m
X
L(x, ) = f (x) + i gi (x) (7.20a)
i=1
>
= f (x) + g(x) , (7.20b)

where in the last line we have concatenated all constraints gi (x) into a
vector g(x), and all the Lagrange multipliers into a vector 2 Rm .
We now introduce the idea of Lagrangian duality. In general, duality
in optimization is the idea of converting an optimization problem in one
set of variables x (called the primal variables), into another optimization
problem in a different set of variables (called the dual variables). We
introduce two different approaches to duality: In this section, we discuss
Lagrangian duality; in Section 7.3.3, we discuss Legendre–Fenchel duality.

Definition 7.1. The problem in (7.17)

min f (x) (7.21)


x

subject to gi (x) 6 0 for all i = 1, . . . , m


primal problem is known as the primal problem, corresponding to the primal variables x.
Lagrangian dual The associated Lagrangian dual problem is given by
problem
maxm D( )
2R
(7.22)
subject to > 0,

where are the dual variables and D( ) = minx2Rd L(x, ).

Remark. In the discussion of Definition 7.1, we use two concepts that are
also of independent interest (Boyd and Vandenberghe, 2004).
minimax inequality First is the minimax inequality, which says that for any function with
two arguments '(x, y), the maximin is less than the minimax, i.e.,

max min '(x, y) 6 min max '(x, y) . (7.23)


y x x y

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.2 Constrained Optimization and Lagrange Multipliers 235

This inequality can be proved by considering the inequality


For all x, y min '(x, y) 6 max '(x, y) . (7.24)
x y

Note that taking the maximum over y of the left-hand side of (7.24) main-
tains the inequality since the inequality is true for all y . Similarly, we can
take the minimum over x of the right-hand side of (7.24) to obtain (7.23).
The second concept is weak duality, which uses (7.23) to show that weak duality
primal values are always greater than or equal to dual values. This is de-
scribed in more detail in (7.27). }
Recall that the difference between J(x) in (7.18) and the Lagrangian
in (7.20b) is that we have relaxed the indicator function to a linear func-
tion. Therefore, when > 0, the Lagrangian L(x, ) is a lower bound of
J(x). Hence, the maximum of L(x, ) with respect to is
J(x) = max L(x, ) . (7.25)
>0

Recall that the original problem was minimizing J(x),


min max L(x, ) . (7.26)
x2Rd >0

By the minimax inequality (7.23), it follows that swapping the order of


the minimum and maximum results in a smaller value, i.e.,
min max L(x, ) > max mind L(x, ) . (7.27)
x2Rd >0 >0 x2R

This is also known as weak duality. Note that the inner part of the right- weak duality
hand side is the dual objective function D( ) and the definition follows.
In contrast to the original optimization problem, which has constraints,
minx2Rd L(x, ) is an unconstrained optimization problem for a given
value of . If solving minx2Rd L(x, ) is easy, then the overall problem
is easy to solve. The reason is that the outer problem (maximization over
) is a maximum over a set of affine functions, and hence is a concave
function, even though f (·) and gi (·) may be nonconvex. The maximum of
a concave function can be efficiently computed.
Assuming f (·) and gi (·) are differentiable, we find the Lagrange dual
problem by differentiating the Lagrangian with respect to x, setting the
differential to zero, and solving for the optimal value. We will discuss two
concrete examples in Sections 7.3.1 and 7.3.2, where f (·) and gi (·) are
convex.
Remark (Equality Constraints). Consider (7.17) with additional equality
constraints
min f (x)
x

subject to gi (x) 6 0 for all i = 1, . . . , m (7.28)


hj (x) = 0 for all j = 1, . . . , n .

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


236 Continuous Optimization

We can model equality constraints by replacing them with two inequality


constraints. That is for each equality constraint hj (x) = 0 we equivalently
replace it by two constraints hj (x) 6 0 and hj (x) > 0. It turns out that
the resulting Lagrange multipliers are then unconstrained.
Therefore, we constrain the Lagrange multipliers corresponding to the
inequality constraints in (7.28) to be non-negative, and leave the La-
grange multipliers corresponding to the equality constraints unconstrained.
}

7.3 Convex Optimization


We focus our attention of a particularly useful class of optimization prob-
lems, where we can guarantee global optimality. When f (·) is a convex
function, and when the constraints involving g(·) and h(·) are convex sets,
convex optimization this is called a convex optimization problem. In this setting, we have strong
problem duality: The optimal solution of the dual problem is the same as the opti-
strong duality mal solution of the primal problem. The distinction between convex func-
tions and convex sets are often not strictly presented in machine learning
literature, but one can often infer the implied meaning from context.
convex set Definition 7.2. A set C is a convex set if for any x, y 2 C and for any scalar
✓ with 0 6 ✓ 6 1, we have
✓x + (1 ✓)y 2 C . (7.29)
Figure 7.5 Example
of a convex set. Convex sets are sets such that a straight line connecting any two ele-
ments of the set lie inside the set. Figures 7.5 and 7.6 illustrate convex
and nonconvex sets, respectively.
Convex functions are functions such that a straight line between any
two points of the function lie above the function. Figure 7.2 shows a non-
convex function, and Figure 7.3 shows a convex function. Another convex
Figure 7.6 Example function is shown in Figure 7.7.
of a nonconvex set.
Definition 7.3. Let function f : RD ! R be a function whose domain is a
convex set. The function f is a convex function if for all x, y in the domain
of f , and for any scalar ✓ with 0 6 ✓ 6 1, we have
f (✓x + (1 ✓)y) 6 ✓f (x) + (1 ✓)f (y) . (7.30)

Remark. A concave function is the negative of a convex function. }


convex function The constraints involving g(·) and h(·) in (7.28) truncate functions at a
concave function scalar value, resulting in sets. Another relation between convex functions
and convex sets is to consider the set obtained by “filling in” a convex
function. A convex function is a bowl-like object, and we imagine pouring
epigraph water into it to fill it up. This resulting filled-in set, called the epigraph of
the convex function, is a convex set.
If a function f : Rn ! R is differentiable, we can specify convexity in

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.3 Convex Optimization 237

Figure 7.7 Example


of a convex
40 function.

30 y = 3x2 5x + 2
y

20

10

0
3 2 1 0 1 2 3
x

terms of its gradient rx f (x) (Section 5.2). A function f (x) is convex if


and only if for any two points x, y it holds that
f (y) > f (x) + rx f (x)> (y x) . (7.31)
If we further know that a function f (x) is twice differentiable, that is, the
Hessian (5.147) exists for all values in the domain of x, then the function
f (x) is convex if and only if r2x f (x) is positive semidefinite (Boyd and
Vandenberghe, 2004).

Example 7.3
The negative entropy f (x) = x log2 x is convex for x > 0. A visualization
of the function is shown in Figure 7.8, and we can see that the function is
convex. To illustrate the previous definitions of convexity, let us check the
calculations for two points x = 2 and x = 4. Note that to prove convexity
of f (x) we would need to check for all points x 2 R.
Recall Definition 7.3. Consider a point midway between the two points
(that is ✓ = 0.5); then the left-hand side is f (0.5 · 2 + 0.5 · 4) = 3 log2 3 ⇡
4.75. The right-hand side is 0.5(2 log2 2) + 0.5(4 log2 4) = 1 + 4 = 5. And
therefore the definition is satisfied.
Since f (x) is differentiable, we can alternatively use (7.31). Calculating
the derivative of f (x), we obtain
1 1
rx (x log2 x) = 1 · log2 x + x · = log2 x + . (7.32)
x loge 2 loge 2
Using the same two test points x = 2 and x = 4, the left-hand side of
(7.31) is given by f (4) = 8. The right-hand side is
f (x) + r>
x (y x) = f (2) + rf (2) · (4 2) (7.33a)
1
= 2 + (1 + ) · 2 ⇡ 6.9 . (7.33b)
loge 2

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


238 Continuous Optimization

Figure 7.8 The


negative entropy x log2 x
function (which is 10
convex) and its tangent at x = 2
tangent at x = 2.

5
f (x)

0 1 2 3 4 5
x

We can check that a function or set is convex from first principles by


recalling the definitions. In practice, we often rely on operations that pre-
serve convexity to check that a particular function or set is convex. Al-
though the details are vastly different, this is again the idea of closure
that we introduced in Chapter 2 for vector spaces.

Example 7.4
A nonnegative weighted sum of convex functions is convex. Observe that
if f is a convex function, and ↵ > 0 is a nonnegative scalar, then the
function ↵f is convex. We can see this by multiplying ↵ to both sides of the
equation in Definition 7.3, and recalling that multiplying a nonnegative
number does not change the inequality.
If f1 and f2 are convex functions, then we have by the definition
f1 (✓x + (1 ✓)y) 6 ✓f1 (x) + (1 ✓)f1 (y) (7.34)
f2 (✓x + (1 ✓)y) 6 ✓f2 (x) + (1 ✓)f2 (y) . (7.35)
Summing up both sides gives us
f1 (✓x + (1 ✓)y) + f2 (✓x + (1 ✓)y)
6 ✓f1 (x) + (1 ✓)f1 (y) + ✓f2 (x) + (1 ✓)f2 (y) , (7.36)
where the right-hand side can be rearranged to
✓(f1 (x) + f2 (x)) + (1 ✓)(f1 (y) + f2 (y)) , (7.37)
completing the proof that the sum of convex functions is convex.
Combining the preceding two facts, we see that ↵f1 (x) + f2 (x) is
convex for ↵, > 0. This closure property can be extended using a sim-
ilar argument for nonnegative weighted sums of more than two convex
functions.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.3 Convex Optimization 239

Remark. The inequality in (7.30) is sometimes called Jensen’s inequality. Jensen’s inequality
In fact, a whole class of inequalities for taking nonnegative weighted sums
of convex functions are all called Jensen’s inequality. }
In summary, a constrained optimization problem is called a convex opti- convex optimization
mization problem if problem

minf (x)
x

subject to gi (x) 6 0 for all i = 1, . . . , m (7.38)


hj (x) = 0 for all j = 1, . . . , n ,
where all functions f (x) and gi (x) are convex functions, and all hj (x) =
0 are convex sets. In the following, we will describe two classes of convex
optimization problems that are widely used and well understood.

7.3.1 Linear Programming


Consider the special case when all the preceding functions are linear, i.e.,
min c> x (7.39)
x2Rd

subject to Ax 6 b ,
where A 2 Rm⇥d and b 2 Rm . This is known as a linear program. It has d linear program
variables and m linear constraints. The Lagrangian is given by Linear programs are
one of the most
>
L(x, ) = c> x + (Ax b) , (7.40) widely used
approaches in
where 2 Rm is the vector of non-negative Lagrange multipliers. Rear- industry.
ranging the terms corresponding to x yields
>
L(x, ) = (c + A> )> x b. (7.41)
Taking the derivative of L(x, ) with respect to x and setting it to zero
gives us
c + A> = 0 . (7.42)
>
Therefore, the dual Lagrangian is D( ) = b. Recall we would like
to maximize D( ). In addition to the constraint due to the derivative of
L(x, ) being zero, we also have the fact that > 0, resulting in the
following dual optimization problem It is convention to
minimize the primal
maxm b> (7.43) and maximize the
2R
dual.
subject to c + A> = 0
> 0.
This is also a linear program, but with m variables. We have the choice
of solving the primal (7.39) or the dual (7.43) program depending on

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


240 Continuous Optimization

whether m or d is larger. Recall that d is the number of variables and m is


the number of constraints in the primal linear program.

Example 7.5 (Linear Program)


Consider the linear program
 > 
5 x1
min
x2R2 3 x2
2 3 2 3
2 2 33
6 2 47  6 8 7 (7.44)
6 7 x1 6 7
6 17 6 7
subject to 6 2 7 x2 6 6 5 7
4 0 1 5 4 15
0 1 8
with two variables. This program is also shown in Figure 7.9. The objective
function is linear, resulting in linear contour lines. The constraint set in
standard form is translated into the legend. The optimal value must lie in
the shaded (feasible) region, and is indicated by the star.

Figure 7.9 2x2 33 2x1


Illustration of a 4x2 2x1 8
10
linear program. The x2 2x1 5
unconstrained x2 1
problem (indicated x2 8

by the contour 8
lines) has a
minimum on the
right side. The
optimal value given 6
x2

the constraints are


shown by the star.
4

0
0 2 4 6 8 10 12 14 16
x1

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.3 Convex Optimization 241

7.3.2 Quadratic Programming


Consider the case of a convex quadratic objective function, where the con-
straints are affine, i.e.,
1 >
mind x Qx + c> x (7.45)
x2R 2
subject to Ax 6 b ,
where A 2 Rm⇥d , b 2 Rm , and c 2 Rd . The square symmetric matrix Q 2
Rd⇥d is positive definite, and therefore the objective function is convex.
This is known as a quadratic program. Observe that it has d variables and
m linear constraints.

Example 7.6 (Quadratic Program)


Consider the quadratic program
 >   > 
1 x1 2 1 x1 5 x1
min + (7.46)
x2R2 2 x2 1 4 x2 3 x2
2 3 2 3
1 0  1
6 1 0 7 x1 6 17
subject to 6 4 0
7 66 7 (7.47)
1 5 x2 4 15
0 1 1
of two variables. The program is also illustrated in Figure 7.4. The objec-
tive function is quadratic with a positive semidefinite matrix Q, resulting
in elliptical contour lines. The optimal value must lie in the shaded (feasi-
ble) region, and is indicated by the star.

The Lagrangian is given by


1 >
L(x, ) = x Qx + c> x + > (Ax b) (7.48a)
2
1 >
= x> Qx + (c + A> )> x b, (7.48b)
2
where again we have rearranged the terms. Taking the derivative of L(x, )
with respect to x and setting it to zero gives
Qx + (c + A> ) = 0 . (7.49)
Assuming that Q is invertible, we get
x= Q 1 (c + A> ) . (7.50)
Substituting (7.50) into the primal Lagrangian L(x, ), we get the dual
Lagrangian
1 >
D( ) = (c + A> )> Q 1 (c + A> ) b. (7.51)
2

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


242 Continuous Optimization

Therefore, the dual optimization problem is given by


1 >
maxm (c + A> )> Q 1 (c + A> ) b
2R 2 (7.52)
subject to > 0.

We will see an application of quadratic programming in machine learning


in Chapter 12.

7.3.3 Legendre–Fenchel Transform and Convex Conjugate


Let us revisit the idea of duality from Section 7.2, without considering
constraints. One useful fact about a convex set is that it can be equiva-
lently described by its supporting hyperplanes. A hyperplane is called a
supporting supporting hyperplane of a convex set if it intersects the convex set, and
hyperplane the convex set is contained on just one side of it. Recall that we can fill up
a convex function to obtain the epigraph, which is a convex set. Therefore,
we can also describe convex functions in terms of their supporting hyper-
planes. Furthermore, observe that the supporting hyperplane just touches
the convex function, and is in fact the tangent to the function at that
point. And recall that the tangent of a function f (x) at a given point x0
df (x)
is the evaluation of the gradient of that function at that point dx x=x
.
0
In summary, because convex sets can be equivalently described by its sup-
porting hyperplanes, convex functions can be equivalently described by a
Legendre transform function of their gradient. The Legendre transform formalizes this concept
Physics students are .
often introduced to We begin with the most general definition, which unfortunately has a
the Legendre
counter-intuitive form, and look at special cases to relate the definition to
transform as
relating the the intuition described in the preceding paragraph. The Legendre–Fenchel
Lagrangian and the transform is a transformation (in the sense of a Fourier transform) from
Hamiltonian in a convex differentiable function f (x) to a function that depends on the
classical mechanics.
tangents s(x) = rx f (x). It is worth stressing that this is a transformation
Legendre–Fenchel
transform of the function f (·) and not the variable x or the function evaluated at x.
convex conjugate The Legendre–Fenchel transform is also known as the convex conjugate
(for reasons we will see soon) and is closely related to duality (Hiriart-
Urruty and Lemaréchal, 2001, chapter 5).

convex conjugate Definition 7.4. The convex conjugate of a function f : RD ! R is a


function f ⇤ defined by

f ⇤ (s) = sup (hs, xi f (x)) . (7.53)


x2RD

Note that the preceding convex conjugate definition does not need the
function f to be convex nor differentiable. In Definition 7.4, we have used
a general inner product (Section 3.2) but in the rest of this section we

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.3 Convex Optimization 243

will consider the standard dot product between finite-dimensional vectors


(hs, xi = s> x) to avoid too many technical details.
To understand Definition 7.4 in a geometric fashion, consider an nice This derivation is
simple one-dimensional convex and differentiable function, for example easiest to
understand by
f (x) = x2 . Note that since we are looking at a one-dimensional problem,
drawing the
hyperplanes reduce to a line. Consider a line y = sx+c. Recall that we are reasoning as it
able to describe convex functions by their supporting hyperplanes, so let progresses.
us try to describe this function f (x) by its supporting lines. Fix the gradi-
ent of the line s 2 R and for each point (x0 , f (x0 )) on the graph of f , find
the minimum value of c such that the line still intersects (x0 , f (x0 )). Note
that the minimum value of c is the place where a line with slope s “just
touches” the function f (x) = x2 . The line passing through (x0 , f (x0 ))
with gradient s is given by
y f (x0 ) = s(x x0 ) . (7.54)
The y -intercept of this line is sx0 + f (x0 ). The minimum of c for which
y = sx + c intersects with the graph of f is therefore
inf sx0 + f (x0 ) . (7.55)
x0

The preceding convex conjugate is by convention defined to be the nega-


tive of this. The reasoning in this paragraph did not rely on the fact that
we chose a one-dimensional convex and differentiable function, and holds
for f : RD ! R, which are nonconvex and non-differentiable.
The classical
Remark. Convex differentiable functions such as the example f (x) = x2 is Legendre transform
a nice special case, where there is no need for the supremum, and there is is defined on convex
differentiable
a one-to-one correspondence between a function and its Legendre trans- functions in RD .
form. Let us derive this from first principles. For a convex differentiable
function, we know that at x0 the tangent touches f (x0 ) so that
f (x0 ) = sx0 + c . (7.56)
Recall that we want to describe the convex function f (x) in terms of its
gradient rx f (x), and that s = rx f (x0 ). We rearrange to get an expres-
sion for c to obtain
c = sx0 f (x0 ) . (7.57)
Note that c changes with x0 and therefore with s, which is why we can
think of it as a function of s, which we call
f ⇤ (s) := sx0 f (x0 ) . (7.58)
Comparing (7.58) with Definition 7.4, we see that (7.58) is a special case
(without the supremum). }
The conjugate function has nice properties; for example, for convex
functions, applying the Legendre transform again gets us back to the orig-
inal function. In the same way that the slope of f (x) is s, the slope of f ⇤ (s)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


244 Continuous Optimization

is x. The following two examples show common uses of convex conjugates


in machine learning.

Example 7.7 (Convex Conjugates)


To illustrate the application of convex conjugates, consider the quadratic
function
y>K 1y
f (y) = (7.59)
2
based on a positive definite matrix K 2 Rn⇥n . We denote the primal
variable to be y 2 Rn and the dual variable to be ↵ 2 Rn .
Applying Definition 7.4, we obtain the function
1
f ⇤ (↵) = sup hy, ↵i y>K y. (7.60)
y2Rn 2
Since the function is differentiable, we can find the maximum by taking
the derivative and with respect to y setting it to zero.
⇥ ⇤
@ hy, ↵i 2 y > K 1 y
= (↵ K 1 y)> (7.61)
@y
and hence when the gradient is zero we have y = 1 K↵. Substituting
into (7.60) yields
✓ ◆> ✓ ◆
⇤ 1 > 1 1 1 1 >
f (↵) = ↵ K↵ K↵ K K↵ = ↵ K↵ .
2 2
(7.62)

Example 7.8
In machine learning, we often use sums of functions; for example, the ob-
jective function of the training set includes a sum of the losses for each ex-
ample in the training set. In the following, we derive the convex conjugate
of a sum of losses `(t), where ` : R ! R. This also illustrates Pthe appli-
n
cation of the convex conjugate to the vector case. Let L(t) = i=1 `i (ti ).
Then,
n
X
L⇤ (z) = sup hz, ti `i (ti ) (7.63a)
t2Rn i=1
n
X
= sup zi t i `i (ti ) definition of dot product (7.63b)
t2Rn i=1

Xn
= sup zi ti `i (ti ) (7.63c)
n
i=1 t2R

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


7.3 Convex Optimization 245

n
X
= `⇤i (zi ) . definition of conjugate (7.63d)
i=1

Recall that in Section 7.2 we derived a dual optimization problem using


Lagrange multipliers. Furthermore, for convex optimization problems we
have strong duality, that is the solutions of the primal and dual problem
match. The Legendre–Fenchel transform described here also can be used
to derive a dual optimization problem. Furthermore, when the function
is convex and differentiable, the supremum is unique. To further investi-
gate the relation between these two approaches, let us consider a linear
equality constrained convex optimization problem.

Example 7.9
Let f (y) and g(x) be convex functions, and A a real matrix of appropriate
dimensions such that Ax = y . Then
min f (Ax) + g(x) = min f (y) + g(x). (7.64)
x Ax=y

By introducing the Lagrange multiplier u for the constraints Ax = y ,


min f (y) + g(x) = min max f (y) + g(x) + (Ax y)> u (7.65a)
Ax=y x,y u

= max min f (y) + g(x) + (Ax y)> u , (7.65b)


u x,y

where the last step of swapping max and min is due to the fact that f (y)
and g(x) are convex functions. By splitting up the dot product term and
collecting x and y ,
max min f (y) + g(x) + (Ax y)> u (7.66a)
u x,y
 h i
= max min y > u + f (y) + min(Ax)> u + g(x) (7.66b)
u y x
 h i
= max min y > u + f (y) + min x> A> u + g(x) (7.66c)
u y x

Recall the convex conjugate (Definition 7.4) and the fact that dot prod- For general inner
products, A> is
ucts are symmetric,
 replaced by the
h i adjoint A⇤ .
max min y > u + f (y) + min x> A> u + g(x) (7.67a)
u y x
>
= max f (u) ⇤ ⇤
g ( A u) . (7.67b)
u

Therefore, we have shown that


min f (Ax) + g(x) = max f ⇤ (u) g ⇤ ( A> u) . (7.68)
x u

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


246 Continuous Optimization

The Legendre–Fenchel conjugate turns out to be quite useful for ma-


chine learning problems that can be expressed as convex optimization
problems. In particular, for convex loss functions that apply independently
to each example, the conjugate loss is a convenient way to derive a dual
problem.

7.4 Further Reading


Continuous optimization is an active area of research, and we do not try
to provide a comprehensive account of recent advances.
From a gradient descent perspective, there are two major weaknesses
which each have their own set of literature. The first challenge is the fact
that gradient descent is a first-order algorithm, and does not use infor-
mation about the curvature of the surface. When there are long valleys,
the gradient points perpendicularly to the direction of interest. The idea
of momentum can be generalized to a general class of acceleration meth-
ods (Nesterov, 2018). Conjugate gradient methods avoid the issues faced
by gradient descent by taking previous directions into account (Shewchuk,
1994). Second-order methods such as Newton methods use the Hessian to
provide information about the curvature. Many of the choices for choos-
ing step-sizes and ideas like momentum arise by considering the curvature
of the objective function (Goh, 2017; Bottou et al., 2018). Quasi-Newton
methods such as L-BFGS try to use cheaper computational methods to ap-
proximate the Hessian (Nocedal and Wright, 2006). Recently there has
been interest in other metrics for computing descent directions, result-
ing in approaches such as mirror descent (Beck and Teboulle, 2003) and
natural gradient (Toussaint, 2012).
The second challenge is to handle non-differentiable functions. Gradi-
ent methods are not well defined when there are kinks in the function.
In these cases, subgradient methods can be used (Shor, 1985). For fur-
ther information and algorithms for optimizing non-differentiable func-
tions, we refer to the book by Bertsekas (1999). There is a vast amount
of literature on different approaches for numerically solving continuous
optimization problems, including algorithms for constrained optimization
problems. Good starting points to appreciate this literature are the books
by Luenberger (1969) and Bonnans et al. (2006). A recent survey of con-
Hugo Gonçalves’ tinuous optimization is provided by Bubeck (2015).
blog is also a good Modern applications of machine learning often mean that the size of
resource for an
datasets prohibit the use of batch gradient descent, and hence stochastic
easier introduction
to Legendre–Fenchel gradient descent is the current workhorse of large-scale machine learning
transforms: methods. Recent surveys of the literature include Hazan (2015) and Bot-
https://tinyurl. tou et al. (2018).
com/ydaal7hj
For duality and convex optimization, the book by Boyd and Vanden-
berghe (2004) includes lectures and slides online. A more mathematical
treatment is provided by Bertsekas (2009), and recent book by one of

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Exercises 247

the key researchers in the area of optimization is Nesterov (2018). Con-


vex optimization is based upon convex analysis, and the reader interested
in more foundational results about convex functions is referred to Rock-
afellar (1970), Hiriart-Urruty and Lemaréchal (2001), and Borwein and
Lewis (2006). Legendre–Fenchel transforms are also covered in the afore-
mentioned books on convex analysis, but a more beginner-friendly pre-
sentation is available at Zia et al. (2009). The role of Legendre–Fenchel
transforms in the analysis of convex optimization algorithms is surveyed
in Polyak (2016).

Exercises
7.1 Consider the univariate function

f (x) = x3 + 6x2 3x 5.

Find its stationary points and indicate whether they are maximum, mini-
mum, or saddle points.
7.2 Consider the update equation for stochastic gradient descent (Equation (7.15)).
Write down the update when we use a mini-batch size of one.
7.3 Consider whether the following statements are true or false:
a. The intersection of any two convex sets is convex.
b. The union of any two convex sets is convex.
c. The difference of a convex set A from another convex set B is convex.
7.4 Consider whether the following statements are true or false:
a. The sum of any two convex functions is convex.
b. The difference of any two convex functions is convex.
c. The product of any two convex functions is convex.
d. The maximum of any two convex functions is convex.
7.5 Express the following optimization problem as a standard linear program in
matrix notation
max p> x + ⇠
x2R2 , ⇠2R

subject to the constraints that ⇠ > 0, x0 6 0 and x1 6 3.


7.6 Consider the linear program illustrated in Figure 7.9,
 >
5 x1
min
x2R2 3 x2
2 3 2 3
2 2 33
6 2 47  687
6 7 x1 6 7
subject to 6
6 2 1 7 6 7
7 x2 6 6 5 7
4 0 1 5 4 15
0 1 8

Derive the dual linear program using Lagrange duality.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


248 Continuous Optimization

7.7 Consider the quadratic program illustrated in Figure 7.4,


 >   >
1 x1 2 1 x1 5 x1
min +
x2R2 2 x2 1 4 x2 3 x2
2 3 2 3
1 0 1

6 1 0 7 6 7
subject to 6 7 1 6 61 7
x
40 1 5 x2 41 5
0 1 1

Derive the dual quadratic program using Lagrange duality.


7.8 Consider the following convex optimization problem
1 >
min w w
w2RD 2
subject to w> x > 1 .

Derive the Lagrangian dual by introducing the Lagrange multiplier .


7.9 Consider the negative entropy of x 2 RD ,
D
X
f (x) = xd log xd .
d=1

Derive the convex conjugate function f ⇤ (s), by assuming the standard dot
product.
Hint: Take the gradient of an appropriate function and set the gradient to zero.
7.10 Consider the function
1 >
f (x) = x Ax + b> x + c ,
2
where A is strictly positive definite, which means that it is invertible. Derive
the convex conjugate of f (x).
Hint: Take the gradient of an appropriate function and set the gradient to zero.
7.11 The hinge loss (which is the loss used by the support vector machine) is
given by
L(↵) = max{0, 1 ↵} ,

If we are interested in applying gradient methods such as L-BFGS, and do


not want to resort to subgradient methods, we need to smooth the kink in
the hinge loss. Compute the convex conjugate of the hinge loss L⇤ ( ) where
is the dual variable. Add a `2 proximal term, and compute the conjugate
of the resulting function
L⇤ ( ) + 2
,
2
where is a given hyperparameter.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


Part II

Central Machine Learning Problems

249
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
8

When Models Meet Data

In the first part of the book, we introduced the mathematics that form
the foundations of many machine learning methods. The hope is that a
reader would be able to learn the rudimentary forms of the language of
mathematics from the first part, which we will now use to describe and
discuss machine learning. The second part of the book introduces four
pillars of machine learning:

Regression (Chapter 9)
Dimensionality reduction (Chapter 10)
Density estimation (Chapter 11)
Classification (Chapter 12)

The main aim of this part of the book is to illustrate how the mathematical
concepts introduced in the first part of the book can be used to design
machine learning algorithms that can be used to solve tasks within the
remit of the four pillars. We do not intend to introduce advanced machine
learning concepts, but instead to provide a set of practical methods that
allow the reader to apply the knowledge they gained from the first part
of the book. It also provides a gateway to the wider machine learning
literature for readers already familiar with the mathematics.

8.1 Data, Models, and Learning


It is worth at this point, to pause and consider the problem that a ma-
chine learning algorithm is designed to solve. As discussed in Chapter 1,
there are three major components of a machine learning system: data,
models, and learning. The main question of machine learning is “What do
we mean by good models?”. The word model has many subtleties, and we model
will revisit it multiple times in this chapter. It is also not entirely obvious
how to objectively define the word “good”. One of the guiding principles
of machine learning is that good models should perform well on unseen
data. This requires us to define some performance metrics, such as accu-
racy or distance from ground truth, as well as figuring out ways to do well
under these performance metrics. This chapter covers a few necessary bits
and pieces of mathematical and statistical language that are commonly

251
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
252 When Models Meet Data

Table 8.1 Example Name Gender Degree Postcode Age Annual salary
data from a Aditya M MSc W21BG 36 89563
fictitious human Bob M PhD EC1A1BA 47 123543
resource database Chloé F BEcon SW1A1BH 26 23989
that is not in a Daisuke M BSc SE207AT 68 138769
numerical format. Elisabeth F MBA SE10AA 33 113888

used to talk about machine learning models. By doing so, we briefly out-
line the current best practices for training a model such that the resulting
predictor does well on data that we have not yet seen.
As mentioned in Chapter 1, there are two different senses in which we
use the phrase “machine learning algorithm”: training and prediction. We
will describe these ideas in this chapter, as well as the idea of selecting
among different models. We will introduce the framework of empirical
risk minimization in Section 8.2, the principle of maximum likelihood in
Section 8.3, and the idea of probabilistic models in Section 8.4. We briefly
outline a graphical language for specifying probabilistic models in Sec-
tion 8.5 and finally discuss model selection in Section 8.6. The rest of this
section expands upon the three main components of machine learning:
data, models and learning.

8.1.1 Data as Vectors


We assume that our data can be read by a computer, and represented ade-
quately in a numerical format. Data is assumed to be tabular (Figure 8.1),
where we think of each row of the table as representing a particular in-
Data is assumed to stance or example, and each column to be a particular feature. In recent
be in a tidy years, machine learning has been applied to many types of data that do not
format (Wickham,
obviously come in the tabular numerical format, for example genomic se-
2014; Codd, 1990).
quences, text and image contents of a webpage, and social media graphs.
We do not discuss the important and challenging aspects of identifying
good features. Many of these aspects depend on domain expertise and re-
quire careful engineering, and, in recent years, they have been put under
the umbrella of data science (Stray, 2016; Adhikari and DeNero, 2018).
Even when we have data in tabular format, there are still choices to be
made to obtain a numerical representation. For example, in Table 8.1, the
gender column (a categorical variable) may be converted into numbers 0
representing “Male” and 1 representing “Female”. Alternatively, the gen-
der could be represented by numbers 1, +1, respectively (as shown in
Table 8.2). Furthermore, it is often important to use domain knowledge
when constructing the representation, such as knowing that university
degrees progress from bachelor’s to master’s to PhD or realizing that the
postcode provided is not just a string of characters but actually encodes
an area in London. In Table 8.2, we converted the data from Table 8.1
to a numerical format, and each postcode is represented as two numbers,

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.1 Data, Models, and Learning 253
Gender ID Degree Latitude Longitude Age Annual Salary Table 8.2 Example
(in degrees) (in degrees) (in thousands) data from a
-1 2 51.5073 0.1290 36 89.563 fictitious human
-1 3 51.5074 0.1275 47 123.543 resource database
+1 1 51.5071 0.1278 26 23.989 (see Table 8.1),
-1 1 51.5075 0.1281 68 138.769 converted to a
+1 2 51.5074 0.1278 33 113.888 numerical format.

a latitude and longitude. Even numerical data that could potentially be


directly read into a machine learning algorithm should be carefully con-
sidered for units, scaling, and constraints. Without additional information,
one should shift and scale all columns of the dataset such that they have
an empirical mean of 0 and an empirical variance of 1. For the purposes
of this book, we assume that a domain expert already converted data ap-
propriately, i.e., each input xn is a D-dimensional vector of real numbers,
which are called features, attributes, or covariates. We consider a dataset to feature
be of the form as illustrated by Table 8.2. Observe that we have dropped attribute
covariate
the Name column of Table 8.1 in the new numerical representation. There
are two main reasons why this is desirable: (1) we do not expect the iden-
tifier (the Name) to be informative for a machine learning task; and (2)
we may wish to anonymize the data to help protect the privacy of the
employees.
In this part of the book, we will use N to denote the number of exam-
ples in a dataset and index the examples with lowercase n = 1, . . . , N .
We assume that we are given a set of numerical data, represented as an
array of vectors (Table 8.2). Each row is a particular individual xn , often
referred to as an example or data point in machine learning. The subscript example
n refers to the fact that this is the nth example out of a total of N exam- data point
ples in the dataset. Each column represents a particular feature of interest
about the example, and we index the features as d = 1, . . . , D . Recall that
data is represented as vectors, which means that each example (each data
point) is a D-dimensional vector. The orientation of the table originates
from the database community, but for some machine learning algorithms
(e.g., in Chapter 10) it is more convenient to represent examples as col-
umn vectors.
Let us consider the problem of predicting annual salary from age, based
on the data in Table 8.2. This is called a supervised learning problem
where we have a label yn (the salary) associated with each example xn label
(the age). The label yn has various other names, including target, re-
sponse variable, and annotation. A dataset is written as a set of example-
label pairs {(x1 , y1 ), . . . , (xn , yn ), . . . , (xN , yN )}. The table of examples
{x1 , . . . , xN } is often concatenated, and written as X 2 RN ⇥D . Fig-
ure 8.1 illustrates the dataset consisting of the two rightmost columns
of Table 8.2, where x = age and y = salary.
We use the concepts introduced in the first part of the book to formalize

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


254 When Models Meet Data

Figure 8.1 Toy data


for linear regression. 150
Training data in
(xn , yn ) pairs from 125
the rightmost two
columns of
Table 8.2. We are
100 ?
interested in the
y
75
salary of a person
aged sixty (x = 60)
50
illustrated as a
vertical dashed red
line, which is not
25
part of the training
data. 0
0 10 20 30 40 50 60 70 80
x

the machine learning problems such as that in the previous paragraph.


Representing data as vectors xn allows us to use concepts from linear al-
gebra (introduced in Chapter 2). In many machine learning algorithms,
we need to additionally be able to compare two vectors. As we will see in
Chapters 9 and 12, computing the similarity or distance between two ex-
amples allows us to formalize the intuition that examples with similar fea-
tures should have similar labels. The comparison of two vectors requires
that we construct a geometry (explained in Chapter 3) and allows us to
optimize the resulting learning problem using techniques from Chapter 7.
Since we have vector representations of data, we can manipulate data to
find potentially better representations of it. We will discuss finding good
representations in two ways: finding lower-dimensional approximations
of the original feature vector, and using nonlinear higher-dimensional
combinations of the original feature vector. In Chapter 10, we will see an
example of finding a low-dimensional approximation of the original data
space by finding the principal components. Finding principal components
is closely related to concepts of eigenvalue and singular value decomposi-
tion as introduced in Chapter 4. For the high-dimensional representation,
feature map we will see an explicit feature map (·) that allows us to represent in-
puts xn using a higher-dimensional representation (xn ). The main mo-
tivation for higher-dimensional representations is that we can construct
new features as non-linear combinations of the original features, which in
turn may make the learning problem easier. We will discuss the feature
kernel map in Section 9.2 and show how this feature map leads to a kernel in
Section 12.4. In recent years, deep learning methods (Goodfellow et al.,
2016) have shown promise in using the data itself to learn new good fea-
tures and have been very successful in areas, such as computer vision,
speech recognition, and natural language processing. We will not cover
neural networks in this part of the book, but the reader is referred to

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.1 Data, Models, and Learning 255

Figure 8.2 Example


150 function (black solid
diagonal line) and
125 its prediction at
x = 60, i.e.,
100 f (60) = 100.
y

75

50

25

0
0 10 20 30 40 50 60 70 80
x

Section 5.6 for the mathematical description of backpropagation, a key


concept for training neural networks.

8.1.2 Models as Functions


Once we have data in an appropriate vector representation, we can get to
the business of constructing a predictive function (known as a predictor). predictor
In Chapter 1, we did not yet have the language to be precise about models.
Using the concepts from the first part of the book, we can now introduce
what “model” means. We present two major approaches in this book: a
predictor as a function, and a predictor as a probabilistic model. We de-
scribe the former here and the latter in the next subsection.
A predictor is a function that, when given a particular input example
(in our case, a vector of features), produces an output. For now, consider
the output to be a single number, i.e., a real-valued scalar output. This can
be written as
f : RD ! R , (8.1)
where the input vector x is D-dimensional (has D features), and the func-
tion f then applied to it (written as f (x)) returns a real number. Fig-
ure 8.2 illustrates a possible function that can be used to compute the
value of the prediction for input values x.
In this book, we do not consider the general case of all functions, which
would involve the need for functional analysis. Instead, we consider the
special case of linear functions
f (x) = ✓ > x + ✓0 (8.2)
for unknown ✓ and ✓0 . This restriction means that the contents of Chap-
ters 2 and 3 suffice for precisely stating the notion of a predictor for
the non-probabilistic (in contrast to the probabilistic view described next)

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


256 When Models Meet Data

Figure 8.3 Example


function (black solid 150
diagonal line) and
its predictive 125
uncertainty at
x = 60 (drawn as a 100
Gaussian).
y
75

50

25

0
0 10 20 30 40 50 60 70 80
x

view of machine learning. Linear functions strike a good balance between


the generality of the problems that can be solved and the amount of back-
ground mathematics that is needed.

8.1.3 Models as Probability Distributions


We often consider data to be noisy observations of some true underlying
effect, and hope that by applying machine learning we can identify the
signal from the noise. This requires us to have a language for quantify-
ing the effect of noise. We often would also like to have predictors that
express some sort of uncertainty, e.g., to quantify the confidence we have
about the value of the prediction for a particular test data point. As we
have seen in Chapter 6, probability theory provides a language for quan-
tifying uncertainty. Figure 8.3 illustrates the predictive uncertainty of the
function as a Gaussian distribution.
Instead of considering a predictor as a single function, we could con-
sider predictors to be probabilistic models, i.e., models describing the dis-
tribution of possible functions. We limit ourselves in this book to the spe-
cial case of distributions with finite-dimensional parameters, which allows
us to describe probabilistic models without needing stochastic processes
and random measures. For this special case, we can think about prob-
abilistic models as multivariate probability distributions, which already
allow for a rich class of models.
We will introduce how to use concepts from probability (Chapter 6) to
define machine learning models in Section 8.4, and introduce a graphical
language for describing probabilistic models in a compact way in Sec-
tion 8.5.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.1 Data, Models, and Learning 257

8.1.4 Learning is Finding Parameters


The goal of learning is to find a model and its corresponding parame-
ters such that the resulting predictor will perform well on unseen data.
There are conceptually three distinct algorithmic phases when discussing
machine learning algorithms:

1. Prediction or inference
2. Training or parameter estimation
3. Hyperparameter tuning or model selection

The prediction phase is when we use a trained predictor on previously un-


seen test data. In other words, the parameters and model choice is already
fixed and the predictor is applied to new vectors representing new input
data points. As outlined in Chapter 1 and the previous subsection, we will
consider two schools of machine learning in this book, corresponding to
whether the predictor is a function or a probabilistic model. When we
have a probabilistic model (discussed further in Section 8.4) the predic-
tion phase is called inference.
Remark. Unfortunately, there is no agreed upon naming for the different
algorithmic phases. The word “inference” is sometimes also used to mean
parameter estimation of a probabilistic model, and less often may be also
used to mean prediction for non-probabilistic models. }
The training or parameter estimation phase is when we adjust our pre-
dictive model based on training data. We would like to find good predic-
tors given training data, and there are two main strategies for doing so:
finding the best predictor based on some measure of quality (sometimes
called finding a point estimate), or using Bayesian inference. Finding a
point estimate can be applied to both types of predictors, but Bayesian
inference requires probabilistic models.
For the non-probabilistic model, we follow the principle of empirical risk empirical risk
minimization, which we describe in Section 8.2. Empirical risk minimiza- minimization
tion directly provides an optimization problem for finding good parame-
ters. With a statistical model, the principle of maximum likelihood is used maximum likelihood
to find a good set of parameters (Section 8.3). We can additionally model
the uncertainty of parameters using a probabilistic model, which we will
look at in more detail in Section 8.4.
We use numerical methods to find good parameters that “fit” the data,
and most training methods can be thought of as hill-climbing approaches
to find the maximum of an objective, for example the maximum of a likeli-
hood. To apply hill-climbing approaches we use the gradients described in The convention in
Chapter 5 and implement numerical optimization approaches from Chap- optimization is to
minimize objectives.
ter 7.
Hence, there is often
As mentioned in Chapter 1, we are interested in learning a model based an extra minus sign
on data such that it performs well on future data. It is not enough for in machine learning
objectives.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


258 When Models Meet Data

the model to only fit the training data well, the predictor needs to per-
form well on unseen data. We simulate the behavior of our predictor on
cross-validation future unseen data using cross-validation (Section 8.2.4). As we will see
in this chapter, to achieve the goal of performing well on unseen data,
we will need to balance between fitting well on training data and finding
“simple” explanations of the phenomenon. This trade-off is achieved us-
ing regularization (Section 8.2.3) or by adding a prior (Section 8.3.2). In
philosophy, this is considered to be neither induction nor deduction, but
abduction is called abduction. According to the Stanford Encyclopedia of Philosophy,
abduction is the process of inference to the best explanation (Douven,
A good movie title is 2017).
“AI abduction”. We often need to make high-level modeling decisions about the struc-
ture of the predictor, such as the number of components to use or the
class of probability distributions to consider. The choice of the number of
hyperparameter components is an example of a hyperparameter, and this choice can af-
fect the performance of the model significantly. The problem of choosing
model selection among different models is called model selection, which we describe in
Section 8.6. For non-probabilistic models, model selection is often done
nested using nested cross-validation, which is described in Section 8.6.1. We also
cross-validation use model selection to choose hyperparameters of our model.
Remark. The distinction between parameters and hyperparameters is some-
what arbitrary, and is mostly driven by the distinction between what can
be numerically optimized versus what needs to use search techniques.
Another way to consider the distinction is to consider parameters as the
explicit parameters of a probabilistic model, and to consider hyperparam-
eters (higher-level parameters) as parameters that control the distribution
of these explicit parameters. }
In the following sections, we will look at three flavors of machine learn-
ing: empirical risk minimization (Section 8.2), the principle of maximum
likelihood (Section 8.3), and probabilistic modeling (Section 8.4).

8.2 Empirical Risk Minimization


After having all the mathematics under our belt, we are now in a posi-
tion to introduce what it means to learn. The “learning” part of machine
learning boils down to estimating parameters based on training data.
In this section, we consider the case of a predictor that is a function,
and consider the case of probabilistic models in Section 8.3. We describe
the idea of empirical risk minimization, which was originally popularized
by the proposal of the support vector machine (described in Chapter 12).
However, its general principles are widely applicable and allow us to ask
the question of what is learning without explicitly constructing probabilis-
tic models. There are four main design choices, which we will cover in
detail in the following subsections:

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.2 Empirical Risk Minimization 259

Section 8.2.1 What is the set of functions we allow the predictor to take?
Section 8.2.2 How do we measure how well the predictor performs on
the training data?
Section 8.2.3 How do we construct predictors from only training data
that performs well on unseen test data?
Section 8.2.4 What is the procedure for searching over the space of mod-
els?

8.2.1 Hypothesis Class of Functions


Assume we are given N examples xn 2 RD and corresponding scalar la-
bels yn 2 R. We consider the supervised learning setting, where we obtain
pairs (x1 , y1 ), . . . , (xN , yN ). Given this data, we would like to estimate a
predictor f (·, ✓) : RD ! R, parametrized by ✓ . We hope to be able to find
a good parameter ✓ ⇤ such that we fit the data well, that is,
f (xn , ✓ ⇤ ) ⇡ yn for all n = 1, . . . , N . (8.3)
In this section, we use the notation ŷn = f (xn , ✓ ⇤ ) to represent the output
of the predictor.
Remark. For ease of presentation, we will describe empirical risk mini-
mization in terms of supervised learning (where we have labels). This
simplifies the definition of the hypothesis class and the loss function. It
is also common in machine learning to choose a parametrized class of
functions, for example affine functions. }

Example 8.1
We introduce the problem of ordinary least-squares regression to illustrate
empirical risk minimization. A more comprehensive account of regression
is given in Chapter 9. When the label yn is real-valued, a popular choice
of function class for predictors is the set of affine functions. We choose a Affine functions are
often referred to as
more compact notation for an affine function by concatenating an addi-
linear functions in
tional unit feature x(0) = 1 to xn , i.e., xn = [1, x(1)
n , xn , . . . , xn ] . The
(2) (D) >
machine learning.
parameter vector is correspondingly ✓ = [✓0 , ✓1 , ✓2 , . . . , ✓D ]> , allowing us
to write the predictor as a linear function
f (xn , ✓) = ✓ > xn . (8.4)
This linear predictor is equivalent to the affine model
D
X
f (xn , ✓) = ✓0 + ✓d x(d)
n . (8.5)
d=1

The predictor takes the vector of features representing a single example


xn as input and produces a real-valued output, i.e., f : RD+1 ! R. The

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


260 When Models Meet Data

previous figures in this chapter had a straight line as a predictor, which


means that we have assumed an affine function.
Instead of a linear function, we may wish to consider non-linear func-
tions as predictors. Recent advances in neural networks allow for efficient
computation of more complex non-linear function classes.

Given the class of functions, we want to search for a good predictor.


We now move on to the second ingredient of empirical risk minimization:
how to measure how well the predictor fits the training data.

8.2.2 Loss Function for Training


Consider the label yn for a particular example; and the corresponding pre-
diction ŷn that we make based on xn . To define what it means to fit the
loss function data well, we need to specify a loss function `(yn , ŷn ) that takes the ground
truth label and the prediction as input and produces a non-negative num-
ber (referred to as the loss) representing how much error we have made
The expression on this particular prediction. Our goal for finding a good parameter vector
“error” is often used ✓ ⇤ is to minimize the average loss on the set of N training examples.
to mean loss.
One assumption that is commonly made in machine learning is that
independent and the set of examples (x1 , y1 ), . . . , (xN , yN ) is independent and identically
identically distributed. The word independent (Section 6.4.5) means that two data
distributed
points (xi , yi ) and (xj , yj ) do not statistically depend on each other, mean-
ing that the empirical mean is a good estimate of the population mean
(Section 6.4.1). This implies that we can use the empirical mean of the
training set loss on the training data. For a given training set {(x1 , y1 ), . . . , (xN , yN )},
we introduce the notation of an example matrix X := [x1 , . . . , xN ]> 2
RN ⇥D and a label vector y := [y1 , . . . , yN ]> 2 RN . Using this matrix
notation the average loss is given by
N
1 X
Remp (f, X, y) = `(yn , ŷn ) , (8.6)
N n=1

empirical risk where ŷn = f (xn , ✓). Equation (8.6) is called the empirical risk and de-
pends on three arguments, the predictor f and the data X, y . This general
empirical risk strategy for learning is called empirical risk minimization.
minimization

Example 8.2 (Least-Squares Loss)


Continuing the example of least-squares regression, we specify that we
measure the cost of making an error during training using the squared
loss `(yn , ŷn ) = (yn ŷn )2 . We wish to minimize the empirical risk (8.6),

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.2 Empirical Risk Minimization 261

which is the average of the losses over the data


N
1 X
minD (yn f (xn , ✓))2 , (8.7)
✓2R N n=1
where we substituted the predictor ŷn = f (xn , ✓). By using our choice of
a linear predictor f (xn , ✓) = ✓ > xn , we obtain the optimization problem
N
1 X
minD (yn ✓ > xn ) 2 . (8.8)
✓2R N n=1
This equation can be equivalently expressed in matrix form
1 2
minD ky X✓k . (8.9)
✓2R N
This is known as the least-squares problem. There exists a closed-form an- least-squares
problem
alytic solution for this by solving the normal equations, which we will
discuss in Section 9.2.

We are not interested in a predictor that only performs well on the


training data. Instead, we seek a predictor that performs well (has low
risk) on unseen test data. More formally, we are interested in finding a
predictor f (with parameters fixed) that minimizes the expected risk expected risk

Rtrue (f ) = Ex,y [`(y, f (x))] , (8.10)

where y is the label and f (x) is the prediction based on the example x.
The notation Rtrue (f ) indicates that this is the true risk if we had access to
an infinite amount of data. The expectation is over the (infinite) set of all Another phrase
possible data and labels. There are two practical questions that arise from commonly used for
expected risk is
our desire to minimize expected risk, which we address in the following
“population risk”.
two subsections:

How should we change our training procedure to generalize well?


How do we estimate expected risk from (finite) data?

Remark. Many machine learning tasks are specified with an associated


performance measure, e.g., accuracy of prediction or root mean squared
error. The performance measure could be more complex, be cost sensitive,
and capture details about the particular application. In principle, the de-
sign of the loss function for empirical risk minimization should correspond
directly to the performance measure specified by the machine learning
task. In practice, there is often a mismatch between the design of the loss
function and the performance measure. This could be due to issues such
as ease of implementation or efficiency of optimization. }

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


262 When Models Meet Data

8.2.3 Regularization to Reduce Overfitting


This section describes an addition to empirical risk minimization that al-
lows it to generalize well (approximately minimizing expected risk). Re-
call that the aim of training a machine learning predictor is so that we can
perform well on unseen data, i.e., the predictor generalizes well. We sim-
ulate this unseen data by holding out a proportion of the whole dataset.
test set This hold out set is referred to as the test set. Given a sufficiently rich class
Even knowing only of functions for the predictor f , we can essentially memorize the training
the performance of
data to obtain zero empirical risk. While this is great to minimize the loss
the predictor on the
test set leaks (and therefore the risk) on the training data, we would not expect the
information (Blum predictor to generalize well to unseen data. In practice, we have only a
and Hardt, 2015). finite set of data, and hence we split our data into a training and a test
set. The training set is used to fit the model, and the test set (not seen
by the machine learning algorithm during training) is used to evaluate
generalization performance. It is important for the user to not cycle back
to a new round of training after having observed the test set. We use the
subscripts train and test to denote the training and test sets, respectively.
We will revisit this idea of using a finite dataset to evaluate expected risk
in Section 8.2.4.
overfitting It turns out that empirical risk minimization can lead to overfitting, i.e.,
the predictor fits too closely to the training data and does not general-
ize well to new data (Mitchell, 1997). This general phenomenon of hav-
ing very small average loss on the training set but large average loss on
the test set tends to occur when we have little data and a complex hy-
pothesis class. For a particular predictor f (with parameters fixed), the
phenomenon of overfitting occurs when the risk estimate from the train-
ing data Remp (f, X train , y train ) underestimates the expected risk Rtrue (f ).
Since we estimate the expected risk Rtrue (f ) by using the empirical risk
on the test set Remp (f, X test , y test ) if the test risk is much larger than
the training risk, this is an indication of overfitting. We revisit the idea of
overfitting in Section 8.3.3.
Therefore, we need to somehow bias the search for the minimizer of
empirical risk by introducing a penalty term, which makes it harder for
the optimizer to return an overly flexible predictor. In machine learning,
regularization the penalty term is referred to as regularization. Regularization is a way
to compromise between accurate solution of empirical risk minimization
and the size or complexity of the solution.

Example 8.3 (Regularized Least Squares)


Regularization is an approach that discourages complex or extreme solu-
tions to an optimization problem. The simplest regularization strategy is

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.2 Empirical Risk Minimization 263

to replace the least-squares problem


1 2
ky X✓k .
min (8.11)
✓ N

in the previous example with the “regularized” problem by adding a


penalty term involving only ✓ :
1 2 2
min ky X✓k + k✓k . (8.12)
✓ N
2
The additional term k✓k is called the regularizer, and the parameter regularizer
is the regularization parameter. The regularization parameter trades regularization
parameter
off minimizing the loss on the training set and the magnitude of the pa-
rameters ✓ . It often happens that the magnitude of the parameter values
becomes relatively large if we run into overfitting (Bishop, 2006).

The regularization term is sometimes called the penalty term, which bi- penalty term
ases the vector ✓ to be closer to the origin. The idea of regularization also
appears in probabilistic models as the prior probability of the parameters.
Recall from Section 6.6 that for the posterior distribution to be of the same
form as the prior distribution, the prior and the likelihood need to be con-
jugate. We will revisit this idea in Section 8.3.2. We will see in Chapter 12
that the idea of the regularizer is equivalent to the idea of a large margin.

8.2.4 Cross-Validation to Assess the Generalization Performance


We mentioned in the previous section that we measure the generalization
error by estimating it by applying the predictor on test data. This data is
also sometimes referred to as the validation set. The validation set is a sub- validation set
set of the available training data that we keep aside. A practical issue with
this approach is that the amount of data is limited, and ideally we would
use as much of the data available to train the model. This would require
us to keep our validation set V small, which then would lead to a noisy
estimate (with high variance) of the predictive performance. One solu-
tion to these contradictory objectives (large training set, large validation
set) is to use cross-validation. K -fold cross-validation effectively partitions cross-validation
the data into K chunks, K 1 of which form the training set R, and
the last chunk serves as the validation set V (similar to the idea outlined
previously). Cross-validation iterates through (ideally) all combinations
of assignments of chunks to R and V ; see Figure 8.4. This procedure is
repeated for all K choices for the validation set, and the performance of
the model from the K runs is averaged.
We partition our dataset into two sets D = R [ V , such that they do not
overlap (R \ V = ;), where V is the validation set, and train our model
on R. After training, we assess the performance of the predictor f on the

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


264 When Models Meet Data

Figure 8.4 K-fold


cross-validation. Training
The dataset is
divided into K = 5
chunks, K 1 of
which serve as the
training set (blue)
and one as the Validation
validation set
(orange hatch).

validation set V (e.g., by computing root mean square error (RMSE) of


the trained model on the validation set). More precisely, for each partition
k the training data R(k) produces a predictor f (k) , which is then applied
to validation set V (k) to compute the empirical risk R(f (k) , V (k) ). We cycle
through all possible partitionings of validation and training sets and com-
pute the average generalization error of the predictor. Cross-validation
approximates the expected generalization error

K
1 X
EV [R(f, V)] ⇡ R(f (k) , V (k) ) , (8.13)
K k=1

where R(f (k) , V (k) ) is the risk (e.g., RMSE) on the validation set V (k) for
predictor f (k) . The approximation has two sources: first, due to the finite
training set, which results in not the best possible f (k) ; and second, due to
the finite validation set, which results in an inaccurate estimation of the
risk R(f (k) , V (k) ). A potential disadvantage of K -fold cross-validation is
the computational cost of training the model K times, which can be bur-
densome if the training cost is computationally expensive. In practice, it
is often not sufficient to look at the direct parameters alone. For example,
we need to explore multiple complexity parameters (e.g., multiple regu-
larization parameters), which may not be direct parameters of the model.
Evaluating the quality of the model, depending on these hyperparameters,
may result in a number of training runs that is exponential in the number
of model parameters. One can use nested cross-validation (Section 8.6.1)
to search for good hyperparameters.
embarrassingly However, cross-validation is an embarrassingly parallel problem, i.e., lit-
parallel tle effort is needed to separate the problem into a number of parallel
tasks. Given sufficient computing resources (e.g., cloud computing, server
farms), cross-validation does not require longer than a single performance
assessment.
In this section, we saw that empirical risk minimization is based on the
following concepts: the hypothesis class of functions, the loss function and
regularization. In Section 8.3, we will see the effect of using a probability
distribution to replace the idea of loss functions and regularization.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.3 Parameter Estimation 265

8.2.5 Further Reading


Due to the fact that the original development of empirical risk minimiza-
tion (Vapnik, 1998) was couched in heavily theoretical language, many
of the subsequent developments have been theoretical. The area of study
is called statistical learning theory (Vapnik, 1999; Evgeniou et al., 2000; statistical learning
Hastie et al., 2001; von Luxburg and Schölkopf, 2011). A recent machine theory
learning textbook that builds on the theoretical foundations and develops
efficient learning algorithms is Shalev-Shwartz and Ben-David (2014).
The concept of regularization has its roots in the solution of ill-posed in-
verse problems (Neumaier, 1998). The approach presented here is called
Tikhonov regularization, and there is a closely related constrained version Tikhonov
called Ivanov regularization. Tikhonov regularization has deep relation- regularization
ships to the bias-variance trade-off and feature selection (Bühlmann and
Van De Geer, 2011). An alternative to cross-validation is bootstrap and
jackknife (Efron and Tibshirani, 1993; Davidson and Hinkley, 1997; Hall,
1992).
Thinking about empirical risk minimization (Section 8.2) as “probabil-
ity free” is incorrect. There is an underlying unknown probability distri-
bution p(x, y) that governs the data generation. However, the approach
of empirical risk minimization is agnostic to that choice of distribution.
This is in contrast to standard statistical approaches that explicitly re-
quire the knowledge of p(x, y). Furthermore, since the distribution is a
joint distribution on both examples x and labels y , the labels can be non-
deterministic. In contrast to standard statistics we do not need to specify
the noise distribution for the labels y .

8.3 Parameter Estimation


In Section 8.2, we did not explicitly model our problem using probability
distributions. In this section, we will see how to use probability distribu-
tions to model our uncertainty due to the observation process and our
uncertainty in the parameters of our predictors. In Section 8.3.1, we in-
troduce the likelihood, which is analogous to the concept of loss functions
(Section 8.2.2) in empirical risk minimization. The concept of priors (Sec-
tion 8.3.2) is analogous to the concept of regularization (Section 8.2.3).

8.3.1 Maximum Likelihood Estimation


The idea behind maximum likelihood estimation (MLE) is to define a func- maximum likelihood
tion of the parameters that enables us to find a model that fits the data estimation
well. The estimation problem is focused on the likelihood function, or likelihood
more precisely its negative logarithm. For data represented by a random
variable x and for a family of probability densities p(x | ✓) parametrized
by ✓ , the negative log-likelihood is given by negative
log-likelihood

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


266 When Models Meet Data

Lx (✓) = log p(x | ✓) . (8.14)

The notation Lx (✓) emphasizes the fact that the parameter ✓ is varying
and the data x is fixed. We very often drop the reference to x when writing
the negative log-likelihood, as it is really a function of ✓ , and write it as
L(✓) when the random variable representing the uncertainty in the data
is clear from the context.
Let us interpret what the probability density p(x | ✓) is modeling for a
fixed value of ✓ . It is a distribution that models the uncertainty of the data.
In other words, once we have chosen the type of function we want as a
predictor, the likelihood provides the probability of observing data x.
In a complementary view, if we consider the data to be fixed (because
it has been observed), and we vary the parameters ✓ , what does L(✓) tell
us? It tells us how likely a particular setting of ✓ is for the observations x.
Based on this second view, the maximum likelihood estimator gives us the
most likely parameter ✓ for the set of data.
We consider the supervised learning setting, where we obtain pairs
(x1 , y1 ), . . . , (xN , yN ) with xn 2 RD and labels yn 2 R. We are inter-
ested in constructing a predictor that takes a feature vector xn as input
and produces a prediction yn (or something close to it), i.e., given a vec-
tor xn we want the probability distribution of the label yn . In other words,
we specify the conditional probability distribution of the labels given the
examples for the particular parameter setting ✓ .

Example 8.4
The first example that is often used is to specify that the conditional
probability of the labels given the examples is a Gaussian distribution. In
other words, we assume that we can explain our observation uncertainty
by independent Gaussian noise (refer to Section 6.5) with zero mean,
"n ⇠ N 0, 2 . We further assume that the linear model x> n ✓ is used for
prediction. This means we specify a Gaussian likelihood for each example
label pair (xn , yn ),
p(yn | xn , ✓) = N yn | x>
n ✓,
2
. (8.15)
An illustration of a Gaussian likelihood for a given parameter ✓ is shown
in Figure 8.3. We will see in Section 9.2 how to explicitly expand the
preceding expression out in terms of the Gaussian distribution.

independent and We assume that the set of examples (x1 , y1 ), . . . , (xN , yN ) are independent
identically and identically distributed (i.i.d.). The word “independent” (Section 6.4.5)
distributed
implies that the likelihood of the whole dataset (Y = {y1 , . . . , yN } and
X = {x1 , . . . , xN } factorizes into a product of the likelihoods of each

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.3 Parameter Estimation 267

individual example
N
Y
p(Y | X , ✓) = p(yn | xn , ✓) , (8.16)
n=1

where p(yn | xn , ✓) is a particular distribution (which was Gaussian in Ex-


ample 8.4). The expression “identically distributed” means that each term
in the product (8.16) is of the same distribution, and all of them share
the same parameters. It is often easier from an optimization viewpoint to
compute functions that can be decomposed into sums of simpler functions.
Hence, in machine learning we often consider the negative log-likelihood Recall log(ab) =
log(a) + log(b)
N
X
L(✓) = log p(Y | X , ✓) = log p(yn | xn , ✓) . (8.17)
n=1

While it is temping to interpret the fact that ✓ is on the right of the condi-
tioning in p(yn |xn , ✓) (8.15), and hence should be interpreted as observed
and fixed, this interpretation is incorrect. The negative log-likelihood L(✓)
is a function of ✓ . Therefore, to find a good parameter vector ✓ that
explains the data (x1 , y1 ), . . . , (xN , yN ) well, minimize the negative log-
likelihood L(✓) with respect to ✓ .
Remark. The negative sign in (8.17) is a historical artifact that is due
to the convention that we want to maximize likelihood, but numerical
optimization literature tends to study minimization of functions. }

Example 8.5
Continuing on our example of Gaussian likelihoods (8.15), the negative
log-likelihood can be rewritten as
N
X N
X
L(✓) = log p(yn | xn , ✓) = log N yn | x>
n ✓,
2
(8.18a)
n=1 n=1
N
X ✓ ◆
1 (yn x>
n ✓)
2
= log p exp (8.18b)
n=1 2⇡ 2 2 2
XN ✓ ◆ XN
(yn x>
n ✓)
2
1
= log exp 2
log p (8.18c)
n=1
2 n=1 2⇡ 2

N N
1 X X 1
= 2
(yn x>
n ✓)
2
log p . (8.18d)
2 n=1 n=1 2⇡ 2

As is given, the second term in (8.18d) is constant, and minimizing L(✓)


corresponds to solving the least-squares problem (compare with (8.8))
expressed in the first term.

It turns out that for Gaussian likelihoods the resulting optimization

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


268 When Models Meet Data

Figure 8.5 For the


given data, the 150
maximum likelihood
estimate of the 125
parameters results
in the black 100
diagonal line. The
orange square
y
75
shows the value of
the maximum
50
likelihood
prediction at
x = 60.
25

0
0 10 20 30 40 50 60 70 80
x

Figure 8.6
Comparing the 150 MLE
predictions with the MAP
maximum likelihood 125
estimate and the
MAP estimate at
100
x = 60. The prior
biases the slope to
y

75
be less steep and the
intercept to be
50
closer to zero. In
this example, the
bias that moves the 25
intercept closer to
zero actually 0
0 10 20 30 40 50 60 70 80
increases the slope.
x

problem corresponding to maximum likelihood estimation has a closed-


form solution. We will see more details on this in Chapter 9. Figure 8.5
shows a regression dataset and the function that is induced by the maxi-
mum-likelihood parameters. Maximum likelihood estimation may suffer
from overfitting (Section 8.3.3), analogous to unregularized empirical risk
minimization (Section 9.2.3). For other likelihood functions, i.e., if we
model our noise with non-Gaussian distributions, maximum likelihood es-
timation may not have a closed-form analytic solution. In this case, we
resort to numerical optimization methods discussed in Chapter 7.

8.3.2 Maximum A Posteriori Estimation


If we have prior knowledge about the distribution of the parameters ✓ , we
can multiply an additional term to the likelihood. This additional term is
a prior probability distribution on parameters p(✓). For a given prior, after

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.3 Parameter Estimation 269

observing some data x, how should we update the distribution of ✓ ? In


other words, how should we represent the fact that we have more specific
knowledge of ✓ after observing data x? Bayes’ theorem, as discussed in
Section 6.3, gives us a principled tool to update our probability distribu-
tions of random variables. It allows us to compute a posterior distribution posterior
p(✓ | x) (the more specific knowledge) on the parameters ✓ from general
prior statements (prior distribution) p(✓) and the function p(x | ✓) that prior
links the parameters ✓ and the observed data x (called the likelihood): likelihood

p(x | ✓)p(✓)
p(✓ | x) = . (8.19)
p(x)
Recall that we are interested in finding the parameter ✓ that maximizes
the posterior. Since the distribution p(x) does not depend on ✓ , we can
ignore the value of the denominator for the optimization and obtain
p(✓ | x) / p(x | ✓)p(✓) . (8.20)
The preceding proportion relation hides the density of the data p(x),
which may be difficult to estimate. Instead of estimating the minimum
of the negative log-likelihood, we now estimate the minimum of the neg-
ative log-posterior, which is referred to as maximum a posteriori estima- maximum a
tion (MAP estimation). An illustration of the effect of adding a zero-mean posteriori
estimation
Gaussian prior is shown in Figure 8.6.
MAP estimation

Example 8.6
In addition to the assumption of Gaussian likelihood in the previous exam-
ple, we assume that the parameter vector is distributed as a multivariate
Gaussian with zero mean, i.e., p(✓) = N 0, ⌃ , where ⌃ is the covari-
ance matrix (Section 6.5). Note that the conjugate prior of a Gaussian
is also a Gaussian (Section 6.6.1), and therefore we expect the posterior
distribution to also be a Gaussian. We will see the details of maximum a
posteriori estimation in Chapter 9.

The idea of including prior knowledge about where good parameters


lie is widespread in machine learning. An alternative view, which we saw
in Section 8.2.3, is the idea of regularization, which introduces an addi-
tional term that biases the resulting parameters to be close to the origin.
Maximum a posteriori estimation can be considered to bridge the non-
probabilistic and probabilistic worlds as it explicitly acknowledges the
need for a prior distribution but it still only produces a point estimate
of the parameters.
Remark. The maximum likelihood estimate ✓ ML possesses the following
properties (Lehmann and Casella, 1998; Efron and Hastie, 2016):

Asymptotic consistency: The MLE converges to the true value in the

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


270 When Models Meet Data

Figure 8.7 Model


fitting. In a M✓
parametrized class
M✓ of models, we
optimize the model M✓ ⇤
parameters ✓ to M⇤
minimize the
M✓ 0
distance to the true
(unknown) model
M ⇤.

limit of infinitely many observations, plus a random error that is ap-


proximately normal.
The size of the samples necessary to achieve these properties can be
quite large.
The error’s variance decays in 1/N , where N is the number of data
points.
Especially, in the “small” data regime, maximum likelihood estimation
can lead to overfitting.
}
The principle of maximum likelihood estimation (and maximum a pos-
teriori estimation) uses probabilistic modeling to reason about the uncer-
tainty in the data and model parameters. However, we have not yet taken
probabilistic modeling to its full extent. In this section, the resulting train-
ing procedure still produces a point estimate of the predictor, i.e., training
returns one single set of parameter values that represent the best predic-
tor. In Section 8.4, we will take the view that the parameter values should
also be treated as random variables, and instead of estimating “best” val-
ues of that distribution, we will use the full parameter distribution when
making predictions.

8.3.3 Model Fitting


Consider the setting where we are given a dataset, and we are interested
in fitting a parametrized model to the data. When we talk about “fit-
ting”, we typically mean optimizing/learning model parameters so that
they minimize some loss function, e.g., the negative log-likelihood. With
maximum likelihood (Section 8.3.1) and maximum a posteriori estima-
tion (Section 8.3.2), we already discussed two commonly used algorithms
for model fitting.
The parametrization of the model defines a model class M✓ with which
we can operate. For example, in a linear regression setting, we may define
the relationship between inputs x and (noise-free) observations y to be
y = ax + b, where ✓ := {a, b} are the model parameters. In this case, the
model parameters ✓ describe the family of affine functions, i.e., straight
lines with slope a, which are offset from 0 by b. Assume the data comes

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.3 Parameter Estimation 271
4 Training data 4 Training data 4 Training data
Figure 8.8 Fitting
MLE MLE MLE (by maximum
2 2 2
likelihood) of
0 0 0
different model
y

y
2 2 2
classes to a
4 4 4
regression dataset.
4 2 0 2 4 4 2 0 2 4 4 2 0 2 4
x x x

(a) Overfitting (b) Underfitting. (c) Fitting well.

from a model M ⇤ , which is unknown to us. For a given training dataset,


we optimize ✓ so that M✓ is as close as possible to M ⇤ , where the “close-
ness” is defined by the objective function we optimize (e.g., squared loss
on the training data). Figure 8.7 illustrates a setting where we have a small
model class (indicated by the circle M✓ ), and the data generation model
M ⇤ lies outside the set of considered models. We begin our parameter
search at M✓0 . After the optimization, i.e., when we obtain the best pos-
sible parameters ✓ ⇤ , we distinguish three different cases: (i) overfitting,
(ii) underfitting, and (iii) fitting well. We will give a high-level intuition
of what these three concepts mean.
Roughly speaking, overfitting refers to the situation where the para- overfitting
metrized model class is too rich to model the dataset generated by M ⇤ ,
i.e., M✓ could model much more complicated datasets. For instance, if the
dataset was generated by a linear function, and we define M✓ to be the
class of seventh-order polynomials, we could model not only linear func-
tions, but also polynomials of degree two, three, etc. Models that over-
fit typically have a large number of parameters. An observation we often One way to detect
make is that the overly flexible model class M✓ uses all its modeling power overfitting in
practice is to
to reduce the training error. If the training data is noisy, it will therefore
observe that the
find some useful signal in the noise itself. This will cause enormous prob- model has low
lems when we predict away from the training data. Figure 8.8(a) gives an training risk but
example of overfitting in the context of regression where the model pa- high test risk during
cross validation
rameters are learned by means of maximum likelihood (see Section 8.3.1).
(Section 8.2.4).
We will discuss overfitting in regression more in Section 9.2.2.
When we run into underfitting, we encounter the opposite problem underfitting
where the model class M✓ is not rich enough. For example, if our dataset
was generated by a sinusoidal function, but ✓ only parametrizes straight
lines, the best optimization procedure will not get us close to the true
model. However, we still optimize the parameters and find the best straight
line that models the dataset. Figure 8.8(b) shows an example of a model
that underfits because it is insufficiently flexible. Models that underfit typ-
ically have few parameters.
The third case is when the parametrized model class is about right.
Then, our model fits well, i.e., it neither overfits nor underfits. This means
our model class is just rich enough to describe the dataset we are given.
Figure 8.8(c) shows a model that fits the given dataset fairly well. Ideally,

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


272 When Models Meet Data

this is the model class we would want to work with since it has good
generalization properties.
In practice, we often define very rich model classes M✓ with many pa-
rameters, such as deep neural networks. To mitigate the problem of over-
fitting, we can use regularization (Section 8.2.3) or priors (Section 8.3.2).
We will discuss how to choose the model class in Section 8.6.

8.3.4 Further Reading


When considering probabilistic models, the principle of maximum likeli-
hood estimation generalizes the idea of least-squares regression for linear
models, which we will discuss in detail in Chapter 9. When restricting
the predictor to have linear form with an additional nonlinear function '
applied to the output, i.e.,
p(yn |xn , ✓) = '(✓ > xn ) , (8.21)
we can consider other models for other prediction tasks, such as binary
classification or modeling count data (McCullagh and Nelder, 1989). An
alternative view of this is to consider likelihoods that are from the ex-
ponential family (Section 6.6). The class of models, which have linear
dependence between parameters and data, and have potentially nonlin-
link function ear transformation ' (called a link function), is referred to as generalized
generalized linear linear models (Agresti, 2002, chapter 4).
model
Maximum likelihood estimation has a rich history, and was originally
proposed by Sir Ronald Fisher in the 1930s. We will expand upon the idea
of a probabilistic model in Section 8.4. One debate among researchers
who use probabilistic models, is the discussion between Bayesian and fre-
quentist statistics. As mentioned in Section 6.1.1, it boils down to the
definition of probability. Recall from Section 6.1 that one can consider
probability to be a generalization (by allowing uncertainty) of logical rea-
soning (Cheeseman, 1985; Jaynes, 2003). The method of maximum like-
lihood estimation is frequentist in nature, and the interested reader is
pointed to Efron and Hastie (2016) for a balanced view of both Bayesian
and frequentist statistics.
There are some probabilistic models where maximum likelihood esti-
mation may not be possible. The reader is referred to more advanced sta-
tistical textbooks, e.g., Casella and Berger (2002), for approaches, such as
method of moments, M -estimation, and estimating equations.

8.4 Probabilistic Modeling and Inference


In machine learning, we are frequently concerned with the interpretation
and analysis of data, e.g., for prediction of future events and decision
making. To make this task more tractable, we often build models that
generative process describe the generative process that generates the observed data.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.4 Probabilistic Modeling and Inference 273

For example, we can describe the outcome of a coin-flip experiment


(“heads” or “tails”) in two steps. First, we define a parameter µ, which
describes the probability of “heads” as the parameter of a Bernoulli distri-
bution (Chapter 6); second, we can sample an outcome x 2 {head, tail}
from the Bernoulli distribution p(x | µ) = Ber(µ). The parameter µ gives
rise to a specific dataset X and depends on the coin used. Since µ is un-
known in advance and can never be observed directly, we need mecha-
nisms to learn something about µ given observed outcomes of coin-flip
experiments. In the following, we will discuss how probabilistic modeling
can be used for this purpose.

8.4.1 Probabilistic Models A probabilistic


Probabilistic models represent the uncertain aspects of an experiment as model is specified
by the joint
probability distributions. The benefit of using probabilistic models is that
distribution of all
they offer a unified and consistent set of tools from probability theory random variables.
(Chapter 6) for modeling, inference, prediction, and model selection.
In probabilistic modeling, the joint distribution p(x, ✓) of the observed
variables x and the hidden parameters ✓ is of central importance: It en-
capsulates information from the following:

The prior and the likelihood (product rule, Section 6.3).


The marginal likelihood p(x), which will play an important role in
model selection (Section 8.6), can be computed by taking the joint dis-
tribution and integrating out the parameters (sum rule, Section 6.3).
The posterior, which can be obtained by dividing the joint by the marginal
likelihood.
Only the joint distribution has this property. Therefore, a probabilistic
model is specified by the joint distribution of all its random variables.

8.4.2 Bayesian Inference


Parameter
A key task in machine learning is to take a model and the data to uncover estimation can be
the values of the model’s hidden variables ✓ given the observed variables phrased as an
optimization
x. In Section 8.3.1, we already discussed two ways for estimating model problem.
parameters ✓ using maximum likelihood or maximum a posteriori esti-
mation. In both cases, we obtain a single-best value for ✓ so that the key
algorithmic problem of parameter estimation is solving an optimization
problem. Once these point estimates ✓ ⇤ are known, we use them to make
predictions. More specifically, the predictive distribution will be p(x | ✓ ⇤ ),
where we use ✓ ⇤ in the likelihood function.
As discussed in Section 6.3, focusing solely on some statistic of the pos-
terior distribution (such as the parameter ✓ ⇤ that maximizes the poste-
rior) leads to loss of information, which can be critical in a system that

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


274 When Models Meet Data

uses the prediction p(x | ✓ ⇤ ) to make decisions. These decision-making


Bayesian inference systems typically have different objective functions than the likelihood, a
is about learning the squared-error loss or a mis-classification error. Therefore, having the full
distribution of
posterior distribution around can be extremely useful and leads to more
random variables.
Bayesian inference robust decisions. Bayesian inference is about finding this posterior distri-
bution (Gelman et al., 2004). For a dataset X , a parameter prior p(✓), and
a likelihood function, the posterior
Z
p(X | ✓)p(✓)
p(✓ | X ) = , p(X ) = p(X | ✓)p(✓)d✓ , (8.22)
p(X )
Bayesian inference is obtained by applying Bayes’ theorem. The key idea is to exploit Bayes’
inverts the theorem to invert the relationship between the parameters ✓ and the data
relationship
X (given by the likelihood) to obtain the posterior distribution p(✓ | X ).
between parameters
and the data. The implication of having a posterior distribution on the parameters is
that it can be used to propagate uncertainty from the parameters to the
data. More specifically, with a distribution p(✓) on the parameters our
predictions will be
Z
p(x) = p(x | ✓)p(✓)d✓ = E✓ [p(x | ✓)] , (8.23)

and they no longer depend on the model parameters ✓ , which have been
marginalized/integrated out. Equation (8.23) reveals that the prediction
is an average over all plausible parameter values ✓ , where the plausibility
is encapsulated by the parameter distribution p(✓).
Having discussed parameter estimation in Section 8.3 and Bayesian in-
ference here, let us compare these two approaches to learning. Parameter
estimation via maximum likelihood or MAP estimation yields a consistent
point estimate ✓ ⇤ of the parameters, and the key computational problem
to be solved is optimization. In contrast, Bayesian inference yields a (pos-
terior) distribution, and the key computational problem to be solved is
integration. Predictions with point estimates are straightforward, whereas
predictions in the Bayesian framework require solving another integration
problem; see (8.23). However, Bayesian inference gives us a principled
way to incorporate prior knowledge, account for side information, and
incorporate structural knowledge, all of which is not easily done in the
context of parameter estimation. Moreover, the propagation of parameter
uncertainty to the prediction can be valuable in decision-making systems
for risk assessment and exploration in the context of data-efficient learn-
ing (Deisenroth et al., 2015; Kamthe and Deisenroth, 2018).
While Bayesian inference is a mathematically principled framework for
learning about parameters and making predictions, there are some prac-
tical challenges that come with it because of the integration problems we
need to solve; see (8.22) and (8.23). More specifically, if we do not choose
a conjugate prior on the parameters (Section 6.6.1), the integrals in (8.22)
and (8.23) are not analytically tractable, and we cannot compute the pos-

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.4 Probabilistic Modeling and Inference 275

terior, the predictions, or the marginal likelihood in closed form. In these


cases, we need to resort to approximations. Here, we can use stochas-
tic approximations, such as Markov chain Monte Carlo (MCMC) (Gilks
et al., 1996), or deterministic approximations, such as the Laplace ap-
proximation (Bishop, 2006; Barber, 2012; Murphy, 2012), variational in-
ference (Jordan et al., 1999; Blei et al., 2017), or expectation propaga-
tion (Minka, 2001a).
Despite these challenges, Bayesian inference has been successfully ap-
plied to a variety of problems, including large-scale topic modeling (Hoff-
man et al., 2013), click-through-rate prediction (Graepel et al., 2010),
data-efficient reinforcement learning in control systems (Deisenroth et al.,
2015), online ranking systems (Herbrich et al., 2007), and large-scale rec-
ommender systems. There are generic tools, such as Bayesian optimiza-
tion (Brochu et al., 2009; Snoek et al., 2012; Shahriari et al., 2016), that
are very useful ingredients for an efficient search of meta parameters of
models or algorithms.
Remark. In the machine learning literature, there can be a somewhat ar-
bitrary separation between (random) “variables” and “parameters”. While
parameters are estimated (e.g., via maximum likelihood), variables are
usually marginalized out. In this book, we are not so strict with this sep-
aration because, in principle, we can place a prior on any parameter and
integrate it out, which would then turn the parameter into a random vari-
able according to the aforementioned separation. }

8.4.3 Latent-Variable Models


In practice, it is sometimes useful to have additional latent variables z latent variable
(besides the model parameters ✓ ) as part of the model (Moustaki et al.,
2015). These latent variables are different from the model parameters
✓ as they do not parametrize the model explicitly. Latent variables may
describe the data-generating process, thereby contributing to the inter-
pretability of the model. They also often simplify the structure of the
model and allow us to define simpler and richer model structures. Sim-
plification of the model structure often goes hand in hand with a smaller
number of model parameters (Paquet, 2008; Murphy, 2012). Learning in
latent-variable models (at least via maximum likelihood) can be done in a
principled way using the expectation maximization (EM) algorithm (Demp-
ster et al., 1977; Bishop, 2006). Examples, where such latent variables
are helpful, are principal component analysis for dimensionality reduc-
tion (Chapter 10), Gaussian mixture models for density estimation (Chap-
ter 11), hidden Markov models (Maybeck, 1979) or dynamical systems
(Ghahramani and Roweis, 1999; Ljung, 1999) for time-series modeling,
and meta learning and task generalization (Hausman et al., 2018; Sæ-
mundsson et al., 2018). Although the introduction of these latent variables

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


276 When Models Meet Data

may make the model structure and the generative process easier, learning
in latent-variable models is generally hard, as we will see in Chapter 11.
Since latent-variable models also allow us to define the process that
generates data from parameters, let us have a look at this generative pro-
cess. Denoting data by x, the model parameters by ✓ and the latent vari-
ables by z , we obtain the conditional distribution
p(x | ✓, z) (8.24)
that allows us to generate data for any model parameters and latent vari-
ables. Given that z are latent variables, we place a prior p(z) on them.
As the models we discussed previously, models with latent variables
can be used for parameter learning and inference within the frameworks
we discussed in Sections 8.3 and 8.4.2. To facilitate learning (e.g., by
means of maximum likelihood estimation or Bayesian inference), we fol-
low a two-step procedure. First, we compute the likelihood p(x | ✓) of the
model, which does not depend on the latent variables. Second, we use this
likelihood for parameter estimation or Bayesian inference, where we use
exactly the same expressions as in Sections 8.3 and 8.4.2, respectively.
Since the likelihood function p(x | ✓) is the predictive distribution of the
data given the model parameters, we need to marginalize out the latent
variables so that
Z
p(x | ✓) = p(x | ✓, z)p(z)dz , (8.25)

where p(x | z, ✓) is given in (8.24) and p(z) is the prior on the latent
The likelihood is a variables. Note that the likelihood must not depend on the latent variables
function of the data z , but it is only a function of the data x and the model parameters ✓ .
and the model
The likelihood in (8.25) directly allows for parameter estimation via
parameters, but is
independent of the maximum likelihood. MAP estimation is also straightforward with an ad-
latent variables. ditional prior on the model parameters ✓ as discussed in Section 8.3.2.
Moreover, with the likelihood (8.25) Bayesian inference (Section 8.4.2)
in a latent-variable model works in the usual way: We place a prior p(✓)
on the model parameters and use Bayes’ theorem to obtain a posterior
distribution
p(X | ✓)p(✓)
p(✓ | X ) = (8.26)
p(X )
over the model parameters given a dataset X . The posterior in (8.26) can
be used for predictions within a Bayesian inference framework; see (8.23).
One challenge we have in this latent-variable model is that the like-
lihood p(X | ✓) requires the marginalization of the latent variables ac-
cording to (8.25). Except when we choose a conjugate prior p(z) for
p(x | z, ✓), the marginalization in (8.25) is not analytically tractable, and
we need to resort to approximations (Bishop, 2006; Paquet, 2008; Mur-
phy, 2012; Moustaki et al., 2015).

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.4 Probabilistic Modeling and Inference 277

Similar to the parameter posterior (8.26) we can compute a posterior


on the latent variables according to
Z
p(X | z)p(z)
p(z | X ) = , p(X | z) = p(X | z, ✓)p(✓)d✓ , (8.27)
p(X )
where p(z) is the prior on the latent variables and p(X | z) requires us to
integrate out the model parameters ✓ .
Given the difficulty of solving integrals analytically, it is clear that mar-
ginalizing out both the latent variables and the model parameters at the
same time is not possible in general (Bishop, 2006; Murphy, 2012). A
quantity that is easier to compute is the posterior distribution on the latent
variables, but conditioned on the model parameters, i.e.,
p(X | z, ✓)p(z)
p(z | X , ✓) = , (8.28)
p(X | ✓)
where p(z) is the prior on the latent variables and p(X | z, ✓) is given
in (8.24).
In Chapters 10 and 11, we derive the likelihood functions for PCA and
Gaussian mixture models, respectively. Moreover, we compute the poste-
rior distributions (8.28) on the latent variables for both PCA and Gaussian
mixture models.
Remark. In the following chapters, we may not be drawing such a clear
distinction between latent variables z and uncertain model parameters ✓
and call the model parameters “latent” or “hidden” as well because they
are unobserved. In Chapters 10 and 11, where we use the latent variables
z , we will pay attention to the difference as we will have two different
types of hidden variables: model parameters ✓ and latent variables z . }
We can exploit the fact that all the elements of a probabilistic model are
random variables to define a unified language for representing them. In
Section 8.5, we will see a concise graphical language for representing the
structure of probabilistic models. We will use this graphical language to
describe the probabilistic models in the subsequent chapters.

8.4.4 Further Reading


Probabilistic models in machine learning (Bishop, 2006; Barber, 2012;
Murphy, 2012) provide a way for users to capture uncertainty about data
and predictive models in a principled fashion. Ghahramani (2015) presents
a short review of probabilistic models in machine learning. Given a proba-
bilistic model, we may be lucky enough to be able to compute parameters
of interest analytically. However, in general, analytic solutions are rare,
and computational methods such as sampling (Gilks et al., 1996; Brooks
et al., 2011) and variational inference (Jordan et al., 1999; Blei et al.,

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


278 When Models Meet Data

2017) are used. Moustaki et al. (2015) and Paquet (2008) provide a good
overview of Bayesian inference in latent-variable models.
In recent years, several programming languages have been proposed
that aim to treat the variables defined in software as random variables
corresponding to probability distributions. The objective is to be able to
write complex functions of probability distributions, while under the hood
the compiler automatically takes care of the rules of Bayesian inference.
probabilistic This rapidly changing field is called probabilistic programming.
programming

8.5 Directed Graphical Models


In this section, we introduce a graphical language for specifying a prob-
directed graphical abilistic model, called the directed graphical model. It provides a compact
model and succinct way to specify probabilistic models, and allows the reader to
visually parse dependencies between random variables. A graphical model
visually captures the way in which the joint distribution over all random
variables can be decomposed into a product of factors depending only on
a subset of these variables. In Section 8.4, we identified the joint distri-
bution of a probabilistic model as the key quantity of interest because it
comprises information about the prior, the likelihood, and the posterior.
Directed graphical However, the joint distribution by itself can be quite complicated, and
models are also it does not tell us anything about structural properties of the probabilis-
known as Bayesian
tic model. For example, the joint distribution p(a, b, c) does not tell us
networks.
anything about independence relations. This is the point where graphical
models come into play. This section relies on the concepts of independence
and conditional independence, as described in Section 6.4.5.
graphical model In a graphical model, nodes are random variables. In Figure 8.9(a), the
nodes represent the random variables a, b, c. Edges represent probabilistic
relations between variables, e.g., conditional probabilities.
Remark. Not every distribution can be represented in a particular choice of
graphical model. A discussion of this can be found in Bishop (2006). }
Probabilistic graphical models have some convenient properties:

They are a simple way to visualize the structure of a probabilistic model.


They can be used to design or motivate new kinds of statistical models.
Inspection of the graph alone gives us insight into properties, e.g., con-
ditional independence.
Complex computations for inference and learning in statistical models
can be expressed in terms of graphical manipulations.

8.5.1 Graph Semantics


directed graphical Directed graphical models/Bayesian networks are a method for representing
model/Bayesian conditional dependencies in a probabilistic model. They provide a visual
network

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.5 Directed Graphical Models 279

description of the conditional probabilities, hence, providing a simple lan-


guage for describing complex interdependence. The modular description With additional
also entails computational simplification. Directed links (arrows) between assumptions, the
arrows can be used
two nodes (random variables) indicate conditional probabilities. For ex-
to indicate causal
ample, the arrow between a and b in Figure 8.9(a) gives the conditional relationships (Pearl,
probability p(b | a) of b given a. 2009).
Figure 8.9
a b x1 x2 x5 Examples of
directed graphical
models.

c x3 x4

(a) Fully connected. (b) Not fully connected.

Directed graphical models can be derived from joint distributions if we


know something about their factorization.

Example 8.7
Consider the joint distribution
p(a, b, c) = p(c | a, b)p(b | a)p(a) (8.29)
of three random variables a, b, c. The factorization of the joint distribution
in (8.29) tells us something about the relationship between the random
variables:
c depends directly on a and b.
b depends directly on a.
a depends neither on b nor on c.
For the factorization in (8.29), we obtain the directed graphical model in
Figure 8.9(a).

In general, we can construct the corresponding directed graphical model


from a factorized joint distribution as follows:

1. Create a node for all random variables.


2. For each conditional distribution, we add a directed link (arrow) to
the graph from the nodes corresponding to the variables on which the
distribution is conditioned.
The graph layout
The graph layout depends on the choice of factorization of the joint dis- depends on the
factorization of the
tribution.
joint distribution.
We discussed how to get from a known factorization of the joint dis-
tribution to the corresponding directed graphical model. Now, we will do

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


280 When Models Meet Data

exactly the opposite and describe how to extract the joint distribution of
a set of random variables from a given graphical model.

Example 8.8
Looking at the graphical model in Figure 8.9(b), we exploit two proper-
ties:
The joint distribution p(x1 , . . . , x5 ) we seek is the product of a set of
conditionals, one for each node in the graph. In this particular example,
we will need five conditionals.
Each conditional depends only on the parents of the corresponding
node in the graph. For example, x4 will be conditioned on x2 .
These two properties yield the desired factorization of the joint distribu-
tion
p(x1 , x2 , x3 , x4 , x5 ) = p(x1 )p(x5 )p(x2 | x5 )p(x3 | x1 , x2 )p(x4 | x2 ) . (8.30)

In general, the joint distribution p(x) = p(x1 , . . . , xK ) is given as


K
Y
p(x) = p(xk | Pak ) , (8.31)
k=1

where Pak means “the parent nodes of xk ”. Parent nodes of xk are nodes
that have arrows pointing to xk .
We conclude this subsection with a concrete example of the coin-flip
experiment. Consider a Bernoulli experiment (Example 6.8) where the
probability that the outcome x of this experiment is “heads” is
p(x | µ) = Ber(µ) . (8.32)
We now repeat this experiment N times and observe outcomes x1 , . . . , xN
so that we obtain the joint distribution
N
Y
p(x1 , . . . , xN | µ) = p(xn | µ) . (8.33)
n=1

The expression on the right-hand side is a product of Bernoulli distribu-


tions on each individual outcome because the experiments are indepen-
dent. Recall from Section 6.4.5 that statistical independence means that
the distribution factorizes. To write the graphical model down for this set-
ting, we make the distinction between unobserved/latent variables and
observed variables. Graphically, observed variables are denoted by shaded
nodes so that we obtain the graphical model in Figure 8.10(a). We see
that the single parameter µ is the same for all xn , n = 1, . . . , N as the
outcomes xn are identically distributed. A more compact, but equivalent,
graphical model for this setting is given in Figure 8.10(b), where we use

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.5 Directed Graphical Models 281
Figure 8.10
µ ↵ µ Graphical models
µ for a repeated
Bernoulli
xn xn experiment.

x1 xN n = 1, . . . , N n = 1, . . . , N

(a) Version with xn explicit. (b) Version with (c) Hyperparameters ↵


plate notation. and on the latent µ.

the plate notation. The plate (box) repeats everything inside (in this case, plate
the observations xn ) N times. Therefore, both graphical models are equiv-
alent, but the plate notation is more compact. Graphical models immedi-
ately allow us to place a hyperprior on µ. A hyperprior is a second layer hyperprior
of prior distributions on the parameters of the first layer of priors. Fig-
ure 8.10(c) places a Beta(↵, ) prior on the latent variable µ. If we treat
↵ and as deterministic parameters, i.e., not random variables, we omit
the circle around it.

8.5.2 Conditional Independence and d-Separation


Directed graphical models allow us to find conditional independence (Sec-
tion 6.4.5) relationship properties of the joint distribution only by looking
at the graph. A concept called d-separation (Pearl, 1988) is key to this. d-separation
Consider a general directed graph in which A, B, C are arbitrary nonin-
tersecting sets of nodes (whose union may be smaller than the complete
set of nodes in the graph). We wish to ascertain whether a particular con-
ditional independence statement, “A is conditionally independent of B
given C ”, denoted by

A?
? B|C , (8.34)

is implied by a given directed acyclic graph. To do so, we consider all


possible trails (paths that ignore the direction of the arrows) from any
node in A to any nodes in B . Any such path is said to be blocked if it
includes any node such that either of the following are true:

The arrows on the path meet either head to tail or tail to tail at the
node, and the node is in the set C .
The arrows meet head to head at the node, and neither the node nor
any of its descendants is in the set C .

If all paths are blocked, then A is said to be d-separated from B by C ,


and the joint distribution over all of the variables in the graph will satisfy
A? ? B | C.

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


282 When Models Meet Data

Figure 8.12 Three


types of graphical
models: (a) Directed
a b a b a b
graphical models
(Bayesian
networks); c c c
(b) Undirected
graphical models (a) Directed graphical model (b) Undirected graphical (c) Factor graph
(Markov random model
fields); (c) Factor
graphs.

Example 8.9 (Conditional Independence)

Figure 8.11 a b c
D-separation
example.

Consider the graphical model in Figure 8.11. Visual inspection gives us


b? ? d | a, c (8.35)
a? ? c|b (8.36)
b 6?
? d|c (8.37)
a 6?
? c | b, e (8.38)

Directed graphical models allow a compact representation of proba-


bilistic models, and we will see examples of directed graphical models in
Chapters 9, 10, and 11. The representation, along with the concept of con-
ditional independence, allows us to factorize the respective probabilistic
models into expressions that are easier to optimize.
The graphical representation of the probabilistic model allows us to
visually see the impact of design choices we have made on the structure
of the model. We often need to make high-level assumptions about the
structure of the model. These modeling assumptions (hyperparameters)
affect the prediction performance, but cannot be selected directly using
the approaches we have seen so far. We will discuss different ways to
choose the structure in Section 8.6.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.6 Model Selection 283

8.5.3 Further Reading


An introduction to probabilistic graphical models can be found in Bishop
(2006, chapter 8), and an extensive description of the different applica-
tions and corresponding algorithmic implications can be found in the book
by Koller and Friedman (2009). There are three main types of probabilistic
graphical models:
directed graphical
Directed graphical models (Bayesian networks); see Figure 8.12(a) model
Undirected graphical models (Markov random fields); see Figure 8.12(b) Bayesian network

Factor graphs; see Figure 8.12(c) undirected graphical


model
Graphical models allow for graph-based algorithms for inference and Markov random
field
learning, e.g., via local message passing. Applications range from rank-
factor graph
ing in online games (Herbrich et al., 2007) and computer vision (e.g.,
image segmentation, semantic labeling, image denoising, image restora-
tion (Kittler and Föglein, 1984; Sucar and Gillies, 1994; Shotton et al.,
2006; Szeliski et al., 2008)) to coding theory (McEliece et al., 1998), solv-
ing linear equation systems (Shental et al., 2008), and iterative Bayesian
state estimation in signal processing (Bickson et al., 2007; Deisenroth and
Mohamed, 2012).
One topic that is particularly important in real applications that we do
not discuss in this book is the idea of structured prediction (Bakir et al.,
2007; Nowozin et al., 2014), which allows machine learning models to
tackle predictions that are structured, for example sequences, trees, and
graphs. The popularity of neural network models has allowed more flex-
ible probabilistic models to be used, resulting in many useful applica-
tions of structured models (Goodfellow et al., 2016, chapter 16). In recent
years, there has been a renewed interest in graphical models due to their
applications to causal inference (Pearl, 2009; Imbens and Rubin, 2015;
Peters et al., 2017; Rosenbaum, 2017).

8.6 Model Selection


In machine learning, we often need to make high-level modeling decisions
that critically influence the performance of the model. The choices we
make (e.g., the functional form of the likelihood) influence the number
and type of free parameters in the model and thereby also the flexibility
and expressivity of the model. More complex models are more flexible in A polynomial
the sense that they can be used to describe more datasets. For instance, a y = a0 +a1 x+a2 x2
can also describe
polynomial of degree 1 (a line y = a0 + a1 x) can only be used to describe
linear functions by
linear relations between inputs x and observations y . A polynomial of setting a2 = 0, i.e.,
degree 2 can additionally describe quadratic relationships between inputs it is strictly more
and observations. expressive than a
first-order
One would now think that very flexible models are generally preferable
polynomial.
to simple models because they are more expressive. A general problem

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


284 When Models Meet Data

Figure 8.13 Nested


cross-validation. We All labeled data
perform two levels
of K-fold
All training data Test data
cross-validation.

To train model Validation

is that at training time we can only use the training set to evaluate the
performance of the model and learn its parameters. However, the per-
formance on the training set is not really what we are interested in. In
Section 8.3, we have seen that maximum likelihood estimation can lead
to overfitting, especially when the training dataset is small. Ideally, our
model (also) works well on the test set (which is not available at training
time). Therefore, we need some mechanisms for assessing how a model
generalizes to unseen test data. Model selection is concerned with exactly
this problem.

8.6.1 Nested Cross-Validation


We have already seen an approach (cross-validation in Section 8.2.4) that
can be used for model selection. Recall that cross-validation provides an
estimate of the generalization error by repeatedly splitting the dataset into
training and validation sets. We can apply this idea one more time, i.e.,
for each split, we can perform another round of cross-validation. This is
nested sometimes referred to as nested cross-validation; see Figure 8.13. The inner
cross-validation level is used to estimate the performance of a particular choice of model
or hyperparameter on a internal validation set. The outer level is used to
estimate generalization performance for the best choice of model chosen
by the inner loop. We can test different model and hyperparameter choices
in the inner loop. To distinguish the two levels, the set used to estimate
test set the generalization performance is often called the test set and the set used
validation set for choosing the best model is called the validation set. The inner loop
estimates the expected value of the generalization error for a given model
(8.39), by approximating it using the empirical error on the validation set,
The standard error i.e.,
is defined as p , K
where K is the
K
1 X
EV [R(V | M )] ⇡ R(V (k) | M ) , (8.39)
number of K k=1
experiments and
is the standard
where R(V | M ) is the empirical risk (e.g., root mean square error) on the
deviation of the risk
of each experiment. validation set V for model M . We repeat this procedure for all models and
choose the model that performs best. Note that cross-validation not only
gives us the expected generalization error, but we can also obtain high-
order statistics, e.g., the standard error, an estimate of how uncertain the

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.6 Model Selection 285

Figure 8.14
Evidence Bayesian inference
embodies Occam’s
razor. The
horizontal axis
describes the space
p(D | M1 )
of all possible
datasets D. The
evidence (vertical
axis) evaluates how
well a model
p(D | M2 ) predicts available
data. Since
p(D | Mi ) needs to
integrate to 1, we
should choose the
D
C model with the
greatest evidence.
Adapted
mean estimate is. Once the model is chosen, we can evaluate the final from MacKay
performance on the test set. (2003).

8.6.2 Bayesian Model Selection


There are many approaches to model selection, some of which are covered
in this section. Generally, they all attempt to trade off model complexity
and data fit. We assume that simpler models are less prone to overfitting
than complex models, and hence the objective of model selection is to find
the simplest model that explains the data reasonably well. This concept is
also known as Occam’s razor. Occam’s razor

Remark. If we treat model selection as a hypothesis testing problem, we


are looking for the simplest hypothesis that is consistent with the data (Mur-
phy, 2012). }
One may consider placing a prior on models that favors simpler models.
However, it is not necessary to do this: An “automatic Occam’s Razor” is
quantitatively embodied in the application of Bayesian probability (Smith
and Spiegelhalter, 1980; Jefferys and Berger, 1992; MacKay, 1992). Fig-
ure 8.14, adapted from MacKay (2003), gives us the basic intuition why
complex and very expressive models may turn out to be a less probable
choice for modeling a given dataset D. Let us think of the horizontal axis These predictions
representing the space of all possible datasets D. If we are interested in are quantified by a
normalized
the posterior probability p(Mi | D) of model Mi given the data D, we can
probability
employ Bayes’ theorem. Assuming a uniform prior p(M ) over all mod- distribution on D,
els, Bayes’ theorem rewards models in proportion to how much they pre- i.e., it needs to
dicted the data that occurred. This prediction of the data given model integrate/sum to 1.
Mi , p(D | Mi ), is called the evidence for Mi . A simple model M1 can only evidence
predict a small number of datasets, which is shown by p(D | M1 ); a more
powerful model M2 that has, e.g., more free parameters than M1 , is able

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


286 When Models Meet Data

to predict a greater variety of datasets. This means, however, that M2


does not predict the datasets in region C as well as M1 . Suppose that
equal prior probabilities have been assigned to the two models. Then, if
the dataset falls into region C , the less powerful model M1 is the more
probable model.
Earlier in this chapter, we argued that models need to be able to explain
the data, i.e., there should be a way to generate data from a given model.
Furthermore, if the model has been appropriately learned from the data,
then we expect that the generated data should be similar to the empirical
data. For this, it is helpful to phrase model selection as a hierarchical
inference problem, which allows us to compute the posterior distribution
over models.
Let us consider a finite number of models M = {M1 , . . . , MK }, where
Bayesian model each model Mk possesses parameters ✓ k . In Bayesian model selection, we
selection place a prior p(M ) on the set of models. The corresponding generative
generative process
process that allows us to generate data from this model is
Figure 8.15
Illustration of the Mk ⇠ p(M ) (8.40)
hierarchical
generative process ✓ k ⇠ p(✓ | Mk ) (8.41)
in Bayesian model D ⇠ p(D | ✓ k ) (8.42)
selection. We place
a prior p(M ) on the and illustrated in Figure 8.15. Given a training set D, we apply Bayes’
set of models. For theorem and compute the posterior distribution over models as
each model, there is
a distribution p(Mk | D) / p(Mk )p(D | Mk ) . (8.43)
p(✓ | M ) on the
corresponding Note that this posterior no longer depends on the model parameters ✓ k
model parameters, because they have been integrated out in the Bayesian setting since
which is used to Z
generate the data D.
p(D | Mk ) = p(D | ✓ k )p(✓ k | Mk )d✓ k , (8.44)
M
where p(✓ k | Mk ) is the prior distribution of the model parameters ✓ k of
model Mk . The term (8.44) is referred to as the model evidence or marginal
likelihood. From the posterior in (8.43), we determine the MAP estimate

M ⇤ = arg max p(Mk | D) . (8.45)
Mk

D With a uniform prior p(Mk ) = K1 , which gives every model equal (prior)
probability, determining the MAP estimate over models amounts to pick-
model evidence
ing the model that maximizes the model evidence (8.44).
marginal likelihood
Remark (Likelihood and Marginal Likelihood). There are some important
differences between a likelihood and a marginal likelihood (evidence):
While the likelihood is prone to overfitting, the marginal likelihood is typ-
ically not as the model parameters have been marginalized out (i.e., we
no longer have to fit the parameters). Furthermore, the marginal likeli-
hood automatically embodies a trade-off between model complexity and
data fit (Occam’s razor). }

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


8.6 Model Selection 287

8.6.3 Bayes Factors for Model Comparison


Consider the problem of comparing two probabilistic models M1 , M2 ,
given a dataset D. If we compute the posteriors p(M1 | D) and p(M2 | D),
we can compute the ratio of the posteriors
p(D | M1 )p(M1 )
p(M1 | D) p(D) p(M1 ) p(D | M1 )
= p(D | M2 )p(M2 )
= . (8.46)
p(M2 | D) p(M2 ) p(D | M2 )
| {z } p(D) | {z } | {z }
posterior odds prior odds Bayes factor

The ratio of the posteriors is also called the posterior odds. The first frac- posterior odds
tion on the right-hand side of (8.46), the prior odds, measures how much prior odds
our prior (initial) beliefs favor M1 over M2 . The ratio of the marginal like-
lihoods (second fraction on the right-hand-side) is called the Bayes factor Bayes factor
and measures how well the data D is predicted by M1 compared to M2 .
Remark. The Jeffreys-Lindley paradox states that the “Bayes factor always Jeffreys-Lindley
favors the simpler model since the probability of the data under a complex paradox
model with a diffuse prior will be very small” (Murphy, 2012). Here, a
diffuse prior refers to a prior that does not favor specific models, i.e.,
many models are a priori plausible under this prior. }
If we choose a uniform prior over models, the prior odds term in (8.46)
is 1, i.e., the posterior odds is the ratio of the marginal likelihoods (Bayes
factor)
p(D | M1 )
. (8.47)
p(D | M2 )
If the Bayes factor is greater than 1, we choose model M1 , otherwise
model M2 . In a similar way to frequentist statistics, there are guidelines
on the size of the ratio that one should consider before ”significance” of
the result (Jeffreys, 1961).
Remark (Computing the Marginal Likelihood). The marginal likelihood
plays an important role in model selection: We need to compute Bayes
factors (8.46) and posterior distributions over models (8.43).
Unfortunately, computing the marginal likelihood requires us to solve
an integral (8.44). This integration is generally analytically intractable,
and we will have to resort to approximation techniques, e.g., numerical
integration (Stoer and Burlirsch, 2002), stochastic approximations using
Monte Carlo (Murphy, 2012), or Bayesian Monte Carlo techniques (O’Hagan,
1991; Rasmussen and Ghahramani, 2003).
However, there are special cases in which we can solve it. In Section 6.6.1,
we discussed conjugate models. If we choose a conjugate parameter prior
p(✓), we can compute the marginal likelihood in closed form. In Chap-
ter 9, we will do exactly this in the context of linear regression. }
We have seen a brief introduction to the basic concepts of machine
learning in this chapter. For the rest of this part of the book we will see

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


288 When Models Meet Data

how the three different flavors of learning in Sections 8.2, 8.3, and 8.4 are
applied to the four pillars of machine learning (regression, dimensionality
reduction, density estimation, and classification).

8.6.4 Further Reading


We mentioned at the start of the section that there are high-level modeling
choices that influence the performance of the model. Examples include the
following:
The degree of a polynomial in a regression setting
The number of components in a mixture model
The network architecture of a (deep) neural network
The type of kernel in a support vector machine
The dimensionality of the latent space in PCA
The learning rate (schedule) in an optimization algorithm
In parametric
models, the number Rasmussen and Ghahramani (2001) showed that the automatic Occam’s
of parameters is razor does not necessarily penalize the number of parameters in a model,
often related to the
but it is active in terms of the complexity of functions. They also showed
complexity of the
model class. that the automatic Occam’s razor also holds for Bayesian nonparametric
models with many parameters, e.g., Gaussian processes.
If we focus on the maximum likelihood estimate, there exist a number of
heuristics for model selection that discourage overfitting. They are called
information criteria, and we choose the model with the largest value. The
Akaike information Akaike information criterion (AIC) (Akaike, 1974)
criterion
log p(x | ✓) M (8.48)
corrects for the bias of the maximum likelihood estimator by addition of
a penalty term to compensate for the overfitting of more complex models
with lots of parameters. Here, M is the number of model parameters. The
AIC estimates the relative information lost by a given model.
Bayesian The Bayesian information criterion (BIC) (Schwarz, 1978)
information Z
criterion
1
log p(x) = log p(x | ✓)p(✓)d✓ ⇡ log p(x | ✓) M log N (8.49)
2
can be used for exponential family distributions. Here, N is the number
of data points and M is the number of parameters. BIC penalizes model
complexity more heavily than AIC.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


9

Linear Regression

In the following, we will apply the mathematical concepts from Chap-


ters 2, 5, 6, and 7 to solve linear regression (curve fitting) problems. In
regression, we aim to find a function f that maps inputs x 2 RD to corre- regression
sponding function values f (x) 2 R. We assume we are given a set of train-
ing inputs xn and corresponding noisy observations yn = f (xn )+✏, where
✏ is an i.i.d. random variable that describes measurement/observation
noise and potentially unmodeled processes (which we will not consider
further in this chapter). Throughout this chapter, we assume zero-mean
Gaussian noise. Our task is to find a function that not only models the
training data, but generalizes well to predicting function values at input
locations that are not part of the training data (see Chapter 8). An il-
lustration of such a regression problem is given in Figure 9.1. A typical
regression setting is given in Figure 9.1(a): For some input values xn , we
observe (noisy) function values yn = f (xn ) + ✏. The task is to infer the
function f that generated the data and generalizes well to function values
at new input locations. A possible solution is given in Figure 9.1(b), where
we also show three distributions centered at the function values f (x) that
represent the noise in the data.
Regression is a fundamental problem in machine learning, and regres-
sion problems appear in a diverse range of research areas and applica-

Figure 9.1
0.4 0.4 (a) Dataset;
(b) possible solution
0.2 0.2
to the regression
0.0 0.0 problem.
y

0.2 0.2

0.4 0.4

4 2 0 2 4 4 2 0 2 4
x x

(a) Regression problem: observed noisy func- (b) Regression solution: possible function
tion values from which we wish to infer the that could have generated the data (blue)
underlying function that generated the data. with indication of the measurement noise of
the function value at the corresponding in-
puts (orange distributions).

289
This material will be published by Cambridge University Press as Mathematics for Machine Learn-
ing by Marc Peter Deisenroth, A. Aldo Faisal, and Cheng Soon Ong. This pre-publication version is
free to view and download for personal use only. Not for re-distribution, re-sale or use in deriva-
tive works. c by M. P. Deisenroth, A. A. Faisal, and C. S. Ong, 2019. https://mml-book.com.
290 Linear Regression

tions, including time-series analysis (e.g., system identification), control


and robotics (e.g., reinforcement learning, forward/inverse model learn-
ing), optimization (e.g., line searches, global optimization), and deep-
learning applications (e.g., computer games, speech-to-text translation,
image recognition, automatic video annotation). Regression is also a key
ingredient of classification algorithms. Finding a regression function re-
quires solving a variety of problems, including the following:
Choice of the model (type) and the parametrization of the regres-
Normally, the type sion function. Given a dataset, what function classes (e.g., polynomi-
of noise could also als) are good candidates for modeling the data, and what particular
be a “model choice”,
parametrization (e.g., degree of the polynomial) should we choose?
but we fix the noise
to be Gaussian in Model selection, as discussed in Section 8.6, allows us to compare var-
this chapter. ious models to find the simplest model that explains the training data
reasonably well.
Finding good parameters. Having chosen a model of the regression
function, how do we find good model parameters? Here, we will need to
look at different loss/objective functions (they determine what a “good”
fit is) and optimization algorithms that allow us to minimize this loss.
Overfitting and model selection. Overfitting is a problem when the
regression function fits the training data “too well” but does not gen-
eralize to unseen test data. Overfitting typically occurs if the underly-
ing model (or its parametrization) is overly flexible and expressive; see
Section 8.6. We will look at the underlying reasons and discuss ways to
mitigate the effect of overfitting in the context of linear regression.
Relationship between loss functions and parameter priors. Loss func-
tions (optimization objectives) are often motivated and induced by prob-
abilistic models. We will look at the connection between loss functions
and the underlying prior assumptions that induce these losses.
Uncertainty modeling. In any practical setting, we have access to only
a finite, potentially large, amount of (training) data for selecting the
model class and the corresponding parameters. Given that this finite
amount of training data does not cover all possible scenarios, we may
want to describe the remaining parameter uncertainty to obtain a mea-
sure of confidence of the model’s prediction at test time; the smaller the
training set, the more important uncertainty modeling. Consistent mod-
eling of uncertainty equips model predictions with confidence bounds.
In the following, we will be using the mathematical tools from Chap-
ters 3, 5, 6 and 7 to solve linear regression problems. We will discuss
maximum likelihood and maximum a posteriori (MAP) estimation to find
optimal model parameters. Using these parameter estimates, we will have
a brief look at generalization errors and overfitting. Toward the end of
this chapter, we will discuss Bayesian linear regression, which allows us to
reason about model parameters at a higher level, thereby removing some
of the problems encountered in maximum likelihood and MAP estimation.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.


9.1 Problem Formulation 291

9.1 Problem Formulation


Because of the presence of observation noise, we will adopt a probabilis-
tic approach and explicitly model the noise using a likelihood function.
More specifically, throughout this chapter, we consider a regression prob-
lem with the likelihood function

p(y | x) = N y | f (x), 2
. (9.1)

Here, x 2 RD are inputs and y 2 R are noisy function values (targets).


With (9.1), the functional relationship between x and y is given as

y = f (x) + ✏ , (9.2)

where ✏ ⇠ N 0, 2 is independent, identically distributed (i.i.d.) Gaus-


sian measurement noise with mean 0 and variance 2 . Our objective is
to find a function that is close (similar) to the unknown function f that
generated the data and that generalizes well.
In this chapter, we focus on parametric models, i.e., we choose a para-
metrized function and find parameters ✓ that “work well” for modeling the
data. For the time being, we assume that the noise variance 2 is known
and focus on learning the model parameters ✓ . In linear regression, we
consider the special case that the parameters ✓ appear linearly in our
model. An example of linear regression is given by

p(y | x, ✓) = N y | x> ✓, 2
(9.3)
>
() y = x ✓ + ✏ , ✏ ⇠ N 0, 2
, (9.4)

where ✓ 2 RD are the parameters we seek. The class of functions de-


scribed by (9.4) are straight lines that pass through the origin. In (9.4),
we chose a parametrization f (x) = x> ✓ . A Dirac delta (delta
The likelihood in (9.3) is the probability density function of y evalu- function) is zero
everywhere except
ated at x> ✓ . Note that the only source of uncertainty originates from the
at a single point,
observation noise (as x and ✓ are assumed known in (9.3)). Without ob- and its integral is 1.
servation noise, the relationship between x and y would be deterministic It can be considered
and (9.3) would be a Dirac delta. a Gaussian in the
limit of 2 ! 0.
likelihood
Example 9.1
For x, ✓ 2 R the linear regression model in (9.4) describes straight lines
(linear functions), and the parameter ✓ is the slope of the line. Fig-
ure 9.2(a) shows some example functions for different values of ✓.
Linear regression
The linear regression model in (9.3)–(9.4) is not only linear in the pa- refers to models that
are linear in the
rameters, but also linear in the inputs x. Figure 9.2(a) shows examples
parameters.
of such functions. We will see later that y = > (x)✓ for nonlinear trans-
formations is also a linear regression model because “linear regression”

c 2019 M. P. Deisenroth, A. A. Faisal, C. S. Ong. To be published by Cambridge University Press.


292 Linear Regression
Figure 9.2 Linear 20
regression example. 10 10

(a) Example 0 0 0
y

y
functions that fall
into this category; 20
10 10

(b) training set; 10 0 10 10 5 0 5 10 10 5 0 5 10


x x x
(c) maximum
likelihood estimate. (a) Example functions (straight (b) Training set. (c) Maximum likelihood esti-
lines) that can be described us- mate.
ing the linear model in (9.4).

refers to models that are “linear in the parameters”, i.e., models that de-
scribe a function by a linear combination of input features. Here, a “fea-
ture” is a representation (x) of the inputs x.
In the following, we will discuss in more detail how to find good pa-
rameters ✓ and how to evaluate whether a parameter set “works well”.
For the time being, we assume that the noise variance 2 is known.

9.2 Parameter Estimation


Consider the linear regression setting (9.4) and assume we are given a
training set training set D := {(x1 , y1 ), . . . , (xN , yN )} consisting of N inputs xn 2
Figure 9.3 RD and corresponding observations/targets yn 2 R, n = 1, . . . , N . The
Probabilistic corresponding graphical model is given in Figure 9.3. Note that yi and yj
graphical model for
are conditionally independent given their respective inputs xi , xj so that
linear regression.
Observed random the likelihood factorizes according to
variables are
shaded, p(Y | X , ✓) = p(y1 , . . . , yN | x1 , . . . , xN , ✓) (9.5a)
deterministic/ N N
Y Y
known values are
= p(yn | xn , ✓) = N y n | x>
n ✓,
2
, (9.5b)
without circles.
n=1 n=1

where we defined X := {x1 , . . . , xN } and Y := {y1 , . . . , yN } as the sets
of training inputs and corresponding targets, respectively. The likelihood
xn yn and the factors p(yn | xn , ✓) are Gaussian due to the noise distribution;
see (9.3).
n = 1, . . . , N In the following, we will discuss how to find optimal parameters ✓ ⇤ 2
RD for the linear regression model (9.4). Once the parameters ✓ ⇤ are
found, we can predict function values by using this parameter estimate
in (9.4) so that at an arbitrary test input x⇤ the distribution of the corre-
sponding target y⇤ is

p(y⇤ | x⇤ , ✓ ⇤ ) = N y⇤ | x> ⇤
⇤✓ ,
2
. (9.6)

In the following, we will have a look at parameter estimation by maxi-


mizing the likelihood, a topic that we already covered to some degree in
Section 8.3.

Draft (2019-12-11) of “Mathematics for Machine Learning”. Feedback: https://mml-book.com.

You might also like