Transformations and joint distributions — theory with a food delivery order, marginals, covariance and the Jacobian (and the 60 % that was 80 %)

One Friday night, waiting for some pizzas at the flat, I assured my flatmate that only six out of ten orders arrive on time. The app promised 30 minutes; I had assumed that the kitchen and the delivery take 10 minutes each on average, independently, and I had computed the probability that the kitchen took under 15 minutes and the delivery under 15: (1 - exp(-1.5))^2 gave 0.60. She had passed ME1 in June, and she took my phone: “Arriving on time means the sum does not go over 30. If the kitchen takes 5, the delivery can take 25.” With the right region, pgamma(30, 2, 0.1), you get 0.80. My mistake was not in any formula: it was in the drawing of the region.

This topic is about two things that are used together throughout the EC2. The first, transforming a variable: if you know the distribution of X X , what is the distribution of Y=g(X) Y = g(X) ? The second, working with two variables at once: the joint distribution, the marginals, the conditionals, independence, covariance and correlation, and how to change variables with a Jacobian. We pick up where we left off in the normal distribution and in continuous random variables: the densities, the exponential, the gamma and the mgf are the same, but now they come in pairs. The proofs, step by step, are in the transformations and joint distributions proofs.

Before you start. For this topic you need what you saw in continuous random variables: that probability is an area under the density, that the distribution function F(x) F(x) is the area up to x x and that differentiating F F gives the density. And, from calculus, integrating exponentials, the chain rule and a 2 × 2 determinant. The topic has two halves. In the first, a single variable: if you know V V , what R=180/V R = 180/V looks like, by two routes (the distribution function and the change of variable theorem). In the second, two variables at once: the joint distribution and, from it, marginals (sum or integrate out the other one), conditionals (slice and rescale), independence (does it factorise?), covariance and correlation (do they move together?) and the Jacobian, which is the change of variable with two variables. We use the same order from start to finish: kitchen X X , delivery R R and arrival Y=X+R Y = X + R . If you get lost, draw the support: almost every mistake in this topic shows up in that drawing.

The order that comes with us

You order dinner through an app from a restaurant near campus. From the moment you tap “Order”, two things happen, one after the other:

  • the kitchen prepares the order: it takes X X minutes;
  • the rider brings it to your door: it takes R R minutes.

The model for the whole topic: X X and R R are independent and both follow an exp⁡(0.1) \exp(0.1) , that is, they take 10 minutes on average each (it is a simple model with made-up data, but it has the shape of waiting times: many short ones and the odd long one). With them we will build the variables you really care about:

  • Y=X+R Y = X + R , the minute the order arrives at your door;
  • U=X/(X+R) U = X / (X + R) , what part of the wait is kitchen.

Y Y depends on X X : if the kitchen is slow, the order arrives late. Measuring that dependence is half of the topic. The other half is knowing what distribution Y Y or U U have without having to simulate.

Transforming one variable: Y = g(X)

The rider and their speed

We start with a single variable. The restaurant is 3 km away and the rider cycles at a speed that depends on the traffic: V∼U(12,20) V \sim U(12, 20) km/h. The delivery time, in minutes, is

R=3V⋅60=180V R = \frac{3}{V}\cdot 60 = \frac{180}{V}

You know the distribution of V V . What is the distribution of R R ? There are two routes and it pays to master both.

Route 1: through the distribution function

The distribution function method has three steps:

  1. Where the new variable lives. V V goes from 12 to 20, so R=180/V R = 180/V goes from 180/20=9 180/20 = 9 to 180/12=15 180/12 = 15 minutes.
  2. Its distribution function, writing the event in terms of the variable you know. For 9<r<15 9 < r < 15 :
FR(r)=P(R≤r)=P ⁣(180V≤r)=P ⁣(V≥180r)=1−FV ⁣(180r)=1−180/r−128 F_R(r) = P(R \le r) = P\!\left(\frac{180}{V} \le r\right) = P\!\left(V \ge \frac{180}{r}\right) = 1 – F_V\!\left(\frac{180}{r}\right) = 1 – \frac{180/r – 12}{8}
  1. Differentiate to get the density:
fR(r)=ddrFR(r)=18⋅180r2=22.5r2,9<r<15 f_R(r) = \frac{d}{dr}F_R(r) = \frac{1}{8}\cdot\frac{180}{r^2} = \frac{22.5}{r^2}, \qquad 9 < r < 15

Look at the third step of the chain: when you solve for V V in 180/V≤r 180/V \le r , the inequality flips, because 180/v 180/v is decreasing. It is where people slip most.

The distribution function method, step by step

The chain above does four things in one line. Here they are separated, because it is the same scheme you will repeat with any g g , also when it is not monotonic:

StepWhat you doWith the riderWhy
1Find the support of the new variable9<r<15 9 < r < 15 Take the endpoints of V V through the function: 180/20=9 180/20 = 9 and 180/12=15 180/12 = 15 . Outside that range, FR F_R is 0 (below) or 1 (above)
2Write FR(r)=P(R≤r) F_R(r) = P(R \le r) P(180/V≤r) P(180/V \le r) It is the definition: you are only naming what you are looking for
3Translate the event into the variable you knowP(V≥180/r) P(V \ge 180/r) You multiply by V V and divide by r r , both positive. The ≥ \ge appears because V V was in the denominator: 180/v 180/v is decreasing, and “short time” means “high speed”
4Use the F F you know1−FV(180/r)=1−180/r−128 1 – F_V(180/r) = 1 – \frac{180/r – 12}{8} P(V≥a)=1−FV(a) P(V \ge a) = 1 – F_V(a) , and the uniform has FV(v)=(v−12)/8 F_V(v) = (v – 12)/8
5Differentiate with respect to r r 18⋅180r2=22.5r2 \frac{1}{8}\cdot\frac{180}{r^2} = \frac{22.5}{r^2} The density is the derivative of F F . The derivative of −180/r -180/r is +180/r2 +180/r^2 : the minus sign goes away on its own

Quick check: at the endpoints of the support, FR(9)=1−20−128=0 F_R(9) = 1 – \frac{20 – 12}{8} = 0 and FR(15)=1−12−128=1 F_R(15) = 1 – \frac{12 – 12}{8} = 1 . If you don’t get 0 at one end and 1 at the other, the mistake is in step 3.

Check you’ve got it: what is FR(10) F_R(10) and what does it mean for the order?

FR(10)=1−18−128=1−0.75=0.25 F_R(10) = 1 – \frac{18 – 12}{8} = 1 – 0.75 = 0.25 : 25 % of deliveries take 10 minutes or less. You can check it by going back to the speed, as in step 3: R≤10  ⟺  180/V≤10  ⟺  V≥18 R \le 10 \iff 180/V \le 10 \iff V \ge 18 , and in a U(12,20) U(12, 20) that is (20−18)/8=2/8=0.25 (20 – 18)/8 = 2/8 = 0.25 . If you got 0.75, you didn’t flip the inequality.

Route 2: the change of variable theorem

If g g is differentiable and strictly monotonic (always increasing or always decreasing) on the support of X X , the density of Y=g(X) Y = g(X) comes out directly:

fY(y)=fX(g−1(y)) ∣ddy g−1(y)∣ f_Y(y) = f_X\big(g^{-1}(y)\big)\,\left|\frac{d}{dy}\,g^{-1}(y)\right|

With the rider: r=180/v  ⟺  v=180/r r = 180/v \iff v = 180/r , and dvdr=−180r2 \frac{dv}{dr} = -\frac{180}{r^2} . Since fV(v)=1/8 f_V(v) = 1/8 on the whole support:

fR(r)=18⋅∣−180r2∣=22.5r2,9<r<15 f_R(r) = \frac{1}{8}\cdot\left|-\frac{180}{r^2}\right| = \frac{22.5}{r^2}, \qquad 9 < r < 15

The same as by route 1. The absolute value does the job of “the inequality flips”: a density can never be negative.

From the rider speed to the delivery timeOn the left, the flat density of the speed, uniform between 12 and 20 km/h, split into four 25 percent bands. On the right, the delivery density, 22.5 over r squared between 9 and 15 minutes, with the same four bands: 18 to 20 km/h becomes the narrow strip from 9 to 10 minutes and 12 to 14 km/h the wide one from 12.86 to 15 minutes.0.00.10.20.31214161820speed V (km/h)density0.00.10.20.39111315delivery R (min)25 %25 %25 %25 %25 %25 %25 %25 %f_V = 1/8f_R = 22.5/r²R = 180/Vdecreasing
Each band keeps its 25 %, but it narrows or widens: that is why the delivery density is not flat.

In the drawing, the speed is flat (all equally likely) and the time is not: there are more orders near 9 minutes than near 15. The reason is in the curve in the middle: when the bike is fast, changing the speed by 1 km/h hardly changes the time, so a lot of probability piles up in a few minutes.

fR <- function(r) 22.5 / r^2          # the density we have found
integrate(fR, 9, 15)                   # it must give 1
ER <- integrate(function(r) r * fR(r), 9, 15)$value
c(ER, 180 / 16)                        # E(R) versus 180 / E(V)
1 - integrate(fR, 9, 12)$value          # P(R > 12): delivery over 12 min

Output:

1 with absolute error < 1.1e-14
[1] 11.49358 11.25000
[1] 0.375

How to read it

  • 1 with absolute error < 1.1e-14: the density is right, it integrates to 1 over its support. If you got something else, check the limits (from 9 to 15, not from 12 to 20).
  • 11.49358 11.25000: the mean delivery time is 11.49 minutes, not the 11.25 you get by dividing 180 by the mean speed of 16 km/h. In general E(g(X))≠g(E(X)) E(g(X)) \ne g(E(X)) unless g g is linear; with a convex g g , like 180/v 180/v , the mean of the result is larger.
  • 0.375: 37.5 % of the orders spend more than 12 minutes on the bike. You can check it without integrating, going back to the speed: R>12  ⟺  180/V>12  ⟺  V<15 R > 12 \iff 180/V > 12 \iff V < 15 , and P(V<15)=(15−12)/8=3/8 P(V < 15) = (15 – 12)/8 = 3/8 . When you doubt a calculation with the new variable, translate it to the old one.

Where the absolute value comes from: the bands in the drawing

The theorem’s formula looks like magic, but you can read it with the drawing of the bands above. A density is probability divided by width. If you cut the speed into four bands of 2 km/h, each one has the same probability, 2/8=0.25 2/8 = 0.25 . When you move to time, each band still has its 0.25, but it takes up a different width:

Speed (km/h)Time (min)Width in minutesAverage density, 0.25 / widthWhy
18 to 209 to 1010.25With a fast bike, 2 km/h change the time by only 1 minute: the probability piles up
16 to 1810 to 11.251.250.20The same probability, in a bit more room
14 to 1611.25 to 12.861.610.16The curve 180/v 180/v keeps getting steeper
12 to 1412.86 to 152.140.12With a slow bike, 2 km/h are more than 2 minutes: the density drops

Those average densities fall between the values of 22.5/r2 22.5/r^2 at the edges of each band (in the first one, 22.5/81=0.28 22.5/81 = 0.28 and 22.5/100=0.225 22.5/100 = 0.225 ). The factor ∣dvdr∣=180/r2 \left|\frac{dv}{dr}\right| = 180/r^2 is exactly that: how many km/h fit into one minute of delivery. And it carries an absolute value because a width is never negative, even when the function goes down.

When g is not monotonic: back to F

The change of variable theorem cannot be used if g g goes up and down on the support, like x2 x^2 or ∣x∣ |x| on the whole line: two values of x x reach the same y y . Then you go back to route 1.

The app estimates the arrival time and is off by E E minutes, with E∼N(0,4) E \sim N(0, 4) (sometimes over, sometimes under). Standardising, Z=E/4∼N(0,1) Z = E/4 \sim N(0, 1) . How is W=Z2 W = Z^2 , the squared standardised error, distributed? For w>0 w > 0 :

FW(w)=P(Z2≤w)=P(−w≤Z≤w)=Φ(w)−Φ(−w) F_W(w) = P(Z^2 \le w) = P(-\sqrt{w} \le Z \le \sqrt{w}) = \Phi(\sqrt{w}) – \Phi(-\sqrt{w})

Differentiating (chain rule on each term):

fW(w)=12w φ(w)+12w φ(−w)=1w φ(w)=12πw e−w/2,w>0 f_W(w) = \frac{1}{2\sqrt{w}}\,\varphi(\sqrt{w}) + \frac{1}{2\sqrt{w}}\,\varphi(-\sqrt{w}) = \frac{1}{\sqrt{w}}\,\varphi(\sqrt{w}) = \frac{1}{\sqrt{2\pi w}}\,e^{-w/2}, \qquad w > 0

It is the chi-squared with 1 degree of freedom, χ2(1) \chi^2(1) , which is also a gamma(1/2, 1/2) \text{gamma}(1/2,\ 1/2) . You will see it again in topic 8. The two branches, +w +\sqrt{w} and −w -\sqrt{w} , contribute the same because the normal is symmetric.

1 - pchisq(4, df = 1)        # P(W > 4): the squared error goes over 4
2 * (1 - pnorm(2))           # P(|Z| > 2): the same, said with the normal

Output:

[1] 0.04550026
[1] 0.04550026

Both give 0.0455: W>4 W > 4 is the same as ∣Z∣>2 |Z| > 2 , that is, the app being off by more than 8 minutes (two standard deviations). 4.55 % of the time.

Applying fX(g−1(y)) ∣(g−1)′(y)∣ f_X(g^{-1}(y))\,|(g^{-1})'(y)| to Y=X2 Y = X^2 with a single root, x=y x = \sqrt{y} . You get half the density (it integrates to 0.5) and you don’t notice until you check. Before using the theorem, ask yourself: is g g monotonic on the whole support of X X ?

Simulating any distribution with runif

There is one transformation that deserves its own section. If X X is continuous with distribution function F F :

  • F(X)∼U(0,1) F(X) \sim U(0, 1) : passing a variable through its own F F gives a uniform.
  • The other way round: if U∼U(0,1) U \sim U(0, 1) , then F−1(U) F^{-1}(U) has distribution function F F .

The second one is what R uses under the hood to simulate. For the kitchen, F(x)=1−e−0.1x F(x) = 1 – e^{-0.1x} ; solving, F−1(u)=−10log⁡(1−u) F^{-1}(u) = -10\log(1 – u) . So 10,000 kitchen times can be made from uniform numbers:

set.seed(2026)
u <- runif(10000)
x <- -10 * log(1 - u)        # the inverse of F: simulated kitchen
c(mean(x), var(x))           # an exp(0.1) has mean 10 and variance 100
mean(x <= 10); pexp(10, 0.1) # simulated proportion versus theoretical

Output:

[1]  9.964396 99.177245
[1] 0.6397
[1] 0.6321206

The mean comes out 9.96 and the variance 99.18, close to 10 and 100, and 64 % of the simulated kitchens take 10 minutes or less, against the 63.2 % that pexp says: the difference is simulation noise. It is exactly what qexp(u, 0.1) does: R’s q is the inverse of its p.

When you can’t remember whether a calculation with F−1 F^{-1} is right, simulate: x <- qexp(runif(1e5), 0.1) and compare its histogram with dexp. If they match, the calculation is right.

Two variables at once: the joint distribution

Dishes and drinks: the discrete case

We start with something you count. In each order from the restaurant, X X is the number of dishes (1, 2 or 3) and Y Y the number of drinks (0, 1 or 2). Looking at many orders, the restaurant has this joint probability function, pij=P(X=xi,Y=yj) p_{ij} = P(X = x_i, Y = y_j) :

X X \\ Y Y 012Marginal of X X
1 dish0.200.150.050.40
2 dishes0.100.200.100.40
3 dishes0.020.080.100.20
Marginal of Y Y 0.320.430.251

Each cell is a probability (non-negative) and together they add up to 1. The marginals are read on the edges: adding each row gives that of X X and adding each column, that of Y Y . For example, P(Y=0)=0.20+0.10+0.02=0.32 P(Y = 0) = 0.20 + 0.10 + 0.02 = 0.32 : 32 % of the orders come without a drink.

Summing the joint table, row by row and column by column

The marginals of the table come from a very concrete question. To find P(X=1) P(X = 1) , you don’t care how many drinks there are: an order with 1 dish can have 0, 1 or 2 drinks, and those three cases don’t overlap. So you add them up:

MarginalCalculationWhy
P(X=1) P(X = 1) 0.20+0.15+0.05=0.40 0.20 + 0.15 + 0.05 = 0.40 Row for 1 dish: every possible number of drinks
P(X=2) P(X = 2) 0.10+0.20+0.10=0.40 0.10 + 0.20 + 0.10 = 0.40 Row for 2 dishes
P(X=3) P(X = 3) 0.02+0.08+0.10=0.20 0.02 + 0.08 + 0.10 = 0.20 Row for 3 dishes. And 0.40+0.40+0.20=1 0.40 + 0.40 + 0.20 = 1 : check done
P(Y=1) P(Y = 1) 0.15+0.20+0.08=0.43 0.15 + 0.20 + 0.08 = 0.43 Column for 1 drink: now you don’t care about the dishes
P(Y=2) P(Y = 2) 0.05+0.10+0.10=0.25 0.05 + 0.10 + 0.10 = 0.25 Column for 2 drinks. With the 0.32 from before, 0.32+0.43+0.25=1 0.32 + 0.43 + 0.25 = 1

Keep this sentence: the marginal of one variable is the joint summed over everything the other one can do. In the continuous case it is the same sentence, with “sum” replaced by “integrate”.

p <- matrix(c(0.20, 0.15, 0.05,
              0.10, 0.20, 0.10,
              0.02, 0.08, 0.10), nrow = 3, byrow = TRUE,
            dimnames = list(dishes = 1:3, drinks = 0:2))
addmargins(p)                       # the table with its marginals
pX <- rowSums(p); pY <- colSums(p)
p["3", ] / pX["3"]                  # drinks when there are 3 dishes

Output:

      drinks
dishes    0    1    2 Sum
   1   0.20 0.15 0.05 0.4
   2   0.10 0.20 0.10 0.4
   3   0.02 0.08 0.10 0.2
   Sum 0.32 0.43 0.25 1.0
  0   1   2 
0.1 0.4 0.5 

How to read it

  • The table with Sum: addmargins adds the row and column of totals, which are the marginals (0.40, 0.40, 0.20 for the dishes; 0.32, 0.43, 0.25 for the drinks) and the 1 in the corner.
  • 0.1 0.4 0.5: it is the conditional distribution of the drinks when the order has 3 dishes: the 3-dish row divided by its marginal, 0.02/0.20, 0.08/0.20, 0.10/0.20 0.02/0.20,\ 0.08/0.20,\ 0.10/0.20 . With 3 dishes, half the orders carry 2 drinks; in general (marginal), only 25 %. Knowing how many dishes there are changes what you expect of the drinks: you can already sense they are not independent.

The whole order: the continuous case

Back to the times. Now we look at the order with two numbers: X X , the minute it leaves the kitchen, and Y=X+R Y = X + R , the minute it arrives at your door. Always Y>X Y > X . Their joint density is

f(x,y)=0.01 e−0.1y,0<x<y f(x, y) = 0.01\,e^{-0.1y}, \qquad 0 < x < y

(where this formula comes from we will see with the Jacobian, at the end). A joint density does two things: f(x,y)≥0 f(x, y) \ge 0 and its integral over the whole plane is 1. And probabilities are volumes: the probability that (X,Y) (X, Y) falls in a region A A is the double integral of f f over A A .

The joint density of the order lives in a trianglePlane with x, the minute the order leaves the kitchen, and y, the minute it arrives, from 0 to 60. The density, shaded in violet, only exists above the diagonal y equals x and is stronger at the bottom. A vertical coral line at x equals 10 runs from y equals 10 upwards; a horizontal green line at y equals 30 runs from x equals 0 to 30. A dashed red region marks x up to 10 and y up to 30 above the diagonal, with probability 0.58.01020304050600102030405060x: leaves the kitchen (min)y: arrives (min)y = xf = 0 (you cannotarrive before leaving)Vertical line at x = 10:y runs from 10 to ∞→ marginal f_X(10)Horizontal line at y = 30:x runs from 0 to 30→ marginal f_Y(30)Dashed region:P(X ≤ 10, Y ≤ 30)= 0.58 (only above y = x)
The diagonal gives the limits of every integral: where each line enters and where it leaves.

In the drawing, the density only lives in the triangle above the diagonal y=x y = x , and it is stronger at the bottom (fast orders). That diagonal is what decides all the integration limits in the topic. For example, the probability that the kitchen finishes within 10 minutes and the order arrives within 30:

P(X≤10, Y≤30)=∫010 ⁣∫x300.01 e−0.1y dy dx=∫0100.1(e−0.1x−e−3)dx=1−e−1−e−3=0.58 P(X \le 10,\ Y \le 30) = \int_0^{10}\!\int_x^{30} 0.01\,e^{-0.1y}\,dy\,dx = \int_0^{10} 0.1\left(e^{-0.1x} – e^{-3}\right)dx = 1 – e^{-1} – e^{-3} = 0.58

Look at the inner limit: y y starts at x x , not at 0, because there is nothing below the diagonal.

Before writing a double integral, draw the support and the region you are asked for, and sweep it with a vertical line (x x fixed): where that line enters and where it leaves are the limits for y y . This is what I was missing with the 30 minutes: the region x+y≤30 x + y \le 30 is a triangle, not the square [0,15]2 [0, 15]^2 .

Arriving within 30 minutes: the square versus the triangleTwo planes with kitchen X horizontally and delivery R vertically, from 0 to 40 minutes. On the left, in red, the square X up to 15 and R up to 15, with probability 0.60, which leaves out orders that do arrive on time, such as kitchen 5 and delivery 22. On the right, in green, the triangle X plus R up to 30, with probability 0.80, which is what was asked.010203040010203040kitchen X (min)delivery R (min)0.60What I computed: X ≤ 15 and R ≤ 15left out:kitchen 5, delivery 22010203040010203040kitchen X (min)0.80What was asked: X + R ≤ 30x + r = 30
Same model and same density: only the region changes. The coral point arrives in 27 minutes and the square does not count it.

The marginals

To keep a single variable, you integrate out the other one (in the discrete case, you add):

fX(x)=∫−∞∞f(x,y) dyfY(y)=∫−∞∞f(x,y) dx f_X(x) = \int_{-\infty}^{\infty} f(x, y)\,dy \qquad\qquad f_Y(y) = \int_{-\infty}^{\infty} f(x, y)\,dx

In the order, the infinities become the edges of the triangle:

fX(x)=∫x∞0.01 e−0.1y dy=0.1 e−0.1x,x>0fY(y)=∫0y0.01 e−0.1y dx=0.01 y e−0.1y,y>0 f_X(x) = \int_x^{\infty} 0.01\,e^{-0.1y}\,dy = 0.1\,e^{-0.1x}, \quad x > 0 \qquad\qquad f_Y(y) = \int_0^{y} 0.01\,e^{-0.1y}\,dx = 0.01\,y\,e^{-0.1y}, \quad y > 0

The kitchen is an exp⁡(0.1) \exp(0.1) , as it should be, and the arrival minute is a gamma(2, 0.1) \text{gamma}(2,\ 0.1) : the sum of two independent exponentials with the same λ \lambda . In the second integral, e−0.1y e^{-0.1y} does not depend on x x and comes out; what is left is the length of the interval, y y .

Integrating the joint density, with the drawing in front of you

It is the dishes and drinks table in continuous form: you fix one variable and “sum” everything the other one can do. The hard part is the limits, and the triangle 0<x<y 0 < x < y gives them:

MarginalWhat you fixThe other one runs…Why those limitsResult
fX(x) f_X(x) The kitchen, x x y y from x x to ∞ \infty Vertical line at x x : it enters the triangle through the diagonal (you can’t arrive before leaving the kitchen) and never leaves through the top0.1 e−0.1x 0.1\,e^{-0.1x}
fY(y) f_Y(y) The arrival, y y x x from 0 0 to y y Horizontal line at y y : the kitchen could have finished at any minute between 0 and the arrival0.01 y e−0.1y 0.01\,y\,e^{-0.1y}

In the first one, the calculation is ∫x∞0.01 e−0.1y dy=0.01⋅e−0.1x0.1=0.1 e−0.1x \int_x^{\infty} 0.01\,e^{-0.1y}\,dy = 0.01\cdot\frac{e^{-0.1x}}{0.1} = 0.1\,e^{-0.1x} : the antiderivative of e−0.1y e^{-0.1y} is −10 e−0.1y -10\,e^{-0.1y} , which is 0 at infinity. With the marginal you can now work out anything about one variable: P(X≤10)=1−e−1=0.63 P(X \le 10) = 1 – e^{-1} = 0.63 , without going back to the joint.

Integrating y y from 0 to infinity “because the arrival is positive”. You get ∫0∞0.01 e−0.1y dy=0.1 \int_0^{\infty} 0.01\,e^{-0.1y}\,dy = 0.1 for every x x : a “density” that is constant for x>0 x > 0 , which would integrate to infinity. When a marginal comes out without the fixed variable in it, check the limits with the drawing.

f <- function(x, y) ifelse(x < y, 0.01 * exp(-0.1 * y), 0)
fX <- function(x) integrate(function(y) f(x, y), x, Inf)$value
fY <- function(y) integrate(function(x) f(x, y), 0, y)$value
c(fX(10), dexp(10, 0.1))              # the marginal of X at x = 10
c(fY(30), dgamma(30, 2, 0.1))         # the marginal of Y at y = 30
pgamma(30, 2, 0.1)                    # P(Y <= 30): arrives on time

Output:

[1] 0.03678794 0.03678794
[1] 0.01493612 0.01493612
[1] 0.8008517

How to read it

  • 0.03678794 0.03678794: integrating the joint density in y y from x x to infinity gives the same as the exponential density. The marginal is right.
  • 0.01493612 0.01493612: the same with the gamma. It is a density height (per minute), not a probability.
  • 0.8008517: the probability that the order arrives in 30 minutes or less is 0.80. It is the one from the beginning: the right one.

If you know the joint, you know the marginals. Not the other way round: two different joints can have the same marginals. That is why the marginals say nothing about how the variables are related.

The conditionals

The distribution of X X given that Y=y Y = y is the joint density cut along the line Y=y Y = y and rescaled so that it integrates to 1 again:

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

In the order:

f(x∣y)=0.01 e−0.1y0.01 y e−0.1y=1y,0<x<yf(y∣x)=0.01 e−0.1y0.1 e−0.1x=0.1 e−0.1(y−x),y>x f(x \mid y) = \frac{0.01\,e^{-0.1y}}{0.01\,y\,e^{-0.1y}} = \frac{1}{y}, \quad 0 < x < y \qquad\qquad f(y \mid x) = \frac{0.01\,e^{-0.1y}}{0.1\,e^{-0.1x}} = 0.1\,e^{-0.1(y – x)}, \quad y > x
Two slices of the triangle: the conditional distributions of the orderOn the left, the density of the kitchen exit given that the order arrived at minute 30: flat at height one thirtieth between 0 and 30, with mean 15. On the right, the density of the arrival given that the kitchen finished at minute 10: zero before 10 and an exponential falling from 0.1 from 10 on, with mean 20.0.000.040.080.12010203040x: leaves the kitchen (min)densitymean 15X | Y = 30 ~ U(0, 30)0.000.040.080.120102030405060y: arrives (min)mean 20Y | X = 10: 10 + exp(0.1)
Knowing one variable changes the distribution of the other: the sign that they are not independent.

Both have a very concrete reading, and in the drawing they look like two slices of the triangle:

  • X∣Y=y∼U(0,y) X \mid Y = y \sim U(0, y) . If the order arrived at minute 30, the kitchen may have finished at any moment between 0 and 30, all equally likely. Its mean is E(X∣Y=30)=15 E(X \mid Y = 30) = 15 .
  • Y∣X=x Y \mid X = x is x x plus an exp⁡(0.1) \exp(0.1) . If the kitchen finished at minute 10, the order arrives 10 minutes later on average: E(Y∣X=x)=x+10 E(Y \mid X = x) = x + 10 .

Two results you will use a lot (theorem 4 in the notes):

E(Y)=E(E(Y∣X))Var(Y)=E(Var(Y∣X))+Var(E(Y∣X)) E(Y) = E\big(E(Y \mid X)\big) \qquad\qquad \mathrm{Var}(Y) = E\big(\mathrm{Var}(Y \mid X)\big) + \mathrm{Var}\big(E(Y \mid X)\big)

With the order: E(Y)=E(X+10)=10+10=20 E(Y) = E(X + 10) = 10 + 10 = 20 and Var(Y)=E(100)+Var(X+10)=100+100=200 \mathrm{Var}(Y) = E(100) + \mathrm{Var}(X + 10) = 100 + 100 = 200 , the same as the gamma(2, 0.1) \text{gamma}(2,\ 0.1) gives: mean 2/0.1 2/0.1 and variance 2/0.12 2/0.1^2 .

Conditioning is slicing and rescaling, first with the table

The formula is easier to understand with the table. Knowing that the order has 1 dish means keeping the row for 1 dish and forgetting the other two. That row adds up to 0.40, not 1, so each cell is divided by 0.40 to make it a distribution again:

Drinks, y y Cell P(X=1,Y=y) P(X = 1, Y = y) P(Y=y∣X=1) P(Y = y \mid X = 1) Why
00.200.20/0.40=0.50 0.20/0.40 = 0.50 Half the 1-dish orders have no drink
10.150.15/0.40=0.375 0.15/0.40 = 0.375 Slice: only the 1-dish row counts
20.050.05/0.40=0.125 0.05/0.40 = 0.125 Rescale: 0.50+0.375+0.125=1 0.50 + 0.375 + 0.125 = 1

In the continuous case it is the same. If the order arrived at minute 30, you slice the triangle along the line y=30 y = 30 : what is left is f(x,30)=0.01 e−3≈0.0005 f(x, 30) = 0.01\,e^{-3} \approx 0.0005 , the same height for every x x between 0 and 30. That “row” doesn’t integrate to 1, but to fY(30)=0.3 e−3=0.01 f_Y(30) = 0.3\,e^{-3} = 0.01 (the 0.01493612 in the R output). After dividing, you get 1/30=0.03 1/30 = 0.03 over the whole interval: a U(0,30) U(0, 30) . The slice is flat because the joint density doesn’t depend on x x .

Check you’ve got it: with the dishes and drinks table, do you get E(Y)=0.93 E(Y) = 0.93 by averaging the conditional means, as the theorem says?

Yes. First, the mean number of drinks in each row, with its conditional distribution. With 1 dish: 0⋅0.50+1⋅0.375+2⋅0.125=0.625 0\cdot 0.50 + 1\cdot 0.375 + 2\cdot 0.125 = 0.625 . With 2 dishes, the row divided by 0.40 is 0.25, 0.50 and 0.25, and the mean is 0.50+0.50=1 0.50 + 0.50 = 1 . With 3 dishes, 0.10, 0.40 and 0.50, and the mean is 0.40+1=1.40 0.40 + 1 = 1.40 . Then you average those means with the weight of each row, which is the marginal of X X : 0.40⋅0.625+0.40⋅1+0.20⋅1.40=0.25+0.40+0.28=0.93 0.40\cdot 0.625 + 0.40\cdot 1 + 0.20\cdot 1.40 = 0.25 + 0.40 + 0.28 = 0.93 . It is the same mean the marginal of Y Y gives. The conditional means go up with the dishes (0.625, 1 and 1.40): another hint that dishes and drinks are not independent.

Independence

X X and Y Y are independent if and only if the joint density is the product of the marginals at every point:

f(x,y)=fX(x) fY(y)for every (x,y) f(x, y) = f_X(x)\,f_Y(y) \quad \text{for every } (x, y)
  • Kitchen and delivery, (X,R) (X, R) : yes. Their joint density is 0.1 e−0.1x⋅0.1 e−0.1r 0.1\,e^{-0.1x}\cdot 0.1\,e^{-0.1r} on the quadrant x>0, r>0 x > 0,\ r > 0 : a product of a function of x x and another of r r , on a rectangular support.
  • Kitchen and arrival, (X,Y) (X, Y) : no. At (x,y)=(20,10) (x, y) = (20, 10) the joint density is 0 (you cannot arrive before leaving the kitchen), but fX(20) fY(10)>0 f_X(20)\,f_Y(10) > 0 .

If the support is not a rectangle (a triangle, a circle, 0<x<y 0 < x < y …), the variables are not independent: the value of one limits what the other can be. You don’t need to compute anything else.

In the discrete table, P(X=1,Y=0)=0.20 P(X = 1, Y = 0) = 0.20 and P(X=1) P(Y=0)=0.40⋅0.32=0.128 P(X = 1)\,P(Y = 0) = 0.40 \cdot 0.32 = 0.128 : a single cell that doesn’t match is enough for them not to be independent.

Checking independence cell by cell

In a table, “the joint is the product of the marginals” means: in each cell, multiply the marginal of its row by the marginal of its column and compare. If all nine match, they are independent; if one fails, they are not:

Dishes / drinks012
1 dish (0.40)0.40⋅0.32=0.128 0.40\cdot 0.32 = 0.128 versus 0.200.40⋅0.43=0.172 0.40\cdot 0.43 = 0.172 versus 0.150.40⋅0.25=0.10 0.40\cdot 0.25 = 0.10 versus 0.05
2 dishes (0.40)0.128 versus 0.100.172 versus 0.200.10 versus 0.10
3 dishes (0.20)0.064 versus 0.020.086 versus 0.080.05 versus 0.10

With 1 dish there are more orders without a drink than there would be if they were independent (0.20 versus 0.128), and with 3 dishes, more with 2 drinks (0.10 versus 0.05). In the continuous case, the check has two questions, in this order: is the support a rectangle? (if not, you are done: they are not independent) and, if it is, can the joint be written as something in x x times something in y y ?

Check you’ve got it: in the cell for 2 dishes and 2 drinks the product matches (0.40 · 0.25 = 0.10). Does that say anything about independence?

Nothing. Independence is “for every (x,y) (x, y) “: all nine cells have to match. One matching can be a coincidence, as here; one failing, like the one for 1 dish and 0 drinks, is enough to say they are not independent. In the exam, to deny it you only need to show one cell that fails; to claim it, you have to check them all.

Expectation of a function of two variables

For any function g(X,Y) g(X, Y) :

E(g(X,Y))=∫ ⁣ ⁣∫g(x,y) f(x,y) dx dy(or ∑x∑yg(x,y) p(x,y)) E\big(g(X, Y)\big) = \int\!\!\int g(x, y)\,f(x, y)\,dx\,dy \qquad \Big(\text{or } \sum_{x}\sum_{y} g(x, y)\,p(x, y)\Big)

Two consequences used all the time:

  • Linearity, always: E(aX+bY)=aE(X)+bE(Y) E(aX + bY) = aE(X) + bE(Y) , whether they are independent or not.
  • The product, only with independence: if X X and Y Y are independent, E(XY)=E(X) E(Y) E(XY) = E(X)\,E(Y) (and, in general, E(g(X)h(Y))=E(g(X)) E(h(Y)) E(g(X)h(Y)) = E(g(X))\,E(h(Y)) ). The converse is not true.

In the order, E(XY) E(XY) is not 10⋅20=200 10 \cdot 20 = 200 , because X X and Y Y are not independent. You have to integrate:

E(XY)=∫0∞ ⁣ ⁣∫x∞x y⋅0.01 e−0.1y dy dx=300 E(XY) = \int_0^{\infty}\!\!\int_x^{\infty} x\,y\cdot 0.01\,e^{-0.1y}\,dy\,dx = 300

Covariance and correlation

The covariance measures whether two variables move together, and the correlation does the same without units:

Cov(X,Y)=E[(X−μX)(Y−μY)]=E(XY)−E(X)E(Y)ρ=Cov(X,Y)σX σY \mathrm{Cov}(X, Y) = E\big[(X – \mu_X)(Y – \mu_Y)\big] = E(XY) – E(X)E(Y) \qquad\qquad \rho = \frac{\mathrm{Cov}(X, Y)}{\sigma_X\,\sigma_Y}

In the order: Cov(X,Y)=300−10⋅20=100 \mathrm{Cov}(X, Y) = 300 – 10\cdot 20 = 100 and

ρ=100100⋅200=12=0.71 \rho = \frac{100}{\sqrt{100}\cdot\sqrt{200}} = \frac{1}{\sqrt{2}} = 0.71

Positive: when the kitchen takes longer, the order tends to arrive later. It is not 1 because the delivery adds noise that has nothing to do with the kitchen.

set.seed(2026)
x <- rexp(10000, 0.1)          # kitchen
r <- rexp(10000, 0.1)          # delivery, independent
y <- x + r                     # arrival minute
round(c(cov(x, y), cor(x, y)), 2)
round(c(cov(x, r), cor(x, r)), 2)
round(c(var(y - x), var(x) + var(y) - 2 * cov(x, y)), 2)

Output:

[1] 103.58   0.72
[1] 0.57 0.01
[1] 98.86 98.86

How to read it

  • 103.58 0.72: with 10,000 simulated orders, the covariance comes out close to 100 and the correlation, 0.72, one hundredth away from the theoretical 0.71.
  • 0.57 0.01: kitchen and delivery are independent and their simulated correlation is practically 0 (not exactly 0 because of the randomness of the simulation).
  • 98.86 98.86, two equal numbers: it is the formula for the variance of a linear combination, the one you need to know:
Var(aX+bY)=a2 Var(X)+b2 Var(Y)+2ab Cov(X,Y) \mathrm{Var}(aX + bY) = a^2\,\mathrm{Var}(X) + b^2\,\mathrm{Var}(Y) + 2ab\,\mathrm{Cov}(X, Y)

With a=−1, b=1 a = -1,\ b = 1 : Var(Y−X)=100+200−2⋅100=100 \mathrm{Var}(Y – X) = 100 + 200 – 2\cdot 100 = 100 , which is the variance of the delivery, as it should be, because Y−X=R Y – X = R . If you forget the covariance term, you get 300: three times as much.

Properties that come up (all proved in the proofs article):

PropertyWhat it says
Cov(X,X)=Var(X) \mathrm{Cov}(X, X) = \mathrm{Var}(X) The variance is the covariance of a variable with itself
−1≤ρ≤1 -1 \le \rho \le 1 It comes from the Cauchy–Schwarz inequality
ρ=±1  ⟺  Y=aX+b \rho = \pm 1 \iff Y = aX + b Sign of a a
Cov(aX+b, cY+d)=ac Cov(X,Y) \mathrm{Cov}(aX + b,\ cY + d) = ac\,\mathrm{Cov}(X, Y) Added constants don’t count
ρaX+b, cY+d=±ρXY \rho_{aX + b,\ cY + d} = \pm\rho_{XY} Changing units doesn’t change the correlation (only its sign, if ac<0 ac < 0 )
Independent ⇒Cov=0 \Rightarrow \mathrm{Cov} = 0 The converse is not true

Covariance cell by cell: dishes and drinks

With the discrete table you can see what covariance does inside, without integrals. For E(XY)−E(X)E(Y) E(XY) – E(X)E(Y) you need three means. Two come from the marginals: E(X)=1⋅0.40+2⋅0.40+3⋅0.20=1.80 E(X) = 1\cdot 0.40 + 2\cdot 0.40 + 3\cdot 0.20 = 1.80 and E(Y)=0⋅0.32+1⋅0.43+2⋅0.25=0.93 E(Y) = 0\cdot 0.32 + 1\cdot 0.43 + 2\cdot 0.25 = 0.93 . For E(XY) E(XY) , each cell contributes its product x⋅y x\cdot y times its probability:

Cell (x,y) (x, y) x⋅y x\cdot y p p x⋅y⋅p x\cdot y\cdot p Why
(1,0),(2,0),(3,0) (1, 0), (2, 0), (3, 0) 00.32 in total0With no drinks, the product is 0: the whole column contributes nothing
(1,1) (1, 1) 10.150.15
(1,2) (1, 2) 20.050.10
(2,1) (2, 1) 20.200.40
(2,2) (2, 2) 40.100.40
(3,1) (3, 1) 30.080.24
(3,2) (3, 2) 60.100.60The one that contributes most: a big order with lots of drinks
Total1E(XY)=1.89 E(XY) = 1.89

So Cov(X,Y)=1.89−1.80⋅0.93=1.89−1.674=0.22 \mathrm{Cov}(X, Y) = 1.89 – 1.80\cdot 0.93 = 1.89 – 1.674 = 0.22 . Positive: more dishes, more drinks. With the variances, Var(X)=3.80−1.802=0.56 \mathrm{Var}(X) = 3.80 – 1.80^2 = 0.56 and Var(Y)=1.43−0.932=0.57 \mathrm{Var}(Y) = 1.43 – 0.93^2 = 0.57 , the correlation is ρ=0.216/0.56⋅0.5651=0.38 \rho = 0.216/\sqrt{0.56\cdot 0.5651} = 0.38 : a weakish linear relationship, much weaker than the one between kitchen and arrival.

The other formula, E[(X−μX)(Y−μY)] E[(X – \mu_X)(Y – \mu_Y)] , explains the sign. Where both variables are above their mean (3 dishes, 2 drinks) or both below (1 dish, 0 drinks), the product of the distances is positive, and those two cells contribute a lot: 0.13 and 0.15. The “crossed” ones (1 dish with 2 drinks, 3 dishes with no drink) subtract, but only a little. The balance is 0.22.

Zero covariance is not independence

This is the mistake of the topic. Covariance only measures the linear relationship. Take the app’s error, E∼N(0,4) E \sim N(0, 4) , and its square, E2 E^2 . They are tightly linked (if you know E E , you know E2 E^2 exactly), but

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

because the normal is symmetric and E(E3)=0 E(E^3) = 0 .

Three point clouds: independence, correlation and zero correlation without independenceThree scatter plots of 400 simulated points. Kitchen and delivery: a shapeless cloud, correlation 0. Kitchen and arrival: a rising cloud, all above the diagonal, correlation 0.71. App error and its square: a perfect parabola with correlation 0, although one variable determines the other.0255002550xrKitchen and deliveryρ = 00408002550xyKitchen and arrivalρ = 0.71070140−12012ee²Error and its squareρ = 0
The third cloud has correlation 0 and is the strongest relationship of the three: covariance only sees straight lines.

In the drawing there are three clouds of 400 simulated points. The one on the left, kitchen and delivery, has no shape: independent. The middle one, kitchen and arrival, goes up: correlation 0.71. The one on the right is a perfect parabola and its correlation is 0. A correlation close to 0 doesn’t let you say there is no relationship: only that there is no straight line.

“Since Cov(X,Y)=0 \mathrm{Cov}(X, Y) = 0 , they are independent.” It only works the other way round (independent ⇒ covariance 0). To prove independence you have to see that the joint density factorises, or that the joint mgf factorises. There is one important exception: with a bivariate normal, ρ=0 \rho = 0 does imply independence.

The bivariate normal

It is the most used joint distribution: two normal variables with a correlation ρ \rho . In the delivery, D D is the distance of the order (km) and R R the delivery time (min), with μD=3, σD=1, μR=15, σR=4 \mu_D = 3,\ \sigma_D = 1,\ \mu_R = 15,\ \sigma_R = 4 and ρ=0.8 \rho = 0.8 (further away, more time). Its density is

f(d,r)=12πσDσR1−ρ2exp⁡{−12(1−ρ2)[(d−μD)2σD2+(r−μR)2σR2−2ρ(d−μD)(r−μR)σDσR]} f(d, r) = \frac{1}{2\pi\sigma_D\sigma_R\sqrt{1 – \rho^2}}\exp\left\{-\frac{1}{2(1 – \rho^2)}\left[\frac{(d – \mu_D)^2}{\sigma_D^2} + \frac{(r – \mu_R)^2}{\sigma_R^2} – \frac{2\rho(d – \mu_D)(r – \mu_R)}{\sigma_D\sigma_R}\right]\right\}

What you need to know about it:

  • Its marginals are normal: D∼N(3,1) D \sim N(3, 1) and R∼N(15,4) R \sim N(15, 4) .
  • So are its conditionals, with the mean moved by the regression line and the standard deviation shrunk:
R∣D=d ∼ N ⁣(μR+ρ σRσD(d−μD),  σR1−ρ2) R \mid D = d \ \sim\ N\!\left(\mu_R + \rho\,\frac{\sigma_R}{\sigma_D}(d – \mu_D),\ \ \sigma_R\sqrt{1 – \rho^2}\right)
  • ρ=0  ⟺   \rho = 0 \iff independent (here, yes).

An order 5 km away: R∣D=5∼N(15+0.8⋅4⋅2, 40.36)=N(21.4, 2.4) R \mid D = 5 \sim N(15 + 0.8\cdot 4\cdot 2,\ 4\sqrt{0.36}) = N(21.4,\ 2.4) .

1 - pnorm(25, 15, 4)          # P(R > 25) without knowing the distance
1 - pnorm(25, 21.4, 2.4)      # P(R > 25 | D = 5)

Output:

[1] 0.006209665
[1] 0.0668072

Without knowing the distance, only 0.62 % of deliveries go over 25 minutes. Knowing that the order goes 5 km away, the probability rises to 6.68 %, more than ten times as much: the information about one variable changes what you expect of the other when they are correlated.

The bivariate normal of the delivery and its slice at D = 5On the left, contour lines of the bivariate normal of distance, mean 3 km, and delivery time, mean 15 minutes, with correlation 0.8: ellipses tilted upwards centred at 3 and 15, and a dashed coral line at distance 5. On the right, the marginal of the delivery time, normal 15 4 in violet, and the conditional given distance 5, normal 21.4 2.4 in coral, taller and shifted, with the tail above 25 minutes shaded, 6.68 percent.391521270123456distance D (km)delivery R (min)D = 5center (3, 15)0.000.050.100.153915212733delivery (min)N(15, 4)N(21.4, 2.4)6.68 %
Knowing the order goes 5 km away moves the bell to the right and narrows it.

In the drawing, the “mountain” of the bivariate normal seen from above: tilted ellipses because ρ>0 \rho > 0 . The dashed line is the slice at D=5 D = 5 : the coral bell on the right is the conditional, centred at 21.4 and narrower than the marginal of R R .

The lab sessions use the mnormt package, whose function dmnorm(cbind(d, r), mean, cov) computes exactly this density from the mean vector and the covariance matrix Σ \Sigma , with ρσDσR=3.2 \rho\sigma_D\sigma_R = 3.2 off the diagonal. In this topic’s practice we write the formula by hand to see where each number comes from.

The joint mgf

As with one variable, the joint moment generating function keeps the whole distribution:

φ(X,Y)(s,t)=E(esX+tY) \varphi_{(X,Y)}(s, t) = E\big(e^{sX + tY}\big)

Its properties:

  • φ(s,0)=φX(s) \varphi(s, 0) = \varphi_X(s) and φ(0,t)=φY(t) \varphi(0, t) = \varphi_Y(t) : the marginals come out by setting a zero.
  • The derivatives at (0,0) (0, 0) give the moments: ∂φ∂s=E(X) \frac{\partial\varphi}{\partial s} = E(X) , ∂2φ∂s2=E(X2) \frac{\partial^2\varphi}{\partial s^2} = E(X^2) , ∂2φ∂s ∂t=E(XY) \frac{\partial^2\varphi}{\partial s\,\partial t} = E(XY) .
  • X X and Y Y are independent   ⟺  φ(s,t)=φ(s,0) φ(0,t) \iff \varphi(s, t) = \varphi(s, 0)\,\varphi(0, t) .

In the order, since sX+tY=(s+t)X+tR sX + tY = (s + t)X + tR with X X and R R independent:

φ(X,Y)(s,t)=E(e(s+t)X) E(etR)=0.10.1−s−t⋅0.10.1−t,t<0.1, s+t<0.1 \varphi_{(X,Y)}(s, t) = E\big(e^{(s+t)X}\big)\,E\big(e^{tR}\big) = \frac{0.1}{0.1 – s – t}\cdot\frac{0.1}{0.1 – t}, \qquad t < 0.1,\ s + t < 0.1

It doesn’t factorise into a part in s s times a part in t t : they are not independent. Differentiating twice, ∂2φ∂s ∂t(0,0)=300=E(XY) \frac{\partial^2\varphi}{\partial s\,\partial t}(0, 0) = 300 = E(XY) , the same number as before.

The mgf is also the quick way to add independent variables: φX+R(t)=φX(t) φR(t)=(0.10.1−t)2 \varphi_{X+R}(t) = \varphi_X(t)\,\varphi_R(t) = \left(\frac{0.1}{0.1 – t}\right)^2 , which is the mgf of a gamma(2, 0.1) \text{gamma}(2,\ 0.1) . That is why the arrival minute is gamma.

Changing two variables at once: the Jacobian

With two variables, the change of variable theorem carries a Jacobian. If U=h1(X,Y) U = h_1(X, Y) and V=h2(X,Y) V = h_2(X, Y) is a bijective transformation, with inverse x=x(u,v), y=y(u,v) x = x(u, v),\ y = y(u, v) :

f(U,V)(u,v)=f(X,Y)(x(u,v), y(u,v)) ∣J∣,J=det⁡(∂x∂u∂x∂v∂y∂u∂y∂v) f_{(U,V)}(u, v) = f_{(X,Y)}\big(x(u, v),\ y(u, v)\big)\,|J|, \qquad J = \det\begin{pmatrix} \frac{\partial x}{\partial u} & \frac{\partial x}{\partial v} \\[4pt] \frac{\partial y}{\partial u} & \frac{\partial y}{\partial v} \end{pmatrix}

What part of the wait is kitchen? We go from (X,R) (X, R) , independent, to T=X+R T = X + R (the total) and U=X/(X+R) U = X/(X + R) (the kitchen fraction). The four steps:

  1. The new support: t>0 t > 0 and 0<u<1 0 < u < 1 .
  2. The inverse: x=ut x = ut and r=t−ut=t(1−u) r = t – ut = t(1 – u) .
  3. The Jacobian:
J=det⁡(∂x∂t∂x∂u∂r∂t∂r∂u)=det⁡(ut1−u−t)=−ut−t(1−u)=−t,∣J∣=t J = \det\begin{pmatrix} \frac{\partial x}{\partial t} & \frac{\partial x}{\partial u} \\[4pt] \frac{\partial r}{\partial t} & \frac{\partial r}{\partial u} \end{pmatrix} = \det\begin{pmatrix} u & t \\ 1 – u & -t \end{pmatrix} = -ut – t(1 – u) = -t, \qquad |J| = t
  1. Substitute:
f(t,u)=0.1 e−0.1ut⋅0.1 e−0.1t(1−u)⋅t=0.01 t e−0.1t,t>0, 0<u<1 f(t, u) = 0.1\,e^{-0.1ut}\cdot 0.1\,e^{-0.1t(1-u)}\cdot t = 0.01\,t\,e^{-0.1t}, \qquad t > 0,\ 0 < u < 1
The change from (kitchen, delivery) to (total, kitchen fraction)On the left, the quadrant of kitchen x and delivery r with two families of lines: in coral those of constant total, x plus r equal to 10, 20, 30, 40 and 50, and in dashed violet the rays from the origin of constant fraction u equal to 0.2, 0.4, 0.6 and 0.8. On the right, the same lines become a straight grid of total t, from 0 to 50, and fraction u, from 0 to 1. The absolute value of the Jacobian is t.010203040010203040kitchen xdelivery rt = 30u = 0.80.00.20.40.60.81.001020304050total tfraction ut = x + ru = x/(x+r)|J| = t
The change turns the quadrant into a rectangle: that is why T and U can come out independent.

The joint density is (something in t t ) × 1 on a rectangle: T∼gamma(2, 0.1) T \sim \text{gamma}(2,\ 0.1) , U∼U(0,1) U \sim U(0, 1) and they are independent. Translated to the order: the fraction of the wait spent in the kitchen is equally likely to be any value between 0 and 1, and it doesn’t depend on whether the order took long or not. Something you can’t see by eye and that the Jacobian gives in four lines.

And the triangle density from the beginning comes out the same way: from (X,R) (X, R) to (X, Y=X+R) (X,\ Y = X + R) , with inverse x=x, r=y−x x = x,\ r = y – x and J=1 J = 1 , you get f(x,y)=0.1 e−0.1x⋅0.1 e−0.1(y−x)=0.01 e−0.1y f(x, y) = 0.1\,e^{-0.1x}\cdot 0.1\,e^{-0.1(y – x)} = 0.01\,e^{-0.1y} with 0<x<y 0 < x < y .

The Jacobian, step by step and with the why

The list above does in three lines what the exam marks separately. The usual order, with the change from (X,R) (X, R) to (T,U) (T, U) :

StepWhat you doIn the orderWhy
1Write the starting joint density and its support0.01 e−0.1(x+r) 0.01\,e^{-0.1(x + r)} , with x>0, r>0 x > 0,\ r > 0 They are independent: the joint is the product of the two exponentials
2Solve for the inversex=ut x = ut , r=t(1−u) r = t(1 – u) The old density is evaluated at x x and r r , so you need them in terms of t t and u u
3The new supportt>0, 0<u<1 t > 0,\ 0 < u < 1 x>0 x > 0 and r>0 r > 0 say that the kitchen is a part (neither nothing nor everything) of a positive total
4The Jacobian and its absolute valueJ=−t J = -t , ∣J∣=t \lvert J \rvert = t You differentiate the inverse, not the direct transformation. The absolute value, because it is a change of area
5Substitute and simplify0.01 t e−0.1t 0.01\,t\,e^{-0.1t} In the exponential, ut+t(1−u)=t ut + t(1 – u) = t : the u u disappears
6Check that it integrates to 1∫0∞0.01 t e−0.1t dt=0.01⋅100=1 \int_0^{\infty} 0.01\,t\,e^{-0.1t}\,dt = 0.01\cdot 100 = 1 It is the whole gamma(2, 0.1) (2,\ 0.1) . If you get something else, the ∣J∣ \lvert J \rvert is missing or the support is wrong

Step 5 is the one with the surprise: since u u disappears from the formula and its support, (0,1) (0, 1) , doesn’t depend on t t , the joint factorises on a rectangle. That is exactly the independence from before.

Check you’ve got it: in the change from (X,R) (X, R) to (X,Y=X+R) (X, Y = X + R) , why is the Jacobian 1 and where does the triangle come from?

The inverse is x=x x = x and r=y−x r = y – x . Its derivatives: ∂x/∂x=1 \partial x/\partial x = 1 , ∂x/∂y=0 \partial x/\partial y = 0 , ∂r/∂x=−1 \partial r/\partial x = -1 and ∂r/∂y=1 \partial r/\partial y = 1 . The determinant is 1⋅1−0⋅(−1)=1 1\cdot 1 – 0\cdot(-1) = 1 : this change neither stretches nor shrinks areas, it only tilts them. The new support comes from the old conditions: x>0 x > 0 stays the same and r>0 r > 0 becomes y−x>0 y – x > 0 , that is, y>x y > x . Hence the triangle 0<x<y 0 < x < y , and hence X X and Y Y can’t be independent.

Forgetting the absolute value of the Jacobian (you get a negative “density”) or not recomputing the support. In the exam, the support of (U,V) (U, V) is worth marks: always write it, and check that the joint density integrates to 1.

In R: what you need

I want…In R
A double integralintegrate(Vectorize(function(x) integrate(function(y) f(x, y), a(x), b(x))$value), x0, x1)
A marginal at a pointintegrate(function(y) f(x0, y), lower, upper)$value
Marginals of a tablerowSums(p), colSums(p), addmargins(p)
Check independence in a tableall.equal(p, outer(rowSums(p), colSums(p)), check.attributes = FALSE)
Covariance and correlation of datacov(x, y), cor(x, y)
Simulate with the inverseqexp(runif(n), rate) (or the inverse you have worked out)
Bivariate normal densitythe formula, or mnormt::dmnorm(cbind(x, y), mu, Sigma)
Draw a surfacez <- outer(x, y, f) and persp(x, y, z) or contour(x, y, z)

The Vectorize is not decoration: integrate passes the outer function a vector of points at once, and the inner one only knows how to compute one. You will see it fail in the practice.

What is proved in this topic

They are all, step by step, in the transformations and joint distributions proofs:

  1. The change of variable theorem, in both versions (g g increasing and decreasing).
  2. If Z∼N(0,1) Z \sim N(0, 1) , then Z2∼χ2(1) Z^2 \sim \chi^2(1) .
  3. F(X)∼U(0,1) F(X) \sim U(0, 1) and F−1(U) F^{-1}(U) has distribution function F F .
  4. E(aX+bY)=aE(X)+bE(Y) E(aX + bY) = aE(X) + bE(Y) and, with independence, E(g(X)h(Y))=E(g(X)) E(h(Y)) E(g(X)h(Y)) = E(g(X))\,E(h(Y)) .
  5. Var(aX+bY) \mathrm{Var}(aX + bY) , Cov(aX+b, cY+d) \mathrm{Cov}(aX + b,\ cY + d) and the correlation of aX+b aX + b and cY+d cY + d .
  6. −1≤ρ≤1 -1 \le \rho \le 1 , with ρ=±1 \rho = \pm 1 only if one variable is a straight line of the other.
  7. The sum of two independent exponentials is a gamma (with the mgf).
  8. The Jacobian of the order: gamma total, uniform fraction and independent.

Typical mistakes

Integration limits of a rectangle on a triangular support. If the joint density lives on 0<x<y 0 < x < y , the marginal of X X is integrated in y y from x x , and that of Y Y in x x up to y y . A marginal that depends on the variable you have just integrated out is wrong.

Using E(XY)=E(X)E(Y) E(XY) = E(X)E(Y) or Var(X+Y)=Var(X)+Var(Y) \mathrm{Var}(X + Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) without independence. In the order, Var(X+Y)=100+200+2⋅100=500 \mathrm{Var}(X + Y) = 100 + 200 + 2\cdot 100 = 500 , not 300.

Concluding independence because a conditional “looks like” the marginal at one point. Independence is at every point: a single cell (or a point of the plane) that fails f=fXfY f = f_X f_Y breaks it.

Always check that what you got is a density: non-negative and integrating to 1 over its support. With integrate in R it takes ten seconds and saves you half an exercise.

Try it in the R playground

Open the site’s R playground (or RStudio) and paste this script. It brings together the rider’s speed, the joint density of the order and the Jacobian:

# 1. The rider: V ~ U(12, 20) km/h and R = 180 / V minutes
set.seed(1); v <- runif(1e5, 12, 20); r <- 180 / v
hist(r, freq = FALSE, breaks = 40)
curve(22.5 / x^2, from = 9, to = 15, add = TRUE, lwd = 2, col = "red")
c(mean(r), 180 / 16)                   # E(R) versus 180 / E(V)
# 2. The order: kitchen X and delivery independent, arrival Y = X + delivery
x <- rexp(1e5, 0.1); y <- x + rexp(1e5, 0.1)
plot(x[1:500], y[1:500], pch = 20); abline(0, 1, col = "red")   # the triangle
c(cov(x, y), cor(x, y))                # close to 100 and 0.71
# 3. The Jacobian: the kitchen fraction is uniform and independent of the total
u <- x / y
hist(u, freq = FALSE); cor(u, y)

What to look at:

  1. In the first histogram, the bars go down from left to right following 22.5/r2 22.5/r^2 , even though the speed was flat.
  2. In the scatter plot, none of the points is below the red line y=x y = x : it is the support 0<x<y 0 < x < y .
  3. Change the second exponential to rexp(1e5, 0.05) (deliveries of 20 minutes on average): does the correlation go up or down? Think about why before running it.
  4. The histogram of u comes out flat and cor(u, y) almost 0. If you change the two rates to different values, does it still come out flat?

Summary

ConceptFormulaIn the order
Change of variable (g g monotonic)fY(y)=fX(g−1(y)) ∣(g−1)′(y)∣ f_Y(y) = f_X(g^{-1}(y))\,\lvert (g^{-1})'(y) \rvert R=180/V R = 180/V : fR(r)=22.5/r2 f_R(r) = 22.5/r^2 on (9,15) (9, 15)
g g not monotonicvia FY F_Y and differentiateZ2∼χ2(1) Z^2 \sim \chi^2(1)
Inverse of F F F−1(U)∼F F^{-1}(U) \sim F kitchen =−10log⁡(1−U) = -10\log(1 – U)
MarginalfX(x)=∫f(x,y) dy f_X(x) = \int f(x, y)\,dy X∼exp⁡(0.1) X \sim \exp(0.1) , Y∼gamma(2, 0.1) Y \sim \text{gamma}(2,\ 0.1)
Conditionalf(x∣y)=f(x,y)/fY(y) f(x \mid y) = f(x, y)/f_Y(y) X∣Y=y∼U(0,y) X \mid Y = y \sim U(0, y)
Independencef=fXfY f = f_X f_Y at every point(X,R) (X, R) yes; (X,Y) (X, Y) no
CovarianceE(XY)−E(X)E(Y) E(XY) – E(X)E(Y) 300−200=100 300 – 200 = 100
CorrelationCov/(σXσY) \mathrm{Cov}/(\sigma_X\sigma_Y) 0.71 0.71
Variance of aX+bY aX + bY a2σX2+b2σY2+2ab Cov a^2\sigma_X^2 + b^2\sigma_Y^2 + 2ab\,\mathrm{Cov} Var(Y−X)=100 \mathrm{Var}(Y – X) = 100
JacobianfUV=fXY ∣J∣ f_{UV} = f_{XY}\,\lvert J \rvert T∼gamma(2, 0.1) T \sim \text{gamma}(2,\ 0.1) , U∼U(0,1) U \sim U(0, 1) , independent

Three ideas to take away:

  1. Draw the support before integrating. The diagonal y=x y = x decides the limits, the independence and half the mistakes in the topic.
  2. Zero covariance is not independence. Independence means the joint density (or the joint mgf) factorises at every point.
  3. Transforming is changing ruler, and the Jacobian is the scale factor. With one variable, ∣dx/dy∣ \lvert dx/dy \rvert ; with two, ∣J∣ \lvert J \rvert . Without it, the density doesn’t integrate to 1.

Now, to prove it: the topic proofs have the eight results step by step, and in the R practice you simulate and integrate them. The next ME1 topic is inequalities and limit theorems: why the mean of many orders looks more and more like a normal.

Next step: proofsTransformations and joint distributions proofs — step by step with the food delivery order, the inverse of F, Cauchy–Schwarz and the Jacobian (and the density that came out negative)

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