TExcelTAD
Home › Probability Theory › Conditioning with Densities

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 "X=0.3X = 0.3 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 XX has density ff and EE is an event with Pr⁡[E]>0\Pr[E] > 0, the conditional density of XX given EE is ff restricted to EE and rescaled:

f(x∣E)={f(x)Pr⁡[E]x∈E0otherwisef(x \mid E) = \begin{cases} \dfrac{f(x)}{\Pr[E]} & x \in E \\ 0 & \text{otherwise} \end{cases}

For XX uniform on [0,1][0, 1] and E={X>0.6}E = \{X > 0.6\}, XX given EE is uniform on (0.6,1](0.6, 1]: conditioning a uniform on an interval gives a uniform.

The memoryless property

Let TT be exponential with rate λ\lambda, so Pr⁡[T>t]=e−λt\Pr[T > t] = e^{-\lambda t}. Given that T>sT > s, the chance of lasting tt more is

Pr⁡[T>s+t∣T>s]=e−λ(s+t)e−λs=e−λt=Pr⁡[T>t]\Pr[T > s + t \mid T > s] = \frac{e^{-\lambda(s+t)}}{e^{-\lambda s}} = e^{-\lambda t} = \Pr[T > t]

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.

python · try it yourself
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 f(x,y)f(x, y), the conditional density of XX given Y=yY = y is

f(x∣y)=f(x,y)fY(y)f(x \mid y) = \frac{f(x, y)}{f_Y(y)}

where fYf_Y is the marginal density of YY. This is the limit of conditioning on y≤Y≤y+Δyy \le Y \le y + \Delta y as Δy→0\Delta y \to 0. Details are in Joint Distributions.

Simulation approximates conditioning on Y=yY = y by keeping samples with YY in a thin window around yy. Example: pick XX uniform on [0,1][0, 1], then YY uniform on [0,X][0, X]. Given Y=0.5Y = 0.5, what is XX? The joint density is 1/x1/x on 0≤y≤x≤10 \le y \le x \le 1, so f(x∣y)∝1/xf(x \mid y) \propto 1/x on [y,1][y, 1], with mean 1−y−ln⁡y\frac{1 - y}{-\ln y}.

python · try it yourself
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 Y=0.5Y = 0.5 tells us X≥0.5X \ge 0.5, and values of XX near 0.5 are favoured, since a small XX makes YY 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 h(t)=f(t)/Pr⁡[T>t]h(t) = f(t)/\Pr[T > t], which is constant exactly for the exponential.

Exercises

Exercise 1: Conditioned uniform

XX is uniform on [0,4][0, 4]. Find Pr⁡[X<1∣X<3]\Pr[X < 1 \mid X < 3].

Answer

1/31/3.

Solution

Given X<3X < 3, XX is uniform on [0,3)[0, 3), so Pr⁡[X<1]=1/3\Pr[X < 1] = 1/3.

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 h(t)≈Pr⁡[t<T≤t+0.1∣T>t]/0.1h(t) \approx \Pr[t < T \le t + 0.1 \mid T > t] / 0.1 from samples of an exponential (rate 0.5) and of a uniform on [0,10][0, 10], at t=1,4,8t = 1, 4, 8.

Answer

Exponential: about 0.5 at every tt (up to noise). Uniform: 1/(10−t)1/(10 - t), rising to 0.50.5 at t=8t = 8.

Solution
python · try it yourself
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 (1−e−0.05)/0.1≈0.488(1 - e^{-0.05})/0.1 \approx 0.488), and at t=8t = 8 only about 2% of the samples survive, so that estimate is noisy.

Test yourself

Which continuous distribution is memoryless?

  1. Uniform
  2. Normal
  3. Exponential
  4. Beta

Conditioning a uniform distribution on [0,1][0, 1] on the event X>0.6X > 0.6 gives…

  1. a uniform distribution on (0.6,1](0.6, 1]
  2. an exponential distribution
  3. a point mass at 0.8
  4. the same distribution