liveBook · Manning

Conditional probability

As explained in Statistics Every Programmer Needs

Conditional probability is the probability of an event given that another event or piece of information is known to hold.

Conditional probability measures the likelihood of an event when another event is known or assumed to have occurred. The new information changes the relevant sample space: instead of considering every possible outcome, consider only outcomes consistent with the condition. This makes conditional probability useful when a forecast, risk assessment, or decision must account for information that was not available in the original estimate. section 3.3

For events A and B, P(A|B) = P(A and B) / P(B), provided P(B) is greater than zero. The numerator counts outcomes satisfying both events; the denominator counts outcomes satisfying the condition. Reversing the question generally changes the answer: P(B|A) uses P(A) as its denominator. section 3.3.3 section 3.3.4

>>> import pandas as pd
>>> contingency_table = pd.DataFrame(test_results, index = symptoms)
>>> print(contingency_table)
     Negative  Positive
No        860        90
Yes        10        40

A pandas contingency table organizes test results by whether symptoms are present. Each cell counts one combination of the two categories, making both the joint count and the appropriate conditioning total visible. section 3.3.3

Symptoms and test results

The table contains 1,000 observations. Among the 50 people with symptoms, 40 have a positive test result, so P(positive test | symptoms) = 40 / (10 + 40) = 80%. The reverse question uses the 130 positive tests: P(symptoms | positive test) = 40 / (90 + 40) ≈ 30.77%. In pandas, retrieve the shared count with contingency_table.iloc[1, 1], then divide by the relevant row or column total. The two answers differ because the conditioning groups differ. section 3.3.3 section 3.3.4

Check whether the events are dependent before treating new information as a reason to revise an estimate. Earlier fair-coin flips do not change the probability of the next flip; returning a drawn card to the deck likewise leaves the next draw unchanged. Apparent relationships, such as weather across locations or trends across market sectors, require assessment rather than assumption. section 3.3.2

How Statistics Every Programmer Needs covers it

How deep it goes
Works through a contingency-table calculation and its equivalent formula, then compares reversed conditions and independence.
Examples
Uses weather forecasts, medical testing, traffic, coin flips, card draws, and a pandas table of symptoms and test results.
What it assumes
Builds on basic probability and random variables while defining contingency tables and demonstrating the calculation with simple counts.
What you can do after
Apply it via pandas contingency tables or the probability formula to calculate and distinguish probabilities conditioned in opposite directions.
How it connects
Connects sample spaces, joint probability, contingency tables, and event independence.

Also explained in

Compare how 11 books explain conditional probability →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.