Bayesian Networks in Python
Probability Refresher
- Random variable — upper case letters (e.g. B)
- Values on a random variable — lower case letters (e.g. b)
- Conditional probability: P(a | b) = P(a,b)/P(b) (i.e. The probability of a given b = The joint probability of a and b divided by The probability of b)
- Product rule: P(a,b) = P(a|b)P(b) (This is actually joint probability, which measure the probability of the two events happening at the same time)
- Bayes Theorem: P(b|a) = P(a|b)P(b)/P(a)
A Simple Bayesian Network
What we want to calculate is P(L, R, W).
We can write this probability as P(L) x P(R) x P(W | R).
- Winning the lottery has no influence on rain or ground being wet
- Ground being wet depends on Rain
Bayesian Network vs. Naive Bayes Classifier (NBC)
You probably have used NBC to perform classification tasks. A key assumption in NBC is that all variables are independent, which is rarely the case. BNs on the other hand can take dependencies between variables into consideration. Also, when the prior probabilities of variables changes or new variables are introduced, NBs can be updated with the new knowledge.
Inference
- Given Bayesian Network describing P(X, Y, Z), what is P(Y)?
Two methods:
- Enumeration
- Variable Elimination
Enumeration
Variable Elimination
- Every variable that is not an ancestor of a query variable or evidence variable is irrelevant to the query
- The iterative procedure:
Choose a variable to eliminate
Sum terms relevant to variable, generate a new factor
Repeat until no more variables to eliminate - Exact inference is #P-Hard (In tree structured BNs, linear time (in the number of entries)
Independence in Bayes Nets
Modeling Monty Hall Problem
We will model the problem as a BN.
We have three variables: guest, prize and monty. Each can take three values (three doors): A, B, C
# for creating Bayesian Belief Networks (BBN)
from pybbn.graph.dag import Bbn
from pybbn.graph.edge import Edge, EdgeType
from pybbn.graph.jointree import EvidenceBuilder
from pybbn.graph.node import BbnNode
from pybbn.graph.variable import Variable#the guest's intitial door selection is completely random
guest = BbnNode(Variable(0, 'guest', ['A', 'B', 'C']), [1.0/3, 1.0/3, 1.0/3])#the door the prize is behind is also completely random
prize = BbnNode(Variable(1, 'prize', ['A', 'B', 'C']), [1.0/3, 1.0/3, 1.0/3])#monty is dependent on both guest and prize
monty = BbnNode(Variable(2, 'monty', ['A', 'B', 'C']), [0, 0.5, 0.5, #A, A
0, 0, 1, #A, B
0, 1, 0, #A, C
0, 0, 1, #B, A
0.5, 0, 0.5, #B, B
1, 0, 0, #B, C
0, 1, 0, #C, A
1, 0, 0, #C, B
0.5, 0.5, 0 #C, C
])
Above, we create three nodes, now, we are going to create the actual BN.
# Create Network
bbn = Bbn() \
.add_node(guest) \
.add_node(prize) \
.add_node(monty) \
.add_edge(Edge(guest, monty, EdgeType.DIRECTED)) \
.add_edge(Edge(prize, monty, EdgeType.DIRECTED))# Convert the BBN to a join tree
join_tree = InferenceController.apply(bbn)
We use the following helper function to print marginal probabilities:
# Define a function for printing marginal probabilities
def print_probs():
for node in join_tree.get_bbn_nodes():
potential = join_tree.get_bbn_potential(node)
print("Node:", node)
print("Values:")
print(potential)
print('----------------')
What is the initial marginal probabilities (with no prior beliefs available):
print_probs()
The output is as follows:
We use the following helper function to update the beliefs.
# To add evidence of events that happened so probability
# distribution can be recalculated
def evidence(ev, nod, cat, val):
ev = EvidenceBuilder() \
.with_node(join_tree.get_bbn_node_by_name(nod)) \
.with_evidence(cat, val) \
.build()
join_tree.set_observation(ev)
Let’s say that the guest selects A.
evidence('ev1', 'guest', 'A', 1.0)# Print marginal probabilities
print_probs()
Now, let’s say Monty selects B.
evidence('ev2', 'monty', 'B', 1.0)# Print marginal probabilities
print_probs()
After above prior beliefs, we can see that P(Prize behind A) = 1/3 and P(Price behind C) = 2/3. So, Guest is better of switching his/her decision!
I hope your updated prior beliefs convinced you that BNs are not only cool but also help you make informed decisions as the world around you is constantly changing.
References