Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Bayesian Networks II

Problem 2.1

Consider the following joint distribution for three binary random variables a,b,c{0,1}a, b, c \in \{0,1\}:

bacp(a,b,c)p(a,b,c)
0000.192
0010.144
0100.048
0110.216
1000.192
1010.064
1100.048
1110.096

(a) Show that aa and bb are dependent, i.e. p(a,b)p(a)p(b)p(a,b) \ne p(a)p(b). (b) Show that aa and bb are conditionally independent given cc, i.e.

p(a,bc)=p(ac)p(bc)p(a,b|c)=p(a|c)p(b|c)

Problem 2.2

Conditional independence properties entailed by a Bayesian Network can be read directly from the graph, using the notion of d-separation. Given the following Bayesian Network (Graph nodes: A, B, C, D, E, F, G, H, I, J), which of the following conditional independence statements hold?

a) AFA \perp F b) AGA \perp G c) BIFB \perp I | F d) DJG,HD \perp J | G,H e) IBHI \perp B | H f) JDJ \perp D g) ICH,FI \perp C | H,F


Problem 2.3

In the Cyanobacteria example, we have the following variables: U={T,F,C,M,W}\mathcal{U}=\{T,F,C,M,W\} where

  • Temperature: T{cold,hot}T \in \{cold, hot\}

  • Presence of fertilizer in water: F{yes,no}F \in \{yes, no\}

  • Presence of cyanobacteria in water: C{yes,no}C \in \{yes, no\}

  • Fish mortality: M{yes,no}M \in \{yes, no\}

  • Water color: W{clear,green}W \in \{clear, green\}

The joint factorization is

P(T,F,C,M,W)=p(t)p(f)p(ct,f)p(wc)p(mc)\mathbb{P}(T,F,C,M,W)=p(t)p(f)p(c|t,f)p(w|c)p(m|c)

Assume the probability distributions are given by: p(t=cold)=0.4,p(t=hot)=0.6p(t=cold)=0.4, \quad p(t=hot)=0.6 p(f=yes)=0.2,p(f=no)=0.8p(f=yes)=0.2, \quad p(f=no)=0.8

The conditional probability table (CPT) for p(ct,f)p(c|t,f) is:

t=coldt=coldt=hott=hot
f=yesf=yesp(c=yes)=0.5p(c=yes)=0.5p(c=yes)=0.95p(c=yes)=0.95
f=nof=nop(c=yes)=0.05p(c=yes)=0.05p(c=yes)=0.8p(c=yes)=0.8

The conditional probability table (CPT) for p(mc)p(m|c) is:

c=yesc=yesc=noc=no
m=yesm=yes0.60.1
m=nom=no0.40.9

The conditional probability table (CPT) for p(wc)p(w|c) is:

c=yesc=yesc=noc=no
w=clearw=clear0.70.2
w=greenw=green0.30.8

We want to compute the posterior probability of fish mortality given colored water:

p(mw=green)=p(m,w=green)p(w=green)p(m|w=green)=\frac{p(m,w=green)}{p(w=green)}

a) Eliminate variables sequentially by working with its chain-rule factorization. The joint probability is

P(F,T,W,M,C)=p(t)p(f)p(ct,f)p(wc)p(mc)\mathbb{P}(F,T,W,M,C)=p(t)p(f)p(c|t,f)p(w|c)p(m|c)

where computing p(mw=green)p(m|w=green) corresponds to

p(mw=green)=t,f,cp(t)p(f)p(ct,f)p(w=greenc)p(mc)p(m|w=green)=\sum_{t,f,c}p(t)p(f)p(c|t,f)p(w=green|c)p(m|c)

Express the procedure how to compute p(mw=green)p(m|w=green) by indicating the dimensions of the conditional probability tables.

b) Compute the conditional probability table (CPT) for p(c,ft)p(c,f|t). c) Compute the conditional probability table (CPT) for p(ct)p(c|t). d) Compute the conditional probability table (CPT) for p(c,t)p(c,t). e) Compute the probability p(c)p(c). f) Compute the conditional probability table (CPT) for p(m,c)p(m,c). g) Compute the conditional probability table (CPT) for p(m,w=green,c)p(m, w=green, c). h) Compute the probability table for p(m,w=green)p(m,w=green). i) Compute p(w=green)p(w=green). j) Compute the conditional probability p(mw=green)p(m|w=green). k) Compute the conditional probability p(mw=green)p(m|w=green) by means of pgmpy.

l) MLE and MAP (Laplace smoothing): For the conditional probability p(c=yest=hot,f=yes)p(c=yes|t=hot,f=yes): (i) give the maximum-likelihood estimator (MLE) in terms of counts, and (ii) give the MAP estimator using Laplace smoothing (add-one).

m) Handling missing entries - expected counts for MAP: Given the dataset with missing entries (observations Di\mathcal{D}_{i}), write the symbolic expression for the expected count E[#{c=yes,t=hot,f=yes}]\mathbb{E}[\#\{c = yes, t = hot, f=yes\}] used in the expectation-maximization / MAP step. Explain briefly how this expectation is computed for: (i) an observation with no missing entries, (ii) an observation with some missing variables (show one short example).


Problem 2.4

Suppose you wish to perform variable elimination on the Bayesian Network shown in the graph (Nodes: A, B, C, D, E, F, G, H, I, J). Consider the following variable elimination ordering: A, B, C, D, E, F, G, H, I, J.

For each iteration of the algorithm (i.e., for each variable in the ordering), determine which factors are removed and which new factors are introduced. As an example, in the first iteration (eliminating variable A), the factors P(A)P(A) and P(BA)P(B|A) are removed, and a new factor g1(B)g_{1}(B) is introduced.


Problem 2.5

In this exercise, you will use the variable elimination algorithm to perform inference on a Bayesian Network. Consider the network with nodes A, B, C, D, E, F and the corresponding CPTs:

P(A=t)=0.3P(A=t)=0.3 P(C=t)=0.6P(C=t)=0.6

(a) P(BA,C)P(B|A,C):

ACP(B=t)P(B=t)
ff0.2
ft0.8
tf0.3
tt0.5

(b) P(DC)P(D|C):

CP(D=t)P(D=t)
f0.9
t0.75

(c) P(EB)P(E|B):

BP(E=t)P(E=t)
f0.2
t0.4

(d) P(FD,E)P(F|D,E):

DEP(F=t)P(F=t)
ff0.95
ft1.00
tf0.00
tt0.25

Assuming a query on A with evidence for B and D, i.e. computing P(AB,D)P(A|B,D), use the variable elimination algorithm to answer the following queries: a) P(A=tB=t,D=f)P(A=t|B=t,D=f) b) P(A=fB=f,D=f)P(A=f|B=f,D=f) c) P(A=tB=t,D=t)P(A=t|B=t,D=t)

Consider now the variable elimination ordering: C, E, F, D, B, A. Use again the variable elimination algorithm and write down the intermediate factors, this time without computing their probability tables. Discuss whether this ordering is better or worse than the one used previously, and explain why.


Problem 2.6

In the Cyanobacteria example (variables U={T,F,C,M,W}\mathcal{U}=\{T,F,C,M,W\}), the joint factorization is:

P(T,F,C,M,W)=p(t)p(f)p(ct,f)p(wc)p(mc)\mathbb{P}(T,F,C,M,W)=p(t)p(f)p(c|t,f)p(w|c)p(m|c)

a) For the conditional probability p(c=yest=hot,f=yes)p(c=yes|t=hot,f=yes): (i) give the maximum-likelihood estimator (MLE) in terms of counts. (ii) give the MAP estimator using Laplace smoothing (add-one).

b) We have a partially observed Bayesian Network. We want to estimate p(c=yest=hot,f=yes)p(c=yes|t=hot,f=yes). Estimate the count by the expected count E(#{c=yes,t=hot,f=yes})\mathbb{E}(\#\{c=yes,t=hot,f=yes\}). (i) For D1=(cold,?,yes,?,clear)\mathcal{D}_{1} = (cold, ?, yes, ?, clear), compute p(c=yes,t=hot,f=yesD1)p(c=yes,t=hot, f=yes|\mathcal{D}_{1}). (ii) For D2=(?,?,yes,yes,clear)\mathcal{D}_{2} = (?, ?, yes, yes, clear), compute p(c=yes,t=hot,f=yesD2)p(c=yes, t=hot, f=yes|\mathcal{D}_{2}). (iii) For D4=(hot,yes,yes,yes,green)\mathcal{D}_{4} = (hot, yes, yes, yes, green), compute p(c=yes,t=hot,f=yesD4)p(c=yes, t=hot, f=yes|\mathcal{D}_{4}). (iv) Indicate in symbolic notation the MAP using the expected counts obtained.

c) Read in cyanobacteria_data.csv and learn the parameters using pgmpy. (i) Learn parameters without smoothing. (ii) Learn parameters with smoothing. Predict p(MW=green)p(M|W=green).

d) Assume a dataset with unobserved fertilizer (F): cyanobacteria_unobserved_fertilizer.csv. Using Expectation Maximization (EM), estimate the values of the unobserved fertilizer variable using pgmpy. Then predict p(MW=green)p(M|W=green).


Problem 2.7

Consider the following causal DAG: UU (Genotype) X\rightarrow X (Smoking) UU (Genotype) Y\rightarrow Y (Lung Cancer) XX (Smoking) Z\rightarrow Z (Tar Deposits) ZZ (Tar Deposits) Y\rightarrow Y (Lung Cancer)

We define the joint distribution P(X,Z,Y)=P(X)P(ZX)P(YZ)\mathbb{P}(X,Z,Y)=P(X)P(Z|X)P(Y|Z) (marginalizing over U implicitly in the provided CPTs for the exercise):

P(X=1)=0.5,P(X=0)=0.5P(X=1)=0.5, P(X=0)=0.5

P(ZX)P(Z|X):

Z=0Z=1
X=00.950.05
X=10.050.95

P(YZ)P(Y|Z):

Y=0Y=1
Z=00.140.86
Z=10.240.76

a) Construct the Bayesian network in pgmpy. b) Learn the parameters from the given CPTs. c) Compute the probabilistic (associational) query P(Y=1X=1)P(Y=1|X=1). d) (Optional) Explain briefly why this differs from the causal effect of smoking on lung cancer.


Solutions

Solution 2.1 a) & b) Calculations provided showing p(a,b)p(a)p(b)p(a,b) \neq p(a)p(b) but p(a,bc)=p(ac)p(bc)p(a,b|c) = p(a|c)p(b|c).

Solution 2.2 a) No, b) Yes, c) No, d) Yes, e) Yes, f) No, g) No.

Solution 2.3 (a - k) Calculations for Variable Elimination steps provided. Result for j: p(m=yesw=green)=0.26p(m=yes|w=green) = 0.26, p(m=now=green)=0.74p(m=no|w=green) = 0.74.

Solution 2.6 (Python Code Snippet)

# Compute p(m | w = "green") using pgmpy
from pgmpy.models import DiscreteBayesianNetwork
from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination

# 1. Define the Bayesian Network structure
model = DiscreteBayesianNetwork([
    ('T', 'C'),
    ('F', 'C'),
    ('C', 'M'),
    ('C', 'W')
])

# 2. Define the Conditional Probability Tables (CPTs)
cpd_T = TabularCPD(variable='T', variable_card=2, values=[[0.4], [0.6]], state_names={'T': ['cold', 'hot']})
cpd_F = TabularCPD(variable='F', variable_card=2, values=[[0.2], [0.8]], state_names={'F': ['yes', 'no']})
cpd_C = TabularCPD(variable='C', variable_card=2,
                   values=[[0.5, 0.05, 0.95, 0.8], [0.5, 0.95, 0.05, 0.2]],
                   evidence=['T', 'F'], evidence_card=[2, 2],
                   state_names={'C': ['yes', 'no'], 'T': ['cold', 'hot'], 'F': ['yes', 'no']})
cpd_M = TabularCPD(variable='M', variable_card=2,
                   values=[[0.6, 0.1], [0.4, 0.9]],
                   evidence=['C'], evidence_card=[2],
                   state_names={'M': ['yes', 'no'], 'C': ['yes', 'no']})
cpd_W = TabularCPD(variable='W', variable_card=2,
                   values=[[0.7, 0.2], [0.3, 0.8]],
                   evidence=['C'], evidence_card=[2],
                   state_names={'W': ['clear', 'green'], 'C': ['yes', 'no']})

# 3. Add CPTs to the model
model.add_cpds(cpd_T, cpd_F, cpd_C, cpd_M, cpd_W)
assert model.check_model()

# 4. Perform inference
infer = VariableElimination(model)
posterior = infer.query(variables=['M'], evidence={'W': 'green'})
print(posterior)

Solution 2.7 (Python Code Snippet)

from pgmpy.models import DiscreteBayesianNetwork
from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination

# Define structure
model = DiscreteBayesianNetwork([("X", "Z"), ("Z", "Y")])

# Define CPTS
cpd_x = TabularCPD("X", 2, [[0.5], [0.5]])
cpd_z = TabularCPD("Z", 2, [[0.95, 0.05], [0.05, 0.95]], evidence=["X"], evidence_card=[2])
cpd_y = TabularCPD("Y", 2, [[0.14, 0.24], [0.86, 0.76]], evidence=["Z"], evidence_card=[2])

# Add to model
model.add_cpds(cpd_x, cpd_z, cpd_y)
model.check_model()

# Inference
infer = VariableElimination(model)
q = infer.query(variables=["Y"], evidence={"X": 1})
# Output: P(Y=1|X=1)=0.73