Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Process-dynamics-aware warping: DPCA-CBDTW#
Bankó’s second thesis extends CBDTW with dynamic PCA: the segments are homogeneous in their dynamics, and the segment models are built on lag-augmented data, so that series are compared by the sequence of their input–output behaviour. Here the classes differ only in the order in which the time constant of a first-order system changes; every series has three regimes, so three segments are used.
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_dynamic_sequences
from mvtsa.evaluation import knn_accuracy, mean_average_precision
C, y = make_dynamic_sequences(n_classes=3, n_per_class=10, random_state=0)
viz.plot_series(C[0], names=["u", "y"])

<Figure size 700x280 with 2 Axes>
Static versus dynamic segment models#
rows = []
for name, m in [
(
"DPCA-CBDTW (2 lags, 4 PCs)",
wp.CBDTW(n_segments=3, n_components=4, lags=2).fit(C),
),
("CBDTW (1 PC)", wp.CBDTW(n_segments=3, n_components=1).fit(C)),
("DTW", "dtw"),
]:
D = pairwise_distances(C, metric=m)
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))
measure 1-NN mAP
DPCA-CBDTW (2 lags, 4 PCs) 1.000 1.000
CBDTW (1 PC) 0.933 0.879
DTW 0.700 0.593
The static correlation of \(u\) and \(y\) already reflects the time constant – a slow system follows the input less closely – so CBDTW is a strong baseline. The dynamic segment models capture the input–output relation itself.
Parameter study: lags and principal components#
With \(L\) lags the segment models live in \(2(L+1)\) dimensions. The dynamic relation \(y_k=a\,y_{k-1}+(1-a)\,u_{k-1}\) and the near-relation of the piecewise constant input show up in the smallest directions: the best models keep all but the last one to three components, while too few components lose the structure.
for L in (1, 2, 3):
res = []
for q in range(1, 2 * (L + 1)):
D = wp.CBDTW(n_segments=3, n_components=q, lags=L).fit(C).pairwise()
res.append(f"{q}:{mean_average_precision(D, y):.2f}")
print(f"lags {L} mAP by number of PCs " + " ".join(res))
plt.show()
lags 1 mAP by number of PCs 1:0.85 2:0.55 3:0.95
lags 2 mAP by number of PCs 1:0.86 2:0.75 3:0.85 4:1.00 5:0.59
lags 3 mAP by number of PCs 1:0.92 2:0.83 3:0.93 4:0.95 5:0.99 6:0.68 7:0.51
Total running time of the script: (0 minutes 1.565 seconds)