1

I have found the following clever code Brownion Motion here to plot a brownian motion:

\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(-2ln(#1))cos(deg(2pi#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}

\end{document}

But i'm not good enough in LaTeX/pgfplot/Tikz to understand how and where i have to change it to get a Brownian bridge instead that brownian motion.

Thanks for your help

Mico
  • 506,678
  • Does the following help? If B(t) is Brownian motion for t\in [0,1], then BB(t)=B(t)-t*B(1) forms a Brownian Bridge on that interval. Usually, one is interested in standardized Brownian motion, i.e., a Wiener process, for which W(1)\sim N(0,1). To generate sample paths of a Wiener process, start by normalizing the x-axis values to range from 0 to 1 instead of from 0 to n and by standardizing the variance of the increments so that Var(B(1))=1. (You may set B(0)=0.) – Mico Jul 22 '23 at 21:16
  • Thx i already know the mathematical definition of the BB. But my skills makes me unable to understand and change the previous Tikz code – Vincent ISOZ Jul 22 '23 at 21:42

1 Answers1

2

While you are waiting for Tikz help, here is an effort in Metapost, that might be simpler to follow / adapt. This is wrapped up in luamplib so you have to compile it with lualatex.

I've followed some of the discussion in this stats answer.

enter image description here

The blue line is a regular brownian random walk, where the y-value is incremented by a normal deviate each time. The red line is the brownian bridge, which is derived from the regular walk by subtracting an appropriately weighted proportion of the final y-value.

To make the example concrete, I used 200 samples and a fixed random seed, so that the final value of the walk w[n] was 17.8543 -- each point of the red line bb[i] is then w[i] - (i/n) * (b - w[n]). This means that bb[n] is w[n] - (n/n) * (b - w[n]) = b as desired.

Here is the code. Compile this with lualatex to get a PDF that looks like the image above.

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\mplibshowlog{enable}
\begin{mplibcode}
% randomseed := uniformdeviate infinity;
randomseed := 778.58453;
beginfig(1);
% parameters: n number of samples, a = start, b = target
numeric n, a, b; n = 200; a = b = 0;

% x and y scales numeric u, v; u = 2; v = 8;

% base line draw ((0, a) -- (n, b)) xscaled u yscaled v;

% w[] is the regular brownian random walk % bb[] is the brownian bridge numeric w[], bb[]; w0 = a; for i=1 upto n: w[i] = w[i-1] + normaldeviate; endfor for i=0 upto n: bb[i] = w[i] + (i/n) * (b - w[n]); endfor

draw ((0, a) for i=1 upto n: -- (i, w[i]) endfor) xscaled u yscaled v withcolor blue; draw ((0, a) for i=1 upto n: -- (i, bb[i]) endfor) xscaled u yscaled v withcolor red;

label.rt("$(" & decimal n & "," & decimal w[n] & ")$", (nu, w[n]v));

endfig; \end{mplibcode} \end{document}

Thruston
  • 42,268