Continuous random variables — theory with the cafeteria microwave, the exponential, the gamma and the beta (and the 18% I promised Laura)

I told Laura there was an 18% chance we would have to wait exactly two minutes in the queue for the cafeteria microwave. I had done the maths in R: dexp(2, 0.5) gave 0.18. She asked “exactly 2, as in not 1.99 and not 2.01?”, and that is when I realised the probability of waiting exactly two minutes is 0, and that 0.18 was not the probability of anything. It was the height of a curve. This topic is about understanding why.

In topic 3 we counted: memes in ten messages, messages until the first useful one. Now we measure: how long until the next person joins the queue, how many minutes each person sets for their lunchbox, at what point of the lunch hour most people arrive. Those variables do not jump from one value to the next: they can take any value in an interval. The whole topic revolves around the microwave in the faculty cafeteria at lunchtime, with made-up but realistic data. Each idea appears first in the example, then with its formula and then in R, with how to read every output.

Before you start. For this topic you need what you saw in topic 3 (probability mass function, distribution function, mean and variance of a discrete variable, and R’s d, p, q and r functions) and, from calculus, being able to integrate powers such as t t or t2 t^2 and to apply the fundamental theorem of calculus: the antiderivative at the upper limit minus the antiderivative at the lower one. Nothing else. The topic has four big ideas, in this order: why in a continuous variable probability is an area and not a height (the density); how that area builds up to a point (the distribution function, and from there the quantiles); how the mean and the variance are computed when the sum turns into an integral; and four named models (uniform, exponential, gamma and beta) with their functions in R. Most of the time we use the same example: the time each person sets the microwave for their lunchbox, with density f(t)=t/8 f(t) = t/8 between 0 and 4 minutes. If you get lost, go back to that straight line and ask yourself which area you are looking at.

From counting to measuring

What a continuous variable is

A random variable is continuous when it can take any value in an interval: (a,b)(a, b), (0,+∞)(0, +\infty) or the whole real line. The time until someone joins the queue, the minutes they set on the microwave, the exact time they arrive: between any two values there are always infinitely many more.

A random variable XX is continuous when it takes values in an interval and its distribution function F(x)=P(X≤x)F(x) = P(X \leq x) is continuous (in the course, also differentiable). Its derivative is the density function, f(x)=F′(x)f(x) = F'(x).

The strangest consequence, and the most important one, is this: the probability of any single value is 0. Why? Think about waiting “exactly 2 minutes”. Not 2.001, not 1.9999; it has to be 2.000000… with infinitely many zeros. If each of the infinitely many possible values had a positive probability, however small, the total would blow up to infinity. So for a continuous variable:

P(X=x)=0for every x P(X = x) = 0 \quad \text{for every } x

So how is probability spread out? Over intervals. It makes sense to ask about “waiting less than 2 minutes” or “between 1 and 3 minutes”, not about “exactly 2”.

From histogram to density

Imagine you write down the time between one person and the next in the queue over many lunchtimes. With 20 times, the histogram is a handful of clumsy bars. With 1,000 times and narrower bars, the outline gets smoother. With a huge amount of data and very thin bars, the outline stops looking like steps and becomes a curve. That curve is the density.

From histogram to densityThree density-scale histograms of times between arrivals: with 20 data points and one-minute bars, irregular; with 1000 data points and half-minute bars, smoother; and with a huge number and 0.1 bars, which follow an exponential curve drawn in coral.0.00.20.40.602468minutesdensity20 times0.00.20.40.602468minutes1,000 times0.00.20.40.602468minutesf(t)a huge numberMore data and thinner bars: the outline of the bars turns into the density curve.
The area of each bar is the proportion of data falling in it. As the bars get thinner, the heights do not go to 0: they approach the curve.

Look at the drawing: in all three panels the height of each bar is “proportion of data in the bar divided by its width” (what R calls density in hist(…, freq = FALSE)). So the area of each bar is the proportion of data falling in it, and the total area is 1. As the bars get thinner, the heights do not go to 0: they approach the curve. That is why, for a continuous variable, what matters is area, not height.

The density function

What it is

The density function f(x)f(x) is the curve that spreads the probability: the probability of an interval is the area under the curve over that interval.

P(a≤X≤b)=∫abf(x) dx P(a \leq X \leq b) = \int_a^b f(x)\,dx

For a function to be a density it must satisfy two things, and they are exactly what the exam asks for when it says “show it is a density” or “find kk so that it is one”:

  1. f(x)≥0f(x) \geq 0 for every xx (there are no negative areas).
  2. ∫−∞+∞f(x) dx=1\int_{-\infty}^{+\infty} f(x)\,dx = 1 (the total probability is 1).

It plays the same role as the mass function in topic 3, with the sum replaced by the integral:

Discrete (topic 3)Continuous (topic 4)
Mass function P(X=xi)P(X = x_i)Density f(x)f(x)
∑iP(X=xi)=1\sum_i P(X = x_i) = 1∫f(x) dx=1\int f(x)\,dx = 1
P(a≤X≤b)=∑a≤xi≤bP(X=xi)P(a \leq X \leq b) = \sum_{a \leq x_i \leq b} P(X = x_i)P(a≤X≤b)=∫abf(x) dxP(a \leq X \leq b) = \int_a^b f(x)\,dx
E(X)=∑xiP(X=xi)E(X) = \sum x_i P(X = x_i)E(X)=∫xf(x) dxE(X) = \int x f(x)\,dx
Endpoints matter: P(X≤4)≠P(X<4)P(X \leq 4) \neq P(X < 4)Endpoints do not matter: P(X≤4)=P(X<4)P(X \leq 4) = P(X < 4)

Reading f(x)f(x) as a probability. f(2)=0.18f(2) = 0.18 does not mean “an 18% chance of waiting 2 minutes”. It is a height, and it can be greater than 1: if a time is uniform between 0 and half a minute, its density is 2 over the whole interval. What can never go above 1 is an area.

An example: how long each person sets

After a week watching the queue (and a few calculations), suppose that the time TT, in minutes, that each person sets their lunchbox for goes from 0 to 4 minutes, and people tend to set rather a lot than a little. A simple model is a density that grows in a straight line:

f(t)=k t,0<t<4 f(t) = k\,t, \qquad 0 < t < 4

and 0 outside (0,4)(0, 4). What is kk? It is positive whenever k>0k > 0, and the integral must equal 1:

∫04k t dt=k[t22]04=k⋅8=1⇒k=18 \int_0^4 k\,t\,dt = k \left[\frac{t^2}{2}\right]_0^4 = k \cdot 8 = 1 \quad \Rightarrow \quad k = \frac{1}{8}

This is the calculation that comes up most in the exam (“find k k so that it is a density”), so slowly:

StepCalculationWhy
1∫04k t dt=1 \int_0^4 k\,t\,dt = 1 The total probability is 1, and outside (0,4) (0, 4) the density is 0: there is only area between 0 and 4
2k∫04t dt k \int_0^4 t\,dt k k is a constant: it comes out of the integral, like a common factor
3∫t dt=t22 \int t\,dt = \frac{t^2}{2} It is the antiderivative of t t : if you differentiate t2/2 t^2/2 , you get t t back
4[t22]04=162−0=8 \left[\frac{t^2}{2}\right]_0^4 = \frac{16}{2} – 0 = 8 Fundamental theorem of calculus: antiderivative at the upper limit minus antiderivative at the lower one
58k=1⇒k=1/8 8k = 1 \Rightarrow k = 1/8 Solve for k k . And it comes out positive, so f(t)≥0 f(t) \geq 0 holds too

With a density that is a straight line, you can check the constant without integrating. The area under k t k\,t between 0 and 4 is a triangle with base 4 and height 4k 4k , so it equals 4⋅4k2=8k \frac{4 \cdot 4k}{2} = 8k . Set it equal to 1 and you get the same k=1/8 k = 1/8 : the height at 4 is 0.5 and the triangle has area 4⋅0.5/2=1 4 \cdot 0.5 / 2 = 1 . If the geometry and the integral do not agree, one of the two is wrong.

So f(t)=t/8f(t) = t/8 between 0 and 4. What is the probability that someone sets more than 3 minutes? It is the area between 3 and 4:

P(T>3)=∫34t8 dt=[t216]34=16−916=716=0.4375 P(T > 3) = \int_3^4 \frac{t}{8}\,dt = \left[\frac{t^2}{16}\right]_3^4 = \frac{16 – 9}{16} = \frac{7}{16} = 0.4375
Probability is an areaDensity f of t equal to t over 8 between 0 and 4: a straight line rising from 0 to 0.5, and 0 outside. The area under the line between 3 and 4, in coral, is 7 sixteenths, 0.4375.0.00.10.20.30.40.50.601234Microwave minutes, tf(t)f(t) = t / 8area = P(T > 3)= 7/16 = 0.4375
The last minute is only a quarter of the interval but takes almost half the probability: the density is higher there.

In the drawing, the line rises from 0 to 0.5 and the area shaded in coral is the probability of going over 3 minutes: almost half of the people, even though the stretch from 3 to 4 is only a quarter of the interval. The density is higher there: that is where probability “piles up”.

Density versus probability, with numbers

Now that we have a concrete density, we can see with numbers what that height is. At t=2 t = 2 , the line is at f(2)=2/8=0.25 f(2) = 2/8 = 0.25 . 0.25 of what? It is not the probability of waiting exactly 2 minutes: that one is 0. Let’s look at shorter and shorter intervals starting at 2 and divide their probability by their width h h . The probabilities come from subtracting areas with the distribution function, F(t)=t2/16 F(t) = t^2/16 , which is built in the next section. They are shown with all their decimals so you can see where they are heading:

Width h h P(2<T<2+h) P(2 < T < 2 + h) Probability divided by widthWhy
10.31250.3125It is the average height of the line between 2 and 3, (0.25+0.375)/2 (0.25 + 0.375)/2 : above 0.25 because the line rises
0.50.1406250.28125With half a minute, the highest part (near 3) is left out and the average height drops
0.10.0256250.25625On such a short stretch the line hardly rises: the area is almost a rectangle of height 0.25
0.010.002506250.250625The probability is now almost 0, but the ratio is not: it stays glued to 0.25

Look at both columns at once. The probability goes to 0 as the interval shrinks: that is why P(T=2)=0 P(T = 2) = 0 . The ratio, on the other hand, approaches 0.25, which is f(2) f(2) . That is the right way to read a density: probability per minute around that point. Just as population density is people per square kilometre and not people, f(2)=0.25 f(2) = 0.25 reads “0.25 of probability for each minute of interval, for short intervals around 2”. This gives the approximation that is used most:

P(x<X<x+h)≈f(x) hfor small h P(x < X < x + h) \approx f(x)\,h \quad \text{for small } h

And it is also why a density can go above 1 without anything breaking: what cannot go above 1 is height times width, the area.

Check you’ve got it: roughly what is the probability that someone sets their lunchbox for between 3 and 3.1 minutes?

About 0.04. The interval is short (h=0.1 h = 0.1 ), so the approximation works: f(3)⋅0.1=38⋅0.1=0.0375 f(3) \cdot 0.1 = \frac{3}{8} \cdot 0.1 = 0.0375 . The exact calculation is F(3.1)−F(3)=9.61−916=0.038 F(3.1) – F(3) = \frac{9.61 – 9}{16} = 0.038 . To two decimals, 0.04 both. The approximation falls slightly short because the line keeps rising inside the interval.

In R

In R, a density is a function like any other, and integrate() computes its integral numerically. With ifelse we tell it that outside (0,4)(0, 4) it is 0:

f <- function(t) ifelse(t > 0 & t < 4, t / 8, 0)   # the heating time density
integrate(f, 0, 4)       # does it integrate to 1?
integrate(f, 3, 4)       # P(T > 3)

Output:

1 with absolute error < 1.1e-14
0.4375 with absolute error < 4.9e-15

How to read it

  • 1 with absolute error < 1.1e-14: the integral between 0 and 4 is 1, so it is a density. The “absolute error below 1.1·10⁻¹⁴” is how far integrate thinks it could be off: nothing. If you get 0.9999999 or 1.0000001, that is rounding; if you get 0.5 or 2, the constant is wrong.
  • 0.4375 with absolute error < 4.9e-15: the probability of going over 3 minutes is 0.4375, the same as by hand. In the exam you give it with 2 decimals: 0.44.
  • integrate returns a list. If you only want the number to keep calculating, ask for its $value: integrate(f, 3, 4)$value gives 0.4375 without the error text.

integrate accepts infinite limits (Inf). For a density on (0,+∞)(0, +\infty), check the constant with integrate(f, 0, Inf).

The distribution function

From density to F and from F to density

The distribution function is the usual one, F(x)=P(X≤x)F(x) = P(X \leq x), but now it is computed by integrating the density from the left up to xx:

F(x)=∫−∞xf(t) dtand, the other way round,f(x)=F′(x) F(x) = \int_{-\infty}^{x} f(t)\,dt \qquad \text{and, the other way round,} \qquad f(x) = F'(x)

And since the probability of any interval is a difference of areas:

P(a<X≤b)=F(b)−F(a) P(a < X \leq b) = F(b) – F(a)

In the example, FF is computed piece by piece:

PieceF(t)F(t)
t≤0t \leq 000
0<t<40 < t < 4∫0tu8 du=t216\int_0^t \frac{u}{8}\,du = \frac{t^2}{16}
t≥4t \geq 411

With it, P(T>3)=1−F(3)=1−9/16=7/16P(T > 3) = 1 – F(3) = 1 – 9/16 = 7/16, the same result without integrating again.

Density and distribution functionTop, the density t over 8 with the area from 0 to 2 shaded, worth 0.25. Bottom, the distribution function t squared over 16, which is 0 before 0, 1 after 4 and 0.25 at t equal to 2; it crosses height 0.5 at the median, 2.83.0.000.250.5001234f(t)area 0.250.000.250.500.751.0001234Microwave minutes, tF(t)F(2) = 0.25median 2.83
The height of F at a point is the area of f up to that point. And where f is high, F rises fast.

Look at how the two graphs are read together. At the top, the density f(t)=t/8f(t) = t/8; at the bottom, F(t)=t2/16F(t) = t^2/16. The height of FF at a point is the area of ff up to that point: at t=2t = 2, the area of the triangle above is 12⋅2⋅0.25=0.25\frac{1}{2} \cdot 2 \cdot 0.25 = 0.25, and F(2)=4/16=0.25F(2) = 4/16 = 0.25. And the slope of FF is the height of ff: where ff is high, FF rises fast.

Why F is worked out piece by piece

The table for F F comes from asking a single question for each t t : how much of the density’s area lies to the left of t t ? The answer changes depending on where t t falls, and that is why there are three pieces:

If t t is…Area to the leftWhy
left of 0 (t≤0 t \leq 0 )0 0 The density is 0 there: nobody sets a negative time, so there is no area to build up
inside (0<t<4 0 < t < 4 )∫0tu8 du=[u216]0t=t216 \int_0^t \frac{u}{8}\,du = \left[\frac{u^2}{16}\right]_0^t = \frac{t^2}{16} You integrate from where the density starts (0) up to t t . The antiderivative of u/8 u/8 is u2/16 u^2/16 , and at 0 it is 0
right of 4 (t≥4 t \geq 4 )1 1 All the area has already built up: F(4)=16/16=1 F(4) = 16/16 = 1 , and beyond that the density adds nothing

Two details that often go unnoticed. The first: inside the integral the letter is u u , not t t , because t t is already taken as the upper limit. The integral runs over the times u u from 0 to t t , and the result depends only on t t . The second: the pieces fit together. The middle one gives F(0)=0 F(0) = 0 and F(4)=1 F(4) = 1 , exactly the values of the pieces on either side, so F F does not jump. If you get a jump when you join the pieces, there is a mistake in the constant or in the antiderivative.

And the way back works as a check: if you differentiate t2/16 t^2/16 you get 2t/16=t/8 2t/16 = t/8 , the density you started from.

Giving only the middle piece, “F(t)=t2/16 F(t) = t^2/16 “, without saying for which t t . With that formula alone, F(8)=64/16=4 F(8) = 64/16 = 4 , a probability greater than 1. The distribution function is defined on the whole real line, and the exam asks for all three pieces.

Check you’ve got it: what is the probability that someone sets their lunchbox for more than 1 minute and at most 3?

0.5. It is a difference of areas: P(1<T≤3)=F(3)−F(1)=916−116=816=0.5 P(1 < T \leq 3) = F(3) – F(1) = \frac{9}{16} – \frac{1}{16} = \frac{8}{16} = 0.5 . Both values fall in the middle piece, so t2/16 t^2/16 is all you need.

The properties of FF are those of topic 3, except one: it no longer has steps.

  • 0≤F(x)≤10 \leq F(x) \leq 1, with F(x)→0F(x) \to 0 on the left and F(x)→1F(x) \to 1 on the right.
  • It is increasing (non-decreasing).
  • It is continuous: it does not jump. That is why P(X=x)=F(x)−F(x−)=0P(X = x) = F(x) – F(x^-) = 0, and why it does not matter whether you write << or ≤\leq.

If you are given FF and asked for the density, differentiate. If you are given ff and asked for FF, integrate from the start of the support up to xx and do not forget the “0 on the left” and “1 on the right” pieces: they count in the exam.

Quantiles and the median

The quantile pp is the value xpx_p that leaves probability pp to its left: F(xp)=pF(x_p) = p. The median is the 0.5 quantile, and the quartiles, the 0.25 and 0.75 quantiles. To find them you solve F(x)=pF(x) = p for xx.

For the lunchbox: t2/16=pt^2/16 = p gives t=4pt = 4\sqrt{p}. The first quartile is 40.25=24\sqrt{0.25} = 2 minutes, the median 40.5=2.834\sqrt{0.5} = 2.83 and the third quartile 40.75=3.464\sqrt{0.75} = 3.46. Half the people set more than 2 minutes and 50 seconds.

Solving is where people trip up most, so step by step with the median (p=0.5 p = 0.5 ):

StepCalculationWhy
1t216=0.5 \frac{t^2}{16} = 0.5 The median leaves half of the area to its left, F(t)=0.5 F(t) = 0.5 . You use the middle piece because 0.5 is between 0 and 1
2t2=8 t^2 = 8 The 16 moves across as a multiplication
3t=±8=±2.83 t = \pm\sqrt{8} = \pm 2.83 A quadratic equation gives two roots
4t=2.83 t = 2.83 The negative one is outside the support (0,4) (0, 4) : nobody sets the microwave for −2.83 -2.83 minutes

And you can always check it the other way round: F(2.83)=2.832/16=8.01/16=0.50 F(2.83) = 2.83^2/16 = 8.01/16 = 0.50 .

Before solving, place the answer. If the density grows to the right, as here, the area piles up at the end and the median has to land to the right of the midpoint, 2. Getting 2.83 makes sense; if you got 1.4, you would know something was wrong without looking at any calculation. Any other quantile works the same way: 90% of people set less than 40.9=3.79 4\sqrt{0.9} = 3.79 minutes, very close to the end, because that is where almost all the area is.

When the equation F(x)=pF(x) = p cannot be solved by hand (it happens a lot with polynomials of degree 3 or more), it is solved numerically. In R, with uniroot(), which looks for where a function is zero inside an interval:

F <- function(t) t^2 / 16            # F on the piece (0, 4)
uniroot(function(t) F(t) - 0.5, c(0, 4))   # the median: F(t) - 0.5 = 0
4 * sqrt(c(0.25, 0.5, 0.75))         # the three quartiles, solved by hand

Output:

$root
[1] 2.828427

$f.root
[1] -1.713868e-07

$iter
[1] 6

$init.it
[1] NA

$estim.prec
[1] 6.103516e-05

[1] 2.000000 2.828427 3.464102

How to read it

  • $root is what you were looking for: the median is 2.828427 minutes, the same as 40.54\sqrt{0.5}. With 2 decimals, 2.83.
  • $f.root is the value of the function at that root: F(t)−0.5=−1.7⋅10−7F(t) – 0.5 = -1.7 \cdot 10^{-7}, practically 0. If it is not almost 0, the root is not a good one.
  • $iter is the number of rounds the algorithm took (6) and $estim.prec, the precision it estimates (about 6 hundred-thousandths of a minute). $init.it shows NA because it did not need to widen the interval.
  • The last line is the three quartiles solved by hand, for comparison: 2, 2.83 and 3.46.

Writing uniroot(F, c(0, 4)) without subtracting 0.5. That looks for where FF is 0, which is at t=0t = 0, and it also fails with “f() values at end points not of opposite sign” if the function does not change sign in the interval. You always pass F(x)−pF(x) – p to uniroot.

The EC1 asks for the median “with the rootSolve library” (the function uniroot.all). The site’s R playground does not have that package, and base R’s uniroot does the same for a single root. On your laptop, in RStudio, you can use either.

Mean, variance and moments

Expectation and variance

They are the ones from topic 3 with an integral instead of the sum:

E(X)=∫−∞+∞x f(x) dx=μVar(X)=∫−∞+∞(x−μ)2f(x) dx=σ2 E(X) = \int_{-\infty}^{+\infty} x\,f(x)\,dx = \mu \qquad \text{Var}(X) = \int_{-\infty}^{+\infty} (x – \mu)^2 f(x)\,dx = \sigma^2

And, as there, the variance is nearly always computed with the shortcut formula Var(X)=E(X2)−μ2\text{Var}(X) = E(X^2) – \mu^2, with E(X2)=∫x2f(x) dxE(X^2) = \int x^2 f(x)\,dx.

Why the mean is an integral

The formula for the mean can be read in words: each value, multiplied by how much it weighs, all added up. In topic 3, “how much it weighs” was P(X=xi) P(X = x_i) . Here a single value weighs nothing, but a small piece of width dx dx around x x weighs roughly f(x) dx f(x)\,dx : it is the approximation from “density versus probability”. So x f(x) dx x\,f(x)\,dx is “the value times what its small piece weighs”, and the integral adds up all the pieces. The variance is the same with (x−μ)2 (x – \mu)^2 instead of x x : the mean of the squared distances.

You can see it with the lunchbox if we split (0,4) (0, 4) into four one-minute pieces and treat it as if it were discrete: each piece, represented by its midpoint, with the probability of the piece.

PieceMidpointProbability of the pieceWhy
(0,1) (0, 1) 0.5F(1)−F(0)=1/16 F(1) – F(0) = 1/16 The first minute has hardly any area: the line starts at 0
(1,2) (1, 2) 1.5F(2)−F(1)=3/16 F(2) – F(1) = 3/16 4/16−1/16 4/16 – 1/16
(2,3) (2, 3) 2.5F(3)−F(2)=5/16 F(3) – F(2) = 5/16 9/16−4/16 9/16 – 4/16
(3,4) (3, 4) 3.5F(4)−F(3)=7/16 F(4) – F(3) = 7/16 16/16−9/16 16/16 – 9/16 : the last minute weighs the most

The “discrete” mean of this table is

0.5⋅1+1.5⋅3+2.5⋅5+3.5⋅716=4216=2.63 \frac{0.5 \cdot 1 + 1.5 \cdot 3 + 2.5 \cdot 5 + 3.5 \cdot 7}{16} = \frac{42}{16} = 2.63

With half-minute pieces it comes out at 2.66, and the finer the pieces, the closer it gets to the value of the integral, 2.67, which is the calculation just below. That is what the integral is: the sum of this table with infinitely thin pieces.

For the lunchbox:

E(T)=∫04t⋅t8 dt=[t324]04=6424=2.67E(T2)=∫04t38 dt=[t432]04=8 E(T) = \int_0^4 t \cdot \frac{t}{8}\,dt = \left[\frac{t^3}{24}\right]_0^4 = \frac{64}{24} = 2.67 \qquad E(T^2) = \int_0^4 \frac{t^3}{8}\,dt = \left[\frac{t^4}{32}\right]_0^4 = 8
Var(T)=8−(83)2=8−649=89=0.89σ=0.94 \text{Var}(T) = 8 – \left(\frac{8}{3}\right)^2 = 8 – \frac{64}{9} = \frac{8}{9} = 0.89 \qquad \sigma = 0.94

The two lines above pack several steps together. Separated:

StepCalculationWhy
1t⋅t8=t28 t \cdot \frac{t}{8} = \frac{t^2}{8} Inside the integral for the mean goes the value times the density: multiply before integrating
2∫04t28 dt=[t324]04=6424=83 \int_0^4 \frac{t^2}{8}\,dt = \left[\frac{t^3}{24}\right]_0^4 = \frac{64}{24} = \frac{8}{3} The antiderivative of t2 t^2 is t3/3 t^3/3 , and the 8 underneath stays: 3⋅8=24 3 \cdot 8 = 24 . Only from 0 to 4, because outside the density is 0
3t2⋅t8=t38 t^2 \cdot \frac{t}{8} = \frac{t^3}{8} For E(T2) E(T^2) , the value being weighted is t2 t^2 , not t t
4∫04t38 dt=[t432]04=25632=8 \int_0^4 \frac{t^3}{8}\,dt = \left[\frac{t^4}{32}\right]_0^4 = \frac{256}{32} = 8 The antiderivative of t3 t^3 is t4/4 t^4/4 , and 4⋅8=32 4 \cdot 8 = 32
58−(83)2=72−649=89 8 – \left(\frac{8}{3}\right)^2 = \frac{72 – 64}{9} = \frac{8}{9} Shortcut formula, with fractions until the end: with 2.67 already rounded you would get 8−7.13=0.87 8 – 7.13 = 0.87
6σ=8/9=0.94 \sigma = \sqrt{8/9} = 0.94 The square root brings the spread back to minutes, the units of T T

On average, each lunchbox spends 2.67 minutes inside, about 2 minutes and 40 seconds. Notice that the mean (2.67) is smaller than the median (2.83): the density has a longer “tail” to the left (the few people who set one minute pull the mean down). It is a distribution with negative skewness.

f <- function(t) ifelse(t > 0 & t < 4, t / 8, 0)
mean_t <- integrate(function(t) t * f(t), 0, 4)$value
ET2 <- integrate(function(t) t^2 * f(t), 0, 4)$value
variance <- ET2 - mean_t^2
round(c(mean = mean_t, ET2 = ET2, variance = variance, sd = sqrt(variance)), 2)

Output:

    mean      ET2 variance       sd 
    2.67     8.00     0.89     0.94 

How to read it

  • Each integrate(…)$value is a plain number we can store. For the mean you integrate t⋅f(t)t \cdot f(t); for E(T2)E(T^2), t2⋅f(t)t^2 \cdot f(t). The function you pass to integrate is what goes inside the integral, not the density on its own.
  • The output is a named vector: mean 2.67, E(T2)=8E(T^2) = 8, variance 0.89 and standard deviation 0.94 minutes. The names appear above each number because we wrote them in c(mean = …).

The properties used most

They are the same as in topic 3, for the same reason (the integral is linear, like the sum). If aa, bb and cc are constants:

  • E(c)=cE(c) = c and Var(c)=0\text{Var}(c) = 0.
  • E(aX+b)=a E(X)+bE(aX + b) = a\,E(X) + b and Var(aX+b)=a2 Var(X)\text{Var}(aX + b) = a^2\,\text{Var}(X).
  • E(g1(X)+g2(X))=E(g1(X))+E(g2(X))E\big(g_1(X) + g_2(X)\big) = E\big(g_1(X)\big) + E\big(g_2(X)\big).
  • E(g(X))=∫g(x)f(x) dxE\big(g(X)\big) = \int g(x) f(x)\,dx: for the expectation of a function of XX you do not need the distribution of g(X)g(X).

For example, if the cafeteria charged €0.10 per microwave minute plus a fixed €0.20, the cost G=0.10 T+0.20G = 0.10\,T + 0.20 would have mean 0.10⋅2.67+0.20=0.470.10 \cdot 2.67 + 0.20 = 0.47 € and variance 0.102⋅0.89=0.00890.10^2 \cdot 0.89 = 0.0089.

Changing variable without recalculating anything

That cost G G is a new variable built from T T . For its mean and variance we did not need to find the density of G G : those of T T are enough. In words, this is what a a and b b do:

  • The b b shifts, it does not stretch. Adding a fixed €0.20 for everyone raises the mean by 0.20, but does not change how spread out the costs are from one another. That is why it disappears from the variance.
  • The a a stretches, and the variance feels it squared. Multiplying by 0.10 shrinks every distance to the mean to a tenth. The variance measures squared distances, so it is multiplied by 0.102=0.01 0.10^2 = 0.01 . The standard deviation, which is in the same units as G G , only by 0.10: 0.10⋅0.94=0.09 0.10 \cdot 0.94 = 0.09 €.

If you want to check it the long way, the property E(g(T))=∫g(t)f(t) dt E\big(g(T)\big) = \int g(t) f(t)\,dt says you integrate the new function against the usual density:

E(G)=∫04(0.10 t+0.20) t8 dt=0.10⋅83+0.20=0.47 E(G) = \int_0^4 (0.10\,t + 0.20)\,\frac{t}{8}\,dt = 0.10 \cdot \frac{8}{3} + 0.20 = 0.47

The same €0.47: by linearity the integral splits in two, one is E(T) E(T) and the other, the total area, 1.

Putting the mean inside a function that is not a straight line. For aT+b aT + b it holds that E(aT+b)=a E(T)+b E(aT + b) = a\,E(T) + b , but not for T2 T^2 : E(T2)=8 E(T^2) = 8 , whereas (E(T))2=(8/3)2=7.11 \big(E(T)\big)^2 = (8/3)^2 = 7.11 . The difference is exactly the variance, 8/9 8/9 . With a function that is not linear, you integrate g(t)f(t) g(t) f(t) (or use integrate in R).

Check you’ve got it: if you measured the microwave time in seconds, what would the mean, the variance and the standard deviation be?

In seconds the time is S=60 T S = 60\,T : a straight line with a=60 a = 60 and b=0 b = 0 . The mean is multiplied by 60: 60⋅8/3=160 60 \cdot 8/3 = 160 seconds, the same 2 minutes and 40 seconds as before. The variance, by 602=3600 60^2 = 3600 : 3600⋅8/9=3200 3600 \cdot 8/9 = 3200 seconds squared. And the standard deviation, only by 60: 60⋅0.9428=56.57 60 \cdot 0.9428 = 56.57 seconds. If you multiply the already rounded 0.94, you get 56.4: that is why it pays to round only at the end.

When a moment does not exist

For finite discrete variables everything existed. For continuous variables with infinite support, an integral may not converge, and then the moment does not exist. Take the density f(x)=3/x4f(x) = 3/x^4 for x≥1x \geq 1 (it integrates to 1: ∫1∞3x−4 dx=[−x−3]1∞=1\int_1^\infty 3x^{-4}\,dx = [-x^{-3}]_1^\infty = 1):

E(X)=∫1∞3x3 dx=32E(X2)=∫1∞3x2 dx=3E(X3)=∫1∞3x dx=[3log⁡x]1∞=+∞ E(X) = \int_1^\infty \frac{3}{x^3}\,dx = \frac{3}{2} \qquad E(X^2) = \int_1^\infty \frac{3}{x^2}\,dx = 3 \qquad E(X^3) = \int_1^\infty \frac{3}{x}\,dx = \big[3 \log x\big]_1^\infty = +\infty

It has a mean and a variance (3−9/4=0.753 – 9/4 = 0.75), but no third moment: the tail falls so slowly that x3f(x)=3/xx^3 f(x) = 3/x no longer leaves a finite area. integrate warns you with an error (“the integral is probably divergent”) instead of giving you a number.

Skewness and kurtosis

As in topic 3, with integrals: the skewness coefficient α3=E[(X−μ)3]/σ3\alpha_3 = E[(X – \mu)^3]/\sigma^3 (positive with the tail on the right, negative on the left) and the kurtosis α4=E[(X−μ)4]/σ4\alpha_4 = E[(X – \mu)^4]/\sigma^4 (how heavy the tails are; sometimes 3 is subtracted). For the lunchbox, the skewness is negative (−0.57), as we had guessed by comparing mean and median.

The moment generating function

The mgf is defined as in topic 3:

φX(t)=E(etX)=∫−∞+∞etxf(x) dx \varphi_X(t) = E\big(e^{tX}\big) = \int_{-\infty}^{+\infty} e^{tx} f(x)\,dx

as long as the integral is finite for the tt in a neighbourhood of 0. And it is used for the same thing: its derivatives at 0 give the moments.

φX(k)(0)=E(Xk),E(X)=φX′(0),E(X2)=φX′′(0) \varphi_X^{(k)}(0) = E\big(X^k\big), \qquad E(X) = \varphi_X'(0), \qquad E(X^2) = \varphi_X''(0)

The practical difference is that in the continuous case computing it means integrating, and sometimes the integral does not exist for any t>0t > 0 (it happens with the density 3/x43/x^4: etxe^{tx} grows faster than any power). For the models in this topic (uniform, exponential and gamma) it does exist and has a closed form that is used a lot in the exam, especially in the bonus questions.

The mgf characterises the distribution: if two variables have the same mgf, they have the same distribution. That is why in topic 5 we will use it to prove which distribution a transformation or a sum has.

The continuous models

Just as topic 3 had named models (binomial, Poisson…), here there are four you need to recognise in a problem. In R, each has its four functions d, p, q and r with the model’s name: unif, exp, gamma and beta.

Uniform

X∼Unif(a,b)X \sim \text{Unif}(a, b) when all values in the interval are “equally likely”: the density is flat.

f(x)=1b−a,a<x<bF(x)=x−ab−a,a<x<b f(x) = \frac{1}{b – a}, \quad a < x < b \qquad F(x) = \frac{x – a}{b – a}, \quad a < x < b
E(X)=a+b2Var(X)=(b−a)212φX(t)=etb−etat (b−a) 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)}

In the cafeteria: you arrive, the microwave is running a 3-minute programme and you have no idea when it started. What is left until it finishes, RR, is uniform between 0 and 3. The average wait is 1.5 minutes and P(R>2)=1−23=13P(R > 2) = 1 – \frac{2}{3} = \frac{1}{3}: one time in three you have to wait more than two minutes just for the person ahead to finish.

punif(2, 0, 3)           # P(R <= 2)
1 - punif(2, 0, 3)       # P(R > 2)
qunif(0.9, 0, 3)         # 90% of the time you wait less than this

Output:

[1] 0.6666667
[1] 0.3333333
[1] 2.7

punif(2, 0, 3) is F(2)=2/3F(2) = 2/3; 1 - punif(…) is the tail, 1/31/3, and qunif(0.9, 0, 3) says that 90% of the time the wait is under 2.7 minutes. In unif, the two numbers after x are the endpoints, min and max, not the mean or the width.

Exponential

The exponential is the model for waiting times until something happens: the time between one person and the next in the queue, until the next notification, until a component fails. T∼exp⁡(λ)T \sim \exp(\lambda), with λ>0\lambda > 0:

f(t)=λe−λt,t>0F(t)=1−e−λt,t>0 f(t) = \lambda e^{-\lambda t}, \quad t > 0 \qquad F(t) = 1 – e^{-\lambda t}, \quad t > 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}, \quad t < \lambda

λ\lambda is a rate: how many things happen per unit of time. If, on average, one person arrives at the microwave every 2 minutes, the rate is λ=0.5\lambda = 0.5 people per minute and the mean time between arrivals, 1/λ=21/\lambda = 2 minutes.

The survival function is used a lot, S(t)=P(T>t)=1−F(t)=e−λtS(t) = P(T > t) = 1 – F(t) = e^{-\lambda t}: the probability that nothing has happened yet after tt minutes. Nobody arriving in 5 minutes: S(5)=e−2.5=0.082S(5) = e^{-2.5} = 0.082.

pexp(1, 0.5)             # P(T <= 1): someone arrives within a minute
1 - pexp(5, 0.5)         # P(T > 5): five minutes with nobody
qexp(0.5, 0.5)           # the median
dexp(2, 0.5)             # the height of the density at 2 (not a probability)

Output:

[1] 0.3934693
[1] 0.082085
[1] 1.386294
[1] 0.1839397

How to read it

  • 0.3934693: 39% of the time the next person arrives within a minute. It is 1−e−0.51 – e^{-0.5}.
  • 0.082085: only 8% of the time do 5 minutes go by with nobody arriving. It is the tail, e−2.5e^{-2.5}.
  • 1.386294: the median is 1.39 minutes, smaller than the mean (2). The exponential has a long right tail: many short times and a few very long ones that pull the mean up. The median comes from solving 1−e−0.5t=0.51 – e^{-0.5t} = 0.5: t=log⁡(2)/0.5t = \log(2)/0.5.
  • 0.1839397: the height of the density at t=2t = 2. This is the number I sold Laura as “an 18% chance”. It is not a probability: P(T=2)=0P(T = 2) = 0. If you wanted something similar, it would be the probability of a small interval: P(1.95<T<2.05)≈0.18⋅0.1=0.018P(1.95 < T < 2.05) \approx 0.18 \cdot 0.1 = 0.018.
Exponential with rate 0.5: the time between arrivalsExponential density with rate 0.5 from 0 to 10 minutes, highest at 0 with value 0.5. Violet area left of 1, worth 0.3935; coral area right of 5, worth 0.0821. Dashed lines at the median, 1.39, and the mean, 2.0.00.10.20.30.40.50246810Minutes until the next arrivalf(t)0.3935P(T < 1) = 0.3935P(T > 5) = 0.0821median 1.39mean 2mode at 0
Many short times and a few long ones: the right tail pulls the mean away from the median.

The drawing shows the shape of the exponential: the density is highest at 0 (the mode is at 0) and keeps falling. The violet area, left of 1, is the 39% of arrivals within a minute; the coral one, right of 5, the 8% of waiting more than 5. The median (1.39) and the mean (2) are separated by the tail.

Memorylessness. The exponential is the only continuous distribution that “does not remember” how long you have already waited:

P(T>s+t∣T>s)=P(T>t) P(T > s + t \mid T > s) = P(T > t)

If 3 minutes have gone by without anyone arriving, the probability that nobody arrives in the next 2 is the same as if you had just got there: e−0.5⋅2=0.37e^{-0.5 \cdot 2} = 0.37. It is the continuous version of what we saw with the geometric in topic 3. Does it make sense in the queue? For people arriving, fairly well: nobody having arrived in 3 minutes does not mean someone is “due”. For the time until an old microwave breaks down, no: wear does matter there, and the exponential would not be a good model.

Memorylessness, with the full calculation

That 0.37 comes out in one line, but underneath there is a conditional probability. “I have been here 3 minutes with nobody arriving and nobody arrives in the next 2” is “T>5 T > 5 given that T>3 T > 3 “:

StepCalculationWhy
1P(T>5∣T>3)=P(T>5 and T>3)P(T>3) P(T > 5 \mid T > 3) = \frac{P(T > 5 \text{ and } T > 3)}{P(T > 3)} It is the definition of conditional probability, the one from topic 2
2P(T>5 and T>3)=P(T>5) P(T > 5 \text{ and } T > 3) = P(T > 5) If more than 5 minutes go by, more than 3 certainly have: the “and” is redundant
3e−0.5⋅5e−0.5⋅3=0.0820.223 \frac{e^{-0.5 \cdot 5}}{e^{-0.5 \cdot 3}} = \frac{0.082}{0.223} Both are survival probabilities, S(t)=e−λt S(t) = e^{-\lambda t}
4e−0.5⋅(5−3)=e−1=0.37 e^{-0.5 \cdot (5 – 3)} = e^{-1} = 0.37 Dividing powers with the same base subtracts the exponents: the 3 minutes already waited cancel out

Step 4 is the whole explanation. In the exponential the survival function is a power, and when you divide only the difference is left, 5−3=2 5 – 3 = 2 : what you have already waited drops out of the calculation. With another density the ratio does not simplify like that. With the lunchbox one, if someone has already had their lunchbox in for 2 minutes, the probability that it goes past 3 is 1−9/161−4/16=712=0.58 \frac{1 – 9/16}{1 – 4/16} = \frac{7}{12} = 0.58 , whereas for someone who has just put it in, lasting one more minute has probability 1−1/16=0.94 1 – 1/16 = 0.94 . There what you have already waited does count, because nobody goes past 4 minutes.

Check you’ve got it: if 1 minute has already gone by with nobody arriving, what is the probability that there is still nobody at 4 minutes?

0.22. It is P(T>4∣T>1)=e−0.5⋅4e−0.5⋅1=e−0.5⋅3=e−1.5=0.22 P(T > 4 \mid T > 1) = \frac{e^{-0.5 \cdot 4}}{e^{-0.5 \cdot 1}} = e^{-0.5 \cdot 3} = e^{-1.5} = 0.22 : by memorylessness, the same as waiting 3 minutes with nobody for someone who has just arrived. The typical mistake is to answer P(T>4)=e−2=0.14 P(T > 4) = e^{-2} = 0.14 , which forgets that the first minute has already gone by with nobody.

The memorylessness of the exponentialSurvival function e to the minus 0.5 t in violet. In dashed coral, the same curve starting again at t equal to 3: the probability of seeing nobody for two more minutes, after 3 with nobody, is 0.3679, the same as seeing nobody in the first 2 minutes.0.000.250.500.751.000235810Minutes since you arrivedP(T > t)P(T > 2) = 0.3679P(T > 5 | T > 3) = 0.3679from when you arriveafter 3 min with nobody
What you have already waited does not count: from minute 3 on, the coral curve is the violet one shifted.

The link with Poisson. If the number of arrivals in an interval of tt minutes is Poisson with mean λt\lambda t, the time between arrivals is exp⁡(λ)\exp(\lambda). They are two sides of the same process: Poisson counts how many arrive; the exponential measures how long until the next one. With λ=0.5\lambda = 0.5, on average 5 people arrive in 10 minutes.

Gamma

What if there are three people ahead of you and each one takes an exponential time? The total time is no longer exponential: it is a gamma. X∼gamma(α,β)X \sim \text{gamma}(\alpha, \beta), with α,β>0\alpha, \beta > 0:

f(x)=βαΓ(α) xα−1e−βx,x>0 f(x) = \frac{\beta^\alpha}{\Gamma(\alpha)}\,x^{\alpha – 1} e^{-\beta x}, \quad x > 0

where Γ(α)=∫0∞tα−1e−t dt\Gamma(\alpha) = \int_0^\infty t^{\alpha – 1} e^{-t}\,dt is the gamma function, which generalises the factorial: Γ(n)=(n−1)!\Gamma(n) = (n – 1)! for integer nn (Γ(1)=1\Gamma(1) = 1, Γ(3)=2\Gamma(3) = 2, Γ(5)=24\Gamma(5) = 24).

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

α\alpha is the shape parameter and β\beta is a rate, like the λ\lambda of the exponential. Cases you need to recognise:

  • gamma(1,λ)=exp⁡(λ)\text{gamma}(1, \lambda) = \exp(\lambda): with α=1\alpha = 1, the formula is the exponential’s.
  • The sum of α\alpha independent exponentials with rate β\beta (with integer α\alpha) is gamma(α,β)\text{gamma}(\alpha, \beta). This is the one that comes up most: several steps in a row, each with an exponential time.
  • gamma(α,1/2)=χ2(2α)\text{gamma}(\alpha, 1/2) = \chi^2(2\alpha), the chi-squared, which we will see in topic 8.

In the cafeteria: there are three people ahead of you, and what each one takes (putting the lunchbox in, waiting, taking it out) is exponential with a mean of 2 minutes, independent of the others. Your wait is W∼gamma(3,0.5)W \sim \text{gamma}(3, 0.5), with mean 3/0.5=63/0.5 = 6 minutes and variance 3/0.25=123/0.25 = 12.

pgamma(5, 3, 0.5)        # P(W <= 5): eating within 5 minutes
1 - pgamma(10, 3, 0.5)   # P(W > 10): more than 10 minutes queuing
qgamma(0.5, 3, 0.5)      # the median wait
qgamma(0.9, 3, 0.5)      # 90% of the time you wait less than this

Output:

[1] 0.4561869
[1] 0.124652
[1] 5.348121
[1] 10.64464

How to read it

  • 0.4561869: with three people ahead, only 46% of the time is it your turn within 5 minutes.
  • 0.124652: 12% of the time the queue lasts more than 10 minutes. A curiosity: it is exactly ppois(2, 5), the probability that in 10 minutes at most 2 people “get served”. Gamma and Poisson tell the same story from different sides.
  • 5.348121: the median, 5.35 minutes, again smaller than the mean (6): the gamma also has its tail on the right.
  • 10.64464: if you want to be 90% sure of not being late for class, allow 10.64 minutes of queue.

In R, pgamma(x, 3, 0.5) takes the third argument as the rate, the β\beta in our formula. If the problem gives you the scale (the mean of each step, 2 minutes), you must write pgamma(x, 3, scale = 2) or pass the rate, 0.5. Writing pgamma(x, 3, 2) models three steps of half a minute each. Same with pexp: pexp(x, 0.5) is mean 2, not pexp(x, 2).

Gammas with rate 0.5 and different shapesFour gamma densities with rate 0.5. With shape 1 it is the exponential, highest at 0. With shape 2, 3 and 5 the curve starts at 0, has its mode at 2, 4 and 8 minutes and becomes more symmetric. Shape 3, in coral, is the wait with three people ahead.0.00.10.20.30.40.5048121620Minutes waitingf(w)mode 4α = 1 (exponential)α = 2α = 3 (three ahead)α = 5
Each step added pushes the peak to the right (mode (α − 1)/β) and rounds the curve.

In the drawing, four gammas with the same rate, 0.5, and shape α\alpha = 1, 2, 3 and 5. With α=1\alpha = 1 it is the exponential (highest at 0). As steps are added, the curve moves away from 0, the mode shifts right ((α−1)/β(\alpha – 1)/\beta: 2, 4 and 8 minutes) and it becomes more symmetric. The coral one, α=3\alpha = 3, is your wait with three people ahead.

If you are given the mean μ\mu and the standard deviation σ\sigma of a gamma, solve for the parameters from the two formulas: β=μ/σ2\beta = \mu/\sigma^2 and α=μ2/σ2\alpha = \mu^2/\sigma^2. For example, mean 6 and variance 12 give β=0.5\beta = 0.5 and α=3\alpha = 3.

Beta

The beta is the model for proportions: variables that live between 0 and 1. X∼beta(α,β)X \sim \text{beta}(\alpha, \beta), with α,β>0\alpha, \beta > 0:

f(x)=Γ(α+β)Γ(α) Γ(β) xα−1(1−x)β−1,0<x<1 f(x) = \frac{\Gamma(\alpha + \beta)}{\Gamma(\alpha)\,\Gamma(\beta)}\,x^{\alpha – 1}(1 – x)^{\beta – 1}, \quad 0 < x < 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)

With α=β=1\alpha = \beta = 1 the density is flat: beta(1,1)=Unif(0,1)\text{beta}(1, 1) = \text{Unif}(0, 1). With α>β\alpha > \beta it piles up towards 1; with α<β\alpha < \beta, towards 0.

In the cafeteria: the lunch hour goes from 13:00 to 14:00. Let XX be the moment a person arrives, as a fraction of the hour (0 = 13:00, 1 = 14:00). People do not arrive uniformly: there is a peak at the start, when classes end, and then it empties out. A good model is X∼beta(2,3)X \sim \text{beta}(2, 3), whose density is

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

(Γ(5)/(Γ(2)Γ(3))=24/2=12\Gamma(5)/(\Gamma(2)\Gamma(3)) = 24/2 = 12). Its mean is 2/5=0.42/5 = 0.4, that is, 13:24; its mode, 1/31/3, 13:20; and its variance, 6/(25⋅6)=0.046/(25 \cdot 6) = 0.04, with standard deviation 0.2 (12 minutes).

pbeta(0.5, 2, 3)                      # arrives before 13:30
pbeta(0.5, 2, 3) - pbeta(0.25, 2, 3)  # between 13:15 and 13:30
1 - pbeta(0.75, 2, 3)                 # in the last quarter of an hour
qbeta(0.5, 2, 3)                      # the median

Output:

[1] 0.6875
[1] 0.4257812
[1] 0.05078125
[1] 0.3857276

Almost 69% of people arrive in the first half hour; 43%, between 13:15 and 13:30; only 5%, in the last quarter of an hour. The median is 0.386 of an hour, about 23 minutes: 13:23. If you want the microwave free, go at 13:45.

Three betas: when people arrive for lunchBeta 2 3 density in violet, peaking at one third (13:20) with mean 0.4; beta 3 2 in coral, its mirror image, peaking at two thirds; and beta 1 1 in grey, flat at height 1, which is the uniform. Under the axis, the times from 13:00 to 14:00.0.00.51.01.52.00.000.250.500.751.00Arrival moment (fraction of the hour)f(x)13:0013:1513:3013:4514:00mode 1/3 (13:20)mean 0.4beta(2, 3)beta(3, 2)beta(1, 1) = Unif(0, 1)
With α < β the beta piles up towards 0; with α > β, towards 1; with α = β = 1, flat.

Look at the three betas in the drawing. The violet one, beta(2,3)\text{beta}(2, 3), is the lunch arrival: it rises fast, peaks at 1/3 and falls slowly towards 1. The coral one, beta(3,2)\text{beta}(3, 2), is its mirror image: it piles up towards the end. And the grey one is beta(1,1)\text{beta}(1, 1), the uniform: all moments equally likely.

R’s d, p, q and r functions

They are the same four letters as in topic 3, but d changes meaning: it is no longer a probability, it is the height of the density.

LetterWhat it returnsExampleResult
dThe density f(x)f(x), a heightdexp(2, 0.5)0.18 (not a probability)
pF(x)=P(X≤x)F(x) = P(X \leq x), an areapexp(2, 0.5)0.63
qThe quantile: the xx with F(x)=pF(x) = pqexp(0.5, 0.5)1.39
rSimulated valuesrexp(3, 0.5)three random times

And the parameters of each model, in the order R wants them:

ModelIn RParametersMean
Unif(a,b)\text{Unif}(a, b)dunif(x, a, b)min, max(a+b)/2(a + b)/2
exp⁡(λ)\exp(\lambda)dexp(x, lambda)rate = λ\lambda1/λ1/\lambda
gamma(α,β)\text{gamma}(\alpha, \beta)dgamma(x, alpha, beta)shape = α\alpha, rate = β\beta (or scale = 1/β1/\beta)α/β\alpha/\beta
beta(α,β)\text{beta}(\alpha, \beta)dbeta(x, alpha, beta)shape1, shape2α/(α+β)\alpha/(\alpha + \beta)
R's four letters on the same gammaGamma 3 0.5 density. In coral, a vertical line at 4 minutes with height 0.1353: that is d. In violet, the area up to 5, 0.4562: that is p. A dashed line at 5.35 marks the 0.5 quantile: that is q. Green dots on the axis, closer together where the curve is high: that is r.0.000.050.100.15045101520Minutes waiting, gamma(3, 0.5)f(w)pd: a heightdgamma(4, 3, 0.5) = 0.1353p: an areapgamma(5, 3, 0.5) = 0.4562q: from area 0.5 to the pointqgamma(0.5, 3, 0.5) = 5.35r: random pointsrgamma(14, 3, 0.5)
d measures heights, p measures areas, q undoes p and r scatters points following the curve.

The drawing puts the four letters on the wait with three people ahead, gamma(3,0.5)\text{gamma}(3, 0.5): d is a height (the line at 4 minutes), p is an area (shaded up to 5), q goes the other way (from an area of 0.5 to the point 5.35) and r gives points that fall more often where the curve is high.

Since endpoints do not matter, for a continuous variable P(X>a)=1−F(a)P(X > a) = 1 – F(a) and P(a<X<b)=F(b)−F(a)P(a < X < b) = F(b) – F(a) without thinking about “greater than” versus “greater than or equal”. That worry from topic 3 (the pbinom(4, …) for “at least 5”) disappears. You can also use lower.tail = FALSE: pexp(5, 0.5, lower.tail = FALSE) gives the tail directly.

How to choose the model

If the variable is…ModelExample in the cafeteria
A “random” value in an interval, with no preferenceUniformWhat is left of the programme of the person ahead
The time until something that happens at a constant rate and without memoryExponentialThe time until the next person arrives
The sum of several independent exponential times, or a positive time with a right tailGammaYour wait with three people ahead
A proportion or a value between 0 and 1BetaThe point in the lunch hour when someone arrives
A density you are given with a constant kkNone with a name: you integrateThe heating time, f(t)=k tf(t) = k\,t

Before choosing, look at the support: if the variable can be negative, it is not exponential or gamma; if it lies between 0 and 1, think beta; if it has a clear maximum and minimum and nothing makes it prefer a value, uniform. The normal, the bell curve, is topic 5.

What is proved in this topic

Here you have seen the results. In the continuous random variable proofs you have, step by step, why they are true, with the same cafeteria:

ResultProof
Finding the constant of a density, its FF, its mean, its variance and its median (the 15-point EC1 exercise)Proof 1
The uniform: mean, variance and mgfProof 2
The exponential: mean and variance by parts, the mgf, the mode at 0 and memorylessnessProof 3
The gamma: Γ(n)=(n−1)!\Gamma(n) = (n – 1)!, mean, variance and mgf without brute-force integration, and its modeProof 4
A mixture of densities is a density; total probability and Bayes with timesProof 5
The beta: it integrates to 1, its mean, its variance and its modeProof 6

Common mistakes

Taking the density for a probability. dexp(2, 0.5) is a height. Probabilities are areas: pexp, integrate. For a continuous variable, P(X=x)=0P(X = x) = 0.

Forgetting the pieces of FF. If the density lives on (0,4)(0, 4), the distribution function is 0 before 0 and 1 after 4. Giving only t2/16t^2/16 is incomplete.

Mixing up rate and mean in the exponential and the gamma. “One person every 2 minutes” is λ=0.5\lambda = 0.5, not λ=2\lambda = 2. In R, the third argument of pgamma is the rate.

Integrating only the density for the mean. E(X)E(X) is ∫xf(x) dx\int x f(x)\,dx: in integrate you have to pass function(x) x * f(x).

Using the exponential when wear matters. Memorylessness is a strong property: if what has already happened changes what will happen (a device that ages), the exponential does not work.

Giving the mgf without its condition. The exponential’s is λ/(λ−t)\lambda/(\lambda – t) only for t<λt < \lambda; for t≥λt \geq \lambda the integral does not converge.

Try it in the R playground

Open the site’s R playground (or RStudio) and paste this script. It brings together the heating time, the arrivals in the queue and the wait with three people ahead:

# A density with a constant: f(t) = t/8 on (0, 4)
f <- function(t) ifelse(t > 0 & t < 4, t / 8, 0)
integrate(f, 0, 4)$value                    # must give 1
integrate(function(t) t * f(t), 0, 4)$value # the mean
curve(f, from = -1, to = 5, lwd = 2, ylab = "f(t)")
# Arrivals in the queue: exponential with rate 0.5 per minute
1 - pexp(5, 0.5)
curve(dexp(x, 0.5), from = 0, to = 10, lwd = 2, ylab = "f(t)")
# Three people ahead: gamma(3, 0.5)
pgamma(5, 3, 0.5)
curve(dgamma(x, 3, 0.5), from = 0, to = 20, lwd = 2, ylab = "f(w)")

What to look at:

  • In the Environment panel, f appears as a function: it is a hand-made density and you can use it in integrate, curve or uniroot.
  • The first plot is the line t/8t/8, with a drop to 0 at the edges. Change t / 8 to t / 4 and run integrate again: now it gives 2, and it is no longer a density.
  • In the exponential, change the 0.5 to 2: the curve hugs 0 and the mean becomes half a minute.
  • In the gamma, change the 3 to 1, 2 and 5 and see how the peak moves: it is the drawing from the gamma section.
  • Type ?dgamma in the console and read the order of the arguments in Help: shape, rate and scale.

Summary

ConceptFormulaIn R
Densityf(x)≥0f(x) \geq 0, ∫f=1\int f = 1; P(a≤X≤b)=∫abfP(a \leq X \leq b) = \int_a^b fintegrate(f, a, b)
DistributionF(x)=∫−∞xfF(x) = \int_{-\infty}^x f, f=F′f = F'p…
A single valueP(X=x)=0P(X = x) = 0—
QuantileF(xp)=pF(x_p) = pq…, uniroot(function(x) F(x) - p, …)
Mean and variance∫xf\int x f, ∫x2f−μ2\int x^2 f – \mu^2integrate(function(x) x * f(x), …)
Uniform1/(b−a)1/(b – a); mean (a+b)/2(a + b)/2, variance (b−a)2/12(b – a)^2/12dunif(x, a, b)
Exponentialλe−λx\lambda e^{-\lambda x}; mean 1/λ1/\lambda, variance 1/λ21/\lambda^2; memorylessdexp(x, lambda)
GammaβαΓ(α)xα−1e−βx\frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha – 1} e^{-\beta x}; mean α/β\alpha/\beta, variance α/β2\alpha/\beta^2dgamma(x, alpha, beta)
Betaxα−1(1−x)β−1B(α,β)\frac{x^{\alpha – 1}(1 – x)^{\beta – 1}}{B(\alpha, \beta)}; mean α/(α+β)\alpha/(\alpha + \beta)dbeta(x, alpha, beta)

Three ideas to take away:

  • For a continuous variable, probability is area, not height. The density can be greater than 1, and the probability of an exact value is 0.
  • Everything from topic 3 carries over by swapping sums for integrals: FF, mean, variance, mgf. And in R, integrate and uniroot do the calculations that would be long by hand.
  • Recognise the model by what it measures: an interval with no preferences (uniform), a memoryless time (exponential), a sum of times (gamma), a proportion (beta). And watch out for rate versus mean.

Next comes seeing why all this is true in the topic’s proofs. Then, in the R practice, we simulate the microwave queue and check the formulas with thousands of times. And to check you have got it, the continuous random variables exercises have exam-style exercises with commented solutions. If you are coming from the previous topic and want to compare, discrete random variables have the same structure with sums. In topic 5 comes the most famous distribution of all: the normal.

Next step: proofsContinuous 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)

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