jluttine / junction-tree

The junction tree algorithm for (discrete) factor graphs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Evidence in form of probabilities

jona2510 opened this issue · comments

Hello,
Taking as an example the Bayesian Network of {cloudy, sprinkler, rain, wetGrass},
Is there any way to provide evidence in the form "Now, I know that the probability of wetGrass-true is 60% and wetGrass-false is 40%"?. Note that this is different from the example where the evidence is in the form "I am completely sure that wetGrass-true is 100% and wetGrass-false is 0%".

I am not entirely sure what you mean by that but would this be what you want:

import junctiontree as jt

jtree = jt.create_junction_tree(
    [
        ["cloudy"],                        # p(cloudy)
        ["cloudy", "sprinkler"],           # p(sprinkler|cloudy)
        ["cloudy", "rain"],                # p(rain|cloudy)
        ["sprinkler", "rain", "wetGrass"], # p(wetGrass|sprinkler,rain)
    ],
    sizes={
        "cloudy": 2,
        "sprinkler": 2,
        "rain": 2,
        "wetGrass": 2,
    }
)

jtree.propagate(
    [
        # p(cloudy)
        np.array([0.5, 0.5]),
        # p(sprinkler|cloudy)
        np.array(
            [
                [0.5, 0.5], # cloudy=False
                [0.9, 0.1], # cloudy=True
            ]
        ),
        # p(rain|cloudy)
        np.array(
            [
                [0.8, 0.2], # cloudy=False
                [0.2, 0.8], # cloudy=True
            ]
        ),
        # p(wetGrass|sprinkler,rain)
        np.array(
            [
                # sprinkler=False
                [
                    [0.4*1.00, 0.6*0.00], # rain=False
                    [0.4*0.10, 0.6*0.90], # rain=True
                ],
                # sprinkler=True
                [
                    [0.4*0.10, 0.6*0.90], # rain=False
                    [0.4*0.01, 0.6*0.99], # rain=True
                ],
            ]
        ),
    ]
)

Note that this library is intentionally quite low level. It doesn't expect any value to be a probability nor does it normalise the results. It just runs the junction tree algorithm for the given factor graph that have the given numerical arrays as the factor potentials whatever they are. It returns the results for the factors and you need to normalise them in order to get probabilities. Also, if you need some specific marginals, you may need to do that marginalisation to some array manually.