0

My aim is to transform a one-dimensional time-series into a two-dimensional phase space. Since the time-series is one-dimensional, the phase space will be a pseudo (lag) phase space.

One theoretical approach to transform a time-series into pseudo phase space is as follows:

  1. The original list of data is the full-length time-series x(t).
  2. A subseries of data is the "lagged" version of the original time-series, starting with the second value of the time-series (instead of with its first one, as the original time-series) x(t+1).

Consequently, the subseries will always have one value less in its list. For a 3D phase space, a second subseries would have two values less in its list. This is where my code related problem comes in, since matplotlib does not allow me to plot a two-dimensional plane when the length of two lists is not equal.

Here is my current code:

import numpy as np
import matplotlib.pyplot as plt

# Example time-series
Data = [924, -5, 24, 1, 0, 242, -5, 42, 5, 1, -9, 50, 3, 432, 0, -5, 4, 1, 2, 3]

# Embedding (time-series to phase space)
x_list = Data[:-1]
y_list = Data[1:]

# Plot
plt.plot(x_list, y_list, c="blue", linewidth=0.5)
plt.show()

This code uses the whole length of the time-series except the last value in the list by x_list = Data[:-1] for the x-axis. For the y-axis, the code uses the whole time-series except the very first item in the list by Data[1:].

While this code works, its result is not a real embedding of a time-series into its two-dimensional phase space, since x_list = Data[:-1] does not include the last value of the time-series.

What would be a proper way for coding and plotting the phase space of subseries that increasingly diminish in length compared to the original time-series data?

Philipp
  • 335
  • 2
  • 4
  • 12

2 Answers2

2

A simple approach is to use and it's shift method:

Data = [924, -5, 24, 1, 0, 242, -5, 42, 5, 1, -9, 50, 3, 432, 0, -5, 4, 1, 2, 3]

import pandas as pd
import matplotlib.pyplot as plt

timeseries = pd.Series(Data)

plt.plot(timeseries, timeseries.shift(), c='blue', linewidth=0.5)

For a lag of 2 use shift(2)

output:

shifted Series

NB. you can also shift with numpy, but it is less elegant IMO

autocorrelation

I am not sure what is your end goal, but in case you try to determine if you have a period, or to perform autocorrelation analysis you can use pandas.plotting.autocorrelation_plot:

pd.plotting.autocorrelation_plot(timeseries)

output:

autocorrelation

mozway
  • 194,879
  • 13
  • 39
  • 75
  • is there a difference between `x = Data; y = x[1:]+[x[0]]` and padas .shift() ? Seems to me that is a big import to get rid of 2 lines of code? – Patrick Artner May 01 '22 at 08:49
  • @PatrickArtner the generalization, it seems that OP wants to apply this to potentially many dimensions. Also I suspect a broader end goal (maybe autocorrelation analysis?), thus the second part of my answer. Otherwise, I don't know why OP asks the question… – mozway May 01 '22 at 08:51
  • Currently, my aim is only the correct embedding of a time-series (e.g., a list of datapoints) into phase space in Python. Further analyses steps surely come later, though. – Philipp May 01 '22 at 08:55
1

For a wrap around solution you could use a list comp to shift the data:

Data = list(range(10))

d = 5
multi = [Data[dims:]+Data[:dims] for dims in range(d) ]

print(*multi, sep="\n")

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
[3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
[4, 5, 6, 7, 8, 9, 0, 1, 2, 3]

if you do not want to wrap around, fix it like so:

d = 5
multi = [(Data[dims:]+Data[:dims])[:-d+1] for dims in range(d)]

to get

[0, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 6, 7]
[3, 4, 5, 6, 7, 8]
[4, 5, 6, 7, 8, 9]

If you want a hypothetical last value you would have to do some extrapolation of the series you got - if that makes more sense then cutting it short ... dunno.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Hello Patrick. As I understand your code, it starts each subseries using x(t+n). This shift is correct. However, the subseries are not decreasing in length; instead, the length of each subseries is equal to the length of the original time-series (list). Adding new numeric values to each additional list has to be avoided. Extrapolation is therefore not needed. – Philipp May 01 '22 at 09:10