Matlab-based table (discrete variable) factor class
The @factor class gives a useful implementation of table based functions over discrete random variables, used in probability distributions, energy functions, and other graphical models. Although originally independent and designed with Matlab in mind, it is similar in many ways to its counterpart in the C++ library libDAI. It supports basic operations common in probabilistic inference, including artihmetic operations, marginalization, optimization, sampling, etc., as well as support for reading and writing some common model formats. It is also designed to work in Octave, the free alternative to Matlab.
Installation: Simply download and extract the functions to a directory called @factor (the "@" symbol is how Matlab denotes an object class and its member functions). If you want additional performance, some of the functions can be compiled with Mex, Matlab's shared object format, but this is not required to use the code.
Help: You may find the following basic help functions useful:
We can create a basic factor object by specifying its variables and a table of values. The table size also specifies the cardinality of each variable; here has three values and has two, . Note that as is common in Matlab, values are 1-based rather than 0-based.
Variables are stored as unsigned integers (uint32s); you can extract the variables, their dimensions, and the table of values directly if needed:
Basic arithmetic (e.g., plus) is defined to produce a factor equivalent to the operator applied to the argument functions. If both factors have the same variables (arguments), the operation is elementwise; if they have different scopes, the operations produce a new function defined over their joint scope:
Basic operations are plus (+), minus (-), times (.*), rdivide (./). Operators mtimes (*) and mrdivide (/) are the same as times/rdivide, but have different behavior on zeros, so that (a/0=0) for all a; this is useful in many inference contexts.
Simple transformations of the functions are also available: exp, power (.^) (scalar elementwise power), log, log2, and log10.
Basic information about the factors themselves can be accessed via:
The most useful aspect of the factor class is to automate the tedious computations underlying many of the mathematical operators common in probabilistic graphical models. These include basic variable elimination operators, specifying the variables to be eliminated:
The closely related functions marginal, maxmarginal, and minmarginal eliminate all variables in the factor except those specified, to produce (unnormalized) marginal functions.
Functions that optimize over or draw samples from a distribution defined by the factor include