Take a look at these three rows of biscuits. Which one looks most like a randomly ordered sequence to you? I bet you've chosen the one in the middle. The side rows just seem too regular to occur just by chance. There's a formal test to answer the question, whether a sequence of categorical or numerical data is random based on the sequence of the individual labels. It's called a runs test. And in this video I'll explain how it works. Let's start with the meaning of the term run in the context of a data sequence. A run is a succession of identical values or labels which are followed and preceded by different values or labels. So for numerical data you could look at the sequence of values above a threshold or at the sequence of increases and decreases in subsequent values. And for categorical data, you could look at labels of one kind that occur in sequence. One such part of the data with identical properties is called a run, and the number of data items it contains is the run length. The runs test only considers binomial data. If you have numerical data, it will be changed into values above or below a mean or a median, or a series of increases and decreases to make it suitable for this test. A categorical data with more than two categories will be aggregated into two categories. For example, in this variable with four classes, you could consider the class A versus anything else. As the runs test focuses on the ordering of the data, it's crucial that this ordering is unchanged from the moment the data was collected. The runs test counts simply the number of runs in a given binomial data sequence. And this statistic, in spite of being rather simple, gives a good indication of whether the data is random, because if very few runs occur relative to the length of the data series, a time trend or some clustering due to lack of independence is suggested. And if in contrast many runs occur systematic short period fluctuations seem to be of influence. Let's look at an example of how this would work. The local confectioner is making many nice cakes. But for some reason the most popular one is a fruit cake. This may be the case because it's good value for money or it's a traditional treat. But perhaps, there's some interaction between the costumers going on the shop as well. And then, you would see that the sale of this cake would not be random but rather, occur in clusters. Let's check this. We take a sample of the sales during a single morning. And a null hypothesis for this test is that the successive sales are not influencing each other, they are independent. Equivalently, you could say that the sequence is random. It turns out that 17 cakes were sold this morning, of which these 9 were fruit cakes. So we have eight other types of cake that were sold. And if you count the number of runs, you get to a value of six. Now, how can we decide whether the number of runs is a value so extreme that we would reject the null hypothesis? For small samples, which is roughly when either of the two group sizes, M or N, are smaller than ten, there are tabulated values to find a p-value or a rejection reason for a given number of runs. But for larger sample sizes, the number of runs approach the normal distribution. In our example, the sample size of both groups is smaller than ten, and therefore we should use table here. Let's have a look at such a table. As you see, the table specifies the critical values of the number of runs. For each combination of group sizes M and N. The values in the cells are critical levels for 0.95 confidence interval. So if you find a number of runs that's equal to or lower than the smaller value, or equal to or higher than the larger value in the sale, you would reject the null hypothesis at a significance level of 0.05. In our case, we had groups of sizes eight and nine respectively and we counted six runs. As this cell shows, it falls within the confidence range. So the null hypothesis of randomness is not objected. There's no evidence that sales of fruit cakes occur in clusters or are somehow varying systematically. To finalize, I will also show what to do when you have larger samples. In that case, you can estimate the sampling distribution of the number of runs by normal distribution. It's mean, is given by this equation. And it's standard deviation by this equation. M and N refer to the sizes of the two groups in the data. And capital M refers to the total sample size, the sum of M and N. Once you have estimated the mean and standard deviation in this way, you can use the z-test to evaluate whether the number of runs is extremely high or low compared to what you expect for your data set under the null hypothesis. Let me summarize what I explained in this video. Independence of data is an important assumption for many statistical test. But it is an assumption that can also be tested. For example, with the runs test. A run is a succession of identical symbols which are followed and preceded by different symbols. The runs tests uses binomial data. It counts the number of runs in the data and compares this to the expected number of runs under a null hypothesis of a random sequence. Numerical data can be turned into binomial data by considering the sign of the difference between the data values and the mean or median. Or alternatively, the sign of the difference between successive values. For small samples, you use a table to find a p-value for the observed number of ranks. But for larger samples, the number of ranks follows a normal distribution with this mean, and this standard deviation. The test then proceeds as in a z-test.