Chapter 23 — Assignment Project: What a Network Says, and What Data Can Confirm
Overview¶
A university models its students with the classic student network (Koller and Friedman): a course’s Difficulty and a student’s Intelligence determine the Grade; intelligence also determines the SAT score; the grade determines the strength of the recommendation Letter. All variables are binary here. You will read the network’s independences, check them against its numbers and against sampled data, find what data could and could not reveal about its arrows, and analyse a selection effect.
| Part | Task | Points |
|---|---|---|
| A | Queries and explaining away | 20 |
| B | Every independence, checked exactly | 20 |
| C | What a sample can confirm | 20 |
| D | The equivalence class | 20 |
| E | A selection effect | 20 |
import itertools
import numpy as np
import networkx as nx
from scipy.stats import chi2, chi2_contingency
PARENTS = {'D': [], 'I': [], 'G': ['I', 'D'], 'S': ['I'], 'L': ['G']}
CPT = {'D': {(): 0.4}, 'I': {(): 0.3}, # P(hard course), P(high intelligence)
'G': {(1, 0): 0.9, (1, 1): 0.5, (0, 0): 0.3, (0, 1): 0.05}, # P(good grade | I, D)
'S': {(1,): 0.8, (0,): 0.05}, # P(high SAT | I)
'L': {(1,): 0.9, (0,): 0.1}} # P(strong letter | G)
G = nx.DiGraph([(p, v) for v, ps in PARENTS.items() for p in ps])
VARS = list(PARENTS)
ROWS = np.array(list(itertools.product([0, 1], repeat=len(VARS))))
P = np.array([np.prod([CPT[v][tuple(r[VARS.index(u)] for u in PARENTS[v])] if r[j] else 1 - CPT[v][tuple(r[VARS.index(u)] for u in PARENTS[v])] for j, v in enumerate(VARS)]) for r in ROWS])
def prob(query, evidence={}):
m = lambda d: np.all([ROWS[:, VARS.index(k)] == v for k, v in d.items()], axis=0) if d else np.ones(len(P), bool)
return P[m({**query, **evidence})].sum() / P[m(evidence)].sum()
print(f'{len(ROWS)} assignments, total probability {P.sum():.6f}')Part A — Queries and Explaining Away (20 points)¶
Compute , and . Explain the change between the last two in one sentence.
Compute and . Why are they different, or why are they the same?
# Your code hereYour explanations:
Part B — Every Independence, Checked Exactly (20 points)¶
For every pair of variables and every conditioning set of at most one other variable, decide d-separation (you may use nx.is_d_separator) and check the numbers: is for all values? Report the d-separated statements and whether the numbers agree.
# Your code herePart C — What a Sample Can Confirm (20 points)¶
Sample 500 students and 20 000 students from the network (ancestral sampling). For each of the statements of Part B, and for the same number of dependent pairs of your choice, run a pooled chi-squared test at the 5% level. How many true independences are wrongly rejected, and how many dependences are missed, at each sample size? What does this mean for learning a network’s structure from data?
# Your code hereYour interpretation:
Part D — The Equivalence Class (20 points)¶
List every network over the same five variables that is Markov equivalent to the student network (same skeleton, same v-structures; reverse subsets of its edges and keep the acyclic, equivalent ones). Which arrows can data alone never orient? Which ones are fixed, and why?
# Your code hereYour answer:
Part E — A Selection Effect (20 points)¶
A scholarship committee only ever sees students with a strong letter (). Compute and in the whole population and among the students the committee sees. Explain the result with the graph, and say what a committee member who concludes “bright students choose harder courses” has done wrong.
# Your code hereYour explanation:
Grading Rubric¶
| Criterion | Excellent | Adequate | Insufficient |
|---|---|---|---|
| A (20) | correct numbers; explaining away and blocking explained with the graph | numbers only | missing |
| B (20) | all statements, checked exactly | statements without checks | missing |
| C (20) | both sizes, both error kinds, consequences for structure learning | one size | missing |
| D (20) | all members listed; fixed and free arrows explained with v-structures | members only | missing |
| E (20) | numbers and a correct collider explanation | numbers only | missing |