Markov random fields
Bayesian networks are a class of models that can compactly represent many interesting probability distributions. However, we have seen in the previous chapter that some distributions may have independence assumptions that cannot be perfectly represented by the structure of a Bayesian network.
In such cases, unless we want to introduce false independencies among the variables of our model, we must fall back to a less compact representation (which can be viewed as a graph with additional, unnecessary edges). This leads to extra, unnecessary parameters in the model, and makes it more difficult to learn these parameters and to make predictions.
There exists, however, another technique for compactly representing and visualizing a probability distribution that is based on the language of undirected graphs. This class of models (known as Markov Random Fields or MRFs) can compactly represent independence assumptions that directed models cannot represent. We will explore the advantages and drawbacks of these methods in this chapter.
Markov Random Fields
Undirected graphical representation of a joint probability of voting preferences over four individuals. The figure on the right illustrates the pairwise factors present in the model. As a motivating example, suppose that we are modeling voting preferences among persons . Let’s say that , , , and are friends, and friends tend to have similar voting preferences. These influences can be naturally represented by an undirected graph.
One way to define a probability over the joint voting decision of is to assign scores to each assignment to these variables and then define a probability as a normalized score. A score can be any function, but in our case, we will define it to be of the form
where is a factor that assigns more weight to consistent votes among friends , e.g.:
The factors in the unnormalized distribution are often referred to as factors. The final probability is then defined as
where is a normalizing constant that ensures that the distribution sums to one.
When normalized, we can view as an interaction that pushes ’s vote closer to that of . The term pushes ’s vote closer to , and the most likely vote will require reconciling these conflicting influences.
Note that unlike in the directed case, we are not saying anything about how one variable is generated from another set of variables (as a conditional probability distribution would do). We simply indicate a level of coupling between dependent variables in the graph. In a sense, this requires less prior knowledge, as we no longer have to specify a full generative story of how the vote of is constructed from the vote of (which we would need to do if we had a factor). Instead, we simply identify dependent variables and define the strength of their interactions; this in turn defines an energy landscape over the space of possible assignments and we convert this energy to a probability via the normalization constant.
A Markov Random Field (MRF) is a probability distribution over variables defined by an undirected graph in which nodes correspond to variables . The probability has the form
where denotes the set of cliques (i.e. fully connected subgraphs) of , and each factor is a nonegative function over the variables in a clique. The partition function
is a normalizing constant that ensures that the distribution sums to one.
Thus, given a graph , our probability distribution may contain factors whose scope is any clique in , which can be a single node, an edge, a triangle, etc. Note that we do not need to specify a factor for each clique. In our above example, we defined a factor over each edge (which is a clique of two nodes). However, we chose not to specify any unary factors, i.e. cliques over single nodes.
Comparison to Bayesian networks
Examples of directed models for our four-variable voting example. None of them can accurately express our prior knowledge about the dependency structure among the variables. In our earlier voting example, we had a distribution over that satisfied and (because only friends directly influence a person’s vote). We can easily check by counter-example that these independencies cannot be perfectly represented by a Bayesian network. However, the MRF turns out to be a perfect map for this distribution.
More generally, MRFs have several advantages over directed models:
- They can be applied to a wider range of problems in which there is no natural directionality associated with variable dependencies.
- Undirected graphs can succinctly express certain dependencies that Bayesian nets cannot easily describe (although the converse is also true)
They also possess several important drawbacks:
- Computing the normalization constant requires summing over a potentially exponential number of assignments. We will see that in the general case, this will be NP-hard; thus many undirected models will be intractable and will require approximation techniques.
- Undirected models may be difficult to interpret.
- It is much easier to generate data from a Bayesian network, which is important in some applications.
It is not hard to see that Bayesian networks are a special case of MRFs with a very specific type of clique factor (one that corresponds to a conditional probability distribution and implies a directed acyclic structure in the graph), and a normalizing constant of one. In particular, if we take a directed graph and add side edges to all parents of a given node (and removing their directionality), then the CPDs (seen as factors over a variable and its ancestors) factorize over the resulting undirected graph. The resulting process is called moralization.
Thus, MRFs have more power than Bayesian networks, but are more difficult to deal with computationally. A general rule of thumb is to use Bayesian networks whenever possible, and only switch to MRFs if there is no natural way to model the problem with a directed graph (like in our voting example).
Independencies in Markov Random Fields
Recall that in the case of Bayesian networks, we defined a set of independencies that were described by a directed graph , and showed how these describe true independencies that must hold in a distribution that factorizes over the directed graph, i.e. .
In an MRF, a node is independent from the rest of the graph given its neighbors (which are referred to at the Markov blanket of . What independencies can be then described by an undirected MRF? The answer here is very simple and intuitive: variables are dependent if they are connected by a path of unobserved variables. However, if ’s neighbors are all observed, then is independent of all the other variables, since they influence only via its neighbors.
In particular, if a set of observed variables forms a cut-set between two halves of the graph, then variables in one half are independent from ones in the other.
Formally, we define the Markov blanket of a variable as the minimal set of nodes such that is independent from the rest of the graph if is observed, i.e. . This notion holds for both directed and undirected models, but in the undirected case the Markov blanket turns out to simply equal a node’s neighborhood.
In the directed case, we found that , but there were distributions whose independencies could not be described by . In the undirected case, the same holds. For example, consider a probability described by a directed v-structure (i.e. the explaining away phenomenon). The undirected model cannot describe the independence assumption .
Conditional Random Fields
An important special case of Markov Random Fields arises when they are applied to model a conditional probability distribution , where and are vector-valued variables. These distributions are common in supervised learning settings in which we are given and want to predict . This setting is also known as structured prediction.
As a motivating example, consider the problem of recognizing a word from a sequence of black-and-white character images given to us in the form of pixel matrices. The output of our predictor is a sequence of alphabet letters .
We could in principle train a classifier to separately predict each from its . However, since the letters together form a word, the predictions across different ought to inform each other. In the above example, the second letter by itself could be either a ‘U’ or a ‘V’; however, since we can tell with high confidence that its neighbors are ‘Q’ and ‘E’, we can infer that ‘U’ is the most likely true label. CRFs enable us to perform this prediction jointly.
Formally, a CRF is a Markov network over variables which specifies a conditional distribution
with partition function
Note that in this case, the partition constant now depends on (therefore, we say that it is a function), which is not surprising: is a probability over that is parametrized by , i.e. it encodes a different probability function for each . In that sense, a conditional random field results in an instantiation of a new Markov Random Field for each input .
More formally, suppose is a chain CRF with two types of factors: image factors for — which assign higher values to that are consistent with an input — as well as pairwise factors for . We may also think of the as probabilities given by, say, standard (unstructured) softmax regression; the can be seen as empirical frequencies of letter co-occurrences obtained from a large corpus of English text (e.g. Wikipedia).
Given a model of this form, we can jointly infer the structured label using MAP inference:
In most practical applications, we further assume that the factors are of the form
where can be an arbitrary set of features describing the compatibility between and .
In our OCR example, we may introduce features that encode the compatibility of the letter with the pixels . For example, may be the probability of letter produced by logistic regression (or a deep neural network) evaluated on pixels . In addition, we introduce features between adjacent letters. These may be indicators of the form , where are two letters of the alphabet. The CRF would then learn weights that would assign more weight to more common probability of consecutive letters , while at the same time making sure that the predicted are consistent with the input ; this process would let us determine in cases where is ambiguous, like in our above example.
The most important realization about CRF features is that they can be arbitrarily complex. In fact, we may define an OCR model with factors , that depend on the entire input . This does not affect computational performance at all, because at inference time, the is always observed, and our decoding problem involves maximizing
where . Using global features only changes the values of the factors, but not their scope, which possesses the same type of chain structure. We will see in the next section that this structure is all that is needed to ensure we can solve this optimization problem tractably.
This observation may be interpreted in a slightly more general form. If we were to model using an MRF (viewed as a single model over with normalizing constant ), then we need to fit two distributions to the data: and . However, if all we are interested in is predicting given , then modeling is unnecessary. In fact, it may be disadvantageous to do so statistically (e.g. we may not have enough data to fit both and ; since the models have shared parameters, fitting one may not result in the best parameters for the other) and it may not be a good idea computationally (we need to make simplifying assumptions in the distribution so that can be handled tractably). CRFs forgo this assumption, and often perform better on prediction tasks.
It is often useful to view MRFs in a way where factors and variables are explicit and separate in the representation. A factor graph is one such way to do this. A factor graph is a bipartite graph where one group is the variables in the distribution being modeled, and the other group is the factors defined on these variables. Edges go between factors and variables that those factors depend on.
This view allows us to more readily see the factor dependencies between variables, and later we’ll see it allows us to compute some probability distributions more easily.