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

Chapter 11
Visualization and explanation

A change point is useful only if the analyst understands what changed; a regime only if the operator can recognize it. This chapter collects the tools that make results readable: a one-page segmentation report, the contributions of the variables to every change, tables of segment statistics, decision-tree rules that describe the states, regime transition networks, zone-labelled visibility graphs that connect time series to statistical process control, and views of alignments and collections.

11.1 A segmentation report

segmentation_dashboard puts on one page what an analyst wants to see about a segmentation: the segmented series with the true change points if known, the timeline of the states, the cost curve of the merge hierarchy with the relative reduction rate (or the segment lengths), and the contributions of the variables to every change (fig. 11.1). The other views of the package – plot_segmentation with a piecewise constant or linear fit, plot_fuzzy_segmentation with the memberships, plot_segmentogram, plot_pca_statistics, plot_segment_loadings, plot_run_length, plot_profile, plot_boundary_agreement – appear throughout the book.

st = sg.StateSegmenter( 
    segmenter=sg.BottomUp("normal", n_segments=6, initial_size=15), n_states=3 
) 
seg = st.segment(X) 
tree = sg.BottomUp("normal", initial_size=15).full_tree(X) 
mv.viz.segmentation_dashboard(X, seg, names=names, truth=cps, tree=tree)
   

Figure11.1

Figure 11.1. A one-page segmentation report of a process with three recurring regimes: segmented series and true change points (top), state timeline, cost curve with the relative reduction rate, and the contributions of the four variables to every change. Produced by examples/09_visualization/plot_01_dashboard.py.

Open in ColabExample plot_01_dashboard.py in the gallery

11.2 What changed?

Contribution plots of multivariate statistical process control attribute an alarm to variables [23]; boundary_contributions does the same for every change point of a segmentation. For each boundary it compares the two adjacent segments (or windows around the boundary) by the standardized shift of every variable’s mean, |x¯right −x¯left|∕s, the change of its variance, |log⁡(sright2∕sleft2)|, and the change of its correlations with the others (the row norm of the difference of the correlation matrices); kind="all" normalizes the three to unit sum and adds them. describe_segments tabulates the statistics of every segment – start, stop, means, standard deviations, slopes and the explained variance of the local PCA model – so that segments can be compared side by side.

print(boundary_contributions(X, seg, kind="all", names=names).round(2)) 
print(describe_segments(X, seg, names=names).round(2).iloc[:, :8])
   

11.3 Rules for states and transition networks

Varga, Szeifert, Réti and Abonyi described the operating regimes of an industrial reactor by decision trees: a shallow tree trained to predict the regime from the process variables turns hidden knowledge into a readable rule base [17]. explain_states fits such a tree to the state labels of a segmentation and returns the rules, their training accuracy and the importances of the variables; two levels are usually enough to describe a handful of regimes in terms an operator can check (section 13.3). transition_graph builds the regime transition network: states are nodes with their number of visits and total and mean durations, observed transitions are edges with their counts and probabilities (plot_transition_graph).

11.4 Six-sigma visibility graphs

Visibility graphs turn a series into a network (section 3.5), but the meaning of their network measures is often unclear. Ferenczi, Kummer and Abonyi connect them to statistical process control [20]: the nodes of the horizontal visibility graph are labelled with the zones of a Shewhart chart – C within one standard deviation of the centre line, B within two, A within three, and out-of-control beyond – and with the violations of the Western Electric rules [143] (one point beyond three sigma; two of three beyond two sigma on one side; four of five beyond one sigma on one side; eight in a row on one side). The edges carry the zone pairs they connect, so the zone-transition statistics of the graph summarize the behaviour of the process in SPC terms (six_sigma_visibility_graph, western_electric_rules, spc_zones). In fig. 11.2 the global features of the graphs of an in-control and a drifting process are nearly identical – the mean degree of a horizontal visibility graph of an uncorrelated series is close to four whatever its distribution [65] – while the zone labels, the edge statistics and the rule flags reveal the drift at once.

G_ok = rp.six_sigma_visibility_graph(in_control, center=0, sigma=1) 
G_drift = rp.six_sigma_visibility_graph(drifting, center=0, sigma=1) 
rows = [] 
for name, G in [("in control", G_ok), ("drifting", G_drift)]: 
    f = rp.graph_features(G) 
    rules = { 
        k: sum(G.nodes[i][k] for i in G.nodes) 
        for k in ("rule1", "rule2", "rule3", "rule4") 
    } 
    rows.append( 
        { 
            "process": name, 
            **{ 
                k: f[k] 
                for k in ("mean_degree", "degree_entropy", "clustering") 
            }, 
            **rules, 
        } 
    ) 
print(pd.DataFrame(rows).round(3).to_string(index=False))
   

Figure11.2

Figure 11.2. Six-sigma visibility graphs of an in-control (top) and a drifting process (bottom): samples coloured by their control-chart zone, visibility arcs above them, zones shaded. Produced by examples/09_visualization/plot_02_visibility_graphs.py.

Open in ColabExample plot_02_visibility_graphs.py in the gallery

11.5 Alignments and collections

For warping, the views of the package show an alignment as connected samples (plot_alignment), the cost matrix with the path and the window (plot_cost_matrix), the expected alignment of soft-DTW (plot_alignment_uncertainty), the warping functions of several pairs (plot_warping_function), segment-level alignments (plot_segment_alignment) and averages (plot_average). For collections, they show distance matrices ordered by cluster, Sammon, MDS and t-SNE maps, dendrograms and cluster prototypes (fig. 11.3).

a, b = G[0], G[int(np.flatnonzero(y == y[0])[1])] 
res = wp.dtw_path(a, b, window=0.1) 
_, E = wp.soft_dtw_alignment(a, b, gamma=0.05)
   

Figure11.3

Figure 11.3. Two GunPoint series of the same class [144]: the DTW alignment with a 10 % window (left) and the expected alignment of soft-DTW with γ = 0.05 (right). Produced by examples/09_visualization/plot_04_warping_maps.py.

Open in ColabExample plot_04_warping_maps.py in the gallery

In a nutshell: Visualization and explanation

A one-page report shows a segmentation, its states, its cost curve and the contributions of the variables to every change. Contributions split each change into shifts of means, variances and correlations; segment tables, decision-tree rules and transition networks describe segments and regimes in operators’ terms. Six-sigma visibility graphs label the nodes of a visibility graph with control-chart zones and Western Electric rules. Alignment, cost-matrix, uncertainty and map views make warping and clustering transparent. In mvtsa: segmentation_dashboard, boundary_contributions, describe_segments, explain_states, transition_graph, six_sigma_visibility_graph, and the plot_* functions of mvtsa.viz.