STAT-251 · Lesson 5 of 12 · 24 min
Continuous random variables and densities
Lifetimes, rainfall amounts, response times, heights and measurement errors can take any value in an interval, not just values from a list. For such quantities the probability of any single exact value is zero, and a pmf is useless. Probability is instead spread smoothly along the line, like mass along a wire, and described by a density. This lesson defines continuous random variables and their densities, relates densities to cdfs through the fundamental theorem of calculus, works through the uniform and exponential families, and introduces the memoryless property and the hazard rate, the two ideas reliability engineers and actuaries use most.
Densities
A random variable \(X\) is continuous if there is a function \(f \ge 0\), its probability density function or pdf, such that for every interval
Taking the interval to be the whole line shows that \(\int_{-\infty}^{\infty} f(x)\,dx = 1\). Conversely, any non-negative integrable function with total integral 1 is the density of some random variable. Probability is area under the density curve. Two consequences follow at once. First, \(P(X = a) = \int_a^a f = 0\) for every \(a\), so for continuous variables it does not matter whether interval endpoints are included: \(P(a < X < b) = P(a \le X \le b)\). Second, a density value is not a probability and can exceed 1. The uniform density on \([0, 0.5]\) is 2 everywhere on the interval.
What a density value does measure is probability per unit length. For a small \(h\), \(P(x \le X \le x + h) \approx f(x)h\) when \(f\) is continuous at \(x\). A density of 0.3 per hour at \(t = 10\) hours means that the chance of failure between 10 hours and 10 hours and 1 minute is about \(0.3/60 = 0.005\). This is why densities change when the units change: if \(X\) is measured in minutes instead of hours, its density is divided by 60.
Density and cdf
The cdf of a continuous variable is \(F(x) = P(X \le x) = \int_{-\infty}^{x} f(t)\,dt\). It is continuous everywhere, since there are no jumps, and by the fundamental theorem of calculus \(F'(x) = f(x)\) at every point where \(f\) is continuous. So the density is the derivative of the cdf, and the cdf is the integral of the density. In practice we often find one from the other. Interval probabilities come straight from the cdf: \(P(a < X \le b) = F(b) - F(a)\).
Not every random variable is discrete or continuous. An insurance claim might be zero with probability 0.9 (no claim filed) and otherwise follow a density on the positive amounts. Its cdf jumps at 0 and then rises smoothly. Such mixed distributions are common in actuarial work and in data with a detection limit, and the cdf handles them without fuss: it is the one description that works for every random variable.
The uniform distribution
\(X\) is uniform on \([a, b]\) if its density is constant there: \(f(x) = 1/(b - a)\) for \(a \le x \le b\) and 0 elsewhere. Its cdf rises linearly from 0 at \(a\) to 1 at \(b\): \(F(x) = (x - a)/(b - a)\). The probability of any subinterval is its length divided by \(b - a\). The uniform distribution on \((0, 1)\) plays a special role, because computers generate it (approximately) and every other distribution can be built from it by the inverse transform of lesson 4. A rounding error, the arrival time of a bus that comes every 15 minutes at a random moment, and the phase of a wave are natural uniform variables.
The exponential distribution
\(X\) is exponential with rate \(\lambda > 0\) if \(f(x) = \lambda e^{-\lambda x}\) for \(x \ge 0\) and 0 for \(x < 0\). Integrating, \(F(x) = 1 - e^{-\lambda x}\) and the survival function is \(S(x) = P(X > x) = e^{-\lambda x}\). The exponential is the standard model for the waiting time until an event that happens at a constant rate: the time to the next radioactive decay, the time between calls at a help line, the time to failure of an electronic component with no wear. Lesson 6 shows that its mean is \(1/\lambda\), so a component failing at rate 0.002 per hour has a mean life of 500 hours.
The exponential distribution is memoryless: for all \(s, t \ge 0\),
A component that has survived \(s\) hours is, as far as its future is concerned, as good as new. That is exactly right for some things, such as radioactive atoms and fuses that fail only from random surges, and badly wrong for others, such as bearings, tires and people, which wear out. A deeper fact is that the exponential is the only continuous distribution with this property. If \(S\) is a survival function with \(S(s + t) = S(s)S(t)\) for all \(s, t \ge 0\), then \(g = \log S\) satisfies \(g(s + t) = g(s) + g(t)\); a monotone solution of this equation is linear, \(g(t) = -\lambda t\), so \(S(t) = e^{-\lambda t}\). Its discrete counterpart, the geometric distribution, is likewise the only memoryless distribution on the positive integers.
Hazard rates
For a lifetime \(T\) with density \(f\) and survival function \(S\), the hazard rate is
the instantaneous failure rate of items that have survived to time \(t\). The exponential has constant hazard \(\lambda\), which is another way of saying it is memoryless. A hazard that increases with age describes wear-out; one that decreases describes infant mortality, where weak items fail early and the survivors are the strong ones. Many products show both, giving the bathtub-shaped hazard of reliability engineering. Since \(h(t) = -\frac{d}{dt}\log S(t)\), the hazard determines the distribution: \(S(t) = \exp(-H(t))\), where \(H(t) = \int_0^t h(u)\,du\) is the cumulative hazard. Lesson 8 meets the Weibull family, whose hazard \(h(t) = k\lambda^k t^{k-1}\) increases, decreases or stays constant as \(k\) is above, below or equal to 1.
Worked problem
Warranty for a pump
A pump’s lifetime in years is exponential with rate 0.25. The maker offers a 2-year warranty. What fraction of pumps fail under warranty? Given that a pump is still working after 4 years, what is the probability it lasts at least 2 more?
Show solution
- \(P(T \le 2) = 1 - e^{-0.25 \times 2} = 1 - e^{-0.5} \approx 0.393\).
- By the memoryless property, \(P(T > 6 \mid T > 4) = P(T > 2) = e^{-0.5} \approx 0.607\).
- The median lifetime solves \(1 - e^{-0.25m} = 0.5\): \(m = 4\ln 2 \approx 2.77\) years, shorter than the mean of 4 years because the density is skewed to the right.
Answer: About 39% fail under warranty; a 4-year-old pump survives 2 more years with probability about 0.61, the same as a new one.
Normalizing constants and support
Densities are often given up to a constant, which is then found by integrating. If \(f(x) = cx^2\) on \([0, 3]\), then \(1 = c\int_0^3 x^2\,dx = 9c\), so \(c = 1/9\). The set where the density is positive is called the support. Always state it. The formula \(f(x) = cx^2\) means nothing until we say where it applies, and many errors in integration come from integrating a density beyond its support. For densities on unbounded supports, check that the integral converges: \(f(x) = c/x^2\) on \([1, \infty)\) works with \(c = 1\), but \(f(x) = c/x\) on \([1, \infty)\) cannot be normalized for any \(c\), because \(\int_1^\infty dx/x\) diverges.
Numerical integration
Many densities, including the normal, have no elementary antiderivative, so their probabilities are computed numerically. Simpson's rule, which fits parabolas through pairs of subintervals, is accurate enough for nearly every practical purpose and is easy to write. The code below normalizes \(f(x) = cx^2\) on \([0, 3]\), computes probabilities for the exponential pump model both exactly and with Simpson's rule, and checks the memoryless property by simulation.
import math, random
def simpson(f, a, b, n=1000): # n must be even
h = (b - a) / n
s = f(a) + f(b) + sum((4 if i % 2 else 2) * f(a + i * h) for i in range(1, n))
return s * h / 3
c = 1 / simpson(lambda x: x * x, 0, 3)
print(f"normalizing constant for c x^2 on [0, 3]: {c:.6f} (exact 1/9 = {1/9:.6f})")
lam = 0.25
f = lambda t: lam * math.exp(-lam * t)
print(f"P(T <= 2): Simpson {simpson(f, 0, 2):.6f}, exact {1 - math.exp(-0.5):.6f}")
print(f"total mass up to 80 years: {simpson(f, 0, 80):.6f}")
rng = random.Random(251)
T = [rng.expovariate(lam) for _ in range(200_000)]
alive4 = [t for t in T if t > 4]
p_cond = sum(t > 6 for t in alive4) / len(alive4)
p_new = sum(t > 2 for t in T) / len(T)
print(f"P(T > 6 | T > 4) simulated {p_cond:.4f}; P(T > 2) simulated {p_new:.4f}")
print(f"sample median {sorted(T)[len(T) // 2]:.3f} vs 4 ln 2 = {4 * math.log(2):.3f}")normalizing constant for c x^2 on [0, 3]: 0.111111 (exact 1/9 = 0.111111) P(T <= 2): Simpson 0.393469, exact 0.393469 total mass up to 80 years: 1.000000 P(T > 6 | T > 4) simulated 0.6083; P(T > 2) simulated 0.6069 sample median 2.785 vs 4 ln 2 = 2.773
Simpson's rule with 1,000 subintervals reproduces the exact answers to six decimals. The simulated conditional probability of surviving 2 more years among pumps that reached 4 years matches the unconditional probability of reaching 2 years, as memorylessness predicts, and the sample median lands near \(4\ln 2\). For a distribution with wear-out, such as a Weibull with \(k = 2\), the same simulation would show old pumps doing worse than new ones.
Percentiles in practice
Engineering and environmental standards are often written in terms of percentiles of continuous distributions. A rainfall that is exceeded with probability 0.01 in any year is called the 100-year rainfall, and drainage is designed to carry it; a structural load exceeded with probability 0.05 over the design life is a characteristic load. For a continuous cdf the \(q\) quantile solves \(F(x_q) = q\) exactly. For the exponential, \(x_q = -\ln(1 - q)/\lambda\). For the pump, the 90th percentile of lifetime is \(-\ln(0.1)/0.25 \approx 9.2\) years: one pump in ten lasts longer than that. The 100-year event, despite its name, has probability \(1 - 0.99^{30} \approx 0.26\) of occurring at least once in a 30-year mortgage, which is a fact many homeowners in flood-prone areas do not know.
Worked example: waiting at a bus stop
Worked problem
Uniform and exponential waits compared
Buses on a downtown route come exactly every 10 minutes, and a rider arrives at a random time. On a suburban route, buses arrive as a Poisson process with an average of one every 10 minutes. Compare the expected waits and the probabilities of waiting more than 8 minutes.
Show solution
- On the downtown route the wait \(W\) is uniform on \((0, 10)\), so \(E[W] = 5\) minutes and \(P(W > 8) = 2/10 = 0.2\).
- On the suburban route the wait is exponential with rate \(1/10\) by the memoryless property, so \(E[W] = 10\) minutes and \(P(W > 8) = e^{-0.8} \approx 0.449\).
Answer: Regular buses give an expected wait of 5 minutes; random buses with the same average frequency give 10 minutes, and a long wait is more than twice as likely.
The result can be surprising: both routes average one bus every 10 minutes, yet the random route doubles the expected wait. The explanation is the inspection paradox. A rider arriving at a random time is more likely to land in a long gap between buses than a short one, because long gaps cover more of the clock. With exponential gaps of mean 10, the gap containing a random arrival has mean 20 minutes, and the rider waits on average half of it. Transit agencies in Canadian cities track headway regularity for exactly this reason: bunching of buses raises the average wait even when the number of buses per hour is unchanged.
The same paradox appears in many surveys. Asking people currently in hospital how long their stay is overstates the typical stay, because long stays are more likely to be in progress on any given day. Asking students about the size of their classes overstates the average class size, because large classes contain more students to ask.
Common mistakes
MistakeReading \(f(x)\) as \(P(X = x)\).
InsteadFor a continuous variable \(P(X = x) = 0\); \(f(x)\) is probability per unit length and can exceed 1.
MistakeIntegrating a density outside its support.
InsteadState the support first and set the integrand to 0 outside it.
MistakeAssuming every quantity with a constant failure rate is reasonable.
InsteadThe exponential is memoryless; items that wear out need an increasing hazard, such as a Weibull with \(k > 1\).
MistakeForgetting that densities change with units.
InsteadIf \(Y = cX\) with \(c > 0\), then \(f_Y(y) = f_X(y/c)/c\).
MistakeThinking a 100-year flood happens once every 100 years.
InsteadIt has probability 0.01 in each year; over many years the chance of at least one is \(1 - 0.99^n\).
Summary and key terms
- A continuous variable has a density \(f \ge 0\) with \(\int f = 1\); probabilities are areas, and single points have probability 0.
- \(F(x) = \int_{-\infty}^x f\), and \(F' = f\) wherever \(f\) is continuous.
- The uniform density is constant on an interval; the exponential has \(f(x) = \lambda e^{-\lambda x}\) and survival \(e^{-\lambda x}\).
- The exponential is the only memoryless continuous distribution; equivalently its hazard rate is constant.
- The hazard \(h = f/S\) determines the distribution through \(S(t) = \exp(-H(t))\).
- Probabilities without closed forms are computed by numerical integration such as Simpson’s rule.
- probability density function
- A function \(f \ge 0\) whose integral over a set gives the probability of that set.
- support
- The set where the density is positive.
- survival function
- \(S(x) = P(X > x) = 1 - F(x)\).
- memoryless
- \(P(X > s + t \mid X > s) = P(X > t)\).
- hazard rate
- \(h(t) = f(t)/S(t)\), the instantaneous failure rate of survivors.
- mixed distribution
- A distribution with both point masses and a density part.
- quantile
- A value \(x_q\) with \(F(x_q) = q\).
Practice problems
Work each problem on paper first, then reveal the answer to check.
Find \(c\) so that \(f(x) = c(1 - x^2)\) on \([-1, 1]\) is a density, and find \(P(X > 1/2)\).
Show answer
\(\int_{-1}^1 (1 - x^2)\,dx = 4/3\), so \(c = 3/4\). \(P(X > 1/2) = \frac34\int_{1/2}^1 (1 - x^2)\,dx = \frac34 \cdot \frac{5}{24} = \frac{5}{32}\).
A cdf is \(F(x) = 1 - (1 + x)e^{-x}\) for \(x \ge 0\). Find the density.
Show answer
\(F'(x) = -e^{-x} + (1 + x)e^{-x} = xe^{-x}\) for \(x \ge 0\).
Show that if \(X\) is exponential with rate \(\lambda\), then \(cX\) is exponential with rate \(\lambda/c\) for \(c > 0\).
Show answer
\(P(cX > x) = P(X > x/c) = e^{-\lambda x/c}\).
A lifetime has hazard \(h(t) = 2t\) for \(t \ge 0\). Find its survival function and density.
Show answer
\(H(t) = t^2\), so \(S(t) = e^{-t^2}\) and \(f(t) = 2te^{-t^2}\): a Weibull with \(k = 2\).
For the uniform distribution on \([2, 10]\), find the 75th percentile.
Show answer
\((x - 2)/8 = 0.75\) gives \(x = 8\).
Show that \(f(x) = 1/(\pi(1 + x^2))\) is a density on \(\mathbb{R}\).
Show answer
It is positive and \(\int_{-\infty}^{\infty} \frac{dx}{1 + x^2} = [\arctan x]_{-\infty}^{\infty} = \pi\). This is the Cauchy density, which reappears in lesson 6.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.