TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Autocorrelation

Time Series
Medium

Autocorrelation measures how a time series correlates with a delayed (lagged) version of itself. It reveals repeating patterns, periodicity, and the degree to which past values predict future values. Autocorrelation at lag k tells you how similar the series is to itself shifted by k time steps.

Given a time series and a maximum lag, compute the autocorrelation for each lag from 0 to max_lag.

Algorithm

  1. Compute the mean and total variance of the series:
xˉ=1n∑t=0n−1x[t]\bar{x} = \frac{1}{n}\sum_{t=0}^{n-1}x[t]xˉ=n1​t=0∑n−1​x[t] γ0=∑t=0n−1(x[t]−xˉ)2\gamma_0 = \sum_{t=0}^{n-1}(x[t]-\bar{x})^2γ0​=t=0∑n−1​(x[t]−xˉ)2
  1. For each lag k, compute the autocovariance and normalize by the total variance:
rk=∑t=0n−k−1(x[t]−xˉ)(x[t+k]−xˉ)γ0r_k = \frac{\sum_{t=0}^{n-k-1}(x[t] - \bar{x})(x[t+k] - \bar{x})}{\gamma_0}rk​=γ0​∑t=0n−k−1​(x[t]−xˉ)(x[t+k]−xˉ)​

Note that r_0 = 1 always (a series perfectly correlates with itself at lag 0).

Return max_lag plus one values rounded to six decimals.

Loading visualization...

Examples

Input: series = [1, 2, 3, 4, 5], max_lag = 2

Output: [1.0, 0.4, -0.1]

Explanation: Normalizing each lagged covariance by lag-zero variance makes the first value 1.

Input: series = [1, -1, 1, -1, 1, -1], max_lag = 2

Output: [1.0, -0.833333, 0.666667]

Hint 1

Center the series once and reuse the lag-zero sum of squares as the denominator.

Hint 2

For each lag, multiply only pairs that remain within the series.

Requirements

  • Compute autocorrelation for each lag from 0 to max_lag inclusive
  • Subtract the mean from values before computing covariances
  • Normalize by the total variance (autocovariance at lag 0)
  • If variance is zero (constant series), return 1.0 for lag 0 and 0.0 for all other lags
  • Return a list of floats of length max_lag + 1

Constraints

  • series has at least 2 elements
  • 0 <= max_lag < len(series)
  • Return a list of floats of length max_lag + 1
  • Time limit: 300 ms
Try Similar Problems
Lag FeaturesDifferencingExponential Moving AverageSimple Moving AveragePercent Change

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Autocorrelation

Time Series
Medium

Autocorrelation measures how a time series correlates with a delayed (lagged) version of itself. It reveals repeating patterns, periodicity, and the degree to which past values predict future values. Autocorrelation at lag k tells you how similar the series is to itself shifted by k time steps.

Given a time series and a maximum lag, compute the autocorrelation for each lag from 0 to max_lag.

Algorithm

  1. Compute the mean and total variance of the series:
xˉ=1n∑t=0n−1x[t]\bar{x} = \frac{1}{n}\sum_{t=0}^{n-1}x[t]xˉ=n1​t=0∑n−1​x[t] γ0=∑t=0n−1(x[t]−xˉ)2\gamma_0 = \sum_{t=0}^{n-1}(x[t]-\bar{x})^2γ0​=t=0∑n−1​(x[t]−xˉ)2
  1. For each lag k, compute the autocovariance and normalize by the total variance:
rk=∑t=0n−k−1(x[t]−xˉ)(x[t+k]−xˉ)γ0r_k = \frac{\sum_{t=0}^{n-k-1}(x[t] - \bar{x})(x[t+k] - \bar{x})}{\gamma_0}rk​=γ0​∑t=0n−k−1​(x[t]−xˉ)(x[t+k]−xˉ)​

Note that r_0 = 1 always (a series perfectly correlates with itself at lag 0).

Return max_lag plus one values rounded to six decimals.

Loading visualization...

Examples

Input: series = [1, 2, 3, 4, 5], max_lag = 2

Output: [1.0, 0.4, -0.1]

Explanation: Normalizing each lagged covariance by lag-zero variance makes the first value 1.

Input: series = [1, -1, 1, -1, 1, -1], max_lag = 2

Output: [1.0, -0.833333, 0.666667]

Hint 1

Center the series once and reuse the lag-zero sum of squares as the denominator.

Hint 2

For each lag, multiply only pairs that remain within the series.

Requirements

  • Compute autocorrelation for each lag from 0 to max_lag inclusive
  • Subtract the mean from values before computing covariances
  • Normalize by the total variance (autocovariance at lag 0)
  • If variance is zero (constant series), return 1.0 for lag 0 and 0.0 for all other lags
  • Return a list of floats of length max_lag + 1

Constraints

  • series has at least 2 elements
  • 0 <= max_lag < len(series)
  • Return a list of floats of length max_lag + 1
  • Time limit: 300 ms
Try Similar Problems
Lag FeaturesDifferencingExponential Moving AverageSimple Moving AveragePercent Change

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.