def wald_wolfowitz(sequence):
R = n_runs = sum(<NUM_LIT:1> for s in groupby(sequence, lambda a: a))<EOL>n = float(sum(<NUM_LIT:1> for s in sequence if s == sequence[<NUM_LIT:0>]))<EOL>m = float(sum(<NUM_LIT:1> for s in sequence if s != sequence[<NUM_LIT:0>]))<EOL>ER = ((<NUM_LIT:2> * n * m ) / (n + m)) + <NUM_LIT:1><EOL>VR = (<NUM_LIT:2> * n * m * (<NUM_LIT:2> * n * m - n - m )) / ((n + m)**<NUM_LIT:2> * (n + m - <NUM_LIT:1>))<EOL>O = (ER - <NUM_LIT:1>) * (ER - <NUM_LIT:2>) / (n + m - <NUM_LIT:1.>)<EOL>assert VR - O < <NUM_LIT>, (VR, O)<EOL>SD = math.sqrt(VR)<EOL>Z = (R - ER) / SD<EOL>return {'<STR_LIT:z>': Z, '<STR_LIT>': ER, '<STR_LIT>': SD, '<STR_LIT:p>': zprob(Z), '<STR_LIT>': R}<EOL>
implements the wald-wolfowitz runs test:
http://en.wikipedia.org/wiki/Wald-Wolfowitz_runs_test
http://support.sas.com/kb/33/092.html

:param sequence: any iterable with at most 2 values. e.g.
                 '1001001'
                 [1, 0, 1, 0, 1]
                 'abaaabbba'

:rtype: a dict with keys of
    `n_runs`: the number of runs in the sequence
    `p`: the support to reject the null-hypothesis that the number of runs
         supports a random sequence
    `z`: the z-score, used to calculate the p-value
    `sd`, `mean`: the expected standard deviation, mean the number of runs,
                  given the ratio of numbers of 1's/0's in the sequence

>>> r = wald_wolfowitz('1000001')
>>> r['n_runs'] # should be 3, because 1, 0, 1
3

>>> r['p'] < 0.05 # not < 0.05 evidence to reject Ho of random sequence
False

# this should show significance for non-randomness
>>> li = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
>>> wald_wolfowitz(li)['p'] < 0.05
True