© Igor Kleiner  |  igkleriner@gmail.com
Interactive Learning Lab

How to Compute SACVF Step by Step

A visual guide to calculating the Sample AutoCovariance Function (SACVF) and Sample AutoCorrelation Function (SACF).

1. Sample AutoCovariance (SACVF)

To measure how a sequence relates to a shifted copy of itself, we use the SACVF, denoted by $\hat{\gamma}(h)$.

$$ \hat{\gamma}(h) = \frac{1}{n} \sum_{t=1}^{n-h} (x_t - \bar{x})(x_{t+h} - \bar{x}) $$

2. Sample AutoCorrelation (SACF)

SACVF depends on the scale of the data. To get a standardized measure between $-1$ and $1$, we divide the SACVF by the variance (which is the SACVF at lag $h=0$). This gives us the SACF, denoted by $\hat{\rho}(h)$.

$$ \hat{\rho}(h) = \frac{\hat{\gamma}(h)}{\hat{\gamma}(0)} $$

Choose an Example Sequence

1. Raw Sequence (\(x_t\))

2. Find Mean (\(\bar{x}\))

4

\(n = 6\)

3. Centered (\(c_t = x_t - \bar{x}\))

Interactive SACVF Sandbox

Adjust the lag $h$ to see how the sequence shifts and which pairs overlap.

0123
Centered $x_t$
Shifted $x_{t+h}$
Product
Ignored (No overlap)

SACF Plot (Sample AutoCorrelation)

Notice that autocorrelation is symmetric around zero: $\hat{\rho}(-h) = \hat{\rho}(h)$.

Step-by-Step Walkthrough

Review the mechanics for each lag applied to your chosen sequence.

0

Lag $h=0$ (Variance)

  • ● No shift is applied.
  • ● All $n$ pairs overlap perfectly. Every centered value is multiplied by itself (squared).
  • ● Note that SACVF at lag 0 is identically the sample variance (using the $1/n$ convention).
1

Lag $h=1$

  • ● Shift the centered sequence to the right by 1 position.
  • ● Only $n-1$ pairs overlap now. The non-overlapping cells are crossed out and ignored.
  • ● Multiply each aligned vertical pair and sum them.
2

Lag $h=2$

  • ● Shift the sequence by 2 positions.
  • ● Only $n-2$ pairs overlap.
3

Lag $h=3$

  • ● Shift by 3 positions.
  • ● Only $n-3$ pairs overlap.
  • ● As $h$ increases, fewer data points contribute, making estimates at large lags less reliable.

Summary of Results

Lag ($h$) Overlapping Pairs Sum of Products SACVF $\hat{\gamma}(h)$ SACF $\hat{\rho}(h)$

Student Assignment: Calculate it yourself!

Enter any 5 numbers. The platform will calculate the mean, centered values, SACVF, and SACF for all possible lags.

How to compute this in Python

You can easily compute this in professional applications using the ready-made functions from Python's statsmodels library:

import numpy as np
import statsmodels.api as sm

# Test with our 'Random' sequence
x = np.array([2, 4, 3, 5, 6, 4])

# Ready-made function for Sample AutoCovariance (SACVF)
# Note: nlag=3 (or nlags=3) specifies the maximum lag to compute (h=0, 1, 2, 3).
# We set it to 3 to match our visual lab and to avoid wasting computation
# time on unnecessary higher lags, which is crucial for huge datasets.
sacvf = sm.tsa.acovf(x, nlag=3)

# Ready-made function for Sample AutoCorrelation (SACF)
sacf = sm.tsa.acf(x, nlags=3)

print("SACVF:", np.round(sacvf, 3))
print("SACF: ", np.round(sacf, 3))