# Overlapping vs non-overapping windows in random walk

Consider a simple random walk. I am trying to compute the variance of differences over a window of certain size dw which could, for example, model returns of a stock over a certain period. I compute and average this difference for 1) non-overlapping windows and 2) for sliding, overlapping windows.

Based on my (limited) statistics knowledge, I would have expected that the sample variance of overlapping windows would be larger, because there are many more correlated samples within overlapping windows and positively correlated samples tend to increase the variance. This is, however, not what I find. The Python code below shows that covariances (and thus variances) are essentially the same:

import numpy as np

#generate RW
dx = np.random.choice([-1,1],1000000)
x=np.cumsum(dx)

#window size
dw=10

#get non-overlapping samples
non_overlapping_samples = np.diff(x[::dw])
print("Non-overlapping variance: ", non_overlapping_samples.std())
print("Non-overlapping covariance: ", np.cov(non_overlapping_samples) )

#get overlapping samples
overlapping_samples=[]
for w0 in range(dw):
overlapping_samples.append(np.diff(x[w0:][::dw]))
overlapping_samples=np.array(overlapping_samples).flatten()
print("Overlapping variance: ", overlapping_samples.std())
print("Overlapping covariance: ", np.cov(overlapping_samples))

output:
Non-overlapping variance:  3.170077114073606
Non-overlapping covariance:  10.049489405072238
Overlapping variance:  3.165723940721805
Overlapping covariance:  10.021818090777733


I was wondering why this is the case? Shouldn't overlapping windows be much more correlated than non-overlapping ones?

At first I thought that on average, the number of positively and negatively correlated samples is the same and cancels out in the variance. However, even if I bias the RW (i.e. choose samples from {-1,2}), the overlapping and non-overlapping variances are the same.

• I cannot read what you have done for "overlapping", but a few observations: (A) your output "variance" of about $3.17$ is in fact a standard deviation; (B) the theoretical variance should be $10$ with $\sqrt{10}\approx 3.162$; (C) your "covariance" numbers look strange and at least in the non-overlapping case should presumably be close to $0$ Mar 20, 2023 at 10:40
• @Henry, thank you. I now suspect that numpy's cov function is not doing what I think it's doing. Mar 20, 2023 at 10:58

non_overlapping_samples and overlapping_samples are both 1-dimensional arrays, so numpy.cov just computes the sample variance.