Continuous random variables — step-by-step proofs with the cafeteria microwave, integration by parts and the gamma function (and the average wait that came out negative)
The first time I worked out the mean of the exponential I got a negative average wait, as if the next person joined the queue before I even looked at it. It was −1/λ minutes. The mistake was in a single line: when integrating by parts, I wrote v=e−λx/λ instead of v=−e−λx/λ. One sign. Since then I write out the integration by parts table (u, du, dv, v) before touching anything, and it is the first thing you will see here.
This article is the extension of the continuous random variables theory: the results are there, and here is why they are true. These are the proofs asked for in the notes, the proposed exercises and the exam, with the same faculty cafeteria microwave. Each one goes step by step, with a single transformation per step and the rule that justifies it, and ends by checking the result with R.
Table of Contents
Before you start: the rules we will use
Every proof relies on these rules. We number them so we can cite them (“by rule 3”).
Linearity of the integral.∫(ag+bh)=a∫g+b∫h. It is what lets you pull out constants and split sums.
Integration by parts.∫udv=uv−∫vdu. You choose as u what simplifies when differentiated (a power of x) and as dv what you know how to integrate (an exponential).
Exponentials beat powers. For λ>0 and any n, xne−λx→0 as x→+∞. It is what makes the [uv]0∞ terms vanish.
The gamma function.Γ(α)=∫0∞tα−1e−tdt for α>0, with Γ(1)=1 and Γ(α+1)=αΓ(α) (we prove it in proof 4).
The recognise-a-density trick. If g is a density, ∫g=1. So if inside an integral you recognise a density “missing its constant”, the integral equals 1 over that constant. It is the rule that saves the most time.
Fundamental theorem of calculus.F(x)=∫−∞xf(t)dt implies F′(x)=f(x), and ∫abf=F(b)−F(a).
Shortcut variance.Var(X)=E(X2)−[E(X)]2 (proved in topic 3; the proof is identical with integrals).
The beta function.B(α,β)=∫01xα−1(1−x)β−1dx=Γ(α+β)Γ(α)Γ(β). The equality with the gammas is not proved in ME1: it is used.
In a density that depends on a constant, the condition “it is positive” is almost never the problem; what is asked for is the integral. But always write both things: f≥0and∫f=1. In the exam, the first one also earns marks.
Proof 1: a density with a constant, its F, its mean, its variance and its median
This is the format of the 15-point EC1 exercise. In the theory we modelled the moment a person arrives for lunch, as a fraction of the hour (0 = 13:00, 1 = 14:00), with a density of the form
f(x)=kx(1−x)2,0<x<1
and 0 outside. We want: (a) the value of k; (b) E(X) and Var(X); (c) the distribution function; (d) the mode; (e) the median.
Why it matters. This is the exercise that comes up most often in EC1, and the five parts are always the same: constant, mean and variance, F, mode and median. If you know the order, the exam almost writes itself. And each part reuses the previous one: k goes into every integral, and the F from part (c) is the one you set equal to 0.5 for the median.
(a) The value of k
What we want to prove. That there is a single k that makes f a density, and which one.
Step 1: positivity. For 0<x<1, x>0 and (1−x)2>0. So f(x)≥0 whenever k>0.
Step 2: expand the polynomial.x(1−x)2=x(1−2x+x2)=x−2x2+x3.
Where that 3010−15+6 comes from: the common denominator of 3, 2 and 5 is 30, so 31=3010, 21=3015 and 51=306. In the second moment, with 4, 5 and 6, it is 60.
Variance (rule 7):
Var(X)=51−(52)2=255−254=251=0.04
The mean arrival is at 0.4 of an hour (13:24), with a standard deviation of 0.04=0.2 hours: 12 minutes.
Multiplying only the first term of the bracket by x. x⋅(x−2x2+x3)=x2−2x3+x4: every term goes up one degree. If a lone x is left in the mean, check this line.
(c) The distribution function
What we want to prove. The expression of F(x)=P(X≤x) for every real x.
Step 1: left of the support. If x≤0, there is no density to the left of x: F(x)=0.
The step being skipped is the product rule. x(1−x)2 has two factors: differentiating x gives 1 and leaves (1−x)2; differentiating (1−x)2 gives 2(1−x)⋅(−1) (chain rule: the derivative of 1−x is −1) and leaves the x. That is where the −2x(1−x) comes from. Then you take out the common factor (1−x), which is in both terms, and inside you are left with 1−x−2x=1−3x.
It is zero at x=1 (the edge, where f=0) and at x=1/3. Between 0 and 1/3, f′>0 (the density rises) and between 1/3 and 1, f′<0 (it falls): x=1/3 is a maximum. The busiest time is 13:20.
(e) The median
You have to solve F(x)=0.5, that is, 3x4−8x3+6x2−0.5=0. It is a fourth-degree equation without a nice root, which is why the exam asks for it with R. Since F is increasing on (0,1), there is a single solution there. uniroot finds it (you have it below): 0.3857, that is, 13:23.
With the tail to the right, the order is mode < median < mean.
The drawing shows the three measures of center on the density: the mode (1/3) at the peak, the median (0.386) leaving half the area on each side and the mean (0.4) slightly further right, dragged by the long tail towards 1. It is the typical order for a density with positive skewness: mode < median < mean.
Result for the cheat sheet: f(x)=12x(1−x)2 on (0,1) is the beta(2,3): mean 2/5, variance 1/25, mode 1/3. In general, recognising a beta saves all the integrals (proof 6).
Check it with R
g <- function(x) x * (1 - x)^2 # the density without the constant
k <- 1 / integrate(g, 0, 1)$value # (a)
f <- function(x) k * g(x)
mean_x <- integrate(function(x) x * f(x), 0, 1)$value # (b)
variance <- integrate(function(x) x^2 * f(x), 0, 1)$value - mean_x^2
F <- function(x) 6 * x^2 - 8 * x^3 + 3 * x^4 # (c)
median_x <- uniroot(function(x) F(x) - 0.5, c(0, 1))$root # (e)
mode_x <- optimize(f, c(0, 1), maximum = TRUE)$maximum # (d)
round(c(k = k, mean = mean_x, variance = variance, median = median_x, mode = mode_x), 4)
qbeta(0.5, 2, 3) # the median of the beta(2, 3)
Output:
k mean variance median mode
12.0000 0.4000 0.0400 0.3857 0.3333
[1] 0.3857276
We get k=12, mean 0.4, variance 0.04 and mode 0.3333, as by hand. The median with uniroot (0.3857) matches qbeta(0.5, 2, 3) to the fourth decimal: they are the same variable. optimize(…, maximum = TRUE) looks for the maximum of a function in an interval; its $maximum is where it is, not how much it is worth.
Check you’ve got it: if the arrival time had density f(x)=kx2(1−x) on (0,1), what would k be? And the mean?
k=12 and mean 0.6. Expand the polynomial, x2(1−x)=x2−x3, and integrate: 31−41=121, so again k=12. For the mean, everything goes up one degree: 12(41−51)=2012=0.6. It is the previous density seen in a mirror (the beta(3,2)): people arrive later, at 13:36 on average instead of 13:24.
Proof 2: the uniform (mean, variance, mgf and how it stretches)
What we want to prove. If X∼Unif(a,b), with f(x)=1/(b−a) on (a,b):
and, also, that a+(b−a)U is Unif(a,b) if U∼Unif(0,1).
Why it matters. The uniform is the simplest distribution and the one used most without saying so: the old microwave in proof 5 is one, and R generates any other variable from uniforms between 0 and 1. The last part, stretching, is the idea that comes back in topic 5 with transformations.
That numerator comes from turning both fractions into twelfths. The first is multiplied top and bottom by 4: 4(b2+ab+a2). The second by 3, expanding the square: 3(a2+2ab+b2). Subtracting, 4b2−3b2=b2, 4ab−6ab=−2ab and 4a2−3a2=a2: you are left with (b−a)2.
Writing 2(b−a)b3−a3 in E(X2), dragging the 2 from the mean. Integrating x2 gives x3/3: the denominator is 3.
and φX(0)=1 (like any mgf). Here, differentiating the mgf to get the mean is longer than integrating directly (you have to take a limit at t=0): that is why, for the uniform, moments are computed from the definition.
How a uniform stretches
If U∼Unif(0,1) and X=a+(b−a)U, for a<x<b:
FX(x)=P(a+(b−a)U≤x)=P(U≤b−ax−a)=b−ax−a
because b−ax−a is between 0 and 1 and there FU(u)=u. That is the F of the Unif(a,b). Differentiating, the density is 1/(b−a).
Multiplying by b − a widens the rectangle and lowers it; adding a shifts it. The area is still 1.
The drawing shows what the transformation does: it stretches the interval (0,1) until it measures b−a and shifts it so it starts at a. Since the area must still be 1, the height drops from 1 to 1/(b−a). With the old microwave from proof 5, which takes between 1 and 5 minutes, X=1+4U: mean 3 and variance 16/12=1.33.
Result for the cheat sheet: Unif(a,b): f=b−a1, F(x)=b−ax−a, mean 2a+b, variance 12(b−a)2, mgf t(b−a)etb−eta.
Check it with R
a <- 1; b <- 5 # the old microwave
mean_x <- integrate(function(x) x * dunif(x, a, b), a, b)$value
variance <- integrate(function(x) x^2 * dunif(x, a, b), a, b)$value - mean_x^2
mgf_num <- integrate(function(x) exp(0.3 * x) * dunif(x, a, b), a, b)$value
mgf_formula <- (exp(0.3 * b) - exp(0.3 * a)) / (0.3 * (b - a))
round(c(mean = mean_x, variance = variance, formula = (b - a)^2 / 12,
mgf_num = mgf_num, mgf_formula = mgf_formula), 4)
set.seed(4)
u <- runif(100000)
round(c(simulated_mean = mean(a + (b - a) * u), simulated_var = var(a + (b - a) * u)), 2)
Output:
mean variance formula mgf_num mgf_formula
3.0000 1.3333 1.3333 2.6099 2.6099
simulated_mean simulated_var
3.00 1.33
The mean is 3 and the variance 1.3333, the same as (5−1)2/12. The mgf at t=0.3, integrated numerically, matches the formula (2.6099). And the 100,000 stretched uniforms have mean and variance 3 and 1.33 again: stretching (0,1) gives the Unif(1,5).
Check you’ve got it: with the old microwave (a=1, b=5), what is E(X2)? Does the variance 1.33 follow from it?
E(X2)=3b2+ab+a2=325+5+1=331=10.33. With rule 7, Var(X)=331−32=331−27=34=1.33, the same as (5−1)2/12. If you carry over the 2 from the mean (the error box), you get E(X2)=124/8=15.5 and a variance of 6.5, which is way off.
Proof 3: the exponential (mean and variance by parts, mgf, mode and memorylessness)
What we want to prove. If T∼exp(λ), with f(t)=λe−λt for t>0:
E(T)=λ1,Var(T)=λ21,φT(t)=λ−tλ(t<λ),
its mode is at 0 and P(T>s+t∣T>s)=P(T>t).
Why it matters. The exponential is the model for “how long until something happens”: the next person in the queue, the next breakdown. This is where the two techniques of the whole article show up, integration by parts and the mgf, plus memorylessness, which the exam asks you to prove and interpret.
It is a density, and its F
It is positive for λ>0. For t>0, integrating (with dude−λu=−λe−λu):
F(t)=∫0tλe−λudu=[−e−λu]0t=1−e−λt
As t→∞, F(t)→1: it integrates to 1. Along the way we get the survival function, S(t)=1−F(t)=e−λt.
The mean, by parts
E(T)=∫0∞tλe−λtdt
Step 1: the parts table (rule 2). Differentiate the power and integrate the exponential:
Choose
And then
u
t
du=dt
dv
λe−λtdt
v=−e−λt
Step 2: apply the rule.
E(T)=[−te−λt]0∞+∫0∞e−λtdt
Step 3: the bracket term. At ∞, te−λt→0 (rule 3); at 0, it is 0. The bracket is 0−0=0.
Step 4: the remaining integral.∫0∞e−λtdt=[−λe−λt]0∞=0−(−λ1)=λ1.
E(T)=λ1
The sign of v decides the result. Differentiating v to check it takes five seconds.
Look at the drawing, because it is exactly where I went wrong. The v carries a minus sign: the derivative of −e−λt is +λe−λt, which is the dv. If you forget it, step 2 ends up with the opposite sign and the mean comes out as −1/λ. A negative wait is the alarm bell.
Checking v means differentiating it: if v′= what was in dv, something is wrong. It takes five seconds and saves the proof.
The trick in the third step is to multiply and divide by λ so that ∫tλe−λt appears, which is the mean we already have: no need to integrate by parts again. By rule 7:
Var(T)=λ22−λ21=λ21
The standard deviation of the exponential equals its mean. With λ=0.5, both are 2 minutes.
The last step only works if λ−t>0: if t≥λ, the exponential e−(λ−t)x does not go to 0 and the integral is infinite. That is why the mgf exists only for t<λ. Differentiating:
The steps squeezed into the mgf. First the two exponentials are combined: etxe−λx=e(t−λ)x=e−(λ−t)x. When you evaluate the bracket, at ∞ it is 0 (if λ−t>0) and at 0 it is −λ−t1; subtracting something negative means adding it, and you get λ−tλ. To differentiate, write λ(λ−t)−1: the exponent comes down and becomes −2, and the derivative of what is inside, λ−t, is −1. Minus times minus, plus.
The same E(T) and E(T2) as by parts, without integrating.
The mode is at 0
f′(t)=−λ2e−λt<0 for every t>0: the density is strictly decreasing. Its maximum is at the left edge, t=0, and is f(0)=λ. The mode does not come from setting the derivative to 0 (it is never zero): it comes from seeing that the function always goes down.
Memorylessness
What we want to prove. For s,t>0, P(T>s+t∣T>s)=P(T>t).
Step 1: the definition of conditional probability (topic 2):
P(T>s+t∣T>s)=P(T>s)P(T>s+t∩T>s)
Step 2: the intersection. If T>s+t, in particular T>s: the event {T>s+t} is inside {T>s} and the intersection is {T>s+t}.
Step 3: the survival function.
P(T>s)P(T>s+t)=e−λse−λ(s+t)=e−λt=P(T>t)
Having waited s minutes does not change what is left.
Result for the cheat sheet: exp(λ): F(t)=1−e−λt, S(t)=e−λt, mean 1/λ, variance 1/λ2, mgf λ/(λ−t) for t<λ, mode 0, memoryless.
Check it with R
lambda <- 0.5
f <- function(t) lambda * exp(-lambda * t)
ET <- integrate(function(t) t * f(t), 0, Inf)$value
ET2 <- integrate(function(t) t^2 * f(t), 0, Inf)$value
mgf <- integrate(function(x) exp(0.2 * x) * f(x), 0, 200)$value # t = 0.2 < lambda; up to 200 (see below)
round(c(ET = ET, ET2 = ET2, Var = ET2 - ET^2, mgf = mgf, formula = lambda / (lambda - 0.2)), 4)
# memorylessness: 3 minutes waiting already, then 2 more
(1 - pexp(5, lambda)) / (1 - pexp(3, lambda))
1 - pexp(2, lambda)
Output:
ET ET2 Var mgf formula
2.0000 8.0000 4.0000 1.6667 1.6667
[1] 0.3678794
[1] 0.3678794
With λ=0.5: mean 2, E(T2)=8=2/0.25, variance 4 and the mgf at t=0.2, 1.6667, the same as 0.5/0.3. Why is the mgf integrated up to 200 and not to Inf? Because integrate evaluates the function at huge points, and there exp(0.2 * x) is infinite and f(x) is 0: R does not know what infinity times zero is and stops with “non-finite function value”. Beyond 200 the integrand is of the order of e−60: cutting there does not change a single decimal. The last two lines are memorylessness with numbers: the probability of waiting 2 more minutes after 3 with nobody is the same (0.3679) as waiting 2 from the start.
Check you’ve got it: with λ=0.5, what is E(T3)? Do it without integrating by parts again.
It is 48. With u=t3, the bracket vanishes (rule 3) and you are left with λ3∫0∞t2λe−λtdt=λ3E(T2). Since E(T2)=λ22, E(T3)=λ33⋅2=0.1256=48. The mgf gives the same: φT′′′(t)=(λ−t)46λ and at t=0, λ36.
Proof 4: the gamma (the Γ function, mean, variance, mgf and mode)
What we want to prove. First, two properties of Γ: Γ(α+1)=αΓ(α) and Γ(n)=(n−1)!. Then, that if X∼gamma(α,β):
Why it matters. The gamma is the wait until several things happen (with three people ahead of you, the third one leaving the microwave). But what you will use most from here is the integral ∫0∞xa−1e−bxdx=Γ(a)/ba: with it, the mean, the variance and the mgf come out without integrating by parts even once.
The Γ function
Γ(1)=1.Γ(1)=∫0∞e−tdt=[−e−t]0∞=1.
Γ(α+1)=αΓ(α), by parts with u=tα, du=αtα−1dt, dv=e−tdt, v=−e−t:
(the bracket vanishes by rule 3 at ∞ and because tα=0 at 0).
Why there is a + in front of the integral: the rule is uv−∫vdu, and here vdu=−e−t⋅αtα−1dt. That minus cancels the minus in the rule. And at 0 the bracket is 0 because α>0: 0α=0.
Γ(n)=(n−1)!. Applying the previous one over and over: Γ(n)=(n−1)Γ(n−1)=(n−1)(n−2)Γ(n−2)=⋯=(n−1)(n−2)⋯1⋅Γ(1)=(n−1)!.
Γ(n) = (n − 1)! at the integers, and a smooth curve between them.
In the drawing, the curve of Γ(x) goes exactly through the factorials: Γ(1)=0!=1, Γ(2)=1!=1, Γ(3)=2!=2, Γ(4)=3!=6. In between it fills the gap smoothly: for example, Γ(1/2)=π≈1.77, which will appear with the normal.
The integral that solves everything
What we want to prove. For a>0 and b>0:
∫0∞xa−1e−bxdx=baΓ(a)
Step 1: change of variableu=bx, that is, x=u/b and dx=du/b. The limits do not change (0 and ∞).
Step 2: substitute.
∫0∞(bu)a−1e−ubdu=ba−1⋅b1∫0∞ua−1e−udu=baΓ(a)
The bookkeeping of the powers of b: (bu)a−1=ba−1ua−1, and dx=du/b puts another b in the denominator. Together, ba−1⋅b=ba. What is left inside is the definition of Γ(a) (rule 4). The limits do not change because b>0: if x=0, u=0; if x→∞, u→∞.
With a=α and b=β we already have that the gamma integrates to 1: Γ(α)βα⋅βαΓ(α)=1. This integral is rule 5 applied to the gamma: every time you see xsomethinge−something⋅x between 0 and ∞, do not integrate by parts; write down the result.
The same integral with b=β−t, which must be positive: hence the condition t<β. With α=1 it is the mgf of the exponential, and raised to α it is the product of α equal exponential mgfs: that is why a sum of α independent exponentials is gamma (in topic 5 it is proved with the mgf of a sum).
The mode, with the logarithm
For α>1, maximising f is the same as maximising logf (the logarithm is increasing), and differentiating the logarithm is much easier:
logf(x)=αlogβ−logΓ(α)+(α−1)logx−βx
dxdlogf(x)=xα−1−β=0⇒x=βα−1
The derivative goes from positive to negative at that point, so it is a maximum. With α=1 the mode is 0 (the exponential). For the wait with three people ahead, gamma(3,0.5): mode 2/0.5=4 minutes.
Result for the cheat sheet: Γ(n)=(n−1)!, ∫0∞xa−1e−bxdx=Γ(a)/ba. gamma(α,β): mean α/β, variance α/β2, mgf (β/(β−t))α for t<β, mode (α−1)/β.
Check it with R
c(gamma(5), factorial(4), gamma(0.5)^2, pi) # Γ(5) = 4! and Γ(1/2)^2 = π
alpha <- 3; beta <- 0.5 # three people ahead
f <- function(x) dgamma(x, alpha, beta)
mean_x <- integrate(function(x) x * f(x), 0, Inf)$value
variance <- integrate(function(x) x^2 * f(x), 0, Inf)$value - mean_x^2
mgf <- integrate(function(x) exp(0.2 * x) * f(x), 0, 200)$value # up to 200, not Inf
mode_x <- optimize(f, c(0, 20), maximum = TRUE)$maximum
round(c(mean = mean_x, variance = variance, mgf = mgf,
formula = (beta / (beta - 0.2))^alpha, mode = mode_x), 4)
Output:
[1] 24.000000 24.000000 3.141593 3.141593
mean variance mgf formula mode
6.0000 12.0000 4.6296 4.6296 4.0000
Γ(5)=24=4! and Γ(1/2)2=π. For the gamma(3,0.5), the numerical integrals give mean 6, variance 12, the mgf at t=0.2 equal to (0.5/0.3)3=4.6296 and the mode at 4.
Check you’ve got it: what is ∫0∞x3e−2xdx? Without integrating by parts.
3/8=0.375. Compare it with xa−1e−bx: the exponent of x is a−1=3, so a=4 (not 3, the usual mistake), and b=2. So the integral is 24Γ(4)=163!=166=83. By parts it would take three rounds.
Proof 5: a mixture of densities (total probability and Bayes with times)
This is the other half of the 15-point EC1 exercise, here with different numbers. The cafeteria has three microwaves:
Microwave
Used by
Time each person takes, T
The new one (N)
50%
Exponential with mean 2 min: exp(0.5)
The old one (O)
30%
Uniform between 1 and 5 min
The back one (B)
20%
Gamma with mean 3 and variance 3: gamma(3,1)
(For the back one, β=μ/σ2=3/3=1 and α=μ2/σ2=9/3=3.)
Why it matters. This is where topic 2 and this one meet: total probability and Bayes, but with an F on each branch instead of a number. It is the second half of the 15-point exercise and the hardest one to set up, because you have to see that “which microwave” is the partition.
A mixture of densities is a density
What we want to prove. If f1,…,fm are densities and p1,…,pm≥0 add up to 1, then f=∑pkfk is a density.
Positivity. Each pkfk(x)≥0 (the product of two non-negative quantities), and a sum of non-negative things is non-negative.
It integrates to 1. By rule 1, ∫∑pkfk=∑pk∫fk=∑pk⋅1=1.
The condition pk≥0 is needed: with a negative weight, the mixture could take negative values.
The distribution function, by total probability
What we want to prove.P(T<t)=0.5FN(t)+0.3FO(t)+0.2FB(t).
Step 1: the microwaves are a partition. Each person uses one and only one of the three.
Step 2: total probability (topic 2) with that partition:
P(T<t)=P(T<t∣N)P(N)+P(T<t∣O)P(O)+P(T<t∣B)P(B)
Step 3: each conditional is the F of its model.P(T<t∣N)=FN(t)=1−e−0.5t; P(T<t∣O)=FO(t)=(t−1)/4 between 1 and 5 (0 before 1); P(T<t∣B)=FB(t), that of the gamma(3,1), computed with pgamma.
P(T<t)=0.5(1−e−0.5t)+0.3FO(t)+0.2FB(t)
For t=2: 0.5⋅0.6321+0.3⋅0.25+0.2⋅0.3233=0.4557. 46% of people finish in under 2 minutes.
Where each number comes from. FN(2)=1−e−1=0.6321. FV(2)=(2−1)/4=0.25: one minute out of the four possible. For FD(2), if you do not have R at hand, integrating the gamma(3,1) by parts twice gives FD(t)=1−e−t(1+t+2t2), and at 2, 1−5e−2=0.3233. The three products, 0.3161, 0.075 and 0.0647, are the Bayes numerators below.
On each branch, the probability of "under 2 minutes" is the F of its model evaluated at 2.
The tree in the drawing is the topic 2 one with one difference: on each branch, the probability of “under 2 minutes” is not a given number, it is an F evaluated at 2. The three branches ending in coral add up to 0.4557.
Bayes: which microwave did they use?
Someone comes out with their lunchbox in under 2 minutes. What is the probability they used the new one? By Bayes:
and, in the same way, P(O∣T<2)=0.075/0.4557=0.1646 and P(B∣T<2)=0.0647/0.4557=0.1419. The three add up to 1. Having finished fast raises the new one from 50% to 69%: it is the one with the most short times.
Doing the same with the variance. The variance of the mixture is not∑pkVark: you have to compute E(T2)=∑pkEk(T2) and subtract [E(T)]2. Besides the variation within each microwave, the mixture has the variation between microwaves.
Result for the cheat sheet: mixture f=∑pkfk; F(t)=∑pkFk(t); P(k∣T<t)=pkFk(t)/F(t); E(T)=∑pkEk(T).
Check it with R
p <- c(N = 0.5, O = 0.3, B = 0.2)
Fk <- function(t) c(N = pexp(t, 0.5), O = punif(t, 1, 5), B = pgamma(t, 3, 1))
Fmixture <- function(t) sum(p * Fk(t))
fmixture <- function(t) 0.5 * dexp(t, 0.5) + 0.3 * dunif(t, 1, 5) + 0.2 * dgamma(t, 3, 1)
integrate(fmixture, 0, Inf)$value # a mixture integrates to 1
round(Fk(2), 4) # each F at t = 2
round(Fmixture(2), 4) # total probability
round(p * Fk(2) / Fmixture(2), 4) # Bayes: which microwave
integrate(function(t) t * fmixture(t), 0, Inf)$value # the mean of the mixture
Output:
[1] 1
N O B
0.6321 0.2500 0.3233
[1] 0.4557
N O B
0.6935 0.1646 0.1419
[1] 2.5
The mixture integrates to 1. The three F values at 2 minutes, the total (0.4557) and the three Bayes probabilities match the hand calculations, and the mean of the mixture is 2.5 minutes. p * Fk(2) multiplies each weight by its F element by element, and dividing the vector by the total does the three Bayes divisions at once.
Check you’ve got it: what is the variance of the mixture? Why is it not 0.5⋅4+0.3⋅1.33+0.2⋅3=3?
3.25. As the error box says, you go through E(T2), which is the weighted mean of the second moments. Each one is its variance plus its mean squared: 4+22=8 for the new one, 1.33+32=10.33 for the old one and 3+32=12 for the back one. Weighted, 0.5⋅8+0.3⋅10.33+0.2⋅12=9.5, and Var(T)=9.5−2.52=3.25. The 3 in the question is only the variation within each microwave; the missing 0.25 is the variation between them, because their means (2, 3 and 3) do not match the mixture’s.
Proof 6: the beta (integrates to 1, mean, variance and mode)
What we want to prove. If X∼beta(α,β), with f(x)=B(α,β)xα−1(1−x)β−1 on (0,1):
Why it matters. The beta models proportions and fractions (like the arrival time in proof 1, which turned out to be one). And it closes the article for a reason: the three formulas come out of the gamma results, without a single new integral. If you forget them in the exam, you rebuild them in two lines.
It integrates to 1
It is positive on (0,1) and, by the definition of B (rule 8), ∫01xα−1(1−x)β−1dx=B(α,β): divided by B(α,β), it integrates to 1. With α=2 and β=3, B(2,3)=Γ(5)Γ(2)Γ(3)=241⋅2=121: that is why proof 1 gave k=12.
The mean
Multiplying by x raises the exponent of x by 1, and the integral is a B again (rule 5 with the beta):
With x2, the exponent goes up by 2 and two factors appear:
E(X2)=B(α,β)B(α+2,β)=(α+β+1)(α+β)(α+1)α
What is skipped here is the same as in the mean, twice: Γ(α+2)=(α+1)αΓ(α) on top and Γ(α+β+2)=(α+β+1)(α+β)Γ(α+β) below. The Γ(α), Γ(β) and Γ(α+β) cancel, and the loose factors remain.
The sign of the second term comes from the chain rule: the derivative of log(1−x) is 1−x1⋅(−1). Then you move it to the other side and cross-multiply; solving, α−1=x(α−1)+x(β−1)=x(α+β−2).
With α=2, β=3: 1/3, 13:20. If α or β is 1 or less, the density has no maximum inside (0,1) and the mode goes to an endpoint (or to the whole interval, for the uniform).
Result for the cheat sheet: B(α,β)=Γ(α)Γ(β)/Γ(α+β); beta(α,β): mean α/(α+β), variance αβ/((α+β)2(α+β+1)), mode (α−1)/(α+β−2).
Check it with R
c(1 / beta(2, 3), gamma(5) / (gamma(2) * gamma(3))) # the constant 12, two ways
a <- 2; b <- 3
mean_x <- integrate(function(x) x * dbeta(x, a, b), 0, 1)$value
variance <- integrate(function(x) x^2 * dbeta(x, a, b), 0, 1)$value - mean_x^2
mode_x <- optimize(function(x) dbeta(x, a, b), c(0, 1), maximum = TRUE)$maximum
round(c(mean = mean_x, formula_mean = a / (a + b), variance = variance,
formula_var = a * b / ((a + b)^2 * (a + b + 1)), mode = mode_x), 4)
beta(2, 3) is R’s B function (not the distribution): 1/B(2,3)=12, the same as with the gammas. The integrals give the mean 0.4, the variance 0.04 and the mode 0.3333 from the formulas.
Check you’ve got it: if the arrival time were the beta(3,2) from the first question, what would the mean, the variance and the mode be?
Mean 3/5=0.6, variance 0.04 and mode 2/3. The mean: 3+23. The variance: 52⋅63⋅2=1506=251, the same as the beta(2,3), because the formula does not change if you swap α and β: a density and its mirror image are equally spread out. The mode: 3+2−23−1=32, 13:40.
Prove it yourself
Three exercises in the style of the proposed exercises. Try each one before opening the solution.
1. Let F(x)=1−e−αxβ for x≥0 and F(x)=0 for x<0, with α,β>0 (it is the Weibull distribution, used for lifetimes with wear). Prove that it is a distribution function, find its density and its median.
Show the solution to exercise 1
It is a distribution function. It is continuous (a composition of continuous functions, and at 0 it is 1−e0=0, the same as from the left). On the left it is 0 and, as x→∞, αxβ→∞ and e−αxβ→0, so F→1. It is increasing because its derivative, the density below, is positive for x>0.
Density. Differentiating (chain rule): f(x)=F′(x)=αβxβ−1e−αxβ for x>0. With β=1 it is the exponential with rate α.
Calling p=1−e−λ, it is (1−p)kp for k=0,1,2,…: a geometric with parameter p (in R’s version, which counts failures). It makes sense: the full minutes that go by without anyone arriving are independent “failures”, because the exponential has no memory. With λ=0.5, p=1−e−0.5=0.39.
When to use each result
If you are asked for…
Use
Watch out for…
The constant of a density
f≥0 and ∫f=1
Writing the positivity too
F from f
∫startxf, piece by piece
The 0 and 1 pieces outside the support
The median or a quantile that cannot be solved for
uniroot(function(x) F(x) - p, interval)
Subtracting p and an interval with a sign change
The mode
Differentiate f (or logf) and set to 0
The edges: the exponential has its mode at 0
Mean or variance of an exponential
Parts, or the mgf
The sign of v=−e−λt
Mean or variance of a gamma or a beta
Recognise the integral (rules 5 and 8)
Γ(α+1)=αΓ(α)
E(ecX)
The mgf at t=c
That c is where the mgf exists
A probability with several “sources”
Total probability with the Fk
That the weights add up to 1
Which source it came from, given the result
Bayes
Dividing by the total, not by the branch
Common mistakes when proving
Forgetting positivity. “It is a density” means two conditions; the integral alone is not enough.
The sign of v in parts.∫e−λtdt=−e−λt/λ. A negative mean for a time gives the mistake away.
Leaving the brackets unevaluated.[−te−λt]0∞=0 must be justified (rule 3), not taken for granted.
Integrating a gamma by parts. It works, but with α=5 it is four integrations. Rule 5 does it in one line.
The mgf without its domain.λ/(λ−t) and (β/(β−t))α are only valid for t below the rate.
Adding variances in a mixture. You add the weighted means, not the variances.
Try it in the R playground
Open the site’s R playground (or RStudio) and paste this script. It checks the six proofs numerically at once:
# 1. Density with a constant (lunch arrivals)
k <- 1 / integrate(function(x) x * (1 - x)^2, 0, 1)$value
k
# 3. Exponential: mean and variance
integrate(function(t) t * dexp(t, 0.5), 0, Inf)$value
integrate(function(t) t^2 * dexp(t, 0.5), 0, Inf)$value - 2^2
# 4. Gamma: Γ(n) = (n - 1)!
gamma(1:6)
factorial(0:5)
# 5. Mixture of the three microwaves
fmixture <- function(t) 0.5 * dexp(t, 0.5) + 0.3 * dunif(t, 1, 5) + 0.2 * dgamma(t, 3, 1)
curve(fmixture, from = 0, to = 10, lwd = 2, ylab = "f(t)")
# 6. Beta: the constant of the beta(2, 3)
1 / beta(2, 3)
What to look at:
The first number is 12, the constant from proof 1. Change (1 - x)^2 to (1 - x)^3 and you will see the constant of the beta(2,4), which is 20.
gamma(1:6) and factorial(0:5) give the same vector: 1, 1, 2, 6, 24, 120.
The mixture curve has a step at 1 and another at 5: they are the edges of the old microwave’s uniform. A mixture can have a shape that no named model has.
In the Environment panel, k and fmixture stay stored: try integrate(fmixture, 0, 2)$value and compare it with the 0.4557 from proof 5.
Summary
Result
Formula
How it is proved
Constant of a density
∫f=1, f≥0
Integrate the polynomial
Uniform
mean 2a+b, variance 12(b−a)2
Difference of squares and of cubes
Exponential
mean λ1, variance λ21, mgf λ−tλ
By parts; memoryless with the conditional
Gamma function
Γ(α+1)=αΓ(α), Γ(n)=(n−1)!
By parts
Gamma
mean βα, variance β2α, mgf (β−tβ)α
∫xa−1e−bx=Γ(a)/ba
Mixture
F=∑pkFk, Bayes, E=∑pkEk
Total probability
Beta
mean α+βα, mode α+β−2α−1
Ratio of B functions
Three ideas to take away:
Almost everything comes from two tools: integration by parts (with the u, du, dv, v table written out) and recognising a density inside an integral so you do not have to integrate.
The properties from topic 3 still hold: shortcut variance, linearity, an mgf that generates moments. Swap the sum for the integral and everything fits.
The mode is found by differentiating, often the logarithm; the median, with uniroot when it cannot be solved for. And it is always checked with R.
Now it is time to take it to the computer: in the continuous random variables practice in R we simulate the microwave queue, check that three exponential waits add up to a gamma and fit a model to data. And in the exercises you have proofs like these in the exam format.
Data Science and Engineering student at ULPGC. I explain what I learn
at university the way I wish it had been explained to me: with its
doubts, its mistakes and the path to the solution.