Probability Basics › Expected Value and Variance

Michael BrenndoerferUpdated December 25, 202510 min read
Machine Learning from ScratchAll chapters
Part 1 of 9Statistics 101
Chapter 3Probability Basics › Expected Value and Variance
Reading section 5 of 12 · Expected Value and Variance · 10 minSection 5 of 12 · 10 min

Book contents

Section 5 of 12 · 10 min read

Probability Basics › Expected Value and VarianceOne section · Sections in this chapter5 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

While a full probability distribution gives complete information about a random variable, we often need summary statistics that capture the key features with just a few numbers. The two most important summary measures are the expected value and variance, which describe the center and spread of a distribution.

Expected Value

The expected value (or mean) of a random variable represents its long-run average value if we repeated the random process infinitely many times. For a discrete random variable XX with values x1,x2,…,xnx_1, x_2, \ldots, x_n and corresponding probabilities p1,p2,…,pnp_1, p_2, \ldots, p_n, the expected value is:

E[X]=∑i=1nxi⋅piE[X] = \sum_{i=1}^n x_i \cdot p_i

Example: Expected Number of Heads in Coin Flips

For a binomial random variable (like counting heads in coin flips), there's a convenient formula: E[X]=n⋅pE[X] = n \cdot p, where nn is the number of trials and pp is the probability of success on each trial. If we flip a fair coin 100 times, the expected number of heads is:

E[X]=100×0.5=50E[X] = 100 \times 0.5 = 50

This means that over many repetitions of 1000 coin flips, the average number of heads will converge to 500.

Example: Expected Value of a Fair Die Roll

Consider rolling a fair six-sided die. The random variable XX represents the outcome, which can be any value from 1 to 6, each with probability 16\frac{1}{6}:

E[X]=1⋅16+2⋅16+3⋅16+4⋅16+5⋅16+6⋅16=216=3.5E[X] = 1 \cdot \frac{1}{6} + 2 \cdot \frac{1}{6} + 3 \cdot \frac{1}{6} + 4 \cdot \frac{1}{6} + 5 \cdot \frac{1}{6} + 6 \cdot \frac{1}{6} = \frac{21}{6} = 3.5

Notice that the expected value of 3.5 is not even a possible outcome of a single die roll! This illustrates an important point: the expected value represents the long-run average, not necessarily an achievable result. If you rolled a die 1000 times and averaged all the results, you'd get very close to 3.5.

For a continuous random variable with probability density function f(x)f(x), the expected value is:

E[X]=∫−∞∞x⋅f(x) dxE[X] = \int_{-\infty}^{\infty} x \cdot f(x) \, dx

The expected value provides a measure of central tendency, answering the question: "What value should I expect on average?" However, knowing only the average doesn't tell us how much variation to expect around that average.

Example: Expected Value of an Exponential Distribution

Consider a continuous random variable XX following an exponential distribution with rate parameter λ=0.5\lambda = 0.5. The exponential distribution models waiting times between events (like time between customer arrivals or component failures). Its probability density function is:

f(x)=λe−λxfor x≥0f(x) = \lambda e^{-\lambda x} \quad \text{for } x \geq 0

To find the expected value, we compute the integral of xx weighted by the probability density function:

E[X]=∫0∞x⋅f(x) dx=∫0∞x⋅λe−λx dxE[X] = \int_0^{\infty} x \cdot f(x) \, dx = \int_0^{\infty} x \cdot \lambda e^{-\lambda x} \, dx

This integral requires integration by parts, which follows the formula:

∫u dv=uv−∫v du\int u \, dv = uv - \int v \, du

We strategically choose our parts to simplify the integral. The key insight is to let uu be the polynomial term (which simplifies when differentiated) and dvdv be the exponential term (which remains manageable when integrated):

u=x⇒du=dxdv=λe−λx dx⇒v=−e−λx\begin{aligned} u &= x & \Rightarrow && du &= dx \\ dv &= \lambda e^{-\lambda x} \, dx & \Rightarrow && v &= -e^{-\lambda x} \end{aligned}

To verify the calculation of vv: when we integrate λe−λx\lambda e^{-\lambda x}, we use substitution. Let w=−λxw = -\lambda x, so dw=−λ dxdw = -\lambda \, dx, which means dx=−1λdwdx = -\frac{1}{\lambda} dw. Then:

∫λe−λx dx=∫λew⋅(−1λ)dw=−∫ew dw=−ew=−e−λx\begin{aligned} \int \lambda e^{-\lambda x} \, dx &= \int \lambda e^{w} \cdot \left(-\frac{1}{\lambda}\right) dw \\ &= -\int e^{w} \, dw \\ &= -e^{w} \\ &= -e^{-\lambda x} \end{aligned}

Now we apply integration by parts:

E[X]=∫0∞x⋅λe−λx dx=[u⋅v]0∞−∫0∞v du=[x⋅(−e−λx)]0∞−∫0∞(−e−λx) dx=[−xe−λx]0∞+∫0∞e−λx dx\begin{aligned} E[X] &= \int_0^{\infty} x \cdot \lambda e^{-\lambda x} \, dx \\[1em] &= \left[u \cdot v\right]_0^{\infty} - \int_0^{\infty} v \, du \\[1em] &= \left[x \cdot \left(-e^{-\lambda x}\right)\right]_0^{\infty} - \int_0^{\infty} \left(-e^{-\lambda x}\right) \, dx \\[1em] &= \left[-x e^{-\lambda x}\right]_0^{\infty} + \int_0^{\infty} e^{-\lambda x} \, dx \end{aligned}

Evaluating the boundary term [−xe−λx]0∞\left[-x e^{-\lambda x}\right]_0^{\infty}:

At the lower limit (x=0x = 0):

−0⋅e−λ⋅0=−0⋅e0=−0⋅1=0-0 \cdot e^{-\lambda \cdot 0} = -0 \cdot e^{0} = -0 \cdot 1 = 0

At the upper limit (x→∞x \to \infty): We need to evaluate lim⁡x→∞−xe−λx\lim_{x \to \infty} -x e^{-\lambda x}. This is an indeterminate form of type ∞⋅0\infty \cdot 0, where the linear term xx grows without bound while the exponential term e−λxe^{-\lambda x} decays to zero. To resolve this, we rewrite it as a fraction and apply L'Hôpital's rule:

lim⁡x→∞−xe−λx=lim⁡x→∞−xeλx\lim_{x \to \infty} -x e^{-\lambda x} = \lim_{x \to \infty} \frac{-x}{e^{\lambda x}}

This is now an ∞∞\frac{\infty}{\infty} form, so we differentiate the numerator and denominator:

lim⁡x→∞−xeλx=lim⁡x→∞ddx(−x)ddx(eλx)=lim⁡x→∞−1λeλx=−1∞=0\lim_{x \to \infty} \frac{-x}{e^{\lambda x}} = \lim_{x \to \infty} \frac{\frac{d}{dx}(-x)}{\frac{d}{dx}(e^{\lambda x})} = \lim_{x \to \infty} \frac{-1}{\lambda e^{\lambda x}} = \frac{-1}{\infty} = 0

The exponential growth in the denominator dominates the linear growth in the numerator. Therefore:

[−xe−λx]0∞=0−0=0\left[-x e^{-\lambda x}\right]_0^{\infty} = 0 - 0 = 0

Evaluating the remaining integral ∫0∞e−λx dx\int_0^{\infty} e^{-\lambda x} \, dx:

Using the antiderivative ∫e−λx dx=−1λe−λx\int e^{-\lambda x} \, dx = -\frac{1}{\lambda} e^{-\lambda x} (which can be verified by differentiation):

∫0∞e−λx dx=[−1λe−λx]0∞=lim⁡x→∞(−1λe−λx)−(−1λe−λ⋅0)=(−1λ⋅0)−(−1λ⋅1)=0+1λ=1λ\begin{aligned} \int_0^{\infty} e^{-\lambda x} \, dx &= \left[-\frac{1}{\lambda} e^{-\lambda x}\right]_0^{\infty} \\[1em] &= \lim_{x \to \infty}\left(-\frac{1}{\lambda} e^{-\lambda x}\right) - \left(-\frac{1}{\lambda} e^{-\lambda \cdot 0}\right) \\[1em] &= \left(-\frac{1}{\lambda} \cdot 0\right) - \left(-\frac{1}{\lambda} \cdot 1\right) \\[1em] &= 0 + \frac{1}{\lambda} \\[1em] &= \frac{1}{\lambda} \end{aligned}

Note: As x→∞x \to \infty, we have e−λx→0e^{-\lambda x} \to 0 since λ>0\lambda > 0.

Combining the results:

E[X]=0+1λ=1λE[X] = 0 + \frac{1}{\lambda} = \frac{1}{\lambda}

For λ=0.5\lambda = 0.5, this gives E[X]=10.5=2E[X] = \frac{1}{0.5} = 2. This means that on average, we expect to wait 2 time units between events. The result is intuitive: a higher rate λ\lambda (more frequent events) leads to a shorter expected waiting time, while a lower rate leads to longer waits. The integral converges beciance

Variance

The variance measures how spread out the distribution is around its expected value. It quantifies the average squared deviation from the mean, giving us a sense of how much uncertainty or variability exists in the random variable. The variance of a random variable XX is defined as:

Var(X)=E[(X−E[X])2]=E[X2]−(E[X])2\text{Var}(X) = E[(X - E[X])^2] = E[X^2] - (E[X])^2

Where:

  • Var(X)\text{Var}(X) is the variance of the random variable XX
  • E[X]E[X] is the expected value (mean)
  • E[X2]E[X^2] is the expected value of the squared random variable

A closely related measure is the standard deviation, which is simply the square root of the variance: σ=Var(X)\sigma = \sqrt{\text{Var}(X)}. The standard deviation has the advantage of being in the same units as the original random variable, making it more interpretable. For example, if we're measuring heights in centimeters, the standard deviation is also in centimeters, while the variance would be in squared centimeters.

These summary statistics prove invaluable in practice. For example, when comparing investment options, the expected value tells us the average return, while the variance indicates the risk. When designing systems, knowing the expected load and its variability helps us provision appropriate capacity.

Example: Variance of the Exponential Distribution

For our exponential distribution with PDF f(x)=λe−λxf(x) = \lambda e^{-\lambda x}, we'll compute the variance using the formula Var(X)=E[X2]−(E[X])2\text{Var}(X) = E[X^2] - (E[X])^2.

We already found that E[X]=1λE[X] = \frac{1}{\lambda}, so (E[X])2=1λ2(E[X])^2 = \frac{1}{\lambda^2}.

Now we need to find E[X2]E[X^2]:

E[X2]=∫0∞x2⋅λe−λx dx=λ∫0∞x2e−λx dxE[X^2] = \int_0^{\infty} x^2 \cdot \lambda e^{-\lambda x} \, dx = \lambda \int_0^{\infty} x^2 e^{-\lambda x} \, dx

We'll use integration by parts twice to evaluate ∫0∞x2e−λx dx\int_0^{\infty} x^2 e^{-\lambda x} \, dx.

First integration by parts: Let u=x2u = x^2 and dv=e−λx dxdv = e^{-\lambda x} \, dx, then du=2x dxdu = 2x \, dx and v=−1λe−λxv = -\frac{1}{\lambda} e^{-\lambda x}.

∫0∞x2e−λx dx=[−x2λe−λx]0∞+2λ∫0∞xe−λx dx=0+2λ∫0∞xe−λx dx\begin{aligned} \int_0^{\infty} x^2 e^{-\lambda x} \, dx &= \left[-\frac{x^2}{\lambda} e^{-\lambda x}\right]_0^{\infty} + \frac{2}{\lambda} \int_0^{\infty} x e^{-\lambda x} \, dx \\[1em] &= 0 + \frac{2}{\lambda} \int_0^{\infty} x e^{-\lambda x} \, dx \end{aligned}

Note: The boundary term vanishes because lim⁡x→∞x2e−λx=0\lim_{x \to \infty} x^2 e^{-\lambda x} = 0 (exponential decay dominates polynomial growth) and the term at x=0x=0 is also zero.

Second integration by parts: For ∫0∞xe−λx dx\int_0^{\infty} x e^{-\lambda x} \, dx, let u=xu = x and dv=e−λx dxdv = e^{-\lambda x} \, dx, then du=dxdu = dx and v=−1λe−λxv = -\frac{1}{\lambda} e^{-\lambda x}.

∫0∞xe−λx dx=[−xλe−λx]0∞+1λ∫0∞e−λx dx=0+1λ⋅1λ=1λ2\begin{aligned} \int_0^{\infty} x e^{-\lambda x} \, dx &= \left[-\frac{x}{\lambda} e^{-\lambda x}\right]_0^{\infty} + \frac{1}{\lambda} \int_0^{\infty} e^{-\lambda x} \, dx \\[1em] &= 0 + \frac{1}{\lambda} \cdot \frac{1}{\lambda} \\[1em] &= \frac{1}{\lambda^2} \end{aligned}

Combining the results:

∫0∞x2e−λx dx=2λ⋅1λ2=2λ3\begin{aligned} \int_0^{\infty} x^2 e^{-\lambda x} \, dx &= \frac{2}{\lambda} \cdot \frac{1}{\lambda^2} = \frac{2}{\lambda^3} \end{aligned}

Therefore:

E[X2]=λ⋅2λ3=2λ2\begin{aligned} E[X^2] &= \lambda \cdot \frac{2}{\lambda^3} \\[1em] &= \frac{2}{\lambda^2} \end{aligned}

Computing the variance:

Var(X)=E[X2]−(E[X])2=2λ2−1λ2=1λ2\begin{aligned} \text{Var}(X) &= E[X^2] - (E[X])^2 \\[1em] &= \frac{2}{\lambda^2} - \frac{1}{\lambda^2} \\[1em] &= \frac{1}{\lambda^2} \end{aligned}

For λ=0.5\lambda = 0.5, this gives Var(X)=10.25=4\text{Var}(X) = \frac{1}{0.25} = 4, and the standard deviation is σ=4=2\sigma = \sqrt{4} = 2.

Notice that for the exponential distribution, the standard deviation equals the mean (σ=E[X]=1λ\sigma = E[X] = \frac{1}{\lambda}). This is a unique property of the exponential distribution and reflects its memoryless nature.the variability is proportional to the expected waiting time.

Example: Visualizing Expected Value and Variance

This example illustrates how expected value and variance characterize different aspects of a distribution. Two distributions can have the same mean but vastly different spreads.

Out[6]:
Visualization
Narrow normal density with a legend identifying μ=0, σ²=1 and the dashed expected-value line E[X]=0.
Low Variance Distribution (σ²=1): The distribution is tightly concentrated around the mean, indicating low uncertainty. Most observations fall within a narrow range, making predictions more reliable.
Wide normal density with a legend identifying μ=0, σ²=4 and the same dashed expected-value line E[X]=0.
High Variance Distribution (σ²=4): The distribution is widely spread around the same mean, indicating high uncertainty. Observations vary considerably, making individual predictions less reliable despite having the same expected value.