# How can I use the distribution of run lengths to test if a sequence is generated from flips of a fair coin?

I have a very long sequence (in the tens of thousands) of binary outcomes from some data-generating process. I believe that these outcomes are iid Bernoulli trials with p = 0.5, equivalent to flipping a fair coin. From this sequence I construct a table of frequencies for runs of heads of various lengths, all the lengths that appear, and likewise for tails.

1. It seems intuitively that there should be an expected value for the number of runs of each length, given the total length of the sequence . How might I calculate that expected value?
2. It seems intuitively that either a surplus or a deficit of the number of runs of each specified length would, if sufficiently large, suggest rejection of the fair coin hypothesis. How might I take the totality, or profile, of such deviations for all the different run lengths to test the hypothesis that the entirety of the sequence was generated by independent Bernoulli trials, as described above.