Open the context
Choose the project folder and name the files or behavior involved.
Advance a probability vector through up to 100 transitions of a Markov matrix with at most eight states. Test zero steps and absorbing states, rejecting negative values and row sums differing from one by more than 1e-12.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from math import fsum, isfinite
def markov_steps(initial, matrix, steps):
if type(initial) is not list or not 1 <= len(initial) <= 8:
raise ValueError("state count")
n = len(initial)
if type(matrix) is not list or len(matrix) != n or any(
type(row) is not list or len(row) != n for row in matrix):
raise ValueError("square matrix")
rows = [initial] + matrix
if any(type(x) not in (int, float) or not 0 <= x <= 1 or
not isfinite(x) for row in rows for x in row):
raise ValueError("probability values")
if any(abs(fsum(row)-1) > 1e-12 for row in rows):
raise ValueError("row sums")
if type(steps) is not int or not 0 <= steps <= 100:
raise ValueError("step count")
result = initial[:]
for _ in range(steps):
result = [fsum(result[i] * matrix[i][j] for i in range(n))
for j in range(n)]
return resultThe example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.