PyMC3 Conditioning Random Variable on Multiple Discrete Parents

364 Views Asked by At

I recently started doing probabilistic programming using PyMC3. In my scenario, I have 3 random variables: On, Triangle, and X, such that X depends on Triangle and On. Triangle and On both follow Bernoulli distributions, and depending on which value they take, the value of X, which follows a Normal, changes.

I wrote up some mock code to test this concept and the code is not good, mainly because you can't call numpy.isnan() on PyMC3 distributions. I just started working in this framework, and I know I'm not writing code that will run, but I'm posting this here so you can see what I've done.

with pymc3.Model() as model:
    on = pymc3.distributions.discrete.Bernoulli('on', p=.7)
    x_on = pymc3.distributions.continuous.Normal('x_on', 10, .4)

    pTriangle_given_on = 1
    pTriangle_given_not_on = .7
    pTriangle = pymc3.math.switch(on, pTriangle_given_on, pTriangle_given_not_on)
    triangle = pymc3.distributions.discrete.Bernoulli('triangle', p=pTriangle)

    name = None
    x_triangle = None

    if triangle:
        name = pymc3.distributions.discrete.Categorical('name', p=[.3, .2, .1, .1, .2, .1])
    else:
        name = pymc3.distributions.discrete.Categorical('name', p=[.1, .5, .4])

    if on:
        x_triangle = pymc3.Deterministic('x_triangle', x_on)
    elif triangle:
        x_triangle = pymc3.Normal('x_triangle', 5, 1)
    else:
        x_triangle = numpy.nan

    trace = pymc3.sample()
    pymc3.plot_posterior(trace)

I'm not sure how to specify the conditional dependence of X on Triangle and On. Any thoughts from you all would be much appreciated.

0

There are 0 best solutions below