# A Brief Introduction to Markov Chains

Markov chains are an essential component of Markov chain Monte Carlo (MCMC) techniques. Under MCMC, the Markov chain is used to sample from some target distribution. To get a better understanding of what a Markov chain is, and further, how it can be used to sample form a distribution, this post introduces and applies a few basic concepts.

A Markov chain is a stochastic process that operates sequentially (e.g. temporally), transitioning from one state to another within an allowed set of states.^{†}

A Markov chain is defined by three elements:

- A
, which is a set of values that the chain is allowed to take*state space* - A
that defines the probability of moving from state to .*transition operator* - An
which defines the probability of being in any one of the possible states at the initial iteration .*initial condition distribution*

The Markov chain starts at some initial state, which is sampled from , then transitions from one state to another according to the transition operator .

A Markov chain is called ** memoryless **if the next state only depends on the current state and not on any of the states previous to the current:

(This memoryless property is formally know as the *Markov property*).

If the transition operator for a Markov chain does not change across transitions, the Markov chain is called ** time homogenous**. A nice property of time homogenous Markov chains is that as the chain runs for a long time and , the chain will reach an equilibrium that is called the chain’s

**:**

*stationary distribution*We’ll see later how the stationary distribution of a Markov chain is important for sampling from probability distributions, a technique that is at the heart of Markov Chain Monte Carlo (MCMC) methods.

## Finite state-space (time homogenous) Markov chain

If the state space of a Markov chain takes on a finite number of distinct values, and it is time homogenous, then the transition operator can be defined by a matrix , where the entries of are:

This means that if the chain is currently in the -th state, the transition operator assigns the probability of moving to the -th state by the entries of -th row of (i.e. each row of defines a conditional probability distribution on the state space). Let’s take a look at a finite state-space Markov chain in action with a simple example.

### Example: Predicting the weather with a finite state-space Markov chain

In Berkeley, CA, there are (literally) only 3 types of weather: sunny, foggy, or rainy (this is analogous to a state-space that takes on three discrete values). The weather patterns are very stable there, so a Berkeley weatherman can easily predict the weather next week based on the weather today with the following transition rules:

If it is *sunny* today, then

- it is highly likely that it will be
*sunny*next week- ,

- it is very unlikely that it will be
*raining*next week - and somewhat likely that it will
*foggy*next week

If it is *foggy* today then

- it is somewhat likely that it will be
*sunny*next week - but slightly less likely that it will be
*foggy*next week- ,

- and fairly unlikely that it will be
*raining*next week.- ,

If it is *rainy* today then

- it is unlikely that it will be
*sunny*next week- ,

- it is somewhat likely that it will be
*foggy*next week- ,

- and it is fairly likely that it will be
*rainy*next week- ,

All of these transition rules can be instantiated in a single 3 x 3 transition operator matrix:

Where each row of corresponds to the weather at iteration (*today*), and each column corresponds to the weather the next week. Let’s say that it is rainy today, what is the probability it will be sunny next week, in two weeks, or in 6 months? We can answer these questions by running a Markov chain from the initial state of “rainy,” transitioning according to . The following chunk of MATLAB code runs the Markov chain.

% FINITE STATE-SPACE MARKOV CHAIN EXAMPLE % TRANSITION OPERATOR % S F R % U O A % N G I % N G N % Y Y Y P = [.8 .15 .05; % SUNNY .4 .5 .1; % FOGGY .1 .3 .6]; % RAINY nWeeks = 25 % INITIAL STATE IS RAINY X(1,:) = [0 0 1]; % RUN MARKOV CHAIN for iB = 2:nWeeks X(iB,:) = X(iB-1,:)*P; % TRANSITION end % DISPLAY figure; hold on h(1) = plot(1:nWeeks,X(:,1),'r','Linewidth',2); h(2) = plot(1:nWeeks,X(:,2),'k','Linewidth',2); h(3) = plot(1:nWeeks,X(:,3),'b','Linewidth',2); h(4) = plot([15 15],[0 1],'g--','Linewidth',2); hold off legend(h, {'Sunny','Foggy','Rainy','Burn In'}); xlabel('Week') ylabel('p(Weather)') xlim([1,nWeeks]); ylim([0 1]); % PREDICTIONS fprintf('\np(weather) in 1 week -->'), disp(X(2,:)) fprintf('\np(weather) in 2 weeks -->'), disp(X(3,:)) fprintf('\np(weather) in 6 months -->'), disp(X(25,:))

Here we see that at week 1 the probability of sunny weather is 0.1. The next week, the probability of sunny weather is 0.26, and in 6 months, there is a 60% chance that it will be sunny. Also note that after approximately 15 weeks the Markov chain has reached the equilibrium/stationary distribution and, chances are, the weather will be sunny. This 15-week period is what is known as the ** burn in** period for the Markov chain, and is the number of transitions it takes the chain to move from the initial conditions to the stationary distribution.

A cool thing about finite state-space time-homogeneous Markov chain is that it is not necessary to run the chain sequentially through all iterations in order to predict a state in the future. Instead we can predict by first raising the transition operator to the -th power, where is the iteration at which we want to predict, then multiplying the result by the distribution over the initial state, . For instance, to predict the probability of the weather in 2 weeks, knowing that it is rainy today (i.e. ):

and in six months:

These are the same results we get by running the Markov chain sequentially through each number of transitions. Therefore we can calculate an approximation to the stationary distribution from by setting to a large number. It turns out that it is also possible to analytically derive the stationary distribution from (hint: think about the properties of eigenvectors).

## Continuous state-space Markov chains

A Markov chain can also have a continuous state space that exists in the real numbers . In this case the transition operator cannot be instantiated simply as a matrix, but is instead some continuous function on the real numbers. Note that the continuous state-space Markov chain also has a burn in period and a stationary distribution. However, the stationary distribution will also be over a continuous set of variables. To get a better understanding of the workings of a continuous state-space Markov chain, let’s look at a simple example.

### Example: Sampling from a continuous distribution using continuous state-space Markov chains

We can use the stationary distribution of a continuous state-space Markov chain in order to sample from a continuous probability distribution: we run a Markov chain for a sufficient amount of time so that it has reached its stationary distribution, then keep the states that the chain visits as samples from that stationary distribution.

In the following example we define a continuous state-space Markov chain. The transition operator is a Normal distribution with unit variance and a mean that is half the distance between zero and the previous state, and the distribution over initial conditions is a Normal distribution with zero mean and unit variance.

To ensure that the chain has moved sufficiently far from the initial conditions and that we are sampling from the chain’s stationary distribution, we will choose to throw away the first 50 burn in states of the chain. We can also run multiple chains simultaneously in order to sample the stationary distribution more densely. Here we choose to run 5 chains simultaneously.

% EXAMPLE OF CONTINUOUS STATE-SPACE MARKOV CHAIN % INITIALIZE randn('seed',12345) nBurnin = 50; % # BURNIN nChains = 5; % # MARKOV CHAINS % DEFINE TRANSITION OPERATOR P = inline('normrnd(.5*x,1,1,nChains)','x','nChains'); nTransitions = 1000; x = zeros(nTransitions,nChains); x(1,:) = randn(1,nChains); % RUN THE CHAINS for iT = 2:nTransitions x(iT,:) = P(x(iT-1),nChains); end % DISPLAY BURNIN figure subplot(221); plot(x(1:100,:)); hold on; minn = min(x(:)); maxx = max(x(:)); l = line([nBurnin nBurnin],[minn maxx],'color','k','Linewidth',2); ylim([minn maxx]) legend(l,'~Burn-in','Location','SouthEast') title('First 100 Samples'); hold off % DISPLAY ENTIRE MARKOV CHAIN subplot(223); plot(x);hold on; l = line([nBurnin nBurnin],[minn maxx],'color','k','Linewidth',2); legend(l,'~Burn-in','Location','SouthEast') title('Entire Chain'); % DISPLAY SAMPLES FROM STATIONARY DISTRIBUTION samples = x(nBurnin+1:end,:); subplot(122); [counts,bins] = hist(samples(:),100); colormap hot b = bar(bins,counts); legend(b,sprintf('Markov Chain\nSamples')); title(['\mu=',num2str(mean(samples(:))),' \sigma=',num2str(var(samples(:)))])

In the upper left panel of the code output we see a close up of the first 100 of the 1000 transitions made by the 5 simultaneous Markov chains; the burn in cutoff is marked by the black line. In the lower left panel we see the entire sequence of transitions for the Markov chains. In the right panel, we can tell from the sampled states that the stationary distribution for this chain is a Normal distribution, with mean equal to zero, and a variance equal to 1.3.

## Wrapping Up

In the previous example we were able to deduce the stationary distribution of the Markov chain by looking at the samples generated from the chain after the burn in period. However, in order to use Markov chains to sample from a specific target distribution, we have to design the transition operator such that the resulting chain reaches a stationary distribution that matches the target distribution. This is where MCMC methods like the Metropolis sampler, the Metropolis-Hastings sampler, and the Gibbs sampler come to rescue. We will discuss each of these Markov-chain-based sampling methods separately in later posts.

^{†} On notation:

- Here we use the shorthand notation to correspond to , for some random variable
- A superscript in parentheses is an index into an iteration or point in time, not a power

Posted on September 24, 2012, in Algorithms, Density Estimation, Sampling Methods, Statistics and tagged Continuous state-space Markov Chain, Finite State-space Markov Chain, Markov Chain, Markov Property, State Space, Stationary Distribution, Time homogeneous Markov chain, Transition Operator. Bookmark the permalink. 3 Comments.

Pingback: MCMC: The Metropolis Sampler « The Clever Machine

Pingback: MCMC: The Metropolis-Hastings Sampler « The Clever Machine

Pingback: A Gentle Introduction to Markov Chain Monte Carlo (MCMC) « The Clever Machine