Skip to content
DeepForgeby svx
All problems
MediumProbabilitypr-220

Lag-1 Autocorrelation

Problem

Lag-1 autocorrelation of a sample sequence:

rho = sum((x_i - mean) * (x_{i+1} - mean)) / sum((x_i - mean)^2).

Return 0.0 for fewer than 2 samples or a constant sequence.

Hint

Use the same mean for both the numerator and denominator.

Test cases

case 1
in: [[1,2,3,4,5]]
expected: 0.4
case 2
in: [[1,1,1]]
expected: 0
case 3
in: [[1,2]]
expected: -0.5
case 4
in: [[5]]
expected: 0
case 5
in: [[1,3,1,3]]
expected: -0.75
Starter codePython
def autocorrelation_lag1(samples):
    # Your code here
    pass

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