决策树与森林

决策树与森林

我想画下面的图。我学习如何画它来自线两侧带有标签的决策树并得到部分解决方案。

我无法做以下事情:

  • 如何让线的起点和决策树的第一个节点在同一垂直线上?
  • 如何让线的第二个点和决策树的第三个节点也在同一条垂直线上?
  • 如何绘制这些边缘标签(测试,无测试......)?
  • 节点标签如何绘制(测试结果揭晓)?
  • 树枝末端的垂直线如何画?

决策树

\documentclass{beamer}
\usepackage{forest}
\tikzset{
decision/.style={rectangle, minimum height=3pt, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt},
chance/.style={circle, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt}
 }

\forestset{
 declare toks={optimality}{},
 declare toks={edge label below}{},
 }


\begin{document}
\begin{frame}[fragile]
\begin{tikzpicture}[xscale=8.5]
\draw[-] (0,0) -- (1,0);
\draw  (0,0) node[below,align=center] {\scriptsize Development \\[-2mm] \scriptsize and test} node[above=0.2cm] {\scriptsize Now} -- (0,0.2);
\draw  (0.65,0) node[below,align=center]{\scriptsize Initial investment \\[-2mm]\scriptsize -\$1,500} node[above=0.2cm] {\scriptsize Year 1} -- (0.65,0.2);
\end{tikzpicture}

\scalebox{0.85}{%
\begin{forest}
my label/.style={
 edge label={node[midway,above,font=\scriptsize,align=center]{#1}}
},
my below/.style={
edge label below={node[midway,below,font=\scriptsize,align=center]{#1}}
},
 for tree={
  grow=0,
  child anchor=west,
  anchor=west,
  text ragged,
  inner sep=1mm,
  edge={draw=black},
  l sep+=30mm,
  s sep+=5mm,
  edge path={
    \noexpand\path[\forestoption{edge}]
    (!u.parent anchor) -- ([xshift=-22.5mm].child anchor)\forestove{optimality} 
   --  (.child anchor)\forestoption{edge label below}\forestoption{edge label};
  },
  }
  [,decision
  []
  [,label={\scriptsize Test results revealed}, chance
    [,decision,my label=Success, my below=(75\% probability)
      [,my label={NPV$=$0} 
      ]
      [,my label={NPV$=$-\$3,611}
      ]
    ]
    [,decision,my label=Failure, my below=(25\% probability)
      [,my label={NPV$=$0}
      ]
      [,my label={NPV$=$\$1,517}
      ]
    ]        
    ]
 ]
 \end{forest}%
 }%

 \scriptsize 
 Squares represent decision points. Circle represents receipt of information. 

 SEC must make two decisions:

 ~~1. Whether to develop and test the engine.

 ~~2. Whether to invest for full-scale production.

 With decision trees, decisions are made in reverse order.

\end{frame}
\end{document}

答案1

这是一项提议。森林基于 TiZ,因此无需将注释与forest环境分开。通过使用,alias您可以为forest节点命名,这些名称可用于查找注释的适当位置。

\documentclass{beamer}
\usepackage{forest}
\tikzset{
decision/.style={rectangle, minimum height=3pt, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt},
chance/.style={circle, minimum width=3pt, draw=black, fill=black, thick, inner sep=0pt}
 }

\forestset{
 declare toks={optimality}{},
 declare toks={edge label below}{},
 }


\begin{document}
\begin{frame}[fragile]

\scalebox{0.85}{%
\begin{forest}
my label/.style={
 edge label={node[midway,above,font=\scriptsize,align=center]{#1}}
},
my below/.style={
edge label below={node[midway,below,font=\scriptsize,align=center]{#1}}
},
 for tree={
  grow=0,
  child anchor=west,
  anchor=west,
  text ragged,
  inner sep=1mm,
  edge={draw=black},
  l sep+=30mm,
  s sep+=5mm,
  edge path={
    \noexpand\path[\forestoption{edge}]
    (!u.parent anchor) -- ([xshift=-22.5mm].child anchor)\forestove{optimality} 
   --  (.child anchor)\forestoption{edge label below}\forestoption{edge label};
  },
  }
  [,decision,alias=Mexico
  [,alias=South Africa]
  [,label={\scriptsize Test results revealed}, chance
    [,decision,my label=Success, my below=(75\% probability)
      [,my label={NPV$=$0},alias=Japan 
      ]
      [,my label={NPV$=$-\$3,611}
      ]
    ]
    [,decision,my label=Failure, my below=(25\% probability),alias=Australia
      [,my label={NPV$=$0}
      ]
      [,my label={NPV$=$\$1,517},alias=New Zealand
      ]
    ]        
    ]
 ]
 \begin{scope}[font=\scriptsize]
  \draw[-] ([yshift=1cm]New Zealand.east) coordinate(tr) -- (tr-|Mexico.west)
  coordinate(tl);
  \draw (Australia.west |- tr) node[below,align=center] {Development\\ 
  and test} -- ++(0,2mm) node[above]{Year 1};
  \draw (tl) node[below,align=center] {Initial investment \\
   -\$1,500} -- ++(0,2mm) node[above]{Now};
  \draw ([yshift=-1mm]South Africa.west) --++(0,2mm);
 \end{scope}
 \end{forest}%
 }%

 \scriptsize 
 Squares represent decision points. Circle represents receipt of information. 

 SEC must make two decisions:

 ~~1. Whether to develop and test the engine.

 ~~2. Whether to invest for full-scale production.

 With decision trees, decisions are made in reverse order.

\end{frame}
\end{document}

在此处输入图片描述

相关内容