One universal training rule: Linear, logistic, softmax, and Poisson regression all share the exact same gradient formula because their target distributions belong to the exponential family.
Four payoffs of canonical form: Cumulant derivatives (\nabla A = \mathbb{E}[T],\nabla^2 A = \mathrm{Cov}(T)), convex moment-matching MLE, maximum entropy, and additive conjugate priors.
Four building blocks: Parameter-free base measure h(\mathbf{x}) \ge 0, sufficient statistic T(\mathbf{x}) \in \mathbb{R}^d, natural parameter \boldsymbol{\eta} \in \mathbb{R}^d, and log-partition normalizer A(\boldsymbol{\eta}) = \log \int h(\mathbf{x})e^{\boldsymbol{\eta}^\top T(\mathbf{x})}\,d\mathbf{x}.
Why T(\mathbf{x}) is sufficient: Across n i.i.d. draws, \boldsymbol{\eta} interacts with the dataset solely through the fixed-size sum \sum_{i=1}^n T(\mathbf{x}_i).
Moving support (\mathrm{Unif}(0, \theta)): Because the support [0, \theta] depends on \theta, the MLE is the sample extreme \hat{\theta}_{\mathrm{MLE}} = \max_i x_i rather than a moment-matching average.
Algebraic heavy tails (Student’s t_\nu(\mu)): The product \prod_{i=1}^n \bigl(1 + (x_i-\mu)^2/\nu\bigr)^{-(\nu+1)/2} has no fixed-size sufficient statistic (Pitman–Koopman–Darmois theorem), making its NLL non-convex with multiple local minima.
Jaynes’s Loaded Die and Maximum Entropy
Where the Exponential Form Comes From
Shannon entropy measures average surprise (in nats): H[p] = -\sum_{k=1}^K p_k \log p_k \quad (\text{fair die: } H = \log 6 \approx 1.792)
Jaynes’s puzzle: A 6-sided die has average roll \mathbb{E}[k] = 4.5 instead of 3.5. Putting mass 1/2 on \{3, 6\} hits mean 4.5, but assumes faces \{1, 2, 4, 5\} are impossible (H = \log 2 \approx 0.693).
Principle of Maximum Entropy: The least-committal distribution matching \mathbb{E}[k] = 4.5 is the exponential tilt: p_k = \frac{e^{\eta k}}{\sum_{j=1}^6 e^{\eta j}}, \quad \eta \approx 0.371 \quad (H \approx 1.614)
Finding the Maximum-Entropy Tilt by Bisection
Monotonicity of the Mean Statistic
The mean roll \sum_k k\,p_k(\eta) grows strictly with \eta (derivative \mathrm{Var}_\eta(k) > 0), so 50 bisection steps find \eta \approx 0.371:
k = np.arange(1, 7)def tilt(eta): # p_k proportional to exp(eta k) w = np.exp(eta * k)return w / w.sum()lo, hi =-5.0, 5.0for _ inrange(50): # bisection: the mean increases with eta eta = (lo + hi) /2 lo, hi = (eta, hi) if (tilt(eta) * k).sum() <4.5else (lo, eta)p = tilt(eta)print('eta =', round(eta, 3), ' p =', p.round(3))print('H =', round(-(p * np.log(p)).sum(), 3), ' log 6 =', round(np.log(6), 3))
eta = 0.371 p = [0.054 0.079 0.114 0.165 0.24 0.347]
H = 1.614 log 6 = 1.792
Minimal entropy sacrifice: The exponential tilt surrenders only \log 6 - 1.614 \approx 0.18\text{ nats} of entropy to shift the mean from 3.5 to 4.5, compared with 1.10\text{ nats} lost by the two-point distribution on \{3, 6\}.
Tangent-line proof (\log u \le u - 1): Substituting u = p(\mathbf{x})/q(\mathbf{x}) gives -D_{\mathrm{KL}}(q \| p) \le \sum_{q>0} q(\mathbf{x})\bigl(\frac{p(\mathbf{x})}{q(\mathbf{x})} - 1\bigr) = \sum_{q>0} p(\mathbf{x}) - 1 \le 0.
Uniqueness: Equality \log u = u - 1 holds only at u = 1, forcing p(\mathbf{x}) = q(\mathbf{x}) everywhere.
The Maximum-Entropy Theorem and Its Corollaries
Why Every Moment-Constrained MaxEnt Distribution Is Exponential Family
Define entropy relative to base measure h(\mathbf{x}) as H_h[q] = -\sum_{\mathbf{x}} q(\mathbf{x})\log\frac{q(\mathbf{x})}{h(\mathbf{x})}.
Proposition (Maximum Entropy): Let p(\mathbf{x}) = h(\mathbf{x})\exp(\boldsymbol{\eta}^\top T(\mathbf{x}) - A(\boldsymbol{\eta})), and let q be any distribution matching its mean statistics, \mathbb{E}_q[T(\mathbf{x})] = \mathbb{E}_p[T(\mathbf{x})]. Then:
Two-line proof: Split \log\frac{q}{h} = \log\frac{q}{p} + \log\frac{p}{h}. The first sum is -D_{\mathrm{KL}}(q \| p) \le 0. Because \log\frac{p}{h} = \boldsymbol{\eta}^\top T(\mathbf{x}) - A(\boldsymbol{\eta}) is linear in T, the second sum depends on q only through its constrained mean: -\boldsymbol{\eta}^\top\mathbb{E}_q[T] + A(\boldsymbol{\eta}) = -\boldsymbol{\eta}^\top\mathbb{E}_p[T] + A(\boldsymbol{\eta}) = H_h[p]. \blacksquare
Famous corollaries:
No constraint (T \equiv 0,h \equiv 1 on K outcomes): Uniform distribution (H = \log K).
Fixed mean \mu and variance \sigma^2 on \mathbb{R}(T(x) = (x, x^2),h \equiv 1): Gaussian \mathcal{N}(\mu, \sigma^2).
Fixed mean m on \{0, 1, 2, \dots\}(T(k) = k):\mathrm{Poisson}(m) when h(k) = 1/k!, or \mathrm{Geometric} when h(k) = 1.
Moment generation by differentiation: Differentiating A(\boldsymbol{\eta}) = \log \int h(\mathbf{x})e^{\boldsymbol{\eta}^\top T(\mathbf{x})}\,d\mathbf{x} once yields the mean of T(\mathbf{x}); differentiating twice yields its covariance matrix.
Universal convexity: Because every covariance matrix is positive semidefinite, A(\boldsymbol{\eta}) and the negative log-likelihood are always convex in \boldsymbol{\eta}.
Computing Moments via Automatic Differentiation
Analytical & Autograd Verification
Bernoulli (A(\eta) = \log(1 + e^\eta)):A'(\eta) = \mathrm{sigmoid}(\eta) = p and A''(\eta) = \mathrm{sigmoid}(\eta)(1 - \mathrm{sigmoid}(\eta)) = p(1 - p).
Poisson (A(\eta) = e^\eta with \eta = \log\lambda): Since e^\eta is its own derivative, A'(\eta) = A''(\eta) = e^\eta = \lambda, explaining in one line why the Poisson mean equals its variance.
Computing Moments via Automatic Differentiation (cont.)
Analytical & Autograd Verification
def derivatives(A, eta):"""First and second derivative of the scalar function A at eta.""" eta = torch.tensor(eta, dtype=torch.float64, requires_grad=True) (dA,) = torch.autograd.grad(A(eta), eta, create_graph=True) (d2A,) = torch.autograd.grad(dA, eta)return dA.item(), d2A.item()cases = [('Bernoulli', torch.nn.functional.softplus, math.log(9/4), 9/13, 36/169), ('Poisson', torch.exp, math.log(4), 4, 4)]for name, A, eta, mean, var in cases: dA, d2A = derivatives(A, eta)print(f"{name:9s} A' = {dA:.4f} (mean {mean:.4f}) A'' = {d2A:.4f} (variance {var:.4f})")
Coin (9/13 heads):\mathrm{sigmoid}(\hat{\eta}) = \frac{9}{13} \implies \hat{\eta} = \log\frac{9}{4} \approx 0.811. At \hat{\eta}, average NLL equals the fitted coin’s entropy (0.617\text{ nats/flip}).
Boundary Divergence and Redundant Logits
When Moment Matching Has No Finite or Unique Solution
Unattainable boundary means (13/13 heads): Since \mathrm{sigmoid}(\eta) \in (0, 1) is open, \bar{T} = 1 has no finite solution (\hat{\eta} \to +\infty, just like linearly separable logistic regression without weight decay).
Non-minimal softmax logits: Because one-hot coordinates sum to \mathbf{1}^\top \mathbf{y} = 1, shifting \mathbf{z} \to \mathbf{z} + c\mathbf{1} leaves probabilities unchanged—exploited by logsumexp(c = \max_k z_k) to prevent overflow.
Generalized Linear Models and Canonical Links
Why Every Standard Loss Has Gradient Input × (Prediction − Target)
Canonical link (\boldsymbol{\eta} = \mathbf{W}^\top\mathbf{x}): The chain rule through \nabla_{\boldsymbol{\eta}} A(\boldsymbol{\eta}) = \boldsymbol{\mu}(\mathbf{x}) yields input times prediction error for every exponential-family loss.
Four GLM heads: Identity \eta (linear regression), \mathrm{sigmoid}(\eta) (logistic), \mathrm{softmax}(\boldsymbol{\eta}) (multi-class), and e^\eta (Poisson count regression).
Origins of the Softmax: Noisy Utility & Gumbel-Max
Discrete Choice Theory & Exact Categorical Sampling
Suppose a decision maker picks the class with the highest noise-perturbed score y = \operatorname{argmax}_{k \in \{1,\dots,K\}}(z_k + \epsilon_k):
Why Gaussian noise (\epsilon_k \sim \mathcal{N}(0, s^2)) stalls for K > 2: The probit model P(y=1) = \Phi\!\left(\frac{z_1 - z_2}{\sqrt{2}s}\right) requires a (K-1)-dimensional numerical integral with no closed form as soon as K \ge 3.
Proposition (Gumbel Noise Yields Softmax): If \epsilon_1, \dots, \epsilon_K are i.i.d. standard Gumbel variables with CDF G(t) = \exp(-e^{-t}), then z_j + \epsilon_j has CDF \exp(-e^{z_j}e^{-t}). Integrating over the winning score t = z_k + \epsilon_k with S = \sum_{j=1}^K e^{z_j} and v = e^{-t} closes analytically: P\Bigl(\operatorname*{argmax}_j (z_j + \epsilon_j) = k\Bigr) = \int_{-\infty}^{\infty} e^{z_k}e^{-t}\exp(-Se^{-t})\,dt = e^{z_k}\int_0^{\infty} e^{-Sv}\,dv = \frac{e^{z_k}}{\sum_{j=1}^K e^{z_j}}
Log-sum-exp as a smooth maximum: The winning score itself satisfies \max_j(z_j + \epsilon_j) \sim \mathrm{Gumbel}(\log \sum_j e^{z_j}), and its gradient with respect to \mathbf{z} is the softmax vector.
The Gumbel-Max Trick and Temperature Scaling
Three Converging Views of Softmax
1. Gumbel-Max sampler: Since \operatorname{argmax}_k(z_k + T\epsilon_k) = \operatorname{argmax}_k(z_k/T + \epsilon_k), adding Gumbel noise of scale T > 0 and taking the argmax draws exact samples from \mathrm{softmax}(\mathbf{z}/T).
2. Statistical physics (Boltzmann law): At temperature T with state energy E_k = -z_k, thermal equilibrium assigns p_k \propto e^{-E_k/T} = e^{z_k/T} (also the MaxEnt distribution at fixed expected logit \sum_k p_k z_k).
3. Probit vs. logit tails: While |\mathrm{sigmoid}(1.7x) - \Phi(x)| < 0.01, on a mislabeled outlier (m \gg 0) logit loss grows linearly (\sim m) while probit loss grows quadratically (\sim m^2/4s^2).
The Gumbel-Max Trick and Temperature Scaling (cont.)
Three Converging Views of Softmax
Verifying the Gumbel-max sampler on \mathbf{z} = (2, 1, 0.1, -1) across 10^5 trials at T = 1 and T = 2:
z = np.array([2.0, 1.0, 0.1, -1.0]) # four logitssoftmax =lambda v: np.exp(v) / np.exp(v).sum()rng = np.random.default_rng()n =100_000for T in (1.0, 2.0): # noise scale = temperature winners = np.argmax(z + T * rng.gumbel(size=(n, 4)), axis=1) freq = np.bincount(winners, minlength=4) / nprint(f'T = {T:.0f}: argmax frequencies {freq.round(3)}')print(f' softmax(z / T) {softmax(z / T).round(3)}'f' max diff {np.abs(freq - softmax(z / T)).max():.3f}')
T = 1: argmax frequencies [0.638 0.234 0.095 0.033]
softmax(z / T) [0.638 0.235 0.095 0.032] max diff 0.001
T = 2: argmax frequencies [0.451 0.274 0.173 0.102]
softmax(z / T) [0.451 0.274 0.174 0.101] max diff 0.002
Bayesian Updating and the Beta Conjugate Prior
From Point Estimates to Posterior Distributions
\mathrm{Beta}(\theta \mid a, b) \cdot \theta^x(1-\theta)^{n-x} \propto \mathrm{Beta}(\theta \mid a + x,\ b + n - x)
Additive pseudo-counts: Prior parameters (a, b) act as virtual heads and tails added directly to observed counts (x, n-x).
Mean vs. mode: Posterior mean \frac{a+x}{a+b+n} uses pseudo-counts (a, b) (giving Laplace’s rule \frac{x+1}{n+2} = \frac{2}{3} under \mathrm{Beta}(1,1)), whereas mode \frac{a+x-1}{a+b+n-2} uses (a-1, b-1).
Posterior Contraction and Non-Conjugate Grid Priors
Analytical Beta Updating vs. 1D Numerical Integration
Contraction at rate 1/\sqrt{n}: From \mathrm{Beta}(1, 1), 9/13 heads yields \mathrm{Beta}(10, 5)(\mathrm{sd} \approx 0.118);90/130 heads yields \mathrm{Beta}(91, 41)(\mathrm{sd} \approx 0.040, smaller by \sqrt{133/16} \approx 2.9\times).
Non-conjugate prior (\eta \sim \mathcal{N}(0, 1) on log-odds): By change of variables, p(\theta) = \frac{\phi(\eta)}{\theta(1-\theta)} is not a Beta density, but in 1D a fine grid normalizes p(\theta)P(x\mid\theta) directly:
Beta(1, 1) : mean 0.667, sd 0.118, mode 0.692
eta ~ N(0, 1): mean 0.644, sd 0.111, mode 0.665
Universal Conjugacy and Dirichlet Smoothing
General Exponential-Family Priors
Proposition (Universal Conjugate Prior): Every exponential-family likelihood p(\mathbf{x} \mid \boldsymbol{\eta}) = h(\mathbf{x})\exp(\boldsymbol{\eta}^\top T(\mathbf{x}) - A(\boldsymbol{\eta})) admits a conjugate prior parameterized by pseudo-statistic sum \boldsymbol{\nu} \in \mathbb{R}^d and pseudo-sample size \kappa > 0:
Add-one smoothing: counts (3,0,1) with \mathrm{Dir}(\mathbf{1}) \to mean (4/7, 1/7, 2/7) \ne 0
Concept Check: Cumulants, GLMs & Conjugacy
Check Your Understanding
Test your mastery of the exponential family on three problems:
Moments from the log-partition: The exponential distribution \mathrm{Exp}(\lambda) has T(x) = x, natural parameter \eta = -\lambda < 0, and A(\eta) = -\log(-\eta). Compute A'(\eta) and A''(\eta) to recover \mathbb{E}[X] and \mathrm{Var}(X).
Poisson GLM gradient step: A canonical-link Poisson regression model (\hat{\lambda} = e^\eta with \eta = \mathbf{w}^\top\mathbf{x}) predicts \hat{\lambda} = 3 for input \mathbf{x}, and observes count y = 5. What is the gradient of the NLL with respect to \eta and \mathbf{w}?
Dirichlet add-one smoothing: A 4-word vocabulary has observed counts \mathbf{x} = (5, 0, 1, 0)(n = 6). What is the MLE probability vector vs. the posterior mean under a uniform \mathrm{Dir}(1, 1, 1, 1) prior?
By \nabla_\eta \mathcal{L} = \mu - T(y), the gradient with respect to \eta is 3 - 5 = -2, and with respect to \mathbf{w} is -2\mathbf{x} (so gradient descent increases \eta and pushes \hat{\lambda} toward 5).
\hat{\mathbf{p}}_{\mathrm{MLE}} = (5/6, 0, 1/6, 0) assigns zero probability to unseen words. The \mathrm{Dir}(1,1,1,1) posterior is \mathrm{Dir}(6, 1, 2, 1) with posterior mean (0.6, 0.1, 0.2, 0.1).