# Creating a Brownian motion plot with many trajectories including the expected value

I'm trying to replicate the Brownian motion plot found on the cover of Bernt Øksendal's textbook Stochastic Differential Equations using LaTeX.

I'm a bit lost in the coding. No math is needed. I've looked up a similar topic (Brownian motion - duplicate), but none of these had an E[X_t] line on the plot. Also, I was unsure how to correct the Brownian motion trajectories (as some of them go into negative, unlike the textbook cover plot) and labeling of the x and y-axis.

## My Attempt

    \documentclass[12pt, a4paper]{article} %uploading the libraries
\usepackage{pgfplots, pgfplotstable}
\usepackage[font=small,labelfont=bf,labelsep=colon]{caption}
\begin{document}
\pgfmathsetseed{5}
% creating the Brownian motion trajectories
\pgfplotstablenew[
create on use/randwalk1/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/randwalk2/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/randwalk3/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/randwalk4/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/randwalk5/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
columns={randwalk1,randwalk2,randwalk3,randwalk4,randwalk5}
]
{700}
% adjusting the plot axes and titles
\begin{figure}[H]
\caption{Brownian Motion}
\centering
\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
axis y line=left,
axis x line=middle,
xlabel= {\scriptsize $t$},
ylabel = {\scriptsize $X_{t}$},
ylabel style = {yshift=-12pt},
xticklabels={,,},
yticklabels={,,},
tick style={draw=none},
line join=bevel,
no markers,
table/x expr={\coordindex/100},
xmin=0,
ymin=-0.5, ymax=3,
enlarge x limits=false
% now, putting it all together
]
\draw (axis cs:5,5) (axis cs:5,-5);
\legend {{\footnotesize $X_t(\omega_1)$}, {\footnotesize $X_t(\omega_2)$}, {\footnotesize  $X_t(\omega_3)$},{\footnotesize  $X_t(\omega_4)$},{\footnotesize  $X_t(\omega_5)$}};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

• This sort of depends on the exact definition of $X_t$. It can't be cumulative distance since it can decrease, and it can't be simple distance since then $E(X_t)=0$. Commented Jan 16 at 1:46
• It's not a Brownian motion. It's a geometric Brownian motion, or exponent of a Brownian motion. For exp(B_t), where B_t~N(0,t) its expected value can be computed directly: E(B_t)=exp(t/2). Commented Jan 16 at 10:53

The thing is that rand in pgf does generate a uniformly distributed random value on [-1,1], and not a normal random value. So the following code does not plot a GBM, strictly speaking. But it looks similar to a GBM plot. You can see that I've also added some drift 0.003 in the exponent to make trajectories more likely increasing similar to the original figure. And since rand is uniform, I've computed E(exp(0.003+0.1*rand))=exp(0.003)*5*(exp(0.1)-exp(-0.1)) and use this expression to plot the expectation.

\documentclass[12pt, a4paper]{article} %uploading the libraries
\usepackage{pgfplots, pgfplotstable}
\usepackage[font=small,labelfont=bf,labelsep=colon]{caption}
\begin{document}
\pgfmathsetseed{5}
% creating the Brownian motion trajectories
\pgfplotstablenew[
create on use/randwalk1/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/randwalk2/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/randwalk3/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/randwalk4/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/randwalk5/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/expectation/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003)*5*(exp(0.1)-exp(-0.1))}{1}
},
columns={randwalk1,randwalk2,randwalk3,randwalk4,randwalk5,expectation}
]
{700}
% adjusting the plot axes and titles
\begin{figure}[h]
\caption{Brownian Motion}
\centering
\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
axis y line=left,
axis x line=middle,
xlabel={\scriptsize $t$},
ylabel={\scriptsize $X_{t}$},
ylabel style={yshift=-12pt},
xticklabels={,,},
yticklabels={,,},
tick style={draw=none},
line join=bevel,
no markers,
table/x expr={\coordindex/100},
xmin=0,
ymin=0,
enlarge x limits=false
]
\legend {{\footnotesize $X_t(\omega_1)$},
{\footnotesize $X_t(\omega_2)$},
{\footnotesize $X_t(\omega_3)$},
{\footnotesize $X_t(\omega_4)$},
{\footnotesize $X_t(\omega_5)$},
{\footnotesize $E[X_t]$}};