Transformations and joint distributions exercises — exam-style with R, a triangle, a joint mgf and a Jacobian (and the zero covariance that cost me a part)

In a practice exam in class I wrote in big letters that two variables were independent because their covariance was 0. It was the last part: I computed Cov(X,Y) \mathrm{Cov}(X, Y) , got 0 and concluded “so they are independent”. One of the variables was the square of the other. The lecturer gave me zero for that part and, next to it, a drawing: a parabola. If one is the square of the other, knowing one you know the other; it’s just that the relationship isn’t a straight line, and covariance only sees straight lines.

These exercises follow the format of the ME1 EC2: one context per exercise, chained parts with their marks, calculations by hand and calculations with R. They are original, with the restaurant and the delivery from the transformations and joint distributions theory, and they go over what comes up most in the topic: the change of variable (and when it doesn’t work), a joint density on a triangle with its constant, marginals, conditionals and covariance, a joint mgf and a Jacobian. If a proof gets stuck, you have them step by step in the topic proofs.

How to use this article

  • Do the exercises with the exam in mind. In the ME1 EC2 you have 4 hours for 50 marks, with RStudio and the data open. These three exercises add up to 40 marks plus a 2.5 bonus: give them about 3 hours and 10 minutes.
  • Paper for what is proved, R for what is computed. Results with 2 decimals, as the exam asks. Whatever you do in R, keep it in a script: at the end of the exam you have to upload the .R file with the code for each exercise.
  • Draw the support before every double integral. Thirty seconds with a pencil give you the limits and tell you whether there can be independence.
  • Mark yourself with the criteria. Each solution comes with “How it is marked” with a split of the marks. It is a guide, mine: it is not the official marking scheme.
  • Finish with the cheat sheet and “For your script”. The cheat sheet only has results (it is what you can take, handwritten: two sheets, both sides, in the EC2) and “For your script” is the topic’s code ready for your .R file. If you want to revise with the computer first, the R practice has the topic’s three programs with comments.

Exercise 1: the speed score (10 marks)

The restaurant gives each order a “speed score” based on the kitchen time X X , in minutes, which follows an exp⁡(0.1) \exp(0.1) : the score is S=e−0.1X S = e^{-0.1X} (1 if the kitchen is instantaneous, close to 0 if it takes ages).

(a) Find the support and the density of S S with the change of variable theorem, justifying that it can be used. What known distribution is it? (3 marks)

(b) Write S S in terms of the distribution function F F of X X and explain why the result of (a) is not a coincidence. (2 marks)

(c) Let Y=X Y = \sqrt{X} . Get its density with the distribution function method and compute P(Y>4) P(Y > 4) . (2 marks)

(d) With R, simulate 10,000 kitchen times from runif (without rexp), compute S S and Y Y , check P(S≤0.3) P(S \le 0.3) and P(Y>4) P(Y > 4) and draw the histogram of Y Y with its density on top. (3 marks)

In (a), solve for x x in terms of s s and look at the sign of the derivative. In (d), the inverse of the exponential’s F F gives you the kitchen time from a uniform number.

Exercise 2: rush hour in half-hours (15 marks)

At rush hour, the restaurant measures times in half-hours. Let X X be the moment the order leaves the kitchen and Y Y the moment it arrives, both in half-hours from the order. Their joint density is

f(x,y)=K(x+y),0<x<y<1 f(x, y) = K(x + y), \qquad 0 < x < y < 1

and 0 elsewhere.

(a) Find K K so that f f is a density. (2 marks)

(b) Get the marginal densities fX(x) f_X(x) and fY(y) f_Y(y) . (3 marks)

(c) Compute the probability that the delivery lasts more than a quarter of an hour, P(Y−X>1/2) P(Y – X > 1/2) . (2 marks)

(d) Get the conditional density f(x∣y) f(x \mid y) and E(X∣Y=y) E(X \mid Y = y) . If the order arrived at 27 minutes (y=0.9 y = 0.9 ), when do you expect it left the kitchen? (3 marks)

(e) Compute Cov(X,Y) \mathrm{Cov}(X, Y) and the correlation coefficient. Are X X and Y Y independent? Justify it. (3 marks)

(f) With R, check the value of K K and the probability in (c) with double integrals. (2 marks)

Draw the triangle 0<x<y<1 0 < x < y < 1 . For fX f_X , y y goes from x x to 1; for fY f_Y , x x goes from 0 to y y . In (c), the region is the strip above the line y=x+1/2 y = x + 1/2 .

Exercise 3: the big order (15 marks)

Group orders take longer in the kitchen: the kitchen time is X∼gamma(2, 0.2) X \sim \text{gamma}(2,\ 0.2) minutes, and the delivery, R∼exp⁡(0.2) R \sim \exp(0.2) minutes, independent of the kitchen. Let T=X+R T = X + R be the total time.

(a) Write the joint density of (X,R) (X, R) and compute its joint moment generating function. (3 marks)

(b) Using the joint mgf, find the distribution of T T and compute with R the probability that the order takes more than 20 minutes. (3 marks)

(c) Compute Cov(X,T) \mathrm{Cov}(X, T) and the correlation coefficient between X X and T T . (2 marks)

(d) Let U=X/T U = X/T , the fraction of the time spent in the kitchen. Get the joint density of (T,U) (T, U) with its support and its marginals, and say whether T T and U U are independent. (5 marks)

(e) With R, simulate 10,000 big orders and check P(U>1/2) P(U > 1/2) , the correlation between T T and U U and the histogram of U U with its density. (2 marks)

In (b), T=X+R T = X + R means φT(t)=E(etX+tR)=φ(X,R)(t,t) \varphi_T(t) = E(e^{tX + tR}) = \varphi_{(X,R)}(t, t) . In (d), it is the theory’s order Jacobian with another density: x=ut x = ut , r=t(1−u) r = t(1 – u) , ∣J∣=t \lvert J \rvert = t .

Bonus (2.5 marks)

The error in the time the app estimates is E∼N(0,3) E \sim N(0, 3) minutes. Let W=E2 W = E^2 . Prove that Cov(E,W)=0 \mathrm{Cov}(E, W) = 0 (1 mark) and that, however, E E and W W are not independent (1.5 marks).

Solutions

Exercise 1

(a) X X takes values in (0,∞) (0, \infty) , so S=e−0.1X S = e^{-0.1X} takes values in (0,1) (0, 1) . The function g(x)=e−0.1x g(x) = e^{-0.1x} is differentiable and strictly decreasing (g′(x)=−0.1e−0.1x<0 g'(x) = -0.1e^{-0.1x} < 0 ): the theorem can be used. The inverse:

s=e−0.1x  ⟺  x=−10log⁡s,dxds=−10s s = e^{-0.1x} \iff x = -10\log s, \qquad \frac{dx}{ds} = -\frac{10}{s}
fS(s)=fX(−10log⁡s)⋅∣−10s∣=0.1 e−0.1(−10log⁡s)⋅10s=0.1 elog⁡s⋅10s=0.1⋅s⋅10s=1,0<s<1 f_S(s) = f_X(-10\log s)\cdot\left|-\frac{10}{s}\right| = 0.1\,e^{-0.1(-10\log s)}\cdot\frac{10}{s} = 0.1\,e^{\log s}\cdot\frac{10}{s} = 0.1\cdot s\cdot\frac{10}{s} = 1, \qquad 0 < s < 1

S∼U(0,1) S \sim U(0, 1) : the speed score is uniform.

Why the absolute value isn’t decoration. g g is decreasing, so dx/ds=−10/s dx/ds = -10/s is negative. Without the absolute value you’d get fS(s)=−1 f_S(s) = -1 : a negative “density”, which can’t be. And notice the order: first the support, then the calculation. Without knowing that S S lives in (0,1) (0, 1) , the 1 you get tells you nothing; with the support, it’s a uniform you recognise at once.

How it is marked: support (0.5), justifying that g g is monotonic and differentiable (0.5), inverse and derivative (1), density with the absolute value (0.5) and identifying it (0.5).

(b) F(x)=1−e−0.1x F(x) = 1 – e^{-0.1x} , so S=e−0.1X=1−F(X) S = e^{-0.1X} = 1 – F(X) . By the probability integral transform, F(X)∼U(0,1) F(X) \sim U(0, 1) for any continuous variable, and if F(X) F(X) is uniform, so is 1−F(X) 1 – F(X) . The result of (a) is a particular case of F(X)∼U(0,1) F(X) \sim U(0, 1) .

Check you’ve got it: if the kitchen time were an exp⁡(0.2) \exp(0.2) and the speed score were still S=e−0.1X S = e^{-0.1X} , would it still be uniform?

No. Now 1−F(X)=e−0.2X 1 – F(X) = e^{-0.2X} , which isn’t the speed score, so the shortcut from (b) no longer works. With the distribution function method: P(S≤s)=P(X≥−10log⁡s)=e0.2⋅10log⁡s=s2 P(S \le s) = P(X \ge -10\log s) = e^{0.2\cdot 10\log s} = s^2 , and differentiating, fS(s)=2s f_S(s) = 2s on (0,1) (0, 1) : a beta(2,1) \text{beta}(2, 1) . For example, P(S≤0.3)=0.09 P(S \le 0.3) = 0.09 , not 0.30. The uniform only comes out when the speed score is exactly 1−F(X) 1 – F(X) .

How it is marked: writing S=1−F(X) S = 1 – F(X) (1) and quoting F(X)∼U(0,1) F(X) \sim U(0, 1) with the step to 1−F(X) 1 – F(X) (1).

(c) g(x)=x g(x) = \sqrt{x} is increasing on (0,∞) (0, \infty) , but the question asks for the F F method. For y>0 y > 0 :

FY(y)=P(X≤y)=P(X≤y2)=1−e−0.1y2,fY(y)=ddy(1−e−0.1y2)=0.2 y e−0.1y2,y>0 F_Y(y) = P(\sqrt{X} \le y) = P(X \le y^2) = 1 – e^{-0.1y^2}, \qquad f_Y(y) = \frac{d}{dy}\left(1 – e^{-0.1y^2}\right) = 0.2\,y\,e^{-0.1y^2}, \quad y > 0
P(Y>4)=1−FY(4)=e−0.1⋅16=e−1.6=0.20 P(Y > 4) = 1 – F_Y(4) = e^{-0.1\cdot 16} = e^{-1.6} = 0.20

Check without Y Y : X>4  ⟺  X>16 \sqrt{X} > 4 \iff X > 16 , and P(X>16)=e−1.6 P(X > 16) = e^{-1.6} .

Why the distribution function method doesn’t need monotonicity. The theorem needs a monotonic g g ; the distribution function method only needs you to translate the event {Y≤y} \{Y \le y\} into an event about X X , and that can always be done. The delicate step is X≤y  ⟺  X≤y2 \sqrt{X} \le y \iff X \le y^2 : it holds because both sides are positive, and that’s why we ask for y>0 y > 0 .

Writing P(X≤y) P(X \le \sqrt{y}) . That’s solving the wrong way round. Check it with a number: X≤4 \sqrt{X} \le 4 is X≤16 X \le 16 , not X≤2 X \le 2 .

How it is marked: FY F_Y (0.75), derivative (0.75) and probability (0.5).

(d)

set.seed(2026)
x <- -10 * log(runif(10000))    # the inverse of F: -10 log(1 - u), and 1 - U is also uniform
s <- exp(-0.1 * x)              # speed score
y <- sqrt(x)
round(c(mean(s <= 0.3), 0.3), 4)
round(c(mean(y > 4), exp(-1.6)), 4)
hist(y, freq = FALSE, breaks = 30, main = "Square root of the kitchen time")
curve(0.2 * x * exp(-0.1 * x^2), add = TRUE, col = "red", lwd = 2)

Output:

[1] 0.2997 0.3000
[1] 0.2007 0.2019
The square root of the kitchen time: histogram and densityDensity-scale histogram of 10,000 square roots of kitchen times, in 0.5 bars from 0 to 10: it rises from 0.05 to a maximum of 0.28 between 2 and 2.5 and falls to almost 0 at 8. The red curve 0.2 y times e to the minus 0.1 y squared follows it. A dashed coral line marks y equals 4: 20 percent lies to its right.0.00.10.20.30246810y = √xdensityP(Y > 4) = 0.200.2 * x * exp(-0.1 * x^2)
Redrawn with the site colours: the density from part (c) fits the simulation.

The proportion of scores below 0.3 is 0.30, that of a uniform, and that of roots greater than 4, 0.20, as in (c). In the drawing, the histogram of Y Y follows the curve 0.2 y e−0.1y2 0.2\,y\,e^{-0.1y^2} : it rises from 0, peaks near 5=2.24 \sqrt{5} = 2.24 and falls with a short tail.

How it is marked: simulating with runif and the inverse (1), the two checks (1) and the histogram with the curve (1).

Exercise 2

The plan, before the first integral. The whole exercise lives in the same triangle, 0<x<y<1 0 < x < y < 1 , so the first thing is to draw it: an order can’t arrive before it leaves the kitchen. Each part is a piece of that drawing: (a) the whole triangle, (b) a vertical slice for fX f_X and a horizontal one for fY f_Y , (c) the small triangle in the top left corner and (d) the slice at height y y .

Integrating over the square [0,1]2 [0, 1]^2 . You get K=1 K = 1 , a number so clean nobody suspects it, and everything else in the exercise ends up wrong by a factor of 2.

(a) The integral over the triangle must be 1. For each y y , x x goes from 0 to y y :

∫01 ⁣ ⁣∫0yK(x+y) dx dy=K∫01[x22+xy]0ydy=K∫013y22 dy=K⋅12=1  ⟺  K=2 \int_0^1\!\!\int_0^y K(x + y)\,dx\,dy = K\int_0^1\left[\frac{x^2}{2} + xy\right]_0^y dy = K\int_0^1 \frac{3y^2}{2}\,dy = K\cdot\frac{1}{2} = 1 \iff K = 2

How it is marked: the triangle’s limits (1) and the calculation (1).

(b)

fX(x)=∫x12(x+y) dy=[2xy+y2]x1=2x+1−2x2−x2=1+2x−3x2,0<x<1 f_X(x) = \int_x^1 2(x + y)\,dy = \Big[2xy + y^2\Big]_x^1 = 2x + 1 – 2x^2 – x^2 = 1 + 2x – 3x^2, \qquad 0 < x < 1
fY(y)=∫0y2(x+y) dx=[x2+2xy]0y=y2+2y2=3y2,0<y<1 f_Y(y) = \int_0^y 2(x + y)\,dx = \Big[x^2 + 2xy\Big]_0^y = y^2 + 2y^2 = 3y^2, \qquad 0 < y < 1

Check: ∫01(1+2x−3x2) dx=1+1−1=1 \int_0^1 (1 + 2x – 3x^2)\,dx = 1 + 1 – 1 = 1 and ∫013y2 dy=1 \int_0^1 3y^2\,dy = 1 . Y Y is a beta(3,1) \text{beta}(3, 1) .

How it is marked: 1.5 for each marginal (limits 0.5, calculation 0.75 and support 0.25).

(c) Y−X>1/2  ⟺  y>x+1/2 Y – X > 1/2 \iff y > x + 1/2 . That line only cuts the triangle if x<1/2 x < 1/2 , and for each x x , y y goes from x+1/2 x + 1/2 to 1:

P(Y−X>1/2)=∫01/2 ⁣ ⁣∫x+1/212(x+y) dy dx=∫01/2(34−3x2)dx=38−18=14 P(Y – X > 1/2) = \int_0^{1/2}\!\!\int_{x + 1/2}^1 2(x + y)\,dy\,dx = \int_0^{1/2}\left(\frac{3}{4} – 3x^2\right)dx = \frac{3}{8} – \frac{1}{8} = \frac{1}{4}

The inner integral: [2xy+y2]x+1/21=2x+1−2x(x+12)−(x+12)2=34−3x2 \big[2xy + y^2\big]_{x + 1/2}^1 = 2x + 1 – 2x(x + \tfrac{1}{2}) – (x + \tfrac{1}{2})^2 = \tfrac{3}{4} – 3x^2 . 25 % of the orders spend more than a quarter of an hour on delivery.

Why the outer integral only goes up to 1/2. The region is the small triangle with vertices (0,1/2) (0, 1/2) , (0,1) (0, 1) and (1/2,1) (1/2, 1) : if x>1/2 x > 1/2 , the line y=x+1/2 y = x + 1/2 goes above 1 and there’s no room left for y y . The obvious move would be to let x x run from 0 to 1, but then the inner integral would have its lower limit above its upper one and would subtract instead of add. With y y on the outside, from 1/2 to 1, and x x from 0 to y−1/2 y – 1/2 , you also get 1/4 1/4 : if the two orders don’t agree, one of the limits is wrong.

How it is marked: drawing or describing the region (0.5), limits (0.75) and calculation (0.75).

(d)

f(x∣y)=f(x,y)fY(y)=2(x+y)3y2,0<x<y f(x \mid y) = \frac{f(x, y)}{f_Y(y)} = \frac{2(x + y)}{3y^2}, \qquad 0 < x < y
E(X∣Y=y)=∫0yx 2(x+y)3y2 dx=23y2[x33+yx22]0y=23y2⋅5y36=5y9 E(X \mid Y = y) = \int_0^y x\,\frac{2(x + y)}{3y^2}\,dx = \frac{2}{3y^2}\left[\frac{x^3}{3} + \frac{yx^2}{2}\right]_0^y = \frac{2}{3y^2}\cdot\frac{5y^3}{6} = \frac{5y}{9}

With y=0.9 y = 0.9 : E(X∣Y=0.9)=0.5 E(X \mid Y = 0.9) = 0.5 half-hours, that is, at 15 minutes. More than half of 0.9: since the density grows with x x , late kitchen exits weigh more.

Why the triangle is sliced at height y y . Conditioning on Y=y Y = y means keeping the horizontal line at that height: on it, x x goes from 0 to y y , not from 0 to 1. Over that slice, f(x,y) f(x, y) doesn’t add up to 1, and that’s why you divide by fY(y) f_Y(y) : ∫0y2(x+y) dx=3y2 \int_0^y 2(x + y)\,dx = 3y^2 , exactly the value of fY(y) f_Y(y) .

Check you’ve got it: if the order arrived after 18 minutes, when do you expect it left the kitchen?

18 minutes is y=0.6 y = 0.6 half-hours. E(X∣Y=0.6)=5⋅0.6/9=0.33 E(X \mid Y = 0.6) = 5\cdot 0.6/9 = 0.33 half-hours, that is, after 10 minutes. The slice is shorter (from 0 to 0.6), but the proportion is the same: 5/9 5/9 of the way.

How it is marked: conditional with its support (1), integral (1.5) and value at 0.9 with its reading (0.5).

(e) With the marginals:

E(X)=∫01x(1+2x−3x2) dx=12+23−34=512E(Y)=∫013y3 dy=34 E(X) = \int_0^1 x(1 + 2x – 3x^2)\,dx = \frac{1}{2} + \frac{2}{3} – \frac{3}{4} = \frac{5}{12} \qquad E(Y) = \int_0^1 3y^3\,dy = \frac{3}{4}
E(XY)=∫01 ⁣ ⁣∫0y2xy(x+y) dx dy=2∫01(y43+y42)dy=2⋅56⋅15=13 E(XY) = \int_0^1\!\!\int_0^y 2xy(x + y)\,dx\,dy = 2\int_0^1\left(\frac{y^4}{3} + \frac{y^4}{2}\right)dy = 2\cdot\frac{5}{6}\cdot\frac{1}{5} = \frac{1}{3}
Cov(X,Y)=13−512⋅34=13−516=148=0.02 \mathrm{Cov}(X, Y) = \frac{1}{3} – \frac{5}{12}\cdot\frac{3}{4} = \frac{1}{3} – \frac{5}{16} = \frac{1}{48} = 0.02

For ρ \rho : E(X2)=13+12−35=730 E(X^2) = \frac{1}{3} + \frac{1}{2} – \frac{3}{5} = \frac{7}{30} , Var(X)=730−25144=43720=0.0597 \mathrm{Var}(X) = \frac{7}{30} – \frac{25}{144} = \frac{43}{720} = 0.0597 ; E(Y2)=35 E(Y^2) = \frac{3}{5} , Var(Y)=35−916=380=0.0375 \mathrm{Var}(Y) = \frac{3}{5} – \frac{9}{16} = \frac{3}{80} = 0.0375 .

ρ=1/480.0597⋅0.0375=0.44 \rho = \frac{1/48}{\sqrt{0.0597\cdot 0.0375}} = 0.44

They are not independent: the support is a triangle (if Y=0.2 Y = 0.2 , X X can’t be 0.5), or, with numbers, f(0.5, 0.2)=0 f(0.5,\ 0.2) = 0 and fX(0.5) fY(0.2)=1.25⋅0.12>0 f_X(0.5)\,f_Y(0.2) = 1.25\cdot 0.12 > 0 . The non-zero covariance also proves it (independent ⇒ covariance 0), but here it wasn’t needed.

Why look at the support before the covariance. If fixing y y changes the possible values of x x , they can’t be independent, with no calculation at all. The covariance only works one way: non-zero proves dependence, but zero proves nothing (that’s the bonus).

Computing E(XY) E(XY) as E(X) E(Y)=516 E(X)\,E(Y) = \frac{5}{16} . That already assumes they’re independent, and the covariance comes out 0 whatever you do. E(XY) E(XY) is integrated with the joint density, over the triangle.

How it is marked: E(X) E(X) , E(Y) E(Y) and E(XY) E(XY) (1.5), covariance and correlation (1) and the justification of dependence (0.5).

(f)

f <- function(x, y) (x + y) * (x < y)                    # without the K
inner <- function(x) integrate(function(y) f(x, y), x, 1)$value
1 / integrate(Vectorize(inner), 0, 1)$value             # K
g <- function(x) integrate(function(y) 2 * f(x, y), x + 0.5, 1)$value
integrate(Vectorize(g), 0, 0.5)$value                   # P(Y - X > 1/2)

Output:

[1] 2
[1] 0.25

2 and 0.25, as by hand. Look at the limits of the second one: the outer one only goes up to 0.5, because for x>0.5 x > 0.5 the strip falls outside the triangle.

How it is marked: each check with Vectorize and the triangle’s limits (1 + 1).

Exercise 3

(a) By independence, the joint density is the product of the marginals:

f(x,r)=0.22Γ(2) x e−0.2x⋅0.2 e−0.2r=0.008 x e−0.2(x+r),x>0, r>0 f(x, r) = \frac{0.2^2}{\Gamma(2)}\,x\,e^{-0.2x}\cdot 0.2\,e^{-0.2r} = 0.008\,x\,e^{-0.2(x + r)}, \qquad x > 0,\ r > 0

And the joint mgf, also by independence, is the product of the mgfs (topic 4):

φ(X,R)(s,t)=E(esX) E(etR)=(0.20.2−s)2⋅0.20.2−t,s<0.2, t<0.2 \varphi_{(X,R)}(s, t) = E\big(e^{sX}\big)\,E\big(e^{tR}\big) = \left(\frac{0.2}{0.2 – s}\right)^2\cdot\frac{0.2}{0.2 – t}, \qquad s < 0.2,\ t < 0.2

How it is marked: joint density with support (1) and mgf with the justification of independence (2).

(b) φT(t)=E(et(X+R))=φ(X,R)(t,t)=(0.20.2−t)3 \varphi_T(t) = E\big(e^{t(X + R)}\big) = \varphi_{(X,R)}(t, t) = \left(\frac{0.2}{0.2 – t}\right)^3 , the mgf of a gamma(3, 0.2) \text{gamma}(3,\ 0.2) . Since the mgf determines the distribution, T∼gamma(3, 0.2) T \sim \text{gamma}(3,\ 0.2) , with a mean of 15 minutes.

1 - pgamma(20, shape = 3, rate = 0.2)

Output:

[1] 0.2381033

0.24: almost one in four big orders takes more than 20 minutes.

Why the mgf and not an integral. The obvious route for a sum is the convolution, fT(t)=∫0tfX(x) fR(t−x) dx f_T(t) = \int_0^t f_X(x)\,f_R(t – x)\,dx . With the mgf everything is a product: both have the same base 0.2/(0.2−t) 0.2/(0.2 – t) , so the exponents add up, 2+1=3 2 + 1 = 3 . That works because the kitchen and the delivery have the same parameter 0.2: with an exp⁡(0.1) \exp(0.1) delivery, the product wouldn’t be the mgf of any gamma.

How it is marked: φT(t)=φ(t,t) \varphi_T(t) = \varphi(t, t) (1), identifying the gamma (1) and the probability (1).

(c) With the covariance property and the independence of X X and R R :

Cov(X,T)=Cov(X,X+R)=Var(X)+Cov(X,R)=20.22+0=50 \mathrm{Cov}(X, T) = \mathrm{Cov}(X, X + R) = \mathrm{Var}(X) + \mathrm{Cov}(X, R) = \frac{2}{0.2^2} + 0 = 50
Var(T)=30.22=75,ρX,T=5050⋅75=23=0.82 \mathrm{Var}(T) = \frac{3}{0.2^2} = 75, \qquad \rho_{X,T} = \frac{50}{\sqrt{50\cdot 75}} = \sqrt{\frac{2}{3}} = 0.82

How it is marked: covariance (1) and correlation (1).

(d) Support: t>0 t > 0 and 0<u<1 0 < u < 1 . Inverse: x=ut x = ut , r=t(1−u) r = t(1 – u) , with J=det⁡(ut1−u−t)=−t J = \det\begin{pmatrix} u & t \\ 1 – u & -t \end{pmatrix} = -t and ∣J∣=t \lvert J \rvert = t .

f(t,u)=0.008 (ut) e−0.2(ut+t−ut)⋅t=0.008 u t2 e−0.2t=[0.004 t2e−0.2t]⋅[2u] f(t, u) = 0.008\,(ut)\,e^{-0.2(ut + t – ut)}\cdot t = 0.008\,u\,t^2\,e^{-0.2t} = \Big[0.004\,t^2e^{-0.2t}\Big]\cdot\Big[2u\Big]

The first bracket is the density of a gamma(3, 0.2) \text{gamma}(3,\ 0.2) (0.23/Γ(3)=0.004 0.2^3/\Gamma(3) = 0.004 ) and the second, that of a beta(2,1) \text{beta}(2, 1) .

fT(t)=∫010.008 u t2e−0.2t du=0.004 t2e−0.2t, t>0fU(u)=∫0∞0.008 u t2e−0.2t dt=2u, 0<u<1 f_T(t) = \int_0^1 0.008\,u\,t^2e^{-0.2t}\,du = 0.004\,t^2e^{-0.2t}, \ t > 0 \qquad f_U(u) = \int_0^\infty 0.008\,u\,t^2e^{-0.2t}\,dt = 2u, \ 0 < u < 1

(The second integral: 0.008 u⋅Γ(3)0.23=0.008 u⋅250=2u 0.008\,u\cdot\frac{\Gamma(3)}{0.2^3} = 0.008\,u\cdot 250 = 2u .) T∼gamma(3, 0.2) T \sim \text{gamma}(3,\ 0.2) , U∼beta(2,1) U \sim \text{beta}(2, 1) , and since f(t,u)=fT(t) fU(u) f(t, u) = f_T(t)\,f_U(u) on a rectangular support, they are independent. In the big order, the kitchen takes on average E(U)=2/3 E(U) = 2/3 of the time, and that doesn’t depend on whether the order took long or not.

The plan for (d), before the Jacobian. Four steps, and each one earns marks: the new support, the inverse, the Jacobian and substituting. The inverse comes from reading the definitions backwards: if U=X/T U = X/T , the kitchen is x=ut x = ut and the delivery is what’s left, r=t−ut r = t – ut . When you substitute, the exponential becomes e−0.2t e^{-0.2t} because x+r=t x + r = t : that’s where u u disappears, and that’s why the density splits into a piece in t t times a piece in u u .

Check you’ve got it: why is the support of (T,U) (T, U) a rectangle here when in exercise 2 it was a triangle?

Because any pair works: for any t>0 t > 0 and any u u in (0,1) (0, 1) , x=ut x = ut and r=t(1−u) r = t(1 – u) are positive, so it’s a possible order. In exercise 2, knowing y y limits x x : if y=0.2 y = 0.2 , x x can’t go above 0.2. Factorising the density isn’t enough: you also need a rectangular support.

How it is marked: support (0.5), inverse and Jacobian (1.5), joint density (1), marginals (1.5) and independence (0.5).

(e)

set.seed(2026)
x <- rgamma(10000, 2, 0.2); r <- rexp(10000, 0.2)
t <- x + r; u <- x / t
round(c(mean(u > 0.5), 1 - 0.5^2), 4)          # P(U > 1/2) = 1 - F(1/2) = 3/4
round(cor(t, u), 3)
hist(u, freq = FALSE, breaks = 20, main = "Kitchen fraction")
curve(2 * x, from = 0, to = 1, add = TRUE, col = "red", lwd = 2)

Output:

[1] 0.7543 0.7500
[1] 0.007
The kitchen fraction of the big order: a beta(2, 1)Density-scale histogram of 10,000 kitchen fractions, in 0.05 bars from 0 to 1: they rise almost in a straight line from 0.05 to 2. The red line 2 u, from 0 to 2, follows them. A dashed coral line at u equals 0.5: 75 percent lies to its right.0.00.51.01.52.00.00.20.40.60.81.0u (kitchen fraction)density2 * xP(U > 1/2) = 0.75
Redrawn with the site colours: the density 2u from part (d), checked with 10,000 orders.

0.75 simulated (0.7543) against 1−(1/2)2=0.75 1 – (1/2)^2 = 0.75 theoretical (the F F of the beta(2, 1) is u2 u^2 ), and a correlation of practically 0. In the drawing, the histogram rises in a straight line following 2u 2u : high kitchen fractions are more frequent than low ones.

How it is marked: the simulation with P(U>1/2) P(U > 1/2) and the correlation (1) and the histogram with the line (1).

Bonus

E(E)=0 E(E) = 0 and, since the density of E E is symmetric about 0, E(E3)=0 E(E^3) = 0 (the integral of an odd function over the whole line). Then

Cov(E,W)=E(E⋅E2)−E(E) E(E2)=E(E3)−0⋅9=0 \mathrm{Cov}(E, W) = E(E\cdot E^2) – E(E)\,E(E^2) = E(E^3) – 0\cdot 9 = 0

For dependence, one event where the joint distribution doesn’t factorise is enough. If ∣E∣≤3 |E| \le 3 , then W=E2≤9 W = E^2 \le 9 , so

P(∣E∣≤3, W>9)=0butP(∣E∣≤3) P(W>9)=0.68⋅0.32>0 P(|E| \le 3,\ W > 9) = 0 \qquad\text{but}\qquad P(|E| \le 3)\,P(W > 9) = 0.68\cdot 0.32 > 0

(P(∣E∣≤3)=P(∣Z∣≤1)=0.68 P(|E| \le 3) = P(|Z| \le 1) = 0.68 .) They are not independent: zero covariance does not imply independence. It is my anecdote from the beginning.

How it is marked: E(E3)=0 E(E^3) = 0 by symmetry (1) and an event that doesn’t factorise, with numbers (1.5).

What’s wrong here?

Three classmates’ solutions with one mistake each. Before reading the explanation, look for the slip.

Fragment 1. “In exercise 2, the marginal of Y Y is fY(y)=∫012(x+y) dx=1+2y f_Y(y) = \int_0^1 2(x + y)\,dx = 1 + 2y , for 0<y<1 0 < y < 1 .”

integrate(function(y) 1 + 2 * y, 0, 1)

Output:

2 with absolute error < 2.2e-14

It integrates to 2: it isn’t a density. The limits are those of the square [0,1]2 [0, 1]^2 , not of the triangle. For a fixed y y , x x only goes up to y y : the right marginal is 3y2 3y^2 .

The marginal of Y: the horizontal line leaves the triangle at x = yThe triangle of x less than y between 0 and 1, in violet, with a horizontal line at y equals 0.6: the green part, from x equals 0 to 0.6, is inside; the dashed red part, from 0.6 to 1, is outside. On the right, the correct marginal, 3 y squared, which integrates to 1, and the wrong one, 1 plus 2 y, which integrates to 2.0.00.51.00.00.51.0xyx from 0 to youtside0 < x < y < 1Right:∫₀ʸ 2(x + y) dx = 3y²integrates to 1Wrong:∫₀¹ 2(x + y) dx = 1 + 2yintegrates to 2
Integrating up to x = 1 adds a part that is not in the support.

Integrate your marginal before going on. If it doesn’t give 1, the mistake is in the limits, and in the drawing you see it straight away: the horizontal line at height y y leaves the triangle at x=y x = y , not at x=1 x = 1 .

Fragment 2. “In the order, X X is the kitchen and Y=X+R Y = X + R the arrival, with Var(X)=100 \mathrm{Var}(X) = 100 and Var(Y)=200 \mathrm{Var}(Y) = 200 . Then Var(X+Y)=100+200=300 \mathrm{Var}(X + Y) = 100 + 200 = 300 .”

set.seed(2026)
x <- rexp(1e5, 0.1); y <- x + rexp(1e5, 0.1)
round(c(var(x + y), var(x) + var(y), var(x) + var(y) + 2 * cov(x, y)), 1)

Output:

[1] 499.8 300.3 499.8

The real variance of X+Y X + Y is about 500, not 300. Adding variances only works with independent (or uncorrelated) variables, and X X and Y Y have covariance 100: 2 Cov(X,Y)=200 2\,\mathrm{Cov}(X, Y) = 200 is missing.

“the variance of a sum is the sum of the variances” without checking whether they are independent. When one variable is built from the other (like the arrival from the kitchen), they never are.

Fragment 3. “If X∼U(−1,1) X \sim U(-1, 1) and Y=X2 Y = X^2 , with x=y x = \sqrt{y} and dxdy=12y \frac{dx}{dy} = \frac{1}{2\sqrt{y}} , by the change of variable theorem fY(y)=12⋅12y=14y f_Y(y) = \frac{1}{2}\cdot\frac{1}{2\sqrt{y}} = \frac{1}{4\sqrt{y}} , 0<y<1 0 < y < 1 .”

integrate(function(y) 1 / (4 * sqrt(y)), 0, 1)

Output:

0.5 with absolute error < 1.4e-15

It integrates to 0.5: half is missing. x2 x^2 isn’t monotonic on (−1,1) (-1, 1) and the theorem can’t be used. With the distribution function, FY(y)=P(−y≤X≤y)=y F_Y(y) = P(-\sqrt{y} \le X \le \sqrt{y}) = \sqrt{y} , and fY(y)=12y f_Y(y) = \frac{1}{2\sqrt{y}} : double, because each y y comes from y \sqrt{y} and from −y -\sqrt{y} .

Before the theorem, one question: does each y y come from a single x x ? If it comes from two, use the F F method.

Practice quiz

Before moving on to the next topic, check you have got it. This quiz is here to consolidate what this article explains: the ME1 exam is not a multiple-choice test (you solve problems by hand and with R), so no mark here counts. There are 10 questions on transformations and joint distributions: key ideas, typical mistakes and many «what does this R code print?», with the outputs from really running it. In the short answers, a decimal point or a decimal comma both work. Answer without running anything, press Check and read the explanation for each option; you can repeat it as many times as you like.

Question 1Single choice

The joint density is f(x, y) = 2(x + y) on the triangle 0 < x < y < 1. A classmate works out Y’s marginal by integrating x from 0 to 1 and gets 1 + 2y, which integrates to 2. What did they do wrong?

  1. They used the square’s limits: for a fixed y, x only goes up to y
  2. They should have integrated y instead of x
  3. A joint density cannot be integrated
  4. Integrating to 2 is normal for a marginal
Solution

Answer: They used the square’s limits: for a fixed y, x only goes up to y

Integrate your marginal before moving on. If it does not give 1, the mistake is in the limits, and the support drawing shows it at once.

Question 2Single choice

X is the cooking time and Y = X + R the order’s arrival time. With Var(X) = 100 and Var(Y) = 200, a classmate says Var(X + Y) = 300. What is wrong?

  1. X and Y are not independent (Y is built from X): 2·Cov(X, Y) = 200 is missing, and the real variance is about 500
  2. Nothing: the variance of a sum is always the sum of the variances
  3. They should subtract the covariance
  4. Var(X + Y) = Var(X) · Var(Y)
Solution

Answer: X and Y are not independent (Y is built from X): 2·Cov(X, Y) = 200 is missing, and the real variance is about 500

Var(X + Y) = Var(X) + Var(Y) + 2·Cov(X, Y). The article’s simulation gives 499.8 against 300.

Question 3Single choice

X ~ U(−1, 1) and Y = X². A classmate applies the change-of-variable theorem with x = √y and gets f_Y(y) = 1/(4√y), which integrates to 0.5. Why?

  1. x² is not monotonic on (−1, 1): two values of x (√y and −√y) reach each y, and the theorem does not apply
  2. They differentiated √y wrongly
  3. The uniform on (−1, 1) has density 1, not 1/2
  4. Y = X² has no density
Solution

Answer: x² is not monotonic on (−1, 1): two values of x (√y and −√y) reach each y, and the theorem does not apply

Before the theorem, one question: does a single x reach each y? If two do, use the distribution-function method.

Question 4Short answer

What number does it print? (Ignore the second part of integrate‘s output: write just the number.)

integrate(function(y) 1 / (2 * sqrt(y)), 0, 1)$value
Solution

Answer: 1

f_Y(y) = 1/(2√y) on (0, 1) is a density: it integrates to 1. 1/(4√y) integrated to 0.5, the clue that half was missing.

Question 5Short answer

With E(X) = 0.4167, E(Y) = 0.75 and E(XY) = 0.3333, what is Cov(X, Y)? (4 decimals.)

round(0.3333 - 0.4167 * 0.75, 4)
Solution

Answer: 0.0208

Cov(X, Y) = E(XY) − E(X)E(Y) = 0.3333 − 0.3125 = 0.0208. Positive: when X is large, Y tends to be large too.

Question 6Single choice

You get Cov(X, Y) = 0. Can you claim that X and Y are independent?

  1. No: covariance 0 only says they are uncorrelated; independence is proved with the joint density (or the mgf)
  2. Yes: covariance 0 and independence are the same thing
  3. Yes, if the means are also positive
  4. No, because the covariance can never be 0
Solution

Answer: No: covariance 0 only says they are uncorrelated; independence is proved with the joint density (or the mgf)

It is the article’s anecdote («the zero covariance that cost me a part») and the fourth question of «Before handing in».

Question 7Match

Match each step with how it is done, for a joint density f(x, y):

Marginal of X
Conditional of Y given X = x
Covariance
Correlation

Definitions:

  • Cov(X, Y) / (σ_X σ_Y)
  • f(x, y) / f_X(x)
  • E(XY) − E(X)E(Y)
  • f_X(x) + f_Y(y)
  • Integrate f over y, with the support's limits for each x
Solution
  • Marginal of X → Integrate f over y, with the support’s limits for each x
  • Conditional of Y given X = x → f(x, y) / f_X(x)
  • Covariance → E(XY) − E(X)E(Y)
  • Correlation → Cov(X, Y) / (σ_X σ_Y)

First of all, draw the support: the limits of every integral come from it.

Question 8True or false

True or false?

  1. If g is monotonic, f_Y(y) = f_X(g⁻¹(y)) · |(g⁻¹)'(y)|.
  2. Every density given in the exam comes with its support.
  3. E(XY) = E(X)E(Y) for any pair of variables.
  4. The correlation is always between −1 and 1.
Solution
  • T — If g is monotonic, f_Y(y) = f_X(g⁻¹(y)) · |(g⁻¹)'(y)|.
  • T — Every density given in the exam comes with its support.
  • F — E(XY) = E(X)E(Y) for any pair of variables.
  • T — The correlation is always between −1 and 1.

E(XY) = E(X)E(Y) holds for independent (or uncorrelated) variables; in general E(XY) must be computed from the joint density.

Question 9Short answer

A discrete variable takes values with probabilities 0.4, 0.4 and 0.2. What does it print?

sum(c(0.4, 0.4, 0.2))
Solution

Answer: 1

It is the marginal pX from the dishes-and-drinks table: adding up the joint’s rows gives the marginal, and a marginal always adds up to 1. It is the first check after getting it.

Question 10Order

Put in order the steps to get the density of Y = g(X) when g is not monotonic:

  1. Check that f_Y integrates to 1
  2. Write F_Y(y) = P(g(X) ≤ y)
  3. Rewrite the event in terms of X (for example, −√y ≤ X ≤ √y)
  4. Differentiate F_Y to get f_Y and write its support
  5. Work out that probability with X’s distribution
Solution

Correct order:

  1. Write F_Y(y) = P(g(X) ≤ y)
  2. Rewrite the event in terms of X (for example, −√y ≤ X ≤ √y)
  3. Work out that probability with X’s distribution
  4. Differentiate F_Y to get f_Y and write its support
  5. Check that f_Y integrates to 1

It is the distribution-function method, the one that always works. The final check with integrate catches limit mistakes.

Try it in the R playground

Open the site’s R playground (or RStudio) and paste this script. It is the whole of exercise 2, plus a simulation of the triangle that isn’t in the solutions:

f <- function(x, y) 2 * (x + y) * (x < y)
double_int <- function(g) integrate(Vectorize(function(x)
  integrate(function(y) g(x, y) * f(x, y), x, 1)$value), 0, 1)$value
EX <- double_int(function(x, y) x); EY <- double_int(function(x, y) y); EXY <- double_int(function(x, y) x * y)
VX <- double_int(function(x, y) x^2) - EX^2; VY <- double_int(function(x, y) y^2) - EY^2
round(c(EX = EX, EY = EY, cov = EXY - EX * EY, rho = (EXY - EX * EY) / sqrt(VX * VY)), 4)
# Simulating the triangle by acceptance: random points in the square, keep some of them
set.seed(1); n <- 2e5
x <- runif(n); y <- runif(n); w <- runif(n, 0, 4)       # 4 is the maximum of f
accept <- w < f(x, y)
xs <- x[accept]; ys <- y[accept]
round(c(mean(xs), mean(ys), cor(xs, ys), mean(ys - xs > 0.5)), 3)
plot(xs[1:500], ys[1:500], pch = 20); abline(0, 1, col = "red")

What to look at:

  1. The moments in the first line are those of solution (e): 5/12 5/12 , 3/4 3/4 , 1/48 1/48 and 0.44.
  2. The acceptance simulation gives almost the same without a single integral: it is another way of checking a result when you don’t trust the limits.
  3. In the cloud, the points pile up at the top right, where x+y x + y is large and the density, high. None is below the diagonal.

Cheat sheet: transformations and joint distributions

The essentials in 30 seconds. One variable: if g g is monotonic, fY(y)=fX(g−1(y)) ∣(g−1)′(y)∣ f_Y(y) = f_X(g^{-1}(y))\,\lvert (g^{-1})'(y) \rvert ; if not, FY F_Y and differentiate. Two variables: draw the support, integrate out the extra one for the marginal, divide for the conditional, and covariance =E(XY)−E(X)E(Y) = E(XY) – E(X)E(Y) .

I want…Formula
Density of g(X) g(X) , g g monotonicfX(g−1(y)) ∣ddyg−1(y)∣ f_X(g^{-1}(y))\,\lvert \frac{d}{dy}g^{-1}(y) \rvert
Density of g(X) g(X) , g g not monotonicFY(y)=P(g(X)≤y) F_Y(y) = P(g(X) \le y) , differentiate
Z2 Z^2 , with Z∼N(0,1) Z \sim N(0, 1) χ2(1)=gamma(1/2, 1/2) \chi^2(1) = \text{gamma}(1/2,\ 1/2)
Uniforms and any F F F(X)∼U(0,1) F(X) \sim U(0, 1) ; F−1(U)∼F F^{-1}(U) \sim F
MarginalfX(x)=∫f(x,y) dy f_X(x) = \int f(x, y)\,dy (support’s limits)
Conditionalf(x∣y)=f(x,y)/fY(y) f(x \mid y) = f(x, y)/f_Y(y)
Law of total expectationE(Y)=E(E(Y∣X)) E(Y) = E(E(Y \mid X))
Law of total varianceVar(Y)=E(Var(Y∣X))+Var(E(Y∣X)) \mathrm{Var}(Y) = E(\mathrm{Var}(Y \mid X)) + \mathrm{Var}(E(Y \mid X))
Independencef(x,y)=fX(x)fY(y) f(x, y) = f_X(x)f_Y(y) at every point (rectangular support)
E E of a sumE(aX+bY)=aE(X)+bE(Y) E(aX + bY) = aE(X) + bE(Y) , always
E E of a productE(XY)=E(X)E(Y) E(XY) = E(X)E(Y) if they are independent
CovarianceCov=E(XY)−E(X)E(Y) \mathrm{Cov} = E(XY) – E(X)E(Y) ; Cov(aX+b,cY+d)=ac Cov \mathrm{Cov}(aX + b, cY + d) = ac\,\mathrm{Cov}
Correlationρ=Cov/(σXσY) \rho = \mathrm{Cov}/(\sigma_X\sigma_Y) , −1≤ρ≤1 -1 \le \rho \le 1
Variance of aX+bY aX + bY a2σX2+b2σY2+2ab Cov(X,Y) a^2\sigma_X^2 + b^2\sigma_Y^2 + 2ab\,\mathrm{Cov}(X, Y)
Joint mgfφ(s,t)=E(esX+tY) \varphi(s, t) = E(e^{sX + tY}) ; φ(s,0)=φX(s) \varphi(s, 0) = \varphi_X(s) ; ∂st2φ(0,0)=E(XY) \partial^2_{st}\varphi(0, 0) = E(XY)
mgf of X+Y X + Y φX+Y(t)=φ(t,t) \varphi_{X+Y}(t) = \varphi(t, t) ; with independence, φX(t)φY(t) \varphi_X(t)\varphi_Y(t)
Sum of gammas, same β \beta gamma(α1+α2,β) \text{gamma}(\alpha_1 + \alpha_2, \beta)
JacobianfUV(u,v)=fXY(x(u,v),y(u,v)) ∣J∣ f_{UV}(u, v) = f_{XY}(x(u, v), y(u, v))\,\lvert J \rvert , J J of the inverse
X/(X+R) X/(X + R) with independent gammas, same β \beta beta(α1,α2) \text{beta}(\alpha_1, \alpha_2) , independent of X+R X + R
Bivariate normal: marginalN(μX,σX) N(\mu_X, \sigma_X)
Bivariate normal: conditionalY∣X=x∼N(μY+ρσYσX(x−μX), σY1−ρ2) Y \mid X = x \sim N\big(\mu_Y + \rho\frac{\sigma_Y}{\sigma_X}(x – \mu_X),\ \sigma_Y\sqrt{1 – \rho^2}\big)
SymptomLikely cause
The marginal doesn’t integrate to 1Rectangle limits on a triangular support
The density of X2 X^2 integrates to 0.5Only one root: g g wasn’t monotonic
Negative densityMissing absolute value of the Jacobian or the derivative
∣ρ∣>1 \lvert\rho\rvert > 1 Mistake in E(XY) E(XY) or in a variance
length(lower) == 1 is not TRUE or result of wrong lengthMissing Vectorize in the outer integral

Before handing in, ask yourself:

  1. Have I written the support of every density I have given?
  2. Does every density I got integrate to 1?
  3. If I used E(XY)=E(X)E(Y) E(XY) = E(X)E(Y) or added variances, were they independent?
  4. If I say “independent”, have I proved it with the joint density or the mgf, and not with the covariance?
ME1 exam coming up?The script for every topic, the printable cheat sheet and the pack for each exam, for free.See the pack

For your script

The topic’s code, commented and tested, to copy into your exam .R file. They are short functions: you give them the density or the data from the question and they return the result.

# ---------- Transformations and joint distributions (ME1, topic 6) ----------
# Double integral of f(x, y) with x between a and b, and y between ylo(x) and yhi(x) (limits depending on x)
# (don't call an argument c: you would hide R's c() function)
double_int <- function(f, a, b, ylo, yhi) integrate(Vectorize(function(x)
  integrate(function(y) f(x, y), ylo(x), yhi(x))$value), a, b)$value
# Constant K: f0 is the density WITHOUT the K
constant <- function(f0, a, b, ylo, yhi) 1 / double_int(f0, a, b, ylo, yhi)
# Marginals at a point (support's limits, as functions)
marg_x <- function(f, x, ylo, yhi) integrate(function(y) f(x, y), ylo(x), yhi(x))$value
marg_y <- function(f, y, xlo, xhi) integrate(function(x) f(x, y), xlo(y), xhi(y))$value
# Moments, covariance and correlation from the joint density
moments <- function(f, a, b, ylo, yhi) {
  E <- function(g) double_int(function(x, y) g(x, y) * f(x, y), a, b, ylo, yhi)
  EX <- E(function(x, y) x); EY <- E(function(x, y) y); EXY <- E(function(x, y) x * y)
  VX <- E(function(x, y) x^2) - EX^2; VY <- E(function(x, y) y^2) - EY^2
  round(c(EX = EX, EY = EY, EXY = EXY, cov = EXY - EX * EY, rho = (EXY - EX * EY) / sqrt(VX * VY)), 4)
}
# Discrete joint table: marginals, covariance, correlation and independence
joint_table <- function(p, xv, yv) {
  pX <- rowSums(p); pY <- colSums(p)
  EX <- sum(xv * pX); EY <- sum(yv * pY); EXY <- sum(outer(xv, yv) * p)
  VX <- sum(xv^2 * pX) - EX^2; VY <- sum(yv^2 * pY) - EY^2
  list(pX = pX, pY = pY, cov = EXY - EX * EY, rho = (EXY - EX * EY) / sqrt(VX * VY),
       independent = isTRUE(all.equal(c(p), c(outer(pX, pY)))))
}
# Check a transformation by simulation: histogram of g(x) with the proposed density
check_change <- function(x, g, fY, ...) {
  y <- g(x); hist(y, freq = FALSE, breaks = 40, main = "Change of variable", ...)
  curve(fY(x), add = TRUE, col = "red", lwd = 2, n = 400)
}
# Bivariate normal: density and conditional Y | X = x
dbiv <- function(x, y, mx, my, sx, sy, rho) {
  q <- ((x - mx) / sx)^2 + ((y - my) / sy)^2 - 2 * rho * (x - mx) * (y - my) / (sx * sy)
  exp(-q / (2 * (1 - rho^2))) / (2 * pi * sx * sy * sqrt(1 - rho^2))
}
cond_biv <- function(x, mx, my, sx, sy, rho) c(mean = my + rho * sy / sx * (x - mx), sd = sy * sqrt(1 - rho^2))

# ---------- Examples (the exercises in this article) ----------
f2 <- function(x, y) 2 * (x + y) * (x < y)
one <- function(x) 1; zero <- function(x) 0; id <- function(x) x
round(constant(function(x, y) (x + y) * (x < y), 0, 1, id, one), 2)     # ex. 2 (a): K = 2
round(double_int(f2, 0, 0.5, function(x) x + 0.5, one), 2)              # ex. 2 (c): 0.25
moments(f2, 0, 1, id, one)                                               # ex. 2 (e)
round(1 - pgamma(20, 3, 0.2), 2)                                         # ex. 3 (b)
p <- matrix(c(0.20, 0.15, 0.05, 0.10, 0.20, 0.10, 0.02, 0.08, 0.10), 3, byrow = TRUE)
str(joint_table(p, 1:3, 0:2))                                            # dishes and drinks
round(cond_biv(5, 3, 15, 1, 4, 0.8), 2)                                  # R | D = 5

Output:

[1] 2
[1] 0.25
    EX     EY    EXY    cov    rho 
0.4167 0.7500 0.3333 0.0208 0.4402 
[1] 0.24
List of 5
 $ pX         : num [1:3] 0.4 0.4 0.2
 $ pY         : num [1:3] 0.32 0.43 0.25
 $ cov        : num 0.216
 $ rho        : num 0.384
 $ independent: logi FALSE
mean   sd 
21.4  2.4 

double_int is the one that pays off most: you give it the density, the limits of x x and those of y y as functions of x x (id is y=x y = x , one is y=1 y = 1 ), and you don’t have to write the Vectorize in the exam. With it you get K=2 K = 2 , the 0.25 in (c) and the moments in (e) with the correlation 0.44. joint_table does the whole discrete part at once (the dishes and drinks table gives covariance 0.216, correlation 0.38 and independent = FALSE), and cond_biv returns the mean and standard deviation of the conditional of the bivariate normal: 21.4 and 2.4.

What comes next?

With two variables at once you can already work with a sum of many. The next ME1 topic is inequalities and limit theorems: Chebyshev’s inequality, the law of large numbers and the central limit theorem, which explain why the mean of many orders looks like a normal even though each time is exponential.

If you want to revise first, you have the theory, the proofs and the practice for this topic, and the normal distribution, where the bivariate normal and the χ2 \chi^2 come from.

Keep goingAll Statistics articles

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

Ready for the exam?ME1 EC2 mock exam — 4 hours with R beside you, from the normal to confidence intervals, four versions and a mark out of 50 (and why exercise 1 is worth double)