Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-066

ACF Vector

Problem

Return the sample autocorrelation vector [rho(0), rho(1), ..., rho(h)] using the biased 1/n estimator: gamma(k) = (1/n) * sum over t < n-k of (x[t] - mean) * (x[t+k] - mean), and rho(k) = gamma(k) / gamma(0).

Lags with k >= n or a zero-variance series produce 0.0; return an empty list for h < 0 or an empty series.

Hint

The first element is always 1.0 for a non-constant series.

Test cases

case 1
in: [[1,2,3,4],2]
expected: [1,0.25,-0.3]
case 2
in: [[1,2,3,4],0]
expected: [1]
case 3
in: [[5,5,5],1]
expected: [0,0]
case 4
in: [[1,2,3,4],4]
expected: [1,0.25,-0.3,-0.45,0]
Starter codePython
def acf_vector(series, h):
    # Your code here
    pass

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