Polson–Sokolov Lab · Joint, marginal and conditional probability

Joint Table Lab

Edit the joint table and watch the row and column totals (the marginals) update. Click a row or column heading to condition on it: the bars show the conditional distribution next to the marginal, and the set picture keeps only that group. The cards below test independence and compare \(P(A \mid B)\) with \(P(B \mid A)\).

← All labs

Based on Chapter 1, Probability and Uncertainty, of Polson and Sokolov, Bayes, Artificial Intelligence, and Deep Learning (vsokolov.org/html/_book).

How to read the table

Each cell is the share of everyone who has both that salary and that happiness. The four rows run from low salary to very high. The three columns run from low happiness to high. The entries already add to 1.

Low happinessMediumHighShare of everyone
Low salary0.030.120.070.22
Medium0.020.130.110.26
High0.010.130.140.28
Very high0.010.090.140.24
Share of everyone0.070.470.461

The outlined cell, 0.14, is the 14% of people with a very high salary who also report high happiness. The rightmost column ignores happiness: 24% have a very high salary. The bottom row ignores salary: 46% report high happiness, and only 7% report low happiness. Salary itself is spread fairly evenly.

Keeping the very high salary row and dividing by its total, 0.24, gives about 0.04, 0.38, and 0.58. High happiness in that group is 0.14 / 0.24 = 0.583, above the 0.46 for everyone. Across the four salaries, the share reporting high happiness is about 0.32, 0.42, 0.50, and 0.58. Higher salary goes with a larger share reporting high happiness, which is why the cuts in the picture do not line up. If salary told you nothing, every row would split 0.07, 0.47, 0.46.

The same 0.14 answers a second question when you divide by the high-happiness column instead of the row. Among people who report high happiness, the share with a very high salary is 0.14 / 0.46 = 0.304. High happiness given a very high salary is 0.583. A very high salary given high happiness is 0.304. One cell, two groups, two totals.

Conditional distribution (the clicked row or column, rescaled) Marginal distribution (no conditioning) Cell A and B

Joint: the overlap

Conditional: inside one group

Independence check

If X and Y are independent, every cell equals its row total times its column total. The third table shows how far each actual cell is from that product.

P(A | B) is not P(B | A)

P(A | B)–
P(B | A)–
P(A and B)–
P(A)–
P(B)–
Base-rate ratio P(A) / P(B)–

Reading a joint table

Three tables in one: joint, marginal, conditional

Every probability in this topic can be read off one joint table. The joint is a cell, a marginal is a row or column total, and a conditional is one row or column rescaled so it adds to 1.

  1. Joint: both happen

    The cell in row \(X = 3\), column \(Y = 2\) holds \(P(X = 3, Y = 2) = 0.14\). Of all people, 14% are very high earners who also report high happiness. In the set picture that cell is one block, and its area is that 14%.

  2. Marginal: add up the joint

    The row total \(P(X = 3) = 0.01 + 0.09 + 0.14 = 0.24\) ignores happiness. The column total \(P(Y = 2) = 0.46\) ignores salary.

  3. Conditional: keep one row, rescale it

    Knowing \(X = 3\) removes every other row. Dividing the row by its total gives \(P(Y = 2 \mid X = 3) = 0.14 / 0.24 = 0.583\). That beats 0.46, so very high earners are more likely to report high happiness. The right-hand set is that one group, stretched to fill the square.

  4. Independence: the conditional equals the marginal

    If every salary row, once rescaled, looked like the marginal row, salary would tell you nothing about happiness. Then each cell equals \(P(x)\cdot P(y)\). In this table the rows do not match, and the cuts in the picture do not line up.

Product rule
\(\displaystyle P(x, y) = P(x)\cdot P(y \mid x) = P(y)\cdot P(x \mid y)\)
Marginal = sum of joint
\(\displaystyle P(x) = \sum_y P(x, y), \quad P(y) = \sum_x P(x, y)\)
Independence
\(\displaystyle P(y \mid x) = P(y) \ \text{for all } x, y\)
\(\displaystyle \iff P(x, y) = P(x)\cdot P(y)\)
Bayes corrects the flip
\(\displaystyle P(A \mid B) = P(B \mid A) \times \frac{P(A)}{P(B)}\)

Takeaway for students. \(P(A \mid B)\) and \(P(B \mid A)\) share the same numerator, \(P(A \text{ and } B)\). They divide it by different totals. High happiness given very high salary is 0.583. Very high salary given high happiness is 0.304. Same cell, two different questions.

Try this in class

  1. Click each salary row in turn. Ask students which row makes "high happiness" most likely before you click. Compare every bar with the dashed marginal at 0.46.
  2. Click the column \(Y = 2\) (high) instead. The bars now show \(P(X \mid Y = 2)\), the salary mix of happy people. Point out that \(0.14 / 0.46 = 0.304\) differs from 0.583.
  3. Look at the picture. The cuts do not line up across the square, and the difference table is not zeros, so salary and happiness are not independent.