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. The cards below test independence and compare P(A | B) with P(B | A).

← All labs

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

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

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.

  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 | X = 3) = 0.14 / 0.24 = 0.583. That beats 0.46, so very high earners are more likely to report high happiness.

  4. Independence: the conditional equals the marginal

    If every row, once rescaled, looked like the marginal row, X would tell you nothing about Y. Then each cell equals P(x)·P(y). Two fair coin tosses pass this test exactly. Happiness and salary do not.

Product rule
P(x, y) = P(x)·P(y | x) = P(y)·P(x | y)
Marginal = sum of joint
P(x) = Σy P(x, y), P(y) = Σx P(x, y)
Independence
P(y | x) = P(y) for all x, y
⇔ P(x, y) = P(x)·P(y)
Bayes corrects the flip
P(A | B) = P(B | A) × P(A) / P(B)

Takeaway for students. P(A | B) and P(B | A) share the same numerator, P(A and B). They divide it by different totals. When one event is rare, such as playing in the NBA, the two conditionals can sit at opposite ends of the scale. Mixing them up is the prosecutor's fallacy.

Try this in class

  1. Load Happiness × Salary and 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 | Y = 2), the salary mix of happy people. Point out that 0.14 / 0.46 = 0.304 differs from 0.583.
  3. Press "Make independent". Every row now has the same shape, the bars sit on the dashed marks, and the difference table turns to zeros. The marginals did not change.
  4. Load Two fair coins and change one cell from 0.25 to 0.35. The entries no longer sum to 1, but the lab rescales them. Ask whether the second toss still ignores the first.
  5. Load NBA. Read P(practice hard | NBA) and P(NBA | practice hard) and find the base-rate ratio that connects them.