Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

ML coding · Statistical inference

The autocorrelation function of a series

Easy · Target 10 minutes · Python and NumPy

Given a series x of length n and an integer max_lag with 0 ≤ max_lag < n, return the sample autocorrelations r0, …, rmax_lag, where

rk = Σt=0n−k−1 (xt − x̄)(xt+k − x̄) / Σt=0n−1 (xt − x̄)²

and x̄ is the mean of the whole series. The series is not constant.

Examples

Input x = [1, 2, 3, 4, 5], max_lag = 2

Output [1, 0.4, -0.1]

A trend: strong positive correlation at lag 1, weaker at lag 2. r₀ is always 1.

Input x = [1, -1, 1, -1, 1, -1], max_lag = 1

Output [1, -0.8333]

Alternating signs: r₁ is strongly negative.

Input x = [0.5, 0.1, -0.3, 0.2], max_lag = 0

Output [1]

Lag 0 only.

Constraints

  • 2 ≤ n ≤ 10⁵
  • 0 ≤ max_lag < n
  • x is not constant
  • Checked to a relative tolerance of 10⁻⁹

Hints

Hint 1

Subtract the mean once, from the whole series. Every lag uses the same deviations.

Hint 2

The numerator at lag k is a dot product of the deviations with themselves shifted by k.

Hint 3

d[:n − k] @ d[k:] for each k, divided by d @ d. For long series and many lags, an FFT computes all of them in O(n log n).

Where candidates go wrong

  • Using a separate mean for each of the two overlapping segments: that is a different estimator, closer to a Pearson correlation of the segments.
  • Dividing each lag by n − k instead of using the same denominator. It reduces the bias but can produce an autocorrelation sequence that is not a valid correlation structure.
  • Looping over t in Python for every lag: O(n · max_lag) interpreted steps instead of vectorised dot products.
Console⌘/Ctrl + Enter runs

Python runs in your browser with NumPy. The first run downloads the runtime, which takes a few seconds.


Why interviewers ask it

A warm-up in research interviews: it shows whether you know the standard estimator (one mean, one denominator) and can vectorise a lagged sum without a Python loop over time.

The theory behind it is in the lesson Statistical inference in code: standard errors, permutation tests, the Kalman filter.


Keep going