Bayesian Reasoning and Machine Learning

David Barber c
(cid:13)

2007,2008,2009,2010,2011,2012,2013,2014

Notation List

V
dom(x)

x = x

p(x = tr)

p(x = fa)

p(x, y)

p(x

y)

∩

p(x

p(x

y)

∪
y)
|

X ⊥⊥ Y| Z

X (cid:62)(cid:62)Y| Z
(cid:82)
x f (x)

I [S]

pa (x)

ch (x)

ne (x)

dim (x)

f (x)
(cid:104)

(cid:105)p(x)

δ(a, b)

dim x

a calligraphic symbol typically denotes a set of random variables . . . . . . . . 7

Domain of a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

The variable x is in the state x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

probability of event/variable x being in the state true . . . . . . . . . . . . . . . . . . . 7

probability of event/variable x being in the state false . . . . . . . . . . . . . . . . . . . 7

probability of x and y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

probability of x and y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

probability of x or y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

The probability of x conditioned on y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

Variables

X

are independent of variables

conditioned on variables

. 11

Z

Y

X

are dependent on variables

Variables
. . 11
For continuous variables this is shorthand for (cid:82) f (x)dx and for discrete vari-
ables means summation over the states of x, (cid:80)
x f (x) . . . . . . . . . . . . . . . . . . 18
Indicator : has value 1 if the statement S is true, 0 otherwise . . . . . . . . . . 19

conditioned on variables

Z

Y

The parents of node x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

The children of node x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Neighbours of node x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

For a discrete variable x, this denotes the number of states x can take . . 36

The average of the function f (x) with respect to the distribution p(x) . 162

Delta function. For discrete a, b, this is the Kronecker delta, δa,b and for
b) . . . . . . . . . . . . . . . . . . . . . . 164
continuous a, b the Dirac delta function δ(a

The dimension of the vector/matrix x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

−

(cid:93) (x = s, y = t)

The number of times x is in state s and y in state t simultaneously . . . 201

(cid:93)x
y

D
n

N

S

σ(x)

erf(x)

xa:b

j

i

∼
Im

II

The number of times variable x is in state y . . . . . . . . . . . . . . . . . . . . . . . . . . 282

Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

Data index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

Number of dataset training points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

Sample Covariance matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

The logistic sigmoid 1/(1 + exp(

x)) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

−

The (Gaussian) error function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

xa, xa+1, . . . , xb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461

The set of unique neighbouring edges on a graph . . . . . . . . . . . . . . . . . . . . . . 591

The m

×

m identity matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613

DRAFT April 24, 2015

Preface

The data explosion

We live in a world that is rich in data, ever increasing in scale. This data comes from many diﬀerent
sources in science (bioinformatics, astronomy, physics, environmental monitoring) and commerce (customer
databases, ﬁnancial transactions, engine monitoring, speech recognition, surveillance, search). Possessing
the knowledge as to how to process and extract value from such data is therefore a key and increasingly
important skill. Our society also expects ultimately to be able to engage with computers in a natural manner
so that computers can ‘talk’ to humans, ‘understand’ what they say and ‘comprehend’ the visual world
around them. These are diﬃcult large-scale information processing tasks and represent grand challenges
for computer science and related ﬁelds. Similarly, there is a desire to control increasingly complex systems,
possibly containing many interacting parts, such as in robotics and autonomous navigation. Successfully
mastering such systems requires an understanding of the processes underlying their behaviour. Processing
and making sense of such large amounts of data from complex systems is therefore a pressing modern day
concern and will likely remain so for the foreseeable future.

Machine Learning

Machine Learning is the study of data-driven methods capable of mimicking, understanding and aiding
human and biological information processing tasks. In this pursuit, many related issues arise such as how
to compress data, interpret and process it. Often these methods are not necessarily directed to mimicking
directly human processing but rather to enhance it, such as in predicting the stock market or retrieving
information rapidly. In this probability theory is key since inevitably our limited data and understanding
of the problem forces us to address uncertainty. In the broadest sense, Machine Learning and related ﬁelds
aim to ‘learn something useful’ about the environment within which the agent operates. Machine Learning
is also closely allied with Artiﬁcial Intelligence, with Machine Learning placing more emphasis on using data
to drive and adapt the model.

In the early stages of Machine Learning and related areas, similar techniques were discovered in relatively
isolated research communities. This book presents a uniﬁed treatment via graphical models, a marriage
between graph and probability theory, facilitating the transference of Machine Learning concepts between
diﬀerent branches of the mathematical and computational sciences.

Whom this book is for

The book is designed to appeal to students with only a modest mathematical background in undergraduate
calculus and linear algebra. No formal computer science or statistical background is required to follow the
book, although a basic familiarity with probability, calculus and linear algebra would be useful. The book
should appeal to students from a variety of backgrounds, including Computer Science, Engineering, applied
Statistics, Physics, and Bioinformatics that wish to gain an entry to probabilistic approaches in Machine
Learning. In order to engage with students, the book introduces fundamental concepts in inference using

III

only minimal reference to algebra and calculus. More mathematical techniques are postponed until as and
when required, always with the concept as primary and the mathematics secondary.

The concepts and algorithms are described with the aid of many worked examples. The exercises and
demonstrations, together with an accompanying MATLAB toolbox, enable the reader to experiment and
more deeply understand the material. The ultimate aim of the book is to enable the reader to construct
novel algorithms. The book therefore places an emphasis on skill learning, rather than being a collection of
recipes. This is a key aspect since modern applications are often so specialised as to require novel methods.
The approach taken throughout is to describe the problem as a graphical model, which is then translated
into a mathematical framework, ultimately leading to an algorithmic implementation in the BRMLtoolbox.

The book is primarily aimed at ﬁnal year undergraduates and graduates without signiﬁcant experience in
mathematics. On completion, the reader should have a good understanding of the techniques, practicalities
and philosophies of probabilistic aspects of Machine Learning and be well equipped to understand more
advanced research level material.

The structure of the book

The book begins with the basic concepts of graphical models and inference. For the independent reader
chapters 1,2,3,4,5,9,10,13,14,15,16,17,21 and 23 would form a good introduction to probabilistic reasoning,
modelling and Machine Learning. The material in chapters 19, 24, 25 and 28 is more advanced, with the
remaining material being of more specialised interest. Note that in each chapter the level of material is of
varying diﬃculty, typically with the more challenging material placed towards the end of each chapter. As
an introduction to the area of probabilistic modelling, a course can be constructed from the material as
indicated in the chart.

The material from parts I and II has been successfully used for courses on Graphical Models. I have also
taught an introduction to Probabilistic Machine Learning using material largely from part III, as indicated.
These two courses can be taught separately and a useful approach would be to teach ﬁrst the Graphical
Models course, followed by a separate Probabilistic Machine Learning course.

A short course on approximate inference can be constructed from introductory material in part I and the
more advanced material in part V, as indicated. The exact inference methods in part I can be covered
relatively quickly with the material in part V considered in more in depth.

A timeseries course can be made by using primarily the material in part IV, possibly combined with material
from part I for students that are unfamiliar with probabilistic modelling approaches. Some of this material,
particularly in chapter 25 is more advanced and can be deferred until the end of the course, or considered
for a more advanced course.

The references are generally to works at a level consistent with the book material and which are in the most
part readily available.

Accompanying code

The BRMLtoolbox is provided to help readers see how mathematical models translate into actual MAT-
LAB code. There are a large number of demos that a lecturer may wish to use or adapt to help illustrate
the material. In addition many of the exercises make use of the code, helping the reader gain conﬁdence
in the concepts and their application. Along with complete routines for many Machine Learning methods,
the philosophy is to provide low level routines whose composition intuitively follows the mathematical de-
scription of the algorithm. In this way students may easily match the mathematics with the corresponding
algorithmic implementation.

IV

DRAFT April 24, 2015

e
s
r
u
o
C
g
n
i
n
r
a
e
L

e
n
i
h
c
a
M

c
i
t
s
i
l
i
b
a
b
o
r
P

e
s
r
u
o
C
t
r
o
h
S

e
c
n
e
r
e
f
n
I

e
t
a
m
i
x
o
r
p
p
A

e
s
r
u
o
C
s
l
e
d
o
M

l
a
c
i
h
p
a
r
G

e
s
r
u
o
C
g
n
i
l
l
e
d
o
M

c
i
t
s
i
l
i
b
a
b
o
r
P

e
s
r
u
o
C
t
r
o
h
S

s
e
i
r
e
s
-
e
m
T

i

Part I:
Inference in Probabilistic Models

Part II:
Learning in Probabilistic Models

Part III:
Machine Learning

Part IV:
Dynamical Models

1: Probabilistic Reasoning
2: Basic Graph Concepts
3: Belief Networks
4: Graphical Models
5: Eﬃcient Inference in Trees
6: The Junction Tree Algorithm
7: Making Decisions

8: Statistics for Machine Learning
9: Learning as Inference
10: Naive Bayes
11: Learning with Hidden Variables
12: Bayesian Model Selection

13: Machine Learning Concepts
14: Nearest Neighbour Classiﬁcation
15: Unsupervised Linear Dimension Reduction
16: Supervised Linear Dimension Reduction
17: Linear Models
18: Bayesian Linear Models
19: Gaussian Processes
20: Mixture Models
21: Latent Linear Models
22: Latent Ability Models

23: Discrete-State Markov Models
24: Continuous-State Markov Models
25: Switching Linear Dynamical Systems
26: Distributed Computation

Part V:
Approximate Inference

27: Sampling
28: Deterministic Approximate Inference

Website

The BRMLtoolbox along with an electronic version of the book is available from

www.cs.ucl.ac.uk/staff/D.Barber/brml

Instructors seeking solutions to the exercises can ﬁnd information at the website, along with additional
teaching materials.

DRAFT April 24, 2015

V

Other books in this area

The literature on Machine Learning is vast with much relevant literature also contained in statistics, en-
gineering and other physical sciences. A small list of more specialised books that may be referred to for
deeper treatments of speciﬁc topics is:

•

•

Graphical models

– Graphical models by S. Lauritzen, Oxford University Press, 1996.
– Bayesian Networks and Decision Graphs by F. Jensen and T. D. Nielsen, Springer Verlag, 2007.
– Probabilistic Networks and Expert Systems by R. G. Cowell, A. P. Dawid, S. L. Lauritzen and D.

J. Spiegelhalter, Springer Verlag, 1999.

– Probabilistic Reasoning in Intelligent Systems by J. Pearl, Morgan Kaufmann, 1988.
– Graphical Models in Applied Multivariate Statistics by J. Whittaker, Wiley, 1990.
– Probabilistic Graphical Models: Principles and Techniques by D. Koller and N. Friedman, MIT

Press, 2009.

Machine Learning and Information Processing

– Information Theory, Inference and Learning Algorithms by D. J. C. MacKay, Cambridge Uni-

versity Press, 2003.

– Pattern Recognition and Machine Learning by C. M. Bishop, Springer Verlag, 2006.
– An Introduction To Support Vector Machines, N. Cristianini and J. Shawe-Taylor, Cambridge

University Press, 2000.

– Gaussian Processes for Machine Learning by C. E. Rasmussen and C. K. I. Williams, MIT press,

2006.

Acknowledgements

Many people have helped this book along the way either in terms of reading, feedback, general insights,
allowing me to present their work, or just plain motivation. Amongst these I would like to thank Dan
Cornford, Massimiliano Pontil, Mark Herbster, John Shawe-Taylor, Vladimir Kolmogorov, Yuri Boykov,
Tom Minka, Simon Prince, Silvia Chiappa, Bertrand Mesot, Robert Cowell, Ali Taylan Cemgil, David Blei,
Jeﬀ Bilmes, David Cohn, David Page, Peter Sollich, Chris Williams, Marc Toussaint, Amos Storkey, Za-
kria Hussain, Le Chen, Seraf´ın Moral, Milan Studen´y, Luc De Raedt, Tristan Fletcher, Chris Vryonides,
Yannis Haralambous (and particularly for his help with example 1.5), Tom Furmston, Ed Challis and Chris
Bracegirdle. I would also like to thank the many students that have helped improve the material during
lectures over the years. I’m particularly grateful to Taylan Cemgil for allowing his GraphLayout package to
be bundled with the BRMLtoolbox.

++

The staﬀ at Cambridge University Press have been a delight to work with and I would especially like to
thank Heather Bergman for her initial endeavors and the wonderful Diana Gillooly for her continued enthu-
siasm.

A heartfelt thankyou to my parents and sister – I hope this small token will make them proud. I’m also
fortunate to be able to acknowledge the support and generosity of friends throughout. Finally, I’d like to
thank Silvia who made it all worthwhile.

VI

DRAFT April 24, 2015

BRMLtoolbox

The BRMLtoolbox is a lightweight set of routines that enables the reader to experiment with concepts in
graph theory, probability theory and Machine Learning. The code contains basic routines for manipulating
discrete variable distributions, along with more limited support for continuous variables. In addition there
are many hard-coded standard Machine Learning algorithms. The website contains also a complete list of
all the teaching demos and related exercise material.

BRMLTOOLKIT

Graph Theory

ancestors
ancestralorder
descendents
children
edges
elimtri
connectedComponents
istree
neigh
noselfpath
parents
spantree
triangulate
triangulatePorder

- Return the ancestors of nodes x in DAG A
- Return the ancestral order or the DAG A (oldest first)
- Return the descendents of nodes x in DAG A
- return the children of variable x given adjacency matrix A
- Return edge list from adjacency matrix A
- Return a variable elimination sequence for a triangulated graph
- Find the connected components of an adjacency matrix
- Check if graph is singly-connected
- Find the neighbours of vertex v on a graph with adjacency matrix G
- return a path excluding self transitions
- return the parents of variable x given adjacency matrix A
- Find a spanning tree from an edge list
- Triangulate adjacency matrix A
- Triangulate adjacency matrix A according to a partial ordering

Potential manipulation

condpot
changevar
dag
deltapot
disptable
divpots
drawFG
drawID
drawJTree
drawNet
evalpot
exppot
eyepot
grouppot
groupstate
logpot
markov
maxpot
maxsumpot
multpots
numstates

- Return a potential conditioned on another variable
- Change variable names in a potential
- Return the adjacency matrix (zeros on diagonal) for a Belief Network
- A delta function potential
- Print the table of a potential
- Divide potential pota by potb
- Draw the Factor Graph A
- plot an Influence Diagram
- plot a Junction Tree
- plot network
- Evaluate the table of a potential when variables are set
- exponential of a potential
- Return a unit potential
- Form a potential based on grouping variables together
- Find the state of the group variables corresponding to a given ungrouped state
- logarithm of the potential
- Return a symmetric adjacency matrix of Markov Network in pot
- Maximise a potential over variables
- Maximise or Sum a potential over variables
- Multiply potentials into a single potential
- Number of states of the variables in a potential

VII

orderpot
orderpotfields
potsample
potscontainingonly
potvariables
setevpot
setpot
setstate
squeezepots
sumpot
sumpotID
sumpots
table
ungrouppot
uniquepots
whichpot

- Return potential with variables reordered according to order
- Order the fields of the potential, creating blank entries where necessary
- Draw sample from a single potential
- Returns those potential numbers that contain only the required variables
- Returns information about all variables in a set of potentials
- Sets variables in a potential into evidential states
- sets potential variables to specified states
- set a potential’s specified joint state to a specified value
- Eliminate redundant potentials (those contained wholly within another)
- Sum potential pot over variables
- Return the summed probability and utility tables from an ID
- Sum a set of potentials
- Return the potential table
- Form a potential based on ungrouping variables
- Eliminate redundant potentials (those contained wholly within another)
- Returns potentials that contain a set of variables

Routines also extend the toolbox to deal with Gaussian potentials:
multpotsGaussianMoment.m, sumpotGaussianCanonical.m, sumpotGaussianMoment.m, multpotsGaussianCanonical.m
See demoSumprodGaussCanon.m, demoSumprodGaussCanonLDS.m, demoSumprodGaussMoment.m

Inference

- Update potentials in absorption message passing on a Junction Tree
- Perform full round of absorption on a Junction Tree
- Perform full round of absorption on an Influence Diagram
- Ancestral sampling from a Belief Network
- get the MAP assignment for a binary MRF with positive W
- Bucket Elimination on a set of potentials
- Conditional Independence check using graph of variable interactions
- Compute the empirical log Bayes Factor and MI for independence/dependence
- Numerical conditional independence measure
- conditional mutual information I(x,y|z) of a potential.

absorb
absorption
absorptionID
ancestralsample
binaryMRFmap
bucketelim
condindep
condindepEmp
condindepPot
condMI
FactorConnectingVariable - Factor nodes connecting to a set of variables
FactorGraph
IDvars
jtassignpot
jtree
jtreeID
LoopyBP
MaxFlow
maxNpot
maxNprodFG
maxprodFG
MDPemDeterministicPolicy - Solve MDP using EM with deterministic policy
MDPsolve
MesstoFact
metropolis
mostprobablepath
mostprobablepathmult
sumprodFG

- Returns a Factor Graph adjacency matrix based on potentials
- probability and decision variables from a partial order
- Assign potentials to cliques in a Junction Tree
- Setup a Junction Tree based on a set of potentials
- Setup a Junction Tree based on an Influence Diagram
- loopy Belief Propagation using sum-product algorithm
- Ford Fulkerson max flow - min cut algorithm (breadth first search)
- Find the N most probable values and states in a potential
- N-Max-Product algorithm on a Factor Graph (Returns the Nmax most probable States)
- Max-Product algorithm on a Factor Graph

- Solve a Markov Decision Process
- Returns the message numbers that connect into factor potential
- Metropolis sample
- Find the most probable path in a Markov Chain
- Find the all source all sink most probable paths in a Markov Chain
- Sum-Product algorithm on a Factor Graph represented by A

Speciﬁc Models

ARlds
ARtrain
BayesLinReg
BayesLogRegressionRVM
CanonVar
cca
covfnGE
EMbeliefnet
EMminimizeKL
EMqTranMarginal
EMqUtilMarginal
EMTotalBetaMessage
EMvalueTable
FA

VIII

- Learn AR coefficients using a Linear Dynamical System
- Fit autoregressive (AR) coefficients of order L to v.
- Bayesian Linear Regression training using basis functions phi(x)
- Bayesian Logistic Regression with the Relevance Vector Machine
- Canonical Variates (no post rotation of variates)
- canonical correlation analysis
- Gamma Exponential Covariance Function
- train a Belief Network using Expectation Maximisation
- MDP deterministic policy solver. Finds optimal actions
- EM marginal transition in MDP
- Returns term proportional to the q marginal for the utility term
- backward information needed to solve the MDP process using message passing
- MDP solver calculates the value function of the MDP with the current policy
- Factor Analysis

DRAFT April 24, 2015

- Fit a mixture of Gaussian to the data X using EM
- Gaussian Process Binary Classification
- Gaussian Process Regression
- Learn a sequence for a Hopfield Network
- HMM Backward Pass
- Backward Pass (beta method) for the Switching Autoregressive HMM
- EM algorithm for HMM
- HMM Forward Pass
- Switching Autoregressive HMM with switches updated only every Tskip timesteps
- HMM Posterior smoothing using the Rauch-Tung-Striebel correction method
- Smoothing for a Hidden Markov Model (HMM)
- Switching Autoregressive HMM smoothing
- Viterbi most likely joint hidden state of a HMM
- A kernel evaluated at two points
- K-means clustering algorithm
- Full Backward Pass for a Latent Linear Dynamical System (RTS correction method)
- Single Backward update for a Latent Linear Dynamical System (RTS smoothing update)
- Full Forward Pass for a Latent Linear Dynamical System (Kalman Filter)
- Single Forward update for a Latent Linear Dynamical System (Kalman Filter)
- Linear Dynamical System : Filtering and Smoothing
- Subspace Method for identifying Linear Dynamical System
- Learning Logistic Linear Regression Using Gradient Ascent (BATCH VERSION)
- EM training of a Mixture of a product of Bernoulli distributions
- EM training for a mixture of Markov Models
- Naive Bayes prediction having used a Dirichlet prior for training

GMMem
GPclass
GPreg
HebbML
HMMbackward
HMMbackwardSAR
HMMem
HMMforward
HMMforwardSAR
HMMgamma
HMMsmooth
HMMsmoothSAR
HMMviterbi
kernel
Kmeans
LDSbackward
LDSbackwardUpdate
LDSforward
LDSforwardUpdate
LDSsmooth
LDSsubspace
LogReg
MIXprodBern
mixMarkov
NaiveBayesDirichletTest
NaiveBayesDirichletTrain - Naive Bayes training using a Dirichlet prior
NaiveBayesTest
NaiveBayesTrain
nearNeigh
pca
plsa
plsaCond
rbf
SARlearn
SLDSbackward
SLDSforward
SLDSmargGauss
softloss
svdm
SVMtrain

- Test Naive Bayes Bernoulli Distribution after Max Likelihood training
- Train Naive Bayes Bernoulli Distribution using Max Likelihood
- Nearest Neighbour classification
- Principal Components Analysis
- Probabilistic Latent Semantic Analysis
- Conditional PLSA (Probabilistic Latent Semantic Analysis)
- Radial Basis function output
- EM training of a Switching AR model
- Backward pass using a Mixture of Gaussians
- Switching Latent Linear Dynamical System Gaussian Sum forward pass
- compute the single Gaussian from a weighted SLDS mixture
- Soft loss function
- Singular Value Decomposition with missing values
- train a Support vector Machine

General

argmax
assign
betaXbiggerY
bar3zcolor
avsigmaGauss
cap
chi2test
count
condexp
condp
dirrnd
field2cell
GaussCond
hinton
ind2subv
ismember_sorted
lengthcell
logdet
logeps
logGaussGamma
logsumexp
logZdirichlet
majority
maxarray
maxNarray

- performs argmax returning the index and value
- Assigns values to variables
- p(x>y) for x~Beta(a,b), y~Beta(c,d)
- Plot a 3D bar plot of the matrix Z
- Average of a logistic sigmoid under a Gaussian
- Cap x at absolute value c
- inverse of the chi square cumulative density
- for a data matrix (each column is a datapoint), return the state counts
- Compute normalised p proportional to exp(logp);
- Make a conditional distribution from the matrix
- Samples from a Dirichlet distribution
- Place the field of a structure in a cell
- Return the mean and covariance of a conditioned Gaussian
- Plot a Hinton diagram
- Subscript vector from linear index
- True for member of sorted set
- Length of each cell entry
- Log determinant of a positive definite matrix computed in a numerically stable manner
- log(x+eps)
- unnormalised log of the Gauss-Gamma distribution
- Compute log(sum(exp(a).*b)) valid for large a
- Log Normalisation constant of a Dirichlet distribution with parameter u
- Return majority values in each column on a matrix
- Maximise a multi-dimensional array over a set of dimensions
- Find the highest values and states of an array over a set of dimensions

DRAFT April 24, 2015

IX

mix2mix
mvrandn
mygamrnd
mynanmean
mynansum
mynchoosek
myones
myrand
myzeros
normp
randgen
replace
sigma
sigmoid
sqdist
subv2ind
sumlog

Miscellaneous

compat
logp
placeobject
plotCov
pointsCov
setup
validgridposition

- Fit a mixture of Gaussians with another mixture of Gaussians
- Samples from a multi-variate Normal(Gaussian) distribution
- Gamma random variate generator
- mean of values that are not nan
- sum of values that are not nan
- binomial coefficient v choose k
- same as ones(x), but if x is a scalar, interprets as ones([x 1])
- same as rand(x) but if x is a scalar interprets as rand([x 1])
- same as zeros(x) but if x is a scalar interprets as zeros([x 1])
- Make a normalised distribution from an array
- Generates discrete random variables given the pdf
- Replace instances of a value with another value
- 1./(1+exp(-x))
- 1./(1+exp(-beta*x))
- Square distance between vectors in x and y
- Linear index from subscript vector.
- sum(log(x)) with a cutoff at 10e-200

- Compatibility of object F being in position h for image v on grid Gx,Gy
- The logarithm of a specific non-Gaussian distribution
- Place the object F at position h in grid Gx,Gy
- return points for plotting an ellipse of a covariance
- unit variance contours of a 2D Gaussian with mean m and covariance S
- run me at initialisation -- checks for bugs in matlab and initialises path
- Returns 1 if point is on a defined grid

X

DRAFT April 24, 2015

Contents

Front Matter

I
II
Notation List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Preface
II
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BRML toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . VII
Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XI

I

Inference in Probabilistic Models

1

1 Probabilistic Reasoning

1.1 Probability Refresher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Interpreting Conditional Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7
7
1.1.1
9
1.1.2 Probability Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.2 Probabilistic Reasoning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3 Prior, Likelihood and Posterior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.3.1 Two dice : what were the individual scores? . . . . . . . . . . . . . . . . . . . . . . . . 19
1.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.5 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.5.1 Basic Probability code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.5.2 General utilities
1.5.3 An example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

1.6 Exercises

2 Basic Graph Concepts

25
2.1 Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.2 Numerically Encoding Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.1 Edge list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.2 Adjacency matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.2.3 Clique matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.4 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.4.1 Utility routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

2.5 Exercises

3 Belief Networks

33
3.1 The Beneﬁts of Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.1.1 Modelling independencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.1.2 Reducing the burden of speciﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.2 Uncertain and Unreliable Evidence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.2.1 Uncertain evidence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

XI

CONTENTS

CONTENTS

3.2.2 Unreliable evidence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.3 Belief Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.3.1 Conditional independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.3.2 The impact of collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.3.3 Graphical path manipulations for independence . . . . . . . . . . . . . . . . . . . . . . 45
3.3.4
d-Separation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.3.5 Graphical and distributional in/dependence . . . . . . . . . . . . . . . . . . . . . . . . 45
3.3.6 Markov equivalence in belief networks . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.3.7 Belief networks have limited expressibility . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.4 Causality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.4.1
Simpson’s paradox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.4.2 The do-calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Inﬂuence diagrams and the do-calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
3.4.3
3.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.6 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.6.1 Naive inference demo
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.6.2 Conditional independence demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.6.3 Utility routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

3.7 Exercises

4 Graphical Models

59
4.1 Graphical Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.2 Markov Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.2.1 Markov properties
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.2.2 Markov random ﬁelds
4.2.3 Hammersley-Cliﬀord Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
4.2.4 Conditional independence using Markov networks . . . . . . . . . . . . . . . . . . . . . 65
4.2.5 Lattice Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4.3 Chain Graphical Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4.4 Factor Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.4.1 Conditional independence in factor graphs . . . . . . . . . . . . . . . . . . . . . . . . . 70
4.5 Expressiveness of Graphical Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
4.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.7 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.8 Exercises

5 Eﬃcient Inference in Trees

5.2 Other Forms of Inference

77
5.1 Marginal Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
5.1.1 Variable elimination in a Markov chain and message passing . . . . . . . . . . . . . . . 77
. . . . . . . . . . . . . . . . . . . . . . . 80
5.1.2 The sum-product algorithm on factor graphs
5.1.3 Dealing with Evidence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.1.4 Computing the marginal likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5.1.5 The problem with loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.2.1 Max-Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
5.2.2 Finding the N most probable states
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
5.2.3 Most probable path and shortest path . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
5.2.4 Mixed inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Inference in Multiply Connected Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5.3.1 Bucket elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
5.3.2 Loop-cut conditioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
5.4 Message Passing for Continuous Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
5.6 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.6.1 Factor graph examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
5.6.2 Most probable and shortest path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

5.3

XII

DRAFT April 24, 2015

CONTENTS

CONTENTS

5.6.3 Bucket elimination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
5.6.4 Message passing on Gaussians . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

5.7 Exercises

6 The Junction Tree Algorithm

6.2 Clique Graphs

6.3 Junction Trees

6.1 Clustering Variables

101
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
6.1.1 Reparameterisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
6.2.1 Absorption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
6.2.2 Absorption schedule on clique trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
6.3.1 The running intersection property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
6.4 Constructing a Junction Tree for Singly-Connected Distributions . . . . . . . . . . . . . . . . 108
6.4.1 Moralisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.4.2 Forming the clique graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.4.3 Forming a junction tree from a clique graph . . . . . . . . . . . . . . . . . . . . . . . . 108
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
6.4.4 Assigning potentials to cliques
6.5 Junction Trees for Multiply-Connected Distributions . . . . . . . . . . . . . . . . . . . . . . . 109
6.5.1 Triangulation algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
6.6 The Junction Tree Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
6.6.1 Remarks on the JTA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
6.6.2 Computing the normalisation constant of a distribution . . . . . . . . . . . . . . . . . 114
6.6.3 The marginal likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Some small JTA examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
6.6.4
Shafer-Shenoy propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
6.6.5
6.7 Finding the Most Likely State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
6.8 Reabsorption : Converting a Junction Tree to a Directed Network . . . . . . . . . . . . . . . 118
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
6.9 The Need For Approximations
6.9.1 Bounded width junction trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
6.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
6.11 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
6.11.1 Utility routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

6.12 Exercises

7 Making Decisions

7.3.1

Syntax of inﬂuence diagrams

7.4 Solving Inﬂuence Diagrams

125
7.1 Expected Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.1.1 Utility of money . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
7.2 Decision Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
7.3 Extending Bayesian Networks for Decisions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
7.4.1 Messages on an ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
7.4.2 Using a junction tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
7.5 Markov Decision Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
7.5.1 Maximising expected utility by message passing . . . . . . . . . . . . . . . . . . . . . . 138
7.5.2 Bellman’s equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
7.6.1 Value iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
7.6.2 Policy iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
7.6.3 A curse of dimensionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
7.7 Variational Inference and Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
7.8 Financial Matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
7.8.1 Options pricing and expected utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
7.8.2 Binomial options pricing model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
7.8.3 Optimal investment
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

7.6 Temporally Unbounded MDPs

7.9 Further Topics

DRAFT April 24, 2015

XIII

CONTENTS

CONTENTS

7.9.1 Partially observable MDPs
7.9.2 Reinforcement learning

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
7.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
7.11 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
7.11.1 Sum/Max under a partial order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
7.11.2 Junction trees for inﬂuence diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
7.11.3 Party-Friend example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
7.11.4 Chest Clinic with Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
7.11.5 Markov decision processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

7.12 Exercises

II Learning in Probabilistic Models

157

8 Statistics for Machine Learning

8.2 Distributions

161
8.1 Representing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
8.1.1 Categorical
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
8.1.2 Ordinal
8.1.3 Numerical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
8.2.1 The Kullback-Leibler Divergence KL(q
p) . . . . . . . . . . . . . . . . . . . . . . . . . 165
|
8.2.2 Entropy and information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
8.3 Classical Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
8.4 Multivariate Gaussian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
8.4.1 Completing the square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
8.4.2 Conditioning as system reversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
8.4.3 Whitening and centering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
8.5 Exponential Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
8.5.1 Conjugate priors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
8.6 Learning distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
8.7 Properties of Maximum Likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
8.7.1 Training assuming the correct model class . . . . . . . . . . . . . . . . . . . . . . . . . 179
8.7.2 Training when the assumed model is incorrect . . . . . . . . . . . . . . . . . . . . . . . 179
8.7.3 Maximum likelihood and the empirical distribution . . . . . . . . . . . . . . . . . . . . 180
8.8 Learning a Gaussian . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
8.8.1 Maximum likelihood training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
8.8.2 Bayesian inference of the mean and variance . . . . . . . . . . . . . . . . . . . . . . . . 181
8.8.3 Gauss-Gamma distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
8.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
8.10 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
8.11 Exercises

9 Learning as Inference

195
9.1 Learning as Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
9.1.1 Learning the bias of a coin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
9.1.2 Making decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
9.1.3 A continuum of parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
. . . . . . . . . . . . . . . . . . . . . . . . . . 198
9.1.4 Decisions based on continuous intervals
9.2 Bayesian methods and ML-II
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
9.3 Maximum Likelihood Training of Belief Networks . . . . . . . . . . . . . . . . . . . . . . . . . 200
9.4 Bayesian Belief Network Training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
9.4.1 Global and local parameter independence . . . . . . . . . . . . . . . . . . . . . . . . . 203
9.4.2 Learning binary variable tables using a Beta prior
. . . . . . . . . . . . . . . . . . . . 204
9.4.3 Learning multivariate discrete tables using a Dirichlet prior . . . . . . . . . . . . . . . 206
9.5 Structure learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
9.5.1 PC algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

XIV

DRAFT April 24, 2015

CONTENTS

CONTENTS

9.5.2 Empirical independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
9.5.3 Network scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
9.5.4 Chow-Liu Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
9.6 Maximum Likelihood for Undirected models . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
9.6.1 The likelihood gradient
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
9.6.2 General tabular clique potentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
9.6.3 Decomposable Markov networks
9.6.4 Exponential form potentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
9.6.5 Conditional random ﬁelds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
9.6.6 Pseudo likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
9.6.7 Learning the structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
9.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
9.8 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
9.8.1 PC algorithm using an oracle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
9.8.2 Demo of empirical conditional independence . . . . . . . . . . . . . . . . . . . . . . . . 229
9.8.3 Bayes Dirichlet structure learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

9.9 Exercises

10 Naive Bayes

233
10.1 Naive Bayes and Conditional Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
10.2 Estimation using Maximum Likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
10.2.1 Binary attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
10.2.2 Multi-state variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
10.2.3 Text classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
10.3 Bayesian Naive Bayes
10.4 Tree Augmented Naive Bayes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
10.4.1 Learning tree augmented Naive Bayes networks . . . . . . . . . . . . . . . . . . . . . . 240
10.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
10.6 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
10.7 Exercises

11 Learning with Hidden Variables

245
11.1 Hidden Variables and Missing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
11.1.1 Why hidden/missing variables can complicate proceedings . . . . . . . . . . . . . . . . 245
11.1.2 The missing at random assumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
11.1.3 Maximum likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
11.1.4 Identiﬁability issues
11.2 Expectation Maximisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
11.2.1 Variational EM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
11.2.2 Classical EM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
11.2.3 Application to Belief networks
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
11.2.4 General case
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
11.2.5 Convergence
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
11.2.6 Application to Markov networks
11.3 Extensions of EM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
11.3.1 Partial M step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
11.3.2 Partial E-step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
11.4 A failure case for EM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
11.5 Variational Bayes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
11.5.1 EM is a special case of variational Bayes . . . . . . . . . . . . . . . . . . . . . . . . . . 262
11.5.2 An example: VB for the Asbestos-Smoking-Cancer network . . . . . . . . . . . . . . . 262
. . . . . . . . . . . . . . . . . . . . . . . . . 265
11.6.1 Undirected models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
11.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
11.8 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
11.9 Exercises

11.6 Optimising the Likelihood by Gradient Methods

DRAFT April 24, 2015

XV

CONTENTS

CONTENTS

12 Bayesian Model Selection

271
12.1 Comparing Models the Bayesian Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
12.2 Illustrations : coin tossing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
12.2.1 A discrete parameter space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
12.2.2 A continuous parameter space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
12.3 Occam’s Razor and Bayesian Complexity Penalisation . . . . . . . . . . . . . . . . . . . . . . 274
12.4 A continuous example : curve ﬁtting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
12.5 Approximating the Model Likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
12.5.1 Laplace’s method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
12.5.2 Bayes information criterion (BIC)
12.6 Bayesian Hypothesis Testing for Outcome Analysis . . . . . . . . . . . . . . . . . . . . . . . . 280
12.6.1 Outcome analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
12.6.2 Hindep : model likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
12.6.3 Hsame : model likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
12.6.4 Dependent outcome analysis
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
12.6.5 Is classiﬁer A better than B? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
12.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
12.8 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
12.9 Exercises

III Machine Learning

291

13 Machine Learning Concepts

13.1 Styles of Learning

295
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
13.1.1 Supervised learning
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
13.1.2 Unsupervised learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
13.1.3 Anomaly detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
13.1.4 Online (sequential) learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
13.1.5 Interacting with the environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
13.1.6 Semi-supervised learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
13.2 Supervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
13.2.1 Utility and Loss
13.2.2 Using the empirical distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
13.2.3 Bayesian decision approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
13.3 Bayes versus Empirical Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
13.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
13.5 Exercises

14 Nearest Neighbour Classiﬁcation

309
14.1 Do As Your Neighbour Does . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
14.2 K-Nearest Neighbours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
14.3 A Probabilistic Interpretation of Nearest Neighbours . . . . . . . . . . . . . . . . . . . . . . . 312
14.3.1 When your nearest neighbour is far away . . . . . . . . . . . . . . . . . . . . . . . . . 313
14.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
14.5 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
14.6 Exercises

15 Unsupervised Linear Dimension Reduction

315
15.1 High-Dimensional Spaces – Low Dimensional Manifolds
. . . . . . . . . . . . . . . . . . . . . 315
15.2 Principal Components Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
15.2.1 Deriving the optimal linear reconstruction . . . . . . . . . . . . . . . . . . . . . . . . . 316
15.2.2 Maximum variance criterion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
15.2.3 PCA algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
15.2.4 PCA and nearest neighbours classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . 320
15.2.5 Comments on PCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320

XVI

DRAFT April 24, 2015

CONTENTS

CONTENTS

15.4 Latent Semantic Analysis

15.3 High Dimensional Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
15.3.1 Eigen-decomposition for N < D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
15.3.2 PCA via Singular value decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
15.4.1 Information retrieval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
15.5 PCA With Missing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
15.5.1 Finding the principal directions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
15.5.2 Collaborative ﬁltering using PCA with missing data . . . . . . . . . . . . . . . . . . . 328
15.6 Matrix Decomposition Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
15.6.1 Probabilistic latent semantic analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
15.6.2 Extensions and variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
15.6.3 Applications of PLSA/NMF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
15.7 Kernel PCA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
15.8 Canonical Correlation Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
15.8.1 SVD formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
15.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
15.10Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
15.11Exercises

16 Supervised Linear Dimension Reduction

343
16.1 Supervised Linear Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
16.2 Fisher’s Linear Discriminant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
16.3 Canonical Variates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
16.3.1 Dealing with the nullspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
16.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
16.5 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
16.6 Exercises

17 Linear Models

17.3 The Dual Representation and Kernels

351
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
17.1 Introduction: Fitting A Straight Line
17.2 Linear Parameter Models for Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
17.2.1 Vector outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
17.2.2 Regularisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
17.2.3 Radial basis functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
17.3.1 Regression in the dual-space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
17.4 Linear Parameter Models for Classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
17.4.1 Logistic regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
17.4.2 Beyond ﬁrst order gradient ascent
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
17.4.3 Avoiding overconﬁdent classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
17.4.4 Multiple classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
17.4.5 The Kernel Trick for Classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
17.5.1 Maximum margin linear classiﬁer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
17.5.2 Using kernels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
17.5.3 Performing the optimisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
17.5.4 Probabilistic interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
17.6 Soft Zero-One Loss for Outlier Robustness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
17.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
17.8 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
17.9 Exercises

17.5 Support Vector Machines

DRAFT April 24, 2015

XVII

CONTENTS

CONTENTS

18 Bayesian Linear Models

373
18.1 Regression With Additive Gaussian Noise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
18.1.1 Bayesian linear parameter models
18.1.2 Determining hyperparameters: ML-II
. . . . . . . . . . . . . . . . . . . . . . . . . . . 375
18.1.3 Learning the hyperparameters using EM . . . . . . . . . . . . . . . . . . . . . . . . . . 376
18.1.4 Hyperparameter optimisation : using the gradient
. . . . . . . . . . . . . . . . . . . . 377
18.1.5 Validation likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
18.1.6 Prediction and model averaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
18.1.7 Sparse linear models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
18.2 Classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
18.2.1 Hyperparameter optimisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
18.2.2 Laplace approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
18.2.3 Variational Gaussian approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
18.2.4 Local variational approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
18.2.5 Relevance vector machine for classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . 387
18.2.6 Multi-class case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
18.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
18.4 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
18.5 Exercises

19 Gaussian Processes

391
19.1 Non-Parametric Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
19.1.1 From parametric to non-parametric
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
19.1.2 From Bayesian linear models to Gaussian processes . . . . . . . . . . . . . . . . . . . . 392
19.1.3 A prior on functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
19.2 Gaussian Process Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
19.2.1 Regression with noisy training outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
19.3 Covariance Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
19.3.1 Making new covariance functions from old . . . . . . . . . . . . . . . . . . . . . . . . . 397
19.3.2 Stationary covariance functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
19.3.3 Non-stationary covariance functions
19.4 Analysis of Covariance Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
19.4.1 Smoothness of the functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
19.4.2 Mercer kernels
. . . . . . . . . . . . . . . . . . . . . . . . . . . 401
19.4.3 Fourier analysis for stationary kernels
19.5 Gaussian Processes for Classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
19.5.1 Binary classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
19.5.2 Laplace’s approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
19.5.3 Hyperparameter optimisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
19.5.4 Multiple classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
19.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
19.7 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
19.8 Exercises

20 Mixture Models

20.3 The Gaussian Mixture Model

409
20.1 Density Estimation Using Mixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
. . . . . . . . . . . . . . . . . . . . . . . . . . 410
20.2 Expectation Maximisation for Mixture Models
20.2.1 Unconstrained discrete tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
20.2.2 Mixture of product of Bernoulli distributions . . . . . . . . . . . . . . . . . . . . . . . 413
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
20.3.1 EM algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
20.3.2 Practical issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
20.3.3 Classiﬁcation using Gaussian mixture models . . . . . . . . . . . . . . . . . . . . . . . 419
20.3.4 The Parzen estimator
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
20.3.5 K-Means
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
20.3.6 Bayesian mixture models

XVIII

DRAFT April 24, 2015

CONTENTS

CONTENTS

20.6 Mixed Membership Models

20.5.1 Joint indicator approach: factorised prior
20.5.2 Polya prior

20.3.7 Semi-supervised learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
20.4 Mixture of Experts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
20.5 Indicator Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
. . . . . . . . . . . . . . . . . . . . . . . . . 423
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
20.6.1 Latent Dirichlet allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
20.6.2 Graph based representations of data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
20.6.3 Dyadic data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
20.6.4 Monadic data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
20.6.5 Cliques and adjacency matrices for monadic binary data . . . . . . . . . . . . . . . . . 429
20.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
20.8 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
20.9 Exercises

21 Latent Linear Models

435
21.1 Factor Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
21.1.1 Finding the optimal bias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
21.2 Factor Analysis : Maximum Likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
21.2.1 Eigen-approach likelihood optimisation . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
21.2.2 Expectation maximisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
21.3 Interlude: Modelling Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
. . . . . . . . . . . . . . . . . . . . . . . . . . . 444
21.4 Probabilistic Principal Components Analysis
21.5 Canonical Correlation Analysis and Factor Analysis
. . . . . . . . . . . . . . . . . . . . . . . 445
21.6 Independent Components Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
21.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
21.8 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
21.9 Exercises

22 Latent Ability Models

451
22.1 The Rasch Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
22.1.1 Maximum likelihood training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
22.1.2 Bayesian Rasch models
22.2 Competition Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
22.2.1 Bradley-Terry-Luce model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
22.2.2 Elo ranking model
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
22.2.3 Glicko and TrueSkill
22.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
22.4 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
22.5 Exercises

IV Dynamical Models

457

23 Discrete-State Markov Models

23.2 Hidden Markov Models

461
23.1 Markov Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
23.1.1 Equilibrium and stationary distribution of a Markov chain . . . . . . . . . . . . . . . . 462
23.1.2 Fitting Markov models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
23.1.3 Mixture of Markov models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
23.2.1 The classical inference problems
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
23.2.2 Filtering p(ht
23.2.3 Parallel smoothing p(ht
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
23.2.4 Correction smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
23.2.5 Sampling from p(h1:T
23.2.6 Most likely joint state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

v1:T )
|

v1:t)
|

v1:T )

|

DRAFT April 24, 2015

XIX

CONTENTS

CONTENTS

23.3 Learning HMMs

23.2.7 Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
23.2.8 Self localisation and kidnapped robots . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
23.2.9 Natural language models
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
23.3.1 EM algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
23.3.2 Mixture emission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
23.3.3 The HMM-GMM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
23.3.4 Discriminative training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
23.4 Related Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
23.4.1 Explicit duration model
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
23.4.2 Input-Output HMM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
23.4.3 Linear chain CRFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
23.4.4 Dynamic Bayesian networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
23.5 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
23.5.1 Object tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
23.5.2 Automatic speech recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
23.5.3 Bioinformatics
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
23.5.4 Part-of-speech tagging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
23.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
23.7 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
23.8 Exercises

24 Continuous-state Markov Models

24.1.1 Stationary distribution with noise

489
24.1 Observed Linear Dynamical Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
24.2 Auto-Regressive Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
24.2.1 Training an AR model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
24.2.2 AR model as an OLDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
24.2.3 Time-varying AR model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
24.2.4 Time-varying variance AR models
24.3 Latent Linear Dynamical Systems
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
24.4 Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
24.4.1 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
24.4.2 Smoothing : Rauch-Tung-Striebel correction method . . . . . . . . . . . . . . . . . . . 500
24.4.3 The likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
24.4.4 Most likely state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
24.4.5 Time independence and Riccati equations . . . . . . . . . . . . . . . . . . . . . . . . . 502
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
24.5.1 Identiﬁability issues
24.5.2 EM algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
24.5.3 Subspace Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
24.5.4 Structured LDSs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
24.5.5 Bayesian LDSs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
24.6.1 Inference
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
24.6.2 Maximum likelihood learning using EM . . . . . . . . . . . . . . . . . . . . . . . . . . 507
24.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
24.8 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
24.8.1 Autoregressive models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510

24.5 Learning Linear Dynamical Systems

24.6 Switching Auto-Regressive Models

24.9 Exercises

XX

DRAFT April 24, 2015

CONTENTS

CONTENTS

25 Switching Linear Dynamical Systems

513
25.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
25.2 The Switching LDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
25.2.1 Exact inference is computationally intractable . . . . . . . . . . . . . . . . . . . . . . . 514
25.3 Gaussian Sum Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
25.3.1 Continuous ﬁltering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
25.3.2 Discrete ﬁltering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
25.3.3 The likelihood p(v1:T ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
25.3.4 Collapsing Gaussians . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
25.3.5 Relation to other methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
25.4 Gaussian Sum Smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
25.4.1 Continuous smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
25.4.2 Discrete smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
25.4.3 Collapsing the mixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
25.4.4 Using mixtures in smoothing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
25.4.5 Relation to other methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
25.5 Reset Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
25.5.1 A Poisson reset model
25.5.2 Reset-HMM-LDS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
25.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
25.7 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
25.8 Exercises

26 Distributed Computation

531
26.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
26.2 Stochastic Hopﬁeld Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
26.3 Learning Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
26.3.1 A single sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
26.3.2 Multiple sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
26.3.3 Boolean networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
26.3.4 Sequence disambiguation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
26.4 Tractable Continuous Latent Variable Models . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
26.4.1 Deterministic latent variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
26.4.2 An augmented Hopﬁeld network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
26.5.1 Stochastically spiking neurons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
26.5.2 Hopﬁeld membrane potential
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
26.5.3 Dynamic synapses
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
26.5.4 Leaky integrate and ﬁre models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
26.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
26.7 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
26.8 Exercises

26.5 Neural Models

V Approximate Inference

545

27 Sampling

549
27.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
27.1.1 Univariate sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
27.1.2 Rejection sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
27.1.3 Multivariate sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
27.2 Ancestral Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
27.2.1 Dealing with evidence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
27.2.2 Perfect sampling for a Markov network . . . . . . . . . . . . . . . . . . . . . . . . . . 555
27.3 Gibbs Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555
27.3.1 Gibbs sampling as a Markov chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556

DRAFT April 24, 2015

XXI

CONTENTS

CONTENTS

27.4 Markov Chain Monte Carlo (MCMC)

27.3.2 Structured Gibbs sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
27.3.3 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
27.4.1 Markov chains
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
27.4.2 Metropolis-Hastings sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
27.5 Auxiliary Variable Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
27.5.1 Hybrid Monte Carlo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
27.5.2 Swendson-Wang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
27.5.3 Slice sampling
27.6 Importance Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
27.6.1 Sequential importance sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
27.6.2 Particle ﬁltering as an approximate forward pass . . . . . . . . . . . . . . . . . . . . . 569
27.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
27.8 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
27.9 Exercises

28 Deterministic Approximate Inference

q)
|

28.4 Variational Bounding Using KL(q

575
28.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
28.2 The Laplace approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
. . . . . . . . . . . . . . . . . . . . . . . 576
28.3 Properties of Kullback-Leibler Variational Inference
28.3.1 Bounding the normalisation constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
28.3.2 Bounding the marginal likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576
28.3.3 Bounding marginal quantities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
. . . . . . . . . . . . . . . . . . . . . . 577
28.3.4 Gaussian approximations using KL divergence
. . . . . . . . . . . 578
28.3.5 Marginal and moment matching properties of minimising KL(p
p) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
|
28.4.1 Pairwise Markov random ﬁeld . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579
28.4.2 General mean ﬁeld equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
. . . . . . . . . . . . 582
28.4.3 Asynchronous updating guarantees approximation improvement
28.4.4 Structured variational approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
28.5 Local and KL Variational Approximations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
28.5.1 Local approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
28.5.2 KL variational approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
28.6 Mutual Information Maximisation : A KL Variational Approach . . . . . . . . . . . . . . . . 587
28.6.1 The information maximisation algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 588
28.6.2 Linear Gaussian decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
28.7 Loopy Belief Propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
28.7.1 Classical BP on an undirected graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
28.7.2 Loopy BP as a variational procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
28.8 Expectation Propagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
28.9 MAP for Markov networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
28.9.1 Pairwise Markov networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
28.9.2 Attractive binary Markov networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
28.9.3 Potts model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 601
28.10Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
28.11Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
28.12Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
28.13Exercises

End Matter

VI Appendix

A Background Mathematics

XXII

609

609

611

DRAFT April 24, 2015

CONTENTS

CONTENTS

A.2 Multivariate Calculus

A.1 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
A.1.1 Vector algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
A.1.2 The scalar product as a projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
A.1.3 Lines in space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
A.1.4 Planes and hyperplanes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
A.1.5 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
A.1.6 Linear transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
A.1.7 Determinants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
A.1.8 Matrix inversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
A.1.9 Computing the matrix inverse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
A.1.10 Eigenvalues and eigenvectors
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
A.1.11 Matrix decompositions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
A.2.1 Interpreting the gradient vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
A.2.2 Higher derivatives
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
A.2.3 Matrix calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
A.3 Inequalities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
A.3.1 Convexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620
A.3.2 Jensen’s inequality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
A.4 Optimisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
A.5 Multivariate Optimisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
A.5.1 Gradient descent with ﬁxed stepsize . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
A.5.2 Gradient descent with line searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
A.5.3 Minimising quadratic functions using line search . . . . . . . . . . . . . . . . . . . . . 623
. . . . . . . . . . . . . . . . . . . . . 623
A.5.4 Gram-Schmidt construction of conjugate vectors
A.5.5 The conjugate vectors algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
A.5.6 The conjugate gradients algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624
A.5.7 Newton’s method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
A.6 Constrained Optimisation using Lagrange Multipliers . . . . . . . . . . . . . . . . . . . . . . . 627
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627

A.6.1 Lagrange Dual

Bibliography

Index

629

645

DRAFT April 24, 2015

XXIII

CONTENTS

CONTENTS

XXIV

DRAFT April 24, 2015

Part I

Inference in Probabilistic Models

1

Introduction to Part I

Probabilistic models explicitly take into account uncertainty and deal with our
imperfect knowledge of the world. Such models are of fundamental signiﬁcance in
Machine Learning since our understanding of the world will always be limited by our
observations and understanding. We will focus initially on using probabilistic models
as a kind of expert system.

In Part I, we assume that the model is fully speciﬁed. That is, given a model of the
environment, how can we use it to answer questions of interest. We will relate the
complexity of inferring quantities of interest to the structure of the graph describing
the model.
In addition, we will describe operations in terms of manipulations on
the corresponding graphs. As we will see, provided the graphs are simple tree-like
structures, most quantities of interest can be computed eﬃciently.

Part I deals with manipulating mainly discrete variable distributions and forms the
background to all the later material in the book.

DRAFT April 24, 2015

3

Some members of the graphical models family and their uses. Nodes further from the Graphical Models
root node are loosely speaking specialised versions of their parents. We discuss many of these models in
Part I, although some of the more specialised models are deferred to later Parts of the book.

4

DRAFT April 24, 2015

GraphicalModelsDirectedDirectedFactorGraphBayesianNetworksDynamicBayesnetsMarkovchainsHMMLDSLatentvariablemodelsDiscreteMixturemodelscluster-ingContinuousdimen-reductover-completerepres.InﬂuencediagramsStrongJTDecisiontheoryChainGraphsUndirectedGraphsMarkovnetworkinputdependentCRFPairwiseBoltz.machine(disc.)Gauss.Process(cont)CliqueGraphsJunctiontreeCliquetreeFactorGraphsGraphical models and associated (marginal) inference methods. Speciﬁc inference methods are highlighted
in red. Loosely speaking, provided the graph corresponding to the model is singly-connected most of
the standard (marginal) inference methods are tractable. Multiply-connected graphs are generally more
problematic, although there are special cases which remain tractable.

DRAFT April 24, 2015

5

GraphicalmodelMultiplyconnecteddecomposablecliquessmallmessages-tractablemessage-passingmessages-intractableapprox-requiredcliqueslargeapprox-requirednon-decomposableJTAcliquessmallabsorptionShafer-Shenoycliquesbig(ormess.intract)approx-requiredcutsetconditioning(inefﬁcient)tractable-special-casesGaussianattractive-binary-MRF-MAPplanar-binary-pure-interaction-MRFSinglyconnectedmessageupdatestractablesum/maxproductmessageupdatesintractableapproxrequired(EP)Bucketelimination(inefﬁcient)6

DRAFT April 24, 2015

CHAPTER 1

Probabilistic Reasoning

We have intuition about how uncertainty works in simple cases. To reach sensible conclusions in complicated
situations, however – where there may be many (possibly) related events and many possible outcomes –
we need a formal ‘calculus’ that extends our intuitive notions. The concepts, mathematical language and
rules of probability give us the formal framework we need.
In this chapter we review basic concepts in
probability – in particular, conditional probability and Bayes’ rule, the workhorses of machine learning.
Another strength of the language of probability is that it structures problems in a form consistent for
computer implementation. We also introduce basic features of the BRMLtoolbox that support manipulating
probability distributions.

1.1 Probability Refresher

Variables, States and Notational Shortcuts

Variables will be denoted using either upper case X or lower case x and a set of variables will typically be
denoted by a calligraphic symbol, for example

a, B, c

=

.

V

{

}

The domain of a variable x is written dom(x), and denotes the states x can take. States will typically
and p(c = heads)
be represented using sans-serif font. For example, for a coin c, dom(c) =
represents the probability that variable c is in state heads.
The meaning of p(state) will often be clear,
without speciﬁc reference to a variable. For example, if we are discussing an experiment about a coin c,
the meaning of p(heads) is clear from the context, being shorthand for p(c = heads). When summing over a
variable (cid:80)
s∈dom(x) f (x = s).
Given a variable, x, its domain dom(x) and a full speciﬁcation of the probability values for each of the
variable states, p(x), we have a distribution for x. Sometimes we will not fully specify the distribution, only
certain properties, such as for variables x, y, p(x, y) = p(x)p(y) for some unspeciﬁed p(x) and p(y). When
clarity on this is required we will say distributions with structure p(x)p(y), or a distribution class p(x)p(y).

x f (x), the interpretation is that all states of x are included, i.e. (cid:80)

heads, tails
}

x f (x)

(cid:80)

≡

{

For our purposes, events are expressions about random variables, such as Two heads in 6 coin tosses. Two
events are mutually exclusive if they cannot both be true. For example the events The coin is heads and
The coin is tails are mutually exclusive. One can think of deﬁning a new variable named by the event so,
for example, p(The coin is tails) can be interpreted as p(The coin is tails = true). We use the shorthand
p(x = tr) for the probability of event/variable x being in the state true and p(x = fa) for the probability of
variable x being in the state false.

Deﬁnition 1.1 (Rules of Probability for Discrete Variables).

7

The probability p(x = x) of variable x being in state x is represented by a value between 0 and 1.
p(x = x) = 1 means that we are certain x is in state x. Conversely, p(x = x) = 0 means that we are certain
x is not in state x. Values between 0 and 1 represent the degree of certainty of state occupancy.

Probability Refresher

The summation of the probability over all the states is 1:

(cid:88)

x∈dom(x)

p(x = x) = 1

This is called the normalisation condition. We will usually more conveniently write (cid:80)

x p(x) = 1.

Two variables x and y can interact through

p(x = a or y = b) = p(x = a) + p(y = b)

p(x = a and y = b)

−

Or, more generally, we can write

p(x or y) = p(x) + p(y)

p(x and y)

−

(1.1.1)

(1.1.2)

(1.1.3)

We will use the shorthand p(x, y) for p(x and y). Note that p(y, x) = p(x, y) and p(x or y) = p(y or x).

Deﬁnition 1.2 (Set notation). An alternative notation in terms of set theory is to write

p(x or y)

p(x

y),

∪

≡

p(x, y)

p(x

y)

∩

≡

(1.1.4)

Deﬁnition 1.3 (Marginals). Given a joint distribution p(x, y) the distribution of a single variable is given
by

p(x) =

(cid:88)

y

p(x, y)

(1.1.5)

Here p(x) is termed a marginal of the joint probability distribution p(x, y). The process of computing a
marginal from a joint distribution is called marginalisation. More generally, one has

p(x1, . . . , xi−1, xi+1, . . . , xn) =

(cid:88)

xi

p(x1, . . . , xn)

(1.1.6)

Deﬁnition 1.4 (Conditional Probability / Bayes’ Rule). The probability of event x conditioned on knowing
event y (or more shortly, the probability of x given y) is deﬁned as

p(x

y)
|

≡

p(x, y)
p(y)

(1.1.7)

If p(y) = 0 then p(x
Bayes’ rule

|

y) is not deﬁned. From this deﬁnition and p(x, y) = p(y, x) we immediately arrive at

p(x

y) =
|

p(y

x)p(x)
|
p(y)

(1.1.8)

Since Bayes’ rule trivially follows from the deﬁnition of conditional probability, we will sometimes be loose
in our language and use the terms Bayes’ rule and conditional probability as synonymous.

As we shall see throughout this book, Bayes’ rule plays a central role in probabilistic reasoning since it helps

8

DRAFT April 24, 2015

Probability Refresher

us ‘invert’ probabilistic relationships, translating between p(y

x) and p(x
|

y).
|

Deﬁnition 1.5 (Probability Density Functions). For a continuous variable x, the probability density f (x)
is deﬁned such that

f (x)

0,

≥

(cid:90) ∞

−∞

f (x)dx = 1

and the probability that x falls in an interval [a, b] is given by

p(a

x

≤

≤

b) =

(cid:90) b

a

f (x)dx

(1.1.9)

(1.1.10)

As shorthand we will sometimes write (cid:82)
x f (x), particularly when we want an expression to be valid for either
continuous or discrete variables. The multivariate case is analogous with integration over all real space, and
the probability that x belongs to a region of the space deﬁned accordingly. Unlike probabilities, probability
densities can take positive values greater than 1.

Formally speaking, for a continuous variable, one should not speak of the probability that x = 0.2 since the
probability of a single value is always zero. However, we shall often write p(x) for continuous variables, thus
not distinguishing between probabilities and probability density function values. Whilst this may appear
strange, the nervous reader may simply replace our p(x) notation for (cid:82)
x∈∆ f (x)dx, where ∆ is a small region
centred on x. This is well deﬁned in a probabilistic sense and, in the limit ∆ being very small, this would
give approximately ∆f (x). If we consistently use the same ∆ for all occurrences of pdfs, then we will simply
have a common prefactor ∆ in all expressions. Our strategy is to simply ignore these values (since in the end
only relative probabilities will be relevant) and write p(x). In this way, all the standard rules of probability
carry over, including Bayes’ Rule.

Remark 1.1 (Subjective Probability). Probability is a contentious topic and we do not wish to get bogged
down by the debate here, apart from pointing out that it is not necessarily the rules of probability that
are contentious, rather what interpretation we should place on them. In some cases potential repetitions
of an experiment can be envisaged so that the ‘long run’ (or frequentist) deﬁnition of probability in which
probabilities are deﬁned with respect to a potentially inﬁnite repetition of experiments makes sense. For
example, in coin tossing, the probability of heads might be interpreted as ‘If I were to repeat the experiment
of ﬂipping a coin (at ‘random’), the limit of the number of heads that occurred over the number of tosses
is deﬁned as the probability of a head occurring.’

Here’s a problem that is typical of the kind of scenario one might face in a machine learning situation. A
ﬁlm enthusiast joins a new online ﬁlm service. Based on expressing a few ﬁlms a user likes and dislikes,
the online company tries to estimate the probability that the user will like each of the 10000 ﬁlms in their
database. If we were to deﬁne probability as a limiting case of inﬁnite repetitions of the same experiment,
this wouldn’t make much sense in this case since we can’t repeat the experiment. However, if we assume
that the user behaves in a manner consistent with other users, we should be able to exploit the large amount
of data from other users’ ratings to make a reasonable ‘guess’ as to what this consumer likes. This degree
of belief or Bayesian subjective interpretation of probability sidesteps non-repeatability issues – it’s just a
framework for manipulating real values consistent with our intuition about probability[158].

1.1.1 Interpreting Conditional Probability

Conditional probability matches our intuitive understanding of uncertainty. For example, imagine a circular
dart board, split into 20 equal sections, labelled from 1 to 20. Randy, a dart thrower, hits any one of the 20
sections uniformly at random. Hence the probability that a dart thrown by Randy occurs in any one of the
20 regions is p(region i) = 1/20. A friend of Randy tells him that he hasn’t hit the 20 region. What is the
probability that Randy has hit the 5 region? Conditioned on this information, only regions 1 to 19 remain
possible and, since there is no preference for Randy to hit any of these regions, the probability is 1/19. The

DRAFT April 24, 2015

9

Probability Refresher

conditioning means that certain states are now inaccessible, and the original probability is subsequently
distributed over the remaining accessible states. From the rules of probability :

p(region 5

not region 20) =
|

p(region 5, not region 20)
p(not region 20)

=

p(region 5)
p(not region 20)

=

1/20
19/20

=

1
19

giving the intuitive result. An important point to clarify is that p(A = a
B = b) should not be interpreted
|
B = b) is the probability of the event A = a occurring’.
as ‘Given the event B = b has occurred, p(A = a
|
In most contexts, no such explicit temporal causality is implied1 and the correct interpretation should be ‘
B = b) is the probability of A being in state a under the constraint that B is in state b’.
p(A = a
|

The relation between the conditional p(A = a
constant since p(A = a, B = b) is not a distribution in A – in other words, (cid:80)
make it a distribution we need to divide : p(A = a, B = b)/ (cid:80)
a does sum to 1. Indeed, this is just the deﬁnition of p(A = a
|

B = b) and the joint p(A = a, B = b) is just a normalisation
|
= 1. To
a p(A = a, B = b) which, when summed over
B = b).

a p(A = a, B = b)

Deﬁnition 1.6 (Independence).

Variables x and y are independent if knowing the state (or value in the continuous case) of one variable
gives no extra information about the other variable. Mathematically, this is expressed by

p(x, y) = p(x)p(y)

Provided that p(x)

= 0 and p(y)

= 0 independence of x and y is equivalent to

p(x

y) = p(x)
|

⇔

p(y

x) = p(y)
|

If p(x

y) = p(x) for all states of x and y, then the variables x and y are said to be independent. If
|

p(x, y) = kf (x)g(y)

(1.1.11)

(1.1.12)

(1.1.13)

) and g(
for some constant k, and positive functions f (
·

) then x and y are independent and we write x
·

⊥⊥

y.

Example 1.1 (Independence). Let x denote the day of the week in which females are born, and y denote
the day in which males are born, with dom(x) = dom(y) =
. It is reasonable to expect that x
}
is independent of y. We randomly select a woman from the phone book, Alice, and ﬁnd out that she was
born on a Tuesday. We also randomly select a male at random, Bob. Before phoning Bob and asking him,
what does knowing Alice’s birth day add to which day we think Bob is born on? Under the independence
assumption, the answer is nothing. Note that this doesn’t mean that the distribution of Bob’s birthday is
necessarily uniform – it just means that knowing when Alice was born doesn’t provide any extra information
than we already knew about Bob’s birthday, p(y
x) = p(y). Indeed, the distribution of birthdays p(y) and
|
p(x) are non-uniform (statistically fewer babies are born on weekends), though there is nothing to suggest
that x are y are dependent.

1, . . . , 7

{

Deterministic Dependencies

Sometimes the concept of independence is perhaps a little strange. Consider the following : variables x and
y are both binary (their domains consist of two states). We deﬁne the distribution such that x and y are
always both in a certain joint state:

p(x = a, y = 1) = 1, p(x = a, y = 2) = 0, p(x = b, y = 2) = 0, p(x = b, y = 1) = 0

Are x and y dependent? The reader may show that p(x = a) = 1, p(x = b) = 0 and p(y = 1) = 1,
p(y = 2) = 0. Hence p(x)p(y) = p(x, y) for all states of x and y, and x and y are therefore independent.

1We will discuss issues related to causality further in section(3.4).

10

DRAFT April 24, 2015

(cid:54)
(cid:54)
(cid:54)
Probability Refresher

This may seem strange – we know for sure the relation between x and y, namely that they are always in the
same joint state, yet they are independent. Since the distribution is trivially concentrated in a single joint
state, knowing the state of x tells you nothing that you didn’t anyway know about the state of y, and vice
versa. This potential confusion comes from using the term ‘independent’ which may suggest that there is no
relation between objects discussed. The best way to think about statistical independence is to ask whether
or not knowing the state of variable y tells you something more than you knew before about variable x,
where ‘knew before’ means working with the joint distribution of p(x, y) to ﬁgure out what we can know
about x, namely p(x).

Deﬁnition 1.7 (Conditional Independence).

X ⊥⊥ Y| Z

(1.1.14)

denotes that the two sets of variables
of the set of variables

X
. For conditional independence,

are independent of each other provided we know the state
must be independent given all states of

and

and

Y

X

Y

. Formally, this means that

Z

Z

,

p(

X

Y|Z

) = p(

X |Z

)p(

Y|Z

)

for all states of
which case

is (unconditionally) independent of

. In case the conditioning set is empty we may also write
.

Z

X

Y

,

,

X

Y

(1.1.15)

for

X ⊥⊥ Y

X ⊥⊥ Y| ∅

, in

If

X

and

Y

are not conditionally independent, they are conditionally dependent. This is written

X (cid:62)(cid:62)Y| Z

Similarly

X (cid:62)(cid:62)Y| ∅

can be written as

.

X(cid:62)(cid:62)Y

(1.1.16)

Intuitively, if x is conditionally independent of y given z, this means that, given z, y contains no additional
information about x. Similarly, given z, knowing x does not tell me anything more about y. Note that

X ⊥⊥ Y| Z ⇒ X

⊥⊥ Y

(cid:48)

(cid:48)
| Z

for

(cid:48)

X

⊆ X

and

(cid:48)

Y

⊆ Y

.

Remark 1.2 (Independence implications). It’s tempting to think that if a is independent of b and b is
independent of c then a must be independent of c:

a
{

⊥⊥

b, b

c
} ⇒

a

⊥⊥

⊥⊥

c

However, this does not follow. Consider for example a distribution of the form

p(a, b, c) = p(b)p(a, c)

From this

p(a, b) =

(cid:88)

c

p(a, b, c) = p(b)

(cid:88)

c

p(a, c)

(1.1.17)

(1.1.18)

(1.1.19)

Hence p(a, b) is a function of b multiplied by a function of a so that a and b are independent. Similarly, one
can show that b and c are independent. However, a is not necessarily independent of c since the distribution
p(a, c) can be set arbitrarily.

Similarly, it’s tempting to think that if a and b are dependent, and b and c are dependent, then a and c
must be dependent:

a
(cid:62)(cid:62)
{

b, b

c
} ⇒
(cid:62)(cid:62)

c
a
(cid:62)(cid:62)

(1.1.20)

However, this also does not follow. We give an explicit numerical example in exercise(3.17).

Finally, note that conditional independence x
exercise(3.20).

y
|

⊥⊥

z does not imply marginal independence x

y. See also

⊥⊥

DRAFT April 24, 2015

11

Probabilistic Reasoning

1.1.2 Probability Tables

Based on the populations 60776238, 5116900 and 2980700 of England (E), Scotland (S) and Wales (W),
the a priori probability that a randomly selected person from the combined three countries would live in
England, Scotland or Wales, is approximately 0.88, 0.08 and 0.04 respectively. We can write this as a vector
(or probability table) :





p(Cnt = E)
p(Cnt = S)
p(Cnt = W)





 =







0.88
0.08
0.04

(1.1.21)

whose component values sum to 1. The ordering of the components in this vector is arbitrary, as long as it
is consistently applied.

For the sake of simplicity, we assume that only three Mother Tongue languages exist : English (Eng),
Scottish (Scot) and Welsh (Wel), with conditional probabilities given the country of residence, England (E),
Scotland (S) and Wales (W). We write a (ﬁctitious) conditional probability table

Cnt = W) = 0.6
p(M T = Eng
Cnt = S) = 0.7
p(M T = Eng
Cnt = E) = 0.95
p(M T = Eng
|
|
|
Cnt = S) = 0.3 p(M T = Scot
Cnt = E) = 0.04 p(M T = Scot
p(M T = Scot
Cnt = W) = 0.0
|
|
|
Cnt = W) = 0.4
p(M T = Wel
Cnt = S) = 0.0
p(M T = Wel
Cnt = E) = 0.01
p(M T = Wel
|
|
|

(1.1.22)

From this we can form a joint distribution p(Cnt, M T ) = p(M T
3

3 matrix with columns indexed by country and rows indexed by Mother Tongue:

|

Cnt)p(Cnt). This could be written as a

×





0.95
0.04
0.01

×
×
×

0.88 0.7
0.88 0.3
0.88 0.0

0.08 0.6
0.08 0.0
0.08 0.4

0.04
0.04
0.04

×
×
×

×
×
×





 =



0.056 0.024

0.836
0.0352 0.024
0.0088

0

0
0.016





(1.1.23)

The joint distribution contains all the information about the model of this environment. By summing the
columns of this table, we have the marginal p(Cnt). Summing the rows gives the marginal p(M T ). Similarly,
one could easily infer p(Cnt
Cnt)p(Cnt) from this joint distribution by dividing an entry of
M T )
|
|
equation (1.1.23) by its row sum.

p(M T

∝

For joint distributions over a larger number of variables, xi, i = 1, . . . , D, with each variable xi taking Ki
states, the table describing the joint distribution is an array with (cid:81)D
i=1 Ki entries. Explicitly storing tables
therefore requires space exponential in the number of variables, which rapidly becomes impractical for a
large number of variables. We discuss how to deal with this issue in chapter(3) and chapter(4).

A probability distribution assigns a value to each of the joint states of the variables. For this reason,
p(T, J, R, S) is considered equivalent to p(J, S, R, T ) (or any such reordering of the variables), since in each
case the joint setting of the variables is simply a diﬀerent index to the same probability. This situation is
more clear in the set theoretic notation p(J
R). We abbreviate this set theoretic notation by using
the commas – however, one should be careful not to confuse the use of this indexing type notation with
functions f (x, y) which are in general dependent on the variable order. Whilst the variables to the left of the
conditioning bar may be written in any order, and equally those to the right of the conditioning bar may be
x1).
written in any order, moving variables across the bar is not generally equivalent, so that p(x1
|

= p(x2

x2)

∩

∩

∩

S

T

|

1.2 Probabilistic Reasoning

The central paradigm of probabilistic reasoning is to identify all relevant variables x1, . . . , xN in the envi-
ronment, and make a probabilistic model p(x1, . . . , xN ) of their interaction. Reasoning (inference) is then
performed by introducing evidence that sets variables in known states, and subsequently computing proba-
bilities of interest, conditioned on this evidence. The rules of probability, combined with Bayes’ rule make
for a complete reasoning system, one which includes traditional deductive logic as a special case[158]. In
the examples below, the number of variables in the environment is very small. In chapter(3) we will discuss

12

DRAFT April 24, 2015

(cid:54)
Probabilistic Reasoning

reasoning in networks containing many variables, for which the graphical notations of chapter(2) will play
a central role.

Example 1.2 (Hamburgers). Consider the following ﬁctitious scientiﬁc information: Doctors ﬁnd that
people with Kreuzfeld-Jacob disease (KJ) almost invariably ate hamburgers, thus p(Hamburger Eater
KJ ) =
|
0.9. The probability of an individual having KJ is currently rather low, about one in 100,000.

1. Assuming eating lots of hamburgers is rather widespread, say p(Hamburger Eater) = 0.5, what is the

probability that a hamburger eater will have Kreuzfeld-Jacob disease?

This may be computed as

p(KJ

Hamburger Eater) =
|

p(Hamburger Eater, KJ )
p(Hamburger Eater)

=

KJ )p(KJ )
p(Hamburger Eater
|

p(Hamburger Eater)

9
10 ×

=

1
100000
1
2

= 1.8

×

10−5

(1.2.1)

(1.2.2)

2. If the fraction of people eating hamburgers was rather small, p(Hamburger Eater) = 0.001, what is the
probability that a regular hamburger eater will have Kreuzfeld-Jacob disease? Repeating the above
calculation, this is given by

9
10 ×

1
100000
1
1000

1/100

≈

(1.2.3)

This is much higher than in scenario (1) since here we can be more sure that eating hamburgers is
related to the illness.

Example 1.3 (Inspector Clouseau). Inspector Clouseau arrives at the scene of a crime. The victim lies dead
in the room alongside the possible murder weapon, a knife. The Butler (B) and Maid (M ) are the inspector’s
main suspects and the inspector has a prior belief of 0.6 that the Butler is the murderer, and a prior belief
of 0.2 that the Maid is the murderer. These beliefs are independent in the sense that p(B, M ) = p(B)p(M ).
(It is possible that both the Butler and the Maid murdered the victim or neither). The inspector’s prior
criminal knowledge can be formulated mathematically as follows:

dom(B) = dom(M ) =

murderer, not murderer
{

}

, dom(K) =

knife used, knife not used
}
{

(1.2.4)

p(B = murderer) = 0.6,

p(M = murderer) = 0.2

p(knife used
p(knife used
p(knife used
p(knife used

B = not murderer, M = not murderer) = 0.3
|
B = not murderer, M = murderer)
= 0.2
|
M = not murderer) = 0.6
B = murderer,
|
M = murderer)
B = murderer,
= 0.1
|

(1.2.5)

(1.2.6)

In addition p(K, B, M ) = p(K
B, M )p(B)p(M ). Assuming that the knife is the murder weapon, what is
|
the probability that the Butler is the murderer? (Remember that it might be that neither is the murderer).
Using b for the two states of B and m for the two states of M ,

K) =

p(B

|

(cid:88)

m

K) =
p(B, m
|

(cid:88)

m

p(B, m, K)
p(K)

=

DRAFT April 24, 2015

(cid:80)
(cid:80)

m p(K
|
m,b p(K

B, m)p(B, m)
b, m)p(b, m)
|

=

p(B) (cid:80)
b p(b) (cid:80)
(cid:80)

m p(K

B, m)p(m)
|
b, m)p(m)
m p(K
|

(1.2.7)

13

where we used the fact that in our model p(B, M ) = p(B)p(M ). Plugging in the values we have (see also
demoClouseau.m)

Probabilistic Reasoning

p(B = murderer
|

knife used) =

6
10

(cid:0) 2
10 ×

6
10

(cid:0) 2
10 ×
6
10

10 ×

6
10

1
10 + 8
10 ×
(cid:0) 2
(cid:1) + 4
10 ×
10

(cid:1)
10 + 8

2

10 ×

(cid:1) =

3
10

300
412 ≈

0.73

(1.2.8)

1

10 + 8

Hence knowing that the knife was the murder weapon strengthens our belief that the butler did it.

Remark 1.3. The role of p(knife used) in the Inspector Clouseau example can cause some confusion. In
the above,

p(knife used) =

(cid:88)

p(b)

b

(cid:88)

m

p(knife used

b, m)p(m)
|

(1.2.9)

0.412. But surely, p(knife used) = 1, since this is given in the question! Note that the
is computed to be
quantity p(knife used) relates to the prior probability the model assigns to the knife being used (in the
absence of any other information). If we know that the knife is used, then the posterior

@@

p(knife used

knife used) =
|

p(knife used, knife used)
p(knife used)

=

p(knife used)
p(knife used)

= 1

(1.2.10)

which, naturally, must be the case.

Example 1.4 (Who’s in the bathroom?). Consider a household of three people, Alice, Bob and Cecil.
Cecil wants to go to the bathroom but ﬁnds it occupied. He then goes to Alice’s room and sees she is there.
Since Cecil knows that only either Alice or Bob can be in the bathroom, from this he infers that Bob must
be in the bathroom.

To arrive at the same conclusion in a mathematical framework, we deﬁne the following events

A = Alice is in her bedroom, B = Bob is in his bedroom, O = Bathroom occupied

(1.2.11)

We can encode the information that if either Alice or Bob are not in their bedrooms, then they must be in
the bathroom (they might both be in the bathroom) as

p(O = tr

A = fa, B) = 1,
|

p(O = tr
|

A, B = fa) = 1

(1.2.12)

The ﬁrst term expresses that the bathroom is occupied if Alice is not in her bedroom, wherever Bob is.
Similarly, the second term expresses bathroom occupancy as long as Bob is not in his bedroom. Then

O = tr, A = tr) =
p(B = fa
|

p(B = fa, O = tr, A = tr)
p(O = tr, A = tr)

p(O = tr
|

=

A = tr, B = fa)p(A = tr, B = fa)

p(O = tr, A = tr)

(1.2.13)

where

p(O = tr, A = tr) = p(O = tr

A = tr, B = fa)p(A = tr, B = fa)
|

+ p(O = tr

A = tr, B = tr)p(A = tr, B = tr)
|

(1.2.14)

A = tr, B = tr) = 0, which encodes that if Alice
Using the fact p(O = tr
|
is in her room and Bob is not, the bathroom must be occupied, and similarly, if both Alice and Bob are in
their rooms, the bathroom cannot be occupied,

A = tr, B = fa) = 1 and p(O = tr
|

O = tr, A = tr) =
p(B = fa
|

p(A = tr, B = fa)
p(A = tr, B = fa)

= 1

(1.2.15)

This example is interesting since we are not required to make a full probabilistic model in this case thanks
to the limiting nature of the probabilities (we don’t need to specify p(A, B)). The situation is common in
limiting situations of probabilities being either 0 or 1, corresponding to traditional logic systems.

14

DRAFT April 24, 2015

Probabilistic Reasoning

@@

Example 1.5 (Aristotle : Modus Ponens). According to logic, the statements ‘All apples are fruit’ and
‘All fruits grow on trees’ lead to the conclusion that ‘All apples grow on trees’. This kind of reasoning is a
form of transitivity : from the statements A

T we can infer A

F and F

T .

⇒

⇒

⇒

To see how this might be deduced using Bayesian, we assume that ‘All apples are fruit’ corresponds to
F = tr) = 1. We then want to
p(F = tr
A = tr) = 0
show that this implies p(T = tr
which (assuming p(A = tr) > 0) is in turn equivalent to showing that p(T = fa, A = tr) = 0. Consider

A = tr) = 1 and ‘All fruit grows on trees’ corresponds to p(T = tr
|
|

A = tr) = 1. Showing this is equivalent to showing p(T = fa
|
|

p(T = fa, A = tr) = p(T = fa, A = tr, F = tr) + p(T = fa, A = tr, F = fa)

(1.2.16)

We can show that both terms on the right are zero. First, consider

p(T = fa, A = tr, F = tr)

≤

F = tr) = 1
This is zero since, by assumption, p(T = fa
|

F = tr)p(F = tr)
p(T = fa, F = tr) = p(T = fa
|
p(T = tr
|

F = tr) = 1

−

1 = 0. Similarly,

−

p(T = fa, A = tr, F = fa)

A = tr)p(A = tr)
p(A = tr, F = fa) = p(F = fa
|

≤

A = tr) = 0.
where again, by assumption, p(F = fa
|

(1.2.17)

(1.2.18)

Example 1.6 (Aristotle : Inverse Modus Ponens). According to Logic, from the statement : ‘If A is true
then B is true’, one may deduce that ‘if B is false then A is false’. To see how this ﬁts in with a probabilistic
A = tr) = 1.
reasoning system we can ﬁrst express the statement : ‘If A is true then B is true’ as p(B = tr
|
Then we may infer

B = fa) = 1
p(A = fa
|

−

p(A = tr

B = fa)
|

A = tr)p(A = tr)
p(B = fa
|
A = tr)p(A = tr) + p(B = fa
p(B = fa
|
|
A = tr) = 1
This follows since p(B = fa
|

A = tr) = 1
|

p(B = tr

= 1

−

−

−

A = fa)p(A = fa)

= 1

(1.2.19)

1 = 0, annihilating the second term.

Both the above examples are intuitive expressions of deductive logic. The standard rules of Aristotelian
logic are therefore seen to be limiting cases of probabilistic reasoning.

Example 1.7 (Soft XOR Gate).
A standard XOR logic gate is given by the table on the right. If we
observe that the output of the XOR gate is 0, what can we say about
A and B? In this case, either A and B were both 0, or A and B were
both 1. This means we don’t know which state A was in – it could
equally likely have been 1 or 0.

A B A xor B
0
0
1
1

0
1
0
1

0
1
1
0

DRAFT April 24, 2015

15

Probabilistic Reasoning

Consider a ‘soft’ version of the XOR gate given on the right,
so that the gate stochastically outputs C = 1 depending on its
inputs, with additionally A
B and p(A = 1) = 0.65, p(B =
⊥⊥
C = 0)?
1) = 0.77. What is p(A = 1
|

A, B)

A B p(C = 1
|
0.1
0
0.99
0
0.8
1
0.25
1

0
1
0
1

++

p(A = 1, C = 0) =

(cid:88)

B

p(A = 1, B, C = 0) =

(cid:88)

B

p(C = 0
|

A = 1, B)p(A = 1)p(B)

A = 1, B = 1)p(B = 1))
A = 1, B = 0)p(B = 0) + p(C = 0
= p(A = 1) (p(C = 0
|
|
0.23 + 0.75

0.77) = 0.405275

= 0.65

(0.2

(1.2.20)

×

×

×

×

p(A = 0, C = 0) =

(cid:88)

B

p(A = 0, B, C = 0) =

(cid:88)

B

p(C = 0
|

A = 0, B)p(A = 0)p(B)

A = 0, B = 1)p(B = 1))
A = 0, B = 0)p(B = 0) + p(C = 0
= p(A = 0) (p(C = 0
|
|
0.23 + 0.01

0.77) = 0.075145

= 0.35

(0.9

×

×

Then

C = 0) =
p(A = 1
|

p(A = 1, C = 0)
p(A = 1, C = 0) + p(A = 0, C = 0)

=

0.405275
0.405275 + 0.075145

= 0.8436

(1.2.21)

Example 1.8 (Larry). Larry is typically late for school. If Larry is late, we denote this with L = late,
otherwise, L = not late. When his mother asks whether or not he was late for school he never admits to
being late. The response Larry gives RL is represented as follows

p(RL = not late

L = not late) = 1,
|

L = late) = 0
p(RL = late
|

The remaining two values are determined by normalisation and are

L = not late) = 0,
p(RL = late
|

p(RL = not late

L = late) = 1
|

Given that RL = not late, what is the probability that Larry was late, i.e. p(L = late
|

(1.2.22)

(1.2.23)

RL = not late)?

Using Bayes’ we have

p(L = late

RL = not late) =
|

p(L = late, RL = not late)
p(RL = not late)

In the above

=

p(L = late, RL = not late)
p(L = late, RL = not late) + p(L = not late, RL = not late)

p(L = late, RL = not late) = p(RL = not late

(cid:124)

(cid:123)(cid:122)
=1

L = late)
|
(cid:125)

p(L = late)

(1.2.24)

(1.2.25)

and

p(L = not late, RL = not late) = p(RL = not late
|
(cid:123)(cid:122)
=1

(cid:124)

L = not late)
(cid:125)

p(L = not late)

(1.2.26)

Hence

p(L = late
RL = not late) =
|

p(L = late)
p(L = late) + p(L = not late)

= p(L = late)

(1.2.27)

Where we used normalisation in the last step, p(L = late) + p(L = not late) = 1. This result is intuitive –
Larry’s mother knows that he never admits to being late, so her belief about whether or not he really was
late is unchanged, regardless of what Larry actually says.

16

DRAFT April 24, 2015

Probabilistic Reasoning

Example 1.9 (Larry and Sue). Continuing the example above, Larry’s sister Sue always tells the truth to
her mother as to whether or not Larry was late for School.

p(RS = not late

L = not late) = 1,
|

p(RS = late

L = late) = 1
|

The remaining two values are determined by normalisation and are

L = not late) = 0,
p(RS = late
|

p(RS = not late

L = late) = 0
|

We also assume p(RS, RL

L) = p(RS
|

|

L)p(RL

L). We can then write
|

p(RL, RS, L) = p(RL

L)p(RS
|

L)p(L)
|

Given that RS = late and RL = not late, what is the probability that Larry was late?

Using Bayes’ rule, we have

(1.2.28)

(1.2.29)

(1.2.30)

RL = not late,RS = late)
p(L = late
|

=

1
Z

L = late)p(RL = not late
p(RS = late
|

L = late)p(L = late)
|

(1.2.31)

where the normalisation Z is given by

L = late)p(RL = not late
p(RS = late
|
L = not late)p(RL = not late
+ p(RS = late
|

L = late)p(L = late)

|

L = not late)p(L = not late)
|

(1.2.32)

Hence

RL = not late, RS = late) =
p(L = late
|

1

1

×

×

1

1

×
p(L = late) + 0

×

p(L = late)
1

×

×

p(L = not late)

= 1

(1.2.33)

This result is also intuitive – Since Larry’s mother knows that Sue always tells the truth, no matter what
Larry says, she knows he was late.

Example 1.10 (Luke). Luke has been told he’s lucky and has won a prize in the lottery. There are 5
prizes available of value £10, £100, £1000, £10000, £1000000. The prior probabilities of winning these 5
prizes are p1, p2, p3, p4, p5, with p0 being the prior probability of winning no prize. Luke asks eagerly ‘Did I
win £1000000?!’.
‘Did I win £10000?!’
asks Luke. ‘Again, I’m afraid not sir’. What is the probability that Luke has won £1000?

‘I’m afraid not sir’, is the response of the lottery phone operator.

Note ﬁrst that p0 + p1 + p2 + p3 + p4 + p5 = 1. We denote W = 1 for the ﬁrst prize of £10, and W = 2, . . . , 5
for the remaining prizes and W = 0 for no prize. We need to compute

p(W = 3
|

W

= 5, W

= 4, W

= 0) =

p(W = 3, W
p(W

= 5, W

= 4, W

= 0)

= 5, W

= 4, W

= 0)

=

p(W = 3)
p(W = 1 or W = 2 or W = 3)

=

p3
p1 + p2 + p3

(1.2.34)

where the term in the denominator is computed using the fact that the events W are mutually exclusive
(one can only win one prize). This result makes intuitive sense : once we have removed the impossible states
of W , the probability that Luke wins the prize is proportional to the prior probability of that prize, with
the normalisation being simply the total set of possible probability remaining.

DRAFT April 24, 2015

17

(cid:54)
(cid:54)
(cid:54)
(cid:54)
(cid:54)
(cid:54)
(cid:54)
(cid:54)
(cid:54)
Prior, Likelihood and Posterior

(a)

(b)

(c)

Figure 1.1:
on 5 possible values of θ. (c): The posterior belief on θ.

(a): Noisy observations of displacements x1, . . . , x100 for a pendulum.

(b): The prior belief

1.3 Prior, Likelihood and Posterior

Much of science deals with problems of the form : tell me something about the variable θ given that I have
and have some knowledge of the underlying data generating mechanism. Our interest is
observed data
then the quantity

D

p(θ

|D

) =

p(

θ)p(θ)
)

D|
p(

D

=

(cid:82)

p(
D|
θ p(

D|

θ)p(θ)

θ)p(θ)

(1.3.1)

θ) of the dataset, and coupled with a prior belief
This shows how from a forward or generative model p(
p(θ) about which variable values are appropriate, we can infer the posterior distribution p(θ
) of the vari-
able in light of the observed data. The most probable a posteriori (MAP ) setting is that which maximises
). For a ‘ﬂat prior’, p(θ) being a constant, not changing with θ, the MAP
the posterior, θ∗ = arg maxθ p(θ
solution is equivalent to the maximum likelihood , namely that θ that maximises the likelihood p(
θ) of the
model generating the observed data. We will return to a discussion of such summaries of the posterior and
parameter learning in chapter(9).

D|

D|

|D

|D

This use of a generative model sits well with physical models of the world which typically postulate how
to generate observed phenomena, assuming we know the model. For example, one might postulate how
to generate a time-series of displacements for a swinging pendulum but with unknown mass, length and
damping constant. Using this generative model, and given only the displacements, we could infer the
unknown physical properties of the pendulum.

Example 1.11 (Pendulum). As a prelude to scientiﬁc inference and the use of continuous variables, we
consider an idealised pendulum for which xt is the angular displacement of the pendulum at time t. Assuming
that the measurements are independent, given the knowledge of the parameter of the problem, θ, we have
that the likelihood of a sequence of observations x1, . . . , xT is given by

p(x1, . . . , xT

θ) =
|

T
(cid:89)

t=1

p(xt

θ)
|

(1.3.2)

If the model is correct and our measurement of the displacements x is perfect, then the physical model is

xt = sin(θt)

(1.3.3)

g/L, where g is the gravitational
where θ represents the unknown physical constants of the pendulum (
attraction and L the length of the pendulum). If, however, we assume that we have a rather poor instrument
to measure the displacements, with a known variance of σ2 (see chapter(8)), then

(cid:112)

xt = sin(θt) + (cid:15)t

18

(1.3.4)

DRAFT April 24, 2015

020406080100−4−2024−0.100.10.20.30.40.50.600.050.10.150.20.250.30.35−0.100.10.20.30.40.50.600.20.40.60.81Prior, Likelihood and Posterior

where (cid:15)t is zero mean Gaussian noise with variance σ2. We can also consider a set of possible parameters
θ and place a prior p(θ) over them, expressing our prior belief (before seeing the measurements) in the
appropriateness of the diﬀerent values of θ. The posterior distribution is then given by

x1, . . . , xT )

p(θ

|

p(θ)

∝

T
(cid:89)

t=1

1
√2πσ2

e− 1

2σ2 (xt−sin(θt))2

(1.3.5)

Despite noisy measurements, the posterior over the assumed possible values for θ becomes strongly peaked
for a large number of measurements, see ﬁg(1.1).

1.3.1 Two dice : what were the individual scores?

Two fair dice are rolled. Someone tells you that the sum of the two scores is 9. What is the posterior
distribution of the dice scores2?
The score of die a is denoted sa with dom(sa) =
and similarly for sb. The three variables
involved are then sa, sb and the total score, t = sa + sb. A model of these three variables naturally takes
the form

1, 2, 3, 4, 5, 6
}

{

sa, sb)
p(t, sa, sb) = p(t
|
(cid:124)
(cid:123)(cid:122)
(cid:125)
likelihood

p(sa, sb)
(cid:124) (cid:123)(cid:122) (cid:125)
prior

The prior p(sa, sb) is the joint probability of score sa
and score sb without knowing anything else. Assuming
no dependency in the rolling mechanism,

p(sa, sb) = p(sa)p(sb)

(1.3.7)

Since the dice are fair both p(sa) and p(sb) are uniform
distributions, p(sa) = p(sb) = 1/6.

Here the likelihood term is

p(t

sa, sb) = I [t = sa + sb]
|

(1.3.8)

which states that the total score is given by sa + sb.
Here I [A] is the indicator function deﬁned as I [A] = 1
if the statement A is true and 0 otherwise.

Hence, our complete model is

sa, sb)p(sa)p(sb)
p(t, sa, sb) = p(t
|

(1.3.9)

where the terms on the right are explicitly deﬁned.

The posterior is then given by,

p(sa, sb

t = 9) =
|

p(t = 9
|

sa, sb)p(sa)p(sb)
p(t = 9)

(1.3.10)

where

p(t = 9) =

(cid:88)

sa,sb

p(t = 9
|

sa, sb)p(sa)p(sb) (1.3.11)

The term p(t = 9) = (cid:80)
mass in only 4 non-zero elements, as shown.

sa, sb)p(sa)p(sb) = 4
p(t = 9
|

sa,sb

2This example is due to Taylan Cemgil.

DRAFT April 24, 2015

(1.3.6)

sa = 5
1/36
1/36
1/36
1/36
1/36
1/36

sa = 5
0
0
0
1
0
0

sa = 6
1/36
1/36
1/36
1/36
1/36
1/36

sa = 6
0
0
1
0
0
0

p(sa)p(sb):
sa = 3
1/36
1/36
1/36
1/36
1/36
1/36

sa = 2
1/36
1/36
1/36
1/36
1/36
1/36

sa = 4
1/36
1/36
1/36
1/36
1/36
1/36

sa, sb):
p(t = 9
|
sa = 3
sa = 2
0
0
0
0
0
0
0
0
0
0
1
0

sa = 4
0
0
0
0
1
0

sa = 1
1/36
1/36
1/36
1/36
1/36
1/36

sa = 1
0
0
0
0
0
0

p(t = 9
|
sa = 2
0
0
0
0
0
0

sa = 1
0
0
0
0
0
0

sa, sb)p(sa)p(sb):

sa = 3
0
0
0
0
0
1/36

sa = 4
0
0
0
0
1/36
0

sa = 5
0
0
0
1/36
0
0

sa = 6
0
0
1/36
0
0
0

sb = 1
sb = 2
sb = 3
sb = 4
sb = 5
sb = 6

sb = 1
sb = 2
sb = 3
sb = 4
sb = 5
sb = 6

sb = 1
sb = 2
sb = 3
sb = 4
sb = 5
sb = 6

sa = 1
0
0
0
0
0
0

sb = 1
sb = 2
sb = 3
sb = 4
sb = 5
sb = 6

p(sa, sb
sa = 2
0
0
0
0
0
0

t = 9):
|
sa = 3
0
0
0
0
0
1/4

sa = 4
0
0
0
0
1/4
0

sa = 5
0
0
0
1/4
0
0

sa = 6
0
0
1/4
0
0
0

1/36 = 1/9. Hence the posterior is given by equal

×

19

Code

1.4 Summary

The standard rules of probability are a consistent, logical way to reason with uncertainty.

Bayes’ rule mathematically encodes the process of inference.

•

•

A useful introduction to probability is given in [292]. The interpretation of probability is contentious and
we refer the reader to [158, 197, 193] for detailed discussions. The website understandinguncertainty.org
contains entertaining discussions on reasoning with uncertainty.

1.5 Code

The BRMLtoolbox code accompanying this book is intended to give the reader some insight into repre-
senting discrete probability tables and performing simple inference. We provide here only the briefest of
descriptions of the code and the reader is encouraged to experiment with the demos to understand better
the routines and their purposes.

1.5.1 Basic Probability code

At the simplest level, we only need two basic routines. One for multiplying probability tables together
(called potentials in the code), and one for summing a probability table. Potentials are represented using a
structure. For example, in the code corresponding to the Inspector Clouseau example demoClouseau.m, we
deﬁne a probability table as

>> pot(1)
ans =

variables: [1 3 2]

table: [2x2x2 double]

This says that the potential depends on the variables 1, 3, 2 and the entries are stored in the array given
by the table ﬁeld. The size of the array informs how many states each variable takes in the order given by
variables. The order in which the variables are deﬁned in a potential is irrelevant provided that one indexes
the array consistently. A routine that can help with setting table entries is setstate.m. For example,

>> pot(1) = setstate(pot(1),[2 1 3],[2 1 1],0.3)

means that for potential 1, the table entry for variable 2 being in state 2, variable 1 being in state 1 and
variable 3 being in state 1 should be set to value 0.3.

The philosophy of the code is to keep the information required to perform computations to a minimum.
Additional information about the labels of variables and their domains can be useful to interpret results,
but is not actually required to carry out computations. One may also specify the name and domain of each
variable, for example

>>variable(3)
ans =

domain: {’murderer’
name: ’butler’

’not murderer’}

The variable name and domain information in the Clouseau example is stored in the structure variable,
which can be helpful to display the potential table:

>> disptable(pot(1),variable);
knife
knife
knife

used
not used
used

maid
maid
maid

=
=
=

= murderer
= murderer
= not murderer

butler
butler
butler

=
=
=

murderer
murderer
murderer

0.100000
0.900000
0.600000

20

DRAFT April 24, 2015

Code

knife
knife
knife
knife
knife

=
=
=
=
=

not used
used
not used
used
not used

maid
maid
maid
maid
maid

= not murderer
= murderer
= murderer
= not murderer
= not murderer

butler
butler
butler
butler
butler

=
=
=
=
=

murderer
not murderer
not murderer
not murderer
not murderer

0.400000
0.200000
0.800000
0.300000
0.700000

Multiplying Potentials

In order to multiply potentials, (as for arrays) the tables of each potential must be dimensionally consistent
– that is the number of states of variable i must be the same for all potentials. This can be checked us-
ing potvariables.m. This consistency is also required for other basic operations such as summing potentials.

multpots.m: Multiplying two or more potentials
divpots.m: Dividing a potential by another

Summing a Potential

sumpot.m: Sum (marginalise) a potential over a set of variables
sumpots.m: Sum a set of potentials together

Making a conditional Potential

condpot.m: Make a potential conditioned on variables

Setting a Potential

setpot.m: Set variables in a potential to given states
setevpot.m: Set variables in a potential to given states and return also an identity potential on the given
states

The philosophy of BRMLtoolbox is that all information about variables is local and is read oﬀ from a
potential. Using setevpot.m enables one to set variables in a state whilst maintaining information about
the number of states of a variable.

Maximising a Potential

maxpot.m: Maximise a potential over a set of variables
See also maxNarray.m and maxNpot.m which return the N -highest values and associated states.

Other potential utilities

setstate.m: Set a potential state to a given value
table.m: Return a table from a potential
whichpot.m: Return potentials which contain a set of variables
potvariables.m: Variables and their number of states in a set of potentials
orderpotfields.m: Order the ﬁelds of a potential structure
uniquepots.m: Merge redundant potentials by multiplication and return only unique ones
numstates.m: Number of states of a variable in a domain
squeezepots.m: Find unique potentials and rename the variables 1,2,...
normpot.m: Normalise a potential to form a distribution

1.5.2 General utilities

y) from p(x, y)
|

condp.m: Return a table p(x
condexp.m: Form a conditional distribution from a log value
logsumexp.m: Compute the log of a sum of exponentials in a numerically precise way
normp.m: Return a normalised table from an unnormalised table
assign.m: Assign values to multiple variables
maxarray.m: Maximize a multi-dimensional array over a subset

DRAFT April 24, 2015

21

1.5.3 An example

The following code highlights the use of the above routines in solving the Inspector Clouseau, example(1.3),
and the reader is invited to examine the code to become familiar with how to numerically represent proba-
bility tables.
demoClouseau.m: Solving the Inspector Clouseau example

Exercises

1.6 Exercises

Exercise 1.1. Prove

p(x, y

z) = p(x
|

z)p(y
|

x, z)
|

and also

p(y

p(x

y, z) =
|

z)
|

x, z)p(x
|
p(y

z)

|

Exercise 1.2. Prove the Bonferroni inequality

p(a, b)

≥

p(a) + p(b)

1

−

(1.6.1)

(1.6.2)

(1.6.3)

Exercise 1.3 (Adapted from [181]). There are two boxes. Box 1 contains three red and ﬁve white balls and
box 2 contains two red and ﬁve white balls. A box is chosen at random p(box = 1) = p(box = 2) = 0.5 and
a ball chosen at random from this box turns out to be red. What is the posterior probability that the red ball
came from box 1?

Exercise 1.4 (Adapted from [181]). Two balls are placed in a box as follows: A fair coin is tossed and a
white ball is placed in the box if a head occurs, otherwise a red ball is placed in the box. The coin is tossed
again and a red ball is placed in the box if a tail occurs, otherwise a white ball is placed in the box. Balls
are drawn from the box three times in succession (always with replacing the drawn ball back in the box). It
is found that on all three occasions a red ball is drawn. What is the probability that both balls in the box are
red?

A secret government agency has developed a scanner which determines whether a person is a terrorist. The
scanner is fairly reliable; 95% of all scanned terrorists are identiﬁed as terrorists, and 95% of all upstanding
citizens are identiﬁed as such. An informant tells the agency that exactly one passenger of 100 aboard an
The police haul oﬀ the plane the ﬁrst person for which the
aeroplane in which you are seated is a terrorist.
scanner tests positive. What is the probability that this person is a terrorist?

@@

Exercise 1.6. Consider three variable distributions which admit the factorisation

p(a, b, c) = p(a
|

c)p(c)
b)p(b
|

(1.6.4)

where all variables are binary. How many parameters are needed to specify distributions of this form?

Exercise 1.7. Repeat the Inspector Clouseau scenario, example(1.3), but with the restriction that either the
maid or the butler is the murderer, but not both. Explicitly, the probability of the maid being the murderer
and not the butler is 0.04, the probability of the butler being the murderer and not the maid is 0.64. Modify
demoClouseau.m to implement this.

Exercise 1.8. Prove

p(a, (b or c)) = p(a, b) + p(a, c)

p(a, b, c)

−

Exercise 1.9. Prove

p(x

z) =
|

(cid:88)

y

p(x
|

y, z)p(y

z) =
|

(cid:88)

y,w

p(x
|

w, y, z)p(w

y, z)p(y

z)

|

|

(1.6.5)

(1.6.6)

22

DRAFT April 24, 2015

Exercises

Exercise 1.10. As a young man Mr Gott visits Berlin in 1969. He’s surprised that he cannot cross into
East Berlin since there is a wall separating the two halves of the city. He’s told that the wall was erected 8
years previously. He reasons that : The wall will have a ﬁnite lifespan; his ignorance means that he arrives
uniformly at random at some time in the lifespan of the wall. Since only 5% of the time one would arrive
in the ﬁrst or last 2.5% of the lifespan of the wall he asserts that with 95% conﬁdence the wall will survive
between 8/0.975
8.2 and 8/0.025 = 320 years. In 1989 the now Professor Gott is pleased to ﬁnd that
his prediction was correct and promotes his prediction method in prestigious journals. This ‘delta-t’ method
is widely adopted and used to form predictions in a range of scenarios about which researchers are ‘totally
ignorant’. Would you ‘buy’ a prediction from Prof. Gott? Explain carefully your reasoning.

≈

Exercise 1.11. Implement the soft XOR gate, example(1.7) using BRMLtoolbox. You may ﬁnd condpot.m
of use.

Exercise 1.12. Implement the hamburgers, example(1.2) (both scenarios) using BRMLtoolbox. To do so
you will need to deﬁne the joint distribution p(hamburgers, KJ) in which dom(hamburgers) = dom(KJ) =
.
tr, fa
}

{
Exercise 1.13. Implement the two-dice example, section(1.3.1) using BRMLtoolbox.

Exercise 1.14. A redistribution lottery involves picking the correct four numbers from 1 to 9 (without
replacement, so 3,4,4,1 for example is not possible). The order of the picked numbers is irrelevant. Every
week a million people play this game, each paying £1 to enter, with the numbers 3,5,7,9 being the most
popular (1 in every 100 people chooses these numbers). Given that the million pounds prize money is split
equally between winners, and that any four (diﬀerent) numbers come up at random, what is the expected
amount of money each of the players choosing 3,5,7,9 will win each week? The least popular set of numbers
is 1,2,3,4 with only 1 in 10,000 people choosing this. How much do they proﬁt each week, on average? Do
you think there is any ‘skill’ involved in playing this lottery?

Exercise 1.15. In a test of ‘psychometry’ the car keys and wrist watches of 5 people are given to a medium.
The medium then attempts to match the wrist watch with the car key of each person. What is the expected
number of correct matches that the medium will make (by chance)? What is the probability that the medium
will obtain at least 1 correct match?

Exercise 1.16.

1. Show that for any function f

(cid:88)

x

p(x

y)f (y) = f (y)
|

2. Explain why, in general,

(cid:88)

x

p(x

y)f (x, y)
|

=

(cid:88)

x

f (x, y)

(1.6.7)

(1.6.8)

Exercise 1.17 (Inspired by singingbanana.com). Seven friends decide to order pizzas by telephone from
Pizza4U based on a ﬂyer pushed through their letterbox. Pizza4U has only 4 kinds of pizza, and each person
chooses a pizza independently. Bob phones Pizza4U and places the combined pizza order, simply stating
how many pizzas of each kind are required. Unfortunately, the precise order is lost, so the chef makes seven
randomly chosen pizzas and then passes them to the delivery boy.

1. How many diﬀerent combined orders are possible?

2. What is the probability that the delivery boy has the right order?

Exercise 1.18. Sally is new to the area and listens to some friends discussing about another female friend.
Sally knows that they are talking about either Alice or Bella but doesn’t know which. From previous conver-
sations Sally knows some independent pieces of information: She’s 90% sure that Alice has a white car, but
doesn’t know if Bella’s car is white or black. Similarly, she’s 90% sure that Bella likes sushi, but doesn’t know
if Alice likes sushi. Sally hears from the conversation that the person being discussed hates sushi and drives
Assume maximal uncertainty
a white car. What is the probability that the friends are talking about Alice?
in the absence of any knowledge of the probabilities.

@@

DRAFT April 24, 2015

23

(cid:54)
Exercises

Exercise 1.19. The weather in London can be summarised as: if it rains one day there’s a 70% chance it
will rain the following day; if it’s sunny one day there’s a 40% chance it will be sunny the following day.

1. Assuming that the prior probability it rained yesterday is 0.5, what is the probability that it was raining

yesterday given that it’s sunny today?

2. If the weather follows the same pattern as above, day after day, what is the probability that it will rain

on any day (based on an eﬀectively inﬁnite number of days of observing the weather)?

3. Use the result from part 2 above as a new prior probability of rain yesterday and recompute the proba-

bility that it was raining yesterday given that it’s sunny today.

A game of Battleships is played on a 10

Exercise 1.20.
10 pixel grid. There are two 5-pixel length
ships placed uniformly at random on the grid, subject to the constraints that (i) the ships cannot over-
lap and (ii) one ship is vertical and the other horizontal. After 10 unsuccessful ‘misses’ in locations
(1, 10), (2, 2), (3, 8), (4, 4), (5, 6), (6, 5), (7, 4), (7, 7), (9, 2), (9, 9) calculate which pixel has the highest proba-
bility of containing a ship. State this pixel and the value of the highest probability.

×

++

24

DRAFT April 24, 2015

CHAPTER 2

Basic Graph Concepts

Often we have good reason to believe that one event aﬀects another, or conversely that some events are
independent. Incorporating such knowledge can produce models that are better speciﬁed and computation-
ally more eﬃcient. Graphs describe how objects are linked and provide a convenient picture for describing
related objects. We will ultimately introduce a graph structure among the variables of a probabilistic model
to produce a ‘graphical model’ that captures relations among the variables as well as their uncertainties. In
this chapter, we introduce the required basic concepts from graph theory.

2.1 Graphs

Deﬁnition 2.1 (Graph). A graph G consists of nodes (also called vertices) and edges (also called links)
between the nodes. Edges may be directed (they have an arrow in a single direction) or undirected. Edges
can also have associated weights. A graph with all edges directed is called a directed graph, and one with
all edges undirected is called an undirected graph.

A

D

A

D

B

C

B

C

E

E

An directed graph G consists of directed edges between nodes.

An undirected graph G consists of undirected edges between nodes.

Graphs with edge weights are often used to model networks and ﬂows along ‘pipes’, or distances between
cities, where each node represents a city. We will also make use of these concepts in chapter(5) and chap-
ter(28). Our main use of graphs though will be to endow them with a probabilistic interpretation and we
develop a connection between directed graphs and probability in chapter(3). Undirected graphs also play a
central role in modelling and reasoning with uncertainty. Essentially, two variables will be independent if
they are not linked by a path on the graph. We will discuss this in more detail when we consider Markov
networks in chapter(4).

Deﬁnition 2.2 (Path, ancestors, descendants). A path A
B from node A to node B is a sequence of
nodes that connects A to B. That is, a path is of the form A0, A1, . . . , An−1, An, with A0 = A and An = B

(cid:55)→

25

and each edge (Ak−1, Ak), k = 1, . . . , n being in the graph. A directed path is a sequence of nodes which
when we follow the direction of the arrows leads us from A to B. In directed graphs, the nodes A such that
A are the descendants
A
(cid:55)→
of A.

A are the ancestors of B. The nodes B such that A

B and B

B and B

(cid:55)→

(cid:55)→

(cid:55)→

Graphs

b

. . .

Deﬁnition 2.3 (Cycle, loop and chord). A cycle is a directed path that starts and returns to the same
a. A loop is a path containing more than two nodes, irrespective of edge direction,
node a
that starts and returns to the same node. For example in ﬁg(2.2b) 1
1 forms a loop, but the
graph is acyclic (contains no cycles). A chord is an edge that connects two non-adjacent nodes in a loop –
4
for example, the 2

3 edge is a chord in the 1

1 loop of ﬁg(2.2a).

→

→

→

→

−

−

−

−

2

3

3

2

4

z

−

−

−

−

−

Deﬁnition 2.4 (Directed Acyclic Graph (DAG)). A DAG is a graph G with directed edges (arrows on each
link) between the nodes such that by following a path of nodes from one node to another along the direction
of each edge no path will revisit a node. In a DAG the ancestors of B are those nodes who have a directed
path ending at B. Conversely, the descendants of A are those nodes who have a directed path starting at
A.

Deﬁnition 2.5 (Relationships in a DAG).

x1

x8

x2

x4

x3

x7

x5

x6

x5, x6

x1, x2, x3

. The children of x4 are
The parents of x4 are pa (x4) =
The family of a node is itself and its parents.
ch (x4) =
The Markov blanket of a node is its parents, children and the parents
of its children
(excluding itself). In this case, the Markov blanket of
x4 is x1, x2, x3, x5, x6, x7.

.
}

{

}

{

@@

DAGs will play a central role in modelling environments with many variables, in particular they are used
for the belief networks that we describe in the following chapter. One can view the directed links on a graph
as ‘direct dependencies’ between parent and child variables. Naively, the acyclic condition prevents circular
reasoning. These connections are discussed in detail in chapter(3).

Deﬁnition 2.6 (Neighbour). For an undirected graph G the neighbours of x, ne (x) are those nodes directly
connected to x.

Deﬁnition 2.7 (Clique).

A

D

B

C

E

Given an undirected graph, a clique is a fully connected subset of nodes.
All the members of the clique are neighbours; for a maximal clique there is
no larger clique that contains the clique. For example this graph has two
maximal cliques,
. Whilst A, B, C
are fully connected, this is a non-maximal clique since there is a larger
fully connected set, A, B, C, D that contains this. A non-maximal clique is
sometimes called a cliquo.

B, C, E
{

A, B, C, D

1 =

2 =

and

C

C

{

}

}

Cliques play a central role in both modelling and inference.
In modelling they will describe variables
that are all dependent on each other, see chapter(4). In inference they describe sets of variables with no
simpler structure describing the relationship between them and hence for which no simpler eﬃcient inference
procedure is likely to exist. We will discuss this issue in detail in chapter(5) and chapter(6).

26

DRAFT April 24, 2015

(cid:54)
(cid:54)
Numerically Encoding Graphs

c

a

f

b

g

d

(a)

e

c

b

g

e

a

f

d

(b)

Figure 2.1:
(b): Multiply-connected graph.

(a): Singly-connected graph.

Deﬁnition 2.8 (Connected graph). An undirected graph is connected if there is a path between every pair
of nodes (i.e. there are no isolated islands). For a graph which is not connected, the connected components
are those subgraphs which are connected.

Deﬁnition 2.9 (Singly Connected Graph). A graph is singly connected if there is only one path from any
node A to any other node B. Otherwise the graph is multiply connected . This deﬁnition applies regardless
of whether or not the edges in the graph are directed. An alternative name for a singly connected graph is
a tree. A multiply-connected graph is also called loopy.

Deﬁnition 2.10 (Spanning Tree).

A spanning tree of an undirected graph G is a singly-connected
subset of the existing edges such that the resulting singly-
connected graph covers all nodes of G. On the right is a graph
and an associated spanning tree. A maximum weight spanning
tree is a spanning tree such that the sum of all weights on the
edges of the tree is at least as large as any other spanning tree of
G.

Procedure 2.1 (Finding a maximal weight spanning tree). An algorithm to ﬁnd a spanning tree with
maximal weight is as follows: Start by picking the edge with the largest weight and add this to the edge
set. Then pick the next candidate edge which has the largest weight and add this to the edge set – if this
results in an edge set with cycles, then reject the candidate edge and propose the next largest edge weight.
Note that there may be more than one maximal weight spanning tree.

2.2 Numerically Encoding Graphs

Our ultimate goal is to make computational implementations of inference. Therefore, if we want to incorpo-
rate graph structure into our models, we need to express graphs in a way that a computer can understand
and manipulate. There are several equivalent possibilities.

2.2.1 Edge list

As the name suggests, an edge list simply lists which node-node pairs are in the graph. For ﬁg(2.2a), an
edge list is L =
. Undirected edges are listed
(1, 2), (2, 1), (1, 3), (3, 1), (2, 3), (3, 2), (2, 4), (4, 2), (3, 4), (4, 3)
}
{
twice, once for each direction.

DRAFT April 24, 2015

27

Numerically Encoding Graphs

2

2

1

4

1

4

3

(a)

3

(b)

(a): An undirected graph can be represented
Figure 2.2:
as a symmetric adjacency matrix. (b): A directed acyclic
graph with nodes labelled in ancestral order corresponds to
a triangular adjacency matrix.

2.2.2 Adjacency matrix

An alternative is to use an adjacency matrix

A =







0 1 1 0
1 0 1 1
1 1 0 1
0 1 1 0







(2.2.1)

where Aij = 1 if there is an edge from node i to node j in the graph, and 0 otherwise. Some authors include
self-connections and place 1’s on the diagonal in this deﬁnition. An undirected graph has a symmetric
adjacency matrix.

Provided that the nodes are labelled in ancestral order (parents always come before children) a directed
graph ﬁg(2.2b) can be represented as a triangular adjacency matrix:

T =







0 1 1 0
0 0 1 1
0 0 0 1
0 0 0 0







Adjacency matrix powers

(2.2.2)

Adjacency matrices may seem wasteful since many of the entries are zero. However, they have a useful
N adjacency matrix A, powers of the adjacency
property that more than redeems them. For an N
matrix (cid:2)Ak(cid:3)
ij specify how many paths there are from node i to node j in k edge hops. If we include 1’s
on the diagonal of A then (cid:2)AN −1(cid:3)
ij is non-zero when there is a path connecting i to j in the graph. If A
corresponds to a DAG the non-zero entries of the jth row of (cid:2)AN −1(cid:3) correspond to the descendants of node
j.

×

2.2.3 Clique matrix

For an undirected graph with N nodes and maximal cliques
in which each column ck has zeros except for ones on entries describing the clique. For example

K a clique matrix is an N

1, . . . ,

C

C

×

C =







1 0
1 1
1 1
0 1







K matrix

(2.2.3)

is a clique matrix for ﬁg(2.2a). A cliquo matrix relaxes the constraint that cliques are required to be
maximal1. A cliquo matrix containing only two-node cliques is called an incidence matrix . For example

Cinc =







1 1 0 0 0
1 0 1 1 0
0 1 1 0 1
0 0 0 1 1







(2.2.4)

1The term ‘cliquo’ for a non-maximal clique is attributed to Julian Besag.

28

DRAFT April 24, 2015

Code

is an incidence matrix for ﬁg(2.2a). It is straightforward to show that CincCT
inc is equal to the adjacency
matrix except that the diagonals now contain the degree of each node (the number of edges it touches).
Similarly, for any cliquo matrix the diagonal entry of [CCT]ii expresses the number of cliquos (columns)
that node i occurs in. Oﬀ diagonal elements [CCT]ij contain the number of cliquos that nodes i and j
jointly inhabit.

Remark 2.1 (Graph Confusions). Graphs are widely used, but diﬀer markedly in what they represent.
Two potential pitfalls are described below.

State Transition Diagrams Such representations are used in Markov chains and ﬁnite state automata.
Each state is a node and a directed edge between node i and node j (with an associated weight pij)
represents that a transition from state i to state j can occur with probability pij. From the graphical
models perspective we use a directed graph x(t)
x(t + 1) to represent this Markov chain. The
state-transition diagram provides a more detailed graphical description of the conditional probability
table p(x(t + 1)

→

x(t)).
|

Neural Networks Neural networks also have nodes and edges. In general, however, neural networks are
graphical representations of functions, whereas graphical models are representations of distributions.

2.3 Summary

•

•

•

•

•

•

A graph is made of nodes and edges, which we will use to represent variables and relations between them.

A DAG is an acyclic graph and will be useful for representing ’causal’ relationships between variables.

Neighbouring nodes on an undirected graph will represent dependent variables.

A graph is singly-connected if there is only one path from any node to any other – otherwise the graph is
multiply-connected.

A clique is group of nodes all of which are connected to each other.

The adjacency matrix is a machine-readable description of a graph. Powers of the adjacency matrix give
information on the paths between nodes.

Good reference for graphs, associated theories and their uses are [86, 121].

2.4 Code

2.4.1 Utility routines

drawNet.m: Draw a graph based on an adjacency matrix
ancestors.m: Find the ancestors of a node in a DAG
edges.m: Edge list from an adjacency matrix
ancestralorder.m: Ancestral order from a DAG
connectedComponents.m: Connected Components
parents.m: Parents of a node given an adjacency matrix
children.m: Children of a node given an adjacency matrix
neigh.m: Neighbours of a node given an adjacency matrix

A connected graph is a tree if the number of edges plus 1 is equal to the number of nodes. However, for a
disconnected graph this is not the case. The code istree.m below deals with the possibly disconnected case.
The routine is based on the observation that any singly-connected graph must always possess a simplicial

DRAFT April 24, 2015

29

node (a leaf node) which can be eliminated to reveal a smaller singly-connected graph.

istree.m: If graph is singly connected return 1 and elimination sequence
spantree.m: Return a spanning tree from an ordered edge list
singleparenttree.m: Find a directed tree with at most one parent from an undirected tree

Additional routines for basic graph manipulations are given at the end of chapter(6).

Exercises

2.5 Exercises

Exercise 2.1. Consider an adjacency matrix A with elements [A]ij = 1 if one can reach state i from state
j in one timestep, and 0 otherwise. Show that the matrix (cid:2)Ak(cid:3)
ij represents the number of paths that lead
from state j to i in k timesteps. Hence derive an algorithm that will ﬁnd the minimum number of steps to
get from state j to state i.

Exercise 2.2. For an N
components. You may wish to examine connectedComponents.m.

×

N symmetric adjacency matrix A, describe an algorithm to ﬁnd the connected

Exercise 2.3. Show that for a connected graph that is singly-connected, the number of edges E must be
1 that is not
equal to the number of nodes minus 1, E = V
−
singly-connected. Hence the condition E = V
1 is a necessary but not suﬃcient condition for a graph to
be singly-connected.

1. Give an example graph with E = V

−

−

Exercise 2.4. Describe a procedure to determine if a graph is singly-connected.

Exercise 2.5. Describe a procedure to determine all the ancestors of a set of nodes in a DAG.

Exercise 2.6. WikiAdjSmall.mat contains a random selection of 1000 Wiki authors, with a link between
Assume that if i
two authors if they ‘know’ each other (see snap.stanford.edu/data/wiki-Vote.html).
‘knows’ j, then j ‘knows’ i. Plot a histogram of the separation (the length of the path between two users on
the graph corresponding to the adjacency matrix) between all users based on separations from 1 to 20. That
is the bin n(s) in the histogram contains the number of pairs with separation s.

++

Exercise 2.7. The ﬁle cliques.mat contains a list of
100 cliques deﬁned on a graph of 10 nodes. Your task
is to return a set of unique maximal cliques, eliminating cliques that are wholly contained within another.
Once you have found a clique, you can represent it in binary form as, for example

@@

(1110011110)

which says that this clique contains variables 1, 2, 3, 6, 7, 8, 9, reading from left to right. Converting this
binary representation to decimal (with the rightmost bit being the units and the leftmost 29) this corresponds
to the number 926. Using this decimal representation, write the list of unique cliques, ordered from lowest
decimal representation to highest. Describe fully the stages of the algorithm you use to ﬁnd these unique
cliques. Hint: you may ﬁnd examining uniquepots.m useful.

Exercise 2.8. Explain how to construct a graph with N nodes, where N is even, that contains at least
(N/2)2 maximal cliques.

Exercise 2.9. Let N be divisible by 3. Construct a graph with N nodes by partitioning the nodes into N/3
subsets, each subset containing 3 nodes. Then connect all nodes, provided they are not in the same subset.
Show that such a graph has 3N/3 maximal cliques. This shows that a graph can have an exponentially large
number of maximal cliques[217].

Exercise 2.10.
the room, stays for some time, and leaves. The testimonies they gave to the police are:

A jewel was stolen from a room during a party2. Each of the guests (A,B,C,D,E,F ) enters

++

2This question is a modiﬁed version of a well known problem, disguised here to make looking up the solution harder.

Apologies to the original author for the lack of a citation.

30

DRAFT April 24, 2015

Exercises

1. A: I was present in the room with E, B

2. B: I was present in the room with A, F and E

3. C: I was present in the room with F and D

4. D: I was present in the room with A and F

5. E: I was present in the room with C

6. F: I was present in the room with C and E

One of the statements in the testimonies is false. Which is it?

Notes:

If any two people are present in the room at the same time, then at least one of them will report the fact that
he was present in the room with the other person. Each above testimony should be interpreted as, for example:

A: I was present in the room with E. I was present in the room with B. It does not necessarily mean that
A was simultaneously present in the room with both E and B (so A and B could have been present in the
room at some time t1, and A and E present in the room at some other time t2). The other testimonies have
a similar interpretation.

The testimonies could be partially false. If X says that he was present in the room with Y and Z, it could
be that the statement X was present in the room with Y is true, but X was present in the room with Z is
false.

DRAFT April 24, 2015

31

Exercises

32

DRAFT April 24, 2015

CHAPTER 3

Belief Networks

We can now make a ﬁrst connection between probability and graph theory. A belief network introduces
structure into a probabilistic model by using graphs to represent independence assumptions among the vari-
ables. Probability operations such as marginalizing and conditioning then correspond to simple operations
on the graph, and details about the model can be ‘read’ from the graph. There is also a beneﬁt in terms of
computational eﬃciency. Belief networks cannot capture all possible relations among variables. However,
they are natural for representing ‘causal’ relations, and they are a part of the family of graphical models we
study further in chapter(4).

3.1 The Beneﬁts of Structure

It’s tempting to think of feeding a mass of undigested data and probability distributions into a computer
and getting back good predictions and useful insights in extremely complex environments. However, unfor-
tunately, such a naive approach is likely to fail. The possible ways variables can interact is extremely large,
so that without some sensible assumptions we are unlikely to make a useful model. Independently specifying
all the entries of a table p(x1, . . . , xN ) over binary variables xi takes O(2N ) space, which is impractical for
more than a handful of variables. This is clearly infeasible in many machine learning and related application
areas where we need to deal with distributions on potentially hundreds if not millions of variables. Structure
is also important for computational tractability of inferring quantities of interest. Given a distribution on N
binary variables, p(x1, . . . , xN ), computing a marginal such as p(x1) requires summing over the 2N −1 states
of the other variables. Even on the most optimistically fast supercomputer this would take far too long,
even for a N = 100 variable system.

The only way to deal with such large distributions is to constrain the nature of the variable interactions
in some manner, both to render speciﬁcation and ultimately inference in such systems tractable. The key
idea is to specify which variables are independent of others, leading to a structured factorisation of the
joint probability distribution. For a distribution on a chain, p(x1, . . . , x100) = (cid:81)99
i=1 φ(xi, xi+1), computing
a marginal p(x1) can be computed in the blink of an eye on modern computers. Belief networks are a con-
venient framework for representing such independence assumptions. We will discuss belief networks more
formally in section(3.3), ﬁrst discussing their natural role as ‘causal’ models.

Belief networks (also called Bayes’ networks or Bayesian belief networks) are a way to depict the indepen-
dence assumptions made in a distribution [161, 182]. Their application domain is widespread, ranging from
troubleshooting[53] and expert reasoning under uncertainty to machine learning. Before we more formally
deﬁne a Belief Network (BN), an example will help motivate the development1.

1The scenario is adapted from [236].

33

R

S

B

E

J

T

(a)

R

A

(b)

The Beneﬁts of Structure

Figure 3.1: (a): Belief network structure for the
‘wet grass’ example. Each node in the graph rep-
resents a variable in the joint distribution, and
the variables which feed in (the parents) to an-
other variable represent which variables are to
(b): Belief
the right of the conditioning bar.
network for the Burglar model.

3.1.1 Modelling independencies

One morning Tracey leaves her house and realises that her grass is wet. Is it due to overnight rain or did
she forget to turn oﬀ the sprinkler last night? Next she notices that the grass of her neighbour, Jack, is also
wet. This explains away to some extent the possibility that her sprinkler was left on, and she concludes
therefore that it has probably been raining.

We can model the above situation by ﬁrst deﬁning the variables we wish to include in our model. In the
above situation, the natural variables are

R
S
J
T

0, 1
}
0, 1
}
0, 1
}
0, 1
}

∈ {
∈ {
∈ {
∈ {

R = 1 means that it has been raining, and 0 otherwise
S = 1 means that Tracey has forgotten to turn oﬀ the sprinkler, and 0 otherwise
J = 1 means that Jack’s grass is wet, and 0 otherwise
T = 1 means that Tracey’s Grass is wet, and 0 otherwise

A model of Tracey’s world then corresponds to a probability distribution on the joint set of the variables of
interest p(T, J, R, S) (the order of the variables is irrelevant).

Since each of the variables in this example can take one of two states, it would appear that we naively
have to specify the values for each of the 24 = 16 states, e.g.
p(T = 1, J = 0, R = 0, S = 1) = 0.057 etc.
However, since there are normalisation conditions for probabilities, we do not need to specify all the state
probabilities. To see how many states need to be speciﬁed, consider the following decomposition. Without
loss of generality and repeatedly using the deﬁnition of conditional probability, we may write

@@

p(T, J, R, S) = p(T

= p(T

= p(T

J, R, S)p(J, R, S)

J, R, S)p(J

J, R, S)p(J

R, S)p(R, S)
|
R, S)p(R
|

S)p(S)
|

|

|

|

(3.1.1)

(3.1.2)

(3.1.3)

|

J, R, S) requires us to specify 23 = 8 values – we need p(T = 1

That is, we may write the joint distribution as a product of conditional distributions. The ﬁrst term
J, R, S) for the 8 joint states of J, R, S.
p(T
|
The other value p(T = 0
J, R, S).
|
Similarly, we need 4 + 2 + 1 values for the other factors, making a total of 15 values in all. In general, for a
distribution on n binary variables, we need to specify 2n
1 values in the range [0, 1]. The important point
here is that the number of values that need to be speciﬁed in general scales exponentially with the number
of variables in the model – this is impractical in general and motivates simpliﬁcations.

J, R, S) is given by normalisation : p(T = 0

J, R, S) = 1
|

p(T = 1
|

−

−

Conditional independence

The modeler often knows constraints on the system. For example, in the scenario above, we may assume
that Tracey’s grass is wet depends only directly on whether or not is has been raining and whether or not
her sprinkler was on. That is, we make a conditional independence assumption

p(T

J, R, S) = p(T
|

|

R, S)

(3.1.4)

Similarly, we assume that Jack’s grass is wet is inﬂuenced only directly by whether or not it has been raining,
and write

R, S) = p(J

p(J

|

R)
|

34

(3.1.5)

DRAFT April 24, 2015

The Beneﬁts of Structure

Furthermore, we assume the rain is not directly inﬂuenced by the sprinkler,

p(R

S) = p(R)
|

which means that our model equation (3.1.3) now becomes

p(T, J, R, S) = p(T

R, S)p(J
|

R)p(R)p(S)
|

(3.1.6)

(3.1.7)

We can represent these conditional independencies graphically, as in ﬁg(3.1a). This reduces the number of
values that we need to specify to 4 + 2 + 1 + 1 = 8, a saving over the previous 15 values in the case where
no conditional independencies had been assumed.

To complete the model, we need to numerically specify the values of each conditional probability table
(CPT). Let the prior probabilities for R and S be p(R = 1) = 0.2 and p(S = 1) = 0.1. We set the
R = 0) = 0.2 (sometimes Jack’s grass is wet
remaining probabilities to p(J = 1
|
|
R = 1, S = 1) = 1,
due to unknown eﬀects, other than rain), p(T = 1
p(T = 1
R = 0, S = 1) = 0.9 (there’s a small chance that even though the sprinkler was left on, it didn’t
|
wet the grass noticeably), p(T = 1

R = 1, S = 0) = 1, p(T = 1
|
|

R = 1) = 1, p(J = 1

R = 0, S = 0) = 0.
|

Inference

Now that we’ve made a model of an environment, we can perform inference. Let’s calculate the probability
T = 1). To do this we use:
that the sprinkler was on overnight, given that Tracey’s grass is wet: p(S = 1
|

p(S = 1
|

T = 1) =

=

=

=

(cid:80)

=

p(S = 1, T = 1)
p(T = 1)
J,R p(J
(cid:80)

(cid:80)

R)p(T = 1
|
|
R)p(T = 1
J,R,S p(J
|
|

J,R p(T = 1, J, R, S = 1)
(cid:80)
J,R,S p(T = 1, J, R, S)
R, S = 1)p(R)p(S = 1)
R, S)p(R)p(S)

(cid:80)

R p(T = 1
(cid:80)

R, S = 1)p(R)p(S = 1)
|
R,S p(T = 1
R, S)p(R)p(S)
|
0.9
0.1 + 1

0.1 + 1
0.1 + 0

0.8
0.2

0.8

×
×

×
×

0.9

×

×

0.2
0.8

×
×

×
×

0.1
0.9 + 1

0.2

×

×

(3.1.8)

(3.1.9)

(3.1.10)

= 0.3382

(3.1.11)

0.9

so the (posterior) belief that the sprinkler is on increases above the prior probability 0.1, due to the evidence
that the grass is wet. Note that in equation (3.1.9), the summation over J in the numerator is unity since, for
any function f (R), a summation of the form (cid:80)
R)f (R) equals f (R). This follows from the deﬁnition
that a distribution p(J
R) must sum to one, and the fact that f (R) does not depend on J. A similar eﬀect
occurs for the summation over J in the denominator.

J p(J

|

|

Let us now calculate the probability that Tracey’s sprinkler was on overnight, given that her grass is wet
and that Jack’s grass is also wet, p(S = 1

T = 1, J = 1). We use conditional probability again:
|

p(S = 1
|

T = 1, J = 1) =

=

=

=

(cid:80)

p(S = 1, T = 1, J = 1)
p(T = 1, J = 1)
R p(T = 1, J = 1, R, S = 1)
(cid:80)
R,S p(T = 1, J = 1, R, S)
R, S = 1)p(R)p(S = 1)
R p(J = 1
|
|
(cid:80)
R)p(T = 1
R,S p(J = 1
R, S)p(R)p(S)
|
|

R)p(T = 1

(cid:80)

0.0344
0.2144

= 0.1604

(3.1.12)

(3.1.13)

(3.1.14)

(3.1.15)

The probability that the sprinkler is on, given the extra evidence that Jack’s grass is wet, is lower than the
probability that the grass is wet given only that Tracey’s grass is wet. This occurs since the fact that Jack’s
grass is also wet increases the chance that the rain has played a role in making Tracey’s grass wet.

Naturally, we don’t wish to carry out such inference calculations by hand all the time. General purpose
algorithms exist for this, such as the junction tree algorithm, chapter(6).

DRAFT April 24, 2015

35

The Beneﬁts of Structure

Example 3.1 (Was it the Burglar?). Here’s another example using binary variables, adapted from [236].
Sally comes home to ﬁnd that the burglar alarm is sounding (A = 1). Has she been burgled (B = 1), or
was the alarm triggered by an earthquake (E = 1)? She turns the car radio on for news of earthquakes and
ﬁnds that the radio broadcasts an earthquake alert (R = 1).

Using Bayes’ rule, we can write, without loss of generality,

p(B, E, A, R) = p(A

B, E, R)p(B, E, R)
|

We can repeat this for p(B, E, R), and continue

p(B, E, A, R) = p(A

B, E, R)p(R
|

|

B, E)p(E

B)p(B)
|

(3.1.16)

(3.1.17)

However, the alarm is surely not directly inﬂuenced by any report on the Radio – that is, p(A
B, E). Similarly, we can make other conditional independence assumptions such that
p(A
|

B, E, R) =
|

p(B, E, A, R) = p(A

B, E)p(R
|

|

E)p(E)p(B)

as depicted in ﬁg(3.1b).

Specifying conditional probability tables

(3.1.18)

Alarm = 1 Burglar Earthquake
1
1
0
0

0.9999
0.99
0.99
0.0001

1
0
1
0

Radio = 1 Earthquake

1
0

1
0

The remaining tables are p(B = 1) = 0.01 and p(E = 1) = 0.000001. The tables and graphical structure
fully specify the distribution. Now consider what happens as we observe evidence.

Initial Evidence: The Alarm is sounding

A = 1) =
p(B = 1
|

=

(cid:80)

(cid:80)

E,R p(B = 1, E, A = 1, R)
(cid:80)
B,E,R p(B, E, A = 1, R)
E,R p(A = 1
B = 1, E)p(B = 1)p(E)p(R
|
(cid:80)

B, E)p(B)p(E)p(R
B,E,R p(A = 1
|

|

E)
|
E)

0.99

≈

(3.1.19)

(3.1.20)

Additional Evidence: The Radio broadcasts an Earthquake warning: A similar calculation gives
p(B = 1
0.01. Thus, initially, because the Alarm sounds, Sally thinks that she’s been
burgled. However, this probability drops dramatically when she hears that there has been an Earthquake.
That is, the Earthquake ‘explains away’ to an extent the fact that the Alarm is ringing. See demoBurglar.m.

A = 1, R = 1)
|

≈

Remark 3.1 (Causal intuitions). Belief networks as we’ve deﬁned them are ways to express independence
statements. Nevertheless, in expressing these independencies it can be useful (though also potentially
In example(3.1) we chose the ordering of the variables as
misleading) to think of ‘what causes what’.
(reading from right to left) B, E, R, A in equation (3.1.17) since B and E can be considered root ‘causes’
and A and R as ‘eﬀects’.

3.1.2 Reducing the burden of speciﬁcation

Consider a discrete variable y with many discrete parental variables x1, . . . , xn, ﬁg(3.2a). Formally, the
x1, . . . , xn).
structure of the graph implies nothing about the form of the parameterisation of the table p(y
|
x1, . . . , xn)
If each parent xi has dim (xi) states, and there is no constraint on the table, then the table p(y
|

36

DRAFT April 24, 2015

Uncertain and Unreliable Evidence

x1

x2

x3

x4

x5

x1

x2

x3

x4

x5

x1

x2

x3

x4

x5

z1

z2

z1

z2

z3

z4

z5

y

(a)

y

(b)

y

(c)

Figure 3.2:
Here only 16 states are required. (c): Noisy logic gates.

(a): If all variables are binary 25 = 32 states are required to specify p(y

x1, . . . , x5).
|

(b):

1) (cid:81)

contains (dim (y)
i dim (xi) entries. If stored explicitly for each state, this would require potentially
huge storage. An alternative is to constrain the table to have a simpler parametric form. For example, one
might write a decomposition in which only a limited number of parental interactions are required (this is
called divorcing parents in [161]). For example, in ﬁg(3.2b), we have

−

p(y

x1, . . . , x5) =
|

(cid:88)

z1,z2

p(y

|

z1, z2)p(z1

|

x1, x2, x3)p(z2

x4, x5)
|

(3.1.21)

Assuming all variables are binary, the number of states requiring speciﬁcation is 23 + 22 + 22 = 16, compared
to the 25 = 32 states in the unconstrained case.

Logic gates

Another technique to constrain tables uses simple classes of conditional tables. For example, in ﬁg(3.2c),
one could use a logical OR gate on binary zi, say

p(y

z1, . . . , z5) =
|

(cid:26) 1 if at least one of the zi is in state 1

0 otherwise

(3.1.22)

xi). When each xi
We can then make a table p(y
|
is binary there are in total only 2 + 2 + 2 + 2 + 2 = 10 quantities required for specifying p(y
x). In this
|
case, ﬁg(3.2c) can be used to represent any noisy logic gate, such as the noisy OR or noisy AND, where the
number of parameters required to specify the noisy gate is linear in the number of parents.

x1, . . . , x5) by including the additional terms p(zi = 1
|

The noisy-OR is particularly common in disease-symptom networks in which many diseases x can give rise
to the same symptom y– provided that at least one of the diseases is present, the probability that the
symptom will be present is high.

3.2 Uncertain and Unreliable Evidence

In the following we make a distinction between evidence that is uncertain, and evidence that is unreliable.

3.2.1 Uncertain evidence

In soft or uncertain evidence, the evidence variable is in more than one state, with the strength of our belief
red, blue, green
about each state being given by probabilities. For example, if x has the states dom(x) =
}
{
the vector (0.6, 0.1, 0.3) represents the belief in the respective states. In contrast, for hard evidence we are
certain that a variable is in a particular state. In this case, all the probability mass is in one of the vector
components, for example (0, 0, 1).

Performing inference with soft-evidence is straightforward and can be achieved using Bayes’ rule. For
example, for a model p(x, y), consider that we have some soft evidence ˜y about the variable y, and wish to

DRAFT April 24, 2015

37

Uncertain and Unreliable Evidence

know what eﬀect this has on the variable x – that is we wish to compute p(x
assumption p(x

y), we have

y, ˜y) = p(x
|
(cid:88)

p(x, y

|
˜y) =
|

y

p(x

˜y) =
|

(cid:88)

y

p(x
|

y, ˜y)p(y

˜y) =

|

(cid:88)

y

p(x

y)p(y
|

˜y)
|

˜y). From Bayes’ rule, and the
|

(3.2.1)

˜y) represents the probability that y is in state i under the soft-evidence. This is a generalisation
where p(y = i
|
of hard-evidence in which the vector p(y
˜y) has all zero component values, except for a single component.
|
This procedure in which we ﬁrst deﬁne the model conditioned on the evidence, and then average over the
distribution of the evidence is also known as Jeﬀrey’s rule.

In the BN we use a dashed circle to represent that a variable is in a soft-evidence
state.

x

y

Example 3.2 (soft-evidence). Revisiting the burglar scenario, example(3.1), imagine that we are only 70%
sure we heard the burglar alarm sounding. For this binary variable case we represent this soft-evidence for
the states (1, 0) as ˜A = (0.7, 0.3). What is the probability of a burglary under this soft-evidence?

p(B = 1

˜A) =
|

(cid:88)

A

p(B = 1
|

A)p(A

˜A) = p(B = 1
|

A = 1)
|

A = 0)
0.7 + p(B = 1
|

×

×

0.3

(3.2.2)

The probabilities p(B = 1
|
before to give

A = 1)

≈

0.99 and p(B = 1

A = 0)
|

≈

0.0001 are calculated using Bayes’ rule as

p(B = 1

˜A)
|

≈

0.6930

(3.2.3)

This is lower than 0.99, the probability of having been burgled when we are sure we heard the alarm.

Holmes, Watson and Mrs Gibbon

An entertaining example of uncertain evidence is given by Pearl[236] that we adapt for our purposes here.
The environment contains four variables

B
A
W
G

tr, fa
}
tr, fa
}
tr, fa
}
tr, fa
}

∈ {
∈ {
∈ {
∈ {

B = tr means that Holmes’ house has been burgled
A = tr means that Holmes’ house Alarm went oﬀ
W = tr means that Watson heard the alarm
G = tr means that Mrs Gibbon heard the alarm

The BN below for this scenario is depicted in ﬁg(3.3a)

p(B, A, G, W ) = p(A

B)p(B)p(W

|

A)p(G
|

A).
|

(3.2.4)

Watson states that he heard the alarm is sounding. Mrs Gibbon is a little deaf and cannot be sure herself
that she heard the alarm, being 80% sure she heard it. This can be dealt with using the soft evidence
technique, ﬁg(3.3b). From Jeﬀrey’s rule, one uses the original model equation (3.2.4) to ﬁrst compute the
model conditioned on the evidence

p(B = tr

W = tr, G) =
|

p(B = tr, W = tr, G)
p(W = tr, G)

=

and then uses the soft-evidence

p(G

˜G) =
|

(cid:26) 0.8 G = tr
0.2 G = fa

to compute

(cid:80)

A p(G
(cid:80)

A)p(W = tr
|

A)p(A
|
A)p(W = tr
|

B = tr)p(B = tr)
|
B)p(B)
A)p(A
|

B,A p(G

|

(3.2.5)

(3.2.6)

p(B = tr

W = tr, ˜G) = p(B = tr
|
|

W = tr, G = tr)p(G = tr
|

˜G)+p(B = tr

˜G) (3.2.7)
W = tr, G = fa)p(G = fa
|
|

A full calculation requires us to numerically specify all the terms in equation (3.2.4); see for example
exercise(3.8).

38

DRAFT April 24, 2015

Uncertain and Unreliable Evidence

B

A

G

W

B

A

G

W

B

A

H

W

B

A

G

H

(a)

(b)

(c)

(d)

(a): Mr Holmes’ burglary worries as given in [236]: (B)urglar, (A)larm, (W)atson, Mrs
Figure 3.3:
(G)ibbon. (b): Mrs Gibbon’s uncertain evidence represented by a dashed circle. (c): Virtual evidence or
the replacement of unreliable evidence can be represented by a dashed line. (d): Mrs Gibbon is uncertain
in her evidence. Holmes also replaces the unreliable Watson with his own interpretation.

3.2.2 Unreliable evidence

@@

Holmes telephones Mrs Gibbon and realises that he doesn’t trust her evidence (he suspects that she’s been
drinking) and
his interpretation is that if the alarm sounded it was 80% probable to have resulted in Mrs
Gibbon stating that she heard it; if the alarm didn’t sound, there is a 20% chance that Mrs Gibbon would
have stated that she heard it. Note that this is not the same as Mrs Gibbon being 80% sure herself that
she heard the alarm – this would be soft evidence whose eﬀect on our calculations would also contain the
A), as in equation (3.2.5). Holmes rather wishes to discard all of this and simply replace it with
term p(G
|
his own interpretation of events. Mr Holmes can achieve this by replacing the term p(G
A) by a so-called
virtual evidence term

|

p(G

A)

|

→

p(H

A),
|

where p(H

A) =
|

(cid:26) 0.8 A = tr
0.2 A = fa

Here the state H is arbitrary and ﬁxed. This is used to modify the joint distribution to

p(B, A, H, W ) = p(A
|

B)p(B)p(W

A)p(H
|

A),
|

(3.2.8)

(3.2.9)

see ﬁg(3.3c). When we then compute p(B = tr
W = tr, H) the eﬀect of Mr Holmes’ judgement will count
|
for a factor of 4 times more in favour of the alarm sounding than not. The values of the table entries are
irrelevant up to normalisation since any constants can be absorbed into the proportionality constant. Note
also that p(H
A) is not a distribution in A, and hence no normalisation is required. This form of evidence
|
is also called likelihood evidence.

Uncertain and unreliable evidence

To demonstrate how to combine such eﬀects as unreliable and uncertain evidence, consider the situation in
which Mrs Gibbon is uncertain in her evidence, and Mr Holmes feels that Watson’s evidence is unreliable
and wishes to replaces it with his own interpretation, see ﬁg(3.3d). To account for this we ﬁrst deal with
the unreliable evidence

p(B, A, W, G)

p(B, A, H, G) = p(B)p(A
|

→

B)p(G

A)p(H

A)

|

|

(3.2.10)

Using this modiﬁed model, we can now use Jeﬀrey’s rule to compute the model conditioned on the evidence

p(B, A
|

H, G) =

(cid:80)

p(B)p(A
A,B p(B)p(A
|

B)p(G
|

A)p(H
|
B)p(G

A)
|
A)p(H

|

|

A)

We now include the uncertain evidence ˜G to form the ﬁnal model

p(B, A
|

H, ˜G) =

(cid:88)

G

H, G)p(G
p(B, A
|

˜G)
|

from which we may compute the marginal p(B

p(B

H, ˜G) =
|

(cid:88)

A

p(B, A
|

H, ˜G)

H, ˜G)
|

DRAFT April 24, 2015

(3.2.11)

(3.2.12)

(3.2.13)

39

3.3 Belief Networks

Deﬁnition 3.1 (Belief network). A belief network is a distribution of the form

p(x1, . . . , xD) =

D
(cid:89)

i=1

p(xi

pa (xi))
|

Belief Networks

(3.3.1)

where pa (xi) represent the parental variables of variable xi. Represented as a directed graph, with an arrow
pointing from a parent variable to child variable, a belief network corresponds to a Directed Acyclic Graph
(DAG), with the ith node in the graph corresponding to the factor p(xi

pa (xi)).
|

Remark 3.2 (Graphs and distributions). A somewhat subtle point is whether or not a belief network
corresponds to a speciﬁc instance of a distribution (as given in deﬁnition(3.1)) requiring also the numerical
speciﬁcation of the conditional probability tables, or whether or not it refers to any distribution which is
consistent with the speciﬁed structure.
In this one can potentially distinguish between a belief network
distribution (containing a numerical speciﬁcation) and a belief network graph (which contains no numerical
speciﬁcation). Normally this issue will not arise much throughout the book, but is potentially important in
clarifying the scope of independence/dependence statements.

In the Wet Grass and Burglar examples, we had a choice as to how we recursively used Bayes’ rule. In a
general 4 variable case we could choose the factorisation,

p(x1, x2, x3, x4) = p(x1

x2, x3, x4)p(x2
|

|

x3, x4)p(x3

x4)p(x4)
|

An equally valid choice is (see ﬁg(3.4))

p(x1, x2, x3, x4) = p(x3

x4, x1, x2)p(x4
|

x1, x2)p(x1
|

x2)p(x2).
|

(3.3.2)

(3.3.3)

In general, two diﬀerent graphs may represent the same independence assumptions, as we will discuss further
in section(3.3.1). If one wishes to make independence assumptions, then the choice of factorisation becomes
signiﬁcant.

The observation that any distribution may be written in the cascade form, ﬁg(3.4), gives an algorithm for
constructing a BN on variables x1, . . . , xn : write down the n
node cascade graph; label the nodes with
the variables in any order; now each successive independence statement corresponds to deleting one of the
edges. More formally, this corresponds to an ordering of the variables which, without loss of generality, we
may write as x1, . . . , xn. Then, from Bayes’ rule, we have

−

p(x1, . . . , xn) = p(x1
= p(x1

|

|

= p(xn)

x2, . . . , xn)p(x2, . . . , xn)
x2, . . . , xn)p(x2
n−1
(cid:89)

p(xi

xi+1, . . . , xn)
|

i=1

x3, . . . , xn)p(x3, . . . , xn)
|

(3.3.4)

(3.3.5)

(3.3.6)

The representation of any BN is therefore a Directed Acyclic Graph (DAG).

Every probability distribution can be written as a BN, even though it may correspond to a fully connected
‘cascade’ DAG. The particular role of a BN is that the structure of the DAG corresponds to a set of
conditional independence assumptions, namely which ancestral parental variables are suﬃcient to specify
each conditional probability table. Note that this does not mean that non-parental variables have no inﬂu-
x3, this does not imply
ence. For example, for distribution p(x1
p(x2
x3). The DAG speciﬁes conditional independence statements of variables on their an-
|
cestors – namely which ancestors are direct ‘causes’ for the variable. The ‘eﬀects’, given by the descendants
of the variable, will generally be dependent on the variable. See also remark(3.3).

x3)p(x3) with DAG x1
|

x1, x3) = p(x2
|

x2)p(x2
|

←

←

x2

40

DRAFT April 24, 2015

Belief Networks

x1

x2

x3

x4

x3

x4

x1

x2

(a)

(b)

Figure 3.4: Two BNs for a 4 variable distribution. Both graphs (a) and (b) represent the same distribution
p(x1, x2, x3, x4). Strictly speaking they represent the same (lack of) independence assumptions – the graphs
say nothing about the content of the tables. The extension of this ‘cascade’ to many variables is clear and
always results in a Directed Acyclic Graph.

Remark 3.3 (Dependencies and the Markov Blanket). Consider a distribution on a set of variables
. For
a variable xi
and corresponding belief network represented by a DAG G, let M B(xi) be the variables
in the Markov blanket of xi. Then for any other variable y that is also not in the Markov blanket of xi
(y
M B(xi). That is, the Markov blanket of xi carries all information
about xi. As an example, for ﬁg(3.2b), M B(z1) =

M B(xi)
}

), then xi

M B(z1).

∈ X \ {

and z1

∈ X

⊥⊥

xi

X

∪

y

|

x1, x2, x3, y, z2
{

}

x4
|

⊥⊥

The DAG corresponds to a statement of conditional independencies in the model. To complete the speci-
pa (xi)). Once
ﬁcation of the BN we need to deﬁne all elements of the conditional probability tables p(xi
the graphical structure is deﬁned, the entries of the conditional probability tables (CPTs) p(xi
pa (xi)) can
|
be expressed. For every possible state of the parental variables pa (xi), a value for each of the states of xi
needs to be speciﬁed (except one, since this is determined by normalisation). For a large number of parents,
writing out a table of values is intractable, and the tables are usually parameterised in a low dimensional
manner. This will be a central topic of our discussion on the application of BNs in machine learning.

|

3.3.1 Conditional independence

Whilst a BN corresponds to a set of conditional independence assumptions, it is not always immediately
clear from the DAG whether a set of variables is conditionally independent of a set of other variables (see
deﬁnition(1.7)). For example, in ﬁg(3.5) are x1 and x2 independent, given the state of x4? The answer is
yes, since we have

p(x1, x2

x4) =
|

1
p(x4)

(cid:88)

x3

p(x1, x2, x3, x4) =

1
p(x4)

(cid:88)

x3

p(x1

x4)p(x2
|

x3, x4)p(x3)p(x4)
|

= p(x1

x4)

|

(cid:88)

x3

p(x2

|

x3, x4)p(x3)

Now

p(x2

|

x4) =

=

1
p(x4)
(cid:88)

x3

p(x2

x3, x4)p(x3)
|

(cid:88)

x1,x3

p(x1, x2, x3, x4) =

1
p(x4)

(cid:88)

x1,x3

p(x1

|

x4)p(x2

|

x3, x4)p(x3)p(x4)

Combining the two results above we have

p(x1, x2

x4) = p(x1
|

|

x4)p(x2

x4)

|

(3.3.7)

(3.3.8)

(3.3.9)

(3.3.10)

(3.3.11)

x1

x2

x3

x4

Figure 3.5: p(x1, x2, x3, x4) = p(x1

x4)p(x2

|

|

x3, x4)p(x3)p(x4).

DRAFT April 24, 2015

41

Belief Networks

x1

x2

x1

x2

x1

x2

x1

x2

x3

(a)

x3

(b)

x3

(c)

x3

(d)

Figure 3.6: By dropping say the connection between variables x1 and x2, we reduce the 6 possible BN graphs
x2, (a)
amongst three variables to 4. (The 6 fully connected ‘cascade’ graphs correspond to (a) with x1
x1. Any other
with x2
→
graphs would be cyclic and therefore not distributions).

x2 and (d) with x2

x1, (b) with x1

x2, (b) with x2

x1, (c) with x1

→

→

→

→

→

so that x1 and x2 are indeed independent conditioned on x4.

We would like to have a general algorithm that will allow us to avoid doing such tedious manipulations by
reading the result directly from the graph. To help develop intuition towards constructing such an algorithm,
consider the three variable distribution p(x1, x2, x3). We may write this in any of the 6 ways

p(x1, x2, x3) = p(xi1|

xi2, xi3)p(xi2|

xi3)p(xi3)

(3.3.12)

where (i1, i2, i3) is any of the 6 permutations of (1, 2, 3). Whilst each factorisation produces a diﬀerent DAG,
all represent the same distribution, namely one that makes no independence statements. If the DAGs are of
the cascade form, no independence assumptions have been made. The minimal independence assumptions
then correspond to dropping a single link in the cascade graph. This gives rise to the 4 DAGs in ﬁg(3.6).
Are any of these graphs equivalent, in the sense that they represent the same distribution? Applying Bayes’
rule gives :

p(x2
(cid:124)

x1)p(x1)
x3)p(x3
|
|
(cid:125)
(cid:123)(cid:122)
graph(c)

= p(x2, x3)p(x3, x1)/p(x3) = p(x1

x3)p(x2, x3)

|

= p(x1
(cid:124)

|

x3)p(x3
|
(cid:123)(cid:122)
graph(d)

x2)p(x2)
(cid:125)

= p(x1
(cid:124)

x3)p(x3)
x3)p(x2
|
|
(cid:123)(cid:122)
(cid:125)
graph(b)

(3.3.13)

(3.3.14)

so that DAGs (b), (c) and (d) represent the same conditional independence (CI) assumptions — given the
state of variable x3, variables x1 and x2 are independent, x1

x3.

x2
|

⊥⊥

However, graph (a) represents something fundamentally diﬀerent, namely: p(x1, x2) = p(x1)p(x2). There is
no way to transform the distribution p(x3

x1, x2)p(x1)p(x2) into any of the others.

|

Remark 3.4 (Graphical Dependence). Belief network (graphs) are good for encoding conditional indepen-
dence but are not well suited for encoding dependence. For example, consider the graph a
b. This may
appear to encode the relation that a and b are dependent. However, a speciﬁc numerical instance of a belief
network distribution could be such that p(b
b. The lesson is that even when the
a) = p(b), for which a
|
DAG appears to show ‘graphical’ dependence, there can be instances of the distributions for which depen-
dence does not follow. The same caveat holds for Markov networks, section(4.2). We discuss this issue in
more depth in section(3.3.5).

⊥⊥

→

3.3.2 The impact of collisions

Deﬁnition 3.2. Given a path
a

, a
b. Note that a collider is path speciﬁc, see ﬁg(3.8).

collider is a node c on

P

c

→

←

with neighbours a and b on

such that

@@

P

P

42

DRAFT April 24, 2015

Belief Networks

x

y

x

y

x

y

z

(a)

z

(b)

z

(c)

x

y

w

z

(d)

Figure 3.7: In graphs (a) and (b), variable z is not a collider.
(a) and (b) represent conditional independence x
conditionally dependent given variable z.

⊥⊥

y

|

(c): Variable z is a collider. Graphs
z. In graphs (c) and (d), x and y are ‘graphically’

In a general BN, how can we check if x
z since

y

|

⊥⊥

z? In ﬁg(3.7a), x and y are independent when conditioned on

p(x, y

z) = p(x
|

|

z)p(y

z)
|

Similarly, for ﬁg(3.7b), x and y are independent conditioned on z since

p(x, y

z)

|

∝

p(z

|

x)p(x)p(y

z)

|

(3.3.15)

(3.3.16)

@@

which is a function of x multiplied by a function of y. In ﬁg(3.7c), however, x and y are graphically dependent
since p(x, y
is a collider — the arrows of its neighbours
are pointing towards it. What about ﬁg(3.7d)? In (d), when we condition on z, x and y will be graphically
dependent, since

x, y)p(x)p(y); in this situation, variable z
|

z)
|

p(z

∝

p(x, y

z) =
|

p(x, y, z)
p(z)

=

1
p(z)

(cid:88)

w

p(z

w)p(w
|

x, y)p(x)p(y)
|

= p(x

z)p(y
|

|

z)

(3.3.17)

@@ The above inequality holds due to the term p(w

x, y) = const. would
|
x and y be independent. Intuitively, variable w becomes dependent on the value of z, and since x and y are
conditionally dependent on w, they are also conditionally dependent on z.

x, y) – only in special cases such as p(w
|

If there is a non-collider z which is conditioned along the path between x and y (as in ﬁg(3.7)(a,b)), then
this path cannot induce dependence between x and y. Similarly, if there is a path between x and y which
contains a collider, provided that this collider is not in the conditioning set (and neither are any of its
descendants) then this path does not make x and y dependent. If there is a path between x and y which
contains no colliders and no conditioning variables, then this path ‘d-connects’ x and y. Note that a collider
is deﬁned relative to a path. In ﬁg(3.8a), the variable d is a collider along the path a
c, but not
along the path a

e (since, relative to this path, the two arrows do not point inwards to d).

−

−

−

d

d

b

b

−

−

−

B

→

Consider the BN: A
C. Here A and C are (unconditionally) independent. However, conditioning
of B makes them ‘graphically’ dependent. Intuitively, whilst we believe the root causes are independent,
given the value of the observation, this tells us something about the state of both the causes, coupling them
and making them (generally) dependent.
In deﬁnition(3.3) below we describe the eﬀect that condition-
ing/marginalisation has on the graph of the remaining variables.

←

Deﬁnition 3.3 (Some properties of belief networks). It is useful to understand what eﬀect conditioning or
marginalising a variable has on a belief network. We state here how these operations eﬀect the remaining
variables in the graph and use this intuition to develop a more complete description in section(3.3.4).

A

B

p(A, B, C) = p(C

A, B)p(A)p(B)
|

(3.3.18)

C

From a ‘causal’ perspective, this models the ‘causes’ A and B as a priori independent,
both determining the eﬀect C.

DRAFT April 24, 2015

43

(cid:54)
Belief Networks

a

b

e

a

e

c

d

b

d

c

(a): The variable d is a collider along the path a
c,
d
−
but not along the path a
b? a and e
e
−
are not d-connected since there are no colliders on the only path
between a and e, and since there is a non-collider b which is in the
b.
conditioning set. Hence a and e are d-separated by b,

Is a

⊥⊥

−

−

−

−

e.

a

d

b

b

|

⇒

⊥⊥

e
|

b

d

d

−

−

−

−

−

−

−

e
|

−
−

e. Is a
d

(b): The variable d is a collider along the path a
e, but not
−
c? There are two paths
d
c
along the path a
⊥⊥
e. The
d
e and a
between a and e, namely a
path a
e is not blocked since although d is a collider on this
path and d is not in the conditioning set, we have a descendant
of the collider d in the conditioning set, namely c. For the path
a
e, the node c is a collider on this path and c is
in the conditioning set. For this path d is not a collider. Hence
this path is not blocked and a and e are (graphically) dependent
given c.

−

−

−

−

−

−

−

−

d

c

c

b

b

Figure 3.8: Collider examples for d-separation and d-connection.

A

B

C

A

B

C

A

B

C

D

A

B

A

B

A

B

A

B

→

→

→

Marginalising over C makes A and B independent. A
and B are (unconditionally) independent : p(A, B) =
p(A)p(B).
In the absence of any information about
the eﬀect C, we retain this belief.

Conditioning on C makes A and B (graphically) de-
pendent — in general p(A, B
C). Al-
|
though the causes are a priori independent, knowing
the eﬀect C in general tells us something about how
the causes colluded to bring about the eﬀect observed.

C)p(B
|

= p(A

C)

|

Conditioning on D, a descendent of a collider C,
makes A and B (graphically) dependent — in general
D).
p(A, B

= p(A

D)
|

D)p(B
|

|

p(A, B, C) = p(A
|

C)p(B

|

C)p(C)

(3.3.19)

Here there is a ‘cause’ C and independent ‘eﬀects’ A and B.

B

→

A

B

Marginalising over C makes A and B (graphically) de-
pendent. In general, p(A, B)
= p(A)p(B). Although
we don’t know the ‘cause’, the ‘eﬀects’ will neverthe-
less be dependent.

DRAFT April 24, 2015

C

C

A

44

(cid:54)
(cid:54)
(cid:54)
Belief Networks

A

B

C

C

A

A

B

→

C)p(B
|

C) = p(A
|

Conditioning on C makes A and B independent:
p(A, B
C). If you know the ‘cause’
|
C, you know everything about how each eﬀect occurs,
independent of the other eﬀect. This is also true for
reversing the arrow from A to C – in this case A would
‘cause’ C and then C ‘cause’ B. Conditioning on C
blocks the ability of A to inﬂuence B.

B

=
= A

B

= A

B

C

C

These graphs all express the same
assump-
conditional
tions.

independence

3.3.3 Graphical path manipulations for independence

Intuitively, we now have all the tools we need to understand when x is independent of y conditioned on z.
Examining the rules in deﬁnition(3.3), we need to look at each path between x and y. Colouring x as red
and y as green and the conditioning node z as yellow, we need to examine each path between x and y and
adjust the edges, following the intuitive rules in ﬁg(3.9).

3.3.4 d-Separation

The above description is intuitive. A more formal treatment that is amenable to computational implemen-
tation is straightforward to obtain from these intuitions. First we deﬁne the DAG concepts of d-separation
and d-connection that are central to determining conditional independence in any BN with structure given
by the DAG[304].

Deﬁnition 3.4 (d-connection, d-separation). If G is a directed graph in which
sets of vertices, then
U between some vertex in
descendent of C is in

are disjoint
in G if and only if there exists an undirected path
such that for every collider C on U , either C or a

are d-connected by
and some vertex in

, and no non-collider on U is in

Z
Y

and

and

Z

X

X

X

Y

Y

,

.

Z

Z

and

Y

X

are d-separated by

Z

in G if and only if they are not d-connected by

in G.

Z

One may also phrase this as follows. For every variable x
and y. A path U is said to be blocked if there is a node w on U such that either

and y

∈ X

∈ Y

, check every path U between x

1. w is a collider and neither w nor any of its descendants is in

, or

Z

2. w is not a collider on U and w is in

If all such paths are blocked then
by

, they are independent conditional on

and

X

Y

in all probability distributions such a graph can represent.

X

Y

. If the variable sets

and

are d-separated

Z

.

Z
are d-separated by

Z

Z

Remark 3.5 (Bayes Ball). The Bayes Ball algorithm[258] provides a linear time complexity algorithm
is called the set
which given a set of nodes
of irrelevant nodes for

determines the set of nodes

such that

X ⊥⊥ Y| Z

and
.

X
given

Z

Y

Y

.

X

Z

3.3.5 Graphical and distributional in/dependence

We have shown

and

Y

X

d-separated by

Z ⇒ X ⊥⊥ Y| Z

in all distributions consistent with the belief network structure.

DRAFT April 24, 2015

45

x

x

x

x

x

x

u

z

u

w

z

u

z

u

z

u

z

z

y

y

y

y

y

u

⇒

⇒

⇒

⇒

⇒

x

x

x

x

x

u

z

u

w

z

u

z

u

z

u

z

y

y

y

y

y

Belief Networks

If z is a collider (bottom path) keep undirected links
between the neighbours of the collider.

If z is a descendant of a collider, this could induce
dependence so we retain the links (making them undi-
rected)

If there is a collider not in the conditioning set (upper
path) we cut the links to the collider variable. In this
case the upper path between x and y is ‘blocked’.

If there is a non-collider which is in the conditioning
set (bottom path), we cut the link between the neigh-
bours of this non-collider which cannot induce depen-
dence between x and y. The bottom path is ‘blocked’.

In this case, neither path contributes to dependence
and hence x

z. Both paths are ‘blocked’.

y
|

⊥⊥

y

x

u

y

w

⇒

z

w

Whilst z is a collider in the conditioning
set, w is a collider that is not in the con-
ditioning set. This means that there is no
path between x and y, and hence x and y
are independent given z.

Figure 3.9: Graphical manipulations to determine independence x
z. After these manipulations,
if there is no undirected path between x and y, then x and y are independent, conditioned on z. Note
that the graphical rules here diﬀer from those in deﬁnition(3.3) which considered the eﬀect on the graph
having eliminated a variable (via conditioning or marginalisation). Here we consider rules for determining
independence based on a graphical representation in which the variables remain in the graph.

⊥⊥

y

|

In other words, if one takes any instance of a distribution P which factorises according to the belief network
P of all the conditional independence statements that can be obtained
structure and then writes down a list
from P , if
. Note that
the list
P could contain more statements than those obtained from the graph. For example for the belief
network graph

then this list must contain the statement

are d-separated by

X ⊥⊥ Y| Z

and

Z

X

L

L

Y

a, b)p(a)p(b)
p(a, b, c) = p(c
|

46

(3.3.20)

DRAFT April 24, 2015

Belief Networks

b

t

g

(a)

f

s

b

t

f

s

g

u

(b)

Figure 3.10: (a): t and f are d-connected by g. (b):
b and f are d-separated by u.

which is representable by the DAG a
b is the only graphical independence statement
we can make. Consider a distribution consistent with equation (3.3.20), for example, on binary variables
dom(a) = dom(b) = dom(c) =

b, then a

⊥⊥

→

←

c

0, 1
{

}

b)2, p[1](a = 1) = 0.3, p[1](b = 1) = 0.4

a, b) = (a
p[1](c = 1
|
then numerically we must have a
a

−

⊥⊥

⊥⊥
b. On the other hand, we can also consider the distribution

b for this distribution p[1]. Indeed the list

(3.3.21)

L[1] contains only the statement

a, b) = 0.5, p[2](a = 1) = 0.3, p[2](b = 1) = 0.4
p[2](c = 1
|
L[2] =

. In this case

a
{

c
}

b, a

c, b

⊥⊥

⊥⊥

⊥⊥

from which

L[2] contains more statements than a

(3.3.22)

b.

⊥⊥

An interesting question is whether or not d-connection similarly implies dependence? That is, do all distri-
butions P consistent with the belief network possess the dependencies implied by the graph? If we consider
the belief network structure equation (3.3.20) above, a and b are d-connected by c, so that graphically a and
b are dependent, conditioned on c. For the speciﬁc instance p[1] we have numerically a
c so that the list
of dependence statements for p[1] contains the graphical dependence statement. Now consider p[2]. The list
of dependence statements for p[2] is empty. Hence the graphical dependence statements are not necessarily
found in all distributions consistent with the belief network. Hence

b
|
(cid:62)(cid:62)

and

Y

X

d-connected by

Z (cid:54)⇒ X (cid:62)(cid:62)Y| Z

in all distributions consistent with the belief network structure.

See also exercise(3.17). This shows that belief networks are powerful in ensuring that distributions necessarily
obey the independence assumptions we expect from the graph. However, belief networks are not suitable
for ensuring that distributions obey desired dependency statements.

Example 3.3. Consider the graph in ﬁg(3.10a).

1. Are the variables t and f unconditionally independent, i.e. t

? Here there are two colliders,
namely g and s – however, these are not in the conditioning set (which is empty), and hence t and f
are d-separated and therefore unconditionally independent.

⊥⊥

| ∅

f

2. What about t

g? There is a path between t and f for which all colliders are in the conditioning
set. Hence t and f are d-connected by g, and therefore t and f are graphically dependent conditioned
on g.

⊥⊥

f

|

Example 3.4. Is
b or f to u contains a collider, b and f are unconditionally independent of u.

b, f
{

in ﬁg(3.10b)? Since the conditioning set is empty and every path from either

} ⊥⊥

| ∅

u

3.3.6 Markov equivalence in belief networks

We have invested a lot of eﬀort in learning how to read conditional independence relations from a DAG.
Happily, we can determine whether two DAGs represent the same set of conditional independence statements
(even when we don’t know what they are) by using a relatively simple rule.

DRAFT April 24, 2015

47

Belief Networks

t1

y1

t2

y2

h

(a)

t2

y2

t1

y1

(b)

Figure 3.11: (a): Two treatments t1, t2 and corresponding
outcomes y1, y2. The health of a patient is represented by
h. This DAG embodies the conditional independence state-
ments t1
, namely that the treat-
t1, y1
ments have no eﬀect on each other. (b): One could repre-
sent the eﬀect of marginalising over h using a bi-directional
edge.

t2, y2

, t2

⊥⊥

⊥⊥

| ∅

| ∅

Deﬁnition 3.5 (Markov equivalence). Two graphs are Markov equivalent if they both represent the same
set of conditional independence statements. This deﬁnition holds for both directed and undirected graphs.

Example 3.5. Consider the belief network with edges A
B, from which the set of conditional
independence statements is A
B, the
set of conditional independence statements is empty. In this case, the two belief networks are not Markov
equivalent.

. For another belief network with edges A

B and A

⊥⊥

→

→

→

←

←

| ∅

B

C

C

Procedure 3.1 (Determining Markov equivalence). Deﬁne an immorality in a DAG as a conﬁguration of
three nodes, A, B, C such that C is a child of both A and B, with A and B not directly connected. Deﬁne
the skeleton of a graph by removing the directions on the arrows. Two DAGs represent the same set of
independence assumptions (they are Markov equivalent) if and only if they have the same skeleton and the
same set of immoralities [78].

Using procedure(3.1) we see that in ﬁg(3.6), BNs (b,c,d) have the same skeleton with no immoralities and
are therefore equivalent. However BN (a) has an immorality and is therefore not equivalent to BNs (b,c,d).
Note that whether unmarried parents having children should be considered immoral is beyond the scope of
this book!

++

3.3.7 Belief networks have limited expressibility

Belief networks ﬁt well with our intuitive notion of modelling ‘causal’ independencies. However, formally
speaking they cannot necessarily graphically represent all the independence properties of a given distribu-
tion.

Consider the DAG in ﬁg(3.11a), (from [249]). This DAG could be used to represent two successive ex-
periments where t1 and t2 are two treatments and y1 and y2 represent two outcomes of interest; h is the
underlying health status of the patient; the ﬁrst treatment has no eﬀect on the second outcome hence
there is no edge from y1 to y2. Now consider the implied independencies in the marginal distribution
p(t1, t2, y1, y2), obtained by marginalising the full distribution over h. There is no DAG containing only
the vertices t1, y1, t2, y2 which represents the independence relations and does not also imply some other
independence relation that is not implied by ﬁg(3.11a). Consequently, any DAG on vertices t1, y1, t2, y2
alone will either fail to represent an independence relation of p(t1, t2, y1, y2), or will impose some additional
independence restriction that is not implied by the DAG. In the above example

p(t1, t2, y1, y2) = p(t1)p(t2)

(cid:88)

h

p(y1

|

t1, h)p(y2

t2, h)p(h)

|

(3.3.23)

cannot in general be expressed as a product of functions deﬁned on a limited set of the variables. However,
(t1, y1) hold in p(t1, t2, y1, y2) —
it is the case that the conditional independence conditions t1

(t2, y2), t2

⊥⊥

⊥⊥

48

DRAFT April 24, 2015

Causality

they are there, encoded in the form of the conditional probability tables. It is just that we cannot ‘see’ this
independence since it is not present in the structure of the marginalised graph (though one can naturally
infer this in the larger graph p(t1, t2, y1, y2, h)). For example, for the BN with link from y2 to y1, we have
t1
y2, which is not true for the distribution in (3.3.23). Similarly, for the BN with link from y1 to y2,
the implied statement t1

y1 is also not true for (3.3.23).

⊥⊥

t2

|

t2
|

⊥⊥

This example demonstrates that BNs cannot express all the conditional independence statements that could
be made on that set of variables (the set of conditional independence statements can be increased by consid-
ering additional variables however). This situation is rather general in the sense that any graphical model
has limited expressibility in terms of independence statements[281]. It is worth bearing in mind that BNs
may not always be the most appropriate framework to express one’s independence assumptions and intu-
itions.

A natural consideration is to use a bi-directional arrow when a variable is marginalised. For ﬁg(3.11a), one
could depict the marginal distribution using a bi-directional edge, ﬁg(3.11b). For a discussion of extensions
of BNs using bi-directional edges see [249].

3.4 Causality

Causality is a contentious topic and the purpose of this section is to make the reader aware of some pitfalls
that can occur and which may give rise to erroneous inferences. The reader is referred to [237] and [78] for
further details.

The word ‘causal’ is contentious particularly in cases where the model of the data contains no explicit tem-
poral information, so that formally only correlations or dependencies can be inferred. For a distribution
p(a, b), we could write this as either (i) p(a
a)p(a). In (i) we might think that b ‘causes’
b)p(b) or (ii) p(b
|
|
a, and in (ii) a ‘causes’ b. Clearly, this is not very meaningful since they both represent exactly the same
distribution, see ﬁg(3.12). Formally BNs only make independence statements, not causal ones. Neverthe-
less, in constructing BNs, it can be helpful to think about dependencies in terms of causation since our
intuitive understanding is usually framed in how one variable ‘inﬂuences’ another. First we discuss a classic
conundrum that highlights potential pitfalls that can arise.

3.4.1 Simpson’s paradox

Simpson’s ‘paradox’ is a cautionary tale in causal reasoning in BNs. Consider a medical trial in which
patient treatment and outcome are recovered. Two trials were conducted, one with 40 females and one with
40 males. The data is summarised in table(3.1). The question is : Does the drug cause increased recovery?
According to the table for males, the answer is no, since more males recovered when they were not given the
drug than when they were. Similarly, more females recovered when not given the drug than recovered when
given the drug. The conclusion appears that the drug cannot be beneﬁcial since it aids neither subpopulation.

However, ignoring the gender information, and collating both the male and female data into one combined
table, we ﬁnd that more people recovered when given the drug than when not. Hence, even though the
drug doesn’t seem to work for either males or females, it does seem to work overall! Should we therefore
recommend the drug or not?

A

B

A

B

R

W

R

W

(a)

(b)

(c)

(d)

Figure 3.12: Both (a) and (b) represent the same distribution p(a, b) = p(a
rain)p(rain).
The graph represents p(rain, grasswet) = p(grasswet
|
grasswet)p(grasswet), although this appears to be causally non-sensical.
p(rain
|

(c):
(d): We could equally have written

b)p(b) = p(b
|
|

a)p(a).

DRAFT April 24, 2015

49

Causality

Males
Given Drug
Not Given Drug

Females
Given Drug
Not Given Drug

Combined
Given Drug
Not Given Drug

Recovered Not Recovered Rec. Rate
12
3

60%
70%

18
7

Recovered Not Recovered Rec. Rate
8
21

20%
30%

2
9

Recovered Not Recovered Rec. Rate
20
24

50%
40%

20
16

Table 3.1: Table for Simpson’s Paradox (from [237])

G

D

R

FD

(a)

G

D

(b)

Resolution of the paradox

(a): A DAG for the relation be-
Figure 3.13:
tween Gender (G), Drug (D) and Recovery (R), see
table(3.1). (b): Inﬂuence diagram. No decision vari-
able is required for G since G has no parents.

R

The ‘paradox’ occurs because we are asking a causal (interventional) question – If we give someone the drug,
what happens? – but we are performing an observational calculation. Pearl[237] would remind us that there
is a diﬀerence between ‘given that we see’ (observational evidence) and ‘given that we do’ (interventional
evidence). We want to model a causal experiment in which we ﬁrst intervene, setting the drug state, and
then observe what eﬀect this has on recovery.

A model of the Gender, Drug and Recovery data (which makes no conditional independence assumptions)
is, ﬁg(3.13a),

p(G, D, R) = p(R

G, D)p(D
|

G)p(G)
|

(3.4.1)

G) in equation
In a causal interpretation, however, if we intervene and give the drug, then the term p(D
(3.4.1) should play no role in the experiment – we decide to give the drug or not independent of gender.
The term p(D
G) therefore needs to be replaced by a term that reﬂects the set-up of the experiment. We
use the idea of an atomic intervention, in which a single variable is set in a particular state. In our atomic
causal intervention, where we set D, we deal with the modiﬁed distribution

|

|

˜p(G, R

D) = p(R
|

|

G, D)p(G)

(3.4.2)

where the terms on the right hand side of this equation are taken from the original BN of the data. To
denote an intervention we use

:
||

p(R

G, D)

||

˜p(R

G, D) =
|

≡

p(R
G, D)p(G)
|
R p(R

G, D)p(G)

(cid:80)

|

= p(R

G, D)
|

(3.4.3)

(One can also consider here G as being interventional – in this case it doesn’t matter since the fact that the
variable G has no parents means that for any distribution conditional on G, the prior factor p(G) will not
be present). Using equation (3.4.3), for the males given the drug 60% recover, versus 70% recovery when
not given the drug. For the females given the drug 20% recover, versus 30% recovery when not given the drug.

Similarly,

p(R

D)

||

˜p(R

D) =
|

≡

(cid:80)
(cid:80)

G, D)p(G)
G p(R
|
G, D)p(G)
R,G p(R
|

(cid:88)

=

G

p(R

G, D)p(G)
|

50

(3.4.4)

DRAFT April 24, 2015

Causality

Using the post intervention distribution, equation (3.4.4), we have

p(recovery
|

p(recovery

drug) = 0.6

no drug) = 0.7
|

0.5 + 0.2

0.5 + 0.3

×

×

×

×

0.5 = 0.4

0.5 = 0.5

(3.4.5)

(3.4.6)

Hence we infer that the drug is overall not helpful, as we intuitively expect, and is consistent with the
results from both subpopulations.

Summarising the above argument, p(G, D, R) = p(R
G, D)p(G)p(D) means that we choose either a Male
|
or Female patient and give them the drug or not independent of their gender, hence the absence of the
term p(D
G) from the joint distribution. One way to think about such models is to consider how to draw
|
a sample from the joint distribution of the random variables – in most cases this should clarify the role of
causality in the experiment.

In contrast to the interventional calculation, the observational calculation makes no conditional independence
assumptions. This means that, for example, the term p(D
G) plays a role in the calculation (the reader
|
might wish to verify that the result given in the combined data in table(3.1) is equivalent to inferring with
the full distribution equation (3.4.1)).

3.4.2 The do-calculus

In making causal inferences we’ve seen above that we must adjust the model to reﬂect any causal experi-
mental conditions. In setting any variable into a particular state we need to surgically remove all parental
links of that variable. Pearl calls this the do operator , and contrasts an observational (‘see’) inference p(x
y)
|
with a causal (‘make’ or ‘do’) inference p(x

do(y)).
|

Deﬁnition 3.6 (Pearl’s Do Operator).

@@ Let all the variables
intervention variables
variables Xc1, . . . , XcK , ck
∈ C
post intervention distribution:

X
X

X

C

∪ X

=
¯C. For a Belief Network p(

¯C be written in terms of the intervention variables

C and the non-
pa (Xi)), inferring the eﬀect of setting
|
, in states xc1, . . . , xcK , is equivalent to standard evidential inference in the

) = (cid:81)

i p(Xi

X

X

@@

p(

X

do(Xc1 = xc1), . . . , do(XcK = xcK )) =

¯C|

(cid:89)

j∈ ¯C

p (Xj

pa (Xj))
|

(3.4.7)

@@ where any parental variable included in the intervention set is set to its intervention state. An alternative
@@

notation is p(

xc1, . . . , xcK ).

¯C||

X

@@

pa (Xci)) are removed from the original Belief Network.

In words, for those variables for which we causally intervene and set in a particular state, the corresponding
Graphically, the eﬀect is to consider each
terms p(Xci|
intervention variable, cut the connections to its parents and set the intervention variable to its intervention
state. For variables which are evidential but non-causal, the corresponding factors are not removed from
the distribution. The interpretation is that the post intervention distribution corresponds to an experiment
in which the causal variables are ﬁrst set and non-causal variables are subsequently observed.

++ For a Belief Network to have a causal interpretation, it means that the ancestral order of the variables must
correspond to the temporal order. This means therefore that if we start with the variables that have no
parents, these must come ﬁrst in time, with their children coming later, etc. Ancestral sampling from a
causal Belief Network therefore corresponds to the temporal evolution of a physical experiment.

3.4.3

Inﬂuence diagrams and the do-calculus

Another way to represent intervention is to modify the basic BN by appending a parental decision variable
FX to any variable X on which an intervention can be made, giving rise to a so-called inﬂuence diagram[78].

DRAFT April 24, 2015

51

For example2, for the Simpson’s paradox example, we may use, ﬁg(3.13b),

˜p(D, G, R, FD) = p(D

FD, G)p(G)p(R

G, D)p(FD)
|

|

where

Code

(3.4.8)

p(D

FD =
|

∅

, G)

≡

p(D

pa (D)), p(D
|

FD = d, G) = 1 for D = d and 0 otherwise
|

Hence, if the decision variable FD is set to the empty state, the variable D is determined by the standard
observational term p(D
pa (D)). If the decision variable is set to a state of D, then the variable puts all its
|
probability in that single state of D = d. This has the eﬀect of replacing the conditional probability term by
a unit factor and any instances of D set to the variable in its interventional state3. A potential advantage
of this inﬂuence diagram approach over the do-calculus is that conditional independence statements can be
derived using standard techniques for the augmented BN. Additionally, for learning, standard techniques
apply in which the decision variables are set to the condition under which each data sample was collected
(a causal or non-causal sample).

Remark 3.6 (Learning the edge directions). In the absence of data from causal experiments, one should
be justiﬁably sceptical about learning ‘causal’ networks. Nevertheless, one might prefer a certain direction
of a link based on assumptions of the ‘simplicity’ of the CPTs. This preference may come from a physical
intuition that whilst root causes may be uncertain, the relationship from cause to eﬀect is clear. In this
sense a measure of the complexity of a CPT is required, such as entropy. Such heuristics can be numerically
encoded and the edge directions learned in an otherwise Markov equivalent graph. See also exercise(12.6).

3.5 Summary

We can reason with certain or uncertain evidence using repeated application of Bayes’ rule.

A belief network represents a factorisation of a distribution into conditional probabilities of variables de-
pendent on parental variables.

Belief networks correspond to directed acyclic graphs.

Variables are conditionally independent x
network corresponds to a conditional independence statement.

z if p(x, y

z) = p(x
|

y
|

⊥⊥

|

z)p(y

z); the absence of a link in a belief
|

If in the graph representing the belief network, two variables are independent, then they are independent
in any distribution consistent with the belief network structure.

Belief networks are natural for representing ‘causal’ inﬂuences.

Causal questions must be addressed by an appropriate causal model.

•

•

•

•

•

•

•

3.6 Code

3.6.1 Naive inference demo

demoBurglar.m: Was it the Burglar demo
demoChestClinic.m: Naive Inference on Chest Clinic. See exercise(3.4).

2Here the inﬂuence diagram is a distribution over variables including decision variables, in contrast to the application of

IDs in chapter(7).

3More general cases can be considered in which the variables are placed in a distribution of states [78].

52

DRAFT April 24, 2015

Exercises

H

P

A

U

a

t

x

D

Figure 3.14: Party animal. Here all variables are binary.
P = Been to
Party, H = Got a Headache, D = Demotivated at work, U = Underperform
at work, A =Boss Angry. Shaded variables are observed in the true state.

l

e

b

s

d

x = Positive X-ray

d = Dyspnea (Shortness of breath)

e = Either Tuberculosis or Lung Cancer

t = Tuberculosis

l = Lung Cancer

b = Bronchitis

a = Visited Asia

s = Smoker

Figure 3.15: Belief network structure for the Chest Clinic example.

3.6.2 Conditional independence demo

The following demo determines whether
for the Chest Clinic network, ﬁg(3.15), and checks
the result numerically4. The independence test is based on the Markov method of section(4.2.4). This
is an alternative to the d-separation method and also more general in that it deals also with conditional
independence in Markov Networks as well as belief networks. Running the demo code below, it may happen
that the numerical dependence is very small – that is

X ⊥⊥ Y | Z

,

p(

X

)

≈

Y|Z

p(

X |Z

)p(

Y|Z

)

(3.6.1)

even though
condindepPot.m: Numerical measure of conditional independence
demoCondindep.m: Demo of conditional independence (using Markov method)

X (cid:62)(cid:62)Y| Z

. This highlights the diﬀerence between ‘structural’ and ‘numerical’ independence.

3.6.3 Utility routines

dag.m: Find the DAG structure for a belief network

3.7 Exercises

Exercise 3.1 (Party Animal). The party animal problem corresponds to the network in ﬁg(3.14). The boss
is angry and the worker has a headache – what is the probability the worker has been to a party? To complete
the speciﬁcations, the probabilities are given as follows:

p(U = tr
p(U = tr
p(A = tr

P = tr, D = tr) = 0.999
|
P = tr, D = fa) = 0.9
|
U = tr) = 0.95
|

p(U = tr
p(U = tr
p(A = tr

P = fa, D = tr) = 0.9
|
P = fa, D = fa) = 0.01
|
U = fa) = 0.5
|

P = tr) = 0.9
p(H = tr
|
p(H = tr
P = fa) = 0.2
|
p(P = tr) = 0.2, p(D = tr) = 0.4

a, b)p(a)p(b). (i) Is a
Exercise 3.2. Consider the distribution p(a, b, c) = p(c
|

b
| ∅

⊥⊥

? (ii) Is a

c?

b
|

⊥⊥

Exercise 3.3. The Chest Clinic network[184] concerns the diagnosis of lung disease (tuberculosis, lung
cancer, or both, or neither), see ﬁg(3.15). In this model a visit to Asia is assumed to increase the probability
of tuberculosis. State if the following conditional independence relationships are true or false

1. tuberculosis

smoking
|

⊥⊥

shortness of breath

4The code for graphical conditional independence is given in chapter(4).

DRAFT April 24, 2015

53

Battery

Fuel

Gauge

Figure 3.16: Belief network of car starting, see exercise(3.6).

Turn Over

Start

Exercises

2. lung cancer

⊥⊥

3. visit to Asia

smoking

bronchitis
|
lung cancer

4. visit to Asia

⊥⊥

⊥⊥

smoking
|
smoking
|

lung cancer, shortness of breath

Exercise 3.4. Consider the Chest Clinic belief network in ﬁg(3.15)[184]. Calculate by hand the values for
s = fa). The table values are:
s = tr), p(d
p(d), p(d
|
|

p(a = tr)
a = tr)
p(t = tr
|
s = tr)
p(l = tr
|
s = tr)
p(b = tr
|
p(x = tr
e = tr)
|
e = tr, b = tr) = 0.9
p(d = tr
|
e = fa, b = tr) = 0.8
p(d = tr
|

= 0.01 p(s = tr)
= 0.5
a = fa)
= 0.05 p(t = tr
= 0.01
|
s = fa)
p(l = tr
= 0.01
= 0.1
|
s = fa)
p(b = tr
= 0.3
= 0.6
|
= 0.98 p(x = tr
e = fa)
= 0.05
|
e = tr, b = fa) = 0.7
p(d = tr
|
e = fa, b = fa) = 0.1
p(d = tr
|

p(e = tr

t, l) = 0 only if both t and l are fa, 1 otherwise.
|

Exercise 3.5. If we interpret the Chest Clinic network exercise(3.4) causally, how can we help a doctor
answer the question ‘If I could cure my patients of Bronchitis, how would this aﬀect my patients’ chance of
being short of breath?’. How does this compare with p(d = tr
b = fa) in a non-causal interpretation, and
|
what does this mean?

Exercise 3.6 ([140]). The network in ﬁg(3.16) concerns the probability of a car starting, with

p(f = empty) = 0.05

p(b = bad) = 0.02
p(g = empty
p(g = empty
b = good) = 0.03
p(t = fa
|
t = tr, f = not empty) = 0.01
p(s = fa
|
t = fa, f = not empty) = 1.0
p(s = fa
|

b = good, f = not empty) = 0.04 p(g = empty
|
p(g = empty
b = bad, f = not empty) = 0.1
|
b = bad) = 0.98
p(t = fa
|
t = tr, f = empty) = 0.92
p(s = fa
|
t = fa, f = empty) = 0.99
p(s = fa
|

b = good, f = empty) = 0.97
|
b = bad, f = empty) = 0.99
|

Calculate P (f = empty
that the car does not start.

s = no), the probability of the fuel tank being empty conditioned on the observation
|

Exercise 3.7. There is a synergistic relationship between Asbestos (A) exposure, Smoking (S) and Cancer
(C). A model describing this relationship is given by

p(A, S, C) = p(C

A, S)p(A)p(S)

|

(3.7.1)

1. Is A

2. Is A

S

S

⊥⊥

⊥⊥

?

| ∅
C?

|

3. How could you adjust the model to account for the fact that people who work in the building industry

have a higher likelihood to also be smokers and also a higher likelihood to asbestos exposure?

Exercise 3.8.

Consider the belief network on the right which represents Mr
(B)urglar,
Holmes’ burglary worries as given in ﬁg(3.3a) :
(A)larm, (W)atson, Mrs (G)ibbon.

B

A

G

W

54

DRAFT April 24, 2015

Exercises

All variables are binary with states

tr, fa
}
{

. The table entries are

p(B = tr)
p(A = tr
|
p(W = tr
p(G = tr

= 0.01
B = fa) = 0.05
B = tr) = 0.99 p(A = tr
|
p(W = tr
A = fa) = 0.5
A = tr) = 0.9
|
|
A = fa) = 0.2
p(G = tr
A = tr) = 0.7
|
|

1. Compute ‘by hand’ (i.e. show your working) :

(a) p(B = tr
|
(b) p(B = tr
|

W = tr)
W = tr, G = fa)

(3.7.2)

2. Consider the same situation as above, except that now the evidence is uncertain. Mrs Gibbon thinks
that the state is G = fa with probability 0.9. Similarly, Dr Watson believes in the state W = fa with
value 0.7. Compute ‘by hand’ the posteriors under these uncertain (soft) evidences:

(a) p(B = tr
|
(b) p(B = tr
|

˜W )
˜W , ˜G)

Exercise 3.9. A doctor gives a patient a (D)rug (drug or no drug) dependent on their (A)ge (old or young)
and (G)ender (male or female). Whether or not the patient (R)ecovers (recovers or doesn’t recover) depends
on all D, A, G. In addition A

G

.

⊥⊥
1. Write down the belief network for the above situation.

| ∅

2. Explain how to compute p(recover

3. Explain how to compute p(recover

drug).
|
do(drug), young).
|

Exercise 3.10. Implement the Wet Grass scenario in section(3.1.1) using the BRMLtoolbox.

Exercise 3.11 (LA Burglar). Consider the Burglar scenario, example(3.1). We now wish to model the
fact that in Los Angeles the probability of being burgled increases if there is an earthquake. Explain how to
include this eﬀect in the model.

Exercise 3.12. Given two belief networks represented as DAGs with associated adjacency matrices A and
B, write a MATLAB function MarkovEquiv(A,B).m that returns 1 if A and B are Markov equivalent, and
zero otherwise.

Exercise 3.13. The adjacency matrices of two belief networks are given below (see ABmatrices.mat). State
if they are Markov equivalent.

A =














0
0
0
0
0
0
0
0
0

0
0
0
0
0
0
0
0
0

1
1
0
0
1
0
0
0
0

1
0
0
0
0
1
0
0
0

0
1
0
0
0
0
0
0
0

1
0
0
0
0
0
0
0
0

0
0
1
0
1
0
0
0
0

0
0
0
1
0
1
0
0
0














0
0
0
1
0
0
1
0
0

,

B =














0
0
0
0
0
1
0
0
0

0
0
0
0
1
0
0
0
0

1
1
0
0
1
0
0
0
0

1
0
0
0
0
1
0
0
0

0
0
0
0
0
0
0
0
0

0
0
0
0
0
0
0
0
0

0
0
1
0
1
0
0
0
0

0
0
0
1
0
1
0
0
0














0
0
0
1
0
0
1
0
0

(3.7.3)

}

∈ {

1, 2, 3

. Computer i can send a message in
Exercise 3.14. There are three computers indexed by i
one timestep to computer j if Cij = 1, otherwise Cij = 0. There is a fault in the network and the task is
to ﬁnd out some information about the communication matrix C (C is not necessarily symmetric). To do
this, Thomas, the engineer, will run some tests that reveal whether or not computer i can send a message
to computer j in t timesteps, t
Cij. For example, he
might know that C13(2) = 1, meaning that according to his test, a message sent from computer 1 will arrive
at computer 3 in at most 2 timesteps. Note that this message could go via diﬀerent routes – it might go
directly from 1 to 3 in one timestep, or indirectly from 1 to 2 and then from 2 to 3, or both. You may
= j, and assumes that
assume Cii = 1. A priori Thomas thinks there is a 10% probability that Cij = 1, i
each such connection is independent of the rest. Given the test information
,
}
compute the a posteriori probability vector

. This is expressed as Cij(t), with Cij(1)

C12(2) = 1, C23(2) = 0

1, 2
}

∈ {

=

≡

C

{

[p(C12 = 1

|C

), p(C13 = 1

|C

), p(C23 = 1

|C

), p(C32 = 1

|C

), p(C21 = 1

|C

), p(C31 = 1

|C

)]

DRAFT April 24, 2015

(3.7.4)

55

(cid:54)
Exercise 3.15. A belief network models the relation between the variables oil, inf, eh, bp, rt which stand for
the price of oil, inﬂation rate, economy health, British Petroleum Stock price, retailer stock price. Each
variable takes the states low, high, except for bp which has states low, high, normal. The belief network model
for these variables has tables

Exercises

p(eh=low)=0.2
oil=low)=0.9
p(bp=low
|
oil=high)=0.1
p(bp=low
|
p(oil=low
eh=low)=0.9
|
inf=low,eh=low)=0.9
p(rt=low
|
inf=high,eh=low)=0.1
p(rt=low
|
p(inf=low
oil=low,eh=low)=0.9
|
oil=high,eh=low)=0.1
p(inf=low
|

p(bp=normal
p(bp=normal
p(oil=low
p(rt=low
|
p(rt=low
|
p(inf=low
p(inf=low

|
|

oil=low)=0.1
|
oil=high)=0.4
|

eh=high)=0.05
|
inf=low,eh=high)=0.1
inf=high,eh=high)=0.01
oil=low,eh=high)=0.1
oil=high,eh=high)=0.01

1. Draw a belief network for this distribution.

2. Given that the BP stock price is normal and the retailer stock price is high, what is the probability that

inﬂation is high?

Exercise 3.16. There are a set of C potentials with potential c deﬁned on a subset of variables

c. If
d we can merge (multiply) potentials c and d since the variables in potential c are contained within
c
X
potential d. With reference to suitable graph structures, describe an eﬃcient algorithm to merge a set of
potentials so that for the new set of potentials no potential is contained within the other.

⊆ X

X

Exercise 3.17. This exercise explores the distinction between d-connection and dependence. Consider the
distribution class

b)p(b
p(a, b, c) = p(c
|
|

a)p(a)

(3.7.5)

for which a is d-connected to c. One might expect that this means that a and c are dependent, a
interest is to show that there are non-trivial distributions for which a

c.

(cid:62)(cid:62)

c. Our

⊥⊥

1, 2
{

}


and dom(b) =

1/4
1/12
2/3

15/40
1/8
1/2

. For

1, 2, 3
{


}

 ,

b) =
p(c
|

(cid:18) 1/3 1/2 15/40
5/8

2/3 1/2

(cid:19)

(3.7.6)

a) =
p(b
|



1. Consider dom(a) = dom(c) =

p(a) =

(cid:18) 3/5
2/5

(cid:19)

,

show that a

c.

⊥⊥

2. Consider

p(a, b, c) =

1
Z
for positive function φ, ψ and Z = (cid:80)

φ(a, b)ψ(b, c)

a,b,c φ(a, b)ψ(b, c). Deﬁning matrices M and N with elements

(3.7.7)

Mij = φ(a = i, b = j),

Nkj = ψ(b = j, c = k)

show that the marginal distribution p(a = i, c = k) is represented by the matrix elements

p(a = i, c = k) =

1
Z

(cid:104)
MNT(cid:105)

ik

3. Show that if

MNT = m0nT
0

for some vectors m0 and n0, then a

c.

⊥⊥

(3.7.8)

(3.7.9)

(3.7.10)

56

DRAFT April 24, 2015

Exercises

4. Writing

M = [m1 m2 m3] ,

N = [n1 n2 n3]

for two dimensional vectors mi, ni, i = 1, . . . , 3, show that

MNT = m1nT

1 + m2nT

2 + m3nT
3

5. Show that by setting

m2 = λm1, n3 = γ (n1 + λn2)

for scalar λ, γ then MNT can be written as m0nT

0 where

m0

≡

m1 + γm3,

n0

≡

n1 + λn2

(3.7.11)

(3.7.12)

(3.7.13)

(3.7.14)

@@

6. Hence construct example tables

c. Verify your examples explicitly using BRMLtoolbox.

a), p(c
(diﬀerent from those speciﬁed in part 1 above) p(a), p(b
|
|

b) for

which a

⊥⊥

Exercise 3.18. Alice and Bob share a bank account which contains an a priori unknown total amount
of money T . Whenever Alice goes to the cash machine, the available amount for withdrawal A for Alice is
always 10% of the total T . Similarly, when Bob goes to the cash machine the available amount for withdrawal
B for Bob is 10% of the total T . Whatever the amount in the bank, Alice and Bob check their available
amounts for withdrawal independently. Draw a belief network that expresses this situation and show that
A

B.

(cid:62)(cid:62)

Exercise 3.19. Assume that the day of the week that females are born on, x, is independent of the day of
the week, y, on which males are born. Assume, however, that the old rhyme is true and that personality
is dependent on the day of the week you’re born on. If a represents the female personality type and b the
b. Whether or not a male and a female are married, m,
y, but a
male personality type, then a
depends strongly on their personality types, m
, but is independent of x and y if we know a and b.
a, b
}
Draw a belief network that can represent this setting. What can we say about the (graphical) dependency
between the days of the week that John and Jane are born on, given that they are not married?

⊥⊥
(cid:62)(cid:62){

x and b

(cid:62)(cid:62)

(cid:62)(cid:62)

++

Exercise 3.20.
A survey of households in which the husband and wife each own their own car is made.
The survey also states whether each household income (inc) is high or low. There are 4 car types, dom(h) =
dom(w) =
, the ﬁrst two being ‘cheap’ and the last two being ‘expensive’. The survey ﬁnds that,
}
given their household income, the types of cars owned by a husband and wife are independent:

1, 2, 3, 4
{

wife’s car type

husband’s car type
|

⊥⊥

family income

Speciﬁcally, p(inc = low) = 0.8 and

p(w

inc = low) =
|

inc = low) =

p(h
|













0.7
0.3
0
0

0.2
0.8
0
0







, p(w

|

inc = high) =













0.2
0.1
0.4
0.3







inc = high) =
, p(h
|













0
0
0.3
0.7

Use BRMLtoolbox to ﬁnd the marginal p(w, h) and show that whilst h

w
|

⊥⊥

inc, it is not the case that h

w.

⊥⊥

++

Exercise 3.21.
A, B, C that have been competing in a tournament, with the following outcomes:

BinGame is a two-player game with only a ‘win’ or ‘lose’ outcome. There are three players,

A beat B 2 times.
B beat C 2 times.

•

•

DRAFT April 24, 2015

57

A beat C 2 times.

C beat A 1 time.

Player D enters the tournament and loses twice to player C.

Exercises

We assume that each player has a skill level from 1, . . . , 10 and that, given the skill levels of two players, sA,
sB, the probability that player A beats player B is 1/(1 + exp(sB
sA)). Assume that a priori the skill levels
of the players are independent and that we place equal probability to each of the 10 skills levels. Additionally,
the outcome of all games, given the skill levels are independent. Using the above tournament information :

−

•

•

•

1. Draw a belief network that represents the independence assumptions above.

2. Are the skill levels of the players a posteriori (i.e. given the game outcomes) independent?

3. Calculate the probability that player D will beat player A in a game of BinGame.

4. Calculate the expected skill level of each of the 4 players.

Exercise 3.22.
Before watching a video for free on their website, the Daily Fail forces you to watch a
video advert. There are in fact 10 adverts available and the Daily Fail randomly selects three advert links
to present to each viewer. After clicking on one of the three advert links, the advert plays, after which the
desired news video can be watched. The data from the Daily Fail from 20 website visitors is as follows:

++

(1, 2, 3)(2, 5, 3)(4, 7, 10)(6, 3, 4)(6, 8, 5)(9, 3, 7)(10, 2, 4)(7, 1, 2)(8, 7, 2)(8, 3, 6)

(8, 6, 4)(4, 3, 9)(5, 4, 1)(9, 5, 1)(6, 7, 8)(4, 9, 7)(10, 8, 6)(5, 4, 3)(6, 3, 2)(1, 4, 2)

where (ai, aj, ak) means that advert i was clicked on in preference to adverts j and k.

Assume that each advert has an implicit ‘interest’ value s
which aﬀects the probability that
viewers will click on the video. If sA, sB, sC are the interest levels of the three randomly chosen adverts
A, B, C, then the probability that the viewer clicks on advert A is assumed to be proportional to exp(sA
−
max(sB, sC)). Assuming that the interest levels are a priori independent and uniform and that each viewer
clicks on adverts independently of the others (given the advert interest levels), calculate the expected interest
levels in each advert.

1, . . . , 5
}

∈ {

58

DRAFT April 24, 2015

CHAPTER 4

Graphical Models

In chapter(3) we saw how belief networks are used to represent statements about independence of variables in
a probabilistic model. Belief networks are simply one way to unite probability and graphical representation.
Many others exist, all under the general heading of ‘graphical models’. Each has speciﬁc strengths and
weaknesses. Broadly, graphical models fall into two classes: those useful for modelling, such as belief
networks, and those useful for inference. This chapter will survey the most popular models from each class.

4.1 Graphical Models

Graphical Models (GMs) are depictions of independence/dependence relationships for distributions. Each
class of GM is a particular union of graph and probability constructs and details the form of independence
assumptions represented. GMs are useful since they provide a framework for studying a wide class of prob-
abilistic models and associated algorithms. In particular they help to clarify modelling assumptions and
provide a uniﬁed framework under which inference algorithms in diﬀerent communities can be related.

It needs to be emphasised that all forms of GM have a limited ability to graphically express conditional
(in)dependence statements[281]. As we’ve seen, belief networks are useful for modelling ancestral condi-
tional independence. In this chapter we’ll introduce other types of GM that are more suited to representing
diﬀerent assumptions. Here we’ll focus on Markov networks, chain graphs (which marry Belief and Markov
networks) and factor graphs. There are many more inhabitants of the zoo of graphical models, see [73, 314].

The general viewpoint we adopt is to describe the problem environment using a probabilistic model, after
which reasoning corresponds to performing probabilistic inference. This is therefore a two part process :

Modelling After identifying all potentially relevant variables of a problem environment, our task is to
describe how these variables can interact. This is achieved using structural assumptions as to the
form of the joint probability distribution of all the variables, typically corresponding to assumptions
of independence of variables. Each class of graphical model corresponds to a factorisation property of
the joint distribution.

Inference Once the basic assumptions as to how variables interact with each other is formed (i.e. the
probabilistic model is constructed) all questions of interest are answered by performing inference on
the distribution. This can be a computationally non-trivial step so that coupling GMs with accurate
inference algorithms is central to successful graphical modelling.

Whilst not a strict separation, GMs tend to fall into two broad classes – those useful in modelling, and those
useful in representing inference algorithms. For modelling, belief networks, Markov networks, chain graphs
and inﬂuence diagrams are some of the most popular. For inference one typically ‘compiles’ a model into a

59

Markov Networks

x2

x2

x1

x3

x1

x3

x1

x4

(a)

x4

(b)

x5

x6

x3

x2

x4

(c)

Figure 4.1:
φ(x1, x2, x4)φ(x2, x3, x4)φ(x3, x5)φ(x3, x6)/Zc.

(a):

φ(x1, x2)φ(x2, x3)φ(x3, x4)φ(x4, x1)/Za

(b):

φ(x1, x2, x3, x4)/Zb

(c):

suitable GM for which an algorithm can be readily applied. Such inference GMs include factor graphs and
junction trees.

4.2 Markov Networks

Belief networks correspond to a special kind of factorisation of the joint probability distribution in which
each of the factors is itself a distribution. An alternative factorisation is, for example

p(a, b, c) =

1
Z

φ(a, b)φ(b, c)

(4.2.1)

where φ(a, b) and φ(b, c) are potentials (see below) and Z is a constant which ensures normalisation, called
the partition function

Z =

(cid:88)

a,b,c

φ(a, b)φ(b, c)

(4.2.2)

Deﬁnition 4.1 (Potential). A potential φ(x) is a non-negative function of the variable x, φ(x)
0. A joint
potential φ(x1, . . . , xn) is a non-negative function of the set of variables. A distribution is a special case
of a potential satisfying normalisation, (cid:80)
x φ(x) = 1. This holds similarly for continuous variables, with
summation replaced by integration.

≥

We will typically use the convention that the ordering of the variables in the potential is not relevant (as
for a distribution) – the joint variables simply index an element of the potential table. Markov Networks
are deﬁned as products of potentials deﬁned on maximal cliques of an undirected graph – see below and
ﬁg(4.1).

Deﬁnition 4.2 (Markov Network). For a set of variables
a product of potentials on subsets of the variables

c
X

⊆ X

p(x1, . . . , xn) =

1
Z

C
(cid:89)

c=1

φc(

c)

X

=

{

X
:

x1, . . . , xn

}

a Markov network is deﬁned as

(4.2.3)

The constant Z ensures the distribution is normalised. Graphically this is represented by an undirected
graph G with
c, c = 1, . . . , C being the maximal cliques of G. For the case in which clique potentials are
strictly positive, this is called a Gibbs distribution.

X

Deﬁnition 4.3 (Pairwise Markov network ). In the special case that the graph contains cliques of only size
2, the distribution is called a pairwise Markov Network , with potentials deﬁned on each link between two
variables.

60

DRAFT April 24, 2015

Markov Networks

1

2

3

5

6

4

(a)

7

1

5

6

7

2

3

4

(b)

(a): φ(1, 2, 3)φ(2, 3, 4)φ(4, 5, 6)φ(5, 6, 7).
Figure 4.2:
(b): By the global Markov property, since every path
from 1 to 7 passes through 4, then 1

4.

7
|

⊥⊥

Whilst a Markov network is formally deﬁned on maximal cliques, in practice
authors often use the term to refer to non-maximal cliques. For example, in the
graph on the right, the maximal cliques are x1, x2, x3 and x2, x3, x4, so that the
graph describes a distribution p(x1, x2, x3, x4) = φ(x1, x2, x3)φ(x2, x3, x4)/Z. In
a pairwise network though the potentials are assumed to be over two-cliques,
giving p(x1, x2, x3, x4) = φ(x1, x2)φ(x1, x3)φ(x2, x3)φ(x2, x4)φ(x3, x4)/Z.

x2

x1

x3

x4

Example 4.1 (Boltzmann machine). A Boltzmann machine is a MN on binary variables dom(xi) =
of the form

0, 1
{

}

p(x) =

1
Z(w, b)

e

(cid:80)

i<j wij xixj +(cid:80)

i bixi

(4.2.4)

@@

where the interactions wij are the ‘weights’ and the bi the ‘biases’. This model has been studied in the
machine learning community as a basic model of distributed memory and computation[2]. The graphical
model of the
= 0.
Consequently, for all but specially constrained W, the graph is multiply-connected and inference will be
typically intractable.

Boltzmann machine is an undirected graph with a link between nodes i and j for wij

Deﬁnition 4.4 (Properties of Markov Networks).

B

B

B

A

A

A

C

C

C

p(A, B, C) = φAC(A, C)φBC(B, C)/Z

(4.2.5)

A

A

→

→

B

B

Marginalising over C makes A and B (graphically)
dependent. In general p(A, B)

= p(A)p(B).

Conditioning on C makes A and B independent:
C).
p(A, B

C)p(B

C) = p(A
|

|

|

4.2.1 Markov properties

We consider here informally the properties of Markov networks and the reader is referred to [182] for detailed
proofs. Consider the MN in ﬁg(4.2a) in which we use the shorthand p(1)
φ(x1, x2, x3)
etc. We will use this undirected graph to demonstrate conditional independence properties. Note that
throughout we will be often dividing by potentials and, in order to ensure this is well deﬁned, we assume the
potentials are positive. For positive potentials the following local, pairwise and global Markov properties
are all equivalent.

p(x1), φ(1, 2, 3)

≡

≡

DRAFT April 24, 2015

61

(cid:54)
(cid:54)
x1

x3

x2

x1

x2

x1

x4

x3

x4

x3

x4

x3

x4

x3

(a)

(b)

(c)

(d)

(e)

Markov Networks

x2

x4

(a-d): Local conditional distributions. Note that no distribution is implied for the parents of
Figure 4.3:
x1, x3) – one should not read from the graph
each variable. That is, in (a) we are given the conditional p(x4
(e): The Markov network consistent with the local
that we imply x1 and x3 are marginally independent.
distributions. If the local distributions are positive, by the Hammersley-Cliﬀord theorem, the only joint
distribution that can be consistent with the local distributions must be a Gibbs distribution with structure
given by (e).

|

Deﬁnition 4.5 (Separation). A subset
every path from any member of
of
A
and

to a member of
are separated.

then

A

B

to any member of

A
is separated from

B
. If

B

=

∅

S

S

separates a subset

A

from a subset

) if
(for disjoint
. If there is no path from a member

and

A

B

B

passes through

then provided no path exists from

S

to

,

B

A

A

B

Deﬁnition 4.6 (Global Markov Property). For disjoint sets of variables, (
from

in G, then

.

B

A ⊥⊥ B| S

,

,

B

A

S

) where

S

separates

A

As an example of the global Markov property,

consider ﬁg(4.2a) for which

@@

p(1, 7
|

4)

∝

=

=

(cid:88)

2,3,5,6
(cid:88)

2,3,5,6



(cid:88)



2,3

p(1, 2, 3, 4, 5, 6, 7)

φ(1, 2, 3)φ(2, 3, 4)φ(4, 5, 6)φ(5, 6, 7)

φ(1, 2, 3)φ(2, 3, 4)







(cid:88)

φ(4, 5, 6)φ(5, 6, 7)








5,6



(4.2.6)

(4.2.7)

(4.2.8)

This implies that p(1, 7

4). This can be inferred since all paths from 1 to 7 pass
4)p(7
4) = p(1
|
|
|

through 4.

@@

Procedure 4.1 (An algorithm for independence). The separation property implies a simple algorithm for
. If there is no path
deciding
from any member of

. We simply remove all links that neighbour the set of variables

is true – see also section(4.2.4).

to any member of

A ⊥⊥ B| S

, then

S

A

B

A ⊥⊥ B| S

For positive potentials, the so-called local Markov property holds

p(x

|X \

x) = p(x

ne (x)).
|

(4.2.9)

That is, when conditioned on its neighbours, x is independent of the remaining variables of the graph. In
addition, the so-called pairwise Markov property holds that for any non-adjacent vertices x and y

x

y
| X \ {

⊥⊥

x, y

.

}

(4.2.10)

4.2.2 Markov random ﬁelds

A MRF is a set of conditional distributions, one for each indexed ‘location’.

62

DRAFT April 24, 2015

Markov Networks

1, . . . , n

Deﬁnition 4.7 (Markov Random Field). A MRF is deﬁned by a set of distributions p(xi
ne (xi)) where
|
indexes the distributions and ne (xi) are the neighbours of variable xi, namely that subset of
i
the variables x1, . . . , xn that the distribution of variable xi depends on. The term Markov indicates that
this is a proper subset of the variables. A distribution is an MRF with respect to an undirected graph G if

∈ {

}

p(xi

|

x\i) = p(xi

ne (xi))
|

(4.2.11)

where ne (xi) are the neighbouring variables of variable xi, according to the undirected graph G. The
xi in set notation.
notation x\i is shorthand for the set of all variables

excluding variable xi, namely

X

X \

4.2.3 Hammersley-Cliﬀord Theorem

An undirected graph G speciﬁes a set of independence statements. An interesting challenge is to ﬁnd
the most general functional form of a distribution that satisﬁes these independence statements. A trivial
example is the graph x1

x2. From this requirement we must have

x3, from which we have x1

x2

x3
|

⊥⊥

−

−
x2)

p(x1

x2, x3) = p(x1
|

Hence

|

(4.2.12)

(4.2.13)

p(x1, x2, x3) = p(x1

|

x2, x3)p(x2, x3) = p(x1

x2)p(x2, x3) = φ12(x1, x2)φ23(x2, x3)
|

where the φ are potentials.

More generally, for any decomposable graph G, see deﬁnition(6.8), we can start at the edge and work inwards
to reveal that the functional form must be a product of potentials on the cliques of G. For example, for
ﬁg(4.2a), we can start with the variable x1 and the corresponding local Markov statement
x4, x5, x6, x7
|
x2, x3 to write

⊥⊥

x1

@@

p(x1, . . . , x7) = p(x1

|

x2, x3)p(x2, x3, x4, x5, x6, x7)

(4.2.14)

Now we consider x1 eliminated and move to the neighbours of x1, namely x2, x3. The graph speciﬁes that
x1, x2, x3 are independent of x5, x6, x7 given x4:

p(x1, x2, x3

x4, x5, x6, x7) = p(x1, x2, x3
|

|

x4)

(4.2.15)

By summing both sides above over x1 we have that p(x2, x3

x4, x5, x6, x7) = p(x2, x3

x4). Hence
|

|

p(x2, x3, x4, x5, x6, x7) = p(x2, x3

x4, x5, x6, x7)p(x4, x5, x6, x7) = p(x2, x3
|

x4)p(x4, x5, x6, x7)
|

(4.2.16)

and

p(x1, . . . , x7) = p(x1

x2, x3)p(x2, x3
|

x4)p(x4, x5, x6, x7)
|

(4.2.17)

Having eliminated x2, x3, we now move to their neighbour(s) on the remaining graph, namely x4. Continuing
in this way, we necessarily end up with a distribution of the form

p(x1, . . . , x7) = p(x1

x2, x3)p(x2, x3
|

x4)p(x4
|

x5, x6)p(x5, x6
|

|

x7)p(x7)

(4.2.18)

The pattern here is clear and shows that the Markov conditions mean that the distribution is expressible
as a product of potentials deﬁned on the cliques of the graph. That is G
F where F is a factorisation
into clique potentials on G. The converse is easily shown, namely that given a factorisation into clique
F . It is clear that for any decomposable
potentials, the Markov conditions on G are implied. Hence G
G, this always holds since we can always work inwards from the edges of the graph.

⇔

⇒

The Hammersley-Cliﬀord theorem is a stronger result and shows that this factorisation property holds for
any undirected graph, provided that the potentials are positive. For a formal proof, the reader is referred
to [182, 36, 219]. An informal argument can be made by considering a speciﬁc example, and we take the

DRAFT April 24, 2015

63

Markov Networks

4-cycle x1
conditions implied by the graph mean that the distribution must be of the form

x1 from ﬁg(4.1a). The theorem states that for positive potentials φ, the Markov

x2

x4

x3

−

−

−

−

p(x1, x2, x3, x4) = φ12(x1, x2)φ23(x2, x3)φ34(x3, x4)φ41(x4, x1)

(4.2.19)

One may readily verify that for any distribution of this form x1
x2, x4. Consider including an additional
term that links x1 to a variable not a member of the cliques that x1 inhabits. That is we include a term
φ13(x1, x3). Our aim is to show that a distribution of the form

x3
|

⊥⊥

p(x1, x2, x3, x4) = φ12(x1, x2)φ23(x2, x3)φ34(x3, x4)φ41(x4, x1)φ13(x1, x3)

(4.2.20)

cannot satisfy the Markov property x1

x3
|

⊥⊥

x2, x4. To do so we examine

p(x1

x2, x3, x4) =
|

=

(cid:80)

(cid:80)

φ12(x1, x2)φ23(x2, x3)φ34(x3, x4)φ41(x4, x1)φ13(x1, x3)
x1 φ12(x1, x2)φ23(x2, x3)φ34(x3, x4)φ41(x4, x1)φ13(x1, x3)
φ12(x1, x2)φ41(x4, x1)φ13(x1, x3)
x1 φ12(x1, x2)φ41(x4, x1)φ13(x1, x3)

If we assume that the potential φ13 is weakly dependent on x1 and x3,

φ13(x1, x3) = 1 + (cid:15)ψ(x1, x3)

where (cid:15)

(cid:28)

1, then p(x1

x2, x3, x4) is given by
|

(4.2.21)

(4.2.22)

(4.2.23)

(cid:80)

φ12(x1, x2)φ41(x4, x1)
x1 φ12(x1, x2)φ41(x4, x1)

(cid:32)

(cid:80)

(1 + (cid:15)ψ(x1, x3))

1 + (cid:15)

x1 φ12(x1, x2)φ41(x4, x1)ψ(x1, x3)
x1 φ12(x1, x2)φ41(x4, x1)

(cid:80)

(cid:33)−1

(4.2.24)

By expanding (1 + (cid:15)f )−1 = 1

−

(cid:15)f + O (cid:0)(cid:15)2(cid:1) and retaining only terms that are ﬁrst order in (cid:15), we obtain

p(x1

x2, x3, x4) =
|

(cid:80)

φ12(x1, x2)φ41(x4, x1)
x1 φ12(x1, x2)φ41(x4, x1)
(cid:34)

(cid:32)

(cid:80)

1 + (cid:15)

ψ(x1, x3)

−

×

x1 φ12(x1, x2)φ41(x4, x1)ψ(x1, x3)
x1 φ12(x1, x2)φ41(x4, x1)

(cid:80)

(cid:35)(cid:33)

+ O (cid:0)(cid:15)2(cid:1) (4.2.25)

The ﬁrst factor above is independent of x3, as required by the Markov condition. However, for (cid:15)
= 0, the
second term varies as a function of x3. The reason for this is that one can always ﬁnd a function ψ(x1, x3)
for which

ψ(x1, x3)

=

(cid:80)

x1 φ12(x1, x2)φ41(x4, x1)ψ(x1, x3)
x1 φ12(x1, x2)φ41(x4, x1)

(cid:80)

(4.2.26)

since the term ψ(x1, x3) on the left is functionally dependent on x1 whereas the term on the right is not a
function of x1. Hence, the only way we can ensure the Markov condition holds is if (cid:15) = 0, namely that there
is no connection between x1 and x3.

One can generalise this argument to show that if the graph of potentials in the distribution contains a
link which is not present in G, then there is some distribution for which a corresponding Markov condition
cannot hold. Informally, therefore, G

F . The converse F

G is trivial.

⇒

⇒

The Hammersley-Cliﬀord theorem also helps resolve questions as to when a set of positive local conditional
pa (xi)) could ever form a consistent joint distribution p(x1, . . . , xn). Each local conditional
distributions p(xi
|
, so we must include such
pa (xi)) corresponds to a factor on the set of variables
distribution p(xi
}
a term in the joint distribution. The MN can form a joint distribution consistent with the local conditional
distributions if and only if p(x1, . . . , xn) factorises according to

xi, pa (xi)

{

|

p(x1, . . . , xn) =

1
Z

exp

(cid:32)

(cid:88)

−

c

Vc(

c)

X

(cid:33)

64

(4.2.27)

DRAFT April 24, 2015

(cid:54)
(cid:54)
Markov Networks

a

c

e

h

i

j

(a)

b

d

f

k

a

c

e

b

d

f

a

c

e

b

d

f

g

i

(b)

i

(c)

(a): Belief network for
Figure 4.4:
which we are interested in checking
.
b
d, i
conditional independence a
}
| {
(b): Ancestral graph.
(c): Ances-
tral, moralised and separated graph
for a
. There is no path
from a red to green node so a and b
are independent given d, i.

d, i
}

| {

⊥⊥

⊥⊥

b

X

where the sum is over all cliques and Vc(
c) is a real function deﬁned over the variables in the clique
indexed by c. Equation (4.2.27) is equivalent to (cid:81)
c), namely a MN on positive clique potentials.
The graph over which the cliques are deﬁned is an undirected graph constructed by taking each local
conditional distribution p(xi
. This is then repeated over all
the local conditional distributions, see ﬁg(4.3). Note that the HC theorem does not mean that, given a set
of conditional distributions, we can always form a consistent joint distribution from them – rather it states
what the functional form of a joint distribution has to be for the conditionals to be consistent with the joint,
see exercise(4.8).

pa (xi)) and drawing a clique on

xi, pa (xi)
}
{

c φ(

X

|

4.2.4 Conditional independence using Markov networks

,

,

X

For
if
undirected graphs) uses the procedure below (see [78, 183]). See ﬁg(4.4) for an example.

each being collections of variables, in section(3.3.4) we discussed an algorithm to determine
for belief networks. An alternative and more general method (since it handles directed and

Z
X ⊥⊥ Y | Z

Y

Procedure 4.2 (Ascertaining independence in Markov and belief networks). For Markov Networks only
the ﬁnal separation criterion needs to be applied:

Ancestral Graph Identify the ancestors

A
remove all other nodes which are not in

of the nodes

. Retain the nodes

, together with any edges in or out of such nodes.

X ∪ Y ∪ Z

X ∪ Y ∪ Z

but

A

Moralisation Add a link between any two remaining nodes which have a common child, but are not already

connected by an arrow. Then remove remaining arrowheads.

Separation Remove links neighbouring

. In the undirected graph so constructed, look for a path which

joins a node in

to one in

Y

X

Z

. If there is no such path deduce that

.

X ⊥⊥ Y| Z

Note that the ancestral step in procedure(4.2) for belief networks is intuitive since, given a set of nodes
form a contribution to the distribution of the form
simply has the eﬀect of removing these variables from the

), so that summing over

, the remaining nodes

and their ancestors

)p(

A

D

,

,

D

X
p(
D|X
DAG.

A

X

A

4.2.5 Lattice Models

Undirected models have a long history in diﬀerent branches of science, especially statistical mechanics on
lattices and more recently as models in visual processing in which the models encourage neighbouring vari-
ables to be in the same states[36, 37, 116].

DRAFT April 24, 2015

65

Consider a model in which our desire is that states of the binary valued variables
x1, . . . , x9, arranged on a lattice (right) should prefer their neighbouring variables
to be in the same state

p(x1, . . . , x9) =

1
Z

(cid:89)

i∼j

φij(xi, xj)

(4.2.28)

where i
∼
undirected graph.

j denotes the set of indices where i and j are neighbours in the

The Ising model

Markov Networks

x1

x4

x7

x2

x5

x8

x3

x6

x9

A set of potentials for equation (4.2.28) that encourages neighbouring variables to have the same state is

φij(xi, xj) = e− 1

2T (xi−xj )2

,

xi

∈ {−

1, +1
}

(4.2.29)

This corresponds to a well-known model of the physics of magnetic systems, called the Ising model which
consists of ‘mini-magnets’ which prefer to be aligned in the same state, depending on the temperature
T . For high T the variables behave independently so that no global magnetisation appears. For low T ,
there is a strong preference for neighbouring mini-magnets to become aligned, generating a strong macro-
magnet. Remarkably, one can show that, in a very large two-dimensional lattice, below the so-called Curie
temperature, Tc
1 variables), the system admits a phase change in that a large fraction of the
variables become aligned – above Tc, on average, the variables are unaligned. This is depicted in ﬁg(4.5)
(cid:12)
(cid:12)
where M =
(cid:12) /N is the average alignment of the variables. That this phase change happens for
non-zero temperature has driven considerable research in this and related areas[41]. Global coherence eﬀects
such as this that arise from weak local constraints are present in systems that admit emergent behaviour .
Similar local constraints are popular in image restoration algorithms to clean up noise, under the assumption
that noise will not show any local spatial coherence, whilst ‘signal’ will.

(cid:12)
(cid:80)N
(cid:12)
(cid:12)

2.269 (for

i=1 xi

±

≈

Example 4.2 (Cleaning up images).

Consider a binary image deﬁned on a set of pixels xi
observe a noise corrupted version yi of each pixel xi, in which the state of yi
1, +1
}
is opposite to xi with some probability. Here the ﬁlled nodes indicate observed noisy
pixels and the unshaded nodes the latent clean pixels. Our interest is to ‘clean up’ the
observed dirty image

, and ﬁnd the most likely joint clean image

, i = 1, . . . , D. We

1, +1
}

∈ {−

∈ {−

.

Y

X

A model for this situation is

p(

X

,

Y

) =

1
Z

(cid:34) D
(cid:89)

i=1

φ(xi, yi)

(cid:35) 


(cid:89)

i∼j



ψ(xi, xj)

 ,

φ(xi, yi) = eβxiyi, ψ(xi, xj) = eαxixj

(4.2.30)

∼

here i
j indicates the set of latent variables that are neighbours. The potential φ encourages the noisy
and clean pixel to be in the same state. Similarly, the potential ψ(xi, xj) encourages neighbouring pixels to
be in the same state. To ﬁnd the most likely clean image, we need to compute

argmax
X

p(

X |Y

) = argmax

p(

X

,

)

Y

X

(4.2.31)

This is a computationally diﬃcult task but can be approximated using iterative methods, see section(28.9).

Figure 4.5: Onsager magnetisation. As the temperature T decreases
towards the critical temperature Tc a phase transition occurs in which
a large fraction of the variables become aligned in the same state.

66

DRAFT April 24, 2015

00.511.5200.51T/TcMChain Graphical Models

a

c

(a)

b

d

a

b

cd

(b)

a

h

b

e

g

d

f

c

c

aedf h

bg

(c)

(d)

Figure 4.6: Chain graphs. The chain components are identiﬁed by deleting the directed edges and identifying
the remaining connected components. (a): Chain components are (a),(b),(c, d), which can be written as a
BN on the cluster variables in (b). (c): Chain components are (a, e, d, f, h), (b, g), (c), which has the cluster
BN representation (d).

On the left is the clean image, from which a noisy corrupted image
is formed (middle). The most likely
restored image is given on the right. See demoMRFclean.m. Note that the parameter β is straightforward
2β), so that β =
to set, given knowledge of the corruption probability pcorrupt, since p(yi
1
2 σ−1(pcorrupt). Setting α is more complex since relating p(xi = xj) to α is not straightforward, see

xi) =
|

= xi

σ (

−

Y

−
section(28.4.1). In the demonstration we set α = 10, pcorrupt = 0.15.

@@
@@

4.3 Chain Graphical Models

Chain Graphs (CGs) contain both directed and undirected links. To develop the intuition, consider ﬁg(4.6a).
The only terms that we can unambiguously specify from this depiction are p(a) and p(b) since there is no
mixed interaction of directed and undirected edges at the a and b vertices. By probability, therefore, we
must have

p(a, b, c, d) = p(a)p(b)p(c, d
|

a, b)

Looking at the graph, we might expect the interpretation to be

b)
a)p(d
a, b) = φ(c, d)p(c
p(c, d
|
|
|

However, to ensure normalisation, and also to retain generality, we interpret this as

b)φ(a, b), with φ(a, b)
a)p(d
a, b) = φ(c, d)p(c
p(c, d
|
|
|

≡





(cid:88)

c,d



−1

a)p(d
φ(c, d)p(c
|
|

b)



This leads to the interpretation of a CG as a DAG over the chain components see below.

Deﬁnition 4.8 (Chain Component). The chain components of a graph G are obtained by :

1. Forming a graph G(cid:48) with directed edges removed from G.

2. Then each connected component in G(cid:48) constitutes a chain component.

DRAFT April 24, 2015

(4.3.1)

(4.3.2)

(4.3.3)

67

(cid:54)
Each chain component represents a distribution over the variables of the component, conditioned on the
parental components. The conditional distribution is itself a product over the cliques of the undirected
component and moralised parental components, including also a factor to ensure normalisation over the
chain component.

Chain Graphical Models

Deﬁnition 4.9 (Chain Graph distribution). The distribution associated with a chain graph G is found by
ﬁrst identifying the chain components, τ

and their associated variables

τ . Then

X

@@

p(x) =

(cid:89)

τ

p (

X

τ

pa (
|

X

τ ))

and

p (

τ
X

pa (
|

X

τ ))

(cid:89)

∝

d∈Dτ

p(xd

pa (xd))
|

(cid:89)

c∈Cτ

c)

φ (

X

(4.3.4)

(4.3.5)

@@

C

τ denotes the union of the cliques in component τ

where
each clique;
proportionality factor is determined implicitly by the constraint that the distribution sums to 1.

τ is the set of variables in component τ that correspond to directed terms p(xd

with φ being the associated functions deﬁned on
pa (xd)). The

D

|

@@

BNs are CGs in which the connected components are singletons. MNs are CGs in which the chain components
are simply the connected components of the undirected graph. CGs can be useful since they are more
expressive of CI statements than either belief networks or Markov networks alone. The reader is referred to
[182] and [106] for further details.

Example 4.3 (Chain graphs are more expressive than Belief or Markov networks). Consider the chain
graph in ﬁg(4.7a), which has chain component decomposition

p(a, b, c, d, e, f ) = p(a)p(b)p(c, d, e, f

a, b)

|

where

p(c, d, e, f

a, b) =
|

a)φ(c, e)φ(e, f )φ(d, f )
p(c
|

p(d

b)φ(a, b)
|

with the normalisation requirement

φ(a, b)

≡





(cid:88)

c,d,e,f

a)φ(c, e)φ(e, f )φ(d, f )
p(c
|

p(d
|

b)





−1

The marginal p(c, d, e, f ) is given by

φ(c, e)φ(e, f )φ(d, f )

(cid:88)

a,b
(cid:124)

φ(a, b)p(a)p(b)

a)
p(c
|

b)
p(d
|

(cid:123)(cid:122)
φ(c,d)

(cid:125)

(4.3.6)

(4.3.7)

@@
@@

(4.3.8)

@@
@@

(4.3.9)

@@
@@

Since the marginal distribution of p(c, d, e, f ) is an undirected 4-cycle, no DAG can express the CI statements
contained in the marginal p(c, d, e, f ). Similarly no undirected distribution on the same skeleton as ﬁg(4.7a)
could express that a and b are independent (unconditionally), i.e. p(a, b) = p(a)p(b).

68

DRAFT April 24, 2015

Factor Graphs

a

c

e

b

d

f

c

e

d

f

c

e

d

f

(a)

(b)

(c)

b

| ∅

⊥⊥

and d

Figure 4.7: The CG (a) expresses a
⊥⊥
(c, f ). No directed graph could express both these
e
|
conditions since the marginal distribution p(c, d, e, f )
is an undirected four cycle, (b). Any DAG on a 4 cycle
must contain a collider, as in (c) and therefore express
a diﬀerent set of CI statements than (b). Similarly, no
connected Markov network can express unconditional
independence and hence (a) expresses CI statements
that no belief network or Markov network alone can
express.

4.4 Factor Graphs

Factor Graphs (FGs) are mainly used as part of inference algorithms1.

Deﬁnition 4.10 (Factor Graph). Given a function

f (x1, . . . , xn) =

(cid:89)

i

ψi (

i)

X

(4.4.1)

The FG has a node (represented by a square) for each factor ψi, and a variable node (represented by
i an undirected link is made between factor ψi and variable xj.
a circle) for each variable xj. For each xj

∈ X

When used to represent a distribution

p(x1, . . . , xn) =

1
Z

(cid:89)

i

ψi (

i)

X

(4.4.2)

++

a normalisation constant Z = (cid:80)
X

(cid:81)

i ψi (

X

i) is assumed.

Here

X

represents all variables in the distribution.

pa (xi)), we may use directed links from the
For a factor ψi (
|
parents to the factor node, and a directed link from the factor node to the child xi. This has the same
structure as an (undirected) FG, but preserves the information that the factors are distributions.

i) which is a conditional distribution p(xi

X

Factor graphs are useful since they can preserve more information about the form of the distribution than
either a belief network or a Markov network (or chain graph) can do alone. Consider the distribution

p(a, b, c) = φ(a, b)φ(a, c)φ(b, c)

(4.4.3)

Represented as a MN, this must have a single clique, as given in ﬁg(4.8c). However, ﬁg(4.8c) could equally
represent some unfactored clique potential φ(a, b, c) so that the factorised structure within the clique is lost.
In this sense, the FG representation in ﬁg(4.8b) more precisely conveys the form of distribution equation
(4.4.3). An unfactored clique potential φ(a, b, c) is represented by the FG ﬁg(4.8a). Hence diﬀerent FGs
can have the same MN since information regarding the structure of the clique potential is lost in the MN.
Similarly, for a belief network, as in ﬁg(4.8d) one can represent this using a standard undirected FG, although
more information about the independence is preserved by using a directed FG representation, as in ﬁg(4.8e).
One can also consider partially directed FGs which contain both directed and undirected edges; this requires
a speciﬁcation of how the structure is normalised, one such being to use an approach analogous to the chain
graph – see [103] for details.

1Formally a FG is an alternative graphical depiction of a hypergraph[86] in which the vertices represent variables, and a
hyperedge a factor as a function of the variables associated with the hyperedge. A FG is therefore a hypergraph with the
additional interpretation that the graph represents a function deﬁned as products over the associated hyperedges. Many thanks
to Robert Cowell for this observation.

DRAFT April 24, 2015

69

Expressiveness of Graphical Models

a

a

a

a

a

c

b

c

b

c

b

c

b

c

b

(a)

(b)

(c)

(d)

(e)

@@

@@
@@
@@

@@
@@

@@
@@
@@
@@
@@
@@

(a): φ(a, b, c).

(b): φ(a, b)φ(b, c)φ(c, a).

(c): φ(a, b, c). Both (a) and (b) have the same
Figure 4.8:
undirected graphical model, (c). (d): (a) is an undirected FG of (d). (e): Directed FG of the BN in (d).
A directed factor represents a term p(children
parents). The advantage of (e) over (a) is that information
|
regarding the marginal independence of variables b and c is clear from graph (e), whereas one could only
ascertain this by examination of the numerical entries of the factors in graph (a).

4.4.1 Conditional independence in factor graphs

Conditional independence questions can be addressed using a rule which works with directed, undirected
and partially directed FGs[103]. To determine whether two variables are independent given a set of condi-
tioned variables, consider all paths connecting the two variables. If all paths are blocked, the variables are
conditionally independent. A path is blocked if one or more of the following conditions is satisﬁed:

•

•

One of the variables in the path is in the conditioning set.

One of the variables or factors in the path has two incoming edges that are part of the path (variable
or factor collider), and neither the variable or factor nor any of its descendants are in the conditioning
set.

4.5 Expressiveness of Graphical Models

It is clear that directed distributions can be represented as undirected distributions since one can asso-
ciate each (normalised) factor of the joint distribution with a potential. For example, the distribution
p(a
c)p(c), with Z = 1.
b) and φ(b, c) = p(b
c)p(c) can be factored as φ(a, b)φ(b, c), where φ(a, b) = p(a
b)p(b
|
|
|
|
Hence every belief network can be represented as some MN by simple identiﬁcation of the factors in the
distributions. However, in general, the associated undirected graph (which corresponds to the moralised
directed graph) will contain additional links and independence information can be lost. For example, the
a, b)p(a)p(b) is a single clique φ(a, b, c) from which one cannot graphically infer that a
MN of p(c
|

⊥⊥

b.

The converse question is whether every undirected model can be represented by a BN with a readily derived
link structure. Consider the example in ﬁg(4.9). In this case, there is no directed model with the same
link structure that can express the (in)dependencies in the undirected graph. Naturally, every probability
distribution can be represented by some BN though it may not necessarily have a simple structure and
be a ‘fully connected’ cascade style graph. In this sense the DAG cannot always graphically represent the
independence properties that hold for the undirected distribution.

Deﬁnition 4.11 (Independence Maps). A graph is an independence map (I-map) of a given distribution P
if every conditional independence statement that one can derive from the graph G is true in the distribution
P . That is

X ⊥⊥ Y| Z G ⇒ X ⊥⊥ Y| Z P

for all disjoint sets

,

,

Y

X

.

Z

(4.5.1)

70

DRAFT April 24, 2015

Expressiveness of Graphical Models

b

b

a

c

a

c

d

(a)

d

(b)

Figure 4.9: (a): An undirected model for which we wish to ﬁnd a
directed equivalent. (b): Every DAG with the same structure as
the undirected model must have a situation where two arrows will
point to a node, such as node d (otherwise one would have a cyclic
graph). Summing over the states of variable d will leave a DAG
on the variables a, b, c with no link between a and c. This cannot
represent the undirected model since when one marginalises over
d this adds a link between a and c.

Similarly, a graph is a dependence map (D-map) of a given distribution P if every conditional independence
statement that one can derive from P is true in the graph G. That is

X ⊥⊥ Y| Z G ⇐ X ⊥⊥ Y| Z P

for all disjoint sets

,

,

Y

X

.

Z

A graph G which is both an I-map and a D-map for P is called a perfect map and

X ⊥⊥ Y| Z G ⇔ X ⊥⊥ Y| Z P

(4.5.2)

(4.5.3)

for all disjoint sets
expressible in the graph G are consistent with P and vice versa.

,
Z

,
Y

X

. In this case, the set of all conditional independence and dependence statements

Note that by contraposition, a dependence map is equivalent to

X (cid:62)(cid:62)Y| Z G ⇒ X (cid:62)(cid:62)Y| Z P

(4.5.4)

meaning that if

and

Y

X

are graphically dependent given

Z

, then they are dependent in the distribution.

One way to think about this is to take a distribution P and write out a list
statements. For a graph G, one writes a list of all the possible independence statements

P of all the independence
G. Then:

L

L

P

L
L
L

P
P =

⊆ L
⊇ L
L

G Dependence Map (D-map)
G Independence Map (I-map)
G Perfect Map

(4.5.5)

In the above we assume the statement l is contained in
independence statements in

.
L

L

if it is consistent with (can be derived from) the

One can also discuss whether or not a distribution class has an associated map. That is whether or not all
numerical instances of distributions consistent with the speciﬁed form obey the constraints required for the
map. To do so we take any numerical instance of a distribution Pi consistent with a given class P and write
out a list
Pi of all the
lists from all possible distribution instances. This list is then used in equation (4.5.5) to determine if there
is an associated map. For example the distribution class

Pi of all the independence statements. One then takes the intersection

P =

i
∩

L

L

L

p(x, y, z) = p(z

x, y)p(x)p(y)
|

(4.5.6)

has a directed perfect map x
fully connected so that
L
independent, a statement which is not contained in
perfect undirected map for the class represented in equation (4.5.6).

y. However, the undirected graph for the class equation (4.5.6) is
G is empty. For any distribution consistent with equation (4.5.6) x and y are
G – hence there is no undirected D-map and hence no

←

→

L

z

Example 4.4. Consider the distribution (class) deﬁned on variables t1, t2, y1, y2[249]:

p(t1, t2, y1, y2) = p(t1)p(t2)

(cid:88)

h

p(y1

t1, h)p(y2
|

t2, h)p(h)
|

DRAFT April 24, 2015

(4.5.7)

71

In this case the list of all independence statements (for all distribution instances consistent with p) is

Expressiveness of Graphical Models

P =

L

y1
{

t2
|

⊥⊥

t1, y2

t1
|

⊥⊥

t2, t2

t1

}

⊥⊥

Consider the graph of the BN

p(y2

y1, t1, t2)p(y1
|

|

t1)p(t1)p(t2)

For this we have

G =

L

y1

t2
|

⊥⊥

{

t1, t2

t1

}

⊥⊥

(4.5.8)

@@

(4.5.9)

@@

(4.5.10)

@@

G

L

Hence
true for the distribution class in equation (4.5.7). However, it is not a D-MAP since
no perfect MAP (a BN or a MN) can represent (4.5.7).

P so that the BN is an I-MAP for (4.5.7) since every independence statement in the BN is
G. In this case

⊂ L

(cid:54)⊆ L

P
L

Remark 4.1 (Forcing dependencies?). Whilst graphical models as we have deﬁned them ensure speciﬁed
independencies, they seem to be inappropriate for ensuring speciﬁed dependencies. Consider the undirected
z. Graphically this expresses that x and z are dependent. However, there are numerical
graph x
instances of distributions for which this does not hold, for example

−

−

y

p(x, y, z) = φ(x, y)φ(y, z)/Z1

(4.5.11)

with φ(x, y) = const. One might complain that this is a pathological case since any graphical representation
of this particular instance contains no link between x and y. Maybe one should therefore ‘force’ potentials
to be non-trivial functions of their arguments and thereby ensure dependency? Consider

φ(x, y) =

x
y

,

φ(y, z) = yz

(4.5.12)

In this case both potentials are non-trivial in the sense that they are truly functionally dependent on their
arguments. Hence, the undirected network contains ‘genuine’ links x

z. Nevertheless,

y and y

−

−

p2(x, y, z) = φ(x, y)φ(y, z)/Z2

x
y

∝

yz = xz

(4.5.13)

x

∝

⇒

xz

⊥⊥

Hence p2(x, z)
z. So ‘forcing’ local non-trivial functions does not guarantee dependence of
path-connected variables. In this case, the algebraic cancellation is clear and the problem is again rather
trivial since for p2, x
z (see however, remark(1.2)). However,
there may be cases where such algebraic simpliﬁcations are highly non-trivial, though nevertheless true.
See, for example, exercise(3.17) in which we construct p(x, y, z)
z, yet
x

z, so one might assume that x

φ(x, y)φ(y, z) for which x

y and y

y and y

⊥⊥

⊥⊥

⊥⊥

(cid:62)(cid:62)

(cid:62)(cid:62)

z.

∝

⊥⊥

4.6 Summary

•

•

•

•

72

Graphical modelling is the discipline of representing probability models graphically.

Belief networks intuitively describe which variables ‘causally’ inﬂuence others and are represented using
directed graphs.

A Markov network is represented by an undirected graph.

Intuitively, linked variables in a Markov network are graphically dependent, describing local cliques of
graphically dependent variables.

DRAFT April 24, 2015

Exercises

•

•

•

•

Markov networks are historically important in physics and may be used to understand how global collabo-
rative phenomena can emerge from only local dependencies.

Graphical models are generally limited in their ability to represent all the possible logical consequences of
a probabilistic model.

Some special probabilistic models can be ‘perfectly’ mapped graphically.

Factor graphs describe the factorisation of functions and are not necessarily related to probability distribu-
tions.

A detailed discussion of the axiomatic and logical basis of conditional independence is given in [47] and
[280].

4.7 Code

condindep.m: Conditional Independence test p(X, Y

Z) = p(X
|

|

Z)p(Y

Z)?
|

4.8 Exercises

Exercise 4.1.

1. Consider the pairwise Markov network,

p(x) = φ(x1, x2)φ(x2, x3)φ(x3, x4)φ(x4, x1)

Express in terms of φ the following:

p(x1

x2, x4),
|

p(x2

x1, x3),
|

p(x3

|

x2, x4),

p(x4

x1, x3)
|

2. For a set of local distributions deﬁned as

(4.8.1)

(4.8.2)

|

p1(x1

x2, x4),

x2, x4),
|
is it always possible to ﬁnd a joint distribution p(x1, x2, x3, x4) consistent with these local conditional
distributions?

x1, x3)
|

x1, x3),

(4.8.3)

p3(x3

p4(x4

p2(x2

|

Exercise 4.2. Consider the Markov network

p(a, b, c) = φab(a, b)φbc(b, c)

(4.8.4)

Nominally, by summing over b, the variables a and c are dependent. For binary b, explain a situation in
which this is not the case, so that marginally, a and c are independent.

Exercise 4.3. Show that for the Boltzmann machine deﬁned on binary variables xi with

p(x) =

1
Z(W, b)

(cid:16)

exp

xTWx + xTb

(cid:17)

one may assume, without loss of generality, W = WT.

Exercise 4.4.

The restricted Boltzmann machine (or Harmonium[269]) is a constrained
Boltzmann machine on a bipartite graph, consisting of a layer of visible
variables v = (v1, . . . , vV ) and hidden variables h = (h1, . . . , hH ):

h1

h2

p(v, h) =

1
Z(W, a, b)

(cid:16)

(cid:17)
vTWh + aTv + bTh

exp

(4.8.6)

v1

v2

v3

All variables are binary taking states 0, 1.

DRAFT April 24, 2015

(4.8.5)

73

1. Show that the distribution of hidden units conditional on the visible units factorises as

v) =

p(h
|

(cid:89)

i

p(hi

v),
|

where σ(x) = ex/(1 + ex).



v) = σ
with p(hi = 1
|

bi +



Wjivj



(cid:88)

j

2. By symmetry arguments, write down the form of the conditional p(v

3.

Is p(h) = (cid:81)

i p(hi)?

h).

|

4. Can the partition function Z(W, a, b) be computed eﬃciently for the RBM?

Exercise 4.5. You are given that

x

y
|

⊥⊥

(z, u) ,

u

z
| ∅

⊥⊥

Exercises

(4.8.7)

@@

(4.8.8)

Derive the most general form of probability distribution p(x, y, z, u) consistent with these statements. Does
this distribution have a simple graphical model?

Exercise 4.6. The undirected graph
clockwise around the pentagon with potentials φ(xi, xj). Show that the joint distribution can be written as

represents a Markov network with nodes x1, x2, x3, x4, x5, counting

p(x1, x2, x3, x4, x5) =

p(x1, x2, x5)p(x2, x4, x5)p(x2, x3, x4)
p(x2, x5)p(x2, x4)

(4.8.9)

and express the marginal probability tables explicitly as functions of the potentials φ(xi, xj).

Exercise 4.7.

Consider the belief network on the right.

1. Write down a Markov network of p(x1, x2, x3).

2. Is your Markov network a perfect map of p(x1, x2, x3)?

h1

h2

x1

x2

x3

Exercise 4.8. Two research labs work independently on the relationship between discrete variables x and
y. Lab A proudly announces that they have ascertained distribution pA(x
y) from data. Lab B proudly
|
announces that they have ascertained pB(y

x) from data.
|

1. Is it always possible to ﬁnd a joint distribution p(x, y) consistent with the results of both labs?
2. Is it possible to deﬁne consistent marginals p(x) and p(y), in the sense that p(x) = (cid:80)

y)p(y)
y pA(x
|
x)p(x)? If so, explain how to ﬁnd such marginals. If not, explain why not.
|

and p(y) = (cid:80)

x pB(y

Exercise 4.9. Research lab A states its ﬁndings about a set of variables x1, . . . , xn as a list LA of conditional
independence statements. Lab B similarly provides a list of conditional independence statements LB.

1. Is it always possible to ﬁnd a distribution which is consistent with LA and LB?

2. If the lists also contain dependence statements, how could one attempt to ﬁnd a distribution that is

consistent with both lists?

Exercise 4.10.
Consider the distribution

p(x, y, w, z) = p(z

w)p(w

|

|

x, y)p(x)p(y)

(4.8.10)

1. Write p(x

2. Write p(y

74

z) using a formula involving (all or some of ) p(z
|
z) using a formula involving (all or some of ) p(z
|

w), p(w
|
w), p(w
|

|

x, y), p(x), p(y).

|
x, y), p(x), p(y).

DRAFT April 24, 2015

Exercises

3. Using the above results, derive an explicit condition for x

distribution.

z and explain if this is satisﬁed for this

y
|

⊥⊥

Exercise 4.11. Consider the distribution

p(t1, t2, y1, y2, h) = p(y1

y2, t1, t2, h)p(y2
|

t2, h)p(t1)p(t2)p(h)
|

1. Draw a belief network for this distribution.

2. Does the distribution

p(t1, t2, y1, y2) =

(cid:88)

h

p(y1

y2, t1, t2, h)p(y2
|

|

t2, h)p(t1)p(t2)p(h)

have a perfect map belief network?

3. Show that for p(t1, t2, y1, y2) as deﬁned above t1

.

y2

| ∅

⊥⊥

Exercise 4.12. Consider the distribution

(4.8.11)

(4.8.12)

p(a, b, c, d) = φab(a, b)φbc(b, c)φcd(c, d)φda(d, a)

(4.8.13)

where the φ are potentials.

1. Draw a Markov network for this distribution.

2. Explain if the distribution can be represented as a (‘non-complete’) belief network.

3. Derive explicitly if a

.

c
| ∅

⊥⊥

Exercise 4.13. Show how for any singly-connected Markov network, one may construct a Markov equivalent
belief network.

Exercise 4.14. Consider a pairwise binary Markov network deﬁned on variables si
with p(s) = (cid:81)
to translate such a Markov network into a Boltzmann machine.

, i = 1, . . . , N ,
}
is a given edge set and the potentials φij are arbitrary. Explain how

ij∈E φij(si, sj), where

∈ {

0, 1

E

++

Exercise 4.15.
would give rise to a set of speciﬁed consistent distributions

Our interest here is to show that it is not always possible to ﬁnd a joint distribution p that
q
{

on subsets of the variables.

}

1. We ﬁrst wish to construct a set of distributions q12(x1, x2), q13(x1, x3), q23(x2, x3) on binary variables

xi

0, 1
}

∈ {

that are ‘marginally’ consistent; that is

(cid:88)

x2
(cid:88)

x1
(cid:88)

x1

q12(x1, x2) =

q12(x1, x2) =

q13(x1, x3) =

(cid:88)

x3
(cid:88)

x3
(cid:88)

x2

q13(x1, x3)

q23(x2, x3)

q23(x2, x3)

with all the q being distributions (non negative and summing to 1). By writing q12(x1 = 0, x2 = 0) = y1,
q12(x1 = 0, x2 = 1) = y2, etc. , show that the above equations can be represented as a linear system

My = c,

yi

0

≤

≤

1

where M is a suitably deﬁned 9
×
(or otherwise), ﬁnd a set of marginally consistent distributions q.

12 matrix. Hence by solving this system using linear programming

DRAFT April 24, 2015

75

Exercises

2. Given a set of marginally consistent q, our interest is to see if we can ﬁnd a joint distribution p which

would give rise to these marginals q. That is

(cid:88)

x1

p(x1, x2, x3) = q23(x2, x3),

(cid:88)

x2

p(x1, x2, x3) = q13(x1, x3),

(cid:88)

x3

p(x1, x2, x3) = q12(x1, x2)

Show that, by writing the 8 states of p as z1, . . . , z8, the above can be expressed as the linear system

Az = y,

zi

0

≤

≤

1,

(cid:88)

i

zi = 1

where A is a suitably deﬁned 12
not always possible to ﬁnd a joint distribution z that would give rise to these marginals.

8 matrix. For a marginally consistent y show numerically that it is

×

The set of marginals consistent with a distribution p is called the marginal polytope of p. The question we
are therefore asking here is whether a given marginal set q is in the marginal polytope of p. Whilst this is
straightforward to solve (using linear-programming for example), for an n-variable system the size of the
linear system will be exponential in n, thus resulting in a computationally hard problem.

This issue is important for at least two reasons: (i) Imagine that diﬀerent research labs are asked to examine
diﬀerent aspects of a problem, each lab summarising their results as a distribution on a subset of the variables.
The question is whether there exists a single joint distribution that is consistent with all these marginals.
(ii) In certain deterministic approximation schemes (see section(28.7.2)) the objective depends only on a set
of marginals and the question is how to characterise the marginal polytope. As this question intimates, this
is generally very diﬃcult. However, as we will see in chapter(6), as least for singly-connected structures,
representing a distribution in terms of locally consistent marginals is straightforward.

Exercise 4.16. The question concerns cleaning up the binary image given in the ﬁle xnoisy.mat. The
objective function to maximise is

++

Wi,jI [xi = xj] +

(cid:88)

i,j

(cid:88)

i

bixi,

xi

0, 1

∈ {

}

where Wi,j = 10 for neighbouring pixels (up, down, left, right) in the image and Wi,j = 0 otherwise. Using
y to represent the noisy image, bi = 2yi. Plot the ﬁnal cleaned up image x and give the maximum objective
function found for this cleaned up image.

76

DRAFT April 24, 2015

CHAPTER 5

Eﬃcient Inference in Trees

In previous chapters we discussed how to set up models. Inference then corresponds to operations such as
summing over subsets of variables. In machine learning and related areas we will often deal with distributions
containing hundreds of variables. In general inference is computationally very expensive and it is useful to
understand for which graphical structures this could be cheap in order that we may make models which we
can subsequently compute with. In this chapter we discuss inference in a cheap case, namely trees, which
has links to classical algorithms in many diﬀerent ﬁelds from computer science (dynamic programming) to
physics (transfer matrix methods).

5.1 Marginal Inference

Given a distribution p(x1, . . . , xn), inference is the process of computing functions of the distribution.
Marginal inference is concerned with the computation of the distribution of a subset of variables, possibly
conditioned on another subset. For example, given a joint distribution p(x1, x2, x3, x4, x5) and evidence
x1 = tr, a marginal inference calculation is

p(x5

|

x1 = tr)

(cid:88)

∝

x2,x3,x4

p(x1 = tr, x2, x3, x4, x5).

(5.1.1)

Marginal inference for discrete models involves summation and will be the focus of our development. In
principle the algorithms carry over to continuous variable models although the lack of closure of most
continuous distributions under marginalisation (the Gaussian being a notable exception) can make the
direct transference of these algorithms to the continuous domain problematic. The focus here is on eﬃcient
inference algorithms for marginal inference in singly connected structures. An eﬃcient algorithm for multiply
connected graphs will be considered in chapter(6).

5.1.1 Variable elimination in a Markov chain and message passing

A key concept in eﬃcient inference is message passing in which information from the graph is summarised
by local edge information. To develop this idea, consider the four variable Markov chain (Markov chains are

a

b

c

d

Figure 5.1: A Markov chain is of the form p(xT ) (cid:81)T −1
xt+1)
for some assignment of the variables to labels xt. Variable Elimi-
nation can be carried out in time linear in the number of variables
in the chain.

t=1 p(xt

|

77

discussed in more depth in section(23.1))

p(a, b, c, d) = p(a

d)p(d)
c)p(c
b)p(b
|
|
|

Marginal Inference

(5.1.2)

as given in ﬁg(5.1), for which our task is to calculate the marginal p(a). For simplicity, we assume that each
of the variables has domain
{
(cid:88)

0, 1
}

. Then

(cid:88)

p(a = 0) =

p(a = 0, b, c, d) =

d)p(d) (5.1.3)
c)p(c
b)p(b
p(a = 0
|
|
|

b∈{0,1},c∈{0,1},d∈{0,1}

b∈{0,1},c∈{0,1},d∈{0,1}

We could carry out this computation by simply summing each of the probabilities for the 2
×
states of the variables b, c and d. This would therefore require 7 addition-of-two-numbers calls.

2

×

2 = 8

A more eﬃcient approach is to push the summation over d as far to the right as possible:

p(a = 0) =

(cid:88)

b∈{0,1},c∈{0,1}

c)
b)p(b
p(a = 0
|
|

(cid:88)

d∈{0,1}
(cid:124)

d)p(d)
p(c
|

(cid:123)(cid:122)
γd(c)

(cid:125)

(5.1.4)

where γd (c) is a (two state) potential. Deﬁning γd (c) requires two addition-of-two-numbers calls, one call
for each state of c. Similarly, we can distribute the summation over c as far to the right as possible:

p(a = 0) =

(cid:88)

b∈{0,1}

b)
p(a = 0
|

(cid:88)

c∈{0,1}
(cid:124)

c)γd (c)
p(b
|

(cid:123)(cid:122)
γc(b)

(cid:125)

Then, ﬁnally,

p(a = 0) =

(cid:88)

b∈{0,1}

b)γc (b)
p(a = 0
|

(5.1.5)

(5.1.6)

By distributing the summations we have made
1 = 5 addition-of-two-numbers calls, compared to
−
23
1 = 7 from the naive approach. Whilst this saving may not appear much, the important point is that
the number of computations for a chain of length T + 1 would be linear, 2T , as opposed to exponential,
2T

1 for the naive approach.

×

−

3

2

−

@@

This procedure is called variable elimination since each time we sum over the states of a variable we elimi-
nate it from the distribution. We can always perform variable elimination in a chain eﬃciently since there is
a natural way to distribute the summations, working inwards from the edges. Note that in the above case,
the potentials are in fact always distributions – we are just recursively computing the marginal distribution
of the right leaf of the chain.

One can view the elimination of a variable as passing a message (information) to a neighbouring node on
the graph. We can calculate a univariate-marginal of any tree (singly connected graph) by starting at a
leaf of the tree, eliminating the variable there, and then working inwards, nibbling oﬀ each time a leaf of
the remaining tree. Provided we perform elimination from the leaves inwards, then the structure of the
remaining graph is simply a subtree of the original tree, albeit with the conditional probability table entries
modiﬁed. This is guaranteed to enable us to calculate any marginal p(xi) using a number of summations
which scales linearly with the number of variables in the tree.

Finding conditional marginals for a chain

Consider the following inference problem, ﬁg(5.1) : Given

p(a, b, c, d) = p(a

d)p(d),
c)p(c
b)p(b
|
|
|

a). This can be computed using
ﬁnd p(d
|
a)
p(d
|

p(a, b, c, d) =

p(a

(cid:88)

(cid:88)

∝

b)p(b
|
|

b,c

b,c

d)p(d) =
c)p(c
|

(cid:88)

(cid:88)

c

b

(cid:124)

p(a

c)
b)p(b
|
|
(cid:123)(cid:122)
γb(c)

(cid:125)

(5.1.7)

d)p(d)

p(c
|

≡

γc (d)

(5.1.8)

78

DRAFT April 24, 2015

Marginal Inference

The missing proportionality constant is found by repeating the computation for all states of variable d.
a) = kγc (d), where γc (d) is the unnormalised result of the summation, we can use
Since we know that p(d
|
the fact that (cid:80)

a) = 1 to infer that k = 1/ (cid:80)
d p(d
|

d γc (d).

In this example, the potential γb (c) is not a distribution in c, nor is γc (d). In general, one may view variable
elimination as the passing of messages in the form of potentials from nodes to their neighbours. For belief
networks variable elimination passes messages that are distributions when following the direction of the
edge, and non-normalised potentials when passing messages against the direction of the edge.

Remark 5.1 (Variable elimination in trees as matrix multiplication). Variable elimination is related to the
associativity of matrix multiplication. For equation (5.1.2) above, we can deﬁne matrices

[Mab]i,j = p(a = i
[Mcd]i,j = p(c = i

b = j),
|
d = j),
|

[Mbc]i,j = p(b = i
|
[Md]i = p(d = i),

c = j),
[Ma]i = p(a = i)

Then the marginal Ma can be written

Ma = MabMbcMcdMd = Mab(Mbc(McdMd))

(5.1.9)

(5.1.10)

since matrix multiplication is associative. This matrix formulation of calculating marginals is called the
transfer matrix method, and is particularly popular in the physics literature[26].

Example 5.1 (Where will the ﬂy be?).

You live in a house with three rooms, labelled 1, 2, 3. There is a door between rooms 1 and 2 and another
between rooms 2 and 3. One cannot directly pass between rooms 1 and 3 in one time-step. An annoying ﬂy
is buzzing from one room to another and there is some smelly cheese in room 1 which seems to attract the
ﬂy more. Using xt to indicate which room the ﬂy is in at time t, with dom(xt) =
, the movement of
the ﬂy can be described by a transition

1, 2, 3
}

{

p(xt+1 = i

xt = j) = Mij
|

where Mij is an element of the transition matrix





M =

0.7 0.5
0
0.3 0.3 0.5
0.2 0.5
0





(5.1.11)

(5.1.12)

The matrix M is called ‘stochastic’ meaning that, as required of a conditional probability table, its columns
sum to 1, (cid:80)3
i=1 Mij = 1. Given that the ﬂy is in room 1 at time t = 1, what is the probability of room
occupancy at time t = 5? Assume a Markov chain which is deﬁned by the joint distribution

p(x1, . . . , xT ) = p(x1)

T −1
(cid:89)

t=1

p(xt+1

xt)
|

We are asked to compute p(x5

x1 = 1) which is given by
|

(cid:88)

x4,x3,x2

p(x5

x4)p(x4
|

x3)p(x3
|

x2)p(x2
|

|

x1 = 1)

(5.1.13)

(5.1.14)

Since the graph of the distribution is a Markov chain, we can easily distribute the summation over the terms.
This is most easily done using the transfer matrix method, giving

x1 = 1) = [M4v]i
p(x5 = i
|

DRAFT April 24, 2015

(5.1.15)

79

Marginal Inference

where v is a vector with components (1, 0, 0)T, reﬂecting the evidence that at time t = 1 the ﬂy is in room
1. Computing this we have (to 4 decimal places of accuracy)

M4v =









0.5746
0.3180
0.1074

(5.1.16)

Similarly, at time t = 6, the occupancy probabilities are (0.5612, 0.3215, 0.1173). The room occupancy
probability is converging to a particular distribution – the stationary distribution of the Markov chain. One
might ask where the ﬂy is after an inﬁnite number of time-steps. That is, we are interested in the large t
behaviour of

p(xt+1) =

(cid:88)

xt

p(xt+1

xt)p(xt)
|

(5.1.17)

At convergence p(xt+1) = p(xt). Writing p for the vector describing the stationary distribution, this means

p = Mp

(5.1.18)

In other words, p is the eigenvector of M with eigenvalue 1[134]. Computing this numerically, the stationary
distribution is (0.5435, 0.3261, 0.1304). Note that software packages usually return eigenvectors with (cid:80)
i e2
i =
1 — the unit eigenvector therefore will usually require normalisation to make this a probability with (cid:80)
i ei =
1.

5.1.2 The sum-product algorithm on factor graphs

Both Markov and belief networks can be represented using factor graphs. For this reason it is convenient to
derive a marginal inference algorithm for FGs since this then applies to both Markov and belief networks.
This is termed the sum-product algorithm since to compute marginals we need to distribute the sum over
variable states over the product of factors. In other texts, this is also referred to as belief propagation.

Non-branching graphs : variable to variable messages

Consider the distribution

p(a, b, c, d) = f1 (a, b) f2 (b, c) f3 (c, d) f4 (d)

(5.1.19)

which has the factor graph represented in ﬁg(5.2). To compute the marginal p(a, b, c), since the variable d
only occurs locally, we use

p(a, b, c) =

(cid:88)

d

p(a, b, c, d) =

(cid:88)

d

f1 (a, b) f2 (b, c) f3 (c, d) f4 (d) = f1 (a, b) f2 (b, c)

(cid:88)

d

(cid:124)

f3 (c, d) f4 (d)

(5.1.20)

(cid:123)(cid:122)
µd→c(c)

(cid:125)

Here µd→c (c) deﬁnes a message from node d to node c and is a function of the variable c. Similarly,

p(a, b) =

(cid:88)

c

p(a, b, c) = f1 (a, b)

(cid:88)

c

(cid:124)

f2 (b, c) µd→c (c)

(cid:123)(cid:122)
µc→b(b)

(cid:125)

Hence

µc→b (b) =

(cid:88)

c

f2 (b, c) µd→c (c)

(5.1.21)

(5.1.22)

It is clear how one can recurse this deﬁnition of messages so that for a chain of n variables the marginal
of the ﬁrst node can be computed in time linear in n. The term µc→b (b) can be interpreted as carrying

80

DRAFT April 24, 2015

Marginal Inference

f1

a

b

f2

f3

c

d

f4

Figure 5.2: For singly connected structures without
branches, simple messages from one variable to its
neighbour may be deﬁned to form an eﬃcient marginal
inference scheme.

marginal information from the graph beyond c. For simple linear structures with no branching, messages
from variables to variables are suﬃcient. However, as we will see below in more general structures with
branching, it is useful to consider two types of messages, namely those from variables to factors and vice
versa.

General singly connected factor graphs

The slightly more complex example,

p(a

c, d)p(c)p(d)p(e
b)p(b
|
|

d)
|

has the factor graph depicted in ﬁg(5.3)

f1 (a, b) f2 (b, c, d) f3 (c) f4 (d, e) f5 (d)

The marginal p(a, b) can be represented by an amputated graph with a message, since

p(a, b) = f1 (a, b)

(cid:88)

c,d
(cid:124)

f2 (b, c, d) f3 (c) f5 (d)

(cid:123)(cid:122)
µf2→b(b)

(cid:88)

e

f4 (d, e)

(cid:125)

(5.1.23)

(5.1.24)

(5.1.25)

where µf2→b (b) is a message from a factor to a variable. This message can be constructed from messages
arriving from the two branches through c and d, namely

µf2→b (b) =

(cid:88)

c,d

f2 (b, c, d) f3 (c)
(cid:124) (cid:123)(cid:122) (cid:125)
µc→f2 (c)

(cid:88)

f5 (d)

f4 (d, e)

(cid:124)

e
(cid:123)(cid:122)
µd→f2 (d)

(cid:125)

Similarly, we can interpret

µd→f2 (d) = f5 (d)
(cid:124) (cid:123)(cid:122) (cid:125)
µf5→d(d)

(cid:88)

f4 (d, e)

(cid:124)

e

(cid:123)(cid:122)
µf4→d(d)

(cid:125)

(5.1.26)

(5.1.27)

To complete the interpretation we identify µc→f2 (c)
simply use a variable to variable message. To compute the marginal p(a), we then have

µf3→c (c). In a non-branching link, one can more

≡

f1 (a, b) µf2→b (b)

(5.1.28)

p(a) =

(cid:88)

b

(cid:124)

(cid:123)(cid:122)
µf1→a(a)

a

f1

b

f2

(cid:125)

c

d

f3

e

f4

f5

Figure 5.3: For a branching singly connected graph,
it is useful to deﬁne messages from both factors to
variables, and variables to factors.

DRAFT April 24, 2015

81

For consistency of interpretation, one also can view the above as

µf1→a (a) =

(cid:88)

b

f1 (a, b) µf2→b (b)
(cid:124)
(cid:125)
(cid:123)(cid:122)
µb→f1 (b)

Marginal Inference

(5.1.29)

We can now see how a message from a factor to a node is formed from summing the product of incoming
node-to-factor messages. Similarly, a message from a node to a factor is given by the product of incoming
factor-to-node messages.

A convenience of this approach is that the messages can be reused to evaluate other marginal inferences.
For example, it is clear that p(b) is given by

(cid:88)

p(b) =

f1 (a, b)

µf2→b (b)

(cid:124)

a

(cid:123)(cid:122)
µf1→b(b)

(cid:125)

If we additionally desire p(c), we need to deﬁne the message from f2 to c,

µf2→c (c) =

(cid:88)

b,d

f2 (b, c, d) µb→f2 (b) µd→f2 (d)

(5.1.30)

(5.1.31)

where µb→f2 (b)
compute the marginal p(c).

≡

µf1→b (b). This demonstrates the reuse of already computed message from d to f2 to

Deﬁnition 5.1 (Message schedule). A message schedule is a speciﬁed sequence of message updates. A valid
schedule is that a message can be sent from a node only when that node has received all requisite messages
from its neighbours. In general, there is more than one valid updating schedule.

Sum-Product algorithm

The sum-product algorithm is described below in which messages are updated as a function of incoming
messages. One then proceeds by computing the messages in a schedule that allows the computation of a
new message based on previously computed messages, until all messages from all factors to variables and
vice-versa have been computed.

Procedure 5.1 (Sum-Product messages on Factor Graphs).

f φf (
Given a distribution deﬁned as a product on subsets of the variables, p(
X
factor graph is singly connected we can carry out summation over the variables eﬃciently.

X

) = 1
Z

(cid:81)

f ), provided the

Initialisation Messages from leaf node factors are initialised to the factor. Messages from leaf variable

nodes are set to unity.

Variable to Factor message

µx→f (x) =

(cid:89)

µg→x (x)

g∈{ne(x)\f }

f2

f1

f3

µ
f
1

→

x(x)

µf2→x (x)

3 → x ( x )

f

µ

µx→f (x)

f

x

82

DRAFT April 24, 2015

Marginal Inference

Factor to Variable message

µf →x (x) =

(cid:88)

Xf \x

φf (

X

f )

(cid:89)

µy→f (y)

y∈{ne(f )\x}

We write (cid:80)
of variables

Xf \x to denote summation over all states in the set
f
X

x.

\

Marginal

p(x)

∝

(cid:89)

f ∈ne(x)

µf →x (x)

y2

f1

f2

y1

µ
y
1

→

f(y

1)

f

µf →x (x)

x

3 )

µy2→f (y2)
( y

3 → f

y

µ

y3

µf1→x(x)

( x )

µ f 2 → x

x

µx←f3(x)

f3

For marginal inference, the important information is the relative size of the message states so that we may
renormalise messages as we wish. Since the marginal will be proportional to the incoming messages for that
variable, the normalisation constant is trivially obtained using the fact that the marginal must sum to 1.
However, if we wish to also compute any normalisation constant using these messages, we cannot normalise
the messages since this global information will then be lost.

5.1.3 Dealing with Evidence

e

X

n, the marginal of a
For a distribution which splits into evidential and non-evidential variables,
X
non-evidential variable p(xi,
e set
e) is given by summing over all the variables in
into their evidential states. There are two ways to reconcile this with the factor graph formalism. Either
we can simply say that by setting the variables
n, and then pass
messages on this new factor graph. Alternatively, we can deﬁne the potentials which contain the variables
e by multiplying each potential that contains an evidential variable by a delta function (an indicator) that
X
is zero unless the variable
xe is in the speciﬁed evidential state. When we then perform a factor to variable
message, the sum of this modiﬁed potential over any of the evidential variable states will be zero except
for that state which corresponds to the evidential setting. Another way to view this is that the sum in
the factor to variable message is only over the non-evidential variables, with any evidential variables in the
potential set into their evidential states.

=
n (except for xi) with

e, we deﬁne a new factor graph on

∪ X

X

X

X

X

X

@@

5.1.4 Computing the marginal likelihood

For a distribution deﬁned as products over potentials φf (

f )

X

) =

p(

X

1
Z

(cid:89)

f

φf (

X

f )

the normalisation is given by
(cid:89)

(cid:88)

Z =

φf (

f )

X

X

f

(5.1.32)

(5.1.33)

To compute this summation eﬃciently we take the product of all incoming messages to an arbitrarily chosen
variable x and then sum over the states of that variable:

(cid:88)

(cid:89)

Z =

µf →x (x)

x

f ∈ne(x)

(5.1.34)

If the factor graph is derived from setting a subset of variables of a BN in evidential states then the
summation over all non-evidential variables will yield the marginal on the visible (evidential) variables. For
example

p(b, d) =

(cid:88)

a,c

p(a

d)p(d).
b)p(b
c)p(c
|
|
|

DRAFT April 24, 2015

(5.1.35)

83

Marginal Inference

This can be interpreted as requiring the sum over a product of suitably deﬁned factors. Hence one can
readily ﬁnd the marginal likelihood of the evidential variables for singly connected BNs.

Log messages

For the above method to work, the absolute (not relative) values of the messages are required, which
prohibits renormalisation at each stage of the message passing procedure. However, without normalisation
the numerical value of messages can become very small, particularly for large graphs, and numerical precision
issues can occur. A remedy in this situation is to work with log messages,

λ = log µ

For this, the variable to factor messages

µx→f (x) =

(cid:89)

µg→x (x)

g∈{ne(x)\f }

become simply

λx→f (x) =

(cid:88)

λg→x (x)

g∈{ne(x)\f }

More care is required for the factors to variable messages, which are deﬁned by

µf →x (x) =

(cid:88)

Xf \x

φf (

f )

X

(cid:89)

µy→f (y)

y∈{ne(f )\x}

Naively, one may write

λf →x (x) = log





(cid:88)

φf (

Xf \x





f )exp

X





(cid:88)

λy→f (y)





y∈{ne(f )\x}

(5.1.36)

(5.1.37)

(5.1.38)

(5.1.39)

(5.1.40)

However, the exponentiation of the log messages will cause potential numerical precision problems. A
solution to this numerical diﬃculty is obtained by ﬁnding the largest value of the incoming log messages,

λ∗
y→f = max

y∈{ne(f )\x}

λy→f (y)

Then

λf →x (x) = λ∗

y→f + log





(cid:88)

Xf \x

φf (

f )exp

X





(cid:88)

y∈{ne(f )\x}





λy→f (y)

−

λ∗
y→f





(5.1.41)

(5.1.42)

By construction the terms exp
1, with at least one term being
equal to 1. This ensures that the dominant numerical contributions to the summation are computed accu-
rately.

y∈{ne(f )\x} λy→f (y)

will be

≤

−

(cid:16)(cid:80)

(cid:17)

λ∗
y→f

Log marginals are readily found using

log p(x) =

(cid:88)

f ∈ne(x)

λf →x (x)

(5.1.43)

84

DRAFT April 24, 2015

Other Forms of Inference

f4

a

d

f1

f3

(a)

b

c

f2

a

f1

f5

(b)

b

c

f2

5.1.5 The problem with loops

(b): Elimi-
(a): Factor graph with a loop.
Figure 5.4:
nating the variable d adds an edge between a and c, demon-
strating that, in general, one cannot perform marginal in-
ference in loopy graphs by simply passing messages along
existing edges in the original graph.

Loops cause a problem with variable elimination (or message passing) techniques since once a variable is
eliminated the structure of the ‘amputated’ graph in general changes. For example, consider the FG

p(a, b, c, d) = f1 (a, b) f2 (b, c) f3 (c, d) f4 (a, d)

depicted in ﬁg(5.4a). The marginal p(a, b, c) is given by

p(a, b, c) = f1 (a, b) f2 (b, c)

(cid:88)

d

(cid:124)

f3 (c, d) f4 (a, d)

(cid:123)(cid:122)
f5(a,c)

(cid:125)

(5.1.44)

(5.1.45)

which adds a link ac in the amputated graph, see ﬁg(5.4b). This means that one cannot account for
information from variable d by simply updating potentials on links in the original graph – one needs to
account for the fact that the structure of the graph changes. The junction tree algorithm, chapter(6) deals
with this by combining variables to make a new singly connected graph for which the graph structure remains
singly connected under variable elimination.

5.2 Other Forms of Inference

5.2.1 Max-Product

A common interest is the most likely state of distribution. That is

argmax
x1,x2,...,xn

p (x1, x2, . . . , xn)

(5.2.1)

To compute this eﬃciently for trees we exploit any factorisation structure of the distribution, analogous to
the sum-product algorithm. That is, we aim to distribute the maximization so that only local computations
are required. To develop the algorithm, consider a function which can be represented as an undirected chain,

f (x1, x2, x3, x4) = φ(x1, x2)φ(x2, x3)φ(x3, x4)

(5.2.2)

for which we wish to ﬁnd the joint state x∗
value of f . Since potentials are non-negative, we may write

1, x∗

3, x∗

2, x∗

4 which maximises f . Firstly, we calculate the maximum

max
x

f (x) = max

x1,x2,x3,x4

φ(x1, x2)φ(x2, x3)φ(x3, x4) = max
x1,x2,x3

φ(x1, x2)φ(x2, x3) max

x4

(cid:124)

φ(x3, x4)
(cid:123)(cid:122)
(cid:125)
γ4(x3)

= max
x1,x2

φ(x1, x2) max

x3

(cid:124)

φ(x2, x3)γ4(x3)
(cid:125)

(cid:123)(cid:122)
γ3(x2)

= max
x1,x2

φ(x1, x2)γ3(x2) = max

x1

max
x2

(cid:124)

φ(x1, x2)γ3(x2)
(cid:125)

(cid:123)(cid:122)
γ2(x1)

The ﬁnal equation corresponds to solving a single variable optimisation and determines both the optimal
value of the function f and also the optimal state x∗
1, the optimal x2 is given
φ(x∗

3, x4). This
procedure is called backtracking. Note that we could have equally started at the other end of the chain by

γ2(x1). Given x∗
2, x3)γ4(x3), x∗

1, x2)γ3(x2), and similarly x∗

x1
3 = argmax

2 = argmax

4 = argmax

1 = argmax

by x∗

φ(x∗

φ(x∗

x3

x4

x2

DRAFT April 24, 2015

85

deﬁning messages γ that pass information from xi to xi+1. The chain structure of the function ensures that
the maximal value (and its state) can be computed in time which scales linearly with the number of factors
in the function. There is no requirement here that the function f corresponds to a probability distribution
(though the factors must be non-negative).

Other Forms of Inference

Example 5.2. Consider a distribution deﬁned over binary variables:

p(a, b, c)

p(a
|

c)p(c)
b)p(b
|

≡

with

p(a = tr
b = tr) = 0.3, p(a = tr
|
|
c = fa) = 0.1, p(c = tr) = 0.4
p(b = tr
|

b = fa) = 0.2, p(b = tr

c = tr) = 0.75
|

What is the most likely joint conﬁguration, argmax

p(a, b, c)?

a,b,c

(5.2.3)

Naively, we could evaluate p(a, b, c) over all the 8 joint states of a, b, c and select that states with highest
probability. An alternative message passing approach is to deﬁne

γc(b)

max
c

c)p(c)
p(b
|

≡

For the state b = tr,

p(b = tr

c = tr)p(c = tr) = 0.75
|

0.4,

c = fa)p(c = fa) = 0.1
|
0.4 = 0.3. Similarly, for b = fa,

p(b = tr

×

0.6

×

Hence, γc(b = tr) = 0.75

×
c = tr)p(c = tr) = 0.25
p(b = fa
|

0.4

×

c = fa)p(c = fa) = 0.9
p(b = fa
|

×

0.6

Hence, γc(b = fa) = 0.9

0.6 = 0.54.

×

We now consider

γb(a)

max
b

b)γc(b)
p(a
|

≡

For a = tr, the state b = tr has value

p(a = tr
|
and state b = fa has value

b = tr)γc(b = tr) = 0.3

×

0.3 = 0.09

p(a = tr

b = fa)γc(b = fa) = 0.2
|

×

0.54 = 0.108

Hence γb(a = tr) = 0.108. Similarly, for a = fa, the state b = tr has value

b = tr)γc(b = tr) = 0.7
p(a = fa
|
and state b = fa has value

×

0.3 = 0.21

b = fa)γc(b = fa) = 0.8
p(a = fa
|

×

0.54 = 0.432

giving γb(a = fa) = 0.432. Now we can compute the optimal state

a∗ = argmax

a

γb(a) = fa

Given this optimal state, we can backtrack, giving

b∗ = argmax

b

p(a = fa
|

b)γc(b) = fa, c∗ = argmax

c

c)p(c) = fa
p(b = fa
|

(5.2.4)

(5.2.5)

(5.2.6)

(5.2.7)

(5.2.8)

(5.2.9)

(5.2.10)

(5.2.11)

(5.2.12)

(5.2.13)

Note that in the backtracking process, we already have all the information required from the computation
of the messages γ.

86

DRAFT April 24, 2015

Other Forms of Inference

If we want to ﬁnd the most likely state for a variable in the centre of the chain we can therefore pass
messages from one end to the other followed by backtracking. This is the approach for example taken by
the Viterbi algorithm for HMMs, section(23.2). Alternatively, we can send messages (carrying the result
of maximisations) simultaneously from both ends of the chain and then read oﬀ the maximal state of
the variable from the state which maximises the product of incoming messages. The ﬁrst is a sequential
procedure since we must have passed messages along the chain before we can backtrack. The second is
a parallel procedure in which messages can be sent concurrently. The latter approach can be represented
using a factor graph as described below.

Using a factor graph

One can also use the factor graph to compute the joint most probable state. Provided that a full schedule
of message passing has occurred, the product of messages into a variable equals the maximum value of the
joint function with respect to all other variables. One can then simply read oﬀ the most probable state by
maximising this local potential.

Procedure 5.2 (Max-Product messages on Factor Graphs).

Given a distribution deﬁned as a product on subsets of the variables, p(
factor graph is singly connected we can carry out maximisation over the variables eﬃciently.

f φf (

X

X

f ), provided the

) = 1
Z

(cid:81)

Initialisation Messages from leaf node factors are initialised to the factor. Messages from leaf variable

nodes are set to unity.

Variable to Factor message

µx→f (x) =

(cid:89)

µg→x (x)

g∈{ne(x)\f }

Factor to Variable message

µf →x (x) = max
Xf \x

φf (

f )

X

(cid:89)

µy→f (y)

y∈{ne(f )\x}

Maximal State

x∗ = argmax

x

(cid:89)

f ∈ne(x)

µf →x (x)

This algorithm is also called belief revision.

5.2.2 Finding the N most probable states

f1

µ
f
1

→

x(x)

f2

µf2→x (x)

µx→f (x)

f

x

f3

y1

3 → x ( x )

f

µ

µ
y
1

→

f(y

1)

y2

f1

f2

f

µf →x (x)

x

3 )

µy2→f (y2)
( y

3 → f

y

µ

y3

µf1→x(x)

( x )

µ f 2 → x

x

µx←f3(x)

f3

It is often of interest to calculate not just the most likely joint state, but the N most probable states,
particularly in cases where the optimal state is only slightly more probable than other states. This is an
interesting problem in itself and can be tackled with a variety of methods. A general technique is given by
Nilson[226] which is based on the junction tree formalism, chapter(6), and the construction of candidate

DRAFT April 24, 2015

87

Other Forms of Inference

lists, see for example [72].

For singly connected structures, several approaches have been developed [227, 319, 285, 272]. For the
hidden Markov model, section(23.2), a simple algorithm is the N -Viterbi approach which stores the N -most
probable messages at each stage of the propagation. For more general singly connected graphs one can
extend the max-product algorithm to an N -max-product algorithm by retaining at each stage the N most
probable messages, see below.

N -max-product

The algorithm for N -max-product is a minor modiﬁcation of the standard max-product algorithm. Compu-
tationally, a straightforward way to accomplish this is to introduce an additional variable for each message
that is used to index the most likely messages. We will ﬁrst develop this for message passing on an undi-
rected graph. Consider the distribution

e

a

b

c

d

p(a, b, c, d, e) = φ(e, a)φ(a, b)φ(b, c)φ(b, d)

(5.2.14)

for which we wish to ﬁnd the two most probable values. Using the notation

i
max
x

f (x)

for the ith highest value of f (x), the maximisation over d can be expressed using the message

γd(b, 1) =

1
max
d

φ(b, d),

γd(b, 2) =

2
max
d

φ(b, d)

Deﬁning messages similarly, the 2 most likely values of p(a, b, c, d, e) can be computed using

(5.2.15)

(5.2.16)

1:2
max
a,b,c,d,e

φ(e, a)φ(a, b)φ(b, c)φ(b, d) =

1:2
max
e,ma

1:2
max
a,mb

φ(e, a)

1:2
max
b,mc,md

φ(a, b)

(cid:124)

1:2
max
c
(cid:124)

φ(b, c)
(cid:123)(cid:122)
(cid:125)
γc(b,mc)

(cid:123)(cid:122)
γb(a,mb)

(cid:124)

(cid:123)(cid:122)
γa(e,ma)

1:2
max
d

(cid:124)

(cid:123)(cid:122)
γd(b,md)

(cid:125)

(cid:125)

(cid:125)

φ(b, d)

(5.2.17)

2
where ma, mb, mc and md index the two highest values. At the ﬁnal stage we now have a table with dim(e)
×
entries, from which we compute the highest two joint states of e, ma. Given these ﬁrst most likely joint
states, e∗, m∗
a,mb φ(e∗, a)γb(a, mb).
One continues backtracking, then ﬁnding the most likely state of b, mc, md and ﬁnally c and d. One may
then restart the backtracking using the second most likely state of e, ma and continue to ﬁnd the most likely
states to have given rise to this, leading to the second most likely joint state.

a one then backtracks to ﬁnd the most likely state of a, mb using arg max1:2

The translation of this to the factor graph formalism is straightforward and contained in maxNprodFG.m.
Essentially the only modiﬁcation required is to deﬁne extended messages which contain the N -most likely
messages computed at each stage. A variable to factor message consists of the product of extended mes-
sages. For a factor to variable message, all extended messages from the neighbours are multiplied together
into a large table. The N -most probable messages are retained, deﬁning a new extended message. The
N -most probable states for each variable can then be read oﬀ by ﬁnding the variable state that maximises
the product of incoming extended messages.

Branches are the bottleneck in the above computation. Consider a term as part of a larger system with

z

a

b

c

φ(z, a)φ(a, b)φ(a, c)

(5.2.18)

88

DRAFT April 24, 2015

Other Forms of Inference

3

5

4

1

2

6

8

7

9

2

−

Figure 5.5: State transition diagram (weights not shown). The shortest
7. Considered as a
(unweighted) path from state 1 to state 7 is 1
Markov chain (random walk), the most probable path from state 1 to state
7. The latter path is longer but more probable since for
7 is 1
8
the path 1
7, the probability of exiting from state 2 into state 7 is
1/5 (assuming each transition is equally likely). Note that in this example,
states 3,4,5,6,7 are ‘absorbing’ – one can enter, but not exit these states.
This would correspond to adding self-loops on nodes 3,4,5,6,7; these are not
drawn here to improve clarity. See demoMostProbablePath.m

9
−
2
−

−
−

−

−

We would like to pass a message along the branch from b to a and then from c to a and then from a to the
rest of the graph. To compute the most likely value we can ﬁnd this from

(cid:26)

max
a

φ(z, a)

max
b

(cid:27) (cid:110)

φ(a, b)

max
c

(cid:111)

φ(a, c)

(5.2.19)

which represents an eﬃcient approach since the maximisation can be carried out over each branch separately.
However, to ﬁnd the second most likely value, we cannot write
(cid:27) (cid:110)

(cid:26)

(cid:111)

2
max
a

φ(z, a)

max
b

φ(a, b)

max
c

φ(a, c)

(5.2.20)

For a ﬁxed z, this would erroneously always force a to be in a diﬀerent state than the state corresponding to
the most likely value. Similarly, we cannot assume that the second most likely state corresponds to ﬁnding
the second most likely state of each factor. Unlike the single most likely state case, therefore, we cannot
distribute the maximisations over each branch but have to search over all branch contributions concurrently.
This therefore corresponds to an exponentially complex search for the N -highest joint branch states. Whilst
non branching links are non-problematic, the degree D of a variable’s node (in either the FG or undirected
representation) contributes an additional exponential term N D to the computational complexity.

5.2.3 Most probable path and shortest path

What is the most likely path from state a to state b for an N state Markov chain? Note that this is not
necessarily the same as the shortest path, as explained in ﬁg(5.5). If we consider a path of length T , this
has probability

p(s2

s1 = a)p(s3
|

s2) . . . p(sT = b
|

sT −1)
|

(5.2.21)

Finding the most probable path can then be readily solved using the max-product (or max-sum algorithm
for the log-transitions) on a simple serial factor graph. To deal with the issue that we don’t know the optimal
T , one approach is to redeﬁne the probability transitions such that the desired state b is an absorbing state
of the chain (that is, one can enter this state but not leave it). With this redeﬁnition, the most probable
joint state will correspond to the most probable state on the product of N
1 transitions. This approach
is demonstrated in demoMostProbablePath.m, along with the more direct approaches described below.

−

An alternative, cleaner approach is as follows: for the Markov chain we can dispense with variable-to-factor
and factor-to-variable messages and use only variable-to-variable messages.
If we want to ﬁnd the most
likely set of states a, s2, . . . , sT −1, b to get us there, then this can be computed by deﬁning the maximal path
probability E (a

b, T ) to get from a to b in T -timesteps:

→

E (a

b, T ) = max

s2,...,sT −1

→

= max

s3,...,sT −1

p(s2

s1 = a)p(s3
|
p(s2
max
s2

|

s1 = a)p(s3

s2)p(s4
|

s3) . . . p(sT = b
|

sT −1)

p(s4

s3) . . . p(sT = b
sT −1)
|
|

|
s2)
|
(cid:125)

(cid:124)

(cid:123)(cid:122)
γ2→3(s3)

To compute this eﬃciently we deﬁne messages

γt→t+1 (st+1) = max

st

γt−1→t (st) p(st+1

st),
|

2,

t

≥

γ1→2 (s2) = p(s2

s1 = a)
|

DRAFT April 24, 2015

(5.2.22)

(5.2.23)

(5.2.24)

89

Other Forms of Inference

until the point

E (a

b, T ) = max
sT −1

→

γT −2→T −1 (sT −1) p(sT = b

sT −1) = γT −1→T (sT = b)
|

(5.2.25)

We can now proceed to ﬁnd the maximal path probability for timestep T + 1. Since the messages up to time
1 will be the same as before, we need only compute one additional message, γT −1→T (sT ), from which
T

−

E (a

→

b, T + 1) = max

sT

γT −1→T (sT ) p(sT +1 = b

sT ) = γT →T +1 (sT +1 = b)
|

(5.2.26)

b, N ) where N is the number of nodes in the graph.
We can proceed in this manner until we reach E (a
→
We don’t need to go beyond this number of steps since those that do must necessarily contain non-simple
paths. (A simple path is one that does not include the same state more than once.) The optimal time t∗
b, N ) is maximal. Given t∗ one can begin to backtrack1.
is then given by which of E (a
Since

b, 2) , . . . , E (a

→

→

E (a

b, t∗) = max
st∗−1

→

γt∗−2→t∗−1 (st∗−1) p(st∗ = b
st∗−1)
|

we know the optimal state

s∗
t∗−1 = argmax
st∗−1

γt∗−2→t∗−1 (st∗−1) p(st∗ = b
st∗−1)
|

We can then continue to backtrack:

s∗
t∗−2 = argmax

st∗−2

γt∗−3→t∗−2 (st∗−2) p(s∗

st∗−2)

t∗−1|

(5.2.27)

(5.2.28)

(5.2.29)

and so on. See mostprobablepath.m.

•

•

•

•

In the above derivation we do not use any properties of probability, except that p must be non-negative
(otherwise sign changes can ﬂip a whole sequence ‘probability’ and the local‘ message recursion no
longer applies). One can consider the algorithm as ﬁnding the optimal ‘product’ path from a to b.
It is straightforward to modify the algorithm to solve the (single-source, single-sink) shortest weighted
path problem. One way to do this is to replace the Markov transition probabilities with exp(
st−1) is the edge weight and is inﬁnite if there is no edge from st−1 to st. This approach
where u(st
|
is taken in shortestpath.m which is able to deal with either positive or negative edge weights. This
method is therefore more general than the well-known Dijkstra’s algorithm [121] which requires weights
to be positive. If a negative edge cycle exists, the code returns the shortest weighted length N path,
where N is the number of nodes in the graph. See demoShortestPath.m.

u(st

−

st−1)),
|

The above algorithm is eﬃcient for the single-source, single-sink scenario, since the messages contain
only N states, meaning that the overall storage is O(N 2).
As it stands, the algorithm is numerically impractical since the messages are recursively multiplied by
values usually less than 1 (at least for the case of probabilities). One will therefore quickly run into
numerical underﬂow (or possibly overﬂow in the case of non-probabilities) with this method.

To ﬁx the ﬁnal point above, it is best to work by deﬁning the logarithm of E. Since this is a monotonic
transformation, the most probable path deﬁned through log E is the same as that obtained from E. In this
case

L (a

b, T ) = max

s2,...,sT −1

→

log [p(s2



s1 = a)p(s3
|

|

s2)p(s4

sT −1)]
s3) . . . p(sT = b
|
|



= max

s2,...,sT −1


log p(s2

|

s1 = a) +

log p(st

st−1) + log p(sT = b
|

sT −1)
|




T −1
(cid:88)

t=3

(5.2.30)

(5.2.31)

@@

1An alternative to ﬁnding t∗ is to deﬁne self-transitions with probability 1, and then use a ﬁxed time T = N . Once the
desired state b is reached, the self-transition then preserves the chain in state b for the remaining timesteps. This procedure is
used in mostprobablepathmult.m

90

DRAFT April 24, 2015

Inference in Multiply Connected Graphs

We can therefore deﬁne new messages

λt→t+1 (st+1) = max

st

[λt−1→t (st) + log p(st+1

st)]
|

(5.2.32)

One then proceeds as before by ﬁnding the most probable t∗ deﬁned on L, and backtracks.

Remark 5.2. A possible confusion is that optimal paths can be eﬃciently found ‘when the graph is loopy’.
Note that the graph in ﬁg(5.5) is a state-transition diagram, not a graphical model. The graphical model
corresponding to this simple Markov chain is the Belief Network (cid:81)
st−1), a linear serial structure.
|
Hence the underlying graphical model is a simple chain, which explains why computation is eﬃcient.

t p(st

Most probable path (multiple-source, multiple-sink)

If we need the most probable path between all states a and b, one could re-run the above single-source-
single-sink algorithm for all a and b. A computationally more eﬃcient approach is to observe that one can
deﬁne a message for each starting state a:

γt→t+1 (st+1

a) = max

st

|

γt−1→t (st

a) p(st+1
|

|

st)

(5.2.33)

and continue until we ﬁnd the maximal path probability matrix for getting from any state a to any state b
in T timesteps:

E (a

b, T ) = max
sT −1

→

γT −2→T −1 (sT −1

a) p(sT = b

sT −1)
|

|

(5.2.34)

Since we know the message γT −2→T −1 (sT −1
path from all starting states a to all states b after T steps. This requires passing an N
γ. We can then proceed to the next timestep T + 1. Since the messages up to time T
as before, we need only compute one additional message, γT −1→T (sT ), from which

a) for all states a, we can readily compute the most probable
|
N matrix message
1 will be the same

×
−

E (a

→

b, T + 1) = max

sT

γT −1→T (sT

a) p(sT +1 = b
|

sT )
|

(5.2.35)

In this way one can then eﬃciently compute the optimal path probabilities for all starting states a and
end states b after t timesteps. To ﬁnd the optimal corresponding path, backtracking proceeds as before,
see mostprobablepathmult.m and demoMostProbablePathMult.m. One can also use the same algorithm to
solve the multiple-source, multiple-sink shortest weighted path problem using exponentiated negative edge
weights, as before. This is a variant of the Floyd-Warshall-Roy algorithm[121].

5.2.4 Mixed inference

An often encountered situation is to infer the most likely state of a joint marginal, possibly given some
evidence. For example, given a distribution p(x1, . . . , xn), ﬁnd

argmax
x1,x2,...,xm

p(x1, x2, . . . , xm) = argmax
x1,x2,...,xm

(cid:88)

xm+1,...,xn

p(x1, . . . , xn)

(5.2.36)

In general, even for tree structured p(x1, . . . , xn), the optimal marginal state cannot be computed eﬃciently.
One way to see this is that due to the summation the resulting joint marginal does not have a structured
factored form as products of simpler functions of the marginal variables. Finding the most probable joint
marginal then requires a search over all the joint marginal states – a task exponential in m. An approximate
solution is provided by the EM algorithm (see section(11.2) and exercise(5.7)).

5.3 Inference in Multiply Connected Graphs

We brieﬂy discuss here some relatively straightforward approaches to dealing with multiply connected graphs
that are conceptually straightforward, or build on the repeated use of singly connected structures. We discuss
a more general algorithm in chapter(6).

DRAFT April 24, 2015

91

Inference in Multiply Connected Graphs

f φf (

x
{

}f ). Assumes non-

Algorithm 5.1 Compute marginal p(x1
|
evidential variables are ordered x1, . . . , xn.
1: procedure Bucket Elimination(p(x) = (cid:81)
Initialize all bucket potentials to unity.
2:
while There are potentials left in the distribution do

f φf (
{

x

3:

}f ).)

evidence) from distribution p(x) = (cid:81)

4:

5:

6:

7:

8:

9:

10:

For each potential φf , ﬁnd its highest variable xj (according to the ordering).
Multiply φf with the potential in bucket j and remove φf the distribution.

end while
for i = bucket n to 1 do

For bucket i sum over the states of variable xi and call this potential γi
Identify the highest variable xh of potential γi
Multiply the existing potential in bucket h by γi

(cid:46) Fill buckets

(cid:46) Empty buckets

12:

11:

end for
The marginal p(x1
return p(x1
13:
14: end procedure

|

evidence)

evidence) is proportional to γ1.
|

(cid:46) The conditional marginal.

5.3.1 Bucket elimination

We consider here a general conditional marginal variable elimination method that works for any distribu-
tion (including multiply connected graphs). Bucket elimination is presented in algorithm(5.1) and can be
considered a way to organise the distributed summation[83]. The algorithm is perhaps best explained by a
simple example, as given below.

Example 5.3 (Bucket Elimination). Consider the problem of calculating the marginal p(f ) of

p(a, b, c, d, e, f, g) = p(f

d)p(g

a, b)p(a)p(b)p(e),
a)p(d
d, e)p(c
|
|
|

|

see ﬁg(2.1a). Whilst this is singly-connected, this serves to explain the general procedure.

p(f ) =

(cid:88)

a,b,c,d,e,g

p(a, b, c, d, e, f, g) =

(cid:88)

a,b,c,d,e,g

p(f

d)p(g
|

a, b)p(a)p(b)p(e)
a)p(d
d, e)p(c
|
|
|

(5.3.1)

(5.3.2)

We can distribute the summation over the various terms as follows: e, b and c are end nodes, so that we
can sum over their values:

p(f ) =

(cid:88)

p(f

(cid:32)

(cid:88)

d)p(a)
|

a, b)p(b)
p(d
|

(cid:33) (cid:32)

(cid:88)

(cid:33) (cid:32)

(cid:88)

e

a)

p(c
|

c

(cid:33)

p(g

d, e)p(e)
|

(5.3.3)

d,a,g

b
For convenience, let’s write the terms in the brackets as (cid:80)
γe (d, g). The term (cid:80)
terms, we can write

b p(d
≡
|
a) is equal to unity, and we therefore eliminate this node directly. Rearranging
c p(c
|

γb (a, d), (cid:80)

d, e)p(e)

a, b)p(b)

e p(g

≡

|

p(f ) =

(cid:88)

d,a,g

p(f

d)p(a)γb (a, d) γe (d, g)
|

(5.3.4)

If we think of this graphically, the eﬀect of summing over b, c, e is eﬀectively to remove or ‘eliminate’ those
variables. We can now carry on summing over a and g since these are end points of the new graph:

p(f ) =

(cid:88)

d

p(f

d)

|

(cid:32)

(cid:88)

a

p(a)γb (a, d)

(cid:33) (cid:32)

(cid:88)

g

(cid:33)

γe (d, g)

Again, this deﬁnes new potentials γa (d), γg (d), so that the ﬁnal answer can be found from

p(f ) =

(cid:88)

d

p(f

|

d)γa (d) γg (d)

(5.3.5)

(5.3.6)

92

DRAFT April 24, 2015

Inference in Multiply Connected Graphs

e

c

b

g

a

d

f

p(e)p(g

d, e)
|

p(c

a)
|

p(b)p(d
|

a, b)

p(b)p(d

a, b)
|

γE (d, g)

γE (d, g)

p(a)

p(a)

p(a)γB (d, a)

p(a)γB (d, a)

p(f

d)

|

p(f

d)
|

p(f

d)
|

d)γG (d)

p(f

|

p(f

d)γG (d) γA (d)
|

γD (f )

Figure 5.6: The bucket elimination algorithm applied to the graph ﬁg(2.1). At each stage, at least one
node is eliminated from the graph. The second stage of eliminating c is trivial since (cid:80)
a) = 1 and has
therefore been skipped over since this bucket does not send any message.

c p(c
|

We illustrate this in ﬁg(5.6). Initially, we deﬁne an ordering of the variables, beginning with the one that
we wish to ﬁnd the marginal for – a suitable ordering is therefore, f, d, a, g, b, c, e. Then starting with the
highest bucket e (according to our ordering f, d, a, g, b, c, e), we put all the potentials that mention e in the
e bucket. Continuing with the next highest bucket, c, we put all the remaining potentials that mention c
in this c bucket, etc. The result of this initialisation procedure is that terms (conditional distributions) in
the DAG are distributed over the buckets, as shown in the left most column of ﬁg(5.6). Eliminating then
the highest bucket e, we pass a message to node g. Immediately, we can also eliminate bucket c since this
sums to unity. In the next column, we have now two fewer buckets, and we eliminate the highest remaining
bucket, this time b, passing a message to bucket a, and so on.

There are some important observations we can make about bucket elimination:

1. To compute say p(x2

evidence) we need to re-order the variables (so that the required marginal variable
is labelled x1) and repeat bucket elimination. Hence each query (calculation of a marginal in this
It would be more eﬃcient to reuse messages, rather than
case) requires re-running the algorithm.
recalculating them each time.

|

2. In general, bucket elimination constructs multi-variable messages γ from bucket to bucket. The storage
requirements of a multi-variable message are exponential in the number of variables of the message.

3. For trees we can always choose a variable ordering to render the computational complexity to be
linear in the number of variables. Such an ordering is called perfect, deﬁnition(6.9), and indeed it can
be shown that a perfect ordering can always easily be found for singly connected graphs (see [92]).
However, orderings exist for which bucket elimination will be extremely ineﬃcient.

5.3.2 Loop-cut conditioning

For multiply connected distributions we run into some diﬃculty with the message passing routines such as
the sum-product algorithm which are designed to work on singly connected graphs only. One way to solve
the diﬃculties of multiply connected (loopy) graphs is to identify nodes that, when removed, would reveal
a singly connected subgraph[236]. Consider the example of ﬁg(5.7). Imagine that we wish to calculate a

DRAFT April 24, 2015

93

Message Passing for Continuous Distributions

c

a

f

b

g

d

(a)

e

c

b

g

e

a

f

d

(b)

Figure 5.7: A multiply con-
nected graph (a) reduced to a
singly connected graph (b) by
conditioning on the variable c.

marginal, say p(d). Then

(cid:88)

(cid:88)

p(d) =

c

a,b,e,f,g

a)p(a)
p(c
|
(cid:125)
(cid:123)(cid:122)
(cid:124)
p∗(a)

p(d
|

a, b)p(b) p(f

c, d)
|
(cid:124) (cid:123)(cid:122) (cid:125)
p∗(f |d)

p(g

d, e)
|

(5.3.7)

where the p∗ potentials are not necessarily distributions. For each state of c, the product of the new po-
tentials on the variables a, b, e, f, g is singly connected, so that standard singly connected message passing
can be used to perform inference. We will need to perform inference for each state of variable c, each state
deﬁning a new singly connected graph (with the same structure) but with modiﬁed potentials.

C

, called the loop cut set and run singly connected inference
More generally, we can deﬁne a set of variables
. This can also be used for ﬁnding the most likely state of a
for each joint state of the cut-set variables
multiply connected joint distribution as well. Hence, for a computational price exponential in the loop-cut
size, we can calculate the marginals (or the most likely state) for a multiply connected distribution. However,
determining a small cut set is in general diﬃcult, and there is no guarantee that this will anyway be small for
a given graph. Whilst this method is able to handle loops in a general manner, it is not particularly elegant
since the concept of messages now only applies conditioned on the cut set variables, and how to re-use
messages for inference of additional quantities of interest becomes unclear. We will discuss an alternative
method for handling multiply connected distributions in chapter(6).

C

5.4 Message Passing for Continuous Distributions

For parametric continuous distributions p(x
θx), message passing corresponds to passing parameters θ of
|
the distributions. For the sum-product algorithm, this requires that the operations of multiplication and
integration over the variables are closed with respect to the family of distributions. This is the case, for
example, for the Gaussian distribution – the marginal (integral) of a Gaussian is another Gaussian, and
the product of two Gaussians is a Gaussian, see section(8.4). This means that we can then implement
the sum-product algorithm based on passing mean and covariance parameters. To implement this requires
some tedious algebra to compute the appropriate message parameter updates. At this stage, the complex-
ities from performing such calculations are a potential distraction, though the interested reader may refer
to demoSumprodGaussMoment.m, demoSumprodGaussCanon.m and demoSumprodGaussCanonLDS.m and also
chapter(24) for examples of message passing with Gaussians. For more general exponential family distri-
butions, message passing is essentially straightforward, though again the speciﬁcs of the updates may be
tedious to work out. In cases where the operations of marginalisation and products are not closed within the
family, the distributions need to be projected back to the chosen message family. Expectation propagation,
section(28.8), is relevant in this case.

5.5 Summary

•

•

94

For a tree-structured factor graph, non-mixed inference is essentially linear in the number of nodes in the
graph (provided the variables are discrete or the inference operations form a tractable closed family).

Computation on trees can be achieved using local ‘message passing’ algorithms, analogous to dynamic
programming.

DRAFT April 24, 2015

Code

•

•

•

•

•

The sum-product and max-product algorithm are particularly useful for computing marginal and most likely
inferences respectively.

Message-passing also holds for continuous variables based on passing messages that update the parameters
of the distribution.

Shortest-path problems can be solved using such message-passing approaches.

Inference in non-trees (multiply connected distributions) is more complex since there is a ﬁll-in eﬀect when
variables are eliminated that adds additional links to the graph.

Inference in multiply connected graphs can be achieved using techniques such as cut-set conditioning which,
by conditioning on a subset of variables, reveal a singly connected structure. However, this is generally
ineﬃcient since the messages cannot be readily re-used.

A take-home message from this chapter is that (non-mixed) inference in singly connected structures is usu-
ally computationally tractable. Notable exceptions are when the message passing operations are not closed
within the message family, or representing messages explicitly requires an exponential amount of space. This
happens for example when the distribution can contain both discrete and continuous variables, such as the
Switching Linear Dynamical system, which we discuss in chapter(25).

Broadly speaking, inference in multiply connected structures is more complex and may be intractable.
However, we do not want to give the impression that this is always the case. Notable exceptions are:
ﬁnding the most likely state in an attractive pairwise MN, section(28.9); ﬁnding the most likely state and
marginals in a binary planar MN with pure interactions, see for example [127, 260]. For N variables in the
graph, a naive use of general purpose routines such as the junction tree algorithm for these inferences would
result in an O (cid:0)2N (cid:1) computation, whereas clever algorithms are able to return the exact results in O (cid:0)N 3(cid:1)
operations. Of interest is bond propagation[191] which is an intuitive node elimination method to perform
marginal inference in pure-interaction Ising models.

5.6 Code

The code below implements message passing on a tree structured factor graph. The FG is stored as an
adjacency matrix with the message between FG node i and FG node j given in Ai,j.
FactorGraph.m: Return a factor graph adjacency matrix and message numbers
sumprodFG.m: Sum-Product algorithm on a factor graph

In general it is recommended to work in log-space in the Max-Product case, particularly for large graphs
since the product of messages can become very small. The code provided does not work in log space and
as such may not work on large graphs; writing this using log-messages is straightforward but leads to less
readable code. An implementation based on log-messages is left as an exercise for the interested reader.
maxprodFG.m: Max-Product algorithm on a factor graph
maxNprodFG.m: N -Max-Product algorithm on a factor graph

5.6.1 Factor graph examples

For the distribution from ﬁg(5.3), the following code ﬁnds the marginals and most likely joint states. The
number of states of each variable is chosen at random.
demoSumprod.m: Test the Sum-Product algorithm
demoMaxprod.m: Test the Max-Product algorithm
demoMaxNprod.m: Test the Max-N -Product algorithm

5.6.2 Most probable and shortest path

mostprobablepath.m: Most probable path
demoMostProbablePath.m: Most probable versus shortest path demo

DRAFT April 24, 2015

95

Exercises

demoShortestPath.m: The shortest path demo works for both positive and negative edge weights.
negative weight cycles exist, the code ﬁnds the best length N shortest path.
mostprobablepathmult.m: Most probable path – multi-source, multi-sink
demoMostProbablePathMult.m: Demo of most probable path – multi-source, multi-sink

If

5.6.3 Bucket elimination

The eﬃciency of Bucket Elimination depends critically on the elimination sequence chosen. In the demon-
stration below we ﬁnd the marginal of a variable in the Chest Clinic exercise using a randomly chosen
elimination order. The desired marginal variable is speciﬁed as the last to be eliminated. For comparison
we use an elimination sequence based on decimating a triangulated graph of the model, as discussed in
section(6.5.1), again under the constraint that the last variable to be ‘decimated’ is the marginal variable of
interest. For this smarter choice of elimination sequence, the complexity of computing this single marginal
is roughly the same as that for the Junction Tree algorithm, using the same triangulation.
bucketelim.m: Bucket Elimination
demoBucketElim.m: Demo Bucket Elimination

5.6.4 Message passing on Gaussians

The following code hints at how message passing may be implemented for continuous distributions. The
reader is referred to the BRMLtoolbox for further details and also section(8.4) for the algebraic manipu-
lations required to perform marginalisation and products of Gaussians. The same principle holds for any
family of distributions which is closed under products and marginalisation, and the reader may wish to
implement speciﬁc families following the method outlined for Gaussians.
demoSumprodGaussMoment.m: Sum-product message passing based on Gaussian Moment parameterisation

5.7 Exercises

Exercise 5.1. Given a pairwise singly connected Markov network of the form

p(x) =

1
Z

(cid:89)

i∼j

φ (xi, xj)

(5.7.1)

explain how to eﬃciently compute the normalisation factor (also called the partition function) Z as a function
of the potentials φ.

Exercise 5.2. Consider a pairwise Markov network deﬁned on binary variables:

p(x) = φ(x1, x100)

99
(cid:89)

i=1

φ(xi, xi+1)

Is it possible to compute argmax
x1,...,x100

p(x) eﬃciently?

(5.7.2)

Exercise 5.3. You are employed by a web start up company that designs virtual environments, in which
players can move between rooms. The rooms which are accessible from another in one time step is given by
the 100
100 matrix M, stored in virtualworlds.mat, where Mij = 1 means that there is a door between
rooms i and j (Mij = Mji). Mij = 0 means that there is no door between rooms i and j. Mii = 1 meaning
that in one time step, one can stay in the same room. You can visualise this matrix by typing imagesc(M).

×

1. Write a list of rooms which cannot be reached from room 2 after 10 time steps.

2. The manager complains that takes at least 13 time steps to get from room 1 to room 100. Is this true?

3. Find the most likely path (sequence of rooms) to get from room 1 to room 100.

4. If a single player were to jump randomly from one room to another (or stay in the same room), with no
1 the player will be in room 1? Assume

preference between rooms, what is the probability at time t
that eﬀectively an inﬁnite amount of time has passed and the player began in room 1 at t = 1.

(cid:29)

96

DRAFT April 24, 2015

Exercises

5. If two players are jumping randomly between rooms (or staying in the same room), explain how to
compute the probability that, after an inﬁnite amount of time, at least one of them will be in room 1?
Assume that both players begin in room 1.

Exercise 5.4. Consider the hidden Markov model:

p(v1, . . . , vT , h1, . . . , hT ) = p(h1)p(v1

T
(cid:89)

t=2

h1)
|

p(vt

ht)p(ht
|

ht−1)
|

in which dom(ht) =

1, . . . , H
{

}

and dom(vt) =

1, . . . , V
{

}

for all t = 1, . . . , T .

1. Draw a belief network representation of the above distribution.

2. Draw a factor graph representation of the above distribution.

(5.7.3)

3. Use the factor graph to derive a Sum-Product algorithm to compute marginals p(ht

the sequence order of messages passed on your factor graph.

v1, . . . , vT ). Explain
|

4. Explain how to compute p(ht, ht+1

v1, . . . , vT ).
|

5. Show that the belief network for p(h1, . . . , hT ) is a simple linear chain, whilst p(v1, . . . , vT ) is a fully

connected cascade belief network.

Exercise 5.5. For a singly connected Markov Network, p(x) = p(x1, . . . , xn), the computation of a marginal
p(xi) can be carried out eﬃciently. Similarly, the most likely joint state x∗ = arg maxx1,...,xn p(x) can be
computed eﬃciently. Explain when the most likely joint state of a marginal can be computed eﬃciently, i.e.
p(x1, . . . , xm) for m < n?
under what circumstances could one eﬃciently (in O (m) time) compute argmax
x1,x2,...,xm

Exercise 5.6. Consider the internet with webpages labelled 1, . . . , N . If webpage j has a link to webpage i,
then we place an element of the matrix Lij = 1, otherwise Lij = 0. By considering a random jump from
webpage j to webpage i to be given by the transition probability

Mij =

Lij
i Lij

(cid:80)

(5.7.4)

what is the probability that after an inﬁnite amount of random surﬁng, one ends up on webpage i? How
could you relate this to the potential ‘relevance’ of a webpage in terms of a search engine?

Exercise 5.7. A special time-homogeneous hidden Markov model is given by

p(x1, . . . , xT , y1, . . . , yT , h1, . . . , hT ) = p(x1

h1)p(y1
|

h1)p(h1)
|

T
(cid:89)

t=2

p(ht

ht−1)p(xt
|

ht)p(yt
|

ht)
|

(5.7.5)

The variable xt has 4 states, dom(xt) =
yt has 4 states, dom(yt) =
The HMM models the following (ﬁctitious) process:

. The hidden or latent variable ht has 5 states, dom(ht) =
A, C, G, T
}
{

(numerically labelled as states 1,2,3,4). The variable
.
}

1, . . . , 5
{

A, C, G, T

}

{

In humans, Z-factor proteins are a sequence on states of the variables x1, x2, . . . , xT . In bananas Z-factor
proteins are also present, but represented by a diﬀerent sequence y1, y2, . . . , yT . Given a sequence x1, . . . , xT
from a human, the task is to ﬁnd the corresponding sequence y1, . . . , yT in the banana by ﬁrst ﬁnding the
most likely joint latent sequence, and then the most likely banana sequence given this optimal latent sequence.
That is, we require

argmax
y1,...,yT

p(y1, . . . , yT

1, . . . , h∗
h∗
T )

|

where

1, . . . , h∗
h∗

T = argmax
h1,...,hT

p(h1, . . . , hT

x1, . . . , xT )
|

(5.7.6)

(5.7.7)

The ﬁle banana.mat contains the emission distributions pxgh (p(x
(p(ht

h)) and transition phtghtm
|
ht−1)). The initial hidden distribution is given in ph1 (p(h1)). The observed x sequence is given in x.

h)), pygh (p(y
|

|

DRAFT April 24, 2015

97

Exercises

1. Explain mathematically and in detail how to compute the optimal y-sequence, using the two-stage

procedure as stated above.

2. Write a MATLAB routine that computes and displays the optimal y-sequence, given the observed

x-sequence. Your routine must make use of the Factor Graph formalism.

3. Explain whether or not it is computationally tractable to compute

argmax
y1,...,yT

p(y1, . . . , yT

x1, . . . , xT )
|

(5.7.8)

4. Bonus question: By considering y1, . . . , yT as parameters, explain how the EM algorithm, section(11.2),
x1, . . . , xT ). Implement this approach with a suitable initiali-
|

may be used to ﬁnd argmax
y1,...,yT

p(y1, . . . , yT

sation for the optimal parameters y1, . . . , yT .

Exercise 5.8.
There are a set of ﬁrstnames: david, anton, fred, jim, barry which are numbered 1
(david) to 5 (barry) and a set of surnames: barber, ilsung, fox, chain, fitzwilliam, quinceadams,
grafvonunterhosen, which are numbered 1 (barber) to 7 (grafvonunterhosen). A string is generated by
ﬁrst randomly sampling a character from a to z. Then we either continue this random character sampling
with probability 0.8, or we start to generate a ﬁrstname. A ﬁrstname is chosen uniformly at random.
After generating a ﬁrstname, we generate a character at random. We continue to generate another letter at
random with probability 0.8, or start to generate a surname (chosen uniformly from the set of surnames). We
continue until the last letter of the surname is generated. The process then goes back to start (unless we are
at the end timepoint T = 10000). For example, we might generate then dtyjimdfilsungffdavidmjfox....
The catch is that the process of character generation is very noisy. The character we want to generate is only
generated correctly with probability 0.3, with probability 0.7 that another character (uniformly at random)
will be generated. Given the 10000 character sequence in the ﬁle noisystring.mat, you must decode the
noisystring to ﬁnd the most likely intended ‘clean’ sequence. Once you have this sequence, you will be able to
ﬁnd a set of (ﬁrstname,surname) pairs as you traverse along the clean sequence. Construct a matrix m(i, j)
with the counts of the number of occurrences of the pair (f irstname(i), surname(j)) in your clean sequence
and display this matrix.

A security company is employed to keep watch on the behaviour of people moving through
Exercise 5.9.
a train station. Using video cameras they are able to track the x, y position of 500 people. The matrix in
drunkproblemX.mat contains the position of the 500 people through time, with a 1 in the matrix representing
that a person is occupying that position. This matrix is generated using the program drunkmover.m which
you may wish to examine. If a person moves out of the grid, they are moved back into a random position in
the grid, as described in drunkmover.m. All but one of the people move only to a single neighbouring point
on the x-y grid in one time step. However, person 1 is a fast and dangerous drunk that we want to track.
The drunk can move from (x, y) to (x

2) at the next time step.

2, y

±

±

Devise a method to track where you think the dangerous drunk is and give a list of the (x, y) coordinates of
the single most likely path the drunk took through the station.

−

−
1 to t with transition matrix pbull(t, t

The ﬁle BearBulldata contains the price of an asset through T = 200 timepoints. The
Exercise 5.10.
1 to t
price takes values from 1 to 100. If the market is in a ‘bear’ state, the price changes from time t
1). If the market is in the ‘bull’ state, the price changes from
with probability transition matrix pbear(t, t
1). If the market is in a ‘bear’ state it will remain so with
time t
probability 0.8. If the market is in a bull state it will remain so with probability 0.7. You may assume that
at timestep 1, the market is uniformly in either a bear or bull state and also that the price distribution at
timestep 1 is uniform. Use this model to compute the probability of the price at time T + 1 given all the
price(1 : T )). Using this probability, compute the
observed prices from time 1 to T; that is p(price(T + 1)
|
expected gain price(T + 1)
price(T ) in the price of the asset, and also the standard deviation in this price
gain price(T + 1)

Exercise 5.11.
only very simple controls that allow you to accelerate at time t an amount ai(t)

−
You ﬁnd yourself adrift in outer space after a space station disaster. Your rocket suit has
in each of three

−
price(T ).

−

−

∈ {−

1, 0, 1
}

98

DRAFT April 24, 2015

++

++

++

++

Exercises

dimensions of space i
1, 2, 3
}
to provide an acceleration vector

∈ {

independently. At each time t, you can therefore command your spacesuit



at =





 ,

a1(t)
a2(t)
a3(t)

ai(t)

1, 0, +1

∈ {−

}

(5.7.9)

According to discrete time Newton’s laws, this changes the velocity vt and position xt according to the
Markovian updates

vt+1 = vt + δat
xt+1 = xt + δvt

(5.7.10)

(5.7.11)

where δ = 0.1 is a given value corresponding to a unit of real time. Each time an acceleration +1 or
1
−
is applied a unit amount of fuel is used. Your task is to get from the origin x1 = (0, 0, 0)T at time 1 to
6.97, 8.59)T at time 102 using the minimum amount
rendezvous with the rescue station at x102 = (4.71,
−
of fuel (it doesn’t matter what speed you have, as long as you rendezvous with the rescue station). At time
t = 1, you are stationary, v1 = (0, 0, 0)T. The total amount of fuel used is (cid:80)100
t=1

(cid:80)3

i=1 |

.
ai(t)
|

1. For each of the three spatial dimensions i

, write down a single equation that relates ai(t),

t

1, 2, . . . , 100

∈ {

}

∈ {
to the rescue station at x102.

1, 2, 3
}

2. What is the minimum amount of fuel that can be used to rendezvous with the rescue station at time

102?

3. Assuming that there is a sequence a1, . . . , a100 that will obtain a rendezvous with the rescue station at
time 102, explain if you believe there is an eﬃcient way to calculate the minimum amount of fuel. If
there is an eﬃcient algorithm, state it. Otherwise explain why no eﬃcient algorithm is available.

An algorithm exists (for example Dijkstra’s algorithm) that ﬁnds the minimum weighted
Exercise 5.12.
path between two speciﬁed nodes on a graph; the algorithm however only generally works if all weights are
non-negative. You have a minimum weighted path problem but with weights uij that can be negative. A
friend suggests that you can still use Dijkstra’s algorithm by making a new set of edge weights that are all
u∗ where u∗ is the minimum value of all the uij weights. Explain why this will not
non-negative, wij = uij
−
generally give the correct minimum weight path.

Exercise 5.13.
It’s the year 2515 and the tax collector Simo Hurtta needs to go on a trip starting from
planet 1 and ending on planet 1725. Interplanetary travel is only possible between certain planets, with the
xj)2 minus
cost of going from planet i to planet j equal to the Euclidian distance between the planets
the tax that he will collect on planet j. Given the set of planet positions x, interplanetary travel possibilities
(the matrix Aij = 1 if it is possible to go from planet i to planet j and collectable taxes for each planet (t)
(see SimoHurtta.mat), what is the minimum cost of travelling from planet 1 to planet 1725? (Assume that
Hurrta does not collect taxes from planet 1.)

(xi

(cid:113)

−

++

++

DRAFT April 24, 2015

99

Exercises

100

DRAFT April 24, 2015

CHAPTER 6

The Junction Tree Algorithm

When the distribution is multiply-connected it would be useful to have a generic inference approach that is
eﬃcient in its reuse of messages. In this chapter we discuss an important structure, the junction tree, that by
clustering variables enables one to perform message-passing eﬃciently (although the structure on which the
message-passing occurs may consist of intractably large clusters). The most important thing is the junction
tree itself, based on which diﬀerent message-passing procedures can be considered. The junction tree helps
forge links with the computational complexity of inference in ﬁelds from computer science to statistics and
physics.

6.1 Clustering Variables

In chapter(5) we discussed eﬃcient inference for singly-connected graphs, for which variable elimination and
message passing schemes are appropriate. In the multiply connected case, however, one cannot in general
perform inference by passing messages only along existing links in the graph. The idea behind the junction
tree algorithm (JTA) is to form a new representation of the graph in which variables are clustered together,
resulting in a singly-connected graph in the cluster variables (albeit on a diﬀerent graph). The main focus
of the development will be on marginal inference, though similar techniques apply to diﬀerent inferences,
such as ﬁnding the most probable state of the distribution.

At this stage it is important to point out that the JTA is not a magic method to deal with intractabilities
resulting from multiply connected graphs; it is simply a way to perform correct inference on a multiply
connected graph by transforming to a singly connected structure. Carrying out the inference on the resulting
junction tree may still be computationally intractable. For example, the junction tree representation of a
general two-dimensional Ising model is a single supernode containing all the variables. Inference in this case
is exponentially complex in the number of variables. Nevertheless, even in cases where implementing the
JTA may be intractable, the JTA provides useful insight into the representation of distributions that can
form the basis for approximate inference. In this sense the JTA is key to understanding issues related to
representations and complexity of inference and is central to the development of eﬃcient inference algorithms.

6.1.1 Reparameterisation

Consider the chain

c)p(c
b)p(b
p(a, b, c, d) = p(a
|
|
|

d)p(d)

From the deﬁnition of conditional probability, we can reexpress this as

p(a, b, c, d) =

p(a, b)
p(b)

p(b, c)
p(c)

p(c, d)
p(d)

p(d) =

p(a, b)p(b, c)p(c, d)
p(b)p(c)

101

(6.1.1)

(6.1.2)

a

b

c

(a)

d

a, b, c

b, c

(b)

b, c, d

6.1:

Figure
φ(a, b, c)φ(b, c, d).
representation of (a).

(a) Markov

(b) Clique

Clique Graphs

network
graph

A useful insight is that the distribution can therefore be written as a product of marginal distributions, di-
vided by a product of the intersection of the marginal distributions: Looking at the numerator p(a, b)p(b, c)p(c, d)
this cannot be a distribution over a, b, c, d since we are overcounting b and c, where this overcounting of
, which have b as their intersection. Similarly,
b arises from the overlap between the sets
Intuitively, we need to
the overcounting of c arises from the overlap between the sets
correct for this overcounting by dividing by the distribution on the intersections. Given the transformed
representation as a product of marginals divided by a product of their intersection (the right equation in
(6.1.2)), a marginal such as p(a, b) can be read oﬀ directly from the factors in the new expression. The aim
of the junction tree algorithm is to form just such a representation of the distribution which contains the
marginals explicitly. We want to do this in a way that works for belief and Markov networks, and also deals
with the multiply connected case. In order to do so, an appropriate way to parameterise the distribution is
in terms of a clique graph, as described in the next section.

.
c, d
}
{

b, c
}
{

a, b
}

b, c
}

and

and

{

{

6.2 Clique Graphs

Deﬁnition 6.1 (Clique Graph). A clique graph consists of a set of potentials, φ1(
deﬁned on a set of variables
s =

X
i. For neighbouring cliques on the graph, deﬁned on sets of variables
j is called the separator and has a corresponding potential φs(

j, the intersection

X

1), . . . , φn(

X
clique graph represents the function

∩ X

X

X
i
X

n) each
i and
s). A

X
X

(cid:81)
(cid:81)

c φc(
s φs(

X
X

c)
s)

(6.2.1)

For notational simplicity we will usually drop the clique potential index c. Graphically clique potentials are
represented by circles/ovals, and separator potentials by rectangles.

1

X

1

2

X

∩ X

2

X

The graph on the left represents φ(

X

1)φ(

X

2)/φ(

1

X

∩ X

2).

Clique graphs translate Markov networks into structures convenient for carrying out inference. Consider
the Markov network in ﬁg(6.1a)

p(a, b, c, d) =

φ(a, b, c)φ(b, c, d)
Z

(6.2.2)

An equivalent representation is given by the clique graph in ﬁg(6.1b), deﬁned as the product of the numerator
clique potentials, divided by the product of the separator potentials. In this case the separator potential
may be set to the normalisation constant Z. By summing we have

Zp(a, b, c) = φ(a, b, c)

(cid:88)

d

φ(b, c, d),

Zp(b, c, d) = φ(b, c, d)

(cid:88)

a

φ(a, b, c)

(6.2.3)

Multiplying the two expressions, we have

Z2p(a, b, c)p(b, c, d) =

φ(a, b, c)

(cid:32)

(cid:33) (cid:32)

φ(b, c, d)

φ(b, c, d)

(cid:88)

a

(cid:88)

d

(cid:33)

φ(a, b, c)

= Z2p(a, b, c, d)

(cid:88)

a,d

p(a, b, c, d)

102

DRAFT April 24, 2015

Clique Graphs

In other words

p(a, b, c, d) =

p(a, b, c)p(b, c, d)
p(c, b)

(6.2.4)

(6.2.5)

The important observation is that the distribution can be written in terms of its marginals on the variables
in the original cliques and that, as a clique graph, it has the same structure as before. All that has changed is
that the original clique potentials have been replaced by the marginals of the distribution and the separator
p(c, b).
by the marginal deﬁned on the separator variables φ(a, b, c)
The usefulness of this representation is that if we are interested in the marginal p(a, b, c), this can be read oﬀ
from the transformed clique potential. To make use of this representation, we require a systematic way of
transforming the clique graph potentials so that at the end of the transformation the new potentials contain
the marginals of the distribution.

p(a, b, c), φ(b, c, d)

p(b, c, d), Z

→

→

→

Remark 6.1. Note that, whilst visually similar, a Factor Graph and a Clique Graph are diﬀerent rep-
resentations. In a clique graph the nodes contain sets of variables, which may share variables with other
nodes.

6.2.1 Absorption

and

V

W

, sharing the variables

S

in common. In this case, the distribution

Consider neighbouring cliques
is
on the variables

=

X

V ∪ W
)

φ (

) =

p(

X

)φ (
W
)

V
φ (

S

and our aim is to ﬁnd a new representation

p(

) =

ˆφ (

) ˆφ (
W
V
ˆφ (
)
in which the potentials are given by

X

S

)

(6.2.6)

)

φ (

V

)

φ (

S

φ (

)

W

(6.2.7)

ˆφ (
V

)

ˆφ (
S

)

ˆφ (

)

W

ˆφ (

V

) = p(

V

), ˆφ (

W

) = p(

W

), ˆφ (

) = p(

S

)

S

(6.2.8)

@@ In this example, we can explicitly work out the new potentials as function of the old potentials by computing

the marginals as follows:

) =

p(

W

(cid:88)

V\S

) =

p(

X

(cid:88)

φ (

V\S

and

)φ (
W
)

V
φ (

S

)

= φ (

W

)

(cid:80)

V\S φ (
)
φ (

)

V

S

) =

p(

V

(cid:88)

W\S

) =

p(

X

(cid:88)

W\S

φ (

)φ (
W
)

V
φ (

)

= φ (

)

V

S

(cid:80)

W\S φ (
)
φ (

)

W

S

(6.2.9)

(6.2.10)

There is a symmetry present in the two equations above – they are the same under interchanging

. One way to describe these equations is through ‘absorption’. We say that the cluster

W
information from cluster

W
by the following updating procedure. First we deﬁne a new separator

V

φ∗ (
S

) =

(cid:88)

V\S

)

φ (

V

and reﬁne the

W

potential using

φ∗ (

W

) = φ (

W

)

φ∗ (
φ (

)
S
)
S

DRAFT April 24, 2015

(6.2.11)

(6.2.12)

103

and
V
‘absorbs’

Clique Graphs

Figure 6.2: An example absorption schedule on a
clique tree. Many valid schedules exist under the con-
straint that messages can only be passed to a neigh-
bour when all other messages have been received.

A

4

3

→ ←

B

C

6

←

1

→ 2

→

←

1

0

E

D

8

9
→←

→

7

←

5

F

The advantage of this interpretation is that the new representation is still a valid clique graph representation
of the distribution since

φ (

)φ∗ (
V
W
φ∗ (
)

S

)

=

φ (

V

)φ (
φ∗ (

W

S

) φ∗(S)
φ(S)
)

=

S

φ (

)φ (
W
)

V
φ (

)

= p(

X

)

(6.2.13)

),
For this simple two clique graph, we see that after
W
which can be veriﬁed by comparing the right of equation (6.2.9) with equation (6.2.12). With these new
updated potentials, we now go back along the graph, from
then φ∗ (
directions, then the separator potential will contain p(
To see this, consider absorbing from

W
has participated in absorption along both
) (this is not the case after only a single absorption).

S
using the updated potentials φ∗ (

W
). After the separator

) contains the marginal p(

absorbs information from

absorbs information from

) and φ∗ (

. After

) = p(

W

W

to

to

V

V

V

V

V

S

)

then φ∗ (

S

)

=

(cid:88)

{W∪V}\S

φ (

)φ (
)

)

V

= p(

S

)

(6.2.14)

W
φ (

S

W

S

φ∗∗ (

(cid:88)

) =

φ∗ (

(cid:88)

φ (

) =

S

W

S
Continuing, we have the new potential φ∗ (

W\S

W\S

W

V
)φ∗ (
)

W
φ (

φ∗ (

) =

V

φ (

)φ∗∗ (
V
φ∗ (
)

S

)

S

=

φ (

V

) given by

) (cid:80)

V
W\S φ (
φ∗ (

W

S

)φ∗ (
)

S

)/φ (

)

S

=

(cid:80)

Hence, in terms of equation (6.2.7), the new representation is ˆφ (
φ∗ (

).

W

V

Deﬁnition 6.2 (Absorption).

W\S φ (
φ (

V
)

S
) = φ∗ (

)φ (

W

)

= p(

V

)

(6.2.15)

), ˆφ (

S

) = φ∗∗ (

), ˆφ (

) =

W

S

V

V

W

and

Let
separator, and let φ (
Absorption from
to
) with
φ (

V

V
W

W

be neighbours in a clique graph, let

), φ (

) and φ (

W

through

S

be their
S
) be their potentials.
) and

replaces the tables φ (

S

S

)

φ (

V

φ∗ (

S

)

φ∗ (

)

W

φ∗ (

) =

S

(cid:88)

V\S

)

φ (

V

φ∗ (

) = φ (

)

W

W

φ∗ (
φ (

)
S
)
S

(6.2.16)

We say that clique
. The
potentials are written on the clique graph (left) to highlight the
potential updating.

absorbs information from clique

W

V

6.2.2 Absorption schedule on clique trees

Having deﬁned the local message propagation approach, we need to deﬁne an update ordering for absorption.
In general, a node
has
received a message from each of its other neighbours. We continue this sequence of absorptions until a
message has been passed in both directions along every link. See, for example, ﬁg(6.2). Note that there are
many valid message passing schemes in this case.

can send exactly one message to a neighbour

, and it may only be sent when

W

V

V

104

DRAFT April 24, 2015

Junction Trees

x1

x4

x1, x4

x1, x4

x4

x4

x4

x4

x3

x2

x3, x4

(a)

x4

(b)

x2, x4

x3, x4

x2, x4

(c)

Figure 6.3: (a): Singly connected Markov network. (b): Clique graph. (c): Clique tree.

Deﬁnition 6.3 (Absorption Schedule). A clique can send a message to a neighbour, provided it has already
received messages from all other neighbours.

6.3 Junction Trees

There are a few stages we need to go through in order to transform a distribution into an appropriate
structure for inference. Initially we explain how to do this for singly connected structures before moving
onto the multiply connected case.

Consider the singly connected Markov network, ﬁg(6.3a)

p(x1, x2, x3, x4) = φ(x1, x4)φ(x2, x4)φ(x3, x4)

(6.3.1)

The clique graph of this singly connected Markov network is multiply connected, ﬁg(6.3b), where the sep-
arator potentials are all set to unity. Nevertheless, let’s try to reexpress the Markov network in terms of
marginals. First we have the relations

p(x1, x4) =

p(x2, x4) =

p(x3, x4) =

(cid:88)

x2,x3
(cid:88)

x1,x3
(cid:88)

x1,x2

p(x1, x2, x3, x4) = φ(x1, x4)

p(x1, x2, x3, x4) = φ(x2, x4)

p(x1, x2, x3, x4) = φ(x3, x4)

(cid:88)

x2
(cid:88)

x1
(cid:88)

x1

φ(x2, x4)

φ(x1, x4)

φ(x1, x4)

(cid:88)

x3
(cid:88)

x3
(cid:88)

x2

φ(x3, x4)

φ(x3, x4)

φ(x2, x4)

(6.3.2)

(6.3.3)

(6.3.4)

Taking the product of the three marginals, we have

p(x1, x4)p(x2, x4)p(x3, x4) = φ(x1, x4)φ(x2, x4)φ(x3, x4)

(cid:32)

(cid:88)

x1

(cid:124)

φ(x1, x4)

(cid:88)

x2

(cid:88)

x3

φ(x2, x4)

(cid:123)(cid:122)
p(x4)2

(cid:33)2

φ(x3, x4)

(6.3.5)

(cid:125)

This means that the Markov network can be expressed in terms of marginals as

p(x1, x2, x3, x4) =

p(x1, x4)p(x2, x4)p(x3, x4)
p(x4)p(x4)

(6.3.6)

Hence a valid clique graph is also given by the representation ﬁg(6.3c). Indeed, if a variable (here x4) occurs
on every separator in a clique graph loop, one can remove that variable from an arbitrarily chosen separator
in the loop. If this leaves an empty separator, we may then simply remove it. This shows that in such cases
we can transform the clique graph into a clique tree (i.e. a singly connected clique graph). Provided that
the original Markov network is singly connected, one can always form a clique tree in this manner.

DRAFT April 24, 2015

105

6.3.1 The running intersection property

Sticking with the above example, consider the clique tree in ﬁg(6.3)

φ(x3, x4)φ(x1, x4)φ(x2, x4)
φ1(x4)φ2(x4)

Junction Trees

(6.3.7)

as a representation of the distribution (6.3.1) where we set φ1(x4) = φ2(x4) = 1 to make this match. Now
perform absorption on this clique tree:

We absorb (x3, x4) (cid:32) (x1, x4). The new separator is

φ∗
1(x4) =

(cid:88)

x3

φ(x3, x4)

and the new potential is

φ∗(x1, x4) = φ(x1, x4)

φ∗
1(x4)
φ1(x4)

= φ(x1, x4)φ∗

1(x4)

Now (x1, x4) (cid:32) (x2, x4). The new separator is

φ∗
2(x4) =

(cid:88)

x1

φ∗(x1, x4)

and the new potential is

φ∗(x2, x4) = φ(x2, x4)

φ∗
2(x4)
φ2(x4)

= φ(x2, x4)φ∗

2(x4)

(6.3.8)

(6.3.9)

(6.3.10)

(6.3.11)

Since we’ve ‘hit the buﬀers’ in terms of message passing, the potential φ(x2, x4) cannot be updated further.
Let’s examine more carefully the value of this new potential,

φ∗(x2, x4) = φ(x2, x4)φ∗

2(x4) = φ(x2, x4)

φ∗(x1, x4)

(cid:88)

x1

= φ(x2, x4)

(cid:88)

x1

φ(x1, x4)

(cid:88)

x3

φ(x3, x4) =

(cid:88)

x1,x3

p(x1, x2, x3, x4) = p(x2, x4)

(6.3.13)

(6.3.12)

Hence the new potential φ∗(x2, x4) contains the marginal p(x2, x4).

To complete a full round of message passing we need to have passed messages in a valid schedule along both
directions of each separator. To do so, we continue as follows:

We absorb (x2, x4) (cid:32) (x1, x4). The new separator is

φ∗∗
2 (x4) =

(cid:88)

x2

φ∗(x2, x4)

and

φ∗∗(x1, x4) = φ∗(x1, x4)

φ∗∗
2 (x4)
φ∗
2(x4)
x2 φ∗(x2, x4) = (cid:80)

Note that φ∗∗
directions, the separator contains the marginal p(x4). The reader may show that φ∗∗(x1, x4) = p(x1, x4).

x2 p(x2, x4) = p(x4) so that now, after absorbing through both

2 (x4) = (cid:80)

Finally, we absorb (x1, x4) (cid:32) (x3, x4). The new separator is

φ∗∗
1 (x4) =

(cid:88)

x1

φ∗∗(x1, x4) = p(x4)

106

(6.3.16)

DRAFT April 24, 2015

(6.3.14)

(6.3.15)

Junction Trees

and

φ∗(x3, x4) = φ(x3, x4)

φ∗∗
1 (x4)
φ∗
1(x4)

= p(x3, x4)

(6.3.17)

Hence, after a full round of message passing, the new potentials all contain the correct marginals.

The new representation is consistent in the sense that for any (not necessarily neighbouring) cliques

with intersection

, and corresponding potentials φ (

) and φ (

W

V

),

and

V

)

W

(6.3.18)

W

(cid:88)

V\I

) =

φ (

V

I
(cid:88)
φ (

W\I

Note that bidirectional absorption following a valid schedule guarantees local consistency for neighbouring
cliques, as in the example above, provided that we started with a clique tree which is a correct representation
of the distribution. To ensure global consistency, if a variable occurs in two cliques it must be present in all
cliques on any path connecting these cliques. An extreme example would be if we removed the link between
cliques (x3, x4) and (x1, x4). In this case this is still a clique tree
(forest); however global consistency could
not be guaranteed since the information required to make clique (x3, x4) consistent with the rest of the
graph cannot reach this clique.

@@

Formally, the requirement for the propagation of local to global consistency is that the clique tree is a
junction tree, as deﬁned below.

See also exercise(6.12).

++

Deﬁnition 6.4 (Junction Tree). A clique tree is a junction tree if, for each pair of nodes,
on the path between
contain the intersection
property.

, all nodes
. This is also called the running intersection

V ∩ W

and

and

W

W

V

V

From this deﬁnition local consistency will be passed on to any neighbours and the distribution will be
globally consistent. Proofs for these results are contained in [161].

Example 6.1 (A consistent junction tree). To gain some intuition about the meaning of consistency,
consider the junction tree in ﬁg(6.4d). After a full round of message passing on this tree, each link is
consistent, and the product of the potentials divided by the product of the separator potentials is just the
original distribution itself. Imagine that we are interested in calculating the marginal for the node abc. That
requires summing over all the other variables, def gh. If we consider summing over h then, because the link
is consistent,

φ∗ (e, h) = φ∗ (e)

(cid:88)

h

(6.3.19)

φ∗(e,h)
φ∗(e)

so that the ratio (cid:80)
is unity, and the eﬀect of summing over node h is that the link between eh and
h
dce can be removed, along with the separator. The same happens for the link between node eg and dce,
and also for cf to abc. The only nodes remaining are now dce and abc and their separator c, which have so
far been unaﬀected by the summations. We still need to sum out over d and e. Again, because the link is
consistent,

φ∗ (d, c, e) = φ∗ (c)

(cid:88)

de

(6.3.20)

de

φ∗(d,c,e)
so that the ratio (cid:80)
φ∗(c) = 1. The result of the summation of all variables not in abc therefore produces
unity for the cliques and their separators, and the summed potential representation reduces simply to the
potential φ∗ (a, b, c) which is the marginal p(a, b, c). It is clear that a similar eﬀect will happen for other
nodes. We can then obtain the marginals for individual variables by simple brute force summation over the
other variables in that potential, for example p(f ) = (cid:80)

c φ∗ (c, f ).

DRAFT April 24, 2015

107

Constructing a Junction Tree for Singly-Connected Distributions

b

f

d

g

a

e

c

h

(b)

a

e

(a)

abc

c

h

c

c

cf

dce

e

d

g

c

e

dce

e

eg

e

eh

eg

(c)

b

f

c

e

(d)

abc

c

cf

eh

(a): Belief Network.

(d):
(b): Moralised version of (a).
Figure 6.4:
A junction tree. This satisﬁes the running intersection property that for any two nodes which contain a
variable in common, any clique on the path linking the two nodes also contains that variable.

(c): Clique graph of (b).

6.4 Constructing a Junction Tree for Singly-Connected Distributions

6.4.1 Moralisation

For belief networks an initial step is required, which is not required in the case of undirected graphs.

Deﬁnition 6.5 (Moralisation). For each variable x add an undirected link between all parents of x and
to x from its parents by undirected links. This creates a ‘moralised’ Markov
replace the directed link
network.

@@

6.4.2 Forming the clique graph

The clique graph is formed by identifying the cliques in the Markov network and adding a link between
cliques that have a non-empty intersection. Add a separator between the intersecting cliques.

6.4.3 Forming a junction tree from a clique graph

For a singly connected distribution, any maximal weight spanning tree of a clique graph is a junction tree.

Deﬁnition 6.6 (Junction Tree). A junction tree is obtained by ﬁnding a maximal weight spanning tree of
the clique graph. The weight of the tree is deﬁned as the sum of all the separator weights of the tree, where
the separator weight is the number of variables in the separator.

If the clique graph contains loops, then all separators on the loop contain the same variable. By continuing
to remove loop links until a tree is revealed, we obtain a junction tree.

108

DRAFT April 24, 2015

Junction Trees for Multiply-Connected Distributions

a

d

a

b

c

b

c

a

d

b

c

a

d

b

c

(a)

(b)

(c)

(d)

abc

ac

(e)

acd

(a): An undirected graph with a loop.

Figure 6.5:
and c in the subgraph.
representation. (e): Junction tree for (a).

(c): The induced representation for the graph in (a).

(b): Eliminating node d adds a link between a
(d): Equivalent induced

Example 6.2 (Forming a Junction Tree). Consider the Belief Network in ﬁg(6.4a). The moralisation
procedure gives ﬁg(6.4b). Identifying the cliques in this graph, and linking them together gives the clique
graph in ﬁg(6.4c). There are several possible junction trees one could obtain from this clique graph, and
one is given in ﬁg(6.4d).

6.4.4 Assigning potentials to cliques

Deﬁnition 6.7 (Clique Potential Assignment). Given a junction tree and a function deﬁned as the product
of a set of potentials φ (cid:0)
n), a valid clique potential assignment places potentials in JT cliques
whose variables can contain them such that the product of the JT clique potentials, divided by the JT
separator potentials, is equal to the function.

1(cid:1), . . . , φ (
X

X

A simple way to achieve this assignment is to list all the potentials and order the JT cliques arbitrarily.
Then, for each potential, search through the JT cliques until the ﬁrst is encountered for which the potential
variables are a subset of the JT clique variables. Subsequently the potential on each JT clique is taken as
the product of all clique potentials assigned to the JT clique. Lastly, we assign all JT separators to unity.
This approach is taken in jtassignpot.m. Note that in some instances it can be that a junction tree clique
is assigned to unity.

Example 6.3. For the belief network of ﬁg(6.4a), we wish to assign its potentials to the junction tree
ﬁg(6.4d). In this case the assignment is unique and is given by

a, b)

φ (abc) = p(a)p(b)p(c
|
d, c)
φ (dce) = p(d)p(e
|
c)
φ (cf ) = p(f
|
e)
φ (eg) = p(g
|
e)
φ (eh) = p(h
|
All separator potentials are initialised to unity.

(6.4.1)

6.5 Junction Trees for Multiply-Connected Distributions

When the distribution contains loops, the construction outlined in section(6.4) does not result in a junction
tree. The reason is that, due to the loops, variable elimination changes the structure of the remaining graph.

DRAFT April 24, 2015

109

Junction Trees for Multiply-Connected Distributions

To see this, consider the following distribution,

p(a, b, c, d) = φ(a, b)φ(b, c)φ(c, d)φ(d, a)

(6.5.1)

as shown in ﬁg(6.5a). Let’s ﬁrst try to make a clique graph. We have a choice about which variable ﬁrst to
marginalise over. Let’s choose d:
(cid:88)

p(a, b, c) = φ(a, b)φ(b, c)

φ(c, d)φ(d, a)

(6.5.2)

d

The remaining subgraph therefore has an extra connection between a and c, see ﬁg(6.5b). We can express
the joint in terms of the marginals using

p(a, b, c, d) =

p(a, b, c)
d φ(c, d)φ(d, a)

(cid:80)

φ(c, d)φ(d, a)

(6.5.3)

To continue the transformation into marginal form, let’s try to replace the numerator terms with probabil-
ities. We can do this by considering

p(a, c, d) = φ(c, d)φ(d, a)

(cid:88)

b

φ(a, b)φ(b, c)

Plugging this into the above equation, we have

p(a, b, c, d) =

d φ(c, d)φ(d, a) (cid:80)
We recognise that the denominator is simply p(a, c), hence

b φ(a, b)φ(b, c)

(cid:80)

p(a, b, c)p(a, c, d)

p(a, b, c, d) =

p(a, b, c)p(a, c, d)
p(a, c)

.

(6.5.4)

(6.5.5)

(6.5.6)

This means that a valid clique graph for the distribution ﬁg(6.5a) must contain cliques larger than those
in the original distribution. To form a JT based on products of cliques divided by products of separators,
we could start from the induced representation ﬁg(6.5c). Alternatively, we could have marginalised over
variables a and c, and ended up with the equivalent representation ﬁg(6.5d).

Generally, the result from variable elimination and re-representation in terms of the induced graph is that a
link is added between any two variables on a loop (of length 4 or more) which does not have a chord. This
is called triangulation. A Markov network on a triangulated graph can always be written in terms of the
product of marginals divided by the product of separators. Armed with this new induced representation,
we can form a junction tree.

Example 6.4. A slightly more complex loopy distribution is depicted in ﬁg(6.6a),

p(a, b, c, d, e, f ) = φ(a, b)φ(b, c)φ(c, d)φ(d, e)φ(e, f )φ(a, f )φ(b, e)

(6.5.7)

There are diﬀerent induced representations depending on which variables we decide to eliminate. The reader
may convince herself that one such induced representation is given by ﬁg(6.6b).

a

f

b

e

(a)

c

a

d

f

b

e

(b)

c

d

Figure 6.6:
Markov network.
resentation.

(a): Loopy ‘ladder’
(b): Induced rep-

110

DRAFT April 24, 2015

Junction Trees for Multiply-Connected Distributions

Deﬁnition 6.8 (Triangulated (Decomposable) Graph). An undirected graph is triangulated if every loop
of length 4 or more has a chord. An equivalent term is that the graph is decomposable or chordal . From
this deﬁnition, one may show that an undirected graph is triangulated if and only if its clique graph has a
junction tree.

6.5.1 Triangulation algorithms

When a variable is eliminated from a graph, links are added between all the neighbours of the eliminated
variable. A triangulation algorithm is one that produces a graph for which there exists a variable elimination
order that introduces no extra links in the graph.

For discrete variables the complexity of inference scales exponentially with clique sizes in the triangulated
graph since absorption requires computing tables on the cliques.
It is therefore of some interest to ﬁnd
a triangulated graph with small clique sizes. However, ﬁnding the triangulated graph with the smallest
maximal clique is a computationally hard problem for a general graph, and heuristics are unavoidable.
Below we describe two simple algorithms that are generically reasonable, although there may be cases where
an alternative algorithm may be considerably more eﬃcient[56, 28, 206].

Remark 6.2 (‘Triangles’). Note that a triangulated graph is not one in which ‘squares in the original graph
have triangles within them in the triangulated graph’. Whilst this is the case for ﬁg(6.6b), this is not true
for ﬁg(6.10d). The term triangulation refers to the fact that every ‘square’ (i.e. loop of length 4) must have
a ‘triangle’, with edges added until this criterion is satisﬁed. See also ﬁg(6.7).

Greedy variable elimination

An intuitive way to think of triangulation is to ﬁrst start with simplicial nodes, namely those which, when
eliminated do not introduce any extra links in the remaining graph. Next consider a non-simplicial node of
the remaining graph that has the minimal number of neighbours. Then add a link between all neighbours
of this node and then eliminate this node from the graph. Continue until all nodes have been eliminated.
(This procedure corresponds to Rose-Tarjan Elimination[250] with a particular node elimination choice).
By labelling the nodes eliminated in sequence, we obtain a perfect ordering (see below). In the case that
(discrete) variables have diﬀerent numbers of states, a more reﬁned version is to choose the non-simplicial
node i which, when eliminated, leaves the smallest clique table size (the product of the size of all the state
dimensions of the neighbours of node i). See ﬁg(6.8) for an example.

Procedure 6.1 (Variable Elimination). In Variable Elimination, one simply picks any non-deleted node x
in the graph, and then adds links to all the neighbours of x. Node x is then deleted. One repeats this until
all nodes have been deleted[250].

Deﬁnition 6.9 (Perfect Elimination Order). Let the n variables in a Markov network be ordered from 1 to
n. The ordering is perfect if, for each node i, the neighbours of i that are later in the ordering, and i itself,
form a (maximal) clique. This means that when we eliminate the variables in sequence from 1 to n, no
additional links are induced in the remaining marginal graph. A graph which admits a perfect elimination
order is decomposable, and vice versa.

d

a

e

c

b

Figure 6.7: This graph is not triangulated, despite its ‘triangular’
appearance. The loop a

a does not have a chord.

d

c

b

−

−

−

−

DRAFT April 24, 2015

111

The Junction Tree Algorithm

a

a

f

f

b

j

b

j

b

j

b

j

c

d

e

a

g

h

i

f

k

l

(a)

c

d

e

a

g

h

i

f

k

l

(d)

c

d

e

a

g

h

i

f

k

l

(b)

c

d

e

a
a

g

h

i

f
f

k

l

(e)

b

j

b
b

j
j

c

d

e

g

h

i

k

l

(c)

c
c

d
d

e
e

g
g

h
h

i
i

k
k

l
l

(f)

(a): Markov network for which we seek a triangulation via greedy variable elimination. We
Figure 6.8:
(b): We then eliminate variables b, d since these only add a
ﬁrst eliminate the simplicial nodes a, e, l.
(c): There are no simplicial nodes at this stage, and we choose to
single extra link to the induced graph.
(d): We eliminate g and h since these are
eliminate f and i, each elimination adding only a single link.
may be eliminated in any order. (f ): Final triangulation.
simplicial. (e): The remaining variables
{
The variable elimination (partial) order is
where the brackets indicate
,
b, d
}
{
that the order in which the variables inside the bracket are eliminated is irrelevant. Compared with the
triangulation produced by the max-cardinality checking approach in ﬁg(6.10d), this triangulation is more
parsimonious.

c, j, k
}
a, e, l
}

f, i
}

c, j, k

g, h

{

{

}

{

{

}

,

,

,

Whilst this variable elimination guarantees a triangulated graph, its eﬃciency depends heavily on the se-
quence of nodes chosen to be eliminated. Several heuristics for this have been proposed, including the one
below, which corresponds to choosing x to be the node with the minimal number of neighbours.

Maximum cardinality checking

Algorithm(6.1) terminates with success if the graph is triangulated. Not only is this a suﬃcient condition
for a graph to be triangulated, but it is also necessary [287]. It processes each node and the time to process
a node is quadratic in the number of adjacent nodes. This triangulation checking algorithm also suggests
a triangulation construction algorithm – we simply add a link between the two neighbours that caused the
algorithm to FAIL, and then restart the algorithm. The algorithm is restarted from the beginning, not just
continued from the current node. This is important since the new link may change the connectivity between
previously labelled nodes. See ﬁg(6.10) for an example1.

6.6 The Junction Tree Algorithm

We now have all the steps required for inference in multiply connected graphs, given in the procedure below.

Procedure 6.2 (Junction tree algorithm).

1This example is due to David Page www.cs.wisc.edu/∼dpage/cs731

112

DRAFT April 24, 2015

The Junction Tree Algorithm

abf

bf

bcf g

cdhi

di

dei

cf g

cf gj

chi

chik

cj

ck

cjk

jk

jkl

Figure 6.9: Junction tree formed from the triangulation ﬁg(6.8f).
One may verify that this satisﬁes the running intersection prop-
erty.

Algorithm 6.1 A check if a graph is decomposable (triangulated). The graph is triangulated if, after
cycling through all the n nodes in the graph, the FAIL criterion is not encountered.

1: Choose any node in the graph and label it 1.
2: for i = 2 to n do
3:

Choose the node with the most labeled neighbours and label it i.
If any two labeled neighbours of i are not adjacent to each other, FAIL.

4:
5: end for

Where there is more than one node with the most labeled neighbours, the tie may be broken arbitrarily.

Moralisation Marry the parents. This is required only for directed distributions. Note that all the parents
of a variable are married together – a common error is to marry only the ‘neighbouring’ parents.

Triangulation Ensure that every loop of length 4 or more has a chord.

Junction Tree Form a junction tree from cliques of the triangulated graph, removing any unnecessary
links in a loop on the cluster graph. Algorithmically, this can be achieved by ﬁnding a tree with
maximal spanning weight with weight wij given by the number of variables in the separator between
cliques i and j. Alternatively, given a clique elimination order (with the lowest cliques eliminated
ﬁrst), one may connect each clique i to the single neighbouring clique j > i with greatest edge weight
wij.

Potential Assignment Assign potentials to junction tree cliques and set the separator potentials to unity.

Message Propagation Carry out absorption until updates have been passed along both directions of every

link on the JT. The clique marginals can then be read oﬀ from the JT.

An example is given in ﬁg(6.11).

6.6.1 Remarks on the JTA

•

•

The algorithm provides an upper bound on the computation required to calculate marginals in the
graph. There may exist more eﬃcient algorithms in particular cases, although generally it is believed
that there cannot be much more eﬃcient approaches than the JTA since every other approach must
perform a triangulation[160, 187]. One particular special case is that of marginal inference for a binary
variable Markov network on a two-dimensional lattice containing only pure quadratic interactions. In
this case the complexity of computing a marginal inference is O (cid:0)n3(cid:1) where n is the number of variables
in the distribution. This is in contrast to the pessimistic exponential complexity suggested by the JTA.

One might think that the only class of distributions for which essentially a linear time algorithm is
available are singly connected distributions. However, there are decomposable graphs for which the
cliques have limited size meaning that inference is tractable. For example an extended version of the
‘ladder’ in ﬁg(6.6a) has a simple induced decomposable representation ﬁg(6.6b), for which marginal
inference would be linear in the number of rungs in the ladder. Eﬀectively these structures are hyper
trees in which the complexity is then related to the tree width of the graph[86].

DRAFT April 24, 2015

113

The Junction Tree Algorithm

1

3

2

6

5

7

10

1

2

5

7

10

1

2

5

7

10

4

8

9

3

4

8

9

3

4

8

9

11

6

11

6

11

(a)

(b)

(c)

(d)

Figure 6.10: Starting with the Markov network in (a), the maximum cardinality check algorithm proceeds
until (b), where an additional link is required, see (c). One continues until the fully triangulated graph (d)
is found.

a

c

f

b

e

d

g

b

e

a

c

f

d

g

h

i

h

i

(a)

(b)

(a): Original loopy Belief Network.
Figure 6.11:
(b): The moralisation links (dashed) are between
nodes e and f and between nodes f and g. The other
additional links come from triangulation. The clique
size of the resulting clique tree (not shown) is four.

•

•

•

•

Ideally, we would like to ﬁnd a triangulated graph which has minimal clique size. However, it can be
shown to be a computationally hard problem to ﬁnd the most eﬃcient triangulation. In practice, most
general purpose triangulation algorithms are chosen to provide reasonable, but clearly not always
optimal, generic performance.

Numerical over/under ﬂow issues can occur under repeated multiplication of potentials. If we only
care about marginals we can avoid numerical diﬃculties by normalising potentials at each step; these
missing normalisation constants can always be found under the normalisation constraint. If required
one can always store the values of these local renormalisations, should, for example, the global nor-
malisation constant of a distribution be required, see section(6.6.2).

X

After clamping variables in evidential states, running the JTA returns the joint distribution on the
c in a clique with all the evidential variables clamped in their evidential
non-evidential variables
states, p(

c, evidence). From this conditionals are straightforward to calculate.

X

which are not contained within a single
Representing the marginal distribution of a set of variables
) may be
clique is in general computationally diﬃcult. Whilst the probability of any state of p(
computed eﬃciently, there are in general an exponential number of such states. A classical example in
this regard is the HMM, section(23.2) which has a singly connected joint distribution p(
). However
the marginal distribution p(
) is fully connected. This means that for example whilst the entropy of
p(

) is straightforward to compute, the entropy of the marginal p(

) is intractable.

H

X

X

V

V

,

,

V

H

V

6.6.2 Computing the normalisation constant of a distribution

For a Markov network

) =

p(

X

1
Z

(cid:89)

i

φ(

i)

X

(6.6.1)

how can we ﬁnd Z eﬃciently? If we used the JTA on the unnormalised distribution (cid:81)
have the equivalent representation:

i φ(

i), we would

X

) =

p(

X

1
Z

114

(cid:81)
(cid:81)

c φ(
s φ(

c)
s)

X
X

(6.6.2)

DRAFT April 24, 2015

The Junction Tree Algorithm

where s and c are the separator and clique indices. Since the distribution must normalise, we can obtain Z
from

Z =

(cid:88)

X

(cid:81)
(cid:81)

c φ(
s φ(

c)
s)

X
X

(6.6.3)

For a consistent JT, summing ﬁrst over the variables of a simplicial JT clique (not including the separator
variables), the marginal clique will cancel with the corresponding separator to give a unity term so that the
clique and separator can be removed. This forms a new JT for which we then eliminate another simplicial
clique. Continuing in this manner we will be left with a single numerator potential so that

Z =

(cid:88)

Xc

φ(

c)

X

(6.6.4)

This is true for any clique c, so it makes sense to choose one with a small number of states so that the
resulting raw summation is eﬃcient. Hence in order to compute the normalisation constant of a distribution
one runs the JT algorithm on an unnormalised distribution and the global normalisation is then given by
the local normalisation of any clique. Note that if the graph is disconnected (there are isolated cliques), the
normalisation is the product of the connected component normalisation constants.

6.6.3 The marginal likelihood

Our interest here is the computation of p(
. Naively,
one could carry out this computation by summing over all the non-evidential variables (hidden variables
) explicitly. In cases where this is computationally impractical an alternative is to use

is a subset of the full variable set

) where

V ⊂ X

=

X

V

H

X \V

p(

H|V

) =

p(

,

V
p(

H
)
V

)

(6.6.5)

One can view this as a product of clique potentials divided by the normalisation p(
method of section(6.6.2) may be directly applied. See demoJTree.m.

V

), for which the general

6.6.4 Some small JTA examples

Example 6.5 (A simple example of the JTA).
Consider running the JTA on the simple graph

p(a, b, c) = p(a

c)p(c)
b)p(b
|
|

(6.6.6)

The moralisation and triangulation steps are trivial, and the JTA is given
immediately by the ﬁgure on the right. A valid assignment is

a

ab

b

b

c

bc

φ (a, b) = p(a

c)p(c)
b), φ (b) = 1, φ (b, c) = p(b
|
|

To ﬁnd a marginal p(b) we ﬁrst run the JTA:

(6.6.7)

•

•

•

Absorbing from ab through b, the new separator is φ∗ (b) = (cid:80)
The new potential on (b, c) is given by

a φ (a, b) = (cid:80)

a p(a

φ∗ (b, c) =

φ (b, c)φ∗ (b)
φ (b)

=

c)p(c)
p(b
|
1

1

×

Absorbing from bc through b, the new separator is

φ∗∗ (b) =

(cid:88)

c

φ∗ (b, c) =

(cid:88)

c

c)p(c)
p(b
|

DRAFT April 24, 2015

b) = 1.
|

(6.6.8)

(6.6.9)

115

The Junction Tree Algorithm

•

The new potential on (a, b) is given by

φ∗ (a, b) =

φ (a, b)φ∗∗ (b)
φ∗ (b)

p(a
|

=

b) (cid:80)

c p(b
|
1

c)p(c)

This is therefore indeed equal to the marginal since (cid:80)

c p(a, b, c) = p(a, b).

The new separator φ∗∗ (b) contains the marginal p(b) since

φ∗∗ (b) =

(cid:88)

c

p(b
|

c)p(c) =

(cid:88)

c

p(b, c) = p(b)

(6.6.10)

(6.6.11)

Example 6.6 (Finding a conditional marginal). Continuing with the distribution in example(6.5), we
a = 1, c = 1). First we clamp the evidential variables in their states. Then we
consider how to compute p(b
|
claim that the eﬀect of running the JTA is to produce on a set of clique variables
the marginals on the
cliques p(

). We demonstrate this below:

X

,

X

V

•

•

•

•

In general, the new separator is given by φ∗ (b) = (cid:80)
b) = 1. However, since
a is clamped in state a = 1, then the summation is not carried out over a, and we have instead
φ∗ (b) = p(a = 1
b).
|
The new potential on the (b, c) clique is given by

a φ (a, b) = (cid:80)

a p(a
|

φ∗ (b, c) =

φ (b, c)φ∗ (b)
φ (b)

=

c = 1)p(c = 1)p(a = 1
p(b
|
|
1

b)

The new separator is normally given by

φ∗∗ (b) =

φ∗ (b, c)

(cid:88)

c

However, since c is clamped in state 1, we have instead

φ∗∗ (b) = p(b
b)
c = 1)p(c = 1)p(a = 1
|
|

The new potential on (a, b) is given by

(6.6.12)

(6.6.13)

@@

(6.6.14)

φ∗ (a, b) =

φ (a, b)φ∗∗ (b)
φ∗ (b)

b)p(b
p(a = 1
|
|

=

c = 1)p(c = 1)p(a = 1
b)
|
p(a = 1
|

b)

which is the joint distribution p(a = 1, b, c = 1).

c = 1)p(c = 1)
b)p(b
= p(a = 1
|
|

(6.6.15)

@@

The eﬀect of clamping a set of variables
i which contains the set of non-evidential variables
marginal p(

i,

V

H

H

V

in their evidential states and running the JTA is that, for a clique
i, the consistent potential from the JTA contains the

). Finding a conditional marginal is then straightforward by ensuring normalisation.

Example 6.7 (ﬁnding the likelihood p(a = 1, c = 1)). One may also use the JTA to compute the marginal
likelihood for variables not in the same clique since the eﬀect of clamping the variables in their evidential
states and running the JTA produces the joint marginals, such as φ∗ (a, b) = p(a = 1, b, c = 1). Then
calculating the likelihood is easy since we just sum out over the non-evidential variables of any converged
potential : p(a = 1, c = 1) = (cid:80)

b φ∗ (a, b) = (cid:80)

b p(a = 1, b, c = 1).

116

DRAFT April 24, 2015

Finding the Most Likely State

6.6.5 Shafer-Shenoy propagation

Consider the Markov network in ﬁg(6.12a) for which a junction tree is given in ﬁg(6.12b). We use the
obvious notation shortcut of writing the variable indices alone. In the absorption procedure, we essentially
store the result of message passing in the potentials and separators. An alternative message passing scheme
for the junction tree can be derived as follows: Consider computing the marginal on the variables 2, 3, 4,
which involves summing over variables 1, 5, 6:

p(2, 3, 4) =

(cid:88)

φ(1, 2, 5)φ(1, 3, 6)φ(1, 2, 3)φ(2, 3, 4)

1,5,6
(cid:88)

(cid:88)

=

φ(1, 2, 5)

(cid:88)

φ(1, 3, 6)

φ(1, 2, 3)

φ(2, 3, 4)

(6.6.16)

(6.6.17)

1

5

(cid:124)

(cid:123)(cid:122)
λ125→123

(cid:124)

6

(cid:125)

(cid:124)

(cid:123)(cid:122)
λ136→123
(cid:123)(cid:122)
λ123→234

(cid:125)

(cid:125)

In general, for a clique i with potential φ(
have received messages from the other neighbours of i, we can send a message

i), and neighbouring clique j with potential φ(

V

V

j), provided we

λi→j =

(cid:88)

Vi\Vj

φ(

i)

V

(cid:89)

k(cid:54)=j

λk→i

(6.6.18)

Once a full round of message passing has been completed, the marginal of any clique is given by the product
of incoming messages.

This message passing scheme is called Shafer-Shenoy propagation and has the property that no division of
potentials is required, unlike absorption. On the other hand, to compute a message we need to take the
product of all incoming messages; in absorption this is not required since the eﬀect of the message passing
is stored in the clique potentials. The separators are not required in the Shafer-Shenoy approach and we
use them here only to indicate which variables the messages depend on. Both absorption and Shafer-Shenoy
propagation are valid message passing schemes on the junction tree and the relative eﬃcacy of the approaches
depends on the topology of the junction tree[187].

6.7 Finding the Most Likely State

It is often of interest to compute the most likely joint state of a distribution:

argmax
x1,...,xn

p(x1, . . . , xn)

(6.7.1)

4

5

1

6

2

3

(a)

125

136

12

13

123

23

234

125

136

12

13

123

23

234

(b)

(c)

(a): Markov network.

(b): Junction tree. Under absorption, once we have absorbed from
Figure 6.12:
125 to 123 and 136 to 123, the result of these absorptions is stored in the new potential on 123. After this
we can absorb from 123 to 234 by operating on the 123 potential and then sending this information to 234.
(c): In Shafer-Shenoy updating, we send a message from a clique to a neighbouring clique based on the
product of all incoming messages.

DRAFT April 24, 2015

117

Reabsorption : Converting a Junction Tree to a Directed Network

Since the development of the JTA is based around a variable elimination procedure and the max operator
distributes over the distribution as well, eliminating a variable by maximising over that variable will have
the same eﬀect on the graph structure as summation did. This means that a junction tree is again an
appropriate structure on which to perform max operations. Once a JT has been constructed, one then uses
the Max Absorption procedure (see below), to perform maximisation over the variables. After a full round
of absorption has been carried out, the cliques contain the distribution on the variables of the clique with
all remaining variables set to their optimal states. The optimal local states can then be found by explicit
optimisation of each clique potential separately.

Note that this procedure holds also for non-distributions – in this sense this is an example of a more general
dynamic programming procedure applied in a case where the underlying graph is multiply connected. This
demonstrates how to eﬃciently compute the optimum of a multiply connected function deﬁned as the product
on potentials.

Deﬁnition 6.10 (Max Absorption).

)

φ (

V

φ∗ (

S

)

φ∗ (

)

W

V

and

Let
separator, and let φ (
Absorption replaces the tables φ (

), φ (

W

W

V

be neighbours in a clique graph, let

be their
S
) be their potentials.

) and φ (

S

) and φ (

S

) with

W

φ∗ (

) = max
V\S

φ (

)

V

S

φ∗ (

W

) = φ (

W

)

φ∗ (
φ (

)
S
)
S

Once messages have been passed in both directions over all separators, according to a valid schedule, the
most-likely joint state can be read oﬀ from maximising the state of the clique potentials. This is implemented
in absorb.m and absorption.m where a ﬂag is used to switch between either sum or max absorption.

6.8 Reabsorption : Converting a Junction Tree to a Directed Network

It is sometimes useful to be able to convert
a consistent JT (in which a full round of message passing has
occurred) back to a BN of a desired form. For example, if one wishes to draw samples from a Markov
network, this can be achieved by ancestral sampling on an equivalent directed structure, see section(27.2.2).

@@

Deﬁnition 6.11 (Reabsorption).

V

S

W

⇒

V

W\S

V

and

be neighbouring cliques in a directed

Let
most one parent. Furthermore, let
Reabsorption into

consistent JT in which each clique in the tree has at
) be the potentials.
) and φ (
be their separator, and φ (
S
removes the separator and forms a (set) conditional distribution

), φ (

W

W

V

S

@@

W
φ (
φ (

p(

W\S|V

) =

We say that clique

W

)
W
)
S
reabsorbs the separator

.

S

(6.8.1)

Revisiting the example from ﬁg(6.4), we have the JT given in ﬁg(6.13a). To ﬁnd a valid directed represen-
tation we ﬁrst orient the JT edges consistently away from a chosen root node (see singleparenttree.m),
thereby forming a directed JT which has the property that each clique has at most one parent clique.
Consider ﬁg(6.13a) which represents

p(a, b, c, d, e, f, g, h) =

p(e, g)p(d, c, e)p(a, b, c)p(c, f )p(e, h)
p(e)p(c)p(c)p(e)

118

(6.8.2)

DRAFT April 24, 2015

The Need For Approximations

dce

c

e

(a)

e

eg

abc

c

cf

dce

e

eh

eg

abc

c

c

e

cf

a b c

e d

f

eh

(b)

g

h

(c)

(a): Junction tree.

Figure 6.13:
away from the clique (abc).
into its child clique.

(b): Directed junction tree in which all edges are consistently oriented
(c): A set chain formed from the junction tree by reabsorbing each separator

We now have many choices as to which clique re-absorbs a separator. One such choice would give

@@

p(a, b, c, d, e, f, g, h) = p(g

c)
e)p(d, e
|
|

p(a, b, c)p(f

e)
c)p(h
|
|

(6.8.3)

This can be represented using a so-called set chain[184] in ﬁg(6.13c) (set chains generalise Belief Networks
to a product of clusters of variables conditioned on parents). By writing each of the set conditional proba-
bilities as local conditional BNs, one may also form a BN. For example, one such would be given from the
decomposition

a, b)p(b
p(c
|
|

a)p(a)p(g

e)p(f
|

c)p(h
|

e, c)p(e
e)p(d
|
|
|

c)

(6.8.4)

6.9 The Need For Approximations

The JTA provides an upper bound on the complexity of (marginal/max) inference and attempts to exploit
the structure of the graph to reduce computations. However, in a great deal of interesting applications the
use of the JTA algorithm would result in clique-sizes in the triangulated graph that are prohibitively large.
A classical situation in which this can arise are disease-symptom networks. For example, for the graph in
ﬁg(6.14), the triangulated graph of the diseases is fully connected, meaning that no simpliﬁcation can occur
in general. This situation is common in such bipartite networks, even when the children only have a small
number of parents. Intuitively, as one eliminates each parent, links are added between other parents, me-
diated via the common children. Unless the graph is highly regular, analogous to a form of hidden Markov
model, this ﬁll-in eﬀect rapidly results in large cliques and intractable computations.

Dealing with large cliques in the triangulated graph is an active research topic and we’ll discuss strategies
for approximate inference in chapter(28).

6.9.1 Bounded width junction trees

In some applications we may be at liberty to choose the structure of the Markov network. For example,
if we wish to ﬁt a Markov network to data, we may wish to use as complex a Markov network as we
can computationally aﬀord.
In such cases we desire that the clique sizes of the resulting triangulated
Markov network are smaller than a speciﬁed ‘tree width’ (considering the corresponding junction tree as
a hypertree). This results in a ‘thin’ junction tree. A simple way to do this is to start with a graph and
include a randomly chosen edge provided that the size of all cliques in the resulting triangulated graph is
below a speciﬁed maximal width. See demoThinJT.m and makeThinJT.m which assumes an initial graph G
and a graph of candidate edges C, iteratively expanding G until a maximal tree width limit is reached. See
also [10] for a discussion on learning an appropriate Markov structure based on data.

DRAFT April 24, 2015

119

Code

Figure 6.14: 5 diseases giving rise to 3 symptoms. The triangulated graph
contains a 5 clique of all the diseases.

d1

d2

d3

d4

d5

s1

s2

s3

6.10 Summary

•

•

•

•

•

The junction tree is a structure on clusters of variables such that, under inference operations such as
marginalisation, the junction-tree structure remains invariant. This resolves the ﬁll-in issue when using
message-passing on a multiply connected graph.

The key stages are moralisation, triangulation, potential assignment, and message-passing.

There are diﬀerent propagation algorithms, including absorption and Shafer-Shenoy. These are both valid
message-passing algorithms on the junction tree and diﬀer in their eﬃciency depending on the branch-
structure of the junction tree.

The junction tree algorithm does not make a diﬃcult inference problem necessarily any easier. It is simply
a way to organise the computations required to correctly carry out message-passing. The computational
complexity is dominated by the clique-size and there is no guarantee that one can ﬁnd cliques with small
sizes in general.

The junction tree algorithm is clever, but not clairvoyant.
It provides only an upper bound on the com-
putational complexity of inference. It may be that there are problems which possess additional structure,
not immediately apparent, that can be exploited to reduce the computational complexity of inference much
below that suggested by the junction-tree approach.

6.11 Code

absorb.m: Absorption update
absorption.m: Full absorption schedule over tree
jtree.m: Form a junction tree
triangulate.m: Triangulation based on simple node elimination

V → S → W

6.11.1 Utility routines

Knowing if an undirected graph is a tree, and returning a valid elimination sequence is useful. A connected
graph is a tree if the number of edges plus 1 is equal to the number of nodes. However, for a possibly discon-
nected graph this is not the case. The code istree.m deals with the possibly disconnected case, returning a
valid elimination sequence if the graph is singly connected. The routine is based on the observation that any
singly connected graph must always possess a simplicial node which can be eliminated to reveal a smaller
singly connected graph.

istree.m: If graph is singly connected return 1 and elimination sequence
elimtri.m: Node elimination on a triangulated graph, with given end node
demoJTree.m: Chest clinic demo

120

DRAFT April 24, 2015

Exercises

6.12 Exercises

Exercise 6.1. Show that the Markov network 1
elimination labelling for this graph.

4

2

Exercise 6.2. Consider the following distribution:

3

is not perfect elimination ordered and give a perfect

p(x1, x2, x3, x4) = φ(x1, x2)φ(x2, x3)φ(x3, x4)

(6.12.1)

1. Draw a clique graph that represents this distribution and indicate the separators on the graph.

2. Write down an alternative formula for the distribution p(x1, x2, x3, x4) in terms of the marginal prob-

abilities p(x1, x2), p(x2, x3), p(x3, x4), p(x2), p(x3)

Exercise 6.3. Consider the distribution

p(x1, x2, x3, x4) = φ(x1, x2)φ(x2, x3)φ(x3, x4)φ(x4, x1)

(6.12.2)

1. Write down a junction tree for the above distribution.

2. Carry out the absorption procedure and demonstrate that this gives the correct result for the marginal

p(x1).

Exercise 6.4. Consider the distribution

p(a, b, c, d, e, f, g, h, i) = p(a)p(b
|

a)p(e
a)p(d
a)p(c
|
|

b)p(f
|

|

c)p(g

e, f )p(i
d)p(h
|
|
|

f, g)

(6.12.3)

1. Draw the belief network for this distribution.

2. Draw the moralised graph.

3. Draw the triangulated graph. Your triangulated graph should contain cliques of the smallest size pos-

sible.

4. Draw a junction tree for the above graph and verify that it satisﬁes the running intersection property.

5. Describe a suitable initialisation of clique potentials.

6. Describe the absorption procedure and write down an appropriate message updating schedule.

Exercise 6.5. This question concerns the distribution

b)p(d
a)p(c
p(a, b, c, d, e, f ) = p(a)p(b
|
|
|

c)p(e

d)p(f
|

a, e)
|

1. Draw the Belief Network for this distribution.

2. Draw the moralised graph.

(6.12.4)

3. Draw the triangulated graph. Your triangulated graph should contain cliques of the smallest size pos-

sible.

4. Draw a junction tree for the above graph and verify that it satisﬁes the running intersection property.

5. Describe a suitable initialisation of clique potentials.

6. Describe the Absorption procedure and an appropriate message updating schedule.

7. Show that the distribution can be expressed in the form

p(a

a, c)p(c
f )p(b
|
|
|

a, e)p(e
a, d)p(d
|

a, f )p(f )
|

DRAFT April 24, 2015

(6.12.5)

121

Exercises

Exercise 6.6.

For the undirected graph on the square lattice as shown, draw a triangulated graph with
the smallest clique sizes possible.

Exercise 6.7.
Consider a binary variable Markov Random Field p(x) = Z−1 (cid:81)
i>j φ(xi, xj), deﬁned
n lattice with φ(xi, xj) = eI[xi=xj ] for i a neighbour of j on the lattice and
on the n
i > j. A naive way to perform inference is to ﬁrst stack all the variables in the tth
column and call this cluster variable Xt, as shown. The resulting graph is then singly
connected. What is the complexity of computing the normalisation constant based on
this cluster representation? Compute log Z for n = 10.

×

X1

X2

X3

Exercise 6.8. Given a consistent junction tree on which a full round of message passing has occurred,
explain how to form a belief network from the junction tree.

Exercise 6.9. The ﬁle diseaseNet.mat contains the potentials for a disease bi-partite belief network, with
20 diseases d1, . . . , d20 and 40 symptoms, s1, . . . , s40. The disease variables are numbered from 1 to 20 and
the symptoms from 21 to 60. Each disease and symptom is a binary variable, and each symptom connects
to 3 parent diseases.

1. Using the BRMLtoolbox, construct a junction tree for this distribution and use it to compute all the

marginals of the symptoms, p(si = 1).

2. Explain how to compute the marginals p(si = 1) in a way more eﬃcient than using the junction tree
formalism. By implementing this method, compare it with the results from the junction tree algorithm.

3. Symptoms 1 to 5 are present (state 1), symptoms 6 to 10 not present (state 2), and the rest not known.

Compute the marginal p(di = 1
|

s1:10) for all diseases.

Exercise 6.10. Consider the distribution

p(y

x1, . . . , xT )p(x1)
|

T
(cid:89)

t=2

p(xt

xt−1)
|

where all variables are binary.

1. Draw a junction tree for this distribution and explain the computational complexity of computing p(xT ),

as suggested by the junction tree algorithm.

2. By using an approach diﬀerent from the plain JTA above, explain how p(xT ) can be computed in time

that scales linearly with T .

Exercise 6.11. Analogous to jtpot=absorption(jtpot,jtsep,infostruct), write a routine
[jtpot jtmess]=ShaferShenoy(jtpot,infostruct) that returns the clique marginals and messages for a
junction tree under Shafer-Shenoy updating. Modify demoJTree.m to additionally output your results for
marginals and conditional marginals alongside those obtained using absorption.

X

1 that connects to only one other clique through their separator

Exercise 6.12 (Clique elimination). Since a junction tree is indeed a tree, it must have an extremal (‘edge’)
1. More generally, we must be able
clique
to label the cliques in an elimination order, 1, 2, . . . , n such that by eliminating clique i we only have cliques
left with label greater than i. For a separator connected to two cliques j and k, we then number this separator
the lower of the two values, min (j, k). Using this elimination ordering of the cliques we can write the JT in
the form (using φ(
) to represent separator
potentials over separator variables

) to represent clique potentials over clique variables

and ψ(

X

X

S

S

)

++

S

φ1(
ψ1(

1)
1)

X
S

) =

p(

X

122

(cid:81)
(cid:81)

c≥2 φc(
s≥2 ψs(

c)
s)

X
S

(6.12.6)

DRAFT April 24, 2015

Exercises

1. Now eliminate clique 1 by summing over all variables in clique 1 that are not in the separator

Show that this gives a new JT on cliques c

2 and separators s

2 of the form

≥

≥

φ(cid:48)
d(

d)

X

(cid:81)

c≥2,c(cid:54)=d φc(
(cid:81)
s≥2 ψs(

X
s)

c)

S
where d is the clique neighbouring clique 1 and
d) (cid:80)
X
ψ1(

X1\S1 φ1(
1)

φ(cid:48)
d(

d) =

φd(

1)

X

X

S

1.

S

(6.12.7)

(6.12.8)

and relate this to the absorption procedure.

2. Show that by continuing in this manner, eliminating cliques one by one, the last clique must contain

the marginal p(

n).

X

3. Explain how by now reversing the elimination schedule, and eliminating cliques one by one, updating
j) will contain the

their potentials in a similar manner to Equation(6.12.8), the updated cliques φj(
marginals p(

X

j).

X

4. Hence explain why, after updating cliques according to the forward and reversed elimination schedules,

the cliques must be globally consistent.

DRAFT April 24, 2015

123

Exercises

124

DRAFT April 24, 2015

CHAPTER 7

Making Decisions

So far we’ve considered modelling and inference of distributions. In cases where we need to make decisions
under uncertainty, we need to additionally express how useful making the right decision is. In this chapter
we are particularly interested in the case when a sequence of decisions need to be taken. The corresponding
sequential decision theory problems can be solved using either a general decision tree approach or by ex-
ploiting structure in the problem based on extending the belief network framework and the corresponding
inference routines. The framework is related to problems in control theory and reinforcement learning.

7.1 Expected Utility

This chapter concerns situations in which decisions need to be taken under uncertainty. Consider the
following scenario: you are asked if you wish to take a bet on the outcome of tossing a fair coin. If you
bet and win, you gain £100.
If you bet and lose, you lose £200.
If you don’t bet, the cost to you is
win, lose
, a decision variable d with
zero. We can set this up using a two state variable x, with dom(x) =
}
and utilities as follows:
dom(d) =

{

bet, no bet
{

}
U (win, bet) = 100, U (lose, bet) =

200, U (win, no bet) = 0, U (lose, no bet) = 0

−

(7.1.1)

Since we don’t know the state of x, in order to make a decision about whether or not to bet, arguably the
best we can do is work out our expected winnings/losses under the situations of betting and not betting[257].
If we bet, we would expect to gain

U (bet) = p(win)

×

U (win, bet) + p(lose)

U (lose, bet) = 0.5

×

100

0.5

−

×

×

200 =

50

−

If we don’t bet, the expected gain is zero, U (no bet) = 0. Based on taking the decision which maximises
expected utility, we would therefore be advised not to bet.

Deﬁnition 7.1 (Subjective Expected Utility). The utility of a decision is

U (d) =

U (d, x)

(cid:104)

(cid:105)p(x)

(7.1.2)

where p(x) is the distribution of the outcome x and d represents the decision.

7.1.1 Utility of money

You are a wealthy individual, with £1, 000, 000 in your bank account. You are asked if you would like to
participate in a fair coin tossing bet in which, if you win, your bank account will become £1, 000, 000, 000.

125

Decision Trees

y e s

( 0 . 6 )

100

−

Rain

(0.4) no

yes

P arty

n

o

y e s

( 0 . 6 )

(0.4) no

Rain

500

0

50

Figure 7.1: A decision tree containing chance nodes (denoted
with ovals), decision nodes (denoted with rectangles) and utility
nodes (denoted with diamonds). Note that a decision tree is not
a graphical representation of a belief network with additional
nodes. Rather, a decision tree is an explicit enumeration of the
possible choices that can be made, beginning with the leftmost
decision node, with probabilities on the links out of chance nodes.

However, if you lose, your bank account will contain only £1000. Assuming the coin is fair, should you take
the bet? If we take the bet our expected bank balance would be

U (bet) = 0.5

×

1, 000, 000, 000 + 0.5

×

1000 = 500, 000, 500.00

(7.1.3)

If we don’t bet, our bank balance will remain at £1, 000, 000. Based on expected utility, we are therefore
advised to take the bet. (Note that if one considers instead the amount one will win or lose, one may show
that the diﬀerence in expected utility between betting and not betting is the same, exercise(7.7)).

Whilst the above makes mathematical sense, few people who are millionaires are likely to be willing to risk
losing almost everything in order to become a billionaire. This means that the subjective utility of money is
not simply the quantity of money. In order to better reﬂect the situation, the utility of money would need
to be a non-linear function of money, growing slowly for large quantities of money and decreasing rapidly
for small quantities of money, exercise(7.2).

7.2 Decision Trees

Decision trees (DTs) are a way to graphically organise a sequential decision process. A decision tree contains
decision nodes, each with branches for each of the alternative decisions. Chance nodes (random variables)
also appear in the tree, with the utility of each branch computed at the leaf of each branch. The expected
utility of any decision can then be computed on the basis of the weighted summation of all branches from
the decision to all leaves from that branch.

Example 7.1 (Party). Consider the decision problem as to whether or not to go ahead with a fund-raising
garden party. If we go ahead with the party and it subsequently rains, then we will lose money (since very
few people will show up); on the other hand, if we don’t go ahead with the party and it doesn’t rain we’re
free to go and do something else fun. To characterise this numerically, we use:

p(Rain = rain) = 0.6, p(Rain = no rain) = 0.4

(7.2.1)

The utility is deﬁned as

U (party, rain) =

100, U (party, no rain) = 500, U (no party, rain) = 0, U (no party, no rain) = 50 (7.2.2)

−

We represent this situation in ﬁg(7.1). The question is, should we go ahead with the party? Since we don’t
know what will actually happen to the weather, we compute the expected utility of each decision:

U (party) =

(cid:88)

Rain

U (party, Rain)p(Rain) =

100

−

×

0.6 + 500

×

0.4 = 140

U (no party) =

(cid:88)

Rain

U (no party, Rain)p(Rain) = 0

0.6 + 50

×

×

0.4 = 20

(7.2.3)

(7.2.4)

126

DRAFT April 24, 2015

Decision Trees

Based on expected utility, we are therefore advised to go ahead with the party. The maximal expected
utility is given by (see demoDecParty.m)

max
P arty

(cid:88)

Rain

p(Rain)U (P arty, Rain) = 140

(7.2.5)

Example 7.2 (Party-Friend). An extension of the Party problem is that if we decide not to go ahead with
the party, we have the opportunity to visit a friend. However, we’re not sure if this friend will be in. The
question is should we still go ahead with the party?

We need to quantify all the uncertainties and utilities. If we go ahead with the party, the utilities are as
before:

Uparty (party, rain) =

100, Uparty (party, no rain) = 500

−

with

p(Rain = rain) = 0.6, p(Rain = no rain) = 0.4

(7.2.6)

(7.2.7)

If we decide not to go ahead with the party, we will consider going to visit a friend. In making the decision
not to go ahead with the party we have utilities

Uparty (no party, rain) = 0, Uparty (no party, no rain) = 50

The probability that the friend is in depends on the weather according to

rain) = 0.8, p(F riend = in
p(F riend = in
|

no rain) = 0.1,
|

The other probabilities are determined by normalisation. We additionally have

Uvisit (friend in, visit) = 200, Uvisit (friend out, visit) =

100

−

(7.2.8)

(7.2.9)

(7.2.10)

with the remaining utilities zero. The two sets of utilities add up so that the overall utility of any decision
sequence is Uparty + Uvisit. The decision tree for the Party-Friend problem is shown is ﬁg(7.2). For each
decision sequence the utility of that sequence is given at the corresponding leaf of the DT. Note that the
leaves contain the total utility Uparty + Uvisit. Solving the DT corresponds to ﬁnding for each decision
node the maximal expected utility possible (by optimising over future decisions). At any point in the tree
choosing that action which leads to the child with highest expected utility will lead to the optimal strategy.
Using this, we ﬁnd that the optimal expected utility has value 140 and is given by going ahead with the
party, see demoDecPartyFriend.m.

Mathematically, we can express the optimal expected utility for the Party-Friend example by summing over
un-revealed variables and optimising over future decisions:

max
P arty

(cid:88)

Rain

p(Rain) max
V isit

(cid:88)

F riend

Rain) [Uparty(P arty, Rain) + Uvisit(V isit, F riend)I [P arty = no]]
p(F riend
|

(7.2.11)

where the term I [P arty = no] has the eﬀect of curtailing the DT if the party goes ahead. To answer the
question as to whether or not to go ahead with the party, we take that state of P arty that corresponds to
the maximal expected utility above. The way to read equation (7.2.11) is to start from the last decision
that needs to be taken, in this case V isit. When we are at the V isit stage we assume that we will have
previously made a decision about P arty and also will have observed whether or not it is raining. However,

DRAFT April 24, 2015

127

s
e
y

P arty

n

o

-100

-100

s
e
y

s
e
y
)

0.6

(

Rain

Rain(140)

(

0

.

4

)

n

o

500

s
e
y

n

o

500

200

(0.8)in

200

in

F riend

(

0
.
2

)

o

u

t

100

−

yes

V isit

n

o

0

(0.8)in

F riend

s
e
y
)

0.6

(

(

0
.
2

)

o

Rain

(

0

.

4

)

n

o

yes

V isit

u

t

0

250

(0.1)in

F riend

(

0
.
9

)

o

u

t

50

−

n

o

50

(0.1)in

F riend

(

0
.
9

)

o

u

t

50

P arty(140)

F riend(140)

o

u

t

100

−

yes

V isit(140)

n

o

s
e
y

Rain(104)

n

o

in

F riend(0)

o

u

t

0

0

250

in

n

o

F riend(

20)

−

o

u

t

50

−

yes

V isit(50)

n

o

50

in

F riend(50)

o

u

t

50

Decision Trees

−

×

×

100

0.6 + 500

(a):
Figure 7.2: Solving a Decision Tree.
Decision Tree for the Party-Friend problem,
(b): Solving the DT cor-
example(7.2).
responds to making the decision with the
highest expected future utility. This can be
achieved by starting at the leaves (utilities).
For a chance parent node x, the utility of
the parent is the expected utility of that
variable. For example, at the top of the DT
we have the Rain variable with the children
100 (probability 0.6) and 500 (probabil-
−
ity 0.4). Hence the expected utility of the
Rain node is
0.4 = 140.
For a decision node, the value of the node
is the optimum of its child values. One re-
curses thus backwards from the leaves to
the root. For example, the value of the
Rain chance node in the lower branch is
given by 140
0.4 = 104. The opti-
×
mal decision sequence is then given at each
decision node by ﬁnding which child node
has the maximal value. Hence the overall
best decision is to decide to go ahead with
the party. If we decided not to do so, and
it does not rain, then the best decision we
could take would be to not visit the friend
(which has an expected utility of 50). A
more compact description of this problem
is given by the inﬂuence diagram, ﬁg(7.4).
See also demoDecPartyFriend.m.

0.6+50

×

(a)

(b)

we don’t know whether or not our friend will be in, so we compute the expected utility by averaging over
this unknown. We then take the optimal decision by maximising over V isit. Subsequently we move to the
next-to-last decision, assuming that what we will do in the future is optimal. Since in the future we will
have taken a decision under the uncertain F riend variable, the current decision can then be taken under
uncertainty about Rain and maximising this expected optimal utility over P arty. Note that the sequence
of maximisations and summations matters – changing the order will in general result in a diﬀerent problem
with a diﬀerent expected utility1.

For the Party-Friend example the DT is asymmetric since if we decide to go ahead with the party we will not
visit the friend, curtailing the further decisions present in the lower half of the tree. Whilst the DT approach
is ﬂexible and can handle decision problems with arbitrary structure, a drawback is that the same nodes are
often repeated throughout the decision tree. For a longer sequence of decisions, the number of branches in
the tree can grow exponentially with the number of decisions, making this representation impractical.

1If one only had a sequence of summations, the order of the summations is irrelevant – likewise for the case of all maximi-

sations. However, summation and maximisation operators do not in general commute.

128

DRAFT April 24, 2015

Extending Bayesian Networks for Decisions

P arty

Rain

U tility

Figure 7.3: An inﬂuence diagram which contains random vari-
ables (denoted with ovals/circles) Decision nodes (denoted with
rectangles) and Utility nodes (denoted with diamonds). Con-
trasted with ﬁg(7.1) this is a more compact representation of the
structure of the problem. The diagram represents the expres-
sion p(rain)u(party, rain). In addition the diagram denotes an
ordering of the variables with party
rain (according to the
convention given by equation (7.3.1)).

≺

7.3 Extending Bayesian Networks for Decisions

An inﬂuence diagram is a Bayesian Network with additional Decision nodes and Utility nodes [149, 161, 175].
The decision nodes have no associated distribution and the utility nodes are deterministic functions of their
parents. The utility and decision nodes can be either continuous or discrete; for simplicity, in the examples
here the decisions will be discrete.

A beneﬁt of decision trees is that they are general and explicitly encode the utilities and probabilities
associated with each decision and event. In addition, we can readily solve small decision problems using
decision trees. However, when the sequence of decisions increases, the number of leaves in the decision tree
grows and representing the tree can become an exponentially complex problem. In such cases it can be
useful to use an Inﬂuence Diagram (ID). An ID states which information is required in order to make each
decision, and the order in which these decisions are to be made. The details of the probabilities and utilities
are not speciﬁed in the ID, and this can enable a more compact description of the decision problem.

7.3.1 Syntax of inﬂuence diagrams

Information Links An information link from a random variable into a decision node

X

D

d

indicates that the state of the variable X will be known before decision D is taken. Information links
from another decision node d in to D similarly indicate that decision d is known before decision D is
taken. We use a dashed link to denote that decision D is not functionally related to its parents.

Random Variables Random variables may depend on the states of parental random variables (as in belief

networks), but also decision node states:

D

X

Y

As decisions are taken, the states of some random variables will be revealed. To emphasise this we
typically shade a node to denote that its state will be revealed during the sequential decision process.

Utilities A utility node is a deterministic function of its parents. The parents can be either random

variables or decision nodes.

D

U

X

In the party example, the BN trivially consists of a single node, and the Inﬂuence Diagram is given in ﬁg(7.3).
The more complex Party-Friend problem is depicted in ﬁg(7.4). The ID generally provides a more compact
representation of the structure of problem than a DT, although details about the speciﬁc probabilities and
utilities are not present in the ID.

DRAFT April 24, 2015

129

Extending Bayesian Networks for Decisions

Figure 7.4: An inﬂuence diagram for the Party-Friend problem,
example(7.2). The partial ordering is P arty∗
≺
F riend. The dashed-link from party to visit is not strictly nec-
essary but retained in order to satisfy the convention that there
is a directed path connecting all decision nodes.

V isit∗

Rain

≺

≺

P arty

V isit

Rain

F riend

Uparty

Uvisit

Partial ordering

An ID deﬁnes a partial ordering of the nodes. We begin by writing those variables
known (evidential variables) before the ﬁrst decision D1. We then ﬁnd that set of variables
are revealed before the second decision D2. Subsequently the set of variables
Dt+1. The remaining fully-unobserved variables are placed at the end of the ordering:

0 whose states are
1 whose states
X
t is revealed before decision

X

X

D1

0
X

≺

1
≺ X

≺

D2, . . . ,

n−1

≺ X

≺

Dn

n
≺ X

(7.3.1)

X

with
k being the variables revealed between decision Dk and Dk+1. The term ‘partial’ refers to the fact
n. For notational clarity, at points
that there is no order implied amongst the variables within the set
below we will indicate decision variables with ∗ to reinforce that we maximise over these variables, and sum
over the non-starred variables. Where the sets are empty we omit writing them.
In ﬁg(7.5a) we consider
an oil exploration situation in which there is ﬁrst a decision to be taken as to whether or not to carry out
a seismic test, with an associated utility (cost) U1. The result of this test is represented by the variable
Seismic, and depends on whether there is oil present. Based on this seismic result, a subsequent decision
is to be taken as to whether or not to drill for oil, which has an associated utility U2. The ordering is
T est∗

Seismic

Drill∗

Oil.

X

≺

≺

≺

@@

The optimal ﬁrst decision D1 is determined by computing

U (D1

0)

|X

≡

(cid:88)

X1

max
D2

. . .

(cid:88)

Xn−1

max
Dn

(cid:88)

(cid:89)

Xn

i∈I

p (xi

pa (xi))
|

(cid:88)

j∈J

Uj (pa (uj))

(7.3.2)

for each state of the decision D1, given
random variables, and
optimal decision D1 is found using

J

denotes the set of indices for the
the indices for the utility nodes. For each state of the conditioning variables, the

0. In equation (7.3.2) above

X

I

argmax
D1

U (D1

0)

|X

(7.3.3)

Remark 7.1 (Reading oﬀ the partial ordering). Sometimes it can be tricky to read the partial ordering from
0 that need to be observed to
the ID. A method is to identify the ﬁrst decision D1 and then any variables
1 that are revealed after decision
make that decision. Then identify the next decision D2 and the variables
D1 is taken and before decision D2 is taken, etc. This gives the partial ordering
D2, . . ..
Place any unrevealed variables at the end of the ordering.

1
≺ X

0
X

D1

≺

≺

X

X

Implicit and explicit information links

The information links are a potential source of confusion. An information link speciﬁes explicitly which
quantities are known before that decision is taken2. We also implicitly assume the no forgetting assumption
that all past decisions and revealed variables are available at the current decision (the revealed variables

2Some authors prefer to write all information links where possible, and others prefer to leave them implicit. Here we largely
take the implicit approach. For the purposes of computation, all that is required is a partial ordering; one can therefore view
this as ‘basic’ and the information links as superﬁcial (see [72]).

130

DRAFT April 24, 2015

Extending Bayesian Networks for Decisions

T est

Oil

U2

T est

Oil

U2

U1

Seismic

Drill

U1

Seismic

Drill

(a)

(b)

(a): The partial ordering is T est∗

Oil. The explicit information links
Figure 7.5:
from T est to Seismic and from Seismic to Drill are both fundamental in the sense that removing either
results in a diﬀerent partial ordering. The shaded node emphasises that the state of this variable will be
revealed during the sequential decision process. Conversely, the non-shaded node will never be observed.
(b): Based on the ID in (a), there is an implicit link from T est to Drill since the decision about T est is
taken before Seismic is revealed.

Seismic

Drill∗

≺

≺

≺

@@

are necessarily the parents of all
future decision nodes). If we were to include all such information links,
IDs would get potentially rather messy. In ﬁg(7.5), both explicit and implicit information links are demon-
strated. We call an information link fundamental if its removal would alter the partial ordering.

Causal consistency

@@ For an Inﬂuence Diagram to be consistent a current decision cannot aﬀect the past. This means that any

random variable descendants of a decision D in the ID must come later in the partial ordering.

Asymmetry

IDs are most convenient when the corresponding DT is symmetric. However, some forms of asymmetry
are relatively straightforward to deal with in the ID framework. For our Party-Friend example, the DT is
asymmetric. However, this is easily dealt with in the ID by using a link from P arty to Uvisit which removes
the contribution from Uvisit when the party goes ahead.

More complex issues arise when the set of variables that can be observed depends on the decision sequence
taken. In this case the DT is asymmetric. In general, Inﬂuence Diagrams are not well suited to modelling
such asymmetries, although some eﬀects can be mediated either by careful use of additional variables, or
extending the ID notation. See [72] and [161] for further details of these issues and possible resolutions.

Example 7.3 (Should I do a PhD?). Consider a decision whether or not to do PhD as part of our education
(E). Taking a PhD incurs costs, UC both in terms of fees, but also in terms of lost income. However, if
we have a PhD, we are more likely to win a Nobel Prize (P ), which would certainly be likely to boost our
Income (I), subsequently beneﬁtting our ﬁnances (UB). This setup is depicted in ﬁg(7.6a). The ordering is
(excluding empty sets)

E∗

I, P

≺ {

}

and

(7.3.4)

dom(E) = (do PhD, no PhD) , dom(I) = (low, average, high) , dom(P ) = (prize, no prize)

(7.3.5)

The probabilities are

p(win Nobel prize
|

no PhD) = 0.0000001

do PhD) = 0.001
p(win Nobel prize
|

p(low
p(low
p(low
p(low

do PhD, no prize) = 0.1 p(average
|
|
no PhD, no prize) = 0.2 p(average
|
|
p(average
do PhD, prize) = 0.01
|
|
p(average
no PhD, prize) = 0.01
|
|

do PhD, no prize) = 0.5 p(high
do PhD, no prize) = 0.4
|
no PhD, no prize) = 0.2
no PhD, no prize) = 0.6 p(high
|
do PhD, prize) = 0.95
p(high
do PhD, prize) = 0.04
|
no PhD, prize) = 0.95
p(high
no PhD, prize) = 0.04
|

DRAFT April 24, 2015

(7.3.6)

(7.3.7)

131

Extending Bayesian Networks for Decisions

Figure 7.6: (a): Education E incurs some
cost, but also gives a chance to win a pres-
tigious science prize. Both of these aﬀect
our likely Incomes, with corresponding long
term ﬁnancial beneﬁts. (b): The start-up
scenario.

S

US

P

I

UB

E

UC

(a)

E

UC

P

I

UB

(b)

The utilities are

UC (do PhD) =
UB (low) = 100000, UB (average) = 200000, UB (high) = 500000

50000, UC (no PhD) = 0,

−

The expected utility of Education is

U (E) =

(cid:88)

I,P

p(I

E, P )p(P
|

E) [UC(E) + UB(I)]
|

(7.3.8)

(7.3.9)

(7.3.10)

so that U (do phd) = 260174.000, whilst not taking a PhD is U (no phd) = 240000.0244, making it on average
beneﬁcial to do a PhD. See demoDecPhD.m.

Example 7.4 (PhDs and Start-up companies). Inﬂuence Diagrams are particularly useful when a sequence
of decisions is taken. For example, in ﬁg(7.6b) we model a new situation
(now dropping the link from
E to I simply to reduce the table size below) in which someone has ﬁrst decided whether or not to take
a PhD. Ten years later in their career they decide whether or not to make a start-up company. This
decision is based on whether or not they won the Nobel Prize. The start-up decision is modelled by S with
dom(S) = (tr, fa). If we make a start-up, this will cost some money in terms of investment. However, the
potential beneﬁt in terms of our income could be high.

++

We model this with (the other required table entries being taken from example(7.3)):

p(low
p(low
p(low
p(low

p(high
start up, no prize) = 0.5
p(average
start up, no prize) = 0.1
start up, no prize) = 0.4
|
|
|
no start up, no prize) = 0.2
no start up, no prize) = 0.6 p(high
no start up, no prize) = 0.2 p(average
|
|
|
start up, prize) = 0.99
p(high
p(average
start up, prize) = 0.005
start up, prize) = 0.005
|
|
|
no start up, prize) = 0.8
p(high
no start up, prize) = 0.15
p(average
no start up, prize) = 0.05
|
|
|

(7.3.11)

and

US (start up) =

200000,

−

US (no start up) = 0

(7.3.12)

Our interest is to advise whether or not it is desirable (in terms of expected utility) to take a PhD, now
bearing in mind that later one may or may not win the Nobel Prize, and may or may not make a start-up
company.

The ordering is (eliding empty sets)

E∗

P

≺

≺

S∗

I

≺

132

(7.3.13)

DRAFT April 24, 2015

Solving Inﬂuence Diagrams

The expected optimal utility for any state of E is

U (E) =

(cid:88)

P

max
S

(cid:88)

I

p(I

|

S, P )p(P

E) [US(S) + UC(E) + UB(I)]
|

where we assume that the optimal decisions are taken in the future. Computing the above, we ﬁnd

U (do PhD) = 190195.00,

U (no PhD) = 240000.02

Hence, we are better oﬀ not doing a PhD. See demoDecPhd.m.

(7.3.14)

(7.3.15)

7.4 Solving Inﬂuence Diagrams

Solving an inﬂuence diagram means computing the optimal decision or sequence of decisions. The direct
variable elimination approach is to take equation (7.3.2) and perform the required sequence of summations
and maximisations explicitly. Due to the causal consistency requirement the future cannot inﬂuence the
past. To help matters with notation, we order the variables and decisions such that we may write the belief
network of the inﬂuence diagram as

p(x1:T , d1:T ) =

T
(cid:89)

t=1

p(xt

x1:t−1, d1:t)
|

@@

For a general utility u(x1:T , d1:T ), solving the ID

then corresponds to carrying out the operations

max
d1

(cid:88)

x1

. . . max

dT

(cid:88)

T
(cid:89)

xT

t=1

p(xt

x1:t−1, d1:t)u(x1:T , d1:T )
|

(7.4.1)

(7.4.2)

Let’s look at eliminating ﬁrst xT and then dT . Our aim is to write a new ID on the reduced variables
x1:T −1, d1:T −1. Since xT and dT only appear in the ﬁnal factor of the belief network, we can write

max
d1

(cid:88)

x1

. . . max
dT −1

(cid:88)

T −1
(cid:89)

xT −1

t=1

p(xt

x1:t−1, d1:t) max
|

dT

(cid:88)

xT

p(xT

x1:T −1, d1:T )u(x1:T , d1:T )
|

(7.4.3)

which is then a new ID

max
d1

(cid:88)

x1

. . . max
dT −1

(cid:88)

T −1
(cid:89)

xT −1

t=1

p(xt

x1:t−1, d1:t)˜u(x1:T −1, d1:T −1)
|

with modiﬁed potential

˜u(x1:T −1, d1:T −1)

max
dT

≡

(cid:88)

xT

p(xT

|

x1:T −1, d1:T )u(x1:T , d1:T )

(7.4.4)

(7.4.5)

This however doesn’t exploit the fact that the utilities will typically have structure. Without loss of gen-
erality, we may also write the utility as one that is independent of xT , dT and one that depends on xT , dT :

u(x1:T , d1:T ) = ua(x1:T −1, d1:T −1) + ub(x1:T , d1:T )

(7.4.6)

Then eliminating xT , dT updates the utility to

˜u(x1:T −1, d1:T −1) = ua(x1:T −1, d1:T −1) + max

dT

(cid:88)

xT

p(xT

|

x1:T −1, d1:T )ub(x1:T , d1:T )

(7.4.7)

DRAFT April 24, 2015

133

7.4.1 Messages on an ID

For an ID with two sets of variables
network as

1,

X

2 and associated decision sets

X

1 and

D

2, we can write the belief

D

Solving Inﬂuence Diagrams

where

p(

X |D

) = p(

2
X

1,

|X

1

D
u(

1
≺ X
,

2
≺ D
) = u(

X

D

≺ X
1,

D

X

1,

2)p(

1

1)

X

D

D
2, and corresponding utilities

|D

) + u(

1,

2,

X

)

D

X

The optimal utility is given by

uopt = max
D1

(cid:88)

X1

max
D2

(cid:88)

X2

p(

X |D

)u(

X

,

)

D

After eliminating



2 and

X

2, we obtain the ID

D

p(

1
X

|D

1)

u(

1,

X

D

) +

max
D2

(cid:88)

X2

p(

2
X

1,

|X

1,

D

2)u(

X

D

(7.4.8)

(7.4.9)

(7.4.10)

(7.4.11)

@@



1,

2,

X

D

)


which we can express in terms of the original distribution p(





∗
(cid:88)

(X ,D)2





p(

X |D

)



u(

X

) +

1,

D

1

∗
(cid:88)

(cid:80)∗

(X ,D)2

p(

)

X |D

(X ,D)2

) as

X |D



p(

X |D

)u(

X

1,

2,

X

D

)


(7.4.12)

Y refers to summing ﬁrst over the chance variables in

where (cid:80)∗
and then maximising over the decision
variables in
. These updates then deﬁne an ID on a reduced set of variables and can be viewed as messages.
The potential usefulness of equation (7.4.12) is that it may be applied to IDs that are causally consistent
(future decisions cannot aﬀect the past) but which are not expressed directly in a causal form.

Y

Y

7.4.2 Using a junction tree

In complex IDs computational eﬃciency in carrying out the series of summations and maximisations may
be an issue and one therefore seeks to exploit structure in the ID. It is intuitive that some form of junction
tree style algorithm is applicable. The treatment here is inspired by [159]; a related approach which deals
with more general chain graphs is given in [72]. We can ﬁrst represent an ID using decision potentials which
consist of two parts, as deﬁned below.

Deﬁnition 7.2 (Decision Potential). A decision potential on a clique C contains two potentials: a probability
potential ρC and a utility potential µC. The joint potentials for the junction tree are deﬁned as

ρ =

(cid:89)

C∈C

ρC,

µ =

(cid:88)

C∈C

µC

with the junction tree representing the term ρµ.

(7.4.13)

(inverse of the) partial ordering which
In this case there are constraints on the triangulation, imposed by the
restricts the variables elimination sequence. This results in a so-called strong Junction Tree. The sequence
of steps required to construct a JT for an ID is given by the following procedure:

@@

Procedure 7.1 (Making a strong junction tree).

Remove Information Edges Parental links of decision nodes are removed.

Moralization Marry all parents of the remaining nodes.

134

DRAFT April 24, 2015

Solving Inﬂuence Diagrams

Remove Utility Nodes Remove the utility nodes and their parental links.

Strong Triangulation Form a triangulation based on an elimination order which obeys the partial ordering

of the variables.

Strong Junction Tree From the strongly triangulated graph, form a junction tree and orient the edges

towards the strong root (the clique that appears last in the elimination sequence).

The cliques are then ordered according to the sequence in which they are eliminated. The separator prob-
ability cliques are initialised to the identity, with the separator utilities initialised to zero. The probability
cliques are then initialised by placing conditional probability factors into the lowest available clique (that is
the probability factors are placed in the cliques closest to the leaves of the tree, furthest from the root) that
can contain them, and similarly for the utilities. Remaining probability cliques are set to the identity and
utility cliques to zero.

Example 7.5 (Junction Tree). An example of a junction tree for an ID is given in ﬁg(7.7a). The
moralisation and triangulation links are given in ﬁg(7.7b). The orientation of the edges follows the
partial ordering with the leaf cliques being the ﬁrst to disappear under the sequence of summations and
maximisations.

A by-product of the above steps is that the cliques describe the fundamental dependencies on previous
decisions and observations. In ﬁg(7.7a), for example, the information link from f to D2 is not present in the
moralised-triangulated graph ﬁg(7.7b), nor in the associated cliques of ﬁg(7.7c). This is because once e is
revealed, the utility U4 is independent of f , giving rise to the two-branch structure in ﬁg(7.7b). Nevertheless,
the information link from f to D2 is fundamental since it speciﬁes that f will be revealed – removing this
link would therefore change the partial ordering.

Absorption

By analogy with the deﬁnition of messages in section(7.4.1), for two neighbouring cliques C1 and C2, where
C1 is closer to the strong root of the JT (the last clique deﬁned through the elimination order), we deﬁne

ρS =

∗
(cid:88)

C2\S

ρC2,

µS =

∗
(cid:88)

C2\S

ρC2µC2

ρnew
C1 = ρC1ρS,

µnew
C1 = µC1 +

µS
ρS

(7.4.14)

(7.4.15)

In the above (cid:80)∗
C is a ‘generalised marginalisation’ operation – it sums over those elements of clique C which
are random variables and maximises over the decision variables in the clique. The order of this sequence of
sums and maximisations follows the partial ordering deﬁned by

.
≺

Absorption is then carried out from the leaves inwards to the root of the strong JT. The optimal setting
of a decision D1 can then be computed from the root clique. Subsequently backtracking may be applied to
infer the optimal decision trajectory. The optimal decision for D can be obtained by working with the clique
containing D which is closest to the strong root and setting any previously taken decisions and revealed
observations into their evidential states. See demoDecAsia.m for an example.

DRAFT April 24, 2015

135

l

U4

U3

a

b

D1

c

d

D4

i

h

D3

j

k

U2

g

D2

(a)

a

b

D1

U1

c

d

e

f

b, c, a

b, c

Solving Inﬂuence Diagrams

D4

i

h

D3

l

j

k

g

D2

e

f

(b)

b, e, d, c

e, D2, g

D2, g

D2, g, D4, i

D4, i

D4, i, l

b, e, d

b, D1, e, f, d

e

f

f, D3, h

D3, h

D3, h, k

h, k

h, k, j

(c)

(a): Inﬂuence Diagram, adapted from [159]. Causal consistency is satisﬁed since there is a

Figure 7.7:
directed path linking all decisions in sequence. The partial ordering is b
g
green, strong triangulation links are in red.
from the leaves of the tree towards the root.

≺
(a, c, d, h, i, j, k, l). (b): Moralised and strongly triangulated graph. Moralisation links are in
(c): Strong Junction Tree. Absorption passes information

(e, f )

D4

D1

D2

D3

(
·

≺

≺

≺

≺

≺

≺

≺

)

Example 7.6 (Absorption on a chain). For the ID of ﬁg(7.8), the moralisation and triangulation steps are
trivial and give the JT:

3: x1, x2, d1

x2

2: x2, x3, d2

x3

1: x3, x4, d3

where the cliques are indexed according the elimination order. The probability and utility cliques are
initialised to

ρ3 (x1, x2, d1) = p(x2
ρ2 (x2, x3, d2) = p(x3
ρ1 (x3, x4, d3) = p(x4

x1, d1) µ3 (x1, x2, d1) = 0
|
x2, d2) µ2 (x2, x3, d2) = u(x2)
|
x3, d3) µ1 (x3, x4, d3) = u(x3) + u(x4)
|

with the separator cliques initialised to

ρ1−2 (x3) = 1 µ1−2 (x3) = 0
ρ2−3 (x2) = 1 µ2−3 (x2) = 0

Updating the separator we have the new probability potential

ρ1−2 (x3)∗ = max
d3

(cid:88)

x4

ρ1 (x3, x4, d3) = 1

(7.4.16)

(7.4.17)

(7.4.18)

136

DRAFT April 24, 2015

Markov Decision Processes

and utility potential

µ1−2 (x3)∗ = max
d3

(cid:88)

x4
(cid:32)

ρ1 (x3, x4, d3) µ1 (x3, x4, d3) = max

d3

= max

d3

u(x3) +

(cid:33)

(cid:88)

x4

p(x4

x3, d3)u(x4)
|

At the next step we update the probability potential

@@

ρ2 (x2, x3, d2)∗ = ρ2 (x2, x3, d2) ρ1−2 (x3)∗ =

p(x3

x2, d2)
|

and utility potential

(cid:88)

x4

p(x4

x3, d3) (u(x3) + u(x4))
|

(7.4.19)

(7.4.20)

(7.4.21)

µ2 (x2, x3, d2)∗ = µ2 (x2, x3, d2) +

µ1−2 (x3)∗
ρ1−2 (x3)

(cid:32)

= u(x2) + max

d3

u(x3) +

(cid:88)

x4

(cid:33)

p(x4

x3, d3)u(x4)
|

(7.4.22)

The next separator decision potential is

ρ2−3 (x2)∗ = max
d2

µ2−3 (x2)∗ = max
d2

= max

d2

(cid:88)

x3

(cid:88)

x3

(cid:88)

x3

ρ2 (x2, x3, d2)∗ = 1

ρ2 (x2, x3, d2) µ2 (x2, x3, d2)∗

(cid:32)

(cid:32)

p(x3

|

x2, d2)

u(x2) + max

d3

u(x3) +

(cid:33)(cid:33)

p(x4

x3, d3)u(x4)
|

(cid:88)

x4

Finally we end up with the root decision potential

ρ3 (x1, x2, d1)∗ = ρ3 (x1, x2, d1) ρ2−3 (x2)∗ = p(x2

x1, d1)
|

and

µ3 (x1, x2, d1)∗ = µ3 (x2, x1, d1) +

µ2−3 (x2)∗
ρ2−3 (x2)∗
(cid:32)

(cid:32)

= max

d2

(cid:88)

x3

p(x3

|

x2, d2)

u(x2) + max

d3

u(x3) +

(cid:33)(cid:33)

p(x4

x3, d3)u(x4)
|

(cid:88)

x4

From the ﬁnal decision potential we have the expression

ρ3 (x1, x2, d1)∗ µ3 (x1, x2, d1)∗

(7.4.23)

(7.4.24)

(7.4.25)

(7.4.26)

(7.4.27)

(7.4.28)

(7.4.29)

which is equivalent to that which would be obtained by simply distributing the summations and maximi-
sations over the original ID. At least for this special case, we therefore have veriﬁed that the JT approach
yields the correct root clique potentials.

7.5 Markov Decision Processes

xt = j). At each time t we consider an
Consider a Markov chain with transition probabilities p(xt+1 = i
|
action (decision), which aﬀects the state at time t + 1. We describe this by

p(xt+1 = i
|

xt = j, dt = k)

(7.5.1)

Associated with each state xt is a utility u(xt), and is schematically depicted in ﬁg(7.8). More generally
one could consider utilities that depend on transitions and decisions, u(xt+1 = i, xt = j, dt = k) and also

DRAFT April 24, 2015

137

d1

x1

d2

x2

u2

d3

x3

u3

x4

u4

Markov Decision Processes

Figure 7.8: Markov Decision Process. These can be
used to model planning problems of the form ‘how
do I get to where I want to be incurring the lowest
total cost?’. They are readily solvable using a message
passing algorithm.

xt = j, dt = k), ut(xt+1 = i, xt = j, dt = k). We’ll stick
time dependent versions of all of these, pt(xt+1 = i
|
with the time-independent (stationary) case here since the generalisations are conceptually straightforward
at the expense of notational complexity. Markov Decision Processes (MDPs) can be used to solve planning
tasks such as how to get to a desired goal state as quickly as possible.

For positive utilities, the total utility of any state-decision path x1:T , d1:T is deﬁned as (assuming we know
the initial state x1)

U (x1:T )

T
(cid:88)

t=2

≡

u(xt)

and the probability with which this happens is given by

p(x2:T

x1, d1:T −1) =
|

T −1
(cid:89)

t=1

p(xt+1

xt, dt)
|

At time t = 1 we want to make that decision d1 that will lead to maximal expected total utility

U (d1

x1)

|

≡

(cid:88)

x2

max
d2

(cid:88)

x3

max
d3

(cid:88)

x4

. . . max
dT −1

(cid:88)

xT

p(x2:T

x1, d1:T −1)U (x1:T )
|

(7.5.2)

(7.5.3)

(7.5.4)

Our task is to compute U (d1
x1) for each state of d1 and then choose that state with maximal expected
total utility. To carry out the summations and maximisations eﬃciently, we could use the junction tree
approach, as described in the previous section. However, in this case, the ID is suﬃciently simple that a
direct message passing approach can be used to compute the expected utility.

|

7.5.1 Maximising expected utility by message passing

Consider the time-dependent decisions (non-stationary policy) MDP

T −1
(cid:89)

t=1

p(xt+1

xt, dt)
|

T
(cid:88)

t=2

u(xt)

For the speciﬁc example in ﬁg(7.8) the joint model of the BN and utility is

p(x4

x3, d3)p(x3
|

x2, d2)p(x2
|

x1, d1) (u(x2) + u(x3) + u(x4))
|

To decide on how to take the ﬁrst optimal decision, we need to compute

(7.5.5)

(7.5.6)

U (d1

|

x1) =

(cid:88)

x2

max
d2

(cid:88)

x3

max
d3

(cid:88)

x4

p(x4

x3, d3)p(x3
|

|

x2, d2)p(x2

|

x1, d1) (u(x2) + u(x3) + u(x4))

(7.5.7)

Since only u(x4) depends on x4 explicitly, we can write

U (d1

|

x1) =

(cid:88)

x2

max
d2

(cid:88)

x3

p(x3

|

(cid:32)

x2, d2)p(x2

x1, d1)

|

u(x2) + u(x3) + max

d3

(cid:33)

x3, d3)u(x4)

(7.5.8)

(cid:88)

x4

p(x4

|

138

DRAFT April 24, 2015

Markov Decision Processes

Deﬁning a message and corresponding value

u3←4(x3)

max
d3

≡

(cid:88)

x4

p(x4

x3, d3)u(x4),
|

v(x3)

≡

u(x3) + u3←4(x3)

we can write

U (d1

x1) =
|

(cid:88)

x2

max
d2

(cid:88)

x3

p(x3

x2, d2)p(x2
|

x1, d1) (u(x2) + v(x3))
|

In a similar manner, only the last term depends on x3 and hence

U (d1

x1) =
|

(cid:88)

x2

(cid:32)

p(x2

x1, d1)
|

u(x2) + max

d2

(cid:33)

(cid:88)

x3

p(x3

x2, d2)v(x3)
|

Deﬁning similarly the value

v(x2)

≡

u(x2) + max

d2

(cid:88)

x3

p(x3

x2, d2)v(x3)
|

then

U (d1

x1) =
|

(cid:88)

x2

p(x2

x1, d1)v(x2)
|

Given U (d1

x1) above, we can then ﬁnd the optimal decision d1 by
|

d∗
1(x1) = argmax

d1

U (d1

x1)
|

7.5.2 Bellman’s equation

In a Markov Decision Process, as above, we can deﬁne utility messages recursively as

ut−1←t(xt−1)

max
dt−1

≡

(cid:88)

xt

p(xt

xt−1, dt−1) [u(xt) + ut←t+1(xt)]
|

It is more common to deﬁne the value of being in state xt as

vt(xt)

≡

u(xt) + ut←t+1(xt),

vT (xT ) = u(xT )

and write then the equivalent recursion

vt−1(xt−1) = u(xt−1) + max
dt−1

(cid:88)

xt

p(xt

|

xt−1, dt−1)vt(xt)

The optimal decision d∗

t is then given by

d∗
t (xt) = argmax

dt

(cid:88)

xt+1

p(xt+1

xt, dt)vt+1(xt+1)
|

Equation(7.5.17) is called Bellman’s equation[30]3.

3The continuous-time analog has a long history in physics and is called the Hamilton-Jacobi equation.

DRAFT April 24, 2015

(7.5.9)

(7.5.10)

(7.5.11)

(7.5.12)

(7.5.13)

(7.5.14)

(7.5.15)

(7.5.16)

(7.5.17)

(7.5.18)

139

Temporally Unbounded MDPs

Figure 7.9: States deﬁned on a two dimensional grid. In each square the top
left value is the state number, and the bottom right is the utility of being
in that state. An ‘agent’ can move from a state to a neighbouring state, as
indicated. The task is to solve this problem such that for any position (state)
one knows how to move optimally to maximise the expected utility. This
means that we need to move towards the goal states (states with non-zero
utility). See demoMDP.

7.6 Temporally Unbounded MDPs

In the previous discussion about MDPs we assumed a given end time, T , from which one can propagate
messages back from the end of the chain. The inﬁnite T case would appear to be ill-deﬁned since the sum
of utilities

u(x1) + u(x2) + . . . + u(xT )

(7.6.1)

will in general be unbounded. There is a simple way to avoid this diﬃculty. If we let u∗ = maxs u(s) be the
largest value of the utility and consider the sum of modiﬁed utilities for a chosen discount factor 0 < γ < 1

T
(cid:88)

t=1

γtu(xt)

u∗

≤

T
(cid:88)

t=1

γt = γu∗ 1
−
1
−

γT
γ

(7.6.2)

where we used the result for a geometric series. In the limit T
this means that the summed modiﬁed
utility γtu(xt) is ﬁnite. The only modiﬁcation required to our previous discussion is to include a factor γ
in the message deﬁnition. Assuming that we are at convergence, we deﬁne a value v(xt = s) dependent
only on the state s, and not the time. This means we replace the time-dependent Bellman’s value recursion
equation (7.5.17) with the time-independent equation

→ ∞

v(s)

≡

u(s) + γ max

d

(cid:88)

s(cid:48)

p(xt = s(cid:48)

|

xt−1 = s, dt−1 = d)v(s(cid:48))

(7.6.3)

We then need to solve equation (7.6.3) for the value v(s) for all states s. The optimal decision policy when
one is in state xt = s is then given by

d∗(s) = argmax

d

(cid:88)

s(cid:48)

p(xt+1 = s(cid:48)

xt = s, dt = d)v(s(cid:48))
|

(7.6.4)

For a deterministic transition p (i.e. for each decision d, only one state s(cid:48) is available), this means that the
best decision is the one that takes us to the accessible state with highest value.

Equation(7.6.3) seems straightforward to solve. However, the max operation means that the equations
are non-linear in the value v and no closed form solution is available. Two popular techniques for solving
equation (7.6.3), are Value and Policy iteration, which we describe below. When the number of states S
is very large, approximate solutions are required. Sampling and state-dimension reduction techniques are
described in [61].

7.6.1 Value iteration

A naive procedure is to iterate equation (7.6.3) until convergence, assuming some initial guess for the values
(say uniform). One can show that this value iteration procedure is guaranteed to converge to a unique
optimum[35]. The convergence rate depends on γ — the smaller γ is, the faster is the convergence. An
example of value iteration is given in ﬁg(7.10).

140

DRAFT April 24, 2015

1020314050607080901001101201301401501601701811902002102202302402502602702802903003103203303403503603703803904004104204304404504614704804905005115205305405505605705805906006106206306406506606706806907017107207317407507607707807908008108208308408508608708808909019109219309409509609709809901000Temporally Unbounded MDPs

{

×

stay, left, right, up, down

Figure 7.10: Value Iteration on a set of 225 states,
corresponding to a 15
15 two dimensional grid. De-
terministic transitions are allowed to neighbours on
. There are three
the grid,
goal states, each with utility 1 – all other states have
utility 0. Plotted is the value v(s) for γ = 0.9 after
30 updates of Value Iteration, where the states index
y grid. The optimal decision for
a point on the x
any state on the grid is to go to the neighbouring state
with highest value. See demoMDP.

−

}

7.6.2 Policy iteration

In policy iteration we ﬁrst assume we know the optimal decision d∗(s) for any state s. We may use this in
equation (7.6.3) to give

v(s) = u(s) + γ

(cid:88)

s(cid:48)

p(xt = s(cid:48)

xt−1 = s, d∗(s))v(s(cid:48))
|

(7.6.5)

The maximisation over d has disappeared since we have assumed we already know the optimal decision for
each state s. For ﬁxed d∗(s), equation (7.6.5) is now linear in the value. Deﬁning the value v and utility u
vectors and transition matrix P,

[v]s = v(s),

[u]s = u(s),

[P]s(cid:48),s = p(s’
|

s, d∗ (s))

in matrix notation, equation (7.6.5) becomes

v = u + γPTv

(cid:16)

I

−

⇔

γPT(cid:17)

v = u

(cid:16)

I

v =

−

⇔

γPT(cid:17)−1

u

(7.6.6)

(7.6.7)

These linear equations are readily solved with Gaussian Elimination. Using this, the optimal policy is
recomputed using equation (7.6.4). The two steps of solving for the value, and recomputing the policy are
iterated until convergence. The procedure may be initialised by guessing an initial d∗(s), then solve the
linear equations (7.6.5) for the value, or alternatively guessing the initial values and solving for the initial
policy.

@@
@@

@@

Example 7.7 (A grid-world MDP). We deﬁne a set of states on
N grid with corresponding utilities
for each state, as given
for example in ﬁg(7.9) for N = 8. The agent is able to deterministically move to
a neighbouring grid state at each time step. After initialising the value of each grid state to unity, the
converged value for each state is
15 grid is given in ﬁg(7.10) in which
the utilities are zero everywhere except for 3 states. The optimal policy is then given by moving to the
neighbouring grid state with highest value.

obtained. An example on a 15

an N

×

×

7.6.3 A curse of dimensionality

Consider the following Tower of Hanoi problem. There are 4 pegs a, b, c, d and 10 disks numbered from 1
to 10. You may move a single disk from one peg to another – however, you are not allowed to put a bigger
numbered disk on top of a smaller numbered disk. Starting with all disks on peg a, how can you move them
all to peg d in the minimal number of moves?

This would appear to be a straightforward Markov decision process in which the transitions are allowed disk
moves. If we use x to represent the state of the disks on the 4 pegs, naively this has 410 = 1048576 states

DRAFT April 24, 2015

141

123456789101112131415123456789101112131415024Variational Inference and Planning

(some are equivalent up to permutation of the pegs, which reduces this by a factor of 2). This large number
of states renders this naive approach computationally problematic.

Many interesting real-world problems suﬀer from this large number of states issue so that a naive approach
to ﬁnd the best decision is computationally infeasible. Finding eﬃcient exact and also approximate state
representations is a key aspect to solving large scale MDPs, see for example [208].

7.7 Variational Inference and Planning

For the ﬁnite-horizon stationary policy MDP, learning the optimal policy can be addressed by a variety of
methods. Two popular approaches are policy gradients and EM style procedures – see for example [109]
and section(11.2).
For many MDPs of interest the optimal policy is deterministic[283], so that methods which explicitly seek
for deterministic policies are of interest. For this reason and to remain close to our discussion on policy
and value iteration, which involved deterministic policies, we focus on this case in our brief discussion here,
referring the reader to other texts [81, 298, 107, 108] for the details on the non-deterministic case. For a
time-independent deterministic policy d(s) that maps a state s to a decision d (which we write as π for
short), we have the expected utility

U (π) =

T
(cid:88)

(cid:88)

ut(xt)

(cid:88)

t
(cid:89)

t=1

xt

x1:t−1

τ =1

p(xτ

xτ −1, d(xτ −1))
|

(7.7.1)

with the convention p(x1
x0, d(x0)) = p(x1). Viewed as a factor graph, this is simply a set of chains, so
|
that for any policy π, the expected utility can be computed easily. In principle one could then attempt to
optimise U with respect to the policy directly. An alternative is to use an EM style procedure[107]. To do
this we deﬁne a (trans-dimensional) distribution

ˆp(x1:t, t) =

ut(xt)
Z(π)

t
(cid:89)

τ =1

p(xτ

xτ −1, d(xτ −1))
|

The normalisation constant Z(π) of this distribution is

(7.7.2)

T
(cid:88)

(cid:88)

t=1

x1:t

ut(xt)

t
(cid:89)

τ =1

p(xτ

xτ −1, d(xτ −1)) =
|

T
(cid:88)

(cid:88)

t=1

x1:t

ut(xt)

t
(cid:89)

τ =1

p(xτ

xτ −1, d(xτ −1)) = U (π)
|

(7.7.3)

If we now deﬁne a variational distribution q(x1:t, t), and consider

KL(q(x1:t, t)

ˆp(x1:t, t))
|

≥

0

this gives the lower bound

log U (π)

H(q(x1:t, t)) +

≥ −

log ut(xt)

(cid:42)

t
(cid:89)

τ =1

(cid:43)

p(xτ

xτ −1, d(xτ −1))
|

q(x1:t,t)

(7.7.4)

(7.7.5)

where H(q(x1:t, t)) is the entropy of the distribution q(x1:t, t). In terms of an EM algorithm, the M-step
requires the dependency on π alone, which is

E(π) =

=

T
(cid:88)

t
(cid:88)

t=1
T
(cid:88)

τ =1
t
(cid:88)

t=1

τ =1

log p(xτ
(cid:104)

xτ −1, d(xτ −1))
|

(cid:105)q(xτ ,xτ −1,t)

q(xτ = s(cid:48), xτ −1 = s, t) log p(xτ = s(cid:48)

xτ −1 = s, d(xτ −1) = d)
|

(7.7.6)

(7.7.7)

For each given state s we now attempt to ﬁnd the optimal decision d, which corresponds to maximising

ˆE(d
s) =
|

(cid:40) T

(cid:88)

t
(cid:88)

(cid:88)

s(cid:48)

t=1

τ =1

(cid:41)

q(xτ = s(cid:48), xτ −1 = s, t)

log p(s(cid:48)

s, d)
|

142

(7.7.8)

DRAFT April 24, 2015

Financial Matters

Deﬁning

q(s(cid:48)

s)
|

∝

T
(cid:88)

t
(cid:88)

t=1

τ =1

q(xτ = s(cid:48), xτ −1 = s, t)

(7.7.9)

we see that for given s, up to a constant, ˆE(d
p(s(cid:48)
|
with q(s(cid:48)

s):

s, d) so that the optimal decision d is given by the index of the distribution p(s(cid:48)

s) is the Kullback-Leibler divergence between q(s(cid:48)
|

s) and
|
s, d) most closely aligned
|

|

d∗(s) = argmin

KL(cid:0)q(s(cid:48)

d

p(s(cid:48)
s)
|

|

s, d)(cid:1)
|

(7.7.10)

The E-step concerns the computation of the marginal distributions required in the M-step. The optimal q
distribution is proportional to ˆp evaluated at the previous decision function d:

q(x1:t, t)

ut(xt)

∝

t
(cid:89)

τ =1

p(xτ

xτ −1, d(xτ −1))
|

For a constant discount factor γ at each time-step and an otherwise time-independent utility4

ut(xt) = γtu(xt)

using this

q(x1:t, t)

γtu(xt)

∝

t
(cid:89)

τ =1

p(xτ

xτ −1, d(xτ −1))
|

(7.7.11)

(7.7.12)

(7.7.13)

For each t this is a simple Markov chain for which the pairwise transition marginals required for the M-step,
equation (7.7.9) are straightforward. This requires inference in a series of Markov models of diﬀerent lengths.
This can be done eﬃciently using a single forward and backward pass [298, 109].

EM and related methods follow closely the spirit of inference in graphical models, but can exhibit disappoint-
ingly slow convergence. More recently, an alternative method using Lagrange Duality shows very promising
performance and the reader is referred to [110] for details. Note that this EM algorithm formally fails in
the case of a deterministic environment (the transition p(xt
xt−1, dt−1) is deterministic) – see exercise(7.8)
|
for an explanation and exercise(7.9) for a possible resolution.

Remark 7.2 (Solving an MDP – easy or hard?). The discussion in section(7.5.1) highlights that solving a
linear-chain inﬂuence diagram (ﬁnding the optimal decision at each timestep) is straightforward, and can
In
be achieved using a simple message passing algorithm, scaling linearly with the length of the chain.
contrast, ﬁnding the optimal time-independent policy π typically is much more complex – hence the reason
for many diﬀerent algorithms that attempt to ﬁnd optimal policies in areas such as time-independent control,
reinforcement learning and games. Mathematically, the reason for this diﬀerence is that the constraint in the
time-independent case that the policy must be the same over all time steps, leads to a graphical structure
that is no longer a chain, with all timepoints connected to a single π. In this case, in general, no simple
linear time message passing algorithm is available to optimise the resulting objective.

7.8 Financial Matters

Utility and decision theory play a major role in ﬁnance, both in terms of setting prices based on expected
future gains, but also in determining optimal investments. In the following sections we brieﬂy outline two
such basic applications.

4In the standard MDP framework it is more common to deﬁne ut(xt) = γt−1u(xt) so that for comparison with the standard

Policy/Value routines one needs to divide the expected utility by γ.

DRAFT April 24, 2015

143

Financial Matters

Su

S∗

Sd

T

Figure 7.11: Options pricing: An asset has market value £S at time
t = 1. We assume that the asset will have market value either £Su
or £Sd at time T . The owner and ‘options buyer’ (client) agree that
if the market value is greater than the ‘strike price’ £S∗ at time T ,
the client has the right to purchase the asset for £S∗. The question
is: how much should the owner of the asset charge the client for the
privilege of having the right to purchase the asset?

S

1

7.8.1 Options pricing and expected utility

An owner has an asset currently priced by the market at £S. The owner wants to give us the opportunity
to purchase this asset at time T for an agreed price of £S∗. At time T , if the market price goes ‘up’ beyond
the strike price to £Su we will decide to purchase the asset from the owner for £S∗ and sell the asset at
this increased value, see ﬁg(7.11). If, however, the price remains ‘down’ below the strike price at £Sd, we
will walk away, leaving the owner with the asset. The question is, how much should the owner charge, £C
for this option of us being able to buy the asset for the agreed price at time T ? To help answer this, we also
need to know how much a risk-free investment would make over the same time period (i.e. how much we
would get from putting money in a safe bank). We assume the interest rate R is known for the time period
T , say 0.06. We call the two people the owner and the client (who may or may not buy the asset).

Two possibilities case

For simplicity, we assume the asset can take only the prices Su or Sd at time T . We also assume we know
ρ. Let’s work out the expected utilities for
the probabilities of these events (see below), namely ρ and 1
both parties:

−

Asset goes up:

U (up, client) =

Su
S∗
−
(cid:124) (cid:123)(cid:122) (cid:125)
immediate proﬁt from selling

−

C
(cid:124)(cid:123)(cid:122)(cid:125)
option cost

−

CR
(cid:124)(cid:123)(cid:122)(cid:125)
lost interest on option cost

U (up, owner) =

S
S∗
−
(cid:124) (cid:123)(cid:122) (cid:125)
immediate loss from sale

+

C
(cid:124)(cid:123)(cid:122)(cid:125)
option cost

+

CR
(cid:124)(cid:123)(cid:122)(cid:125)
gained interest on option cost

−

SR
(cid:124)(cid:123)(cid:122)(cid:125)
lost interest

(7.8.1)

(7.8.2)

The ﬁnal SR term above comes from entering into the option deal – otherwise the owner could have just
sold the asset at time 1, and then put the resulting sum in the bank.

Asset goes down:

U (down, client) =

−

C
(cid:124)(cid:123)(cid:122)(cid:125)
option cost

−

CR
(cid:124)(cid:123)(cid:122)(cid:125)
lost interest on option cost

The above follows since in this case the client doesn’t act on his option to sell the asset.

U (down, owner) =

S
Sd
−
(cid:124) (cid:123)(cid:122) (cid:125)
change in asset value

+

C
(cid:124)(cid:123)(cid:122)(cid:125)
option cost

+

CR
(cid:124)(cid:123)(cid:122)(cid:125)
gained interest on option cost

−

SR
(cid:124)(cid:123)(cid:122)(cid:125)
lost interest

The expected utility for the client is

U (client) = ρ

U (up, client) + (1

ρ)

×

= ρ (Su
= ρ (Su

C

−

S∗
S∗)

−

−

−

−

C(1 + R)

−
CR) + (1

×

U (down, client)
CR)
C
ρ) (

−

−

−

The expected utility for the owner is

U (owner) = ρ

×

= ρ (S∗
= ρ (S∗

U (up, owner) + (1
S + C + CR
Sd) + Sd

−

−

ρ)

−
×
SR) + (1

U (down, owner)
ρ) (Sd
SR

−

−

S + C(1 + R)

−

−

−

S + C + CR

SR)

−

(7.8.3)

(7.8.4)

(7.8.5)

(7.8.6)

(7.8.7)

(7.8.8)

(7.8.9)

(7.8.10)

144

DRAFT April 24, 2015

Financial Matters

It seems reasonable to assume that both the client and the owner should have the same expected beneﬁt,
U (client) = U (owner). Hence

ρ (Su

S∗)

−

−

C(1 + R) = ρ (S∗

Sd) + Sd

−

−

S + C(1 + R)

SR

−

Solving for C, we ﬁnd

C =

ρ (Su

−

2S∗ + Sd)

Sd + S(1 + R)

−

2(1 + R)

(7.8.11)

(7.8.12)

All the quantities required to price the option are assumed known, except for ρ. One way to set this is
described below.

Setting ρ

It seems reasonable to expect ρ to be set by some process which describes the true probability of the price
increase. However, given a value for ρ and knowing the two possible prices Su and Sd, the owner can compute
the expected utility of just holding on to the asset. This is

ρSu + (1

ρ)Sd

S

−

−

(7.8.13)

Alternatively, the owner could sell the asset for £S and put his money in the bank, collecting the interest
RS at time T . In a fair market we must have that the expected reward for holding on to the asset is the
same as the risk-free return on the asset:

ρSu + (1

ρ)Sd

−

−

S = RS

⇒

ρ =

S(1 + R)
Su

−
Sd

−

Sd

(7.8.14)

Using this value to price the option in equation (7.8.12) ensures that the expected gain from oﬀering the
option and not oﬀering the option is the same to the owner and, furthermore that if the option is available,
the expected reward for both parties is the same.

7.8.2 Binomial options pricing model

If we have more than two timepoints, we can readily extend the above. It’s easiest to assume that at each
time t we have two price change possibilities – either the price can go up by a factor u > 1 or down by
a factor d < 1. For T timesteps, we then have a set of possible values the asset can take at time T . For
some of them the client will sell the asset (when ST > S∗) otherwise not. We need to work out the expected
gains for both the owner and client as before, assuming ﬁrst that we know ρ (which is the probability of the
price increasing by a factor u in one time step). For a sequence of n ups and T
n downs, we have a price
ST = SundT −n. If this is greater than S∗, then the client will sell the asset and have utility

−

I (cid:2)SundT −n > S∗

(cid:3) (SundT −n

S∗

−

−

C(1 + R))

The probability of n ups and T

n downs is

−

β(T, n, ρ)

(cid:19)

(cid:18)T
n

≡

ρn(1

−

ρ)T −n

(7.8.15)

(7.8.16)

where (cid:0)T
n

(cid:1) is the binomial coeﬃcient. Hence the total expected utility for the client on the upside is

U (client, up) =

T
(cid:88)

n=0

β(T, n, ρ)I (cid:2)SundT −n > S∗

(cid:3) (SundT −n

S∗

−

−

C(1 + R))

Similarly,

U (client, down) =

C(1 + R)

−

T
(cid:88)

n=0

β(T, n, ρ)I (cid:2)SundT −n < S∗

(cid:3)

DRAFT April 24, 2015

(7.8.17)

(7.8.18)

145

The total expected utility for the client is then

U (client) = U (client, up) + U (client, down)

Similarly, for the owner

U (owner, up) = (S∗

−

S(1 + R) + C(1 + R))

T
(cid:88)

n=0

β(T, n, ρ)I (cid:2)SundT −n > S∗

(cid:3)

Financial Matters

(7.8.19)

(7.8.20)

and

and

U (owner, down) =

T
(cid:88)

n=0

β(T, n, ρ)I (cid:2)SundT −n < S∗

(cid:3) (cid:0)SundT −n

S(1 + R) + C(1 + R)(cid:1)

−

(7.8.21)

@@

U (owner) = U (owner, up) + U (owner, down)

Setting

U (client) = U (owner)

results in a simple linear equation for C.

Setting ρ

(7.8.22)

(7.8.23)

To set ρ, we can use a similar logic as in the two timestep case. First we compute the expected value of the
asset at time T , which is

T
(cid:88)

n=0

β(T, n, ρ)SundT −n

(7.8.24)

and equate the expected gain equal to the gain from a risk free investment in the asset:

T
(cid:88)

n=0

β(T, n, ρ)SundT −n

S = RS

−

⇒

T
(cid:88)

n=0

β(T, n, ρ)undT −n = R + 1

(7.8.25)

Knowing u and d, we can solve the above for ρ, and then use this in the equation for C. We can learn u and
d from past observed data (in the literature they are often related to the observed variance in the prices[74]).

The binomial options pricing approach is a relatively simplistic way to price options. The celebrated Black-
Scholes method [45] is essentially a limiting case in which the number of timepoints becomes inﬁnite[150].

7.8.3 Optimal investment

Another example of utility in ﬁnance, and which is related to Markov decision processes, is the issue of
how best to invest your wealth in order to maximise some future criterion. We’ll consider here a very
simple setup, but one which can be readily extended to more complex scenarios. We assume that we have
two assets, a and b with prices at time t given by sa
t , sb
t. The prices are assumed to independently follow
Markovian updating:

t = sa
sa

t−1(1 + (cid:15)a
t )

⇒

p(sa
t |

t−1, (cid:15)a
sa

t ) = δ (cid:0)sa

(1 + (cid:15)a

t )sa

t−1

(cid:1)

t −

) is the Dirac delta function. The price increments follow a Markov transition

where δ(
·
t , (cid:15)b
p((cid:15)a
t|

t−1, (cid:15)b
(cid:15)a

t−1) = p((cid:15)a
t |

t−1)p((cid:15)b
(cid:15)a
t|

(cid:15)b
t−1)

(7.8.26)

(7.8.27)

Using this one can model eﬀects such as price increments being likely to stay the same (as in bank interest)
or more variable (as in the stock market).

146

DRAFT April 24, 2015

Financial Matters

(a)

(b)

(c)

Figure 7.12: (a): Two assets through time – a risky asset whose value ﬂuctuates wildly, and a stable asset
that grows slowly. (b): The wealth of our portfolio over time, based on investing with the hope to achieve
(c): The optimal investment decisions through time, with 1 corresponding to
a wealth of 1.5 at T = 40.
placing all wealth in the safe asset and 0 placing all the money on the risky asset.

We have an investment decision 0
a at time t. We will invest the rest of our wealth in asset b. If asset a is priced at sa
a fraction dt of our current wealth wt to purchase a quantity qa
are given by

1 that says what fraction of our current wealth wt we buy of asset
t and we decide to use
t of asset b, these

t of asset a and quantity qb

dt

≤

≤

qa
t =

dtwt
sa
t

,

qb
t =

dt)

wt(1

−
sb
t

(7.8.28)

At time step t + 1, the prices of assets a and b will have changed to sa
be

t+1, sb

t+1, so that our new wealth will

wt+1 = qa

t sa

t+1 + qb

t sb

t+1 =

t+1

dtwtsa
sa
t

+

wt(1

dt)sb
−
sb
t

t+1

(cid:16)

= wt

dt(1 + (cid:15)a

t+1) + (1

−

This can be expressed as a transition

dt)(1 + (cid:15)b

t+1)

(cid:17)

p(wt+1

wt, (cid:15)a
|

t+1, (cid:15)b

t+1, dt) = δ

(cid:16)

wt+1

wt

−

(cid:16)

dt(1 + (cid:15)a

t+1) + (1

−

dt)(1 + (cid:15)b

t+1)

(cid:17)(cid:17)

(7.8.29)

(7.8.30)

At an end time T we have a utility u(wT ) that expresses our satisfaction with our wealth. Given that we
start with a wealth w1 and assume we know (cid:15)a
1, we want to ﬁnd the best decision d1 that will maximise
our expected utility at time T . To do so, we also bear in mind that at any intermediate time 1 < t < T we
can adjust the fraction dt of wealth in asset a. The Markov chain is given by (see also section(23.1))

1, (cid:15)b

p((cid:15)a

1:T , (cid:15)b

1:T , w2:T

1, (cid:15)b
(cid:15)a
|

1, w1, d1:T −1) =

T
(cid:89)

t=2

p((cid:15)a
t |

t−1)p((cid:15)b
(cid:15)a
t|

(cid:15)b
t−1)p(wt

wt−1, (cid:15)a
|

t , (cid:15)b

t, dt−1)

(7.8.31)

The expected utility of a decision d1 is

1, (cid:15)b
(cid:15)a

1, w1) =

U (d1

|

(cid:88)

2 ,(cid:15)b
(cid:15)a

2,w2

. . . max
dT −2

(cid:88)

T −1,(cid:15)b
(cid:15)a

T −1,wT −1

max
dT −1

(cid:88)

T ,(cid:15)b
(cid:15)a

T ,wT

p((cid:15)a

1:T , (cid:15)b

1:T , w2:T

1, (cid:15)b
(cid:15)a
|

1, w1, d1:T −1)u(wT ) (7.8.32)

which, for the corresponding inﬂuence diagram, corresponds to the ordering

d1

≺

(cid:110)

2, (cid:15)b
(cid:15)a

2, w2

(cid:111)

d2

≺

≺

. . .

≺

(cid:110)

T −1, (cid:15)b
(cid:15)a

T −1, wT −1

(cid:111)

dT −1

≺

≺

(cid:110)

T , (cid:15)b
(cid:15)a

T , wT

(cid:111)

(7.8.33)

To compute U (d1

1, w1), we ﬁrst can carry out the operations at T to give a message

1, (cid:15)b
(cid:15)a
|
T −1, (cid:15)b

γT −1←T ((cid:15)a

T −1, wT −1)

max
dT −1

≡

(cid:88)

T ,(cid:15)b
(cid:15)a

T ,wT

p((cid:15)a
T |

T −1)p((cid:15)b
(cid:15)a
T |

(cid:15)b
T −1)p(wT

wT −1, (cid:15)a
|

T , (cid:15)b

T , dT −1)u(wT ) (7.8.34)

And, generally,

γt−1←t((cid:15)a

t−1, (cid:15)b

t−1, wt−1)

max
dt−1

≡

(cid:88)

t ,(cid:15)b
(cid:15)a

t ,wt

p((cid:15)a
t |

t−1)p((cid:15)b
(cid:15)a
t|

(cid:15)b
t−1)p(wt

|

wt−1, (cid:15)a

t , (cid:15)b

t, dt−1)γt←t+1((cid:15)a

t , (cid:15)b

t, wt) (7.8.35)

DRAFT April 24, 2015

147

05101520253035400.511.522.5305101520253035400.811.21.41.61.8051015202530354000.20.40.60.81d1

h1

v1

d2

h2

v2

u2

d3

h3

v3

u3

h4

v4

u4

Further Topics

Figure 7.13: An example Partially Observable Markov
Decision Process (POMDP). The ‘hidden’ variables h
are never observed. In solving the Inﬂuence Diagram
we are required to ﬁrst sum over variables that are
never observed; doing so will couple together all past
observed variables and decisions; any decision at time
t will then depend on all previous decisions. Note that
the no-forgetting principle means that we do not need
to explicitly write that each decision depends on all
previous observations – this is implicitly assumed.

so that

U (d1

1, (cid:15)b
(cid:15)a
|

1, w1) =

(cid:88)

2 ,(cid:15)b
(cid:15)a

2,w2

p((cid:15)a
2|

1)p((cid:15)b
(cid:15)a
2|

(cid:15)b
1)p(w2

w1, (cid:15)a
|

2, (cid:15)b

2, d1)γ2←3((cid:15)a

2, (cid:15)b

2, w2)

(7.8.36)

Note that this process is not equivalent to a ‘myopic’ strategy which would make an investment decision to
maximise the expected next-period wealth.

For a continuous wealth wt the above messages are diﬃcult to represent. A simple strategy, therefore, is to
discretise all wealth values and also the price changes (cid:15)a
t and investment decisions dt, see exercise(7.13).
In this case, one needs to approximate the delta function δ(x) by the distribution which is zero for every
state except that discrete state which is closest to the real value x.

t , (cid:15)b

Example 7.8 (Optimal investment). We demonstrate a simple optimal portfolio investment problem in
ﬁg(7.12), in which there is a safe bank asset and a risky ‘stock market’ asset. We start with a unit wealth
and wish to obtain a wealth of 1.5 at time t = 40. If we place all our money in the bank, we will not be
able to reach this desired amount, so we must place some of our wealth at least in the risky asset. In the
beginning the stock market does poorly, and our wealth is correspondingly poor. The stock market picks
up suﬃciently so that after around t = 20, we no longer need to take many risks and may place most of our
money in the bank, conﬁdent that we will reach our investment objective.

7.9 Further Topics

7.9.1 Partially observable MDPs

In a POMDP there are states that are not observed. This seemingly innocuous extension of the MDP case
can lead however to computational diﬃculties. Let’s consider the situation in ﬁg(7.13), and attempt to
compute the optimal expected utility based on the sequence of summations and maximisations. The sum
over the hidden variables couples all the decisions and observations, meaning that we no longer have a simple
chain structure for the remaining maximisations. For a POMDP of length t, this leads to an intractable
problem with complexity exponential in t. An alternative view is to recognise that all past decisions and
observations v1:t, d1:t−1, can be summarised in terms of a belief in the current latent state, p(ht
v1:t, d1:t−1).
|
This suggests that instead of having an actual state, as in the MDP case, we need to use a distribution
over states to represent our current knowledge. One can therefore write down an eﬀective MDP albeit over
belief distributions, as opposed to ﬁnite states. Approximate techniques are required to solve the resulting
‘inﬁnite’ state MDPs, and the reader is referred to more specialised texts for a study of approximation
procedures. See for example [161, 164].

7.9.2 Reinforcement learning

Reinforcement Learning deals mainly with time-independent Markov Decision Processes. The added twist
is that the transition p(s(cid:48)
s, d) (and possibly the utility) is unknown. Initially an ‘agent’ begins to explore
|

148

DRAFT April 24, 2015

Further Topics

the set of states and utilities (rewards) associated with taking decisions. The set of accessible states and
their rewards populates as the agent traverses its environment. Consider for example a maze problem with a
given start and goal state, though with an unknown maze structure. The task is to get from the start to the
goal in the minimum number of moves on the maze. Clearly there is a balance required between curiosity
and acting to maximise the expected reward. If we are too curious (don’t take optimal decisions given the
currently available information about the maze structure) and continue exploring the possible maze routes,
this may be bad. On the other hand, if we don’t explore the possible maze states, we might never realise
that there is a much more optimal short-cut to follow than that based on our current knowledge. This
exploration-exploitation tradeoﬀ is central to the diﬃculties of RL. See [283] for an extensive discussion of
reinforcement learning.

From model based to model free learning

@@
@@

Consider a MDP with state transitions p(xt+1
|
that depend only on the state xt. The expected utility of taking decision dt in state xt,
derived using a similar argument as in section(7.5.2), for a discount factor γ.
we have (now including discounting)

xt). For simplicity we consider utilities
|
xt), can be
|
Analogous to equation (7.5.10),

xt, dt) and policy p(dt

U (dt

U (dt

xt) =
|

(cid:88)

xt+1

(cid:32)

p(xt+1

xt, dt)
|

u(xt+1) + γ max

d

(cid:33)

(cid:88)

x(cid:48)

p(x(cid:48)

xt+1, d)v(x(cid:48))
|

Using the value recursion

v(xt+1) = u(xt+1) + γ max

d

(cid:88)

x(cid:48)

p(x(cid:48)

xt+1, d)v(x(cid:48))
|

we can write

U (dt

|

xt) =

(cid:88)

xt+1

p(xt+1

xt, dt)v(xt+1)
|

Substituting this into equation (7.9.2) we have

v(xt+1) = u(xt+1) + γ max

d

xt+1)
U (d
|

Substituting equation (7.9.4) into equation (7.9.3) we obtain

U (dt

|

xt) =

(cid:88)

xt+1

(cid:18)

(cid:19)

p(xt+1

xt, dt)
|

u(xt+1) + max

d

xt+1)
U (d
|

(7.9.1)

(7.9.2)

(7.9.3)

(7.9.4)

(7.9.5)

x). Given this solution, when
If we know the model p(xt+1
xt, dt) we can solve equation (7.9.5) for U (d
|
|
we are in state x, the optimal policy is to take the decision d = arg maxd U (d
x). In the case that we do
|
not wish to explicitly store or describe a model p(xt+1
xt, dt) we can use a sample from this transition to
|
approximate equation (7.9.5); if we are in state xt and take decision dt the environment returns for us a
sample xt+1. This gives the one-sample estimate to equation (7.9.5):

˜U (dt

|

xt) = u(xt+1) + γ max

d

˜U (d
xt+1)
|

This gives a highly stochastic update and to ensure convergence it is preferable to use

˜Ut+1(dt

xt) = (1
|

−

αt) ˜Ut(dt

xt) + αt
|

(cid:18)

u(xt+1) + γ max

d

˜Ut(d
xt+1)
|

(cid:19)

which can be written

˜Ut+1(dt

xt) = ˜Ut(dt
|

xt) + αt
|

(cid:18)

u(xt+1) + γ max

d

˜Ut(d
xt+1)
|

−

˜Ut(dt

(cid:19)

xt)
|

DRAFT April 24, 2015

(7.9.6)

(7.9.7)

(7.9.8)

149

Further Topics

α < 1, (cid:80)

, (cid:80)

t α2

≤

t αt =

where the learning rate satisﬁes 0
. (For example αt = 1/t.) This gives a
∞
procedure called Q-learning for updating the approximation to U based on samples from the environment5.
This is a simple and powerful scheme and as such is one of the most popular model-free methods in re-
inforcement learning. A complicating factor is that if we select a decision based on d = arg maxd ˜U (d
x)
|
this inﬂuences the sample that will be next drawn. Nevertheless, under certain conditions (essentially all
decisions are repeatedly sampled for each state), the sample estimate ˜Ut(d
x)
x) converges to the exact U (d
|
|
in the limit t

[311].

t <

∞

→ ∞

Bayesian reinforcement learning

(observed transitions and utilities) one aspect of the RL problem
For a given set of environment data
can be considered as ﬁnding the policy that maximises expected reward, given only prior belief about the
environment and observed decisions and states.
If we assume we know the utility function but not the
transition, we may write

X

U (π

) =

U (π
(cid:104)

θ)
|

(cid:105)p(θ|X )
where θ represents the environment state transition,

|X

θ = p(xt+1

xt, dt)
|

Given a set of observed states and decisions,

p(θ

)

|X

∝

p(

X |

θ)p(θ)

(7.9.9)

(7.9.10)

(7.9.11)

where p(θ) is a prior on the transition. Similar techniques to the EM style training can be carried through
in this case as well[81, 298, 108]. Rather than the policy being a function of the state and the environment
θ, optimally one needs to consider a policy p(dt
xt, b(θ)) as a function of the state and the belief in the
environment, b(θ)
). This means that, for example, if the belief in the environment has high
p(θ
entropy, the agent can recognise this and explicitly carry out decisions/actions to explore the environment.
A further complication in RL is that the data collected
depends on the policy π. If we write t for an
‘episode’ in which policy πt is followed and data
t collected, then the utility of the policy π given all the
historical information is

|X

≡

X

X

|

U (π

π1:t,
|

1:t) =

X

U (π
(cid:104)

|

θ)

(cid:105)p(θ|X1:t,π1:t)

(7.9.12)

Depending on the prior on the environment, and also on how long each episode is, we will have diﬀerent
posteriors for the environment parameters. If we then set

πt+1 = argmax

π

U (π

π1:t,
|

1:t)

X

(7.9.13)

this aﬀects the data we collect at the next episode
be very diﬀerent depending on the episodes and priors.

X

t+1. In this way, the trajectory of policies π1, π2, . . . can

7.10 Summary

•

•

•

•

One way to take decisions is to take that decision that maximises the expected utility of the decision.

Sequential decision problems can be modelled using decision trees. These are powerful but unwieldy in long
decision sequences.

Inﬂuence diagrams extend belief networks to the decision arena. Eﬃcient inference approaches carry over
to this case as well, including extensions using the strong junction tree formalism.

The sequence in which information is revealed and decisions are taken is speciﬁed in the inﬂuence diagram.
The optimal utility is not invariant to the corresponding partial-ordering.

5The standard notation in the Q-learning literature uses Q(x, d) in place of U (d|x).

150

DRAFT April 24, 2015

Code

a

t

x

dx

Ux

l

e

s

d

b

dh

Uh

s = Smoking

x = Positive X-ray

d = Dyspnea (Shortness of breath)

e = Either Tuberculosis or Lung Cancer

t = Tuberculosis

l = Lung Cancer

b = Bronchitis

a = Visited Asia
dh = Hospitalise?
dx = Take X-ray?

Figure 7.14: Inﬂuence Diagram for the ‘Chest Clinic’ Decision example.

•

•

Markov decision processes correspond to a simple chain-like inﬂuence diagram, for which inference is
straightforward, and corresponds to the classical Bellman equations.

Reinforcement learning can be considered an extension of the Markov decision framework when the model
of the environment in which the agent acts needs to be learned on the basis of experience.

In this chapter we discussed planning and control as an inference problem with particular attention to
discrete variables. See example(28.2) for an application of approximate inference to continuous control.

7.11 Code

7.11.1 Sum/Max under a partial order

maxsumpot.m: Generalised elimination operation according to a partial ordering
sumpotID.m: Sum/max an ID with probability and decision potentials
demoDecParty.m: Demo of summing/maxing an ID

7.11.2 Junction trees for inﬂuence diagrams

In the code
There is no need to specify the information links provided that a partial ordering is given.
jtreeID.m no check is made that the partial ordering is consistent with the inﬂuence diagram.
In this
case, the ﬁrst step of the junction tree formulation in section(7.4.2) is not required. Also the moralisation
and removal of utility nodes is easily dealt with by deﬁning utility potentials and including them in the
moralisation process.

The strong triangulation is found by a simple variable elimination scheme which seeks to eliminate a variable
with the least number of neighbours, provided that the variable may be eliminated according to the speciﬁed
partial ordering. The junction tree is constructed based only on the elimination clique sequence
N .
C
i to
obtained from the triangulation routine. The junction tree is then obtained by connecting a clique
the ﬁrst clique j > i that is connected to this clique. Clique
i is then eliminated from the graph. In this
manner a junction tree of connected cliques is formed. We do not require the separators for the inﬂuence
diagram absorption since these can be computed and discarded on the ﬂy.

1, . . . ,

C

C

C

Note that the code only computes messages from the leaves to the root of the junction tree, which is
suﬃcient for taking decisions at the root. If one desires an optimal decision at a non-root, one would need
to absorb probabilities into a clique which contains the decision required. These extra forward probability
absorptions are required because information about any unobserved variables can be aﬀected by decisions

DRAFT April 24, 2015

151

Code

and observations in the past. This extra forward probability schedule is not given in the code and left as an
exercise for the interested reader.
jtreeID.m: Junction Tree for an Inﬂuence Diagram
absorptionID.m: Absorption on an Inﬂuence Diagram
triangulatePorder.m: Triangulation based on a partial ordering
demoDecPhD.m: Demo for utility of Doing PhD and Startup

7.11.3 Party-Friend example

The code below implements the Party-Friend example in the text. To deal with the asymmetry the V isit
utility is zero if P arty is in state yes.
demoDecPartyFriend.m: Demo for Party-Friend

7.11.4 Chest Clinic with Decisions

The table for the Chest Clinic Decision network, ﬁg(7.14) is taken from exercise(3.4), see [131, 72]. There is
a slight modiﬁcation however to the p(x
e) table. If an x-ray is taken, then information about x is available.
However, if the decision is not to take an x-ray no information about x is available. This is a form of
asymmetry. A straightforward approach in this case is to make dx a parent of the x variable and set the
distribution of x to be uninformative if dx = fa.

|

p(s = tr) = 0.5
p(a = tr) = 0.01
p(t = tr
a = fa) = 0.01
p(t = tr
a = tr) = 0.05
|
|
s = fa) = 0.01
p(l = tr
s = tr) = 0.1
p(l = tr
|
|
s = fa) = 0.3
p(b = tr
s = tr) = 0.6
p(b = tr
|
|
e = tr, dx = tr) = 0.98 p(x = tr
p(x = tr
e = fa, dx = tr) = 0.05
|
|
p(x = tr
p(x = tr
e = fa, dx = fa) = 0.5
e = tr, dx = fa) = 0.5
|
|
p(d = tr
e = tr, b = tr) = 0.9
e = tr, b = fa) = 0.3
p(d = tr
|
|
e = fa, b = fa) = 0.1
p(d = tr
e = fa, b = tr) = 0.2
p(d = tr
|
|

(7.11.1)

The two utilities are designed to reﬂect the costs and beneﬁts of taking an x-ray and hospitalising a patient:

l = tr
t = tr
dh = tr
180
l = fa 120
t = tr
dh = tr
t = fa l = tr
dh = tr
160
t = fa l = fa 15
dh = tr
l = tr
dh = fa t = tr
2
l = fa 4
dh = fa t = tr
dh = fa t = fa l = tr
0
dh = fa t = fa l = fa 40

(7.11.2)

t = tr
dx = tr
0
t = fa 1
dx = tr
dx = fa t = tr
10
dx = fa t = fa 10

(7.11.3)

We assume that we know whether or not the patient has been to Asia, before deciding on taking an x-ray.
The partial ordering is then

dx

a

≺

d, x

≺ {

} ≺

dh

b, e, l, s, t

≺ {

}

(7.11.4)

The demo demoDecAsia.m produces the results:

utility table:
asia = yes takexray = yes 49.976202
asia = no takexray = yes 46.989441
48.433043
asia = yes takexray = no
47.460900
asia = no takexray = no

which shows that optimally one should take an x-ray only if the patient has been to Asia.
demoDecAsia.m: Junction Tree Inﬂuence Diagram demo

152

DRAFT April 24, 2015

Exercises

7.11.5 Markov decision processes

In demoMDP.m we consider a simple two dimensional grid in which an ‘agent’ can move to a grid square either
above, below, left, right of the current square, or stay in the current square. We deﬁned goal states (grid
squares) that have high utility, with others having zero utility.
demoMDPclean.m: Demo of Value and policy iteration for a simple MDP
MDPsolve.m: MDP solver using value or policy iteration
Routines for eﬃcient MDP variational solvers are available from the book website. There is also code for
fast Lagrange duality techniques, which are beyond the scope of our discussion here.

7.12 Exercises

Exercise 7.1. You play a game in which you have a probability p of winning. If you win the game you gain
an amount £S and if you lose the game you lose an amount £S. Show that the expected gain from playing
the game is £(2p

1)S.

−

Exercise 7.2. It is suggested that the utility of money is based, not on the amount, but rather how much we
have relative to other people. Assume a distribution p(i), i = 1, . . . , 10 of incomes using a histogram with 10
bins, each bin representing an income range. Use a histogram to roughly reﬂect the distribution of incomes
in society, namely that most incomes are around the average with few very wealthy and few extremely poor
people. Now deﬁne the utility of an income x as the chance that income x will be higher than a randomly
chosen income y (under the distribution you deﬁned) and relate this to the cumulative distribution of p.
Write a program to compute this probability and plot the resulting utility as a function of income. Now
repeat the coin tossing bet of section(7.1.1) so that if one wins the bet one’s new income will be placed in the
top histogram bin, whilst if one loses one’s new income is in the lowest bin. Compare the optimal expected
utility decisions under the situations in which one’s original income is (i) average, and (ii) much higher
than average.

Exercise 7.3.

Derive a partial ordering for the ID on the right, and
explain how this ID diﬀers from that of ﬁg(7.5).

T est

Oil

U2

U1

Seismic

Drill

Exercise 7.4. This question follows closely demoMDP.m, and represents a problem in which a pilot wishes
to land an airplane. The matrix U (x, y) in the ﬁle airplane.mat contains the utilities of being in position
15 grid
x, y and is a very crude model of a runway and taxiing area. The airspace is represented by an 18
(Gx = 18, Gy = 15 in the notation employed in demoMDP.m). The matrix U (8, 4) = 2 represents that position
(8, 4) is the desired parking bay of the airplane (the vertical height of the airplane is not taken in to account).
The positive values in U represent runway and areas where the airplane is allowed. Zero utilities represent
neutral positions. The negative values represent unfavourable positions for the airplane. By examining the
matrix U you will see that the airplane should preferably not veer oﬀ the runway, and also should avoid two
small villages close to the airport.

×

At each timestep the plane can perform one of the following actions stay up down left right:
For stay, the airplane stays in the same x, y position.
For up, the airplane moves to the x, y + 1 position.
For down, the airplane moves to the x, y
For left, the airplane moves to the x
For right, the airplane moves to the x + 1, y position.

1, y position.

1 position.

−

−

@@

, and the plane remains in its current
A move that takes the airplane out of the airspace is not allowed
position x, y. For example, if we issue the right action, then we stay at x, y if x + 1, y is out of the airspace.

DRAFT April 24, 2015

153

Exercises

1. The airplane begins in at point x = 1, y = 13. Assuming that an action deterministically results in the
intended grid move, ﬁnd the optimal xt, yt sequence for times t = 1, . . . , for the position of the aircraft.

2.

The pilot tells you that there is a fault with the airplane for the right action. Provided x + 1, y is in
the airspace for current position x, y:

@@

If x, y + 1 is out of the airspace, then we go right to x + 1, y with probability 1.

If x, y +1 is in the airspace, we go right to x+1, y with probability 0.9 and up to x, y +1 with probability
0.1.

Assuming again that the airplane begins at point x = 1, y = 13, return the optimal xt, yt sequence for
times t = 1, . . . , for the position of the aircraft.

Exercise 7.5.

The inﬂuence diagram depicted describes the ﬁrst stage of a game. The decision
variable dom(d1) =
, indicates the decision to either play the ﬁrst
}
stage or not. If you decide to play, there is a cost c1(play) = C1, but no cost oth-
erwise, c1(no play) = 0. The variable x1 describes if you win or lose the game,
dom(x1) =

play, not play
{

win, lose

, with probabilities:
}

d1 = no play) = 0
p(x1 = win
|

(7.12.1)

{
p(x1 = win
|
The utility of winning/losing is

d1 = play) = p1,

u1(x1 = win) = W1,

u1(x1 = lose) = 0

(7.12.2)

Show that the expected utility gain of playing this game is

U (d1 = play) = p1W1

C1

−

c1

d1

x1

u1

(7.12.3)

Exercise 7.6. Exercise(7.5) above describes the ﬁrst stage of a new two-stage game. If you win the ﬁrst
stage x1 = win, you have to make a decision d2 as to whether or not play in the second stage dom(d2) =
. If you do not win the ﬁrst stage, you cannot enter the second stage. If you decide to play
play, not play
{
the second stage, you win with probability p2:

}

p(x2 = win
|

x1 = win, d2 = play) = p2

If you decide not to play the second stage there is no chance to win:

p(x2 = win
|

x1 = win, d2 = not play) = 0

The cost of playing the second stage is

c2(d2 = play) = C2,

c2(d2 = no play) = 0

and the utility of winning/losing the second stage is

u2(x2 = win) = W2,

u2(x2 = lose) = 0

(7.12.4)

(7.12.5)

(7.12.6)

(7.12.7)

1. Draw an Inﬂuence Diagram that describes this two-stage game.

2. A gambler needs to decide if he should even enter the ﬁrst stage of this two-stage game. Show that

based on taking the optimal future decision d2 the expected utility based on the ﬁrst decision is:

U (d1 = play) =

(cid:26) p1(p2W2
p1W1

−
C1

−

C2) + p1W1

C1

−

if p2W2
if p2W2

C2
C2

0
0

≥
≤

−
−

(7.12.8)

Exercise 7.7. You have £B in your bank account. You are asked if you would like to participate in a bet in
which, if you win, your bank account will become £W . However, if you lose, your bank account will contain
only £L. You win the bet with probability pw.

154

DRAFT April 24, 2015

Exercises

1. Assuming that the utility is given by the number of pounds in your bank account, write down a formula
for the expected utility of taking the bet, U (bet) and also the expected utility of not taking the bet,
U (no bet).

2. The above situation can be formulated diﬀerently. If you win the bet you gain £(W

B). If you lose
L). Compute the expected amount of money you gain if you bet Ugain(bet) and

−

the bet you lose £(B
if you don’t bet Ugain(no bet).

−

3. Show that U (bet)

U (no bet) = Ugain(bet)

Ugain(no bet).

−

−

Exercise 7.8. Consider an objective

F (θ) =

(cid:88)

x

U (x)p(x

θ)
|

(7.12.9)

for a positive function U (x) and that our task is to maximise F with respect to θ. An Expectation-
Maximisation style bounding approach (see section(11.2)) can be derived by deﬁning the auxiliary distribution

˜p(x

θ) =
|

U (x)p(x
F (θ)

θ)
|

so that by considering KL(q(x)

˜p(x)) for some variational distribution q(x) we obtain the bound
|
(cid:105)q(x) +
The M-step states that the optimal q distribution is given by

log p(x
(cid:104)

(cid:105)q(x) +

log U (x)

log F (θ)

log q(x)

≥ − (cid:104)

(cid:105)q(x)

θ)
|

(cid:104)

q(x) = ˜p(x

θold)

|

(7.12.10)

(7.12.11)

(7.12.12)

At the E-step of the algorithm the new parameters θnew are given by maximising the ‘energy’ term

θnew = argmax

θ

log p(x
(cid:104)

θ)
|

(cid:105)˜p(x|θold)

Show that for a deterministic distribution

p(x

θ) = δ (x, f (θ))
|

the E-step fails, giving θnew = θold.

Exercise 7.9. Consider an objective

F(cid:15)(θ) =

(cid:88)

x

U (x)p(cid:15)(x

θ)
|

for a positive function U (x) and

p(cid:15)(x

θ) = (1
|

−

(cid:15))δ (x, f (θ)) + (cid:15)n(x), 0

(cid:15)

≤

≤

1

(7.12.13)

(7.12.14)

(7.12.15)

(7.12.16)

and an arbitrary distribution n(x). Our task is to maximise F with respect to θ. As the previous exercise
showed, if we attempt an EM algorithm in the limit of a deterministic model (cid:15) = 0, then no-updating occurs
and the EM algorithm fails to ﬁnd θ that optimises F0(θ).

1. Show that

F(cid:15)(θ) = (1

−

(cid:15))F0(θ) + (cid:15)

(cid:88)

x

n(x)U (x)

and hence

F(cid:15)(θnew)

−

F(cid:15)(θold) = (1

(cid:15)) [F0(θnew)

F0(θold)]

−

−

DRAFT April 24, 2015

(7.12.17)

(7.12.18)

155

Exercises

2. Show that if for (cid:15) > 0 we can ﬁnd a θnew such that F(cid:15)(θnew) > F(cid:15)(θold), then necessarily F0(θnew) >

F0(θold).

3. Using this result, derive an EM-style algorithm that guarantees to increase F(cid:15)(θ) (unless we are already

at an optimum) for (cid:15) > 0 and therefore guarantees to increase F0(θ). Hint: use

θ) =

˜p(x
|

U (x)p(cid:15)(x
F(cid:15)(θ)

θ)
|

(7.12.19)

and consider KL(q(x)

˜p(x)) for some variational distribution q(x).
|

Exercise 7.10. The ﬁle IDjensen.mat contains probability and utility tables for the inﬂuence diagram of
ﬁg(7.7a). Using BRMLtoolbox, write a program that returns the maximal expected utility for this ID using a
strong junction tree approach, and check the result by explicit summation and maximisation. Similarly, your
program should output the maximal expected utility for both states of d1, and check that the computation
using the strong junction tree agrees with the result from explicit summation and maximisation.

Exercise 7.11. For a POMDP, explain the structure of the strong junction tree, and relate this to the
complexity of inference in the POMDP.

Exercise 7.12.

(i) Deﬁne a partial order for the ID depicted. (ii) Draw a (strong) junction
tree for this ID.

i

b

f

u2

d

g

a

e

u1

c

h

Exercise 7.13. exerciseInvest.m contains the parameters for a simple investment problem in which the
prices of two assets, a and b follow Markovian updating, as in section(7.8.3). The transition matrices of
these are given, as is the end time T , initial wealth w1, and initial price movements (cid:15)a
1, wealth and
investment states. Write a function of the form

1, (cid:15)b

[d1 val]=optdec(epsilonA1,epsilonB1,desired,T,w1,pars)

where desired is the desired wealth level at time T . The end utility is deﬁned by

u(wT ) =

0

(cid:26) 10000 wT

1.5w1
wT < 1.5w1

≥

(7.12.20)

Using your routine, compute the optimal expected utility and decision at time 1.
(Round wealth values to the
nearest possible discrete values given in exerciseInvest.m.) Draw also an inﬂuence diagram that describes
this Markov decision problem.

Exercise 7.14.
Tom writes two cheques, one of which has a value of twice the other. You can’t see the
value of either cheque and are asked to pick one. The one you select has a value of £100. You can cash this
cheque, or exchange it for the other cheque. Your friend Randy explains that it doesn’t really matter what
you do since you have a 50% chance of having in your hand the higher value cheque. What do you think
you should do? What if you don’t even look at the value of the cheque you ﬁrst select?

@@

++

156

DRAFT April 24, 2015

Part II

Learning in Probabilistic Models

157

Introduction to Part II

In Part II we address how to learn a model from data. In particular we will discuss
learning a model as a form of inference on an extended distribution, now taking into
account the parameters of the model.

Learning a model or model parameters from data forces us to deal with uncertainty
since with only limited data we can never be certain which is the ‘correct’ model. We
also address how the structure of a model, not just its parameters, can in principle be
learned.

In Part II we show how learning can be achieved under simplifying assumptions, such
as maximum likelihood that set parameters by those that would most likely reproduce
the observed data. We also discuss the problems that arise when, as is often the case,
there is missing data.

Together with Part I, Part II prepares the basic material required to embark on under-
standing models in Machine Learning, having the tools required to learn models from
data and subsequently query them to answer questions of interest.

DRAFT April 24, 2015

159

Maximum likelihood algorithms for learning in graphical models. The leaf nodes denote speciﬁc algorithms
described in Part II.

160

DRAFT April 24, 2015

GraphicalmodelUndirectedcompletedatadecomposableunconstr.countingconstr.limitedcliquesIPFexp.formISgradientnon-decomposablegradient(difﬁcult)incompletedatadecomposableexp.formISgradientnondecomposableIPF(difﬁcult)gradient(difﬁcult)gradientDirectedcompletedatacountingincompletedataVariationalEMCHAPTER 8

Statistics for Machine Learning

In this chapter we discuss some classical distributions and their manipulations. In previous chapters we’ve
assumed that we know the distributions and have concentrated on the inference problem.
In machine
learning we will typically not fully know the distributions and need to learn them from available data. This
means we need familiarity with standard distributions, for which the data will later be used to set the
parameters.

8.1 Representing Data

The numeric encoding of data can have a signiﬁcant eﬀect on performance and an understanding of the
options for representing data is therefore of considerable importance. We brieﬂy outline three central
encodings below.

8.1.1 Categorical

For categorical (or nominal) data, the observed value belongs to one of a number of classes, with no intrinsic
ordering, and can be represented simply by an integer. An example of a categorical variable would be the
description of the type of job that someone does, e.g. healthcare, education, ﬁnancial services, transport,
homeworker, unemployed, engineering etc. which could be represented by the values 1, 2, . . . , 7. Another
way to transform such data into numerical values would be to use 1-of-m encoding. For example, if there
are four kinds of jobs: soldier, sailor, tinker, spy, we could represent a soldier as (1,0,0,0), a sailor as (0,1,0,0),
a tinker as (0,0,1,0) and a spy as (0,0,0,1). In this encoding the distance between the vectors representing
two diﬀerent professions is constant. Note that 1-of-m encoding induces dependencies in the profession
attributes since if one of the attributes is 1, the others must be zero.

8.1.2 Ordinal

An ordinal variable consists of categories with an ordering or ranking of the categories, e.g. cold, cool, warm,
hot. In this case, to preserve the ordering, we could use say -1 for cold, 0 for cool, +1 for warm and +2 for
hot. This choice is somewhat arbitrary, and one should bear in mind that results may be dependent on the
numerical coding used.

8.1.3 Numerical

Numerical data takes on values that are real numbers, e.g. a temperature measured by a thermometer, or
the salary that someone earns.

161

Distributions

8.2 Distributions

Distributions over discrete variables, section(1.1) have been the focus of much of the book up to this point.
Here we discuss also distributions over continuous variables, for which the concepts of marginalisation and
conditioning carry over from the discrete case, simply on replacing summation over the discrete states with
integration over the continuous domain of the variable.

Deﬁnition 8.1 (Probability Density Functions). For a continuous variable x, the probability density p(x)
is deﬁned such that

p(x)

0,

≥

(cid:90) ∞

−∞

p(x)dx = 1,

p(a

x

≤

≤

b) =

(cid:90) b

a

p(x)dx

We will also refer to continuous probability densities as distributions.

Deﬁnition 8.2 (Averages and Expectation).

f (x)
(cid:104)

(cid:105)p(x)

(8.2.1)

(8.2.2)

denotes the average or expectation of f (x) with respect to the distribution p(x). A common alternative
notation is

E (f (x))

When the context is clear, one may drop the notational dependency on p(x). The notation

y
f (x)
|
(cid:104)

(cid:105)

(8.2.3)

(8.2.4)

is shorthand for the average of f (x) conditioned on knowing the state of variable y, i.e. the average of f (x)
with respect to the distribution p(x

y).
|

An advantage of the expectation notations is that they hold whether the distribution is over continuous or
discrete variables. In the discrete case

f (x)
(cid:104)

(cid:105) ≡

(cid:88)

x

f (x = x)p(x = x)

and for continuous variables,

f (x)
(cid:104)

(cid:105) ≡

(cid:90) ∞

−∞

f (x)p(x)dx

(8.2.5)

(8.2.6)

apple, orange, pear
,
The reader might wonder what
x
}
{
(cid:105)
(cid:104)
with associated probabilities p(x) for each of the states, what does
makes sense
refer to? Clearly,
if f (x = x) maps the state x to a numerical value. For example f (x = apple) = 1, f (x = orange) = 2,
f (x = pear) = 3 for which
is meaningful. Unless the states of the discrete variable are associated
(cid:104)
with a numerical value, then

means when x is discrete. For example, if dom(x) =

(cid:105)
has no meaning.

f (x)
(cid:104)

x
(cid:104)

(cid:105)

(cid:105)

f (x)
x
(cid:105)

(cid:104)

Result 8.1 (Change of variables). For a univariate continuous random variable x with distribution p(x)
the transformation y = f (x), where f (x) is a monotonic function, has distribution

p(y) = p(x)

(cid:19)−1

(cid:18) df
dx

, x = f −1(y)

For multivariate x and bijection f (x), then y = f (x) has distribution
(cid:18) ∂f
∂x

(cid:12)
(cid:12)
p(y) = p(x = f −1(y))
det
(cid:12)
(cid:12)

(cid:19)(cid:12)
−1
(cid:12)
(cid:12)
(cid:12)

(8.2.7)

(8.2.8)

162

DRAFT April 24, 2015

Distributions

where the Jacobian matrix has elements

(cid:20) ∂f
∂x

(cid:21)

=

ij

∂fi(x)
∂xj

(8.2.9)

Sometimes one needs to consider transformations between diﬀerent dimensions. For example, if z has lower
dimension than x, then one may introduce additional variables z(cid:48) to deﬁne a new multivariate y = (z, z(cid:48))
with the same dimension as x. Then one applies the above transformation to give the distribution on the
joint variables y, from which p(z) can be obtained by marginalisation.

Deﬁnition 8.3 (Moments). The kth moment of a distribution is given by the average of xk under the
distribution:
xk(cid:69)
(cid:68)

(8.2.10)

p(x)

For k = 1, we have the mean, typically denoted by µ,

µ

x

≡ (cid:104)

(cid:105)

(8.2.11)

Deﬁnition 8.4 (Cumulative Distribution Function). For a univariate distribution p(x), the CDF is deﬁned
as

cdf (y)

p(x

≡

≤

y) =

I [x

(cid:104)

≤

y]

For an unbounded domain, cdf (

(cid:105)p(x)
) = 0 and cdf (

−∞

) = 1.

∞

(8.2.12)

Deﬁnition 8.5 (Moment Generating Function). For a distribution p(x), we deﬁne the moment generating
function g(t) as

g(t) = (cid:10)etx(cid:11)

p(x)

The usefulness of this is that by diﬀerentiating g(t), we ‘generate’ the moments,

dk
dtk g(t) =

(cid:68)

lim
t→0

xk(cid:69)

p(x)

(8.2.13)

(8.2.14)

Deﬁnition 8.6 (Mode). The mode x∗ of a distribution p(x) is the state of x at which the distribution takes
its highest value, x∗ = arg maxx p(x). A distribution could have more than one mode (be multi-modal). A
widespread abuse of terminology is to refer to any isolated local maximum of p(x) to be a mode.

Deﬁnition 8.7 (Variance and Correlation).
)2(cid:69)

σ2

(x

(cid:68)

x

≡

− (cid:104)

(cid:105)

p(x)

(8.2.15)

The variance measures the ‘spread’ of a distribution around the mean. The square root of the variance, σ
is called the standard deviation and is a natural length scale suggesting how far typical values drawn from
p(x) might be from the mean. The notation var(x) is also used to emphasise for which variable the variance
is computed. The reader may show that an equivalent expression is

σ2

≡

(cid:10)x2(cid:11)

2

x
(cid:105)

− (cid:104)

DRAFT April 24, 2015

(8.2.16)

163

For a multivariate distribution the matrix with elements

Distributions

Σij =

(xi

(cid:104)

µi) (xj

µj)
(cid:105)

−

−
is called the covariance matrix . The diagonal entries of the covariance matrix contain the

(8.2.17)

where µi =
variance of each variable. An equivalent expression is

xi

(cid:104)

(cid:105)

Σij =

xixj

xi

xj

(cid:105) − (cid:104)
The correlation matrix has elements

(cid:105) (cid:104)

(cid:104)

(cid:105)

(cid:28) (xi

ρij =

−
σi

µi)

(xj

(cid:29)

µj)

−
σj

(8.2.18)

(8.2.19)

where σi is the deviation of variable xi. The correlation is a normalised form of the covariance so that each
element is bounded
1. The reader will note a resemblance of the correlation coeﬃcient and the
ρij
scalar product, equation (A.1.3). See also exercise(8.40).

1
−

≤

≤

xj the covariance Σij is zero. Similarly independent variables
For independent variables xi and xj, xi
have zero correlation – they are ‘uncorrelated’. Note however that the converse is not generally true – two
variables can be uncorrelated but dependent. A special case is for when xi and xj are Gaussian distributed
for which independence is equivalent to being uncorrelated, see exercise(8.2).

⊥⊥

Deﬁnition 8.8 (Skewness and Kurtosis). The skewness is a measure of the asymmetry of a distribution:

(cid:68)

(x

− (cid:104)

x
(cid:105)
σ3

)3(cid:69)

p(x)

γ1

≡

(8.2.20)

where σ2 is the variance of x with respect to p(x). A positive skewness means the distribution has a heavy
tail to the right. Similarly, a negative skewness means the distribution has a heavy tail to the left.

The kurtosis is a measure of how peaked around the mean a distribution is:

(cid:68)

(x

− (cid:104)

x
(cid:105)
σ4

)4(cid:69)

p(x)

3

−

γ2

≡

(8.2.21)

A distribution with positive kurtosis has more mass around its mean than would a Gaussian with the same
mean and variance. These are also called super Gaussian. Similarly a negative kurtosis (sub Gaussian)
distribution has less mass around its mean than the corresponding Gaussian. The kurtosis is deﬁned such
that a Gaussian has zero kurtosis (which accounts for the -3 term in the deﬁnition).

Deﬁnition 8.9 (Delta function). For continuous x, we deﬁne the Dirac delta function

δ(x

x0)

−

which is zero everywhere

(cid:90) ∞

δ(x

−

−∞

x0)f (x)dx = f (x0)

except at x0, where there is a spike. (cid:82) ∞

−∞ δ(x

x0)dx = 1 and

−

(8.2.22)

@@

(8.2.23)

One can view the Dirac delta function as an inﬁnitely narrow Gaussian: δ(x

x0) = limσ→0

−

(cid:0)x x0, σ2(cid:1).

N

The Kronecker delta,

δx,x0

(8.2.24)
is similarly zero everywhere, except for δx0,x0 = 1. The Kronecker delta is equivalent to δx,x0 = I [x = x0].
We use the expression δ (x, x0) to denote either the Dirac or Kronecker delta, depending on the context.

164

DRAFT April 24, 2015

Distributions

Deﬁnition 8.10 (Empirical Distribution). For a set of datapoints x1, . . . , xN , which are states of a random
variable x, the empirical distribution has probability mass distributed evenly over the datapoints, and zero
elsewhere.

For a discrete variable x the empirical distribution is, see ﬁg(8.1),

p(x) =

1
N

N
(cid:88)

n=1

I [x = xn]

where N is the number of datapoints.

For a continuous distribution we have

p(x) =

1
N

N
(cid:88)

n=1

xn)

δ (x

−

where δ (x) is the Dirac Delta function.

The mean of the empirical distribution is given by the sample mean of the datapoints

ˆµ =

1
N

N
(cid:88)

n=1

xn

Similarly, the variance of the empirical distribution is given by the sample variance

ˆσ2 =

1
N

N
(cid:88)

n=1

(xn

ˆµ)2

−

For vectors the sample mean vector has elements

ˆµi =

1
N

N
(cid:88)

n=1

xn
i

and sample covariance matrix has elements

ˆΣij =

1
N

N
(cid:88)

n=1

(xn

i −

ˆµi) (cid:0)xn

j −

(cid:1)

ˆµj

(8.2.25)

(8.2.26)

(8.2.27)

(8.2.28)

(8.2.29)

(8.2.30)

8.2.1 The Kullback-Leibler Divergence KL(q

p)
|

The Kullback-Leibler divergence KL(q

|

p) measures the ‘diﬀerence’ between distributions q and p[71].

Deﬁnition 8.11 (KL divergence). For two distributions q(x) and p(x)

KL(q

p)
|

log q(x)

≡ (cid:104)

−

log p(x)

(cid:105)q(x) ≥

0

(8.2.31)

1

2

3

4

Figure 8.1: Empirical distribution over a discrete variable with
4 states. The empirical samples consist of n samples at each
of states 1, 2, 4 and 2n samples at state 3 where n > 0. On
normalising this gives a distribution with values 0.2, 0.2, 0.4, 0.2
over the 4 states.

DRAFT April 24, 2015

165

The KL divergence is

0

≥

The KL divergence is widely used and it is therefore important to understand why the divergence is positive.

Distributions

To see this, consider the following linear bound on the function log(x)

log(x)

x

1

−

≤

(8.2.32)

as plotted in the ﬁgure on the right. Replacing x by p(x)/q(x) in the above bound

p(x)
q(x) −

1

≥

log

p(x)
q(x)

(8.2.33)

Since probabilities are non-negative, we can multiply both sides by q(x) to obtain

p(x)

q(x)

−

≥

q(x) log p(x)

−

q(x) log q(x)

(8.2.34)

We now integrate (or sum in the case of discrete variables) both sides. Using (cid:82) p(x)dx = 1, (cid:82) q(x)dx = 1,

1

1

−

log p(x)

≥ (cid:104)

log q(x)

(cid:105)q(x)

−

Rearranging gives

log q(x)
(cid:104)

−

log p(x)

(cid:105)q(x) ≡

KL(q

p)
|

≥

0

(8.2.35)

(8.2.36)

The KL divergence is zero if and only if the two distributions are exactly the same.

Deﬁnition 8.12 (α-divergence). For two distributions q(x) and p(x) and real α the α-divergence is deﬁned
as

Dα(p

q)
|

≡

(cid:68) pα−1(x)
qα−1(x)

(cid:69)

p(x)

1

−

α(1

α)

−

0

≥

(8.2.37)

The Kullback-Leibler divergence KL(p
veriﬁed using L’Hˆopital’s rule.

q) corresponds to D1(p
|

q) and KL(q
|

p) = D0(p
|

|

q), which is readily

8.2.2 Entropy and information

For both discrete and continuous variables, the entropy is deﬁned as

H(p)

≡ − (cid:104)

log p(x)

(cid:105)p(x)

(8.2.38)

For continuous variables, this is also called the diﬀerential entropy, see also exercise(8.34). The entropy is
a measure of the uncertainty in a distribution. One way to see this is that

H(p) =

KL(p

−

u) + const.
|

(8.2.39)

u)
|

0, the less like a uniform distribution p is, the smaller
where u is a uniform distribution. Since KL(p
will be the entropy. Or, vice versa, the more similar p is to a uniform distribution, the greater will be the
entropy. Since the uniform distribution contains the least information a priori about which state p(x) is
in, the entropy is therefore a measure of the a priori uncertainty in the state occupancy. For a discrete
distribution we can permute the state labels without changing the entropy. For a discrete distribution the
entropy is positive, whereas the diﬀerential entropy can be negative.
The mutual information is a measure of dependence between (sets of) variables
variables

, conditioned on

and

≥

X

Y

.

Z

166

DRAFT April 24, 2015

1234−4−3−2−10123Classical Distributions

Deﬁnition 8.13 (Mutual Information).

MI(

X

;

)

Y|Z

≡ (cid:104)

KL(p(

,

)

p(
|

X |Z

Y|Z

)p(

Y|Z

))

(cid:105)p(Z) ≥

0

(8.2.40)

X

If
and one writes MI(

is true, then MI(
;

X ⊥⊥ Y| Z

).

X

Y

;

Y|Z

) is zero, and vice versa. When

=

, the average over p(
∅

Z

Z

) is absent

X

8.3 Classical Distributions

Deﬁnition 8.14 (Bernoulli Distribution). The Bernoulli distribution concerns a discrete binary variable x,
with dom(x) =

. The states are not merely symbolic, but real values 0 and 1.

0, 1
{

}

p(x = 1) = θ

From normalisation, it follows that p(x = 0) = 1

θ. From this

−

= 0

x
(cid:104)

(cid:105)

×

p(x = 0) + 1

×

p(x = 1) = θ

The variance is given by var(x) = θ (1

θ).

−

(8.3.1)

(8.3.2)

Deﬁnition 8.15 (Categorical Distribution). The categorical distribution generalises the Bernoulli distribu-
,
tion to more than two (symbolic) states. For a discrete variable x, with symbolic states dom(x) =

1, . . . , C
{

}

p(x = c) = θc,

(cid:88)

c

θc = 1

The Dirichlet is conjugate to the categorical distribution.

(8.3.3)

Deﬁnition 8.16 (Binomial Distribution). The Binomial describes the distribution of a discrete two-state
variable x, with dom(x) =
where the states are symbolic. The probability that in n Bernoulli Trials
(independent samples), x1, . . . , xn there will be k ‘success’ states 1 observed is

1, 0

}

{

p(y = k

θ) =
|

(cid:19)

θk (1

(cid:18)n
k

−

θ)n−k ,

n
(cid:88)

i=1

y

≡

I (cid:2)xi = 1(cid:3)

(cid:1)
where (cid:0)n
k

n!/(k!(n

−

≡

k)!) is the binomial coeﬃcient. The mean and variance are

= nθ,

y
(cid:104)

(cid:105)

var(y) = nθ (1

θ)

−

The Beta distribution is the conjugate prior for the Binomial distribution.

(8.3.4)

(8.3.5)

Deﬁnition 8.17 (Multinomial Distribution). Consider a multi-state variable x, with dom(x) =
,
}
with corresponding state probabilities θ1, . . . , θK. We then draw n samples from this distribution. The
probability of observing the state 1 y1 times, state 2 y2 times, . . . , state K yK times in the n samples is

1, . . . , K
{

@@

p(y1, . . . , yK

θ) =
|

n!
y1! . . . yK!

K
(cid:89)

i=1

θyi
i

@@

where n =

(cid:80)K

i=1yi.

yi

(cid:104)

(cid:105)

= nθi, var(yi) = nθi (1

θi) ,

−

yiyj
(cid:104)

yi

yj

(cid:105) (cid:104)

(cid:105)

=

(cid:105) − (cid:104)

nθiθj (i

−

= j)

DRAFT April 24, 2015

(8.3.6)

(8.3.7)

167

(cid:54)
Classical Distributions

(a)

(b)

Figure 8.2: (a): Exponential distribution. (b): Laplace (double exponential) distribution.

The Dirichlet distribution is the conjugate prior for the multinomial distribution.

Deﬁnition 8.18 (Poisson Distribution). The Poisson distribution can be used to model situations in which
the expected number of events scales with the length of the interval within which the events can occur. If λ
is the expected number of events per unit interval, then the distribution of the number of events x within
an interval tλ is

p(x = k

λ) =
|

1
k!

e−λt (λt)k ,

k = 0, 1, 2, . . .

For a unit length interval (t = 1),

= λ, var(x) = λ

x
(cid:104)

(cid:105)

(8.3.8)

(8.3.9)

The Poisson distribution can be derived as a limiting case of a Binomial distribution in which the success
probability scales as θ = λ/n, in the limit n

.
→ ∞

Deﬁnition 8.19 (Uniform distribution). For a variable x, the distribution is uniform if p(x) = const. over
the domain of the variable.

Deﬁnition 8.20 (Exponential Distribution). For x

0, see ﬁg(8.2a),

≥

p(x

λ)
|

≡

λe−λx

One can show that for rate λ

=

x
(cid:104)

(cid:105)

1
λ

,

var(x) =

1
λ2

The alternative parameterisation b = 1/λ is called the scale.

Deﬁnition 8.21 (Gamma Distribution).

Gam (x

α, β) =

|

1
βΓ(α)

(cid:18) x
β

(cid:19)α−1

e− x
β ,

0, α > 0, β > 0

x

≥

α is called the shape parameter, β is the scale parameter and the Gamma function is deﬁned as

(8.3.10)

(8.3.11)

(8.3.12)

(8.3.13)

DRAFT April 24, 2015

Γ(a) =

(cid:90) ∞

0

ta−1e−tdt

168

−101234500.511.5  λ=0.2λ=0.5λ=1λ=1.5−50500.20.40.60.81  λ=0.2λ=0.5λ=1λ=1.5Classical Distributions

(a)

(b)

Figure 8.3: Gamma distribution. (a): varying α for ﬁxed β. (b): varying β for ﬁxed α.

The parameters are related to the mean and variance through

α =

(cid:17)2

,

(cid:16) µ
s

β =

s2
µ

(8.3.14)

where µ is the mean of the distribution and s is the standard deviation. The mode is given by (α
for α

1, see ﬁg(8.3).

≥

1) β,

−

An alternative parameterisation uses the inverse scale

Gamis (x

α, β) = Gam (x
|

α, 1/β)
|

∝

xα−1e−βx

Deﬁnition 8.22 (Inverse Gamma distribution).

InvGam (x
|

α, β) =

βα
Γ (α)

1
xα+1 e−β/x

This has mean β/(α

−

1) for α > 1 and variance

β2
(α−1)2(α−2)

for α > 2.

Deﬁnition 8.23 (Beta Distribution).

p(x

α, β) = B (x
|

α, β) =
|

1
B(α, β)

xα−1 (1

−

x)β−1 , 0

x

1

≤

≤

where the Beta function is deﬁned as

B(α, β) =

Γ(α)Γ(β)
Γ(α + β)

(8.3.15)

(8.3.16)

(8.3.17)

(8.3.18)

(a)

(b)

Figure 8.4: Beta distribution. The parameters α and β can also be written in terms of the mean and
variance, leading to an alternative parameterisation, see exercise(8.16).

DRAFT April 24, 2015

169

012345012345  α=1 β=0.2α=2 β=0.2α=5 β=0.2α=10 β=0.2012345012345  α=2 β=0.1α=2 β=0.5α=2 β=1α=2 β=200.20.40.60.8100.511.522.53  α=0.1 β=0.1α=1 β=1α=2 β=2α=5 β=500.20.40.60.8100.511.522.53  α=0.1 β=2α=1 β=2α=2 β=2α=5 β=2Classical Distributions

Figure 8.5: Top: 200 datapoints x1, . . . , x200 drawn from a Gaussian distribution.
Each vertical line denotes a datapoint at the corresponding x value on the hori-
zontal axis. Middle: Histogram using 10 equally spaced bins of the datapoints.
Bottom: Gaussian distribution
(x µ = 5, σ = 3) from which the datapoints
were drawn. In the limit of an inﬁnite amount of data, and limitingly small bin
size, the normalised histogram tends to the Gaussian probability density function.

N

and Γ(x) is the Gamma function. Note that the distribution can be ﬂipped by interchanging x for 1
which is equivalent to interchanging α and β. See ﬁg(8.4).

−

x,

The mean and variance are given by

=

x
(cid:104)

(cid:105)

α
α + β

,

var(x) =

αβ
(α + β)2 (α + β + 1)

Deﬁnition 8.24 (Laplace Distribution).

p(x

λ)
|

≡

λe− 1

b |x−µ|

For scale b

= µ,

x
(cid:104)

(cid:105)

var(x) = 2b2

The Laplace distribution is also known as the Double Exponential distribution, ﬁg(8.2b).

Deﬁnition 8.25 (Univariate Gaussian Distribution).

p(x

µ, σ2) =
|

N

(cid:0)x µ, σ2(cid:1)

1
√2πσ2

≡

e− 1

2σ2 (x−µ)2

(8.3.19)

(8.3.20)

(8.3.21)

(8.3.22)

where µ is the mean of the distribution, and σ2 the variance. This is also called the normal distribution.

One can show that the parameters indeed correspond to

µ =

x

(cid:105)N (x µ,σ2) ,

(cid:104)

σ2 =

(cid:68)

(x

µ)2(cid:69)

−

N (x µ,σ2)

(8.3.23)

For µ = 0 and σ = 1, the Gaussian is called the standard normal distribution. See ﬁg(8.5) for a depiction of
the univariate Gaussian and samples therefrom.

Deﬁnition 8.26 (Student’s t-distribution).

p(x

µ, λ, ν) = Student (x
|

µ, λ, ν) =
|

(cid:34)

(cid:19) 1
2

Γ( ν+1
2 )
Γ( ν
2 )

(cid:18) λ
νπ

λ (x

1 +

−
ν

(cid:35)− ν+1

2

µ)2

(8.3.24)

where µ is the mean, ν the degrees of freedom, and λ scales the distribution. The variance is given by

var(x) =

ν
λ (ν

−

2)

, for ν > 2

(8.3.25)

For ν
the distribution become fatter.

→ ∞

the distribution tends to a Gaussian with mean µ and variance 1/λ. As ν decreases the tails of

170

DRAFT April 24, 2015

−5051015−50510150102030−505101500.10.2Classical Distributions

(a)

(b)

(c)

(d)

Figure 8.6: Dirichlet distribution with parameter (u1, u2, u3) displayed on the simplex x1, x2, x3
x2 + x3 = 1. Black denotes low probability and white high probability. (a): (3, 3, 3) (b): (0.1, 1, 1).
(4, 3, 2). (d): (0.05, 0.05, 0.05).

≥

0, x1 +
(c):

The t-distribution can be derived from a scaled mixture

(cid:90) ∞

p(x

µ, a, b) =
|

τ =0 N

(cid:0)x µ, τ −1(cid:1) Gamis (τ

2 e− τ

2 (x−µ)2

(cid:90) ∞

=

=

τ =0
ba
Γ(a)

(cid:17) 1

(cid:16) τ
2π
Γ(a + 1
2 )
√2π

(cid:16)

b + 1

2 (x

1

−

µ)2(cid:17)a+ 1

2

a, b) dτ
|
bae−bτ τ a−1 1
Γ(a)

dτ

(8.3.26)

(8.3.27)

(8.3.28)

This matches equation (8.3.24) on setting ν = 2a and λ = a/b.

Deﬁnition 8.27 (Dirichlet Distribution). The Dirichlet distribution is a distribution on probability distri-
butions, α = (α1, . . . , αQ), αi

0, (cid:80)

i αi = 1:

p(α) =

1
Z(u)

δ

(cid:32) Q
(cid:88)

i=1

αi

−

≥

(cid:33) Q
(cid:89)
1

q=1

uq−1
q

α

I [αq

0]

≥

where

Z(u) =

(cid:81)Q
q=1 Γ(uq)
(cid:17)
(cid:16)(cid:80)Q

q=1 uq

Γ

It is conventional to denote the distribution as

Dirichlet (α

u)
|

(8.3.29)

(8.3.30)

(8.3.31)

The parameter u controls how strongly the mass of the distribution is pushed to the corners of the simplex.
Setting uq = 1 for all q corresponds to a uniform distribution, ﬁg(8.6). In the binary case Q = 2, this is
equivalent to a Beta distribution.

The product of two Dirichlet distributions is another Dirichlet distribution

Dirichlet (θ

u1) Dirichlet (θ
|

u2) = Dirichlet (θ
|

|

u1 + u2)

The marginal of a Dirichlet is also Dirichlet:

(cid:90)

θj

Dirichlet (θ

u) = Dirichlet (cid:0)θ\j|

|

u\j

(cid:1)

DRAFT April 24, 2015

(8.3.32)

(8.3.33)

171

00.5100.5100.51x3x2x100.5100.5100.51x3x2x100.5100.5100.51x3x2x100.5100.5100.51x3x2x1Multivariate Gaussian

(a)

(b)

(a): Bivariate Gaussian with mean (0, 0) and covariance [1, 0.5; 0.5, 1.75]. Plotted on the
Figure 8.7:
vertical axis is the probability density value p(x). (b): Probability density contours for the same bivariate
Gaussian. Plotted are the unit eigenvectors scaled by the square root of their eigenvalues, √λi.

The marginal of a single component θi is a Beta distribution:



p(θi) = B

θi



uj



ui,
|

(cid:88)

j(cid:54)=i

8.4 Multivariate Gaussian

(8.3.34)

The multivariate Gaussian plays a central role in data analysis and as such we discuss its properties in some
detail.

Deﬁnition 8.28 (Multivariate Gaussian Distribution).

p(x

µ, Σ) =
|

N

(x µ, Σ)

1
det (2πΣ)

(cid:112)

≡

e− 1

2 (x−µ)TΣ−1(x−µ)

(8.4.1)

where µ is the mean vector of the distribution, and Σ the covariance matrix. The inverse covariance Σ−1
is called the precision.

One may show

µ =

x

(cid:105)N (x µ,Σ) ,

(cid:104)

Σ =

(cid:68)

(x

µ) (x

−

−

µ)T(cid:69)

N (x µ,Σ)

(8.4.2)

Note that det (ρM) = ρDdet (M), where M is a D
notation in the normalisation constant of deﬁnition(8.28).

×

D matrix, which explains the dimension independent

The moment representation uses µ and Σ to parameterise the Gaussian. The alternative canonical repre-
sentation

p(x

b, M, c) = ce− 1
|

2 xTMx+xTb

is related to the moment representation via

Σ = M−1,

µ = M−1b,

1
det (2πΣ)

(cid:112)

= ce

1
2 bTM−1b

(8.4.3)

(8.4.4)

172

DRAFT April 24, 2015

−4−2024−4−202400.020.040.060.080.10.120.14−4−3−2−101234−4−3−2−101234  0.020.040.060.080.10.12Multivariate Gaussian

The multivariate Gaussian is widely used and it is instructive to understand the geometric picture. This
can be achieved by viewing the distribution in a diﬀerent co-ordinate system. First we use the fact that
every real symmetric matrix D

D has an eigen-decomposition

×

Σ = EΛET

(8.4.5)

where ETE = I and Λ = diag (λ1, . . . , λD). In the case of a covariance matrix, all the eigenvalues λi are
positive. This means that one can use the transformation

y = Λ− 1

2 ET (x

µ)

−

so that

µ)T Σ−1 (x

(x

−

µ) = (x

−

−

µ)T EΛ−1ET (x

µ) = yTy

−

(8.4.6)

(8.4.7)

Under this transformation, the multivariate Gaussian reduces to a product of D univariate zero-mean unit
variance Gaussians (since the Jacobian of the transformation is a constant). This means that we can view a
multivariate Gaussian as a shifted, scaled and rotated version of a ‘standard’ (zero mean, unit covariance)
Gaussian in which the centre is given by the mean, the rotation by the eigenvectors, and the scaling by the
square root of the eigenvalues, as depicted in ﬁg(8.7b). A Gaussian with covariance Σ = ρI for some scalar
ρ is an example of an isotropic meaning ‘same under rotation’. For any isotropic distribution, contours of
equal probability are spherical around the origin.

Result 8.2 (Product of two Gaussians). The product of two Gaussians is another Gaussian, with a multi-
plicative factor, exercise(8.35):

(x µ1, Σ1)

N

N

(x µ2, Σ2) =

N

(x µ, Σ)

(cid:16)

exp

−

1

2 (µ1 −
(cid:112)

µ2)T S−1 (µ1 −
det (2πS)

where S

≡

Σ1 + Σ2 and the mean and covariance are given by

µ = Σ1S−1µ2 + Σ2S−1µ1

Σ = Σ1S−1Σ2

(cid:17)

µ2)

(8.4.8)

(8.4.9)

8.4.1 Completing the square

A useful technique in manipulating Gaussians is completing the square. For example, the expression

exp

(cid:18)

−

1
2

xTAx + bTx

(cid:19)

can be transformed as follows. First we complete the square:

1
2

xTAx

−

bTx =

(cid:0)x

1
2

−

A−1b(cid:1)T

A (cid:0)x

A−1b(cid:1)

1
2

−

−

bTA−1b

Hence

@@

exp

(cid:18)

−

1
2

xTAx

+bTx

(cid:19)

=

N

(cid:0)x A−1b, A−1(cid:1) (cid:113)

det (cid:0)2πA−1(cid:1)exp

(cid:19)

bTA−1b

(cid:18) 1
2

From this one can derive

(cid:90)

exp

(cid:18)

1
2

−

xTAx + bTx

(cid:19)

dx =

(cid:113)

det (cid:0)2πA−1(cid:1)exp

(cid:19)

bTA−1b

(cid:18) 1
2

DRAFT April 24, 2015

(8.4.10)

(8.4.11)

(8.4.12)

(8.4.13)

173

Result 8.3 (Linear Transform of a Gaussian). Let y be linearly related to x through

Multivariate Gaussian

y = Mx + η

where x

η, η

⊥⊥

p(y) =

N

(µ, Σ), and x

∼ N
(cid:17)
(cid:16)
y Mµx + µ, MΣxMT + Σ

∼ N

(µx, Σx). Then the marginal p(y) = (cid:82)

(8.4.14)

x p(y

x)p(x) is a Gaussian
|

(8.4.15)

Result 8.4 (Partitioned Gaussian). Consider a distribution
and y of potentially diﬀering dimensions,

N

(z µ, Σ) deﬁned jointly over two vectors x

z =

(cid:19)

(cid:18) x
y

with corresponding mean and partitioned covariance

µ =

(cid:19)

(cid:18) µx
µy

Σ =

(cid:18) Σxx Σxy
Σyx Σyy

(cid:19)

where Σyx

≡
p(x) =

ΣT

xy. The marginal distribution is given by

(x µx, Σxx)

N

and conditional

p(x

y) =
|

N

(cid:0)x µx + ΣxyΣ−1

yy

(cid:0)y

(cid:1), Σxx

µy

−

−

ΣxyΣ−1

yy Σyx

(cid:1)

Result 8.5 (Gaussian average of a quadratic function).

(cid:68)

xTAx

(cid:69)

N (x µ,Σ)

= µTAµ + trace (AΣ)

(8.4.16)

(8.4.17)

(8.4.18)

(8.4.19)

(8.4.20)

8.4.2 Conditioning as system reversal

y). The formula for this Gaussian is
For a joint Gaussian distribution p(x, y), consider the conditional p(x
|
given in equation (8.4.19). An equivalent and useful way to write this result is to consider a ‘reversed’ linear
system of the form

x = ←−Ay + ←−η ,

where ←−η

(cid:16)

(cid:17)
←−η ←−µ , ←−Σ

∼ N

(8.4.21)

and show that the marginal over the ‘reverse’ noise ←−η is equivalent to conditioning. That is, for a Gaussian

p(x

y) =
|

(cid:90)

(cid:16)

x

δ

←−Ay

−

(cid:17)
− ←−η

p(←−η ),

p(←−η ) =

N

(cid:16)

(cid:17)
←−η ←−µ , ←−Σ

(8.4.22)

for suitably deﬁned ←−A, ←−µ , ←−Σ. To show this, we need to make the statistics of x under this linear system
match those given by the conditioning operation, (8.4.19). The mean and covariance of the linear system
equation (8.4.21) are given by

µx = ←−Ay + ←−µ ,

Σxx = ←−Σ.

We can make these match equation (8.4.19) by setting

←−A = ΣxyΣ−1
yy ,

←−Σ = Σxx

−

ΣxyΣ−1

yy Σyx,

←−µ = µx −

ΣxyΣ−1

yy µy

(8.4.23)

(8.4.24)

This means that we can write an explicit linear system of the form equation (8.4.21) where the parameters
are given in terms of the statistics of the original system. This is particularly useful in deriving results in
inference with Linear Dynamical Systems, section(24.3).

174

DRAFT April 24, 2015

Exponential Family

8.4.3 Whitening and centering

For a set of data x1, . . . , xN , with dim xn = D, we can transform this data to y1, . . . , yN with zero mean
using centering:

yn = xn

m

−

where the mean m of the data is given by

m =

1
N

N
(cid:88)

n=1

xn

(8.4.25)

(8.4.26)

Furthermore, we can transform to values z1, . . . , zN that have zero mean and unit covariance using whitening

zn = S− 1

2 (xn

m)

−

where the covariance S of the data is given by

S =

1
N

N
(cid:88)

n=1

(xn

−

m) (xn

−

m)T

(8.4.27)

(8.4.28)

An equivalent approach is to compute the SVD decomposition of the matrix of centered datapoints

USVT = Y,

Y = (cid:2)y1, . . . , yN (cid:3)

then for the D

N matrix

×

Z = √N diag (1/S1,1, . . . , 1/SD,D) UTY

(8.4.29)

(8.4.30)

the columns of Z = (cid:0)z1, . . . , zN (cid:1) have zero mean and unit covariance, see exercise(8.32).

Result 8.6 (Entropy of a Gaussian). The diﬀerential entropy of a multivariate Gaussian p(x) =
is

N

(x µ, Σ)

H(x)

≡ − (cid:104)

log p(x)

(cid:105)p(x) =

1
2

log det (2πΣ) +

D
2

where D = dim x. Note that the entropy is independent of the mean µ.

(8.4.31)

8.5 Exponential Family

A theoretically convenient class of distributions are the exponential family, which contains many standard
distributions, including the Gaussian, Gamma, Poisson, Dirichlet, Wishart, Multinomial.

Deﬁnition 8.29 (Exponential Family). For a distribution on a (possibly multidimensional) variable x
(continuous or discrete) an exponential family model is of the form

p(x

θ) = h(x)exp
|

(cid:32)

(cid:88)

i

(cid:33)

ηi (θ) Ti(x)

ψ(θ)

−

(8.5.1)

θ are the parameters, Ti(x) the test statistics, and ψ (θ) is the log partition function that ensures normali-
sation

ψ (θ) = log

(cid:90)

x

h(x)exp

(cid:33)

ηi (θ) Ti(x)

.

(cid:32)

(cid:88)

i

(8.5.2)

One can always transform the parameters to the form η (θ) = θ in which case the distribution is in canonical
form:

p(x

θ) = h(x)exp
|

(cid:16)

θTT(x)

(cid:17)

ψ (θ)

−

DRAFT April 24, 2015

(8.5.3)

175

For example the univariate Gaussian can be written

1
√2πσ2

exp

(cid:18)

1
2σ2 (x

−

−

Deﬁning t1(x) = x, t2(x) =

−

(cid:19)

(cid:18)

µ)2

= exp

1
2σ2 x2 +
x2/2 and , θ1 = µ, θ2 = σ2, h(x) = 1, then

µ
σ2 x

µ2
2σ2 −

1
2

−

−

log 2πσ2

η1(θ) =

θ1
θ2

,

η2(θ) =

1
θ2

,

ψ(θ) =

(cid:18) θ2
1
θ2

1
2

(cid:19)

+

log 2πθ2

Learning distributions

(cid:19)

.

(8.5.4)

@@

(8.5.5)

@@

Note that the parameterisation is not necessarily unique – we can for example rescale the functions Ti(x)
and inversely scale ηi by the same amount to arrive at an equivalent representation.

8.5.1 Conjugate priors

For an exponential family likelihood

p(x

θ) = h(x)exp
|

(cid:16)

θTT(x)

(cid:17)

ψ (θ)

−

and prior with hyperparameters α, γ,

p(θ

α, γ)
|

∝

(cid:16)

θTα

exp

(cid:17)

γψ (θ)

−

the posterior is

p(θ

x, α, γ)
|

p(x

∝
= p(θ

θ)p(θ
|

α, γ)
|

∝

exp

T(x) + α, 1 + γ)
|

(cid:16)

θT [T(x) + α]

[γ + 1] ψ (θ)

(cid:17)

−

(8.5.6)

(8.5.7)

(8.5.8)

(8.5.9)

so that the prior, equation (8.5.7), is conjugate for the exponential family likelihood equation (8.5.6); that
is, the posterior is of the same form as the prior, but with modiﬁed hyperparameters. Whilst the likelihood
is in the exponential family, the conjugate prior is not necessarily in the exponential family.

8.6 Learning distributions

For a distribution p(x
the θ that best explains the data

θ), parameterised by θ, and data
|

X

. There are various criteria for deﬁning this:

= (cid:8)x1, . . . , xN (cid:9), learning corresponds to inferring

X

Bayesian Methods In this one examines the posterior p(θ

)

p(

θ)p(θ). This gives rise to a distribu-

tion over θ. The Bayesian method itself says nothing about how to best summarise this posterior.

|X

∝

X |

Maximum A posteriori This is a summarisation of the posterior, that is

θM AP = argmax

p(θ

θ

)

|X

(8.6.1)

Maximum Likelihood Under a ﬂat prior, p(θ) = const., the MAP solution is equivalent to setting θ to

that value that maximises the likelihood of observing the data

θM L = argmax

θ

θ)

p(

X |

(8.6.2)

Moment Matching Based on an empirical estimate of a moment (say the mean), θ is set such that the

moment (or moments) of the distribution matches the empirical moment.

Pseudo Likelihood For multivariate x = (x1, . . . , xN ), one sets the parameters based on

θ = argmax

N
(cid:88)

D
(cid:88)

θ

n=1

i=1

log p(xn
i |

xn
\i, θ)

The pseudo-likelihood method is sometimes used when the full likelihood p(x

(8.6.3)

θ) is diﬃcult to compute.

|

DRAFT April 24, 2015

176

Learning distributions

In seeking the ‘best’ single parameter θ, we are often required to carry out a numerical optimisation. This
is not necessarily a trivial step, and considerable eﬀort is often spent either in attempting to deﬁne models
for which the resulting computational diﬃculties are minimal, or in ﬁnding good approximations that ﬁnd
useful optima of complex objective functions, see section(A.5).

In this book we focus on the Bayesian methods and maximum likelihood. We ﬁrst reiterate some of the
basic ground covered in section(1.3), in which the Bayesian and maximum likelihood methods are related.

Deﬁnition 8.30. Prior, Likelihood and Posterior

For data
of the data to a posterior belief:

X

and variable θ, Bayes’ rule tells us how to update our prior beliefs about the variable θ in light

p(θ)
θ)
p(
X |
(cid:124)(cid:123)(cid:122)(cid:125)
(cid:124) (cid:123)(cid:122) (cid:125)
prior
likelihood
p(
)
X
(cid:124) (cid:123)(cid:122) (cid:125)
evidence

=

p(θ
)
|X
(cid:124) (cid:123)(cid:122) (cid:125)
posterior

(8.6.4)

The evidence is also called the marginal likelihood . Note that the term ‘evidence’ is (rather unfortunately)
used for both the marginal likelihood of observations and the observations themselves.

The term ‘likelihood’ is used for the probability that a model generates observed data. More fully, if we
condition on the model M , we have

p(θ

|X

, M ) =

p(

X |

M )

|

θ, M )p(θ
M )
p(

X |

where we see the role of the likelihood p(

X |

θ, M ) and model likelihood p(

X |

M ).

The most probable a posteriori (MAP ) setting is that which maximises the posterior,

θM AP = argmax

p(θ

θ

, M )

|X

(8.6.5)

For a ‘ﬂat prior’, p(θ
that θ that maximises p(

|

θ, M ),

X |

M ) being a constant, the MAP solution is equivalent to maximum likelihood namely

θM L = argmax

p(

θ

θ, M )

X |

(8.6.6)

Deﬁnition 8.31 (conjugacy). If the posterior is of the same parametric form as the prior, then we call
the prior the conjugate distribution for the likelihood distribution. That is, for a prior on parameter θ,
with hyperparameter α, p(θ
is the same form as the prior, but with updated
hyperparameters, p(θ

α), the posterior given data
|
, α) = p(θ

D

|D

α(cid:48)).
|

Deﬁnition 8.32 (Independent and Identically distributed). For a variable x, and a set of i.i.d. observations,
x1, . . . , xN , conditioned on θ, we assume there is no dependence between the observations

p(x1, . . . , xN

θ) =
|

N
(cid:89)

n=1

p(xn

θ)

|

(8.6.7)

, it is interesting to know
For non-Bayesian methods which return a single value for θ, based on the data
how ‘good’ the procedure is. Concepts that can help in this case are ‘bias’ and ‘consistency’. The bias

X

DRAFT April 24, 2015

177

measures if the estimate of θ is correct ‘on average’. The property of an estimator such that the parameter
θ converges to the true model parameter θ0 as the sequence of data increases is termed consistency.

Properties of Maximum Likelihood

Deﬁnition 8.33 (Unbiased estimator). Given data
distribution p(x
θ) we can use the data
|
The estimator is a function of the data, which we write ˆθ(

X

= (cid:8)x1, . . . , xN (cid:9), formed from i.i.d. samples of a
to estimate the parameter θ that was used to generate the data.

X

X

). For an unbiased (parameter) estimator

(8.6.8)

(cid:68)ˆθ(
X

(cid:69)
)

= θ

p(X |θ)

More generally, one can consider any function of the distribution p(x), with scalar value θ, for example the
mean θ =
) if
(cid:68)ˆθ(
X

) is an unbiased estimator of θ with respect to the data distribution ˜p(

(cid:105)p(x). Then ˆθ(
x
= θ.

(cid:69)
)

˜p(X )

X

X

(cid:104)

A classical example for estimator bias are those of the mean and variance. Let

) =

ˆµ(

X

1
N

N
(cid:88)

n=1

xn

This is an unbiased estimator of the mean

x

(cid:105)p(x) since, for iid data

(cid:104)

ˆµ(
(cid:104)

X

(cid:105)p(X ) =
)

1
N

N
(cid:88)

n=1

xn

(cid:104)

(cid:105)p(xn) =

1
N

N

x

(cid:105)p(x) =

(cid:104)

x
(cid:104)

(cid:105)p(x)

On the other hand, consider the estimator of the variance,

ˆσ2(

X

) =

1
N

N
(cid:88)

n=1

(xn

))2

ˆµ(

X

−

This is biased since (omitting a few lines of algebra)

(cid:10)ˆσ2(
X

)(cid:11)

p(X ) =

1
N

N
(cid:88)

n=1

(cid:68)
(xn

ˆµ(

X

−

))2(cid:69)

=

N

1

σ2

−
N

8.7 Properties of Maximum Likelihood

(8.6.9)

(8.6.10)

(8.6.11)

(8.6.12)

A crude summary of the posterior is given by a distribution with all its mass in a single most likely state,
δ (cid:0)θ, θM AP (cid:1), see deﬁnition(8.30) . In making such an approximation, potentially useful information concern-
ing the reliability of the parameter estimate is lost. In contrast the full posterior reﬂects our beliefs about
the range of possibilities and their associated credibilities.

The term ‘maximum likelihood’ refers to the parameter θ for which the observed data is most likely to be
generated by the model. One can motivate MAP from a decision theoretic perspective. If we assume a
utility that is zero for all but the correct θ,

U (θtrue, θ) = I [θtrue = θ]

then the expected utility of θ is

U (θ) =

(cid:88)

θtrue

I [θtrue = θ] p(θtrue

) = p(θ

)

|X

|X

(8.7.1)

(8.7.2)

This means that the maximum utility decision is to return that θ with the highest posterior value.

178

DRAFT April 24, 2015

Properties of Maximum Likelihood

When a ‘ﬂat’ prior p(θ) = const. is used the MAP parameter assignment is equivalent to the maximum
likelihood setting

θM L = argmax

θ

θ)

p(

X |

Since the logarithm is a strictly increasing function, then for a positive function f (θ)

θopt = argmax

θ

f (θ)

⇔

θopt = argmax

θ

log f (θ)

(8.7.3)

(8.7.4)

so that the MAP parameters can be found either by optimising the MAP objective or, equivalently, its
logarithm,

log p(θ

|X

) = log p(

X |

θ) + log p(θ)

log p(

)

X

−

(8.7.5)

where the normalisation constant, p(
the i.i.d. assumption, it is a summation of data terms,

X

), is not a function of θ. The log likelihood is convenient since, under

log p(θ

) =

|X

(cid:88)

n

log p(xn

|

θ) + log p(θ)

log p(

)

X

−

(8.7.6)

so that quantities such as derivatives of the log-likelihood w.r.t. θ are straightforward to compute.

8.7.1 Training assuming the correct model class

=

θ0). Our
Consider a dataset
|
θ0) and examine if,
interest is to ﬁt a model p(x
|
in the limit of a large amount of data, the parameter θ learned by maximum likelihood matches the correct
parameter θ0. Our derivation below is non-rigorous, but highlights the essence of the argument.

θ) of the same form as the correct underlying model p(x

generated from an underlying parametric model p(x

xn, n = 1, . . . , N

X

{

}

|

Assuming the data is i.i.d., the scaled log likelihood is

L(θ)

≡

1
N

log p(

X |

θ) =

1
N

N
(cid:88)

n=1

log p(xn

θ)
|

(8.7.7)

In the limit N
generating the data

→ ∞

, the sample average can be replaced by an average with respect to the distribution

L(θ) =N →∞

log p(x

θ)
|

(cid:105)p(x|θ0) =

(cid:104)

KL(cid:0)p(x

−

θ0)
p(x
|
|

|

θ)(cid:1) + (cid:10)log p(x

θ0)(cid:11)
|

p(x|θ0)

(8.7.8)

Up to a negligible constant, this is the Kullback-Leibler divergence between two distributions in x, just
with diﬀerent parameter settings. The θ that maximises L(θ) is that which minimises the Kullback-Leibler
divergence, namely θ = θ0. In the limit of a large amount of data we can therefore, in principle, learn the
correct parameters (assuming we know the correct model class). That is, maximum likelihood is a consistent
estimator.

8.7.2 Training when the assumed model is incorrect

θ) for the assumed model, and p(x

We write q(x
φ) for the correct generating model. Repeating the above
|
calculations in the case of the assumed model being correct, we have that, in the limit of a large amount of
data, the scaled log likelihood is

|

L(θ) =

log q(x
(cid:104)

θ)
|

(cid:105)p(x|φ) =

KL(p(x
|

−

q(x
φ)
|

θ)) +
|

log p(x
(cid:104)

φ)
|

(cid:105)p(x|φ)

(8.7.9)

Since q and p are not of the same form, setting θ to φ does not necessarily minimise KL(p(x
therefore does not necessarily optimize L(θ).

q(x
φ)
|

θ)), and
|

|

DRAFT April 24, 2015

179

8.7.3 Maximum likelihood and the empirical distribution

Given a dataset of discrete variables

X

= (cid:8)x1, . . . , xN (cid:9) we deﬁne the empirical distribution as

Learning a Gaussian

q(x) =

1
N

N
(cid:88)

n=1

I [x = xn]

in the case that x is a vector of variables,

I [x = xn] =

I [xi = xn
i ]

(cid:89)

i

(8.7.10)

(8.7.11)

The Kullback-Leibler divergence between the empirical distribution q(x) and a distribution p(x) is

KL(q

p) =
|

log q(x)
(cid:104)

(cid:105)q(x) − (cid:104)

log p(x)

(cid:105)q(x)

Our interest is the functional dependence of KL(q
dent of p(x) we may consider this constant and focus on the second term alone. Hence

p) on p. Since the entropic term
|

log q(x)
(cid:104)

KL(q

p) =
|

− (cid:104)

log p(x)

(cid:105)q(x) + const. =

1
N

−

N
(cid:88)

n=1

log p(xn) + const.

(8.7.12)

(cid:105)q(x) is indepen-

(8.7.13)

We recognise (cid:80)N
n=1 log p(xn) as the log likelihood under the model p(x), assuming that the data is i.i.d. This
means that setting parameters by maximum likelihood is equivalent to setting parameters by minimising the
Kullback-Leibler divergence between the empirical distribution and the parameterised distribution. In the
case that p(x) is unconstrained, the optimal choice is to set p(x) = q(x), namely the maximum likelihood
optimal distribution corresponds to the empirical distribution.

8.8 Learning a Gaussian

Given the importance of the Gaussian distribution, it is instructive to explicitly consider maximum likelihood
and Bayesian methods for ﬁtting a Gaussian to data.

8.8.1 Maximum likelihood training

(x µ, Σ) with unknown mean µ
Given a set of training data
and covariance Σ, how can we ﬁnd these parameters? Assuming the data are drawn i.i.d. the log likelihood
is

= (cid:8)x1, . . . , xN (cid:9), drawn from a Gaussian

N

X

L(µ, Σ)

N
(cid:88)

n=1

≡

log p(x

µ, Σ) =
|

−

1
2

N
(cid:88)

n=1

(xn

−

µ)T Σ−1 (xn

µ)

−

−

N
2

log det (2πΣ)

(8.8.1)

Optimal µ

Taking the partial derivative with respect to the vector µ we obtain the vector derivative

µL(µ, Σ) =

∇

N
(cid:88)

n=1

Σ−1 (xn

µ)

−

Equating to zero gives that at the optimum of the log likelihood,

N
(cid:88)

n=1

Σ−1xn = N µΣ−1

and therefore, optimally µ is given by the sample mean

µ =

1
N

N
(cid:88)

n=1

xn

180

(8.8.2)

(8.8.3)

(8.8.4)

DRAFT April 24, 2015

Learning a Gaussian

(a) Prior

(b) Posterior

Figure 8.8: Bayesian approach to inferring the mean and precision (inverse variance) of a Gaussian based
(a): A Gauss-Gamma prior with µ0 = 0, α = 2, β = 1, γ = 1.
on N = 10 randomly drawn datapoints.
(b): Gauss-Gamma posterior conditional on the data. For comparison, the sample mean of the data is 1.87
and maximum likelihood optimal variance is 1.16 (computed using the N normalisation). The 10 datapoints
were drawn from a Gaussian with mean 2 and variance 1. See demoGaussBayes.m.

Optimal Σ

The derivative of L with respect to the matrix Σ requires more work.
dependence on the covariance, and also parameterise using the inverse covariance, Σ−1,

It is convenient to isolate the

L =

1
2

−

trace









Σ−1

N
(cid:88)

n=1
(cid:124)



µ)T






(cid:125)

(xn

−

µ) (xn

−

(cid:123)(cid:122)
≡M

+

N
2

log det (cid:0)2πΣ−1(cid:1)

Using M = MT, we obtain

∂

∂Σ−1 L =

1
2

−

M +

N
2

Σ

Equating the derivative to the zero matrix and solving for Σ gives the sample covariance

Σ =

1
N

N
(cid:88)

n=1

(xn

−

µ) (xn

−

µ)T

(8.8.5)

(8.8.6)

(8.8.7)

Equations (8.8.4) and (8.8.7) deﬁne the maximum likelihood solution mean and covariance for training data
. Consistent with our previous results, in fact these equations simply set the parameters to their sample
X
statistics of the empirical distribution. That is, the mean is set to the sample mean of the data and the
covariance to the sample covariance.

8.8.2 Bayesian inference of the mean and variance

For simplicity we here deal only with the univariate case. Assuming i.i.d. data the likelihood is

µ, σ2) =

p(

X |

1
(2πσ2)N/2 exp

(cid:32)

−

1
2σ2

N
(cid:88)

n=1

(cid:33)

(xn

µ)2

−

For a Bayesian treatment, we require the posterior of the parameters

(8.8.8)

)

p(

p(µ, σ2

σ2)p(σ2)
|
Our aim is to ﬁnd conjugate priors for the mean and variance. A convenient choice for a prior on the mean
µ is that it is a Gaussian centred on µ0:

µ, σ2)p(µ, σ2) = p(

µ, σ2)p(µ

(8.8.9)

X |

X |

|X

∝

µ0, σ2

0) =

p(µ

|

(cid:112)

1
2πσ2
0

exp

(cid:18)

−

1
2σ2
0

(µ0

−

(cid:19)

µ)2

DRAFT April 24, 2015

(8.8.10)

181

The posterior is then

p(µ, σ2

)

|X

∝

1
σ0

1
σN exp

(cid:32)

−

1
2σ2
0

(µ0

−

µ)2

1
2σ2

−

(cid:88)

(xn

n

−

(cid:33)

µ)2

p(σ2)

It is convenient to write this in the form

p(µ, σ2

) = p(µ

σ2,
|

X

)p(σ2

)

|X

|X

Learning a Gaussian

(8.8.11)

(8.8.12)

Since equation (8.8.11) has quadratic contributions in µ in the exponent, the conditional posterior p(µ
is Gaussian. To identify this Gaussian we multiply out the terms in the exponent to arrive at

σ2,
|

X

)

exp

(cid:18)

−

(cid:0)aµ2

1
2

−

(cid:19)

2bµ + c(cid:1)

with

a =

1
σ2
0

+

N
σ2 , b =

µ0
σ2
0

+

(cid:80)

n xn
σ2

, c =

µ2
0
σ2
0

(cid:88)

+

n

(xn)2
σ2

Using the identity

aµ2

−

(cid:18)

2bµ + c = a

µ

b
a

−

(cid:19)2

(cid:18)
c

+

(cid:19)

b2
a

−

we can write

(cid:32)

p(µ, σ2

)

|X

∝

√aexp

(cid:124)

(cid:18)

−

µ

a

1
2
(cid:123)(cid:122)
p(µ|X ,σ2)

(cid:19)2(cid:33)

b
a

−

(cid:125)

exp

(cid:18)

−

1
2

1
√a
(cid:124)

(cid:18)
c

b2
a

(cid:19)(cid:19) 1
σ0

−
(cid:123)(cid:122)
p(σ2|X )

(8.8.13)

(8.8.14)

(8.8.15)

(8.8.16)

1
σN p(σ2)
(cid:125)

We encounter a diﬃculty in attempting to ﬁnd a conjugate prior for σ2 because the term b2/a is not a simple
expression of σ2. For this reason we constrain

σ2
0 ≡

γσ2

for some ﬁxed hyperparameter γ. Deﬁning the constants

˜a =

1
γ

+ N, ˜b =

µ0
γ

(cid:88)

+

xn, ˜c =

n

µ2
0
γ

(cid:88)

+

(xn)2

n

we have

b2
a

=

1
σ2

c

−

(cid:32)

˜c

−

(cid:33)

˜b2
˜a

Using this expression in equation (8.8.16) we obtain

p(σ2

)

|X

∝

(cid:0)σ2(cid:1)−N/2

exp

(cid:32)

−

(cid:32)

˜b2
˜a

˜c

−

1
2σ2

(cid:33)(cid:33)

p(σ2)

(8.8.17)

(8.8.18)

(8.8.19)

(8.8.20)

An inverse Gamma distribution for the prior p(σ2) is therefore conjugate. For a Gauss-Inverse-Gamma
prior:

p(µ, σ2) =

N

(cid:0)µ µ0, γσ2(cid:1) InvGam (cid:0)σ2

α, β(cid:1)
|

the posterior is also Gauss-Inverse-Gamma with

p(µ, σ2

) =

|X

N

(cid:32)

µ

(cid:33)

˜b
˜a

,

σ2
˜a

InvGam

(cid:32)

α +

σ2

|

N
2

, β +

1
2

(cid:32)

˜c

−

˜b2
˜a

(cid:33)(cid:33)

(8.8.21)

(8.8.22)

182

DRAFT April 24, 2015

Learning a Gaussian

8.8.3 Gauss-Gamma distribution

It is common to use a prior on the precision, deﬁned as the inverse variance

1
σ2

λ

≡

If we then use a Gamma prior

p(λ

α, β) = Gam (λ
|

α, β) =
|

1
βαΓ(α)

λα−1e−λ/β

the posterior will be

p(λ

|X

, α, β) = Gam

(cid:16)

λ

(cid:17)
α + N/2, ˜β
|

where

1
˜β

=

1
β

+

1
2

(cid:32)

˜c

−

(cid:33)

˜b2
˜a

The Gauss-Gamma prior distribution

µ0, α, β, γ) =
p(µ, λ
|

N

(cid:0)µ µ0, γλ−1(cid:1) Gam (λ
|

α, β)

(8.8.23)

(8.8.24)

(8.8.25)

(8.8.26)

(8.8.27)

is therefore the conjugate prior for a Gaussian with unknown mean µ and precision λ. The posterior for
this prior is a Gauss-Gamma distribution with parameters

p(µ, λ

|X

, µ0, α, β, γ) =

(cid:32)

µ

(cid:33)

˜b
˜a

,

1
˜aλ

N

Gam

(cid:16)

λ

(cid:17)
α + N/2, ˜β
|

(8.8.28)

The marginal p(µ
is given in ﬁg(8.8). The maximum likelihood solution is recovered in the limit of a ‘ﬂat’ prior µ0 = 0, γ

, µ0, α, β, γ) is a Student’s t-distribution. An example of a Gauss-Gamma prior/posterior

→
, see exercise(8.23). The unbiased estimators for the mean and variance are given using

, α = 1/2, β

|X

∞
→ ∞
the prior µ0 = 0, γ

, α = 1, β

→ ∞

, exercise(8.24).

→ ∞

For the multivariate case, the extension of these techniques uses a multivariate Gaussian distribution for
the conjugate prior on the mean, and an Inverse Wishart distribution for the conjugate prior on the
covariance[136].

8.9 Summary

Classical univariate distributions include the exponential, Gamma, Beta, Gaussian and Poisson.

A classical distribution of distributions is the Dirichlet distribution.

Multivariate distributions are often diﬃcult to deal with computationally. A special case is the multivariate
Gaussian, for which marginals and normalisation constants can be computed in time cubic in the number
of variables in the model.

A useful measure of the diﬀerence between distributions is the Kullback-Leibler divergence.

Bayes’ rule enables us to achieve parameter learning by translating a priori parameter belief into a posterior
parameter belief based on observed data.

Bayes’ rule itself says nothing about how best to summarise the posterior distribution.

Conjugate distributions are such that the prior and posterior are from the same distribution, just with
diﬀerent parameters.

Maximum likelihood corresponds to a simple summarisation of the posterior under a ﬂat prior.

•

•

•

•

•

•

•

•

DRAFT April 24, 2015

183

•

Provided that we are using the correct model class, maximum likelihood will learn the optimal parameters
in the limit of a large amount of data – otherwise there is no such guarantee.

Exercises

8.10 Code

demoGaussBayes.m: Bayesian ﬁtting of a univariate Gaussian
logGaussGamma.m: Plotting routine for a Gauss-Gamma distribution

8.11 Exercises

Exercise 8.1. In a public lecture, the following phrase was uttered by a Professor of Experimental Psy-
chology: ‘In a recent data survey, 90% of people claim to have above average intelligence, which is clearly
nonsense!’
[Audience Laughs]. Is it theoretically possible for 90% of people to have above average intelli-
gence? If so, give an example, otherwise explain why not. What about above median intelligence?

Exercise 8.2. Consider the distribution deﬁned on real variables x, y:

p(x, y)

∝

(x2 + y2)2e−x2−y2

,

dom(x) = dom(y) =

{−∞

. . .

∞}

Show that
are uncorrelated, show that they are nevertheless dependent.

= 0. Furthermore show that x and y are uncorrelated,

=

x

y

(cid:104)

(cid:104)

(cid:105)

(cid:105)

(8.11.1)

xy
(cid:104)

(cid:105)

=

x
(cid:104)

y
(cid:105) (cid:104)

(cid:105)

. Whilst x and y

Exercise 8.3. For a variable x with dom(x) =
0, 1
{
x1, . . . , xn from this distribution, the probability of observing k states 1 is the Binomial distribution

, and p(x = 1) = θ, show that in n independent draws
}

(cid:19)

θk (1

(cid:18)n
k

−

θ)n−k

(8.11.2)

Exercise 8.4 (Normalisation constant of a Gaussian). The normalisation constant of a Gaussian distribu-
tion is related to the integral

I =

(cid:90) ∞

−∞

e− 1

2 x2

dx

By considering

I 2 =

(cid:90) ∞

−∞

e− 1

2 x2

dx

(cid:90) ∞

−∞

e− 1

2 y2

dy =

(cid:90) ∞

(cid:90) ∞

−∞

−∞

e− 1

2 (x2+y2)dxdy

and transforming to polar coordinates,

x = r cos θ, y = r sin θ, dxdy

rdrdθ, r = 0, . . . ,

, θ = 0, . . . , 2π

∞

→

show that

(8.11.3)

(8.11.4)

1. I = √2π
−∞ e− 1

2. (cid:82) ∞

2σ2 (x−µ)2

dx = √2πσ2

Exercise 8.5. For a univariate Gaussian distribution, show that

x

(cid:105)N (x µ,σ2)
µ)2(cid:69)
(cid:68)

(x

−

N (x µ,σ2)

1. µ =

(cid:104)

2. σ2 =

184

DRAFT April 24, 2015

Exercises

Exercise 8.6. Using

xTAx = trace

(cid:16)

AxxT(cid:17)

derive result(8.5).

Exercise 8.7. Show that the marginal of a Dirichlet distribution is another Dirichlet distribution:

(cid:90)

θj

Dirichlet (θ

u) = Dirichlet (cid:0)θ\j|

|

u\j

(cid:1)

Exercise 8.8. For a Beta distribution, show that

(cid:68)

xk(cid:69)

=

B(α + k, β)
B(α, β)

=

(α + k
(α + β + k

−
−

where we used Γ(x + 1) = xΓ(x).

1)(α + k
1)(α + β + k) . . . (α + β)

2) . . . (α)

−

Exercise 8.9. For the moment generating function g(t)

(cid:10)etx(cid:11)

≡

p(x), show that

dk
dtk g(t) =

(cid:68)

lim
t→0

xk(cid:69)

p(x)

(8.11.5)

(8.11.6)

(8.11.7)

(8.11.8)

Exercise 8.10 (Change of variables). Consider a one dimensional continuous random variable x with
corresponding p(x). For a variable y = f (x), where f (x) is a monotonic function, show that the distribution
of y is

p(y) = p(x)

(cid:19)−1

(cid:18) df
dx

, x = f −1(y)

More generally, for vector variables, and y = f (x), we have:

(cid:12)
(cid:12)
p(y) = p(x = f −1(y))
(cid:12)
(cid:12)

det

(cid:18) ∂f
∂x

(cid:19)(cid:12)
−1
(cid:12)
(cid:12)
(cid:12)

where the Jacobian matrix has elements

(cid:20) ∂f
∂x

(cid:21)

=

ij

∂fi(x)
∂xj

Exercise 8.11 (Normalisation of a Multivariate Gaussian). Consider

(cid:90) ∞

(cid:18)

I =

exp

−∞

−

1
2

(x

−

µ)T Σ−1 (x

(cid:19)

µ)

dx

−

By using the transformation

z = Σ− 1

2 (x

µ)

−

show that

I =

(cid:112)

det (2πΣ)

Exercise 8.12. Consider the partitioned matrix

M =

(cid:19)

(cid:18) A B
C D

(8.11.9)

(8.11.10)

(8.11.11)

(8.11.12)

(8.11.13)

(8.11.14)

(8.11.15)

for which we wish to ﬁnd the inverse M−1. We assume that A is m
invertible. By deﬁnition, the partitioned inverse

×

m and invertible, and D is n

n and

×

M−1 =

(cid:19)

(cid:18) P Q
R S

DRAFT April 24, 2015

(8.11.16)

185

must satisfy

(cid:18) A B
C D

(cid:19) (cid:18) P Q
R S

(cid:19)

(cid:18) Im 0
In
0

=

(cid:19)

Exercises

(8.11.17)

where Im is the m
derive the results

×

m identity matrix, and 0 the zero matrix of the same dimension as D. Using the above,

P = (cid:0)A
R =

−

BD−1C(cid:1)−1

Q =

A−1B (cid:0)D

CA−1B(cid:1)−1

−
D−1C (cid:0)A

−
BD−1C(cid:1)−1 S = (cid:0)D

CA−1B(cid:1)−1

−

−

−

(8.11.18)

Exercise 8.13. Show that for Gaussian distribution p(x) =
zero.

N

(cid:0)x µ, σ2(cid:1) the skewness and kurtosis are both

Exercise 8.14. Consider a small interval of time δt and let the probability of an event occurring in this
small interval be θδt. Derive a distribution that expresses the probability of at least one event in an interval
from 0 to t.

Exercise 8.15. Consider a vector variable x = (x1, . . . , xn)T and set of functions deﬁned on each component
of x, φi(xi). For example for x = (x1, x2)T we might have

φ1(x1) =

x1

− |

|

, φ2(x2) =

x2
2

−

Consider the distribution

p(x

θ) =
|

1
Z

(cid:16)

exp

θTφ(x)

(cid:17)

(8.11.19)

(8.11.20)

where φ(x) is a vector function with ith component φi(xi), and θ is a parameter vector. Each component is
tractably integrable in the sense that

(cid:90) ∞

−∞

exp (θiφi(xi))dxi

can be computed either analytically or to an acceptable numerical accuracy. Show that

1. xi

xj.

⊥⊥

2. The normalisation constant Z can be tractably computed.

3. Consider the transformation

x = My

(8.11.21)

(8.11.22)

for an invertible matrix M. Show that the distribution p(y
stant is known), and that, in general, yi
multivariate distributions.

(cid:62)(cid:62)

M, θ) is tractable (its normalisation con-
|
yj. Explain the signiﬁcance of this in deriving tractable

Exercise 8.16. Show that we may reparameterise the Beta distribution, deﬁnition(8.23) by writing the
parameters α and β as functions of the mean m and variance s using

α = βγ,

γ

≡

m/(1

m)

−

β =

(cid:18)

1
1 + γ

γ
s (1 + γ)2 −

(cid:19)
1

Exercise 8.17. Consider the function

f (γ + α, β, θ)

θγ+α−1 (1

θ)β−1

−

≡

186

(8.11.23)

(8.11.24)

(8.11.25)

DRAFT April 24, 2015

Exercises

show that

lim
γ→0

∂
∂γ

f (γ + α, β, θ) = θα−1 (1

θ)β−1 log θ

−

(8.11.26)

and hence that
(cid:90)

θα−1 (1

θ)β−1 log θdθ = lim
γ→0

−

(cid:90)

∂
∂γ

f (γ + α, β, θ)dθ =

(cid:90)

∂
∂α

f (α, β, θ)dθ

(8.11.27)

Using this result, show therefore that

log θ

(cid:105)B(θ|α,β) =

(cid:104)

∂
∂α

log B(α, β)

where B(α, β) is the Beta function. Show additionally that

log (1

(cid:104)

θ)

(cid:105)B(θ|α,β) =

−

∂
∂β

log B(α, β)

Using the fact that

B(α, β) =

Γ(α)Γ(β)
Γ(α + β)

where Γ(x) is the gamma function, relate the above averages to the digamma function, deﬁned as

ψ(x) =

d
dx

log Γ(x)

(8.11.28)

(8.11.29)

(8.11.30)

(8.11.31)

Exercise 8.18. Using a similar ‘generating function’ approach as in exercise(8.17), explain how to compute

log θi

(cid:104)

(cid:105)Dirichlet(θ|u)

Exercise 8.19. Consider the function

(cid:90) ∞

f (x) =

δ

0

(cid:32) n
(cid:88)

i=1

(cid:33)

(cid:89)

i

θi

x

−

θui−1
i

dθ1 . . . dθn

Show that the Laplace transform ˜f (s)

˜f (s) =

n
(cid:89)

i=1

(cid:26)(cid:90) ∞

0

(cid:27)

e−sθiθui−1
i

dθi

≡

=

(cid:82) ∞
0 e−sxf (x)dx is
n
(cid:89)

Γ (ui)

1
(cid:80)
i ui
s

i=1

By using that the inverse Laplace transform of 1/s1+q is xq/Γ(1 + q), show that

f (x) =

(cid:81)n
i=1 Γ (ui)
Γ ((cid:80)
i ui)

(cid:80)

i ui

x

(8.11.32)

(8.11.33)

(8.11.34)

(8.11.35)

Hence show that the normalisation constant of a Dirichlet distribution with parameters u is given by

(cid:81)n
i=1 Γ (ui)
Γ ((cid:80)
i ui)

Exercise 8.20. Derive the formula for the diﬀerential entropy of a multi-variate Gaussian.

Exercise 8.21. Show that for a gamma distribution Gam (x

α, β) the mode is given by
|

x∗ = (α

−
provided that α

1) β

1.

≥

Exercise 8.22. Consider a distribution p(x

θ) and a distribution p(x

θ + δ) for small δ.

|

|

DRAFT April 24, 2015

(8.11.36)

(8.11.37)

187

1. Take the Taylor expansion of

KL(p(x

p(x
θ)
|
|

θ + δ))

|
for small δ and show that this is equal to

δ2
2

−

(cid:28) ∂2

∂θ2 log p(x

(cid:29)

θ)
|

p(x|θ)

Exercises

(8.11.38)

(8.11.39)

2. More generally for a distribution parameterised by a vector with elements θi + δi, show that a small

change in the parameter results in

(cid:88)

i,j

δiδj
2

Fij

where the Fisher Information matrix is deﬁned as

Fij =

(cid:28) ∂2

−

∂θi∂θj

(cid:29)

log p(x

θ)
|

p(x|θ)

(8.11.40)

(8.11.41)

3. Show that the Fisher information matrix is positive semideﬁnite by expressing it equivalently as

Fij =

(cid:28) ∂
∂θi

log p(x

∂
∂θj

θ)
|

log p(x

θ)
|

(cid:29)

p(x|θ)

Exercise 8.23. Consider the joint prior distribution

(8.11.42)

(cid:0)µ µ0, γλ−1(cid:1) Gam (λ

µ0, α, β, γ) =

p(µ, λ
|
, then the prior distribution becomes ‘ﬂat’ (independent of µ and λ)
Show that for µ0 = 0, γ
for α = 1/2. Show that for these settings the mean and variance that jointly maximise the posterior equation
(8.8.28) are given by the standard maximum likelihood settings

α, β)
|

(8.11.43)

→ ∞

→ ∞

, β

N

µ∗ =

1
N

(cid:88)

xn,

n

σ2
∗ =

1
N

(cid:88)

(xn

n

µ∗)2

−

(8.11.44)

Exercise 8.24. Show that for equation (8.8.28) in the limit µ0 = 0, γ
optimal mean and variance obtained from

, α = 1, β

→ ∞

, the jointly

→ ∞

argmax
µ,λ

p(µ, λ

|X

, µ0,α, β, γ)

is given by

µ∗ =

1
N

(cid:88)

xn,

n

σ2
∗ =

1
N + 1

(cid:88)

(xn

n

µ∗)2

−

(8.11.45)

(8.11.46)

where σ2

∗ = 1/λ∗. Note that these correspond to the standard ‘unbiased’ estimators of the mean and variance.
) given in equation (8.8.28) compute the

Exercise 8.25. For the Gauss-Gamma posterior p(µ, λ
µ0, α, β,
|
marginal posterior p(µ
µ0, α, β,
|

). What is the mean of this distribution?

X

X

Exercise 8.26. This exercise concerns the derivation of equation (8.4.15).

1. By considering
(cid:90)

p(y) =

p(y

(cid:90)

∝

show that

x)p(x)dx
|
(cid:18)

exp

−

Mx

(y

−

−

1
2

µ)T Σ−1 (y

Mx

µ)

−

−

−

1
2

(x

−

µx)T Σ−1

x (x

(cid:19)

µx)

dx

−

(8.11.47)

(cid:18)

p(y)

exp

∝

−

yTΣ−1y

1
2

(cid:19) (cid:90)

exp

(cid:18)

1
2

−

xTAx + xT (By + c)

(cid:19)

for suitably deﬁned A, B, c.

188

(8.11.48)

(8.11.49)

DRAFT April 24, 2015

Exercises

2. Using equation (8.4.13), show that

(cid:18)

p(y)

exp

∝

−

yTΣ−1y

1
2

1
2

−

(By + c)T A−1 (By + c)

(cid:19)

3. This establishes that p(y) is Gaussian. We now need to ﬁnd the mean and covariance of this Gaussian.

We can do this by the lengthly process of completing the square. Alternatively, we can appeal to

y
(cid:104)

= M

x
(cid:104)

(cid:105)

+

η
(cid:104)

(cid:105)

= Mµx + µ

(cid:105)
By considering

(cid:68)

(y

y

) (y
(cid:105)

− (cid:104)

− (cid:104)

y

)T(cid:69)
(cid:105)

=

(cid:68)

(Mx + η

Mµx −

−

µ) (Mx + η

Mµx −

−

µ)T(cid:69)

(8.11.50)

and the independence of x and η, derive the formula for the covariance of p(y).

Exercise 8.27. Consider the multivariate Gaussian distribution p(x)
components x1, . . . , xn:

∼ N

(x µ, Σ) on the vector x with

p(x) =

1
det (2πΣ)

(cid:112)

e− 1

2 (x−µ)TΣ−1(x−µ)

(8.11.51)

Calculate p(xi

x1, . . . , xi−1, xi+1, . . . , xn). Hint: make use of equation (8.4.19).
|

Exercise 8.28. Observations y0, . . . , yn−1 are noisy i.i.d. measurements of an underlying variable x with
y0, . . . , yn−1) is Gaussian
p(x)
|
∼ N
with mean

(cid:0)yi x, σ2(cid:1) for i = 0, . . . , n

1. Show that p(x

(cid:1) and p(yi

(cid:0)x 0, σ2
0

∼ N

x)

−

|

µ =

nσ2
0
0 + σ2 y

nσ2

where y = (y0 + y1 + . . . + yn−1)/n and variance σ2

n such that

1
σ2
n

=

n
σ2 +

1
σ2
0

.

(8.11.52)

(8.11.53)

= x1, . . . , xN where each xn is independently drawn from a
Exercise 8.29. Consider a set of data
Gaussian with known mean µ and unknown variance σ2. Assume a gamma distribution prior on τ = 1/σ2,

X

p(τ ) = Gamis (τ

a, b)

|
1. Show that the posterior distribution is

) = Gamis

p(τ

|X

(cid:32)
τ

a +

|

N
2

, b +

1
2

N
(cid:88)

n=1

(cid:33)

(xn

µ)2

−

2. Show that the distribution for x is

(cid:90)

(cid:18)

p(x

|X

) =

p(x

τ )p(τ
|

|X

)dτ = Student

x

µ, λ =
|

(cid:19)

a(cid:48)
b(cid:48) , ν = 2a(cid:48)

where a(cid:48) = a + 1

2 N , b(cid:48) = b + 1

2

(cid:80)N

n=1 (xn

µ)2.

−

(8.11.54)

(8.11.55)

(8.11.56)

Exercise 8.30. The Poisson distribution is a discrete distribution on the non-negative integers, with

p(x) =

e−λλx
x!

x = 0, 1, 2, . . .

(8.11.57)

You are given a sample of n observations x1, . . . , xn independently drawn from this distribution. Determine
the maximum likelihood estimator of the Poisson parameter λ.

DRAFT April 24, 2015

189

Exercise 8.31. For a Gaussian mixture model

p(x) =

(cid:88)

i

pi

N

(x µi, Σi) ,

pi > 0,

(cid:88)

i

pi = 1

show that p(x) has mean

=

x
(cid:104)

(cid:105)

(cid:88)

i

piµi

and covariance

(cid:88)

(cid:16)

pi

Σi + µiµT
i

i

(cid:17)

(cid:88)

−

i

piµi

(cid:88)

pjµT
j

j

Exercises

(8.11.58)

(8.11.59)

(8.11.60)

Exercise 8.32. Show that for the whitened data matrix, given in equation (8.4.30), ZZT = N I.

Exercise 8.33. Consider a uniform distribution pi = 1/N deﬁned on states i = 1, . . . , N . Show that the
entropy of this distribution is

H =

N
(cid:88)

−

i=1

pi log pi = log N

(8.11.61)

and that therefore as the number of states N increases to inﬁnity, the entropy diverges to inﬁnity.

Exercise 8.34. Consider a continuous distribution p(x), x
[0, 1]. We can form a discrete approximation
with probabilities pi to this continuous distribution by identifying a continuous value i/N for each state
i = 1, . . . , N . With this

∈

pi =

p(i/N )
i p(i/N )

(cid:80)

show that the entropy H =

(cid:80)

i pi log pi is given by

−

H =

1
i p(i/N )

(cid:80)

−

(cid:88)

i

p(i/N ) log p(i/N ) + log

(cid:88)

i

p(i/N )

Since for a continuous distribution

(cid:90) 1

0

p(x)dx = 1

a discrete approximation of this integral into bins of size 1/N gives

1
N

N
(cid:88)

i=1

p(i/N ) = 1

Hence show that for large N ,

(cid:90) 1

H

≈ −

0

p(x) log p(x)dx + const.

(8.11.62)

(8.11.63)

(8.11.64)

(8.11.65)

(8.11.66)

where the constant tends to inﬁnity as N
. Note that this result says that as a continuous distribu-
tion has essentially an inﬁnite number of states, the amount of uncertainty in the distribution is inﬁnite
(alternatively, we would need an inﬁnite number of bits to specify a continuous value). This motivates the
deﬁnition of the diﬀerential entropy, which neglects the inﬁnite constant of the limiting case of the discrete
entropy.

→ ∞

Exercise 8.35. Consider two multivariate Gaussians

(x µ1, Σ1) and

(x µ2, Σ2).

N

N

190

DRAFT April 24, 2015

Exercises

1. Show that the log product of the two Gaussians is given by

1
2

−

xT (cid:0)Σ−1

1 + Σ−1
2

(cid:1) x+xT (cid:0)Σ−1

1 µ1 + Σ−1

2 µ2

(cid:16)

(cid:1)

1
2

−

µT

1 Σ−1

1 µ1 + µT

2 Σ−1

2 µ2

(cid:17)

1
2

−

log det (2πΣ1) det (2πΣ2)

2. Deﬁning A = Σ−1

1 + Σ−1

2 and b = Σ−1

1 µ1 + Σ−1

2 µ2 we can write the above as

1
2

−

(cid:0)x

−

A−1b(cid:1)T

A (cid:0)x

A−1b(cid:1)+

1
2

−

bTA−1b

(cid:16)

1
2

−

µT

1 Σ−1

1 µ1 + µT

2 Σ−1

2 µ2

(cid:17)

1
2

−

log det (2πΣ1) det (2πΣ2)

Writing Σ = A−1 and µ = A−1b show that the product of two Gaussians is a Gaussian with covariance

Σ = Σ1 (Σ1 + Σ2)−1 Σ2

mean

µ = Σ1 (Σ1 + Σ2)−1 µ2 + Σ2 (Σ1 + Σ2)−1 µ1

(8.11.67)

(8.11.68)

and log prefactor

bTA−1b

1
2

(cid:16)

1
2

−

µT

1 Σ−1

1 µ1 + µT

2 Σ−1

2 µ2

(cid:17)

1
2

−

log det (2πΣ1) det (2πΣ2) +

1
2

log det (2πΣ)

3. Show that this can be written as

(x µ1, Σ1)

N

N

(x µ2, Σ2) =

N

(x µ, Σ)

(cid:16)

exp

−

where S = Σ1 + Σ2.

Exercise 8.36. Show that

1

2 (µ1 −
(cid:112)

µ2)T S−1 (µ1 −
det (2πS)

(cid:17)

µ2)

(8.11.69)

∂
∂θ (cid:104)

log p(x

θ)
|
Exercise 8.37. Using (cid:10)f 2(x)(cid:11)

(cid:105)p(x|θ0) |θ=θ0 = 0

f (x)

2
(cid:105)

≥

− (cid:104)

0, and a suitably chosen function f (x), show that

(cid:29)

(cid:28) p(x)
q(x)

1

p(x) ≥

(8.11.70)

(8.11.71)

for distributions q(x) and p(x). This is related to the α = 2 divergence.

Exercise 8.38. Show that for any α, Dα(p

p) = 0 and that Dα(p
|

q)
|

≥

0 for any distributions p(x), q(x).

Exercise 8.39. Show that for two D dimensional Gaussians,

2KL(

N

(x µ1, Σ1)

|N

(x µ2, Σ2)) = trace (cid:0)Σ−1

2 Σ1

(cid:1) + (µ1 −

µ2)T Σ−1
2

(µ1 −

µ2) + log det (cid:0)Σ2Σ−1

1

(cid:1)

D

−

Exercise 8.40. For data pairs (xn, yn), n = 1, . . . , N , the correlation can be considered a measure of the
extent to which a linear relationship holds between x and y. For simplicity, we consider that both x and y
have zero mean, and wish to consider the validity of the linear relation

x = αy

A measure of the discrepancy of this linear assumption is given by

E(α)

N
(cid:88)

n=1

≡

(xn

−

αyn)2

DRAFT April 24, 2015

(8.11.72)

(8.11.73)

191

1. Show that the α that minimises E(α) is given by

α∗ =

c
σ2
y

where

c =

1
N

(cid:88)

xnyn,

n

σ2
y =

1
N

(cid:88)

(yn)2

n

2. A measure of the ‘linearity’ between x and y is then

E(α∗)
E(0)

Exercises

(8.11.74)

(8.11.75)

(8.11.76)

which is 1 when there is no linear relation (α = 0) and 0 when there is a perfect linear relation (since
then E(α∗) = 0). Show that the correlation coeﬃcient, deﬁnition(8.7), is given by

(cid:115)
1

ρ =

E(α∗)
E(0)

−

(8.11.77)

3. Deﬁning the vectors x = (x1, . . . , xN )T, y = (y1, . . . , yN )T, show that the correlation coeﬃcient is the

cosine of the angle between x and y,

ρ =

xTy
y
x
|
and hence show that

| |

|

1
−

≤

ρ

≤

1.

4. Show that for the more general relation

x = αy + γ

for constant oﬀset γ, setting γ to minimise

E(α, γ)

N
(cid:88)

n=1

≡

(xn

αyn

γ)2

−

−

(8.11.78)

(8.11.79)

(8.11.80)

has the eﬀect of simply replacing xn by xn
and y data respectively.

−

¯x and yn by yn

−

¯y, where ¯x and ¯y are the mean of the x

Exercise 8.41. For variables x, y, and z = x + y, show that the correlation coeﬃcients are related by
ρx,y. With reference to the correlation coeﬃcient as the angle between two vectors, explain why
ρx,z
ρx,y is geometrically obvious.
ρx,z

≥
≥

Exercise 8.42. Consider a ‘Boltzman machine’ distribution on binary variables xi

0, 1

∈ {

, i = 1, . . . , D
}

p(x

W) =
|

1
Zp(W)

(cid:16)

exp

xTWx

(cid:17)

and that we wish to ﬁt another distribution q of the same form to p

q(x

U) =
|

1
Zq(U)

(cid:16)

xTUx

(cid:17)

exp

1. Show that

argmin
U

KL(p

q) = argmax
|

U

trace (UC)

log Zq(U),

−

Cij

xixj

≡ (cid:104)

(cid:105)p

2. Hence show that knowing the ‘cross moment’ matrix C of p is suﬃcient to fully specify p.

(8.11.81)

(8.11.82)

(8.11.83)

192

DRAFT April 24, 2015

(z µ, Σ) deﬁned jointly over two vectors x and y of potentially

N

Exercises

3. Generalise the above result to all models in the exponential family.

++

Exercise 8.43. Consider a distribution
diﬀering dimensions,
(cid:18) x
y

z =

(cid:19)

with corresponding mean and partitioned covariance

µ =

(cid:19)

(cid:18) µx
µy

Σ =

(cid:18) Σxx Σxy
Σyx Σyy

(cid:19)

where Σyx

ΣT

xy.

≡

1. By considering p(x

y)

|

p(x

y)

|

∝

exp

1
2

−

where

∝
(cid:110)

p(x, y), show that

(x

µx)T P (x

−

µx) + 2 (x

−

−

µx)T Q (cid:0)y

(cid:1)(cid:111)

µy

−

(cid:18) Σxx Σxy
Σyx Σyy

(cid:19)−1

(cid:18) P Q
QT S

=

(cid:19)

2. By completing the square, show that

(8.11.84)

(8.11.85)

(8.11.86)

(8.11.87)

p(x

y)

|

∝

exp

(cid:110)(cid:0)x

1
2

−

−

µx + P−1Q (cid:0)y

(cid:1)(cid:1)T

P (cid:0)x

µy

−

−

µx + P−1Q (cid:0)y

(cid:1)(cid:1)(cid:111)

µy

−

(8.11.88)

3. Using partitioned matrix inversion, show that

ΣyxP + ΣyyQT = 0

and that therefore

P−1Q =

ΣxyΣ−1
yy

−

Show also that
P = (cid:0)Σxx

ΣxyΣ−1

yy Σyx

(cid:1)−1

−

4. Hence show that

p(x

y) =
|

N

(cid:0)x µx + ΣxyΣ−1

yy

(cid:0)y

(cid:1), Σxx

µy

−

−

ΣxyΣ−1

yy Σyx

(cid:1)

(8.11.89)

(8.11.90)

(8.11.91)

(8.11.92)

Exercise 8.44. As for the question above, consider the joint Gaussian distribution p(z). Consider

(cid:90)

p(x) =

p(x, y)dy =

(cid:90)

p(z)dy

1. Show that

(8.11.93)

p(x)

exp

∝

(cid:110)

(x

1
2

−

−

µx)T P (x

µx)

−

(cid:111) (cid:90)

exp

(cid:16)

2 (x

1
2

−

−

µx)T Q (cid:0)y

(cid:1) + (cid:0)y

µy

−

−

(cid:1)T S (cid:0)y

µy

−

(cid:1)(cid:17)

µy

dy

2. By completing the square, show that
µx)T Q (cid:0)y

(cid:1) + (cid:0)y

µy

−

2 (x
(cid:16)

=

−
y

µy + S−1QT (x

−

−

µy
(cid:17)T

(cid:1)T S (cid:0)y
(cid:16)

S

y

−
µx)

−

(cid:1)

µy

−
µy + S−1QT (x

(8.11.94)

(8.11.95)

(cid:17)

µx)

−

(x

−

−

µx)T QS−1QT (x

µx)
−
(8.11.96)

DRAFT April 24, 2015

193

3. Using the fact that

(cid:90)

exp

1
2

−

(cid:26)(cid:16)

y

−

µy + S−1QT (x

(cid:17)T

(cid:16)

y

S

µx)

−

−

µy + S−1QT (x

−

(cid:17)(cid:27)

µx)

dy =

(cid:113)

det (cid:0)2πS−1(cid:1)

Exercises

show that

p(x)

exp

∝

1
2

−

(x

−

µx)T (cid:16)

P

−

QS−1QT(cid:17)

(x

µx)

−

4. Since

(cid:18) Σxx Σxy
Σyx Σyy

(cid:19)

(cid:18) P Q
QT S

=

(cid:19)−1

Use partitioned matrix inversion to show that

Σxx =

(cid:16)

P

−

QS−1QT(cid:17)−1

5. Hence show that

p(x) =

N

(x µx, Σxx)

(8.11.97)

(8.11.98)

(8.11.99)

(8.11.100)

(8.11.101)

Exercise 8.45. Consider a not necessarily square matrix T and Gaussian distributed x, with p(x) =

(x µ, Σ). We are interested in the distribution of the variable y = Tx, where dim y

N
Deﬁne

z =

(cid:18) y
˜x

(cid:19)

=

x

(cid:19)

(cid:18) T
˜I
(cid:124) (cid:123)(cid:122) (cid:125)
M

where ˜I is any matrix such that M is square and invertible.

dim x.

≤

(8.11.102)

1. Show that

(cid:90)

p(z) =

2. Show that

(cid:90)

p(z

x)p(x)dx =
|

δ (z

−

Mx) p(x)dx

exp

∝

1
2

−

(cid:0)M−1z

−

µ(cid:1)T

Σ−1 (cid:0)M−1z

µ(cid:1) (8.11.103)

−

p(z) =

N

(cid:16)

z Mµ, MΣMT(cid:17)

(8.11.104)

3. Using p(y) = (cid:82) p(z)d˜x and the result that the marginal of a Gaussian is a Gaussian (see above

exercise), show that

p(y) =

N

(cid:16)

y Tµ, TΣTT(cid:17)

(8.11.105)

194

DRAFT April 24, 2015

CHAPTER 9

Learning as Inference

In previous chapters we largely assumed that all distributions are fully speciﬁed for the inference tasks.
In machine learning and related ﬁelds, however, the distributions need to be learned on the basis of data.
Learning is then the problem of integrating data with domain knowledge of the model environment. In this
chapter we discuss how learning can be phrased as an inference problem.

9.1 Learning as Inference

9.1.1 Learning the bias of a coin

Consider data expressing the results of tossing a coin. We write vn = 1 if on toss n the coin comes up heads,
and vn = 0 if it is tails. Our aim is to estimate the probability θ that the coin will be a head, p(vn = 1
θ) = θ
|
– called the ‘bias’ of the coin. For a fair coin, θ = 0.5. The variables in this environment are v1, . . . , vN and
θ and we require a model of the probabilistic interaction of the variables, p(v1, . . . , vN , θ). Assuming there
is no dependence between the observed tosses, except through θ, we have the belief network

p(v1, . . . , vN , θ) = p(θ)

N
(cid:89)

n=1

p(vn

θ)
|

(9.1.1)

which is depicted in ﬁg(9.1). The assumption that each observation is independent and identically dis-
tributed is called the i.i.d. assumption.

Learning refers to using the observations v1, . . . , vN to infer θ. In this context, our interest is

v1, . . . , vN ) =

p(θ

|

p(v1, . . . , vN , θ)
p(v1, . . . , vN )

=

p(v1, . . . , vN

θ)p(θ)
|

p(v1, . . . , vN )

(9.1.2)

We still need to fully specify the prior p(θ). To avoid complexities resulting from continuous variables, we’ll
consider a discrete θ with only three possible states, θ

. Speciﬁcally, we assume

p(θ = 0.1) = 0.15,

p(θ = 0.5) = 0.8,

(9.1.3)

as shown in ﬁg(9.2a). This prior expresses that we have 80% belief that the coin is ‘fair’, 5% belief the coin
is biased to land heads (with θ = 0.8), and 15% belief the coin is biased to land tails (with θ = 0.1). The
distribution of θ given the data and our beliefs is

v1, . . . , vN )

p(θ

|

∝

∝

N
(cid:89)

p(vn

p(θ)

n=1
(cid:80)N

p(θ)θ

n=1

θ) = p(θ)
|

I[vn=1] (1

−

θ)

N
(cid:89)

I[vn=1] (1

θ

n=1
(cid:80)N

I[vn=0]

n=1

I[vn=0]

θ)

−

(9.1.4)

(9.1.5)

195

0.1, 0.5, 0.8
}

∈ {
p(θ = 0.8) = 0.05

θ

v3

(a)

vN

· · ·

v1

v2

θ

vn

(b)

N

Learning as Inference

(a): Belief network for coin
Figure 9.1:
tossing model. (b): Plate notation equiv-
alent of (a). A plate replicates the quanti-
ties inside the plate a number of times as
speciﬁed in the plate.

In the above (cid:80)N
n=1
NH . Likewise, (cid:80)N

n=1

I [vn = 1] is the number of occurrences of heads, which we more conveniently denote as

I [vn = 0] is the number of tails, NT . Hence

p(θ

v1, . . . , vN )
|

p(θ)θNH (1

θ)NT

−
For an experiment with NH = 2, NT = 8, the posterior distribution is

∝

p(θ = 0.1

p(θ = 0.5

p(θ = 0.8

) = k

) = k

) = k

|V

|V

|V

×

×

×

0.15

0.8

×
0.05

0.12
×
0.52
×
0.82

0.98 = k
×
0.58 = k
×
0.28 = k

×

6.46

7.81

×

8.19

×

×

×

10−4
×
10−4
10−8

×

where
7.81

×

is shorthand for v1, . . . , vN . From the normalisation requirement we have 1/k = 6.46

V
10−4 + 8.19

10−8 = 0.0014, so that

×

p(θ = 0.1

|V

) = 0.4525,

p(θ = 0.5

) = 0.5475,

|V

p(θ = 0.8

|V

) = 0.0001

(9.1.6)

(9.1.7)

(9.1.8)

(9.1.9)

10−4 +

×

(9.1.10)

as shown in ﬁg(9.2b). These are the ‘posterior’ parameter beliefs. In this case, if we were asked to choose a
single a posteriori most likely value for θ, it would be θ = 0.5, although our conﬁdence in this is low since
the posterior belief that θ = 0.1 is also appreciable. This result is intuitive since, even though we observed
more Tails than Heads, our prior belief was that it was more likely the coin is fair.

Repeating the above with NH = 20, NT = 80, the posterior changes to

p(θ = 0.1

)

1
≈

1.93

10−6,

p(θ = 0.5

)

1.93

10−6,

p(θ = 0.8

10−35 (9.1.11)

−

|V

|V
ﬁg(9.2c), so that the posterior belief in θ = 0.1 dominates. This is reasonable since in this situation, there
are so many more tails than heads that this is unlikely to occur from a fair coin. Even though we a priori
thought that the coin was fair, a posteriori we have enough evidence to change our minds.

|V

×

×

×

≈

2.13

)
≈

@@

9.1.2 Making decisions

In itself, the Bayesian posterior merely represents our beliefs and says nothing about how best to summarise
these beliefs. In situations in which decisions need to be taken under uncertainty we need to additionally
specify what the utility of any decision is, as in chapter(7).

0.8

0.1

0.1

0.5
θ

(a)

0.8

0.1

0.5
θ

(b)

0.8

0.5
θ

(c)

(a): Prior encoding our be-
Figure 9.2:
liefs about the amount the coin is biased to
heads. (b): Posterior having seen NH = 2
(c): Posterior
heads and NT = 8 tails.
having seen NH = 20 heads and NT = 80
tails. Assuming any value of 0
1 is
possible the ML setting is θ = 0.2 in both
NH = 2, NT = 8 and NH = 20, NT = 80.

≤

≤

θ

196

DRAFT April 24, 2015

Learning as Inference

In the coin tossing scenario where θ is assumed to be either 0.1, 0.5 or 0.8, we setup a decision problem as
follows: If we correctly state the bias of the coin we gain 10 points; being incorrect, however, loses 20 points.
We can write this using

U (θ, θ0) = 10I (cid:2)θ = θ0(cid:3)

20I (cid:2)θ

= θ0(cid:3)

(9.1.12)

where θ0 is the true value for the bias. The expected utility of the decision that the coin is θ = 0.1 is

−

U (θ = 0.1) = U (θ = 0.1, θ0 = 0.1)p(θ0 = 0.1

)

|V

|V
Plugging in the numbers from equation (9.1.10), we obtain

+ U (θ = 0.1, θ0 = 0.5)p(θ0 = 0.5

) + U (θ = 0.1, θ0 = 0.8)p(θ0 = 0.8

)

(9.1.13)

|V

U (θ = 0.1) = 10

Similarly

U (θ = 0.5) = 10

and

U (θ = 0.8) = 10

×

×

×

0.4525

20

×

−

0.5475

20

×

−

0.0001 =

6.4270

−

0.5475

20

×

−

0.4525

20

×

−

0.0001 =

3.5770

−

0.0001

20

×

−

0.4525

20

×

−

0.5475 =

19.999

−

The best (that with the highest utility) is to say that the coin is unbiased, θ = 0.5.

Repeating the above calculations for NH = 20, NT = 80, we arrive at

U (θ = 0.1) = 10

(1

−

×

1.93

×

10−6)

−

20 (cid:0)1.93

×

10−6 + 2.13

×

10−35(cid:1) = 9.9999

U (θ = 0.5) = 10

U (θ = 0.8) = 10

1.93

2.13

×

×

×

×

10−6

−

20 (cid:0)1

1.93

×

−

10−6 + 2.13

10−35(cid:1)

×

20.0

≈ −

10−35

20 (cid:0)1

−

1.93

−

×

10−6 + 1.93

10−6(cid:1)

×

20.0

≈ −

so that the best decision in this case is to choose θ = 0.1.

(9.1.14)

(9.1.15)

(9.1.16)

(9.1.17)

(9.1.18)

(9.1.19)

As more information about the distribution p(v, θ) becomes available the posterior p(θ
ingly peaked, aiding our decision making process.

|V

) becomes increas-

9.1.3 A continuum of parameters

In section(9.1.1) we considered only three possible values for θ. Here we discuss a continuum of parameters.

Using a ﬂat prior

We ﬁrst examine the case of a ‘ﬂat’ or uniform prior p(θ) = k for some constant k. For continuous variables,
normalisation requires

(cid:90)

p(θ)dθ = 1

Since θ represents a probability we must have 0

(cid:90) 1

0

p(θ)dθ = k = 1

θ

≤

≤

1,

Repeating the previous calculations with this ﬂat continuous prior, we have

p(θ

|V

) =

1
c

θNH (1

θ)NT

−

where c is a constant to be determined by normalisation,

c =

(cid:90) 1

0

θNH (1

−

θ)NT dθ

≡

B(NH + 1, NT + 1)

where B(α, β) is the Beta function. See ﬁg(9.3) for an example.

DRAFT April 24, 2015

(9.1.20)

(9.1.21)

(9.1.22)

(9.1.23)

197

(cid:54)
Learning as Inference

|V

Figure 9.3: Posterior p(θ
) assuming a ﬂat prior on θ. (blue)
NH = 2, NT = 8 and (red) NH = 20, NT = 80. In both cases,
the most probable state of the posterior (maximum a posteriori)
is 0.2, which makes intuitive sense, since the fraction of Heads to
Tails in both cases is 0.2. Where there is more data, the posterior
is more certain and sharpens around the most probable value.

Using a conjugate prior

Determining the normalisation constant of a continuous distribution requires that the integral of the unnor-
malised posterior can be carried out. For the coin tossing case, it is clear that if the prior is of the form of
a Beta distribution, then the posterior will be of the same parametric form. For prior

p(θ) =

1
B(α, β)

θα−1 (1

θ)β−1

−

the posterior is

p(θ

)

|V

∝

θα−1 (1

−

θ)β−1 θNH (1

θ)NT

−

so that

p(θ

|V

) = B(θ

α + NH , β + NT )
|

(9.1.24)

(9.1.25)

(9.1.26)

The prior and posterior are of the same form (both Beta distributions) but simply with diﬀerent parameters.
Hence the Beta distribution is ‘conjugate’ to the Binomial distribution.

9.1.4 Decisions based on continuous intervals

To illustrate the use of continuous variables in decision making, we consider a simple decision problem.
The result of a coin tossing experiment is NH = 2 heads and NT = 8 tails. You now need to make a
decision: you win 10 dollars if you correctly guess which way the coin is biased – towards heads or tails.
If your guess is incorrect, you lose a million dollars. What is your decision? (Assume an uninformative prior).

We need two quantities, θ for our guess and θ0 for the truth. Then the utility of saying Heads is

U (θ > 0.5, θ0 > 0.5)p(θ0 > 0.5

|V

) + U (θ > 0.5, θ0 < 0.5)p(θ0 < 0.5

|V

)

(9.1.27)

In the above,

p(θ0 < 0.5

|V

) =

=

≡

(cid:90) 0.5

p(θ0

)dθ0

0

|V
1
B(α + NH , β + NT )
I0.5(α + NH , β + NT )

0

(cid:90) 0.5

θα+NH −1 (1

−

θ)β+NT −1 dθ

(9.1.28)

(9.1.29)

(9.1.30)

where Ix(a, b) is the regularised incomplete Beta function. For the case of NH = 2, NT = 8, under a ﬂat
prior,

p(θ0 < 0.5

) = I0.5(NH + 1, NT + 1) = 0.9673

|V
Since the events are exclusive, p(θ0
heads is more likely is

0.5

|V

≥

) = 1

−

0.9673 = 0.0327. Hence the expected utility of saying

(9.1.31)

10

×

0.0327

−

1000000

×

0.9673 =

9.673

−

×

105.

Similarly, the utility of saying tails is more likely can be computed to be

10

×

0.9673

−

1000000

×

0.0327 =

3.269

−

×

104.

(9.1.32)

(9.1.33)

198

DRAFT April 24, 2015

00.20.40.60.810510θBayesian methods and ML-II

a

s

c

(a)

θ

v

(a)

θ(cid:48)

θ

v

(b)

θa

an

θs

sn

cn

n = 1 : N

θc

(b)

Figure 9.4: (a): A model for the relationship between
(b):
lung Cancer, Asbestos exposure and Smoking.
Plate notation replicating the observed n datapoints
with the CPTs tied across all datapoints.

(a): Standard ML learning. The best parameter θ is found
Figure 9.5:
by maximising the probability that the model generates the observed data
(b): ML-II learning. In cases where we have a
θopt = arg maxθ p(v
prior preference for the parameters θ, but with unspeciﬁed hyperparameter
θ(cid:48), we can ﬁnd θ(cid:48) by θ(cid:48)

opt = arg maxθ(cid:48) p(v

θ).
|

θ(cid:48)) = arg maxθ(cid:48)
|

p(v
(cid:104)

θ)
|

(cid:105)p(θ|θ(cid:48)).

Since the expected utility of deciding ‘tails’ is highest, we are better oﬀ taking the decision that the coin is
more likely to come up tails.

If we modify the above so that we lose 100 million dollars if we guess tails when in fact it is heads, the
106. In this case, even though we are more conﬁdent that
expected utility of saying tails would be
the coin is likely to come up tails, we would pay such a penalty of making a mistake in saying tails, that it
is in fact better to say heads.

3.27

×

−

9.2 Bayesian methods and ML-II

θ), for which we wish to the learn the optimal parameters θ given
Consider a parameterised distribution p(v
θ) is depicted in ﬁg(9.5a), where a dot indicates that no distribution is present
some data. The model p(v
|
on that variable. For a single observed datapoint v, setting θ by maximum likelihood corresponds to ﬁnding
the parameter θ that maximises p(v

|

θ).
|

|

In some cases we may have an idea about which parameters θ are more appropriate and can express this prior
preference using a distribution p(θ). If the prior were fully speciﬁed, then there is nothing to ‘learn’ since
v) is now fully known. However, in many cases in practice, we are unsure of the exact parameter settings
p(θ
θ(cid:48)) with hyperparameter θ(cid:48).
of the prior, and hence specify a parametersised prior using a distribution p(θ
This is depicted in ﬁg(9.5b). Learning then corresponds to ﬁnding the optimal θ(cid:48) that maximises the
θ(cid:48)). This is known as an ML-II procedure since it corresponds to maximum
likelihood p(v
|
likelihood, but at the higher, hyperparameter level[33, 197]. By treating the parameters θ as variables, one
can view this then as learning under hidden variables, for which the methods of chapter(11) are applicable.
We will encounter examples of this ML-II procedure later, for example in section(18.1.2).

θ(cid:48)) = (cid:82)
|

θ p(v

θ)p(θ

|

|

DRAFT April 24, 2015

199

Maximum Likelihood Training of Belief Networks

a
1
1
0
0
1
0
1

s
1
0
1
1
1
0
0

c
1
0
1
0
1
0
1

Figure 9.6: A database containing information about the Asbestos exposure
(1 signiﬁes exposure), being a Smoker (1 signiﬁes the individual is a smoker),
and lung Cancer (1 signiﬁes the individual has lung Cancer). Each row
contains the information for each of the seven individuals in the database.

9.3 Maximum Likelihood Training of Belief Networks

Consider the following model of the relationship between exposure to asbestos (a), being a smoker (s) and
the incidence of lung cancer (c)

a, s)p(a)p(s)
p(a, s, c) = p(c
|

(9.3.1)

.
0, 1
which is depicted in ﬁg(9.4a). Each variable is binary, dom(a) =
}
We assume that there is no direct relationship between Smoking and exposure to Asbestos. This is the
kind of assumption that we may be able to elicit from medical experts. Furthermore, we assume that we
have a list of patient records, ﬁg(9.6), where each row represents a patient’s data. To learn the table entries
p(c
a, s) we can do so by counting the number of times variable c is in state 1 for each of the 4 parental
|
states of a and s:

, dom(c) =
}

, dom(s) =

0, 1
{

0, 1
}

{

{

a = 0, s = 1) = 0.5
p(c = 1
a = 0, s = 0) = 0,
p(c = 1
|
|
a = 1, s = 1) = 1
a = 1, s = 0) = 0.5 p(c = 1
p(c = 1
|
|

(9.3.2)

Similarly, based on counting, p(a = 1) = 4/7, and p(s = 1) = 4/7. These three CPTs then complete the full
distribution speciﬁcation.

Setting the CPT entries in this way by counting the relative number of occurrences corresponds mathemat-
ically to maximum likelihood learning under the i.i.d. assumption, as we show below.

Maximum likelihood corresponds to counting

For a BN there is a constraint on the form of p(x), namely

p(x) =

K
(cid:89)

i=1

p(xi

|

pa (xi))

(9.3.3)

pa (xi)), as shown in section(8.7.3), we can
To compute the maximum likelihood setting of each term p(xi
|
equivalently minimise the Kullback-Leibler divergence between the empirical distribution q(x) and p(x). For
the BN p(x), and empirical distribution q(x) we have

KL(q

p) =

|

−

(cid:42) K
(cid:88)

i=1

(cid:43)

log p (xi

pa (xi))

|

q(x)

+ const. =

K
(cid:88)

−

i=1

log p (xi
(cid:104)

pa (xi))
|

(cid:105)q(xi,pa(xi)) + const.

(9.3.4)

This follows using the general result

f (
(cid:104)

i)

X

(cid:105)q(X ) =

(cid:104)

f (

X

i)

(cid:105)q(Xi)

(9.3.5)

which says that if the function f only depends on a subset of the variables, we only need to know the
marginal distribution of this subset of variables in order to carry out the average.

Since q(x) is ﬁxed, we can add on entropic terms in q and equivalently mimimize

KL(q

p) =

|

=

K
(cid:88)

i=1
K
(cid:88)

i=1

(cid:104)
(cid:104)

log q(xi

pa (xi))
|

(cid:105)q(xi,pa(xi)) − (cid:104)

log p (xi

pa (xi))
|

(cid:105)q(xi,pa(xi))

(cid:105)

+const.

KL(q(xi
(cid:104)

p(xi
pa (xi))
|
|

|

pa (xi)))

(cid:105)q(pa(xi))

+const.

(9.3.6)

@@

(9.3.7)

@@

200

DRAFT April 24, 2015

Maximum Likelihood Training of Belief Networks

The ﬁnal line is a positive weighted sum of individual Kullback-Leibler divergences. The minimal Kullback-
Leibler setting, and that which corresponds to maximum likelihood, is therefore

p(xi

|

pa (xi)) = q(xi

pa (xi))
|

In terms of the original data, this is

p(xi = s
|

pa (xi) = t)

N
(cid:88)

n=1

∝

I [xn

i = s, pa (xn

i ) = t]

(9.3.8)

(9.3.9)

pa (xi)) can be set by counting the
This expression corresponds to the intuition that the table entry p(xi
|
occurs in the dataset (where t is a vector of parental states).
xi = s, pa (xi) = t
number of times the state
}
{
The table is then given by the relative number of counts of being in state s compared to the other states s(cid:48),
for ﬁxed joint parental state t.

An alternative method to derive this intuitive result is to use Lagrange multipliers, see exercise(9.4). For
reader less comfortable with the above Kullback-Leibler derivation, a more direct example is given below
which makes use of the notation

(cid:93) (x1 = s1, x2 = s2, x3 = s3, . . .)

(9.3.10)

to denote the number of times that states x1 = s1, x2 = s2, x3 = s3, . . . occur together in the training data.
See also section(10.1) for further examples.

Example 9.1. We wish to learn the table entries of the distribution p(x1, x2, x3) = p(x1
x2, x3)p(x2)p(x3).
x2 = 1, x3 = 0) using maximum likelihood. For i.i.d.
We address here how to ﬁnd the CPT entry p(x1 = 1
|
data, the contribution from p(x1

|

x2, x3) to the log likelihood is
|

(cid:88)

n

log p(xn
1 |

2 , xn
xn
3 )

The number of times p(x1 = 1
|
the number of such occurrences in the training set. Since (by the normalisation constraint) p(x1 = 0
1, x3 = 0) = 1
likelihood is

x2 = 1, x3 = 0) occurs in the log likelihood is (cid:93) (x1 = 1, x2 = 1, x3 = 0),
x2 =
x2 = 1, x3 = 0) to the log
x2 = 1, x3 = 0), the total contribution of p(x1 = 1
|

p(x1 = 1
|

−

|

x2 = 1, x3 = 0)
(cid:93) (x1 = 1, x2 = 1, x3 = 0) log p(x1 = 1
|

+ (cid:93) (x1 = 0, x2 = 1, x3 = 0) log (1

x2 = 1, x3 = 0))
p(x1 = 1
|

−

(9.3.11)

Using θ

p(x1 = 1
|

≡

x2 = 1, x3 = 0) we have

(cid:93) (x1 = 1, x2 = 1, x3 = 0) log θ + (cid:93) (x1 = 0, x2 = 1, x3 = 0) log (1

−
Diﬀerentiating the above expression w.r.t. θ and equating to zero gives

θ)

(cid:93) (x1 = 1, x2 = 1, x3 = 0)
θ

−

(cid:93) (x1 = 0, x2 = 1, x3 = 0)

1

θ

−

= 0

The solution for optimal θ is then

p(x1 = 1
|

x2 = 1, x3 = 0) =

(cid:93) (x1 = 1, x2 = 1, x3 = 0)
(cid:93) (x1 = 1, x2 = 1, x3 = 0) + (cid:93) (x1 = 0, x2 = 1, x3 = 0)

,

corresponding to the intuitive counting procedure.

DRAFT April 24, 2015

(9.3.12)

(9.3.13)

(9.3.14)

201

Maximum Likelihood Training of Belief Networks

xn−1

xn

x1

x2

· · ·

y

Figure 9.7: A variable y with a large number of parents
x1, . . . , xn requires the speciﬁcation of an exponen-
tially large number of entries in the conditional prob-
x1, . . . , xn). One solution to this diﬃculty
ability p(y
|
x1, . . . , xn, θ).
is to parameterise the conditional, p(y
|

Conditional probability functions

Consider a binary variable y with n binary parental variables, x = (x1, . . . , xn), see ﬁg(9.7). There are 2n
entries in the CPT of p(y
x) so that it is infeasible to explicitly store these entries for even moderate values
|
of n. To reduce the complexity of this CPT we may constrain the form of the table. For example, one could
use a function

x, w) =
p(y = 1
|

1
1 + e−wTx

(9.3.15)

where we only need to specify the n-dimensional parameter vector w.

In this case, rather than using maximum likelihood to learn the entries of the CPTs directly, we instead
learn the value of the parameter w. Since the number of parameters in w is small (n, compared with 2n
in the unconstrained case), we also have some hope that with a small number of training examples we can
learn a reliable value for w.

Example 9.2. Consider the following 3 variable model p(x1, x2, x3) = p(x1
0, 1
{

, i = 1, 2, 3. We assume that the CPT is parameterised using θ = (θ1, θ2) with

}

|

x2, x3)p(x2)p(x3), where xi

∈

x2, x3, θ)
p(x1 = 1
|

≡

e−θ2

1−θ2

2(x2−x3)2

(9.3.16)

One may verify that the above probability is always positive and lies between 0 and 1. Due to normalisation,
we must have

x2, x3) = 1
p(x1 = 0
|

−

x2, x3)
p(x1 = 1
|

(9.3.17)

For unrestricted p(x2) and p(x3), the maximum likelihood setting is p(x2 = 1)
(cid:93) (x3 = 1). The contribution to the log likelihood from the term p(x1
1)

(cid:93) (x2 = 1), and p(x3 =
x2, x3, θ), assuming i.i.d. data, is
|

∝

∝

L(θ1, θ2) =

N
(cid:88)

n=1

I [xn

1 = 1] (cid:0)

θ2
1 −

−

2(xn
θ2

2 −

3 )2(cid:1) + I [xn
xn

1 = 0] log

(cid:16)

1

−

e−θ2

1−θ2

2(xn

2 −xn

3 )2(cid:17)

(9.3.18)

This objective function needs to be optimised numerically to ﬁnd the best θ1 and θ2. The gradient is

dL
dθ1

=

N
(cid:88)

n=1

dL
dθ2

=

N
(cid:88)

n=1

−

2I [xn

1 = 1] θ1 + 2I [xn

1 = 0]

−

θ1e−θ2
e−θ2
1

1−θ2
1−θ2

2(xn
2(xn

2 −xn
3 )2
2 −xn

3 )2

−

2I [xn

1 = 1] θ2 (xn

3 )2 + 2θ2I [xn
xn

1 = 0]

2 −

(xn

2 −
1
−

3 )2e−θ2
xn
1−θ2
e−θ2

1−θ2
2(xn

2(xn
2 −xn

3 )2

2 −xn

3 )2

(9.3.19)

(9.3.20)

The gradient can be used as part of a standard optimisation procedure (such as conjugate gradients, see
section(A.5)) to ﬁnd the maximum likelihood parameters θ1, θ2.

202

DRAFT April 24, 2015

Bayesian Belief Network Training

9.4 Bayesian Belief Network Training

An alternative to maximum likelihood training of a BN is to use a Bayesian approach in which we maintain
a distribution over parameters. We continue with the Asbestos, Smoking, Cancer scenario,

a, s)p(a)p(s)
p(a, c, s) = p(c
|

(9.4.1)

as represented in ﬁg(9.4a). So far we’ve only speciﬁed the independence structure, but not the entries of the
tables p(c
, we would
a, s), p(a), p(s). Given a set of visible observations,
}
|
like to learn appropriate distributions for the table entries.

(an, sn, cn) , n = 1, . . . , N

=

V

{

To begin we need a notation for the table entries. With all variables binary we have parameters such as

θa) = θa, p(c = 1
p(a = 1
|
|

a = 0, s = 1, θc) = θ0,1

c

and similarly for the remaining parameters θ1,1

c

, θ0,0
c

, θ1,0
c

. For our example, the parameters are

θa, θs, θ0,0

c

(cid:124)

, θ0,1
c

, θ1,0
c
(cid:123)(cid:122)
θc

, θ1,1
c
(cid:125)

(9.4.2)

(9.4.3)

In the following section, section(9.4.1), we describe ﬁrst useful independence assumptions on the general
form of the prior variables, before making a speciﬁc numerical prior speciﬁcation in section(9.4.2).

9.4.1 Global and local parameter independence

In Bayesian learning of BNs, we need to specify a prior on the joint table entries. Since in general dealing
with multi-dimensional continuous distributions is computationally problematic, it is useful to specify only
uni-variate distributions in the prior. As we show below, this has a pleasing consequence that for i.i.d. data
the posterior also factorises into uni-variate distributions.

Global parameter independence

A convenient assumption is that the prior factorises over parameters. For our Asbestos, Smoking, Cancer
example, we assume

p(θa, θs, θc) = p(θa)p(θs)p(θc)

Assuming the data is i.i.d., we then have the joint model

p(θa, θs, θc,

V

) = p(θa)p(θs)p(θc)

(cid:89)

n

p(an

θa)p(sn
|

θs)p(cn
|

sn, an, θc)
|

(9.4.4)

(9.4.5)

the belief network for which is given in ﬁg(9.8.) A convenience of the factorised prior for a BN is that the
posterior also factorises, since

p(θa, θs, θc

)

|V

∝

=

∝

p(θa, θs, θc,
(cid:40)

(cid:89)

p(θa)

)

V
p(an

p(θa

n
a)p(θs

|V

(cid:41) (cid:40)

θa)
|

p(θs)

(cid:89)

p(sn

n

θs)
|

(cid:41) (cid:40)

p(θc)

(cid:89)

n

p(cn

|

sn, an, θc)

(cid:41)

s)p(θc

|V

c)

|V

(9.4.6)

so that one can consider each parameter posterior separately. In this case, ‘learning’ involves computing the
posterior distributions p(θi

i is the set of training data restricted to the family of variable i.

i) where

|V

V

The global independence assumption conveniently results in a posterior distribution that factorises over the
conditional tables. However, the parameter θc is itself 4 dimensional. To simplify this we need to make a
further assumption as to the structure of each local table.

DRAFT April 24, 2015

203

Bayesian Belief Network Training

Figure 9.8: A Bayesian parameter model for the relationship between
lung Cancer, Asbestos exposure and Smoking with factorised parameter
priors. The global parameter independence assumption means that the
prior over tables factorises into priors over each conditional probability ta-
ble. The local independence assumption, which in this case comes into
a, s), means that p(θc) factorises in (cid:81)
c ), where
eﬀect only for p(c
|
.
(0, 0), (0, 1), (1, 0), (1, 1)
}
{

a,s∈P p(θa,s

=

P

θa

an

θs

sn

cn

n = 1 : N

θa,s
c

(a, s)

∈ P

Local parameter independence

If we further assume that the prior for the table factorises over all states a, c:

p(θc) = p(θ0,0

c )p(θ1,0

c )p(θ0,1

c )p(θ1,1
c )

then the posterior is given by

p(

θc)p(θ0,0
c)
p(θc
c
|
∝
|V
V
= (cid:2)θ0,0
(cid:3)(cid:93)(a=0,s=0,c=1) (cid:2)1
c
(cid:124)

c )p(θ1,1
c )p(θ0,1
c )
(cid:3)(cid:93)(a=0,s=0,c=0)

(cid:3)(cid:93)(a=1,s=0,c=1) (cid:2)1

(cid:3)(cid:93)(a=1,s=0,c=0)

(cid:3)(cid:93)(a=1,s=1,c=1) (cid:2)1

(cid:3)(cid:93)(a=1,s=1,c=0)

(cid:2)θ1,0
c
(cid:124)

×

(cid:3)(cid:93)(a=0,s=1,c=1) (cid:2)1

(cid:3)(cid:93)(a=0,s=1,c=0)

p(θ0,0
c )
(cid:125)

(cid:2)θ0,1
c
(cid:124)

p(θ1,0
c )
(cid:125)

(cid:2)θ1,1
c
(cid:124)

θ0,1
c
−
(cid:123)(cid:122)
∝p(θ0,1
c

|Vc)

θ1,1
c
−
(cid:123)(cid:122)
∝p(θ1,1
c

|Vc)

c )p(θ1,0
θ0,0
c
−
(cid:123)(cid:122)
∝p(θ0,0
c

|Vc)

θ1,0
c
−
(cid:123)(cid:122)
∝p(θ1,0
c

|Vc)

so that the posterior also factorises over the parental states of the local conditional table.

Posterior marginal table

A marginal probability table is given by, for example,

a = 1, s = 0,
p(c = 1
|

V

) =

(cid:90)

θc

a = 1, s = 0, θ1,0
p(c = 1
|

c )p(θc

|V

c)

The integral over all the other tables in equation (9.4.9) is unity, and we are left with

a = 1, s = 0,
p(c = 1
|

V

) =

(cid:90)

θ1,0
c

a = 1, s = 0, θ1,0
p(c = 1
|

c )p(θ1,0
c

|V

c) =

(cid:90)

θ1,0
c

c p(θ1,0
θ1,0
c

c)

|V

(9.4.7)

p(θ0,1
c )
(cid:125)

p(θ1,1
c )
(cid:125)

(9.4.8)

(9.4.9)

(9.4.10)

9.4.2 Learning binary variable tables using a Beta prior

We continue the example of section(9.4.1) where all variables are binary, but using a continuous valued table
prior. The simplest case is to start with p(a
θa) since this requires only a univariate prior distribution p(θa).
|
The likelihood depends on the table variable via

p(a = 1
|

θa) = θa

so that the total likelihood term is

θ(cid:93)(a=1)
a

(1

−

θa)(cid:93)(a=0)

204

(9.4.11)

(9.4.12)

DRAFT April 24, 2015

Bayesian Belief Network Training

The posterior is therefore

p(θa

a)

|V

∝

p(θa)θ(cid:93)(a=1)
a

(1

θa)(cid:93)(a=0)

−

(9.4.13)

θa)β then conjugacy will hold, and the mathematics
This means that if the prior is also of the form θα
−
of integration will be straightforward. This suggests that the most convenient choice is a Beta distribution,

a (1

p(θa) = B (θa

αa, βa) =

|

1
B(αa, βa)

θαa−1
a

(1

−

θa)βa−1

for which the posterior is also a Beta distribution:

p(θa

a) = B (θa

|V

αa + (cid:93) (a = 1) , βa + (cid:93) (a = 0))
|

The marginal table is given by (following similar reasoning as for equation (9.4.10))

p(a = 1

a) =

|V

(cid:90)

p(θa

a)θa =

|V

θa

αa + (cid:93) (a = 1)
αa + (cid:93) (a = 1) + βa + (cid:93) (a = 0)

(9.4.14)

(9.4.15)

(9.4.16)

using the result for the mean of a Beta distribution, deﬁnition(8.23).

The situation for the table p(c
a, s) is slightly more complex since we need to specify a prior for each of
|
the parental tables. As above, this is most convenient if we specify a Beta prior, one for each of the (four)
parental states. Let’s look at a speciﬁc table

a = 1, s = 0)
p(c = 1
|

Assuming the local independence property, we have p(θ1,0

c

c) given by

|V

B (cid:0)θ1,0
c

|

αc(a = 1, s = 0) + (cid:93) (c = 1, a = 1, s = 0) , βc(a = 1, s = 0) + (cid:93) (c = 0, a = 1, s = 0)(cid:1)

As before, the marginal probability table is then given by

a = 1, s = 0,
p(c = 1
|

c) =

V

αc(a = 1, s = 0) + (cid:93) (c = 1, a = 1, s = 0)
αc(a = 1, s = 0) + βc(a = 1, s = 0) + (cid:93) (a = 1, s = 0)

since (cid:93) (a = 1, s = 0) = (cid:93) (c = 0, a = 1, s = 0) + (cid:93) (c = 1, a = 1, s = 0).

(9.4.17)

(9.4.18)

(9.4.19)

The prior parameters αc(a, s) are called hyperparameters. A complete ignorance prior would correspond
to setting α = β = 1, see ﬁg(8.4).

It is instructive to examine this Bayesian solution under various conditions:

No data limit N

0 In the limit of no data, the marginal probability table corresponds to the prior,

→
which is given in this case by

a = 1, s = 0) =
p(c = 1
|

αc(a = 1, s = 0)
αc(a = 1, s = 0) + βc(a = 1, s = 0)

(9.4.20)

For a ﬂat prior α = β = 1 for all states a, c, this would give a prior probability of p(c = 1
0) = 0.5.

a = 1, s =
|

Inﬁnite data limit N

In this limit the marginal probability tables are dominated by the data counts,
since these will typically grow in proportion to the size of the dataset. This means that in the inﬁnite
(or very large) data limit,

→ ∞

a = 1, s = 0,
p(c = 1
|

V

)

→

(cid:93) (c = 1, a = 1, s = 0)
(cid:93) (c = 1, a = 1, s = 0) + (cid:93) (c = 0, a = 1, s = 0)

(9.4.21)

which corresponds to the maximum likelihood solution.

This eﬀect that the large data limit of a Bayesian procedure corresponds to the maximum likelihood
solution is general unless the prior has a pathologically strong eﬀect.

DRAFT April 24, 2015

205

Bayesian Belief Network Training

Zero hyperparameter limit When αc = βc = 0, the marginal table equation (9.4.19) corresponds to the
maximum likelihood table setting for any amount of data. When αc = βc = 0, the Beta distribution
places mass 0.5 at 0 and mass 0.5 at 1. Note that this equivalence of the maximum likelihood solution
with the marginal table under zero hyperparameter values contrasts with the equivalence of the MAP
table under uniform hyperparameter values.

Example 9.3 (Asbestos-Smoking-Cancer).

Consider the binary variable network

a, s)p(a)p(s)
p(c, a, s) = p(c
|

(9.4.22)

The data
marginal posterior tables are given by

V

is given in ﬁg(9.6). Using a ﬂat Beta prior α = β = 1 for all conditional probability tables, the

p(a = 1

|V

) =

1 + (cid:93) (a = 1)
2 + N

=

1 + 4
2 + 7

=

5
9 ≈

0.556

(9.4.23)

By comparison, the maximum likelihood setting is 4/7 = 0.571. The Bayesian result is a little more
cautious, which squares with our prior belief that any setting of the probability is equally likely, pulling the
posterior towards 0.5.

Similarly,

p(s = 1

|V

) =

1 + (cid:93) (s = 1)
2 + N

=

1 + 4
2 + 7

=

5
9 ≈

0.556

and

a = 1, s = 1,
p(c = 1
|

V

) =

1 + (cid:93) (c = 1, a = 1, s = 1)
2 + (cid:93) (c = 1, a = 1, s = 1) + (cid:93) (c = 0, a = 1, s = 1)

=

1 + 2
2 + 2

=

a = 1, s = 0,
p(c = 1
|

V

) =

1 + (cid:93) (c = 1, a = 1, s = 0)
2 + (cid:93) (c = 1, a = 1, s = 0) + (cid:93) (c = 0, a = 1, s = 0)

=

1 + 1
2 + 2

=

a = 0, s = 1,
p(c = 1
|

V

) =

1 + (cid:93) (c = 1, a = 0, s = 1)
2 + (cid:93) (c = 1, a = 0, s = 1) + (cid:93) (c = 0, a = 0, s = 1)

=

1 + 1
2 + 2

=

a = 0, s = 0,
p(c = 1
|

V

) =

1 + (cid:93) (c = 1, a = 0, s = 0)
2 + (cid:93) (c = 1, a = 0, s = 0) + (cid:93) (c = 0, a = 0, s = 0)

=

1 + 0
2 + 1

=

(9.4.24)

(9.4.25)

(9.4.26)

@@

(9.4.27)

(9.4.28)

3
4

1
2

1
2

1
3

9.4.3 Learning multivariate discrete tables using a Dirichlet prior

The natural generalisation to our discussion of Bayesian learning of BNs is to consider variables that can
take more than two states. In this case the natural conjugate prior is given by the Dirichlet distribution,
which generalises the Beta distribution to more than two states. Again we assume throughout i.i.d. data
and the local and global parameter prior independencies. Since under the global parameter independence
assumption the posterior factorises over variables (as in equation (9.4.6)), we can concentrate on the posterior
of a single variable.

206

DRAFT April 24, 2015

Bayesian Belief Network Training

No parents

Let’s consider a variable v with dom(v) =
1, . . . , I
θ) = θi, the contribution to the posterior from a datapoint vn is
θi, i.e. p(v = i
|

{

}

. If we denote the probability of v being in state i by

p(vn

θ) =
|

I
(cid:89)

i=1

I[vn=i]
i

,

θ

I
(cid:88)

i=1

θi = 1

so that the posterior for θ given a dataset

= (cid:8)v1, . . . , vN (cid:9)

V

p(θ

)

|V

p(θ)

∝

N
(cid:89)

I
(cid:89)

n=1

i=1

I[vn=i]
i

θ

= p(θ)

I
(cid:89)

i=1

θ

i

(cid:80)N

n=1

I[vn=i]

It is convenient to use a Dirichlet prior distribution with hyperparameters u

p(θ)= Dirichlet (θ

u)
|

∝

I
(cid:89)

i=1

θui−1
i

Using this prior the posterior becomes

p(θ

)

|V

∝

I
(cid:89)

i=1

θui−1
i

I
(cid:89)

i=1

θ

i

(cid:80)N

n=1

I[vn=i]

=

I
(cid:89)

i=1

ui−1+(cid:80)N
i

n=1

θ

I[vn=i]

which means that the posterior is given by

p(θ

|V

) = Dirichlet (θ

u + c)
|

where c is a count vector with components

ci =

N
(cid:88)

n=1

I [vn = i]

being the number of times state i was observed in the training data.

The marginal table is given by integrating

p(v = i

|V

) =

(cid:90)

θ

θ)p(θ
p(v = i
|

|V

) =

(cid:90)

θip(θi

)

|V

θi

The single-variable marginal distribution of a Dirichlet is a Beta distribution,



p(θi

|V

) = B

θi

ui + ci,
|



uj + cj

 .

(cid:88)

j(cid:54)=i

The marginal table is then given by the mean of the Beta distribution

p(v = i

|V

) =

ui + ci
j uj + cj

(cid:80)

which generalises the binary state formula equation (9.4.16).

Parents

(9.4.29)

(9.4.30)

(9.4.31)

(9.4.32)

(9.4.33)

(9.4.34)

(9.4.35)

(9.4.36)

(9.4.37)

To deal with the general case of a variable v with parents pa (v) we denote the probability of v being in
state i, conditioned on the parents being in state j as

p(v = i
|

pa (v) = j, θ) = θi(v; j)

DRAFT April 24, 2015

(9.4.38)

207

Bayesian Belief Network Training

a
1
1
0
0
1
0
1

s
1
0
1
1
1
0
0

c
2
0
1
0
2
0
1

Figure 9.9: A database of patient records about Asbestos exposure (1 signiﬁes ex-
posure), being a Smoker (1 signiﬁes the individual is a smoker), and lung Cancer (0
signiﬁes no cancer, 1 signiﬁes early stage cancer, 2 signiﬁes late state cancer). Each
row contains the information for each of the seven individuals in the database.

where (cid:80)
the number of parental states S will be exponential in K.

i θi(v; j) = 1. This forms the components of a vector θ(v; j). Note that if v has K parents then

Writing θ(v) = [θ(v; 1), . . . , θ(v; S)], local (parental state) independence means

p(θ(v)) =

p(θ(v; j))

(cid:89)

j

and global independence means

p(θ) =

(cid:89)

v

p(θ(v))

(9.4.39)

(9.4.40)

where θ = (θ(v), v = 1, . . . , V ) represents the combined table of all the variables.

Parameter posterior

Thanks to the global parameter independence assumption the posterior factorises, with one posterior table
per variable. Each posterior table for a variable v depends only on the data
(v) of the family of the
variable. Assuming a Dirichlet distribution prior

D

p(θ(v; j)) = Dirichlet (θ(v; j)

u(v; j))
|

the posterior is also Dirichlet

p(θ(v)

|D

(v)) =

(cid:89)

j

u(cid:48)(v; j)(cid:1)
Dirichlet (cid:0)θ(v; j)
|

where the hyperparameter prior term is updated by the observed counts,

u(cid:48)
i(v; j)

≡

ui(v; j) + (cid:93) (v = i, pa (v) = j) .

By analogy with the no-parents case, the marginal table is given by
u(cid:48)
i(v; j).

p(v = i

(v))

pa (v) = j,
|

D

∝

(9.4.41)

(9.4.42)

(9.4.43)

(9.4.44)

Example 9.4. Consider the p(c
a, s)p(s)p(a) asbestos example with dom(a) = dom(s) =
|
now with the variable c taking three states, dom(c) =
ﬁg(9.9). The marginal table under a Dirichlet prior is then given by, for example

, except
, accounting for diﬀerent kinds of cancer, see

0, 1, 2
{

0, 1
}

{

}

a = 1, s = 1,
p(c = 0
|

V

) =

u0(a = 1, s = 1) + (cid:93) (c = 0, a = 1, s = 1)
i∈{0,1,2} ui(a = 1, s = 1) + (cid:93) (c = i, a = 1, s = 1)

(cid:80)

Assuming a ﬂat Dirichlet prior, which corresponds to setting all components of u to 1, this gives

a = 1, s = 1,
p(c = 0
|

V

) =

a = 1, s = 1,
p(c = 1
|

V

) =

1 + 0
3 + 2

=

1 + 0
3 + 2

=

1
5

1
5

a = 1, s = 1,
p(c = 2
|

3
5
a = 1, s = 0), p(c
and similarly for the other three tables p(c
|
|

1 + 2
3 + 2

) =

=

V

a = 1, s = 1).
a = 0, s = 1), p(c
|

(9.4.45)

(9.4.46)

(9.4.47)

(9.4.48)

208

DRAFT April 24, 2015

Structure learning

Algorithm 9.1 PC algorithm for skeleton learning.

1: Start with a complete undirected graph G on the set
2: i = 0
3: repeat
4:

for x

do
Adj

∈ V
for y

{

x

∈

do
Determine if there is a subset
x
⊥⊥
end for

y
| S

}

S

. If this set exists remove the x

5:

6:

7:

8:

end for
i = i + 1.
9:
10: until all nodes have

i neighbours.

≤

of all vertices.

V

of size i of the neighbours of x (not including y) for which

y link from the graph G and set

xy =

S

.

S

−

Model likelihood

For a variable v, and i.i.d. data

(cid:90)

pa (vn)) =

(cid:89)

p(vn

n

|

p(θ(v))

p(vn

pa (vn) , θ(v))
|

{
(cid:89)

n

(v) =

D

(vn

pa (vn)), n = 1, . . . , N
|

}

for the family of this variable,

θ(v)

(cid:90)




(cid:89)

θ(v)



j

1
Z(u(v; j))

(cid:89)

i

θi(v; j)ui(v;j)−1

(9.4.49)






(cid:89)

(cid:89)

(cid:89)

n

j

i

θi(v; j)

I[vn=i,pa(vn)=j]

(9.4.50)

θi(v; j)ui(v;j)−1+(cid:93)(v=i,pa(v)=j)

(cid:90)

(cid:89)

θ(v;j)

i

(cid:89)

j

(cid:89)

j

1
Z(u(v; j))

Z(u(cid:48)(v; j))
Z(u(v; j))

(9.4.51)

(9.4.52)

=

=

=

where Z(u) is the normalisation constant of a Dirichlet distribution with hyperparameters u; u(cid:48) is as given
in equation (9.4.43).

For a belief network on variables v = (v1, . . . , vD) the joint probability of all variables factorises into the
local probabilities of each variable conditioned on its parents. The likelihood of a complete set of i.i.d. data

= (cid:8)v1, . . . , vN (cid:9) is then given by:

D

) =

p(

D

(cid:89)

(cid:89)

k

n

p(vn
k |

pa (vn

k )) =

(cid:89)

(cid:89)

k

j

Z(u(cid:48)(vk; j))
Z(u(vk; j))

(9.4.53)

where u(cid:48) is given by equation (9.4.43). Expression (9.4.53) can be written explicitly in terms of Gamma
functions, see exercise(9.9). In the above expression in general the number of parental states diﬀers for each
variable vk, so that implicit in the above formula is that the state product over j goes from 1 to the number
of parental states of variable vk. Due to the local and global parameter independence assumptions, the
logarithm of the model likelihood is a product of terms, one for each variable vk and parental conﬁguration
j. This is called the likelihood decomposable property.

9.5 Structure learning

Up to this point we have assumed that we are given both the structure of the distribution and a dataset
. A more complex task is when we need to learn the structure of the network as well. We’ll consider the
D
case in which the data is complete (i.e. there are no missing observations). Since for D variables, there is
an exponentially large number (in D) of BN structures, it’s clear that we cannot search over all possible
structures. For this reason structure learning is a computationally challenging problem and we must rely on
constraints and heuristics to help guide the search. Whilst in general structure learning is intractable, a cele-
brated tractable special case is when the network is constrained to have at most one parent, see section(9.5.4).

DRAFT April 24, 2015

209

Structure learning

For all but the sparsest networks, estimating the dependencies to any accuracy requires a large amount of
data, making testing of dependencies diﬃcult. Consider the following simple situation of two independent
(xn, yn), n = 1, . . . , N
variables, p(x, y) = p(x)p(y). Based on a ﬁnite sample from this joint distribution
we want to try to understand if x is independent of y. One way to do this is to compute the empirical mutual
information I(x, y); if this is zero then, empirically, x and y are independent. However, for a ﬁnite amount
of data, two variables will typically have non-zero mutual information, so that a threshold needs to be set
to decide if the measured dependence is signiﬁcant under the ﬁnite sample, see section(9.5.2).

=

D

{

,
}

Other complexities arise from the concern that a Belief or Markov Network on the visible variables alone
may not be a parsimonious way to represent the observed data if, for example, there may be latent variables
which are driving the observed dependencies. We will not enter into such issues in our discussion here
and limit the presentation to two central approaches, one which attempts to make a network structure
consistent with local empirical dependencies (the PC algorithm), and one which builds a structure that is
most probable for the global data (network scoring).

9.5.1 PC algorithm

The PC algorithm[274] ﬁrst learns the skeleton of a graph, after which edges may be oriented to form a
(partially oriented) DAG. The procedure to learn the skeleton is based on using the empirical data to test if
two variables are independent. A variety of approaches can be used to ascertain independence, as described
in section(9.5.2).

−

The PC algorithm begins at the ﬁrst round with a complete skeleton G and attempts to remove as many
. If an x and y pair are deemed independent
links as possible. At the ﬁrst step we test all pairs x
y is removed from the complete graph. One repeats this for all the pairwise links. In the
then the link x
second round, for the remaining graph, one examines each x
y link and conditions on a single neighbour
z of x. If x
y. One repeats in this way through all the variables. At each
z then remove the link x
round the number of neighbours in the conditioning set is increased by one. See algorithm(9.1), ﬁg(9.10)1
and demoPCoracle.m. A reﬁnement of this algorithm, known as NPC for necessary path PC[276] limits the
number of independence checks to remove inconsistencies resulting from the empirical estimates of condi-
tional mutual information.

⊥⊥

⊥⊥

| ∅

−

−

y

y

|

Given a learned skeleton, a partial DAG can be constructed using algorithm(9.2). Note that this is necessary
since the undirected graph G is a skeleton – not a belief network of the independence assumptions discovered.
y link was removed on the basis
For example, we may have a graph G with x
x
y, although this is inconsistent
z
∅
(cid:62)(cid:62)
with the discovery in the ﬁrst round x
y. This is the reason for the orientation part: for consistency, we
must have x

y in which the x
z
−
y (graphically) implies x

z, see example(9.5). See also ﬁg(9.11).

. As a MN the graph x

y, for which x

y and x

| ∅ → S

xy =

−
−

⊥⊥

⊥⊥

−

−

y

z

→

←

⊥⊥

y
|

(cid:62)(cid:62)

Example 9.5 (Skeleton orienting).

z

z

x

x

y

y

x

y
| ∅ ⇒

⊥⊥

x

z

y
|

⊥⊥

⇒

z

z

x

x

y

y

If x is (unconditionally) independent of y, it must
be that z is a collider since otherwise marginalis-
ing over z would introduce a dependence between
x and y.

If x is independent of y conditioned on z, z must
not be a collider. Any other orientation is ap-
propriate.

1This example appears in [161] and [223] – thanks also to Seraf´ın Moral for his online notes.

210

DRAFT April 24, 2015

Structure learning

x

z

x

z

t

(a)

t

(j)

y

x

y

x

y

x

y

x

y

x

y

x

y

x

y

x

w

z

w

z

w

z

w

z

w

z

w

z

w

z

w

z

t

(b)

t

(c)

t

(d)

t

(e)

t

(f)

t

(g)

t

(h)

y

x

y

x

y

x

y

x

y

x

y

x

y

x

y

x

w

z

w

z

w

z

w

z

w

z

w

z

w

z

w

z

t

(k)

t

(l)

t

(m)

t

(n)

t

(o)

t

(p)

t

(q)

y

w

y

w

t

(i)

t

(r)

Figure 9.10: PC algorithm.
conditional independence tests will be performed.
In the ﬁrst round (i = 0) all the pairwise mutual informations x
x and y removed if deemed independent (green line).
on three variables x, y, z of the remaining graph, removing the link x
are shown.
(when i gets incremented to 3) since there are no nodes with 3 or more neighbours.
During this process the sets Sx,y =
z, w
, Sx,w =
{
demoPCoracle.m

(a): The BN from which data is assumed generated and against which
(c-l):
are checked, and the link between
(m-o): i = 1. We now look at connected subsets
z is true. Not all steps
y
. The algorithm terminates after this round
(r): Final skeleton.
were found. See also

(b): The initial skeleton is fully connected.

(p,q): i = 2. We now examine all x

, Sz,w = y, Sx,t =

a, b
}

, Sy,t =

y if x

z, w

⊥⊥

⊥⊥

⊥⊥

| {

| ∅

−

∅

}

∅

{

}

y

y

|

Algorithm 9.2 Skeleton orientation algorithm (returns a DAG).

1: Unmarried Collider: Examine all undirected links x
2: repeat
3:

x
z
−
→
For x
−
If for x

x

⇒

y
y, if there is a directed path from x to y orient x
w, z

y there is a w such that x

w, y

→

→

4:

y

z

z

y

→
−

z

−

−

y. If z

xy set x

(cid:54)∈ S

z

→

←

y.

5:
−
6: until No more edges can be oriented.
7: The remaining edges can be arbitrarily oriented provided that the graph remains a DAG and no additional

→

→

→

−

w then orient z

w

colliders are introduced.

Example 9.6. In ﬁg(9.10) we describe the processes of the PC algorithm in learning the structure for a
belief network on the variables x, y, z, w, t. In this case, rather than using data to assess independence, we
assume that we have access to an ‘oracle’ that can correctly answer any independence question put to it.
In practice, of course, we will not be so fortunate! Once the skeleton has been found, we then orient the
skeleton, as in ﬁg(9.11).

9.5.2 Empirical independence

Mutual information test

Given data we can obtain an estimate of the conditional mutual information by using the empirical distri-
bution p(x, y, z) estimated by simply counting occurrences in the data. In practice, however, we only have
a ﬁnite amount of data to estimate the empirical distribution. This means that for data sampled from a
distribution for which the variables truly are independent, the empirical mutual information will neverthe-
less typically be greater than zero. An issue therefore is what threshold to use for the empirical conditional
mutual information to decide if this is suﬃciently far from zero to be caused by dependence. A frequen-
tist approach is to compute the distribution of the conditional mutual information and then see where the

DRAFT April 24, 2015

211

Structure learning

z) is Chi-square distributed with (X

sample value is compared to the distribution. According to [178], under the null hypothesis that the vari-
ables are independent, 2N MI(x; y
1)Z degrees of freedom with
dim (x) = X, dim (y) = Y , dim (z) = Z. This can then be used to form a hypothesis test; if the sample value
of the empirical mutual information is ‘signiﬁcantly’ in the tails of the chi-square distribution, we deem that
the variables are conditionally dependent. This classical approach can work well for large amounts of data,
but is less eﬀective in the case of small amounts of data. An alternative pragmatic approach is to estimate
the threshold based on empirical samples of the MI under controlled independent/dependent conditions –
see demoCondindepEmp.m for a comparison of these approaches.

1)(Y

−

−

|

x

z

y

x

y

x

y

x

w

z

w

z

w

z

y

w

t

(a)

t

(b)

t

(c)

t

(d)

, Sy,t =

(a):
Figure 9.11: Skeleton orientation algorithm.
, Sz,w =
The skeleton along with Sx,y =
∅
Sx,y, so form
y, Sx,t =
(d): Final
collider.
partially oriented DAG. The remaining edge may be
oriented as desired, without violating the DAG condi-
tion. See also demoPCoracle.m.

(cid:54)∈
}
Sz,w, so form collider.

z, w
}
{
(c): t

∅
. (b): z

, Sx,w =

z, w

(cid:54)∈

{

Bayesian conditional independence test

A Bayesian approach to testing for independence can be made by comparing the likelihood of the data under
the independence hypothesis, versus the likelihood under the dependent hypothesis. For the independence
hypothesis, ﬁg(9.12a), we have a joint distribution over variables and parameters:

p(x, y, z, θ

indep) = p(x

z, θx|z)p(y

z, θy|z)p(z
|

θz)p(θx|z)p(θy|z)p(θz)
|

|

|H

(9.5.1)

For categorical distributions, it is convenient to use a prior Dirichlet (θ
local as well as global parameter independence. For a set of assumed i.i.d. data (
1, . . . , N , the likelihood is then given by integrating over the parameters θ:

u) on the parameters θ, assuming also
|
) = (xn, yn, zn) , n =

,

,

X

Y

Z

p(

,

,

Y

X

Z|H

indep) =

p(θ

θ

(cid:90)

indep)

|H

(cid:89)

n

p(xn, yn, zn

θ,
|

H

indep)

Thanks to conjugacy, this is straightforward and gives the expression

p(

,

,

Y

X

Z|H

indep) =

Z(uz + (cid:93) (z))
Z(uz)

(cid:89)

z

Z(ux|z + (cid:93) (x, z))
Z(ux|z)

Z(uy|z + (cid:93) (y, z))
Z(uy|z)

@@

(9.5.2)

where ux|z is a hyperparameter matrix of pseudo counts for each state of x given each state of z. Z(v) is
the normalisation constant of a Dirichlet distribution with vector parameter v.

For the dependent hypothesis, ﬁg(9.12b), we have

p(x, y, z, θ

dep) = p(x, y, z

|H

θx,y,z)p(θx,y,z)
|

The likelihood is then

p(

,

,

Y

X

Z|H

dep) =

Z(ux,y,z + (cid:93) (x, y, z))
Z(ux,y,z)

Assuming each hypothesis is equally likely, for a Bayes’ Factor

,

p(
X
p(
X

,

Y
,
Y

indep)
dep)

Z|H
,
Z|H

(9.5.3)

(9.5.4)

(9.5.5)

greater than 1, we assume that conditional independence holds; otherwise we assume the variables are con-
ditionally dependent. demoCondindepEmp.m suggests that the Bayesian hypothesis test tends to outperform
the conditional mutual information approach, particularly in the small sample size case, see ﬁg(9.13).

212

DRAFT April 24, 2015

Structure learning

θz

zn

xn

yn

N

θx|z

θy|z

θx,y,z

xn, yn, zn

N

(a)

(b)

indep for conditional independence x

Figure 9.12: Bayesian conditional independence test
(a): A model
using Dirichlet priors on the tables.
(b):
z.
H
A model
z.
y
dep for conditional dependence x
(cid:62)(cid:62)
By computing the likelihood of the data under each
model, a numerical score for the validity of the con-
ditional independence assumption can be formed. See
demoCondindepEmp.m.

⊥⊥

H

y

|

|

x

z1

z2

y

y

Figure 9.13: Conditional independence test of x
z1, z2 with x, y, z1, z2 having
3, 2, 4, 2 states respectively. From the oracle belief network shown, in each experiment
the tables are drawn at random and 20 examples are sampled to form a dataset. For
each dataset a test is carried out to determine if x and y are independent conditioned
on z1, z2 (the correct answer being that they are independent). Over 500 experiments,
the Bayesian conditional independence test correctly states that the variables are con-
ditionally independent 74% of the time, compared with only 50% accuracy using the
chi-square mutual information test. See demoCondindepEmp.m.

⊥⊥

|

9.5.3 Network scoring

k p(vk

An alternative to local methods such as the PC algorithm is to evaluate the whole network structure on
the set of variables v. That is we wish to ascertain how well a belief network with a particular structure
p(v) = (cid:81)
pa (vk)) ﬁts the data. In a probabilistic context, given a model structure M , we wish to
|
M )p(M ). Some care is needed here since we have to ﬁrst ‘ﬁt’ each model with
compute p(M
)
|D
parameters θ, p(v
. If we do this using maximum likelihood alone, with no constraints on
θ, we will always end up favouring that model M with the most complex structure (assuming p(M ) = const.).
This can be remedied by using the Bayesian technique

∝
θ, M ) to the data

D|

p(

D

|

p(

M ) =

D|

(cid:90)

p(

θ

θ, M )p(θ

D|

M )

|

(9.5.6)

In the case of directed networks, as we saw in section(9.4), the assumptions of local and global parameter
independence make the integrals tractable. For a discrete state network and Dirichlet priors, we have p(
M )
given explicitly by the Bayesian Dirichlet score equation (9.4.53). First we specify the hyperparameters
u(v; j), and then search over structures M , to ﬁnd the one with the best score p(
M ). The simplest
setting for the hyperparameters is set them all to unity[69]. Another setting is the ‘uninformative prior’[55]

D|

D|

ui(v; j) =

α
dim (v) dim (pa (v))

(9.5.7)

where dim (x) is the number of states of the variable(s) x, giving rise to the BDeu score, for an ‘equivalent
sample size’ parameter α. A discussion of these settings is given in [141] under the concept of likelihood
equivalence, namely that two networks which are Markov equivalent should have the same score. How dense
the resulting network is can be sensitive to α[278, 266, 277]. Including an explicit prior p(M ) on the networks
to favour those with sparse connections is also a sensible idea, for which one considers the modiﬁed score
p(

M )p(M ).

D|

Searching over structures is a computationally demanding task. However, since the log-score decomposes
into additive terms involving only the family of each variable v, we can compare two networks diﬀering in a
single edge eﬃciently since when we make an adjustment within a family, no other terms outside the family

DRAFT April 24, 2015

213

Structure learning

x1

x3

x6

x5

x2

x4

x7

x1

x3

x6

x2

x4

x7

x8

x8

x5

x5

x1

x3

x6

x2

x4

x7

x8

(a)

(b)

(c)

(a): The correct structure in which all
Figure 9.14: Learning the structure of a Bayesian network.
variables are binary. The ancestral order is x2, x1, x5, x4, x3, x8, x7, x6. The dataset is formed from 1000
(b): The learned structure based on the PC algorithm using the Bayesian
samples from this network.
empirical conditional independence test. Undirected edges may be oriented arbitrarily (provided the graph
(c): The learned structure based on the Bayes Dirichlet network scoring method. See
remains acyclic).
demoPCdata.m and demoBDscore.m.

are aﬀected. This means that, given a candidate family, we can ﬁnd which parents in this family should
connect to the child; this computation can be carried out for all families independently. To help ﬁnd the
best families, search heuristics based on local addition/removal/reversal of edges [69, 141] that increase the
score are popular[141]. In learnBayesNet.m we simplify the problem for demonstration purposes in which
we assume we know the ancestral order of the variables, and also the maximal number of parents of each
variable. In practice, it is unlikely that a large number of parents will inﬂuence a variable; even if this were
the case, we would in general require an amount of data exponential in the number of parents to ascertain
this. One could in principle approach this by assuming parametric forms for such large tables, although this
is not common in practice.

Example 9.7 (PC algorithm versus network scoring). In ﬁg(9.14) we compare the PC algorithm with
network scoring based (with Dirichlet hyperparameters set to unity) on 1000 samples from a known belief
network. The PC algorithm conditional independence test is based on the Bayesian factor (9.5.5) in which
Dirichlet priors with u = 0.1 were used throughout.

In the network scoring approach, in general there is no need to assume an ancestral ordering. However,
here we assume that we know the correct ancestral ordering, and also limit the number of parents of each
variable to be at most two.
In this case, we can then easily search over all possible graph structures,
choosing that with the highest posterior score. This proceeds by for example looking at the contribution of
variable x7 and its family. Since, according to the given ancestral ordering, x1, x2, x3, x4, x5, x8 are possible
parents of x7, in principle we need to search over all the 26 parental conﬁgurations for this family. However,
since we assume that there are only maximally two parents, this reduces to 1 + (cid:0)6
(cid:1) = 22 parental
(cid:1) + (cid:0)6
2
1
conﬁgurations. We can perform this optimisation for the parental structure of variable x7 independently of
the parental structure of the other variables, thanks to the likelihood decomposable property of the network
score. Similarly, we carry out this optimisation for the other variables separately, based on their possible
parents according to the ancestral order.

In ﬁg(9.14) the network scoring technique outperforms the PC algorithm. This is partly explained by the
network scoring technique being provided with the correct ancestral order and the constraint that each
variable has maximally two parents.

214

DRAFT April 24, 2015

Structure learning

Algorithm 9.3 Chow-Liu Trees

1: for i = 1 to D do
2:

for j = 1 to D do

3:

Compute the mutual information for the pair of variables xi, xj: wij = MI(xi; xj).

end for

4:
5: end for
6: For the undirected graph
G
7: Choose an arbitrary variable as the root node of the tree
8: Form a directed tree by orienting all edges away from the root node.

T

.

with edge weights w, ﬁnd a maximum weight undirected spanning tree

.

T

9.5.4 Chow-Liu Trees

Consider a multivariate distribution p(x) that we wish to approximate with a distribution q(x). Furthermore,
we constrain the approximation q(x) to be a Belief Network in which each node has at most one parent,
see ﬁg(9.15). First we assume that we have chosen a particular labelling of the D variables so that children
have higher parent indices than their parents. The DAG single parent constraint then means

q(x) =

D
(cid:89)

i=1

q(xi

xpa(i)),
|

pa(i) < i, or pa(i) =

∅

(9.5.8)

where pa(i) is the single parent index of node i. To ﬁnd the best approximating distribution q in this
constrained class, we may minimise the Kullback-Leibler divergence

KL(p
|

q) =

log p(x)
(cid:104)

(cid:105)p(x) −

D
(cid:88)

i=1

(cid:10)log q(xi

xpa(i))(cid:11)
|

p(xi,xpa(i))

(9.5.9)

Since p(x) is ﬁxed, the ﬁrst term is constant. By adding a term (cid:10)log p(xi
p(x) alone, we can write

|

xpa(i))(cid:11)

p(xi,xpa(i)) that depends on

q) = const.
KL(p
|

−

D
(cid:88)

i=1

(cid:68)(cid:10)log q(xi

xpa(i))(cid:11)
|

p(xi|xpa(i)) −

(cid:10)log p(xi

|

xpa(i))(cid:11)

p(xi|xpa(i))

(cid:69)

p(xpa(i))

(9.5.10)

This enables us to recognise that, up to a negligible constant, the overall Kullback-Leibler divergence is a
positive sum of individual Kullback-Leibler divergences so that the optimal setting is therefore

q(xi

xpa(i)) = p(xi
|

xpa(i))
|

(9.5.11)

Plugging this solution into equation (9.5.9) and using log p(xi
obtain

xpa(i)) = log p(xi, xpa(i))
|

−

log p(xpa(i)) we

KL(p
|

q) = const.

D
(cid:88)

i=1

−

(cid:10)log p(xi, xpa(i))(cid:11)

p(xi,xpa(i)) +

D
(cid:88)

i=1

(cid:10)log p(xpa(i))(cid:11)

p(xpa(i))

(9.5.12)

We still need to ﬁnd the optimal parental structure pa(i) that minimises the above expression. If we add
and subtract an entropy term we can write

KL(p
|

q) =

−

D
(cid:88)

i=1

(cid:10)log p(xi, xpa(i))(cid:11)

p(xi,xpa(i)) +

D
(cid:88)

i=1

(cid:10)log p(xpa(i))(cid:11)

p(xpa(i)) +

D
(cid:88)

i=1

log p(xi)
(cid:104)

(cid:105)p(xi)

D
(cid:88)

i=1

−

log p(xi)
(cid:104)

(cid:105)p(xi) + const.

(9.5.13)

x1

x2

x3

x4

Figure 9.15: A Chow-Liu Tree in which each variable xi has at most one
parent. The variables may be indexed such that 1

D.

i

≤

≤

DRAFT April 24, 2015

215

Structure learning

For two variables xi and xj and distribution p(xi, xj), the mutual information deﬁnition(8.13) can be written
as

(cid:28)

MI(xi; xj) =

log

(cid:29)

p(xi, xj)
p(xi)p(xj)

p(xi,xj )

(9.5.14)

which can be seen as the Kullback-Leibler divergence KL(p(xi, xj)
Using this, equation (9.5.13) is

p(xi)p(xj)) and is therefore non-negative.
|

KL(p

q) =
|

−

D
(cid:88)

i=1

MI(cid:0)xi; xpa(i)

(cid:1)

D
(cid:88)

i=1

−

log p(xi)
(cid:104)

(cid:105)p(xi) + const.

(9.5.15)

Since our task is to ﬁnd the optimal parental indices pa(i), and the entropic term (cid:80)
(cid:105)p(xi) of
the ﬁxed distribution p(x) is independent of this mapping, ﬁnding the optimal mapping is equivalent to
maximising the summed mutual informations

log p(xi)

i (cid:104)

D
(cid:88)

i=1

MI(cid:0)xi; xpa(i)

(cid:1)

(9.5.16)

under the constraint that pa(i) < i. Since we also need to choose the optimal initial labelling of the variables
as well, the problem is equivalent to computing all the pairwise mutual informations

wij = MI(xi; xj)

(9.5.17)

and then ﬁnding a maximal spanning tree for the graph with edge weights w (see spantree.m)[64]. Once
found, we need to identify a directed tree with at most one parent. This is achieved by choosing any node
and then orienting edges consistently away from this node.

Maximum likelihood Chow-Liu trees

If p(x) is the empirical distribution

p(x) =

1
N

N
(cid:88)

n=1

δ (x, xn)

then

KL(p

q) = const.
|

−

1
N

(cid:88)

n

log q(xn)

(9.5.18)

(9.5.19)

Hence the distribution q that minimises KL(p
data. This means that if we use the mutual information found from the empirical distribution, with

q) is equivalent to that which maximises the likelihood of the
|

p(xi = a, xj = b)

∝

(cid:93) (xi = a, xj = b)

(9.5.20)

then the Chow-Liu tree produced corresponds to the maximum likelihood solution amongst all single-parent
trees. An outline of the procedure is given in algorithm(9.3). An eﬃcient algorithm for sparse data is also
available[205].

Remark 9.1 (Learning Tree structured Belief Networks). The Chow-Liu algorithm pertains to the discus-
sion in section(9.5) on learning the structure of Belief networks from data. Under the special constraint that
each variable has at most one parent, the Chow-Liu algorithm returns the maximum likelihood structure to
ﬁt the data.

216

DRAFT April 24, 2015

Maximum Likelihood for Undirected models

9.6 Maximum Likelihood for Undirected models

Consider a Markov network p(
clique parameters θ = (θ1, . . . , θC)

X

) deﬁned on (not necessarily maximal) cliques

θ) =

p(

X |

1
Z(θ)

(cid:89)

c

φc(

θc)

c
X

|

The term

Z(θ) =

(cid:88)

(cid:89)

X

c

φc(

c
X

θc)
|

c
X

⊆ X

, c = 1, . . . , C with

(9.6.1)

(9.6.2)

ensures normalisation, with the notation (cid:80)

. Given a set of data,

X

n, n = 1, . . . , N

}

{X

X indicating a summation over all states of the set of variables
, and assuming i.i.d. data, the log likelihood is

L(θ)=

(cid:88)

n

log p(

X

n

θ) =
|

(cid:88)

(cid:88)

n

c

log φc(

n
c |

X

θc)

N log Z(θ)

−

(9.6.3)

Our interest is to ﬁnd the parameters that maximise the log likelihood L(θ). In general learning the optimal
parameters θc, c = 1, . . . , C is awkward since they are coupled via Z(θ). Unlike the BN, the objective
function does not split into a set of isolated parameter terms and in general we need to resort to numerical
methods. In special cases, however, exact results still apply, in particular when the MN is decomposable
and no constraints are placed on the form of the clique potentials, as we discuss in section(9.6.3). More
generally, however, gradient based techniques may be used and also give insight into properties of the
maximum likelihood solution.

9.6.1 The likelihood gradient

The gradient of the log likelihood with respect to a clique parameter θc is given by

∂
∂θc

L(θ) =

(cid:88)

n

∂
∂θc

log φc(

n
c |

X

θc)

(cid:28) ∂
∂θc

N

−

(cid:29)

log φc(

X

θc)

c

|

.

p(Xc|θ)

This is obtained by using the result

∂
∂θc

log Z(θ) =

1
Z(θ)

(cid:88)

X

∂
∂θc

φc(

X

c

θc)
|

(cid:89)

c(cid:48)(cid:54)=c

φc(cid:48)(

X

c(cid:48)

θc(cid:48)) =
|

(cid:28) ∂
∂θc

(cid:29)

log φc(

c
X

θc)
|

.

p(Xc|θ)

The gradient can then be used as part of a standard numerical optimisation package.

Exponential form potentials

A common form of parameterisation is to use an exponential form

(cid:16)

φc(

X

c) = exp

θT
c ψc(

c)

X

(cid:17)

(9.6.4)

(9.6.5)

(9.6.6)

where θc are the vector parameters and ψc(
c. From equation (9.6.4) to ﬁnd the gradient, we need

X

c) is a ﬁxed ‘feature function’ deﬁned on the variables of clique

∂
∂θc

log φc(

c
X

θc) =
|

∂
∂θc

θT
c ψc(

X

c) = ψc(

c).

X

Using this in equation (9.6.4), we ﬁnd that the L(θ) has a zero derivative when

1
N

(cid:88)

n

ψc(

n
c ) =

X

ψc(
(cid:104)

X

c)

(cid:105)p(Xc) .

DRAFT April 24, 2015

(9.6.7)

(9.6.8)

217

Maximum Likelihood for Undirected models

Hence the maximum likelihood solution satisﬁes that the empirical average of a feature function matches
the average of the feature function with respect to the model. By deﬁning the empirical distribution on the
clique variables

c as

X

c)

(cid:15)(

X

≡

1
N

N
(cid:88)

n=1

I [

X

c =

n
c ]

X

we can write equation (9.6.8) more compactly as

ψc(
(cid:104)

X

(cid:105)(cid:15)(Xc) =
c)

ψc(
(cid:104)

c)

(cid:105)p(Xc) .

X

(9.6.9)

(9.6.10)

An example of learning such an exponential form is given in example(9.8). We return to learning the
parameters of these models in section(9.6.4).

Example 9.8 (Boltzmann Machine learning). We deﬁne the BM as

W) =

p(v

|

1
Z(W)

1

2 vTWv,

e

Z(W) =

(cid:88)

1
2 vTWv

e

v

(9.6.11)

for symmetric W and binary variables dom(vi) =
the log likelihood is

0, 1
{

}

. Given a set of training data,

= (cid:8)v1, . . . , vN (cid:9),

D

L(W) =

1
2

N
(cid:88)

n=1

(vn)T Wvn

N log Z(W)

−

Diﬀerentiating w.r.t. wij, i

= j and wii we have the gradients

∂L
∂wij

=

N
(cid:88)

(cid:16)

n=1

i vn
vn

j − (cid:104)

vivj

(cid:105)p(v|W)

(cid:17)

,

∂L
∂wii

=

1
2

N
(cid:88)

(cid:16)

n=1

vn
i − (cid:104)

vi

(cid:105)p(v|W)

(cid:17)

.

A simple algorithm to optimise the weight matrix W is to use gradient ascent,

wnew

ij = wold

ij + η1

∂L
∂wij

,

wnew

ii = wold

ii + η2

∂L
∂wii

(9.6.12)

(9.6.13)

(9.6.14)

for learning rates η1, η2 > 0. The intuitive interpretation is that learning will stop (the gradient is zero) when
(cid:105)p(v|W) match those of the empirical distribution, (cid:80)
j /N .
the second order statistics of the model
BM learning however is diﬃcult since
(cid:105)p(v|W) is typically computationally intractable for an arbitrary
interaction matrix W and therefore needs to be approximated. Indeed, one cannot compute the likelihood
L(W) exactly for a general matrix W so that monitoring performance is also diﬃcult.

vivj
(cid:104)
vivj
(cid:104)

n vn

i vn

9.6.2 General tabular clique potentials

For unconstrained clique potentials we have a separate table for each of the states deﬁned on the clique. In
writing the log likelihood, it is convenient to use the identity

n
c ) =

φc(

X

(cid:89)

Yc

φc(

Y

I[Yc=X n
c ]

c)

(9.6.15)

where the product is over all states of potential c. This expression follows since the indicator is zero for all
but the single observed state

n
c . The log likelihood is then

X

(cid:88)

(cid:88)

(cid:88)

L(θ) =

c

Yc

n

I [

Y

c =

218

n
c ] log φc(

X

Y

N log Z(φ)

c)

−

(9.6.16)

DRAFT April 24, 2015

(cid:54)
Maximum Likelihood for Undirected models

where

Z(φ) =

(cid:88)

(cid:89)

Yc

c

φc(

Y

c)

Diﬀerentiating the log likelihood with respect to a speciﬁc table entry φc(

Y

c) we obtain

∂
∂φc(

c)

Y

L(θ) =

(cid:88)

n

I [

Y

c =

n
c ]

X

1
φc(

Y

N

c) −

c)
c)

p(
Y
φc(
Y

(9.6.17)

(9.6.18)

Equating to zero, and rewriting in terms of the variables
when

X

, the maximum likelihood solution is obtained

p(

X

c) = (cid:15)(

c)

X

(9.6.19)

c) matches the empirical distribution on each clique (cid:15)(

where the empirical distribution is deﬁned in equation (9.6.9). That is, the unconstrained optimal maximum
likelihood solution is given by setting the clique potentials such that the marginal distribution on each clique
p(
c). Note that this only describes the form that
the optimal maximum likelihood solution should take, and doesn’t give us a closed form expression for
setting the tables. To ﬁnd the optimal tables in this case would still require a numerical procedure, such as
gradient based methods based on equation (9.6.18), or the IPF method described below.

X

X

Iterative proportional ﬁtting

According to the general result of equation (9.6.19) the maximum likelihood solution is such that the clique
marginals match the empirical marginals. Assuming that we can absorb the normalisation constant into an
arbitrarily chosen clique, we can drop explicitly representing the normalisation constant. For a clique c, the
requirement that the marginal of p matches the empirical marginal on the variables in the clique is

φ(

c)

X

(cid:88)

(cid:89)

X\c

d(cid:54)=c

φ(

X

d) = (cid:15)(

c)

X

(9.6.20)

Given an initial setting for the potentials we can then update φ(

X

c) to satisfy the above marginal requirement,

φnew(

c) =

X

(cid:80)

X\c

(cid:15)(
(cid:81)

c)
X
d(cid:54)=c φ(

d)

X

which is required for each of the states of
is equivalent to ascertaining if

φ(

φnew(

c) =

X

c)(cid:15)(
X
X
c)
p(

c)

X

X

c. By multiplying and dividing the right hand side by φ(

c) this

(9.6.21)

X

(9.6.22)

This is a so-called Iterative Proportional Fitting (IPF) update and corresponds to a coordinate-wise optimi-
sation of the log likelihood in which the coordinate corresponds to φc(
c), with all other parameters ﬁxed.
In this case this conditional optimum is analytically given by the above setting. One proceeds by selecting
another potential to update, and continues updating until some convergence criterion is met. Note that in
general, with each update, the marginal p(
c) needs to be recomputed; computing these marginals may be
expensive unless the width of the junction tree formed from the graph is suitably limited.

X

X

9.6.3 Decomposable Markov networks

Whilst for general Markov networks we require numerical methods to ﬁnd the maximum likelihood solution,
there is an important special case for which we can ﬁnd the optimal tables very easily. If the MN corre-
sponding is decomposable, then we know (from the junction tree representation) that we can express the
distribution in the form of a product of local marginals divided by the separator distributions

) =

p(

X

(cid:81)
(cid:81)

c p(
s p(

c)
s)

X
X

DRAFT April 24, 2015

(9.6.23)

219

Maximum Likelihood for Undirected models

φ (x1, x2)

φ (x2)

φ (x2, x3, x5)

φ (x5)

φ (x5, x6)

x1

x2

x3

x4

(a)

x5

x6

φ (x2, x5)

φ (x2, x4, x5)

(b)

x1

x2, x3, x5

x6

x4

(c)

(a): A decomposable Markov network.

(c): Set chain for
Figure 9.16:
(a) formed by choosing clique x2, x3, x5 as root and orienting edges consistently away from the root. Each
separator is absorbed into its child clique to form the set chain.

(b): A junction tree for (a).

Algorithm 9.4 Learning of an unconstrained decomposable Markov network using maximum likelihood.
c), c = 1, . . . , C and the empirical
We have a triangulated (decomposable) Markov network on cliques φc(
marginal distributions on all cliques and separators, (cid:15)(

X

c), (cid:15)(

s)

X

X

1: Form a junction tree from the cliques.
2: Initialise each clique φc(
3: Choose a root clique on the junction tree and orient edges consistently away from this root.
4: For this oriented junction tree, divide each clique by its parent separator.
5: Return the new potentials on each clique as the maximum likelihood solution.

c) and each separator φs(

s) to (cid:15)(

c) to (cid:15)(

s).

X

X

X

X

By reabsorbing the separators into the numerator terms, we can form a set chain distribution, section(6.8)

) =

p(

X

(cid:89)

c

p(

X

c

|X\c)

(9.6.24)

Since this is directed, and provided no constraint is placed on the tables, the maximum likelihood solution
|X\c) based on counting the instances
to learning the tables is given by assigning each set chain factor p(
in the dataset[182], see learnMarkovDecom.m. The procedure is perhaps best explained by an example, as
given below. See algorithm(9.4) for a general description.

X

c

n, n = 1, . . . , N
Example 9.9. Given a dataset
}
wish to ﬁt by maximum likelihood a MN of the form

{X

, with corresponding empirical distribution (cid:15)(

X

), we

p(x1, . . . , x6) =

1
Z

φ(x1, x2)φ(x2, x3, x5)φ(x2, x4, x5)φ(x5, x6)

(9.6.25)

where the potentials are unconstrained tables, see ﬁg(9.16a). Since the graph is decomposable, we know it
admits a factorisation of clique potentials divided by the separators:

p(x1, . . . , x6) =

p(x1, x2)p(x2, x3, x5)p(x2, x4, x5)p(x5, x6)
p(x2)p(x2, x5)p(x5)

(9.6.26)

We can convert this to a set chain by reabsorbing the denominators into numerator terms, see section(6.8).
For example, by choosing the clique x2, x3, x5 as root, we can write

p(x1, . . . , x6) = p(x1

x2)
|
(cid:124) (cid:123)(cid:122) (cid:125)
φ(x1,x2)

p(x2, x3, x5)
(cid:123)(cid:122)
(cid:125)
(cid:124)
φ(x2,x3,x5)

p(x4
(cid:124)

x2, x5)
|
(cid:123)(cid:122)
(cid:125)
φ(x2,x4,x5)

x5)
p(x6
|
(cid:124) (cid:123)(cid:122) (cid:125)
φ(x5,x6)

(9.6.27)

where we identiﬁed the factors with clique potentials, and the normalisation constant Z is unity, see
ﬁg(9.16b). The advantage is that in this representation, the clique potentials are independent since the
distribution is a BN on cluster variables. The log likelihood for an i.i.d. dataset is

L =

(cid:88)

n

log p(xn
1 |

2 ) + log p(xn
xn

2 , xn

3 , xn

5 ) + log p(xn
4 |

2 , xn
xn

5 ) + log p(xn
6 |

xn
5 )

(9.6.28)

220

DRAFT April 24, 2015

Maximum Likelihood for Undirected models

x3

x2

x4

x1

(a)

x5

φ4,5φ1,5

x1,4

φ1,2φ1,4

x2,4

φ2,3φ2,4φ3,4

(b)

9.17:

Interpreted as

(a):
the dis-
Figure
represents
tribution φ(x1, x4, x5)φ(x1, x2, x4)φ(x2, x4, x3).
a
(b): A junction tree for the pairwise
φ(x4, x5)φ(x1, x4)φ(x4, x5)φ(x1, x2)φ(x2, x4)φ(x2, x3)φ(x3, x4).
MN in (a). We have a choice where to place the pairwise cliques, and this is one valid choice, using the
shorthand φa,b = φa,b(xa, xb) and xa,b =

a Markov network,

graph represents

pairwise MN,

xa, xb

graph

the

the

As

{

.
}

where each of the terms is an independent parameter of the model. The maximum likelihood solution then
corresponds (as for the BN case) to simply setting each factor to the empirical distribution

φ(x1, x2) = (cid:15)(x1

x2), φ(x2, x3, x5) = (cid:15)(x2, x3, x5), φ(x2, x4, x5) = (cid:15)(x4
|

x2, x5), φ(x5, x6) = (cid:15)(x6
|

x5)
|
(9.6.29)

Constrained decomposable Markov networks

If there are no constraints on the forms of the maximal clique potentials of the Markov network, as we’ve
seen, learning is straightforward. Here our interest is when the functional form of the maximal clique is
constrained to be a product of potentials on smaller cliques2:

φc(

X

c) =

(cid:89)

i

φi
c(

X

i
c )

(9.6.30)

with no constraint being placed on the non-maximal clique potentials φi
c(
cannot write down directly the maximum likelihood solution for the non-maximal clique potentials φi
c(

i
c ). In general, in this case one
i
c ).

X

X

Consider the graph in ﬁg(9.17) which we consider a pairwise MN. In this case, the clique potentials are con-
strained, so that we cannot simply write down the solution, as we did in the unconstrained decomposable
case. Since the graph is decomposable, there are however, computational savings that can be made in this
case[10]. For an empirical distribution (cid:15), maximum likelihood requires that all the pairwise marginals of the
MN match the corresponding marginals obtained from (cid:15). As explained in ﬁg(9.17) we have a choice as to
which junction tree clique each potential is assigned to, with one valid choice being given in ﬁg(9.17b).

Let’s consider updating the potentials within the 1, 2, 4 clique. Keeping the potentials of the other cliques
φ4,5φ1,5 and φ2,3φ2,4φ3,4 ﬁxed we can update the potentials φ1,2, φ1,4. Using a bar to denote ﬁxed potentials,
the marginal requirement that the MN marginal p(x1, x2, x4) matches the empirical marginal (cid:15)(x1, x2, x4)
can be written in shorthand as

p1,2,4 = (cid:15)1,2,4

(9.6.31)

We can express this requirement in terms of the pairs of variables x1, x2 and x1, x4 within the φ1,2φ1,4 clique
as

p1,2 = (cid:15)1,2,

p1,4 = (cid:15)1,4

(9.6.32)

2A Boltzmann machine is of this form since any unconstrained binary pairwise potentials can be converted into a BM. For

other cases in which the φi

c are constrained, then Iterative scaling may be used in place of IPF.

DRAFT April 24, 2015

221

Algorithm 9.5 Eﬃcient Iterative Proportional Fitting. Given a set of φi, i = 1, . . . , I and a corresponding
set of reference (empirical) marginal distributions on the variables of each potential, (cid:15)i, we aim to set all φ
such that all marginals of the Markov network match the given empirical marginals.

Maximum Likelihood for Undirected models

6:

7:

8:

9:

1: Given a Markov network on potentials φi, i = 1, . . . , I, triangulate the graph and form the cliques
2: Assign potentials to cliques. Thus each clique has a set of associated potentials
3: Initialise all potentials (for example to unity).
4: repeat
5:

Choose a clique c as root.
Propagate messages towards the root and compute the separators on the boundary of the root.
repeat

F

c

1, . . . ,

C

C.

C

Choose a potential φi in clique c, i
Perform an IPF update for φi, given ﬁxed boundary separators and other potentials in c.

∈ F

c.

until Potentials in clique c converge.

10:
11: until All Markov network marginals converge to the reference marginals.

Taking the ﬁrst marginal requirement, this means

p1,2 =

(cid:88)

x3,x4,x5

¯φ1,5 ¯φ4,5φ1,4φ1,2 ¯φ2,4 ¯φ2,3 ¯φ3,4 = (cid:15)1,2

which can be expressed as

(cid:32)

(cid:88)

(cid:88)

x5

x4

(cid:124)

(cid:33)

¯φ1,5 ¯φ4,5

φ1,4φ1,2

(cid:123)(cid:122)
γ1,4

(cid:125)

(cid:32)

(cid:88)

x3

(cid:124)

(cid:33)

¯φ2,4 ¯φ2,3 ¯φ3,4

= (cid:15)1,2

(cid:123)(cid:122)
γ2,4

(cid:125)

(9.6.33)

(9.6.34)

The ‘messages’ γ1,4 and γ1,2 are the boundary separator tables when we choose the central clique as root
and carry out absorption towards the root. Given these ﬁxed messages we can then perform IPF updates
of the root clique using

φnew
1,2 =

(cid:80)

(cid:15)1,2
x4 γ1,4φ1,4γ2,4

After making this update, we can subsequently update φ1,4 similarly using the constraint

(cid:32)

(cid:88)

(cid:88)

x5

x2

(cid:124)

(cid:33)

¯φ1,5 ¯φ4,5

φ1,4φ1,2

(cid:123)(cid:122)
γ1,4

(cid:125)

(cid:32)

(cid:88)

x3

(cid:124)

(cid:33)

¯φ2,4 ¯φ2,3 ¯φ3,4

= (cid:15)1,4

(cid:123)(cid:122)
γ2,4

(cid:125)

so that

φnew
1,4 =

(cid:80)

(cid:15)1,4
x2 γ1,4φ1,2γ2,4

(9.6.35)

(9.6.36)

(9.6.37)

We then iterate these updates until convergence within this 1, 2, 4 clique. Given converged updates for this
clique, we can choose another clique as root, propagate towards the root and compute the separator cliques
on the boundary of the root. Given these ﬁxed boundary clique potentials we again perform IPF within the
clique.

This ‘eﬃcient’ IPF procedure is described more generally in algorithm(9.5) for an empirical distribution (cid:15).
More generally, IPF minimises the Kullback-Leibler divergence between a given reference distribution (cid:15) and
the Markov network. See demoIPFeff.m and IPF.m.

222

DRAFT April 24, 2015

Maximum Likelihood for Undirected models

Figure 9.18: Learning digits (from Simon Lucas’ algoval system) using a Markov network. Top row: the
14 pixels. Second row: the training data
36 training examples. Each example is a binary image on 18
with 50% missing pixels (grey represents a missing pixel). Third row: Reconstructions from the missing
data using a thin-junction-tree MN with maximum clique size 15. Bottom row: Reconstructions using a
thin-junction-tree Boltzmann machine with maximum clique size 15, trained using eﬃcient IPF.

×

(a): Based on the pair-
Figure 9.19:
wise empirical entropies H(xi, xj) edges are
ordered, high entropy edges ﬁrst. Shown
is the adjacency matrix of the resulting
Markov network whose junction tree has
15 in size (white represents an
cliques
≤
(b): Indicated are the number of
edge).
cliques that each pixel is a member of, in-
dicating a degree of importance. Note that
the lowest clique membership value is 1, so
that each pixel is a member of at least one
clique.

(a)

(b)

Example 9.10 (Learning with a structured Markov network). In this example we aim to ﬁt a Markov
network to data, constrained so that inference in the Markov network is computationally cheap by ensuring
that the junction tree of the Markov network has limited clique sizes.

×

14 = 252 binary pixel handwritten twos are presented,

In ﬁg(9.18) 36 examples of 18
forming
the training set from which we wish to ﬁt a Markov network. First all pairwise empirical entropies
H(xi, xj), i, j = 1, . . . , 252 were computed and used to rank edges, with highest entropy edges ranked ﬁrst.
Edges were included in a graph G, highest ranked ﬁrst, provided the triangulated G had all cliques less than
size 15. This resulted in 238 unique cliques and an adjacency matrix for the triangulated G as presented in
ﬁg(9.19a). In ﬁg(9.19b) the number of times that a pixel appears in the 238 cliques is shown, and indicates
the degree of importance of each pixel in distinguishing between the 36 examples. Two models were then
trained and used to compute the most likely reconstruction based on missing data p(xmissing

xvisible).
|

The ﬁrst model was a Markov network on the maximal cliques of the graph, for which essentially no training
is required, and the settings for each clique potential can be obtained as explained in algorithm(9.4).
The model makes 3.8% errors in reconstruction of the missing pixels. Note that the unfortunate eﬀect of
reconstructing a white pixel surrounded by black pixels is an eﬀect of the limited training data. With larger
amounts of data the model would recognise that such eﬀects do not occur.

In the second model, the same maximal cliques were used, but the maximal clique potentials restricted to be
the product of all pairwise two-cliques within the maximal clique. This is equivalent to using a structured
Boltzmann machine, and was trained using the eﬃcient IPF approach of algorithm(9.5). The corresponding
reconstruction error is 20%. This performance is worse than the ﬁrst model since the Boltzmann machine is a
more constrained Markov network and struggles to represent the data well. See demoLearnThinMNDigit.m.

DRAFT April 24, 2015

223

5010015020025050100150200250  246810121424681012141618204060801001201401601802002209.6.4 Exponential form potentials

For exponential form potentials

φc(

X

c) = exp

(cid:16)

θT
c ψc(

(cid:17)

c)

X

Maximum Likelihood for Undirected models

(9.6.38)

we saw in section(9.6.1) how to compute the derivatives for use in standard numerical optimisation proce-
dures. In the following section we outline another popular numerical technique.

Iterative scaling

We consider Markov networks of the exponential form

θ) =

p(

X |

1
Z(θ)

(cid:89)

c

eθcfc(Xc)

(9.6.39)

where the ‘feature functions’ fc(
(Note that
equation (9.6.38) can be written in this form by having multiple potentials within the same clique.). The
normalisation requirement is

0 and c ranges of the non-maximal cliques

⊂ X

c)

≥

X

X

c

Z(θ) =

(cid:88)

(cid:89)

X

c

exp (θcfc(

c))

X

(9.6.40)

A maximum likelihood training algorithm for a Markov network, somewhat analogous to the EM approach
of section(11.2) can be derived as follows[32]. Consider the bound, for positive x:

log x

x

1

−

≤

⇒ −

log x

x

1

−

≥

Hence

log

−

Z(θ)
Z(θold) ≥

1

−

Z(θ)
Z(θold) ⇒ −

log Z(θ)

≥ −

log Z(θold) + 1

Z(θ)
Z(θold)

−

Then we can write a bound on the log likelihood

1
N

L(θ)

1
N

(cid:88)

c,n

≥

θcfc(

n
c )

X

log Z(θold) + 1

−

Z(θ)
Z(θold)

−

(9.6.41)

(9.6.42)

(9.6.43)

As it stands, the bound (9.6.43) is in general not straightforward to optimise since the parameters of each
potential are coupled through the Z(θ) term. For convenience it is useful to ﬁrst reparmameterise and write

θc = θc
(cid:124)

θold
c
(cid:125)

−
(cid:123)(cid:122)
αc

+θold
c

Then

Z(θ) =

(cid:88)

exp

(cid:32)

(cid:88)

X

c

fc(

c)θc

X

(cid:33)

(cid:88)

=

exp

(cid:32)

(cid:88)

X

c

(cid:33)

c)θold
c

fc(

X

exp

(cid:32)

(cid:88)

c

(cid:33)

fc(

c)αc

X

One can decouple this using an additional bound derived by ﬁrst considering:

exp

(cid:32)

(cid:88)

c

(cid:33)

αcfc(

c)

X

= exp

(cid:32)

(cid:88)

(cid:34)

pc

αc

c

(cid:88)

d

fd(

d)

X

(cid:35)(cid:33)

where

pc

≡

224

c)

fc(
X
d fd(

(cid:80)

d)

X

(9.6.44)

(9.6.45)

(9.6.46)

(9.6.47)

DRAFT April 24, 2015

Maximum Likelihood for Undirected models

Since pc

0 and (cid:80)

c pc = 1 we may apply Jensen’s inequality to give

αcfc(

c)

X

(cid:33)

(cid:88)

≤

c

pc exp

(cid:32)

(cid:88)

d

(cid:33)

fd(

d)αc

X

(cid:88)

c

≥
(cid:32)

exp

Hence

Z(θ)

(cid:88)

≤

X

exp

(cid:32)

(cid:88)

c

fc(

c)θold
c

X

(cid:33)

(cid:88)

c



pcexp

αc





(cid:88)

f

fd(

c)

X

Plugging this bound into (9.6.43) we have

(9.6.48)

(9.6.49)

1
N

L(θ)

(cid:88)

≥

c





(cid:124)

1
N

(cid:88)

n

n
c )θc

fc(

X

(cid:42)

(cid:32)

pcexp

αc

−

(cid:88)

d

fd(

c)

X

(cid:33)(cid:43)

p(X |θold)

(cid:123)(cid:122)
LB(θc)





(cid:125)

log Z(θold)

+1

−

(9.6.50)

The term in curly brackets contains the potential parameters θc in an uncoupled fashion. Diﬀerentiating
with respect to θc the gradient of each lower bound is given by

∂LB(θc)
∂θc

=

1
N

(cid:88)

n

(cid:42)

n
c )

fc(

X

−

c)exp

fc(

X

(cid:32)

(cid:16)

θold
c

θc

−

(cid:17) (cid:88)

d

fd(

d)

X

(cid:33)(cid:43)

p(X |θold)

(9.6.51)

Intuitively,
This can be used as part of a gradient based optimisation procedure to learn the parameters θc.
the parameters converge when the empirical average of the functions f match the average of the functions
with respect to samples drawn from the distribution, in line with our general condition for maximum likeli-
hood optimal solution.

In general, there would appear to be little advantage in using the above procedure over the general gradient
approach in section(9.6.1) [211]. However, in the special case that the functions sum to 1, (cid:80)
c) = 1,
c fc(
the zero of the gradient (9.6.51) can be found analytically, giving the iterative scaling (IS) update

X

θc = θold

c + log

1
N

(cid:88)

n

fc(

n
c )

X

−

log

fc(

X

(cid:104)

c)

(cid:105)p(Xc|θold)

(9.6.52)

The constraint that the features fc need to be non-negative can be relaxed at the expense of additional
variational parameters, see exercise(9.12).

If the junction tree formed from this exponential form Markov network has limited tree width, computational
savings can be made by performing IPF over the cliques of the junction tree and updating the parameters
θ within each clique using IS[10]. This is a modiﬁed version of the constrained decomposable case. See also
[290] for a uniﬁed treatment of propagation and scaling on junction trees.

9.6.5 Conditional random ﬁelds

For an input x and output y, a CRF is deﬁned by a conditional distribution [282, 180]

p(y

x) =
|

1
Z(x)

(cid:89)

k

φk(y, x)

(9.6.53)

for (positive) potentials φk(y, x). To make learning more straightforward, the potentials are usually deﬁned
as exp (λkfk(y, x)) for ﬁxed functions f (y, x) and parameters λk. In this case the distribution of the output
conditioned on the input is

p(y

x, λ) =
|

1
Z(x, λ)

(cid:89)

k

exp (λkfk(y, x))

DRAFT April 24, 2015

(9.6.54)

225

Maximum Likelihood for Undirected models

CRFs can also be viewed simply as Markov networks with exponential form potentials, as in section(9.6.4).
Equation(9.6.54) is equivalent to equation (9.6.39) where the parameters θ are here denoted by λ and the
variables x are here denoted by y. In the CRF case the inputs x simply have the eﬀect of determining the
feature fk(y, x).

For an i.i.d. dataset of input-outputs,
D
likelihood requires the maximisation of

=

{

(xn, yn), n = 1, . . . , N

, training based on conditional maximum
}

L(λ)

≡

N
(cid:88)

n=1

log p(yn

|

xn, λ) =

N
(cid:88)

(cid:88)

n=1

k

λkfk(yn, xn)

−

log Z(xn, λ)

(9.6.55)

In general no closed form solution for the optimal λ exists and this needs to be determined numerically.
The methods we’ve discussed, such as iterative scaling may be readily adapted to this optimisation problem,
although in practice gradient based techniques are to be preferred[211]. For completeness we describe
gradient based training. The gradient has components

∂
∂λi

L =

(cid:88)

(cid:16)

n

fi(yn, xn)

− (cid:104)

fi(y, xn)

(cid:105)p(y|xn,λ)

(cid:17)

(9.6.56)

fi(y, xn)
(cid:104)

(cid:105)p(y|xn,λ) can be problematic and their tractability depends on the structure of the poten-
The terms
tials. For a multivariate y, provided the structure of the cliques deﬁned on subsets of y is singly-connected,
then computing the average is generally tractable. More generally, provided the cliques of the resulting
junction tree have limited width, then exact marginals are available. An example of this is given for a
linear-chain CRF in section(23.4.3) – see also example(9.11) below.

Another quantity often useful for numerical optimisation is the Hessian which has components

∂2
∂λi∂λj

L =

(cid:88)

n

(

fi(y, xn)
(cid:104)

(cid:105) (cid:104)

fj(y, xn)

fi(y, xn)fj(y, xn)

)
(cid:105)

(9.6.57)

(cid:105) − (cid:104)

xn, λ). This expression is a (negated) sum of covariance
where the averages above are with respect to p(y
elements, and is therefore negative (semi) deﬁnite. Hence the function L(λ) is concave and has only a
single global optimum. In practice CRFs often have many thousands if not millions of parameters λ so that
computing the Newton update associated with the inverse of the Hessian may be prohibitively expensive.
In this case alternative optimisation methods such as conjugate gradients are preferable.

|

In practice regularisation terms are often added to prevent overﬁtting (see section(13.2.2) for a discussion
of regularisation) . Using a term

(cid:88)

−

k

kλ2
c2
k

(9.6.58)

for positive regularisation constants c2
negative deﬁnite and hence the overall objective function remains concave.

k discourages the weights λ from being too large. This term is also

Once trained a CRF can be used for predicting the output distribution for a novel input x∗. The most likely
output y∗ is equivalently given by

y∗ = argmax

y

log p(y

x∗) = argmax
|

y

(cid:88)

k

λkfk(y, x∗)

−

log Z(x∗, λ)

Since the normalisation term is independent of y, ﬁnding the most likely output is equivalent to

y∗ = argmax

y

(cid:88)

k

λkfk(y, x∗)

(9.6.59)

(9.6.60)

226

DRAFT April 24, 2015

Maximum Likelihood for Undirected models

(a)

(b)

Figure 9.20: (a): Training results for a linear chain CRF. There are 5 training sequences, one per subpanel.
(each state represented
In each subpanel the top row corresponds to the input sequence x1:20, xt
1, . . . , 5
}
(each state
by a diﬀerent colour). The middle row is the correct output sequence y1:20, yt
. The
represented by a diﬀerent colour). Together the input and output sequences make the training data
bottom row contains the most likely output sequence given the trained CRF, arg maxy1:20 p(y1:20
).
(b): Five additional test input sequences (top), correct output sequence (middle) and predicted output
sequence (bottom).

D
x1:20,
|

1, 2, 3

∈ {

∈ {

D

}

Natural language processing

In a natural language processing application, xt might represent a word and yt a corresponding linguistic
tag (‘noun’,‘verb’, etc. ). A more suitable form in this case is to constrain the CRF to be of the form

exp

(cid:32)

(cid:88)

k

µkgk(yt, yt−1) +

(cid:33)

ρlhl(yt, xt)

(cid:88)

l

(9.6.61)

for binary functions gk and hl and parameters µk and ρl. The grammatical structure of tag-tag transitions
is encoded in gk(yt, yt−1) and linguistic tag information in hk(yt, xt), with the importance of these being
determined by the corresponding parameters[180]. In this case inference of the marginals
is
straightforward since the factor graph corresponding to the inference problem is a linear chain.

ytyt−1
(cid:104)

x1:T
|

(cid:105)

Variants of the linear chain CRF are used heavily in natural language processing, including part-of-speech
tagging and machine translation (in which the input sequence x represents a sentence say in English and
the output sequence y the corresponding translation into French). See, for example, [229].

Example 9.11 (Linear chain CRF). We consider a CRF with X = 5 input states and Y = 3 output states
of the form

p(y1:T

x1:T ) =
|

(cid:80)

e

T
(cid:89)

t=2

k µkgk(yt,yt−1)+(cid:80)

l ρlhl(yt,xt)

(9.6.62)

Here the binary functions gk(yt, yt−1) = I [yt = ak] I [yt−1 = bk], k = 1, . . . , 9 , ak
∈
, simply index the transitions between two consecutive outputs. The binary functions hl(yt, xt) =
1, 2, 3
}
{
I [yt = al] I [xt = cl], l = 1, . . . , 15, al
, index the translation of the input to the
output. There are therefore 9 + 15 = 24 parameters in total. In ﬁg(9.20) we plot the training and test
results based on a small set of data. As we see, the model learns to predict the output well, both for the
training and test data. The training of the CRF is obtained using 50 iterations of gradient ascent with a
learning rate of 0.1. See demoLinearCRF.m.

1, 2, 3
}

1, 2, 3, 4, 5

, cl
}

1, 2, 3

∈ {

, bk

∈ {

∈ {

}

DRAFT April 24, 2015

227

2468101214161820123246810121416182012324681012141618201232468101214161820123246810121416182012324681012141618201232468101214161820123246810121416182012324681012141618201232468101214161820123Maximum Likelihood for Undirected models

9.6.6 Pseudo likelihood

Consider a MN on variables x with dim (x) = D of the form

p(x

θ) =
|

1
Z

(cid:89)

c

φc(

c
X

θc)
|

(9.6.63)

For all but specially constrained φc, the partition function Z will be intractable and the likelihood of a set
of i.i.d. data intractable as well. A surrogate is to use the pseudo likelihood of each variable conditioned on
all other variables (which is equivalent to conditioning on only the variable’s neighbours for a MN)

L(cid:48)(θ) =

N
(cid:88)

D
(cid:88)

n=1

i=1

log p(xn
i |

xn
\i|

θ)

(9.6.64)

xn
\i|

The terms p(xn
θ) are usually straightforward to work out since they require ﬁnding the normalisation
i |
of a univariate distribution only. In this case the gradient can be computed exactly, and learning of the
parameters θ carried out. In general, the solution found does not correspond to the maximum likelihood
solution. However, at least for some special cases, such as the Boltzmann machine, this forms a consistent
estimator[152].

9.6.7 Learning the structure

Learning the structure of a Markov network can also be based on independence tests, as for belief networks
in section(9.5). A criterion for ﬁnding a MN on a set of nodes
is to use the fact that no edge exits between
x and y if, conditioned on all other nodes, x and y are deemed independent. This is the pairwise Markov
property described in section(4.2.1). By checking x
for every pair of variables x and y, this
⊥⊥
edge deletion approach in principle reveals the structure of the network[236]. For learning the structure
from an oracle, this method is sound. However, a practical diﬃculty in the case where the independencies
are determined from data is that checking if x
requires in principle enormous amounts of
data. The reason for this is that the conditioning selects only those parts of the dataset consistent with
the conditioning. In practice this will result in very small numbers of remaining datapoints, and estimating
independencies on this basis is unreliable.

| X \ {

| X \ {

x, y

x, y

⊥⊥

X

}

}

y

y

The Markov boundary criterion[236] uses the local Markov property, section(4.2.1), namely that conditioned
on its neighbours, a variable is independent of all other variables in the graph. By starting with a variable x
and an empty neighbourhood set, one can progressively include neighbours, testing if their inclusion renders
the remaining non-neighbours independent of x. A diﬃcultly with this is that, if one doesn’t have the correct
Markov boundary, then including a variable in the neighbourhood set may be deemed necessary. To see
this, consider a network which corresponds to a linear chain and that x is at the edge of the chain. In this
case, only the nearest neighbour of x is in the Markov boundary of x. However, if this nearest neighbour
were not currently in the set, then any other non-nearest neighbour would be included, even though this is
not strictly required. To counter this, the neighbourhood variables included in the neighbourhood of x may
be later removed if they are deemed superﬂuous to the boundary[112].

In cases where speciﬁc constraints are imposed, such as learning structures whose resulting triangulation
has a bounded tree-width, whilst still formally diﬃcult, approximate procedures are available[275].

In terms of network scoring methods for undirected networks, computing a score is hampered by the fact
that the parameters of each clique become coupled in the normalisation constant of the distribution. This
issue can be addressed using hyper Markov priors[79].

9.7 Summary

•

For discrete belief networks, it is particularly convenient to use a Dirichlet parameter prior since this is
conjugate to the categorical distribution.

228

DRAFT April 24, 2015

Code

•

•

•

•

•

Provided we assume local and global parameter independence, the posterior over belief network tables
factorises.

Learning the structure of a belief network is more complex. The PC algorithm uses local independence
tests to decide if two variables should be linked. A global alternative is based on using a network scoring
method such as the model likelihood of a network structure under a Dirichlet prior.

Learning the maximum likelihood parameters of a decomposable Markov network is straightforward and
can be achieved by counting.

For non-decomposable Markov networks, no closed form solution exists. The maximum likelihood criterion
is equivalent to ensuring that clique marginals match empirical marginals. The iterative proportional ﬁtting
algorithm is a technique to set the tables to ensure these marginals match.

For Markov networks parameterised using feature functions, iterative scaling is a maximum likelihood
technique that enables individual parameter updates to be made. Gradient based approaches are also
straightforward and popular in conditional random ﬁelds.

9.8 Code

condindepEmp.m: Bayes test and Mutual Information for empirical conditional independence
condMI.m: Conditional Mutual Information
condMIemp.m: Conditional Mutual Information of Empirical distribution
MIemp.m: Mutual Information of Empirical distribution

9.8.1 PC algorithm using an oracle

z, rather than using data to determine the empirical depen-
This demo uses an oracle to determine x
dence. The oracle is itself a belief network. For the partial orientation only the ﬁrst ‘unmarried collider’
rule is implemented.
demoPCoracle.m: Demo of PC algorithm with an oracle
PCskeletonOracle.m: PC algorithm using an oracle
PCorient.m: Orient a skeleton

⊥⊥

y

|

9.8.2 Demo of empirical conditional independence

z is true. For the other
For half of the experiments, the data is drawn from a distribution for which x
y
|
half of the experiments, the data is drawn from a random distribution for which x
z is false. We then
⊥⊥
measure the fraction of experiments for which the Bayes test correctly decides x
z. We also measure the
y
|
⊥⊥
z, based on setting
fraction of experiments for which the Mutual Information test correctly decides x
⊥⊥
the threshold equal to the median of all the empirical conditional mutual information values. A similar
empirical threshold can also be obtained for the Bayes’ factor (although this is not strictly kosher in the
pure Bayesian spirit since one should in principle set the threshold to zero). The test based on the assumed
chi-squared distributed MI is included for comparison, although it seems to be impractical in these small
data cases.
demoCondIndepEmp.m: Demo of empirical conditional independence based on data

⊥⊥

y

y

|

|

9.8.3 Bayes Dirichlet structure learning

It is interesting to compare the result of demoPCdata.m with demoBDscore.m.
PCskeletonData.m: PC algorithm using empirical conditional independence
demoPCdata.m: Demo of PC algorithm with data
BDscore.m: Bayes Dirichlet (BD) score for a node given parents
learnBayesNet.m: Given an ancestral order and maximal parents, learn the network
demoBDscore.m: Demo of structure learning

DRAFT April 24, 2015

229

Fuse

Drum

Toner

Paper

Roller

Burning

Quality

Wrinkled

Mult. Pages

Paper Jam

Exercises

Figure 9.21: Printer Nightmare belief network. All variables are binary. The upper variables without parents
are possible problems (diagnoses), and the lower variables consequences of problems (faults).

9.9 Exercises

Exercise 9.1 (Printer Nightmare). Cheapco is, quite honestly, a pain in the neck. Not only did they buy
a dodgy old laser printer from StopPress and use it mercilessly, but try to get away with using substandard
components and materials. Unfortunately for StopPress, they have a contract to maintain Cheapco’s old
warhorse, and end up frequently sending the mechanic out to repair the printer. They decide to make a
statistical model of Cheapco’s printer, so that they will have a reasonable idea of the fault based only on the
information that Cheapco’s secretary tells them on the phone. In that way, StopPress hopes to be able to
send out to Cheapco only a junior repair mechanic, having most likely diagnosed the fault over the phone.

Based on the manufacturer’s information, StopPress has a good idea of the dependencies in the printer, and
what is likely to directly aﬀect other printer components. The belief network in ﬁg(9.21) represents these
assumptions. However, the speciﬁc way that Cheapco abuse their printer is a mystery, so that the exact
probabilistic relationships between the faults and problems is idiosyncratic to Cheapco. StopPress has the
following table of faults in which each column represents a visit.

fuse assembly malfunction
drum unit
toner out
poor paper quality
worn roller
burning smell
poor print quality
wrinkled pages
multiple pages fed
paper jam

0
0
1
1
0
0
1
0
0
0

0
0
1
0
0
0
1
0
0
0

0
0
0
1
0
0
1
1
1
1

1
0
0
0
0
1
0
0
0
1

0
1
0
1
0
0
1
0
0
0

0
0
1
0
0
0
1
0
0
0

0
0
0
1
1
0
0
0
1
1

0
1
1
0
0
0
1
0
0
1

0
0
0
1
0
0
0
1
1
1

0
0
0
1
0
0
0
0
0
1

0
1
0
0
0
0
1
0
0
0

0
1
1
1
0
0
1
0
0
0

1
0
0
1
0
1
0
1
0
0

0
0
0
0
1
0
0
1
0
1

1
0
0
0
1
0
0
1
1
0

1. The above table is contained in printer.mat. Learn all table entries on the basis of maximum likeli-

hood.

2. Program the belief network using the tables maximum likelihood tables and BRMLtoolbox. Compute
the probability that there is a fuse assembly malfunction given that the secretary complains there is a
burning smell and that the paper is jammed, and that there are no other problems.

3. Repeat the above calculation using a Bayesian method in which a ﬂat Beta prior is used on all tables.

Continue to use these tables for the remainder of this question.

++

4. Given the above information from the secretary, what is the most likely joint diagnosis over the diag-
evidence)? Use the
|

nostic variables – that is the joint most likely p(F use, Drum, T oner, P aper, Roller
max-absorption method on the associated junction tree.

5. Compute the joint most likely state of the distribution

p(F use, Drum, T oner, P aper, Roller

burning smell, paper jammed)

|

Explain how to compute this eﬃciently using the max-absorption method.

Exercise 9.2. Consider data xn, n = 1, . . . , N . Show that for a Gaussian distribution, the maximum
likelihood estimator of the mean is ˆm = 1
N

n=1 xn and variance is ˆσ2 = 1
N

n=1(xn

ˆm)2.

(cid:80)N

(cid:80)N

−

230

DRAFT April 24, 2015

Exercises

Exercise 9.3. A training set consists of one dimensional examples from two classes. The training examples
from class 1 are

0.5, 0.1, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.35, 0.25

and from class 2 are

0.9, 0.8, 0.75, 1.0

(9.9.1)

(9.9.2)

Fit a (one dimensional) Gaussian using Maximum Likelihood to each of these two classes. Also estimate the
class probabilities p1 and p2 using maximum likelihood. What is the probability that the test point x = 0.6
belongs to class 1?

Exercise 9.4. For a set of N observations (training data),
X
observations, the log likelihood for a belief network to generate

= (cid:8)x1, . . . , xN (cid:9), and independently gathered

is

X

log p(

X

) =

N
(cid:88)

K
(cid:88)

n=1

i=1

We deﬁne the notation

log p (xn
i |

pa (xn

i ))

θi
pa (xi) = t)
s(t) = p(xi = s
|

(9.9.3)

(9.9.4)

representing the probability that variable xi is in state s given the parents of variable xi are in the vector of
states t. Using a Lagrangian

N
(cid:88)

K
(cid:88)

n=1

i=1

L

≡

log p (xn
i |

pa (xn

i )) +

(cid:32)
1

λi
ti

K
(cid:88)

(cid:88)

i=1

ti

(cid:88)

−

s

(cid:0)ti(cid:1)

θi
s

(cid:33)

Show that the maximum likelihood setting of θi

s(tj) =
θj

(cid:80)N

n=1
(cid:80)
s

n=1

(cid:80)N

(cid:104)

I

(cid:105)

(cid:104)

I

xn
j = s
(cid:104)

I

xn
j = s

pa
(cid:104)

(cid:105)

I

(cid:16)

(cid:17)

xn
j
(cid:16)

pa

xn
j

s(t) is
= tj(cid:105)
= tj(cid:105)
(cid:17)

(9.9.5)

(9.9.6)

Exercise 9.5 (Conditional Likelihood training). Consider a situation in which we partition observable
variables into disjoint sets x and y and that we want to ﬁnd the parameters that maximize the conditional
likelihood,

CL(θ) =

1
N

N
(cid:88)

n=1

log p(yn

xn, θ),

|

(9.9.7)

for a set of training data
. All data is assumed generated from the same distribu-
{
}
x, θ0)p(x
θ0) for some unknown parameter θ0. In the limit of a large amount of i.i.d.
tion p(x, y
|
training data, does CL(θ) have an optimum at θ0?

θ0) = p(y

(xn, yn) , n = 1, . . . , N

|

|

Exercise 9.6 (Moment Matching). One way to set parameters of a distribution is to match the moments
of the distribution to the empirical moments. This sometimes corresponds to maximum likelihood (for the
Gaussian distribution for example), though generally this is not consistent with maximum likelihood.
For data with mean m and variance s, show that to ﬁt a Beta distribution B (x
we use

α, β) by moment matching,
|

m(m

α =

m2
s

−

s)

−

, β = α

1

m

−
m

Does this correspond to maximum likelihood?

DRAFT April 24, 2015

(9.9.8)

231

Exercises

Exercise 9.7. For i.i.d. data 0
that the log likelihood is given by

≤

xn

1, n = 1, . . . , N , generated from a Beta distribution B (x
|

≤

a, b), show

L(a, b)

(a

1)

−

≡

N
(cid:88)

n=1

log xn + (b

N
(cid:88)

n=1

1)

−

log(1

xn)

−

−

N log B(a, b)

where B(a, b) is the Beta function. Show that the derivatives are

(9.9.9)

∂
∂a

L =

N
(cid:88)

n=1

log xn

−

N ψ(a)

+N ψ(a + b),

∂
∂b

L =

N
(cid:88)

n=1

log(1

xn)

−

−

N ψ(b)

+N ψ(a + b)

(9.9.10)

@@
@@

where ψ(x)

d log Γ(x)/dx is the digamma function, and suggest a method to learn the parameters a, b.

≡

Exercise 9.8. Consider the Boltzmann machine as deﬁned in example(9.8). Write down the pseudo likeli-
hood for a set of i.i.d. data v1, . . . , vN and derive the gradient of this with respect to wij, i

= j.

Exercise 9.9. Show that the model likelihood equation (9.4.53) can be written explicitly as
(cid:20) Γ (u(cid:48)
i(vk; j))
Γ (ui(vk; j))

Γ ((cid:80)
Γ ((cid:80)

i ui(vk; j))
i u(cid:48)
i(vk; j))

M ) =

(cid:89)

(cid:89)

(cid:89)

D|

p(

(cid:21)

k

j

i

(9.9.11)

Exercise 9.10. Deﬁne the set
N
parents. For a given ancestral order a, the restricted set is written

as consisting of 8 node belief networks in which each node has at most 2

a
N

1. How many belief networks are in

a?

N

2. What is the computational time to ﬁnd the optimal member of
assuming that computing the BD score of any member of
decomposability of the BD score.

N

a using the Bayesian Dirichlet score,
a takes 1 second and bearing in mind the

N

3.

Estimate the time required to ﬁnd the optimal member of

Exercise 9.11. For the Markov network

p(x, y, z) =

1
Z

φ1(x, y)φ2(y, z)

?

N

@@

(9.9.12)

derive an iterative scaling algorithm to learn the unconstrained tables φ1(x, y) and φ2(x, y) based on a set of
i.i.d. data

.

Exercise 9.12. In section(9.6.4) we considered maximum likelihood learning of a Markov network p(
(cid:81)

c) with parameters θc and potentials of the form

)

∝

X

,

,

X

Y

Z

c φc(

X
φc(

c) = exp (θcfc(

c))

X

X

with the constraint fc(

X

c)

≥

0. Our interest here is to drop this positive constraint on fc(

(cid:88)

θcfc(

c) =

X

(cid:88)

pc

c)

θcfc(
pc

X

c

c
for auxiliary variables pc > 0 such that (cid:80)
algorithm for general fc in which each parameter θc can be updated separately.

c pc = 1, explain how to derive a form of iterative scaling training

Exercise 9.13. Write a MATLAB routine A = ChowLiu(X) where X is a D
N data matrix containing
a multivariate datapoint on each column that returns a Chow-Liu maximum likelihood tree for X. The tree
structure is to be returned in the sparse matrix A. You may ﬁnd the routine spantree.m useful. The ﬁle
ChowLiuData.mat contains a data matrix for 10 variables. Use your routine to ﬁnd the maximum likelihood
Chow Liu tree, and draw a picture of the resulting DAG with edges oriented away from variable 1.

×

Exercise 9.14.

Show that for graph with N > 1 nodes, there are at least

++

N
(cid:89)

n=1

2n−1 = 2N (N −1)/2

and less than N !2N (N −1)/2 valid DAGs.

232

DRAFT April 24, 2015

(9.9.13)

c). By considering

X

(9.9.14)

(cid:54)
CHAPTER 10

Naive Bayes

So far we’ve discussed methods in some generality without touching much on how we might use the methods
in a practical setting. Here we discuss one of the simplest methods that is widely used in practice to classify
data. This is a useful junction since it enables us to discuss the issues of parameter learning from data and
also (constrained) structure learning.

10.1 Naive Bayes and Conditional Independence

We shall discuss machine learning concepts in some detail in chapter(13). Here, we require only the intuitive
concept of classiﬁcation, which means giving a discrete label to an input. For example, one might wish to
classify an input image into one of two classes - male or female. Naive Bayes (NB) is a popular classiﬁcation
method and aids our discussion of conditional independence, overﬁtting and Bayesian methods. In NB, we
form a joint model of a D-dimensional attribute (input) vector x and the corresponding class label c

p(x, c) = p(c)

D
(cid:89)

i=1

p(xi

c)
|

(10.1.1)

|

whose belief network is depicted in ﬁg(10.1a). Coupled with a suitable choice for each conditional distribution
p(xi

c), we can then use Bayes’ rule to form a classiﬁer for a novel input vector x∗:

x∗) =
p(c
|

p(x∗

c)p(c)
|
p(x∗)

=

p(x∗
c)p(c)
|
c p(x∗

c)p(c)

(cid:80)

|

(10.1.2)

In practice it is common to consider only two classes dom(c) =
. The theory we describe below is
}
valid for any number of classes c, though our examples are restricted to the binary class case. Also, the
attributes xi are often taken to be binary, as we shall do initially below as well. The extension to more than
two attribute states, or continuous attributes is straightforward.

0, 1
{

Example 10.1. EZsurvey.org partitions radio station listeners into two groups – the ‘young’ and ‘old’. They
assume that, given the knowledge that a customer is either ‘young’ or ‘old’, this is suﬃcient to determine
whether or not a customer will like a particular radio station, independent of their likes or dislikes for any
other stations:

p(r1, r2, r3, r4

age) = p(r1
|

age)p(r2
|

age)p(r3
|

age)p(r4
|

|

age)

(10.1.3)

where each of the variables r1, r2, r3, r4 can take the states
like or dislike, and the ‘age’ variable can take
the value young or old. Thus the information about the age of the customer determines the individual

233

(10.1.4)

(10.1.5)

Estimation using Maximum Likelihood

radio station preferences without needing to know anything else. To complete the speciﬁcation, given that
a customer is young, she has a 95% chance to like Radio1, a 5% chance to like Radio2, a 2% chance to like
Radio3 and a 20% chance to like Radio4. Similarly, an old listener has a 3% chance to like Radio1, an 82%
chance to like Radio2, a 34% chance to like Radio3 and a 92% chance to like Radio4. They know that 90%
of the listeners are old.

Given this model, and the fact that a new customer likes Radio1, and Radio3, but dislikes Radio2 and
Radio4, what is the probability that the new customer is young? This is given by

p(young

|

r1 = like, r2 = dislike, r3 = like, r4 = dislike)

=

p(r1 = like, r2 = dislike, r3 = like, r4 = dislike
(cid:80)
age p(r1 = like, r2 = dislike, r3 = like, r4 = dislike

young)p(young)
|
age)p(age)

|

Using the naive Bayes structure, the numerator above is given by

p(r1 = like

young)p(r2 = dislike
|

young)p(r3 = like
|

|

young)p(r4 = dislike

young)p(young)
|

Plugging in the values we obtain

0.95

0.95

0.02

0.8

×

×

×

×

0.1 = 0.0014

The denominator is given by this value plus the corresponding term evaluated assuming the customer is old,

0.03

×

0.18

×

0.34

×

0.08

×

0.9 = 1.3219

10−4

×

Which gives

p(young

|

r1 = like, r2 = dislike, r3 = like, r4 = dislike) =

0.0014
0.0014 + 1.3219

10−4 = 0.9161

×

(10.1.6)

10.2 Estimation using Maximum Likelihood

Learning the table entries for NB is a straightforward application of the more general BN learning discussed
in section(9.3). For a fully observed dataset, maximum likelihood learning of the table entries corresponds
to counting the number of occurrences in the training data, as we show below. This is a useful exercise to
reinforce and make more concrete the general theory of section(9.3).

10.2.1 Binary attributes

Consider a dataset
, i = 1, . . . , D and associated
class label cn. The number of datapoints from class c = 0 is denoted n0 and the number from class c = 1
θc
i . The
is denoted n1. For each attribute of the two classes, we need to estimate the values p(xi = 1
|

(xn, cn), n = 1, . . . , N
{

of binary attributes, xn

0, 1
}

i ∈ {

c)

≡

}

c

cn

xn
i

θc

θi,c

i = 1 : D

x1

x2

x3

(a)

n = 1 : N

(b)

(a): The central
Figure 10.1: Naive Bayes classiﬁer.
assumption is that given the class c, the attributes xi
(b): Assuming the data is i.i.d.,
are independent.
maximum likelihood learns the optimal parameters θc
of the distribution p(c) and the parameters θi,c of the
class-dependent attribute distributions p(xi

c).

|

234

DRAFT April 24, 2015

Estimation using Maximum Likelihood

c) = 1
c) is given by the normalisation requirement, p(xi = 0
other probability, p(xi = 0
|
|

−

p(xi = 1
|

c) = 1

θc
i .

−

Based on the NB conditional independence assumption the probability of observing a vector x can be
compactly written1

c) =

p(x

|

D
(cid:89)

i=1

p(xi

c) =
|

D
(cid:89)

i=1

(θc

i )xi(1

i )1−xi
θc

−

(10.2.1)

In the above expression, xi is either 0 or 1 and hence each i term contributes a factor θc
θc
i
if xi = 0. Together with the assumption that the training data is i.i.d. generated, the log likelihood of the
attributes and class labels is

i if xi = 1 or 1

−

(cid:88)

L =

log p(xn, cn) =

log p(cn)

(cid:88)

n

(cid:89)

i

n



(cid:88)



i,n

=

i log θcn
xn

i + (1

xn
i ) log(1

−

−

cn)

p(xn
i |



θcn
i )



(10.2.2)

+ n0 log p(c = 0) + n1 log p(c = 1)

(10.2.3)

This can be written more explicitly in terms of the parameters as

L =

(cid:88)

i,n

(cid:8)I [xn

i = 1, cn = 0] log θ0

i + I [xn

i = 0, cn = 0] log(1

i ) + I [xn
θ0

i = 1, cn = 1] log θ1
i

−

+ I [xn

i = 0, cn = 1] log(1

−

i )(cid:9) + n0 log p(c = 0) + n1 log p(c = 1)
θ1

(10.2.4)

We can ﬁnd the maximum likelihood optimal θc

i by diﬀerentiating w.r.t. θc

i and equating to zero, giving

θc
i = p(xi = 1
|

c) =

=

(cid:80)
n

n

(cid:80)

I [xn

i = 1, cn = c]

I [xn
i = 0, cn = c] + I [xn
number of times xi = 1 for class c
number of datapoints in class c

i = 1, cn = c]

Similarly, optimising equation (10.2.3) with respect to p(c) gives

p(c) =

number of times class c occurs
total number of data points

(10.2.5)

(10.2.6)

(10.2.7)

These results are consistent with our general theory in section(9.3) that maximum likelihood corresponds
to setting tables by counting.

Classiﬁcation boundary

We classify a novel input x∗ as class 1 if

x∗)
x∗) > p(c = 0
p(c = 1
|
|

Using Bayes’ rule and writing the log of the above expression, this is equivalent to

log p(x∗

c = 1) + log p(c = 1)
|

−

log p(x∗) > log p(x∗

|

c = 0) + log p(c = 0)

log p(x∗)

−

From the deﬁnition of the classiﬁer, this is equivalent to (the normalisation constant
dropped from both sides)

−

(cid:88)

i

log p(x∗
i |

c = 1) + log p(c = 1) >

(cid:88)

i

log p(x∗
i |

c = 0) + log p(c = 0)

(10.2.8)

(10.2.9)

log p(x∗) can be

(10.2.10)

1This makes use of the general notation that any quantity raised to the power 0 is 1, i.e. x0 ≡ 1. Unfortunately, there is a
potential general mathematical notational confusion with xy meaning possibly x raised to the power y, or alternatively that y
is simply indexing a set of x variables. This potential conﬂict will not hopefully arise too often, and can most often be resolved
by reference to the meaning of the symbols involved.

DRAFT April 24, 2015

235

Estimation using Maximum Likelihood

Using the binary encoding xi

, we therefore classify x∗ as class 1 if

(cid:88)

i

(cid:8)x∗

i log θ1

i + (1

x∗
i ) log(1

−

−

∈ {

0, 1
}
i )(cid:9) + log p(c = 1) >
θ1

(cid:8)x∗

i log θ0

i + (1

(cid:88)

i

x∗
i ) log(1

−

−

i )(cid:9) + log p(c = 0)
θ0

(10.2.11)

This decision rule can be expressed in the form: classify x∗ as class 1 if (cid:80)
i + a > 0 for some suitable
choice of weights wi and constant a, see exercise(10.4). The interpretation is that w speciﬁes a hyperplane
in the attribute space and x∗ is classiﬁed as 1 if it lies on the positive side of the hyperplane.

i wix∗

Example 10.2 (Are they Scottish?). Consider the following vector of binary attributes:

(shortbread, lager, whiskey, porridge, football)

(10.2.12)

A vector x = (1, 0, 1, 1, 0)T would describe that a person likes shortbread, does not like lager, drinks
whiskey, eats porridge, and has not watched England play football. Together with each vector x, there is a
label nat describing the nationality of the person, dom(nat) =

, see ﬁg(10.2).

scottish, english
{

}

We wish to classify the vector x = (1, 0, 1, 1, 0)T as either scottish or english. Using Bayes’ rule:

p(scottish

x) =
|

p(x

scottish)p(scottish)
|

p(x)

=

p(x

|

p(x

scottish)p(scottish)
|
scottish)p(scottish) + p(x

english)p(english)
|

(10.2.13)

By maximum likelihood the ‘prior’ class probability p(scottish) is given by the fraction of people in the
database that are Scottish, and similarly p(english) is given as the fraction of people in the database that
are English. This gives p(scottish) = 7/13 and p(english) = 6/13.

For p(x

nat) under the Naive Bayes assumption:
|
p(x

nat)p(x2
|

nat)p(x3
|

nat) = p(x1
|

nat)p(x4
|

nat)p(x5
|

|

nat)

(10.2.14)

so that knowing whether not someone is Scottish, we don’t need to know anything else to calculate the
probability of their likes and dislikes. Based on the table in ﬁg(10.2) and using maximum likelihood we
have:

english) = 1/2
p(x1 = 1
|
english) = 1/2
p(x2 = 1
|
english) = 1/3
p(x3 = 1
|
english) = 1/2
p(x4 = 1
|
english) = 1/2
p(x5 = 1
|
For x = (1, 0, 1, 1, 0)T, we get

p(x1 = 1
|
p(x2 = 1
|
p(x3 = 1
|
p(x4 = 1
|
p(x5 = 1
|

scottish) = 1
scottish) = 4/7
scottish) = 3/7
scottish) = 5/7
scottish) = 3/7

p(scottish

3
7 ×
4
7 ×
Since this is greater than 0.5, we would classify this person as being Scottish.

3
7 ×
13 + 1
7

5
7 ×
2 ×

4
7 ×
1
2 ×

7
13
1
3 ×

1
×
5
7 ×

3
7 ×

3
7 ×

1
2 ×

1
2 ×

x) =

6
13

×

1

|

= 0.8076

(10.2.15)

(10.2.16)

Small data counts

In example(10.2), consider trying to classify the vector x = (0, 1, 1, 1, 1)T. In the training data, all Scottish
people say they like shortbread. This means that for this particular x, p(x, scottish) = 0, and therefore
that we make the extremely conﬁdent classiﬁcation p(scottish
x) = 0. This demonstrates a diﬃculty using
|
maximum likelihood with sparse data. One way to ameliorate this is to smooth the probabilities, for
example by adding a small number to the frequency counts of each attribute. This ensures that there are

236

DRAFT April 24, 2015

Estimation using Maximum Likelihood

0
0
1
1
1

1
0
1
1
0

1
1
0
0
0

1
1
0
0
1
(a)

0
1
0
0
1

0
0
0
1
0

1
0
0
1
1

1
1
0
0
1

1
1
1
1
0

1
1
0
1
0
(b)

1
1
0
1
1

1
0
1
1
0

1
0
1
0
0

6

10.2:
for

Figure
English
attributes
people
tastes
(shortbread, lager, whiskey, porridge, f ootball).
Each column represents
tastes of an
individual. (b): Scottish tastes for 7 people.

(a):
over

the

no zero probabilities in the model. An alternative is to use a Bayesian approach that discourages extreme
probabilities, as discussed in section(10.3).

Potential pitfalls with encoding

In practice,
In many oﬀ-the-shelf packages implementing Naive Bayes, binary attributes are assumed.
however, the case of non-binary attributes often occurs. Consider the following attribute : age. In a survey,
a person’s age is marked down using the variable a
1, 2, 3. a = 1 means the person is between 0 and 10
∈
years old, a = 2 means the person is between 10 and 20 years old, a = 3 means the person is older than
20. One way to transform the variable a into a binary representation would be to use three binary variables
(a1, a2, a3) with (1, 0, 0), (0, 1, 0), (0, 0, 1) representing a = 1, a = 2, a = 3 respectively. This is called
M coding since only 1 of the binary variables is active in encoding the M states. By construction,
1
this means that the variables a1, a2, a3 are dependent – for example, if we know that a1 = 1, we know that
a2 = 0 and a3 = 0. Regardless of any class conditioning, these variables will always be dependent, contrary
to the assumption of Naive Bayes. A correct approach is to use variables with more than two states, as
explained in section(10.2.2).

of

−

−

10.2.2 Multi-state variables

The extension of the above method to class variables c with more than two states is straightforward. We
concentrate here therefore on extending the attribute variables to having more than two states. For a
variable xi with states, dom(xi) =

, the likelihood of observing a state xi = s is denoted

1, . . . , S
{

}

p(xi = s
|

c) = θi

s(c)

with (cid:80)
1, . . . , N is

s p(xi = s

c) = 1. The class conditional likelihood of generating the i.i.d. data
|

D

N
(cid:89)

n=1

p(xn

cn) =
|

N
(cid:89)

D
(cid:89)

S
(cid:89)

C
(cid:89)

n=1

i=1

s=1

c=1

θi
s(c)

I[xn

i =s]I[cn=c].

(10.2.17)

= (xn, cn), n =

(10.2.18)

The eﬀect of the indicators is that only those terms θi
for class c. This then gives the class conditional log-likelihood

s(c) survive for which there are attributes i in state s

L(θ) =

N
(cid:88)

D
(cid:88)

S
(cid:88)

C
(cid:88)

n=1

i=1

s=1

c=1

I [xn

i = s] I [cn = c] log θi

s(c)

(10.2.19)

We can optimize with respect to the parameters θ using a Lagrange multiplier (one for each of the attributes
i and classes c) to ensure normalisation. This gives the Lagrangian

(θ, λ) =

L

N
(cid:88)

D
(cid:88)

S
(cid:88)

C
(cid:88)

n=1

i=1

s=1

c=1

I [xn

i = s] I [cn = c] log θi

s(c) +

C
(cid:88)

D
(cid:88)

c=1

i=1

(cid:32)

λc
i

S
(cid:88)

s=1

1

−

(cid:33)

θi
s(c)

(10.2.20)

To ﬁnd the optimum of this function we may diﬀerentiate with respect to θi
the resulting equation we obtain

s(c) and equate to zero. Solving

N
(cid:88)

n=1

I [xn

i = s] I [cn = c]
θi
s(c)

= λc
i

DRAFT April 24, 2015

(10.2.21)

237

Bayesian Naive Bayes

Figure 10.3: Bayesian Naive Bayes with a factorised prior on the class con-
ditional attribute probabilities p(xi = s
c). For simplicity we assume that
|
the class probability θc
p(c) is learned with maximum likelihood, so that
no distribution is placed over this parameter.

≡

θc

n = 1 : N

cn

xn
i

θi,c

c = 1 : C

i = 1 : D

Hence, by normalisation,

θi
s(c) = p(xi = s
|

c) =

(cid:80)
I [xn
n
s(cid:48),n(cid:48) I (cid:2)xn(cid:48)

i = s] I [cn = c]
i = s(cid:48)(cid:3) I [cn(cid:48) = c]

(cid:80)

(10.2.22)

The maximum likelihood setting for the parameter p(xi = s
|
attribute i is in state s for class c.

c) equals the relative number of times that

10.2.3 Text classiﬁcation

Consider a set of documents about politics, and another set about sport. Our interest is to make a method
that can automatically classify a new document as pertaining to either sport or politics. We search through
both sets of documents to ﬁnd say the 100 most commonly occurring words (not including so-called ‘stop
words’ such as ‘a’ or ‘the’). Each document is then represented by a 100 dimensional vector representing the
number of times that each of the words occurs in that document – the so called bag of words representation
(this is a crude representation of the document since it discards word order). A Naive Bayes model speciﬁes
c), where xi is the count of the number of times word i
a distribution of these number of occurrences p(xi
appears in documents of type c. One can achieve this using either a multistate representation (as discussed
in section(10.2.2)) or using a continuous xi to represent the relative frequency of word i in the document.
In the latter case p(xi

c) could be conveniently modelled using for example a Beta distribution.
|

|

Despite the simplicity of naive Bayes, it can classify novel documents surprisingly well[137] (although nat-
urally the real practical methods work with many more attributes and choose them wisely). Intuitively a
potential justiﬁcation for the conditional independence assumption is that if we know that a document is
about politics, this is a good indication of the kinds of other words we will ﬁnd in the document. Because
Naive Bayes is a reasonable classiﬁer in this sense, and has minimal storage and fast training, it has been
applied to time and storage critical applications, such as automatically classifying webpages into types[309],
and spam ﬁltering[8]. It also forms one of the simplest yet most commonly used basic machine learning
classiﬁcation routines.

10.3 Bayesian Naive Bayes

As we saw in the previous section, naive Bayes can be a powerful method for classiﬁcation, yet can be overly
zealous in the case of small counts. If a single attribute i has no counts for class c then, irrespective of
the other attributes, the classiﬁer will say that x cannot be from class c. This happens because the prod-
uct of 0 with anything else remains 0. To counter overconﬁdence eﬀect, we can use a simple Bayesian method.

Given a dataset

=

(xn, cn), n = 1, . . . , N
{

D

, we predict the class c of an input x using
}

x,
p(c
|

)

D

∝

p(x,

, c)

D

∝

p(x

|D

, c)p(c

|D

)

238

(10.3.1)

@@

DRAFT April 24, 2015

Bayesian Naive Bayes

For convenience we will simply set p(c

|D

) using maximum likelihood

p(c

|D

) =

1
N

(cid:88)

n

I [cn = c]

(10.3.2)

However, as we’ve seen, setting the parameters of p(x
, c) using maximum likelihood training can yield
over-conﬁdent predictions in the case of sparse data. A Bayesian approach that addresses this diﬃculty
θi
uses priors on the probabilities p(xi = s
s(c) to discourage extreme values. The model is depicted in
ﬁg(10.3).

c)
|

|D

≡

The prior
Writing θi(c) = (cid:0)θi
we make the global factorisation assumption (see section(9.4)) and use a prior

1(c), . . . , θi

S(c)(cid:1) as the vector of probabilities and θ = (cid:8)θi(c), i = 1, . . . , D, c = 1, . . . , C(cid:9),

p(θ) =

p(θi(c))

(cid:89)

i,c

(10.3.3)

We consider discrete xi each of which take states from 1, . . . , S. In this case p(xi = s
c) corresponds to a
|
categorical distribution, for which the conjugate prior is a Dirichlet distribution. Under the factorised prior
assumption (10.3.3) we deﬁne a prior for each attribute i and class c,
ui(c)(cid:1)
|

p(θi(c)) = Dirichlet (cid:0)θi(c)

(10.3.4)

where ui(c) is the hyperparameter vector of the Dirichlet distribution for table p(xi

c).

|

The posterior

Consistent with our general Bayesian BN training result in section(9.4), the parameter posterior factorises

p(θ(c∗)

|D

) =

(cid:89)

i

p(θi(c∗)

|D

)

where

p(θi(c∗)

p(θi(c∗))

(cid:89)

)

|D

∝

p(xn
i |

θi(c∗))

n:cn=c∗
By conjugacy, the posterior for class c∗ is a Dirichlet distribution,

p(θi(c∗)

|D

) = Dirichlet (cid:0)θi(c∗)

ˆui(c∗)(cid:1)
|

where the vector ˆui(c∗) has components

(cid:2)ˆui(c∗)(cid:3)

s = ui

s(c∗) +

(cid:88)

n:cn=c∗

I [xn

i = s]

(10.3.5)

(10.3.6)

(10.3.7)

(10.3.8)

For Dirichlet hyperparameters ui(c∗) the above equation updates the hyperparameter by the number of
times variable i is in state s for class c∗ data. A common default setting is to take all components of u to
be 1.

Classiﬁcation

The posterior class distribution for a novel input x∗ is given by

p(c∗

x∗,
|

)

D

∝

p(c∗, x∗,

)

D

∝

p(c∗

|D

)p(x∗

|D

, c∗) = p(c∗

)

|D

(cid:89)

i

p(x∗

i |D

, c∗)

To compute p(x∗

p(x∗

i = s

|D

, c∗) we use
(cid:90)

i |D
, c∗) =

θi(c∗)

(cid:90)

θi(c∗)

=

DRAFT April 24, 2015

p(x∗

i = s, θi(c∗)

, c∗) =

|D

(cid:90)

θi(c∗)

p(x∗

θi(c∗))p(θi(c∗)
i = s
|

|D

s(c∗)p(θi(c∗)
θi

|D

)

(10.3.9)

)

(10.3.10)

(10.3.11)

239

Tree Augmented Naive Bayes

c

x1

x2

x3

x4

Figure 10.4: Tree Augmented Naive (TAN) Bayes. Each variable xi has
at most one parent. The maximum likelihood optimal TAN structure is
computed using a modiﬁed Chow-Liu algorithm in which the conditional
mutual information MI(xi; xj
c) is computed for all i, j. A maximum weight
spanning tree is then found and turned into a directed graph by orienting
the edges outwards from a chosen root node. The table entries can then be
read oﬀ using the usual maximum likelihood counting argument.

|

Using the general identity

(cid:90)

θsDirichlet (θ

u) dθ =
|

1
Z(u)

(cid:90) (cid:89)

s(cid:48)

us(cid:48) −1+I[s(cid:48)=s]dθ =

θs(cid:48)

Z(u(cid:48))
Z(u)

where Z(u) is the normalisation constant of the distribution Dirichlet (

(cid:26) us

= s(cid:48)
us + 1 s = s(cid:48)

s

u(cid:48)
s =

we obtain

p(c∗

x∗,
|

)

D

∝

p(c∗

)

|D

Z(u∗i(c∗))
Z(ˆui(c∗))

(cid:89)

i

where

s (c∗) = ˆui
u∗i

s(c∗) + I [x∗

i = s]

u) and

·|

(10.3.12)

(10.3.13)

(10.3.14)

(10.3.15)

Example 10.3 (Bayesian Naive Bayes). Repeating the previous analysis for the ‘Are they Scottish?’ data
from example(10.2), the probability under a uniform Dirichlet prior for all the tables, gives a value of 0.764
for the probability that (1, 0, 1, 1, 0) is Scottish, compared with a value of
0.8076 under the standard Naive
Bayes assumption. See demoNaiveBayes.m.

@@

10.4 Tree Augmented Naive Bayes

A natural extension of Naive Bayes is to relax the assumption that the attributes are independent given the
class:

p(x

=

c)
|

D
(cid:89)

i=1

p(xi

c)
|

(10.4.1)

The question then arises – which structure should we choose for p(x
c)? As we saw in section(9.5), learning
|
a structure is computationally infeasible for all but very small numbers of attributes. A practical algorithm
therefore requires a speciﬁc form of constraint on the structure. In section(9.5.4) we saw that we can learn
single-parent tree-structured networks eﬃciently. Below we extend this to learning class dependent tree
networks for classiﬁcation.

10.4.1 Learning tree augmented Naive Bayes networks

c) of the form of a tree structure with a single-parent constraint, see ﬁg(10.4),
For a distribution p(x1, . . . , xD
|
we can readily ﬁnd the class conditional maximum likelihood solution by computing the Chow-Liu tree for
each class. One then adds links from the class node c to each variable and learns the class conditional
probabilities from c to x, which can be read oﬀ for maximum likelihood using the usual counting argument.
Note that this would generally result in a diﬀerent Chow-Liu tree for each class.

240

DRAFT April 24, 2015

(cid:54)
(cid:54)
Exercises

Practitioners typically constrain the network to have the same structure for all classes. The maximum
likelihood objective under the TAN constraint then corresponds to maximising the conditional mutual in-
formation[104]

MI(xi; xj

c) =
|

KL(p(xi, xj
(cid:104)

p(xi
c)
|

|

|

c)p(xj

c))

(cid:105)p(c)

|

(10.4.2)

see exercise(10.7). Once the structure is learned one subsequently sets parameters by maximum likelihood
counting. Techniques to prevent overﬁtting are discussed in [104] and can be addressed using Dirichlet
priors, as for the simpler Naive Bayes structure.

One can readily consider less restrictive structures than single-parent Belief Networks. However, ﬁnding
optimal BN structures is generally computationally infeasible and heuristics are required to limit the search
space.

10.5 Summary

•

•

•

Naive Bayes is a simple class-conditional generative model of data that can be used to form a simple
classiﬁer.

Bayesian training of the parameters is straightforward.

An extension of the standard naive Bayes’ model is to consider attributes with at most a single parent
attribute (in addition to the class label). Finding the maximum likelihood optimal tree augmented structure
is straightforward and corresponds to a maximum spanning tree problem with weights given by the class
conditional mutual information.

10.6 Code

NaiveBayesTrain.m: Naive Bayes trained with Maximum Likelihood
NaiveBayesTest.m: Naive Bayes test
NaiveBayesDirichletTrain.m: Naive Bayes trained with Bayesian Dirichlet
NaiveBayesDirichletTest.m: Naive Bayes testing with Bayesian Dirichlet
demoNaiveBayes.m: Demo of Naive Bayes

10.7 Exercises

Exercise 10.1. A local supermarket specializing in breakfast cereals decides to analyze the buying patterns
of its customers. They make a small survey asking 6 randomly chosen people their age (older or younger
than 60 years) and which of the breakfast cereals (Cornﬂakes, Frosties, Sugar Puﬀs, Branﬂakes) they like.
Each respondent provides a vector with entries 1 or 0 corresponding to whether they like or dislike the cereal.
Thus a respondent with (1101) would like Cornﬂakes, Frosties and Branﬂakes, but not Sugar Puﬀs. The
older than 60 years respondents provide the following data (1000), (1001), (1111), (0001). The younger than
60 years old respondents responded (0110), (1110). A novel customer comes into the supermarket and says
she only likes Frosties and Sugar Puﬀs. Using naive Bayes trained with maximum likelihood, what is the
probability that she is younger than 60?

Exercise 10.2. A psychologist does a small survey on ‘happiness’. Each respondent provides a vector with
entries 1 or 0 corresponding to whether they answer ‘yes’ to a question or ‘no’, respectively. The question
vector has attributes

x = (rich, married, healthy)

DRAFT April 24, 2015

(10.7.1)

241

Exercises

Thus, a response (1, 0, 1) would indicate that the respondent was ‘rich’, ‘unmarried’, ‘healthy’. In addition,
each respondent gives a value c = 1 if they are content with their lifestyle, and c = 0 if they are not. The fol-
lowing responses were obtained from people who claimed also to be ‘content’ : (1, 1, 1), (0, 0, 1), (1, 1, 0), (1, 0, 1)
and for ‘not content’: (0, 0, 0), (1, 0, 0), (0, 0, 1), (0, 1, 0), (0, 0, 0).

1. Using Naive Bayes, what is the probability that a person who is ‘not rich’, ‘married’ and ‘healthy’ is

‘content’ ?

2. What is the probability that a person who is ‘not rich’ and ‘married’ is ‘content’ ? (That is, we do not

know whether or not they are ‘healthy’).

3. Consider the following vector of attributes :

x1 = 1 if customer is younger than 20 ; x1 = 0 otherwise

(10.7.2)

x2 = 1 if customer is between 20 and 30 years old ; x2 = 0 otherwise

(10.7.3)

x3 = 1 if customer is older than 30 ; x3 = 0 otherwise

x4 = 1 if customer walks to work ; x4 = 0 otherwise

(10.7.4)

(10.7.5)

Each vector of attributes has an associated class label ‘rich’ or ‘poor’. Point out any potential diﬃculties
with using your previously described approach to training using naive Bayes. Hence describe how to
extend your previous naive Bayes method to deal with this dataset.

Exercise 10.3. Whizzco decide to make a text classiﬁer. To begin with they attempt to classify documents
as either sport or politics. They decide to represent each document as a (row) vector of attributes describing
the presence or absence of words.

x = (goal, football, golf, defence, oﬀence, wicket, oﬃce, strategy)

(10.7.6)

Training data from sport documents and from politics documents is represented below in MATLAB using a
matrix in which each row represents the 8 attributes.

xP=[1 0 1 1 1 0 1 1; % Politics

0 0 0 1 0 0 1 1;
1 0 0 1 1 0 1 0;
0 1 0 0 1 1 0 1;
0 0 0 1 1 0 1 1;
0 0 0 1 1 0 0 1]

xS=[1 1 0 0 0 0 0 0; % Sport

0 0 1 0 0 0 0 0;
1 1 0 1 0 0 0 0;
1 1 0 1 0 0 0 1;
1 1 0 1 1 0 0 0;
0 0 0 1 0 1 0 0;
1 1 1 1 1 0 1 0]

Using a maximum likelihood naive Bayes classiﬁer, what is the probability that the document x = (1, 0, 0, 1, 1, 1, 1, 0)
is about politics?

Exercise 10.4. A Naive Bayes Classiﬁer for binary attributes xi
i = p(xi =
∈ {
class = 1), θ0
class = 0), and p1 = p(class = 1) and p0 = p(class = 0). Show that the decision
i = p(xi = 1
1
|
|
to classify a datapoint x as class 1 holds if wTx + b > 0 for some w and b, and state explicitly w and b as
a function of θ1, θ0, p1, p0.

is parameterised by θ1

0, 1
}

Exercise 10.5. This question concerns spam ﬁltering. Each email is represented by a vector

x = (x1, . . . , xD)

(10.7.7)

where xi
∈ {
The symbols/words are

0, 1

. Each entry of the vector indicates if a particular symbol or word appears in the email.
}

money, cash, !!!, viagra, . . . , etc.

242

(10.7.8)

DRAFT April 24, 2015

Exercises

so that for example x2 = 1 if the word ‘cash’ appears in the email. The training dataset consists of a set of
vectors along with the class label c, where c = 1 indicates the email is spam, and c = 0 not spam. Hence,
the training set consists of a set of pairs (xn, cn), n = 1, . . . , N . The naive Bayes model is given by

p(c, x) = p(c)

D
(cid:89)

i=1

p(xi

c)
|

(10.7.9)

1. Derive expressions for the parameters of this model in terms of the training data using maximum

likelihood. Assume that the data is independent and identically distributed

p(c1, . . . , cN , x1, . . . , xN ) =

N
(cid:89)

n=1

p(cn, xn)

Explicitly, the parameters are

c = 0), i = 1, . . . , D
c = 1), p(xi = 1
p(c = 1), p(xi = 1
|
|

(10.7.10)

(10.7.11)

x).
2. Given a trained model p(x, c), explain how to form a classiﬁer p(c
|

3. If ‘viagra’ never appears in the spam training data, discuss what eﬀect this will have on the classiﬁcation
for a new email that contains the word ‘viagra’. Explain how you might counter this eﬀect. Explain
how a spammer might try to fool a naive Bayes spam ﬁlter.

Exercise 10.6. For a distribution p(x, c) and an approximation q(x, c), show that when p(x, c) corresponds
to the empirical distribution, ﬁnding q(x, c) that minimises the Kullback-Leibler divergence

KL(p(x, c)
|

q(x, c))

corresponds to maximum likelihood training of q(x, c) assuming i.i.d. data.

Exercise 10.7. Consider a distribution p(x, c) and a Tree Augmented approximation

q(x, c) = q(c)

(cid:89)

i

q(xi

xpa(i), c),
|

pa(i) < i or pa(i) =

∅

(10.7.12)

(10.7.13)

Show that for the optimal q(x, c) constrained as above, the solution q(x, c) that minimises KL(p(x, c)
q(x, c))
|
when plugged back into the Kullback-Leibler expression gives, as a function of the parental structure,

KL(p(x, c)
|

q(x, c)) =

(cid:28)

(cid:88)

−

i

log

p(xi, xpa(i)|
c)
c)p(xi
p(xpa(i)|

c)
|

(cid:29)

+ const.

p(xi,xpa(i),c)

(10.7.14)

c) has the same structure, minimis-
This shows that under the single-parent constraint and that each tree q(x
|
ing the Kullback-Leibler divergence is equivalent to maximising the sum of conditional mutual information
terms. From exercise(10.6), we know therefore that this also corresponds to the maximum likelihood setting
for the parental structure. This can then be achieved by ﬁnding a maximal weight spanning tree, as in the
case of the Chow-Liu tree.

DRAFT April 24, 2015

243

Exercises

244

DRAFT April 24, 2015

CHAPTER 11

Learning with Hidden Variables

In many models some variables are not directly observed, but are latent or ‘hidden’. This can also occur when
some data are not observed. In this chapter we discuss methods for learning in the presence of such missing
information, and in particular develop the Expectation-Maximisation algorithm and related variants.

11.1 Hidden Variables and Missing Data

In practice data entries are often missing resulting in incomplete information to specify a likelihood. Obser-
vational variables may be split into visible (those for which we actually know the state) and missing (those
whose states would nominally be known but are missing for a particular datapoint).

Another scenario in which not all variables in the model are observed are the so-called hidden or latent
variable models. In this case there are variables which are essential for the model description but never
observed. For example, the underlying physics of a model may contain latent processes which are essential
to describe the model, but cannot be directly measured.

11.1.1 Why hidden/missing variables can complicate proceedings

In learning the parameters of models as previously described in chapter(9), we assumed we have complete
information to deﬁne all variables of the joint model of the data p(x
θ). Consider the Asbestos-Smoking-
|
Cancer network of section(9.3). Using the multivariate variable x = (a, s, c), if patient n has a complete
record, the likelihood of this record is

p(xn

|

θ) = p(an, sn, cn

θ) = p(cn
|

|

an, sn, θc)p(an

θa)p(sn
|

θs)
|

(11.1.1)

which is factorised in terms of the table entry parameters. We exploited this property to show that table
entries θ can be learned by considering only local information, both in the maximum likelihood and Bayesian
frameworks.

Now consider the case that for some of the patients, only partial information is available. For example,
for patient n with incomplete record xn = (c = 1, s = 1) it is known that the patient has cancer and is a
smoker, but whether or not they had exposure to asbestos is unknown. Since we can only use the ‘visible’
available information it would seem reasonable (see section(11.1.2)) to assess parameters using the marginal
likelihood

p(xn

θ) =

|

(cid:88)

a

p(a, sn, cn

θ) =

|

(cid:88)

a

p(cn

a, sn, θc)p(a
|
|

θa)p(sn

θs)
|

(11.1.2)

245

Hidden Variables and Missing Data

θ

θ

xvis

xinv

xvis

xinv

minv

(a)

minv

(b)

(a): Missing at random assumption. The
Figure 11.1:
mechanism that generates the missing data does not depend
on either the parameter θ of the model, or on the value of
(b): Missing completely at random
the missing data.
assumption. The mechanism generating the missing data
is completely independent of the model. Note that in both
cases the direction of the arrow between xvis and xinv is
irrelevant.

Using the marginal likelihood, however, may result in computational diﬃculties since the likelihood equation
(11.1.2) cannot now be factorised into a product of the separate table parameters of the form fs(θs)fa(θa)fc(θc).
In this case the maximisation of the likelihood is more complex since the parameters of diﬀerent tables are
coupled.

A similar complication holds for Bayesian learning. As we saw in section(9.4.1), under a prior factorised
over each CPT θ, the posterior is also factorised. However, the missing variable introduces dependencies in
the posterior parameter distribution, making the posterior more complex. In both the maximum likelihood
and Bayesian cases, one has a well deﬁned likelihood function of the table parameters/posterior.
Note that missing data does not always make the parameter posterior non-factorised. For example, if
the cancer state is unobserved above, because cancer is a collider with no descendants, the conditional
distribution simply sums to 1, and one is left with a factor dependent on a and another on s.

11.1.2 The missing at random assumption

Under what circumstances is it valid to use the marginal likelihood to assess parameters? We partition
the variables x into those that are ‘visible’, xvis and ‘invisible’, xinv, so that the set of all variables can be
written x = [xvis, xinv]. For the visible variables we have an observed state xvis = v, whereas the state of
the invisible variables is unknown. To complete the picture we also need to model the process that informs
when data will be missing. We use an indicator minv = 1 to denote that the state of the invisible variables
is unknown and require then a model p(minv
xvis, xinv, θ). Then for a datapoint which contains both visible
and invisible information,

|

p(xvis = v, minv = 1

θ) =
|

=

(cid:88)

xinv
(cid:88)

xinv

p(xvis = v, xinv, minv = 1
θ)
|

p(minv = 1
|

xvis = v, xinv, θ)p(xvis = v, xinv

θ)
|

(11.1.3)

(11.1.4)

If we assume that the mechanism which generates invisible data is independent of the parameter and the
missing value xinv

p(minv = 1

xvis = v, xinv, θ) = p(minv = 1
|
|

xvis = v)

then

p(xvis = v, minv = 1

θ) = p(minv = 1
|
|

xvis = v)

(cid:88)

xinv

p(xvis = v, xinv

θ)
|

= p(minv = 1
|

xvis = v)p(xvis = v
|

θ)

(11.1.5)

(11.1.6)

(11.1.7)

Only the term p(xvis = v
θ) conveys information about the parameter θ of the model. Therefore, provided
|
the mechanism by which the data is missing depends only on the visible states, we may simply use the
marginal likelihood to assess parameters. This is called the missing at random (MAR) assumption, see
ﬁg(11.1).

246

DRAFT April 24, 2015

Hidden Variables and Missing Data

Example 11.1 (Not missing at random). EZsurvey.org stop men on the street and ask them their favourite
colour. All men whose favourite colour is pink decline to respond to the question – for any other colour,
all men respond to the question. Based on the data, EZsurvey.org produce a histogram of men’s favourite
colour, based on the likelihood of the visible data alone, conﬁdently stating that none of them likes pink.
For simplicity, we assume there are only three colours, blue, green and pink. EZsurvey.org attempts to ﬁnd
the histogram with probabilities θb, θg, θp with θb + θg + θp = 1. Each respondent produces a visible response
blue, green, pink
x with dom(x) =
, otherwise m = 1 if there is no response. Three men are asked their
}
{
favourite colour, giving data

(cid:8)x1, x2, x3(cid:9) =

{

blue, missing, green

}

Based on the likelihood of the visible data alone we have the log likelihood for i.i.d. data

L(θb, θg, θp) = log θb + log θg + λ (1

θb

−

−

θg

−

θp)

where the last Lagrange term ensures normalisation. Maximising the expression we arrive at

θb =

1
2

, θg =

1
2

, θp = 0

(11.1.8)

(11.1.9)

(11.1.10)

Hence EZsurvey.org arrive at the erroneous conclusion that no men like pink, even though the reality is that
some men like pink – they just don’t want to say so. The unreasonable result that EZsurvey.org produce
is due to not accounting correctly for the mechanism which produces the data. In this case the data is not
MAR since whether or not the data is missing depends on the state of the missing variable.

The correct mechanism that generates the data (including the missing data is)

θ)p(x3 = green
θ)p(m2 = 1
p(x1 = blue
|
|

|

θ) = θbθpθg = θb (1

θb

−

−

θg) θg

(11.1.11)

where we used p(m2 = 1
θ) = θp since the probability that a datapoint is missing is the same as the
|
probability that the favourite colour is pink. Maximising the likelihood, we arrive at

θb =

1
3

, θg =

1
3

, θp =

1
3

(11.1.12)

as we would expect. On the other hand if there is another visible variable, t, denoting the time of day,
and the probability that men respond to the question depends only on the time t alone (for example the
probability of having missing data is high during rush hour), then we may indeed treat the missing data as
missing at random.

A stronger assumption than MAR is that the missing data mechanism is completely independent of all other
model processes

p(minv = 1
|

xvis = v, xinv, θ) = p(minv = 1)

(11.1.13)

which is called missing completely at random. This applies for example to latent variable models in which
the variable state is always missing, independent of anything else.

11.1.3 Maximum likelihood

Throughout the remaining discussion we will assume any missing data is MAR or missing completely at
random. We partition the variables into ‘visible variables v for which we know the states and ‘hidden’
variables h whose states will not be observed. For maximum likelihood we may then learn model parameters
θ by optimising the on the visible variables alone

θ) =

p(v

|

(cid:88)

h

θ)
p(v, h
|

with respect to θ.

DRAFT April 24, 2015

(11.1.14)

247

Expectation Maximisation

11.1.4 Identiﬁability issues

The marginal likelihood objective function depends on the parameters only through p(v
parameter solutions may exist. For example, consider a latent variable model with distribution

θ), so that equivalent
|

p(x1, x2

θ) = θx1,x2
|

(11.1.15)

in which variable x2 is never observed. This means that the marginal likelihood only depends on the entry
x2 θx1,x2. Given a maximum likelihood solution θ∗, we can then always ﬁnd an equivalent
p(x1
maximum likelihood solution θ(cid:48) provided (see exercise(11.9))

θ) = (cid:80)
|

θ(cid:48)
x1,x2 =

(cid:88)

x2

θ∗
x1,x2

(cid:88)

x2

(11.1.16)

In other cases there is an inherent symmetry in the parameter space of the marginal likelihood. For example,
consider the network over binary variables

a, s)p(a)p(s)
p(c, a, s) = p(c
|

(11.1.17)

We assume we know the table for p(s) and that our aim is to learn the asbestos table ˆp(a = 1) and cancer
tables

ˆp(c = 1

a = 1, s = 1), ˆp(c = 1
|

a = 1, s = 0), ˆp(c = 1
|

a = 0, s = 1), ˆp(c = 1
|

a = 0, s = 0)
|

(11.1.18)

where we used a ‘ˆ’ to denote that these are parameter estimates.

We assume that we have missing data such that the states of variable a are never observed. In this case
an equivalent solution (in the sense that it has the same marginal likelihood) is given by interchanging the
states of a:

ˆp(cid:48)(a = 0) = ˆp(a = 1)

and the four tables

(11.1.19)

ˆp(cid:48)(c = 1
ˆp(cid:48)(c = 1

a = 0, s = 1) = ˆp(c = 1
|
|
a = 1, s = 1) = ˆp(c = 1
|
|

a = 1, s = 1),
a = 0, s = 1),

ˆp(cid:48)(c = 1
|
ˆp(cid:48)(c = 1
|

a = 0, s = 0) = ˆp(c = 1
a = 1, s = 0) = ˆp(c = 1

a = 1, s = 0)
|
a = 0, s = 0)
|

(11.1.20)

A similar situation occurs in a more general setting in which the state of a variable is consistently unobserved
(mixture models are a case in point) yielding an inherent symmetry in the solution space. A well known
characteristic of maximum likelihood algorithms is that ‘jostling’ occurs in the initial stages of training in
which these symmetric solutions compete.

11.2 Expectation Maximisation

The EM algorithm is a convenient and general purpose iterative approach to maximising the likelihood
under missing data/hidden variables[84, 202]. It is generally straightforward to implement and can achieve
large jumps in parameter space, particularly in the initial iterations.

11.2.1 Variational EM

The key feature of the EM algorithm is to form an alternative objective function for which the parameter
coupling eﬀect discussed in section(11.1.1) is removed, meaning that individual parameter updates can be
achieved, akin to the case of fully observed data. The way this works is to replace the marginal likelihood
with a lower bound – it is this lower bound that has the useful decoupled form.

We ﬁrst consider a single variable pair (v, h), where v stands for ‘visible’ and h for ‘hidden’. The model of the
θ). To derive the
θ) and our interest is to set θ by maximising the marginal likelihood p(v
data is then p(v, h
|
|

248

DRAFT April 24, 2015

Expectation Maximisation

Algorithm 11.1 Expectation Maximisation. Compute Maximum Likelihood value for data with hidden
variables. Input: a distribution p(x
|

. Returns ML candidate θ.

θ) and dataset

V

1: t = 0
2: Choose an initial setting for the parameters θ0.
3: while θ not converged (or likelihood not converged) do
4:

5:

6:

7:

t + 1

t
for n = 1 to N do

←

qn
t (hn
end for
θt = arg maxθ

vn) = p(hn
|

vn, θt−1)
|

8:
9: end while
10: return θt

(cid:80)N

n=1 (cid:104)

log p(hn, vn

θ)
|

t (hn|vn)

(cid:105)qn

(cid:46) Iteration counter
(cid:46) Initialisation

(cid:46) Run over all datapoints
(cid:46) E step

(cid:46) M step

(cid:46) The max likelihood parameter estimate.

bound on the marginal likelihood, consider the Kullback-Leibler divergence (which is always non-negative)
v) and the parametric model p(h
between a ‘variational’ distribution q(h
|
|

v, θ):

KL(q(h

v)
|

p(h
|

v, θ))
|

log q(h

≡ (cid:104)

v)
|

−

log p(h

v, θ)
|

(cid:105)q(h|v) ≥

0

(11.2.1)

The term ‘variational’ refers to the fact that this distribution will be a parameter of an optimisation problem.
Using p(h

θ) does not depend on h,

v, θ) = p(h, v
|

θ)/p(v
|
v, θ)) =
|

θ) and the fact that p(v
|

|
log p(h, v

KL(q(h

v)
|

p(h
|

(cid:104)
Rearranging, we obtain a bound on the marginal likelihood1

(cid:105)q(h|v) − (cid:104)

log q(h

v)
|

θ)
|

(cid:105)q(h|v) + log p(v

θ)

|

≥

0

(11.2.2)

(11.2.3)

log p(v

θ)

|

≥ − (cid:104)
(cid:124)

v)

log q(h
|
(cid:123)(cid:122)
Entropy

(cid:105)q(h|v)
(cid:125)

+

θ)
log p(h, v
(cid:104)
|
(cid:123)(cid:122)
(cid:124)
Energy

(cid:105)q(h|v)
(cid:125)

The energy term is also called the ‘expected complete data log likelihood’. The bound is potentially useful
since the θ dependent energy term is similar in form to the fully observed case, except that terms with
missing data have their log likelihood weighted by a prefactor. Equation(11.2.3) is a marginal likelihood
bound for a single training example. Under the i.i.d. assumption, the log likelihood of all training data

= (cid:8)v1, . . . , vN (cid:9) is the sum of the individual log likelihoods:

V

log p(

V|

θ) =

N
(cid:88)

n=1

log p(vn

θ)
|

Summing over the training data, we obtain a bound on the log (marginal) likelihood

log p(

V|

θ)

˜L(
{

q

}

≥

, θ)

N
(cid:88)

(cid:104)

n=1

≡ −
(cid:124)

log q(hn

vn)
|

(cid:123)(cid:122)
entropy

(cid:105)q(hn|vn)
(cid:125)

+

N
(cid:88)

n=1
(cid:124)

(cid:104)

log p(hn, vn

(cid:123)(cid:122)
energy

θ)
|

(cid:105)q(hn|vn)
(cid:125)

(11.2.4)

(11.2.5)

Note that the bound ˜L(
{
set q(hn

vn) = p(hn

q

}

|

vn, θ), n = 1, . . . , N .
|

, θ) is exact (that is, the right hand side is equal to the log likelihood) when we

The bound depends both on θ and the set of variational distributions
. Our aim is then to try to optimise
this bound w.r.t. θ and
; in doing so we will push up the lower bound, and hopefully increase thereby
the likelihood itself. A simple iterative procedure to optimise the bound is to ﬁrst ﬁx θ and then optimise
w.r.t.
and optimise the bound w.r.t. θ. These are known as the ‘E’ and ‘M’ steps and
, and then ﬁx
are repeated until convergence:

q
{

}

{

}

{

}

}

{

q

q

q

E-step For ﬁxed θ, ﬁnd the distributions q(hn

vn), n = 1, . . . , N that maximise equation (11.2.5).
|

M-step For ﬁxed q(hn

|

vn), n = 1, . . . , N , ﬁnd the parameters θ that maximise equation (11.2.5).

1This is analogous to a standard partition function bound in statistical physics, from where the terminology ‘energy’ and

‘entropy’ hails.

DRAFT April 24, 2015

249

11.2.2 Classical EM

In the variational E-step above, the fully optimal setting is

q(hn

|

vn) = p(hn

vn, θ)
|

Expectation Maximisation

(11.2.6)

Since q is ﬁxed during the M-step, performing the M-step optimisation is equivalent to maximising the energy
term alone, see algorithm(11.1). From here onwards we shall use the term ‘EM’ to refer to the classical
EM algorithm, unless otherwise stated. It is important to note that the EM algorithm cannot generally
guarantee to ﬁnd the fully optimal maximum likelihood solution and can get trapped in local optima, as
discussed in example(11.2).

Example 11.2 (EM for a one-parameter model). We consider a model small enough that we can plot fully
the evolution of the EM algorithm. The model is on a single visible variable v with dom(v) = R and single
two-state hidden variable h with dom(h) =

1, 2
}

θ) = p(v
. We deﬁne a model p(v, h
|

h, θ)p(h) with
|

{

p(v

h, θ) =
|

1
√π

e−(v−θh)2

(11.2.7)

and p(h = 1) = p(h = 2) = 0.5. For an observation v = 2.75 our interest is to ﬁnd the parameter θ that
optimises the likelihood

p(v = 2.75
|

θ) =

(cid:88)

h=1,2

h, θ)p(h) =
p(v = 2.75
|

(cid:16)

1
2√π

e−(2.75−θ)2

+ e−(2.75−2θ)2(cid:17)

(11.2.8)

The log likelihood is plotted in ﬁg(11.2a) with optimum at θ = 1.325. If the state of h were given, the log
likelihood would be a single bump, rather than the more complex double bump in the case of missing data.
To use the EM approach to ﬁnd the optimum θ, we need to work out the energy

log p(v, h
|
(cid:104)

θ)

(cid:105)q(h|v) =
=

log p(v

(cid:104)

−

(cid:68)

(v

−

h, θ)
|
θh)2(cid:69)

(cid:105)q(h|v) +

log p(h)
(cid:104)

(cid:105)q(h|v)

+ const.

q(h|v)

(11.2.9)

(11.2.10)

There are two h states of the q distribution and using q(h) as shorthand for q(h
v), normalisation requires
|
q(1) + q(2) = 1. Due to the normalisation, we can fully parameterise q using q(2) alone. The EM procedure
then iteratively optimises the lower bound

log p(v = 2.75

˜L(q(2), θ)

θ)
|

≥

q(1) log q(1)

≡ −

q(2) log q(2)

−

(cid:88)

−

h=1,2

q(h) (2.75

−

θh)2 + const.

(11.2.11)

From an initial starting θ, the EM algorithm ﬁnds the q distribution that optimises ˜L(q(2), θ) (E-step) and
then updates θ (M-step). Depending on the initial θ, the solution found is either a global or local optimum
of the likelihood, see ﬁg(11.2).

The M-step is easy to work out analytically in this case with θnew = v
sets qnew(h) = p(h

v, θ) so that
|

(cid:105)q(h) / (cid:10)h2(cid:11)

h
(cid:104)

q(h). Similarly, the E-step

qnew(h = 2) =

h = 2, θ)p(h = 2)
p(v = 2.75
|

p(v = 2.75)

=

e−(2.75−2θ)2
e−(2.75−2θ)2 + e−(2.75−θ)2

where we used

h = 1, θ)p(h = 1) + p(v = 2.75
p(v = 2.75) = p(v = 2.75
|
|

h = 2, θ)p(h = 2)

(11.2.12)

(11.2.13)

250

DRAFT April 24, 2015

Expectation Maximisation

(a)

(b)

(c)

(a): The log likelihood for the model described in example(11.2).

(b): Contours of the
Figure 11.2:
lower bound ˜L(q(h = 2), θ). For an initial choice θ = 1.9, successive updates of the E (vertical) and M
(horizontal) steps are plotted, with the algorithm converging to the global optimum maximum likelihood
setting for θ. (c): Starting at θ = 1.95, the EM algorithm converges to a local optimum.

Example 11.3. Consider a simple model

p(x1, x2

θ)
|

where dom(x1) = dom(x2) =

1, 2
{

. Assuming an unconstrained distribution
}

p(x1, x2

θ) = θx1,x2,
|

θ1,1 + θ1,2 + θ2,1 + θ2,2 = 1

(11.2.14)

(11.2.15)

our aim is to learn θ from the data x1 = (1, 1) , x2 = (1, ?) , x3 = (?, 2). The energy term for the classical
EM is

log p(x1 = 1, x2 = 1

θ) +
|

(cid:104)

log p(x1 = 1, x2

θ)

(cid:105)p(x2|x1=1,θold) +

|

log p(x1, x2 = 2
|
(cid:104)

θ)

(cid:105)p(x1|x2=2,θold)

Writing out fully each of the above terms on a separate line gives the energy

log θ1,1

+p(x2 = 1

+p(x1 = 1

x1 = 1, θold) log θ1,1 + p(x2 = 2
|
x2 = 2, θold) log θ1,2 + p(x1 = 2
|

x1 = 1, θold) log θ1,2
|
x2 = 2, θold) log θ2,2
|

(11.2.16)

(11.2.17)

(11.2.18)

(11.2.19)

This expression resembles the standard log likelihood of fully observed data except that terms with missing
data have their weighted log parameters. The parameters are conveniently decoupled in this bound (apart
from the trivial normalisation constraint) so that ﬁnding the optimal parameters is straightforward. This is
achieved by the M-step update which gives

1 + p(x2 = 1

θ1,1
∝
θ2,1 = 0

x1 = 1, θold) θ1,2
|
θ2,2

x1 = 1, θold) + p(x1 = 1
|
x2 = 2, θold)
|
θold
x1,x2 (E-step) etc. The E and M-steps are iterated till convergence.

p(x2 = 2
p(x1 = 2

x2 = 2, θold)
|

∝
∝

where p(x2

x1, θold)
|

∝

(11.2.20)

The EM algorithm increases the likelihood

Whilst, by construction, the EM algorithm cannot decrease the lower bound on the likelihood, an important
question is whether or not the log likelihood itself is necessarily increased by this procedure.

DRAFT April 24, 2015

251

11.522.53−1.55−1.5−1.45−1.4−1.35−1.3−1.25−1.2−1.15−1.1−1.05θlogp(v|θ)θq(h=2)11.522.530.10.20.30.40.50.60.70.80.9θq(h=2)11.522.530.10.20.30.40.50.60.70.80.9Expectation Maximisation

s
1
0
1
1
1
0
0

c
1
0
1
0
1
0
1

Figure 11.3: A database containing information about being a Smoker (1 signiﬁes the
individual is a smoker), and lung Cancer (1 signiﬁes the individual has lung Cancer).
Each row contains the information for an individual, so that there are 7 individuals in
the database.

We use θ(cid:48) for the new parameters, and θ for the previous parameters in two consecutive iterations. Using
q(hn
vn, θ) we see that as a function of the parameters, the lower bound for a single variable
|
pair (v, h) depends on θ and θ(cid:48):

vn) = p(hn

|

LB(θ(cid:48)

θ)
|

≡ − (cid:104)

v, θ)
log p(h
|

(cid:105)p(h|v,θ) + (cid:10)log p(h, v

|

θ(cid:48))(cid:11)

p(h|v,θ) .

From the deﬁnition of the lower bound, equation (11.2.3), we have

log p(v

θ(cid:48)) = LB(θ(cid:48)
|

θ) + KL(p(h
|
|

p(h
v, θ)
|

v, θ(cid:48))).
|

(11.2.21)

(11.2.22)

That is, the Kullback-Leibler divergence is the diﬀerence between the lower bound and the true likelihood.
We may then also write

log p(v

θ) = LB(θ
|

|

v, θ)
θ) + KL(p(h
|
(cid:124)
(cid:123)(cid:122)
0

p(h
|

v, θ))
|
(cid:125)

Hence

log p(v

θ(cid:48))

|

−

log p(v

θ) = LB(θ(cid:48)
|
(cid:124)

|

θ)

−
(cid:123)(cid:122)
≥0

LB(θ

θ)
(cid:125)

|

v, θ)
+ KL(p(h
|
(cid:123)(cid:122)
≥0

(cid:124)

p(h
|

v, θ(cid:48)))
|
(cid:125)

(11.2.23)

(11.2.24)

The ﬁrst assertion is true since, by deﬁnition of the M-step, we search for a θ(cid:48) which has a higher value
for the bound than our starting value θ. The second assertion is true by the non-negative property of the
Kullback-Leibler divergence.

For more than a single datapoint, we simply sum each individual bound for log p(vn
θ). Hence we reach the
important conclusion that the EM algorithm increases, not only the lower bound on the marginal likelihood,
but the marginal likelihood itself (more correctly, EM cannot decrease these quantities).

|

Shared parameters and tables

It is often the case in models that parameters are shared between components of the model. The application
of EM to this shared parameter case is essentially straightforward. According to the energy term, we need
to identify all those terms in which the shared parameter occurs. The objective for the shared parameter is
then the sum over all energy terms containing the shared parameter.

11.2.3 Application to Belief networks

Conceptually, the application of EM to training Belief Networks with missing data is straightforward. The
battle is more notational than conceptual. We begin the development with an example, from which intuition
about the general case can be gleaned.

Example 11.4. Consider the network.

a, s)p(a)p(s)
p(a, c, s) = p(c
|

(11.2.25)

for which we have a set of data, but that the states of variable a are never observed, see ﬁg(11.3). Our goal
is to learn the CPTs p(c
a, s) and p(a) and p(s). To apply EM, algorithm(11.1) to this case, we ﬁrst assume
|
s , θ0
a, θ0
initial parameters θ0
c .

252

DRAFT April 24, 2015

Expectation Maximisation

The ﬁrst E-step, for iteration t = 1 then deﬁnes a set of distributions on the hidden variables (here the
hidden variable is a). For notational convenience, we write qn

t (a) in place of qn

vn). Then
t (a
|

qn=1
t=1 (a) = p(a

c = 1, s = 1, θ0),
|

qn=2
t=1 (a) = p(a

c = 0, s = 0, θ0)
|

(11.2.26)

and so on for the 7 training examples, n = 2, . . . , 7.

We now move to the ﬁrst M-step. The energy term for any iteration t is:

E(θ) =

=

7
(cid:88)

n=1
7
(cid:88)

n=1

log p(cn

|

(cid:104)

an, sn) + log p(an) + log p(sn)

(cid:105)qn

t (a)

(cid:110)
(cid:104)

log p(cn

an, sn)
|

t (a) +

(cid:105)qn

log p(an)
(cid:104)

(cid:111)
t (a) + log p(sn)
(cid:105)qn

(11.2.27)

(11.2.28)

The ﬁnal term is the log likelihood of the variable s, and p(s) appears explicitly only in this term. Hence,
the usual maximum likelihood rule applies, and p(s = 1) is simply given by the relative number of times
that s = 1 occurs in the database, giving p(s = 1) = 4/7, p(s = 0) = 3/7.

The contribution of parameter p(a = 1) to the energy occurs in the terms

(cid:88)
n {

t (a = 0) log p(a = 0) + qn
qn

t (a = 1) log p(a = 1)
}

which, using the normalisation constraint is

log p(a = 0)

(cid:88)

n

qn
t (a = 0) + log(1

p(a = 0))

−

(cid:88)

n

qn
t (a = 1)

(11.2.29)

(11.2.30)

Diﬀerentiating with respect to p(a = 0) and solving for the zero derivative we get the M-step update for
p(a = 0)

p(a = 0) =

(cid:80)

n qn
t (a = 0) + (cid:80)

t (a = 0)
n qn

(cid:80)

n qn

t (a = 1)

=

1
N

(cid:88)

n

qn
t (a = 0)

(11.2.31)

That is, whereas in the standard maximum likelihood estimate, we would have the real counts of the data
in the above formula, here they have been replaced with our guessed values qn

t (a = 0) and qn

t (a = 1).

A similar story holds for p(c = 1
a = 0, s = 1). Again we need to consider that normalisation means that
|
p(c = 0
a = 0, s = 1) also contributes. The contribution of this term to the energy is from those data indices
|
n for which s = 1

(cid:88)

n:cn=1,sn=1

qn
a = 0, s = 1) +
t (a = 0) log p(c = 1
|

(cid:88)

qn
t (a = 0) log (1

n:cn=0,sn=1

a = 0, s = 1))
p(c = 1
|

−

a = 0, s = 1) gives
Optimising with respect to p(c = 1
|

a = 0, s = 1) =
p(c = 1
|

(cid:80)
n

I [cn = 1] I [sn = 1] qn

(cid:80)
n

I [cn = 1] I [sn = 1] qn
t (a = 0) + (cid:80)

n

t (a = 0)

I [cn = 0] I [sn = 1] qn

t (a = 0)

For comparison, the setting in the complete data case is

a = 0, s = 1) =
p(c = 1
|

(cid:80)
n

n
I [cn = 1] I [sn = 1] I [an = 0] + (cid:80)
n

I [cn = 0] I [sn = 1] I [an = 0]

(cid:80)

I [cn = 1] I [sn = 1] I [an = 0]

(11.2.32)

(11.2.33)

There is an intuitive relationship between these updates: in the missing data case we replace the indicators
by the assumed distributions q.
Iterating the E and M steps, these parameters will converge to a local
likelihood optimum.

DRAFT April 24, 2015

253

11.2.4 General case

A belief network on a multivariate variable x takes the general form

p(x) =

(cid:89)

i

p(xi

|

pa (xi)).

Expectation Maximisation

(11.2.34)

Some of the variables will be observed, and others hidden. We therefore partition the variable x into visible
= (cid:8)v1, . . . , vN (cid:9) our interest is to learn
and hidden multivariate parts x = (v, h). Given an i.i.d. dataset
V
the tables of p(x) to maximise the likelihood on the visible data
. For each datapoint index n, each mul-
V
tivariate variable xn = (vn, hn) decomposes into its visible and hidden parts; an upper variable index will
typically the datapoint, and a lower index the variable number.

From equation (11.2.5), the form of the energy term for belief networks is

log p(xn)

(cid:88)
n (cid:104)

(cid:105)qt(hn|vn) =

(cid:88)

(cid:88)

n

i

log p(xn
i |
(cid:104)

pa (xn

i ))

(cid:105)qt(hn|vn)

(11.2.35)

Here t indexes the iteration count for the EM algorithm. It is useful to deﬁne the following notation:

t (x) = qt(hn
qn

vn)δ(v, vn)
|
t (x) sets the visible variables in the observed state, and deﬁnes a conditional distribution

(11.2.36)

This means that qn
on the unobserved variables. We then deﬁne the mixture distribution

qt(x) =

1
N

N
(cid:88)

n=1

qn
t (x)

(11.2.37)

The energy term in the left hand side of equation (11.2.35) can be written more compactly using this notation
as

log p(xn)

(cid:88)
n (cid:104)

(cid:105)qt(hn|vn) = N

log p(x)
(cid:104)

(cid:105)qt(x)

To see this consider the right hand side of the above

N

(cid:104)

log p(x)

(cid:105)qt(x) = N

(cid:88)

x

[log p(x)]

1
N

(cid:88)

n

qt(hn

|

vn)δ(v, vn) =

(cid:88)
n (cid:104)

log p(xn)

(cid:105)qt(hn|vn)

(11.2.38)

(11.2.39)

Using this compact notation for the energy, and the structure of the belief network, we can decompose the
energy as

log p(x)
(cid:104)

(cid:105)qt(x) =

(cid:88)

i

log p(xi
(cid:104)

pa (xi))
|

(cid:105)qt(x) =

(cid:88)

(cid:68)

i

log p(xi
(cid:104)

|

pa (xi))

(cid:105)qt(xi|pa(xi))

(cid:69)

qt(pa(xi))

(11.2.40)

This means that maximising the energy is equivalent to minimising

(cid:88)

(cid:68)

i

log qt(xi
(cid:104)

|

pa (xi))

(cid:105)qt(xi|pa(xi)) − (cid:104)

log p(xi

pa (xi))
|

(cid:105)qt(xi|pa(xi))

(cid:69)

qt(pa(xi))

(11.2.41)

where we added the constant ﬁrst term to make this into the form of a Kullback-Leibler divergence. Since
this is a sum of independent Kullback-Leibler divergences, optimally the M-step is given by setting

pnew(xi

pa (xi)) = qt(xi
|

pa (xi))
|

(11.2.42)

In practice, storing the qt(x) over the states of all variables x is prohibitively expensive. Fortunately, since
the M-step only requires the distribution on the family of each variable xi, one only requires the local
distributions qn

t (xi, pa (xi)). We may therefore dispense with the global qt(x) and equivalently use

pnew(xi

pa (xi)) =
|

(cid:80)
n qn
t (xi, pa (xi))
(cid:80)
n(cid:48) qn(cid:48)
t (pa (xi))

254

(11.2.43)

DRAFT April 24, 2015

Expectation Maximisation

Algorithm 11.2 EM for Belief Networks. Input: a BN DAG and dataset on the visible variables
the maximum likelihood estimate of the tables p(xi

pa (xi)), i = 1, . . . , K.
|

. Returns

V

pa (xi)) to initial values.

pa (xi)) not converged (or likelihood not converged) do
|
t + 1

1: t = 1
2: Set pt (xi
|
3: while p (xi
t
4:
for n = 1 to N do
qn
t (x) = pt (hn
end for
for i = 1 to K do

←

5:

7:

6:

8:

vn) δ(v, vn)
|

@@

9:

pt+1(xi

pa (xi)) =

|

(cid:80)N
(cid:80)N

n=1 qn
n(cid:48)=1 qn(cid:48)

t (xi,pa(xi))
t (pa(xi))

(cid:46) Iteration counter
(cid:46) Initialisation

(cid:46) Run over all datapoints
(cid:46) E step

(cid:46) Run over all variables
(cid:46) M step

end for
10:
11: end while
12: return pt(xi

pa (xi))
|

(cid:46) The max likelihood parameter estimate.

Using the EM algorithm, the optimal setting for the E-step is to use qt(hn
vn). With this
notation, the EM algorithm can be compactly stated as in algorithm(11.2). See also EMbeliefnet.m. An
illustration of the evolution of the log likelihood under EM iterations is given in ﬁg(11.4). For readers less
comfortable with the above KL-based derivation, we describe a more classical approach based on Lagrange
multipliers for a speciﬁc case in example(11.5).

vn) = pold(hn

|

|

Example 11.5 (Another belief network example). Consider a ﬁve variable distribution with discrete vari-
ables,

p(x1, x2, x3, x4, x5) = p(x1

x2)p(x2
|

x3)p(x3
|

x4)p(x4
|

x5)p(x5)
|

(11.2.44)

in which the variables x2 and x4 are consistently hidden in the training data, and training data for x1, x3, x5
are always present. The distribution can be represented as a belief network

x1

x2

x3

x4

x5

The M-step is given by maximising the energy. According to the general form for the energy, equation
(11.2.35), we need to consider a variational distribution q(h
v) on the hidden variables h = (x2, x4) condi-
|
tioned on the visible variables v = (x1, x3, x5). Using n as the datapoint index and t as the EM iteration
counter, we require variational distributions for each datapoint

qt(xn

2 , xn
4 |

1 , xn
xn

3 , xn
5 )

(11.2.45)

To keep the notation more compact, we drop here the iteration counter index and write the above as simply
qn(x2, x4). In this case, the contributions to the energy have the form

(cid:88)
n (cid:104)

log p(xn
1 |

x2)p(x2

3 )p(xn
xn
3 |
|

x4)p(x4

5 )p(xn
xn
5 )
|

(cid:105)qn(x2,x4)

which may be written as

x2)

(cid:105)qn(x2,x4) +

log p(x2

xn
3 )
|

(cid:105)qn(x2,x4)

(cid:88)
n (cid:104)

log p(xn
1 |
(cid:88)
n (cid:104)

+

(cid:88)
n (cid:104)
(cid:105)qn(x2,x4) +

log p(xn
3 |

x4)

(cid:88)
n (cid:104)

log p(x4

xn
5 )
|

(cid:105)qn(x2,x4) +

(cid:88)

n

log p(xn
5 )

DRAFT April 24, 2015

(11.2.46)

(11.2.47)

255

A useful property can now be exploited, namely that each term depends on only those hidden variables in
the family that that term represents. Thus we may write

Expectation Maximisation

(cid:88)
n (cid:104)

log p(xn
1 |
(cid:88)
n (cid:104)

+

x2)

(cid:105)qn(x2) +

log p(xn
3 |

x4)

(cid:88)
n (cid:104)
(cid:105)qn(x4) +

log p(x2

xn
3 )
|

(cid:105)qn(x2)

(cid:88)
n (cid:104)

log p(x4

xn
5 )
|

(cid:105)qn(x4) +

log p(xn
5 )

(cid:88)

n

(11.2.48)

x2). When will the table entry p(x1 = i
|

The ﬁnal term can be set using maximum likelihood. Let us consider therefore a more diﬃcult table,
1 is in
p(x1
state i. Since there is a summation over all the states of variables x2 (due to the average), there is also a term
x2 = j) is
with variable x2 in state j. Hence the contribution to the energy from terms of the form p(x1 = i
|

x2 = j) occur in the energy? This happens whenever xn
|

(cid:88)

n

I [xn

1 = i] qn(x2 = j) log p(x1 = i

x2 = j)
|

(11.2.49)

where the indicator function I [xn
sation of the table, we add a Lagrange term:

1 = i] equals 1 if xn

1 is in state i and is zero otherwise. To ensure normali-

(cid:88)

n

I [xn

1 = i] qn(x2 = j) log p(x1 = i

x2 = j) + λ
|

(cid:40)
1

(cid:88)

−

k

(cid:41)

p(x1 = k

x2 = j)
|

Diﬀerentiating with respect to p(x1 = i
|

x2 = j) and equating to zero we get

I [xn

1 = i]

(cid:88)

n

qn(x2 = j)

p(x1 = i
|

x2 = j)

= λ

or

p(x1 = i

x2 = j)
|

∝

(cid:88)

n

I [xn

1 = i] qn(x2 = j).

Hence

p(x1 = i

x2 = j) =
|

(cid:80)
n
(cid:80)

n,k

I [xn
I [xn

1 = i] qn(x2 = j)
1 = k] qn(x2 = j)

From the E-step we have

qn(x2 = j) = pold(x2 = j

1 , xn
xn

3 , xn
5 )

|

(11.2.50)

(11.2.51)

(11.2.52)

(11.2.53)

(11.2.54)

This optimal distribution is easy to compute since this is the marginal on the family, given some evidential
variables. Hence, the M-step update for the table is

pnew(x1 = i

x2 = j) =
|

(cid:80)
n
(cid:80)

n,k

I [xn
I [xn

1 , xn
xn
1 = i] pold(x2 = j
|
1 , xn
xn
1 = k] pold(x2 = j
|

3 , xn
5 )
3 , xn
5 )

If there were no hidden data, equation (11.2.55) would read

pnew(x1 = i

x2 = j)
|

∝

(cid:88)

n

I [xn

1 = i] I [xn

2 = j]

(11.2.55)

(11.2.56)

All that we do, therefore, in the general EM case, is to replace those deterministic functions such as I [xn
1 , xn
xn
by their missing variable equivalents pold(x2 = i
|

3 , xn

5 ).

2 = i]

256

DRAFT April 24, 2015

Extensions of EM

Figure 11.4: Evolution of the log-likelihood versus iterations un-
der the EM training procedure (from solving the Printer Night-
mare with missing data, exercise(11.1)). Note how rapid progress
is made at the beginning, but convergence can be slow.

11.2.5 Convergence

Convergence of EM can be slow, particularly when the number of missing observations is greater than the
number of visible observations. In practice, one often combines the EM with gradient based procedures to
improve convergence, see section(11.6). Note also that the log likelihood is typically a non-convex function
of the parameters. This means that there may be multiple local optima and the solution found often depends
on the initialisation.

11.2.6 Application to Markov networks

Whilst our examples have been for belief networks, we may also apply the EM application to learning the
parameters of Markov networks that have missing data. For a MN deﬁned over visible and hidden variables
with separate parameters θc for each clique c

p(v, h
|

θ) =

1
Z(θ)

(cid:89)

c

φc(h, v

θc)

|

the EM variational bound is

log p(v

θ)
|

≥

H(q) +

(cid:88)
c (cid:104)

log φc(h, v

θc)
|

(cid:105)q(h) −

log Z(θ)

(11.2.57)

(11.2.58)

where H(p) is the entropy function of a distribution, H(p)
the clique parameters in the second term, the parameters are nevertheless coupled in the normalisation

(cid:105)p(x). Whilst the bound decouples

log p(x)

≡ − (cid:104)

Z(θ) =

(cid:88)

C
(cid:89)

v,h

c=1

φc(h, v

θc),
|

θ = (θ1, . . . , θC)

(11.2.59)

Because of this we cannot directly optimise the above bound on a parameter by parameter basis. One
approach is to use an additional bound log Z(θ) from above, as for iterative scaling, section(9.6.4) to decouple
the clique parameters in Z; we leave the details as an exercise for the interested reader.

11.3 Extensions of EM

11.3.1 Partial M step

It is not necessary to ﬁnd the full optimum of the energy term at each iteration. As long as one ﬁnds a
parameter θ(cid:48) which has a higher energy than that of the current parameter θ, then the conditions required
in section(11.2.2) still hold, and the likelihood cannot decrease at each iteration.

11.3.2 Partial E-step

The E-step requires us to ﬁnd the optimum of

log p(

V|

θ)

N
(cid:88)

≥ −

(cid:104)

n=1

log q(hn

vn)

|

(cid:105)q(hn|vn) +

N
(cid:88)

(cid:104)

n=1

log p(hn, vn

θ)
|

(cid:105)q(hn|vn)

with respect to q(hn

vn). The fully optimal setting is

|
vn) = p(hn

vn)
|

q(hn

|

DRAFT April 24, 2015

(11.3.1)

(11.3.2)

257

024681012−120−110−100−90−80−70−60−50log likelihoodFor a guaranteed increase in likelihood at each iteration, from section(11.2.2) we required that this fully
optimal setting of q is used. Unfortunately, therefore, one cannot in general guarantee that a partial E-step
(in which one would only partially optimsise the lower bound with respect to q for ﬁxed θ) would always
increase the likelihood. Of course, it is guaranteed to increase the lower bound on the likelihood, though
not the likelihood itself. We discuss some partial E-step scenarios below.

Extensions of EM

Intractable energy

The EM algorithm assumes that we can calculate

log p(h, v
(cid:104)

|

θ)

(cid:105)q(h|v)

(11.3.3)

However, there may be cases in which we cannot computationally carry out the averages with respect to the
fully optimal form of q. In this case one may consider a restricted class
of q-distributions for which the
averages can be carried out. For example, one class for which the averages may become computationally
v); another popular class are Gaussian q distri-
tractable is the factorised distributions q(h
|
|
by using a numerical optimisation routine:
butions. We can then ﬁnd the best distribution in the class

v) = (cid:81)

j q(hj

Q

Q

qopt = argmin
q∈Q

KL(q(h)

v, θ))

p(h
|
|

(11.3.4)

Alternatively, one can assume a certain structured form for the q distribution, and learn the optimal factors
of the distribution by free form functional calculus. This approach is taken for example in section(28.4.2).

Viterbi training

An extreme case of a partial E-step is to restrict q(hn
term

vn) to a delta-function. In this case, the entropic
|

(cid:105)q(hn|vn) is constant (zero for discrete h), so that the optimal delta function q is to set

vn)
|

log q(hn
(cid:104)
q(hn

|

vn) = δ (hn, hn
∗ )

where

hn
∗ = argmax

h

p(h, vn

θ)
|

When used in the energy, the average with respect to q is trivial and the energy becomes simply

N
(cid:88)

n=1

log p(hn

∗ , vn

θ)
|

The corresponding bound on the log-likelihood is then

log p(

V|

θ)

≥

H +

N
(cid:88)

n=1

log p(hn

∗ , vn

θ)
|

where H is the entropy of the delta function (zero for discrete h).

(11.3.5)

(11.3.6)

(11.3.7)

(11.3.8)

As a partial justiﬁcation for this technique, provided there is suﬃcient data, one might hope that the likeli-
hood as a function of the parameter θ will be sharply peaked around the optimum value. This means that
v, θopt) by a delta function will be reasonable, and an
at convergence the approximation of the posterior p(h
|
update of EM using Viterbi training will produce a new θ approximately the same as θopt. For any highly
suboptimal θ, however, p(h
v, θ) may be far from a delta function, and therefore a Viterbi update is less
|
reliable in terms of leading to an increase in the likelihood itself. This suggests that the initialisation of θ
for Viterbi training is more critical than for the standard EM. Note that since Viterbi training corresponds
to a partial E-step, EM training with this restricted class of q distribution is therefore only guaranteed to
increase the lower bound on the log likelihood, not the likelihood itself.

This technique is popular in the speech recognition community for training HMMs, section(23.2), from where
the terminology Viterbi training arises.

258

DRAFT April 24, 2015

A failure case for EM

Stochastic EM

Another approximate q(hn
samples from the fully optimal distribution p(hn
discussion on sampling) hn

vn) distribution that is popular is to use an empirical distribution formed by
|
vn, θ). That is one draws samples (see chapter(27) for a
1 , . . . , hn

L from p(hn

vn, θ) and forms a q distribution
|

|

q(hn

vn) =
|

1
L

L
(cid:88)

l=1

δ (hn, hn
l )

The energy then becomes proportional to

N
(cid:88)

L
(cid:88)

n=1

l=1

log p(hn

l , vn

θ)
|

(11.3.9)

(11.3.10)

so that, as in Viterbi training, the energy is always computationally tractable for this restricted q class.
Provided that the samples from p(hn
vn) are reliable, stochastic training will produce an energy function
|
with (on average) the same characteristics as the true energy under the classical EM algorithm. This means
that the solution obtained from stochastic EM should tend to that from classical EM as the number of
samples increases.

11.4 A failure case for EM

Whilst the EM algorithm is very useful, there are some cases in which it does not work. Consider a likelihood
of the form

@@

(cid:90)

θ) =

p(v

|

p (v

|

h

h, θ) p(h),

with

p (v

h, θ) = δ (v, f (h
|

θ))
|

(11.4.1)

If we attempt an EM approach for this, this will fail (see also exercise(7.8)). To see why this happens, the
M-step sets

@@
@@

θnew = argmax

θ

θ)
log p(v, h
|
(cid:104)

p(h|v,θold)
(cid:105)

= argmax

θ

log p(v

h, θ)
(cid:105)

|

(cid:104)

p(h|v,θold)

(11.4.2)

where we used the fact that for this model p(h) is independent of θ. In the case that p (v
then

θ))
h, θ) = δ (v, f (h
|
|

θold)

p(h
|

θold)) p(h)
δ (v, f (h
|

∝

so that optimising the energy gives the update

@@

θnew = argmax

θ

θ))
log δ (v, f (h
(cid:105)
|
(cid:104)

p(h|v,θold)

(11.4.3)

(11.4.4)

|

θold) is zero everywhere expect that h∗ for which v = f (h∗
θold), then the energy term becomes
Since p(h
|
|
log δ (f (h∗
θold), f (h∗
= θold and zero when θ = θold. Hence
θ = θold is optimal and the EM algorithm fails to produce a meaningful parameter update.2 This situation
occurs in practice, and has been noted in particular in the context of Independent Component Analysis[239].
Whilst using a delta-function for the output is clearly extreme, a similar slowing down of parameter updates
can occur when the term p(v

θ)). This is eﬀectively negative inﬁnity if θ
|

h, θ) becomes close to deterministic.

|

One can attempt to heal this behaviour by deriving an EM algorithm based on the distribution

@@

p(cid:15)(v, h
|

θ) = (1

θ)) p(h) + (cid:15)n(v, h), 0
(cid:15))δ (v, f (h
|

−

≤

(cid:15)

≤

1

(11.4.5)

2For discrete variables and the Kronecker delta, the energy attains the maximal value of zero when θ = θold. In the case
of continuous variables, however, the log of the Dirac delta function is not well deﬁned. Considering the delta function as the
limit of a narrow width Gaussian, for any small but ﬁnite width, the energy is largest when θ = θold.

DRAFT April 24, 2015

259

(cid:54)
Variational Bayes

n(v, h) is an arbitrary distribution. The original deterministic model corresponds to

where
ing

(cid:90)

p(cid:15)(v

θ) =
|

p(cid:15)(v, h
|

θ),

h

p(cid:15)(v

θ) = (1
|

−

(cid:15))p0(v

θ) + (cid:15)
|

n(v)

θ). Deﬁn-
p0(v, h
|

@@
@@

(11.4.6)

@@
@@

an EM algorithm for p(cid:15)(v

|

θ), 0 < (cid:15) < 1

cannot decrease the likelihood and therefore satisﬁes

@@

p(cid:15)(v

θnew)
|

−

p(cid:15)(v

θold) = (1
|

−

(cid:15)) (p0(v

θnew)
|

−

p0(v

|

θold)) > 0

which implies

p0(v

|

θnew)

p0(v

|

−

θold) > 0

(11.4.7)

(11.4.8)

This means that the EM algorithm for the non-deterministic case 0 < (cid:15) < 1 is guaranteed to increase the
likelihood under the deterministic model p0(v
θ) at each iteration (unless we are at convergence). See [107]
|
for an application of this ‘antifreeze’ technique to learning Markov Decision Processes with EM.

11.5 Variational Bayes

Variational Bayes is analogous to EM in that it helps us to deal with hidden variables; however it is a
Bayesian method that returns a posterior distribution on parameters, rather than a single best θ as given
by maximum likelihood. To keep the notation simple, we’ll initially assume only a single datapoint with
observation v. Our interest is then the parameter posterior
(cid:88)

(11.5.1)

p(θ

v)
|

∝

p(v

θ)p(θ)
|

θ)p(θ)
p(v, h
|

∝

h

The VB approach assumes a factorised approximation of the joint hidden and parameter posterior, see
ﬁg(11.5):

p(h, θ

v)
|

≈

q(h)q(θ)

(11.5.2)

The optimal settings for the factors q(h) and q(θ) can be found by minimising the Kullback-Leibler divergence
between p(h, θ

v) and q(h)q(θ) as discussed below.

|

A bound on the marginal likelihood

By minimising the KL divergence,

KL(q(h)q(θ)
|

p(h, θ

v)) =
|

log q(h)
(cid:104)

(cid:105)q(h) +

log q(θ)
(cid:104)

(cid:105)q(θ) − (cid:104)

log p(h, θ

v)
|

(cid:105)q(h)q(θ) ≥

0

we arrive at the bound

log p(v)

≥ − (cid:104)

log q(h)

(cid:105)q(h) − (cid:104)

log q(θ)

(cid:105)q(θ) +

log p(v, h, θ)
(cid:104)

(cid:105)q(h)q(θ)

(11.5.3)

(11.5.4)

Minimizing the Kullback-Leibler divergence with respect to q(θ) and q(h) is equivalent to obtaining the
tightest lower bound on log p(v). A simple coordinate-wise procedure in which we ﬁrst ﬁx the q(θ) and solve
for q(h) and then vice versa is analogous to the E and M step of the EM algorithm:

E-step

M-step

qnew(h) = argmin

(cid:16)

KL

q(h)

q(h)qold(θ)
p(h, θ
|

v)
|

(cid:17)

qnew(θ) = argmin

KL(qnew(h)q(θ)

q(θ)

p(h, θ
|

v))
|

(11.5.5)

(11.5.6)

V

H

and hidden variables

, the procedure is described in algorithm(11.3). For
For a set of observations
distributions q(
) and q(θ) which are parameterised or otherwise constrained, the best distributions in the
minimal KL sense are returned. In general, each iteration of VB is guaranteed to increase the bound on
the marginal likelihood, but not the marginal likelihood itself. Like the EM algorithm, VB can (and often
does) suﬀer from local maxima issues. This means that the converged solution can be dependent on the
initialisation.

H

260

DRAFT April 24, 2015

Variational Bayes

Algorithm 11.3 Variational Bayes.

1: t = 0
2: Choose an initial distribution q0(θ).
3: while θ not converged (or likelihood bound not converged) do
4:

t
t + 1
←
) = arg minq(H) KL(q(
qt(
H
qt(θ) = arg minq(θ) KL(qt(

5:

6:
7: end while
8: return qt(θ)

p(
)qt−1(θ)
|
, θ
p(
)q(θ)
H
|

H
H

H
|V

, θ
))

))

|V

hn

vn

N

(a)

θ

hn

N

θ

(b)

(cid:46) Iteration counter
(cid:46) Initialisation

(cid:46) E step
(cid:46) M step

(cid:46) The posterior parameter approximation.

(a): Generic form of a
Figure 11.5:
(b): A
model with hidden variables.
factorised posterior approximation used in
Variational Bayes.

Unconstrained approximations

For ﬁxed q(θ) the contribution to the KL divergence equation (11.5.3) from q(h) is

log q(h)

(cid:105)q(h) − (cid:104)

(cid:104)

log p(v, h, θ)

˜p(h)) + const.
(cid:105)q(h)q(θ) = KL(q(h)
|

where

˜p(h)

(cid:16)

exp

1
˜Z

≡

log p(v, h, θ)
(cid:104)

(cid:105)q(θ)

(cid:17)

and ˜Z is a normalising constant. Hence, for ﬁxed q(θ), the E-step sets q(h) to ˜p,

q(h)

exp

log p(v, h, θ)
(cid:104)

(cid:105)q(θ) ∝

exp

θ)
log p(v, h
|

(cid:105)q(θ)

(cid:104)

∝

Similarly, for ﬁxed q(h), the M-step sets

q(θ)

exp

log p(v, h, θ)
(cid:104)

(cid:105)q(h) = p(θ) exp

(cid:104)

θ)
log p(v, h
|

(cid:105)q(h)

∝

These E and M-step updates are iterated to convergence.

i.i.d. Data

(11.5.7)

(11.5.8)

(11.5.9)

(11.5.10)

Under the i.i.d. assumption, we obtain a bound on the marginal likelihood for the whole dataset
(cid:8)v1, . . . , vN (cid:9):

V

=

@@

log p(

V

)

≥

(cid:88)

(cid:110)

− (cid:104)

n

log q(hn)

(cid:105)q(hn) − (cid:104)

log q(θ)

(cid:105)q(θ) +

(cid:104)

log p(vn, hn, θ)

(cid:111)

(cid:105)q(hn)q(θ)

(11.5.11)

The bound holds for any q(hn) and q(θ) but is tightest for the converged estimates from the VB procedure.
For an i.i.d. dataset, it is straightforward to show that without loss of generality we may assume

q(h1, . . . , hN ) =

q(hn)

(cid:89)

n

Under this we arrive at algorithm(11.4).

DRAFT April 24, 2015

(11.5.12)

261

Algorithm 11.4 Variational Bayes (i.i.d. data).

1: t = 0
2: Choose an initial distribution q0(θ).
3: while θ not converged (or likelihood bound not converged) do
4:

5:

6:

7:

←

t + 1

t
for n = 1 to N do
(cid:16)

qn
t (hn)
end for
qt(θ)

∝

p(θ) exp

(cid:104)
(cid:16)(cid:80)

exp

log p(vn, hn

(cid:17)

θ)
|

(cid:105)qt−1(θ)

log p(vn, hn

(cid:17)

θ)
|

t (hn)

(cid:105)qn

n (cid:104)

8:
∝
9: end while
10: return qn

t (θ)

Variational Bayes

(cid:46) Iteration counter
(cid:46) Initialisation

(cid:46) Run over all datapoints
(cid:46) E step

(cid:46) M step

(cid:46) The posterior parameter approximation.

11.5.1 EM is a special case of variational Bayes

If we wish to ﬁnd a summary of the parameter posterior corresponding to only the most likely point θ∗,
then we may use a restricted q(θ) of the form

q(θ) = δ (θ, θ∗)

(11.5.13)

where θ∗ is the single optimal value of the parameter. If we plug this assumption into equation (11.5.4) we
obtain the bound

log p(v

θ∗)

|

≥ − (cid:104)

log q(h)

(cid:105)q(h) +

log p(v, h, θ∗)
(cid:104)

(cid:105)q(h) + const.

The M-step is then given by

(cid:16)

θ∗ = argmax

θ

θ)
log p(v, h
|

(cid:105)q(h) + log p(θ)

(cid:104)

(cid:17)

(11.5.14)

(11.5.15)

For a ﬂat prior p(θ) = const., this is therefore equivalent to energy maximisation in the EM algorithm.
Using this single optimal value in the VB E-step update for q(hn) we have

qn
t (h)

θ∗)
p(v, h
|

∝

p(h
|

∝

v, θ∗)

(11.5.16)

which is the standard E-step of EM. Hence EM is a special case of VB under a ﬂat prior p(θ) = const. and
delta function approximation of the parameter posterior.

11.5.2 An example: VB for the Asbestos-Smoking-Cancer network

In section(9.4) we showed how to apply Bayesian methods to train a belief network, giving rise to a posterior
distribution over parameter. In our previous discussion, the data was fully observed. Here, however, we wish
to revisit this case, now assuming however that some of the observations can be missing. This complicates
the Bayesian analysis and motivates approximate methods such as VB.
Let’s reconsider Bayesian learning in the binary variable asbestos-smoking-cancer network, as described in
section(9.4)

a, s)p(a)p(s)
p(a, c, s) = p(c
|

in which we use a factorised parameter prior

p(θc)p(θa)p(θs)

(11.5.17)

(11.5.18)

When all the data
section(11.1.1) if the state of a (asbestos) is not observed, the parameter posterior no longer factorises:

is i.i.d. and observed, the parameter posterior factorises. However, as we discussed in

V

p(θa, θs, θc

)

|V

∝

∝

∝

p(θa)p(θs)p(θc)p(

p(θa)p(θs)p(θc)

p(θa)p(θs)p(θc)

n
(cid:89)

n

V|
(cid:89)

θa, θs, θc)
p(vn

θa, θs, θc)
|

p(sn

θs)
|

(cid:88)

an

p(cn

|

sn, an, θc)p(an

θa)
|

(11.5.19)

(11.5.20)

(11.5.21)

262

DRAFT April 24, 2015

Variational Bayes

θs

s

N

θa

a

c

θc

(a)

θs

θa

a

N

θc

(b)

Figure 11.6: (a): A model for the relation-
ship between lung Cancer, Asbestos expo-
sure and Smoking with factorised parame-
ter priors. Variables c and s are observed,
but variable a is consistently missing. (b):
A factorised parameter posterior approxi-
mation.

where the summation over a prevents the factorisation into a product of the individual table parameters.
This means that it becomes more awkward to represent the posterior since we cannot exactly do this by
using posterior distributions on each parameter table alone. This is a situation in which VB can be useful
since it enables us to impose factorisations on the posterior. In VB we consider an approximation to the
posterior over the parameters and latent variables q(h)q(θ).
In this case θ = (θa, θs, θc) and the latent
variables are asbestos, an (one for each datapoint). For this example the visible variables are smoking and
cancer:

, n = 1, . . . , N . The exact joint distribution over all these variables is then

=

sn, cn
{

}

V

p(θa, θs, θc, a1, . . . , aN

)

|V

∝

p(θa)p(θs)p(θc)
(cid:124)
(cid:123)(cid:122)
(cid:125)
prior

(cid:89)

n
(cid:124)

p(cn

sn, an, θc)p(sn
|

θs)p(an
|

θa)
|

(cid:123)(cid:122)
posterior

(cid:125)

In VB we make a factorised assumption, splitting the parameters and latent variables:

p(θ, a1:N

)

|V

≈

q(θ)q(a1:N )

(11.5.22)

(11.5.23)

From the general results in equation (11.5.9), we have (ignoring terms that are independent of a in the
exponent)

@@

q(a1:N )

exp

∝

(cid:32)

(cid:88)
n (cid:104)

log p(cn

sn, an, θc)
|

(cid:105)q(θc)

+

(cid:88)
n (cid:104)

log p(an

θa)

|

(cid:105)q(θa)

(cid:33)

From this we see immediately that our approximation automatically factorises

q(a1:N ) =

q(an)

(cid:89)

n

Similarly, from equation (11.5.10) we have

q(θ)

∝

p(θ) exp

= p(θ) exp

(cid:32)

(cid:32)

(cid:88)
n (cid:104)

(cid:88)
n (cid:104)

log p(cn

log p(cn

sn, an, θc)p(sn
|

|

θs)p(an

sn, an, θc)
|

(cid:105)q(an)

(cid:33) (cid:40)

(cid:89)

n

(cid:33)

θa)
|

(cid:105)q(an)
(cid:41)

p(sn

θs)
|

exp

(cid:32)

(cid:88)
n (cid:104)

log p(an

(11.5.24)

(11.5.25)

(11.5.26)

(cid:33)

θa)

(cid:105)q(an)

|

(11.5.27)

Since the parameter prior is factorised, we can collect terms in θa, θs and θc and see that the VB assumption
automatically results in a factorised parameter posterior approximation. Hence, our VB approximation is
of the form, see ﬁg(11.6),

p(θa, θs, θc, a1, . . . , aN

)

|V

≈

q(θa)q(θc)q(θs)

q(an)

(cid:89)

n

(11.5.28)

All that remains is to form E-step (q(an) updates) and M-step (q(θ) updates), as described below.

DRAFT April 24, 2015

263

M-step: q(θ) updates

From above we have

q(θa)

∝

p(θa)

(cid:89)

n

exp

(cid:104)

log p(an

θa)
|

(cid:105)q(an)

where

log p(an
(cid:104)

θa)
|

(cid:105)q(an) = q(an = 1) log θa + q(an = 0) log (1

−

θa)

Hence

exp

log p(an
(cid:104)

θa)
|

(cid:105)q(an) = θq(an=1)

a

θa)q(an=0)

(1

−

It is convenient to use a Beta distribution prior,

p(θa) = B (θa

α, β)
|

∝

θα−1
a

(1

−

θa)β−1

since the posterior approximation is then also a Beta distribution:

(cid:32)

q(θa) = B

θa

α +
|

q(an = 1), β +

(cid:88)

n

A similar calculation gives

(cid:32)

q(θs) = B

α +

θs

|

I [sn = 1] , β +

(cid:88)

n

(cid:33)

q(an = 0)

(cid:33)

I [sn = 0]

(cid:88)

n

(cid:88)

n

Variational Bayes

(11.5.29)

(11.5.30)

(11.5.31)

(11.5.32)

(11.5.33)

(11.5.34)

Finally, we have a table for each of the parental states of c. For example

q(θc(a = 0, s = 1)) = B

(cid:32)

α +

θc

|

(cid:88)

n

I [sn = 1] q(an = 0), β +

(cid:88)

n

I [sn = 0] q(an = 1)

(11.5.35)

(cid:33)

These are reminiscent of the standard Bayesian equations, equation (9.4.15), except that the missing data
counts have been replaced by q’s.

E-step: q(an) updates

From equation (11.5.24), we have

(cid:16)

q(an)

exp

(cid:105)q(θc) +
For example, if assume that for datapoint n, s is in state 1 and c in state 0, then

θa)
|

(cid:105)q(θa)

∝

(cid:104)

|

log p(cn

sn, an, θc)

log p(an
(cid:104)

(cid:17)

q(an = 1)

exp

(cid:104)

∝

log (1

−

θc(s = 1, a = 1))

(cid:105)q(θc(s=1,a=1)) +

log θa
(cid:104)

(cid:105)q(θa)

and

q(an = 0)

exp

∝

(cid:16)

(cid:104)

log (1

−

θc(s = 1, a = 0))

log (1
(cid:104)

−

θa)

(cid:105)q(θa)

(cid:17)

(cid:105)q(θc(s=1,a=1)) +
log θ
(cid:104)

These updates require the Beta distribution averages
straightforward to compute, see exercise(8.17).

(cid:105)B(θ|α,β) and

log (1
(cid:104)

−

θ)

(cid:105)B(θ|α,β); these are

The complete VB procedure is then given by iterating equations (11.5.33,11.5.34,11.5.35) and (11.5.37,11.5.38)
until convergence.

264

DRAFT April 24, 2015

(11.5.36)

(11.5.37)

(11.5.38)

Optimising the Likelihood by Gradient Methods

Given a converged factorised approximation, computing a marginal table such as p(a = 1
forward under the approximation

|V

) is then straight-

p(a = 1

)

|V

≈

(cid:90)

θa

q(a = 1
|

θa)q(θa) =

α + (cid:80)

n q(an = 1)
n q(an = 0) + β + (cid:80)

α + (cid:80)

n q(an = 1)

(11.5.39)

The application of VB to learning the tables in arbitrarily structured BNs is a straightforward extension
of the technique outlined here. Under the factorised approximation, q(h, θ) = q(h)q(θ), one will always
obtain a simple updating equation analogous to the full data case, but with the missing data replaced by
variational approximations. Nevertheless, if a variable has many missing parents, the number of states in
the average with respect to the q distribution can become intractable, and further constraints on the form
of the approximation, or additional bounds are required.

One may readily extend the above to the case of Dirichlet distributions on multinomial variables, see
exercise(11.5). Indeed, the extension to the exponential family is straightforward.

11.6 Optimising the Likelihood by Gradient Methods

The EM algorithm typically works well when the amount of missing information is small compared to the
complete information.
In this case EM exhibits approximately the same convergence as Newton based
gradient method[255]. However, if the fraction of missing information approaches unity, EM can converge
very slowly.
In the case of continuous parameters θ, an alternative is to compute the gradient of the
likelihood directly and use this as part of a standard continuous variable optimisation routine. The gradient
is straightforward to compute using the following identity. Consider the log likelihood

L(θ) = log p(v

θ)

|

The derivative can be written

∂θL(θ) =

1
p(v

|

∂θp(v

θ) =
|

θ)

(cid:90)

∂θ

θ)
p(v, h
|

h

1
p(v

θ)
|

At this point, we take the derivative inside the integral

(11.6.1)

(11.6.2)

∂θL(θ) =

1
p(v

|

θ)

(cid:90)

∂θp(v, h
|

h

θ) =

(cid:90)

h

θ) =
v, θ)∂θ log p(v, h
p(h
|
|

∂θ log p(v, h
|
(cid:104)

θ)

(cid:105)p(h|v,θ)

(11.6.3)

where we used ∂ log f (x) = (1/f (x))∂f (x). The right hand side is the average of the derivative of the
log complete likelihood. This is closely related to the derivative of the energy term in the EM algorithm,
though note that the average here is performed with respect the current distribution parameters θ and not
θold as in the EM case. Used in this way, computing the derivatives of latent variable models is relatively
straightforward. These derivatives may then be used as part of a standard optimisation routine such as
conjugate gradients[255].

11.6.1 Undirected models

Whilst equation (11.6.3) represents the general case, it is not always possible to easily compute the required
averages. Consider an undirected model which contains both hidden and visible variables

p(v, h
|

θ) =

1
Z(θ)

θ))
exp (φ(v, h
|

For i.i.d. data, the log likelihood on the visible variables is (assuming discrete v and h)

L(θ) =



log

(cid:88)

n

(cid:88)

h

exp φ(vn, h
θ)
|

−

log

(cid:88)

h,v

θ)
exp φ(v, h
|





DRAFT April 24, 2015

(11.6.4)

(11.6.5)

265

which has gradient


∂
∂θ

L =

(cid:88)

n







(cid:28) ∂
∂θ

(cid:124)

(cid:29)

θ)

φ(vn, h
|
(cid:123)(cid:122)
clamped average

p(h|vn,θ)
(cid:125)



(cid:28) ∂
∂θ

−

(cid:124)

(cid:29)





p(h,v|θ)

(cid:125)

θ)
φ(v, h
|
(cid:123)(cid:122)
free average

Exercises

(11.6.6)

For a Markov Network p that is intractable (the partition function Z cannot be computed eﬃciently and
averages w.r.t. p cannot be computed exactly), the gradient is particularly diﬃcult to estimate since it is
the diﬀerence of two averages, each of which needs to be estimated. Even getting the sign of the gradient
correct can therefore be computationally diﬃcult. For this reason learning in general unstructured Markov
networks is particularly diﬃcult (the unstructured Boltzmann machine with hidden units being a particular
case in point).

11.7 Summary

•

•

•

•

•

Provided the data is missing at random, we can safely learn parameters by maximising the likelihood of the
observed data.
Variational Expectation Maximisation is a general-purpose algorithm for maximum likelihood learning under
missing information.
The classical EM algorithm is a special case of V-EM and guarantees an improvement (non-decrease) in
the likelihood at each iteration.
Bayesian learning in the case of missing information is potentially problematic since the posterior is typically
not factored according to the prior assumptions. In this case approximations are useful, such as variational
Bayes, which assumes a factorisation between the parameters and the latent/missing variables.
The gradient can be easily computed for latent variable models and may be used as part of an optimisation
routine. This provides an alternative training approach in cases when EM is slow to converge.

11.8 Code

demoEMchestclinic.m: Demo of EM in learning the Chest Clinic Tables

In the demo code we take the original Chest Clinic network [184] and draw data samples from this network.
Our interest is then to see if we can use the EM algorithm to estimate the tables based on the data (with
some parts of the data missing at random). We assume that we know the correct BN structure, only that
the CPTs are unknown. We assume the logic gate table is known, so we do not need to learn this.

EMbeliefnet.m: EM training of a Belief Network

code implements maximum likelihood learning of BN tables based on data with possibly missing

The
values.

11.9 Exercises

Exercise 11.1 (Printer Nightmare continued). Continuing with the BN given in ﬁg(9.21), the following
table represents data gathered on the printer, where ? indicates that the entry is missing. Each column
represents a datapoint. Use the EM algorithm to learn all CPTs of the network.

266

DRAFT April 24, 2015

Exercises

fuse assembly malfunction
drum unit
toner out
poor paper quality
worn roller
burning smell
poor print quality
wrinkled pages
multiple pages fed
paper jam

?
?
1
1
0
0
1
0
0
?

?
0
1
0
0
?
1
0
?
0

?
?
0
1
?
?
1
1
1
1

1
0
?
0
?
1
0
0
0
1

0
1
?
1
?
0
1
0
?
?

0
0
1
?
0
0
1
0
0
0

?
0
0
1
1
0
0
?
1
1

0
1
1
0
?
0
1
0
0
1

?
?
0
1
0
0
0
1
1
1

0
?
?
1
0
?
0
?
?
1

0
1
0
?
?
0
1
0
0
0

?
1
1
1
0
?
1
0
0
?

1
?
?
1
?
1
?
1
?
0

?
0
0
?
1
0
?
1
0
1

1
0
?
0
1
?
0
1
1
?

The table is contained in EMprinter.mat, using states 1, 2, nan in place of 0, 1, ? (since BRMLtoolbox requires
states to be numbered 1,2,....). Given no wrinkled pages, no burning smell and poor print quality, what is
the probability there is a drum unit problem?

Exercise 11.2. Consider the following distribution over discrete variables,

p(x1, x2, x3, x4, x5) = p(x1

x2, x4)p(x2
|

x3)p(x3
|

x4)p(x4
|

x5)p(x5),
|

(11.9.1)

@@

in which the variables x2 and x4 are consistently hidden in the training data, and training data for x1, x3, x5
are always present. Derive the EM update for the table

p(x1

x2, x4).

|

Exercise 11.3. Consider a simple two variable BN

p(y, x) = p(y

x)p(x)
|

(11.9.2)

{

(yn, xn) , n = 1, . . . , N

. You have a set of training
where both y and x are binary variables, dom(x) =
, in which for some cases xn may be missing. We are speciﬁcally interested
data
in learning the table p(x) from this data. A colleague suggests that one can set p(x) by simply looking at
datapoints where x is observed, and then setting p(x = 1) to be the fraction of observed x that is in state 1.
Explain how this suggested procedure relates to maximum likelihood and EM.

, dom(y) =

1, 2
}

1, 2
}

}

{

{

Exercise 11.4. Assume that a sequence v1, . . . , vT , vt
single chain of length T , we have

∈ {

1, . . . , V

}

is generated by a Markov chain. For a

p(v1, . . . , vT ) = p(v1)

T −1
(cid:89)

t=1

p(vt+1

vt)
|

For simplicity, we denote the sequence of visible variables as

v = (v1, . . . , vT )

For a single Markov chain labelled by h,

h) = p(v1

p(v

|

T −1
(cid:89)

t=1

h)

|

p(vt+1

vt, h)

|

(11.9.3)

(11.9.4)

(11.9.5)

In total there are a set of H such Markov chains (h = 1, . . . , H). The distribution on the visible variables is
therefore

p(v) =

H
(cid:88)

h=1

p(v

h)p(h)
|

(11.9.6)

1. There are a set of training sequences, vn, n = 1, . . . , N . Assuming that each sequence vn is inde-
pendently and identically drawn from a Markov chain mixture model with H components, derive the
Expectation Maximisation algorithm for training this model.

2. The ﬁle sequences.mat contains a set of ﬁctitious bio-sequence in a cell array sequences{\acup{n}}(t).
Thus sequences{3}(:) is the third sequence, GTCTCCTGCCCTCTCTGAAC which consists of 20 timesteps.
There are 20 such sequences in total. Your task is to cluster these sequences into two clusters, assum-
ing that each cluster is modelled by a Markov chain. State which of the sequences belong together by
vn) is highest. You may wish to use mixMarkov.m.
assigning a sequence vn to that state for which p(h
|

DRAFT April 24, 2015

267

Exercise 11.5. Write a general purpose routine VBbeliefnet(pot,x,pars) along the lines of EMbeliefnet.m
that performs Variational Bayes under a Dirichlet prior, using a factorised parameter approximation. As-
sume both global and local parameter independence for the prior and the approximation q, section(9.4.1).

Exercise 11.6. Consider a 3 ‘layered’ Boltzmann Machine which has the form

Exercises

p(v, h1, h2, h3

θ) =
|

1
Z

φ(v, h1

|

θ1)φ(h1, h2

θ2)φ(h2, h3
|

θ3)
|

where dim v = dim h1 = dim h2 = dim h3 = V

φ(x, y

|

θ) = exp





V
(cid:88)

i,j=1

Wijxiyj + Aijxixj + Bijyiyj





(11.9.7)

(11.9.8)

All variables are binary with states 0, 1 and the parameters for each layer l are θl = (cid:8)Wl, Al, Bl(cid:9).

1. In terms of ﬁtting the model to visible data v1, . . . , vN , is the 3 layered model above any more powerful

than ﬁtting a two-layered model (the factor φ(h2, h3

θ3) is not present in the two-layer case)?

|

2. If we use a restricted potential

φ(x, y

θ) = exp
|



Wijxiyj







(cid:88)

i,j

(11.9.9)

is the three layered model more powerful in being able to ﬁt the visible data than the two-layered model?

Exercise 11.7. The sigmoid Belief Network is deﬁned by the layered network

p(xL)

L
(cid:89)

l=1

p(xl−1

xl)
|

(11.9.10)

where vector variables have binary components xl
addition

0, 1
}

∈ {

wl and the width of layer l is given by wl. In

p(xl−1

xl) =
|

wl(cid:89)

i=1

p(xl−1
i

xl)
|

and

p(xl−1

xl) = σ
i = 1
|

(cid:16)

i,lxl(cid:17)
wT

,

σ(x) = 1/(1 + e−x)

(11.9.11)

(11.9.12)

for a weight vector wi,l describing the interaction from the parental layer. The top layer, p(xL) describes a
factorised distribution p(xL

1 ), . . . , p(xL

wL).

1. Draw the Belief Network structure of this distribution.

2. For the layer x0, what is the computational complexity of computing the likelihood p(x0), assuming

that all layers have equal width w?

3. Assuming a fully factorised approximation for an equal width network,

p(x1, . . . , xL

x0)
|

≈

L
(cid:89)

w
(cid:89)

l=1

i=1

q(xl
i)

(11.9.13)

write down the energy term of the Variational EM procedure for a single data observation x0, and
discuss the tractability of computing the energy.

Exercise 11.8. Show how to ﬁnd the components 0

(θb, θg, θp)

≤

≤

1 that maximise equation (11.1.9).

268

DRAFT April 24, 2015

Exercises

Exercise 11.9. A 2
learned using maximal marginal likelihood in which x2 is never observed. Show that if

2 probability table, p(x1 = i, x2 = j) = θi,j, with 0

θi,j

×

≤

≤

1, (cid:80)2

i=1

θ =

(cid:18) 0.3 0.3
0.2 0.2

(cid:19)

is given as a maximal marginal likelihood solution, then

θ =

(cid:18) 0.2 0.4
0
0.4

(cid:19)

has the same marginal likelihood score.

(cid:80)2

j=1 θi,j = 1 is

(11.9.14)

(11.9.15)

DRAFT April 24, 2015

269

Exercises

270

DRAFT April 24, 2015

CHAPTER 12

Bayesian Model Selection

So far we’ve mostly used Bayes’ rule for inference at the parameter level. Applied at the model level, Bayes’
rule gives a method for evaluating competing models. This provides an alternative to classical statistical
hypothesis testing techniques.

12.1 Comparing Models the Bayesian Way

Given two models M1 and M2 with parameters θ1, θ2 and associated parameter priors,

p(x, θ1

M1) = p(x
|

θ1, M1)p(θ1
|

M1),
|

p(x, θ2

M2) = p(x
|

θ2, M2)p(θ2
|

|

M2)

(12.1.1)

how can we compare the performance of the models in ﬁtting a set of data
? The
application of Bayes’ rule to models gives a framework for answering questions like this – a form of Bayesian
hypothesis testing, applied at the model level. More generally, given an indexed set of models M1, . . . , Mm,
and associated prior beliefs in the appropriateness of each model p(Mi), our interest is the model posterior
probability

x1, . . . , xN
{

=

D

}

p(

p(Mi

) =

|D

D|

Mi)p(Mi)
)
p(

D

where

) =

p(

D

m
(cid:88)

i=1

p(

Mi)p(Mi)

D|

Model Mi is parameterised by θi, and the model likelihood is given by

(cid:90)

p(

Mi) =

D|

p(

θi, Mi)p(θi

D|

Mi)dθi

|

(12.1.2)

(12.1.3)

(12.1.4)

In discrete parameter spaces, the integral is replaced with summation. Note that the number of parameters
dim (θi) need not be the same for each model.

A point of caution here is that p(Mi
) only refers to the probability relative to the set of models speciﬁed
M1, . . . , Mm. This is not the absolute probability that model M ﬁts ‘well’. To compute such a quantity
would require one to specify all possible models. Whilst interpreting the posterior p(Mi
) requires some
care, comparing two competing model hypotheses Mi and Mj is straightforward and only requires the Bayes’
factor

|D

|D

p(Mi
p(Mj
(cid:123)(cid:122)
(cid:124)
Posterior Odds

|D
|D

)
)
(cid:125)

=

Mi)
p(
D|
Mj)
p(
D|
(cid:123)(cid:122)
(cid:125)
(cid:124)
Bayes’ Factor

p(Mi)
p(Mj)
(cid:124) (cid:123)(cid:122) (cid:125)
Prior Odds

271

(12.1.5)

Illustrations : coin tossing

(a)

(b)

(a): Discrete prior p(θ

Figure 12.1:
Mf air) model of a ‘fair’ coin. A perfectly unbiased coin has θ = 0.5,
|
which would corresponds to a prior δ(θ, 0.5) – however, we assume a more general form here to illustrate
how richer prior assumptions can be used. (b): Prior p(θ
Mbiased) for a biased ‘unfair’ coin. In both cases
|
we are making explicit choices here about what we consider to be ‘fair’ and ‘unfair’.

which does not require integration/summation over all possible models. We also call the posterior odds the
posterior Bayes’ factor.

12.2 Illustrations : coin tossing

We’ll consider two illustrations for testing whether a coin is biased or not. The ﬁrst uses a discrete parameter
space to keep the mathematics simple. In the second we use a continuous parameter space. In both cases
that consists of a sequence x1, . . . , xN of outcomes dom(xn) =
we have a dataset

.

heads, tails
}
{

D

12.2.1 A discrete parameter space

Mf air) in ﬁg(12.1a) and for the biased coin the distribution p(θ
|

We consider two competing models, one corresponding to a fair coin, and the other a biased coin. The
bias of the coin, namely the probability that the coin will land heads, is speciﬁed by θ, so that a truly
. For the fair coin we use the
fair coin has θ = 0.5. For simplicity we assume dom(θ) =
distribution p(θ
Mbiased) in ﬁg(12.1b). Note
that these priors essentially encode our subjective beliefs about what we mean for a coin to be biased, or
not. We are free to choose any prior distributions we wish. A strength of the Bayesian framework is that in
doing so, we spell out what we mean by (in this case) ‘biased’ or ‘unbiased’. This is the ‘subjective’ beauty
of the framework; if others disagree with the subjective choice of the prior, they are free to declare their
own competing assumptions and carry through the corresponding analysis, adding to the possible models
available.

0.1, 0.2, . . . , 0.9
}
{

|

For each model M , the likelihood for the model to generate the data
tails, is given by

D

, which contains NH heads and NT

p(

M ) =

D|

(cid:88)

p(

D|

θ
= 0.1NH (1

−

θ, M )p(θ

M ) =

(cid:88)

θNH (1

|

θ
0.1)NT p(θ = 0.1

θ)NT p(θ

M )
|

−

M ) + . . . + 0.9NH (1
|

−

0.9)NT p(θ = 0.9
|

M )

(12.2.1)

(12.2.2)

Assuming that p(Mf air) = p(Mbiased) the posterior odds is given by the ratio of the two model likelihoods.

Example 12.1 (Discrete parameter space).

5 Heads and 2 Tails Using NH = 5, NT = 2 in equation (12.2.2) we obtain p(

Mf air) = 0.00786 and

D|

p(

Mbiased) = 0.0072. The posterior odds is

D|

p(Mf air
p(Mbiased

)
)

|D
|D

= 1.09

(12.2.3)

indicating that there is little to choose between the two models.

272

DRAFT April 24, 2015

00.10.20.30.40.50.60.70.80.9100.20.40.60.800.10.20.30.40.50.60.70.80.9100.050.10.15Illustrations : coin tossing

(a)

(b)

Figure 12.2: Probability density priors on the probability of a Head p(θ).
p(θ
Note the diﬀerent vertical scales in the two cases.

(b): For an biased coin we choose p(θ

Mf air) = B (θ
|

50, 50).

Mbiased) = 0.5 (B (θ
|

(a): For a fair coin we choose
10, 3)).
|

3, 10) + B (θ
|

|

50 Heads and 20 Tails For this case, repeating the above calculation, we obtain p(

Mf air) = 1.5

D|

10−20

×

and p(

Mbiased) = 1.4

10−19. The posterior odds is

×

= 0.109

(12.2.4)

D|
p(Mf air
p(Mbiased

)
)

|D
|D

indicating that we have around 10 times the belief in the biased model as opposed to the fair model.

12.2.2 A continuous parameter space

Here we repeat the above calculation but for continuous parameter spaces. As for the discrete case, we are
free to choose any prior we wish; below we consider some simple priors for which the integrations required
are straightforward.

Fair coin

For the fair coin, a uni-modal prior is appropriate. We use Beta distribution

@@

Mf air) = B (θ

p(θ

|

a, b) ,

|

B (θ

a, b)
|

≡

1
B(a, b)

θa−1 (1

θ)b−1

−

(12.2.5)

for convenience since this is conjugate to the binomial distribution and the required integrations are trivial.
For the fair coin prior we chose a = 50, b = 50, as shown in ﬁg(12.2a). The likelihood is then given by

p(

Mf air) =

D|

(cid:90)

θ

p(θ)θNH (1

θ)NT =

−

1
B(a, b)

(cid:90)

θ

θa−1 (1

−

θ)b−1 θNH (1

θ)NT

−

θNH +a−1 (1

−

θ)NT +b−1 =

B(NH + a, NT + b)
B(a, b)

=

1
B(a, b)

Biased coin

(cid:90)

θ

(12.2.6)

(12.2.7)

For the biased coin, we use a bimodal distribution formed, for convenience, as a mixture of two Beta
distributions:

Mbiased) =

p(θ

|

1
2

[B (θ

|

a1, b1) + B (θ

a2, b2)]

|

as shown in ﬁg(12.2b). The model likelihood p(

Mbiased) is given by

D|

(12.2.8)

@@

p(

Mbiased) =

D|

(cid:90)

p(θ

|

θ

Mbiased)θNH (1

θ)NT =

−

(cid:26) B(NH + a1, NT + b1)
B(a1, b1)

1
2

+

B(NH + a2, NT + b2)
B(a2, b2)

(cid:27)

DRAFT April 24, 2015

(12.2.9)

273

00.20.40.60.810246800.20.40.60.8100.511.5Occam’s Razor and Bayesian Complexity Penalisation

n) for n = 1 (top) to n = 5 (bottom) dice. Plotted
Figure 12.3: The likelihood of the total dice score, p(t
|
along the horizontal axis is the total score t. The vertical line marks the comparison for p(t = 9
n) for the
|
diﬀerent number of die. The more complex models, which can reach more states, have lower likelihood, due
to normalisation over t.

Assuming no prior preference for either a fair or biased coin p(M ) = const., and repeating the above scenario
in the discrete parameter case:

Example 12.2 (Continuous parameter space).

5 Heads and 2 Tails Here p(

Mf air) = 0.0079 and p(

D|

Mbiased) = 0.00622. The posterior odds is

D|

p(Mf air
p(Mbiased

)
)

|D
|D

= 1.27

(12.2.10)

indicating that there is little to choose between the two models.

50 Heads and 20 Tails Here p(

Mf air) = 9.4

D|

10−21 and p(

×

Mbiased) = 1.09

D|

×

10−19. The posterior

odds is

p(Mf air
p(Mbiased

)
)

|D
|D

= 0.087

(12.2.11)

indicating that we have around 11 times the belief in the biased model as opposed to the fair model.

12.3 Occam’s Razor and Bayesian Complexity Penalisation

We return to the dice scenario of section(1.3.1). There we assumed there are two dice whose scores s1 and
s2 are not known. Only the sum of the two scores t = s1 + s2 is known. We then computed the posterior
t = 9) for the two die. We repeat the calculation here but now for multiple
joint score distribution p(s1, s2
|
dice and with the twist that we don’t know how many dice there are1, only that the sum of the scores is 9.
That is, we know (cid:80)n
i=1 si = 9 but are not told the number of dice involved n. Assuming a priori that any
number n is equally likely, what is the posterior distribution over n?

From Bayes’ rule, we need to compute the posterior distribution over models

p(n

t) =
|

n)p(n)
p(t
|
p(t)

1This description of Occam’s razor is due to Taylan Cemgil.

(12.3.1)

274

DRAFT April 24, 2015

000.10.2000.10.2000.10.2000.10.212345678910111213141516171819202122232425262728293000.10.2Occam’s Razor and Bayesian Complexity Penalisation

0.5

0

1

2

3

n

4

5

6

Figure 12.4: The posterior distribution p(n
t = 9) of
|
the number of die given the observed summed score of
9.

In the above the likelihood term is given by

p(t

n) =
|

(cid:88)

s1,...,sn

p(t, s1, . . . , sn

n) =

|

(cid:88)

s1,...,sn

p(t

s1, . . . , sn)
|

(cid:89)

i

p(si) =

(cid:88)

s1,...,sn

(cid:34)

I

t =

n
(cid:88)

i=1

si

(cid:35)

(cid:89)

i

p(si)

(12.3.2)

where p(si) = 1/6 for all scores si. By enumerating all 6n states, we can explicitly compute p(t
n), as
|
displayed in ﬁg(12.3). The important observation is that as the models explaining the data become more
‘complex’ (n increases), more states become accessible. Assuming p(n) = const., the posterior p(n
t = 9)
|
is plotted in ﬁg(12.4). A posteriori, there are only 3 plausible models, namely n = 2, 3, 4 since the rest are
either too complex, or impossible.

++

Occam’s Razor

For a model M with parameters θ, the likelihood of generating the data is given by

p(

M ) =

D|

(cid:90)

p(

θ

θ, M )p(θ

D|

M )

|

To simplify the argument, we place ﬂat priors on the parameter space,

p(θ

M ) = 1/V
|

where V is the volume (number of states in the discrete case) of the parameter spaces. Then

p(

M ) =

D|

(cid:82)
θ p(

θ, M )

D|
V

We can approximate the likelihood p(

θ, M ) by thresholding it at a value (cid:15):

D|

p(

θ, M )

D|

≈

(cid:26) L∗ p(
p(
0

θ, M )
(cid:15)
θ, M ) < (cid:15)

≥

D|
D|

That is, when the likelihood is appreciable (bigger than (cid:15)) we give it value L∗, otherwise we give the value
0. Then

M ) = L∗ V (cid:15)
V

p(

D|

,

V (cid:15)

≡

(cid:90)

1

θ:p(D|θ,M )≥(cid:15)

One can then interpret the model likelihood p(
plied by the fraction of the parameter volume for which the likelihood is high.

D|

M ) as approximately the high likelihood value L∗ multi-

D|

(a): The likelihood p(

θ, Mcomplex) has a higher
Figure 12.5:
maximum value than then simpler model, but the likelihood
drops quickly as we move away from regions of high likelihood.
(b): The likelihood p(
θ, Msimple) has a lower maximum value
than then complex model, but the likelihood changes less quickly
as we move away from regions of high likelihood. The corre-
sponding volume of parameter space in which the model ﬁts well
can then be higher for the simpler model.

D|

275

(a)

(b)

DRAFT April 24, 2015

Occam’s Razor and Bayesian Complexity Penalisation

(a)

(b)

(c)

Figure 12.6:
y = ax has maximum likelihood 5.9
maximum likelihood 1.1

10−10.

×

×

(a): Data for which we wish to ﬁt a regression model.
10−11.

(b): The best ‘simple’ model ﬁt
(c): The best ‘complex’ model ﬁt y = ax + cos(bx) has

Consider two models Msimple and Mcomplex with corresponding parameters θ and φ. Then, for ﬂat parameter
priors, we can approximate

Msimple) = L∗

simple

p(

D|

V (cid:15)
simple
Vsimple

,

p(

D|

Mcomplex) = L∗

complex

V (cid:15)
complex
Vcomplex

At this point it is useful to reﬂect on what constitutes a ‘complex’ model. This can be characterised by a
relative ﬂexibility in the data generating process, compared to a ‘simple’ model. Consequently, this means
that as we move in the space of the complex model, we will generate very diﬀerent datasets compared to
. This is a form of parameter sensitivity meaning that the likelihood of generating
our given dataset
the observed data
will typically drop dramatically as we move away from regions of parameter space in
which the model ﬁts well. Hence a simple model Msimple that has a similar maximum likelihood to a complex
model, L∗
complex, typically the fraction of the parameter space in which the likelihood is appreciable
Mcomplex),
will be smaller for the complex model than the simple model, meaning that p(
see ﬁg(12.5). If we have no prior preference for either model p(Msimple) = p(Msimple) the Bayes factor is
given by

D
D
L∗

Msimple) > p(

simple ≈

D|

D|

p(Msimple
p(Mcomplex

)
)

|D
|D

=

p(
p(

D|
D|

Msimple)
Mcomplex)

and the Bayes factor will typically prefer the simpler of two competing models with similar maximum
likelihood values. This demonstrates the Occam’s razor eﬀect of Bayesian model inference which penalises
models which are over complex.

As an example of this eﬀect, consider the regression problem in ﬁg(12.6) for which we consider the following
two models of the ‘clean’ underlying regression function:

Msimple : y0 = ax

Mcomplex : y0 = ax + cos(bx)

To account for noise in the observations, y = y0 + (cid:15), (cid:15)

(cid:0)(cid:15) 0, σ2(cid:1), we use a mode

∼ N

p(y

x, a, Msimple) =
|

N

(cid:0)y ax, σ2(cid:1)

The likelihood of a collection

Y

of independent observations for a set of inputs

is then

X

(cid:90)

p(

Y|X

, Msimple) =

p(a
|

a

Msimple)

N
(cid:89)

n=1

p(yn

|

xn, a, Msimple)

276

DRAFT April 24, 2015

05101520−2024681012xy05101520−2024681012xy05101520−2024681012xyA continuous example : curve ﬁtting

(a)

(b)

, a, b, Mcomplex) (b):
Figure 12.7: Likelihood plots for the problem in ﬁg(12.6).
The likelihood p(
, a, Msimple) plotted on the same scale as (a). This displays the characteristic of a
‘complex’ model in the likelihood drops dramatically as we move only a small distance in parameter space
from a point with high likelihood. Whilst the maximum likelihood of the complex model is higher than the
simpler model, the volume of parameter space in which the complex model ﬁts the data well is smaller than
for the simpler model, giving rise to p(

(a): The likelihood p(

, Msimple) > p(

, Mcomplex).

Y|X

Y|X

Y|X

Y|X

Similarly,

p(y

x, a, b, Mcomplex) =
|

N

(cid:0)y ax + cos(bx), σ2(cid:1)

and

p(

Y|X

, Mcomplex) =

Mcomplex)
p(a, b
|

a,b

(cid:90)

N
(cid:89)

n=1

p(yn

|

xn, a, b, Mcomplex)

Using a discrete set of 21 values for a, evenly spaced from 0.4 to 0.6, and 121 discrete values for b, evenly
spaced from -15 to 15, we can compute the corresponding likelihoods p(
, a, b, Mcomplex). For this data, the maximum likelihoods are
p(

, a, Msimple) and

Y|X

Y|X

max
a

p(

Y|X

, a, Msimple) = 5.9

10−11,

×

max
a,b

p(

Y|X

, a, b, Mcomplex) = 1.1

10−10

×

so that the more complex model has a higher maximum likelihood. However, as we can see in ﬁg(12.7), the
fraction of the parameter space in which the complex model ﬁts the data well is relatively small. Using a
ﬂat prior for the parameter spaces of both models, we obtain

p(

Y|X

, Msimple) = 1.36

10−11,

×

p(

Y|X

, Mcomplex) = 1.87e

12

×

−

10−11

Whilst the more complex model has a higher maximum likelihood value by a factor 2, it is roughly 7 times
less likely to be the correct model, compared to the simpler model.

12.4 A continuous example : curve ﬁtting

Consider an additive set of periodic functions

y0(x)

≡

w0 + w1 cos(x) + w2 cos(2x) + . . . + wK cos(Kx)

This can be conveniently written in vector form

y0(x)

≡

wTφ(x)

DRAFT April 24, 2015

(12.4.1)

(12.4.2)

277

ab−15−10−50510150.40.420.440.460.480.50.520.540.560.580.6ba00.40.420.440.460.480.50.520.540.560.580.6where φ(x) is a K + 1 dimensional vector with elements

Approximating the Model Likelihood

φ(x)

(1, cos(x), cos(2x), . . . , cos(Kx))T

≡

and the vector w contains the weights of the additive function. We are given a set of data
drawn from this distribution, where yn is the clean y0(xn) corrupted with additive zero mean Gaussian noise
with variance σ2,

=

D

yn = y0(xn) + (cid:15)n,

(cid:15)n

(cid:0)(cid:15)n 0, σ2(cid:1)

∼ N

(12.4.4)

see ﬁg(12.8) and ﬁg(12.9). Assuming i.i.d. data, we are interested in the posterior probability of the number
of coeﬃcients, given the observed data:
p(K) (cid:81)
p(

x1, . . . , xN , K)
|

K)p(K)
)
p(

n p(xn)
)

p(y1, . . . , yN

(12.4.5)

p(K

) =

D|

|D

p(

=

We will assume that a priori we have no preference for the number of frequency components in the model,
p(K) = const. The likelihood term above is given by the integral

D

D

(12.4.3)
(xn, yn), n = 1, . . . , N
{

}

p(y1, . . . , yN

x1, . . . , xN , K) =
|

(cid:90)

w

p(w

N
(cid:89)

n=1

K)
|

p(yn

xn, w, K)
|

(12.4.6)

For p(w
the integral, (see section(8.4) and exercise(12.3))

K) =
|

N

(w 0, IK/α), the integrand is a Gaussian in w for which it is straightforward to evaluate

2 log p(y1, . . . , yN

x1, . . . , xN , K) =
|

N log (cid:0)2πσ2(cid:1)

−

N
(cid:88)

−

n=1

(yn)2
σ2 +bTA−1b

−

log det (A) + K log (α) (12.4.7)

@@
@@

where

A

≡

αI +

1
σ2

N
(cid:88)

n=1

φ(xn)φT(xn),

b

≡

1
σ2

N
(cid:88)

n=1

ynφ(xn)

(12.4.8)

Assuming α = 1 and σ = 0.5, we sampled some data from a model with K = 5 components, ﬁg(12.9a).
Given then this data, and assuming that we know the correct noise level σ and prior precision α = 1, the
task is to infer the number of components K that were used to generate this data. The posterior p(K
)
|D
plotted in ﬁg(12.9b) is sharply peaked at K = 5, which is the value used to generate the data. The clean
posterior mean reconstructions

for K = 5 are plotted in ﬁg(12.9c).

yn
0 |D(cid:105)

(cid:104)

12.5 Approximating the Model Likelihood

For a model with continuous parameter vector θ, dim (θ) = K and data

(cid:90)

p(

M ) =

p(

θ, M )p(θ

D|

D|
For a generic expression

M )dθ
|

p(

θ, M )p(θ

M ) = exp (

f (θ))

D|

|

−

, the model likelihood is

D

(12.5.1)

(12.5.2)

unless f is of a particularly simple form (quadratic in θ for example), for large K, the integral in (12.5.1)
is high dimensional and cannot be exactly evaluated. In order to implement Bayesian model comparison
methods in practice, therefore, typically we need to use some form of approximation for the model likelihood.

K

w

xn

yn
0

yn

N

for regression under the i.i.d.

Figure 12.8: Belief Network representation of a Hierarchical Bayesian
Model
data assumption. Note that
the intermediate nodes on yn
0 are included to highlight the role of the
w, x) =
‘clean’ underlying model.
Since p(y
p(y
|
(cid:0)y wTx, σ2(cid:1), we can if desired do away
(cid:0)y y0, σ2(cid:1) δ (cid:0)y0
(cid:82)
y0 N
with the intermediate node y0 and place directly arrows from w and xn to
yn.

w, x) = (cid:82)
y0

y0)p(y0
|

wTx(cid:1) =

N

−

|

278

DRAFT April 24, 2015

Approximating the Model Likelihood

(a)

(b)

(c)

Figure 12.9:
model.
the mean posterior vector of the optimal dimensional model p(w
is the reconstruction. Plotted in dots is the true underlying clean data.

(a): The data generated with additive Gaussian noise σ = 0.5 from a K = 5 component
T φ(x) where
(c): The reconstruction of the data using
is
(cid:105)
, K = 5). Plotted in the continuous line

(b): The posterior p(K

|D

|D

w

w

).

(cid:105)

(cid:104)

(cid:104)

12.5.1 Laplace’s method

A simple approximation of (12.5.1) is given by Laplace’s method, section(28.2), which ﬁnds the optimum
θ∗ of the posterior and then ﬁts a Gaussian at this point based on the local curvature, giving

log p(

M )

D|

≈

log p(

θ∗, M ) + log p(θ∗

D|

M ) +
|

1
2

log det (cid:0)2πH−1(cid:1)

where θ∗ is the MAP solution

θ∗ = argmax

θ

p(

θ, M )p(θ

D|

M )
|

and H is the Hessian of f (θ)

log p(

≡ −

θ, M )p(θ

D|

|

M ) evaluated at θ∗.

For data

D

= (cid:8)x1, . . . , xN (cid:9) that is i.i.d. generated the above specialises to

p(

M ) =

D|

(cid:90)

p(θ

M )
|

N
(cid:89)

n=1

p(xn

θ, M )dθ
|

In this case Laplace’s method computes the optimum of the function

f (θ) = log p(θ

−

M ) +
|

N
(cid:88)

n=1

log p(xn

θ, M )
|

(12.5.3)

(12.5.4)

(12.5.5)

(12.5.6)

A partial justiﬁcation for Laplace’s approximation is that as the number of datapoints N increases, the
posterior typically becomes increasingly peaked around a single most likely explanation for the data. This
means that a narrow width Gaussian will tend to be a good approximation in the large data limit. Quantify-
ing how well an approximation works though is typically rather diﬃcult. The Laplace method’s popularity
stems from it simplicity although clearly there
will be cases where the posterior is known to be strongly
non-Gaussian, in which case Laplace’s method should be used with some caution.

@@

12.5.2 Bayes information criterion (BIC)

The Bayes information criterion is a simpler version of Laplace’s method that replaces the exact Hessian
with a crude approximation. For i.i.d. data the Hessian scales with the number of training examples, N ,
and a somewhat severe approximation is to set H
N IK where K = dim (θ). Continuing with the notation
from Laplace’s method, this gives the approximation

≈

log p(

M )

D|

≈

log p(

θ∗, M ) + log p(θ∗

D|

DRAFT April 24, 2015

M ) +
|

K
2

log 2π

K
2

−

log N

(12.5.7)

279

−10−50510−3−2−101231234567891000.20.40.60.81−10−50510−3−2−10123For a simple prior that penalises the length of the parameter vector, p(θ
to

M ) =
|

N

(θ 0, I), the above reduces

Bayesian Hypothesis Testing for Outcome Analysis

log p(

M )

log p(

θ∗, M )

(θ∗)T θ∗

1
2

K
2

log N

−
The Bayes Information Criterion[261] approximates (12.5.8) by ignoring the penalty term, giving

D|

D|

−

≈

BIC = log p(

θ∗, M )

D|

K
2

−

log N

(12.5.8)

(12.5.9)

Typically the BIC criterion is used when no speciﬁc prior is speciﬁed on θ, in which case θ∗ is given by the
maximum likelihood setting. The BIC criterion may be used as an approximate way to compare models,
where the term
In general, the Laplace approximation, equation
(12.5.3), is to be preferred to the BIC criterion since it more correctly accounts for the uncertainty in the
posterior parameter estimate. Other techniques that aim to improve on the Laplace method are discussed
in section(28.3) and section(28.8).

K
2 log N penalises model complexity.

−

12.6 Bayesian Hypothesis Testing for Outcome Analysis

In outcome analysis we wish to analyse the results of some experimental data. We assume however, that we
make no detailed model of the data generating mechanism, asking rather generic questions such as whether
the results support some basic hypothesis such as whether two classiﬁers are performing diﬀerently. The
techniques we discuss are quite general, but we phrase them in terms of classiﬁer analysis for concreteness.
The central question we consider here therefore is how to assess whether two classiﬁers are performing dif-
ferently. For techniques which are based on Bayesian classiﬁers there will always be, in principle, a direct
way to estimate the suitability of the model M by computing p(M
, M ). We
consider here the less fortunate situation where the only information presumed available is the test perfor-
mance of the two classiﬁers.

p(M ) (cid:82)

θ, M )p(θ

θ p(

D|

|D

|D

∝

)

To outline the basic issue, we consider two classiﬁers A and B which predict the class of 55 test examples.
Classiﬁer A makes 20 errors, and 35 correct classiﬁcations, whereas classiﬁer B makes 23 errors and 32 correct
classiﬁcations. Is classiﬁer A better than classiﬁer B? Our lack of conﬁdence in pronouncing that A is better
than B results from the small number of test examples. On the other hand if classiﬁer A makes 200 errors
and 350 correct classiﬁcations, whilst classiﬁer B makes 230 errors and 320 correct classiﬁcations, intuitively,
we would be more conﬁdent that classiﬁer A is better than classiﬁer B. Perhaps the most practically relevant
question from a machine learning perspective is the probability that classiﬁer A outperforms classiﬁer B,
given the available test information. Whilst this question can be addressed using a Bayesian procedure,
section(12.6.5), we ﬁrst focus on a simpler question, namely whether classiﬁer A and B are the same[15].

12.6.1 Outcome analysis

Consider a situation where two classiﬁers A and B have been tested on some data, so that we have, for each
example in the test set, an outcome pair

(oa(n), ob(n)) , n = 1, . . . , N

(12.6.1)

(and similarly for ob). That is, there are Q
where N is the number of test data points, and oa
∈ {
possible types of outcomes that can occur. For example, for binary classiﬁcation we will typically have the
four cases

1, . . . , Q

}

dom(o) =

{

TruePositive, FalsePositive, TrueNegative, FalseNegative

}

(12.6.2)

If the classiﬁer predicts class c

true, false
}

∈ {

and the truth is class t

true, false
}

∈ {

these are deﬁned as

TruePositive
FalsePositive
TrueNegative
FalseNegative

c = true
c = true
c = false
c = false

t = true
t = false
t = false
t = true

We call oa =
for classiﬁer B. To be speciﬁc we have two hypotheses we wish to test:

, the outcomes for classiﬁer A, and similarly ob =

oa(n), n = 1, . . . , N

}

{

(12.6.3)

ob(n), n = 1, . . . , N
{

}

280

DRAFT April 24, 2015

Bayesian Hypothesis Testing for Outcome Analysis

α

oa

β

α

ob

oa

ob

(a)

(b)

P

oa, ob

(c)

(a): Hindep : Corresponds to the out-
Figure 12.10:
comes for the two classiﬁers being independently gen-
(b): Hsame: both outcomes are generated
erated.
(c): Hdep : the out-
from the same distribution.
comes are dependent.

1. Hindep : oa and ob are from diﬀerent categorical distributions.

2. Hsame : oa and ob are from the same categorical distribution.

q, with unknown parameters γc. Hypothesis
γ, H) = γc
In both cases we will use categorical models p(oc = q
|
2 will correspond to using the same parameters γa = γb for both classiﬁers, and hypothesis 1 to using
diﬀerent parameters, as we will discuss below. In the Bayesian framework we want to ﬁnd how likely it is
that a model/hypothesis is responsible for generating the data. For any hypothesis H this is given by

p(H

oa, ob) =
|

p(oa, ob

H)p(H)
|
p(oa, ob)

(12.6.4)

where p(H) is the prior belief that H is the correct hypothesis. Note that the normalising constant p(oa, ob)
does not depend on the hypothesis. For all hypotheses we make the independence of trials assumption

p(oa, ob

H) =
|

N
(cid:89)

n=1

p(oa(n), ob(n)
|

H).

To make further progress we need to clarify the meaning of the hypotheses.

12.6.2 Hindep : model likelihood

From Bayes’ rule we may write the posterior hypothesis probability as

p(Hindep

oa, ob) =
|

p(oa, ob, Hindep)
p(oa, ob)

=

p(oa, ob

Hindep)p(Hindep)
|
p(oa, ob)

(12.6.5)

(12.6.6)

α, Hindep), and
The outcome model for classiﬁer A is speciﬁed using continuous parameters, α, giving p(oa
similarly we use β for classiﬁer B. The ﬁnite amount of data means that we are uncertain as to these
parameter values and therefore the joint term in the numerator above is

|

p(oa, ob, Hindep) =

(cid:90)

= p(Hindep)

where we assumed

α, β, Hindep)p(α, β
p(oa, ob
|
(cid:90)

|

Hindep)p(Hindep)dαdβ
(cid:90)

p(oa

α, Hindep)p(α
|

Hindep)dα
|

p(ob

β, Hindep)p(β

|

(12.6.7)

Hindep)dβ
|

(12.6.8)

p(α, β

Hindep) = p(α
|

Hindep)p(β
|

Hindep) and p(oa, ob
|

α, β, Hindep) = p(oa
|

α, Hindep)p(ob
|

|

β, Hindep)

(12.6.9)

See ﬁg(12.10a) for a depiction of these independence assumptions. Note that one might expect there to be
a speciﬁc constraint that the two models A and B are diﬀerent. However since the models are assumed
independent and each has parameters sampled from an eﬀectively inﬁnite set (α and β are continuous), the
a priori probability that values of randomly sampled α and β are the same is zero, and we may consider
Hindep as equivalent to Hdiﬀerent.

Since we are dealing with categorical distributions, it is convenient to use the Dirichlet prior, which is
conjugate to the categorical distribution:

p(α

Hindep) =
|

1
Z(u)

(cid:89)

uq−1
α
q

,

q

Z(u) =

DRAFT April 24, 2015

(cid:81)Q
q=1 Γ(uq)
(cid:16)(cid:80)Q
(cid:17)

q=1 uq

Γ

(12.6.10)

281

Bayesian Hypothesis Testing for Outcome Analysis

The prior hyperparameter u controls how strongly the mass of the distribution is pushed to the corners of
the simplex, see ﬁg(8.6). Setting uq = 1 for all q corresponds to a uniform prior. The likelihood of observing
oa is given by
(cid:90)

p(oa

α, Hindep)p(α
|

Hindep)dα =
|

(cid:90) (cid:89)

(cid:93)a
q
α
q

1
Z(u)

(cid:89)

q

q

uq−1
q

α

dα =

Z(u + (cid:93)a)
Z(u)

(12.6.11)

where (cid:93)a is a vector with components (cid:93)a
Hence

q being the number of times that variable a is in state q in the data.

p(oa, ob, Hindep) = p(Hindep)

Z(u + (cid:93)a)
Z(u)

Z(u + (cid:93)b)
Z(u)

where Z(u) is given by equation (12.6.10).

12.6.3 Hsame : model likelihood

(12.6.12)

In Hsame, the hypothesis is that the outcomes for the two classiﬁers are generated from the same categorical
distribution, see ﬁg(12.10b). Hence
(cid:90)

p(oa, ob, Hsame) = p(Hsame)

α, Hsame)p(α
|

|

Hsame)dα

p(oa

α, Hsame)p(ob
|
Z(u + (cid:93)a + (cid:93)b)
Z(u)

= p(Hsame)

Bayes’ factor

If we assume no prior preference for either hypothesis, p(Hindep) = p(Hsame), then

p(Hindep
p(Hsame

oa, ob)
|
oa, ob)
|

=

Z(u + (cid:93)a)Z(u + (cid:93)b)
Z(u)Z(u + (cid:93)a + (cid:93)b)

The higher this ratio is, the more we believe that the data were generated by two diﬀerent categorical
distributions.

Example 12.3. Two people classify the expression of each image into happy, sad or normal, using states
1, 2, 3 respectively. Each column of the data below represents an image classiﬁed by the two people (person
1 is the top row and person 2 the second row). Are the two people essentially in agreement?

1
1

3
3

1
1

3
2

1
2

1
3

3
3

2
3

2
2

3
3

1
3

1
2

1
2

1
2

1
2

1
1

1
2

1
1

1
3

2
2

To help answer this question, we perform a Hindep versus Hsame test. From this data, the count vector for
person 1 is [13, 3, 4] and for person 2, [4, 9, 7]. Based on a ﬂat prior for the categorical distribution and
assuming no prior preference for either hypothesis, we have the posterior Bayes’ factor

p(persons 1 and 2 classify diﬀerently)
p(persons 1 and 2 classify the same)

=

Z([14, 4, 5])Z([5, 10, 8])
Z([1, 1, 1])Z([18, 13, 12])

= 12.87

(12.6.16)

where the Z function is given in equation (12.6.10). This is strong evidence the two people are classifying
the images diﬀerently.

Below we discuss some further examples for the Hindep versus Hsame test. As above, the only quantities we
need for this test are the vector counts from the data. We assume that there are three kinds of outcomes,
, and we want to test if two classiﬁers are essentially producing
Q = 3, for example dom(o) =
}
the same outcome distributions, or diﬀerent. Throughout we assume a ﬂat prior on the table entries, u = 1.

good, bad, ugly
{

282

DRAFT April 24, 2015

(12.6.13)

(12.6.14)

(12.6.15)

Bayesian Hypothesis Testing for Outcome Analysis

Example 12.4 (Hindep versus Hsame).

•

•

•

We have the two outcome counts (cid:93)a = [39, 26, 35] and (cid:93)b = [63, 12, 25]. The posterior Bayes’ factor
equation (12.6.15) is 20.7 – strong evidence in favour of the two classiﬁers being diﬀerent.
Alternatively, consider the two outcome counts (cid:93)a = [52, 20, 28] and (cid:93)b = [44, 14, 42]. Then, the
posterior Bayes’ factor equation (12.6.15) is 0.38 – weak evidence against the two classiﬁers being
diﬀerent.
As a ﬁnal example, consider counts (cid:93)a = [459, 191, 350] and (cid:93)b = [465, 206, 329]. This gives a posterior
Bayes’ factor equation (12.6.15) of 0.008 – strong evidence that the two classiﬁers are statistically the
same.

In all cases the results are consistent with the actual model in fact used to generate the count data.

12.6.4 Dependent outcome analysis

Here we consider the case that outcomes are dependent. For example, it may be the case that when classiﬁer
A works well, classiﬁer B will also work well. Our interest is to evaluate the hypothesis:

Hdep : the outcomes that the two classiﬁers make are dependent

(12.6.17)

To do so we assume a categorical distribution over the joint states, see ﬁg(12.10c):

p(oa(n), ob(n)

P, Hdep)
|

Here P is a Q

×

Q matrix of probabilities:

[P ]ij = p(oa = i, ob = j)

(12.6.18)

(12.6.19)

so [P ]ij is the probability that A makes outcome i and B makes outcome j. Then,

(cid:90)

p(o

Hdep) =
|

p(o, P

Hdep)dP =
|

(cid:90)

p(o

P, Hdep)p(P
|

Hdep)dP
|

where, for convenience, we write o = (oa, ob). Assuming a Dirichlet prior on P, with hyperparameters U,
we have

p(o, Hdep) = p(Hdep)

Z(vec (U + (cid:93)))
Z(vec(U))

(12.6.20)

where vec(D) is a vector formed from concatenating the rows of the matrix D. Here (cid:93) is the count matrix,
with [(cid:93)]ij equal to the number of times that joint outcome (oa = i, ob = j) occurred in the N datapoints.
We assume the uniform prior, [U ]ij = 1,

i, j.

∀

Testing for dependencies in the outcomes: Hdep versus Hindep

To test whether or not the outcomes of the classiﬁers are dependent Hdep against the hypothesis that they
are independent Hindep we may use, assuming p(Hindep) = p(Hdep),

p(Hindep
p(Hdep

|

o)
|
o)

=

Z(u + (cid:93)a)
Z(u)

Z(u + (cid:93)b)
Z(u)

Z(vec(U))
Z(vec (U + (cid:93)))

Example 12.5 (Hdep versus Hindep).

DRAFT April 24, 2015

(12.6.21)

283

Bayesian Hypothesis Testing for Outcome Analysis

Consider the outcome count matrix (cid:93)





98
93
7
168 13 163
245 12 201





so that (cid:93)a = [511, 32, 457], and (cid:93)b = [198, 344, 458]. Then

p(Hindep
p(Hdep

o)
|
o)
|

= 3020

- strong evidence that the classiﬁers perform independently.

Consider the outcome count matrix (cid:93)

•

•





120 83
82
107 162
4
170 203 70





so that (cid:93)a = [359, 485, 156], and (cid:93)b = [284, 273, 443]. Then

p(Hindep
p(Hdep

o)
|
o)
|

10−18

= 2

×

- strong evidence that the classiﬁers perform dependently.

These results are in fact consistent with the way the data was generated in each case.

12.6.5 Is classiﬁer A better than B?

(12.6.22)

(12.6.23)

(12.6.24)

(12.6.25)

We return to the question with which we began this outcome analysis. Given the common scenario of observ-
ing a number of (binary) errors for classiﬁer A on a test set and a number for B, can we say which classiﬁer
.
is better? This corresponds to the special case of binary classes Q = 2 with dom(e) =
}
Then for θa being the probability that classiﬁer A generates a correct label, and similarly for θb, one way to
judge this is to compare the hypotheses:

correct, incorrect

{

1. Ha>b : oa and ob are from diﬀerent binomial distributions with corresponding probabilities θa, θb and

θa > θb.

2. Hsame : oa and ob are from the same binomial distribution.

Writing

=

oA, oB
{

D

, and assuming independent parameter priors, we have
}

(cid:90)

p(

Ha>b) =

D|

θa>θb

p(oA

θa)p(oB
|

|

θb)p(θa)p(θb)

Using

p(oA

θa) = θ
|

(cid:93)a
correct
a

(1

−

θa)(cid:93)a

incorrect ,

p(oB

|

θb) = θ

(cid:93)b
correct
b

(1

θb)(cid:93)b

incorrect

−

and Beta distribution priors

p(θa) = B (θa

u1, u2) ,
|

p(θb) = B (θb

u1, u2)
|

then one may readily show, using the Beta function B(x, y):

(12.6.26)

(12.6.27)

(12.6.28)

p(

Ha>b) =

D|

284

B(u1 + (cid:93)a

correct, u2 + (cid:93)a

correct, u2 + (cid:93)b

incorrect)

incorrect)B(u1 + (cid:93)b
(B(u1, u2))2

(cid:90)

p(θa > θb

|D

θa>θb

, Ha>b) (12.6.29)

DRAFT April 24, 2015

Bayesian Hypothesis Testing for Outcome Analysis

where

p(θa > θb

|D

, Ha>b) =

B (θa

θa>θb

(cid:90)

u1 + (cid:93)a
|

correct, u2 + (cid:93)a

incorrect) B

(cid:16)

θb

u1 + (cid:93)b
|

correct, u2 + (cid:93)b

incorrect

(cid:17)

(12.6.30)

The integral above needs to be computed numerically – see betaXbiggerY.m. For the Hsame hypothesis,
using θ = θa = θb:

p(

Hsame) =

D|

=

(cid:90)

θ

1
B(u1, u2)
B(u1 + (cid:93)a

correct + (cid:93)b

θu1+(cid:93)a

correct+(cid:93)b

correct(1

θ)u2+(cid:93)a

incorrect+(cid:93)b

incorrect

−
incorrect + (cid:93)b
correct, u2 + (cid:93)a
B(u1, u2)

incorrect)

(12.6.31)

(12.6.32)

The question of whether A is better than B can then be addressed by computing

Ha>b)
Hsame)

p(
p(

D|
D|

=

correct, u2 + (cid:93)a

B(u1 + (cid:93)a
B(u1, u2)B(u1 + (cid:93)a

incorrect)B(u1 + (cid:93)b

correct + (cid:93)b

correct, u2 + (cid:93)a

correct, u2 + (cid:93)b
incorrect + (cid:93)b

incorrect)
incorrect)

p(θa > θb

|D

, Ha>b)

(12.6.33)

Examining the quotient term, we see that this is related to the Hindep hypothesis via

Ha>b)
Hsame)

p(
p(

D|
D|

=

Hindep)
Hsame)

p(
p(

D|
D|

p(θa > θb

, Ha>b)

|D

(12.6.34)

where the second term decreases the Hindep versus Hsame Bayes’ factor. This is intuitive since Ha>b places
more constraints on the parameter space than Hindep.

Example 12.6. Classiﬁer A makes 20 errors, and 35 correct classiﬁcations, whereas classiﬁer B makes 27
errors and 28 correct classiﬁcations. Using a ﬂat prior u1 = u2 = 1 this gives

p(θa > θb

oA, oB, Hindep) = betaXbiggerY(1+35,1+20,1+28,1+27)= 0.909
|

The Bayes’ factor is then

Ha>b)
Hsame)

p(
p(

D|
D|

=

B(1 + 35, 1 + 20)B(1 + 28, 1 + 27)
B(1, 1)B(1 + 35 + 28, 1 + 20 + 27)

0.909 = 0.516

(12.6.35)

(12.6.36)

On the other hand if classiﬁer A makes 200 errors and 350 correct classiﬁcations, whilst classiﬁer B makes

270 errors and 280 correct classiﬁcations, we have

p(θa > θb

oA, oB, Hsame) =betaXbiggerY(1+350,1+200,1+280,1+270)= 1.0
|

and

Ha>b)
Hsame)

p(
p(

D|
D|

=

B(1 + 350, 1 + 200)B(1 + 280, 1 + 270)
B(1, 1)B(1 + 350 + 280, 1 + 200 + 270)

1.0 = 676

(12.6.37)

(12.6.38)

This demonstrates the intuitive eﬀect that even though the proportion of correct/incorrect classiﬁcations
doesn’t change for the two scenarios, our conﬁdence in determining which is the better classiﬁer increases
with the amount of data. See also ﬁg(12.11).

12.7 Summary

•

•

Bayes’ rule enables us to evaluate models based on how well they ﬁt the data via the model likelihood.

There is no need to explicitly penalise ‘complex’ models in the Bayesian approach since it automatically
incorporates an Occam’s razor eﬀect due to the integral over the posterior parameter distribution.

DRAFT April 24, 2015

285

Exercises

(a)

(b)

Figure 12.11: Two classiﬁers A and B and their posterior distributions of the probability that they
(a): For A with 35 correct and 20 incorrect labels,
classify correctly (using a uniform Beta prior).
1 + 28, 1 + 27) (dashed curve). Also
B (x
1 + 35, 1 + 20) (solid curve); B with 28 correct 27 incorrect B (y
|
|
plotted is the posterior assuming that both classiﬁers are the same, B (x
1 + 35 + 28, 1 + 20 + 27), (dot-
ted curve). Whilst the posterior overlap of the two classiﬁers A and B is small, they both overlap with
the posterior for the assumption that the classiﬁers are the same. For this reason, there is no signiﬁ-
(b): For A with 350 correct and 200 incorrect labels (solid
cant evidence that A is better than B.
curve), B (x
1 + 280, 1 + 270) (dashed curve). As
|
the amount of data increases the overlap between the distributions decreases and the certainty that one
classiﬁer is better than the other correspondingly increases.

1 + 350, 1 + 200); B with 280 correct 270 incorrect B (y
|

|

•

•

Computing the model likelihood can be a complex task. In continuous parameter models, Laplace’s method
provides a simple approximation, the BIC being a cruder version of Laplace’s approximation.

Assessing performance on the basis of a limited amount of data can be achieved using simple Bayesian
hypothesis testing.

12.8 Code

demoBayesErrorAnalysis.m: Demo for Bayesian error analysis
betaXbiggerY.m: p(x > y) for x

a, b), y

B (x

B (y

c, d)

∼

|

∼

|

12.9 Exercises

Exercise 12.1. Write a program to implement the fair/biased coin tossing model selection example of
section(12.2.1) using a discrete domain for θ. Explain how to overcome potential numerical issues in dealing
with large NH and NT (of the order of 1000).

Exercise 12.2. You work at Dodder’s hedge fund and the manager wants to model next day ‘returns’ yt+1
based on current day information xt. The vector of ‘factors’ each day, xt captures essential aspects of the
market. He argues that a simple linear model

yt+1 =

K
(cid:88)

k=1

wkxkt

(12.9.1)

should be reasonable and asks you to ﬁnd the weight vector w, based on historical information
. In addition he also gives you a measure of the ‘volatility’ σ2

=

(xt, yt+1), t = 1, . . . , T
{

1
}

−

D

t for each day.

286

DRAFT April 24, 2015

00.10.20.30.40.50.60.70.80.91012345678900.10.20.30.40.50.60.70.80.91051015202530Exercises

1. Under the assumption that the returns are i.i.d. Gaussian distributed

p(y1:T

x1:T , w) =
|

T
(cid:89)

t=2

xt−1, w) =

p(yt

|

(cid:16)

T
(cid:89)

t=2

N

yt wTxt−1, σ2
t

(cid:17)

explain how to set the weight vector w by maximum likelihood.

(12.9.2)

2. Your hedge fund manager is however convinced that some of the factors are useless for prediction and
wishes to remove as many as possible. To do this you decide to use a Bayesian model selection method
in which you use a prior

N

p(w

(w 0, I)

M ) =
|
where M = 1, . . . , 2K
1 indexes the model. Each model uses only a subset of the factors. By
translating the integer M into a binary vector representation, the model describes which factors are to
be used. For example if K = 3, there would be 7 models

(12.9.3)

−

,

,

,

{

}

{

1, 0, 0

0, 1, 1
{

0, 1, 0
}

0, 0, 1
{

1, 1, 1
{
}
(w3 0, 1). Similarly model 7 would be
where the ﬁrst model is yt = w3x3 with weight prior p(w3) =
yt = w1x1 + w2x2 + w3x3 with p(w1, w2, w3) =
((w1, w2, w3) (0, 0, 0), I3). You decide to use a ﬂat
prior p(M ) = const. Draw the hierarchical Bayesian network for this model and explain how to ﬁnd
the best model for the data using Bayesian model selection by suitably adapting equation (12.4.7).

1, 0, 1
}

(12.9.4)

1, 1, 0

N

N

}

}

{

{

}

,

,

,

3. Using the data dodder.mat, perform Bayesian model selection as above for K = 6 and ﬁnd which of

the factors x1, . . . , x6 are most likely to explain the data.

Exercise 12.3. Here we will derive the expression (12.4.7) and also an alternative form.

@@

@@

1. Starting from

p(w)

N
(cid:89)

n=1

p(yn

w, xn, K) =
|

N

(w 0, I/α)

(cid:16)

yn wTφ(xn), σ2(cid:17)

(cid:89)
n N
2 wTw

=

1

(2πα−1)K/2 e− α

1

(2πσ2)N/2 e− 1

2σ2

(cid:80)

n(yn−wTφ(xn))2

Show that this can be expressed as

1
(2πα−1)K/2

where

1

(2πσ2)N/2 e− 1

2σ2

(cid:80)

n(yn)2

e− 1

2 wTAw+bTw

A = αI +

1
σ2

(cid:88)

n

φ(xn)φT(xn)

b =

1
σ2

(cid:88)

n

ynφ(xn)

2. By completing the square (see section(8.4.1)), derive (12.4.7).

(12.9.5)

(12.9.6)

(12.9.7)

(12.9.8)

3. Since each yn, n = 1, . . . , N is linearly related through w and w is Gaussian distributed, the joint vector
y1, . . . , yN is Gaussian distributed. Using Gaussian propagation, result(8.3), derive an alternative
expression for log p(y1, . . . , yN

x1, . . . , xN ).
|

Exercise 12.4. Similar to example(12.3), three people classify images into 1 of three categories. Each col-
umn in the table below represents the classiﬁcations of each image, with the top row being the class from per-
son 1, the middle from person 2 and the bottom from person 3. You wish to estimate p(class of image
person =
|
i), assuming that for person i each image class is drawn independently from this distribution.

1
1
1

3
3
2

1
1
1

3
2
1

1
2
1

1
3
3

3
3
2

2
3
2

2
2
2

3
3
3

1
3
1

1
2
2

1
2
1

1
2
2

1
2
1

1
1
1

1
2
2

1
1
3

1
3
3

2
2
2

DRAFT April 24, 2015

287

Assuming no prior preference amongst hypotheses and a uniform prior on counts, compute

p(persons 1, 2 and 3 classify diﬀerently)
p(persons 1, 2 and 3 classify the same)

Exercises

(12.9.9)

Exercise 12.5. Consider a classiﬁer that makes R correct classiﬁcations and W wrong classiﬁcations. Is
the classiﬁer better than random guessing? Let
represent the fact that there are R right and W wrong
answers. Assume also that the classiﬁcations are i.i.d.

D

1. Show that under the hypothesis that the data is generated purely at random, the likelihood is

p(

random) = 0.5R+W

D|H

2. Deﬁne θ to be the probability that the classiﬁer makes an error. Then

p(

θ) = θR (1

D|

θ)W

−

Now consider

p(

non random) =

D|H

(cid:90)

θ

p(

θ)p(θ)

D|

Show that for a Beta prior, p(θ) = B (θ

a, b)
|

p(

non random) =

D|H

B(R + a, W + b)
B(a, b)

where B(a, b) is the Beta-function.

3. Considering the random and non-random hypotheses as a priori equally likely, show that

p(

H

random

) =

|D

0.5R+W
0.5R+W + B(R+a,W +b)

B(a,b)

(12.9.10)

(12.9.11)

(12.9.12)

(12.9.13)

(12.9.14)

4. For a ﬂat prior a = b = 1 compute the probability that for 10 correct and 12 incorrect classiﬁcations,
the data is from a purely random distribution (according to equation (12.9.14)). Repeat this for 100
correct and 120 incorrect classiﬁcations.

5. Show that the standard deviation in the number of errors of a random classiﬁer is 0.5√R + W and

relate this to the above computation.

Exercise 12.6. Our interest here is to discuss a method to learn the direction of an edge in a belief network.
Consider a distribution

p(x, y

θ, My→x) = p(x
|

|

y, θx|y)p(y

θy)
|

(12.9.15)

where θ are the parameters of the conditional probability tables. For a prior p(θ) = p(θx|y)p(θy) and i.i.d.
data

=

xn, yn, n = 1, . . . , N
{

D

, the likelihood of the data is
}

(cid:90)

p(

My→x) =

D|

θ

p(θx|y)p(θy)

(cid:89)

p(xn

n

|

yn, θx|y)p(yn

θy)
|

For binary variables, x

0, 1
}

∈ {

, y

0, 1
}

∈ {

,

θy) = θy,
p(y = 1
|
and Beta distribution priors

y, θx|y) = θ1|y
p(x = 1
|

(12.9.16)

(12.9.17)

p(θy) = B (θy

α, β) ,

|

p(θ1|y) = B (cid:0)θ1|y|

α1|y, β1|y

(cid:1) ,

p(θ1|1, θ1|0) = p(θ1|1)p(θ1|0)

(12.9.18)

288

DRAFT April 24, 2015

Exercises

show that p(

My→x) is given by

D|

B((cid:93) (x = 1, y = 0) + α1|0, (cid:93) (x = 0, y = 0) + β1|0)
B(α1|0, β1|0)

B((cid:93) (x = 1, y = 1) + α1|1, (cid:93) (x = 0, y = 1) + β1|1)
B(α1|1, β1|1)

(12.9.19)

Now derive a similar expression for the model with the edge direction reversed, p(
same values for all hyperparameters. Using this, derive a simple expression for the Bayes’ factor

D|

Mx→y), assuming the

p(
p(

My→x)
Mx→y)

D|
D|

and relate this expression to the complexity of the tables.

(12.9.20)

DRAFT April 24, 2015

289

Exercises

290

DRAFT April 24, 2015

Part III

Machine Learning

291

Introduction to Part III

Machine Learning is fundamentally about extracting value from large datasets. Often
the motivation is ultimately to produce an algorithm that can either mimic or enhance
human/biological performance.

In part III we begin by discussing some of the basic concepts in Machine Learning,
namely supervised and unsupervised learning. We then move on to discuss some
standard models in Machine Learning and related areas.

DRAFT April 24, 2015

293

Machine learning is a large ﬁeld and the diagram denotes loose associations between subjects.
In part
III we discuss some of the standard machine learning concepts including probabilistic variants of classical
algorithms.

294

DRAFT April 24, 2015

MachineLearningSupervisedLearningReinforce-mentlearningDimensionreductionPartialleastsquaresCanonicalvariatesFisher’slineardisc.ClassiﬁcationDiscriminativeGaussianprocessLogisticregressionSupportvectormachineGenerative(classconditional)NaiveBayesMixturemodelsFactoranalysismixturesNearestneighbourMixtureofexpertsRegressionGaussianProcessParametricModellinearnon-linearTime-seriespredictionSemi-supervisedLearningUnsupervisedLearningTime-seriesmodellingMarkovchainARcontinuousCRFundirectedLatentMarkovchainHMMdiscreteLDScontinuousSLDSmixedDimensionreductionLinearFactoranalysisPCAPLSANMFNonLinearVisualisationLatentvariablemodelsundercompleteovercompletesparsebasisMixedmembershipLatentDirichletAllocationCliquedecomp.AnomalydetectionCHAPTER 13

Machine Learning Concepts

Machine learning is the body of research related to automated large-scale data analysis. Historically, the
ﬁeld was centred around biologically inspired models and the long term goals of much of the community
are oriented to producing models and algorithms that can process information as well as biological systems.
The ﬁeld also encompasses many of the traditional areas of statistics with, however, a strong focus on
mathematical models and also prediction. Machine learning is now central to many areas of interest in
computer science and related large-scale information processing domains.

13.1 Styles of Learning

Broadly speaking the main two subﬁelds of machine learning are supervised learning and unsupervised
learning. In supervised learning the focus is on accurate prediction, whereas in unsupervised learning the
aim is to ﬁnd compact descriptions of the data. In both cases, one is interested in methods that generalise
well to previously unseen data. In this sense, one distinguishes between data that is used to train a model
and data that is used to test the performance of the trained model, see ﬁg(13.1). We discuss ﬁrst the basic
characteristics of some learning frameworks before discussing supervised learning in more detail.

13.1.1 Supervised learning

male, female
}
=
{

Consider a database of face images, each represented by a vector1 x. Along with each image x is an output
that states if the image is of a male or female. A database of 10000 such image-class
class y
∈ {
. The task is to make an accurate predictor y(x∗) of the
(xn, yn) , n = 1, . . . , 10000
pairs is available,
}
sex of a novel image x∗. This is an example application that would be hard to program in a traditional
manner since formally specifying a rule that diﬀerentiates male from female faces is diﬃcult. An alternative
is to give example faces and their gender labels and let a machine automatically ‘learn’ such a rule.

D

Deﬁnition 13.1 (Supervised Learning). Given a set of data
the task is
to learn the relationship between the input x and output y such that, when given a novel input x∗ the
predicted output y∗ is accurate. The pair (x∗, y∗) is not in
but assumed to be generated by the same
unknown process that generated
. To specify explicitly what accuracy means one deﬁnes a loss function
L(ypred, ytrue) or, conversely, a utility function U =

(xn, yn) , n = 1, . . . , N
{

L.

=

D

D

D

}

−

In supervised learning our interest is describing y conditioned on knowing x. From a probabilistic modelling
perspective, we are therefore concerned primarily with the conditional distribution p(y
). The term
‘supervised’ indicates that there is a notional ‘supervisor’ specifying the output y for each input x in the
available data

. The output is also called a ‘label’, particularly when discussing classiﬁcation.

x,
|

D

D

1For an m × n face image with elements Fmn we can form a vector by stacking the entries of the matrix.

295

Train

Test

Styles of Learning

Figure 13.1: In training and evaluating a model, conceptually
there are two sources of data. The parameters of the model are
set on the basis of the train data only. If the test data is generated
from the same underlying process that generated the train data,
an unbiased estimate of the generalisation performance can be
obtained by measuring the test data performance of the trained
model. Importantly, the test performance should not be used to
adjust the model parameters since we would then no longer have
an independent measure of the performance of the model.

Predicting tomorrow’s stock price y(T + 1) based on past observations y(1), . . . , y(T ) is a form of supervised
where time t is the input
learning. We have a collection of times and prices
and the price y(t) is the output.

(t, y(t)) , t = 1, . . . , T

=

D

}

{

Example 13.1. A father decides to teach his young son what a sports car is. Finding it diﬃcult to explain
in words, he decides to give some examples. They stand on a motorway bridge and, as each car passes
underneath, the father cries out ‘that’s a sports car!’ when a sports car passes by. After ten minutes, the
father asks his son if he’s understood what a sports car is. The son says, ‘sure, it’s easy’. An old red VW
Beetle passes by, and the son shouts – ‘that’s a sports car!’. Dejected, the father asks – ‘why do you say
that?’. ‘Because all sports cars are red!’, replies the son.

This is an example scenario for supervised learning. Here the father plays the role of the supervisor, and his
son is the ‘student’ (or ‘learner’). It’s indicative of the kinds of problems encountered in machine learning
in that it is not easy to formally specify what a sports car is – if we knew that, then we wouldn’t need
to go through the process of learning. This example also highlights the issue that there is a diﬀerence
between performing well on training data and performing well on novel test data. The main interest in
supervised learning is to discover an underlying rule that will generalise well, leading to accurate prediction
on new inputs. If there is insuﬃcient train data then, as in this scenario, generalisation performance may
be disappointing.

If the output is one of a discrete number of possible ‘classes’, this is called a classiﬁcation problem.
In
If the output is continuous, this is
classiﬁcation problems we will generally use c for the output.
called a regression problem. For example, based on historical information of demand for sun-cream in your
supermarket, you are asked to predict the demand for the next month.
In some cases it is possible to
discretise a continuous output and then consider a corresponding classiﬁcation problem. However, in other
cases it is impractical or unnatural to do this, for example if the output y is a high dimensional continuous
valued vector.

13.1.2 Unsupervised learning

Deﬁnition 13.2 (Unsupervised learning). Given a set of data
in unsupervised
learning we aim to ﬁnd a plausible compact description of the data. An objective is used to quantify the
accuracy of the description. In unsupervised learning there is no special prediction variable so that, from a
probabilistic perspective, we are interested in modelling the distribution p(x). The likelihood of the model
to generate the data is a popular measure of the accuracy of the description.

xn, n = 1, . . . , N

=

D

}

{

Example 13.2. A supermarket chain wishes to discover how many diﬀerent basic consumer buying
behaviours there are based on a large database of supermarket checkout data. Items brought by a customer

296

DRAFT April 24, 2015

Styles of Learning

on a visit to a checkout are represented by a (very sparse) 10000 dimensional vector x which contains a 1
in the ith element if the customer bought product i and 0 otherwise. Based on 10 million such checkout
= (cid:8)xn, n = 1, . . . , 107(cid:9) the supermarket chain wishes to discover
vectors from stores across the country,
patterns of buying behaviour.

D

In the table each column represents the products bought by a
customer (7 customer records with the ﬁrst 6 of the 10,000 prod-
ucts are shown). A 1 indicates that the customer bought that
item. We wish to ﬁnd common patterns in the data, such as if
someone buys diapers they are also likely to buy aspirin.

coﬀee
tea
milk
beer
diapers
aspirin

1
0
1
0
0
0

0
0
0
0
0
1

0
1
1
0
1
1

1
0
1
1
0
0

0
0
0
1
1
1

0
0
1
0
0
0

0
0
1
1
1
1

· ·
· ·
· ·
· ·
· ·
· ·

Example 13.3 (Clustering).

The table represents a collection of unlabelled two-dimensional points.
By simply eye-balling the data, we can see that there are two apparent
clusters, one centred around (0,5) and the other around (35,45). A
reasonable compact description of the data is that is has two clusters,
one centred at (0,0) and one at (35,45), each with a standard deviation
of 10.

x1
x2

-2
7

-6
22

-1
1

11
1

-1
-8

46
52

33
40

42
33

32
54

45
39

13.1.3 Anomaly detection

A baby processes a mass of initially confusing sensory data. After a while the baby begins to understand
her environment so that sensory data from the same environment is familiar or expected. When a strange
face presents itself, the baby recognises that this is not familiar and becomes upset. The baby has learned a
representation of the environment and can distinguish the expected from the unexpected; this is an example
of unsupervised learning. Detecting anomalous events in industrial processes (plant monitoring), engine
monitoring and unexpected buying behaviour patterns in customers all fall under the area of anomaly
detection. This is also known as ‘novelty’ detection.

13.1.4 Online (sequential) learning

was given beforehand. In online learning data arrives
In the above situations we assumed that the data
sequentially and we continually update our model as new data becomes available. Online learning may occur
in either a supervised or unsupervised context.

D

13.1.5 Interacting with the environment

In certain situations, an agent may be able to interact in some manner with its environment. This interaction
can complicate but also enrich the potential for learning.

Query (Active) Learning Here the agent has the ability to request data from the environment. For
example, a predictor might recognise that it is less conﬁdently able to predict in certain regions of
the space x and therefore requests more training data in this region. Active learning can also be
considered in an unsupervised context in which the agent might request information in regions where
p(x) is currently uninformative.

Reinforcement Learning In reinforcement learning an agent inhabits an environment in which it may
take actions. Some actions may eventually be beneﬁcial (lead to food for example), whilst others may

DRAFT April 24, 2015

297

−100102030405001020304050Supervised Learning

be disastrous (lead to being eaten for example). Based on accumulated experience, the agent needs
to learn which action to take in a given situation in order to maximise the probability of obtaining
a desired long term goal (long term survival, for example). Actions that lead to long term rewards
need to be reinforced. Reinforcement learning has connections with control theory, Markov decision
processes and game theory. Whilst we discussed MDPs and brieﬂy mentioned how an environment
can be learned based on delayed rewards in section(7.9.2), we will not discuss this topic further and
refer the reader to specialised texts, for example [283].

13.1.6 Semi-supervised learning

In machine learning, a common scenario is to have a small amount of labelled and a large amount of
unlabelled data. For example, it may be that we have access to many images of faces; however, only a small
number of them may have been labelled as instances of known faces. In semi-supervised learning, one tries
to use the unlabelled data to make a better classiﬁer than that based on the labelled data alone. This is
a common issue in many examples since often gathering unlabelled data is cheap (taking photographs, for
example). However, typically the labels are assigned by humans, which is expensive.

13.2 Supervised Learning

Supervised and unsupervised learning are mature ﬁelds with a wide range of practical tools and associated
theoretical analyses. Our aim here is to give a brief introduction to the issues and ‘philosophies’ behind the
approaches. We focus here on supervised learning and classiﬁcation in particular.

13.2.1 Utility and Loss

Given a new input x∗, the optimal prediction depends on how costly making an error is. This can be
In forming a decision function c(x∗) that will
quantiﬁed using a loss function (or conversely a utility).
produce a class label for the new input x∗, we don’t know the true class, only our surrogate for this, the
x∗). If U (ctrue, cpred) represents the utility of making a decision cpred when the
predictive distribution p(c
|
truth is ctrue, the expected utility for the decision function is

U (c(x∗)) =

(cid:88)

ctrue

U (ctrue, c(x∗))p(ctrue

x∗)

|

and the optimal decision function c(x∗) is that which maximises the expected utility,

c(x∗) = argmax
c(x∗)

U (c(x∗))

(13.2.1)

(13.2.2)

One may also consider equivalently a loss L(ctrue, c(x)), for which the expected loss with respect to p(c, x)
is then termed the risk . The optimal decision function is then that which minimises the risk with respect
to θ.

Zero-one loss/utility

A ‘count the correct predictions’ measure of prediction performance is based on the zero-one utility (or
conversely the zero-one loss):

U (ctrue, c∗) =

(cid:26) 1 if c∗ = ctrue
= ctrue

0 if c∗

For the two class case, we then have the expected utility equation (13.2.1) given by

U (c(x∗)) =

(cid:26) p(ctrue = 1
p(ctrue = 2

x∗) for c(x∗) = 1
|
x∗) for c(x∗) = 2
|

Hence, in order to have the highest expected utility, the decision function c(x∗) should correspond to selecting
x∗):
the highest class probability p(c
|
(cid:26) 1 if p(c = 1
x∗) > 0.5
|
x∗) > 0.5
2 if p(c = 2
|

c(x∗) =

(13.2.5)

In the case of a tie, either class is selected at random with equal probability.

298

DRAFT April 24, 2015

(13.2.3)

(13.2.4)

(cid:54)
Supervised Learning

General loss/utility functions

In general, for a two-class problem, we have

U (c(x∗)) =

(cid:26) U (ctrue = 1, c∗ = 1)p(ctrue = 1
U (ctrue = 1, c∗ = 2)p(ctrue = 1

x∗) + U (ctrue = 2, c∗ = 1)p(ctrue = 2
|
|
x∗) + U (ctrue = 2, c∗ = 2)p(ctrue = 2
|
|

x∗) for c(x∗) = 1
x∗) for c(x∗) = 2

(13.2.6)

and the optimal decision function c(x∗) chooses that class with highest expected utility. One can readily
generalise this to multiple-class situations using a utility matrix with elements

Uij = U (ctrue = i, cpred = j)

(13.2.7)

where the i, j element of the matrix contains the utility of predicting class j when the true class is i. Con-
Uij. In some applications the utility matrix
versely one could think of a loss-matrix with entries Lij =
is highly non-symmetric. Consider a medical scenario in which we are asked to predict whether or not the
cancer, benign
patient has cancer dom(c) =
. If the true class is cancer yet we predict benign, this could
}
{
have terrible consequences for the patient. On the other hand, if the class is benign yet we predict cancer,
this may be less disastrous for the patient. Such asymmetric utilities can favour conservative decisions –
in the cancer case, we would be more inclined to decide the sample is cancerous than benign, even if the
predictive probability of the two classes is equal.

−

In solving for the optimal decision function c(x∗) in equation (13.2.5) we are assuming that the model p(c, x)
is correct. However, in practice we typically don’t know the correct model underlying the data – all we have
=
is a dataset of examples
and our domain knowledge. We therefore need to
}
) which should ideally be close to the true but unknown joint data distribution
form a distribution p(c, x
ptrue(c, x). Only then can our decisions be expected to generalise well to examples outside of the train
data. Communities of researchers in machine learning form around diﬀerent strategies to address the lack
of knowledge about ptrue(c, x).

(xn, cn) , n = 1, . . . , N

D
|D

{

Squared loss/utilty

In regression problems, for a real-valued prediction ypred and truth ytrue, a common loss function is the
squared loss

L(ytrue, ypred) =

(cid:16)

ytrue

−

ypred(cid:17)2

(13.2.8)

The above decision framework then follows through, replacing summation with integration for the continuous
variables.

13.2.2 Using the empirical distribution

A direct approach to not knowing the correct model ptrue(c, x) is to replace it with the empirical distribution

p(c, x

|D

) =

1
N

N
(cid:88)

n=1

δ (c, cn) δ (x, xn)

(13.2.9)

That is, we assume that the underlying distribution is approximated by placing equal mass on each of the
points (xn, cn) in the dataset. Using this gives the empirical expected utility

U (c, c(x))

(cid:105)p(c,x|D) =

(cid:104)

1
N

(cid:88)

n

U (cn, c(xn))

or conversely the empirical risk

R =

1
N

(cid:88)

n

L(cn, c(xn))

DRAFT April 24, 2015

(13.2.10)

(13.2.11)

299

,

C

X

x∗

p(x, c)

θ

c∗ = c(x∗

θ)
|

L(c, c(x
(cid:104)

θ))
|

(cid:105)p(x,c) + λP (θ)

Supervised Learning

,

C

X

Figure 13.2: Empirical risk approach. Given the
, a model of the data p(x, c) is made,
dataset
usually using the empirical distribution. For a
classiﬁer c(x
θ), the parameter θ is learned by
|
minimising the penalised empirical risk with re-
spect to θ. The penalty parameter λ is set by
validation. A novel input x∗ is then assigned to
class c(x∗

θ), given this optimal θ.
|

Assuming the loss is minimal when the correct class is predicted, the optimal decision c(x) for any input in
then c(x∗) is undeﬁned.
the train set is given by c(xn) = cn. However, for any new x∗ not contained in
In order to deﬁne the class of a novel input, one may use a parametric function c(x
θ). For example for a
|
two class problem dom(c) =

D

, a linear decision function is given by
}

1, 2
{
(cid:26) 1 if θTx + θ0

0
2 if θTx + θ0 < 0

≥

θ) =

c(x

|

(13.2.12)

If the vector input x is on the positive side of a hyperplane deﬁned by the vector θ and bias θ0, we assign
it to class 1, otherwise to class 2. (We return to the geometric interpretation of this in chapter(17)). The
empirical risk then becomes a function of the parameters θ =

,

θ, θ0

{

}

R(θ

) =

|D

1
N

(cid:88)

n

L(cn, c(xn

θ))
|

The optimal parameters θ are given by minimising the empirical risk with respect to θ,

θopt = argmin

θ

R(θ

)

|D

The decision for a new datapoint x∗ is then given by c(x∗

θopt).
|

(13.2.13)

(13.2.14)

In this empirical risk minimisation approach, as we make the decision function c(x
θ) more ﬂexible, the
|
θ)
empirical risk goes down. However, if we make c(x
|
will perform well on a novel input x∗. The reason for this is that a ﬂexible decision function c(x
θ) is one
|
for which the class label can change for only a small change in x. Such ﬂexibility seems good since it means
that we will be able to ﬁnd a parameter setting θ so that the train data is ﬁtted well. However, since we are
θ) may change rapidly
only constraining the decision function on the known training points, a ﬂexible c(x
|
as we move away from the train data, leading to poor generalisation. To constrain the complexity of c(x
θ)
we may minimise the penalised empirical risk

θ) too ﬂexible we will have little conﬁdence that c(x
|

|

R(cid:48)(θ

|D

) = R(θ

|D

) + λP (θ)

(13.2.15)

θ). The regularisation constant, λ, determines
where P (θ) is a function that penalises complex functions c(x
|
the strength of this penalty and is typically set by validation – see below. The empirical risk approach is
summarised in ﬁg(13.2).

For the linear decision function above, it is reasonable to penalise wildly changing classiﬁcations in the
sense that if we change the input x by only a small amount we expect (on average) minimal change in the
class label. The squared diﬀerence in θTx + θ0 for two inputs x1 and x2 is (cid:0)θT∆x(cid:1)2
x1.
By constraining the length of θ to be small we limit the ability of the classiﬁer to change class for only a
small change in the input space. Assuming the distance between two datapoints is distributed according
to an isotropic multivariate Gaussian with zero mean and covariance σ2I, the average squared change is
(cid:68)(cid:0)θT∆x(cid:1)2(cid:69)
= σ2θTθ, motivating the choice of the Euclidean squared length of the parameter θ as the
penalty term, P (θ) = θTθ.

where ∆x

x2

−

≡

300

DRAFT April 24, 2015

Supervised Learning

Train

Validate

Test

Figure 13.3: Models can be trained using the train data based on
diﬀerent regularisation parameters. The optimal regularisation
parameter is determined by the empirical performance on the
validation data. An independent measure of the generalisation
performance is obtained by using a separate test set.

Algorithm 13.1 Setting regularisation parameters using cross-validation.

i
validate

D

(cid:9) , i = 1, . . . , K

1: Choose a set of regularisation parameters λ1, . . . , λA
2: Choose a set of training and validation set splits (cid:8)
i
train,
3: for a = 1 to A do
4:

for i = 1 to K do

D

θi
a = argmin

(cid:2)R(θ

i

train) + λaP (θ)(cid:3)

θ

|D

5:

6:

end for
L(λa) = 1
K

7:
8: end for
9: λopt = argmin

λa

(cid:80)K

i=1 R(θi

a|D

i
validate)

L(λa)

Validation

In penalised empirical risk minimisation we need to set the regularisation constant λ. This can be achieved
by evaluating the performance of the learned classiﬁer c(x
validate for several diﬀerent
λ values, and choosing the λ which gave rise to the classiﬁer with the best performance. It’s important that
the validation data is not the data on which the model was trained since we know that the optimal setting
for λ in that case is zero, and again we will have little conﬁdence in the generalisation ability.

θ) on validation data
|

D

D

we split this into disjoint parts,

validate, where the size of the validation set is
Given a dataset
D
usually chosen to be smaller than the train set, see ﬁg(13.3). For each parameter λa one then ﬁnds the
minimal empirical risk parameter θa. The optimal λ is chosen as that which gives rise to the model with
the minimal validation risk. Using the optimal regularisation parameter λ, many practitioners retrain θ on
the basis of the whole dataset

train,

D

.

D

In cross-validation the dataset is partitioned into training and validation sets multiple times with validation
i
results obtained for each partition. Each partition produces a diﬀerent training
validate
set, along with an optimal penalised empirical risk parameter θi
a and associated (unregularised) validation
performance R(θi
i
validate). The performance of regularisation parameter λa is taken as the average of the
validation performances over i. The best regularisation parameter is then given as that with the minimal
is split
average validation error, see algorithm(13.1). More speciﬁcally, in K-fold cross-validation the data
i
into K equal sized disjoint parts
validate. This gives a
K. Then
D
total of K diﬀerent training-validation sets over which performance is averaged, see ﬁg(13.4). In practice

i
train and validation

i
validate =

i
train =

1, . . . ,

i and

D\D

a|D

D

D

D

D

D

D

D

Train

Validate

Train

Validate

Train

Validate

Train

Test

DRAFT April 24, 2015

Figure 13.4: In cross-validation the dataset is split into several
train-validation sets. Depicted is 3-fold cross-validation. For
a range of regularisation parameters, the optimal regularisation
parameter is found based on the empirical validation performance
averaged across the diﬀerent splits.

301

Supervised Learning

(a)

(b)

Figure 13.5: The true function which generated the noisy data is the dashed line; the function learned from
the data is given by the solid line. (a): The unregularised ﬁt (λ = 0) to training given by
. Whilst
(b): The
the training data is well ﬁtted, the error on the validation examples, denoted by +, is high.
regularised ﬁt (λ = 0.5). Whilst the train error is high, the validation error is low.

×

10-fold cross-validation is popular, as is leave-one-out cross-validation in which the validation sets consist of
only a single example.

Example 13.4 (Finding a good regularisation parameter). In ﬁg(13.5), we ﬁt the function a sin(wx)
to
the plotted data, learning the parameters a, w based on minimising the squared loss. The unregularised
solution ﬁg(13.5a) badly overﬁts the data, and has a high validation error. To encourage a smoother solution,
a regularisation term w2 is used. The validation error based on several diﬀerent values of the regularisation
parameter λ was computed, and λ = 0.5 gives the lowest validation error. The resulting ﬁt to the data,
found by retraining a and w using the validation-optimal λ, is reasonable, see ﬁg(13.5b).

Beneﬁts of the empirical risk approach

In the limit of a large amount of training data the empirical distribution tends to the correct distri-
bution.

The discriminant function is chosen on the basis of minimal risk, which is the quantity we are ultimately
interested in.

The procedure is conceptually straightforward.

•

•

•

Drawbacks of the empirical risk approach

It seems extreme to assume that the data follows the empirical distribution, particularly for small
amounts of training data. More reasonable assumptions for p(x) would take into account likely x that
could arise, not just those in the train data.

If the loss function changes, the discriminant function needs to be retrained.

Some problems require an estimate of the conﬁdence of the prediction. Whilst there may be heuristic
ways to evaluating conﬁdence in the prediction, this is not inherent in the framework.

When there are many penalty parameters, performing cross-validation in a discretised grid of the
parameters becomes infeasible.

During validation, many models are trained, and all but one subsequently discarded.

•

•

•

•

•

13.2.3 Bayesian decision approach

An alternative to using the empirical distribution is to ﬁrst ﬁt a model p(c, x
. Given
θ) to the train data
|
this model, the decision function c(x) is automatically determined from the maximal expected utility (or
minimal risk) with respect to this model, as in equation (13.2.6), in which the unknown p(ctrue
x) is replaced
|

D

302

DRAFT April 24, 2015

−3−2−10123−1.5−1−0.500.511.5−3−2−10123−1.5−1−0.500.511.5Supervised Learning

,

C

X

x∗

θ

θ)
p(x, c
|

x∗, θ)

p(c
|

L(c, c∗)
(cid:104)

(cid:105)p(c|x∗,θ)

c∗

Figure 13.6: Bayesian decision approach. A
model p(x, c
θ) is ﬁtted to the data. After learn-
|
ing the optimal model parameters θ, we compute
x, θ). For a novel x∗, the distribution of the
p(c
|
x∗, θ). The prediction (de-
assumed ‘truth’ is p(c
|
cision) is then given by that c∗ which minimises
L(c, c∗)
the expected risk

(cid:104)

(cid:105)p(c|x∗,θ).

x, θ). See ﬁg(13.6) for a schematic depiction of the Bayesian decision approach.
with p(c
|

There are two main approaches to ﬁtting p(c, x
θ) to data
|
distribution using

D

, see ﬁg(13.7). We could parameterise the joint

p(c, x
|

x, θc|x)p(x
θ) = p(c
|

θx)
|

discriminative approach

or

p(c, x
|

θ) = p(x

c, θx|c)p(c
|
|

θc)

generative approach

(13.2.16)

(13.2.17)

We’ll consider these two approaches below in the context of trying to make a system that can distinguish
between a male and female face. The setup is that we have a database of face images in which each image
is represented as a real-valued vector xn, n = 1, . . . , N ), along with a label cn
stating if the image
is male or female.

0, 1
}

∈ {

Generative approach p(x, c
|

θ) = p(x

θc)
c, θx|c)p(c
|
|

For simplicity we use maximum likelihood training for the parameters θ. Assuming the data
have a log likelihood

D

is i.i.d., we

log p(

θ) =

D|

(cid:88)

n

log p(xn

cn, θx|c) +
|

(cid:88)

n

log p(cn

θc)

|

(13.2.18)

As we see the dependence on θx|c occurs only in the ﬁrst term, and θc only occurs in the second. This
means that learning the optimal parameters is equivalent to isolating the data for the male-class and ﬁt-
c = male, θx|male). We may similarly isolate the female data and ﬁt a separate model
ting a model p(x
|
c = female, θx|female). The class distribution p(c
p(x
θc) is set according to the ratio of males/females in the
|
set of training data.

|

To make a classiﬁcation of a new image x∗ as either male or female, we use Bayes’ rule:

p(c = male

x∗) =
|

p(x∗, c = male
θx|male)
|
θx|male) + p(x∗, c = female

p(x∗, c = male
|

θx|female)
|

(13.2.19)

θc

cn

θx|c

θc|x

θx

xn

N

cn

xn

N

(a)

(b)

Figure 13.7: Two generic strategies for probabilistic
classiﬁcation. (a): Class dependent generative model
of x. After learning parameters, classiﬁcation is ob-
tained by making x evidential and inferring p(c
x).
|
(b): A discriminative classiﬁcation method p(c
x).
|

DRAFT April 24, 2015

303

Supervised Learning

Based on zero-one loss, if this probability is greater than 0.5 we classify x∗ as male, otherwise female. For a
general loss function, we use this probability as part of a decision process, as in equation (13.2.6).

Advantages Prior information about the structure of the data is often most naturally speciﬁed through
c). For example, for male faces, we would expect to see heavier eyebrows, a
|

a generative model p(x
squarer jaw, etc.

Disadvantages The generative approach does not directly target the classiﬁcation model p(c
x) since the
|
If the data x is complex, ﬁnding a suitable
goal of generative training is rather to model p(x
generative data model p(x
c) is a diﬃcult task. Furthermore, since each generative model is separately
|
trained for each class, there is no competition amongst the models to explain the x data. On the
other hand it might be that making a model of p(c
x) is simpler, particularly if the decision boundary
|
between the classes has a simple form, even if the data distribution of each class is complex, see
ﬁg(13.8).

c).
|

x, θc|x)p(x
Discriminative approach p(x, c
θ) = p(c
|
|

θx)
|

Assuming i.i.d. data, the log likelihood is
(cid:88)

log p(cn

xn, θc|x) +

log p(

θ) =

D|

n

|

(cid:88)

n

log p(xn

θx)
|

(13.2.20)

The parameters are isolated in the two terms so that maximum likelihood training is equivalent to ﬁnding
the parameters of θc|x that will best predict the class c for a given training input x. The parameters θx
for modelling the data occur only in the second term above, and setting them can therefore be treated
as a separate unsupervised learning problem. This approach consequently isolates modelling the decision
boundary from modelling the input distribution, see ﬁg(13.8).

Classiﬁcation of a new point x∗ is based on

x, θopt
c|x )
p(c
|

(13.2.21)

As for the generative case, this approach still learns a joint distribution p(c, x) = p(c
x)p(x) which can be
|
used as part of a decision process if required, equation (13.2.6).

Advantages The discriminative approach directly addresses ﬁnding an accurate classiﬁer p(c
x) based on
|
modelling the decision boundary, as opposed to the class conditional data distribution in the generative
approach. Whilst the data from each class may be distributed in a complex way, it could be that the
decision boundary between them is relatively easy to model.

Disadvantages Discriminative approaches are usually trained as ‘black-box’ classiﬁers, with little prior
knowledge built used to describe how data for a given class is distributed. Domain knowledge is often
more easily expressed using the generative framework.

Hybrid generative-discriminative approaches

One could use a generative description, p(x
c), building in prior information, and use this to form a joint
|
distribution p(x, c), from which a discriminative model p(c
x) may be formed, using Bayes’ rule. Speciﬁcally,
|
we can use

(cid:80)

x, θ) =
p(c
|

θc)
c, θx|c)p(c
|
θc)
c, θx|c)p(c
|
|

p(x
|
c p(x
(cid:1), for this hybrid model can be found by maximising the probability
Subsequently the parameters θ = (cid:0)θx|c, θc
θx). This approach would appear to leverage
of being in the correct class. A separate model is learned for p(x
the advantages of both the discriminative and generative frameworks since we can more readily incorporate
domain knowledge in the generative model p(x
c, θx|c) yet train this in a discriminative way. This approach
is rarely taken in practice since the resulting functional form of the likelihood depends in a complex manner
on the parameters. In this case no parameter separation between θc and θx|c occurs (as was previously the
case for the generative and discriminative approaches).

(13.2.22)

|

|

304

DRAFT April 24, 2015

Supervised Learning

Figure 13.8: Each point represents a high dimensional vector with an
associated class label, either male or female. The point x∗ is a new
point for which we would like to predict whether this should be male or
male) generates
female. In the generative approach, a male model p(x
data similar to the ‘m’ points. Similarly, the female model p(x
female)
|
generates points that are similar to the ‘f’ points above. We then use
x∗) using the two ﬁtted
Bayes’ rule to calculate the probability p(male
|
models, as given in the text. In the discriminative approach, we directly
x∗), which cares less about how the points ‘m’
make a model of p(male
|
or ‘f’ are distributed, but more about describing the boundary which
can separate the two classes, as given by the line. In this case, modelling
the distribution of the data is not required since our decisions are only
related to which side of the decision boundary a point lies.

|

Features and preprocessing

It is often the case that in discriminative training, transforming the raw input x into a form that more
directly captures the relevant label information can greatly improve performance. For example, in the male-
female classiﬁcation case, it might be that building a classiﬁer directly in terms of the elements of the face
vector x is diﬃcult. However, using ‘features’ which contain geometric information such as the distance
between eyes, width of mouth, etc. may make ﬁnding a classiﬁer easier.
In practice data is also often
preprocessed to remove noise, centre an image etc.

Learning lower-dimensional representations in semi-supervised learning

One way to exploit a large amount of unlabelled training data to improve classiﬁcation is to ﬁrst ﬁnd a
lower dimensional representation h of the data x. Based on this, the mapping from h to c may be rather
simpler to learn than a mapping from x to c directly. We use cn =
to indicate that the class for datapoint
n is missing. We can then form the likelihood on the visible data using, see ﬁg(13.9),

∅

p(

C

,

,

X

θ) =

H|

(cid:89)

(cid:8)p(cn

n

|

hn, θc|h)(cid:9)I[cn(cid:54)=∅]

p(xn

hn, θx|h)p(h
θh)
|

|

and set any parameters for example by using maximum likelihood

θopt = argmax

θ

(cid:88)

H

p(

,

C

X

,

θ)

H|

(13.2.23)

(13.2.24)

Beneﬁts of the Bayesian decision approach

This is a conceptually ‘clean’ approach, in which one tries ones best to model the environment (using
either a generative or discriminative approach), independent of the subsequent decision process. In
this case learning the environment is separated from the eﬀect this will have on the expected utility.
The decision c∗ for a novel input x∗ can be a highly complex function of x∗ due to the maximisation
operation.

If p(x, c
|

θ) is the ‘true’ model of the data, this approach is optimal.

•

•

•

Drawbacks of the Bayesian decision approach

•

•

θ) is poor, the prediction c∗ could be highly inaccurate since modelling
If the environment model p(c, x
|
the environment is divorced from prediction.

θ) from its eﬀect on decisions, in practice
To avoid fully divorcing the learning of the model p(c, x
|
θ) which are set by validation
one often includes regularisation terms in the environment model p(c, x
|
based on an empirical loss.

DRAFT April 24, 2015

305

*mmmmmmmffffffffffxBayes versus Empirical Decisions

θc|h

θh

θx|h

cn

hn

xn

N

Figure 13.9: A strategy for semi-supervised learning. When cn is miss-
hn) is absent. The large amount of training data
ing, the term p(cn
|
helps the model learn a good lower dimension/compressed representa-
tion h of the data x. Fitting then a classiﬁcation model p(c
h) using
|
this lower dimensional representation may be much easier than ﬁtting
a model directly from the complex data to the class, p(c
|

x).

13.3 Bayes versus Empirical Decisions

The empirical risk and Bayesian approaches are at the extremes of the philosophical spectrum.
In the
empirical risk approach one makes a seemingly over-simplistic data generating assumption. However decision
function parameters are set based on the task of making decisions. On the other hand, the Bayesian approach
attempts to learn meaningful p(c, x) without regard to its ultimate use as part of a larger decision process.
What ‘objective’ criterion can we use to learn p(c, x), particularly if we are only interested in classiﬁcation
with a low test-risk? The following example is intended to recapitulate the two generic Bayes and empirical
risk approaches we’ve been considering. Note that we’ve previously written utilities suggesting that they
are both for example class labels; however the theory applies more generally, for example to utilities u(c, d)
where d is some decision (which is not necessarily of the form of the class label c).

Example 13.5 (The two generic decision strategies). Consider a situation in which, based on patient
information x, we need to take a decision d as whether or not to operate. The utility of operating u(c, d)
depends on whether or not the patient has cancer, c. For example

u(cancer, operate) = 100
u(cancer, don’t operate) = 0 u(benign, don’t operate) = 70

u(benign, operate) = 30

(13.3.1)

We have independent true assessments of whether or not a patient had cancer, giving rise to a set of
. Faced with a new patient with information ,x, we need to
historical records
=
}
make a decision whether or not to operate.

(xn, cn), n = 1, . . . , N
{

D

x,
) (for example using a discriminative
In the Bayesian decision approach one would ﬁrst make a model p(c
|
model such as logistic regression, section(17.4.1)). Using this model the decision is given by that which
maximises the expected utility

D

d = argmax

[p(cancer

d

x,
|

x,
)u(cancer, d) + p(benign
|

D

D

)u(benign, d)]

(13.3.2)

x,
In this approach learning the model p(c
) is divorced from the ultimate use of the model in the decision
|
making process. An advantage of this approach is that, from the viewpoint of expected utility, it is optimal
– provided the model p(c
) is ‘correct’. Unfortunately, this is rarely the case. Given the limited model
|
resources, it might make sense to focus on ensuring the prediction of cancer is correct since this has a more
signiﬁcant eﬀect on the utility. However, formally, this would require a corruption of this framework.

x,

D

D

The alternative empirical utility approach recognises that the task can be stated as to translate patient
information x into an operation decision d. To do so one could parameterise this as d(x) = f (x
θ) and then
|
learn θ under maximising the empirical utility

u(θ) =

(cid:88)

n

u(f (xn

θ), cn)
|

(13.3.3)

For example, if x is a vector representing the patient information and θ the parameter, we might use a
linear decision function such as

(cid:26) θTx

0 d = operate
θTx < 0 d = don’t operate

≥

f (x

θ) =
|

306

(13.3.4)

DRAFT April 24, 2015

Exercises

The advantage of this approach is that the parameters of the decision are directly related to the utility of
x) and would wish to make
making the decision. However, it may be that we have a good model of p(c
|
use of this. A disadvantage is that we cannot easily incorporate such domain knowledge into the decision
function.

Both approaches are heavily used in practice and which is to be preferred depends very much on the problem.
Whilst the Bayesian approach appears formally optimal, it is prone to model mis-speciﬁcation. A pragmatic
alternative Bayesian approach is to ﬁt a parameterised distribution p(c, x
, where λ penalises
λ) to the data
|
complexity of the ﬁtted distribution, setting λ using validation on the risk. This has the potential advantage
of allowing one to incorporate sensible prior information about p(c, x) whilst assessing competing models
in the light of their actual predictive risk. Similarly, for the empirical risk approach, one can modify the
extreme empirical distribution assumption by using a more plausible model p(c, x) of the data.

D

13.4 Summary

•

•

•

•

Supervised and unsupervised learning are the two main branches of machine learning considered in this
book.

The two classical approaches in supervised learning are empirical risk minimisation and Bayesian decision
theory.

In the empirical risk minimisation, there is typically no explicit model of the data, and the focus is on the
end use of the predictor.

In the Bayesian decision approach, an explicit model of the data is made, with the end decision/classiﬁcation
being computed independently of the ﬁtting of the model to the data.

A general introduction to machine learning is given in [215]. An excellent reference for Bayesian decision
theory is [33]. Approaches based on empirical risk are discussed in [302].

13.5 Exercises

class 1) =
Exercise 13.1. Given the distributions p(x
|
corresponding prior occurrence of classes p1 and p2 (p1+p2 = 1), calculate the decision boundary p(class1
|
0.5 explicitly as a function of µ1, µ2, σ2
and are they all reasonable?

(cid:1), with
x) =
2, p1, p2. How many solutions are there to the decision boundary

(cid:1) and p(x
|

(cid:0)x µ1, σ2

(cid:0)x µ2, σ2

class 2) =

1, σ2

N

N

2

1

Exercise 13.2. Under zero-one loss, the Bayes’ decision rule chooses class k if p(class k
for all j
q(class j
decision rule.

x)
|
Imagine instead we use a randomized decision rule, choosing class j with probability
x). Calculate the error for this decision rule, and show that the error is minimized by using Bayes’
|

x) > p(class j
|

= k.

Exercise 13.3. For a novel input x, a predictive model of the class c is given by p(c = 1
x) = 0.7,
|
x) = 0.1. The corresponding utility matrix U (ctrue, cpred) has elements
x) = 0.2, p(c = 3
p(c = 2
|
|






(13.5.1)



3
5
0
4
3 0

−

1
2
−
10

In terms of maximal expected utility, which is the best decision to take?

Exercise 13.4. Consider datapoints generated from two diﬀerent classes. Class 1 has the distribution
(cid:0)x m2, σ2(cid:1). The prior probabil-
p(x
x) is of the
ities of each class are p(c = 1) = p(c = 2) = 1/2. Show that the posterior probability p(c = 1
|

(cid:0)x m1, σ2(cid:1) and class 2 has the distribution p(x

c = 1)
|

c = 2)
|

∼ N

∼ N

DRAFT April 24, 2015

307

(cid:54)
form

x) =
p(c = 1
|

1 + exp

1

−

(ax + b)

and determine a and b in terms of m1, m2 and σ2.

Exercises

(13.5.2)

Exercise 13.5. WowCo.com is a new startup prediction company. After years of failures, they eventually
ﬁnd a neural network with a trillion hidden units that achieves zero test error on every learning problem
posted on the internet up to last week. Each learning problem included a train and test set. Proud of their
achievement, they market their product aggressively with the claim that it ‘predicts perfectly on all known
problems’. Discuss whether or not these claims would justify buying this product.

Exercise 13.6. For a prediction model ˜p(y
a measure of the accuracy as

|

x) and true data generating distribution p(x, y), one may deﬁne

(cid:90)

A =

p(x, y)˜p(y

x,y

x)
|

which is the average overlap between the true and predicting distributions.

1. By deﬁning

ˆp(x, y)

≡
and considering

p(x, y)˜p(y

x)
|

A

KL(q(x, y)

ˆp(x, y))
|

≥

0

show that for any distribution q(x, y),

log A

log ˜p(y

x)

≥ (cid:104)
2. Consider a set of train data

|

(cid:105)q(x,y) −
=

KL(q(x, y)
p(x, y))
|

(xn, yn), n = 1, . . . , N

{

}

and deﬁne the empirical distribution

D

q(x, y) =

1
N

N
(cid:88)

n=1

δ (x, xn) δ (y, yn)

Show that

log A

≥

1
N

N
(cid:88)

n=1

log ˜p(yn

xn)
|

KL(q(x, y)
|

−

p(x, y))

This shows that the log prediction accuracy is lower bounded by the training accuracy and the ‘gap’
between the empirical distribution and the unknown true data generating mechanism. According to
this naive bound (which doesn’t account for possible overﬁtting), the best thing to do to increase the
prediction accuracy is to increase the training accuracy (since the Kullback-Leibler term is indepen-
dent of the predictor). As N increases, the empirical distribution tends to the true distribution and
the Kullback-Leibler term becomes small, justifying minimising the train error.

Assuming that the train outputs are drawn from a distribution p(y
istic, show that

x) = δ (y, f (x)) which is determin-
|

log A

≥

1
N

N
(cid:88)

n=1

log ˜p(yn

xn)
|

−

KL(q(x)

p(x))
|

(13.5.9)

and hence that, provided the train data is correctly predicted with full certainty, the accuracy can be
related to the empirical and true input distribution by

A

≥

exp (

KL(q(x)

−

p(x)))
|

(13.5.10)

Exercise 13.7. You wish to make a classiﬁer for a variable c based on two kinds of inputs, x and y. You
have a generative model p(y
x). Explain how to combine these to make a
x, y).
model p(c
|

c) and a discriminative model p(c
|
|

308

DRAFT April 24, 2015

(13.5.3)

(13.5.4)

(13.5.5)

(13.5.6)

(13.5.7)

(13.5.8)

CHAPTER 14

Nearest Neighbour Classiﬁcation

Often when faced with a classiﬁcation problem it is useful to ﬁrst employ a simple method to produce a
baseline against which more complex methods can be compared. In this chapter we discuss the simple nearest
neighbour method. The nearest neighbour methods are extremely popular and can perform surprisingly well.
We also discuss how these methods are related to probabilistic mixture models.

14.1 Do As Your Neighbour Does

Successful prediction typically relies on smoothness in the data – if the class label can change as we move
a small amount in the input space, the problem is essentially random and no algorithm will generalise well.
In machine learning one constructs appropriate measures of smoothness for the problem at hand and hopes
to exploit this to obtain good generalisation. Nearest neighbour methods are a useful starting point since
they readily encode basic smoothness intuitions and are easy to program.

In a classiﬁcation problem each input vector x has a corresponding class label, cn
. Given a
}
, n = 1, . . . , N , and a novel x, we aim to return the correct class
dataset of N train examples,
c(x). A simple, but often eﬀective, strategy for this supervised learning problem can be stated as: for novel
x, ﬁnd the nearest input in the train set and use the class of this nearest input, algorithm(14.1). For vectors
x and x(cid:48) representing two diﬀerent datapoints, we measure ‘nearness’ using a dissimilarity function d(x, x(cid:48)).
A common dissimilarity is the squared Euclidean distance

xn, cn
{

1, . . . , C

∈ {

=

D

}

d(x, x(cid:48)) = (x

x(cid:48))T(x

x(cid:48))

(14.1.1)

−

−

x(cid:48))2. Based on the squared Euclidean distance, the decision
which can be more conveniently written (x
boundary is determined by the perpendicular bisectors of the closest training points with diﬀerent training
labels, see ﬁg(14.1). This partitions the input space into regions classiﬁed equally and is called a Voronoi
tessellation.

−

The nearest neighbour algorithm is simple and intuitive. There are, however, some issues:

•

How should we measure the distance between points? Whilst the Euclidean square distance is popular,
this may not always be appropriate. A fundamental limitation of the Euclidean distance is that it does
not take into account how the data is distributed. For example if the length scales of the components of
the vector x vary greatly, the largest length scale will dominate the squared distance, with potentially
useful class-speciﬁc information in other components of x lost. The Mahalanobis distance

d(x, x(cid:48)) = (cid:0)x

x(cid:48)(cid:1)T Σ−1 (cid:0)x

x(cid:48)(cid:1)

−

−

(14.1.2)

where Σ is the covariance matrix of the inputs (from all classes) can overcome some of these problems
since it eﬀectively rescales the input vector components.

309

K-Nearest Neighbours

Figure 14.1: In nearest neighbour classiﬁcation a new
vector is assigned the label of the nearest vector in the
training set. Here there are three classes, with training
points given by the circles, along with their class. The
dots indicate the class of the nearest training vector.
The decision boundary is piecewise linear with each
segment corresponding to the perpendicular bisector
between two datapoints belonging to diﬀerent classes,
giving rise to a Voronoi tessellation of the input space.

Algorithm 14.1 Nearest neighbour algorithm to classify a vector x, given train data
(xn, cn), n = 1, . . . , N

=

D

{

:
}

1: Calculate the dissimilarity of the test point x to each of the train points, dn = d (x, xn), n = 1, . . . , N .
2: Find the train point xn∗ which is nearest to x :

n∗ = argmin

n

d (x, xn)

3: Assign the class label c(x) = cn∗.
4: In the case that there are two or more nearest neighbours with diﬀerent class labels, the most numerous class

is chosen. If there is no one single most numerous class, we use the K-nearest-neighbours.

The whole dataset needs to be stored to make a classiﬁcation since the novel point must be compared
to all of the train points. This can be partially addressed by a method called data editing in which
datapoints which have little or no eﬀect on the decision boundary are removed from the training
dataset. Depending on the geometry of the training points, ﬁnding the nearest neighbour can also be
accelerated by examining the values of each of the components xi of x in turn. Such an axis-aligned
space-split is called a KD-tree[218] and can reduce the possible set of candidate nearest neighbours in
the training set to the novel x∗, particularly in low dimensions.
Each distance calculation can be expensive if the datapoints are high dimensional. Principal Com-
ponents Analysis, see chapter(15), is one way to address this and replaces x with a low dimensional
projection p. The Euclidean distance of two datapoints (cid:0)xa
is then approximately given by
(cid:0)pa
, see section(15.2.4). This is both faster to compute and can also improve classiﬁcation
accuracy since only the large scale characteristics of the data are retained in the PCA projections.

pb(cid:1)2

xb(cid:1)2

−

−

It is not clear how to deal with missing data or incorporate prior beliefs and domain knowledge.

•

•

•

14.2 K-Nearest Neighbours

If your neighbour is simply mistaken (has an incorrect training class label), or is not a particularly repre-
sentative example of his class, then these situations will typically result in an incorrect classiﬁcation. By
including more than the single nearest neighbour, we hope to make a more robust classiﬁer with a smoother
decision boundary (less swayed by single neighbour opinions). If we assume the Euclidean distance as the
dissimilarity measure, the K-Nearest Neighbour algorithm considers a hypersphere centred on the test point
x. The radius of the hypersphere is increased until it contains exactly K train inputs. The class label c(x)
is then given by the most numerous class within the hypersphere, see ﬁg(14.2).

310

DRAFT April 24, 2015

K-Nearest Neighbours

Choosing K

Figure 14.2: In K-nearest neighbours, we centre a hypersphere around the
point we wish to classify (here the central dot). The inner circle corre-
sponds to the nearest neighbour, a square. However, using the 3 near-
est neighbours, we ﬁnd that there are two round-class neighbours and one
square-class neighbour– and we would therefore classify the central point as
round-class. In the case of a tie, one may increase K until the tie is broken.

Whilst there is some sense in making K > 1, there is certainly little sense in making K = N (N being the
number of training points). For K very large, all classiﬁcations will become the same – simply assign each
novel x to the most numerous class in the train data. This suggests that there is an optimal intermediate
setting of K which gives the best generalisation performance. This can be determined using cross-validation,
as described in section(13.2.2).

(a)

(b)

(c)

Figure 14.3: Some of the train examples of the digit zero (a), one (b) and seven (c). There are 300 train
examples of each of these three digit classes.

Example 14.1 (Handwritten Digit Example). Consider two classes of handwritten digits, zeros and ones.
Each digit contains 28
28 = 784 pixels. The train data consists of 300 zeros, and 300 ones, a subset
of which are plotted in ﬁg(14.3a,b). To test the performance of the nearest neighbour method (based on
Euclidean distance) we use an independent test set containing a further 600 digits. The nearest neighbour
method, applied to this data, correctly predicts the class label of all 600 test points. The reason for the high
success rate is that examples of zeros and ones are suﬃciently diﬀerent that they can be easily distinguished.

×

A more diﬃcult task is to distinguish between ones and sevens. We repeat the above experiment, now
using 300 training examples of ones, and 300 training examples of sevens, ﬁg(14.3b,c). Again, 600 new test
examples (containing 300 ones and 300 sevens) were used to assess the performance. This time, 18 errors
are found using nearest neighbour classiﬁcation – a 3% error rate for this two class problem. The 18 test
points on which the nearest neighbour method makes errors are plotted in ﬁg(14.4). If we use K = 3 nearest
neighbours, the classiﬁcation error reduces to 14 – a slight improvement. As an aside, the best machine
learning methods classify real world digits (over all 10 classes) to an error of less than 1 per cent – better
than the performance of an ‘average’ human.

DRAFT April 24, 2015

311

A Probabilistic Interpretation of Nearest Neighbours

Figure 14.4: ‘1’ versus ‘7’ classiﬁcation us-
ing the NN method. (Top) The 18 out of
600 test examples that are incorrectly clas-
siﬁed; (Bottom) the nearest neighbours in
the training set corresponding to each test-
point above.

14.3 A Probabilistic Interpretation of Nearest Neighbours

Consider the situation where we have data from two classes – class 0 and class 1. We make the following
mixture model for data from class 0 which places a Gaussian on each datapoint:

p(x

c = 0) =
|

1
N0

(cid:88)

n∈ class 0

N

(cid:0)x xn, σ2I(cid:1) =

1
N0

1
(2πσ2)D/2

(cid:88)

e−(x−xn)2/(2σ2)

n∈ class 0

(14.3.1)

where D is the dimension of a datapoint x and N0 are the number of train points of class 0, and σ2 is
the variance. This is a Parzen estimator , and models the data as a uniform weighted sum of Gaussian
distributions centred on the training points, ﬁg(14.5).

Similarly, for data from class 1:

p(x

c = 1) =
|

1
N1

(cid:88)

n∈ class 1

N

(cid:0)x xn, σ2I(cid:1) =

1
N1

1
(2πσ2)D/2

(cid:88)

e−(x−xn)2/(2σ2)

n∈ class 1

To classify a new datapoint x∗, we use Bayes’ rule

x∗) =
p(c = 0
|

p(x∗

|

p(x∗

c = 0)p(c = 0)

|
c = 0)p(c = 0) + p(x∗

c = 1)p(c = 1)

|

(14.3.2)

(14.3.3)

The maximum likelihood setting of p(c = 0) is N0/(N0 + N1), and p(c = 1) = N1/(N0 + N1). An analogous
x∗). To see which class is most likely we may use the ratio
expression to equation (14.3.3) holds for p(c = 1
|

x∗)
p(c = 0
|
x∗)
p(c = 1
|

=

p(x∗
p(x∗

|
|

c = 0)p(c = 0)
c = 1)p(c = 1)

If this ratio is greater than one, we classify x∗ as 0, otherwise 1.

(14.3.4)

Equation(14.3.4) is a complicated function of x∗. However, if σ2 is very small, the numerator, which is a
sum of exponential terms, will be dominated by that term for which datapoint xn0 in class 0 is closest to
the point x∗. Similarly, the denominator will be dominated by that datapoint xn1 in class 1 which is closest
to x∗. In this case, therefore,

p(c = 0
|
p(c = 1
|

x∗)
x∗) ≈

e−(x∗−xn0 )2/(2σ2)p(c = 0)/N0
e−(x∗−xn1 )2/(2σ2)p(c = 1)/N1

=

e−(x∗−xn0 )2/(2σ2)
e−(x∗−xn1 )2/(2σ2)

(14.3.5)

0, with certainty we classify x∗ as class 0 if x∗ is closer to xn0 than to xn1. The
Taking the limit σ2
nearest (single) neighbour method is therefore recovered as the limiting case of a probabilistic generative
model, see ﬁg(14.5).

→

Figure 14.5: A probabilistic interpretation of nearest neighbours. For each
class we use a mixture of Gaussians to model the data from that class p(x
c),
|
placing at each training point an isotropic Gaussian of width σ2. The width
of each Gaussian is represented by the circle. In the limit σ2
0 a novel
point (small black dot) is assigned the class of its nearest neighbour. For
ﬁnite σ2 > 0 the inﬂuence of non-nearest neighbours has an eﬀect, resulting
in a soft version of nearest neighbours.

→

312

DRAFT April 24, 2015

Exercises

The motivation for K nearest neighbours is to produce a classiﬁcation that is robust against unrepresen-
tative single nearest neighbours. To ensure a similar kind of robustness in the probabilistic interpretation,
we may use a ﬁnite value σ2 > 0. This smoothes the extreme probabilities of classiﬁcation and means
that more points (not just the nearest) will have an eﬀective contribution in equation (14.3.4). The exten-
sion to more than two classes is straightforward, requiring a class conditional generative model for each class.

By using a richer generative model of the data we may go beyond the Parzen estimator approach. We will
examine such cases in some detail in later chapters, in particular chapter(20).

14.3.1 When your nearest neighbour is far away

For a novel input x∗ that is far from all training points, Nearest Neighbours, and its soft probabilistic variant
will conﬁdently classify x∗ as belonging to the class of the nearest training point. This is arguably opposite
to what we would like, namely that the classiﬁcation should tend to the prior probabilities of the class based
on the number of training data per class. A way to avoid this problem is, for each class, to include a ﬁctitious
large-variance mixture component at the mean of all the data, one for each class. For novel inputs close to
the training data, this extra ﬁctitious component will have no appreciable eﬀect. However, as we move away
from the high density regions of the training data, this additional ﬁctitious component will dominate since
it has larger variance than any of the other components. As the distance from x∗ to each ﬁctitious class
point is the same, in the limit that x∗ is far from the training data, the eﬀect is that no class information
from the position of x∗ occurs. See section(20.3.3) for an example.

14.4 Summary

•

•

Nearest neighbour methods are general classiﬁcation methods.

The NN method can be understood as a class conditional mixture of Gaussians in the limit of a vanishingly
small covariance for each mixture component model.

14.5 Code

nearNeigh.m: K nearest neighbour
majority.m: Find the majority entry in each column of a matrix
demoNearNeigh.m: K nearest neighbour Demo

14.6 Exercises

Exercise 14.1. The ﬁle NNdata.mat contains training and test data for the handwritten digits 5 and 9.
Using leave one out cross-validation, ﬁnd the optimal K in K-nearest neighours, and use this to compute
the classiﬁcation accuracy of the method on the test data.

Exercise 14.2. Write a routine SoftNearNeigh(xtrain,xtest,trainlabels,sigma) to implement soft
nearest neighbours, analogous to nearNeigh.m. Here sigma is the variance σ2 in equation (14.3.1). The
ﬁle NNdata.mat contains training and test data for the handwritten digits 5 and 9. Using leave one out
cross-validation, ﬁnd the optimal σ2 and use this to compute the classiﬁcation accuracy of the method on
the test data. Hint: you may have numerical diﬃculty with this method. To avoid this, consider using the
logarithm, and how to numerically compute log (cid:0)ea + eb(cid:1) for large (negative) a and b. See also logsumexp.m.

Exercise 14.3. The editor at YoMan! (a ‘mens’ magazine) has just had a great idea. Based on the success
of a recent national poll to test IQ, she decides to make a ‘Beauty Quotient’ (BQ) test. She collects as

DRAFT April 24, 2015

313

Exercises

many images of male faces as she can, taking care to make sure that all the images are scaled to roughly
the same size and under the same lighting conditions. She then gives each male face a BQ score from 0
(‘Severely Aesthetically Challenged’) to 100 (‘Generously Aesthetically Gifted’). Thus, for each real-valued
D-dimensional image x, there is an associated value b in the range 0 to 100. In total she collects N images
and associated scores,
. One morning, she bounces into your oﬃce and tells you the
good news : it is your task to make a test for the male nation to determine their Beauty Quotient. The
idea, she explains, is that a man can send online an image of their face x∗, to YoMan! and will immediately
receive an automatic BQ response b∗.

(xn, bn) , n = 1, . . . , N

}

{

1. As a ﬁrst step, you decide to use the K nearest neighbour method (KNN) to assign a BQ score b∗ to

a novel test image x∗. Describe how to determine the optimal number of neighbours K to use.

2. Your line manager is pleased with your algorithm but is disappointed that it does not provide any simple
explanation of Beauty that she can present in a future version of YoMan! magazine. To address this,
you decide to make a model based on linear regression. That is

b = wTx

(14.6.1)

where w is a parameter vector chosen to minimise

E(w) =

(cid:88)

(cid:16)

bn

n

−

wTxn(cid:17)2

After training (ﬁnding a suitable w), how can YoMan! explain to its readership in a simple way what
facial features are important for determining one’s BQ?

314

DRAFT April 24, 2015

CHAPTER 15

Unsupervised Linear Dimension Reduction

High-dimensional data is prevalent in machine learning and related areas. Indeed, there often arises the
situation in which there are more data dimensions than there are data examples. In such cases we seek a
lower dimensional representation of the data. In this chapter we discuss some standard methods which can
also improve the prediction performance by removing ‘noise’ from the representation.

15.1 High-Dimensional Spaces – Low Dimensional Manifolds

≈

In machine learning problems data is often high dimensional – images, bag-of-word descriptions, gene-
expressions etc. In such cases we cannot expect the training data to densely populate the space, meaning
that there will be large parts in which little is known about the data. For the hand-written digits from
chapter(14), the data is 784 dimensional and for binary valued pixels the number of possible images is
2784
10236. Nevertheless, we would expect that only a handful of examples of a digit should be suﬃcient
(for a human) to understand how to recognise a 7. Digit-like images must therefore occupy a highly
constrained volume in the 784 dimensions and we expect only a small number of degrees of freedom to
be required to describe the data to a reasonable accuracy. Whilst the data vectors may be very high
dimensional, they will therefore typically lie close to a much lower dimensional ‘manifold’ (informally, a
two-dimensional manifold corresponds to a warped sheet of paper embedded in a high dimensional space),
meaning that the distribution of the data is heavily constrained. Here we concentrate on computationally
eﬃcient linear dimension reduction techniques in which a high dimensional datapoint x is projected down
to a lower dimensional vector y by

y = Fx + const.

(15.1.1)

dim (x), with dim (y) < dim (x). The methods in this
The non-square matrix F has dimensions dim (y)
chapter are largely non-probabilistic, although many have natural probabilistic interpretations. For example,
PCA is closely related to Factor Analysis, as described in chapter(21).

×

15.2 Principal Components Analysis

If data lies close to a linear subspace, as in ﬁg(15.1) we can accurately approximate each data point by using
vectors that span the linear subspace alone. In such cases we aim to discover a low dimensional co-ordinate
system in which we can approximately represent the data. We express the approximation for datapoint xn
as

xn

c +

≈

M
(cid:88)

j=1

j bj
yn

˜xn

≡

315

(15.2.1)

Principal Components Analysis

Figure 15.1:
In linear dimension reduction a linear
subspace is ﬁtted such that the average squared dis-
tance between datapoints (red rings) and their projec-
tions onto the plane (black dots) is minimal.

Here the vector c is a constant and deﬁnes a point in the linear subspace and the bj are ‘basis’ vectors that
span the linear subspace (also known as ‘principal component coeﬃcients’ or ‘loadings’). Note that some
authors deﬁne PCA without the constant c. Collectively we can write B = (cid:2)b1, . . . , bM (cid:3). The yn
i are the
low dimensional coordinates of the data, forming a lower dimension yn for each datapoint n; collectively we
can write these lower dimensional vectors as Y = (cid:2)y1, . . . , yN (cid:3). Equation(15.2.1) expresses how to ﬁnd the
reconstruction ˜xn given the lower dimensional representation yn (which has components yn
i , i = 1, . . . , M ).
For a data space of dimension dim (x) = D, we hope to accurately describe the data using only a small
number M

D of co-ordinates y.

(cid:28)

To determine the best lower dimensional representation it is convenient to use the squared distance error
between x and its reconstruction ˜x:

E(B, Y, c) =

N
(cid:88)

D
(cid:88)

n=1

i=1

[xn

i −

i ]2
˜xn

(15.2.2)

It is straightforward to show that the optimal bias c is given by the mean of the data (cid:80)
We therefore assume that the data has been centred (has zero mean (cid:80)
zero, and concentrate on ﬁnding the optimal basis B below.

n xn/N , exercise(15.1).
n xn = 0), so that we can set c to

15.2.1 Deriving the optimal linear reconstruction

To ﬁnd the best basis vectors B (deﬁning [B]i,j = bj
wish to minimize the sum of squared diﬀerences between each vector x and its reconstruction ˜x:

i ) and corresponding low dimensional coordinates Y, we

E(B, Y) =

N
(cid:88)

D
(cid:88)

n=1

i=1


xn

i −

M
(cid:88)

j=1


2

j bj
yn

i



= trace

(cid:16)

(X

−

BY)T (X

(cid:17)

BY)

−

(15.2.3)

where X = (cid:2)x1, . . . , xN (cid:3).

An important observation is that the optimal solution for B and Y is not unique since the reconstruction
error E(B, Y) depends only on the product BY. Indeed, without loss of generality, we may constrain B
to be an orthonormal matrix. To see this, consider an invertible transformation Q of the basis B so that
BQ is an orthonormal matrix, ˜BT ˜B = I. Since Q is invertible, we may write BY = ˜B ˜Y on deﬁning
˜B
˜Y = Q−1Y. Since ˜Y is unconstrained (because Y is unconstrained), without loss of generality, we may
consider equation (15.2.3) under the orthonormality constraint BTB = I, namely that the basis vectors are
mutually orthogonal and of unit length.

≡

By diﬀerentiating equation (15.2.3) with respect to yn

k we obtain (using the orthonormality constraint on

316

DRAFT April 24, 2015

−2024−2024−10123Principal Components Analysis

B)

1
2

∂
∂yn
k

−

E(B, Y) =


xn

i −

(cid:88)

i

(cid:88)

j bj
yn

i

j


 bk

i =

(cid:88)

i

i bk
xn

i −

(cid:88)

yn
j

(cid:88)

bj
i bk
i

=

(cid:88)

j

i

(cid:124) (cid:123)(cid:122) (cid:125)
δjk

i

i bk
xn

i −

yn
k

The squared error E(B, Y) therefore has zero derivative when

yn
k =

(cid:88)

i xn
bk
i ,

i

which can be written as Y = BTX

(15.2.4)

We now substitute this solution into equation (15.2.3) to write the squared error as a function of B alone.
Using

BY)T (X

(X

−

−

BY) = XTX

XTBBTX

−

XTBBTX + XTB BTB
(cid:124) (cid:123)(cid:122) (cid:125)
I

−

BTX

The last two terms above therefore cancel. Using then trace (ABC) = trace (CAB) we obtain

E(B) = trace

(cid:16)

XXT (cid:16)

I

BBT(cid:17)(cid:17)

−

Hence the objective becomes

E(B) = (N

−

(cid:104)
trace (S)

1)

−

(cid:16)

SBBT(cid:17)(cid:105)

trace

(15.2.5)

(15.2.6)

(15.2.7)

where S is the sample covariance matrix of the data1. Since we assumed the data is zero mean, this is

S =

1

N

1

−

XXT =

1

N

1

−

N
(cid:88)

n=1

xn(xn)T

More generally, for non-zero mean data, we have

S =

1

N

1

−

N
(cid:88)

(xn

n=1

−

m)(xn

m)T,

−

m =

1
N

N
(cid:88)

n=1

xn

(15.2.8)

(15.2.9)

To minimise equation (15.2.7) under the constraint BTB = I we use a set of Lagrange multipliers L, so that
the objective is to minimize

trace

−

(cid:16)

SBBT(cid:17)

+ trace

(cid:16)

(cid:16)

L

BTB

(cid:17)(cid:17)

I

−

(15.2.10)

1 and the trace (S) term). Since the constraint is symmetric, we can
(neglecting the constant prefactor N
assume that L is also symmetric. Diﬀerentiating with respect to B and equating to zero we obtain that at
the optimum

−

SB = BL

(15.2.11)

We need to ﬁnd matrices B and L that satisfy this equation. One solution is given when L is diagonal, in
which case this is a form of eigen-equation and the columns of B are the corresponding eigenvectors of S. In
this case, trace (cid:0)SBBT(cid:1) = trace (L), which is the sum of the eigenvalues corresponding to the eigenvectors
forming B. For this eigen solution, therefore

1

N

1

−

E(B) =

trace (L) + trace (S) =

−

M
(cid:88)

−

i=1

λi + const.

(15.2.12)

1Here we use the unbiased sample covariance, simply because this is standard in the literature. If we were to replace this
with the sample covariance as deﬁned in chapter(8), the only change required is to replace N − 1 by N throughout, which has
no eﬀect on the form of the solutions found by PCA.

DRAFT April 24, 2015

317

Principal Components Analysis

Figure 15.2: Projection of two dimensional data using
one dimensional PCA. Plotted are the original dat-
apoints x (larger rings) and their reconstructions ˜x
(small dots) using 1 dimensional PCA. The lines rep-
resent the orthogonal projection of the original data-
point onto the ﬁrst eigenvector. The arrows are the
two eigenvectors scaled by the square root of their cor-
responding eigenvalues. The data has been centred to
have zero mean. For each ‘high dimensional’ datapoint
x, the ‘low dimensional’ representation y is given in
this case by the distance (possibly negative) from the
origin along the ﬁrst eigenvector direction to the cor-
responding orthogonal projection point.

Since we wish to minimise E(B), we therefore deﬁne the basis using the eigenvectors with largest corre-
sponding eigenvalues.
λ2, . . ., the squared error is then given by, from
equation (15.2.7)

If we order the eigenvalues λ1

≥

1

N

1

−

E(B) = trace (S)

trace (L) =

−

D
(cid:88)

i=1

λi

−

M
(cid:88)

i=1

λi =

D
(cid:88)

λi

i=M +1

(15.2.13)

Whilst the solution to this eigen-problem is unique, this only serves to deﬁne the solution subspace since
one may rotate and scale B and Y such that the value of the squared loss is exactly the same (since the
least squares objective depends only on the product BY). The justiﬁcation for choosing the non-rotated
eigen solution is given by the additional requirement that the basis (columns of B) correspond to directions
of maximal variance, as explained in section(15.2.2).

15.2.2 Maximum variance criterion

To break the invariance of least squares projection with respect to rotations and rescaling, we need an
additional criterion. One such is given by ﬁrst searching for the single direction b such that the variance of
the data projected onto this direction is maximal amongst all possible such projections. This makes sense
since we are looking for ‘interesting’ directions along which the data varies a lot. Using equation (15.2.4)
for a single vector b we have

yn =

(cid:88)

bixn
i

i

(15.2.14)

The projection of a datapoint onto a direction b is bTxn for a unit length vector b. Hence the sum of
squared projections is

(cid:88)

(cid:16)

bTxn(cid:17)2

= bT

n

(cid:34)

(cid:88)

n

(cid:35)

xn (xn)T

b = (N

1)bTSb

−

(15.2.15)

the squared projection becomes λ(N

Ignoring constants, this is the negative of equation (15.2.7) for a single basis vector b. Since, optimally, b is
an eigenvector with Sb = λb,
1). Hence the optimal single b which
maximises the projection variance is given by the eigenvector corresponding to the largest eigenvalue of S.
Under the criterion that the next optimal direction b(2) should be orthonormal to the ﬁrst, one can readily
show that b(2) is given by the second largest eigenvector, and so on. This explains why, despite the squared
loss equation (15.2.7) being invariant with respect to arbitrary rotation (and scaling) of the basis vectors,
the ones given by the eigen-decomposition have the additional property that they correspond to directions
of maximal variance. These maximal variance directions found by PCA are called the principal directions.

−

@@

15.2.3 PCA algorithm

The routine for PCA is presented in algorithm(15.1). In the notation of y = Fx, the projection matrix
F corresponds to ET. Similarly for the reconstruction equation (15.2.1), the coordinate yn corresponds to

318

DRAFT April 24, 2015

−2−10123−2−1.5−1−0.500.511.52Principal Components Analysis

Algorithm 15.1 Principal Components Analysis to form an M -dimensional approximation of a dataset
xn, n = 1, . . . , N

{

1: Find the D

m =

×

1
N

, with dim (xn) = D.
}
1 sample mean vector and D

N
(cid:88)

n=1

xn, S =

1

N

1

−

N
(cid:88)

(xn

n=1

D covariance matrix

m)(xn

m)T

−

×

−

2: Find the eigenvectors e1, . . . , eD of the covariance matrix S, sorted so that the eigenvalue of ei is larger than

ej for i < j. Form the matrix E = [e1, . . . , eM ].

3: The lower dimensional representation of each data point xn is given by

yn = ET(xn

m)

−

4: The approximate reconstruction of the original datapoint xn is

xn

≈

m + Eyn

5: The total squared error over all the training data made by the approximation is

N
(cid:88)

n=1

(xn

−

˜xn)2 = (N

D
(cid:88)

λj

j=M +1

1)

−

@@

where λM +1 . . .

λD are the eigenvalues discarded in the projection.

(15.2.16)

(15.2.17)

(15.2.18)

ETxn and bi corresponds to ei. The PCA reconstructions are orthogonal projections of the data onto the
subspace spanned by the eigenvectors corresponding to the M largest eigenvalues of the covariance matrix,
see ﬁg(15.2).

×

Example 15.1 (Reducing the dimension of digits). We have 892 examples of handwritten 5’s, where each
28 real-values pixels, see ﬁg(15.3). Each image matrix is stacked to form a 784
image consists of 28
892 dimensional data matrix X. The covariance matrix of this data has
dimensional vector, giving a 784
eigenvalue spectrum as plotted in ﬁg(15.4), where we plot only the 100 largest eigenvalues. Note how after
around 40 components, the mean squared reconstruction error is small, indicating that the data lies close
to a 40 dimensional linear subspace. The eigenvalues are computed using pca.m. The reconstructions using
diﬀerent numbers of eigenvectors (100, 30 and 5) are plotted in ﬁg(15.3). Note how using only a small
number of eigenvectors, the reconstruction more closely resembles the mean image.

×

Example 15.2 (Eigenfaces). In ﬁg(15.5) we present example images for which we wish to ﬁnd a lower
dimensional representation. Using PCA the mean and ﬁrst 48 ‘eigenfaces’ are presented along with recon-

Figure 15.3: Top row : a selection of the
digit 5 taken from the database of 892 ex-
amples. Plotted beneath each digit is the
reconstruction using 100, 30 and 5 eigen-
vectors (from top to bottom). Note how
the reconstructions for fewer eigenvectors
express less variability from each other, and
resemble more a mean 5 digit.

DRAFT April 24, 2015

319

structions of the original data using these eigenfaces, see ﬁg(15.6). The PCA respresentation was found
using the SVD techniques, as described in section(15.3.2).

Principal Components Analysis

15.2.4 PCA and nearest neighbours classiﬁcation

In nearest neighbours classiﬁcation, chapter(14), we need to compute the distance between datapoints. For
high-dimensional data computing the squared Euclidean distance between vectors can be expensive, and
also sensitive to noise. It is therefore often useful to project the data to a lower dimensional representation
ﬁrst. For example, in making a classiﬁer to distinguish between the digit 1 and the digit 7, example(14.1),
we can form a lower dimensional representation and use this as a more robust representation of the data.
To do so we ﬁrst ignore the class label to make a dataset of 1200 training points. Each of the training points
xn is then projected to a lower dimensional PCA representation yn. Subsequently, any distance calculations
(xa

yb)2. To justify this, consider

xb)2 are replaced by (ya

−

−

(xa

−

xb)T(xa

xb)

−

(Eya + m

Eyb

m)T(Eya + m

≈
= (ya
= (ya

−
−
yb)TETE(ya
−
yb)
yb)T(ya

−

−

−

yb)

Eyb

m)

−

−

(15.2.19)

where the last equality is due to the orthonormality of eigenvectors, ETE = I.

Using 19 principal components (see example(15.3) as to why this number was chosen) and the nearest
neighbour rule to classify 1’s and 7’s gave a test-set error of 14 in 600 examples, compared to 18 from
the standard method on the non-projected data. A plausible explanation for this improvement is that the
new PCA representation of the data is more robust since only the ‘interesting’ directions in the space are
retained, with low variance directions discarded.

Example 15.3 (Finding the best PCA dimension). There are 600 examples of the digit 1 and 600 examples
of the digit 7. We will use half the data for training and the other half for testing. The 600 training examples
were further split into a training set of 400 examples and a separate validation set of 200 examples. PCA
was used to reduce the dimensionality of the inputs, and then nearest neighbours used to classify the 200
validation examples. Diﬀerent reduced dimensions were investigated and, based on the validation results,
19 was selected as the optimal number of PCA components retained, see ﬁg(15.7). The independent test
error on 600 independent examples using 19 dimensions is 14.

15.2.5 Comments on PCA

The ‘intrinsic’ dimension of data

How many dimensions should the linear subspace have? From equation (15.2.13), the reconstruction error
is proportional to the sum of the discarded eigenvalues.
If we plot the eigenvalue spectrum (the set of
eigenvalues ordered by decreasing value), we might hope to see a few large values and many small values. If
the data does lie close to an M dimensional linear subspace, we would see M large eigenvalues with the rest

Figure 15.4: The digits data consists of 892 examples of the digit
5, each image being represented by a 784 dimensional vector.
Plotted are 100 largest eigenvalues (scaled so that the largest
eigenvalue is 1) of the sample covariance matrix.

320

DRAFT April 24, 2015

02040608010000.20.40.60.81eigenvalue numbereigenvalueHigh Dimensional Data

Figure 15.5: 100 training images. Each image consists of 92
×
112 = 10304 greyscale pixels. The train data is scaled so that,
represented as an image, the components of each image sum to 1.
10−5.
The average value of each pixel across all images is 9.70
This is a subset of the 400 images in the full Olivetti Research
Face Database.

×

(a): SVD reconstruc-
Figure 15.6:
tion of the images in ﬁg(15.5) using
a combination of the 49 eigen-images.
(b): The eigen-images are found us-
ing SVD of the images in ﬁg(15.5)
and taking the mean and 48 eigenvec-
tors with largest corresponding eigen-
value. The images corresponding to
the largest eigenvalues are contained
in the ﬁrst row, and the next 7 in the
row below, etc. The root mean square
10−5,
reconstruction error is 1.121
a small improvement over PLSA (see
ﬁg(15.16)).

×

(a)

(b)

being very small. This gives an indication of the number of degrees of freedom in the data, or the intrinsic
dimensionality. Directions corresponding to the small eigenvalues are then interpreted as ‘noise’.

Non-linear dimension reduction

In PCA we are presupposing that the data lies close to a linear subspace. Is this really a good description?
More generally, we would expect data to lie on low dimensional non-linear subspace. Also, data is often
clustered – examples of handwritten 4’s look similar to each other and form a cluster, separate from the 8’s
cluster. Nevertheless, since linear dimension reduction is computationally relatively straightforward, this is
one of the most common dimensionality reduction techniques.

15.3 High Dimensional Data

D matrix is O (cid:0)D3(cid:1). You
The computational complexity of computing the eigen-decomposition of a D
might be wondering therefore how it is possible to perform PCA on high dimensional data. For example, if
106 square matrix.
we have 500 images each of 1000
It would appear to be a signiﬁcant computational challenge to ﬁnd the eigen-decomposition of this matrix
directly. In this case, however, since there are only 500 such vectors, the number of non-zero eigenvalues
cannot exceed 500. One can exploit this fact to reduce the O (cid:0)D3(cid:1) complexity of the naive approach, as
described below.

1000 = 106 pixels, the covariance matrix will be a 106

×

×

×

DRAFT April 24, 2015

321

High Dimensional Data

Figure 15.7: Finding the optimal PCA dimension to use for clas-
sifying hand-written digits using nearest neighbours. 400 train-
ing examples are used, and the validation error plotted on 200
further examples. Based on the validation error, we see that a
dimension of 19 is reasonable.

15.3.1 Eigen-decomposition for N < D

First note that for zero mean data, the sample covariance matrix can be expressed as

[S]ij =

1

N

1

−

N
(cid:88)

n=1

i xn
xn
j

In matrix notation this can be written

1

XXT

S =

N
where the D

−
×
X = (cid:2)x1, . . . , xN (cid:3)

1
N matrix X contains all the data vectors:

(15.3.1)

(15.3.2)

(15.3.3)

Since the eigenvectors of a matrix M are equal to those of γM for scalar γ, one can consider more simply
the eigenvectors of XXT. Writing the D
N
diagonal matrix Λ, the eigen-decomposition of the scaled covariance S satisﬁes

N matrix of eigenvectors as E and the eigenvalues as an N

×

×

⇒

XXTE = EΛ

XTXXTE = XTEΛ

XTX ˜E = ˜EΛ
(15.3.4)
where we deﬁned ˜E = XTE. The ﬁnal expression above represents the eigenvector equation for XTX.
N so that calculating the eigen-decomposition takes O (cid:0)N 3(cid:1) operations,
This is a matrix of dimensions N
compared with O (cid:0)D3(cid:1) operations in the original high-dimensional space. We can therefore calculate the
eigenvectors ˜E and eigenvalues Λ of this matrix more easily. Once found, we use the fact that the eigenvalues
of S are given by the diagonal entries of Λ and the eigenvectors by

⇒

×

E = X ˜EΛ−1

(15.3.5)

15.3.2 PCA via Singular value decomposition

An alternative to using an eigen-decomposition routine to ﬁnd the PCA solution is to make use of the
Singular Value Decomposition (SVD) of an D

N dimensional matrix X. This is given by

X = UDVT

×

(15.3.6)

where UTU = ID and VTV = IN and D is a D
N diagonal matrix of the (positive) singular values. We
assume that the decomposition has ordered the singular values so that the upper left diagonal element of D
contains the largest singular value. The matrix XXT can then be written as

×

XXT = UDVTVDTUT

= U ˜DUT

(15.3.7)

@@

≡

DDT is a D

where ˜D
D diagonal matrix with the N squared singular values on the ﬁrst diagonal
entries, and zero elsewhere. Since U ˜DUT is in the form of an eigen-decomposition, the PCA solution is
equivalently given by performing the SVD decomposition of X, for which the eigenvectors are then given by
U, and corresponding eigenvalues by the square of the singular values.

×

Equation(15.3.6) shows that PCA is a form of matrix decomposition method:

X = UDVT

UM DM VT
M

≈

where UM , DM , VM correspond to taking only the ﬁrst M singular values of the full matrices.

(15.3.8)

322

DRAFT April 24, 2015

020406080100024681012number of eigenvaluesnumber of errorsLatent Semantic Analysis

Figure 15.8: (Top) Document data for a dictionary containing 10 words and 2000 documents. Black indicates
that a word was present in a document. The data consists of two distinct topics and a random background
topic. The ﬁrst topic contains two sub-topics which diﬀer only in their usage of the ﬁrst two words, ‘inﬂuenza’
and ‘ﬂu’. (Bottom) The projections of each datapoint onto the two principal components.

15.4 Latent Semantic Analysis

In the document analysis literature PCA is also called Latent Semantic Analysis and is concerned with
analysing a set of N documents. Each document is represented by a vector

xn = (xn

1 , . . . , xn

D)T

(15.4.1)

of word occurrences. For example the ﬁrst element xn
1 might count how many times the word ‘cat’ appears
2 the number of occurrences of ‘dog’, etc. This bag of words 2 is formed by ﬁrst choosing
in document n, xn
a dictionary of D words. The vector element xn
is the (possibly normalised) number of occurrences of the
i
word i in the document n. Typically D will be large, of the order 106, and x will be very sparse since any
document contains only a small fraction of the available words in the dictionary. Using (cid:93)i,n to represent the
number of times that term i occurs in document n, the term-frequency is deﬁned as

tfn

i ≡

(cid:93)i,n
i (cid:93)i,n

(cid:80)

(15.4.2)

An issue with this representation is that frequently occurring words such as ‘the’ will dominate. To counter
this one can measure how unique a term i is by seeing how many documents contain the term, and deﬁne
the inverse-document frequency

idfi

log

≡

N
number of documents that contain term i

(15.4.3)

An alternative to the above term-frequency representation is the term frequency–inverse document frequency
(TF-IDF) representation which is given by

i = tfn
xn

i ×

idfi

(15.4.4)

which gives high weight to terms that appear often in a document, but rarely amongst documents.

Given a set of documents
The whole document database is represented by the so-called term-document matrix

, the aim in LSA is to form a lower dimensional representation of each document.

D

X = (cid:2)x1, . . . , xN (cid:3)

(15.4.5)

which has dimension D
N , see for example ﬁg(15.8), with entries typically deﬁned by either the term-
frequency, or TF-IDF representation. An interpretation of PCA in this case is that the principal directions
deﬁne ‘topics’. PCA is arguably suboptimal for document analysis since we would expect the presence
of a latent topic to contribute only positive counts to the data. A related version of PCA in which the
decomposition is constrained to have positive elements is called PLSA, and discussed in section(15.6).

×

2More generally one can consider term-counts, in which terms can be single words, or sets of words, or even sub-words.

DRAFT April 24, 2015

323

200400600800100012001400160018002000246810200400600800100012001400160018002000123Latent Semantic Analysis

Example 15.4 (Latent Topic). We have a small dictionary containing the words inﬂuenza, ﬂu, headache,
nose, temperature, bed, cat, dog, rabbit, pet. The database contains a large number of articles that discuss
ailments, and articles which seem to talk about the eﬀects of inﬂuenza, in addition to some background
documents that are not speciﬁc to ailments; other documents also discuss pet related issues. Some of the
more formal documents exclusively use the term inﬂuenza, whereas the other more ‘tabloid’ documents use
the informal term ﬂu. Each document is represented by a 10-dimensional vector in which element i of that
vector is set to 1 if word i occurs in the document, and 0 otherwise. The data is represented in ﬁg(15.8).
The data is generated using the artiﬁcial mechanism described in demoLSI.m.

The result of using PCA on this data is represented in ﬁg(15.9) where we plot the eigenvectors, scaled by
their eigenvalue. To aid interpretability, we do not use the bias term c in equation (15.2.1). The ﬁrst
eigenvector groups all the ‘ailment’ words together, the second the ‘pet’ words and the third deals with
the diﬀerent usage of the terms inﬂuenza and ﬂu. Note that unlike clustering models (see section(20.3) for
example) in PCA (and related methods such as PLSA) a datapoint can in principle be constructed from
many basis vectors, so that a document could represent a mixture of diﬀerent topics.

Rescaling

In LSA it is common to scale the transformation so that the projected vectors have approximately unit
covariance (assuming centred data). Using

y = √N

−

1D−1

M UT

M x

the covariance of the projections is obtained from

UM D−1

M = D−1

M UT

M UDDTUTUM D−1

M ≈

1

(cid:88)

N

1

−

n

yn (yn)T = D−1

M UT
M

(cid:88)

n

(cid:124)

xn (xn)T

(cid:123)(cid:122)
XXT

(cid:125)

Given y, the approximate reconstruction ˜x is

˜x =

1
√N

−

1

UM DM y

(15.4.6)

I

(15.4.7)

The Euclidean distance between two points xa and xb is then approximately

(cid:16)

˜xa, ˜xb(cid:17)

d

=

1

N

1

−

(cid:16)

ya

−

yb(cid:17)T

DM UT

M UM DM

(cid:16)

ya

yb(cid:17)

−

≈

N

(cid:16)

ya

−

1

1

−

yb(cid:17)T

D2
M

(cid:16)

ya

yb(cid:17)

−

It is common to ignore the D2
the projected space to be the Euclidean distance between the y vectors.

M term (and 1/ (N

−

1) factor), and to consider a measure of dissimilarity in

15.4.1 Information retrieval

Consider a large collection of documents from the web, creating a database
. Our interest it to ﬁnd
the most similar document to a speciﬁed query document. Using a bag-of-words style representation for
document n, xn, and similarly for the query document, x∗ we address this task by ﬁrst deﬁning a measure
of dissimilarity between documents, for example

D

d(xn, xm) = (xn

xm)T (xn

xm)

−

−

One then searches for the document that minimises this dissimilarity:

nopt = argmin

n

d(xn, x∗)

324

(15.4.8)

(15.4.9)

DRAFT April 24, 2015

PCA With Missing Data

Figure 15.9: Hinton diagram of the eigenvector matrix E where each eigen-
vector column is scaled by the corresponding eigenvalue. The dark shaded
squares indicates positive and light shaded squares negative values (the area
of each square corresponds to the magnitude), showing that there are only
a few large eigenvalues. Note that the overall sign of any eigenvector is
irrelevant. The ﬁrst eigenvector corresponds to a topic in which the words
inﬂuenza, ﬂu, headache, nose, temperature, bed are prevalent. The second
eigenvector denotes the ‘pet’ topic words. The third eigenvector shows that
there is negative correlation between the occurrence of inﬂuenza and ﬂu.
The interpretation of the eigenvectors as ‘topics’ can be awkward since the
basis eigenvectors are by deﬁnition orthogonal. Contrast this basis with
that found using PSLA with two components, ﬁg(15.14).

and returns document xnopt as the result of the search query. The squared diﬀerence between two documents
can also be written

x(cid:48)(cid:1)T (cid:0)x

(cid:0)x

−

−

x(cid:48)(cid:1) = xTx + x(cid:48)Tx(cid:48)

2xTx(cid:48)

−

If, as is commonly done, the bag-of-words representations are scaled to have unit length,

ˆx =

x
√xTx

so that ˆxTˆx = 1, the distance is

(cid:0)ˆx

−

ˆx(cid:48)(cid:1)T (cid:0)ˆx

−

ˆx(cid:48)(cid:1) = 2

(cid:16)

1

ˆxTˆx(cid:48)(cid:17)

−

and one may equivalently consider the cosine similarity

s(ˆx, ˆx(cid:48)) = ˆxTˆx(cid:48) = cos (θ)

where θ is the angle between the unit vectors ˆx and ˆx(cid:48), ﬁg(15.10).

(15.4.10)

(15.4.11)

(15.4.12)

(15.4.13)

A diﬃculty with using a bag-of-words representation is that the representation will have mostly zeros.
Hence diﬀerences may be due to ‘noise’ rather than any real similarity between the query and database
document. LSA helps alleviate this problem somewhat by using a lower dimensional representation y of
the high-dimensional x. The y capture the main variations in the data and are less sensitive to random
uncorrelated noise. Using the dissimilarity deﬁned in terms of the lower dimensional y is therefore more
robust and likely to result in the retrieval of more useful documents.

Example 15.5. Continuing the Inﬂuenza example, someone who uploads a query document which uses the
term ‘ﬂu’ might also be interested in documents about ‘inﬂuenza’. However, the search query term ‘ﬂu’ does
not contain the word ‘inﬂuenza’, so how can one retrieve such documents? Since the ﬁrst component using
PCA (LSA) groups all ‘inﬂuenza’ terms together, if we use only the ﬁrst component of the representation y
to compare documents, this will retrieve documents independent of whether the term ‘ﬂu’ or ‘inﬂuenza’ is
used.

15.5 PCA With Missing Data

When values of the data matrix X are missing, the standard PCA algorithm as described cannot be applied.
Unfortunately, there is no ‘quick ﬁx’ PCA solution when some of the xn
i are missing and more complex

DRAFT April 24, 2015

325

1234567891010987654321PCA With Missing Data

(a): Two bag-of-word vectors. The Euclidean distance
Figure 15.10:
between the two is large. (b): Normalised vectors. The Euclidean distance
is now related directly to the angle between the vectors. In this case two
documents which have the same relative frequency of words will both have
the same dissimilarity, even though the number of occurrences of the words
is diﬀerent.

(a)

(b)

numerical procedures need to invoked. A simple approach in this case is to require the squared reconstruction
error to be small only for the existing elements of X. That is3

E(B, Y) =

N
(cid:88)

D
(cid:88)

n=1

i=1


xn

i −

γn
i


2

(cid:88)

j bj
yn

i



j

where γn
to ym

i = 1 if the ith entry of the nth vector is available, and is zero otherwise.

k , we ﬁnd that the optimal weights satisfy

i ym
γm

j bj

i bk

i =

(cid:88)

i,j

i xm
γm

i bk
i

(cid:88)

i

(15.5.1)

Diﬀerentiating with respect

@@

(15.5.2)

@@

One then substitutes this expression into the squared error, and minimises the error with respect to B under
the orthonormality constraint. An alternative iterative optimisation procedure is as follows: First select a
random D

M matrix ˆB. Then iterate until convergence the following two steps:

×

Optimize Y for ﬁxed B

E( ˆB, Y) =

N
(cid:88)

D
(cid:88)

n=1

i=1


xn

i −

γn
i


2

(cid:88)

yn
j

ˆbj
i



j

(15.5.3)

For ﬁxed ˆB the above E( ˆB, Y) is a quadratic function of the matrix Y, which can be optimised
directly. By diﬀerentiating and equating to zero, one obtains the ﬁxed point condition

(cid:88)

γn
i

i

(cid:32)

xn
i −

(cid:88)

l

(cid:33)

yn
l

ˆbl
i

ˆbk
i = 0

Deﬁning

y(n)(cid:105)
(cid:104)

= yl
n,

l

(cid:104)

M(n)(cid:105)

kl

(cid:88)

=

ˆbl
i

ˆbk
i γn
i ,

i

[cn]k =

(cid:88)

i

i xn
γn
i

ˆbk
i ,

in matrix notation, we then have a set of linear systems:

c(n) = M(n)y(n),

n = 1, . . . , N

(15.5.4)

(15.5.5)

(15.5.6)

One may solve each linear system for yn using Gaussian elimination4. It can be that one or more of
the above linear systems is underdetermined– this can occur when there are less observed values in
the nth data column of X than there are components M . In this case one may use the pseudo-inverse
to provide a minimal length solution.

Optimize B for ﬁxed Y

One now freezes ˆY and considers the function

2

E(B, ˆY) =


xn

γn
i

N
(cid:88)

D
(cid:88)

n=1

i=1

(cid:88)

j bj
ˆyn

i



j

(15.5.7)

i −

3For simplicity we assume that there is no mean term included.
4One can avoid explicit matrix inversion by using the \ operator in MATLAB

326

DRAFT April 24, 2015

PCA With Missing Data

Figure 15.11: Top: original data matrix X. Black is
missing, white present. The data is constructed from
a set of only 5 basis vectors. Middle : X with missing
data (80% sparsity). Bottom : reconstruction found
using svdm.m, SVD for missing data. This problem is
essentially easy since, despite there being many miss-
ing elements, the data is indeed constructed from a
model for which SVD is appropriate. Such techniques
have application in collaborative ﬁltering and recom-
mender systems where one wishes to ‘ﬁll in’ missing
values in a matrix.

For ﬁxed ˆY the above expression is quadratic in the matrix B, which can again be optimised using
linear algebra. This corresponds to solving a set of linear systems for the ith row of B:

m(i) = F(i)b(i)

where
m(i)(cid:105)
(cid:104)

k

(cid:88)

=

i xn
γn

i ˆyn
k ,

n

F(i)(cid:105)
(cid:104)

kj

(cid:88)

=

i ˆyn
γn

j ˆyn
k

n

Mathematically, this is b(i) = F(i)−1

m(i).

(15.5.8)

(15.5.9)

In this manner one is guaranteed to iteratively decrease the value of the squared error loss until a minimum
is reached. This technique is implemented in svdm.m – see also ﬁg(15.11). Eﬃcient techniques based on
updating the solution as a new column of X arrives one at a time (‘online’ updating) are also available, see
for example [52].

15.5.1 Finding the principal directions

For the missing data case the basis B found using the above technique is based only on minimising the
squared reconstruction error and therefore does not necessarily satisfy the maximal variance (or principal
directions) criterion, namely that the columns of B point along the eigen-directions. For a given B, Y with
approximate decomposition X
BY we can return a new orthonormal basis U by performing SVD on the
≈
completed data, BY = USVT to return an orthonormal basis U.

An alternative is to explicitly transform the solution B. Forming the SVD of B,

B = UDVT

the principal directions ˜B are given by

˜B = BVD−1

(15.5.10)

(15.5.11)

If the D
make the above well deﬁned, one may append D with the columns of the identity:

M matrix B is non-square M < D, then the matrix D will be non-square and non-invertible. To

×

D(cid:48) = [D, IM +1, . . . , ID]

(15.5.12)

where IK is the Kth column of the identity matrix, and we use D(cid:48) in place of D in equation (15.5.11) above.

DRAFT April 24, 2015

327

501001502002503002040608010050100150200250300204060801005010015020025030020406080100Matrix Decomposition Methods

Figure 15.12: (a): Under-complete representation. There are too few basis
vectors to represent the datapoints exactly. (b): Over-complete represen-
tation. There are too many basis vectors to form a unique representation
of a datapoint in terms of a linear combination of the basis vectors.

(a)

(b)

15.5.2 Collaborative ﬁltering using PCA with missing data

A database contains a set of vectors, each describing the ﬁlm ratings for a user in the database. The entry
in the vector xn speciﬁes the rating the user n gives to the ith ﬁlm. The matrix X = (cid:2)x1, . . . , xN (cid:3)
xn
i
contains the ratings for all the N users and has many missing values since any single user will only have
given a rating for a small selection of the possible D ﬁlms. In a practical example one might have D = 104
ﬁlms and N = 106 users. For any user n the task is to predict reasonable values for the missing entries of
their rating vector xn, thereby providing a suggestion as to which ﬁlms they might like to view. Viewed
as a missing data problem, one can ﬁt B and Y using svdm.m as above. Given B and Y we can form a
reconstruction on all the entries of X, by using

˜X = BY

(15.5.13)

giving therefore a prediction for the missing values.

15.6 Matrix Decomposition Methods

Given a data matrix X for which each column represents a datapoint, an approximate matrix decomposition
is of the form X
BY into a basis matrix B and weight (or coordinate) matrix Y. Symbolically, matrix
decompositions are of the form

≈









(cid:124)

X : Data

(cid:123)(cid:122)
D×N









(cid:125)

≈









(cid:124)









(cid:125)





(cid:124)

B : Basis

(cid:123)(cid:122)
D×M

Y : Weights/Components

(cid:123)(cid:122)
M ×N





(cid:125)

(15.6.1)

By considering the SVD of the data matrix, we see that PCA is in this class.
In this section we will
consider some further common matrix decomposition methods. The form of the decomposition we have
previously considered has been under-complete, although over-complete decompositions are also of interest,
as described below.

Under-complete decompositions

When M < D, there are fewer basis vectors than dimensions, ﬁg(15.12a). The matrix B is then called ‘tall’
or ‘thin’. In this case the matrix Y forms a lower dimensional approximate representation of the data X,
PCA being a classic example.

Over-complete decompositions

For M > D the basis is over-complete, there being more basis vectors than dimensions, ﬁg(15.12b). In such
cases additional constraints are placed on either the basis or components. For example, one might require
that only a small number of the large number of available basis vectors is used to form the representation
for any given x. Other popular constraints are that the basis vectors themselves should be sparse. Such
sparse-representations are common in theoretical neurobiology where issues of energy eﬃciency, rapidity of
processing and robustness are of interest[230, 168, 268].

328

DRAFT April 24, 2015

Matrix Decomposition Methods

Below we discuss some popular constrained matrix factorisation methods, in particular including positivity
constraints on both the basis vectors and the components.

15.6.1 Probabilistic latent semantic analysis

Consider two objects, x and y, where dom(x) =
(xn, yn) , n = 1, . . . , N
. We have a count matrix with elements Cij which describes the number of times
{
the joint state x = i, y = j was observed in the dataset. We can transform this count matrix into a frequency
matrix p with elements

1, . . . , J
{

and a dataset

, dom(y) =

1, . . . , I

}

}

{

}

p(x = i, y = j) =

Cij
ij Cij

(cid:80)

(15.6.2)

Our interest is to ﬁnd a decomposition of this frequency matrix of the form in ﬁg(15.13a)

p(x = i, y = j)
(cid:123)(cid:122)
(cid:125)
(cid:124)
Xij

(cid:88)

≈

k

z = k)
˜p(x = i
|
(cid:125)
(cid:123)(cid:122)
(cid:124)
Bik

˜p(y = j
(cid:124)

z = k)˜p(z = k)
|
(cid:125)

(cid:123)(cid:122)
Ykj

≡

˜p(x = i, y = j)

(15.6.3)

where all quantities ˜p are distributions. This is then a form of matrix decomposition into a positive basis B
and positive coordinates Y. This has the interpretation of discovering latent features/topics z that describe
the joint behaviour of x and y. After training, the term ˜p(z) can be used to rank the basis vectors or ‘topics’
(the columns on B) in terms of importance, if desired. Note that PLSA is not a sequential solver in the
sense that the optimal two basis vectors solution is not generally equivalent to the taking the highest ranked
(according to ˜p(z)) columns of the three basis vector solution – see ﬁg(15.14).

An EM style training algorithm

In order to ﬁnd the approximate decomposition we ﬁrst need a measure of diﬀerence between the matrix
with elements pij and the approximation with elements ˜pij. Since all elements are bounded between 0 and 1
and sum to 1, we may interpret p as a joint probability and ˜p as an approximation to this. For probabilities,
a useful measure of discrepancy is the Kullback-Leibler divergence

˜p) =
KL(p
|

log p
(cid:104)

(cid:105)p − (cid:104)

log ˜p

(cid:105)p

(15.6.4)

Since p is ﬁxed, minimising the Kullback-Leibler divergence with respect to the approximation ˜p is equivalent
to maximising the ‘likelihood’ term

log ˜p

(cid:104)

(cid:105)p. This is

(cid:88)

x,y

p(x, y) log ˜p(x, y)

@@ where the general notation (cid:80)

Z implies summation over all states of the variables

derive an EM style algorithm to learn ˜p(x

KL(q(z

˜p(z
x, y)
|

x, y)) =
|

|

(cid:88)

q(z

z

Using

˜p(z

x, y) =
|

˜p(x, y, z)
˜p(x, y)

z), ˜p(y
|

z) and ˜p(z). To do this, consider
|
x, y) log q(z
|

x, y) log ˜p(z
|

x, y)
|

x, y)
|

(cid:88)

q(z

−

≥

z

and rearranging, this gives the bound,

log ˜p(x, y)

(cid:88)

≥ −

z

q(z

x, y) log q(z
|

x, y) +
|

(cid:88)

z

q(z

x, y) log ˜p(z, x, y)
|

Plugging this into the ‘likelihood’ term above, we have the bound

Z

0

(15.6.5)

. It’s convenient to

(15.6.6)

(15.6.7)

(15.6.8)

(cid:88)

x,y

p(x, y) log ˜p(x, y)

(cid:88)

x,y

≥ −

(cid:88)

p(x, y)

q(z

x, y) log q(z
|

x, y)
|

z
(cid:88)

x,y

+

p(x, y)

(cid:88)

z

q(z

|

x, y) [log ˜p(x

z) + log ˜p(y
|

z) + log ˜p(z)]
|

(15.6.9)

DRAFT April 24, 2015

329

Matrix Decomposition Methods

y

x

y

z

(b)

Figure 15.13: (a): Joint PLSA. (b): Conditional PLSA.

z

(a)

x

M-step

For ﬁxed ˜p(x
|

z), ˜p(y

(cid:88)

x,y

p(x, y)

(cid:88)

z

z), the contribution to the bound from ˜p(z) is
|

q(z

x, y) log ˜p(z)
|

It is straightforward to see that the optimal setting of ˜p(z) is

˜p(z) =

(cid:88)

x,y

q(z

|

x, y)p(x, y)

(15.6.10)

(15.6.11)

(cid:16)(cid:80)

x,y q(z

x, y)p(x, y)
|

(cid:17)

˜p(z)
|

. Similarly, for ﬁxed ˜p(y

z), ˜p(z),
|

since equation (15.6.10) is, up to a constant, KL
the contribution to the bound from ˜p(x

z) is
|

(cid:88)

x,y

p(x, y)

(cid:88)

z

q(z

x, y) log ˜p(y
|

z)
|

Therefore, optimally

z)

˜p(x
|

∝

and similarly,

˜p(y

z)
|

∝

(cid:88)

y

(cid:88)

x

E-step

p(x, y)q(z

x, y)
|

p(x, y)q(z

x, y)
|

The optimal setting for the q distribution at each iteration is

x, y) = ˜p(z

q(z

|

x, y)
|

which is ﬁxed throughout the M-step.

(15.6.12)

(15.6.13)

(15.6.14)

(15.6.15)

The ‘likelihood’ equation (15.6.5) is guaranteed to increase (and the Kullback-Leibler divergence equation
(15.6.4) decrease) under iterating between the E and M-steps, since the method is analogous to an EM pro-
cedure. The procedure is given in algorithm(15.2) and a demonstration is in demoPLSA.m. Generalisations,
such as using simpler q distributions, (corresponding to generalised EM procedures) are immediate based
on modifying the above derivation.

Example 15.6 (PLSA for documents). We repeated the analysis of the toy document data in ﬁg(15.8),
now using PLSA. As we see from ﬁg(15.14), the basis found is interpretable and intuitive. Also, the
‘projections’ onto these basis vectors is also reasonable, and corresponds to what we would expect. The
limited expressibility of the two-basis model causes the two ‘inﬂuenza’ and ‘ﬂu’ terms to be placed in the
same basis vector and each basis vector clearly represents two non-overlapping topics. Note that there is
no requirement in general for the topics to be non-overlapping; this just happens to be the case for this
example only. For the richer three-basis vector model, the model distinguishes clearly two similar topics,
diﬀering in only a single word.

330

DRAFT April 24, 2015

Matrix Decomposition Methods

Algorithm 15.2 PLSA: Given a frequency matrix p(x = i, y = j), return a decomposition (cid:80)
k)˜p(y = j

k ˜p(x = i
|

z =

z = k)˜p(z = k). See plsa.m
|

z).

|

1: Initialise ˜p(z), ˜p(x
z), ˜p(y
|
2: while Not Converged do
3:

Set q(z
x, y) = ˜p(z
|
Set ˜p(x
z)
|
Set ˜p(y
z)
5:
|
6: end while
7: Set ˜p(z) = (cid:80)

(cid:80)
(cid:80)

∝
∝

4:

x,y p(x, y)q(z

x, y)

|

|

x, y)
y p(x, y)q(z
x p(x, y)q(z

x, y)
|
x, y)
|

(cid:46) E-step
(cid:46) M-Steps

Algorithm 15.3 Conditional PLSA: Given a frequency matrix p(x = i
y = j), return a decomposition
|
(cid:80)
y = j). See plsaCond.m
|
y).

z = k)˜p(z = k
k ˜p(x = i
|

|

z), ˜p(z
|

1: Initialise ˜p(x
2: while Not Converged do
3:

Set q(z
x, y) = ˜p(z
|
Set ˜p(x
z)
|
Set ˜p(z
y)
5:
|
6: end while

x, y)
|
y p(x
x p(x

(cid:80)
(cid:80)

∝
∝

4:

y)q(z
|
y)q(z
|

x, y)
|
x, y)
|

Conditional PLSA

In some cases it is more natural to consider a conditional frequency matrix

p(x = i

y = j)
|

and seek an approximate decomposition

y = j)
p(x = i
|
(cid:125)
(cid:123)(cid:122)
(cid:124)
Xij

(cid:88)

≈

k

˜p(x = i
|
(cid:123)(cid:122)
(cid:124)
Bik

z = k)
(cid:125)

y = j)
˜p(z = k
|
(cid:124)
(cid:123)(cid:122)
(cid:125)
Ykj

as depicted in ﬁg(15.13b). Deriving an EM style algorithm for this is straightforward, exercise(15.9), and is
presented in algorithm(15.3).

Example 15.7 (Discovering the basis). A set of images is given in ﬁg(15.15a). These were created by ﬁrst
deﬁning 4 base images ﬁg(15.15b). Each base image is positive and scaled so that the sum of the pixels is
unity, (cid:80)
z = k) = 1, where k = 1, . . . , 4 and x indexes the pixels, see ﬁg(15.15). We then summed
i p(x = i
|
each of these images using a randomly chosen positive set of 4 weights (under the constraint that the weights
y = j) and j indexes the training image. This
sum to 1) to generate a training image with elements p(x = i
|
was repeated 144 times to form the full train set, ﬁg(15.15a). The task is, given only the training set images,
to reconstruct the basis from which the images were formed. We assume that we know the correct number
of base images, namely 4. The results of using conditional PLSA on this task are presented in ﬁg(15.15c)
and using SVD in ﬁg(15.15d). In this case PLSA ﬁnds the correct ‘natural’ basis, corresponding to the way
the images were generated. The eigenbasis is better in terms of mean squared reconstruction error of the
training images, but in this case does not correspond to the constraints under which the data was generated.

Example 15.8 (Eigenfaces verus PLSA faces). We return to example(15.2) and now rerun the experiment,
seeking a PLSA basis, rather than an eigenbases. In ﬁg(15.16) we show the reconstruction of the original
100 images and the corresponding PLSA basis. The reconstruction error is necessarily higher for the PLSA
solution since the optimal mean squared error solution is given by PCA (PLSA has more constraints on the
form of the solution than PCA). However, the basis found by PLSA is arguably more interpretable since

DRAFT April 24, 2015

331

(cid:46) E-step
(cid:46) M-Steps

(15.6.16)

(15.6.17)

Matrix Decomposition Methods

(a)

(b)

(c)

(b):
Figure 15.14: PLSA for the document data in ﬁg(15.8).
Hinton diagram for three basis vectors. (c): The projections for the three basis vectors case. The solution
is quite satisfactory since the ﬁrst 1000 documents are clearly considered to be from the similar ‘ailments’
topics, the next 500 from some other non-speciﬁc ‘background’ topic, and the last 500 from a separate ‘pet’
topic.

(a): Hinton diagram for two basis vectors.

(a)

(b)

(c)

(d)

(a): Training data, consisting of a positive (convex) combination of the base images.

(b):
Figure 15.15:
The chosen base images from which the training data is derived. (c): Basis learned using conditional PLSA
(d): Eigenbasis (sometimes
on the training data. This is virtually indistinguishable from the true basis.
called ‘eigenfaces’).

the positive features are positively summed to produce an image. Because of this, the PLSA basis vectors
tend to be rather sparse.

15.6.2 Extensions and variations

PLSA and Latent Dirichlet Allocation

++

An alternative viewpoint of PLSA is as a form of generative model. We will phrase this in the same language
as Latent Dirichlet Allocation, section(20.6.1), since the two models are very similar. We will describe a
generative process for document n. The document is described by a distribution over K latent topics πn
k = 1. These topic distributions will form parameters of the model. Document n
with 0
is a list of words which we generate as follows. For word position w in the document, we ﬁrst draw a topic
k . Given the topic for this word position, we now generate a word vn
n) = πn
zw for this word from p(zw = k
w
from the word distribution for topic k

πn
k ≤

1, (cid:80)L

k=1 πn

≤

|

p(vn

zn
w = k) = θi|k
w = i
|

332

(15.6.18)

DRAFT April 24, 2015

121098765432112310987654321200400600800100012001400160018002000123Matrix Decomposition Methods

(a): Conditional
Figure 15.16:
PLSA reconstruction of the images in
ﬁg(15.5) using a positive convex com-
bination of the 49 positive base im-
ages in (b). The root mean square
10−5.
reconstruction error is 1.391
The base images tend to be more ‘lo-
calised’ than the corresponding eigen-
images ﬁg(15.6b). Here one sees local
structure such as foreheads, chins, etc.

×

(a)

(b)

We repeat this process for each of the Wn word positions in the document. This describes a distribution

p(vn

1 , . . . , vn

Wn, zn

1 , . . . , zn

Wn) =

W n
(cid:89)

w=1

p(vn
w|

w)p(zn
zn
w|

n)

The marginal distribution over the observed data is then

p(vn

1 , . . . , vn

Wn) =

W n
(cid:89)

(cid:88)

w=1

zn
w

p(vn
w|

w)p(zn
zn
w|

n)

Since the zn

w is simply a summation variable, we can simplify the above to

p(vn

1 , . . . , vn

Wn) =

W n
(cid:89)

(cid:88)

w=1

k

p(vn
w|

k)p(k

n)
|

(15.6.19)

(15.6.20)

(15.6.21)

Consider a speciﬁc word i in our dictionary of D words. Each time word i appears in the document, we will
obtain a factor (cid:80)

n). An equivalent expression for the likelihood is therefore
|

k p(i
|

k)p(k

p(vn

1 , . . . , vn

Wn) =

D
(cid:89)

(cid:32)

(cid:88)

i=1

k

(cid:33)fi,n

p(vn

k)p(k
w = i
|

n)

|

(15.6.22)

where fi,n is the frequency of occurrence of word i in document n. Assuming that we would generate each
document in the same way, the likelihood of a collection of documents is the product of the above likelihoods,
one for each document. The parameters of this model are then the word distributions for each topic, θi|k and
the topic distributions for each document πn
k . Maximising the likelihood with respect to these parameters
is equivalent to maximising the log likelihood which is

N
(cid:88)

D
(cid:88)

n=1

i=1

fi,n log

K
(cid:88)

k=1

p(i

k)p(k
|

n)
|

(15.6.23)

k) = θk|i and p(k
where p(i
|
counts p(i, n) = f(i, n)/ (cid:80)

n) = πn
|
i,n fi,n, this is PLSA with the factorisation

k . This is then exactly in the form of PLSA. If we normalise the frequency

p(i, n)

(cid:88)

≈

k

˜p(i

k)˜p(k
|

n)
|

(15.6.24)

for which a very eﬃcient EM style algorithm is available, see exercise(15.10). The diﬀerence with Latent
Dirichlet Allocation is that, rather than ˜p(k
n) being a parameter, these topic distributions are rather
sampled from a Dirichlet distribution. An alternative probabilistic interpretation of PLSA can be made via
Poisson processes[58].

|

DRAFT April 24, 2015

333

Matrix Decomposition Methods

Non-negative matrix factorisation

Non-negative Matrix Factorisation (NMF) considers a decomposition in which both the basis and weight
matrices have non-negative entries; this can be considered as constrained Factor Analysis[233]. Closely
related works are [185] which is a generalisation of PLSA (with no requirement that the basis or components
sum to unity). In all cases EM-style training algorithms exist, although their convergence can be slow. We
will encounter similar models in the discussion on Independent Component Analysis, section(21.6).

Gradient based training

EM style algorithms are easy to derive and implement but can exhibit poor convergence. Gradient based
methods to simultaneously optimize with respect to the basis and the components have been developed, but
require a parameterisation that ensures positivity of the solutions[233].

Array decompositions

It is straightforward to extend PLSA to the decomposition of multidimensional arrays. For example

p(s, t, u)

(cid:88)

≈

v,w

˜p(s, t, u

|

v, w)˜p(v, w) =

(cid:88)

v,w

u, v)˜p(u
˜p(s, t
|
|

w)˜p(v)˜p(w)

(15.6.25)

Such extensions require only additional bookkeeping.

15.6.3 Applications of PLSA/NMF

Modelling citations

We have a collection of research documents which cite other documents. For example, document 1 might
cite documents 3, 2, 10, etc. Given only the list of citations for each document, can we identify key research
papers and the communities that cite them? Note that this is not the same question as ﬁnding the most
cited documents – rather we want to identify documents with communities and ﬁnd their relevance for a
community.

1, . . . , D

We use the variable d
to index citations (both d and c
have the same domain, namely the index of a research article). If document d = i cites article c = j then we
set the entry of the matrix Cij = 1. If there is no citation, Cij is set to zero. We can form a ‘distribution’
over documents and citations using

to index documents and c

1, . . . , D

∈ {

∈ {

}

}

p(d = i, c = j) =

Cij
ij Cij

(cid:80)

(15.6.26)

and use PLSA to decompose this matrix into citation-topics, see the example below.

Example 15.9 (Modelling citations). The Cora corpus [201] contains an archive of around 30,000 computer
science research papers. From this archive the authors in [66] extracted the papers in the machine learning
category, consisting of 4220 documents and 38,372 citations. Using these the distribution equation (15.6.26)
was formed. The documents have additionally been categorised by hand into 7 topics: Case-based reasoning,
Genetic Algorithms, Neural Networks, Probabilistic methods, Reinforcement Learning, Rule Learning and
Theory. In [66] the joint PLSA method is ﬁtted to the data using dim (z) = 7 topics. From the trained
model the expression p(c = j
z = k) deﬁnes how authoritative paper j is according to community z = k.
|
The method discovers intuitively meaningful topics, as presented in table(15.1).

334

DRAFT April 24, 2015

Kernel PCA

(Reinforcement Learning)
Learning to predict by the methods of temporal diﬀerences. Sutton.
Neuronlike adaptive elements that can solve diﬃcult learning control problems. Barto et al.
Practical Issues in Temporal Diﬀerence Learning. Tesauro.
(Rule Learning)
Explanation-based generalization: a unifying view. Mitchell et al.
Learning internal representations by error propagation. Rumelhart et al.
Explanation-Based Learning: An Alternative View. DeJong et al.
(Neural Networks)
Learning internal representations by error propagation. Rumelhart et al.
Neural networks and the bias-variance dilemma. Geman et al.
The Cascade-Correlation learning architecture. Fahlman et al.
(Theory)
Classiﬁcation and Regression Trees. Breiman et al.
Learnability and the Vapnik-Chervonenkis dimension. Blumer et al.
Learning Quickly when Irrelevant Attributes Abound. Littlestone.
(Probabilistic Reasoning)
Probabilistic Reasoning in Intelligent Systems: Networks of Plausible Inference. Pearl.

factor 1
0.0108
0.0066
0.0065
factor 2
0.0038
0.0037
0.0036
factor 3
0.0120
0.0061
0.0049
factor 4
0.0093
0.0066
0.0055
factor 5
0.0118
0.0094 Maximum likelihood from incomplete data via the em algorithm. Dempster et al.
0.0056
factor 6
0.0157
0.0132
0.0096
factor 7
0.0063
0.0054
0.0033

Local computations with probabilities on graphical structures. Lauritzen et al.
(Genetic Algorithms)
Genetic Algorithms in Search, Optimization, and Machine Learning. Goldberg.
Adaptation in Natural and Artiﬁcial Systems. Holland.
Genetic Programming: On the Programming of Computers by Means of Natural Selection. Koza.
(Logic)
Eﬃcient induction of logic programs. Muggleton et al.
Learning logical deﬁnitions from relations. Quinlan.
Inductive Logic Programming Techniques and Applications. Lavrac et al.

Table 15.1: Highest ranked documents according to p(c
|
based on similarity to the Cora topics. Reproduced from [66].

z). The factor topic labels are manual assignments

Modelling the web

Consider a collection of websites, indexed by i. If website j points to website i, set Cij = 1, otherwise set
Cij = 0. This gives a directed graph of website-to-website links. Since a website will discuss usually only a
small number of ‘topics’ we might be able to explain why there is a link between two websites using a PLSA
decomposition. These algorithms have proved useful for internet search for example to determine the latent
topics of websites and identify the most authoritative websites. See [67] for a discussion.

Physical models

Non-negative decompositions can arise naturally in physical situations. For example, in acoustics, positive
amounts of energy combine linearly from diﬀerent signal sources to form the observed signal. If we consider
that two kinds of signals are present in an acoustic signal, say a piano and a singer then using NMF one
can learn two separate bases, one for each ‘instrument’, and then reconstruct a given signal using only one
of the bases. This means that one could potentially remove the singer from a recording, leaving only the
piano. This is analogous to reconstructing the images in ﬁg(15.15a) using say only one of the learned basis
images, see example(15.7). See [305] for a related model in acoustics.

15.7 Kernel PCA

Kernel PCA is a non-linear extension of PCA designed to discover non-linear subspace. Here we only brieﬂy
In kernel PCA, we replace each x by a
describe the approach and refer the reader to [259] for details.
‘feature’ vector ˜x
φ(x). Note that the use of ˜x here does not have the interpretation we used before as the
approximate reconstruction. Rather, the feature map φ takes a vector x and produces a higher dimensional
vector ˜x. For example we could map a two dimensional vector x = [x1, x2]T using

≡

φ(x) = (cid:2)x1, x2, x2

1, x2

2, x1x2, x3

1, . . .(cid:3)T

(15.7.1)

The idea is then to perform PCA on these higher dimensional feature vectors, subsequently mapping back
the eigenvectors to the original space x. The main challenge is to write this without explicitly computing

DRAFT April 24, 2015

335

Kernel PCA

PCA in the potentially very high dimensional feature vector space. As a reminder, in standard PCA, for
zero mean data, one forms an eigen-decomposition of the sample matrix5

S =

1
N

˜X ˜XT

For simplicity, we concentrate here on ﬁnding the ﬁrst principal component ˜e which satisﬁes

˜X ˜XT˜e = λ(cid:48)˜e

(15.7.2)

(15.7.3)

for corresponding eigenvalue λ (writing λ(cid:48) = N λ). The ‘dual’ representation is obtained by pre-multiplying
by ˜XT, so that in terms of ˜f

˜XT˜e, the standard PCA eigen-problem reduces to solving:

≡

˜XT ˜X˜f = λ(cid:48)˜f

The feature eigenvector ˜e is then recovered using

˜X˜f = λ(cid:48)˜e

We note that matrix ˜XT ˜X has elements

(cid:104)
˜XT ˜X

(cid:105)

mn

= φ(xm)Tφ(xn)

(15.7.4)

(15.7.5)

(15.7.6)

and recognise this as the scalar product between vectors. This means that the matrix is positive semideﬁnite
and we may equivalently use a covariance function kernel, see section(19.3),

(cid:104)

(cid:105)

˜XT ˜X

mn

= k(xm, xn) = Kmn

Then equation (15.7.4) can be written as

K˜f = λ(cid:48)˜f

(15.7.7)

(15.7.8)

One then solves this eigen-equation to ﬁnd the N dimensional principal dual feature vector ˜f . The projection
of the feature ˜x is given by

y = ˜xT˜e =

˜xT ˜X˜f

1
λ

(15.7.9)

More generally, for a larger number of components, the ith kernel PCA projection yi can be expressed in
terms of the kernel directly as

yi =

1
N λi

N
(cid:88)

n=1

k(x, xn) ˜f i
n

(15.7.10)

where i is the eigenvalue label and ˜f i

n is the nth component of the ith eigenvector of K.

The above derivation implicitly assumed zero mean features ˜x. Even if the original data x is zero mean,
due to the non-linear mapping, however, the features may not be zero mean. To correct for this one may
show that the only modiﬁcation required is to replace the matrix K in equation (15.7.8) above with

K(cid:48)

mn = k(xm, xn)

1
N

−

N
(cid:88)

d=1

k(xd, xn)

1
N

−

N
(cid:88)

d=1

k(xm, xd) +

1
N 2

N
(cid:88)

d=1,d(cid:48)=1

k(xd(cid:48)

, xd)

(15.7.11)

5We use the normalisation N as opposed to N − 1 just for notational convenience – in practice, there is little diﬀerence.

336

DRAFT April 24, 2015

Canonical Correlation Analysis

Finding the reconstructions

Through equation (15.7.10), the above gives a procedure for ﬁnding the KPCA projection y. However, in
many cases we would also like to have an approximate reconstruction x(cid:48) using the lower dimensional y.
This is not straightforward since the mapping from y to x is in general highly non-linear. Here we outline
a procedure for achieving this. First we ﬁnd the reconstruction ˜x∗ of the feature space vector ˜x, using

(cid:88)

˜x∗ =

yi˜ei =

(cid:88)

i

i

yi

1
λi

(cid:88)

n

˜f n
i φ(xn)

(15.7.12)

Given ˜x∗ we try to ﬁnd that point x(cid:48) in the original data space that maps to ˜x∗. This can be found by
minimising

E(x(cid:48)) = (cid:0)φ(x(cid:48))

˜x∗(cid:1)2

−

Up to negligable constants this is

E(x(cid:48)) = k(x(cid:48), x(cid:48))

2

−

(cid:88)

i

yi
λi

(cid:88)

n

˜f n
i k(xn, x(cid:48))

(15.7.13)

(15.7.14)

One then ﬁnds x(cid:48) by minimising E(x(cid:48)) numerically. The resulting procedure gives a way to form a non-linear
extension of PCA; however it is computationally relatively demanding and results in a complex optimisation
problem. See [259] for details and example applications.

15.8 Canonical Correlation Analysis

Consider variables x and y being ‘diﬀerent views’ of the same underlying object. For example x might rep-
resent a segment of video and y the corresponding audio. Given then a collection
,
}
an interesting challenge is to identify which parts of the audio and video are strongly correlated. One might
expect, for example, that the mouth region of the video is strongly correlated with the audio.

(xn, yn) , n = 1, . . . , N

{

One way to achieve this is to project each x and y to one dimension using aTx and bTy such that the
correlation between the projections is maximal. The unnormalised correlation between the projections aTx
and bTy is

aTxnbTyn = aT

(cid:88)

n

(cid:35)

xnynT

b.

(cid:34)

(cid:88)

n

Deﬁning

Sxy

1
N

≡

(cid:88)

xnynT

n

and similarly for Syx, Sxx, Syy, the normalised correlation is

aTSxyb
aTSxxa(cid:112)bTSyyb

(cid:112)

(15.8.1)

(15.8.2)

(15.8.3)

Since equation (15.8.3) is invariant with respect to recaling a and also b, we can consider the equivalent
objective

E(a, b) = aTSxyb

(15.8.4)

subject to aTSxxa = 1 and bTSyyb = 1. To ﬁnd the optimal projections a, b, under the constraints, we use
the Lagrangian,

(a, b, λa, λb)

L

aTSxyb +

≡

(cid:16)

1

λa
2

−

aTSxxa

(cid:17)

+

(cid:16)

1

λb
2

−

(cid:17)
bTSyyb

DRAFT April 24, 2015

(15.8.5)

337

Canonical Correlation Analysis

(a)

(b)

(c)

(a): Training data. The top panel contains the X matrix
Figure 15.17: Canonical Correlation Analysis.
of 100, 15 dimensional points, and the bottom the corresponding 30 dimensional Y matrix. (b): The data
in (a) was produced using X = Ah, Y = Bh where A is a 15
1 matrix. The
underlying latent h is a 1
(c): Matrices A and B learned by
CCA. Note that they are close to the true A and B up to rescaling and sign changes. See demoCCA.m.

100 dimensional randomly chosen vector.

1 matrix, and B is a 30

×

×

×

from which we obtain the zero derivative criteria

Sxyb = λaSxxa,

Syxa = λbSyyb

Hence

aTSxyb = λaaTSxxa = λa,

bTSyxa = λbbTSyyb = λb

(15.8.6)

(15.8.7)

Since aTSxyb = bTSyxa we must have λa = λb = λ at the optimum. If we assume that Syy is invertible we
can write,

b =

1
λ

S−1

yy Syxa

and use this to eliminate b in equation (15.8.6), giving

SxyS−1

yy Syxa = λ2Sxxa

which is a generalised eigen-problem. Assuming that Sxx is invertible we can equivalently write

xx SxyS−1
S−1

yy Syxa = λ2a

(15.8.8)

(15.8.9)

(15.8.10)

which is a standard eigen-problem (albeit with λ2 as the eigenvalue). Once this is solved we can ﬁnd b using
equation (15.8.8).

15.8.1 SVD formulation

It is straightforward to show that we can ﬁnd a (the optimal projection of x) by ﬁrst computing the SVD
of

− 1
xx SxyS
2

S

− 1
2
yy

(15.8.11)

in the form UDVT and extracting the maximal singular vector u1 of U (the ﬁrst column of U). Then a is
− 1
yy v1, where v1 is the ﬁrst column of V. In this way, the
optimally S
2
extension to ﬁnding M multiple directions A = (cid:2)a1, . . . , aM (cid:3) and B = (cid:2)b1, . . . , bM (cid:3) is clear – one takes the
corresponding ﬁrst M singular values accordingly. Doing so maximises the criterion

− 1
xx u1, and similarly, b is optimally S
2

trace (cid:0)ATSxyB(cid:1)
(cid:112)

(cid:112)

trace (ATSxxA)

trace (BTSyyB)

338

(15.8.12)

DRAFT April 24, 2015

2040608010051015202530training data2040608010051015051015202530−2−10123051015−2−1012true model051015202530−0.2−0.100.10.20.3051015−0.4−0.200.20.40.6learned modelExercises

This approach is taken in cca.m – see ﬁg(15.17) for a demonstration. One can also show that CCA corre-
sponds to factor analysis under a block restriction on the form of the factor loadings, see section(21.2.1).

CCA and related kernel extensions have been applied in machine learning contexts, for example to model
the correlation between images and text in order to improve image retrieval from text queries, see [138].

15.9 Summary

•

•

•

•

PCA is a classical linear dimension reduction method and assumes that the data lies close to a linear
subspace.

The PCA representation can be found by an eigen-decomposition of the data covariance matrix, or alter-
natively using an SVD decomposition of the data matrix.

More generally, PCA is special case of a matrix decomposition method. Other standard methods include
PLSA and non-negative matrix factorisation, which can be considered as constrained forms of PCA (posi-
tivity constraints).

Canonical correlation analysis attempts to ﬁnd a low dimensional representation that jointly models the
two related data spaces. CCA is a special case of the probabilistic factor analysis model.

PCA appears in many diﬀerent research communities and can be referred to diﬀerently. For example it is
also known as the Karhunen-Lo`eve decomposition and proper orthogonal decomposition.

Another important use for dimension reduction is in data visualisation. Methods such as PCA can be used
in this context, though are not designed to produce visually interpretable results. See [252] and [300] for
discussions of work in this area.

15.10 Code

pca.m: Principal components analysis
demoLSI.m: Demo of latent semantic indexing/analysis
svdm.m: Singular value decomposition with missing data
demoSVDmissing.m: Demo SVD with missing data
plsa.m: Probabilistic Latent Semantic Analysis
plsaCond.m: Conditional probabilistic latent semantic analysis
demoPLSA.m: Demo of PLSA
cca.m: Canonical correlation analysis (CCA)
demoCCA.m: Demo of canonical correlation analysis

15.11 Exercises

Exercise 15.1. As described in section(15.2), we wish to show that the optimal bias c is equal to the sample
mean of the data.

1. Explain why, when considering a bias c, and carrying out the derivation in section(15.2.1), we will

obtain, analogously to equation (15.2.1):

E(B, c) =

(cid:88)

(xn

n

c)T (cid:16)

I

−

−

BBT(cid:17)

(xn

c)

−

DRAFT April 24, 2015

(15.11.1)

339

2. Show that for a matrix M

∂
∂c

cTMc = Mc + MTc

3. Using this derivative result, show that the optimal bias c satisﬁes

(cid:16)

I

0 =

−

BBT(cid:17) (cid:88)

(xn

n

c)

−

Exercises

(15.11.2)

(15.11.3)

and hence determine that the optimal bias is given by the mean of the data, c = (cid:80)N

n=1 xn/N .

Exercise 15.2. Consider a dataset in two dimensions where the data lies on the circumference of a circle
of unit radius. What would be the eﬀect of using PCA on this dataset, in which we attempt to reduce the
dimensionality to 1? Suggest an alternative one dimensional representation of the data.
Exercise 15.3. Consider two vectors xa and xb and their corresponding PCA approximations c + (cid:80)M
and c+(cid:80)M
eigenvector ei has corresponding eigenvalue λi. Approximate (xa
of the data, and show that this is equal to (a

i=1 aiei
i=1 biei, where the eigenvectors ei, i = 1, . . . , M are mutually orthogonal and have unit length. The
xb)2 by using the PCA representations

b)2.

−

−

Exercise 15.4. Show how the solution for a to the CCA problem in equation (15.8.9) can be transformed
into the form expressed by equation (15.8.11), as claimed in the text.

Exercise 15.5. Let S be the covariance matrix of the data. The Mahalanobis distance between xa and xb
is deﬁned as

(cid:16)

xa

−

xb(cid:17)T

S−1 (cid:16)

xa

xb(cid:17)

.

−

(15.11.4)

Explain how to approximate this distance using M -dimensional PCA.

Exercise 15.6 (PCA with external inputs). In some applications, one may suspect that certain external
variables v have a strong inﬂuence on how the data x is distributed. For example, if x represents an image,
it might be that we know the lighting condition v under which the image was made – this will have a large
eﬀect on the image. It would make sense therefore to include the known lighting condition in forming a lower
dimensional representation of the image. Note that we don’t want to form a lower dimensional representation
of the joint (x, v), rather we want to form a lower dimensional representation of x alone, bearing in mind
that some of the variability observed may be due to v. We therefore assume an approximation

xn

(cid:88)

≈

j

j bj +
yn

(cid:88)

k ck
vn

k

(15.11.5)

i , i = 1, . . . , N , n = 1, . . . , N , basis vectors bj, j = 1, . . . , J and ck, k = 1, . . . , K
where the coeﬃcients yn
are to be determined. The external inputs v1, . . . , vN are given. The sum squared error loss between the xn
and their linear reconstruction equation (15.11.5) is

E =


xn

i −

(cid:88)

n,i

(cid:88)

j

j bj
yn

i −

(cid:88)

k


2

k ck
vn
i



Find the parameters (cid:8)bj, ck(cid:9) , j = 1, . . . , J, k = 1, . . . , K that minimise E.

Exercise 15.7. Consider the following 3-dimensional datapoints:

(1.3, 1.6, 2.8)(4.3,

1.4, 5.8)(

0.6, 3.7, 0.7)(

0.4, 3.2, 5.8)(3.3,

0.4, 4.3)(

0.4, 3.1, 0.9)

−

−

−

−

−

(15.11.6)

(15.11.7)

Perform Principal Components Analysis by:

1. Calculating the mean, c, of the data.

2. Calculating the covariance matrix S = 1
6

(cid:80)6

n=1 xn(xn)T

ccT of the data.

−

340

DRAFT April 24, 2015

Exercises

3. Finding the eigenvalues and eigenvectors ei of the covariance matrix.

1 (xn

2 (xn

−
c))e1 + (eT

−
2 (xn

4. You should ﬁnd that only two eigenvalues are large, and therefore that the data can be well represented
using two components only. Let e1 and e2 be the two eigenvectors with largest eigenvalues. Calculate
the two dimensional representation of each datapoint (eT

c)), n = 1, . . . , 6.

c), eT

5. Calculate the reconstruction of each datapoint c + (eT

1 (xn

−

c))e2, n = 1, . . . , 6.

−

Exercise 15.8. Show that for the missing data case, the transformed solution ˜B given in equation (15.5.11)
satisﬁes ˜BT ˜B = I.

Exercise 15.9. Consider a ‘conditional frequency matrix’

p(x = i

y = j)
|

(15.11.8)

Following section(15.6.1), show how to derive an EM style algorithm for an approximate decomposition of
this matrix in the form

p(x = i

y = j)
|

≈

(cid:88)

k

˜p(x = i
|

z = k)˜p(z = k

y = j)

|

where k = 1, . . . , Z, i = 1, . . . , X, j = 1, . . . , Y .

(15.11.9)

++

Exercise 15.10. Consider a probability matrix with elements p(i, n), 0
we wish to approximate by a factoriation

≤

p(i, n)

≤

1, (cid:80)

i,n p(i, n) = 1 that

p(i, n)

(cid:88)

≈

k

˜p(i

k)˜p(k
|

n)
|

(15.11.10)

If we ﬁnd the factors ˜p by the requirement of minimal KL(p

˜p), show that we obtain the update equations
|

˜pnew(i
|

k)

˜p(i

k)
|

∝

(cid:88)

n

(cid:80)

n)

˜p(i, n)˜p(k
k ˜p(i
|

|
k)˜p(k

n)

|

˜pnew(k

n)
|

∝

˜p(k

n)
|

(cid:88)

i

(cid:80)

˜p(i, n)˜p(k
k ˜p(i

n)
|
k)˜p(k
|

n)
|

(15.11.11)

(15.11.12)

DRAFT April 24, 2015

341

Exercises

342

DRAFT April 24, 2015

CHAPTER 16

Supervised Linear Dimension Reduction

PCA is a popular and very useful method. However, if we subsequently use the projected data in a classiﬁca-
tion problem, by not making use of the class labels of the data, we are potentially forming lower dimensional
representations that are suboptimal in terms of how well separated they are amongst the diﬀerent classes.
In this chapter we discuss some classical methods that reduce the data dimension such that the resulting
data is well separated between the classes.

16.1 Supervised Linear Projections

In cases where class
In chapter(15) we discussed dimension reduction using an unsupervised procedure.
information is available, and our ultimate interest is to reduce dimensionality for improved classiﬁcation, it
makes sense to use the available class information in forming the projection. We consider data from two
diﬀerent classes. For class 1, we have a set of N1 datapoints,

(cid:110)

1, . . . , xN1
x1
1

(cid:111)

1 =

X

and similarly for class 2, we have a set of N2 datapoints

(cid:110)

2, . . . , xN2
x1
2

(cid:111)

2 =

X

Our interest is then to ﬁnd a linear projection,

y = WTx

(16.1.1)

(16.1.2)

(16.1.3)

L, L < D, such that for two datapoints xi and xj in the same class, the distance
where dim W = D
between their projections yi and yj should be small. Conversely, for datapoints in diﬀerent classes, the
distance between their projections should be large. This may be useful for classiﬁcation purposes since for
a novel point x∗, if its projection

×

y∗ = WTx∗

(16.1.4)

is close to class 1 projected data, we would expect x∗ to belong to class 1. In forming the supervised pro-
jection, only the class discriminative parts of the data are retained, so that the procedure can be considered
a form of supervised feature extraction.

16.2 Fisher’s Linear Discriminant

We ﬁrst restrict our attention to binary class data. Also, for simplicity, we project the data down to one
dimension. The canonical variates algorithm of section(16.3) deals with the generalisations.

343

Fisher’s Linear Discriminant

(a)

(b)

Figure 16.1: The large crosses represent data from class 1, and the large circles from class 2. Their projections
onto 1 dimension are represented by their small counterparts. (a): Fisher’s Linear Discriminant Analysis.
Here there is little class overlap in the projections. (b): Unsupervised dimension reduction using Principal
Components Analysis for comparison. There is considerable class overlap in the projection. In both (a) and
(b) the one dimensional projection is the distance along the line, measured from an arbitrary chosen ﬁxed
point on the line.

Gaussian assumption

We model the data from each class with a Gaussian. That is

p(x1) =

N

(x1 m1, S1) ,

p(x2) =

(x2 m2, S2)

N

(16.2.1)

where m1 is the sample mean of class 1 data, and S1 the sample covariance; similarly for class 2. The
projections of the points from the two classes are then given by

1 = wTxn
yn
1 ,

2 = wTxn
yn
2

Because the projections are linear, the projected distributions are also Gaussian,

p(y1) =

p(y2) =

N

N

(cid:0)y1 µ1, σ2
(cid:0)y2 µ2, σ2

1

2

(cid:1) ,
(cid:1) ,

µ1 = wTm1,
µ2 = wTm2,

σ2
1 = wTS1w
2 = wTS2w
σ2

(16.2.2)

(16.2.3)

(16.2.4)

We search for a projection w such that the projected distributions have minimal overlap. This can be
µ2)2 is large. However, if
achieved if the projected Gaussian means are maximally separated, that is (µ1
1, σ2
the variances σ2
2 are also large, there could still be a large overlap still in the classes. A useful objective
function therefore is

−

(µ1
π1σ2

µ2)2
−
1 + π2σ2
2

(16.2.5)

where πi represents the fraction of the dataset in class i. In terms of the projection w, the objective equation
(16.2.5) is

wT (m1

m2) (m1
wT (π1S1 + π2S2) w

−

−

m2)T w

=

wTAw
wTBw

F (w) =

where

A = (m1

m2) (m1

−

m2)T ,

−

B = π1S1 + π2S2

The optimal w can be found by diﬀerentiating equation (16.2.6) with respect to w. This gives

∂
∂w

wTAw
wTBw

=

2
(wTBw)2

(cid:104)(cid:16)

wTBw

(cid:17)

Aw

(cid:16)

wTAw

(cid:17)

(cid:105)

Bw

−

(16.2.6)

(16.2.7)

(16.2.8)

344

DRAFT April 24, 2015

−202468−5−4−3−2−1012345−202468−5−4−3−2−1012345Canonical Variates

and therefore the zero derivative requirement is
(cid:16)

(cid:16)

(cid:17)

(cid:17)

wTBw

Aw =

wTAw

Bw

Multiplying by the inverse of B we have

B−1 (m1

m2) (m1

wTAw
wTBw
m2)T w is a scalar, the optimal projection is explicitly given by

m2)T w =

w

−

−

Since (m1

w

∝

−
B−1 (m1

m2)

−

(16.2.9)

(16.2.10)

(16.2.11)

Although the proportionality factor depends on w, we may take it to be constant since the objective function
F (w) in equation (16.2.6) is invariant to rescaling of w. We may therefore take

w = kB−1 (m1

m2)

−

It is common to rescale w to have unit length, wTw = 1, such that

k =

(cid:113)

(m1

−

1
m2)T B−2 (m1

m2)

−

(16.2.12)

(16.2.13)

An illustration of the method is given in ﬁg(16.1), which demonstrates how supervised dimension reduction
can produce lower dimensional representations more suitable for subsequent classiﬁcation than an unsuper-
vised method such as PCA.

One can also arrive at equation (16.2.12) from a diﬀerent starting objective. By treating the projection as
a regression problem y = wTx + b in which the outputs y are deﬁned as y1 and y2 for classes 1 and class 2
respectively, one may show that, for suitably chosen y1 and y2, the solution using a least squares criterion is
given by equation (16.2.12) [88, 43]. This also suggests a way to regularise LDA, see exercise(16.3). Kernel
extensions of LDA are possible, see for example [82, 264].

When the naive method breaks down

The above derivation relied on the existence of the inverse of B.
In practice, however, B may not be
invertible, and the above procedure requires modiﬁcation. A case where B is not invertible is when there
are fewer datapoints N1 + N2 than dimensions D. A related problematic case is when there are elements
of the input vectors that never vary. For example, in the hand-written digits case, the pixels at the corner
Let’s call such a pixel z. The matrix B will then have a zero entry for
edges are actually always zero.
[B]z,z (indeed the whole zth row and column of B will be zero) so that for any vector of the form

wT = (0, 0, . . . , wz, 0, 0, . . . , 0)

wTBw = 0

⇒

(16.2.14)

This shows that the denominator of Fisher’s objective can become zero, and the objective ill deﬁned. We
will address these issues in section(16.3.1).

16.3 Canonical Variates

Canonical Variates generalises Fisher’s method to projections of more than one dimension and more than
two classes. The projection of any point is given by

y = WTx

where W is a D

L matrix. Assuming that the data x from class c is Gaussian distributed,

×
(x mc, Sc)

p(x) =

N

the projections y are also Gaussian

p(y) =

(cid:16)

N

y WTmc, WTScW

(cid:17)

By analogy with equation (16.2.7), we deﬁne the following matrices:

DRAFT April 24, 2015

(16.3.1)

(16.3.2)

(16.3.3)

345

Between class Scatter Find the mean m of the whole dataset and mc, the mean of the each class c.

Canonical Variates

Form

A

≡

C
(cid:88)

c=1

Nc (mc

−

m) (mc

m)T

−

where Nc is the number of datapoints in class c, c = 1, . . . , C.

Within class Scatter Compute the covariance matrix Sc of the data for each class c. Deﬁne

B

≡

C
(cid:88)

c=1

NcSc

Assuming B is invertible (see section(16.3.1) otherwise), we can deﬁne the Cholesky factor ˜B, with

˜BT ˜B = B

A natural objective is then to maximise the Raleigh quotient

(cid:16)

trace

WT ˜B−TA ˜B−1W

(cid:17)

trace (WTW)

F (W)

≡

(16.3.4)

(16.3.5)

(16.3.6)

(16.3.7)

If we assume an orthonormality constraint on W, then we equivalently require the maximisation of

(cid:16)

WTCW

(cid:17)

trace

, subject to WTW = I

F (W)

≡

where

C

≡

1
D

˜B−TA ˜B−1

Since C is symmetric and positive semideﬁnite, it has a real eigen-decomposition

C = EΛET

(16.3.8)

(16.3.9)

(16.3.10)

where Λ = diag (λ1, λ2, . . . , λD) is diagonal with non-negative entries containing the eigenvalues, sorted by
decreasing order, λ1

. . . and ETE = I. Hence

λ2

F (W) = trace

WTEΛETW

(cid:17)

(16.3.11)

≥

≥
(cid:16)

By setting W = [e1, . . . , eL], where el is the lth eigenvector, the objective F (W) becomes the sum of the
ﬁrst L eigenvalues. This setting maximises the objective function since forming W from any other columns
of E would give a lower sum. The procedure is outlined in algorithm(16.1). Note that since A has rank C,
there can be no more than C

1 non-zero eigenvalues and corresponding directions.

−

q1

q2

Figure 16.2: Each three dimensional datapoint lies in a two-
dimensional plane, meaning that the matrix B is not full
rank, and therefore not invertible. A solution is given by
ﬁnding vectors q1, q2 that span the plane, and expressing
the Canonical Variates solution in terms of these vectors
alone.

346

DRAFT April 24, 2015

Canonical Variates

Algorithm 16.1 Canonical Variates

1: Compute the between and within class scatter matrices A, equation (16.3.4) and B, equation (16.3.5).
2: Compute the Cholesky factor ˜B of B.
3: Compute the L principal eigenvectors [e1, . . . , eL] of ˜B−TA ˜B−1.
4: Return W = [e1, . . . , eL] as the projection matrix.

(a)

(b)

(a): Canonical Variates projection of examples of handwritten digits 3(‘+’), 5(‘o’) and
Figure 16.3:
7(diamond). There are 800 examples from each digit class. Plotted are the projections down to 2 dimensions.
(b): PCA projections for comparison.

16.3.1 Dealing with the nullspace

The above derivation of Canonical Variates (and also Fisher’s LDA) requires the invertibility of the matrix
B. However, as we discussed in section(16.2), one may encounter situations where B is not invertible.
A solution is to require that W lies only in the subspace spanned by the data (that is there can be no
contribution from the nullspace). To do this we ﬁrst concatenate the training data from all classes into one
large matrix X. A basis for X can be found using, for example, the thin-SVD technique which returns an
orthonormal non-square basis matrix Q. We then require the solution W to be expressed in this basis, see
ﬁg(16.2):

W = QW(cid:48)

(16.3.12)

for some matrix W(cid:48). Substituting this in the Canonical Variates objective equation (16.3.7), we obtain

F (W(cid:48))

(cid:16)

W(cid:48)TQTAQW(cid:48)(cid:17)
trace
trace (cid:0)W(cid:48)TQTBQW(cid:48)(cid:1)

≡

(16.3.13)

This is of the same form as the standard quotient, equation (16.3.7), on replacing the between-scatter A
with

A(cid:48)

≡

QTAQ

and the within-scatter B with

B(cid:48)

≡

QTBQ

(16.3.14)

(16.3.15)

In this case B(cid:48) is guaranteed invertible since B is projected down to the basis that spans the data. One may
then carry out Canonical Variates, as in section(16.3) above, which returns the matrix W(cid:48). Transforming,
back, W is then given by equation (16.3.12). See also CanonVar.m.

DRAFT April 24, 2015

347

−0.1−0.0500.050.10.15−0.06−0.04−0.0200.020.040.060.080.10.120.14−3000−2500−2000−1500−1000−500−1000−50005001000Exercises

Example 16.1 (Using canonical variates on the digit data). We apply canonical variates to project the digit
data onto two dimensions, see ﬁg(16.3). There are 800 examples of a three, 800 examples of a ﬁve and 800
examples of a seven. Thus, overall, there are 2400 examples lying in a 784 (28
28 pixels) dimensional space.
The canonical variates projected data onto two dimensions has very little class overlap, see ﬁg(16.3a). In
comparison the projections formed from PCA, which discards the class information, displays a high degree
of class overlap. The diﬀerent scales of the canonical variates and PCA projections is due to the diﬀerent
In PCA W is unitary; in canonical variates WTBW = I,
constraints on the projection matrices W.
meaning that W will scale with the inverse square root of the largest eigenvalues of the within class scatter
matrix. Since the canonical variates objective is independent of linear scaling, W can be rescaled with an
arbitrary scalar prefactor γW, as desired.

×

16.4 Summary

Fisher’s linear discriminant seeks a scalar projection that is maximally diﬀerent for data from each of two
classes.

Canonical variates generalises Fisher’s method to multiple classes and multiple projected dimensions.

Fisher’s method and Canonical variates are related to standard eigen-problems.

•

•

•

The applicability of canonical variates depends on our assumption that a Gaussian is a good description of
the data. Clearly, if the data is multimodal, using a single Gaussian to model the data in each class is a poor
assumption. This may result in projections with a large class overlap. In principle, there is no conceptual
diﬃculty in using more complex distributions, and a more general criteria such as maximal Kullback-Leibler
divergence between projected distributions. However, such criteria typically result in diﬃcult optimisation
problems. Canonical variates is popular due to its simplicity and lack of local optima issues in constructing
the projection.

16.5 Code

CanonVar.m: Canonical Variates
demoCanonVarDigits.m: Demo for Canonical Variates

16.6 Exercises

Exercise 16.1. What happens to Fisher’s Linear Discriminant if there are less datapoints than dimensions?

Exercise 16.2. Modify demoCanonVarDigits.m to project and visualise the digits data in 3 dimensions.

Exercise 16.3. Consider N1 class 1 datapoints xn1, n1 = 1, . . . , N1 and N2 class 2 datapoints xn2, n2 =
1, . . . , N2. We deﬁne a linear predictor for the data,

y = wTx + b

(16.6.1)

with the aim to predict value y1 for data from class 1 and y2 for data from class two. A measure of the ﬁt
is given by

E(w, b
y1, y2) =
|

N1(cid:88)

(cid:16)

n1=1

y1

wTxn1 −

−

(cid:17)2
b

+

N2(cid:88)

(cid:16)

n2=1

y2

wTxn2 −

−

(cid:17)2
b

348

(16.6.2)

DRAFT April 24, 2015

Exercises

Show that by setting y1 = (N1 + N2)/N1 and y2 = (N1 + N2)/N2 the w which minimises E corresponds to
Fisher’s LDA solution. Hint: ﬁrst show that the two zero derivative conditions are

(cid:88)

(cid:16)

n1

y1

b

−

−

wTxn1

(cid:17)

+

(cid:88)

(cid:16)

n2

y2

b

−

−

wTxn2

(cid:17)

= 0

and

(cid:88)

(cid:16)

n1

y1

b

−

−

(cid:17)

wTxn1

xT

n1 +

(cid:88)

(cid:16)

n2

y2

b

−

−

(cid:17)

wTxn2

xT

n2 = 0

which can be reduced to the single equation

(cid:18)

N (m1

−

m2) =

N B +

N1N2
N

(m1

−

m2) (m1

−

(cid:19)

w

m2)T

where B is as deﬁned for LDA in the text, equation (16.2.7).

(16.6.3)

(16.6.4)

(16.6.5)

Note that this suggests a way to regularise LDA, namely by adding on a term λwTw to E(w, b
|
can be absorbed into redeﬁning equation (16.3.5) as

y1, y2). This

B(cid:48) = B + λI

(16.6.6)

That is, we increase the covariance B by an additive amount λI. The optimal regularising constant λ may
be set by cross-validation.

Exercise 16.4. Consider the digit data of 892 ﬁves digit5.mat and 1028 sevens digit7.mat. Make a
training set which consists of the ﬁrst 500 examples from each digit class. Use Canonical Variates to ﬁrst
project the data down to 10 dimensions and compute the Nearest Neighbour performance on the remaining
digits. Alternatively, use PCA to reduce the data to 10 dimensions, and compare the resulting Nearest
Neighbour classiﬁcation performance. Visualise also the 10 directions found by Canonical Variates and the
10 principal directions of PCA.

Exercise 16.5. Consider an objective function of the form

F (w)

A(w)
B(w)

≡

(16.6.7)

where A(w) and B(w) are positive functions, and our task is to maximise F (w) with respect to w. It may
be that this objective does not have a simple algebraic solution, even though A(w) and B(w) are simple
functions. We can consider an alternative objective, namely

J(w, λ) = A(w)

λB(w)

−

where λ is a constant scalar. Choose an initial point wold at random and set

λold

≡

A(wold)/B(wold)

In that case J(wold, λold) = 0. Now choose a w such that

J(w, λold) = A(w)

λoldB(w)

0

≥

−

This is certainly possible since J(wold, λold) = 0. If we can ﬁnd a w such that J(w, λold) > 0, then

A(w)

−

λoldB(w) > 0

(16.6.8)

(16.6.9)

(16.6.10)

(16.6.11)

Show that for such a w, F (w) > F (wold), and suggest an iterative optimisation procedure for objective
functions of the form F (w).

DRAFT April 24, 2015

349

Exercises

350

DRAFT April 24, 2015

CHAPTER 17

Linear Models

In this chapter we discuss some classical methods for prediction based on ﬁtting simple linear models to data.
These include standard methods such as linear and logistic regression, and also their kernelised variants.
We also discuss the popular support vector machine and related methods for ensuring good generalisation
performance.

17.1 Introduction: Fitting A Straight Line

Given training data
ﬁt is

(xn, yn) , n = 1, . . . , N
{

}

, for scalar input xn and scalar output yn, a linear regression

y(x) = a + bx

(17.1.1)

To determine the best parameters a, b, we use a measure of the discrepancy between the observed outputs
and the linear regression ﬁt such as the sum squared training error. This is also called ordinary least squares
and minimises the average vertical projection of the points y to the ﬁtted line, ﬁg(17.1a):

E(a, b) =

N
(cid:88)

n=1

[yn

−

y(xn)]2 =

N
(cid:88)

(yn

n=1

bxn)2

a

−

−

(17.1.2)

Our task is to ﬁnd the parameters a and b that minimise E(a, b). Diﬀerentiating with respect to a and b we
obtain

∂
∂a

E(a, b) =

N
(cid:88)

(yn

n=1

2
−

bxn),

a

−

−

∂
∂b

E(a, b) =

N
(cid:88)

(yn

n=1

2
−

bxn)xn

a

−

−

(17.1.3)

Dividing by N and equating to zero, the optimal parameters are given from the solution to the two linear
equations

y

(cid:104)

(cid:105) −

a

b

x
(cid:104)

(cid:105)

−

= 0,

xy

(cid:104)

(cid:105) −

a

x
(cid:104)

(cid:105) −

b (cid:10)x2(cid:11) = 0

(17.1.4)

to denote 1
N

(cid:80)N

n=1 f (xn, yn). We can readily solve the equations(17.1.4)

where we used the notation
to determine a and b:

f (x, y)
(cid:104)

(cid:105)

a =

y
(cid:104)

(cid:105) −

b

x
(cid:104)

(cid:105)

(17.1.5)

(17.1.6)

b (cid:10)x2(cid:11) =

yx
(cid:104)

(cid:105) − (cid:104)

x

(

y
(cid:104)

(cid:105)

(cid:105) −

b

)

x
(cid:104)

(cid:105)

⇒

(cid:104)(cid:10)x2(cid:11)

b

x

2(cid:105)
(cid:105)

− (cid:104)

=

xy
(cid:104)

x

y
(cid:105) (cid:104)

(cid:105)

(cid:105) − (cid:104)

351

Linear Parameter Models for Regression

(a)

(b)

Figure 17.1: Data from singbats – the number of chirps per second, versus the temperature in Fahrenheit.
(a): Straight line regression ﬁt to the singbat data.
(b): PCA ﬁt to the data. In regression we minimize
the residuals – the vertical distances from datapoints to the line. In PCA the ﬁt minimizes the orthogonal
projections to the line. Both lines go through the mean of the data.

Hence

(cid:105)

(17.1.7)

b = (cid:104)

xy
(cid:105) − (cid:104)
x2
(cid:105) − (cid:104)
(cid:104)

x
y
(cid:105) (cid:104)
2
x
(cid:105)

and a is found by substituting this value for b into equation (17.1.5).

In contrast to ordinary least squares regression, PCA from chapter(15) minimises the orthogonal projection
of y to the line and is known as orthogonal least squares – see example(17.1).

Example 17.1. In ﬁg(17.1) we plot the number of chirps c per second for singbats, versus the temperature
t in degrees Fahrenheit. A biologist believes that there is a simple relation between the number of chirps
and the temperature of the form

c = a + bt

(17.1.8)

where she needs to determine the parameters a and b. For the singbat data, the ﬁt is plotted in ﬁg(17.1a).
For comparison we plot the ﬁt from the PCA, ﬁg(17.1b), which minimises the sum of the squared orthogonal
projections from the data to the line.

17.2 Linear Parameter Models for Regression

We can generalise the above to ﬁtting linear functions of vector inputs x. For a dataset
a linear parameter regression model (LPM) is deﬁned by1

{

(xn, yn) , n = 1, . . . , N

,

}

y(x) = wTφ(x)

(17.2.1)

where φ(x) is a vector valued function of the input vector x. For example, in the case of a straight line ﬁt,
with scalar input and output, equation (17.1.1), we have

φ(x) = (1, x)T,

w = (a, b)T,

(17.2.2)

We deﬁne the train error as the sum of squared diﬀerences between the observed outputs and the predictions
under the linear model:

E(w) =

N
(cid:88)

(yn

n=1

−

wTφn)2,

where φn

φ (xn)

≡

1Note that the model is linear in the parameter w – not necessarily linear in x.

(17.2.3)

352

DRAFT April 24, 2015

607080901001106065707580859095100105110chirps per sectemperature (F)607080901001106065707580859095100105110chirps per sectemperature (F)Linear Parameter Models for Regression

Figure 17.2: Cubic polynomial ﬁt to the singbat data.

We now wish to determine the parameter vector w that minimises E(w). In terms of the components of w,
the squared error is

E(w) =

N
(cid:88)

(yn

n=1

(cid:88)

−

i

wiφn

i )(yn

(cid:88)

−

j

wjφn
j )

Diﬀerentiating with respect to wk, and equating to zero gives

N
(cid:88)

n=1

ynφn

k =

(cid:88)

wi

N
(cid:88)

i φn
φn
k

i

n=1

or, in matrix notation,

N
(cid:88)

n=1

ynφn =

N
(cid:88)

n=1

φn(φn)Tw

These are called the normal equations, for which the solution is

w =

(cid:32) N
(cid:88)

n=1

φn(φn)T

(cid:33)−1 N
(cid:88)

n=1

ynφn

(17.2.4)

(17.2.5)

(17.2.6)

(17.2.7)

Although we write the solution using matrix inversion, in practice one ﬁnds the numerical solution using
Gaussian elimination[129] since this is faster and numerically more stable.

Example 17.2 (A cubic polynomial ﬁt).

A cubic polynomial is given by

y(x) = w1 + w2x + w3x2 + w4x3

As a LPM, this can be expressed using

φ(x) = (cid:0)1, x, x2, x3(cid:1)T

(17.2.8)

(17.2.9)

The ordinary least squares solution has the form given in equation (17.2.7). The ﬁtted cubic polynomial is
plotted in ﬁg(17.2). See also demoCubicPoly.m.

Example 17.3 (Predicting return). In ﬁg(17.3) we ﬁt an LPM with vector inputs x to a scalar output y.
The vector x represents factors that are believed to aﬀect the stock price of a company, with the stock price

DRAFT April 24, 2015

353

607080901001106065707580859095100105110chirps per sectemperature (F)Linear Parameter Models for Regression

Figure 17.3: Predicting stock return using a linear LPM. The top
ﬁve panels present the inputs x1, . . . , x5 for 20 train days (blue)
and 5 test days (red). The corresponding train output (stock re-
turn) y for each day is given in the bottom panel. The predictions
y21, . . . , y25 are the predictions based on yt = (cid:80)
i wixit with w
trained using ordinary least squares. With a regularisation term
0.01wTw, the OLS learned w is [1.42, 0.62, 0.27,
0.26, 1.54]. De-
spite the simplicity of these models, their application in the ﬁ-
nance industry is widespread, with signiﬁcant investment made
on ﬁnding factors x that may be indicative of future return. See
demoLPMhedge.m.

−

return given by the scalar y. A hedge fund manager believes that the returns may be linearly related to the
factors:

yt =

5
(cid:88)

i=1

wixit

(17.2.10)

and wishes to ﬁt the parameters w in order to use the model to predict future stock returns. This is
straightforward using ordinary least squares, this being simply an LPM with a linear φ function. See
ﬁg(17.3) for an example. Such models also form the basis for more complex models in ﬁnance, see for
example [209].

17.2.1 Vector outputs

It is straightforward to generalise the above framework to vector outputs y. Using a separate weight vector
wi for each output component yi, we have

yi(x) = wT

i φ(x)

The mathematics follows similarly to before, and we may deﬁne a train error per output as

E(wi) =

(cid:88)

(cid:16)

n

yn
i −

i φn(cid:17)2
wT

,

E(w) =

(cid:88)

i

E(wi)

(17.2.11)

(17.2.12)

Since the training error decomposes into individual terms, one for each output, the weights for each output
can be trained separately. In other words, the problem decomposes into a set of independent scalar output
problems. In case the parameters w are tied or shared amongst the outputs, the training is still straight-
forward since the objective function remains linear in the parameters, and this is left as an exercise for the
interested reader.

17.2.2 Regularisation

For most purposes, our interest is not just to ﬁnd the function that best ﬁts the train data but one that
that will generalise well. To control the complexity of the ﬁtted function we may add an extra regularising
term R(w) to the train error to penalise rapid changes in the output

E(cid:48)(w) = E(w) + λR(w)

354

(17.2.13)

DRAFT April 24, 2015

051015202500.510510152025−0.500.50510152025−0.500.50510152025−2.5−2−1.50510152025−1010510152025024Linear Parameter Models for Regression

(a)

(b)

Figure 17.4: (a): A set of ﬁxed-width (α = 1) radial basis functions, exp
, with the centres
mi evenly spaced. By taking a linear combination of these functions we can form a ﬂexible function class.
(b): The
are the training points, and the + are the validation points. The solid line is the correct
underlying function sin(10x) which is corrupted with a small amount of additive noise to form the train
data. The dashed line is the best predictor based on the validation set.

×

−

−

(cid:16)

(cid:0)x

1
2

mi(cid:1)2(cid:17)

where λ is a scalar amount that adjust the strength of the regularisation term. For example a regularising
term that can be added to equation (17.2.3) is

R(w) =

N
(cid:88)

N
(cid:88)

e

n=1

n(cid:48)=1

(cid:16)

xn−xn(cid:48) (cid:17)2 (cid:104)

−γ

(cid:105)2

y(xn(cid:48)

)

y(xn)

−

(17.2.14)

(cid:104)
y(xn)
(cid:16)

γ

xn

(cid:105)2
)

y(xn(cid:48)
xn(cid:48)(cid:17)2(cid:19)

−

−

penalises large diﬀerences in the outputs corresponding to two inputs. The

has the eﬀect of weighting more heavily terms for which two input vectors xn

are close together; γ is a ﬁxed length-scale parameter. Since y = wTφ(x), expression (17.2.14) can

The factor
(cid:18)

−

factor exp
and xn(cid:48)
be written as

wTRw

where

R

≡

N
(cid:88)

n=1

N
(cid:88)

−γ
e
n(cid:48)=1

xn−xn(cid:48) (cid:17)2 (cid:16)
(cid:16)

φn

−

φn(cid:48)(cid:17) (cid:16)

φn

φn(cid:48)(cid:17)T

−

The regularised train error is then

E(cid:48)(w) =

N
(cid:88)

(yn

n=1

−

wTφn)2 + λwTRw

(17.2.15)

(17.2.16)

(17.2.17)

By diﬀerentiating the regularised training error and equating to zero, we ﬁnd the optimal w is given by

w =

(cid:32)

(cid:88)

n

φn(φn)T + λR

(cid:33)−1 N
(cid:88)

ynφn

n=1

In practice it is common to use a regulariser that penalises the sum squared length of the weights

R(w) = wTw =

(cid:88)

w2
i

i

(17.2.18)

(17.2.19)

which corresponds to setting R = I. This is known as ridge regression. Regularising pararameters such as
λ, γ may be determined using a validation set, section(13.2.2).

DRAFT April 24, 2015

355

00.20.40.60.8100.10.20.30.40.50.60.70.80.91x00.20.40.60.81−1.5−1−0.500.511.5Linear Parameter Models for Regression

17.2.3 Radial basis functions

A popular LPM is given by the non-linear function φ(x) with components

φi(x) = exp

(cid:18)

1
2α2

−

(cid:0)x

−

mi(cid:1)2(cid:19)

(17.2.20)

These basis functions are bump shaped, with the centre of bump i being given by mi and the width by α.
An example is given in ﬁg(17.4) in which several RBFs are plotted with diﬀerent centres. In LPM regression
we can then use a linear combination of these bumps to ﬁt the data. One can apply the same approach
using vector inputs. For vector x and centre m, the radial basis function depends on the distance between
x and the centre m, giving a bump in input space, ﬁg(17.6).

Example 17.4 (Setting α). Consider ﬁtting the data in ﬁg(17.4b) using 16 radial basis functions uniformly
spread over the input space, with width parameter α and regularising term λwTw. The generalisation
performance on the test data depends heavily on the width and regularising parameter λ. In order to ﬁnd
reasonable values for these parameters we may use a validation set. For simplicity we set the regularisation
parameter to λ = 0.0001 and use the validation set to determine a suitable α.
In ﬁg(17.5) we plot the
validation error E(w) as a function of α, choosing then the α with the lowest validation error. The predictions
for this optimal α are also given in ﬁg(17.4b).

A curse of dimensionality

If the data has non-trivial behaviour over some input region, then we need to cover this region input space
fairly densely with bump type functions. In the above case, we used 16 basis functions for a one dimensional
input space. In 2 dimensions if we wish to cover each dimension to the same discretisation level, we would
need 162 = 256 basis functions. Similarly, for 10 dimensions we would need 1610
1012 functions. To
ﬁt such an LPM would require solving a linear system in more than 1012 variables. This explosion in the
number of basis functions with the input dimension is a ‘curse of dimensionality’.

≈

A possible remedy is to make the basis functions very broad so that each covers more of the high dimensional
space. However, this will mean a lack of ﬂexibility of the ﬁtted function since it is constrained to be smooth.
Another approach is to place basis functions centred on the training input points and add some more basis
functions randomly placed close to the training inputs. The rationale behind this is that when we come to
do prediction, we will most likely see novel x that are close to the training points – we do not need to make
accurate predictions over all the space. A further approach is to make the positions of the basis functions
adaptive, allowing them to be moved around in the space to minimise the error. This approach is used in
neural network models[42]. An alternative is to reexpress the problem of ﬁtting an LPM by reparameterising
the problem, as discussed below.

Figure 17.5: The validation error as a function of the basis func-
tion width for the validation data in ﬁg(17.4b) and RBFs in
ﬁg(17.4a). Based on the validation error, the optimal setting
of the basis function width parameter is α = 0.25.

356

DRAFT April 24, 2015

00.20.40.60.81012345678αvalidation errorThe Dual Representation and Kernels

(a): The output of an RBF
Figure 17.6:
(cid:0)x
/α2). Here m1 =
function exp(
(0, 0.3)T and α = 0.25.
(b): The combined
output for two RBFs with m1 as above and
m2 = (0.5,

m1(cid:1)2

0.5)T.

−

−

1
2

−

(a)

(b)

17.3 The Dual Representation and Kernels

Consider a set of training data with inputs,
1, . . . , N . For an LPM of the form

=

{

X

xn, n = 1, . . . , N

}

and corresponding outputs yn, n =

f (x) = wTx

(17.3.1)

our interest is to ﬁnd the ‘best ﬁt’ parameters w. We assume that we have found an optimal parameter w∗.
. That is, x⊥ is in the nullspace
The nullspace of
if

are those x⊥ which are orthogonal to all the inputs in

X

X

(cid:16)

x⊥(cid:17)T

xn = 0,

for all n = 1, . . . , N

If we then consider the vector w∗ with an additional component in the nullspace,

(cid:16)

w∗ + x⊥(cid:17)T

xn = wT

∗ xn

(17.3.2)

(17.3.3)

This means that adding a contribution to w∗ outside of the space spanned by
, has no eﬀect on the
predictions on the train data. If the training criterion depends only on how well the LPM predicts the train
data, there is therefore no need to consider contributions to w from outside of
. That is, without loss of
generality we may consider the representation

X

X

w =

N
(cid:88)

m=1

amxm

(17.3.4)

The parameters a = (a1, . . . , aN ) are called the dual parameters. We can then write the output of the LPM
directly in terms of the dual parameters,

wTxn =

N
(cid:88)

m=1

am (xm)T xn

(17.3.5)

More generally, for a vector function φ(x), the solution will lie in the space spanned by φ(x1), . . . , φ(xN ),

w =

N
(cid:88)

n=1

anφ (xn)

and we may write

wTφ(xn) =

N
(cid:88)

m=1

amφ (xm)T φ (xn) =

N
(cid:88)

m=1

amK (xm, xn)

where we have deﬁned a kernel function

K (xm, xn)

≡

φ (xm)T φ (xn)

[K]m,n

≡

In matrix form, the output of the LPM on a training input x is then

wTφ(xn) = [Ka]n = aTkn

(17.3.6)

(17.3.7)

(17.3.8)

(17.3.9)

where kn is the nth column of the Gram matrix K. By construction, the Gram matrix must be positive
semideﬁnite, and the kernel a covariance function, see section(19.3).

DRAFT April 24, 2015

357

−1−0.500.51−1−0.8−0.6−0.4−0.200.20.40.60.8100.51x(1)x(2)−1−0.500.51−1−0.500.5100.511.5x(1)x(2)Linear Parameter Models for Classiﬁcation

17.3.1 Regression in the dual-space

For ordinary least squares regression, using equation (17.3.9), we have a train error

E(a) =

N
(cid:88)

(cid:16)

n=1

yn

−

aTkn(cid:17)2

(17.3.10)

Equation(17.3.10) is analogous to the standard regression equation (17.2.3) on interchanging a for w and
kn for φ(xn). Similarly, the regularisation term can be expressed as

wTw =

N
(cid:88)

n,m=1

anamφ (xn) φ (xm) = aTKa

By direct analogy the optimal solution for a is therefore

a =

(cid:32) N
(cid:88)

n=1

kn (kn)T + λK

(cid:33)−1 N
(cid:88)

ynkn

n=1

We can express the above solution more conveniently by ﬁrst writing

a =

(cid:32) N
(cid:88)

n=1

K−1kn (kn)T + λI

(cid:33)−1 N
(cid:88)

n=1

ynK−1kn

(17.3.11)

(17.3.12)

(17.3.13)

Since kn is the nth column of K then K−1kn is the nth column of the identity matrix. With a little
manipulation, we can therefore rewrite equation (17.3.13) more simply as

a = (K + λI)−1 y

(17.3.14)

where y is the vector with components formed from the training inputs y1, . . . , yN . Using this, the prediction
for a new input x∗ is given by

y(x∗) = kT

∗ (K + λI)−1 y

where the vector k∗ has components

[k∗]m = K (x∗, xm)

(17.3.15)

(17.3.16)

This dual space solution shows that predictions can be expressed purely in terms of the kernel K (x, x(cid:48)).
This means that we may dispense with deﬁning the vector functions φ(x) and deﬁne a kernel function
directly. This approach is also used in Gaussian Processes, chapter(19) and enables us to use eﬀectively
very large (even inﬁnite) dimensional vectors φ without ever explicitly needing to compute them. Note that
the Gram matrix K has dimension N
N , which means that the computational complexity of performing
the matrix inversion in equation (17.3.16) is O (cid:0)N 3(cid:1). For moderate to large N (greater than 5000), this
will be prohibitively expensive, and numerical approximations are required. This is in contrast to the
computational complexity of solving the normal equations in the original weight space viewpoint which
is O
. The dual parameterisation therefore helps us with the curse of dimensionality since the
complexity of learning in the dual parameterisation scales cubically with the number of training points –
not cubically with the dimension of the φ vector.

dim (φ)3(cid:17)

×

(cid:16)

17.4 Linear Parameter Models for Classiﬁcation

In a binary classiﬁcation problem we are given train data,
c
0, 1
}
to class 1 using

, where the targets
. Inspired by the LPM regression model, we can assign the probability that a novel input x belongs

(xn, cn), n = 1 . . . , N
{

∈ {

=

D

}

x) = f (xTw)
p(c = 1
|

358

(17.4.1)

DRAFT April 24, 2015

Linear Parameter Models for Classiﬁcation

Figure 17.7: The logistic sigmoid function σβ(x) = 1/(1 + e−βx).
The parameter β determines the steepness of the sigmoid. The
full (blue) line is for β = 1 and the dashed (red) for β = 10.
As β
, the logistic sigmoid tends to a Heaviside step func-
tion. The dotted curve (magenta) is the error function (probit)
0.5 (1 + erf(λx)) for λ = √π/4, which closely matches the stan-
dard logistic sigmoid with β = 1.

→ ∞

f (x)

1. In the statistics literature, f (x) is termed a mean function – the inverse function
where 0
f −1(x) is the link function2. Two popular choices for the function f (x) are the logit and probit functions.
The logit is given by

≤

≤

f (x) =

ex
1 + ex =

1
1 + e−x

(17.4.2)

which is also called the logistic sigmoid and written σ(x), ﬁg(17.7). The scaled version is deﬁned as

σβ(x) = σ(βx)

(17.4.3)

A closely related model is probit regression which uses in place of the logistic sigmoid the cumulative
distribution of the standard normal distribution

f (x) =

1
√2π

(cid:90) x

−∞

e− 1

2 t2

dt =

1
2

(1 + erf(x))

where the standard error function,

is

erf(x)

2
√π

≡

(cid:90) x

0

e−t2

dt

(17.4.4)

(17.4.5)

The shape of the probit and logistic functions are similar under rescaling, see ﬁg(17.7). We focus below on
the logit function.

17.4.1 Logistic regression

Logistic regression corresponds to the model

x) = σ(b + xTw)
p(c = 1
|

where b is a scalar, and w is a vector.

The decision boundary

(17.4.6)

The decision boundary is deﬁned as that set of x for which p(c = 1
the hyperplane

|

x) = p(c = 0

|

x) = 0.5. This is given by

b + xTw = 0

(17.4.7)

On the side of the hyperplane for which b + xTw > 0, inputs x are classiﬁed as 1’s, and on the other side
they are classiﬁed as 0’s. The ‘bias’ parameter b simply shifts the decision boundary by a constant amount.
The orientation of the decision boundary is determined by w, the normal to the hyperplane, see ﬁg(17.8).
To clarify the geometric interpretation, let x be a point on the decision boundary and consider a new point
x∗ = x + w⊥, where w⊥ is a vector perpendicular to w, so that wTw⊥ = 0. Then

b + wTx∗ = b + wT (cid:16)

x + w⊥(cid:17)

= b + wTx + wTw⊥ = b + wTx = 0

(17.4.8)

Thus if x is on the decision boundary, so is x plus any vector perpendicular to w. In D dimensions, the space
of vectors that are perpendicular to w occupy a D
1 dimensional hyperplane. For example, if the data is
−
two dimensional, the decision boundary is a one dimensional hyperplane, a line, as depicted in ﬁg(17.8).

2These models are part of the ‘generalised linear models’ class in the statistics literature which includes the regression model

and classiﬁcation models as special cases.

DRAFT April 24, 2015

359

−5−4−3−2−101234500.10.20.30.40.50.60.70.80.91Linear Parameter Models for Classiﬁcation

w

x) = 0.5 (solid line).
Figure 17.8: The decision boundary p(c = 1
|
For two dimensional data, the decision boundary is a line. If all
the train data for class 1 (ﬁlled circles) lie on one side of the line,
and for class 0 (open circles) on the other, the data is said to
be linearly separable. More generally, w deﬁnes the normal to a
hyperplane and data is linearly separable if data from each of the
two classes lies on opposite sides of the hyperplane.

Deﬁnition 17.1 (Linear separability). If all the train data for class 1 lies on one side of a hyperplane, and
for class 0 on the other, the data is said to be linearly separable.

For D dimensional data, provided there are no more than D training points, then these are linearly separable
if they are linearly independent. To see this, let cn = +1 if xn is in class 1, and cn =
1 if xn is in class 0.
For the data to be linearly separable we require

−

wTxn + b =

(cid:15)ncn,

n = 1, . . . , N

(17.4.9)

@@

(cid:15)n are arbitrary positive constants. The above equations state that each input

where
the decision boundary. If there are N = D datapoints, the above can be written in matrix form as

is the correct side of

@@
@@

Xw + b =

d

(17.4.10)

@@

where X is a square matrix whose nth column contains xn and [b]i = b.
Provided that X is invertible the solution is

The vector d has element [d]n = (cid:15)ncn.

@@

w = X−1 (cid:0)

d

b(cid:1)

−

(17.4.11)

@@

The bias b can be set arbitrarily. This shows that provided the xn are linearly independent, we can always
ﬁnd a hyperplane that linearly separates the data. Provided the data are not-collinear (all occupying the
1 dimensional subspace) the additional bias enables D + 1 arbitrarily labelled points to be linearly
same D
separated in D dimensions.

−

An example dataset that is not linearly separable is given by the following four training points and class
labels

([0, 0], 0), ([0, 1], 1), ([1, 0], 1), ([1, 1], 0)
}
{

(17.4.12)

This data represents the XOR function, and is plotted in ﬁg(17.9). This function is not linearly separable
since no straight line has all inputs from one class on one side and the other class on the other. Classifying
data which is not linearly separable can only be achieved using a non-linear decision boundary. It might be
that data is non-linearly separable in the original data space. An alternative is to map the data to a higher
dimension using a non-linear vector function; this creates a set of non-linearly dependent high-dimensional
vectors which can then be separated using a high-dimensional hyperplane. We discuss this in section(17.4.5).

The perceptron

We brieﬂy describe here the perceptron, an important early model in the ﬁeld of AI, see for example [42].
The perceptron deterministically assigns x to class 1 if b + wTx

0, and to class 0 otherwise. That is

≥

x) = θ(b + xTw)
p(c = 1
|

(17.4.13)

Figure 17.9: The XOR problem. This is not linearly separable.

360

DRAFT April 24, 2015

Linear Parameter Models for Classiﬁcation

where the step function is deﬁned as
(cid:26) 1 x > 0
0
0 x

θ(x) =

≤
If we consider the logistic regression model

x) = σβ
p(c = 1
|

(cid:16)

b + xTw

(cid:17)

and take the limit β

, we have the perceptron like classiﬁer

→ ∞
b + xTw > 0
1
0.5 b + xTw = 0
b + xTw < 0
0






x) =
p(c = 1
|

(17.4.14)

(17.4.15)

(17.4.16)

The only diﬀerence between this ‘probabilistic perceptron’ and the standard perceptron is in the technical
deﬁnition of the value of the step function at 0. The perceptron may therefore essentially be viewed as a
limiting case of logistic regression.

Maximum likelihood training

For this class discriminative model, we do not model the input distribution p(x) so that we may equivalently
consider the likelihood of the set of output class variables
. If
we assume that each data point has been drawn independently from the same distribution that generates
the data (the standard i.i.d. assumption), the likelihood is (writing explicitly the conditional dependence
on the parameters b, w)

conditioned on the set of training inputs

X

C

@@
@@

b, w,

p(

C|

X

) =

N
(cid:89)

n=1

p(cn

xn, b, w)
|

(cid:8)(cid:8)(cid:8)
p(xn) =

N
(cid:89)

n=1

xn, b, w)cn
p(c = 1
|

(1

xn, b, w))1−cn
p(c = 1
|

−

(cid:8)(cid:8)(cid:8)
p(xn) (17.4.17)

where we have used the fact that cn

0, 1

∈ {

. For logistic regression this gives the log likelihood as
}

L(w, b) =

N
(cid:88)

n=1

cn log σ(b + wTxn) + (1

cn) log

(cid:16)

1

−

−

(cid:17)
σ(b + wTxn)

(17.4.18)

Gradient ascent

There is no closed form solution to the maximisation of L(w, b) which needs to be carried out numerically.
One of the simplest methods is gradient ascent for which the gradient is given by

wL =

∇

N
(cid:88)

(cn

n=1

−

σ(wTxn + b))xn

Here we made use of the derivative relation for the logistic sigmoid

dσ(x)/dx = σ(x)(1

σ(x))

−

The derivative with respect to the bias is

dL
db

N
(cid:88)

(cn

=

n=1

−

σ(wTxn + b))

The gradient ascent procedure then corresponds to updating the weights and bias using

wnew = w + η

wL,

∇

bnew = b + η

dL
db

(17.4.19)

(17.4.20)

(17.4.21)

(17.4.22)

where η, the learning rate is a scalar chosen small enough to ensure convergence3. The application of the
above rule will lead to a gradual increase in the log likelihood.

3In principle one may use a diﬀerent learning rate for each parameter.

DRAFT April 24, 2015

361

Linear Parameter Models for Classiﬁcation

x) = 0.5
Figure 17.10: The decision boundary p(c = 1
|
x) =
(solid line) and conﬁdence boundaries p(c = 1
|
x) = 0.1 after 10000 iterations of
0.9 and p(c = 1
|
(a): Linearly
batch gradient ascent with η = 0.1.
(b): Non-linearly separable data.
separable data.
Note how the conﬁdence interval remains broad, see
demoLogReg.m.

(a)

(b)

Batch training

Writing the updates (17.4.22) explicitly gives

wnew = w + η

N
(cid:88)

(cn

n=1

−

σ(wTxn + b))xn,

bnew = b + η

N
(cid:88)

(cn

n=1

−

σ(wTxn + b))

(17.4.23)

This is called a batch update since the parameters w and b are updated only after passing through the whole
(batch) of train data.
For linearly separable data, we can also show that the weights must become inﬁnite at convergence. Taking
the scalar product of Equation(17.4.19) with w, we have the zero gradient requirement

N
(cid:88)

n=1

(cn

−

σn) wTxn = 0

where σn

≡

σ (cid:0)wTxn + b(cid:1). For simplicity we assume b = 0. For linearly separable data we have

wTxn

(cid:26) > 0 if cn = 1
< 0 if cn = 0

Then, using the fact that 0

σn

1, we have

(cn

−

σn) wTxn

(cid:26)

≥
≥

≤

≤
0 if cn = 1
0 if cn = 0

(17.4.24)

(17.4.25)

(17.4.26)

Each term (cn
σn) wTxn is therefore non-negative and the zero gradient condition requires the sum of
these terms to be zero. This can only happen if all the terms are zero, implying that cn = σn, requiring the
sigmoid to saturate and the weights to be inﬁnite.

−

Online training

In practice it is common to update the parameters after each training example pair (xn, cn) has been
considered:

wnew = w +

η
N

(cn

−

σ(wTxn + b))xn,

bnew = b +

η
N

(cn

−

σ(wTxn + b))

(17.4.27)

An advantage of online training is that the dataset need not be stored since only the performance on the
current input is required. Provided that the data is linearly separable, the above online procedure converges
(provided η is not too large). However, if the data is not linearly separable, the online version will not
converge since the opposing class labels will continually pull the weights one way and then the other as each
conﬂicting example is used to form an update. For the limiting case of the perceptron (replacing σ(x) with
θ(x)) and linearly separable data, online updating converges in a ﬁnite number of steps[228, 42], but does
not converge for non-linearly separable data.

362

DRAFT April 24, 2015

00.20.40.60.8100.10.20.30.40.50.60.70.80.9100.20.40.60.8100.10.20.30.40.50.60.70.80.91Linear Parameter Models for Classiﬁcation

×

Figure 17.11: Logistic regression for classifying handwritten digits 1 and 7.
Displayed is a Hinton diagram of the 784 learned weight vector w, plotted as
a 28
28 image for visual interpretation. Light squares are positive weights
and an input x with a (positive) value in this component will tend to increase
the probability that the input is classed as a 7. Similarly, inputs with
positive contributions in the dark regions tend to increase the probability
as being classed as a 1 digit. Note that the elements of each input x are
either positive or zero.

Geometry of the error surface

The Hessian of the log likelihood L(w) is the matrix with elements4

Hij

≡

∂2L
∂wiwj

=

(cid:88)

−

n

i xn
xn

j σn(1

σn)

−

This is negative semideﬁnite since, for any z,

(cid:88)

ij

ziHijzj =

(cid:88)

i,j,n

−

zixn

i zjxn

j σn(1

σn)

−

≤ −

(cid:32)

(cid:88)

(cid:88)

(cid:33)2

zixn
i

n

i

0

≤

(17.4.28)

(17.4.29)

This means that the error surface is concave (an upside down bowl) and batch gradient ascent converges to
the optimal solution, provided the learning rate η is small enough.

Example 17.5 (Classifying Handwritten Digits). We apply logistic regression to the 600 handwritten digits
of example(14.1), in which there are 300 ones and 300 sevens in the train data. Using gradient ascent training
with a suitably chosen stopping criterion, the number of errors made on the 600 test points is 12, compared
with 14 errors using Nearest Neighbour methods. See ﬁg(17.11) for a visualisation of the learned w.

17.4.2 Beyond ﬁrst order gradient ascent

Since the surface has a single optimum, a Newton update

wnew = wold + ηH−1wold

(17.4.30)

where H is the Hessian matrix as above and 0 < η < 1, will typically converge much faster than gradient
ascent. However, for large scale problems with dim (w)
1, the inversion of the Hessian is computationally
demanding and limited memory BFGS or conjugate gradient methods are more practical alternatives, see
section(A.4).

(cid:29)

17.4.3 Avoiding overconﬁdent classiﬁcation

Provided the data is linearly separable the weights will continue to increase and the classiﬁcations will
become extreme. This is undesirable since the resulting classiﬁcations will be over-conﬁdent. One way
to prevent this is early stopping in which only a limited number of gradient updates are performed. An
alternative method is to add a penalty term to the objective function

L(cid:48)(w, b) = L(w, b)

αwTw.

−

(17.4.31)

The scalar constant α > 0 encourages smaller values of w (remember that we wish to maximise the log
likelihood). An appropriate value for α can be determined using validation data.

4For simplicity we ignore the bias b. This can readily be dealt with by extending x to a D + 1 dimensional vector ˆx with a

1 in the D + 1 component. Then for a D + 1 dimensional ˆw = (cid:0)wT, wD+1

(cid:1)T

, we have ˆwT ˆx = wTx + wD+1.

DRAFT April 24, 2015

363

Linear Parameter Models for Classiﬁcation

x) = σ(wTφ(x)) us-
Figure 17.12: Logistic regression p(c = 1
|
ing a quadratic function φ(x) = (1, x1, x2, x2
2, x1x2)T. 1000
iterations of gradient ascent training were performed with a
learning rate η = 0.1. Plotted are the datapoints for the
two classes (cross) and (circle) and the equal probability con-
tours. The decision boundary is the 0.5-probability contour. See
demoLogRegNonLinear.m.

1, x2

17.4.4 Multiple classes

For more than two classes, one may use the softmax function

p(c = i
|

x) =

ewT
i x+bi
j=1 ewT

(cid:80)C

j x+bj

(17.4.32)

where C is the number of classes. When C = 2 this can be reduced to the logistic sigmoid model. One can
show that the likelihood for this case is also concave, see exercise(17.3) and [318]. Gradient based training
methods then can be applied to training these models, as a straightforward extension of the two-class case.

17.4.5 The Kernel Trick for Classiﬁcation

A drawback of logistic regression as described above is the simplicity of the decision surface – a hyperplane.
Analogous to the regression case, one way to achieve more complex non-linear decision boundaries is to map
the inputs x in a non-linear way to a higher dimensional φ(x) and use
(cid:17)
wTφ(x) + b

(17.4.33)

(cid:16)

x) = σ
p(c = 1
|

Mapping into a higher dimensional space makes it easier to ﬁnd a separating hyperplane since any set of
points that are linearly independent can be linearly separated provided we have as many dimensions as
datapoints. For the maximum likelihood criterion, we may use exactly the same algorithm as before on
replacing x with φ(x). See ﬁg(17.12) for a demonstration using a quadratic function. Since only the scalar
product between the φ vectors plays a role the dual representation section(17.3) may again be used in which
we assume the weight can be expressed in the form

w =

(cid:88)

n

αmφ(xn)

(17.4.34)

We then subsequently ﬁnd a solution in terms of the dual parameters αn. This is potentially advantageous
since there may be less training points than dimensions of φ. The classiﬁer depends only on the scalar
product wTφ(x) = (cid:80)
n αnφ(xn)Tφ(x) and we can write more generally, using a positive deﬁnite kernel,
K(x, x(cid:48))

x) = σ
p(c = 1
|

(cid:33)

anK(x, xn)

(cid:32)

(cid:88)

n

For convenience, we can write the above as
(cid:16)

(cid:17)
aTk(x)

x) = σ
p(c = 1
|

(17.4.35)

(17.4.36)

where the N dimensional vector k(x) has elements [k(x)]m = K(x, xm). Then the above is of exactly the
same form as the original speciﬁcation of logistic regression, namely as a function of a linear combination of
vectors. Hence the same training algorithm to maximise the likelihood can be employed, simply on replacing
xn with k(xn). The details are left to the interested reader and follow closely the treatment of Gaussian
Processes for classiﬁcation, section(19.5).

364

DRAFT April 24, 2015

0.10.10.10.10.10.20.20.20.20.20.20.30.30.30.30.30.30.40.40.40.40.40.40.50.50.50.50.50.60.60.60.60.60.70.70.70.70.70.80.80.80.80.80.90.90.90.90.9−0.500.511.5−0.500.511.5Support Vector Machines

17.5 Support Vector Machines

Like kernel logistic regression, SVMs are a form of kernel linear classiﬁer. However, the SVM uses an
objective which more explicitly encourages good generalisation performance. SVMs do not ﬁt comfortably
within a probabilistic framework and as such we describe them here only brieﬂy, referring the reader to the
wealth of excellent literature on this topic5. The description here is inspired largely by [75].

17.5.1 Maximum margin linear classiﬁer

In the SVM literature it is common to use +1 and
by weight w and bias b, a linear discriminant is given by

−

1 to denote the two classes. For a hyperplane deﬁned

(cid:26)

wTx + b

0 class +1
≥
< 0 class -1

(17.5.1)

For a point x that is close to the decision boundary at wTx + b = 0, a small change in x can lead to a
change in classiﬁcation. To make the classiﬁer more robust we therefore impose that for the train data at
least, the decision boundary should be separated from the data by some ﬁnite amount (cid:15)2 (assuming in the
ﬁrst instance that the data is linearly separable):

wTx + b

(cid:26)

≥
<

(cid:15)2

(cid:15)2

−

class +1
class -1

(17.5.2)

Since w, b and (cid:15)2 can all be rescaled arbitrary, we need to ﬁx the scale of the above to break this invariance.
It is convenient to set (cid:15) = 1 so that a point x+ from class +1 that is closest to the decision boundary satisﬁes

wTx+ + b = 1

and a point x− from class -1 that is closest to the decision boundary satisﬁes

wTx− + b =

1
−

(17.5.3)

(17.5.4)

From vector algebra, ﬁg(17.13), the distance from the origin along the direction w to a point x is given by

wTx
√wTw

(17.5.5)

The margin between the hyperplanes for the two classes is then the diﬀerence between the two distances
along the direction w which is

wT
√wTw

(x+

−

x−) =

2
√wTw

(17.5.6)

To set the distance between the two hyperplanes to be maximal, we therefore need to minimise the length
wTw. Given that for each xn we have a corresponding label yn
, in order to classify the training
}
labels correctly and maximise the margin, the optimisation problem is equivalent to:

1
−

+1,

∈ {

minimise

wTw

1
2

subject to yn (cid:16)

(cid:17)
wTxn + b

1,

≥

n = 1, . . . , N

(17.5.7)

This is a quadratic programming problem. The factor 0.5 is just for convenience.

To account for potentially mislabelled training points (or for data that is not linearly separable), we relax
the exact classiﬁcation constraint and use instead

yn (cid:16)

(cid:17)
wTxn + b

ξn

1

−

≥

(17.5.8)

where the ‘slack variables’ are ξn
0. Here each ξn measures how far xn is from the correct margin, see
ﬁg(17.14). For 0 < ξn < 1 datapoint xn is on the correct side of the decision boundary. However for ξn > 1,
the datapoint is assigned the opposite class to its training label. Ideally we want to limit the size of these
‘violations’ ξn. Here we brieﬂy describe two standard approaches.

≥

DRAFT April 24, 2015

365

Support Vector Machines

Figure 17.13: SVM classiﬁcation of data from two
classes (open circles and ﬁlled circles). The decision
boundary wTx+b = 0 (solid line). For linearly separa-
ble data the maximum margin hyperplane is equidis-
tant from the closest opposite class points. These sup-
port vectors are highlighted in blue and the margin
in red. The distance of the decision boundary from
b/√wTw, and the distance of a gen-
the origin is
eral point x from the origin along the direction w is
xTw/√wTw.

−

Figure 17.14: Slack margin. The term ξn measures
how far a variable is from the correct side of the mar-
If ξn > 1 then the point will be
gin for its class.
misclassiﬁed and treated as an outlier.

w

origin

ξn

w

origin

2-Norm soft-margin

The 2-norm soft-margin objective is

minimise

wTw +

1
2

C
2

(cid:88)

(ξn)2

n

subject to yn (cid:16)

(cid:17)
wTxn + b

ξn, n = 1, . . . , N

(17.5.9)

1

−

≥

where C controls the number of mislabellings of the train data. The constant C needs to be determined
empirically using a validation set. The optimisation problem expressed by (17.5.9) can be formulated using
the Lagrangian

L(w, b, ξ, α) =

wTw +

1
2

C
2

(cid:88)

(ξn)2

n

−

(cid:88)

αn (cid:104)

yn (cid:16)

(cid:17)
wTxn + b

n

1 + ξn(cid:105)

,

−

αn

≥

0, ξn

≥

0 (17.5.10)

w, b, ξ and maximised with respect to α. For points xn on the
which is to be minimised with respect to
‘correct’ side of the decision boundary yn (cid:0)wTxn + b(cid:1)
1 + ξn > 0 so that maximising L with respect to
α requires the corresponding αn to be set to zero. Only training points that are support vectors lying on
the decision boundary have non-zero αn. Diﬀerentiating the Lagrangian and equating to zero, we have the
conditions

−

@@

∂
∂wi

∂
∂b

L(w, b, ξ, α) = wi

(cid:88)

−

n

αnynxn

i = 0

L(w, b, ξ, α) =

(cid:88)

−

n

αnyn = 0

∂
∂ξn L(w, b, ξ, α) = Cξn

−

αn = 0

From this we see that the solution for w is given by

w =

(cid:88)

n

αnynxn

5http://www.support-vector.net

(17.5.11)

(17.5.12)

(17.5.13)

(17.5.14)

366

DRAFT April 24, 2015

Support Vector Machines

Since only the support vectors have non-zero αn, the solution for w will typically depend on only a small
number of the training data. Using these conditions and substituting back into the original problem, the
objective is equivalent to minimising

L(α) =

(cid:88)

αn

n

1
2

(cid:88)

n,m

−

ynymαnαm (xn)T xm

1
2C

−

(cid:88)

(αn)2

n

subject to

ynαn = 0,

(cid:88)

n

αn

0

≥

If we deﬁne

K(xn, xm) = (xn)T xm

The optimisation problem is

maximize

(cid:88)

αn

subject to

n
(cid:88)

n

1
2

−

n,m
ynαn = 0,

αn

0

≥

(cid:88)

ynymαnαm

(cid:18)

K(xn, xm) +

(cid:19)

1
C

δn,m

Optimising this objective is discussed in section(17.5.3).

1-norm soft-margin (box constraint)

In the 1-norm soft-margin version, one uses a 1-norm penalty

(cid:88)

ξn

C

n

to give the optimisation problem:

(17.5.15)

(17.5.16)

(17.5.17)

(17.5.18)

minimise

1
2

wTw +C

(cid:88)

ξn

subject to yn (cid:16)

(cid:17)
wTxn + b

n

ξn, ξn

1

−

≥

0,

≥

n = 1, . . . , N (17.5.19)

where C is an empirically determined penalty factor that controls the number of mislabellings of the train
data. To reformulate the optimisation problem we use the Lagrangian

L(w, b, ξ) =

1
2

wTw + C

(cid:88)

ξn

n

(cid:88)

−

n

αn (cid:104)

yn (cid:16)

(cid:17)
wTxn + b

1 + ξn(cid:105)

−

(cid:88)

−

n

rnξn,

αn

≥

0, ξn

≥

0, rn

0

≥

(17.5.20)

The variables rn are introduced in order to give a non-trivial solution (otherwise αn = C). Following a
similar argument as for the 2-norm case, by diﬀerentiating the Lagrangian and equating to zero, we arrive
at the optimisation problem

maximize

(cid:88)

αn

(cid:88)

ynymαnαmK(xn, xm)

1
2

−

n,m
ynαn = 0,

subject to

n
(cid:88)

n

αn

0

≤

C

≤

which is closely related to the 2-norm problem except that we now have the box-constraint 0

(17.5.21)

αn

C.

≤

≤

17.5.2 Using kernels
The ﬁnal objectives (17.5.17) and (17.5.21) depend on the inputs xn only via the scalar product (xn)T xn.
If we map x to a vector function of x, then we can write

K(xn, xm) = φ(xn)Tφ(xm)

(17.5.22)

This means that we can use any positive semideﬁnite kernel K and make a non-linear classiﬁer. See also
section(19.3).

DRAFT April 24, 2015

367

Soft Zero-One Loss for Outlier Robustness

Figure 17.15: SVM training. The solid red and solid blue circles represent train
data from diﬀerent classes. The support vectors are highlighted in green. For
the unﬁlled test points, the class assigned to them by the SVM is given by the
colour. See demoSVM.m

17.5.3 Performing the optimisation

Both of the above soft-margin SVM optimisation problems (17.5.17) and (17.5.21) are quadratic programs
for which the exact computational cost scales as O (cid:0)N 3(cid:1). Whilst these can be solved with general purpose
routines, speciﬁcally tailored routines that exploit the structure of the problem are preferred in practice. Of
particular practical interest are ‘chunking’ techniques that optimise over a subset of the α. In the limit of
updating only two components of α, this can be achieved analytically, resulting in the Sequential Minimal
Optimisation algorithm[241], whose practical performance is typically O (cid:0)N 2(cid:1) or better. A variant of this
algorithm [99] is provided in SVMtrain.m. See ﬁg(17.15) for a demonstration.

Once the optimal solution α1

∗ is found the decision function for a new point x is

∗ ynK(xn, x) + b∗
αn

(cid:88)

n

assign to class 1
assign to class -1

∗, . . . , αN
(cid:26) > 0
< 0

The optimal b∗ is determined using the maximum margin condition, equations(17.5.3,17.5.4):

b∗ =

(cid:34)

1
2

min
yn=1

(cid:88)

m

∗ ymK(xm, xn)
αm

max
yn=−1

−

(cid:88)

m

(cid:35)
∗ ymK(xm, xn)
αm

17.5.4 Probabilistic interpretation

(17.5.23)

(17.5.24)

Kernelised logistic-regression has some of the characteristics of the SVM but does not express the large
margin requirement. Also the sparse data usage of the SVM is similar to that of the Relevance Vector
Machine we discuss in section(18.2.5). However, a probabilistic model whose MAP assignment matches
exactly the SVM is hampered by the normalisation requirement for a probability distribution. Whilst,
arguably, no fully satisfactory direct match between the SVM and a related probabilistic model has been
achieved, approximate matches have been obtained[271].

17.6 Soft Zero-One Loss for Outlier Robustness

Both the support vector machine and logistic regression are potentially misled by outliers. For the SVM,
a mislabelled datapoint that is far from the correct side of the decision boundary would require a large
slack ξ. However, since exactly such large ξ are discouraged, it is unlikely that the SVM would admit such
a solution. For logistic regression, the probability of generating a mislabelled point far from the correct
side of the decision boundary is so exponentially small that this will never happen in practice. This means
that the model trained with maximum likelihood will never present such a solution. In both cases therefore
mislabelled points (or outliers) potentially have a signiﬁcant impact on the location of the decision boundary.

A robust technique to deal with outliers is to use the zero-one loss in which a mislabeled point contributes
only a relatively small loss. Soft variants of this are obtained by using the objective

N
(cid:88)

n=1

(cid:104)
σβ(b + wTxn)

cn(cid:105)2

+ λwTw

−

(17.6.1)

which is to be minimised with respect to w and b. For β
the ﬁrst term above tends to the zero-one
loss. The second term represents a penalty on the length of w and prevents overﬁtting. Kernel extensions

→ ∞

368

DRAFT April 24, 2015

−2−1012−2.5−2−1.5−1−0.500.511.52Soft Zero-One Loss for Outlier Robustness

Figure 17.16: Soft zero-one loss decision boundary (solid line) versus logistic
regression (dotted line). The number of mis-classiﬁed training points using
the soft zero-one loss is 2, compared to 3 for logistic regression. The penalty
λ = 0.01 was used for the soft-loss, with β = 10. For logistic regression, no
penalty term was used. The outliers have a signiﬁcant impact on the decision
boundary for logistic regression, whilst the soft zero-one loss essentially
gives up on the outliers and ﬁts a classiﬁer to the remaining points. See
demoSoftLoss.m.

of this soft zero-one loss are straightforward.

Unfortunately, the objective (17.6.1) is non-convex and ﬁnding the optimal w, b is computationally dif-
ﬁcult. A simple-minded scheme is to ﬁx all components of w except one and then perform a numerical
one-dimensional optimisation over this single parameter wi. At the next step, another parameter wj is cho-
sen, and the procedure repeated until convergence. As usual, λ can be set using validation. The practical
diﬃculties of minimising non-convex high-dimensional objective functions means that these approaches are
rarely used in practice. A discussion of practical attempts in this area is given in [306].

An illustration of the diﬀerence between logistic regression and this soft zero-one loss is given in ﬁg(17.16),
which demonstrates how logistic regression is inﬂuenced by the mass of the data points, whereas the zero-one
loss attempts to minimise the number of mis-classiﬁcations whilst maintaining a large margin.

17.7 Summary

•

•

•

•

Fitting linear regression models based on least-squares is straightforward and requires only the solution of
a linear system.

In classiﬁcation, the maximum likelihood criterion typically results in a simple concave function of the
parameters, for which simple gradient based learning methods are suitable for training.

The well-known and historically important perceptron can be viewed as a limiting case of logistic regression.

The kernel extensions of these linear models enable one to ﬁnd non-linear decision boundaries in the
classiﬁcation case. These techniques are closely related to Gaussian Processes.

Historical note

The perceptron has a long history in artiﬁcial intelligence and machine learning. Rosenblatt discussed the
perceptron as a model for human learning, arguing that its distributive nature (the input-output ‘patterns’
are stored in the weight vector) is closely related to the kind of information storage believed to be present
in biological systems[251]. To deal with non-linear decision boundaries, the main thrust of research in the
ensuing neural network community was on the use of multilayered structures in which the outputs of per-
ceptrons are used as the inputs to other perceptrons, resulting in potentially highly non-linear discriminant
functions. This line of research was largely inspired by analogies to biological information processing in
which layered structures are prevalent. Such multilayered artiﬁcial neural networks are fascinating and,
once trained, are extremely fast in forming their decisions. However, reliably training these systems is a
highly complex task and probabilistic generalisations in which priors are placed on the parameters lead to
computational diﬃculties. Whilst perhaps less inspiring from a biological viewpoint, the alternative route
of using the kernel trick to boost the power of a linear classiﬁer has the advantage of ease of training and
generalisation to probabilistic variants. More recently, however, there has been a resurgence of interest in
multilayer systems, with new heuristics aimed at improving the diﬃculties in training, see for example [147].

DRAFT April 24, 2015

369

−4−202468−4−2024681012Exercises

17.8 Code

demoCubicPoly.m: Demo of ﬁtting a cubic polynomial
demoLogReg.m: Demo of logistic regression
LogReg.m: Logistic regression gradient ascent training
demoLogRegNonLinear.m: Demo of logistic regression with a non-linear φ(x)
SVMtrain.m: SVM training using the SMO algorithm
demoSVM.m: SVM demo
demoSoftLoss.m: softloss demo
softloss.m: softloss function

17.9 Exercises

Exercise 17.1.

1. Give an example of a two-dimensional dataset for which the data are linearly separable, but not linearly

independent.

2. Can you ﬁnd a dataset which is linearly independent but not linearly separable?

Exercise 17.2. Show that for both Ordinary and Orthogonal Least Squares regression ﬁts to data (xn, yn), n =
1, . . . , N ) the ﬁtted lines go through the point (cid:80)N

n=1(xn, yn)/N .

Exercise 17.3. Consider the softmax function for classifying an input vector x into one of c = 1, . . . , C
classes using

x) =
p(c
|

ewT
c x
c(cid:48)=1 ewT
c(cid:48) x
A set of input-class examples is given by

(cid:80)C

=

(xn, cn), n = 1, . . . , N
{

.
}

D

(17.9.1)

1. Relate this model to logistic regression when C = 2.

2. Write down the log-likelihood L of the classes conditional on the inputs, assuming that the data is i.i.d.

3. Compute the Hessian with elements

Hij =

∂2L(
)
D
∂wiwj

where w is the stacked vector

(cid:16)

w =

wT

1 , . . . , wT
C

(cid:17)T

(17.9.2)

(17.9.3)

and show that the Hessian is negative semideﬁnite, that is zTHz
≤
you need to use to use the result that the variance is non-negative.

0 for any z. Hint: at some point

Exercise 17.4. Derive from equation (17.5.9) the dual optimisation problem equation (17.5.15).

Exercise 17.5. A datapoint x is projected to a lower dimensional vector ´x using

´x = Mx

(17.9.4)

and corresponding
where M is a given ‘fat’ (short and wide) matrix. For a set of data
binary class labels yn
, using logistic regression on the projected datapoints ´xn corresponds to a form
of constrained logistic regression in the original higher dimensional space x. Explain if it is reasonable to
use an algorithm such as PCA to ﬁrst reduce the data dimensionality before using logistic regression.

xn, n = 1, . . . , N
{

0, 1
}

∈ {

}

370

DRAFT April 24, 2015

Exercises

Exercise 17.6. The logistic sigmoid function is deﬁned as σ(x) = ex/(1+ex). What is the inverse function,
σ−1(x)?

Exercise 17.7. Given a dataset
, logistic regression uses the
}
}
x) = σ(wTx + b). Assuming the data is drawn independently and identically, show that the
model p(c = 1
|
derivative of the log likelihood L with respect to w is

(xn, cn), n = 1, . . . , N
{

, where cn

∈ {

0, 1

=

D

wL =

∇

N
(cid:88)

(cid:16)

n=1

(cid:16)

σ

cn

−

wTxn + b

(cid:17)(cid:17)

xn

(17.9.5)

Exercise 17.8. Consider a dataset
sional vector.

=

{

D

(xn, cn), n = 1, . . . , N

, where cn
}

0, 1

∈ {

, and x is a D dimen-
}

1. Show that if the training data is linearly separable with the hyperplane wTx + b, the data is also

separable with the hyperplane ˜wTx + ˜b, where ˜w = λw, ˜b = λb for any scalar λ > 0.

2. What consequence does the above result have for maximum likelihood training of logistic regression for

linearly separable data?

=

(xn, cn), n = 1, . . . , N
Exercise 17.9. Consider a dataset
, and x is a N dimen-
sional vector. Hence we have N datapoints in a N dimensional space. In the text we showed that to ﬁnd
a hyperplane (parameterised by w and b) that linearly separates this data we need, for each datapoint xn,
wTxn + b = (cid:15)n where (cid:15)n > 0 for cn = 1 and (cid:15)n < 0 for cn = 0. Furthermore, we suggested an algorithm to
ﬁnd such a hyperplane. Comment on the relation between maximum likelihood training of logistic regression
and the algorithm suggested above.

, where cn

∈ {

0, 1

D

{

}

}

Exercise 17.10. Given training data
a discriminative model is

=

{

D

(xn, cn), n = 1, . . . , N

, cn

}

0, 1
}

∈ {

, where x are vector inputs,

x) = σ(b0 + v1g(wT
p(c = 1
|

1 x + b1) + v2g(wT

2 x + b2))

(17.9.6)

where g(x) = exp(
and two hidden units).

−

0.5x2) and σ(x) = ex/(1 + ex) (this is a neural network[42] with a single hidden layer

1. Write down the log likelihood for the class conditioned on the inputs, based on the usual i.i.d. assump-

tion.

2. Calculate the derivatives of the log likelihood as a function of the network parameters, w1, w2, b1, b2,

v0,v1, v2.

3. Comment on the relationship between this model and logistic regression.

4. Comment on the decision boundary of this model.

DRAFT April 24, 2015

371

Exercises

372

DRAFT April 24, 2015

CHAPTER 18

Bayesian Linear Models

The previous chapter discussed the use of linear models in classiﬁcation and regression.
In this chapter
we discuss using priors on the parameters and the resulting posterior distribution over parameters. This
represents a powerful extension since it enables us to specify in a principled way our prior knowledge and
takes into account that when data is limited there will be considerable uncertainty as to which is the ‘best’
parameter estimate.

18.1 Regression With Additive Gaussian Noise

The linear models in chapter(17) were trained under maximum likelihood and do not deal with the is-
sue that, from a probabilistic perspective, parameter estimates are inherently uncertain due to the lim-
ited available training data. Regression refers to inferring a mapping on the basis of observed data
, where (xn, yn) represents an input-output pair. We discuss here the scalar
D
output case (and vector inputs x) with the extension to the vector output case y being straightforward. We
assume that each (clean) output is generated from a model f (x; w) where the parameters w of the function
f are unknown. An observed output y is generated by the addition of noise η to the clean model output,

(xn, yn), n = 1, . . . , N

=

}

{

y = f (x; w) + η

If the noise is Gaussian distributed, η
probability

∼ N

(18.1.1)
(cid:0)η 0, σ2(cid:1), the model generates an output y for input x with

p(y

w, x) =
|

N

(cid:0)y f (x; w), σ2(cid:1) =

1
√2πσ2

exp

(cid:18)

−

1
2σ2 [y

−

(cid:19)

f (x; w)]2

(18.1.2)

Here our interest is only in modelling this output distribution, and not the distribution of the inputs. As such
If we assume that each data input-output pair is generated
no parameters are used to model the inputs.
identically and independently, the likelihood the model generates the data is

p(

w) =

D|

N
(cid:89)

n=1

p(yn

w, xn)p(xn)
|

(18.1.3)

We may use a prior weight distribution p(w) to quantify our a priori belief in the suitability of each parameter
setting. Writing

, the posterior weight distribution is then given by

=

D
p(

x,

y
{D
D
w)p(w)

}

∝

p(w

)

|D

∝

D|

p(

y
D

w,
|

D

x)p(w)

Using the Gaussian noise assumption, and for convenience deﬁning β = 1/σ2, this gives

(18.1.4)

(18.1.5)

log p(w

) =

|D

β
2

−

N
(cid:88)

n=1

[yn

−

f (xn; w)]2 + log p (w) +

N
2

log β + const.

373

Regression With Additive Gaussian Noise

α

w

xn

yn

N

Figure 18.1: Belief Network representation of a Bayesian Model for
regression under the i.i.d. data assumption. The hyperparameter α
acts as a form of regulariser, controlling the ﬂexibility of the prior on
the weights w. The hyperparameter β controls the level of noise on
the observations.

β

Note the similarity between equation (18.1.5) and the regularised training error equation (17.2.17). In the
probabilistic framework, we identify the choice of a sum squared error with the assumption of additive
Gaussian noise. Similarly, the regularising term is identiﬁed with log p(w).

18.1.1 Bayesian linear parameter models

Linear parameter models, as discussed in chapter(17), have the form

f (x; w) =

B
(cid:88)

i=1

wiφi(x)

≡

wTφ(x)

(18.1.6)

where the parameters wi are also called ‘weights’ and the number of basis functions is dim (w) = B. Such
models have a linear parameter dependence, but may represent a non-linear input-output mapping if the
basis functions φi(x) are non-linear in x. Since the output scales linearly with w, we can discourage extreme
output values by penalising large weight values. A natural weight prior is thus

p(w

α) =
|

N

(cid:0)w 0, α−1I(cid:1) =

(cid:17) B

2 exp

(cid:16) α
2π

(cid:17)

wTw

(cid:16)

α
2

−

(18.1.7)

where the precision α is the inverse variance. If α is large, the total squared length of the weight vector w
α, β) is then speciﬁed, see ﬁg(18.1). Under the Gaussian
is encouraged to be small. The full model p(
D
noise assumption, the posterior distribution is

, w

|

log p(w

Γ,
|

D

) =

−

β
2

N
(cid:88)

(cid:104)

n=1

yn

−

wTφ(xn)

(cid:105)2

α
2

−

wTw + const.

(18.1.8)

α, β
{

where Γ =
represents the hyperparameter set. Parameters that determine the functions φ may also be
included in the hyperparameter set. Completing the square, section(8.4.1), the weight posterior is therefore
a Gaussian distribution,

}

p(w

Γ,
|

D

) =

N

(w m, S)

where the covariance and mean are given by

(cid:32)

S =

αI + β

N
(cid:88)

n=1

(cid:33)−1

φ (xn) φT (xn)

,

m = βS

N
(cid:88)

n=1

ynφ (xn)

The mean prediction for an input x is then given by

(cid:90)

¯f (x)

≡

f (x; w)p(w

|D

, Γ)dw = mTφ (x) .

Similarly, the variance of the underlying estimated clean function is

var(f (x)) =

wTφ(x)

(cid:28)(cid:104)

(cid:105)2(cid:29)

¯f (x)2 = φT(x)Sφ(x)

p(w|D,Γ) −

(18.1.9)

(18.1.10)

(18.1.11)

(18.1.12)

The output variance var(f (x)) depends only on the input variables and not on the training outputs y. Since
the additive noise η is uncorrelated with the model outputs, the predictive variance is

var(y(x)) = var(f (x)) + σ2

(18.1.13)

and represents the variance of the ‘noisy’ output for an input x.

374

DRAFT April 24, 2015

Regression With Additive Gaussian Noise

(a)

(b)

(c)

(a):
Figure 18.2: Along the horizontal axis we plot the input x and along the vertical axis the output y.
(b): Prediction using regularised training
The raw input-output training data and basis functions φi(x).
and (poorly chosen) ﬁxed hyperparameters. (c): Prediction using ML-II optimised hyperparameters. Also
plotted are standard error bars on the clean underlying function, ¯f (x)

var(f (x)).

(cid:112)

±

Example 18.1. In ﬁg(18.2b), we show the mean prediction on the data in ﬁg(18.2a) using 15 Gaussian
basis functions

φi(x) = exp (cid:0)

0.5(x

−

−

ci)2/λ2(cid:1)

(18.1.14)

with width λ = 0.032 and centres ci spread out evenly over the 1-dimensional input space from
2 to 2. We
set the other hyperparameters by hand to β = 100 and α = 1. The prediction severely overﬁts the data, a
result of a poor choice of hyperparameter settings. This is resolved in ﬁg(18.2c) using the ML-II parameters,
as described below.

−

18.1.2 Determining hyperparameters: ML-II

The hyperparameter posterior distribution is

p(Γ

|D

p(

)

∝

Γ)p(Γ)

D|

(18.1.15)

A simple summarisation of the posterior is given by the MAP assignment which takes the single ‘optimal’
setting:

Γ∗ = argmax

Γ

p(Γ

|D

)

(18.1.16)

If the prior belief about the hyperparameters is weak (p(Γ)
maximises the marginal likelihood

≈

const.), this is equivalent to using the Γ that

(cid:90)

p(

Γ) =

D|

p(

Γ, w)p(w

D|

Γ)dw

|

(18.1.17)

This approach to setting hyperparameters is called ‘ML-II’ [33] or the Evidence Procedure[195].

In the case of Bayesian Linear Parameter models under Gaussian additive noise, computing the marginal
likelihood equation (18.1.17) involves only Gaussian integration. A direct approach to deriving an expression
for the marginal likelihood is to consider

@@
@@

p(

Γ, w)p(w) = exp

D|

(cid:32)

−

β
2

N
(cid:88)

n=1

(cid:104)
yn

−

wTφ(xn)

(cid:105)2

α
2

−

(cid:33)

wTw

(β/2π)N/2 (α/2π)B/2

(18.1.18)

DRAFT April 24, 2015

375

By collating terms in w (completing the square, section(8.4.1)), the above represents a Gaussian in w with
additional factors. After integrating over this Gaussian we have

Regression With Additive Gaussian Noise

2 log p(

Γ) =

D|

β

−

N
(cid:88)

n=1

where

d = β

(cid:88)

n

φ(xn)yn

(yn)2 + dTS−1d

+ log det (S) + B log α + N log β

N log (2π)

−

(18.1.19)

@@

(18.1.20)

See exercise(18.2) for an alternative expression.

Example 18.2. We return to the example in ﬁg(18.2) and try to ﬁnd a more appropriate setting for the
hyperparameters. Using the hyperparameters α, β, λ that optimise expression (18.1.19) gives the results in
ﬁg(18.2c) where we plot both the mean predictions and standard predictive error bars. This demonstrates
that an acceptable setting for the hyperparameters can be obtained by maximising the marginal likelihood.
Generally speaking, provided the number of hyperparameters is low compared to the number of datapoints,
setting hyperparameters using ML-II will not be at risk of overﬁtting.

18.1.3 Learning the hyperparameters using EM

As described above, we can set hyperparameters Γ by maximising the marginal likelihood equation (18.1.17).
A convenient computational procedure to achieve this is to interpret the w as latent variables and apply
the EM algorithm, section(11.2). From equation (18.1.17), the energy term is

E

log p(

w, Γ)p(w

≡ (cid:104)

D|

Γ)
|

(cid:105)p(w|D,Γold)

(18.1.21)

According to the general EM procedure we need to maximise the energy term. For a hyperparameter Γ the
derivative of the energy is given by

∂
∂Γ

E

≡

(cid:28) ∂
∂Γ

log p(

w, Γ)p(w

D|

(cid:29)

Γ)

|

p(w|D,Γold)

For the Bayesian LPM with Gaussian weight and noise distributions, we obtain

∂
∂β

E =

N
2β −

=

N
2β −

1
2

1
2

N
(cid:88)

n=1
N
(cid:88)

n=1

(cid:28)(cid:104)

yn

−

(cid:105)2(cid:29)

wTφ(xn)

(cid:104)
yn

−

mTφ(xn)

(cid:105)2

−

1
2

p(w|Γold,D)
(cid:32)

trace

S

(cid:33)

φ(xn)φT(xn)

N
(cid:88)

n=1

(18.1.22)

(18.1.23)

(18.1.24)

where S and m are given in equation (18.1.10). Solving for the zero derivatives gives the M-step update

1
βnew =

1
N

N
(cid:88)

(cid:104)

n=1

yn

−

mTφ(xn)

(cid:105)2

+ trace

(cid:16)

(cid:17)
SˆS

N
(cid:88)

φ(xn)φT(xn)

where

ˆS

1
N

≡

n=1
Similarly, for α,
B
2α −

∂
∂α

E =

(cid:68)

wTw

1
2

(cid:69)

p(w|Γold,D)

=

(cid:16)

B
2α −

1
2

trace (S) + mTm

(cid:17)

which, on equating to zero, gives the update

1
αnew =

1
B

(cid:16)

(cid:17)
trace (S) + mTm

(18.1.25)

(18.1.26)

(18.1.27)

An alternative ﬁxed point procedure that can be more rapidly convergent than EM is given in equation
(18.1.37). Closed form updates for other hyperparameters, such as the width of the basis functions, are
generally not available, and the corresponding energy term needs to be optimised numerically.

376

DRAFT April 24, 2015

Regression With Additive Gaussian Noise

Figure 18.3: Predictions for an RBF for diﬀerent widths λ. For each λ the ML-II optimal α, β are obtained
by running the EM procedure to convergence and subsequently used to form the predictions. In each panel
the dots represent the training points, with x along the horizontal axis and y along the vertical axis. Mean
predictions are plotted, along with predictive error bars of one standard deviation. According to ML-II, the
best model corresponds to λ = 0.37, see ﬁg(18.4). The smaller values of λ overﬁt the data, giving rise to too
‘rough’ functions. The largest values of λ underﬁt, giving too ‘smooth’ functions. See demoBayesLinReg.m.

18.1.4 Hyperparameter optimisation : using the gradient

To maximise equation (18.1.17) with respect to hyperparameters Γ, we can make use of the general identity
from equation (11.6.3) which, in this context, is

∂
∂Γ

log p(

Γ) =

D|

(cid:28) ∂
∂Γ

log p(

w, Γ)p(w

D|

(cid:29)

Γ)

|

p(w|D,Γ)

Since the likelihood is independent of α,

log p(

Γ) =

D|

(cid:28) ∂
∂α

log p(w

α)

|

(cid:29)

p(w|Γ,D)

∂
∂α

Using

log p(w

α) =
|

−

wTw +

α
2

B
2

log α + const.

(18.1.28)

(18.1.29)

(18.1.30)

λ, α∗(λ), β∗(λ))
Figure 18.4: The log marginal likelihood log p(
having found the optimal values of the hyperparameters α and
β using ML-II. These optimal values are dependent on λ. Ac-
cording to ML-II, the best model corresponds to λ = 0.37. From
ﬁg(18.3) we see that this gives a reasonable ﬁt to the data.

D|

DRAFT April 24, 2015

377

−202−2−1012lambda=0.01−202−2−1012lambda=0.05−202−2−1012lambda=0.09−202−2−1012lambda=0.13−202−2−1012lambda=0.17−202−2−1012lambda=0.21−202−2−1012lambda=0.25−202−2−1012lambda=0.29−202−2−1012lambda=0.33−202−2−1012lambda=0.37−202−2−1012lambda=0.41−202−2−1012lambda=0.45−202−2−1012lambda=0.49−202−2−1012lambda=0.53−202−2−1012lambda=0.57−202−2−1012lambda=0.61−202−2−1012lambda=0.65−202−2−1012lambda=0.69−202−2−1012lambda=0.73−202−2−1012lambda=0.77−0.100.10.20.30.40.50.60.702468101214x 109log marginal likelihoodlambda(18.1.34)

(18.1.35)

we obtain

∂
∂α

log p(

Γ) =

D|

(cid:28)

1
2

wTw +

−

B
α

(cid:29)

p(w|Γ,D)

Setting the derivative to zero, the optimal α satisﬁes

(cid:68)

wTw

(cid:69)

0 =

−

p(w|Γ,D)

+

B
α

One may now form a ﬁxed point equation

αnew =

B

wTw
(cid:104)

(cid:105)p(w|Γ,D)

Regression With Additive Gaussian Noise

(18.1.31)

(18.1.32)

(18.1.33)

which is equivalent to an EM update, equation (18.1.27), for this model. For a Gaussian posterior,
p(w

(w m, S),

) =

Γ,
|

D
wTw

N
(cid:69)

(cid:68)

= trace

(cid:16)(cid:68)

wwT(cid:69)

w

w

(cid:105) (cid:104)

(cid:105)

− (cid:104)

T +

w
(cid:104)

w

(cid:105) (cid:104)

T(cid:17)
(cid:105)

= trace (S) + mTm

αnew =

B
trace (S) + mTm

One may similarly ﬁnd the gradient and associated ﬁxed point update for β which, again is equivalent to
the EM update for this model.

Gull-MacKay ﬁxed point iteration

From equation (18.1.32) we have

(cid:68)

wTw

(cid:69)

0 =

α

−

p(w|Γ,D)

+ B =

αS

−

−

αmTm + B

so that an alternative ﬁxed point equation[135, 194] is

αnew =

B

−

αtrace (S)
mTm

(18.1.36)

(18.1.37)

In practice this update converges more rapidly than equation (18.1.35). Similarly, one can form an alternative
update for β

1

βtrace

(cid:16)

(cid:17)

SˆS

βnew =

1
N

(cid:80)N

−
n=1 [yn

mTφ(xn)]2

−

(18.1.38)

Example 18.3 (Learning the basis function widths). In ﬁg(18.3) we plot the training data for a regression
problem using a Bayesian LPM. A set of 10 Radial Basis Functions (RBFs) are used,

φi(x) = exp (cid:0)

0.5(x

−

−

ci)2/λ2(cid:1)

(18.1.39)

with ci, i = 1, . . . , 10 spread out evenly between
2 and 2. The hyperparameters α and β are learned by
ML-II under EM updating. For a ﬁxed width λ we then present the predictions, each time ﬁnding the
optimal α and β for this width. The optimal joint α, β, λ hyperparameter setting is obtained as described
in ﬁg(18.4) which shows the marginal log likelihood for a range of widths. The ﬁt resulting from the jointly
optimal α, β, λ hyperparameter is reasonable.

−

378

DRAFT April 24, 2015

Regression With Additive Gaussian Noise

18.1.5 Validation likelihood

The hyperparameters found by ML-II are those which are best at explaining the training data. In principle,
this is diﬀerent from those that are best for prediction and, in practice therefore, it is reasonable to set
hyperparameters also by validation techniques. One such method is to set hyperparameters by minimal
prediction error on a validation set. Another common technique is to set hyperparameters Γ by their
likelihood on a validation set

(xm

val), m = 1, . . . , M

val, ym

val,

val

:
}

{X

} ≡ {

Y
(cid:90)

p(

Y

val

Γ,
|

train,

X

train,

Y

X

val) =

w

p(

Y

val

w, Γ)p(w
|

Γ,
|

train,

X

Y

train)

(18.1.40)

from which we obtain (see exercise(18.3))

log p(

Y

val

Γ,
|

train,

D

val) =

X

1
2

−

log det (2πCval)

1
2

−

(yval

−

Φvalm)T C−1

val (yval

−

Φvalm)

(18.1.41)

where yval = (cid:2)y1

val, . . . , yM
val

(cid:3)T

, covariance

Cval

≡

ΦvalSΦT

val + σ2IM

and the design matrix of explanatory variables

ΦT

val = (cid:2)φ(x1

val), . . . , φ(xM

val)(cid:3)

(18.1.42)

(18.1.43)

The optimal hyperparameters Γ∗ can then be found by maximising (18.1.41) with respect to Γ.

18.1.6 Prediction and model averaging

For ﬁxed hyperparameters, the Bayesian LPM deﬁnes a Gaussian posterior distribution on the weights w.
The distribution can be used to compute the expected predictor and also the variance of the predictor.
More generally, however, we can place a prior distribution on the hyperparameters themselves and obtain a
corresponding posterior

p(Γ

|D

p(

)

∝

Γ)p(Γ)

D|

(18.1.44)

The mean function predictor is then given by integrating over both the posterior weights and the hyperpa-
rameters

(cid:90)

¯f (x) =

f (x; w)p(w, Γ

|D

)dwdΓ =

(cid:90) (cid:26)(cid:90)

f (x; w)p(w

Γ,
|

D

)dw

p(Γ

)dΓ

|D

(cid:27)

(18.1.45)

The term in curly brackets is the mean predictor for ﬁxed hyperparameters. Equation(18.1.45) then weights
each mean predictor by the posterior probability of the hyperparameter p(Γ
). This is a general recipe for
combining model predictions, where each model is weighted by its posterior probability. However, computing
the integral over the hyperparameter posterior is numerically challenging and approximations are usually
required. Provided the hyperparameters are well determined by the data, we may instead approximate the
above hyperparameter integral by ﬁnding the MAP hyperparameters and use

|D

(cid:90)

¯f (x)

≈

f (x; w)p(w

Γ∗,
|

D

)dw

(18.1.46)

Under a ﬂat prior p(Γ) = const., this is equivalent to using the mean predictor with hyperparameters set by
ML-II. An alternative is to draw samples Γl, l = 1, . . . , L from the posterior distribution p(Γ
) and form
then a predictor by averaging over the samples

|D

¯f (x)

1
L

≈

L
(cid:88)

(cid:90)

l=1

f (x; w)p(w

Γl,

|

D

)dw

DRAFT April 24, 2015

(18.1.47)

379

18.1.7 Sparse linear models

Regression With Additive Gaussian Noise

A common interest is to attempt to explain data using as few of the inputs as possible. More generally, we
can try to ﬁnd the most parsimonious explanation using a limited number of features φi(x). From a Bayesian
LPM perspective, we require a prior that strongly encourages only a limited number of weights wi to be
active, with the rest being set to zero. Formally, this corresponds to a non-Gaussian prior on the weights
which hampers the use of exact Bayesian methods. Non Bayesian methods make use of penalty terms such
as L1 (lasso) regularisation (cid:80)
for which the objective of the LPM remains concave [291]l; this is closely
related to using Laplace priors in the Bayesian setting. Here we brieﬂy discuss some approximate methods
for sparse linear regression.

wi

i |

|

The relevance vector machine

The relevance vector machine assumes that only a small number of components of the basis function vector
are relevant in determining the solution for w. For a predictor,

f (x; w) =

B
(cid:88)

i=1

wiφi(x)

≡

wTφ(x)

(18.1.48)

it is often the case that some basis functions will be redundant in the sense that a linear combination of the
other basis functions can reproduce the training outputs with insigniﬁcant loss in accuracy. To exploit this
eﬀect and seek a parsimonious solution we may use a more reﬁned prior that encourages each wi itself to be
small:

p(w

α) =
|

(cid:89)

i

p(wi

αi)
|

For computational convenience, in the RVM one typically chooses a Gaussian

p(wi

|

αi) =

N

(cid:0)wi 0, α−1

i

(cid:1) =

(cid:17) 1

2 exp

(cid:16) αi
2π

(cid:16)

αi
2

−

(cid:17)

.

w2
i

(18.1.49)

(18.1.50)

Sparsity is then achieved by optimising over the hyperparameters αi, with large αi eﬀectively forcing a
weight wi to be zero. The modiﬁcations required to the description of section(18.1.1) are to replace S with

(cid:32)

S =

diag (α) + β

(cid:33)−1

φ (xn) φT (xn)

N
(cid:88)

n=1

The marginal likelihood is then given by

(18.1.51)

2 log p(

Γ) =

D|

β

−

N
(cid:88)

n=1

(yn)2 + dTS−1d

log det (S) +

−

B
(cid:88)

i=1

log αi + N log β

−

N log (2π)

(18.1.52)

The EM update for β is unchanged, and the EM update for each αi is

1
αnew
i

= [S]ii + m2
i

(18.1.53)

A potential diﬃculty with this approach is that there are as many hyperparameters as there are parameters
and ﬁnding the optimal hyperparameters by ML-II can be problematic, resulting in overly aggressive pruning
of the weights. This can be ameliorated by using a more fully Bayesian approach [294].

Spike and slab priors

A natural alternative approach to sparse linear regression is to use binary indicators si

0, 1
}

∈ {

f (x; w) =

B
(cid:88)

i=1

siwiφi(x),

p(w) =

(cid:0)wi 0, σ2(cid:1)

(cid:89)

N

i

380

(18.1.54)

DRAFT April 24, 2015

Classiﬁcation

Algorithm 18.1 Evidence Procedure for Bayesian Logistic Regression

1: Initialise w and α.
2: while Not Converged do
3:

4:
5: end while

Find optimal w∗ by iterating equation (18.2.16), equation (18.2.15) to convergence.
Update α according to equation (18.2.9).

(cid:46) E-step
(cid:46) M-Step

so that only those weights wi with si = 1 will contribute to the function. One can specify a level of sparsity
by choosing a prior on the joint set p(s1, . . . , sB) that encourages only a small number of the si to be 1, the
rest being zero. This can be achieved using a product of Bernoulli distributions

p(s) =

B
(cid:89)

i=1

θsi (1

−

θ)1−si

where 0

θ

≤

≤

1 speciﬁes the prior level of sparsity. This is equivalent to using the original LPM

f (x; w) =

B
(cid:88)

i=1

wiφi(x),

with the ‘spike and slab’ weight prior

p(w

s) =
|

(cid:89)

(cid:8)si

i

N

(cid:0)wi 0, σ2(cid:1) + (1

si)δ (wi)(cid:9)

−

(18.1.55)

(18.1.56)

(18.1.57)

(cid:0)wi 0, σ2(cid:1) when si = 1. These
which places either a spike at 0 (when si = 0) or a broader Gaussian ‘slab’
two formulations are equivalent and result in a non-Gaussian posterior for the weights, for which approx-
imation methods are required. Popular approaches include Gibbs sampling, chapter(27), and variational
methods chapter(28).

N

18.2 Classiﬁcation

For the logistic regression model

w, x) = σ
p(c = 1
|

(cid:33)

wiφi(x)

(cid:32) B
(cid:88)

i=1

(18.2.1)

the maximum likelihood method returns only a single optimal w. To deal with the inevitable uncertainty
in estimating w we need to determine the posterior distribution. To do so we ﬁrst deﬁne a prior on the
weights p(w). As for the regression case, a convenient choice is a Gaussian

p(w

α) =
|

N

(cid:0)w 0, α−1I(cid:1) =

αB/2
(2π)B/2 exp

(cid:16)

(cid:17)

αwTw/2

−

(18.2.2)

where α is the inverse variance (precision). Given a dataset of input-class labels,
=
the parameter posterior is (assuming we make no model of the input distribution)

D

(xn, cn) , n = 1, . . . , N
{

,
}

p(w

α,
|

D

) =

p(

D|

w, α)p(w
p(

α)

D|

α)

|

=

1

p(

α)

D|

p(w

α)
|

N
(cid:89)

n=1

p(cn

xn, w)
|

(18.2.3)

Unfortunately, this distribution is not of any standard form and exactly inferring statistics such as the mean
is formally computationally intractable.

DRAFT April 24, 2015

381

18.2.1 Hyperparameter optimisation

Analogous to the regression case, hyperparameters such as α can be set by maximising the marginal likelihood

Classiﬁcation

(cid:90)

p(

α) =

D|

p(

w)p(w

D|

α)dw =
|

(cid:90) N
(cid:89)

n=1

xn, w)

p(cn

|

(cid:16) α
2π

(cid:17)B/2

exp

(cid:16)

α
2

−

(cid:17)

wTw

dw.

(18.2.4)

There are several approaches one could take to approximate this integral and below we discuss the Laplace
and a variational technique. Common to all approaches, however, is the form of the gradient, diﬀering only
in the statistics under an approximation to the posterior. For this reason we derive ﬁrst generic hyperpa-
rameter update formulae that apply under both approximations.

To ﬁnd the optimal α, we search for the zero derivative of log p(
regression case, and we immediately obtain

α). This is equivalent to the linear

D|

∂
∂α

log p(

α) =

D|

(cid:28)

1
2

wTw +

−

(cid:29)

B
α

p(w|α,D)

Setting the derivative to zero, an exact equation is that the optimal α satisﬁes

(cid:68)

wTw

(cid:69)

0 =

−

p(w|α,D)

+

B
α

One may now form a ﬁxed point equation

αnew =

B

wTw
(cid:104)

(cid:105)p(w|α,D)

(18.2.5)

(18.2.6)

(18.2.7)

The averages in the above expression cannot be computed exactly and are replaced by averages with respect
to an approximation of the posterior q(w
) =

). For a Gaussian approximation of the posterior, q(w

α,
|

D

α,
|

D

(w m, S)

N

(cid:68)

wTw

(cid:69)

= trace

(cid:16)(cid:68)

wwT(cid:69)

w

w

(cid:105) (cid:104)

(cid:105)

− (cid:104)

T +

w
(cid:104)

w

(cid:105) (cid:104)

T(cid:17)
(cid:105)

= trace (S) + mTm

αnew =

B
trace (S) + mTm

In this case the Gull-Mackay alternative ﬁxed point equation[135, 194] is

αnew =

αS
B
−
mTm

(18.2.8)

(18.2.9)

(18.2.10)

The hyperparameter updates (18.2.9) and (18.2.10) have the same form as for the regression model. The
mean m and covariance S of the posterior in the regression and classiﬁcation cases are however diﬀerent.
In the classiﬁcation case we need to approximate the mean and covariance, as discussed below.

18.2.2 Laplace approximation

The Laplace approximation, section(28.2) is a simple approximation made by ﬁtting a Gaussian locally
around the most probable point of the posterior. The weight posterior for the logistic regression model is
given by

p(w

α,
|

D

)

∝

where

exp (

E(w))

−

E(w) =

wTw

α
2

N
(cid:88)

n=1

−

(cid:16)

wThn(cid:17)

,

log σ

hn

≡

(2cn

−

1)φn

(18.2.11)

(18.2.12)

382

DRAFT April 24, 2015

Classiﬁcation

By approximating E(w) by a quadratic function in w, we obtain a Gaussian approximation q(w
p(w

, α). To do so we ﬁrst ﬁnd the minimum of E(w). Diﬀerentiating, we obtain

|D

E = αw

∇

N
(cid:88)

(1

n=1

−

−

σn)hn,

(cid:16)

wThn(cid:17)

σ

σn

≡

It is convenient to use a Newton method to ﬁnd the optimum. The Hessian matrix with elements

Hij

≡

∂2
∂wi∂wj

E(w)

is given by

H = αI +

N
(cid:88)

n=1
(cid:124)

σn(1

−

σn)φn (φn)T

(cid:123)(cid:122)
J

(cid:125)

, α) to

|D

(18.2.13)

(18.2.14)

(18.2.15)

Note that the Hessian is positive semideﬁnite (see exercise(18.4)) so that the function E(w) is convex (bowl
shaped), and ﬁnding a minimum of E(w) is numerically unproblematic. A Newton update then is

wnew = w

H−1

E

∇

−

Given a converged w, the posterior approximation is given by

q(w

|D

, α) =

N

(w m, S) ,

H−1

S

≡

(18.2.16)

(18.2.17)

where m = w∗ is the converged estimate of the minimum point of E(w) and H is the Hessian of E(w) at
this point.

Approximating the marginal likelihood

Using the Laplace approximation, the marginal likelihood is given by

p(

α) =

D|

(cid:90)

w

p(

w)p(w

D|

α) =
|

(cid:90)

N
(cid:89)

w

n=1

xn, w)

p(cn

|

(cid:16) α
2π

(cid:17)B/2

e− α

2 wTw

(cid:90)

∝

w

e−E(w)

(18.2.18)

For an optimum value m = w∗, we approximate the marginal likelihood using (see section(28.2))

log p(

α)

D|

≈

L(α)

≡ −

α
2

(w∗)Tw∗ +

(cid:88)

(cid:16)

(w∗)T hn(cid:17)

log σ

n

1
2

−

log det (αI + J) +

B
2

log α (18.2.19)

Given this approximation L(α) to the marginal likelihood, an alternative strategy for hyperparameter opti-
misation is to optimise L(α) with respect to α. By diﬀerentiating L(α) directly, the reader may show that
the resulting updates are in fact equivalent to using the general condition equation (18.2.6) under a Laplace
approximation to the posterior statistics.

Making predictions

Ultimately, our interest is to classify in novel situations, averaging over posterior weight uncertainty (as-
suming α is ﬁxed to a suitable value),

(cid:90)

x,
p(c = 1
|

D

) =

x, w)p(w
p(c = 1
|

|D

)dw

(18.2.20)

The B dimensional integrals over w cannot be computed analytically and numerical approximation is re-
quired. Using the Laplace approximation, we replace the exact posterior p(w
) with our Laplace’s method
Gaussian approximation q(w

(w m, S)

) =

|D

(cid:90)

x,
p(c = 1
|

)

D

≈

DRAFT April 24, 2015

|D

N

p(c = 1
|

x, w)q(w

)dw =

|D

(cid:90)

(cid:16)

σ

˜xTw

(cid:17)

(w m, S) dw

N

(18.2.21)

383

Classiﬁcation

−

−

Figure 18.5: Bayesian logistic regression using RBF
functions φi(x) = exp (cid:0)
mi)2(cid:1), placing the cen-
λ(x
tres mi on a subset of the training points. The green
points are training data from class 1, and the red
points are training data from class 0. The contours
represent the probability of being in class 1. The op-
timal value of α found by ML-II is 0.45 (λ is set by
hand to 2). See demoBayesLogRegression.m

where, for notational convenience, we write ˜x
φ(x). To compute the predictions it would appear that we
need to carry out an integral in B dimensions. However, since the term σ (cid:0)˜xTw(cid:1) depends on w via the scalar
product ˜xTw, we only require the integral over the one-dimensional projection h
˜xTw, see exercise(18.5).
That is

≡

≡

(cid:90)

x,
p(c = 1
|

)

D

≈

σ (h) q(h

x,
|

D

)dh

(18.2.22)

Since under the Laplace approximation w is Gaussian distributed, then so is the linear projection h

x,
q(h
|

D

) =

N

(cid:16)

h ˜xTm, ˜xTΣ˜x

(cid:17)

.

(18.2.23)

Predictions may then be made by numerically evaluating the one-dimensional integral over the Gaussian
distribution in h, equation (18.2.22). Fast approximate methods for computing this are discussed below.

Approximating the Gaussian average of a logistic sigmoid

Predictions under a Gaussian posterior approximation require the computation of

I

σ(x)

≡ (cid:104)

(cid:105)N (x µ,σ2)

(18.2.24)

Gaussian quadrature is an obvious numerical candidate[244]. An alternative is to replace the logistic sigmoid
by a suitably transformed erf function[195], the reason being that the Gaussian average of an erf function
is another erf function. Using a single erf, an approximation is1

σ(x)

1
2

≈

(1 + erf (νx))

(18.2.25)

These two functions agree at
. A reasonable criterion is that the derivatives of these two should
agree at x = 0 since then they have locally the same slope around the origin and have globally similar shape.
Using σ(0) = 0.5 and that the derivative is σ(0)(1

σ(0)), this requires

−∞

, 0,

∞

−

1
4

=

ν
√π ⇒

ν =

√π
4

(18.2.26)

A more accurate approximation can be obtained by taking a convex combination of scaled erf functions [22],
see logsigapp.m

1Note that the deﬁnition of the erf function used here is taken to be consistent with MATLAB, namely that erf(x) ≡
(cid:82) x
0 e−t2

dt. Other authors deﬁne it to be the cumulative density function of a standard Gaussian,

(cid:82) x
−∞ e− 1

2√
π

2 t2

dt.

2√
π

384

DRAFT April 24, 2015

−6−4−20246−6−4−202460.10.10.10.10.20.20.20.20.20.20.30.30.30.30.30.30.30.30.40.40.40.40.40.40.40.40.50.50.50.50.50.50.60.60.60.60.70.70.70.70.80.80.80.90.9Classiﬁcation

18.2.3 Variational Gaussian approximation

An alternative to Laplace’s method is to use a so-called variational method. Since the marginal likelihood is a
key quantity that can be used for example for hyperparameter selection, it is useful to have a lower bound on
the log marginal likelihood. Like in EM, one can then ﬁnd the best hyperparameters by maximising this lower
bound, rather than the likelihood itself. To keep the notation reasonably simple, we drop the conditioning
).
on hyperparameters throughout and attempt to ﬁnd a lower bound on the log marginal likelihood p(
One approach to obtaining a bound is based on the Kullback-Leibler divergence

D

KL(q(w)

p(w
|
) = p(

Since p(w

|D

))

|D

≥

0

log p(

D

)

≥ (cid:104)

D|
log p(

D
w)p(w)

D|

w)p(w)/p(

) we obtain

(cid:105)q(w) − (cid:104)

log q(w)

(cid:105)q(w)

(18.2.27)

(18.2.28)

which holds for any distribution q(w). The angled brackets
the explicit form for the logistic regression model, the right hand side is given by2

(cid:104)·(cid:105)q denote expectation with respect to q. Using

KL

B

≡

N
(cid:88)

(cid:68)

n=1

log σ

(cid:16)

snwT˜xn

(cid:17)(cid:69)

q(w) −

KL(q(w)

p(w))
|

(18.2.29)

To form a tractable lower bound, we need to choose a class of distributions q(w) for which the above expres-
sion can be evaluated, for example a Gaussian q(w) =
(w 0, Σ)
then KL(q(w)
p(w)) is straightforward. For numerical convenience, we parameterise the covariance of the
|
approximation using a Cholesky decomposition S = CTC for upper triangular C. This gives

(w m, S). For a Gaussian prior p(w) =

N

N

p(w)) = 4
2KL(q(w)
|

−

(cid:88)

i

log Cii

−

log det (Σ) + trace

(cid:16)

CTCΣ−1(cid:17)

+ mTΣ−1m + B

(18.2.30)

where B = dim (˜x). In computing the bound (18.2.29), the problematic remaining terms are

(cid:68)

log σ

(cid:16)

snwT˜xn

In

≡

(cid:17)(cid:69)

N (w m,S)

We deﬁne the activation an
(cid:0)an ˜µn, ˜σ2

p(an) =

≡
(cid:1) ,

n

N

snwT˜xn. Since w is Gaussian distributed, so is the activation,

˜µn = sn˜x(cid:62)

n m, ˜σ2

n = ˜x(cid:62)

n C(cid:62)C˜xn

(18.2.31)

(18.2.32)

so that

In =

(cid:104)

log σ (an)

(cid:105)N (an ˜µn,˜σ2

n) =

(cid:104)

log σ (˜µn + z˜σn)

(cid:105)z

(cid:104)·(cid:105)z denotes expectation with respect to the standard normal distribution z

where
In this
way In can be computed by any standard one-dimensional numerical integration method, such as Gaussian
quadrature. Since the bound (18.2.29) is therefore numerically accessible for any parameters m, C of the
approximating Gaussian, we may proceed to ﬁnd the optimal parameters by direct numerical maximisation
of the lower bound. The gradient of the bound with respect to m is

(z 0, 1).

∼ N

∂
KL
B
∂m

=

Σ−1m +

−

N
(cid:88)

n=1

sn˜xn (1

− (cid:104)

σ (˜µn + z˜σn)

(cid:105)z)

Similarly, one may show that

∂

KL
B
∂C

= C−T

−

CΣ−1 + C

N
(cid:88)

n=1

˜xn˜xT
n
˜σn (cid:104)

zσ (˜µn + z˜σn)

(cid:105)z

(18.2.33)

(18.2.34)

with the understanding that only the upper triangular part of the matrix expression is to be taken. One
may show that the bound equation (18.2.29) is concave in the mean and covariance[60]. These gradients
may then be used as part of a general purpose optimisation routine to ﬁnd the best m and C approximation
parameters.

2The generalisation to using φ(x) in the below follows from simply replacing x with φ(x) throughout.

DRAFT April 24, 2015

385

Classiﬁcation

Figure 18.6: The logistic sigmoid σ(x) = 1/(1 +
exp(
x)) (red) and a Gaussian lower bound (blue
dashed curve) with operating point ξ = 2.5.

−

18.2.4 Local variational approximation

An alternative to the KL bounding method above is to use a so-called local method that bounds each term
in the integrand. To lower bound

) =

p(

D

(cid:90) (cid:89)

n

(cid:16)

σ

snwT˜xn

(cid:17)

p(w)dw

we may bound the logistic sigmoid function[155], see ﬁg(18.6),

σ(x)

σ(ξ)

≥

(cid:18) 1
2

(x

ξ)

−

−

λ (ξ) (cid:0)x2

(cid:19)
,

ξ2(cid:1)

−

λ (ξ)

1
2ξ

≡

(cid:18)

σ (ξ)

(cid:19)

1
2

−

Hence

log σ

(cid:16)

snwT˜xn

(cid:17)

log σ (ξn) +

≥

snwT˜xn

1
2

1
2

ξn

−

−

(cid:20)(cid:16)

(cid:17)2

wT˜xn

λ (ξn)

(cid:21)

ξ2
n

−

Using this we can write

(cid:90)

p(

)

D

≥

dw

N

(w 0, Σ)

(cid:89)

e

n

log σ(ξn)+ 1

2 snwT ˜xn− 1

(cid:104)
2 ξn−λ(ξn)

(wT ˜xn)2

−ξ2
n

(cid:105)

(18.2.35)

(18.2.36)

(18.2.37)

(18.2.38)

For ﬁxed ξn, n = 1, . . . , N , the right hand side can be analytically integrated over w, resulting in the bound

(cid:17)

(cid:16)

ˆS

det

det (Σ)

+

1
2

1
2

log

log p(

)

D

≥

ˆmT ˆS−1 ˆm +

N
(cid:88)

(cid:20)

n=1

log σ(ξn)

(cid:21)

+ λ (ξn) ξ2
n

ξn
2

−

where

A = Σ−1 + 2

N
(cid:88)

n=1

λ (ξn) ˜xn˜xT

n , b =

N
(cid:88)

n=1

1
2

sn˜xn, ˆS

A−1,

ˆm

A−1b

≡

≡

(18.2.39)

(18.2.40)

The bound equation (18.2.39) may then be maximised with respect to the variational parameters ξn. At
convergence, we may take the Gaussian

as an approximation to the posterior.

(cid:17)
w ˆm, ˆS

(cid:16)

N

Relation to KL variational procedure

As an alternative to the numerical integration in the KL procedure described in section(18.2.3), we may
(cid:1)(cid:11)
instead bound the problematic term (cid:10)log σ (cid:0)snwT˜xn
q(w) in equation (18.2.29) using (18.2.37). As we dis-
cuss in section(28.5), this enables us to relate the KL and the local bounding procedures [60], showing that
for unconstrained Cholesky parameterisation C the KL approach results in a provably tighter lower bound
than the local approach.

As an illustration of these approximations, for two-dimensional data, D = 2, we plot contours of the
posterior and approximations in ﬁg(18.7) which again demonstrates the over-compact nature of the local
approximation. The Laplace approximation, which does not yield a bound, is plotted for comparison.

386

DRAFT April 24, 2015

−6−4−2024600.51Classiﬁcation

(a)

(b)

(c)

(d)

Figure 18.7: Posterior approximations for a two-dimensional Bayesian logistic regression posterior based on
N = 20 datapoints. (a) True posterior. (b) The Gaussian from the Jaakola-Jordan local approximation. (c)
The Laplace Gaussian approximation. (d) The variational Gaussian approximation from the KL approach.

18.2.5 Relevance vector machine for classiﬁcation

It is straightforward to apply the sparse linear model methods of section(18.1.7) to classiﬁcation. For
example, in adopting the RVM prior to classiﬁcation, as before we encourage individual weights to be small
using

p(w

α) =
|

(cid:89)

i

p(wi

αi),
|

p(wi

αi) =
|

N

(cid:18)

wi 0,

(cid:19)

1
αi

(18.2.41)

The only alterations to the previous ML-II approach is to replace equation (18.2.13) and equation (18.2.15)
with

E]i = αiwi

[
∇

(cid:88)

(1

n

−

−

σn)hn
i ,

H = diag (α) + J

(18.2.42)

These are used in the Newton update formula as before. The update equation for the α’s is given by

αnew

i =

1
i + Sii

m2

Similarly, the Gull-MacKay update is given by

αnew

i =

1

αiSii
−
m2
i

(18.2.43)

(18.2.44)

Running this procedure, one typically ﬁnds that many of the α’s tend to inﬁnity and the corresponding
weights are pruned from the system. The remaining weights typically correspond to basis functions (in the
RBF case) in the centres of mass of clusters of datapoints of the same class, see ﬁg(18.8). Contrast this with
the situation in SVMs, where the retained datapoints tend to be on the decision boundaries. The number
of training points retained by the RVM tends to be very small – smaller indeed than the number retained
in the SVM framework. Whilst the RVM does not support large margins, and hence may be a less robust
classiﬁer, it does retain the advantages of a probabilistic framework[295]. A potential critique of the RVM,
coupled with an ML-II procedure for learning the αi is that it is overly aggressive in terms of pruning.
Indeed, as one may verify running demoBayesLogRegRVM.m it is common to ﬁnd an instance of a problem
for which there exists a set of αi such that the training data can be classiﬁed perfectly; however, after using
ML-II, so many of the αi are set to zero that the training data can no longer be classiﬁed perfectly. An
alternative is to apply the spike-and-slab prior technique which suﬀers less from overly aggressive pruning,
although requires more sophisticated approximation methods.

18.2.6 Multi-class case

We brieﬂy note that the multi-class case can be treated by using the softmax function under a one-of-m
class coding scheme. The class probabilities are
eym
Σm(cid:48)eym(cid:48)

y) =
|

(18.2.45)

p(c = m

DRAFT April 24, 2015

387

p(w|D)−1012−3−2.5−2−1.5−1−0.50JJ−1012−3−2.5−2−1.5−1−0.50Laplace−1012−3−2.5−2−1.5−1−0.50VG−1012−3−2.5−2−1.5−1−0.50Classiﬁcation

(a)

(b)

Figure 18.8: Classiﬁcation using the RVM with RBF e−λ(x−m)2
, placing a basis function on a subset of the
training data points. The green points are training data from class 1, and the red points are training data
(b): The
from class 0. The contours represent the probability of being in class 1.
training points weighted by their relevance value 1/αn. Nearly all the points have a value so small that they
eﬀectively vanish. See demoBayesLogRegRVM.m

(a): Training points.

which automatically enforces the constraint (cid:80)
m p(c = m) = 1. Naively it would appear that for C classes,
the cost of the Laplace approximation scales as O (cid:0)C3N 3(cid:1). However, one may show by careful implementa-
tion that the cost may be reduced to only O (cid:0)CN 3(cid:1), analogous to the cost savings possible in the Gaussian
Process classiﬁcation model [318, 247].

18.3 Summary

•

•

A simple extension of linear regression is achieved by using a Gaussian prior on the parameters. Coupled
with the assumption of additive Gaussian noise on the output, the posterior distribution is Gaussian, for
which predictions can be computed readily.

In the case of classiﬁcation, no closed form Bayesian solution is obtained by using simple Gaussian priors
on the parameters, and approximations are required. The parameter posterior is nevertheless well-behaved
so that simple unimodal approximations may be adequate.

18.4 Code

demoBayesLinReg.m: Demo of Bayesian linear Regression
BayesLinReg.m: Bayesian linear regression
demoBayesLogRegRVM.m: Demo of Bayesian logistic regression (RVM)
BayesLogRegressionRVM.m: Bayesian logistic regression (RVM)
avsigmaGauss.m: Approximation of the Gaussian average of a logistic sigmoid
logsigapp.m: Approximation of the logistic sigmoid using mixture of erfs

388

DRAFT April 24, 2015

−8−6−4−202468−8−6−4−2024680.10.10.10.10.10.10.20.20.20.20.20.20.30.30.30.30.30.30.30.30.40.40.40.40.40.40.40.40.40.50.50.50.50.50.60.60.60.70.70.70.80.80.90.9−8−6−4−202468−8−6−4−2024680.10.10.10.10.10.10.20.20.20.20.20.20.30.30.30.30.30.30.30.30.40.40.40.40.40.40.40.40.40.50.50.50.50.50.60.60.60.70.70.70.80.80.90.9Exercises

18.5 Exercises

Exercise 18.1. This exercise concerns Bayesian regression.

1. Show that for f = wTx and p(w)

(w 0, Σ), that p(f

ﬁnd the mean and covariance of this Gaussian.

∼ N

x) is Gaussian distributed. Furthermore,

|

2. Consider a target point t = f + (cid:15), where (cid:15)

|
Exercise 18.2. A Bayesian Linear Parameter regression model is given by

∼ N

(cid:0)(cid:15) 0, σ2(cid:1). What is p(f

t, x)?

yn = wTφ(xn) + ηn

(18.5.1)

In vector notation y = (cid:0)y1, . . . , yN (cid:1)T this can be written

y = Φw + η

(18.5.2)
with ΦT = (cid:2)φ(x1), . . . , φ(xN )(cid:3) and η is a zero mean Gaussian distributed vector with covariance β−1I. An
expression for the marginal likelihood of a dataset is given in equation (18.1.19). We aim to ﬁnd a more
compact expression for the likelihood given the hyperparameters Γ

p(y1, . . . , yN

x1, . . . , xN , Γ)
|

(18.5.3)

Since yn is linearly related to w and p(w) =

N

(cid:0)w 0, α−1I(cid:1), then y is Gaussian distributed with mean

y

(cid:104)

(cid:105)

= Φ

w
(cid:104)

(cid:105)

= 0

and covariance matrix
(cid:68)

C =

yyT(cid:69)

y

y

(cid:105) (cid:104)

T =
(cid:105)

(Φw + η) (Φw + η)T(cid:69)
(cid:68)

− (cid:104)

1. Show that the covariance matrix can be expressed as

C =

1
β

I +

1
α

ΦΦT

2. Hence show that the log marginal likelihood can we written as

log p(y1, . . . , yN

x1, . . . , xN , Γ) =
|

−

1
2

log det (2πC)

1
2

−

yTC−1y

(18.5.4)

(18.5.5)

(18.5.6)

(18.5.7)

Exercise 18.3. Using exercise(18.2) as a basis, derive expression (18.1.41) for the log likelihood on a
validation set.

Exercise 18.4. Consider the function E(w) as deﬁned in equation (18.2.12).

1. Show that

the Hessian matrix which has elements,

Hij

≡

∂2
∂wi∂wj

(cid:34)

E(w) =

αI +

N
(cid:88)

n=1

σn (1

−

σn) φn (φn)T

(cid:35)

ij

2. Show that the Hessian is positive deﬁnite.

),
Exercise 18.5. Show that for any function f (
·

(cid:90)

(cid:90)

f (xTw)p(w)dw =

f (h)p(h)dh

(18.5.8)

(18.5.9)

where p(h) is the distribution of the scalar xTw. The signiﬁcance is that any high-dimensional integral of
the above form can be reduced to a one-dimensional integral over the distribution of the ‘ﬁeld’ h[22].

DRAFT April 24, 2015

389

Exercise 18.6. This exercise concerns Bayesian Logistic Regression. Our interest is to derive the op-
timal regularisation parameter α based on the Laplace approximation in section(18.2.2) to the marginal
log-likelihood given by

log p(

α)

D|

≈

L(α)

≡ −

α
2

(w)Tw +

(cid:88)

(cid:16)

(w)T hn(cid:17)

log σ

n

1
2

−

log det (αI + J) +

B
2

log α

(18.5.10)

Exercises

n log σ (cid:0)wThn(cid:1), as in equation
The Laplace procedure ﬁnds ﬁrst an optimal w∗ that minimises αwTw/2
(18.2.12) which will depend on the setting of α. Formally, therefore, in ﬁnding the α that optimises L(α)
we should make use of the total derivative formula

(cid:80)

−

dL
dα

=

∂L
∂α

(cid:88)

+

i

∂L
∂wi

∂wi
∂α

(18.5.11)

However, when evaluated at w = w∗, ∂L
∂w = 0. This means that in order to compute the derivative with
respect to α, we only need consider the terms with an explicit α dependence. Equating the derivative to zero
and using

∂ log det (M) = trace (cid:0)M−1∂M(cid:1)

show that the optimal α satisﬁes the ﬁxed point equation

αnew =

N

(w∗)Tw∗ + trace

(cid:16)

(αI + J)−1(cid:17)

(18.5.12)

(18.5.13)

390

DRAFT April 24, 2015

CHAPTER 19

Gaussian Processes

In Bayesian linear parameter models, we saw that the only relevant quantities are related to the scalar
product of data vectors. In Gaussian Processes we use this to motivate a prediction method that does not
necessarily correspond to any ‘parametric’ model of the data. Such models are ﬂexible Bayesian predictors.

19.1 Non-Parametric Prediction

Gaussian Processes are ﬂexible Bayesian models that ﬁt well within the probabilistic modelling framework.
In developing GPs it is useful to ﬁrst step back and see what information we need to form a predictor. Given
a set of training data

=

(xn, yn), n = 1, . . . , N
{

}

=

D

X ∪ Y

(19.1.1)

where xn is the input for datapoint n and yn the corresponding output (a continuous variable in the regression
case and a discrete variable in the classiﬁcation case), our aim is to make a prediction y∗ for a new input x∗.
In the discriminative framework no model of the inputs x is assumed and only the outputs are modelled,
conditioned on the inputs. Given a joint model

p(y1, . . . , yN , y∗

x1, . . . , xN , x∗) = p(
|

Y

, y∗

|X

, x∗)

(19.1.2)

we may subsequently use conditioning to form a predictor p(y∗
). In previous chapters we’ve made
much use of the i.i.d. assumption that each datapoint is independently sampled from the same generating
distribution. In this context, this might appear to suggest the assumption

x∗,
|

D

p(y1, . . . , yN , y∗

x1, . . . , xN , x∗) = p(y∗
|

|X

, x∗)

(cid:89)

p(yn

n

, x∗)

|X

(19.1.3)

However, this is clearly of little use since the predictive conditional is simply p(y∗
, x∗)
meaning the predictions make no use of the training outputs. For a non-trivial predictor we therefore need
to specify a joint non-factorised distribution over outputs.

, x∗) = p(y∗

|D

|X

19.1.1 From parametric to non-parametric

If we revisit our i.i.d. assumptions for parametric models, we used a parameter θ to make a model of the
input-output distribution p(y

x, θ). For a parametric model predictions are formed using
|

p(y∗

x∗,
|

)

D

∝

p(y∗, x∗,

) =

D

(cid:90)

θ

p(y∗,

, x∗,

Y

, θ)

X

(cid:90)

∝

θ

p(y∗,

θ, x∗,

Y|

)p(θ

X

(cid:8)(cid:8)(cid:8)
x∗,
)
X
|

(19.1.4)

391

θ

yN

xN

(a)

y1

x1

y∗

x∗

y1

x1

yN

xN

(b)

y∗

x∗

Non-Parametric Prediction

(a): A parametric model for predic-
Figure 19.1:
(b): The form of the
tion assuming i.i.d. data.
model after integrating out the parameters θ. Our
non-parametric model will have this structure.

Under the assumption that, given θ, the data is i.i.d., we obtain

p(y∗

x∗,
|

)

D

(cid:90)

∝

θ

p(y∗

x∗, θ)p(θ)
|

(cid:89)

p(yn

n

θ, xn)

|

(cid:90)

∝

θ

x∗, θ)p(θ

p(y∗

|

)

|D

where

p(θ

)

|D

∝

p(θ)

(cid:89)

n

p(yn

θ, xn)
|

After integrating over the parameters θ, the joint data distribution is given by

p(y∗,

x∗,

) =

X

Y|

(cid:90)

θ

p(y∗

x∗, θ)p(θ)
|

(cid:89)

p(yn

n

θ, xn)

|

(19.1.5)

(19.1.6)

(19.1.7)

which does not in general factorise into individual datapoint terms, see ﬁg(19.1). The idea of a non-
parametric approach is to specify the form of the dependencies p(y∗,
) without reference to an
explicit parametric model. One route towards a non-parametric model is to start with a parametric model
and integrate out the parameters.
In order to make this tractable, we use a simple linear parameter
predictor with a Gaussian parameter prior. For regression this leads to closed form expressions, although
the classiﬁcation case will require numerical approximation.

x∗,

Y|

X

19.1.2 From Bayesian linear models to Gaussian processes

To develop the GP, we brieﬂy revisit the Bayesian linear parameter model of section(18.1.1). For parameters
w and basis functions φi(x) the output is given by (assuming zero output noise)

y =

(cid:88)

i

wiφi(x)

If we stack all the y1, . . . , yN into a vector y, then we can write the predictors as

y = Φw

where Φ = (cid:2)φ(x1), . . . , φ(xN )(cid:3)T

is the design matrix. Assuming a Gaussian weight prior

p(w) =

N
the joint output

(w 0, Σw)

x) =

p(y

|

(cid:90)

w

δ (y

−

Φw) p(w)

is Gaussian distributed with mean

y
(cid:104)

= Φ

w

(cid:105)p(w) = 0

(cid:104)
(cid:105)
and covariance

(cid:68)

yyT(cid:69)

(cid:68)

wwT(cid:69)

= Φ

p(w)

ΦT = ΦΣwΦT =

(cid:16)

ΦΣw

1
2

(cid:17) (cid:16)

ΦΣw

(cid:17)T

1
2

.

(19.1.8)

(19.1.9)

(19.1.10)

(19.1.11)

(19.1.12)

(19.1.13)

392

DRAFT April 24, 2015

Non-Parametric Prediction

From this we see that the Σw can be absorbed into Φ using its Cholesky decomposition. In other words,
without loss of generality we may assume Σw = I. Hence, after integrating out the weights, the Bayesian
linear regression model induces a Gaussian distribution on any set of outputs y as

p(y

x) =
|

N

(y 0, K)

where the covariance matrix K depends on the training inputs alone via

[K]n,n(cid:48) = φ(xn)Tφ(xn(cid:48)

),

n, n(cid:48) = 1, . . . , N

(19.1.14)

(19.1.15)

Since the matrix K is formed as the scalar product of vectors, it is by construction positive semideﬁnite,
see section(19.4.2). After integrating out the weights, the only thing the model directly depends on is the
covariance matrix K. In a Gaussian process we directly specify the joint output covariance K as a function
of two inputs, rather than specifying a linear model with parameters w. Speciﬁcally we need to deﬁne the
n, n(cid:48) element of the covariance matrix for any two inputs xn and xn(cid:48)
. This is achieved using a covariance
function k(xn, xn(cid:48)

)

[K]n,n(cid:48) = k(xn, xn(cid:48)

)

(19.1.16)

The matrix K formed from a covariance function k is called the Gram matrix. The required form of the
function k(xn, xn(cid:48)
) is very special – when applied to create the elements of the matrix K it must produce a
positive deﬁnite matrix. We discuss how to create such covariance functions in section(19.3). One explicit
straightforward construction is to form the covariance function from the scalar product of the basis vector
φ(xn) and φ(xn(cid:48)
). For ﬁnite-dimensional φ this is known as a ﬁnite dimensional Gaussian Process. Given
any covariance function we can always ﬁnd a corresponding basis vector representation – that is, for any
GP, we can always relate this back to a parametric Bayesian LPM. However, for many commonly used
covariance functions, the basis functions corresponds to inﬁnite dimensional vectors. It is in such cases that
the advantages of using the GP framework are particularly evident since we would not be able to compute
eﬃciently with the corresponding inﬁnite dimensional parametric model.

19.1.3 A prior on functions

The nature of many machine learning applications is such that the knowledge about the true underlying
mechanism behind the data generation process is limited. Instead one relies on generic ‘smoothness’ assump-
tions; for example we might wish that for two inputs x and x(cid:48) that are close, the corresponding outputs
y and y(cid:48) should be similar. Many generic techniques in machine learning can be viewed as diﬀerent char-
acterisations of smoothness. An advantage of the GP framework in this respect is that the mathematical
smoothness properties of the functions are well understood, giving conﬁdence in the procedure.

For a given covariance matrix K, equation (19.1.14) speciﬁes a distribution on functions1 in the following
sense: we specify a set of input points x = (x1, . . . , xN ) and a N
N covariance matrix K. Then we draw
a vector y = (cid:0)y1, . . . , yN (cid:1) from the Gaussian deﬁned by equation (19.1.14). We can then plot the sampled
‘function’ at the ﬁnite set of points (xn, yn), n = 1, . . . , N . What kind of function does a GP correspond
to?
In ﬁg(19.2a) we show three sample functions drawn from a Squared Exponential covariance function
2 to 3. Each sampled function looks reasonably smooth.
deﬁned over 500 points uniformly spaced from
Conversely, for the Ornstein Uhlenbeck covariance function, the sampled functions ﬁg(19.2c) look locally
rough. These smoothness properties are related to the form of the covariance function, as discussed in
section(19.4.1).

×

−

Consider two scalar inputs, xi and xj and corresponding sampled outputs yi and yj. For a covariance func-
tion with large k(xi, xj), we expect yi and yj to be very similar since they are highly correlated. Conversely,
for a covariance function that has low k(xi, xj), we expect yi and yj to be eﬀectively independent. In general,
we would expect the correlation between yi and yj to decrease the further apart xi and xj are2.

1The term ‘function’ is potentially confusing since we do not have an explicit functional form for the input output-mapping.

For any ﬁnite set of inputs x1, . . . , xN the values for the ‘function’ are given by the outputs at those points y1, . . . , yN .

2For periodic functions one can have high correlation even if the inputs are far apart.

DRAFT April 24, 2015

393

Gaussian Process Prediction

The zero mean assumption implies that if we were to draw a large number of such ‘functions’, the mean
across these functions at a given point x tends to zero. Similarly, for any two points x and x(cid:48) if we compute
the sample covariance between the corresponding y and y(cid:48) for all such sampled functions, this will tend
to the covariance function value k(x, x(cid:48)). The zero-mean assumption can be easily relaxed by deﬁning a
mean function m(x) to give p(y
(y m, K). In many practical situations one typically deals with
‘detrended’ data in which such mean trends have been already removed. For this reason much of the
development of GPs in the machine learning literature is for the zero mean case.

x) =

N

|

19.2 Gaussian Process Prediction

For a dataset
y1, . . . , yN , y∗ given the joint inputs x1, . . . , xN , x∗. For convenience we write this as

and novel input x∗, a zero mean GP makes a Gaussian model of the joint outputs

x, y
{

=

D

}

p(y, y∗

x, x∗) =
|

N

(cid:0)y, y∗ 0N +1, K+(cid:1)

(19.2.1)

where 0N +1 is an N + 1 dimensional zero-vector. The covariance matrix K+ is a block matrix with elements

Kx,x

Kx,x∗

K+

≡

Kx∗,x

Kx∗,x∗

where Kx,x is the covariance matrix of the training inputs x = (cid:0)x1, . . . , xN (cid:1) – that is

[Kx,x]n,n(cid:48) ≡

k(xn, xn(cid:48)

),

n, n(cid:48) = 1, . . . , N

The N

1 vector Kx,x∗ has elements

×
[Kx,x∗]n,∗ ≡

k(xn, x∗)

n = 1, . . . , N

Kx∗,x is the transpose of the above vector. The scalar covariance is given by

Kx∗,x∗

≡

k(x∗, x∗)

(19.2.2)

(19.2.3)

(19.2.4)

The predictive distribution p(y∗
Gaussian distribution

x∗, x, y) is obtained by Gaussian conditioning using result(8.4), giving a
|

p(y∗

x∗,
|

D

) =

N

(cid:0)y∗ Kx∗,xK−1

x,xy, Kx∗,x∗

Kx∗,xK−1

x,xKx,x∗(cid:1)

−

(19.2.5)

GP regression is an exact method and there are no issues with local minima. Furthermore, GPs are attractive
since they automatically model uncertainty in the predictions. However, the computational complexity for
making a prediction is O (cid:0)N 3(cid:1) due to the requirement of performing the matrix inversion (or solving the
corresponding linear system by Gaussian elimination). This can be prohibitively expensive for large datasets
and a large body of research on eﬃcient approximations exists. A discussion of these techniques is beyond
the scope of this book, and the reader is referred to [247].

19.2.1 Regression with noisy training outputs

To prevent overﬁtting to noisy data it is useful to assume that a training output yn is the result of some
clean Gaussian process f n corrupted by independent additive Gaussian noise,

yn = f n + (cid:15)n, where

(cid:15)n

(cid:0)(cid:15)n 0, σ2(cid:1)

∼ N

394

(19.2.6)

DRAFT April 24, 2015

Gaussian Process Prediction

In this case our interest is to predict the clean signal f ∗ for a novel input x∗. Then the distribution
p(y, f ∗

x, x∗) is a zero mean Gaussian with block covariance matrix
|
(cid:18) Kx,x + σ2I Kx,x∗
Kx∗,x∗

Kx∗,x

(cid:19)

(19.2.7)

so that Kx,x is replaced by Kx,x + σ2I in forming the prediction, equation (19.2.5). This follows since

and, using the assumed independence of the noise with the clean signal, f m
two noise components, (cid:15)m

= n, we have

(cid:15)n, m

(19.2.8)

(cid:15)n and the independence of

⊥⊥

yn

(cid:104)

(cid:105)

=

f n
(cid:104)

(cid:105)

+

(cid:15)n

(cid:104)

(cid:105)

= 0 + 0

ymyn
(cid:104)

(cid:105)

=

=

⊥⊥
(f m + (cid:15)m) (f n + (cid:15)n)
(cid:105)
f mf n
f n(cid:15)m
(cid:104)
(cid:105)
(cid:104)
(cid:124) (cid:123)(cid:122) (cid:125)
k(xm,xn)

f m(cid:15)n
(cid:104)
(cid:105)
(cid:124) (cid:123)(cid:122) (cid:125)
(cid:104)f m(cid:105)(cid:104)(cid:15)n(cid:105)

+

+

(cid:104)

(cid:105)(cid:104)f n(cid:105)(cid:104)(cid:15)m(cid:105) +

(cid:15)m(cid:15)n
(cid:104)

(cid:105)σ2δm,n .

(19.2.9)

Using

(cid:15)m

(cid:104)

(cid:105)

= 0 we obtain the form in equation (19.2.7).

Example 19.1. Training data from a one-dimensional input x and one dimensional output y are plotted
in ﬁg(19.2b,d), along with the mean regression function ﬁt, based on two diﬀerent covariance functions.
Note how the smoothness of the prior translates into smoothness of the prediction. The smoothness of
the function space prior is a consequence of the choice of covariance function. Naively, we can partially
understand this by the behaviour of the covariance function at the origin, section(19.4.1). An intuitive
way to think about GP regression is to sample an ‘inﬁnite’ number of functions from the prior; these are
then ‘ﬁltered’ by how well they ﬁt the data by the likelihood term, leaving a posterior distribution on the
functions. See demoGPreg.m

The marginal likelihood and hyperparameter learning

For a set of N one-dimensional training inputs represented by the N
matrix K deﬁned on the inputs x = (cid:0)x1, . . . , xN (cid:1), the log marginal likelihood is

×

1 dimensional vector y and a covariance

log p(y

x) =
|

−

1
2

yTK−1y

1
2

−

log det (2πK)

(19.2.10)

One can learn any free (hyper)parameters of the covariance function by maximising the marginal likelihood.
For example, a squared exponential covariance function may have parameters λ, v0:

k(x, x(cid:48)) = v0 exp

(cid:26)

1
2

−

λ (cid:0)x

−

(cid:27)

x(cid:48)(cid:1)2

(19.2.11)

The λ parameter in equation (19.2.11) speciﬁes the appropriate length-scale of the inputs, and v0 the
variance of the function. The dependence of the marginal likelihood (19.2.10) on the parameters is typically
complex and no closed form expression for the maximum likelihood optimum exists; in this case one resorts
to numerical optimisation techniques such as conjugate gradients.

Vector inputs

For regression with vector inputs and scalar outputs we need to deﬁne a covariance as a function of the two
vectors, k(x, x(cid:48)). Using the multiplicative property of covariance functions, deﬁnition(19.3), a simple way
to do this is to deﬁne

k(x, x(cid:48)) =

k(xi, x(cid:48)
i)

(cid:89)

i

DRAFT April 24, 2015

(19.2.12)

395

(cid:54)
Covariance Functions

(a)

(c)

(b)

(d)

×

Figure 19.2: The input space from -2 to 3 is split evenly into 1000 points x1, . . . , x1000.
(a):
Three samples from a GP prior with Squared Exponential (SE) covariance function, λ = 2. The
1000 covariance matrix K is deﬁned using the SE kernel, from which the samples are drawn us-
1000
(b): Prediction based on training points. Plotted is the posterior
ing mvrandn(zeros(1000,1),K,3).
predicted function based on the SE covariance. The central line is the mean prediction, with standard errors
70. (c): Three samples from the Ornstein-Uhlenbeck
bars on either side. The log marginal likelihood is
(d): Posterior prediction for the OU covariance. The log marginal likelihood is
GP prior with λ = 2.
3, meaning that the SE covariance is much more heavily supported by the data than the rougher OU

≈

≈
covariance.

For example, for the squared exponential covariance function this gives

k(x, x(cid:48)) = e−(x−x(cid:48))2

(19.2.13)

though ‘correlated’ forms are possible as well, see exercise(19.6). We can generalise the above using param-
eters:

k(x, x(cid:48)) = v0 exp

(cid:40)

−

1
2

D
(cid:88)

l=1

(cid:41)

(cid:0)xl

λl

(cid:1)2

x(cid:48)
l

−

(19.2.14)

where xl is the lth component of x and θ = (v0, λ1, . . . , λD) are the parameters. The λl in equation (19.2.14)
allow a diﬀerent length scale on each input dimension and can be learned by numerically maximising the
marginal likelihood. For irrelevant inputs, the corresponding λl will become small, and the model will ignore
the lth input dimension.

19.3 Covariance Functions

Covariance functions k(x, x(cid:48)) are special in that they deﬁne elements of a positive deﬁnite matrix. These
functions are also referred to as ‘kernels’, particulary in the machine learning literature.

396

DRAFT April 24, 2015

−2−1.5−1−0.500.511.522.53−3−2.5−2−1.5−1−0.500.511.52−2−1.5−1−0.500.511.522.53−1.5−1−0.500.511.52−2−1.5−1−0.500.511.522.53−2.5−2−1.5−1−0.500.511.522.5−2−1.5−1−0.500.511.522.53−1.5−1−0.500.511.5Covariance Functions

Deﬁnition 19.1 (Covariance function). Given any collection of points x1, . . . , xM , a covariance function
k(xi, xj) deﬁnes the elements of an M

M matrix

×

[C]i,j = k(xi, xj)

such that C is positive semideﬁnite.

19.3.1 Making new covariance functions from old

The following rules (see exercise(19.1)) generate new covariance functions from existing covariance functions
k1, k2 [196],[247].

Deﬁnition 19.2 (Sum).

k(x, x(cid:48)) = k1(x, x(cid:48)) + k2(x, x(cid:48))

Deﬁnition 19.3 (Product).

k(x, x(cid:48)) = k1(x, x(cid:48))k2(x, x(cid:48))

Deﬁnition 19.4 (Product Spaces). For z =

(cid:18) x
y

(cid:19)
,

k(z, z(cid:48)) = k1(x, x(cid:48)) + k2(y, y(cid:48))

and

k(z, z(cid:48)) = k1(x, x(cid:48))k2(y, y(cid:48))

Deﬁnition 19.5 (Vertical Rescaling).

k(x, x(cid:48)) = a(x)k1(x, x(cid:48))a(x(cid:48))

for any function a(x).

Deﬁnition 19.6 (Warping and Embedding).

k(x, x(cid:48)) = k1

(cid:0)u(x), u(x(cid:48))(cid:1)

(19.3.1)

(19.3.2)

(19.3.3)

(19.3.4)

(19.3.5)

(19.3.6)

for any mapping x

→

u(x), where the mapping u(x) has arbitrary dimension.

A small collection of covariance functions commonly used in machine learning is given below. We refer the
reader to [247] and [117] for further popular covariance functions.

19.3.2 Stationary covariance functions

Deﬁnition 19.7 (Stationary Kernel). A kernel k(x, x(cid:48)) is stationary if the kernel depends only on the
separation x

x(cid:48). That is

−
k(x, x(cid:48)) = k(x

x(cid:48))

−

DRAFT April 24, 2015

(19.3.7)

397

Covariance Functions

x(cid:48). This means that for functions
For a stationary covariance function we may write k(d), where d = x
drawn from the GP, on average, the functions depend only on the distance between inputs and not on
the absolute position of an input. In other words, the functions are on average translation invariant. For
isotropic covariance functions, the covariance is deﬁned as a function of the distance
. Such covariance
functions are, by construction, rotationally invariant.

d
|

−

|

Deﬁnition 19.8 (Squared Exponential).

k(d) = exp

2(cid:17)

(cid:16)

d
|

− |

(19.3.8)

The Squared Exponential is one of the most common covariance functions. There are many ways to show
that this is a covariance function. An elementary technique is to consider

(cid:18)

xn(cid:48)(cid:17)(cid:19)

(cid:16)

−

−

1
2

xn

xn

exp

xn(cid:48)(cid:17)T (cid:16)

(cid:12)xn(cid:48)(cid:12)
The ﬁrst two factors on the right above form a kernel of the form φ(xn)φ(xn(cid:48)
(xn)T xn(cid:48)
nential, we have

) =
is the linear kernel. Taking the exponential and writing the power series expansion of the expo-

). In the ﬁnal term k1(xn, xn(cid:48)

(cid:16)

(xn)T xn(cid:48)(cid:17)

(19.3.9)

= exp

1
2 |

exp

exp

2
|

xn

1
2

−

−

−

(cid:12)
(cid:12)

(cid:18)

(cid:19)

(cid:18)

2(cid:19)
(cid:12)
(cid:12)

(cid:16)

exp

k1(xn, xn(cid:48)

(cid:17)
)

=

∞
(cid:88)

i=1

1
i!

1(xn, xn(cid:48)
ki

)

(19.3.10)

this can be expressed as a series of integer powers of k1, with positive coeﬃcients. By the product (with
itself) and sum rules above, this is therefore a kernel as well. We then use the fact that equation (19.3.9) is
the product of two kernels, and hence also a kernel.

Deﬁnition 19.9 (γ-Exponential).

k(d) = exp (

d

− |

γ),
|

0 < γ

2

≤

(19.3.11)

When γ = 2 we have the squared exponential covariance function. When γ = 1 this is the Ornstein-
Uhlenbeck covariance function.

Deﬁnition 19.10 (Mat´ern).

k(d) =

ν Kν (

d
|

|

d
|

)
|

where Kν is a modiﬁed Bessel function, ν > 0.

Deﬁnition 19.11 (Rational Quadratic).

k(d) =

(cid:16)

1 +

d
|

|

2(cid:17)−α

,

α > 0

(19.3.12)

(19.3.13)

Deﬁnition 19.12 (Periodic). For 1-dimensional x and x(cid:48), a stationary (and isotropic) covariance function
can be obtained by ﬁrst mapping x to the two dimensional vector u(x) = (cos(x), sin(x)) and then using
the SE covariance exp

u(x(cid:48)))2(cid:17)

(u(x)

[196]

(cid:16)

−
−
λ sin2 (cid:0)ω(x

−

x(cid:48)) = exp (cid:0)

k(x

−

See ﬁg(19.4a).

398

x(cid:48))(cid:1)(cid:1),

−

λ > 0

(19.3.14)

DRAFT April 24, 2015

Analysis of Covariance Functions

(a)

(b)

Figure 19.3: (a): Plots of the Gamma-Exponential covariance e−|x|γ
versus x. The case γ = 2 corresponds
to the SE covariance function. The drop in the covariance is much more rapid as a function of the separation
x for small γ, suggesting that the functions corresponding to smaller γ will be locally rough (though possess
(b): As for (a) but zoomed in towards the origin. For the SE
relatively higher long range correlation).
case, γ = 2, the derivative of the covariance function is zero, whereas the OU covariance γ = 1 has a ﬁrst
order contribution to the drop in the covariance, suggesting that locally OU sampled functions will be much
rougher than SE functions.

19.3.3 Non-stationary covariance functions

Deﬁnition 19.13 (Linear).

k(x, x(cid:48)) = xTx(cid:48)

Deﬁnition 19.14 (Neural Network).

(cid:32)

k(x, x(cid:48)) = arcsin

2xTx(cid:48)
(cid:112)

√1 + 2xTx

1 + 2x(cid:48)Tx(cid:48)

(cid:33)

(19.3.15)

(19.3.16)

The functions deﬁned by this covariance always go through the origin. To shift this, one may use the
(1, x) where the 1 has the eﬀect of a ‘bias’ from the origin. To change the scale of the bias
embedding x
and non-bias contributions one may use additional parameters x
(b, λx). The NN covariance function
can be derived as a limiting case of a neural network with inﬁnite hidden units[317], and making use of
exact integral results in [21]. See ﬁg(19.4b).

→

→

Deﬁnition 19.15 (Gibbs).

k(x, x(cid:48)) =

(cid:18) ri(x)ri(x(cid:48))
i (x) + r2
r2

i (x(cid:48))

(cid:89)

i

(cid:19) 1
2

(cid:32)

exp

−

i)2
x(cid:48)
(xi
−
i (x) + r2
r2
i (x(cid:48))

(cid:33)

for functions ri(x) > 0 [122].

19.4 Analysis of Covariance Functions

19.4.1 Smoothness of the functions

(19.3.17)

We examine local smoothness for a translation invariant kernel k(x, x(cid:48)) = k(x
points x and x(cid:48), separated by a small amount δ

x(cid:48)). For two one-dimensional
1, x(cid:48) = x + δ, the covariance between the outputs y and

−

(cid:28)

DRAFT April 24, 2015

399

00.511.522.5300.20.40.60.81  21.51.00.500.020.040.060.080.10.120.140.160.180.20.650.70.750.80.850.90.951  21.51.00.5Analysis of Covariance Functions

(a)

(b)

Figure 19.4: Samples from a GP prior for 500 x points uniformly placed from -20 to 20.
from the periodic covariance function exp (cid:0)
−
covariance function with bias b = 5 and λ = 1.

(a): Samples
(b): Samples from the Neural Network

2 sin2 0.5(x

x(cid:48))(cid:1).

−

y(cid:48) is, by Taylor expansion,

k(x, x(cid:48))

k(0) + δ

≈

dk
dx |

x=0 + O (cid:0)δ2(cid:1)

(19.4.1)

so that the change in the covariance at the local level is dominated by the ﬁrst derivative of the covariance
function. For the SE covariance k(x) = e−x2

,

dk
dx

=

2xe−x2

−

(19.4.2)

is zero at x = 0. This means that for the SE covariance function, the ﬁrst order change in the covariance is
zero, and only higher order δ2 terms contribute.

For the Ornstein-Uhlenbeck covariance, k(x) = e−|x|, the right derivative at the origin is

lim
δ→0

k(δ)

−
δ

k(0)

= lim
δ→0

1

e−δ
−
δ

=

1
−

(19.4.3)

where this result is obtained using L’Hˆopital’s rule. Hence for the OU covariance function, there is a ﬁrst
order negative change in the covariance; at the local level, this decrease in the covariance is therefore much
more rapid than for the SE covariance, see ﬁg(19.3). Since low covariance implies low dependence (in
Gaussian distributions), locally the functions generated from the OU process are rough, whereas they are
smooth in the SE case. A more formal treatment for the stationary case can be obtained by examining the
eigenvalue-frequency plot of the covariance function (spectral density), section(19.4.3). For rough functions
the density of eigenvalues for high frequency components is higher than for smooth functions.

19.4.2 Mercer kernels

Consider the function

k(x, x(cid:48)) = φ(x)Tφ(x(cid:48)) =

B
(cid:88)

s=1

φs(x)φs(x(cid:48))

(19.4.4)

where φ(x) is a vector with component functions φ1(x), φ2(x), . . . , φB(x). Then for a set of points x1, . . . , xP ,
we construct the matrix K with elements

[K]ij = k(xi, xj) =

B
(cid:88)

s=1

φs(xi)φs(xj)

(19.4.5)

We claim that the matrix K so constructed is positive semideﬁnite and hence a valid covariance matrix.
Recalling that a matrix is positive semideﬁnite if for any non zero vector z, zTKz
0. Using the deﬁnition

≥

400

DRAFT April 24, 2015

−20−15−10−505101520−2−1.5−1−0.500.511.5−20−15−10−505101520−3−2−101234Analysis of Covariance Functions

of K above we have

zTKz =

P
(cid:88)

i,j=1

ziKijzj =

B
(cid:88)

(cid:34) P

(cid:88)

(cid:35)
ziφs(xi)





P
(cid:88)



φs(xj)zj



=

s=1

i=1

(cid:124)

(cid:123)(cid:122)
γs

j=1

(cid:125)

(cid:124)

(cid:123)(cid:122)
γs

(cid:125)

B
(cid:88)

s=1

γ2
s ≥

0

(19.4.6)

Hence any function of the form equation (19.4.4) is a covariance function. We can generalise the Mercer
kernel to complex functions φ(x) using

k(x, x(cid:48)) = φ(x)Tφ†(x(cid:48))

(19.4.7)

where † represents the complex conjugate. Then the matrix K formed from inputs xi, i = 1, . . . , P is positive
semideﬁnite since for any real vector z,

zTKz =

B
(cid:88)

(cid:34) P

(cid:88)

(cid:35)
ziφs(xi)





P
(cid:88)

s=1

i=1

(cid:124)

(cid:123)(cid:122)
γs

j=1

(cid:125)

(cid:124)



s(xj)zj
φ†



=

(cid:123)(cid:122)
γ†
s

(cid:125)

B
(cid:88)

s=1

γs
|

2
|

0

≥

(19.4.8)

where we made use of the general result for a complex variable xx† =
write

2. A further generalisation is to
|

x
|

(cid:90)

k(x, x(cid:48)) =

f (s)φ(x, s)φ†(x(cid:48), s)ds

(19.4.9)

for real f (s)
assuming we can interchange the sum over the components of z with the integral over s), we obtain

0, and scalar complex functions φ(x, s). Then replacing summations with integration (and

≥

zTKz =

(cid:90)

(cid:34) P

(cid:88)

(cid:35)
ziφ(xi, s)

f (s)





P
(cid:88)

φ†(xj, s)zj

i=1

(cid:124)

(cid:123)(cid:122)
γ(s)

j=1

(cid:125)

(cid:124)

(cid:123)(cid:122)
γ†(s)

(cid:90)

ds =

f (s)

γ(s)
|

2 ds
|

≥

0





(cid:125)

(19.4.10)

19.4.3 Fourier analysis for stationary kernels

For a function g(x) with Fourier transform ˜g(s), we may use the inverse Fourier transform to write

g(x) =

(cid:90)

1
2π

˜g(s)e−ixsds

where i

√

≡

−

1. For a stationary kernel k(x) with Fourier transform ˜k(s), we can therefore write

x(cid:48)) =

k(x

−

(cid:90)

1
2π

˜k(s)e−i(x−x(cid:48))sds =

(cid:90)

1
2π

˜k(s)e−ixseix(cid:48)sds

(19.4.11)

(19.4.12)

which is of the same form as equation (19.4.9) where the Fourier transform ˜k(s) is identiﬁed with f (s)
and φ(x, s) = e−isx. Hence, provided the Fourier transform ˜k(s) is positive, the translation invariant kernel
x(cid:48)) is a covariance function. Bochner’s Theorem[247] asserts the converse that any translation invariant
k(x
covariance function must have such a Fourier representation.

−

Application to the squared exponential kernel
For the translation invariant squared exponential kernel, k(x) = e− 1

2 x2

, its Fourier transform is

˜k(s) =

(cid:90) ∞

−∞

e− 1

2 x2+isxdx = e− s2

2

(cid:90) ∞

−∞

e− 1

2 (x+is)2

dx = √2πe− s2

2

(19.4.13)

Hence the Fourier transform of the SE kernel is a Gaussian. Since this is positive the SE kernel is a covariance
function.

DRAFT April 24, 2015

401

Gaussian Processes for Classiﬁcation

c1

cN

y1

x1

yN

xN

c∗

y∗

x∗

Figure 19.5: GP classiﬁcation. The GP induces a Gaussian distribution on
the latent activations y1, . . . , yN , y∗, given the observed values of c1, . . . , cN .
The classiﬁcation of the new input x∗ is then given via the correlation
induced by the training points on the latent activation y∗.

19.5 Gaussian Processes for Classiﬁcation

Adapting the GP framework to classiﬁcation requires replacing the Gaussian regression term p(y
x) with a
corresponding classiﬁcation term p(c
x) for a discrete label c. To do so we will use the GP to deﬁne a latent
|
continuous space y which will then be mapped to a class probability using
(cid:90)

(cid:90)

|

x) =
p(c
|

y,(cid:26)x)p(y
p(c
|

x)dy =
|

y)p(y
p(c
|

x)dy
|

(19.5.1)

Given training data inputs
input x∗, then

X

= (cid:8)x1, . . . , xN (cid:9), corresponding class labels

= (cid:8)c1, . . . , cN (cid:9), and a novel

C

(cid:90)

p(c∗

x∗,
|

,

C

X

) =

p(c∗

y∗)p(y∗
|

,

C

|X

)dy∗

(19.5.2)

where

p(y∗

,

)

C

|X

∝
=

=

=

p(y∗,
(cid:90)

C|X
p(y∗,

)

,

(cid:90)

p(

C|Y
(cid:90) (cid:40) N
(cid:89)

Y
C|X
)p(y∗,

p(cn

yn)

|

n=1

(cid:124)

(cid:123)(cid:122)
class mapping

, x∗)d

Y|X
(cid:41)

Y
, x∗)d

Y

x1, . . . , xN , x∗)
p(y1, . . . , yN , y∗
|
(cid:124)
(cid:125)
(cid:123)(cid:122)
Gaussian Process

dy1, . . . , dyN

(cid:125)

(19.5.3)

,

The graphical structure of the joint class and latent y distribution is depicted in ﬁg(19.5.) The posterior
marginal p(y∗
) is the marginal of a Gaussian Process, multiplied by a set of non-Gaussian maps from the
latent activations to the class probabilities. We can reformulate the prediction problem more conveniently
as follows:
p(y∗,

(19.5.4)

p(y∗,

p(y∗

, x∗,

x∗,

x∗,

)p(

|X

C

)

)

)

,

,

,

Y|

X

C

∝

Y

C|

X

∝

|Y

X

Y|C

X

where

p(

)

,

X

∝

Y|C

(cid:40) N
(cid:89)

n=1

(cid:41)

p(cn

yn)
|

p(y1, . . . , yN

x1, . . . , xN )
|

(19.5.5)

In equation (19.5.4) the term p(y∗
) does not contain any class label information and is simply a
conditional Gaussian. The advantage of the above description is that we can therefore form an approximation
) and then reuse this approximation in the prediction for many diﬀerent x∗ without needing to
to p(
rerun the approximation[318, 247].

, x∗,

Y|C

|Y

X

X

,

19.5.1 Binary classiﬁcation

For the binary class case we will use the convention that c
for a real valued activation y. A convenient choice is the logistic transfer function3

. We therefore need to specify p(c = 1
|

1, 0
}

∈ {

y)

σ(x) =

1
1 + e−x

(19.5.6)

3We will also refer to this as ‘the sigmoid function’. More strictly a sigmoid function refers to any ‘s-shaped’ function (from

the Greek for ‘s’).

402

DRAFT April 24, 2015

Gaussian Processes for Classiﬁcation

Then

y) = σ ((2c
p(c
|

−

1) y)

(19.5.7)

−

x) = 1

is a valid distribution since σ(
σ(x), ensuring that the sum over the class states is 1. A diﬃculty
is that the non-linear class mapping term makes the computation of the posterior distribution equation
(19.5.3) diﬃcult since the integrals over y1, . . . , yN cannot be carried out analytically. There are many ap-
proximate techniques one could apply in this case, including variational methods analogous to that described
in section(18.2.3). Below we describe the straightforward Laplace method, leaving the more sophisticated
methods for further reading[247].

−

19.5.2 Laplace’s approximation

In the Laplace method, section(28.2), we approximate the non-Gaussian distribution (19.5.5) by a Gaussian4
q(

),

,

Y|C

X

p(

Y|C

)

,

X

≈

q(

Y|C

)

,

X

From equation (19.5.4), approximate predictions can then be formed through the joint Gaussian

p(y∗,

x∗,

Y|

,

)

C

X

≈

p(y∗

|Y

, x∗,

)q(

)

,

X

Y|C

X

(19.5.8)

(19.5.9)

We will then marginalise this Gaussian to ﬁnd the Gaussian distribution on y∗ alone, and then use this to
form the prediction via p(c∗
y∗).
|
For compactness we deﬁne the class label vector, and outputs

c = (cid:0)c1, . . . , cN (cid:1)T

,

y = (cid:0)y1, . . . , yN (cid:1)T

(19.5.10)

and notationally drop the (ever present) conditioning on the inputs x. Also for convenience, we deﬁne

σ = (cid:0)σ(y1), . . . , σ(yN )(cid:1)T

Finding the mode

(19.5.11)

The Laplace approximation, section(28.2), corresponds to a second order expansion around the mode of the
distribution. Our task is therefore to ﬁnd the maximum of

p(y

c)

|

∝

p(y, c) = exp (Ψ(y))

where

Ψ(y) = cTy

N
(cid:88)

n=1

−

log(1 + eyn)

1
2

−

yTK−1

x,xy

1
2

−

log det (Kx,x)

N
2

−

log 2π

(19.5.12)

(19.5.13)

The maximum needs to be found numerically, and it is convenient to use the Newton method [132, 318, 247].

ynew = y

(
∇∇

−

Ψ)−1

Ψ

∇

Diﬀerentiating equation 19.5.13 with respect to y we obtain the gradient and Hessian

Ψ = (c

∇

Ψ =

σ)
−
−
K−1
x,x −

D

K−1

x,xy

∇∇

−
where the ‘noise’ matrix is given by

D = diag (σ1(1

σ1), . . . , σN (1

σN ))

−

−

(19.5.14)

(19.5.15)

(19.5.16)

(19.5.17)

4Some authors use the term Laplace approximation solely for approximating an integral. Here we use the term to refer to a

Gaussian approximation of a non-Gaussian distribution.

DRAFT April 24, 2015

403

Gaussian Processes for Classiﬁcation

Using these expressions in the Newton update, (19.5.14) gives

ynew = y + (cid:0)K−1

x,x + D(cid:1)−1 (cid:0)c

σ

−

−

K−1

x,xy(cid:1)

To avoid unnecessary inversions, one may rewrite this in the form

ynew = Kx,x (I + DKx,x)−1 (Dy + c

σ)

−

(19.5.18)

(19.5.19)

For an initial guess of y, we repeatedly apply equation (19.5.19) until convergence. This is guaranteed to
converge since the Hessian is negative deﬁnite and has a unique maximum.

Making predictions

Given a converged solution ˜y we have found a Gaussian approximation

, x∗,

q(y

|X

) =

C

N

(cid:16)

y ˜y, (cid:0)K−1

x,x + D(cid:1)−1(cid:17)

(19.5.20)

We now have Gaussians for p(y∗

y) and q(y
|

|X

, x∗,

C

) in equation (19.5.9). Predictions are then made using

(cid:90)

p(y∗

x∗,
|

,

)

C

X

≈

p(y∗

x∗,
|

X

, y)q(y

, x∗,

)dy

C

|X

where, by conditioning, section(8.4.2),

p(y∗

y, x∗,
|

X

) =

N

(cid:0)y∗ Kx∗,xK−1

x,xy, Kx∗,x∗

Kx∗,xK−1

x,xKx,x∗(cid:1)

−

We can also write this as a linear system

y∗ = Kx∗,xK−1

x,xy + η

(19.5.21)

(19.5.22)

(19.5.23)

(cid:0)η 0, Kx∗,x∗
Kx∗,xK−1
where η
ing over y and the noise η, we obtain

∼ N

−

x,xKx,x∗(cid:1). Using equation (19.5.23) and equation (19.5.20) and averag-

x∗,

y∗
(cid:104)

|

,

X

C(cid:105) ≈

Kx∗,xK−1

x,x ˜y = Kx∗,x (c

σ (˜y))

−

Similarly, the variance of the latent prediction is

var(y∗

x∗,
|

,

)

C

X

Kx∗,xK−1
x,x

≈
= Kx∗,x∗

(cid:0)K−1

K−1

x,x + D(cid:1)−1
(cid:0)Kx,x + D−1(cid:1)−1

Kx∗,x

Kx,x∗

x,xKx,x∗ + Kx∗,x∗

−

Kx∗,xK−1

x,xKx,x∗

−

where the last line is obtained using the Matrix Inversion Lemma, deﬁnition(A.11).

The class prediction for a new input x∗ is then given by

x∗,
p(c∗ = 1
|

,

)

C

X

≈ (cid:104)

σ(y∗)

(cid:105)N (y∗ (cid:104)y∗(cid:105),var(y∗))

(19.5.24)

(19.5.25)

(19.5.26)

(19.5.27)

In order to calculate the Gaussian integral over the logistic sigmoid function, we use an approximation of
the sigmoid function based on the error function erf(x), see section(18.2.2) and avsigmaGauss.m.

Example 19.2. An example of binary classiﬁcation is given in ﬁg(19.6) in which one-dimensional input
training data with binary class labels is plotted along with the class probability predictions on a range
γ) + 0.001δij. The
of input points.
square exponential covariance produces a smoother class prediction than the Ornstein-Uhlenbeck covariance
function. See demoGPclass1D.m and demoGPclass.m.

xi
In both cases the covariance function is of the form 2exp (
|

xj

−

|

404

DRAFT April 24, 2015

Gaussian Processes for Classiﬁcation

(a)

(b)

Figure 19.6: Gaussian Process classiﬁcation. The x-axis are the inputs, and the class is the y-axis. Green
x∗) for
points are training points from class 1 and red from class 0. The dots are the predictions p(c = 1
|
points x∗ ranging across the x axis.
(b): OU covariance
(γ = 1). See demoGPclass1D.m.

(a): Square exponential covariance (γ = 2).

Marginal likelihood

The marginal likelihood is given by

(cid:90)

p(

C|X

) =

p(

C|Y

Y

)p(

Y|X

)

Under the Laplace approximation, the marginal likelihood is approximated by

(cid:90)

p(

C|X

)

≈

y

exp (Ψ(˜y))exp

(cid:18)

−

1
2

(y

−

˜y)T A (y

(cid:19)

˜y)

−

where A =

−∇∇

Ψ. Integrating over y gives

log p(

C|X

)

≈

log q(

C|X

)

where

log q(

C|X

) = Ψ(˜y)

= Ψ(˜y)

log det (2πA)

log det (cid:0)K−1

x,x + D(cid:1) +

N
2

log 2π

−

−

1
2
1
2
N
(cid:88)

−

n=1

= cT ˜y

log(1 + exp (˜yn))

1
2

−

˜yTK−1

x,x ˜y

1
2

−

log det (I + Kx,xD)

(19.5.28)

(19.5.29)

(19.5.30)

(19.5.31)

(19.5.32)

(19.5.33)

where ˜y is the converged iterate of equation 19.5.18. One can also simplify the above using that at conver-
gence K−1

σ(y).

x,x ˜y = c

−

19.5.3 Hyperparameter optimisation

The approximate marginal likelihood can be used to assess hyperparameters θ of the kernel. A little care is
required in computing derivatives of the approximate marginal likelihood since the optimum ˜y depends on
θ. We use the total derivative formula [25]

d
dθ

∂
∂θ

log q(

C|X

) =

∂
∂θ

log q(

C|X

(cid:88)

) +

i

∂
∂ ˜yi

log q(

C|X

)

d
dθ

˜yi

log q(

C|X

) =

(cid:104)

1
2

∂
∂θ

−

yTK−1

x,xy + log det (I + Kx,xD)

(cid:105)

DRAFT April 24, 2015

(19.5.34)

(19.5.35)

405

−10−8−6−4−2024681000.20.40.60.81−10−8−6−4−2024681000.20.40.60.81Gaussian Processes for Classiﬁcation

which can be evaluated using the standard results for the derivative of a matrix determinant and inverse.
Since the derivative of Ψ is zero at ˜y, and noting that D depends explicitly on ˜y,

∂
∂ ˜yi

log q(

C|X

) =

1
2

∂
∂ ˜yi

−

log det (I + Kx,xD)

The implicit derivative is obtained from using the fact that at convergence

˜y = Kx,x (c

σ(y))

−

to give

d
dθ

˜y = (I + Kx,xD)−1 ∂
∂θ

Kx,x (c

σ)

−

(19.5.36)

(19.5.37)

(19.5.38)

These results are substituted into equation (19.5.34) to ﬁnd an explicit expression for the derivative. See
exercise(19.7).

19.5.4 Multiple classes

The extension of the preceding framework to multiple classes is essentially straightforward and may be
achieved using the softmax function

p(c = m

y) =
|

eym
Σm(cid:48)eym(cid:48)
which automatically enforces the constraint (cid:80)
m p(c = m) = 1. Naively it would appear that for C classes,
the cost of implementing the Laplace approximation for the multiclass case scales as O (cid:0)C3N 3(cid:1). However,
one may show by careful implementation that the cost is only O (cid:0)CN 3(cid:1), and we refer the reader to [318, 247]
for details.

(19.5.39)

19.6 Summary

•

•

•

•

Gaussian Processes are powerful regression models and mathematically well understood.

The computational complexity of the prediction is cubic in the number of datapoints. This can be prohibitive
for large datasets and approximate implementations are required.

Gaussian Process classiﬁcation is analytically intractable and approximations are required. The posterior is
log concave so that simple unimodal approximation schemes can provide satisfactory results.

Many classical models in statistics and physics are related to Gaussian Processes. For example, one can
view linear dynamical systems, chapter(24), as specially constrained GPs.

Gaussian Processes have been heavily developed within the machine learning community over recent years
and ﬁnding eﬃcient approximations for both regression and classiﬁcation remains an active research topic.
We direct the interested reader to [262] and [247] for further discussion.

19.7 Code

GPreg.m: Gaussian Process Regression
demoGPreg.m: Demo GP regression
covfnGE.m: Gamma-Exponential Covariance function
GPclass.m: Gaussian Process Classiﬁcation
demoGPclass.m: Demo Gaussian Process Classiﬁcation

406

DRAFT April 24, 2015

(19.8.2)

(19.8.3)

Exercises

19.8 Exercises

Exercise 19.1. The Gram matrix K of a covariance function k(x, x(cid:48)) is positive semideﬁnite and hence
expressible as

Kij =

(cid:88)

l

uilujl

for suitable uil.

(19.8.1)

1. The Gram matrix of the sum of two covariance functions, k+(x, x(cid:48)) = k1(x, x(cid:48)) + k2(x, x(cid:48)), is of the

form

K+ = K1 + K2

for suitable Gram matrices K1 and K2. Show that k+(x, x(cid:48)) is a covariance function.

2. Consider the element-wise (Hadamard) product of two positive semideﬁnite matrices. That is

ijK2
ij

K∗

ij = K1
ij = (cid:80)

Using K1
the product of two covariance functions, k∗(x, x(cid:48)) = k1(x, x(cid:48))k2(x, x(cid:48)), is a covariance function.

m vimvjm, show that K∗ is positive semideﬁnite, and hence that

l uilujl and K2

ij = (cid:80)

Exercise 19.2. Show that the sample covariance matrix with elements Sij = (cid:80)N
¯xi = (cid:80)N

i /N , is positive semideﬁnite.

n=1 xn

n=1 xn

i xn

j /N

Exercise 19.3. Show that

k(x

x(cid:48)) = exp (cid:0)

−
is a covariance function.

−

(cid:12)
(cid:12)sin (cid:0)x

x(cid:48)(cid:1)(cid:12)
(cid:12)

(cid:1)

−

Exercise 19.4. Consider the function

f (xi, xj) = exp

(cid:18)

−

1
2

(xi

−

(cid:19)

xj)2

for one dimensional inputs xi. Show that

f (xi, xj) = exp

(cid:18)

−

(cid:19)

1
2

x2
i

exp (xixj) exp

(cid:19)

(cid:18)

1
2

x2
j

−

¯xi ¯xj, where

−

(19.8.4)

(19.8.5)

(19.8.6)

By Taylor expanding the central term, show that exp
sentation for the kernel f (xi, xj) as the scalar product of two inﬁnite dimensional vectors.

−

−

is a kernel and ﬁnd an explicit repre-

(cid:16)

1
2 (xi

xj)2(cid:17)

Exercise 19.5. Show that for a covariance function k1 (x, x(cid:48)) then

k(x, x(cid:48)) = f (cid:0)k1

(cid:0)x, x(cid:48)(cid:1)(cid:1)

is also a covariance function for any polynomial f (x) with positive coeﬃcients. Show therefore that
exp (k1 (x, x(cid:48))) and tan (k1 (x, x(cid:48))) are covariance functions.

Exercise 19.6. For a covariance function

k1(x, x(cid:48)) = f ((cid:0)x

show that

k2(x, x(cid:48)) = f ((cid:0)x

−

−

x(cid:48)(cid:1)T (cid:0)x

x(cid:48)(cid:1))

−

x(cid:48)(cid:1)T A (cid:0)x

x(cid:48)(cid:1))

−

is also a valid covariance function for a positive deﬁnite symmetric matrix A.

DRAFT April 24, 2015

(19.8.7)

(19.8.8)

(19.8.9)

407

Exercise 19.7. Show that the derivative of the Laplace approximation to the marginal likelihood equation
(19.5.34) is given by

Exercises

d
dθ

log q(

) =

C|X

1
2

where

yTK−1

x,xK(cid:48)

x,xK−1

x,xy

1
2

−

trace (cid:0)L−1K(cid:48)

x,xD(cid:1)

1
2

−

(cid:88)

i

MiiD(cid:48)
ii

(cid:2)L−1K(cid:48)

x,x (c

K(cid:48)

x,x ≡

∂
∂θ

Kx,x, L

I + Kx,xD, M

L−1Kx,x, D(cid:48)

diag

≡
Hint: You will need to make use of the general derivative results

≡

≡

(cid:18) ∂
∂y1

D11,

∂
∂y2

σ)(cid:3)

i (19.8.10)

−

(cid:19)

D22, . . .

(19.8.11)

∂
∂x

A−1 =

A−1

−

(cid:19)

A

(cid:18) ∂
∂x

A−1,

∂
∂x

log det (A) = trace

(cid:18)
A−1 ∂
∂x

(cid:19)

A

(19.8.12)

Exercise 19.8 (String kernel). Let x and x(cid:48) be two strings of characters and φs(x) be the number of times
that substring s appears in string x. Then

k(x, x(cid:48)) =

(cid:88)

s

wsφs(x)φs(x(cid:48))

(19.8.13)

is a (string kernel) covariance function, provided the weight of each substring ws is positive.

1. Given a collection of strings about politics and another collection about sport, explain how to form a

GP classiﬁer using a string kernel.

2. Explain how the weights ws can be adjusted to improve the ﬁt of the classiﬁer to the data and give an
explicit formula for the derivative with respect to ws of the log marginal likelihood under the Laplace
approximation.

Exercise 19.9 (Vector regression). Consider predicting a vector output y given training data
xn, yn, n = 1, . . . , n

. To make a GP predictor

=

X ∪ Y

(19.8.14)

{

}

p(y∗

x∗,
|

,

)

X

Y

we need a Gaussian model
p(y1, . . . , yN , y∗

x1, . . . , xn, x∗)
|
A GP requires then a speciﬁcation of the covariance c(ym
two diﬀerent input vectors. Show that under the dimension independence assumption

(19.8.15)
xn, xm) of the components of the outputs for

i , yn
j |

c(ym

xn, xm) = ci(ym

xn, xm)δi,j

(19.8.16)

i , yn
i |

where ci(ym
constructed independently, one for each output dimension i.

xn, xm) is a covariance function for the ith dimension, that separate GP predictors can be

i , yn
j |
i , yn
i |

Exercise 19.10. Consider the Markov update of a linear dynamical system, section(24.1),

xt = Axt−1 + ηt,

t

2

≥

where A is a given matrix and ηt is zero mean Gaussian noise with covariance (cid:10)ηi,tηj,t(cid:48)
p(x1) =

(x1 0, Σ).

N

1. Show that x1, . . . , xt is Gaussian distributed.

2. Show that the covariance matrix of x1, . . . , xt has elements

(cid:68)
xt(cid:48)xT
t

(cid:69)

= At(cid:48)−1Σ (cid:0)At−1(cid:1)T

+ σ2

min(t,t(cid:48))
(cid:88)

τ =2

At(cid:48)−τ (cid:0)At−τ (cid:1)T

and explain why a linear dynamical system is a (constrained) Gaussian Process.

3. Consider

(19.8.17)
(cid:11) = σ2δi,jδt,t(cid:48). Also,

(19.8.18)

yt = Bxt + (cid:15)t

(19.8.19)
(cid:11) = ν2δi,jδt,t(cid:48). The vectors (cid:15) are uncor-
where (cid:15)t is zero mean Gaussian noise with covariance (cid:10)(cid:15)i,t(cid:15)j,t(cid:48)
related with the vectors η. Show that the sequence of vectors y1, . . . , yt is a Gaussian Process with a
suitably deﬁned covariance function.

408

DRAFT April 24, 2015

CHAPTER 20

Mixture Models

Mixture models assume that the data is essentially clustered with each component in the mixture repre-
senting a cluster. In this chapter we view mixture models from the viewpoint of learning with missing data
and discuss some of the classical algorithms, such as EM training of Gaussian mixture models. We also
discuss more powerful models which allow for the possibility that an object can be a member of more than
one cluster. These models have applications in areas such as document modelling.

20.1 Density Estimation Using Mixtures

A mixture model is one in which a set of component models is combined to produce a richer model:

p(v) =

H
(cid:88)

h=1

h)p(h)

p(v

|

(20.1.1)

indexes each
The variable v is ‘visible’ or ‘observable’ and the discrete variable h with dom(h) =
component model p(v
h), along with its weight p(h). The variable v can be either discrete or continuous.
|
Mixture models have natural application in clustering data, where h indexes the cluster. This interpretation
can be gained from considering how to generate a sample datapoint v from the model equation (20.1.1).
First we sample a cluster h from p(h), and then draw a visible state v from p(v

1, . . . , H
{

}

h).
|

For a set of i.i.d. data v1, . . . , vN , a mixture model is of the form, ﬁg(20.1),

p(v1, . . . , vN , h1, . . . , hn) =

N
(cid:89)

n=1

p(vn

|

hn)p(hn)

from which the observation likelihood is given by

p(v1, . . . , vN ) =

N
(cid:89)

(cid:88)

n=1

hn

p(vn

hn)p(hn)
|

Finding the most likely assignment of datapoints to clusters is achieved by inference of

argmax
h1,...,hN

p(h1, . . . , hN

v1, . . . , vN )
|

(20.1.2)

(20.1.3)

(20.1.4)

which, thanks to the factorised form of the distribution is equivalent to computing arg maxhn p(hn
each datapoint.

|

vn) for

409

Expectation Maximisation for Mixture Models

θh

θv|h

hn

vn

N

Figure 20.1: A mixture model has a graphical representation as
a DAG with a single hidden node, h which indexes the mixture
component; given a setting of h, we then generate an observation
v from p(v
h). Independence of the N observations means the
model is replicated by the plate. The parameters are assumed
common across all datapoints.

|

In most applications, however, the ‘location’ of the clusters is a priori unknown and the parameters θ of
the model need to be learned to locate where these clusters are. Explicitly writing the dependence on the
parameters, the model for a single datapoint v and its corresponding cluster index h is

θ) = p(v
p(v, h
|

h, θv|h)p(h
|

θh)
|

(20.1.5)

The optimal parameters θv|h, θh of a mixture model are then most commonly set by maximum likelihood,

θopt = argmax

θ

p(v1, . . . , vN

|

θ) = argmax

(cid:89)

p(vn

θ

n

θ)

|

(20.1.6)

Numerically this can be achieved using an optimisation procedure such as gradient based approaches. Al-
ternatively, by treating the component indices as latent variables, one may apply the EM algorithm, as
described in the following section, which in many classical models produces simple update formulae.

Example 20.1. The data in ﬁg(20.2) naturally has two clusters and can be modelled with a mixture of
two two-dimensional Gaussians, each Gaussian describing one of the clusters. Here there is a clear visual
interpretation of the meaning of ‘cluster’, with the mixture model placing two datapoints in the same cluster
if they are both likely to be generated by the same model component. A priori we don’t know the location
of these two clusters and need to ﬁnd the parameters; the Gaussian mean and covariance for each cluster
mh, Ch, h = 1, 2. This can be achieved using maximum likelihood.

20.2 Expectation Maximisation for Mixture Models

Our task is to ﬁnd the parameters θ that maximise the likelihood of the observations v1, . . . , vN

p(v1, . . . , vN

θ) =

|

N
(cid:89)

(cid:40)

(cid:88)

n=1

h

(cid:41)

p(vn

h, θ)p(h
|

|

θ)

(20.2.1)

By treating the index h as a missing variable, mixture models can be trained using the EM algorithm,
section(11.2). There are two sets of parameters – θv|h for each component model p(v
h, θv|h) and θh for the
θh). According to the general approach for i.i.d. data of section(11.2), for the M-step
mixture weights p(h
|

|

(x m1, C1)+1/2

Figure 20.2: Two dimensional data which displays clusters. In this case a Gaussian
(x m2, C2) would ﬁt the data well for suitable
mixture model 1/2
means m1, m2 and covariances C1, C2. To the human eye, identifying these clusters
is an easy task; however, we will be interested in automatic methods that can cluster
potentially very high dimensional data, for which the cluster solutions may be far less
obvious.

N

N

410

DRAFT April 24, 2015

−3−2−10123−3−2−101234Expectation Maximisation for Mixture Models

we need to consider the energy term:

E(θ) =

=

N
(cid:88)

n=1
N
(cid:88)

n=1

log p(vn, h
|

(cid:104)

θ)

(cid:105)pold(h|vn)

(cid:10)log p(vn

h, θv|h)(cid:11)
|

pold(h|vn) +

N
(cid:88)

n=1

log p(h
(cid:104)

θh)
|

(cid:105)pold(h|vn)

(20.2.2)

(20.2.3)

and maximise (20.2.3) with respect to the parameters θv|h, θh, h = 1, . . . , H. The E-step results in the
update

pnew(h
|

vn)

∝

p(vn

h, θold
|

θold
h )
v|h)p(h
|

(20.2.4)

For initial parameters θ, one then updates the E and M-steps until convergence. This is a general approach
for training mixture models. Below we ﬂesh out how these updates work for some speciﬁc models.

20.2.1 Unconstrained discrete tables

Here we consider training a simple belief network p(v
in which the tables are unconstrained. This is a special case of the more general framework discussed in
section(11.2), although it is instructive to see how the EM algorithm can be derived for this speciﬁc case.

θh), dom(v) =
h, θv|h)p(h
|
|

, dom(h) =
}

1, . . . , V
{

1, . . . , H

{

}

M-step: p(h)

If no constraint is placed on p(h
θh) we may write the parameters as simply p(h), with the understanding
|
that 0

h p(h) = 1. Isolating the dependence of equation (20.2.3) on p(h) we obtain

1 and (cid:80)

p(h)

≤

≤

N
(cid:88)

n=1

log p(h)
(cid:104)

(cid:105)pold(h|vn) =

(cid:88)

h

log p(h)

N
(cid:88)

n=1

pold(h
|

vn)

(20.2.5)

We now wish to maximise equation (20.2.5) with respect to p(h) under the constraint that (cid:80)
hp(h) =
1. There are diﬀerent ways to perform this constrained optimisation. One approach is to use Lagrange
multipliers, see exercise(20.4). Another, arguably more elegant approach is to use the techniques described
in section(11.2) based on the similarity of the above to a Kullback-Leibler divergence. First we deﬁne the
distribution

˜p(h)

≡

(cid:80)
h

(cid:80)N

n=1 pold(h
(cid:80)N

vn)
|
n=1 pold(h

vn)
|

=

1
N

N
(cid:88)

n=1

pold(h

vn)
|

Then maximising equation (20.2.5) is equivalent to maximising

log p(h)

(cid:105)˜p(h) =

(cid:104)

log p(h)
(cid:104)
(cid:124)

(cid:105)˜p(h) − (cid:104)
(cid:123)(cid:122)
−KL(˜p|p)

log ˜p(h)

(cid:105)˜p(h)
(cid:125)

log ˜p(h)

+

(cid:104)

(cid:105)˜p(h)

(20.2.6)

(20.2.7)

log ˜p(h)

since equation (20.2.5) is related to
(cid:105)˜p(h) by the constant factor N . By subtracting the θ independent
term
p). This
(cid:105)˜p(h) from equation (20.2.7), we obtain the negative Kullback-Leibler divergence KL(˜p
|
means that the optimal p(h) is that distribution which minimises the Kullback-Leibler divergence. Optimally,
therefore p(h) = ˜p(h), so that the M-step is given by

log p(h)

(cid:104)

(cid:104)

pnew(h) =

1
N

N
(cid:88)

n=1

vn)
pold(h
|

DRAFT April 24, 2015

(20.2.8)

411

Expectation Maximisation for Mixture Models

θh

θvi|h

hn

vn
i

i = 1, . . . , D

n = 1, . . . , N

Figure 20.3: Mixture of a product of Bernoulli distributions. In
a Bayesian Treatment, a parameter prior is used. In the text we
simply set the parameters using maximum likelihood.

M-step : p(v

h)

|

The dependence of equation (20.2.3) on p(v

h) is
|

N
(cid:88)

n=1

(cid:10)log p (cid:0)vn

(cid:1)(cid:11)

h, θv|h
|

pold(h|vn) =

(cid:88)

N
(cid:88)

H
(cid:88)

v

n=1

h=1

I [vn = v] pold(h

vn) log p (v
|

h)
|

(20.2.9)

If the distributions p (cid:0)v
(cid:1) are not constrained, we can apply a similar Kullback-Leibler method, as
we did in section(11.2) and above for p(h). As an alternative to that approach, we describe here how the
h) is a distribution for each of the mixture
Lagrange method works in this case. We need to ensure that p(v
|
states h = 1, . . . , H. This can be achieved using a set of Lagrange multipliers, giving the Lagrangian:

h, θv|h

|

V
(cid:88)

N
(cid:88)

H
(cid:88)

L ≡

v=1

n=1

h=1

I [vn = v] pold(h

vn) log p (v
|

h) +
|

H
(cid:88)

h=1

λ(h)

(cid:32)
1

V
(cid:88)

v=1

−

(cid:33)

p(v

h)

|

(20.2.10)

Diﬀerentiating with respect to p(v = i

h = j) and equating to zero,
|

∂
L
h = j)
∂p(v = i
|

=

N
(cid:88)

n=1

I [vn = i] pold(h = j
p(v = i

vn = i)
|
h = j)
|

−

Solving this, we have

λ(h = j) = 0

(20.2.11)

h = j) =
p(v = i
|

1
λ(h = j)

N
(cid:88)

n=1

I [vn = i] pold(h = j

vn = i)
|

(20.2.12)

Using the normalisation requirement, (cid:80)
the above equation, summed over v. Hence the M-step update is given by

h = j) = 1 shows that λ(h = j) is just the numerator of
v p(v = i
|

(cid:80)N

I [vn = i] pold(h = j

h = j) =
|

(cid:80)V

i=1

n=1
(cid:80)N

n=1

I [vn = i] pold(h = j

vn = i)
|

vn = i)
|

pnew(v = i

E-step

According to the general EM procedure, section(11.2), optimally we set pnew(h
|

vn):
vn) = pold(h
|

pnew(h
|

vn) =

(cid:80)

pold(vn
h)pold(h)
|
h pold(vn

h)pold(h)
|

(20.2.13)

(20.2.14)

Equations (20.2.8,20.2.13,20.2.14) are repeated until convergence and guarantee that the likelihood equation
(20.2.1) cannot decrease. The initialisation of the tables and mixture probabilities can severely aﬀect the
quality of the solution found since the likelihood often has local optima. If random initialisations are used,
it is recommended to record the converged value of the likelihood itself, to see which parameters have the
higher likelihood. The solution with the highest likelihood is to be preferred.

412

DRAFT April 24, 2015

Expectation Maximisation for Mixture Models

20.2.2 Mixture of product of Bernoulli distributions

As an example mixture model, and one that can be used for practical clustering, see example(20.2), we
describe a simple mixture model that can be used to cluster binary vectors, v = (v1, . . . , vD)T, vi
.
}
The mixture of Bernoulli products1 model is given by

∈ {

0, 1

p(v) =

H
(cid:88)

h=1

p(h)

D
(cid:89)

i=1

p(vi

h)
|

(20.2.15)

where each term p(vi
1 . . . , H
p(h) and p(vi = 1
{
|
samples. In this case, for each datapoint n, we draw a cluster index h
each i = 1, . . . , D, we draw a state vi

h) is a Bernoulli distribution. The model is depicted in ﬁg(20.3) and has parameters
|
. One way to understand the model is to imagine drawing
}
from p(h). Then for

h), dom(h) =

from p(vi

1, . . . , H

∈ {

h).

}

0, 1
}

∈ {

|

EM training

To train the model under maximum likelihood it is convenient to use the EM algorithm which, as usual,
may be derived by writing down the energy:

(cid:88)
n (cid:104)

log p(vn, h)

(cid:105)pold(h|vn) =

(cid:88)

(cid:88)

n

i

log p(vn
i |

h)

(cid:104)

(cid:105)pold(h|vn) +

(cid:88)
n (cid:104)

log p(h)

(cid:105)pold(h|vn)

(20.2.16)

and then performing the maximisation over the table entries. From our general results, section(11.2),
we know that the M-step is equivalent to maximum likelihood when all variables are observed, and then
replacing the unobserved variables h by the conditional distribution p(h
v). Using this we can write down
|
immediately the M-step as

pnew(vi = 1
|

h = j) =

pnew(h = j) =

and the E-step by

(cid:80)
I [vn
n
i = 1] pold(h = j

i = 1] pold(h = j
I [vn

vn) + (cid:80)
n

vn)
|
i = 0] pold(h = j

|

(cid:80)
I [vn
n
(cid:80)
vn)
n pold(h = j
|
(cid:80)
vn)
n pold(h(cid:48)
h(cid:48)
|

(cid:80)

pnew(h = j

vn)
|

∝

pold(h = j)

D
(cid:89)

i=1

pold(vn
i |

h = j)

Equations (20.2.17,20.2.18) are iterated until convergence.

vn)
|

(20.2.17)

(20.2.18)

If an attribute i is missing for datapoint n, one needs to sum over the states of the corresponding vn
eﬀect of performing the summation for this model is simply to remove the corresponding factor p(vn
i |
the algorithm, see exercise(20.1).

i . The
h) from

Initialisation

The EM algorithm can be very sensitive to initial conditions. Consider the following initialisation: p(vi =
vn) = p(h = j).
1
|
The subsequent M-step updates are

h = j) = 0.5, with p(h) set arbitrarily. This means that at the ﬁrst iteration, pold(h = j
|

pnew(h) = pold(h),

pnew(vi

h = j) = pnew(vi
|

h = j(cid:48))
|

(20.2.19)

for any j, j(cid:48). This means that the parameters p(v
h) immediately become independent of h and the model
|
is numerically trapped in a symmetric solution. It makes sense, therefore, to initialise the parameters in a
non-symmetric fashion.

The model is readily extendable to more than two output class, and this is left as an exercise for the
interested reader.

1This is similar to the Naive Bayes classiﬁer in which the class labels are always hidden.

DRAFT April 24, 2015

413

Expectation Maximisation for Mixture Models

Figure 20.4: Top: Data from questionnaire responses. 150 people were each asked 5 questions, with ‘yes’
(white) and ‘no’ (black) answers. Gray denotes that the absence of a response (missing data). This training
data was generated by a two component product of Bernouilli model. Missing data was simulated by
randomly removing values from the dataset. Middle: the ‘correct’ h values sampled from the data. A ‘0’
denotes hn = 1 and ‘1’ denotes hn = 2. Bottom: The estimated p(hn

vn) values.
i = 2
|

Example 20.2 (Questionnaire). A company sends out a questionnaire containing a set of D ‘yes/no’
questions to a set of customers. The binary responses of a customer are stored in a vector v = (v1, . . . , vD)T.
In total N customers send back their questionnaires, v1, . . . , vN , and the company wishes to perform an
analysis to ﬁnd what kinds of customers it has. The company assumes there are H essential types of
customer for which the proﬁle of responses is deﬁned by only the customer type.

Data from a questionnaire containing 5 questions, with 150 respondents is presented in ﬁg(20.4). The data
has a large number of missing values. We assume there are H = 2 kinds of respondents and attempt to assign
each respondent into one of the two clusters. Running the EM algorithm on this data, with random initial
values for the tables, produces the results in ﬁg(20.5). Based on assigning each datapoint vn to the cluster
with maximal posterior probability hn = arg maxh p(h
h)p(h), the model
|
assigns 90% of the data to the correct cluster (which is known in this simulated case). See MIXprodBern.m.

vn), given a trained model p(v

|

Example 20.3 (Handwritten digits). We have a collection of 5000 handwritten digits which we wish to
28 = 784 dimensional binary vector. Using a mixture of
cluster into 20 groups, ﬁg(20.6). Each digit is a 28
Bernoulli products, trained with 50 iterations of EM (with random initialisation), the clusters are presented
in ﬁg(20.6). As we see, the method captures natural clusters in the data – for example, there are two kinds
of 1, one slightly more slanted than the other, two kinds of 4, etc.

×

(a)

(b)

414

Figure 20.5: EM learning of a mixture of
(a): True p(h) (left)
Bernoulli products.
and learned p(h) (right) for h = 1, 2. (b):
h) (right)
h) (left) and learned p(v
True p(v
|
for v = 1, . . . , 5. Each column pair corre-
sponds to p(vi
h = 1) (right column) and
|
p(vi
h = 2) (left column) with i = 1, . . . , 5.
|
The learned probabilities are reasonably
close to the true values.

|

DRAFT April 24, 2015

0501001501234505010015000.5105010015000.511200.10.20.30.40.50.60.70.80.911200.10.20.30.40.50.60.70.80.911234500.10.20.30.40.50.60.70.80.911234500.10.20.30.40.50.60.70.80.91The Gaussian Mixture Model

Figure 20.6: Top: a selection of 200 of the 5000 handwritten digits in the training set. Bottom: the trained
h) for h = 1, . . . , 20 mixtures. See demoMixBernoulliDigits.m.
cluster outputs p(vi = 1
|

20.3 The Gaussian Mixture Model

We turn our attention now to modelling continuous vector observations x (x plays the role of the ‘visible’
variable v previously). Gaussians are particularly convenient continuous mixture components since they
constitute ‘bumps’ of probability mass, aiding an intuitive interpretation of the model. As a reminder, a D
dimensional Gaussian distribution for a continuous variable x is
(cid:27)

(cid:26)

m, S) =

p (x

|

(cid:112)

1
det (2πS)

exp

−

(x

−

1
2

m)T S−1 (x

m)

−

where m is the mean and S is the covariance matrix. A mixture of Gaussians is then

p (x) =

H
(cid:88)

i=1

p(x

|

mi, Si)p(i)

where p(i) is the mixture weight for component i. For a set of data
i.i.d. assumption, the log likelihood is

X

= (cid:8)x1, . . . , xN (cid:9) and under the usual

log p(

X |

θ) =

N
(cid:88)

log

H
(cid:88)

n=1

i=1

p(i)

1
det (2πSi)

(cid:112)

exp

(cid:26)

1
2

−

(xn

−

mi)T S−1

i

(xn

(cid:27)

mi)

−

(20.3.3)

mi, Si, p(i), i = 1, . . . , H
{

. The optimal parameters θ can be set using maxi-
where the parameters are θ =
mum likelihood, bearing in mind the constraint that the Si must be symmetric positive deﬁnite matrices, in
addition to 0
i p(i) = 1. Gradient based optimisation approaches are feasible under a parame-
terisation of the Si (e.g. Cholesky decomposition) and p(i) (e.g. softmax) that enforce the constraints. An
alternative is the EM approach which in this case is particularly convenient since it automatically provides
parameter updates that ensure these constraints.

1, (cid:80)

p(i)

≤

≤

}

20.3.1 EM algorithm

From the general approach, section(11.2), for the M-step we need to consider the energy. Using the fact
that the component index i plays the role of the latent variable, the energy is given by

N
(cid:88)

(cid:104)

n=1

log p(xn, i)

(cid:105)pold(i|xn) =

N
(cid:88)

n=1

log [p(xn
(cid:104)

i)p(i)]
|

(cid:105)pold(i|xn)

Plugging in the deﬁnition of the Gaussian components, we have

(20.3.4)

N
(cid:88)

H
(cid:88)

n=1

i=1

xn)
pold(i
|

(cid:26)

1
2

−

(xn

−

mi)T S−1

i

(xn

mi)

−

1
2

−

log det (2πSi) + log p(i)

(20.3.5)

(cid:27)

The M-step requires the maximisation of the above with respect to mi, Si, p(i).

DRAFT April 24, 2015

415

(20.3.1)

(20.3.2)

M-step : optimal mi

Maximising equation (20.3.5) with respect to mi is equivalent to minimising

N
(cid:88)

H
(cid:88)

n=1

i=1

pold(i

xn) (xn
|

−

mi)T S−1

i

(xn

mi)

−

Diﬀerentiating with respect to mi and equating to zero we have

N
(cid:88)

n=1

2
−

pold(i
|

xn)S−1

i

(xn

−

mi) = 0

Hence, optimally,

mi =

(cid:80)N

xn)xn
n=1 pold(i
|
(cid:80)N
xn)
n=1 pold(i
|

By deﬁning the membership distribution

pold(n

i)

|

≡

(cid:80)N

xn)
pold(i
|
xn)
n=1 pold(i
|

The Gaussian Mixture Model

(20.3.6)

(20.3.7)

(20.3.8)

(20.3.9)

which quantiﬁes the membership of datapoints to cluster i, we can write equation (20.3.8) more compactly
as the update

mnew

i =

N
(cid:88)

n=1

pold(n

i)xn

|

(20.3.10)

Intuitively, this updates the mean for cluster i as the average over all datapoints weighted by their mem-
bership to cluster i.

M-step : optimal Si

Optimising equation (20.3.5) with respect to Si is equivalent to minimising

N
(cid:88)

(cid:68)

n=1

(∆n

i )TS−1

i ∆n

i −

log det (cid:0)S−1
i

(cid:1)(cid:69)

pold(i|xn)

mi. To aid the matrix calculus, we isolate the dependency on Si to give

where ∆n

xn

i ≡
(cid:32)

−

trace

S−1
i

N
(cid:88)

n=1

pold(i

xn)∆n
|

i (∆n

i )T

(cid:33)

−

log det (cid:0)S−1
i

(cid:1)

N
(cid:88)

n=1

pold(i

xn)
|

Diﬀerentiating with respect to S−1

i

and equating to zero, we obtain

N
(cid:88)

n=1

xn)∆n
pold(i
|

i (∆n

i )T

Si

−

N
(cid:88)

n=1

xn) = 0
pold(i
|

Using the membership distribution pold(n

i), the resulting update is given by

|

Snew

i =

N
(cid:88)

n=1

pold(n

i) (xn
|

−

mi) (xn

−

mi)T

(20.3.11)

(20.3.12)

(20.3.13)

(20.3.14)

As for the mean, this essentially softly ‘ﬁlters’ those datapoints that belong to cluster i and takes their
covariance. This update also ensures that Si is symmetric positive semideﬁnite. A special case is to
constrain the covariances Si to be diagonal for which the update is, see exercise(20.2),

Si =

N
(cid:88)

n=1

pold(n

i)diag
|

(cid:16)

(xn

−

mi) (xn

−

mi)T(cid:17)

416

(20.3.15)

DRAFT April 24, 2015

The Gaussian Mixture Model

Algorithm 20.1 EM training for the GMM
1: Initialise the centres mi, covariances Si and weights p(i) > 0, (cid:80)
2: while Likelihood not converged or termination criterion not reached do
3:

for n = 1, . . . N do

i p(i) = 1 i = 1, . . . , H.

(cid:110)

−

1
2 (xn

−

mi)T S−1

i

(xn

−

(cid:111)

mi)

det (Si)− 1

2

4:

5:

6:

7:

8:

9:

10:

11:

12:

13:

14:

15:

16:

for i = 1, . . . , H do

xn) = p(i) exp

xn) so that (cid:80)
|
xn)
i) = p(i
|
i) so that (cid:80)
|

n p(n

p(i
|
end for
Normalise p(i
p(n
|
end for
Normalise p(n
for i = 1, . . . , H do
mi = (cid:80)N
n=1 p(n
Si = (cid:80)N
n=1 p(n
(cid:80)N
p(i) = 1
N

i)xn
|
i) (xn
|
n=1 p(i

−
xn)
|

end for
L = (cid:80)N

n=1 log (cid:80)H

i=1 p(i)

17: end while

xn) = 1
i p(i
|

i) = 1 for each i.

|

mi) (xn

mi)T

−

1
√det(2πSi)

exp

(cid:110)

−

1
2 (xn

−

mi)T S−1

i

(xn

(cid:111)

mi)

−

(cid:46) responsibility

(cid:46) membership

(cid:46) M-step for means
(cid:46) M-step for covariances
(cid:46) M-step for weights

(cid:46) Log likelihood

where above diag (M) means forming a new matrix from the matrix M with zero entries except for the
diagonal entries of M. A more extreme case is that of isotropic Gaussians Si = σ2
i I. The reader may show
that the optimal update for σ2
in this case is given by taking the average of the diagonal entries of the
i
diagonally constrained covariance update,

σ2
i =

1
D

N
(cid:88)

n=1

pold(n

i)(xn
|

−

mi)2

M-step : optimal mixture coeﬃcients

(20.3.16)

If no constraint is placed on the weights, the update follows the general formula given in equation (20.2.8),

pnew(i) =

1
N

N
(cid:88)

n=1

xn)
pold(i
|

E-step

From the general theory, section(11.2), the E-step is given by

p(i

xn)
|

∝

p(xn

i)p(i).
|

Explicitly, this is given by the responsibility

xn) =

p(i
|

(cid:110)

p(i) exp

(cid:80)

i(cid:48) p(i(cid:48)) exp

−
(cid:110)

1
2 (xn
−
1
2 (xn

mi)T S−1

(xn

i
mi(cid:48))T S−1

−
i(cid:48) (xn

−

−

(cid:111)

mi)

mi(cid:48))

−

det (Si)− 1
(cid:111)

2

det (Si(cid:48))− 1

2

(20.3.17)

(20.3.18)

(20.3.19)

The above equations (20.3.8,20.3.14,20.3.17,20.3.19) are iterated in sequence until convergence. Note that
this means that the ‘new’ means are used in the update for the covariance, see algorithm(20.1).

The performance of EM for Gaussian mixtures can be strongly dependent on the initialisation, which we
In addition, constraints on the covariance matrix are required in order to ﬁnd sensible
discuss below.
solutions.

DRAFT April 24, 2015

417

(a) 1 iteration

(b) 50 iterations

The Gaussian Mixture Model

Figure 20.7: Training a mixture of 10 isotropic
Gaussians (a): If we start with large variances
for the Gaussians, even after one iteration, the
Gaussians are centred close to the mean of the
(b): The Gaussians begin to separate
data.
(c): One by one, the Gaussians move towards
appropriate parts of the data (d): The ﬁnal con-
verged solution. The Gaussians are constrained
to have variances greater than a set amount. See
demoGMMem.m.

(c) 125 iterations

(d) 150 iterations

20.3.2 Practical issues

Inﬁnite troubles

A diﬃculty arises with using maximum likelihood to ﬁt a Gaussian mixture model. Consider placing a
mi, Si) with mean mi set to one of the datapoints mi = xn. The contribution from that
component p(x
|
Gaussian for datapoint xn will be

p(xn

|

mi, Si) =

1
det (2πSi)

(cid:112)

e− 1

2 (xn−xn)TS−1

i

(xn−xn) =

1
det (2πSi)

(cid:112)

(20.3.20)

In the limit that the ‘width’ of the covariance goes to zero (the eigenvalues of Si tend to zero), this proba-
bility density becomes inﬁnite. This means that one can obtain a maximum likelihood solution by placing
zero-width Gaussians on a selection of the datapoints, resulting in an inﬁnite likelihood. This is clearly
undesirable and arises because, in this case, the maximum likelihood solution does not constrain the param-
eters in a sensible way. Note that this is not related to the EM algorithm, but a property of the maximum
likelihood method itself. All computational methods which aim to ﬁt unconstrained mixtures of Gaussians
using maximum likelihood therefore succeed in ﬁnding ‘reasonable’ solutions merely by getting trapped in
favourable local maxima. A remedy is to include an additional constraint on the width of the Gaussians,
ensuring that they cannot become too small. One approach is to monitor the eigenvalues of each covariance
matrix and if an update would result in a new eigenvalue smaller than a desired threshold, the update is
In GMMem.m we use a similar approach in which we constrain the determinant (the product of
rejected.
the eigenvalues) of the covariances to be greater than a desired speciﬁed minimum value. One can view
the formal failure of maximum likelihood in the case of Gaussian mixtures as a result of an inappropriate
prior. maximum likelihood is equivalent to MAP in which a ﬂat prior is placed on each matrix Si. This is
unreasonable since the matrices are required to be positive deﬁnite and of non-vanishing width. A Bayesian
solution to this problem is possible, placing a prior on covariance matrices. The natural prior in this case is
the Wishart Distribution, or a Gamma distribution in the case of a diagonal covariance.

Initialisation

A useful initialisation strategy is to set the covariances to be diagonal with large variances. This gives the
components a chance to ‘sense’ where data lies. An illustration of the performance of the algorithm is given
in ﬁg(20.7).

Symmetry breaking

If the covariances are initialised to large values, the EM algorithm appears to make little progress in the
beginning as each component jostles with the others to try to explain the data. Eventually one Gaussian

418

DRAFT April 24, 2015

The Gaussian Mixture Model

(a)

(b)

(c)

(a): A Gaussian mixture model with H = 4 components. There is a component (purple)
Figure 20.8:
with large variance and small weight that has little eﬀect on the distribution close to where the other three
components have appreciable mass. As we move further away this additional component gains in inﬂuence.
(b): The GMM probability density function from (a).
(c): Plotted on a log scale, the inﬂuence of each
Gaussian far from the origin becomes clearer.

component breaks away and takes responsibility for explaining the data in its vicinity, see ﬁg(20.7). The
origin of this initial jostling is an inherent symmetry in the solution – it makes no diﬀerence to the likelihood
if we relabel what the components are called. The symmetries can severely handicap EM in ﬁtting a large
number of component models in the mixture since the number of permutations increases dramatically with
the number of components. A heuristic is to begin with a small number of components, say two, for which
symmetry breaking is less problematic. Once a local broken solution has been found, more models are
included into the mixture, initialised close to the currently found solutions.
In this way, a hierarchical
scheme is envisaged. Another popular method for initialisation is to center the means to those found by the
K-means algorithm, see section(20.3.5) – however, this itself requires a heuristic initialisation.

20.3.3 Classiﬁcation using Gaussian mixture models

We can use GMMs as part of a class conditional generative model, in order to make a powerful classiﬁer.
Consider data drawn from two classes, c
1 from
class 1, and another GMM p(x
2 from class 2. This gives rise to two class-conditional
GMMs,

. We can ﬁt a GMM p(x

1) to the data

2) to the data

c = 2,
|

1, 2
}

c = 1,

∈ {

X

X

X

X

|

p(x

c,

|

c) =

X

H
(cid:88)

i=1

p(i
|

c)

N

(x mc

i , Sc
i )

For a novel point x∗, the posterior class probability is

x∗,
p(c
|

X

)

∝

p(x∗

c)p(c)

c,
|

X

(20.3.21)

(20.3.22)

where p(c) is the prior class probability. The maximum likelihood setting is that p(c) is proportional to the
number of training points in class c.

Overconﬁdent classiﬁcation

Consider a testpoint x∗ a long way from the training data for both classes. For such a point, the proba-
bility that either of the two class models generated the data is very low. Nevertheless, one probability will
be exponentially higher than the other (since the Gaussians drop exponentially quickly at diﬀerent rates),
meaning that the posterior probability will be conﬁdently close to 1 for that class which has a component
closest to x∗. This is an unfortunate property since we would end up conﬁdently predicting the class of
novel data that is not similar to anything we’ve seen before. We would prefer the opposite eﬀect that for
novel data far from the training data, the classiﬁcation conﬁdence drops and all classes become equally likely.

A remedy for this situation is to include an additional component in the Gaussian mixture for each class
, and let m be the mean
that is very broad. We ﬁrst collect the input data from all classes into a dataset
of all this data and S the covariance. Then for the model of each class c data we include an additional

X

DRAFT April 24, 2015

419

−10−8−6−4−2024681000.050.10.150.20.25−10−8−6−4−2024681000.050.10.150.20.25−10−8−6−4−20246810−180−160−140−120−100−80−60−40−200The Gaussian Mixture Model

Figure 20.9: Class conditional GMM train-
ing and classiﬁcation. (a): Data from two
diﬀerent classes. We ﬁt a GMM with two
components to the data from each class.
The (magenta) diamond is a test point far
from the training data we wish to classify.
(b): Upper subpanel are the class probabil-
ities p(c = 1
n) for the 40 training points,
|
and the 41st point, being the test point.
This shows how the test point is conﬁdently
placed in class 1. The lower subpanel are
the class probabilities but including the ad-
ditional large variance Gaussian term. The
class label for the test point is now maxi-
mally uncertain. See demoGMMclass.m.

(a)

(b)

Gaussian (dropping the notational dependency on

)

X

p(x

c) =
|

H
(cid:88)

i=1

˜pc
i N

(x mc

i , Sc

i ) + ˜pc

H+1N

where

(cid:26) pc
i
δ

˜pc
i ∝

i
H
i = H + 1

≤

(x m, λS)

(20.3.23)

(20.3.24)

where δ is a small positive value and λ inﬂates the covariance (we take δ = 0.0001 and λ = 10 in
demoGMMclass.m). The eﬀect of the additional component on the training likelihood is negligible since
it has small weight and large variance compared to the other components, see ﬁg(20.8). However, as we
move away from the region where the ﬁrst H components have appreciable mass, the additional component
gains in inﬂuence since it has a higher variance. If we include the same additional component in the GMM
for each class c then the inﬂuence of this additional component will be the same for each class, dominating
as we move far from the inﬂuence of the other components. For a point far from the training data the
likelihood will be roughly equal for each class since in this region the additional broad component dominates
each class with equal measure. The posterior distribution will then tend to the prior class probability p(c),
mitigating the deleterious eﬀect of a single GMM dominating when a testpoint is far from the training data.

Example 20.4. The data in ﬁg(20.9a) has a cluster structure for each class. Based on ﬁtting a GMM to
each of the two classes, a test point (diamond) far from the training data is conﬁdently classiﬁed as belonging
to class 1. This is an undesired eﬀect since we would prefer that points far from the training data are not
classiﬁed with any certainty. By including an additional large variance Gaussian component for each class
this has little eﬀect on the class probabilities of the training data, yet has the desired eﬀect of making the
class probability for the test point maximally uncertain, ﬁg(20.9b).

20.3.4 The Parzen estimator

The Parzen density estimator is formed by placing a ‘bump of mass’, ρ(x

xn), on each datapoint,
|

p(x) =

1
N

N
(cid:88)

n=1

xn)

ρ(x

|

A popular choice is (for a D dimensional x)

ρ(x

xn) =
|

N

(cid:0)x xn, σ2ID

(cid:1)

420

(20.3.25)

(20.3.26)

DRAFT April 24, 2015

−8−6−4−20246810−6−4−2024681012051015202530354000.20.40.60.81051015202530354000.20.40.60.81The Gaussian Mixture Model

Algorithm 20.2 K-means

1: Initialise the centres mi, i = 1, . . . , K.
2: while not converged do
3:

For each centre i, ﬁnd all the xn for which i is the nearest (in Euclidean sense) centre.
Call this set of points
Update the means

i. Let Ni be the number of datapoints in set

N

N

i.

4:

5:

mnew

i =

1
Ni

(cid:88)

xn

n∈Ni

6: end while

giving the mixture of Gaussians

p(x) =

1
N

N
(cid:88)

n=1

1
(2πσ2)D/2 exp

(cid:18)

−

1
2σ2 (x

−

(cid:19)

xn)2

(20.3.27)

There is no training required for a Parzen estimator – only the positions of the N datapoints need storing.
Whilst the Parzen technique is a reasonable and cheap way to form a density estimator, it does not enable
us to form any simpler description of the data. In particular, we cannot perform clustering since there is
no lower number of clusters assumed to underly the data generating process. This is in contrast to GMMs
trained using maximum likelihood on a ﬁxed number H

N of components.

≤

20.3.5 K-Means

Consider a mixture of K isotropic Gaussians in which each covariance is constrained to be equal to σ2I,
with mixture weights pi

0, (cid:80)

i pi = 1

≥

p(x) =

K
(cid:88)

i=1

pi

N

(cid:0)x mi, σ2I(cid:1)

(20.3.28)

Whilst the EM algorithm breaks down if a Gaussian component is allowed to set mi equal to a datapoint
0, by constraining all components to have the same variance σ2, the algorithm has a well deﬁned
with σ2
→
limit as σ2
0. The reader may show, exercise(20.3), that in this case the membership distribution equation
(20.3.9) becomes deterministic

→

p(n

i)

|

∝

(cid:26) 1
0

if mi is closest to xn
otherwise

(20.3.29)

In this limit the EM update (20.3.10) for the mean mi is given by taking the average of the points closest to
mi. This limiting and constrained GMM then reduces to the so-called K-means algorithm, algorithm(20.2).
Despite its simplicity the K-means algorithm converges quickly and often gives a reasonable clustering, pro-
vided the centres are initialised sensibly. See ﬁg(20.10).

K-means is often used as a simple form of data compression. Rather than sending the datapoint xn, one
sends instead the index of the centre to which it is associated. This is called vector quantisation and is
a form of lossy compression. To improve the quality, more information can be transmitted such as an
approximation of the diﬀerence between xn and the corresponding closest mean m, which can be used to
improve the reconstruction of the compressed datapoint.

20.3.6 Bayesian mixture models

Bayesian extensions include placing priors on the parameters of each model in the mixture, and also on
the component mixture weights. In most cases this will give rise to an intractable integral for the marginal
likelihood. Methods that approximate the integral include sampling techniques [105]. See also [119, 70] for
an approximate variational treatment focussed on Bayesian Gaussian mixture models.

DRAFT April 24, 2015

421

Mixture of Experts

(a): 550 datapoints
Figure 20.10:
clustered using K-means with 3 com-
ponents. The means are given by the
(b): Evolution of the
red crosses.
mean square distance to the nearest
centre against iterations of the algo-
rithm. The means were initialised to
be close to the overall mean of the
data. See demoKmeans.m.

(a)

(b)

20.3.7 Semi-supervised learning

In some cases we may know to which mixture component certain datapoints belong. For example, given a
collection of images that we wish to cluster, it may be that we already have cluster labels for a subset of
the images. Given this information we want to ﬁt a mixture model with a speciﬁed number of components
H and parameters θ. We write (vm
∗ ), m = 1, . . . , M for the M known datapoints and corresponding
components, and (vn, hn), n = 1, . . . , N for the remaining datapoints whose components hn are unknown.
We aim then to maximise the likelihood

∗ , hm

p(v1:M
∗

, v1:N

h1:M
∗

, θ) =

|

(cid:40)

(cid:89)

m

p(vm
∗ |

hm
∗ , θ)

(cid:41)(cid:40)

(cid:89)

(cid:88)

n

hn

(cid:41)

p(vn

hn, θ)p(hn)
|

(20.3.30)

If we were to lump all the datapoints together, this is essentially equivalent to the standard unsupervised
case, expect that some of the h are ﬁxed into known states. The only eﬀect on the EM algorithm is therefore
vm
in the terms p(h
in the known
∗ ) = δh,hm
|
|
state, resulting in a minor modiﬁcation of the standard algorithm, exercise(20.6).

vm
∗ ) for the labelled datapoints which are delta functions p(h

∗

20.4 Mixture of Experts

The mixture of experts model[162] is an extension to input-dependent mixture weights. For an output y (a
discrete class or continuous regression variable) and input x, this has the general form, see ﬁg(20.11),

p(y

x, W, U) =
|

H
(cid:88)

h=1

p(y

x, wh)p(h
|
|

x, U),

(20.4.1)

Here h indexes the mixture component. Each expert h has parameters wh with W = [w1, . . . , wH ] and
corresponding gating parameters uh with U = [u1, . . . , uH ]. Unlike a standard mixture model, the compo-
x, U) is dependent on the input x. This so-called gating distribution is conventionally
nent distribution p(h
|
taken to be of the softmax form

x, U) =
p(h
|

euT
hx
h euT
hx

(cid:80)

(20.4.2)

x, wh), each with a diﬀerent parameter
The idea is that we have a set of H predictive models (experts), p(y
|
wh, h = 1, . . . , H. How suitable model h is for predicting the output for input x is determined by the
alignment of input x with the weight vector uh. In this way the input x is softly assigned to the appropriate
experts.

Learning the parameters

Maximum likelihood training can be achieved using a form of EM. We will not derive the EM algorithm
for the mixture of experts model in full, merely pointing the direction along which the derivation would
continue. For a single datapoint x, the EM energy term is

log p(y
(cid:104)

x, wh)p(h
|

x, U)
|

(cid:105)p(h|x,Wold,Uold)

422

(20.4.3)

DRAFT April 24, 2015

−8−6−4−202468−4−20246811.522.533.544.550510152025Indicator Models

n = 1, . . . , N

xn

yn

hn

W

U

For regression a simple choice is

x, wh) =

p(y

|

N

(cid:16)

y xTwh, σ2(cid:17)

and for (binary) classiﬁcation

x, wh) = σ(xTwh)
p(y = 1
|

Figure 20.11: Mixture of experts model. The prediction of the
output yn (real or continuous) given the input xn is averaged
xn, whn). The expert hn is selected
over individual experts p(yn
by the gating mechanism with probability p(hn
xn, U), so that
some experts will be more responsible for predicting the output
for xn in ‘their’ part of the input space. The parameters W, U
can be learned by maximum likelihood after marginalising over
the hidden expert indices h1, . . . , hN .

|

|

(20.4.4)

(20.4.5)

In both cases computing the derivatives of the energy with respect to the parameters W is straightforward,
so that an EM algorithm is readily available. An alternative to EM is to compute the gradient of the
likelihood directly using the standard approach discussed in section(11.6).

A Bayesian treatment is to consider

x) = p(y
p(y, W, U, h
|

|

x, wh)p(h

x, u)p(W)p(U)
|

(20.4.6)

h p(wh), p(U) = (cid:81)
where it is conventional to assume p(W) = (cid:81)
h p(uh). The integrals required to calculate
the marginal likelihood are generally intractable and approximations are required. See [310] for a variational
treatment for regression and [44] for a variational treatment of classiﬁcation. An extension to Bayesian model
selection in which the number of experts is estimated is considered in [156].

20.5 Indicator Models

Indicator models generalise our previous mixture models by allowing a more general prior distribution on
cluster assignments. For consistency with the literature we use an indicator z, as opposed to a hidden
variable h, although they play the same role. A clustering model with parameters θ on the component
models and joint indicator prior p(z1:N ) takes the form, see ﬁg(20.12a),

p(v1:N

θ) =
|

(cid:88)

z1:N

p(v1:N

z1:N , θ)p(z1:N )
|

Since the zn indicate cluster membership,

p(v1:N

θ) =
|

(cid:88)

z1:N

p(z1:N )

N
(cid:89)

n=1

p(vn

zn, θ)
|

Below we discuss the role of diﬀerent indicator priors p(z1:N ) in clustering.

20.5.1 Joint indicator approach: factorised prior

Assuming prior independence of indicators,

p(z1:N ) =

N
(cid:89)

n=1

p(zn),

zn

1, . . . , K

∈ {

}

DRAFT April 24, 2015

(20.5.1)

(20.5.2)

(20.5.3)

423

z1

v1

zN

vN

z1

v1

θ

(a)

zN

vN

π

θ

(b)

π

zn

vn

θ

(c)

N

Indicator Models

Figure 20.12: (a): A generic mixture model for
data v1:N . Each zn indicates the cluster of each
datapoint. θ is a set of parameters and zn = k
selects parameter θk for datapoint vn. (b): For
a potentially large number of clusters one way
to control complexity is to constrain the joint
(c): Plate notation of
indicator distribution.
(b).

we obtain from equation (20.5.2)

p(v1:N

θ) =
|

(cid:88)

N
(cid:89)

z1:N

n=1

p(vn

zn, θ)p(zn) =
|

N
(cid:89)

(cid:88)

n=1

zn

p(vn

zn, θ)p(zn)
|

(20.5.4)

which recovers the standard mixture model equation (20.1.3). As we discuss below, more sophisticated joint
indicator priors can be used to explicitly control the complexity of the indicator assignments and open the
path to essentially ‘inﬁnite dimensional’ models.

20.5.2 Polya prior

1, using a factorised joint indicator
For a large number of available clusters (mixture components) K
distribution could potentially lead to overﬁtting, resulting in little or no meaningful clustering. One way to
control the eﬀective number of components that are used is via a parameter π that regulates the complexity,
see ﬁg(20.12b,c),

(cid:29)

p(z1:N ) =

(cid:90)

(cid:40)

(cid:89)

π

n

(cid:41)

p(zn

π)

|

p(π)

(20.5.5)

where p(z

π) is a categorical distribution,

|
p(zn = k

π) = πk

|
This means, for example, that if
πk is low, we are therefore unlikely to select cluster k for any datapoint n. A
convenient choice for p(π) is the Dirichlet distribution (since this is conjugate to the categorical distribution),

@@

p(π) = Dirichlet (π

α)
|

∝

K
(cid:89)

k=1

π

α/K−1
k

The integral over π in equation (20.5.5) can be performed analytically to give a Polya distribution:

p(z1:N ) =

K
(cid:89)

Γ(α)
Γ(N + α)

Γ(Nk + α/K)
Γ(α/K)

,

Nk

≡

(cid:88)

I [zn = k]

k=1

n
The number of unique clusters used is then given by U = (cid:80)
I [Nk > 0]. The distribution over likely cluster
k
numbers is controlled by the parameter α. The scaling α/K in equation (20.5.7) ensures a sensible limit
, see ﬁg(20.13), in which limit the models are known as Dirichlet process mixture models. This
as K
approach means that we do not need to explicitly constrain the number of possible components K since the
number of active components U remains limited even for very large K.

→ ∞

Clustering is achieved by considering argmax

z1:N

p(z1:N

v1:N ). In practice it is common to consider
|

argmax
zn

p(zn

|

v1:N )

424

(20.5.9)

DRAFT April 24, 2015

(20.5.6)

(20.5.7)

(20.5.8)

Mixed Membership Models

(a)

(b)

(c)

Figure 20.13: The distribution of the number of unique clusters U when indicators are sampled from a Polya
(a): K = 50, (b): K = 100, (c):
distribution equation (20.5.8), with α = 2, and N = 50 datapoints.
K = 1000. Even though the number of available clusters K is larger than the number of datapoints, the
number of used clusters U remains constrained. See demoPolya.m.

Unfortunately, posterior inference of p(zn
v1:N ) for this class of models is formally computationally in-
tractable and approximate inference techniques are required. A detailed discussion of these techniques is
beyond the scope of this book and we refer the reader to [179] for a deterministic (variational) approach
and [221] for a discussion of sampling approaches.

|

20.6 Mixed Membership Models

Unlike standard mixture models in which each object is assumed to have been generated from a single
cluster, in mixed membership models an object may be a member of more than one group. Latent Dirichlet
Allocation discussed below is an example of such a mixed membership model, and is one of a number of
models developed in recent years [4, 98].

20.6.1 Latent Dirichlet allocation

Latent Dirichlet Allocation[46] considers that each datapoint may belong to more than a single cluster. A
typical application is to identify topic clusters in a collection of documents. A single document contains a
sequence of words, for example

v = (the, cat, sat, on, the, mat)

(20.6.1)

If each word in the available dictionary of D words is assigned to a unique state (say dog = 1, tree = 2, cat =
3, . . .), we can represent then the nth document as a vector of word indices

vn = (cid:0)vn

1 , . . . , vn
Wn

(cid:1) ,

vn
i ∈ {

1, . . . , D

}

(20.6.2)

where Wn is the number of words in the nth document. The number of words Wn in each document can
vary although the overall dictionary from which they came is ﬁxed.

k=1 πn

The aim is to ﬁnd common topics in documents, assuming that any document could potentially contain
more than one topic. It is useful to think ﬁrst of an underlying generative model of words, including latent
topics (which we will later integrate out). For each document n we have a distribution of topics πn with
(cid:80)K
k = 1 which gives a latent description of the document in terms of its topic membership. For example,
document n (which discusses issues related to wildlife conservation) might have a topic distribution with
high mass on the latent ‘animals’ and ‘environment’, topics. Note that the topics are indeed latent – the
name ‘animal’ would be given post-hoc based on the kinds of words that the latent topic would generate,
θi|k. As in section(20.5.2), to control complexity one may use a Dirichlet prior to limit the number of topics
active in any particular document:

p(πn

α) = Dirichlet (πn
|

|

α)

where α is a vector of length the number of topics.

DRAFT April 24, 2015

(20.6.3)

425

051015200501001502002500510152005010015020025005101520050100150200250Mixed Membership Models

Figure 20.14: Latent Dirichlet Allocation. For document n we ﬁrst sample
a distribution of topics πn. Then for each word position w = 1, . . . , Wn in
the document we sample a topic zn
w from the topic distribution. Given the
topic we then sample a word from the word distribution of that topic. The
parameters of the model are the word distributions for each topic θ, and the
parameters of the topic distribution α.

α

πn

zn
w

vn
w

Wn

N

θ

We ﬁrst sample a probability distribution (histogram) πn that represents the topics likely to occur for this
document. Then, for each word-position in the document, sample a topic and subsequently a word from the
distribution of words for that topic. For document n and the wth word-position in the document, vn
w, we
use zn
to indicate to which of the K possible topics that word belongs. For each topic k, one
then has a categorical distribution over all the words i = 1, . . . , D, in the dictionary:

1, . . . , K

w ∈ {

}

p(vn

zn
w = k, θ) = θi|k
w = i
|

For example, the ‘animal’ topic has high probability to emit animal-like words, etc.

(20.6.4)

A generative model for sampling a document vn with Wn word positions is then given by, see ﬁg(20.14),:

1. Choose πn

α)
∼
|
2. For each of word position vn

Dirichlet (πn

w, w = 1, . . . , Wn :

(a) Choose a topic zn
(b) Choose a word vn

w ∼
w ∼

p (zn
w|
p (cid:0)vn
w|

πn)
θ·|zn
w

(cid:1)

Training the LDA model corresponds to learning the parameters α, which relates to the number of topics,
and θ, which describes the distribution of words within each topic. Unfortunately, ﬁnding the requisite
marginals for learning from the posterior is formally computationally intractable. Eﬃcient approximate in-
ference for this class of models is a topic of research interest and both variational and sampling approaches
have recently been developed[46, 289, 242].

There are close similarities between LDA and PLSA[125], section(15.6.1), both of which describe a document
in terms of a distribution over latent topics. LDA is a probabilistic model for which issues such as setting
hyperparameters can be addressed using maximum likelihood. PLSA on the other hand is essentially
a matrix decomposition technique (such as PCA). Issues such as hyperparameters setting for PLSA are
therefore addressed using validation data. Whilst PLSA is a description only of the training data, LDA is
a generative data model and can in principle be used to synthesise new documents.

Example 20.5. An illustration of the use of LDA is given in ﬁg(20.15)[46]. The documents are taken from
the TREC Associated Press corpus containing 16,333 newswire articles with 23,075 unique terms. After
removing a standard list of stop words (frequent words such as ‘the’,‘a’ etc. that would otherwise dominate
the statistics), the EM algorithm (with variational approximate inference) was used to ﬁnd the Dirichlet and
conditional categorical parameters for a 100-topic LDA model. The top words from four resulting categorical
distributions θi|k are illustrated ﬁg(20.15a). These distributions capture some of the underlying topics in
the corpus. An example document from the corpus is presented along with the words coloured by the most
probable latent topic they correspond to.

426

DRAFT April 24, 2015

Mixed Membership Models

Arts
new
ﬁlm
show
music
movie
play
musical
best
actor
ﬁrst
york
opera
theater
actress
love

Budgets
million
tax
program
budget
billion
federal
year
spending
new
state
plan
money
programs
government
congress

Children Education
children
women
people
child
years
families
work
parents
says
family
welfare
men
percent
care
life

school
students
schools
education
teachers
high
public
teacher
bennett
manigat
namphy
state
president
elementary
haiti

(a)

The William Randolph Hearst Foundation will give $ 1.25 million to Lin-
coln Center, Metropolitan Opera Co., New York Philharmonic and Juilliard
School. Our board felt that we had a real opportunity to make a mark on
the future of the performing arts with these grants an act every bit as
important as our traditional areas of support in health, medical research,
education and the social services, Hearst Foundation President Randolph
A. Hearst said Monday in announcing the grants. Lincoln Centers share
will be $200,000 for its new building, which will house young artists and
provide new public facilities. The Metropolitan Opera Co. and New York
Philharmonic will receive $400,000 each. The Juilliard School, where music
and the performing arts are taught, will get $250,000. The Hearst Foun-
dation, a leading supporter of the Lincoln Center Consolidated Corporate
Fund, will make its usual annual $100,000 donation, too.

(b)

(a): A subset of the latent topics discovered by LDA and the high probability words
Figure 20.15:
associated with each topic. Each column represents a topic, with the topic name such as ‘arts’ assigned by
(b): A document from the training
hand after viewing the most likely words corresponding to the topic.
data in which the words are coloured according to the most likely latent topic. This demonstrates the
mixed-membership nature of the model, assigning the datapoint (document in this case) to several clusters
(topics). Reproduced from [46].

1

2

3

(a)

4

5

1

2

3

(b)

4

5

Figure 20.16: (a) The social network of a set of 5 individuals, repre-
sented as an undirected graph. Here individual 3 belongs to the group
(1, 2, 3) and also (3, 4, 5). (b) By contrast, in graph partitioning, one
breaks the graph into roughly equally sized disjoint partitions such
that each node is a member of only a single partition, with a minimal
number of edges between partitions.

20.6.2 Graph based representations of data

Mixed membership models are used in a variety of contexts and are distinguished also by the form of data
available. Here we focus on analysing a representation of the interactions amongst a collection of objects;
in particular, the data has been processed such that all the information of interest is characterised by an
interaction matrix. For graph based representations of data, two objects are similar if they are neighbours
on a graph representing the data objects. In the ﬁeld of social-networks, for example, each individual is
represented as a node in a graph, with a link between two nodes if the individuals are friends. Given a
graph one might wish to identify communities of closely linked friends. Interpreted as a social network, in
ﬁg(20.16a), individual 3 is a member of his work group (1, 2, 3) and also the poker group (3, 4, 5). These two
groups of individuals are otherwise disjoint. Discovering such groupings contrasts with graph partitioning
in which each node is assigned to only one of a set of subgraphs, ﬁg(20.16b), for which a typical criterion
is that each subgraph should be roughly of the same size and that there are few connections between the
subgraphs[169].

Another example is that nodes in the graph represent products and a link between nodes i and j indicates
that customers who buy product i frequently also buy product j. The aim is to decompose the graph into
groups, each corresponding to products that are commonly co-bought by customers[128]. A growing area of
application of graph based representations is in bioinformatics in which nodes represent genes, and a link
between them representing that the two genes have similar activity proﬁles. The task is then to identify
groups of similarly behaving genes[5].

20.6.3 Dyadic data

Consider two kinds of objects, for example, ﬁlms and customers. Each ﬁlm is indexed by f = 1, . . . , F and
each user by u = 1, . . . , U . The interaction of user u with ﬁlm f can be described by the element of a matrix
Muf representing the rating a user gives to a ﬁlm. A dyadic dataset consists of such a matrix and the aim
is to decompose this matrix to explain the ratings by ﬁnding types of ﬁlms and types of user.

DRAFT April 24, 2015

427

Mixed Membership Models

(a)

(b)

(a): There are 6 documents and 13 words. A link
Figure 20.17: Graphical representation of dyadic data.
represents that a particular word-document pair occurs in the dataset. (b): A latent decomposition of (a)
using 3 ‘topics’. A topic corresponds to a collection of words, and each document a collection of topics. The
open nodes indicate latent variables.

Another example is to consider a collection of documents, summarised by an interaction matrix in which
Mwd is 1 if word w appears in document d and zero otherwise. This matrix can be represented as a bipartite
graph, as in ﬁg(20.17a). The
upper nodes words, with a link
lower nodes represent documents, and the
between them if that word occurs in that document. One then seeks assignments of documents to groups
or latent ‘topics’ to succinctly explain the link structure of the bipartite graph via a small number of latent
nodes, as schematically depicted in ﬁg(20.17b). One may view this as a form of matrix factorisation, as in
PLSA section(15.6.1) [148, 204]

@@
@@

Mwd

(cid:88)

≈

t

UwtV T
td

(20.6.5)

where t indexes the topics and the feature matrices U and V control the word-to-topic mapping and the topic-
to-document mapping. This diﬀers from latent Dirichlet allocation which has a probabilistic interpretation
of ﬁrst generating a topic and then a word, conditional on the chosen topic. Here the interaction between
document-topic matrix V and word-topic matrix U is non-probabilistic.
In [204], real-valued data is modelled using

U, W, V) =
p(M
|

N

(cid:16)

(cid:17)
M UWVT, σ2I

(20.6.6)

where U and V are assumed binary and the real-valued W is a topic-interaction matrix. In this viewpoint
learning then consists of inferring U,W,V, given the dyadic observation matrix M. Assuming factorised
priors, the posterior over the matrices is

p(U, W, V

M)
|

p(M
|

∝

U, W, V)p(U)p(W)p(V)

(20.6.7)

A convenient choice is a Gaussian prior distribution for W, with the feature matrices U and V sampled
from Beta-Bernoulli priors. The resulting posterior distribution is formally computationally intractable, and
in [204] this is addressed using a sampling approximation.

20.6.4 Monadic data

In monadic data there is only one type of object and the interaction between the objects is represented by
a square interaction matrix. For example one might have a matrix with elements Aij = 1 if proteins i and j
can bind to each other and 0 otherwise. A depiction of the interaction matrix is given by a graph in which an
edge represents an interaction, for example ﬁg(20.18). In the following section we discuss a particular mixed
membership model and highlight potential applications. The method is based on clique decompositions of
graphs and as such we require a short digression into clique-based graph representations.

1

2

3

4

428

Figure 20.18: The minimal clique cover is (1, 2, 3), (2, 3, 4).

DRAFT April 24, 2015

Mixed Membership Models

1

2

3

4

1

2

3

4

(a)

(b)

Figure 20.19: Bipartite representations of the decomposi-
tions of ﬁg(20.18). Shaded nodes represent observed vari-
ables, and open nodes latent variables. (a) Incidence matrix
representation. (b) Minimal clique decomposition.

20.6.5 Cliques and adjacency matrices for monadic binary data

A symmetric adjacency matrix has elements Aij
∈ {
For the graph in ﬁg(20.18), the adjacency matrix is

0, 1

}

, with a 1 indicating a link between nodes i and j.

A =






1 1
1 1
1 1
0 1

1
1
1
1






0
1
1
1

(20.6.8)

where we include self connections on the diagonal. Given A, our aim is to ﬁnd a ‘simpler’ description that
reveals the underlying cluster structure, such as (1, 2, 3) and (2, 3, 4) in ﬁg(20.18). Given the undirected
graph in ﬁg(20.18), the incidence matrix Finc is an alternative description of the adjacency structure[86].
Given the V nodes in the graph, we construct Finc as follows: For each link i
j in the graph, form a
column of the matrix Finc with zero entries except for a 1 in the ith and jth row. The column ordering is
arbitrary. For example, for the graph in ﬁg(20.18) an incidence matrix is

∼

Finc =






1
1
0
0

1 0 0
0 1 1
1 1 0
0 0 1






0
0
1
1

(20.6.9)

The incidence matrix has the property that the adjacency structure of the original graph is given by the
outer product of the incidence matrix with itself. The diagonal entries contain the degree (number of links)
of each node. For our example, this gives

FincFT

inc =






2 1
1 3
1 1
0 1

1
1
3
1






0
1
1
2

so that

A = H

(cid:16)

FincFT
inc

(cid:17)

(20.6.10)

(20.6.11)

) is the element-wise Heaviside step function, [H(M)]ij = 1 if Mij > 0 and is 0 otherwise. A useful
Here H(
·
viewpoint of the incidence matrix is that it identiﬁes two-cliques in the graph (here we are using the term
‘clique’ in the non-maximal sense). There are ﬁve 2-cliques in ﬁg(20.18), and each column of Finc speciﬁes
which elements are in each 2-clique. Graphically we can depict this incidence decomposition as a bipartite
graph, as in ﬁg(20.19a) where the open nodes represent the ﬁve 2-cliques. The incidence matrix can be
generalised to describe larger cliques. Consider the following matrix as a decomposition for ﬁg(20.18), and
its outer-product:

F =






1
1
1
0




,

0
1
1
1

FFT =






1 1
1 2
1 2
0 1

1
2
2
1






0
1
1
1

(20.6.12)

The interpretation is that F represents a decomposition into two 3-cliques. As in the incidence matrix, each
column represents a clique, and the rows containing a ‘1’ express which elements are in the clique deﬁned
by that column. This decomposition can be represented as the bipartite graph of ﬁg(20.19b). For the graph
of ﬁg(20.18), both Finc and F satisfy
FFT(cid:17)

(20.6.13)

A = H

= H

(cid:16)

(cid:16)

(cid:17)

FincFT
inc

DRAFT April 24, 2015

429

Mixed Membership Models

Figure 20.20: The function σ(x)
β increases, this sigmoid function tends to a step function.

≡

(cid:0)1 + eβ(0.5−x)(cid:1)−1

for β = 1, 10, 100. As

One can view equation (20.6.13) as a form of matrix factorisation of the binary square (symmetric) matrix A
into non-square binary matrices. For our clustering purposes, the decomposition using F is to be preferred
to the incidence decomposition since F decomposes the graph into a smaller number of larger cliques. A
formal speciﬁcation of the problem of ﬁnding a minimum number of maximal fully-connected subsets is the
computationally hard problem MIN CLIQUE COVER[113, 267].

Deﬁnition 20.1 (Clique matrix). Given an adjacency matrix [A]ij , i, j = 1, . . . , V (Aii = 1), a clique
, i = 1, . . . , V, c = 1, . . . , C such that A = H(FFT). Diagonal elements
matrix F has elements Fic
(cid:2)FFT(cid:3)
ii express the number of cliques/columns that node i occurs in. Oﬀ-diagonal elements (cid:2)FFT(cid:3)
contain the number of cliques/columns that nodes i and j jointly inhabit [18].

0, 1
}

∈ {

ij

Whilst ﬁnding a clique decomposition F is easy (use the incidence matrix for example), ﬁnding a clique
decomposition with the minimal number of columns, i.e. solving MIN CLIQUE COVER, is NP-Hard[113, 9].
In [130] the cliques are required to be maximal, although in our deﬁnition the cliques may be non-maximal.

A generative model of adjacency matrices

Given an adjacency matrix A and a prior on clique matrices F, our interest is the posterior

p(F

A)
|

∝

p(A

F)p(F)
|

(20.6.14)

We ﬁrst concentrate on the generative term p(A
F). To ﬁnd ‘well-connected’ clusters, we relax the constraint
|
that the decomposition is in the form of perfect cliques in the original graph and view the absence of links
C matrix F, we desire that the higher
as statistical ﬂuctuations away from a perfect clique. Given a V
the overlap between rows2 fi and fj is, the greater the probability of a link between i and j. This may be
achieved using, for example,

×

with

F) = σ
p(Aij = 1
|

(cid:17)

(cid:16)

fif T
j

(cid:16)

1 + eβ(0.5−x)(cid:17)−1

σ(x)

≡

(20.6.15)

(20.6.16)

where β controls the steepness of the function, see ﬁg(20.20). The 0.5 shift in equation (20.6.16) ensures
that σ approximates the step-function since the argument of σ is an integer. Under equation (20.6.15), if
fi and fj have at least one ‘1’ in the same position, fif T
F) is high. Absent links
|
F). The parameter β controls how strictly σ(FFT) matches A; for
F) = 1
contribute p(Aij = 0
p(Aij = 1
|
|
large β, very little ﬂexibility is allowed and only cliques will be identiﬁed. For small β, subsets that would
be cliques if it were not for a small number of missing links, are clustered together. The setting of β is user
and problem dependent.

0.5 > 0 and p(Aij = 1

j −

−

Assuming each element of the adjacency matrix is sampled independently from the generating process, the
joint probability of observing A is (neglecting its diagonal elements),

p(A

F) =
|

(cid:89)

(cid:104)

(cid:16)

σ

fif T
j

(cid:17)(cid:105)Aij (cid:104)
1

i,j

(cid:16)

σ

fif T
j

(cid:17)(cid:105)1−Aij

−

2We use lower indices fi to denote the ith row of F.

(20.6.17)

430

DRAFT April 24, 2015

−2−1.5−1−0.500.511.5200.20.40.60.81xMixed Membership Models

(a)

(b)

(c)

(a): Adjacency ma-
Figure 20.21:
trix of 105 Political Books (black=1).
(b): Clique matrix: 521 non-zero en-
(c): Adjacency reconstruction
tries.
using an approximate clique matrix
with 10 cliques – see also ﬁg(20.22)
and demoCliqueDecomp.m.

The ultimate quantity of interest is the posterior distribution of clique structure, equation (20.6.14), for
which we now specify a prior p(F) over clique matrices.

Clique matrix prior p(F)

Since we are interested in clustering, ideally we want to place as many nodes in the graph as possible in a
cluster. This means that we wish to bias the contributions to the adjacency matrix A to occur from a small
number of columns of F. To achieve this we ﬁrst reparameterise F as

F = (cid:0)α1f 1, . . . , αCmaxf Cmax(cid:1)

(20.6.18)

0, 1

play the role of indicators and f c is column c of F. Cmax is an assumed maximal number
where αc
of clusters. Ideally, we would like to ﬁnd an F with a low number of indicators α1, . . . , αCmax in state 1. To
achieve this we deﬁne a prior distribution on the binary hypercube α = (α1, . . . , αCmax),

∈ {

}

p(α

ν) =
|

(cid:89)

c

ναc (1

−

ν)1−αc

(20.6.19)

To encourage a small number of the α(cid:48)
that ν is less than 0.5. This gives rise to a Beta-Bernoulli distribution

cs to be 1, we use a Beta prior p(ν) with suitable parameters to ensure

p(α) =

(cid:90)

p(α

ν)p(ν) =
|

ν

B(a + N, b + Cmax

B(a, b)

N )

−

(20.6.20)

where B(a, b) is the Beta function and N = (cid:80)Cmax
c=1 αc is the number of indicators in state 1. To encour-
age that only a small number of components should be active, we set a = 1, b = 3. The distribution
Cmax with a bias towards vertices close to the ori-
(20.6.20) is on the vertices of the binary hypercube
0, 1
}
{
gin (0, . . . , 0). Through equation (20.6.18), the prior on α induces a prior on F. The resulting distribution
α)p(α) is formally intractable and in [18] this is addressed using a variational technique.
p(F, α
|

A)
|

p(F

∝

Clique matrices also play a natural role in the parameterisation of positive deﬁnite matrices under the
constraint of speciﬁed zeros in the matrix, see exercise(20.7)[18].

Example 20.6 (Political Books Clustering). The data consists of 105 books on US politics sold by the
online bookseller Amazon. The adjacency matrix with element Aij = 1 ﬁg(20.21a), represents frequent
co-purchasing of books i and j (from Valdis Krebs). Additionally, books are labelled ‘liberal’, ‘neutral’,
or ‘conservative’ according to the judgement of a politically astute reader. The interest is to assign books
to clusters, using A alone, and then see if these clusters correspond in some way to the ascribed political
leanings of each book. Note that the information here is minimal – all that is known to the clustering
algorithm is which books were co-bought (matrix A); no other information on the content or title of the
books are exploited by the algorithm. With an initial Cmax = 200 cliques, Beta parameters a = 1, b = 3 and
steepness β = 10, the most probable posterior marginal solution contains 142 cliques ﬁg(20.21b), giving a
perfect reconstruction of the adjacency A. For comparison, the incidence matrix has 441 2-cliques. However,
this clique matrix is too large to provide a compact interpretation of the data – indeed there are more clusters
than books. To cluster the data more aggressively, we ﬁx Cmax = 10 and re-run the algorithm. This results
only in an approximate clique decomposition, A
10

H(FFT), as plotted in ﬁg(20.21c). The resulting 105

≈

×

DRAFT April 24, 2015

431

204060801002040608010020406080100120140204060801002040608010020406080100Mixed Membership Models

10 dimensional clique matrix broken into 3 groups by a politically
Figure 20.22: Political Books. 105
astute reader. A black square indicates q(fic) > 0.5. Liberal books (red), Conservative books (green),
Neutral books(yellow). By inspection, cliques 5,6,7,8,9 largely correspond to ‘conservative’ books.

×

approximate clique matrix is plotted in ﬁg(20.22) and demonstrates how individual books are present in
more than one cluster. Interestingly, the clusters found only on the basis of the adjacency matrix have some
correspondence with the ascribed political leanings of each book; cliques 5, 6, 7, 8, 9 correspond to largely
‘conservative’ books. Most books belong to more than a single clique/cluster, suggesting that they are not
single topic books, consistent with the assumption of a mixed membership model.

20.7 Summary

Mixture models are discrete latent variable models and can be trained using maximum likelihood.

A classical approach to training is to use the EM algorithm, though gradient based approaches are also
possible.

Standard mixture models assume that a priori each object (datapoint) can be a member of only a single
cluster.

In mixed-membership models, an object may a priori belong to more than a single cluster. Models such as
latent Dirichlet allocation have interesting application for example to text modelling including the automatic
discovery of latent topics.

Mixed-membership models may also be considered for monadic or dyadic data.

•

•

•

•

•

The literature on mixture modelling is extensive, and a good overview and entrance to the literature is
contained in [203].

20.8 Code

MIXprodBern.m: EM training of a mixture of product Bernoulli distributions
demoMixBernoulli.m: Demo of a mixture of product Bernoulli distributions

GMMem.m: EM training of a mixture of Gaussians
GMMloglik.m: GMM log likelihood
demoGMMem.m: Demo of a EM for mixture of Gaussians

432

DRAFT April 24, 2015

05101000 Years for Revenge  Bush vs. the Beltway  Charlie Wilson’s War  Losing Bin Laden  Sleeping With the Devil  The Man Who Warned America  Why America Slept  Ghost Wars  A National Party No More  Bush Country  Dereliction of Duty  Legacy  Off with Their Heads  Persecution  Rumsfeld’s War  Breakdown  Betrayal  Shut Up and Sing  Meant To Be  The Right Man  Ten Minutes from Normal  Hillary’s Scheme  The French Betrayal of America  Tales from the Left Coast  Hating America  The Third Terrorist  Endgame  Spin Sisters  All the Shah’s Men  Dangerous Dimplomacy  The Price of Loyalty  House of Bush, House of Saud  The Death of Right and Wrong  Useful Idiots  The O’Reilly Factor  Let Freedom Ring  Those Who Trespass  Bias  Slander  The Savage Nation  Deliver Us from Evil  Give Me a Break  The Enemy Within  The Real America  Who’s Looking Out for You?  The Official Handbook Vast Right Wing Conspiracy  Power Plays  Arrogance  The Perfect Wife  The Bushes  Things Worth Fighting For  Surprise, Security, the American Experience  Allies  Why Courage Matters  Hollywood Interrupted  Fighting Back  We Will Prevail  The Faith of George W Bush  Rise of the Vulcans  Downsize This!  Stupid White Men  Rush Limbaugh Is a Big Fat Idiot  The Best Democracy Money Can Buy  The Culture of Fear  America Unbound  The Choice  The Great Unraveling  Rogue Nation  Soft Power  Colossus  The Sorrows of Empire  Against All Enemies  American Dynasty  Big Lies  The Lies of George W. Bush  Worse Than Watergate  Plan of Attack  Bush at War  The New Pearl Harbor  Bushwomen  The Bubble of American Supremacy  Living History  The Politics of Truth  Fanatics and Fools  Bushwhacked  Disarming Iraq  Lies and the Lying Liars Who Tell Them  MoveOn’s 50 Ways to Love Your Country  The Buying of the President 2004  Perfectly Legal  Hegemony or Survival  The Exception to the Rulers  Freethinkers  Had Enough?  It’s Still the Economy, Stupid!  We’re Right They’re Wrong  What Liberal Media?  The Clinton Wars  Weapons of Mass Deception  Dude, Where’s My Country?  Thieves in High Places  Shrub  Buck Up Suck Up  The Future of Freedom  Empire  Exercises

demoGMMclass.m: Demo GMM for classiﬁcation

Kmeans.m: K-means
demoKmeans.m: Demo of K-means

demoPolya.m: Demo of the number of active clusters from a Polya distribution
dirrnd.m: Dirichlet random distribution generator

cliquedecomp.m: Clique matrix decomposition
cliquedecomp.c: Clique matrix decomposition (C-code)
DemoCliqueDecomp.m: Demo clique matrix decomposition

20.9 Exercises

Exercise 20.1. Consider a mixture of factorised models for vector observations v

p(v) =

(cid:88)

h

p(h)

(cid:89)

i

p(vi

h)
|

(20.9.1)

For assumed i.i.d. data vn, n = 1, . . . , N , some observation components may be missing so that, for example
the third component of the ﬁfth datapoint, v5
3 is unknown. Show that maximum likelihood training on the
observed data corresponds to ignoring components vn

i that are missing.

Exercise 20.2. Derive the optimal EM update for ﬁtting a mixture of Gaussians under the constraint that
the covariances are diagonal.

Exercise 20.3. Consider a mixture of K isotropic Gaussians, each with the same covariance, Si = σ2I. In
the limit σ2

0 show that the EM algorithm tends to the K-means clustering algorithm.

→

Exercise 20.4. Consider the term

N
(cid:88)

(cid:104)

n=1

log p(h)

(cid:105)pold(h|vn)

(20.9.2)

We wish to optimise the above with respect to the distribution p(h). This can be achieved by deﬁning the
Lagrangian

L =

N
(cid:88)

n=1

log p(h)
(cid:104)

(cid:105)pold(h|vn) + λ

(cid:32)
1

(cid:88)

−

h

(cid:33)

p(h)

(20.9.3)

By diﬀerentiating the Lagrangian with respect to p(h) and using the normalisation constraint (cid:80)
show that, optimally

h p(h) = 1,

p(h) =

1
N

N
(cid:88)

n=1

vn)
pold(h
|

(20.9.4)

Exercise 20.5. We showed that ﬁtting an unconstrained mixture of Gaussians using maximum likelihood is
problematic since, by placing one of the Gaussians over a datapoint and letting the covariance determinant
(x µ, Σ) to i.i.d.
go to zero, we obtain an inﬁnite likelihood. In contrast, when ﬁtting a single Gaussian
data x1, x2, . . . , xN show that the maximum likelihood optimum for Σ has non-zero determinant, and that
the optimal likelihood remains ﬁnite.

N

Exercise 20.6. Modify GMMem.m suitably so that it can deal with the semi-supervised scenario in which the
mixture component h of some of the observations v is known.

DRAFT April 24, 2015

433

Exercise 20.7. You wish to parameterise covariance matrices S under the constraint that speciﬁed elements
are zero. The constraints are speciﬁed using a matrix A with elements Aij = 0 if Sij = 0 and Aij = 1
otherwise. Consider a clique matrix Z, for which

Exercises

A = H(ZZT)

and matrix

S∗ = Z∗ZT
∗

with

[Z∗]ij =

(cid:26) 0
θij

if Zij = 0
if Zij = 1

(20.9.5)

(20.9.6)

(20.9.7)

for parameters θ. Show that for any θ, S∗ is positive semideﬁnite and parameterises covariance matrices
under the zero constraints speciﬁed by A.

434

DRAFT April 24, 2015

CHAPTER 21

Latent Linear Models

In this chapter we discuss some simple continuous latent variable models. The Factor Analysis model is
a classical statistical model and is essentially a probabilistic version of PCA. Such models can be used
to form simple low-dimensional generative models of data. As an example we consider an application to
face recognition. By extending the model to use non-Gaussian priors on the latent variables, independent
dimensions underlying the data can be discovered, and can give rise to radically diﬀerent low dimensional
representations of the data than aﬀorded by PCA.

21.1 Factor Analysis

In chapter(15) we discussed Principal Components Analysis which forms lower dimensional representations
of data based on assuming that the data lies close to a linear subspace. Here we describe a related prob-
abilistic model for which extensions to Bayesian methods can be envisaged. Any probabilistic model may
also be used as a component of a larger more complex model, such as a mixture model, enabling natural
generalisations.

We use v to describe a real data vector to emphasise that this is a visible (observable) quantity. The dataset
is then given by a set of vectors,

= (cid:8)v1, . . . , vN (cid:9)

V

(21.1.1)

where dim (v) = D. Our interest is to ﬁnd a lower dimensional probabilistic description of this data. If
data lies close to a H-dimensional linear subspace we may accurately approximate each datapoint by a low
H-dimensional coordinate system. In general, datapoints will not lie exactly on the linear subspace and
we model this discrepancy with Gaussian noise. Mathematically, the FA model generates an observation v
according to, see ﬁg(21.1),

v = Fh + c + (cid:15)

where the noise (cid:15) is Gaussian distributed, with zero mean and covariance Ψ

(cid:15)

∼ N

((cid:15) 0, Ψ)

(21.1.2)

(21.1.3)

The constant bias c sets the origin of the coordinate system1. The D
H factor loading matrix F plays a
similar role as the basis matrix in PCA. Similarly, the hidden coordinates h plays the role of the components
we used in section(15.2). The diﬀerence between PCA and Factor Analysis is in the choice of Ψ:

×

1Depending on the application, it can be sometimes useful to force the origin to be zero to aid the interpretation of the

factors; this results in only a minor modiﬁcation of the framework.

435

h1

h2

h3

v1

v2

v3

v4

v5

Figure 21.1: Factor Analysis. The visible vector variable v is related
to the vector hidden variable h by a linear mapping, with independent
additive Gaussian noise on each visible variable. The prior on the
hidden variable may be taken to be an isotropic Gaussian, thus being
independent across its components.

Factor Analysis

Probabilistic PCA

Ψ = σ2I

Factor Analysis

Ψ = diag (ψ1, . . . , ψD)

(21.1.4)

(21.1.5)

Factor analysis therefore diﬀers from probabilistic PCA in that it has a richer description for the oﬀ-subspace
noise Ψ.

A probabilistic description

From equation (21.1.2) and equation (21.1.3), given h, the data is Gaussian distributed with mean Fh + c
and covariance Ψ

h) =

p (v

|

N

(v Fh + c, Ψ)

(cid:18)

exp

∝

−

1
2

Fh

(v

−

−

c)T Ψ−1 (v

(cid:19)

Fh

c)

−

−

(21.1.6)

To complete the model, we need to specify the hidden distribution p(h). A convenient choice is a Gaussian

p (h) =

N

(h 0, I)

(cid:16)

exp

(cid:17)

hTh/2

−

∝

(21.1.7)

Under this prior the coordinates h will be preferentially concentrated around values close to 0. If we sample
a h vector from p(h) and then draw a value for v using p(v
h), the sampled v vectors would produce a saucer
|
(h 0, ΣH ) has
or ‘pancake’ of points in the v space, see ﬁg(21.2). Using a correlated Gaussian prior p(h) =
no eﬀect on the ﬂexibility of the model since ΣH can be absorbed into F, exercise(21.3). Since v is linearly
related to h through equation (21.1.2) and both (cid:15) and h are Gaussian, then v is Gaussian distributed. The
mean and covariance can be computed using propagation, result(8.3):

N

(cid:90)

p (v) =

p (v

h) p (h) dh =
|

N

(cid:16)

v c, FFT + Ψ

(cid:17)

(21.1.8)

What this says is that our model for the data is a Gaussian centred on c with covariance matrix constrained
to be of the form FFT + Ψ. The number of free parameters for factor analysis is therefore D(H + 1).
Compared to an unconstrained covariance on v which has D(D + 1)/2 free parameters, by choosing H
D,
we have signiﬁcantly fewer parameters to estimate in factor analysis than in unconstrained covariance case.

(cid:28)

Invariance of the likelihood under factor rotation

Since the matrix F only appears in the ﬁnal model p(v) through FFT + Ψ, the likelihood is unchanged if
we rotate F using FR, with RRT = I:

FR(FR)T + Ψ = FRRTFT + Ψ = FFT + Ψ

(21.1.9)

The solution space for F is therefore not unique – we can arbitrarily rotate the matrix F and produce an
equally likely model of the data. Some care is therefore required when interpreting the entries of F. Varimax
provides a more interpretable F by using a suitable rotation matrix R. The aim is to produce a rotated
F for which each column has only a small number of large values. Finding a suitable rotation results in a
non-linear optimisation problem and needs to be solved numerically. See [199] for details.

436

DRAFT April 24, 2015

Factor Analysis : Maximum Likelihood

(a)

(b)

(a): 1000 latent two-dimensional
Figure 21.2: Factor Analysis: 1000 points generated from the model.
points hn sampled from
(h 0, I). These are transformed to a point on the three-dimensional plane by
(b): For each point xn
xn
0 = c + Fhn. The covariance of x0 is degenerate, with covariance matrix FFT.
0
((cid:15) 0, Ψ) and added to the in-plane vector to form a
on the plane a random noise vector is drawn from
N
sample xn, plotted in red. The distribution of points forms a ‘pancake’ in space. Points ‘underneath’ the
plane are not shown.

N

21.1.1 Finding the optimal bias

For a set of data

V

and using the usual i.i.d. assumption, the log likelihood is

log p(

V|

F, Ψ, c) =

N
(cid:88)

n=1

log p(vn) =

1
2

−

N
(cid:88)

n=1

(vn

−

c)T Σ−1

D (vn

c)

−

−

N
2

log det (2πΣD)

(21.1.10)

where

ΣD

≡

FFT + Ψ

(21.1.11)

Diﬀerentiating equation (21.1.10) with respect to c and equating to zero, we arrive at the maximum likelihood
optimal setting that the bias c is the mean of the data,

c =

1
N

N
(cid:88)

n=1

vn

¯v

≡

(21.1.12)

We will use this setting throughout. With this setting the log likelihood equation (21.1.10) can be written

log p(

V|

F, Ψ) =

N
2

−

(cid:0)trace (cid:0)Σ−1

D S(cid:1) + log det (2πΣD)(cid:1)

where S is the sample covariance matrix

S =

1
N

N
(cid:88)

n=1

(v

−

¯v) (v

−

¯v)T

(21.1.13)

(21.1.14)

21.2 Factor Analysis : Maximum Likelihood

We now specialise to the assumption that Ψ = diag (ψ1, . . . , ψD). We consider two methods for learning
the factor loadings F: the ‘eigen’ approach2 of section(21.2.1) and the EM approach, section(21.2.2). The
eigen-approach is common in statistics and software packages, whilst the EM approach is more common in
machine learning.

2The presentation here follows closely that of [323].

DRAFT April 24, 2015

437

−4−20246−4−20246−2−101234−4−20246−4−20246−2−101234Factor Analysis : Maximum Likelihood

21.2.1 Eigen-approach likelihood optimisation

As we will show, if the noise matrix Ψ is given, we can ﬁnd the optimal factor matrix F by solving an
eigen-problem. If the Ψ is unknown, from a starting guess for Ψ, we can ﬁnd the optimal F and use this to
reestimate the noise Ψ, iterating this two-step process until convergence. This section is necessarily rather
technical and can be skipped on ﬁrst reading.

Optimal F for ﬁxed Ψ

To ﬁnd the maximum likelihood setting of F we diﬀerentiate the log likelihood equation (21.1.13) with
respect to F and equate to zero. This gives

0 = trace (cid:0)Σ−1

D (∂FΣD)Σ−1

D S(cid:1)

trace (cid:0)Σ−1

D ∂FΣD

(cid:1)

−

Using

∂F(ΣD)= ∂F(FFT) = F(∂FFT) + (∂FF)FT

a stationary point is given when

Σ−1

D F = Σ−1

D SΣ−1

D F

The optimal F therefore satisﬁes

F = SΣ−1

D F

Using the deﬁnition of ΣD, equation (21.1.11), one can rewrite Σ−1

D F as (see exercise(21.4))

Σ−1

D F = Ψ−1F

(cid:16)

I + FTΨ−1F

(cid:17)−1

Plugging this into the zero derivative condition, equation (21.2.4) can be rearranged to

(cid:16)

F

I + FTΨ−1F

(cid:17)

= SΨ−1F

Using the reparameterisations

Ψ− 1

2 F,

˜F

≡

˜S = Ψ− 1

2 SΨ− 1

2

equation (21.2.6) can be written in the ‘isotropic’ form

(cid:16)

˜F

I + ˜FT ˜F

(cid:17)

= ˜S ˜F

We assume that the transformed factor matrix ˜F has a thin SVD decomposition

˜F = UH LWT

where dim (UH ) = D

H, dim (L) = H

×

×

H, dim (W) = H

H and

×

UT

H UH = IH ,

WTW = IH

(21.2.1)

(21.2.2)

(21.2.3)

(21.2.4)

(21.2.5)

(21.2.6)

(21.2.7)

(21.2.8)

(21.2.9)

(21.2.10)

and L = diag (l1, . . . , lH ) are the singular values of ˜F. Plugging this assumption into equation (21.2.8) we
obtain

UH LWT (cid:16)

IH + WL2WT(cid:17)

= ˜SUH LWT

which gives

UH

(cid:0)IH + L2(cid:1) = ˜SUH ,

L2 = diag (cid:0)l2

1, . . . , l2
H

(cid:1)

(21.2.11)

(21.2.12)

Equation(21.2.12) is then an eigen-equation for UH . Intuitively, it’s clear that we need to ﬁnd then the
eigen-decomposition of ˜S and then set the columns of UH to those eigenvectors corresponding to the largest
eigenvalues. This is derived more formally below.

438

DRAFT April 24, 2015

Factor Analysis : Maximum Likelihood

Determining the appropriate eigenvalues

We can relate the form of the solution to the eigen-decomposition of ˜S

˜S = UΛUT,

U = [UH

Ur]
|

(21.2.13)

where Ur are arbitrary additional columns chosen to complete UH to form an orthogonal U, UTU = UUT =
I. Using Λ = diag (λ1, . . . , λD), equation (21.2.12) stipulates 1 + l2
1, i = 1, . . . , H.
Given the solution for ˜F, the solution for F is found from equation (21.2.7). To determine the optimal λi
we write the log likelihood in terms of the λi as follows. Using the new parameterisation,

i = λi, or li = √λi

−

ΣD = Ψ

1
2

(cid:16)

(cid:17)
˜F ˜FT + I

1
2

Ψ

and S = Ψ

1

2 ˜SΨ

1
2 , we have

trace (cid:0)Σ−1

D S(cid:1) = trace

(cid:18)(cid:16)

˜F ˜FT + ID

(cid:17)−1

(cid:19)

˜S

(21.2.14)

(21.2.15)

The log likelihood equation (21.1.10) in this new parameterisation is

2
N

−

log p(

V|

F, Ψ) = trace

(cid:18)(cid:16)

ID + ˜F ˜FT(cid:17)−1

(cid:19)

˜S

+ log det

(cid:16)

ID + ˜F ˜FT(cid:17)

+ log det (2πΨ)

(21.2.16)

Using λi = 1 + l2

i , and equation (21.2.9) we can write

ID + ˜F ˜FT = ID + UH L2UT

H = Udiag (λ1, . . . , λH , 1, . . . , 1) UT

so that the inverse of this matrix is given by Udiag (cid:0)λ−1

1 , . . . , λ−1

H , 1, . . . , 1(cid:1) UT and hence

(cid:18)(cid:16)

ID + ˜F ˜FT(cid:17)−1

˜S

(cid:19)

(cid:88)

=

trace

i

λi
λ(cid:48)
i

,

λ(cid:48)
i =

(cid:26) λi
1

i
H
≤
i > H

Similarly

log det

(cid:16)

ID + ˜F ˜FT(cid:17)

=

H
(cid:88)

i=1

log λi

(21.2.17)

(21.2.18)

(21.2.19)

Using this we can write the log likelihood as a function of the eigenvalues (for ﬁxed Ψ) as

2
N

−

log p(

V|

F, Ψ) =

H
(cid:88)

i=1

log λi + H +

D
(cid:88)

i=H+1

λi + log det (2πΨ)

(21.2.20)

To maximise the likelihood we need to minimise the right hand side of the above. Since log λ < λ we should
place the largest H eigenvalues in the (cid:80)

i log λi term. A solution for ﬁxed Ψ is therefore

F = Ψ

1

2 UH (ΛH

IH )

1
2 R

−

where

ΛH

≡

diag (λ1, . . . , λH )

(21.2.21)

(21.2.22)

are the H largest eigenvalues of Ψ− 1
R is an arbitrary orthogonal matrix.

2 SΨ− 1

2 , with UH being the matrix of the corresponding eigenvectors.

DRAFT April 24, 2015

439

Algorithm 21.1 Factor analysis training using SVD for N D-dimensional datapoints v1, . . . , vN . H is the
latent number of factors required.

Factor Analysis : Maximum Likelihood

1: Initialise the diagonal noise Ψ
2: Find the mean ¯v of the data v1, . . . , vN
3: Find the variance σ2
4: Compute the centred matrix X = (cid:2)v1
5: while Likelihood not converged or termination criterion not reached do
6:

i for each component i of the data v1
¯v(cid:3)

i , . . . , vN
i

¯v, . . . , xN

−

−

7:

8:

9:

10:

Form the scaled data matrix ˜X = Ψ− 1
2 X/√N
2
Perform SVD for ˜X = U ˜Λ ˜WT and set Λ = ˜Λ
Set UH to the ﬁrst H columns of U and set ΛH to contain the ﬁrst H diagonal entries of Λ.
2 UH (ΛH
F = Ψ
(cid:110)(cid:80)H
N
L =
2
Ψ = diag (cid:0)σ2(cid:1)

IH )
i=1 log λi + H + (cid:80)D
diag (cid:0)FFT(cid:1)

i=H+1 λi + log det (2πΨ)

−

−

(cid:111)

1
2

1

(cid:46) factor update
(cid:46) log likelihood

(cid:46) noise update

11:
12: end while

−

SVD based approach

Rather than ﬁnding the eigen-decomposition of Ψ− 1
considering the thin SVD decomposition of

2 SΨ− 1

2 we can avoid forming the covariance matrix by

˜X =

1
√N

Ψ− 1

2 X

where the centred data matrix is

(cid:2)v1

X

≡

−

¯v, . . . , xN

¯v(cid:3)

−

Given a thin decomposition

˜X = UH ˜Λ ˜WT

(21.2.23)

(21.2.24)

(21.2.25)

we obtain the eigenvalues λi = ˜Λ2
are available[52].

ii. When the matrix X is too large to store in memory, online SVD methods

Finding the optimal Ψ

The zero derivative of the log likelihood equation (21.1.13) with respect to Ψ occurs when

Ψ = diag

(cid:16)

S

−

FFT(cid:17)

(21.2.26)

where F is given by equation (21.2.21). There is no closed form solution to equations(21.2.26, 21.2.21). A
simple iterative scheme is to ﬁrst guess values for the diagonal entries of Ψ and then ﬁnd the optimal F
using equation (21.2.21). Subsequently Ψ is updated using

Ψnew = diag

(cid:16)

S

−

FFT(cid:17)

(21.2.27)

We update F using equation (21.2.21) and Ψ using equation (21.2.27) until convergence, see algorithm(21.1).

Alternative schemes for updating the noise matrix Ψ can improve convergence considerably. For example
updating only a single component of Ψ with the rest ﬁxed can be achieved using a closed form expression[323].

21.2.2 Expectation maximisation

An alternative way way to train factor analysis that is popular in machine learning is to use EM, sec-
tion(11.2). We assume that the bias c has been optimally set to the data mean ¯v.

440

DRAFT April 24, 2015

Factor Analysis : Maximum Likelihood

M-step

As usual, we need to consider the energy which, neglecting constants, is

E (F, Ψ) =

N
(cid:88)

n=1

(cid:28) 1
2

−

(dn

−

Fh)T Ψ−1 (dn

(cid:29)

Fh)

−

N
2

q(h|vn) −

log det (Ψ)

(21.2.28)

where dn
Maximising E (F, Ψ) with respect to F gives

vn

≡

−

¯v. The optimal variational distribution q(h

F = AH−1

where

A

≡

1
N

(cid:88)

n

dn

h
(cid:104)

T
q(h|vn) ,
(cid:105)

H

≡

1
N

(cid:88)

hhT(cid:69)
(cid:68)

n

q(h|vn)

Finally

vn) is determined by the E-step below.
|

(21.2.29)

(21.2.30)

Ψ =

1
N

(cid:88)

n

(cid:18)(cid:68)

(dn

diag

Fh) (dn

−

−

Fh)T(cid:69)

q(h|vn)

(cid:32)

(cid:19)

= diag

1
N

(cid:88)

n

dn(dn)T

−

(cid:33)

2FAT + FHFT

(21.2.31)

Note that the new F is used in the above update for the covariance.

E-step

The above updates depend on the statistics
the E-step we have

(cid:104)

q(h

vn)
|

∝

p(vn

|

h)p(h) =

N

(h mn, Σ)

h

(cid:105)q(h|vn) and (cid:10)hhT(cid:11)

q(h|vn). Using the EM optimal choice for

(21.2.32)

with

mn =

h
(cid:105)q(h|vn) =

(cid:104)

(cid:16)

I + FTΨ−1F

(cid:17)−1

FTΨ−1dn,

(cid:16)

Σ =

I + FTΨ−1F

(cid:17)−1

(21.2.33)

Using these results we can express the statistics in equation (21.2.30) as

H = Σ +

1
N

(cid:88)

n

mn(mn)T

(21.2.34)

Equations (21.2.29,21.2.31,21.2.33) are iterated till convergence. As for any EM algorithm, the likelihood
equation (21.1.10) (under the diagonal constraint on Ψ) increases at each iteration. Convergence using this
EM technique can be slower than that of the eigen-approach of section(21.2.1). Provided however that a
reasonable initialisation is used, the performance of the two training algorithms can be similar. A useful
initialisation is to use PCA and then set F to the principal directions.

Mixtures of FA

An advantage of probabilistic models is that they may be used as components in more complex models, such
as mixtures of FA[293]. Training can then be achieved using EM or gradient based approaches. Bayesian
extensions are clearly of interest; whilst formally intractable they can be addressed using approximate
methods, for example [105, 192, 119].

DRAFT April 24, 2015

441

Interlude: Modelling Faces

F

G
¯x12

¯x13

f1

¯x11

G

¯x21

f2

µ

¯x22

origin

Figure 21.3: Latent Identity Model. The mean µ represents
the mean of the faces. The subspace F represents the direc-
tions of variation of diﬀerent faces so that f1 = µ + Fh1 is a
mean face for individual 1, and similarly for f2 = µ + Fh2.
The subspace G denotes the directions of variability for
any individual face, caused by pose, lighting etc. This vari-
ability is assumed the same for each person. A particular
mean face is then given by the mean face of the person plus
pose/illumination variation, for example ¯x12 = f1 + Gw12.
A sample face is then given by a mean face ¯xij plus Gaussian
noise from

((cid:15)ij 0, Σ).

N

21.3 Interlude: Modelling Faces

Factor Analysis has widespread application in statistics and machine learning. As an inventive application
of FA, highlighting the probabilistic nature of the model, we describe a face modelling technique that has as
its heart a latent linear model[245]. Consider a gallery of face images
so
}
that the vector xij represents the jth image of the ith person. As a latent linear model of faces we consider

xij, i = 1, . . . , I; j = 1, . . . , J

=

X

{

xij = µ + Fhi + Gwij + (cid:15)ij

(21.3.1)

Here F (dim F = D
variability related to pose, illumination etc. within the diﬀerent images of each person. The contribution

F ) is used to model variability between people, and G (dim G = D

G) models

×

×

µ + Fhi

fi

≡

(21.3.2)

accounts for variability between diﬀerent people, being constant for individual i. For ﬁxed i, the contribution

Gwij + (cid:15)ij,

with (cid:15)ij

((cid:15)ij 0, Σ)

∼ N

(21.3.3)

accounts for the variability over the images of person i, explaining why two images of the same person do not
look identical. See ﬁg(21.3) for a graphical representation. As a probabilistic linear latent variable model,
we have for an image xij:

p(xij

|

hi, wij, θ) =

N

(xij µ + Fhi + Gwij, Σ)

p(hi) =

N

(hi 0, I) ,

p(wij) =

(wij 0, I)

N

The parameters are θ =

. For the collection of images, assuming i.i.d. data,

p(

X

, w, h

θ) =
|

p(xij

hi, wij, θ)p(wij)

p(hi)

|






F, G, µ, Σ
{
}



J
(cid:89)

I
(cid:89)

i=1



j=1

θ

(21.3.4)

(21.3.5)

(21.3.6)

wij

hi

xij

J

I

Figure 21.4: The jth image of the ith person, xij, is modelled
using a linear latent model with parameters θ.

442

DRAFT April 24, 2015

Interlude: Modelling Faces

(a) Mean

(b) Variance

(c)

(d)

(e)

(f)

(g)

(h)

3 vector (the 3
Figure 21.5: Latent Identity Model of face images. Each image is represented by a 70
comes from the RGB colour coding). There are I = 195 individuals in the database and J = 4 images per
person. (a): Mean of the data. (b): Per pixel standard deviation – black is low, white is high. (c,d,e):
Three directions from the between-individual subspace F.
(f,g,h): Three samples from the model with h
ﬁxed and drawing randomly from w in the within-individual subspace G. Reproduced from [245].

70

×

×

for which the graphical model is depicted in ﬁg(21.4). The task of learning is then to maximise the likelihood

(cid:90)

θ) =

p(

X |

w,h

p(

X

, w, h

θ)
|

(21.3.7)

This model can be seem as a constrained version of Factor Analysis by using stacked vectors (here for only
a single individual, I = 1)








x11
x12
...
x1J








=















µ
µ
...
µ








+

F G 0 . . . 0
F 0 G . . . 0
...
...
...
...
. . .
0 . . . G
F 0

















h1
w11
w12
...
w1J

















+








(cid:15)11
(cid:15)12
...
(cid:15)1J

(21.3.8)

The generalisation to multiple individuals I > 1 is straightforward. The model can be trained using either a
constrained form of the eigen method, or EM as described in [245]. Example images from the trained model
are presented in ﬁg(21.5).

Recognition

Using the above constrained factor analysis models for faces, one can extend their application to classiﬁca-
tion, as we now brieﬂy describe. In closed set face recognition a new ‘probe’ face x∗ is to be matched to
n the nth gallery face is forced to share its latent
a person n in the gallery of training faces. In model
identity variable hn with the test face, indicating that these faces belong to the same person3, see ﬁg(21.6).
Assuming a single exemplar per person (J = 1),

M

p(x1, . . . , xI , x∗

n) = p(xn, x∗)

|M

I
(cid:89)

i=1,i(cid:54)=n

p(xi)

Bayes’ rule then gives the posterior class assignment

(21.3.9)

p(

x1, . . . , xI , x∗)

n

p(x1, . . . , xI , x∗

n)p(

n)

M

M
3This is analogous to Bayesian outcome analysis in section(12.6) in which the hypotheses assume that either the errors were

|M

∝

|

(21.3.10)

generated from the same or a diﬀerent model.

DRAFT April 24, 2015

443

h1

x1

h2

x2

h1

x∗

x1

h2

x2

x∗

w1

w2

w∗

w1

w2

w∗

(a) M1

(b) M2

Probabilistic Principal Components Analysis

M

In model

Figure 21.6: Face recognition model (de-
picted only for a single exemplar per per-
(a):
son, J = 1).
1 the
test image (or ‘probe’) x∗ is assumed to
be from person 1, albeit with a diﬀerent
pose/illumination.
2
M
the test image is assumed to be from per-
son 2. One calculates p(x1, x2, x∗
1) and
p(x1, x2, x∗
2) and then uses Bayes’ rule
to infer to which person the test image x∗
most likely belongs.

(b): For model

|M

|M

M

n) is constant and can be neglected. All these marginal quantities are

For a uniform prior, the term p(
straightforward to derive since they are simply marginals of a Gaussian.
In practice, the best results are obtained using a between-individual subspace dimension F and within-
individual subspace dimension G both equal to 128. This model then has performance competitive with the
state-of-the-art[245]. A beneﬁt of the probabilistic model is that the extension to mixtures of this model
is essentially straightforward, which boosts performance further. Related models can also be used for the
‘open set’ face recognition problem in which the probe face may or may not belong to one of the individuals
in the database[245].

21.4 Probabilistic Principal Components Analysis

PPCA[296] corresponds to Factor Analysis under the restriction Ψ = σ2ID. Plugging this assumption into
the eigen-solution equation (21.2.21) gives

F = σUH (ΛH

IH )

1
2 R

−

(21.4.1)

where the eigenvalues (diagonal entries of ΛH ) and corresponding eigenvectors (columns of UH ) are the
largest eigenvalues of σ−2S. Since the eigenvalues of σ−2S are those of S simply scaled by σ−2 (and the
eigenvectors are unchanged), we can equivalently write

F = UH

(cid:0)ΛH

σ2IH

(cid:1) 1

2 R

−

(21.4.2)

where R is an arbitrary orthogonal matrix with RTR = I and UH , ΛH are the eigenvectors and correspond-
ing eigenvalues of the sample covariance S. Classical PCA, section(15.2), is recovered in the limit σ2
0.
Note that for a full correspondence with PCA, one needs to set R = I, which points F along the principal
directions.

→

Optimal σ2

A particular convenience of PPCA is that the optimal noise σ2 can be found immediately. We order the
eigenvalues of S so that λ1
λD. In equation (21.2.20) an expression for the log likelihood is given
in which the eigenvalues are those σ−2S. On replacing λi with λi/σ2 we can therefore write an explicit
expression for the log likelihood in terms of σ2 and the eigenvalues of the sample covariance S,

λ2, ...

≥

≥

L(σ2) =

(cid:32)

N
2

−

D log(2π) +

H
(cid:88)

i=1

log λi +

1
σ2

D
(cid:88)

i=H+1

λi + (D

−

H) log σ2 + H

(cid:33)

By diﬀerentiating L(σ2) and equating to zero, the maximum likelihood optimal setting for σ2 is

σ2 =

1

−

H

D

D
(cid:88)

λj

j=H+1

(21.4.3)

(21.4.4)

In summary PPCA is obtained by taking the H principal eigenvalues and corresponding eigenvectors of the
sample covariance matrix S, and setting the variance by equation (21.4.4). The single-shot training nature
of PPCA makes it an attractive algorithm and also gives a useful initialisation for Factor Analysis.

444

DRAFT April 24, 2015

Canonical Correlation Analysis and Factor Analysis

Figure 21.7: For a 5 hidden unit model, here are plotted the
results of training PPCA and FA on 100 examples of the hand-
written digit seven. The top row contains the 5 Factor Analysis
factors and the bottom row the 5 largest eigenvectors from PPCA
are plotted.

Example 21.1 (A Comparison of FA and PPCA). We trained both PPCA and FA to model handwritten
digits of the number 7. From a database of 100 such images, we ﬁtted both PPCA and FA (100 iterations
of EM in each case from the same random initialisation) using 5 hidden units. The learned factors for these
models are in ﬁg(21.7). To get a feeling for how well each of these models the data, we drew 25 samples from
each model, as given in ﬁg(21.8a). Compared with PPCA, in FA the individual noise on each observation
pixel enables a cleaner representation of the regions of zero sample variance.

21.5 Canonical Correlation Analysis and Factor Analysis

We outline how CCA, as discussed in section(15.8), is related to a constrained form of FA. As a brief re-
minder, CCA considers two spaces X and Y where, for example, X might represent an audio sequence of a
person speaking and Y the corresponding video sequence of the face of the person speaking. The two streams
of data are dependent since we would expect the parts around the mouth region to be correlated with the
speech signal. The aim in CCA is to ﬁnd a low dimensional representation that explains the correlation
between the X and Y spaces.

A model that achieves a similar eﬀect to CCA is to use a latent factor h to underlie the data in both the X
and Y spaces, see ﬁg(21.5). That is

(cid:90)

p(x, y) =

p(x

h)p(y
|

h)p(h)dh
|

where

h) =

p(x

|

N

(x ha, Ψx) ,

p(y

h) =
|

N

(y hb, Ψy) ,

p(h) =

N

(h 0, 1)

We can express equation (21.5.2) as a form of Factor Analysis by writing

(cid:18) x
y

(cid:19)

(cid:18) a
b

=

(cid:19)

h +

(cid:18) (cid:15)x
(cid:15)y

(cid:19)

,

By using the stacked vectors

z =

(cid:18) x
y

(cid:19)

,

f =

(cid:18) a
b

(cid:19)

,

(cid:15)x

∼ N

((cid:15)x 0, Ψx) , (cid:15)y

∼ N

((cid:15)y 0, Ψy)

(21.5.1)

(21.5.2)

(21.5.3)

(21.5.4)

(a):

Figure 21.8:
25 sam-
ples from the learned FA model.
Note how the noise variance de-
pends on the pixel, being zero
for pixels on the boundary of the
(b): 25 samples from
image.
the learned PPCA model with
the same noise variance on each
pixel.

(a) Factor Analysis

(b) PPCA

DRAFT April 24, 2015

445

FAFAFAFAFAPCAPCAPCAPCAPCAIndependent Components Analysis

h

x

y

Figure 21.9: Canonical Correlation Analysis. CCA corresponds to the
latent variable model in which a common latent variable generates
both the observed x and y variables. This is therefore a formed of
constrained Factor Analysis.

and integrating out the latent variable h, we obtain

p(z) =

N

(z 0, Σ) ,

Σ = ﬀ T + Ψ,

Ψ =

(cid:18) Ψx

0
0 Ψy

(cid:19)

(21.5.5)

This is therefore clearly simply a factor analysis model, in this case with a single latent factor. We can learn
the best a and b by maximising the likelihood on a set of data. From the FA results equation (21.2.6) the
optimal f is given by (with S being the sample covariance matrix)

(cid:16)

f

1 + f TΨ−1f

(cid:17)

= SΨ−1f

f

⇒

∝

SΨ−1f

(21.5.6)

so that optimally f is given by the principal eigenvector of SΨ−1. By imposing Ψx = σ2
above equation can be expressed as the coupled equations

xI, Ψy = σ2

yI the

a

∝

1
σ2
x

Sxxa +

1
σ2
y

Sxyb,

b

∝

1
σ2
x

Syxa +

1
σ2
y

Syyb

Eliminating b we have, for an arbitrary proportionality constant γ,

(cid:18)
I

γ
σ2
x

−

(cid:19)

Sxx

a =

(cid:18)
I

Sxy

γ2
xσ2
σ2
y

γ
σ2
y

−

(cid:19)−1

Syy

Syxa

(21.5.7)

(21.5.8)

x, σ2

In the limit σ2
0, this tends to the zero derivative condition equation (15.8.9) so that CCA can be
seen as in fact a limiting form of FA (see [11] for a more thorough correspondence). By viewing CCA in
this manner extensions to using more than a single latent dimension H become clear, see exercise(21.2), in
addition to the beneﬁts of a probabilistic interpretation.

y →

As we’ve indicated, CCA corresponds to training a form of FA by maximising the joint likelihood p(x, y
w, u).
If the x represents rather an input and y the output, we are more interested in ﬁnding a good predictive
x, w, u) corresponds to a
representation. In this case, training based on maximising the conditional p(y
|
special case of a technique called Partial Least Squares, see for example [82]. This correspondence is left as
an exercise for the interested reader.

|

21.6 Independent Components Analysis

Independent Components Analysis (ICA) seeks a representation of data v using a coordinate system h in
which the components hi are independent[238, 151]. Such independent coordinate systems arguably form a
natural representation of the data. In ICA it is common to assume that the observations are linearly related
to the latent variables h. For technical reasons, the most convenient practical choice is to use4

v = Ah

(21.6.1)

where A is a square mixing matrix so that the likelihood of an observation v is

A) =

p(v

|

(cid:90)

p(v

h, A)
|

(cid:89)

i

(cid:90)

p(hi)dh =

δ (v

−

Ah)

(cid:89)

i

p(hi)dh =

1
det (A)
|
|

(cid:89)

i

p((cid:2)A−1v(cid:3)

i)

(21.6.2)

4This treatment follows that presented in [197].

446

DRAFT April 24, 2015

Independent Components Analysis

xi

(cid:112)
5
|
−

Figure 21.10: Latent data is sampled from the prior p(xi)
∝
) with the mixing matrix A shown in green to cre-
exp(
|
ate the observed two dimensional vectors y = Ax. The red lines
are the mixing matrix estimated by ica.m based on the obser-
vations. For comparison, PCA produces the blue (dashed) com-
ponents. Note that the components have been scaled to improve
visualisation. As expected, PCA ﬁnds the orthogonal directions
of maximal variation. ICA however, correctly estimates the di-
rections in which the components were independently generated.
See demoICA.m.

For a given set of data
likelihood is conveniently written in terms of B = A−1,

V

= (cid:0)v1, . . . , vN (cid:1) and prior p(h), our aim is to ﬁnd A. For i.i.d. data, the log

L(B) = N log det (B) +

(cid:88)

(cid:88)

n

i

log p([Bvn]i)

Note that for a Gaussian prior

p(h)

exp (cid:0)

h2(cid:1)

−

∝

the log likelihood becomes

L(B) = N log det (B)

(cid:88)

−

n

(vn)T BTBvn + const.

(21.6.3)

(21.6.4)

(21.6.5)

RB, with RTR = I. This means that for
which is invariant with respect to an orthogonal rotation B
a Gaussian prior p(h), we cannot estimate uniquely the mixing matrix. To break this rotational invariance
we therefore need to use a non-Gaussian prior. Assuming we have a non-Gaussian prior p(h), taking the
derivative w.r.t. Bab we obtain

→

∂
∂Bab

L(B) = N Aba +

(cid:88)

n

φ([Bv]a)vn
b

where

φ(x)

d
dx

≡

log p(x) =

1
p(x)

d
dx

p(x)

A simple gradient ascent learning rule for B is then

(cid:32)

Bnew = B + η

B−T +

(cid:33)

φ(Bvn) (vn)T

1
N

(cid:88)

n

(21.6.6)

(21.6.7)

(21.6.8)

An alternative ‘natural gradient’ algorithm[7, 197] that approximates a Newton update is given by multi-
plying the gradient by BTB on the right to give the update

(cid:32)

Bnew = B + η

I +

(cid:33)

φ(Bvn) (Bvn)T

B

1
N

(cid:88)

n

(21.6.9)

Here η is a learning rate which in the code ica.m we nominally set to 0.5. The performance of the algorithm
is relatively insensitive to the choice of the prior function φ(x). In ica.m we use the tanh function. An
example is given in ﬁg(21.10) in which the data clearly has an underlying ‘X’ shape, although the axes of
the ‘X’ are not orthogonal. In this case, the ICA and PCA representations are very diﬀerent, see ﬁg(21.10).
A natural extension is to consider noise on the outputs, exercise(21.6), for which an EM algorithm is readily
available. However, in the limit of low output noise, the EM formally fails, an eﬀect which is related to the
general discussion in section(11.4).

DRAFT April 24, 2015

447

−2−1.5−1−0.500.511.5−1.5−1−0.500.511.5A popular alternative estimation method is FastICA5 and can be related to an iterative maximum likelihood
optimisation procedure. ICA can also be motivated from several alternative directions, including information
theory[29]. We refer the reader to [151] for an in-depth discussion of ICA and related extensions.

Exercises

21.7 Summary

•

•

•

•

Factor Analysis is a classical probabilistic method for ﬁnding low-dimensional representations of the data.

There is no closed form solution in general and iterative procedures are typically used to ﬁnd the maximum
likelihood parameters.

Canonical Correlation Analysis is a special case of Factor Analysis.

Under the assumption of non-Gaussian latent variable priors, one is able to discover independent directions
in the data.

21.8 Code

FA.m: Factor Analysis
demoFA.m: Demo of Factor Analysis
ica.m: Independent Components Analysis
demoIca.m: Demo ICA

21.9 Exercises

Exercise 21.1. Factor Analysis and scaling. Assume that a H-factor model holds for x. Now consider the
transformation y = Cx, where C is a non-singular square diagonal matrix. Show that Factor Analysis is
scale invariant, i.e. that the H-factor model also holds for y, with the factor loadings appropriately scaled.
How must the speciﬁc factors be scaled?

Exercise 21.2. For the constrained Factor Analysis model

x =

(cid:19)

(cid:18) A 0
0 B

h + (cid:15),

(cid:15)

∼ N

((cid:15) 0, diag (ψ1, . . . , ψn)) ,

h

∼ N

(h 0, I)

(21.9.1)

derive a maximum likelihood EM algorithm for the matrices A and B, assuming the datapoints x1, . . . , xN
are i.i.d.

Exercise 21.3. An apparent extension of FA analysis is to consider a correlated prior

p(h) =

N

(h 0, ΣH )

(21.9.2)

Show that, provided no constraints are placed on the factor loading matrix F, using a correlated prior p(h)
is an equivalent model to the original uncorrelated FA model.

Exercise 21.4. Using the Woodbury identity and the deﬁnition of ΣD in equation (21.2.3), show that one
can rewrite Σ−1

D F as

Σ−1

D F = Ψ−1F

(cid:16)

I + FTΨ−1F

(cid:17)−1

5See www.cis.hut.fi/projects/ica/fastica/

(21.9.3)

448

DRAFT April 24, 2015

Exercises

Exercise 21.5. For the log likelihood function

L(σ2) =

N
2

−

(cid:32)

D log(2π) +

H
(cid:88)

i=1

log λi +

1
σ2

D
(cid:88)

i=H+1

λi + (D

−

H) log σ2 + H

(cid:33)

Show L(σ2) is maximal for

σ2 =

1

−

H

D

D
(cid:88)

λj

j=H+1

(21.9.4)

(21.9.5)

Exercise 21.6. Consider an ICA model in which y represents the outputs and x the latent components

p(y, x

|

W) =

(cid:89)

j

p(yj

x, W)
|

(cid:89)

i

p(xi),

W = [w1, . . . , wJ ]

with

p(yj

x, W) =
|

N

(cid:16)

yj wT

j x, σ2(cid:17)

(21.9.6)

(21.9.7)

1. For the above model derive an EM algorithm for a set of i.i.d. data y1, . . . , yN and show that the
(cid:105)p(x|yn,W) and (cid:10)xxT(cid:11)

required statistics for the M-step are

p(x|yn,W).

x
(cid:104)

2. Show that for a non-Gaussian prior p(xi), the posterior

p(x

y, W)
|

(21.9.8)

is non-factorised, non-Gaussian and generally intractable (its normalisation constant cannot be com-
puted eﬃciently).

3. Show that in the limit σ2

0, the EM algorithm fails.

→

DRAFT April 24, 2015

449

Exercises

450

DRAFT April 24, 2015

CHAPTER 22

Latent Ability Models

In this chapter we discuss an application of latent variable models to ascertaining the ability of players
in games. These models are applicable in a variety of contexts from exam performance to football match
prediction to online gaming analysis.

22.1 The Rasch Model

Consider an exam in which student s answers question q either correctly xqs = 1 or incorrectly xqs = 0.
For a set of N students and Q questions, the performance of all students is given in the Q
N binary
matrix X. Based on this data alone we wish to evaluate the ability of each student. One approach is to
deﬁne the ability as as the fraction of questions student s answered correctly. A more subtle analysis is to
accept that some questions are more diﬃcult than others so that a student who answered diﬃcult questions
should be awarded more highly than a student who answered the same number of easy questions. A priori,
however, we do not know which are the diﬃcult questions and this needs to be estimated based on X. To
account for inherent diﬀerences in question diﬃculty we may model the probability that a student s gets
a question q correct based on the student’s latent ability αs and the latent diﬃculty of the question δq. A
simple generative model of the response is, see ﬁg(22.2)

×

α, δ) = σ (αs
p(xqs = 1
|

−

δq)

(22.1.1)

where σ (x) = 1/(1 + e−x). Under this model, the higher the latent ability is above the latent diﬃculty of
the question, the more likely it is that the student will answer the question correctly.

22.1.1 Maximum likelihood training

We may use maximum likelihood to ﬁnd the best parameters α, δ. Making the i.i.d. assumption, the
likelihood of the data X under this model is

p(X

α, δ) =
|

S
(cid:89)

Q
(cid:89)

s=1

q=1

σ (αs

−

δq)xqs (1

σ (αs

−

−

δq))1−xqs

The log likelihood is then

L

≡

log p(X

α, δ) =
|

(cid:88)

q,s

xqs log σ (αs

δq) + (1

−

−

xqs) log (1

σ (αs

δq))

−

−

with derivatives

∂L
∂αs

=

Q
(cid:88)

q=1

(xqs

−

σ (αs

−

δq)) ,

∂L
∂δq

=

−

S
(cid:88)

s=1

(xqs

σ (αs

δq))

−

−

451

(22.1.2)

(22.1.3)

(22.1.4)

The Rasch Model

Figure 22.1: The Rasch model for analysing questions. Each
element of the binary matrix X, with xqs = 1 if student s gets
question q correct, is generated using the latent ability of the
student αs and the latent diﬃculty of the question δq.

Q

δq

xqs

αs

S

A simple way to learn the parameters is to use gradient ascent, see demoRasch.m, with extensions to Newton
methods being straightforward.

The generalisation to more than two responses xqs
can be achieved using a softmax-style
function. More generally, the Rasch model falls under item response theory, a subject dealing with the
analysis of questionnaires[101].

1, 2, . . .
}

∈ {

Missing data

Assuming the data is missing at random, missing data can be treated by computing the likelihood of only
the observed elements of X. In rasch.m missing data is assumed to be coded as nan so that the likelihood
and gradients are straightforward to compute based on summing only over terms containing non nan entries.

Example 22.1. We display an example of the use of the Rasch model
in ﬁg(22.2), estimat-
Based on using the
ing the latent abilities of 20 students based on a set of 50 questions.
number of questions each student answered correctly,
the best students are (ranked from ﬁrst)
8, 6, 1, 19, 4, 17, 20, 7, 15, 5, 12, 16, 2, 3, 18, 9, 11, 14, 10, 13. Alternatively, ranking students according to the
latent ability gives 8, 6, 19, 1, 20, 4, 17, 7, 15, 12, 5, 16, 2, 3, 18, 9, 11, 14, 10, 13. This diﬀers (only slightly in this
case) from the number-correct ranking since the Rasch model takes into account the fact that some students
answered diﬃcult questions correctly. For example student 20 answered some diﬃcult questions correctly.

22.1.2 Bayesian Rasch models

The Rasch model will potentially overﬁt the data especially when there is only a small amount of data. For
this case a natural extension is to use a Bayesian technique, placing independent priors on the ability and
question diﬃculty, so that the posterior ability and question diﬃculty is given by

p(α, δ

X)
|

∝

p(X

α, δ)p(α)p(δ)
|

Natural priors are those that discourage very large values of the parameters, such as Gaussians

p(α) =

(cid:89)
s N

(cid:0)αs 0, σ2(cid:1) ,

p(δ) =

(cid:89)
q N

(cid:0)δq 0, τ 2(cid:1)

(22.1.5)

(22.1.6)

σ2, τ 2). Even using Gaussian
where σ2 and τ 2 are hyperparameters that can be learned by maximising p(X
|
X) is not of a standard form and approximations are
priors, however, the posterior distribution p(α, δ
|
required.
In this case however, the posterior is log concave so that approximation methods based on
variational or Laplace techniques are potentially adequate, chapter(28); alternatively one may use sampling
approximations, chapter(27).

452

DRAFT April 24, 2015

Competition Models

(a)

(c)

(b)

(d)

Figure 22.2: Rasch model.
The estimated latent diﬃculty of each question.
correctly. (d): The estimated latent ability.

(a): The data of correct answers (white) and incorrect answers (black).

(b):
(c): The fraction of questions each student answered

22.2 Competition Models

22.2.1 Bradley-Terry-Luce model

The Bradley-Terry-Luce model assesses the ability of players based on one-on-one matches. Here we describe
games in which only win/lose outcomes arise, leaving aside the complicating possibility of draws. For this
win/lose scenario, the BTL model is a straightforward modiﬁcation of the Rasch model so that for latent
ability αi of player i and latent ability αj of player j, the probability that i beats j is given by

p(i (cid:66) j

α) = σ (αi
|

−

αj)

where i (cid:66) j stands for player i beats player j. Based on a matrix of games data X with

xn
ij =

(cid:26) 1
0

if i (cid:66) j in game n
otherwise

the likelihood of the model is given by

p(X

α) =
|

(cid:89)

(cid:89)

n

ij

[σ (αi

−

αj)]xn

ij =

(cid:89)

ij

[σ (αi

−

αj)]Mij

(22.2.1)

(22.2.2)

(22.2.3)

where Mij = (cid:80)
Bayesian technique can then proceed as for the Rasch model.

n xn

ij is the number of times player i beat player j. Training using maximum likelihood or a

These models are also called pairwise comparison models and pioneered by Thurstone in the 1920’s who
applied such models to a wide range of data [77].

DRAFT April 24, 2015

453

questionstudent5101520253035404550246810121416182005101520253035404550−8−6−4−20246questionestimated difficulty0246810121416182000.10.20.30.40.50.60.7studentfraction of questions correct02468101214161820−2.5−2−1.5−1−0.500.511.5studentestimated abilityCompetition Models

(a)

(b)

(a): The data M with Mij being the number of times that competitor i beat
Figure 22.3: BTL model.
competitor j. (b): The true versus estimated ability of the 100 competitors (unlabelled). Even though the
data is quite sparse, a reasonable estimate of the latent ability of each competitor is found.

Example 22.2. An example application of the BTL model is given in ﬁg(22.3) in which a matrix M
containing the number of times that competitor i beat competitor j is given. The matrix entries M were
drawn from a BTL model based on ‘true abilities’. Using M alone the maximum likelihood estimate of these
latent abilities is in close agreement with the true abilities.

22.2.2 Elo ranking model

The Elo system [95] used in chess ranking is closely related to the BTL model above, though there is the
added complication of the possibility of draws. In addition, the Elo system takes into account a measure
of the variability in performance. For a given ability αi, the actual performance πi of player i in a game is
given by

πi = αi + (cid:15)i

(22.2.4)
(cid:0)(cid:15)i 0, σ2(cid:1). The variance σ2 is ﬁxed across all players and thus takes into account intrinsic

where (cid:15)i
variability in the performance. More formally the Elo model modiﬁes the BTL model to give

∼ N

p(X

α) =
|

(cid:90)

π

p(X

π)p(π
|

α),
|

p(π

α) =
|

N

(cid:0)π α, σ2I(cid:1)

where p(X
|

π) is given by equation (22.2.3) on replacing α with π.

22.2.3 Glicko and TrueSkill

(22.2.5)

Glicko[126] and TrueSkill[142] are essentially Bayesian versions of the Elo model with the reﬁnement that
the latent ability is modelled, not by a single number, but by a Gaussian distribution

p(αi

θi) =
|

N

(cid:0)αi µi, σ2

i

(cid:1)

(22.2.6)

This can capture the fact that a player may be consistently reasonable (quite high µi and low σ2
erratic genius (high µi but with large σ2

i ). The parameters of the model are then

i ) or an

θ = (cid:8)µi, σ2

i , i = 1, . . . , S(cid:9)

(22.2.7)

for a set of S players. The interaction model p(X
α) is as for the win/lose Elo model, equation (22.2.1).
|
The likelihood for the model given the parameters is

p(X

θ) =
|

(cid:90)

α

p(X

α)p(α
|

|

θ)

454

(22.2.8)

DRAFT April 24, 2015

competitorcompetitor  2040608010010203040506070809010000.511.522.533.544.55−10−50510−10−50510true abilityestimated abilityExercises

This integral is formally intractable and numerical approximations are required. In this context expectation
propagation, section(28.8), has proven to be a useful technique[210]. The TrueSkillsystem is used for example
to assess the abilities of players in online gaming, also taking into account the abilities of teams of individuals
in tournaments. A temporal extension has recently been used to reevaluate the change in ability of chess
players with time[76].

22.3 Summary

The Rasch model is a simple model of latent student ability and question diﬃculty. In principle it is better
able to assess student performance than simply counting the number of correct questions since it implicitly
takes into account the fact that some questions are more diﬃcult than others.

Related models can be used to assess the underlying ability of players in games.

•

•

22.4 Code

rasch.m: Rasch model training
demoRasch.m: Demo for the Rasch model

22.5 Exercises

Exercise 22.1 (Bucking Bronco). bronco.mat contains information about a bucking bronco competition.
There are 500 competitors and 20 bucking broncos. A competitor j attempts to stay on a bucking bronco i
for a minute. If the competitor succeeds, the entry Xij is 1, otherwise 0. Each competitor gets to ride three
bucking broncos only (the missing data is coded as nan). Having viewed all the 500 amateurs, Desperate Dan
enters the competition and bribes the organisers into letting him avoid having to ride the diﬃcult broncos.
Based on using a Rasch model, which are the top 10 most diﬃcult broncos, in order of the most diﬃcult
ﬁrst?

Exercise 22.2 (BTL training).

1. Show that the log likelihood for the Bradley-Terry-Luce model is given by

L(α) =

(cid:88)

ij

Mij log σ (αi

αj)

−

(22.5.1)

where Mij is the number of times that player i beats player j in a set of games.

2. Compute the gradient of L(α).

3. Compute the Hessian of the BTL model and verify that it is negative semideﬁnite.

Exercise 22.3 (La Reine).

1. Program a simple gradient ascent routine to learn the latent abilities of competitors based on a series

of win/lose outcomes.

2. In a modiﬁed form of Swiss cow ‘ﬁghting’, a set of cows compete by pushing each other until submission.
At the end of the competition one cow is deemed to be ‘la reine’. Based on the data in BTL.mat (for
which Xij contains the number of times cow i beat cow j), ﬁt a BTL model and return a ranked list of
the top ten best ﬁghting cows, ‘la reine’ ﬁrst.

DRAFT April 24, 2015

455

Exercises

Exercise 22.4. An extension of the BTL model is to consider additional factors that describe the state
of the competitors when they play. For example, we have a set of S football teams, and a set of matrices
X1, . . . , XN , with X n
ij = 1 if team i beat team j in match n. In addition we have for each match n and team
i a vector of binary factors f n
, h = 1, . . . , H that describes the team. For example, for the team
0, 1
}
i = 1 (Madchester United), the factor f n
1,1 = 1 if Bozo is playing in match n, 0 if not. It is suggested that
the ability of team i in game n is measured by

h,i ∈ {

αn

i = di +

H
(cid:88)

h=1

wh,if n

h,i.

(22.5.2)

Here di is a default latent ability of the team which is assumed constant across all games. We have such a
set of factors for each match.

1. Using the above deﬁnition of the latent ability in the BTL model, our interest is to ﬁnd the weights
W and abilities d that best describe the ability of the team, given that we have a set of historical plays
(Xn, Fn), n = 1, . . . , N . Write down the likelihood for the BTL model as a function of the set of all
team weights W, d.

2. Compute the gradient of the log likelihood of this model.

3. Explain how this model can be used to assess the importance of Bozo’s contribution to Madchester

United’s ability.

4. Given learned W, d and the knowledge that Madchester United (team 1) will play Chelski (team 2)
tomorrow explain how, given the list of factors f for Chelski (which includes issues such as who will
be playing in the team), one can select the best Madchester United team to maximise the probability of
winning the game.

456

DRAFT April 24, 2015

Part IV

Dynamical Models

457

Introduction to Part IV

Natural organisms inhabit a dynamical environment and arguably a large part
intelligence is in modelling causal relations and consequences of ac-
of natural
tions.
In a
more artiﬁcial environment, there are many instances where predicting the future is
of interest, particularly in areas such as ﬁnance and also in tracking of moving objects.

In this sense, modelling temporal data is of fundamental

interest.

In Part IV, we discuss some of the classical models of timeseries that may be used to
represent temporal data and also to make predictions of the future. Many of these
models are well known in diﬀerent branches of science from Physics to Engineering
and are heavily used in areas such as speech recognition, ﬁnancial prediction and
control. We also discuss some more sophisticated models in chapter 25, which may be
skipped at ﬁrst reading.

As an allusion to the fact that natural organisms inhabit a temporal world, we also ad-
dress in chapter 26 some basic models of how information processing might be achieved
in distributed systems.

DRAFT April 24, 2015

459

Some dynamical graphical models for timeseries. In Part IV we cover inference and learning for most of the
standard models above.

460

DRAFT April 24, 2015

DynamicmodelsMarkovmodelsCompleteCont.OLDSARDiscreteMarkov-chainLatentDBN(factorisedstructure)discretelatentHMMcont.latentstate-spacemodelsLDSswitchingmodels(egSLDS)Non-MarkovmodelsGaussianprocessCHAPTER 23

Discrete-State Markov Models

Timeseries require specialised models since the number of variables can be very large and typically increases
as new datapoints arrive. In this chapter we discuss models in which the process generating the observed
data is fundamentally discrete. These models give rise to classical models with interesting applications in
many ﬁelds from ﬁnance to speech processing and website ranking.

23.1 Markov Models

Timeseries are datasets for which the constituent datapoints can be naturally ordered. This order often
corresponds to an underlying single physical dimension, typically time, though any other single dimension
may be used. The time-series models we consider are probability models over a collection of random variables
v1, . . . , vT with individual variables vt indexed by discrete time t. A probabilistic time-series model requires
a speciﬁcation of the joint distribution p(v1, . . . , vT ). For the case in which the observed data vt are discrete,
the joint probability table for p(v1, . . . , vT ) has exponentially many entries. We therefore cannot expect to
independently specify all the exponentially many entries and need to make simpliﬁed models under which
these entries can be parameterised in a lower dimensional manner. Such simpliﬁcations are at the heart of
timeseries modelling and we will discuss some classical models in the following sections.

Deﬁnition 23.1 (Time-Series Notation).

xa:b

≡

xa, xa+1, . . . , xb,

with xa:b = xa for b

a

≤

(23.1.1)

For timeseries data v1, . . . , vT , we need a model p(v1:T ). For consistency with the causal nature of time, it
is natural to consider the cascade decomposition

p(v1:T ) =

T
(cid:89)

t=1

v1:t−1)

p(vt

|

(23.1.2)

with the convention p(vt
v1:t−1) = p(v1) for t = 1. It is often useful to assume that the inﬂuence of the
|
immediate past is more relevant than the remote past and in Markov models only a limited number of
previous observations are required to predict the future, see ﬁg(23.1).

Deﬁnition 23.2 (Markov chain). A Markov chain deﬁned on either discrete or continuous variables v1:T is
one in which the following conditional independence assumption holds:

p(vt

v1, . . . , vt−1) = p(vt
|

vt−L, . . . , vt−1)
|

(23.1.3)

461

Markov Models

v1

v2

v3

v4

v1

v2

v3

v4

(a)

(b)

Figure 23.1: (a): First order Markov chain. (b): Second order Markov chain.

where L

≥

1 is the order of the Markov chain and vt =

∅

for t < 1. For a ﬁrst order Markov chain,

p(v1:T ) = p(v1)p(v2

v1)p(v3
|

v2) . . . p(vT
|

vT −1)
|

(23.1.4)

For a stationary Markov chain the transitions p(vt = s(cid:48)
the chain is non-stationary, p(vt = s(cid:48)

vt−1 = s) = f (s(cid:48), s, t).

|

|

vt−1 = s) = f (s(cid:48), s) are time-independent. Otherwise

For a discrete state ﬁrst order time-independent Markov chain one can visualise the transition p(vt
using a state-transition diagram, as in ﬁg(23.2).

vt−1)
|

1

3

2

Figure 23.2: A state transition diagram for a three state Markov
chain. Note that a state transition diagram is not a graphical
model – it simply displays the non-zero entries of the transition
matrix p(i
j). The absence of a link from j to i indicates that
|
j) = 0.
p(i
|

23.1.1 Equilibrium and stationary distribution of a Markov chain

For a transition p(xt
discrete state system, this is given by

xt−1), it is interesting to know how the marginal p(xt) evolves through time. For a
|

p(xt = i) =

(cid:88)

j

xt−1 = j)
p(xt = i
|
(cid:125)
(cid:123)(cid:122)
(cid:124)
Mij

p(xt−1 = j)

(23.1.5)

For an initial distribution p(x1), the above recursively deﬁnes the marginal for all future timepoints. The
marginal p(xt = i) has the interpretation of the frequency that we visit state i at time t, given we started
xτ −1). That
with a sample from p(x1) and subsequently repeatedly drew samples from the transition p(xτ
is, by drawing a state x1 = x1, from p(x1) we can draw samples x2, . . . , xt from the Markov chain by ﬁrst
x2 = x2) etc. As we repeatedly sample a new
drawing a sample from p(x2
|
state from the chain, the marginal distribution at time t, represented by the vector [pt]i =
p(xt = i) and
initial distribution p1 is

x1 = x1), and then from p(x3
|

|

@@

pt = Mpt−1 = Mt−1p1

(23.1.6)

, p∞ is independent of the initial distribution p1, then p∞ is called the equilibrium distribution
If, for t
of the chain. See exercise(23.2) for an example of a Markov chain which does not have an equilibrium
distribution. The so-called stationary distribution of a Markov chain is deﬁned by the condition

→ ∞

p∞(i) =

(cid:88)

j

xt−1 = j)p∞(j)
p(xt = i
|

In matrix notation this can be written as the vector equation

p∞ = Mp∞

(23.1.7)

(23.1.8)

so that the stationary distribution is proportional to the eigenvector with unit eigenvalue of the transition
matrix. Note that there may be more than one stationary distribution. See exercise(23.1) and [134].

462

DRAFT April 24, 2015

Markov Models

h

v1

v2

v3

v4

(a)

Figure 23.3: Mixture of ﬁrst order Markov chains. The
indexes
1, . . . , H
discrete hidden variable dom(h) =
the Markov chain (cid:81)
vt−1, h). Such models can
t p(vt
be useful as simple sequence clustering tools.

}

{

|

Example 23.1 (PageRank ). Despite their apparent simplicity, Markov chains have been put to interesting
use in information retrieval and search-engines. Deﬁne the matrix

Aij =

(cid:26) 1
0

if website j has a hyperlink to website i
otherwise

From this we can deﬁne a Markov transition matrix with elements

Mij =

Aij
i(cid:48) Ai(cid:48)j

(cid:80)

(23.1.9)

(23.1.10)

The equilibrium distribution of this Markov Chain has the interpretation : If we follow links at random,
jumping from website to website, the equilibrium distribution component p∞(i) is the relative number of
times we will visit website i. This has a natural interpretation as the ‘importance’ of website i; if a website
is isolated in the web, it will be visited infrequently by random hopping; if a website is linked by many
others it will be visited more frequently.

A crude search engine works as follows. For each website i a list of words associated with that website is
collected. After doing this for all websites, one can make an ‘inverse’ list of which websites contain word
w. When a user searches for word w, the list of websites that contain that word is then returned, ranked
according to the importance of the site (as deﬁned by the equilibrium distribution).

23.1.2 Fitting Markov models

Given a sequence v1:T , ﬁtting a stationary ﬁrst order Markov chain by maximum likelihood corresponds to
setting the transitions by counting the number of observed (ﬁrst-order) transitions in the sequence:

p(vτ = i
|

vτ −1 = j)

T
(cid:88)

t=2

∝

I [vt = i, vt−1 = j]

(23.1.11)

To show this, for convenience we write p(vτ = i
vτ −1 = j)
|
known):

≡

θi|j, so that the likelihood is (assuming v1 is

p(v2:T

θ, v1) =
|

T
(cid:89)

t=2

θvt|vt−1 =

T
(cid:89)

(cid:89)

t=2

i,j

I[vt=i,vt−1=j]
i|j

θ

Taking logs and adding the Lagrange constraint for the normalisation,

L(θ) =

T
(cid:88)

(cid:88)

t=2

i,j

I [vt = i, vt−1 = j] log θi|j +

(cid:32)
1

(cid:88)

λj

j

(cid:88)

−

i

θi|j

(cid:33)

(23.1.12)

(23.1.13)

Diﬀerentiating with respect to θi|j and equating to zero, we immediately arrive at the intuitive setting, equa-
tion (23.1.11). For a set of timeseries, vn
1:Tn, n = 1, . . . , N , the transition is given by counting all transitions
across time and datapoints. The maximum likelihood setting for the initial ﬁrst timestep distribution is
p(v1 = i)

I [vn

1 = i].

(cid:80)
n

∝

DRAFT April 24, 2015

463

Bayesian ﬁtting

For simplicity, we assume a factorised prior on the transition

p(θ) =

(cid:89)

j

p(θ·|j)

Markov Models

(23.1.14)

A convenient choice for each conditional transition is a Dirichlet distribution with hyperparameters uj,
p(θ·|j) = Dirichlet (cid:0)θ·|j|
uj

(cid:1), since this is conjugate to the categorical transition, giving

p(θ

v1:T )
|

∝

p(v1:T

θ)p(θ)
|

∝

(cid:89)

(cid:89)

t

i,j

I[vt=i,vt−1=j]
i|j

θ

uij −1
i|j

θ

=

(cid:89)

j

Dirichlet (cid:0)θ·|j|
ˆuj

(cid:1)

(23.1.15)

where ˆuj =

uij+ (cid:80)T

t=2

I [vt−1 = j, vt = i], being the number of j

i transitions in the dataset.

@@

→

23.1.3 Mixture of Markov models

=

vn
1:T , n = 1, . . . , N
{

Given a set of sequences
, how might we cluster them? To keep the notation less
cluttered, we assume that all sequences are of the same length T with the extension to diﬀering lengths being
straightforward. One simple approach is to ﬁt a mixture of Markov models. Assuming the data is i.i.d.,
n p(vn
p(
1:T ), we deﬁne a mixture model for a single sequence v1:T . Here we assume each component
model is ﬁrst order Markov

) = (cid:81)

V

V

}

p(v1:T ) =

H
(cid:88)

h=1

p(h)p(v1:T

h) =
|

H
(cid:88)

h=1

p(h)

T
(cid:89)

t=1

p(vt

vt−1, h)
|

(23.1.16)

The graphical model is depicted in ﬁg(23.3). Clustering can then be achieved by ﬁnding the maximum
likelihood parameters p(h), p(vt

vt−1, h) and subsequently assigning the clusters according to p(h
|
|

vn
1:T ).

EM algorithm

The EM algorithm, section(11.2), is particularly convenient for ﬁnding the maximum likelihood solution in
this case since the M-step can be performed simply. Under the i.i.d. data assumption, the log likelihood is

log p(

) =

V

N
(cid:88)

log

H
(cid:88)

n=1

h=1

p(h)

T
(cid:89)

t=1

p(vn
t |

vn
t−1, h)

For the M-step, our task is to maximise the energy

E =

N
(cid:88)

n=1

log p(vn
(cid:104)

1:T , h)

(cid:105)pold(h|vn

1:T ) =

(cid:40)

N
(cid:88)

(cid:104)

n=1

log p(h)

(cid:105)pold(h|vn

1:T ) +

T
(cid:88)

t=1

log p(vt
(cid:104)

The contribution to the energy from the parameter p(h) is

vt−1, h)
|

(cid:105)pold(h|vn

1:T )

N
(cid:88)

n=1

log p(h)
(cid:104)

(cid:105)pold(h|vn

1:T )

By deﬁning

ˆpold(h)

N
(cid:88)

n=1

∝

pold(h

vn
1:T )
|

one can view maximising (23.1.18) as equivalent to minimising

(cid:16)

KL

(cid:17)

ˆpold(h)
|

p(h)

so that the optimal choice from the M-step is to set pnew = ˆpold, namely

pnew(h)

N
(cid:88)

n=1

∝

vn
pold(h
1:T )
|

464

(23.1.17)

(cid:41)

(23.1.18)

(23.1.19)

(23.1.20)

(23.1.21)

DRAFT April 24, 2015

Markov Models

For those less comfortable with this argument, a direct maximisation including a Lagrange term to ensure
normalisation of p(h) can be used to derive the same result.

Similarly, the M-step for p(vt

vt−1, h) is
|

pnew(vt = i
vt−1 = j, h = k)
|

∝

N
(cid:88)

n=1

pold(h = k

vn
1:T )
|

T
(cid:88)

t=2

The initial term p(v1

h) is updated using
|

pnew(v1 = i
h = k)
|

∝

N
(cid:88)

n=1

pold(h = k

1:T )I [vn
vn
|

1 = i]

Finally, the E-step sets

I [vn

t = i] I (cid:2)vn

t−1 = j(cid:3)

vn
pold(h
1:T )
|

∝

p(h)p(vn

h) = p(h)

1:T |

T
(cid:89)

t=1

p(vn
t |

vn
t−1, h)

(23.1.22)

(23.1.23)

(23.1.24)

Given an initialisation, the EM algorithm then iterates (23.1.21),(23.1.22), (23.1.23) and (23.1.24) until
convergence.

For long sequences, explicitly computing the product of many terms may lead to numerical underﬂow issues.
In practice it is therefore best to work with logs,

vn
log pold(h
1:T ) = log p(h) +
|

T
(cid:88)

t=1

log p(vn
t |

vn
t−1, h) + const.

(23.1.25)

In this way any large constants common to all h can be removed and the distribution may be computed
accurately. See mixMarkov.m.

Example 23.2 (Gene Clustering). Consider the 20 ﬁctitious gene sequences below presented in an arbitrarily
chosen order. Each sequence consists of 20 symbols from the set
. The task is to try to cluster
these sequences into two groups, based on the assumption that gene sequences in the same cluster follow a
stationary Markov chain.

A, C, G, T
{

}

CATAGGCATTCTATGTGCTG
GTGCCTGGACCTGAAAAGCC
GTTGGTCAGCACACGGACTG
TAAGTGTCCTCTGCTCCTAA
GCCAAGCAGGGTCTCAACTT

CCAGTTACGGACGCCGAAAG
CGGCCGCGCCTCCGGGAACG
CCTCCCCTCCCCTTTCCTGC
CACCATCACCCTTGCTAAGG
CATGGACTGCTCCACAAAGG

TGGAACCTTAAAAAAAAAAA
AAAGTGCTCTGAAAACTCAC
CACTACGGCTACCTGGGCAA
AAAGAACTCCCCTCCCTGCC
AAAAAAACGAAAAACCTAAG

GTCTCCTGCCCTCTCTGAAC
ACATGAACTACATAGTATAA
CGGTCCGTCCGAGGCACTC
CAAATGCCTCACGCGTCTCA
GCGTAAAAAAAGTCCTGGGT

(23.1.26)

A simple approach is to assume that the sequences are generated from a two-component H = 2 mixture of
Markov models and train the model using maximum likelihood. The likelihood has local optima so that the
procedure needs to be run several times and the solution with the highest likelihood chosen. One can then
vn
assign each of the sequences by examining p(h = 1
1:T ). If this posterior probability is greater than 0.5, we
|
assign it to cluster 1, otherwise to cluster 2. Using this procedure, we ﬁnd the following clusters:

CATAGGCATTCTATGTGCTG
CCAGTTACGGACGCCGAAAG
CGGCCGCGCCTCCGGGAACG
ACATGAACTACATAGTATAA
GTTGGTCAGCACACGGACTG
CACTACGGCTACCTGGGCAA
CGGTCCGTCCGAGGCACTCG
CACCATCACCCTTGCTAAGG
CAAATGCCTCACGCGTCTCA
GCCAAGCAGGGTCTCAACTT
CATGGACTGCTCCACAAAGG

TGGAACCTTAAAAAAAAAAA
GTCTCCTGCCCTCTCTGAAC
GTGCCTGGACCTGAAAAGCC
AAAGTGCTCTGAAAACTCAC
CCTCCCCTCCCCTTTCCTGC
TAAGTGTCCTCTGCTCCTAA
AAAGAACTCCCCTCCCTGCC
AAAAAAACGAAAAACCTAAG
GCGTAAAAAAAGTCCTGGGT

(23.1.27)

where sequences in the ﬁrst column are assigned to cluster 1, and sequences in the second column to cluster
2. In this case the data in (23.1.26) were in fact generated by a two-component Markov mixture and the
posterior assignment (23.1.27) is in agreement with the known clusters. See demoMixMarkov.m

DRAFT April 24, 2015

465

h1

v1

h2

v2

h3

v3

h4

v4

23.2 Hidden Markov Models

Hidden Markov Models

Figure 23.4: A ﬁrst order hidden Markov model with
, t = 1 : T .
‘hidden’ variables dom(ht) =
The ‘visible’ variables vt can be either discrete or con-
tinuous.

1, . . . , H

}

{

The Hidden Markov Model (HMM) deﬁnes a Markov chain on hidden (or ‘latent’) variables h1:T . The
ht). This
observed (or ‘visible’) variables are dependent on the hidden variables through an emission p(vt
|
deﬁnes a joint distribution

p(h1:T , v1:T ) = p(v1

h1)p(h1)
|

T
(cid:89)

t=2

p(vt

ht)p(ht
|

ht−1)
|

(23.2.1)

for which the graphical model is depicted in ﬁg(23.4). For a stationary HMM the transition p(ht
emission p(vt
of the many applications of HMMs is given in section(23.5).

ht−1) and
|
ht) distributions are constant through time. The use of the HMM is widespread and a subset
|

Deﬁnition 23.3 (Transition Distribution). For a stationary HMM the transition distribution p(ht+1
deﬁned by the H

H transition matrix

×

ht) is
|

Ai(cid:48),i = p(ht+1 = i(cid:48)

ht = i)
|

and an initial distribution

ai = p(h1 = i).

Deﬁnition 23.4 (Emission Distribution). For a stationary HMM and emission distribution p(vt
discrete states vt

H emission matrix

1, . . . , V

∈ {

, we deﬁne a V
}

×

Bi,j = p(vt = i
|

ht = j)

(23.2.2)

(23.2.3)

ht) with

|

(23.2.4)

For continuous outputs, ht selects one of H possible output distributions p(vt

ht), ht
|

1, . . . , H

∈ {

.

}

In the engineering and machine learning communities, the term HMM typically refers to the case of discrete
variables ht, a convention that we adopt here. In statistics the term HMM often refers to any model with
the independence structure in equation (23.2.1), regardless of the form of the variables ht (see for example
[57]).

23.2.1 The classical inference problems

The common inference problems in HMMs are summarised below:

Filtering
Prediction
Smoothing
Likelihood
Most likely Hidden path (Viterbi alignment)

(Inferring the present)
(Inferring the future)
(Inferring the past)

v1:t)
p(ht
|
v1:s)
p(ht
|
p(ht
v1:u)
|
p(v1:T )
argmax
h1:T

p(h1:T

v1:T )
|

t > s
t < u

The most likely hidden path problem is termed Viterbi alignment in the engineering and speech recognition
literature. All these classical inference problems are computationally straightforward since the distribution
is singly-connected, so that any standard inference method can be adopted for these problems. The factor
graph and junction trees for the ﬁrst order HMM are given in ﬁg(23.5). In both cases, after suitable setting
of the factors and clique potentials, ﬁltering corresponds to passing messages from left to right and upwards;

466

DRAFT April 24, 2015

Hidden Markov Models

smoothing corresponds to a valid schedule of message passing/absorption both forwards and backwards along
all edges. It is also straightforward to derive appropriate recursions directly, as we will do below. This is
both instructive and also useful in constructing compact and numerically stable algorithms. The algorithms
we derive below only exploit the conditional independence statements in the belief network structure so
that analogous procedures hold for all models which have these conditional independencies; for example for
continuous states on replacing summation with integration.

23.2.2 Filtering p(ht

v1:t)
|

Filtering refers to ascertaining the distribution of the latent variable ht given all information up to the present
v1:t)
v1:t. To do so, we ﬁrst compute the joint marginal p(ht, v1:t) from which the conditional marginal p(ht
can subsequently be obtained by normalisation. A recursion for p(ht, v1:t) is obtained by considering:

|

p(ht, v1:t) =

=

=

(cid:88)

ht−1
(cid:88)

ht−1
(cid:88)

ht−1

p(ht, ht−1, v1:t−1, vt)

p(vt

(cid:24)(cid:24)(cid:24)v1:t−1, ht,(cid:8)
|

(cid:8)(cid:8)

ht−1)p(ht

(cid:24)(cid:24)(cid:24)v1:t−1, ht−1)p(v1:t−1, ht−1)
|

ht)p(ht

p(vt

|

|

ht−1)p(ht−1, v1:t−1)

(23.2.5)

(23.2.6)

(23.2.7)

The cancellations follow from the conditional independence assumptions of the model. Hence if we deﬁne

α(ht) = p(ht, v1:t)

equation (23.2.7) above gives the α-recursion

α(ht) = p(vt

ht)
|
(cid:124) (cid:123)(cid:122) (cid:125)
corrector

(cid:88)

ht−1
(cid:124)

p(ht

ht−1)α(ht−1)
|

,

t > 1

(cid:123)(cid:122)
predictor

(cid:125)

with

α(h1) = p(h1, v1) = p(v1

h1)p(h1)

|

(23.2.8)

(23.2.9)

(23.2.10)

This recursion has the interpretation that the ﬁltered distribution α(ht−1) is propagated forwards by the
dynamics for one timestep to reveal a new ‘prior’ distribution at time t. This distribution is then modulated
by the observation vt, incorporating the new evidence into the ﬁltered distribution (this is also referred to
as a predictor-corrector method). Since each α is smaller than 1, and the recursion involves multiplication
by terms less than 1, the α’s can become very small. To avoid numerical problems it is therefore advisable
to work with log α(ht), see HMMforward.m.

Normalisation gives the ﬁltered posterior

p(ht

v1:t)
|

∝

α(ht)

(23.2.11)

If we only require the ﬁltered posterior we are free to rescale the α’s as we wish. In this case an alternative
to working with log α messages is to work with normalised α messages so that (cid:80)

ht α(ht) = 1.

h1

v1

h3

v3

h2

v2

(a)

h4

v4

v1, h1

h1

h1, h2

h2

h2, h3

h3

h3, h4

h2

v2, h2

h3

v3, h3

h4

v4, h4

(b)

Figure 23.5: (a): Factor graph for the ﬁrst order HMM of ﬁg(23.4). (b): Junction tree for ﬁg(23.4).

DRAFT April 24, 2015

467

We can write equation (23.2.7) above directly as a recursion for the ﬁltered distribution

p(ht

v1:t)

|

∝

(cid:88)

ht−1

p(vt

ht)p(ht
|

ht−1)p(ht−1
|

v1:t−1)
|

t > 1

(23.2.12)

Hidden Markov Models

Intuitively, the term p(ht−1
replacing their inﬂuence by a modiﬁed ‘prior’ distribution on ht. One may interpret p(vt
likelihood, giving rise to the joint posterior p(ht, ht−1
the previous posterior becomes the new prior.

1 and
ht−1) as a
|
v1:t) under Bayesian updating. At the next timestep
|

v1:t−1) has the eﬀect of removing all nodes in the graph before time t

ht)p(ht
|

−

|

23.2.3 Parallel smoothing p(ht

v1:T )
|

There are two main approaches to computing p(ht
v1:T ). Perhaps the most common in the HMM literature
|
is the parallel method which is equivalent to message passing on factor graphs. In this one separates the
smoothed posterior into contributions from the past and future:

p(ht, v1:T ) = p(ht, v1:t, vt+1:T ) = p(ht, v1:t)
(cid:125)

(cid:124)

(cid:123)(cid:122)
past

p(vt+1:T
(cid:124)

|
(cid:123)(cid:122)
future

ht,(cid:8)(cid:8)v1:t)
(cid:125)

= α(ht)β(ht)

(23.2.13)

The cancellation above occurs due to the fact that ht d-separates the past from the future. The term α(ht)
is obtained from the ‘forward’ α recursion, (23.2.9). The β(ht) term may be obtained using a ‘backward’ β
recursion as we show below. The forward and backward recursions are independent and may therefore be
run in parallel, with their results combined to obtain the smoothed posterior.

The β recursion

p(vt:T

ht−1) =
|

=

=

(cid:88)

ht
(cid:88)

ht
(cid:88)

ht

Deﬁning

p(vt, vt+1:T , ht

ht−1)
|

p(vt

(cid:24)(cid:24)(cid:24)(cid:24)vt+1:T , ht,(cid:8)(cid:8)(cid:8)
|

ht−1)p(vt+1:T , ht

ht−1)
|

p(vt

ht)p(vt+1:T
|

ht,(cid:8)
|

(cid:8)(cid:8)

ht−1)p(ht

ht−1)
|

β(ht)

p(vt+1:T

ht)

|
equation (23.2.16) above gives the β-recursion

≡

β(ht−1) =

(cid:88)

ht

ht)p(ht

p(vt

|

|

ht−1)β(ht),

2

T

t

≤

≤

(23.2.14)

(23.2.15)

(23.2.16)

(23.2.17)

(23.2.18)

with β(hT ) = 1. As for the forward pass, working in log space is recommended to avoid numerical diﬃculties.
If one only desires posterior distributions, one can also perform local normalisation at each stage to give
(cid:80)
ht β(ht) = 1 since only the relative magnitude of the components of β are of importance. The smoothed

posterior is then given by

p(ht

|

v1:T )

≡

γ(ht) =

(cid:80)

α(ht)β(ht)
ht α(ht)β(ht)

Together the α

−

β recursions are called the Forward-Backward algorithm.

23.2.4 Correction smoothing

(23.2.19)

An alternative to the parallel method is to form a recursion directly for the smoothed posterior. This can
be achieved by recognising that conditioning on the present makes the future redundant:

p(ht

|

v1:T ) =

(cid:88)

ht+1

p(ht, ht+1

v1:T ) =

|

(cid:88)

ht+1

p(ht

|

ht+1, v1:t,(cid:24)(cid:24)(cid:24)(cid:24)vt+1:T )p(ht+1

v1:T )

|

(23.2.20)

468

DRAFT April 24, 2015

(23.2.21)

v1:t):
|

(23.2.22)

Hidden Markov Models

This gives a recursion for γ(ht)

p(ht

v1:T ):
|

≡

γ(ht) =

(cid:88)

ht+1

p(ht

ht+1, v1:t)γ(ht+1)
|

with γ(hT )

∝

p(ht

ht+1, v1:t) =
|

|
v1:t)
|
v1:t)
|

p(ht+1, ht
p(ht+1
v1:t) = (cid:80)
|

α(hT ). The term p(ht

ht+1, v1:t) may be computed using the ﬁltered results p(ht

=

p(ht+1

ht)p(ht
v1:t)
|
|
v1:t)
p(ht+1

|
v1:t) can be found by normalisation. This is a form of
where the term p(ht+1
|
dynamics reversal , as if we are reversing the direction of the hidden to hidden arrow in the HMM. This
procedure, also termed the Rauch-Tung-Striebel smoother1, is sequential since we need to ﬁrst complete
the α recursions, after which the γ recursion may begin. This is a so-called correction smoother since it
‘corrects’ the ﬁltered result. Interestingly, once ﬁltering has been carried out, the evidential states v1:T are
not needed during the subsequent γ recursion. The α

γ recursions are related through

ht p(ht+1

β and α

ht)p(ht

|

−

−

γ(ht)

∝

α(ht)β(ht)

(23.2.23)

Computing the pairwise marginal p(ht, ht+1

v1:T )

|
To implement the EM algorithm for learning, section(23.3.1), we require terms such as p(ht, ht+1
v1:T ).
These can be obtained by message passing on either a factor graph or junction tree (for which the pairwise
marginals are contained in the cliques, see ﬁg(23.4b)). Alternatively, an explicit recursion is as follows:

|

p(ht, ht+1

v1:T )

|

∝
= p(vt+2:T
= p(vt+2:T
= p(vt+2:T

p(v1:t, vt+1, vt+2:T , ht+1, ht)
(cid:40)(cid:40)(cid:40)(cid:40)(cid:40)(cid:40)
v1:t, vt+1, ht, ht+1)p(v1:t, vt+1, ht+1, ht)
|
ht+1)p(vt+1
|
ht+1)p(vt+1
|

(cid:24)(cid:24)(cid:24)(cid:24)
v1:t, ht, ht+1)p(v1:t, ht+1, ht)
|
ht+1)p(ht+1

(cid:8)(cid:8)v1:t, ht)p(v1:t, ht)
|

|

Rearranging, we therefore have

p(ht, ht+1

v1:T )

|

∝

α(ht)p(vt+1

ht+1)p(ht+1

ht)β(ht+1)
|

|

See HMMsmooth.m.

The likelihood p(v1:T )

The likelihood of a sequence of observations can be computed from

p(v1:T ) =

(cid:88)

hT

p(hT , v1:T ) =

α(hT )

(cid:88)

hT

An alternative computation can be found by making use of the decomposition

p(v1:T ) =

T
(cid:89)

t=1

v1:t−1)

p(vt

|

Each factor can be computed using

p(vt

v1:t−1) =
|

=

=

(cid:88)

ht
(cid:88)

ht
(cid:88)

ht

p(vt, ht

v1:t−1)

|

p(vt

p(vt

|

|

ht,(cid:24)(cid:24)(cid:24)v1:t−1)p(ht

v1:t−1)
|

ht)

(cid:88)

ht−1

p(ht

ht−1,(cid:24)(cid:24)(cid:24)v1:t−1)p(ht−1
|

v1:t−1)
|

(23.2.24)

(23.2.25)

(23.2.26)

(23.2.27)

(23.2.28)

(23.2.29)

(23.2.30)

1It is most common to use this terminology for the continuous variable case, though we adopt it here also for the discrete

variable case.

DRAFT April 24, 2015

469

where the ﬁnal term p(ht−1

v1:t−1) is the ﬁltered result.
|

In both approaches the likelihood of an output sequence requires only a forward computation (ﬁltering). If
required, one can also compute the likelihood using equation (23.2.13),

Hidden Markov Models

p(v1:T ) =

(cid:88)

ht

α(ht)β(ht)

which is valid for any 1

t

≤

≤

T .

23.2.5 Sampling from p(h1:T

v1:T )
|

(23.2.31)

Sometimes one wishes to sample joint trajectories h1:T from the posterior p(h1:T
v1:T ). A general purpose
|
method is described in section(27.2.2) which can be applied in this case by noting that conditioned on the
observations v1:T , one can write the latent distribution as a Markov network. This means that the posterior
distribution p(h1:T

v1:T ) is a simple linear Markov chain and we can reexpress the distribution as

|

p(h1:T

|

v1:T ) = p(h1

h2, v1:T ) . . . p(hT −1
|

hT , v1:T )p(hT
|

v1:T )
|

where, from equation (23.2.25),

p(ht−1

ht, v1:T )
|

∝

p(ht−1, ht

v1:T )

|

∝

α(ht−1)p(ht

ht−1)
|

(23.2.32)

(23.2.33)

v1:T ). Given this state, one then draws from
hT , v1:T ) using equation (23.2.33). This procedure is known as forward-ﬁltering-backward-sampling

Sampling then begins by ﬁrst drawing a state hT from p(hT
p(hT −t
since one ﬁrst needs to run ﬁltering to compute the required ‘time-reversed’ transitions p(ht−1

|

|

ht, v1:T ).
|

23.2.6 Most likely joint state

The most likely path h1:T of p(h1:T

|

v1:T ) is the same as the most likely state (for ﬁxed v1:T ) of

p(h1:T , v1:T ) =

(cid:89)

t

p(vt

ht)p(ht
|

ht−1)
|

(23.2.34)

The most likely path can be found using the max-product version of the factor graph or max-absorption on
the junction tree. Alternatively, an explicit derivation can be obtained by considering:

max
hT

T
(cid:89)

t=1

p(vt

ht)p(ht
|

ht−1) =
|

(cid:40)T −1
(cid:89)

t=1

(cid:41)

p(vt

ht)p(ht
|

ht−1)
|

p(vT

max
hT
(cid:124)

hT )p(hT
|

(cid:123)(cid:122)
µ(hT −1)

hT −1)
|

(cid:125)

(23.2.35)

The message µ(hT −1) conveys information from the end of the chain to the penultimate timestep. We can
continue in this manner, deﬁning the recursion

µ(ht−1) = max

ht

p(vt

ht)p(ht
|

ht−1)µ(ht),
|

2

t

≤

≤

T

(23.2.36)

with µ(hT ) = 1. This means that the eﬀect of maximising over h2, . . . , hT is compressed into a message
µ(h1) so that the most likely state h∗

1 is given by

h∗
1 = argmax

h1

p(v1

|

h1)p(h1)µ(h1)

Once computed, backtracking gives

h∗
t = argmax

ht

p(vt

ht)p(ht
|

h∗
t−1)µ(ht)
|

(23.2.37)

(23.2.38)

This special case of the max-product algorithm is called the Viterbi algorithm. Similarly, one may use the
N -max-product algorithm, section(5.2.1), to obtain the N -most likely hidden paths.

470

DRAFT April 24, 2015

Figure 23.6: Localising the burglar. The latent variable ht
∈
5 grid
1, . . . , 25
denotes the positions, deﬁned over the 5
}
{
of the ground ﬂoor of the house. (a): A representation of the
probability that the ‘ﬂoor will creak’ at each of the 25 positions,
p(vcreak
h). Light squares represent probability 0.9 and dark
|
(b): A representation of the probability p(vbump
h)
square 0.1.
that the burglar will bump into something in each of the 25 po-
sitions.

×

|

Hidden Markov Models

(a) ‘Creaks’

(b) ‘Bumps’

(a) Creaks and Bumps

(b) Filtering

(c) Smoothing

(d) Viterbi

(e) True Burglar position

(cid:16)

(cid:17)

, where vcreak

vcreak
t
= 2 otherwise) and vbump

(a): Each panel represents the
= 1 means that there was a ‘creak in the ﬂoorboard’

Figure 23.7: Localising the burglar through time for 10 time steps.
, vbump
visible information vt =
t
(vcreak
t
are 10 panels, one for each time t = 1, . . . , 10. The left half of the panel represents v1
The lighter shade represents the occurrence of a creak or bump, the darker shade the absence.
ﬁltered distribution p(ht
p(ht
future observations. (d): The most likely (Viterbi) burglar path arg maxh1:10 p(h1:10
path of the burglar.

= 1 meaning ‘bumped into something’ (and is in state 2 otherwise). There
t and the right half v2
t .
(b): The
(c): The smoothed distribution
v1:10) that represents the distribution of the burglar’s position given that we know both the past and
|
v1:10). (e): The actual

v1:t) representing where we think the burglar is.
|

|

t

t

23.2.7 Prediction

The one-step ahead predictive distribution is given by

p(vt+1

v1:t) =
|

(cid:88)

ht,ht+1

p(vt+1

ht+1)p(ht+1
|

ht)p(ht
|

v1:t)
|

(23.2.39)

Example 23.3 (A localisation example). You’re asleep upstairs in your house and awoken by noises from
downstairs. You realise that a burglar is on the ground ﬂoor and attempt to understand where he is from
listening to his movements. You mentally partition the ground ﬂoor into a 5
5 grid. For each grid position
you know the probability that if someone is in that position the ﬂoorboard will creak, ﬁg(23.6a). Similarly
you know for each position the probability that someone will bump into something in the dark, ﬁg(23.6b).
The ﬂoorboard creaking and bumping into objects can occur independently. In addition you assume that
the burglar will move only one grid square – forwards, backwards, left or right in a single timestep. Based
on a series of bump/no bump and creak/no creak information, ﬁg(23.7a), you try to ﬁgure out based on
your knowledge of the ground ﬂoor, where the burglar might be.

×

We can represent the scenario using a HMM where h
variable has a composite form v = vcreak

denotes the grid square. The visible
vbump where vcreak and vbump each take two states. To use our

1, . . . , 25
}

∈ {

⊗

DRAFT April 24, 2015

471

m1

m2

m3

h1

v1

h2

v2

h3

v3

h4

v4

Hidden Markov Models

Figure 23.8: A model for robot self-localisation. At
each time the robot makes an intended movement, mt.
As a generative model, knowing the intended move-
ment mt and the current grid position ht, the robot
has an idea of where he should be at the next time-
step and what sensor reading vt+1 he would expect
there. Based on only the sensor information v1:T and
the intended movements m1:T , the task is to infer a
distribution over robot locations p(h1:T

m1:T , v1:T ).
|

standard code we form a new visible variable v with four states using

p(v

h) = p(vcreak
|

h)p(vbump
|

h)
|

(23.2.40)

|

Based on the past information, our belief as to where the burglar might be is represented by the ﬁltered
v1:t), ﬁg(23.7). In the beginning the ﬁltered distribution has signiﬁcant mass in many states
distribution p(ht
since we don’t yet have suﬃcient information to establish where the burglar is. As time goes along and we
gather more information, the ﬁltered distribution becomes more concentrated in a small number of states.
After the burglar has left at T = 10, the police arrive and try to piece together where the burglar went,
based on the sequence of creaks and bumps you provide. At any time t, the information as to where the
burglar could have been is represented by the smoothed distribution p(ht
v1:10). The smoothed distribution
|
is generally more concentrated in a few states than the ﬁltered distribution since we have more information
(from the past and future) to help establish the burglar’s position at that time. The police’s single best-guess
v1:10).
for the trajectory the burglar took is provided by the most likely joint hidden state arg maxh1:10 p(h1:10
See demoHMMburglar.m.

|

23.2.8 Self localisation and kidnapped robots

A robot has an internal grid-based map of its environment and for each location h
knows the
likely sensor readings he would expect in that location. The robot is ‘kidnapped’ and placed somewhere in
the environment. The robot then starts to move, gathering sensor information. Based on these readings
v1:t and intended movements m1:t, the robot attempts to ﬁgure out his location by comparing his actual
sensor readings with his internal map of expected sensor readings for each location. Due to wheel slippage
on the ﬂoor an intended action by the robot, such as ‘move forwards’, might not be successful. Given all
the information the robot has, he would like to infer p(ht
v1:t, m1:t). This problem diﬀers from the burglar
|
scenario in that the robot now has knowledge of the intended movements he makes. This should give more
information as to where he could be. One can view this as extra ‘visible’ information, though it is more
natural to think of this as additional input information. A model of this scenario is, see ﬁg(23.9),

1, . . . , H

∈ {

}

p(v1:T , m1:T , h1:T ) =

T
(cid:89)

t=1

p(vt

ht)p(ht
|

ht−1, mt−1)p(mt)
|

(23.2.41)

The visible variables v1:T are known, as are the intended movements m1:T . The model expresses that the
movements selected by the robot are random (hence no decision making in terms of where to go next).
We assume that the robot has full knowledge of the conditional distributions deﬁning the model (he knows
the ‘map’ of his environment and all state transition and emission probabilities). If our interest is only in
localising the robot, since the inputs m are known, this model is in fact a form of time-dependent HMM:

p(v1:T , h1:T ) =

T
(cid:89)

t=1

p(vt

ht)p(ht
|

ht−1, t)
|

(23.2.42)

ht−1, t) deﬁned by the intended movement mt−1. Any inference task
for a time-dependent transition p(ht
required then follows the standard stationary HMM algorithms, albeit on replacing the time-independent

|

472

DRAFT April 24, 2015

Hidden Markov Models

(a)

(b)

(c)

Figure 23.9: Filtering and smoothing for robot tracking using a HMM with S = 50. (a): A realisation from
the HMM example described in the text. The dots indicate the true latent locations of the robot, whilst
the open circles indicate the noisy measured locations. (b): The squares indicate the ﬁltering distribution
at each timestep t, p(ht
v1:t). This probability is proportional to the grey level with black corresponding to
1 and white to 0. Note that the posterior for the ﬁrst timesteps is multimodal, therefore the true position
(c): The squares indicate the smoothing distribution at each timestep t,
cannot be accurately estimated.
p(t
v1:T ). Note that, for t < T , we estimate the position retrospectively and the uncertainty is signiﬁcantly
|
lower when compared to the ﬁltered estimates.

|

transitions p(ht

ht−1) with the known time-dependent transitions.
|

In self localisation and mapping (SLAM) the robot does not know the map of his environment. This corre-
sponds to having to learn the transition and emission distributions on-the-ﬂy as he explores the environment.

Example 23.4 (Robot localisation).

Consider the following toy tracking problem (from Taylan Cemgil). A robot
is moving around a circular corridor and at any time occupies one of S
possible locations, as indicated. At each timestep t, the robot stays where
it is with probability (cid:15), or moves to the next point in a counter-clockwise
direction with probability 1

(cid:15).

−

This can be conveniently represented by an S
example, for S = 3, we have






A = (cid:15)



 + (1

(cid:15))



−

1 0 0
0 1 0
0 0 1

0 0 1
1 0 0
0 1 0

S matrix A with elements Aji = p(ht = j

ht−1 = i). For
|

×



 .

(23.2.43)

At each timestep t, the robot sensors measure its position, obtaining either the correct location with prob-
ability w or a uniformly random location with probability 1

w. For example, for S = 3, we have

−

B = w





1 0 0
0 1 0
0 0 1



 +

w)

(1

−
3





1 1 1
1 1 1
1 1 1



 .

(23.2.44)

A typical realisation y1:T from the process deﬁned by this HMM with S = 50, (cid:15) = 0.5, T = 30 and w = 0.3 is
depicted in ﬁg(23.9a). We are interested in inferring the true locations of the robot from the noisy measured
locations. At each time t, the true location can be inferred from the ﬁltered posterior p(ht
v1:t), ﬁg(23.9b),
|
which uses measurements up to t; or from the smoothed posterior p(ht
v1:T ), ﬁg(23.9c), which uses both
past and future observations and is therefore generally more accurate.

|

DRAFT April 24, 2015

473

23.2.9 Natural language models

A simple generative model of language can be obtained from the letter-to-letter transitions (a so-called
bigram). In the example below, we use this in a HMM to clean up mis-typings.

Learning HMMs

Example 23.5 (Stubby ﬁngers). A ‘stubby ﬁngers’ typist has the tendency to hit either the correct key
or a single neighbouring key. For simplicity we assume that there are 27 keys: lower case a to lower case z
and the space bar. To model this we use an emission distribution Bij = p(v = i
h = j) where i = 1, . . . , 27,
|
j = 1, . . . , 27, as depicted in ﬁg(23.10). A database of letter-to-next-letter frequencies, yields the transition
matrix Aij = p(h(cid:48) = i
h = j) in English. For simplicity we assume that p(h1) is uniform. Also we assume
|
that each intended key press results in a single press. Given a typed sequence kezrninh what is the most
likely word that this corresponds to? By listing the 200 most likely hidden sequences (using the N -max-
product algorithm) and discarding those that are not in a standard English dictionary, the most likely word
that was intended is learning. See demoHMMbigram.m.

(a)

(b)

(b): The letter
Figure 23.10:
emission matrix for a typist with ‘stubby ﬁngers’ in which the key or a neighbour on the keyboard is likely
to be hit.

(a): The letter-to-letter transition matrix for English p(h(cid:48) = i
h = j).
|

23.3 Learning HMMs

V

= (cid:8)v1, . . . , vN (cid:9) of N sequences, where sequence vn = vn

Given a set of data
1:Tn is of length Tn, we seek
the HMM transition matrix A, emission matrix B, and initial vector a most likely to have have generated
. We make the i.i.d. assumption so that each sequence is independently generated and assume that we
V
know the number of hidden states H. For simplicity we concentrate here on the case of discrete visible
variables, assuming also we know the number of states V . Whilst implementing either EM or gradient
based maximum likelihood is straightforward for the HMM, the likelihood has many local optima, and care
needs to be taken with initialisation.

23.3.1 EM algorithm

The application of EM to the HMM model is called the Baum-Welch algorithm and follows the general
strategy outlined in section(11.2). The EM algorithm is convenient in this case and leads to closed form
expressions for the M-step.

474

DRAFT April 24, 2015

  abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz 00.10.20.30.40.50.60.70.80.9  abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz 0.050.10.150.20.250.30.350.40.450.50.55Learning HMMs

M-step

Assuming i.i.d. data, the M-step is given by maximising the ‘energy’:

N
(cid:88)

(cid:104)

n=1

log p(vn

1 , vn

2 . . . , vn

T n, hn

1 , hn

2 , . . . , hn

T n)

(cid:105)pold(hn|vn)

(23.3.1)

with respect to the parameters A, B, a; hn denotes h1:Tn. Using the form of the HMM, we obtain

(cid:40)

log p(h1)
(cid:104)

(cid:105)pold(h1|vn) +

N
(cid:88)

n=1

Tn−1
(cid:88)

t=1

log p(ht+1
(cid:104)

ht)
|

(cid:105)pold(ht,ht+1|vn) +

log p(vn
t |

ht)

(cid:104)

(cid:105)pold(ht|vn)

(23.3.2)

(cid:41)

Tn(cid:88)

t=1

where for compactness we drop the sequence index from the h variables. To avoid potential confusion, we
write pnew(h1 = i) to denote the (new) table entry for the probability that the initial hidden variable is in
state i. Optimising equation (23.3.2) with respect to p(h1), (and enforcing p(h1) to be a distribution) we
obtain

anew
i ≡

pnew(h1 = i) =

1
N

N
(cid:88)

n=1

vn)
pold(h1 = i
|

(23.3.3)

which is the average number of times (with respect to pold) that the ﬁrst hidden variable is in state i.
Similarly, the M-step for the transition is

Anew

i(cid:48),i ≡

pnew(ht+1 = i(cid:48)

ht = i)

|

∝

N
(cid:88)

Tn−1
(cid:88)

n=1

t=1

pold(ht = i, ht+1 = i(cid:48)

vn)
|

(23.3.4)

which is the number of times that a transition from hidden state i to hidden state i(cid:48) occurs, averaged over
all times (since we assumed stationarity) and training sequences. Normalising, we obtain

Anew

i(cid:48),i =

(cid:80)N

(cid:80)Tn−1

n=1
(cid:80)N

n=1

(cid:80)
i(cid:48)

t=1 pold(ht = i, ht+1 = i(cid:48)
(cid:80)Tn−1

|
t=1 pold(ht = i, ht+1 = i(cid:48)

vn)

vn)
|

Finally, the M-step update for the emission is

Bnew

j,i ≡

pnew(vt = j

ht = i)
|

∝

N
(cid:88)

Tn(cid:88)

n=1

t=1

I [vn

t = j] pold(ht = i
|

vn)

(23.3.5)

(23.3.6)

which is the expected number of times that, for the observation being in state j, the hidden state is i. The
proportionality constant is determined by the normalisation requirement.

E-step

In computing the M-step above the quantities pold(h1 = i
are obtained by smoothed inference using the techniques described in section(23.2.1).

vn), pold(ht = i, ht+1 = i(cid:48)
|

vn) and pold(ht = i
|
|

vn)

Equations (23.3.3,23.3.5,23.3.6) are repeated until convergence. See HMMem.m and demoHMMlearn.m.

Parameter initialisation

The EM algorithm converges to a local maximum of the likelihood and, in general, there is no guarantee that
the algorithm will ﬁnd the global maximum. How best to initialise the parameters is a thorny issue, with a
suitable initialisation of the emission distribution often being critical for success[246]. A practical strategy is
h)p(h)
to initialise the emission p(v
|
to the data.

h) based on ﬁrst ﬁtting a simpler non-temporal mixture model (cid:80)
|

h p(v

DRAFT April 24, 2015

475

Learning HMMs

Continuous observations

For a continuous vector observation vt, with dim (vt) = D, we require a model p(vt
ht) mapping the discrete
|
state ht to a distribution over outputs. Using a continuous output does not change any of the standard
inference message passing equations so that inference can be carried out for essentially arbitrarily complex
emission distributions. Indeed, for ﬁltering, smoothing and Viterbi inference, the normalisation Z of the
h) = φ(v, h)/Z is not required. For learning, however, the emission normalisation constant is
emission p(v
|
required since this is dependent on the parameters of the model.

23.3.2 Mixture emission

To make a richer emission model (particularly for continuous observations), one approach is use a mixture

p(vt

ht) =
|

(cid:88)

kt

p(vt

|

kt, ht)p(kt

ht)
|

(23.3.7)

where kt is a discrete summation variable. For learning, it is useful to consider the kt as additional latent
variables; the EM algorithm then carries through in a straightforward way. Using the notation q as shorthand
for pold, the E-step sets:

q(kt

hn
t )
|

∝

p(vn

hn
t , kt)p(kt
|

ht)
|

The energy is given by

E =

(cid:88)

T
(cid:88)

(cid:68)

n

t=1

log q(kt

− (cid:104)

hn
t )

|

(cid:105)q(kt|hn

t ) +

log p(vn
t |
(cid:104)

kt, hn
t )

(cid:105)q(kt|hn

t ) +

log p(kt
(cid:104)

hn
t )
|

(cid:105)q(kt|hn
t )

(cid:69)

q(hn

t |vn

1:T )

The contribution from each emission component p(v = v

h = h, k = k) is
|

(cid:88)

T
(cid:88)

n

t=1

q(kt = k
|

t = h)q(hn
hn

t = h

1:T ) log p(vn
vn
t |
|

h = h, k = k)

(23.3.8)

(23.3.9)

(23.3.10)

hn
t = h), one needs to numerically optimise the above expression with respect to the
For ﬁxed q(kt = k
|
emission parameters. Similarly, the contribution to the energy bound from the mixture weights is given by

log p(k = k
|

h = h)

(cid:88)

T
(cid:88)

n

t=1

t = h)q(hn
hn
q(kt = k
|

t = h
|

vn
1:T )

so that the M-step update for the mixture weights is,

p(k = k
|

h = h)

∝

(cid:88)

T
(cid:88)

n

t=1

t = h)q(hn
hn
q(kt = k
|

t = h

vn
1:T )
|

(23.3.11)

(23.3.12)

In this case the EM algorithm is composed of an ‘emission’ EM loop in which the transitions and q(hn
h
The ‘transition’ EM loop ﬁxes the emission distribution p(v

t =
hn
h, k) are learned, along with updating q(kt = k
t = h).
|
|
ht−1).
h) and learns the best transition p(ht
|
|

vn
1:T ) are ﬁxed, during which the emissions p(v
|

23.3.3 The HMM-GMM

A common continuous observation mixture emission model component is a Gaussian

p(vt

|

kt, ht) =

N

(cid:0)vt µkt,ht, Σkt,ht

(cid:1)

(23.3.13)

so that kt, ht indexes the K
H mean vectors and covariance matrices. EM updates for these means and
covariances are straightforward to derive from equation (23.3.9), see exercise(23.14). These models are
common in tracking applications and speech recognition (usually under the constraint that the covariances
are diagonal).

×

476

DRAFT April 24, 2015

Related Models

c1

h1

v1

c2

h2

v2

c3

h3

v3

c4

h4

v4

23.3.4 Discriminative training

Figure 23.11: An explicit duration HMM. The counter
variables ct deterministically count down to zero.
When they reach one, a h transition is allowed, and a
new value for the duration ct is sampled.

HMMs can be used for supervised learning of sequences. That is, for each sequence vn
1:T , we have a cor-
responding class label cn. For example, we might associate a particular composer c
with a
sequence v1:T and wish to make a model that will predict the composer for a novel music sequence. A
generative approach to using HMMs for classiﬁcation is to train a separate HMM for each class, p(v1:T
c)
|
and subsequently use Bayes’ rule to form the classiﬁcation for a novel sequence v∗

1, . . . , C

∈ {

}

1:T using

p(c∗

|

v∗
1:T ) =

p(v∗
1:T |
c(cid:48)=1 p(v∗

(cid:80)C

c∗)p(c∗)

c(cid:48))p(c(cid:48))

1:T |

(23.3.14)

If the data are noisy and diﬃcult to model, however, this generative approach may not work well since
much of the expressive power of each model is used to model the complex data, rather than focussing on
the decision boundary. In applications such as speech recognition, improvements in performance are often
reported when the models are trained in a discriminative way. In discriminative training, see for example
[163], one deﬁnes a new single discriminative model, formed from the C HMMs using

v1:T ) =
p(c
|

p(v1:T
c(cid:48)=1 p(v1:T

c)p(c)
|

(cid:80)C

c(cid:48))p(c(cid:48))
|

(23.3.15)

and then maximises the likelihood of a set of observed classes and corresponding observations v1:T . For a
single data pair, (cn, vn

1:T ), the log likelihood is

log p(cn

1:T ) = log p(vn
vn
1:T |
|
(cid:124)
(cid:123)(cid:122)
generative likelihood

cn)
(cid:125)

+ log p(cn)

log

−

C
(cid:88)

c(cid:48)=1

p(vn

c(cid:48))p(c(cid:48))

1:T |

(23.3.16)

The ﬁrst term above represents the generative likelihood term, with the last term accounting for the dis-
crimination. Whilst deriving EM style updates is hampered by the discriminative terms, computing the
gradient is straightforward using the technique described in section(11.6).

23.4 Related Models

23.4.1 Explicit duration model

ht−1 = i)
|

θi, the probability that the latent dynamics stays in
For a HMM with self-transition p(ht = i
state i for τ timesteps is θτ
i , which decays exponentially with time. In practice, however, we would often
like to constrain the dynamics to remain in the same state for a minimum number of timesteps, or to have
a speciﬁed duration distribution. A way to enforce this is to use a latent counter variable ct which at the
beginning is initialised to a duration sampled from the duration distribution pdur(ct) with maximal duration
Dmax. Then at each timestep the counter decrements by 1, until it reaches 1, after which a new duration is
sampled:

≡

p(ct

ct−1) =
|

(cid:26) δ (ct, ct−1
pdur(ct)

1)

−

ct−1 > 1
ct−1 = 1

The state ht can transition only when ct = 1:

p(ht

|

ht−1, ct) =

(cid:26) δ (ht, ht−1)
ptran(ht

ht−1)
|

ct > 1
ct = 1

DRAFT April 24, 2015

(23.4.1)

(23.4.2)

477

Related Models

Including the counter variable c deﬁnes a joint latent distribution p(c1:T , h1:T ) that ensures h remains in a
desired minimal number of timesteps, see ﬁg(23.11). Since dim (ct
ht) = DmaxH, naively the computa-
⊗
(cid:1). However, when one runs the forward
tional complexity of inference in this model scales as O (cid:0)T H 2D2
and backward recursions, the deterministic nature of the transitions means that this can be reduced to
O (cid:0)T H 2Dmax

(cid:1)[214] – see also exercise(23.15).

max

The hidden semi-Markov model generalises the explicit duration model in that once a new duration ct is
ht) deﬁned on a segment of the next ct observations[232].
sampled, the model emits a distribution p(vt:t+ct−1
|

23.4.2 Input-Output HMM

The IOHMM[31] is a HMM with additional input variables x1:T , see ﬁg(23.12). Each input can be continuous
or discrete and modulates the transitions

p(v1:T , h1:T

x1:T ) =
|

(cid:89)

t

p(vt

|

ht, xt)p(ht

ht−1, xt)
|

(23.4.3)

The IOHMM may be used as a conditional predictor, where the outputs vt represent the prediction at time
ht−1, xt) are usually
t. In the case of continuous inputs and discrete outputs, the tables p(vt
|
parameterised using a non-linear function, for example

ht, xt) and p(ht

|

p(vt = y

|

ht = h, xt = x, w)

∝

(cid:16)

exp

wT

h,yφh,y(x)

(cid:17)

(23.4.4)

@@

where φh,y(x) is a vector function of the input x. Inference then follows in a similar manner as for the
standard HMM. Deﬁning

@@

α(ht)

p(ht, v1:t

x1:t)
|

≡

the forward pass is given by

α(ht) =

=

(cid:88)

ht−1
(cid:88)

ht−1

p(ht, ht−1, v1:t−1, vt

x1:t)

|

p(vt

v1:t−1, x1:t, ht, ht−1)p(ht
|

|

v1:t−1, x1:t, ht−1)p(v1:t−1, ht−1

x1:t)
|

= p(vt

xt, ht)

|

(cid:88)

ht−1

p(ht

ht−1, xt)α(ht−1)
|

The γ backward pass is

(23.4.5)

(23.4.6)

(23.4.7)

(23.4.8)

p(ht

|

x1:T , v1:T ) =

(cid:88)

ht+1

p(ht, ht+1

|

x1:t+1, xt+2:T , v1:T ) =

(cid:88)

ht+1

p(ht

ht+1, x1:t+1, v1:t)p(ht+1
|

x1:T , v1:T )
|

for which we need

p(ht

ht+1, x1:t+1, v1:t) =

p(ht+1, ht
p(ht+1
The likelihood can be found from (cid:80)

|

hT

x1:t+1, v1:t)
|
x1:t+1, v1:t)
|

=

(cid:80)

p(ht+1
ht p(ht+1

ht, xt+1)p(ht
|

x1:t, v1:t)
|

ht, xt+1)p(ht
|

x1:t, v1:t)
|

(23.4.9)

(23.4.10)

α(hT ).

x4

h4

v4

Figure 23.12: A ﬁrst order input-output hidden
Markov model. The input x and output v nodes are
shaded to emphasise that their states are known dur-
ing training. During testing, the inputs are known and
the outputs are predicted.

x1

h1

v1

x2

h2

v2

x3

h3

v3

478

DRAFT April 24, 2015

Related Models

x

y2

y3

y1

Direction bias

Figure 23.13: Linear chain CRF. Since the input x is observed, the distribu-
tion is just a linear chain factor graph. The inference of pairwise marginals
x) is therefore straightforward using message passing.
p(yt, yt−1

|

x1:T ) given both past and future input information x1:T .
Consider predicting the output distribution p(vt
|
Because the hidden states are unobserved we have p(vt
x1:t). Thus the IOHMM prediction uses
x1:T ) = p(vt
|
only past information and discards any future contextual information. This ‘direction bias’ is sometimes
considered problematic (particularly in natural language modelling) and motivates the use of undirected
models, such as conditional random ﬁelds.

|

23.4.3 Linear chain CRFs

Linear chain Conditional Random Fields (CRFs) are an extension of the unstructured CRFs we brieﬂy
discussed in section(9.6.5) and have application to modelling the distribution of a set of outputs y1:T given
an input vector x. For example, x might represent a sentence in English, and y1:T should represent the
translation into French. Note that the vector x does not have to have dimension T . A ﬁrst order linear
chain CRF has the form

p(y1:T

x, λ) =
|

1
Z(x, λ)

T
(cid:89)

t=2

φt(yt, yt−1, x, λ)

(23.4.11)

where λ are the free parameters of the potentials. In practice it is common to use potentials of the form

exp

(cid:32) K
(cid:88)

k=1

(cid:33)

λkfk,t(yt, yt−1, x)

(23.4.12)

where fk,t(yt, yt−1, x) are ‘features’, see also section(9.6.5). Given a set of input-output sequence pairs,
(xn, yn
(assuming all sequences have equal length T for simplicity), we can learn the
{
parameters λ by maximum likelihood. Under the standard i.i.d. data assumption, the log likelihood is

1:T ) , n = 1, . . . , N

}

L(λ) =

(cid:88)

(cid:88)

t,n

k

λkfk(yn

t , yn

t−1, xn)

(cid:88)

−

n

log Z(xn, λ)

(23.4.13)

The reader may readily check that the log likelihood is concave so that the objective function has no local
optima. The gradient is given by

∂
∂λi

L(λ) =

(cid:88)

(cid:16)

n,t

fi(yn

t , yn

t−1, xn)

fi(yt, yt−1, xn)

− (cid:104)

(cid:105)p(yt,yt−1|xn,λ)

(cid:17)

(23.4.14)

x, λ). Since equation (23.4.11) cor-
Learning therefore requires inference of the marginal terms p(yt, yt−1
|
responds to a linear chain factor graph, see ﬁg(23.13), inference of pairwise marginals is straightforward
using message passing. This can be achieved using either the standard factor graph message passing or by
deriving an explicit algorithm, see exercise(23.10). Given the gradient, one may use any standard numeri-
cal optimisation routine to learn the parameters λ. In some applications, particularly in natural language
processing, the dimension K of the vector of features f1, . . . , fK may be many hundreds of thousands. This
means that the storage of the Hessian is not feasible for Newton based training and either limited memory
methods or conjugate gradient techniques are typically preferred[308].

After training we can use the model to ﬁnd the most likely output sequence for a novel input x∗. This is
straightforward since

y∗
1:T = argmax

y1:T

(cid:89)

t

φt(yt, yt−1, x∗, λ)

(23.4.15)

corresponds again to a simple linear chain, for which max-product inference yields the required result, see
also exercise(23.9). See example(9.11) for a small example.

DRAFT April 24, 2015

479

Applications

Figure 23.14: A Dynamic Bayesian Network. Possible
transitions between variables at the same time-slice
have not been shown.

Figure 23.15: A Coupled HMM. For example the up-
per HMM might model speech, and the lower the cor-
responding video sequence. The upper hidden units
then correspond to phonemes, and the lower to mouth
positions; this model therefore captures the expected
coupling between mouth positions and phonemes.

x3(t)

x2(t)

x1(t)

x3(t + 1)

x2(t + 1)

x1(t + 1)

v1(t)

v1(t + 1)

h1(t)

h1(t + 1)

h2(t)

h2(t + 1)

v2(t)

v2(t + 1)

23.4.4 Dynamic Bayesian networks

A DBN is deﬁned as a belief network replicated through time. For a multivariate xt, with dim (xt) = D,
the DBN deﬁnes a joint model

p(x1, . . . , xT ) =

T
(cid:89)

D
(cid:89)

t=1

i=1

p(xi(t)

x\i(t), x(t
|

−

1))

(23.4.16)

where x\i(t) denotes the set of variables at time t, except for xi(t). The form of each p(xi(t)
1))
is chosen such that the overall distribution remains acyclic. At each time-step t there is a set of variables
xi(t), i = 1, . . . , D, some of which may be observed. In a ﬁrst order DBN, each variable xi(t) has parental
variables taken from the set of variables in the previous time-slice, xt−1, or from the present time-slice. In
most applications, the model is temporally homogeneous so that one may fully describe the distribution in
terms of a two-time-slice model, ﬁg(23.14). The generalisation to higher-order models is straightforward. A
coupled HMM is a special DBN that may be used to model coupled ‘streams’ of information, for example
video and audio, see ﬁg(23.15)[224].

x\i(t), x(t
|

−

23.5 Applications

23.5.1 Object tracking

HMMs are used to track moving objects, based on an understanding of the dynamics of the object (encoded
in the transition distribution) and an understanding of how an object with a known position would be
observed (encoded in the emission distribution). Given an observed sequence, the hidden position can then
be inferred. The burglar, example(3.1) is a case in point. HMMs have been applied in many tracking
contexts, including tracking people in videos, musical pitch, and many more[57, 246, 54].

23.5.2 Automatic speech recognition

Many speech recognition systems make use of HMMs[321]. Roughly speaking, the raw scalar speech signal
x1:T is ﬁrst translated into a stream of continuous acoustic vectors v1:T where at time t, vt represents which
frequencies are present in the speech signal in a small window around time t. These acoustic vectors are
typically formed from taking a discrete Fourier transform of the speech signal over a small window around
time t, with additional transformations to mimic human auditory processing. Alternatively, related forms
of linear coding of the observed acoustic waveform may be used[143].

The corresponding discrete latent state ht represents a phoneme – a basic unit of human speech (for which
there are 44 in standard English). Training data is painstakingly constructed by a human linguist who

480

DRAFT April 24, 2015

Applications

determines the phoneme ht for each time t and many diﬀerent observed sequences x1:T . Given then each
acoustic vector vt and an associated phoneme ht, one may use maximum likelihood to ﬁt a mixture of
(usually isotropic) Gaussians p(vt

ht) to vt. This forms the emission distribution for a HMM.
|

Using the database of labelled phonemes, the phoneme transition p(ht
ht−1) can be learned (by simple count-
ing) and forms the transition distribution for a HMM. Note that in this case, since the ‘hidden’ variable
h and observation v are known during training, training the HMM is straightforward and boils down to
training the emission and transition distributions independently using the observed acoustic vectors and
associated phonemes.

|

For a new sequence of acoustic vectors v1:T we can then use the HMM to infer the most likely phoneme
v1:T ), which takes into account both the way that phonemes
sequence through time, arg maxh1:T p(h1:T
generate acoustic vectors, and also the prior language constraints of phoneme to phoneme transitions. The
fact that people speak at diﬀerent speeds can be addressed using time-warping in which the latent phoneme
remains in the same state for a number of timesteps. If the HMM is used to model a single word, it is natural
to constrain the hidden state sequence to only go ‘forwards’ through the set of available phonemes (i.e. one
cannot revisit a state, except possibly the current state). In this case the structure of the transition matrices
corresponds to a DAG state-transition diagram; under suitable labelling of the states, this can always be
represented by a triangular left-to-right transition matrix .

|

23.5.3 Bioinformatics

In the ﬁeld of Bioinformatics HMMs have been widely applied to modelling genetic sequences. Multiple
sequence alignment using forms of constrained HMMs have been particularly successful. Other applications
involve gene ﬁnding and protein family modelling[177, 90].

23.5.4 Part-of-speech tagging

Consider the sentence below in which each word has been linguistically tagged

hospitality_NN is_BEZ an_AT excellent_JJ virtue_NN ,_,
but_CC not_XNOT when_WRB the_ATI guests_NNS have_HV
to_TO sleep_VB in_IN rows_NNS in_IN the_ATI cellar_NN !_!

The linguistic tags are denoted at the end of each word, for example NN is the singular common noun
tag, ATI is the article tag etc. Given a training set of such tagged sentences, the task is to tag a novel
sentence. One approach is to use ht to be a tag, and vt to be a word and ﬁt a HMM to this data. For the
training data, both the tags and words are observed so that maximum likelihood training of the transition
and emission distributions can be achieved by simple counting. Given a new sequence of words, the most
likely tag sequence can be inferred using the Viterbi algorithm.

More recent part-of-speech taggers tend to use conditional random ﬁelds in which the input sequence x1:T
is the sentence and the output sequence y1:T is the tag sequence. One possible parameterisation of a linear
chain CRF is to use a potential of the form φ(yt−1, yt)φ(yt, x), see section(9.6.5), in which the ﬁrst factor
encodes the grammatical structure of the language and the second the a priori likely tag yt[180].

23.6 Summary

Timeseries require simplifying assumptions such as the Markov assumption which makes it feasible to specify
a model for the process.

A discrete-state Markov chain is a generalisation of deterministic ﬁnite-state transitions to stochastic tran-
sitions between states.

Mixtures of Markov models and extensions thereof can be used as simple timeseries clustering models.

•

•

•

DRAFT April 24, 2015

481

Exercises

•

•

•

•

Hidden Markov models are popular timeseries models in which the latent process is a discrete-state Markov
chain. The observations can be either discrete or continuous and the classical inference tasks of ﬁltering,
smoothing and computing the joint most likely latent sequence are computationally straightforward.

Dynamic Bayes nets are essentially structured HMMs with conditional independencies encoded in their
transition and emission distributions.

HMMs have widespread application in a variety of tracking scenarios, from speech recognition to genetic
sequence analysis.

Undirected linear chain structures such as the conditional random ﬁeld are popular in areas such as natural
language processing for translating an input sequence to a structured output sequence.

23.7 Code

demoMixMarkov.m: Demo for mixture of Markov models
mixMarkov.m: Mixture of Markov models
demoHMMinference.m: Demo of HMM inference
HMMforward.m: Forward α recursion
HMMbackward.m: Forward β recursion
HMMgamma.m: RTS γ ‘correction’ recursion
HMMsmooth.m: Single and pairwise α
β smoothing
HMMviterbi.m: Most likely state (Viterbi) algorithm
demoHMMburglar.m: Demo of burglar localisation
demoHMMbigram.m: Demo of stubby ﬁngers typing
HMMem.m: EM algorithm for HMM (Baum-Welch)
demoHMMlearn.m: Demo of EM algorithm for HMM (Baum-Welch)
demoLinearCRF.m: Demo of learning a linear chain CRF
linearCRFpotential.m: Linear CRF potential
linearCRFgrad.m: Linear CRF gradient
linearCRFloglik.m: Linear CRF log likelihood

−

23.8 Exercises

Exercise 23.1. A stochastic matrix Mij has non-negative entries with (cid:80)
λ and eigenvector e such (cid:80)
be equal to 1.

j Mijej = λei. By summing over i show that, provided (cid:80)

i Mij = 1. Consider an eigenvalue
i ei > 0, then λ must

Exercise 23.2. Consider the Markov chain with transition matrix

M =

(cid:19)

(cid:18) 0 1
1 0

(23.8.1)

Show that this Markov chain does not have an equilibrium distribution and state a stationary distribution
for this chain.

Exercise 23.3. Consider a HMM with 3 states (M = 3) and 2 output symbols, with a left-to-right state
transition matrix





A =

0.5 0.0 0.0
0.3 0.6 0.0
0.2 0.4 1.0





482

(23.8.2)

DRAFT April 24, 2015

Exercises

where Aij

B =

p(ht+1 = i
≡
(cid:18) 0.7 0.4 0.8
0.3 0.6 0.2

ht = j), emission matrix Bij
|

ht = j)
p(vt = i
|

≡

(cid:19)

(23.8.3)

and initial state probability vector a = (0.9 0.1 0.0)T. Given the observed symbol sequence is v1:3 = (1, 2, 1):

1. Compute p(v1:3).

2. Compute p(h1

v1:3).

|

3. Find the most probable hidden state sequence arg maxh1:3 p(h1:3

v1:3).
|

Exercise 23.4. This exercise follows from example(23.5). Given the 27 long character string
rgenmonleunosbpnntje vrancg typed with ‘stubby ﬁngers’, what is the most likely correct English sentence
v1:27) for this sequence? You will need
intended? In the list of decoded sequences, what value is log p(h1:27
|
to modify demoHMMbigram.m suitably.

Exercise 23.5. Show that if a HMM transition matrix A and emission matrix B are initialised to uniformly
constant values, then the EM algorithm fails to update the parameters meaningfully.

Exercise 23.6. Consider the problem of ﬁnding the most likely joint output sequence v1:T for a HMM. That
is,

v∗
1:T ≡

argmax
v1:T

p(v1:T )

where

p(h1:T , v1:T ) =

T
(cid:89)

t=1

ht)p(ht

p(vt

|

ht−1)

|

(23.8.4)

(23.8.5)

1. Explain why a local message passing algorithm cannot, in general, be found for this problem and discuss

the computational complexity of ﬁnding an exact solution.

2. Explain how to adapt the expectation maximisation algorithm to form a recursive algorithm for ﬁnding
1:T . Explain why your approach guarantees an improved solution at each iteration.

an approximate v∗
Additionally, explain how the algorithm can be implemented using local message passing.

Exercise 23.7. Explain how to train a HMM using EM (expectation maximisation), but with a constrained
transition matrix. In particular, explain how to learn a transition matrix with a triangular structure.

Exercise 23.8.

Using the correspondence A = 1, C = 2, G = 3, T = 4 deﬁne a 4
sequences of the form

×

4 transition matrix p that produces

A, C, G, T, A, C, G, T, A, C, G, T, A, C, G, T, . . .

Now deﬁne a new transition matrix

pnew = 0.9*p + 0.1*ones(4)/4

Deﬁne a 4

×

4 transition matrix q that produces sequences of the form

T, G, C, A, T, G, C, A, T, G, C, A, T, G, C, A, . . .

Now deﬁne a new transition matrix

qnew = 0.9*q + 0.1*ones(4)/4

(23.8.6)

(23.8.7)

(23.8.8)

(23.8.9)

Assume that the probability of being in the initial state of the Markov chain p(h1) is constant for all four
states A, C, G, T .

DRAFT April 24, 2015

483

1. What is the probability that the Markov chain pnew generated the sequence S given by

A, A, G, T, A, C, T, T, A, C, C, T, A, C, G, C

S

≡

Exercises

(23.8.10)

2. Similarly what is the probability that S was generated by qnew? Does it make sense that S has a higher

likelihood under pnew compared with qnew?

3. Using the function randgen.m, generate 100 sequences of length 16 from the Markov chain deﬁned by
pnew. Similarly, generate 100 sequences each of length 16 from the Markov chain deﬁned by qnew.
Concatenate all these sequences into a cell array v so that v
200
1
}
{
{
the last sequence. Use MixMarkov.m to learn the Maximum Likelihood parameters that generated these
sequences. Assume that there are H = 2 kinds of Markov chain. The result returned in phgv indicates
the posterior probability of sequence assignment. Do you agree with the solution found?

contains the ﬁrst sequence and v

}

4. Take the sequence S as deﬁned in equation (23.8.10). Deﬁne an emission distribution that has 4 output

states such that

p(v = i

h = j) =
|

(cid:26) 0.7 i = j
= j
0.1 i

(23.8.11)

Using this emission distribution and the transition given by pnew deﬁned in equation (23.8.7), adapt
demoHMMinferenceSimple.m suitably to ﬁnd the most likely hidden sequence hp
1:16 that generated the
observed sequence S. Repeat this computation but for the transition qnew to give hq
1:16. Which hidden
sequence – hp
1:16 is to be preferred? Justify your answer.

1:16 or hq

Exercise 23.9. Derive an algorithm that will ﬁnd the most likely joint state

argmax
h1:T

T
(cid:89)

t=2

φt(ht−1, ht)

for arbitrarily deﬁned potentials φt(ht−1, ht).

1. First consider

max
h1:T

T
(cid:89)

t=2

φt(ht−1, ht)

(23.8.12)

(23.8.13)

Show how the maximisation over hT may be pushed inside the product and that the result of the
maximisation can be interpreted as a message

γT −1←T (hT −1)

2. Derive the recursion

γt−1←t(ht−1) = max

ht

φt(ht, ht−1)γt←t+1(ht)

3. Explain how the above recursion enables the computation of

argmax
h1

T
(cid:89)

t=2

φt(ht, ht−1)

(23.8.14)

(23.8.15)

(23.8.16)

4. Explain how once the most likely state for h1 is computed, one may eﬃciently compute the remaining

optimal states h2, . . . , hT .

Exercise 23.10. Derive an algorithm that will compute pairwise marginals

p(ht, ht−1)

from the joint distribution

p(h1:T )

T
(cid:89)

t=2

∝

φt(ht−1, ht)

for arbitrarily deﬁned potentials φt(ht−1, ht).

(23.8.17)

(23.8.18)

484

DRAFT April 24, 2015

(cid:54)
Exercises

1. First consider

(cid:88)

T
(cid:89)

h1,...,hT

t=2

φt(ht, ht−1)

(23.8.19)

@@

Show how the summation over h1 may be pushed inside the product and that the result of the
can be interpreted as a message

summation

α1→2(h2) =

φ2(h1, h2)

(cid:88)

h1

(23.8.20)

2. Show that the summation over the variables h1:t−1 can be accomplished via the recursion

αt−1→t(ht) =

(cid:88)

ht−1

φt(ht−1, ht)αt−2→t−1(ht−1)

(23.8.21)

3. Similarly, show that one can push the summation of hT inside the product to deﬁne

βT −1←T (hT −1) =

φT (hT −1, hT ).

(cid:88)

hT

Show that the summation over the variables hT :t+1 can be accomplished via the recursion

βt←t+1(ht) =

(cid:88)

ht+1

φt+1(ht, ht+1)βt+1←t+2(ht+1)

4. Show that

@@

p(ht, ht−1)

∝

αt−2→t−1(ht−1)φ(ht−1, ht)βt←t+1(ht)

Exercise 23.11. A second order HMM is deﬁned as

p(h1:T , v1:T ) = p(h1)p(v1

h1)p(h2
|

h1)p(v2
|

h2)
|

T
(cid:89)

t=3

p(ht

ht−1, ht−2)p(vt
|

|

ht)

(23.8.22)

(23.8.23)

(23.8.24)

(23.8.25)

Following a similar approach to the ﬁrst order HMM, derive explicitly a message passing algorithm to
compute the most likely joint state

argmax
h1:T

p(h1:T

v1:T )

|

(23.8.26)

Exercise 23.12. Derive an algorithm to eﬃciently compute the gradient of the log likelihood for an HMM;
assume unconstrained discrete transition and emission matrices.

Exercise 23.13. Consider the HMM deﬁned on hidden variables
v1, . . . , vT

{

}

p(

,

V

H

) = p(h1)p(v1

T
(cid:89)

t=2

h1)
|

p(ht

|

ht−1)p(vt

ht)

|

Show that the posterior p(

H|V

) is a Markov chain

p(

H|V

) = ˜p(h1)

T
(cid:89)

t=2

˜p(ht

|

ht−1)

where ˜p(ht

|

ht−1) and ˜p(h1) are suitably deﬁned distributions.

DRAFT April 24, 2015

=

{

H

h1, . . . , hT

}

and observations

=

V

(23.8.27)

(23.8.28)

485

Exercise 23.14. For training a HMM with a Gaussian mixture emission (the HMM-GMM model) in sec-
tion(23.3.3), derive the following EM update formulae for the means and covariances:

Exercises

µnew

k,h =

N
(cid:88)

T
(cid:88)

n=1

t=1

ρk,h(t, n)vn
t

and

where

Σnew

k,h =

N
(cid:88)

T
(cid:88)

n=1

t=1

ρk,h(t, n) (cid:0)vn

t −

µk,h

(cid:1) (cid:0)vn

t −

(cid:1)T

µk,h

ρk,h(t, n) =

t = h)q(hn
hn
vn
q(kt = k
t = h
1:T )
|
|
(cid:80)
hn
t = h)q(hn
vn
t q(kt = k
t = h
1:T )
|
|

(cid:80)
n

(23.8.29)

(23.8.30)

(23.8.31)

Exercise 23.15. Consider the HMM duration model deﬁned by equation (23.4.2) and equation (23.4.1)
with emission distribution p(vt

ht). Our interest is to derive a recursion for the ﬁltered distribution
|

αt(ht, ct)

≡

p(ht, ct, v1:t)

1. Show that :

αt(ht, ct) = p(vt

ht)
|

(cid:88)

ht−1,ct−1

p(ht

ht−1, ct)p(ct
|

ct−1)αt−1(ht−1, ct−1)
|

2. Using this derive

αt(ht, ct)
ht)
p(vt
|

=

(cid:88)

ht−1

p(ht

ht−1, c)p(ct
|

|

ct−1 = 1)αt−1(ht−1, ct−1 = 1)

(23.8.32)

(23.8.33)

+

(cid:88)

ht−1

p(ht

|

ht−1, c)

Dmax(cid:88)

ct−1=2

ct−1)αt−1(ht−1, ct−1)
p(c
|

(23.8.34)

3. Show that the right hand side of the above can be written as

(cid:88)

ht−1

p(ht

ht−1, ct = c)p(ct = c
|
|

ct−1 = 1)αt−1(ht−1, 1)

+ I [c

= Dmax]

(cid:88)

ht−1

p(ht

ht−1, c)αt−1(ht−1, c + 1)
|

(23.8.35)

4. Show that the recursion for α is then given by

αt(h, 1) = p(vt

and for c > 1

ht = h)pdur(1)
|

ptran(h
|

ht−1)αt−1(ht−1, 1)

(cid:88)

ht−1
+ I [Dmax

= 1] p(vt

ht = h)
|

(cid:88)

ht−1

ptran(ht

ht−1)αt−1(ht−1, 2)
|

(23.8.36)

αt(h, c) = p(vt

ht = h)

|

pdur(c)αt−1(h, 1) + I [c
{

= Dmax] αt−1(h, c + 1)

}

(23.8.37)

5. Explain why the computational complexity of ﬁltered inference in the duration model is O (cid:0)T H 2Dmax

(cid:1).

6. Derive an eﬃcient smoothing algorithm for this duration model.

486

DRAFT April 24, 2015

(cid:54)
(cid:54)
(cid:54)
Exercises

++

Consider searching for a pattern s1, s2, . . . , sL in a longer text

Exercise 23.16 (Fuzzy String Search).
v1, v2, . . . , vT . For example one might seek to ﬁnd whether the pattern ACGT AA occurs in the text AAACGT AAT AT .
We can model this naively using a HMM with L + 1 states. The idea is that we ﬁrst deﬁne a generating
mechanism which places the pattern s in the text (possibly multiple times). We can do this by deﬁning a
latent state that either represents not emitting the pattern, or the position in the pattern. For notation here
we deﬁne the state ht = 0 to mean that t is not the starting position of the pattern. We then deﬁne

p(ht+1 = 0
|

ht = 0) = τ,

p(ht+1 = L
|

ht = 0) = 1

τ

−

to denote that the latent variable h either remains at the non-starting point with probability τ or jumps to
τ . The remaining latent state transition is deﬁned as
the starting point of the pattern s with probability 1

−

p(ht+1

|

ht) = δ (ht+1, ht

1)

−

which decrements the latent variable deterministically. This is used as a counter for the position in the
pattern. Given a position in the pattern, the emission is given by p(vt

ht). In the deterministic case,
|

ht) = δ (vt

p(vt

|

−

sht+1−L)

and, more generally, one can deﬁne a distribution p(vt

ht) to account for possible corruption.
|
1. Show that inference (ﬁltering, smoothing and Viterbi) can be computed with O (T L) operations in both
the deterministic (perfect matching) and ‘fuzzy’ matching case. (Note that for the perfect matching
case, the reader is referred to classical approaches such as the Aho-Corasick and Boyer-Moore algo-
rithms which are generally much faster).

2. For the text 12132324233144241314223142331234, with st

, ﬁnd the smoothed probability
pt that the pattern 2314243 starts at position t in the sequence based on assuming τ = 0.5 and p(vt =
ht = i) = 0.9, with uniform probability on the mismatch states. Assume also that when ht = 0 there
i
|
is a uniform emission distribution.

1, 2, 3, 4
}

∈ {

++

Exercise 23.17.

As noted in section(23.2.2), a numerical issue with the standard α-recursion

α(ht) = p(vt

ht)
|

(cid:88)

ht−1

p(ht

ht−1)α(ht−1)
|

(23.8.38)

is that these α messages will typically become exponentially small in time, resulting in numerical underﬂow.
Whilst this can be addressed by computing log α messages, this would require special treatment of cases
in which the α messages have zero values. An alternative to using log α messages is to renormalise the
messages at each stage, resulting in messages that represent the ﬁltered distribution p(ht
v1:t). A drawback
of normalising the messages is that we are then unable to directly compute the likelihood term p(v1:T ) =
(cid:80)
ht α(hT ). However, there is a way to deﬁne normalised messages and still retain the information required

|

to compute the log-likelihood. Deﬁne the local normalisation term

zt

≡

(cid:88)

ht

p(vt

ht)p(ht
|

ht−1)˜α(ht−1)
|

and normalised message

˜α(ht) =

1
zt

p(vt

|

ht)p(ht

|

ht−1)˜α(ht−1)

where z1

≡

(cid:80)

h1 p(v1

h1)p(h1) and ˜α(h1) = p(v1
|
t
(cid:89)

α(ht) = ˜α(ht)

zτ

τ =1

and that therefore log p(v1:T ) = (cid:80)T

t=1 log(zt).

DRAFT April 24, 2015

h1)h(h1)/z1. Show that

|

(23.8.39)

(23.8.40)

(23.8.41)

487

Exercises

488

DRAFT April 24, 2015

CHAPTER 24

Continuous-state Markov Models

Many physical systems can be understood as continuous variable models undergoing a transition that de-
pends only on the state of the system at the previous time. In this chapter we discuss some of the classical
models in this area which are highly specialised to retain tractability of inference. These models are found
in a wide variety of ﬁelds from ﬁnance to signal processing in engineering.

24.1 Observed Linear Dynamical Systems

In many practical timeseries applications the data is naturally continuous, particularly for models of the
physical environment. In contrast to discrete-state Markov models, chapter(23), parametric continuous state
distributions are not automatically closed under operations such as products and marginalisation. To make
practical algorithms for which inference and learning can be carried out eﬃciently, we therefore are heavily
restricted in the form of the continuous transition p(vt
vt−1). A simple yet powerful class of such transitions
|
are the linear dynamical systems. A deterministic observed linear dynamical system1 (OLDS) deﬁnes the
temporal evolution of a vector vt according to the discrete-time update equation

vt = Atvt−1

(24.1.1)

where At is the transition matrix at time t. For the case that At is invariant with t, the process is called
stationary or time-invariant, which we assume throughout unless explicitly stated otherwise.

A motivation for studying OLDSs is that many equations that describe the physical world can be written
if vt describes
as an OLDS. OLDSs are interesting since they may be used as simple prediction models:
the state of the environment at time t, then Avt predicts the environment at time t + 1. As such, these
models, have widespread application in many branches of science, from engineering and physics to economics.

The OLDS equation (24.1.1) is deterministic so that if we specify v1, all future values v2, v3, . . . , are deﬁned.
For a dim (v) = V dimensional vector, its evolution is described by (assuming A is diagonalisable)

vt = At−1v1 = PΛt−1P−1v1

(24.1.2)

where Λ = diag (λ1, . . . , λV ), is the diagonal eigenvalue matrix, and P is the corresponding eigenvector
matrix of A. If any λi > 1 then for large t, vt will explode. On the other hand, if λi < 1, then λt−1
i will
tend to zero. For stable systems we require therefore no eigenvalues of magnitude greater than 1 and only
will contribute in the long term. Note that the eigenvalues may be complex which
unit eigenvalues

λi = 1
|
|

1We use the terminology ‘observed’ LDS to diﬀerentiate from the more general LDS state-space model.

In some texts,

however, the term LDS is applied to the models under discussion in this chapter.

489

corresponds to rotational behaviour, see exercise(24.1).

More generally, we may consider additive noise on v and deﬁne a stochastic OLDS, as deﬁned below.

Observed Linear Dynamical Systems

Deﬁnition 24.1 (Observed Linear Dynamical System).

vt = Atvt−1 + ηt

where ηt is a noise vector sampled from a Gaussian distribution,

(ηt µt, Σt)

N

This is equivalent to a ﬁrst order Markov model with transition

p(vt

vt−1) =
|

N

(vt Atvt−1 + µt, Σt)

(24.1.3)

(24.1.4)

(24.1.5)

At t = 1 we have an initial distribution p(v1) =
independent, µt ≡

A, Σt

µ, At

≡

≡

N

Σ, the process is called time-invariant.

(v1 µ1, Σ1). For t > 1 if the parameters are time-

24.1.1 Stationary distribution with noise

Consider the one-dimensional linear system with independent additive noise

vt = avt−1 + ηt,

ηt

∼ N

(cid:0)ηt 0, σ2

v

(cid:1)

(24.1.6)

If we start at some state v1, and then for t > 1 recursively sample according to vt = avt−1 + ηt, what is the
distribution of vt? Since the transitions are linear and the noise is Gaussian, then vt must also be Gaussian.
Assuming that we can represent the distribution of vt−1 as a Gaussian with mean µt−1 and variance σ2
t−1,
vt−1

(cid:0)vt−1 µt−1, σ2

(cid:1), then using

= 0 we have

t−1

ηt
(cid:104)

(cid:105)

∼ N

ηt

= a
(cid:68)

vt
(cid:104)
(cid:105)
(cid:11) =
(cid:10)v2
t
t = a2σ2
σ2

vt−1
+
(cid:104)
(cid:105)
(cid:105) ⇒
(avt−1 + ηt)2(cid:69)
t−1 + σ2
v

(cid:104)

⇒

µt = aµt−1
= a2 (cid:10)v2

t−1

(cid:11) + 2a

vt−1

ηt

(cid:105) (cid:104)

(cid:105)

(cid:104)

+ (cid:10)η2
t

(cid:11)

so that

vt

∼ N

(cid:0)vt aµt−1, a2σ2

t−1 + σ2
v

(cid:1)

Inﬁnite time limit

Does the distribution of the vt, t
indeﬁnitely with t, as does the mean value.

(cid:29)

1 tend to a steady, ﬁxed distribution? If a

(24.1.7)

(24.1.8)

(24.1.9)

(24.1.10)

1 the variance increases

≥

For a < 1, and assuming there is a ﬁnite variance σ2
stationary distribution satisﬁes

∞ for the inﬁnite time case, from equation (24.1.9), the

∞ = a2σ2
σ2

∞ + σ2

v ⇒

σ2
∞ =

1

σ2
v
a2

−

(24.1.11)

Similarly, the mean is given by µ∞ = a∞µ1. Hence, for a < 1, the mean tends to zero yet the variance
remains ﬁnite. Even though the magnitude of vt−1 is decreased by a factor of a at each iteration, the
additive noise on average boosts the magnitude so that it remains steady in the long run. More generally
for a system updating a vector vt according to non-zero additive noise,

vt = Avt−1 + ηt

(24.1.12)

for the existence of a steady state we require that all eigenvalues of A must be < 1.

490

DRAFT April 24, 2015

Auto-Regressive Models

Figure 24.1: Fitting an order 3 AR model to the training points. The x
axis represents time, and the y axis the value of the timeseries. The dots
represent the 100 observations y1:100. The solid line indicates the mean
σ. See demoARtrain.m
predictions

(cid:105)t , t > 100, and the dashed lines

(cid:105)t ±

y
(cid:104)

y

(cid:104)

24.2 Auto-Regressive Models

A scalar time-invariant auto-regressive model is deﬁned by

vt =

L
(cid:88)

l=1

alvt−l + ηt,

(cid:0)ηt µ, σ2(cid:1)

ηt

∼ N

(24.2.1)

where a = (a1, . . . , aL)T are called the AR coeﬃcients and σ2 is called the innovation noise. The model
predicts the future based on a linear combination of the previous L observations. As a belief network, the
AR model can be written as an Lth order Markov model:

p(v1:T ) =

T
(cid:89)

t=1

p(vt

|

vt−1, . . . , vt−L),

with vi =

for i

0

≤

∅

with

p(vt

vt−1, . . . , vt−L) =
|

N

(cid:32)

vt

L
(cid:88)

l=1

(cid:33)

alvt−l, σ2

Introducing the vector of the L previous observations

ˆvt−1

≡

[vt−1, vt−2, . . . , vt−L]T

we can write more compactly

vt−1, . . . , vt−L) =

p(vt

|

N

(cid:16)

vt aT ˆvt−1, σ2(cid:17)

(24.2.2)

(24.2.3)

(24.2.4)

(24.2.5)

AR models are heavily used in ﬁnancial time-series prediction (see for example [288]), being able to capture
simple trends in the data. Another common application area is in speech processing whereby for a one-
dimensional speech signal partitioned into windows of length T
L, the AR coeﬃcients best able to describe
the signal in each window are found[231]. These AR coeﬃcients then form a compressed representation of
the signal and subsequently transmitted for each window, rather than the original signal itself. The signal
can then be approximately reconstructed based on the AR coeﬃcients. This is known as a linear predictive
vocoder[273]. Note that an interesting property of the AR representation of a signal is that if we scale the
signal by a constant amount ρ, then

(cid:29)

ρvt =

L
(cid:88)

l=1

alρvt−l + ρηt,

(24.2.6)

so that provided we scale the noise suitably, the same AR coeﬃcients can be used to represent the signal;
the AR coeﬃcients are therefore to a degree amplitude invariant.

DRAFT April 24, 2015

491

020406080100120140−5005010015020024.2.1 Training an AR model

Maximum likelihood training of the AR coeﬃcients is straightforward based on

log p(v1:T ) =

T
(cid:88)

t=1

log p(vt

ˆvt−1) =
|

−

1
2σ2

T
(cid:88)

(cid:16)

t=1

vt

−

(cid:17)2

ˆvT
t−1a

T
2

−

log(2πσ2)

Diﬀerentiating w.r.t. a and equating to zero we arrive at

(cid:88)

(cid:16)

t

vt

−

(cid:17)
ˆvT
t−1a

ˆvt−1 = 0

so that optimally

a =

(cid:32)

(cid:88)

t

(cid:33)−1

ˆvt−1 ˆvT

t−1

(cid:88)

t

vt ˆvt−1

Auto-Regressive Models

(24.2.7)

(24.2.8)

(24.2.9)

These equations can be solved by Gaussian elimination. The linear system has a Toeplitz form that can be
more eﬃciently solved using the Levinson-Durbin method [89]. Similarly, optimally,

σ2 =

1
T

T
(cid:88)

(cid:16)

t=1

vt

−

(cid:17)2

ˆvT
t−1a

(24.2.10)

Above we assume that ‘negative’ timesteps are available in order to keep the notation simple. If times before
the window over which we learn the coeﬃcients are not available, a minor adjustment is required to start
the summations from t = L + 1. Given a trained a, future predictions can be made using vt+1 = ˆvT

t a.

Example 24.1 (Fitting a trend). We illustrate with a simple example how AR models can be used to
estimate trends underlying timeseries data. A third-order AR model was ﬁt to the set of 100 observations
(cid:105)t was then recursively generated
shown in ﬁg(24.1) using maximum likelihood. A prediction for the mean
as

y
(cid:104)

(cid:26) (cid:80)3

i=1 ai

yt

y

(cid:104)

(cid:105)t−i

for
for

y
(cid:104)

(cid:105)t =

t > 100,
100 .
t

≤

As we can see (solid line in ﬁg(24.1)), the predicted means for time t > 100 capture an underlying trend in
the data. Whilst this example is very simple, AR models are quite powerful and can model complex signal
behaviour.

24.2.2 AR model as an OLDS

We can write equation (24.2.1) as an OLDS using








vt
vt−1
...
vt−L+1








=








a1
1
...
0

a2
0

1
. . .

. . . aL
0
. . .

. . .
1

0
0















vt−1
vt−2
...
vt−L















+








ηt
0
...
0

which in vector notation is

ˆvt = Aˆvt−1 + ηt,

ηt ∼ N
where we deﬁne the block matrices
(cid:18) a1:L−1 aL
0
I

A =

(cid:19)

,

(ηt 0, Σ)

Σ =

(cid:18)

σ2

01,1:L−1

(cid:19)

01:L−1,1 01:L−1,1:L−1

(24.2.11)

(24.2.12)

(24.2.13)

In this representation, the ﬁrst component of the vector is updated according to the standard AR model,
with the remaining components being copies of the previous values.

492

DRAFT April 24, 2015

Auto-Regressive Models

a1

v1

a2

v2

a3

v3

a4

v4

Figure 24.2: A time-varying AR model as a latent
LDS. Since the observations are known, this model is a
time-varying latent LDS, for which smoothed inference
determines the time-varying AR coeﬃcients.

24.2.3 Time-varying AR model

An alternative to maximum likelihood is to view learning the AR coeﬃcients as a problem in inference in
a latent LDS, a model which is discussed in detail in section(24.3). If at are the latent AR coeﬃcients, the
term

vt = ˆvT

t−1at + ηt,

(cid:0)ηt 0, σ2(cid:1)

ηt

∼ N

(24.2.14)

can be viewed as the emission distribution of a latent LDS in which the hidden variable is at and the
time-dependent emission matrix is given by ˆvT

t−1. By placing a simple latent transition

at = at−1 + ηa
t ,

ηa
t ∼ N

(cid:0)ηa

t 0, σ2

aI(cid:1)

we encourage the AR coeﬃcients to change slowly with time. This deﬁnes a model, see ﬁg(24.2)

p(v1:T , a1:T ) =

(cid:89)

t

p(vt

at, ˆvt−1)p(at
|

at−1)
|

(24.2.15)

(24.2.16)

Our interest is then in the conditional p(a1:T
v1:T ) from which we can compute the a-posteriori most likely
|
sequence of AR coeﬃcients. Standard smoothing algorithms can then be applied to yield the time-varying
AR coeﬃcients, see demoARlds.m.

Deﬁnition 24.2 (Discrete Fourier Transform). For a sequence x0:N −1 the DFT f0:N −1 is deﬁned as

fk =

N −1
(cid:88)

n=0

xne− 2πi

N kn,

k = 0, . . . , N

1

−

(24.2.17)

fk is a (complex) representation as to how much frequency k is present in the sequence x0:N −1. The power
of component k is deﬁned as the absolute length of the complex fk.

Deﬁnition 24.3 (Spectrogram). Given a timeseries x1:T the spectrogram at time t is a representation of
the frequencies present in a window localised around t. For each window one computes the Discrete Fourier
Transform, from which we obtain a vector of log power in each frequency. The window is then moved
(usually) one step forward and the DFT recomputed. Note that by taking the logarithm, small values in
the original signal can translate to visibly appreciable values in the spectrogram.

Example 24.2 (Nightingale). In ﬁg(24.3a) we plot the raw acoustic recording for a 5 second fragment of a
nightingale song (freesound.org, sample 17185). The spectrogram is also plotted and gives an indication
of which frequencies are present in the signal as a function of time. The nightingale song is very complicated
but at least locally can be very repetitive. A crude way to ﬁnd which segments repeat is to cluster the
time-slices of the spectrogram.
In ﬁg(24.3c) we show the results of ﬁtting a Gaussian mixture model,
section(20.3), with 8 components, from which we see there is some repetition of components locally in time.
An alternative representation of the signal is given by the time-varying AR coeﬃcients, section(24.2.3), as
plotted in ﬁg(24.3d). A GMM clustering with 8 components ﬁg(24.3e) in this case produces a somewhat
clearer depiction of the diﬀerent phases of the nightingale singing than that aﬀorded by the spectrogram.

DRAFT April 24, 2015

493

Auto-Regressive Models

(a)

(b)

(c)

(d)

(e)

(b): Spectrogram of (a) up to 20,000 Hz.

(a): The raw recording of 5 seconds of a nightingale song (with additional background
Figure 24.3:
(c): Clustering of the results in panel (b) using an
birdsong).
8 component Gaussian mixture model. Plotted at each time vertically is the distribution over the cluster
indices (from 1 to 8), so that the darker the component, the more responsible that component is for the
(d): The 20 time-varying AR coeﬃcients learned using σ2
spectrogram slice at that timepoint.
v = 0.001,
σ2
(e): Clustering the results in panel (d) using a Gaussian mixture model with 8
h = 0.001, see ARlds.m.
components. The AR components group roughly according to the diﬀerent song regimes.

24.2.4 Time-varying variance AR models

In the standard AR model, equation (24.2.1), the variance σ2 is assumed ﬁxed throughout time. For some
applications, particular in ﬁnance, this is undesirable since the ‘volatility’ can change dramatically with
time. A simple extension of the AR model is to write

vt =

¯vt =

L
(cid:88)

l=1
L
(cid:88)

l=1

alvt−l + ηt,

ηt

∼ N

(cid:0)ηt µ, σ2

t

(cid:1)

alvt−l

σ2
t = α0 +

Q
(cid:88)

i=1

αi (vt−i

¯vt−i)2

−

(24.2.18)

(24.2.19)

(24.2.20)

≥

where αi
0. The motivation is that equation (24.2.20) represents an estimate of the variance of the
noise, based on a weighted sum of the squared discrepancies between the mean prediction ¯v and the actual
observation v over the previous Q timesteps. This is called an AutoRegressive Conditional Heteroskedasticity
(ARCH) model [96]. A further extension is to the generalised ARCH (GARCH) model, for which

σ2
t = α0 +

Q
(cid:88)

i=1

αi (vt−i

−

¯vt−i)2 +

P
(cid:88)

i=1

βiσ2

t−i

494

(24.2.21)

DRAFT April 24, 2015

Latent Linear Dynamical Systems

σ2
1

v1

σ2
2

v2

(a)

σ2
t

σ2
1

vt

v1

σ2
2

v2

(b)

σ2
t

vt

(a): A ﬁrst order L = 1, Q = 1 ARCH model, in which the observations are dependent
Figure 24.4:
on the previous observation, and the variance is dependent on the previous observations in a deterministic
(b): An L = 1, Q = 1, P = 2 GARCH model, in which the observations are dependent on
manner.
the previous observation, and the variance is dependent on the previous observations and previous two
variances in a deterministic manner. These are special cases of the general deterministic latent variable
models, section(26.4).

where βi

0.

≥

One can interpret these as deterministic latent variable models, see section(26.4), albeit with the extension
to a higher-order Markov process.
In this sense, learning of the parameters by maximum likelihood is
straightforward since derivatives of the log likelihood can be computed by deterministic propagation, as
described in section(26.4). Indeed, based on this insight, a whole range of possible non-linear ‘volatility’
models come into view.

24.3 Latent Linear Dynamical Systems

The Latent LDS deﬁnes a stochastic linear dynamical system in a latent (or ‘hidden’) space on a sequence
of vectors h1:T . Each observation vt is as linear function of the latent vector ht. This model is also called a
linear Gaussian state space model 2. The model can also be considered a form of LDS on the joint variables
xt = (vt, ht), with parts of the vector xt missing. For this reason we will also sometimes refer to this model
as a Linear Dynamical System (without the ‘latent’ preﬁx). These models are powerful models of timeseries
and their use is widespread. Their latent nature means that we can use the latent variable ht to track and
explain the observation t. The formal deﬁnition of the model is given below.

Deﬁnition 24.4 (Latent linear dynamical system).

ht = Atht−1 + ηh
vt = Btht + ηv
t

t ηh
t ∼ N
ηv
t ∼ N

(cid:0)ηh
(cid:1) transition model
¯ht, Σh
t
t
t ¯vt, Σv
(ηv
emission model
t )

(24.3.1)

t and ηv

where ηh
t are noise vectors. At is called the transition matrix and Bt the emission matrix . The
terms ¯ht and ¯vt are the hidden and output bias respectively. The transition and emission models deﬁne a
ﬁrst order Markov model

p(h1:T , v1:T ) = p(h1)p(v1

T
(cid:89)

t=2

h1)

|

p(ht

ht−1)p(vt
|

|

ht)

with the transitions and emissions given by Gaussian distributions

p(ht

ht−1) =
|
p(vt

ht) =
|

(cid:16)

ht Atht−1 + ¯ht, Σh
t
(vt Btht + ¯vt, Σv
t )

(cid:17)

,

N

N

p(h1) =

N

(h1 µπ, Σπ)

(24.3.2)

(24.3.3)

(24.3.4)

2These models are also often called Kalman Filters. We avoid this terminology here since the word ‘ﬁlter’ refers to a speciﬁc

kind of inference and runs the risk of confusing a ﬁltering algorithm with the model itself.

DRAFT April 24, 2015

495

h1

v1

h2

v2

h3

v3

h4

v4

Inference

Figure 24.5: A (latent) LDS. Both hidden and visible
variables are Gaussian distributed.

Figure 24.6: A single phasor plotted as a damped two
dimensional rotation ht+1 = γRθht with a damping
factor 0 < γ < 1. By taking a projection onto the y
axis, the phasor generates a damped sinusoid.

The model is represented as a belief network in ﬁg(24.5) with the extension to higher orders being intuitive.
One may also include an external input ot at each time, which will add Cot to the mean of the hidden
variable and Dot to the mean of the observation.

Explicit expressions for the transition and emission distributions are given below for the time-invariant
Σv with zero biases ¯vt = 0, ¯ht = 0. Each hidden variable is a
case At
multidimensional Gaussian distributed vector ht, with transition

Σh, Σv

B, Σh

A, Bt

t ≡

t ≡

≡

≡

p(ht

ht−1) =
|

1
(cid:112)
2πΣh
|

|

exp

(cid:18)

−

1
2

(ht

−

Aht−1)T Σ−1

h (ht

(cid:19)

Aht−1)

−

which states that ht has a mean Aht−1 and covariance Σh. Similarly,

p(vt

ht) =
|

1
(cid:112)
2πΣv
|

|

exp

(cid:18)

1
2

−

(vt

−

Bht)T Σ−1
v

(vt

(cid:19)

Bht)

−

describes an output vt with mean Bht and covariance Σv.

Example 24.3. Consider a dynamical system deﬁned on two dimensional vectors ht:

ht+1 = Rθht,

with Rθ =

(cid:18) cos θ
sin θ

sin θ
−
cos θ

(cid:19)

(24.3.5)

(24.3.6)

(24.3.7)

Rθ rotates the vector ht through angle θ in one timestep. Under this LDS we trace out points on a circle,
h1, . . . , ht through time. By taking a scalar projection of ht, for example,

vt = [ht]1 = [1 0]Tht,

(24.3.8)

the elements vt, t = 1, . . . , T describe a sinusoid through time, see ﬁg(24.6). By using a block diagonal
R = blkdiag (Rθ1, . . . , Rθm) and taking a scalar projection of the extended m
2 dimensional ht vector, one
can construct a representation of a signal in terms of m sinusoidal components. Hence we see that LDSs
can represent potentially highly complex period behaviour.

×

24.4 Inference

Given an observation sequence v1:T we here consider ﬁltering and smoothing, as we did for the HMM,
section(23.2.1). For the HMM, in deriving the various message passing recursions, we used only the inde-
pendence structure encoded by the belief network. Since the LDS has the same independence structure as
the HMM, we can use the same independence assumptions in deriving the updates for the LDS. However,
in implementing them we need to deal with the issue that we now have continuous hidden variables, rather
than discrete states. The fact that the distributions are Gaussian means that we can deal with continuous

496

DRAFT April 24, 2015

Inference

messages exactly.
integration. For example, the ﬁltering recursion (23.2.7) becomes

In translating the HMM message passing equations, we ﬁrst replace summation with

p(ht

v1:t)
|

∝

(cid:90)

ht−1

p(vt

ht)p(ht
|

|

ht−1)p(ht−1

v1:t−1),
|

t > 1

(24.4.1)

Since the product of two Gaussians is another Gaussian, and the integral of a Gaussian is another Gaussian,
v1:t) is also Gaussian. This closure property of Gaussians means that we may represent
the resulting p(ht
|
(ht−1 ft−1, Ft−1) with mean ft−1 and covariance Ft−1. The eﬀect of equation (24.4.1) is
p(ht−1
N
equivalent to updating the mean ft−1 and covariance Ft−1 into a mean ft and covariance Ft for p(ht
v1:t).
|
Our task below is to ﬁnd explicit algebraic formulae for these updates.

v1:t−1) =
|

Numerical stability

Translating the message passing inference techniques we developed for the HMM into the LDS is largely
straightforward.
Indeed, one could simply run a standard sum-product algorithm (albeit for continuous
variables), see demoSumprodGaussCanonLDS.m. In long timeseries, however, numerical instabilities can build
up and may result in grossly inaccurate results, depending on the transition and emission distribution
parameters and the method of implementing the message updates. For this reason specialised routines
have been developed that are numerically stable under certain parameter regimes[303]. For the HMM
in section(23.2.1), we discussed two alternative methods for smoothing, the parallel β approach, and the
sequential γ approach. The β recursion is suitable when the emission and transition covariance entries are
large, and the γ recursion preferable in the more standard case of small covariance values.

Analytical shortcuts

In deriving the inference recursions we need to frequently multiply and integrate Gaussians. Whilst in prin-
ciple straightforward, this can be algebraically tedious and, wherever possible, it is useful to appeal to known
shortcuts. For example, one can exploit the general result that the linear transform of a Gaussian random
variable is another Gaussian random variable. Similarly it is convenient to make use of the conditioning
formulae, as well as the dynamics reversal intuition. These results are stated in section(8.4), and below we
derive the most useful for our purposes here. This will also explain how the equations for ﬁltering can be
derived.

Consider a linear transformation of a Gaussian random variable:

y = Mx + η,

η

∼ N

(η µ, Σ) ,

x

∼ N

(x µx, Σx)

(24.4.2)

where x and η are assumed to be generated from independent processes. To ﬁnd the distribution p(y), one
approach would be to write this formally as

(cid:90)

p(y) =

(y Mx + µ, Σ)

(x µx, Σx) dx

N

N

(24.4.3)

and carry out the integral (by completing the square). However, since a Gaussian variable under linear
transformation is another Gaussian, we can take a shortcut and just ﬁnd the mean and covariance of the
transformed variable. Its mean is given by

y

(cid:104)

(cid:105)

= M

+

x
(cid:104)

(cid:105)

η

(cid:104)

(cid:105)

= Mµx + µ

(24.4.4)

To ﬁnd the covariance of p(y), consider the displacement of a variable x from its mean, which we write as

∆x

x

x

(cid:105)

− (cid:104)

≡

The covariance is, by deﬁnition, (cid:10)∆x∆xT(cid:11). For y, the displacement is

∆y = M∆x + ∆η,

DRAFT April 24, 2015

(24.4.5)

(24.4.6)

497

Algorithm 24.1 LDS Forward Pass. Compute the ﬁltered posteriors p(ht
parameters θt = (cid:8)A, B, Σh, Σv, ¯h, ¯v(cid:9)

t. The log-likelihood L = log p(v1:T ) is also returned.

v1:t)
|

≡ N

Inference

(ft, Ft) for a LDS with

= LDSFORWARD(0, 0, v1; θ1)

f1, F1, p1
}
{
L
log p1
←
for t

2, T do

= LDSFORWARD(ft−1, Ft−1, vt; θt)

←
ft, Ft, pt
{
L
←
end for
function ldsforward(f , F, v; θ)

}
L + log pt

Bµh + ¯v

Af + ¯h,
µv ←
AFAT + Σh, Σvv
µv),
vv (v
−
µv)T Σ−1

µh ←
Σhh
←
vhΣ−1
µh + ΣT
f (cid:48)
(cid:16)
1
p(cid:48)
2 (v
exp
return f (cid:48), F(cid:48), p(cid:48)

←

←

−

−

←

BΣhhBT + Σv, Σvh
vhΣ−1
F(cid:48)
Σhh
−
←
(cid:17)
(cid:112)
det (2πΣvv)
µv)
/

ΣT

←
vv Σvh

vv (v

−

BΣhh

(cid:46) Mean of p(ht, vt
(cid:46) Covariance of p(ht, vt
(cid:46) Find p(ht

v1:t−1)
|
v1:t−1)
|
v1:t) by conditioning
|

(cid:46) Compute p(vt

v1:t−1)

|

end function

So that the covariance is
∆y∆yT(cid:69)
(cid:68)

=

(cid:68)

(M∆x + ∆η) (M∆x + ∆η)T(cid:69)
(cid:68)

= M

∆x∆xT(cid:69)

MT + M

∆x∆ηT(cid:69)
(cid:68)

(cid:68)

∆η∆xT(cid:69)

+

MT +

(cid:68)

∆η∆ηT(cid:69)

Since the noises η and x are assumed independent, (cid:10)∆η∆xT(cid:11) = 0 we have

Σy = MΣxMT + Σ

24.4.1 Filtering

We represent the ﬁltered distribution as a Gaussian with mean ft and covariance Ft,

p(ht

v1:t)
|

∼ N

(ht ft, Ft)

(24.4.7)

(24.4.8)

This is called the moment representation. Our task is then to ﬁnd a recursion for ft, Ft in terms of ft−1,
Ft−1. A convenient approach is to ﬁrst ﬁnd the joint distribution p(ht, vt
v1:t−1) and then condition on
|
vt to ﬁnd the distribution p(ht
v1:t−1) is a Gaussian whose statistics can be found
|
from the relations

v1:t). The term p(ht, vt
|

vt = Bht + ηv
t ,

ht = Aht−1 + ηh
t

Using the above, and assuming time-invariance and zero biases, we readily ﬁnd

(cid:68)

∆ht∆hT
t |
(cid:68)
∆vt∆hT
t |
(cid:68)
∆vt∆vT
t |
vt
(cid:104)
ht
(cid:104)

v1:t−1

v1:t−1

v1:t−1

(cid:69)

(cid:69)

(cid:69)

= AFt−1AT + Σh,

(cid:16)

(cid:16)

= B

= B

AFt−1AT + Σh

AFt−1AT + Σh

(cid:17)

(cid:17)

,

BT + Σv,

v1:t−1
|
v1:t−1
|

= BA
= A

ht−1
(cid:104)
ht−1
(cid:104)

v1:t−1
|
v1:t−1
|

(cid:105)

(cid:105)

(cid:105)

,

(cid:105)

In the above, using our moment representation of the forward messages

ht−1
(cid:104)

v1:t−1
|

(cid:105) ≡

ft−1,

Then, using conditioning3 p(ht

∆ht−1∆hT

t−1|
vt, v1:t−1) will have mean

≡

v1:t−1

(cid:69)

Ft−1

(cid:68)

|

ft

ht

≡ (cid:104)

v1:t−1
|

(cid:105)

+

(cid:68)
∆ht∆vT
t |

(cid:69) (cid:68)

v1:t−1

3p(x|y) is a Gaussian with mean µx + ΣxyΣ−1

yy

(cid:69)−1

v1:t−1

∆vt∆vT
t |
(cid:1) and covariance Σxx − ΣxyΣ−1

v1:t−1
|

(cid:0)y − µy

(vt

− (cid:104)

vt

)

(cid:105)

yy Σyx.

(24.4.9)

(24.4.10)

(24.4.11)

(24.4.12)

(24.4.13)

(24.4.14)

(24.4.15)

(24.4.16)

498

DRAFT April 24, 2015

Inference

and covariance

Ft

≡

(cid:68)
∆ht∆hT
t |

v1:t−1

(cid:69)

(cid:68)

−

∆ht∆vT
t |

v1:t−1

(cid:69) (cid:68)

∆vt∆vT
t |

v1:t−1

(cid:69)−1 (cid:68)

∆vt∆hT
t |

(cid:69)

v1:t−1

Writing out the above explicitly we have for the mean and covariance:

ft = Aft−1 + PBT (cid:16)

BPBT + Σv
(cid:17)−1

BPBT + Σv

(vt

−

BAft−1)

(cid:17)−1

BP

PBT (cid:16)

Ft = P

−

where

AFt−1AT + Σh

P

≡

(24.4.17)

(24.4.18)

(24.4.19)

(24.4.20)

The recursion is initialised with f0 = 0, F0 = 0. The ﬁltering procedure is presented in algorithm(24.1) with
a single update in LDSforwardUpdate.m.

One can write the covariance update as

Ft = (I

−

KB) P

where we deﬁne the Kalman gain matrix

K = PBT (cid:16)

ΣV + BPBT(cid:17)−1

Symmetrising the updates

(24.4.21)

(24.4.22)

A potential numerical issue with the covariance update (24.4.21) is that it is the diﬀerence of two positive
deﬁnite matrices. If there are numerical errors, the Ft may not be numerically positive deﬁnite, nor sym-
metric. Using the Woodbury identity, deﬁnition(A.11), equation (24.4.19) can be written more compactly
as

(cid:16)

Ft =

P−1 + BTΣ−1

v B

(cid:17)−1

(24.4.23)

Whilst this is positive semideﬁnite, this is numerically expensive since it involves two matrix inversions. An
alternative is to use the deﬁnition of K, from which we can write

KΣvKT = (I

−

KB) PBTKT

Hence we arrive at Joseph’s symmetrized update[114]

Ft = (I

−

KB) P (I

−

KB)T + KΣvKT

(24.4.24)

(24.4.25)

The right-hand side of the above is the addition of two positive deﬁnite matrices so that the resulting update
for the covariance is more numerically stable. A similar method can be used in the backward pass below.
An alternative is to avoid using covariance matrices directly and use their square root as the parameter,
deriving updates for these instead[243, 39].

Prediction

From the dynamics, deﬁnition(24.4), the distribution of the future visible variable is given by propagating
the ﬁltered distribution forwards one timestep (written here for the time-independent case)

p(vt+1

v1:t) =
|

=

ht,ht+1
(cid:16)

p(vt+1

ht+1)p(ht+1
ht)p(ht
|
|
vt+1 B (cid:0)Aft + ¯h(cid:1) + ¯v, B

(cid:16)

v1:t)
|

AFtAT + Σh

(cid:90)

N

DRAFT April 24, 2015

(cid:17)

(cid:17)

BT + Σv

(24.4.26)

(24.4.27)

499

24.4.2 Smoothing : Rauch-Tung-Striebel correction method

The smoothed posterior p(ht
Gaussian. By representing the posterior as a Gaussian with mean gt and covariance Gt,

v1:T ) is necessarily Gaussian since it is the conditional marginal of a larger
|

Inference

p(ht

v1:T )
|

∼ N

(ht gt, Gt)

we can form a recursion for gt and Gt as follows:

p(ht

v1:T ) =
|

=

(cid:90)

ht+1

(cid:90)

ht+1

p(ht, ht+1

v1:T )

|

p(ht

|

v1:T , ht+1)p(ht+1

v1:T ) =
|

(cid:90)

ht+1

p(ht

v1:t, ht+1)p(ht+1
|

v1:T )
|

The term p(ht

v1:t, ht+1) can be found by conditioning the joint distribution
|
p(ht, ht+1

v1:t) = p(ht+1
|

ht,(cid:24)(cid:24)v1:t)p(ht
|

v1:t)
|

(24.4.28)

(24.4.29)

(24.4.30)

(24.4.31)

which is obtained in the usual manner by ﬁnding its mean and covariance: The term p(ht
Gaussian from ﬁltering with mean ft and covariance Ft. Hence the joint distribution p(ht, ht+1
means

v1:t) is a known
v1:t) has

|

|

ht
(cid:104)

v1:t
|

(cid:105)

= ft,

ht+1

v1:t

|

(cid:105)

(cid:104)

= Aft

and covariance elements

(cid:68)

∆ht∆hT
t |

v1:t

(cid:69)

= Ft,

(cid:68)

∆ht∆hT

t+1|

(cid:69)

= FtAT,

(cid:68)

v1:t

(24.4.32)

(cid:69)

= AFtAT + Σh

(24.4.33)

∆ht+1∆hT

v1:t

t+1|

To ﬁnd p(ht
be useful to use the system reversal result, section(8.4.2), which interprets p(ht
linear system going backwards in time:

v1:t, ht+1) we may use the conditioned Gaussian results, result(8.4). However, it will turn out to
v1:t, ht+1) as an equivalent
|

|

ht = ←−Atht+1 + ←−mt + ←−η t

where

←−At

≡

(cid:68)
∆ht∆hT

v1:t

t+1|

(cid:69) (cid:68)

∆ht+1∆hT

v1:t

t+1|

(cid:69)−1

←−mt

ht

≡ (cid:104)

v1:t
|

(cid:105) −

(cid:68)

∆ht∆hT

v1:t

t+1|

(cid:69) (cid:68)

∆ht+1∆hT

t+1|

(24.4.34)

(24.4.35)

(24.4.36)

(cid:69)−1

v1:t

ht+1
(cid:104)

v1:t
|

(cid:105)

and ←−η t

∼ N

(cid:16)

←−η t 0, ←−Σ t

(cid:17)

, with

←−Σ t

≡

(cid:68)
∆ht∆hT
t |

v1:t

(cid:69)

(cid:68)

−

∆ht∆hT

v1:t

t+1|

(cid:69) (cid:68)

∆ht+1∆hT

(cid:69)−1 (cid:68)

v1:t

∆ht+1∆hT
t |

v1:t

(cid:69)

(24.4.37)

t+1|

Using dynamics reversal, equation (24.4.34) and assuming that ht+1 is Gaussian distributed, it is then
straightforward to work out the statistics of p(ht

v1:T ). The mean is given by
|
+ ←−mt = ←−Atgt+1 + ←−mt

gt

ht

≡ (cid:104)

v1:T

|

(cid:105)

= ←−At

ht+1

v1:T

|

(cid:105)

(cid:104)

and covariance

(cid:68)

Gt

≡

∆ht∆hT
t |

v1:T

(cid:69)

(cid:68)

= ←−At

∆ht+1∆hT

t+1|

(cid:69)

←−AT

t + ←−Σ t = ←−AtGt+1←−AT

t + ←−Σ t

v1:T

(24.4.38)

(24.4.39)

This procedure is the Rauch-Tung-Striebel Kalman smoother[248]. This is called a ‘correction’ method
v1:T ). The
since it takes the ﬁltered estimate p(ht
|
procedure is outlined in algorithm(24.2) and is detailed in LDSbackwardUpdate.m. See also LDSsmooth.m.

v1:t) and ‘corrects’ it to form a smoothed estimate p(ht
|

500

DRAFT April 24, 2015

Inference

Algorithm 24.2 LDS Backward Pass. Compute the smoothed posteriors p(ht
ﬁltered results from algorithm(24.1).

v1:T ). This requires the
|

GT
for t

←

FT , gT

T
←
gt, Gt

−
}

fT
←
1, 1 do
= LDSBACKWARD(gt+1, Gt+1, ft, Ft; θt)

{
end for
function ldsbackward(g, G, f , F; θ)

Σh(cid:48)h(cid:48)

Af + ¯h,
ΣT
F
←−Ag + ←−m, G(cid:48)

µh ←
←−Σ
←
g(cid:48)
←
return g(cid:48), G(cid:48)

h(cid:48)hΣ−1

−

←

h(cid:48)h(cid:48)Σh(cid:48)h, ←−A

←

AFAT + Σh,
ΣT
←
←−AG←−AT + ←−Σ

h(cid:48)hΣ−1

Σh(cid:48)h
h(cid:48)h(cid:48), ←−m

AF
←
f
←

−

←−Aµh

(cid:46) Dynamics Reversal p(ht

(cid:46) Statistics of p(ht, ht+1

v1:t)
|
ht+1, v1:t)
|
(cid:46) Backward propagation

end function

Sampling a trajectory from p(h1:T

v1:T )
|

One may use equation (24.4.34) to draw a sample trajectory h1:T from the posterior p(h1:T
by ﬁrst drawing a sample hT from the Gaussian posterior p(hT
we write

v1:T ) =
|

v1:T ). We start
|
(hT fT , FT ). Given this value,

N

hT −1 = ←−AT −1hT + ←−mT −1 + ←−η T −1

(24.4.40)

, we then have a value for hT −1. We
By drawing a sample from the zero mean Gaussian
continue in this way, reversing backwards in time, to build up the sample trajectory hT , hT −1, hT −2, . . . , h1.
This is equivalent to the forward-ﬁltering-backward-sampling approach described in section(23.2.5).

←−η T −1 0, ←−Σ T −1

N

(cid:16)

(cid:17)

The cross moment

An advantage of the dynamics reversal interpretation given above is that the cross moment (which is required
for learning) is immediately obtained from

(cid:68)

∆ht∆hT

v1:T

t+1|

(cid:69)

= ←−AtGt+1

(cid:68)

⇒

hthT

t+1|

v1:T

(cid:69)

= ←−AtGt+1 + gtgT

t+1

(24.4.41)

24.4.3 The likelihood

For the discrete HMM in section(23.2), we showed how the likelihood can be computed directly from the
ﬁltered messages (cid:80)
α(hT ). We cannot apply that technique here since the message distributions we are
hT
passing are conditional distributions p(ht
v1:t), rather than joint distributions p(ht, v1:t). However, help is
|
at hand and we may compute the likelihood using the decomposition

p(v1:T ) =

T
(cid:89)

t=1

p(vt

|

v1:t−1)

in which p(vt

v1:t−1) =

|

N

(vt µt, Σt) with

µ1 ≡
µt ≡

Bµπ
Σ1
BAft−1 Σt

≡
≡

BΣBT + Σπ
B (cid:0)AFt−1AT + Σh

(cid:1) BT + Σv

t = 1
t > 1

The log likelihood is then given by

log p(v1:T ) =

1
2

−

T
(cid:88)

t=1

(cid:104)
(vt

−

µt)T Σ−1
t

(vt

−

µt) + log det (2πΣt)

(cid:105)

DRAFT April 24, 2015

(24.4.42)

(24.4.43)

(24.4.44)

501

24.4.4 Most likely state

Since the mode of a Gaussian is equal to its mean, there is no diﬀerence between the most probable joint
posterior state

Inference

argmax
h1:T

p(h1:T

v1:T )
|

and the set of most probable marginal states

ht = argmax

ht

p(ht

v1:T ),
|

t = 1, . . . , T

(24.4.45)

(24.4.46)

Hence the most likely hidden state sequence is equivalent to the smoothed mean sequence.

24.4.5 Time independence and Riccati equations

Both the ﬁltered Ft and smoothed Gt covariance recursions are independent of the observations v1:T ,
depending only on the parameters of the model. This is a general characteristic of linear Gaussian systems.
Typically the covariance recursions converge quickly to values that are constant throughout the dynamics,
In practice therefore one
with only appreciable diﬀerences close to the boundaries t = 1 and t = T .
often drops the time-dependence of the covariances and approximates them with a single time-independent
covariance. This approximation dramatically reduces storage requirements. The converged ﬁltered F satisﬁes
the recursion

F = AFAT + Σh

(cid:16)

−

AFAT + Σh

(cid:17)

BT (cid:16)

B

(cid:16)

AFAT + Σh

(cid:17)

BT + Σv

(cid:17)−1

(cid:16)

B

AFAT + Σh

(cid:17)

(24.4.47)

which is a form of algebraic Riccati equation. A technique to solve these equations is to initialise the
covariance F to Σ. With this, a new F is found using the right hand side of (24.4.47), and subsequently
recursively updated. Alternatively, using the Woodbury identity, the converged covariance satisﬁes

(cid:18)(cid:16)

F =

AFAT + Σh

(cid:17)−1

+ BTΣv

−1B

(cid:19)−1

(24.4.48)

although this form is less numerically convenient in forming an iterative solver for F since it requires two
matrix inversions.

Example 24.4 (Newtonian Trajectory Analysis). A toy rocket with unknown mass and initial velocity
In addition, the constant accelerations from the rocket’s propulsion system are
is launched in the air.
unknown. It is known that Newton’s laws apply and an instrument can make very noisy measurements
of the horizontal distance x(t) and vertical height y(t) of the rocket at each time t. Based on these noisy
measurements, our task is to infer the position of the rocket at each time.

Although this is perhaps most appropriately considered using continuous time dynamics, we will translate
this into a discrete time approximation. Newton’s law states that

d2
dt2 x =

fx(t)
m

,

d2
dt2 y =

fy(t)
m

(24.4.49)

where m is the mass of the object and fx(t), fy(t) are the horizontal and vertical forces respectively. As
they stand, these equations are not in a form directly usable in the LDS framework. A naive approach is to
reparameterise time to use the variable ˜t such that t
˜t∆, where ˜t is integer and ∆ is a unit of time. The
dynamics is then

≡

x((˜t + 1)∆) = x(˜t∆) + ∆x(cid:48)(˜t∆), y((˜t + 1)∆) = y(˜t∆) + ∆y(cid:48)(˜t∆)

(24.4.50)

where y(cid:48)(t)

≡

dy
dt . We can write an update equation for the x(cid:48) and y(cid:48) as

x(cid:48)((˜t + 1)∆) = x(cid:48)(˜t∆) + fx(˜t∆)∆/m,

y(cid:48)((˜t + 1)∆) = y(cid:48)(˜t∆) + fx(˜t∆)∆/m

(24.4.51)

502

DRAFT April 24, 2015

Learning Linear Dynamical Systems

Figure 24.7: Estimate of the trajectory of a Newtonian
ballistic object based on noisy observations (small cir-
cles). All time labels are known but omitted in the
plot. The ‘x’ points are the true positions of the ob-
ject, and the crosses ‘+’ are the estimated smoothed
mean positions
of the object plotted every
|
several time steps. See demoLDStracking.m

xt, yt
(cid:104)

v1:T

(cid:105)

These two sets of coupled discrete time diﬀerence equations approximate Newton’s law equation (24.4.49).
For simplicity, we relabel ax(t) = fx(t)/m(t), ay(t) = fy(t)/m(t). These accelerations are unknown but
assumed to change slowly over time

ax((˜t + 1)∆) = ax(˜t∆) + ηx, ay((˜t + 1)∆) = ay(˜t∆) + ηy,

(24.4.52)

where ηx and ηy are small noise terms. The initial distributions for the accelerations are assumed vague,
using a zero mean Gaussian with large variance. We describe the above model by deﬁning

(cid:2)x(cid:48)(t), x(t), y(cid:48)(t), y(t), ax(t), ay(t)(cid:3)T

ht

≡

(24.4.53)

as the hidden variable, giving rise to a H = 6 dimensional LDS with transition and emission matrices as
below:

A =











0 ∆ 0
0
1
0
0
0
0
0
∆ 1
0 ∆
0
0
0
1
0
0
0 ∆ 1
0
0
1
0
0
0
0
1
0
0
0
0
0











,

B =

(cid:18) 0 1 0 0 0 0
0 0 0 1 0 0

(cid:19)

(24.4.54)

We use large a covariance Σπ to state that little is known about the latent state initial values. Based
then on noisy observations vt = Bht + ηt we attempt to infer the unknown trajectory using smoothing. A
demonstration is given in ﬁg(24.7). Despite the signiﬁcant observation noise, the object trajectory can be
accurately inferred.

24.5 Learning Linear Dynamical Systems

Whilst in many applications, particularly of underlying known physical processes, the parameters of the
LDS are known, in many machine learning tasks we need to learn the parameters of the LDS based on v1:T .
For simplicity we assume that we know the dimensionality H of the LDS.

24.5.1 Identiﬁability issues

An interesting question is whether we can uniquely identify (learn) the parameters of an LDS. There are
always trivial redundancies in the solution obtained by permuting the hidden variables arbitrarily and
ﬂipping their signs. To show that there are potentially many more equivalent solutions, consider the following
LDS

vt = Bht + ηv
t ,

ht = Aht−1 + ηh
t

(24.5.1)

We now attempt to transform this original system to a new form which will produce exactly the same
outputs v1:T . For an invertible matrix R we consider

Rht = RAR−1Rht−1 + Rηh
t

DRAFT April 24, 2015

(24.5.2)

503

−1000100200300400500600700800−150−100−50050100150200250300xyLearning Linear Dynamical Systems

which is representable as a new latent dynamics

ˆht = ˆAˆht−1 + ˆηh
t

where ˆA
≡
the transformed h:

RAR−1, ˆht

≡

Rht, ˆηh

t ≡

Rηh

t . In addition, we can reexpress the outputs to be a function of

(24.5.3)

vt = BR−1Rht + ηv

t = ˆBˆht + ηv
t

(24.5.4)

Hence, provided we place no constraints on A, B and Σh there exists an inﬁnite space of equivalent solutions,
ˆA = RA R−1, ˆB = BR−1, ˆΣH = RΣhRT, all with the same likelihood value. This means that interpreting
the learned parameters needs to be done with some care.

24.5.2 EM algorithm

For simplicity, we assume we have a single sequence v1:T , to which we wish to ﬁt a LDS using maximum
likelihood. Since the LDS contains latent variables one approach is to use the EM algorithm. As usual, the
M-step of the EM algorithm requires us to maximise the energy

log p(v1:T , h1:T )
(cid:104)

(cid:105)pold(h1:T |v1:T )

(24.5.5)

with respect to the parameters A, B, a, Σ, Σv, Σh. Thanks to the form of the LDS the energy decomposes
as

log p(h1)
(cid:104)

(cid:105)pold(h1|v1:T ) +

T
(cid:88)

t=2

log p(ht
(cid:104)

ht−1)
|

(cid:105)pold(ht,ht−1|v1:T ) +

T
(cid:88)

t=1

log p(vt
(cid:104)

ht)
|

(cid:105)pold(ht|v1:T )

(24.5.6)

It is straightforward to derive that the M-step for the parameters is given by (angled brackets
expectation with respect to the smoothed posterior pold(h1:T

v1:T )):
|

µnew

π =

Σnew

π =

h1
(cid:105)
(cid:104)
(cid:68)
h1hT
1

(cid:69)

h1

− (cid:104)

Anew =

T −1
(cid:88)

(cid:68)

(cid:69)

ht+1hT
t

T

h1

(cid:105) (cid:104)
(cid:105)
(cid:32)T −1
(cid:88)

(cid:33)−1

(cid:69)

(cid:68)

hthT
t

Bnew =

t=1

T
(cid:88)

t=1

t=1

(cid:32) T

(cid:88)

(cid:68)

T

(cid:33)−1

(cid:69)

hthT
t

t=1

vt

ht
(cid:104)

(cid:105)

T
(cid:88)

(cid:16)

t=1

vtvT

t −

vt

ht
(cid:104)

T BnewT
(cid:105)

−

Bnew

ht
(cid:104)

(cid:105)

t + Bnew (cid:68)
vT

hthT
t

(cid:69)

BnewT(cid:17)

ht+1hT

t+1

(cid:69)

−

Anew (cid:68)

hthT

t+1

(cid:69)

(cid:68)

−

ht+1hT
t

(cid:69)

AnewT + Anew (cid:68)

hthT
t

(cid:69)

AnewT(cid:17)

Σnew

v =

Σnew

h =

1
T

T

T −1
(cid:88)

(cid:16)(cid:68)

1

t=1

1

−

The above can be simpliﬁed to

Σnew

v =

1
T

(cid:88)

(cid:16)

t

vtvT

t −

vt

ht
(cid:104)

T BnewT(cid:17)
(cid:105)

Similarly,

Σnew

h =

T −1
(cid:88)

(cid:16)(cid:68)

1

t=1

1

−

T

ht+1hT

t+1

(cid:69)

−

Anew (cid:68)

hthT

t+1

(cid:69)(cid:17)

The statistics required therefore include smoothed means, covariances, and cross moments. The extension
to learning multiple timeseries is straightforward since the energy is simply summed over the individual

504

DRAFT April 24, 2015

denote

(cid:104)·(cid:105)

(24.5.7)

(24.5.8)

(24.5.9)

(24.5.10)

(24.5.11)

(24.5.12)

(24.5.13)

(24.5.14)

Learning Linear Dynamical Systems

sequences.

The performance of the EM algorithm for the LDS often depends heavily on the initialisation. If we remove
the hidden to hidden links, the model is closely related to Factor Analysis (the LDS can be considered a
temporal extension of Factor Analysis). One initialisation technique therefore is to learn the B matrix using
Factor Analysis by treating the observations as temporally independent.

Note that whilst the LDS model in not identiﬁable, as described in section(24.5.1), the M-step for the EM
algorithm is unique. This apparent contradiction is resolved when one considers that the EM algorithm is
a conditional method, updating depending on the previous parameters and ultimately the initialisation. It
is this initialisation that breaks the invariance.

24.5.3 Subspace Methods

An alternative to maximum likelihood training is to use a subspace method[301, 265]. The chief beneﬁt
of these techniques is that they avoid the convergence diﬃculties of EM. To motivate subspace techniques,
consider a deterministic LDS

ht = Aht−1,

vt = Bht

(24.5.15)

Under this assumption, vt = Bht = BAht−1 and, more generally, vt = BAt−1h1. This means that a H-
dimensional system underlies all visible information since all points Ath1 lie in a H-dimensional subspace,
which is then projected to form the observation. This suggests that some form of subspace identiﬁcation
technique will enable us to learn A and B.

Given a set of observation vectors v1, . . . , vt, consider the block Hankel matrix formed from stacking L
consecutive observation vectors. For example, for T = 6 and L = 3, this is





M =

v1 v2 v3 v4
v2 v3 v4 v5
v3 v4 v5 v6





If the v are generated from a (noise free) LDS, we can write

(24.5.16)





M =

Bh2

Bh1
Bh4
BAh1 BAh2 BAh3 BAh4
BA2h1 BA2h2 BA2h3 BA2h4

Bh3

We now ﬁnd the SVD of M,

M = ˆU ˆS ˆVT
(cid:124) (cid:123)(cid:122) (cid:125)
W





 =



B
BA
BA2



 (h1 h2 h3 h4)

(24.5.17)

(24.5.18)

where W is termed the extended observability matrix . The matrix ˆS will contain the singular values up to
the dimension of the hidden variables H, with the remaining singular values 0. From equation (24.5.17),
this means that the emission matrix B is contained in ˆU1:V,1:H . The estimated hidden variables are then
contained in the submatrix W1:H,1:T −L+1,

(h1 h2 h3 h4) = W1:H,1:T −L+1

(24.5.19)

Based on the relation ht = Aht−1 one can then ﬁnd the best least squares estimate for A by minimising

T
(cid:88)

t=2

Aht−1)2

(ht

−

for which the optimal solution is

A = (h2 h3

. . . ht) (h1 h2

. . . hT −1)†

DRAFT April 24, 2015

(24.5.20)

(24.5.21)

505

Switching Auto-Regressive Models

s1

v1

s2

v2

s3

v3

s4

v4

In
Figure 24.8: A ﬁrst order switching AR model.
terms of inference, conditioned on v1:T , this is a HMM.

where † denotes the pseudo inverse, see LDSsubspace.m. Estimates for the covariance matrices can also be
obtained from the residual errors in ﬁtting the block Hankel matrix and extended observability matrix.
Whilst this derivation formally holds only for the noise free case one can nevertheless apply this in the case
of non-zero noise and hope to gain an estimate for A and B that is correct in the mean. In addition to
forming a solution in its own right, the subspace method forms a potentially useful way to initialise the EM
algorithm.

24.5.4 Structured LDSs

Many physical equations are local both in time and space. For example in weather models the atmosphere
is partitioned into cells hi(t) each containing the pressure at that location. The equations describing how
the pressure updates only depend on the pressure at the current cell and small number of neighbouring cells
at the previous time t
1. If we use a linear model and measure some aspects of the cells at each time, then
the weather is describable by a LDS with a highly structured sparse transition matrix A. In practice, the
weather models are non-linear but local linear approximations are often employed[270]. A similar situation
arises in brain imaging in which voxels (local cubes of activity) depend only on their neighbours from the
previous timestep[111].

−

Another application of structured LDSs is in temporal independent component analysis. This is deﬁned
as the discovery of a set of independent latent dynamical processes, from which the data is a projected
observation.
If each independent dynamical process can itself be described by a LDS, this gives rise to
a structured LDS with a block diagonal transition matrix A. Such models can be used to extract inde-
pendent components under prior knowledge of the likely underlying frequencies in each of the temporal
compoments[62].

24.5.5 Bayesian LDSs

The extension to placing priors on the transition and emission parameters of the LDS leads in general to
computational diﬃculties in computing the likelihood. For example, for a prior on A, the likelihood is
p(v1:T ) = (cid:82)
A)p(A) which is diﬃcult to evaluate since the dependence of the likelihood on the
matrix A is a complicated function. Approximate treatments of this case are beyond the scope of this
book, although we brieﬂy note that sampling methods[57, 105] are popular in this context, in addition to
deterministic variational approximations[27, 23, 62].

A p(v1:T

|

24.6 Switching Auto-Regressive Models

Whilst the linear dynamical models considered so far in this chapter are powerful, they nevertheless have
some inherent restrictions. For example, they cannot model abrupt changes in the observation. Here we
describe an extension of the AR model. We consider a set of S diﬀerent AR models, each with associated
coeﬃcients a(s), s = 1, . . . ,
S, and allow the model to select one of these AR models at each time. For a
time-series of scalar values v1:T an Lth order switching AR model can be written as

@@

vt = ˆvT

t−1a(st) + ηt,

(cid:0)ηt 0, σ2(st)(cid:1)

ηt

∼ N

where we now have a set of AR coeﬃcients θ = (cid:8)a(s), σ2(s), s
themselves have a Markov transition p(s1:T ) = (cid:81)

1, . . . , S

}

∈ {

t p(st

st−1) so that the full model is, see ﬁg(24.7),
|

p(v1:T , s1:T

θ) =
|

(cid:89)

t

p(vt

vt−1, . . . , vt−L, st,
|

θ)p(st
|

|

st−1)

506

(24.6.2)

DRAFT April 24, 2015

(24.6.1)

(cid:9). The discrete switch variables

Switching Auto-Regressive Models

Figure 24.9: Learning a Switching AR
model. The upper plot shows the train
data. The colour indicates which of the
two AR models is active at that time.
Whilst this information is plotted here, this
is assumed unknown to the learning algo-
rithm, as are the coeﬃcients a(s). We
assume that the order L = 2 and num-
ber of switches S = 2 however is known.
In the bottom plot we show the time se-
ries again after training in which we colour
the points according to the most likely
smoothed AR model at each timestep. See
demoSARlearn.m.

24.6.1 Inference

Given an observed sequence v1:T and parameters θ inference is straightforward since this is a form of HMM.
To make this more apparent we may write

p(v1:T , s1:T ) =

(cid:89)

t

ˆp(vt

st)p(st
|

st−1)
|

where

ˆp(vt

st)
|

≡

p(vt

vt−1, . . . , vt−L, st) =
|

N

(cid:16)

vt ˆvT

(cid:17)
t−1a(st), σ2(st)

Note that the emission distribution ˆp(vt

st) is time-dependent. The ﬁltering recursion is then
|

α(st) =

(cid:88)

st−1

ˆp(vt

st)p(st
|

|

st−1)α(st−1)

(24.6.3)

(24.6.4)

(24.6.5)

Smoothing can be achieved using the standard recursions, modiﬁed to use the time-dependent emissions,
see demoSARinference.m.

With high frequency data it is unlikely that a change in the switch variable is reasonable at each time t. A
simple constraint to account for this is to use a modiﬁed transition

ˆp(st

st−1) =
|

(cid:26) p(st
st−1)
|
δ (st

−

mod (t, Tskip) = 0

st−1) otherwise

(24.6.6)

24.6.2 Maximum likelihood learning using EM

To ﬁt the set of AR coeﬃcients and innovation variances, a(s), σ2(s), s = 1, . . . , S, using maximum likelihood
training for a set of data v1:T , we may make use of the EM algorithm.

M-step

Up to negligible constants, the energy is given by

E =

(cid:88)
t (cid:104)

log p(vt

ˆvt−1, a(st))
|

(cid:105)pold(st|v1:T ) +

(cid:88)
t (cid:104)

log p(st

st−1)
|

(cid:105)pold(st,st−1)

(24.6.7)

which we need to maximise with respect to the parameters θ. Using the deﬁnition of the emission and
isolating the dependency on a, we have
(cid:28) 1

(cid:17)2

(cid:29)

(cid:16)

(cid:88)

ˆvT
t−1a(st)

+ log σ2(st)

+ const.

(24.6.8)

2E =

−

σ2(st)

t

vt

−

pold(st|v1:T )

DRAFT April 24, 2015

507

050100150200250300350400−100−50050100sample switches050100150200250300350400−100−50050100learned switchesSwitching Auto-Regressive Models

On diﬀerentiating with respect to a(s) and equating to zero, the optimal a(s) satisﬁes the linear equation

(cid:88)

t

pold(st = s

v1:T )
|

vt ˆvt−1
σ2(s)

=

(cid:34)

(cid:88)

t

pold(st = s
|

v1:T )

(cid:35)

ˆvt−1 ˆvT
σ2(s)

t−1

a(s)

(24.6.9)

which may be solved using Gaussian elimination. Similarly one may show that updates that maximise the
energy with respect to σ2 are

σ2(s) =

1
t(cid:48) pold(s(cid:48)
t = s

(cid:80)

(cid:88)

v1:T )

|

t

pold(st = s

v1:T )
|

(cid:104)
vt

−

(cid:105)2

ˆvT
t−1a(s)

(24.6.10)

st−1) follows the standard EM for HMM rule, equation (23.3.5), see SARlearn.m. Here
The update for p(st
|
we don’t include an update for the prior p(s1) since there is insuﬃcient information at the start of the
sequence and assume p(s1) is ﬂat.

E-step

The M-step requires the smoothed statistics pold(st = s
obtained from HMM inference.

v1:T ) and pold(st = s, st−1 = s(cid:48)
|

|

v1:T ) which can be

Example 24.5 (Learning a switching AR model). In ﬁg(24.9) the train data is generated by an Switching
AR model so that we know the ground truth as to which model generated which parts of the data. Based
on the train data (assuming the labels st are unknown), a Switching AR model is ﬁtted using EM. In this
case the problem is straightforward so that a good estimate is obtained of both the sets of AR parameters
and which switches were used at which time.

Example 24.6 (Modelling parts of speech). In ﬁg(24.10) a segment of a speech signal (corresponding to
the spoken digit ‘four’) is shown. We model this data using a switching AR model with S = 10 states.
Each of the 10 available AR models is responsible for modelling the dynamics of a basic subunit of speech
[97, 207]. The model was trained on many example clean spoken digit sequences using S = 10 states with a
left-to-right transition matrix. An additional complexity is that we wish to use the model to clean up a noisy
speech signal. A simple model for the noise is that it is Gaussian and additive on the original speech signal
vt, forming now a new noisy observation ˜vt. Based then on a sequence of noisy ˜v1:T we wish to infer the
switch states and the clean signal v1:T . Unfortunately, this task is no longer in the form of an SAR since we
now have both continuous and discrete latent states. Formally this model falls in the class of the switching
linear dynamical systems, as described in the following chapter. The results using this more complex model
show how denoising of complex signals can be achieved and are a prelude to the more advanced material in
the following chapter.

24.7 Summary

•

•

•

•

508

Continuous observations can be modelled using autoregressive models.

Observed linear dynamical systems are the vector versions of autoregressive models.

In order for these
Latent continuous dynamical processes can be used to model many physical systems.
to be computationally tractable, one needs to restrict the transition and emission distributions. The latent
linear dynamical system is such as restriction to linear Gaussian transitions and emissions.

The latent linear dynamical system is a powerful timeseries model with widespread application in tracking
and signal representation.

DRAFT April 24, 2015

Code

s1

v1

˜v1

s2

v2

˜v2

s3

v3

˜v3

s4

v4

˜v4

Figure 24.10: (a): A latent switching (second order) AR model. Here the st indicates which of a set of 10
available AR models is active at time t. The square nodes emphasise that these are discrete variables. The
‘clean’ AR signal vt, which is not observed, is corrupted by additive noise to form the noisy observations
In terms of inference, conditioned on ˜v1:T , this can be expressed as a Switching LDS, chapter(25).
˜vt.
(b): Signal reconstruction using the latent switching AR model in (a). Top: noisy signal ˜v1:T ; bottom:
reconstructed clean signal v1:T . The dashed lines and the numbers show the most-likely state segmentation
arg maxs1:T p(s1:T

˜v1:T ) of the 10 states, from left (1) to right (10).
|

24.8 Code

In the Linear Dynamical System code below only the simplest form of the recursions is given. No attempt
has been made to ensure numerical stability.

LDSforwardUpdate.m: LDS forward
LDSbackwardUpdate.m: LDS backward
LDSsmooth.m: Linear dynamical system : ﬁltering and smoothing
LDSforward.m: Alternative LDS forward algorithm (see SLDS chapter)
LDSbackward.m: Alternative LDS backward algorithm (see SLDS chapter)
demoSumprodGaussCanonLDS.m: Sum-product algorithm for smoothed inference
demoLDStracking.m: Demo of tracking in a Newtonian system
LDSsubspace.m: Subspace Learning (Hankel matrix method)
demoLDSsubspace.m: Demo of subspace learning method

24.8.1 Autoregressive models

Note that in the code the autoregressive vector a has as its last entry the ﬁrst AR coeﬃcient (i.e. in reverse
order to that presented in the text).

ARtrain.m: Learn AR coeﬃcients (Gaussian Elimination)
demoARtrain.m: Demo of ﬁtting an AR model to data
ARlds.m: Learn time-varying AR coeﬃcients using a LDS
demoARlds.m: Demo of learning AR coeﬃcients using an LDS
demoSARinference.m: Demo for inference in a switching autoregressive model
SARlearn.m: Learning of a SAR using EM
demoSARlearn.m: Demo of SAR learning
HMMforwardSAR.m: Switching autoregressive HMM forward pass
HMMbackwardSAR.m: Switching autoregressive HMM backward pass

DRAFT April 24, 2015

509

123456789101234567891024.9 Exercises

Exercise 24.1. Consider the two-dimensional linear model

ht = Rθht−1,

Rθ =

(cid:18) cos θ
sin θ

sin θ
−
cos θ

(cid:19)

Exercises

(24.9.1)

where Rθ is a rotation matrix which rotates the vector ht through angle θ in one timestep.

1. Explain why the eigenvalues of a rotation matrix are (in general) imaginary.

2. Explain how to model a sinusoid, rotating with angular velocity ω using a two-dimensional latent LDS.

3. By writing

(cid:18) xt
yt

(cid:19)

(cid:18) R11 R12
R21 R22

(cid:19) (cid:18) xt−1
yt−1

=

(cid:19)

eliminate yt to write an equation for xt+1 in terms of xt and xt−1.

4. Explain how to model a sinusoid using an AR model.

(24.9.2)

5. Explain the relationship between the second order diﬀerential equation ¨x =

λx, which describes
a Harmonic Oscillator, and the second order diﬀerence equation which approximates this diﬀerential
equation. Is it possible to ﬁnd a diﬀerence equation which exactly matches the solution of the diﬀerential
equation at particular points?

−

Exercise 24.2. Show that for any square anti-symmetric matrix M,

M =

MT

−

the matrix exponential (in MATLAB this is expm)

A = exp (M)

is orthogonal, namely

ATA = I

(24.9.3)

(24.9.4)

(24.9.5)

Explain how to construct random orthogonal matrices with some control over the angles of the complex
eigenvalues.

Exercise 24.3. Run demoLDStracking.m which tracks a ballistic object using a Linear Dynamical system,
see example(24.4). Modify demoLDStracking.m so that in addition to the x and y positions, the x speed is
also observed. Compare and contrast the accuracy of the tracking with and without this extra information.

Exercise 24.4. nightsong.mat contains a small stereo segment nightingale song sampled at 44100 Hertz.

1. Plot the original waveform using plot(x(:,1))

2. Plot the spectrogram using

y=myspecgram(x(:,1),1024,44100); imagesc(log(abs(y)))

3. The routine demoGMMem.m demonstrates ﬁtting a mixture of Gaussians to data. The mixture assign-
ment probabilities are contained in phgn. Write a routine to cluster the data v=log(abs(y)) using 8
Gaussian components, and explain how one might segment the series x into diﬀerent regions.

4. Examine demoARlds.m which ﬁts autoregressive coeﬃcients using an interpretation as a Linear Dy-
namical System. Adapt the routine demoARlds.m to learn the AR coeﬃcients of the data x. You will
almost certainly need to subsample the data x – for example by taking every 4th datapoint. With the
learned AR coeﬃcients (use the smoothed results) ﬁt a Gaussian mixture with 8 components. Compare
and contrast your results with those obtained from the Gaussian mixture model ﬁt to the spectrogram.

510

DRAFT April 24, 2015

Exercises

Exercise 24.5. Consider a supervised learning problem in which we make a linear model of the scalar output
yt based on vector input xt:

yt = wT

t xt + ηy

t

where ηy

t is zero mean Gaussian noise with variance σ2

y. Train data

(24.9.6)

=

(xt, yt), t = 1, . . . , T
{

}

D

is available.

1. For a time-invariant weight vector wt

noise variance σ2

≡
y by maximum likelihood.

w, explain how to ﬁnd the single weight vector w and the

2. Extend the above model to include a transition

wt = wt−1 + ηw
t

(24.9.7)

where ηw
is zero mean Gaussian noise with a given covariance Σw; w1 has zero mean. Explain
t
how to cast ﬁnding
as smoothing in a related Linear Dynamical System. Write a routine W =
LinPredAR(X,Y,SigmaW,SigmaY) that takes an input data matrix X = [x1, . . . , xT ] where each column
contains an input, and vector Y = [y1, . . . , yT ]T; SigmaW is the additive weight noise and SigmaY is an
assumed known time-invariant output noise. The returned W contains the smoothed mean weights.

wt
(cid:104)

|D(cid:105)

Exercise 24.6. This exercise relates to forming an α-β style smoothing approach, as described in sec-
tion(23.2.3), but applied to the LDS. Note that the derivations in section(23.2.3) hold for continuous vari-
ables as well simply on replacing summation with integration.

1. By virtue of the fact that the smoothed posterior for a LDS, γ(ht)

v1:T ) is Gaussian, and using
the relation γ(ht) = α(ht)β(ht), explain why the β message for an LDS can be represented in the form

p(ht

≡

|

β(ht) = ztexp

(cid:18)

1
2

−

t Ztht + hT
hT

t zt

(cid:19)

where Zt is a (not necessarily full rank) matrix.

2. Based on the recursion

β(ht−1) =

(cid:90)

ht

p(vt

ht)p(ht
|

|

ht−1)β(ht)

derive the recursion (ignoring the prefactor zt)

Lt = Q−1
t + BT
(cid:0)Q−1
Zt−1 = AT
t
t −
t L−1
t Q−1
zt−1 = AT
t

t R−1
t Bt + Zt
t L−1
Q−1
(cid:16)
t R−1
BT

t Q−1
t
t + zt

(cid:1) At
(cid:17)

(24.9.8)

(24.9.9)

(24.9.10)

(24.9.11)

(24.9.12)

with initialisation ZT = 0, zT = 0. The notation Qt
distribution p(ht

≡
t is the covariance of the emission p(vt

Σv

ht−1), and Rt
|

≡

Σh

t is the covariance matrix of the transition

ht).
|

(24.9.13)

3. Show that the posterior covariance and mean are given by

(cid:0)F−1

t + Zt

(cid:1)−1

,

(cid:0)F−1

t + Zt

(cid:1)−1 (cid:0)F−1

t

ft + zt

(cid:1)

where Ft and ft are the ﬁltered mean and covariance.

Note that this parallel smoothing recursion is not appropriate in the case of small covariance since, due to
the explicit appearance of the inverse of the covariances, numerical stability issues can arise. However, it is
possible to reexpress the recursion without explicit reference to inverse noise covariances, see [16].

DRAFT April 24, 2015

511

Exercises

512

DRAFT April 24, 2015

CHAPTER 25

Switching Linear Dynamical Systems

Hidden Markov models assume that the underlying process is discrete; linear dynamical systems that the
underlying process is continuous. However, there are scenarios in which the underlying system might jump
from one continuous regime to another. In this chapter we discuss a class of models that can be used in this
situation. Unfortunately the technical demands of this class of models are somewhat more involved than in
previous chapters, although the models are correspondingly more powerful.

25.1 Introduction

Complex timeseries which are not well described globally by a single Linear Dynamical System may be
divided into segments, each modelled by a potentially diﬀerent LDS. Such models can handle situations
in which the underlying model ‘jumps’ from one parameter setting to another. For example a single LDS
might well represent the normal ﬂows in a chemical plant. When a break in a pipeline occurs, the dynamics
of the system changes from one set of linear ﬂow equations to another. This scenario can be modelled
using a set of two linear systems, each with diﬀerent parameters. The discrete latent variable at each time
st
indicates which of the LDSs is most appropriate at the current time. This is
called a Switching LDS and is used in many disciplines, from econometrics to machine learning[12, 63, 59,
235, 324, 189].

normal, pipe broken

∈ {

}

25.2 The Switching LDS

At each time t, a switch variable st
1, . . . , S describes which of a set of LDSs is to be used. The continuous
observation (or ‘visible’) variable vt, dim (vt) = V is linearly related to the continuous hidden variable ht,
dim (ht) = H by

∈

vt = B(st)ht + ηv(st),

ηv(st)

(ηv(st) ¯v(st), Σv(st))

∼ N

(25.2.1)

is active at time t. The
Here st describes which one from the set of emission matrices
observation noise ηv(st) is drawn from a Gaussian with mean ¯v(st) and covariance Σv(st). The transition
dynamics of the continuous hidden state ht is linear,

B(1), . . . , B(S)
{

}

ht = A(st)ht−1 + ηh(st),

ηh(st)

(cid:16)

∼ N

ηh(st) ¯h(st), Σh(st)

(cid:17)

(25.2.2)

and the switch variable st selects a single transition matrix from the available set
. The
Gaussian transition noise ηh(st) also depends on the switch variable. The dynamics of st itself is Markovian,
st−1). For the more general ‘augmented’ aSLDS model the switch st is dependent on
with transition p(st
|

A(1), . . . , A(S)
}

{

513

Gaussian Sum Filtering

s1

s2

s3

s4

h1

v1

h2

v2

h3

v3

h4

v4

Figure 25.1: The independence structure of the aSLDS.
Square nodes st denote discrete switch variables; ht are
continuous latent/hidden variables, and vt continuous ob-
served/visible variables. The discrete state st determines
which Linear Dynamical system from a ﬁnite set of Linear
Dynamical systems is operational at time t. In the SLDS
links from h to s are not normally considered.

both the previous st−1 and ht−1. The model deﬁnes a joint distribution (see ﬁg(25.1))

p(v1:T , h1:T , s1:T ) =

T
(cid:89)

t=1

p(vt

ht, st)p(ht
|

ht−1, st)p(st
|

ht−1, st−1)
|

with

p(vt

|

ht, st) =

N

(vt ¯v(st) + B(st)ht, Σv(st)) ,

p(ht

ht−1, st) =
|

N

(cid:0)ht ¯h(st) + A(st)ht, Σh(st)(cid:1)

(25.2.3)

At time t = 1, p(s1
Gaussians p(h1

h0, s0) denotes the initial switch distribution p(s1), and p(h1
|
s1) =
|

(h1 µπ(s1), Σπ(s1)).

N

h0, s1) denotes the initial
|

The SLDS can be thought of as a marriage between a hidden Markov model and a Linear Dynamical System.
The SLDS is also called a Jump Markov model/process, switching Kalman Filter, Switching Linear Gaussian
State Space model, Conditional Linear Gaussian Model.

25.2.1 Exact inference is computationally intractable

Both exact ﬁltered and smoothed inference in the SLDS is intractable, scaling exponentially with time. As
an informal explanation, consider ﬁltered posterior inference for which, by analogy with equation (23.2.9),
the forward pass is

p(st+1, ht+1

v1:t+1) =

|

(cid:90)

(cid:88)

st

ht

p(st+1, ht+1

st, ht, vt+1)p(st, ht
|

v1:t)
|

(25.2.4)

|

|

|

v1) = p(h1

s1, v1)p(s1
|

v1) is an indexed set of Gaussians. At timestep 2, due to the
At timestep 1, p(s1, h1
summation over the states s1, p(s2, h2
v1:2) will be an indexed set of S Gaussians; similarly at timestep
3, it will be S2 and, in general, gives rise to St−1 Gaussians at time t. Even for small t, the number of
components required to exactly represent the ﬁltered distribution is therefore computationally intractable.
Analogously, smoothing is also intractable. The origin of the intractability of the SLDS diﬀers from ‘struc-
tural intractability’ that we’ve previously encountered. In the SLDS, in terms of the cluster variables x1:T ,
(st, ht) and visible variables v1:T , the graph of the distribution is singly-connected. From a purely
with xt
graph theoretic viewpoint, one would therefore envisage little diﬃculty in carrying out inference. Indeed, as
we saw above, the derivation of the ﬁltering algorithm is straightforward since the graph is singly-connected.
However, the numerical implementation of the algorithm is intractable since the description of the messages
requires an exponentially increasing number of terms.

≡

In order to deal with this intractability, several approximation schemes have been introduced [105, 120,
188, 174, 173]. Here we focus on techniques which approximate the switch conditional posteriors using a
limited mixture of Gaussians. Since the exact posterior distributions are mixtures of Gaussians, albeit with
an exponentially large number of components, the aim is to drop low weight components such that the
resulting approximation accurately represents the posterior.

25.3 Gaussian Sum Filtering

Equation(25.2.4) describes the exact ﬁltering recursion and generates an exponentially increasing number of
components with time. In general, however, the inﬂuence of ancient observations will be much less relevant

514

DRAFT April 24, 2015

Gaussian Sum Filtering

than that of recent observations. This suggests that the ‘eﬀective time’ is limited and that therefore a cor-
responding limited number of components in the Gaussian mixture may suﬃce to accurately represent the
v1:t) based on a Gaussian mixture approxi-
ﬁltered posterior [6]. Our aim is to form a recursion for p(st, ht
|
mation of p(ht
v1:t), the
v1:t)
exact recursion equation (25.2.4) is approximated by

st, v1:t). Given an approximation of the ﬁltered distribution p(st, ht
|

q(st, ht

≈

|

|

q(st+1, ht+1

v1:t+1) =

|

(cid:90)

(cid:88)

st

ht

p(st+1, ht+1

st, ht, vt+1)q(st, ht
|

v1:t)
|

(25.3.1)

This approximation to the ﬁltered posterior at the next timestep will contain S times more components
than in the previous timestep and, to prevent an exponential explosion in mixture components, we will need
to subsequently collapse the mixture q(st+1, ht+1
v1:t+1) in a suitable way. It is useful to break the ﬁltered
|
approximation for equation (25.2.4) into continuous and discrete parts:

q(ht, st

|

v1:t) = q(ht

st, v1:t)q(st
|

|

v1:t)

and derive separate ﬁltered update formulae, as described below.

(25.3.2)

25.3.1 Continuous ﬁltering

The exact representation of p(ht
feasibility we therefore approximate this with a limited I-component mixture

st, v1:t) is a mixture with O (cid:0)St−1(cid:1) components. To retain computational
|

q(ht

st, v1:t) =
|

I
(cid:88)

it=1

q(ht

|

it, st, v1:t)q(it

st, v1:t)

|

(25.3.3)

it, st, v1:t) is a Gaussian parameterised with mean f (it, st) and covariance F(it, st). Strictly
where q(ht
speaking, we should use the notation ft(it, st) since, for each time t, we have a set of means indexed by it, st,
although we drop these dependencies in the notation used here.

|

An important remark is that many techniques approximate p(ht
state of st. Naturally, this gives rise to a mixture of Gaussians for p(ht
However, in making a single Gaussian approximation to p(ht
may be poor. Our aim here is to maintain an accurate approximation to p(ht
Gaussians.

st, v1:t) using a single Gaussian, one for each
|
v1:t).
st p(ht
st, v1:t) the representation of the posterior
|
st, v1:t) by using a mixture of
|

st, v1:t)p(st
|

v1:t) = (cid:80)

|

|

To ﬁnd a recursion for the approximating distribution we ﬁrst assume that we know the ﬁltered approxi-
mation q(ht, st
v1:t) and then propagate this forwards using the exact dynamics. To do so consider ﬁrst the
|
relation

q(ht+1

st+1, v1:t+1) =
|

=

(cid:88)

st,it
(cid:88)

st,it

q(ht+1, st, it

st+1, v1:t+1)
|

q(ht+1

st, it, st+1, v1:t+1)q(st, it
|

st+1, v1:t+1)
|

(25.3.4)

Wherever possible we now substitute the exact dynamics and evaluate each of the two factors above. The
usefulness of decomposing the update in this way is that the new ﬁltered approximation is of the form of
a Gaussian mixture, where q(ht+1
st+1, v1:t+1) are the weights or
|
mixing proportions of the components. We describe below how to compute these terms explicitly. Equa-
S components which we will collapse back to I
tion(25.3.4) produces a new Gaussian mixture with I
components at the end of the computation.

st, it, st+1, v1:t+1) is Gaussian and q(st, it
|

×

Evaluating q(ht+1

st, it, st+1, v1:t+1)

|

We aim to ﬁnd a ﬁltering recursion for q(ht+1
st, it, st+1, v1:t+1). Since this is conditional on switch states
|
and components, this corresponds to a single LDS forward step which can be evaluated by considering ﬁrst
the joint distribution

q(ht+1, vt+1

|

st, it, st+1, v1:t) =

(cid:90)

ht

p(ht+1, vt+1

ht,(cid:26)(cid:26)st, (cid:19)(cid:19)it, st+1, v1:t)q(ht
|

|

st, it,(cid:24)(cid:24)(cid:24)st+1, v1:t)

(25.3.5)

DRAFT April 24, 2015

515

and subsequently conditioning on vt+1. In the above we use the exact dynamics where possible. To ease
the burden on notation we derive this for ¯ht, ¯vt
0 for all t. The exact forward dynamics is then given by

≡

vt+1 = B(st+1)ht+1 + ηv(st+1),

ht+1 = A(st+1)ht + ηh(st+1),

Given the mixture component index it,

q(ht

|

v1:t, it, st) =

N

(ht f (it, st), F(it, st))

Gaussian Sum Filtering

(25.3.6)

(25.3.7)

we propagate this Gaussian with the exact dynamics equation (25.3.6). Then q(ht+1, vt+1
is a Gaussian with covariance and mean elements

st, it, st+1, v1:t)
|

Σhh = A(st+1)F(it, st)AT(st+1) + Σh(st+1), Σvv = B(st+1)ΣhhBT(st+1) + Σv(st+1)
Σvh = B(st+1)Σhh = ΣT

hv, µv = B(st+1)A(st+1)f (it, st), µh = A(st+1)f (it, st)

(25.3.8)

These results are obtained from integrating the forward dynamics, equations (25.2.1,25.2.2) over ht, using
result(8.3). To ﬁnd q(ht+1
st, it, st+1, v1:t) on vt+1 using
|
the standard Gaussian conditioning formulae, result(8.4), to obtain

st, it, st+1, v1:t+1) we now condition q(ht+1, vt+1

|

q(ht+1

|

st, it, st+1, v1:t+1) =

ht+1 µh|v, Σh|v

(cid:17)

(cid:16)

N

with

µh|v = µh + ΣhvΣ−1

vv (vt+1

µv) ,

−

Σh|v = Σhh

−

ΣhvΣ−1

vv Σvh

where the quantities required are deﬁned in equation (25.3.8).

(25.3.9)

(25.3.10)

Evaluating the mixture weights q(st, it

st+1, v1:t+1)

|

Up to a normalisation constant the mixture weight in equation (25.3.4) can be found from

q(st, it

|

st+1, v1:t+1)

q(vt+1

it, st, st+1, v1:t)q(st+1
|

it, st, v1:t)q(it
|

st, v1:t)q(st
|

|

v1:t)

∝

(25.3.11)

The ﬁrst factor in equation (25.3.11), q(vt+1
st, v1:t) and q(st
as given in equation (25.3.8). The last two factors q(it
|
it, st, v1:t) is found from
ﬁltered iteration. Finally, q(st+1

it, st, st+1, v1:t) is Gaussian with mean µv and covariance Σvv,
v1:t) are given from the previous
|

|

q(st+1

|

it, st, v1:t) =

(cid:26)

|
p(st+1
(cid:104)
p(st+1

|

ht, st)
|
st)

(cid:105)q(ht|it,st,v1:t)

augmented SLDS
standard SLDS

(25.3.12)

In the aSLDS, the term in equation (25.3.12) will generally need to be computed numerically. A sim-
it, st, v1:t).
ple approximation is to evaluate equation (25.3.12) at the mean value of the distribution q(ht
|
To take covariance information into account an alternative would be to draw samples from the Gaussian
q(ht
ht, st) by sampling. Note that this does not
|
equate Gaussian Sum ﬁltering for the augmented SLDS with a sequential sampling procedure, such as
Particle Filtering, section(27.6.2). The sampling here is exact, for which no convergence issues arise.

it, st, v1:t) and thus approximate the average of p(st+1
|

Closing the recursion

We are now in a position to calculate equation (25.3.4). For each setting of the variable st+1, we have a
mixture of I
S Gaussians. To prevent the number of components increasing exponentially with time, we
×
numerically collapse q(ht+1

st+1, v1:t+1) back to I Gaussians to form
|

q(ht+1

st+1, v1:t+1)
|

→

I
(cid:88)

it+1=1

q(ht+1

|

it+1, st+1, v1:t+1)q(it+1

st+1, v1:t+1)

|

(25.3.13)

The numerical collapse then generates the new Gaussian components and corresponding mixture weights.
Any method of choice may be supplied to collapse a mixture to a smaller mixture. A straightforward
approach is to repeatedly merge low-weight components, as explained in section(25.3.4). In this way the
st+1, v1:t+1), it+1
new mixture coeﬃcients q(it+1
1, . . . , I are deﬁned. This completes the description of
|
st+1, v1:t+1) in equation
how to form a recursion for the continuous ﬁltered posterior approximation q(ht+1
|
(25.3.2).

∈

516

DRAFT April 24, 2015

Gaussian Sum Filtering

|

Figure 25.2: Gaussian Sum Filtering. The leftmost column depicts the
v1:t) for two states S = 2
previous Gaussian mixture approximation q(ht, it
(red and blue) and three mixture components I = 3, with the mixture weight
represented by the area of each oval. There are S = 2 diﬀerent linear systems
which take each of the components of the mixture into a new ﬁltered state,
the colour of the arrow indicating which dynamic system is used. After one
time-step each mixture component branches into a further S components so
v1:t+1) contains S2I components
that the joint approximation q(ht+1, st+1
(middle column). To keep the representation computationally tractable the
mixture of Gaussians for each state st+1 is collapsed back to I components.
This means that each coloured set of Gaussians needs to be approximated
by a smaller I component mixture of Gaussians. There are many ways to
achieve this. A naive but computationally eﬃcient approach is to simply
ignore the lowest weight components, as depicted on the right column, see
mix2mix.m.

|

25.3.2 Discrete ﬁltering

A recursion for the switch variable distribution in equation (25.3.2) is

q(st+1

v1:t+1)
|

∝

(cid:88)

it,st

q(st+1, it, st, vt+1, v1:t)

The r.h.s. of the above equation is proportional to

(cid:88)

st,it

q(vt+1

st+1, it, st, v1:t)q(st+1
|

it, st, v1:t)q(it
|

st, v1:t)q(st
|

v1:t)
|

(25.3.14)

(25.3.15)

for which all terms have been computed during the recursion for q(ht+1
st+1, v1:t+1). We therefore now have
|
all the quantities required to compute the Gaussian Sum approximation of the ﬁltering forward pass. A
schematic representation of Gaussian Sum ﬁltering is given in ﬁg(25.2) and the pseudo code is presented in
algorithm(25.1). See also SLDSforward.m.

25.3.3 The likelihood p(v1:T )

The likelihood p(v1:T ) may be found from

p(v1:T ) =

T −1
(cid:89)

t=0

p(vt+1

v1:t)
|

(25.3.16)

where

p(vt+1

v1:t)
|

≈

(cid:88)

it,st,st+1

q(vt+1

it, st, st+1, v1:t)q(st+1
|

it, st, v1:t)q(it
|

st, v1:t)q(st
|

v1:t)
|

In the above expression, all terms have been computed in forming the recursion for the ﬁltered posterior
q(ht+1, st+1

v1:t+1).
|

25.3.4 Collapsing Gaussians

A central part of the above ﬁltering recursion is the collapse of a mixture of Gaussians to a smaller number
of Gaussians. That is, given a mixture of N Gaussians

p(x) =

N
(cid:88)

i=1

pi

N

(x µi, Σi)

(25.3.17)

we wish to collapse this to a smaller K < N mixture of Gaussians. We describe a simple method which
has the advantage of computational eﬃciency, but the disadvantage that no spatial information about the

DRAFT April 24, 2015

517

t+1tGaussian Sum Smoothing

it wt(it, st)

Algorithm 25.1 aSLDS Forward Pass. Approximate the ﬁltered posterior p(st
(cid:80)

v1:t)
|
(ht ft(it, st), Ft(it, st)). Also return the approximate log-likelihood L

≡
≡
S
the number of components in each Gaussian mixture approximation. We require I1 = 1, I2
≤
θ(s) = (cid:8)A(s), B(s), Σh(s), Σv(s), ¯h(s), ¯v(s)(cid:9). The routine LDSFORWARD is found in algorithm(24.1).

αt, p(ht
st, v1:t)
≡
|
log p(v1:T ). It are
It−1.

S, It

N

≤

×

←

for s1

1 to S do
f1(1, s1), F1(1, s1), ˆp
}
{
α1
←
end for

p(s1)ˆp

= LDSFORWARD(0, 0, v1; θ(s1))

for t

2 to T do

←
for st

←
for i

1 to S do

1 to It−1, and s
←
←
µx|y(i, s), Σx|y(i, s), ˆp
}
{
p∗(st
i, s)
|
p(cid:48)(st, i, s)

p(st
wt−1(i, s)p∗(st

ht−1, st−1 = s)
|

≡ (cid:104)
←

end for
Collapse the It−1
p(i, s
st)
|
(cid:80)It
it=1 p(it

×
p(cid:48)(st, i, s)

∝
st, v1:t)p(ht
|
Ft(it, st) and mixture weights wt(it, st)
(cid:80)
Compute αt(st)

i,s p(cid:48)(st, i, s)

∝

end for
normalise αt
L
←
end for

L + log (cid:80)

st,i,s p(cid:48)(st, i, s)

1 to S do
= LDSFORWARD(ft−1(i, s), Ft−1(i, s), vt; θ(st))

(cid:105)p(ht−1|it−1=i,st−1=s,v1:t−1)

i, s)αt−1(s)ˆp

|

S mixture of Gaussians deﬁned by µx|y,Σx|y,

to

a Gaussian with It

components,

p(ht

≈
st, it, v1:t). This deﬁnes the new means ft(it, st), covariances
|

and weights
st, v1:t)
|

p(it

st, v1:t).

≡

|

mixture is used[297]. First we describe how to collapse a mixture to a single Gaussian. This can be achieved
by ﬁnding the mean and covariance of the mixture distribution (25.3.17). These are

µ =

(cid:88)

i

piµi,

Σ =

(cid:88)

(cid:16)

pi

Σi + µiµT
i

i

(cid:17)

−

µµT

(25.3.18)

1 Gaussians with the
To collapse a mixture then to a K-component mixture we may ﬁrst retain the K
largest mixture weights. The remaining N
K + 1 Gaussians are simply merged to a single Gaussian using
the above method. Alternative heuristics such as recursively merging the two Gaussians with the lowest
mixture weights are also reasonable.

−

−

More sophisticated methods which retain some spatial information would clearly be potentially useful. The
method presented in [188] is a suitable approach which considers removing Gaussians which are spatially
similar (and not just low-weight components), thereby retaining a sense of diversity over the possible solu-
tions. In applications with many thousands of timesteps, speed can be a factor in determining which method
of collapsing Gaussians is to be preferred.

25.3.5 Relation to other methods

Gaussian Sum Filtering can be considered a form of ‘analytical particle ﬁltering’, section(27.6.2), in which
instead of point distributions (delta functions) being propagated, Gaussians are propagated. The collapse
operation to a smaller number of Gaussians is analogous to resampling in Particle Filtering. Since a Gaussian
is more expressive than a delta-function, the Gaussian Sum ﬁlter is generally an improved approximation
technique over using point particles. See [17] for a numerical comparison.

25.4 Gaussian Sum Smoothing

Approximating the smoothed posterior p(ht, st
v1:T ) is more involved than ﬁltering and requires additional
|
approximations. For this reason smoothing is more prone to failure since there are more assumptions that
need to be satisﬁed for the approximations to hold. The route we take here is to assume that a Gaussian Sum

518

DRAFT April 24, 2015

Gaussian Sum Smoothing

ﬁltered approximation has been carried out, and then approximate the γ backward pass, analogous to that
of section(23.2.4). By analogy with the RTS smoothing recursion equation (23.2.20), the exact backward
pass for the SLDS reads

p(ht, st

|

v1:T ) =

(cid:90)

(cid:88)

st+1

ht+1

p(ht, st

ht+1, st+1, v1:t)p(ht+1, st+1
|

v1:T )
|

(25.4.1)

|

v1:T )p(ht+1

v1:T ) = p(st+1
|

where p(ht+1, st+1
st+1, v1:T ) is composed of the discrete and continuous com-
|
ponents of the smoothed posterior at the next time step. The recursion runs backwards in time, beginning
with the initialisation p(hT , sT
v1:T ) set by the ﬁltered result (at time t = T , the ﬁltered and smoothed
|
posteriors coincide). Apart from the fact that the number of mixture components will increase at each step,
computing the integral over ht+1 in equation (25.4.1) is problematic since the conditional distribution term
is non-Gaussian in ht+1. For this reason it is more useful to derive an approximate recursion by beginning
with the exact relation

p(ht, st

|

v1:T ) =

(cid:88)

st+1

p(st+1

v1:T )p(ht
|

st, st+1, v1:T )p(st
|

st+1, v1:T )
|

which can be expressed more directly in terms of the SLDS dynamics as

(25.4.2)

p(ht, st

|

v1:T ) =

(cid:88)

st+1

p(st+1

v1:T )
|

p(ht
(cid:104)

ht+1, st, st+1, v1:t,(cid:24)(cid:24)(cid:24)(cid:24)vt+1:T )
|

(cid:105)p(ht+1|st,st+1,v1:T )

p(st

ht+1, st+1, v1:T )
|

× (cid:104)

(cid:105)p(ht+1|st+1,v1:T )

(25.4.3)

v1:T ) from the future timestep.
In forming the recursion we assume access to the distribution p(ht+1, st+1
|
However, we also require the distribution p(ht+1
st, st+1, v1:T ) which is not directly known and needs to be
|
inferred, in itself a computationally challenging task. In the Expectation Correction (EC) approach [17] one
assumes the approximation (see ﬁg(25.3))

p(ht+1

st, st+1, v1:T )
|

≈

p(ht+1

st+1, v1:T )
|

resulting in an approximate recursion for the smoothed posterior,

(25.4.4)

p(ht, st

v1:T )

|

≈

(cid:88)

st+1

p(st+1

v1:T )
|

p(ht
(cid:104)

ht+1, st, st+1, v1:t)
|

(cid:105)ht+1 (cid:104)

p(st

|

ht+1, st+1, v1:T )

(cid:105)ht+1

(25.4.5)

(cid:104)·(cid:105)ht+1 represents averaging with respect to the distribution p(ht+1

st+1, v1:T ). In carrying out the
where
approximate recursion, (25.4.5) we will end up with a mixture of Gaussians that grows at each timestep.
To avoid the exponential explosion problem, we use a ﬁnite mixture approximation, q(ht+1, st+1

|

v1:T ):
|

v1:T )
|

p(ht+1, st+1

st+1, v1:T )q(st+1
|
and plug this into the approximate recursion above. From equation (25.4.5) a recursion for the approximation
is given by

v1:T ) = q(ht+1

q(ht+1, st+1

v1:T )
|

(25.4.6)

≈

|

q(ht, st

v1:T ) =
|

(cid:88)

st+1

q(st+1

v1:T )
|

q(ht
(cid:104)
(cid:124)

ht+1, st, st+1, v1:t)
|

(cid:123)(cid:122)
q(ht|st,st+1,v1:T )

(cid:105)q(ht+1|st+1,v1:T )
(cid:125)

q(st
(cid:104)
(cid:124)

|

ht+1, st+1, v1:t)

(cid:105)q(ht+1|st+1,v1:T )
(cid:125)

(cid:123)(cid:122)
q(st|st+1,v1:T )

As for ﬁltering, wherever possible, we replace approximate terms by their exact counterparts and parame-
terise the posterior using

q(ht+1, st+1

|

v1:T ) = q(ht+1

st+1, v1:T )q(st+1
|

v1:T )
|

(25.4.8)

To reduce the notational burden here we outline the method only for the case of using a single component
approximation in both the forward and backward passes. The extension to using a mixture to approximate
each p(ht+1
st+1, v1:T ) is conceptually straightforward and deferred to section(25.4.4). In the single Gaussian
|
case we assume we have a Gaussian approximation available for

(25.4.7)

q(ht+1

|

st+1, v1:T ) =

N

(ht+1 g(st+1), G(st+1))

DRAFT April 24, 2015

(25.4.9)

519

st−1

ht−1

vt−1

st

ht

vt

st+1

st+2

ht+1

ht+2

vt+1

vt+2

Gaussian Sum Smoothing

st+1, st, v1:T ) by p(ht+1
|

The EC backpass approximates
Figure 25.3:
st+1, v1:T ). The moti-
p(ht+1
|
vation for this is that st inﬂuences ht+1 only indirectly
through ht. However, ht will most likely be heavily in-
ﬂuenced by v1:t, so that not knowing the state of st
is likely to be of secondary importance. The green
shaded node is the variable we wish to ﬁnd the pos-
terior for. The values of the blue shaded nodes are
known, and the red shaded node indicates a known
variable which is assumed unknown in forming the ap-
proximation.

25.4.1 Continuous smoothing

For given st, st+1, an RTS style recursion for the smoothed continuous distribution is obtained from equation
(25.4.7), giving

q(ht

|

st, st+1, v1:T ) =

(cid:90)

ht+1

p(ht

|

ht+1, st, st+1, v1:t)q(ht+1

st+1, v1:T )
|

(25.4.10)

To compute equation (25.4.10) we then perform a single update of the LDS backward recursion, sec-
tion(24.4.2).

25.4.2 Discrete smoothing

The second average in equation (25.4.7) corresponds to a recursion for the discrete variable and is given by

q(st
(cid:104)

ht+1, st+1, v1:t)
|
The average of q(st
simple approach is to approximate the average by evaluation at the mean1

ht+1, st+1, v1:t) with respect to q(ht+1

(cid:105)q(ht+1|st+1,v1:T ) ≡

st+1, v1:T ).
|

q(st

|

st+1, v1:T ) cannot be achieved in closed form. A
|

(25.4.11)

q(st
(cid:104)
where

ht+1, st+1v1:t)
|
ht+1
(cid:104)

st+1, v1:T
|

(cid:105)

(cid:105)q(ht+1|st+1,v1:T ) ≈

q(st

(cid:12)
ht+1, st+1, v1:t)
(cid:12)ht+1=(cid:104)ht+1|st+1,v1:T (cid:105)
|

(25.4.12)

is the mean of ht+1 with respect to q(ht+1

st+1, v1:T ).
|

Replacing ht+1 by its mean gives the approximation

q(st
(cid:104)

ht+1, st+1, v1:t)
|

(cid:105)q(ht+1|st+1,v1:T ) ≈

1
Z

where

e− 1

2 zT

t+1(st,st+1)Σ−1(st,st+1|v1:t)zt+1(st,st+1)

(cid:112)

det (Σ(st, st+1

v1:t))
|

q(st

st+1, v1:t) (25.4.13)
|

|

≡ (cid:104)

ht+1

st+1, v1:T

zt+1(st, st+1)

st, st+1, v1:t
|
and Z ensures normalisation over st. Σ(st, st+1
v1:t) is the ﬁltered covariance of ht+1 given st, st+1 and the
|
observations v1:t, which may be taken from Σhh in equation (25.3.8). Approximations which take covariance
information into account can also be considered, although the above simple (and fast) method may suﬃce
in practice [17, 207].

(25.4.14)

ht+1

(cid:105) − (cid:104)

(cid:105)

25.4.3 Collapsing the mixture

From section(25.4.1) and section(25.4.2) we now have all the terms in equation (25.4.8) to compute the
approximation to equation (25.4.7). Due to the summation over st+1 in equation (25.4.7), the number of
mixture components is multiplied by S at each iteration. To prevent an exponential explosion of components,
the mixture equation (25.4.7) is then collapsed to a single Gaussian

q(ht, st

v1:T )
|

→

q(ht

st, v1:T )q(st
|

v1:T )
|

The collapse to a mixture is discussed in section(25.4.4).

1In general this approximation has the form (cid:104)f (x)(cid:105) ≈ f ((cid:104)x(cid:105)).

(25.4.15)

520

DRAFT April 24, 2015

Gaussian Sum Smoothing

Algorithm 25.2 aSLDS: EC Backward Pass.
(cid:80)Jt

Approximates p(st

v1:T ) and p(ht
|

jt=1 ut(jt, st)

(gt(jt, st), Gt(jt, st)) using a mixture of Gaussians. JT = IT , Jt
routine needs the results from algorithm(25.1). The routine LDSBACKWARD is found in algorithm(24.2).

N

≤

×

It

S

st, v1:T )
≡
Jt+1. This

|
×

FT , gT

GT
for t

←

←
for s

T

−
←

fT , uT
←
1 to 1 do
1 to S, s(cid:48)

wT

←

end for
for st

1 to S do

1 to S, i

1 to It, j(cid:48)

1 to Jt+1 do

←

(µ, Σ)(i, s, j(cid:48), s(cid:48)) = LDSBACKWARD(gt+1(j(cid:48), s(cid:48)), Gt+1(j(cid:48), s(cid:48)), ft(i, s), Ft(i, s), θ(s(cid:48)))
p(it, st
p(i, s, j(cid:48), s(cid:48)

←
ht+1, st+1 = s(cid:48), jt+1 = j(cid:48), v1:t)
p(st = s, it = i
|
v1:T )ut+1(j(cid:48), s(cid:48))p(it, st
|

jt+1, st+1, v1:T ) =
|

(cid:104)
p(st+1 = s(cid:48)

jt+1, st+1, v1:T )

v1:T )
|

←

←

|

(cid:105)p(ht+1|st+1=s(cid:48),jt+1=j(cid:48),v1:T )

←
Collapse the mixture deﬁned by weights p(it = i, st+1 = s(cid:48), jt+1 = j(cid:48)
∝
p(i, st, j(cid:48), s(cid:48)
v1:T ), means µ(it, st, jt+1, st+1) and covariances Σ(it, st, jt+1, st+1) to a mix-
|
ture with Jt components. This deﬁnes the new means gt(jt, st), covariances Gt(jt, st) and
mixture weights ut(jt, st).
v1:T )
p(st
|
end for

it,j(cid:48),s(cid:48) p(it, st, j(cid:48), s(cid:48)

v1:T )
|

st, v1T )

(cid:80)

←

|

end for

25.4.4 Using mixtures in smoothing

The extension to the mixture case is straightforward based on the representation

p(ht

st, v1:T )
|

≈

J
(cid:88)

jt=1

q(jt

st, v1:T )q(ht
|

st, jt, v1:T ).
|

Analogously to the case with a single component,

(25.4.16)

q(ht, st

v1:T ) =
|

(cid:88)

it,jt+1,st+1

p(st+1

v1:T )p(jt+1
|

|

st+1, v1:T )q(ht

jt+1, st+1, it, st, v1:T )
|

q(it, st

× (cid:104)

|

ht+1, jt+1, st+1, v1:t)

(cid:105)q(ht+1|jt+1,st+1,v1:T )

(25.4.17)

The average in the last line of the above equation can be tackled using the same techniques as outlined in
the single Gaussian case. To approximate q(ht
jt+1, st+1, it, st, v1:T ) we consider this as the marginal of the
|
joint distribution

q(ht, ht+1

it, st, jt+1, st+1, v1:T ) = q(ht
|

ht+1, it, st, jt+1, st+1, v1:t)q(ht+1
|

|

it, st, jt+1, st+1, v1:T ) (25.4.18)

As in the case of a single mixture, the problematic term is q(ht+1
equation (25.4.4), we make the assumption

it, st, jt+1, st+1, v1:T ). Analogously to
|

q(ht+1

it, st, jt+1, st+1, v1:T )
|

≈

q(ht+1

|

jt+1, st+1, v1:T )

meaning that information about the current switch state st, it is ignored. We can then form

p(ht

|

st, v1:T ) =

(cid:88)

it,jt+1,st+1

p(it, jt+1, st+1

st, v1:T )p(ht

|

|

it, st, jt+1, st+1, v1:T )

This mixture can then be collapsed to a smaller mixture using any method of choice, to give

p(ht

|

st, v1:T )

(cid:88)

≈

jt

q(jt

st, v1:T )q(ht
|

jt, st, v1:T )
|

(25.4.19)

(25.4.20)

(25.4.21)

The resulting procedure is sketched in algorithm(25.2), including using mixtures in both the forward and
backward passes.

DRAFT April 24, 2015

521

25.4.5 Relation to other methods

A classical smoothing approximation for the SLDS is generalised pseudo Bayes
In GPB one starts from the exact recursion

(GPB) [12, 173, 172].

Gaussian Sum Smoothing

p(st

v1:T ) =
|

(cid:88)

st+1

p(st, st+1

v1:T ) =

|

(cid:88)

st+1

p(st

st+1, v1:T )p(st+1
|

|

v1:T )

The quantity p(st

st+1, v1:T ) is diﬃcult to obtain and GPB makes the approximation
|

p(st

st+1, v1:T )
|

≈

p(st

st+1, v1:t)
|

Plugging this into equation (25.4.22) we have

p(st

v1:T )
|

≈

=

(cid:88)

st+1

(cid:88)

st+1

p(st

st+1, v1:t)p(st+1
|

v1:T )
|

p(st+1
st p(st+1

st)p(st
|

v1:t)
|

st)p(st

(cid:80)

v1:t)
|

|

p(st+1

v1:T )
|

(25.4.22)

(25.4.23)

(25.4.24)

(25.4.25)

v1:T ). Computing the smoothed recursion
The recursion is initialised with the approximate ﬁltered p(sT
|
for the switch states in GPB is then equivalent to running the RTS backward pass on a hidden Markov
model, independently of the backward recursion for the continuous variables. The only information the
v1:t)
GPB method uses to form the smoothed distribution p(st
|
st). This approximation drops information from the future since
is the Markov switch transition p(st+1
|
information passed via the continuous variables is not taken into account.
In contrast to GPB, the EC
Gaussian smoothing technique preserves future information passing through the continuous variables. GPB
forms an approximation for p(ht
st+1, v1:T ) is replaced
by q(st

st, v1:T ) by using the recursion (25.4.8) where q(st
|

st+1, v1:t). In SLDSbackward.m one may choose to use either EC or GBP.

v1:T ) from the ﬁltered distribution p(st
|

|

|

Example 25.1 (Traﬃc Flow). A illustration of modelling and inference with a SLDS is to consider a simple
network of traﬃc ﬂow, ﬁg(25.4). Here there are 4 junctions a, b, c, d and traﬃc ﬂows along the roads in the
direction indicated. Traﬃc ﬂows into the junction at a and then goes via diﬀerent routes to d. Flow out of
a junction must match the ﬂow into a junction (up to noise). There are traﬃc light switches at junctions a
and b which, depending on their state, route traﬃc diﬀerently along the roads. Using φ to denote the clean
(noise free) ﬂow, we model the ﬂows using the switching linear system:

φa(t)
φa→d(t)
φa→b(t)
φb→d(t)
φb→c(t)
φc→d(t)






=






φa(t
−
φa(t
−
φa(t
−
φa→b(t
φa→b(t
φb→c(t

1)
1) (0.75
×
1) (0.25
×
1)0.5
×
1) (0.5
1)

−
−
−

×

I [sa(t) = 1] + 1
×
I [sa(t) = 1] + 1
×
I [sb(t) = 1]
I [sb(t) = 1] + 1

I [sb(t) = 2])

×

I [sa(t) = 2])
I [sa(t) = 3])

(25.4.26)

By identifying the ﬂows at time t with a 6 dimensional vector hidden variable ht, we can write the above
ﬂow equations as

ht = A(st)ht−1 + ηh
t

(25.4.27)

for a set of suitably deﬁned matrices A(s) indexed by the switch variable s = sa
2 = 6
states. We additionally include noise terms to model cars parking or de-parking during a single timestep.
The covariance Σh is diagonal with a larger variance at the inﬂow point a to model that the total volume
of traﬃc entering the system can vary.

×

(cid:78) sb, which takes 3

Noisy measurements of the ﬂow into the network are taken at a

v1,t = φa(t) + ηv

1(t)

522

(25.4.28)

DRAFT April 24, 2015

Gaussian Sum Smoothing

a

d

b

c

Figure 25.4: A representation of the traﬃc ﬂow between junctions at a,b,c,d, with
b carry 0.75 and 0.25 of the ﬂow out
d and a
traﬃc lights at a and b. If sa = 1 a
d; for sa = 3, all the
of a respectively. If sa = 2 all the ﬂow from a goes through a
ﬂow goes through a
d
and b

b. For sb = 1 the ﬂow out of b is split equally between b

c. For sb = 2 all ﬂow out of b goes along b

→

→

→

→

→

c.

→

→

Figure 25.5: Time evolution of the traﬃc ﬂow measured at
two points in the network. Sensors measure the total ﬂow
into the network (upper panel) φa(t)and the total ﬂow out
of the network (lower panel), φd(t) = φa→d(t) + φb→d(t) +
φc→d(t). The total inﬂow at a undergoes a random walk.
Note that the ﬂow measured at d can momentarily drop
c in two
to zero if all traﬃc is routed through a
consecutive time steps.

→

→

b

along with a noisy measurement of the total ﬂow out of the system at d,

v2,t = φa→d(t) + φb→d(t) + φc→d(t) + ηv

2(t)

(25.4.29)

The observation model can be represented by vt = Bht + ηv
The switch variables follow a simple Markov transition p(st
the same state in preference to jumping to another state. See demoSLDStraffic.m for details.

6 projection matrix B.
t using a constant 2
st−1) which biases the switches to remain in
|

×

Given the above system and a prior which initialises all ﬂow at a, we draw samples from the model using
forward (ancestral) sampling which form the observations v1:100, ﬁg(25.5). Using only the observations
and the known model structure we then attempt to infer the latent switch variables and traﬃc ﬂows using
Gaussian Sum ﬁltering and smoothing (EC method) with 2 mixture components per switch state, ﬁg(25.6).

We note that a naive HMM approximation based on discretising each continuous ﬂow into 20 bins would
206 or 384 million states. Even for modest size problems, a naive approximation based on
contain 2
discretisation is therefore impractical.

×

×

3

Example 25.2 (Following the price trend). The following is a simple model of the price trend of a stock,
which assumes that the price tends to continue going up (or down) for a while before it reverses direction:

h1,t = h1,t−1 + h2,t−1 + ηh
h2(t) = I [st = 1] h2,t−1 + ηh

1 (st)
2 (st)

vt = h1,t + ηv(st)

(25.4.30)

(25.4.31)

(25.4.32)

here h1 represents the ‘clean’ price and h2 the direction. There is only a single observation variable at each
.
time, which is the clean price plus a small amount of noise. There are two switch states, dom(st) =
}
When st = 1, the model functions normally, with the direction being equal to the previous direction plus
a small amount of noise ηh
2 (st = 1). When st = 2 however, the direction is sampled from a Gaussian with
a large variance. The transition p(st
st−1) is set so that normal dynamics is more likely, and when st = 2
it is likely to go back to normal dynamics the next timestep. Full details are in SLDSpricemodel.mat.
In ﬁg(25.7) we plot some samples from the model and also smoothed inference of the switch distribution,
showing how we can analyse the series to infer the points of likely change in the stock price direction. See
also exercise(25.1).

1, 2
{

|

DRAFT April 24, 2015

523

0204060801000204002040608010002040Reset Models

(a)

(b)

(c)

Figure 25.6: Given the observations from ﬁg(25.5) we infer the ﬂows and switch states of all the latent
variables. (a): The correct latent ﬂows through time along with the switch variable state used to generate
the data. The colours corresponds to the ﬂows at the corresponding coloured edges/nodes in ﬁg(25.4). (b):
Filtered ﬂows based on a I = 2 Gaussian Sum forward pass approximation. Plotted are the 6 components
v1:t),p(sb
v1:t)
v1:t
of the vector
t|
|
plotted below. (c): Smoothed ﬂows
v1:T ) using a
|
Gaussian Sum smoothing approximation (EC with J = 1).

with the posterior distribution of the sa and sb traﬃc light states p(sa
t |
and corresponding smoothed switch states p(st

ht
(cid:104)

v1:T

ht

(cid:104)

(cid:105)

(cid:105)

|

Figure 25.7: The top panel is a time series of ‘prices’. The prices
tend to keep going up or down with infrequent changes in the
direction. Based on ﬁtting a simple SLDS model to capture this
kind of behaviour, the probability of a signiﬁcant change in the
price direction is given in the panel below based on the smoothed
v1:T ).
distribution p(st = 2
|

25.5 Reset Models

Reset models are special switching models in which the switch state isolates the present from the past,
resetting the position of the latent dynamics (these are also known as changepoint models). Whilst these
models are rather general, it can be helpful to consider a speciﬁc model, and here we consider the SLDS
changepoint model with two states. We use the state st = 0 to denote that the LDS continues with the
standard dynamics. With st = 1, however, the continuous dynamics is reset to a prior:

p(ht

ht−1, st) =
|

(cid:26) p0(ht
ht−1) st = 0
|
p1(ht)
st = 1

where

p0(ht

ht−1) =
|

N

(cid:0)ht Aht−1 + µ0, Σ0(cid:1) ,

p1(ht) =

N

(cid:0)ht µ1, Σ1(cid:1)

Similarly we write

p(vt

ht, st) =
|

(cid:26) p0(vt
p1(vt

ht) st = 0
|
ht) st = 1
|

(25.5.1)

(25.5.2)

(25.5.3)

st−1), see ﬁg(25.8).
For simplicity we assume the switch dynamics are ﬁrst order Markov with transition p(st
|
Under this model the dynamics follows a standard LDS, but when st = 1, ht is reset to a value drawn from

524

DRAFT April 24, 2015

020406080100010200204060801000102002040608010001020020406080100010200204060801000102002040608010001020204060801001232040608010012020406080100010200204060801000102002040608010001020020406080100010200204060801000102002040608010001020204060801001232040608010012020406080100010200204060801000102002040608010001020020406080100010200204060801000102002040608010001020204060801001232040608010012020406080100120140160180200−40−30−20−1001002040608010012014016018020000.20.40.60.81Reset Models

s1

s2

s3

s4

h1

v1

h2

v2

h3

v3

h4

v4

Figure 25.8: The independence structure of a reset model.
Square nodes st denote the binary reset variables and ht
the continuous state. The ht are continuous variables, and
vt continuous observations. If the dynamics resets, st = 1,
the dependence of the continuous ht on the past is cut.

a Gaussian distribution, independent of the past. Such models are of interest in prediction where the time-
series is following a trend but suddenly changes and the past is forgotten. Whilst this may not seem like a
big change to the model, this model is computationally more tractable, exact ﬁltered inference scaling with
O (cid:0)T 2(cid:1), compared to O (cid:0)T 2T (cid:1) in the general two-state SLDS. To see this, consider the ﬁltering recursion

α(ht, st)

(cid:90)

(cid:88)

∝

ht−1

st−1

p(vt

ht, st)p(ht
|

ht−1, st)p(st
|

st−1)α(ht−1, st−1)
|

We now consider the two cases

α(ht, st = 0)

(cid:90)

(cid:88)

∝

ht−1

st−1

p0(vt

ht)p0(ht
|

st−1)α(ht−1, st−1)
ht−1)p(st = 0
|
|

α(ht, st = 1)

p1(vt

ht)p1(ht)
|

(cid:90)

(cid:88)

ht−1

st−1

st−1)α(ht−1, st−1)
p(st = 1
|

p1(vt

ht)p1(ht)
|

(cid:88)

st−1

p(st = 1
|

st−1)α(st−1)

∝

∝

(25.5.4)

(25.5.5)

(25.5.6)

v1:t) is not a mixture model in ht, but contains only a single
Equation(25.5.6) shows that p(ht, st = 1
|
component proportional to p1(vt

ht)p1(ht). If we use this information in equation (25.5.5) we have
|

α(ht, st = 0)

(cid:90)

∝

ht−1

p0(vt

|

ht)p0(ht

st−1 = 0)α(ht−1, st−1 = 0)
ht−1)p(st = 0
|
|

(cid:90)

+

ht−1

p0(vt

ht)p0(ht
|

|

ht−1)p(st = 0

st−1 = 1)α(ht−1, st−1 = 1)
|

(25.5.7)

Assuming α(ht−1, st−1 = 0) is a mixture distribution with K components, then α(ht, st = 0) will be a mixture
with K + 1 components. In general, therefore, α(ht, st = 0) will contain T components and α(ht, st = 1)
a single component. As opposed to the full SLDS case, the number of components therefore grows only
linearly with time, as opposed to exponentially. This means that the computational eﬀort to perform exact
ﬁltering scales as O (cid:0)T 2(cid:1). Smoothing can be achieved using the α
β approach, see exercise(25.3). Despite
−
this reduction in complexity over the SLDS, for long timeseries T
1, ﬁltering and smoothing for these
reset models can still be computationally expensive. See [51] for approximations based on retaining only a
limited number of mixture components, reducing the complexity to linear in T .

(cid:29)

Run-length formalism

One may also describe reset models using a ‘run-length’ formalism deﬁning at each time t a latent variable
rt which describes the length of the current segment[3]. If there is a change, the run-length variable is reset
to zero, otherwise it is increased by 1:

p(rt

rt−1) =
|

(cid:26) Pcp
1

−

rt = 0
rt = rt−1 + 1

Pcp

where Pcp is the probability of a reset (or ‘changepoint’). The joint distribution is given by

p(v1:T , r1:T ) =

(cid:89)

t

p(rt

|

rt−1)p(vt

v1:t−1, rt),
|

p(vt

v1:t−1, rt) = p(vt
|

vt−rt:t−1)
|

DRAFT April 24, 2015

(25.5.8)

(25.5.9)

525

Reset Models

with the understanding that if rt = 0 then p(vt
vt−rt:t−1) = p(vt). The graphical model of this distribution
|
is awkward to draw since the number of links depends on the run-length rt. Predictions can be made using

p(vt+1

v1:t) =
|

(cid:88)

rt

p(vt+1

vt−rt:t)p(rt
|

|

v1:t)

(25.5.10)

where the ﬁltered ‘run-length’ p(rt

v1:t) is given by the forward recursion:
|

p(rt, v1:t) =

=

=

(cid:88)

rt−1
(cid:88)

rt−1
(cid:88)

rt−1

p(rt, rt−1, v1:t−1, vt) =

(cid:88)

p(rt, vt

rt−1, v1:t−1)p(rt−1, v1:t−1)

p(vt

rt,(cid:24)(cid:24)(cid:24)rt−1, v1:t−1)p(rt
|

|

|

rt−1
rt−1,(cid:24)(cid:24)(cid:24)v1:t−1)p(rt−1, v1:t−1)

p(rt

rt−1)p(vt
|

|

vt−rt:t−1)p(rt−1, v1:t−1)

which shows that ﬁltered inference scales with O (cid:0)T 2(cid:1).

25.5.1 A Poisson reset model

The changepoint structure is not limited to conditionally Gaussian cases only. To illustrate this, we consider
the following model2: At each time t, we observe a count yt which we assume is Poisson distributed with an
unknown positive intensity h. The intensity is constant, but at certain unknown times t, it jumps to a new
value. The indicator variable ct denotes whether time t is such a changepoint or not. Mathematically, the
model is:

p(h0) =
p(ct) =

G

(h0; a0, b0)
(ct; π)

BE

p(ht

|

ht−1, ct) = I [ct = 0] δ(ht, ht−1) + I [ct = 1]
p(vt

(vt; ht)

ht) =
|

PO

(ht; ν, b)

G

(25.5.11)

(25.5.12)

(25.5.13)

(25.5.14)

The symbols

,

G

BE

and

PO

denote the Gamma, Bernoulli and the Poisson distributions respectively:

(h; a, b) = exp ((a

1) log h

bh

log Γ(a) + a log b)

G

BE

PO

−

(c; π) = exp (c log π + (1

(v; h) = exp (v log h

h

−

−

−

−
c) log(1

−

π))

−

log Γ(v + 1))

(25.5.15)

(25.5.16)

(25.5.17)

Given observed counts v1:T , the task is to ﬁnd the posterior probability of a change and the associated
intensity levels for each region between two consecutive changepoints. Plugging the above deﬁnitions in the
generic updates equation (25.5.5) and equation (25.5.6), we see that α(ht, ct = 0) is a Gamma potential,
and that α(gt, ct = 1) is a mixture of Gamma potentials, where a Gamma potential is deﬁned as

φ(h) = el

(h; a, b)

G

(25.5.18)

via the triple (a, b, l). For the corrector update step we need to calculate the product of a Poisson term with
the observation model p(vt
(vt; ht). A useful property of the Poisson distribution is that, given the
ht) =
|
observation, the latent variable is Gamma distributed:

PO

(v; h) = v log h

h

log Γ(v + 1)

PO

= (v + 1

−

−
1) log h

−
(h; v + 1, 1)

=

G

h

−

−

log Γ(v + 1)

(25.5.19)

(25.5.20)

(25.5.21)

Hence, the update equation requires multiplication of two Gamma potentials. A nice property of the Gamma
density is that the product of two Gamma densities is also a Gamma potential:

(a1, b1, l1)

×

(a2, b2, l2) = (a1 + a2

−

2This example is due to Taylan Cemgil.

1, b1 + b2, l1 + l2 + g(a1, b1, a2, b2))

(25.5.22)

526

DRAFT April 24, 2015

Reset Models

where

g(a1, b1, a2, b2)

log

≡

Γ(a1 + a2

−
Γ(a1)Γ(a2)

1)

+ log(b1 + b2) + a1 log(b1/(b1 + b2)) + a2 log(b2/(b1 + b2)) (25.5.23)

The α recursions for this reset model are therefore closed in the space of a mixture of Gamma potentials,
with an additional Gamma potential in the mixture at each timestep. A similar approach can be used to
form the smoothing recursions, see exercise(25.3).

Example 25.3 (Coal mining disasters). We illustrate the Poisson reset model on the coal mining disaster
dataset [157]. The data set consists of the number of deadly coal-mining disasters in England per year over
a time span of 112 years from 1851 to 1962. It is widely agreed in the statistical literature that a change in
the intensity (the expected value of the number of disasters) occurs around the year 1890, after new health
y1:T ) along with the ﬁltering
and safety regulations were introduced. In ﬁg(25.9) we show the marginals p(ht
density. Note that we are not constraining the number of changepoints and in principle allow any number.
The smoothed density suggests a sharp decrease around t = 1890.

|

Estimation of change points.

(Top)
Figure 25.9:
coal mining disaster dataset. (Middle) Filtered esti-
mate of the marginal intensity p(ht
v1:t) and (Bottom)
|
Here, darker color
v1:T ).
smoothed estimate p(ht
|
means higher probability.

25.5.2 Reset-HMM-LDS

The reset model deﬁned by equations (25.5.1,25.5.3) above is useful in many applications, but is limited
since only a single dynamical model is considered. An extension is to consider a set of available dynamical
models, indexed by st
, with a reset that cuts dependency of the continuous variable on the
past[100, 59]:

1, . . . , S

∈ {

}

p(ht

ht−1, st, ct) =
|

(cid:26) p0(ht
p1(ht

ht−1, st)
|
st)
|

ct = 0
ct = 1

(25.5.24)

The states st follow a Markovian dynamics p(st
changes, otherwise, no reset occurs:

st−1, ct−1), see ﬁg(25.10). A reset occurs if the state st
|

st, st−1) = I [st

= st−1]

p(ct = 1
|

(25.5.25)
The computational complexity of ﬁltering for this model is O (cid:0)S2T 2(cid:1) which can be understood by analogy
with the reset α recursions, equations(25.5.5,25.5.6) on replacing ht by (ht, st). To see this we consider the
ﬁltering recursion for the two cases

α(ht, st, ct = 0) =

(cid:90)

(cid:88)

ht−1

st−1,ct−1

p0(vt

ht, st)p0(ht
|

ht−1, st)p(st
|

st, st−1)α(ht−1, ct−1)
st−1, ct−1)p(ct = 0
|
|

(25.5.26)

α(ht, st, ct = 1) =

(cid:90)

(cid:88)

p1(vt

ht−1

= p1(vt

st−1,ct−1
ht, st)p1(ht
|

ht, st)p1(ht
|

st)p(st
|

st, st−1)α(ht−1, st−1, ct−1)
st−1, ct)p(ct = 1
|
|

st)
|

(cid:88)

st−1,ct−1

st, st−1)p(st
p(ct = 1
|

st−1, ct−1)α(st−1, ct−1)
|

(25.5.27)

DRAFT April 24, 2015

527

186018701880189019001910192019301940195019600246# of accidentsfiltered intensity18601870188018901900191019201930194019501960246smoothed intensityyear18601870188018901900191019201930194019501960246(cid:54)
c1

s1

h1

v1

c2

s2

h2

v2

c3

s3

h3

v3

c4

s4

h4

v4

Exercises

Figure 25.10: The independence structure of a reset-HMM-
LDS model. Square nodes ct
denote reset vari-
ables; ht are continuous latent variables, and vt continuous
determines
observations. The discrete state st
which Linear Dynamical system from a ﬁnite set of Linear
Dynamical systems is operational at time t.

1, . . . , S

0, 1
}

∈ {

∈ {

}

From equation (25.5.27) we see that α(ht, st, ct = 1) contains only a single component proportional to
p1(vt
st). This is therefore exactly analogous to the standard reset model, except that we
|
need now to index a set of messages with st, therefore each message taking O (S) steps to compute. The
computational eﬀort to perform exact ﬁltering scales as O (cid:0)S2T 2(cid:1).

ht, st)p1(ht
|

25.6 Summary

•

•

•

•

The switching linear dynamical system is a marriage of a discrete state HMM with the continuous latent
state linear dynamical system.
It is able to model discrete jumps in an underlying continuous process,
ﬁnding application in a large variety of domains from ﬁnance to speech processing.

The classical
requires an exponential amount of space.

inference problems in the SLDS are formally intractable since representing the messages

Many approximation methods have been developed for the SLDS and in this chapter we described a robust
deterministic method based on a mixture of Gaussians representation.

Reset models are such that the continuous variable forgets the past when reset. Unlike the SLDS such
models are much more amenable to exact inference. Extensions include the reset-HMM which allows for a
set of discrete and continuous states in which a special discrete state resets the continuous states.

25.7 Code

SLDSforward.m: SLDS forward
SLDSbackward.m: SLDS backward (Expectation Correction)
mix2mix.m: Collapse a mixture of Gaussians to a smaller mixture of Gaussians
SLDSmargGauss.m: Marginalise an SLDS Gaussian mixture
logeps.m: Logarithm with oﬀset to deal with log(0)
demoSLDStraffic.m: Demo of traﬃc ﬂow using a switching linear dynamical system

25.8 Exercises

Exercise 25.1. Consider the setup described in example(25.2), for which the full SLDS model is given in
SLDSpricemodel.mat, following the notation used in demoSLDStraffic.m. Given the data in the vector v
your task is to ﬁt a prediction model to the data. To do so, approximate the ﬁltered distribution p(ht, st
v1:t)
|
using a mixture of I = 2 components. The prediction of the mean price at the next day is then

vpred
t+1 =

h1,t + h2,t
(cid:104)

(cid:105)p(ht|v1:t)

528

(25.8.1)

DRAFT April 24, 2015

Exercises

Figure 25.11: Data from an intermittent mean-reverting process. See exercise(25.2).

where p(ht

v1:t) = (cid:80)
|

st p(ht, st

v1:t).
|

1. Compute the mean prediction error

mean_abs_pred_error=mean(abs(vpred(2:200)-v(2:200)))

2. Compute the mean naive prediction error

mean_abs_pred_error_naive=mean(abs(v(1:199)-v(2:200)))
which corresponds to saying that tomorrow’s price will be the same as today’s.

Hint: you might ﬁnd SLDSmargGauss.m of interest.

Exercise 25.2. The data in ﬁg(25.11) are observed prices from an intermittent mean-reverting process,
contained in meanrev.mat. There are two states S = 2. There is a true (latent) price pt and an observed
price vt (which is plotted). When s = 1, the true underlying price reverts back towards the mean m = 10
with rate r = 0.9. Otherwise the true price follows a random walk:

(cid:26) r(pt−1

−
pt−1 + ηp
t

m) + m + ηp
t

st = 1
st = 2

pt =

where

(cid:26)

ηp
t ∼

N
N

(ηp
(ηp

t 0, 0.0001) st = 1
st = 2
t 0, 0.01)

The observed price vt is related to the unknown price pt by

vt

∼ N

(vt pt, 0.001)

(25.8.2)

(25.8.3)

(25.8.4)

It is known that 95% of the time st+1 is in the same state as at time t > 1 and that at time t = 1 either
state of s1 is equally likely. Also at t = 1, p1
(p1 m, 0.1). Based on this information, and using
Gaussian Sum ﬁltering with I = 2 components (use SLDSforward.m), what is the probability at time t = 280
that the dynamics is following a random walk, p(s280 = 2
v1:280)? Repeat this computation for smoothing
|
v1:400) based on using Expectation Correction with I = J = 2 components.
p(s280 = 2
|

∼ N

Exercise 25.3. We here derive a smoothing recursion based on the α
described in section(25.5). Smoothing can be achieved using

−

β approach for the reset-LDS

p(ht, st

v1:T )

|

∝

v1:t)
p(ht, st
|
(cid:123)(cid:122)
(cid:125)
(cid:124)
α(ht,st)

ht, st)
p(vt+1:T
|
(cid:123)(cid:122)
(cid:125)
(cid:124)
β(ht,st)

From the formal β recursion, we have

β(ht−1, st−1) =

(cid:90)

(cid:88)

st

ht

p(vt

ht, st)p(ht
|

|

ht−1, st)p(st

st−1)β(ht, st)
|

1. Show that the rhs of equation (25.8.6) can be written as

st−1)
p(st = 0
|

(cid:124)

(cid:90)

ht

p0(vt

ht)p0(ht
|

|

(cid:123)(cid:122)
β0(ht−1,st−1)

ht−1)β(ht, st = 0)

st−1)
+ p(st = 1
|

(cid:125)

(cid:124)

(cid:90)

ht

DRAFT April 24, 2015

(25.8.5)

(25.8.6)

p1(vt

ht)p1(ht)β(ht, st = 1)

|
(cid:123)(cid:122)
β1(st−1)

(cid:125)

(25.8.7)

529

0501001502002503003504009.51010.5112. Writing

β(ht, st) = β0(ht, st) + β1(st)

derive then the recursions

Exercises

(25.8.8)

β0(ht−1, st−1) = p(st = 0
st−1)
|

(cid:90)

ht

p0(vt

|

ht)p0(ht

ht−1) (cid:2)β0(ht, st = 0) + β1(st = 0)(cid:3)
|

(25.8.9)

and

β1(st−1) = p(st = 1
st−1)
|

(cid:90)

ht

p1(vt

|

ht)p1(ht) (cid:2)β0(ht, st = 1) + β1(st = 1)(cid:3)

(25.8.10)

The β1 contribution is simply a scalar, so that its complexity of representation is ﬁxed. According to the
above β0 recursion, we include an additional component in the representation of β0 with each timestep that
we go backwards in time. Thus the number of components to represent β0(ht, st) will be O (T
t), since
at time T , we may deﬁne β(hT , st) = 1. This means that the term p(ht, st, v1:T ) = α(ht, st)β(ht, st) will
t)) components. To form a complete smoothing pass over all time therefore takes O (cid:0)T 3(cid:1)
contain O (t(T
time.

−

−

530

DRAFT April 24, 2015

CHAPTER 26

Distributed Computation

In this chapter we discuss from a probabilistic perspective models that are loosely based on a crude under-
standing of neural systems in biology. This is a fascinating area and we show how patterns can be stored
and recalled by reference to our standard tools from learning probabilistic models. We also discuss a general
model that enables us to consider non-linear latent continuous dynamics whilst retaining computational
tractability.

26.1 Introduction

How natural organisms process information is a fascinating subject and one of the grand challenges of science.
Whilst this subject is still in its early stages, loosely speaking, there are some generic properties that most
such systems are believed to possess: patterns are stored in a set of neurons; recall of patterns is robust to
noise; neural activity is essentially binary; information processing is distributed and highly modular. In this
chapter we discuss some of the classical toy models that have been developed as a test bed for analysing
such properties[65, 80, 68, 144].

26.2 Stochastic Hopﬁeld Networks

Hopﬁeld networks are models of biological memory in which a pattern is represented by the activity of a
set of V interconnected neurons. The term ‘network’ here refers to the set of neurons, see ﬁg(26.1), and not
the belief network representation of distribution of neural states unrolled through time, ﬁg(26.2). At time
t neuron i ﬁres vi(t) = +1 or is quiescent vi(t) =
1 (not ﬁring) depending on the states of the neurons at
the preceding time t

−
1. Explicitly, neuron i ﬁres depending on the potential

−

ai(t)

bi +

≡

V
(cid:88)

j=1

wijvj(t)

(26.2.1)

where wij characterizes the eﬃcacy with which neuron j transmits a binary signal to neuron i. The bias
bi relates to the neuron’s predisposition to ﬁring. Writing the state of the network at time t as v(t)
(v1(t), . . . , vV (t))T, the probability that neuron i ﬁres at time t + 1 is modelled as

≡

p(vi(t + 1) = 1
|

v(t)) = σβ (ai(t))

(26.2.2)

where σβ(x) = 1/(1 + e−βx) and β controls the level of stochastic behaviour of the neuron. The probability
of being in the quiescent state is given by normalization

p(vi(t + 1) =

v(t)) = 1
1
|
−

v(t)) = 1
p(vi(t + 1) = 1
|

−

−

σβ (ai(t))

(26.2.3)

531

v4

v3

v2

v4

v1

Learning Sequences

Figure 26.1: A depiction of a Hopﬁeld network (for 5 neurons).
The connectivity of the neurons is described by a weight matrix
with elements wij. The graph represents a snapshot of the state
of all neurons at time t which simultaneously update as functions
of the network at the previous time t

1.

−

These two rules can be compactly written as

p(vi(t + 1)

v(t)) = σβ (vi(t + 1)ai(t))
|

which follows directly from 1

−

vi(t + 1) = sgn (ai(t))

σβ(x) = σβ(

x). In the limit β

−

(26.2.4)

, the neuron updates deterministically

→ ∞

(26.2.5)

In a synchronous Hopﬁeld network all neurons update independently and simultaneously, and we can rep-
resent the temporal evolution of the neurons as a dynamic belief network, ﬁg(26.2)

p(v(t + 1)

v(t)) =

|

V
(cid:89)

i=1

p(vi(t + 1)

v(t)).
|

(26.2.6)

Given this description of how neurons update, we wish to use the network to do interesting things, for
example to store pattern sequences and recall them under some cue. The patterns will be stored in the
weights and biases and in the following section we address how to learn suitable settings based on simple
local learning rules.

26.3 Learning Sequences

26.3.1 A single sequence

=

Given a sequence of network states,
, we would like the network to store this sequence
}
such that it can be later recalled under some cue. That is, if the network is initialized in the correct starting
state of the training sequence v(t = 1), the remainder of the training sequence for t > 1 should be reproduced
under the deterministic dynamics equation (26.2.5), without error. Two classical approaches to learning a
temporal sequence are the Hebb and Pseudo Inverse rules[144]. In both the standard Hebb and PI cases,
the biases bi are usually set to zero.

v(1), . . . , v(T )
{

V

Standard Hebb rule

The standard Hebb rule sets the weights according to1

wij =

1
V

T −1
(cid:88)

t=1

vi(t + 1)vj(t)

1Donald Hebb, a neurobiologist actually stated[139]

(26.3.1)

Let us assume that the persistence or repetition of a reverberatory activity (or ‘trace’) tends to induce lasting
cellular changes that add to its stability. . . When an axon of cell A is near enough to excite a cell B and repeatedly
or persistently takes part in ﬁring it, some growth process or metabolic change takes place in one or both cells
such that A’s eﬃciency, as one of the cells ﬁring B, is increased.

This statement is sometimes misinterpreted to mean that weights are exclusively of the correlation form equation (26.3.1)(see
[286] for a discussion). This can severely limit the performance and introduce adverse storage artifacts including local
minima[144].

532

DRAFT April 24, 2015

Learning Sequences

v4(t)

v3(t)

v2(t)

v1(t)

v4(t + 1)

v3(t + 1)

v2(t + 1)

v1(t + 1)

Figure 26.2: A dynamic belief network representation of a Hop-
ﬁeld Network. The network operates by simultaneously gener-
ating a new set of neuron states from the previous set. Equa-
tion(26.2.6) deﬁnes a Markov transition matrix, modelling the
v(t + 1) and furthermore imposes
transition probability v(t)
the constraint that the neurons are conditionally independent
given the previous state of the network.

→

The Hebb rule can be motivated mathematically by considering

T −1
(cid:88)

τ =1

vi(τ + 1)

(cid:88)

j

vj(τ )vj(t)

(cid:88)

j

wijvj(t) =

=

1
V

1
V

vi(t + 1)

(cid:88)

v2
j (t) +

1
V

T −1
(cid:88)

τ (cid:54)=t

vi(τ + 1)

(cid:88)

j

vj(τ )vj(t)

= vi(t + 1) +

vi(τ + 1)

(cid:88)

j

vj(τ )vj(t)

j

1
V

T −1
(cid:88)

τ (cid:54)=t

If the patterns are uncorrelated then the ‘interference’ term

Ω

≡

1
V

T −1
(cid:88)

τ (cid:54)=t

vi(τ + 1)

(cid:88)

j

vj(τ )vj(t)

(26.3.2)

(26.3.3)

(26.3.4)

(26.3.5)

will be relatively small. To see this, we ﬁrst note that for uniform randomly drawn patterns, the mean of Ω
is zero, since the patterns are randomly

1. The variance is therefore given by

±

(cid:10)Ω2(cid:11) =

1
V 2

T −1
(cid:88)

(cid:88)

τ,τ (cid:48)(cid:54)=t

j,k

(cid:10)vi(τ + 1)vi(τ (cid:48) + 1)vj(τ )vj(t)vk(τ (cid:48))vk(t)(cid:11)

For j

= k, all the terms are independent and contribute zero on average. Therefore

(cid:10)Ω2(cid:11) =

1
V 2

T −1
(cid:88)

(cid:88)

τ,τ (cid:48)=1

j

(cid:10)vi(τ + 1)vi(τ (cid:48) + 1)vj(τ )vj(τ (cid:48))v2

j (t)(cid:11)

When τ

= τ (cid:48) all the terms are independent zero mean and contribute zero. Hence

(cid:10)Ω2(cid:11) =

1
V 2

(cid:88)

(cid:88)

τ (cid:54)=t

j

(cid:10)v2

i (τ + 1)v2

j (τ )v2

j (t)(cid:11) =

T

1

−
V

(26.3.6)

(26.3.7)

(26.3.8)

Provided that the number of neurons V is signiﬁcantly larger than the length of the sequence, T , the average
size of the interference will therefore be small. In this case the term vi(t + 1) in equation (26.3.4) dominates,
meaning that the sign of (cid:80)
j wijvj(t) will be that of vi(t + 1), and the correct pattern sequence recalled. A
careful analysis beyond our scope shows that the Hebb rule is capable of storing a random (uncorrelated)
temporal sequence of length 0.269V time steps[91]. However, the Hebb rule performs poorly for the case of
correlated patterns since interference from the other patterns becomes signiﬁcant[144, 68].

Pseudo inverse rule

The PI rule ﬁnds a matrix [W]ij = wij that solves the linear equations

(cid:88)

j

wijvj(t) = vi(t + 1),

t = 1, . . . , T

1

−

DRAFT April 24, 2015

(26.3.9)

533

(cid:54)
(cid:54)
Under this condition sgn
j wijvj(t)
recalled. In matrix notation we require

(cid:16)(cid:80)

(cid:17)

WV = ˆV

where

= sgn (vi(t + 1)) = vi(t + 1) so that patterns will be correctly

Learning Sequences

(26.3.10)

[V]it = vi(t),

t = 1, . . . , T

1,

−

(cid:105)

(cid:104)

ˆV

it

= vi(t + 1),

t = 2, . . . , T

(26.3.11)

For T < V the problem is under-determined so that multiple solutions exist. One solution is given by the
pseudo inverse:

W = ˆV

(cid:16)

VTV

(cid:17)−1

VT

(26.3.12)

The Pseudo Inverse (PI) rule can store any sequence of V linearly independent patterns. Whilst attractive
compared to the standard Hebb rule in terms of its ability to store longer correlated sequences, this rule
suﬀers from very small basins of attraction for temporally correlated patterns, see ﬁg(26.3).

The maximum likelihood Hebb rule

An alternative to the above classical algorithms is to view this as a problem of pattern storage in the DBN,
equation (26.2.6) [19]. First, we need to clarify what we mean by ‘store’. Given that we initialize the network
in a state v(t = 1), we wish that the remaining sequence will be generated with high probability. That is,
we wish to adjust the network parameters such that the probability

p(v(T ), v(T

v(1))
1), . . . , v(2)
|

−

(26.3.13)

is maximal2. Furthermore, we might hope that the sequence will be recalled with high probability not just
when initialized in the correct state but also for states close (in Hamming distance) to the correct initial
state v(1).

Due to the Markov nature of the dynamics, the conditional likelihood is

p(v(T ), v(T

v(1)) =
1), . . . , v(2)
|

−

T −1
(cid:89)

t=1

p(v(t + 1)

v(t))
|

(26.3.14)

This is a product of transitions from given states to given states. Since these transition probabilities are
known, equation (26.2.6) and equation (26.2.2), the conditional likelihood can be easily evaluated. The
sequence log (conditional) likelihood is

L(w, b)

log

≡

T −1
(cid:89)

t=1

p(v(t + 1)

v(t)) =
|

T −1
(cid:88)

t=1

log p(v(t + 1)

v(t)) =
|

T −1
(cid:88)

V
(cid:88)

t=1

i=1

log σβ (vi(t + 1)ai(t))

(26.3.15)

Our task is then to ﬁnd weights w and biases b that maximise L(w, b). There is no closed form solution
and the parameters need to be determined numerically. Nevertheless, this corresponds to a straightforward
computational problem since the log likelihood is a convex function. To show this, we compute the Hessian
(neglecting b for expositional clarity):

d2L
dwijdwkl

=

β2

−

T −1
(cid:88)

t=1

where we deﬁned

vi(t + 1)vj(t)γi(t)(1

−

γi(t))vk(t + 1)vl(t)δik

γi(t)

1

−

≡

σβ (vi(t + 1)ai(t)) .

2Static patterns can also be considered in this framework as a set of patterns that map to each other.

(26.3.16)

(26.3.17)

534

DRAFT April 24, 2015

Learning Sequences

Figure 26.3: Leftmost panel: The highly-correlated train-
ing sequence we desire to store. The other panels show the
temporal evolution of the network after initialization in the
correct starting state but corrupted with 30% noise. During
recall, deterministic updates β =
were used. The Max-
imum Likelihood rule was trained using 10 batch epochs
with η = 0.1. See also demoHopfield.m

∞

It is straightforward to show that the Hessian is negative semideﬁnite (see exercise(26.4)) and hence the
likelihood has a single global maximum. To increase the likelihood of the sequence, we can use a simple
method such as gradient ascent3

wnew

ij = wij + η

dL
dwij

,

bnew
i = bi + η

dL
dbi

where

dL
dwij

= β

T −1
(cid:88)

t=1

γi(t)vi(t + 1)vj(t),

dL
dbi

= β

T −1
(cid:88)

t=1

γi(t)vi(t + 1)

(26.3.18)

(26.3.19)

The learning rate η is chosen empirically to be suﬃciently small to ensure convergence. The maximum
likelihood learning rule equation (26.3.19) can be seen as a modiﬁed Hebb learning rule, the basic Hebb rule
being given when γi(t)
1. As learning progresses, the factors γi(t) will typically tend to values close to
either 1 or 0, and hence the learning rule can be seen as asymptotically equivalent to making an update
only in the case of disagreement (ai(t) and vi(t + 1) are of diﬀerent signs). This batch training procedure
can be readily converted to an online process in which an update occurs immediately after the presentation
of two consecutive patterns.

≡

Storage capacity of the ML Hebb rule

The ML Hebb rule is capable of storing a sequence of V linearly independent patterns. To see this, we ﬁrst
. Each neuron has
form an input-output training set for each neuron i,
an associated weight vector wi
,
∞
a perceptron[144]. For perfect recall of the patterns, we therefore need only that the vectors constituting
the pattern sequence be linearly separable. This will be the case if the patterns are linearly independent,
regardless of the outputs vi(t + 1), t = 1, . . . , T

(v(t), vi(t + 1)), t = 1, . . . , T
wij, j = 1, . . . , V , which forms a logistic regressor or, in the limit β =

1, see section(17.4.1).

1
}

≡

−

{

−

Relation to the perceptron rule

In the limit that the activation is large,

γi(t)

≈

(cid:26) 1 vi(t + 1)ai < 0
0
0 vi(t + 1)ai

≥

ai
|

| (cid:29)

1

(26.3.20)

Provided the activation and desired next output are the same sign, no update is made for neuron i. In
this limit, equation (26.3.19) is called the perceptron rule[144, 85]. For an activation a that is close to the
decision boundary, a small change can lead to a diﬀerent sign of the neural ﬁring. To guard against this it
is common to include a stability criterion

γi(t) =

(cid:26) 1 vi(t + 1)ai < M
M
0 vi(t + 1)ai

≥

(26.3.21)

3Naturally, one can use more sophisticated methods such as the Newton method, or conjugate gradients.
neurobiology the emphasis is on gradient style updates since these are deemed to be biologically more plausible.

In theoretical

DRAFT April 24, 2015

535

Training Sequencetimeneuron number1020102030405060708090100Max Likelihood1020102030405060708090100Hebb1020102030405060708090100Pseudo Inverse1020102030405060708090100Learning Sequences

Figure 26.4: The fraction of neurons correct for the ﬁ-
nal state of the network T = 50 for a 100 neuron Hop-
ﬁeld network trained to store a length 50 sequence of
patterns. After initialization in the correct initial state
at t = 1, the Hopﬁeld network is updated determinis-
tically, with a randomly chosen percentage of the neu-
rons ﬂipped after updating. The correlated sequence
of length T = 50 was produced by ﬂipping with prob-
ability 0.5, 20% of the previous state of the network.
A fraction correct value of 1 indicates perfect recall
of the ﬁnal state, and a value of 0.5 indicates a per-
formance no better than random guessing of the ﬁnal
state. For maximum likelihood 50 epochs of training
were used with η = 0.02. During recall, deterministic
updates β =
were used. The results presented are
averages over 5000 simulations, resulting in standard
errors of the order of the symbol sizes.

∞

(a)

where M is an empirically chosen positive threshold.

Example 26.1 (Storing a correlated sequence). In ﬁg(26.3) we consider storage of a temporal sequence
of length T = 20 of 100 neurons using the three learning rules: Hebb, Maximum Likelihood and Pseudo
Inverse. The sequence is highly correlated and therefore represents a diﬃcult learning task. The biases bi
are set to zero throughout to facilitate comparison. The initial state of the training sequence, corrupted by
30% noise is presented to the trained networks, and we desire that the remaining training sequence will be
recalled from this initial noisy state. Whilst the Hebb rule is operating in a feasible limit for uncorrelated
patterns, the strong correlations in this training sequence give rise to poor results. The PI rule is capable
of storing a sequence of length 100 yet is not robust to perturbations from the correct initial state. The
Maximum Likelihood rule performs well after a small amount of training.

Stochastic interpretation

By straightforward manipulations, the weight update rule in equation (26.3.19) can be written as

dL
dwij

=

T −1
(cid:88)

t=1

(cid:16)

1
2

vi(t + 1)

vi(t + 1)

− (cid:104)

(cid:105)p(vi(t+1)|ai(t))

(cid:17)

vj(t)

A stochastic, online learning rule is therefore

∆wij(t) = η (vi(t + 1)

˜vi(t + 1)) vj(t)

−

(26.3.22)

(26.3.23)

where ˜vi(t+1) is sampled in state 1 with probability σβ(ai(t)), and
rate η is small, this stochastic updating will approximate the learning rule (26.3.18,26.3.19).

1 otherwise. Provided that the learning

−

Example 26.2 (Recalling sequences under perpetual noise). We consider 50 neurons storing a T = 50
length sequence (with zero biases and compare the performance of the Maximum Likelihood learning rule
with the Hebb, Pseudo Inverse, and Perceptron rule. The training sequences are produced by starting from
a random initial state, v(1), and then choosing at random 20% percent of the neurons to ﬂip, each of the
chosen neurons being ﬂipped with probability 0.5, giving a random training sequence with a high degree of

536

DRAFT April 24, 2015

00.050.10.150.20.250.30.350.40.450.50.50.550.60.650.70.750.80.850.90.951flip probabilityfraction correctsequence length=50Max Likelihoodnoise trained Max Likelihoodperceptron (M=10)perceptron (M=0)hebbpseudo inverseLearning Sequences

(a)

(b)

(a): Original T = 15 binary video sequence on a set of 81

(b): The
Figure 26.5:
reconstructions beginning from a 20% noise perturbed initial state. Every odd time reconstruction is also
randomly perturbed. Despite the high level of noise the basin of attraction of the pattern sequence is very
broad and the patterns immediately fall back close to the pattern sequence even after a single timestep.

111 = 8991 neurons.

×

temporal correlation.

After training, the network is initialized to a noise corrupted version of the correct initial state v(t = 1)
) for the same number of steps as the
from the training sequence. The dynamics is then run (at β =
length of the training sequence. The fraction of bits of the recalled ﬁnal state which are the same as the
training sequence ﬁnal state v(T ) is then measured, ﬁg(26.4). At each stage in the dynamics (except the
last), the state of the network is corrupted with noise by ﬂipping each neuron state with the speciﬁed ﬂip
probability.

∞

The standard Hebb rule performs relatively poorly, particularly for small ﬂip rates, whilst the other
methods perform relatively well, being robust at small ﬂip rates. As the ﬂip rate increases, the pseudo
inverse rule becomes unstable, especially for the longer temporal sequence which places more demands
on the network. The perceptron rule can perform as well as the maximum likelihood rule, although its
performance is critically dependent on an appropriate choice of the threshold M . The results for M = 0
Perceptron training are poor for small ﬂip rates. An advantage of the maximum likelihood rule is that it
performs well without the need for ﬁne tuning of parameters.

A similar example for a larger network is given in ﬁg(26.5) which consists of highly correlated sequences.
The weights are learned using the maximum likelihood procedure. For such short sequences the basin of
attraction is very large and the video sequence can be stored robustly.

26.3.2 Multiple sequences

If we assume that the sequences are
We now address
independent, the log likelihood of a set of sequences is the sum of the individual sequences. The gradient is
given by

learning a set of sequences

.
}

{V

n, n = 1, . . . , N

dL
dwij

= β

N
(cid:88)

T −1
(cid:88)

n=1

t=1

i (t)vn
γn

i (t + 1)vn

j (t),

where

γn
i (t)

1

−

≡

σβ (vn

i (t + 1)an

i (t)) ,

dL
dbi

= β

N
(cid:88)

T −1
(cid:88)

n=1

t=1

i (t)vn
γn

i (t + 1)

an
i (t) = bi +

wijvn

j (t)

(cid:88)

j

(26.3.24)

(26.3.25)

The log likelihood remains convex since it is a sum of convex functions, so that the standard gradient based
learning algorithms can be used successfully here as well.

DRAFT April 24, 2015

537

Tractable Continuous Latent Variable Models

26.3.3 Boolean networks

v(t)). However, less
The Hopﬁeld network is one particular parameterisation of the table p(vi(t + 1) = 1
|
constrained parameters may be considered. In the fully unconstrained case each neuron i has an associated
2V parental states. However, to specify this exponentially large number of states is impractical and an
interesting restriction is to consider that each neuron has only K parents, so that each table contains 2K
entries. Learning the table parameters by maximum likelihood is straightforward since the log likelihood is
a convex function of the table entries. Hence, for any given sequence (or set of sequences) one may readily
ﬁnd parameters that maximise the sequence reconstruction probability. The maximum likelihood method
also produces large basins of attraction for the associated stochastic dynamical system. Such models are of
interest in Artiﬁcial Life and Random Boolean networks in which emergent macroscopic behaviour appears
from local update rules. Such systems are also used to study the robustness of chemical and gene regulatory
networks[171].

26.3.4 Sequence disambiguation

A limitation of time-independent ﬁrst order networks deﬁned on visible variables alone (such as the Hopﬁeld
vt = v) is the same every time the joint state v is
network) is that the observation transition p(vt+1
|
encountered. This means that if the sequence contains a subsequence such as a, b, a, c this cannot be recalled
with high probability since a joint state a transitions to diﬀerent states at diﬀerent times. Whilst one could
attempt to resolve this sequence disambiguation problem using a higher order Markov model to account
for a longer temporal context, or by using a time-dependent model, we would lose biological plausibility.
Using latent variables is an alternative way to sequence disambiguation. In the Hopﬁeld model the recall
capacity can be increased using latent variables by making a sequencing in the joint latent-visible space
that is linearly independent, even if the visible variable sequence alone is not. In section(26.4) we discuss a
general method that extends dynamic belief networks deﬁned on visible variables alone, such as the Hopﬁeld
network, to include non-linearly updating latent variables.

26.4 Tractable Continuous Latent Variable Models

A dynamic belief network with hidden (latent) variables h and visible variables (observations) v takes the
form

v(1))
p(v(1 : T ), h(1 : T )) = p(v(1))p(h(1)
|

T −1
(cid:89)

t=1

p(v(t + 1)

v(t), h(t))p(h(t + 1)
|

v(t), v(t + 1), h(t)) (26.4.1)
|

As we saw in chapter(23), provided all hidden variables are discrete, inference in these models is straight-
forward. However, in many physical systems it is more natural to assume continuous h(t). In chapter(24)
we saw that one such tractable continuous h(t) model is given by linear Gaussian transitions and emissions
- the LDS. Whilst this is useful, we cannot represent non-linear changes in the latent process using an LDS
alone. The Switching LDS of chapter(25) is able to model non-linear continuous dynamics (via switching)
although we saw that this leads to computational diﬃculties. For computational reasons we therefore seem
limited to either purely discrete h (with no limitation on the discrete transitions) or purely continuous h
(but be forced to use simple linear dynamics). Is there a way to have a continuous state with non-linear
dynamics for which posterior inference remains tractable? The answer is yes, provided that we assume the
hidden transitions are deterministic[13]. When conditioned on the visible variables, this renders the hidden
unit distribution trivial. This allows the consideration of rich non-linear dynamics in the hidden space. Note
not limited to the distributed computation context of this chapter; for example the
that such models are
ARCH and GARCH models of chapter(24) are special cases.

@@

26.4.1 Deterministic latent variables

Consider a Belief Network deﬁned on a sequence of visible variables v(1 : T ). To enrich the model we
include additional continuous latent variables h(1 : T ) that follow a non-linear Markov transition. To retain
tractability of inference, we constrain the latent dynamics to be deterministic, described by

p(h(t + 1)

|

v(t + 1), v(t), h(t)) = δ (h(t + 1)

f (v(t + 1), v(t), h(t), θh))

−

(26.4.2)

538

DRAFT April 24, 2015

Tractable Continuous Latent Variable Models

h(1)

h(2)

h(t)

v(1)

v(2)

(a)

v(t)

h(1)

h(2)

h(t)

v(1)

(b)

v(t)

v(2)

(c)

(a): A ﬁrst order dynamic belief network with deterministic hidden transitions (represented
Figure 26.6:
(b):
by diamonds) that is, each hidden node is certainly in a single state, determined by its parents.
Conditioning on the visible variables forms a directed chain in the hidden space which is deterministic.
Hidden unit inference can be achieved by forward propagation alone. (c): Integrating out hidden variables
gives a cascade style directed visible graph so that each v(t) depends on all v(1 : t

1).

−

The (possibly non-linear) function f parameterises the conditional probability table. Whilst the restric-
tion to deterministic transitions appears severe, the model retains some attractive features: The marginal
p(v(1 : T )) is non-Markovian, coupling all the variables in the sequence, see ﬁg(26.6c), whilst hidden unit
v(1 : T )) is deterministic. See also ﬁg(26.6).
inference p(h(1 : T )
|

The adjustable parameters of the hidden and visible distributions are represented by θh and θv respectively.
For learning, the log likelihood of a single training sequence v(1 : T ) is

L = log p(v(1)

θv) +
|

T −1
(cid:88)

t=1

log p(v(t + 1)

v(t), h(t), θv)
|

where the hidden unit values are calculated recursively using

h(t + 1) = f (v(t + 1), v(t), h(t), θh)

(26.4.3)

(26.4.4)

To maximise the log likelihood using gradient techniques we need the derivatives with respect to the model
parameters. These can be calculated recursively as follows:

dL
dθv

=

∂
∂θv

log p(v(1)

θv) +
|

T −1
(cid:88)

t=1

∂
∂θv

log p(v(t + 1)

v(t), h(t), θv)
|

dL
dθh

=

T −1
(cid:88)

t=1

∂
∂h(t)

log p(v(t + 1)

v(t), h(t), θv)
|

dh(t)
dθh

dh(t)
dθh

=

∂f (t)
∂θh

+

∂f (t)

∂h(t

1)

−

1)

dh(t

−
dθh

where we use the shorthand

f (t)

≡

f (v(t), v(t

1), h(t

1), θh)

−

−

(26.4.5)

(26.4.6)

(26.4.7)

(26.4.8)

Hence the derivatives can be calculated by deterministic forward propagation. The case of training multiple
independently generated sequences is a straightforward extension obtained by summing the above over the
individual sequences.

Whilst the deterministic latent variable models are very general, in the context of this chapter it is interesting
to apply them to some simple neurobiological models, enriching the Hopﬁeld model with more powerful
internal dynamics.

DRAFT April 24, 2015

539

Tractable Continuous Latent Variable Models

(a)

(b)

(a): The training sequence consists of a random set of vectors (V = 3)
Figure 26.7:
(b): The reconstruction using H = 7 hidden units. The
over T = 10 time steps.
initial state v(t = 1) for the recalled sequence was set to the correct initial training
value albeit with one of the values ﬂipped. Note that the method is capable of sequence
disambiguation in the sense that transitions of the form a, b, . . . , a, c can be recalled.

26.4.2 An augmented Hopﬁeld network

To make the deterministic latent variable model more explicit, we consider the case of continuous vector
hidden variables h(t) and discrete, binary vector visible variables with components vi(t)
In
particular, we restrict attention to the Hopﬁeld model augmented with latent variables that have a simple
linear dynamics (see exercise(26.5) for a non-linear extension):

1, 1
}

∈ {−

.

h(t + 1) = 2σ(Ah(t) + Bv(t))

−

1 deterministic latent transition

p(v(t + 1)

|

v(t), h(t)) =

V
(cid:89)

i=1

σ (vi(t + 1)φi(t)) ,

φ(t)

≡

Ch(t) + Dv(t)

(26.4.9)

(26.4.10)

This model generalises a recurrent stochastic heteroassociative Hopﬁeld network[144] to include deterministic
hidden units dependent on previous network states. The parameters of the model are A, B, C, D. For
gradient based training we require the derivatives with respect to each of these parameters. The derivative
of the log likelihood for a generic parameter θ is

d
dθ

L =

(cid:88)

i

νi(t)

d
dθ

φi(t),

νi(t)

(1

−

≡

σ(vi(t + 1)φi(t))) vi(t + 1)

This gives:

d
dAαβ

d
dCαβ

d
dAαβ

d
dBαβ

φi(t) =

(cid:88)

Cij

j

d
dAαβ

hj(t),

d
dBαβ

φi(t) =

(cid:88)

Cij

j

d
dBαβ

hj(t)

φi(t) = δiαhβ(t),

d
dDαβ

φi(t) = δiαvβ(t)

hi(t + 1) = 2σ(cid:48)

i(t + 1)

hi(t + 1) = 2σ(cid:48)

i(t + 1)

(cid:88)

Aij

j

d
dAαβ

(cid:88)

Aij

j

d
dBαβ

hj(t) + δiαhβ(t)

hj(t) + δiαvβ(t)

σ(cid:48)
i(t)

≡

σ(hi(t)) (1

σ(hi(t)))

−

(26.4.11)

(26.4.12)

(26.4.13)

(26.4.14)

(26.4.15)

(26.4.16)

If we assume that h(1) is a given ﬁxed value (say 0), we can compute the derivatives recursively by forward
propagation. Gradient based training for this augmented Hopﬁeld network is therefore straightforward
to implement. This model extends the power of the original Hopﬁeld model, being capable of resolving
ambiguous transitions in sequences such as a, b, a, c, see example(26.3). In terms of a dynamic system, the
learned network is an attractor with the training sequence as a stable point and demonstrates that such
models are capable of learning attractor networks more powerful than those without hidden units.

540

DRAFT April 24, 2015

Neural Models

Example 26.3 (Sequence disambiguation). The sequence in ﬁg(26.7a) contains repeated patterns and
therefore cannot be reliably recalled with a ﬁrst order model containing visible variables alone. To deal with
this we consider a Hopﬁeld network with 3 visible units and 7 additional hidden units with deterministic
(linear) latent dynamics. The model was trained with gradient ascent to maximise the likelihood of the
binary sequence in ﬁg(26.7a). See demoHopfieldLatent.m. As shown in ﬁg(26.7b), the learned network is
capable of recalling the sequence correctly, even when initialised in an incorrect state, having no diﬃculty
with the fact that the sequence transitions are ambiguous.

26.5 Neural Models

The tractable deterministic latent variable model introduced in section(26.4) presents an opportunity to
extend models such as the Hopﬁeld network to include more biologically realistic processes without losing
computational tractability. First we discuss a general framework for learning in a class of neural models[14,
240], this being a special case of the deterministic latent variable models[13] and a generalisation of the
spike-response model of theoretical neurobiology[118].

26.5.1 Stochastically spiking neurons

We assume that neuron i ﬁres depending on the membrane potential ai(t) through

p(vi(t + 1) = 1
|

v(t), h(t)) = p(vi(t + 1) = 1
ai(t))
|

To be speciﬁc, we take throughout

p(vi(t + 1) = 1
|

ai(t)) = σ (ai(t))

Here we deﬁne the quiescent state as vi(t + 1) = 0, so that

p(vi(t + 1)

|

ai(t)) = σ ((2vi(t + 1)

1)ai(t))

−

(26.5.1)

(26.5.2)

(26.5.3)

The use of the sigmoid function σ(x) is not fundamental and is chosen merely for analytical convenience.
The log-likelihood of a sequence of visible states

=

is

v(1), . . . , v(T )
}

{

V

L =

T −1
(cid:88)

V
(cid:88)

t=1

i=1

log σ ((2vi(t + 1)

1)ai(t))

−

for which the gradient is

dL
dwij

=

T −1
(cid:88)

t=1

(vi(t + 1)

σ(ai(t)))

−

dai(t)
dwij

(26.5.4)

(26.5.5)

. Here wij are parameters of the membrane potential (see below).
where we used the fact that vi
We take equation (26.5.5) as common in the following models in which the membrane potential ai(t) is
described with increasing sophistication.

0, 1
}

∈ {

26.5.2 Hopﬁeld membrane potential

As a ﬁrst step, we show how the Hopﬁeld maximum likelihood training rule, as described in section(26.3.1),
can be recovered as a special case of the above framework. The Hopﬁeld membrane potential is

ai(t)

≡

V
(cid:88)

j=1

wijvj(t)

bi

−

(26.5.6)

where wij characterizes the eﬃcacy of information transmission from neuron j to neuron i, and bi is a bias.
by adjustment of
Applying the maximum likelihood framework to this model to learn a temporal sequence

V

DRAFT April 24, 2015

541

Neural Models

Figure 26.8: Learning with depression : U = 0.5,
τ = 5, δt = 1, η = 0.25. Despite the apparent
complexity of the dynamics, learning appropriate neu-
ral connection weights is straightforward using max-
imum likelihood. The reconstruction using the stan-
dard Hebb rule by contrast is poor[14].

the parameters wij (the bi are ﬁxed for simplicity), we obtain the (batch) learning rule (using dai/dwij = vj(t)
from equation (26.5.5))

wnew

ij = wij + η

dL
dwij

,

dL
dwij

=

T −1
(cid:88)

t=1

(vi(t + 1)

−

σ(ai(t))) vj(t),

(26.5.7)

where the learning rate η is chosen empirically to be suﬃciently small to ensure convergence. Equa-
tion(26.5.7) matches equation (26.3.19) (which uses the

1 encoding).

±

26.5.3 Dynamic synapses

In more realistic synaptic models, neurotransmitter generation depends on a ﬁnite rate of cell subcomponent
production and the quantity of vesicles released is aﬀected by the history of ﬁring[1]. Loosely speaking, when
a neuron ﬁres it releases a chemical substance from a local reservoir. If the neuron ﬁres several times in
short succession, its ability to continue ﬁring weakens since the reservoir of release chemical is depleted.
This phenomenon can be modelled by using a depression mechanism that reduces the membrane potential

ai(t) = wijxj(t)vj(t)

for depression factors xj(t)

xj(t + 1) = xj(t) + δt

∈
(cid:18) 1

−

xj(t)
τ

−

(cid:19)

U xj(t)vj(t)

[0, 1]. A simple dynamics for these depression factors is[299]

(26.5.8)

(26.5.9)

where δt, τ , and U represent time scales, recovery times and spiking eﬀect parameters respectively. Note that
these depression factor dynamics are exactly of the form of deterministic hidden variables. It is therefore
straightforward to include these dynamic synapses in a principled way using the maximum likelihood learning
framework. For the Hopﬁeld potential, the learning dynamics is simply given by equations (26.5.5,26.5.9),
with

dai(t)
dwij

= xj(t)vj(t)

(26.5.10)

Example 26.4 (Learning with depression). In ﬁg(26.8) we demonstrate learning a random temporal se-
quence of 20 timesteps for 50 neurons with dynamic depressive synapses. After learning wij the trained
network is initialised in the ﬁrst state of the training sequence. The remaining states of the sequence were
then correctly recalled by forward sampling of the learned model. The corresponding generated factors xi(t)
are also plotted during the reconstruction; we see the characteristic drop in x after a neuron ﬁres with grad-
ual recovery. For comparison, we plot the results of using the dynamics having set the wij using the temporal
Hebb rule, equation (26.3.1). The poor performance of the correlation based Hebb rule demonstrates the
necessity, in general, to tailor the learning rule to the dynamical system it attempts to control.

542

DRAFT April 24, 2015

neuron numberOriginalt10205101520253035404550Reconstructiont10205101520253035404550x valuest10205101520253035404550Hebb Reconstructiont10205101520253035404550Neural Models

26.5.4 Leaky integrate and ﬁre models

Leaky integrate and ﬁre models move a step further towards biological realism in which the membrane
potential increments if it receives an excitatory stimulus (wij > 0), and decrements if it receives an inhibitory
stimulus (wij < 0). After ﬁring, the membrane potential is reset to a low value below the ﬁring threshold,
and thereafter steadily increases to a resting level (see for example [65, 118]). A model that incorporates
such eﬀects is



ai(t) =

αai(t

1) +

−

(cid:88)

j



wijvj(t) + θrest (1

α)

 (1

vi(t

−

−

−

1)) + vi(t

−

1)θf ired

(26.5.11)

Since vi
synaptic input, the potential equilibrates to θrest with time constant

, if neuron i ﬁres at time t
}

1 the potential is reset to θf ired at time t. Similarly, with no
1/ log α[14].

∈ {

0, 1

−

−

Despite the increase in complexity of the membrane potential over the Hopﬁeld case, deriving appropriate
learning dynamics for this new system is straightforward since, as before, the hidden variables (here the
membrane potentials) update in a deterministic fashion. The membrane potential derivatives are

dai(t)
dwij

= (1

vi(t

−

−

(cid:18)

1))

α

1)

dai(t

−
dwij

(cid:19)

+ vj(t)

(26.5.12)

By initialising the derivative dai(t=1)
the gradient which can be used to adapt wij in the usual manner wnew
apply synaptic dynamics to this case by replacing the term vj(t) in equation (26.5.12) by xj(t)vj(t).

= 0, equations (26.5.5,26.5.11,26.5.12) deﬁne a ﬁrst order recursion for
ij = wij + ηdL/dwij. We could also

dwij

Although a detailed discussion of the properties of the neuronal responses for networks trained in this way
is beyond our scope here, an interesting consequence of the learning rule equation (26.5.12) is a spike-
time dependent learning window in qualitative agreement with experimental observation in real biological
systems[240, 200].

26.6 Summary

•

•

•

Classical models such as the Hopﬁeld network can be trained to learn temporal sequences using maximum
likelihood. This results in a robust storage mechanism in which the patterns have a large basin of attraction.

We can form tractable non-linear continuous latent systems provided the latent dynamics is deterministic.
These deterministic latent variable models are powerful yet inference is straightforward.

We demonstrated how complex models in neurobiology can be considered within the deterministic latent
variable framework, and also how learning rules can be derived in a straightforward manner. It is important
that the learning rule is derived with the particular neural dynamics in mind – otherwise undesirable eﬀects
in pattern storage can occur.

26.7 Code

demoHopfield.m: Demo of Hopﬁeld sequence learning
HebbML.m: Gradient ascent training of a set of sequences using maximum likelihood
HopfieldHiddenNL.m: Hopﬁeld network with additional non-linear latent variables
demoHopfieldLatent.m: Demo of Hopﬁeld net with deterministic latent variables
HopfieldHiddenLikNL.m: Hopﬁeld network with hidden variables sequence likelihood

DRAFT April 24, 2015

543

26.8 Exercises

Exercises

Exercise 26.1. Consider a very large V
(cid:29)
single temporal sequence v(1 : T ) of length T
be computationally diﬃcult to store. Explain how to justify the assumption

1 stochastic Hopﬁeld network, section(26.2), used to store a
V . In this case the weight matrix with elements wij may

(cid:28)

wij =

T −1
(cid:88)

t=1

ui(t)vi(t + 1)vj(t)

(26.8.1)

where ui(t) are the dual parameters and derive a maximum likelihood update rule for the dual parameters
ui(t).

Exercise 26.2. A Hopﬁeld network is used to store a raw uncompressed binary video sequence. Each image
in the sequence contains 106 binary pixels. At a rate of 10 frames per second, how many hours of video can
106 neurons store?

Exercise 26.3. Derive the update equation (26.3.22).

Exercise 26.4. Show that the Hessian equation (26.3.16) is negative semideﬁnite. That is

(cid:88)

i,j,k,l

xijxkl

d2L
dwijdwkl ≤

0

for any x

= 0.

Exercise 26.5. For the augmented Hopﬁeld network of section(26.4.2), with latent dynamics

hi(t + 1) = 2σ



(cid:88)



j



Aijhj(t) + Bijvj(t)



1

−

derive the derivative recursions described in section(26.4.2).

(26.8.2)

(26.8.3)

Exercise 26.6. The storage of a static pattern v can be considered equivalent to the requirement that, under
temporal dynamics, the probability p(v(t + 1) = v
v(t) = v) is high. Based on this, estimate numerically
how many such static patterns a 100 neuron stochastic Hopﬁeld network can robustly recover when 10% of
the elements of a pattern are ﬂipped.

|

544

DRAFT April 24, 2015

(cid:54)
Part V

Approximate Inference

545

Introduction to Part V

In Part I we discussed inference and showed that for certain models this is com-
putationally tractable. However, for many models of interest, one cannot perform
inference exactly and approximations are required.

In Part V we discuss approximate inference methods, beginning with sampling based
approaches. These are popular and well known in many branches of the mathematical
sciences, having their origins in chemistry and physics. We also discuss alternative
deterministic approximate inference methods which in some cases can have remarkably
accurate performance.

It is important to bear in mind that no single algorithm is going to be best on all
inference tasks. For this reason, we attempt throughout to explain the assumptions
behind the techniques so that one may select an appropriate technique for the problem
at hand.

DRAFT April 24, 2015

547

Approximate inference methods and their loose associations. The leaf nodes denote speciﬁc methods. Part
V discusses these methods and their application to models of interest in machine learning.

548

DRAFT April 24, 2015

Graphicalmodeldeterministicapprox.Laplace(cont.variables)variationalboundsKL(q|p)factorised(naiveMF)structuredotherconvexboundsloopymessagepassingEP(messagesintract.)Samplingapprox.exactsampling(egan-cestral)MCMCauxilliaryvariablemethodsHybridMCMCSlicesamplingMetropolisHastingsGibbssamplingstructuredGibbssamplingImportancesamplingCHAPTER 27

Sampling

In cases where exact results cannot be obtained, a general purpose approach is to draw samples from the
distribution. In this chapter we discuss classical exact sampling methods that are typically limited to a
small number of variables or models with a high degree of structure. When these can no longer be applied,
we discuss approximate sampling methods including Markov chain Monte Carlo.

27.1 Introduction

= (cid:8)x1, . . . , xL(cid:9) of a variable x from a distribution p(x).
Sampling concerns drawing realisations (samples)
For a discrete variable x, in the limit of a large number of samples, the fraction of samples in state x tends
to p(x = x). That is,

X

lim
L→∞

1
L

L
(cid:88)

l=1

(cid:105)
(cid:104)
xl = x

I

= p(x = x)

(27.1.1)

In the continuous case, one can consider a region R such that the probability that the samples occupy R
tends to the integral of p(x) over R. Given a ﬁnite set of samples, one can then approximate expectations
using

f (x)

(cid:105)p(x) ≈

(cid:104)

1
L

L
(cid:88)

l=1

f (xl)

ˆfX

≡

(27.1.2)

The subscript in ˆfX emphasises that the approximation is dependent on the set of samples drawn. This
sample approximation holds for both discrete and continuous variables.

and can itself be considered as generating a distri-
A sampling procedure produces realisations of the set
bution ˜p(
). Provided the marginals of the sampling distribution are equal to the marginals of the target
distribution, ˜p(xl) = p(xl), then the average of the approximation ˆfX with respect to draws of the sample
set

X

X

is

X

(cid:69)

(cid:68) ˆfX

=

˜p(X )

1
L

L
(cid:88)

l=1

(cid:68)

(cid:69)
f (xl)

˜p(xl)

f (x)

=

(cid:104)

(cid:105)p(x)

(27.1.3)

Hence the mean of the sample approximation is the exact mean of f provided only that the marginals of
) then ˆfX is an unbiased estimator for
˜p(
X
) does
f (x)
(cid:104)
not factorise into (cid:81)

) correspond to the required marginals p(x); that is, using ˜p(
(cid:105)p(x). Note that this holds, even if the individual samples x1, . . . , xL are dependent, that is ˜p(

X

X

l ˜p(xl).

549

Introduction

For any sampling method, an important issue is the variance of the sample estimate. If this is low then only
a small number of samples are required since the sample mean must be close to the true mean (assuming it
is unbiased). Deﬁning
(cid:68) ˆfX

∆f (x) = f (x)

∆ ˆfX = ˆfX

(27.1.4)

f (x)

(cid:69)

,

−

˜p(X )

− (cid:104)

(cid:105)p(x)

the variance of the approximation is (assuming ˜p(xl) = p(xl), for all l)

(cid:88)

(cid:68)
∆f (xl)∆f (xl(cid:48)

(cid:69)
)

˜p(xl,xl(cid:48) )

(cid:28)(cid:104)

∆ ˆfX

(cid:105)2(cid:29)

˜p(X )

=

=

1
L2

1
L2

l,l(cid:48)


L

[∆f (x)]2(cid:69)
(cid:68)

+

˜p(x)

(cid:88)

(cid:68)

∆f (xl)∆f (xl(cid:48)

(cid:69)
)

l(cid:54)=l(cid:48)





˜p(xl,xl(cid:48) )

(27.1.5)

(27.1.6)

Provided the samples are independent

) =

˜p(

X

L
(cid:89)

l=1

˜p(xl)

and ˜p(x) = p(x), then ˜p(xl, xl(cid:48)
(cid:10)∆f (xl)(cid:11) (cid:68)
∆f (xl(cid:48)

(cid:69)
)

which is zero since

) = p(xl)p(xl(cid:48)

). The second term in equation (27.1.6) above is composed of

∆f (x)
(cid:105)

(cid:104)

= 0. Hence

(cid:28)(cid:104)

∆ ˆfX

(cid:105)2(cid:29)

[∆f (x)]2(cid:69)
(cid:68)

=

1
L

p(x)

˜p(X )

(27.1.7)

and the variance of the approximation scales inversely with the number of samples. In principle, therefore,
provided the samples are independently drawn from p(x), only a small number of samples is required to
accurately estimate the expectation. Importantly, this result is independent of the dimension of x. However,
the critical diﬃculty is in actually generating independent samples from p(x). Drawing samples from high-
dimensional distributions is generally diﬃcult and few guarantees exist to ensure that in a practical timeframe
the samples produced are independent. Whilst a dependent sampling scheme may be unbiased, the variance
of the resulting estimate can be high such that a large number of samples may be required for expectations to
be approximated accurately. There are many diﬀerent sampling algorithms, all of which work in principle,
but each working in practice only when the distribution satisﬁes particular properties[123]; for example
Markov chain Monte Carlo methods do not produce independent samples and a large number of samples
may be necessary to produce a satisfactory approximation. Before we develop schemes for multivariate
distributions, we consider the univariate case.

27.1.1 Univariate sampling

In the following, we assume that a random number generator exists which is able to produce a value uniformly
at random from the unit interval [0, 1]. We will make use of this uniform sampler to draw samples from
non-uniform distributions.

Discrete case

Consider the one dimensional discrete distribution p(x) where dom(x) =

1, 2, 3
}

{

, with

p(x) =






0.6 x = 1
0.1 x = 2
0.3 x = 3

(27.1.8)

This represents a partitioning of the unit interval [0, 1] in which the interval [0, 0.6] has been labelled as
If we were to drop a point
state 1, (0.6, 0.7] as state 2, and (0.7, 1.0] as state 3, ﬁg(27.1).
anywhere
would land in interval 1 is 0.6, and the
at random, uniformly in the interval [0, 1], the chance that
chance that it would be in interval 2 is 0.1 and similarly, for interval 3, 0.3. This therefore deﬁnes a valid

×

×

550

DRAFT April 24, 2015

Introduction

1

×

2

3

Figure 27.1: A representation of the discrete distribu-
tion equation (27.1.8). The unit interval from 0 to 1
is partitioned in parts whose lengths are equal to 0.6,
0.1 and 0.3.

sampling procedure for discrete one-dimensional distributions in which we draw from a uniform distribution
and then identify the partition of the unit interval in which it lies. This can easily be found using the
cumulant, see algorithm(27.1). Sampling from a discrete univariate distribution is straightforward since
computing the cumulant takes only O (K) steps for a K state discrete variable. In our example, we have
(c0, c1, c2, c3) = (0, 0.6, 0.7, 1). We then draw a sample uniformly from [0, 1], say u = 0.66. Then the sampled
state would be state 2, since this is in the interval (c1, c2]. Since the samples are independent, the number
of samples required to accurately represent a marginal is reasonably small, see ﬁg(27.2).

Continuous case

Intuitively, the generalisation from the discrete to the continuous case is clear. First we calculate the
cumulant density function

(cid:90) y

C(y) =

p(x)dx

−∞

(27.1.9)

Then we sample u uniformly from [0, 1], and obtain the corresponding sample x by solving C(x) = u
⇒
x = C−1(u). Formally, therefore, sampling of a continuous univariate variable is straightforward provided
we can compute the integral of the corresponding probability density function.

For special distributions, however, one may avoid explicit use of the cumulant of p(x) by using alternative
procedures based on coordinate transformations. For the Gaussian, for example, one may avoid the use of
the cumulant, see exercise(27.1).

27.1.2 Rejection sampling

Consider that we have an eﬃcient sampling procedure for a distribution q(x). Can we use this to help us
sample from another distribution p(x)? We assume p(x) is known only up to a normalisation constant Z,
p(x) = p∗(x)/Z. One way to sample from p(x) is to use a binary auxiliary variable y
and deﬁne
q(x, y) = q(x)q(y

∈ {

0, 1

}

x) with
|

q(x, y = 1) = q(x)q(y = 1
|

x)

(27.1.10)

We can use the term q(y = 1
x)
x) to our advantage if we set q(y = 1
∝
|
|
p(x) and sampling from q(x, y) gives us a procedure for sampling from p(x). To achieve this we assume we
can ﬁnd a positive M such that

p(x)/q(x) since then q(x, y = 1)

∝

q(y = 1
|

x) =

p∗(x)
M q(x) ≤

1

x

∀

(27.1.11)

Algorithm 27.1 Sampling from a univariate discrete distribution p with K states.

1: Label the K states as i = 1, . . . , K, with associated probabilities pi.
2: Calculate the cumulant

ci =

(cid:88)

j≤i

pj

and set c0 = 0.

3: Draw a value u uniformly at random from the unit interval [0, 1].
4: Find that i for which ci−1 < u
5: Return state i as a sample from p.

ci.

≤

DRAFT April 24, 2015

551

Algorithm 27.2 Rejection sampling to draw L independent samples from p(x) = p∗(x)/Z.

1: Given p∗(x) and q(x), ﬁnd M such that p∗(x)/q(x)
2: for l = 1 to L do
repeat
3:

≤

M for all x.

Introduction

Draw a candidate sample xcand from q(x).
Let a = p∗(xcand)
M q(xcand)
Draw a value u uniformly between 0 and 1.

4:

5:

6:

7:

until u
a
≤
xl = xcand

8:
9: end for

(cid:46) Accept the candidate

x),
To sample y given x we then draw a value u uniformly between 0 and 1. If this value is less then q(y = 1
|
we set y = 1, otherwise we set y = 0. To draw a sample from p(x) we ﬁrst draw a candidate xcand from
xcand). If y = 1 we take xcand as an independent sample from p(x) – otherwise
q(x), and then a y from q(y
|
no sample is made, see algorithm(27.2). Note that the samples are drawn only proportionally to p(x) –
however, since this proportionality constant is common to all x, this is equivalent to drawing from p(x).

The expected rate that we accept a sample is

(cid:90)

q(y = 1) =

x)q(x) =
q(y = 1
|

x

Z
M

(27.1.12)

so that, to increase the acceptance rate, we seek the minimal M subject to p∗(x)
M q(x). In cases where
q(x) has free parameters q(x
β), the parameter β can be adjusted to minimise M . If we set q(x) = p(x)
x) will be less than
and M = Z then q(y = 1
x) = 1 and sampling is eﬃcient. In general, however, q(y = 1
|
|
1 and sampling less than ideally eﬃcient. In high dimensions for vector x, q(y = 1
x) will often be much
|
less than 1 so that rejection sampling can be extremely ineﬃcient. To see this, consider a simple scenario
in which the distributions p(x) and q(x) are factorised, p(x) = (cid:81)D

i=1 p(xi) and q(x) = (cid:81)D

i=1 q(xi). Then

≤

|

q(y = 1
|

x) =

D
(cid:89)

i=1

p∗(xi)
Miq(xi)

=

D
(cid:89)

i=1

xi) = O (cid:0)γD(cid:1)
q(y = 1
|

(27.1.13)

γ

≤

≤

xi) for each dimension. Hence the probability of accepting x
where 0
1 is the typical value of q(y = 1
|
may decrease exponentially with the number of dimensions of x. Rejection sampling is therefore a potentially
useful method of drawing independent samples in very low dimensions, but is likely to be impractical in
higher dimensions.

27.1.3 Multivariate sampling

One way to generalise the one dimensional (univariate) discrete case to a higher dimensional (multivariate)
distribution p(x1, . . . , xn) is to translate this into an equivalent one-dimensional distribution. This can be
achieved by enumerating all the possible joint states (x1, . . . , xn), giving each a unique integer i from 1 to
the total number of states, and constructing a univariate distribution with probability p(i). This then trans-
forms the multivariate distribution into an equivalent univariate distribution, and sampling can be achieved
as before. In general, of course, this procedure is impractical since the number of states grows exponentially
with the number of variables x1, . . . , xn.

(a)

(b)

Figure 27.2: Histograms of the samples from the three
. (a): 20 sam-
state distribution p(x) =
}
(b): 1000 samples. As the number of samples
ples.
increases, the relative frequency of the samples tends
to the distribution p(x).

0.6, 0.1, 0.3

{

552

DRAFT April 24, 2015

123024681230200400600Introduction

An alternative exact approach, that holds for both discrete and continuous variables is to capitalise on the
relation

p(x1, x2) = p(x2

x1)p(x1)
|

(27.1.14)

We can sample from the joint distribution p(x1, x2) by ﬁrst sampling a state for x1 from the one-dimensional
x1).
p(x1), and then, with x1 clamped to this state, sampling a state for x2 from the one-dimensional p(x2
It is clear how to generalise this to more variables by using a cascade decomposition:

|

p(x1, . . . , xn) = p(xn

xn−1, . . . , x1)p(xn−1
|

xn−2, . . . , x1) . . . p(x2
|

x1)p(x1)
|

(27.1.15)

However, in order to apply this technique, we need to know the conditionals p(xi
xi−1, . . . , x1). Unless these
|
are explicitly given we need to compute these from the joint distribution p(x1, . . . , xn). Computing such
conditionals will, in general, require the summation over an exponential number of states and, except for
small n, generally also be impractical. For belief networks, however, by construction the conditionals are
speciﬁed so that this technique becomes practical, as we discuss in section(27.2).

Drawing samples from a multivariate distribution is in general therefore a complex task and one seeks to
exploit any structural properties of the distribution to make this computationally more feasible. A common
approach is to seek to transform the distribution into a product of lower dimensional distributions, for
which the general change of variables method for continuous variables, deﬁnition(8.1), is required. A classic
example of this is sampling from a multivariate Gaussian, which can be reduced to sampling from a set of
univariate Gaussians by a suitable coordinate transformation, as discussed in example(27.1). For much of
the remaining chapter we discuss methods that are appropriate for drawing from multivariate distributions
when no obvious transformation exists to reduce the problem to a univariate form. Note that if there
nevertheless exist transformations that result even in approximately independent variables, this can still be
a very useful preprocessing step.

Example 27.1 (Sampling from a multivariate Gaussian). Our interest is to draw a sample from the mul-
tivariate Gaussian p(x) =
(x m, S). For a general covariance matrix S, p(x) does not factorise into a
product of univariate distributions. However, consider the transformation

N

y = C−1 (x

m)

−

(27.1.16)

where C is chosen so that CCT = S. Since this is a linear transformation, y is also Gaussian distributed
with mean

= (cid:10)C−1 (x

y

(cid:104)

(cid:105)

−

m)(cid:11)

p(x) = C−1 (cid:16)

x
(cid:104)

(cid:105)p(x) −

(cid:17)

m

= C−1 (m

m) = 0

−

(27.1.17)

Since the mean of y is zero, the covariance is given by

(cid:68)

yyT(cid:69)

= C−1 (cid:68)

(x

m) (x

−

−

m)T(cid:69)

p(x)

p(x)

C−T = C−1SC−T = C−1CCTC−T = I

(27.1.18)

Hence

p(y) =

N

(y 0, I) =

(cid:89)

N

i

(yi 0, 1)

(27.1.19)

A sample from y can then be obtained by independently drawing a sample from each of the univariate zero
mean unit variance Gaussians. Given a sample for y, a sample for x is obtained using

x = Cy + m

(27.1.20)

Drawing samples from a univariate Gaussian is a well-studied topic, with a popular method being the
Box-Muller technique, exercise(27.1).

DRAFT April 24, 2015

553

x1

x2

x3

x4

x5

x6

27.2 Ancestral Sampling

Belief networks take the general form:

p(x) =

(cid:89)

i

p(xi

|

pa (xi))

Ancestral Sampling

Figure 27.3: An ancestral Belief Network without any
evidential variables. To sample from this distribution,
we draw a sample from variable 1, and then variables,
2,. . . ,6 in order.

(27.2.1)

where we assume that each of the conditional distributions p(xi
pa (xi)) is speciﬁed. Provided that no
variables are evidential, we can sample from this distribution in a straightforward manner. For convenience,
we ﬁrst rename the variable indices so that parent variables always come before their children (ancestral
ordering), for example (see ﬁg(27.3))

|

p(x1, . . . , x6) = p(x1)p(x2)p(x3

x1, x2)p(x4
|

|

x3)p(x5

x3)p(x6
|

x4, x5)
|

(27.2.2)

One can sample ﬁrst from those nodes that do not have any parents (here, x1 and x2). Given these values,
one can then sample x3, and then x4 and x5 and ﬁnally x6. Despite the presence of loops in the graph, such
a forward sampling procedure is straightforward. This procedure holds for both discrete and continuous
variables. If one attempted to carry out an exact marginal inference scheme in a complex multiply connected
graph, the moralisation and triangulation steps can result in very large cliques, so that exact inference
becomes intractable. However, regardless of the loop structure, ancestral sampling remains straightforward.
Ancestral or ‘forward’ sampling is a case of perfect sampling (also termed exact sampling) since each sample
is indeed independently drawn from the required distribution. This is in contrast to Markov Chain Monte
Carlo methods sections(27.3,27.4) for which (dependent) samples are drawn from p(x) only in the limit of
a large number of iterations.

27.2.1 Dealing with evidence

How can we sample from a distribution in which a subset of variables xE are clamped to evidential states?
Writing x = xE

x\E , formally we wish to sample from

∪

p(x\E |

xE ) =

p(x\E , xE )
p(xE )

(27.2.3)

If an evidential variable xi has no parents, then one can simply set the variable into this state and continue
forward sampling as before. For example, to compute a sample from p(x1, x3, x4, x5, x6
x2) deﬁned in equa-
|
tion (27.2.2), one simply clamps x2 into its evidential state and continues forward sampling. The reason this
is straightforward is that conditioning on x2 merely deﬁnes a new distribution on a subset of the variables,
for which the belief network representation of the distribution is immediately known and remains ancestral.

On the other hand, consider sampling from p(x1, x2, x3, x4, x5

x6). Using Bayes’ rule, we have
|

p(x1, x2, x3, x4, x5

x6) =

(cid:80)

|

p(x1)p(x2)p(x3
x1,x2,x3,x4,x5 p(x1)p(x2)p(x3

x1, x2)p(x4
|

x3)p(x5
|
x1, x2)p(x4

x3)p(x6
|
x3)p(x5
|

x4, x5)
|
x3)p(x6
|

x4, x5)
|

|

(27.2.4)

The conditioning on x6 means that the structure of the distribution on the non-evidential variables changes –
for example x4 and x5 become coupled (conditioned on x3, x4 and x5 are independent; however, conditioned
on x3 and x6, x4 and x5 are dependent). One could attempt to work out an equivalent new forward sam-
pling structure, see exercise(27.3), although generally this will be as complex as running an exact inference
approach.

An alternative is to proceed with forward sampling from the non-evidential distribution, and then discard
any samples which do not match the evidential states, see exercise(27.8) for an analogous situation that

554

DRAFT April 24, 2015

Gibbs Sampling

x2

x5

x1

x4

x3

x6

Figure 27.4: The shaded nodes are the Markov blanket of x4 for the belief
x\4) we clamp x3, x5, x6
network of ﬁg(27.3). To draw a sample from p(x4
|
into their evidential states and draw a sample from p(x4
x4, x5)/Z
where Z is a normalisation constant.

x3)p(x6
|

|

justiﬁes this procedure. However, this is generally not recommended since the probability that a sample
from p(x) will be consistent with the evidence is roughly O (1/ (cid:81)
i is the number of
states of evidential variable i. In principle one can ease this eﬀect by discarding the sample as soon as any
variable state is inconsistent with the evidence. Nevertheless, the number of re-starts required to obtain a
valid sample would, on average, be very large. For this reason, alternative non-exact procedures are more
common, as we discuss in section(27.3).

i ) where dim xe

i dim xe

27.2.2 Perfect sampling for a Markov network

For a Markov network we can draw exact samples by forming an equivalent directed representation of the
graph, see section(6.8), and subsequently using ancestral sampling on this directed graph. This is achieved
by ﬁrst choosing a root clique and then consistently orienting edges away from this clique. An exact sample
can then be drawn from the Markov network by ﬁrst sampling from the root clique and then recursively
from the children of this clique. See potsample.m, JTsample.m and demoJTreeSample.m.

27.3 Gibbs Sampling

The ineﬃciency of methods such as ancestral sampling under evidence motivates alternative techniques. An
important and widespread technique is Gibbs sampling which is generally straightforward to implement.

No evidence

Assume we have a joint sample state x1 from the multivariate distribution p(x). We then consider a
particular variable, xi, for which we wish to draw a sample. Using conditioning we may write

p(x) = p(xi

x1, . . . , xi−1, xi+1, . . . , xn)p(x1, . . . , xi−1, xi+1, . . . , xn)
|

(27.3.1)

Given a joint initial state x1, from which we can read oﬀ the ‘parental’ state x1
then draw a sample x2

i from

1, . . . , x1

i−1, x1

i+1, . . . , x1

n, we

p(xi

1, . . . , x1
x1
|

i−1, x1

i+1, . . . , x1
n)

p(xi

x\i)

|

≡

(27.3.2)

1, . . . , x1

We assume this distribution is easy to sample from since it is univariate. We call this new joint sample (in
which only xi has been updated) x2 = (cid:0)x1
(cid:1). One then selects another variable xj
to sample and, by continuing this procedure, generates a set x1, . . . , xL of samples in which each xl+1 diﬀers
from xl in only a single component. Clearly, this is not an exact sampler since the resulting samples are
highly dependent. Whilst Gibbs sampling is therefore generally straightforward to implement, a drawback is
that the samples are strongly dependent. Nevertheless, as discussed in section(27.1), provided the marginal
of the sampling distribution is correct, then this is still a valid sampler. We outline in section(27.3.1) why
in the limit of a large number of samples, this holds and the sampler becomes valid.

i+1, . . . , x1
n

i−1, x2

i , x1

For a general distribution, the conditional p(xi
For a belief network, the Markov blanket of xi is

x\i) depends only on the Markov blanket of the variable xi.
|

p(xi

x\i) =
|

1
Z

p(xi

pa (xi))
|

(cid:89)

j∈ch(i)

p(xj

pa (xj))
|

(27.3.3)

see for example, ﬁg(27.4). The normalisation constant for this univariate distribution is straightforward to
work out from the requirement:
(cid:89)

(cid:88)

(27.3.4)

Z =

xi

p(xi

pa (xi))
|

j∈ch(i)

p(xj

pa (xj))

|

DRAFT April 24, 2015

555

Gibbs Sampling

Figure 27.5: A two dimensional distribution for which Gibbs sampling fails.
The distribution has mass only in the shaded quadrants. Gibbs sampling
1, xl
proceeds from the lth sample state (xl
xl
1),
|
) where xl+1
which we write (xl+1
1 = xl
1. One then continues with a
), etc. If we start in the lower left quadrant and
sample from p(x1
proceed this way, the upper right region is never explored.

2) and then sampling from p(x2

, xl+1
2
1
x2 = xl+1
|

2

In the case of a continuous variable xi the summation above is replaced with integration. Due to the local
structure of a belief network, only the parental and parents-of-children states are required in forming the
sample update.

Evidence

Evidence is readily dealt with by clamping for all samples the evidential variables into their evidential states.
There is also no need to sample for these variables, since their states are known. One then proceeds as before,
selecting a non-evidential variable and determining its distribution conditioned on its Markov blanket, and
subsequently drawing a sample from this variable.

27.3.1 Gibbs sampling as a Markov chain

In Gibbs sampling we have a sample of the joint variables xl at stage l. Based on this we produce a new
joint sample xl+1. This means that we can write Gibbs sampling as a procedure that draws from

xl+1

q(xl+1

xl)
|

∼

(27.3.5)

for some distribution q(xl+1
then Gibbs sampling corresponds to drawing samples using the Markov transition

xl). If we choose the variable to update, xi, at random from a distribution q(i),
|

q(xl+1

xl) =
|

(cid:88)

i

q(xl+1

xl, i)q(i),
|

q(xl+1

xl, i) = p(xl+1
|

i

xl
\i)
|

(cid:89)

(cid:16)

δ

xl+1
j

, xl
j

(cid:17)

j(cid:54)=i

(27.3.6)

with q(i) > 0, (cid:80)
i q(i) = 1. That is, we select a variable, then sample from its conditional, copying across
the states of the other variables from the previous sample. Our interest is to show that the stationary
distribution of q(x(cid:48)
x) is p(x). We carry this out assuming x is continuous – the discrete case is analogous:
|

(cid:90)

x

q(x(cid:48)

|

x)p(x) =

=

=

=

(cid:88)

i
(cid:88)

i

(cid:88)

i
(cid:88)

i

(cid:90)

q(i)

x
(cid:90)

x
(cid:90)

q(i)

q(i)

xi
q(i)p(x(cid:48)
i|

q(x(cid:48)

x, i)p(x)
|

(cid:89)

j(cid:54)=i

δ (cid:0)x(cid:48)

j, xj

(cid:1) p(x(cid:48)
x\i)p(xi, x\i)
i|

x(cid:48)
p(x(cid:48)
\i)p(xi, x(cid:48)
i|

\i)

x(cid:48)
\i)p(x(cid:48)

\i) =

(cid:88)

i

q(i)p(x(cid:48)) = p(x(cid:48))

(27.3.7)

@@

(27.3.8)

(27.3.9)

(27.3.10)

Hence, as long as we continue to draw samples according to the distribution q(x(cid:48)
x), in the limit of a large
number of samples we will ultimately tend to draw (dependent) samples from p(x). Any distribution q(i)
suﬃces so that visiting all variables equally often is a valid choice. Technically, we also require that q(x(cid:48)
x)
|
has p(x) as its equilibrium distribution, so that no matter in which state we start, we always converge to
p(x); see ﬁg(27.5) and section(27.3.3) for a discussion of this issue.

|

556

DRAFT April 24, 2015

1x2xGibbs Sampling

27.3.2 Structured Gibbs sampling

One can extend Gibbs sampling by using conditioning to reveal a tractable distribution on the remaining
variables. For example, consider the distribution, ﬁg(27.6a)

p(x1, x2, x3, x4) = φ(x1, x2)φ(x2, x3)φ(x3, x4)φ(x4, x1)φ(x1, x3)

(27.3.11)

In single-site Gibbs sampling we would condition on three of the four variables, and sample from the
remaining variable. For example, using x to emphasise known states,

p(x1

x2, x3, x4)
|

∝

φ(x1, x2)φ(x4, x1)φ(x1, x3)

(27.3.12)

However, we may use more limited conditioning as long as the conditioned distribution is easy to sample
from. In the case of equation (27.3.11) we can condition on x3 alone to give

p(x1, x2, x4

x3)

|

∝

φ(x1, x2)φ(x2, x3)φ(x3, x4)φ(x4, x1)φ(x1, x3)

This can be written as a modiﬁed distribution, ﬁg(27.6b)

p(x1, x2, x4

x3)

|

∝

φ(cid:48)(x1, x2)φ(cid:48)(x4, x1)

(27.3.13)

(27.3.14)

As a distribution on x1, x2, x4 this is a singly-connected linear chain Markov network from which samples
can be drawn exactly, as explained in section(27.2.2). A simple approach is to compute the normalisation
constant by any of the standard techniques, for example, using the factor graph method. One may then
convert this undirected linear chain to a directed graph, and use ancestral sampling. These operations are
linear in the number of variables in the conditioned distribution. Alternatively, one may form a junction
tree from a set of potentials, choose a root and then form a set chain by reabsorption on the junction tree.
Ancestral sampling can then be performed on the resulting oriented clique tree. This is the approach taken
in GibbsSample.m.

In the above example one can also reveal a tractable distribution by conditioning on x1,

p(x3, x2, x4

x1)
|

∝

φ(x1, x2)φ(x2, x3)φ(x3, x4)φ(x4, x1)φ(x1, x3)

(27.3.15)

and then draw a sample of x2, x3, x4 from this distribution using another exact sampler. A structured Gibbs
sampling procedure is then to ﬁrst draw a sample x1, x2, x4 from equation (27.3.13) and then a sample
x3, x2, x4 from equation (27.3.15). These two steps (of drawing exact conditional samples) are then iterated.
Note that x2 and x4 are not constrained to be equal to their values in the previous sample. This procedure
is generally to be preferred to the single-site Gibbs updating since the samples are less dependent from one
sample to the next.

See demoGibbsSample.m for a comparison of unstructured and structured sampling from a set of potentials.

27.3.3 Remarks

If the initial sample x1 is in a part of the state space that has very low probability, it may take some time
for the samples to become representative, as only a single component of x is updated at each iteration. This
motivates a so-called burn in stage in which the initial samples are discarded.

In single site Gibbs sampling there will be a high degree of dependence in any two successive samples, since
only one variable (in the single-site updating version) is updated at each stage. This motivates subsampling

x1

x4

x2

x1

x2

x3

x4

(a)

(b)

(a): A toy ‘intractable’ distribution.
Figure 27.6:
Gibbs sampling by conditioning on all variables ex-
cept one leads to a simple univariate conditional dis-
(b): Conditioning on x3 yields a new
tribution.
distribution that is singly-connected, for which exact
sampling is straightforward.

DRAFT April 24, 2015

557

Markov Chain Monte Carlo (MCMC)

(a)

(b)

Figure 27.7: Two hundred Gibbs samples for a two dimensional Gaussian. At each stage only a single
(a): For a Gaussian with low correlation, Gibbs sampling can move through the
component is updated.
likely regions eﬀectively. (b): For a strongly correlated Gaussian, Gibbs sampling is less eﬀective and does
not rapidly explore the likely regions, see demoGibbsGauss.m.

in which, say, every 10th, sample xK, xK+10, xK+20, . . ., is taken, and the rest discarded.

Due to its simplicity, Gibbs sampling is one of the most popular sampling methods and is particularly con-
venient when applied to belief networks due to the Markov blanket property1. Gibbs sampling is a special
case of the MCMC framework and, as with all MCMC methods, one should bear in mind that convergence
can be an issue since it is generally unknown how many samples are needed to be reasonably sure that a
sample estimate is accurate.

Gibbs sampling assumes that we can move throughout the space eﬀectively by only single co-ordinate up-
dates. We also require that every state can be visited inﬁnitely often. In ﬁg(27.5), we show a two dimensional
continuous distribution that has mass only in the lower left and upper right regions. In that case, if we
start in the lower left region, we will always remain there, and never explore the upper right region. This
problem occurs when two regions are not connected by a ‘probable’ Gibbs path.

A Gibbs sampler is a perfect sampler (provided we subsample appropriately) when the distribution is
factorised – that is the variables are independent. This suggests that in general Gibbs sampling will be less
eﬀective when variables are strongly correlated. For example, if we consider Gibbs sampling from a strongly
correlated two variable Gaussian distribution, then updates will move very slowly in space, see ﬁg(27.7). It
is useful therefore to ﬁnd variable transformations that render the new variables approximately independent,
since then one may apply Gibbs sampling more eﬀectively.

27.4 Markov Chain Monte Carlo (MCMC)

We assume we have a multivariate distribution in the form

p(x) =

1
Z

p∗(x)

(27.4.1)

where p∗(x) is the unnormalised distribution and Z = (cid:82)
x p∗(x) is the (computationally intractable) normal-
isation constant. We assume we are able to evaluate p∗(x = x), for any state x, but not p(x = x) since
Z is intractable. The idea in MCMC sampling is to sample, not directly from p(x), but from a diﬀerent
distribution such that, in the limit of a large number of samples, eﬀectively the samples will be from p(x).
To achieve this we forward sample from a Markov transition whose stationary distribution is equal to p(x).

1The BUGS package www.mrc-bsu.cam.ac.uk/bugs is general purpose software for sampling from belief networks.

558

DRAFT April 24, 2015

−4−3−2−101234−3−2−10123−3−2−10123−2−1.5−1−0.500.511.52Markov Chain Monte Carlo (MCMC)

27.4.1 Markov chains

Consider the conditional distribution q(xl+1
generate samples x1, x2, . . . , xL. After a long time L
meaning that we can eventually get from any state to any other state
periodically revisit any state) the samples are from the
as (for a continuous variable)

xl). If we are given an initial sample x1, then we can recursively
|
1, (and provided the Markov chain is ‘irreducible’,
and ‘aperiodic’, meaning we don’t
unique stationary distribution q∞(x) which is deﬁned

(cid:29)

@@
@@

q∞(x(cid:48)) =

(cid:90)

x

q(x(cid:48)

x)q∞(x)
|

(27.4.2)

The condition for a discrete variable is analogous on replacing integration with summation. The idea in
MCMC is, for a given distribution p(x), to ﬁnd a transition q(x(cid:48)
x) which has p(x) as its stationary distri-
|
bution. If we can do so, then we can draw samples from the Markov chain by forward sampling and take
these as samples from p(x) as the chain converges towards its stationary distribution.

Note that for every distribution p(x) there will be more than one transition q(x(cid:48)
x) with p(x) as its sta-
tionary distribution. This is why there are many diﬀerent MCMC sampling methods, each with diﬀerent
characteristics and varying suitability for the particular distribution at hand. We’ve already encountered
one class of samplers from the MCMC family, namely Gibbs sampling which corresponds to a particular
transition q(x(cid:48)

x). Below we discuss more general members of this family.
|

|

(27.4.3)

1 a positive function. This deﬁnes a

≤

(27.4.4)

x) is equal to p(x) for any proposal
|

27.4.2 Metropolis-Hastings sampling

Consider the following transition

q(x(cid:48)

x) = ˜q(x(cid:48)
|

|

x)f (x(cid:48), x) + δ(x(cid:48), x)

(cid:18)
1

(cid:90)

−

x(cid:48)(cid:48)

(cid:19)

˜q(x(cid:48)(cid:48)

x)f (x(cid:48)(cid:48), x)
|

where ˜q(x(cid:48)
x) is a so-called proposal distribution and 0 < f (x(cid:48), x)
|
valid distribution q(x(cid:48)
x) since it is non-negative and

|

(cid:90)

x(cid:48)

q(x(cid:48)

x) =
|

(cid:90)

x(cid:48)

x)f (x(cid:48), x) + 1

˜q(x(cid:48)

|

(cid:90)

−

x(cid:48)(cid:48)

˜q(x(cid:48)(cid:48)

|

x)f (x(cid:48)(cid:48), x) = 1

Our interest is to set f (x(cid:48), x) such that the stationary distribution of q(x(cid:48)
˜q(x(cid:48)

x). That is

|

p(x(cid:48)) =

(cid:90)

x

q(x(cid:48)

x)p(x) =
|

(cid:90)

x

˜q(x(cid:48)

x)f (x(cid:48), x)p(x) + p(x(cid:48))
|

(cid:18)
1

(cid:90)

−

x(cid:48)(cid:48)

(cid:19)

˜q(x(cid:48)(cid:48)

x(cid:48))f (x(cid:48)(cid:48), x(cid:48))
|

In order that this holds, we require (changing the integral variable from x(cid:48)(cid:48) to x)

(cid:90)

x

˜q(x(cid:48)

x)f (x(cid:48), x)p(x) =
|

(cid:90)

x

˜q(x

x(cid:48))f (x, x(cid:48))p(x(cid:48))
|

Now consider the Metropolis-Hastings acceptance function

f (x(cid:48), x) = min

(cid:18)

1,

(cid:19)

˜q(x
|
˜q(x(cid:48)

x(cid:48))p(x(cid:48))
x)p(x)
|

(cid:18)

= min

1,

(cid:19)

x(cid:48))p∗(x(cid:48))
˜q(x
|
x)p∗(x)
˜q(x(cid:48)
|

which is deﬁned for all x, x(cid:48) and has the ‘detailed balance’ property

f (x(cid:48), x)˜q(x(cid:48)

x)p(x) = min (cid:0)˜q(x(cid:48)
|
= min (cid:0)˜q(x

x(cid:48))p(x(cid:48))(cid:1)
x)p(x), ˜q(x
|
|
x)p(x)(cid:1) = f (x, x(cid:48))˜q(x
x(cid:48))p(x(cid:48)), ˜q(x(cid:48)
|
|

x(cid:48))p(x(cid:48))

|

(27.4.5)

(27.4.6)

(27.4.7)

(27.4.8)

(27.4.9)

Hence the function f (x(cid:48), x) as deﬁned above ensures equation (27.4.6) holds and that q(x(cid:48)
stationary distribution.

x) has p(x) as its
|

How do we sample from q(x(cid:48)
proportional to ˜q(x(cid:48)

x)f (x(cid:48), x) and the other δ(x(cid:48), x) with mixture coeﬃcient 1
|

x)? Equation(27.4.3) can be interpreted as a mixture of two distributions, one
|
x)f (x(cid:48)(cid:48), x). To
|

x(cid:48)(cid:48) ˜q(x(cid:48)(cid:48)

−

(cid:82)

DRAFT April 24, 2015

559

Algorithm 27.3 Metropolis-Hastings MCMC sampling.

Markov Chain Monte Carlo (MCMC)

1: Choose a starting point x1.
2: for i = 2 to L do
3:

Draw a candidate sample xcand from the proposal ˜q(x(cid:48)
Let a = ˜q(xl−1|xcand)p(xcand)
˜q(xcand|xl−1)p(xl−1)
if a
else

1 then xl = xcand

≥

xl−1).

|

draw a random value u uniformly from the unit interval [0, 1].
if u < a then xl = xcand
else

xl = xl−1

end if

4:

5:

6:

7:

8:

9:

10:

11:

(cid:46) Accept the candidate

(cid:46) Accept the candidate

(cid:46) Reject the candidate

end if

12:
13: end for

Figure 27.8: Metropolis-Hastings samples from a bi-variate
distribution p(x1, x2) using a proposal ˜q(x(cid:48)
(x(cid:48) x, I).
We also plot the iso-probability contours of p. Although
p(x) is multi-modal, the dimensionality is low enough and
the modes suﬃciently close such that a simple Gaussian pro-
posal distribution is able to bridge the two modes. In higher
dimensions, such multi-modality is more problematic. See
demoMetropolis.m

x) =
|

N

x) and accept this with probability f (x(cid:48), x). Since
draw a sample from this, we draw a sample from ˜q(x(cid:48)
|
drawing from ˜q(x(cid:48)
x) and accepting are performed independently, the probability of accepting the drawn
|
candidate is the product of these probabilities, namely ˜q(x(cid:48)
x)f (x(cid:48), x). Otherwise the candidate is rejected
|
and we take the sample x(cid:48) = x. Using the properties of the acceptance function, equation (27.4.7), the
following is equivalent to the above procedure: When

˜q(x

|

x(cid:48))p∗(x(cid:48)) > ˜q(x(cid:48)

x)p∗(x)
|

(27.4.10)

x(cid:48))p∗(x(cid:48))/˜q(x(cid:48)

we accept the sample from ˜q(x(cid:48)
x) with probability
|
If we reject the candidate we take x(cid:48) = x. Note that if the candidate x(cid:48) is
˜q(x
|
rejected, we take the original x as the new sample. Hence each iteration of the algorithm produces a sample
– either a copy of the current sample, or the candidate sample, see algorithm(27.3). A rough rule of thumb
is to choose a proposal distribution for which the acceptance rate is between 50% and 85%[115].

x). Otherwise we accept the sample x(cid:48) from q(x(cid:48)
|

x)p∗(x).
|

Gaussian proposal distribution

A common proposal distribution for vector x is
(cid:0)x(cid:48) x, σ2I(cid:1)

2σ2 (x(cid:48)−x)2

e− 1

˜q(x(cid:48)

∝

for which ˜q(x(cid:48)

x) =
|

N
x) = ˜q(x
|
(cid:18)
f (x(cid:48), x) = min

1,

|

x(cid:48)) and the acceptance criterion equation (27.4.7) becomes

(cid:19)

p∗(x(cid:48))
p∗(x)

(27.4.11)

(27.4.12)

If the unnormalised probability p∗(x(cid:48)) of the candidate state is higher than the current state, p∗(x), we
therefore accept the candidate. Otherwise, we accept the candidate only with probability p∗(x(cid:48))/p∗(x). If

560

DRAFT April 24, 2015

Auxiliary Variable Methods

(a)

(b)

(c)

(a): Multi-modal distribution p(x) for which we desire samples.

(b):
Figure 27.9: Hybrid Monte Carlo.
(c): This is a plot of (b) from above.
HMC forms the joint distribution p(x)p(y) where p(y) is Gaussian.
Starting from the point x, we ﬁrst draw a y from the Gaussian p(y), giving a point (x, y), given by the
green line. Then we use Hamiltonian dynamics (white line) to traverse the distribution at roughly constant
energy for a ﬁxed number of steps, giving x(cid:48), y(cid:48). We accept this point if H(x(cid:48), y(cid:48)) > H(x, y(cid:48)) and make the
H(x, y(cid:48))). If
new sample x(cid:48) (red line). Otherwise this candidate is accepted with probability exp(H(x(cid:48), y(cid:48))
rejected the new sample x(cid:48) is taken as a copy of x.

−

the candidate is rejected, the new sample is taken to be a copy of the previous sample x. See ﬁg(27.8) for
a demonstration.

In high dimensions it is unlikely that a random candidate sampled from a Gaussian will result in a candidate
probability higher than the current value, exercise(27.5). Because of this, only very small jumps (σ2 small)
are likely to be accepted. This limits the speed at which we explore the space x and increases the dependency
between samples. As an aside, the acceptance function (27.4.12) highlights that sampling is diﬀerent from
ﬁnding the optimum. Provided x(cid:48) has a higher probability than x, we accept x(cid:48). However, we may also
accept candidates that have a lower probability than the current sample.

27.5 Auxiliary Variable Methods

A practical concern in MCMC methods is ensuring that one moves eﬀectively through the signiﬁcant proba-
bility regions of the distribution. For methods such as Metropolis-Hastings with local proposal distributions
(local in the sense they are unlikely to propose a candidate far from the current sample), if the target distri-
bution has isolated islands of high density, then the chance that we would move from one island to the other
is very small. Conversely, if we attempt to make the proposal less local by using one with a high variance
the chance then of landing at random on a high density island is remote. Auxiliary variable methods use
additional dimensions to aid exploration and in certain cases to provide a bridge between isolated high
density islands. See also [213] for the use of auxiliary variables in perfect sampling.

Consider drawing samples from p(x). For an auxiliary variable y we introduce a distribution p(y
the joint distribution

x) to form
|

p(x, y) = p(y

x)p(x)
|

(27.5.1)

If we draw samples (xl, yl) from this joint distribution then a valid set of samples from p(x) is given by
taking the xl alone. If we sampled x directly from p(x) and then y from p(y
x), introducing y is pointless
since there is no eﬀect on the x sampling procedure. In order for this to be useful, therefore, the auxiliary
variable must inﬂuence how we sample x. Below we discuss some of the common auxiliary variable schemes.

|

27.5.1 Hybrid Monte Carlo

Hybrid MC is a method for continuous variables that aims to make non-local jumps in the sample space
and, in so doing, to jump potentially from one mode to another. We deﬁne the distribution from which we

DRAFT April 24, 2015

561

−5−4−3−2−101234500.511.522.53x 10−3Algorithm 27.4 Hybrid Monte Carlo sampling

Auxiliary Variable Methods

1: Start from x1
2: for i = 1 to L do
3:

4:

5:

6:

Draw a new sample y from p(y).
Choose a random (forwards or backwards) trajectory direction.
Starting from xi, y, follow Hamiltonian dynamics for a ﬁxed number of steps, giving a candidate x(cid:48), y(cid:48).
Accept the candidate xi+1 = x(cid:48)
if H(x(cid:48), y(cid:48)) > H(x, y), otherwise accept it with probability
H(x, y)).

exp(H(x(cid:48), y(cid:48))

If rejected, we take the sample as xi+1 = xi.

−

7:
8: end for

wish to sample as

p(x) =

1
Zx

eHx(x)

(27.5.2)

for some given ‘Hamiltonian’ Hx(x). We then deﬁne another, ‘easy’ auxiliary distribution from which we
can readily generate samples,

p(y) =

1
Zy

eHy(y)

so that the joint distribution is given by

p(x, y) = p(x)p(y) =

1
Z

eHx(x)+Hy(y) =

1
Z

eH(x,y),

H(x, y)

≡

H(x) + H(y)

(27.5.3)

(27.5.4)

In the standard form of the algorithm, a multi-dimensional Gaussian is chosen for the auxiliary distribution
with dim (y) = dim (x), so that

Hy(y) =

1
2

−

yTy

(27.5.5)

The HMC algorithm ﬁrst draws from p(y) and subsequently from p(x, y). For a Gaussian p(y), sampling
from this is straightforward. In the next ‘dynamic’ step, a sample is drawn from p(x, y) using a Metropolis
MCMC sampler. The idea is to go from one point of the space x, y to a new point x(cid:48), y(cid:48) that is a non-trivial
distance from x, y and which will be accepted with a high probability. The candidate (x(cid:48), y(cid:48)) will have a
good chance to be accepted if H(x(cid:48), y(cid:48)) is close to H(x, y) – this can be achieved by following a contour of
equal ‘energy’ H, as described in the next section.

Hamiltonian dynamics

We wish to make an update x(cid:48) = x + ∆x, y(cid:48) = y + ∆y for small ∆x and ∆y such that the Hamiltonian
H(x, y)

Hx(x) + Hy(y) is conserved,

≡
H(x(cid:48), y(cid:48))

H(x, y)

≈

We can satisfy this (up to ﬁrst order) by considering the Taylor expansion

H(x(cid:48), y(cid:48)) = H(x + ∆x, y + ∆y)

H(x, y) + ∆xT

≈

xH(x, y) + ∆yT

∇

yH(x, y) + O (cid:0)

∆x

|

2(cid:1) + O (cid:0)
|

∆y
|

2(cid:1)
|

∇

Conservation, up to ﬁrst order, therefore requires

∆xT

∇

xH(x, y) + ∆yT

yH(x, y) = 0

∇

This is a single scalar requirement, and there are therefore many diﬀerent solutions for ∆x and ∆y that
satisfy this single condition. It is customary to use Hamiltonian dynamics, which corresponds to the setting:

∆x = (cid:15)

∇

yH(x, y),

∆y =

(cid:15)
∇
−

xH(x, y)

562

(27.5.9)

DRAFT April 24, 2015

(27.5.6)

(27.5.7)

(27.5.8)

For the HMC method,
y so that

yHy(y) =

∇

−
x(t + 1) = x(t)

Auxiliary Variable Methods

(a)

(b)

(c)

(a): Current sample of states
Figure 27.10: Swendson-Wang updating for p(x)
(here on a nearest neighbour lattice). (b): Like coloured neighbours are bonded together with probability
e−β, forming clusters of variables. (c): Each cluster is given a random colour, forming the new sample.
1

i∼j exp βI [xi = xj].

∝

(cid:81)

−

where (cid:15) is a small value to ensure that the Taylor expansion is accurate. Hence

x(t + 1) = x(t) + (cid:15)

∇

yHy(y),

y(t + 1) = y(t)

(cid:15)
∇

−

xHx(x)

(27.5.10)

xH(x, y) =

∇

∇

xHx(x) and

∇

yH(x, y) =

∇

yHy(y). For the Gaussian case,

(cid:15)y,

−

y(t + 1) = y(t)

(cid:15)
∇

−

xH(x)

(27.5.11)

There are speciﬁc ways to implement the Hamiltonian dynamics called Leapfrog discretisation that are more
accurate than the simple time-discretisation used above and we refer the reader to [220] for details.

−

In order to make a symmetric proposal distribution, at the start of the dynamic step, we choose (cid:15) = +(cid:15)0
(cid:15)0 uniformly. We then follow the Hamiltonian dynamics for many time steps (usually of the order
or (cid:15) =
of several hundred) to reach a candidate point x(cid:48), y(cid:48). If the Hamiltonian dynamics is numerically accurate,
H(x(cid:48), y(cid:48)) will have roughly the same value as H(x, y). We then do a Metropolis step, and accept the point
x(cid:48), y(cid:48) if H(x(cid:48), y(cid:48)) > H(x, y) and otherwise accept it with probability exp(H(x(cid:48), y(cid:48))
H(x, y)). If rejected,
we take the initial point x, y as the sample. Combined with the p(y) sample step, we then have the general
procedure as described in algorithm(27.4).

−

In HMC we use not just the potential Hx(x) to deﬁne candidate samples, but the gradient of Hx(x) as
well. An intuitive explanation for the success of the algorithm is that it is less myopic than straightforward
Metropolis since the gradient enables the algorithm to feel its way to other regions of high probability by
contouring around paths in the augmented space. One can also view the auxiliary variables as momentum
variables – it is as if the sample has now a momentum which can carry it through the low-density x-regions.
Provided this momentum is high enough, we can escape local regions of signiﬁcant probability, see ﬁg(27.9).

27.5.2 Swendson-Wang

Originally, the SW method was introduced to alleviate the problems encountered in sampling from Ising
models close to their critical temperature[284]. At this point large islands of same-state variables form so
that strong correlations appear in the distribution – the scenario under which, for example, Gibbs sampling
is not well suited. The method has since been generalised to other models[94], although here we outline the
procedure for the Ising model only, referring the reader to more specialised text for the extensions [38].

The Ising model with no external ﬁelds is deﬁned on variables x = (x1, . . . , xn), xi
form

0, 1
}

∈ {

and takes the

p(x) =

1
Z

(cid:89)

i∼j

eβI[xi=xj ]

(27.5.12)

which means that this is a pairwise Markov network with a potential contribution eβ if neighbouring nodes
i and j on a square lattice are in the same state, and a contribution 1 otherwise. We assume that β > 0
which encourages neighbours to be in the same state. The lattice based neighbourhood structure makes
0.9 which encourages large scale islands of same-state
this diﬃcult to sample from, and especially when β

≈

DRAFT April 24, 2015

563

Algorithm 27.5 Swendson-Wang sampling

1: Start from a random conﬁguration of all x1
2: for l = 2 to L do
3:

for each i, j in the edge set do

1, . . . , x1
n.

If xl−1

i = xl−1

j

, we bind variables xi and xj with probability 1

−

end for
For each cluster formed from the above bonds, set the state of the cluster uniformly at random.
This gives a new joint conﬁguration xl
1, . . . , xl
n.

4:

5:

6:

7:
8: end for

Auxiliary Variable Methods

e−β.

variables to form.

The aim is to remove the problematic terms eβI[xi=xj ] by the use of auxiliary real-valued ‘bond’ variables,
yij, one for each edge on the lattice, making the conditional p(x

y) easy to sample from. This is given by
|

p(x

y)
|

∝

p(y

x)p(x)
|

∝

p(y

x)
|

eβI[xi=xj ]

(cid:89)

i∼j

Using p(y

x) we can cancel the terms eβI[xi=xj ] by setting
|

p(y

x) =
|

(cid:89)

i∼j

p(yij

|

xi, xj) =

(cid:89)

i∼j

I

1
zij

0 < yij < eβI[xi=xj ](cid:105)
(cid:104)

(27.5.13)

(27.5.14)

where I (cid:2)0 < yij < eβI[xi=xj ](cid:3) denotes a uniform distribution between 0 and eβI[xi=xj ]; zij is the normalisation
constant zij = eβI[xi=xj ]. Hence

p(x

y)
|

∝

∝

∝

p(y
(cid:89)

i∼j
(cid:89)

i∼j

0 < yij < eβI[xi=xj ](cid:105)
(cid:104)

I

x)p(x)
|
1
eβI[xi=xj ]
0 < yij < eβI[xi=xj ](cid:105)
(cid:104)

I

eβI[xi=xj ]

(27.5.15)

(27.5.16)

(27.5.17)

. If yij > 1, then to draw a sample from
We ﬁrst assume we have a sample for all the bond variables
}
y), we must have 1 < eβI[xi=xj ], which means that xi and xj are constrained to be in the same state.
p(x
|
Otherwise, if yij < 1, then this introduces no constraint on xi and xj. Hence, wherever yij > 1, we bind xi
and xj to be in the same state, otherwise not.

yij
{

To sample from the bond variables p(yij
state. Then p(yij

xi = xj) = U (cid:0)yij

xi, xj) consider ﬁrst the situation that xi and xj are in the same
|

(cid:2)0, eβ(cid:3)(cid:1). A bond will occur if yij > 1, which occurs with probability

|

p(yij > 1

xi = xj) =
|

(cid:90) ∞

yij =1

1
zij

(cid:104)

0 < yij < eβ(cid:105)

I

=

eβ

1
eβ = 1
−

−

e−β

(27.5.18)

Hence, if xi = xj, we bind xi and xj to be in the same state with probability 1
and xj are in diﬀerent states, p(yij

e−β. On the other hand if xi
[0, 1]), and yij is uniformly distributed between 0 and 1.

= xj) = U (yij

−

|

|

xi
|

Figure 27.11: Ten successive samples from a 25
, with β =
0.88, close to the critical temperature. The Swendson-Wang procedure is used. Starting in a random initial
conﬁguration, the samples quickly move away from this initial state. The samples display the characteristic
long-range correlations close to the critical temperature.

25 Ising model p(x)

exp

×

∝

(cid:16)(cid:80)

(cid:17)
i∼j βI [xi = xj]

564

DRAFT April 24, 2015

(cid:54)
Auxiliary Variable Methods

Figure 27.12: The full slice for a given y.
Ideally slice sampling would
draw an x sample from anywhere on the full slice (green). In general this
is intractable for a complex distribution and a local approximate slice is
formed instead, see ﬁg(27.13).

(a)

(c)

(b)

(d)

(a): For the current sample x, a point y is sampled between 0 and p∗(x), giving a point
Figure 27.13:
(x, y) (black circle). Then an interval of width w is placed around x, the blue bar. The ends of the bar
(b): The interval is increased by w
denote if the point is in the slice (green) or out of the slice (red).
(c): Given an interval a sample x(cid:48) is taken uniformly in the interval.
until it hits a point out of the slice.
If the candidate x(cid:48) is not in the slice (red), p(x(cid:48)) < y, the candidate is rejected and the interval is shrunk.
(d): The sampling from the interval is repeated until a candidate is in the slice (green), and is subsequently
accepted.

After doing this for all the xi and xj pairs, we will end up with a graph in which we have clusters of like-
state bonded variables. The algorithm then simply chooses a random state for each cluster – that is, with
probability 0.5 all variables in the cluster are in state 1. This is useful since we are able to update variables,
even if they are strongly dependent. This is in contrast to say Gibbs sampling which when the variables
are strongly dependent will only very rarely change the state of a variable from its neighbours since it is
strongly encouraged to agree with them; this results in a critical slowing down eﬀect and Gibbs sampling
essentially freezes. See algorithm(27.5), ﬁg(27.10) and ﬁg(27.11). This technique has found application in
spatial statistics, particularly image restoration[146].

27.5.3 Slice sampling

Slice sampling[222] is an auxiliary variable technique that aims to overcome some of the diﬃculties in choos-
ing an appropriate length scale in methods such as Metropolis sampling. The brief discussion here follows
that presented in [197] and [43]. We want to draw samples from p(x) = 1
Z p∗(x) where the normalisation
constant Z is unknown. By introducing the auxiliary variable y and deﬁning the distribution

p(x, y) =

(cid:26) 1/Z for 0

y
otherwise

≤

p∗(x)

≤

0

we have
(cid:90)

p(x, y)dy =

(cid:90) p∗(x)

0

1
Z

dy =

1
Z

p∗(x) = p(x)

(27.5.19)

(27.5.20)

which shows that the marginal of p(x, y) over y is equal to the distribution we wish to draw samples from.
Hence if we draw samples from p(x, y), we can ignore the y samples and we will have a valid sampling scheme
for p(x).

To draw from p(x, y) we use Gibbs sampling, ﬁrst drawing from p(y
sample from p(y

x) means that we draw a value y from the uniform distribution U (y
|

x) and then from p(x

|

|

y). Drawing a
|
[0, p∗(x)]). Given

DRAFT April 24, 2015

565

Algorithm 27.6 Slice Sampling (univariate).

1: Choose a starting point x1 and step size w.
2: for i = 1 to L do
3:

Draw a vertical coordinate y uniformly from the interval (cid:0)0, p∗(xi)(cid:1).
Create a horizontal interval (xleft, xright) that contains xi as follows:
Draw r
∼
xleft = xi
−
while p∗(xleft) > y do

rw, xright = xi + (1

(0, 1))

U (r

r)w

−

|

xleft = xleft

w

−

end while
while p∗(xright) > y do
xright = xright + w

Importance Sampling

(cid:46) Create an initial interval

(cid:46) Step out left

(cid:46) Step out right

end while
accept = false
while accept = false do

draw a random value x(cid:48) uniformly from the unit interval (xleft, xright).
if p∗(x(cid:48)) > y then
accept = true

(cid:46) Found a valid sample

else

modify the interval (xleft, xright) as follows:
if x(cid:48) > xi then
xright = x(cid:48)

else

xleft = x(cid:48)

end if

(cid:46) Shrinking

4:

5:

6:

7:

8:

9:

10:

11:

12:

13:

14:

15:

16:

17:

18:

19:

20:

21:

22:

23:

24:

25:

26:

end if
end while
xi+1 = x(cid:48)

27:
28: end for

a sample y, one then draws a sample x from p(x
distribution over x such that p∗(x) > y:

y). Using p(x
|

|

y)

∝

p(x, y) we see that p(x

y) is the
|

p(x

y)
|

∝

I [p∗(x) > y]

(27.5.21)

That is, x is uniformly distributed from the ‘slice’ that satisﬁes p∗(x) > y, ﬁg(27.12). Computing the nor-
malisation of this distribution is in general non-trivial since we would in principle need to search over all
x to ﬁnd those for which p∗(x) > y. The challenge in slice sampling is therefore to sample from the slice.
This can be addressed using MCMC techniques. Ideally we would like to get as much of the slice as feasible,
since this will improve the ‘mixing’ of the chain (the rate that we converge to the stationary distribution).
If we concentrate on the part of the slice only very local to the current x, then the samples move through
the space very slowly. If we attempt to guess at random a point a long way from x and check if it is in the
slice, this will be mostly unsuccessful.

We will not discuss how to do this in general and highlight only how this can be achieved for a univariate
slice. The happy compromise presented in algorithm(27.6)[222] and described in ﬁg(27.13) determines an
appropriate incremental widening of an initial interval. Once the largest potential interval is determined
we attempt to sample from this. If the sample point within the interval is in fact not in the slice, this is
rejected and the interval is shrunk.

27.6 Importance Sampling

Importance sampling is a technique to approximate averages with respect to an intractable distribution
p(x). The term ‘sampling’ is arguably a misnomer since the method does not attempt to draw samples from

566

DRAFT April 24, 2015

Importance Sampling

Z where p∗(x) can be evaluated but Z = (cid:82)

p(x). Rather the method draws samples from a simpler importance distribution q(x) and then reweights
them such that averages with respect to p(x) can be approximated using the samples from q(x). Consider
p(x) = p∗(x)
x p∗(x) is an intractable normalisation constant. The
average of f (x) with respect to p(x) is given by
(cid:82)
x f (x) p∗(x)
(cid:82)

f (x)p(x) =

q(x) q(x)

(27.6.1)

=

(cid:90)

(cid:82)
x f (x)p∗(x)
(cid:82)
x p∗(x)

x

p∗(x)
q(x) q(x)

x

Let x1, . . . , xL be samples from q(x). We can then approximate the above average by

(cid:90)

x

f (x)p(x)

≈

(cid:80)L

l=1 f (xl) p∗(xl)
(cid:80)L

q(xl)

p∗(xl)
q(xl)

l=1

=

L
(cid:88)

l=1

f (xl)wl

where we deﬁne the normalised importance weights

wl =

p∗(xl)/q(xl)
l=1 p∗(xl)/q(xl)

(cid:80)L

,

with

L
(cid:88)

l=1

wl = 1

(27.6.2)

(27.6.3)

In principle, reweighing the samples from q will give the correct result for the average with respect to p.

The importance weight vector w can be seen as a measure of how well q ﬁts p since for q = p, the weight
vector has uniform values, w = 1/L. Hence the more variable the components of the weight vector are,
the less well-matched q is to p. Since the weight is a measure of how well q matches p, unless q and p are
well-matched, there will typically be only a small number of dominant weights. This is particularly evident
in high dimensions. As an indication of this eﬀect, consider a D-dimensional multivariate x. Using u as
the vector of unnormalised importance weights with ui = p(xi)/q(xi), a measure of the variability of two
components of u is given by (dropping momentarily the notational dependence on x)

(cid:68)

(ui

−

uj)2(cid:69)

= (cid:10)u2
i

(cid:11) + (cid:10)u2
j

(cid:11)

2

ui
(cid:104)

−

uj

(cid:105) (cid:104)

(cid:105)

where the averages are with respect to q. The mean unnormalised weights are

=

ui
(cid:104)

(cid:105)

uj

(cid:104)

(cid:105)

=

(cid:90)

x

p(x)
q(x)

q(x) = 1

and

(cid:10)u2
i

(cid:11) = (cid:10)u2
j

(cid:11) =

(cid:29)

(cid:28) p2(x)
q2(x)

q(x)

(cid:28) p(x)
q(x)

=

(cid:29)

p(x)

(27.6.4)

(27.6.5)

(27.6.6)

For simplicity we assume that q(x) and p(x) both factorise, p(x) = (cid:81)D
further that the distributions are axis-aligned, being the same for each co-ordinate d. Then

d=1 p(xd), q(x) = (cid:81)D

d=1 q(xd) and

(cid:10)u2
i

(cid:11) =

(cid:28) p(x)
q(x)

(cid:29)D

p(x)

(27.6.7)

Since

(cid:69)

(cid:68) p(x)
q(x)

p(x)

> 1 for q

= p (this is the 2-divergence, exercise(8.37)), the variability of the weights is

(cid:68)

(ui

−

uj)2(cid:69)

= 2

(cid:32)(cid:28) p(x)
q(x)

(cid:33)

1

(cid:29)D

p(x) −

(27.6.8)

which grows exponentially with the dimension D. This means that a single unnormalised importance weight
is likely to dominate. After normalisation, typically, therefore in high dimensions the weight vector w will
have only a single signiﬁcantly non-zero component.

A method that can help address this weight dominance is resampling. Given the weight distribution
w1, . . . , wL, one draws a set of L sample indices. This new set of indices will almost certainly contain
repeats since any of the original low-weight samples will most likely not be included. The weight of each of
these new samples is set uniformly to 1/L. This procedure helps select only the ‘ﬁttest’ of the samples and
is known as Sampling Importance Resampling[253].

DRAFT April 24, 2015

567

(cid:54)
Importance Sampling

h1

v1

h2

v2

h3

v3

h4

v4

Figure 27.14: A dynamic belief network. In many ap-
ht)
plications of interest, the emission distribution p(vt
|
is non-Gaussian, leading to the formal intractability of
ﬁltering/smoothing.

27.6.1 Sequential importance sampling

1:t from q(x1:t). If we assume that for the previous time, we have a set of paths xl

Our interest is to apply importance sampling to temporal distributions p(x1:t) for which the importance
samples from q(x1:t) are paths x1:t. When a new observation at time t arrives, we require a set of sample
paths xl
1:t−1 along with
their importance weights wl
t by simply sampling from the
transition q(xt
x1:t−1) and updating the weights, without needing to explicitly sample whole new paths. To
|
see this, consider the unnormalised importance weights for a sample path xl

t−1, we can ﬁnd the new importance weights wl

1:t

˜wl

t =

p∗(xl
q(xl

1:t)
1:t)

=

p∗(xl
q(xl

1:t−1)
1:t−1)

p∗(xl
1:t)
1:t−1)q(xl
t|

p∗(xl

,

xl
1:t−1)

˜wl

1 =

p∗(xl
1)
q(xl
1)

(27.6.9)

Using p(x1:t) = p(xt
weights recursively using

x1:t−1)p(x1:t−1), we can ignore constants and equivalently deﬁne the unnormalised
|

˜wl

t = ˜wl

t−1αl
t,

where

αl
t ≡

xl
p∗(xl
1:t−1)
t|
q(xl
xl
1:t−1)
t|

t > 1

(27.6.10)

(27.6.11)

This means that in Sequential Importance Sampling (SIS) we need only deﬁne the conditional impor-
tance distribution q(xt
x1:t−1) =
|
p(xt
x1:t−1), although this choice is impractical in most cases, as we discuss below. Sequential importance
|
sampling is also known as particle ﬁltering.

x1:t−1). The ideal setting of the sequential importance distribution is q(xt
|

Dynamic belief networks

Consider distributions with a hidden Markov independence structure, see ﬁg(27.14),

p(v1:t, h1:t) = p(v1

h1)p(h1)
|

t
(cid:89)

t=2

p(vt
ht)
|
(cid:124) (cid:123)(cid:122) (cid:125)
emission

p(ht
(cid:124)

ht−1)
|
(cid:125)
(cid:123)(cid:122)
transition

(27.6.12)

where v1:t are observations and h1:t are the random variables. Our interest is to draw sample paths h1:t
given the observations v1:t. In some models, such as the HMM, this is straightforward. However, in other
cases, for example when the emission p(vt
ht) is intractable to normalise, we can use SIS to draw samples
|
instead. For dynamic belief networks, equation (27.6.11) simpliﬁes to

αl
t ≡

p(vt

t)p(hl
hl
hl
t−1)
t|
|
hl
q(hl
1:t−1)
t|

The optimal importance distribution is achieved by using the importance transition

q(ht

h1:t−1)
|

∝

p(vt

ht)p(ht
|

ht−1)
|

(27.6.13)

(27.6.14)

It is often the case, however, that drawing samples from this optimal q(ht
the transition is easy to sample from, a common sequential importance distribution is

h1:t−1)

|

is diﬃcult. In cases where

@@

q(ht

h1:t−1) = p(ht
|

ht−1)
|

in which case, from equation (27.6.13), αl
by

t = p(vt

˜wl

t = ˜wl

t−1p(vt

hl
t)

|

ht) and the unnormalised weights are recursively deﬁned
|

(27.6.15)

(27.6.16)

A drawback of this procedure is that after a small number of iterations only very few particle weights will be
signiﬁcantly non-zero due to the mismatch between the importance distribution q and the target distribution
p. This can be addressed using resampling, as described in section(27.6)[153, 87].

568

DRAFT April 24, 2015

Importance Sampling

27.6.2 Particle ﬁltering as an approximate forward pass

Particle ﬁltering can be viewed as an approximation to the exact ﬁltering recursion. Using ρ to represent
the ﬁltered distribution,

ρ(ht)

p(ht

v1:t)
|

∝

the exact ﬁltering recursion is

(cid:90)

ρ(ht)

p(vt

ht)
|

∝

p(ht

ht−1)ρ(ht−1)
|

ht−1

(27.6.17)

(27.6.18)

A PF can be viewed as an approximation of equation (27.6.18) in which the message ρ(ht−1) is approximated
by a sum of delta-spikes:

ρ(ht−1)

L
(cid:88)

l=1

≈

(cid:16)

wl

t−1δ

ht−1, hl

t−1

(cid:17)

(27.6.19)

t−1 are the normalised importance weights (cid:80)L

where wl
t−1 are the particles. In other
words, the ρ message is represented as a weighted mixture of delta-spikes where the weight and position of
the spikes are the parameters of the distribution. Using equation (27.6.19) in equation (27.6.18), we have

t−1 = 1, and hl

l=1 wl

ρ(ht)

1
Z

≈

p(vt

ht)

|

L
(cid:88)

l=1

t−1)wl
hl

t−1

p(ht

|

(27.6.20)

The constant Z is used to normalise the distribution ρ(ht). Although ρ(ht−1) was a simple sum of delta-
spikes, in general ρ(ht) will not be – the delta-spikes get ‘broadened’ by the transition and emission factors.
Our task is then to approximate ρ(ht) as a new sum of delta-spikes. Below we discuss a method to achieve
this for which explicit knowledge of the normalisation Z is not required. This is useful since in many tracking
applications the normalisation of the emission p(vt

ht) is unknown.
|

A Monte-Carlo sampling approximation

A simple approach to forming an approximate mixture-of-delta functions representation of equation (27.6.20)
is to generate a set of points using sampling. In principle any sampling method can be used, including pow-
erful MCMC approaches.

In particle ﬁlters, importance sampling is use to generate the new particles. That is we generate a set
of samples h1
from some importance distribution q(ht) which gives the unnormalised importance
weights

t , . . . , hL
t

p(vt

˜wl

t =

t) (cid:80)L
hl
|

t−1)wl(cid:48)
hl(cid:48)
l(cid:48)=1 p(hl
t|
q(hl
t)

t−1

Deﬁning the normalised weights:

wl

t =

˜wl
t
l(cid:48) ˜wl(cid:48)
t

(cid:80)

we obtain an approximation

ρ(ht)

≈

L
(cid:88)

l=1

wl
tδ

(cid:16)

ht, hl
t

(cid:17)

(27.6.21)

(27.6.22)

(27.6.23)

Ideally one would use the importance distribution that makes the importance weights uniform, namely

q(ht)

p(vt

ht)

|

∝

L
(cid:88)

l=1

p(ht

t−1)wl
hl
|

t−1

DRAFT April 24, 2015

(27.6.24)

569

Importance Sampling

×

Figure 27.15: Tracking an object with
a particle ﬁlter containing 50 parti-
cles. The small circles are the par-
ticles, scaled by their weights. The
correct corner position of the face is
’, the ﬁltered average
given by the ‘
by the large circle ‘o’, and the most
likely particle by ‘+’.
Ini-
tial position of the face without noise
and corresponding weights of the par-
(b): Face with noisy back-
ticles.
ground and the tracked corner posi-
tion after 20 timesteps. The Forward-
Sampling-Resampling PF method is
used to maintain a healthy pro-
portion of non-zero weights.
See
demoParticleFilter.m

(a):

(a)

(b)

However, this is often diﬃcult to sample from directly due to the unknown normalisation of the emission
p(vt

ht). A simpler alternative is to sample from the transition mixture:
|

q(ht) =

L
(cid:88)

l=1

t−1)wl
hl

t−1

p(ht

|

(27.6.25)

To do so, one ﬁrst samples a component l∗ from the histogram with weights w1
sample index, say l∗, one then draws a sample from p(ht
become simply

hl∗
t−1).
|

t−1, . . . , wL
t−1. Given this
In this case the unnormalised weights

˜wl

t = p(vt

hl
t)

|

(27.6.26)

This Forward-Sampling-Resampling procedure is used in demoParticleFilter.m and in the following toy
example.

Example 27.2 (A toy face-tracking example). At time t a binary face template is in a two-dimensional
location ht, which describes the upper-left corner of the template. At time t = 1 the position of the face is
known, see ﬁg(27.15a). In subsequent times the face moves randomly according to

ht = ht−1 + σηt

(27.6.27)

(ηt 0, I) is a two dimensional zero mean unit covariance noise vector. In addition, a fraction
where ηt ∼ N
of the binary pixels in the whole image are selected at random and their states ﬂipped. The aim is to try
to track the upper-left corner of the face through time.

We need to deﬁne the emission distribution p(vt
following compatibility function

ht) on the binary pixels with vi
|

0, 1
}

∈ {

. Consider the

φ(vt, ht) = vT

t ˜v(ht)

570

(27.6.28)

DRAFT April 24, 2015

510152025303540510152025303540102030405000.010.020.030.040.050.060.07particle weights510152025303540510152025303540102030405000.010.020.030.040.050.060.070.080.09particle weightsImportance Sampling

@@

@@

@@

where ˜v(ht) is the vector representing the whole image with a clean face placed at position ht and zeros
outside of the face template. Then φ(vt, ht) measures the overlap between the face template at a speciﬁc
location and the noisy image restricted to the template pixels. The compatibility function is maximal when
the observed image vt has the face placed at position ht. We can

therefore deﬁne

p(vt

ht)
|

∝

Tht
vt
1Tht

(27.6.29)

A subtlety is that ht is continuous, and in the compatibility function we ﬁrst map ht to the nearest integer
representation. In ﬁg(27.15a) 50 particles are used to track the face. The particles are plotted along
pixel
with their corresponding weights. For each t > 1, 5% of the pixels are selected at random in the image
and their states ﬂipped. Using the forward-sampling-resampling method we can successfully track the face
despite the presence of the background clutter.

Real tracking applications involve complex issues, including tracking multiple objects, transformations of
the object (scaling, rotation, morphology changes). Nevertheless, the principles are largely the same and
many tracking applications work by seeking compatibility functions, often based on the colour histogram in
a template.

27.7 Summary

•

•

•

Exact sampling can be achieved for models such as belief networks, although forward sampling in the case
of evidence can be ineﬃcient.

Provided independent samples are drawn from a distribution, only a small number of samples is required to
obtain a good estimate of an expectation. However, drawing independent samples from high-dimensional
non-standard distributions is computationally extremely diﬃcult.

Markov chain Monte Carlo methods are approximate sampling methods which converge to drawing samples
from the correct distribution in the limit of a large number of samples. Whilst powerful, assessing conver-
gence of the method can be diﬃcult. Also, samples are often highly dependent, so that a great number of
samples may be required to obtain a reliable estimate of an expectation.

27.8 Code

potsample.m: Exact sample from a set of potentials
ancestralsample.m: Ancestral sample from a belief network
JTsample.m: Sampling from a consistent junction tree
GibbsSample.m: Gibbs sampling from a set of potentials
demoMetropolis.m: Demo of Metropolis sampling for a bimodal distribution
metropolis.m: Metropolis sample
logp.m: Log of a bimodal distribution
demoParticleFilter.m: Demo of particle ﬁltering (forward-sampling-resampling method)
placeobject.m: Place an object in a grid
compat.m: Compatibility function
demoSampleHMM.m: Naive Gibbs sampling for a HMM

DRAFT April 24, 2015

571

27.9 Exercises

Exercise 27.1 (Box-Muller method). Let x1

U (x1

|

∼

[0, 1]), x2

U (x2

|

∼

[0, 1]) and

(cid:112)

−

2 log x1 cos 2πx2,

y1 =

Show that

(cid:112)

y2 =

−

2 log x1 sin 2πx2

(cid:90)

p(y1, y2) =

p(y1

x1, x2)p(y2
|

|

x1, x2)p(x1)p(x2)dx1dx2 =

(y1 0, 1)

N

N

(y2 0, 1)

Exercises

(27.9.1)

(27.9.2)

and suggest an algorithm to sample from a univariate normal distribution. Hint: Use the change of variable
result, result(8.1), for vectors y = (y1, y2) and x = (x1, x2).

Exercise 27.2. Consider the distribution p(x)
with q(x) =

≤
(cid:0)x 0, σ2(cid:1) show that a suitable value for M such that p∗(s)/q(x)

exp(sin(x)) for

∝

≤

−

x

π

N
M = e1+ π2

2σ2 √2πσ2

π. Using rejection sampling

M is

≤

(27.9.3)

For a suitably chosen σ2 draw 10,000 samples from p(x) and plot the resulting histogram of the samples.

Exercise 27.3. Consider the distribution

p(x1, . . . , x6) = p(x1)p(x2)p(x3

x1, x2)p(x4
|

|

x3)p(x5

|

x3)p(x6

x4, x5)
|

(27.9.4)

For x5 ﬁxed in a given state x5, write down a distribution on the remaining variables p(cid:48)(x1, x2, x3, x4, x6)
and explain how forward (ancestral) sampling can be carried out for this new distribution.

Exercise 27.4. Consider an Ising model on an M

×

M square lattice with nearest neighbour interactions:

p(x)

∝

exp β

(cid:88)

i∼j

I [xi = xj]

(27.9.5)

Now consider the M
square a label bj, so that each square is associated with a particular variable. Show that

M grid as a checkerboard, and give each white square a label wi, and each black

×

p(b1, b2, . . . ,

|

w1, w2, . . .) = p(b1

w1, w2, . . .)p(b2
|

|

w1, w2, . . .) . . .

(27.9.6)

That is, conditioned on the white variables, the black variables are independent. The converse is also true,
that conditioned on the black variables, the white variables are independent. Explain how this can be exploited
by a Gibbs sampling procedure. This procedure is known as checkerboard or black and white sampling.

Exercise 27.5. Consider the symmetric Gaussian proposal distribution

˜q(x(cid:48)

|

x) =

N

(cid:0)x(cid:48) x, σ2

q I(cid:1)

and the target distribution

p(x) =

N

(cid:0)x 0, σ2

pI(cid:1)

where dim (x) = N . Show that

(cid:28)

log

(cid:29)

p(x(cid:48))
p(x)

N σ2
q
2σ2
p

=

−

˜q(x(cid:48)|x)

(27.9.7)

(27.9.8)

(27.9.9)

Discuss how this result relates to the probability of accepting a Metropolis-Hastings update under a Gaussian
proposal distribution in high-dimensions.

572

DRAFT April 24, 2015

Exercises

Exercise 27.6. The ﬁle demoSampleHMM.m performs naive Gibbs sampling of the posterior p(h1:T
v1:T ) for
|
a HMM for T = 10. At each Gibbs update a single variable ht is chosen, with the remaining h variables
clamped. The procedure starts from t = 1 and sweeps forwards through time. When the end time t = T
is reached, the joint state h1:T is taken as a sample from the posterior. The parameter λ controls how
ht−1) will be. Adjust demoSampleHMM.m to run 100 times
deterministic the hidden transition matrix p(ht
|
for the same λ, computing a mean absolute error for the posterior marginal p(ht
v1:T ) over these 100 runs.
|
Then repeat this for λ = 0.1, 1, 10, 20. Finally, repeat this whole procedure 20 times for diﬀerent random
draws of the transition and emission matrices, and average the errors in computing the smoothed posterior
marginal by Gibbs sampling. Discuss why the performance of this Gibbs sampling routine deteriorates with
increasing λ.

Exercise 27.7. Consider the following MATLAB code snippet:

c=p(1); i=1; r=rand;
while r>c && i<n

i=i+1; c=c+p(i);

end
sample=i;

1. Explain why this draws a sample state i from a discrete distribution with probability p(i), i = 1, . . . , n.

2. Explain how to eﬀectively sample from the distribution p(i) = e−λλi/i!, 0 < λ < 1, i = 0, . . . ,

contains an inﬁnite number of discrete states.

which

∞

Exercise 27.8.
aims to provide an approximation to the parameter posterior p(θ
ated from the likelihood p(
constant. For example, if we specify a model such as xt+1 = f (xt
ple a dataset
Consider drawing samples from a posterior p(θ

This exercise discusses a technique called Approximate Bayesian Computation (ABC) which
(cid:48) can be gener-
θ), even if the likelihood cannot be calculated up to some unknown normalisation
(ηt 0, 1), we can easily sam-
θ).
|

from this, without needing to specify the normalisation constant of p(

) in cases where a sample

θ)+ηt, ηt
|

x1, . . . , xT

θ)p(θ).

∼ N

(cid:48) =

|D

p(

D

D

D

D

{

}

)

|

(cid:48)

(cid:48)

|D

∝

D|

1. Show that for

q(

,

D

D

(cid:48), θ) = q(

(cid:48)

(cid:48))p(

D

D|D

θ)p(θ), where q(
|

D|D

(cid:48)) = δ (cid:0)

(cid:48)(cid:1)

D − D

the marginal distribution is

q(θ

|D

) = p(θ

)

|D

2. Hence show that the following procedure generates a sample from p(θ

):

|D

(27.9.10)

(27.9.11)

(a) Sample θ from p(θ)
(b) Sample a ‘candidate’ dataset

(cid:48) from p(

(cid:48)

θ)
|

D

D

(c) If

(cid:48) =

D

D

accept the candidate as a sample, otherwise make no sample and go to (a).

3. One may relax the delta-function constraint and use, for example, q(

chosen σ2. Then

p(θ

)

|D

q(θ

≈

|D

) =

(cid:88)

(cid:16)

θ

wlδ

l

θl(cid:17)

−

where wl

≡

l)

q(
l=1 q(

D|D

(cid:80)L

D|D

(cid:48)) =

(cid:0)

N

D D

(cid:48), σ2I(cid:1) for some

D|D

l)

(27.9.12)

Explain how this approximate sampling procedure is related to the Parzen estimator.
requires only the ability to sample a dataset
of p(

Note that this
(cid:48) given θ – we do not explicitly require the normalisation

D

(cid:48)

θ) to do this.
|

D

4. By considering a single D-dimensional datapoint x and sampled datapoint xi, both generated from the

same underlying distribution

(cid:0)x 0, ν2ID×D

(cid:1) show that for the unnormalised weight

++

++

++

ui = exp

(cid:18)

1
2σ2

−

(cid:0)x

−

DRAFT April 24, 2015

N

xi(cid:1)2(cid:19)

(27.9.13)

573

the typical ratio of the weights can be assessed using

(cid:42)(cid:18)

log

ui
uj

(cid:19)2(cid:43)

N (x 0,ν2I)N (xi 0,ν2I)N (xj 0,ν2I)

= 12D

ν4
σ4

Exercises

(27.9.14)

Explain why the normalised weights will typically be dominated by a single component, rendering the
This shows the ‘curse of dimen-
sampling method described in part (3) above generally impractical.
sionality’ for methods such as ABC, meaning that in general such techniques need to be used with
considerable care in high dimensional parameter spaces.

Soccer teams Aces and Bruisers are arch rivals. They have played each other already 20
Exercise 27.9.
times this season and will play one more ﬁnal game of the season. The goalkeepers of both teams have
been the same all season and will also be the same for the ﬁnal game. However, each team has a squad of
20 players, with 10 players being selected from each squad to make up a team of 11 players (10 plus the
goalkeeper) each game. The ﬁle soccer.mat contains the history of the team sheets and which team won
the game (+1 for an Aces win over Bruisers and
1 for a loss). Both team coaches use the universal rating
system for a player:

−

++

++

2 genius
1 superb
0 very average
1 muppet
2 worse than your grandma

−
−

The outcome (which assumes both goalkeepers are equally matched in their abilities), independently for each
game, is modelled by

ta, tb, a, b) = σ
p(Aces beats Bruisers
|

(cid:32) 10
(cid:88)

(cid:16)

i=1

ata

i −

btb

i

(cid:33)

(cid:17)

(27.9.15)

where σ(x) = 1/(1 + exp(
and bj
2,
−
in the Aces team is given by ta

x)). Here aj

, j = 1, . . . , 20 is the ability of player j in Aces
}
, j = 1, . . . , 20 is the ability of player j in Bruisers. The 10 players selected to play
}
, and similarly for Bruisers, tb
}

1, . . . , 20
}

1, . . . , 20

1, 0, 1, 2

1, 0, 1, 2

i ∈ {

i ∈ {

∈ {−

∈ {−

2,

−

−

.

1. Which are the 10 best players for Aces and 10 best for Bruisers?

2. Given that we know the Bruisers will ﬁeld players numbered 1 to 10, what would be the best team of
players that Aces can ﬁeld to maximise their chance of winning? How does this relate to selecting the
10 best Aces players above?

574

DRAFT April 24, 2015

CHAPTER 28

Deterministic Approximate Inference

Sampling methods are popular and well known for approximate inference.
In this chapter we give an
introduction to the less well known class of deterministic approximation techniques. These have been
spectacularly successful in branches of the information sciences and many have their origins in the study of
large-scale physical systems.

28.1 Introduction

Deterministic approximate inference methods are an alternative to the sampling techniques discussed in
chapter(27). Drawing exact independent samples is typically computationally intractable and assessing the
quality of the sample estimates is diﬃcult. In this chapter we discuss some alternatives. The ﬁrst, Laplace’s
method, is a simple perturbation technique. The second class of methods are those that produce rigorous
bounds on quantities of interest. Such methods are interesting since they provide certain knowledge – it
may be suﬃcient, for example, to show that a marginal probability is greater than 0.1 in order to make an
informed decision. A further class of methods are the consistency methods, such as loopy belief propagation.
Such methods have revolutionised certain ﬁelds, including error correction[197]. It is important to bear in
mind that no single approximation technique, deterministic or stochastic, is going to beat all others on all
problems, given the same computational resources. In this sense, insight as to the properties of the various
approximations is useful in matching an approximation method to the problem at hand.

28.2 The Laplace approximation

Consider a distribution on a continuous variable of the form

p(x) =

1
Z

e−E(x)

(28.2.1)

The Laplace method makes a Gaussian approximation of p(x) based on a local perturbation expansion
around a mode x∗. First we ﬁnd the mode numerically, giving

x∗ = argmin

x

E(x)

Then a Taylor expansion up to second order around this mode gives

E(x)

≈

E(x∗) + (x

x∗)T

E

x∗ +

(x

x∗)T H (x

x∗)

where H
tion of the distribution is given by the Gaussian

−
x∗ is the Hessian evaluated at the mode. At the mode,
|

≡ ∇∇

E(x)

∇

−

−

|

1
2

q(x) =

1
Zq

e− 1

2 (x−x∗)TH(x−x∗) =

(cid:0)x x∗, H−1(cid:1)

N

575

(28.2.2)

(28.2.3)

E

x∗ = 0, and an approxima-
|

∇

(28.2.4)

which has mean x∗ and covariance H−1, with Zq =
estimate the integral

(cid:112)

det (2πH−1). We can use the above expansion to

Properties of Kullback-Leibler Variational Inference

(cid:90)

x

e−E(x)

(cid:90)

≈

x

e−E(x∗)− 1

2 (x−x∗)TH(x−x∗) = e−E(x∗)(cid:112)

det (2πH−1)

(28.2.5)

The Laplace Gaussian ﬁt to a distribution is not necessarily the ‘best’ Gaussian approximation. As we’ll see
below, other criteria, such as based on minimal KL divergence between p(x) and a Gaussian approximation
may be more appropriate, depending on the context. A beneﬁt of Laplace’s method is its relative simplicity
compared with other approximate inference techniques.

28.3 Properties of Kullback-Leibler Variational Inference

Variational methods can be used to approximate a complex distribution p(x) by a simpler distribution q(x).
Given a deﬁnition of discrepancy between an approximation q(x) to p(x), any free parameters of q(x) are
then set by minimising the discrepancy. This class of techniques are also called ‘mean ﬁeld’ methods in the
physics literature.

A particularly popular measure of the discrepancy between an approximation q(x) and the intractable
distribution p(x) is the Kullback-Leibler divergence

KL(q

p) =
|

(cid:104)

log q

(cid:105)q − (cid:104)

log p

(cid:105)q

(28.3.1)

It is straightforward to show that KL(q
0 and is zero if and only if the distributions p and q are identical,
see section(8.2.1). Note that whilst the KL divergence cannot be negative, there is no upper bound on the
value it can potentially take so that the discrepancy can be ‘inﬁnitely’ large.

p)
|

≥

28.3.1 Bounding the normalisation constant

For a distribution of the form

p(x) =

1
Z

eφ(x)

we have

KL(q

p) =
|

(cid:104)

log q(x)

(cid:105)q(x) − (cid:104)

log p(x)

(cid:105)q(x) =

log q(x)
(cid:104)

(cid:105)q(x) − (cid:104)

φ(x)

(cid:105)q(x) + log Z

Since KL(q

p)
|

≥

0 this immediately gives the bound

log Z

≥ − (cid:104)
(cid:124)

log q(x)
(cid:123)(cid:122)
entropy

(cid:105)q(x)
(cid:125)

+

φ(x)
(cid:104)
(cid:123)(cid:122)
(cid:124)
energy

(cid:105)q(x)
(cid:125)

(28.3.2)

(28.3.3)

(28.3.4)

which is called the ‘free energy’ bound in the physics community [254]. The KL(q
p) method provides
|
therefore a lower bound on the normalisation constant. The art is then to choose a class of approximating
distributions q such that both its entropy and the energy term are tractably computable.

28.3.2 Bounding the marginal likelihood

In Bayesian modelling the likelihood of the model

(cid:90)

p(

) =

D|M

p(
(cid:124)

θ,
)
D|
M
(cid:125)
(cid:123)(cid:122)
likelihood

p(θ
)
|M
(cid:124) (cid:123)(cid:122) (cid:125)
prior

θ

with parameters θ generating data

is given by

D

M

(28.3.5)

This quantity is fundamental to model comparison. However, in cases where θ is high-dimensional, the
integral over θ is often diﬃcult to perform. Using Bayes’ rule,

p(θ

,

|D

M

) =

576

p(

D|

θ,
p(

)p(θ
M
)
D|M

)
|M

(28.3.6)

DRAFT April 24, 2015

Properties of Kullback-Leibler Variational Inference

Figure 28.1: Fitting a mixture of Gaussians p(x) (blue) with a single Gaus-
sian. The green curve minimises KL(q
p) corresponding to ﬁtting a local
|
model. The red curve minimises KL(p
q) corresponding to moment match-
|
ing.

and considering

p(θ
KL(q(θ)
|

|D

,

)) =

M

=

log q(θ)
(cid:104)
log q(θ)
(cid:104)

(cid:105)q(θ) − (cid:104)
(cid:105)q(θ) − (cid:104)

log p(θ

log p(

,

|D
θ,

)
(cid:105)q(θ)
)p(θ

(cid:105)q(θ) + log p(
)
|M

)
D|M

M

M

D|

the non-negativity of the Kullback-Leibler divergence gives the bound

log p(

)
D|M

log q(θ)

≥ − (cid:104)
=

log p(
(cid:104)

D|

log p(
(cid:105)q(θ) +
(cid:104)
)
θ,
(cid:105)q(θ) −
M

M

D|
KL(q(θ)

θ,

)p(θ

p(θ
|

|M

)
|M
))

(cid:105)q(θ)

(28.3.7)

(28.3.8)

(28.3.9)

(28.3.10)

This bound holds for any distribution q(θ) and becomes equality when q(θ) = p(θ
). Since using
the optimal setting is assumed computationally intractable, the idea in variational bounding is to choose a
distribution family for q(θ) for which the bound is computationally tractable, and then maximise the bound
with respect to any free parameters of q(θ). The resulting bound then can be used as a surrogate for the
exact marginal likelihood in model comparison.

M

|D

,

28.3.3 Bounding marginal quantities

The Kullback-Leibler approach provides a lower bound on normalisation constants. Combined with an upper
bound, obtained using alternative methods, see for example [307] and exercise(28.6), we are able to bracket
marginals l
u, see exercise(28.9). The tightness of the resulting bracket gives an indication as to
how tight the bounding procedures are. Even in cases where the resulting bracket is weak – for example it
might be that the result is that 0.1 < p(cancer = true) < 0.99, this may be suﬃcient for decision making
purposes since the probability of cancer is suﬃciently large to merit action.

p(xi)

≤

≤

28.3.4 Gaussian approximations using KL divergence

Minimising KL(q

p)
|

Using a simple approximation q(x) of a more complex distribution p(x) by minimising KL(q
p) tends to give
|
a solution for q(x) that focuses on a local mode of p(x), thereby underestimating the variance of p(x). To
show this, consider approximating a mixture of two Gaussians with equal variance σ2,

p(x) =

(cid:0)

1
2

N

(cid:0)x

−

µ, σ2(cid:1) +

N

(cid:0)x µ, σ2(cid:1)(cid:1)

see ﬁg(28.1), with a single Gaussian

q(x) =

N

(cid:0)x m, s2(cid:1)

We wish to ﬁnd the optimal m, s2 that minimise

(28.3.11)

(28.3.12)

log p(x)

KL(q

p) =
|

log q(x)
(cid:104)

(cid:105)q(x) − (cid:104)

(cid:105)q(x)
σ, then by setting
If we consider the case that the two Gaussian components of p(x) are well separated, µ
q(x) to be centred on the left mode at
µ, so that
the second mode at µ has negligible contribution to the Kullback-Leibler divergence. In this sense one can
approximate p(x)

µ the Gaussian q(x) only has appreciable mass close to

(cid:29)

−

−

1
2 q(x), so that

(28.3.13)

≈

KL(q

p)
|

≈ (cid:104)

log q(x)

(cid:105)q(x) − (cid:104)

DRAFT April 24, 2015

log p(x)

(cid:105)q(x) = log 2

(28.3.14)

577

−30−20−10010203000.050.10.150.20.250.30.350.4Properties of Kullback-Leibler Variational Inference

Figure 28.2: A planar pairwise Markov random ﬁeld on a set of variables x1, . . . , x25,
representing a distribution of the form (cid:81)
i∼j φ(xi, xj). In statistical physics such lat-
tice models include the Ising model on binary ‘spin’ variables xi
with
φ(xi, xj) = ewij xixj .

1
}
−

∈ {

+1,

p) =
|

log q(x)/p(x)
(cid:104)

On the other hand, setting m = 0, which is the correct mean of the distribution p(x), very little of the mass
of the mixture is captured unless s2 is large, giving a poor ﬁt and large KL divergence. Another way to view
this is to consider KL(q
(cid:105)q(x); provided q is close to p around where q has signiﬁcant mass,
of order 1 and the KL divergence small. Setting m = 0 means that q(x)/p(x) is
the ratio q(x)/p(x) will be
large where q has signiﬁcant mass, and is therefore a poor ﬁt. The optimal solution in this case is therefore
to place the Gaussian close to a single mode. However, for two modes that are less well-separated, the
optimal solution will not necessarily be to place the Gaussian around a local mode. In general, the optimal
Gaussian ﬁt needs to be determined numerically – that is, there is no closed form solution to ﬁnding the
optimal mean and (co)variance parameters.

@@

Minimising KL(p

q)
|

Bearing in mind that in general KL(q
For ﬁtting a Gaussian q(x) =

q), it is also useful to understand the properties of KL(p
= KL(p
|
(cid:0)x m, s2(cid:1) to p based on KL(p

p)

|

q).
|

N

q), we have
|
m)2(cid:69)

p(x) −

1
2

log s2 + const.

KL(p

q) =
|

(cid:104)

log p(x)

(cid:105)p(x) − (cid:104)

log q(x)

(cid:105)p(x) =

−

(cid:68)

(x

1
2s2

−

Minimising this with respect to m and

s2 we obtain:

m =

x

(cid:105)p(x) ,

(cid:104)

s2 = (cid:10)(x

m)2(cid:11)

p(x)

−

(28.3.15)

@@

(28.3.16)

so that the optimal Gaussian ﬁt matches the ﬁrst and second moments of p(x).

In the case of ﬁg(28.1), the mean of p(x) is a zero, and the variance of p(x) is large. This solution is
p). The ﬁt found
therefore dramatically diﬀerent from that produced by ﬁtting the Gaussian using KL(q
|
using KL(q
q) focusses on
|
making q ﬁt p well to the global statistics of the distribution (possibly at the expense of a good local match).

p) focusses on making q ﬁt p well locally, see also exercise(28.17), whereas KL(p
|

28.3.5 Marginal and moment matching properties of minimising KL(p
For simplicity, consider a factorised approximation q(x) = (cid:81)

i q(xi). Then

q)
|

KL(p

q) =
|

(cid:104)

log p(x)

(cid:105)p(x) −

(cid:88)

i

log q(xi)
(cid:104)

(cid:105)p(xi)

(28.3.17)

The ﬁrst entropic term is independent of q(x) so that up to a constant independent of q(x), the above is

(cid:88)

i

KL(p(xi)
|

q(xi))

(28.3.18)

so that optimally q(xi) = p(xi). That is, the optimal factorised approximation is to set the factors of q(xi)
to the marginals of p(xi), exercise(28.13).

Another approximating distribution class that yields a known form for the approximation is the exponential
family. In this case minimising KL(p
q) corresponds to moment matching, see exercise(28.13). In practice,
|
one generally cannot compute the moments of p(x) (since the distribution p(x) is considered ‘intractable’),
so that ﬁtting q to p based only on KL(p
q) does not itself lead to a practical algorithm for approximate
|
inference. Nevertheless, as we will see, it is a useful subroutine for local approximations, in particular
Expectation Propagation.

578

DRAFT April 24, 2015

(cid:54)
Variational Bounding Using KL(q

p)

|

Figure 28.3: A distribution on pixels. The ﬁlled nodes indicate observed noisy pixels,
the unshaded nodes a Markov Random Field on latent clean pixels. The task is to infer
the clean pixels given the noisy pixels. The MRF encourages the posterior distribution
on the clean pixels to contain neighbouring pixels in the same state.

28.4 Variational Bounding Using KL(q

p)
|

In this section we discuss how to ﬁt a distribution q(x) from some assumed family to an ‘intractable’
distribution p(x). As we saw above for the case of ﬁtting Gaussians, the optimal q needs to be found
numerically. This itself can be a complex task (indeed, formally this can be just as diﬃcult as performing
inference directly with the intractable p) and the reader may wonder why we trade a diﬃcult inference
task for a potentially diﬃcult optimisation problem. The general idea is that the optimisation problem has
some local smoothness properties that enable one to rapidly ﬁnd a reasonable optimum based on generic
optimisation methods. To make these ideas more concrete, we discuss a particular case of ﬁtting q to a
formally intractable p in section(28.4.1) below.

28.4.1 Pairwise Markov random ﬁeld

A canonical intractable distribution is the pairwise Markov Random Field1 deﬁned on binary variables
xi

, i = 1, . . . , D,

+1,

∈ {

1
}
−

p(x) =

1
Z(w, b)

(cid:80)
e

i,j wij xixj +(cid:80)

i bixi

Here the partition function Z(w, b) ensures normalisation,

Z(w, b) =

(cid:88)

e

x

(cid:80)

i,j wij xixj +(cid:80)

i bixi

(28.4.1)

(28.4.2)

Since x2
motivational example for this model is described below.

i = 1, the terms wiix2

i are constant and without loss of generality we may set wii to zero. A

Example 28.1 (Bayesian image denoising). Consider a binary image y generated by corrupting a clean
image x; our interest it to recover the clean image given the corrupted image. We assume a noisy pixel
generating process that takes each clean pixel xi

1 and ﬂips its binary state:

∈ ±

p(y

x) =
|

(cid:89)

i

p(yi

xi),

|

p(yi

xi)
|

∝

eγyixi

(28.4.3)

The probability that yi and xi are in the same state is eγ/(eγ +e−γ). Our interest is the posterior distribution
on clean pixels p(x
y). We assume that clean images are reasonably smooth and can be described using a
|
MRF prior deﬁned on a lattice, ﬁg(28.2),

(cid:80)
e

ij wij xixj

p(x)

∝

(28.4.4)

where wij > 0, for neighbouring i and j, and wij = 0 otherwise. This encodes the assumption that clean
images tend to have neighbouring pixels in the same state. An isolated pixel in a diﬀerent state to its
neighbours is unlikely under this prior. We now have the joint distribution

p(x, y) = p(x)

(cid:89)

i

p(yi

xi) =
|

(cid:80)
e

1
Z

ij wij xixj +(cid:80)

i γyixi

(28.4.5)

1Whilst inference with a general MRF is formally computationally intractable (no exact polynomial time methods are
known), two celebrated results that we mention in passing are that for the planar MRF model with pure interactions (b = 0),
the partition function is computable in polynomial time[170, 102, 191, 127, 260], as is the MAP state for attractive planar Ising
models w > 0 [133], see section(28.9).

DRAFT April 24, 2015

579

see ﬁg(28.3), from which the posterior is given by

Variational Bounding Using KL(q

p)
|

p(x

y) =
|

x)p(x)

p(y
|
x p(y

(cid:80)

x)p(x) ∝
|

(cid:80)

ij wij xixj +(cid:80)

i γyixi

e

(28.4.6)

y) and the normali-
Quantities such as the MAP state (most a posteriori probable image), marginals p(xi
|
sation constant are of interest. This example is equivalent to the Markov network in example(4.2). On the
left below is the clean image, the middle is the noisy image, and on the right is the most likely posterior
clean image arg maxx p(x

y) found using iterated conditional modes, section(28.9.1).
|

We discuss how to compute the MAP state for example(28.1) in section(28.9) and concentrate ﬁrst on a
technique that can bound the normalisation constant Z, a quantity that is useful in model comparison.

Kullback-Leibler based methods

For the MRF we have

KL(q

p) =
|

(cid:104)

log q

(cid:105)q −

(cid:88)

ij

wij

xixj

(cid:104)

(cid:105)q −

(cid:88)

i

bi

xi

(cid:105)q + log Z

(cid:104)

≥

0

Rewriting, this gives a bound on the log-partition function

log Z

≥ − (cid:104)
(cid:124)

log q
(cid:123)(cid:122)
entropy

(cid:105)q
(cid:125)

(cid:88)

+

ij
(cid:124)

wij

(cid:104)

xixj

(cid:105)q +
(cid:123)(cid:122)
energy

(cid:88)

i

bi

xi

(cid:104)

(cid:105)q
(cid:125)

(28.4.7)

(28.4.8)

xi

(cid:105)p ,

The bound saturates when q = p. This is of little help, however, since we cannot compute the averages
(cid:105)p with respect to this intractable distribution p. The idea of a variational method is to assume
xixj
(cid:104)
a simpler tractable distribution q for which these averages can be computed, along with the entropy of q.
Minimising the KL divergence with respect to any free parameters of q(x) is then equivalent to maximising
the lower bound on the log partition function.

(cid:104)

Factorised approximation

A ‘naive’ assumption is the fully factorised distribution

q(x) =

(cid:89)

i

qi(xi)

The graphical model of this approximation is given in ﬁg(28.4a). In this case

log Z

(cid:88)

≥ −

(cid:104)

i

log qi

(cid:105)qi +

(cid:88)

ij

wij

xixj
(cid:104)

(cid:105)q(xi,xj ) +

(cid:88)

i

bi

xi
(cid:104)

(cid:105)q(xi)

(28.4.9)

(28.4.10)

(a)

(b)

(c)

(a): Naive Mean Field approximation
(b): A spanning tree approxima-
(c): A decomposable (hypertree) approxima-

i qi(xi).

Figure 28.4:
q(x) = (cid:81)
tion.
tion.

580

DRAFT April 24, 2015

Variational Bounding Using KL(q

p)

|

For a factorised distribution q(x) = (cid:81)
use the convenient parametrization

qi(xi = 1) =

eαi
eαi + e−αi

i q(xi), and

xixj
(cid:104)

(cid:105)

=

xi
(cid:104)

xj

(cid:105) (cid:104)

, i
(cid:105)

= j. For a binary variable, one may

(28.4.11)

(28.4.12)

(28.4.13)

so that for xi

∈ {−
(cid:105)qi = +1

xi

1, +1
}
q(xi = 1)

(cid:104)

×
This gives the following lower bound on the log partition function:

−

×

−

1

q(xi =

1) = tanh(αi)

log Z

(α)

≥ B

(cid:88)

≡

i

H(αi) +

(cid:88)

i(cid:54)=j

wij tanh(αi) tanh(αj) +

(cid:88)

i

bi tanh(αi)

where H(αi) is the binary entropy of a distribution parameterised according to equation (28.4.11):

H(αi) = log (cid:0)eα

i + e−αi(cid:1)

αi tanh(αi)

−

(28.4.14)

B

(α) with respect to the variational parameters α. The bound

Finding the best factorised approximation in the minimal Kullback-Leibler divergence sense then corre-
sponds to maximising the bound
, equation
(28.4.13), is generally non-convex in α and riddled with local optima. Finding the globally optimal α is
therefore typically a computationally hard problem. It seems that we have simply replaced the computa-
(α).
tionally hard problem of computing log Z by an equally hard computational problem of maximising
Indeed, written as a factor graph in α, the structure of this optimisation problem matches exactly that of
the original MRF. However, the hope is that by approximating a diﬃcult discrete summation by a contin-
uous optimisation problem, we will be able to bring to the table eﬀective continuous variable optimisation
techniques. A particularly simple optimisation technique is to solve for the zero derivative of the bound
equation (28.4.13). Diﬀerentiating and equating to zero, a little algebra leads to the requirement that the
optimal solution satisﬁes the equations

B

B

αi = bi +

(cid:88)

i,j

wij tanh(αj),

i
∀

(28.4.15)

(α). This is
One may show that sequentially updating any αi according to equation (28.4.15) increases
called asynchronous updating and is guaranteed to lead to a (local) minimum of the KL divergence, see
section(28.4.3). Once a converged solution α has been identiﬁed, in addition to a bound on log Z, we can
approximate

B

xi

(cid:104)

xi

(cid:105)p ≈ (cid:104)

(cid:105)q = tanh(αi)

Validity of the factorised approximation

(28.4.16)

When might one expect such a naive factorised approximation to work well? Clearly, for equation (28.4.1),
if wij is very small, the distribution p will be eﬀectively factorised and the approximation will be accurate.
A more interesting case is when each variable xi has many neighbours. In this case it is useful to write the
MRF as (ignoring the bias terms bi for simplicity)

p(x) =

1
Z

(cid:80)

e

ij wij xixj =

1
Z

eD (cid:80)

i xi

1
D

(cid:80)

j wij xj =

eD (cid:80)

i xizi

1
Z

where the local ‘ﬁelds’ are deﬁned as

zi

≡

1
D

(cid:88)

j

wijxj

(28.4.17)

(28.4.18)

An interesting question is how zi is distributed. We now invoke a circular (but self-consistent) argument:
Let’s assume that p(x) is factorised. Assuming that each wij is O (1), the mean of zi is

=

zi

(cid:104)

(cid:105)

1
D

(cid:88)

j

wij

xj

(cid:104)

(cid:105)

= O (1)

DRAFT April 24, 2015

(28.4.19)

581

(cid:54)
The variance is

(cid:11)

(cid:10)z2
i

2 =

zi

(cid:105)

− (cid:104)

1
D2

D
(cid:88)

k=1

(cid:16)

1

w2
ik

xk

2(cid:17)
(cid:105)

− (cid:104)

= O (1/D)

Variational Bounding Using KL(q

p)
|

(28.4.20)

Hence for large D the variance of the ﬁeld zi is much smaller than its mean value. Since each of the terms
xj in the summation (cid:80)
j wijxj is independent, provided the wij are not extreme, the conditions of validity
of the central limit theorem hold[134], and zi will be Gaussian distributed. In particular, as D increases the
ﬂuctuations around the mean diminish, and we may write

p(x)

1
Z

≈

eD (cid:80)

i xi(cid:104)zi(cid:105)

(cid:89)

≈

i

p(xi)

(28.4.21)

The assumption that p is approximately factorised is therefore self-consistent in the limit of MRFs with
a large number of neighbours. Hence the factorised approximation would appear to be reasonable in the
extreme limits of (i) a very weakly connected system wij
0, or (ii) a large densely connected system with
random weights. The fully factorised approximation is also called the naive mean ﬁeld theory since for the
MRF case it assumes that we can replace the eﬀect of the neighbours by a mean of the ﬁeld at each site.

≈

28.4.2 General mean ﬁeld equations

For a general intractable distribution p(x) on discrete or continuous x, the KL divergence between a factorised
approximation q(x) = (cid:81)

i q(xi) and p(x) is

KL(q(x)
|

p(x)) =

(cid:88)

(cid:104)

i

log q(xi)

(cid:105)q(xi) − (cid:104)

log p(x)

(cid:81)
(cid:105)

i q(xi)

Isolating the dependency of the above on a single factor q(xi) we have

log q(xi)
(cid:104)

(cid:105)q(xi) −

(cid:68)

log p(x)
(cid:104)

(cid:81)
(cid:105)

(cid:69)

j(cid:54)=i q(xj )

q(xi)

(28.4.22)

(28.4.23)

Up to a normalisation constant, this is therefore the KL divergence between q(xi) and a distribution pro-
portional to exp

so that the optimal setting for q(xi) satisﬁes

(cid:16)

(cid:17)

q(xi)

exp

∝

log p(x)
(cid:81)
(cid:105)

(cid:104)

j(cid:54)=i q(xj )

(cid:17)

(28.4.24)

These are known as the mean-ﬁeld equations and deﬁne a new approximation factor in terms of the previous
approximation factors. Note that if the normalisation constant of p(x) is unknown, this presents no problem
since this constant is simply absorbed into the normalisation of the factors q(xi). In other words one may
replace p(x) with the unnormalised p∗(x) in equation (28.4.24). Beginning with an initial randomly chosen
set of distributions q(xi), the mean-ﬁeld equations are iterated until convergence. Asynchronous updating
is guaranteed to decrease the KL divergence at each stage, as we show below.

28.4.3 Asynchronous updating guarantees approximation improvement

For a factorised variational approximation equation (28.4.22), we claim that each update equation (28.4.24)
reduces the Kullback-Leibler approximation error. To show this we write a single updated distribution as

log p(x)
(cid:81)
(cid:105)

j(cid:54)=i q(xj )

(cid:104)
(cid:16)

qnew
i =

1
Zi

exp

log p(x)
(cid:104)

(cid:81)
(cid:105)

j(cid:54)=i qold
j

The joint distribution under this single update is

qnew = qnew

i

qold
j

(cid:89)

j(cid:54)=i

Our interest is the change in the approximation error under this single mean-ﬁeld update:

(28.4.25)

(28.4.26)

(28.4.27)

DRAFT April 24, 2015

KL(qnew

∆

≡

p)
|

−

(cid:16)

KL

(cid:17)

qold

p
|

582

Variational Bounding Using KL(q

p)

|

x1

x4

x2

x1

x3

x4

x2

x3

(a)

(b)

Figure 28.5:
(b): A structured singly-connected approximation.

(a): A toy ‘intractable’ distribution.

Using

KL(qnew

p) =
|

log qnew
i
(cid:104)

(cid:105)qnew

i

+

(cid:88)

(cid:68)

j(cid:54)=i

log qold
j

(cid:69)

(cid:68)

j −
qold

log p(x)
(cid:105)
(cid:104)

(cid:81)

j(cid:54)=i qold
j

(cid:69)

qnew
i

and deﬁning the un-normalised distribution

q∗
i (xi) = exp

log p(x)
(cid:104)

(cid:81)
(cid:105)

j(cid:54)=i qold
j

= Ziqnew

i

i −
qold
(cid:69)
(cid:68)
log qold
i

(cid:68)
(cid:104)

log p

i − (cid:104)
qold

(cid:69)

qnew
i

j(cid:54)=i qold
j

(cid:81)
(cid:105)
log q∗

i (cid:105)qnew

i

+

(cid:104)

(cid:81)
(cid:105)

j(cid:54)=i qold
j

(cid:69)

qold
i

(cid:68)

+

log p
(cid:104)
log q∗

i (cid:105)qold

i

then

∆ =

=

=

=

(cid:104)

(cid:104)

−

−

log qnew
i

(cid:105)qnew

i −

(cid:68)
log qold
i

(cid:69)

log q∗

i (cid:105)qnew
i −
(cid:68)

log Zi

−

(cid:69)

log Zi

(cid:16)

−
qold
i

|

KL

(cid:17)

qnew
i

0

≤

Hence

(cid:16)

KL

qnew

(cid:17)

p

|

≤

(cid:16)

KL

(cid:17)

qold

p
|

log qold
i

+

log q∗
(cid:104)

i (cid:105)qold

i

qold
i

(28.4.28)

(28.4.29)

(28.4.30)

(28.4.31)

(28.4.32)

(28.4.33)

(28.4.34)

so that updating a single component of q at a time is guaranteed to improve the approximation. Note that
this result is quite general, holding for any distribution p(x). In the case of a Markov network the guaranteed
approximation improvement is equivalent to a guaranteed increase (strictly speaking a non-decrease) in the
lower bound on the partition function.

Remark 28.1 (Intractable energy). Even for a fully factorised approximation the mean-ﬁeld equations
log p∗(x)
may not be tractably implementable. For this we need to be able to compute
j(cid:54)=i q(xj ). For some
models of interest this is still not possible and additional approximations are required.

(cid:81)
(cid:105)

(cid:104)

28.4.4 Structured variational approximation

One can extend the factorised KL variational approximation by using non-factorised q(x)[256, 24]. Those
for which averages of the variables can be computed in linear time include spanning trees, ﬁg(28.4b) and
decomposable graphs ﬁg(28.4c). For example, for the distribution, ﬁg(28.5a),

p(x1, x2, x3, x4) =

1
Z

φ(x1, x2)φ(x2, x3)φ(x3, x4)φ(x4, x1)φ(x1, x3)

(28.4.35)

a tractable q distribution would be, ﬁg(28.5b),

q(x1, x2, x3, x4) =

1
˜Z

˜φ(x1, x2) ˜φ(x1, x3) ˜φ(x1, x4)

In this case we have

KL(q

p) = Hq(x1, x2) + Hq(x1, x3) + Hq(x1, x4)
|

−

3Hq(x1) +

(cid:88)

(cid:104)

i∼j

log φ(xi, xj)

(cid:105)q(xi,xj )

DRAFT April 24, 2015

(28.4.36)

(28.4.37)

583

Variational Bounding Using KL(q

p)
|

X

) is the entropy of q(

where Hq(
). Since q is singly-connected, computing the marginals and entropy
is straightforward (since the entropy requires only pairwise marginals on graph neighbours). In this case,
however, we cannot apply directly the standard mean ﬁeld update equations since there are constraints
(cid:80)
xj q(xi, xj) = q(xj). We can either deal with these using Lagrange multipliers, or ensure that in perform-

X

ing an mean-ﬁeld style update, we are including the contributions from coupled terms.

More generally one can exploit any structural approximation by use of, for example, the junction tree
algorithm to compute the required moments. However, the computational expense typically increases expo-
nentially with the hypertree width[315].

Example 28.2 (Robot arm: control via inference). Control problems can also be cast as inference problems,
see for example [165]. Consider the position vt = (xt, yt)T of an n-link robot arm in a two-dimensional place,
where each link i

in the arm is of unit length and angle hi,t:

1, . . . , n

∈ {

}

xt =

n
(cid:88)

i=1

cos hi,t,

yt =

n
(cid:88)

i=1

sin hi,t

(28.4.38)

Our interest is to use the robot arm to track a given sequence v1:T such that the joint angles ht do not
change much from one time to the next. This is a classical control problem that we can formulate as an
inference problem using a model

p(v1:T , h1:T ) = p(v1

h1)p(h1)

|

T
(cid:89)

t=2

p(vt

ht)p(ht
|

ht−1)
|

where the terms are

(28.4.39)



p(vt

ht) =
|

N

vt

(cid:32) n
(cid:88)

i=1

cos hi,t,

n
(cid:88)

i=1

(cid:33)T



sin hi,t

, σ2I

 , p(ht

ht−1) =

|

N

(cid:0)ht ht−1, ν2I(cid:1)

(28.4.40)

solution to the

control problem is

sequence
then given by the most
One
v1:T ). Here we consider an alternative, the maximum posterior marginal solution at each
arg maxh1:T p(h1:T
|
v1:T ). Due to the non-linear observation, this posterior marginal cannot be computed
time arg maxht p(ht
exactly. A simple approximation is to use a fully factorised variational distribution p(h1:T
q(h1:T )
where

likely posterior

v1:T )
|

≈

|

q(h1:T ) =

T
(cid:89)

n
(cid:89)

t=1

i=1

q(hi,t)

(28.4.41)

From the general form of the mean-ﬁeld equations, the update for q(hi,t) is given by (for 1 < t < T )

2 log q(hi,t) =

−

1
ν2

where

(cid:1)2

+

¯hi,t−1

(cid:0)hi,t
−
1
σ2 (cos hi,t

+

−

(cid:0)hi,t

1
ν2
αi,t)2 +

¯hi,t+1

(cid:1)2

−
1
σ2 (sin hi,t

βi,t)2 + const.

−

¯hi,t+1

hi,t+1

≡ (cid:104)

, αi,t

xt

−

≡

(cid:105)

(cid:88)

j(cid:54)=i

cos hj,t
(cid:104)

(cid:105)

, βi,t

yt

−

≡

(cid:88)

j(cid:54)=i

sin hj,t
(cid:104)

(cid:105)

(28.4.42)

(28.4.43)

and the above averages are with respect to the q marginal distribution. Due to the non-linearities, the above
marginal distributions are non-Gaussian. However, since they are only one-dimensional, the required aver-
ages can readily be computed using quadrature. The above mean-ﬁeld equations are iterated to convergence.
See demoRobotArm.m for further details and ﬁg(28.6) for a demonstration. Note that the planning problem
of starting from a point (x1, y1) to smoothly move the arm to a desired end point (xT , yT ) is a special case
of this framework. One can achieve this either by removing the observation terms for the intermediate times
or, equivalently, making the observation variances for those intermediate times extremely large.

584

DRAFT April 24, 2015

Local and KL Variational Approximations

(a)

(b)

(c)

Figure 28.6: (a): The desired trajectory of the end point of a three link robot arm. Green denotes time 1
(b): The learned trajectory based on a fully factorised KL variational approximation.
and red time 100.
(c): The robot arm sections every 2nd timestep, from time 1 (top left) to time 100 (bottom right). The
control problem of matching the trajectory using a smoothly changing angle set is solved using this simple
approximation.

28.5 Local and KL Variational Approximations

In ﬁtting a model parameterised by w to data

D

, one often encounters parameter posteriors of the form

p(w

) =

|D

1
Z N

(w µ, Σ) f (w)

where

(cid:90)

Z =

N

(w µ, Σ) f (w)dw

(28.5.1)

(28.5.2)

(w µ, Σ) represents a prior on
A classical example is Bayesian logistic regression, section(18.2) in which
the weight w, f (w) represents the likelihood p(
In all but limited special cases, the
function f (w) is not a simple squared exponential, resulting in a posterior distribution of a non-standard
Inevitably, therefore, approximations are required. When the dimension of the parameter vector
form.

w) and Z = p(

D|

N

D

).

DRAFT April 24, 2015

585

−2−1012−1−0.500.511.522.5−2−1012−1−0.500.511.522.5Local and KL Variational Approximations

dim (w) = W is large, ﬁnding an accurate posterior approximation is in general non-trivial. Our particular
interest here is to form an approximation of p(w
) that also gives a principled lower bound on the marginal
likelihood p(

|D

).

D

28.5.1 Local approximation

In a local method [225, 154, 124, 234], one replaces f by a suitable function for which the integral can be
computed. Since in our case the integrand is composed of a Gaussian in w, it is most convenient to bound
f (w) by an exponential quadratic function

f (w)

≥

c(ξ)e− 1

2 wTF(ξ)w+wTf (ξ)

(28.5.3)

where the matrix F(ξ), vector f (ξ) and scalar c(ξ) depend on the speciﬁc function f ; ξ is a variational
parameter that enables one to ﬁnd the tightest bound. We will discuss explicit c, F and f functions later,
but for the moment leave them unspeciﬁed. Then from equation (28.5.2)

Z

≥

c(ξ)
det (2πΣ)

(cid:112)

(cid:90)

exp

(cid:18)

1
2

−

(w

−

µ)T Σ−1 (w

(cid:19)

(cid:18)

µ)

exp

−

−

1
2

wTF(ξ)w + wTf (ξ)

(cid:19)

dw

Which can be expressed as

2 µTΣ−1µ

e− 1
(cid:112)

det (2πΣ)

(cid:90)

(cid:18)

exp

−

1
2

wTAw + wTb

(cid:19)

dw

c(ξ)

Z

≥

where

A = Σ−1 + F(ξ),

b = Σ−1µ + f (ξ)

(28.5.4)

(28.5.5)

Whilst both A and b are functions of ξ, we notationally drop this dependency to keep the description more
compact. Completing the square and integrating, we have log Z

B(ξ), with

≥

(ξ)

B

≡

log c(ξ)

1
2

−

µTΣ−1µ +

bTAb

1
2

1
2

−

log det (ΣA)

(28.5.6)

To obtain the tightest bound on log Z, one maximises B(ξ) with respect to ξ. In many practical problems
of interest, f (w) = (cid:81)M
s=1 fs(w) for local site functions fs. By bounding each site individually, we obtain a
bound

(ξ1, . . . , ξM ). The bound may be then optimised numerically with respect to the vector ξ.

B

28.5.2 KL variational approximation

An alternative to the above local bounding method is to consider a KL approach based on ﬁtting a Gaussian
to the distribution. Deﬁning

˜p(w)

N

≡

(w µ, Σ) f (w)

Z

(28.5.7)

By ﬁtting a Gaussian q(w) =
log Z

KL(m, S) with

≥ B

(w m, S) based on minimising the KL(q(w)

˜p(w)), we obtain the bound
|

N

KL(m, S)

B

≡ − (cid:104)

log q(w)

(cid:105) −

1
2

log det (2πΣ)

(cid:68)

(w

1
2

−

−

µ)T Σ−1 (w

(cid:69)

µ)

−

+

log f (w)
(cid:105)
(cid:104)

(cid:104)·(cid:105)

denotes expectation with respect to q(w). One then numerically ﬁnds the best parameters m, S
where
that maximise the bound. Since the entropy of a Gaussian is trivial, the only potentially problematic term
. A class of functions for which
in evaluating this bound is
(cid:105)N (w m,S) is computationally
(cid:105)
tractable is when f (w) = f (wTh) for some ﬁxed vector h. In this case, the projection wTh is also Gaussian
distributed and

log f (w)
(cid:104)

log f (w)
(cid:104)

(cid:68)

(cid:69)
log f (wTh)

N (w m,S)

=

log f (a)
(cid:104)

(cid:105)N (a mTh,hTSh)

586

(28.5.8)

DRAFT April 24, 2015

Mutual Information Maximisation : A KL Variational Approach

which can be readily computed using any one-dimensional integration routine. Explicitly, as a function of
m, S, we have

2

KL(m, S)

B

≡ −

log det (S) + W + log det (Σ)
(cid:16)

Σ−1 (cid:16)

S + (m

trace

−

µ) (m

−

−

µ)T(cid:17)(cid:17)

+ 2

log f (a)
(cid:104)

(cid:105)N (a mTh,hTSh)

(28.5.9)

Whilst, in general, the variational bounds are non-concave in their variational parameters, provided f is
KL(m, S) is jointly concave in m and S. By using structured covariances S the method
log-concave then
is scalable to very high dimensional problems [60].

B

Relation between the KL and local bounds

The KL and local variational methods both provide a lower bound on the normalisation term Z in equation
(28.5.2). It is interesting therefore to understand what relation there is between these bounds. Using the
bound on f (w), we obtain a new bound

KL(m, S)

B

˜
KL(m, S, ξ)
B

≥

where

(28.5.10)

˜
KL
B

≡ − (cid:104)

log q(w)

(cid:105)−

1
2

log det (2πΣ)

(cid:68)

(w

1
2

−

−

µ)T Σ−1 (w

(cid:69)

µ)

−

+log c(ξ)

(cid:68)

1
2

−

wTF(ξ)w

(cid:69)

(cid:68)

+

wTf (ξ)

(cid:69)

which, using equation (28.5.5), can be written as

log q(w)

− (cid:104)

1
2

log det (2πΣ) + log c(ξ)

1
2

−

µTΣ−1µ

1
2

−

(cid:68)

wTAw

(cid:69)

+

(cid:68)

wTb

(cid:69)

(28.5.11)

(cid:105) −

˜
KL =
B

By deﬁning

˜q(w) =

N

(cid:0)w A−1b, A−1(cid:1)

then

(28.5.12)

˜
KL =
B

KL(q(w)
|

−

˜q(w))

1
2

−

log det (2πΣ) + log c(ξ)

1
2

−

µTΣ−1µ +

bTA−1b +

1
2

1
2

log det (cid:0)2πA−1(cid:1)

Since m, S only appear via q(w) in the KL term, the tightest bound is given when m, S are set such that
q(w) = ˜q(w). At this setting the KL term in ˜
B

KL disappears and m and S are given by

Sξ = (cid:0)Σ−1 + F(ξ)(cid:1)−1

,

mξ = Sξ

(cid:0)Σ−1µ + f (ξ)(cid:1) .

For this setting of the variational parameters the bound matches the local bound (28.5.6). Since
˜
KL(m, S, ξ) we therefore have that,
B

KL(mξ, Sξ)

B

˜
KL(mξ, Sξ, ξ) =
B

B

≥

(ξ).

Importantly, the VG bound can be tightened beyond this setting:

max
m,S B

KL(m, S)

KL(mξ, Sξ)

≥ B

(28.5.13)

KL(m, S)

B

≥

(28.5.14)

(28.5.15)

Thus the optimal KL bound is provably tighter than both the local variational bound (28.5.6) and the KL
bound calculated using the optimal local moments mξ and Sξ.

28.6 Mutual Information Maximisation : A KL Variational Approach

Here we take a short interlude to discuss an application of the Kullback-Leibler variational approach in
information theory. A common goal is to maximise information transfer, measured by the mutual information
(see also deﬁnition(8.13))

I(X, Y )

H(X)

H(X

Y )

|

−

≡

DRAFT April 24, 2015

(28.6.1)

587

Mutual Information Maximisation : A KL Variational Approach

x

y1

y2

y3

y4

Figure 28.7: An information transfer problem. For a ﬁxed distribution
i x(cid:1), ﬁnd the op-
p(x) and parameterised distributions p(yj
timal parameters wi that maximise the mutual information between
the variables x and y. Such considerations are popular in theoretical
neuroscience and aim to understand how the receptive ﬁelds wi of a
neuron relate to the statistics of the environment p(x).

x) = σ (cid:0)wT

|

Algorithm 28.1 IM algorithm for maximising mutual information I(X, Y ) for ﬁxed p(x) and adjustable
parameters are p(y

x, θ).
|

1: Choose a class of approximating distributions Q (for example factorised)
2: Initialise the parameters θ
3: repeat
4:

θnew = argmax

log q(x

y)

5:

qnew(x

θ

(cid:104)

y) = argmax
|

q(x|y)∈Q (cid:104)

|

(cid:105)p(x)p(y|x,θ)
y)

log q(x

|

(cid:105)p(x)p(y|x,θnew)

6: until converged

where the entropy and conditional entropy are deﬁned

H(X)

≡ − (cid:104)

log p(x)

(cid:105)p(x) ,

H(X

Y )
|

≡ − (cid:104)

log p(x

y)
|

(cid:105)p(x,y)

(28.6.2)

Here we are interested in the situation in which p(x) is ﬁxed, but p(y
x, θ) has adjustable parameters θ that
|
we wish to set in order to maximise I(X, Y ). In this case H(X) is constant and the optimisation problem is
equivalent to minimising the conditional entropy H(X
Y ). Unfortunately, in many cases of practical interest
|
H(X
Y ) is computationally intractable. We discuss in section(28.6.1) a general procedure based on the
|
Kullback-Leibler divergence to approximately maximise the mutual information.

Example 28.3. Consider a neural transmission system in which xi
a non-ﬁring state (0) or ﬁring state (1), and yj
independently, depending only on the emitting neurons, we have

0, 1
}

∈ {

denotes an emitting neuron in
a receiving neuron. If each receiving neuron ﬁres

∈ {

0, 1

}

x) =

p(y

|

(cid:89)

i

p(yi

x)
|

where for example we could use

x) = σ
p(yi = 1
|

(cid:17)

(cid:16)

wT

i x

(28.6.3)

(28.6.4)

Given an empirical distribution on neural ﬁrings p(x), our interest is to set the weights
information transfer, see ﬁg(28.7). Since p(x) is ﬁxed, this requires maximising

wi

{

}

to maximise

log p(x
(cid:104)

|

y)

(cid:105)p(y|x)p(x)
y) = p(y

The quantity p(x
that the conditional entropy is typically intractable to compute and we require an approximation.

x)p(x)/p(y) is a non-factorised function of y (due to the term p(y). This means

|

|

(28.6.5)

28.6.1 The information maximisation algorithm

Consider

KL(p(x

y)
|

q(x
|

y))
|

≥

0

588

(28.6.6)

DRAFT April 24, 2015

Mutual Information Maximisation : A KL Variational Approach

c

a

e

d

b

f

Figure 28.8: Belief propagation can be derived by considering
how to compute the marginal of a variable on a MRF. In this
case the marginal p(d) depends on messages transmitted via the
neighbours of d. By deﬁning local messages on the links of the
graph, a recursive algorithm for computing all marginals can be
derived, see text.

This immediately gives a bound

(cid:88)

x

p(x

y) log p(x
|

y)
|

−

(cid:88)

x

p(x

|

y) log q(x

y)
|

≥

0

Multiplying both sides by p(y), we obtain

(cid:88)

x,y

p(y)p(x

|

y) log p(x

y)
|

≥

(cid:88)

x,y

p(x, y) log q(x
|

y)

From the deﬁnition, the left side of the above bound is

H(X

−

Y ). Hence
|

I(X, Y )

H(X) +

log q(x

y)
|

(cid:105)p(x,y) ≡

(cid:104)

≥

˜I(X, Y )

(28.6.7)

(28.6.8)

(28.6.9)

From this lower bound on the mutual information we arrive at the information maximisation (IM) algorithm[20].
x, θ), we seek to maximise ˜I(X, Y ) with re-
Given a distribution p(x) and a parameterised distribution p(y
spect to θ. A co-ordinate wise optimisation procedure is presented in algorithm(28.1). The Blahut-Arimoto
algorithm in information theory (see for example [198]) is a special case in which the optimal decoder

|

q(x

y)
|

∝

p(y

x, θ)p(x)
|

(28.6.10)

is used. In applications where the Blahut-Arimoto algorithm is intractable to implement, the IM algorithm
can provide an alternative by restricting q to a tractable family of distributions (tractable in the sense that
the lower bound can be computed). The Blahut-Arimoto algorithm is analogous to the EM algorithm for
maximum likelihood and guarantees a non-decrease of the mutual information at each stage of the update,
see section(11.2.2). Similarly, the IM procedure is analogous to a variational EM procedure and each step
of the procedure cannot decrease the lower bound on the mutual information.

28.6.2 Linear Gaussian decoder

A special case of the IM framework is to use a linear Gaussian decoder

q(x

y) =
|

N

(x Uy, Σ)

log q(x

y) =
|

−

⇒

1
2

(x

−

Uy)T Σ−1 (x

Uy)

−

1
2

−

log det (2πΣ)

(28.6.11)

Plugging this into the bound, equation (28.6.9), and optimising with respect to Σ, and U, we obtain

Σ =

(cid:68)

(x

Uy) (x

−

−

Uy)T(cid:69)

,

(cid:68)

xyT(cid:69) (cid:68)

yyT(cid:69)−1

U =

(28.6.12)

where

(cid:104)·(cid:105) ≡ (cid:104)·(cid:105)p(x,y). Using this setting in the bound we obtain
(cid:18)(cid:68)
yyT(cid:69)−1 (cid:68)

xyT(cid:69) (cid:68)

xxT(cid:69)

log det

H(X)

I(X, Y )

(cid:68)

yxT(cid:69)(cid:19)

1
2

−

≥

−

N
2

−

(1 + log 2π)

(28.6.13)

where N = dim (x). This is equivalent to Linsker’s as-if-Gaussian approximation to the mutual information
[190]. One can therefore view Linsker’s approach as a special case of the IM algorithm restricted to linear-
Gaussian decoders. In principle, one can therefore improve on Linsker’s method by considering more powerful
non-linear-Gaussian decoders. Applications of this technique to neural systems are discussed in [20].

DRAFT April 24, 2015

589

Loopy Belief Propagation

x1

x2

λ

x

2

→

x

i(

x

i)

xi

λx1→xi (xi)
(x i)
→ x i

λ x 3

x3

λxi→xj (xj)

xj

Figure 28.9: Loopy Belief Propagation. Once a node has received
incoming messages from all neighbours (excluding the one it wants to
send a message to), it may send an outgoing message to a neighbour:

λxi→xj (xj) =

(cid:88)

xi

φ (xi, xj)λx1→xi (xi) λx2→xi (xi) λx3→xi (xi)

28.7 Loopy Belief Propagation

Belief Propagation is a technique for exact inference of marginals p(xi) for singly-connected distributions
p(x). There are diﬀerent formulations of BP, the most modern treatment being the sum-product algorithm
on the corresponding factor graph, as described in section(5.1.2). An important observation is that the
algorithm is purely local – the updates are unaware of the global structure of the graph. This means that
even if the graph is multiply-connected (it is loopy) one can still apply the algorithm and ‘see what happens’.
Provided the loops in the graph are relatively long, one may hope that ‘loopy’ BP will converge to a good
approximation of the true marginals. When the method converges the results can be surprisingly accurate.
In the following we will show how loopy BP can also be motivated by a variational objective. To do so, we
make a connection to the classical BP algorithm (rather than the factor graph sum-product algorithm). For
this reason we brieﬂy describe below the classical BP approach.

28.7.1 Classical BP on an undirected graph

BP can be derived by considering how to calculate a marginal in terms of messages on an undirected
graph. Consider calculating the marginal p(d) = (cid:80)
a,b,c,e,f p(a, b, c, d, e, f ) for the pairwise Markov network
in ﬁg(28.8). We denote both a node and its state by the same symbol, so that (cid:80)
b φ (d, b) denotes summation
over the states of the variable b. To compute the summation eﬃciently we may distribute the summations
as follows:

p(d) =

1
Z

(cid:88)

(cid:88)

φ (b, d)

φ (a, d)

(cid:88)

φ (d, f )

(cid:124)

b

(cid:123)(cid:122)
λb→d(d)

(cid:125)

(cid:124)

a

(cid:123)(cid:122)
λa→d(d)

f

(cid:125)

(cid:124)

(cid:123)(cid:122)
λf→d(d)

(cid:125)

(cid:88)

e

(cid:124)

φ (d, e)

(cid:88)

φ (c, e)

(cid:124)

c

(cid:123)(cid:122)
λc→e(e)

(cid:123)(cid:122)
λe→d(d)

(cid:125)

(cid:125)

(28.7.1)

where we deﬁne messages λn1→n2 (n2) sending information from node n1 to node n2 as a function of the
state of node n2. In general, a node xi passes a message to node xj via

λxi→xj (xj) =

(cid:88)

xi

φ (xi, xj)

(cid:89)

λxk→xi (xi)

k∈ne(i),k(cid:54)=j

See also ﬁg(28.9). At convergence the marginal p(xi) is then given by

q(xi)

∝

(cid:89)

i∈ne(j)

λxj→xi (xi)

the prefactor being determined by normalisation. The pairwise marginals are approximated by

q(xi, xj)

∝





(cid:89)

k∈ne(i)\j







λxk→xi (xi)

 φ(xi, xj)



(cid:89)

λxk→xj (xj)



k∈ne(j)\i

(28.7.2)

(28.7.3)

(28.7.4)

For a singly-connected distribution p, this message passing scheme converges and the marginal corresponds
to the exact result. For multiply-connected (loopy) structures, belief propagation will generally result in an
approximation.

590

DRAFT April 24, 2015

Loopy Belief Propagation

28.7.2 Loopy BP as a variational procedure

A variational procedure that corresponds to loopy BP can be derived by considering the terms of a standard
variational approximation based on the Kullback-Leibler divergence KL(q
p)[320]. We take as our example
|
a pairwise Markov network deﬁned on potentials φ(xi, xj),

p(x) =

1
Z

(cid:89)

i∼j

φ(xi, xj)

(28.7.5)

where i
an approximating distribution q(x), the Kullback-Leibler bound is

j denotes the unique neighbouring edges on the graph (each edge is counted only once). Using

∼

log Z

≥ − (cid:104)
(cid:124)

log q(x)
(cid:123)(cid:122)
entropy

(cid:105)q(x)
(cid:125)

+

(cid:88)

i∼j
(cid:124)

(cid:104)

log φ(xi, xj)

(cid:105)q(x)
(cid:125)

(cid:123)(cid:122)
energy

Since

log φ(xi, xj)

(cid:105)q(x) =

(cid:104)

(cid:104)

log φ(xi, xj)

(cid:105)q(xi,xj )

(28.7.6)

(28.7.7)

each contribution to the energy depends on q(x) only via the pairwise marginals q(xi, xj). This suggests that
these marginals should form the natural parameters of any approximation. Can we then ﬁnd an expression
(cid:105)q(x) in terms of these pairwise marginals? Consider a case in which the required
for the entropy
marginals are

log q(x)

− (cid:104)

q(x1, x2), q(x2, x3), q(x3, x4)

(28.7.8)

Either by appealing to the junction tree representation, or by straightforward algebra, one can show that
we can uniquely express q in terms of these marginals using

q(x) =

q(x1, x2)q(x2, x3)q(x3, x4)
q(x2)q(x3)

(28.7.9)

An intuitive way to arrive at this result is by examining the numerator of equation (28.7.9). The variable x2
appears twice, as does the variable x3 and, since any joint distribution cannot have such replicated variables,
we must compensate for ‘overcounting’ x2 and x3 by dividing by these marginals. In this case, the entropy
of q(x) can be written as

Hq(x) =

−
More generally, from chapter(6), any decomposable graph can be represented as

(cid:105)q(x) = Hq(x1, x2) + Hq(x2, x3) + Hq(x3, x4)

log q(x)

Hq(x2)

− (cid:104)

−

q(x) =

(cid:81)
(cid:81)

c q(
s q(

c)
s)

X
X

Hq(x3)

(28.7.10)

(28.7.11)

c) are the marginals deﬁned on cliques of the graph, with

where the q(
and the q(
entropy of the distribution is then given by a sum of marginal entropies minus the separator entropies.

c being the variables of the clique,
X
s) are deﬁned on the separators (intersections of neighbouring cliques). The expression for the

X

X

Bethe Free energy

Consider now a Markov network corresponding to a non-decomposable graph, for example the 4-cycle

p(x) =

1
Z

φ(x1, x2)φ(x2, x3)φ(x3, x4)φ(x4, x1)

The energy requires therefore that our approximating distribution deﬁnes the pairwise marginals

q(x1, x2), q(x2, x3), q(x3, x4), q(x4, x1)

(28.7.12)

(28.7.13)

Assuming that these marginals are given, can we ﬁnd an expression for the entropy of the joint distribution
q(x) in terms of its pairwise marginals q(xi, xj)? In general this is not possible since the graph contains loops

DRAFT April 24, 2015

591

Loopy Belief Propagation

(so that the junction tree representation would result in cliques greater than size 2). However, a simple ‘no
overcounting’ approximation is to write

q(x)

q(x1, x2)q(x2, x3)q(x3, x4)q(x4, x1)
q(x1)q(x2)q(x3)q(x4)

.

≈

Using this we can approximate the entropy as

Hq(x)

≈

Hq(x1, x2) + Hq(x2, x3) + Hq(x3, x4) + Hq(x1, x4)

4
(cid:88)

i=1

−

Hq(xi)

(28.7.14)

(28.7.15)

In general, to make the distribution ‘dimensionally consistent’ we need to compensate by a factor q(xi)ci
where ci is the number of neighbours of variable xi minus 1. With this approximation the (negative) log
partition function is known as the Bethe free energy. Our interest is then to maximise this expression with
respect to the parameters q(xi, xj) subject to marginal consistency constraints, (cid:80)
xj q(xi, xj) = q(xi). These
constraints may be enforced using Lagrange multipliers γij(xi). One can write the Bethe free energy (the
approximated Kullback-Leibler divergence up to a constant) as

(q, λ)

F

≡ −

(cid:88)

i∼j

Hq(xi, xj)+

(cid:88)

i

ciHq(xi)

(cid:88)

−

i∼j

log φ(xi, xj)
(cid:104)

(cid:105)q(xi,xj ) +

(cid:88)

(cid:88)

i∼j

xi



γij(xi)

q(xi)

(cid:88)

−

xj



q(xi, xj)



(28.7.16)

We neglect including Lagrange terms to enforce normalisation of the q since these only add constant terms
which can be later absorbed by explicitly normalising q. Expression (28.7.16) is no longer a bound on the
log partition function since the entropy approximation is not a lower bound on the true entropy. The task
is now to minimize equation (28.7.16) with respect to the parameters, namely all the pairwise marginals
q(xi, xj) and the Lagrange multipliers γ. A simple scheme to optimise equation (28.7.16) is to use a ﬁxed
point iteration by equating the derivatives of the Bethe free energy with respect to the parameters q(xi, xj)
to zero, and likewise for the Lagrange multipliers. Diﬀerentiating with respect to q(xi, xj) and equating to
zero, we obtain

log q(xi, xj)

log φ(xi, xj)

γij(xi)

−

−

−

γji(xj) + const. = 0

so that

q(xi, xj)

∝

φ(xi, xj)˜γij(xi)˜γji(xj)

(28.7.17)

(28.7.18)

where ˜γij(xi)

exp γij(xi). Similarly, diﬀerentiating with respect to q(xi) and equating to zero, we obtain

≡
ci log q(xi) +

−

(cid:88)

j∈ne(i)

γij(xi) + const. = 0

so that

q(xi)

∝

(cid:89)

1/ci
ij

˜γ

(xi)

j∈ne(i)

From equation (28.7.18) we can match equation (28.7.4) by mapping

˜γij(xi) =

(cid:89)

λxk→xi (xi)

k∈ne(i)\j

(28.7.19)

(28.7.20)

(28.7.21)

We can verify that this assignment satisﬁes the single marginal requirements equation (28.7.20) and equation
(28.7.3) since

(cid:89)

1/ci
ij

˜γ

(xi) =

(cid:89)

(cid:89)

λxk→xi (xi)1/ci =

j∈ne(i)

j∈ne(i)

k∈ne(i)\j

(cid:89)

j∈ne(i)

λxj→xi (xi)

(28.7.22)

Hence the ﬁxed point equations for minimising the Bethe free energy are equivalent to belief propagation
[320]. The convergence of loopy belief propagation can be heavily dependent on the topology of the graph
and also the message updating schedule[312, 216]. The potential beneﬁt of the Bethe free energy viewpoint is
that it opens up the possibility of using more general optimisation techniques than BP. The so-called double-
loop techniques iteratively isolate convex contributions to the Bethe Free energy, interleaved with concave
contributions. At each stage, the resulting optimisiations can be carried out eﬃciently[322, 145, 320].

592

DRAFT April 24, 2015

Expectation Propagation

(a)

(b)

(c)

(a): The Markov network (left) that we wish to approximate the marginals
Figure 28.10:
p(w), p(x), p(y), p(z) for. All tables are drawn ﬁrst from a uniform distribution and then raised to a power
(b):
α and renormalised. On the right is shown the naive mean ﬁeld approximation factorised structure.
There are 64 = 1296 states of the distribution. Shown is a randomly sampled distribution for α = 5 which
has many isolated peaks, suggesting the distribution is far from factorised. In this case the MF and Gibbs
(c): As α is increased to 25, typically only one state of
sampling approximations may perform poorly.
the distribution dominates. Whilst the distribution is then simple and essentially factorised, ﬁnding this
maximal single state is numerically challenging. See demoMFBPGibbs.m and ﬁg(28.11).

Validity of loopy belief propagation

For a Markov network which has a loop, a change in a variable on the loop eventually reverberates back
to the same variable. However, if there are a large number of variables in the loop, and the individual
neighbouring links are not all extremely strong, the numerical eﬀect of the loop is small in the sense that
the inﬂuence of the variable on itself will be small. In such cases one would expect the belief propagation
approximation to be accurate. An area of particular success for loopy belief propagation inference is in error
correction based on low density parity check codes; these are usually explicitly designed to have this long-
loop property[197] so that loopy belief propagation produces good results. In many examples of practical
interest, however, loops can be very short (for example a Markov network on a lattice).
In such cases
a naive implementation of loopy BP will most likely fail. A natural extension is to cluster variables to
alleviate strong local dependencies; this technique is called the Kikuchi or Cluster Variation method [167].
More elaborate ways of clustering variables can be considered using region graphs[320, 313].

Example 28.4. The ﬁle demoMFBPGibbs.m compares the performance of naive Mean Field (MF) theory,
belief propagation and unstructured Gibbs sampling on marginal inference in a pairwise Markov network

p(w, x, y, z) = φwx(w, x)φwy(w, y)φwz(w, z)φxy(x, y)φxz(x, z)φyz(y, z)

(28.7.23)

in which all variables take 6 states. In the experiment the tables are selected from a uniform distribution
raised to a power α. For α close to zero, all the tables are essentially ﬂat and therefore the variables become
independent, a situation for which MF, BP and Gibbs sampling are ideally suited. As α is increased to 5, the
dependencies amongst the variables increase and the methods perform worse, especially MF and Gibbs. As
α is increased to 25, the distribution becomes sharply peaked around a single state, such that the posterior
is eﬀectively factorised, see ﬁg(28.10). This suggests that a MF approximation (and also Gibbs sampling)
should work well. However, ﬁnding this state is computationally diﬃcult and the methods often get stuck
in local minima, see ﬁg(28.11). Belief propagation seems less susceptible to being trapped in local minima
in this regime and tends to outperform both MF and Gibbs sampling.

28.8 Expectation Propagation

The messages in schemes such as belief propagation are not always representable in a compact form. The
switching linear dynamical system, as described in chapter(25) is such an instance, with the messages requir-

DRAFT April 24, 2015

593

wxyzoriginal graphwxyzFactorised graph0500100000.050.10.150.2p distribution0500100000.20.40.60.81p distributionExpectation Propagation

ing an exponential amount of storage. This limits BP to cases such as discrete networks, or more generally
exponential family messages. Expectation Propagation extends the applicability of BP by projecting the
messages back to a chosen distribution family at each stage. This projection is obtained by using a Kullback-
Leibler measure[210, 263, 212].

Consider a distribution deﬁned on subsets of variables

i of the form

X

p(x) =

1
Z

(cid:89)

i

φi(

X

i)

(28.8.1)

In EP one identiﬁes those factors φi(
distribution ˜p(x) tractable. One then sets any free parameters of ˜φi(
divergence KL(p
steps of the algorithm, we consider the speciﬁc example a pairwise Markov network

i), would render the
i) by minimising the Kullback-Leibler
˜p). The general approach is outlined in algorithm(28.2). To gain some intuition into the
|

i) which, if replaced by simpler factors ˜φi(

X

X

X

p(x) =

1
Z

φ1,2(x1, x2)φ2,3(x2, x3)φ3,4(x3, x4)φ4,1(x4, x1)

(28.8.2)

If we replace all terms φi,j(xi, xj) by approximate factors
with factor graph as depicted in ﬁg(28.12a).
˜φi,j(xi) ˜φi,j(xj) then the resulting joint distribution ˜p is factorised and hence tractable. Since the variable
xi appears in more than one term from p, we need to index the approximation factors appropriately. A
convenient way to do this is

˜p(x) =

1
˜Z

˜φ2→1 (x1) ˜φ1→2 (x2)
(cid:123)(cid:122)
(cid:125)
(cid:124)
≈φ1,2(x1,x2)

˜φ3→2 (x2) ˜φ2→3 (x3)
(cid:123)(cid:122)
(cid:125)
(cid:124)
≈φ2,3(x2,x3)

˜φ4→3 (x3) ˜φ3→4 (x4)
(cid:125)
(cid:123)(cid:122)
(cid:124)
≈φ3,4(x3,x4)

˜φ1→4 (x4) ˜φ4→1 (x1)
(cid:125)
(cid:123)(cid:122)
(cid:124)
≈φ4,1(x4,x1)

(28.8.3)

which is represented in ﬁg(28.12b). The idea in EP is now to determine the optimal approximation factors
˜φ by the self-consistent requirement that, on replacing approximation factors by their exact counterparts,
there is no diﬀerence to the marginals of ˜p. Consider the approximation parameters ˜φ3→2 (x2) and ˜φ2→3 (x3).
To set these we ﬁrst replace the contribution ˜φ3→2 (x2) ˜φ2→3 (x3) by the exact factor φ2,3(x2, x3). This gives
a modiﬁed approximation

˜p∗ =

1
˜Z∗

˜φ2→1 (x1) ˜φ1→2 (x2) φ(x2, x3) ˜φ4→3 (x3) ˜φ3→4 (x4) ˜φ1→4 (x4) ˜φ4→1 (x1) =

φ2,3(x2, x3) ˜Z ˜p
˜φ3→2 (x2) ˜φ2→3 (x3) ˜Z∗
(28.8.4)

The intuition is that if all approximation parameters are set correctly, then replacing the approximation
factors by the exact counterpart should not change the computation of the marginals. To measure how
much using the modiﬁed ˜p∗ changes from ˜p we use the Kullback-Leibler divergence between this distribution
and our approximation,

KL(˜p∗

˜p) =

|

log ˜p∗
(cid:104)

(cid:105)˜p∗ − (cid:104)

log ˜p

(cid:105)˜p∗

(28.8.5)

Figure 28.11: The absolute error in computing the marginal p(xi)
for the graph in ﬁg(28.10), averaged over all four marginals, for
loopy belief propagation (BP), Gibbs sampling (Gibbs) and naive
mean ﬁeld using a factorised approximation (MF). Plotted along
the horizontal axis is the parameter α that controls the ‘complex-
ity’ of the true distribution, see ﬁg(28.10). The vertical axis is the
error averaged over 100 random realisations for the true distribu-
tion. For small α, the distribution is essentially factorised and all
methods work reasonably well. As α increases the distribution
becomes more peaked around a limited number of states, and
ﬁnding these states becomes increasingly diﬃcult. All methods
use a similar amount of computation with 50 updates for each of
the four variables.

594

DRAFT April 24, 2015

051015202500.050.10.150.20.25α  BPMFGibbsExpectation Propagation

x1

x4

x2

x1

x2

x1

x3

x4

x3

x4

(a)

(b)

(c)

x2

x3

(a): Multiply-connected fac-
Figure 28.12:
(b): Expecta-
tor graph representing p(x).
tion propagation approximates (a) in terms of a
tractable factor graph. The open squares indi-
cate that the factors are parameters of the ap-
proximation. The basic EP approximation is to
replace all factors in p(x) by product factors.
(c): Tree structured EP.

We would like to set the parameters such that this Kullback-Leibler divergence is minimal. Since our
interest is in updating ˜φ3→2 (x2) and ˜φ2→3 (x3), we isolate the contribution from these parameters to the
Kullback-Leibler divergence which is

KL(˜p∗

˜p) = log ˜Z
|

−

(cid:68)

(cid:69)
log ˜φ3→2 (x2) ˜φ2→3 (x3)

+ const.

˜p∗(x2,x3)

(28.8.6)

Also, since ˜p is factorised, up to a constant proportionality factor, the dependence of ˜Z on ˜φ3→2 (x2) and
˜φ2→3 (x3) is

˜Z

(cid:88)

∝

x2

˜φ1→2 (x2) ˜φ3→2 (x2)

(cid:88)

x3

˜φ2→3 (x3) ˜φ4→3 (x3)

(28.8.7)

Diﬀerentiating the Kullback-Leibler divergence equation (28.8.6) with respect to ˜φ3→2 (x2) and equating to
zero, we obtain

˜φ1→2 (x2) ˜φ3→2 (x2)

(cid:80)

x2

˜φ1→2 (x2) ˜φ3→2 (x2)

= ˜p∗(x2)

Similarly, optimising w.r.t. ˜φ2→3 (x3) gives

˜φ2→3 (x3) ˜φ4→3 (x3)

(cid:80)

x3

˜φ2→3 (x3) ˜φ4→3 (x3)

= ˜p∗(x3)

Approximating Z

(28.8.8)

(28.8.9)

The above updates only determine the approximation factors up to a proportionality constant. This is ﬁne
if we only wish to ﬁnd approximations for the marginals since any missing proportionality constants can be
determined by the requirement that each marginal is normalised. However if we wish to also approximate
Z, we care about such factors. To address this we write the optimal updates as

˜φ3→2 (x2) = z3→2

˜p∗(x2)
˜φ1→2 (x2)

and

˜φ2→3 (x3) = z2→3

˜p∗(x3)
˜φ4→3 (x3)

(28.8.10)

(28.8.11)

where z3→2 and z2→3 are proportionality terms. We can determine these proportionalities by the requirement
that the term approximation ˜φ3→2 (x2) ˜φ2→3 (x3) has the same eﬀect on the normalisation of ˜p as it has on
˜p∗. That is

(cid:88)

x1,x2,x3,x4

˜φ2→1 (x1) ˜φ1→2 (x2) ˜φ3→2 (x2) ˜φ2→3 (x3) ˜φ4→3 (x3) ˜φ3→4 (x4) ˜φ1→4 (x4) ˜φ4→1 (x1)

(cid:88)

=

x1,x2,x3,x4

˜φ2→1 (x1) ˜φ1→2 (x2) φ(x2, x3) ˜φ4→3 (x3) ˜φ3→4 (x4) ˜φ1→4 (x4) ˜φ4→1 (x1)

(28.8.12)

DRAFT April 24, 2015

595

which, on substituting in the updates equation (28.8.10) and equation (28.8.11), reduces to

MAP for Markov networks

z2→3z3→2 =

z∗
2,3
˜z2,3

where

and

˜z2,3 =

z∗
2,3 =

(cid:88)

x2,x3

(cid:88)

x2,x3

˜φ1→2 (x2)

˜p∗(x2)
˜φ1→2 (x2)

˜p∗(x3)
˜φ4→3 (x3)

˜φ4→3 (x3)

˜φ1→2 (x2) φ(x2, x3) ˜φ4→3 (x3)

(28.8.13)

(28.8.14)

(28.8.15)

Any choice of local normalisations z2→3, z3→2 that satisﬁes equation (28.8.13) suﬃces to ensure that the
scale of the term approximation matches. For example, one may set

z2→3 = z3→2 =

(cid:115)

z∗
2,3
˜z2,3

Once set, an approximation for the global normalisation constant of p is

(28.8.16)

˜Z

Z

≈

(28.8.17)
The above gives a procedure for updating the terms ˜φ3→2 (x2) and ˜φ2→3 (x3). One then chooses another term
and updates the corresponding approximation factors. We repeat this until all approximation parameters
have converged (or a suitable termination criterion is reached in the case of non-convergence). The generic
procedure is outlined in algorithm(28.2).

Comments on EP

•

•

•

•

For the Markov network example above, EP corresponds to belief propagation (the sum-product form
on the factor graph). This is intuitively clear since in both EP and BP the product of messages
incoming to a variable is proportional to the approximation of the marginal of that variable. A
diﬀerence, however, is the schedule:
in EP all messages corresponding to a term approximation are
updated simultaneously (in the above ˜φ3→2 (x2) and ˜φ2→3 (x3)), whereas in BP they are updated
sequentially.
EP is a useful extension of BP to cases in which the BP messages cannot be easily represented;
introducing the exact factors φ in the approximation ˜p increases the complexity of the approximation
to ˜p∗ which is resolved by projecting the approximation back to ˜p. In the case that the approximating
distribution ˜p is in the exponential family, the minimal Kullback-Leibler projection step equates to
matching moments of the approximating distribution to p∗. See [263] for a more detailed discussion.
In general there is no need to replace all terms in the joint distribution with factorised approximations.
One only needs that the resulting approximating distribution is tractable; this results in a structured
Expectation Propagation algorithm, see ﬁg(28.12c).
EP and its extensions are closely related to procedures such as tree-reweighting[307] and fractional
EP[316] designed to compensate for message overcounting eﬀects.

28.9 MAP for Markov networks

Consider a Markov network
1
Z

p(x) =

eE(x)

Then the most likely state is given by

x∗ = argmax

x

p(x) = argmax

x

E(x)

(28.9.1)

(28.9.2)

For a general Markov network we cannot naively exploit dynamic programming intuitions to ﬁnd an exact
solution since the graph will generally be loopy. Below we consider some general techniques that can be
used to approximate x∗.

596

DRAFT April 24, 2015

MAP for Markov networks

Algorithm 28.2 Expectation Propagation: approximation of p(x) = 1
Z

(cid:81)

i φi(

X

i).

1: Choose terms ˜φi(

i) to give a tractable distribution:

˜p(x) =

1
˜Z

˜φi(

i)

X

X
(cid:89)

i

5:

6:

7:

2: Initialise all parameters ˜φi(
3: repeat
4:

Select a term ˜φi(
Replace the term ˜φi(

X

i).

X

˜p∗

φi(

i)

X

∝

(cid:89)

j(cid:54)=i

X
˜φj(

j)

X

i) from ˜p to update.

i) by the exact term φi(

i) to form

X

Find the parameters of ˜φi(

X

i) by

˜φi(

X

i)

argmin
˜φi(Xi)

∝

KL(˜p∗

˜p)

|

Set any proportionality terms of ˜φi(

i) by requiring

(cid:88)

x

φi(

i)

X

(cid:89)

j(cid:54)=i

˜φj(

j) =

X

(cid:88)

(cid:89)

x

j

X
˜φj(

j)

X

8: until converged
9: return

˜p(x) =

1
˜Z

(cid:89)

i

˜φi(

i),

X

(cid:88)

(cid:89)

˜Z =

x

i

˜φi(

i)

X

(28.8.18)

(28.8.19)

(28.8.20)

(28.8.21)

(28.8.22)

as an approximation to p(x), where ˜Z approximates the normalisation constant Z.

Iterated Conditional Modes

A simple general approximate solution can be found as follows: ﬁrst initialise all x at random. Then select
a variable xi and ﬁnd the state of xi that maximally improves E(x), keeping all other variables ﬁxed.
One then repeats this selection and local maximal state computation until convergence. This axis aligned
optimisation procedure is called Iterated Conditional Modes[37]. Due to the Markov properties it’s clear
that we can improve on this ICM method by simultaneously optimising all variables conditioned on their
respective Markov blankets (similar to the approach used in black-white sampling). Another improvement
is to clamp a subset of the variables to reveal a singly-connected structure on the un-clamped variables, and
subsequently to ﬁnd the exact MAP state on the un-clamped variables by a max-sum algorithm. One then
chooses a new subset of variables to clamp, thus ﬁnding an approximate solution by solving a sequence of
tractable problems.

Dual Decomposition

A general approach is to decompose a diﬃcult optimisation problem into a set of easier problems. In this
approach we ﬁrst identify tractable ‘slave’ objectives Es(x), s = 1, . . . , S such that the ‘master’ objective
E(x) decomposes as

E(x) =

(cid:88)

s

Es(x)

(28.9.3)

Then the x that optimises the master problem is equivalent to optimising each slave problem Es(xs) under
can be imposed by a Lagrangian
the constraint that the slaves agree xs = x, s = 1, . . . , S [34]. This constraint

@@

DRAFT April 24, 2015

597

MAP for Markov networks

b

c

f

(a)

a

e

d

a

e

b

c

f

(b)

d

T

S

(blue) and

(a): A directed graph with edge
Figure 28.13:
weights wij from node i to j with wij = 0 if no edge ex-
ists from i to j. (b): A graph cut partitions the nodes
into two groups
(red). The weight of
the cut is the sum of the weights of edges that leave
S
(red). Intuitively, it is clear that
(blue) and land in
after assigning nodes to state 1 (for blue) and 0 (red)
that the weight of the cut corresponds to the summed
weights of neighbours in diﬀerent states. Here we high-
light those weight contributions. The non-highlighted
edges do not contribute to the cut weight. Note that
only one of the edge directions contributes to the cut.

T

(x,

L

xs
{

}

, λ) =

(cid:88)

s

Es(xs) +

(cid:88)

s

λs (xs

x)

−

(28.9.4)

Finding the stationary point w.r.t. x, gives the constraint (cid:80)

s λs = 0, so that we may then consider

(
L

xs
{

}

, λ) =

(cid:88)

s

Es(xs) + λsxs

Given λ, we then optimise each slave problem

x∗
s = argmax

xs

(Es(xs) + λsxs)

The Lagrange dual, section(A.6.1), is given by (noting here we have a maximisation problem)

s(λs) = max

(Es(xs) + λsxs)

L

xs

In this case the dual bound on the primal is

(cid:88)
s L

s(λs)

E(x∗)

≥

where x∗ is the solution of the primal problem x∗ = argmax

x

subgradient’ method to minimise each

s(λs)

L

λ(cid:48)
s = λ

αx∗
s

−

where α is a chosen positive constant. Then we project,

¯λ =

1
S

(cid:88)

λ(cid:48)
s,

s

s = λ(cid:48)
λnew

s −

¯λ

which ensures that (cid:80)

s λnew

s = 0.

28.9.1 Pairwise Markov networks

Consider a pairwise Markov network p(x)

eE(x) with

E(x)

(cid:88)

i∼j

≡

fij (xi, xj) +

(cid:88)

i

∝
gi(xi, x0
i )

E(x). To update λ one may use a ‘projected

(28.9.9)

(28.9.10)

(28.9.11)

j denotes the set of neighbouring variables and f (xi, xj) = f (xj, xi). This means that an
where i
∼
j in the graph corresponding to the Markov network contributes a term f (xi, xj),
undirected edge i
not 2f (xi, xj). Here the terms f (xi, xj) represent pairwise interactions. The terms g(xi, x0
i ) represent
unary interactions, written for convenience in terms of a pairwise interaction with a ﬁxed (non-variable) x0.
Typically the term f (xi, xj) is used to ensure that neighbouring variables xi and xj are in similar states;
the term gi(xi, x0

i ) is used to bias xi to be close to a desired state x0
i .

−

598

DRAFT April 24, 2015

(28.9.5)

(28.9.6)

(28.9.7)

(28.9.8)

MAP for Markov networks

Iterated conditional modes

Such models have application in image restoration in which an observed noisy image x0 is to be cleaned, see
example(28.1) and ﬁg(28.3). To do so we seek a clean image x for which each clean pixel value xi is close to
the observed noisy pixel value x0
i , whilst being in a similar state to its clean neighbours. In example(28.1)
we used ICM to ﬁnd the approximate most likely state by simply updating randomly chosen variables, one
at a time.

Dual decomposition

For a binary Markov network

E(x) =

1
2

(cid:88)

ij

xixjwij +

(cid:88)

i

cixi =

1
2

xTWx + xTc

we may deﬁne tractable slave problems

Es(x) =

1
2

xTWsx + xTcs

(28.9.12)

(28.9.13)

by identifying a set of tree structured matrices Ws such that W = (cid:80)
s Ws and unary terms cs such that
c = (cid:80)
s cs. The dual decomposition technique then proceeds to solve each slave tree exactly, and subse-
quently updates λ to push the tree solutions towards agreement [176]. Similar general methods of solving
trees under the constraint of agreement are discussed in [307].

28.9.2 Attractive binary Markov networks

Whilst, in general, no eﬃcient exact solution exists for the general MAP Markov network problem, an
important tractable special case is discussed below. Consider ﬁnding the MAP of a Markov network with
binary variables dom(xi) =
0. In this case our task is to ﬁnd
0, 1
}
the assignment x that maximises

and positive connections wij = wji

≥

{

E(x)

(cid:88)

i∼j

≡

wijI [xi = xj] +

(cid:88)

cixi

i

(28.9.14)

∼

j denotes neighbouring variables.

where i
For this particular case an eﬃcient exact MAP algorithm
exists for arbitrary topology of the interactions wij[133]. The algorithm ﬁrst translates the MAP assignment
problem into an equivalent min s-t-cut problem[40], for which eﬃcient algorithms exist. In min s-t-cut, we
need a graph with positive weights on the edges. This is clearly satisﬁed if wij > 0, although the bias term
(cid:80)

i cixi needs to be addressed.

Dealing with the bias terms

To translate the MAP assignment problem to a min-cut problem we need to deal with the additional term
(cid:80)
i cixi. First consider the eﬀect of including a new node x∗ and connecting this to each existing node i
0, 1
}

with weight ci. Using that for binary variables xi

∈ {

,

I [xi = xj] = xixj + (1

xi)(1

−

−

xj) = 2xixj

xi

−

−

xj + 1

this then adds a term

ciI [xi = x∗] =

(cid:88)

i

(cid:88)

i

ci (xix∗ + (1

xi) (1

x∗))

−

−

If we set x∗ in state 1, this contributes

(cid:88)

cixi

i

DRAFT April 24, 2015

(28.9.15)

(28.9.16)

(28.9.17)

599

MAP for Markov networks

(a): A Graph with bidirectional
Figure 28.14:
weights wij = wji augmented with a source node s
and sink node t. Each node has a corresponding bias
whose sign is indicated. The source node is linked
to the nodes corresponding to positive bias, and the
(b): A graph
nodes with negative bias to the sink.
(blue) and
cut partitions the nodes into two groups
is the union of the source node s
T
is the union of the sink node
and nodes in state 1,
t and nodes in state 0. The weight of the cut is the
sum of the edge weights from
(red). The
red lines indicate contributions to the cut, and can be
considered penalties since we wish to ﬁnd the minimal
cut.

(red), where

(blue) to

S

S

S

T

T

s

s

a+

e

−

a

e

b
−

c+

b

c

f +

(a)

f

(b)

d

−

d

t

t

Otherwise, if we set x∗ in state 0 we obtain

(cid:88)

i

ci (1

−

xi) =

(cid:88)

−

i

cixi + const.

(28.9.18)

Our requirement that the weights need to be positive can therefore be achieved by deﬁning not a single
additional node x∗, but rather two. We deﬁne a source node xs, set to state 1 and connect it to those xi
which have positive ci, deﬁning wsi = wis = ci. In addition we deﬁne a sink node xt set to state 0 and
connect all nodes with negative ci, to xt, using weight wit = wti =
ci, (which is therefore positive). For
the source node clamped to xs = 1 and the sink node to xt = 0, then including the source and sink, we have

−

E(x) =

(cid:88)

i∼j

wijI [xi = xj] + const.

(28.9.19)

which is equal to the energy function, equation (28.9.14), with positive weights.

Deﬁnition 28.1 (Graph Cut). For a graph G with nodes v1, . . . , vD, and weights wij > 0 a cut is a partition
of the nodes into two disjoint groups, called
. The weight of a cut is deﬁned as the sum of the
weights that leave

, see ﬁg(28.13).

and land in

and

S

T

S

T

For symmetric w, the weight of a cut corresponds to the sum of weights between mismatched neighbours,
see ﬁg(28.13b). That is,

cut(x) =

(cid:88)

i∼j

wijI [xi

= xj]

Since I [xi

= xj] = 1

I [xi = xj], we can deﬁne the weight of the cut equivalently as

−

cut(x) =

(cid:88)

i∼j

wij (1

−

I [xi = xj]) =

(cid:88)

−

i∼j

wijI [xi = xj] + const. =

E(x) + const.

−

(28.9.20)

(28.9.21)

so that the minimal cut assignment will correspond to maximising E(x). In the Markov network case, our
translation into a weighted graph with positive interactions then requires that we identify the source and

600

DRAFT April 24, 2015

(cid:54)
(cid:54)
MAP for Markov networks

(a): Noisy
Figure 28.15:
greyscale image deﬁned using
244 intensity labels
for each
(b): Restored im-
pixel.
age. The α-expansion method
was used, with suitable interac-
tions w and bias c to ensure rea-
sonable results. From [49].

(a)

(b)

, and the sink and all variables in state 0 with

, see ﬁg(28.14).
all other variables assigned to state 1 with
Our task is then to ﬁnd the minimal cut from
. A fundamental result in discrete mathematics is that
the min s-t-cut solution corresponds to the max-ﬂow solution from the source s to the sink t [40]. There are
eﬃcient algorithms for max-ﬂow, see for example [49], which take O (cid:0)D3(cid:1) operations or less, for a graph with
D nodes. This means that one can ﬁnd the exact MAP assignment of an attractive binary Markov network
eﬃciently in O (cid:0)D3(cid:1) operations.
In MaxFlow.m we implement the Ford-Fulkerson (Edmonds-Karp-Dinic
breadth ﬁrst search variant)[93].

to

S

S

T

T

28.9.3 Potts model

A Markov network deﬁned on variables with more than two states, xi
model :

∈ {

0, 1, 2, . . . , S

}

is called a Potts

E(x) =

(cid:88)

i∼j

wijI [xi = xj] +

(cid:88)

ciI (cid:2)xi = x0

i

(cid:3)

i

(28.9.22)

where we assume wij > 0 and the x0
i are known. This model has immediate application in non-binary image
restoration, and also in clustering based on a similarity score. This problem cannot be translated directly
into a graph-cut problem and no eﬃcient exact algorithm is known. A useful approach is to approximate
the problem as a sequence of binary problems, as we describe below.

Potts to binary Markov network translation

Consider the α-expansion representation

xi = siα + (1

si)xold
i

−

(28.9.23)

where si
or α, depending on
the binary variable si. Using a new binary vector variable s we can therefore restrict x to a subpart of the
full space and write a new objective function in terms of s alone (see below):

. This restricts xi to be either the state xold

0, 1, 2, . . . , S

0, 1
}

and α

∈ {

∈ {

}

i

E(s) =

(cid:88)

i∼j

w(cid:48)
ij

I [si = sj] +

c(cid:48)
isi + const.

(cid:88)

i

(28.9.24)

for w(cid:48)
ij > 0. This new problem is of the form of an attractive binary Markov network which can be solved
exactly using the graph cuts procedure. This translation imposes a constraint since the x cannot access all
the space, but enables us to solve the constrained problem eﬃciently. We then choose another α value (at
random) and ﬁnd the optimal s for the new α. In this way we are guaranteed to iteratively increase E.

DRAFT April 24, 2015

601

For a given α and xold, the transformation of the Potts model objective is given by using si
considering

0, 1

∈ {

}

and

Further Reading

I [xi = xj] = I

(cid:104)

siα + (1

si)xold
i = sjα + (1
(cid:104)

(cid:105)

−
sj)I

−
+ (1

= (1

si)(1

i = xold
xold
= sisjuij + aisi + bjsj + const.

−

−

j

(cid:105)

sj)xold
j

with

uij

1

−

≡

I

(cid:104)
xold
i = α

(cid:105)

−

(cid:104)

I

xold
j = α

(cid:105)

(cid:104)

+ I

i = xold
xold

j

(cid:105)

(cid:104)

si)sjI

xold
i = α

(cid:105)

+ si(1

(cid:104)

sj)I

xold
j = α

(cid:105)

+ sisj

−

−

(28.9.25)

(28.9.26)

with ai and bi deﬁned in the obvious manner. By enumeration it is straightforward to show that uij is either
0, 1
0, 1 or 2. Using the mathematical identity, for si
}

∈ {

,

sisj =

1
2

(I [si = sj] + si + sj

1)

−

we can write,

I [xi = xj] =

uij
2

(I [si = sj] + si + sj) + aisi + bjsj + const.

(28.9.27)

(28.9.28)

Hence terms wijI [xi = xj] translate to positive interaction terms I [si = sj] wijuij/2. All the unary terms
are easily exactly mapped into corresponding unary terms c(cid:48)
i deﬁned as the sum of all unary terms
in si. This shows that the positive interaction wij in terms of the original variables x maps to a positive
interaction in the new variables s. Hence we can ﬁnd the maximal state of s using a graph cut algorithm.
A related procedure is described in [50].

isi for c(cid:48)

Example 28.5 (Potts model for image reconstruction). An example image restoration problem for nearest
neighbour interactions on a pixel lattice and suitably chosen w, c is given in ﬁg(28.15). The images are
non-binary so that the optimal MAP assignment cannot be computed exactly in an eﬃcient way. The
alpha-expansion technique was used here combined with an eﬃcient min-cut approach to approximate the
MAP assignment, see [49] for details.

28.10 Further Reading

Approximate inference is a highly active research area and increasingly links to convex optimisation[48] are
being developed. See [307] for a general overview.

28.11 Summary

•

•

•

•

•

602

Deterministic methods oﬀer an alternative to sampling techniques.

For continuous distributions, perturbation approaches such as Laplace’s method provide a simple approxi-
mation.

Variational bounding approaches, such as minimal KL divergence, can provide bounds on quantities of
interest, for example the normalisation constant of a distribution and the marginal likelihood. These are
part of a larger class of methods derived from convex analysis.

The deterministic bounding approaches can be applied to other areas such as bounding the mutual infor-
mation.

Consistency methods such as loopy belief propagation can work extremely well when the structure of the
distribution is close to a tree. These methods have been very successful in information theory and error
correction.

DRAFT April 24, 2015

Exercises

•

•

Loopy belief propagation can also be motivated via the optimisation of the Bethe free energy objective
function, although the method does not produce a bound on quantities of interest.

For binary attractive Markov networks we can ﬁnd the MAP state exactly in polynomial time. This method
can be used as a subroutine in more complex multistate problems in which the MAP search problem is
broken into a sequence of binary MAP problems.

28.12 Code

LoopyBP.m: Loopy belief propagation (Factor Graph formalism)
demoLoopyBP.m: Demo of loopy belief propagation
demoMFBPGibbs.m: Comparison of Mean Field, belief propagation and Gibbs sampling
demoMRFclean.m: Demo of analysing a dirty picture
MaxFlow.m: Max-Flow Min-Cut algorithm (Ford-Fulkerson)
binaryMRFmap.m: Optimising a binary Markov network

28.13 Exercises

Exercise 28.1. The ﬁle p.mat contains a distribution p(x, y, z) on ternary state variables. Using BRML-
p) and
toolbox, ﬁnd the best approximation q(x, y)q(z) that minimises the Kullback-Leibler divergence KL(q
|
state the value of the minimal Kullback-Leibler divergence for the optimal q.

Exercise 28.2. Consider the pairwise Markov network deﬁned on a 2
Using BRMLtoolbox,

×

2 lattice, as given in pMRF.mat.

1. Find the optimal fully factorised approximation (cid:81)4

i=1 qBP
i

factor graph formalism.

2. Find the optimal fully factorised approximation (cid:81)4

i=1 qM F
i

tions.

by loopy belief propagation, based on the

by solving the variational mean ﬁeld equa-

3. By pure enumeration, compute the exact marginals pi.

4. Averaged over all 4 variables, compute the mean expected deviation in the marginals

1
4

4
(cid:88)

i=1

1
2

2
(cid:88)

j=1

qi(x = j)
|

−

pi(x = j)

|

for both the BP and MF approximations, and comment on your results.

Exercise 28.3. In LoopyBP.m the message schedule is chosen at random. Modify the routine to choose a
schedule using a forward-reverse elimination sequence on a random spanning tree.

Exercise 28.4 (Double Integration Bounds). Consider a bound

f (x)

g(x)

≥

Then for

˜f (x)

(cid:90) x

≡

a

f (x)dx,

˜g(x)

(cid:90) x

≡

a

g(x)dx

show that:

DRAFT April 24, 2015

(28.13.1)

(28.13.2)

603

1. ˜f (x)

2. ˆf (x)

˜g(x),

≥

ˆg(x)

≥
ˆf (x)

for x

a

≥
for all x, where

(cid:90) x

≡

a

˜f (x)dx

ˆg(x)

(cid:90) x

≡

a

˜g(x)dx

Exercises

(28.13.3)

The signiﬁcance is that this double integration (or summation in the case of discrete variables) is a general
procedure for generating a new bound from an existing bound [186].

Exercise 28.5. This question concerns deriving both the standard mean ﬁeld bound and more powerful lower
bounds for the Boltzmann machine.

1. Starting from

ex

0

≥

and using the double integration procedure, show that

ex

≥

ea(1 + x

a)

−

(28.13.4)

2. By replacing x

sTWs for s

a Boltzmann machine

→

Z =

(cid:88)

esTWs

s

0, 1

∈ {

}

D, and a

→

hTs+θ derive a bound on the partition function of

(28.13.5)

3. Show that this bound is equivalent to a naive mean ﬁeld bound on the partition function. Hint: optimise

with respect to θ ﬁrst.

4. Discuss how one can generate tighter bounds on the partition function of a Boltzmann distribution by

further application of the double integration procedure.

Exercise 28.6. Consider a pairwise Markov network

p(x) =

1
Z

exTWx+bTx

for symmetric W. Consider the decomposition

W =

(cid:88)

i

qiWi,

i = 1, . . . , I

(28.13.6)

(28.13.7)

qi

i qi = 1, and the graph corresponding to each matrix Wi is a tree. Explain how to
where 0
form an upper bound on the normalisation Z and discuss a naive method to ﬁnd the tightest upper bound.
Hint: Consider

e(cid:104)x(cid:105). See also [307].

ex

≤

≤

1 and (cid:80)

(cid:104)

(cid:105) ≥

Exercise 28.7. Derive Linkser’s bound on the Mutual Information, equation (28.6.13).

Exercise 28.8. Consider the average of a positive function f (x) with respect to a distribution p(x)

(cid:90)

J = log

p(x)f (x)

x

where f (x)
(cid:90)

≥

0. The simplest version of Jensen’s inequality states that

J

≥

x

p(x) log f (x)

(28.13.8)

(28.13.9)

1. By considering a distribution r(x)

show that

p(x)f (x), and KL(q

|

∝

r), for some variational distribution q(x),

J

KL(q(x)

p(x)) +
|

log f (x)

(cid:105)q(x)

≥ −

(cid:104)
The bound saturates when q(x)
p(x)f (x). This shows that if we wish to approximate the average J,
the optimal choice for the approximating distribution q(x) depends on both the distribution p(x) and
integrand f (x).

∝

(28.13.10)

604

DRAFT April 24, 2015

Exercises

2. Furthermore, show that

J

KL(q(x)
|

≥ −

p(x))

KL(q(x)
|

−

f (x))

−

H(q(x))

(28.13.11)

where H(q(x)) is the entropy of q(x). The ﬁrst term encourages q to be close to p. The second
encourages q to be close to f , and the third encourages q to be sharply peaked.

Exercise 28.9. For a Markov network deﬁned over D binary variables xi

p(x) =

exTWx

1
Z

show that

p(xi) =

Z\i
Z

where

Z\i ≡

(cid:88)

exTWx

x1,...,xi−1,xi+1,...,xD

0, 1

∈ {

, i = 1, . . . , D, we deﬁne
}

(28.13.12)

(28.13.13)

(28.13.14)

and explain why a bound on the marginal p(xi) requires both upper and lower bounds on partition functions
Z.

Exercise 28.10. Consider the model in example(28.2). Implement a structured EP approximation based
on replacing the observation factors p(vt

1
2 hT
0. The ﬂow
Exercise 28.11. Consider a directed graph such that the capacity of an edge x
on an edge f (x, y)
0 must not exceed the capacity of the edge. The aim is to maximise the ﬂow from a
deﬁned source node s to a deﬁned sink node t. In addition ﬂow must be conserved such that for any node
other than the source or sink (y

ht) by terms of the form ct exp (cid:0)

t Atht + hT

y is c(x, y)

= s, y

= t),

t bt

(cid:1).

→

≥

≥

−

|

(cid:88)

x

f (x, y) =

(cid:88)

x

f (y, x)

A cut is deﬁned as a partition of the nodes into two non-overlapping sets
t in

. Show that:

T

(28.13.15)

and

T

S

such that s is in

and

S

1. The net ﬂow from s to t, val(f ) is the same as the net ﬂow from

val(f ) =

(cid:88)

f (x, y)

x∈S,y∈T

(cid:88)

−

y∈T ,x∈S

f (y, x)

to

:

T

S

(28.13.16)

2. val(f )

≤

(cid:80)

x∈S,y∈T f (x, y) namely that the ﬂow is upper bounded by the capacity of the cut.

The max-ﬂow-min-cut theorem further states that the maximal ﬂow is actually equal to the capacity of the
cut.

Exercise 28.12 (Potts to Ising translation). Consider the function E(x) deﬁned on a set of multistate
variables dom(xi) =

0, 1, 2, . . . , S

wijI [xi = xj] +

ciI (cid:2)xi = x0

i

(cid:3)

(28.13.17)

{

E(x) =

(cid:88)

i∼j

,
}
(cid:88)

i

where wij > 0 and both the pixel states x0
maximisation of E(x). Using the restricted parameterisation

i and ci are known. Our interest is to ﬁnd an approximate

xi = siα + (1

si)xold
i

−

where si
and for a given α
0, 1, 2, . . . , N
maximising ˜E(s) over the binary variables s, where

0, 1
}

∈ {

∈ {

˜E(s) =

(cid:88)

i∼j

w(cid:48)
ij

I [si = sj] +

c(cid:48)
isi + const.

(cid:88)

i

, show how the maximisation of E(x) is equivalent to

}

(28.13.18)

(28.13.19)

ij > 0. This new problem is of the form of an attractive binary Markov network which can be solved

for w(cid:48)
exactly using the graph cuts procedure.

DRAFT April 24, 2015

605

(cid:54)
(cid:54)
Exercise 28.13. Consider an approximating distribution in the exponential family,

q(x) =

1
Z(φ)

eφTg(x)

We wish to use q(x) to approximate a distribution p(x) using the KL divergence KL(p

q).
|

1. Show that optimally

g(x)

(cid:105)p(x) =

g(x)
(cid:104)

(cid:105)q(x)

(cid:104)

2. Show that a Gaussian can be written in the exponential form

(cid:0)x µ, σ2(cid:1) =

N

1
Z(φ)

eφTg(x)

where g1(x) = x, g2(x) = x2 for suitably chosen φ.

Exercises

(28.13.20)

(28.13.21)

(28.13.22)

3. Hence show that the optimal Gaussian ﬁt

sense, matches the moments:

N

(cid:0)x µ, σ2(cid:1) to any distribution, in the minimal KL(p

q)
|

µ =

(cid:105)p(x) ,
x

(cid:104)

σ2 = (cid:10)x2(cid:11)

p(x) − (cid:104)

x

2
p(x)
(cid:105)

Exercise 28.14. For a pairwise binary Markov network, p, with partition function

Z(w, b) =

(cid:88)

(cid:80)
e

x

i,j wij xixj +(cid:80)

i bixi

show that the mean

xi
(cid:104)

(cid:105)p can be computed using a generating function approach

d
dbi

log Z(w, b) =

1
Z(w, b)

(cid:88)

xie

x

(cid:80)

i∼j wij xixj +(cid:80)

i bixi =

xi

(cid:104)

(cid:105)p

and that similarly the covariance is given by

xixj
(cid:104)

(cid:105)p − (cid:104)

xi

xj

(cid:105)p (cid:104)

(cid:105)p =

d2
dbidbj

log Z(w, b)

(28.13.23)

(28.13.24)

(28.13.25)

(28.13.26)

(α) in equation (28.4.13), show that the approximate
By replacing log Z(w, b) by the mean ﬁeld bound
mean and variance given by the naive mean ﬁeld theory is equivalent to those obtained on replacing log Z
by the lower bound in the above generating function.

B

Exercise 28.15. The naive mean ﬁeld theory applied to a pairwise Markov network

(cid:80)
e

i,j wij xixj +(cid:80)

i bixi

p(x)

∝

dom(xi) =
{
this we can approximate

0, 1
}

, gives a factorised approximation q(x) = (cid:81)

i q(xi), based on minimising KL(q

(28.13.27)

p). Using

|

xi

xj

(cid:105)q (cid:104)

(cid:105)p ≈ (cid:104)

xixj
(cid:104)

(cid:105)q ,
To produce a better, non-factorised approximation to
(cid:105)p we could ﬁt a non-factorised q. The linear-
response method[166] may also be used, based on a perturbation expansion of the free energy. Alternatively,
show that

(28.13.28)

xixj

= j

(cid:104)

i

xixj
(cid:104)

(cid:105)p = p(xi = 1, xj = 1) = p(xi = 1
|

xj = 1)p(xj = 1)

(28.13.29)

Explain how this can be used to form an improved non-factorised approximation to

xixj
(cid:104)

(cid:105)p.

Exercise 28.16. Derive the EP updates equation (28.8.8) and equation (28.8.9).

606

DRAFT April 24, 2015

(cid:54)
Exercises

Exercise 28.17. Consider ﬁtting a univariate Gaussian q(x) =
imising KL(q

p) with respect to µ, σ2.
|

N

(cid:0)x µ, σ2(cid:1) to a distribution p(x) by min-

1. Show that, provided all quantities are well deﬁned, the optimal variance and mean of the approximating

Gaussian satisfy the implicit equations

σ2 =

−

1

(cid:68) d2

(cid:69)
dx2 log p(x)

(cid:28) d
dx

(cid:29)

log p(x)

= 0

q(x)

,

q(x)

2. Hence relate the optimal variance to the maximal and minimal ‘local curvature’ of p(x):

−

maxx

1
d2
dx2 log p(x) ≤

σ2

≤ −

minx

1
d2
dx2 log p(x)

(28.13.30)

(28.13.31)

3. Consider a distribution p(x) that is a mixture of well-separated Gaussians, p(x) = (cid:80)

(cid:1).
Show that, to a good approximation, the optimal choice is to set the mean of the approximating distri-
bution to one of the components, µ = µi. Show furthermore that

(cid:0)x µi, σ2

i pi

N

i

σ2
min

(cid:46) σ2 (cid:46) σ2

max

(28.13.32)

where σ2
explain therefore why the optimal variance σ2 will be smaller than the variance of p(x).

max are the minimal and maximal variances of the Gaussian components of p(x), and

min, σ2

DRAFT April 24, 2015

607

Exercises

608

DRAFT April 24, 2015

Part VI

Appendix

609

APPENDIX A

Background Mathematics

A.1 Linear Algebra

A.1.1 Vector algebra

Let x denote the n-dimensional column vector with components















x1
x2
...
xn

The vector with all elements equal to 1 is written 1, and similarly the vector with all zero values is written
0.

Deﬁnition A.1 (scalar product). The scalar product w

x is deﬁned as:

·

x =

w

·

n
(cid:88)

i=1

wixi = wTx

The length of a vector is denoted

, the squared length is given by
|

x
|

2 = xTx = x2 = x2
|

1 + x2

+ x2
n

· · ·

x
|
2 +

A unit vector x has

x

|

|

= 1. The scalar product has a natural geometric interpretation as:

x =

w

·

w
|

x

| |

|

cos(θ)

(A.1.1)

(A.1.2)

(A.1.3)

where θ is the angle between the two vectors. Thus if the lengths of two vectors are ﬁxed their inner product
is largest when θ = 0, whereupon one vector is a constant multiple of the other.
If the scalar product
xTy = 0, then x and y are orthogonal (they are at right angles to each other). A set of vectors orthonormal
if they are mutually orthogonal and have unit length.

Deﬁnition A.2 (Linear dependence). A set of vectors x1, . . . , xn is linearly dependent if there exists a
vector xj that can be expressed as a linear combination of the other vectors. If the only solution to

n
(cid:88)

i=1

αixi = 0

is for all αi = 0, i = 1, . . . , n, the vectors x1, . . . , xn are linearly independent.

611

(A.1.4)

Linear Algebra

Figure A.1: Resolving a vector a into components along the orthogonal
directions e and e∗. The projection of a onto these two directions are
lengths α and β along the directions e and e∗.

A.1.2 The scalar product as a projection

Suppose that we wish to resolve the vector a into its components along the orthogonal directions speciﬁed
by the unit vectors e and e∗, see ﬁg(A.1). That is
e∗ = 0. We are required to ﬁnd the
=
scalar values α and β such that

∗ = 1 and e

e
|
|

e
|

|

·

a = αe + βe∗

From this we obtain

e = αe

a

·

·

e + βe∗

e,

·

e∗ = αe

a

·

·

e∗ + βe∗

e∗

·

From the orthogonality and unit lengths of the vectors e and e∗, this becomes simply

e = α,

a

·

e∗ = β

a

·

This means that we can write the vector a in terms of the orthonormal components e and e∗ as

a = (a

·

e) e + (a

e∗) e∗

·

(A.1.5)

(A.1.6)

(A.1.7)

(A.1.8)

The scalar product between a and e projects the vector a onto the (unit) direction e. The projection of a
vector a onto a direction speciﬁed by general f is a·f
|f |2 f .

A.1.3 Lines in space

A line in 2 (or more) dimensions can be speciﬁed as follows. The vector of any point along the line is given,
for some s, by the equation

p = a + su,

s

.
∈ R

(A.1.9)

where u is parallel to the line, and the line passes through the point a, see ﬁg(A.2). An alternative
speciﬁcation can be given by realising that all vectors along the line are orthogonal to the normal of the
line, n (u and n are orthonormal). That is

(p

a)

·

−

n = 0

p

·

⇔

n = a

n

·

(A.1.10)

If the vector n is of unit length, the right hand side of the above represents the shortest distance from the
origin to the line, drawn by the dashed line in ﬁg(A.2) (since this is the projection of a onto the normal
direction).

A.1.4 Planes and hyperplanes

To deﬁne a two-dimensional plane (in arbitrary dimensional space) one may specify two vectors u and v that
lie in the plane (they need not be mutually orthogonal), and a position vector a in the plane, see ﬁg(A.3).
Any vector p in the plane can then be written as

p = a + su + tv,

(s, t)

.
∈ R

(A.1.11)

Figure A.2: A line can be speciﬁed by some position vector on
the line, a, and a unit vector along the direction of the line, u.
In 2 dimensions, there is a unique direction, n, perpendicular to
the line. In three dimensions, the vectors perpendicular to the
direction of the line lie in a plane, whose normal vector is in the
direction of the line, u.

612

DRAFT April 24, 2015

e*eβαaee*aapnuLinear Algebra

Figure A.3: A plane can be speciﬁed by a point in the
plane, a and two, non-parallel directions in the plane,
u and v. The normal to the plane is unique, and in
the same direction as the directed line from the origin
to the nearest point on the plane.

An alternative deﬁnition is given by considering that any vector within the plane must be orthogonal to the
normal of the plane n.

(p

a)

·

−

n = 0

p

·

⇔

n = a

n

·

(A.1.12)

The right hand side of the above represents the shortest distance from the origin to the plane, drawn by the
dashed line in ﬁg(A.3). The advantage of this representation is that it has the same form as a line. Indeed,
this representation of (hyper)planes is independent of the dimension of the space. In addition, only two
quantities need to be deﬁned – the normal to the plane and the distance from the origin to the plane.

A.1.5 Matrices

×

n matrix A is a collection of scalar values arranged in a rectangle of m rows and n columns. A
1 matrix. The i, j element of matrix A can be written Aij or more

An m
vector can be considered as an n
×
conventionally aij. Where more clarity is required, one may write [A]ij.

Deﬁnition A.3 (Matrix addition). For two matrices A and B of the same size,

[A + B]ij = [A]ij + [B]ij

(A.1.13)

Deﬁnition A.4 (Matrix multiplication). For an l by n matrix A and an n by m matrix B, the product
AB is the l by m matrix with elements

[AB]ik =

n
(cid:88)

j=1

[A]ij [B]jk ;

i = 1, . . . , l k = 1, . . . , m .

(A.1.14)

Note that in general BA
= AB. When BA = AB we say that they A and B commute. The matrix I is
the identity matrix , necessarily square, with 1’s on the diagonal and 0’s everywhere else. For clarity we may
n matrix A, ImA = AIn = A. The
also write Im for a square m
×
identity matrix has elements [I]ij = δij given by the Kronecker delta:

m identity matrix. Then for an m

×

(cid:26) 1 i = j
= j
0 i

δij

≡

(A.1.15)

Deﬁnition A.5 (Transpose). The transpose BT of the n by m matrix B is the m by n matrix with
components
BT(cid:105)

k = 1, . . . , m j = 1, . . . , n .

(A.1.16)

= Bjk ;

(cid:104)

kj

= B and (AB)T = BTAT. If the shapes of the matrices A,B and C are such that it makes sense to

(cid:0)BT(cid:1)T
calculate the product ABC, then

(ABC)T = CTBTAT

DRAFT April 24, 2015

(A.1.17)

613

anuvp(cid:54)
(cid:54)
A square matrix A is symmetric if AT = A. A square matrix is called Hermitian if A = AT∗ where ∗
denotes the complex conjugate operator. For Hermitian matrices, the eigenvectors form an orthogonal set
with real eigenvalues.

Linear Algebra

Deﬁnition A.6 (Trace).

trace (A) =

(cid:88)

Aii =

(cid:88)

λi

i

i

where λi are the eigenvalues of A.

A.1.6 Linear transformations

(A.1.18)

If we deﬁne ui to be the vector with zeros everywhere expect for the ith entry, then a vector can be expressed
as x = (cid:80)

i xiui. Then a linear transformation of x is given by

Ax =

(cid:88)

i

xiAui =

(cid:88)

xiai

i

where ai is the ith column of A.

Rotations

The unit vectors (1, 0)T and (0, 1)T under rotation by θ radians transform to the vectors

(cid:18) cos θ
sin θ

(cid:19)

,

(cid:19)

(cid:18)

sin θ
−
cos θ

which thus form the columns of the rotation matrix:

R =

(cid:18) cos θ
sin θ

sin θ
−
cos θ

(cid:19)

Multiplying a vector by R, Rx, rotates the vector through θ radians.

A.1.7 Determinants

(A.1.19)

(A.1.20)

(A.1.21)

Deﬁnition A.7 (Determinant). For a square matrix A, the determinant is the volume of the transformation
of the matrix A (up to a sign change). That is, we take a hypercube of unit volume and map each vertex
under the transformation. The volume of the resulting object is deﬁned as the determinant. Writing
[A]ij = aij,

det

(cid:19)

(cid:18)a11 a12
a21 a22

= a11a22

a21a12

−

(A.1.22)



det



a11 a12 a13
a21 a22 a23
a31 a32 a33



 = a11 (a22a33

a23a32)

−

−

a12 (a21a33

−

a31a23) + a13 (a21a32

a31a22)

−

(A.1.23)

The determinant in the (3

3) case has the form

×

a11det

(cid:19)

(cid:18)a22 a23
a32 a33

−

a12det

(cid:19)

(cid:18)a21 a23
a31 a33

+ a13det

(cid:19)

(cid:18)a21 a22
a31 a32

(A.1.24)

The determinant of the (3
the (2

1)i+1a1idet (Ai) where Ai is
2) matrix formed from A by removing the ith row and column. This form of the determinant

3) matrix A is given by the sum of terms (

×

−

×

614

DRAFT April 24, 2015

Linear Algebra

generalises to any dimension. That is, we can deﬁne the determinant recursively as an expansion along the
top row of determinants of reduced matrices. The absolute value of the determinant is the volume of the
transformation.
AT(cid:17)

= det (A)

(A.1.25)

det

(cid:16)

For square matrices A and B of equal dimensions,

det (AB) = det (A) det (B) ,

det (I) = 1

det (cid:0)A−1(cid:1) = 1/det (A)

⇒

(A.1.26)

Deﬁnition A.8 (Orthogonal matrix). A square matrix A is orthogonal if AAT = I = ATA. From the
properties of the determinant, we see therefore that an orthogonal matrix has determinant
1 and hence
corresponds to a volume preserving transformation.

±

n matrix X the rank of X is the maximum number of linearly
Deﬁnition A.9 (Matrix rank). For an m
independent columns (or equivalently rows). The matrix is full rank if it has rank equal to min(m, n) –
otherwise the matrix is rank deﬁcient. A square matrix that is rank deﬁcient is singular.

×

A.1.8 Matrix inversion

Deﬁnition A.10 (Matrix inversion). For a square matrix A, its inverse satisﬁes

A−1A = I = AA−1

(A.1.27)

It is not always possible to ﬁnd a matrix A−1 such that A−1A = I, in which case A is singular . Geo-
metrically, singular matrices correspond to projections: if we transform each of the vertices v of a binary
hypercube using Av, the volume of the transformed hypercube is zero. Hence if det (A) = 0, the matrix A is
a form of projection or ‘collapse’ which means A is singular. Given a vector y and a singular transformation,
A, one cannot uniquely identify a vector x for which y = Ax. Provided the inverses exist

(AB)−1 = B−1A−1

For a non-square matrix A such that AAT is invertible, then the right pseudo inverse, deﬁned as

A† = AT (cid:16)

AAT(cid:17)−1

satisﬁes AA† = I. The left pseudo inverse is given by

A† =

(cid:16)

ATA

(cid:17)−1

AT

and satisﬁes A†A = I.

(A.1.28)

(A.1.29)

(A.1.30)

Deﬁnition A.11 (Matrix inversion lemma (Woodbury formula)). Provided the appropriate inverses exist:

(cid:16)

A + UVT(cid:17)−1

= A−1

−

(cid:16)

A−1U

I + VTA−1U

(cid:17)−1

VTA−1

(cid:16)

A + UVT(cid:17)

det

= det (A) det

(cid:16)

I + VTA−1U

(cid:17)

DRAFT April 24, 2015

(A.1.31)

(A.1.32)

615

Deﬁnition A.12 (Block matrix inversion). For matrices A, B, C, D, provided the appropriate inverses
exist:

Linear Algebra

(A.1.33)

(A.1.34)

(cid:20) A B
C D

(cid:21)−1

(cid:34)

=

BD−1C(cid:1)−1

(cid:0)A
D−1C (cid:0)A

−

−

−

−
BD−1C(cid:1)−1 D−1C (cid:0)A

−

(cid:0)A

BD−1C(cid:1)−1 BD−1

(cid:35)

BD−1C(cid:1)−1 BD−1

−

A.1.9 Computing the matrix inverse

For a 2

×

2 matrix, A =

(cid:19)

(cid:18)a b
c d

, the inverse matrix has elements

1

ad

(cid:19)
b
−
a

(cid:18) d
c
−
The quantity ad
matrix, and we refer the reader to more specialised texts, such as [279, 129].

= A−1

bc

−

−

bc is the determinant of A. There are many ways to compute the inverse of a general

If one wants to solve only a linear system, Ax = b, algebraically, the solution is given by x = A−1b. This
n matrix A−1. However, in practice, A−1 is not explicitly
would suggest that one needs to compute the n
required – only x is needed. This can be obtained more rapidly and with greater numerical precision using
Gaussian elimination[279, 129].

×

A.1.10 Eigenvalues and eigenvectors

The eigenvectors of a matrix correspond to a natural coordinate system in which the geometric transforma-
tion represented by A can be most easily understood.

Deﬁnition A.13 (Eigenvalues and Eigenvectors). For an n
with eigenvalue λ if

×

n square matrix A, e is an eigenvector of A

Ae = λe

(A.1.35)

Geometrically, the eigenvectors are special directions such that the eﬀect of the transformation A along a
direction e is simply to scale the vector e. For a rotation matrix R in general there will be no direction
preserved under the rotation so that the eigenvalues and eigenvectors are complex valued (which is why the
Fourier representation, which corresponds to representation in a rotated basis, is necessarily complex).

For an (n
eigenvector. We can reform equation (A.1.35) as

×

n) dimensional matrix, there are (including repetitions) n eigenvalues, each with a corresponding

(A

−

λI) e = 0

(A.1.36)

We can write equation (A.1.36) as Be = 0, where B
λI. If B has an inverse, then a solution is
e = B−10 = 0, which trivially satisﬁes the eigen-equation. For any non-trivial solution to the problem
Be = 0, we therefore need B to be non-invertible. This is equivalent to the condition that B has zero
determinant. Hence λ is an eigenvalue of A if

A

−

≡

det (A

−

λI) = 0

(A.1.37)

This is known as the characteristic equation. This determinant equation will be a polynomial in λ of degree
n and the resulting equation is known as the characteristic polynomial. Once we have found an eigenvalue,
the corresponding eigenvector can be found by substituting this value for λ in equation (A.1.35) and solving
the linear equations for e. It may be that for an eigenvalue λ the eigenvector is not unique and there is a
space of corresponding vectors. An important relation between the determinant and the eigenvalues of a
matrix is

det (A) =

n
(cid:89)

i=1

λi

616

(A.1.38)

DRAFT April 24, 2015

Linear Algebra

Hence a matrix is singular if it has a zero eigenvalue. The trace of a matrix can be expressed as

trace (A) =

(cid:88)

λi

i

(A.1.39)

For a real symmetric matrix A = AT, and eigenvectors ei, ej, then (ei)Tej = 0 if the eigenvalues λi and λj
are diﬀerent. This can be shown by considering:

Aei = λiei

⇒

(ej)TAei = λi(ej)Tei

Since A is symmetric, then

(A.1.40)

λi(ej)Tei = λj(ej)Tei
((ej)TA)ei = (Aej)Tei = λj(ej)Tei
(A.1.41)
= λj, this condition can be satisﬁed only if (ej)Tei = 0, namely that the eigenvectors are orthogonal.

⇒

If λi

Deﬁnition A.14 (Trace-Log formula). For a positive deﬁnite matrix A,

trace (log A)

log det (A)

≡

(A.1.42)

Note that the above logarithm of a matrix is not the element-wise logarithm. In MATLAB the required
function is logm. In general for an analytic function f (x), f (M) is deﬁned via the power-series expansion
of the function. On the right, since det (A) is a scalar, the logarithm is the standard logarithm of a scalar.

A.1.11 Matrix decompositions

Deﬁnition A.15 (Spectral decomposition). A real n

n symmetric matrix A has an eigen-decomposition

×

A =

n
(cid:88)

i=1

λieieT
i

where λi is the eigenvalue of eigenvector ei and the eigenvectors form an orthogonal set,

(cid:0)ei(cid:1)T

ej = δij

(cid:0)ei(cid:1)T

ei

In matrix notation

A = EΛET

(A.1.43)

(A.1.44)

(A.1.45)

where E = (cid:2)e1, . . . , en(cid:3) is the matrix of eigenvectors and Λ the corresponding diagonal eigenvalue matrix.
More generally, for a square non-symmetric diagonalisable A we can write

A = EΛE−1

Deﬁnition A.16 (Singular Value Decomposition). The SVD decomposition of a n

X = USVT

where dim U = n

×

n with UTU = In. Also dim V = p

p with VTV = Ip.

×

(A.1.46)

p matrix X is

×

(A.1.47)

The matrix S has dim S = n
p with zeros everywhere except on the diagonal entries. The singular values
are the diagonal entries [S]ii and are non-negative. The singular values are ordered so that the upper The left
diagonal element of S contains the largest singular value and Sii
Sjj for i < j. Assuming n < p (otherwise
transpose X), the computational complexity of computing the SVD is O (cid:0)4n2p + 8np2 + 9n3(cid:1) [129]. For the
‘thin’ SVD with n > p only the ﬁrst p columns of U and S are computed giving a decomposition

×

≥

X = UpSpVT

where Up has dimension n

p, and Sp is the p

×

×

p diagonal matrix of singular values.

DRAFT April 24, 2015

(A.1.48)

617

(cid:54)
Deﬁnition A.17 (Quadratic form).

xTAx + xTb

Multivariate Calculus

(A.1.49)

Deﬁnition A.18 (Positive deﬁnite matrix). A symmetric matrix A with the property that xTAx
0 for
any vector x is called positive semideﬁnite. A symmetric matrix A, with the property that xTAx > 0 for
any vector x
= 0 is called positive deﬁnite. A positive deﬁnite matrix has full rank and is thus invertible.
Using the eigen-decomposition of A,

≥

xTAx =

(cid:88)

i

λixTei(ei)Tx =

(cid:88)

(cid:16)

xTei(cid:17)2

λi

i

(A.1.50)

which is greater than zero if and only if all the eigenvalues are positive. Hence A is positive deﬁnite if and
only if all its eigenvalues are positive.

Eigenfunctions

(cid:90)

x

K(x(cid:48), x)φa(x) = λaφa(x(cid:48))

The eigenfunctions of a real symmetric kernel, K(x(cid:48), x) = K(x, x(cid:48)) are orthogonal:

(cid:90)

x

φa(x)φ∗

b (x) = δab

(A.1.51)

(A.1.52)

where φ∗(x) is the complex conjugate of φ(x). A kernel has a decomposition (provided the eigenvalues are
countable)

K(xi, xj) =

(cid:88)

µ

λµφµ(xi)φ∗

µ(xj)

Then

yiK(xi, xj)yj =

(cid:88)

i,j

(cid:88)

i,j,µ

λµyiφµ(xi)φ∗

µ(xj)yj =

(cid:88)

λµ

µ

(cid:32)

(cid:88)

i

(cid:124)

(cid:33)

yiφµ(xi)

(cid:32)

(cid:88)

yiφ∗

µ(xi)

(cid:123)(cid:122)
zi

i

(cid:125)

(cid:124)

(cid:123)(cid:122)
z∗
i

(cid:33)

(cid:125)

(A.1.53)

(A.1.54)

which is greater than zero if the eigenvalues are all positive (since for complex z, zz∗
are uncountable the appropriate decomposition is

≥

0). If the eigenvalues

K(xi, xj) =

(cid:90)

λ(s)φ(xi, s)φ∗(xj, s)ds

A.2 Multivariate Calculus

(A.1.55)

Deﬁnition A.19 (Partial derivative). Consider a function of n variables, f (x1, x2, . . . , xn)
partial derivative of f w.r.t. xi is deﬁned as the following limit (when it exists)

≡

f (x). The

∂f
∂xi

= lim
h→0

f (x1, . . . , xi−1, xi + h, xi+1, . . . , xn)

h

f or g:

∇

The gradient vector of f is denoted

f (x)

∇

≡

g(x)

≡











∂f
∂x1

...

∂f
∂xn

f (x)

−

(A.2.1)

(A.2.2)

618

DRAFT April 24, 2015

(cid:54)
Multivariate Calculus

Figure A.4: Interpreting the gradient. The ellipses are contours
of constant function value, f = const. At any point x, the gradi-
f (x) points along the direction of maximal increase
ent vector
of the function.

∇

A.2.1

Interpreting the gradient vector

Consider a function f (x) that depends on a vector x. We are interested in how the function changes when
the vector x changes by a small amount : x
x + δ, where δ is a vector whose length is very small.
According to a Taylor expansion, the function f will change to

→

f (x + δ) = f (x) +

(cid:88)

i

δi

∂f
∂xi

+ O (cid:0)δ2(cid:1)

We can interpret the summation above as the scalar product between the vector
[

and δ.

f ]i = ∂f
∂xi

∇

f (x + δ) = f (x) + (

δ + O (cid:0)δ2(cid:1)

f )

·

∇

(A.2.3)

f with components

∇

(A.2.4)

The gradient points along the direction in which the function increases most rapidly. To see this, consider
a direction ˆp (a unit length vector). Then a displacement, δ units along this direction changes the function
value to

(A.2.5)

(A.2.6)

f (x + δ ˆp)

f (x) + δ

f (x)

ˆp

·

∇

≈

The direction ˆp for which the function has the largest change is that which maximises the overlap

f (x)

ˆp =

·

∇

|∇

f (x)

ˆp

cos θ =

|

||

f (x)
|

cos θ

where θ is the angle between ˆp and
Hence, the direction along which the function changes the most rapidly is along

∇

|∇
f (x). The overlap is maximised when θ = 0, giving ˆp =

f (x).

∇

A.2.2 Higher derivatives

The second derivative of an n-variable function is deﬁned by

(cid:19)

∂
∂xi

(cid:18) ∂f
∂xj

i = 1, . . . , n; j = 1, . . . , n

which is usually written

∂2f
∂xi∂xj

,

i

= j

∂2f
2 ,
∂xi

i = j

If the partial derivatives ∂2f /∂xi∂xj and ∂2f /∂xj∂xi exist then

∂2f /∂xi∂xj = ∂2f /∂xj∂xi .

f (x)/

|∇

f (x)

.
|

∇

(A.2.7)

(A.2.8)

(A.2.9)

This is also denoted by
matrix called the Hessian matrix of f (x).

∇∇

f . These n2 second partial derivatives are represented by a square, symmetric

Hf (x) =







∂2f
2
∂x1

...

. . .

∂2f
∂x1∂xn

...

∂2f
∂x1∂xn

. . .

∂2f
2
∂xn







DRAFT April 24, 2015

(A.2.10)

619

x1x2f(x)(cid:54)
Deﬁnition A.20 (Chain rule). Let each xj be parameterized by u1, . . . , um, i.e. xj = xj(u1, . . . , um).

Inequalities

∂f
∂uα

=

n
(cid:88)

j=1

∂f
∂xj

∂xj
∂uα

or in vector notation

∂
∂uα

f (x(u)) =

f T(x(u))

∇

∂x(u)
∂uα

(A.2.11)

(A.2.12)

Deﬁnition A.21 (Directional derivative). Assume f is diﬀerentiable. We deﬁne the scalar directional
derivative (Dvf )(x∗) of f in a direction v at a point x∗. Let x = x∗ + hv, Then

(Dvf )(x∗) =

d
dh

f (x∗ + hv)

(cid:12)
(cid:12)
(cid:12)
(cid:12)h=0

(cid:88)

=

j

vj

∂f
∂xj

(cid:12)
(cid:12)
(cid:12)
(cid:12)
(cid:12)
(cid:12)x=x∗

f Tv

=

∇

A.2.3 Matrix calculus

Deﬁnition A.22 (Derivative of a matrix trace). For matrices A and B

∂
∂A

trace (AB)

BT

≡

Deﬁnition A.23 (Derivative of log det (A)).

∂ log det (A) = ∂trace (log A) = trace (cid:0)A−1∂A(cid:1)

So that

∂
∂A

log det (A) = A−T

Deﬁnition A.24 (Derivative of a matrix inverse). For an invertible matrix A,

∂A−1

A−T∂AA−1

≡ −

A.3

Inequalities

A.3.1 Convexity

Deﬁnition A.25 (Convex function). A function f (x) is deﬁned as convex if for any x, y and 0

f (λx + (1

λ)y)

−

≤

λf (x) + (1

λ)f (y)

−

If

f (x) is convex, f (x) is called concave.

−

(A.2.13)

(A.2.14)

(A.2.15)

(A.2.16)

(A.2.17)

λ

1

≤

≤

(A.3.1)

An intuitive picture is given by considering the quantity λx + (1
λ)y. As we vary λ from 0 to 1, this traces
points between x (λ = 0) and y (λ = 1). Hence for λ = 0 we start at the point x, f (x) and as λ increase
trace a straight line towards the point y, f (y) at λ = 1. Convexity states that the function f always lies

−

620

DRAFT April 24, 2015

Multivariate Optimisation

below this straight line. Geometrically this means that the function f (x) is always always non-decreasing.
Hence if d2f (x)/dx2
0 the function is convex. As an example, the function log x is concave since its second
≥
derivative is negative:

d
dx

log x =

1
x

,

d2
dx2 log x =

1
x2

−

A.3.2 Jensen’s inequality

For a convex function, f (x), it follows directly from the deﬁnition of convexity that

f (

x
(cid:104)

(cid:105)p(x))
for any distribution p(x).

f (x)

≤ (cid:104)

(cid:105)p(x)

(A.3.2)

(A.3.3)

A.4 Optimisation

Deﬁnition A.26 (Critical point). When all ﬁrst-order partial derivatives at a point are zero (i.e.
f = 0)
then the point is said to be a stationary or critical point. A critical point can correspond to a minimum,
maximum or saddle point of the function.

∇

There is a minimum of f at x∗ if f (x∗)
stationary point,

≤

f (x∗) = 0. The Taylor expansion of f at the optimum is given by

f (x) for all x suﬃciently close to x∗. This requires x∗ to be a

∇

f (x∗ + hv) = f (x∗) + h2vTHf v + O(h3)

(A.4.1)

Thus the minimum condition requires that vTHf v

0, i.e. the Hessian is non-negative deﬁnite.

≥

Deﬁnition A.27 (Conditions for a minimum). Suﬃcient conditions for a minimum at x∗ are (i)
and (ii) Hf (x∗) is positive deﬁnite.

∇

f (x∗) = 0

For a quadratic function f (x) = 1

2 xTAx

−

bTx + c, with symmetric A the condition

f (x∗) = 0 reads:

∇

Ax∗

−

b = 0

(A.4.2)

If A is invertible this equation has the unique solution x∗ = A−1b.
corresponds to the minimum point.

If A is positive deﬁnite then x∗

A.5 Multivariate Optimisation

In most cases the optima of functions cannot be found by algebraic means alone and numerical techniques are
required. The search techniques that we consider here are iterative, i.e. we proceed towards the minimum x∗
by a sequence of steps. Perhaps the simplest approach to minimising a multivariate function f (x) is to break
the problem into a sequence of one-dimensional problems. By initialising the elements of x at random, one
then selects a component of x to update, keeping all others ﬁxed. This coordinatewise optimisation decreases
the function via a sequence of one-dimensional minimisations. Whilst such a procedure is simple, it can be
ineﬃcient in high-dimensions, particularly when there are strong dependencies between the components in
x. For this reason, it is useful to use gradient-based procedures that take the local geometry of the objective
into account. We will consider a general class of iterative gradient-based method for which, on the kth step,
we take a step of length αk in the direction pk,

xk+1 = xk + αkpk

DRAFT April 24, 2015

(A.5.1)

621

Multivariate Optimisation

Figure A.5: Optimisation using line search along
steepest descent directions. Following the steepest
way downhill from a point (and continuing for a ﬁ-
nite time in that direction) doesn’t always result in
the fastest way to get to the bottom.

A.5.1 Gradient descent with ﬁxed stepsize

Locally, if we are at point xk, we can decrease f (x) by taking a step in the direction
gradient descent works, consider the general update

g(x). To see why

−

xk+1 = xk

xf

α

∇

−

For small α we can expand f around xk using Taylor’s theorem:

f (xk + αkpk)

f (xk)

α

xf

||

||∇

−

≈

(A.5.2)

(A.5.3)

2. If α is non-inﬁnitesimal, it is always possible that we will
so that the change in f is ∆f =
||
step over the true minimum. Making η very small guards against this, but means that the optimization
process will take a very long time to reach a minimum. A simple idea that can improve convergence of
gradient descent is to include at each iteration a proportion of the change from the previous iteration,
pk =

βgk−1, where β is the momentum coeﬃcient.

||∇

gk

xf

−

α

−

−

An unfortunate aspect of gradient descent is that the change in the function value depends on the coordinate
f (x).
system. Consider a new coordinate system
Then the change in the function ˆf under a gradient update is

x = My for an invertible square matrix M.

Deﬁne ˆf (y)

≡

@@
@@

∆ ˆf

ˆf (y

α

y ˆf )

ˆf (y)

α

−
In the original coordinate system, the change in x is

≈ −

||∇

∇

−

≡

y ˆf
2
||

Since

xf (x), transforming the change in y to a change in x we have

∆x =

α

xf

−
∇
y ˆf = MT

∇
M∆y =

∇
αMMT

−

xf (x)

∇

which, unless MMT = I, is not equal to ∆x. Similarly the change in the function value is

∆ ˆf =

α

−

∇

xf (x)TMMT

xf (x)

∇

which, except for an orthogonal M, is not equal to ∆f =

α

−

||∇

xf

2.
||

A.5.2 Gradient descent with line searches

(A.5.4)

(A.5.5)

An extension to the idea of gradient descent is to choose the direction of steepest descent, as indicated
by the gradient g, but to calculate the value of the step to take which most reduces the value of f when
moving in that direction. This involves solving the one-dimensional problem of minimizing f (xk + αkgk)
with respect to αk, and is known as a line search. To ﬁnd the optimal step size at the k-th step, we choose
αk to minimize f (xk + αkpk). So setting F (λ) = f (xk + λpk), at this step we solve the one-dimensional
minimization problem for F (λ). Thus our choice of αk = λ∗ will satisfy F (cid:48)(αk) = 0. Now

F (cid:48)(αk) =

=

d
dh
d
dh

F (αk + h)|h=0 =

d
dh

f (xk + αkpk + hpk)|h=0

f (xk+1 + hpk)|h=0 = (Dpk f )(xk+1) =

f T(xk+1)pk

∇

(A.5.6)

So F (cid:48)(αk) = 0 means the directional derivative in the search direction must vanish at the new point and
this gives condition 0 = gT
k+1pk. If the step size is chosen to reduce f as much as it can in that direction,
then no further decrease in E can be made by moving in that direction for the moment. Thus the next step
will have no component in that direction and will be at right angles to the previous just taken. This can
lead to zig-zag type behaviour in the optimisation.

622

DRAFT April 24, 2015

Multivariate Optimisation

A.5.3 Minimising quadratic functions using line search

Consider minimising the quadratic function

f (x) =

1
2

xTAx

−

bTx + c

(A.5.7)

where A is positive deﬁnite and symmetric. Although we know where the minimum of this simple function,
based on using linear algebra, we wish to use this function as a toy model for more complex functions. As we
‘zoom in’ to the minimum of a smooth function, it will increasingly appear quadratic. Hence at such small
scales, methods that work for the quadratic case should work in general smooth functions. If the general
function looks roughly quadratic on the larger scale, then these methods will also work well in that case.
One approach is to search along a particular direction p, and ﬁnd a minimum along this direction. We can
then search for a deeper minimum by looking in diﬀerent directions. That is, we can search ﬁrstly along a
line x + λp such that the function attains a minimum. This has solution,

λ =

(b

p

Ax)
−
pTAp

·

= −∇

f (x)
·
pTAp

p

(A.5.8)

How should we now choose the next line search direction pnew? It would seem sensible to choose successive
line search directions p according to pnew =
f (x), so that each time we minimise the function along
the line of steepest descent. However, this is generally not the optimal choice, see ﬁg(A.5). If the matrix
A were diagonal, then the minimisation is straightforward and can be carried out independently for each
dimension. If we could therefore ﬁnd an invertible matrix P with the property that PTAP is diagonal then
the solution is easy since for

−∇

ˆf (ˆx) =

1
2

ˆxTPTAPˆx

bTPˆx + c

−

(A.5.9)

with x = Pˆx, we can compute the minimum for each dimension of ˆx separately and then retransform to
ﬁnd x∗ = Pˆx∗. The columns of such a matrix P are called conjugate vectors.

Deﬁnition A.28 (Conjugate vectors). The vectors pi, i = 1, . . . , k are called conjugate to the matrix A, if
and only if for i, j = 1, . . . , k and i

= j:

i Apj = 0 and pT
pT

i Api > 0 .

The two conditions guarantee that conjugate vectors are linearly independent: Assume that

0 =

k
(cid:88)

j=1

αjpj =

i−1
(cid:88)

j=1

αjpj + αipi +

k
(cid:88)

j=i+1

αjpj

(A.5.10)

(A.5.11)

Now multiplying from the left with pT
As we can make this argument for any i = 1, . . . , k, all of the αi must be zero.

i A yields 0 = αipT

i Api. So αi is zero since we know that pT

i Api > 0.

A.5.4 Gram-Schmidt construction of conjugate vectors

Assume we already have k conjugate vectors p1, . . . , pk and let v be a vector which is linearly independent
of p1, . . . , pk. We then use a Gram-Schmidt procedure:

pk+1 = v

k
(cid:88)

j=1

pT
j Av
pT
j Apj

−

pj

(A.5.12)

for which it is clear that the vectors p1, . . . , pk+1 are conjugate if A is positive deﬁnite. We can construct n
conjugate vectors for a positive deﬁnite matrix in the following way: We start with n linearly independent
vectors u1, . . . , un. We then set p1 = u1 and use (A.5.12) to compute p2 from p1 and v = u2. Next we set
v = u3 and compute p3 from p1, p2 and v. Continuing in this manner we obtain n conjugate vectors. Note
that at each stage of the procedure the vectors u1, . . . , uk span the same subspace as the vectors p1, . . . , pk.

DRAFT April 24, 2015

623

(cid:54)
A.5.5 The conjugate vectors algorithm

Multivariate Optimisation

Let us assume that when minimising f (x) = 1
2 xTAx
conjugate to A which we use as our search directions. Using this our iterative solution takes the form

bTx + c we ﬁrst construct n vectors p1, . . . , pn

−

xk+1 = xk + αkpk .

where at each step we choose αk by an exact line search with

αk =

pT
k gk
pT
k Apk

−

.

(A.5.13)

(A.5.14)

This conjugate vectors algorithm has the geometrical interpretation that not only is the directional derivative
zero at the new point along the direction pk, it is zero along all the previous search directions p1, . . . , pk,
f T(xn+1)pi = 0, for i = 1, . . . , n;
known as the Luenberger expanding subspace theorem.
that is

In particular

∇

f T(xn+1)(p1, p2, . . . , pn) = 0 .

∇

(A.5.15)

f (xn+1) = 0 and the
The square matrix P = (p1, p2, . . . pn) is invertible since the pi are conjugate, so
point xn+1 is therefore the minimum x∗ of the quadratic function f . So in contrast to gradient descent, for
a quadratic function the conjugate vectors algorithm converges in a ﬁnite number of steps.

∇

A.5.6 The conjugate gradients algorithm

The conjugate gradients algorithm is a special case of the conjugate vectors algorithm in which we construct
the conjugate vectors on-the-ﬂy. After k-steps of the conjugate vectors algorithm we need to construct a
vector pk+1 which is conjugate to p1, . . . , pk. In the conjugate gradients algorithm one makes the special
choice v =
f (xk+1)
f (xk+1) = 0. In the latter case xk+1
is linearly independent of p1, . . . , pk and a valid choice for v, unless
is our minimum and the algorithm terminates. Using the notation gk =
f (xk), the equation for the new
∇
search direction given by the Gram-Schmidt procedure equation (A.5.12) is:

f (xk+1). The gradient at the new point xk+1 is orthogonal to pi, i = 1, . . . , k, so

−∇

∇

∇

pk+1 =

gk+1 +

−

k
(cid:88)

i=1

pT
i Agk+1
pT
i Api

pi .

(A.5.16)

Since gk+1 is orthogonal to pi, i = 1, . . . , k, we have pT

k+1gk+1 =

gT
k+1gk+1. So αk+1 can be written as

−

αk+1 =

gT
k+1gk+1
pT
k+1Apk+1

,

(A.5.17)

and in particular αk+1
= 0. We now want to show that because we have been using the conjugate gradients
algorithm at the previous steps as well, in equation (A.5.16) all terms but the last in the sum over i vanish.
We shall assume that k > 0 since in the ﬁrst step (k = 0) we just set p1 =

g1. First note that

−

gi+1

−
and since αi

gi = Axi+1

b

−

−

(Axi

= 0, then Api = (gi+1

b) = A(xi+1

xi) = αiApi

−

−
gi)/αi. So in equation (A.5.16):

i Agk+1 = gT
pT

k+1Api = gT

−
k+1(gi+1

gi)/αi = (gT

k+1gi+1

gT
k+1gi)/αi

−

−

(A.5.18)

(A.5.19)

Since the pi were obtained by applying the Gram-Schmidt procedure to the gradients gi, we have gT
and gT

k+1pi = 0

k+1gi = 0 for i = 1, . . . , k. This shows that
(cid:26) 0
gT
k+1gk+1/αk

i Agk+1 = (gT
pT

gT
k+1gi)/αi =

k+1gi+1

−

Hence equation (A.5.16) simpliﬁes to

pk+1 =

gk+1 +

−

gT
k+1gk+1/αk
k Apk

pT

pk .

624

i < k

if 1
if i = k

≤

(A.5.20)

(A.5.21)

DRAFT April 24, 2015

(cid:54)
(cid:54)
Multivariate Optimisation

Algorithm A.1 Conjugate Gradients for minimising a function f (x)

1: k = 1
2: Choose x1.
3: p1 =
g1
−
4: while gk
5:

= 0 do
αk = argmin

f (xk + αkpk)

6:

7:

8:

αk
xk+1 := xk + αkpk
βk := gT
k+1gk+1/(gT
pk+1 :=
−
k = k + 1

k gk)
gk+1 + βkpk

9:
10: end while

This can be brought into an even simpler form by applying equation (A.5.17) to αk:

pk+1 =

gT
k+1gk+1
pT
k Apk
We shall write this in the form

gk+1 +

−

pT
k Apk
gT
k gk

pk =

gk+1 +

−

gT
k+1gk+1
gT
k gk

pk

pk+1 =

gk+1 + βkpk where βk =

−

gT
k+1gk+1
gT
k gk

.

(cid:46) Line Search

(A.5.22)

(A.5.23)

The formula (A.5.23) for βk is due to Fletcher and Reeves[244]. Since the gradients are orthogonal, βk can
also be written as

βk =

gT
k+1(gk+1
gT
k gk

−

gk)

,

(A.5.24)

this is the Polak-Ribi`ere formula. The choice between the two expressions for βk can be of some importance
if f is not quadratic with the Polak-Ribi`ere formula the most commonly used. The important point here
is that we derived the above algorithm for the particular case of minimising a quadratic function f . By
writing the algorithm however only in terms of f and its gradients, we make no explicit reference to the
fact that f is quadratic. This means that we can therefore apply this algorithm to non-quadratic functions
as well. The more similar the function f is to a quadratic function, the more conﬁdent we can be that the
algorithm will ﬁnd the minimum.

A.5.7 Newton’s method

Consider a function f (x) that we wish to ﬁnd the minimum of. A Taylor expansion up to second order gives

f (x + ∆) = f (x) + ∆T

∆
|
The matrix Hf is the Hessian. Diﬀerentiating the right hand side with respect to ∆ (or, equivalently,
3) term) has its lowest value
∆
completing the square), we ﬁnd that the right hand side (ignoring the O(
|
|
when

∆THf ∆ + O(
|

(A.5.25)

f +

3)

∇

1
2

f =

∇

Hf ∆

−

⇒

∆ =

H−1

f ∇

−

f

@@

Hence, an optimisation routine to minimise f is given by the Newton update

xk+1 = xk

(cid:15)H−1

f ∇

f

−

(A.5.26)

(A.5.27)

where the scalar 0 < (cid:15) < 1 is often used in practice to improve the convergence. A beneﬁt of Newton method
over gradient descent is that the decrease in the objective function is invariant under a linear change of co-
f (x), since H ˆf = MTHf M and
ordinates,
xf the change in the
original x system is given by

x = My. Deﬁning ˆf (y)

y ˆf = MT

∇

∇

≡

@@

(cid:15)M∆y =

−

(cid:16)

(cid:15)M

−

MTHf M

(cid:17)−1

MT

xf =

∇

(cid:15)H−1

f ∇

−

xf

which means that the change in the variable (and hence function value) is independent of the coordinate
system.

DRAFT April 24, 2015

625

(cid:54)
Algorithm A.2 Quasi-Newton for minimising a function f (x)

Multivariate Optimisation

1: k = 1
2: Choose x1
3: ˜H1 = I
4: while gk
= 0 do
˜Hkgk
pk =
5:
−
αk = argmin

6:

7:

8:

f (xk + αkpk)

αk
xk+1 := xk + αkpk
sk = xk+1
k = k + 1

−

xk, yk = gk+1

9:
10: end while

gk, and update ˜Hk+1

−

(cid:46) Line Search

Quasi-Newton methods

For large-scale problems both storing the Hessian and solving the resulting linear system is computationally
demanding, especially if the matrix is close to singular. An alternative is to set up the iteration

xk+1 = xk

αkSkgk.

−

(A.5.28)

For Sk = A−1 we have Newton’s method, while if Sk = I we have steepest descent. In general it would
seem to be a good idea to choose Sk to be an approximation to the inverse Hessian. Also note that it is
important that Sk be positive deﬁnite so that for small αk we obtain a descent method. The idea behind
most quasi-Newton methods is to try to construct an approximate inverse Hessian ˜Hk using information
gathered as the descent progresses, and to set Sk = ˜Hk. As we have seen, for a quadratic optimization
problem we have the relationship

gk+1

−

gk = A(xk+1

xk)

−

Deﬁning

sk = xk+1

xk

−

and

yk = gk+1

gk

−

we see that equation A.5.29 becomes

yk = Ask

It is therefore reasonable to demand that

˜Hk+1yi = si

1

i

≤

≤

k

(A.5.29)

(A.5.30)

(A.5.31)

(A.5.32)

After n linearly independent steps we would then have ˜Hn+1 = A−1. For k < n there are an inﬁnity of
solutions for ˜Hk+1 satisfying equation A.5.32. A popular choice is the Broyden-Fletcher-Goldfarb-Shanno
(or BFGS) update, given by

(cid:32)

˜Hk+1 = ˜Hk +

1 +

(cid:33)

˜Hkyk
yT
k
yT
k sk

sksT
k
sT
k yk −

skyT
k

˜Hk + ˜HkyksT
k

sT
k yk

(A.5.33)

This is a rank-2 correction to ˜Hk constructed from the vectors sk and ˜Hkyk. The direction vectors, pk =

˜Hkgk, produced by the algorithm obey

−

pT

i Apj = 0

i < j

1

≤

k,

≤

˜Hk+1Api = pi

1

i

≤

≤

k

(A.5.34)

The storage requirements for Quasi Newton methods scale quadratically with the number of variables,
and hence these methods tend to be used only for smaller problems. Limited memory BFGS reduces the
storage by only using the l latest updates in computing the approximate Hessian inverse, equation (A.5.33).
In contrast, the memory requirements for pure Conjugate Gradient methods scale only linearly with the
dimension of x. As before, although the algorithm was derived with a quadratic function in mind, the ﬁnal
form of the algorithm depends only on f and its gradients and may therefore be applied to non-quadratic
functions f .

626

DRAFT April 24, 2015

(cid:54)
Constrained Optimisation using Lagrange Multipliers

A.6 Constrained Optimisation using Lagrange Multipliers

Consider ﬁrst the problem of minimising f (x) subject to a single constraint c(x) = 0. A formal treatment
of this problem is beyond the scope of these notes and requires understanding the conditions under which
the optimum can be found[48]. As an informal argument, however, imagine that we have already identiﬁed
an x that satisﬁes the constraint, that is c(x) = 0. How can we tell if this x minimises the function f ? We
are only allowed to search for lower function values around this x in directions which are consistent with
the constraint. For a small change δ, the change in the constraint is,

c(x + δ)

c(x) + δ

c(x)

· ∇

≈

Let us also explore the change in f along a direction δ where δ

c(x) = 0,

· ∇

f (x + δ)

f (x) +

f (x)

δ.

·

∇

≈

(A.6.1)

(A.6.2)

We are looking for a point x and direction δ such that the change in f and c is minimal. This dual
requirement can be represented as to ﬁnd x and δ that minimise

δ
|

· ∇

f (x)

2 + γ
|

δ
|

2

c(x)
|

· ∇

where γ > 0. This is a simple quadratic form and optimising for δ gives that

f (x) =

∇

δ
δ

γ

−

c(x)
f (x) ∇

· ∇
· ∇

c(x)

Hence at the constrained optimum

f (x) = λ

∇

∇

c(x)

(A.6.3)

(A.6.4)

(A.6.5)

for some scalar λ. We can formulate this requirement as to look for x and λ that constitute a stationary
point of the Lagrangian

(x, λ) = f (x)

L

λc(x)

−

(A.6.6)

Diﬀerentiating with respect to x, we obtain the requirement
respect to λ, we get that c(x) = 0.

∇

f (x) = λ

∇

c(x), and diﬀerentiating with

In the multiple constraint case

ci(x) = 0
}
{

we ﬁnd the stationary point of the Lagrangian

(x, λ) = f (x)

L

(cid:88)

−

i

λici(x).

A.6.1 Lagrange Dual

Consider the ‘primal’ problem

minimise f (x) subject to c(x) = 0

The Lagrange dual is deﬁned as

(λ) = min

[f (x) + λc(x)]

L

x

By construction, for any x,

(λ)

L

≤

f (x) + λc(x)

Now consider the optimal x∗ that solves the primal problem equation (A.6.8). Then

(λ)

L

≤

f (x∗) + λc(x∗) = f (x∗)

DRAFT April 24, 2015

(A.6.7)

(A.6.8)

(A.6.9)

(A.6.10)

(A.6.11)

627

where the last step follows since x∗ solves the primal problem, meaning that c(x∗) = 0. Since
the optimal λ is given by solving the unconstrained ‘dual’ problem

(λ)

L

≤

f (x∗),

Constrained Optimisation using Lagrange Multipliers

max

λ L

(λ)

In addition to providing a bound that enables one to bracket the optimal primal value,

(λ)

L

≤

f (x∗)

f (x),

≤

for c(x) = 0

(A.6.12)

(A.6.13)

the dual possesses the interesting property that it is concave, irrespective of whether f is convex. This
follows since by construction the function f (x) + λc(x) is concave in λ for any point x. More explicitly,
consider

(λ + δ) = min

[f (x) + (λ + δ)c(x)]

≤

f (x) + λc(x) + δc(x)

x

L

Similarly,

(λ

L

−

δ)

≤

f (x) + λc(x)

δc(x)

−

(A.6.14)

(A.6.15)

Averaging equation (A.6.14) and equation (A.6.15), and taking the minimum over x of both sides, we have

1
2

(λ

(
L

−

δ) +

(λ + δ))

L

(λ)

≤ L

which shows that

is concave.

L

(A.6.16)

628

DRAFT April 24, 2015

Bibliography

[1] L. F. Abbott, J. A. Varela, K. Sen, and S. B. Nelson. Synaptic Depression and Cortical Gain Control. Science,

275:220–223, 1997.

[2] D. H. Ackley, G. E. Hinton, and T. J. Sejnowski. A Learning Algorithm for Boltzmann Machines. Cognitive

Science, 9:147–169, 1985.

[3] R. P. Adams and D. J. C. MacKay. Bayesian Online Changepoint Detection. Cavendish laboratory, department

of physics, University of Cambridge, Cambridge, UK, 2006.

[4] E. Airoldi, D. Blei, E. Xing, and S. Fienberg. A latent mixed membership model for relational data. In LinkKDD
’05: Proceedings of the 3rd international workshop on Link discovery, pages 82–89, New York, NY, USA, 2005.
ACM.

[5] E. M. Airoldi, D. M. Blei, S. E. Fienberg, and E. P. Xing. Mixed membership stochastic blockmodels. Journal

of Machine Learning Research, 9:1981–2014, 2008.

[6] D. L. Alspach and H. W. Sorenson. Nonlinear Bayesian Estimation Using Gaussian Sum Approximations. IEEE

Transactions on Automatic Control, 17(4):439–448, 1972.

[7] S-i. Amari. Natural Gradient Learning for Over and Under-Complete Bases in ICA. Neural Computation,

11:1875–1883, 1999.

[8] I. Androutsopoulos, J. Koutsias, K. V. Chandrinos, and C. D. Spyropoulos. An experimental comparison of
In Proceedings of the
naive Bayesian and keyword-based anti-spam ﬁltering with personal e-mail messages.
23rd annual international ACM SIGIR conference on Research and development in information retrieval, pages
160–167, New York, NY, USA, 2000. ACM.

[9] S. Arora and C. Lund. Hardness of approximations. In Approximation algorithms for NP-hard problems, pages

399–446. PWS Publishing Co., Boston, MA, USA, 1997.

[10] F. R. Bach and M. I. Jordan. Thin junction trees. In T. G. Dietterich, S. Becker, and Z. Ghahramani, editors,
Advances in Neural Information Processing Systems (NIPS), number 14, pages 569–576, Cambridge, MA, 2001.
MIT Press.

[11] F. R. Bach and M. I. Jordan. A probabilistic interpretation of canonical correlation analysis. Computer Science

Division and Department of Statistics 688, University of California Berkeley, Berkeley, USA, 2005.

[12] Y. Bar-Shalom and Xiao-Rong Li. Estimation and Tracking : Principles, Techniques and Software. Artech

House, Norwood, MA, 1998.

[13] D. Barber. Dynamic Bayesian Networks with Deterministic Tables. In S. Becker, S. Thrun, and K. Obermayer,
editors, Advances in Neural Information Processing Systems (NIPS), number 15, pages 713–720, Cambridge,
MA, 2003. MIT Press.

[14] D. Barber. Learning in Spiking Neural Assemblies. In S. Becker, S. Thrun, and K. Obermayer, editors, Advances
in Neural Information Processing Systems (NIPS), number 15, pages 149–156, Cambridge, MA, 2003. MIT Press.

[15] D. Barber. Are two Classiﬁers performing equally? A treatment using Bayesian Hypothesis Testing. IDIAP-

RR 57, IDIAP, Rue de Simplon 4, Martigny, CH-1920, Switerland, May 2004. IDIAP-RR 04-57.

629

BIBLIOGRAPHY

BIBLIOGRAPHY

[16] D. Barber. The auxiliary variable trick for deriving kalman smoothers. IDIAP-RR 87, IDIAP, Rue de Simplon

4, Martigny, CH-1920, Switerland, December 2004. IDIAP-RR 04-87.

[17] D. Barber. Expectation Correction for smoothing in Switching Linear Gaussian State Space models. Journal of

Machine Learning Research, 7:2515–2540, 2006.

[18] D. Barber. Clique Matrices for Statistical Graph Decomposition and Parameterising Restricted Positive Deﬁnite
Matrices. In D. A. McAllester and P. Myllymaki, editors, Uncertainty in Artiﬁcial Intelligence, number 24, pages
26–33, Corvallis, Oregon, USA, 2008. AUAI press.

[19] D. Barber and F. V. Agakov. Correlated sequence learning in a network of spiking neurons using maximum

likelihood. Informatics Research Reports EDI-INF-RR-0149, Edinburgh University, 2002.

[20] D. Barber and F.V. Agakov. The IM Algorithm: A variational approach to Information Maximization.

In

Advances in Neural Information Processing Systems (NIPS), number 16, 2004.

[21] D. Barber and C. M. Bishop. Bayesian Model Comparison by Monte Carlo Chaining. In M. C. Mozer, M. I.
Jordan, and T. Petsche, editors, Advances in Neural Information Processing Systems (NIPS), number 9, pages
333–339, Cambridge, MA, 1997. MIT Press.

[22] D. Barber and C. M. Bishop. Ensemble Learning in Bayesian Neural Networks. In Neural Networks and Machine

Learning, pages 215–237. Springer, 1998.

[23] D. Barber and S. Chiappa. Uniﬁed Inference for Variational Bayesian Linear Gaussian State-Space Models. In
B. Sch¨olkopf, J. Platt, and T. Hoﬀman, editors, Advances in Neural Information Processing Systems (NIPS),
number 19, pages 81–88, Cambridge, MA, 2007. MIT Press.

[24] D. Barber and W. Wiegerinck. Tractable Variational Structures for Approximating Graphical Models. In M. S.
Kearns, S. A. Solla, and D. A. Cohn, editors, Advances in Neural Information Processing Systems (NIPS),
number 11, pages 183–189, Cambridge, MA, 1999. MIT Press.

[25] D. Barber and C. K. I. Williams. Gaussian processes for Bayesian classiﬁcation via hybrid Monte Carlo. In
M. C. Mozer, M. I. Jordan, and T. Petsche, editors, Advances in Neural Information Processing Systems NIPS
9, pages 340–346, Cambridge, MA, 1997. MIT Press.

[26] R. J. Baxter. Exactly solved models in statistical mechanics. Academic Press, 1982.

[27] M. J. Beal, F. Falciani, Z. Ghahramani, C. Rangel, and D. L. Wild. A Bayesian approach to reconstructing

genetic regulatory networks with hidden factors. Bioinformatics, (21):349–356, 2005.

[28] A. Becker and D. Geiger. A suﬃciently fast algorithm for ﬁnding close to optimal clique trees. Artiﬁcial

Intelligence, 125(1-2):3–17, 2001.

[29] A. J. Bell and T. J. Sejnowski. An Information-Maximization Approach to Blind Separation and Blind Decon-

volution. Neural Computation, 7(6):1129–1159, 1995.

[30] R. E. Bellman. Dynamic Programming. Princeton University Press, Princeton, NJ, 1957. Paperback edition by

Dover Publications (2003).

[31] Y. Bengio and P. Frasconi.

Input-Output HMMs for sequence processing.

IEEE Trans. Neural Networks,

(7):1231–1249, 1996.

[32] A. L. Berger, S. D. Della Pietra, and V. J. D. Della Pietra. A maximum entropy approach to natural language

processing. Computational Linguistics, 22(1):39–71, 1996.

[33] J. O. Berger. Statistical Decision Theory and Bayesian Analysis. Springer, second edition, 1985.

[34] D. P. Bertsekas. Nonlinear Programming. Athena Scientiﬁc, 2nd edition, 1999.

[35] D. P. Bertsekas. Dynamic Programming and Optimal Control. Athena Scientiﬁc, second edition, 2000.

[36] J. Besag. Spatial Interactions and the Statistical Analysis of Lattice Systems. Journal of the Royal Statistical

Society, Series B, 36(2):192–236, 1974.

[37] J. Besag. On the statistical analysis of dirty pictures. Journal of the Royal Statistical Society, Series B, 48:259–

302, 1986.

[38] J. Besag and P. Green. Spatial statistics and Bayesian computation. Journal of the Royal Statistical Society,

Series B, 55:25–37, 1993.

630

DRAFT April 24, 2015

BIBLIOGRAPHY

BIBLIOGRAPHY

[39] G. J. Bierman. Measurement updating using the U-D factorization. Automatica, 12:375–382, 1976.

[40] N. L. Biggs. Discrete Mathematics. Oxford University Press, 1990.

[41] K. Binder and A. P. Young. Spin glasses: Experimental facts, theoretical concepts, and open questions. Rev.

Mod. Phys., 58(4):801–976, Oct 1986.

[42] C. M. Bishop. Neural Networks for Pattern Recognition. Oxford University Press, 1995.

[43] C. M. Bishop. Pattern Recognition and Machine Learning. Springer, 2006.

[44] C. M. Bishop and M. Svens´en. Bayesian hierarchical mixtures of experts. In U. Kjaerulﬀ and C. Meek, editors,
Proceedings Nineteenth Conference on Uncertainty in Artiﬁcial Intelligence, pages 57–64. Morgan Kaufmann,
2003.

[45] F. Black and M. Scholes. The Pricing of Options and Corporate Liabilities. Journal of Political Economy,

81(3):637–654, 1973.

[46] D. Blei, A. Ng, and M. Jordan. Latent Dirichlet allocation. Journal of machine Learning Research, (3):993–1022,

2003.

[47] R. R. Bouckaert. Bayesian belief networks: from construction to inference. PhD thesis, University of Utrecht,

1995.

[48] S. Boyd and L. Vandenberghe. Convex Optimization. Cambridge University Press, 2004.

[49] Y. Boykov and V. Kolmogorov. An experimental comparison of min-cut/max-ﬂow algorithms for energy mini-

mization in vision. IEEE Trans. Pattern Anal. Mach. Intell., 26(9):1124–1137, 2004.

[50] Y. Boykov, O. Veksler, and R. Zabih. Fast approximate energy minimization via graph cuts. IEEE Trans.

Pattern Anal. Mach. Intell., 23:1222–1239, 2001.

[51] C. Bracegirdle and D. Barber. Switch-reset models : Exact and approximate inference. In Proceedings of The
Fourteenth International Conference on Artiﬁcial Intelligence and Statistics (AISTATS), volume 10, 2011.

[52] M. Brand.

Incremental singular value decomposition of uncertain data with missing values.

In European

Conference on Computer Vision (ECCV), pages 707–720, 2002.

[53] J. Breese and D. Heckerman. Decision-theoretic troubleshooting: A framework for repair and experiment.
In E. Horvitz and F. Jensen, editors, Uncertainty in Artiﬁcial Intelligence, number 12, pages 124–132, San
Francisco, CA, 1996. Morgan Kaufmann.

[54] H. Bunke and T. Caelli. Hidden Markov models: applications in computer vision. Machine Perception and

Artiﬁcial Intelligence. World Scientiﬁc Publishing Co., Inc., River Edge, NJ, USA, 2001.

[55] W. Buntine. Theory reﬁnement on Bayesian networks. In Uncertainty in Artiﬁcial Intelligence, number 7, pages

52–60, San Francisco, CA, 1991. Morgan Kaufmann.

[56] A. Cano and S. Moral. Advances in Intelligent Computing – IPMU 1994, chapter Heuristic Algorithms for the
Triangulation of Graphs, pages 98–107. Number 945 in Lectures Notes in Computer Sciences. Springer-Verlag,
1995.

[57] O. Capp´e, E. Moulines, and T. Ryden. Inference in Hidden Markov Models. Springer, New York, 2005.

[58] A. T. Cemgil. Bayesian Inference in Non-negative Matrix Factorisation Models. Technical Report CUED/F-

INFENG/TR.609, University of Cambridge, July 2008.

[59] A. T. Cemgil, B. Kappen, and D. Barber. A Generative Model for Music Transcription. IEEE Transactions on

Audio, Speech and Language Processing, 14(2):679–694, 2006.

[60] E. Challis and D. Barber. Concave Gaussian Variational Approximations for Inference in Large-Scale Bayesian
Linear Models. In Proceedings of The Fourteenth International Conference on Artiﬁcial Intelligence and Statistics
(AISTATS). JMLR, 2011.

[61] H. S. Chang, M. C. Fu, J. Hu, and S. I. Marcus. Simulation-based Algorithms for Markov Decision Processes.

Springer, 2007.

[62] S. Chiappa and D. Barber. Bayesian Linear Gaussian State Space Models for Biosignal Decomposition. Signal

Processing Letters, 14(4):267–270, 2007.

DRAFT April 24, 2015

631

BIBLIOGRAPHY

BIBLIOGRAPHY

[63] S. Chib and M. Dueker. Non-Markovian Regime Switching with Endogenous States and Time-Varying State
Strengths. Econometric Society 2004 North American Summer Meetings 600, Econometric Society, August 2004.

[64] C. K. Chow and C. N. Liu. Approximating discrete probability distributions with dependence trees. IEEE

Transactions on Information Theory, 14(3):462–467, 1968.

[65] P. S. Churchland and T. J. Sejnowski. The Computational Brain. MIT Press, Cambridge, MA, USA, 1994.

[66] D. Cohn and H. Chang. Learning to probabilistically identify authoritative documents. In P. Langley, editor,

International Conference on Machine Learning, number 17, pages 167–174. Morgan Kaufmann, 2000.

[67] D. Cohn and T. Hofmann. The Missing Link - A Probabilistic Model of Document Content and Hypertext

Connectivity. Number 13, pages 430–436, Cambridge, MA, 2001. MIT Press.

[68] A. C. C. Coolen, R. K¨uhn, and P. Sollich. Theory of Neural Information Processing Systems. Oxford University

Press, 2005.

[69] G. F. Cooper and E. Herskovits. A Bayesian Method for the Induction of Probabilistic Networks from Data.

Machine Learning, 9(4):309–347, 1992.

[70] A. Corduneanu and C. M. Bishop. Variational Bayesian Model Selection for Mixture Distributions. In T. Jaakkola

and T. Richardson, editors, Artifcial Intelligence and Statistics, pages 27–34. Morgan Kaufmann, 2001.

[71] M. T. Cover and J. A. Thomas. Elements of Information Theory. Wiley, 1991.

[72] R. G. Cowell, A. P. Dawid, S. L. Lauritzen, and D. J. Spiegelhalter. Probabilistic Networks and Expert Systems.

Springer, 1999.

[73] D. R. Cox and N. Wermuth. Multivariate Dependencies. Chapman and Hall, 1996.

[74] J. C. Cox, S. A. Ross, and M. Rubinstein. Option Pricing: A Simpliﬁed Approach. Journal of Financial

Economics, 7:229–263, 1979.

[75] N. Cristianini and J. Shawe-Taylor. An Introduction To Support Vector Machines. Cambridge University Press,

2000.

[76] P. Dangauthier, R. Herbrich, T. Minka, and T. Graepel. Trueskill through time: Revisiting the history of chess.
In B. Sch¨olkopf, J. Platt, and T. Hoﬀman, editors, Advances in Neural Information Processing Systems (NIPS),
number 19, pages 569–576, Cambridge, MA, 2007. MIT Press.

[77] H. A. David. The method of paired comparisons. Oxford University Press, New York, 1988.

[78] A. P. Dawid. Inﬂuence diagrams for causal modelling and inference. International Statistical Review, 70:161–189,

2002.

[79] A. P. Dawid and S. L. Lauritzen. Hyper Markov Laws in the Statistical Analysis of Decomposable Graphical

Models. Annals of Statistics, 21(3):1272–1317, 1993.

[80] P. Dayan and L.F. Abbott. Theoretical Neuroscience. MIT Press, 2001.

[81] P. Dayan and G. E. Hinton. Using Expectation-Maximization for Reinforcement Learning. Neural Computation,

9:271–278, 1997.

[82] T. De Bie, N. Cristianini, and R. Rosipal. Handbook of Geometric Computing : Applications in Pattern Recogni-
tion, Computer Vision, Neuralcomputing, and Robotics, chapter Eigenproblems in Pattern Recognition. Springer-
Verlag, 2005.

[83] R. Dechter. Bucket Elimination: A unifying framework for probabilistic inference algorithms. In E. Horvitz
and F. Jensen, editors, Uncertainty in Artiﬁcial Intelligence, pages 211–219, San Francisco, CA, 1996. Morgan
Kaufmann.

[84] A. P. Dempster, N. M. Laird, and D. B. Rubin. Maximum Likelihood from Incomplete Data via the EM

Algorithm. Journal of the Royal Statistical Society. Series B (Methodological), 39(1):1–38, 1977.

[85] S. Diederich and M. Opper. Learning of Correlated Patterns in Spin-Glass Networks by Local Learning Rules.

Physical Review Letters, 58(9):949–952, 1986.

[86] R. Diestel. Graph Theory. Springer, 2005.

632

DRAFT April 24, 2015

BIBLIOGRAPHY

BIBLIOGRAPHY

[87] A. Doucet and A. M. Johansen. A Tutorial on Particle Filtering and Smoothing: Fifteen years later. In D. Crisan

and B. Rozovsky, editors, Oxford Handbook of Nonlinear Filtering. Oxford University Press, 2009.

[88] R. O. Duda, P. E. Hart, and D. G. Stork. Pattern Classiﬁcation. Wiley-Interscience Publication, 2000.

[89] J. Durbin. The ﬁtting of time series models. Rev. Inst. Int. Stat., 28:233–243, 1960.

[90] R. Durbin, S. R. Eddy, A. Krogh, and G. Mitchison. Biological Sequence Analysis : Probabilistic Models of

Proteins and Nucleic Acids. Cambridge University Press, 1999.

[91] A. D¨uring, A. C. C. Coolen, and D. Sherrington. Phase diagram and storage capacity of sequence processing

neural networks. Journal of Physics A, 31:8607–8621, 1998.

[92] J. M. Gutierrez E. Castillo and A. S. Hadi. Expert Systems and Probabilistic Network Models. Springer Verlag,

1997.

[93] J. Edmonds and R. M. Karp. Theoretical improvements in algorithmic eﬃciency for network ﬂow problems.

Journal of the ACM, 19(2):248–264, 1972.

[94] R. Edwards and A. Sokal. Generalization of the fortium-kasteleyn-swendson-wang representation and monte

carlo algorithm. Physical Review D, 38:2009–2012, 1988.

[95] A. E. Elo. The rating of chess players, past and present. Arco, New York, second edition, 1986.

[96] R. F. Engel. GARCH 101: The Use of ARCH/GARCH Models in Applied Econometrics. Journal of Economic

Perspectives, 15(4):157–168, 2001.

[97] Y. Ephraim and W. J. J. Roberts. Revisiting autoregressive hidden Markov modeling of speech signals. IEEE

Signal Processing Letters, 12(2):166–169, February 2005.

[98] E. Erosheva, S. Fienberg, and J. Laﬀerty. Mixed membership models of scientiﬁc publications. In Proceedings

of the National Academy of Sciences, volume 101, pages 5220–5227, 2004.

[99] R-E. Fan, P-H. Chen, and C-J. Lin. Working Set Selection Using Second Order Information for Training Support

Vector Machines. Journal of Machine Learning Research, 6:1889–1918, 2005.

[100] P. Fearnhead. Exact and Eﬃcient Bayesian inference for multiple changepoint problems. Technical report,

Deptartment of Mathematics and Statistics, Lancaster University, 2003.

[101] G. H. Fischer and I. W. Molenaar. Rasch Models: Foundations, Recent Developments, and Applications. Springer,

New York, 1995.

[102] M. E. Fisher. Statistical Mechanics of Dimers on a Plane Lattice. Physical Review, 124:1664–1672, 1961.

[103] B. Frey. Extending Factor Graphs as to Unify Directed and Undirected Graphical Models. In C. Meek and
U. Kjærulﬀ, editors, Uncertainty in Artiﬁcial Intelligence, number 19, pages 257–264. Morgan Kaufmann, 2003.

[104] N. Friedman, D. Geiger, and M. Goldszmidt. Bayesian Network Classiﬁers. Machine Learning, 29:131–163,

1997.

[105] S. Fr¨uhwirth-Schnatter. Finite Mixture and Markov Switching Models. Springer, 2006.

[106] M. Frydenberg. The chain graph Markov property. Scandanavian Journal of Statistics, 17:333–353, 1990.

[107] T. Furmston and D. Barber. Solving deterministic policy (po)mpds using expectation-maximisation and an-
tifreeze. In First international workshop on learning and data mining for robotics (LEMIR), September 2009.
In conjunction with ECML/PKDD-2009.

[108] T. Furmston and D. Barber. Variational methods for Reinforcement Learning. In Teh. Y. W. and M. Titter-
ington, editors, Proceedings of The Thirteenth International Conference on Artiﬁcial Intelligence and Statistics
(AISTATS), volume 9, pages 241–248, Chia Laguna, Sardinia, Italy, May 13-15 2010. JMLR.

[109] T. Furmston and D. Barber. Eﬃcient Inference in Markov Control Problems.

In Uncertainty in Artiﬁcial

Intelligence, number 27, Corvallis, Oregon, USA, 2011.

[110] T. Furmston and D. Barber. Lagrange Dual Decomposition for Finite Horizon Markov Decision Processes. In

European Conference on Machine Learning (ECML), 2011.

DRAFT April 24, 2015

633

BIBLIOGRAPHY

BIBLIOGRAPHY

[111] A. Galka, O. Yamashita, T. Ozaki, R. Biscay, and P. Valdes-Sosa. A solution to the dynamical inverse problem

of EEG generation using spatiotemporal Kalman ﬁltering. NeuroImage, (23):435–453, 2004.

[112] P. Gandhi, F. Bromberg, and D. Margaritis. Learning markov network structure using few independence tests.

In Proceedings of the SIAM International Conference on Data Mining, pages 680–691, 2008.

[113] M. R. Garey and D. S. Johnson. Computers and Intractability, A Guide to the Theory of NP-Completeness.

W.H. Freeman and Company, New York, 1979.

[114] A. Gelb. Applied optimal estimation. MIT press, 1974.

[115] A. Gelman, G. O. Roberts, and W. R. Gilks. Eﬃcient Metropolis jumping rules. In J. O. Bernardo, J. M. Berger,
A. P. Dawid, and A. F. M. Smith, editors, Bayesian Statistics, volume 5, pages 599–607. Oxford University Press,
1996.

[116] S. Geman and D. Geman. Stochastic relaxation, Gibbs distributions, and the Bayesian restoration of images. In
Readings in uncertain reasoning, pages 452–472, San Francisco, CA, USA, 1990. Morgan Kaufmann Publishers
Inc.

[117] M. G. Genton. Classes of kernels for machine learning: A statistics perspective. Journal of Machine Learning

Research, 2:299–312, 2001.

[118] W. Gerstner and W. M. Kistler. Spiking Neuron Models. Cambridge University Press, 2002.

[119] Z. Ghahramani and M. J. Beal. Variational Inference for Bayesian Mixtures of Factor Analysers. In S. A. Solla,
T. K. Leen, and K-R. M¨uller, editors, Advances in Neural Information Processing Systems (NIPS), number 12,
pages 449–455, Cambridge, MA, 2000. MIT Press.

[120] Z. Ghahramani and G. E. Hinton. Variational learning for switching state-space models. Neural Computation,

12(4):963–996, 1998.

[121] A. Gibbons. Algorithmic Graph Theory. Cambridge University Press, 1991.

[122] M. Gibbs. Bayesian Gaussian processes for regression and classiﬁcation. PhD thesis, University of Cambridge,

1997.

[123] W. R. Gilks, S. Richardson, and D. J. Spiegelhalter. Markov chain Monte Carlo in practice. Chapman & Hall,

1996.

[124] M Girolami. A variational method for learning sparse and overcomplete representations. Neural Computation,

13:2517–2532, 2001.

[125] M. Girolami and A. Kaban. On an equivalence between PLSI and LDA. In Proceedings of the 26th annual
international ACM SIGIR conference on Research and development in information retrieval, pages 433–434,
New York, NY, USA, 2003. ACM Press.

[126] M. E. Glickman. Parameter estimation in large dynamic paired comparison experiments. Applied Statistics,

48:377–394, 1999.

[127] A. Globerson and T. Jaakkola. Approximate inference using planar graph decomposition.

In B. Sch¨olkopf,
J. Platt, and T. Hoﬀman, editors, Advances in Neural Information Processing Systems (NIPS), number 19,
pages 473–480, Cambridge, MA, 2007. MIT Press.

[128] D. Goldberg, D. Nichols, B. M. Oki, and D. Terry. Using collaborative ﬁltering to weave an information tapestry.

Communications ACM, 35:61–70, 1992.

[129] G. H. Golub and C. F. van Loan. Matrix Computations. Johns Hopkins University Press, 3rd edition, 1996.

[130] M. C. Golumbic and I. Ben-Arroyo Hartman. Graph Theory, Combinatorics, and Algorithms. Springer-Verlag,

2005.

[131] C. Goutis. A graphical method for solving a decision analysis problem. IEEE Transactions on Systems, Man

and Cybernetics, 25:1181–1193, 1995.

[132] P. J. Green and B. W. Silverman. Nonparametric Regression and Generalized Linear Models, volume 58 of

Monographs on Statistics and Applied Probability. Chapman and Hall, 1994.

[133] D. M. Greig, B. T. Porteous, and A. H. Seheult. Exact maximum a posteriori estimation for binary images.

Journal of the Royal Statistical Society, Series B, 2:271–279, 1989.

634

DRAFT April 24, 2015

BIBLIOGRAPHY

BIBLIOGRAPHY

[134] G. Grimmett and D. Stirzaker. Probability and Random Processes. Oxford University Press, second edition,

1992.

[135] S. F. Gull. Bayesian data analysis: straight-line ﬁtting. In J. Skilling, editor, Maximum entropy and Bayesian

methods (Cambridge 1988), pages 511–518. Kluwer, 1989.

[136] A. K. Gupta and D. K. Nagar. Matrix Variate Distributions. Chapman and Hall/CRC, Boca Raton, Florida

USA, 1999.

[137] D. J. Hand and K. Yu. Idiot’s Bayes—Not So Stupid After All? International Statistical Review, 69(3):385–398,

2001.

[138] D. R. Hardoon, S. Szedmak, and J. Shawe-Taylor. Canonical Correlation Analysis: An Overview with Applica-

tion to Learning Methods. Neural Computation, 16(12):2639–2664, 2004.

[139] D. O. Hebb. The organization of behavior. Wiley, New York, 1949.

[140] D. Heckerman. A Tutorial on Learning With Bayesian Networks. Technical Report MSR-TR-95-06, Microsoft

Research, Redmond, WA, March 1996. Revised November 1996.

[141] D. Heckerman, D. Geiger, and D. Chickering. Learning Bayesian Networks: The Combination of Knowledge

and Statistical Data. Machine Learning, 20(3):197–243, 1995.

[142] R. Herbrich, T. Minka, and T. Graepel. TrueSkillTM: A Bayesian Skill Rating System. In B. Sch¨olkopf, J. Platt,
and T. Hoﬀman, editors, Advances in Neural Information Processing Systems (NIPS), number 19, pages 569–576,
Cambridge, MA, 2007. MIT Press.

[143] H. Hermansky. Should recognizers have ears? Speech Communication, 25:3–27, 1998.

[144] J. Hertz, A. Krogh, and R. Palmer. Introduction to the theory of Neural Computation. Addison-Wesley, 1991.

[145] T. Heskes. Convexity arguments for eﬃcient minimization of the Bethe and Kikuchi free energies. Journal of

Artiﬁcial Intelligence Research, 26:153–190, 2006.

[146] D. M. Higdon. Auxiliary variable methods for Markov chain Monte Carlo with applications. Journal of the

American Statistical Association, 93(442):585–595, 1998.

[147] G. E. Hinton and R. R. Salakhutdinov. Reducing the dimensionality of data with neural networks. Science,

(313):504–507, 2006.

[148] T. Hofmann, J. Puzicha, and M. I. Jordan. Learning from dyadic data. In M. S. Kearns, S. A. Solla, and D. A.
Cohn, editors, Advances in Neural Information Processing Systems (NIPS), pages 466–472, Cambridge, MA,
1999. MIT Press.

[149] R. A. Howard and J. E. Matheson. Inﬂuence diagrams. Decision Analysis, 2(3), 2005. Republished version of

the original 1981 report.

[150] J. C. Hull. Options, Futures, and Other Derivatives. Prentice Hall, 1997.

[151] A. Hyv¨arinen, J. Karhunen, and E. Oja. Independent Component Analysis. Wiley, 2001.

[152] Aapo Hyv¨arinen. Consistency of Pseudolikelihood Estimation of Fully Visible Boltzmann Machines. Neural

Computation, 18(10):2283–2292, 2006.

[153] M. Isard and A. Blake. CONDENSATION Conditional Density Propagation for Visual Tracking. International

Journal of Computer Vision, 29:5–28, 1998.

[154] T. S. Jaakkola and M. I. Jordan. Variational probabilistic inference and the qmr-dt network. Journal of Artiﬁcial

Intelligence Research, 10:291–322, 1999.

[155] T. S. Jaakkola and M. I. Jordan. Bayesian parameter estimation via variational methods. Statistics and

Computing, 10(1):25–37, 2000.

[156] R. A. Jacobs, F. Peng, and M. A. Tanner. A Bayesian approach to model selection in hierarchical mixtures-of-

experts architectures. Neural Networks, 10(2):231–241, 1997.

[157] R. G. Jarrett. A note on the intervals between coal-mining disasters. Biometrika, (66):191–193, 1979.

[158] E. T. Jaynes. Probability Theory : The Logic of Science. Cambridge University Press, 2003.

DRAFT April 24, 2015

635

BIBLIOGRAPHY

BIBLIOGRAPHY

[159] F. Jensen, F. V. Jensen, and D. Dittmer. From Inﬂuence Diagrams to Junction Trees. In Proceedings of the
10th Annual Conference on Uncertainty in Artiﬁcial Intelligence (UAI-94), pages 367–373, San Francisco, CA,
1994. Morgan Kaufmann.

[160] F. V. Jensen and F. Jensen. Optimal Junction Trees. In R. Lopez de Mantaras and D. Poole, editors, Uncertainty

in Artiﬁcial Intelligence, number 10, pages 360–366, San Francisco, CA, 1994. Morgan Kaufmann.

[161] F. V. Jensen and T. D. Nielson. Bayesian Networks and Decision Graphs. Springer Verlag, second edition, 2007.

[162] M. I. Jordan and R. A. Jacobs. Hierarchical mixtures of experts and the EM algorithm. Neural Computation,

6:181–214, 1994.

[163] B. H. Juang, W. Chou, and C. H. Lee. Minimum classiﬁcation error rate methods for speech recognition. IEEE

Transactions on Speech and Audio Processing, 5:257–265, 1997.

[164] L. P. Kaelbling, M. L. Littman, and A. R. Cassandra. Planning and acting in partially observable stochastic

domains. Artiﬁcial Intelligence, 101(1-2):99–134, 1998.

[165] H. J. Kappen. An introduction to stochastic control theory, path integrals and reinforcement learning.

In
Proceedings 9th Granada seminar on Computational Physics: Computational and Mathematical Modeling of
Cooperative Behavior in Neural Systems, volume 887, pages 149–181. American Institute of Physics, 2007.

[166] H. J. Kappen and F. B. Rodr´ıguez. Eﬃcient learning in Boltzmann machines using linear response theory.

Neural Compution, 10(5):1137–1156, 1998.

[167] H. J. Kappen and W. Wiegerinck. Novel iteration schemes for the Cluster Variation Method. In T. G. Dietterich,
S. Becker, and Z. Ghahramani, editors, Advances in Neural Information Processing Systems (NIPS), number 14,
pages 415–422, Cambridge, MA, 2002. MIT Press.

[168] Y. Karklin and M. S. Lewicki. Emergence of complex cell properties by learning to generalize in natural scenes.

Nature, (457):83–86, November 2008.

[169] G. Karypis and V. Kumar. A fast and high quality multilevel scheme for partitioning irregular graphs. Siam

Journal on Scientiﬁc Computing, 20(1):359–392, 1998.

[170] P. W. Kasteleyn. Dimer Statistics and Phase Transitions. Journal of Mathematical Physics, 4(2):287–293, 1963.

[171] S. A. Kauﬀman. At Home in the Universe: The Search for Laws of Self-Organization and Complexity. Oxford

University Press, Oxford, UK, 1995.

[172] C-J. Kim. Dynamic linear models with Markov-switching. Journal of Econometrics, 60:1–22, 1994.

[173] C-J. Kim and C. R. Nelson. State-Space models with regime switching. MIT Press, 1999.

[174] G. Kitagawa. The Two-Filter Formula for Smoothing and an implementation of the Gaussian-sum smoother.

Annals of the Institute of Statistical Mathematics, 46(4):605–623, 1994.

[175] U. B. Kjaerulﬀ and A. L. Madsen. Bayesian Networks and Inﬂuence Diagrams : A Guide to Construction and

Analysis. Springer, 2008.

[176] N. Komodakis, N. Paragios, and G. Tziritas. MRF Optimization via Dual Decomposition: Message-Passing

Revisited. In IEEE 11th International Conference on Computer Vision, ICCV, pages 1–8, 2007.

[177] A. Krogh, M. Brown, I. Mian, K. Sjolander, and D. Haussler. Hidden Markov models in computational biology:

Applications to protein modeling. Journal of Molecular Biology, 235:1501–1531, 1994.

[178] S. Kullback. Information Theory and Statistics. Dover, 1968.

[179] K. Kurihara, M. Welling, and Y. W. Teh. Collapsed Variational Dirichlet Process Mixture Models. In Proceedings

of the International Joint Conference on Artiﬁcial Intelligence, volume 20, pages 2796–2801, 2007.

[180] J. Laﬀerty, A. McCallum, and F. Pereira. Conditional random ﬁelds: Probabilistic models for segmenting and
In C. E. Brodley and A. P. Danyluk, editors, International Conference on Machine

labeling sequence data.
Learning, number 18, pages 282–289, San Francisco, CA, 2001. Morgan Kaufmann.

[181] H. Lass. Elements of Pure and Applied Mathematics. McGraw-Hill (reprinted by Dover), 1957.

[182] S. L. Lauritzen. Graphical Models. Oxford University Press, 1996.

636

DRAFT April 24, 2015

BIBLIOGRAPHY

BIBLIOGRAPHY

[183] S. L. Lauritzen, A. P. Dawid, B. N. Larsen, and H-G. Leimer. Independence properties of directed Markov ﬁelds.

Networks, 20:491–505, 1990.

[184] S. L. Lauritzen and D. J. Spiegelhalter. Local computations with probabilities on graphical structures and their

application to expert systems. Journal of Royal Statistical Society B, 50(2):157 – 224, 1988.

[185] D. D. Lee and H. S. Seung. Algorithms for non-negative matrix factorization. In T. K. Leen, T. G. Dietterich,
and V. Tresp, editors, Advances in Neural Information Processing Systems (NIPS), number 13, pages 556–562,
Cambridge, MA, 2001. MIT Press.

[186] M. A. R. Leisink and H. J. Kappen. A Tighter Bound for Graphical Models. In Neural Computation, volume 13,

pages 2149–2171. MIT Press, 2001.

[187] V. Lepar and P. P. Shenoy. A Comparison of Lauritzen-Spiegelhalter, Hugin, and Shenoy-Shafer Architectures
In G. Cooper and S. Moral, editors, Uncertainty in

for Computing Marginals of Probability Distributions.
Artiﬁcial Intelligence, number 14, pages 328–333, San Francisco, CA, 1998. Morgan Kaufmann.

[188] U. Lerner, R. Parr, D. Koller, and G. Biswas. Bayesian Fault Detection and Diagnosis in Dynamic Systems. In

Proceedings of the Seventeenth National Conference on Artiﬁcial Intelligence (AIII-00), pages 531–537, 2000.

[189] U. N. Lerner. Hybrid Bayesian Networks for Reasoning about Complex Systems. Computer science department,

Stanford University, 2002.

[190] R. Linsker. Improved local learning rule for information maximization and related applications. Neural Networks,

18(3):261–265, 2005.

[191] Y. L. Loh, E. W. Carlson, and M. Y. J. Tan. Bond-propagation algorithm for thermodynamic functions in

general two-dimensional Ising models. Physical Review B, 76(1):014404, 2007.

[192] H. Lopes and M. West. Bayesian model assessment in factor analysis. Statistica Sinica, (14):41–67, 2003.

[193] T. J. Loredo. From Laplace To Supernova Sn 1987A: Bayesian Inference In Astrophysics.

In P.F. Fougere,

editor, Maximum Entropy and Bayesian Methods, pages 81–142. Kluwer, 1990.

[194] D. J. C. MacKay. Bayesian interpolation. Neural Computation, 4(3):415–447, 1992.

[195] D. J. C. MacKay. Probable Networks and plausible predictions – a review of practical Bayesian methods for

supervised neural networks. Network: Computation in Neural Systems, 6(3):469–505, 1995.

[196] D. J. C. MacKay. Introduction to Gaussian Processes. In Neural Networks and Machine Learning, volume 168
of NATO advanced study institute on generalization in neural networks and machine learning, pages 133–165.
Springer, August 1998.

[197] D. J. C. MacKay. Information Theory, Inference and Learning Algorithms. Cambridge University Press, 2003.

[198] U. Madhow. Fundamentals of Digital Communication. Cambridge University Press, 2008.

[199] K. V. Mardia, J. T. Kent, and J. M. Bibby. Multivariate Analysis. Academic Press, 1997.

[200] H. Markram, J. Lubke, M. Frotscher, and B. Sakmann. Regulation of synaptic eﬃcacy by coincidence of

postsynaptic APs and EPSPs. Science, 275:213–215, 1997.

[201] A. McCallum, K. Nigam, J. Rennie, and K. Seymore. Automating the construction of internet portals with

machine learning. Information Retrieval Journal, 3:127–163, 2000.

[202] G. McLachlan and T. Krishnan. The EM Algorithm and Extensions. John Wiley and Sons, 1997.

[203] G. McLachlan and D. Peel. Finite Mixture Models. Wiley Series in Probability and Statistics. Wiley-Interscience,

2000.

[204] E. Meeds, Z. Ghahramani, R. M. Neal, and S. T. Roweis. Modeling Dyadic Data with Binary Latent Factors.
In B. Sch¨olkopf, J. Platt, and T. Hoﬀman, editors, Advances in Neural Information Processing Systems (NIPS),
volume 19, pages 977–984, Cambridge, MA, 2007. MIT Press.

[205] M. Meila. An Accelerated Chow and Liu Algorithm: Fitting Tree Distributions to High-Dimensional Sparse
Data. In I. Bratko, editor, International Conference on Machine Learning, pages 249–257, San Francisco, CA,
1999. Morgan Kaufmann.

DRAFT April 24, 2015

637

BIBLIOGRAPHY

BIBLIOGRAPHY

[206] M. Meila and M. I. Jordan. Triangulation by continuous embedding.

In M. C. Mozer, M. I. Jordan, and
T. Petsche, editors, Advances in Neural Information Processing Systems (NIPS), number 9, pages 557–563,
Cambridge, MA, 1997. MIT Press.

[207] B. Mesot and D. Barber. Switching Linear Dynamical Systems for Noise Robust Speech Recognition. IEEE

Transactions of Audio, Speech and Language Processing, 15(6):1850–1858, 2007.

[208] N. Meuleau, M. Hauskrecht, K-E. Kim, L. Peshkin, Kaelbling. L. P., T. Dean, and C. Boutilier. Solving Very
In Proceedings of the Fifteenth National Conference on

Large Weakly Coupled Markov Decision Processes.
Artiﬁcial Intelligence, pages 165–172, 1998.

[209] T. Mills. The Econometric Modelling of Financial Time Series. Cambridge University Press, 2000.

[210] T. Minka. Expectation Propagation for approximate Bayesian inference. In J. Breese and D. Koller, editors,
Uncertainty in Artiﬁcial Intelligence, number 17, pages 362–369, San Francisco, CA, 2001. Morgan Kaufmann.

[211] T. Minka. A comparison of numerical optimizers for logistic regression. Technical report, Microsoft Research,

2003. research.microsoft.com/

minka/papers/logreg.

∼

[212] T. Minka. Divergence measures and message passing. Technical Report MSR-TR-2005-173, Microsoft Research

Ltd., Cambridge, UK, December 2005.

[213] A. Mira, J. Møller, and G. O. Roberts. Perfect slice samplers. Journal of the Royal Statistical Society, 63(3):593–

606, 2001. Series B (Statistical Methodology).

[214] C. Mitchell, M. Harper, and L. Jamieson. On the complexity of explicit duration HMM’s. Speech and Audio

Processing, IEEE Transactions on, 3(3):213–217, May 1995.

[215] T. Mitchell. Machine Learning. McGraw-Hill, 1997.

[216] J. Mooij and H. J. Kappen. Suﬃcient conditions for convergence of Loopy Belief Propagation. IEEE Information

Theory, 53:4422–4437, 2007.

[217] J. W. Moon and L. Moser. On cliques in graphs. Israel Journal of Mathematics, (3):23–28, 1965.

[218] A. Moore.

A tutorial

on

kd-trees.

Technical

report,

1991.

Available

from

http://www.cs.cmu.edu/

awm/papers.html.

∼

[219] J. Moussouris. Gibbs and Markov Random Systems with Constraints. Journal of Statistical Physics, 10:11–33,

1974.

[220] R. M. Neal. Probabilistic inference using Markov Chain Monte Carlo methods. CRG-TR-93-1, Dept. of Computer

Science, University of Toronto, 1993.

[221] R. M. Neal. Markov Chain Sampling Methods for Dirichlet Process Mixture Models. Journal of Computational

and Graphical Statistics, 9(2):249–265, 2000.

[222] R. M. Neal. Slice sampling. Annals of Statistics, 31:705–767, 2003.

[223] R. E. Neapolitan. Learning Bayesian Networks. Prentice Hall, 2003.

[224] A. V. Neﬁan, L. Luhong, P. Xiaobo, X. Liu, C. Mao, and K. Murphy. A coupled HMM for audio-visual speech
recognition. In IEEE International Conference on Acoustics, Speech, and Signal Processing, volume 2, pages
2013–2016, 2002.

[225] H. Nickisch and M. Seeger. Convex Variational Bayesian Inference for Large Scale Generalized Linear Models.

International Conference on Machine Learning, 26:761–768, 2009.

[226] D. Nilsson. An eﬃcient algorithm for ﬁnding the m most probable conﬁgurations in a probabilistic expert

system. Statistics and Computing, 8:159–173, 1998.

[227] D. Nilsson and J. Goldberger. Sequentially ﬁnnding the N -best list in Hidden Markov Models. Internation Joint

Conference on Artiﬁcial Intelligence (IJCAI), 17, 2001.

[228] A. B. Novikoﬀ. On convergence proofs on perceptrons. In Symposium on the Mathematical Theory of Automata
(New York, 1962), volume 12, pages 615–622, Brooklyn, N.Y., 1963. Polytechnic Press of Polytechnic Institute
of Brooklyn.

638

DRAFT April 24, 2015

BIBLIOGRAPHY

BIBLIOGRAPHY

[229] F. J. Och and H. Ney. Discriminative training and maximum entropy models for statistical machine transla-
tion. In Proceedings of the Annual Meeting of the Association for Computational Linguistics, pages 295–302,
Philadelphia, July 2002.

[230] B. A. Olshausen and D. J. Field. Sparse coding with an overcomplete basis set: A strategy employed by V1?

Vision Research, 37:3311–3325, 1998.

[231] A. V. Oppenheim, R. W. Shafer, M. T. Yoder, and W. T. Padgett. Discrete-Time Signal Processing. Prentice

Hall, third edition, 2009.

[232] M. Ostendorf, V. Digalakis, and O. A. Kimball. From HMMs to Segment Models: A Uniﬁed View of Stochastic
Modeling for Speech Recognition. IEEE Transactions on Speech and Audio Processing, 4:360–378, 1995.

[233] P. Paatero and U. Tapper. Positive matrix factorization: A non-negative factor model with optimal utilization

of error estimates of data values. Environmetrics, 5:111–126, 1994.

[234] A. Palmer, D. Wipf, K. Kreutz-Delgado, and B. Rao. Variational EM algorithms for non-Gaussian latent variable
models. In B. Sch¨olkopf, J. Platt, and T. Hoﬀman, editors, Advances in Neural Information Processing Systems
(NIPS), number 19, pages 1059–1066, Cambridge, MA, 2006. MIT Press.

[235] V. Pavlovic, J. M. Rehg, and J. MacCormick. Learning switching linear models of human motion. In T. K. Leen,
T. G. Dietterich, and V. Tresp, editors, Advances in Neural Information Processing Systems (NIPS), number 13,
pages 981–987, Cambridge, MA, 2001. MIT Press.

[236] J. Pearl. Probabilistic Reasoning in Intelligent Systems : Networks of Plausible Inference. Morgan Kaufmann,

1988.

[237] J. Pearl. Causality: Models, Reasoning and Inference. Cambridge University Press, 2000.

[238] B. A. Pearlmutter and L. C. Parra. Maximum Likelihood Blind Source Separation: A Context-Sensitive Gen-
eralization of ICA. In M. C. Mozer, M. I. Jordan, and T. Petsche, editors, Advances in Neural Information
Processing Systems (NIPS), number 9, pages 613–619, Cambridge, MA, 1997. MIT Press.

[239] K. B. Petersen and O. Winther. The EM algorithm in independent component analysis. In IEEE International

Conference on Acoustics, Speech, and Signal Processing, volume 5, pages 169–172, 2005.

[240] J-P. Pﬁster, T. Toyiozumi, D. Barber, and W. Gerstner. Optimal Spike-Timing Dependent Plasticity for Precise

Action Potential Firing in Supervised learning. Neural Computation, 18:1309–1339, 2006.

[241] J. Platt. Fast Training of Support Vector Machines Using Sequential Minimal Optimization. In B. Sch¨olkopf,
C. J. C. Burges, and A. J. Smola, editors, Advances in Kernel Methods - Support Vector Learning, pages 185–208.
MIT Press, 1999.

[242] I. Porteous, D. Newman, A. Ihler, A. Asuncion, P. Smyth, and M. Welling. Fast collapsed Gibbs sampling for
Latent Dirichlet Allocation. In KDD ’08: Proceeding of the 14th ACM SIGKDD international conference on
Knowledge discovery and data mining, pages 569–577, New York, NY, USA, 2008. ACM.

[243] J. E. Potter and R. G. Stern. Statistical ﬁltering of space navigation measurements. In American Institute of
Aeronautics and Astronautics Guidance and Control Conference, volume 13, pages 775–801, Cambridge, Mass.,
August 1963.

[244] W. Press, W. Vettering, S. Teukolsky, and B. Flannery. Numerical Recipes in Fortran. Cambridge University

Press, 1992.

[245] S. J. D. Prince and J. H. Elder. Probabilistic Linear Discriminant Analysis for Inferences About Identity. In

IEEE 11th International Conference on Computer Vision ICCV, pages 1–8, 2007.

[246] L. R. Rabiner. A tutorial on hidden Markov models and selected applications in speech recognition. Proc. of

the IEEE, 77(2):257–286, 1989.

[247] C. E. Rasmussen and C. K. I. Williams. Gaussian Processes for Machine Learning. MIT Press, 2006.

[248] H. E. Rauch, G. Tung, and C. T. Striebel. Maximum Likelihood estimates of linear dynamic systems. American

Institute of Aeronautics and Astronautics Journal (AIAAJ), 3(8):1445–1450, 1965.

[249] T. Richardson and P. Spirtes. Ancestral Graph Markov Models. Annals of Statistics, 30(4):962–1030, 2002.

[250] D. Rose, R. E. Tarjan, and E. S. Lueker. Algorithmic aspects of vertex elimination of graphs. SIAM Journal on

Computing, (5):266–283, 1976.

DRAFT April 24, 2015

639

BIBLIOGRAPHY

BIBLIOGRAPHY

[251] F. Rosenblatt. The Perceptron: A Probabilistic Model for Information Storage and Organization in the Brain.

Psychological Review, 65(6):386–408, 1958.

[252] S. T. Roweis and L. J. Saul. Nonlinear Dimensionality Reduction by Locally Linear Embedding. Science,

290(5500):2323–2326, 2000.

[253] D. B. Rubin. Using the SIR algorithm to simulate posterior distributions. In M. H. Bernardo, K. M. Degroot,

D. V. Lindley, and A. F. M. Smith, editors, Bayesian Statistics 3. Oxford University Press, 1988.

[254] D. Saad and M. Opper. Advanced Mean Field Methods Theory and Practice. MIT Press, 2001.

[255] R. Salakhutdinov, S. Roweis, and Z. Ghahramani. Optimization with EM and Expectation-Conjugate-Gradient.
In T. Fawcett and N. Mishra, editors, International Conference on Machine Learning, number 20, pages 672–679,
Menlo Park, CA, 2003. AAAI Press.

[256] L. K. Saul and M. I. Jordan. Exploiting tractable substructures in intractable networks. In D. S. Touretzky,
M. Mozer, and M. E. Hasselmo, editors, Advances in Neural Information Processing Systems (NIPS), number 8,
pages 486–492, Cambridge, MA, 1996. MIT Press.

[257] L. Savage. The Foundations of Statistics. Wiley, 1954.

[258] R. D. Schachter. Bayes-ball: The rational pastime (for determining irrelevance and requisite information in belief
networks and inﬂuence diagrams). In G. Cooper and S. Moral, editors, Uncertainty in Artiﬁcial Intelligence,
number 14, pages 480–487, San Francisco, CA, 1998. Morgan Kaufmann.

[259] B. Sch¨olkopf, A. Smola, and K. R. M¨uller. Nonlinear Component Analysis as a Kernel Eigenvalue Problem.

Neural Computation, 10:1299–1319, 1998.

[260] N. N. Schraudolph and D. Kamenetsky. Eﬃcient Exact Inference in Planar Ising Models. In D. Koller, D. Schuur-
mans, Y. Bengio, and L. Bottou, editors, Advances in Neural Information Processing Systems (NIPS), number 21,
pages 1417–1424, Cambridge, MA, 2009. MIT Press.

[261] E. Schwarz. Estimating the dimension of a model. Annals of Statistics, 6(2):461–464, 1978.

[262] M. Seeger. Gaussian Processes for Machine Learning. International Journal of Neural Systems, 14(2):69–106,

2004.

[263] M. Seeger. Expectation propagation for exponential families. Technical report, Department of EECS, Berkeley,

2005. www.kyb.tuebingen.mpg.de/bs/people/seeger.

[264] J. Shawe-Taylor and N. Cristianini. Kernel Methods for Pattern Analysis. Cambridge University Press, 2004.

[265] S. Siddiqi, B. Boots, and G. Gordon. A Constraint Generation Approach to Learning Stable Linear Dynamical
Systems. In J. C. Platt, D. Koller, Y. Singer, and S. Roweis, editors, Advances in Neural Information Processing
Systems (NIPS), number 20, pages 1329–1336, Cambridge, MA, 2008. MIT Press.

[266] T. Silander, P. Kontkanen, and P. Myllym¨aki. On Sensitivity of the MAP Bayesian Network Structure to the
Equivalent Sample Size Parameter. In R. Parr and L. van der Gaag, editors, Uncertainty in Artiﬁcial Intelligence,
number 23, pages 360–367, Corvallis, Oregon, USA, 2007. AUAI press.

[267] S. S. Skiena. The algorithm design manual. Springer-Verlag, New York, USA, 1998.

[268] E. Smith and M. S. Lewicki. Eﬃcient auditory coding. Nature, 439(7079):978–982, 2006.

[269] P. Smolensky. Parallel Distributed Processing: Volume 1: Foundations, chapter Information processing in
dynamical systems: Foundations of harmony theory, pages 194–281. MIT Press, Cambridge, MA, 1986.

[270] G. Sneddon. Studies in the atmospheric sciences, chapter A Statistical Perspective on Data Assimilation in

Numerical Models. Number 144 in Lecture Notes in Statistics. Springer-Verlag, 2000.

[271] P. Sollich. Bayesian Methods for Support Vector Machines: Evidence and Predictive Class Probabilities. Machine

Learning, 46(1-3):21–52, 2002.

[272] D. X. Song, D. Wagner, and X. Tian. Timing Analysis of Keystrokes and Timing Attacks on SSH. In Proceedings

of the 10th conference on USENIX Security Symposium. USENIX Association, 2001.

[273] A. S. Spanias. Speech coding: a tutorial review. Proceedings of the IEEE, 82(10):1541–1582, Oct 1994.

[274] P. Spirtes, C. Glymour, and R. Scheines. Causation, Prediction, and Search. MIT press, 2 edition, 2000.

640

DRAFT April 24, 2015

BIBLIOGRAPHY

BIBLIOGRAPHY

[275] N. Srebro. Maximum Likelihood Bounded Tree-Width Markov Networks. In J. Breese and D. Koller, editors,
Uncertainty in Artiﬁcial Intelligence, number 17, pages 504–511, San Francisco, CA, 2001. Morgan Kaufmann.

[276] H. Steck. Constraint-Based Structural Learning in Bayesian Networks using Finite Data Sets. PhD thesis,

Technical University Munich, 2001.

[277] H. Steck. Learning the Bayesian Network Structure: Dirichlet Prior vs Data. In D. A. McAllester and P. Myl-
lymaki, editors, Uncertainty in Artiﬁcial Intelligence, number 24, pages 511–518, Corvallis, Oregon, USA, 2008.
AUAI press.

[278] H. Steck and T. Jaakkola. On the Dirichlet prior and Bayesian regularization. In S. Becker, S. Thrun, and

K. Obermayer, editors, NIPS, pages 697–704. MIT Press, 2002.

[279] G. Strang. Linear Algebra and Its Applications. Brooks Cole, 1988.

[280] M. Studen´y. On mathematical description of probabilistic conditional independence structures. PhD thesis,

Academy of Sciences of the Czech Republic, 2001.

[281] M. Studen´y. On non-graphical description of models of conditional independence structure. In HSSS Workshop

on Stochastic Systems for Individual Behaviours. Louvain la Neueve, Belgium, 22-23 January 2001.

[282] C. Sutton and A. McCallum. An introduction to conditional random ﬁelds for relational learning. In L. Getoor

and B. Taskar, editors, Introduction to Statistical Relational Learning. MIT press, 2006.

[283] R. S. Sutton and A. G. Barto. Reinforcement Learning: An Introduction. MIT Press, 1998.

[284] R. J. Swendsen and J-S. Wang. Nonuniversal critical dynamics in Monte Carlo simulations. Physical Review

Letters, 58:86–88, 1987.

[285] B. K. Sy. A Recurrence Local Computation Approach Towards Ordering Composite Beliefs in Bayesian Belief

Networks. International Journal of Approximate Reasoning, 8:17–50, 1993.

[286] T. Sejnowski. The Book of Hebb. Neuron, 24:773–776, 1999.

[287] R. E. Tarjan and M. Yannakakis. Simple linear-time algorithms to test chordality of graphs, test acyclicity of

hypergraphs, and selectively reduce acyclic hypergraphs. SIAM Journal on Computing, 13(3):566–579, 1984.

[288] S. J. Taylor. Modelling Financial Time Series. World Scientiﬁc, second edition, 2008.

[289] Y. W. Teh, D. Newman, and M. Welling. A Collapsed Variational Bayesian Inference Algorithm for Latent
Dirichlet Allocation. In J. C. Platt, D. Koller, Y. Singer, and S. Roweis, editors, Advances in Neural Information
Processing Systems (NIPS), number 20, pages 1481–1488, Cambridge, MA, 2008. MIT Press.

[290] Y. W. Teh and M. Welling. The uniﬁed propagation and scaling algorithm. In T. G. Dietterich, S. Becker,
and Z. Ghahramani, editors, Advances in Neural Information Processing Systems (NIPS), number 14, pages
953–960, Cambridge, MA, 2002. MIT Press.

[291] R. Tibshirani. Regression shrinkage and selection via the lasso. Journal of the Royal Statistical Society (B),

58:267–288, 1996.

[292] H. Tijms. Understanding Probability. Cambridge University Press, 2003.

[293] M. Tipping and C. M. Bishop. Mixtures of probabilistic principal component analysers. Neural Computation,

11(2):443–482, 1999.

[294] M. E. Tipping. Sparse Bayesian Learning and the Relevance Vector Machine. Journal of Machine Learning

Research, (1):211–244, 2001.

[295] M. E. Tipping. Sparse Bayesian learning and the relevance vector machine. Journal of Machine Learning

Research, 1:211–244, 2001.

[296] M. E. Tipping and C. M. Bishop. Probabilistic principal component analysis. Journal of the Royal Statistical

Society, Series B, 61(3):611–622, 1999.

[297] D. M. Titterington, A. F. M. Smith, and U. E. Makov. Statistical analysis of ﬁnite mixture distributions. Wiley,

1985.

[298] M. Toussaint, S. Harmeling, and A. Storkey. Probabilistic inference for solving (PO)MDPs. Research Report

EDI-INF-RR-0934, University of Edinburgh, School of Informatics, 2006.

DRAFT April 24, 2015

641

BIBLIOGRAPHY

BIBLIOGRAPHY

[299] M. Tsodyks, K. Pawelzik, and H. Markram. Neural Networks with Dynamic Synapses. Neural Computation,

10:821–835, 1998.

[300] L. van der Matten and G. Hinton. Visualizing Data using t-SNE. Journal of Machine Learning Research,

9:2579–2605, 2008.

[301] P. Van Overschee and B. De Moor. Subspace Identiﬁcation for Linear Systems; Theory, Implementations,

Applications. Kluwer, 1996.

[302] V. Vapnik. The Nature of Statistical Learning Theory. Springer, New York, 1995.

[303] M. Verhaegen and P. Van Dooren. Numerical Aspects of Diﬀerent Kalman Filter Implementations.

IEEE

Transactions of Automatic Control, 31(10):907–917, 1986.

[304] T. Verma and J. Pearl. Causal networks : Semantics and expressiveness. In R. D. Schacter, T. S. Levitt, L. N.
Kanal, and J.F. Lemmer, editors, Uncertainty in Artiﬁcial Intelligence, volume 4, pages 69–76, Amsterdam,
1990. North-Holland.

[305] T. O. Virtanen, A. T. Cemgil, and S. J. Godsill. Bayesian extensions to nonnegative matrix factorisation for
audio signal modelling. In IEEE International Conference on Acoustics, Speech, and Signal Processing, pages
1825–1828, 2008.

[306] G. Wahba. Support Vector Machines, Repreducing Kernel Hilbert Spaces, and Randomized GACV, pages 69–88.

MIT Press, 1999.

[307] M. J. Wainwright and M. I. Jordan. Graphical models, exponential families, and variational inference. Founda-

tions and Trends in Machine Learning, 1(1-2):1–305, 2008.

[308] H. Wallach. Eﬃcient training of conditional random ﬁelds. Master’s thesis, Division of Informatics, University

of Edinburgh, 2002.

[309] Y. Wang, J. Hodges, and B. Tang. Classiﬁcation of Web Documents Using a Naive Bayes Method. 15th IEEE

International Conference on Tools with Artiﬁcial Intelligence, pages 560–564, 2003.

[310] S. Waterhouse, D. Mackay, and T. Robinson. Bayesian methods for mixtures of experts. In D. S. Touretzky,
M. Mozer, and M. E. Hasselmo, editors, Advances in Neural Information Processing Systems (NIPS), number 8,
pages 351–357, Cambridge, MA, 1996. MIT Press.

[311] C. Watkins and P. Dayan. Q-learning. Machine Learning, 8:279–292, 1992.

[312] Y. Weiss and W. T. Freeman. Correctness of Belief Propagation in Gaussian Graphical Models of Arbitrary

Topology. Neural Computation, 13(10):2173–2200, 2001.

[313] M. Welling, T. P. Minka, and Y. W. Teh. Structured Region Graphs: Morphing EP into GBP. In F. Bacchus
and T. Jaakkola, editors, Uncertainty in Artiﬁcial Intelligence, number 21, pages 609–614, Corvallis, Oregon,
USA, 2005. AUAI press.

[314] J. Whittaker. Graphical Models in Applied Multivariate Statistics. John Wiley & Sons, 1990.

[315] W. Wiegerinck. Variational approximations between mean ﬁeld theory and the Junction Tree algorithm. In
C. Boutilier and M. Goldszmidt, editors, Uncertainty in Artiﬁcial Intelligence, number 16, pages 626–633, San
Francisco, CA, 2000. Morgan Kaufmann.

[316] W. Wiegerinck and T. Heskes. Fractional Belief Propagation. In S. Becker, S. Thrun, and K. Obermayer, editors,
Advances in Neural Information Processing Systems (NIPS), number 15, pages 438–445, Cambridge, MA, 2003.
MIT Press.

[317] C. K. I. Williams. Computing with inﬁnite networks. In M. C. Mozer, M. I. Jordan, and T. Petsche, editors,
Advances in Neural Information Processing Systems NIPS 9, pages 295–301, Cambridge, MA, 1997. MIT Press.

[318] C. K. I. Williams and D. Barber. Bayesian classiﬁcation with Gaussian processes. IEEE Trans Pattern Analysis

and Machine Intelligence, 20:1342–1351, 1998.

[319] C. Yanover and Y. Weiss. Finding the M Most Probable Conﬁgurations Using Loopy Belief Propagation. In
S. Thrun, L. Saul, and B. Sch¨olkopf, editors, Advances in Neural Information Processing Systems (NIPS),
number 16, pages 1457–1464, Cambridge, MA, 2004. MIT Press.

[320] J. S. Yedidia, W. T. Freeman, and Y. Weiss. Constructing free-energy approximations and generalized belief

propagation algorithms. Information Theory, IEEE Transactions on, 51(7):2282–2312, July 2005.

642

DRAFT April 24, 2015

BIBLIOGRAPHY

BIBLIOGRAPHY

[321] S. Young, D. Kershaw, J. Odell, D. Ollason, V. Valtchev, and P. Woodland. The HTK Book Version 3.0.

Cambridge University Press, 2000.

[322] A. L. Yuille and A. Rangarajan. The concave-convex procedure. Neural Computation, 15(4):915–936, 2003.

[323] J.-H. Zhao, P. L. H. Yu, and Q. Jiang. ML estimation for factor analysis: EM or non-EM? Statistics and

Computing, 18(2):109–123, 2008.

[324] O. Zoeter. Monitoring non-linear and switching dynamical systems. PhD thesis, Radboud University Nijmegen,

2005.

DRAFT April 24, 2015

643

BIBLIOGRAPHY

BIBLIOGRAPHY

644

DRAFT April 24, 2015

Index

average, 158

backtracking, 83
bag of words, 234, 319
batch update, 356
Baum-Welch, 468
Bayes Information Criterion, 275
Bayes’

factor, 208, 267
model selection, 267
theorem, 9

Bayes’ rule, see Bayes’ theorem
Bayesian

decision theory, 299
hypothesis testing, 267, 276
image denoising, 573
linear model, 367
mixture model, 412
model selection, 267
Occam’s razor, 270
outcome analysis, 276
Bayesian Dirichlet score, 209
Bayesian linear model, 386
BD score, 209

BDeu score, 209

BDeu score, 209
belief network

chest clinic, 51

belief network

asbestos-smoking-cancer, 202
cascade, 38
divorcing parents, 35
dynamic, 474
noisy AND gate, 35
noisy logic gate, 35
noisy OR gate, 35
sigmoid, 264
structure learning, 205
training

Bayesian, 199
belief propagation, 78, 586

loopy, 584

M coding, 233

−

−

of

1
N -max-product, 86
α-expansion, 595
α-recursion, 461
β-recursion, 462
γ-recursion, 463
maximum likelihood

Hopﬁeld network, 528

naive mean ﬁeld, 576

absorbing state, 87
absorption, 99

inﬂuence diagram, 131

acceptance function, 553
active learning, 293
acyclic, 26
adjacency matrix, 28, 424
algebraic Riccati equation, 496
ancestor, 26
ancestral ordering, 548
ancestral sampling, 548
antifreeze, 256
approximate inference, 115, 419, 569
belief propagation, 586, 587
Bethe free energy, 585
double integration bound, 597
expectation propagation, 587
graph cut, 594
Laplace approximation, 569
switching linear dynamical system, 508
variational approach, 570
variational inference, 573

AR model, see auto-regressive model
ARCH model, 488
Artiﬁcial Life, 532
asymmetry, 127
asynchronous updating, 575
auto-regressive model, 485

ARCH, 488
GARCH, 488
switching, 500
time-varying, 487

auxiliary variable sampling, 555

belief revision, see max-product, 85

645

INDEX

Bellman’s equation, 135
Bessel function, 392
beta

distribution, 165
function, 165, 193
Bethe free energy, 586
bias, 174

unbiased estimator, 174

bigram, 468
binary entropy, 575
binomial coeﬃcient, 163
binomial options pricing model, 141
bioinformatics, 475
black and white sampling, 566
black-box, 300
Black-Scholes option pricing, 142
Blahut-Arimoto algorithm, 583
Boltzmann machine, 59, 71, 214, 598

restricted, 71
bond propagation, 93
Bonferroni inequality, 22
Boolean network, 532
Bradley-Terry-Luce model, 447
bucket elimination, 90
burn in, 551

calculus, 610
canonical correlation analysis, 332

constrained factor analysis, 439

canonical variates, 339
causal consistency, 127
causality, 47, 127

do calculus, 49
inﬂuence diagrams, 49
post intervention distribution, 49

CCA

see canonical correlation analysis, 332

centering, 171
chain graph, 65

chain component, 65

chain rule, 612
chain structure, 84
changepoint model, 518
checkerboard, 566
chest clinic, 51

missing data, 262
with decisions, 147

children, see directed acyclic graph
Cholesky, 340
chord, 26
chordal, 107
Chow-Liu tree, 211
classiﬁcation, 292, 357, 396

Bayesian, 375
boundary, 231
error analysis, 276

646

INDEX

linear parameter model, 352
multiple classes, 358
performance, 276

random guessing, 284

softmax, 358

clique, 26

decomposition, 423
graph, 98
matrix, 28, 423

cliquo, 28
Cluster Variation method, 587
clustering, 293
collaborative ﬁltering, 324
collider, see directed acyclic graph
commute, 605
compatibility function, 565
competition model

Bradley-Terry-Luce, 447
Elo, 448
TrueSkill, 448
competition models, 447
concave function, 612
conditional entropy, 582
conditional likelihood, 227
conditional mutual information, 237
conditional probability, 8
conditional random ﬁeld, 221, 473
conditioning, 170

loop cut set, 92

conjugate distribution, 173
exponential family, 172
Gaussian, 178, 179
prior, 172

conjugate gradient, 357
conjugate gradients algorithm, 616
conjugate vector, 615
conjugate vectors algorithm, 616
connected components, 27
connected graph, 27
consistent, 103
consistent estimator, 174
control, 578
convex function, 612
correction smoother, 463, 494
correlation

matrix, 160
cosine similarity, 321
coupled HMM, 474
covariance, 160
matrix, 160

covariance function, 387, 390
γ-exponential, 392
construction, 391
Gibbs, 393
isotropic, 392

DRAFT April 24, 2015

INDEX

INDEX

Mat´ern, 392
Mercer kernel, 394
neural network, 393
non-stationary, 393
Ornstein-Uhlenbeck, 392, 394
periodic, 392
rational quadratic, 392
smoothness, 394
squared exponential, 392
stationary, 391

CPT, see conditional probability table
CRF, see conditional random ﬁeld
critical point, 613
cross-validation, 297
cumulant, 545
curse of dimensionality, 137, 350
cut set conditioning, 91
cycle, 26

D-map, see dependence map
DAG, see directed acyclic graph
data

anomaly detection, 293
catagorical, 157
dyadic, 421
handwritten digits, 357
labelled, 291
monadic, 422
numerical, 157
ordinal, 157
unlabelled, 292
data compression, 415

vector quantisation, 415

decision boundary, 353
decision function, 294
decision theory, 121, 194, 299
decision tree, 122
decomposable, 107
degree, 29
degree of belief, 9
delta function, see Dirac delta function

Kronecker, 160
density estimation, 403

Parzen estimator, 414

dependence
map, 69
descendant, 26
design matrix, 373, 386
determinant, 606
deterministic latent variable model, 532
diﬀerentiation, 611
digamma function, 183
digit data, 315
Dijkstra’s algorithm, 88
dimension reduction
linear, 311, 314

DRAFT April 24, 2015

supervised, 337
dimensionality reduction

linear, 316
non-linear, 317

Dirac delta function, 160, 161
directed acyclic graph, 38

ancestor, 26
ancestral order, 28
cascade, 38
children, 26
collider, 40
descendant, 26
family, 26
immorality, 46
moralisation, 63
parents, 26
direction bias, 473
directional derivative, 612
Dirichlet

distribution, 167

Dirichlet process mixture models, 418
discount factor, 136
discriminative

approach, 300
training, 300

discriminative approach, 299
discriminative training, 471
dissimilarity function, 305
distributed computation, 525
distribution, 7

Bernoulli, 163, 407
beta, 165, 167, 182, 200, 269
binomial, 163
Categorical, 163
change of variables, 158, 181
conjugate, 178
continuous, 9
density, 9
Dirichlet, 167, 183, 202, 208, 418, 419
discrete, 7
divergence, 161
double exponential, 166
empirical, 161, 295

average, 158
expectation, 158

exponential, 164
exponential family, 171

canonical, 171

gamma, 412
mode, 183

Gauss-gamma, 179, 184
Gauss-inverse-gamma, 178
Gaussian

canonical exponential form, 172
conditioning, 170

647

INDEX

INDEX

conjugate, 178, 179
entropy, 171
mixture, 186
multivariate, 168
normalisation, 180, 181
partitioned, 170
propagation, 170
system reversal, 170
univariate, 166
inverse gamma, 165
inverse Wishart, 179
isotropic, 169
joint, 8
kurtosis, 160
Laplace, 166
marginal, 8
mode, 159
moment, 159
multinomial, 163
normal, 166
Poisson, 164, 185
Polya, 418
scaled mixture, 167
skewness, 160
Student’s t, 166
uniform, 162, 164
Wishart, 412

domain, 7
double integration bound, 597
dual parameters, 351
dual representation, 351
dyadic data, 421
dynamic Bayesian network, 474
dynamic synapses, 536
dynamical system
linear, 483
non-linear, 532
dynamics reversal, 494

early stopping, 357
edge list, 27
eﬃcient IPF, 218
eigen

decomposition, 169, 314, 609
equation, 313
function, 610
problem, 333
spectrum, 394
value, 314, 608

Elo model, 447
emission distribution, 460
emission matrix, 489
empirical

independence, 207

empirical distribution, 161, 176, 295
empirical risk, 295

penalised, 296

empirical risk minimisation, 296
energy, 245
entropy, 110, 162, 186, 245, 582

diﬀerential, 162, 186
Gaussian, 171

EP, see expectation propagation
error function, 353
estimator

consistent, 174

evidence, see marginal likelihood

hard, 35
likelihood, 37
soft, 35
uncertain, 35
virtual, 37

Evidence Procedure, 369
exact sampling, 548
expectation, see average
expectation correction, 512, 513
expectation maximisation, 151, 244, 325, 370, 468,

498

algorithm, 244, 245
antifreeze, 256
belief networks, 248
E-step, 245
energy, 245
entropy, 245
failure case, 255
intractable energy, 254
M-step, 245
mixture model, 405
partial E-step, 253
partial M-step, 253
variational Bayes, 258
Viterbi training, 254
expectation propagation, 587
exponential family, 171
canonical form, 171
conjugate, 172

extended observability matrix, 499

face model, 436
factor analysis, 431

factor rotation, 430
probabilistic PCA, 438
training

EM, 434
SVD, 432
factor graph, 67
factor loading, 429
family, see directed acyclic graph
feature map, 330
ﬁltering, 461
ﬁnance, 139

optimal investment, 142

648

DRAFT April 24, 2015

INDEX

INDEX

option pricing, 140

ﬁnite dimensional Gaussian Process, 387
Fisher information, 184
Fisher’s linear discriminant, 337
Floyd-Warshall-Roy algorithm, 89
forward sampling, 548
Forward-Backward, 462
forward-ﬁltering-backward-sampling, 464
Forward-Sampling-Resampling, 564

gamma

digamma, 183
distribution, 164
function, 166, 183

GARCH model, 488
Gaussian

canonical representation, 168
distribution, 166
moment representation, 168, 492
sub, 160
super, 160

Gaussian mixture model, 409

Bayesian, 412
collapsing the mixture, 511
EM algorithm, 409
inﬁnite problems, 412
k-means, 415
Parzen estimator, 414
symmetry breaking, 412

Gaussian process, 385
classiﬁcation, 396
Laplace approximation, 397
multiple classes, 400
regression, 388
smoothness, 394
weight space view, 386
Gaussian sum ﬁltering, 508
Gaussian sum smoothing, 512
generalisation, 291, 295
generalised pseudo Bayes, 516
generative

approach, 299
model, 299
training, 299

generative approach, 299
Gibbs sampling, 549
Glicko, 448
GMM, see Gaussian mixture model
Google, 457
gradient, 611

descent, 613

Gram matrix, 351, 387
Gram-Schmidt procedure, 615
graph, 25

acyclic, 26
adjacency matrix, 28

DRAFT April 24, 2015

chain, 65
chain structured, 84
chord, 26
chordal, 107
clique, 26, 28, 98
clique matrix, 28
cliquo, 28
connected, 27
cut, 594
cycle, 26
decomposable, 107
descendant, 28
directed, 25
disconnected, 111
edge list, 27
factor, 67
loop, 26
loopy, 27
multiply connected, 27, 105
neighbour, 26
path, 25
separation, 63
set chain, 115
singly connected, 27
skeleton, 46
spanning tree, 27
tree, 27
triangulated, 107
undirected, 25
vertex

degree, 29

graph cut algorithm, 594
graph partitioning, 421
Gull-MacKay iteration, 372

Hamilton-Jacobi equation, 135
Hamiltonian dynamics, 556
Hammersley-Cliﬀord theorem, 61
handwritten digits, 357
Hankel matrix, 499
harmonium, see restricted Boltzmann machine
Heaviside step function, 423
Hebb, 526
Hebb rule, 526
hedge fund, 282
Hermitian, 606
Hessian, 357, 611
hidden Markov model, 110, 254, 460

α recursion, 461
β recursion, 462
coupled, 474
direction bias, 473
discriminative training, 471
duration model, 471
entropy, 110
ﬁltering, 461

649

INDEX

INDEX

input-output, 472
likelihood, 463
most likely state (MAP), 464
pairwise marginal, 463
Rauch Tung Striebel smoother, 463
smoothing

parallel, 462
sequential, 462

viterbi, 86
Viterbi algorithm, 464

hidden variables, 241
HMM, see hidden Markov model
Hopﬁeld network, 525
augmented, 534
capacity, 529
Hebb rule, 527
heteroassociative, 534
maximum likelihood, 528
perceptron, 529
pseudo inverse rule, 527
sequence learning, 526

hybrid Monte Carlo, 555
hyper Markov, 224
hyper tree, 109
hyperparameter, 172, 201, 369
hyperplane, 604
hypothesis testing, 267

Bayesian error analysis, 276

I-map, see independence map
ICA, 440
identiﬁability, 497
identity matrix, 605
IID, see independent and identically distributed
IM algorithm, see information-maximisation algorithm
immorality, 46
importance

distribution, 561
sampling, 560

particle ﬁlter, 563
resampling, 562
sequential, 562

weight, 561
incidence matrix, 28
independence

Bayesian, 208
conditional, 11, 32
empirical, 207
map, 68
Markov equivalent, 46
mutual information, 163
naive Bayes, 229
parameter, 199
perfect map, 69

independent and identically distributed, 173, 191
independent components analysis, 443

indicator function, 19
indicator model, 417
induced representation, 106
inference, 75

bond propagation, 93
bucket elimination, 90
causal, 47
cut set conditioning, 91
HMM, 461
linear dynamical system, 490
MAP, 93
marginal, 75
Markov decision process, 138
max-product, 83
message passing, 75
mixed, 89
MPM, 93
sum-product algorithm, 80
transfer matrix, 77
variable elimination, 75

inﬂuence diagram, 125
absorption, 131
asymmetry, 127
causal consistency, 127
chest clinic, 146
decision potential, 130
fundamental link, 127
information link, 125
junction tree, 130
no forgetting assumption, 126
partial order, 126
probability potential, 130
solving, 129
utility, 125
utility potential, 130

information link, 125
information maximisation, 583
information retrieval, 320, 457
information-maximisation algorithm, 582
innovation noise, 485
input-output HMM, 472
inverse modus ponens, 15
IPF, see iterative proportional ﬁtting, 215

eﬃcient, 218

Ising model, see Markov network, 64, 93

approximate inference, 573

isotropic, 169, 411
isotropic covariance functions, 392
item response theory, 446
Iterated Conditional Modes, 591
iterative proportional ﬁtting, 215
iterative scaling, 220

Jeﬀrey’s rule, 36
Jensen’s inequality, 598, 613
Joseph’s symmetrized update, 493

650

DRAFT April 24, 2015

INDEX

INDEX

jump Markov model, see switching linear dynamical

system
junction tree, 101, 104
absorption, 99
algorithm, 97, 108
clique graph, 98
computational complexity, 110
conditional marginal, 112
consistent, 103
hyper tree, 109
inﬂuence diagram, 130
marginal likelihood, 111
most likely state, 113
normalisation constant, 110
potential, 98
running intersection property, 102
separator, 98
strong, 131
strong triangulation, 131
tree width, 109
triangulation, 106

k-means, 415
Kalman ﬁlter, 489
Kalman gain, 493
KD-tree, 306
kernel, 351, see covariance function

classiﬁer, 358
kidnapped robot, 466
Kikuchi, 587
KL divergence, see Kullback-Leibler divergence
KNN, see nearest neighbour
Kronecker delta, 160, 605
Kullback-Leibler divergence, 161, 245, 571
kurtosis, 160

labelled data, 291
Lagrange

dual, 619
multiplier, 619

Lagrangian, 619
Laplace approximation, 376, 397, 569
latent ability model, 445
latent Dirichlet allocation, 419
latent linear model, 429
latent semantic analysis, 319
latent topic, 320
latent variable, 241

deterministic, 532
model, 241

lattice model, 63
LDA regularised, 343
LDS, see linear dynamical system
leaky integrate and ﬁre model, 537
Leapfrog discretisation, 557
learning

DRAFT April 24, 2015

active, 293
anomaly detection, 293
Bayesian, 192
belief network, 196
belief networks

EM, 248

Dirichlet prior, 202
inference, 191
nearest neighbour, 305
online, 293
query, 293
reinforcement, 293
semi-supervised, 294, 416
sequences, 468, 497, 526
sequential, 293
structure, 205
supervised, 291
unsupervised, 292, 429

learning rate, 355
likelihood, 111, 173, 463, 495, 511

bound, 244
marginal, 81, 173
model, 173, 205

approximate, 274

pseudo, 224

likelihood decomposable, 205
line search, 614, 615
linear algebra, 603
linear dimension reduction, 311, 314

canonical correlation analysis, 332
latent semantic analysis, 319
non-negative matrix factorisation, 328
probabilistic latent semantic analysis, 325
supervised, 337
unsupervised, 311

Linear Discriminant Analysis, 337
linear discriminant analysis, 337

as regression, 342
penalised, 342
regularised, 342

linear dynamical system, 170, 489

cross moment, 495
dynamics reversal, 494
ﬁltering, 492
identiﬁability, 497
inference, 490
learning, 497
likelihood, 495
most likely state, 496
numerical stability, 491
Riccati equations, 496
smoothing, 494
subspace method, 499
switching, 507
symmetrising updates, 493

651

INDEX

INDEX

linear Gaussian state space model, 489
linear model, 345
Bayesian, 367
classiﬁcation, 352
factor analysis, 431
latent, 429
regression, 346

linear parameter model, 346

Bayesian, 368
linear perceptron, 354
linear separability, 354
linear transformation, 606
linearly independent, 603
linearly separable, 354
Linsker’s as-if-Gaussian approximation, 583
localisation, 465
logic

Aristotle, 15
logistic regression, 353
logistic sigmoid, 353
logit, 353
loop, 26
loop cut set, 92
loopy, 27
loss function, 294, 295
squared, 295
zero-one, 294

loss matrix, 295
Luenberger expanding subspace theorem, 616

Mahalanobis distance, 305, 335
manifold

linear, 311
low dimensional, 311

MAP, see most probable a posteriori, see most prob-

able a posteriori

MAR, see missing at random
margin, 359
soft, 360

marginal, 8

generalised, 131

marginal likelihood, 81, 111, 173, 243, 369, 389

approximate, 371, 376, 377, 399

marginalisation, 8
Markov

blanket, 26
chain, 263, 455

ﬁrst order, 484
stationary distribution, 456

equivalent, 46
global, 60
hyper, 224
local, 60
model, 455
pairwise, 60
random ﬁeld, 220

652

approximation, 573

Markov chain, 75, 139, 553
absorbing state, 87
PageRank, 457

Markov chain Monte Carlo, 552
auxiliary variable, 555

hybrid Monte Carlo, 555
slice sampling, 559
Swendson-Wang, 557

Gibbs sampling, 550
Metropolis-Hastings, 553
proposal distribution, 553
structured Gibbs sampling, 551

Markov decision process, 133, 148
Bellman’s equation, 135
discount factor, 136
partially observable, 144
planning, 138
policy iteration, 137
reinforcement learning, 144
stationary deterministic policy, 138
temporally unbounded, 136
value iteration, 136
Markov equivalence, 46
Markov network, 58

Boltzmann machine, 59
continuous-state temporal, 483
discrete-state temporal, 455
Gibbs distribution, 58
Hammersley Cliﬀord theorem, 61
pairwise, 58
potential, 58

Markov random ﬁeld, 60, 599, 600

alpha-expansion, 595
attractive binary, 593
graph cut, 594
map, 590
Potts model, 595

matrix, 605

adjacency, 28, 424
block inverse, 608
Cholesky, 340
clique, 28
Gram, 331
Hankel, 499
incidence, 28
inversion, 607
inversion lemma, 607
orthogonal, 607
positive deﬁnite, 610
pseudo inverse, 607
rank, 607
stochastic, 77

matrix factorisation, 328
max-product, 83

DRAFT April 24, 2015

INDEX

INDEX

N most probable states, 86

max-sum, 87
maximum cardinality checking, 108
maximum likelihood, 18, 173, 174, 176, 196, 355

belief network, 196
Chow-Liu tree, 212
counting, 196
empirical distribution, 176
factor analysis, 431
Gaussian, 176
gradient optimisation, 261
Markov network, 213
ML-II, 195
naive Bayes, 230
properties, 174

MCMC, see Markov chain Monte Carlo
mean ﬁeld theory, 576

asynchronous updating, 576

Mercer kernel, 394
message

passing, 100
schedule, 80, 101

message passing, 75
Metropolis-Hastings acceptance function, 553
Metropolis-Hastings sampling, 553
minimum clique cover, 424
missing at random, 242
completely, 243

missing data, 241
mixed inference, 89
mixed membership model, 419
mixing matrix, 440
mixture

Gaussian, 186

mixture model, 403

Bernoulli product, 407
Dirichlet process mixture, 418
expectation maximisation, 405
factor analysis, 435
Gaussian, 409
indicator approach, 417
Markov chain, 458
PCA, 435

mixture of experts, 416
MN, see Markov network
mode, 159
model

auto-regressive, 485
changepoint, 518
deterministic latent variable, 532
faces, 436
leaky integrate and ﬁre, 537
linear, 345
mixed membership, 419
mixture, 403

Rasch, 445
model selection, 267
approximate, 274

moment, 159
moment representation, 492
momentum, 614
monadic data, 422
money

ﬁnancial prediction, 282
loadsa, 282
moralisation, 63, 104
most probable a posteriori, 18, 173
most probable path

multiple-source multiple-sink, 89

most probable state

N most probable, 85
MRF, see Markov random ﬁeld
multiply connected, 27
multiply connected-distributions, 105
mutual information, 163, 212, 581

approximation, 581
conditional, 162
maximisation, 582

naive Bayes, 229, 407
Bayesian, 234
tree augmented, 236
naive mean ﬁeld theory, 574
nearest neighbour, 305

probabilistic, 308

network ﬂow, 599
network modelling, 330
neural computation, 525
neural network, 365, 535
depression, 536
dynamic synapses, 536
leaky integrate and ﬁre, 537

Newton update, 357
Newton’s method, 617
no forgetting assumption, 126
node

extremal, 85
simplicial, 85

non-negative matrix factorisation, 328
normal distribution, 166
normal equations, 347
normalised importance weights, 561

observed linear dynamical system, 483
Occam’s razor, 270
One of m encoding, 157
online learning, 293
optimisation, 198, 613

Broyden-Fletcher-Goldfarb-Shanno, 618
conjugate gradients algorithm, 616
conjugate vectors algorithm, 616

DRAFT April 24, 2015

653

INDEX

INDEX

constrained optimisation, 619
critical point, 613
gradient descent, 613
Luenberger expanding subspace theorem, 616
Newton’s method, 617
quasi Newton method, 618

option pricing, 140
ordinary least squares, 345
orthogonal, 603
orthogonal least squares, 346
orthonormal, 603
outlier, 362
over-complete representation, 324
over-complete representations, 324
overcounting, 98
overﬁtting, 222, 272, 388

PageRank, 457
pairwise comparison models, 447
pairwise Markov network, 58
parents, see directed acyclic graph
part-of-speech tagging, 475
Partial Least Squares, 440
partial order, 126
partially observable MDP, 144
particle ﬁlter, 562
partition function, 58, 598
partitioned matrix
inversion, 181

Parzen estimator, 308, 414
path

blocked, 43
PC algorithm, 206
PCA, see Principal Components Analysis
perceptron, 354

logistic regression, 355
perfect elimination order, 107
perfect map, see independence
perfect sampling, 548
planning, 138
plant monitoring, 293
plate, 192
Poisson distribution, 164, 185
policy, 136

iteration, 137
stationary deterministic, 138

Polya distribution, 418
POMDP, see partially observable MDP
positive deﬁnite
matrix, 387

parameterisation, 425

posterior, 173

Dirichlet, 203

potential, 58
Potts model, 595
precision, 168, 179, 368

654

prediction

auto-regression, 485
ﬁnancial, 282
non-parametric, 385
parameteric, 385
predictive variance, 368
predictor-corrector, 461
Principal Components Analysis, 311, 316

algorithm, 314
high dimensional data, 317
kernel, 330
latent semantic analysis, 319
missing data, 321
probabilistic, 438
principal directions, 314
printer nightmare, 226
missing data, 262

prior, 173
probabilistic latent semantic analysis, 325

conditional, 327
EM algorithm, 325
probabilistic PCA, 438
probability

conditional, 8

function, 198
density, 9, 158
frequentist, 9
posterior, 173
potential, 130
prior, 173
subjective, 9

probit, 353
probit regression, 353
projection, 604
proposal distribution, 553
Pseudo Inverse, 526
pseudo inverse

Hopﬁeld network, 527

pseudo likelihood, 224

quadratic form, 610
quadratic programming, 359
query learning, 293
questionnaire analysis, 445

radial basis functions, 350
Raleigh quotient, 340
Random Boolean networks, 532
Rasch model, 445
Bayesian, 446

Rauch-Tung-Striebel, 463
reabsorption, 114
region graphs, 587
regresion

linear parameter model, 346

regression, 292, 386

DRAFT April 24, 2015

INDEX

INDEX

logisitic, 353

regularisation, 272, 296, 348, 357
reinforcement learning, 144, 293
rejection sampling, 545
relevance vector machine, 374, 381
reparameterisation, 97
representation
dual, 351
over-complete, 324
sparse, 324
under-complete, 324

resampling, 561
reset model, 518
residuals, 346
responsibility, 411
restricted Boltzmann machine, 71
Riccati equation, 496
ridge regression, 349
risk, 294
robot arm, 578
robust classiﬁcation, 362
Rose-Tarjan-Lueker elimination, 107
running intersection property, 102, 103

sample

mean, 161
variance, 161

sampling, 419

ancestral, 548
forward-ﬁltering-backward-sampling, 464, 495
Gibbs, 549
importance, 560
multi-variate, 546
particle ﬁlter, 563
rejection, 545
univariate, 544

Sampling Importance Resampling, 561
scalar product, 603
scaled mixture, 167
search engine, 457
self localisation and mapping, 467
semi-supervised learning, 294

lower dimensional representations, 301

separator, 98
sequential importance sampling, 562
sequential minimal optimisation, 362
set chain, 115
Shafer-Shenoy propagation, 113
shortest path, 87
shortest weighted path, 88
sigmoid

logistic, 353

sigmoid belief network, 264
sigmoid function

approximate average, 378

simple path, 88

DRAFT April 24, 2015

simplicial nodes, 107
Simpson’s Paradox, 47
singly connected, 27
singular, 607
Singular Value Decomposition, 318, 609

thin, 341, 609

skeleton, 46, 206
skewness, 160
slice sampling, 559
smoothing, 462
softmax function, 358, 416
spam ﬁltering, 234
spanning tree, 27
sparse representation, 324
spectrogram, 487
speech recognition, 474
spike response model, 535
squared Euclidean distance, 305
squared loss, 295
standard deviation, 159
standard normal distribution, 166
stationary, 553

distribution, 78

stationary Markov chain, 456
stochastic matrix, 77
stop words, 420
strong Junction Tree, 130
strong triangulation, 131
structure learning, 205
Bayesian, 209
network scoring, 209
PC algorithm, 206
undirected, 224

structured Expectation Propagation, 590
subsampling, 551
subspace method, 499
sum-product, 584
sum-product algorithm, 80
supervised learning, 291

-semi, 294
classiﬁcation, 292
regression, 292

support vector machine, 359

chunking, 362
training, 362
support vectors, 360
SVD, see Singular Value Decomposition
SVM, see support vector machine
Swendson-Wang sampling, 557
switching AR model, 500
switching Kalman ﬁlter, see switching linear dynam-

ical system

switching linear dynamical system, 507

changepoint model, 518
collapsing Gaussians, 511

655

INDEX

INDEX

expectation correction, 512
ﬁltering, 508
Gaussian sum smoothing, 512
generalised Pseudo Bayes, 516
inference

computational complexity, 508

likelihood, 511
smoothing, 514
symmetry breaking, 412
system reversal, 170

tagging, 475
tall matrix, 324
term-document matrix, 319
test set, 291
text analysis, 329, 419

latent semantic analysis, 319
latent topic, 320
probabilistic latent semantic analysis, 325

TF-IDF, 319
time-invariant LDS, 496
Tower of Hanoi, 137
trace-log formula, 609
train set, 291
training

batch, 356
discriminative, 300
generative, 299
generative-discriminative, 300
HMM, 468
linear dynamical system, 497
online, 356
transfer matrix, 77
transition distribution, 460
transition matrix, 483, 489
tree, 27, 76

Chow-Liu, 211

tree augmented network, 236
tree width, 109
triangulation, 106, 107

check, 109
greedy elimination, 107
maximum cardinality, 108
strong, 131
variable elimination, 107

TrueSkill, 448

uncertainty, 162
under-complete representation, 324
undirected graph, 25
undirected model
learning

hidden variable, 261
latent variable, 261
uniform distribution, 164
unit vector, 603

656

unlabelled data, 293
unsupervised learning, 292, 429
utility, 121, 294
matrix, 295
money, 121
potential, 130
zero-one loss, 294

validation, 297
cross, 297

value, 135
value iteration, 136
variable

hidden, 244
missing, 244
visible, 244

variable elimination, 75
variance, 159
variational approximation

factorised, 574
structured, 576, 577

variational Bayes, 256

expectation maximisation, 258

varimax, 430
vector algebra, 603
vector quantisation, 415
visualisation, 334
Viterbi, 86, 254
Viterbi algorithm, 464
Viterbi alignment, 460
Viterbi training, 254
volatility, 488
Voronoi tessellation, 305

web

modelling, 330

website, 330

analysis, 457
whitening, 171, 186
Woodbury formula, 607

XOR function, 354

zero-one loss, 294, 362

DRAFT April 24, 2015

