I have a grid of 30x30 which is discretized into 1x1, 900 cells. And the probability of moving from a particular cell to one step up, down, left, and right are 0.4, 0.1, 0.2, 0.3 respectively. I want to initialize a transition probability matrix of 900x900, where 900 represents the hidden states/cells. In a row, most of the values would be zero and a maximum of only 4 columns would be initialized. Any help on how to write code to initialize such a matrix would be helpful. Thank you.
Asked
Active
Viewed 339 times
1 Answers
1
Let us start at some initial state which is stored in our transition matrix in position (i,j). We can define the probability of moving left as P((i,j+1), (i,j)), right as P((i,j+2), (i,j)), up as P((i,j-2), (i,j)), and down as P((i,j-1), (i,j)). Then all we need to do is
def kth_diag_indices(A: ndarray, offset: int)-> ndarray:
return np.where(np.eye(A.shape[0], k=offset) == 1)
n_states = 30
P = np.zeros((n_states,n_states))
P[kth_diag_indices(P,-2)] = 0.4 # up
P[kth_diag_indices(P,-1)] = 0.1 # down
P[kth_diag_indices(P, 1)] = 0.2 # left
P[kth_diag_indices(P, 2)] = 0.3 # left
Note that the main diagonal stays zero'd, since the sum of the probabilities you provided is 1, meaning P((i,j), (i,j)) = 0 if the transition matrix is to remain stochastic. Furthermore, the 0th row cannot move up or down in this definition, and the nth cannot move left or right, which is not ideal. If you want different behavior, you need to define your transition matrix differently, but you can use the same principles described here.

v0rtex20k
- 1,041
- 10
- 20
-
I'm not able to understand your definition of transition matrix here. Can you please elaborate? – Anurag Singla Mar 12 '21 at 22:31
-
And won't be the number of states 900? – Anurag Singla Mar 12 '21 at 22:38
-
No, there will be 30 states, each with 30 transition options - you yourself said " have a grid of 30x30 which is discretized into 1x1, 900 cells", so clearly you understand why there need to be 900 entries in *P*. Like I said, this is not how I would personally design the transition matrix, but you asked for "a maximum of only four columns would be initialized", with a specific shape, so that's what I made. The helper function ```kth_diag_indices`` can be used for any transition matrix, so you can apply it as needed if you decide to redesign your matrix. – v0rtex20k Mar 13 '21 at 14:58