Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-028

Autocovariance Lag k

Problem

Compute the sample autocovariance at lag k using the biased 1/n denominator: gamma(k) = (1/n) * sum over t = 0, ..., n-k-1 of (x[t] - mean) * (x[t+k] - mean).

Return 0.0 if k is negative or k is at least n.

Hint

Always divide by the full length n, not by n - k; that is the biased estimator.

Test cases

case 1
in: [[1,2,3,4],0]
expected: 1.25
case 2
in: [[1,2,3,4],1]
expected: 0.3125
case 3
in: [[1,2,3,4],2]
expected: -0.375
case 4
in: [[1,2,3,4],4]
expected: 0
Starter codePython
def autocovariance(series, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor