markov_chain
Builtin index · Statistics & ML
result = markov_chain(P, [initial_state=0])
Input: P, initial_state · Output: result
What it is
Builds a discrete Markov chain from an (S, S) row-stochastic transition matrix P (every row must sum to 1 — rows that don't are rejected outright, never quietly renormalized) and an integer initial_state (default 0, a state index in 0..S-1).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = markov_chain(P, [initial_state=0]) | P matrixnot documented yetinitial_state matrix, optional (default 0)not documented yet | transition matrixthe (S, S) matrix P itself | Returns a kind="markov_chain" Record with field transition (the (S, S) matrix P itself) and methods .simulate/.stationary. |
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
markov_chain(P, [initial_state=0]) — a discrete-time, discrete-state Markov chain over a row-stochastic transition matrix P (row i is state i's outgoing distribution; check_row_stochastic rejects anything else with a clear error rather than silently normalizing). .simulate(n_steps, [seed=]) and .stationary() are separate generic-verb builtins (see their own doc comments, near predict/score) since neither takes an X the way a supervised/clustering model's predict/score do; n-step transition probabilities need no new builtin at all — chain.transition ^ n already works via the existing matrix-power operator (matrix_pow).
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.