Primer S. Probability and statistics from the first course
Data Mining as Observation, draft 0.3, 2026-09-09. Read this primer if sections 0.7 to 0.9 of chapter 0 stop you, or if the words null, bootstrap, p-value, Poisson, or bits are familiar without being usable. Nothing here is a result. Each section rebuilds one idea from counting, gives an example that can be checked by hand, and says which section of chapter 0 stands on it.
Chapter 0 assumes a first course in statistics and begins with a covariance matrix, a bootstrap interval, and a Poisson null in the space of three sections. This primer starts from what a probability is and ends at the ideas the book uses every week, the sampling distribution of an estimate, the null model, the correction for many comparisons, and the bit. Everything in it is standard and is covered at greater length in TSK Appendix C [1]. The book's intervals come from the bootstrap and its nulls from simulation, and section S.11 says how a simulation is made reproducible, since every number in the book was.
S.1 A probability is a long-run fraction, and a conditional probability is a fraction of a fraction
An experiment has a set of possible outcomes, and an event is a set of those outcomes. The probability of an event is the fraction of repetitions in which it would occur if the experiment were repeated without end. For equally likely outcomes it is the count of outcomes in the event over the count of all outcomes. Probabilities lie between zero and one, the probability that an event does not occur is one minus the probability that it does, and the probability that either of two events occurs is the sum of their probabilities less the probability of both.
$$\Pr[A^{c}]=1-\Pr[A],\qquad \Pr[A\cup B]=\Pr[A]+\Pr[B]-\Pr[A\cap B]. \tag{S.1}$$
The conditional probability of $A$ given $B$ is the fraction of the repetitions in which $B$ occurred that also had $A$. It is the probability of both divided by the probability of $B$. Two events are independent when knowing one changes nothing about the other, so the conditional probability equals the plain one and the probability of both is the product.
$$\Pr[A\mid B]=\frac{\Pr[A\cap B]}{\Pr[B]},\qquad A,B\ \text{independent}\iff \Pr[A\cap B]=\Pr[A]\,\Pr[B]. \tag{S.2}$$
Bayes' rule turns a conditional probability around. It follows from writing the probability of both events in the two possible orders. Its content is that the probability of a cause given an observation depends on how common the cause is to begin with, the base rate, and not only on how well the observation fits the cause.
$$\Pr[A\mid B]=\frac{\Pr[B\mid A]\,\Pr[A]}{\Pr[B]},\qquad \Pr[B]=\Pr[B\mid A]\Pr[A]+\Pr[B\mid A^{c}]\Pr[A^{c}]. \tag{S.3}$$
Example. Two fair dice. The outcomes are the thirty-six ordered pairs. The event that the sum is seven contains six of them, so its probability is $6/36$. Given that the first die shows three, the sum is seven only when the second shows four, so the conditional probability is $1/6$, the same as the plain probability, and the two events are independent. Given that the sum is seven, the first die shows three with probability $1/6$ as well. Now a screening test. A condition has base rate one percent. The test detects it ninety percent of the time when present and reports it five percent of the time when absent. The probability of a positive report is $0.01\cdot0.9+0.99\cdot0.05=0.009+0.0495=0.0585$, and by Bayes' rule the probability of the condition given a positive report is $0.009/0.0585$, about $0.154$. A test that is right ninety percent of the time is wrong about five reports in six, because the condition is rare.
Used in. Section 0.14, where precision is this conditional probability with a classifier as the test. Section S.9. Chapter 6.
S.2 A random variable has an expectation and a variance
A random variable is a number attached to each outcome. Its distribution lists the values it can take with the probability of each, and for a discrete variable that list is the probability mass function. The expectation, also called the mean, is the average of the values weighted by their probabilities. It is the long-run average of the variable over repetitions. The variance is the expectation of the squared distance from the mean, and the standard deviation is its square root, which is in the units of the variable itself.
$$\mathbb E[X]=\sum_x x\,\Pr[X=x],\qquad \operatorname{Var}(X)=\mathbb E\big[(X-\mu)^{2}\big]=\mathbb E[X^{2}]-\mu^{2},\qquad \sigma=\sqrt{\operatorname{Var}(X)}. \tag{S.4}$$
Expectation is linear. The expectation of a sum is the sum of the expectations, whether or not the variables are independent, and a constant times a variable has that constant times the expectation. Variance is not linear. Scaling a variable by $a$ scales its variance by $a^{2}$, shifting it changes nothing, and the variance of a sum is the sum of the variances only when the variables are independent, or at least uncorrelated in the sense of section S.5.
$$\mathbb E[aX+bY]=a\,\mathbb E[X]+b\,\mathbb E[Y],\qquad \operatorname{Var}(aX+b)=a^{2}\operatorname{Var}(X),\qquad \operatorname{Var}(X+Y)=\operatorname{Var}(X)+\operatorname{Var}(Y)\ \text{if independent}. \tag{S.5}$$
Standardizing a variable subtracts its mean and divides by its standard deviation. The result, a z-score, has mean zero and variance one and says how many standard deviations a value sits from the mean. It is the same operation on a column of a table that chapter 3 calls standardization, applied with the column's own mean and spread.
Example. A fair die. The expectation is $(1+2+3+4+5+6)/6=3.5$. The expectation of the square is $(1+4+9+16+25+36)/6=91/6$, about $15.167$, so the variance is $15.167-12.25=2.917$ and the standard deviation is $1.708$. A roll of six has z-score $(6-3.5)/1.708=1.46$. Two independent dice have a sum with expectation $7$ and variance $5.833$.
Used in. Section 0.3, where the variance of a column and the covariance of two are the entries of the covariance matrix. Section S.4.
S.3 The distributions the book names
Four distributions carry the book, and each has a mean and a variance a reader can compute.
A Bernoulli variable is one with probability $p$ and zero otherwise. Its mean is $p$ and its variance is $p(1-p)$. A binomial variable counts the ones among $n$ independent Bernoulli trials. Its mean is $np$, its variance is $np(1-p)$, and the probability of exactly $k$ ones is the number of ways to choose which $k$ trials succeed, times $p^{k}$, times $(1-p)^{n-k}$.
$$\Pr[X=k]=\binom{n}{k}p^{k}(1-p)^{n-k},\qquad \binom{n}{k}=\frac{n!}{k!\,(n-k)!},\qquad \mathbb E[X]=np. \tag{S.6}$$
A Poisson variable counts rare events. When $n$ is large and $p$ is small with $np$ held at a value $\mu$, the binomial probabilities settle to a formula with $\mu$ as its only parameter. Its mean and its variance are both $\mu$. The book uses it whenever slots are handed out at random among many points and the question is how many land on one point, which is the null of section 0.9 and chapter 3.
$$\Pr[X=k]=e^{-\mu}\frac{\mu^{k}}{k!},\qquad \mathbb E[X]=\operatorname{Var}(X)=\mu. \tag{S.7}$$
A uniform variable on the interval from zero to one is equally likely to fall anywhere in it. Its mean is one half and its variance is one twelfth. A p-value computed correctly under its null is uniform, which section S.7 uses.
A normal variable has the bell-shaped density with mean $\mu$ and standard deviation $\sigma$. About sixty-eight percent of its mass lies within one standard deviation of the mean, about ninety-five percent within two, and about $99.7$ percent within three. The multiplier for exactly ninety-five percent is $1.96$. The normal matters because of the central limit theorem of section S.4, which says that averages of many independent variables are close to normal whatever the variables were.
$$\Pr[\mu-1.96\,\sigma\le X\le\mu+1.96\,\sigma]\approx0.95. \tag{S.8}$$
Example. Ten fair coin flips. The probability of exactly seven heads is $\binom{10}{7}/2^{10}=120/1024$, about $0.117$. Now the chapter 3 null. Five hundred queries each retrieve ten neighbours from fifty thousand points, so five thousand slots are handed out and each lands on a given point with probability one in fifty thousand. The count at one point is binomial with $n=5000$ and $p=1/50000$, and the Poisson with mean $\mu=0.1$ is its limit. The probability of count zero is $e^{-0.1}$, about $0.905$. Count one has probability $0.0905$, count two has $0.00452$, and count three has $0.000151$. The probability of three or more is one minus the first three, about $0.000155$, which times fifty thousand points is $7.7$, so several points reach three by chance. The probability of four or more is about $3.8$ in a million, which times fifty thousand is $0.19$, so a count of four is not expected from any point. The no-structure ceiling is three, which is the number section 0.9 states and chapter 3 uses.
Used in. Section 0.9, equation 0.17. Chapter 3 section 3.5. Chapter 8, where the normal multiplier appears in the corrected test of section 0.9.
S.4 A sample has statistics, and each statistic has a spread of its own
A sample is a list of $n$ observed values. Its mean is their average. Its median is the middle value once sorted, or the average of the two middle values. A percentile is the value below which a stated fraction of the sample lies, and the median is the fiftieth. The sample variance averages the squared deviations from the sample mean, dividing by $n-1$ rather than $n$, because the sample mean sits closer to its own sample than the true mean does and the deviations come out a little small. The sample standard deviation is its square root.
$$\bar x=\frac1n\sum_i x_i,\qquad s^{2}=\frac{1}{n-1}\sum_i(x_i-\bar x)^{2}. \tag{S.9}$$
The skewness is the average cubed deviation divided by the cube of the standard deviation. It is zero for a symmetric sample, positive when the long tail is on the high side, and negative when it is on the low side. Chapter 3 uses it to summarize how unevenly nearest-neighbour lists are shared.
$$\text{skew}=\frac{\frac1n\sum_i(x_i-\bar x)^{3}}{\sigma^{3}}. \tag{S.10}$$
A statistic computed from a sample would come out differently on a different sample of the same size. The distribution of the statistic across such samples is its sampling distribution, and the standard deviation of that distribution is the standard error of the statistic. For the mean of $n$ independent values with standard deviation $\sigma$, the standard error is $\sigma$ over the square root of $n$. Halving it takes four times the data. That is the law of large numbers in a usable form, the mean settles toward the expectation, and the central limit theorem adds that its sampling distribution is close to normal once $n$ is a few dozen, whatever the shape of the values.
$$\operatorname{SE}(\bar x)=\frac{\sigma}{\sqrt n},\qquad \frac{\bar x-\mu}{\sigma/\sqrt n}\ \approx\ \text{normal with mean } 0 \text{ and variance } 1. \tag{S.11}$$
Example. The sample $2,4,4,4,5,5,7,9$ has $n=8$ and mean $40/8=5$. The deviations are $-3,-1,-1,-1,0,0,2,4$, their squares sum to $32$, so the variance with $n$ in the denominator is $4$ and the standard deviation $2$, while the sample variance with $n-1$ is $32/7$, about $4.571$, with standard deviation $2.138$. The median is $4.5$. The cubed deviations sum to $-27-1-1-1+0+0+8+64=42$, so the skewness with the population standard deviation is $(42/8)/8=0.656$, positive because the $9$ is far out on the high side. Change the $9$ to $90$ and the mean becomes $15.125$ while the median stays at $4.5$, which is why the book reports a median where one value can be wild. The standard error of the mean, with $\sigma=2$, is $2/\sqrt8=0.707$, and a sample of thirty-two would be needed to bring it to $0.354$.
Used in. Section 0.9, the standard error. Chapter 3 section 3.5, skewness. Chapter 10, percentiles and min-over-strata.
S.5 Two columns together, covariance, correlation, and the line through the cloud
The covariance of two variables is the expectation of the product of their deviations from their means. It is positive when they move together, negative when one rises as the other falls, and zero when there is no linear tendency either way. The correlation divides the covariance by the two standard deviations, which removes the units and confines the value to the interval from minus one to one.
$$\operatorname{Cov}(X,Y)=\mathbb E\big[(X-\mu_X)(Y-\mu_Y)\big],\qquad r=\frac{\operatorname{Cov}(X,Y)}{\sigma_X\sigma_Y}. \tag{S.12}$$
For two columns of a table, the correlation is the cosine of section L.2 applied to the two centered columns. That is worth holding onto. It says that correlation ignores the mean and the scale of each column, and that a correlation of one means the two centered columns point the same way as vectors.
The least-squares line predicts $Y$ from $X$ by the straight line that minimizes the sum of squared vertical errors. Its slope is the covariance divided by the variance of $X$, and it passes through the point of the two means. The square of the correlation is the fraction of the variance of $Y$ the line accounts for.
$$\hat y=a+b\,x,\qquad b=\frac{\operatorname{Cov}(X,Y)}{\operatorname{Var}(X)},\qquad a=\bar y-b\,\bar x,\qquad r^{2}=\frac{\text{variance explained}}{\text{variance of }Y}. \tag{S.13}$$
Two warnings that the book repeats. A correlation says nothing about which variable causes which, or whether a third causes both. And a correlation computed over a whole table can differ from, and even reverse, the correlation within every group of rows, which is Simpson's paradox, treated in chapter 5 [3]. Correlation between ranks rather than values, the Spearman correlation, is introduced in section 0.8 and is unchanged by any increasing transformation of either column.
Example. $x=(1,2,3,4,5)$ and $y=(2,4,5,4,5)$. The means are $3$ and $4$. The deviations are $(-2,-1,0,1,2)$ and $(-2,0,1,0,1)$. Their products sum to $4+0+0+0+2=6$, the squared deviations of $x$ sum to $10$, and those of $y$ sum to $6$. The covariance with $n$ in the denominator is $1.2$ and the correlation is $6/\sqrt{60}$, about $0.775$. The slope is $6/10=0.6$, the intercept is $4-0.6\cdot3=2.2$, and the line is $\hat y=2.2+0.6x$. It accounts for $r^{2}=0.6$ of the variance of $y$. Multiply every $y$ by ten and add a hundred, and the covariance becomes $12$, the slope $6$, and the correlation stays at $0.775$.
Used in. Section 0.3, the covariance matrix. Section 0.6, where correlation is a quotient that discards shift and scale. Section 0.8. Chapter 3 section 3.1.
S.6 An estimate comes with an interval, and the bootstrap makes one from the sample alone
A statistic used to stand in for an unknown quantity is an estimate, and the rule that produces it is an estimator. An estimator is unbiased when its expectation over samples is the quantity it estimates, which is why the sample variance of equation S.9 divides by $n-1$. An estimate without a statement of its spread is not interpretable, and the book gives the spread in one of four forms that section 0.9 lists.
A confidence interval at ninety-five percent is a range computed from the sample by a rule that, over repeated samples, would cover the true value ninety-five times in a hundred. For a mean, the normal approximation of equation S.11 gives the estimate plus and minus $1.96$ standard errors, with the sample standard deviation standing in for $\sigma$. For small samples the multiplier is larger, from the $t$ distribution, and at $n=8$ it is $2.36$.
$$\bar x\ \pm\ 1.96\,\frac{s}{\sqrt n}. \tag{S.14}$$
The bootstrap makes an interval without a formula. Draw $n$ values from the sample with replacement, so that some values repeat and some are left out, and compute the statistic on the draw. Repeat a few thousand times. The spread of those recomputed statistics estimates the sampling distribution, and the $2.5$th and $97.5$th percentiles of them are the percentile interval. It works for statistics that have no formula for a standard error, such as a median, an AUROC, or a rank correlation, which is why the book uses it. A paired bootstrap draws the same rows for two methods at once and recomputes their difference, so that the variation the two methods share cancels and the interval on the difference is narrower than two separate intervals would suggest [2].
Example. The sample of section S.4 has mean $5$, sample standard deviation $2.138$, and $n=8$, so the standard error is $2.138/\sqrt8=0.756$ and the normal interval is $5\pm1.48$, from $3.52$ to $6.48$. With the $t$ multiplier it is $5\pm1.79$. One bootstrap draw might be $4,9,2,5,4,4,7,9$, with mean $5.5$, and another $2,2,4,4,5,5,5,7$, with mean $4.25$. Two thousand such draws give two thousand means, and the interval is the middle ninety-five percent of them. Exercise 19 runs it.
Used in. Section 0.9. Chapter 8 sections 8.5 and 8.6. Chapter 11, where every recall is reported with its interval.
S.7 A test names a null, computes a statistic, and reports how surprising the statistic would be
A null hypothesis is the statement that the effect under study is absent, the two groups have the same mean, the correlation is zero, the classifier is no better than chance. A test statistic is a number computed from the sample that would be near some reference value under the null and far from it otherwise. The p-value is the probability, if the null were true, of a statistic at least as far from the reference as the one observed. A small p-value says the sample would be surprising under the null. It does not say the null is improbable, since it is computed assuming the null, and it does not say the effect is large.
The threshold below which a p-value is called significant is a convention, usually $0.05$, and it fixes the rate of false alarms when the null is true. A type I error rejects a true null, at that rate by construction. A type II error fails to reject a false null, and the power of a test is one minus that rate, which grows with the sample size and the size of the effect.
A permutation test computes the p-value by construction rather than by formula. When the null says the group labels carry no information, every reassignment of the labels is as likely as the observed one. Compute the statistic under every reassignment, or under a few thousand random ones, and the p-value is the fraction at least as extreme as the observed. It requires no assumption about the shape of the data. A two-sided p-value counts departures in both directions, and the book reports two-sided values unless the source says otherwise.
$$p=\Pr\big[\,|T|\ge|t_{\mathrm{obs}}|\ \big|\ \text{null}\,\big]=\frac{\#\{\text{reassignments with } |T|\ge|t_{\mathrm{obs}}|\}}{\#\{\text{reassignments}\}}. \tag{S.15}$$
Example. Group A scored $8$ and $9$, group B scored $4$ and $5$, and the statistic is the difference of the two means, observed at $8.5-4.5=4$. Under the null the four numbers are exchangeable, and there are six ways to choose which two are called A. The differences are $4$, $-1$, $0$, $0$, $1$, and $-4$. Two of the six are at least $4$ in absolute value, so the two-sided p-value is $2/6$, about $0.33$, and the one-sided value is $1/6$. With two values per group no result can reach $0.05$, whatever the numbers, because $1/6$ is the smallest p-value the design can produce. That is the power of the test, and the sample size set it before any data arrived.
Used in. Section 0.9, where the book says what it means by a p-value and never uses one as evidence on its own. Chapter 8 throughout, and its rule that every claim carries a null beside it.
S.8 Many tests at once, and why the best of forty is not a result
Run forty tests at the five percent level when every null is true, and each has a five percent chance of a false alarm. The expected number of false alarms is two. The probability of at least one is one minus $0.95^{40}$, about $0.87$. Reporting the best of the forty as if it had been the only test overstates it, because the smallest of forty null p-values is not distributed like one p-value.
The family-wise error rate is the probability of at least one false alarm across a set of tests. The Bonferroni correction divides the threshold by the number of tests, which by the union bound of equation S.1 holds the family-wise rate at or below the original threshold whatever the dependence among the tests. It is conservative, and TSK chapter 10 covers the alternatives that control the false discovery rate instead [1].
$$\Pr[\text{at least one false alarm}]\le m\,\alpha,\qquad \alpha_{\mathrm{Bonf}}=\frac{\alpha}{m}. \tag{S.16}$$
Two cases the book meets are subtler than forty independent tests. Repeated cross-validation runs the same model on overlapping training sets, so the folds are not independent and a standard test that counts each fold as a fresh sample understates the variance. Section 0.9 gives the Nadeau and Bengio correction [4]. And a search that tries many models, features, or thresholds and reports the best has run as many tests as it tried, whether or not any p-value was computed. Chapter 8 section 8.5 counts them.
Example. Forty tests at $0.05$ give an expected two false alarms and an $87$ percent chance of at least one. Bonferroni sets the threshold at $0.05/40=0.00125$, and the chance of at least one false alarm is then at most $40\cdot0.00125=0.05$.
Used in. Section 0.9. Chapter 7, where nine wins became three. Chapter 8 section 8.5.
S.9 A classifier is a conditional probability, and its scores can be read four ways
A classifier with two classes and a threshold produces four counts on a test set. True positives, false positives, false negatives, and true negatives, arranged as the confusion matrix. Every standard score is a conditional probability read off that table. Precision is the fraction of predicted positives that are positive. Recall, also called the true positive rate or sensitivity, is the fraction of positives that were predicted. The false positive rate is the fraction of negatives that were predicted positive. Accuracy is the fraction of all rows classified correctly, and it is the score most easily fooled, since a classifier that predicts the majority class for every row scores the majority's share.
$$P=\Pr[\text{positive}\mid\text{predicted positive}]=\frac{TP}{TP+FP},\qquad R=\Pr[\text{predicted positive}\mid\text{positive}]=\frac{TP}{TP+FN}. \tag{S.17}$$
Precision is the posterior probability of Bayes' rule with the classifier as the test, and it falls with the base rate for the same reason the screening test of section S.1 did. Recall and the false positive rate do not depend on the base rate, which is why a classifier is compared across datasets by those two and not by precision.
A classifier that outputs a score rather than a decision can be read without a threshold. The AUROC of section 0.8 is the probability that a random positive scores above a random negative, which is a probability statement about pairs and is $0.5$ for a score that carries no information. And a score is calibrated when, among the rows given score $s$, the fraction that are positive is $s$. Calibration is a statement about the conditional probability of the class given the score, and a score can rank perfectly while being miscalibrated everywhere. Section 0.14 gives the measure.
$$\text{calibrated}\iff\Pr[\text{positive}\mid\text{score}=s]=s. \tag{S.18}$$
Example. One thousand rows, fifty of them positive. A classifier reports thirty true positives, ten false positives, twenty false negatives, and nine hundred and forty true negatives. Precision is $30/40=0.75$, recall is $30/50=0.6$, the false positive rate is $10/950$, about $0.011$, and accuracy is $970/1000=0.97$. The classifier that calls every row negative has accuracy $0.95$, so the $0.97$ is two points above doing nothing. Now three positives scoring $0.9$, $0.4$, and $0.35$ and two negatives scoring $0.6$ and $0.3$. Of the six positive-negative pairs, the positive scores higher in four, so the AUROC is $4/6$, about $0.667$.
Used in. Section 0.8, equation 0.16. Section 0.14, equations 0.28 and 0.29. Chapter 6 and chapter 8 section 8.5.
S.10 Information is measured in bits, and a model is scored by the bits it saves
An outcome with probability $p$ carries $-\log_2 p$ bits of surprise. A fair coin's outcome carries one bit, an outcome with probability one in eight carries three, and a certainty carries none. The entropy of a distribution is its expected surprise, the average number of bits needed to report an outcome drawn from it when the code is chosen as well as possible [5]. A uniform distribution over $k$ outcomes has entropy $\log_2 k$, and any other distribution over the same outcomes has less.
$$H(p)=-\sum_i p_i\log_2 p_i. \tag{S.19}$$
When outcomes are drawn from $p$ but coded as if they came from a model $q$, the expected bits per outcome is the cross-entropy, and the excess over the entropy is the Kullback and Leibler divergence of section 0.11, the bits wasted by using the wrong model. It is never negative and is zero only when the model is right. The book quotes it in nats, which use the natural logarithm, and one nat is $1.4427$ bits.
$$H(p,q)=-\sum_i p_i\log_2 q_i,\qquad \mathrm{KL}(p\,\|\,q)=H(p,q)-H(p)=\sum_i p_i\log_2\frac{p_i}{q_i}\ \ge0. \tag{S.20}$$
The likelihood of a model is the probability it assigns to the data that were observed, and the maximum likelihood estimate is the choice of the model's parameters that makes that probability largest. Its logarithm is a sum over observations, so maximizing likelihood is minimizing the total surprise the model assigns to the data, which is the cross-entropy again with the sample standing in for $p$. Perplexity, section 0.11, is two to the power of that cross-entropy per token, and a language model is trained by making it small.
Example. A fair die has entropy $\log_2 6$, about $2.585$ bits. The distribution $(0.5,0.25,0.25)$ has entropy $0.5\cdot1+0.25\cdot2+0.25\cdot2=1.5$ bits, and its perplexity is $2^{1.5}$, about $2.83$. Code it with the uniform model $(1/3,1/3,1/3)$ and the cross-entropy is $\log_2 3$, about $1.585$ bits, so the divergence is $0.085$ bits, or $0.059$ nats, wasted per outcome. Seven heads in ten flips of a coin with unknown $p$ have likelihood $p^{7}(1-p)^{3}$, whose logarithm has derivative $7/p-3/(1-p)$, zero at $p=0.7$, so the maximum likelihood estimate is the observed fraction.
Used in. Section 0.7, where a bit is the unit of a budget and each bit halves a rounding error. Section 0.11, equations 0.22 and 0.24. Chapter 4 and chapter 11.
S.11 A simulation is a sample too, and the seed is part of the record
Drawing a random sample of $k$ rows from a table can be done with replacement, so a row can appear twice, as the bootstrap does, or without replacement, so each appears at most once. A computer draws with a pseudo-random generator, a deterministic rule that produces a sequence indistinguishable from chance from a starting value called the seed. The same seed gives the same sequence, so a simulation that records its seed can be rerun to the last digit, and one that does not cannot. Every simulated number in the book names its seed in the chapter's sources table, and section 0.12 says what the commit hash beside it adds.
A null model is a simulation with the structure under test removed and everything else kept. Section S.3 built one by hand, slots handed out at random. Where no formula exists, the null is run many times and the observed statistic is compared with the distribution of simulated ones, which is the permutation test of section S.7 in general form. The chance level of a statistic is its expectation under the null, and the book prints it beside every reading of that statistic, since a number with no chance level beside it cannot be read.
A Monte Carlo estimate of a probability is the fraction of runs in which the event occurred, and it is a sample proportion with a standard error of its own, from the Bernoulli variance of section S.3. A hundred runs pin a probability near one in six to about four points either way, and ten thousand runs to about half a point.
$$\hat p=\frac{\#\text{runs with the event}}{N},\qquad \operatorname{SE}(\hat p)=\sqrt{\frac{\hat p(1-\hat p)}{N}}. \tag{S.21}$$
Example. Hand five thousand slots to fifty thousand points at random, one hundred times with seed $0$, and count the runs in which some point reaches four. Section S.3 gives an expected $0.19$ such points per run, so the probability that a run has at least one is about $0.17$, and a hundred runs might show it $17$ times. The estimate is $0.17$ with standard error $\sqrt{0.17\cdot0.83/100}=0.038$, which covers the prediction. Exercise 21 runs it, and the notebook records the seed.
Used in. Section 0.9 and section 0.12. Chapter 3 section 3.5 and chapter 8 section 8.4, where the null was more than half the apparent effect three times.
S.12 Where each tool goes next
The table names, for each section of this primer, the chapter 0 section that builds on it and the first chapter that uses the result.
| Primer section | Builds toward | First used in earnest |
|---|---|---|
| S.1 probability, conditioning, Bayes | 0.14 | chapter 6 |
| S.2 random variables, expectation, variance, z-scores | 0.3 | chapter 3 section 3.1 |
| S.3 binomial, Poisson, uniform, normal | 0.9, equation 0.17 | chapter 3 section 3.5 |
| S.4 sample statistics, skewness, standard error | 0.9 | chapter 3 section 3.5 |
| S.5 covariance, correlation, the least-squares line | 0.3, 0.6, 0.8 | chapter 3 section 3.1 |
| S.6 intervals and the bootstrap | 0.9 | chapter 8 section 8.5 |
| S.7 nulls, p-values, permutation tests | 0.9 | chapter 8 |
| S.8 multiple comparisons | 0.9, equation 0.18 | chapter 7, chapter 8 section 8.5 |
| S.9 the confusion matrix, AUROC, calibration | 0.8, 0.14 | chapter 6 |
| S.10 bits, entropy, likelihood | 0.7, 0.11 | chapter 4 section 4.2 |
| S.11 seeds, null models, Monte Carlo | 0.9, 0.12 | chapter 3 section 3.5 |
Exercises
Warm-up questions are answered in a sentence or two. By-hand problems are computed on paper and checked against the answer key at the end of the book. Programming problems produce a committed notebook.
Warm-up
- A screening test comes back positive. Why is the probability that the condition is present not the test's detection rate?
- Why does the sample variance divide by $n-1$ rather than $n$?
- A sample is made four times larger. What happens to the standard error of its mean?
- A count is Poisson with mean $0.1$. Roughly what fraction of points receive a count of zero?
- Two columns have correlation $0.9$. What does that say about whether one causes the other?
- A test reports a p-value of $0.03$. Is that the probability that the null hypothesis is true? Say what it is instead.
- Forty tests are run at the five percent level with every null true. How many false alarms are expected, and what threshold does Bonferroni set?
- A classifier has accuracy $0.97$ on a dataset in which five percent of rows are positive. Why is that number not impressive on its own?
By hand
- Two fair dice. Compute the probability that the sum is eight, the probability that the sum is eight given that the first die shows five, and say whether the two events are independent.
- A condition has base rate two percent. A test detects it ninety-five percent of the time when present and reports it ten percent of the time when absent. Compute the probability of a positive report and the probability of the condition given a positive report.
- For the sample $3,3,4,6,9$ compute the mean, the median, the variance and standard deviation with $n$ in the denominator, the sample variance with $n-1$, and the skewness with the population standard deviation. State the sign of the skewness before computing it.
- Ten thousand points receive two thousand slots at random, so the count at one point is Poisson with mean $0.2$. Compute the probabilities of counts zero, one, two, and three, the expected number of points with a count of three or more, the expected number with four or more, and the no-structure ceiling.
- For $x=(1,2,3,4)$ and $y=(1,3,2,6)$ compute the covariance with $n$ in the denominator, the correlation, and the least-squares line.
- A sample of twenty-five values has mean $10$ and sample standard deviation $4$. Compute the standard error, the normal ninety-five percent interval, and the sample size that would bring the interval's half-width to $0.5$.
- Group A scored $10$ and $12$, group B scored $7$ and $8$. List the six reassignments, the difference of means under each, and the two-sided permutation p-value of the observed difference.
- A confusion matrix has forty true positives, sixty false positives, ten false negatives, and eight hundred and ninety true negatives. Compute precision, recall, F1, accuracy, the base rate, and the accuracy of the classifier that calls everything negative.
- Outcomes are drawn from $(0.5,0.5)$ and coded with the model $(0.9,0.1)$. Compute the entropy, the cross-entropy, and the divergence, in bits.
Programming
- Simulate one hundred thousand rolls of two dice with a recorded seed. Estimate the probability that the sum is eight, report its standard error by equation S.21, and compare with the exact value of exercise 9.
- Bootstrap the mean of the sample of section S.4 with two thousand draws. Report the percentile interval and compare it with the normal interval of section S.6.
- Draw two groups of twenty values from the same normal distribution and run a permutation test on the difference of means with one thousand reassignments. Repeat the whole experiment one thousand times and report the fraction of p-values below $0.05$.
- Hand five thousand slots to fifty thousand points at random, one hundred times, and record the busiest count in each run. Report how often it reached three and how often four, with the standard error of each fraction, beside the predictions of section S.3.
- Draw forty p-values from the uniform distribution, which is what forty true nulls produce, and record whether any falls below $0.05$. Repeat one thousand times and report the fraction of repetitions with at least one, beside the $0.87$ of section S.8. Repeat with the Bonferroni threshold.
Answers to the warm-up and by-hand problems are collected in the answer key at the end of the book.
previous, Primer L. Linear algebra from the first course ยท next, Chapter 0. The mathematics this book uses