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

Conditional Expected Value: Formula, Worked Example and Excel/R/Python

How to calculate expected value and conditional expected value, with a worked example, a reading guide, and Excel, R and Python snippets.

Written and maintained by CASRAI Editorial Board

Last updated

The conditional expected value of X given Y = y is the average of X when you only look at outcomes where Y equals y. You compute it exactly like an ordinary expected value, except the probabilities are the conditional probabilities P(X = x | Y = y), which are the joint probabilities divided by P(Y = y). This guide starts from the plain expected-value calculation, then adds the conditioning step, with every number worked by hand and checked in code.

Definition in one paragraph

The expected value (mean) of a discrete random variable is the probability-weighted average of its possible values. The conditional expected value, written E[X | Y = y], is the same weighted average after you restrict attention to the outcomes in which Y = y and rescale the probabilities so they again sum to 1. It answers questions such as “what is the average of X among the cases where Y = 0?” Because it depends on y, it is a function of y, and E[X | Y] (with no value of y fixed) is itself a random variable.

How is expected value calculated? The unconditional formula

For a discrete random variable X, multiply each possible value by its probability and add the products. OpenStax Introductory Statistics 2e states that the mean of a discrete probability function is the expected value, with the formula:

μ = E[X] = Σ x · P(x)

  • x: one possible value of the random variable X.
  • P(x): the probability that X takes the value x.
  • Σ: the sum over every possible value of x. The probabilities must add to 1.
  • μ or E[X]: the expected value, two notations for the same quantity.

How to find expected value from a table

This is the same recipe whether you ask how to get, find, compute or calculate the expected value: list the values, list their probabilities, multiply row by row, then add. OpenStax Example 4.3 counts the days per week a student plays soccer: x = 0, 1, 2 with P(x) = 0.2, 0.5, 0.3.

x P(x) x · P(x)
0 0.2 0
1 0.5 0.5
2 0.3 0.6
Total 1.0 1.1

So E[X] = 0 + 0.5 + 0.6 = 1.1 days. No one plays exactly 1.1 days; the expected value is a long-run average, not a value you must be able to observe.

The same book’s Example 4.5 shows a game that costs $2 to play and pays a profit of $100,000 if all five numbers match, with P(win) = 0.00001 and P(lose) = 0.99999. Then μ = (−2)(0.99999) + (100,000)(0.00001) = −1.99998 + 1 = −0.99998, about −$1 per game over many plays. That is how the expected value of a bet is read: as the average result per play in the long run. For the special case of a binomial count, see the expected value of a binomial distribution.

How do you calculate conditional expected value? Formula with every symbol explained

Pishro-Nik, Introduction to Probability, Statistics, and Random Processes (section 5.1.5), gives the discrete formula:

E[X | Y = y] = Σ xᵢ · PX|Y(xᵢ | y), where PX|Y(xᵢ | y) = P(X = xᵢ, Y = y) / P(Y = y)

  • xᵢ: each possible value of X.
  • P(X = xᵢ, Y = y): the joint probability that X = xᵢ and Y = y happen together (see the joint probability formula).
  • P(Y = y): the marginal probability of the conditioning event, the sum of the joint probabilities over all x for that y. It must be greater than 0; if it is 0 the conditional expectation is undefined.
  • PX|Y(xᵢ | y): the conditional probability mass function, which is a probability mass function in its own right (probability mass function formula).

The calculation has four steps: (1) pick the row or column of the joint table for the conditioning value y; (2) add its joint probabilities to get P(Y = y); (3) divide each joint probability by that total to get conditional probabilities; (4) multiply each x by its conditional probability and add.

Worked example with real numbers

The joint probability mass function used in Pishro-Nik section 5.1.5 has X and Y each taking the values 0 and 1, with P(0,0) = 1/5, P(0,1) = 2/5, P(1,0) = 2/5 and P(1,1) = 0. The four probabilities sum to 1/5 + 2/5 + 2/5 + 0 = 1.

Step 1, marginal of Y. P(Y = 0) = 1/5 + 2/5 = 3/5. P(Y = 1) = 2/5 + 0 = 2/5.

Step 2, conditional probabilities given Y = 0. P(X = 0 | Y = 0) = (1/5) / (3/5) = 1/3. P(X = 1 | Y = 0) = (2/5) / (3/5) = 2/3. They sum to 1, which confirms the rescaling worked.

Step 3, conditional expectation given Y = 0. E[X | Y = 0] = 0 · (1/3) + 1 · (2/3) = 2/3.

Step 4, given Y = 1. P(X = 0 | Y = 1) = (2/5) / (2/5) = 1 and P(X = 1 | Y = 1) = 0 / (2/5) = 0, so E[X | Y = 1] = 0.

Condition P(Y = y) P(X = 0 | y) P(X = 1 | y) E[X | Y = y]
Y = 0 3/5 1/3 2/3 2/3
Y = 1 2/5 1 0 0

Check with the law of total expectation. Averaging the conditional expectations over Y gives E[E[X | Y]] = (2/3)(3/5) + 0 · (2/5) = 2/5. Directly, E[X] = 0 · (3/5) + 1 · (2/5) = 2/5, since P(X = 1) = 2/5 + 0 = 2/5. The two agree, which is the identity E[X] = E[E[X | Y]] in Pishro-Nik’s text. If your conditional expectations do not average back to the unconditional one, the usual cause is a missing division by P(Y = y).

Interpretation: what the result means

E[X | Y = y] is the average of X among the cases where Y = y. Comparing it with E[X] shows whether knowing Y shifts your expectation of X. Here E[X] = 2/5, E[X | Y = 0] = 2/3 and E[X | Y = 1] = 0.

What you see Reading
E[X | Y = y] differs from E[X] Knowing that Y = y changes the average of X; here 2/3 versus 2/5 for Y = 0.
E[X | Y = y] equals E[X] for every y Knowing Y does not change the average of X.
The E[X | Y = y] values weighted by P(Y = y) They must add up to E[X] (law of total expectation); use this as a check.
A single value such as 2/3 A long-run average over the cases where Y = 0, not the most likely value of X.

Assumptions and common mistakes

Mistake Why it is wrong
Using joint probabilities directly, without dividing by P(Y = y) The weights no longer sum to 1. In the example, 0 · (1/5) + 1 · (2/5) = 2/5 is not E[X | Y = 0] = 2/3.
Conditioning on an event with probability 0 The formula divides by P(Y = y), so it is undefined when P(Y = y) = 0.
Treating E[X | Y] as a number E[X | Y = y] is a number for a fixed y; E[X | Y] is a random variable that takes the value E[X | Y = y] when Y = y.
Reading the expected value as the most likely value The expected value is a weighted average; the soccer example has E[X] = 1.1 although X can only be 0, 1 or 2.
Probabilities that do not sum to 1 Check Σ P(x) = 1 before multiplying.

This guide covers the discrete case. For events of the form “at least one”, see probability of at least one; for counts from repeated yes/no trials, see the binomial experiment.

How to report it

State the quantity, the conditioning event and the value, and give the calculation basis. For example: “Given Y = 0, the expected value of X was E[X | Y = 0] = 2/3 (0.67), compared with an unconditional expected value of E[X] = 0.40.” For an unconditional mean taken from a frequency table, see how to find the mean of a frequency distribution table.

How to calculate it in Excel, R or Python

All three snippets use the joint table from the worked example and return E[X | Y = 0] = 2/3.

Excel

Put X in A2:A5 (0, 0, 1, 1), Y in B2:B5 (0, 1, 0, 1) and the joint probability in C2:C5 (0.2, 0.4, 0.4, 0). The unconditional expected value, such as the soccer example with values in A2:A4 and probabilities in B2:B4, is =SUMPRODUCT(A2:A4,B2:B4), which returns 1.1. The conditional value is:

=SUMPRODUCT((B2:B5=0)*A2:A5*C2:C5)/SUMIF(B2:B5,0,C2:C5)

The numerator is 0.4 and the denominator is 0.6, giving 0.6667.

R

x = c(0, 0, 1, 1)
y = c(0, 1, 0, 1)
p = c(0.2, 0.4, 0.4, 0)
sum(x[y == 0] * p[y == 0]) / sum(p[y == 0])   # 0.6667

Python

x = [0, 0, 1, 1]
y = [0, 1, 0, 1]
p = [0.2, 0.4, 0.4, 0.0]
num = sum(xi * pi for xi, yi, pi in zip(x, y, p) if yi == 0)
den = sum(pi for yi, pi in zip(y, p) if yi == 0)
print(num / den)   # 0.6666...

FAQ

How to calculate expected probability?

“Expected probability” is not a separate quantity. If you mean the expected value of a random variable, multiply each value by its probability and add the products. If you mean the probability expected under a model, you calculate that probability first and then use it as the weight P(x) in the formula above.

How is expected value calculated?

Σ x · P(x): multiply each possible value by its probability and add (OpenStax Introductory Statistics 2e, section 4.2). For the soccer table the answer is 1.1.

How to compute, get or find expected value?

All four phrasings ask for the same calculation: list each possible value, list its probability, multiply the pairs and add the products, Σ x · P(x). In a spreadsheet, =SUMPRODUCT(values, probabilities) does it in one step.

How do you calculate the expected value of a game or bet?

Use the net result as x: a $2 stake that wins $100,000 with probability 0.00001 gives (−2)(0.99999) + (100,000)(0.00001) = −0.99998, about −$1 per play.

How do you calculate the conditional expected value?

Restrict the joint table to the condition, divide the joint probabilities by P(Y = y), then compute Σ x · P(x | y). For Y = 0 in the worked example the result is 2/3.

Sources

Date checked: 2026-10-29.

Follow CASRAI

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

Ask CASRAI · free to try

Ask about Conditional Expected Value: Formula, Worked Example and Excel/R/Python

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.