dtw_distance#

dtw_distance(x: ndarray, y: ndarray, window: float = None) float[source]#

Compute the dtw distance between two time series.

Originally proposed in [1] DTW computes the distance between two time series by considering their alignments during the calculation. This is done by measuring the pointwise distance (normally using Euclidean) between all elements of the two time series and then using dynamic programming to find the warping path that minimises the total pointwise distance between realigned series.

Mathematically dtw can be defined as:

\[dtw(x, y) = \sqrt{\sum_{(i, j) \in \pi} \|x_{i} - y_{j}\|^2}\]
Parameters:
x: np.ndarray, of shape (n_channels, n_timepoints) or (n_timepoints,)

First time series.

y: np.ndarray, of shape (m_channels, m_timepoints) or (m_timepoints,)

Second time series.

window: float, default=None

The window to use for the bounding matrix. If None, no bounding matrix is used.

Returns:
float

dtw distance between x and y.

Raises:
ValueError

If x and y are not 1D or 2D arrays.

References

[1]

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

Examples

>>> import numpy as np
>>> from aeon.distances import dtw_distance
>>> x = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
>>> y = np.array([[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
>>> dtw_distance(x, y)
768.0