How can I generate binary time series such that:

- Average probability of observing 1 is specified (say 5%);
- Conditional probability of observing 1 at time $t$ given the value at $t-1$ ( say 30% if $t-1$ value was 1)?

**Answer**

Use a two-state Markov chain.

If the states are called 0 and 1, then the chain can be represented by a 2×2 matrix $P$ giving the transition probabilities between states, where $P_{ij}$ is the probability of moving from state $i$ to state $j$. In this matrix, each row should sum to 1.0.

From statement 2, we have $P_{11} = 0.3$, and simple conservation then says $P_{10} = 0.7$.

From statement 1, you want the long-term probability (also called equilibrium or steady-state) to be $P_1 = 0.05$. This says $$P_1 = 0.05 = 0.3 P_1 + P_{01}(1-P_1)$$ Solving gives $$P_{01} = 0.0368421$$ and a transition matrix $$P = \left(

\begin{array}{cc}

0.963158 & 0.0368421 \\

0.7 & 0.3

\end{array}

\right)$$

(You can check your transtion matrix for correctness by raising it to a high power–in this case 14 does the job–each row of the result gives the identical steady state probabilities)

Now in your random number program, start by randomly choosing state 0 or 1; this selects which row of $P$ you’re using. Then use a uniform random number to determine the next state. Spit out that number, rinse, repeat as necessary.

**Attribution***Source : Link , Question Author : user333 , Answer Author : Mike Anderson*