0% found this document useful (0 votes)
269 views

Putational Statistics Using Matlab

computational
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
269 views

Putational Statistics Using Matlab

computational
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

Computational Statistics with Matlab

Mark Steyvers May 13, 2011

Contents
1 Sampling from Random Variables 1.1 Standard distributions . . . . . . . . . . . . . . . . . . . . . 1.2 Sampling from non-standard distributions . . . . . . . . . . 1.2.1 Inverse transform sampling with discrete variables . . 1.2.2 Inverse transform sampling with continuous variables 1.2.3 Rejection sampling . . . . . . . . . . . . . . . . . . . 2 Markov Chain Monte Carlo 2.1 Monte Carlo integration . . . . . . . . . . . . . . 2.2 Markov Chains . . . . . . . . . . . . . . . . . . . 2.3 Putting it together: Markov chain Monte Carlo . 2.4 Metropolis Sampling . . . . . . . . . . . . . . . . 2.5 Metropolis-Hastings Sampling . . . . . . . . . . . 2.6 Metropolis-Hastings for Multivariate Distributions 2.6.1 Blockwise updating . . . . . . . . . . . . . 2.6.2 Componentwise updating . . . . . . . . . . 2.7 Gibbs Sampling . . . . . . . . . . . . . . . . . . . 3 Basic concepts in Bayesian Data Analysis 3.1 Parameter Estimation Approaches . . . . . . 3.1.1 Maximum Likelihood . . . . . . . . . 3.1.2 Maximum a posteriori . . . . . . . . 3.1.3 Posterior Sampling . . . . . . . . . . 3.2 Example: Estimating a Weibull distribution 3.3 Example: Logistic Regression . . . . . . . . 3.4 Example: Mallows Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4 7 8 11 12 15 15 16 18 18 22 26 26 31 35 39 40 40 40 41 42 45 45 46 46 48 49 51 52

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . .

4 Directed Graphical Models 4.1 A Short Review of Probability Theory . . . . . . . . . . 4.2 The Burglar Alarm Example . . . . . . . . . . . . . . . . 4.2.1 Conditional probability tables . . . . . . . . . . . 4.2.2 Explaining away . . . . . . . . . . . . . . . . . . 4.2.3 Joint distributions and independence relationships 1

CONTENTS 4.3 Graphical Model Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3.1 Example: Consensus Modeling with Gaussian variables . . . . . . . . 5 Approximate Inference in Graphical Models 5.1 Prior predictive distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Posterior distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.1 Rejection Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.2 MCMC Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.3 Example: Posterior Inference for the consensus model with normally distributed variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.4 Example: Posterior Inference for the consensus model with contaminants 6 Sequential Monte Carlo 6.1 Hidden Markov Models . . . . . . . . . . . . . . . . . . . 6.1.1 Example HMM with discrete outcomes and states 6.1.2 Viterbi Algorithm . . . . . . . . . . . . . . . . . . 6.2 Bayesian Filtering . . . . . . . . . . . . . . . . . . . . . . 6.3 Particle Filters . . . . . . . . . . . . . . . . . . . . . . . 6.3.1 Sampling Importance Resampling (SIR) . . . . . 6.3.2 Direct Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2 54 54 57 57 60 60 61 63 66 69 70 71 72 73 74 74 76

Note to Students
Exercises
This course book contains a number of exercises in which you are asked to simulate Matlab code, produce new code, as well as produce graphical illustrations and answers to questions. The exercises marked with ** are optional exercises that can be skipped when time is limited.

Organizing answers to exercises


It is helpful to maintain a document that organizes all the material related to the exercises. Matlab can facilitate part of this organization using the publish option. For example, if you have a Matlab script that produces a gure, you can publish the code as well as the gure produced by the code to a single external document. You can nd the publishing option in the Matlab editor under the le menu. You can also use the publish function directly in the command window. You can change the publish conguration (look under the le menu of the editor window) to produce pdfs, Word documents and a number of le formats. For this course however, you can hand in your answers in any way you see t and sometimes, it might be useful to just hand in handwritten answers. 1

Matlab documentation
It will probably happen many times that you will need to nd the name of a Matlab function or a description of the input and output variables for a given Matlab function. It is strongly recommended to have the Matlab documentation running in a separate window for quick consultation. You can access the Matlab documentation by typing doc in the command window. For specic help on a given matlab function, such as the function fprintf, you can type doc fprintf to get a help screen in the matlab documentation window or help fprintf to get a description in the matlab command window.

note: in this version, a new chapter 3 was inserted which is not fully completed yet

Chapter 1 Sampling from Random Variables


Probabilistic models proposed by researchers are often too complicated for analytic approaches. Increasingly, researchers rely on computational, numerical-based methods when dealing with complex probabilistic models. By using a computational approach, the researcher is freed from making unrealistic assumptions required for some analytic techniques (e.g. such as normality and independence). The key to most approximation approaches is the ability to sample from distributions. Sampling is needed to predict how a particular model will behave under some set of circumstances, and to nd appropriate values for the latent variables (parameters) when applying models to experimental data. Most computational sampling approaches turn the problem of sampling from complex distributions into subproblems involving simpler sampling distributions. In this chapter, we will illustrate two sampling approaches: the inverse transformation method and rejection sampling. These approaches are appropriate mostly for the univariate case where we are dealing with single-valued outcomes. In the next chapter, we discuss Markov chain Monte Carlo approaches that can operate eciently with multivariate distributions.

1.1

Standard distributions

Some distributions are used so often, that they become part of a standard set of distributions supported by Matlab. The Matlab Statistics Toolbox supports a large number of probability distributions. Using Matlab, it becomes quite easy to calculate the probability density, cumulative density of these distributions, and to sample random values from these distributions. Table 1.1 lists some of the standard distributions supported by Matlab. The Matlab documentation lists many more distributions that can be simulated with Matlab. Using online resources, it is often easy to nd support for a number of other common distributions. To illustrate how we can use some of these functions, Listing 1.1 shows Matlab code that visualizes the Normal(, ) distribution where = 100 and = 15. To make things concrete, imagine that this distribution represents the observed variability of IQ coecients in some population. The code shows how to display the probability density and the cumulative 4

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES

Table 1.1: Examples of Matlab functions for evaluating probability density, cumulative density and drawing random numbers Distribution Normal Uniform (continuous) Beta Exponential Uniform (discrete) Binomial Multinomial Poisson
Probability Density Function 0.03 0.025 0.02 1

PDF normpdf unifpdf betapdf exppdf unidpdf binopdf mnpdf poisspdf

CDF normcdf unifcdf betacdf expcdf unidcdf binocdf poisscdf


Cumulative Density Function

Random Number Generation norm unifrnd betarnd exprnd unidrnd binornd mnrnd poissrnd
Histogram of random values 1800 1600

0.8

1400 1200

frequency
80 100 x 120 140

0.6

pdf

0.015 0.01 0.005 0 60

cdf

1000 800 600

0.4

0.2

400 200

80

100 x

120

140

0 60

50

100 x

150

200

Figure 1.1: Illustration of the Normal(, ) distribution where = 100 and = 15. density. It also shows how to draw random values from this distribution and how to visualize the distribution of these random samples using the hist function. The code produces the output as shown in Figure 1.1. Similarly, Figure 1.2 visualizes the discrete distribution Binomial(N, ) distribution where N = 10 and = 0.7. The binomial arises in situations where a researcher counts the number of successes out of a given number of trials. For example, the Binomial(10, 0.7) distribution represents a situation where we have 10 total trials and the probability of success at each trial, , equals 0.7.

Exercises
1. Adapt the Matlab program in Listing 1.1 to illustrate the Beta(, ) distribution where = 2 and = 3. Similarly, show the Exponential() distribution where = 2. 2. Adapt the matlab program above to illustrate the Binomial(N, ) distribution where N = 10 and = 0.7. Produce an illustration that looks similar to Figure 1.2. 3. Write a demonstration program to sample 10 values from a Bernoulli() distribution with = 0.3. Note that the Bernoulli distribution is one of the simplest discrete distri-

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES

Listing 1.1: Matlab code to visualize Normal distribution.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

%% Explore the mu = 100; % sigma = 15; % xmin = 70; % xmax = 130; % n = 100; % k = 10000; % x p c

Normal distribution N( mu , sigma ) the mean the standard deviation minimum x value for pdf and cdf plot maximum x value for pdf and cdf plot number of points on pdf and cdf plot % number of random draws for histogram

create a set of values ranging from xmin to xmax = linspace( xmin , xmax , n ); = normpdf( x , mu , sigma ); % calculate the pdf = normcdf( x , mu , sigma ); % calculate the cdf

figure( 1 ); clf; % create a new figure and clear the contents subplot( 1,3,1 ); plot( x , p , 'k' ); xlabel( 'x' ); ylabel( 'pdf' ); title( 'Probability Density Function' ); subplot( 1,3,2 ); plot( x , c , 'k' ); xlabel( 'x' ); ylabel( 'cdf' ); title( 'Cumulative Density Function' ); % draw k random numbers from a N( mu , sigma ) distribution y = normrnd( mu , sigma , k , 1 ); subplot( 1,3,3 ); hist( y , 20 ); xlabel( 'x' ); ylabel( 'frequency' ); title( 'Histogram of random values' );
Probability Distribution 0.35 0.3 0.8 0.25 1 Cumulative Probability Distribution 30 25 20 0.6 Histogram

Cumulative Probability

0.2 0.15 0.1 0.05 0

Frequency
0 1 2 3 4 5 6 x 7 8 9 10

Probability

15 10

0.4

0.2

5 0

5 6 x

9 10

5 6 x

9 10

Figure 1.2: Illustration of the Binomial(N, ) distribution where N = 10 and = 0.7.

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES

butions to simulate. There are only two possible outcomes, 0 and 1. With probability , the outcome is 1, and with probability 1 , the outcome is 0. In other words, p(X = 1) = , and p(X = 0) = 1 . This distribution can be used to simulate outcomes in a number of situations, such as head or tail outcomes from a weighted coin, correct/incorrect outcomes from true/false questions, etc. In Matlab, you can simulate the Bernoulli distribution using the binomial distribution with N = 1. However, for the purpose of this exercise, please write the code needed to sample Bernoulli distributed values that does not make use of the built-in binomial distribution. 4. It is often useful in simulations to ensure that each replication of the simulation gives the exact same result. In Matlab, when drawing random values from distributions, the values are dierent every time you restart the code. There is a simple way to seed the random number generators to insure that they produce the same sequence. Write a Matlab script that samples two sets of 10 random values drawn from a uniform distribution between [0,1]. Use the seeding function between the two sampling steps to demonstrate that the two sets of random values are identical. Your Matlab code could use the following line: seed=1; rand(state,seed); randn(state,seed); Note that there are more advanced methods to seed the random number generator in Matlab using RandStream methods. For most applications in this course, such methods are not needed. 5. Suppose we know from previous research that in a given population, IQ coecients are Normally distributed with a mean of 100 and a standard deviation of 15. Calculate the probability that a randomly drawn person from this population has an IQ greater than 110 but smaller than 130. You can achieve this using one line of matlab code. What does this look like? ** 6 The Dirichlet distribution is currently not supported by Matlab. Can you nd a matlab function, using online resources, that implements the sampling from a Dirichlet distribution?

1.2

Sampling from non-standard distributions

Suppose we wish to sample from a distribution that is not one of the standard distributions that is supported by Matlab. In modeling situations, this situation frequently arises, because a researcher can propose new noise processes or combinations of existing distributions. Computational methods for solving complex sampling problems often rely on sampling distributions that we do know how to sample from eciently. The random values from these simple distributions can then be transformed or compared to the target distribution. In fact, some of the techniques discussed in this section are used by Matlab internally to sample from distributions such as the Normal and Exponential distributions.

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES

1.2.1

Inverse transform sampling with discrete variables

Inverse transform sampling (also known as the inverse transform method) is a method for generating random numbers from any probability distribution given the inverse of its cumulative distribution function. The idea is to sample uniformly distributed random numbers (between 0 and 1) and then transform these values using the inverse cumulative distribution function. The simplicity of this procedure lies in the fact that the underlying sampling is just based on transformed uniform deviates. This procedure can be used to sample many dierent kinds of distributions. In fact, this is how Matlab implements many of its random number generators. It is easiest to illustrate this approach on a discrete distribution where we know the probability of each individual outcome. In this case, the inverse transform method just requires a simple table lookup. To give an example of a non-standard discrete distribution, we use some data from experiments that have looked at how well humans can produce uniform random numbers (e.g. Treisman and Faulkner, 1987). In these experiments, subjects produce a large number of random digits (0,..,9) and investigators tabulate the relative frequencies of each random digit produced. As you might suspect, subjects do not always produce uniform distributions. Table 1.2.1 shows some typical data. Some of the low and the high numbers are underrepresented while some specic digits (e.g. 4) are overrepresented. For some reason, the digits 0 and 9 were never generated by the subject (perhaps because the subject misinterpreted the instructions). In any case, this data is fairly typical and demonstrates that humans are not very good are producing uniformly distributed random numbers. Table 1.2: Probability of digits observed in human random digit generation experiment. The generated digit is represented by X ; p(X ) and F (X ) are the probability mass and cumulative probabilities respectively. The data was estimated from subject 6, session 1, in experiment by Treisman and Faulkner (1987). X 0 1 2 3 4 5 6 7 8 9 p(X ) 0.000 0.100 0.090 0.095 0.200 0.175 0.190 0.050 0.100 0.000 F (X ) 0.000 0.100 0.190 0.285 0.485 0.660 0.850 0.900 1.000 1.000

Suppose we now want to mimic this process and write an algorithm that samples digits

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES

according to the probabilities shown in Table 1.2.1. Therefore, the program should produce a 4 with probability .2, a 5 with probability .175, etc. For example, the code in Listing 1.2 implements this process using the built-in matlab function randsample. The code produces the illustration shown in Figure 1.2.1. Instead of using the built-in functions such as randsample or mnrnd, it is helpful to consider how to implement the underlying sampling algorithm using the inverse transform method. We rst need to calculate the cumulative probability distribution. In other words, we need to know the probability that we observe an outcome equal to or smaller than some particular value. If F (X ) represents the cumulative function, we need to calculate F (X = x) = p(X <= x). For discrete distributions, this can be done using simple summation. The cumulative probabilities of our example are shown in the right column of Table 1.2.1. In the inverse transform algorithm, the idea is to sample uniform random deviates (i.e., random numbers between 0 and 1) and to compare each random number against the table of cumulative probabilities. The rst outcome for which the random deviate is smaller than (or is equal to) the associated cumulative probability corresponds to the sampled outcome. Figure 1.2.1 shows an example with a uniform random deviate of U = 0.8 that leads to a sampled outcome X = 6. This process of repeated sampling of uniform deviates and comparing these to the cumulative distribution forms the basis for the inverse transform method for discrete variables. Note that we are applying an inverse function, because we are doing an inverse table lookup. Listing 1.2: Matlab code to simulate sampling of random digits.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

% Simulate the distribution observed in the % human random digit generation task % probabilities for each digit theta = [0.000; ... % digit 0 0.100; ... % digit 1 0.090; ... % digit 2 0.095; ... % digit 3 0.200; ... % digit 4 0.175; ... % digit 5 0.190; ... % digit 6 0.050; ... % digit 7 0.100; ... % digit 8 0.000 ] ... % digit 9 % fix the random number generator seed = 1; rand( 'state' , seed ); % let's say we draw K random values K = 10000; digitset = 0:9; Y = randsample(digitset,K,true,theta);

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES


24 25 26 27 28 29 30 31 32 33

10

% create a new figure figure( 1 ); clf; % Show the histogram of the simulated draws counts = hist( Y , digitset ); bar( digitset , counts , 'k' ); xlim( [ 0.5 9.5 ] ); xlabel( 'Digit' ); ylabel( 'Frequency' ); title( 'Distribution of simulated draws of human digit generator' );

Distribution of simulated draws of human digit generator 2500

2000

Frequency

1500

1000

500

4 Digit

Figure 1.3: Illustration of the BINOMIAL(N, ) distribution where N = 10 and = 0.7.

Exercises
1. Create the Matlab program that implements the inverse tranform method for discrete variables. Use it to sample random digits with probabilities as shown in Table 1.2.1. In order to show that the algorithm is working, sample a large number of random digits and create a histogram. Your program should never sample digits 0 and 9 as they are given zero probability in the table. ** 2 One solution to the previous exercise that does not require any loops is by using the multinomial random number generator mnrnd. Show how to use this function to sample digits according to the probabilities shown in Table 1.2.1.

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES


1 0.9 0.8 0.7 0.6

11

F(X)

0.5 0.4 0.3 0.2 0.1 0 0 1 2 3 4 X 5 6 7 8 9

Figure 1.4: Illustration of the inverse transform procedure for generating discrete random variables. Note that we plot the cumulative probabilities for each outcome. If we sample a uniform random number of U = 0.8, then this yields a random value of X = 6 ** 3 Explain why the algorithm as described above might be inecient when dealing with skewed probability distributions. [hint: imagine a situation where the rst N -1 outcomes have zero probability and the last outcome has probability one]. Can you think of a simple change to the algorithm to improve its eciency?

1.2.2

Inverse transform sampling with continuous variables

The inverse transform sampling approach can also be applied to continuous distributions. Generally, the idea is to draw uniform random deviates and to apply the inverse function of the cumulative distribution applied to the random deviate. In the following, let F (X ) be the cumulative density function (CDF) of our target variable X and F 1(X ) the inverse of this function, assuming that we can actually calculate this inverse. We wish to draw random values for X . This can be done with the following procedure: 1. Draw U Uniform(0, 1) 2. Set X = F 1(U ) 3. Repeat Lets illustrate this approach with a simple example. Suppose we want to sample random numbers from the exponential distribution. When > 0, the cumulative density function is F (x|) = 1 exp(x/). Using some simple algebra, one can nd the inverse of this function, which is F 1 (u|) = log(1 u). This leads to the following sampling procedure to sample random numbers from a Exponental() distribution:

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES 1. Draw U Uniform(0, 1) 2. Set X = log(1 U ) 3. Repeat

12

Exercises
1. Implement the inverse transform sampling method for the exponential distribution. Sample a large number of values from this distribution, and show the distribution of these values. Compare the distribution you obtain against the exact distribution as obtained by the PDF of the exponential distribution (use the command exppdf). ** 2 Matlab implements some of its own functions using Matlab code. For example, when you call the exponential random number generator exprnd, Matlab executes a function that is stored in its own internal directories. Please locate the Matlab function exprnd and inspect its contents. How does Matlab implement the sampling from the exponential distribution? Does it use the inverse transform method? Note that the path to this Matlab function will depend on your particular Matlab installation, but it probably looks something like C:\Program Files\MATLAB\R2009B\toolbox\stats\exprnd.m

1.2.3

Rejection sampling

In many cases, it is not possible to apply the inverse transform sampling method because it is dicult to compute the cumulative distribution or its inverse. In this case, there are other options available, such as rejection sampling, and methods using Markov chain Monte Carlo approaches that we will discuss in the next chapter. The main advantage of the rejection sampling method is that it does not require any burn-in period. Instead, all samples obtained during sampling can immediately be used as samples from the target distribution. One way to illustrate the general idea of rejection sampling (also commonly called the accept-reject algorithm) is with Figure 1.5. Suppose we wish to draw points uniformly within a circle centered at (0, 0) and with radius 1. At rst, it seems quite complicated to directly sample points within this circle in uniform fashion. However, we can apply rejection sampling by rst drawing (x, y ) values uniformly from within the square surrounding the circle, and rejecting any samples for which x2 + y 2 > 1. Note that in this procedure, we used a very simple proposal distribution, such as the uniform distribution, as a basis for sampling from a much more complicated distribution. Rejection sampling allows us to generate observations from a distribution that is dicult to sample from but where we can evaluate the probability of any particular sample. In other words, suppose we have a distribution p(), and it is dicult to sample from this distribution directly, but we can evaluate the probability density or mass p() for a particular value of .

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES


(A) 1 (B) 1

13

0.5

0.5

0.5

y
0.5 0 x 0.5 1

0.5

1 1

1 1

0.5

0 x

0.5

Figure 1.5: Sampling points uniformly from unit circle using rejection sampling The rst choice that the researcher needs to make is the proposal distribution. The proposal distribution is a simple distribution q (), that we can directly sample from. The idea is to evaluate the probability of the proposed samples under both the proposal distribution and the target distribution and reject samples that are unlikely under the target distribution relative to the proposal distribution. Figure 1.6 illustrates the procedure. We rst need to nd a constant c such that cq () p() for all possible samples . The proposal function q () multiplied by the constant c is known as the comparison distribution and will always lie on top of our target distribution. Finding the constant c might be non-trivial, but lets assume for now that we can do this using some calculus. We now draw a number u from a uniform distribution between [0, cq ()]. In other words, this is some point on the line segment between 0 and the height of the comparison distribution evaluated at our proposal . We will reject the proposal if u > p() and accept it otherwise. If we accept the proposal, the sampled value is a draw from the target distribution p(). Here is a summary of the computational procedure: 1. Choose a density q () that is easy to sample from 2. Find a constant c such that cq () p() for all 3. Sample a proposal from proposal distribution q () 4. Sample a uniform deviate u from the interval [0, cq ()] 5. Reject the proposal if u > p(), accept otherwise 6. Repeat steps 3, 4, and 5 until desired number of samples is reached; each accepted sample is a draw from p() The key to an ecient operation of this algorithm is to have as many samples accepted as possible. This depends crucially on the choice of the proposal distribution. A proposal

CHAPTER 1. SAMPLING FROM RANDOM VARIABLES


14

Figure 1.6: Illustration of rejection sampling. The particular sample shown in the gure will be rejected distribution that is dissimilar to the target distribution will lead to many rejected samples, slowing the procedure down.

Exercises
1. Suppose we want to sample from a Beta(, ) distribution where = 2 and = 1. This gives the probability density p(x) = 2x for 0 < x < 1. Assume that for whatever reason, we do not have access to the Beta random number generator that Matlab provides. Instead, the goal is to implement a rejection sampling algorithm in Matlab that samples from this distribution. For this exercise, use a simple uniform proposal distribution (even though this is not a good choice as a proposal distribution). The constant c should be 2 in this case. Visualize the histogram of sampled values and verify that the distribution matches the histogram obtained by using Matlabs betarnd sampling function. What is the percentage of accepted samples? How might we improve the rejection sampler? ** 2 The procedure shown in Figure 1.5 forms the basis for the Box-Muller method for generating Gaussian distributed random variables. We rst generate uniform coordinates (x, y ) from the unit circle using the rejection sampling procedure that rejects any (x, y ) pair with x2 + y 2 > 1. Then, for each pair (x, y ) we evaluate the quantix +y ) x +y ) and z2 = y 2ln( . The values z1 and z2 are each ties z1 = x 2ln( x2 +y2 x2 +y2 Gaussian distributed with zero mean and unit variance. Write a Matlab program that implements this Box-Muller method and verify that the sampled values are Gaussian distributed.
2 2

1/2

1/2

Chapter 2 Markov Chain Monte Carlo


The application of probabilistic models to data often leads to inference problems that require the integration of complex, high dimensional distributions. Markov chain Monte Carlo (MCMC), is a general computational approach that replaces analytic integration by summation over samples generated from iterative algorithms. Problems that are intractable using analytic approaches often become possible to solve using some form of MCMC, even with high-dimensional problems. The development of MCMC is arguably the biggest advance in the computational approach to statistics. While MCMC is very much an active research area, there are now some standardized techniques that are widely used. In this chapter, we will discuss two forms of MCMC: Metropolis-Hastings and Gibbs sampling. Before we go into these techniques though, we rst need to understand the two main ideas underlying MCMC: Monte Carlo integration, and Markov chains.

2.1

Monte Carlo integration

Many problems in probabilistic inference require the calculation of complex integrals or summations over very large outcome spaces. For example, a frequent problem is to calculate the expectation of a function g (x) for the random variable x (for simplicity, we assume x is a univariate random variable). If x is continuous, the expectation is dened as: E [g (x)] = g (x)p(x)dx (2.1)

In the case of discrete variables, the integral is replaced by summation: E [g (x)] = g (x)p(x)dx (2.2)

These expectations arise in many situations where we want to calculate some statistic of a distribution, such as the mean or variance. For example, with g (x) = x, we are calculating the mean of a distribution. Integration or summation using analytic techniques can become quite challenging for certain distributions. For example, the density p(x) might have a 15

CHAPTER 2. MARKOV CHAIN MONTE CARLO

16

functional form that does not lend itself to analytic integration. For discrete distributions, the outcome space might become so large to make the explicit summation over all possible outcomes impractical. The general idea of Monte Carlo integration is to use samples to approximate the expectation of a complex distribution. Specically, we obtain a set of samples x(t), t = 1, . . . , N , drawn independently from distribution p(x). In this case, we can approximate the expectations in 2.1 and 2.2 by a nite sum: 1 E [g (x)] = n
n

g (x ( t ) )
t=1

(2.3)

In this procedure, we have now replaced analytic integration with summation over a suitably large set of samples. Generally, the accuracy of the approximation can be made as accurate as needed by increasing n. Crucially, the precision of the approximation depends on the independence of the samples. When the samples are correlated, the eective sample size decreases. This is not an issue with the rejection sampler discussed in the last chapter, but a potential problem with MCMC approaches.

Exercises
1. Develop Matlab code to approximate the mean of a Beta(, ) distribution with = 3 and = 4 using Monte Carlo integration. You can use the Matlab function betarnd to draw samples from a Beta distribution. You can compare your answer with the analytic solution: /( + ). [Note: this can be done with one line of Matlab code]. 2. Similarly, approximate the variance of a Gamma(a, b) distribution with a = 1.5 and b = 4 by Monte Carlo integration. The Matlab command gamrnd allows you to sample from this distribution. Your approximation should get close to the theoretically derived answer of ab2.

2.2

Markov Chains

A Markov chain is a stochastic process where we transition from one state to another state using a simple sequential procedure. We start a Markov chain at some state x(1), and use a transition function p(x(t)|x(t1)), to determine the next state, x(2) conditional on the last state. We then keep iterating to create a sequence of states: x(1) x(2) . . . x(t) . . . (2.4)

Each such a sequence of states is called a Markov chain or simply chain. The procedure for generating a sequence of T states from a Markov chain is the following: 1. Set t = 1

CHAPTER 2. MARKOV CHAIN MONTE CARLO 2. Generate a initial value u, and set x(t) = u 3. Repeat t=t+1 Sample a new value u from the transition function p(x(t) |x(t1)) Set x(t) = u 4. Until t = T

17

Importantly, in this iterative procedure, the next state of the chain at t + 1 is based only on the previous state at t. Therefore, each Markov chain wanders around the state space and the transition to a new state is only dependent on the last state. It is this local dependency what makes this procedure Markov or memoryless. As we will see, this is an important property when using Markov chains for MCMC. When initializing each Markov chain, the chain will wander in state space around the starting state. Therefore, if we start a number of chains, each with dierent initial conditions, the chains will initially be in a state close to the starting state. This period is called the burnin. An important property of Markov chains is that the starting state of the chain no longer aects the state of the chain after a suciently long sequence of transitions (assuming that certain conditions about the Markov chain are met). At this point, the chain is said to reach its steady state and the states reect samples from its stationary distribution. This property that Markov chains converge to a stationary distribution regardless of where we started (if certain regularity conditions of the transition function are met), is quite important. When applied to MCMC, it allow us to draw samples from a distribution using a sequential procedure but where the starting state of the sequence does not aect the estimation process. Example. Figure 2.1 show an example of a Markov chain involving a (single) continuous variable x. For the transition function, samples were taken from a Beta(200(0.9x(t1) + 0.05), 200(1 0.9x(t1) 0.05)) distribution. This function and its constants are chosen somewhat arbitrarily, but help to illustrate some basic aspects of Markov chains. The process was started with four dierent initial values, and each chain was for continued for T = 1000 iterations. The two panels of the Figure show the sequence of states at two dierent time scales. The line colors represent the four dierent chains. Note that the rst 10 iterations or so show a dependence of the sequence on the initial state. This is the burnin period. This is followed by the steady state for the remainder of the sequence (the chain would continue in the steady state if we didnt stop it). How do we know exactly when the steady state has been reached and the chain converges? This is often not easy to tell, especially in high-dimensional state spaces. We will dier the discussion of convergence until later.

Exercises
1. Develop Matlab code to implement the Markov chain as described in the example. Create an illustration similar to one of the panels in Figure 2.1. Start the Markov

CHAPTER 2. MARKOV CHAIN MONTE CARLO


1 1

18

0.8

0.8

0.6

0.6

0.4

x
0 10 20 30 40 50 t 60 70 80 90 100

0.4

0.2

0.2

100

200

300

400

500 t

600

700

800

900

1000

Figure 2.1: Illustration of a Markov chain starting with four dierent initial conditions. The right and left panes show the sequence of states at dierent temporal scales. chain with four dierent initial values uniformly drawn from [0,1]. [tip: if X is a T x K matrix in Matlab such that X (t, k ) stores the state of the k th Markov chain at the tth iteration, the command plot(X) will simultaneously display the K sequences in dierent colors].

2.3

Putting it together: Markov chain Monte Carlo

The two previous sections discussed the main two ideas underlying MCMC, Monte Carlo sampling and Markov chains. Monte Carlo sampling allows one to estimate various characteristics of a distribution such as the mean, variance, kurtosis, or any other statistic of interest to a researcher. Markov chains involve a stochastic sequential process where we can sample states from some stationary distribution. The goal of MCMC is to design a Markov chain such that the stationary distribution of the chain is exactly the distribution that we are interesting in sampling from. This is called the target distribution 1 . In other words, we would like the states sampled from some Markov chain to also be samples drawn from the target distribution. The idea is to use some clever methods for setting up the transition function such that no matter how we initialize each chain, we will convergence to the target distribution. There are a number of methods that achieve this goal using relatively simple procedures. We will discuss Metropolis, MetropolisHastings, and Gibbs sampling.

2.4

Metropolis Sampling

We will start by illustrating the simplest of all MCMC methods: the Metropolis sampler. This is a special case of the Metropolis-Hastings sampler discussed in the next section.
The target distribution could be the posterior distribution over the parameters in the model or the posterior predictive distribution of a model (i.e., what future data could the model predict?), or any other distribution of interest to the researcher. Later chapters will hopefully make clearer that this abstract sounding concept of the target distribution can be something very important for a model.
1

CHAPTER 2. MARKOV CHAIN MONTE CARLO

19

Suppose our goal is to sample from the target density p(), with < < . The Metropolis sampler creates a Markov chain that produces a sequence of values: (1) (2) . . . (t) . . . (2.5)

where (t) represents the state of a Markov chain at iteration t. The samples from the chain, after burnin, reect samples from the target distribution p(). In the Metropolis procedure, we initialize the rst state, (1) to some initial value. We then use a proposal distribution q (|(t1)) to generate a candidate point that is conditional on the previous state of the sampler2. The next step is to either accept the proposal or reject it. The probability of accepting the proposal is: = min 1, p( ) p((t1) ) (2.6)

To make a decision on whether to actually accept or reject the proposal, we generate a uniform deviate u. If u , we accept the proposal and the next state is set equal to the proposal: (t) = . If u > , we reject the proposal, and the next state is set equal to the old state: (t) = (t1) . We continue generating new proposals conditional on the current state of the sampler, and either accept or reject the proposals. This procedure continues until the sampler reaches convergence. At this point, the samples (t) reect samples from the target distribution p(). Here is a summary of the steps of the Metropolis sampler: 1. Set t = 1 2. Generate a initial value u, and set (t) = u 3. Repeat t=t+1 Generate a proposal from q (|(t1))
( ) Evaluate the acceptance probability = min 1, p(p (t1) )

Generate a u from a Uniform(0,1) distribution If u , accept the proposal and set (t) = , else set (t) = (t1). 4. Until t = T Figure 2.2 illustrates the procedure for a sequence of two states. To intuitively understand why the process leads to samples from the target distribution, note that 2.6 will always accept a new proposal if the the new proposal is more likely under the target distribution than the
The proposal distribution is a distribution that is chosen by the researcher and good choices for the distribution depend on the problem. One important constraint for the proposal distribution is that it should cover the state space such that each potential outcome in state space has some non-zero probability under the proposal distribution
2

CHAPTER 2. MARKOV CHAIN MONTE CARLO

20

old state. Therefore, the sampler will move towards the regions of the state space where the target function has high density. However, note that if the new proposal is less likely than than the current state, it is still possible to accept this worse proposal and move toward it. This process of always accepting a good proposal, and occasionally accepting a bad proposal insures that the sampler explores the whole state space, and samples from all parts of a distribution (including the tails). A key requirement for the Metropolis sampler is that the proposal distribution is symmetric, such that q ( = (t)|(t1) ) = q ( = (t1)|(t) ). Therefore, the probability of proposing some new state given the old state, is the same as proposing to go from the new state back to the old state. This symmetry holds with proposal distributions such as the Normal, Cauchy, Student-t, as well as uniform distributions. If this symmetry does not hold, you should use the Metropolis-Hastings sampler discussed in the next section. A major advantage of the Metropolis sampler is that Equation 2.6 involves only a ratio of densities. Therefore, any terms independent of in the functional form of p() will drop out. Therefore, we do not need to know the normalizing constant of the density or probability mass function. The fact that this procedure allows us to sample from unnormalized distributions is one of its major attractions. Sampling from unnormalized distributions frequently happens in Bayesian models, where calculating the normalization constant is dicult or impractical. Example 1. Suppose we wish to generate random samples from the Cauchy distribution (note that here are better ways to sample from the Cauchy that do not rely on MCMC, but we just use as an illustration of the technique). The probability density of the Cauchy is given by: 1 f ( ) = (2.7) (1 + 2) Because we do not need any normalizing constants in the Metropolis sampler, we can rewrite this to: 1 f ( ) (2.8) (1 + 2 ) Therefore, the Metropolis acceptance probability becomes = min 1, 1 + [ (t) ] 2 1 + [ ]2 ) (2.9)

We will use the Normal distribution as the proposal distribution. Our proposals are generated from a Normal((t), ) distribution. Therefore, the mean of the distribution is centered on the current state and the parameter , which needs to be set by the modeler, controls the variability of the proposed steps. This is an important parameter that we will investigate in the the Exercises. Listing 2.1 show the Matlab function that returns the unnormalized density of the Cauchy distribution. Listing 2.2 shows Matlab code that implements the Metropolis sampler. Figure 2.3 shows the simulation results for a single chain run for 500 iterations. The upper panel shows the theoretical density in the dashed red line and the histogram shows the distribution of all 500 samples. The lower panel shows the sequence of samples of one chain.

CHAPTER 2. MARKOV CHAIN MONTE CARLO

21

& (

'

 

! "

8 D

F I

Figure 2.2: Illustration of the Metropolis sampler to sample from target density p(). (A) the current state of the chain is (t). (B) a proposal distribution around the current state is used to generate a proposal . (C) the proposal was accepted and the new state is set equal to the proposal, and the proposal distribution now centers on the new state.

CHAPTER 2. MARKOV CHAIN MONTE CARLO

22

Exercises
1. Currently, the program in Listing 2.2 takes all states from the chain as samples to approximate the target distribution. Therefore, it also includes samples while the chain is still burning in. Why is this not a good idea? Can you modify the code such that the eect of burnin is removed? 2. Explore the eect of dierent starting conditions. For example, what happens when we start the chain with = 30? 3. Calculate the proportion of samples that is accepted on average. Explore the eect of parameter on the average acceptance rate. Can you explain what is happening with the accuracy of the reconstructed distribution when is varied? 4. As a followup to the previous question, what is (roughly) the value of that leads to a 50% acceptance rate? It turns out that this is the acceptance rate for which the Metropolis sampler, in the case of Gaussian distributions, converges most quickly to the target distribution. 5. Suppose we apply the Metropolis sampler to a Normal(, ) density as the target distribution, with = 0 and = 1. Write down the equation for the acceptance probability, and remove any proportionality constants from the density ratio. [note: the Matlab documentation for normpdf shows the functional form for the Normal density]. ** 6 Modify the code such that the sequences of multiple chains (each initialized dierently) are visualized simultaneously.

Listing 2.1: Matlab function to evaluate the unnormalized Cauchy.


1 2 3

function y = cauchy( theta ) %% Returns the unnormalized density of the Cauchy distribution y = 1 ./ (1 + theta.2);

2.5

Metropolis-Hastings Sampling

The Metropolis-Hasting (MH) sampler is a generalized version of the Metropolis sampler in which we can apply symmetric as well as asymmetric proposal distributions. The MH sampler operates in exactly the same fashion as the Metropolis sampler, but uses the following acceptance probability: p( ) q ((t1)| ) = min 1, (2.10) p((t1) ) q (|(t1) )

CHAPTER 2. MARKOV CHAIN MONTE CARLO

23

Listing 2.2: Matlab code to implement Metropolis sampler for Example 1


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

%% Chapter 2. Use Metropolis procedure to sample from Cauchy density %% Initialize the Metropolis sampler T = 500; % Set the maximum number of iterations sigma = 1; % Set standard deviation of normal proposal density thetamin = 30; thetamax = 30; % define a range for starting values theta = zeros( 1 , T ); % Init storage space for our samples seed=1; rand( 'state' , seed ); randn('state',seed ); % set the random seed theta(1) = unifrnd( thetamin , thetamax ); % Generate start value %% Start sampling t = 1; while t < T % Iterate until we have T samples t = t + 1; % Propose a new value for theta using a normal proposal density theta star = normrnd( theta(t1) , sigma ); % Calculate the acceptance ratio alpha = min( [ 1 cauchy( theta star ) / cauchy( theta(t1) ) ] ); % Draw a uniform deviate from [ 0 1 ] u = rand; % Do we accept this proposal? if u < alpha theta(t) = theta star; % If so, proposal becomes new state else theta(t) = theta(t1); % If not, copy old state end end %% Display histogram of our samples figure( 1 ); clf; subplot( 3,1,1 ); nbins = 200; thetabins = linspace( thetamin , thetamax , nbins ); counts = hist( theta , thetabins ); bar( thetabins , counts/sum(counts) , 'k' ); xlim( [ thetamin thetamax ] ); xlabel( ' \ theta' ); ylabel( 'p(\ theta)' ); %% Overlay the theoretical density y = cauchy( thetabins ); hold on; plot( thetabins , y/sum(y) , 'r' , 'LineWidth' , 3 ); set( gca , 'YTick' , [] ); %% Display history of our samples subplot( 3,1,2:3 ); stairs( theta , 1:T , 'k' ); ylabel( 't' ); xlabel( ' \ theta' ); set( gca , 'YDir' , 'reverse' ); xlim( [ thetamin thetamax ] );

CHAPTER 2. MARKOV CHAIN MONTE CARLO

24

p()
30

20

10

10

20

30

0 50 100 150 200 250 300 350 400 450 500 30

20

10

10

20

30

Figure 2.3: Simulation results where 500 samples were drawn from the Cauchy distribution using the Metropolis sampler. The upper panel shows the theoretical density in the dashed red line and the histogram shows the distribution of the samples. The lower panel shows the sequence of samples of one chain

CHAPTER 2. MARKOV CHAIN MONTE CARLO


(t1)

25

( | ) The MH sampler has the additional ratio of q in 2.10. This corrects for any asymmeq ( |(t1) ) tries in the proposal distribution. For example, suppose we have a proposal distribution with a mean centered on the current state, but that is skewed in one direction. If the proposal distribution prefers to move say left over right, the proposal density ratio will correct for this asymmetry. Here is a summary of the steps of the MH sampler:

1. Set t = 1 2. Generate an initial value u, and set (t) = u 3. Repeat t=t+1 Generate a proposal from q (|(t1))
( ) q ( | ) Evaluate the acceptance probability = min 1, p(p (t1) ) q ( |(t1) )
(t1)

Generate a u from a Uniform(0,1) distribution If u , accept the proposal and set (t) = , else set (t) = (t1). 4. Until t = T The fact that asymmetric proposal distributions can be used allows the MetropolisHastings procedure to sample from target distributions that are dened on a limited range (other than the uniform for which Metropolis sampler can be used). With bounded variables, care should be taken in constructing a suitable proposal distribution. Generally, a good rule is to use a proposal distribution has positive density on the same support as the target distribution. For example, if the target distribution has support over 0 < , the proposal distribution should have the same support.

Exercise
1. Suppose a researcher investigates response times in an experiment and nds that the Weibull(a, b) distribution with a = 2, and b = 1.9 captures the observed variability in response times. Write a Matlab program that implements the Metropolis-Hastings sampler in order to sample response times from this distribution. The pdf for the Weibull is given by the Matlab command wblpdf. Create a gure that is analogous to Figure 2.3. You could use a number of dierent proposal distributions in this case. For this exercise, use samples from a Gamma((t), 1/ ) distribution. This proposal density has a mean equal to (t) so it is centered on the current state. The parameter controls the acceptance rate of the sampler it is a precision parameter such that higher values are associated with less variability in the proposal distribution. Can you nd a value for to get (roughly) an acceptance rate of 50%? Calculate the variance of this distribution using the Monte Carlo approach with the samples obtained from the

CHAPTER 2. MARKOV CHAIN MONTE CARLO

26

Metropolis-Hastings sampler. If you would like to know how close your approximation is, use online resources to nd the analytically derived answer.

2.6

Metropolis-Hastings for Multivariate Distributions

Up to this point, all of the examples we discussed involved univariate distributions. It is fairly straightforward though to generalize the MH sampler to multivariate distributions. There are two dierent ways to extend the procedure to sample random variables in multidimensional spaces.

2.6.1

Blockwise updating

In the rst approach, called blockwise updating, we use a proposal distribution that has the same dimensionality as the target distribution. For example, if we want to sample from a probability distribution involving N variables, we design a N -dimensional proposal distribution, and we either accept or reject the proposal (involving values for all N variables) as a block. In the following, we will use the vector notation = (1, 2 , . . . , N ) to represent a random variable involving N components, and (t) represents the tth state in our sampler. This leads to a generalization of the MH sampler where the scalar variables are now replaced by vectors : 1. Set t = 1 2. Generate an initial value u = (u1 , u2, . . . , uN ), and set (t) = u 3. Repeat t=t+1 Generate a proposal from q ( | (t1))
( ) q ( | ) Evaluate the acceptance probability = min 1, p(p (t1) ) q ( |(t1) )
(t1)

Generate a u from a Uniform(0,1) distribution If u , accept the proposal and set (t) = , else set (t) = (t1). 4. Until t = T Example 1 (adopted from Gill, 2008). Suppose we want to sample from the bivariate exponential distribution p(1 , 2) = exp ((1 + )1 (2 + )2 max(1 , 2)) (2.11)

For our example, we will restrict the range of 1 and 2 to [0,8] and the set the constants to the following: 1 = 0.5, 2 = 0.1, + 0.01, max(1 , 2) = 8. This bivariate density is visualized

CHAPTER 2. MARKOV CHAIN MONTE CARLO

27

Listing 2.3: Matlab code to implement bivariate density for Example 1


1 2 3 4 5 6 7

function y = bivexp( theta1 , theta2 ) %% Returns the density of a bivariate exponential function lambda1 = 0.5; % Set up some constants lambda2 = 0.1; lambda = 0.01; maxval = 8; y = exp( (lambda1+lambda)*theta1(lambda2+lambda)*theta2lambda*maxval );

in Figure 2.4, right panel. The Matlab function that implements this density function is shown in Listing 2.3. To illustrate the blockwise MH sampler, we use a uniform proposal distribution, where proposals for 1 and 2 are sampled from a Uniform(0,8) distribution. In other words, we sample proposals for uniformly from within a box. Note that with this particular proposal distribution, we are not conditioning our proposals on the previous state of the sampler. This is known as an independence sampler. This is actually a very poor q ((t1) | ) proposal distribution but leads to a simple implementation because the ratio q =1 ( |(t1) ) and therefore disappears from the acceptance ratio. The Matlab code that implements the sampler is shown in Listing 2.4. Figure 2.4, left panel shows the approximated distribution using 5000 samples.

Figure 2.4: Results of a Metropolis sampler for the bivariate exponential (right) approximated with 5000 samples (left)

Listing 2.4: Blockwise Metropolis-Hastings sampler for bivariate exponential distribution


1

%% Chapter 2. Metropolis procedure to sample from Bivariate Exponential

CHAPTER 2. MARKOV CHAIN MONTE CARLO


2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

28

% Blockwise updating. Use a uniform proposal distribution %% Initialize the Metropolis sampler T = 5000; % Set the maximum number of iterations thetamin = [ 0 0 ]; % define minimum for theta1 and theta2 thetamax = [ 8 8 ]; % define maximum for theta1 and theta2 seed=1; rand( 'state' , seed ); randn('state',seed ); % set the random seed theta = zeros( 2 , T ); % Init storage space for our samples theta(1,1) = unifrnd( thetamin(1) , thetamax(1) ); % Start value for theta1 theta(2,1) = unifrnd( thetamin(2) , thetamax(2) ); % Start value for theta2 %% Start sampling t = 1; while t < T % Iterate until we have T samples t = t + 1; % Propose a new value for theta theta star = unifrnd( thetamin , thetamax ); pratio = bivexp( theta star(1) , theta star(2) ) / ... bivexp( theta(1,t1) , theta(2,t1) ); alpha = min( [ 1 pratio ] ); % Calculate the acceptance ratio u = rand; % Draw a uniform deviate from [ 0 1 ] if u < alpha % Do we accept this proposal? theta(:,t) = theta star; % proposal becomes new value for theta else theta(:,t) = theta(:,t1); % copy old value of theta end end %% Display histogram of our samples figure( 1 ); clf; subplot( 1,2,1 ); nbins = 10; thetabins1 = linspace( thetamin(1) , thetamax(1) , nbins ); thetabins2 = linspace( thetamin(2) , thetamax(2) , nbins ); hist3( theta' , 'Edges' , {thetabins1 thetabins2} ); xlabel( ' \ theta 1' ); ylabel(' \ theta 2' ); zlabel( 'counts' ); az = 61; el = 30; view(az, el); %% Plot the theoretical density subplot(1,2,2); nbins = 20; thetabins1 = linspace( thetamin(1) , thetamax(1) , nbins ); thetabins2 = linspace( thetamin(2) , thetamax(2) , nbins ); [ theta1grid , theta2grid ] = meshgrid( thetabins1 , thetabins2 ); ygrid = bivexp( theta1grid , theta2grid ); mesh( theta1grid , theta2grid , ygrid ); xlabel( ' \ theta 1' ); ylabel(' \ theta 2' ); zlabel( 'f(\ theta 1,\ theta 2)' ); view(az, el);

CHAPTER 2. MARKOV CHAIN MONTE CARLO

29

In this equation, d( , ) is the Kendall tau distance between two orderings. This distance measures the number of adjacent pairwise swaps that are needed to bring the two orderings into alignment. For example, if = (Adams, W ashington, J efferson, Madison, Monroe), then d( , ) = 1 because one swap is needed to make the two orderings identical. Similarly, if = (Adams, J efferson, W ashington, Madison, Monroe), then d( , ) = 2 because two swaps are needed. Note that in Kendall tau distance, only adjacent items can be swapped. The scaling parameter controls how sharply peaked the distribution of remembered orderings is around the true ordering. Therefore, by increasing , the model makes it more likely that the correct ordering (or something similar) will be produced. The problem is now to generate orderings according to Mallows model, given the true ordering and scaling parameter . This can be achieved in very simple ways using a Metropolis sampler. We start the sampler with (1) corresponding to a random permutation of items. At each iteration, we then make proposals that slightly modify the current state. This can be done in a number of ways. The idea here is to use a proposal distribution where the current ordering is permuted by transposing any randomly chosen pair of items (and not just adjacent items). Formally, we draw proposals from the proposal distribution q ( = | (t1)) = 1/ 0
N 2

Example 2. Many researchers have proposed probabilistic models for order information. Order information can relate to preference rankings over political candidates, car brands and icecream avors, but can also relate to knowledge about the relative order of items along some temporal of physical dimension. For example, suppose we ask individuals to remember the chronological order of US presidents. Steyvers, Lee, Miller, and Hemmer (2009) found that individuals make a number of mistakes in the ordering of presidents that can be captured by simple probabilistic models, such as Mallows model. To explain Mallows model, lets give a simple example. Suppose that we are looking at the rst ve presidents: Washington, Adams, Jeerson, Madison, and Monroe. We will represent this true ordering by a vector = (1, 2, 3, 4, 5) = (W ashington, Adams, J efferson, Madison, Monroe). Mallows model now proposes that the remembered orderings tend to be similar to the true ordering, with very similar orderings being more likely than dissimilar orderings. Specically, according to Mallows model, the probability that an individual remembers an ordering is proportional to: p( | , ) exp (d( , )) (2.12)

if S ( , (t1)) = 1 otherwise

(2.13)

where S ( , (t1)) is the Cayley distance. This distance counts the number of transpositions of any pair of items needed to bring two orderings into alignment (therefore, the dierence with the Kendall tau distance is that any pairwise swap counts as one, even nonadjacent swaps). This is just a complicated way to describe a very simple proposal distribution: just swap two randomly chosen items from the last ordering, and make that the proposed ordering. Because the proposal distribution is symmetric, we can use the Metropolis sampler. The acceptance probability is

CHAPTER 2. MARKOV CHAIN MONTE CARLO

30

= min 1,

The Matlab implementation of the Kendall tau distance function is shown in Listing 2.5. The Matlab code for the Metropolis sampler is shown in Listing 2.6. Currently, the code does not do all that much. It simply shows what the state is every 10 iterations for a total of 500 iterations. Here is some sample output from the program: Listing 2.5: Function to evaluate Kendall tau distance
1 2 3 4 5 6 7 8 9 10 11 12 13 14

p( | , ) p( (t1)| , )

= min 1,

exp (d( , )) exp (d( (t1), ))

(2.14)

function tau = kendalltau( order1 , order2 ) %% Returns the Kendall tau distance between two orderings % Note: this is not the most efficient implementation [ dummy , ranking1 ] = sort( order1(:)' , 2 , 'ascend' ); [ dummy , ranking2 ] = sort( order2(:)' , 2 , 'ascend' ); N = length( ranking1 ); [ ii , jj ] = meshgrid( 1:N , 1:N ); ok = find( jj(:) > ii(:) ); ii = ii( ok ); jj = jj( ok ); nok = length( ok ); sign1 = ranking1( jj ) > ranking1( ii ); sign2 = ranking2( jj ) > ranking2( ii ); tau = sum( sign1 = sign2 );

Listing 2.6: Implementation of Metropolis-Hastings sampler for Mallows model


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

%% Chapter 2. Metropolis sampler for Mallows model % samples orderings from a distribution over orderings %% Initialize model parameters lambda = 0.1; % scaling parameter labels = { 'Washington' , 'Adams' , 'Jefferson' , 'Madison' , 'Monroe' }; omega = [ 1 2 3 4 5 ]; % correct ordering L = length( omega ); % number of items in ordering %% Initialize the Metropolis sampler T = 500; % Set the maximum number of iterations seed=1; rand( 'state' , seed ); randn('state',seed ); % set the random seed theta = zeros( L , T ); % Init storage space for our samples theta(:,1) = randperm( L ); % Random ordering to start with %% Start sampling t = 1; while t < T % Iterate until we have T samples t = t + 1; % Our proposal is the last ordering but with two items switched

CHAPTER 2. MARKOV CHAIN MONTE CARLO


22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

31

lasttheta = theta(:,t1); % Get the last theta % Propose two items to switch whswap = randperm( L ); whswap = whswap(1:2); theta star = lasttheta; theta star( whswap(1)) = lasttheta( whswap(2)); theta star( whswap(2)) = lasttheta( whswap(1)); % calculate Kendall tau distances dist1 = kendalltau( theta star , omega ); dist2 = kendalltau( lasttheta , omega ); % Calculate the acceptance ratio pratio = exp( dist1*lambda ) / exp( dist2*lambda ); alpha = min( [ 1 pratio ] ); u = rand; % Draw a uniform deviate from [ 0 1 ] if u < alpha % Do we accept this proposal? theta(:,t) = theta star; % proposal becomes new theta else theta(:,t) = lasttheta; % copy old theta end % Occasionally print the current state if mod( t,10 ) == 0 fprintf( 't=%3d \ t' , t ); for j=1:L fprintf( '%15s' , labels { theta(j,t)} ); end fprintf( ' \ n' ); end end

t=400 t=410 t=420 t=430 t=440 t=450 t=460 t=470 t=480 t=490 t=500

Jefferson Washington Washington Jefferson Washington Jefferson Washington Monroe Adams Adams Monroe

Madison Monroe Jefferson Monroe Madison Washington Jefferson Washington Washington Madison Adams

Adams Madison Madison Washington Monroe Adams Adams Jefferson Monroe Jefferson Madison

Monroe Jefferson Adams Adams Adams Madison Madison Adams Jefferson Monroe Jefferson

Washington Adams Monroe Madison Jefferson Monroe Monroe Madison Madison Washington Washington

2.6.2

Componentwise updating

A potential problem with the blockwise updating approach is that it might be dicult to nd suitable high-dimensional proposal distributions. A related problem is that blockwise updating can be associated with high rejection rates. Instead of accepting or rejecting a

CHAPTER 2. MARKOV CHAIN MONTE CARLO

32

proposal for involving all its components simultaneously, it might be computationally simpler to make proposals for individual components of , one at a time. This leads to a componentwise updating approach. For example, suppose we have a bivariate distribution = (1, 2). We rst initialize (1) (1) the sampler with some suitable values for 1 and 2 . At each iteration t, we rst make (t1) a proposal 1 depending on the last state 1 . We then evaluate the acceptance ratio (t1) (t1) (t1) comparing the likelihood of (1 , 2 ) against (1 , 2 ). Note that in this proposal, we have only varied the rst component and kept the second component constant. In the (t1) next step, we make a proposal 2 depending on the last state 2 . We then evaluate the (t) (t) (t1) acceptance ratio comparing the likelihood of (1 , 2 ) against (1 , 2 ). Importantly, in this second step, we are holding the rst component constant, but this is the updated value from the rst step. Therefore, what happens in the second step is conditional on what happens in the rst step. Here is a summary of the steps for a bivariate componentwise MH sampler: 1. Set t = 1 2. Generate an initial value u = (u1 , u2, . . . , uN ), and set (t) = u 3. Repeat t=t+1
Generate a proposal 1 from q (1|1 (t1)

)
(t1) (t1) p(1 ,2 ) , p(1 2 (t1)

Evaluate the acceptance probability = min 1, Generate a u from a Uniform(0,1) distribution

) |1 (t1) q (1 |1 )

q (1

(t1)

If u , accept the proposal and set 1 = 1 , else set 1 = 1 Generate a proposal 2 from q (2|2 (t1)

(t)

(t)

(t1)

)
) p(1 ,2 (t) (t1) p(1 ,2 ) (t )

Evaluate the acceptance probability = min 1, Generate a u from a Uniform(0,1) distribution

q (2

) |2 ( t 1) | q (2 ) 2

(t1)

If u , accept the proposal and set 2 = 2 , else set 2 = 2

(t)

(t)

(t1)

4. Until t = T Example 3. We will illustrate the componentwise sampler by sampling from a multivariate normal distribution. Please note that this just serves as an example there are much better and direct ways to sample from a multivariate normal distribution. The multivariate normal distribution is parametrized by and . The parameter is a 1 x N vector containing the mean in the N -dimensional space. The parameter is a N x N covariance 0 .3 matrix. For our example, we will use a bivariate normal with = (0, 0) and = ( 01 .3 1 ).

CHAPTER 2. MARKOV CHAIN MONTE CARLO

33

The density of this distribution is illustrated in the right panel of Figure 2.5. The goal is to sample values from this distribution. We use a componentwise Metropolis sampler with a normal distribution for the components 1 and 2. The proposal distribution is centered around the old value of each component and has standard deviation . The code in Listing 2.7 shows the Matlab implementation for this sampler. The output of this code is shown in Figure 2.5.

Figure 2.5: Results of the componentwise Metropolis sampler for the bivariate normal distribution. Left panel shows the approximation using 5000 samples. Right panel shows the theoretical density.

Listing 2.7: Implementation of componentwise Metropolis sampler for bivariate normal


1 2 3 4 5 6 7 8 9 10 11 12 13 14

%% Chapter 2. Metropolis procedure to sample from Bivariate Normal % Componentwise updating. Use a normal proposal distribution %% Parameters of the Bivariate normal mu = [ 0 0 ]; sigma = [ 1 0.3; ... 0.3 1 ]; %% Initialize the Metropolis sampler T = 5000; % Set the maximum number of iterations propsigma = 1; % standard dev. of proposal distribution thetamin = [ 3 3 ]; % define minimum for theta1 and theta2 thetamax = [ 3 3 ]; % define maximum for theta1 and theta2 seed=1; rand( 'state' , seed ); randn('state',seed ); % set the random seed

CHAPTER 2. MARKOV CHAIN MONTE CARLO


15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

34

state = zeros( 2 , T ); % Init storage space for the state of the sampler theta1 = unifrnd( thetamin(1) , thetamax(1) ); % Start value for theta1 theta2 = unifrnd( thetamin(2) , thetamax(2) ); % Start value for theta2 t = 1; % initialize iteration at 1 state(1,t) = theta1; % save the current state state(2,t) = theta2; %% Start sampling while t < T % Iterate until we have T samples t = t + 1; %% Propose a new value for theta1 new theta1 = normrnd( theta1 , propsigma ); pratio = mvnpdf( [ new theta1 theta2 ] , mu , sigma ) / ... mvnpdf( [ theta1 theta2 ] , mu , sigma ); alpha = min( [ 1 pratio ] ); % Calculate the acceptance ratio u = rand; % Draw a uniform deviate from [ 0 1 ] if u < alpha % Do we accept this proposal? theta1 = new theta1; % proposal becomes new value for theta1 end %% Propose a new value for theta2 new theta2 = normrnd( theta2 , propsigma ); pratio = mvnpdf( [ theta1 new theta2 ] , mu , sigma ) / ... mvnpdf( [ theta1 theta2 ] , mu , sigma ); alpha = min( [ 1 pratio ] ); % Calculate the acceptance ratio u = rand; % Draw a uniform deviate from [ 0 1 ] if u < alpha % Do we accept this proposal? theta2 = new theta2; % proposal becomes new value for theta2 end %% Save state state(1,t) = theta1; state(2,t) = theta2; end %% Display histogram of our samples figure( 1 ); clf; subplot( 1,2,1 ); nbins = 12; thetabins1 = linspace( thetamin(1) , thetamax(1) , nbins ); thetabins2 = linspace( thetamin(2) , thetamax(2) , nbins ); hist3( state' , 'Edges' , {thetabins1 thetabins2} ); xlabel( ' \ theta 1' ); ylabel(' \ theta 2' ); zlabel( 'counts' ); az = 61; el = 30; view(az, el); %% Plot the theoretical density subplot(1,2,2); nbins = 50; thetabins1 = linspace( thetamin(1) , thetamax(1) , nbins ); thetabins2 = linspace( thetamin(2) , thetamax(2) , nbins );

CHAPTER 2. MARKOV CHAIN MONTE CARLO


66 67 68 69 70 71 72 73

35

[ theta1grid , theta2grid ] = meshgrid( thetabins1 , thetabins2 ); zgrid = mvnpdf( [ theta1grid(:) theta2grid(:)] , mu , sigma ); zgrid = reshape( zgrid , nbins , nbins ); surf( theta1grid , theta2grid , zgrid ); xlabel( ' \ theta 1' ); ylabel(' \ theta 2' ); zlabel( 'pdf(\ theta 1,\ theta 2)' ); view(az, el); xlim([thetamin(1) thetamax(1)]); ylim([thetamin(2) thetamax(2)]);

Exercises
1. Modify the code for Example 1 to implement a component-wise sampler. Use a uniform proposal distribution for both 1 and 2 . Visualize the distribution in the same way as Figure 2.4. Calculate the acceptance probability separately for 1 and 2 . Why is the acceptance probability for 2 higher than for 1 ? 2. Investigate the eect of the scaling parameter in Mallows model. How do the sampled orders change as a function of this parameter? 3. Modify the Metropolis sampler for Mallows model and sample from a 10 as opposed to 5 dimensional space of orderings. ** 4 The purpose of this exercise is to compare the approximated distribition under Mallows with the exact distribution. Note that Equation 2.12 showed the probability for Mallows model but dropped the constant of proportionality. The exact probability under Mallows model is p( | , ) = (1) exp (d( , )) where () is a normalizing
1exp(i) function where () = N i=1 1exp() . Therefore, it is possible with small N to calculate the exact probability of any ordering . Run the sampler in Listing 2.6 and calculate a frequency distribution of samples (i.e., what number of times does each unique sequence occur in the sampler?). Compare these frequencies against the exact probability under Mallows model.

2.7

Gibbs Sampling

A drawback of the Metropolis-Hastings and rejection samplers is that it might be dicult to tune the proposal distribution. In addition, a good part of the computation is performed producing samples that are rejected and not used in the approximation. The Gibbs sampler is a procedure in which all samples are accepted, leading to improved computational eciency. An additional advantage is that the researcher does not have to specify a proposal distribution, leaving some guessing work out of the MCMC procedure. However, the Gibbs sampler can only be applied in situations where we know the full conditional distributions of each component in the multivariate distribution conditioned on all other components. In some cases, these conditional distributions are simply not known,

CHAPTER 2. MARKOV CHAIN MONTE CARLO

36

and Gibbs sampling cannot be applied. However, in many Bayesian models, distributions are used that lend themselves to Gibbs sampling. To illustrate the Gibbs sampler, we look at the bivariate case where we have a joint distribution f (1 , 2). The key requirement for the Gibbs sampler is that we can derive the (t) (t) two conditional distributions f (1 |2 = 2 ) and f (2 |1 = 1 ) that is, the distribution of each variable conditioned on a specic realization of the other variable. It is also required that we can sample from these distributions. We rst initialize the sampler with some (1) (1) suitable values for 1 and 2 . At each iteration t, we follow steps that are very similar to (t) the componentwise MH sampler. In the rst step, we rst sample a new value for 1 that (t1) is conditional on 2 , the previous state of the other component. We do this by sampling (t1) a proposal from f (1 |2 = 2 ). As opposed to Metropolis-Hastings, we will always accept this proposal so the new state can be immediately updated. In the second step, we sample (t) (t) a new value for 2 that is conditional on 1 , the current state of the other component. (t) We do this by sampling a proposal from f (2 |1 = 1 ). Therefore, the procedure involves iterative conditional sampling we keep going back and forth by sampling the new state of a variable by conditioning on the current values for the other component. Here is a summary of the Gibbs sampling procedure: 1. Set t = 1 2. Generate an initial value u = (u1 , u2), and set (t) = u 3. Repeat t=t+1 Sample 1 from the conditional distribution f (1 |2 = 2
(t) (t) (t1) (t)

Sample 2 from the conditional distribution f (2 |1 = 1 ) 4. Until t = T Example 1. In Example 3 from the previous section, we applied a Metropolis sampler for the bivariate normal distribution. This distribution can be sampled more eciently using Gibbs sampling (although still not as eciently as with direct methods). We start with = (1 , 2) having a bivarite normal distribution Norm(, ) (2.15)

with mean vector = (1 , 2 ) and covariance matrix . For this example, lets assume the following covariance structure: 1 = (2.16) 1 Using analytic methods, we can derive the conditional distributions for each component of conditional on a realization for the other component: 1 Norm(1 + (2 2 ), 1 2 ) (2.17)

CHAPTER 2. MARKOV CHAIN MONTE CARLO 2 Norm(2 + (1 1 ), This leads to the following Gibbs sampling procedure: 1. Set t = 1 2. Generate an initial value u = (u1 , u2), and set (t) = u 3. Repeat t=t+1 Sample 1 from Norm(1 + (2
(t) (t) (t1)

37 1 2 ) (2.18)

Sample 2 from Norm(2 + (1 1 ), 4. Until t = T

(t)

2 ),

1 2) 1 2 )

Figure 2.6 shows the results of a simulation with = (0, 0) and = 0.7. The sampler was run with a single chain for 5000 iterations. Instead of producing 3D histograms as in Figure 2.7, this gure just shows a scatter plot of all individual samples (right panel) and a progression of the sampler state for the rst 20 iterations (left panel).
1.5 1 0.5 0 1 0.5 4 3 2

0 1

1 1.5 2 2.5 3 3 2 3 4 4

Figure 2.6: Results of a Gibbs sampler for the bivariate normal distribution. Left panel shows the progression of the sampler state for the rst 20 iterations. Right panel shows a scatterplot of 5000 sampled values.

CHAPTER 2. MARKOV CHAIN MONTE CARLO

38

Exercises
1. Implement the Gibbs sampler from Example 1 in Matlab and produce an illustration similar to Figure 2.6. You can use the parameters = (0, 0) and = 0.7. What happens when = 0.7? 2. Compare the output you get from the Gibbs sampler with random samples that are drawn from Matlabs own multivariate normal random number generator (using command mvnrnd).

Chapter 3 Basic concepts in Bayesian Data Analysis


Up to this point, we have illustrated some Markov chain Monte Carlo techniques but it might not be clear how this is useful for the purpose of modeling or data analysis. In this chapter, we will show that MCMC can be used in the context of Bayesian data analysis. The goal will be to nd parameter values in a probabilistic model that best explain the data. We will start with some very simple probabilistic models that involve a few parameters. Through examples, we can see how to t models to data from a Bayesian perspective. In the next chapter, we will generalize these results to a class of probabilistic models that can be dened by directed edges among variables (so called directed graphical models). To start, lets assume that the data we want to analyze can be represented by D, and that the parameters of a model are represented by . Lets also assume that we have a model that is designed to explain the data in a generative sense. This means that we can dene the likelihood p(D|) of the model. This is essentially how likely the data is according to some instantiation of the model in terms of its model parameters . Note the direction of this conditional probability. The likelihood gives an explanation of the data in terms of the parameters. In this coursebook, we will assume that it will be possible to evaluate the probability mass or density of this likelihood. Therefore, given some instantiation of the parameters, we should be able, given the denition of our model, to say what the likelihood is 1 . The primary goal in any modeling analysis is to choose parameters of a model such that we can best explain the data with the model. There are a number of approaches to choose model parameters.
1 Note that in some models, the likelihood cannot be evaluated. These might involve probabilistic models in which there only is a process that can be used to simulate how samples of the data can be generated. In these cases, we need to resort to dierent techniques such as approximate bayesian inference, a topic that is outside the scope of this course book

39

CHAPTER 3. BASIC CONCEPTS IN BAYESIAN DATA ANALYSIS

40

3.1
3.1.1

Parameter Estimation Approaches


Maximum Likelihood

A very basic approach to parameter estimation is maximum likelihood (ML). The goal here is to nd the parameter estimates that maximize the likelihood of the data given the model that operates with those parameter estimates. This corresponds to: M L = arg max p(D|i )
i

where the values i vary across the range of allowable values. The ML estimate corresponds to the values of the parameters that best agree with the actually observed data. Although this seems like a very straightforward way of estimating parameters in a probabilistic model, there are some serious drawbacks to this approach. For example, when we deal with small samples sizes, the ML estimate might not even be dened. It might also be nontrivial to nd the maximum of the likelihood function when there are many parameters in the model. In this case, numerical techniques that explore the gradient of the likelihood function might get stuck in suboptimal solutions (e.g., local modes) or show a sensitivity to the choice of starting values. For our purposes, it is most important to realize that this estimation procedure does not correspond to a Bayesian approach.

3.1.2

Maximum a posteriori

A Bayesian approach to parameter estimation is to treat as a random variable. We associate a prior distribution with this variable: p(). The prior distribution tells us how likely the parameter values are when we have not observed any data yet. In a Bayesian context, the goal is to estimate the posterior distribution over parameters given our data. This is also known as posterior inference. In other words, we would like to know p(|D). Using Bayes rule, we can evaluate this conditional probability with: p(|D) = p(D|)p() p(D) (3.1)

For our purposes, we rarely need to explicitly evaluate the denominator p(D). Therefore, we can ignore any constant of proportionality and just write: p(|D) p(D|) p()
posterior likelihood prior

(3.2)

Note that the posterior distribution is specied by a simple product of the likelihood (how likely is the data given the model that uses these parameter estimates) and the prior (how likely are these parameter values to begin with?). In Bayesian data analysis, one way to apply a model to data is to nd the maximum a posteriori (MAP) parameter values. The goal here is to nd the parameter estimates that maximize the posterior probability of the

CHAPTER 3. BASIC CONCEPTS IN BAYESIAN DATA ANALYSIS

41

parameters given the data. In other words, we nd the mode of the posterior distribution. This corresponds to: M AP = arg max p(i |D)
i

= arg max
i i

p(D|i )p(i ) p(D)

(3.3)

= arg max p(D|i )p(i ) This looks similar to the maximum likelihood estimation procedure. The dierence is that the prior over parameters will inuence parameter estimation. This procedure will nd parameters that make the data likely (a large likelihood) but that also are likely to begin with (large prior probability). Although it seems that the prior should not play a role in parameter estimation, there are some good reasons to prefer the MAP over the ML estimation, especially when only few data points are available or when the data is degenerate in some way (e.g., the data shows no variation). There is a large literature on this, and we will not review it here.

3.1.3

Posterior Sampling

Finally, the Bayesian approach that we are really interested in is posterior sampling. Note that with the MAP approach, we get a single set of parameter values for a model. Therefore, we are characterizing the posterior distribution with the mode of this distribution. This seems conceptually very clear and useful but it has some drawbacks. For example, suppose there are multiple sets of parameter values that all have very high posterior probability. Shouldnt we try to nd these other sets of parameter values as well? Also, suppose there are tradeos between parameter values. For example, suppose we have a model with two parameters A and B and we achieve high posterior probability by either setting A to a high value and B to a low value or the other way around, setting A to a low and B to a high value. In a MAP estimate, we are not getting an understanding of such parameter correlations, but we can measure such correlations using posterior sampling. In a more comprehensive Bayesian approach, the goal is to characterize the full posterior distribution and not to simply nd the mode of this distribution. In some cases, we might be able to nd an analytic expression for the posterior distribution. However, in many cases, we have to resort to sampling techniques, such as MCMC, to get samples from the posterior distribution. These samples can be used to calculate a number of things, such as means, variances and other moments of the distribution. We can also check whether there are any correlations between parameters. Here is one key insight: if you understand that MCMC can be used to draw samples from a distribution, you should realize that MCMC can be used to get samples from the posterior distribution. Therefore, all the methods that we have discussed in the previous chapter can be used for the problem of posterior inference.

CHAPTER 3. BASIC CONCEPTS IN BAYESIAN DATA ANALYSIS

42

3.2

Example: Estimating a Weibull distribution

Suppose we have some univariate data that consist of response time measurements in an experiment. To model this data, lets assume that the Weibull distribution will be a useful descriptive model. The pdf for the Weibull is: p(y |a, b) = bab y b1 e( a ) I(0,inf) (y )
y b

(3.4)

This distribution has two parameters: a controls the scale of the distribution and b controls the shape of the distribution. Lets say that our data is represented by yi , where i 1..N , and N is the number of observations. In our model, lets assume that each observation is an independent draw from a Weibull distribution. Given these assumptions, we now have a likelihood function for our data. Given that each observation is assumed to be an independent draw from a Weibull with parameters a and b, the likelihood of all observations is given by: p(y1 , ..., yN |a, b) = p(y1 |a, b)p(y2|a, b)...p(yN |a, b) =
i

p(yi |a, b)
b1 bab yi e
yi b ( a )

(3.5) I(0,inf) (yi )

=
i

We are not completely done with our model description however. In any Bayesian analysis, we also need to specify prior distributions over parameters. Lets assume that parameters a and b are independently distributed according to an exponential distribution with parameters a and b respectively. The pdfs for these prior distributions are: a 1 p(a|a ) = e a a (3.6) 1 b p(a|b ) = e b b We should point out that the model expressed above can be described in much simpler terms using sampling notation: yi |a, b Weibull(a, b) a Exp(a ) b Exp(b ) (3.7)

where we simply have used the terms Weibull amd Exp as notations for the full functional form in Equation 3.4 and Equation 3.6. We now have a fully specied model where we have a likelihood as well as a prior on all parameters. The goal is to do posterior inference and nd the posterior distribution over parameters conditional on the data. We previously stated that: p(|D) p(D|) p()
posterior likelihood prior

(3.8)

CHAPTER 3. BASIC CONCEPTS IN BAYESIAN DATA ANALYSIS In our case, =(a,b) and D=(y1 ,...,yN ). Therefore, we can write: p(a, b|y1, ..., yN ) p(y1 , ..., yN |a, b) p(a, b)
posterior likelihood prior

43

(3.9)

Note that we have decided to use independent priors over parameters a and b. This allows us to write: p(a, b|y1, ..., yN ) p(y1 , ..., yN |a, b) p(a)p(b) (3.10)
posterior likelihood prior

To see what the functional form of this posterior distribution looks like, we can insert equations 3.5 into the likelihood term and 3.6 into the prior terms. This leads to p(a, b|y1, ..., yN )
posterior b1 ( a ) bab yi e I(0,inf) (yi ) i likelihood
yi b

1 a 1 b e a e b a b
prior

(3.11)

This expression is not amenable to analytic techniques. However, we can use sampling techniques such as MCMC to draw samples from this posterior distribution. It is important to realize that although the expression in 3.11 looks daunting, we can evaluate the unnormalized density of this function for any set of parameter values a and b and observations y1,...,yN (remember that we only need unnormalized densities in MCMC, which is an appealing feature of this approach). Our posterior distribution is bivariate as it involves our two parameters a and b. Suitable MCMC approaches are the Gibbs sampler and the bivariate MH sampler from the previous chapter. Because the conditional distributions in 3.11 do not correspond to any analytic expression, we have to use a Metropolis Hastings sampler. We will choose a component-wise updating scheme. Here is a high-level description of the MH procedure: 1. Set t = 1 2. Generate an initial value for a and b 3. Repeat t=t+1 Do a MH step on a Generate a proposal a from a suitable proposal distribution Evaluate the acceptance probability using a, a, and b Generate a u from a Uniform(0,1) distribution Do a MH step on b If u , accept the proposal and set a = a

CHAPTER 3. BASIC CONCEPTS IN BAYESIAN DATA ANALYSIS Generate a proposal b from a suitable proposal distribution Evaluate the acceptance probability using b , b, and a Generate a u from a Uniform(0,1) distribution If u , accept the proposal and set b = b 4. Until t = T

44

Exercises
1. What might be suitable proposal distributions for a and b in the MH sampler outlined above? 2. Give the matlab code for evaluating the acceptance probability for the two MH steps in the MH sampler? There is no need to write out the full functional form. Just use matlab functions such as wblpdf and exppdf in your expression. 3. The likelihood ratio in the expression for the acceptance probability of the MH sampler can often contain products over a large set of terms. For example, we might have the Q i p(yi |a,b) Q expression p(yi |a ,b) where we rst evaluate the product in numerator, then the prodi uct in denominator and then take the ratio of these products. This is mathematically ) equivalent to i pp((yyii||aa,b ,b) where we take each individual ratio and then multiply. For computational reasons, why is it better to use the latter approach? 4. Implement the full Metropolis-Hastings sampler from this example in Matlab. If you get stuck, you can use the supplied code ch3 posteriorinference weibull.m. You can use a = b = 1. For the data y , you can use the dataset provided in the matlab le rtdata1 supplied on the website. In this dataset, there are 100 observations total. 5. With your MH sampler, estimate the mean of a and b in the posterior distribution 6. With the statistics toolbox, you can also get parameter estimates from a maximum likelihood procedure. Use the wblfit to nd the ML estimates for the dataset provided by in the matlab le rtdata1. Are the answers you get from ML dierent from the Bayesian procedure? 7. Assume that you have very sparse data. Instead of 100 observations, you now have only one observation. Lets say y1 = 2 and N =1. What are the mean posterior estimates you get for this data using your MH procedure? What does matlabs maximum likelihood procedure give you? ** 8 Explore the eect of the parameters of the prior, 1 and 2 .

CHAPTER 3. BASIC CONCEPTS IN BAYESIAN DATA ANALYSIS

45

3.3
tba

Example: Logistic Regression

3.4
tba

Example: Mallows Model

Chapter 4 Directed Graphical Models


Graphical models are a general class of probabilistic models that allow researchers to reason with uncertain data in the presence of latent variables. Latent variables in cognitive science can include any information that is unobservable, such as attentional states, knowledge representations, contents of memory, and brain states. In short, anything that is relevant to explain the observed data but was itself not observed, can become a latent variable in a graphical model. In a graphical model, the researcher posits certain local statistical dependencies between the random variables that correspond to the latent variables and observed data. We will restrict ourselves to directed graphical models, in which the statistical dependency between random variables is based on directional relationships. These models are also known as Bayesian networks and belief networks, although these terms all have somewhat dierent connotations. Another class of graphical modes, not discussed here, are undirected graphical models, such as Markov random elds. These models are useful when it is natural to express the relationship between variables using undirected relationships. It is important to realize that graphical models form a very large class of models. Many models that are typically not described as graphical models can be reinterpreted within a graphical modeling framework. For example, many forms of neural networks can be usefully analyzed with graphical models. Similarly, many process models proposed by cognitive psychologists often are framed as stochastic processes that can be couched as graphical models. In this chapter, we will rst review some basic rules and concepts related to probability theory. We will then go into the important concepts of graphical models that are needed to solve inference problems.

4.1

A Short Review of Probability Theory

There are a few basic rules that form the basis for all probabilistic machinery to solve inference problems in graphical models. Here, we just briey review these rules. For an in depth introduction into probability theory, you can consult a number of books and online 46

CHAPTER 4. DIRECTED GRAPHICAL MODELS

47

resources. In the sum rule, we can write the marginal probability of x as a sum over the joint distribution of x and y where we sum over all possibilities of y , p(x) =
y

p(x, y ).

(4.1)

In the product rule, we can rewrite a joint distribution as a product of a conditional and marginal probability p(x, y ) = p(y |x)p(x) = p(x|y )p(y ). (4.2)

In the chain rule, the product rule is applied repeatedly to give expressions for the joint probability involving more than two variables. For example, the joint distribution over three variables can be factorized into a product of conditional probabilities: p(x, y, z ) = p(x|y, z )p(y, z ) = p(x|y, z )p(y |z )p(z ). p(x|y )p(y ) p(x) p(x|y )p(y ) = y p(x|y )p(y ) (4.3)

From these rules, we can obtain Bayes rule, which plays a central role in graphical models: p(y |x) =

(4.4)

Importantly, using approximate inference techniques, we rarely need to explicitly evaluate the denominator p(x). Instead, we can just evaluate the conditional probability up to a constant of proportionality: p(y |x) p(x|y )p(y ) (4.5) Finally, there are the concepts of independence and conditional independence. Two variables are said to be independent if their joint distribution factorizes into a product of two marginal probabilities: p(x, y ) = p(x)p(y ). (4.6) Note that if two variables are uncorrelated, that does not mean they are statistically independent. There are many ways to measure statistical association between variables and correlation is just one of them. However, if two variables are independent, this will ensure there is no correlation between them. Another consequence of independence is that if x and y are independent, the conditional probability of x given y is just the probability of x: p(x|y ) = p(x). (4.7)

CHAPTER 4. DIRECTED GRAPHICAL MODELS


` W b c d V ` e U V W X Y ` W a a

48

` T R

Figure 4.1: A graphical model that illustrates the conditional dependencies between variables. In other words, by conditioning on a particular y , we have learned nothing about x because of independence. Two variables x and y and said to be conditionally independent of z if the following holds: p(x, y |z ) = p(x|z )p(y |z ). (4.8) Therefore, if we learn about z , x and y become independent. Another way to write that x and y are conditionally independent of z is p(x|z, y ) = p(x|z ). (4.9)

In other words, if we condition on z , and now also learn about y , this is not going to change the probability of x. It is important to realize that conditional independence between x and y does not imply independence between x and y . We will see some examples of the dierence between independence and conditional independence in the context of graphical models.

Exercises
1. Use the chain rule to give the general factorization of a joint distribution involving 4 variables

4.2

The Burglar Alarm Example

In the following example, we will give a classic example of a graphical model from the AI and Machine Learning literature: the burglar alarm network. This example has been described in many sources, and originates from Judea Pearls early work on reasoning with uncertainty. In this example, consider a burglar alarm installed in your home. This alarm is set o somewhat reliably by the presence of a burglar. However, it can also be set o by an earthquake. Currently, you are traveling in your car so you cannot directly hear the alarm. However, your neighbor has said that she will call you on your cell phone if she hears the

CHAPTER 4. DIRECTED GRAPHICAL MODELS

49

alarm. Finally, while you are traveling in your car, you are also listening to the news on the radio and presumably, if there was an earthquake, you would probably hear about it on the radio. Suppose you now get a call from your neighbor, what is the probability that there is a burglar in your house? This example sets up a number of issues on how we should reason with uncertainty when there are multiple variables involved. To model this situation, we can use a graphical model as shown in Figure 4.1. There are ve random variables in this example: earthquake, burglar, alarm, neighbor call and radio report. For simplicity, lets assume these are binary variables; either the alarm goes o or it doesnt, the neighbor calls, or she doesnt, etc. The illustration also shows how we can replace these words by letter codes (e.g., e, b, etc). In the graphical model, the nodes are random variables and arrows indicate conditional dependencies between variables. For example, the network encodes the intuition that the status of the alarm depends on earthquakes and burglars, and that the neighbors call depends on the alarm. It is useful to think of these conditional dependencies as causal relationships between variables the burglar might cause the alarm to go o, which in turn might cause your neighbor to call. This is how Judea Pearl originally thought of graphical models as ways to reason probabilistically about causes and eects. However, you should keep in mind that graphical models can also be constructed in the absence of any causal interpretation. The most important idea behind graphical models is that the graph structure implies a number of independence and conditional independence relations. For example, the illustration shows that we have assumed that earthquakes are independent of burglaries one event doesnt cause the other event. In other words, we have implied p(e, b) = p(e)p(b). Note that this assumption might not be true in the real world, but this is assumed in the network. Also, we have assumed that it is the alarm that might cause the neighbor to call, and not the burglary itself or an earthquake event. This implies a conditional independence relationship we have assumed that the neighbor call is independent of the burglary event, conditional on knowing something about the alarm. In other words, we have assumed that if one already knows about the alarm, knowing about the burglar will not change the probability of the neighbor calling. Specically, the network implies the following conditional independence relationship: p(c|a, b) = p(c|a).

4.2.1

Conditional probability tables

Up to this point, we havent said anything about the exact functional relationship between variables. There are many ways to parametrize the conditional probability relationships in graphical models. In our example, we have only binary variables with outcomes that can be represented by 1 (true) and 0 (false). One simple way to parametrize the relationships is by conditional probability tables. This is illustrated in Figure 4.2. For the root nodes in the network, we only have to specify the prior probabilities of each outcome. We have assumed that the probability of an earthquake (a priori) is .002. Similarly, we have assumed that the probability of a burglary is 0.001. For the alarm event, we need to specify a conditional probability that depends only on the parent nodes. We have assumed that the alarm

CHAPTER 4. DIRECTED GRAPHICAL MODELS


x y

50

Figure 4.2: The conditional probability tables (CPTs) for the graphical model example. might still go o even in the absence of earthquakes or burglaries (with probability 0.01). Furthermore, we have assumed that an earthquake by itself might trigger the alarm (with probability 0.29), that a burglary by itself more reliably triggers the alarm (with probability 0.94) and that the combination of an earthquake and burglary raises the probability to 0.98. Furthermore, we have assumed that our neighbor is likely to call (with probability 0.90) if the alarm goes o, but not always. In addition, with probability 0.05, the neighbor can call even if there is no alarm (the neighbor might be calling about something else). With all the conditional probabilities specied, we can now engage in probabilistic inference. We can calculate the probability of one event if we know about the outcomes of other events. We can reason from causes to eects but also from eects to causes. Because our example is so simple, we can use exact methods to calculate any probability of interest, just by using some standard rules in probability theory, such as the sum, product and Bayes rule. For example, suppose we want to calculate the probability that the alarm is triggered knowing that the burglary is taking place. In other words, we need to know p(a = 1|b = 1). Note that in this question, we have not said anything about earthquakes, another potential trigger for of alarm and therefore, we need to sum over all possibilities regarding earthquakes. Using the sum and product rules, we can calculate this by p(a = 1|b = 1) = p(a = 1|b = 1, e = 0)p(e = 0) + p(a = 1|b = 1, e = 1)p(e = 1) = (0.94)(0.998) + (0.98)(0.002) = 0.9401 Suppose we hear a radio report on an earthquake, what is the probability that an earthquake happened, i.e., p(e = 1|r = 1)? Note that this probability is not 1 because we have assumed that earthquakes are likely but not guaranteed to be reported. Note also that we cannot

CHAPTER 4. DIRECTED GRAPHICAL MODELS

51

directly read o this probability from the conditional probability table because the table shows the probability of a report conditional on an earthquake and not the other way around. In this case, we can use Bayes rule to ip the conditional probabilities as follows: p(e = 1|r = 1) = p(r = 1|e = 1)p(e = 1) p(r = 1|e = 1)p(e = 1) + p(r = 1|e = 0)p(e = 0) (.992)(0.002) = (.992)(0.002) + (.001)(.998) = .6653

Exercises
1. Calculate the probability that the alarm is not triggered if a burglary is taking place: p(a = 0|b = 1). For this and the following exercises, show how you derived the answer, and do not just give a single numeric answer. 2. Calculate the probability that you get a call from your neighbor if a burglary is taking place: p(c = 1|b = 1). Note that the answer from the previous exercise will be part of this calculation. 3. Calculate the probability that there is a burglary if the alarm is triggered: p(b = 1|a = 1). 4. Are the two events, radio report and burglary independent? ** 5 Calculate the probability that there is a burglary if you get a call from your neighbor: p(b = 1|c = 1).

4.2.2

Explaining away

It is important to realize that if two events are a priori independent, that does not necessarily mean that they are still independent if we gain additional knowledge about another event. In other words, two events that are independent might become conditionally dependent on another outcome. This leads to the explaining away phenomenon. For example, in the burglar alarm network, the earthquake and burglary events are independent, a priori. Suppose we now learn that the alarm has indeed been triggered. Are the earthquake and burglary events still independent conditional on this extra bit of information? It turns out they are not. To see this, suppose we also know that an earthquake has taken place. This will in fact lower the probability of a burglary. Intuitively, the earthquake explains away the fact that the alarm has been triggered. Similarly, if we know that an earthquake has denitely not happened, this will raise the probability of the burglary event. More formally, in this network, we have independence p(b|e) = p(b) but not conditional independence p(b|e, a) = p(b|a).

CHAPTER 4. DIRECTED GRAPHICAL MODELS

52

Exercises
1. Calculate the probabilities p(b = 1|a = 1, e = 1) and p(b = 1|a = 1). Note that the last probability was already calculated in a previous exercise. Verify that the rst probability is much lower than the second probability.

4.2.3

Joint distributions and independence relationships

The directed relationships between random variables in a graphical model can be translated to a joint distribution over all random variables. Suppose we have a network with K variables x1, x2 , . . . , xK , the joint distribution can be calculated by:
K

p(x1 , . . . , xK ) =
k=1

p(xk |pak )

(4.10)

where pak represents the realizations of variables that are parents of the k th node in the network. Therefore, the joint distribution can be factorized as a product of probability of each node conditional on its parents. Note that if a node k has no parents (it is a root node), the conditional probability p(xk |pak ) becomes p(xk ). We can turn a particular factorization of a joint distribution into a graphical representation. The key here is that sometimes, it is useful to visually inspect the graph structure to understand the properties of a graphical model. At other times, it is useful to inspect the implied factorization of the joint distribution. The directed graphs we are considering are called directed acyclic graphs or DAGs. These are subject to an important restriction: there cannot be any directed cycles in the graph, whereby following the arrows from one node leads us to back to the same node. In other words, there cannot be any (directed) loops in the graph. For the alarm example, the joint distribution factorizes as follows: p(a, b, c, e, r) = p(c|a)p(a|e, b)p(r|e)p(e)p(b). Therefore, the probability of a particular set of outcomes can easily be evaluated. If we want to know the probability of getting the neighbors call while the alarm is triggered and there is an earthquake but no burglary and there is an earthquake report on the radio, we can write p(a = 1, b = 0,c = 1, e = 1, r = 1) = p(c = 1|a = 1)p(a = 1|e = 1, b = 0)p(r = 1|e = 1)p(e = 1)p(b = 0) = (0.90)(0.29)(0.992)(0.002)(0.999) = 0.0005173. For small networks, it is relatively easy to just visually read o the independence relationships from a network. Instead of visual inspection, one can also use the joint distribution to derive all the conditional independence relations. For example, consider the graphical model in Figure 4.3, panel C. Suppose we want to know whether x and y are independent

CHAPTER 4. DIRECTED GRAPHICAL MODELS


53

Figure 4.3: Four example graphical models conditional on z , such that p(x, y |z ) = p(x|z )p(y |z ). We can use the product rule to write the conditional probability p(x, y |z ) as follows: p(x, y |z ) = p(x, y, z ) p(z )

We can replace the joint distribution in the numerator by the factorization that the network implies: p(x, y |z ) = p(x, y, z ) p(z ) p(x|z )p(y |z )p(z ) = p(z ) = p(x|z )p(y |z )

Therefore, the conditional independence holds in this case.

Exercises
1. Consider the graphical model in Figure 4.3, panel A. Is this a directed acyclic graph? Give the factorization of the joint probability. 2. For the graphical models B and D in Figure 4.3, determine if x and y are conditionally independent of z . Also, in what cases are x and y independent?

CHAPTER 4. DIRECTED GRAPHICAL MODELS

54

4.3

Graphical Model Notation

With the alarm example, we have already seen some of the basic ways in which we can represent conditional distributions as directed links between nodes in a graph. In this section, we will elaborate the graphical model notation that is needed to represent most probabilistic models. Unfortunately, graphical model notation is not fully standardized, and dierent authors use slightly dierent notations. Suppose we have a situation where we have observed four outcomes in our experiment: y1, y2 , y3 , and y4 . We now create a model for our data that incorporates our beliefs about how the data was generated. In this simple example, suppose we believe that the four observed outcomes were all generated by a Normal(, ) distribution with the same mean and standard deviation . The corresponding graphical model is shown in Figure 4.4, panel A. The nodes corresponding to our observations are shaded in gray. The nodes corresponding to the latent variables are not shaded. This is an important notational convenience that helps to visualize the relationships between observed and latent variables. In hierarchical models where the same sampling steps are repeated many times over, it becomes unwieldy to visualize represent the full tree that includes each individual sampling step. We can simplify this using plates. Figure 4.4, panel B, shows how the model in panel A can be represented more compactly using plate notation. The plate represents a repeated sampling step where the subscript i indexes the particular observed value of y . Another notational convenience is to distinguish between variables that have probabilistic or deterministic relationships with their parent variables. For example, suppose that in our experiment, we are really interested in the precision of the normal distribution, where = 1/ . Figure 4.4, panel C shows how we can include this in our model. We use a double-bordered node for to indicate that the variable is deterministic. Therefore, if we know , the value of is automatically determined (and vice versa) there is no uncertainty associated with their relationship. In this particular example, we could have drawn an arrow from directly to yi and have omitted the node for altogether. However, in many cases, it is useful (and necessary) to specically include any deterministic variables in the model. In order to communicate graphical models to other researchers, the graphs help to visualize the dependencies between variables but do not provide the exact probabilistic or functional dependence between variables. To do this, we can use probability notation such as yi Normal(, ), where the symbol can be read as distributed as. This is equivalent to writing p(yi ) = Normal(yi|, ) or p(yi |, ) = Normal(yi |, ) to explicitly represent the conditional dependence on and .

4.3.1

Example: Consensus Modeling with Gaussian variables

Suppose we have an experiment where we have M individuals participating in an estimation task. In this estimation task, individuals are given questions where the answers consist of a single number. For example, they might be asked to estimate the number of dots in a display, or give an answer to a general knowledge question such as how many people live in

CHAPTER 4. DIRECTED GRAPHICAL MODELS


55

Figure 4.4: Examples of graphical models where shaded and unshaded nodes indicate observed and unobserved variables respectively. The models in panel A are B are identical. The plate notation in panel B provides a more compact representation of models with repeated sampling steps. Panel C illustrates a deterministic variable as indicated by the double-bordered node. Irvine, California. Although we as experimenters know the true answer to each question, the goal of the modeling is to estimate the true answer on the basis of all the estimates given by individuals. This setup is similar to Cultural Consensus models developed by Batchelder and colleagues, except that we apply this to questions with continuous as opposed to discrete answers (such as true/false or multiple choice questions). In this consensus model, we are assuming that each individual is giving N repeated estimates for the same question. Therefore, the observed data consists of estimates yij where i = 1, . . . , N indexes the repetition and j = 1, . . . , M indexes the individual. In the model for this task, we assume that all estimates are Normally distributed centered around the mean , which is a latent variable in the model. We also assume that each individual is associated with a separate standard deviation j . This allows the model to associate dierent levels of uncertainty for each individual. To complete the model, we put a Gamma(a,b) prior on j , which is the precision associated with individual j . We map from precision to standard deviation simply by j = 1/ j . This leads to the following model: yij Norm(, j ) j = 1/ j j Gamma(a, b)

(4.11)

where a and b are hyperparameters in the model. The associated graphical model is shown in Figure 4.5. Note that there two plates in the model. The inner plate indicates the repeated samples for a given individual, and the outer plate indicates the sampling across individuals. The fact that plates can be nested is quite helpful to create compact representations for hierarchical models.

CHAPTER 4. DIRECTED GRAPHICAL MODELS

56

Figure 4.5: A consensus model for normally distributed data.

Exercises
1. Suppose in the model in Figure 4.4, panel A, the experimenter lost the observed value y3 because of a programming error. How would this change the graphical model? 2. Write down the joint distribution p(y1, y2 , y3, y4 , , ) for the model in Figure 4.4, panel B (equivalent to the model in panel A). Instead of writing one long sequence of terms, it is helpful to use product notation (i.e., ) 3. Write down the joint distribution p(, j , y11, y12, . . . , yN M ) for the consensus model in Figure 4.11. Again, use product notation. 4. For the consensus model in the example, how would the graphical model change if the means are not latent variables but are observed instead? 5. The consensus model in the example models the situation for just a single question. Suppose we want to expand the model to K separate questions, where each question is associated with a single latent truth k . To simplify the model, we assume that each individual has an uncertainty j regardless of what question is asked. Visualize the corresponding graphical model. 6. Suppose we have the following model: zi Bernoulli(1/2) 0 = 1/2 i1 Gaussian(, ) Uniform(0.5, 1) Gamma(0.001, 0.001) i = 0 z i = 0 i1 zi = 1

ki Binomial(i , n) Write down the corresponding graphical model when ki and n are the only observed variables, and i = 1, . . . , p. Note that this model was taken from the coursebook by Lee and Wagenmakers.

Chapter 5 Approximate Inference in Graphical Models


A standard application of graphical models is to nd the distributions for the latent variables that might explain the observed data. In a Bayesian framework, this corresponds to estimating the posterior distribution over the latent variables. These inferred distributions can be used to test theoretical assumptions about some underlying process (e.g. a cognitive process). Another application of graphical models is to produce predictive distributions. This can be useful to simulate what the model believes a priori the data should look like, or how the model believes any future data should look like after learning about some observed data. Such simulations can be useful to the modeler to test the assumptions of some theoretical framework and to make predictions that can be tested with empirical research.

5.1

Prior predictive distributions

The prior predictive is the distribution the model predicts over the observed variables, before any data is considered. In other words, these are the predictions the model makes there is no observed data to condition on (see Figure 5.1A. This corresponds to a graphical model where the observed variables are turned (temporarily) into latent variables such that the whole graphical model consists of latent variables. This can be useful in a number of ways. In some cases, a model will predict data patterns that the researcher knows will either never occur in an experiment or occur only rarely. This can happen when the model is too complicated or when the researcher fails to incorporate some important constraints in the model. Another reason to consider the prior predictive distribution is to create synthetic datasets for testing purposes. Before applying any probabilistic model to real data, it is often helpful to do posterior inference on the model on data that were produced by itself. This helps to check the inference procedures that were used. If the inference works properly, the model should infer distributions over the latent variables that are similar to the ones that were sampled when producing the articial data. Specically, let y = (y1, . . . , yN ) represent the set of observed variables in a graphical 57

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS


      ! 

58

Figure 5.1: Three inference problems in graphical models. The question marks indicate the variables that need to be inferred for each problem. model. Let = (1 , . . . , M ) be the set of latent variables in a model. The prior predictive distribution corresponds to: p(y ) = = p(y , )d (5.1) p( )p(y | )d

Therefore, we integrate over all possible combinations of the latent variables and weight the predictions that ow from the latent variables with the prior probability of the latent variables. Note that integration is replaced by summation for discrete latent variables in the network. In some cases, the integrals in Equation 5.1 can be evaluated analytically. In this section, we will focus instead on a simple procedure that approximates the prior predictive using Monte Carlo sampling. This procedure is based on ancestral sampling. The idea is very simple. To draw one sample from the prior predictive, we start by sampling from the root nodes in the network (these are the nodes that have no parents). If the root nodes are constants in the model set by the researcher, we dont sample and keep those values. In the next step, we draw samples from the child nodes of the root nodes. In this case, we are drawing from distributions that are conditional on the sampled values of the parent nodes. On each subsequent step, we draw samples from nodes for which the parent nodes already were sampled and stop when all variables have an assigned sample. This process of ancestral sampling repeats for a number of iterations until we have drawn enough samples. The important aspect in ancestral sampling is to always start at the root nodes at the top of the network, and to work your work down to the bottom of the network, and at each step to only sample from a node whose parent nodes have been sampled. Note that this method is just based on Monte Carlo sampling and not Markov chain Monte Carlo. Therefore, there is no burnin period all samples drawn in this procedure can be accepted as samples from the prior predictive distribution.

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS

59

400

350

300

250

200
yij

150

100

50

50

7 8 9 10 11 12 13 14 15 Individual

Figure 5.2: Data pattern sampled from the prior predictive distribution for the consensus model with normal distributions. The model used = 150, a = 0.3, b = 3.33, M = 15 and N = 20.

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS

60

Exercises
1. Consider the alarm network discussed earlier and shown in 4.2. Suppose we want to calculate the prior predictive distribution for node c, the neighbor call. Use the ancestral sampling method to draw 10,000 samples from the prior predictive distribution. On the basis of these samples, what is the predicted probability, a priori, of a neighbor call? (** what is the exact probability?). 2. Consider the consensus model described in Equation 4.11. Suppose that = 150 and that the hyperparameters a and b are set to 0.3 and 3.33 respectively. Suppose that the number of individuals M = 15 and the number of repetitions per individual, N = 20. Intuitively, this corresponds to a situation where 15 individuals all give 20 independent estimates that are centered around the true answer of 150. Draw a single sample from the prior predictive distribution over yij . You can visualize the results as in Figure 5.2. This gure shows some possible data pattern from the prior predictive (keep in mind that your results will look dierent because a dierent random seed is used). It shows that some individuals are highly variable in their estimates (corresponding to a high j but all estimates are centered around 150. Save the resulting data pattern to a le. Youll need it for a later exercise.

5.2

Posterior distributions

One of the most important uses for graphical models is to explain the observed data through a set of latent variables. In other words, the problem is to nd distributions over latent variables such that they are likely to reproduce the observed data (see Figure 5.1B for a canonical example of this problem). In Bayesian modeling, this corresponds to the problem of nding the posterior distribution p( |y ) = p(1 , 2 , . . . , M |y1, . . . , yN ). In some simple graphical models, this can be calculated exactly using Bayes rule. For example, in the alarm network, it was relatively straightforward to calculate the probability of a burglary given a phone call from the neighbor. In other cases, message passing schemes can be used to calculate the exact posterior distribution. We will not go into these approaches in this chapter. Instead, we will discuss two sampling approaches.

5.2.1

Rejection Sampling

This approach is one the simplest methods to do posterior sampling and it only works when the observed variables are discrete. We mention this method primarily because of its simplicity. The idea is that one uses the ancestral sampling method to simulate the prior predictive distribution but the procedure is changed such that all samples that lead to values of the observed variables that are unequal to the actual observed values are discarded. When any mismatch occurs for any of the observed values, new samples need to drawn. When the observed values are a priori very unlikely in the model, it leads to a highly inecient approach because the majority of samples will be rejected.

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS

61

Exercise
1. Consider again the alarm network. Suppose the observed data consists of node c, the neighbor calling. Suppose we have just heard the phone ringing and it is the neighbor calling. Use rejection sampling to calculate the posterior probability of a burglary, p(b = 1|c = 1). Note that you can use the code you developed to draw samples from the prior predictive distribution. You just have to modify it to reject samples that lead to the outcome c = 0.

5.2.2

MCMC Sampling

We can consider the posterior distribution p( |y ) = p(1 , 2, . . . , M |y1, . . . , yN ) as simply some multivariate distribution that needs to be sampled from. In Chapter 2, we have considered two approaches to sample from multivariate distributions: the Gibbs sampler and the componentwise Metropolis-Hastings sampler. We can use these approaches to sample from posterior distributions. The only dierence is that now we are conditioning on observed data and that we can often simplify the sampling steps by making use of the conditional independence relationships in graphical models. Two latent variables To illustrate the general MCMC sampler, we look at a case involving two latent variables 1 and 2. We represent instantiations (i.e. samples) of these variables by v1 and v2 respectively. (t) (t) We represent the state of the sampler at iteration t with 1 and 2 . For the rst iteration, (1) we initialize the sampler with some suitable values v1 and v2 and set the initial state 1 = v1 (1) and 2 = v2. At each iteration t, we rst sample a new value v1 for the rst latent variable (t) from the conditional distribution p(1 |y , 2 = v2). We then update the state 1 = v1 with the sampled value. In the second step, we sample a new value v2 for the second latent variable (t) from the conditional distribution f (2 |y , 1 = v1). We then update the state 2 = v2 with the sampled value. This completes one iteration of the sampler. Here is a summary of the sampling procedure for two latent variables: 1. Set t = 1 2. Generate initial values v1, v2 for the two latent variables 3. Set the initial state 1 = v1 and 2 = v2 4. Repeat t=t+1 Sample v2 p(2 |y , 1 = v1 )
(t) (t) (t)

Sample v1 p(1 |y , 2 = v2 )
(t)

Update the state 1 = v1 and 2 = v2

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS 5. Until t = T Multiple latent variables

62

This procedure can be generalized to M latent variables. In one iteration of the sampler, each latent variable j is visited and assigned a value by drawing from the distribution of that variable conditional on the assignments to all other latent variables as well as the observed data. In our notation, we will write j to refer to all variables other than j . After all latent variables are assigned a value, we update the state (t) with these values. Here is a description of the general MCMC sampling approach for posterior distributions: 1. Set t = 1 2. Generate initial values v1, v2, . . . , vM , for each latent variable 3. Set the initial state (t) = v 4. Repeat t=t+1 j=0 Repeat j=j+1 Sample vj p(j |y , j = vj ) Update the state j = vj Until j = M 5. Until t = T Markov Blankets At the heart of the general MCMC approach for posterior inference is the conditional distribution p(j |y , j = vj ) where we sample a value for a latent variable conditioned on the instantiations of all other latent variables as well as the data. When calculating this distribution, one can take advantage of the conditional independence relationships in the graphical model. To illustrate this, look at the example graphical model in Figure 5.3, panel A. Suppose in our sampler, we are at the step where need to sample an assignment for the third latent variable 3 by sampling from the conditional p(3|1 , 2, 4, 5 , y1, y2). Using the product rule, we can write this as proportional to the joint distribution p(3 |1, 2 , 4, 5, y1, y2 ) p(3 , 1, 2 , 4, 5, y1 , y2) We can simplify the joint distribution using the general factorization in 4.10, giving us: p(3 |1 , 2, 4, 5 , y1, y2) p(1 )p(2)p(4 )p(3 |1)p(5 |2 )p(y1|3 , 5)p(y2 |5, 4 )
(t)

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS


h P Q R P S R i p

63

` ( B C )

b 0 D E 1

f 4 H 8 9 I $ % 5 V W

d 2 F G 3

T " 6 7 @ A # & '

U X Y

Figure 5.3: Illustration of Markov blankets for an example graphical model (A). The Markov blankets for 3 and 5 are shown in (B) and (C) respectively. Now, note that we are looking for a distribution for 3. Any term on the right-hand side that does not depend on 3 can be removed from the equation, leading to p(3|1 , 2, 4, 5 , y1, y2) p(3 |1)p(y1 |3, 5) On the right-hand side, we only have the terms 3 , 1 , 5, and y1. All other latent variables and observed data are irrelevant in this conditional distribution. We call the variables that are relevant for the conditional distribution the Markov blanket. Figure 5.3, panel B shows the Markov blanket for 3. We can use similar reasoning to derive the Markov blanket for 5, as shown in panel C. Instead of going through these calculations each time to determine the Markov blanket, we can use a simple denition. The Markov blanket for a node includes all parents, all children and all parents of these children.

5.2.3

Example: Posterior Inference for the consensus model with normally distributed variables

To give an example of a posterior inference procedure, we revisit the consensus model discussed earlier. The model is yij Normal(, 1/ j ) (5.2) j Gamma(a, b) where we have removed the latent variable j and instead directly placed the precision term j into the normal distribution. Therefore, we have the latent variables j , where j = 1, . . . , M , and the latent variable . We will treat the variables a and b as constants in the model and we will not sample them. In our MCMC sampler, we will need to able to sample from a conditional distribution p(j |y , j , , a, b) to get values for the individual precision variables. Using Markov blankets, we can derive that p(j |y , j , ) = p(j |y1j , . . . , yN j , , a, b). We also

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS

64

need to sample from a conditional distribution p(|y , , a, b). Again, using the conditional independence relations, we can derive that p(|y , , a, b) = p(|y , ). This leads to the following general MCMC procedure where the state at iteration t consists of sampled values (t)and (t): 1. Set t = 1 2. Generate initial values v1, . . . , vM for the precision variables 3. Set the initial state (t) = v 4. Generate initial value vM +1 for variable 5. Set the initial state (t) = vM +1 6. Repeat t=t+1 Sample v2 p(2 |y12, . . . , yN 2, = vM +1, a, b) Sample v1 p(1 |y11, . . . , yN 1, = vM +1, a, b)

...

Update the state (t) = v

Sample vM p(M |y1M , . . . , yN M , = vM +1, a, b)

Update the state (t) = vM +1 7. Until t = T

Sample vM +1 p(|y , = (t))

The next step is to gure out how to actually draw samples from these conditional distributions. One solution would be to do Metropolis-Hastings for each step. However, the structure of the model allows a Gibbs sampling solution where we can directly sample from the conditional distribution. Using conjugacy from Bayesian statistics (which we will discuss in a later section), the conditional distribution for the precision parameter is based on the following distribution: j |y1j , . . . , yN j , , a, b Gamma a + N , 2 b+ 1
N i=1

(yij )2 /2

(5.3)

We can also use conjugacy to derive a simple form for the conditional distribution for : N M y 1 i=1 j =1 j ij |y , Normal , (5.4) M M N j =1 j N j =1 j

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS

65

150.8 150.6
(t)

150.4 150.2 150 149.8 100 3 150 200 250 iteration 300 350 400

(t)

0 100

150

200

250 iteration

300

350

400

Figure 5.4: Illustration of the Gibbs sampler for the consensus model. Top panel shows the samples drawn for . Top panel shows the samples drawn for all individual precision variables. These sampling equations can be used in the Gibbs sampler for the model. To test the Gibbs sampler, we sampled synthetic data using the prior predictive distribution. For this synthetic dataset, we used = 150 and set the hyperparameters a and b to 0.3 and 3.33 respectively. We simulated the model with M = 15 individuals N = 20 repetitions per individual. Figure 5.4 shows the samples drawn from the posterior distribution over and for this synthetic dataset. Note that the rst 100 samples were discarded and that the sampler was run for 400 iterations. Figure 5.5 shows the correlation between the inferred precision parameters (averaged over the last 300 iterations) and the precision parameters that were sampled in the prior predictive distribution. As you can observe, the model is able to (roughly) recover the precisions for each (simulated) individual.

Exercises
1. Write a Matlab program that implements the Gibbs sampler for the consensus model. The dataset can be based on the synthetic data you created in an earlier exercise. Use the parameter and sampler settings as specied above. Create visualizations that are similar to Figure5.4 and Figure 5.5. 2. What is the inferred mean when you average the sampled values for the last 300 iter-

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS


1

66

10

10

10
True

10

10

10

10

10

10

10 Inferred

10

10

Figure 5.5: Correspondence between the inferred precisions for each (simulated) individual and the true values that were drawn in the prior predictive distribution ations? How does this inferred mean compare with the mean calculated by averaging over all observed data values? When would you expect to see a major dierence?

5.2.4

Example: Posterior Inference for the consensus model with contaminants

The beauty of graphical models is that it is often easy to make the model more psychologically plausible and applicable to real-world data. For example, suppose in the consensus model that the estimates generated by individuals occasionally contain noise that are unrelated to the true distribution. This could be because individuals might be mistyping numbers, forgetting what the question was, or any number of reasons that are deeply troubling to researchers who conduct these experiments. Lets introduce a new latent variable in the model, xij that represents the status of each datapoint. If xij = 1, the ith estimate for individual j is assumed to be a contaminant, and when xij = 0, the estimate is generated from the consensus model. Lets assume that if a contaminant is produced, it is sampled from a Uniform(ymin , ymax) distribution. Finally, lets assume that is the probability that a particular estimate is a contaminant we will assume that this probability is the same across all individuals. This leads to the following model:

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS


True Contaminants 1000 1000 Inferred Contaminants

67

900

900

800

800

700

700

600

600

yij

500

yij
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Individual

500

400

400

300

300

200

200

100

100

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Individual

Figure 5.6: Illustration of the data from the consensus model with contaminants. The left panel shows the synthetic data produced from the model where the cross symbols indicate contaminants. The right panel shows the same data but with the inferred contaminants at the 400th iteration from a Gibbs sampler.

xij Bernoulli( ) yij

Norm(, 1/ j ) xij = 0 Uniform(ymin , ymax ) xij = 1

(5.5)

j Gamma(a, b)

Exercises
1. Create a synthethic dataset from the consensus model with contaminants. You can keep the same parameters as before (a=0.3, b=3.33, = 150, M =15, N =20) and assume that ymin =0, ymax =1000, and = 0.1. Visualize the data as in Figure 5.6, left panel where the cross marks indicate the estimates associated with contaminants.

CHAPTER 5. APPROXIMATE INFERENCE IN GRAPHICAL MODELS

68

2. Consider the problem of posterior inference for the consensus model with contaminants. It might be helpful to rst consider the inference for the latent variables j and . Suppose we actually tell the inference algorithm what the true values for xij are. How should we change the Equations 5.3 and 5.4? 3. Next we need to know how to calculate the conditional probability p(xij |yij , , j ). Use Bayes rule to nd the two terms that this probability is proportional to. Write out how this conditional probability can be evaluated when xij = 1 and xij = 0. 4. Write the program that performs the posterior inference over all latent variables j , , and xij . Use the last iteration of the sampler to visualize the inferred contaminants similar to Figure 5.6. ** 5 Instead of taking just a single sample from the Gibbs sampler to interpret the data (as we did in the last exercise), it is much better to average across many samples. For example, we can calculate the probability that a particular estimate was assigned to the contaminant model. Calculate this probability and modify the visualization to indicate this probability for each estimate. ** 6 Consider the basic consensus model again without contaminants. We have only considered the Normal distribution for data generation. In spite of many comments in the literature that the Normal distribution is a natural choice to model real-world data, it is often the case that many measurements are not Normally distributed. They might be restricted to positive numbers only or might have heavy tails, leading to extreme measurements. Change the model to incorporate dierent data generating distributions, such as the Gamma distribution (which is bounded at zero) and the Student-t distribution (which has heavy tails). With these distributions, we can no longer use conjugacy to sample from nice distributions. However, we can use the MetropolisHastings sampler to sample each individual latent parameter conditional on the state for all other latent parameters. Implement such a sampler. Once you have reached this stage, I can give you some real estimation data to test your model on.

Chapter 6 Sequential Monte Carlo


In the preceding chapters, we have investigated inference methods that allow researchers to apply probabilistic models to data in a posthoc fashion the modeling can start as soon as the data collection has nished. This is known as batch processing. In many applications however, there is no natural end point to the data collection. The data arrive gradually over time and the goal is to draw inferences about latent variables given the data observed up to the current point in time. In this case, we could still opt to apply batch processing methods such as MCMC. For example, in a naive application of MCMC methods, we could rerun the inference algorithm every time we get a new observation. Although this procedure leads to proper inferences about the data, it is computationally inecient. It requires the explicit storage of all data observed to this point which might be impractical if we are dealing with situations in which large amounts of data needs to be processed. More importantly, processing time increases with every new observation because the batch processing procedure is applied to all data observed up to this point. For this reason, it is desirable to use inference methods where the processing time does not depend on time itself each new observed data point takes a constant amount of processing time. Sequential Monte Carlo (SMC) methods are a general class of Monte Carlo sampling techniques for sequential inference problems. They allow for the online processing of data which lead to time and memory requirements that are constant in the total number of observations received over time. A typical application for SMC methods are tracking problems (also known as ltering problems) in robotics and vision, where the goal is to estimate the state of a system that changes over time using a sequence of noisy measurements. For example, in object tracking, the goal is to continually track the position of an object on the basis of noisy measurements about the objects position. In this case, it is not sucient to only use the latest measurement in the inference about the current position. Objects typically move smoothly over time so all recent measurements are useful to make inferences about the current position. Recently, SMC techniques have also been introduced in other areas of cognitive science including language, decision-making and categorization. For example, Pearl, Goldwater and Steyvers have recently used SMC methods for online word segmentation problems, where 69

CHAPTER 6. SEQUENTIAL MONTE CARLO

70

the problem is to extract words from a continuous stream of speech. Brown and Steyvers applied SMC methods such as particle lters to model decision-making strategies in nonstationary environments. Griths and Sanborn investigated how particle lters can explain order eects observed in categorization tasks. Finally, Vul has applied particle lters to model human performance in an object-tracking task. In this chapter, we will discuss two SMC methods to estimate the hidden state of a dynamically changing system: decayed MCMC and particle lters. As the name suggests, sequential Monte Carlo approaches are based on sampling the hidden state can be estimated on the basis of a set of samples that approximate posterior distributions. We will not go into methods such as Kalman lters and Extended Kalman lters because they make Gaussian assumptions about state transitions and measurement noise that are often unrealistic. In addition, the computational overhead to estimate Kalman and Extended Kalman lters might also be prohibitive in some instances. One desirable feature of SMC approaches such as particle lters is that the approximation can be made arbitrarily precise by varying the number of particles. For some low-dimensional problems, very good approximations can be obtained with a small number of particles (e.g. 50) leading to computationally ecient procedures.

Exercise
1. To get a good sense of the tracking problem in robotics and vision, watch the YouTube videos named Particle lter tracking with adaptive parameters estimation and SIR particle lter using color statistics. If you want to see a funny tracking application involving belly dancing, do a search for Human Motion Tracking - Belly.

6.1

Hidden Markov Models

A standard approach to probabilistic modeling with dynamics is to use hidden Markov models (HMMs). In an earlier chapter, we already introduced Markov models. The main dierence between Markov models and HMMs is, not surprisingly, that the state in an HMM is hidden (i.e. a latent variable). The graphical model of a HMM is shown in Figure 6.1. The variable xt represents the latent system state of the system at time t. In object tracking, the state could correspond to the position and direction of the object. The variable yt represents the observed set of measurements at time t. In object tracking applications, measurements could relate to any kind of sensor data. In the graphical model, we have assumed that the system state evolves over time according to p(xt |xt1), a rst order Markov process where the state of system at time t only depends on the state at time t 1. For example, we can assume that the objects new position is only related to the last position (i.e., it is a memoryless system). It is also assumed that the noisy measurements y at time t are determined by p(yt |xt). Therefore, the observations at time y are determined solely by the state of the system at time t the

CHAPTER 6. SEQUENTIAL MONTE CARLO


q r u v y

71

Figure 6.1: Graphical model measurements are conditionally independent given the system state. Finally, to complete the model, we also need to specify a prior (x0) over states at the start of measurement.

6.1.1

Example HMM with discrete outcomes and states

To get started with an simple HMM, suppose we have a model that tracks the emotional state of a person. In this model, the hidden state is discrete and consists of two possible outcomes: Happy and Sad. We will assume that the emotional state of a person can change over time, sometimes unpredictably (as in real life). We will use a simple change process where with probability a, the state changes and with probability 1 a, the state remains the same. For example, when a = 0.1, we expect that on average 1 out of 10 trials will be associated with a change in emotional state. We also need to specify a prior p(x0 ) over states at the rst time step which we assume to be uniform. To complete the model, we will also assume that the observations are discrete and consist of only two possible outcomes: Smile and Grimace. As you might suspect, we parametrize the model such that smiles are more likely in a happy state and grimaces more likely in a sad state. Specically, we will assume that with probability b, a person produces an outcome that is consistent with their emotional state. Instead of using symbols, we will represent Happy and Smile by the number 2 and Sad and Grimace by the number 1. Therefore, xt = 2 represents a happy state at time t and yt = 1 represents an observed grimace at time t. This leads to the following HMM: p(xt = j |xt1 ) = p(yt = k |xt ) = a j = xt1 (1 a) j = xt1

b k = xt (1 b) k = xt

(6.1)

Figure 6.2 shows a synthetic dataset produced by the model over 200 time steps when a = 0.1, b = 0.8. Note that smiles are more likely in the happy state but can also occasionally occur in the sad state. Also, notice how the latent state changes 14 times over a period of 200 trials.

CHAPTER 6. SEQUENTIAL MONTE CARLO


State sequence happy

72

sad 0 20 40 60 80 100 120 140 160 180 200

Observation sequence smile grimace 0 20 40 60 80 100 time 120 140 160 180 200

Figure 6.2: Sequence of hidden states and observed outcomes produced by an HMM

Exercises
1. Write a Matlab program that generates a synthetic dataset from the HMM model specied above. Use a = 0.1, b = 0.8 and generate an output sequence for 200 iterations. Visualize results in a way similar to Figure 6.2. Save the resulting output sequence as well as hidden state sequence to a le. 2. Repeat the last exercise but use the Matlab function hmmgenerate to create the output sequence.

6.1.2

Viterbi Algorithm

Before we go into SMC techniques, we will rst discuss a standard batch processing approach to estimate hidden states in HMMs: the Viterbi algorithm. This algorithm estimates the state sequence x 1 , x 2, . . . , x t that is most likely to have generated the output sequence y1 , y2, . . . , yt . We will not go into the details of the operation of the algorithm but just note a few of its properties. First, the algorithm requires knowledge of the probabilities p(xt |xt1) as well as p(yt |xt). Therefore, it needs to know how states can evolve over time and how observations are produced from the hidden state. Second, the Viterbi algorithm is a maximum likelihood estimator of the posterior it only gives the mode of the posterior distribution over state sequences but it does not give the full posterior distribution or samples from the posterior distribution as with MCMC methods. Finally, the Viterbi algorithm returns the estimate of the full state sequence. Therefore, it gives estimates for all hidden states up to the current time step. This is in contrast to SMC algorithms that are designed to estimate only the current hidden state. Figure 6.3 shows the estimated state sequence from the Viterbi algorithm applied to the data from the example HMM for emotional states. Note that the algorithm is fairly accurate in recovering the state sequence, but the results are obviously dependent on the parameterization of the model.

CHAPTER 6. SEQUENTIAL MONTE CARLO


True State Sequence happy

73

sad 0 20 40 60 80 100 120 140 160 180 200

Estimated State Sequence from Viterbi Algorithm happy

sad 0 20 40 60 80 100 time 120 140 160 180 200

Figure 6.3: Estimated state sequence for example HMM compared with the true state sequence

Exercises
1. Write a Matlab program that estimates the most likely state sequence from a synthetic dataset produced in the last exercise. Matlab implements the Viterbi algorithm with the function hmmviterbi. 2. Suppose we measure the accuracy of the estimate by the percentage of cases where the estimated state matches the true state. What is the accuracy according to this measure?

6.2

Bayesian Filtering

In many applications of HMMs, the observed data arrives sequentially over time leading to online inference problems such as Bayesian ltering. In this case, we want to estimate the posterior distribution p(xt |y1, . . . , yt ). This is the distribution over the just the current state at time t given all measurements up to and including time t. Note that we are not asking here about the full history of the system state, but only the latest state. If the full history is needed, it might be best to use MCMC methods. In the ltering task, SMC methods can be applied eciently because the results of the inference procedure at time t can be re-used again at time t + 1.

Exercises
1. In this exercise, we will explore the use of the Viterbi algorithm as a batch processing approach to the ltering task. Change the Matlab code from the last exercise to implement the Bayesian ltering task. Therefore, simulate the case where the data arrives sequentially over time and at each time, the Viterbi algorithm is applied to all data observed up to time t where t varies from 1 to 200. You can use the estimate of the last hidden state as a estimate of the ltered state. Note that you will therefore

CHAPTER 6. SEQUENTIAL MONTE CARLO

74

apply the Viterbi algorithm exactly 200 times. Visualize the ltered estimates over time by plotting the sequence of estimated hidden states up to t as a function of t (essentially you are creating a triangular display). Compare this with the true state sequence. 2. Estimate the time it takes at each iteration of the algorithm. What is (roughly) the relationship between the size of the dataset t and the time it takes to get a ltered estimate with the Viterbi algorithm? What is the problem highlighted by this relationship? 3. Measure the accuracy in the ltering task dened by the percentage of cases where the ltered estimate matches the true hidden state. Why is the accuracy in this case lower than in the exercise where the Viterbi algorithm was applied once to the full dataset.

6.3

Particle Filters

In the SMC approach to ltering, the posterior distribution over the current latent state is based on an approximation from a nite set of samples. Particle ltering is one form of SMC. In particle lters, each particle represents a sample or hypothesis about the current latent state. As the number of particles increases, the approximation improves and starts to resemble to the exact Bayesian posterior. Although a low number of particles is not desirable for engineering applications, as it can lead to poor approximations, they can be very useful to explain suboptimal human performance in a number of sequential tasks (e.g. Brown and Steyvers, 2009; Yi, Steyvers, and Lee, 2010). Note that particle ltering does not refer to a particular algorithm that rather a general approach there are many dierent methods to implement particle lters. A particularly attractive feature of particle lters is that no memory of previous states is needed. All the information needed for the ltering task is contained in a xed set of particles leading to a constant memory demand over time. However, when applying particle lters to high-dimensional problems, a very large number of particles might be needed to achieve good performance. This can in some cases lead to very slow performance, especially when overly simplistic particle lters are used.

6.3.1

Sampling Importance Resampling (SIR)

One of the most basic particle lters is Sampling Importance Resampling (SIR). It is based on the idea of importance sampling where a distribution is approximated by a set of samples weighted by the density of the target function such that the dense regions of the posterior are more heavily weighted than the tails. In the SIR lter, we always work with a xed set of M particles. To introduce notation, let xi t represent the hypothesis about the latent state of the ith particle at time t. Across particles, we can approximate the distribution of the ltered estimate at time t by the set of particles {xi t }. If a point estimate is needed for the

CHAPTER 6. SEQUENTIAL MONTE CARLO

75

latent state, we can take the mode or mean of this distribution. The SIR particle lter as described below is desirable for its simplicity. It only requires that one can sample from the state transition distribution p(xt |xt1) and can evaluate the likelihood p(yt |xt). We initialize the particle lter by sampling each particle from the prior distribution p(x0 ). At each iteration, we then evolve the set of particles from the last generation. First, we sample for each particle from the last generation a candidate hypothesis for the current latent state. Therefore, each particle leads to a projection of the hidden state to the current time step. We then calculate the likelihood of the current observation yt under each particle. This likelihood serves as the importance weight. We then normalize the importance weights across particles. In the second step, we evolve the set of particles from the last generation by sampling, with replacement, from the candidate hypotheses weighted by the importance weights. Therefore, candidate hypotheses that make the current observations likely lead to higher importance weights and are more likely to be sampled. This resampling continues until M new particles are generated. The procedure can be summarized as follows: 1. At time t = 0, initialize each particle xi 0 p(x0 ) where i = 1, . . . , M 2. Repeat t=t+1 For i=1:M
i Generate a proposal xi t p(xt |xt1 ) i Calculate a weight wt = p(yt |x i t)

End

i i Normalize weights, wt = wt /

i =1,...,M

i wt

i i Sample M new particles xi t with replacement from {x t } with weights wt

3. Until t = T Note that in the resampling step, the same proposal can be sampled multiple times. Also, a particular proposal might not be sampled at all. Therefore, each particle can lead to a varying number of ospring depending on the proximity of particle to the region in state space that best explains the current observation bad particles die out and are taken over by particles that do (at least for the current time step) explain the data. In this particular version of SIR, the proposal distribution equals the state transition distribution. This version of SIR is also known as the bootstrap or condensation algorithm. It is important to realize that this choice of proposal distribution can lead to problems with high-dimensional cases. This is because in the rst step of the SIR approach, we sample proposals from the state transition distribution without taking the current observation into account. The current observation is taken into account only in the second step, by weighting each particle by the likelihood. Therefore, we are blindly proposing how the current hidden state evolves from the last state and then correct only afterwards for any unlikely proposals.

CHAPTER 6. SEQUENTIAL MONTE CARLO


True State Sequence happy

76

sad 0 20 40 60 80 100 120 140 160 180 200

Filtered Estimate from Particle Filter Algorithm happy

sad 0 20 40 60 80 100 120 140 160 180 200

Particles

40 20 0

20

40

60

80

100 time

120

140

160

180

200

Figure 6.4: Predictions from the Sequential Importance Resampling (SIR) particle lter with M = 50 particles. The bottom panel shows the distribution over particles at each time step. For the middle panel, we have taken the mode of the distribution as the point estimate for the hidden state. In some cases, this leads to a situation where none of the candidate proposals come near the regions in the state space that can adequately explain the current observation. In other versions of SIR particle lters, more complex proposal distributions are used that do take the current observation into account (in the proposal generation). Also, some authors have proposed methods such as unscented Kalman lters to deal with this problem. Figure 6.4 shows the results of the SIR lter applied to the example data. The number of particles, M was set to 50. The bottom panel shows the distribution over particles for each iteration. The middle panel shows the mode of the particles to create a point estimate for the latent state at each time t. Importantly, remember that in the ltering task, a ltering estimate at time t implies that all data up to and including time t has been observed.

6.3.2

Direct Simulation

The method of direct simulation is a slight improvement over the simple SIR approach. Similar to the SIR approach, there are two steps, one to sample proposals for the current hidden state and one to assess the likelihood of the data given each proposal. However, we reject any proposed particles if they lead to unlikely predicted distributions for the observed data. Therefore, we are simultaneously weighting the prior distribution from the state transitions and the evidence from the observed data. Here is a summary of the procedure:

CHAPTER 6. SEQUENTIAL MONTE CARLO 1. At time t = 0, initialize each particle xi 0 p(x0 ) where i = 1, . . . , M 2. Repeat t=t+1 Set the particle count m=0 Repeat Sample a random particle i Uniform(1, . . . , M ) Sample a proposal x p(xt |xi t1 ) Sample u Uniform(0, 1)

77

Until m = M 3. Until t = T

If u < p(yt |x), accept the proposal, set m = m + 1, and set xm t = x

Exercises
1. Write a Matlab program that implements the SIR particle lter and the direct simulation method. Apply each particle lter with M = 50 particles to the emotional states dataset. Use the particle lters to estimate the hidden state at each time t up to t = 200. You can assume a uniform prior for p(x0 ). 2. Look up the paper A Tutorial on Particle Filters for Online Nonlinear/Non-Gaussian Bayesian Tracking by Arulampalam et al. (2002) and carefully read about the example problem on page 183. Implement the dynamic system given by Equations 79-83 to create an articial dataset. Apply the SIR and direct simulation particle lter to this data.

You might also like