Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 17: Introduction to Markov Chains

Welcome to Chapter 17! In this chapter, we venture into the world of stochastic processes by exploring Markov Chains. Markov chains are a fundamental concept used to model systems that transition between different states over time, where the future state depends only on the current state, not on the sequence of events that preceded it. This ‘memoryless’ property makes them incredibly useful for modeling various real-world phenomena, from weather patterns and stock market movements to customer behavior and website navigation.

Learning Objectives:

16.1 What is a Markov Chain?

A Markov chain is a mathematical model describing a sequence of possible events (or states) where the probability of transitioning to the next state depends only on the current state and not on the sequence of states that preceded it. This is known as the Markov Property (or memorylessness).

Key components:

Example: Customer Subscription Model

Consider a company offering subscription plans: Free, Basic, and Premium. Customers can switch plans month-to-month, or they might churn (cancel). We can model this as a Markov chain:

16.2 The Transition Matrix

The transition probabilities of a Markov chain with kk states can be conveniently organized into a k×kk \times k matrix called the Transition Matrix, often denoted by PP.

P=(P11P12⋯P1kP21P22⋯P2k⋮⋮⋱⋮Pk1Pk2⋯Pkk)P = \begin{pmatrix} P_{11} & P_{12} & \cdots & P_{1k} \\ P_{21} & P_{22} & \cdots & P_{2k} \\ \vdots & \vdots & \ddots & \vdots \\ P_{k1} & P_{k2} & \cdots & P_{kk} \end{pmatrix}

Where PijP_{ij} is the probability of transitioning from state ii to state jj in one step.

Properties of a Transition Matrix:

  1. All entries must be non-negative: Pij≥0P_{ij} \ge 0 for all i,ji, j.

  2. The sum of probabilities in each row must equal 1: ∑j=1kPij=1\sum_{j=1}^{k} P_{ij} = 1 for all ii. (From any state ii, the system must transition to some state jj in the next step).

Example: Subscription Model Transition Matrix

Let’s define a plausible transition matrix for our subscription model. States are ordered: 0: Free, 1: Basic, 2: Premium, 3: Churned.

From \ ToFreeBasicPremiumChurned
Free0.600.200.100.10
Basic0.100.600.200.10
Premium0.050.100.700.15
Churned0.000.000.001.00
Transition Matrix P:
[[0.6  0.2  0.1  0.1 ]
 [0.1  0.6  0.2  0.1 ]
 [0.05 0.1  0.7  0.15]
 [0.   0.   0.   1.  ]]

Row sums: [1. 1. 1. 1.]

16.3 Simulating Markov Chain Paths

We can simulate the progression of a system through states over time using the transition matrix. Given a current state, we use the corresponding row in the transition matrix as probabilities to randomly choose the next state.

We can use numpy.random.choice for this.

Simulated 12-month journey starting from Free:
Free -> Free -> Free -> Free -> Premium -> Premium -> Premium -> Premium -> Premium -> Premium -> Premium -> Churned -> Churned

Run the simulation cell multiple times to see different possible paths a customer might take.

16.4 n-Step Transition Probabilities

The transition matrix PP gives the probabilities of moving between states in one step. What if we want to know the probability of transitioning from state ii to state jj in n steps?

This is given by the (i,j)(i, j)-th entry of the matrix power PnP^n.

Pij(n)=P(Xt+n=sj∣Xt=si)=(Pn)ijP^{(n)}_{ij} = P(X_{t+n} = s_j | X_t = s_i) = (P^n)_{ij}

We can calculate matrix powers using numpy.linalg.matrix_power.

Transition Matrix after 3 steps (P^3):
[[0.264 0.244 0.208 0.284]
 [0.132 0.293 0.282 0.294]
 [0.085 0.15  0.396 0.369]
 [0.    0.    0.    1.   ]]

Probability(State=Premium at month 3 | State=Free at month 0): 0.208
Probability(State=Churned at month 6 | State=Basic at month 0): 0.521

16.5 Classification of States (Brief Introduction)

States in a Markov chain can be classified based on their long-term behavior:

In our subscription model:

16.6 Stationary Distributions

For certain types of Markov chains (specifically, irreducible and aperiodic ones), the distribution of probabilities across states converges to a unique stationary distribution (or steady-state distribution), regardless of the initial state. Let π=[π1,π2,...,πk]\pi = [\pi_1, \pi_2, ..., \pi_k] be the row vector representing this distribution, where πj\pi_j is the long-run probability of being in state jj.

The stationary distribution π\pi satisfies the equation:

πP=π\pi P = \pi

subject to the constraint that ∑j=1kπj=1\sum_{j=1}^{k} \pi_j = 1.

This means that if the distribution of states is π\pi, it will remain π\pi after one more step. π\pi is the left eigenvector of the transition matrix PP corresponding to the eigenvalue λ=1\lambda = 1.

Important Note: Our subscription model has an absorbing state. In such cases, the long-run probability will eventually concentrate entirely in the absorbing state(s). For any starting state other than ‘Churned’, the probability of being in ‘Churned’ approaches 1 as n→∞n \to \infty. The concept of a unique stationary distribution across all states applies more directly to chains where you can always move between states (irreducible chains).

Example: Finding Stationary Distribution for a Weather Model

Let’s consider a simpler, irreducible weather model (Sunny, Cloudy, Rainy) to illustrate finding a stationary distribution.

Weather States: ['Sunny', 'Cloudy', 'Rainy'] Weather Matrix W:

   Sunny Cloudy Rainy
Sunny  [0.7,  0.2,   0.1]
Cloudy [0.3,  0.5,   0.2]
Rainy  [0.2,  0.4,   0.4]

We need to find π=[πS,πC,πR]\pi = [\pi_S, \pi_C, \pi_R] such that πW=π\pi W = \pi and πS+πC+πR=1\pi_S + \pi_C + \pi_R = 1. This is equivalent to finding the left eigenvector for eigenvalue 1.

Stationary Distribution (Long-run probabilities):
  Sunny: 0.4681
  Cloudy: 0.3404
  Rainy: 0.1915

Verification (pi * W): [0.46808511 0.34042553 0.19148936]

This stationary distribution tells us that, in the long run, regardless of whether today is Sunny, Cloudy, or Rainy, the probability of a future day being Sunny is about 44.7%, Cloudy is about 36.8%, and Rainy is about 18.4%.

16.7 Hands-on: Analyzing the Subscription Model Long-Term

Let’s use our simulation and n-step transition probabilities to see what happens to subscribers in the long run in our original model with the ‘Churned’ state.

Distribution of states after 24 months starting from 'Basic' (based on 5000 simulations):
  Free: 0.0080
  Basic: 0.0162
  Premium: 0.0206
  Churned: 0.9552

Theoretical probabilities after 24 months starting from 'Basic':
  Free: 0.0090
  Basic: 0.0142
  Premium: 0.0212
  Churned: 0.9556

Notice how the simulation results closely match the theoretical probabilities calculated using the matrix power PnP^n. Also, observe that as the number of steps (simulation_length) increases, the probability mass shifts significantly towards the ‘Churned’ state, as expected for a model with an absorbing state.

16.8 Summary

In this chapter, we introduced Markov chains, a powerful tool for modeling systems that transition between states based only on their current state.

We learned:

Markov chains form the basis for many more advanced models and algorithms in fields like reinforcement learning, finance, genetics, and operations research.

Exercises

  1. Simple Weather Model: Consider the weather model (Sunny, Cloudy, Rainy) with transition matrix W. If it’s Sunny today, what is the probability it will be Rainy the day after tomorrow (in 2 steps)? Calculate this using matrix multiplication.

  2. Gambler’s Ruin (Simulation): A gambler starts with $3. They bet $1 on a fair coin flip (50% chance win, 50% chance lose). They stop if they reach $0 (ruin) or $5 (win). a. Define the states (amount of money: 0, 1, 2, 3, 4, 5). b. Define the transition matrix. Note the absorbing states 0 and 5. c. Simulate 10000 games starting from $3. What proportion end in ruin ($0) and what proportion end in winning ($5)?

  3. Stationary Distribution Verification: For the weather model, manually verify that the calculated stationary distribution π\pi satisfies πW=π\pi W = \pi.

  4. Modify Subscription Model: Change the ‘Churned’ state in the subscription model P so that there’s a small probability (e.g., 0.05) of a churned customer re-subscribing to the ‘Free’ plan each month (adjust the P33P_{33} probability accordingly so the row still sums to 1). Is the chain still absorbing? Try to find the new stationary distribution using the eigenvector method. What does it represent now?

(Exercise 1) Probability Sunny -> Rainy in 2 steps: 0.1500

(Exercise 2) Gambler's Ruin Simulation (10000 games starting at $3):
  Proportion ending in Ruin ($0): 0.3973
  Proportion ending in Win ($5): 0.6027

(Exercise 3) Stationary Distribution Verification:
  Calculated pi: [0.46808511 0.34042553 0.19148936]
  Result pi * W: [0.46808511 0.34042553 0.19148936]
  Are they close? True

(Exercise 4) Modified Transition Matrix:
[[0.6  0.2  0.1  0.1 ]
 [0.1  0.6  0.2  0.1 ]
 [0.05 0.1  0.7  0.15]
 [0.05 0.   0.   0.95]]

Is 'Churned' still absorbing? False
The chain is no longer absorbing, as state 'Churned' can transition to 'Free'.
The chain should now be irreducible if all other states can eventually reach Churned and Churned can reach Free.

Stationary Distribution for Modified Matrix:
  Free: 0.1205
  Basic: 0.0843
  Premium: 0.0964
  Churned: 0.6988

This represents the long-run proportion of time the system spends in each state.
Even though customers churn, the small chance of returning means there's a non-zero steady state for all plans.

Verification (pi_mod * P_mod): [0.12048193 0.08433735 0.09638554 0.69879518]