Data Science
The Transition Probability Matrix (TPM): Building It from Trip Data
Part 2: how to turn a log of journeys into a Transition Probability Matrix (TPM), read it, check it and use it to look several steps ahead.
- Part 1 covered: States, transitions and transition probabilities, with weather, Snakes and Ladders and Sampur traffic example
- Removal effect of closing the flyover
- Build the Transition Probability Matrix (TPM)
- Read the TPM and check it against Part 1
- Using the TPM to look several moves ahead
Part 1 built the idea of a Markov chain from three everyday examples:
- Weather: A city's weather moves between Sunny, Cloudy and Rainy. After a sunny day there was a 60% chance of more sun, 30% of cloud and 10% of rain. Those three situations are the states, the day-to-day changes are transitions, and the percentages are transition probabilities.
- Snakes and Ladders: Each square is a state and the die gives every roll a 1 in 6 chance. Square 25 is an absorbing state: once a player reaches it, the game is over.
- Traffic in Sampur: Cars move between junctions until they reach the Airport or leave the city. Closing the flyover cut airport arrivals by 78.5%. That drop is the removal effect, the number that later becomes a marketing channel's credit.
In all three examples the probabilities were simply given. We said 60% of cars turn towards Market Square, and we assumed that these are given somehow. In real life, it doesn't work that way. A traffic office, or a marketing team, has a log of what happened, and the probabilities have to be worked out from it.
This post starts from a sample trip log and shows how the probabilities are calculated from it:
- A look at the sample data: 2,000 car trips through Sampur.
- Turning those trips into probabilities, step by step. The result is a Transition Probability Matrix (TPM).
- What a TPM is, how to read it, and how close our numbers come to Part 1's.
- Using the TPM to look several steps ahead.
Sample data: A trip log from Sampur
📌 About this data: The trip log used in this post is sample data created for this article. Sampur is a fictional city and no real cars were tracked. The trips were generated from the road map in Part 1, so they behave like real traffic and the results can be checked against known values.
In real life, a log like this would come from number-plate cameras or GPS data. Each time a car passes a junction its location is recorded, and the readings for one car are joined into a trip. In marketing the same kind of log comes from web analytics data such as GA4, where each customer journey is the list of channels a customer came through before buying or leaving.
Each row of the log is one trip: a trip ID and the path the car followed. Here are the first five of the 2,000 trips:
trip_id path
1 City entry > Ring Road > Left city
2 City entry > Ring Road > Flyover > Airport
3 City entry > Ring Road > Market Square > Left city
4 City entry > Ring Road > Airport
5 City entry > Market Square > Flyover > AirportA quick profile of the 2,000 trips:
- 959 trips (about 48%) ended at the Airport and 1,041 left the city.
- Most trips are short. 836 trips made two moves and 1,021 made three. Only 143 made four or more, mostly cars that went back and forth between Market Square and Ring Road.
- The five most common routes account for 1,507 of the 2,000 trips:
| Route | Trips |
|---|---|
| City entry → Market Square → Flyover → Airport | 474 |
| City entry → Market Square → Left city | 390 |
| City entry → Ring Road → Left city | 315 |
| City entry → Ring Road → Flyover → Airport | 197 |
| City entry → Ring Road → Airport | 131 |
Table 1: The five most common routes in the 2,000 sample trips.
The log contains no probabilities at all, only what each car did. The next section turns it into probabilities.
Building the TPM from the sample data
Three steps turn the trip log into a table of probabilities.
Step 1: Split each trip into moves
A trip that visits k locations contains k − 1 moves, one for each pair of consecutive locations. Trip 2 (City entry > Ring Road > Flyover > Airport) has three moves:
- City entry → Ring Road
- Ring Road → Flyover
- Flyover → Airport
Trip 3 also has three: City entry → Ring Road, Ring Road → Market Square and Market Square → Left city. Each move is one observation of a transition, and the 2,000 trips contain 5,329 moves in total.
Step 2: Count the moves
Next, tally every move in a table of counts, with the current location as the row and the next location as the column:
| From \ To | Market Square | Ring Road | Flyover | Airport | Left city | Row total |
|---|---|---|---|---|---|---|
| City entry | 1,226 | 774 | 0 | 0 | 0 | 2,000 |
| Market Square | 0 | 253 | 659 | 0 | 418 | 1,330 |
| Ring Road | 104 | 0 | 313 | 188 | 422 | 1,027 |
| Flyover | 0 | 0 | 0 | 771 | 201 | 972 |
Table 2: Counted moves in the trip log. The City entry column is left out because no car returns to it, and the Airport and Left city rows are left out because no car leaves them.
Two quick checks show that the counts hang together:
- Every car leaves City entry exactly once, so that row totals 2,000, the number of trips.
- Every car that arrives at a junction must also leave it. Market Square received 1,226 cars from City entry and 104 from Ring Road, which adds up to its row total of 1,330. The same check works for Ring Road (774 + 253 = 1,027) and the Flyover (659 + 313 = 972).
Step 3: Divide each row by its total
Dividing each count by its row total turns counts into probabilities. Take the Market Square row. Cars left Market Square 1,330 times:
- to the Flyover: 659 ÷ 1,330 = 0.495
- out of the city: 418 ÷ 1,330 = 0.314
- to Ring Road: 253 ÷ 1,330 = 0.190
Doing the same for every row gives the full table of probabilities:
| From \ To | City entry | Market Square | Ring Road | Flyover | Airport | Left city |
|---|---|---|---|---|---|---|
| City entry | 0 | 0.613 | 0.387 | 0 | 0 | 0 |
| Market Square | 0 | 0 | 0.190 | 0.495 | 0 | 0.314 |
| Ring Road | 0 | 0.101 | 0 | 0.305 | 0.183 | 0.411 |
| Flyover | 0 | 0 | 0 | 0 | 0.793 | 0.207 |
| Airport | 0 | 0 | 0 | 0 | 1 | 0 |
| Left city | 0 | 0 | 0 | 0 | 0 | 1 |
Table 3: The Transition Probability Matrix (TPM) built from the 2,000 sample trips. A row may add up to 0.999 because of rounding.
This table is a Transition Probability Matrix (TPM). Every number in it was calculated from the trip log: a count divided by a row total. Figure 1 shows the same two steps as heatmaps.
Figure 1: From counted moves (left) to the TPM (right), using the 2,000 sample trips.
In both panels the vertical axis is the current location and the horizontal axis is the next location; darker cells hold larger values.
What is a Transition Probability Matrix (TPM)?
A Transition Probability Matrix (TPM) is a table that holds every transition probability of a Markov chain. Each row is the state the process is in now, each column is a state it could move to next, and the cell where they meet is the probability of that move. In notation, the cell in row i and column j is written p(i → j): the probability of moving to state j, given that the process is in state i now.
Here is how to read the TPM in Table 3:
- One cell: Market Square → Flyover is 0.495. Of all the cars at Market Square, about half took the flyover next.
- A whole row: each row adds up to 1, because every car at a junction has to go somewhere next. The Ring Road row says 10.1% of cars went to Market Square, 30.5% to the Flyover, 18.3% to the Airport and 41.1% left the city.
- Zeros: most cells are 0 because no car ever made that move. There is no road from City entry straight to the Airport, so that move never appears in the log.
- Absorbing rows: no car ever leaves the Airport or Left city, so those rows have no moves to count. By convention each gets a 1 on its own diagonal cell, meaning a finished trip stays where it ended.
Junctions such as Market Square and Ring Road are called transient states: cars pass through them and eventually leave for good. To describe the chain completely we also need its starting point. In Sampur every car begins at City entry.
Part 1's weather table was a TPM too:
| Today \ Tomorrow | Sunny | Cloudy | Rainy |
|---|---|---|---|
| Sunny | 0.6 | 0.3 | 0.1 |
| Cloudy | 0.3 | 0.3 | 0.4 |
| Rainy | 0.2 | 0.3 | 0.5 |
There the probabilities were simply given. In the Table 3 they come from counting what cars did, which is how a TPM is built in practice.
Checking the TPM
Because the sample data was created from the road map in Part 1, we know what each probability should be. That makes it possible to check the TPM against the values used to create the data:
| Move | TPM from 2,000 sample trips | Part 1 value |
|---|---|---|
| City entry → Market Square | 0.613 | 0.6 |
| City entry → Ring Road | 0.387 | 0.4 |
| Market Square → Ring Road | 0.190 | 0.2 |
| Market Square → Flyover | 0.495 | 0.5 |
| Market Square → Left city | 0.314 | 0.3 |
| Ring Road → Market Square | 0.101 | 0.1 |
| Ring Road → Flyover | 0.305 | 0.3 |
| Ring Road → Airport | 0.183 | 0.2 |
| Ring Road → Left city | 0.411 | 0.4 |
| Flyover → Airport | 0.793 | 0.8 |
| Flyover → Left city | 0.207 | 0.2 |
Table 4: The TPM from the sample trips compared with the Part 1 values used to create them.
Every probability in the TPM is within two percentage points of the Part 1 value. The small gaps are sampling noise: 2,000 trips is a sample, and a different sample would give slightly different counts. With more trips the gaps would shrink. Real data never comes with known values to check against, so testing the method on sample data first shows that counting and dividing gives the right answer.
🔍 Deep dive: how the sample trips were created
The sample trips were created by a small program. Every car starts at City entry. At each junction the program draws a random number between 0 and 1 and checks which range it falls in. Each range is as wide as the Part 1 probability of that move, so Market Square → Flyover, with a probability of 0.5, gets half of the range.
| At this junction | Random number → next location |
|---|---|
| City entry | 0.00–0.60 → Market Square · 0.60–1.00 → Ring Road |
| Market Square | 0.00–0.20 → Ring Road · 0.20–0.70 → Flyover · 0.70–1.00 → Left city |
| Ring Road | 0.00–0.10 → Market Square · 0.10–0.40 → Flyover · 0.40–0.60 → Airport · 0.60–1.00 → Left city |
| Flyover | 0.00–0.80 → Airport · 0.80–1.00 → Left city |
Table 5: Random-number ranges used to pick each car's next location.
For example, Car A draws 0.72 at City entry and goes to Ring Road, then draws 0.55 and reaches the Airport: City entry > Ring Road > Airport. Car B draws 0.31, 0.45 and 0.12, giving City entry > Market Square > Flyover > Airport. Every car gets fresh random numbers, so the 2,000 cars spread across many different routes, and each car stops as soon as it reaches the Airport or Left city.
Over 2,000 cars, each route turns up roughly in proportion to its probability. The route City entry → Market Square → Flyover → Airport has a probability of 0.6 × 0.5 × 0.8 = 0.24, so about 480 cars were expected to take it, and the log has 474. The random numbers start from a fixed point called a seed, so rerunning the program with the same seed produces the same 2,000 trips.
What comes next
Part 3 uses this TPM to answer two practical questions: does the route a car has taken so far change where it goes next, and how much does each junction matter to airport arrivals? Part 4 (optional) then shows the exact matrix maths behind those answers.
Common mistakes
- Discarding journeys that end without success, which inflates every transition towards the good outcome.
- Leaving absorbing rows empty or failing to check that each row sums to 1.
- Pooling periods in which behaviour changed, since the model assumes transition probabilities stay stable.
Key Takeaways
- A Transition Probability Matrix (TPM) holds every transition probability; rows are "from" states, columns are "to" states and each row sums to 1.
- A TPM is built from journey data by splitting trips into moves, counting them and dividing each row by its total, keeping unsuccessful trips in the data.
- Absorbing states have a 1 on the diagonal, and a 0 marks a move that never happens.
- On 2,000 sample Sampur trips, every probability in the TPM lands within two percentage points of the Part 1 value.
- Multiplying the TPM by itself shows where the process will be after any number of steps.
➡️ Up next: Part 3: First- vs Second-Order Markov Chains and the Removal Effect. Does the route a car has taken so far change where it goes next? Part 3 tests first- vs second-order chains and works out the removal effect of every junction by hand.
Quick Quiz: Test Yourself
🧠 Test yourself: 4 questions (click to open)
1. In a TPM, what must every row add up to?
2. Cars left a junction 400 times: 100 times to the airport and 300 times out of the city. What is the estimated probability of moving from that junction to the airport?
3. How does an absorbing state appear in the TPM?
4. Why should trips that end in "Left city" stay in the data?
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.