jluttine / junction-tree

The junction tree algorithm for (discrete) factor graphs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Observing variables in the middle of the graph.

dante-ga opened this issue · comments

Hello! Could you please describe how in general to run inference while observing an arbitary variable?
For example, how to change "cond_values" to calculate:
P( wet_grass = 1 | cloudy = 0) ?
P( sprinkler = 1 | rain = 0) ?

In short, observing just means that you choose one element along the axis that corresponds to that variable in all conditional probability tables.

For instance, observing cloudy=0 in the readme example, just set the probability of the other case to zero:

values = [
    np.array([0.5, 0.0]),
    ...
]

That is, the probability of cloudy=1 was now set to 0.

Another option is to create an entirely new graph where you remove the other "rows"/"columns" except the one that was observed. Then, you need to set the key size of cloudy to 1 and remove correct elements from each array in values. That graph has a bit smaller arrays so it can be a bit more efficient. But on the other hand, you need to re-run the entire junction tree algorithm from scratch for each different conditioning.

Similarly, for rain=0, set the probabilities of the other cases to zero:

values = [
    ...
    np.array(
        [
            [0.8, 0.0],
            [0.2, 0.0]
        ]
    ),
    ....
]

(Note that it is just coincidence here that 0.8 and 0.2 sum to 1. That's not what happens in general. In the previous example, it was just 0.5 so it didn't sum to 1. Just if you wonder.)

Again, you can also modify the key size to one and remove the corresponding elements from all relevant arrays in values.

Of course, these could be automated or provided a bit easier user interface. The library is quite low-level on purpose so that one can have good control over what exactly happens and when.

Did this help?

Thank you for a very detailed explanation!