Interactive lab · continuous-time Markov chains
CTMC Rate Lab
This lab builds up to a continuous-time Markov chain from a single coin flip. We start with a Bernoulli trial, repeat it to get the geometric distribution, and draw that as a two-state Markov chain. Then we run the trials every Δt time units and shrink Δt, so that the geometric becomes the exponential. Sections 5 to 9 then move to chains with several states. They contrast the transition probability matrix P of a discrete-time chain with the transition rate matrix Q of a continuous-time chain, explain what qjk and qjj mean, and end with Equations 6.12 and 6.13.
We write the success probability of one trial as lower-case p, keeping capital P for matrices and for probabilities of events. Notation follows Equation 6.12: qkj is the rate from state k to state j, so row k of Q lists the ways out of k.
How it works and what to try
How it works: It builds up in steps. A Bernoulli trial, repeated until the first success, gives the geometric distribution, drawn as a two-state chain. Running one trial every Δt with p = qΔt and shrinking Δt turns the geometric into the exponential. With several states, a discrete chain that ticks every Δt runs beside a continuous one that jumps at any instant. Racing exponential alarm clocks show what qjk and qjj mean, and the page derives P(Δt) ≈ I + QΔt. Probability then flows between states like fluid between tanks, which leads to Equations 6.12 and 6.13.
Try this: Drag Δt towards zero and watch the geometric staircase close in on e−qt. Run 2,000 clock races and check that the holding time is Exp(−qjj) and that each clock wins a share qjk/(−qjj) of the races. Then edit Q and press Jump to π* to see every tank’s inflow balance its outflow.
Equation numbers refer to Chapter 6 of S. Keshav, Mathematical Foundations of Computer Networking, Addison-Wesley, 2012.
A Bernoulli trial
A Bernoulli trial is a single experiment with exactly two outcomes: success with probability p, and failure with probability 1 − p. Tossing a fair coin and calling heads a success is a Bernoulli trial with p = 0.5; rolling a six with a die is one with p = 1/6. One way to run a trial is to pick a uniform random number U between 0 and 1 and call it a success when U < p, which happens with probability exactly p.
Repeat until the first success: the geometric distribution
Now run independent Bernoulli trials, all with the same p, until the first success. Let X be the number of failures before that success, so X can be 0, 1, 2, and so on. The event X = k means k failures followed by one success. The trials are independent, so we multiply their probabilities:
P(X = k) = (1 − p)k p, k = 0, 1, 2, …
The geometric as a two-state Markov chain
We can draw the same experiment as a Markov chain with two states. The chain starts in F, meaning no success yet. Each trial is one step. With probability 1 − p the trial fails and the chain stays in F, which is the self-loop. With probability p it succeeds and the chain moves to S. Once in S it stays there with probability 1, so S is an absorbing state.
Shading shows the probability of being in each state after n = 0 steps, starting in F.
Here is a picture of the same thing that we will use again later. Think of probability as a fluid held in the states. At every step, a pump on the arrow F → S moves a fraction p of whatever is currently in F into S. The transition from F to S therefore happens at a rate of p per step: for each unit of probability in F, an amount p is pumped into S at each step. The amount actually pumped at step n is p · πF(n), which shrinks as F empties, and that is exactly the geometric probability P(X = n). Section 4 turns “p per step” into “q per unit time” through p = qΔt. Section 8 uses the same pump picture for a general chain, where the arrow k → j carries probability at qkjπk(t) per unit time.
Trials on a clock: from a probability per step to a rate, and the exponential
Now put the trials on a clock, with one trial every Δt time units. Suppose successes happen at a rate of q per unit time, so that one short slot of length Δt succeeds with probability p = qΔt. If we halve Δt, the probability per trial halves, but there are twice as many trials per unit time, so the rate q = p/Δt is unchanged.
In the two-state chain of section 3, this means taking one step every Δt time units with p replaced by qΔt: F has a self-loop of 1 − qΔt, the arrow F → S carries qΔt, and S keeps everything it receives.
In the pump picture of section 3, each step moves a fraction qΔt of the probability in F into S. There are 1/Δt steps per unit time, so for each unit of probability it holds, F loses qΔt × 1/Δt = q per unit time. This rate does not depend on Δt, so it doesn't change even as Δt goes to zero. The continuous chain on the right has no steps and no self-loops, only an arrow labelled with the rate q.
Shading shows the probability in each state at time t = 0, starting in F. On the left πF = (1 − qΔt)n after n steps; on the right πF = e−qt.
Shrink Δt: the geometric becomes the exponential
Keep the rate q fixed and let Δt shrink. The first success happens at time T = (X + 1)Δt. By time t there have been ⌊t/Δt⌋ trials, and no success yet means that all of them failed:
P(T > t) = (1 − qΔt)⌊t/Δt⌋ → e−qt as Δt → 0
This is the standard limit (1 − x/m)m → e−x, with x = qt and m = t/Δt trials. In the discrete picture every slot multiplies the probability of no success yet by 1 − qΔt. In the limit, every unit of time multiplies it by e−q.
The exponential distribution
A random time T ≥ 0 has the exponential distribution with rate q, written Exp(q), when
- the probability that it has not yet happened by time t is P(T > t) = e−qt. Its CDF is therefore 1 − e−qt and its density is f(t) = qe−qt.
- Its mean is 1/q and its variance is 1/q2. A higher rate means shorter waits.
- It is memoryless: P(T > s + t | T > s) = e−qt. Having already waited s tells you nothing about how much longer you will wait. This is the continuous form of the geometric's rule of multiplying by 1 − p for each extra failure, and the exponential is the only continuous distribution with this property.
- Its rate is constant: if nothing has happened yet, the chance that it happens in the next short interval dt is about q dt. That is the p = qΔt we started this section with.
A ticking clock versus a running clock
In sections 1 to 4 the chain had only one way to leave its starting state. Now allow several states, each with arrows to several others. Both chains below have the same arrows. The discrete chain acts only at clock ticks, one step every Δt time units; at each tick it uses a row of P, which can send it back to where it already is (the self-loops). The continuous chain sits in a state for a random, exponentially distributed time and then jumps, never to itself.
The continuous chain is described by a matrix of rates, the transition rate matrix Q. For now, read it as a table: off the diagonal, qjk is the rate of jumping from j to k, and each diagonal entry is chosen so that its row sums to 0. Section 6 explains what these entries mean, and section 7 derives Q and its relation to P in detail. Edit the rates in Q and every view from here on updates.
Rate matrix Q (edit the off-diagonal rates; the diagonal is computed)
Red diagonal cells: qjj = −(sum of the other entries in row j). Each row sums to 0 because probability leaving j must arrive somewhere else.
Discrete time: one step every Δt = 0.2
Continuous time: jumps at any instant
Grey tick marks show every instant the discrete chain checks its die. The continuous chain's jumps fall anywhere on the time axis.
The long-run share of time in each state agrees for the two chains. That is no accident: πP = π + Δt·πQ, so πP = π exactly when πQ = 0. Section 7 shows where P = I + QΔt comes from.
What qjk and qjj mean: racing alarm clocks
When the chain enters state j, start one alarm clock for every arrow out of j. The clock for j → k rings after an Exp(qjk) time, so a larger rate means it tends to ring sooner. The first clock to ring decides both when the chain leaves and where it goes. Each clock is like the F → S arrow of section 4, and its waiting time is memoryless. The minimum of independent exponentials is itself exponential, with rate equal to the sum of the rates: that sum is −qjj.
When does the chain leave 1?
Where does it go?
Bars: share of races each clock won. Black marks: qjk / (−qjj). These are the jump probabilities of the embedded discrete chain, which records only where the chain goes and ignores how long it waits.
The rate matrix Q: why P(Δt) = I + QΔt
Worked example: the two-state chain of section 4
Now write the two-state chain of section 4 in matrix form, and see where the rate matrix used in sections 5 and 6 comes from. We work it out on the two-state chain first, because it is the simplest case, and then repeat the steps for the chain of section 5. With p = qΔt, the one-step transition matrix P(Δt) is exactly the identity plus Δt times a new matrix Q, called the transition rate matrix. Here is why, step by step.
- Write down the one-step matrix. In one slot, a chain in F succeeds with probability qΔt and fails with probability 1 − qΔt. A chain in S stays in S with probability 1. With rows and columns in the order F, S:
P(Δt) = 1 − qΔtqΔt01
- Subtract the identity. I is the matrix of a slot in which nothing happens, so P(Δt) − I keeps only the change. Every entry of the difference has a factor of Δt:
P(Δt) − I = −qΔtqΔt00 = Δt · −qq00 = QΔtSo P(Δt) = I + QΔt, and each entry of Q = (P(Δt) − I)/Δt is a change in probability per unit time.
- Off-diagonal entries are rates. qFS = PFS(Δt)/Δt = qΔt/Δt = q. This is the probability of jumping from F to S in one slot, divided by the length of the slot, which is what we mean by the rate of jumping from F to S. It stays the same however small Δt becomes.
- Diagonal entries are minus the total rate of leaving. qFF = (PFF(Δt) − 1)/Δt = −qΔt/Δt = −q. The diagonal of P − I is the probability lost because the chain did not stay in F, so it is negative. Here F has only one way out, so its total rate of leaving is q. When a state has several ways out, each takes its own share of the probability, and the diagonal loses the sum of those shares: qjj = −Σk≠j qjk.
- Each row of Q sums to 0. Each row of P(Δt) sums to 1, because after the slot the chain must be somewhere. Each row of I also sums to 1. So each row of P(Δt) − I sums to 0, and dividing by Δt leaves it at 0. In words, whatever probability leaves F must arrive somewhere else, so the loss on the diagonal exactly cancels the gains off it.
- The row for S is all zeros. The S row of P(Δt) is (0, 1), the same as the S row of I, so the difference is (0, 0). Nothing ever leaves S, so every rate out of S is 0.
A caution: P(Δt) = I + QΔt holds exactly here only because we chose the slot probability to be p = qΔt. If successes can really happen at any instant at rate q, the chance of a success within one slot is 1 − e−qΔt = qΔt − (qΔt)2/2 + …, which is slightly less than qΔt. The exact one-step matrix is then P(Δt) = eQΔt = I + QΔt + Q2Δt2/2 + …, which agrees with I + QΔt only to first order in Δt. In general, therefore, the rate matrix is defined by a limit, Q = limΔt→0 (P(Δt) − I)/Δt, and its entries are the slopes at t = 0 of the transition probabilities. The plot below shows these slopes as the dashed tangent lines.
The same steps for the chain of section 5
Nothing in steps 1 to 6 depended on the chain having only two states. Take the chain of section 5 and look at it every Δt time units. In a short slot, the probability of jumping from j to another state k is about qjkΔt: each arrow pumps a fraction qjkΔt of the probability in j into k. The probability of staying in j is about 1 − Σk≠j qjkΔt = 1 + qjjΔt. Collecting these entries in a matrix gives the same result as before:
P(Δt) ≈ I + QΔt, that is, Q = limΔt→0 (P(Δt) − I)/Δt
As in the two-state case, each off-diagonal entry of Q is a rate, each diagonal entry is minus the total rate of leaving that state, and each row sums to 0 because each row of P(Δt) and of I sums to 1. For the continuous chain the exact one-step matrix is eQΔt. The linear form misses only paths that make two or more jumps within one slot, and these have probability of order Δt2.
From P(Δt) ≈ I + QΔt to Equation 6.12
The distribution one slot later is the current distribution times the one-step matrix:
π(t + Δt) = π(t) P(Δt) ≈ π(t)(I + QΔt) = π(t) + π(t)QΔt
Subtract π(t), divide by Δt, and let Δt → 0:
dπ(t)/dt = limΔt→0 (π(t + Δt) − π(t))/Δt = π(t) Q
The j-th entry of π(t)Q is Σk πk(t) qkj, the sum down column j of Q weighted by π. Splitting off the term k = j gives
dπj(t)/dt = qjjπj(t) + Σk≠j qkjπk(t),
which is Equation 6.12. Section 8 reads this equation as probability flowing between states.
Probability as a fluid: reading Equation 6.12
Equation 6.12, which we derived at the end of section 7, says how the probability πj(t) of being in state j at time t changes over time:
The first term is the rate at which probability flows out of j: it is negative, because qjj ≤ 0. The sum adds the rates at which probability flows into j from every other state k. The rest of this section makes that reading visual.
Instead of following a single trajectory, as in section 5, follow the whole distribution π(t). Think of πj(t) as fluid in tank j. Along each arrow k → j fluid flows at qkj πk(t) per unit time. Tank j drains at (−qjj) πj(t), the sum of its outgoing flows. Pick a tank to see its balance.
Equation 6.13: the stationary distribution
For an ergodic continuous-time Markov chain (for a finite chain, one in which every state can be reached from every other), as t → ∞ the probabilities πj(t) tend to limits π*j that depend neither on time nor on the starting state. These stationary probabilities are defined implicitly by
The first line is Equation 6.12 with the rate of change set to 0, which is what we expect of a stationary distribution, and with πj(t) replaced by its limiting value π*j. There is one such line for each state. In the fluid picture of section 8 it says that every tank loses exactly as much as it gains: (−qjj)π*j = Σk≠j qkjπ*k, outflow equals inflow. These are called the global balance equations. In matrix form they read π*Q = 0, the continuous-time counterpart of π*P = π* for a discrete-time chain.
Why do we need the second line? The balance equations are homogeneous: if π* solves them, so do 2π* and the all-zero vector, so they cannot fix the scale. They also contain one equation too many. If we add all n balance equations, the coefficient of π*k is the sum of row k of Q, which is 0, so the sum is just 0 = 0. The n balance equations therefore give only n − 1 independent constraints. The normalization Σj π*j = 1 supplies the missing one. In practice we drop any one balance equation and use the normalization in its place.
Equation 6.13 for the chain of section 5
Edit the rates in section 5 and these equations update.
Guess and check
Type a candidate distribution and see which balance equations it satisfies. Inflow is Σk≠j qkjπk, outflow is (−qjj)πj, and net is inflow minus outflow.
The absorbing chain of sections 3 and 4 is not ergodic, because it can never return to F, but Equation 6.13 still has a unique solution. Its rate matrix has rows (−q, q) and (0, 0). The balance at F gives −qπ*F = 0, so π*F = 0, and the normalization then gives π*S = 1: in the long run all the probability is in S.
The two matrices side by side
| Transition probability matrix P (discrete time) | Transition rate matrix Q (continuous time) | |
|---|---|---|
| Off-diagonal entry | Pjk = Pr(next state is k | now in j), unitless, in [0, 1] | qjk = rate of jumping j → k, per unit time, any value ≥ 0 |
| Diagonal entry | Pjj = probability of staying put for one step, ≥ 0 | qjj = −Σk≠j qjk = minus the total rate of leaving, ≤ 0 |
| Row sum | 1 | 0 |
| Time in a state | Geometric number of steps, mean 1/(1 − Pjj) | Exponential, mean 1/(−qjj) |
| Evolution | π(n+1) = π(n) P | dπ/dt = π(t) Q, so π(t) = π(0) eQt |
| Stationary distribution | π* P = π*, Σ π*j = 1 | π* Q = 0, Σ π*j = 1 (Equation 6.13) |
| Link | P(Δt) = eQΔt ≈ I + QΔt for small Δt | |