Overview

16 Markov processes and the Metropolis algorithm

The chapter introduces Markov processes as a general way to generate random sequences by starting in an initial state and repeatedly moving to a new state based only on the current one. It uses intuitive examples such as walking through a city, gambling with chips, and generating text to show that a state can be almost anything, discrete or continuous. A general Markov process is then implemented as a distribution over sequences, with an initial distribution and a transition function that produces the next-state distribution or ends the sequence.

The text then explores Markov text generation by treating words as states and learning initial-word and next-word distributions from a corpus such as Shakespeare. By parsing sentences, counting transitions, and building categorical distributions, the program can generate new text that sounds plausibly Shakespearean even if it is often nonsensical. This serves as a small-scale model of modern language generation and highlights both the power and the limits of memoryless random processes.

Finally, the chapter turns to Bayesian inference for continuous distributions and shows how the Metropolis algorithm makes posterior sampling possible when only a non-normalized probability density is known. Using Bayes’ theorem, the posterior density is formed from a prior and a likelihood, then sampled by proposing nearby values and accepting or rejecting them based on relative weight. The prior can serve as the initial distribution, a centered normal distribution can serve as the proposal, and the resulting Markov chain produces samples from the posterior, enabling practical probabilistic reasoning from observed evidence.

Summary

  • Sampling a Markov process generates a random sequence of values. The generated sequence can be finite or infinite, and the values can be discrete or continuous.
  • Markov processes generate a random sequence of states, which can be anything. The first state is chosen randomly by sampling from an initial distribution. Each subsequent state is chosen by calling a transition function with the most recently generated state and sampling from the resulting distribution.
  • If states are words, Markov processes generate texts. An easy way to create an initial distribution and transition function is to analyze an existing text and record the distributions of initial words in sentences and word pairs in those sentences.
  • This basic technique generates implausible texts; we can generate more plausible texts (texts more likely to fool the reader) by using a variety of techniques, some of which are expensive or unethical. The intent to generate plausible random text lies with the author of the model, not the model.
  • If we have the PDF of a target distribution and know basic facts about the areas with high weight and the size of the highly probable portions of the support, we can use the Metropolis algorithm to create a Markov process in which the sequence of states is the sequence of samples from the target distribution.
  • The Metropolis algorithm is particularly useful for generating continuous posterior distributions. Given a distribution that represents our beliefs prior to observations and a likelihood function which models how the world works, we can obtain a posterior distribution that precisely updates our uncertainty in the context of newly observed evidence.

FAQ

What is a Markov process?A Markov process is a random sequence of states where the next state depends only on the current state. It starts from an initial distribution, then repeatedly transitions based on the current state until it stops or continues indefinitely.
What kinds of things can a state in a Markov process represent?A state can be almost anything, such as a city intersection, an amount of money, a word in a sentence, or a continuous value like a real number. Markov processes can work with both discrete and continuous states.
How does the general-purpose Markov process implementation work?It stores an initial distribution and a transition function. Sampling begins by drawing the first state from the initial distribution, then repeatedly samples the next state from the transition distribution returned by the current state.
What is the gambler’s ruin example used to demonstrate?It shows a Markov process with a finite termination condition. The gambler’s chip count changes by +1 or -1 until reaching 0 or 10, at which point the process ends.
Why do gambler’s ruin samples usually have finite length?Because the process stops when the gambler reaches an absorbing state, such as 0 chips or 10 chips. Eventually, the random walk reaches one of those endpoints and terminates.
How can Markov processes be used to generate random text?By treating words as states, using the first word of each sentence as the initial distribution, and using word-to-word transitions based on which words follow each other in a corpus. Sampling from this process generates new sentences.
Why do simple Markov text generators often produce nonsensical output?Because they only remember the current state, not the broader context. If the state is just one word, the generator can lose coherence across a sentence, so the output may sound Shakespearean but not actually make sense.
How is the posterior distribution computed from a prior and likelihood?Using Bayes’ theorem, the posterior PDF is proportional to prior(a) × likelihood(a).Weight(evidence). The normalization constant is not needed if the sampling algorithm can handle a non-normalized PDF.
What does the Metropolis algorithm do?It samples from a target distribution using only a non-normalized PDF. It proposes a new sample near the current one, compares target weights, and accepts or rejects the proposal with a probability based on the weight ratio.
Why is the Metropolis algorithm useful for posterior sampling?It lets us sample from continuous posterior distributions even when we cannot directly normalize the posterior PDF. We can use the prior as the initial distribution and a centered proposal distribution to generate samples that approximate the posterior.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Fabulous Adventures in Data Structures and Algorithms ebook for free