title: M(ultivariate) SSA (Singular Spectrum Analysis) Turning Hankel Matrix Back Into Time Series?
body: <pre><code>def get_time_series_from_hankel(self, hankel_matrix, current_channel_num):
      assert SSA.is_hankel_matrix(hankel_matrix)
      A, B = hankel_matrix.shape
      channel_result = []
      reversed_matrix = hankel_matrix[::-1]
      sub_matrix = reversed_matrix [:, (current_channel_num)*B//self.total_channel_num:(current_channel_num+1)*B//self.total_channel_num]
      iter_range = range(-A + 1, B//self.total_channel_num)
      for j in iter_range:
          channel_result.append(sub_matrix.diagonal(j).mean())
      return np.array(channel_result)
  </code></pre>
  <p>I'm using the above code to recreate the original time series from the hankel matrices produced by the SSA algorithm. My code works perfectly well in the univariate case and perfectly reproduces the original time series. For some reason, it only <em>half</em> works in the multivariate case. The two channels will perfectly match their respective original time series, <em>on opposite halves of the time frame</em>. This is demonstrated below with the good univariate case (the SSA output is shifted by one so it's visible, otherwise it would perfectly overlap the original) and with the BAD multivariate case (blue and orange are the originals, green and red are the respective reproductions). Observe that the red channel matches the orange original in the second half, and the green channel matches the blue original in the first half.</p>
  <p>I've narrowed down the problem to this function I wrote that recreates the time series from the Hankel matrix, and I've tried messing around with it as much as I can, but I just don't know where the bug is coming from. I'm sure I'm missing a very, very obvious point about linear algebra somewhere, but I'm not sure where.
  <a href="https://i.stack.imgur.com/8PQbF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8PQbF.png" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/aSAME.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aSAME.png" alt="enter image description here" /></a></p>