stationary
Builtin index · Statistics & ML
result = chain.stationary()
Input: no input · Output: result
What it is
Computes the long-run state distribution of a markov_chain chain by power iteration; converges for any irreducible, aperiodic chain.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = chain.stationary() | no input | result vector | Returns a length-S probability vector. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
P = [0.9, 0.5, 0.1, 0.5] as matrix(2, 2) # column-major: rows [0.9 0.1], [0.5 0.5]
chain = markov_chain(P, 0)
print("stationary: {round(chain.stationary(), 4)}")
print("3-step: {round(chain.transition ^ 3, 4)}")
trans = [0.7, 0.4, 0.3, 0.6] as matrix(2, 2)
emit = [0.5, 0.1, 0.4, 0.3, 0.1, 0.6] as matrix(2, 3)
h = hmm(trans, emit, [0.6, 0.4])
print("P(normal, cold, dizzy) = {round(h.forward([0, 1, 2]), 5)}")
print("most likely path = {h.viterbi([0, 1, 2])}")
stationary: [0.8333, 0.1667] 3-step: [0.844, 0.156; 0.78, 0.22] P(normal, cold, dizzy) = 0.03628 most likely path = [0, 0, 1]
from statistics-ml
How it works
stationary(chain) — callable as chain.stationary(). The Markov chain's long-run state distribution, found by power iteration (v <- normalize(v * P) from a uniform start) rather than an eigenvector extraction: eig only computes eigenvectors for a SYMMETRIC input (see numeric::linalg::eig's own doc comment), and a general transition matrix has no reason to be symmetric. See markov_stationary's own doc comment.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.