prerequisite
Matrices and vectors
Vectors as lists, matrices as grids, the two products a Markov chain needs, and why a row vector times a row-stochastic matrix is one step of the chain
Before this
Nothing beyond first-year college math. This is a starting page.
Why you need this
A Markov chain's transition table is a matrix. "Where will the chain probably be after one step" is a row vector times that matrix. "After two steps" is the same vector times the matrix squared. That is all the linear algebra the layout and regime stages need, and this page covers exactly that and nothing more: no determinants, no inverses.
The idea
Vectors
A vector is an ordered list of numbers. Its length is the number of entries. Written horizontally it is a row vector, ; written vertically it is a column vector. The numbers are the same either way; the shape only matters when you multiply. is the -th entry, counting from 1 in math and from 0 in code.
In this cluster, a vector of state probabilities is a row vector: , with entries for states.
Matrices
A matrix is a grid of numbers with rows and columns, called an matrix. The entry sits in row , column : row first, column second, always. A matrix is square when .
Matrix times column vector
is a column vector whose -th entry is row of multiplied entry-wise with and summed:
Row vector times matrix
is a row vector whose -th entry is multiplied entry-wise with column of and summed:
Same matrix, same numbers, different answer from . Order matters, and Markov chains use the row-vector form.
Matrix times matrix
is row of against column of : . The result has as many rows as and as many columns as . means .
Row-stochastic matrix
A square matrix is row-stochastic when every entry is non-negative and every row sums to 1. Each row is then a categorical distribution. The transition matrix of a Markov chain is row-stochastic by definition:
where is the state at time . Row answers "from state , where next?" and its entries must sum to 1 because the chain has to go somewhere.
Why is the next step
Let . The law of total probability says
That last expression is the row-vector-times-matrix formula. So is the distribution over states one step later. It is still a probability vector: its entries sum to 1 because each row of does.
Powers are steps
Two steps is . The entry is the probability of going from to in exactly two steps, summed over every intermediate state . In general holds the -step probabilities.
One sentence on the long run: a stationary distribution is a row vector with , a mix of states that one more step does not change, and for most chains repeated multiplication settles onto it.
Worked example
Three column states: flat (1), gap (2), climb (3).
Rows sum to 1.0, 1.0, 1.0, so it is row-stochastic. says a gap is followed by a climb 30 percent of the time. says a gap never follows a gap.
Start certain in flat: . Then , which is just row 1. If you know you are in flat, the next-step distribution is flat's row.
Two steps: multiply again, column by column.
| value | ||
|---|---|---|
| 1 | ||
| 2 | ||
| 3 |
So , summing to 1.00. Because was , this is also row 1 of .
One entry of by hand: from gap to flat in two steps, Read it as three routes: gap, flat, flat; gap, gap, flat (impossible here); gap, climb, flat.
A mixed start: gives .
In code, then iterated:
function rowTimesMatrix(v, A) {
const out = new Array(A[0].length).fill(0);
for (let i = 0; i < v.length; i++)
for (let j = 0; j < A[0].length; j++) out[j] += v[i] * A[i][j];
return out;
}
const A = [[0.6, 0.3, 0.1], [0.7, 0.0, 0.3], [0.5, 0.2, 0.3]];
let v = [1, 0, 0];
for (let t = 1; t <= 3; t++) {
v = rowTimesMatrix(v, A);
console.log(t, v.map(x => x.toFixed(3)).join(" "));
}
// 1 0.600 0.300 0.100
// 2 0.620 0.200 0.180
// 3 0.602 0.222 0.176
Keep going to 60 steps and the vector stops moving at about . That is the stationary distribution: in the long run about 60 percent of columns are flat, 22 percent gaps, 18 percent climbs, regardless of where the level started.
In a map generator
- Layout. The parameter file stores as an array of rows. Sampling a level uses one row at a time; predicting the mix of a level uses . The initial distribution is a row vector too.
- Regime over time. tells you the expected mix of regimes after ticks without simulating anything. The stationary distribution is a design target: if you want regions calm 60 percent of the time, tune until its stationary vector says so. See Regime chains.
- Fill. A hidden Markov model's emission matrix is also row-stochastic, one row per hidden state. The forward algorithm is a row-vector-times-matrix product with an entry-wise scaling by a column of after each step.
- Export. Matrices ship as nested arrays; the check "every row sums to 1 within rounding" belongs in the loader.
Common mistakes
- Transposed matrix. Columns sum to 1 instead of rows. Symptom: no longer sums to 1; probability leaks or piles up over many steps.
- Using where was meant. Symptom: the same leak, and a "next step" that depends on the wrong row.
- Indexing
A[j][i]in code. Symptom: a chain designed so gap never follows gap produces gap, gap anyway. - Rounded rows. A row summing to 0.999 loses a tenth of a percent per step. Symptom: after 500 ticks the regime vector sums to 0.6. Renormalize, or store the last entry as one minus the rest.
- Reading as a path. gives the distribution of where you end up, not a sequence of states. Symptom: someone draws a level from the entries of and gets a smeared average with no structure.
- Off by one between math and code. Math counts states from 1, code from 0. Symptom: state 1's row is read for state 2.
Cost
With states, costs time and the matrix takes memory, the vector . Advancing a vector steps is . Computing explicitly by repeated multiplication is , which is worth it only if you reuse for many starting vectors. For the of 6 to 30 in this cluster none of this registers; it starts to hurt when is the number of tiles, in the hundreds, and then the matrix is mostly zeros and a sparse representation pays off.
Going further
- Markov chains, where this matrix becomes a generator.
- Hidden Markov models, which add the emission matrix .
- Forward algorithm, a vector-matrix product with scaling.
- Regime chains, where the stationary distribution is the design knob.
- The matrix multiplication and Markov matrix chapters of any first linear algebra text.