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

Conditional Probability Distribution: Formula, Worked Example and Code

Conditional probability distribution explained: the discrete and continuous formulas, two worked examples with the arithmetic, and Excel, R and Python code.

Written and maintained by CASRAI Editorial Board

Last updated

A conditional probability distribution describes how one random variable behaves once you know the value of another. If Y is an outcome and X is something you have observed, the conditional distribution of Y given X = x is the full set of probabilities (or the density) for Y in the subgroup where X equals x. This guide gives the formulas for the discrete and continuous cases, one worked example of each with the arithmetic shown, reading guidance, reporting wording, and code for Excel, R and Python.

Definition in one paragraph

A conditional distribution is the distribution of a random variable, computed with the rules of conditional probability, after observing the value taken by another random variable (StatLect, “Conditional probability distributions”). The rule is “conditional = joint / marginal”: take the slice of the joint distribution at X = x, then divide by the marginal probability (or density) of X at x so the slice adds up to 1 (Davis Ross, STAT 350 handout 23). Because there is a separate answer for every value of x, you get a family of distributions, not a single one.

What is probability, and how do you find it?

Probability is a number from 0 to 1 that measures how likely an event is. For a discrete outcome you find it by adding the probabilities of the outcomes that make up the event; for a continuous outcome you find it from the area under a density curve. Conditioning changes the starting point: you ask for the probability of an event inside the group where something else is already known. The single-event version is covered in how to calculate conditional probability and conditional probability and Bayes’ theorem; this page extends it to whole distributions. The joint distribution you start from is explained in the joint probability formula guide.

Formula, with every symbol explained

Discrete case: the conditional probability mass function

pY|X(y | x) = P(Y = y | X = x) = pX,Y(x, y) / pX(x), for a fixed x with pX(x) > 0.

  • pY|X(y | x): the conditional probability mass function of Y given X = x.
  • pX,Y(x, y): the joint probability that X = x and Y = y at the same time.
  • pX(x): the marginal probability that X = x, found by summing the joint probabilities over all values of y.
  • x is held constant; y is the variable that runs over the outcomes. For background on mass functions, see the probability mass function guide.

Continuous case: what is a probability density function, and what is the conditional one?

A probability density function (pdf) describes a continuous variable: probabilities come from areas under the curve, and the height of the curve at a point is not itself a probability. The conditional pdf has the same “joint over marginal” shape:

fY|X(y | x) = fX,Y(x, y) / fX(x), for a fixed x with fX(x) > 0.

Here fX,Y is the joint density and fX(x) is the marginal density of X, found by integrating the joint density over y. As a function of y, the conditional density is proportional to the joint density at that x; dividing by fX(x) only rescales it so the area under the curve is 1 (Davis Ross). To see where the variable X comes from, read what a random variable is.

Worked example with real numbers

Discrete: a joint count table

The counts below are illustrative numbers invented for this guide, not data from a real study. 100 participants are classed by training format (X) and by a competency rating (Y) scored Low = 1, Mid = 2, High = 3.

Training (X) Low Mid High Row total
None 12 18 10 40
Online 9 21 20 50
In-person 5 3 2 10
Column total 26 42 32 100

Step 1, marginal of X. Sum each row: pX(None) = 40/100 = 0.40, pX(Online) = 0.50, pX(In-person) = 0.10.

Step 2, divide. Each cell over 100 is a joint probability (Online, High = 20/100 = 0.20). For X = Online: pY|X(High | Online) = 0.20 / 0.50 = 0.40, pY|X(Mid | Online) = 0.21 / 0.50 = 0.42, pY|X(Low | Online) = 0.09 / 0.50 = 0.18. These add to 0.18 + 0.42 + 0.40 = 1.00, as a distribution must.

Conditional distribution of Y given X P(Low) P(Mid) P(High) Sum E[Y | X]
None (n = 40) 0.30 0.45 0.25 1.00 1.95
Online (n = 50) 0.18 0.42 0.40 1.00 2.22
In-person (n = 10) 0.50 0.30 0.20 1.00 1.70

Step 3, conditional mean. E[Y | X = Online] = 1(0.18) + 2(0.42) + 3(0.40) = 0.18 + 0.84 + 1.20 = 2.22. The same sum gives 1.95 for None (0.30 + 0.90 + 0.75) and 1.70 for In-person (0.50 + 0.60 + 0.60).

The reverse is a different distribution. P(High | Online) = 20/50 = 0.40, but P(Online | High) = 20/32 = 0.625. The first divides by the Online row total, the second by the High column total.

Continuous: slicing a joint density

This example follows Davis Ross, example 23.2. Spin two independent Uniform(1, 4) spinners; let X be the sum and Y the larger value. The joint density is fX,Y(x, y) = 2/9 on the region 1 < y < 4, y + 1 < x < 2y (the same region as x/2 < y < x − 1). Integrating over x gives the marginal fY(y) = (2/9)(2y − (y + 1)) = (2/9)(y − 1).

Condition on Y = 3. Then x runs from 4 to 6, fY(3) = (2/9)(2) = 4/9, and fX|Y(x | 3) = (2/9) / (4/9) = 1/2 for 4 < x < 6. That is a Uniform(4, 6) density, and its area is 2 × 1/2 = 1. So P(X > 5.5 | Y = 3) = (6 − 5.5) × 1/2 = 1/4.

Interpretation

Read each conditional distribution as an ordinary distribution for the subgroup defined by the condition. Compare rows (or slices) to see whether knowing X changes what you expect for Y. If every conditional distribution of Y is identical, X tells you nothing about Y; in the example above the three rows differ, so training format and rating are associated in these numbers.

What you see How to read it
Conditional probabilities sum to 1 across y The slice was divided by the marginal correctly.
Rows differ from one another The distribution of Y depends on X; the example’s rows range from E[Y | X] = 1.70 to 2.22.
A conditional density value above 1 Allowed for a density; only areas are probabilities.
P(Y | X) differs from P(X | Y) They answer different questions and use different denominators.

Assumptions and common mistakes

  • Conditioning on something impossible. The formulas need pX(x) > 0 (or fX(x) > 0). A row with no observations has no conditional distribution.
  • Forgetting to divide. A raw slice of the joint table is not a distribution; in the example the Online row of joint probabilities sums to 0.50, not 1.
  • Swapping the direction. P(High | Online) = 0.40 is not P(Online | High) = 0.625.
  • Reading a density as a probability. fY|X(y | x) = 1/2 above is a height, not a 50% chance.

How to report it (APA example)

Name the conditioning variable, give the group sizes, and report the conditional proportions or the conditional mean. For the table above:

Among participants who completed online training (n = 50), 40% received a High rating, compared with 25% of participants with no training (n = 40) and 20% of those with in-person training (n = 10). The conditional mean rating was 2.22 for online training, 1.95 for no training and 1.70 for in-person training.

State the denominator for each percentage, and say which variable is the condition. Reporting a conditional proportion without its group size hides how much data it rests on. For related reporting of a hypothesis-test result, see what a p value is.

How to calculate it in Excel, R or Python

Excel

Put the counts in B2:D4 with scores 1, 2, 3 for the columns. In F2 enter =B2/SUM($B2:$D2) and fill across to H4 (adjust the column references as you fill) to get each conditional probability. For the conditional mean in I2: =SUMPRODUCT(B2:D2,{1,2,3})/SUM(B2:D2), filled down.

R

m <- matrix(c(12,18,10, 9,21,20, 5,3,2), nrow = 3, byrow = TRUE,
            dimnames = list(c("None","Online","In-person"), c("Low","Mid","High")))
cond <- prop.table(m, 1)   # divide each row by its row total
rowSums(cond)              # 1 1 1
cond %*% 1:3               # conditional means: 1.95, 2.22, 1.70

Python

import pandas as pd
df = pd.DataFrame([[12,18,10],[9,21,20],[5,3,2]],
                  index=["None","Online","In-person"], columns=["Low","Mid","High"])
cond = df.div(df.sum(axis=1), axis=0)     # P(Y | X): rows sum to 1
means = (cond * [1, 2, 3]).sum(axis=1)    # E[Y | X]
print(cond.round(2)); print(means.round(2))

For the other direction, P(X | Y), divide by column totals instead: df.div(df.sum(axis=0), axis=1).

FAQ

What is a conditional probability distribution?

It is the distribution of one variable among the cases where another variable has a given value, equal to the joint distribution divided by the marginal of the condition.

How to calculate a probability value from it?

Add the conditional probabilities of the outcomes you want, or integrate the conditional density over the range you want. In the continuous example, P(X > 5.5 | Y = 3) = 1/4.

Is “probability value” the same as a p-value?

No. A p-value is a probability computed under a null hypothesis; see the p-value dictionary entry. This guide is about the distribution of one variable given another.

What is probability of default?

It is a finance term for the probability that a borrower fails to repay. Like any probability it can be conditioned on other information, but it is a single event probability rather than the full distribution described here.

Sources

Follow CASRAI

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

Ask CASRAI · free to try

Ask about Conditional Probability Distribution: 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.