Joint Distributions and Transformations

Dive into Deep Learning · §36.2

Joint Distributions and Transformations: Conditioning, Covariance, and Jacobians
joint & marginal densities · conditional slicing · Eve’s law · optimal MSE predictor · covariance & correlation · Jacobian change of variables

Dependence, Predictability, and Correlation Differ

Motivation

  • Dependent (p_{X,Y} \ne p_X p_Y): On the unit disk, observing Y = 0.9 restricts |X| \le \sqrt{1 - 0.9^2} \approx 0.44.
  • Unpredictable in mean (\mathbb{E}[X \mid Y = y] = 0): Every horizontal chord is symmetric around 0, so knowing Y narrows the range of X without shifting its center.
  • Uncorrelated (\mathrm{Cov}(X, Y) = 0): Independence implies \mathbb{E}[X \mid Y] = \mathbb{E}[X], which implies \mathrm{Cov}(X, Y) = 0, though neither arrow reverses.

Joint Densities: Probability per Unit Area

Multivariate Densities

P\bigl((X, Y) \in \mathcal{D}\bigr) = \iint_{\mathcal{D}} p_{X,Y}(x, y)\,dx\,dy

  • Dart joint density: Constant p_{X,Y}(x, y) = 1/\pi inside the unit disk x^2 + y^2 \le 1 (area \pi) and 0 outside.
  • Why flat \ne factorized: Even though 1/\pi is constant inside the disk, the circular support indicator \mathbf{1}_{x^2 + y^2 \le 1} cannot factor as a(x)\,b(y).

Marginal Densities: Integrating Out Variables

The Sum Rule for Continuous Densities

  • To recover the density of X alone, sum the probabilities of small squares \epsilon^2 p_{X,Y}(x, \epsilon i) along the vertical strip at x and cancel \epsilon: p_X(x) = \int_{-\infty}^{\infty} p_{X,Y}(x, y)\,dy
  • Dart marginal is a semicircle: At horizontal position x \in [-1, 1], the vertical strip crosses the disk from y = -\sqrt{1-x^2} to +\sqrt{1-x^2}: p_X(x) = \int_{-\sqrt{1-x^2}}^{\sqrt{1-x^2}} \frac{1}{\pi}\,dy = \frac{2}{\pi}\sqrt{1 - x^2}
  • A flat joint density produces a curved marginal because chords through the center are longer than chords near the rim.
image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

Conditional Densities: Slicing and Renormalizing

Conditioning on Zero-Probability Events

  • Because P(Y = y) = 0, the discrete formula P(X, Y)/P(Y) divides zero by zero. Conditioning on a thin strip Y \in [y, y+\epsilon] and sending \epsilon \downarrow 0 gives the conditional density: p_{X \mid Y}(x \mid y) = \frac{p_{X,Y}(x, y)}{p_Y(y)} \quad \text{for } p_Y(y) > 0
  • Geometric picture: Slice the joint surface p_{X,Y} along the horizontal line Y = y and divide by the slice area p_Y(y) so the profile integrates to 1.
  • Dart conditional: Given Y = y, X is uniform along the chord [-\sqrt{1-y^2}, \sqrt{1-y^2}] with density \frac{1}{2\sqrt{1-y^2}}.
image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

Chain Rule, Bayes’ Rule, and Independence

Factorization & Sum of Independent Variables

p_{X \mid Y}(x \mid y) = \frac{p_{Y \mid X}(y \mid x)\,p_X(x)}{\int p_{Y \mid X}(y \mid x')\,p_X(x')\,dx'}

  • Independence (X \perp Y): Holds iff p_{X,Y}(x, y) = p_X(x)\,p_Y(y) (e.g., 4xy = (2x)(2y) on [0, 1]^2, unlike 1/\pi on the disk).
  • Convolution of a sum: When X \perp Y, marginalizing (X, X+Y) yields p_{X+Y}(s) = \int_{-\infty}^\infty p_X(x)\,p_Y(s - x)\,dx.

Conditional Expectation and the Tower Property

Averaging Within Strips

The conditional expectation \mathbb{E}[X \mid Y = y] is the center of mass of the conditional density on the slice Y = y:

\mathbb{E}[X \mid Y = y] = \int_{-\infty}^{\infty} x\,p_{X \mid Y}(x \mid y)\,dx, \qquad \mathrm{Var}(X \mid Y) = \mathbb{E}[X^2 \mid Y] - \bigl(\mathbb{E}[X \mid Y]\bigr)^2

  • Pulling out known factors: Within the strip Y = y, any function h(Y) behaves as a constant: \mathbb{E}\bigl[h(Y)\,g(X, Y) \mid Y\bigr] = h(Y)\,\mathbb{E}\bigl[g(X, Y) \mid Y\bigr]
  • Law of Total Expectation (Tower Property): Averaging the strip averages recovers the overall mean: \mathbb{E}\bigl[\mathbb{E}[X \mid Y]\bigr] = \int \left(\int x\,p_{X \mid Y}(x \mid y)\,dx\right) p_Y(y)\,dy = \iint x\,p_{X,Y}(x, y)\,dx\,dy = \mathbb{E}[X]

The Law of Total Variance (Eve’s Law)

Splitting Variance Into Within-Strip and Between-Strip Parts

\mathrm{Var}(X) = \mathbb{E}\bigl[\mathrm{Var}(X \mid Y)\bigr] + \mathrm{Var}\bigl(\mathbb{E}[X \mid Y]\bigr)

  • Unexplained (within strips): \mathbb{E}[\mathrm{Var}(X \mid Y)] = \mathbb{E}[X^2] - \mathbb{E}[(\mathbb{E}[X \mid Y])^2] measures average spread left over inside each slice Y = y.
  • Explained (between strips): \mathrm{Var}(\mathbb{E}[X \mid Y]) = \mathbb{E}[(\mathbb{E}[X \mid Y])^2] - (\mathbb{E}[X])^2 measures how much the slice centers move across Y.

The Conditional Mean Optimizes Squared Error

Theoretical Foundation of Regression

\mathbb{E}\bigl[(X - h(Y))^2\bigr] = \mathbb{E}\bigl[\mathrm{Var}(X \mid Y)\bigr] + \mathbb{E}\bigl[(\mathbb{E}[X \mid Y] - h(Y))^2\bigr]

  • Orthogonal split: Writing X - h(Y) = (X - m(Y)) + (m(Y) - h(Y)) with m(Y) = \mathbb{E}[X \mid Y], the cross term vanishes in every strip Y = y.
  • Unique MSE minimizer & R^2 ceiling: Setting h(Y) = \mathbb{E}[X \mid Y] eliminates the second term, explaining at most fraction R^2 \le \frac{\mathrm{Var}(\mathbb{E}[X \mid Y])}{\mathrm{Var}(X)} of \mathrm{Var}(X).

Eve’s Law in Action: Dart vs. Gaussian Mixture

Analytic & Empirical Variance Decomposition

  • Dart on the disk (0\% explained): Each chord at height y is symmetric (\mathbb{E}[X \mid Y=y] = 0) with uniform variance (1 - y^2)/3. Since \mathbb{E}[Y^2] = \frac{1}{2}\mathbb{E}[R^2] = \frac{1}{4}: \mathrm{Var}(X) = \frac{1 - 1/4}{3} + \mathrm{Var}(0) = \frac{1}{4} + 0
  • Two-component mixture (72\% explained): Label Z \sim \mathrm{Bernoulli}(0.2) selects \mathcal{N}(3, 1) vs. \mathcal{N}(-1, 1). Within-group variance is 1; between-group variance is 0.2(0.8)(3 - (-1))^2 = 2.56 (total 3.56).

Simulating 10^6 mixture draws confirms that Eve’s law holds exactly for the empirical sample itself:

n = 10**6
z = rng.uniform(size=n) < 0.2                          # component label Z
x = np.where(z, rng.normal(3, 1, n), rng.normal(-1, 1, n))
w = np.array([np.mean(~z), np.mean(z)])                # P(Z=0), P(Z=1)
cond_mean = np.array([x[~z].mean(), x[z].mean()])      # E[X | Z]
within = w @ np.array([x[~z].var(), x[z].var()])       # E[Var(X | Z)]
between = w @ (cond_mean - x.mean())**2                # Var(E[X | Z])
print(f'Var(X) {x.var():.3f} = within {within:.3f} + between {between:.3f}')
Var(X) 3.562 = within 1.000 + between 2.562

Covariance: Measuring Linear Co-Movement

Bivariate Second Moment

  • The covariance averages the product of deviations from the two means: \sigma_{XY} = \mathrm{Cov}(X, Y) = \mathbb{E}\bigl[(X - \mu_X)(Y - \mu_Y)\bigr] = \mathbb{E}[XY] - \mu_X\mu_Y
  • Bilinearity & shift invariance:
    • \mathrm{Cov}(X, X) = \mathrm{Var}(X)
    • \mathrm{Cov}(cX + d, Y) = c\,\mathrm{Cov}(X, Y)
    • \mathrm{Cov}(X + Z, Y) = \mathrm{Cov}(X, Y) + \mathrm{Cov}(Z, Y)
  • Scale sensitivity: Converting Y from dollars to cents multiplies \mathrm{Cov}(X, Y) by 100 without changing the point cloud’s shape.
image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

Variance of a Sum and the 1/√n Monte Carlo Rate

Why Averaging Reduces Noise

\mathrm{Var}\!\left(\sum_{i=1}^n X_i\right) = \sum_{i=1}^n \mathrm{Var}(X_i) + \sum_{i \ne j} \mathrm{Cov}(X_i, X_j)

  • Independent draws (\mathrm{Cov} = 0): \mathrm{Var}(\bar{X}_n) = \sigma^2/n \implies \sigma_{\bar{X}_n} = \sigma/\sqrt{n} (halving error requires 4\times samples).
  • Correlated ensemble (\mathrm{Corr} = \rho): \mathrm{Var}(\bar{Y}_n) = \sigma^2\bigl(\rho + \frac{1-\rho}{n}\bigr) \to \rho\sigma^2, leaving an irreducible correlation floor.

Correlation Coefficient and Cosine Geometry

Scale-Free Linear Association

  • Dividing by both standard deviations strips away physical units to define the correlation coefficient: \rho(X, Y) = \frac{\mathrm{Cov}(X, Y)}{\sigma_X \sigma_Y} \in [-1, 1]
  • Proof of |\rho| \le 1 (Cauchy–Schwarz): For every t \in \mathbb{R}, \mathrm{Var}(tX + Y) = \sigma_X^2 t^2 + 2\,\mathrm{Cov}(X, Y)t + \sigma_Y^2 \ge 0 A non-negative quadratic has discriminant \Delta \le 0, giving \mathrm{Cov}(X,Y)^2 \le \sigma_X^2\sigma_Y^2, with \rho = \pm 1 \iff Y = cX + d.
  • Hilbert-space view: Under inner product \langle \tilde{X}, \tilde{Y} \rangle = \mathbb{E}[\tilde{X}\tilde{Y}], correlation is literally \cos\angle(\tilde{X}, \tilde{Y}).
image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

Pitfall: Uncorrelated Does Not Mean Independent

Three Classic Counterexamples

  • Why \mathrm{Cov}(X, Y) = 0 is weaker than X \perp Y: Zero covariance only rules out a linear slope; symmetric nonlinear coupling leaves \mathbb{E}[XY] = \mathbb{E}[X]\mathbb{E}[Y] intact.
  • Three counterexamples: Circular disk (x^2+y^2 \le 1), parabola (Y = X^2), and random-sign Gaussian (Y = SX with |Y| = |X|).
  • Representation learning takeaway: Linear PCA whitening (\boldsymbol{\Sigma} = \mathbf{I}) removes linear correlation but does not make features independent unless they are jointly Gaussian.

The Covariance Matrix and Directional Variance

Second Moments of Random Vectors

For a random vector \mathbf{X} = (X_1, \dots, X_n)^\top with mean vector \boldsymbol{\mu} = \mathbb{E}[\mathbf{X}], the n \times n covariance matrix collects all variances on its diagonal and pairwise covariances off-diagonal:

\boldsymbol{\Sigma} = \mathbb{E}\bigl[(\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^\top\bigr], \qquad \Sigma_{ij} = \mathrm{Cov}(X_i, X_j)

  • Symmetry: \boldsymbol{\Sigma}^\top = \boldsymbol{\Sigma} because \mathrm{Cov}(X_i, X_j) = \mathrm{Cov}(X_j, X_i).
  • Positive semidefiniteness (\boldsymbol{\Sigma} \succeq \mathbf{0}): Projecting \mathbf{X} onto any fixed direction vector \mathbf{a} \in \mathbb{R}^n produces a scalar random variable \mathbf{a}^\top \mathbf{X} whose variance is the quadratic form: \mathrm{Var}(\mathbf{a}^\top \mathbf{X}) = \mathbb{E}\bigl[\mathbf{a}^\top (\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^\top \mathbf{a}\bigr] = \mathbf{a}^\top \boldsymbol{\Sigma}\mathbf{a} \ge 0
  • Spectral interpretation: The eigenvectors of \boldsymbol{\Sigma} point along the principal axes of variance (PCA), while for our symmetric dart on the unit disk, \boldsymbol{\Sigma} = \mathrm{diag}(1/4, 1/4).

1D Change of Variables: Conservation of Probability

How Transformations Reshape a Density

  • If X has density p_X and Y = g(X) for a smooth, strictly monotone map g, why is p_Y(y) \ne p_X(g^{-1}(y))?
  • Probability mass, not density height, is conserved: An interval of width \Delta x carries probability p_X(x)\Delta x and stretches to width \Delta y \approx |g'(x)|\Delta x: p_Y(y)\,\Delta y \approx p_X(x)\,\Delta x \implies p_Y(y) = \frac{p_X(x)}{|g'(x)|}
  • Formal 1D Rule: On the image interval g(I), p_Y(y) = p_X\bigl(g^{-1}(y)\bigr)\left|\frac{dg^{-1}}{dy}(y)\right|
image/svg+xml Matplotlib v3.11.2, https://matplotlib.org/

Worked Example: Log-Normal vs. Naive Plug-In

Why the Jacobian Factor 1/y Shifts the Peak

  • Let X \sim \mathcal{N}(0, 1) and Y = e^X > 0. Here g^{-1}(y) = \log y and \left|\frac{dg^{-1}}{dy}\right| = \frac{1}{y}, yielding the log-normal density: p_Y(y) = \frac{1}{y\sqrt{2\pi}}\exp\!\left(-\frac{(\log y)^2}{2}\right), \quad y > 0
  • Peak shift: Differentiating \log p_Y(y) = -\log y - \frac{1}{2}(\log y)^2 + C gives mode y = e^{-1} \approx 0.368.
  • Failure of naive p_X(\log y): Omitting 1/y puts the peak at y = 1 and encloses total area \int p_X(x)e^x\,dx = \mathbb{E}[e^X] = e^{1/2} \approx 1.65 \ne 1.

Multivariate Change of Variables and Normalizing Flows

Jacobian Determinants in Deep Generative Models

\log p_{\mathbf{Y}}(\mathbf{y}) = \log p_{\mathbf{X}}(\mathbf{x}) - \log\bigl|\det J_g(\mathbf{x})\bigr|

  • Local volume stretch: An invertible map \mathbf{y} = g(\mathbf{x}) expands volume by |\det J_g(\mathbf{x})|, so stacking L flow layers subtracts \sum_{\ell=1}^L \log|\det J_{g_\ell}|.
  • Triangular affine coupling (RealNVP): Setting y_1 = x_1 and y_2 = x_2 e^{s(x_1)} + t(x_1) makes J_g lower triangular with O(1) log-determinant s(x_1).

Coordinate Choice: The Dart in Polar Coordinates

Unknotting Dependence via a Jacobian

Express the dart’s Cartesian position (x, y) in polar coordinates (r, \theta) via (x, y) = (r\cos\theta, r\sin\theta):

J_g(r, \theta) = \begin{pmatrix} \frac{\partial x}{\partial r} & \frac{\partial x}{\partial \theta} \\ \frac{\partial y}{\partial r} & \frac{\partial y}{\partial \theta} \end{pmatrix} = \begin{pmatrix} \cos\theta & -r\sin\theta \\ \sin\theta & r\cos\theta \end{pmatrix} \implies \det J_g(r, \theta) = r(\cos^2\theta + \sin^2\theta) = r

  • Applying the multivariate change-of-variables rule from (x, y) (where p_{X,Y} = 1/\pi) to (r, \theta) \in [0, 1] \times [0, 2\pi): p_{R,\Theta}(r, \theta) = p_{X,Y}(r\cos\theta, r\sin\theta)\,\bigl|\det J_g(r, \theta)\bigr| = \frac{1}{\pi}\cdot r = \underbrace{2r}_{p_R(r)} \cdot \underbrace{\frac{1}{2\pi}}_{p_\Theta(\theta)}
  • Factorization into independent coordinates: In Cartesian coordinates, X and Y were coupled by the circular rim; in polar coordinates, the joint density factors cleanly into R \perp \Theta with p_R(r) = 2r and \Theta \sim \mathrm{Unif}[0, 2\pi).

Concept Check: Conditioning, Variance & Jacobians

Check Your Understanding

Test your grasp of joint distributions and density transformations on three problems:

  1. Explained variance (R^2) ceiling: Using Eve’s law and the optimal predictor decomposition, what is the maximum R^2 = 1 - \frac{\mathbb{E}[(X - h(Y))^2]}{\mathrm{Var}(X)} any model h(Y) can achieve when predicting the dart’s X from Y?
  2. Minimum correlation of n exchangeable variables: If Y_1, \dots, Y_n each have variance \sigma^2 and pairwise correlation \rho, use \mathrm{Var}(\sum_{i=1}^n Y_i) \ge 0 to find the smallest possible negative correlation \rho.
  3. Unbounded transformed density: If U \sim \mathrm{Unif}(0, 1), what is the density p_Y(y) of Y = U^2 on (0, 1)? Why does it blow up near y = 0 while still integrating to 1?

Answers:

  1. R^2 \le \frac{\mathrm{Var}(\mathbb{E}[X \mid Y])}{\mathrm{Var}(X)}. For the dart, \mathbb{E}[X \mid Y] = 0 is constant, so \mathrm{Var}(\mathbb{E}[X \mid Y]) = 0 and the ceiling is R^2 = 0 for every predictor h(Y).
  2. \mathrm{Var}(\sum_{i=1}^n Y_i) = n\sigma^2(1 + (n-1)\rho) \ge 0 \implies \boldsymbol{\rho \ge -\frac{1}{n-1}} (attained when \sum_{i=1}^n Y_i = 0, e.g., Y_2 = -Y_1 gives \rho = -1 for n=2).
  3. g^{-1}(y) = \sqrt{y} has derivative \frac{1}{2\sqrt{y}}, so p_Y(y) = \frac{1}{2\sqrt{y}} on (0, 1). Squaring strongly compresses intervals near 0, piling probability density to \infty as y \downarrow 0, yet \int_0^1 \frac{1}{2\sqrt{y}}\,dy = [\sqrt{y}]_0^1 = 1.

Summary: Joint Distributions and Transformations

Key Takeaways

Operation / Theorem Mathematical Formula Dart / Worked Benchmark Deep Learning Role
Marginalization p_X(x) = \int_{-\infty}^\infty p_{X,Y}(x, y)\,dy Flat disk 1/\pi \to semicircle \frac{2}{\pi}\sqrt{1-x^2} Integrating out latent variables
Conditioning & Independence p_{X\mid Y}(x\mid y) = \frac{p_{X,Y}(x,y)}{p_Y(y)}; X \perp Y \iff p_{X,Y} = p_X p_Y Uniform on chord \|x\| \le \sqrt{1-y^2} (dependent) Discriminative & autoregressive models
Optimal MSE Predictor \mathbb{E}[(X-h(Y))^2] = \mathbb{E}[\mathrm{Var}(X\mid Y)] + \mathbb{E}[(\mathbb{E}[X\mid Y]-h(Y))^2] Best predictor h^\star(Y) = \mathbb{E}[X \mid Y] Neural networks trained with L_2 loss

Summary: Joint Distributions and Transformations (cont.)

Key Takeaways

Operation / Theorem Mathematical Formula Dart / Worked Benchmark Deep Learning Role
Law of Total Variance \mathrm{Var}(X) = \mathbb{E}[\mathrm{Var}(X \mid Y)] + \mathrm{Var}(\mathbb{E}[X \mid Y]) Mixture: 1.00\text{ (within)} + 2.56\text{ (between)} = 3.56 ANOVA, R^2 ceiling & noise decomposition
Covariance & Sum Variance \mathrm{Cov}(X,Y) = \mathbb{E}[XY]-\mu_X\mu_Y; \mathrm{Var}(\bar{X}_n) = \sigma^2/n \mathrm{Cov}(X,Y)=0 on disk despite dependence Monte Carlo \sigma/\sqrt{n} & ensemble diversity
Covariance Matrix \boldsymbol{\Sigma} \boldsymbol{\Sigma} = \mathbb{E}[(\mathbf{X}-\boldsymbol{\mu})(\mathbf{X}-\boldsymbol{\mu})^\top]; \mathrm{Var}(\mathbf{a}^\top\mathbf{X}) = \mathbf{a}^\top\boldsymbol{\Sigma}\mathbf{a} \ge 0 Disk \boldsymbol{\Sigma} = \mathrm{diag}(1/4, 1/4) PCA, feature whitening & Mahalanobis metrics
Jacobian Density Rule \log p_{\mathbf{Y}}(\mathbf{y}) = \log p_{\mathbf{X}}(\mathbf{x}) - \log\|\det J_g(\mathbf{x})\| Log-normal peak e^{-1}; polar p_{R,\Theta} = 2r \cdot \frac{1}{2\pi} Normalizing flows & reparameterized densities