Conditional Probability Calculator

By · Last updated:

Conditional Probability Calculator P(A|B) = P(A ∩ B) / P(B)

Pick what you know: a joint and a marginal probability, the three inputs of Bayes’ theorem, or a 2×2 table of counts. Probabilities can be decimals (0.05), percentages (5%) or fractions (1/20). You get P(A|B), P(B|A), P(A|not B), an independence check and an area diagram.

Examples:

Conditional probability is the probability that an event A happens given that another event B has happened, written P(A|B) and equal to P(A and B) divided by P(B). This conditional probability calculator returns P(A|B), P(B|A), P(A|not B) and an independence check from a joint and a marginal probability, from the inputs of Bayes’ theorem, or from a 2×2 table of counts.

What is conditional probability?

Conditional probability measures how likely one event is once you know that another has occurred. Learning that B happened removes every outcome in which B did not happen, so the question becomes: of the outcomes that are left, what fraction also belong to A? The diagram in the tool draws exactly that. The coloured region is B, and P(A|B) is the dark share of it.

The condition changes the answer whenever the two events are related. The probability that an email contains the word “invoice” is one number; the probability that it contains that word given that it is spam is usually a different one. When knowing B tells you nothing about A, the events are independent and P(A|B) is simply P(A).

What is the conditional probability formula?

\[ P(A \mid B) = \frac{P(A \cap B)}{P(B)}, \qquad P(B) > 0 \]

\(P(A \cap B)\) is the joint probability that both events happen, \(P(B)\) is the marginal probability of the condition, and the vertical bar is read as “given”. If \(P(B) = 0\) the ratio is undefined, because there is nothing to condition on. Writing the same joint probability as \(P(B \mid A)\,P(A)\) turns the definition into Bayes’ theorem, which reverses the direction of the condition:

\[ P(A \mid B) = \frac{P(B \mid A)\,P(A)}{P(B)}, \qquad P(B) = P(B \mid A)\,P(A) + P(B \mid \lnot A)\,\bigl(1 - P(A)\bigr) \]

Here \(P(A)\) is the prior, \(P(B \mid A)\) is the likelihood, \(P(B \mid \lnot A)\) is how often B shows up when A is false, and \(P(A \mid B)\) is the posterior. The second equation is the law of total probability: B can happen with A or without it, and the two routes add.

Why is P(A|B) not the same as P(B|A)?

Take a screening test with made-up round numbers. This is an arithmetic illustration, not medical advice. Let A be “has the condition” and B be “tests positive”. The condition affects 1% of people, the test catches 90% of real cases, and it wrongly flags 5% of people who are well. Those are the default inputs above. Spread over 10,000 people they give this table:

Tests positive (B)Tests negative (not B)Total
Has the condition (A)9010100
Does not (not A)4959,4059,900
Total5859,41510,000

There are 585 positive results and only 90 of them belong to people with the condition, so P(A|B) = 90 / 585 = 0.1538, about 15%. The test’s sensitivity, P(B|A) = 90 / 100 = 0.9, is a different quantity. Both use the same 90 people in the numerator, but one divides by the 100 people in the top row and the other by the 585 people in the first column.

The gap comes from the base rate. People without the condition outnumber those with it 99 to 1, so even a 5% error rate among them produces 495 false positives, more than five times the 90 true ones. The calculator also reports P(B) = 585 / 10,000 = 0.0585 and P(A|not B) = 10 / 9,415 = 0.0011, the chance of having the condition after a negative result.

How do you check whether two events are independent?

A and B are independent when P(A and B) = P(A) × P(B), which is the same as saying P(A|B) = P(A). With P(A) = 0.4, P(B) = 0.3 and P(A and B) = 0.12, the product is 0.4 × 0.3 = 0.12, so the events are independent and P(A|B) = 0.12 / 0.3 = 0.4, unchanged from P(A). With P(A) = 0.3, P(B) = 0.5 and P(A and B) = 0.2, the product is 0.15, not 0.2, so they are dependent: P(A|B) = 0.4 while P(A) is only 0.3.

The tool calls the two sides equal when they differ by less than 0.000000001. That is an exact check on the numbers you typed. Counts from a sample almost never match exactly even when the underlying events are independent, and deciding whether a gap is larger than chance needs a significance test, which this page does not run.

Where is conditional probability used in machine learning?

  • Precision and recall. Precision is P(actually positive | predicted positive) and recall is P(predicted positive | actually positive): the same two events with the condition swapped. Read the table above as a classifier and recall is 90 / 100 = 0.90 while precision is 90 / 585 = 0.1538. On imbalanced data the large negative class supplies most of the predicted positives, which is why the two metrics drift apart. Both come straight out of a confusion matrix.
  • Naive Bayes. A Naive Bayes classifier applies Bayes’ theorem with the assumption that the features are conditionally independent given the class, so \(P(y \mid x_1, \dots, x_n) \propto P(y) \prod_i P(x_i \mid y)\). The assumption is rarely true, but it reduces training to estimating one small conditional distribution per feature.
  • Classifier outputs. The number a probabilistic classifier returns is an estimate of P(class | features). The probabilities from a softmax output layer are conditional on the input, and checking calibration means asking whether those estimates match the observed frequencies.
  • Decisions under uncertainty. Once you have a posterior, the expected cost of each action weights every outcome by its conditional probability.

Common mistakes

  • Swapping the condition. Quoting P(B|A) when the question asks for P(A|B). A 90% sensitive test does not make a positive result 90% reliable.
  • Ignoring the base rate. The prior P(A) matters as much as the likelihood. A rare event stays fairly unlikely after one piece of imperfect evidence.
  • Dividing by the grand total. In a table of counts, cell divided by grand total is the joint probability. The conditional divides by the row or column total of the condition.
  • Multiplying without checking independence. P(A and B) = P(A) × P(B) holds only for independent events. Otherwise use P(A and B) = P(A|B) × P(B).
  • Treating an undefined result as zero. If P(B) = 0 then P(A|B) has no value. It is not 0.
  • Mixing percentages and decimals. Enter five percent as 0.05 or 5%, never as 5.

How do you calculate conditional probability in Python?

Bayes’ theorem needs nothing beyond arithmetic. The NumPy version starts from the 2×2 count table instead and divides one cell by a row or column total computed with numpy.sum.

# Plain arithmetic: Bayes' theorem
p_a = 0.01               # prior P(A)
p_b_given_a = 0.9        # likelihood P(B|A)
p_b_given_not_a = 0.05   # P(B|not A)

p_b = p_b_given_a * p_a + p_b_given_not_a * (1 - p_a)
p_a_given_b = p_b_given_a * p_a / p_b
p_a_given_not_b = (1 - p_b_given_a) * p_a / (1 - p_b)

print(round(p_b, 6))              # 0.0585
print(round(p_a_given_b, 6))      # 0.153846
print(round(p_a_given_not_b, 6))  # 0.001062

# NumPy: the same answers from a 2x2 table of counts
import numpy as np

# rows: A, not A    columns: B, not B
t = np.array([[90, 10],
              [495, 9405]])

print(round(t[0, 0] / t[:, 0].sum(), 6))  # P(A|B)     0.153846
print(round(t[0, 0] / t[0, :].sum(), 6))  # P(B|A)     0.9
print(round(t[0, 1] / t[:, 1].sum(), 6))  # P(A|not B) 0.001062
print(round(t[:, 0].sum() / t.sum(), 6))  # P(B)       0.0585

Frequently asked questions

Is P(A|B) the same as P(B|A)?

No. They share the numerator P(A and B) but divide by different totals: P(A|B) divides by P(B) and P(B|A) divides by P(A). They are equal only when P(A) = P(B) or when the joint probability is 0. In the screening example P(B|A) is 0.9 and P(A|B) is 0.1538.

What happens when P(B) is 0?

P(A|B) is undefined. The formula would divide by zero, and there is no meaningful way to restrict attention to an event that cannot happen. The calculator reports “undefined” in that case instead of returning a number.

How is conditional probability different from joint probability?

Joint probability P(A and B) is the chance that both events happen, measured against all outcomes. Conditional probability P(A|B) is measured against only the outcomes where B happened. The two are linked by P(A and B) = P(A|B) × P(B), so the conditional is never smaller than the joint.

When do you need Bayes’ theorem?

Use it when you know the conditional in one direction and need the other. If you have P(B|A), P(B|not A) and the prior P(A), Bayes’ theorem gives P(A|B). If you already know P(A and B) and P(B), divide them directly.

Related calculators

If your probabilities come from drawing items without replacement, the hypergeometric distribution calculator produces them, and the combinations calculator handles the counting underneath. The rest of the group is on the statistics calculators page.

More tools that pair with this one: