Skip to content
All library documents

Constructing Exponential and Geometric Autocorrelation Matrices

Article Quant Q&A · Author: Lydia

Summary

The document asks how to build two structured matrices whose entries decay with the distance between time indices: one uses exponential decay controlled by a positive scale parameter, and the other uses powers of a coefficient whose magnitude is below one. A concise NumPy approach creates arrays of time indices, forms all pairwise distances with a mesh grid, and applies the selected decay function elementwise.

Additional answers show two ways to calculate an autocorrelation sequence from a one-dimensional series: a convolution-style correlation and lagged sample correlations. The examples illustrate array-based computation and advise against Python’s specialized matrix class. The discussion does not compare the estimators’ statistical properties or establish that the resulting matrices are appropriate for any particular return process. In practice, the convolution result is not normalized as a correlation, while the lagged correlation approach has its own finite-sample and implementation choices.

Key ideas

  • Pairwise time-index distances can be used to build exponential-decay or geometric-decay matrices.
  • NumPy mesh grids provide a vectorized way to evaluate a function over all time-index pairs.
  • Autocorrelation sequences can be estimated through convolution or lagged sample correlations.
  • The convolution example returns unnormalized correlation values, so it should not be confused with normalized autocorrelation.

Tags

Full text
# Build autocorrelation matrix in Python


# Build autocorrelation matrix in Python












I am interested to build the following matrices in Python $B_{tt'} = e^{−|t − t'|/\tau }$ and $B_{tt'} = b^{|t−t'|}$, where $t,t' = 1, 2, \ldots , T$ for some fixed $T$, $|b| < 1$, and $\tau > 0$. Could someone please demonstrate to me a Pythonic way to build these matrices? Thanks!

## Answer by RRG (score 2)

https://quant.stackexchange.com/a/53676

```
import numpy as np

t = np.linspace(1,T,T)

t1,t2 = np.meshgrid(t,t)

Btt = np.exp(-np.abs(t1-t2)/tau) #Btt = b**np.abs(t1-t2)
```

## Answer by amdopt (score 1)

https://quant.stackexchange.com/a/53658

Below will do the trick without using `np.matrix`. The matrix class isn't used much. See docs here. They recommend avoiding it and using arrays instead.

Below are 2 separate approaches. The first function is convolutional, the second is statistical (normalized on [-1,1] interval). The input `x` should be a 1-d array. Both functions return a 1-d array.

```
import numpy as np 

def autocorr_conv(x):
    result = np.correlate(x, x, mode='full')
    return result[result.size // 2:]

def autocorr_stat(x):
    return np.array([1]+[np.corrcoef(x[:-i], x[i:])[0,1] for i in range(1, len(x)-1])
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.