In this post, we will go over the mathematics behind DTW. Then, two illustrative examples are provided to better understand the concept. If you are not interested in the math behind it, please jump to examples.

Dynamic Time Warping (DTW) is a way to compare two -usually temporal- sequences that do not sync up perfectly. It is a method to calculate the optimal matching between two sequences. DTW is useful in many domains such as speech recognition, data mining, financial markets, etc. Itβs commonly used in data mining to measure the distance between two time-series.

In this post, we will go over the mathematics behind DTW. Then, two illustrative examples are provided to better understand the concept. If you are not interested in the math behind it, please jump to examples.

Letβs assume we have two sequences like the following:

*π=π₯[1], π₯[2], β¦, x[i], β¦, x[n]*

*Y=y[1], y[2], β¦, y[j], β¦, y[m]*

The sequences π and π can be arranged to form an π-by-π grid, where each point (π, j) is the alignment between π₯[π] and π¦[π].

A warping path π maps the elements of π and π to minimize the *distance* between them. π is a sequence of grid points (π, π). We will see an example of the warping path later.

The Optimal path to (π_π, π_π) can be computed by:

where π is the Euclidean distance. Then, the overall path cost can be calculated as

The warping path is found using a dynamic programming approach to align two sequences. Going through all possible paths is βcombinatorically explosiveβ [1]. Therefore, for efficiency purposes, itβs important to limit the number of possible warping paths, and hence the following constraints are outlined:

**Boundary Condition**: This constraint ensures that the warping path begins with the start points of both signals and terminates with their endpoints.**Monotonicity condition**: This constraint preserves the time-order of points (not going back in time).**Continuity (step size) condition**: This constraint limits the path transitions to adjacent points in time (not jumping in time).

In addition to the above three constraints, there are other less frequent conditions for an allowable warping path:

**Warping window condition**: Allowable points can be restricted to fall within a given warping window of width π (a positive integer).**Slope condition**: The warping path can be constrained by restricting the slope, and consequently avoiding extreme movements in one direction.

An acceptable warping path has combinations of **chess king** moves that are:

- Horizontal moves: (π, π) β (π, π+1)
- Vertical moves: (π, π) β (π+1, π)
- Diagonal moves: (π, π) β (π+1, π+1)

time-series-analysis data-science pattern-recognition dynamic-programming python

A real-world client-facing project with real sensor data.In the project, there are two data sets, each consists of one week of sensor readings are provided to accomplish the following four tasks.

Master Applied Data Science with Python and get noticed by the top Hiring Companies with IgmGuru's Data Science with Python Certification Program. Enroll Now

What is a Time Series? Depending on the frequency of observations, a time series may typically be hourly, daily, weekly, monthly, quarterly and annual. Sometimes, you might have seconds and minute-wise time series as well, like, number of clicks and user visits every minute etc.

I knew I wanted to do two things in the process of writing my bachelorβs thesis: improve my programming skills and work with time-series data prediction.

TS may look like a simple data object and easy to deal with, but the reality is that for someone new it can be a daunting task just to prepare the dataset before the actual fun stuff can begin.