如何在 LaTeX 中製作以下機率樹?
謝謝您的幫忙!
機率樹1:
機率樹2:
\forestset{my label/.style={if n=1{edge label={node [midway, above left, font=\tiny] {$#1$}},}{edge label={node [midway, below left, font=\tiny] {$#1$}},
}
}
}
\begin{forest}
for tree={
grow'=0,
math content,
parent anchor=east,
child anchor=west,
edge path={
\noexpand\path [\forestoption{edge}] (!u.parent anchor) node
[circle, fill, minimum size=2.5pt, inner sep=0pt,
anchor=center]
{} -- (.child anchor)\forestoption{edge label};
},
edge={->},
anchor=west,
l sep+=15mm,
s sep+=10mm,
}
%Left figure
[
[\textbf{\textit{p}}^{good}, my label={P(good\rvert club_1)},
tikz={\node (sa) [above=10mm of .north, anchor=base] {$S_A$};}]
[\textbf{\textit{p}}_{rough}^{bad}, my label={P(bad\rvert club_1)}]
]
[, phantom, l sep-=25mm
%Right figure
[
[\textbf{\textit{p}}^{good}, my label={P(good\rvert club_2)},
tikz={\node (sb) [anchor=base east] at (.west |- sa.base) {$S_B$};
\draw [thick] (!r |- sa.south) -- (.east |- sa.south);}]
[\textbf{\textit{p}}^{bad}_{rough}, my label={P(bad\rvert club_2)}]
]
]
\end{forest}
機率樹3:
答案1
這是最複雜的樹。您應該能夠修改它以產生更簡單的樹。如果您遇到困難,只需發布您擁有的程式碼並詢問如何解決您遇到的問題。
這使用了強大的forest
包,允許使用標準括號符號來繪製樹。我對之前問題的回答介紹forest
並解釋如何將樹變成括號規範。
\documentclass[tikz, border=10pt]{standalone}
\usepackage{forest,mathtools}
\usetikzlibrary{arrows.meta,positioning}% requires TikZ/PGF 3 - comment if you don't have arrows.meta
%\usetikzlibrary{positioning}% uncomment if you don't have arrows.meta
\begin{document}
\forestset{
my label/.style={
if n=1{
edge label={node [midway, above left, font=\tiny] {$#1$}},
}{
edge label={node [midway, below left, font=\tiny] {$#1$}},
}
}
}
\begin{forest}
for tree={
grow'=0,
math content,
parent anchor=east,
child anchor=west,
edge path={
\noexpand\path [\forestoption{edge}] (!u.parent anchor) node [circle, fill, minimum size=2.5pt, inner sep=0pt, anchor=center] {} -- (.child anchor)\forestoption{edge label};
},
edge={-{Stealth[]}},% requires TikZ/PGF version 3 - comment if you don't have arrows.meta
%edge={->},% uncomment this line if you don't have arrows.meta
anchor=west,
l sep+=15mm,
s sep+=5mm,
}
[
[p^{good}, my label={P[good\rvert club_1]}, tikz={\node (sa) [above=10mm of .north, anchor=base] {$S_A$};}
]
[p^{bad}, my label={P[bad\rvert club_1]}
[p^{bad}_{rough}, my label={\delta\cdot P[bad\rvert club_1]}
[, phantom, l sep-=25mm
[
[p^{good}, my label={P[good\rvert club_2]},
tikz={
\node (sb) [anchor=base east] at (.west |- sa.base) {$S_B$};
\draw [thick] (!r |- sa.south) -- (.east |- sa.south);
}
]
[p^{bad}_{rough}, my label={P[bad\rvert club_2]}
]
]
]
]
[p^{bad}_{sand}, my label={(1-\delta)\cdot P[bad\rvert club_1]}
]
]
]
\end{forest}
\end{document}
編輯
以下是對所有三棵樹的一些幫助:
\documentclass[tikz, border=10pt, multi]{standalone}
\usepackage{forest,mathtools}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\forestset{
my label/.style={
if n=1{
edge label={node [midway, above left, font=\tiny] {$#1$}},
}{
edge label={node [midway, below left, font=\tiny] {$#1$}},
}
}
}
\begin{forest}
for tree={
grow'=0,
math content,
parent anchor=east,
child anchor=west,
edge path={
\noexpand\path [\forestoption{edge}] (!u.parent anchor) node [circle, fill, minimum size=2.5pt, inner sep=0pt, anchor=center] {} -- (.child anchor)\forestoption{edge label};
},
edge={-{Stealth[]}},
anchor=west,
l sep+=15mm,
s sep+=5mm,
if level=1{tier=t}{}
}
[
[p^{bad}_{O.B.}, tikz={\node (sa) [above=10mm of .north, anchor=base east] {$unplayable$};}
]
[, phantom
[,
[p^{bad}_{rough},
tikz={
\node (sb) [anchor=base east] at (.center |- sa.base) {$playable$};
\draw [thick] (!r |- sa.south) -- (.east |- sa.south);
}
]
[p^{bad}_{trees}]
[p^{bad}_{sand}
]
]
]
[p^{bad}_{water}
]
]
\end{forest}
\begin{forest}
for tree={
grow'=0,
math content,
parent anchor=east,
child anchor=west,
edge path={
\noexpand\path [\forestoption{edge}] (!u.parent anchor) node [circle, fill, minimum size=2.5pt, inner sep=0pt, anchor=center] {} -- (.child anchor)\forestoption{edge label};
},
edge={-{Stealth[]}},
anchor=west,
l sep+=15mm,
s sep+=5mm,
if level=1{tier=t}{}
}
[
[p^{good}, my label={P[good\rvert club_1]}, tikz={\node (sa) [above=10mm of .north, anchor=base] {$S_A$};}
]
[, phantom
[,
[p^{good}, my label={P[good\rvert club_2]},
tikz={
\node (sb) [anchor=base east] at (.west |- sa.base) {$S_B$};
\draw [thick] (!r |- sa.south) -- (.east |- sa.south);
}
]
[,phantom]
[p^{bad}_{rough}, my label={P[bad\rvert club_2]}
]
]
]
[p^{bad}_{rough}, my label={P[bad\rvert club_1]}
]
]
\end{forest}
\begin{forest}
for tree={
grow'=0,
math content,
parent anchor=east,
child anchor=west,
edge path={
\noexpand\path [\forestoption{edge}] (!u.parent anchor) node [circle, fill, minimum size=2.5pt, inner sep=0pt, anchor=center] {} -- (.child anchor)\forestoption{edge label};
},
edge={-{Stealth[]}},
anchor=west,
l sep+=15mm,
s sep+=5mm,
}
[
[p^{good}, my label={P[good\rvert club_1]}, tikz={\node (sa) [above=10mm of .north, anchor=base] {$S_A$};}
]
[p^{bad}, my label={P[bad\rvert club_1]}
[p^{bad}_{rough}, my label={\delta\cdot P[bad\rvert club_1]}
[, phantom, l sep-=25mm
[
[p^{good}, my label={P[good\rvert club_2]},
tikz={
\node (sb) [anchor=base east] at (.west |- sa.base) {$S_B$};
\draw [thick] (!r |- sa.south) -- (.east |- sa.south);
}
]
[p^{bad}_{rough}, my label={P[bad\rvert club_2]}
]
]
]
]
[p^{bad}_{sand}, my label={(1-\delta)\cdot P[bad\rvert club_1]}
]
]
]
\end{forest}
\end{document}
請查看我上面鏈接的答案,以幫助理解括號符號。否則這看起來就像魔術一樣,你不可能適應;)。