I'd like to understand how to implement in python a Markov Chain on a 2D grid (a 2D numpy array) given the values for all the states (positions) of the grid and also the transition matrix between all states.
Here I will use some figures to better explain my goal.
I have a 2x2 grid, each cell of the grid is identified by a state (the number of the grid cell) and by a value (some property of the cell).
I also created a possible transition matrix from each cell to each other cell of the grid. The value inside each cell of the transition matrix is the probability of transition from the cell selected by the column, to the cell selected by the row.
In this case for example the cell with state d
can only transit to cell b
, or c
, with probability 0.6 and 0.4 respectively (that is Q(b,d) = 0.6 and Q(c,d) = 0.4).
At each transition I want to apply the function mean
between the value stored in the cell at time t
and the new values transiting in the cell at time t+1
.
For example after one cycle in this case (the transitions that actually took place in this case are presented under the arrow).
In each cell the old value has been averaged with the values of the cells that transitioned to that cell.
I'd like to create an efficient code that can implement this algorithm. I already have a code that does this but without markov chain and I think I may obtain significant better performance by using MC.