Simple mean reversion measure for binary time series I am trying to define a simple measure for mean reversion in a stochastic sequence of ones and zeros, which I denote by $x_t$.
Yes, a unit root test on the cumulative sum could be a viable choice, but I started off by correlating the current value of the stochastic process with the deviation of the cumulative sum from its expected trend. 
In more detail: let $p_1$ be the probability of $x_t=1$ (and hence $P(x_t=0)=1-p_1$), $n$ the length of the vector $x$. 
I then define the above mentioned deviation as

y = cumsum(x) - (1:n)*p1

and my measure of mean reversion as the (negative) correlation between  $x_t$ and  $y_{t-1}$: 

mr = -cor(x[2:n], y[1:(n-1)])

Or as a function:

MeanRevert = function(x,p1){
  n=length(x)
  #y is the detrended random walk.
  y = cumsum(x) - (1:n)*p1
  mr = -cor(x[2:n], y[1:(n-1)])
  return(mr)
}

Now for a purely random walk, I expected an average zero value for that correlation but there appears to be a bias:

p1 = 0.12;N=100;
MRsample=vector() 
for (i in 1:500){
    x= sample(c(1,0), N, replace=TRUE, p = c(p1, 1-p1))
    MRsample = c(MRsample, MeanRevert(x, p1=p1))
  }
hist(MRsample, xlab ="mean reversion", main =paste("p=",p1, "N=",N))


What am I missing, what causes this bias ?
Thanks!
ML
 A: Let me recap your setup: Suppose we have $X,X_{1},\ldots,X_{n}$ i.i.d.
with $p=P(X=1)=1-P(X=0)$. So $E X=p$. 
Then define $Y_{n}=-np+\sum_{i=1}^{n}X_{i}$. So $E Y_{n}=0$. 
So your proposed measure is the correlation
between $X_{n}$ and $Y_{n-1}$. Since $X_{n}$ and $Y_{n-1}$ are
independent, this correlation is zero. 
Yet your sample correlations do not have mean $0$. 
I think the issue is that the pairs $(X_i,Y_{i-1})$, for i=2,..,n are not independent, because of the $Y_i$'s. This could be the source of the bias.  
**
My original response is below, but Markus points out that sample correlation is unbiased when the true correlation is zero:
Sample correlation is a biased estimate of correlation, so maybe this isn't surprising.  Take a look at the paper "Bias in Estimation and Hypothesis Testing of Correlation" inPsicológica (2003), 24, 133-158, by  Donald W. Zimmerman, Bruno D. Zumbo, and Richard H. Williams.(http://www.uv.es/revispsi/articulos1.03/9.ZUMBO.pdf)
How does it look for sample covariance, which is unbiased (and should be zero)?
