# Binomial trees using forest package

I need to draw a 3-period tree with different elements at each stage, for example:

the upper path:

(12)(12) ---> ()

the lower path:

(13)(12) ---> (123)

how can I draw such a tree?

Edit:

something like this but for a 3-period tree using the forest package to make it more neat:

    \documentclass[12pt, a4paper]{report}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}
\tikzset{bullet/.style={circle,fill,inner sep=2pt}}

\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=0.1cm,
column sep=3cm,
row sep=0.1cm,nodes={text width=8em}
]
{
&   &  \\
& {$()$} &   \\
$(12)$ &   &  \\
&  {$(123)$} &   \\
&   &  \\
};
\node[bullet,right=0mm of tree-3-1.east](b-3-1){};
\node[bullet,left=0mm of tree-2-2.west](b-2-2){};
\node[bullet,left=0mm of tree-4-2.west](b-4-2){};
\draw[->] (b-3-1) -- (b-2-2) node [midway,above] {$(12)$};
\draw[->] (b-3-1) -- (b-4-2) node [midway,below] {$(13)$};
\end{tikzpicture}
\end{document}

• Since you know that forest exists, can you post a minimal document that shows what you've tried and what you're having problems with? Also, for people not in your particular discipline, your terms won't mean much, or the notation you're using in your question, even though we might know how to help you draw the tree. Furthermore, there are plenty of examples on the site, many with forest: tex.stackexchange.com/search?q=binomial+tree Oct 9, 2019 at 15:41
• I used a different code from another question but I want unique labelling for each step
– Math
Oct 9, 2019 at 16:01
• So show us what you've done and what you want. Oct 9, 2019 at 16:04
• Where did this code come from? Can you edit your question to include a link to its source (I'm assuming a question here on the site.) Oct 9, 2019 at 16:07
• I don't understand the question. As @AlanMunn suggested.
– cfr
Oct 9, 2019 at 22:22

Like Alan Munn and cfr I also do not understand the problem. You could make the code you have a bit more versatile by using the auto enumeration that can be found in the forest manual on p. 52, and then all you need to do is to add some basic pgf functions. You may want to adjust the items "cat","mole","duck","marmot",... to your needs.

\documentclass[12pt, a4paper]{report}
\usepackage{forest}
\bracketset{action character=@}
\def\pexp#1#2{\pgfmathsetmacro{\x}{int(#1)}\pgfmathparse{\x>0?(\x>1?"#2^{\x}":"#2"):""}\pgfmathresult}%
\def\btreelabel#1#2{node[midway,sloped,font=\scriptsize,/forest,if n=1{/tikz/below}{/tikz/above}]{$\pexp{#1}{p}\pexp{#2}{(1-p)}$}}%
\def\btree#1#2#3{,grow=east,l*=2,edge label=\btreelabel{#1}{#2},if={#3>0}{append=[@\btree{#1}{#2+1}{#3-1}],append=[@\btree{#1+1}{#2}{#3-1}]}{}}%

\xdef\LstItems{"cat","mole","duck","marmot","koala","rhino","bear","hare","opossum"}
\pgfmathdeclarefunction{mitem}{1}{%
\begingroup%
\pgfmathparse{{\LstItems}[#1-1]}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%

\begin{document}
\begin{forest}
for tree={grow'=0},tempcounta=1,
before typesetting nodes={for descendants={content/.pgfmath={mitem(int(tempcounta))}, tempcounta+=1}}
[pft@\btree{0}{0}{2}]
\end{forest}
\end{document} \documentclass[12pt, a4paper]{report}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={grow'=east,l sep=4em,s sep=3em,circle,inner sep=2pt,fill}
[,label=left:{$(12)$}
[,label=right:{$()$},edge label={node[midway,sloped,above]{(12)}}]
[,label=right:{$(123)$},edge label={node[midway,sloped,below]{(13)}}]
]
\end{forest}
\end{document} \documentclass[12pt, a4paper]{report}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={grow'=east,l sep=4em,s sep=3em,circle,inner sep=2pt,fill}
[,label=left:{$(12)$}
[,label=above:{$()$},edge label={node[midway,sloped,above]{$(12)$}}
[,label=right:$X$,edge label={node[midway,sloped,above]{$x$}}]
[,label=right:$Y$,edge label={node[midway,sloped,below]{$y$}}]
]
[,label=below:{$(123)$},edge label={node[midway,sloped,below]{(13)}}
[,label=right:$U$,edge label={node[midway,sloped,above]{$u$}}]
[,label=right:$V$,edge label={node[midway,sloped,below]{$v$}}]
]
]
\end{forest}
\end{document} • the values p, p^2 etc, I want to change them to say x and z, how can I do this?
– Math
Oct 10, 2019 at 11:21
• how do I change the probabilities on the trees to something different? for example I'm drawing trees for a particular group, so I need different elements
– Math
Oct 10, 2019 at 11:54
• I want a 3 period binomial tree with 4 terminal nodes as in your answer, however I want to rename the associated probabilities labelled on the line segments.
– Math
Oct 10, 2019 at 14:42
– Math
Oct 10, 2019 at 14:43
• Also, how can I 'push' the (12) closer to the tree?
– Math
Oct 10, 2019 at 14:50

Based on answer (which was significantly improved by edit of @marmoth, thank you again!):

\documentclass[12pt, margin=3mm]{standalone}
\usepackage{forest}

\begin{document}
\forestset{
declare toks={elo}{font=\footnotesize, inner sep=2pt, midway, sloped}, % Edge Label Options
anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
decision edge label/.style 2 args= {
edge label/.expanded={node[anchor=#1,\forestoption{elo}]{$#2$}}
},
decision/.style={if n=1
{decision edge label={south}{#1}}
{decision edge label={north}{#1}}
},
decision tree/.style={
for tree={grow'=east,
s sep=1em,l=13ex,
if n children=0{anchors=west}{
if n=1{anchors=south east}{anchors=north east}
},
},
anchors=east,
dot, for descendants=dot,
delay={for descendants={split option={content}{;}{content,decision}}},
}
}

\begin{forest} decision tree
[(12)
[(X);(12),
[A;a]
[B;b]
]
[(123);(13),
[C;c]
[D;d]
]
]
\end{forest}
\end{document} • Very nice. Have you tried sometimes istgame package to create a tree? Oct 10, 2019 at 21:08
• @Sebastiano, no. But i sow some answer here using this package. I'm more interested to improve my knowledge about forest. Oct 10, 2019 at 21:26