I have seen a lot of explanations of what Bayesian networks are, but I simply cannot wrap my head around their use in code. So here is my three part question.

  1. Am I right in my definition of Bayes nets? Bayesian networks are a way to (visually) portray how variables/events are linked and how they will affect each other. Probabilities can be added to each node to give you a greater sense of what will happen. In conjunction with an acting AI, Bayes nets are used to predict the AI's actions and inform what it should be doing, i.e. giving minimax a better prediction.
  2. Can you give a pseudo code representation of a Bayes net?
  3. Let's get a little more specific. I have a creature simulator, the creature has a home where he sleeps and, there are patches of grass where he eats. There are other creatures out there that he wants to avoid because they will fight him. So his action -> reaction set is:

    {hungry->find food -> eat food
    tiered -> go to shelter -> sleep
    in danger -> avoid other creature
    under attack -> run to shelter}

I am trying to express this as an mdp so that it will learn what actions are best when; and what are the best spots (most prosperous & least dangerous) on the map.

How, if at all, am I supposed to use a Bayes net? If the situation requires probabilities feel free to add your own, pseudo code also appreciated.

  • 1
    $\begingroup$ The idea is less that they're for visual representation, and more that they're for compact representation of Joint Probability distributions. You trade having to store a massive table of probabilities for the time of computing probabilities from the information in the Bayes net. $\endgroup$ May 13, 2013 at 17:46
  • 2
    $\begingroup$ what do you mean by (2)? (3) is a decision problem not sure how its related to (1) and (2) $\endgroup$
    – seteropere
    May 13, 2013 at 18:54
  • $\begingroup$ @seteropere for 2, I don't really understand where a bayes net fits into to a program so I wanted to see pseudo code for a basic bayes net. So that I could see how it applied to programing. For 3, again I don't know where bayes nets fit into code, this is a place that I thought it might be applicable. If not then my definition is wrong. So does that mean 1. is wrong or my usage of 1 is wrong. In which case when can a bayes net be used in programing? $\endgroup$ May 14, 2013 at 2:46
  • $\begingroup$ BN is a management tool for uncertainty. Usually, you need to have a solver in order to answer queries over the structure. There are many BN solvers out there cs.ubc.ca/~murphyk/Software/bnsoft.html . So how BN fits into programming? by implementing a solver for it. $\endgroup$
    – seteropere
    May 14, 2013 at 3:10
  • 2
    $\begingroup$ I don't see a definition anywhere. Bayesian networks are sets of random variables with certain properties. I don't know what you mean by "code representation" or "fit into a program" or "apply [BN] to programming". $\endgroup$
    – Raphael
    Aug 26, 2013 at 13:04

1 Answer 1


I am trying to state a possible bayes net: What the creature wants to know is what to do next. So the root node is 'ACTION'. Possible actions are (let's say field 1 = home): [move to field 1, move to field 2, ... move to field n, sleep, eat] or [go north, go south, go east, go west, eat, sleep]. 'ACTION' depends on the creature's 'STATE', that can be [afraid(by another creature), under attack, hungry, tired]. 'STATE' depends on 'ANOTHER CREATURE' and 'FOOD AVAILABLE'. The latter two nodes can be [yes,no] and both depend on the current 'FIELD', [field 1, ..., field n].







Each node has a table containing the probabilities p(value|value of dependency1, value dependency2,...). For example the 'ACTION' table holds the probabilities for each combination of p(action to take | value of 'STATE', value of 'FIELD').

I think suitable methods to estimate the model's probabilities are Q-learning or SARSA-learning.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.