3

How do I make the following probability trees in LaTeX?

Thanks for the help!!


Probability Tree 1:

probability tree 1

Probability Tree 2:

probability tree 2

\forestset{my label/.style={if n=1{edge label={node [midway, above left, font=\tiny] {$#1$}},}{edge label={node [midway, below left, font=\tiny] {$#1$}},
}
}
}

\begin{forest}
for tree={
    grow'=0,
    math content,
    parent anchor=east,
    child anchor=west,
    edge path={
        \noexpand\path [\forestoption{edge}] (!u.parent anchor) node
        [circle, fill, minimum size=2.5pt, inner sep=0pt,
        anchor=center]
        {} -- (.child anchor)\forestoption{edge label};
        },
    edge={->},
    anchor=west,
    l sep+=15mm,
    s sep+=10mm,
}

%Left figure
[
   [\textbf{\textit{p}}^{good}, my label={P(good\rvert club_1)},
       tikz={\node (sa) [above=10mm of .north, anchor=base] {$S_A$};}]
   [\textbf{\textit{p}}_{rough}^{bad}, my label={P(bad\rvert club_1)}]
]


[, phantom, l sep-=25mm

%Right figure

[
[\textbf{\textit{p}}^{good}, my label={P(good\rvert club_2)},
   tikz={\node (sb) [anchor=base east] at (.west |- sa.base) {$S_B$};
   \draw [thick] (!r |- sa.south) -- (.east |- sa.south);}]

[\textbf{\textit{p}}^{bad}_{rough}, my label={P(bad\rvert club_2)}]
]
]
\end{forest}

Probability Tree 3:

probability tree 3

Andrew
  • 55

1 Answers1

5

Here is the most complex tree. You should be able to modify it to produce the simpler trees. If you get stuck, just post the code you have and ask how to solve the problem you come across.

This uses the powerful forest package which allows the standard bracket notation to be used for drawing trees. My answer to an earlier question introduces forest and explains how to turn a tree into a bracket specification.

\documentclass[tikz, border=10pt]{standalone}
\usepackage{forest,mathtools}
\usetikzlibrary{arrows.meta,positioning}% requires TikZ/PGF 3 - comment if you don't have arrows.meta
%\usetikzlibrary{positioning}% uncomment if you don't have arrows.meta
\begin{document}
\forestset{
  my label/.style={
    if n=1{
      edge label={node [midway, above left, font=\tiny] {$#1$}},
    }{
      edge label={node [midway, below left, font=\tiny] {$#1$}},
    }
  }
}
\begin{forest}
  for tree={
    grow'=0,
    math content,
    parent anchor=east,
    child anchor=west,
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) node [circle, fill, minimum size=2.5pt, inner sep=0pt, anchor=center] {} -- (.child anchor)\forestoption{edge label};
    },
    edge={-{Stealth[]}},% requires TikZ/PGF version 3 - comment if you don't have arrows.meta
    %edge={->},% uncomment this line if you don't have arrows.meta
    anchor=west,
    l sep+=15mm,
    s sep+=5mm,
  }
  [
    [p^{good}, my label={P[good\rvert club_1]}, tikz={\node (sa) [above=10mm of .north, anchor=base] {$S_A$};}
    ]
    [p^{bad}, my label={P[bad\rvert club_1]}
      [p^{bad}_{rough}, my label={\delta\cdot P[bad\rvert club_1]}
        [, phantom, l sep-=25mm
          [
            [p^{good}, my label={P[good\rvert club_2]},
              tikz={
                \node (sb) [anchor=base east] at (.west |- sa.base) {$S_B$};
                \draw [thick] (!r |- sa.south) -- (.east |- sa.south);
              }
            ]
            [p^{bad}_{rough}, my label={P[bad\rvert club_2]}
            ]
          ]
        ]
      ]
      [p^{bad}_{sand}, my label={(1-\delta)\cdot P[bad\rvert club_1]}
      ]
    ]
  ]
\end{forest}
\end{document}

probability tree

EDIT

Here is some help with all three trees:

\documentclass[tikz, border=10pt, multi]{standalone}
\usepackage{forest,mathtools}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\forestset{
  my label/.style={
    if n=1{
      edge label={node [midway, above left, font=\tiny] {$#1$}},
    }{
      edge label={node [midway, below left, font=\tiny] {$#1$}},
    }
  }
}

\begin{forest}
  for tree={
    grow'=0,
    math content,
    parent anchor=east,
    child anchor=west,
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) node [circle, fill, minimum size=2.5pt, inner sep=0pt, anchor=center] {} -- (.child anchor)\forestoption{edge label};
    },
    edge={-{Stealth[]}},
    anchor=west,
    l sep+=15mm,
    s sep+=5mm,
    if level=1{tier=t}{}
  }
  [
  [p^{bad}_{O.B.}, tikz={\node (sa) [above=10mm of .north, anchor=base east] {$unplayable$};}
    ]
    [, phantom
      [,
        [p^{bad}_{rough},
          tikz={
            \node (sb) [anchor=base east] at (.center |- sa.base) {$playable$};
            \draw [thick] (!r |- sa.south) -- (.east |- sa.south);
          }
        ]
        [p^{bad}_{trees}]
        [p^{bad}_{sand}
        ]
      ]
    ]
    [p^{bad}_{water}
    ]
  ]
\end{forest}
\begin{forest}
  for tree={
    grow'=0,
    math content,
    parent anchor=east,
    child anchor=west,
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) node [circle, fill, minimum size=2.5pt, inner sep=0pt, anchor=center] {} -- (.child anchor)\forestoption{edge label};
    },
    edge={-{Stealth[]}},
    anchor=west,
    l sep+=15mm,
    s sep+=5mm,
    if level=1{tier=t}{}
  }
  [
    [p^{good}, my label={P[good\rvert club_1]}, tikz={\node (sa) [above=10mm of .north, anchor=base] {$S_A$};}
    ]
    [, phantom
      [,
        [p^{good}, my label={P[good\rvert club_2]},
          tikz={
            \node (sb) [anchor=base east] at (.west |- sa.base) {$S_B$};
            \draw [thick] (!r |- sa.south) -- (.east |- sa.south);
          }
        ]
        [,phantom]
        [p^{bad}_{rough}, my label={P[bad\rvert club_2]}
        ]
      ]
    ]
    [p^{bad}_{rough}, my label={P[bad\rvert club_1]}
    ]
  ]
\end{forest}
\begin{forest}
  for tree={
    grow'=0,
    math content,
    parent anchor=east,
    child anchor=west,
    edge path={
      \noexpand\path [\forestoption{edge}] (!u.parent anchor) node [circle, fill, minimum size=2.5pt, inner sep=0pt, anchor=center] {} -- (.child anchor)\forestoption{edge label};
    },
    edge={-{Stealth[]}},
    anchor=west,
    l sep+=15mm,
    s sep+=5mm,
  }
  [
    [p^{good}, my label={P[good\rvert club_1]}, tikz={\node (sa) [above=10mm of .north, anchor=base] {$S_A$};}
    ]
    [p^{bad}, my label={P[bad\rvert club_1]}
      [p^{bad}_{rough}, my label={\delta\cdot P[bad\rvert club_1]}
        [, phantom, l sep-=25mm
          [
            [p^{good}, my label={P[good\rvert club_2]},
              tikz={
                \node (sb) [anchor=base east] at (.west |- sa.base) {$S_B$};
                \draw [thick] (!r |- sa.south) -- (.east |- sa.south);
              }
            ]
            [p^{bad}_{rough}, my label={P[bad\rvert club_2]}
            ]
          ]
        ]
      ]
      [p^{bad}_{sand}, my label={(1-\delta)\cdot P[bad\rvert club_1]}
      ]
    ]
  ]
\end{forest}
\end{document}

three trees

Take a look at the answer I linked above for help understanding the bracket notation. Else this will just seem like magic tricks and be impossible for you to adapt ;).

cfr
  • 198,882
  • This is great!!! Thank you what if it were in a \documentclass{article} ? – Andrew Jul 29 '15 at 23:27
  • @Andrew It will work fine in article. standalone is just good for making examples or working on images before including them in a main document. Just add the \usepackage... and \usetikzlibrary... to your document and then include the code for the tree. You can e.g. put it in a \begin{figure}...\end{figure} or however you wish ;). – cfr Jul 29 '15 at 23:59
  • for some reason, my up-to-date version of MiKTeX does not like \usetikzlibrary{arrows.meta} . This is what the error message says: ! I can't find file tikzlibraryarrows.meta.code.tex. ...nput tikzlibrary\pgf@temp .code.tex – Andrew Jul 30 '15 at 00:23
  • @Andrew That is an update problem. MikTeX definitely now has TikZ/PGF version 3 available. However, you can drop that library and change -{Stealth[]} to -> in the definition of edge if you don't want to update. – cfr Jul 30 '15 at 00:48
  • @Andrew I've added some commented lines to make this clearer. If you'd rather not update, just comment the \usetikzlibrary and edge lines indicated and uncomment the alternatives just below them. – cfr Jul 30 '15 at 01:22
  • Sorry. I struggling to figure out how to make either of the other two images. The code I have for prob tree 2 is in with my original post. @cfr – Andrew Jul 30 '15 at 02:57
  • @Andrew See above. But do take a look at the answer I linked to. These are pretty easy to adapt once you understand the brackets notation. But it can be deeply mysterious initially. – cfr Jul 30 '15 at 03:15
  • THANK YOU! I will. what is the purpose of [phantom]...[, phantom] @cfr – Andrew Jul 30 '15 at 04:05
  • @Andrew They just help with spacing or placement of other nodes. They are essentially invisible in that nothing is drawn, but they affect the way forest packs the other nodes. – cfr Jul 30 '15 at 13:04