2
$\begingroup$

Suppose we are given a sequence of heads and tails for 1000 coin tosses. how do we check if it's actually random?

I believe I need to do a hypothesis test, I tried to write some examples to get an idea and I think it has something to do with the number of consecutive heads or tails parts but I'm not sure how to proceed.

$\endgroup$
  • 1
    $\begingroup$ Since all $2^n$ possible sequences are equally likely, no one specific sequence is any more "odd" than any other -- unless you specify the kind of nonrandomness you're looking for (where it makes perfect sense to treat some sequences as indicating that particular kind of nonrandomness). [In particular, you shouldn't decide what you're testing for by looking at the data] $\endgroup$ – Glen_b Jul 4 '18 at 0:25
  • $\begingroup$ @Glen_b In some sense you are right, but how do you explain my idea on the question? the fact that a coin toss sequence with 500 consecutive heads and then 500 consecutive tails is much less likely to happen randomly than one with 20 sections of consecutive heads and tails. $\endgroup$ – Alaleh Ahmadian Jul 17 '18 at 20:35
  • $\begingroup$ If you're suggesting you want to look for the kind of nonrandomness that would tend to produce longer runs than you'd expect from a fair, random coin, then that's exactly what I meant by "unless you specify the kind of nonrandomness you're looking for" (since you are then looking for a particular form of nonrandomness related to longer runs) -- it was not clear to me from your question that this is the kind of nonrandomness you were wishing to identify, but there are certainly tests for that ("runs of one kind" tests; if you don't specify the coin must be fair, that would be Wald-Wolfowitz)... $\endgroup$ – Glen_b Jul 18 '18 at 0:45
  • $\begingroup$ ... see here. If you also apply the restriction that p=1/2 then it can also be done but it changes the distribution. $\endgroup$ – Glen_b Jul 18 '18 at 0:49
  • $\begingroup$ @Glen_b the question is about a fair coin (p=1/2) and from my description of problem I assumed I've specified what kind of nonrandomness I'm looking for, sorry if it wasn't explained correctly, thanks for the tips, I'll look into them $\endgroup$ – Alaleh Ahmadian Jul 18 '18 at 12:14
2
$\begingroup$

Testing whether or not the sequence is "random" can be fairly difficult, since a process can be random in many different ways. To make the problem more manageable, you could test the Hypothesis: $$H_0: X_1, X_2, \cdots X_n \stackrel{iid}{\sim} Bernoulli(\theta)$$

From here, there are several test statistics that you could incorporate to test whether this specification is reasonable. A few choices are listed below. You can add to the list to fit your specific goal.

\begin{align*} T_0 &= \text{ longest streak of $0$'s} \\ T_1 &= \text{ longest streak of $1$'s} \\ T_2 &= \text{ number of state changes} \\ T_3 &= \text{ auto-correlation of lag 1} \end{align*}

Rather than trying to find the sampling distributions of these test statistics under $H_0$, we can test these hypotheses empirically, using the most likely value of $\theta$ (Andrew Gelman does this in Bayesian Data Analysis).


I have simulated a series of $1000$ coin flips from a model which is NOT iid Bernoulli (although it is still random). We could test these hypotheses in R as follows.

  • The proportion of heads in my sample is $\hat\theta = 0.38$
  • $T_0 = 19$
  • $T_1 = 11$
  • $T_2 = 376$
  • $T_3 = 0.198$

Now we can test these statistics against their empirical null as follows. Note that I wrote separate functions for calculating the test statistics above.

 M <- 2000
 T0 <- T1 <- T2 <- T3 <- rep(NA, M)
 for(m in 1:M){
   #Simulate data according to the Null
   x <- rbinom(1000, 1, 0.39)
   #Compute test statistics 
   T0 <- get_T0(x)
   T1[m] <- get_T1(x)
   T2[m] <- get_T2(x)
   T3[m] <- get_T3(x)
 }

And now we plot the empirical null distributions along with the observed value from the data. Clearly the observed data doesn't seem to match the Null distribution here... indicating that our null hypothesis is probably incorrect.

enter image description here

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ thanks, can you tell me why is M=2000 in your code? $\endgroup$ – Alaleh Ahmadian Jul 3 '18 at 23:23
  • $\begingroup$ It was an arbitrary choice, you can choose M to be whatever you want. I'm just simulating data from the null a large number of times, so that the histograms give us an approximation to the null distribution. $\endgroup$ – knrumsey Jul 3 '18 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.