🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
Search courses…
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Mutual Information

Information TheoryMI🟢 Free Lesson

Advertisement

Mutual Information


Core Definitions


Key Formulas


Properties and Theorems


Worked Examples


Python Implementation

import numpy as np
from collections import Counter
from sklearn.feature_selection import mutual_info_regression, mutual_info_classif

def mutual_information(x, y, base=2):
    """Compute MI between two discrete arrays."""
    n = len(x)
    x, y = np.array(x), np.array(y)

    # Joint distribution
    xy = np.stack([x, y], axis=1)
    xy_counter = Counter(map(tuple, xy))
    pxy = np.array([count / n for count in xy_counter.values()])

    # Marginals
    x_counter = Counter(x)
    y_counter = Counter(y)
    px = np.array([x_counter[k] / n for k in xy_counter.keys().__iter__()])
    # Rebuild properly
    px = np.array([sum(pxy[i] for i in range(len(pxy))
                       if list(xy_counter.keys())[i][0] == k)
                   for k in x_counter.keys()])

    # Simpler approach: compute from joint
    joint = np.zeros((len(set(x)), len(set(y))))
    x_vals, y_vals = sorted(set(x)), sorted(set(y))
    x_map = {v: i for i, v in enumerate(x_vals)}
    y_map = {v: i for i, v in enumerate(y_vals)}
    for xi, yi in zip(x, y):
        joint[x_map[xi], y_map[yi]] += 1
    joint /= n

    # MI from joint
    mi = 0.0
    for i in range(joint.shape[0]):
        for j in range(joint.shape[1]):
            if joint[i, j] > 0:
                px = joint[i].sum()
                py = joint[:, j].sum()
                mi += joint[i, j] * np.log2(joint[i, j] / (px * py))
    return mi

def conditional_mi(x, y, z):
    """Estimate I(X; Y | Z) by averaging I(X; Y) within each Z value."""
    z_vals = np.unique(z)
    total = 0.0
    for z_val in z_vals:
        mask = z == z_val
        if mask.sum() > 1:
            total += mutual_information(x[mask], y[mask]) * mask.mean()
    return total

# --- Feature Selection Example ---
np.random.seed(42)
X = np.random.randn(1000, 5)
y = X[:, 0] * 2 + X[:, 1] * 0.5 + np.random.randn(1000) * 0.1

mi_scores = mutual_info_regression(X, y, random_state=42)
print("MI scores per feature:")
for i, score in enumerate(mi_scores):
    print(f"  Feature {i}: {score:.4f}")

top_features = np.argsort(mi_scores)[::-1][:3]
print(f"Top 3 features: {top_features}")
# Feature 0 and 1 should have highest MI

Applications in AI/ML


Common Mistakes

MistakeWhy It's WrongCorrect Approach
Using correlation instead of MICorrelation only captures linear dependenciesUse MI to capture any type of dependency
Assuming MI = causationMI measures association, not causationMI is a necessary but not sufficient condition for causation
Ignoring the sign of MIMI is always non-negativeIf you get a negative MI estimate, check your implementation
Not handling continuous variablesMI estimation for continuous data requires binning or k-NNUse mutual_info_regression from sklearn
Confusing MI with conditional MIConditional MI accounts for the effect of a third variable

Interview Questions

Q1: What's the difference between MI and correlation? A: Correlation measures linear dependence only. MI captures any type of dependence (linear, nonlinear, monotonic, etc.). MI = 0 if and only if variables are independent. Correlation = 0 only implies no linear relationship.

Q2: Why is MI symmetric but correlation can be asymmetric in causal interpretation? A: MI is a mutual measure— by definition. Correlation is also symmetric, but causal relationships are directional. MI measures shared information, not causal direction.

Q3: How do you estimate MI for continuous variables? A: Options include: (1) binning/discretization, (2) k-NN based estimators (Kragh & Pethel), (3) kernel density estimation. sklearn's mutual_info_regression uses k-NN estimation.

Q4: What is the information bottleneck? A: It's a method to find a compressed representation of that maximizes while minimizing . The Lagrange multiplier controls the trade-off between compression and prediction.

Q5: Can MI be greater than the entropy? A: No. . MI cannot exceed the entropy of either variable.


Practice Problems


Quick Reference

QuantityFormulaNotes
MI (entropy form)Reduction in uncertainty
MI (KL form)Distance from independence
Chain ruleDecompose multi-variable MI
Conditional MIMI after controlling for Z
Data processingProcessing cannot add info

Cross-References

  • 081 - Entropy — MI is defined in terms of entropy.
  • 083 - KL Divergence: — MI is a special case of KL divergence.
  • 084 - Cross-Entropy — Cross-entropy loss minimizes , related to MI through .
  • 085 - Applications — Feature selection, information bottleneck, decision trees.

Summary

Need Expert Mathematics Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement