Note
Go to the end to download the full example code or to run this example in your browser via Binder.
When the information is in the correlation structure#
The classes of this collection share the same pool of correlation structures and differ only in their order; the means and variances are the same everywhere and the noise is strong. Sample-level DTW sees mostly noise, while CBDTW compares the latent structures directly.
import matplotlib.pyplot as plt
import pandas as pd
from mvtsa import pairwise_distances, viz
from mvtsa import warping as wp
from mvtsa.datasets import make_correlation_sequences
from mvtsa.evaluation import (
knn_accuracy,
mean_average_precision,
precision_recall_curve_knn,
)
C, y = make_correlation_sequences(n_classes=4, n_per_class=10, random_state=0)
viz.plot_series(C[0][:, :4], names=["x1", "x2", "x3", "x4"])

<Figure size 700x500 with 4 Axes>
CBDTW against sample-level measures#
measures = {
"CBDTW": wp.CBDTW(n_segments=8, n_components=2).fit(C),
"CBDTW, Grassmann": wp.CBDTW(
n_segments=8, n_components=2, local="grassmann"
).fit(C),
"DTW": "dtw",
"DTW_I": "dtw_i",
}
rows, curves = [], {}
for name, m in measures.items():
D = pairwise_distances(C, metric=m)
curves[name] = precision_recall_curve_knn(D, y, r_items=9)
rows.append(
{
"measure": name,
"1-NN": knn_accuracy(D, y),
"mAP": mean_average_precision(D, y),
}
)
print(pd.DataFrame(rows).round(3).to_string(index=False))
viz.plot_precision_recall(curves)

measure 1-NN mAP
CBDTW 1.000 0.999
CBDTW, Grassmann 1.000 0.998
DTW 0.800 0.681
DTW_I 0.175 0.310
<Axes: xlabel='recall', ylabel='precision'>
Parameter study: number of segments and of principal components#
for k in (4, 8, 12):
for q in (1, 2, 3):
D = wp.CBDTW(n_segments=k, n_components=q).fit(C).pairwise()
print(
f"{k:2d} segments, {q} PCs: mAP {mean_average_precision(D, y):.3f}"
)
plt.show()
4 segments, 1 PCs: mAP 0.758
4 segments, 2 PCs: mAP 1.000
4 segments, 3 PCs: mAP 0.969
8 segments, 1 PCs: mAP 0.851
8 segments, 2 PCs: mAP 0.999
8 segments, 3 PCs: mAP 0.978
12 segments, 1 PCs: mAP 0.877
12 segments, 2 PCs: mAP 0.998
12 segments, 3 PCs: mAP 0.970
Total running time of the script: (0 minutes 6.080 seconds)