[Tex/LaTex] How to draw a graph along with probabilty tables using TikZ package

graphicsgraphstablestikz-pgf

I need help to draw a Directed Acyclic Graph of Bayesian Network along with probability of each node using TikZ package. Here is an example:

enter image description here

Table grid can be a full or partial as in above example.

Best Answer

I am in a good mood to provide you with a minimal working example (MWE) but in future questions, I advise you to provide one. (Summer break for teachers here :) See for instance: "Just do it for me" text building block

Here it is in tikz. You can do the rest for practice. See the pgfmanual. You can also type texdoc pgf in your terminal or command line.

Here are some explanations.

  • Usually, node commands are done as follows: \node (<node name>) [<options>] {<text>};
  • Edit: The positioning library enables you to specify how far the location of nodes/coordinates relative to other nodes/coordinates. Reference: Package PGF Math Error: Unknown operator `o' or `of'
  • The arrows library allows you to use other types of arrows aside from the default of >.
  • The shapes library allows usage of shapes like ellipse for nodes.

The partial code

\documentclass[border=5, convert={density=150}]{standalone}

\usepackage{array}
\newcolumntype{C}{>{$}c<{$}}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows}
\tikzset{
    events/.style={ellipse, draw, align=center},
}
\begin{document}
\begin{tikzpicture}[node distance=2cm, >=stealth']
\node [events] (cloudy) {Cloudy};
\node [events, below left = of cloudy] (sprinkler) {Sprinkler};
\node [events, below right = of cloudy] (rain) {Rain};
\node [events, below right = of sprinkler] (wetgrass) {WetGrass};

\draw [->] (cloudy) -- (sprinkler);
\draw [->] (cloudy) -- (rain);
\draw [->] (rain) -- (wetgrass);
\draw [->] (sprinkler) -- (wetgrass);

\node [above = of cloudy] {
    \begin{tabular}{CC}
    \mathrm{P(C=F)} & \mathrm{P(C=T)}\\
    \hline
    0.5 & 0.5\\
    \end{tabular}
    };

\node [right = of rain, anchor=west] {
    \begin{tabular}{c|CC}
    C & \mathrm{P(C=F)} & \mathrm{P(C=T)}\\
    \hline
    F & 0.8 & 0.2\\
    T & 0.2 & 0.8\\
    \end{tabular}
    };  

\end{tikzpicture}
\end{document}

The output

enter image description here