By Jamal Hussein · Last updated:
Expected Value Calculator E[X] = Σ x · p(x)
Paste a discrete probability distribution, one outcome per line as value probability or value,probability. Probabilities can be decimals or fractions such as 1/6. You get E[X], E[X²], the variance, the standard deviation and a chart of the distribution.
The expected value of a discrete random variable is the probability-weighted average of its possible values: multiply each value by its probability and add the results. This expected value calculator takes a list of values and probabilities and returns E[X], E[X²], the variance, the standard deviation and the sum of the probabilities, with a chart and the full working.
What is expected value?
Expected value, written E[X] or μ, is the long-run average of a random quantity. If you could repeat the same random experiment a very large number of times and average the outcomes, that average would settle on E[X]. Outcomes with a high probability pull the average strongly toward themselves; rare outcomes pull only a little, unless their values are extreme.
It is not the most likely outcome, and it does not have to be a possible outcome at all. A fair six-sided die has an expected value of 3.5, a number that never appears on any face. The most likely outcome is the mode, the middle outcome is the median (see the percentile calculator for that), and the expected value is the mean. For a skewed distribution all three can be far apart.
What is the expected value formula?
For a discrete random variable that takes the values \(x_1, x_2, \dots, x_n\) with probabilities \(p_1, p_2, \dots, p_n\):
Here \(x_i\) is one possible value, \(p_i = P(X = x_i)\) is the probability of that value, and \(n\) is the number of distinct outcomes. Every \(p_i\) must be at least 0 and together they must add up to 1. The spread around the expected value comes from the same kind of sum applied to the squared values:
The variance \(\operatorname{Var}(X)\) is the expected squared distance from the mean, and the standard deviation \(\sigma\) puts that spread back into the units of \(X\).
How do you calculate expected value step by step?
Take the fair die that loads as the default example above. Each face from 1 to 6 has probability 1/6. Multiply every face by 1/6 and add the products:
| x | p | x · p | x² · p |
|---|---|---|---|
| 1 | 1/6 = 0.1667 | 0.1667 | 0.1667 |
| 2 | 1/6 = 0.1667 | 0.3333 | 0.6667 |
| 3 | 1/6 = 0.1667 | 0.5000 | 1.5000 |
| 4 | 1/6 = 0.1667 | 0.6667 | 2.6667 |
| 5 | 1/6 = 0.1667 | 0.8333 | 4.1667 |
| 6 | 1/6 = 0.1667 | 1.0000 | 6.0000 |
| Total | 1 | 3.5000 | 15.1667 |
The third column sums to 21/6, so E[X] = 3.5. The fourth column sums to 91/6, so E[X²] = 15.1667. The variance is 15.1667 − 3.5² = 15.1667 − 12.25 = 2.9167, and the standard deviation is the square root of that, 1.7078. The calculator shows the same four numbers and the same table in its working panel.
The sign of the result carries meaning too. A bet that loses 1 unit with probability 37/38 and wins 35 units with probability 1/38 has E[X] = −37/38 + 35/38 = −2/38 = −0.0526. On average the player gives up about 5.3% of the stake per bet, even though the usual single outcome is a loss of exactly 1. The Roulette bet preset loads this case.
What is linearity of expectation?
Expectation is linear: E[aX + b] = a·E[X] + b for any constants a and b, and E[X + Y] = E[X] + E[Y] for any two random variables, whether or not they are independent. That is why the expected total of two dice is 3.5 + 3.5 = 7 without listing all 36 combinations. Variance does not share this property; variances only add when the variables are uncorrelated.
Where is expected value used in machine learning?
Much of machine learning is the estimation or minimisation of an expected value.
- Loss functions. The quantity a model is really meant to minimise is the expected loss (the risk) over the data distribution. That distribution is unknown, so training minimises the empirical risk instead: the plain average of the loss over the training sample, which is the expected value under a distribution that gives each example probability 1/n.
- Reinforcement learning. A value function is the expected cumulative reward from a state, and an action value is the expected reward of taking one action and following the policy afterwards. Agents choose actions by comparing these expectations.
- Cost-sensitive decisions. Combine the error types from a confusion matrix with a cost table and you can pick the action with the lower expected cost. If a transaction is fraudulent with probability 0.2, a missed fraud costs 100 and a wrongly blocked payment costs 5, then approving has expected cost 0.2 × 100 = 20 and blocking has expected cost 0.8 × 5 = 4, so blocking is the better decision.
- Cross-entropy. The cross-entropy between a true distribution p and a predicted distribution q is \(H(p, q) = -\sum_x p(x) \log q(x)\), which is the expected value of \(-\log q(X)\) when \(X\) is drawn from p. The predicted probabilities usually come out of a softmax layer.
In every case the arithmetic is the same weighted sum. Written with vectors, E[X] is simply the dot product of the value vector and the probability vector.
Common mistakes when calculating expected value
- Reading it as the most likely result. E[X] is an average over many repetitions, not a forecast of the next one.
- Using probabilities that do not sum to 1. Percentages typed as 20 instead of 0.2, or a missing outcome, give a meaningless total. This tool warns you and only rescales when you tick Normalise probabilities.
- Taking a plain average of the values. Adding the values and dividing by their count is only correct when every outcome is equally likely.
- Squaring in the wrong order. E[X²] is not (E[X])². For the die they are 15.1667 and 12.25, and the gap between them is exactly the variance.
- Dropping the sign of a loss. Losses must be entered as negative values, otherwise a losing bet looks profitable.
How do you calculate expected value in Python?
With NumPy the expected value is a dot product, or equivalently a weighted average:
import numpy as np
values = np.array([1, 2, 3, 4, 5, 6])
probs = np.full(6, 1 / 6)
ev = np.dot(values, probs)
ev_alt = np.average(values, weights=probs)
ev2 = np.dot(values ** 2, probs)
var = ev2 - ev ** 2
sd = np.sqrt(var)
print(round(ev, 6)) # 3.5
print(round(ev_alt, 6)) # 3.5
print(round(ev2, 6)) # 15.166667
print(round(var, 6)) # 2.916667
print(round(sd, 6)) # 1.707825
One difference matters: numpy.average divides by the sum of the weights, so it normalises silently, while numpy.dot does not. If the probabilities do not sum to 1 the two functions disagree, which is a quick way to catch a broken distribution.
Frequently asked questions
Can expected value be negative?
Yes. Expected value is negative whenever the probability-weighted losses outweigh the weighted gains. A roulette bet that loses 1 with probability 37/38 and wins 35 with probability 1/38 has an expected value of about −0.0526 per unit staked.
Is expected value the same as the mean?
Yes. The expected value is the mean of a probability distribution, also called the population mean. A sample mean is an estimate of it, and the estimate gets closer as the sample grows, which is the law of large numbers.
What if my probabilities do not add up to 1?
Then the list is not a valid probability distribution and the weighted sum is not an expected value. Check for a missing outcome or a typing error first. If the numbers are relative weights, divide each one by their total to normalise them.
How do you find the variance from the expected value?
Compute E[X²] by multiplying each squared value by its probability and adding, then subtract the square of E[X]. For a fair die that is 15.1667 − 12.25 = 2.9167, and the standard deviation is its square root, 1.7078.
Related calculators
Need the probabilities first? Counting problems start with the combination calculator, and sampling without replacement gives a ready-made distribution in the hypergeometric probability calculator. Every tool in this group is listed on the statistics calculators page.
More tools that pair with this one: