I have the following states and transition matrix
import numpy as np
n_states = 3
states = np.arange(n_states)
T = np.array([
[0.5, 0.5, 0],
[0.5, 0, 0.5],
[0, 0, 1]
])
I would like to simulate n_sims
paths where each path consist of n_steps
. Each path starts at 0. Therefore, I write
n_sims = 100
n_steps = 10
paths = np.zeros((n_sims, n_steps), dtype=int)
With the help of np.random.Generator.choice
I would like to “fill to the right” the paths using the transition matrix.
My attempt look as follows
rng = np.random.default_rng(seed=123)
for s in range(1, n_steps+1):
paths[:,s] = rng.choice(
a=n_states,
size=n_sim,
p=T[paths[:,s-1]]
)
This result in the following error:
ValueError: p must be 1-dimensional
How can I overcome this? If possible, I would like to prevent for-loops and vectorize the code.
1