# directed bayesian network and factor graphs

I have a directed bayesian given by the figure below. In the figure the circles are random variables and the shaded ones are observed. The rectangular nodes are constants representing the hyper parameters over the prior distributions.

I have now tried to prepare the corresponding factor graph for the problem, which I hope represents the same joint distribution as the product of the factors (denoted by the rectangular nodes). The random variables are denoted similarly using circles. This is shown in the image below. Can someone comment on whether I have done it correctly?

So, as far as I think, I have the following:

$$f_1 = P(\lambda) \\ f_2 = P(w|\lambda) \\ f_3 = P(y|w, \phi, x) \\ f_4 = P(\phi)$$

So, the product represents $P(\lambda) \times P(w|\lambda) \times P(y|w, \phi, x) \times P(\phi)$, which is the joint distribution specified by the original factor graph i.e.

$$f(\lambda, \phi, w) = f_1(\lambda) f_2(w, \lambda) f_3(w, \phi) f_4(\phi)$$

Also, now if I want to compute the marginal at $w$, can I write it as:

$$P(w) = \sum_{\lambda}f_2(w, \lambda)f_1(\lambda) \sum_{phi}f_3(w, \phi) f_4(\phi)$$

The idea being I compute this joint distribution and then I can marginalise by summing and product of factors somehow.