\documentclass{article}

\usepackage{hyperref}
\usepackage{setspace}
\usepackage{fancyvrb}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}

\begin{document}

\DefineVerbatimEnvironment{pflcodeve}{Verbatim} {xleftmargin=3.0em,fontsize=\small}

\newenvironment{pflcode}
  {\VerbatimEnvironment \setstretch{0.8} \begin{pflcodeve}}
  {\end{pflcodeve} }

\newcommand{\true}             {\mathtt{t}}
\newcommand{\false}            {\mathtt{f}}
\newcommand{\pathsep}          { $\triangleright$ }
\newcommand{\tableline}        {\noalign{\hrule height 0.8pt}}
\newcommand{\optionsection}[1] {\subsection*{\texttt{#1}}}

\tikzstyle{nodestyle}   = [draw, thick, circle, minimum size=0.9cm]
\tikzstyle{bnedgestyle} = [-triangle 45,thick]

\setlength{\parskip}{\baselineskip}

\title{\Huge\textbf{Prolog Factor Language (PFL) Manual}}

\author{Tiago Gomes\\\texttt{tiago.avv@gmail.com} \and V\'{i}tor Santos Costa\\\texttt{vsc@fc.up.pt}\\\\
CRACS \& INESC TEC, Faculty of Sciences, University of Porto
}


\date{}

\maketitle
\thispagestyle{empty}
\vspace{5cm}
\begin{center}
  \large Last revision: January 11, 2013
\end{center}
\newpage



%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
\section{Introduction}
The Prolog Factor Language (PFL) is a language that extends Prolog for providing a syntax to describe first-order probabilistic graphical models. These models can be either directed (bayesian networks) or undirected (markov networks). This language replaces the old one known as CLP($\mathcal{BN}$).

The package also includes implementations for a set of well-known inference algorithms for solving probabilistic queries on these models. Both ground and lifted inference methods are support.



%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
\section{Installation}
PFL is included with the \href{http://www.dcc.fc.up.pt/~vsc/Yap/}{YAP} Prolog system. However, there isn't yet a stable release of YAP that includes PFL. So you will need to install a development version of YAP. To to so, you must have installed the \href{http://git-scm.com/}{Git} version control system. The commands to perform a default installation of YAP in your home directory in a Unix-based environment are shown next.

\begin{enumerate}
 \setlength\itemindent{-0.01cm}
 \item \texttt{\$ cd \$HOME}
 \item \texttt{\$ git clone git://yap.git.sourceforge.net/gitroot/yap/yap-6.3}
 \item \texttt{\$ cd yap-6.3/}
 \item \texttt{\$ ./configure --enable-clpbn-bp --prefix=\$HOME}
 \item \texttt{\$ make depend \& make install}
\end{enumerate}

In case you want to install YAP somewhere else or with different settings, please consult the YAP documentation. From now on, we will assume that the directory \texttt{\$HOME\pathsep bin} (where the binary is) is in your \texttt{\$PATH} environment variable.

\label{examples-directory}
Once in a while, we will refer to the PFL examples directory. In a default installation, this directory will be located at \texttt{\$HOME\pathsep share\pathsep doc\pathsep Yap\pathsep packages\pathsep examples\pathsep CLPBN}.



%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
\section{Language}
A first-order probabilistic graphical model is described using parametric factors, commonly known as parfactors. The PFL syntax for a parfactor is

$$Type~~F~~;~~Phi~~;~~C.$$

, where
\begin{itemize}
\item $Type$ refers the type of network over which the parfactor is defined. It can be \texttt{bayes} for directed networks, or \texttt{markov} for undirected ones.

\item $F$ is a comma-separated sequence of Prolog terms that will define sets of random variables under the constraint $C$. If $Type$ is \texttt{bayes}, the first term defines the node while the remaining terms define its parents.

\item $Phi$ is either a Prolog list of potential values or a Prolog goal that unifies with one. Notice that if $Type$ is \texttt{bayes}, this will correspond to the conditional probability table. Domain combinations are implicitly assumed in ascending order, with the first term being the 'most significant' (e.g. $\mathtt{x_0y_0}$, $\mathtt{x_0y_1}$, $\mathtt{x_0y_2}$, $\mathtt{x_1y_0}$, $\mathtt{x_1y_1}$, $\mathtt{x_1y_2}$).

\item $C$ is a (possibly empty) list of Prolog goals that will instantiate the logical variables that appear in $F$, that is, the successful substitutions for the goals in $C$ will be the valid values for the logical variables. This allows the constraint to be defined as any relation (set of tuples) over the logical variables.
\end{itemize}


\begin{figure}[t!]
\begin{center}
\begin{tikzpicture}[>=latex',line join=bevel,transform shape,scale=0.8]

\node (cloudy)    at (50bp,  122bp) [nodestyle,ellipse,inner sep=0pt,minimum width=2.7cm] {$Cloudy$};
\node (sprinkler) at ( 0bp,  66bp)  [nodestyle,ellipse,inner sep=0pt,minimum width=2.7cm] {$Sprinkler$};
\node (rain)      at (100bp, 66bp)  [nodestyle,ellipse,inner sep=0pt,minimum width=2.7cm] {$Rain$};
\node (wetgrass)  at (50bp,  10bp)  [nodestyle,ellipse,inner sep=0pt,minimum width=2.7cm] {$WetGrass$};
\draw [bnedgestyle] (cloudy)    -- (sprinkler);
\draw [bnedgestyle] (cloudy)    -- (rain);
\draw [bnedgestyle] (sprinkler) -- (wetgrass);
\draw [bnedgestyle] (rain)      -- (wetgrass);

\node [above=0.4cm of cloudy,inner sep=0pt] {
\begin{tabular}[b]{lc}
  $C$      & $P(C)$ \\ \tableline
  $\true$  & 0.5 \\
  $\false$ & 0.5 \\
\end{tabular}
};

\node [left=0.4cm of sprinkler,inner sep=0pt] {
\begin{tabular}{lcc}
  $S$      & $C$      & $P(S|C)$ \\ \tableline
  $\true$  & $\true$  & 0.1 \\
  $\true$  & $\false$ & 0.5 \\
  $\false$ & $\true$  & 0.9 \\
  $\false$ & $\false$ & 0.5 \\
\end{tabular}
};

\node [right=0.4cm of rain,inner sep=0pt] {
\begin{tabular}{llc}
  $R$      & $C$      & $P(R|C)$ \\ \tableline
  $\true$  & $\true$  & 0.8 \\
  $\true$  & $\false$ & 0.2 \\
  $\false$ & $\true$  & 0.2 \\
  $\false$ & $\false$ & 0.8 \\
\end{tabular}
};

\node [below=0.4cm of wetgrass,inner sep=0pt] {
\begin{tabular}{llll}
  $W$      &  $S$     & $R$      & $P(W|S,R)$ \\ \tableline
  $\true$  & $\true$  & $\true$  & \hspace{1em} 0.99 \\
  $\true$  & $\true$  & $\false$ & \hspace{1em} 0.9  \\
  $\true$  & $\false$ & $\true$  & \hspace{1em} 0.9  \\
  $\true$  & $\false$ & $\false$ & \hspace{1em} 0.0  \\
  $\false$ & $\true$  & $\true$  & \hspace{1em} 0.01 \\
  $\false$ & $\true$  & $\false$ & \hspace{1em} 0.1  \\
  $\false$ & $\false$ & $\true$  & \hspace{1em} 0.1  \\
  $\false$ & $\false$ & $\false$ & \hspace{1em} 1.0  \\
\end{tabular}
};

\end{tikzpicture}
\caption{The sprinkler network.}
\label{fig:sprinkler-bn}
\end{center}
\end{figure}

Towards a better understanding of the language, next we show the PFL representation for the network found in Figure~\ref{fig:sprinkler-bn}.

\begin{pflcode}
:- use_module(library(pfl)).

bayes cloudy ; cloudy_table ; [].

bayes sprinkler, cloudy ; sprinkler_table ; [].

bayes rain, cloudy ; rain_table ; [].

bayes wet_grass, sprinkler, rain ; wet_grass_table ; [].

cloudy_table(
    [ 0.5,
      0.5 ]).

sprinkler_table(
    [ 0.1, 0.5,
      0.9, 0.5 ]).

rain_table(
    [ 0.8, 0.2,
      0.2, 0.8 ]).

wet_grass_table(
    [ 0.99, 0.9, 0.9, 0.0,
      0.01, 0.1, 0.1, 1.0 ]).
\end{pflcode}

In the example, we started by loading the PFL library, then we have defined one factor for each node, and finally we have specified the probabilities for each conditional probability table.

Notice that this network is fully grounded, as all constraints are empty. Next we present the PFL representation for a well-known markov logic network - the social network model. For convenience, the two main weighted formulas of this model are shown below.

\begin{pflcode}
1.5 : Smokes(x) => Cancer(x)
1.1 : Smokes(x) ^ Friends(x,y) => Smokes(y)
\end{pflcode}

Next, we show the PFL representation for this model.

\begin{pflcode}
:- use_module(library(pfl)).

person(anna).
person(bob).

markov smokes(X), cancer(X) ;
    [4.482, 4.482, 1.0, 4.482] ;
    [person(X)].

markov friends(X,Y), smokes(X), smokes(Y) ;
    [3.004, 3.004, 3.004, 3.004, 3.004, 1.0, 1.0, 3.004] ;
    [person(X), person(Y)].
\end{pflcode}
%markov smokes(X) ; [1.0, 4.055]; [person(X)].
%markov cancer(X) ; [1.0, 9.974]; [person(X)].
%markov friends(X,Y) ; [1.0, 99.484] ; [person(X), person(Y)].

Notice that we have defined the world to be consisted of only two persons, \texttt{anna} and \texttt{bob}. We can easily add as many persons as we want by inserting in the program a fact like \texttt{person @ 10.}~. This would automatically create ten persons named \texttt{p1}, \texttt{p2}, \dots, \texttt{p10}.

Unlike other fist-order probabilistic languages, in PFL the logical variables that appear in the terms are not directly typed, and they will be only constrained by the goals that appears in the constraint of the parfactor. This allows the logical variables to be constrained to any relation (set of tuples), and not only pairwise (in)equalities. For instance, the next example defines a network with three ground factors, each defined respectively over the random variables \texttt{p(a,b)}, \texttt{p(b,d)} and \texttt{p(d,e)}.

\begin{pflcode}
constraint(a,b).
constraint(b,d).
constraint(d,e).

markov p(A,B); some_table; [constraint(A,B)].
\end{pflcode}

We can easily add static evidence to PFL programs by inserting a fact with the same functor and arguments as the random variable, plus one extra argument with the observed state or value. For instance, suppose that we know that \texttt{anna} and \texttt{bob} are friends. We can add this knowledge to the program with the following fact: \texttt{friends(anna,bob,t).}~.

One last note for the domain of the random variables. By default, all terms instantiate boolean (\texttt{t}/\texttt{f}) random variables. It is possible to choose a different domain for a term by appending a list of its possible values or states. Next we present a self-explanatory example of how this can be done.

\begin{pflcode}
bayes professor_ability::[high, medium, low] ; [0.5, 0.4, 0.1].
\end{pflcode}

More probabilistic models defined using PFL can be found in the examples directory.



%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
\section{Querying}
In this section we demonstrate how to use PFL to solve probabilistic queries. We will use the sprinkler network as example.

Assuming that the current directory is the one where the examples are located, first we load the model with the following command.

\texttt{\$ yap -l sprinkler.pfl}

Let's suppose that we want to estimate the marginal probability for the $WetGrass$ random variable. To do so, we call the following goal.

\texttt{?- wet\_grass(X).}

The output of this goal will show the marginal probability for each $WetGrass$ possible state or value, that is, \texttt{t} and \texttt{f}. Notice that in PFL a random variable is identified by a term with the same functor and arguments plus one extra argument.

Now let's suppose that we want to estimate the probability for the same random variable, but this time we have evidence that it had rained in the day before. We can estimate this probability without resorting to static evidence with:

\texttt{?- wet\_grass(X), rain(t).}

PFL also supports calculating joint probability distributions. For instance, we can obtain the joint probability for $Sprinkler$ and $Rain$ with:

\texttt{?- sprinkler(X), rain(Y).}



%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
\section{Inference Options}
PFL supports both ground and lifted inference methods. The inference algorithm can be chosen by calling \texttt{set\_solver/1}. The following are supported:
\begin{itemize}
 \item \texttt{ve},  variable elimination (written in Prolog)
 \item \texttt{hve}, variable elimination (written in C++)
 \item \texttt{jt},  junction tree
 \item \texttt{bdd}, binary decision diagrams
 \item \texttt{bp},  belief propagation
 \item \texttt{cbp}, counting belief propagation
 \item \texttt{gibbs}, gibbs sampling
 \item \texttt{lve}, generalized counting first-order variable elimination (GC-FOVE)
 \item \texttt{lkc}, lifted first-order knowledge compilation
 \item \texttt{lbp}, lifted first-order belief propagation
\end{itemize}

For instance, if we want to use belief propagation to solve some probabilistic query, we need to call first:

\texttt{?- set\_solver(bp).}

It is possible to tweak some parameters of PFL through \texttt{set\_pfl\_flag/2} predicate. The first argument is a option name that identifies the parameter that we want to tweak. The second argument is some possible value for this option. Next we explain the available options in detail.

\optionsection{verbosity}
This option controls the level of debugging information that will be shown.

\begin{itemize}
  \item Values: a positive integer (default is 0 - no debugging). The higher the number, the more information that will be shown.
  \item Affects: \texttt{hve}, \texttt{bp}, \texttt{cbp}, \texttt{lve}, \texttt{lkc} and \texttt{lbp}.
\end{itemize}

For instance, we can view some basic debugging information by calling the following goal.

\texttt{?- set\_pfl\_flag(verbosity, 1).}


\optionsection{use\_logarithms}
This option controls whether the calculations performed during inference should be done in a logarithm domain or not.
\begin{itemize}
  \item Values: \texttt{true} (default) or \texttt{false}.
  \item Affects: \texttt{hve}, \texttt{bp}, \texttt{cbp}, \texttt{lve}, \texttt{lkc} and \texttt{lbp}.
\end{itemize}


\optionsection{hve\_elim\_heuristic}
This option allows to choose which elimination heuristic will be used by the \texttt{hve}.
\begin{itemize}
  \item Values: \texttt{sequential}, \texttt{min\_neighbors}, \texttt{min\_weight}, \texttt{min\_fill} and\\ \texttt{weighted\_min\_fill} (default).
  \item Affects: \texttt{hve}.
\end{itemize}

An explanation for each of these heuristics can be found in Daphne Koller's book \textit{Probabilistic Graphical Models}.


\optionsection{bp\_max\_iter}
This option establishes a maximum number of iterations. One iteration consists in sending all possible messages.

\begin{itemize}
  \item Values: a positive integer (default is \texttt{1000}).
  \item Affects: \texttt{bp}, \texttt{cbp} and \texttt{lbp}.
\end{itemize}


\optionsection{bp\_accuracy}
This option allows to control when the message passing should cease. Be the residual of one message the difference (according some metric) between the one sent in the current iteration and the one sent in the previous. If the highest residual is lesser than the given value, the message passing is stopped and the probabilities are calculated using the last messages that were sent.

\begin{itemize}
  \item Values: a float-point number (default is \texttt{0.0001}).
  \item Affects: \texttt{bp}, \texttt{cbp} and \texttt{lbp}.
\end{itemize}


\optionsection{bp\_msg\_schedule}
This option allows to control the message sending order.
\begin{itemize}
  \item Values:
\begin{itemize}
 \item \texttt{seq\_fixed} (default), at each iteration, all messages are sent with the same order.

 \item \texttt{seq\_random}, at each iteration, all messages are sent with a random order.

 \item \texttt{parallel}, at each iteration, all messages are calculated using only the values of the previous iteration.

 \item \texttt{max\_residual}, the next message to be sent is the one with maximum residual (as explained in the paper \textit{Residual Belief Propagation: Informed Scheduling for Asynchronous Message Passing}).
\end{itemize}
  \item Affects: \texttt{bp}, \texttt{cbp} and \texttt{lbp}.
\end{itemize}


\optionsection{export\_libdai}
This option allows exporting the current model to the \href{http://cs.ru.nl/~jorism/libDAI/doc/fileformats.html}{libDAI} file format.
\begin{itemize}
  \item Values: \texttt{true} or \texttt{false} (default).
  \item Affects: \texttt{hve}, \texttt{bp}, and \texttt{cbp}.
\end{itemize}


\optionsection{export\_uai}
This option allows exporting the current model to the \href{http://graphmod.ics.uci.edu/uai08/FileFormat}{UAI08} file format.
\begin{itemize}
  \item Values: \texttt{true} or \texttt{false} (default).
  \item Affects: \texttt{hve}, \texttt{bp}, and \texttt{cbp}.
\end{itemize}


\optionsection{export\_graphviz}
This option allows exporting the factor graph's structure into a format that can be parsed by \href{http://www.graphviz.org/}{Graphviz}.
\begin{itemize}
  \item Values: \texttt{true} or \texttt{false} (default).
  \item Affects: \texttt{hve}, \texttt{bp}, and \texttt{cbp}.
\end{itemize}

\optionsection{print\_fg}
This option allows to print a textual representation of the factor graph.
\begin{itemize}
  \item Values: \texttt{true} or \texttt{false} (default).
  \item Affects: \texttt{hve}, \texttt{bp}, and \texttt{cbp}.
\end{itemize}



%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
\section{Parameter Learning}
PFL is capable to learn the parameters for bayesian networks, through an implementation of the expectation-maximization algorithm.

Next we show an example of parameter learning for the sprinkler network.

\begin{pflcode}
:- [sprinkler.pfl].

:- use_module(library(clpbn/learning/em)).

data(t, t, t, t).
data(_, t, _, t).
data(t, t, f, f).
data(t, t, f, t).
data(t, _, _, t).
data(t, f, t, t).
data(t, t, f, t).
data(t, _, f, f).
data(t, t, f, f).
data(f, f, t, t).

main :-
    findall(X, scan_data(X), L),
    em(L, 0.01, 10, CPTs, LogLik),
    writeln(LogLik:CPTs).

scan_data([cloudy(C), sprinkler(S), rain(R), wet_grass(W)]) :-
    data(C, S, R, W).
\end{pflcode}

Parameter learning is done by calling the \texttt{em/5} predicate. Its arguments are the following.

\texttt{em(+Data, +MaxError, +MaxIters, -CPTs, -LogLik)}

Where,
\begin{itemize}
 \item \texttt{Data} is a list of samples for the distribution that we want to estimate. Each sample is a list of either observed random variables or unobserved random variables (denoted when its state value is not instantiated).
 \item \texttt{MaxError} is the maximum error allowed before stopping the EM loop.
 \item \texttt{MaxIters} is the maximum number of iterations for the EM loop.
 \item \texttt{CPTs} is a list with the estimated conditional probability tables.
 \item \texttt{LogLik} is the log-likelihood.
\end{itemize}


It is possible to choose the solver that will be used for the inference part during parameter learning with the \texttt{set\_em\_solver/1} predicate (defaults to \texttt{hve}). At the moment, only the following solvers support parameter learning: \texttt{ve}, \texttt{hve}, \texttt{bdd}, \texttt{bp} and \texttt{cbp}.

Inside the \texttt{learning} directory from the examples directory, one can find more examples of parameter learning.



%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
\section{External Interface}
This package also includes an external command for perform inference over probabilistic graphical models described in formats other than PFL. Currently two are support, the \href{http://cs.ru.nl/~jorism/libDAI/doc/fileformats.html}{libDAI file format}, and the \href{http://graphmod.ics.uci.edu/uai08/FileFormat}{UAI08 file format}.

This command's name is \texttt{hcli} and its usage is as follows.

\begin{verbatim}
   $ ./hcli [solver=hve|bp|cbp] [<OPTION>=<VALUE>]...
   <FILE> [<VAR>|<VAR>=<EVIDENCE>]...
\end{verbatim}

Let's assume that the current directory is the one where the examples are located. We can perform inference in any supported model by passing the file name where the model is defined as argument. Next, we show how to load a model with \texttt{hcli}.

\texttt{\$ ./hcli burglary-alarm.uai}

With the above command, the program will load the model and print the marginal probabilities for all defined random variables. We can view only the marginal probability for some variable with a identifier $X$, if we pass $X$ as an extra argument following the file name. For instance, the following command will output only the marginal probability for the variable with identifier $0$.

\texttt{\$ ./hcli burglary-alarm.uai 0}

If we give more than one variable identifier as argument, the program will output the joint probability for all the passed variables.

Evidence can be given as a pair containing a variable identifier and its observed state (index), separated by a '=`. For instance, we can introduce knowledge that some variable with identifier $0$ has evidence on its second state as follows.

\texttt{\$ ./hcli burglary-alarm.uai 0=1}

By default, all probability tasks are resolved using the \texttt{hve} solver. It is possible to choose another solver using the \texttt{solver} option as follows.

\texttt{\$ ./hcli solver=bp burglary-alarm.uai}

Notice that only the \texttt{hve}, \texttt{bp} and \texttt{cbp} solvers can be used with \texttt{hcli}.

The options that are available with the \texttt{set\_pfl\_flag/2} predicate can be used in \texttt{hcli} too. The syntax is a pair \texttt{<Option>=<Value>} before the model's file name.



%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
\section{Further Information}
Please check the paper \textit{Evaluating Inference Algorithms for the Prolog Factor Language} for further information.

Any question? Don't hesitate to contact us!

\end{document}
