Similarities in machine learning — Dynamic Time Warping example
By: Nastasia Saby
Tags: Time Series Analysis, Data Science, Clustering, Distance, Machine Learning

Photo by Sharon McCutcheon on Unplash

In machine learning we can realize clustering. Clustering is a way to group items that are similar.

To do it we need to compare the items between them.

Different types of data to compare

For instance, to compare 2 prices (100 and 200), we can do a substraction. 200 - 100 = 100.

The distance between the two prices is 100.

Computing a distance can be less trivial.

Suppose we have diamonds data. We have their prices, the day for each price and the brand which sells the diamond. We would like to know which brand is cheap, average or expensive.

We can make a clustering by comparing their prices. We group the diamonds according to their prices.

The problem is that a price can vary over time.

For instance, diamondsForEver, an imaginary brand sells a diamond during a week. The price is as follows:

Monday: 102

Thuesday: 150

Wednesday: 120

Thursday: 108

Friday: 110

Saturday: 109

Sunday: 135

Another brand named coolDiamonds sells the same diamond as follows:

Monday: 116

Thuesday: 143

Wednesday: 110

Thursday: 118

Friday: 101

Saturday: 122

Sunday: 138

Here it is more difficult to compare the prices for the two brands.

We are facing two time series.

The good news is there is a way to get the distance between time series: the Dynamic Time Warping.

With the Dynamic Time Warping, we can get a measure of similarity taking account the accelerations and decelerations. Then we could compare many websites between them to know which ones are closer than others.

Dynamic Time Warping (DTW)

I am going to compute it with three days to get an easier example.

To calculate it, we are going to construct a matrix.

We initialized the matrix with “Infinite” to signal the absence of value.

Matrix with initialized values

For each cell, the formula is the distance between (x -y) + smallest of some adjacent cells.

For example, we want to compute B3. A3, A4 and B4 are the adjacent cells.

Distance + min of adjacent cells

To compute B3, we are going to make the difference between 102 and 116, which is 14.

The adjacent cells for B3 are A3, A4 and B4:

Computing B3

So the result for B3 is 14 + nothing = 14.

B3 result 14 + nothing = 14

To compute B2, we make the difference between 150 and 116, which is 34. The adjacent cells for B2 are A2, A3 and B3:

Computing B2

The smallest of these cells is 14. So the result is 34 + 14 = 48.

B2 result = 34 + 14 = 48

We continue.

Fill some cells

Now, to compute C2, we make the difference between 143 and 150, which is 7. We take the smallest of the adjacent cells:

Computing C2

The smallest value is 14. So the result is 7 + 14 = 21.

C2 result = 7 + 14 = 21

We continue and return the last value 31 (D1).

Last value 31

We have a way to compute the distance between our values.

Note that the distance between x and y can be the difference between them. It can be something else like a squarred substraction |x -y|².

Conclusion

DTW was first used to recognize similarities in speech voice. But it can be useful in machine learning to compute distance between two time series. This way, we can group them in clusters the correct way for instance.

We can compute similarities in machine learning using several other ways like tanimoto distance or manhattan distance.