Markov random field and conditional random field are common models for undirected probabilistic graphical models.
In this blog post, I would like to discuss mathematically about the definitions, relationships, caveats, tricks, and applications of Markov random field and conditional random field.
Markov Random Field
Suppose we have a set of factors,
Φ={ϕ1(D1),ϕ2(D2),⋯,ϕk(Dk)}
where Di is a clique of the undirected graph, Di⊆{X1,X2,⋯,Xn}, and ⋃i=1kDi={X1,X2,⋯,Xn}.
We define the joint distribution P(X1,X2,⋯,Xn) as
P(X1,X2,⋯,Xn)=ZΦPΦ(X1,X2,⋯,Xn)
where PΦ(X1,X2,⋯,Xn) is the unnormalized measure and ZΦ is the partition function. Concretely,
PΦ(X1,X2,⋯,Xn)=i=1∏kϕi(Di)
and
ZΦ=X1,X2,⋯,Xn∑PΦ(X1,X2,⋯,Xn)
Note that by definition ZΦ is intractable to compute, even if Xi is a binary variable.
Conditional Random Field
Conditional random field as to Markov random field is just like the conditional probability distribution as to the joint distribution.
Suppose we have a set of factors and the random variables could be separated into two disjoint sets X and Y,
Φ=ϕ1(D1),ϕ2(D2),⋯,ϕk(Dk)
where Di is a clique of the undirected graph, Di⊆{X,Y}, and ⋃i=1kDi={X,Y}.
We define the conditional distribution P(Y∣X) as
P(Y∣X)=ZΦ(X)PΦ(X,Y)
where PΦ(X,Y) is the unnormalized measure which is the same as the one in Gibbs distribution and ZΦ(X) is a different partition function. Concretely,
PΦ(X,Y)=i=1∏kϕi(Di)
and
ZΦ(X)=Y∑PΦ(X,Y)
Note that computing ZΦ(X) is still intractable. However, if the number of variables in Y are small, and the number of possible values for Y are small, it is feasible to compute ZΦ(X). For example, X={X1,X2,⋯,X1080×3} could be the pixels of a $1080$P RGB image, Y={Y} is just the label of the image, and ∣Y∣=10, computing ZΦ(X) will be very fast.
In many discriminative tasks, conditional random field is favorable over Markov random field.
Exponential Form
We could change the form of the unnormalized measure PΦ(X) or PΦ(X,Y) to exponential form from the exponential family. For example, for PΦ(X), we have
Now, it just looks like a Boltzmann distribution (Gibbs distribution).
We call ψi(Di) the potential function for clique Di, and the energy of the state E(X), sometimes referred as the energy function, is defined as
E(X)=i=1∑kψi(Di)
Conditional Random Field and Conventional Classification Model
Suppose k=1, and D={X,Y}, we then have
PΦ(X,Y)=exp(f(X,Y))
So the conditional probability distribution P(Y∣X) is
P(Y∣X)=∑Yexp(f(X,Y))exp(f(X,Y))
Doesn’t this look familiar? It is just the softmax function used for conventional classification models, and f(X,Y) is just the logit for class Y with input X.
Choices for f or ϕ
The definition of Markov random field or conditional random field does not specify the form of f or ϕ. So this means it could be any function. However, in practice, indicator functions and weights are used, thus the number of parameters used for the model could be reduced. Concretely,
ϕi(Di)=exp(−j=1∑liwjgj(Di))
fi(Di)=−j=1∑liwjgj(Di)
where g is just a binary indicator function, and usually li=∣Di∣.
Intractable Partition Function
The intractable partition function in Markov random field and conditional random field is a headache. To make it tractable to compute, we usually have to pose some structure to it when we create the model.
So with the original form, we have to do k2n=2n2n additions, whereas using the refined structured form, we only have to do $4$ additions.
The structure we could pose to the partition function varies from problem to problem. However, the example above demonstrated that we could apply some structures to the random variables in the model so that computing the partition function might become feasible.
Inference
To find the optimal X∗ that has the largest P(X), computing the potential function ZΦ or ZΦ(X) is not necessary as it is just a constant value.
However, it is still very difficult to find the optimal X∗ especially when each variable in X is a discrete variable.
There are some optimization algorithms that approximately find the optimum when the energy function E(X) is of certain form. We will probably discuss some of them in the future.