The Long Run
← Back to blog

Data Science

First- vs Second-Order Markov Chains and the Removal Effect

Part 3: does a car's past route change its next move, and how much does each junction matter? The removal effect worked out by hand.

·9 min read
Covered so far
  • Part 1: Parts of a Markov chain and the removal effect
  • Part 2: Building the TPM from sample data and looking several moves ahead
In this part
  • First- vs second-order chains, and a test of which one Sampur needs
  • The removal effect for every junction, worked out by hand
  • What these results mean for marketing attribution

In Part 2 we looked at how to turn 2,000 sample trips through Sampur into a Transition Probability Matrix (TPM). This post uses that TPM to answer two practical questions:

  1. Does the route a car has taken so far change where it goes next? This is the question of first-order versus second-order chains.
  2. How much does each junction matter to airport arrivals? This is the removal effect, which Part 1 introduced with the flyover. Here we work it out for every junction by hand.

This post ends with what these answers mean for marketing attribution, in plain terms. Part 4 shows the matrix maths behind the numbers; it is optional and can be skipped.

First-order and second-order chains

First order: Only the current state(or location) matters

Parts 1 and 2 used a first-order chain. It assumes the next move depends only on the current state, which is the Markov property explained in Part 1.

A first-order chain aims to explain the average behaviour at each location. It answers the question "of all the cars at Ring Road, where do they go next?" The Ring Road row of the TPM built in Part 2 gives the answer: 10.1% to Market Square, 30.5% to the Flyover, 18.3% to the Airport and 41.1% out of the city. Every car at Ring Road gets those same odds, whether it came straight from City entry or by way of Market Square.

Second order: The previous state(or location) matters too

A second-order chain relaxes that assumption, so the next move can depend on the current state and the one before it. It aims to explain whether the route so far changes what happens next.

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

There is a plausible reason it might in Sampur. A driver who reached Ring Road from Market Square has already passed up one chance to take the flyover. That driver may not be heading for the airport at all, and so may be less likely to take the flyover now. A first-order chain can't capture this, because it never looks back.

To let the chain look one step back, each state becomes a pair of locations: where the car is now and where it came from. Ring Road splits into two states, "Ring Road, coming from City entry" and "Ring Road, coming from Market Square", and each gets its own row in the TPM. Figure 1 shows the split.

Figure 1: In a second-order chain, Ring Road becomes two states, one for each junction a car can arrive from.

How the second-order numbers are calculated. They come from the same sample trip log as Part 2, in three steps.

1. Find every visit to Ring Road, and note where the car came from and where it went next. Some examples from the log:

TripPathCame fromWent to next
1City entry > Ring Road > Left cityCity entryLeft city
2City entry > Ring Road > Flyover > AirportCity entryFlyover
4City entry > Ring Road > AirportCity entryAirport
10City entry > Market Square > Ring Road > AirportMarket SquareAirport
31City entry > Market Square > Ring Road > Flyover > Left cityMarket SquareFlyover

Table 1: Example visits to Ring Road from the sample trip log.

2. Group the visits by where the car came from, and count where it went next. Across all 2,000 trips there are 774 visits from City entry and 253 from Market Square. These match Part 2's count table: 774 moves from City entry to Ring Road and 253 from Market Square to Ring Road.

3. Divide each count by its group's total.

Arrived at Ring Road from→ Market Square→ Flyover→ Airport→ Left cityTotal
City entry80 ÷ 774 = 10.3%248 ÷ 774 = 32.0%131 ÷ 774 = 16.9%315 ÷ 774 = 40.7%774
Market Square24 ÷ 253 = 9.5%65 ÷ 253 = 25.7%57 ÷ 253 = 22.5%107 ÷ 253 = 42.3%253
Anywhere (first order)104 ÷ 1,027 = 10.1%313 ÷ 1,027 = 30.5%188 ÷ 1,027 = 18.3%422 ÷ 1,027 = 41.1%1,027

Table 2: Next moves from Ring Road. Each cell shows the count, the group total and the resulting probability.

The first two rows are the second-order chain: each group has its own total. The last row is the first-order chain: it adds the two groups together (80 + 24 = 104, 774 + 253 = 1,027) and divides by the single total, ignoring where the car came from.

The same question matters in marketing. A first-order model treats every visit through email the same way. A second-order model asks whether a customer who reached email after a paid search ad behaves differently from one who reached it from social media. A chain of order k looks back k steps in the same way.

Extra Complexity - Second Order Chain - Perspective about extra memory

The added context comes at a cost. With n locations, a second-order chain can have up to n² paired states and a third-order chain up to n³. For 10 marketing channels, that becomes 100 possible states, and each one needs enough observations to estimate its probabilities reliably.

Splitting states also splits the data. Ring Road's 1,027 moves become groups of 774 and 253, and the smaller group gives noisier estimates. With 100 paired states, many pairs might be seen only a handful of times.

Does Sampur need a second-order chain?

The trip log from Part 2 can test whether the extra memory is needed. Figure 2 compares what happens after Ring Road overall with what happens when the previous location is taken into account.

Figure 2: Next moves from Ring Road, overall (first order) and split by the previous location (second order).

Each group of bars is a possible next location, and bar height is the share of moves going there. The three colours tell broadly the same story. The largest gap is for the flyover, 32% for cars arriving from City entry against 26% for those arriving from Market Square, but the second group contains only 253 moves.

A chi-square test of independence checks whether a gap like this is real. It gives p ≈ 0.11, which means that even if the previous location made no difference at all, gaps at least this large would turn up about 11% of the time by chance. That is too often to treat the gap as real, so a first-order chain is good enough for Sampur. The result is expected, because the sample trips were generated by a first-order process. Real customer journeys often show larger, genuine differences, and that is when a second-order model earns its extra cost.

AspectFirst-order chainSecond-order chain
Next move depends onCurrent state onlyCurrent and previous state
What a state isA single location or channelAn ordered pair, such as "Market Square → Ring Road"
Possible states with n locationsnUp to n²
Data requiredModestMuch larger; sparse pairs give unstable estimates
Main riskMisses sequence effectsOverfitting when data is thin
Typical useDefault starting pointWhen tests show the previous step changes behaviour

💡 Tip: Start with a first-order chain, then test whether conditioning on the previous state changes the next-move distribution. Move to a higher order only when the difference is both statistically clear and large enough to matter, and when each paired state has enough data behind it.

The removal effect, Manual Calcualtion

Part 1 measured the removal effect of the flyover: close it, count how many cars still reach the airport, and compare with the open network.

Removal effect = 1 − (airport arrivals with the junction closed 
                              ÷ airport arrivals with all roads open)

The rule for closing a junction stays the same as in Part 1: a car that arrives at a closed junction cannot continue and leaves the city. So when a junction closes, every route that passes through it is lost, and only the routes that avoid it still reach the airport.

That makes the calculation a matter of striking out routes. Table 3 lists the six main routes to the airport from Part 1, with their chances and the junctions each one uses.

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

#Route to the airportChanceUses Market SquareUses Ring RoadUses Flyover
1Entry → Market Square → Flyover → Airport24.00%YesYes
2Entry → Ring Road → Flyover → Airport9.60%YesYes
3Entry → Ring Road → Airport8.00%Yes
4Entry → Market Square → Ring Road → Flyover → Airport2.88%YesYesYes
5Entry → Market Square → Ring Road → Airport2.40%YesYes
6Entry → Ring Road → Market Square → Flyover → Airport1.60%YesYesYes
Longer routes that loop between Market Square and Ring Road0.99%YesYesMostly
All routes49.47%

Table 3: Routes from City entry to the Airport, their chances and the junctions they use.

With all roads open, 49.47% of cars reach the airport. Now close each junction in turn.

Close Market Square. Only routes 2 and 3 avoid it: 9.60% + 8.00% = 17.60%. Every looping route passes through Market Square, so none of them survive.

Removal effect = 1 − (17.60 ÷ 49.47) = 1 − 0.356 = 0.644, or 64.4%

Close Ring Road. Only route 1 avoids it, so 24.00% of cars still reach the airport. The looping routes all use Ring Road, so they are lost too.

Removal effect = 1 − (24.00 ÷ 49.47) = 1 − 0.485 = 0.515, or 51.5%

Close the Flyover. Routes 3 and 5 avoid it: 8.00% + 2.40% = 10.40%. A few looping cars also reach the airport without the flyover, adding about 0.21%, for 10.61% in total, as in Part 1.

Removal effect = 1 − (10.61 ÷ 49.47) ≈ 1 − 0.2145 ≈ 0.785, or 78.5%
Junction closedCars that still reach the airportRemoval effect
None (all roads open)49.47%Baseline
Market Square17.60%64.4%
Ring Road24.00%51.5%
Flyover10.61%78.5%

Table 4: Removal effect of each junction.

Figure 3: Share of cars reaching the airport when each junction is closed in turn, with the resulting removal effects.

In Figure 3 the blue bar is the open network and each red bar is one closure. The bar height is the share of cars that still reach the airport, and the red label above it is the removal effect. The Flyover matters most, followed by Market Square and then Ring Road.

That order is not the same as the order of traffic. In the 2,000 sample trips from Part 2, cars passed through Market Square 1,330 times, Ring Road 1,027 times and the Flyover 972 times. The Flyover carries the least traffic of the three, yet it has the largest removal effect, because most successful trips end with it and few good alternatives exist. Ring Road carries more traffic than the Flyover but has the smallest removal effect, because cars that skip it still have a strong route through Market Square and the Flyover.

📌 Key idea: A removal effect measures dependency, not traffic. It asks how many successful trips would disappear without a junction, which a simple count of cars cannot tell you.

Why this matters for attribution

Swap the road names for marketing ones and the same logic applies:

  • Junctions become channels, such as paid search, email or social media.
  • Reaching the airport becomes a purchase, and leaving the city becomes a journey that ends without one.
  • Closing a junction becomes asking what would happen if a channel were switched off.

The removal effect then answers a direct business question: how many purchases would we lose without this channel? That is a better guide to a channel's value than how often it appears in journeys. A channel that shows up in many journeys can matter less than one that appears less often but sits on the routes that lead to a purchase, just as the Flyover matters more than the busier Market Square.

There is one more step. The three removal effects add up to 194.4%, well above 100%, because most trips pass through more than one junction, so the same airport arrival depends on several of them at once. To split the credit for each arrival, the effects are scaled so they add up to 100%:

JunctionRemoval effectShare of credit
Flyover78.5%40.4%
Market Square64.4%33.1%
Ring Road51.5%26.5%
Total194.4%100%

Table 5: Removal effects scaled into shares of credit.

Each share is a junction's removal effect divided by the total. For the Flyover, 78.5 ÷ 194.4 = 40.4%. If 100 airport arrivals had to be shared out, the Flyover would get about 40, Market Square 33 and Ring Road 27.

That is the core of Markov attribution. Part 5 applies it to marketing journeys and compares the result with the rule-based models from the earlier series. Part 4, which is optional, shows how to calculate all of these numbers exactly with matrix algebra.

Common mistakes

  • Choosing a high-order chain on a small dataset, where many paired states are seen only a handful of times.
  • Treating the busiest junction, or the most frequent channel, as the most important one. Traffic is not the same as removal effect.
  • Using removal effects directly as credit. They overlap, so they must be scaled to add up to 100% first.

Key Takeaways

  • A first-order chain explains the average behaviour at each state; a second-order chain tests whether the previous state changes the next move, at the cost of far more states and data.
  • For Sampur, a chi-square test (p ≈ 0.11) shows the previous junction makes no real difference, so a first-order chain is enough.
  • Removal effect = 1 − (arrivals with the junction closed ÷ arrivals with all roads open), and it can be worked out by striking out the routes that use the closed junction.
  • The Flyover's removal effect is 78.5%, Market Square's 64.4% and Ring Road's 51.5%; the busiest junction is not the most important one.
  • Removal effects overlap and add up to 194.4%; scaled to 100% they become shares of credit, which is the core of Markov attribution.

➡️ Up next: Part 4: The Fundamental Matrix (optional) shows the matrix maths that gives these numbers exactly. To go straight to marketing, skip to Part 5: Markov Chain Attribution, where Sampur's junctions become channels.

Quick Quiz: Test Yourself

🧠 Test yourself: 5 questions (click to open)

1. A model uses 8 channels. How many paired states can a second-order chain have at most?

2. In a second-order chain for Sampur, what counts as a single state?

3. With all roads open, 40% of cars reach a destination. With one junction closed, 30% do. What is that junction's removal effect?

4. Cars pass through Market Square more often than through the Flyover, yet the Flyover has the larger removal effect. Why?

5. Why do the removal effects of Sampur's three junctions add up to more than 100%?

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