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/λ-1/\lambda minutes. The mistake was in a single line: when integrating by parts, I wrote v=e−λx/λv = e^{-\lambda x}/\lambda instead of v=−e−λx/λv = -e^{-\lambda x}/\lambda. One sign. Since then I write out the integration by parts table (uu, dudu, dvdv, vv) 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.

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”).

  1. Linearity of the integral. ∫(a g+b h)=a∫g+b∫h\int (a\,g + b\,h) = a\int g + b\int h. It is what lets you pull out constants and split sums.
  2. Integration by parts. ∫u dv=u v−∫v du\int u\,dv = u\,v – \int v\,du. You choose as uu what simplifies when differentiated (a power of xx) and as dvdv what you know how to integrate (an exponential).
  3. Exponentials beat powers. For λ>0\lambda > 0 and any nn, xne−λx→0x^n e^{-\lambda x} \to 0 as x→+∞x \to +\infty. It is what makes the [u v]0∞[u\,v]_0^\infty terms vanish.
  4. The gamma function. Γ(α)=∫0∞tα−1e−t dt\Gamma(\alpha) = \int_0^\infty t^{\alpha – 1} e^{-t}\,dt for α>0\alpha > 0, with Γ(1)=1\Gamma(1) = 1 and Γ(α+1)=α Γ(α)\Gamma(\alpha + 1) = \alpha\,\Gamma(\alpha) (we prove it in proof 4).
  5. The recognise-a-density trick. If gg is a density, ∫g=1\int 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.
  6. Fundamental theorem of calculus. F(x)=∫−∞xf(t) dtF(x) = \int_{-\infty}^x f(t)\,dt implies F′(x)=f(x)F'(x) = f(x), and ∫abf=F(b)−F(a)\int_a^b f = F(b) – F(a).
  7. Shortcut variance. Var(X)=E(X2)−[E(X)]2\text{Var}(X) = E(X^2) – [E(X)]^2 (proved in topic 3; the proof is identical with integrals).
  8. The beta function. B(α,β)=∫01xα−1(1−x)β−1 dx=Γ(α) Γ(β)Γ(α+β)B(\alpha, \beta) = \int_0^1 x^{\alpha – 1}(1 – x)^{\beta – 1}\,dx = \frac{\Gamma(\alpha)\,\Gamma(\beta)}{\Gamma(\alpha + \beta)}. 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≥0f \geq 0 and ∫f=1\int 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)=k x (1−x)2,0<x<1 f(x) = k\,x\,(1 – x)^2, \qquad 0 < x < 1

and 0 outside. We want: (a) the value of kk; (b) E(X)E(X) and Var(X)\text{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 F , mode and median. If you know the order, the exam almost writes itself. And each part reuses the previous one: k k goes into every integral, and the F 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 kk that makes ff a density, and which one.

Step 1: positivity. For 0<x<10 < x < 1, x>0x > 0 and (1−x)2>0(1 – x)^2 > 0. So f(x)≥0f(x) \geq 0 whenever k>0k > 0.

Step 2: expand the polynomial. x(1−x)2=x(1−2x+x2)=x−2x2+x3x(1 – x)^2 = x(1 – 2x + x^2) = x – 2x^2 + x^3.

Step 3: integrate term by term (rule 1):

∫01(x−2x2+x3) dx=[x22−2x33+x44]01=12−23+14=6−8+312=112 \int_0^1 (x – 2x^2 + x^3)\,dx = \left[\frac{x^2}{2} – \frac{2x^3}{3} + \frac{x^4}{4}\right]_0^1 = \frac{1}{2} – \frac{2}{3} + \frac{1}{4} = \frac{6 – 8 + 3}{12} = \frac{1}{12}

Step 4: require the integral to equal 1. k⋅112=1k \cdot \frac{1}{12} = 1, so k=12k = 12 (positive, as step 1 required).

f(x)=12 x (1−x)2,0<x<1 f(x) = 12\,x\,(1 – x)^2, \qquad 0 < x < 1

(b) The mean and the variance

Mean. By definition, E(X)=∫01x f(x) dxE(X) = \int_0^1 x\,f(x)\,dx:

E(X)=12∫01(x2−2x3+x4) dx=12[x33−x42+x55]01=12⋅10−15+630=1230=25 E(X) = 12\int_0^1 (x^2 – 2x^3 + x^4)\,dx = 12\left[\frac{x^3}{3} – \frac{x^4}{2} + \frac{x^5}{5}\right]_0^1 = 12 \cdot \frac{10 – 15 + 6}{30} = \frac{12}{30} = \frac{2}{5}

Second moment. E(X2)=∫01x2f(x) dxE(X^2) = \int_0^1 x^2 f(x)\,dx:

E(X2)=12∫01(x3−2x4+x5) dx=12(14−25+16)=12⋅15−24+1060=1260=15 E(X^2) = 12\int_0^1 (x^3 – 2x^4 + x^5)\,dx = 12\left(\frac{1}{4} – \frac{2}{5} + \frac{1}{6}\right) = 12 \cdot \frac{15 – 24 + 10}{60} = \frac{12}{60} = \frac{1}{5}

Where that 10−15+630 \frac{10 – 15 + 6}{30} comes from: the common denominator of 3, 2 and 5 is 30, so 13=1030 \frac{1}{3} = \frac{10}{30} , 12=1530 \frac{1}{2} = \frac{15}{30} and 15=630 \frac{1}{5} = \frac{6}{30} . In the second moment, with 4, 5 and 6, it is 60.

Variance (rule 7):

Var(X)=15−(25)2=525−425=125=0.04 \text{Var}(X) = \frac{1}{5} – \left(\frac{2}{5}\right)^2 = \frac{5}{25} – \frac{4}{25} = \frac{1}{25} = 0.04

The mean arrival is at 0.4 of an hour (13:24), with a standard deviation of 0.04=0.2\sqrt{0.04} = 0.2 hours: 12 minutes.

Multiplying only the first term of the bracket by xx. x⋅(x−2x2+x3)=x2−2x3+x4x \cdot (x – 2x^2 + x^3) = x^2 – 2x^3 + x^4: every term goes up one degree. If a lone xx 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)F(x) = P(X \leq x) for every real xx.

Step 1: left of the support. If x≤0x \leq 0, there is no density to the left of xx: F(x)=0F(x) = 0.

Step 2: inside the support (rule 6). If 0<x<10 < x < 1:

F(x)=∫0x12 (t−2t2+t3) dt=12(x22−2x33+x44)=6x2−8x3+3x4 F(x) = \int_0^x 12\,(t – 2t^2 + t^3)\,dt = 12\left(\frac{x^2}{2} – \frac{2x^3}{3} + \frac{x^4}{4}\right) = 6x^2 – 8x^3 + 3x^4

Step 3: to the right. If x≥1x \geq 1, all the probability has been accumulated: F(x)=1F(x) = 1.

PieceF(x)F(x)
x≤0x \leq 000
0<x<10 < x < 16x2−8x3+3x46x^2 – 8x^3 + 3x^4
x≥1x \geq 111

Check. F(0)=0F(0) = 0 and F(1)=6−8+3=1F(1) = 6 – 8 + 3 = 1: the middle expression joins the other two, so FF is continuous. And F′(x)=12x−24x2+12x3=12x(1−x)2=f(x)F'(x) = 12x – 24x^2 + 12x^3 = 12x(1 – x)^2 = f(x).

(d) The mode

The mode of a continuous variable is the value where the density is highest. Differentiate and set to 0:

f′(x)=12[(1−x)2−2x(1−x)]=12 (1−x)(1−x−2x)=12 (1−x)(1−3x) f'(x) = 12\left[(1 – x)^2 – 2x(1 – x)\right] = 12\,(1 – x)(1 – x – 2x) = 12\,(1 – x)(1 – 3x)

The step being skipped is the product rule. x(1−x)2 x(1 – x)^2 has two factors: differentiating x x gives 1 and leaves (1−x)2 (1 – x)^2 ; differentiating (1−x)2 (1 – x)^2 gives 2(1−x)⋅(−1) 2(1 – x) \cdot (-1) (chain rule: the derivative of 1−x 1 – x is −1 -1 ) and leaves the x x . That is where the −2x(1−x) -2x(1 – x) comes from. Then you take out the common factor (1−x) (1 – x) , which is in both terms, and inside you are left with 1−x−2x=1−3x 1 – x – 2x = 1 – 3x .

It is zero at x=1x = 1 (the edge, where f=0f = 0) and at x=1/3x = 1/3. Between 0 and 1/3, f′>0f' > 0 (the density rises) and between 1/3 and 1, f′<0f' < 0 (it falls): x=1/3x = 1/3 is a maximum. The busiest time is 13:20.

(e) The median

You have to solve F(x)=0.5F(x) = 0.5, that is, 3x4−8x3+6x2−0.5=03x^4 – 8x^3 + 6x^2 – 0.5 = 0. It is a fourth-degree equation without a nice root, which is why the exam asks for it with R. Since FF is increasing on (0,1)(0, 1), there is a single solution there. uniroot finds it (you have it below): 0.3857, that is, 13:23.

Mode, median and mean of 12x(1 − x)²The density 12 x times 1 minus x squared between 0 and 1, with its peak of 1.78 at one third. Three lines: the mode at 0.333, the median at 0.386, leaving a shaded area of 0.5 to its left, and the mean at 0.4.0.00.51.01.52.00.00.20.40.60.81.0x (fraction of the lunch hour)f(x)mode 0.333median 0.386mean 0.4area 0.5f(x) = 12x(1 − x)²
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)2f(x) = 12x(1 – x)^2 on (0,1)(0, 1) is the beta(2,3)\text{beta}(2, 3): mean 2/52/5, variance 1/251/25, mode 1/31/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=12k = 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)=k x2(1−x) f(x) = k\,x^2(1 – x) on (0,1) (0, 1) , what would k k be? And the mean?

k=12 k = 12 and mean 0.6. Expand the polynomial, x2(1−x)=x2−x3 x^2(1 – x) = x^2 – x^3 , and integrate: 13−14=112 \frac{1}{3} – \frac{1}{4} = \frac{1}{12} , so again k=12 k = 12 . For the mean, everything goes up one degree: 12(14−15)=1220=0.6 12 \left(\frac{1}{4} – \frac{1}{5}\right) = \frac{12}{20} = 0.6 . It is the previous density seen in a mirror (the beta(3,2) \text{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)X \sim \text{Unif}(a, b), with f(x)=1/(b−a)f(x) = 1/(b – a) on (a,b)(a, b):

E(X)=a+b2,Var(X)=(b−a)212,φX(t)=etb−etat (b−a)    (t≠0) E(X) = \frac{a + b}{2}, \qquad \text{Var}(X) = \frac{(b – a)^2}{12}, \qquad \varphi_X(t) = \frac{e^{tb} – e^{ta}}{t\,(b – a)} \;\; (t \neq 0)

and, also, that a+(b−a)Ua + (b – a)U is Unif(a,b)\text{Unif}(a, b) if U∼Unif(0,1)U \sim \text{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.

The mean

E(X)=∫abx⋅1b−a dx=1b−a[x22]ab=b2−a22(b−a)=(b−a)(b+a)2(b−a)=a+b2 E(X) = \int_a^b x \cdot \frac{1}{b – a}\,dx = \frac{1}{b – a}\left[\frac{x^2}{2}\right]_a^b = \frac{b^2 – a^2}{2(b – a)} = \frac{(b – a)(b + a)}{2(b – a)} = \frac{a + b}{2}

The key step is the difference of squares, b2−a2=(b−a)(b+a)b^2 – a^2 = (b – a)(b + a), which lets b−ab – a cancel. The mean is the midpoint, as you would expect from a flat density.

The variance

First, the second moment, with the difference of cubes b3−a3=(b−a)(b2+ab+a2)b^3 – a^3 = (b – a)(b^2 + ab + a^2):

E(X2)=1b−a[x33]ab=b3−a33(b−a)=b2+ab+a23 E(X^2) = \frac{1}{b – a}\left[\frac{x^3}{3}\right]_a^b = \frac{b^3 – a^3}{3(b – a)} = \frac{b^2 + ab + a^2}{3}

Then rule 7 with common denominator 12:

Var(X)=b2+ab+a23−(a+b)24=4b2+4ab+4a2−3a2−6ab−3b212=a2−2ab+b212=(b−a)212 \text{Var}(X) = \frac{b^2 + ab + a^2}{3} – \frac{(a + b)^2}{4} = \frac{4b^2 + 4ab + 4a^2 – 3a^2 – 6ab – 3b^2}{12} = \frac{a^2 – 2ab + b^2}{12} = \frac{(b – a)^2}{12}

That numerator comes from turning both fractions into twelfths. The first is multiplied top and bottom by 4: 4(b2+ab+a2) 4(b^2 + ab + a^2) . The second by 3, expanding the square: 3(a2+2ab+b2) 3(a^2 + 2ab + b^2) . Subtracting, 4b2−3b2=b2 4b^2 – 3b^2 = b^2 , 4ab−6ab=−2ab 4ab – 6ab = -2ab and 4a2−3a2=a2 4a^2 – 3a^2 = a^2 : you are left with (b−a)2 (b – a)^2 .

Writing b3−a32(b−a)\frac{b^3 – a^3}{2(b – a)} in E(X2)E(X^2), dragging the 2 from the mean. Integrating x2x^2 gives x3/3x^3/3: the denominator is 3.

The mgf

For t≠0t \neq 0:

φX(t)=∫abetx 1b−a dx=1b−a[etxt]ab=etb−etat (b−a) \varphi_X(t) = \int_a^b e^{tx}\,\frac{1}{b – a}\,dx = \frac{1}{b – a}\left[\frac{e^{tx}}{t}\right]_a^b = \frac{e^{tb} – e^{ta}}{t\,(b – a)}

and φX(0)=1\varphi_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=0t = 0): that is why, for the uniform, moments are computed from the definition.

How a uniform stretches

If U∼Unif(0,1)U \sim \text{Unif}(0, 1) and X=a+(b−a)UX = a + (b – a)U, for a<x<ba < x < b:

FX(x)=P(a+(b−a)U≤x)=P(U≤x−ab−a)=x−ab−a F_X(x) = P\big(a + (b – a)U \leq x\big) = P\left(U \leq \frac{x – a}{b – a}\right) = \frac{x – a}{b – a}

because x−ab−a\frac{x – a}{b – a} is between 0 and 1 and there FU(u)=uF_U(u) = u. That is the FF of the Unif(a,b)\text{Unif}(a, b). Differentiating, the density is 1/(b−a)1/(b – a).

Stretching and shifting a uniformOn the left, the uniform between 0 and 1, a rectangle of height 1. On the right, X equals 1 plus 4 U: a rectangle between 1 and 5 with height 0.25 and mean 3. Both have area 1.0.000.251.0001udensityarea 1U ~ Unif(0, 1)0.000.251.0001356xarea 1X = 1 + 4U ~ Unif(1, 5)mean 3×4, +1
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)(0, 1) until it measures b−ab – a and shifts it so it starts at aa. Since the area must still be 1, the height drops from 1 to 1/(b−a)1/(b – a). With the old microwave from proof 5, which takes between 1 and 5 minutes, X=1+4UX = 1 + 4U: mean 3 and variance 16/12=1.3316/12 = 1.33.

Result for the cheat sheet: Unif(a,b)\text{Unif}(a, b): f=1b−af = \frac{1}{b – a}, F(x)=x−ab−aF(x) = \frac{x – a}{b – a}, mean a+b2\frac{a + b}{2}, variance (b−a)212\frac{(b – a)^2}{12}, mgf etb−etat(b−a)\frac{e^{tb} – e^{ta}}{t(b – a)}.

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(5 – 1)^2/12. The mgf at t=0.3t = 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)(0, 1) gives the Unif(1,5)\text{Unif}(1, 5).

Check you’ve got it: with the old microwave (a=1 a = 1 , b=5 b = 5 ), what is E(X2) E(X^2) ? Does the variance 1.33 follow from it?

E(X2)=b2+ab+a23=25+5+13=313=10.33 E(X^2) = \frac{b^2 + ab + a^2}{3} = \frac{25 + 5 + 1}{3} = \frac{31}{3} = 10.33 . With rule 7, Var(X)=313−32=31−273=43=1.33 \text{Var}(X) = \frac{31}{3} – 3^2 = \frac{31 – 27}{3} = \frac{4}{3} = 1.33 , the same as (5−1)2/12 (5 – 1)^2/12 . If you carry over the 2 from the mean (the error box), you get E(X2)=124/8=15.5 E(X^2) = 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⁡(λ)T \sim \exp(\lambda), with f(t)=λe−λtf(t) = \lambda e^{-\lambda t} for t>0t > 0:

E(T)=1λ,Var(T)=1λ2,φT(t)=λλ−t    (t<λ), E(T) = \frac{1}{\lambda}, \qquad \text{Var}(T) = \frac{1}{\lambda^2}, \qquad \varphi_T(t) = \frac{\lambda}{\lambda – t} \;\; (t < \lambda),

its mode is at 0 and P(T>s+t∣T>s)=P(T>t)P(T > s + t \mid 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\lambda > 0. For t>0t > 0, integrating (with ddue−λu=−λe−λu\frac{d}{du} e^{-\lambda u} = -\lambda e^{-\lambda u}):

F(t)=∫0tλe−λu du=[−e−λu]0t=1−e−λt F(t) = \int_0^t \lambda e^{-\lambda u}\,du = \left[-e^{-\lambda u}\right]_0^t = 1 – e^{-\lambda t}

As t→∞t \to \infty, F(t)→1F(t) \to 1: it integrates to 1. Along the way we get the survival function, S(t)=1−F(t)=e−λtS(t) = 1 – F(t) = e^{-\lambda t}.

The mean, by parts

E(T)=∫0∞t λe−λt dt E(T) = \int_0^\infty t\,\lambda e^{-\lambda t}\,dt

Step 1: the parts table (rule 2). Differentiate the power and integrate the exponential:

ChooseAnd then
uuttdu=dtdu = dt
dvdvλe−λt dt\lambda e^{-\lambda t}\,dtv=−e−λtv = -e^{-\lambda t}

Step 2: apply the rule.

E(T)=[−t e−λt]0∞+∫0∞e−λt dt E(T) = \Big[-t\,e^{-\lambda t}\Big]_0^\infty + \int_0^\infty e^{-\lambda t}\,dt

Step 3: the bracket term. At ∞\infty, t e−λt→0t\,e^{-\lambda t} \to 0 (rule 3); at 0, it is 0. The bracket is 0−0=00 – 0 = 0.

Step 4: the remaining integral. ∫0∞e−λt dt=[−e−λtλ]0∞=0−(−1λ)=1λ\int_0^\infty e^{-\lambda t}\,dt = \left[-\frac{e^{-\lambda t}}{\lambda}\right]_0^\infty = 0 – \left(-\frac{1}{\lambda}\right) = \frac{1}{\lambda}.

E(T)=1λ E(T) = \frac{1}{\lambda}
The integration by parts tableParts table for the mean of the exponential: u equals t, du equals dt; dv equals lambda e to the minus lambda t dt, v equals minus e to the minus lambda t, with the minus sign circled in red. Differentiating v gives back dv. The result is 0 plus 1 over lambda; without the sign it would be minus 1 over lambda.E(T) = ∫ t · λe^(−λt) dt, by partsu = tdu = dtdv = λe^(−λt) dtv = −e^(−λt)differentiateintegrateCheck: differentiating v gives +λe^(−λt) = dv[−t e^(−λt)] from 0 to ∞ + ∫ e^(−λt) dt = 0 + 1/λWithout the minus sign in v, everything flips sign: 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 vv carries a minus sign: the derivative of −e−λt-e^{-\lambda t} is +λe−λt+\lambda e^{-\lambda t}, which is the dvdv. If you forget it, step 2 ends up with the opposite sign and the mean comes out as −1/λ-1/\lambda. A negative wait is the alarm bell.

Checking vv means differentiating it: if v′≠v' \neq what was in dvdv, something is wrong. It takes five seconds and saves the proof.

The second moment and the variance

E(T2)=∫0∞t2 λe−λt dt E(T^2) = \int_0^\infty t^2\,\lambda e^{-\lambda t}\,dt

With u=t2u = t^2, du=2t dtdu = 2t\,dt and the same dvdv and vv:

E(T2)=[−t2e−λt]0∞+∫0∞2t e−λt dt=0+2λ∫0∞t λe−λt dt=2λ⋅E(T)=2λ2 E(T^2) = \Big[-t^2 e^{-\lambda t}\Big]_0^\infty + \int_0^\infty 2t\,e^{-\lambda t}\,dt = 0 + \frac{2}{\lambda}\int_0^\infty t\,\lambda e^{-\lambda t}\,dt = \frac{2}{\lambda} \cdot E(T) = \frac{2}{\lambda^2}

The trick in the third step is to multiply and divide by λ\lambda so that ∫t λe−λt\int t\,\lambda e^{-\lambda t} appears, which is the mean we already have: no need to integrate by parts again. By rule 7:

Var(T)=2λ2−1λ2=1λ2 \text{Var}(T) = \frac{2}{\lambda^2} – \frac{1}{\lambda^2} = \frac{1}{\lambda^2}

The standard deviation of the exponential equals its mean. With λ=0.5\lambda = 0.5, both are 2 minutes.

The mgf, and the moments again

φT(t)=∫0∞etxλe−λx dx=λ∫0∞e−(λ−t)x dx=λ[−e−(λ−t)xλ−t]0∞=λλ−t \varphi_T(t) = \int_0^\infty e^{tx}\lambda e^{-\lambda x}\,dx = \lambda\int_0^\infty e^{-(\lambda – t)x}\,dx = \lambda\left[-\frac{e^{-(\lambda – t)x}}{\lambda – t}\right]_0^\infty = \frac{\lambda}{\lambda – t}

The last step only works if λ−t>0\lambda – t > 0: if t≥λt \geq \lambda, the exponential e−(λ−t)xe^{-(\lambda – t)x} does not go to 0 and the integral is infinite. That is why the mgf exists only for t<λt < \lambda. Differentiating:

φT′(t)=λ(λ−t)2⇒φT′(0)=1λφT′′(t)=2λ(λ−t)3⇒φT′′(0)=2λ2 \varphi_T'(t) = \frac{\lambda}{(\lambda – t)^2} \Rightarrow \varphi_T'(0) = \frac{1}{\lambda} \qquad \varphi_T''(t) = \frac{2\lambda}{(\lambda – t)^3} \Rightarrow \varphi_T''(0) = \frac{2}{\lambda^2}

The steps squeezed into the mgf. First the two exponentials are combined: etxe−λx=e(t−λ)x=e−(λ−t)x e^{tx} e^{-\lambda x} = e^{(t – \lambda)x} = e^{-(\lambda – t)x} . When you evaluate the bracket, at ∞ \infty it is 0 (if λ−t>0 \lambda – t > 0 ) and at 0 it is −1λ−t -\frac{1}{\lambda – t} ; subtracting something negative means adding it, and you get λλ−t \frac{\lambda}{\lambda – t} . To differentiate, write λ(λ−t)−1 \lambda(\lambda – t)^{-1} : the exponent comes down and becomes −2 -2 , and the derivative of what is inside, λ−t \lambda – t , is −1 -1 . Minus times minus, plus.

The same E(T)E(T) and E(T2)E(T^2) as by parts, without integrating.

The mode is at 0

f′(t)=−λ2e−λt<0f'(t) = -\lambda^2 e^{-\lambda t} < 0 for every t>0t > 0: the density is strictly decreasing. Its maximum is at the left edge, t=0t = 0, and is f(0)=λf(0) = \lambda. 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>0s, t > 0, P(T>s+t∣T>s)=P(T>t)P(T > s + t \mid T > s) = P(T > t).

Step 1: the definition of conditional probability (topic 2):

P(T>s+t∣T>s)=P(T>s+t ∩ T>s)P(T>s) P(T > s + t \mid T > s) = \frac{P(T > s + t \,\cap\, T > s)}{P(T > s)}

Step 2: the intersection. If T>s+tT > s + t, in particular T>sT > s: the event {T>s+t}\{T > s + t\} is inside {T>s}\{T > s\} and the intersection is {T>s+t}\{T > s + t\}.

Step 3: the survival function.

P(T>s+t)P(T>s)=e−λ(s+t)e−λs=e−λt=P(T>t) \frac{P(T > s + t)}{P(T > s)} = \frac{e^{-\lambda(s + t)}}{e^{-\lambda s}} = e^{-\lambda t} = P(T > t)

Having waited ss minutes does not change what is left.

Result for the cheat sheet: exp⁡(λ)\exp(\lambda): F(t)=1−e−λtF(t) = 1 – e^{-\lambda t}, S(t)=e−λtS(t) = e^{-\lambda t}, mean 1/λ1/\lambda, variance 1/λ21/\lambda^2, mgf λ/(λ−t)\lambda/(\lambda – t) for t<λt < \lambda, 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\lambda = 0.5: mean 2, E(T2)=8=2/0.25E(T^2) = 8 = 2/0.25, variance 4 and the mgf at t=0.2t = 0.2, 1.6667, the same as 0.5/0.30.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−60e^{-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 \lambda = 0.5 , what is E(T3) E(T^3) ? Do it without integrating by parts again.

It is 48. With u=t3 u = t^3 , the bracket vanishes (rule 3) and you are left with 3λ∫0∞t2λe−λt dt=3λE(T2) \frac{3}{\lambda} \int_0^\infty t^2 \lambda e^{-\lambda t}\,dt = \frac{3}{\lambda} E(T^2) . Since E(T2)=2λ2 E(T^2) = \frac{2}{\lambda^2} , E(T3)=3⋅2λ3=60.125=48 E(T^3) = \frac{3 \cdot 2}{\lambda^3} = \frac{6}{0.125} = 48 . The mgf gives the same: φT′′′(t)=6λ(λ−t)4 \varphi_T'''(t) = \frac{6\lambda}{(\lambda – t)^4} and at t=0 t = 0 , 6λ3 \frac{6}{\lambda^3} .

Proof 4: the gamma (the Γ function, mean, variance, mgf and mode)

What we want to prove. First, two properties of Γ\Gamma: Γ(α+1)=α Γ(α)\Gamma(\alpha + 1) = \alpha\,\Gamma(\alpha) and Γ(n)=(n−1)!\Gamma(n) = (n – 1)!. Then, that if X∼gamma(α,β)X \sim \text{gamma}(\alpha, \beta):

E(X)=αβ,Var(X)=αβ2,φX(t)=(ββ−t)α    (t<β),mode=α−1β    (α≥1) E(X) = \frac{\alpha}{\beta}, \qquad \text{Var}(X) = \frac{\alpha}{\beta^2}, \qquad \varphi_X(t) = \left(\frac{\beta}{\beta – t}\right)^\alpha \;\; (t < \beta), \qquad \text{mode} = \frac{\alpha – 1}{\beta} \;\; (\alpha \geq 1)

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−bx dx=Γ(a)/ba \int_0^\infty x^{a – 1} e^{-bx}\,dx = \Gamma(a)/b^a : with it, the mean, the variance and the mgf come out without integrating by parts even once.

The Γ function

Γ(1)=1\Gamma(1) = 1. Γ(1)=∫0∞e−t dt=[−e−t]0∞=1\Gamma(1) = \int_0^\infty e^{-t}\,dt = \big[-e^{-t}\big]_0^\infty = 1.

Γ(α+1)=α Γ(α)\Gamma(\alpha + 1) = \alpha\,\Gamma(\alpha), by parts with u=tαu = t^\alpha, du=αtα−1 dtdu = \alpha t^{\alpha – 1}\,dt, dv=e−t dtdv = e^{-t}\,dt, v=−e−tv = -e^{-t}:

Γ(α+1)=∫0∞tαe−t dt=[−tαe−t]0∞+α∫0∞tα−1e−t dt=0+α Γ(α) \Gamma(\alpha + 1) = \int_0^\infty t^\alpha e^{-t}\,dt = \Big[-t^\alpha e^{-t}\Big]_0^\infty + \alpha\int_0^\infty t^{\alpha – 1} e^{-t}\,dt = 0 + \alpha\,\Gamma(\alpha)

(the bracket vanishes by rule 3 at ∞\infty and because tα=0t^\alpha = 0 at 0).

Why there is a + + in front of the integral: the rule is u v−∫v du u\,v – \int v\,du , and here v du=−e−t⋅αtα−1 dt v\,du = -e^{-t} \cdot \alpha t^{\alpha – 1}\,dt . That minus cancels the minus in the rule. And at 0 the bracket is 0 because α>0 \alpha > 0 : 0α=0 0^\alpha = 0 .

Γ(n)=(n−1)!\Gamma(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)!\Gamma(n) = (n – 1)\Gamma(n – 1) = (n – 1)(n – 2)\Gamma(n – 2) = \dots = (n – 1)(n – 2)\cdots 1 \cdot \Gamma(1) = (n – 1)!.

The gamma function goes through the factorialsCurve of gamma of x for x between 0.15 and 4.3, high near 0, with a minimum near 1.46 and growing fast. Coral dots at 1, 2, 3 and 4 with values 1, 1, 2 and 6, the factorials from 0 to 3. Green dot at one half with value root pi, 1.77.0246800.51234xΓ(x)Γ(1) = 0! = 1Γ(2) = 1! = 1Γ(3) = 2! = 2Γ(4) = 3! = 6Γ(1/2) = √π ≈ 1.77
Γ(n) = (n − 1)! at the integers, and a smooth curve between them.

In the drawing, the curve of Γ(x)\Gamma(x) goes exactly through the factorials: Γ(1)=0!=1\Gamma(1) = 0! = 1, Γ(2)=1!=1\Gamma(2) = 1! = 1, Γ(3)=2!=2\Gamma(3) = 2! = 2, Γ(4)=3!=6\Gamma(4) = 3! = 6. In between it fills the gap smoothly: for example, Γ(1/2)=π≈1.77\Gamma(1/2) = \sqrt{\pi} \approx 1.77, which will appear with the normal.

The integral that solves everything

What we want to prove. For a>0a > 0 and b>0b > 0:

∫0∞xa−1e−bx dx=Γ(a)ba \int_0^\infty x^{a – 1} e^{-bx}\,dx = \frac{\Gamma(a)}{b^a}

Step 1: change of variable u=bxu = bx, that is, x=u/bx = u/b and dx=du/bdx = du/b. The limits do not change (0 and ∞\infty).

Step 2: substitute.

∫0∞(ub)a−1e−u dub=1ba−1⋅b∫0∞ua−1e−u du=Γ(a)ba \int_0^\infty \left(\frac{u}{b}\right)^{a – 1} e^{-u}\,\frac{du}{b} = \frac{1}{b^{a – 1} \cdot b}\int_0^\infty u^{a – 1} e^{-u}\,du = \frac{\Gamma(a)}{b^a}

The bookkeeping of the powers of b b : (ub)a−1=ua−1ba−1 \left(\frac{u}{b}\right)^{a – 1} = \frac{u^{a – 1}}{b^{a – 1}} , and dx=du/b dx = du/b puts another b b in the denominator. Together, ba−1⋅b=ba b^{a – 1} \cdot b = b^a . What is left inside is the definition of Γ(a) \Gamma(a) (rule 4). The limits do not change because b>0 b > 0 : if x=0 x = 0 , u=0 u = 0 ; if x→∞ x \to \infty , u→∞ u \to \infty .

With a=αa = \alpha and b=βb = \beta we already have that the gamma integrates to 1: βαΓ(α)⋅Γ(α)βα=1\frac{\beta^\alpha}{\Gamma(\alpha)} \cdot \frac{\Gamma(\alpha)}{\beta^\alpha} = 1. This integral is rule 5 applied to the gamma: every time you see xsomethinge−something⋅xx^{\text{something}} e^{-\text{something} \cdot x} between 0 and ∞\infty, do not integrate by parts; write down the result.

The mean and the variance without brute force

E(X)=βαΓ(α)∫0∞x⋅xα−1e−βx dx=βαΓ(α)∫0∞x(α+1)−1e−βx dx=βαΓ(α)⋅Γ(α+1)βα+1 E(X) = \frac{\beta^\alpha}{\Gamma(\alpha)}\int_0^\infty x \cdot x^{\alpha – 1} e^{-\beta x}\,dx = \frac{\beta^\alpha}{\Gamma(\alpha)}\int_0^\infty x^{(\alpha + 1) – 1} e^{-\beta x}\,dx = \frac{\beta^\alpha}{\Gamma(\alpha)} \cdot \frac{\Gamma(\alpha + 1)}{\beta^{\alpha + 1}}

and since Γ(α+1)=αΓ(α)\Gamma(\alpha + 1) = \alpha\Gamma(\alpha):

E(X)=α Γ(α)Γ(α)⋅βαβα+1=αβ E(X) = \frac{\alpha\,\Gamma(\alpha)}{\Gamma(\alpha)} \cdot \frac{\beta^\alpha}{\beta^{\alpha + 1}} = \frac{\alpha}{\beta}

Same with x2x^2, which raises the exponent to α+2\alpha + 2:

E(X2)=βαΓ(α)⋅Γ(α+2)βα+2=(α+1)αβ2Var(X)=α2+αβ2−α2β2=αβ2 E(X^2) = \frac{\beta^\alpha}{\Gamma(\alpha)} \cdot \frac{\Gamma(\alpha + 2)}{\beta^{\alpha + 2}} = \frac{(\alpha + 1)\alpha}{\beta^2} \qquad \text{Var}(X) = \frac{\alpha^2 + \alpha}{\beta^2} – \frac{\alpha^2}{\beta^2} = \frac{\alpha}{\beta^2}

(using Γ(α+2)=(α+1)Γ(α+1)=(α+1)α Γ(α)\Gamma(\alpha + 2) = (\alpha + 1)\Gamma(\alpha + 1) = (\alpha + 1)\alpha\,\Gamma(\alpha)).

The mgf

φX(t)=βαΓ(α)∫0∞xα−1e−(β−t)x dx=βαΓ(α)⋅Γ(α)(β−t)α=(ββ−t)α,t<β \varphi_X(t) = \frac{\beta^\alpha}{\Gamma(\alpha)}\int_0^\infty x^{\alpha – 1} e^{-(\beta – t)x}\,dx = \frac{\beta^\alpha}{\Gamma(\alpha)} \cdot \frac{\Gamma(\alpha)}{(\beta – t)^\alpha} = \left(\frac{\beta}{\beta – t}\right)^\alpha, \qquad t < \beta

The same integral with b=β−tb = \beta – t, which must be positive: hence the condition t<βt < \beta. With α=1\alpha = 1 it is the mgf of the exponential, and raised to α\alpha it is the product of α\alpha equal exponential mgfs: that is why a sum of α\alpha independent exponentials is gamma (in topic 5 it is proved with the mgf of a sum).

The mode, with the logarithm

For α>1\alpha > 1, maximising ff is the same as maximising log⁡f\log f (the logarithm is increasing), and differentiating the logarithm is much easier:

log⁡f(x)=αlog⁡β−log⁡Γ(α)+(α−1)log⁡x−βx \log f(x) = \alpha\log\beta – \log\Gamma(\alpha) + (\alpha – 1)\log x – \beta x
ddxlog⁡f(x)=α−1x−β=0⇒x=α−1β \frac{d}{dx}\log f(x) = \frac{\alpha – 1}{x} – \beta = 0 \quad \Rightarrow \quad x = \frac{\alpha – 1}{\beta}

The derivative goes from positive to negative at that point, so it is a maximum. With α=1\alpha = 1 the mode is 0 (the exponential). For the wait with three people ahead, gamma(3,0.5)\text{gamma}(3, 0.5): mode 2/0.5=42/0.5 = 4 minutes.

Result for the cheat sheet: Γ(n)=(n−1)!\Gamma(n) = (n – 1)!, ∫0∞xa−1e−bx dx=Γ(a)/ba\int_0^\infty x^{a – 1} e^{-bx}\,dx = \Gamma(a)/b^a. gamma(α,β)\text{gamma}(\alpha, \beta): mean α/β\alpha/\beta, variance α/β2\alpha/\beta^2, mgf (β/(β−t))α(\beta/(\beta – t))^\alpha for t<βt < \beta, mode (α−1)/β(\alpha – 1)/\beta.

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!\Gamma(5) = 24 = 4! and Γ(1/2)2=π\Gamma(1/2)^2 = \pi. For the gamma(3,0.5)\text{gamma}(3, 0.5), the numerical integrals give mean 6, variance 12, the mgf at t=0.2t = 0.2 equal to (0.5/0.3)3=4.6296(0.5/0.3)^3 = 4.6296 and the mode at 4.

Check you’ve got it: what is ∫0∞x3e−2x dx \int_0^\infty x^3 e^{-2x}\,dx ? Without integrating by parts.

3/8=0.375 3/8 = 0.375 . Compare it with xa−1e−bx x^{a – 1} e^{-bx} : the exponent of x x is a−1=3 a – 1 = 3 , so a=4 a = 4 (not 3, the usual mistake), and b=2 b = 2 . So the integral is Γ(4)24=3!16=616=38 \frac{\Gamma(4)}{2^4} = \frac{3!}{16} = \frac{6}{16} = \frac{3}{8} . 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:

MicrowaveUsed byTime each person takes, TT
The new one (N)50%Exponential with mean 2 min: exp⁡(0.5)\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)\text{gamma}(3, 1)

(For the back one, β=μ/σ2=3/3=1\beta = \mu/\sigma^2 = 3/3 = 1 and α=μ2/σ2=9/3=3\alpha = \mu^2/\sigma^2 = 9/3 = 3.)

Why it matters. This is where topic 2 and this one meet: total probability and Bayes, but with an F 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,…,fmf_1, \dots, f_m are densities and p1,…,pm≥0p_1, \dots, p_m \geq 0 add up to 1, then f=∑pkfkf = \sum p_k f_k is a density.

Positivity. Each pkfk(x)≥0p_k f_k(x) \geq 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\int \sum p_k f_k = \sum p_k \int f_k = \sum p_k \cdot 1 = 1.

The condition pk≥0p_k \geq 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.5 FN(t)+0.3 FO(t)+0.2 FB(t)P(T < t) = 0.5\,F_N(t) + 0.3\,F_O(t) + 0.2\,F_B(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) P(T < t) = P(T < t \mid N)\,P(N) + P(T < t \mid O)\,P(O) + P(T < t \mid B)\,P(B)

Step 3: each conditional is the FF of its model. P(T<t∣N)=FN(t)=1−e−0.5tP(T < t \mid N) = F_N(t) = 1 – e^{-0.5t}; P(T<t∣O)=FO(t)=(t−1)/4P(T < t \mid O) = F_O(t) = (t – 1)/4 between 1 and 5 (0 before 1); P(T<t∣B)=FB(t)P(T < t \mid B) = F_B(t), that of the gamma(3,1)\text{gamma}(3, 1), computed with pgamma.

P(T<t)=0.5 (1−e−0.5t)+0.3 FO(t)+0.2 FB(t) P(T < t) = 0.5\,(1 – e^{-0.5t}) + 0.3\,F_O(t) + 0.2\,F_B(t)

For t=2t = 2: 0.5⋅0.6321+0.3⋅0.25+0.2⋅0.3233=0.45570.5 \cdot 0.6321 + 0.3 \cdot 0.25 + 0.2 \cdot 0.3233 = 0.4557. 46% of people finish in under 2 minutes.

Where each number comes from. FN(2)=1−e−1=0.6321 F_N(2) = 1 – e^{-1} = 0.6321 . FV(2)=(2−1)/4=0.25 F_V(2) = (2 – 1)/4 = 0.25 : one minute out of the four possible. For FD(2) F_D(2) , if you do not have R at hand, integrating the gamma(3,1) \text{gamma}(3, 1) by parts twice gives FD(t)=1−e−t(1+t+t22) F_D(t) = 1 – e^{-t}\left(1 + t + \frac{t^2}{2}\right) , and at 2, 1−5e−2=0.3233 1 – 5e^{-2} = 0.3233 . The three products, 0.3161 0.3161 , 0.075 0.075 and 0.0647 0.0647 , are the Bayes numerators below.

Total probability with timesTree with three branches: new microwave with weight 0.5 and exponential with rate 0.5, F of 2 equal to 0.6321, product 0.3161; old one with weight 0.3 and uniform between 1 and 5, F of 2 equal to 0.25, product 0.075; back one with weight 0.2 and gamma 3 1, F of 2 equal to 0.3233, product 0.0647. The sum is 0.4557.Person0.5Newexp(0.5)F(2) = 0.63210.5 · 0.6321 = 0.31610.3OldUnif(1, 5)F(2) = 0.25000.3 · 0.2500 = 0.07500.2Back onegamma(3, 1)F(2) = 0.32330.2 · 0.3233 = 0.0647Sum: P(T < 2) = 0.4557
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 FF 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:

P(N∣T<2)=P(T<2∣N) P(N)P(T<2)=0.5⋅0.63210.4557=0.6935 P(N \mid T < 2) = \frac{P(T < 2 \mid N)\,P(N)}{P(T < 2)} = \frac{0.5 \cdot 0.6321}{0.4557} = 0.6935

and, in the same way, P(O∣T<2)=0.075/0.4557=0.1646P(O \mid T < 2) = 0.075/0.4557 = 0.1646 and P(B∣T<2)=0.0647/0.4557=0.1419P(B \mid 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.

The mean of the mixture

Since the density of TT is ∑pkfk\sum p_k f_k, by rule 1:

E(T)=∫t∑kpkfk(t) dt=∑kpk∫t fk(t) dt=∑kpk Ek(T)=0.5⋅2+0.3⋅3+0.2⋅3=2.5 E(T) = \int t \sum_k p_k f_k(t)\,dt = \sum_k p_k \int t\,f_k(t)\,dt = \sum_k p_k\,E_k(T) = 0.5 \cdot 2 + 0.3 \cdot 3 + 0.2 \cdot 3 = 2.5

Doing the same with the variance. The variance of the mixture is not ∑pkVark\sum p_k \text{Var}_k: you have to compute E(T2)=∑pkEk(T2)E(T^2) = \sum p_k E_k(T^2) and subtract [E(T)]2[E(T)]^2. Besides the variation within each microwave, the mixture has the variation between microwaves.

Result for the cheat sheet: mixture f=∑pkfkf = \sum p_k f_k; F(t)=∑pkFk(t)F(t) = \sum p_k F_k(t); P(k∣T<t)=pkFk(t)/F(t)P(k \mid T < t) = p_k F_k(t)/F(t); E(T)=∑pkEk(T)E(T) = \sum p_k E_k(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 FF 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 FF 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 0.5 \cdot 4 + 0.3 \cdot 1.33 + 0.2 \cdot 3 = 3 ?

3.25. As the error box says, you go through E(T2) E(T^2) , which is the weighted mean of the second moments. Each one is its variance plus its mean squared: 4+22=8 4 + 2^2 = 8 for the new one, 1.33+32=10.33 1.33 + 3^2 = 10.33 for the old one and 3+32=12 3 + 3^2 = 12 for the back one. Weighted, 0.5⋅8+0.3⋅10.33+0.2⋅12=9.5 0.5 \cdot 8 + 0.3 \cdot 10.33 + 0.2 \cdot 12 = 9.5 , and Var(T)=9.5−2.52=3.25 \text{Var}(T) = 9.5 – 2.5^2 = 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(α,β)X \sim \text{beta}(\alpha, \beta), with f(x)=xα−1(1−x)β−1B(α,β)f(x) = \frac{x^{\alpha – 1}(1 – x)^{\beta – 1}}{B(\alpha, \beta)} on (0,1)(0, 1):

E(X)=αα+β,Var(X)=αβ(α+β)2(α+β+1),mode=α−1α+β−2    (α,β>1) E(X) = \frac{\alpha}{\alpha + \beta}, \qquad \text{Var}(X) = \frac{\alpha\beta}{(\alpha + \beta)^2(\alpha + \beta + 1)}, \qquad \text{mode} = \frac{\alpha – 1}{\alpha + \beta – 2} \;\; (\alpha, \beta > 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)(0, 1) and, by the definition of BB (rule 8), ∫01xα−1(1−x)β−1 dx=B(α,β)\int_0^1 x^{\alpha – 1}(1 – x)^{\beta – 1}\,dx = B(\alpha, \beta): divided by B(α,β)B(\alpha, \beta), it integrates to 1. With α=2\alpha = 2 and β=3\beta = 3, B(2,3)=Γ(2)Γ(3)Γ(5)=1⋅224=112B(2, 3) = \frac{\Gamma(2)\Gamma(3)}{\Gamma(5)} = \frac{1 \cdot 2}{24} = \frac{1}{12}: that is why proof 1 gave k=12k = 12.

The mean

Multiplying by xx raises the exponent of xx by 1, and the integral is a BB again (rule 5 with the beta):

E(X)=1B(α,β)∫01x(α+1)−1(1−x)β−1 dx=B(α+1,β)B(α,β) E(X) = \frac{1}{B(\alpha, \beta)}\int_0^1 x^{(\alpha + 1) – 1}(1 – x)^{\beta – 1}\,dx = \frac{B(\alpha + 1, \beta)}{B(\alpha, \beta)}

We write both BB with gammas (rule 8) and use Γ(z+1)=zΓ(z)\Gamma(z + 1) = z\Gamma(z) top and bottom:

B(α+1,β)B(α,β)=Γ(α+1)Γ(β)Γ(α+β+1)⋅Γ(α+β)Γ(α)Γ(β)=α Γ(α)Γ(α)⋅Γ(α+β)(α+β)Γ(α+β)=αα+β \frac{B(\alpha + 1, \beta)}{B(\alpha, \beta)} = \frac{\Gamma(\alpha + 1)\Gamma(\beta)}{\Gamma(\alpha + \beta + 1)} \cdot \frac{\Gamma(\alpha + \beta)}{\Gamma(\alpha)\Gamma(\beta)} = \frac{\alpha\,\Gamma(\alpha)}{\Gamma(\alpha)} \cdot \frac{\Gamma(\alpha + \beta)}{(\alpha + \beta)\Gamma(\alpha + \beta)} = \frac{\alpha}{\alpha + \beta}

The variance

With x2x^2, the exponent goes up by 2 and two factors appear:

E(X2)=B(α+2,β)B(α,β)=(α+1)α(α+β+1)(α+β) E(X^2) = \frac{B(\alpha + 2, \beta)}{B(\alpha, \beta)} = \frac{(\alpha + 1)\alpha}{(\alpha + \beta + 1)(\alpha + \beta)}

What is skipped here is the same as in the mean, twice: Γ(α+2)=(α+1)α Γ(α) \Gamma(\alpha + 2) = (\alpha + 1)\alpha\,\Gamma(\alpha) on top and Γ(α+β+2)=(α+β+1)(α+β) Γ(α+β) \Gamma(\alpha + \beta + 2) = (\alpha + \beta + 1)(\alpha + \beta)\,\Gamma(\alpha + \beta) below. The Γ(α) \Gamma(\alpha) , Γ(β) \Gamma(\beta) and Γ(α+β) \Gamma(\alpha + \beta) cancel, and the loose factors remain.

Var(X)=α(α+1)(α+β)(α+β+1)−α2(α+β)2=α(α+1)(α+β)−α2(α+β+1)(α+β)2(α+β+1) \text{Var}(X) = \frac{\alpha(\alpha + 1)}{(\alpha + \beta)(\alpha + \beta + 1)} – \frac{\alpha^2}{(\alpha + \beta)^2} = \frac{\alpha(\alpha + 1)(\alpha + \beta) – \alpha^2(\alpha + \beta + 1)}{(\alpha + \beta)^2(\alpha + \beta + 1)}

The numerator, expanded: α3+α2β+α2+αβ−α3−α2β−α2=αβ\alpha^3 + \alpha^2\beta + \alpha^2 + \alpha\beta – \alpha^3 – \alpha^2\beta – \alpha^2 = \alpha\beta. So

Var(X)=αβ(α+β)2(α+β+1) \text{Var}(X) = \frac{\alpha\beta}{(\alpha + \beta)^2(\alpha + \beta + 1)}

With α=2\alpha = 2, β=3\beta = 3: 6/(25⋅6)=1/256/(25 \cdot 6) = 1/25, the one from proof 1.

The mode

With α,β>1\alpha, \beta > 1, using the logarithm:

ddxlog⁡f(x)=α−1x−β−11−x=0  ⇒  (α−1)(1−x)=(β−1)x  ⇒  x=α−1α+β−2 \frac{d}{dx}\log f(x) = \frac{\alpha – 1}{x} – \frac{\beta – 1}{1 – x} = 0 \;\Rightarrow\; (\alpha – 1)(1 – x) = (\beta – 1)x \;\Rightarrow\; x = \frac{\alpha – 1}{\alpha + \beta – 2}

The sign of the second term comes from the chain rule: the derivative of log⁡(1−x) \log(1 – x) is 11−x⋅(−1) \frac{1}{1 – x} \cdot (-1) . Then you move it to the other side and cross-multiply; solving, α−1=x(α−1)+x(β−1)=x(α+β−2) \alpha – 1 = x(\alpha – 1) + x(\beta – 1) = x(\alpha + \beta – 2) .

With α=2\alpha = 2, β=3\beta = 3: 1/31/3, 13:20. If α\alpha or β\beta is 1 or less, the density has no maximum inside (0,1)(0, 1) and the mode goes to an endpoint (or to the whole interval, for the uniform).

Result for the cheat sheet: B(α,β)=Γ(α)Γ(β)/Γ(α+β)B(\alpha, \beta) = \Gamma(\alpha)\Gamma(\beta)/\Gamma(\alpha + \beta); beta(α,β)\text{beta}(\alpha, \beta): mean α/(α+β)\alpha/(\alpha + \beta), variance αβ/((α+β)2(α+β+1))\alpha\beta/((\alpha + \beta)^2(\alpha + \beta + 1)), mode (α−1)/(α+β−2)(\alpha – 1)/(\alpha + \beta – 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)

Output:

[1] 12 12
        mean formula_mean     variance  formula_var         mode 
      0.4000       0.4000       0.0400       0.0400       0.3333 

beta(2, 3) is R’s BB function (not the distribution): 1/B(2,3)=121/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) \text{beta}(3, 2) from the first question, what would the mean, the variance and the mode be?

Mean 3/5=0.6 3/5 = 0.6 , variance 0.04 0.04 and mode 2/3 2/3 . The mean: 33+2 \frac{3}{3 + 2} . The variance: 3⋅252⋅6=6150=125 \frac{3 \cdot 2}{5^2 \cdot 6} = \frac{6}{150} = \frac{1}{25} , the same as the beta(2,3) \text{beta}(2, 3) , because the formula does not change if you swap α \alpha and β \beta : a density and its mirror image are equally spread out. The mode: 3−13+2−2=23 \frac{3 – 1}{3 + 2 – 2} = \frac{2}{3} , 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βF(x) = 1 – e^{-\alpha x^\beta} for x≥0x \geq 0 and F(x)=0F(x) = 0 for x<0x < 0, with α,β>0\alpha, \beta > 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=01 – e^0 = 0, the same as from the left). On the left it is 0 and, as x→∞x \to \infty, αxβ→∞\alpha x^\beta \to \infty and e−αxβ→0e^{-\alpha x^\beta} \to 0, so F→1F \to 1. It is increasing because its derivative, the density below, is positive for x>0x > 0.

Density. Differentiating (chain rule): f(x)=F′(x)=αβxβ−1e−αxβf(x) = F'(x) = \alpha\beta x^{\beta – 1} e^{-\alpha x^\beta} for x>0x > 0. With β=1\beta = 1 it is the exponential with rate α\alpha.

Median. 1−e−αxβ=1/2  ⟺  e−αxβ=1/2  ⟺  αxβ=log⁡2  ⟺  x=(log⁡2/α)1/β1 – e^{-\alpha x^\beta} = 1/2 \iff e^{-\alpha x^\beta} = 1/2 \iff \alpha x^\beta = \log 2 \iff x = (\log 2/\alpha)^{1/\beta}.

2. Let X∼gamma(α,3)X \sim \text{gamma}(\alpha, 3). Using only the mgf of XX, find E(eX)E(e^X).

Show the solution to exercise 2

The mgf is φX(t)=E(etX)=(3/(3−t))α\varphi_X(t) = E(e^{tX}) = (3/(3 – t))^\alpha for t<3t < 3. What is asked, E(eX)E(e^X), is the mgf evaluated at t=1t = 1, which satisfies 1<31 < 3:

E(eX)=φX(1)=(33−1)α=(32)α E(e^X) = \varphi_X(1) = \left(\frac{3}{3 – 1}\right)^\alpha = \left(\frac{3}{2}\right)^\alpha

There is nothing to integrate: the trick is to recognise eXe^{X} as etXe^{tX} with t=1t = 1 and to check that this tt is where the mgf exists.

3. Let T∼exp⁡(λ)T \sim \exp(\lambda) and let XX be the integer part of TT: X=kX = k if k≤T<k+1k \leq T < k + 1, with k=0,1,2,…k = 0, 1, 2, \dots Find the mass function of XX and recognise its distribution.

Show the solution to exercise 3

For each kk, with the FF of the exponential:

P(X=k)=P(k≤T<k+1)=F(k+1)−F(k)=e−λk−e−λ(k+1)=(e−λ)k(1−e−λ) P(X = k) = P(k \leq T < k + 1) = F(k + 1) – F(k) = e^{-\lambda k} – e^{-\lambda(k + 1)} = \left(e^{-\lambda}\right)^k\left(1 – e^{-\lambda}\right)

Calling p=1−e−λp = 1 – e^{-\lambda}, it is (1−p)k p(1 – p)^k\,p for k=0,1,2,…k = 0, 1, 2, \dots: a geometric with parameter pp (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\lambda = 0.5, p=1−e−0.5=0.39p = 1 – e^{-0.5} = 0.39.

When to use each result

If you are asked for…UseWatch out for…
The constant of a densityf≥0f \geq 0 and ∫f=1\int f = 1Writing the positivity too
FF from ff∫startxf\int_{\text{start}}^x f, piece by pieceThe 0 and 1 pieces outside the support
The median or a quantile that cannot be solved foruniroot(function(x) F(x) - p, interval)Subtracting pp and an interval with a sign change
The modeDifferentiate ff (or log⁡f\log f) and set to 0The edges: the exponential has its mode at 0
Mean or variance of an exponentialParts, or the mgfThe sign of v=−e−λtv = -e^{-\lambda t}
Mean or variance of a gamma or a betaRecognise the integral (rules 5 and 8)Γ(α+1)=αΓ(α)\Gamma(\alpha + 1) = \alpha\Gamma(\alpha)
E(ecX)E(e^{cX})The mgf at t=ct = cThat cc is where the mgf exists
A probability with several “sources”Total probability with the FkF_kThat the weights add up to 1
Which source it came from, given the resultBayesDividing 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 vv in parts. ∫e−λt dt=−e−λt/λ\int e^{-\lambda t}\,dt = -e^{-\lambda t}/\lambda. A negative mean for a time gives the mistake away.
  • Leaving the brackets unevaluated. [−te−λt]0∞=0[-t e^{-\lambda t}]_0^\infty = 0 must be justified (rule 3), not taken for granted.
  • Integrating a gamma by parts. It works, but with α=5\alpha = 5 it is four integrations. Rule 5 does it in one line.
  • The mgf without its domain. λ/(λ−t)\lambda/(\lambda – t) and (β/(β−t))α(\beta/(\beta – t))^\alpha are only valid for tt 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)\text{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

ResultFormulaHow it is proved
Constant of a density∫f=1\int f = 1, f≥0f \geq 0Integrate the polynomial
Uniformmean a+b2\frac{a + b}{2}, variance (b−a)212\frac{(b – a)^2}{12}Difference of squares and of cubes
Exponentialmean 1λ\frac{1}{\lambda}, variance 1λ2\frac{1}{\lambda^2}, mgf λλ−t\frac{\lambda}{\lambda – t}By parts; memoryless with the conditional
Gamma functionΓ(α+1)=αΓ(α)\Gamma(\alpha + 1) = \alpha\Gamma(\alpha), Γ(n)=(n−1)!\Gamma(n) = (n – 1)!By parts
Gammamean αβ\frac{\alpha}{\beta}, variance αβ2\frac{\alpha}{\beta^2}, mgf (ββ−t)α\left(\frac{\beta}{\beta – t}\right)^\alpha∫xa−1e−bx=Γ(a)/ba\int x^{a – 1} e^{-bx} = \Gamma(a)/b^a
MixtureF=∑pkFkF = \sum p_k F_k, Bayes, E=∑pkEkE = \sum p_k E_kTotal probability
Betamean αα+β\frac{\alpha}{\alpha + \beta}, mode α−1α+β−2\frac{\alpha – 1}{\alpha + \beta – 2}Ratio of BB functions

Three ideas to take away:

  • Almost everything comes from two tools: integration by parts (with the uu, dudu, dvdv, vv 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.

Next step: practiceContinuous random variables — practice in R with a simulated microwave queue, three lunchboxes that add up to a gamma and a model fitted to data (and the rgamma that gave me times four times too short)

Did this article help you? Here is how you can support SergioLearns →