fuzzyclust.relational.dtw_distance_matrix#

fuzzyclust.relational.dtw_distance_matrix(X, Y=None, window=None)[source]#

Dynamic time warping distances between all pairs of (equal-length) series.

Dynamic time warping aligns two series by a monotone, continuous path through the matrix of local costs \(c(t,s)=\norm{x_t-y_s}^2\) so that the summed cost along the path is minimal (Sakoe and Chiba, 1978),

\[\mathrm{DTW}(\mathbf{x},\mathbf{y})^2=\min_{\text{paths}}\sum_{(t,s)\in\text{path}} \norm{x_t-y_s}^2 ,\]

with the symmetric step pattern \((t-1,s),(t,s-1),(t-1,s-1)\) and the Sakoe–Chiba band \(|t-s|\le w\) as the global constraint. The square root of the minimal cost is returned, so that for window=0 the result is the Euclidean distance of the two series. The recursion runs over all pairs simultaneously (vectorized over the pairs), so the cost is \(O(N^2\,T\,w)\) with n_series \(N\), length \(T\) and band width \(w\).

Parameters:
Xarray_like of shape (n_series, length) or (n_series, length, n_channels)

The series (multichannel series use the squared Euclidean norm of the channel difference as local cost).

Yarray_like of the same kind, optional

Second set of series (same length); the result is then rectangular.

windowint, float in (0, 1) or None, default=None

Half width \(w\) of the Sakoe–Chiba band in samples, or as a fraction of the length; None imposes no constraint, 0 gives the Euclidean distance.

Returns:
Dndarray of shape (n_series, n_series) or (n_series, n_other)

DTW distances (not squared). Note that DTW is not a metric: the triangle inequality may fail, so the squared matrix is in general not Euclidean and relational c-means needs the \(\beta\)-spread (NERFCM).

References

[1]

H. Sakoe, S. Chiba, “Dynamic programming algorithm optimization for spoken word recognition”, IEEE Transactions on Acoustics, Speech, and Signal Processing 26(1), 43–49, 1978.

Examples

>>> import numpy as np
>>> from fuzzyclust.relational import dtw_distance_matrix
>>> t = np.linspace(0, 1, 30)
>>> X = np.vstack([np.sin(2 * np.pi * t), np.sin(2 * np.pi * (t - 0.1)), np.cos(2 * np.pi * t)])
>>> D = dtw_distance_matrix(X, window=5)
>>> bool(D[0, 1] < D[0, 2]), D.shape
(True, (3, 3))

Examples using fuzzyclust.relational.dtw_distance_matrix#

Fuzzy c-medoids on dynamic-time-warping dissimilarities of time series

Fuzzy c-medoids on dynamic-time-warping dissimilarities of time series