[Tex/LaTex] Hierarchical statistical diagrams

diagramsstatisticstikz-pgf

I would like to create a diagram like the one below in LaTeX (probably with TikZ). This diagram and others can be found on this website.

It looks like it was made with LaTeX, but I am not entirely sure. How can I approach this problem? Does TikZ provide an easy notation or template for creating such diagrams?

enter image description here

Best Answer

Some of those diagrams, don't require the power of TikZ; for exmaple, using a standard array you can produce

\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}

\newcommand\Marrowdown{\rotatebox[origin=c]{-90}{$\longrightarrow$}}

\begin{document}

\[
\setlength\arraycolsep{12pt}
\begin{array}{lccc}
  \text{parameters} & \theta_{1} & \theta_{2} & \theta_{3} \\
  & \Marrowdown & \Marrowdown & \Marrowdown \\ 
  \text{observations} & y_{1} & y_{2} &  y_{3} 
\end{array}
\]

\end{document}

enter image description here

TikZ offers a number of possible alternatives; for example, a matrix of math nodes can be used to produce all those diagrams. A simple example:

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

\begin{document}

\begin{tikzpicture}
\matrix[matrix of math nodes,column sep=15pt,row sep=15pt] (mat)
{
  \theta_{1} & \theta_{2} & \theta_{3} \\
  y_{1} & y_{2} &  y_{3} \\ 
};
\foreach \Columna in {1,2,3}
  \draw[->,>=latex] (mat-1-\Columna) -- (mat-2-\Columna);
\node[anchor=east] at ([xshift=-20pt]mat-1-1) {parameters};
\node[anchor=east] at ([xshift=-20pt]mat-2-1) {observations};

\end{tikzpicture}

\end{document}

enter image description here

Another possibility using chains:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,chains}

\begin{document}

\begin{tikzpicture}
\begin{scope}[
  every node/.style={on chain,join},
  every join/.style={draw,->}
]
\begin{scope}[start chain=1 going below]
  \node {$\theta_{1}$};
  \node {$y_{1}$};
\end{scope}
\begin{scope}[xshift=1cm,start chain=2 going below]
  \node {$\theta_{2}$};
  \node {$y_{2}$};
\end{scope}
\begin{scope}[xshift=2cm,start chain=3 going below]
  \node {$\theta_{3}$};
  \node {$y_{3}$};
\end{scope}
\end{scope}
\node[anchor=east] at ([xshift=-20pt]1-1) {parameters};
\node[anchor=east] at ([xshift=-20pt]1-2) {observations};
\end{tikzpicture}

\end{document}

enter image description here

For trees, specially if they are complex, I'd suggest you the powerful forest package (it's built upon PGF/TikZ and was designed specifically to build trees):

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={
  edge={->,>=latex},
  parent anchor=south,
  child anchor=north,
  content format={\ensuremath{\forestoption{content}}},
  }
[{\mu,\sigma^{2}},name=level0
  [\theta_{1},name=level1
    [y_{1},name=level2]
  ]
  [\theta_{2}
    [y_{2}]
  ]
  [\cdots,edge={draw=none}
    [\cdots,edge={draw=none}]
  ]
  [\theta_{k}
    [y_{k}]
  ]
]
\foreach \Name/\Label in {level2/parameters,level1/observations,level0/model}
  \node[anchor=east] at ([xshift=-30pt]\Name) {\Label};   
\end{forest}

\end{document}

enter image description here

As you can see, there are several options, which one to choose depends on the complexity of the diagrams you intend to draw. What is important is to be consistent; I mean, for a single document the ideal would be to choose one tool and stick to it (to guarantee things as the same kind of arrow tips, same distance between nodes, etc.).