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.

Chapter 23 — Lab: Independence and d-Separation

Open in Colab

Lab Overview

You will write the tools that read independence from a Bayesian network’s graph, and check one against the numbers.

ExerciseTopicPoints
1Chain, fork or collider?10
2The joint probability of one assignment15
3All trails between two nodes15
4Is a trail active?20
5d-separation10
6The Markov blanket10
7v-structures and Markov equivalence20
ChallengeThe back-door adjustment+10

How it works. Each exercise has a function to complete where you see # TODO. Run the check cell under it: ✅ means your function is right, ❌ tells you what is wrong. Graphs are networkx.DiGraphs; you may use G.predecessors, G.successors, nx.descendants, but not nx.is_d_separator. Do not change the check cells.


Exercise 1: Chain, Fork or Collider? (10 points)

connection(G, a, b, c): a, b, c are consecutive on a trail (a–b and b–c are edges in either direction). Return 'collider' if both edges point into b, 'fork' if both point out of b, and 'chain' otherwise.


Exercise 2: The Joint Probability of One Assignment (15 points)

joint(x): x is a dict giving 0 or 1 for every variable. Return ∏vP(xv∣parents(xv))\prod_v P(x_v \mid \text{parents}(x_v)) using PARENTS and CPT (which store P(v=1∣parents)P(v = 1 \mid \text{parents})).


Exercise 3: All Trails (15 points)

trails(G, x, y) returns every simple path between x and y in the graph ignoring directions, each as a list of nodes from x to y. (Hint: nx.all_simple_paths on G.to_undirected().)


Exercise 4: Is a Trail Active? (20 points)

active(G, trail, Z): every consecutive triple must pass. A chain or fork passes if its middle node is not in Z; a collider passes if its middle node or one of its descendants is in Z. Use your connection.


Exercise 5: d-Separation (10 points)

d_sep(G, x, y, Z): True if no trail between x and y is active given Z.


Exercise 6: The Markov Blanket (10 points)

markov_blanket(G, v): the parents, the children, and the children’s other parents of v, as a set without v.


Exercise 7: Markov Equivalence (20 points)

v_structures(G) returns the set of triples (a, c, b) with a < b (alphabetically), edges a → c ← b, and no edge between a and b. equivalent(G1, G2) is True if the two graphs have the same skeleton (undirected edges) and the same v-structures.


Challenge: The Back-Door Adjustment (+10 points)

adjusted(stats, t): stats maps (treatment, stratum) to (successes, patients). Return ∑sP(success∣t,s) P(s)\sum_s P(\text{success} \mid t, s)\,P(s), where P(s)P(s) is the share of all patients (both treatments) in stratum ss.


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • one paragraph: a study of hospital patients finds that diabetes is less common among patients with a broken leg. Draw the graph that explains it, and say what the study should have done.