Measuring Relationships: From Inner Products to Attention · Chapter 4
Mutual information: dependence beyond linearity
Zero correlation does not mean independence. Mutual information measures any form of statistical dependence through probability distributions. This chapter derives its three equivalent definitions and its non-negativity, shows that for Gaussians it is fully determined by the correlation coefficient, reads a confusion matrix as a joint distribution, and shows how it drives camera–LiDAR calibration.
Series contentsChapter 4 of 4
Series
Measuring Relationships: From Inner Products to Attention- 01Inner products, covariance and correlation
- 02Cross-correlation and convolution: the sliding inner product
- 03From convolution to attention
- 04Mutual information: dependence beyond linearity
The question
Chapter 1 showed that for the covariance is zero although is fully determined by . We need a quantity that meets two conditions: it is zero exactly when the variables are independent, and it does not care what form the dependence takes. That quantity is mutual information.
This chapter also answers two questions left open by the series overview: how confusion matrices relate to these concepts, and how to tell apart the three levels of relationship measures, computational mechanisms and evaluation tools.
Intuition
Before observing we are uncertain about . Observing reduces that uncertainty, and the size of the reduction is the mutual information.
- If and are independent, knowing does not help and the mutual information is zero.
- For , knowing removes all uncertainty about , so the mutual information equals the full uncertainty of .
Another view: mutual information measures how far the joint distribution is from what it would be under independence, . Correlation looks only at the linear trend of a scatter plot; mutual information looks at the whole shape of the joint distribution.
Formalism
Entropy
For a discrete random variable, the entropy is
with joint entropy and conditional entropy defined analogously. With base-2 logarithms the unit is the bit.
Three equivalent definitions
First equals third. Using :
First equals second. This follows directly from the chain rule .
The third form is a Kullback–Leibler divergence,
and it is symmetric in and .
Non-negativity and independence
For distributions and , sum over the support of . Since is concave, Jensen’s inequality gives
Equality holds exactly when is constant on the support, that is, when .
Hence , and exactly when for all , which is independence.
This is precisely what correlation cannot offer: zero correlation rules out only linear dependence.
Back to Y = X²
Let be uniform on and . By Chapter 1 their covariance is zero.
is 0 with probability and 1 with probability . Given , is certain, so and
The Gaussian case: the bridge between correlation and information
For continuous variables, sums become integrals and entropy becomes differential entropy ; the definition of mutual information keeps the same form. Unlike differential entropy itself, mutual information stays non-negative.
It is also invariant under separate invertible transformations of the two variables, . Correlation, by comparison, is invariant only under affine maps.
A -dimensional Gaussian has differential entropy .
For a bivariate Gaussian with standard deviations and correlation , . Substituting:
Three consequences follow:
- For jointly Gaussian variables, mutual information is a monotone function of alone; the correlation coefficient already carries all the dependence.
- independence. This is the special case in which zero correlation does imply independence.
- As , . A deterministic relationship between continuous variables carries infinite information; the finite values seen in practice come from discretization or finite samples.
A confusion matrix is a joint distribution table
With classes and samples, let count samples with true label and predicted label , and set .
The result is the empirical joint distribution of : row sums give the true class distribution and column sums the predicted one. Then
Two binary examples, each with 50 samples per class, so bit:
| Confusion matrix | Accuracy | |
|---|---|---|
| 0.85 | ≈ 0.397 bit | |
| 0 | 1 bit |
The second classifier swaps every label. Its accuracy is zero, yet its mutual information reaches the maximum .
The reason is that mutual information measures how much the prediction tells us about the truth, not whether the prediction is correct, and swapping labels loses no information. This is why mutual information cannot replace accuracy, precision or recall as an evaluation metric, and it motivates the level distinction below.
Three levels
| Level | Concepts | Question answered |
|---|---|---|
| Relationship measure | Covariance, correlation, mutual information | How much linear or general dependence exists between two quantities? |
| Computational mechanism | Cross-correlation, convolution, attention | How does a model compute and use that relationship? |
| Evaluation tool | Confusion matrix | What does the joint distribution of prediction and truth look like? |
A tool at one level can be read in the language of another, as when mutual information is computed from a confusion matrix. That does not make them the same kind of object.
The thread of this series connects them: from “do two vectors look alike?” (the inner product) to “how much statistical dependence do two random variables share?” (mutual information).
In robotics
Camera–LiDAR extrinsic calibration
The goal is to recover the LiDAR-to-camera extrinsics without a calibration target. Pandey et al. (2012) proceed as follows.
For a candidate , project every LiDAR point into the image with the camera intrinsics , at pixel . Collect pairs of the point’s reflectivity and the gray value at its projection. Estimate the joint distribution of with a histogram or kernel density, compute the mutual information , and solve
Why does this work? With the correct extrinsics, each pair comes from the same physical surface point, so reflectivity and brightness are statistically dependent. With wrong extrinsics, pairs come from mismatched surfaces and are closer to independent.
Why not correlation? The relationship between reflectivity and gray value changes with material and lighting; it need not be linear or even monotonic. Mutual information requires only that some dependence exists, not a particular functional form.
The same idea appeared earlier in multimodal medical image registration, such as CT to MR, in the work of Viola and Wells (1997) and Maes et al. (1997).
Practical caveats in estimation
- Histogram (plug-in) estimators depend on the number of bins and are biased upward for small samples; truly independent variables still give a positive estimate. The -nearest-neighbour KSG estimator (Kraskov et al., 2004) is a common alternative.
- Optimization: as a function of , the mutual-information objective is non-convex, so a reasonable initial guess is needed, for example from a CAD model or hand measurement.
Experiment
The script below checks three things in turn: histogram estimates against the Gaussian formula; the case; and mutual information computed from a confusion matrix.
import numpy as np
rng = np.random.default_rng(0)
def mi_from_joint(P):
"""Mutual information in bits from an (unnormalized) joint count or probability table."""
P = P / P.sum()
px = P.sum(axis=1, keepdims=True)
py = P.sum(axis=0, keepdims=True)
nz = P > 0
return float((P[nz] * np.log2(P[nz] / (px @ py)[nz])).sum())
def mi_hist(x, y, bins=32):
H, _, _ = np.histogram2d(x, y, bins=bins)
return mi_from_joint(H)
# 1. Gaussian: histogram estimate vs -0.5*log2(1 - rho^2)
n = 200_000
for rho in [0.0, 0.5, 0.9]:
x = rng.standard_normal(n)
y = rho * x + np.sqrt(1 - rho**2) * rng.standard_normal(n)
print(rho, round(mi_hist(x, y), 3), round(-0.5 * np.log2(1 - rho**2), 3))
# estimates sit slightly above the theory (0, 0.208, 1.198); even rho = 0 gives a small positive value
# 2. Y = X^2: correlation near 0, large mutual-information estimate
x = rng.uniform(-1, 1, n)
print(np.corrcoef(x, x**2)[0, 1].round(3), round(mi_hist(x, x**2), 3))
# the true value is infinite; the estimate grows with the number of bins
# 3. confusion matrices
C = np.array([[40, 10], [5, 45]])
print(round(mi_from_joint(C), 3), np.trace(C) / C.sum()) # about 0.397, 0.85
S = np.array([[0, 50], [50, 0]])
print(round(mi_from_joint(S), 3), np.trace(S) / S.sum()) # 1.0, 0.0
References
- Shannon, C. E. (1948). A mathematical theory of communication. Bell System Technical Journal, 27(3), 379–423; 27(4), 623–656.
- Cover, T. M., & Thomas, J. A. (2006). Elements of Information Theory (2nd ed.). Wiley.
- Viola, P., & Wells, W. M. (1997). Alignment by maximization of mutual information. International Journal of Computer Vision, 24(2), 137–154.
- Maes, F., Collignon, A., Vandermeulen, D., Marchal, G., & Suetens, P. (1997). Multimodality image registration by maximization of mutual information. IEEE Transactions on Medical Imaging, 16(2), 187–198.
- Pandey, G., McBride, J. R., Savarese, S., & Eustice, R. M. (2012). Automatic targetless extrinsic calibration of a 3D lidar and camera by maximizing mutual information. AAAI Conference on Artificial Intelligence.
- Kraskov, A., Stögbauer, H., & Grassberger, P. (2004). Estimating mutual information. Physical Review E, 69(6), 066138.
- StatQuest (YouTube channel): introductory videos on entropy and mutual information. https://www.youtube.com/@statquest