SciPy

prob140.MarkovChain.simulate_chain

MarkovChain.simulate_chain(starting_condition, n, end=None)[source]

Simulates a path of length n following the markov chain with the initial condition of starting_condition

Parameters:

starting_condition : state or Distribution

If a state, simulates n steps starting at that state. If a Distribution, samples from that distribution to find the starting state

n : integer

Number of steps to take

end : state (optional)

Chain stops as soon as it reaches end

Returns:

Array

Array of the path taken

Examples

>>> mc = Table().states(make_array("A", "B")).transition_probability(make_array(0.5, 0.5, 0.3, 0.7)).toMarkovChain()
>>> mc.simulate_chain("A", 10)
array(['A', 'A', 'A', 'A', 'B', 'B', 'A', 'A', 'B', 'B', 'B'])
>>> mc.simulate_chain("B", 10)
array(['B', 'B', 'B', 'A', 'B', 'A', 'B', 'A', 'A', 'A', 'B'])
>>> start = Table().states(make_array("A", "B")).probability(make_array(.8, .2))
>>> mc.simulate_chain(start, 10)
array(['A', 'A', 'A', 'B', 'B', 'A', 'B', 'B', 'A', 'B', 'A'])
>>> mc.simulate_chain(start, 10, end='A')
array(['B', 'B', 'B', 'A'])