TikZ를 사용하여 확률 트리 그리기

TikZ를 사용하여 확률 트리 그리기

다음 코드는 3단계 확률 트리를 생성해야 하지만 컴파일에 실패합니다. 왜 작동하지 않는지 아는 사람이 있습니까?

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

관련 정보