I'm wondering whether there is an algorithm to simulate a discrete Markov chain with a specific number of occurrences of state knowing the transition matrix way.
For example, how to simulate in R a Markov chain of length $n$ with $p$ occurrences ($p < n$)
TransitionMatrix<- matrix(c(0.7, 0.3, 0.4, 0.6),byrow=TRUE, nrow=2)
colnames(TransitionMatrix) <- c('0','1') row.names(TransitionMatrix) <- c('0','1')
Suppose you have transition probabilities $p_1 \leq p_2 \dots \leq p_k$, $\sum p_k = 1$. Define the distribution function in pseudo code:
In the above, I have assumed that the transition probabilities are all the same, i.e. independent of the state. Otherwise, the $p_i$ will have to depend on
state.The above code is just saying that if I have numbers $p_1$, ..., $p_n$ such that $\sum p_i = 1$, then I can partition the unit interval $[0,1]$ with these numbers. The area of the rectangle [$p_1 + ... + p_{k-1}, p_1 + ... + p_k$] with unit height is $p_k$, and that's the probability we want for the $k^{th}$ transition. This is the standard way of generating probability distributions, since if $X$ has cdf $F$, then $F(X) \sim U(0,1)$.
Now pick an initial state (say $s = 1$) and a state you want to measure, say $r = 4$.
I should mention this algorithm is the worst.