Recently, I came across this algorithm called "Dynamic Time Warp" (e.g. https://cran.r-project.org/web/packages/dtw/vignettes/dtw.pdf).

Although this algorithm looks quite involved and complicated, it seems that the main purpose is to determine if two (seemingly different - e.g. two people walking at different speeds) time series are "similar to each other". It also seems that the DTW algorithm can be used for clustering time series data as well (https://cran.r-project.org/web/packages/dtwclust/vignettes/dtwclust.pdf).

Has anyone ever used the DTW algorithm before? What kind of problem did you use it on? What industry did the data come from (e.g. environmental, finance, etc.)? Was it successful? How exactly did it help you in your work/research?

Does it make sense to use the DTW algorithm to check if two stock returns (or financial time series) are related to each other? I am confused as to why this algorithm is useful. Once you have found out that two time series are "related", what can you do with this information? Does DTW answer similar questions as Granger Causality or Cointegration?


  • $\begingroup$ Many of your questions can be answered by searching this site for "DTW" or "Dynamic Time Warping." $\endgroup$
    – whuber
    May 12 at 17:12

1 Answer 1


DTW is an algorithm for measuring the distance between two time series. It's an alternative to the Euclidean distance (which is the mean squared distance between the time series at each time step), and is useful when the time series are or may be out of step, for example if one is phase shifted or stretched. DTW allows for this situation by "warping" or re-aligning the time series so the phase-shifted or stretched points are correctly matched. See A measure of distance between time series: Dynamic Time Warping by Jessica Leung and Robert James for a more complete explanation.

DTW (or any algorithm for measuring the distance between two time series) can't tell you if two time series are related. What it can tell you is that if you have three time series A, B, and C, if the DTW distance between A and B is less than the DTW distance between A and C, then time series A is more like time series B than time series C.

As DTW does nothing more than tell you the distance between two time series, it is paired with another algorithm to solve a time series task. For instance, it is often paired with the Nearest Neighbours (NN) classification algorithm for time series classification tasks. Until fairly recently (5 or so years ago) DTW-1NN was considered the go-to algorithm for time series classification. Similarly, when DTW is used for clustering it is combined with a standard clustering algorithm such as k-means, it is used to measure the distances between time series, while k-means does the actual clustering.

See these answers by Alesandr Blekh and Hassan ISMAIL FAWAZ to "Dynamic Time Warping Clustering" for more on DTW and time series clustering.


Not the answer you're looking for? Browse other questions tagged or ask your own question.