[Tex/LaTex] How to draw Brownian motions in tikz/pgf

tikz-pgf

Here I want to draw some Brownian motions in tikz, like this:
enter image description here
Furthermore, I want to truncate the trajectory of Brownian motion, like this:
enter image description here

I have tried many times with random functions in tikz, but always fail.

BTW, the figures uploaded are screenshots from "Brownian Motion РDraft version of May 25, 2008" written by Peter M̦rters and Yuval Peres.

Best Answer

How about this? It's pseudo random, but you can make it repeatable by setting \pgfmathsetseed{integer}:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\begin{document}

\newcommand{\Emmett}[5]{% points, advance, rand factor, options, end label
\draw[#4] (0,0)
\foreach \x in {1,...,#1}
{   -- ++(#2,rand*#3)
}
node[right] {#5};
}

\begin{tikzpicture}
\draw[help lines] (0,-5) grid (15,5);
\Emmett{750}{0.02}{0.2}{red}{first one}
\Emmett{750}{0.02}{0.2}{green}{second one}
\Emmett{750}{0.02}{0.2}{blue}{third one}
\end{tikzpicture}

%\pgfmathsetseed{1337}

\end{document}

enter image description here


Edit 1: Truncated is also doable:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\begin{document}

\newcommand{\Emmett}[5]{% points, advance, rand factor, options, end label
\draw[#4] (0,0)
\foreach \x in {1,...,#1}
{   -- ++(#2,rand*#3)
}
node[right] {#5};
}

\newcommand{\Lathrop}[6]{% points, advance, rand factor, options, end label, truncate from point
\draw[#4] (0,0)
\foreach \x in {1,...,#6}
{   -- ++(#2,rand*#3)
}
coordinate (tempcoord) {};
\pgfmathsetmacro{\remaininglength}{(#1-#6)*#2}
\draw[#4] (tempcoord) -- ++ (\remaininglength,0) node[right] {#5};
}

\begin{tikzpicture}
\draw[help lines] (0,-5) grid (15,5);
\Lathrop{750}{0.02}{0.23}{red!70!black}{first one}{300}
\Lathrop{750}{0.02}{0.18}{green!70!black,thick}{second one}{400}
\Lathrop{750}{0.02}{0.21}{blue!70!black}{third one}{500}
\end{tikzpicture}

\end{document}

enter image description here


Edit 2: Ah, now I get the truncation request: Now you can specify upper and lower bounds and draw straight lines for them:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{xifthen}

\begin{document}

\newcommand{\Emmett}[5]{% points, advance, rand factor, options, end label
\draw[#4] (0,0)
\foreach \x in {1,...,#1}
{   -- ++(#2,rand*#3)
}
node[right] {#5};
}

\newcommand{\Lathrop}[9]{% points, advance, rand factor, options, end label, upper, lower trunc, draw trunc  lines, trunc draw options
\begin{scope}
\pgfmathsetmacro{\picwidth}{#1*#2}
\clip (0,#6*28.453+0.5\pgflinewidth) rectangle (\picwidth,#7*28.453-0.5\pgflinewidth);
\ifthenelse{\equal{#8}{y}}
    {\draw[#9] (0,#6) -- (\picwidth,#6) (0,#7) -- (\picwidth,#7);}
    {}
\draw[#4] (0,0)
\foreach \x in {1,...,#1}
{   -- ++(#2,rand*#3)
}
coordinate (#5) ;
\end{scope}
\node[right,#4] at (#5) {#5};
}

\begin{tikzpicture}
\draw[help lines] (0,-5) grid (15,5);
\Lathrop{750}{0.02}{0.2}{red!70!black}{first one}{1.5}{-2.3}{n}{}
\Lathrop{750}{0.02}{0.2}{green!70!black,thick}{second one}{1.1}{-1.7}{y}{green!70!black,densely dashed}
\Lathrop{750}{0.02}{0.3}{blue!70!black}{third one}{2.4}{-2.7}{y}{blue!70!black,thin,densely dotted}
\end{tikzpicture}

\end{document}

enter image description here

P.S. there are still some issues as the placements of the labels. The command now has 9 parameters, one should switch to pgfkeys for a convineant key-value interface.

Related Question