绘制 5 周期二项树

绘制 5 周期二项树

我想画一个 5 周期的二项式树。我找到了一些只有 3 周期的代码。我试图将其扩展到 5 周期,但最后却变得太乱了。我不希望节点重叠。这意味着如果它是 5 周期,则有 2^5=32 个终端节点。

这是我想要绘制的图表的一个例子,但它有 3 个周期。

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

在此处输入图片描述

这是 3 个周期的不同方法,但节点重叠。我尝试创建路径,但无法将其更改为不重叠。

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

在此处输入图片描述

非常感谢您的回复。我想我的问题问得不太清楚。我希望节点不重叠,此外,每个节点的概率和值都不同。我重写了我的代码。也许我的方法不够直接。

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

在此处输入图片描述

答案1

这是使用强大forest包的开始。此编辑版本通过定义一个样式来自动化树的生成,bag该样式采用 2 个参数,分别为 的数量W和 的数量B,自动创建边缘标签并将计算中使用的分数附加到终端节点。

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

森林二项式

答案2

forest下面是用于自动构建树的动态代码:

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

渲染

它并不完整,但可以向您展示如何使用forest包构建动态树。

相关内容