ACOVF

This function computes the autocovariance function (ACOVF), which describes covariance between a series and lagged versions of itself.

For lag k, autocovariance is defined as:

\gamma_k = \operatorname{Cov}(x_t, x_{t-k})

The output is useful for understanding serial dependence magnitude before normalization into autocorrelation.

Excel Usage

=ACOVF(x, adjusted, demean, fft, missing, nlag)
  • x (list[list], required): Time-series observations as a 2D range.
  • adjusted (bool, optional, default: false): Use denominator n-k instead of n in covariance estimation.
  • demean (bool, optional, default: true): Subtract sample mean before covariance estimation.
  • fft (bool, optional, default: true): Use FFT-based convolution.
  • missing (str, optional, default: “none”): Missing-data handling mode.
  • nlag (int, optional, default: null): Maximum lag to return; null returns full available range.

Returns (list[list]): 2D table with columns lag and acovf.

Example 1: Autocovariance with default settings

Inputs:

x adjusted demean fft missing nlag
1 2 3 4 5 6 false true true none

Excel formula:

=ACOVF({1,2,3,4,5,6}, FALSE, TRUE, TRUE, "none", )

Expected output:

Result
0 2.91667
1 1.45833
2 0.166667
3 -0.791667
4 -1.25
5 -1.04167
Example 2: Autocovariance with adjusted denominator

Inputs:

x adjusted demean fft missing nlag
3 5 4 6 5 7 6 true true true none 4

Excel formula:

=ACOVF({3,5,4,6,5,7,6}, TRUE, TRUE, TRUE, "none", 4)

Expected output:

Result
0 1.55102
1 0.115646
2 0.791837
3 -0.80102
4 -0.312925
Example 3: Autocovariance without demeaning

Inputs:

x adjusted demean fft missing nlag
2 2 3 3 4 4 5 false false false none 3

Excel formula:

=ACOVF({2,2,3,3,4,4,5}, FALSE, FALSE, FALSE, "none", 3)

Expected output:

Result
0 11.8571
1 9.57143
2 8
3 5.85714
Example 4: Autocovariance limited to selected lags

Inputs:

x adjusted demean fft missing nlag
1 4 2 5 3 6 4 7 false true true none 2

Excel formula:

=ACOVF({1,4,2,5,3,6,4,7}, FALSE, TRUE, TRUE, "none", 2)

Expected output:

Result
0 3.5
1 -0.625
2 2

Python Code

import numpy as np
from statsmodels.tsa.stattools import acovf as sm_acovf

def acovf(x, adjusted=False, demean=True, fft=True, missing='none', nlag=None):
    """
    Estimate autocovariance values of a time series across lags.

    See: https://www.statsmodels.org/stable/generated/statsmodels.tsa.stattools.acovf.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        x (list[list]): Time-series observations as a 2D range.
        adjusted (bool, optional): Use denominator n-k instead of n in covariance estimation. Default is False.
        demean (bool, optional): Subtract sample mean before covariance estimation. Default is True.
        fft (bool, optional): Use FFT-based convolution. Default is True.
        missing (str, optional): Missing-data handling mode. Valid options: None, Raise, Conservative, Drop. Default is 'none'.
        nlag (int, optional): Maximum lag to return; null returns full available range. Default is None.

    Returns:
        list[list]: 2D table with columns lag and acovf.
    """
    try:
        def to1d(values):
            if isinstance(values, list):
                if all(isinstance(row, list) for row in values):
                    raw = [item for row in values for item in row]
                else:
                    raw = values
            else:
                raw = [values]

            out = []
            for item in raw:
                try:
                    out.append(float(item))
                except (TypeError, ValueError):
                    continue
            return out

        if missing not in ("none", "raise", "conservative", "drop"):
            return "Error: missing must be one of 'none', 'raise', 'conservative', or 'drop'"

        series = to1d(x)
        if len(series) < 2:
            return "Error: x must contain at least two numeric values"

        acovf_vals = sm_acovf(
            np.asarray(series, dtype=float),
            adjusted=adjusted,
            demean=demean,
            fft=fft,
            missing=missing,
            nlag=nlag,
        )

        arr = np.asarray(acovf_vals, dtype=float)
        return [[lag, float(arr[lag])] for lag in range(len(arr))]
    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

Time-series observations as a 2D range.
Use denominator n-k instead of n in covariance estimation.
Subtract sample mean before covariance estimation.
Use FFT-based convolution.
Missing-data handling mode.
Maximum lag to return; null returns full available range.