Suppose I have a system which can have two states, 0 and 1, with a transition probability matrix T such as, for example,
[[ 0.8 0.2]
[ 0.1 0.9]]
Define S(N) as the cumulative sum of the state indices visited after N transitions, given that the initial state is 0. I would like to calculate the expected value of S(N).
For example, $S(2) = 0.8^{2}\times0 + 0.8\times0.2\times1 + 0.2\times0.1\times1+0.2\times0.9\times2 = 0.54$.
What would be a general formula/algorithm to calculate this? I could simply keep track of all the 'branches' of the tree, but their number increases exponentially with N and this doesn't seem like an efficient method.
I realized that this can be solved using the recursive relation
$$S(i,n) = \sum_{j} T_{ij}\left[i + S\left(j, n-1\right)\right]$$
where $S(i, n)$ is the expected value of the cumulative sum $S$ given an initial state $i$ after $n$ transitions. This can be implemented using bottom-up dynamic programming. For example, in Python,
which prints
from which we can read off that $S(0, 2) = 0.54$. This can be checked with a Monte Carlo simulation:
which (for one instance) prints
0.5426, in good agreement with the analytical result.