induwara.lk
induwara.lkAI · Information Theory

Mutual Information Calculator

Calculate the mutual information I(X;Y) between two discrete variables from a count or joint-probability table, with the full breakdown — marginal, joint and conditional entropies, normalized MI, and every per-cell term. All in your browser, no signup, matched to scikit-learn.

By Induwara AshinsanaUpdated Jul 10, 2026
Mutual information calculator
Input mode
Log base
Contingency table (rows = X, columns = Y)
rows
cols
Row and column labelsX \ Y

Enter how many observations fall in each X-by-Y cell. Counts are divided by the total to form the joint distribution.

Presets
I(X;Y) — bits
0.1245
Shared information · 0.1245 bits
NMI (arithmetic)
0.1263
2I / (H(X)+H(Y)) · 12.63%
NMI (min)
0.1282
I / min(H(X),H(Y)) · 12.82%
Table size
2 × 2
Normalised to sum 1
Decimals
Entropies (bits)H(X) = 0.9710H(Y) = 1.0000H(X,Y) = 1.8464H(X|Y) = 0.8464H(Y|X) = 0.8755

Information split

H(X|Y)
H(Y|X)

H(X,Y) = H(X|Y) + I(X;Y) + H(Y|X) = 0.8464 + 0.1245 + 0.8755 = 1.8464 bits

Counts were divided by the total (N = 100) to form the joint probability table.

Cross-check. Summing the per-cell terms gives 0.1245 bits; the independent identity H(X) + H(Y) − H(X,Y) = 0.9710 + 1.0000 1.8464 = 0.1245. They reconcile, as they must.

Per-cell working — I(X;Y)

x, yp(x,y)p(x)·p(y)ratiolog()p(x,y)·log
1, 10.30000.20001.50000.58500.1755
1, 20.10000.20000.5000-1.0000-0.1000
2, 10.20000.30000.6667-0.5850-0.1170
2, 20.40000.30001.33330.41500.1660
I(X;Y) total (bits)0.1245

Method: I(X;Y) = Σ p(x,y)·log[p(x,y)/(p(x)p(y))], base-2 → bits, base-e → nats, with 0·log0 = 0— the same plug-in estimator as scikit-learn's mutual_info_score. Sources cited below the tool. No data leaves this page.

How it works

Mutual information quantifies how much two variables tell you about each other. It ties together the rest of information theory: it is the shared region between the two entropies H(X) and H(Y), and it equals the drop in one variable's uncertainty once the other is known. Following Cover & Thomas, for a joint distribution p(x,y) over discrete X (rows) and Y (columns) it is defined as:

I(X;Y) = Σₓ Σᵧ p(x,y) · log_b[ p(x,y) / (p(x)·p(y)) ]

The base of the logarithm sets the unit — base 2 gives bits, base e (the natural log) gives nats, converting with 1 nat = 1/ln2 ≈ 1.4427 bits. The calculator follows the plug-in estimator used by scikit-learn:

  1. Read the grid into a matrix. In counts mode, divide every cell by the grand total N to get the joint probabilities p(x,y). In probabilities mode, optionally rescale so the table sums to 1. Reject negative or empty cells and tables smaller than 2×2.
  2. Sum across each row for the marginal p(x), and down each column for p(y). These are the distributions of X and Y on their own.
  3. Compute the entropies with the convention 0·log0 = 0: H(X) = −Σ p(x) log p(x), likewise H(Y), and the joint H(X,Y) = −Σ p(x,y) log p(x,y).
  4. Sum p(x,y)·log[p(x,y)/(p(x)·p(y))] over every cell for I(X;Y). Whenever p(x,y) > 0 both marginals are at least that large, so the ratio is positive and the result is always finite — unlike KL divergence, mutual information never diverges.
  5. Derive the conditional entropies H(X|Y) = H(X,Y) − H(Y) and H(Y|X) = H(X,Y) − H(X), and the normalized MI: the arithmetic-mean form 2I/(H(X)+H(Y)) and the min form I/min(H(X),H(Y)), both clamped to 0 when a variable is degenerate.

As a built-in correctness check, the tool computes I(X;Y) two independent ways — the direct per-cell sum above and the entropy identity I = H(X) + H(Y) − H(X,Y) — and shows them reconciling. Two facts double as sanity signals: mutual information is always ≥ 0, and it is exactly 0 if and only if X and Y are independent. Because it is a plug-in estimate, MI is biased slightly upward on small samples; this version applies no Miller–Madow or other bias correction, matching mutual_info_score.

Worked examples

Spam-word contingency table (bits)

Counts [[30, 10], [20, 40]], N = 100, base 2

  1. Joint p = [[0.30, 0.10], [0.20, 0.40]]
  2. Marginals p(x) = [0.40, 0.60]; p(y) = [0.50, 0.50]
  3. H(X) = −(0.4·log₂0.4 + 0.6·log₂0.6) = 0.970951 bits
  4. H(Y) = 1 bit; H(X,Y) = 1.846439 bits
  5. I(X;Y) = 0.970951 + 1 − 1.846439 = 0.124511 bits
  6. Direct sum: 0.175489 − 0.100000 − 0.116993 + 0.166015 = 0.124511 ✓
  7. NMI_arith = 2·0.124511/1.970951 = 0.126345

Perfect dependence (bits)

Joint probs [[0.5, 0], [0, 0.5]], base 2

  1. Marginals p(x) = p(y) = [0.5, 0.5] ⇒ H(X) = H(Y) = 1 bit
  2. H(X,Y) = −(0.5·log₂0.5 + 0.5·log₂0.5) = 1 bit
  3. I(X;Y) = 1 + 1 − 1 = 1 bit (X determines Y)
  4. NMI_arith = 2·1/(1+1) = 1.0 — maximal dependence

Degenerate Y — constant column (edge case)

Joint probs [[0.5, 0], [0.5, 0]], base 2

  1. p(y) = [1, 0] ⇒ H(Y) = 1·log1 + 0·log0 = 0 bits
  2. H(X) = 1 bit; H(X,Y) = 1 bit
  3. I(X;Y) = 1 + 0 − 1 = 0 bits (a constant shares no information)
  4. NMI_min = I/min(1,0) = 0/0 → reported as 0 (guarded)

Frequently asked questions

Sources & references

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.