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

How to Calculate Conditional Probability: Formula, Example and Code

Conditional probability is P(A|B) = P(A and B) / P(B). Worked two-way table example, independence check, and Excel, R and Python code.

Written and maintained by CASRAI Editorial Board

Last updated

To calculate conditional probability, divide the probability that both events happen by the probability of the event you are conditioning on: P(A|B) = P(A and B) / P(B). The result is the chance of A once you know B has occurred. This guide works one two-way table through every step, including the swapped-conditional mistake, an independence check, and Excel, R and Python code.

What is conditional probability?

Conditional probability is the probability that an event A occurs given that another event B has already occurred. It is written P(A|B) and read “the probability of A given B” (OpenStax Introductory Statistics 2e, section 3.1). Knowing B happened shrinks the sample space to the outcomes in B, and A is then measured only inside that smaller space. For a die roll, the sample space {1, 2, 3, 4, 5, 6} reduces to {1, 3, 5} when you are told the roll is odd (OpenStax, section 3.2).

Conditional probability formula: P(A|B) = P(A and B) / P(B)

The formula is the multiplication rule rearranged. OpenStax states the multiplication rule as P(A and B) = P(B)P(A|B); dividing both sides by P(B) gives the conditional form (section 3.3).

  • P(A|B): probability of A given B. This is the number you want.
  • P(A and B): probability that A and B both occur (the joint probability).
  • P(B): probability of the conditioning event. It must be greater than 0, because you cannot condition on an event that never happens.

The same idea works with counts. In a table, P(A|B) is the count in the cell where A and B overlap divided by the total count for B. The joint probability is covered separately in the joint probability formula guide.

How to calculate conditional probability step by step

  1. Define the event you are conditioning on (B) and the event you want (A). The word “given”, “if” or “among” usually marks B.
  2. Find P(B), or the total count for B.
  3. Find P(A and B), or the count where both hold.
  4. Divide the joint value by the value for B.
  5. Check that the answer lies between 0 and 1 and that you divided by the right total.

Worked example from a two-way table

The table below is the 100-person handedness example used in OpenStax section 3.1.

Right-handed Left-handed Total
Male 43 9 52
Female 44 4 48
Total 87 13 100

Probability of left-handed given female

Let L = left-handed and F = female. The condition is F, so the sample space becomes the 48 females.

  • P(L and F) = 4/100 = 0.04
  • P(F) = 48/100 = 0.48
  • P(L|F) = 0.04 / 0.48 = 0.0833

Counting directly gives the same answer: 4 left-handed females out of 48 females is 4/48 = 0.0833, which matches OpenStax.

Why the order matters: P(F|L) is not P(L|F)

Reverse the condition and the denominator changes. Given left-handed, the sample space is the 13 left-handed people: P(F|L) = 4/13 = 0.3077. Both answers share the numerator 4 and differ only in the denominator (48 versus 13). Confusing the two is the most common error in conditional probability.

Reversing the condition with Bayes’ rule

If you know P(L|F), P(F) and P(L), you can reverse the condition: P(F|L) = P(L|F) × P(F) / P(L) = 0.0833 × 0.48 / 0.13 = 0.04 / 0.13 = 0.3077, the same value as the direct count. For the Bayesian framing, see interpreting Bayes factors and Bayesian vs frequentist statistics.

Interpreting the result

Comparison What it tells you
P(A|B) = P(A) Knowing B does not change the probability of A, so A and B are independent (OpenStax, section 3.2).
P(A|B) > P(A) B makes A more likely in this data.
P(A|B) < P(A) B makes A less likely in this data.

In the table, P(L) = 13/100 = 0.13 while P(L|F) = 0.0833, so being female lowers the proportion of left-handers in this sample, and L and F are not independent here.

How to check independence with conditional probability

OpenStax gives three equivalent conditions for independence: P(A|B) = P(A), P(B|A) = P(B), or P(A and B) = P(A)P(B); you need to show only one (section 3.2). Its Example 3.9 has P(G) = 0.6, P(H) = 0.5 and P(G and H) = 0.3. Then P(G|H) = 0.3 / 0.5 = 0.6 = P(G), so G and H are independent, and the product check agrees: 0.6 × 0.5 = 0.3. OpenStax also advises that if you do not know whether events are independent, you should assume they are dependent until you can show otherwise.

Assumptions and common mistakes

  • P(B) must be positive. The formula divides by P(B).
  • Swapped conditions. P(A|B) and P(B|A) usually differ (0.0833 versus 0.3077 above).
  • Wrong denominator. Divide by the total for the condition, not by the grand total. Using 4/100 instead of 4/48 gives the joint probability, not the conditional one.
  • Assuming independence. Multiplying P(A) × P(B) for a joint probability is valid only when independence has been shown.
  • Treating a sample proportion as a population probability. The 0.0833 describes these 100 people.

How to report conditional probability

Name both events, give the counts and the proportion, and state the base used. Example wording for a results section:

Among the 48 female participants, 4 were left-handed, a conditional probability of P(L|F) = .08, compared with an overall left-handed proportion of .13 (13 of 100).

Give the counts so readers can recompute the figure, and say which group is the denominator.

How to calculate conditional probability in Excel, R or Python

Excel

Put the counts in B2:C3 (rows Male and Female, columns Right and Left). Then P(L|F):

=C3/SUM(B3:C3)

This returns 0.083333.

R

tab <- matrix(c(43, 44, 9, 4), nrow = 2,
  dimnames = list(sex = c("M", "F"), hand = c("R", "L")))
prop.table(tab, 1)["F", "L"]   # row proportions: P(L|F) = 0.0833

prop.table(tab, 1) divides each cell by its row total, which is conditioning on the row variable. Use prop.table(tab, 2) to condition on columns and get P(F|L) = 0.3077.

Python (pandas)

import pandas as pd
df = pd.DataFrame({"R": [43, 44], "L": [9, 4]}, index=["M", "F"])
p_l_given_f = df.loc["F", "L"] / df.loc["F"].sum()   # 0.0833

FAQ

How do you calculate conditional probability?

Divide the probability that both events occur by the probability of the given event: P(A|B) = P(A and B) / P(B). With a count table, divide the cell count by the total for the given event, as in 4/48 = 0.0833 above.

What is the difference between conditional and joint probability?

A joint probability, P(A and B), is measured against everyone (4/100 = 0.04). A conditional probability is measured only within B (4/48 = 0.0833).

How do you know if two events are independent?

If P(A|B) = P(A), knowing B tells you nothing about A, and the events are independent.

Sources

Follow CASRAI

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

Ask CASRAI · free to try

Ask about How to Calculate Conditional Probability: Formula, 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.