Conditional Probability Calculator
Find the probability of A given that B has happened, P(A | B). Enter P(A and B) and P(B), and optionally P(A), or fill in a two-way table of counts. You get P(A | B) with every step, the reverse probability P(B | A), P(A | not B), the probability of A or B, an exact test of whether the two events are independent, and the joint probabilities in a table.
To turn P(B | A) into P(A | B) when you know how common A is, use the Bayes' theorem calculator. To test whether two categorical variables in a sample are related, use the chi-square test of independence; for other single-event and combined-event problems, see the probability calculator.
Both events happen
The event you condition on
Adds P(B | A), independence and the table
Related Calculators
Bayes' Theorem Calculator
Update a prior probability with new evidence to get the posterior P(A|B) and total P(B).
Probability of Multiple Events Calculator
Find P(A and B) and P(A or B) for two events, or the chance that all, none or at least one of several events happen.
Chi-Square Test of Independence Calculator
Run Pearson's chi-square test on an r×c contingency table with expected counts, residuals, Cramér's V, and p-value.
What conditional probability means
Conditional probability answers the question: how likely is A once you know that B has happened? It is written P(A | B) and read as the probability of A given B. Learning that B happened rules out every outcome outside B, so the sample space shrinks to B, and P(A | B) is the share of B in which A also occurs. Take a card from a standard deck: the probability of a king is 4 / 52, but the probability of a king given that the card is a face card is 4 / 12, because only the 12 face cards are still possible.
The condition can make an event more likely, less likely or leave it unchanged. When P(A | B) equals P(A), knowing B tells you nothing about A and the two events are independent.
Formulas
P(A | B) = P(A and B) / P(B), where P(B) > 0
P(A and B) = P(A | B) · P(B)
P(not A | B) = 1 − P(A | B)
P(A or B) = P(A) + P(B) − P(A and B)
Independent events: P(A and B) = P(A) · P(B), which is the same as P(A | B) = P(A)
From a table of counts: P(A | B) = count(A and B) / count(B)
The condition goes after the bar and supplies the denominator. P(A | B) and P(B | A) have the same numerator, P(A and B), but different denominators, so they differ unless P(A) = P(B) or P(A and B) = 0. The rule P(not A | B) = 1 − P(A | B) holds because both probabilities use the same condition. The condition cannot be swapped: P(A | not B) is not 1 − P(A | B). In the first example below P(A | B) = 0.6 and P(A | not B) = 0.2.
How to read the results
| Output | What it tells you |
|---|---|
| P(A | B) | The probability of A when B is known to have happened. This is the main result. |
| P(not A | B) | The probability that A does not happen, given B. It equals 1 − P(A | B). |
| P(B | A) | The reverse condition, the probability of B given A. It needs P(A) and is undefined when P(A) = 0. |
| P(A | not B) | The probability of A when B did not happen. It needs P(A) and is undefined when P(B) = 1. |
| P(A or B) | The probability that at least one of the two events happens. |
| P(A) × P(B) and Independent events? | The product that P(A and B) would equal if the events were independent, and the result of comparing the two exactly. |
| Joint probabilities table | P(A and B), P(A and not B), P(not A and B) and P(not A and not B) with their row and column totals. The four inner cells add up to 1. |
| Table of counts | Every probability is worked out from the counts, for example P(A | B) = count(A and B) / count(B). A conditional probability is undefined when its condition never occurs in the table. |
Worked example 1: from probabilities
Suppose P(A and B) = 0.3, P(B) = 0.5 and P(A) = 0.4. Load example fills in these values.
- P(A | B) = 0.3 / 0.5 = 0.6, and P(not A | B) = (0.5 − 0.3) / 0.5 = 0.4.
- P(B | A) = 0.3 / 0.4 = 0.75. The two conditional probabilities differ because A is less common than B.
- P(A | not B) = (0.4 − 0.3) / (1 − 0.5) = 0.1 / 0.5 = 0.2.
- P(A or B) = 0.4 + 0.5 − 0.3 = 0.6.
- Independence: P(A) × P(B) = 0.4 × 0.5 = 0.2, which is not the observed P(A and B) = 0.3, so the events are not independent.
Knowing that B happened raises the probability of A from 0.4 to 0.6. The joint table has 0.3 for A and B, 0.1 for A and not B, 0.2 for not A and B and 0.4 for neither, which adds up to 1.
Worked example 2: from a two-way table
Of 100 students, A means the student passed the exam and B means the student attended the review session. Forty students attended and passed, 30 passed without attending, 10 attended and failed and 20 did neither. Choose Two-way table of counts and enter 40, 30, 10, 20 (or use Load example).
- Totals: 70 passed, 50 attended, so P(A) = 0.7, P(B) = 0.5 and P(A and B) = 40 / 100 = 0.4.
- P(A | B) = 40 / 50 = 0.8: among the 50 who attended, 80% passed.
- P(A | not B) = 30 / 50 = 0.6: among the 50 who did not attend, 60% passed.
- P(B | A) = 40 / 70 = 0.5714: among the 70 who passed, 57.14% attended. This is a different question from P(A | B).
- P(B | not A) = 10 / 30 = 0.3333: among the 30 who failed, a third attended.
- Independence: 40 × 20 = 800 but 30 × 10 = 300. The cross-products differ, so passing and attending are not independent in this group.
Independent events
Two events are independent when knowing one does not change the probability of the other: P(A | B) = P(A), or equivalently P(A and B) = P(A) · P(B). With P(A) = 0.5, P(B) = 0.4 and P(A and B) = 0.2 the calculator finds P(A | B) = 0.2 / 0.4 = 0.5 = P(A) and P(A) × P(B) = 0.2 = P(A and B), so the events are independent. Then P(A | not B) = 0.3 / 0.6 = 0.5 is also equal to P(A).
Independent is not the same as mutually exclusive. Events that cannot happen together have P(A and B) = 0, so if both have a positive probability they are dependent: once B has happened, A is impossible.
The test is exact. The probabilities are read as exact decimals, so 0.1 × 0.7 equals 0.07 here, although most software using floating-point numbers gets 0.06999999999999999 and would call the events dependent. Counts from a real sample are rarely exactly independent, though. A difference between the cross-products can arise by chance, and the chi-square test of independence decides whether it is larger than chance would explain.
Common mistakes
- Swapping the condition. P(A | B) and P(B | A) are different questions. The share of people with a disease who test positive is not the share of positive tests that belong to people with the disease; the second depends on how rare the disease is. The Bayes' theorem calculator converts one into the other, and the article Bayes' theorem explained shows why.
- Dividing by the wrong total. P(A | B) is divided by P(B), the condition. Dividing P(A and B) by P(A) gives P(B | A).
- Treating not B as the complement of the whole conditional. P(not A | B) = 1 − P(A | B), but P(A | not B) is a separate quantity with a different condition.
- Entering percentages. Enter 40% as 0.4. Values above 1 are rejected.
- Conditioning on an impossible event. When P(B) = 0 there is no probability of A given B, and the calculator says so instead of returning a number.
Conditional probability in other software
| Tool | Command |
|---|---|
| Excel / Google Sheets | =B2/B4 with count(A and B) in B2 and count(B) in B4, or =COUNTIFS(A:A, "yes", B:B, "yes") / COUNTIF(B:B, "yes") on raw data |
| Python (pandas) | pd.crosstab(df["B"], df["A"], normalize="index"): each row is one value of B and holds P(A | B) |
| R | prop.table(table(B, A), margin = 1): each row is one value of B and holds P(A | B) |
| Python (plain) | p_a_given_b = p_a_and_b / p_b |
With the condition in the rows, normalising over rows gives the probability of the column variable given the row value. Normalising over columns instead gives the reverse conditional probability P(B | A).
Frequently Asked Questions
What is conditional probability?
Conditional probability is the probability that an event A happens given that another event B has already happened. It is written P(A | B) and calculated as P(A and B) / P(B). Knowing B shrinks the possible outcomes to B, and P(A | B) is the share of B in which A also occurs.
How do I calculate P(A | B)?
Divide the probability that both events happen by the probability of the condition: P(A | B) = P(A and B) / P(B). If P(A and B) = 0.3 and P(B) = 0.5, then P(A | B) = 0.3 / 0.5 = 0.6. From a table of counts, divide the count of A and B by the count of B.
What is the difference between P(A | B) and P(B | A)?
They have the same numerator, P(A and B), but different denominators: P(A | B) divides by P(B) and P(B | A) divides by P(A). They answer different questions and are equal only when P(A) = P(B) or when the events cannot happen together, P(A and B) = 0. Bayes' theorem relates them: P(A | B) = P(B | A) · P(A) / P(B).
How do I find a conditional probability from a two-way table?
Divide the cell by the total of the row or column that matches the condition. To find P(A | B), take the count of A and B and divide it by the total count of B, which is the cell for A and B plus the cell for not A and B. Do not divide by the grand total, which gives the joint probability P(A and B).
How do I know whether two events are independent?
Events are independent when P(A and B) = P(A) · P(B), which is the same as P(A | B) = P(A): knowing B does not change the probability of A. For counts in a table this means the cross-products of the cells are equal, (A and B) × (neither) = (A only) × (B only). This calculator checks the equality exactly.
What is the difference between independent and mutually exclusive events?
Mutually exclusive events cannot happen together, so P(A and B) = 0. Independent events do not affect each other, so P(A and B) = P(A) · P(B). If two events both have a positive probability and are mutually exclusive, they are dependent, because when one happens the other becomes impossible.
Can P(A | B) be larger than P(A)?
Yes. P(A | B) is larger than P(A) exactly when P(A and B) is larger than P(A) · P(B), that is, when B makes A more likely. In the first example P(A) = 0.4 and P(A | B) = 0.6. When P(A | B) is smaller than P(A), B makes A less likely.
Why does the calculator say P(B) must be greater than 0?
P(A | B) = P(A and B) / P(B) divides by P(B), and the probability of A given an event that never happens is not defined. The calculator reports this instead of returning a number. In table mode a conditional probability is shown as undefined when its condition has a count of 0.
Embed This Calculator
Add this free calculator to your course page or LMS.
Adjust the height value to fit your page.