latex 中的 n 周期二项式树/格子

latex 中的 n 周期二项式树/格子

我在 tex.exchange.com 上看到了很多关于如何在 latex 中构建 2 周期或 3 周期二项式树的示例。我尝试修改这些示例以构建一般的 n 周期二项式树,但没有成功。我正在寻找的是这样的:在此处输入图片描述

任何帮助都值得感激。提前致谢。


我尝试编辑以下代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
  matrix of nodes,
  minimum size=1cm,
  column sep=3.5cm,
  row sep=1cm,
]
{
      &   & F \\
      & C &   \\
  \$A &   & E \\
      & B &   \\
      &   & D \\
};
\draw[->] (tree-3-1) -- (tree-2-2) node [midway,above] {$P$};
\draw[->] (tree-3-1) -- (tree-4-2) node [midway,below] {$(1-p)$};
\draw[->] (tree-2-2) -- (tree-1-3) node [midway,above] {$P^2$};
\draw[->] (tree-2-2) -- (tree-3-3) node [midway,below] {$(1-p)p$};
\draw[->] (tree-4-2) -- (tree-3-3) node [midway,above] {$(1-p)p$};
\draw[->] (tree-4-2) -- (tree-5-3) node [midway,below] {$(1-p)^2$};
\end{tikzpicture}
\end{document}    

当我修改它时,我遇到了编译错误。所以这就是我所得到的。我在将“->”编辑为点时遇到了麻烦。

答案1

一种可能性是组织一个\ifnum节日。

\documentclass[tikz,border=3.14mm]{standalone} 
\usetikzlibrary{decorations.markings}
\newcommand{\mypow}[3][]{
\ifnum#3=0
\else
\ifnum#3=1
#2
\else
#2^{#3}
\fi
\fi
}
\begin{document}
\begin{tikzpicture}[scale=1.5,decoration={markings,
  mark=between positions 0 and 1 step 8pt
  with { \draw [fill] (0,0) circle [radius=1pt];}}] % from https://tex.stackexchange.com/a/52849/121799
\foreach \X in {0,...,5}
{\foreach \Y in {0,...,\X}
{
\pgfmathtruncatemacro{\Z}{\X-\Y}
\ifnum\X<3
\fill (\X,{\Y-\X/2}) coordinate (X-\X-\Y) circle (1pt);
\node[anchor=south] at (\X,{\Y-\X/2+0.1}){$\mypow{u}{\Y}\mypow{d}{\Z}S_0$}; 
\fi
\ifnum\X=4
\ifnum \Y=1
\coordinate (X-\X-\Y) at (\X,{\Y-\X/2});
\else
\ifnum \Y=2
\coordinate (X-\X-\Y) at (\X,{\Y-\X/2});
\else
\fill (\X,{\Y-\X/2}) coordinate (X-\X-\Y) circle (1pt);
\fi
\fi
\fi
\ifnum\X=5
\ifnum\Y=2
\coordinate (X-\X-\Y) at (\X,{\Y-\X/2});
\else
\fill (\X,{\Y-\X/2}) coordinate (X-\X-\Y) circle (1pt);
\fi
\pgfmathtruncatemacro{\XmY}{\X-\Y}
\ifcase\XmY
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{n}$}; 
\or
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{n-1}$}; 
\or
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{n-2}$}; 
\or
\relax
\or
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{1}$}; 
\or
\node[anchor=west] (nu-\XmY) at ({\X+0.1},{\Y-\X/2}){$\nu_{0}$}; 
\fi
\fi
\ifnum\X=3
\coordinate (X-\X-\Y) at (\X,{\Y-\X/2});
\fi
\ifnum\X=0
\else
\pgfmathtruncatemacro{\prevX}{\X-1}
\foreach \Z in {0,...,\prevX}
{\pgfmathtruncatemacro{\DiffYZ}{ifthenelse(\Y==\Z,1,0)+ifthenelse(\Y==\Z+1,1,0)}
\ifnum\DiffYZ>0
\ifnum\X<3
\draw (X-\prevX-\Z) -- (X-\X-\Y);
\fi
\ifnum\X=3
\draw[dashed,shorten >=4mm] (X-\prevX-\Z) -- (X-\X-\Y);
\fi
\ifnum\X=4
\ifnum\Y=1
\else
\draw[dashed,shorten <=4mm] (X-\prevX-\Z) -- (X-\X-\Y);
\fi
\fi
\ifnum\X=5
\ifnum\Z=1
\else
\ifnum\Y=2
\else
\draw (X-\prevX-\Z) -- (X-\X-\Y);
\fi
\fi
\fi
\fi}
\fi
}
}
%
\draw[decorate] (X-5-1) -- (X-5-3);
\draw[decorate] (X-3-0) -- (X-3-3);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容