Null hypothesis of the Wald-Wolfowitz runs test

From Wikipedia

The runs test (also called Wald–Wolfowitz test after Abraham Wald and Jacob Wolfowitz) is a non-parametric statistical test that checks a randomness hypothesis for a two-valued data sequence. More precisely, it can be used to test the hypothesis that the elements of the sequence are mutually independent.

1. I was wondering if its null hypothesis is that the elements of the sequence are iid? (note that the quote only mentions mutual independence.)
2. What if the sequence is three-valued?

Thanks and regards!

• The part you've quoted only mentions mutual independence; but ten lines down:- "These parameters do not assume that the positive and negative elements have equal probabilities of occurring, but only assume that the elements are independent and identically distributed." – Scortchi Jul 16 '13 at 14:16
• A trinomial runs test sounds interesting. The same statistic (number of runs from a sequence of length $n$) could be used but I don't know its distribution. Why don't you try to derive it (following the Wald-Wolfowitz derivation) or simulate it for a few different nulls & sample sizes & post an answer? – Scortchi Jul 18 '13 at 9:27

For 3 or more sequences, it's called the k-Category Extension of the Runs test. You can find a derivation here: https://ncss-wpengine.netdna-ssl.com/wp-content/themes/ncss/pdf/Procedures/NCSS/Analysis_of_Runs.pdf

I wrote an R function to calculate the p-value as follows:

ww_test <- function(x) {
x <- factor(x)
n <- length(x)
njs <- table(x)
njs <- structure(.Data=as.vector(njs), .Names=names(njs))
mu <- (n*(n+1) - sum(njs^2))/n
sdev_l <- n^2*(n-1)
sdev_u <- sum(njs^2*(sum(njs^2)+n*(n+1))) - 2*n*sum(njs^3)-n^3
sdev <- sqrt(sdev_u/sdev_l)
r <- sum(diff(as.numeric(x)) != 0) + 1
z <- (r - mu) / sdev
pvalue <- 2*pnorm(-abs(z))
return(pvalue)
}