wannesm / dtaidistance

Time series distances: Dynamic Time Warping (fast DTW implementation in C)
Other
1.08k stars 184 forks source link

Inconsistent behavior for `psi` relaxation for `s1` and `s2` #211

Closed raui100 closed 2 months ago

raui100 commented 2 months ago

I've stumbled upon the following incosistency: Given the arrays double s1[] = {0.0, 1.0}, double s2[] = {0.0, 0.0} and DTWSettings settings = dtw_settings_default() I would expect the following code to produce the same results.
However 1. calculates 1.0 and 2. calculates 0.0. Is this correct and expected?

  1. settings.psi_1e = 1;
    double d = dtw_distance_ndim_euclidean(s1, 2, s2, 2, 1, &settings);
  2. settings.psi_2e = 1;
    double d = dtw_distance_ndim_euclidean(s1, 2, s2, 2, 1, &settings);
C test code ```c Test(dtw_psi, test_a_d) { double s1[] = {0.0, 1.0}; double s2[] = {0.0, 0.0}; DTWSettings settings = dtw_settings_default(); settings.psi_1e = 1; double d = dtw_distance_ndim_euclidean(s1, 2, s2, 2, 1, &settings); cr_log_warn("%f", d); cr_assert_float_eq(d, 1.0, 0.001); } Test(dtw_psi, test_a_e) { double s1[] = {0.0, 0.0}; double s2[] = {0.0, 1.0}; DTWSettings settings = dtw_settings_default(); settings.psi_2e = 1; double d = dtw_distance_ndim_euclidean(s1, 2, s2, 2, 1, &settings); cr_log_warn("%f", d); cr_assert_float_eq(d, 0.0, 0.001); } ```
wannesm commented 2 months ago

Thanks for the report and useful example. There was indeed a bug in the distance function for psi_1e. Fix available in the master branch.

However, code 1 and code 2 are expected to give different results.

Code 1 should return 0.0 and code 2 returns 1.0. If psi_1e is set to 1, the last value in the first series can be ignored. In this case that is a 1.0 that would be mapped to a 0.0. The result is thus 0.0 instead of 1.0. If psi_2e is set to 1, the last value in the second series can be ignored. But this last value is 0.0, same like the first value. The 1.0 in the second series is thus still aligned to a 0.0 with a difference of 1.0. The result is thus 1.0.

raui100 commented 2 months ago

Thank you so much for the work you put into this library :) And thanks a bunch for the explanation...