I would like to simulate and plot many paths of a discrete random variable $\tau_{t}$ that follows the following process:

  1. with probability $\tau_{t-1}$, $\tau_{t}$ is either $0.6$ with probability $x$ or $0.4$ with probability $1-x$. Then, $\tau_{t+1}$ is drawn according to the process.
  2. with probability $1-\tau_{t-1}$, $\tau_{t}=\tau_{t-1}$. Then, $\tau_{t+1}$ is also equal to $\tau_{t}$ with probability $y$, or it is drawn according to point 1 of the described process with probability $1-y$.

1 Answer 1


You can make use of RandomChoice and NestList. Since $\tau_{t+1}$ may depend not only on $\tau_t$ but also on $\tau_{t-1}$, I keep an additional flag named branch which indicates whether $\tau_{t}$ was set to $\tau_{t-1}$ so that $\tau_{t+1}$ should be set according to different rules.

simulateτ[x_, y_, τ0_, steps_] := Module[{nextτ},
  nextτ[{τ_, branch_}] := If[branch == 0,
     {τ, 1 - τ} -> {
       RandomChoice[{x, 1 - x} -> {{0.6, 0}, {0.4, 0}}],
       {τ, 1}
     {y, 1 - y} -> {
       {τ, 0},
       nextτ[{τ, 0}]
  First /@ NestList[nextτ, {τ0, 0}, steps]

Plotting is challenging because it is a discrete-state process. Here is an example for 100 trajectories with 200 steps each for $x=0.2, y=0.8, \tau_0 = 0.1$

ListPlot[Table[simulateτ[0.2, 0.8, 0.1, 200], 100], 
 Joined -> True, PlotStyle -> Opacity[.05, Gray]]

enter image description here

  • $\begingroup$ This works perfectly, thanks really a lot! Just a small follow-up: suppose that, when I leave branch 1, I want the first tau determined according to the x 1-x draw, and then the following according to the full process (including the tau / 1-tau draw). Should I add another branch? $\endgroup$
    – Federico
    Commented May 3, 2023 at 12:42
  • $\begingroup$ @Federico, I don't really follow what you are trying to ask. Can you please write your question more precisely (or refer to the steps in your original post)? $\endgroup$
    – Domen
    Commented May 3, 2023 at 12:48
  • $\begingroup$ sure, sorry for the lack of clarity. I have in mind something like this. During the "ordinary process", my random variable is 0.4 with probability x and 0.6 with probability 1-x. However, at the beginning of every period of the ordinary process, the variable may jump to a "slow process", where it remains constant, with probability $\tau_{t-1}$. When in the slow process, the variable can go back to the "ordinary process" with probability y. Does it make sense? $\endgroup$
    – Federico
    Commented May 3, 2023 at 12:54
  • 1
    $\begingroup$ What you describe – and also your initial question – could very well be described also by a Markov chain (see DiscreteMarkovProcess) with 6 states as I schematically draw (I've just noticed there are some missing connections going from the slow back to the ordinary process, but you get the point). You can also adapt my code in a way that the branch flag denotes whether you are in the "ordinary" or "slow" process. $\endgroup$
    – Domen
    Commented May 3, 2023 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.