movingpandas.Trajectory.dtw_distance#

Trajectory.dtw_distance(other, units=(None, None, None, None), radius=None)#

Return the Dynamic Time Warping (DTW) distance to the other trajectory or geometric object.

DTW finds the alignment between the two ordered point sequences that minimises the accumulated pairwise distance, allowing the sequences to be stretched or compressed along their order (“warped”). Unlike the Fréchet distance, which is the largest single gap along the best alignment, DTW sums the matched point distances, so it reflects the overall cumulative deviation between the trajectories.

By default the exact DTW distance is computed, which takes O(n*m) time (and O(n+m) memory, the dynamic program is evaluated in vectorized anti-diagonal slices). For long trajectories, pass radius to use the FastDTW approximation by Salvador & Chan (2007), which runs in linear time and memory for a given radius. FastDTW never underestimates the exact distance, and a larger radius generally gets closer to the exact distance at the cost of speed. The approximation is not guaranteed to improve at every step, though. Euclidean point distances are used throughout, in both the exact and the approximate computation.

Distances are computed using Euclidean geometry, so a UserWarning is raised for trajectories in a geographic (lat/lon) CRS. Project to a suitable planar CRS first for meaningful results.

If units have been declared:

  • For geographic projections, in declared units

  • For known CRS units, in declared units

  • For unknown CRS units, in declared units as if CRS is in meters

Parameters:
  • other (Trajectory, LineString, or Point) – Other trajectory or geometric object

  • units (str) – Units in which to calculate distance values (default: CRS units) For more info, check the list of supported units at https://movingpandas.org/units

  • radius (int, optional) – Radius of the FastDTW search window. Default None (exact DTW).

Returns:

DTW distance

Return type:

float

References

Salvador, S., & Chan, P. (2007). Toward accurate dynamic time warping in linear time and space. Intelligent Data Analysis, 11(5), 561-580.