Contingency tables, Venn and tree diagrams – conditional probability in practice

Conditional probabilities are everywhere: the chance of surviving a shipwreck given sex and class, of a vaccine working, of a positive test meaning disease. The three classic tools for keeping track of them are the contingency table, the Venn diagram and the tree diagram. Here you can enter numbers yourself, turn trees round – and meet Simpson’s paradox, where a treatment is better in every single group yet worse overall.

Contingency tables

A contingency table (the term was introduced by Karl Pearson in 1904) counts how many fall into each combination of two categorical variables. Rows and columns are summed to margins. Three kinds of probability can be read from the table:

The classic example is the Titanic, which sank on the night of 14–15 April 1912. Of the 2201 on board (passengers and crew) 711 survived. The principle “women and children first” shows clearly in the table.

Try it: You can edit every number and name in the table. Switch between row and column percentages: $P(\text{survived}\,|\,\text{female}) = 73\ \%$, but $P(\text{female}\,|\,\text{survived}) = 48\ \%$ – two quite different questions. The vaccine trial is Pfizer–BioNTech’s phase 3 trial (Polack et al., NEJM 2020): 8 cases of covid-19 among about 18,200 vaccinated against 162 among about 18,300 given placebo. The relative risk is 0.05, i.e. an efficacy of $1 - 0.05 = 95\ \%$.

Risk, odds and independence

Let $p_1$ and $p_2$ be the probability of the outcome in the two rows. Then

$$\text{relative risk} = \frac{p_1}{p_2},\qquad \text{odds ratio} = \frac{p_1/(1-p_1)}{p_2/(1-p_2)} = \frac{ad}{bc}$$

where $a, b, c, d$ are the four cells. If the row variable and the column variable are independent, the row percentages are equal, and each cell is expected to contain $\text{row total}\times\text{column total}/\text{total}$. The χ² test (see statistical distributions) measures the distance between the observed and the expected counts; for a 2×2 table it has 1 degree of freedom. But remember: an association in a contingency table does not prove cause. That requires a randomised trial like the vaccine trial, where only the treatment distinguishes the groups.

Venn diagrams

A Venn diagram shows the same four cells as a 2×2 table: $A \cap B$, $A \cap \bar B$, $\bar A \cap B$ and $\bar A \cap \bar B$. It is good for seeing intersections and the addition rule, but less good for conditional probabilities, because the areas of the circles are rarely drawn to scale. The page on probability has an interactive, area-true Venn diagram, and on the page on logic you can shade regions for any expression in three sets.

Tree diagrams

A tree diagram shows an experiment in several stages. Each branch carries a conditional probability, and the probability of a whole path from root to leaf is the product of the branch probabilities (the multiplication rule):

$$P(A \cap B) = P(A)\,P(B\,|\,A)$$

The branches from each node sum to 1, and the leaves together sum to 1. The tree naturally follows the order in which things happen: first one is ill or well, then the test turns out positive or negative. But often one wants to know the reverse – and then the tree must be turned round.

Try it: On the left the tree in its natural order, on the right the same experiment with B first. The leaves on the right are the same four numbers as on the left, merely in a different order – they are the four cells of the contingency table. Choose “Medical test”: 1 % are ill, the test detects 90 % of them and gives a false alarm in 9 % of the healthy. The reversed tree shows that only about 9 % of those testing positive are actually ill. “Urn” is two draws without replacement from an urn with 3 red and 2 blue balls: after a red one there are only 2 red left among 4.

The law of total probability and Bayes’ theorem

The probability of $B$ is found by adding all the paths to $B$:

$$P(B) = P(A)\,P(B\,|\,A) + P(\bar A)\,P(B\,|\,\bar A)$$

and turning the tree round is Bayes’ theorem:

$$P(A\,|\,B) = \frac{P(A)\,P(B\,|\,A)}{P(B)}$$

It has a page of its own: Bayes’ theorem. A good rule of thumb from the psychologist Gerd Gigerenzer: calculate in natural frequencies – “out of 1000 people 10 are ill; 9 of them test positive; of the 990 healthy, 89 test positive” – and the answer $9/(9+89) \approx 9\ \%$ becomes almost self-evident. The contingency table is precisely natural frequencies.

Simpson’s paradox

An association that holds in every single subgroup can reverse when the groups are combined. The phenomenon was described by Edward Simpson in 1951 (and before him by Udny Yule in 1903). A famous example is a British study of the treatment of kidney stones (Charig et al., British Medical Journal 1986): open surgery (A) had a higher success rate than keyhole surgery (B) for both small and large stones – but a lower one overall.

Try it: The explanation is on the right: the doctors preferentially gave the gentler keyhole surgery to patients with small stones, which are easy to treat, and open surgery to the difficult cases. Stone size is a confounder – a background variable that affects both the choice of treatment and the outcome. Another famous example is admissions to the University of California, Berkeley in 1973: overall a smaller proportion of the female applicants were admitted, but in most individual departments the women did as well or better. The women applied mainly to the departments with many applicants per place.

Good advice

Ask: percentage of what?

Row and column percentages answer different questions. “90 % of the ill test positive” is not the same as “90 % of those testing positive are ill”.

Count before you calculate

Rewrite probabilities as numbers in an imagined group of, say, 1000 or 100,000 people. Then contingency table, tree and Bayes become simple counting.

Look for confounders

Before concluding that A leads to B, ask whether a third variable affects both. Randomisation is the surest cure.

Absolute and relative risk

“Risk halved” sounds like a lot, but from 2 in 10,000 to 1 in 10,000 is an absolute difference of 1 in 10,000. State both.

Further reading