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 26 — Lab: Temporal and Undirected Models

Open in Colab

Lab Overview

You will implement the algorithms of the chapter and check each against brute force on small cases: the three HMM algorithms, learning an HMM by counting, a Kalman step, the Gibbs conditional of an Ising pixel, and the probability of a label sequence under a linear-chain CRF.

ExerciseTopicPoints
1The forward algorithm15
2Smoothing with forward–backward15
3Viterbi decoding15
4Learning an HMM by counting10
5One Kalman step15
6The conditional of an Ising pixel15
7A CRF’s probability of a labelling15
ChallengeSystematic resampling+10

How it works. States and observations are integers from 0. An HMM is (pi, A, B) with A[i, j] =P(zt+1=j∣zt=i)= P(z_{t+1} = j \mid z_t = i) and B[i, k] =P(ot=k∣zt=i)= P(o_t = k \mid z_t = i). Complete each function where you see # TODO and run the check cell under it. Do not change the check cells.


Exercise 1: The Forward Algorithm (15 points)

forward(o, pi, A, B) returns (alpha, loglik): alpha[t] is the filtered distribution P(zt∣o1:t)P(z_t \mid o_{1:t}) (each row sums to 1), and loglik is log⁡P(o1:T)\log P(o_{1:T}). Normalise at each step and add up the logarithms of the normalisers.


Exercise 2: Smoothing with Forward–Backward (15 points)

smooth(o, pi, A, B) returns gamma with gamma[t] =P(zt∣o1:T)= P(z_t \mid o_{1:T}). Compute the backward messages from the end (normalised as you like, as long as each row of gamma is renormalised to 1).


Exercise 3: Viterbi Decoding (15 points)

viterbi(o, pi, A, B) returns the most likely state sequence as an integer array. Work with logarithms (np.log(0) is -inf, which is fine: silence the warning with np.errstate(divide='ignore')).


Exercise 4: Learning an HMM by Counting (10 points)

count_hmm(Z, Os, K, M, alpha=1.0) learns (pi, A, B) from labelled sequences (Z[n] the states, Os[n] the observations of sequence n), adding the pseudo-count alpha to every cell before normalising each row.


Exercise 5: One Kalman Step (15 points)

kalman_step(mu, P, y, F, Q, H, R) predicts and then updates with the measurement y, and returns the new (mu, P). If y is None, it only predicts.

μ−=Fμ\mu^- = F\mu, P−=FPF⊤+QP^- = FPF^\top + Q; K=P−H⊤(HP−H⊤+R)−1K = P^- H^\top (H P^- H^\top + R)^{-1}; μ′=μ−+K(y−Hμ−)\mu' = \mu^- + K(y - H\mu^-); P′=(I−KH)P−P' = (I - KH)P^-.


Exercise 6: The Conditional of an Ising Pixel (15 points)

In the denoising model P(x∣y)∝exp⁡(β∑i∼jxixj+η∑ixiyi)P(x \mid y) \propto \exp(\beta \sum_{i \sim j} x_i x_j + \eta \sum_i x_i y_i) with x,y∈{−1,+1}x, y \in \{-1, +1\}, p_plus(x, y, i, j, beta, eta) returns P(xij=+1∣all other pixels,y)P(x_{ij} = +1 \mid \text{all other pixels}, y). Only the 4 neighbours (fewer at the border) and yijy_{ij} matter: σ(2(βs+ηyij))\sigma\big(2(\beta s + \eta y_{ij})\big) with ss the sum of the neighbours and σ\sigma the logistic function.


Exercise 7: A CRF’s Probability of a Labelling (15 points)

A linear-chain CRF over TT positions and KK labels is given by its unary scores U (T×KT \times K, already computed from the input) and transition scores Tm (K×KK \times K). The score of a labelling zz is ∑tUt,zt+∑t≥1Tmzt−1,zt\sum_t U_{t, z_t} + \sum_{t \ge 1} Tm_{z_{t-1}, z_t}, and P(z∣o)=exp⁡(score)/ZP(z \mid o) = \exp(\text{score}) / Z. crf_log_prob(U, Tm, z) returns log⁡P(z∣o)\log P(z \mid o), with log⁡Z\log Z computed by the forward algorithm in log space (use logsumexp).


Challenge: Systematic Resampling (+10 points)

A particle filter resamples NN particles in proportion to their weights. Systematic resampling draws one uniform number u∈[0,1/N)u \in [0, 1/N) and takes the particles at the positions u,u+1/N,u+2/N,…u, u + 1/N, u + 2/N, \dots of the cumulative weights. systematic(weights, rng) returns the NN chosen indices (sorted). Each particle ii is then chosen either ⌊Nwi⌋\lfloor N w_i \rfloor or ⌈Nwi⌉\lceil N w_i \rceil times, less random than independent draws.


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • one paragraph: in Exercise 3, Viterbi’s path and the sequence of the most likely states at each step (from Exercise 2) can differ. Find an observation sequence where they do, and explain why both answers are correct for their own question.