使用 TikZ 绘制概率树

使用 TikZ 绘制概率树

以下代码应该生成一个三步概率树,但编译失败。有人知道为什么它不起作用吗?

\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

它就是不喜欢所有的空白行。当我删除这些空白行时,您的代码可以正常编译。我还进行了更新以删除已弃用的\tikzstyle,但这对于编译来说不是必需的。

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

概率树

答案2

这是绘制这棵树的另一种方法,使用istgame包裹:

在此处输入图片描述

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

相关内容