Mutual Information Calculator

Information theory

Mutual information (bits)0.278
Joint entropy H(X, Y) (bits)1.722
Normalised MI (share of smaller entropy)0.278
Mutual information (nats)0.193
How this was calculated
  1. Inputs: the 2 x 2 table holds 40, 10, 10 and 40 observations, 100 in total.
  2. Marginals: X splits 0.500 / 0.500 and Y splits 0.500 / 0.500.
  3. Cell terms: each cell adds p x log2(p / (px x py)), and the four terms sum to 0.278 bits.
  4. Mutual information result: 0.278 bits - 0.278 of the smaller variable entropy.

Correlation asks whether two variables move together in a straight line. Mutual information asks the broader question: how much does knowing one variable tell you about the other, of any shape at all? It comes from Shannon's information theory, and its unit is the bit - the amount of uncertainty removed, on average, when one variable is revealed. A fair coin's worth of dependence is a large effect; most real pairs of variables share far less, which is why the scale needs the precision this calculator keeps.

Enter the four counts of a 2 x 2 contingency table - the joint tally of two binary variables - and the page returns the mutual information in bits, the joint entropy of the pair, the same information in nats, and a normalised version scaled by the smaller variable's own entropy. The examples include a strongly associated table, the independence benchmark, and a mildly associated table with uneven margins.

Worked examples

Mutual information of a diagonal table

(40 · 10 · 10 · 40)

Mutual information (bits)0.278
Joint entropy H(X, Y) (bits)1.722
Normalised MI (share of smaller entropy)0.278
Mutual information (nats)0.193

Mutual information at independence

(25 · 25 · 25 · 25)

Mutual information (bits)0.000
Joint entropy H(X, Y) (bits)2.000
Normalised MI (share of smaller entropy)0.000
Mutual information (nats)0.000

Mutual information with uneven margins

(30 · 5 · 15 · 50)

Mutual information (bits)0.279
Joint entropy H(X, Y) (bits)1.648
Normalised MI (share of smaller entropy)0.299
Mutual information (nats)0.193

Information shared between two variables

The definition runs through entropy. Each variable alone has an entropy - how uncertain its outcome is. Together they have a joint entropy. If the variables were independent, the joint entropy would be exactly the sum of the two separate entropies; every bit by which it falls short of that sum is information the variables share, and that shortfall is the mutual information. The first worked example's table - 40, 10, 10, 40 - has a joint entropy of 1.722 bits against two variables worth a bit each, so the shared information is 0.278 bits.

Computing it cell by cell tells the same story in finer grain. Each cell contributes its probability times the log of the ratio between its actual probability and the probability independence would predict from the margins. Cells that occur more often than independence predicts contribute positively; cells starved by the association contribute negatively; and the weighted sum is the mutual information. A diagonal-heavy table like the first example is rich in over-populated diagonal cells, which is where its 0.278 bits come from.

The measure is symmetric - the information X gives about Y equals the information Y gives about X - and it detects any dependence, curved, thresholded or otherwise, that a correlation coefficient would flatten or miss entirely. That generality is why mutual information is the workhorse dependence measure in feature selection, neuroscience and anywhere else relationships refuse to be straight lines.

  • MI = H(X) + H(Y) - H(X, Y): the shortfall of the joint entropy below the sum of the parts.
  • Zero means statistical independence; there is no negative mutual information.
  • Bits use log base 2; nats use the natural log and are smaller by a factor of ln 2.
  • Normalised MI divides by the smaller entropy, bounding the score between 0 and 1.

Building the marginals from the table

Everything starts with the row and column totals. In the third worked example the table 30, 5, 15, 50 gives X a split of 0.350 to 0.650 and Y a split of 0.450 to 0.550 - uneven margins that matter, because independence with uneven margins already predicts an uneven table. The association is only what remains after those margins have had their say, and the third example's remainder is 0.279 bits: nearly the same as the first example's 0.278, reached by a completely different route - lopsided margins with a strong diagonal rather than even margins with a stronger one.

The second example is the calibration point everyone should run once. A table of 25 in every cell has margins of exactly half and half, every cell sits exactly where independence predicts, and the mutual information is 0.000 bits while the joint entropy reaches its maximum of 2.000 bits for two binary variables. Any software that does not return exactly zero on that table has a bug or a bias correction switched on; this page returns the plain, uncorrected statistic.

Sample size deserves a word. The plug-in estimator used here is biased upward in small samples - random noise manufactures apparent association - and with only a hundred observations, as in all three examples, a reading like 0.279 bits should be compared against what shuffled versions of the same table produce before it is called a discovery. The statistic is exact; its interpretation at small counts is where care belongs.

TableJoint entropyMI (bits)Normalised MI
40 / 10 / 10 / 401.7220.2780.278
25 / 25 / 25 / 252.0000.0000.000
30 / 5 / 15 / 501.6480.2790.299
Run the independence benchmark through any MI tool before trusting it: an even 25 in every cell must return exactly 0.000 bits.

Worked examples

Example 1: Cell a: X = 0, Y = 0 40; Cell b: X = 0, Y = 1 10; Cell c: X = 1, Y = 0 10; Cell d: X = 1, Y = 1 40. Mutual information (bits) = 0.278. Supporting values: Joint entropy H(X, Y) (bits) 1.722; Normalised MI (share of smaller entropy) 0.278; Mutual information (nats) 0.193. Inputs: the 2 x 2 table holds 40, 10, 10 and 40 observations, 100 in total. Marginals: X splits 0.500 / 0.500 and Y splits 0.500 / 0.500. Cell terms: each cell adds p x log2(p / (px x py)), and the four terms sum to 0.278 bits. Mutual information result: 0.278 bits - 0.278 of the smaller variable entropy.

Example 2: Cell a: X = 0, Y = 0 25; Cell b: X = 0, Y = 1 25; Cell c: X = 1, Y = 0 25; Cell d: X = 1, Y = 1 25. Mutual information (bits) = 0.000. Supporting values: Joint entropy H(X, Y) (bits) 2.000; Normalised MI (share of smaller entropy) 0.000; Mutual information (nats) 0.000. Inputs: the 2 x 2 table holds 25, 25, 25 and 25 observations, 100 in total. Marginals: X splits 0.500 / 0.500 and Y splits 0.500 / 0.500. Cell terms: each cell adds p x log2(p / (px x py)), and the four terms sum to 0.000 bits. Mutual information result: 0.000 bits - 0.000 of the smaller variable entropy.

Example 3: Cell a: X = 0, Y = 0 30; Cell b: X = 0, Y = 1 5; Cell c: X = 1, Y = 0 15; Cell d: X = 1, Y = 1 50. Mutual information (bits) = 0.279. Supporting values: Joint entropy H(X, Y) (bits) 1.648; Normalised MI (share of smaller entropy) 0.299; Mutual information (nats) 0.193. Inputs: the 2 x 2 table holds 30, 5, 15 and 50 observations, 100 in total. Marginals: X splits 0.350 / 0.650 and Y splits 0.450 / 0.550. Cell terms: each cell adds p x log2(p / (px x py)), and the four terms sum to 0.279 bits. Mutual information result: 0.279 bits - 0.299 of the smaller variable entropy.

Bits, nats and normalised MI

Bits and nats are the same information in different units, like inches and centimetres. The conversion is a multiplication by the natural log of 2, so the first example's 0.278 bits is 0.193 nats. Machine learning papers tend to work in nats because the natural log plays well with calculus; information theorists and anyone explaining the number to humans tend to prefer bits, where one bit is the answer to one yes-no question.

Normalised MI answers a different need: comparability. Raw mutual information is capped by the smaller variable's entropy, so a pair of skewed variables can never reach the values an even pair can. Dividing by that smaller entropy - 0.299 in the third example, where the cap bites - expresses the association as a share of the maximum the margins allowed. For comparing associations across datasets with different margins, the normalised figure is usually the fairer one; for measuring information in absolute terms, bits remain the honest unit.

Mutual information versus correlation

A correlation coefficient sees one pattern - a straight line - and reports its strength. Mutual information sees every pattern and reports total dependence. A variable that determines another through a U-shape can have near-zero correlation and large mutual information; two variables locked in a straight line max out both measures together. Neither statistic is the upgrade of the other; they are answers to different questions, and choosing between them is choosing what kind of relationship you are hunting.

The price of MI's generality is data hunger and opacity. It needs enough counts to fill the table - this 2 x 2 version is the most frugal case - and it does not tell you the direction or shape of what it found. The professional workflow is often sequential: mutual information to screen for dependence worth investigating, then a shape-aware method to characterise it. The 0.279 bits in the third example is an invitation to look closer, not a conclusion about how the variables relate.

Where the 2 x 2 case is used

Two binary variables are everywhere: a test result against a true diagnosis, a feature's presence against a class label, a treatment against an outcome. In feature selection for classification, ranking candidate features by their mutual information with the label is a standard first pass, and the numbers look exactly like this page's examples - a few tenths of a bit for a genuinely informative feature, 0.000 for noise. In diagnostic research the same table underlies the likelihood machinery, and MI offers a single symmetric summary of the whole table.

Whatever the application, report the table with the statistic. A mutual information of 0.278 bits from the table 40, 10, 10, 40 is a checkable claim; the same figure quoted alone is an assertion. The four counts are the evidence, and this page is built so that the evidence and the answer always travel together.

FAQs

What does mutual information measure?

How much knowing one variable reduces uncertainty about another, in bits. It is the amount by which the pair's joint entropy falls short of the sum of the two separate entropies, and it detects dependence of any shape, not just straight-line association.

How much mutual information does the first table carry?

The table 40, 10, 10, 40 carries 0.278 bits. Its joint entropy is 1.722 bits against two one-bit variables, and the normalised MI is also 0.278 because both margins are even.

What does the second example prove?

That the statistic is calibrated at independence. With 25 in every cell, every cell sits exactly where the margins predict, the joint entropy reaches its maximum of 2.000 bits, and the mutual information is exactly 0.000.

Why does the third table need normalising?

Its margins are uneven - X splits 0.350 to 0.650 - which caps the raw MI below what even margins allow. Its raw value is 0.279 bits, and dividing by the smaller entropy gives a normalised MI of 0.299, the fairer figure for comparisons across datasets.

What is the difference between bits and nats?

Only the logarithm base. Bits use log base 2, nats use the natural log; multiply bits by ln 2 to get nats. The first example's 0.278 bits is 0.193 nats - the same information in smaller units.

Can mutual information be negative?

No. Unlike a correlation coefficient it has no sign and no negative range: independence is exactly zero and any dependence adds a positive amount. Direction and shape must be read from the table itself, which is why this page always shows the counts with the result.

Sources

Related calculators

KL Divergence Calculator
Kullback-Leibler divergence between two Bernoulli (binary) distributions in nats and bits, with the reverse divergence and the symmetrised Jeffreys sum - the information loss when Q stands in for P.
Jensen-Shannon Divergence Calculator
Jensen-Shannon divergence between two Bernoulli (binary) distributions: the bounded, symmetric cousin of KL divergence, reported in bits with the Jensen-Shannon distance.
Shannon Entropy Calculator
Shannon entropy of a probability distribution in bits, with the maximum entropy for the outcome count, efficiency and perplexity.