Sampling distributions — exam-style exercises with the Virtual Campus, a laptop battery and two printers (and the degrees of freedom I put the wrong way round)
In the class mock of the EC2 I got a two-variances question and lost it entirely over a detail. It asked for a and b such that , with 13 data on top and 9 below. I was completely sure and wrote qf(0.05, 8, 12) and qf(0.95, 8, 12). Out came a very reasonable and very wrong interval: I had swapped the degrees of freedom. The first is the top sample’s, , and the second the bottom one’s. I lost the whole question. That mistake, with its output, is fragment 3 of “What’s wrong here?”.
These are the exam-style exercises for sampling distributions: three chained exercises, each with its own context, mixing paper (proofs and calculations) and R, with marks for each part as in the EC2, plus a bonus. Before doing them it helps to have the topic’s proofs and R practice at hand.
Table of Contents
How to use this article
- Do the exercises with the exam in mind. In ME1’s 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.
- Paper for what is proved, R for what is computed. Calculations to 2 decimals, as the exam asks. Keep whatever you do in R in a script: at the end of the exam you have to upload the
.Rfile with the code for each exercise. - Before each quantile, decide the distribution and its degrees. Normal, t, chi-square or F? or ? Which sample goes on top?
- Mark yourself with the criteria. Each solution comes with “How it is marked”, a split of the marks. It is a guide, mine: not the official marking scheme.
- Finish with the cheat sheet and “For your script”. The cheat sheet only has results (that is what you can bring, handwritten: two sheets, both sides, in the EC2) and “For your script” is the topic’s code ready for your
.Rfile.
Exercise 1: the Virtual Campus first thing in the morning (10 marks)
The time a course page of the Virtual Campus takes to load at 8:00, in seconds, follows a normal with mean 2.4 and standard deviation 0.5. You load the page 16 times on different days (an SRS of size 16).
- (2 marks) Give the distribution of the sample mean and compute .
- (3 marks) Find a value q such that . Justify the distribution you use.
- (3 marks) Find two values a and b, leaving the same probability in each tail, such that .
- (2 marks) Compute .
Exercise 2: the laptop battery in the exam (15 marks)
Your laptop’s manufacturer says the battery lasts 6 hours on average with brightness at 100 %. Before the EC2, ten classmates with the same model measure how long it lasts, first with brightness at 100 % and on another day with brightness at 50 %. Assume the durations are normal.
bright100 <- c(5.23, 5.72, 4.49, 4.61, 6.64, 4.95, 6.76, 6.20, 5.66, 4.90) # hours bright50 <- c(5.49, 6.15, 4.50, 5.07, 6.79, 5.50, 7.23, 7.06, 6.00, 5.22) # the same 10 laptops
- (3 marks) With the 100 % data, compute in R the sample mean and standard deviation, the statistic and if the manufacturer is right. Do the data support the manufacturer’s claim?
- (3 marks) Prove that, if is an SRS from an , . Justify each piece and its independence.
- (3 marks) The two measurements of each laptop are not independent. Call the difference (brightness 50 % minus brightness 100 %) and justify that . Compute its value if and .
- (3 marks) Prove that if , then , and check in R, with the from part 1, that .
- (3 marks) Simulate 10,000 samples of 10 laptops from an , compute the statistic from part 1 in each and compare its 97.5 % quantile with
qt(0.975, 9)andqnorm(0.975). Draw the histogram with the density on top.
Exercise 3: two printers in the library (15 marks)
The library has an old printer (A) and a new one (B). The time per page, in seconds, is normal for both. The two samples are independent: 13 jobs on A and 9 on B.
- (3 marks) The technician says A’s variance is double B’s: . Give the distribution of and find a and b, with the same probability in each tail, such that .
- (3 marks) Prove that if , then , and from there that . Check in R that the a from part 1 can be computed without the left tail.
After a service, the technician says both printers now have the same standard deviation. These are the times measured afterwards:
A <- c(7.7, 10.1, 7.2, 8.7, 10.5, 7.9, 8.1, 7.9, 8.3, 8.8, 10.0, 7.7, 7.4) # 13 jobs B <- c(7.5, 6.5, 7.5, 7.0, 5.3, 8.0, 7.4, 8.7, 8.7) # 9 jobs
- (3 marks) With , prove that , with the pooled variance.
- (3 marks) With the data, compute and the statistic from part 3 if , and the probability . Do the two printers look equally fast?
- (3 marks) Is the data ratio compatible with ? Answer with the central 90 % interval of the relevant F and with
pf.
Bonus (2.5 marks)
Let , with . Using that Z and Y are independent, prove that . (Hint: compute for a with the gamma integral.)
Solutions
Exercise 1
Part 1. The population is normal, so the mean is exactly normal: .
1 - pnorm(2.6, 2.4, 0.5 / sqrt(16)) # P(mean > 2.6)
Output:
[1] 0.05479929
5.48 % of the time the mean of 16 loads exceeds 2.6 seconds.
Part 2. Each , its square is a and the 16 are independent: the sum is a (μ and σ known: 16 degrees, not 15). q is its 95 % quantile:
qchisq(0.95, 16)
Output:
[1] 26.29623
.
Why 16 degrees and not 15. Before looking up the quantile, look at what is inside the brackets: , with the population mean, not with . Since μ and σ are given in the statement, nothing is estimated and no degree is lost: 16 squares of standard normals, 16 degrees. The obvious path, “it looks like , so “, gives qchisq(0.95, 15) = 25.00.
Check you’ve got it: what if the sum used the sample mean instead of 2.4?
Then it really is 15 degrees: the sum is , the of part 3. When you use , the 16 deviations add up to zero and one of them is fixed by the others. You would get .
Part 3. With and , :
With 5 % in each tail, and of the . Solve by going back:
a <- 0.25 * qchisq(0.05, 15) / 15 b <- 0.25 * qchisq(0.95, 15) / 15 round(c(a = a, b = b), 2)
Output:
a b 0.12 0.42
. Notice the interval is not symmetric about : the chi-square is skewed.
How to set up part 3. All you know about is that is a . So the path is to translate the event about into one about U (multiply all three sides by , which is positive, so the inequalities don’t flip), find the quantiles there and undo the change. An interval “0.25 plus or minus something” doesn’t work: the chi-square is not symmetric.
Using the quantiles 0.025 and 0.975 out of habit. With 90 % in the middle there is 5 % in each tail: 0.05 and 0.95. With the others you get , the 95 % interval.
Part 4.
1 - pchisq(15 * 0.4 / 0.25, 15)
Output:
[1] 0.06509349
6.5 %, consistent with part 3: 0.4 is a little below b = 0.42, which leaves 5 % above.
How it is marked. Part 1: the normal with (1), the probability (1). Part 2: justifying (2), the quantile (1); with 15 degrees, −1. Part 3: moving to the (1), the two quantiles (1), solving for a and b (1). Part 4: the change and the result (2).
Exercise 2
Part 1.
bright100 <- c(5.23, 5.72, 4.49, 4.61, 6.64, 4.95, 6.76, 6.20, 5.66, 4.90) n <- length(bright100) t_obs <- sqrt(n) * (mean(bright100) - 6) / sd(bright100) round(c(mean = mean(bright100), s = sd(bright100), t_obs = t_obs), 2) pt(t_obs, n - 1) # P(T <= t_obs) if mu = 6
Output:
mean s t_obs 5.52 0.82 -1.88 [1] 0.04676651
The mean is 5.52 hours and the sample standard deviation 0.82. Since σ is estimated by s, the statistic is a : . If the manufacturer were right, a mean this low would come up less than 5 % of the time: the data do not really support the 6 hours.
Part 2. The three pieces (proof 7 of the topic):
- , because (mean of independent normals).
- (decomposition of the sum of squares and the mgf).
- Z depends only on and Y only on ; in a normal sample and are independent, so Z and Y are too.
By the definition of the t:
Part 3. Each is a difference of two normals from the same laptop, so it is normal; the Ds of different laptops are independent. So is an SRS from an and, by part 2 applied to the Ds, .
bright50 <- c(5.49, 6.15, 4.50, 5.07, 6.79, 5.50, 7.23, 7.06, 6.00, 5.22) d <- bright50 - bright100 t_d <- sqrt(n) * mean(d) / sd(d) # mu_D = 0 round(c(mean_d = mean(d), s_d = sd(d), t_d = t_d), 3) 1 - pt(t_d, n - 1) # P(T >= t_d)
Output:
mean_d s_d t_d 0.385 0.232 5.242 [1] 0.0002666158
Turning brightness down gives on average 0.385 hours more (about 23 minutes), with a standard deviation of the differences of only 0.232. and : if brightness had no effect, this would almost never happen. With paired data, the variation between laptops cancels out when you subtract, and that is why the difference shows so clearly.
Why subtract instead of comparing two samples. The obvious path is the two-sample t for independent samples. But these are the same ten laptops: one with a worn battery lasts little at both brightness levels. That variation between laptops (standard deviations of 0.82 and 0.91) would end up in the denominator and hide the effect of brightness. Subtracting within each laptop removes it, and only the variation of the differences is left, 0.23.
Check you’ve got it: what would you get treating the two columns as independent samples?
With the two-sample t and 18 degrees, and : you would see nothing. And it wouldn’t even be valid, because that t needs independent samples and these are not.
Part 4. , so . (square of a standard normal) and it is independent of Y: it is a ratio of chi-squares over their degrees, . And since , and the t is symmetric:
round(c(with_F = 1 - pf(t_obs^2, 1, n - 1), with_t = 2 * (1 - pt(abs(t_obs), n - 1))), 4)
Output:
with_F with_t 0.0935 0.0935
Both give 0.0935: the F with 1 degree on top is the t with its two tails together.
Part 5.
set.seed(2026)
t_sim <- replicate(10000, { x <- rnorm(10, 6, 0.8); sqrt(10) * (mean(x) - 6) / sd(x) })
round(c(q975 = unname(quantile(t_sim, 0.975)), t9 = qt(0.975, 9), normal = qnorm(0.975)), 3)
hist(t_sim, freq = FALSE, breaks = 50, main = "", xlab = "t")
curve(dt(x, 9), add = TRUE, lwd = 2, col = "red")
Output:
q975 t9 normal 2.300 2.262 1.960
The 97.5 % quantile of the 10,000 simulated statistics sits next to the one and far from the normal’s 1.96. In the picture, the histogram follows t(9) in the tails too: there are more values beyond ±3 than a normal would allow.
Why sd(x) inside replicate. The whole part depends on one line: the statistic is computed with the standard deviation of each simulated sample, not with the population’s 0.8. It is that s, changing from sample to sample, that fattens the tails. If you put 0.8, each value is an exact standard normal: with the same seed, the 97.5 % quantile comes out at 2.01, right next to the normal’s 1.96 and far from the 2.26 of .
How it is marked. Part 1: mean and s (1), the statistic with s (1), the probability with pt and 9 degrees and the conclusion (1). Part 2: the normal (1), the chi-square (1), independence and the ratio (1). Part 3: justifying that the Ds are a normal SRS (1.5), the calculation (1.5). Part 4: the proof (2), the check (1). Part 5: simulation with the sample inside (1), quantiles (1), histogram with freq = FALSE and the curve (1).
Exercise 3
Part 1. By proof 8, . With , that is , so
with and of the :
a <- 2 * qf(0.05, 12, 8); b <- 2 * qf(0.95, 12, 8) round(c(a = a, b = b), 3)
Output:
a b 0.702 6.568
.
How to set up part 1. The statement doesn’t give you any variance, only the relation between them. That’s fine: in the ratio from proof 8 the σs only appear as , so knowing it equals 2 is enough. What you can’t do is look up a and b directly in the , as if the variances were equal: you would get , which are exactly the quantiles of part 5, where the technician does say they are equal. The factor 2 is the only thing that tells the two parts apart.
Part 2. If , with and independent, then . For the quantiles:
round(c(a_direct = 2 * qf(0.05, 12, 8), a_no_left_tail = 2 / qf(0.95, 8, 12)), 3)
Output:
a_direct a_no_left_tail
0.702 0.702
Both give 0.702: qf(0.05, 12, 8) is the same as 1 / qf(0.95, 8, 12) (degrees swapped and tail swapped).
Part 3. It is proof 9: , which standardised is an N(0, 1); by the sum of independent chi-squares; the two are independent (one depends on the means and the other on the variances). The ratio of the normal over the root of the chi-square over 20 gives the expression in the question, in which σ cancels: .
Part 4.
A <- c(7.7, 10.1, 7.2, 8.7, 10.5, 7.9, 8.1, 7.9, 8.3, 8.8, 10.0, 7.7, 7.4) B <- c(7.5, 6.5, 7.5, 7.0, 5.3, 8.0, 7.4, 8.7, 8.7) m <- length(A); n <- length(B) sp <- sqrt(((m - 1) * var(A) + (n - 1) * var(B)) / (m + n - 2)) t_obs <- (mean(A) - mean(B)) / (sp * sqrt(1 / m + 1 / n)) round(c(mean_A = mean(A), mean_B = mean(B), sp = sp, t_obs = t_obs), 3) 2 * (1 - pt(abs(t_obs), m + n - 2)) # P(|T| >= |t_obs|)
Output:
mean_A mean_B sp t_obs 8.485 7.400 1.076 2.324 [1] 0.03080616
The old one takes 8.48 s per page on average and the new one 7.40. With , and : if they were equally fast, a difference like this would come up 3 % of the time. The new one does look faster.
Why two tails and 20 degrees. The question is whether they are equally fast, without saying which one you suspect is slower: so you add both tails (with one you would get 0.015, half). There are degrees because each sample loses one when estimating its mean. And mind the order: the pooled t is only valid if the two σs are equal, which is exactly what part 5 checks.
Part 5. With , :
f_obs <- var(A) / var(B) round(c(f_obs = f_obs, q05 = qf(0.05, 12, 8), q95 = qf(0.95, 12, 8), p = pf(f_obs, 12, 8)), 3)
Output:
f_obs q05 q95 p 1.024 0.351 3.284 0.497
The ratio is 1.024 and the central 90 % of the goes from 0.351 to 3.284: it is inside, and well centred (it leaves 49.7 % below). The data are compatible with equal σs, which is what part 4 needed.
Check you’ve got it: does the conclusion change if you put B in the numerator?
No. The ratio becomes and the F to use is , with the degrees swapped too. Its central 90 % goes from 0.30 to 2.85, so 0.98 is still inside, and pf gives 0.503. What you can’t do is change only one of the two things: the ratio or the degrees.
How it is marked. Part 1: the F with (12, 8) and the factor 2 (1.5), a and b (1.5). Part 2: the reciprocal (1.5), the quantile rule and the check (1.5). Part 3: normal (1), chi-square with 20 degrees (1), independence and ratio (1). Part 4: (1), the statistic (1), the two-tailed probability with 20 degrees (1). Part 5: the quantiles (1.5), the conclusion (1.5).
Bonus
By the independence of Z and Y, , because . For :
The integral is that of a gamma with shape (which is why is needed). Since :
k <- 9 c(E_1_over_Y = integrate(function(y) dchisq(y, k) / y, 0, Inf)$value, formula = 1 / (k - 2)) c(var_t = integrate(function(x) x^2 * dt(x, k), -Inf, Inf)$value, formula = k / (k - 2))
Output:
E_1_over_Y formula 0.1428571 0.1428571 var_t formula 1.285714 1.285714
With 9 degrees, and the variance of is , more than the normal’s: those are the heavy tails.
Why go through . The obvious path, integrating times the density of the t, is a long calculation. The independence of Z and Y avoids it: the expectation of the product splits, you already know , and all that’s left is the integral of times a gamma density. That integral only converges if : with 1 or 2 degrees, the t has no finite variance.
What’s wrong here?
Three fragments with one mistake each. Before reading the explanation, try to find it.
Fragment 1. With the 10 laptops from exercise 2 (, ), the value that leaves 2.5 % in each tail of the statistic:
qnorm(0.975)
Output:
[1] 1.959964
The standard deviation comes from the sample, so the statistic is a , not a normal. The right value is qt(0.975, 9) = 2.262. With 1.96, everything you build on top (a bound, an interval) will be too narrow.
Ask where σ comes from. If it is a number in the question (“standard deviation 0.5”), normal. If you compute it with sd(), t with .
Fragment 2. The value b such that for 10 laptops, if :
b <- 0.8^2 * qchisq(0.95, 10) / 9 round(b, 3)
Output:
[1] 1.302
With 10 data, : the quantile is the ‘s, not the ‘s. The odd thing is that the 9 in the denominator is right, so the mistake doesn’t jump out. The right one: 0.8^2 * qchisq(0.95, 9) / 9 = 1.203, not 1.302.
In the picture, the two chi-squares: the 95 % quantile of sits to the right of the ‘s, and that is why b comes out too large.
Fragment 3. The one from my mock: a and b such that with 13 data on top, 9 below and equal σs:
round(c(a = qf(0.05, 8, 12), b = qf(0.95, 8, 12)), 3)
Output:
a b 0.305 2.849
The degrees go in the order of the ratio: on top , below . The right ones are qf(0.05, 12, 8) = 0.351 and qf(0.95, 12, 8) = 3.284. With the degrees swapped, b comes out 2.85 instead of 3.28: the interval falls short at the top and a ratio of 3 would be taken as unusual when it isn’t.
Swapping the F’s degrees “because it doesn’t matter”. It only doesn’t matter if you also invert the ratio and switch tails: .
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 sampling 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.
With 10 laptops, the mean and the standard deviation come from the sample (sd()). Which quantile leaves 2.5 % in each tail of the mean’s statistic?
qt(0.975, 9), which is 2.262qnorm(0.975), which is 1.96qt(0.975, 10)qchisq(0.975, 9)
Solution
Answer: qt(0.975, 9), which is 2.262
Ask yourself where σ comes from: if it is a number in the problem, normal; if you compute it with sd(), t with n − 1.
10 laptops, normal population with σ = 0.8. What value b satisfies P(S² > b) = 0.05? (3 decimals.)
round(0.8^2 * qchisq(0.95, 9) / 9, 3)
Solution
Answer: 1.203
9S²/σ² ~ χ²(9), so b = σ² · q_0.95(χ²(9)) / 9 = 0.64 · 16.92 / 9 = 1.203. With χ²(10) you would get 1.302: snippet 2’s degrees-of-freedom mistake.
13 values in sample 1 and 9 in sample 2, equal σ. What distribution does S₁²/S₂² have?
- F(12, 8)
- F(8, 12)
- F(13, 9)
- t(20)
Solution
Answer: F(12, 8)
Swapping the degrees «because it doesn’t matter» only works if you also invert the ratio and change tail: q_0.95(8, 12) = 1/q_0.05(12, 8).
What does it print? (3 decimals.)
round(qf(0.95, 12, 8), 3)
Solution
Answer: 3.284
It is the mock exam’s b with the degrees the right way round: F(12, 8) leaves 5 % above 3.284.
Population with σ = 2 and a sample of n = 16. What is the standard deviation of the sample mean?
2 / sqrt(16)
Solution
Answer: 0.5
Var(mean) = σ²/n, so its standard deviation is σ/√n = 2/4 = 0.5.
Normal population. Match each statistic with its distribution:
| Sample mean, σ known | |
| (n − 1)S²/σ² | |
| √n(mean − μ)/S | |
| (S₁²/σ₁²)/(S₂²/σ₂²) |
Definitions:
- N(μ, σ/√n)
- F(m − 1, n − 1)
- t(n − 1)
- N(0, σ²)
- χ²(n − 1)
Solution
- Sample mean, σ known → N(μ, σ/√n)
- (n − 1)S²/σ² → χ²(n − 1)
- √n(mean − μ)/S → t(n − 1)
- (S₁²/σ₁²)/(S₂²/σ₂²) → F(m − 1, n − 1)
It is the cheat sheet’s «The essentials in 30 seconds». Also, in a normal population the mean and S² are independent.
True or false?
- E(S²) = σ² (that is why S² divides by n − 1).
- A simple random sample is n independent, identically distributed variables.
- With more data, the sample mean has more variance.
- Student’s t looks more and more like N(0, 1) as the degrees of freedom grow.
Solution
- T — E(S²) = σ² (that is why S² divides by n − 1).
- T — A simple random sample is n independent, identically distributed variables.
- F — With more data, the sample mean has more variance.
- T — Student’s t looks more and more like N(0, 1) as the degrees of freedom grow.
Var(mean) = σ²/n goes down as n grows. And the t has heavier tails than the normal, which thin out with more degrees.
You have computed a χ²(9) quantile for the variance. What is left to give b for S²?
- Solving back to the original statistic: b = σ² · quantile / (n − 1)
- Nothing: the quantile is already b
- Taking its square root
- Dividing by n
Solution
Answer: Solving back to the original statistic: b = σ² · quantile / (n − 1)
P(9S²/0.64 > q) = 0.05 ⇔ P(S² > 0.64 · q / 9) = 0.05.
What does it print? (3 decimals.)
round(qt(0.975, 9), 3)
Solution
Answer: 2.262
With 9 degrees of freedom, the t leaves 2.5 % above 2.262: more than 1.96, because estimating σ adds uncertainty.
For the mean, when do you use the normal and when the t?
- Normal if σ is given in the problem; t with n − 1 if σ is estimated from the data
- Normal with large n and t with small n, always
- t if the population is normal; normal otherwise
- Always the t, which is safer
Solution
Answer: Normal if σ is given in the problem; t with n − 1 if σ is estimated from the data
The first question of «Before you hand in»: is σ from the problem or from the data?
Try it in the R playground
Open the R playground on the site (or RStudio) and paste this script. It checks three answers from the exercises by simulation:
set.seed(2026) m <- 10000 # Exercise 1, part 3: P(0.12 <= S^2 <= 0.42) with 16 loads N(2.4, 0.5) s2 <- replicate(m, var(rnorm(16, 2.4, 0.5))) mean(s2 >= 0.25 * qchisq(0.05, 15) / 15 & s2 <= 0.25 * qchisq(0.95, 15) / 15) # close to 0.90 # Exercise 3, part 1: S_A^2 / S_B^2 with sigma_A^2 = 2 sigma_B^2 r <- replicate(m, var(rnorm(13, 0, sqrt(2))) / var(rnorm(9, 0, 1))) mean(r >= 2 * qf(0.05, 12, 8) & r <= 2 * qf(0.95, 12, 8)) # close to 0.90 # Fragment 2: the right b and the wrong one v <- replicate(m, var(rnorm(10, 6, 0.8))) c(right = mean(v > 0.8^2 * qchisq(0.95, 9) / 9), wrong = mean(v > 0.8^2 * qchisq(0.95, 10) / 9))
Things to look at:
- That both interval proportions come out close to 0.90.
- That the right b leaves about 5 % of the variances above it and the wrong one quite a bit less.
- Replace
sqrt(2)with1in exercise 3: with equal σs, the interval computed for double the variance no longer hits 90 %.
Cheat sheet: sampling distributions
The essentials in 30 seconds. SRS = iid. , , . Normal population: ; , independent of ; ; .
| I want… | Result | In R |
|---|---|---|
| , σ known | pnorm(x, mu, sigma / sqrt(n)) | |
| Quantile of | qchisq(p, n) | |
1 - pchisq((n - 1) * c / sigma^2, n - 1) | ||
| b with | solve | sigma^2 * qchisq(1 - alpha, n - 1) / (n - 1) |
| a, b with | equal tails | sigma^2 * qchisq(c(alpha/2, 1 - alpha/2), n - 1) / (n - 1) |
| with S | pt(t, n - 1) | |
| Two-tailed t value | qt(1 - alpha/2, n - 1) | |
| Ratio of variances | qf(p, m - 1, n - 1), pf(x, m - 1, n - 1) | |
| a, b for with | r * qf(c(alpha/2, 1 - alpha/2), m - 1, n - 1) | |
| Two means, common σ | with | sp <- sqrt(((m-1)*var(x) + (n-1)*var(y)) / (m+n-2)) |
| Paired | with the differences | d <- x - y; sqrt(n) * mean(d) / sd(d) |
Properties of the families:
| Distribution | Mean | Variance | Other |
|---|---|---|---|
| k | 2k | gamma(k/2, 1/2); mgf ; they add up | |
| 0 (k > 1) | (k > 2) | symmetric; t(1) Cauchy; | |
| (n > 2) | — | ; |
Symptom / likely cause:
| Symptom | Likely cause |
|---|---|
| The b for comes out too large | qchisq(…, n) instead of n - 1 |
| The b for comes out smaller than σ² for a right tail | You didn’t go back multiplying by |
| An F interval falls short at the top | Degrees of freedom swapped |
| “Too small” probabilities for the mean with few data | pnorm with s instead of pt |
| The curve doesn’t fit the simulated histogram | Degrees of freedom; or freq = FALSE is missing |
Before you hand in, ask yourself:
- Does σ come from the question (normal, chi-square with n) or from the data (t, chi-square with n − 1)?
- How many degrees of freedom does each distribution have, and why?
- In an F, which sample goes on top?
- Have I solved by going back to the original statistic (, not the chi-square)?
For your script
The topic’s code, commented, for your exam .R file. Your own functions and the code for the solved exercises, without the questions:
# ===== Topic 8: sampling distributions (chi^2, t, F) =====
# --- Sample mean (sigma known) ---
p_mean <- function(x, mu, sigma, n, tail = "left") # P(mean <= x) or P(mean > x)
if (tail == "left") pnorm(x, mu, sigma / sqrt(n)) else 1 - pnorm(x, mu, sigma / sqrt(n))
# --- Sample variance: (n - 1) S^2 / sigma^2 ~ chi^2(n - 1) ---
p_s2_above <- function(c, sigma, n) 1 - pchisq((n - 1) * c / sigma^2, n - 1) # P(S^2 > c)
b_s2 <- function(alpha, sigma, n) sigma^2 * qchisq(1 - alpha, n - 1) / (n - 1) # P(S^2 > b) = alpha
ab_s2 <- function(conf, sigma, n) { # P(a <= S^2 <= b) = conf, equal tails
al <- 1 - conf
sigma^2 * qchisq(c(a = al / 2, b = 1 - al / 2), n - 1) / (n - 1)
}
# --- Sum of squares with mu and sigma known: chi^2(n) ---
q_sum_squares <- function(p, n) qchisq(p, n)
# --- One-sample t (sigma estimated) ---
t_one <- function(x, mu0) sqrt(length(x)) * (mean(x) - mu0) / sd(x) # ~ t(n - 1)
t_paired <- function(x, y, mu_d = 0) t_one(x - y, mu_d) # the differences
# --- Two samples: F and pooled-variance t ---
ab_ratio <- function(conf, m, n, r = 1) { # P(a <= S1^2/S2^2 <= b), sigma1^2 = r sigma2^2
al <- 1 - conf
r * qf(c(a = al / 2, b = 1 - al / 2), m - 1, n - 1) # m = size of the TOP sample
}
sp <- function(x, y) sqrt(((length(x) - 1) * var(x) + (length(y) - 1) * var(y)) / (length(x) + length(y) - 2))
t_two <- function(x, y, dif0 = 0) # ~ t(m + n - 2) if equal sigmas
((mean(x) - mean(y)) - dif0) / (sp(x, y) * sqrt(1 / length(x) + 1 / length(y)))
p_two_tailed_t <- function(t, df) 2 * (1 - pt(abs(t), df)) # P(|T| >= |t|)
# --- Check a distribution by simulation (the sample is made INSIDE) ---
check_dist <- function(statistic, m = 10000, density, ...) {
v <- replicate(m, statistic())
hist(v, freq = FALSE, breaks = 50, main = "")
curve(density(x, ...), add = TRUE, col = "red", lwd = 2)
invisible(v)
}
# example: check_dist(function() 14 * var(rnorm(15, 40, 6)) / 36, density = dchisq, df = 14)
# --- Solved exercises ---
# Virtual Campus: N(2.4, 0.5), 16 loads
p_mean(2.6, 2.4, 0.5, 16, "right") # 0.05
q_sum_squares(0.95, 16) # 26.30
round(ab_s2(0.90, 0.5, 16), 2); p_s2_above(0.4, 0.5, 16) # 0.12, 0.42 and 0.07
# Battery: one-sample t and paired
bright100 <- c(5.23, 5.72, 4.49, 4.61, 6.64, 4.95, 6.76, 6.20, 5.66, 4.90)
bright50 <- c(5.49, 6.15, 4.50, 5.07, 6.79, 5.50, 7.23, 7.06, 6.00, 5.22)
pt(t_one(bright100, 6), 9) # 0.047
1 - pt(t_paired(bright50, bright100), 9) # 0.0003
# Printers: F with sigma_A^2 = 2 sigma_B^2 and two-sample t
round(ab_ratio(0.90, 13, 9, r = 2), 3) # 0.702 and 6.568
A <- c(7.7, 10.1, 7.2, 8.7, 10.5, 7.9, 8.1, 7.9, 8.3, 8.8, 10.0, 7.7, 7.4)
B <- c(7.5, 6.5, 7.5, 7.0, 5.3, 8.0, 7.4, 8.7, 8.7)
p_two_tailed_t(t_two(A, B), 13 + 9 - 2) # 0.031
pf(var(A) / var(B), 12, 8) # 0.497
The numbers in the comments are the solutions’: if you get something else when you paste it, check the data.
What next?
With sampling distributions you close the block on the normal and the limit theorems. The next ME1 topic is point estimation: how to get a good value for an unknown parameter from a sample, with the method of moments and maximum likelihood. There and stop being just statistics and become estimators, and what you saw today (their mean, their variance and their distribution) is exactly what you need to decide whether they are good. And in confidence intervals you will use qchisq, qt and qf exactly as here.
If you want to review first, you have the theory, the proofs and the practice for this topic, and the limit theorems, where the normal of the mean comes from when the population is not normal.
Keep goingAll Statistics articlesDid 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)


