5.1 Naïve inference

This representation scheme immediately suggests an algorithm for inference—although in fact it is a bad one, as we shall see. Naïvely, we can represent the joint distribution simply as the product of all the conditionals, and then collapse over (marginalize out) all the variables not of interest:

1 # don't do this: compute the entire joint first
2 p_X1X2X3X4X5X6X7X8 = (
3 p_X1*p_X2*p_X3givenX1X2*p_X4givenX3*p_X5givenX3*
4 p_X6givenX4*p_X7givenX5*p_X8givenX6X7
5 )
6 assert p_X1X2X3X4X5X6X7X8.shape == (2, 2, 2, 2, 2, 2, 2, 2)
7
8 p_X8 = p_X1X2X3X4X5X6X7X8.sum(axis=(0,1,2,3,4,5,6), keepdims=True)
9 assert p_X8.shape == (1, 1, 1, 1, 1, 1, 1, 2)

Now think about how many operations were carried out by the single call to sum: In short, 282^{8} numbers (the tabular representation of the joint distribution) were reduced to 212^{1} numbers by addition, so 28−22^{8}-2 additions were carried out. In general, the costs of such a reduction will be11 1 This cost could surely be called 282^{8}, but for consistency with what follows, we will use the glass-half-full 272^{7}, the reduction across the first axis dominating all subsequent ones. 𝒪︀⁢(KN−1)\mathcal{O}({K}^{N-1}), with K{K} the number of states a single discrete random variable can take on, and NN the number of such variables—in a word, exponentially expensive in the number of variables. In a graph with hundreds of variables, such an approach to inference is infeasible.

a
b
Figure 5.1: A directed graph and its clique tree. (5.1a) A directed acyclic graph and (5.1b) its corresponding clique tree.