[Tex/LaTex] Plotting a Probability Tree with TikZ

tikz-pgf

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}

Best Answer

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