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 22 — Lab: Probabilistic Graphical Models

Open in Colab

Lab Overview

ExerciseTopicPoints
1Bayes’ rule10
2Joint probabilities in a Bayesian network15
3Inference by enumeration20
4Likelihood weighting15
5HMM filtering: the forward algorithm20
6The Viterbi algorithm20
ChallengeGibbs sampling+10

Complete the code where you see # TODO, then run the check cell under it.


Exercise 1: Bayes’ Rule (10 points)

Complete posterior(prior, sensitivity, false_positive_rate): the probability of having the condition after a positive test.

Question. The test’s quality is the same in both cases. Why is a positive result so much more convincing when the prevalence is 10%?

Your answer:


Exercise 2: Joint Probabilities (15 points)

Complete joint(net, assignment): the product of P(Xi∣Parents(Xi))P(X_i \mid \text{Parents}(X_i)) over all variables, for a complete assignment (a dict variable → bool). Use prob.


Exercise 3: Enumeration (20 points)

Complete enumerate_query(net, query, evidence), returning P(query=True∣evidence)P(\text{query} = \text{True} \mid \text{evidence}): for each value of the query, sum joint over every combination of the hidden variables (those that are neither the query nor evidence), then normalise.

Question. Use your function to answer: are JohnCalls and MaryCalls independent? Are they independent given Alarm? Show the numbers that prove it.


Exercise 4: Likelihood Weighting (15 points)

Complete likelihood_weighting(net, query, evidence, n, rng): for each of n samples, go through the variables in order; fix each evidence variable and multiply the sample’s weight by its probability given the sampled parents; sample every other variable (rng.random() < prob(...)). Return the weighted fraction of samples in which the query is True.


Exercise 5: HMM Filtering (20 points)

The machine of the chapter: states 0 = OK, 1 = Worn; readings 0 = Low, 1 = High. Complete filter_hmm(obs, pi0, A, B), returning an array with P(Xt=Worn∣e1:t)P(X_t = \text{Worn} \mid e_{1:t}) for every tt: start with α1=π0⊙B:,e1\alpha_1 = \pi_0 \odot B_{:, e_1}; then predict with AA and weight by the evidence; normalise each αt\alpha_t (this avoids underflow on long sequences and gives the filtered probabilities directly).


Exercise 6: The Viterbi Algorithm (20 points)

Complete viterbi(obs, pi0, A, B), returning the most likely state sequence as a list. Work with log probabilities: δ1=log⁡(π0⊙B:,e1)\delta_1 = \log(\pi_0 \odot B_{:, e_1}); at each step, for every state jj keep max⁡i[δt−1(i)+log⁡Aij]+log⁡Bj,et\max_i [\delta_{t-1}(i) + \log A_{ij}] + \log B_{j, e_t} and the ii that achieved it; then follow the back-pointers from the best final state.

Question. On the readings Low, Low, High, Low, High, High, High, Low, compare the Viterbi path with the filtered probabilities. Why can the two disagree at some time steps?

Your answer:


Challenge: Gibbs Sampling (+10 points)

Complete gibbs(net, query, evidence, n, rng, burn_in): start from a random assignment of the hidden variables (evidence fixed); at each step pick the next hidden variable in turn and resample it from P(v∣Markov blanket)∝P(v∣parents)∏c∈childrenP(c∣parents of c)P(v \mid \text{Markov blanket}) \propto P(v \mid \text{parents}) \prod_{c \in \text{children}} P(c \mid \text{parents of } c); after burn_in steps, count how often the query is True.


Lab Report

Submit this notebook with every check cell showing ✅ and your answers to the three questions.