---
title: "Problem set 9: latent variables, sampling, ensembles, reinforcement learning and hardware"
heading: "Problem set 9: probabilistic PCA and whitening, filtering and likelihoods in hidden Markov models, mixing of chains and samplers, importance weights, tree splits and boosting rounds, forests, values and updates in reinforcement learning, and serving and quantizing models"
eyebrow: S14 · Problem set 9
type: problems
---

::: hook
These problems cover lessons 40 to 47: principal components as a probabilistic model, hidden Markov models followed through several observations, how fast Markov chains and samplers forget their starting point, ensembles of trees, values and updates for a learning agent, and the memory, bandwidth and precision that decide what a model costs to run.
:::

::: pool
pick: 12

::: check
kind: parts
given: a = 6 | 10; b = 3 | 4; c = 0.8 | 1.2; d = 0.4 | 0.6

## A four-dimensional data set's covariance has eigenvalues {a}, {b}, {c} and {d}. Probabilistic PCA keeps two components.

::: part
answer: (c + d)/2
tolerance: 0.5%

### What noise variance does it estimate?

::: misses
- [c + d] It's the average of the discarded eigenvalues.
- [any] $\sigma^2 = \frac1{D - M}\sum_{i > M}\lambda_i$.
:::
:::

::: part
answer: (a + b)/(a + b + c + d)
tolerance: 0.5%

### What fraction of the variance do the two components explain?

::: misses
- [any] Kept eigenvalues over the total.
:::
:::
:::

::: check
kind: parts
given: a = 4 | 9; b = 1 | 4; c = 1 | -1.5; x = 2 | -1; y = 1 | 2

## Data have covariance $\begin{bmatrix}a & c \\ c & b\end{bmatrix}$ with a = {a}, b = {b}, c = {c}. A point lies at ({x}, {y}) from the mean.

::: part
answer: sqrt((b*x^2 - 2*c*x*y + a*y^2)/(a*b - c^2))
tolerance: 0.5%

### How far is it after whitening (its Mahalanobis distance)?

::: misses
- [sqrt(x^2/a + y^2/b)] That ignores the covariance c.
- [sqrt(x^2 + y^2)] That's the plain distance.
- [any] $\sqrt{\mathbf x^T\Sigma^{-1}\mathbf x}$.
:::
:::
:::

::: check
kind: parts
given: q = 0.02 | 0.1; s = 0.95 | 0.99; r = 0.3 | 0.6; e = 0.05 | 0.1; f = 0.6 | 0.9

## A machine is faulty with probability {q} at hour 1. A healthy machine stays healthy with probability {s}; a faulty one is repaired with probability {r} per hour. Its alarm fires with probability {e} when healthy and {f} when faulty, and it fires in hours 1 and 2.

::: part
answer: q*f/(q*f + (1 - q)*e)
tolerance: 0.5%

### What is P(faulty at hour 1 | the first alarm)?

::: misses
- [any] Bayes' theorem with the alarm's two likelihoods.
:::
:::

::: part
answer: (q*f/(q*f + (1 - q)*e)*(1 - r) + (1 - q*f/(q*f + (1 - q)*e))*(1 - s))*f/((q*f/(q*f + (1 - q)*e)*(1 - r) + (1 - q*f/(q*f + (1 - q)*e))*(1 - s))*f + (1 - (q*f/(q*f + (1 - q)*e)*(1 - r) + (1 - q*f/(q*f + (1 - q)*e))*(1 - s)))*e)
tolerance: 0.5%

### What is P(faulty at hour 2 | both alarms)?

::: misses
- [any] Predict through the transition, then update on the second alarm.
:::
:::
:::

::: check
kind: parts
given: q = 0.05 | 0.2; s = 0.9 | 0.98; r = 0.2 | 0.5; e = 0.05 | 0.2; f = 0.7 | 0.95

## The same kind of machine: faulty with probability {q} at hour 1, staying healthy with probability {s}, repaired with probability {r}, alarms {e} healthy and {f} faulty.

::: part
answer: (q*f + (1 - q)*e)
tolerance: 0.5%

### What is the probability of an alarm in hour 1?

::: misses
- [any] Average the alarm's probability over the state.
:::
:::

::: part
answer: q*f*((1 - r)*f + r*e) + (1 - q)*e*((1 - s)*f + s*e)
tolerance: 0.5%

### What is the probability of alarms in both hours 1 and 2?

::: misses
- [(q*f + (1 - q)*e)^2] The alarms aren't independent: they share the hidden state.
- [any] Forward algorithm: sum over both hours' states.
:::
:::
:::

::: check
kind: parts
given: a = 0.05 | 0.2; b = 0.1 | 0.4; e = 0.01 | 0.001

## A two-state chain leaves state 1 with probability {a} and state 2 with probability {b}.

::: part
answer: abs(1 - a - b)
tolerance: 0.5%

### By what factor does its distance from equilibrium shrink each step?

::: misses
- [any] The second eigenvalue, $|1 - a - b|$.
:::
:::

::: part
answer: ln(e)/ln(abs(1 - a - b))
unit: steps
tolerance: 0.5%

### How many steps bring that distance down by a factor of {e}?

::: misses
- [any] $\ln\epsilon/\ln|1 - a - b|$.
:::
:::
:::

::: check
kind: parts
given: s = 0.5 | 1 | 2; g = 1 | 3; A = 0.25 | 0.44 | 0.6

## A random-walk Metropolis sampler targets a Gaussian of standard deviation {g} with proposal steps of standard deviation {s}.

::: part
answer: 2/pi*atan(2*g/s)
tolerance: 0.5%

### What fraction of proposals does it accept?

::: misses
- [any] $\frac2\pi\tan^{-1}(2\sigma/s)$.
:::
:::

::: part
answer: 2*g/tan(A*pi/2)
tolerance: 0.5%

### What step size would give an acceptance rate of {A}?

::: misses
- [any] Invert: $s = 2\sigma/\tan(\pi A/2)$.
:::
:::
:::

::: check
kind: parts
given: r = 0.5 | 0.9 | 0.99; K = 500 | 5000

## A Gibbs sampler alternates between two standard Gaussian variables with correlation {r}.

::: part
answer: (1 - r^2)/(1 + r^2)
tolerance: 0.5%

### What fraction of its sweeps count as independent samples?

::: misses
- [(1 - r)/(1 + r)] The chain's lag-one correlation is ρ², not ρ.
- [any] $(1 - \rho^2)/(1 + \rho^2)$.
:::
:::

::: part
answer: K*(1 + r^2)/(1 - r^2)
unit: sweeps
tolerance: 0.5%

### How many sweeps give {K} effective samples?

::: misses
- [any] Divide by that fraction.
:::
:::
:::

::: check
kind: parts
given: s = 1.2 | 2 | 4; K = 1000 | 10000

## Importance sampling estimates expectations under N(0, 1) with samples from N(0, {s}²).

::: part
answer: sqrt(2*s^2 - 1)/s^2
tolerance: 0.5%

### What fraction of the samples is effective (1 over the mean squared weight)?

::: misses
- [1/s] The mean squared weight is $s^2/\sqrt{2s^2 - 1}$.
- [any] $\sqrt{2s^2 - 1}/s^2$.
:::
:::

::: part
answer: K*s^2/sqrt(2*s^2 - 1)
unit: samples
tolerance: 0.5%

### How many samples give an effective {K}?

::: misses
- [any] Divide by that fraction.
:::
:::
:::

::: check
kind: parts
given: a = 30 | 40; b = 10 | 5; c = 10 | 20; d = 50 | 35

## A split sends {a} positive and {b} negative examples left and {c} positive and {d} negative right.

::: part
answer: -((a + c)/(a + b + c + d))*log((a + c)/(a + b + c + d))/log(2) - ((b + d)/(a + b + c + d))*log((b + d)/(a + b + c + d))/log(2)
unit: bits
tolerance: 0.5%

### What is the parent node's entropy?

::: misses
- [any] Binary entropy of the overall positive fraction.
:::
:::

::: part
answer: -((a + c)/(a + b + c + d))*log((a + c)/(a + b + c + d))/log(2) - ((b + d)/(a + b + c + d))*log((b + d)/(a + b + c + d))/log(2) - (a + b)/(a + b + c + d)*(-(a/(a + b))*log(a/(a + b))/log(2) - (b/(a + b))*log(b/(a + b))/log(2)) - (c + d)/(a + b + c + d)*(-(c/(c + d))*log(c/(c + d))/log(2) - (d/(c + d))*log(d/(c + d))/log(2))
unit: bits
tolerance: 0.5%

### What is the split's information gain?

::: misses
- [any] Parent entropy minus the size-weighted children's entropies.
:::
:::
:::

::: check
kind: parts
given: e = 0.1 | 0.2 | 0.35; w = 0.01 | 0.05

## An AdaBoost round's stump has weighted error {e}. One example it misclassified had weight {w}.

::: part
answer: 0.5*ln((1 - e)/e)
tolerance: 0.5%

### What is the stump's vote α?

::: misses
- [ln((1 - e)/e)] AdaBoost's vote has a factor ½.
- [any] $\frac12\ln[(1 - \epsilon)/\epsilon]$.
:::
:::

::: part
answer: w/(2*e)
tolerance: 0.5%

### What is that example's weight after reweighting and normalizing?

::: misses
- [w*sqrt((1 - e)/e)] Normalize too: the misclassified examples' weights end up summing to ½.
- [any] Misclassified weights are scaled by $1/(2\epsilon)$ overall.
:::
:::
:::

::: check
kind: parts
given: s = 1 | 4; r = 0.6 | 0.4; u = 0.3 | 0.15; k = 1.2 | 1.5; B = 50 | 500

## Bagged trees each have prediction variance {s} and pairwise correlation {r}. Sampling features at each split cuts the correlation to {u} but raises each tree's variance by a factor of {k}.

::: part
answer: r*s + (1 - r)*s/B
tolerance: 0.5%

### What variance does averaging {B} bagged trees give?

::: misses
- [s/B] The correlated part doesn't average away.
- [any] $\rho\sigma^2 + (1 - \rho)\sigma^2/B$.
:::
:::

::: part
answer: u*k*s + (1 - u)*k*s/B
tolerance: 0.5%

### And {B} trees with feature sampling?

::: misses
- [any] The same formula with the new correlation and variance.
:::
:::
:::

::: check
kind: parts
given: r = 1 | 2; u = 0 | -0.5; a = 0.1 | 0.4; b = 0.3 | 0.6; g = 0.9 | 0.99

## A sensor policy earns {r} per step while charged and {u} while low, goes low with probability {a} and recharges with probability {b}; γ = {g}.

::: part
answer: (r*(1 - g + g*b) + g*a*u)/((1 - g + g*a)*(1 - g + g*b) - g^2*a*b)
tolerance: 0.5%

### What is the charged state worth?

::: misses
- [r/(1 - g)] It doesn't stay charged forever.
- [any] Solve $V = R + \gamma PV$.
:::
:::

::: part
answer: (b*r + a*u)/(a + b)
tolerance: 0.5%

### What is its long-run average reward per step?

::: misses
- [any] Stationary probabilities b/(a + b) and a/(a + b) weight the rewards.
:::
:::
:::

::: check
kind: parts
given: g = 0.8 | 0.9; Q = 1 | 2.5; r = 0.5 | 1; M = 3 | 4; a = 0.2 | 0.5; e = 0.001 | 0.000001

## An agent with discount {g} has Q(s, a) = {Q}, receives r = {r} and lands where the best action value is {M}; its learning rate is {a}.

::: part
answer: Q + a*(r + g*M - Q)
tolerance: 0.5%

### What does Q(s, a) become?

::: misses
- [Q + a*(r + M - Q)] Discount the next state.
- [any] $Q + \alpha(r + \gamma\max Q' - Q)$.
:::
:::

::: part
answer: ln(e)/ln(g)
unit: sweeps
tolerance: 0.5%

### How many value-iteration sweeps would shrink an error by a factor of {e} at this discount?

::: misses
- [any] $\ln\epsilon/\ln\gamma$.
:::
:::
:::

::: check
kind: parts
given: P = 3 | 8 | 30; b = 4 | 8; F = 200 | 1000; W = 1 | 3.35; B = 8 | 64

## A {P}-billion-parameter model with {b}-bit weights runs on a chip with {F} TFLOP/s and {W} TB/s, at batch {B}.

::: part
answer: F*b/(16*W)
tolerance: 0.5%

### At what batch does it become compute-bound?

::: misses
- [any] $16B/b = F/W$.
:::
:::

::: part
answer: ((B*W*1e12/(P*1e9*b/8)) + F*1e12/(2*P*1e9) - abs(B*W*1e12/(P*1e9*b/8) - F*1e12/(2*P*1e9)))/2
unit: tokens/s
tolerance: 0.5%

### What throughput does it reach at batch {B}?

::: misses
- [any] The smaller of the memory and compute limits.
:::
:::
:::

::: check
kind: parts
given: P = 7 | 70; e = 640 | 1300

## A {P}-billion-parameter model's weights are quantized symmetrically. Reading a byte from DRAM costs {e} pJ.

::: part
answer: P*1e9*(8 - 4)/8/1e9
unit: GB
tolerance: 0.5%

### How much memory does going from 8-bit to 4-bit weights save?

::: misses
- [any] 4 bits per weight.
:::
:::

::: part
answer: 10*log(4*127^2) - 10*log(4*7^2)
unit: dB
tolerance: 0.5%

### How much signal-to-quantization-noise ratio does a full-scale uniform signal lose (127 levels against 7 on each side)?

::: misses
- [24] About 6 dB per bit, but with symmetric levels it's $20\log(127/7)$.
- [any] $10\log(4Q^2)$ for each.
:::
:::

::: part
answer: P*1e9/2*e*1e-12
unit: J
tolerance: 0.5%

### How much energy does reading the 4-bit weights once take?

::: misses
- [any] Half a byte per weight.
:::
:::
:::
:::
