Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evidence in form of probabilities #7

Open
jona2510 opened this issue Sep 26, 2018 · 1 comment
Open

Evidence in form of probabilities #7

jona2510 opened this issue Sep 26, 2018 · 1 comment

Comments

@jona2510
Copy link

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%".

@jluttine
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants