Practice / Probability for ML
The normal distribution and z-scores
Before you start
The normal distribution is the bell curve: the shape of measurement noise, of averages by the central limit theorem, and of the random weights a network starts from. Every normal is a shifted and stretched copy of one standard normal, so a single table, or a single function , answers every question about all of them once a value is converted to a z-score. These ten problems practice that conversion in both directions, from a value to a probability and from a probability back to a value, then use the closure of normals under linear maps and independent sums, look at the shape of the density itself, approximate a binomial count, and end with two places where the normal is only a model: a mixture of two normals, and an outlier rule run on many points at once. The five mistakes at the end are the ones that give a believable number: a two-sided cutoff used for a one-sided rate, standard deviations added instead of variances, a count approximated without the continuity correction, a density value read as a probability, and a mixture summarized by one normal with the same mean and variance.
- Normal has density , mean and variance . The second parameter is the variance; the standard deviation is .
- Standardizing. If is Normal, then is Normal. The value is the z-score of : how many standard deviations it lies from the mean.
- is the standard normal CDF, , and is its density. By symmetry . So .
- Values used here. , , , . The cutoffs and satisfy . Other values come from a table or
scipy.stats.norm.cdfandnorm.ppf. - Closure. For constants and , is Normal. For independent normals , is normal with mean and variance : variances add, in a sum or a difference.
- Binomial. A Binomial count has mean and variance . For large it is close to a normal with those moments; since the count takes only whole values, is approximated by , the continuity correction.
Builds on: Continuous random variables: densities, CDFs, expectation and the uniform, exponential and normal
Problems
- ·
A model's response time is Normal in milliseconds. Find , and .
- ·
A layer's weights are initialized from Normal. Use the 68-95-99.7 rule to estimate the fraction of weights with , the fraction in , and an interval that holds of them. Then give the exact values from .
- ··
A detector's score on negative examples is Normal, and on positive examples Normal. Choose the threshold that flags of negatives (the false positive rate), and find the fraction of positives it catches (the true positive rate).
- ·
A model scores on benchmark A, where published models have mean and standard deviation , and on benchmark B, where they have mean and standard deviation . Assuming both are roughly normal, on which benchmark is the result more exceptional? What fraction of models does it beat on each?
- ··
Show that if is Normal and , then is Normal, by computing its CDF. What changes for ? Then a sensor reads temperature as Normal in degrees Celsius: find the distribution of and .
- ··
A request passes through preprocessing, Normal ms, then inference, Normal ms, independently. Find the distribution of the total time and . Then find the distribution of inference minus preprocessing and the probability that the difference is under ms.
- ···
For , find the maximum value of and where it occurs, and show that the inflection points are at . Evaluate the peak for , and the ratio .
- ··
A classifier that guesses at random answers balanced yes-or-no questions, so its number correct is Binomial. Approximate with a normal and the continuity correction, and compare with the exact value and with the approximation without the correction.
- ···
A model's score is drawn from a Gaussian mixture: with probability from Normal and with probability from Normal. Find its mean, its variance and . Compare with a single normal of the same mean and variance. Then find the probability that a score of came from the second component.
- ···
An anomaly detector flags any point with . On points drawn independently from a normal distribution with known mean and standard deviation, find the probability that one point is flagged, the expected number of flags, and the probability of at least one. What threshold on gives one expected flag?
Answers
- ; ;
- About (exactly ) have ; about (exactly ) lie in ; lie in
- ; true positive rate
- More exceptional on A (, beating about ) than on B (, beating about )
- is Normal for every , with standard deviation ; is Normal and
- Total: Normal, ; difference: Normal,
- Maximum at , which is for ; inflection points at , where the density is of its peak
- with the correction, against exactly; without it
- Mean , variance , (one normal with those moments gives ); a score of came from the second component with probability
- ; expected flags and at least one almost surely, with no anomaly present; gives one expected flag
Worked solutions
Problem 1
A model's response time is Normal in milliseconds. Find , and .
- , so Standardize, then read .
- ms is standard deviations below the mean.
- Symmetry: the area above equals the area below .
- at and at The two ends of the interval.
- The area below the top end minus the area below the bottom end.
- ; ; The first two are equal because and sit symmetrically about the mean. Whether or is written makes no difference for a continuous variable.
Problem 2
A layer's weights are initialized from Normal. Use the 68-95-99.7 rule to estimate the fraction of weights with , the fraction in , and an interval that holds of them. Then give the exact values from .
- means , and about lies within , so about lies outsideThe rule: , and within , and standard deviations.
- is : about The band between and has two equal halves, one on each side.
- is within : the interval Three standard deviations either side.
- Exact: and From the table values.
- About (exactly ) have ; about (exactly ) lie in ; lie in The rule is a rounded form of . TensorFlow's truncated-normal initializer redraws every weight beyond , which here is about one weight in .
Problem 3
A detector's score on negative examples is Normal, and on positive examples Normal. Choose the threshold that flags of negatives (the false positive rate), and find the fraction of positives it catches (the true positive rate).
- means A one-sided rate: all sits in the upper tail.
- Invert .
- Undo the standardization: .
- For positives, Standardize the same threshold with the positives' mean and standard deviation.
- Symmetry.
- ; true positive rate About of positives are missed. Moving trades one error for the other, and tracing that trade as varies draws the ROC curve.
Problem 4
A model scores on benchmark A, where published models have mean and standard deviation , and on benchmark B, where they have mean and standard deviation . Assuming both are roughly normal, on which benchmark is the result more exceptional? What fraction of models does it beat on each?
- On A: Standard deviations above that benchmark's mean.
- On B: B's raw gain of points is larger, but B's scores are twice as spread out.
- and The fraction of models scoring below each result.
- More exceptional on A (, beating about ) than on B (, beating about )A z-score removes the units and the spread, so results on different scales can be compared. The comparison relies on both distributions having the same shape; the percentiles are only as good as the normal model.
Problem 5
Show that if is Normal and , then is Normal, by computing its CDF. What changes for ? Then a sensor reads temperature as Normal in degrees Celsius: find the distribution of and .
- Solve for ; dividing by keeps the inequality.
- Standardize .
- Multiply the top and bottom by .
- That is the CDF of NormalStep 3 is of a z-score with mean and standard deviation .
- For : Dividing by flips the inequality.
- Standardize, and multiply the top and bottom by as in step 3.
- Symmetry, , with : again a normal CDF, now with standard deviation .
- has mean and standard deviation The closure rule with , .
- degrees Fahrenheit is degrees Celsius, one standard deviation up on either scale.
- is Normal for every , with standard deviation ; is Normal and A change of units moves and stretches the distribution but leaves every z-score where it was. Standardizing a feature is the case , .
Problem 6
A request passes through preprocessing, Normal ms, then inference, Normal ms, independently. Find the distribution of the total time and . Then find the distribution of inference minus preprocessing and the probability that the difference is under ms.
- Mean of the total: Means add.
- Variance of the total: , so the standard deviation is For independent variables, variances add.
- A sum of independent normals is normal.
- The difference has mean and variance Subtracting is adding times a variable, and : the variances still add.
- Standardize the difference.
- Total: Normal, ; difference: Normal, The standard deviation of the total is , not (Mistake 2). Independence is needed for the variances to add; correlated stages add a covariance term.
Problem 7
For , find the maximum value of and where it occurs, and show that the inflection points are at . Evaluate the peak for , and the ratio .
- Let , so with Every normal density is the standard one, moved and rescaled.
- Chain rule on .
- Product rule: .
- only at , that is , a maximum, and changes sign from to there.
- changes sign where : , and , so the sign is that of .
- Peak ; for it is .
- in the exponent.
- Maximum at , which is for ; inflection points at , where the density is of its peakThe inflection points are where the curve turns from bending down to bending up, which is the way to read off a plotted bell curve. A density above is fine: it is probability per unit length, not a probability (Mistake 4).
Problem 8
A classifier that guesses at random answers balanced yes-or-no questions, so its number correct is Binomial. Approximate with a normal and the continuity correction, and compare with the exact value and with the approximation without the correction.
- Mean , variance , standard deviation The binomial's moments.
- The count stands for the interval , so the area starts half a unit below .
- Standardize.
- From a table.
- Without the correction: This leaves out half of the bar at .
- with the correction, against exactly; without itThe corrected value is off by of the answer, the uncorrected one by . A model that gets of right is unlikely, but not impossible, to be guessing: this is the p-value of a one-sided test.
Problem 9
A model's score is drawn from a Gaussian mixture: with probability from Normal and with probability from Normal. Find its mean, its variance and . Compare with a single normal of the same mean and variance. Then find the probability that a score of came from the second component.
- The mean of a mixture is the weighted mean of the means.
- For each component, .
- , standard deviation Within-component variance plus the spread of the means, .
- Total probability over the two components.
- The first component contributes almost nothing.
- One normal: Same mean and variance, different shape.
- Responsibility: Bayes' rule, with each component's density at .
- and .
- Mean , variance , (one normal with those moments gives ); a score of came from the second component with probability The single normal underestimates this tail by about (Mistake 5). The responsibility in step 7 is the E step of the EM algorithm for fitting a mixture.
Problem 10
An anomaly detector flags any point with . On points drawn independently from a normal distribution with known mean and standard deviation, find the probability that one point is flagged, the expected number of flags, and the probability of at least one. What threshold on gives one expected flag?
- Both tails.
- Expected flags: Linearity of expectation; independence is not needed for this.
- Independence: no point is flagged with probability each time.
- , which is to eleven decimal places for small .
- One expected flag needs , so Divide the target count by , and split it over two tails.
- ; expected flags and at least one almost surely, with no anomaly present; gives one expected flagA cutoff that is rare for one point is routine across thousands, the multiple-comparisons problem in another form. Real data usually have heavier tails than the normal, so the true count of false flags is often higher still.
Where this goes wrong
1. Using the two-sided cutoff for a one-sided rate
The number is the one remembered alongside , from confidence intervals and two-sided tests.
- The threshold must flag of negatives, NormalRight so far: Problem 3.
- “ means .”The habit that causes the mistake: leaves in each of two tails. A false positive rate counts only the upper tail, so the cutoff is .
- This flags only of negatives, and the true positive rate falls from to . The detector is stricter than asked, and catches about fewer positives in every .
2. Adding standard deviations
The means of the two stages add, and the standard deviation looks like the same kind of quantity.
- The total has mean msRight so far: Problem 6.
- “Its standard deviation is ms.”Variances add for independent variables, not standard deviations: .
- The right answer is , so the wrong spread makes a slow request look more than three times as common. Adding standard deviations is right only when the two times are perfectly correlated.
3. Dropping the continuity correction
A normal is continuous, so it seems natural to put the boundary of the count where it is written.
- is Binomial, approximated by , NormalRight so far: Problem 8.
- “.”Each whole count stands for the interval under the curve, so the bar at starts at .
- The exact value is and the corrected approximation . Shifting the wrong way, to , is worse still: . For use ; for use .
4. Reading a density value as a probability
The density is the function that gets plotted and evaluated, and its value at a point looks like the chance of that point.
- is Normal, with Right so far: Problem 7.
- “ is the probability that is .”A probability is an area under ; a single point has probability , and is probability per unit length.
- A probability above is impossible, which is the clue. The chance of landing within of is , close to : density times width, for a short interval.
5. Treating a mixture as one normal
Any distribution has a mean and a variance, and a normal with the same two numbers looks like a fair summary of it.
- The mixture of Problem 9 has mean and standard deviation Right so far.
- “So is approximately Normal.”A normal is determined by two numbers, but a mixture is not: this one has two bumps, and its upper tail is the tail of the second component.
- The mixture gives , about more. A z-score is only a probability when the distribution really is normal; with two bumps it can be far off in either direction.
Print this set: normal-distribution-and-z-scores.pdf (problems, answers, and worked solutions on separate pages).
Read the guide: The normal distribution and z-scores: divide by σ, pick the right tail, and add the variances
More at eulerpad.app. This is one of 82 free practice sets with worked solutions at eulerpad.app/practice, alongside guides to the topics behind them.
EulerPad, the app, reads your handwritten math on iPad, iPhone and Android and checks it line by line. It's free to start.
This set is also Chapter 12 of Probability & Statistics for Machine Learning, a 250-problem workbook with worked solutions: eulerpad.app/workbooks