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}
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.
Best Answer
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:
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 saymy 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 theedge
.s sep
andl 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).