# Test for IID sampling

How would you test or check that sampling is IID (Independent and Identically Distributed)? Note that I do not mean Gaussian and Identically Distributed, just IID.

And idea that comes to my mind is to repeatedly split the sample in two sub-samples of equal size, perform the Kolmogorov-Smirnov test and check that the distribution of the p-values is uniform.

Any comment on that approach, and any suggestion is welcome.

Clarification after starting bounty: I am looking for a general test that can be applied to non time series data.

-
Is it time series data? – danas.zuokas May 18 '12 at 9:40
@gui11aume have you tried the "eyeball" test? That is, plot the data and see if it looks IID. – Macro May 18 '12 at 12:57
I haven't. I am not sure what you mean: plot the values in the order they come (possibly random)? And then check the absence of striking pattern? – gui11aume May 18 '12 at 13:12
Did you have a look at "the run test" ? en.wikipedia.org/wiki/Wald%E2%80%93Wolfowitz_runs_test – Stéphane Laurent May 27 '12 at 8:23
Sorry. I was having in mind the following run test: apprendre-en-ligne.net/random/run.html (but this is written in French) – Stéphane Laurent May 28 '12 at 14:59

What you conclude about if data is IID comes from outside information, not the data itself. You as the scientist need to determine if it is a reasonable to assume the data IID based on how the data was collected and other outside information.

Consider some examples.

Scenario 1: We generate a set of data independently from a single distribution that happens to be a mixture of 2 normals.

Scenario 2: We first generate a gender variable from a binomial distribution, then within males and females we independently generate data from a normal distribution (but the normals are different for males and females), then we delete or lose the gender information.

In scenario 1 the data is IID and in scenario 2 the data is clearly not Identically distributed (different distributions for males and females), but the 2 distributions for the 2 scenarios are indistinguishable from the data, you have to know things about how the data was generated to determine the difference.

Scenario 3: I take a simple random sample of people living in my city and administer a survey and analyse the results to make inferences about all people in the city.

Scenario 4: I take a simple random sample of people living in my city and administer a survey and analyze the results to make inferences about all people in the country.

In scenario 3 the subjects would be considered independent (simple random sample of the population of interest), but in scenario 4 they would not be considered independent because they were selected from a small subset of the population of interest and the geographic closeness would likely impose dependence. But the 2 datasets are identical, it is the way that we intend to use the data that determines if they are independent or dependent in this case.

So there is no way to test using only the data to show that data is IID, plots and other diagnostics can show some types of non-IID, but lack of these does not guarantee that the data is IID. You can also compare to specific assumptions (IID normal is easier to disprove than just IID). Any test is still just a rule out, but failure to reject the tests never proves that it is IID.

Decisions about whether you are willing to assume that IID conditions hold need to be made based on the science of how the data was collected, how it relates to other information, and how it will be used.

Edits:

Here are another set of examples for non-identical.

Scenario 5: the data is residuals from a regression where there is heteroscedasticity (the variances are not equal).

Scenario 6: the data is from a mixture of normals with mean 0 but different variances.

In scenario 5 we can clearly see that the residuals are not identically distributed if we plot the residuals against fitted values or other variables (predictors, or potential predictors), but the residuals themselves (without the outside info) would be indistinguishable from scenario 6.

-
The first part of this answer, in particular, seems a little bit confused (or confusing) to me. Being iid is a well-defined mathematical property of a finite set of random variables. Your scenarios 1 and 2 are identical if the random variables in the second case are obtained "after losing the gender information". They're iid in both cases! – cardinal May 27 '12 at 1:15
GregSnow I don't completely agree with your assertion. It may be that you know that data come from a sequence of identically distributed random variables. You don't know exactly what model generated it. It could be that they are independently generated or alternately came from a stationary time series. To decide which is the case suppose that you know that the identical distribution is normal. Then both possiblities fall under the category of a stationary sequence and it will be iid if and only all the nonzero lag autocorrelations are 0. It is perfectly reasonable to test to see if the correla – Michael Chernick May 27 '12 at 2:27
@cardinal, so do you agree that the data in scenario 2 is not identically distributed before losing the gender information? So we would have a case where they are not identical, but the only way to tell the difference is to use information outside of the variable being looked at (gender in this case). Yes being IID is a well defined mathematical property, but so is being an integer, can you test whether the data point 3. is an integer stored as a floating point number or a continuous value that has been rounded without outside information about where it came from. – Greg Snow May 27 '12 at 3:34
So what you are saying is that there might exist some additional information contained in variables $Z$ so that marginally $X_i \perp X_j, i\neq j$, but $X_i|Z$ may no longer be independent of $X_j|Z$. In the first case, $Z$ is the vector of gender labels; in the second case, $Z$ is the design information. I think that's a good observation. – StasK May 27 '12 at 4:44
But all of what you say above uses information about how the data was collected/generated, not just the data itself. And even if we have data that supports that there is no time series autocorrelation that does not tell us anything about spatial correlation or other types of non-independence. Can we really test for every possible type of dependence and get meaningful results? or should we use information about how the data was collected to guide which tests are most likely to be meaningful? – Greg Snow May 27 '12 at 6:26
well, for a non-stationary time series it may not even sense to look at the autocorrelation as a function of lag. If ${\rm cor}(y_{t}, y_{s}) = f(s,t)$ and $f(s,t)$ is not a function of only $|s-t|$ then all sorts of weird things can happen by pretending it is. I'm really just asking if you have any ideas for the case where you know the time series is not stationary – Macro May 18 '12 at 12:45