The Long Run
← Back to blog

Data Science

What is a Markov Chain?

Part 1: What is a Markov chain?, Explained with simple examples of Snakes & Ladders and City Traffic.

·11 min read
Covered so far
  • The Marketing Channel Attribution series covered six rule-based models: First Click, Last Click, Last Non-Direct Click, Linear, Time Decay and Position Based
  • Each one splits credit by a fixed rule, and none of them learns from the data
In this part
  • What is a Markov chain. States, Transitions and Transition probabilities
  • Explained with simple examples of Snakes & Ladders and Traffic in Sampur
  • The removal effect when the flyover closes in the Traffic example
  • How the removal effect maps to marketing channels

The earlier Marketing Channel Attribution series covered rule-based attribution models: First Click, Last Click, Last Non-Direct Click, Linear, Time Decay and Position Based. Each one splits credit by a rule chosen in advance. None of them looks at the data to learn how customers actually move between channels.

Data-driven attribution fills that gap by learning credit from real customer journeys. The Markov chain model is one of the most widely used ways to do it (Shapley values are another). This series builds the Markov approach from the ground up.

What is a Markov chain?

A Markov chain is a model of something that moves from one situation to another, one step at a time, where the chance of each next step depends only on where it is right now.

in another words,

A Markov chain is a way to describe a process that moves through a series of states, where the probability of moving to the next state depends solely on the current state and not on the sequence of events that preceded it.

or in another words,

A Markov chain is a mathematical system that moves from one state to another based on probabilistic rules, where the probability of the next state depends only on the current state and not on the sequence of events that preceded it

or,

In simple words, a Markov chain is a map of where things can go next and how likely each move is. It follows one simple rule: to predict the next step, we only need to know the current situation, not how we got there*.

Ex:

  1. A game piece moving from square to square is one such example.
  2. A online shopper clicking from an online ad to a product page and then to checkout is another.

To understand the parts of a Markov chain, let's consider a weather example.The daily weather in a city can be either Sunny, Cloudy or Rainy, and any types of weather can be followed by any other weather next day. The numbers are made up for explanation purpose only.

Figure 1: A three-state weather chain. The boxes are states, the arrows are transitions and the numbers are transition probabilities.

A Markov chain has three components:

  • States: It is the distinct situations the process can be in. The weather example has three states: Sunny, Cloudy and Rainy.
  • Transitions: It is the move from one state to the next at each step. "Sunny today, Rainy tomorrow" is a transition. "Sunny today, Sunny tomorrow" is another. Staying the same weather the next day is also a transition, and counts as a move. With three states there are nine possible transitions, and in the above example all of them can happen.
  • Transition probabilities: It is the the chance (or likelihood or probability) of each transition. After a sunny day there is a 60% chance of more sun, a 30% chance of cloud and a 10% chance of rain. Every move is possible, but some are far more likely than others. The probabilities leaving any one state always add up to 1 (100%), because something has to happen next: for example Sunny, 0.6 + 0.3 + 0.1 = 1.

All of these probabilities can be fit in one table, with today's weather as the rows and tomorrow's as the columns. In the Part 2 where we understand more about Markov chains, we call this table with a formale name - Transition Probability Matrix (TPM).

Today \ TomorrowSunnyCloudyRainyRow total
Sunny0.60.30.11
Cloudy0.30.30.41
Rainy0.20.30.51

The chain can also look more than one day ahead, using two simple calculations.

  1. Multiply along a path. The chance that a sunny Monday is followed by a cloudy Tuesday and then a rainy Wednesday is 0.3 × 0.4 = 0.12, or 12%. (Sunny → Cloudy is 0.3, and Cloudy → Rainy is 0.4, so multiply them together.)
  2. Add across paths. A sunny Monday can reach a rainy Wednesday in three ways, depending on Tuesday's weather:
  • via Sunny (0.6 × 0.1 = 0.06),
  • via Cloudy (0.3 × 0.4 = 0.12) or
  • via Rainy (0.1 × 0.5 = 0.05).

Adding them gives 0.06 + 0.12 + 0.05 = 0.23, so there is a 23% chance of rain two days after a sunny day.

The above two calculations is used later in this post, when we count how many cars reach the airport.

The rule that we discussed at the start of this section, that only the current situation matters, has a name: the Markov property, also called memorylessness. In the weather example, tomorrow's chances depend only on today. A cloudy day has the same 30/30/40 odds whether it followed a sunny day or a rainy one. The idea is named after the Russian mathematician Andrey Markov, who began studying such processes in 1906.

Real weather has more weather states than this, but this example is limited to 3 weather states to show the structure. In Part 3 of this series we look at what to do when one step of memory isn't enough.

📌 Key idea: To predict the next step, a Markov chain needs one piece of information: where the process is now.

Example 1: Snakes and Ladders

This example aims to explain the concepts of:

  • States
  • Transitions
  • Transition Probabilities
  • It also introduces a special kind of state called an absorbing state.

Snakes and Ladders is a Markov chain most of us played as children. Figure 2 shows a small 25-square board.

Figure 2: A small Snakes and Ladders board. The player's square is the state; the die roll decides the transition.

Here is how the three components show up on the board:

  • States: Each square is a state, so the board has 25 of them.
  • Transitions: On each turn the die roll moves the player from one square to another. Ladders and snakes are transitions that jump several squares at once.
  • Transition probabilities: A fair die gives each number a 1 in 6 chance. A player on square 13 moves to square 14, 15, 16, 17, 18 or 19, each with probability 1/6. Square 1 is more interesting. Rolling a 1 lands on square 2, where a ladder lifts the player to 19, and rolling a 4 lands on square 5, where a ladder lifts them to 16. So from square 1 the next state is 3, 4, 6, 7, 16 or 19, each with probability 1/6. Ladders and snakes change where a roll ends up, not the odds of the roll.

The board also shows memorylessness property. A player on square 13 has the same chances on the next roll whether they got there through a run of lucky sixes or after sliding down a snake a few turns earlier, only the current square matters.

Square 25 is a special kind of state. Once a player reaches it, the game ends and the player never move again. A state that cannot be left is called an absorbing state. On this board, square 25 is the only one: reaching it means winning, and there is no move after that.

Absorbing states are central to attribution, because every customer journey eventually ends in one of two absorbing outcomes: a purchase or abandonment.

Example 2: Traffic in a city

In the above example of Snakes and Ladders we learned the building blocks. This example adds the more relevant aspects that matter for marketing channel attribution:

  • The transition probabilities differ from move to move, and they come from observed data rather than from a die.
  • There are two absorbing states competing with each other: a car either reaches the airport or leaves the city.
  • It raises a question the board game cannot: how much does one road contribute to successful trips? The answer to this question is the removal effect.

Consider the fictional city of Sampur. Cars enter the city, pass through a sequence of junctions, and finish in one of two ways: they reach the airport, or they leave the city by some other road. Traffic surveys have recorded the share of cars taking each turn at every junction.

Figure 3: Sampur's road network as a Markov chain. Numbers on arrows are transition probabilities.

Each box in Figure 3 is a state and each arrow is a possible transition. The number on an arrow is the transition probability of that move. At Market Square, for instance, 50% of cars take the flyover, 20% turn towards Ring Road and 30% leave the city. The three grey "Left city" boxes represent a single state, drawn three times to keep the diagram readable. Airport and Left city are the two absorbing states.

The Markov property here states that a car at Ring Road behaves the same way whether it came directly from the city entry or by way of Market Square. In Part 3 of this blog we find more when this assumption is reasonable and what to do when it fails.

Following these probabilities, about 49.5% of cars entering Sampur eventually reach the airport. The section on simulated cars below shows how to work that figure out by manual calculation.

Flyover Closes : Removal Effect

Suppose the city plans to close the flyover for repairs and wants to know how much it matters. Counting the cars that use it is a poor answer, because heavy use does not prove that trips depend on it. The more useful question is how many airport arrivals would be lost without it.

To answer that question, we make one simplifying assumption: a car that would have taken the flyover cannot continue and leaves the city. In Markov terms, every transition into the flyover is redirected to the Left city state, while every other probability stays the same.

Figure 4: Before and after the closure. Trips heading for the flyover are redirected to "Left city".

Measuring the impact by simulation

A simulation makes the effect concrete. Each of 10,000 virtual cars starts at the city entry and, at every junction, picks its next location at random according to the transition probabilities, stopping when it reaches an absorbing state. The experiment is run once with the flyover open and once with it closed.

Figure 5: Outcomes of 10,000 simulated trips with the flyover open (blue) and closed (red).

In Figure 5 the horizontal axis shows the two possible outcomes and the vertical axis counts cars. Blue bars represent the open network and red bars the closed one. With the flyover open, 4,878 cars reach the airport. With it closed, only 1,069 do. The impact of the closure is summarised by the removal effect:

Removal effect = 1 − (airport arrivals with the road closed 
                                  ÷ airport arrivals with all roads open)
               = 1 − (1,069 ÷ 4,878)
               ≈ 0.78

Roughly 78% of airport arrivals depend on the flyover. Keep in mind that 4,878 and 1,069 are the counts from one random run of the simulation, so they wobble slightly from run to run. The exact expected counts are 4,947 and 1,061, which give the exact removal effect of 78.5%. The next section shows how those numbers come from.

Where do the 10,000 cars and these numbers come from?

The number 10,000, is simply a large, round number of virtual cars. Each car's trip is random, so the totals shift a little from one run to the next, and the more cars you simulate, the smaller that shift becomes. With 10,000 cars, the airport count usually lands within about 100 cars of its expected value.

How this simulation workds: The car starts at City entry, and a random number decides its first turn: 60% of the time it goes to Market Square and 40% of the time to Ring Road. At Market Square another random number picks Flyover (50%), Ring Road (20%) or Left city (30%), and so on until the car reaches either the Airport or Left city. Repeat this for 10,000 cars and count where they finished.

We can also get the expected numbers without a computer, using two calculations:

  1. The chance of a route is the probabilities along it multiplied together.
  2. The chance of reaching the airport is the sum of the chances of every route that ends there.

Table 1 applies these calculations to the six main routes to the airport.

Route to the airportCalculationChanceCars out of 10,000Uses the flyover?
Entry → Market Square → Flyover → Airport0.6 × 0.5 × 0.824.0%2,400Yes
Entry → Ring Road → Flyover → Airport0.4 × 0.3 × 0.89.6%960Yes
Entry → Ring Road → Airport0.4 × 0.28.0%800No
Entry → Market Square → Ring Road → Flyover → Airport0.6 × 0.2 × 0.3 × 0.82.88%288Yes
Entry → Market Square → Ring Road → Airport0.6 × 0.2 × 0.22.4%240No
Entry → Ring Road → Market Square → Flyover → Airport0.4 × 0.1 × 0.5 × 0.81.6%160Yes
Total of the six routes48.5%4,848

Table 1: The six main routes from City entry to the Airport with the flyover open.

The six routes add up to 48.5%, or about 4,848 cars. A few cars loop between Market Square and Ring Road one or more times before reaching the airport, and those longer routes add the remaining 1%. That brings the total to 49.5%, or about 4,947 cars.

Now close the flyover. Every route that uses it disappears, and only the two routes marked "No" survive: 8.0% + 2.4% = 10.4%. Adding the loop routes gives 10.6%, or about 1,061 cars. Putting these numbers into the formula:

Removal effect ≈ 1 − (10.4% ÷ 48.5%)
               ≈ 1 − 0.215
               ≈ 0.785

That is the 78.5% quoted above.

💡 Tip: The removal effect compares two scenarios of the same model. It measures dependency, which is different from popularity: a road can carry heavy traffic and still have a small removal effect if trips have good alternatives.

Mapping the flyovers concept to Marketing Channels

Every element of the traffic example has a direct counterpart in attribution. These are summarized in the table below, which shows how the removal effect of a road maps to the removal effect of a marketing channel.

Traffic networkCustomer journey
City entryStart of a customer journey
Junctions (Market Square, Ring Road, Flyover)Marketing touchpoints (paid search, social, email)
Turning probabilitiesProbability that a customer moves from one touchpoint to the next
AirportConversion
Left cityNull outcome: the journey ends without a conversion
Closing the flyoverRemoving a channel from the journey data
Removal effectShare of conversions that depend on the channel

The tip above applies to marketing as well. A channel can appear in many journeys and still contribute little if customers would have converted through other routes.

Key Takeaways

  • A Markov chain consists of states, transitions and transition probabilities, and each state's outgoing probabilities sum to 1.
  • The Markov property means the next move depends only on the current state.
  • Absorbing states, such as the finish square or the airport, end the process; attribution uses two of them, conversion and null.
  • The chance of a route is the product of the probabilities along it, and the chance of an outcome is the sum over every route that leads to it.
  • The removal effect measures how many successful outcomes disappear when one state is removed, and it captures dependency rather than popularity.
  • Sampur's road network will serve as the running example in Parts 2 to 4.

➡️ Up next: Part 2: The Transition Probability Matrix (TPM). Part 1 gave the probabilities. Part 2 starts from a log of 2,000 car trips and shows how to work them out: count every move, divide by the row totals, and use the result to look several moves ahead.

Quick Quiz: Test Yourself

🧠 Test yourself: 5 questions (click to open)

1. What does the Markov property (memorylessness) state?

2. In the Sampur example, which of these is an absorbing state?

3. With all roads open, 50% of cars reach the airport. With one junction closed, 20% do. What is that junction's removal effect?

4. Why is counting the cars that use the flyover not enough to measure its importance?

5. In attribution, what does the "Left city" state correspond to?

0 of 5 answered

Unsure about a term? See the Markov Chain Attribution Jargon Checklist.

In case of any queries or feedback, feel free to drop a comment or reach out using the Contact section.

Comments