# Brownian motions projected on a 3D graph

I found this image on a presentation. I am working on MWE but I was wondering if you had ever come through that type of representation with a projection on the 3d graph ? It could look quite like TeXexample but impossible to adapt to real data so far. MWE to follow. The green graph is projected on the 3D graph (transformation) and projected on the axis below.

Following @marmot answer, I adapted the code with the correct 3D functions (Call).

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}[scale=1.8,   declare function={
Nprime(\x) = 1/(sqrt(2*pi))*exp(-0.5*(pow(\x,2)));
normcdf(\x,\m,\SIG) = 1/(1 + exp(-0.07056*((\x-\m)/\SIG)^3 - 1.5976*(\x-\m)/\SIG));
d2(\x,\y,\KK,\RR,\SIG) = (ln(\x/\KK)+(\RR-(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
d1(\x,\y,\KK,\RR,\SIG) = d2(\x,\y,\KK,\RR,\SIG) + (\SIG*(sqrt(\y)));
Call(\x,\y,\KK,\RR,\SIG) = \x*normcdf(d1(\x,\y,\KK,\RR,\SIG),0,1)-\KK*exp(-\RR*\y)*normcdf(d2(\x,\y,\KK,\RR,\SIG),0,1);
Brownian(\x)= ; %% I'd like to generate a  function brownian motion, starting at 100 with a \sig standard deviation over time
}
]
\begin{axis}[view={20}{20},axis on top,xlabel=$S$,ylabel=Time,zlabel=Option
price,mesh/interior colormap name=hot,colormap/hot,3d box=complete,grid,grid
style={thin,gray!40},axis line style={gray!40}]

% I fix the following parameters of the Call function
\def\KK{100}
\def\TT{0.5}
\def\RR{0}
\def\SIG{0.15}

domain=0.1:1,domain=50:150] {Call(\x,\y,\KK,\RR,\SIG)};
\end{axis}
\end{tikzpicture}
\end{document}


• The projection of the red graph (yielding the cyan and blue graphs) is almost trivial: just set the y or z coordinate to zero. What's not trivial is to guess the red graph from your screen shot. So please add an MWE. An example, though in a slightly different context, can be found here. Yet this is unlikely the only example of this kind.
– user121799
Oct 24 '18 at 0:43

If you have a function, you can do the projections by, well, projecting the result.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[scale=1.8,declare function={f(\x,\y)=exp(0.1*\y);
g(\x)=sin(\x*100)+0.2*cos(567*\x);}]
\begin{axis}[view={45}{40},axis on top,
xlabel=$x$,ylabel=$y$,
mesh/interior colormap name=hot,
colormap/hot]
{f(x,y)};
\end{axis}
\end{tikzpicture}
\end{document}


Similarly for the Brownian motion.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}[scale=1.8,declare function={f(\x,\y)=exp(0.1*\y);
g(\x)=sin(\x*100)+0.2*cos(567*\x);}]
\pgfmathsetseed{42}
\foreach \X in {0,...,50}
{
\ifnum\X=0
\pgfmathsetmacro{\Y}{rand}
\pgfmathsetmacro{\myf}{f(\X/10,\Y)}
\xdef\LstBottom{(\X/10,{\Y},{0.31})}
\xdef\LstOnSurf{(\X/10,{\Y},\myf)}
\xdef\LstFront{(\X/10,{-2.5},\myf)}
\else
\pgfmathsetmacro{\Y}{\LastY+0.3*rand}
\pgfmathsetmacro{\myf}{f(\X/10,\Y)}
\xdef\LstBottom{\LstBottom (\X/10,{\Y},{0.31})}
\xdef\LstOnSurf{\LstOnSurf (\X/10,{\Y},\myf)}
\xdef\LstFront{\LstFront (\X/10,{-2.5},\myf)}
\fi
\xdef\LastY{\Y}}
\begin{axis}[view={45}{40},axis on top,zmin=0.3,
xlabel=$x$,ylabel=$y$,
mesh/interior colormap name=hot,
colormap/hot]
{f(x,y)};
\end{axis}
\end{tikzpicture}
\end{document}


• I finally found a way to input the correct functions in MWE. I am no far from a solution. Instead of the g function of cosinus, i'd rather generate a brownian motion. It would be projected on the Call function, and projected again in the frame (time, option value). I feel the magic will happen !
– JeT
Mar 3 '19 at 23:54
• I accepted the answer. Unfortunately I am stuck on the projections :s
– JeT
Mar 4 '19 at 8:53
• @Julien-ElieTaieb I think I misread your above comment, sorry! I read it as you could achieve what you wanted. Rereading it: you want a Brownian motion, right? In any case, I added one.
– user121799
Mar 5 '19 at 6:16
• Merci @Marmot :) I'm going to apply your code to my functions and post an update once I have the solution.
– JeT
Mar 5 '19 at 12:59
– user121799
Mar 6 '19 at 1:26

Thanks to Marmot answer I could achieve what I wanted. Many parameters to play with to see the deformations.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{ifthen}

\tikzset{
declare function={
normcdf(\x,\m,\SIG) = 1/(1 + exp(-0.07056*((\x-\m)/\SIG)^3 - 1.5976*(\x-\m)/\SIG));
d2(\x,\y,\KK,\RR,\SIG) = (ln(\x/\KK)+(\RR-(pow(\SIG,2)/2)*\y))/(\SIG*(sqrt(\y)));
d1(\x,\y,\KK,\RR,\SIG) = d2(\x,\y,\KK,\RR,\SIG) + (\SIG*(sqrt(\y)));
Call(\x,\y,\KK,\RR,\SIG) = \x*normcdf(d1(\x,\y,\KK,\RR,\SIG),0,1)
-\KK*exp(-\RR*\y)*normcdf(d2(\x,\y,\KK,\RR,\SIG),0,1);
}
}

\def\Type{Call} \def\KK{100}    \def\RR{0}  \def\SIG{0.1}   \def\LastS{120}
\def\ViewX{260} \def\ViewY{30}
\def\NbPoint{50}

\begin{document}

\begin{tikzpicture}

\pgfmathsetseed{4}

\tikzset{
TermPoint/.style={mark=ball, mark options={ball color=black,mark size=2}},
LastPoint/.style={draw=none,mark=ball,mark size=5pt,mark options={ball color = red},mark repeat={\NbPoint}},
LastPointPayOff/.style={draw=blue!60, mark=ball, mark size=2pt, mark options={ball color = blue}, mark repeat={\NbPoint}},
}

\foreach \T in {0,...,\NbPoint}
{
\ifnum\T=0
\pgfmathsetmacro{\S}{\LastS}
\pgfmathsetmacro{\myf}{\Type(\S,{\T/10+0.005},\KK,\RR,\SIG))}
\xdef\LstBottom{(\T/10,{\S},{0.0})}
\xdef\LstOnSurf{(\T/10,{\S},\myf)}
\xdef\LstFront{(\T/10,{50},\myf)}
\else
\pgfmathsetmacro{\S}{\LastS+2*(rand+rand+rand+rand+rand)}
\pgfmathsetmacro{\myf}{\Type(\S,{\T/10+0.005},\KK,\RR,\SIG))}
\xdef\LstBottom{\LstBottom (\T/10,{\S},{00})}
\xdef\LstOnSurf{\LstOnSurf (\T/10,{\S},\myf)}
\xdef\LstFront{\LstFront (\T/10,{50},\myf)}
\fi
\xdef\LastS{\S}
}

\begin{axis}[
view={\ViewX}{\ViewY},
axis on top,
xlabel=Time to maturity,
ylabel=$S$,
zlabel=\Type,
mesh/interior colormap name=hot,
colormap/hot,
xtick = {0,1,2,3,4}]