Conditioning with Densities
Conditional probability works the same way for continuous random variables, with one twist: we often want to condition on an event of probability zero, such as " exactly". This lesson covers conditioning on events of positive probability, the memoryless property of the exponential distribution, and conditional densities.
Conditioning on an event
If has density and is an event with , the conditional density of given is restricted to and rescaled:
For uniform on and , given is uniform on : conditioning a uniform on an interval gives a uniform.
The memoryless property
Let be exponential with rate , so . Given that , the chance of lasting more is
The past does not matter: a component with exponential lifetime that has run for 5 years is "as good as new". Among continuous distributions, only the exponential has this property, which is why it models events that happen "at random" with no ageing, such as radioactive decay or arrivals of independent requests.
import random
rng = random.Random(3)
lam, n = 0.5, 400_000
T = [rng.expovariate(lam) for _ in range(n)]
p_fresh = sum(t > 2 for t in T) / n
survivors = [t for t in T if t > 3]
p_after_3 = sum(t > 5 for t in survivors) / len(survivors)
print("P(T > 2) ~", round(p_fresh, 4))
print("P(T > 5 | T > 3) ~", round(p_after_3, 4))
print("exact e^(-1) =", round(2.718281828 ** -1, 4))
# a uniform lifetime on [0, 10] is NOT memoryless
U = [rng.uniform(0, 10) for _ in range(n)]
print("uniform: P(U > 2) =", round(sum(u > 2 for u in U) / n, 3),
" P(U > 5 | U > 3) =", round(sum(u > 5 for u in U) / sum(u > 3 for u in U), 3))P(T > 2) ~ 0.3679
P(T > 5 | T > 3) ~ 0.37
exact e^(-1) = 0.3679
uniform: P(U > 2) = 0.8 P(U > 5 | U > 3) = 0.714
The uniform lifetime "ages": having survived 3 years, it is closer to its hard limit of 10.
Conditional densities
For two continuous variables with joint density , the conditional density of given is
where is the marginal density of . This is the limit of conditioning on as . Details are in Joint Distributions.
Simulation approximates conditioning on by keeping samples with in a thin window around . Example: pick uniform on , then uniform on . Given , what is ? The joint density is on , so on , with mean .
import math
import random
rng = random.Random(8)
xs = []
for _ in range(400_000):
x = rng.random()
y = rng.uniform(0, x)
if abs(y - 0.5) < 0.01:
xs.append(x)
print("samples kept:", len(xs))
print("E[X | Y = 0.5] ~", round(sum(xs) / len(xs), 4))
print("exact (1 - y)/(-ln y) =", round(0.5 / -math.log(0.5), 4))samples kept: 5531
E[X | Y = 0.5] ~ 0.7189
exact (1 - y)/(-ln y) = 0.7213
Knowing tells us , and values of near 0.5 are favoured, since a small makes more concentrated near any value it allows.
In software
Memorylessness explains why "time already waited" does not predict "time left" for exponential processes, a fact behind retry logic and timeout analysis. Survival analysis (customer churn, hardware failure) studies distributions that do age, using the hazard rate , which is constant exactly for the exponential.
Exercises
Exercise 1: Conditioned uniform
is uniform on . Find .
Answer
.
Solution
Given , is uniform on , so .
Exercise 2: Waiting for a bus
Buses arrive as a Poisson process with an average gap of 10 minutes (exponential gaps). You have waited 8 minutes. What is the expected remaining wait?
Answer
10 minutes.
Solution
By memorylessness, the remaining wait is again exponential with mean 10. (With buses on a fixed 10-minute timetable, the answer would be 2 minutes.)
Exercise 3: Hazard rates (code)
Estimate the hazard rate from samples of an exponential (rate 0.5) and of a uniform on , at .
Answer
Exponential: about 0.5 at every (up to noise). Uniform: , rising to at .
Solution
import random
rng = random.Random(4)
n = 400_000
E = [rng.expovariate(0.5) for _ in range(n)]
U = [rng.uniform(0, 10) for _ in range(n)]
def hazard(samples, t, dt=0.1):
alive = [s for s in samples if s > t]
return sum(s <= t + dt for s in alive) / len(alive) / dt
for t in [1, 4, 8]:
print(t, round(hazard(E, t), 3), round(hazard(U, t), 3), round(1 / (10 - t), 3))1 0.491 0.113 0.111
4 0.482 0.161 0.167
8 0.428 0.5 0.5
The estimates for the exponential sit slightly below 0.5 because a window of width 0.1 is not infinitely thin (the exact value for this window is ), and at only about 2% of the samples survive, so that estimate is noisy.
Test yourself
Which continuous distribution is memoryless?
- Uniform
- Normal
- Exponential
- Beta
Conditioning a uniform distribution on on the event gives…
- a uniform distribution on
- an exponential distribution
- a point mass at 0.8
- the same distribution