The normal distribution — R practice with 10,000 gofio packets, a scale and boxes of 12 (and the curve stuck to the floor)
The first time I wanted to see whether my simulated packets were normal, I drew the histogram with hist(weights), put curve(dnorm(x, 1010, 6), add = TRUE) on top and the curve came out as a line stuck to the floor. I thought dnorm was broken. It was not: the histogram was in frequencies, with bars of 700 packets, and the density never goes above 0.07. On the same scale, the bell was as tall as the thickness of the axis line. I was missing freq = FALSE, which puts the bars on the density scale (total area 1) so they can be compared with the curve. That argument shows up in all three programs today.
This practice is the R side of the normal distribution theory (and of its proofs). There are three medium-level programs with the cooperative’s gofio filling machine, : the first simulates 10,000 packets and uses them to check pnorm, qnorm and the 68-95-99.7 rule; the second takes 60 weighings from a scale, which is what you would have in an exam, and decides whether they look like a normal; the third adjusts the machine and fills boxes of 12. The scale data are made up, but with what real data have: they are not perfectly symmetric and the mean is not exactly what the manufacturer says.
Table of Contents
Before you start
You should be clear on:
- That is written in R with the standard deviation:
dnorm(x, mean, sd),pnorm(q, mean, sd),qnorm(p, mean, sd)andrnorm(n, mean, sd). - That
pnormgives the area on the left andqnormtakes an area on the left. - That standardising, , does not change probabilities, and that the 68-95-99.7 rule holds for any normal.
- That the sum of independent normals is normal, with the means added and the variances added.
If any of these sounds shaky, go over the theory before carrying on.
Every program that simulates starts with set.seed(2026). That way your numbers match the ones here. If you remove the seed, the last digits change, but the conclusions do not.
Program 1: ten thousand simulated packets
We simulate with rnorm what the machine would produce over a long shift and check, with the data, what the theory computes with formulas: the proportion of packets under 1 kg, the 68-95-99.7 rule and the quantiles. What makes it tricky is the scale of the histogram (the anecdote at the start) and telling simulation noise apart from a mistake.
The idea
Simulation is a lab: if the model is , the proportion of simulated packets meeting a condition must look like the probability pnorm gives, and the quantiles of the data (quantile) like those of qnorm. They will not be equal, because 10,000 packets are many but not infinite; the difference should be small. The plan:
- Simulate and look at a few values, the mean and the standard deviation.
- Compare the simulated proportion and the theoretical probability of .
- Count how many fall within 1, 2 and 3 deviations of the mean.
- Compare quantiles and draw the histogram with the bell on top.
Put into words: the program asks the machine each question twice. Once to the simulated data, by counting (“how many of these 10,000 packets weigh less than 1000 g?”), and once to the formula (“what area does pnorm leave to the left of 1000?”). If the model and the code are right, the two answers look alike; if one comes out double the other, the third argument is almost always the culprit. Each block of the code is one of those pairs: proportion and pnorm, quantiles and qnorm, histogram and dnorm.
Before coding, think about what mean(weights < 1000) is. weights < 1000 is a vector of TRUE and FALSE; its mean is the proportion of TRUE. It is how you count in R without loops.
The code
# Program 1: ten thousand simulated packets
set.seed(2026) # so the same values always come out
weights <- rnorm(10000, mean = 1010, sd = 6)
head(round(weights, 1)) # the first six packets
round(c(mean = mean(weights), sd = sd(weights)), 2)
# Proportion of packets under 1 kg: simulated versus theoretical
round(c(simulated = mean(weights < 1000), theoretical = pnorm(1000, 1010, 6)), 4)
# The 68-95-99.7 rule in the simulated data
within <- sapply(1:3, function(k) mean(abs(weights - 1010) <= k * 6))
round(within, 4)
# Simulated quantiles versus qnorm
round(quantile(weights, c(0.01, 0.10, 0.90)), 2)
round(qnorm(c(0.01, 0.10, 0.90), 1010, 6), 2)
# Density-scale histogram with the bell on top
hist(weights, freq = FALSE, breaks = 40, col = "grey90",
main = "10,000 packets", xlab = "grams")
curve(dnorm(x, 1010, 6), add = TRUE, lwd = 2, col = "red")
The sapply line is the densest: for each from 1 to 3 it computes abs(weights - 1010) <= k * 6, which is “the packet is less than deviations from the mean”, and its mean, the proportion. sapply gathers the three proportions into a vector.
| Interval | abs(weights - 1010) <= k * 6 counts packets between… | |
|---|---|---|
| 1 | 1004 and 1016 g | |
| 2 | 998 and 1022 g | |
| 3 | 992 and 1028 g |
The comparisons, packet by packet
Before you trust the proportions of 10,000 packets, look at what each comparison does with the first six (the ones from head, here with two decimals). The column is how many standard deviations each one is from 1010:
| Packet | Weight | weights < 1000 | Within 1 deviation (1004-1016) | Within 2 deviations (998-1022) | Why | |
|---|---|---|---|---|---|---|
| 1 | 1013.12 | 0.52 | FALSE | TRUE | TRUE | half a above: it falls in every band |
| 2 | 1003.52 | −1.08 | FALSE | FALSE | TRUE | it misses 1004 by half a gram: outside the first band |
| 3 | 1010.84 | 0.14 | FALSE | TRUE | TRUE | almost at the mean |
| 4 | 1009.49 | −0.08 | FALSE | TRUE | TRUE | the same, half a gram below |
| 5 | 1006.00 | −0.67 | FALSE | TRUE | TRUE | 4 g below, less than one |
| 6 | 994.90 | −2.52 | TRUE | FALSE | FALSE | the only short one: more than two below; it only gets into the 3 band (992-1028) |
With these six, mean(weights < 1000) would give 1/6 = 0.17, and the three bands 4/6 = 0.67, 5/6 = 0.83 and 6/6 = 1. With all 10,000, on the other hand, the first question gets 460 TRUE and the bands get 6842, 9548 and 9979: those are the 0.0460 and the 0.6842, 0.9548 and 0.9979 in the output. With six packets the numbers jump around; with ten thousand they settle close to the theory.
Output
[1] 1013.1 1003.5 1010.8 1009.5 1006.0 994.9
mean sd
1010.02 6.01
simulated theoretical
0.0460 0.0478
[1] 0.6842 0.9548 0.9979
1% 10% 90%
996.18 1002.28 1017.76
[1] 996.04 1002.31 1017.69
How to read it
1013.1 1003.5 1010.8 1009.5 1006.0 994.9: the first six packets. Almost all close to 1010, and one of 994.9 g, which is deviations away: unusual, but among 10,000 packets a few like that have to turn up.mean 1010.02,sd 6.01: the model’s parameters, recovered from the data up to noise. It is the first check of any simulation.simulated 0.0460,theoretical 0.0478: 4.60 % of the simulated packets weigh less than 1 kg; the model says 4.78 %. The difference, 0.18 points, is the randomness of simulating: with another seed, the simulated value would move a few tenths above or below 4.78.0.6842 0.9548 0.9979: the 68-95-99.7 rule in the data. The exact values are 0.6827, 0.9545 and 0.9973.- Quantiles: the lightest 1 % weigh less than 996.18 g (theoretical, 996.04), the lightest 10 %, less than 1002.28 (1002.31), and the heaviest 10 %, more than 1017.76 (1017.69). The extreme quantiles (the 1 %) move more than the central ones, because they rest on few data: only 100 packets lie below.
In the drawing, the histogram the last two lines make: one-gram bars, on the density scale, and the red bell on top. The bars follow the curve, with small teeth above and below that are the noise. The tallest bar (between 1010 and 1011 g) is 0.0702 and the theoretical peak is 0.0665: with 10,000 data points, a one-gram bar holds about 700 packets, and a few extra already show in its height.
Questions you might ask
Why freq = FALSE? Because hist by default puts on the vertical axis how many data points fall in each bar (frequencies). The density is on another scale: the bar areas only add up to 1 if the height is “proportion divided by width”. Without freq = FALSE, the dnorm curve is squashed against the axis, as in the anecdote.
Why does breaks = 40 give one-gram bars? breaks = 40 does not force 40 bars: it is a suggestion, and hist picks “nice” cut points near that number. With weights between 987 and 1034, you get 47 one-gram bars. If you want control, pass the cut points: breaks = seq(985, 1035, by = 1).
How many packets would it take for the simulated value to match the theoretical one? It never matches exactly, but the standard error of a simulated proportion is : with and , about 0.002. Getting 0.0460 instead of 0.0478 is a difference of less than one such error. With a million packets, the error drops to 0.0002. You will see it in detail in topic 7.
Why abs(weights - 1010) and not abs(weights - mean(weights))? Because we want to check the model, . With mean(weights) you would get almost the same, but you would be checking the data against themselves. In program 2, where we do not know , we will have no choice but to use the mean of the data.
Writing rnorm(10000, 1010, 36) because you were told “variance 36”. You would get a cloud of packets six times wider (between 900 and 1120 g), and sd(weights) would give 36. If the simulated standard deviation is not what you expected, look at the third argument.
Try it yourself
- Change
10000to100and run it again: compare the simulated proportion under 1 kg with 0.0478 and notice how the histogram shape breaks down. - Simulate the new machine, , and repeat the comparison: how many packets under one kilo do you get now? (The theory says
pnorm(1000, 1004, 4)= 0.1587.)
Program 2: are the 60 weighings from the scale normal?
Now we do not simulate: we have 60 packets weighed on the cooperative’s scale one morning. We do not know or ; we estimate them with the mean and standard deviation of the data. Then we decide whether a normal describes those weighings well, with two plots: the histogram with the bell on top and the quantile-quantile plot. What makes it tricky is that with 60 data points no plot comes out “perfect”, and you have to know how to tell what is normal from what is worrying.
The idea
- Write the data in a vector (in the exam they would come in a
.csvor.rdatafile). - Estimate with
meanand withsd. - Density-scale histogram and, on top, the normal with those parameters.
qqnorm: if the data are normal, the points fall close to a straight line;qqlinedraws that line.- Compare what the data say and what the model says on two specific questions.
In words, this program goes the other way round from the first one. There we knew the model and checked whether the data followed it; here there are only data. First we get a candidate from them (the normal with their mean and standard deviation) and then we ask whether it is believable. The histogram shows the shape at a glance, but it changes depending on where you cut the bars. The quantile-quantile plot has no bars: it looks at the data one by one, and that is why it is the one that gives the tails away.
Before coding, think about what the quantile-quantile plot does: it sorts the 60 data points from smallest to largest and puts each one against the standard normal quantile it “should” have given its position. If the data are a stretched and shifted normal, the points form a straight line with slope and height at 0.
The code
# Program 2: are the 60 weighings from the scale normal?
weighings <- c(1012.3, 1004.8, 1009.1, 1006.9, 1015.5, 1009.4, 1006.9, 1007.8, 1004.1, 1011.7,
1009.5, 1010.1, 1008.4, 1011.4, 1006.3, 1006.6, 1001.7, 1004.2, 1013.7, 1005.9,
1011.3, 1007.5, 1017.0, 1008.4, 1002.0, 1011.0, 1014.9, 1010.9, 1005.5, 1008.2,
1018.4, 1003.9, 1004.7, 1006.4, 1008.4, 1005.3, 1009.0, 998.2, 1013.2, 1016.5,
1007.1, 1004.3, 1005.7, 1006.9, 1005.7, 998.4, 1011.1, 1011.6, 1017.4, 1004.5,
1001.9, 1005.4, 1000.1, 1005.3, 1011.0, 1004.2, 1005.2, 1014.2, 1005.3, 1005.0)
m <- mean(weighings); s <- sd(weighings) # we estimate mu and sigma from the data
round(c(n = length(weighings), mean = m, sd = s), 2)
summary(weighings)
# Histogram with the fitted normal on top
hist(weighings, freq = FALSE, breaks = seq(996, 1020, by = 3), col = "grey90",
main = "60 weighings", xlab = "grams")
curve(dnorm(x, m, s), add = TRUE, lwd = 2, col = "red")
# Quantile-quantile plot: points on the line = normal
qqnorm(weighings); qqline(weighings, col = "red", lwd = 2)
# What the model says versus what the data say
round(c(data = mean(weighings < 1000), model = pnorm(1000, m, s)), 4)
round(c(data = mean(abs(weighings - m) <= s), model = pnorm(1) - pnorm(-1)), 4)
Output
n mean sd 60.00 1007.96 4.54 Min. 1st Qu. Median Mean 3rd Qu. Max. 998.2 1005.1 1007.0 1008.0 1011.0 1018.4 data model 0.0333 0.0399 data model 0.7500 0.6827
How to read it
n 60,mean 1007.96,sd 4.54: the scale says the machine puts in 1007.96 g on average, a couple of grams less than the manufacturer’s 1010, with a standard deviation of 4.54 g, better than the catalogue’s 6. They are the estimates of and (in topic 9 you will see why these and not others).summary: minimum 998.2, quartiles 1005.1 and 1011.0, median 1007.0 and maximum 1018.4. The mean (1008.0) is one gram above the median (1007.0): there is a slight tail to the right, a few quite heavy packets (1017.0, 1017.4, 1018.4).data 0.0333,model 0.0399: on the scale, 2 of the 60 packets (3.33 %) weigh less than 1 kg; the fitted normal model says 3.99 %. With 60 data points, one packet more or less moves the proportion by 1.67 points: the difference says nothing against the model.data 0.7500,model 0.6827: 75 % of the weighings are less than one standard deviation from the mean; the normal would say 68.27 %. There is a bit more data in the center than the bell asks for, which is what happens when one tail is longer than the other.
In the histogram, the 3 g bars follow the red bell: they rise up to the 1005-1008 g one (0.10, the tallest, above the curve) and fall. Those at 1008-1011 and 1011-1014 are slightly below, and the last two, from 1014 to 1020, slightly above: more data in the center-left and a slightly longer right tail, which was already visible in summary.
In the quantile-quantile plot, each point is a weighing: the lightest on the left, the heaviest on the right. The points in the middle fall on the red line; those on the left too, and the five on the right curve upwards: the heaviest packets are somewhat heavier than a normal would predict. It is the right tail, seen another way. The qqline line goes through the quartiles of the data (1005.1 and 1011.0) and has slope 4.36, close to the standard deviation of the data.
The quantile-quantile plot, point by point
qqnorm does three things with each weighing: it puts it in its place (from 1, the lightest, to 60), gives it the proportion that place deserves, , and looks up the standard normal quantile with that proportion. That quantile is the horizontal coordinate; the weighing, the vertical one. qqline draws the line through the two quartiles: those of the data (1005.15 and 1011.025) against those of the standard normal (−0.67 and 0.67). Its slope is and at 0 it is worth 1008.09. This is how some of the points come out:
| Place | Weighing | Proportion | Theoretical | Line: | Weighing − line | Why |
|---|---|---|---|---|---|---|
| 1 | 998.2 | 0.5/60 | −2.39 | 997.66 | 0.54 | the lightest, at the far left; half a gram above: on the line |
| 2 | 998.4 | 1.5/60 | −1.96 | 999.55 | −1.15 | a bit below; a gram up or down is the noise of 60 data |
| 30 | 1006.9 | 29.5/60 | −0.02 | 1008.00 | −1.10 | the center: almost 0 |
| 57 | 1016.5 | 56.5/60 | 1.57 | 1014.92 | 1.58 | right end: 1.58 g more than the line asks for |
| 60 | 1018.4 | 59.5/60 | 2.39 | 1018.51 | −0.11 | the heaviest goes back to the line |
What counts is not a single point but the pattern. From place 55 to 58 the four weighings are more than a gram above the line (1.02, 1.14, 1.58 and 1.37), in a row and on the same side: that is the upward curve at the right end. The line does not go through the mean (1007.96) because qqline uses the quartiles, not the mean or the standard deviation; that is why its slope, 4.36, is close to the standard deviation, 4.54, but not the same.
Check you’ve got it: if the scale had 120 weighings, what theoretical quantile would the lightest one get?
−2.64. Its proportion would be , and qnorm(0.0042) gives −2.64, against the −2.39 of the lightest of 60. With more data, the plot reaches further out at the ends: a large sample has rarer weighings.
Verdict: a normal describes these weighings well in the center and in the left tail, which is the one that matters for the 1 kg label. The small asymmetry on the right does not change the proportion of short packets. If the question were about packets that are too heavy, it would be worth looking with more data.
Questions you might ask
Why sd and not the quasi-variance or the variance? sd is already the square root of the quasi-variance (it divides by ). For , the standard deviation, which is what dnorm and pnorm want. If you used var(weighings) in pnorm, you would make the theory mistake: 20.6 instead of 4.54.
What would the quantile-quantile plot look like if the data were not normal? With right-skewed data (some exponential waiting times, for example), the points form a curve bending upwards at both ends, like a smile. With heavier tails than the normal, an S. With bounded data (proportions), the ends flatten out. An almost perfect line with a few points off at one end, as here, is what you expect from a normal sample of 60.
Why the histogram cut points every 3? With 60 data points, 1 g bars would leave many bars with 0 or 1 data point and the picture would be noise. The Sturges rule R uses by default would give about 7 bars; here the cut points are fixed (from 996 to 1020) so the picture is the same every time and does not depend on R choosing “nicely”.
Isn’t there a test to know whether they are normal? Yes, like the Shapiro-Wilk test (shapiro.test), and you will see it in ME2. In ME1 the exam asks you to draw and explain: “the points of the quantile-quantile plot are on the line except…”, “the histogram is bell-shaped…”.
Reading the quantile-quantile plot the wrong way round. The horizontal axis has the theoretical quantiles (of the standard normal, between −2.4 and 2.4) and the vertical one the data. A point above the line on the right means “this data point is bigger than a normal would expect”, not smaller.
Try it yourself
- Change
breaks = seq(996, 1020, by = 3)tobreaks = 20and tobreaks = 4. In which one would you say the data are normal? And in which not? With few data points, the histogram depends a lot on the cut points; that is why you look at it together with the quantile-quantile plot. - Run
qqnorm(rexp(60, 1 / 8))andqqline(rexp(60, 1 / 8))(exponential times with mean 8) and compare it with the scale’s: that is what a quantile-quantile plot of non-normal data looks like.
Program 3: adjust the machine and fill boxes of 12
With the model validated, the cooperative makes decisions: where to set the machine’s mean so that only 1 % of the packets fall short, how much gofio that costs, how boxes of 12 behave and whether it would be better to buy a more precise machine. What makes it tricky is the reversed quantile (the 1 % on the left) and the standard deviation of the box.
The idea
- Solve for : , so .
- Compute how much is given away (in grams per packet and in kilos per 10,000 packets).
- Simulate 10,000 boxes of 12 packets with
replicateand compare their mean and standard deviation with the theory: and . - Ask about the boxes weighing less than 12 kg and less than 12,150 g.
- Solve for the a machine with mean 1004 would need for the same 1 %.
In words, program 3 turns the first one around. There we gave a weight and asked for an area (pnorm); here we give the area we want, 1 %, and ask where to set the machine (qnorm). Then the variable becomes the box, the sum of 12 packets. Its mean is 12 times the packet’s, but its standard deviation only times, because some packets go over and others fall short, and they partly cancel out.
Before coding, think about the sign: is negative (−2.33), so is greater than 1000. If you get a mean below 1000, you have the quantile the wrong way round.
The code
# Program 3: adjust the machine and fill boxes of 12
sigma <- 6
# 1) Where to set the mean so that only 1 % weigh less than 1000 g?
mu_new <- 1000 - qnorm(0.01) * sigma
round(mu_new, 2)
# What is given away per packet and per 10,000 packets, in kilos
round(c(per_packet = mu_new - 1000, kilos_10000 = (mu_new - 1000) * 10000 / 1000), 2)
# 2) Boxes of 12 packets with the adjusted machine
set.seed(2026)
boxes <- replicate(10000, sum(rnorm(12, mu_new, sigma)))
round(c(mean = mean(boxes), sd = sd(boxes)), 2)
round(c(theor_mean = 12 * mu_new, theor_sd = sqrt(12) * sigma, wrong = 12 * sigma), 2)
# The box says 12 kg: how many weigh less than 12000 g?
c(simulated = mean(boxes < 12000),
theoretical = pnorm(12000, 12 * mu_new, sqrt(12) * sigma))
# And below 12,150 g (1012.5 g per packet on average)
round(c(simulated = mean(boxes < 12150),
theoretical = pnorm(12150, 12 * mu_new, sqrt(12) * sigma)), 4)
hist(boxes, freq = FALSE, breaks = 40, col = "grey90",
main = "10,000 boxes of 12", xlab = "grams")
curve(dnorm(x, 12 * mu_new, sqrt(12) * sigma), add = TRUE, lwd = 2, col = "red")
# 3) What if, instead of raising the mean, we buy a more precise machine?
sigma_new <- (1004 - 1000) / qnorm(0.99) # sigma with mean 1004 and 1 % below
round(sigma_new, 2)
pnorm(1000, 1004, sigma_new)
replicate(10000, sum(rnorm(12, mu_new, sigma))) repeats what is inside 10,000 times: simulate 12 packets and add them up. The result is a vector of 10,000 box weights. It is how you simulate “a variable that is the sum of others” without writing a loop.
Output
[1] 1013.96
per_packet kilos_10000
13.96 139.58
mean sd
12167.66 21.13
theor_mean theor_sd wrong
12167.50 20.78 72.00
simulated theoretical
0.000000e+00 3.855333e-16
simulated theoretical
0.1997 0.1999
[1] 1.72
[1] 0.01
How to read it
1013.96: the machine’s mean has to be set at 1013.96 g. Check by hand: .per_packet 13.96,kilos_10000 139.58: each packet carries 13.96 g extra, and every 10,000 packets, 139.58 kg of gofio given away. That is what it costs to meet the label with a machine.mean 12167.66,sd 21.13versustheor_mean 12167.50,theor_sd 20.78: the simulated boxes weigh what the theory says ( and ), up to noise.wrong 72.00is what you would get by adding the standard deviations: more than three times the real one.0.000000e+00 3.855333e-16: none of the 10,000 simulated boxes weighs less than 12 kg, and the model says the probability is , practically 0. The box is standard deviations away. Even though 1 % of the packets fall short, a whole box almost never does: the short packets are offset by the long ones.simulated 0.1997,theoretical 0.1999: one box in five weighs less than 12,150 g. Here simulation and theory agree in the third decimal.1.72and0.01: a machine with mean 1004 g would need g for the same 1 %. It would give away 4 g per packet instead of 13.96, but it would have to be three and a half times more precise.
The box of 12, step by step
The 0.1999 of boxes under 12,150 g comes from five steps, and each one uses a different property:
| Step | Calculation | Result | Why |
|---|---|---|---|
| Packet mean | 1013.96 | the 1 % quantile is −2.33; subtracting it raises the mean | |
| Box mean | 12,167.5 | means add up: 12 packets, 12 times the mean | |
| Box variance | 432 | with independent packets the variances add up, not the standard deviations | |
| Standard deviation | 20.78 | it is the same as | |
| Standardise 12,150 | −0.84 | the 12,150 g box is 0.84 deviations below the mean | |
| Probability | 0.1999 | the pnorm(12150, …) in the output |
In the drawing, the 10,000 boxes: 5 g bars and, on top, the bell . The peak is at 12,165-12,170 g and almost everything lies between 12,100 and 12,230 g. The 12,000 g line does not even fit in the picture: it is to the left, beyond the edge.
Questions you might ask
Why 1000 - qnorm(0.01) * sigma and not 1000 + qnorm(0.01) * sigma? From you get . Since qnorm(0.01) is negative, subtracting adds. With +, the mean would be 986.04, and half the packets would weigh less than a kilo. 1000 + qnorm(0.99) * sigma also works, by symmetry: .
Why doesn’t the box inherit the packets’ 1 %? Because “the box weighs less than 12 kg” is not “some packet weighs less than 1 kg”. For the box to fall short, the 12 packets together have to lose 167.5 g, and that requires almost all of them to fail at once and in the same direction. It is the same idea that makes the box’s standard deviation and not .
And the probability that at least one packet in the box weighs less than 1 kg? That is another question, and it is binomial (topic 3): each packet fails with probability 0.01, so . In 11.4 % of the boxes there is some short packet, even though the whole box weighs plenty.
Why 12,150? Because it is a threshold where the probability is neither almost 0 nor almost 1, so the comparison between simulated (0.1997) and theoretical (0.1999) says something. With 12,000, both give 0 and you learn nothing from the simulation.
Computing the box’s standard deviation as 12 * sigma (72) instead of sqrt(12) * sigma (20.78). With 72, the probability of a box under 12 kg would be pnorm(12000, 12167.5, 72) = 0.01: you would be inventing a problem the cooperative does not have.
What would happen if the 12 packets in a box were not independent?
Program 3 assumes that each packet deviates on its own. Imagine it does not: the machine drifts a little with each box (a fuller hopper, the humidity of the gofio) and the 12 packets share that drift. Each packet still has , but one part (standard deviation 3) is common to the box and the rest () is its own, because 9 + 27 = 36. Run it after program 3:
# Run after program 3: it uses mu_new and sigma
sigma <- 6
mu_new <- 1000 - qnorm(0.01) * sigma
set.seed(2026)
# Each box has a common drift (sd 3) shared by its 12 packets,
# and each packet its own part (sd sqrt(27)), so that each one still has sd 6
boxes_dep <- replicate(10000, {
common <- rnorm(1, 0, 3) # a single number for the whole box
sum(mu_new + common + rnorm(12, 0, sqrt(27))) # 12 packets with the same drift
})
round(c(mean = mean(boxes_dep), sd = sd(boxes_dep)), 2)
round(c(independent = sqrt(12) * sigma, with_drift = sqrt((12 * 3)^2 + 12 * 27)), 2)
round(c(simulated = mean(boxes_dep < 12150), theoretical = pnorm(12150, 12 * mu_new, sqrt(1620))), 4)
Output:
mean sd
12167.74 40.81
independent with_drift
20.78 40.25
simulated theoretical
0.3332 0.3319
The mean does not change (12,167.74 is 12,167.5 plus noise), but the box’s standard deviation almost doubles: 40.81 in the simulation, 40.25 in the calculation. The common part does not cancel out: if the drift is +3 g, it is +3 g in all 12 packets, and it adds g in one go, like the of the common mistake. Only the packets’ own parts cancel out, and they contribute of variance. Together: . Boxes under 12,150 g go from one in five to one in three (0.3332 against 0.1999). The only holds with independent packets; the topic on joint distributions shows what happens when they are not.
Try it yourself
- Change the 1 % to 0.1 % (
qnorm(0.001)): how many kilos are given away every 10,000 packets now? - Change the boxes of 12 to boxes of 24 and check that the box’s standard deviation becomes , not .
Common mistakes in this practice
- Forgetting
freq = FALSEinhistbefore drawing a density on top: the curve comes out stuck to the axis. - Putting the variance in
rnorm,pnormorqnorm. The third argument issd. Ifsd()of the simulated data is not what you expected, start there. - Mixing up the simulated proportion with the probability. The simulated one changes with the seed and with ; the theoretical one does not. They are similar if the model is right and is large.
- Expecting a perfect line in
qqnormwith few data points. A few points off at the ends are normal; what is worrying is a curve or an S across the whole plot. - Adding standard deviations when simulating or computing boxes. Variances add: .
- Solving for the mean with the quantile the wrong way round. If you get less than 1000 for “only 1 % below 1000”, check the sign.
Try it in the R playground
Open the site’s R playground (or RStudio) and paste this script, which brings together the three programs in small:
set.seed(2026) weights <- rnorm(10000, 1010, 6) c(mean(weights < 1000), pnorm(1000, 1010, 6)) # simulated versus theoretical hist(weights, freq = FALSE, breaks = 40) curve(dnorm(x, 1010, 6), add = TRUE, lwd = 2, col = "red") qqnorm(weights[1:60]); qqline(weights[1:60], col = "red") # 60 simulated packets mu <- 1000 - qnorm(0.01) * 6 # the adjusted machine boxes <- replicate(5000, sum(rnorm(12, mu, 6))) c(sd(boxes), sqrt(12) * 6)
What to look at:
- Remove
freq = FALSEand run the two histogram lines again: it is the curve stuck to the floor from the beginning. - The quantile-quantile plot of 60 simulated packets from a real normal: it is not a perfect line either. Run it several times (without the seed) and notice how much the ends move.
- In the Environment panel,
boxesis a vector of 5000 numbers: tryView(boxes)orsummary(boxes). - Change the 12 in the boxes to 48 and compare
sd(boxes)withsqrt(48) * 6. - Type
?qqnormand look in Help at whatqqlinedoes: which points the line goes through.
Summary
rnorm(n, mean, sd)simulates; the proportion of simulated values meeting a condition approaches the probability frompnorm, andquantileapproachesqnorm, with an error that shrinks like .- To compare a histogram with a density,
hist(…, freq = FALSE)andcurve(dnorm(…), add = TRUE). - With data, and are estimated with
meanandsd, and normality is checked with the histogram andqqnorm+qqline: points on the line, normal; curves or S shapes, not. - Quantiles drive the adjustments: leaves 1 % below 1000. And lowering saves more than raising .
- A box of independent packets has standard deviation ; that is why it almost never inherits the failures of a single packet.
replicate(n, expression)simulates sums, means or anything that repeats, without loops.
Checklist: have you practised it?
- Have you run the three programs and checked the outputs?
- Could you explain why the simulated proportion (0.0460) is not exactly the theoretical one (0.0478)?
- Can you read a quantile-quantile plot: what is on each axis and what a point above the line means?
- Could you solve for a machine’s mean for a given percentage, with the right sign?
- Why is the standard deviation of a box of 12 and not ?
- Have you tried the “Try it yourself” tasks without looking at the theory?
If you answered yes to almost everything, you are ready for the normal distribution exercises, where I no longer give you the code. See you there!
Next step: exercisesThe normal distribution — exam-style exercises with mills, boxes of 12 and half-kilo bags (and the 10 % that was in the wrong tail)Did this article help you? Here is how you can support SergioLearns →



