below is one way to define two stochastic Bernoulli random variables, one depending on the other with decorators. the model is meant to be:
p(A) = 0.5
p(B=True|A=True) = 0.75
p(B=True|A=False) = 0.05
using decorators in PyMC it is:
import pymc
from pymc import DiscreteUniform, Exponential, deterministic, Poisson, Uniform
import numpy as np
def make_model():
@pymc.stochastic(dtype=bool)
def A(value=False):
def logp(value):
return pymc.bernoulli_like(value, 0.5)
def random():
return bool(np.random.binomial(1, 10**logp(value)))
@pymc.stochastic(dtype=bool)
def B(value=False, A=False):
def logp(value, A):
if A:
logp = pymc.bernoulli_like(value, 0.75)
else:
logp = pymc.bernoulli_like(value, 0.05)
return logp
def random(A):
return bool(np.random.binomial(1, 10**logp(True, A)))
return locals()
test = pymc.Model(make_model())
is this the most correct and compact way to do it? could some code be saved by defining the nodes explicitly as pymc.Bernoulli
variables, rather than with the stochastic
decorator?
the above code seems redundant having to always define how to sample from a bernoulli RV, but maybe this is unavoidable?
finally is it necessary to invoke sampling functions from numpy
like np.random.binomial
or are there sampling functions in PyMC?