# Calculating Conditional Probability Table (Migrated from community.research.microsoft.com) • ### Question

• xyc posted on 06-22-2009 10:00 PM

Hi,

I'm new to Infer.NET and I'm trying to understand how to use it by coding a sub-set of my bayesian network which is similar to the following model.

S                                   M
p(s) = 0.3                 p(m) = 0.6
\                           /
\                       /
\                   /
L     p(l|s,m) = 0.05
|      p(l|s,~m) = 0.1
|     p(l|~s,m) = 0.1
|     p(l|~s,~m) = 0.2
|
T   p(t|l) = 0.3
p(t|~l) = 0.8

I can create the model using the example given in a previous thread.  But, in my actual network, variables S and M each have 10 discrete states. This means variable L would have 100 rows in its conditional probability table (CPT).  I would like to calculate the CPT values as required using Kevin Murphy's formula which is suitable for my needs.

p(L|S,M) = [p(L|S)p(L|M)] / p(L)

This would mean I wouldn't have to code 100 rows of Variable.If() conditions, especially when this is just a sub-set of my actual model.  In my model variable L will be dependent on a few more other variables, each having more states, so there will be much more than 100 rows in the resulting CPT.

I've tried several different ways to code this in Infer.NET, none of which really gives me a full model.  In one attempt I broke the model into different sections: I code the top-half of the model first (S, M, L); calculate the CPT as required given the observations; then substitute this value into the bottom-half of the model (L, T).  It works if I only ever make inferences on the bottom variables (e.g. T).  But if T is my observed variable and I want to infer something at the top (e.g. M) this attempt will not handle it.

What would be the best approach for this?  Any assistant would be greatly appreciated.

Thanks,
xyc

Friday, June 3, 2011 4:55 PM

### Answers

• minka replied on 06-23-2009 10:51 AM

Hi xyc, it sounds like you don't really want S and M to be parents of L in the style of a Bayes net.  You want something more like a factor graph, where S is directly connected to L and so is M.  To attach a factor between S and L alone, you can condition on S and say Variable.ConstrainEqualRandom(L, Variable.Discrete(...)) or you can condition on L and say Variable.ConstrainEqualRandom(S, Variable.Discrete(...)).  Either way works.  Just make sure that L has a prior distribution, in addition to these constraints.

Alternatively, the formula you gave for p(L|S,M) can be interpreted as a Bayes net where L is the parent of S and M, so that's another way to encode it.  This approach seems cleanest for this problem. What you'd do is condition on the values of L, rather than S and M.  For each value of L, you can either generate S using S.SetTo(Variable.Discrete(...)) as in the previous thread, or you can attach a constraint using Variable.ConstrainEqualRandom(S, Variable.Discrete(...)).  Attaching a constraint is a bit more flexible as it allows S to have a separate prior distribution (in fact S must have a separate prior if you use ConstrainEqualRandom).

You can think of ConstrainEqualRandom as a way to attach dependencies between variables without the requirement of a parent-child relationship in the Bayes net sense.

Friday, June 3, 2011 4:55 PM