# Simple Bayesian Network/ Sprinkler example

**URL:** <https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258>\
**Category:** General\
**Created:** [July 24, 2017, 1:12pm UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258 "2017-07-24T13:12:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljmachado](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.edwardlib.org/ljmachado/32/360_2.png) [@ljmachado](https://discourse.edwardlib.org/u/ljmachado)\
**Post date:** [July 24, 2017, 1:12pm UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258/1 "2017-07-24T13:12:57Z")

</div>

Hi,

I’m trying to build a model where I can add subjective expert information to the usual machine learning model. To get a bet understanding on how to do that with Edward, I wanted to implement the classic Sprinkler causal model from many Bayesian tutorials. However I’m having trouble finding myself in how to do that and I hope someone here could help me.

Do I have to explicitly create a ConditionalDistribution class to create the graph model or is there a easier way? Is creating the inference model much different than the BayesRegression example? Is there any examples of Edward to build causal graph models?

Thanks!

---

<div class="post-metadata">

**Author:** ![janislavjankov](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@janislavjankov](https://discourse.edwardlib.org/u/janislavjankov)\
**Post date:** [August 10, 2017, 7:00am UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258/2 "2017-08-10T07:00:31Z")

</div>

Hi There! I was also looking for a way to build that model and do simple inference on it, for instance the p(R=T|G=T).  
The way I was able to make it work is this:

```
rain = edm.Bernoulli(probs=0.2)
p_sprinkler = tf.where(tf.cast(rain, tf.bool), 0.01, 0.4)
sprinkler = edm.Bernoulli(probs=p_sprinkler)
p_grass_wet = tf.where(tf.cast(rain, tf.bool),
                       tf.where(tf.cast(sprinkler, tf.bool), 0.99, 0.8),
                       tf.where(tf.cast(sprinkler, tf.bool), 0.9, 0.00000001))
grass_wet = edm.Bernoulli(probs=p_grass_wet)

# Inference
q_rain = edm.Bernoulli(probs=tf.nn.sigmoid(tf.Variable(tf.random_normal([]))))
ed.get_session()
inf = edi.KLpq({rain: q_rain}, data={grass_wet: tf.constant(1, dtype=tf.int32)})
inf.run(n_samples=50)
print(q_rain.probs.eval())

```

For the complete working code check:  
[https://notebooks.azure.com/janislavjankov/libraries/shared/html/edward-sprinkler.ipynb](https://notebooks.azure.com/janislavjankov/libraries/shared/html/edward-sprinkler.ipynb)

Not sure if this is the correct way but it’s working.  
Another approach would be to just sample [rain, grass\_wet] and compute the conditional probability by counting the results, essentially applying the conditional probability formula p(r=t|g=t) = p(r=t, g=t) / p(g=t) but using the approximated (through the sampling) probabilities. But this is not really using any inference algorithms that Edward provides.

---

<div class="post-metadata">

**Author:** ![Achtung](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Achtung](https://discourse.edwardlib.org/u/Achtung)\
**Post date:** [December 5, 2017, 3:58am UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258/3 "2017-12-05T03:58:22Z")

</div>

@janislavjankov, this is great, thanks!

Could you suggest a way to infer the distribution parameters given lots of training data? For example, I generate many samples of rain, sprinkler, and grass\_wet to infer the probabilities of each node. I’m not entirely sure how to do it with Edward/TF? Do you have any suggestions?

---

<div class="post-metadata">

**Author:** ![janislavjankov](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@janislavjankov](https://discourse.edwardlib.org/u/janislavjankov)\
**Post date:** [December 5, 2017, 6:22am UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258/4 "2017-12-05T06:22:04Z")

</div>

Hi @Achtung I’m glad you find this helpful. Not sure I understand your question correctly… if you want to find the conditional probabilities using a lot of sampled data then directly applying the conditional probability formula with the approximated probabilities (I.e. number of samples that were true over all the samples) should give you good results.  
Perhaps if you provide a code sample or the data and what you want to infer I could help.

---

<div class="post-metadata">

**Author:** ![Achtung](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Achtung](https://discourse.edwardlib.org/u/Achtung)\
**Post date:** [December 5, 2017, 3:51pm UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258/5 "2017-12-05T15:51:11Z")

</div>

@janislavjankov, sure.

Given data like [{ rain = 1, sprinkler = 1, grass\_wet = 1 }, {rain = 1, sprinkler = 0, grass\_wet =0}, …], how do we infer the probability of it raining?

In the code sample, the probability was hard coded to 0.2. However, I want to be able to infer this parameter (and/or other parameters such as p\_grass\_wet). In particular, I want it to be based on multiple observations. In the example you’ve given, there is one observation that grass\_wet = 1.

I had attempted this earlier with this post ([Simple Hierarchical Distribution](https://discourse.edwardlib.org/t/simple-hierarchical-distribution/425)), but I think I was quite confused in several respects. In particular, I wasn’t sure how to plug in a lot of data and be able to do inference.

---

<div class="post-metadata">

**Author:** ![janislavjankov](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@janislavjankov](https://discourse.edwardlib.org/u/janislavjankov)\
**Post date:** [December 8, 2017, 6:30pm UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258/6 "2017-12-08T18:30:24Z")

</div>

@Achtung I uploaded the notebook here: [https://notebooks.azure.com/janislavjankov/libraries/shared/html/edward-sprinkler-inf.ipynb](https://notebooks.azure.com/janislavjankov/libraries/shared/html/edward-sprinkler-inf.ipynb)  
Snippet for the inference part:

```
n_batches = samples.shape[0] // N
grass_wet_ph = tf.placeholder(tf.int32, shape=[N])
inf = edi.KLqp({prob_grass_wet: q_prob_grass_wet}, data={grass_wet: grass_wet_ph})
inf.initialize(n_iter=100 * n_batches, n_samples=10, scale={grass_wet: n_batches})
sess.run(tf.global_variables_initializer())
for i in range(inf.n_iter):
    data_index = i * N % samples.shape[0]
    info_dict = inf.update(feed_dict={grass_wet_ph: samples[data_index: data_index + N, 2]})
    inf.print_progress(info_dict)

```

I hope you can use this as a starting point.

---

<div class="post-metadata">

**Author:** ![Achtung](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Achtung](https://discourse.edwardlib.org/u/Achtung)\
**Post date:** [December 8, 2017, 8:02pm UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258/7 "2017-12-08T20:02:11Z")

</div>

@janislavjankov, this is awesome! Exactly what I was looking for. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![sjha8](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@sjha8](https://discourse.edwardlib.org/u/sjha8)\
**Post date:** [September 4, 2018, 7:01pm UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258/8 "2018-09-04T19:01:55Z")

</div>

The link is broken. Could you please share the code on github or elsewhere? Thanks for your help.

---

<div class="post-metadata">

**Author:** ![liuchenbaidu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.edwardlib.org/liuchenbaidu/32/355_2.png) [@liuchenbaidu](https://discourse.edwardlib.org/u/liuchenbaidu)\
**Post date:** [September 7, 2018, 8:43am UTC](https://discourse.edwardlib.org/t/simple-bayesian-network-sprinkler-example/258/9 "2018-09-07T08:43:19Z")

</div>

which algorithm of the inference  
because the other inference python lib like pyro we can set algorithm.

like optimizer = tf.train.AdamOptimizer(learning\_rate=FLAGS.lr)  
but in the example code i don’t find any.
