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.

Solutions · 12. Information: entropy, Kullback–Leibler divergence and fault diagnosis

School of Engineering, Nazarbayev University

Worked solutions to the three exercises of Lecture 12. Each solution is computed here and checked with assert statements against an exact identity or an independent calculation. The first code cell sets up the notebook and runs Demonstration 1 of the lecture without showing its output, because some of the solutions use its variables.

Exercise 1

Problem. Verify the Chain Rule of Shannon Entropy H(X,Y)=H(X)+H(Y∣X)=H(Y)+H(X∣Y)H(X, Y) = H(X) + H(Y \mid X) = H(Y) + H(X \mid Y), symmetry I(X;Y)=I(Y;X)I(X; Y) = I(Y; X), conditioning bound 0≤H(X∣Y)≤H(X)0 \le H(X \mid Y) \le H(X), and exact KL identity I(X;Y)=DKL(PXY∥PX⊗PY)I(X; Y) = D_{\mathrm{KL}}(P_{XY} \| P_X \otimes P_Y) across a continuum of coupled (8×6)(8 \times 6) fault-sensor joint distributions parameterized by coupling strength λ∈[0,1]\lambda \in [0, 1] (where λ=0\lambda = 0 is independent PXPYTP_X P_Y^T and λ=1\lambda = 1 is a sharp diagnostic sensor channel).


Exercise 1: Chain rule of entropy and mutual information KL identity on coupled joint tables
============================================================================================
At lambda=0 (independent): H(X)=2.6475, H(X|Y)=2.6475, I(X;Y)=0.00e+00 bits
At lambda=1 (coupled)    : H(X)=2.6475, H(X|Y)=0.9388, I(X;Y)=1.7087 bits
Max discrepancy between Chain-Rule I(X;Y) and D_KL(P_XY || P_X x P_Y): 8.88e-16 bits
<Figure size 814x451 with 1 Axes>

Figure S12.1. Entropies of the coupled fault-sensor distribution against the coupling λ\lambda. H(X)H(X) stays constant; H(X∣Y)H(X|Y) falls and I(X;Y)I(X;Y) rises, and their sum equals H(X)H(X) at every λ\lambda.

Explanation. Writing log⁡2P(x,y)P(x)P(y)=log⁡21P(x)−log⁡21P(x∣y)\log_2 \frac{P(x, y)}{P(x)P(y)} = \log_2 \frac{1}{P(x)} - \log_2 \frac{1}{P(x \mid y)} and averaging over P(x,y)P(x, y) shows that the mutual information is the KL divergence from the product of marginals PX⊗PYP_X \otimes P_Y to the true joint law PXYP_{XY}. Because DKL≥0D_{\mathrm{KL}} \ge 0 by Gibbs’ inequality, observing a noisy measurement YY can never increase expected posterior entropy H(X∣Y)H(X \mid Y) (though a single surprising realization Y=yY = y can temporarily raise H(X∣Y=y)H(X \mid Y = y) when a previously unlikely fault suddenly becomes plausible).


Exercise 2

Problem. Audit two real-world failure modes in information-based diagnostics: (a) Show how unguarded np.sum(-p * np.log2(p)) returns nan on sparse histograms and q_i = 0 yields inf KL divergence unless Laplace-smoothed; (b) Model all 24 states (12 coins x {heavy, light}) of the 12-Coin Two-Pan Balance Riddle, compute first-step ternary information gain H3H_3 for weighing kk vs. kk coins (k∈{1..6}k \in \{1..6\}), prove why 6 vs 6 leaves 12>32=912 > 3^2 = 9 states and cannot finish in 3 weighings, and build a complete verified 3-weighing solver starting with 4 vs 4.


Exercise 2: Sparse NaN/Inf entropy traps and exact information audit of the 12-coin balance riddle
==================================================================================================
[Part A] Naive -sum(p*log2(p)) on sparse bins = nan vs. guarded shannon_entropy_metrics = 1.8242 bits
         Unsmoothed D_KL(live || ref_with_zeros) = inf vs. Jeffreys/Laplace (+0.5) = 0.0671 bits

[Part B] 1st Weighing k vs k | Branch sizes (L<R, L=R, L>R) | Max Branch (<=9 needed) | Info Gain [bits]
--------------------------------------------------------------------------------------------
  1 vs 1 coins             | ( 2, 20,  2)                | 20 -> impossible (> 9)    | 0.8167
  2 vs 2 coins             | ( 4, 16,  4)                | 16 -> impossible (> 9)    | 1.2516
  3 vs 3 coins             | ( 6, 12,  6)                | 12 -> impossible (> 9)    | 1.5000
  4 vs 4 coins             | ( 8,  8,  8)                |  8 -> feasible (<= 3^2=9) | 1.5850
  5 vs 5 coins             | (10,  4, 10)                | 10 -> impossible (> 9)    | 1.4834
  6 vs 6 coins             | (12,  0, 12)                | 12 -> impossible (> 9)    | 1.0000
Verified 4-vs-4 ternary decision tree: identified 24/24 states in max 3 weighings
<Figure size 814x440 with 2 Axes>

Figure S12.2. First weighing of the 12-coin puzzle. Bars: largest number of states left after weighing kk against kk coins; only 4 against 4 leaves no more than 32=93^2 = 9 (dashed). Line: information gained, largest at 4 against 4, log⁡23\log_2 3 bits.

Explanation. Weighing 6 vs 6 puts every coin on the scale, guaranteeing the scale cannot balance (P(Balanced)=0\mathbb{P}(\text{Balanced}) = 0). Throwing away the 3rd ternary outcome collapses a log⁡23=1.585-bit\log_2 3 = 1.585\text{-bit} ternary balance into a 1.000-bit1.000\text{-bit} binary device, leaving 24/2=1224 / 2 = 12 states—strictly exceeding the 32=93^2 = 9 state capacity of the remaining two weighings. Leaving 4 coins off the pans (4 vs 4) splits 24→(8,8,8)24 \to (8, 8, 8), extracting the full log⁡23=1.585 bits\log_2 3 = 1.585\ \mathrm{bits} on Step 1 so every branch fits comfortably inside ≤9\le 9 states.


Exercise 3

Problem. Implement bayesian_active_sensor_polling(prior_probs, sensor_tpr, sensor_fpr, true_fault, max_steps=12, seed=42) in NumPy. Support MM fault candidates where sensor_tpr and sensor_fpr specify noisy binary sensors (either 1-D arrays of length MM for per-valve point sensors or 2-D (T, M) sensitivity matrices). At each step tt, evaluate the exact expected posterior Shannon entropy EYs[H(ppost(⋅∣Ys))]\mathbb{E}_{Y_s}[H(p_{\mathrm{post}}(\cdot \mid Y_s))] for every sensor ss, poll the sensor maximizing expected entropy reduction, draw a noisy binary reading, and update the posterior via Bayes’ rule. Benchmark active polling vs. round-robin polling across 250 Monte Carlo episodes.


Exercise 3: Implement it yourself — active Bayesian noisy sensor polling
========================================================================
After 6 polls : Active mean H = 0.738 bits vs. Round-Robin = 1.043 bits
After 12 polls: Active MAP accuracy = 92.8% (H=0.235) vs. Round-Robin = 88.0% (H=0.458)
<Figure size 825x451 with 1 Axes>

Figure S12.3. Mean posterior entropy against the number of noisy polls for round-robin polling (dashed) and for choosing each poll by the expected entropy drop (solid).

Summary. Choosing each poll by the expected drop in posterior entropy both confirms and bisects: when two suspects have similar posterior probabilities, one noisy reading cannot separate them, and the rule polls the sensors that discriminate between them again. After 12 polls the mean posterior entropy is 0.235 bits against 0.458 bits for round-robin polling, and the most probable fault is the true one in 92.8 % of the runs against 88.0 %.