Bayesian Network Builder

There are several paradigms in software development, functional programming, object oriented programming, and each of them is intended to solve a particular family of problems. Probabilistic programming is a paradigm that addresses statistical problems. Bayesian Network Builder brings in the hands of software developers the ability to deal with probabilities without being statisticians.

I like to compare the Bayes Rule to car driving. While we are confident in our ability to drive forward in the direction in front of us, we are a little bit reluctant to drive backward. Let’s watch it in action with the burglary example:

Burglary Example

I’m at work, neighbour John calls to say my alarm is ringing, but neighbour Mary doesn’t call. Sometimes the alarm is set off by minor earthquakes. Both burglary and earthquake are rather rare events.

The question we want to answer: Is there really a burglar at home?

Considering that:

  • John always calls when he hears the alarm, but sometimes confuses the telephone ringing with the alarm.

  • Mary likes rather loud music and sometimes misses the alarm.

import com.sap.bnb.bn._
import com.sap.bnb.dsl._

val g = graph {
 "burglar" <~ Flip(.001)
 "earthquake" <~ Flip(.002)
 "alarm" <~ ("burglar", "earthquake",
     (true, true) -> Flip(.95),
     (true, false) -> Flip(.94),
     (false, true) -> Flip(.29),
     (false, false) -> Flip(.001))
  "alarm" ~ (true -> Flip(.9), false -> Flip(.05)) ~> "JohnCalls"
  "alarm" ~ (true -> Flip(.7), false -> Flip(.01)) ~> "MaryCalls"
}
val burglar = g.evidences("JohnCalls" -> true, "MaryCalls" -> false)
      .solve("burglar").value.get
println(s"posterior: $burglar")
println("chances burglary: " + f"${burglar.chances(true) * 100}%2.1f%%")
posterior: true -> .005, false -> .995
chances burglary: 0,6%

Bayesian inference helps us to avoid falling into cognitive illusions, the mistakes that we normally do when we trust too much our intuitions. If you want to know more about that, I wrote about it and if it does not actually solve any incumbent business problem, it might be helpful for winning a car in the next TV quiz!

Updated: