I would like to plot a simulation of a Markov process in R^2. Basically, I want to draw a line that starts at (x_0,y_0)=(0,2) and then follow this rule: For each i=1,...,10, the line will go through (i,y_{i-1}+1) with probability 1/2 and (i,y_{i-1}-1) with probability 1/2. So I designed the following TikZ code to draw this figure:
\begin{tikzpicture}[scale=0.6]
\coordinate[label=left:$2$] (0) at (0,2){};
\pgfmathsetmacro{\z}{2};
\newcounter{c};
\setcounter{c}{0};
\pgfmathdeclarerandomlist{dir}{{-1}{1}};
\foreach \i in {1,2,...,10}{
\pgfmathrandomitem{\d}{dir};
\pgfmathsetmacro{\y}{\z+\d};
\pgfmathsetmacro{\x}{\i};
\coordinate (\i) at (\x,\y){};
\pgfmathtruncatemacro{\p}{\i-1};
\draw[red] (\p)--(\i);
\pgfmathsetmacro{\z}{\y};
}
\draw[dashed,thick] (\l)--(20,2);
\end{tikzpicture}
First, we draw a coordinate at (0,2). Then, we choose \d uniformly at random from {-1,1} and draw a coordinate at (1,2+\d) and we set \z to be 2+\d. Then we choose \d again at random from {-1,1} and draw a coordinate at (2,\z+\d), and so on...
However, this does not give what the picture should look like. In fact, the updating process with \z and \y does not seem to occur, and I don't know why.
`). Also, you do not need the<br>and<p>. – Caramdir Jan 21 '11 at 1:20