Runs Test for Detecting Non-Randomness using Excel
The goal of any continuous improvement program is having a process that exhibits a state of statistical control. So, having a way to detect when a process is going out of statistical control is an important feature of a monitoring system. A system that exhibits statistical control yields a stream of data that is random. When data behaves randomly such data should be independent and identically distributed. In this article I explore the use of a single sample Runs Test to detect non-randomness in a set of data.
Properties of Random Data
Independence simply means that the current state of the process that contributes to the current data point should not determine what the next data value should be. In other words, each of the six potential sources of variation (Man, Method, Machine, Material, Mother Nature, and Measurement) are not more active than any of the other sources. When each of these sources of variation behave randomly then the data they yield will also behave randomly. If one or more of these sources of variation changes significantly then the data value may experience a significant change. When this happens then the data stream may experience an event that results in non-randomness.
Univariate Random model
A defining feature of random data is that it shares a common mean and standard deviation. We can typically observe this when we create a histogram and observe a bell-shaped curve having a single peak. If this is the case, we then have a fixed univariate distribution and say that the data are identically distributed. If this assumption is true then we have a constant mean and standard deviation that can be modelled as:
In this expression, Yi, is our data value. That value is equal to an average, , plus or minus some deviation, εi, from
.
Randomness is one key assumption in determining if a univariate process is in statistical control. If the data that describes a process is not random then we have evidence of special cause variation. Then hopefully a Quality Engineer can identify a root cause and implement measures to address such special cause variation. In the event such measures are unsuccessful then an alternative model may be needed. Such a model may be a times series model or a non-linear model with time as the independent variable.
Types of Runs Test to Detect Non-Randomness
So, let’s look at two procedures that help us determine whether a data set behaves randomly or not. The following procedure is a Runs Test for detecting non-randomness. In this contribution, I will discuss a procedure that detects runs above or below a reference value. In a follow up contribution, I will discuss a second procedure detects serial runs up or down.
These Run Test procedures require that we code each data using a + or – sign to indicate if a data falls above or below a reference value. This sign convention is also used to distinguish serial trends up or down.
Runs Test to Detect Non-Randomness Above/Below a Reference Value
The following procedure helps identify shifts in the process mean above or below a reference value. The reference value is either the sample average or the median for a set of data. This is shown in Figure 1.
As shown in Figure 1, there are two runs, both having a length of 8. The values above the reference line are coded with a plus sign (+). Likewise, the values below the reference line are coded with a negative sign (-). This is shown in Figure 2.
How to handle Values Equal to a Reference
When a value equals a reference, it is neither above (+) or below (-) that reference. So, how do we treat such a value? Let’s consider the following treatment conditions. First, skip the value, do not count it. This is the most common practice. However, this decreases the sample size and if the final sample size is less than 25, we should use the Exact Test instead of the Asymptotic Z Test. Second, treat the value as above (+). Third, treat the value as below (-). Last, treat the value the same as the previous value. For large data sets, option one is the most practical. However, when the data set is small and there are several values that equal the reference then I often analyze all cases and see if their conclusions differ.
Consider the following example. Here I show positive (+) runs and negative (-) runs. The values that equal the reference are shown using a zero (0).
+ + + + – – – + + – – 0 – – – + + + – – 0 + + – – + + + + – – – – – +
Let’s count the positive and negative runs for the cases described earlier.
Case 1: Skip Zero
+ + + + – – – + + – – – – – + + + – – + + – – + + + + – – – – – +
Positive Runs = 6, Negative Runs = 5
Case 2: Treat Zero as Positive
+ + + + – – – + + – – + – – – + + + – – + + + – – + + + + – – – – – +
Positive Runs = 7, Negative Runs = 6
Case 3: Treat Zero as Negative
+ + + + – – – + + – – – – – – + + + – – – + + – – + + + + – – – – – +
Positive Runs = 6, Negative Runs = 5
Case 4: Treat Zero as Previous
+ + + + – – – + + – – – – – – + + + – – – + + – – + + + + – – – – – +
Positive Runs = 6, Negative Runs = 5
Each of these cases are summarized in table 1.
As shown in table 1, cases 1, 3, and 4 yield the same number of positive and negative runs and together they differ from case 2. Case two has 7 positive and 6 negative runs. This case sees an increase in the total number of runs from 11 to 13. Since the sample size, n, exceeds 25 we would use the Asymptotic Z Test. We would use this test and evaluate if Case 2 and Case 1 differ in their conclusions. Please note, I selected Case 1 given the smaller sample size (33). If both cases yield the same conclusion then it would reason that comparing Cases 3 and 4 to Case 1 would render the same conclusion as well.
Runs Test Procedure for Non-Randomness Above/Below a Reference Value
Step 1:
Define the runs test as a hypothesis we seek to test.
H0: the runs were produced in a random manner
Ha: the runs were not produced in a random manner
Step 2:
Compute the average or median for the data set.
Step 3:
Code the values above or below the average using a positive (+) or negative (-). A run is then defined as a consecutive sequence of positive or negative values.
Step 4:
Count the total number of runs, R.
Step 5:
Count the number of positive n(+) and negative n(-) values.
Step 6:
Compute the expected number of runs, .
In this expression, n(+) and n(-) are the number of positive and negative values in the data series.
Step 7: Compute the standard deviation for the number of runs, sR.
In this expression, n(+) and n(-) are the number of positive and negative values in the data series.
Step 8: Compute Asymptotic Z Test statistic.
Please note, a continuity correction can be applied to our Asymptotic Z Test. If the actual number of runs, R, is larger than the expected number of, , we use the following Z statistic.
If the actual number of runs, R is less than the expected number of runs, , we use the following Z statistic.
In these Z expressions, R, is the total number of number of runs, , is the expected number of runs, and sR is the standard deviation of the number of runs.
Step 9: Define the alpha risk (α). This value is typically set at 0.05.
Step 10: Determine the critical value Z1-∝/2.
We reject the null hypothesis if the absolute value of our test statistic, Z, exceeds the critical value defined by our alpha risk. Standard practice assumes and alpha risk of 5% or α=0.05. The critical Z value used here splits the alpha risk between the upper and lower tails of the standard normal distribution. This is so because we have no prior knowledge to suggest if the actual number of runs will be too few or to many. If the computed Z value is statistically significant and negative this implies there are too few runs. Likewise, if the value is statistically significant and positive it implies there are too many runs.
The critical value for Z is obtained from a Standard Normal table when n(+) + n(-) ≥ 25.
Runs Test Procedure to Detect Non-Randomness Above/Below an Average
The following data set of n=50 observations was randomly generated using a Normal Distribution having a mean of 50 and standard deviation of 5. The data appears in Table 1.
Step 1:
Let’s state our hypothesis.
H0: the runs were produced in a random manner
Ha: the runs were not produced in a random manner
Step 2:
The average for this data set is 50.0996.
Step 3:
In Table 2, the values above the average appear in RED cells. Likewise, the values below the average appear in WHITE cells. In this example, none of the values equaled the average.
I used Excel conditional formatting to highlight the cells in RED having an average greater than 50.0996. I show this in Figure 3.
Step 4: In table 2, we can count the total number of runs. To do so, count the number of continuous red and white runs starting in the first column and counting across the remaining columns. For the data in table 2, the total number of runs, R = 23.
Step 5:
Count the number of Red (+) and White (-) values. From table 2, we have n(+) = 26 positive (red) values and n(-) = 24 negative (white) values.
Step 6:
Let’s compute the expected number of runs, .
Notice that the expected number of runs, R ̅, is higher than the actual number of runs, R. As such, we would expect our computed Z value would be a small negative value.
Step 7:
Let’s now compute the standard deviation for the number of runs, sR, using the expression below.
Step 8:
Now we can compute the Z statistic using the expression below.
The negative Z value implies that we have too few runs. However, we don’t know if this is statistically significant until we compare this value to our critical Z value in the following step.
Step 9:
Standard statistical practice assumes an alpha risk of 5% or α=0.05. Let’s use this value and determine the critical upper tail Z value.
As shown, the critical upper tail Z value is 1.96. We can find this value in a normal standard table or using the following Excel formula, =ABS(NORM.S.INV(0.025)).
Step 10:
Now that we have computed Z and critical Z (Z1-α/2) values we can determine if the absolute value, |Z|, exceeds the critical value (Z1-α/2). If it does, we can reject the null hypothesis and concluded that the runs were not produced randomly.
Since the absolute value of our test statistic, Z, is less than the critical value we fail to reject the null hypothesis and conclude the run were produced in a random order.
Runs Test Procedure for Detecting Non-Randomness Above/Below a Median
In table 3, shown are n=50 observations. This data appears in table 1 with the exception that a few data had their order transposed. This was done for illustrative purposes so as to yield a statistically significant Z value. We will perform a Runs Test on this data and use the median as our central value. Please note the median for the data in tables 1 and 2 are the same.
Step 1:
Let’s state our hypothesis.
H0: the runs were produced in a random manner
Ha: the runs were not produced in a random manner
Step 2:
The median for this data set is 50.5.
Please note, the mean and median tend to differ when the data is skewed. This occurs when one or more values are clustered toward one end of their range and can also include some extreme values. In this case, the median will not be affected whereas the average will change with the level of skewness. When performing a RUNS TESTS, and in the presence of skewness, the median will be more sensitive to detecting RUNS than the mean.
Step 3:
In Table 2, the values above the average appear in RED cells. Likewise, the values below the average appear in WHITE cells. In this example, none of the values equaled the median.
I used Excel conditional formatting to highlight the cells in RED having a median greater than 50.5 This is shown in Figure 4.
Step 4:
In table 3, we can count the total number of runs. To do so, count the number of continuous red and white runs starting in the first column and counting across the remaining columns. For the data in table 2, the total number of runs, R = 17.
Step 5:
Count the number of Red (+) and White (-) values. From table 3, we have n(+) = 25 positive (Red) values and n(-) = 25 negative (white) values.
Step 6:
Let’s compute the expected number of runs, .
Notice that the expected number of runs, , is higher than the actual number of runs, R. As such, we would see a computed Z value that is negative. Given, there are 9 fewer runs (29 – 17) than expected we will want to determine if this is statistically significant.
Step 7:
Let’s now compute the standard deviation for the number of runs, sR, using the expression below.
Step 8:
Now we can compute the Z statistic using the expression below.
The negative Z value implies that we have too few runs. However, we don’t know if this is statistically significant until we compare this value to our critical Z value in the following step.
Step 9:
Standard statistical practice assumes an alpha risk of 5% or α=0.05. Let’s use this value and determine the critical upper tail Z value.
As shown, the critical upper tail Z value is 1.96. We can find this value in a normal standard table or using the following Excel formula, =ABS(NORM.S.INV(0.025)).
Step 10:
Now that we have computed Z and critical Z (Z1-α/2) values we can determine if the absolute value, |Z|, exceeds the critical value (Z1-α/2). If it does, we can reject the null hypothesis and concluded that the runs were not produced randomly.
|-3.05|>1.96
Since the absolute value of our test statistic, Z, is greater than the critical value we reject the null hypothesis and conclude the run were produced in a non-random order. Given the negative value, we conclude that there were too few runs.
Summary
The equations used in this post are not EXACT TESTS, rather, they are standard normal Z tests that depend on the number of values. The standard normal Z test approaches the Exact test when n(+) + n(-) ≥ 25. If this is not the case, then using the Exact test is appropriate. When the sample size is large (≥ 25) then the standard normal Z test yield answers as good as the Exact test.
Given the sample size was n = 50, we used an Asymptotic Z Test not the Exact Test. Second, we did not apply a continuity correction when using our Asymptotic Z Test. To do so, we would have needed to know if the actual number of Runs, R, fell below or above the expected number of runs, . Once we know which side our actual number of runs, R, falls we would use such knowledge and change our hypothesis to a one-side test.
In this post I demonstrated how to detect runs above and below a reference value (mean and median). I trust you found this article interesting and informative. If so, please leave your comment saying so at the end of the post. And please share this content with others that you feel will benefit from reading it on social media.
POPULAR RESOURCES
Please visit these popular blog post resources:
- Xbar and R Chart Formula and Constants – The Definitive Guide.
- Estimating the d2 and d3 Constants Using Minitab.
- D2 Values for the Distribution of the Average Range.
- How to Calculate Gage Repeatability Using the Average Range.
- Control Chart Constant – How to Drive A2 and E2.
- Range Statistics – How to Calculate Standard Deviation.
Leave a Reply