Erstellen eines Wahrscheinlichkeitsbaums mit TikZ

Erstellen eines Wahrscheinlichkeitsbaums mit TikZ

Der folgende Code sollte einen Wahrscheinlichkeitsbaum mit drei Schritten erzeugen, kann aber nicht kompiliert werden. Hat jemand eine Idee, warum das nicht funktioniert?

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}

\begin{tikzpicture}[scale=1.5,font=\footnotesize]

% Specify spacing for each level of the tree

\tikzstyle{level 1}=[level distance=12mm,sibling distance=43mm]

\tikzstyle{level 2}=[level distance=15mm,sibling distance=20mm]

\tikzstyle{level 3}=[level distance=15mm,sibling distance=10mm]


\tikzset{

  solid node/.style={circle,draw,inner sep=1,fill=black},

  }


   % The Tree

     \node(0)[solid node]{}

   child{node(1)[solid node,label=left:{$V=1$}]{}

child{node[solid node,label=left:{$s_1=H$}]{}

child{node[solid node,label=below:{$s_2=U$}]{} edge from parent node [left]{$q$}}

 child{node[solid node,label=below:{$s_2=D$}]{} edge from parent node [right]{$1-q$}}

 edge from parent node [left]{$p$}

 }

 child{node[solid node,label=right:{$s_1=H$}]{}

child{node[solid node,label=below:{$s_2=U$}]{} edge from parent node [left]{$q$}}

child{ node[solid node,label=below:{$s_2=D$}]{} edge from parent node [right]{$1-q$}}

edge from parent node [right]{$1-p$}

}

edge from parent node [left, yshift=3]{$\frac{1}{2}$}

}

child{node(2)[solid node,label=right:{$V=-1$}]{}

child{node[solid node,label=left:{$s_1=L$}]{}

child{node[solid node,label=below:{$s_2=U$}]{} edge from parent node [left]{$1-q$}}

child{ node[solid node,label=below:{$s_2=D$}]{} edge from parent node [right]{$q$}}

edge from parent node [left]{$p$}

}

child{node[solid node,label=right:{$s_1=L$}]{}

child{node[solid node,label=below:{$s_2=U$}]{} edge from parent node [left]{$1-q$}}

child{ node[solid node,label=below:{$s_2=D$}]{} edge from parent node [right]{$q$}}

edge from parent node [right]{$1-p$}

}

edge from parent node [right, yshift=3]{$\frac{1}{2}$}

};


\end{tikzpicture}

\end{document}

Antwort1

Es mag einfach nicht all die leeren Zeilen. Wenn ich diese entferne, wird Ihr Code problemlos kompiliert. Ich habe auch aktualisiert, um die veraltete Verwendung von zu entfernen \tikzstyle, aber das ist für die Kompilierung nicht erforderlich.

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}
  [
    scale=1.5,
    font=\footnotesize,
    level 1/.style={level distance=12mm,sibling distance=43mm},
    level 2/.style={level distance=15mm,sibling distance=20mm},
    level 3/.style={level distance=15mm,sibling distance=10mm},
    solid node/.style={circle,draw,inner sep=1,fill=black},
  ]

  \node(0)[solid node]{}

  child{node(1)[solid node,label=left:{$V=1$}]{}
    child{node[solid node,label=left:{$s_1=H$}]{}
      child{node[solid node,label=below:{$s_2=U$}]{} edge from parent node [left]{$q$}}
      child{node[solid node,label=below:{$s_2=D$}]{} edge from parent node [right]{$1-q$}}
      edge from parent node [left]{$p$}
    }
    child{node[solid node,label=right:{$s_1=H$}]{}
      child{node[solid node,label=below:{$s_2=U$}]{} edge from parent node [left]{$q$}}
      child{ node[solid node,label=below:{$s_2=D$}]{} edge from parent node [right]{$1-q$}}
      edge from parent node [right]{$1-p$}
    }
    edge from parent node [left, yshift=3]{$\frac{1}{2}$}
  }
  child{node(2)[solid node,label=right:{$V=-1$}]{}
    child{node[solid node,label=left:{$s_1=L$}]{}
      child{node[solid node,label=below:{$s_2=U$}]{} edge from parent node [left]{$1-q$}}
      child{ node[solid node,label=below:{$s_2=D$}]{} edge from parent node [right]{$q$}}
      edge from parent node [left]{$p$}
    }
    child{node[solid node,label=right:{$s_1=L$}]{}
      child{node[solid node,label=below:{$s_2=U$}]{} edge from parent node [left]{$1-q$}}
      child{ node[solid node,label=below:{$s_2=D$}]{} edge from parent node [right]{$q$}}
      edge from parent node [right]{$1-p$}
    }
    edge from parent node [right, yshift=3]{$\frac{1}{2}$}
  };

\end{tikzpicture}
\end{document}

Wahrscheinlichkeitsbaum

Antwort2

Hier ist eine alternative Möglichkeit, diesen Baum zu zeichnen, mit demistgamePaket:

Bildbeschreibung hier eingeben

\documentclass{standalone}

\usepackage{istgame}

\begin{document}   

\begin{istgame}[scale=1.5,font=\footnotesize]
\xtdistance{15mm}{43mm}
\istroot(0)
  \istb{\frac12}[al]
  \istb{\frac12}[ar]
  \endist
\xtdistance{15mm}{20mm}
\istroot(1)(0-1)<180>{$V=1$}
  \istb{p}[l]
  \istb{1-p}[r]
  \endist
\istroot(2)(0-2)<0>{$V=-1$}
  \istb{p}[l]
  \istb{1-p}[r]
  \endist
\xtdistance{15mm}{10mm}
\istroot(a)(1-1)<180>{$s_1=H$}
  \istb{q}[l]{s_2=U}
  \istb{1-q}[r]{s_2=D}
  \endist
\istroot(b)(1-2)<0>{$s_1=H$}
  \istb{q}[l]{s_2=U}
  \istb{1-q}[r]{s_2=D}
  \endist
\istroot(c)(2-1)<180>{$s_1=L$}
  \istb{1-q}[l]{s_2=U}
  \istb{q}[r]{s_2=D}
  \endist
\istroot(d)(2-2)<0>{$s_1=L$}
  \istb{1-q}[l]{s_2=U}
  \istb{q}[r]{s_2=D}
  \endist
\end{istgame}    

\end{document}

verwandte Informationen