5

I need to make a probability tree with 3 outcomes per event. Here is the code I am using:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}

% 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]

\begin{tikzpicture}[grow=right, sloped]
\node[bag] {Urn $3G, 4R, 2W$}
    child {
        node[bag] {$2G, 4R, 2W$}        
            child {
                node[end, label=right: {$P(G_1\cap G_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}$}] {}
                edge from parent
                node[above] {$G$}
                node[below]  {$\frac{1}{4}$}
            }
            child {
                node[end, label=right: {$P(G_1\cap R_2)=\frac{1}{3}\times\frac{1}{2}=\frac{1}{6}$}] {}
                edge from parent
                node[above] {$R$}
                node[below]  {$\frac{1}{2}$}
            }
            child {
                node[end, label=right: {$P(G_1\cap W_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}$}] {}
                edge from parent
                node[above] {$W$}
                node[below]  {$\frac{1}{4}$}
            }
            edge from parent 
            node[above] {$G$}
            node[below]  {$\frac{1}{3}$}
    }
    child {
        node[bag] {$3G, 3R, 2W$}   
            child {
                node[end, label=right: {$P(R_1\cap G_2)=\frac{4}{9}\times\frac{3}{8}=\frac{2}{5}$}] {}
                edge from parent
                node[above] {$G$}
                node[below]  {$\frac{3}{8}$}
            }
            child {
                node[end, label=right: {$P(R_1\cap R_2)=\frac{4}{9}\times\frac{3}{8}=\frac{4}{15}$}] {}
                edge from parent
                node[above] {$R$}
                node[below]  {$\frac{3}{8}$}
            }
            child {
                node[end, label=right: {$P(R_1\cap W_2)=\frac{4}{9}\times\frac{1}{4}=\frac{4}{15}$}] {}
                edge from parent
                node[above] {$W$}
                node[below]  {$\frac{1}{4}$}
            }
            edge from parent 
            node[above] {$R$}
            node[below]  {$\frac{4}{9}$}
    }
    child {
        node[bag] {$3G, 4R, 1W$}   
            child {
                node[end, label=right: {$P(W_1\cap G_2)=\frac{2}{9}\times\frac{3}{8}=\frac{1}{12}$}] {}
                edge from parent
                node[above] {$G$}
                node[below]  {$\frac{3}{8}$}
            }
            child {
                node[end, label=right: {$P(W_1\cap R_2)=\frac{2}{9}\times\frac{1}{2}=\frac{1}{9}$}] {}
                edge from parent
                node[above] {$R$}
                node[below]  {$\frac{1}{2}$}
            }
            child {
                node[end, label=right: {$P(W_1\cap W_2)=\frac{2}{9}\times\frac{1}{8}=\frac{1}{36}$}] {}
                edge from parent
                node[above] {$W$}
                node[below]  {$\frac{1}{8}$}
            }
            edge from parent         
            node[above] {$W$}
            node[below]  {$\frac{2}{9}$}
    };
\end{tikzpicture}

\end{document}

Which produces output

How can I make this tree less "squeezed"?

I would rather not use an alternative, I've been using this same tree for other situations with just 2 outcomes per event.

  • Forest will lay out the tree automatically, accounting for everything but the (edge) labels i.e. in the vast majority of cases, it will ensure that edges/nodes don't cross. However, if you don't want to switch, then you just have to tweak manually. – cfr May 8 '16 at 23:20
  • Note that putting the text into the nodes rather than in label nodes, where possible, will help whichever tree-drawing package you use. – cfr May 8 '16 at 23:23
  • 1
    Increase the level 1 sibling distance and/or decrease it for level 2. Note that \tikzstyle is deprecated. – cfr May 8 '16 at 23:25
9

Basically, with this approach to drawing trees, you have to figure out suitable spacing for the tree. TikZ doesn't do it for you.

In this case, the bag nodes need to be wider (or you need to break the lines when mathematical expressions are too long). To avoid the crossings of edges and mixing up of nodes, you need to increase the sibling distance for level 1 relative to the sibling distance for level 2.

For example:

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{trees}
\begin{document}
\tikzset{%
  level 1/.style={level distance=3.5cm, sibling distance=4.5cm},
  level 2/.style={level distance=3.5cm, sibling distance=2cm},
  bag/.style={text width=50pt, text centered},
  end/.style={circle, minimum width=3pt, fill, inner sep=0pt},
}
\begin{tikzpicture}[grow=right, sloped]
  \node[bag] {Urn $3G, 4R, 2W$}
  child {
    node[bag] {$2G, 4R, 2W$}
    child {
      node[end, label=right: {$P(G_1\cap G_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}$}] {}
      edge from parent
      node[above] {$G$}
      node[below]  {$\frac{1}{4}$}
    }
    child {
      node[end, label=right: {$P(G_1\cap R_2)=\frac{1}{3}\times\frac{1}{2}=\frac{1}{6}$}] {}
      edge from parent
      node[above] {$R$}
      node[below]  {$\frac{1}{2}$}
    }
    child {
      node[end, label=right: {$P(G_1\cap W_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}$}] {}
      edge from parent
      node[above] {$W$}
      node[below]  {$\frac{1}{4}$}
    }
    edge from parent
    node[above] {$G$}
    node[below]  {$\frac{1}{3}$}
  }
  child {
    node[bag] {$3G, 3R, 2W$}
    child {
      node[end, label=right: {$P(R_1\cap G_2)=\frac{4}{9}\times\frac{3}{8}=\frac{2}{5}$}] {}
      edge from parent
      node[above] {$G$}
      node[below]  {$\frac{3}{8}$}
    }
    child {
      node[end, label=right: {$P(R_1\cap R_2)=\frac{4}{9}\times\frac{3}{8}=\frac{4}{15}$}] {}
      edge from parent
      node[above] {$R$}
      node[below]  {$\frac{3}{8}$}
    }
    child {
      node[end, label=right: {$P(R_1\cap W_2)=\frac{4}{9}\times\frac{1}{4}=\frac{4}{15}$}] {}
      edge from parent
      node[above] {$W$}
      node[below]  {$\frac{1}{4}$}
    }
    edge from parent
    node[above] {$R$}
    node[below]  {$\frac{4}{9}$}
  }
  child {
    node[bag] {$3G, 4R, 1W$}
    child {
      node[end, label=right: {$P(W_1\cap G_2)=\frac{2}{9}\times\frac{3}{8}=\frac{1}{12}$}] {}
      edge from parent
      node[above] {$G$}
      node[below]  {$\frac{3}{8}$}
    }
    child {
      node[end, label=right: {$P(W_1\cap R_2)=\frac{2}{9}\times\frac{1}{2}=\frac{1}{9}$}] {}
      edge from parent
      node[above] {$R$}
      node[below]  {$\frac{1}{2}$}
    }
    child {
      node[end, label=right: {$P(W_1\cap W_2)=\frac{2}{9}\times\frac{1}{8}=\frac{1}{36}$}] {}
      edge from parent
      node[above] {$W$}
      node[below]  {$\frac{1}{8}$}
    }
    edge from parent
    node[above] {$W$}
    node[below]  {$\frac{2}{9}$}
  };
\end{tikzpicture}
\end{document}

modified tree is less squished

Despite your saying you're not interested in alternative solutions, I cannot draw a tree without offering a Forest solution. Well, not usually.

The nice think about Forest is that the tree can be specified very concisely. Although I've not done so here, it would not be difficult to create the content of the final layer of nodes programmatically by collecting the labels above and below the edges.

The style probability tree can be reused once defined. When used, it allows you to say my label=<above>:<below> to specify the edge labels. The final tier's labels are specified within the nodes (or could be automated, as mentioned above) and the circles are created using a suitable 'arrow' for the edge. s sep and l sep are used to ensure the tree is spread out sufficiently to accommodate the edge labels (because Forest can do this bit automatically), but no measures are needed to ensure that the tree's main nodes and edges do not conflict (Forest does do this automatically).

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{forest}
\begin{document}
\forestset{%
  declare toks={my label}{},
  probability tree/.style={%
    for tree={%
      math content,
      grow'=0,
      child anchor=parent,
      l sep+=40pt,
      +edge={every node/.append style={sloped, midway}},
      delay={%
        split option={my label}{:}{above edge, below edge},
      }
    },
    where n children=0{%
      +edge={-{Circle[width=3pt,length=3pt]}},
      if n=1{
        !u.s sep+=20pt,
      }{}
    }{%
      text width=50pt,
      text centered,
      if={isodd(n_children)}{%
        for n/.wrap pgfmath arg={{##1}{calign with current edge}}{int(.5*(n_children()+1))}
      }{},
    },
    above edge/.style={%
      +edge label={node [above] {$##1$}},
    },
    below edge/.style={%
      +edge label={node [below] {$##1$}},
    },
  },
}
\begin{forest}
  probability tree
  [{Urn $3G, 4R, 2W$}, plain content
  [{2G, 4R, 2W}, my label=G:\frac{1}{3}
      [{P(G_1\cap G_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}}, my label=G:\frac{1}{4}
      ]
      [{P(G_1\cap R_2)=\frac{1}{3}\times\frac{1}{2}=\frac{1}{6}},my label=R:\frac{1}{2}
      ]
      [{P(G_1\cap W_2)=\frac{1}{3}\times\frac{1}{4}=\frac{1}{12}},my label=W:\frac{1}{4}
      ]
    ]
    [{3G, 3R, 2W}, my label=R:\frac{4}{9}
      [{P(R_1\cap G_2)=\frac{4}{9}\times\frac{3}{8}=\frac{2}{5}},my label=G:\frac{3}{8}
      ]
      [{P(R_1\cap R_2)=\frac{4}{9}\times\frac{3}{8}=\frac{4}{15}},my label=R:\frac{3}{8}
      ]
      [{P(R_1\cap W_2)=\frac{4}{9}\times\frac{1}{4}=\frac{4}{15}}, my label=W:\frac{1}{4}
      ]
    ]
    [{3G, 4R, 1W}, my label=W:\frac{2}{9}
      [{P(W_1\cap G_2)=\frac{2}{9}\times\frac{3}{8}=\frac{1}{12}},my label=G:\frac{3}{8}
      ]
      [{P(W_1\cap R_2)=\frac{2}{9}\times\frac{1}{2}=\frac{1}{9}},my label=R:\frac{1}{2}
      ]
      [{P(W_1\cap W_2)=\frac{2}{9}\times\frac{1}{8}=\frac{1}{36}},my label=W:\frac{1}{8}
      ]
    ]
  ]
\end{forest}
\end{document}

Forest tree

2

Some small tweak suggestions:

  • Increase the size of the text in the nodes bag, so your edges do not overlap text. That is, instead of 4em in the definition, use something like:

    \tikzstyle{bag} = [text width=5em, text centered]

  • Change the vertical stretching with a global option. For instance when beginning the tikzpicture environment, use something like:

    \begin{tikzpicture}[grow=right, sloped,yscale=1.5]

  • This looks a lot better, except that the yscale doesn't fix the problem (i.gyazo.com/542ac7d64b8c14cdcc95e6b8660f5627.png) (I tried higher values too, and they still won't fix it) – MCMastery May 8 '16 at 23:19
  • Isn't this more of a comment than an answer? Especially since the second point doesn't seem likely to help much. Also \tikzstyle is deprecated. – cfr May 9 '16 at 0:43
  • 1
    @MCMastery The yscale won't help because the problem is the large sibling distance at level 2 relative to that at level 1. To fix it, you have to ensure not just that each is sufficient, but that the ratio between them is sufficient given the number of children each level 2 node has. – cfr May 9 '16 at 0:47

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.