Skip to main content
v2026.11,858 entries · CC-BY 4.0

Conditional Probability and Bayes’ Theorem: Formula, Worked Example and Code

Bayes’ theorem reverses a conditional probability. See the formula, a worked screening example with the arithmetic, how to report it, and code.

Written and maintained by CASRAI Editorial Board

Last updated

Conditional probability is the probability of one event given that another has occurred, and Bayes’ theorem is the rule that reverses the direction of that conditioning: it turns P(E | H) into P(H | E). This guide shows how the two fit together, works a full screening example with the arithmetic, and gives Excel, R and Python code. For the basic definition on its own, see how to calculate conditional probability.

Is Bayes’ theorem conditional probability?

Yes. Bayes’ theorem is not a separate kind of probability; it is an identity between conditional probabilities. It follows directly from the definition of conditional probability, so every term in it, P(H | E), P(E | H), is itself a conditional probability. The Stanford Encyclopedia of Philosophy treats it the same way: its first section, “Conditional Probabilities and Bayes’ Theorem,” defines conditional probability as a ratio and then states the theorem in terms of it.

The derivation takes two lines (the algebra here is standard and is not taken from the cited entry). The joint probability of H and E can be written in two ways: P(H and E) = P(E) × P(H | E) and P(H and E) = P(H) × P(E | H). Setting them equal and dividing by P(E) gives the theorem. What Bayes’ theorem adds is not new probability, but a way to get from “how likely is this evidence if the hypothesis is true” to “how likely is the hypothesis given this evidence.”

Conditional probability: definition in one paragraph

The conditional probability of H given E is the share of the probability of E that also belongs to H: P(H | E) = P(H and E) / P(E), defined only when P(E) > 0 (Stanford Encyclopedia of Philosophy, “Bayes’ Theorem,” section 1; OpenStax, Introductory Statistics 2e, section 3.2, uses the same ratio in its worked examples, P(G | H) = P(G and H) / P(H)). Conditioning shrinks the sample space to the outcomes where E happened and asks how much of that smaller space is H.

Bayes’ theorem formula with every symbol explained

The simple form, as given in the Stanford Encyclopedia entry, is:

P(H | E) = P(H) × P(E | H) / P(E)

  • H: the hypothesis or condition you want to assess (for example, “the person has the disease”).
  • E: the evidence you observed (for example, “the test is positive”).
  • P(H): the prior probability of H before seeing E. In screening this is the prevalence.
  • P(E | H): the likelihood, the probability of the evidence if H is true. For a diagnostic test this is the sensitivity.
  • P(E): the overall probability of the evidence, whether or not H is true.
  • P(H | E): the posterior probability of H after seeing E. This is the quantity you usually want.

Conditional probability with Bayes’ theorem: expanding P(E)

P(E) is rarely known directly. The extended form, also in the Stanford Encyclopedia entry, builds it from the two ways E can happen, with H true and with H false (written ~H):

P(H | E) = P(H) P(E | H) / [P(H) P(E | H) + P(~H) P(E | ~H)]

Here P(~H) = 1 − P(H), and P(E | ~H) is the probability of the evidence when H is false (for a test, the false-positive rate, which is 1 − specificity). The denominator is the law of total probability applied to E.

Bayes’ theorem and conditional probability: worked example

This is an illustrative example with hypothetical numbers chosen for clear arithmetic; they do not describe any real test. Suppose a condition has a prevalence of 1%, a screening test has a sensitivity of 90% (P(positive | condition) = 0.90) and a specificity of 95% (P(negative | no condition) = 0.95, so the false-positive rate is 0.05). A person tests positive. What is P(condition | positive)?

Step 1: the formula

P(condition | positive) = (0.01 × 0.90) / [(0.01 × 0.90) + (0.99 × 0.05)]

Step 2: the arithmetic

  • Numerator: 0.01 × 0.90 = 0.0090
  • False-positive term: 0.99 × 0.05 = 0.0495
  • Denominator: 0.0090 + 0.0495 = 0.0585
  • Result: 0.0090 / 0.0585 = 0.1538, about 15.4%

Step 3: the same result as a frequency table

Imagine 10,000 people screened. Of them, 1% (100) have the condition and 9,900 do not.

Group Test positive Test negative Total
Has the condition 90 (0.90 × 100) 10 100
Does not have it 495 (0.05 × 9,900) 9,405 (0.95 × 9,900) 9,900
Total 585 9,415 10,000

Of the 585 positive results, 90 are true, and 90 / 585 = 0.1538. The test detects 90% of cases, yet fewer than one positive in six is a real case, because the 9,900 people without the condition produce far more false positives than the 100 with it produce true ones. P(positive | condition) = 0.90 and P(condition | positive) = 0.15 are different quantities, and confusing them is the classic base-rate error. The same table also gives the negative result: P(condition | negative) = 10 / 9,415 = 0.0011.

The test-accuracy terms used here are covered in more detail in sensitivity vs. specificity, which treats positive and negative predictive values and likelihood ratios.

Interpretation: reading the result

What you see How to read it
P(H | E) larger than P(H) The evidence raised the probability of H. In the example, 1% rose to 15.4%.
P(H | E) smaller than P(H) The evidence lowered it. In the example, a negative result lowered 1% to 0.11%.
P(H | E) equal to P(H) The evidence is independent of H and carries no information about it.
High P(E | H) but low P(H | E) A low prior (rare H) or a high P(E | ~H) is dominating the denominator. This is the base-rate effect.

Moving from a prior to a posterior is the core of Bayesian updating; to see how the updated probability is summarised for continuous parameters, read summarizing a posterior distribution, and for comparing hypotheses by the strength of evidence see interpreting Bayes factors.

Assumptions and common mistakes

  • P(E) must be greater than 0. The conditional probability is undefined otherwise.
  • Swapping the two conditionals. P(H | E) is not P(E | H). The worked example shows a gap from 0.90 to 0.15.
  • Ignoring the prior. Using only the likelihood discards the base rate. The prior must describe the population the person came from.
  • Forgetting the false-positive term. The denominator needs both P(H) P(E | H) and P(~H) P(E | ~H).
  • Treating repeat tests as independent without checking. If a second positive test is independent of the first given the true status, you can reuse the posterior as the new prior: 0.1538 becomes the prior and the new result is 0.1538 × 0.90 / (0.1538 × 0.90 + 0.8462 × 0.05) = 0.766. If the tests share a cause of error, the independence assumption fails and the result overstates the certainty.
  • Treating the theorem as a Bayesian-only tool. The formula is a statement about conditional probabilities; what differs between schools of statistics is how the prior is justified. See Bayesian vs. frequentist statistics.

For related ideas about combining events, see the joint probability formula.

How to report it

State the prior, the likelihood inputs and the posterior, and say that the values are assumed. Follow the number-formatting rules of the style guide your journal or institution requires.

Assuming a prevalence of 0.01, a sensitivity of 0.90 and a specificity of 0.95, the probability of the condition given a positive result was 0.15 (Bayes’ theorem, extended form). The probability given a negative result was 0.001.

How to calculate it in Excel, R or Python

Excel

With prevalence in A1 (0.01), sensitivity in A2 (0.90) and specificity in A3 (0.95), enter:

=(A1*A2)/(A1*A2+(1-A1)*(1-A3))

The cell returns 0.1538.

R

prev <- 0.01; sens <- 0.90; spec <- 0.95
posterior <- (prev * sens) / (prev * sens + (1 - prev) * (1 - spec))
posterior   # 0.1538462

Python

prev, sens, spec = 0.01, 0.90, 0.95
posterior = (prev * sens) / (prev * sens + (1 - prev) * (1 - spec))
print(round(posterior, 4))  # 0.1538

FAQ

Is Bayes’ theorem conditional probability?

Yes. It is a rule that follows from the definition of conditional probability, P(H | E) = P(H and E) / P(E), and relates P(H | E) to P(E | H).

What is the difference between conditional probability and Bayes’ theorem?

Conditional probability is the quantity: the probability of one event given another. Bayes’ theorem is the formula that computes one conditional probability from its reverse, using the prior and the probability of the evidence.

When should I use Bayes’ theorem instead of the basic formula?

Use the basic ratio P(H and E) / P(E) when you can read the joint probability from the data. Use Bayes’ theorem when you know P(E | H), the prior and the false-positive rate but not the joint probability, as in a screening problem.

Why is the probability so much lower than the test’s sensitivity?

Because the condition is rare. When the prior is small, false positives from the large unaffected group outnumber the true positives, as the frequency table shows.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Conditional Probability and Bayes’ Theorem: Formula, Worked Example and Code

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.