Update Log
2021-12-17: Initial Upload

The Gramian Angular Field is a tool which can convert 1-D time series data into 2-D images for further analysis.

Mapping from Catesian Coordinates to Polar Coordinates

The first step of this algorithm is to rescale the data in Catesian coordinates to [0, 1] or [-1, 1] by one of the following methods:

x~1i=(ximax(X)+(ximin(X))max(X)min(X)\tilde{x}_{-1}^{i}=\frac{\left(x_{i}-\max (X)+\left(x_{i}-\min (X)\right)\right.}{\max (X)-\min (X)}

or

x~0i=ximin(X)max(X)min(X)\tilde{x}_{0}^{i}=\frac{x_{i}-\min (X)}{\max (X)-\min (X)}

Then the timestamps of time series are divided by NN depending on how many points in time are there in the time series, and it determines the size of the converted Gram matrix. After the division, the intermediate results are mapped to rr in the polar coordinate, while the cosine value are mapped to ϕ\phi. That’s why we need to have them rescaled before the mapping to make sure that they fall within the range of cosine function. [0, 1] corresponds to the cosine function in [0, π/2], while cosine values in the interval [−1, 1] fall into the angular bounds [0, π]. See the definition below.

{ϕi=arccosxiri=1N\left\{ \begin{aligned} \phi_{i}&=&\arccos x_{i} \\ r_{i}&=&\frac{1}{N} \end{aligned} \right.

Generating Gram Matrix

Gram Matrix is a useful tool in linear algebra. It is defined as a matrix generated by dot product of a group of n-dimension vectors.

G=(u1,v1u1,v2u1,vnu2,v1u2,v2u2,vnun,v1un,v2un,vn)G=\left(\begin{array}{cccc} \left\langle u_{1}, v_{1}\right\rangle & \left\langle u_{1}, v_{2}\right\rangle & \cdots & \left\langle u_{1}, v_{n}\right\rangle \\ \left\langle u_{2}, v_{1}\right\rangle & \left\langle u_{2}, v_{2}\right\rangle & \cdots & \left\langle u_{2}, v_{n}\right\rangle \\ \vdots & \vdots & \ddots & \vdots \\ \left\langle u_{n}, v_{1}\right\rangle & \left\langle u_{n}, v_{2}\right\rangle & \cdots & \left\langle u_{n}, v_{n}\right\rangle \end{array}\right)

They symbol uu and vv are vectors, and u,v\langle u, v\rangle indicates the dot product of vector uu and vv. The authors defined two specific dot product operations for data in polar coordinate, to better exploit the information stored in angles:

\begin{align} u \oplus v=\cos \left(\theta_{u}+\theta_{v}\right)\\ u \oplus v=\sin \left(\theta_{u}-\theta_{v}\right) \end{align}

where θ\theta indicates the angle of vector uu and vv in the polar coordinate, and the subscripts corresponds to the rir_{i}. Under this condition, the Gram Matrix of the rescaled 1-D time series data becomes

\begin{equation} G=\left(\begin{array}{cccc} \cos \left(\phi_{1}+\phi_{1}\right) & \cos \left(\phi_{1}+\phi_{2}\right) & \cdots & \cos \left(\phi_{1}+\phi_{n}\right) \\ \cos \left(\phi_{2}+\phi_{1}\right) & \cos \left(\phi_{2}+\phi_{2}\right) & \cdots & \cos \left(\phi_{2}+\phi_{n}\right) \\ \vdots & \vdots & \ddots & \vdots \\ \cos \left(\phi_{n}+\phi_{1}\right) & \cos \left(\phi_{n}+\phi_{2}\right) & \cdots & \cos \left(\phi_{n}+\phi_{n}\right) \end{array}\right) \end{equation}

or

\begin{equation} G=\left(\begin{array}{cccc} \sin \left(\phi_{1}-\phi_{1}\right) & \sin \left(\phi_{1}-\phi_{2}\right) & \cdots & \sin \left(\phi_{1}-\phi_{n}\right) \\ \sin \left(\phi_{2}-\phi_{1}\right) & \sin \left(\phi_{2}-\phi_{2}\right) & \cdots & \sin \left(\phi_{2}-\phi_{n}\right) \\ \vdots & \vdots & \ddots & \vdots \\ \sin \left(\phi_{n}-\phi_{1}\right) & \sin \left(\phi_{n}-\phi_{2}\right) & \cdots & \sin \left(\phi_{n}-\phi_{n}\right) \end{array}\right) \end{equation}

depending on which operation is taken to form the matrix. Equation (3) is called Gramian Angular Summation Fields (GASF), and Gramian Angular Differnce Fields (GADF) for equation (4). Note that unlike a standard dot production operation the customized one is not linear, nor positive definite, so the authors used a different symbol \oplus here.

Thie presentation of time series has a inherent advantage which is the ability to keep the absolute temporal relations, and time increases as the position moves from top-left to bottom-right. You may notice that elements lie on the main diagonal are in the temporal order, and every other element indicate the relation of any two points in time.

Final Image

Finally, the color coded images are generated by using normalisation and a colormap, see[1].

Sparse Representation

Let’s check out the density of Gramian Angular Field.

paAsj8A

From the above figure, we can see that Gramian Angular Field is quite sparse. To explain this feature, let’s check the formula of uvu \oplus v back in Catesian coordinate.

cos(θ1+θ2)=cos(arccos(u)+arccos(v))=cos(arccos(u))cos(arccos(v))sin(arccos(u))sin(arccos(v))=uv+1u21v2=u,v1u21v2\begin{aligned} \cos(\theta_1 + \theta_2) & = \cos(\arccos(u) + \arccos(v))\\ & = \cos(\arccos(u)) \cdot \cos(\arccos(v)) - \sin (\arccos(u)) \cdot \sin(\arccos(v))\\ & = u \cdot v + \sqrt{1-u^2} \cdot \sqrt{1-v^2}\\ & = \langle u, v \rangle - \sqrt{1-u^2} \cdot \sqrt{1-v^2} \end{aligned}

It can be seen from the above the equation that the newly defined dot production operation is actually a standard dot production plus a penalty item, which pushes the result towards 1-1. For uu and vv closer to 1, the penalty increases, because these samples are more likely to be gaussian noise.

Others

  • Limitation: if the length of time series data is nn, the size of generated matrix will be n×nn \times n. The authors recommend to use Piecewise Aggregation Approximation to smooth the input data and limit the size.
  • Library: a third-party library ‘pyts’[2] has a built-in function to do the conversion with GAF.

References


  1. Matplotlib http://matplotlib.org/3.5.0/api/_as_gen/matplotlib.axes.Axes.imshow.html#matplotlib.axes.Axes.imshow ↩︎

  2. pyts https://pyts.readthedocs.io/en/stable/ ↩︎