pythonnumpymatrixscipytoeplitz

Generate specific Toeplitz covariance matrix


I want to generate a statistical sample from a multidimensional normal distribution. For this I need to generate one specific kind of covariance matrix:

1    0.99 0.98 0.97 ...
0.99 1    0.99 0.98 ...
0.98 0.99 1    0.99 ...
0.97 0.98 0.99 1    ...
...  ...  ...  ...

Is there a way to generate this kind of matrix for multiple dimensions easily, without writing it by hand? (I need to have matrices with 50-100 dimensions, so doing it by hand is very tedious.)


Solution

  • You can use the scipy.linalg.toeplitz function, as it is made for exactly this kind of matrix:

    >>> import numpy as np
    >>> from scipy import linalg
    >>> linalg.toeplitz(np.arange(1,0,-0.1), np.arange(1,0,-0.1))
    array([[1. , 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1],
           [0.9, 1. , 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2],
           [0.8, 0.9, 1. , 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3],
           [0.7, 0.8, 0.9, 1. , 0.9, 0.8, 0.7, 0.6, 0.5, 0.4],
           [0.6, 0.7, 0.8, 0.9, 1. , 0.9, 0.8, 0.7, 0.6, 0.5],
           [0.5, 0.6, 0.7, 0.8, 0.9, 1. , 0.9, 0.8, 0.7, 0.6],
           [0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 0.9, 0.8, 0.7],
           [0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 0.9, 0.8],
           [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 0.9],
           [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]])