11

I want to draw a 5 period binomial tree. I have found some code for only 3 period. I was trying to extend it to 5 period, but it turned out too messy at the end. I don't want the nodes overlapping. This means if it is 5 period, there are 2^5=32 terminal nodes.

Here is an example that I want to graph, but it is 3 period.

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}


% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]

% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels. 
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {Bag 1 $4W, 3B$}
    child {
        node[bag] {Bag 2 $4W, 5B$}        
            child {
                node[end, label=right:
                    {$P(W_1\cap W_2)=\frac{4}{7}\cdot\frac{4}{9}$}] {}
                edge from parent
                node[above] {$W$}
                node[below]  {$\frac{4}{9}$}
            }
            child {
                node[end, label=right:
                    {$P(W_1\cap B_2)=\frac{4}{7}\cdot\frac{5}{9}$}] {}
                edge from parent
                node[above] {$B$}
                node[below]  {$\frac{5}{9}$}
            }
            edge from parent 
            node[above] {$W$}
            node[below]  {$\frac{4}{7}$}
    }
    child {
        node[bag] {Bag 2 $3W, 6B$}        
        child {
                node[end, label=right:
                    {$P(B_1\cap W_2)=\frac{3}{7}\cdot\frac{3}{9}$}] {}
                edge from parent
                node[above] {$B$}
                node[below]  {$\frac{3}{9}$}
            }
            child {
                node[end, label=right:
                    {$P(B_1\cap B_2)=\frac{3}{7}\cdot\frac{6}{9}$}] {}
                edge from parent
                node[above] {$W$}
                node[below]  {$\frac{6}{9}$}
            }
        edge from parent         
            node[above] {$B$}
            node[below]  {$\frac{3}{7}$}
    };
\end{tikzpicture}

\end{document}

enter image description here

This is a different method for 3 period, but the nodes are overlapping. I tried to create the paths, but I cannot change it to not overlap.

\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}

enter image description here

Thank you very much for all your response. I think I did not ask my question clearly. I want the nodes not overlapping, in addition, the probability and value at each node is different. I have rewritten my code. Maybe my method is not straight forward.

     \documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{lscape}
\usepackage{tikz}
\usepackage[margin=0.05in]{geometry}
\begin{document}
\begin{landscape}
% Define styles for bags and leafs
\tikzstyle{bag} = [text width=7em, text centered]
\tikzstyle{end} = []
\begin{tikzpicture}[sloped]
  \node (0) at ( 0,0) [bag] {$\widetilde{\mathbb{E}}^5[V_5]=0.02655056$};
  \node (11) at ( 4,-4.8) [bag] {$\widetilde{\mathbb{E}}^5_1[V_5](T)=0.01385611$};
  \node (12) at ( 4,4.8) [bag] {$\widetilde{\mathbb{E}}^5_1[V_5](H)=0.04152669$};
  \node (21) at ( 8,-7.2) [bag] {$\widetilde{\mathbb{E}}^5_2[V_5](TT)=0.004184939$};
  \node (22) at ( 8,-2.4) [bag] {$\widetilde{\mathbb{E}}^5_2[V_5](TH)=0.024557033$};
  \node (23) at ( 8,2.4) [bag] {$\widetilde{\mathbb{E}}^5_2[V_5](HT)=0.024557033$};
  \node (24) at ( 8,7.2) [bag] {$\widetilde{\mathbb{E}}^5_2[V_5](HH)=0.061260166$};
  \node (31) at (12,-8.4) [bag] {$\widetilde{\mathbb{E}}^5_3[V_5](TTT)=0$};
  \node (32) at (12,-6) [bag] {$\widetilde{\mathbb{E}}^5_3[V_5](TTH)=0.008604436$};
  \node (33) at (12,-3.6) [bag] {$\widetilde{\mathbb{E}}^5_3[V_5](THT)=0.008604436$};
  \node (34) at (12,-1.2) [bag] {$\widetilde{\mathbb{E}}^5_3[V_5](THH)=0.041876731$};
  \node (35) at (12,1.2) [bag] {$\widetilde{\mathbb{E}}^5_3[V_5](HTT)=0.008604436$};
  \node (36) at (12,3.6) [bag] {$\widetilde{\mathbb{E}}^5_3[V_5](HTH)=0.041876731$};
  \node (37) at (12,6) [bag] {$\widetilde{\mathbb{E}}^5_3[V_5](HHT)=0.041876731$};
  \node (38) at (12,8.4) [bag] {$\widetilde{\mathbb{E}}^5_3[V_5](HHH)=0.083155004$};

  \node (41) at (16,-9) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](TTTT)=0$};
  \node (42) at (16,-7.8) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](TTTH)=0$};
  \node (43) at (16,-6.6) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](TTHT)=0$};
  \node (44) at (16,-5.4) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](TTHH)=0.0175$};
  \node (45) at (16,-4.2) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](THTT)=0$};
  \node (46) at (16,-3) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](THTH)=0.0175$};
  \node (47) at (16,-1.8) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](THHT)=0.0175$};
  \node (48) at (16,-0.6) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](THHH)=0.0675$};
  \node (49) at (16,0.6) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](HTTT)=0$};
  \node (410) at (16,1.8) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](HTTH)=0.0175$};
  \node (411) at (16,3) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](HTHT)=0.0175$};
  \node (412) at (16,4.2) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](HTHH)=0.0675$};
  \node (413) at (16,5.4) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](HHTT)=0.0175$};
  \node (414) at (16,6.6) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](HHTH)=0.0675$};
  \node (415) at (16,7.8) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](HHHT)=0.0675$};
  \node (416) at (16,9) [bag] {$\widetilde{\mathbb{E}}^5_4[V_5](HHHH)=0.1$};

   \node (51) at (20,-9.3) [bag] {$V_5(TTTTT)=$$0$};
   \node (52) at (20,-8.7) [bag] {$V_5(TTTTH)=$$0$};
   \node (53) at (20,-8.1) [bag] {$V_5(TTTHT)=$$0$};
   \node (54) at (20,-7.5) [bag] {$V_5(TTTHH)=$$0$};
   \node (55) at (20,-6.9) [bag] {$V_5(TTHTT)=$$0$};
   \node (56) at (20,-6.3) [bag] {$V_5(TTHTH)=$$0$};
   \node (57) at (20,-5.7) [bag] {$V_5(TTHHT)=$$0$};
   \node (58) at (20,-5.1) [bag] {$V_5(TTHHH)=$$0.035$};
   \node (59) at (20,-4.5) [bag] {$V_5(THTTT)=$$0$};
   \node (510) at (20,-3.9) [bag] {$V_5( THTTH)=$$0$};
   \node (511) at (20,-3.3) [bag] {$V_5(THTHT)=$$0$};
   \node (512) at (20,-2.7) [bag] {$V_5(THTHH)=$$0.035$};
   \node (513) at (20,-2.1) [bag] {$V_5(THHTT)=$$0$};
   \node (514) at (20,-1.5) [bag] {$V_5(THHTH)=$$0.035$};
   \node (515) at (20,-0.9) [bag] {$V_5(THHHT)=$$0.035$};
   \node (516) at (20,-0.3) [bag] {$V_5(THHHH)=$$0.1$};
   \node (517) at (20,0.3) [bag] {$V_5(HTTTT)=$$0$};
   \node (518) at (20,0.9) [bag] {$V_5(HTTTH)=$$0$};
   \node (519) at (20,1.5) [bag] {$V_5(HTTHT)=$$0$};
   \node (520) at (20,2.1) [bag] {$V_5(HTTHH)=$$0.035$};
   \node (521) at (20,2.7) [bag] {$V_5(HTHTT)=$$0$};
   \node (522) at (20,3.3) [bag] {$V_5(HTHTH)=$$0.035$};
   \node (523) at (20,3.9) [bag] {$V_5(HTHHT)=$$0.035$};
   \node (524) at (20,4.5) [bag] {$V_5(HTHHH)=$$0.1$};
   \node (525) at (20,5.1) [bag] {$V_5(HHTTT)=$$0$};
   \node (526) at (20,5.7) [bag] {$V_5(HHTTH)=$$0.035$};
   \node (527) at (20,6.3) [bag] {$V_5(HHTHT)=$$0.035$};
   \node (528) at (20,6.9) [bag] {$V_5(HHTHH)=$$0.1$};
   \node (529) at (20,7.5) [bag] {$V_5(HHHTT)=$$0.035$};
   \node (530) at (20,8.1) [bag] {$V_5(HHHTH)=$$0.1$};
   \node (531) at (20,8.7) [bag] {$V_5(HHHHT)=$$0.1$};
   \node (532) at (20,9.3) [bag] {$V_5(HHHHH)=$$0.1$};

  \draw [->] (0) to node [below] {0.5412} (11);
  \draw [->] (0) to node [above] {0.4587706} (12);
  \draw [->] (11) to node [below] {0.5252734} (21);
  \draw [->] (11) to node [above] {0.4747266} (22);
  \draw [->] (12) to node [below] {0.5376511} (23);
  \draw [->] (12) to node [above] {0.4623489} (24);

  \draw [->] (21) to node [below] {0.5136300 } (31);
  \draw [->] (21) to node [above] { 0.4863700 } (32);
    \draw [->] (22) to node [below] {0.5205441} (33);
  \draw [->] (22) to node [above] { 0.4794559} (34);
    \draw [->] (23) to node [below] { 0.5205441} (35);
  \draw [->] (23) to node [above] { 0.4794559} (36);
    \draw [->] (24) to node [below] {  0.5304204} (37);
  \draw [->] (24) to node [above] {  0.4695796} (38);

  \draw [->] (31) to node [below] {0.5054728} (41);
  \draw [->] (31) to node [above] { 0.4945272} (42);
    \draw [->] (32) to node [below] { 0.5083179} (43);
  \draw [->] (32) to node [above] { 0.4916821} (44);
    \draw [->] (33) to node [below] { 0.5083179} (45);
  \draw [->] (33) to node [above] { 0.4916821} (46);
    \draw [->] (34) to node [below] { 0.5124654} (47);
  \draw [->] (34) to node [above] {0.4875346} (48);
    \draw [->] (35) to node [below] {0.5083179} (49);
  \draw [->] (35) to node [above] { 0.4916821} (410);
    \draw [->] (36) to node [below] { 0.5124654 } (411);
  \draw [->] (36) to node [above] {0.4875346 } (412);
    \draw [->] (37) to node [below] {0.5124654} (413);
  \draw [->] (37) to node [above] { 0.4875346} (413);
    \draw [->] (38) to node [below] { 0.5183076 } (415);
  \draw [->] (38) to node [above] {0.4816924} (416);

  \draw [->] (41) to node [below] {0.5}(51);
  \draw [->] (41) to node [above] {0.5}(52);
    \draw [->] (42) to node [below] {0.5}(53);
  \draw [->] (42) to node [above] {0.5}(54);
    \draw [->] (43) to node [below] {0.5}(55);
  \draw [->] (43) to node [above] {0.5}(56);
    \draw [->] (44) to node [below] {0.5}(57);
  \draw [->] (44) to node [above] {0.5}(58);
    \draw [->] (45) to node [below] {0.5}(59);
  \draw [->] (45) to node [above] {0.5}(510);
    \draw [->] (46) to node [below] {0.5}(511);
  \draw [->] (46) to node [above] {0.5}(512);
    \draw [->] (47) to node [below] {0.5}(513);
  \draw [->] (47) to node [above] {0.5}(514);
    \draw [->] (48) to node [below] {0.5}(515);
  \draw [->] (48) to node [above] {0.5}(516);
    \draw [->] (49) to node [below] {0.5}(517);
  \draw [->] (49) to node [above] {0.5}(518);
    \draw [->] (410) to node [below] {0.5}(519);
  \draw [->] (410) to node [above] {0.5}(520);
    \draw [->] (411) to node [below] {0.5}(521);
  \draw [->] (411) to node [above] {0.5}(522);
    \draw [->] (412) to node [below] {0.5}(523);
  \draw [->] (412) to node [above] {0.5}(524);
    \draw [->] (413) to node [below] {0.5}(525);
  \draw [->] (413) to node [above] {0.5}(526);
    \draw [->] (414) to node [below] {0.5}(527);
  \draw [->] (414) to node [above] {0.5}(528);
    \draw [->] (415) to node [below] {0.5}(529);
  \draw [->] (415) to node [above] {0.5}(530);
    \draw [->] (416) to node [below] {0.5}(531);
  \draw [->] (416) to node [above] {0.5}(532);

\end{tikzpicture}

\begin{flushright}
Figure 4.3
\end{flushright}
\end{landscape}
\end{document}

enter image description here

  • 2
    Go to the forest when you need trees. texdoc.net/texmf-dist/doc/latex/forest/forest.pdf – user11232 Feb 10 '15 at 7:50
  • I'm not clear whether your question is answered or not. You've accepted my answer, but you've now edited the question with a different tree because (I think) you believe that we've misunderstood. But, as far as I can see, you can adapt the tree I posted to produce the target tree. In particular, my code assumes that the probability and content of the nodes varies. It does assume that at each node, the total of the probabilities of each branch to a child is 1 and that each node has either 0 or 2 children. But that seems true of the new tree you've posted, too. forest really is best here. – cfr Feb 11 '15 at 1:33
7

Here is a beginning using the powerful forest package. This edited version somewhat automatises the production of the tree by defining a bag style which takes 2 arguments for the number of W and the number of B, automatically creating the edge labels and appending the fractions used in the calculation to the terminal nodes.

\documentclass[tikz,border=5pt,10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}

\newcounter{bag}
\forestset{
  bag/.style 2 args= {
    text width=4em,
    text centered,
    if n=1{
      content = {$\frac{#1}{\pgfmathsetmacro\totalinbag{int(#1+#2)}\totalinbag}$},
    }{
      content = {$\frac{#2}{\pgfmathsetmacro\totalinbag{int(#1+#2)}\totalinbag}$},
    },
    before typesetting nodes={
      content/.wrap pgfmath arg = {Bag ##1 $#1W, #2B$}{int(level()+1)},
    },
    for children={
      if n=1{
        branch label={W}{#1}{\pgfmathsetmacro\totalinbag{int(#1+#2)}\totalinbag},
      }{
        branch label={B}{#2}{\pgfmathsetmacro\totalinbag{int(#1+#2)}\totalinbag},
      }
    },
  },
  terminus/.style = {
    align=left,
    child anchor=west,
    for parent={s sep=10mm},
    edge path={
      \noexpand\path [draw, -{Circle[]}, \forestoption{edge}] (!u.parent anchor) -- (.child anchor)\forestoption{edge label};
    },
    delay={
      content/.wrap 6 pgfmath args = {$P(##6)=$##5$\cdot$##4$\cdot$##3$\cdot$##2$\cdot$##1}{content("!u")}{content("!uu")}{content("!uuu")}{content("!uuuu")}{content("!uuuuu")}{content()}
    },
  },
  branch label/.style n args = 3{
    edge label={node [above, midway, sloped] {$#1$} node [below, midway, sloped] {$\frac{#2}{#3}$}},
  },
}

  \begin{forest}
    for tree={
      if n children=0{terminus}{},
      grow=0,
      l sep=15mm,
    }
    [,bag={4}{3}
      [,bag={4}{5}
        [,bag={1}{3}
          [,bag={2}{3}
            [,bag={2}{4}
              [{W_1\cap W_2\cap W_3\cap W_4}
              ]
              [{W_1\cap W_2\cap W_3\cap B_4}
              ]
            ]
            [,bag={2}{4}
              [{W_1\cap W_2\cap W_3\cap W_4}
              ]
              [{W_1\cap W_2\cap W_3\cap B_4}
              ]
            ]
          ]
          [,bag={2}{3}
            [,bag={2}{4}
              [{W_1\cap W_2\cap B_3\cap W_4}
              ]
              [{W_1\cap W_2\cap B_3\cap B_4}
              ]
            ]
            [,bag={2}{4}
              [{W_1\cap W_2\cap B_3\cap W_4}
              ]
              [{W_1\cap W_2\cap B_3\cap B_4}
              ]
            ]
          ]
        ]
        [,bag={1}{3}
          [,bag={2}{3}
            [,bag={2}{4}
              [{W_1\cap B_2\cap W_3\cap W_4}
              ]
              [{W_1\cap B_2\cap W_3\cap B_4}
              ]
            ]
            [,bag={2}{4}
              [{W_1\cap B_2\cap W_3\cap W_4}
              ]
              [{W_1\cap B_2\cap W_3\cap B_4}
              ]
            ]
          ]
          [,bag={2}{3}
            [,bag={2}{4}
              [{W_1\cap B_2\cap B_3\cap W_4}
              ]
              [{W_1\cap B_2\cap B_3\cap B_4}
              ]
            ]
            [,bag={2}{4}
              [{W_1\cap B_2\cap B_3\cap W_4}
              ]
              [{W_1\cap B_2\cap B_3\cap B_4}
              ]
            ]
          ]
        ]
      ]
      [,bag={3}{6}
        [,bag={1}{3}
          [,bag={6}{7}
            [,bag={1}{2}
              [{B_1\cap W_2\cap W_3}
              ]
              [{B_1\cap W_2\cap W_3}
              ]
            ]
            [,bag={1}{2}
              [{B_1\cap W_2\cap W_3}
              ]
              [{B_1\cap W_2\cap W_3}
              ]
            ]
          ]
          [,bag={6}{7}
            [,bag={4}{8}
              [{B_1\cap W_2\cap B_3}
              ]
              [{B_1\cap W_2\cap B_3}
              ]
            ]
            [,bag={4}{8}
              [{B_1\cap W_2\cap B_3}
              ]
              [{B_1\cap W_2\cap B_3}
              ]
            ]
          ]
        ]
        [,bag={1}{3}
          [,bag={4}{6}
            [,bag={3}{6}
              [{B_1\cap B_2\cap W_3}
              ]
              [{B_1\cap B_2\cap W_3}
              ]
            ]
            [,bag={3}{6}
              [{B_1\cap B_2\cap W_3}
              ]
              [{B_1\cap B_2\cap W_3}
              ]
            ]
          ]
          [,bag={4}{6}
            [,bag={3}{6}
              [{B_1\cap B_2\cap W_3}
              ]
              [{B_1\cap B_2\cap W_3}
              ]
            ]
            [,bag={3}{6}
              [{B_1\cap B_2\cap W_3}
              ]
              [{B_1\cap B_2\cap W_3}
              ]
            ]
          ]
        ]
      ]
    ]
  \end{forest}

\end{document}

forest binomial

  • I appreciate that. This saves my life. – user71804 Feb 10 '15 at 19:38
  • @user71804 You're welcome. It is a great package. – cfr Feb 10 '15 at 19:40
8

Here is a dynamic code using forest that automatically builds the tree:

\documentclass[]{standalone}
\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}]}{}}%

\begin{document}
\begin{forest}
    before typesetting nodes={for descendants={content/.pgfmath={int(content("!u")*2-int(n)+2)}}}
    [1@\btree{0}{0}{5}]% Set tree depth here
    % \btree#1#2#3 builds a subtree starting with probability p^#1,(1-p)^#2, and depth #3
\end{forest}
\end{document}

rendering

It is not complete, but could show you how to build dynamic trees with the forest package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.