0

I'm trying to build a simple Bayesian network, where rain and sprinkler are the parents of wetgrass, but rain and sprinkler each have three (fuzzy-logic type rather rather than the usual two boolean) states, and wetgrass has two states (true/false). I can't find anywhere in the pymc3 docs what syntax to use to describe the CPTs for this -- I'm trying the following based on 2-state examples but it's not generalizing to three states the way I thought it would. Can anyone show the correct way to do this? (And also for the more general case where wetgrass has three states too.)

rain = mc.Categorical('rain', p = np.array([0.5, 0. ,0.5]))   

sprinker = mc.Categorical('sprinkler', p=np.array([0.33,0.33,0.34]))  

wetgrass = mc.Categorical('wetgrass',
    mc.math.switch(rain,
        mc.math.switch(sprinker, 10, 1, -4),
        mc.math.switch(sprinker, -20, 1, 3),
        mc.math.switch(sprinker, -5, 1, -0.5)))

[gives error at wetgrass definition: Wrong number of inputs for Switch.make_node (got 4((, , , )), expected 3) ]

As I understand it - switch is a theano function similar to (b?a:b) in a C program; which is only doing a two way comparison. It's maybe possible to set up the CPT using a whole load of binary switches like this, but I really want to just give a 3D matrix CPT as the input as in BNT and other bayes net libraries. Is this currently possible ?

charles.fox
  • 483
  • 4
  • 10

1 Answers1

1

You can code a three-way switch using two individual switches:

tt.switch(sprinker == 0,
    10
    tt.switch(sprinker == 1, 1, -4))

But in general it is probably better to index into a table:

table = tt.constant(np.array([[...], [...]]))
value = table[rain, sprinker]
aseyboldt
  • 1,090
  • 8
  • 14
  • Thanks -- this is working to define the network, however I'm having trouble running sampling and logp caluculations - I'll ask this as a separate question now though. – charles.fox Apr 21 '17 at 10:53
  • New question and running code including aseyboldt's answer to this one is here: http://stackoverflow.com/questions/43540977/pymc3-mutli-category-bayesian-network-sampling-and-logp-calculate – charles.fox Apr 21 '17 at 10:59