Skip to content

Commit

Permalink
Added an example with new plate and gate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jluttine committed Sep 6, 2012
1 parent 1325ba8 commit 81cd260
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 94 deletions.
160 changes: 76 additions & 84 deletions example.tex
Original file line number Diff line number Diff line change
Expand Up @@ -56,93 +56,27 @@
\tikz{\node[factor] [label=$\mathcal{N}$] {}; }
\\
Plate &
\tikz{
%\node[latent] (x) {$x_m$};
%\node[fit=(x), label={[name=plate-label]right below:n in N}] (plate) {};
%\node[draw, fit=(plate)(plate-label)] {};
%\node[plate, fit=(x)] (plate) {n in N};
%\node[plates] {testi} ;
%\node[plate, fit=(x), label=n in N] {}; %{$\forall m \in \mathcal{M}$};
}
\end{tabular}
\end{center}
\end{table}

% A table of node types
\begin{table}[ht]
\caption{Node types}
\begin{center}
\begin{tabular}{llc}
Type & Command & Output
\\
\hline
Plate &
\texttt{\textbackslash plate} &
\tikz{
\node[latent] (x) {$x_m$};
\plate {} {(x)} {$\forall m \in \mathcal{M}$} {};
}
\\
Plate with freely located caption &
\texttt{\textbackslash shiftedplate} &
\tikz{
\node[latent] (x1) {$x_m$};
\shiftedplate {} {(x1)} {$\forall m \in \mathcal{M}$} {}
{yshift=1pt, above left = 2pt of x1.north east};
}
\\
Factor &
\texttt{\textbackslash node[factor]} &
\tikz{
% Nodes
\node[obs] (x) at (2,0.5) {$x$};
\node[latent] (mu) at (0,1) {$\mu$};
\node[latent] (tau) at (0,0) {$\tau$};
% Factors
%\node[factor] (f) [left=of x, label={[name=f-caption]above:$\mathcal{N}$}] {}; %
\node[factor] (f) [left=of x, label=$\mathcal{N}$] {}; %
% Draw arrows
\draw[<-] (x) -- (f) edge[-] (mu)
edge[-] (tau);
}
\\
Factor with no inputs nor outputs &
% What is the purpose of this command?
\texttt{\textbackslash nofactor} &
\tikz{
\nofactor {N} {} {$\mathcal{A}$} {above=1pt}
}
\\
Factor with one input &
\texttt{\textbackslash factor} &
\tikz{
\node[latent] (l) at (0,0) {$\lambda$};
\node[obs] (k) at (2,0) {$k$};
\factorexperimental {N} {(1,0)} {$\mathcal{P}$} {l} {k} {above}
}
\\
Factor with two inputs &
\texttt{\textbackslash twofactor} &
\tikz{
\node[latent] (mu) at (0,1) {$\mu$};
\node[latent] (tau) at (0,0) {$\tau$};
\node[obs] (x) at (2,0.5) {$x$};
\twofactor {N} {(1,0.5)} {$\mathcal{N}$} {mu} {tau} {x} {above=1pt}
\newplate[x-plate] {(x)} {$m \in \mathcal{M}$};
}
\\
Gate &
% TODO: Add the input to the gate!
\texttt{\textbackslash gate} &
&
\tikz{
% Nodes
\node[latent] (l) at (0,3) {$\lambda$};
\node[latent] (p) at (1.5,1.5) {$\phi$};
\node[obs] (k) at (0,0) {$k$};
\node[latent] (l) at (0,3) {$\lambda$}; %
\node[latent] (p) at (1.5,1.5) {$\phi$}; %
\node[obs] (k) at (0,0) {$k$}; %
% Factor
%\nofactor {kf} {above=1 of k} {Multivariate} {above=1pt};
\factorexperimental {k-factor} {(0,1.5)} {Multi} {p} {k} {above};
% \nofactor {kf} {above=1 of k} {Multivariate} {above=1pt};
\node[factor, label={[name=k-factor-caption]Multi}] (k-factor)
at (0,1.5) {}; %
% Gate
\newgate {k-gate} {(k-factor)(k-factor-caption)} {l};
\newgate[k-gate] {(k-factor)(k-factor-caption)} {l};
% Connections
\draw[<-] (k) -- (k-factor) edge[-] (p);
}
\end{tabular}
\end{center}
Expand All @@ -154,25 +88,83 @@
\begin{center}
\begin{tikzpicture}
% Define nodes
\node[const] (a) at (0,3) {$\alpha$};
\node[const] (a) at (0,3) {$\alpha$};
\node[latent] (w) at (0,2) {$\mathbf{w}_n$};
\node[latent] (x) at (2,2) {$\mathbf{x}_n$};
\node[latent] (t) at (4,0) {$\tau$};
\node[obs] (y) at (1,0) {$\mathbf{y}_n$};
\node[obs] (y) at (1,0) {$\mathbf{y}_n$};

% Connect the nodes
\draw[->] (a) -- (w);
\draw[->] (x) -- (y);
\draw[->] (w) -- (y);
\draw[->] (t) -- (y);
\draw[<-] (w) -- (a);
\draw[<-] (y) edge (x)
edge (w)
edge (t);

% Plates
\plate{N}{(x)(y)(w)}{$n=1,\ldots,N$}{}
\newplate {(x)(y)(w)} {$n=1,\ldots,N$}
\end{tikzpicture}
\end{center}
\caption{A simple Bayesian network.}
\end{figure}

% Latent Dirichlet allocation
\begin{figure}[ht]
\begin{center}
\begin{tikzpicture}[x=2cm,y=2cm]

% Nodes

\node[obs] (X) {$X$} ; %
\node[latent, above=of X] (T) {$T$} ; %
\node[latent, above=of T] (theta) {$\theta$}; %
\node[const, above=of theta] (atheta) {$\alpha_\theta$};


% Factors
\node[factor, above=of X, label={[name=X-factor-label]Multi}]
(X-factor) {}; %
\node[factor, above=of T,
label={[name=T-factor-label]left:Multi}] (T-factor) {}; %
\node[factor, above=of theta,
label={[name=theta-factor-label]left:Dir}] (theta-factor) {}; %

% More nodes
\node[latent, right=of X-factor] (phi) {$\phi$}; %
\node[const, above=of phi] (aphi) {$\alpha_\phi$}; %

\node[factor, above=of phi, label={[name=phi-factor-label]right:Dir}]
(phi-factor) {};

\draw[<-] (X) -- (X-factor) edge[-] (phi);
\draw[<-] (phi) -- (phi-factor) edge[-] (aphi);
\draw[<-] (T) -- (T-factor) edge[-] (theta);
\draw[<-] (theta) -- (theta-factor) edge[-] (atheta);

\newgate[X-gate] {(X-factor)(X-factor-label)} {T}

\newplate[plate1] {(X)(X-gate)(T)(T-factor)(T-factor-label)}
{$\forall 1 \leq i \leq n_d$}; %
\newplate {(plate1)(theta)(theta-factor)(theta-factor-label)}
{$\forall d \in \mathcal{D}$} ; %
\newplate {(phi)(phi-factor)(phi-factor-label)} {$\forall t \in
\mathcal{T}$} ; %

\end{tikzpicture}

\end{center}

\caption{Latent Dirichlet allocation as directed factor graph.}

\end{figure}

% Latent Dirichlet allocation
\begin{figure}[ht]
\begin{center}
\begin{tikzpicture}[x=2cm,y=2cm]
\end{tikzpicture}
\end{center}
\end{figure}


% Latent Dirichlet allocation
\begin{figure}[ht]
Expand Down
42 changes: 32 additions & 10 deletions tikzlibrarybayesnet.code.tex
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,56 @@

% Latent node
\tikzstyle{latent} = [circle,fill=white,draw=black,inner sep=1pt,
minimum size=20pt, font=\fontsize{10}{10}\selectfont]
minimum size=20pt, font=\fontsize{10}{10}\selectfont, node distance=1]
% Observed node
\tikzstyle{obs} = [latent,fill=gray!25]
% Constant node
\tikzstyle{const} = [rectangle, inner sep=0pt]
\tikzstyle{const} = [rectangle, inner sep=0pt, node distance=1]
% Factor node
\tikzstyle{factor} = [rectangle, fill=black,minimum size=5pt, inner
sep=0pt, node distance=15pt]
sep=0pt, node distance=0.4, font=\small]

%\tikzstyle{label} += [font=\footnotesize]

% Caption node?
%\tikzstyle{caption} = [node distance=2pt]

\tikzset{plates/.code={\text{Hello world!}}}

% Plate node
\tikzstyle{plate} = [draw, rectangle]
\tikzstyle{plate} = [draw, rectangle, rounded corners, fit=#1]
% Constant node
\tikzstyle{wrap} = [inner sep=0pt, fit=#1]
% Caption node
\tikzstyle{caption} = [font=\footnotesize] %
\tikzstyle{plate caption} = [caption, node distance=0, inner sep=0pt,
below left=5pt and 0pt of #1.south east] %
\tikzstyle{factor caption} = [caption] %
\tikzstyle{every label} += [caption] %

\tikzset{>={triangle 45}}

\pgfdeclarelayer{b}
\pgfdeclarelayer{f}
\pgfsetlayers{b,main,f}

% name, fitlist, caption
\newcommand{\newplate}[3][default]{ %
\node[wrap=#2] (#1-wrap) {}; %
\node[plate caption=#1-wrap] (#1-caption) {#3}; %
\node[plate=(#1-wrap)(#1-caption)] (#1) {}; %
}

% name, fitlist, input
\newcommand{\newgate}[3][default]{ %
\node[rectangle,draw,dashed, inner sep=2pt, fit=#2] (#1) {}; %
\draw [-*,thick] (#3) -- (#1); %
}

%\newcommand{\newfactor}[3][default]{ %
% \node[factor]
% }


% shapename, fitlist, caption, pos
\newcommand{\plate}[4]{ %
Expand Down Expand Up @@ -127,12 +155,6 @@
\node (#1) [rectangle,draw,dashed, inner sep=2pt, fit=#2]{};
}

% shapename, fitlist, input
\newcommand{\newgate}[3]{ %
\node (#1) [rectangle,draw,dashed, inner sep=2pt, fit=#2] {}; %
\draw [-*,thick] (#3) -- (#1); %
}

%shapename, fitlist1, fitlist2, caption1, caption2
\newcommand{\vertgate}[5]{ %
\begin{pgfonlayer}{b}
Expand Down

0 comments on commit 81cd260

Please sign in to comment.