Skip to content

[ENH] Implementation of DTW: should it have the square root? #669

Open
@sylvaincom

Description

@sylvaincom

Describe the bug

Hi,

First of all, thanks for the great work at aeon.

Your implementation of DTW (aeon.distances.dtw_distance) differs from the one by tslearn (tslearn.metrics.dtw) as it does not seem the apply the square root.

Steps/Code to reproduce the bug

The following returns an AssertionError:

# Imports
import numpy as np
from aeon.datasets import load_arrow_head
from aeon.distances import dtw_distance as dtw_aeon
from tslearn.metrics import dtw as dtw_tslearn
from scipy.spatial.distance import pdist, squareform
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import adjusted_rand_score

# Load some data
X, y = load_arrow_head(return_type="numpy2d")

# Compute the distance matrix for different implementations
distance_matrix_dtw_aeon = squareform(pdist(X, dtw_aeon))
distance_matrix_dtw_tslearn = squareform(pdist(X, dtw_tslearn))

# Check if the distance matrix are the same
err_msg = "The distance matrix from DTW differs between aeon and tslearn"
assert np.allclose(distance_matrix_dtw_aeon, distance_matrix_dtw_tslearn), err_msg

The following returns True:

distance_matrix_dtw_aeon_sqrt = np.sqrt(distance_matrix_dtw_aeon)
np.allclose(distance_matrix_dtw_aeon_sqrt, distance_matrix_dtw_tslearn)

Expected results

No error is thrown

Actual results

AssertionError: The distance matrix from DTW differs between aeon and tslearn

Versions

System: python: 3.10.6 (main, Oct 24 2022, 11:04:34) [Clang 12.0.0 ] executable: [/Users/my_username/opt/anaconda3/envs/my_env/bin/python](https://file+.vscode-resource.vscode-cdn.net/Users/my_username/opt/anaconda3/envs//bin/python) machine: macOS-10.16-x86_64-i386-64bit

Python dependencies:
pip: 22.1.2
setuptools: 59.8.0
sklearn: 0.0
aeon: 0.4.0
statsmodels: 0.14.0
numpy: 1.23.5
scipy: 1.9.3
pandas: 2.0.3
matplotlib: 3.5.3
joblib: 1.2.0
numba: 0.56.2
pmdarima: None
tsfresh: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions