TITLE: Determining whether a sequence of coin flips is random
QUESTION [6 upvotes]: Say we have observed a finite sequence of coin flips. Is there a metric for how likely this sequence is generated by a truly random coin flip. 
For example, if we flip a coin 1000 times presumably seeing 500 heads and then 500 tails is less likely than a random assortment of Heads and Tails. 
Also, presumably we would not see every even flip be a tails and every odd flip to be a heads.

REPLY [6 votes]: There are some different metrics which can be used to indicate randomness when flipping a  coin. One of these is to analyze the length of longest runs. 

Longest runs:
A run is a sequence of consecutive heads or tails. We can calculate the probability of runs of length $k$ of a fair coin via the generating function
  \begin{align*}
W_k(z)=\frac{1-z^{k+1}}{1-2z+z^{k+1}}
\end{align*}
  where the coefficient $[z^n]$ of $W_k(z)$ gives the number of possibilities to have runs of length $\leq k$ when flipping a coin $n$ times. The probability for a run having length $k$ is therefore
  \begin{align*}
\frac{1}{2^n}[z^n]\left(\frac{1-z^{k+1}}{1-2z+z^{k+1}}-\frac{1-z^{k}}{1-2z+z^{k}}\right)
\end{align*}

The derivation of this generating function is given in detail in section I.4.1 in  Analytic Combinatorics by Philippe Flajolet and Robert Sedgewick.

They also cite a nice story attributed to T. Varga (p. 52) around this theme:

''A class of high school children is divided into two sections. In one of the sections, each child is given a coin which he throws two hundred times, recording the resulting head and tail sequence on a piece of paper. In the other section, the children do not receive coins, but are told instead that they should try to write down a ‘random’ head and tail sequence of length two hundred. Collecting these slips of paper, [a statistician] then tries to subdivide them into their original groups. Most of the time, he succeeds quite well.” 
The statistician’s secret is to determine the probability distribution of the maximum length of runs of consecutive letters in a random binary word of length $n$ (here $n=200$). The probability that this parameter equals $k$ is
  \begin{align*}
\frac{1}{2^{200}}[z^{200}]\left(\frac{1-z^{k+1}}{1-2z+z^{k+1}}-\frac{1-z^{k}}{1-2z+z^{k}}\right)
\end{align*}
  The probabilities are then easily computed using any symbolic package: for $n=200$, the values found are 
  $$
\begin{array}{r|ccccc}
k&3&4&5&\color{blue}{6}&\color{blue}{7}\\
\hline\\
\mathbb{P}(k)&6.54\,10^{-8}&7.07\,10^{-4}&0.0339&\color{blue}{0.1660}&\color{blue}{0.2574}\\
\\
\\
k&\color{blue}{8}&\color{blue}{9}&\color{blue}{10}&\color{blue}{11}&\color{blue}{12}\\
\hline\\
\mathbb{P}(k)&\color{blue}{0.2235}&\color{blue}{0.1459}&\color{blue}{0.0829}&\color{blue}{0.00440}&\color{blue}{0.0226}\\
\end{array}
$$
  Thus, in a randomly produced sequence of length $200$, there are usually runs of length $6$ or more: the probability of the event turns out to be close to $97\%$ (and there is still a probability of about $8\%$ to have a run of length $11$ or more). On the other hand most children (and adults) are usually afraid of writing down runs longer than $4$ or $5$ as this is felt as strongly “non-random”. The statistician simply selects the slips that contain runs of length $6$ or more as the true random ones. Voilà! 


Another metric is the first and last equality of number of heads and tails when flipping a fair coin $n$ times. This is strongly connected with the so-called Arcsine law. The following is from chapter III: Fluctuations in Coin Tossing and Random Walks of the classic An Introduction to Probability Theory and Its Applications, Vol. I by W. Feller.

Arcsine law:
Let's assume we play a game, flipping a fair coin. We interprete resulting sequences of heads and tails as random walks starting in $(0,0)$ and going $(1,1)$ if head occurs and $(1,-1)$ if tail occurs.
Then the following is valid: With probability $\frac{1}{2}$ no equalization occurs in the second half of the game regardless of the length of the game. Furthermore, the probabilities near the end point are greatest.
This is due to the Arcsine law for last visits (see e.g. Vol 1, ch.3, section 4, Theorem 1 in W. Feller's book): The   probability that up  to  and  including epoch $2n$ the last  visit to the origin occurs at  epoch  $2k$ is   given by
  \begin{align*}
\alpha_{2k,2n}=\frac{1}{4^n}\binom{2k}{k}\binom{2n-2k}{n-k}
\end{align*}
  Since according to Stirling's  formula
  \begin{align*}
\binom{2k}{k}\sim    \frac{1}{\sqrt{\pi    k}}
\end{align*}
  it     can  be shown that for fixed $0<x<1$ and $n$ sufficiently large
  \begin{align*}
\sum_{k<xn}\alpha_{2k,2n}\approx  \frac{2}{\pi}\arcsin \sqrt{x}
\end{align*}

Other metrics are the number of changes of sign (more heads than tails or vice versa), first passages and returns to the origin,  the equidistribution of heads and tails, etc. Chapter III by W. Feller provides a thorough introduction.