Introduction
In this story, we will explore a topic in probability theory that is one of those with the most practical applications: statistical tests. However, we will not simply list the tests and take the test statistics as well as the distributions involved, like the t-distribution or the chi-squared distribution, as given. Instead, we will start from the very beginning and develop a statistical test ourselves, using the example of the sample variance.
Here, we want to determine how probable a sample with a certain sample variance is if it actually comes from a distribution with a given theoretical variance. So we will first examine how we can define a test statistic that captures this. Subsequently, we will derive the distribution of this test statistic depending on the quantity we want to check, the sample variance. Based on that, we will finally be able to state how high the probability of a given value of our test statistic is, under the assumption that the sample actually comes from a distribution with the given theoretical variance.
That sounds like some work, and we will indeed have to go a bit more into depth here. But the result is promising, as we really gain a good and fundamental understanding of statistical tests. In particular, we will have understood how statistical tests are developed.
A Measure for How Well Two Variances Match
To begin our journey towards our first statistical test, let us pick up the example from the introduction again. So we have drawn a sample from a normal distribution whose mean and variance we do not know. Now we want to determine whether this normal distribution may have a certain variance \(\sigma_0^2\). Needless to say, we start by computing the sample variance of our sample and look at how similar it is to \(\sigma_0^2\).
But what next? How can we determine how well this sample variance fits the theoretical one? Because obviously, the two will not be 100% identical, i.e., in most cases there will be some deviation, even if \(\sigma_0^2\) is indeed the theoretical variance of the distribution we have drawn our sample from. But how much deviation is still OK?
Answering this question is actually quite simple. All we need is a quantity, usually referred to as a test statistic, that captures the information of how well the sample variance fits a theoretical one. For example, any of these quantities would meet this requirement:
- \(T = s - \sigma_0\)
- \(T = \frac{s^2}{\sigma_0^2}\)
- \(T = \frac{s^6}{\sigma_0^6}\)
- \(T = 7 \cdot \frac{s^2}{\sigma_0^2}\)
But which of these should we take? To answer this, let us look at what we have to do as a next step. What is the basic idea of a statistical test?
A statistical test consists of a test statistic and a null hypothesis, usually denoted as \(H_0\). The test statistic is an expression that captures the information of how well a certain quantity fits another. The null hypothesis is a claim about the relationship between these two quantities. In our case with the variances, this would be, for example, \(\sigma^2 = \sigma_0^2\). The opposite of this claim is called the alternative hypothesis, usually denoted as \(H_A\).
For the test statistic, it is necessary to determine its distribution. This way, under the assumption that \(H_0\) is actually true, we can assign probabilities to the possible values of the test statistic. If the test statistic calculated from the observations has a very low probability, it is very unlikely to observe such a value if \(H_0\) is actually true. In this case, we say that \(H_0\) is rejected. If, on the other hand, the test statistic has a high probability, nothing speaks against \(H_0\), and we say that \(H_0\) cannot be rejected.
If we incorporate this into our considerations for choosing the test statistic, we will find that the biggest hurdle is determining its distribution. It is therefore advisable to choose the test statistic in such a way that this is possible, or even relatively easy.
In our variance example, it turns out that the simple ratio between the sample and the theoretical variance is the most expedient. Intuitively, this also makes sense: an additional arbitrary factor or a power would only complicate things unnecessarily. A difference between the two would not match the nature of a variance or standard deviation. One rather speaks of something being \(k\) times more precise than of some difference.
Distribution of the Test Statistic
So if we use \(\frac{s^2}{\sigma_0^2}\) as our test statistic, we now have to determine the distribution of this quantity. Writing this quantity out, we get
\begin{equation} \frac{s^2}{\sigma_0^2} = \frac{\sum_{i=1}^n (X_i - \bar{X})^2}{(n-1)\sigma_0^2} = \frac{1}{n-1} \sum_{i=1}^n \Big(\frac{X_i - \bar{X}}{\sigma_0}\Big)^2 \end{equation}where we can see that this chosen ratio almost standardizes the random variables \(X_i\), because for \(s^2\) we subtract the sample mean and then divide by \(\sigma_0\). The only inaccuracies are that we actually would have to subtract the theoretical mean \(\mu\), and that we of course have to assume \(H_0\) to be true, because only then does \(\sigma_0^2\) actually correspond to the theoretical variance. But this is the assumption for this whole test anyway, which is why it is called a hypothesis.
Regarding the theoretical mean \(\mu\): let us assume for a moment that we actually subtracted it; then we get
\begin{equation} \frac{1}{n-1} \sum_{i=1}^n \Big(\frac{X_i - \mu}{\sigma_0}\Big)^2 = \frac{1}{n-1} \sum_{i=1}^n Z_i^2, \end{equation}where we denote the now standard normally distributed random variables by \(Z_i\). What we can already conclude from this is the following: if we had chosen
\begin{equation} \frac{(n-1)s^2}{\sigma_0^2} \end{equation}as our test statistic, i.e., simply scaled by the constant factor \(n-1\), the test statistic would simplify even further, in terms of its distribution, to
\begin{equation} \sum_{i=1}^n \Big(\frac{X_i - \mu}{\sigma_0}\Big)^2 = \sum_{i=1}^n Z_i^2 \end{equation}All of this, of course, under the assumption that we actually subtracted the theoretical mean. But what error are we actually making if we take the sample mean instead? Let us take a closer look by splitting the difference between each observation and the theoretical mean into the difference to the sample mean \(\bar{X}\) and the difference from \(\bar{X}\) to the theoretical mean \(\mu\). This leads us to
\begin{equation} \frac{\sum_{i=1}^n((X_i-\bar{X}) + (\bar{X}-\mu))^2}{\sigma_0^2}. \end{equation}Now we can see what happens to the error \(\bar{X}-\mu\) we are making and how it affects the resulting distribution.
In principle, all we did here is add a zero, i.e., \(-\bar{X}+\bar{X}\). However, this typical trick along the lines of “now we add a cleverly chosen zero” often seems quite counterintuitive, and one wonders how anyone comes up with something like that in the first place. If, on the other hand, we look at it as above, i.e., as splitting the difference into two parts, the whole thing arises much more naturally, and we understand how one comes up with this idea.
After multiplying out the terms, the above equation can be rearranged to the following
\begin{equation} \sum_{i=1}^n\Big(\frac{X_i-\bar{X}}{\sigma_0}\Big)^2 + \sum_{i=1}^n\Big(\frac{\bar{X}-\mu}{\sigma_0}\Big)^2 \end{equation} \begin{equation} + 2\sum_{i=1}^n\frac{(X_i-\bar{X})(\bar{X}-\mu)}{\sigma_0^2}. \end{equation}Note that the last term is zero, since \(\sum_{i=1}^n(X_i-\bar{X})\) will always be zero. Another perspective on the whole thing is that the only way to split the difference between \(X_i\) and \(\mu\) into two differences such that our equation is just the sum of two squared differences (without the \(2ab\) part of the binomial expression) is to use the sample mean \(\bar{X}\) as the splitting point. Let us keep that in mind, as we will need it later.
Let us continue with determining the distribution of our test statistic: What have we found out so far? Summing things up, we can state that
\begin{equation} \sum_{i=1}^n Z_i^2 = \frac{(n-1)s^2}{\sigma_0^2} + \sum_{i=1}^n\Big(\frac{\bar{X}-\mu}{\sigma_0}\Big)^2, \end{equation}which in turn can be further simplified to
\begin{equation} \frac{(n-1)s^2}{\sigma_0^2} = \sum_{i=1}^n Z_i^2 - n\Big(\frac{\bar{X}-\mu}{\sigma_0}\Big)^2. \end{equation}Those who know the distribution of the sample mean of a normal distribution, something we examined more closely in the story Distribution of the Sample Mean of a Normally Distributed Sample, will already see where this is going. Because it turns out that the factor \(n\) now exactly standardizes the last term as well: the expression inside the square, \(\sqrt{n}\,\frac{\bar{X}-\mu}{\sigma_0}\), also follows a standard normal distribution, so we can simply write \(Z^2\) for the last term.
In principle, we have thus already been able to reduce our test statistic, in terms of its distribution, to a sum or difference of squared standard normally distributed random variables. That is already something. With the help of our small insight from before, with \(\bar{X}\) as a special splitting point, we will see that it gets even a bit simpler.
Let us regard our random variables \(Z_i\) as the coordinates of a vector space, where each random variable is represented by one coordinate axis. Since we assume independent observations, the joint density in this space is simply given by multiplying the individual densities
\begin{equation} f(z_1,\ldots,z_n) = \prod_{i=1}^n\frac{1}{\sqrt{2\pi}}e^{-\frac{z_i^2}{2}} = (2\pi)^{-\frac{n}{2}}e^{-\frac{1}{2}(z_1^2+\cdots+z_n^2)}. \end{equation}So we obtain the multivariate standard normal distribution, which is rotationally symmetric, because its density only depends on the length of the vector. In this geometric interpretation, the sum of squares \(\sum Z_i^2\) in the equation above simply means the squared length of the vector \(\vec{Z} = (Z_1,\ldots,Z_n)\). But what does subtracting \(Z^2\), the square of our likewise standard normally distributed expression, mean?
Because of \(\bar{Z} = \frac{1}{n}\sum Z_i = \frac{\bar{X}-\mu}{\sigma_0}\), we can write this expression as
\begin{equation} Z = \sqrt{n}\,\bar{Z} = \frac{Z_1+\cdots+Z_n}{\sqrt{n}} = \vec{Z}\cdot\vec{u}, \quad \text{with} \quad \vec{u} = \frac{1}{\sqrt{n}}(1,\ldots,1) \end{equation}So \(Z\) is nothing but the coordinate of our vector along the diagonal, i.e., in the direction of the unit vector \(\vec{u}\). And this is exactly where \(\bar{X}\) as a special splitting point comes into play: our splitting from before decomposes \(\vec{Z}\) into the part \(\bar{Z}\cdot(1,\ldots,1)\) along the diagonal and the remainder \((Z_1-\bar{Z},\ldots,Z_n-\bar{Z})\), whose squared length is exactly our test statistic \(\frac{(n-1)s^2}{\sigma_0^2}\). Since the components of this remainder sum up to 0, it is perpendicular to \(\vec{u}\). The vanished \(2ab\) part is therefore simply the dot product of two orthogonal vectors, and our equation is de facto the Pythagorean theorem.
Now let us rotate the coordinate system such that the last axis points in the direction of \(\vec{u}\) and the remaining \(n-1\) axes are perpendicular to it. A rotation changes neither lengths nor volumes, and since the joint density only depends on the length, the new coordinates \(Y_1,\ldots,Y_n\) are again independent and standard normally distributed. The last coordinate is exactly \(Y_n = Z\), and hence
\begin{equation} \frac{(n-1)s^2}{\sigma_0^2} = \sum_{i=1}^n Z_i^2 - Z^2 = \sum_{i=1}^n Y_i^2 - Y_n^2 = \sum_{i=1}^{n-1} Y_i^2. \end{equation}So the subtraction simply removes the part of the vector along one axis, as illustrated in the following figure.
For the final result, this means that exactly one of the \(n\) squares (in the rotated coordinates \(Y_i\)) cancels out of the sum. Incidentally, this geometric insight also shows beautifully how the mean essentially corresponds to a projection onto the diagonal (red), while the variance results from the perpendicular distance to the diagonal (yellow). This is something we pursue further in the story Discovering a New Formula for the Sample Variance, where it leads us to a new formula for calculating the sample variance.
Continuing with our story here, we see that we are left with a sum of \(n-1\) terms
\begin{equation} \frac{(n-1)s^2}{\sigma_0^2} = \sum_{i=1}^{n-1} Y_i^2, \quad Y_i \sim \mathcal{N}(0,1) \end{equation}So we have now found out that, in terms of its distribution, our test statistic simply corresponds to the sum of \(n-1\) squared, independent, standard normally distributed random variables. The question now is: how can we determine the distribution of such a sum? That is a somewhat larger excursion, which we undertake in the story Derivation of the Chi-Squared Distribution. Going through everything again in detail here would make this story a bit too extensive. If you are not yet familiar with this derivation in detail, it would therefore be a good idea to continue with that story now and then come back here afterwards.
To sum up, let us just mention here that this sum and, as already noted, also the density are constant along every spherical shell. Determining the density can ultimately be reduced to determining the surface area of an \((n-1)\)-dimensional sphere. What comes out is the so-called chi-squared distribution.
Now that we have determined the distribution of our test statistic
\begin{equation} \frac{(n-1)s^2}{\sigma_0^2} \sim \chi^2_{n-1} \end{equation}we know how likely different values of it are. In particular, we can now define acceptance and rejection regions and draw conclusions about for which values it would be advisable not to reject \(H_0\) and for which, on the other hand, it would be advisable to reject it. Let us look at this in a concrete application in the next part.
To conclude this section, let us briefly mention the other candidates for our test statistic. Actually, all of them are monotonic functions of \(s^2\). Hence, once we know the distribution of \(\frac{(n-1)s^2}{\sigma_0^2}\), their distributions follow directly by a simple change of variables, and they even lead to exactly the same test decisions. The only real exception is \(s - \sigma_0\), whose distribution under \(H_0\) still depends on \(\sigma_0\) itself, so we would need a different distribution for every \(\sigma_0\). Our choice is simply the one for which everything falls into place most neatly, because it directly reduces to a sum of squared standard normally distributed random variables. Therefore, we will set the others aside. But of course, for a fun late-night activity, feel free to give them a try.
Applying a Statistical Test
So, now assuming we have agreed on the test statistic \(\frac{(n-1)s^2}{\sigma_0^2}\) and we have determined its distribution \(\mathcal{D}\) (in our concrete example, this is the chi-squared distribution), how can we now apply this test in practice?
Let us go through the whole thing using an example. Imagine a company producing steel beams that are supposed to be 3.5 meters long. The manufacturer of the machines used for production guarantees a precision of \(\sigma_0=1.5\,\text{mm}\), where the lengths of the beams are assumed to be normally distributed. To make sure that the machines actually meet these specifications, a sample of 20 steel beams is taken at the end of each day and their lengths are measured. From these measurements, the sample variance is then calculated. Finally, the company checks with the help of a statistical test whether the sample is fine, i.e., whether the obtained sample variance fits the theoretical variance corresponding to the precision of 1.5 mm.
In line with what we discussed above, we choose \(T = \frac{(n-1)s^2}{\sigma_0^2}\) as our test statistic. Since we have determined its distribution, we can now make statements about how likely it is to observe a certain value of \(T\), under the assumption that the null hypothesis \(H_0: \sigma^2 = \sigma_0^2\) is true. If it does not hold, the derived distribution is no longer valid either, because \(T\) can then no longer be reduced to a sum of squared standard normally distributed random variables.
Today, the measured lengths yield a sample variance of \(s^2=0.0000031\). Plugging the numbers into our test statistic, we get
\begin{equation} T = \frac{19 \cdot 0.0000031}{0.00000225} = 26.1777. \end{equation}Let us now look at this result graphically by taking a look at the probability density function \(\mathcal{D}_{PDF}\) of our test statistic \(T\).
If the variance of the sample were exactly equal to the theoretical one, their ratio would be 1 and \(T\) would thus be given by \(n-1\), which in our case means 19. The more the value deviates from this expected value, the more unlikely this becomes. It should be noted here that the density reaches its maximum slightly below the expected value (more precisely at \(n-3 = 17\)), but drops sharply towards the edges, as can be seen in the figure above. We see that our value of 26.1777 already deviates somewhat from the expected value and in any case already lies in a region where the density is lower. The probability of observing such a value if the true standard deviation really is \(\sigma_0 = 1.5\,\text{mm}\) is therefore certainly already lower. But is this already enough for us to reject \(H_0\) with a clear conscience?
The answer to this question depends on how certain we want to be. Even if the test statistic takes on a value of 50, it is possible, albeit very rare, that this happens although \(\sigma\) is actually only 1.5 mm. So no matter how far from 19 we set our boundary for rejecting the null hypothesis, there is always a certain chance that we have simply observed a very rare event and that it would actually be correct not to reject \(H_0\).
However, we must set this boundary somewhere; otherwise, we might as well abandon statistical tests altogether. Consequently, we accept that in some rare cases, we will make an error and reject \(H_0\) when we should not. This type of error is known as a type I error, and the probability of it occurring is denoted by \(\alpha\). The value of \(\alpha\) directly corresponds to where we set our cut-off boundary.
For example, if we want a significance level of \(5\%\), i.e., we want to make the error of rejecting \(H_0\) although we actually should not in only \(5\%\) of cases, this means that we set the boundaries such that the cumulative probability at the tails of the distribution \(\mathcal{D}\) sums up to \(5\%\) in total. So at each tail, a region of \(2.5\%\) will lie outside our acceptance region, as shown by the red areas in the figure above.
Furthermore, there is of course also the other type of error: when we do not reject \(H_0\) although we actually should. This is the so-called type II error. What is its probability? That depends on how far off we actually were with the assumption that \(H_0\) is true. After all, this assumption was also the basis for determining the distribution \(\mathcal{D}\). However, if \(\sigma_0\) is no longer the true standard deviation, the terms in our test statistic \(T\) are no longer standardized to a standard normal distribution. Hence, the distribution of \(T\) will be a different one. Let us examine more closely how it changes.
Our test statistic is no longer exactly normalized by \(\sigma_0\). It is, however, of course normalized by the actual theoretical variance \(\sigma^2\). So we can write
\begin{equation} T = \frac{(n-1)s^2}{\sigma^2} \cdot \frac{\sigma^2}{\sigma_0^2} \end{equation}and hence the first part will still follow exactly the distribution \(\mathcal{D}\), as it can again be reduced to the sum of squared standard normally distributed random variables. The second term, however, is just a constant. This means that our new distribution of \(T\) for the case that \(H_0\) is in truth not correct, let us call it \(\mathcal{D}^{H_A}\), will simply be a horizontally stretched version of \(\mathcal{D}\), where the stretching factor is given by \(\frac{\sigma^2}{\sigma_0^2}\). Looking at the following figure, we see the two density functions (one for the case that \(H_0\) is actually true and one for the case that it is not).
Let us elaborate on that second type of error a little more based on our example. We had \(\sigma_0=1.5\,\text{mm}\) but let us say it actually worsened to \(\sigma=2.5\,\text{mm}\) as in the figure above. So the null hypothesis \(H_0\) is not met and the alternative hypothesis \(H_A\) is true and the actual distribution of \(T\) is \(\mathcal{D}^{H_A}\), which is \(\mathcal{D}\) stretched by a factor of \(\frac{6.25}{2.25}\). However, what does this mean when we apply the statistical test?
This has the consequence that our assumption about the distribution of \(T\) itself was wrong. We are assessing the likelihoods based on \(\mathcal{D}_{PDF}\) and we defined a \(2.5\%\) cut-off boundary at each tail of this distribution. This means that between those values we do not reject \(H_0\). However, the test statistic \(T\) does not follow that distribution. If \(H_A\) is true it follows \(\mathcal{D}^{H_A}\). But of course, this distribution will also have some non-zero probability in our acceptance range, and in all those cases we will make an error, namely the type II error.
To determine the probability of that error, usually denoted as \(\beta\), we have to look at the actual distribution of \(T\) in that case, which is \(\mathcal{D}_{PDF}^{H_A}\). To be more precise, all we have to do is take the cumulative probability of \(\mathcal{D}_{PDF}^{H_A}\) where it falls in our acceptance range. Looking at the illustration below, we see there is quite a high probability that \(T\) will take on a value in that range.
If one experiments with different values for \(\sigma\) and \(\alpha\), one will notice two things. First, if we increase \(\alpha\) (i.e., we are more willing to accept type I errors), the probability \(\beta\) of a type II error will generally decrease. Second, if \(\sigma \neq \sigma_0\) (so \(H_0\) should ideally be rejected) but \(\sigma\) is nevertheless very close to \(\sigma_0\), the probability \(\beta\) shoots up and becomes enormously high. This is because the stretched distribution \(\mathcal{D}_{PDF}^{H_A}\) is then almost identical to the originally assumed distribution \(\mathcal{D}_{PDF}\). Imagine that the curve of \(\mathcal{D}_{PDF}^{H_A}\) in the figure above were scaled back so that it almost coincides with \(\mathcal{D}_{PDF}\). This would result in a strong increase of the blue area, i.e., of the probability of making a type II error.
However, this is usually not a big issue. In most cases, we are not testing for exact equality. For example, if the company accepts a sample because it passed the test but the true standard deviation \(\sigma\) is actually 1.58 mm instead of 1.5 mm, this small difference is generally negligible. Moreover, this difference is so minor that it would be difficult to detect based on a sample alone. To identify such a small deviation, one would need to measure nearly every steel beam produced to achieve a sufficiently large sample size that would make this variation detectable.
Conclusion and Outlook
With this we will wrap it up for this story. We have delved into the foundations of statistical tests, focusing on the test of sample variance against a theoretical variance. We started by designing a test statistic, then showed why it is necessary to derive its distribution, and finally applied this to determine how probable our observed test statistic is under the assumption that the sample comes from a distribution with the specified variance. Along the way, we also saw the two types of errors such a test can make and how they depend on where we set our boundaries.
Moving forward, we will expand on these fundamentals by exploring other statistical tests and their applications. We will also look into more complex scenarios where multiple variables are involved and how to handle these cases efficiently. Additionally, we will work through the derivation of further distributions of test statistics, such as the t-distribution. So stay tuned for upcoming stories.