概率树正在碰撞

概率树正在碰撞

我需要制作一个概率树,每个事件有 3 个结果。这是我使用的代码:

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

产生输出

如何才能让这棵树不那么“受挤压”呢?

我宁愿不使用替代方案,我一直对其他情况使用同一棵树,每个事件只有 2 个结果。

答案1

基本上,使用这种方法绘制树时,您必须确定树的合适间距。TikZ 不会为您完成此操作。

在这种情况下,bag节点需要更宽(或者当数学表达式太长时,您需要断开线条)。为了避免边的交叉和节点的混淆,您需要增加第 1 级相对于第 2 级的兄弟距离的兄弟距离。

例如:

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

改良后的树被压扁的程度更小

尽管你说你对替代解决方案不感兴趣,但我无法画出一棵树而不提供森林解决方案。嗯,通常不是。

Forest 的优点在于可以非常简洁地指定树。虽然我在这里没有这样做,但通过收集边缘上方和下方的标签以编程方式创建最后一层节点的内容并不困难。

样式probability tree定义后即可重复使用。使用时,您可以my label=<above>:<below>指定边缘标签。最后一层的标签在节点内指定(或者可以自动指定,如上所述),并使用合适的“箭头”为 . 创建圆圈,用于确保edge树充分展开以容纳边缘标签(因为 Forest 可以自动执行此操作),但无需采取任何措施来确保树的主节点和边缘不发生冲突(Forest 会自动执行此操作)。s sepl sep

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

森林树

答案2

一些小的调整建议:

  • 增加节点中文本的大小bag,这样边缘就不会与文本重叠。也就是说,4em在定义中,使用类似以下内容:

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

  • 使用全局选项更改垂直拉伸。例如,在开始环境时tikzpicture,使用如下命令:

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

相关内容