skip to content

Given a sunny/rainy transition matrix, how do you compute the chance of rain two days from now?

level: middleimportance: should knowfreq 62%

answer

  1. two steps, not two rows
  2. matrix multiplication, not elementwise
  3. sum over the intermediate day
  4. read entry (sunny, rainy) of P squared

basics

~20 s

Raise the transition matrix to the second power. Entry (i, j) of P squared is the probability of being in state j two steps after starting in state i, so read the sunny-to-rainy entry of that matrix.

solid answer

~40 s

Multi-step probabilities come from matrix powers: the (i, j) entry of `P^n` is `P(X_n = j | X_0 = i)`. Take a weather chain with `P(sunny -> sunny) = 0.8`, `P(sunny -> rainy) = 0.2`, `P(rainy -> sunny) = 0.4`, `P(rainy -> rainy) = 0.6`. Squaring gives the sunny row `[0.72, 0.28]` and the rainy row `[0.56, 0.44]`, so starting from a sunny day the chance of rain two days out is 0.28. That 0.28 is `0.8 * 0.2 + 0.2 * 0.6` — it sums over both routes through the intermediate day, which is why you multiply matrices rather than squaring entries. If you only care about one starting distribution, propagate the row vector instead: `v_n = v_0 P^n`, computed as repeated vector-matrix products.

go deeper

for a junior

Be ready to say that n-step probabilities come from the nth power of the transition matrix and to read the right entry out of a two-by-two result.

for a middle

Explain why the power works — the sum over the intermediate state, i.e. Chapman-Kolmogorov — and do the two-by-two arithmetic correctly on the spot.

for a senior

Show judgment about computation: propagate a vector instead of forming the power, use repeated squaring for large horizons, and sanity-check rows summing to 1.

for a principal

Frame when an exact matrix computation is the right tool at all, versus when the state space is too large or too poorly estimated for multi-step forecasts to mean anything.

## The transition matrix For a chain on `k` states, the one-step transition matrix `P` is the `k x k` array with ``` P[i][j] = P(X_(n+1) = j | X_n = i) ``` Every entry is non-negative and **every row sums to 1**, because from state `i` the chain must go somewhere. This is the row-stochastic convention, which pairs with distributions written as **row vectors** multiplied on the left: `v_(n+1) = v_n P`. ## Why powers give multi-step probabilities To get from `i` to `j` in two steps the chain must pass through some intermediate state `m`. Splitting on that intermediate state and adding up the disjoint routes: ``` P(X_2 = j | X_0 = i) = sum over m of P[i][m] * P[m][j] ``` That sum is exactly the definition of the (i, j) entry of the matrix product `P * P`. Iterating gives the general **Chapman-Kolmogorov** relation ``` P^(m+n) = P^m * P^n ``` so the n-step transition matrix is simply `P^n`. Nothing else is needed: no simulation, no recursion, just a matrix power. ## Worked weather example Order the states `[sunny, rainy]` and take ``` P = [ 0.8 0.2 ] [ 0.4 0.6 ] ``` Sunny days persist 80% of the time; rainy days persist 60% of the time. Squaring, entry by entry: - sunny -> sunny in two: `0.8 * 0.8 + 0.2 * 0.4 = 0.64 + 0.08 = 0.72` - sunny -> rainy in two: `0.8 * 0.2 + 0.2 * 0.6 = 0.16 + 0.12 = 0.28` - rainy -> sunny in two: `0.4 * 0.8 + 0.6 * 0.4 = 0.32 + 0.24 = 0.56` - rainy -> rainy in two: `0.4 * 0.2 + 0.6 * 0.6 = 0.08 + 0.36 = 0.44` ``` P^2 = [ 0.72 0.28 ] [ 0.56 0.44 ] ``` Rows still sum to 1 — a useful arithmetic check. Note that the two rows are already closer to each other than the rows of `P` were: information about today's weather is decaying with each step, which is the seed of long-run behaviour. ## The classic mistake A very common wrong answer squares each entry: `0.2^2 = 0.04` for two-day rain. That number answers a different question — the probability of the specific path sunny -> rainy -> rainy is `0.2 * 0.6 = 0.12`, and `0.04` is not even that. Elementwise squares also fail the sanity check, since `0.64 + 0.04 = 0.68` is not 1. Multi-step probabilities always sum over intermediate states. ## Propagating a distribution instead If you start from uncertainty rather than a known state — say a 50/50 belief about today — put it in a row vector `v_0 = [0.5, 0.5]` and push it forward: ``` v_1 = v_0 P = [0.5*0.8 + 0.5*0.4, 0.5*0.2 + 0.5*0.6] = [0.6, 0.4] v_2 = v_1 P = [0.6*0.8 + 0.4*0.4, 0.6*0.2 + 0.4*0.6] = [0.64, 0.36] ``` For a single starting distribution this is cheaper than forming `P^n`: each step costs `k^2` operations rather than the `k^3` of a matrix-matrix product. If you genuinely need `P^n` for a large `n`, repeated squaring gets there in about `log2(n)` matrix multiplications. ## Convention traps Some texts write transition matrices **column-stochastic**, with columns summing to 1, and update column vectors as `v_(n+1) = P v_n`. The mathematics is identical — that matrix is the transpose of the row-stochastic one — but the entry you read flips from (i, j) to (j, i). Interviewers do not care which convention you pick; they care that you state it and stay consistent. Checking that your rows (or columns) sum to 1 catches a transposed matrix immediately. ## Numerical notes Repeated multiplication accumulates floating-point error, and rows can drift slightly off 1; renormalising each row after many multiplications keeps things clean. Also watch the horizon: for large `n` the rows of `P^n` typically stop changing, which is a signal that you should be asking about the long-run distribution instead of computing ever-larger powers.

  • Why can you not get two-step probabilities by squaring each entry of the matrix?
    Because a two-step probability sums over every intermediate state: `P(i -> j in 2) = sum over m of P[i][m] * P[m][j]`. Squaring entries keeps only the single route `i -> j -> j` scaled wrongly, and the resulting rows no longer sum to 1, which is an instant tell that the operation was not a matrix product.
  • How do you compute the state distribution after n steps from an uncertain starting belief?
    Write the belief as a row vector `v_0` and compute `v_n = v_0 P^n`. In practice, multiply the vector through the matrix `n` times rather than forming the matrix power: each step costs `k^2` operations instead of `k^3`, and you get every intermediate distribution for free.
  • What changes if the transition matrix is written column-stochastic?
    Columns sum to 1 instead of rows, distributions are column vectors, and the update becomes `v_(n+1) = P v_n`. The n-step matrix is still `P^n`, but the entry you read is (j, i) rather than (i, j). Say which convention you are using before you compute anything.

saying these in an interview costs you the question

  • Squares each entry of P instead of multiplying matrices
  • Follows one path only and ignores the other route
  • Silently mixes row-stochastic and column-stochastic conventions
  • Cannot check that rows of the power still sum to 1
  • Simulates the chain when a two-line matrix product answers it exactly

context