Graphics, Figures & TablesBranching Brownian Motion

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
dr_kelly
Posts: 1
Joined: Mon Feb 29, 2016 10:29 pm

Branching Brownian Motion

Post by dr_kelly »

I would like to plot a branching brownian motion but i couldn't find any function for brownian paths. Is it possible to plot it using TikZ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Branching Brownian Motion

Post by Stefan Kottwitz »

Welcome to the forum!

Yes, it would be easily possible with TikZ, or PSTricks, or calculations done with Lua. I would use pgfplots, since it gives us a coordinate system with axes and many options for customizing.

Jake gave an example here:

Code: Select all

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}


% Create a function for generating inverse normally distributed numbers using the Box–Muller transform
\pgfmathdeclarefunction{invgauss}{2}{%
  \pgfmathparse{sqrt(-2*ln(#1))*cos(deg(2*pi*#2))}%
}
% Code for brownian motion
\makeatletter
\pgfplotsset{
    table/.cd,
    brownian motion/.style={
        create on use/brown/.style={
            create col/expr accum={
                (\coordindex>0)*(
                    max(
                        min(
                            invgauss(rnd,rnd)*0.1+\pgfmathaccuma,
                            \pgfplots@brownian@max
                        ),
                        \pgfplots@brownian@min
                    )
                ) + (\coordindex<1)*\pgfplots@brownian@start
            }{\pgfplots@brownian@start}
        },
        y=brown, x expr={\coordindex},
        brownian motion/.cd,
        #1,
        /.cd
    },
    brownian motion/.cd,
            min/.store in=\pgfplots@brownian@min,
        min=-inf,
            max/.store in=\pgfplots@brownian@max,
            max=inf,
            start/.store in=\pgfplots@brownian@start,
        start=0
}
\makeatother
%

% Initialise an empty table with a certain number of rows
\pgfplotstablenew{201}\loadedtable % How many steps?



\begin{document}
\pgfplotsset{
        no markers,
        xmin=0,
        enlarge x limits=false,
        scaled y ticks=false,
        ymin=-1, ymax=1
}
\tikzset{line join=bevel}
\pgfmathsetseed{3}
\begin{tikzpicture}
\begin{axis}
   \addplot table [brownian motion] {\loadedtable};
   \addplot table [brownian motion] {\loadedtable};
\end{axis}
\end{tikzpicture}

\pgfmathsetseed{3}
\begin{tikzpicture}
\begin{axis}
    \addplot table [
        brownian motion={%
            max=0.5,
            min=-0.75
        }
    ] {\loadedtable};
    \addplot table [
        brownian motion={%
            start=0.5,
            min=-0.5, max=0.75
        }
    ] {\loadedtable};
\end{axis}
\end{tikzpicture}
\end{document} 
brownian-motion.png
brownian-motion.png (23.08 KiB) Viewed 2071 times
Jake explained it:
This approach uses pgfplotstable to calculate the Brownian motions as cumulative sums of random normally distributed values (thanks to horchler for pointing out the need for normality). You have to first initialise an empty table, using something like \pgfplotstablenew{200}\loadedtable, and then you can draw the brownian motions using \addplot table [brownian motion] {\loadedtable};.

You can set the initial value and the maximum and minimum values using

Code: Select all

\addplot table [brownian motion={start=0.5, min=-1, max=1}] {\loadedtable};
Stefan
LaTeX.org admin
Post Reply