17

The following code should produce a three step probability tree, but fail to compile. Does anybody have an idea why it doesn't work?

\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}
1
  • Welcome! Note that \tikzstyle is deprecated, but looks like a good first question ;).
    – cfr
    Jun 11, 2016 at 21:01

2 Answers 2

19

It just doesn't like all the blank lines. When I remove those, your code compiles fine. I've also updated to remove the deprecated use of \tikzstyle, but that's not required for compilation.

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

probability tree

1

Here is an alternative way of drawing this tree by using the istgame package:

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .