Home > Enterprise >  Creating shifted Hankel matrix
Creating shifted Hankel matrix

Time:10-22

Say I have some time-series data in the form of a simple array.

X1 = np.array[(1, 2, 3, 4]

The Hankel matrix can be obtained by using scipy.linalg.hankel, which would look something like this:

hankel(X1)

array([[1, 2, 3, 4],
       [2, 3, 4, 0],
       [3, 4, 0, 0],
       [4, 0, 0, 0]])
 

Now assume I had a larger array in the form of

X2 = np.array([1, 2, 3, 4, 5, 6, 7])

What I want to do is fill in the zeros in this matrix with the numbers that are next in the index (specific to each row). Taking the same Hankel matrix earlier by using the first four values in the array X2, I'd like to see the following output:

hankel(X2[:4])

array([[1, 2, 3, 4],
       [2, 3, 4, 5],
       [3, 4, 5, 6],
       [4, 5, 6, 7]])

How would I do this? I'd ideally like to use this for larger data. Appreciate any tips or pointers given. Thanks!

CodePudding user response:

If you have a matrix with the appropriate index values into your dataset, you can use integer array indexing directly into your dataset.

To create the index matrix, you can simply use the upper-left quadrant of a double-sized Hankel array. There are likely simpler ways to create the index matrix, but this does the trick.

>>> X = np.array([9, 8, 7, 6, 5, 4, 3])
>>> N = 4  # the size of the "window"
>>> indices = scipy.linalg.hankel(np.arange(N*2))[:N, :N]
>>> indices
array([[0, 1, 2, 3],
       [1, 2, 3, 4],
       [2, 3, 4, 5],
       [3, 4, 5, 6]])

>>> X[indices]
array([[9, 8, 7, 6],
       [8, 7, 6, 5],
       [7, 6, 5, 4],
       [6, 5, 4, 3]])
  • Related