2

I saw a lot of examples on tex.exchange.com about how to construct a 2-period or 3-period binomialtree in latex. I tried to modify these examples to construct a generel n-period binomialtree, but it didn't work. What I'm looking for is something like this:enter image description here

Any help is appreciated. Thanks in advance.


I tried the edit the following code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
  matrix of nodes,
  minimum size=1cm,
  column sep=3.5cm,
  row sep=1cm,
]
{
      &   & F \\
      & C &   \\
  \$A &   & E \\
      & B &   \\
      &   & D \\
};
\draw[->] (tree-3-1) -- (tree-2-2) node [midway,above] {$P$};
\draw[->] (tree-3-1) -- (tree-4-2) node [midway,below] {$(1-p)$};
\draw[->] (tree-2-2) -- (tree-1-3) node [midway,above] {$P^2$};
\draw[->] (tree-2-2) -- (tree-3-3) node [midway,below] {$(1-p)p$};
\draw[->] (tree-4-2) -- (tree-3-3) node [midway,above] {$(1-p)p$};
\draw[->] (tree-4-2) -- (tree-5-3) node [midway,below] {$(1-p)^2$};
\end{tikzpicture}
\end{document}    

I got compile errors when I modify it. So this is what I have. I have troubles editing the "->" to dots.

gariban
  • 21
  • 2
    Since you tried to modify, please show us the code you tried, so we don't need to start from zero. – Sigur Jun 09 '18 at 14:20
  • It looks like you've got two separate accounts, which means you cannot edit your original post or leave comments. The Stack Exchange staff can merge them together for you. – Phelype Oleinik Jun 09 '18 at 15:40
  • Do you want to draw that tree as is? Or do you want to draw the tree for any n? I assume the former, but your question suggested the latter. – cfr Jun 09 '18 at 22:46
  • Related: https://tex.stackexchange.com/questions/237483/how-to-use-tikz-and-macros-to-very-efficiently-draw-recombining-binomial-trees-w/237562#237562, https://tex.stackexchange.com/questions/330327/code-for-binomial-tree-does-not-work-after-one-year etc. – cfr Jun 09 '18 at 22:51

1 Answers1

6

One possibility is to organize an \ifnum festival.

\documentclass[tikz,border=3.14mm]{standalone} 
\usetikzlibrary{decorations.markings}
\newcommand{\mypow}[3][]{
\ifnum#3=0
\else
\ifnum#3=1
#2
\else
#2^{#3}
\fi
\fi
}
\begin{document}
\begin{tikzpicture}[scale=1.5,decoration={markings,
  mark=between positions 0 and 1 step 8pt
  with { \draw [fill] (0,0) circle [radius=1pt];}}] % from https://tex.stackexchange.com/a/52849/121799
\foreach \X in {0,...,5}
{\foreach \Y in {0,...,\X}
{
\pgfmathtruncatemacro{\Z}{\X-\Y}
\ifnum\X<3
\fill (\X,{\Y-\X/2}) coordinate (X-\X-\Y) circle (1pt);
\node[anchor=south] at (\X,{\Y-\X/2+0.1}){$\mypow{u}{\Y}\mypow{d}{\Z}S_0$}; 
\fi
\ifnum\X=4
\ifnum \Y=1
\coordinate (X-\X-\Y) at (\X,{\Y-\X/2});
\else
\ifnum \Y=2
\coordinate (X-\X-\Y) at (\X,{\Y-\X/2});
\else
\fill (\X,{\Y-\X/2}) coordinate (X-\X-\Y) circle (1pt);
\fi
\fi
\fi
\ifnum\X=5
\ifnum\Y=2
\coordinate (X-\X-\Y) at (\X,{\Y-\X/2});
\else
\fill (\X,{\Y-\X/2}) coordinate (X-\X-\Y) circle (1pt);
\fi
\pgfmathtruncatemacro{\XmY}{\X-\Y}
\ifcase\XmY
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{n}$}; 
\or
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{n-1}$}; 
\or
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{n-2}$}; 
\or
\relax
\or
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{1}$}; 
\or
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{0}$}; 
\fi
\fi
\ifnum\X=3
\coordinate (X-\X-\Y) at (\X,{\Y-\X/2});
\fi
\ifnum\X=0
\else
\pgfmathtruncatemacro{\prevX}{\X-1}
\foreach \Z in {0,...,\prevX}
{\pgfmathtruncatemacro{\DiffYZ}{ifthenelse(\Y==\Z,1,0)+ifthenelse(\Y==\Z+1,1,0)}
\ifnum\DiffYZ>0
\ifnum\X<3
\draw (X-\prevX-\Z) -- (X-\X-\Y);
\fi
\ifnum\X=3
\draw[dashed,shorten >=4mm] (X-\prevX-\Z) -- (X-\X-\Y);
\fi
\ifnum\X=4
\ifnum\Y=1
\else
\draw[dashed,shorten <=4mm] (X-\prevX-\Z) -- (X-\X-\Y);
\fi
\fi
\ifnum\X=5
\ifnum\Z=1
\else
\ifnum\Y=2
\else
\draw (X-\prevX-\Z) -- (X-\X-\Y);
\fi
\fi
\fi
\fi}
\fi
}
}
%
\draw[decorate] (X-5-1) -- (X-5-3);
\draw[decorate] (X-3-0) -- (X-3-3);
\end{tikzpicture}
\end{document}

enter image description here