3

A long time ago I have written a code for a probability tree (see picture below), unfortunately i lost the code. Does somebody have an idea, how i can realize it again?

I have already tried an old version but it fails

\begin{figure}[H]
\centering
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=0.6cm,y=0.5cm]
\clip(0,-6.5) rectangle (18,6);
 \draw [line width=2pt] (2,-1)-- (9,2);
 \draw [line width=2pt] (2,-1)-- (9,-4);
  \draw [line width=2pt] (9,2)-- (16,4);
  \draw [line width=2pt] (9,2)-- (16,0);
  \draw [line width=2pt] (9,-4)-- (16,-2);
   \draw [line width=2pt] (9,-4)-- (16,-6);
  \filldraw [red] (2,-1) circle (2.8pt);
  \filldraw [red] (9,2) circle (2.8pt);
  \filldraw [red] (9,-4) circle (2.8pt);
  \filldraw [red] (16,-6) circle (2.8pt);
   \filldraw [red] (16,-2) circle (2.8pt);
  \filldraw [red] (16,4) circle (2.8pt);
 \filldraw [red] (16,0) circle (2.8pt);
 \node (A) at (9,2.8) {$V$};
 \node (B) at (9,-3.2) {$\overline{V}$};
 \node (C) at (16,4.8) {$S$};
 \node (D) at (16,0.8) {$\overline{S}$};
 \node (E) at (16,-1.2) {$S$};
 \node (F) at (16,-5.2) {$\overline{S}$}; 
\node (G) at (5,1.5) {\scriptsize {\color{blue}$P(V)=0,4$}};
\node (G) at (5,-3.5) {\scriptsize {\color{blue}$P(\overline{V})=0,6$}};
\node (G) at (12,3.8) {\scriptsize {\color{blue}$P(S|V)=0,6$}};
\node (G) at (12,0) {\scriptsize {\color{blue}$P(\overline{S}|V)=0,4$}};
\node (G) at (12,-2) {\scriptsize {\color{blue}$P(S|\overline{V})=0,3$}};
\node (G) at (12,-6) {\scriptsize {\color{blue}$P(\overline{S}|\overline{V})=0,7$}};
\end{tikzpicture}
\caption{Baumdiagramm für Aufgabe 11}

\end{figure}  

probability tree

  • What does fail? Does the document not compile or does it look different? Can you add the document header? (With \documentclass{scrartcl} \usepackage{tikz} \usetikzlibrary{arrows} \begin{document}and \end{document} it compiles without errors. (TexLive 2016)) – knut Apr 16 '17 at 18:34
  • The code above does not fail at all. It is just not like the tree that is in the picture. – David Apr 16 '17 at 18:44
  • I posted the code just because i was told that my post is too short or not precise enough – David Apr 16 '17 at 18:45
2

In short, to modify your code, replace all the instances where you have

\filldraw [red] (x,y) circle (2.8pt);

with

\node [draw=black,fill=red!10,circle] at (x,y) {$A$};

Except for the first red circle, that you can just remove. And of course replace $A$ with the appropriate symbol for the node in question. Here is a version of your code where I cleaned up a little bit. I didn't change the text in the nodes, but that you can manage.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,x=0.6cm,y=0.5cm]
\coordinate (O) at (2,-1);
\begin{scope}[every node/.style={circle,draw=black,fill=red!10}]
 \node (A) at (9,2) {$V$};
 \node (B) at (9,-4) {$\overline{V}$};
 \node (C) at (16,4) {$S$};
 \node (D) at (16,0) {$\overline{S}$};
 \node (E) at (16,-2) {$S$};
 \node (F) at (16,-6) {$\overline{S}$}; 
\end{scope}

\draw (O) -- (A) -- (C)
             (A) -- (D)
      (O) -- (B) -- (E)
             (B) -- (F);

\begin{scope}[every node/.style={blue,font=\scriptsize}]
\node at (5,1.5) {$P(V)=0,4$};
\node at (5,-3.5) {$P(\overline{V})=0,6$};
\node at (12,3.8) {$P(S|V)=0,6$};
\node at (12,0) {$P(\overline{S}|V)=0,4$};
\node at (12,-2) {$P(S|\overline{V})=0,3$};
\node at (12,-6) {$P(\overline{S}|\overline{V})=0,7$};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

TikZ tree

However, this is a tree, so you could also use the tree drawing capabilities of TikZ, as such:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
   circ/.style={draw=black,fill=red!15,circle},
   grow=right,
   level 1/.style={sibling distance=2cm},
   level 2/.style={sibling distance=1cm},
   level distance=3cm]
\node [coordinate] {}
  child {
    node[circ] {$\bar{A}$}
      child { node[circ] {$\bar{D}$}
        edge from parent
        node [below=2] {$0.9999$}}
      child { node[circ] {$D$}
        edge from parent
        node [above=2] {$0.0001$}}
    edge from parent
    node [below=2] {$0.9999$}
    }
  child {
    node[circ] {$A$}
      child { node[circ] {$\bar{D}$}
        edge from parent
        node [below=2] {$0.0001$}}
      child { node[circ] {$D$}
        edge from parent
        node [above=2] {$0.9999$}
  }
   edge from parent
   node [above=2] {$0.0001$}
  };
\end{tikzpicture}
\end{document}

enter image description here

forest

Or using the more powerful forest package, which is rather popular I think. I'm no expert though, so it's quite possible the edge labels could be solved more elegantly.

\documentclass[border=5mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
  math content,
  if n=0{coordinate}{circle,draw=black,fill=red!10},
  grow=0,
  l=3cm
}
[
 [\overline{A},edge label={node[midway,below=2pt]{0.9999}}
  [\overline{D},edge label={node[midway,below]{0.9999}}]
  [D,edge label={node[midway,above]{0.0001}}] 
 ]
 [A,edge label={node[midway,above=2pt]{0.0001}}
  [\overline{D},edge label={node[midway,below]{0.0001}}]
  [D,edge label={node[midway,above]{0.9999}}] 
 ]
]
\end{forest}
\end{document}

enter image description here

1

A small variation of forest case in Torbjørn T. answer:

\documentclass[border=5mm]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}% <-- added

\begin{document}
\forestset{% <-- added
  EL/.style 2 args={% shortens for (my) edge label
    edge label={node[midway, font=\footnotesize, #1]{#2}},
                    },
        }% end of forestset

\begin{forest}
for tree={
  math content, % <-- all text is math
  if n=0{coordinate}{circle,draw=black,fill=red!10,drop shadow},% <-- slightly changed
  grow=0,
  l sep = 24mm,
        }% end of for tree
[
    [\overline{A},EL={below=1pt}{0.9999}% <-- changed
        [\overline{D},EL={below}{0.9999}]
        [D,EL={above}{0.0001}]
    ]
    [A,EL={above=2pt}{0.0001}
        [\overline{D},EL={below}{0.0001}]
        [D,EL={above}{0.9999}]
    ]
]
\end{forest}
\end{document}

Differences are in writing of edges' labels. Result is similar:

enter image description here

(Idea for used label writing is taken from cfr answer here.)

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.