0
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ 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$ $\endgroup$
    – Henry
    Mar 20, 2023 at 10:40
  • $\begingroup$ @Henry, thank you. I now suspect that numpy's cov function is not doing what I think it's doing. $\endgroup$
    – Botond
    Mar 20, 2023 at 10:58

1 Answer 1

1
$\begingroup$

Your intuition about overlapping and non-overlapping samples is correct, but that's not what your code is doing.

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

Try computing the autocorrelation instead.

$\endgroup$
1
  • $\begingroup$ Thanks for the answer. That would explain the covariance problem, but it still does not explain why the variances are so close to each other. I must have another bug too $\endgroup$
    – Botond
    Mar 20, 2023 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.