Segmentation, Warping and Clustering of Multivariate Time Series in PythonThe mvtsa toolbox

Chapter 3
Representations

Segmentation, warping and clustering rarely work on the raw samples alone. This chapter collects the representations the later chapters build on: piecewise approximations that compress a series into segments, symbolic words, the qualitative episodes of trend analysis, feature vectors and path signatures that summarize a whole series, networks and images that expose its structure, and matrix profiles that index its repeating subsequences. Each representation answers a different question about a series, and each has a role in a later method.

3.1 Piecewise approximations

A piecewise approximation replaces a series by c segments and one simple model per segment. The piecewise aggregate approximation (PAA) uses frames of equal length and their means [49, 50]; the adaptive piecewise constant approximation (APCA) lets the segment lengths vary [51]; the piecewise linear approximation (PLA) fits a line to every segment, which here is found by bottom-up segmentation with the linear cost of chapter 4; perceptually important points (PIP) keep the samples that deviate most from the line between their already selected neighbours [52, 53]. PLA is the representation of the MPLA, SPLA and MDPLA measures of section 8.4: every segment is described by its mean and by the angle of its slope [7].

p = rp.pla(x, n_segments=12) 
print("slope angles (degrees):", np.degrees(p.angles[:, 0]).round(0))
   

Figure 3.1

Figure 3.1. Piecewise linear approximation of a noisy two-tone signal with twelve segments found by bottom-up segmentation. Produced by examples/01_basics/plot_02_representations.py.

Open in ColabExample plot_02_representations.py in the gallery

3.2 Symbolic representations

Symbolic representations turn a series into a word over a small alphabet, which makes string algorithms, hashing and indexing applicable. SAX z-normalizes the series, computes its PAA, and maps every frame mean to a letter by breakpoints that cut the standard normal distribution into equiprobable intervals [54, 55]; the MINDIST between two words lower-bounds the Euclidean distance of the series. ABBA compresses the series into linear pieces with a tolerance and clusters the pieces by their length and increment, so that the alphabet adapts to the data [56]. SFA quantizes the leading Fourier coefficients instead of frame means [57]. sax, abba and sfa implement the three.

print("SAX word:", "".join(rp.sax(x, n_segments=16, alphabet_size=5)))
   

3.3 Qualitative episodes

Process operators describe trends qualitatively: the temperature rose faster and faster, then levelled off. Cheung and Stephanopoulos formalized such descriptions with seven triangular primitives defined by the signs of the first and second derivatives [58]: A (increasing, concave), B (decreasing, concave), C (decreasing, convex), D (increasing, convex), E (linear increase), F (linear decrease) and G (steady) (fig. 3.2). A signal is smoothed, its derivatives are estimated, and maximal runs of equal sign patterns become episodes, each with a duration and a magnitude. Balaskó, Bankó and Abonyi compared process trends by aligning such episode strings [9, 10]; section 8.5 shows the alignment. triangular_episodes extracts the episodes, episode_string writes them as letters (fig. 3.3).

Figure 3.2

Figure 3.2. The seven triangular primitives of qualitative trend analysis [58], defined by the signs of the first and second derivatives of the smoothed signal.
eps = rp.triangular_episodes(x, sigma=6) 
print("episodes:", rp.episode_string(eps))
   

Figure 3.3

Figure 3.3. Qualitative episodes of the signal of fig. 3.1: the smoothed signal, the episodes coloured by primitive, and their triangles. The smoothing width decides how many wiggles survive (parameter study of the script). Produced by examples/01_basics/plot_02_representations.py.

3.4 Features and path signatures

A feature vector summarizes a whole series: moments, autocorrelations, spectral and entropy measures, trend and crossing statistics. Feature-based methods are fast, robust to different lengths and interpretable; large feature libraries and their compact selections, such as hctsa and catch22, show how far this goes [59, 60]. extract_features computes seventeen features per variable, in the spirit of catch22 – moments, autocorrelation at the first lag and at its first zero crossing, the first minimum of the autocorrelation, permutation entropy [61], spectral centroid and entropy, trend, mean crossings, longest monotone run and others – and the pairwise correlations of the variables; feature_matrix stacks them for a collection.

The signature of a path is the sequence of its iterated integrals,

S(𝐗)i1…ik = ∫ 0<t1<⋯<tk<T dxt1i1 ⋯dxtkik , (3.1)

which characterizes the path up to reparametrization and is the natural feature map of multivariate streams in rough path theory [62, 63]. The first level holds the increments, the second level the signed areas between pairs of variables – a measure of which variable leads which. For piecewise linear paths the signature is computed exactly from Chen’s identity, segment by segment; signature_features returns the levels up to depth three, optionally with time as an extra channel and with the lead–lag transformation.

3.5 Networks and images

A series can be turned into a network whose structure reflects its dynamics. In the natural visibility graph two samples are linked if the straight line between them passes above all samples in between [64]; in the horizontal version the line must be horizontal [65]. The degree distribution of the horizontal visibility graph of an uncorrelated series is known exactly, so departures from it measure correlation and nonlinearity. Labelling the nodes with the zones of a Shewhart control chart gives the six-sigma visibility graph of section 11.4 [20]. The recurrence network links samples whose delay vectors are closer than 𝜀 [66]; its adjacency matrix is the recurrence plot [67]. The ordinal partition network has the ordinal patterns of the delay vectors as nodes and their transitions as edges [68, 69]. Gramian angular and Markov transition fields encode a series as an image [70]. Figure 3.4 shows a recurrence plot of a series that switches from a periodic to a random-walk regime.

Figure 3.4

Figure 3.4. Recurrence plot (10 % recurrence rate) of a series that is periodic in its first half and a random walk in its second half: diagonal lines mark the periodic regime, a blocky texture the drifting one. Produced by examples/09_visualization/plot_03_recurrence_images.py.

Open in ColabExample plot_03_recurrence_images.py in the gallery

3.6 Matrix profiles

The matrix profile of a series stores, for every subsequence of length w, the distance to its nearest neighbour elsewhere in the series and the index of that neighbour [26]; the STOMP algorithm computes it in O(T2) time by updating dot products along diagonals [71]. Low values mark motifs, high values discords, and the arcs between subsequences and their neighbours reveal the regime structure: few arcs cross a regime boundary. FLUSS turns this observation into a segmentation [27], and ClaSP uses the k-nearest-neighbour profile [28]; both are discussed in section 6.3. matrix_profile and knn_profile compute the z-normalized (shape) or plain Euclidean (level-sensitive) profiles; auto_window estimates the subsequence width from the autocorrelation.

In a nutshell: Representations

Piecewise approximations (PAA, APCA, PLA, PIP) compress a series into segments; symbolic words (SAX, ABBA, SFA) make string methods applicable; qualitative episodes describe trends in the language of operators; features and path signatures summarize whole series; visibility, recurrence and ordinal networks and image encodings expose dynamics; matrix profiles index repeating subsequences. PLA and episodes are the representations of the segment-level warping methods of chapter 8, features and signatures of feature-based clustering, matrix profiles of profile-based segmentation.