MODULE REFERENCE

dtw

anomx.data.processing.dtw.dtw

Dynamic Time Warping (DTW)

DTWAlignment

DTWAlignment(series1: TimeSeries, series2: TimeSeries, cost: CostMatrix)

Dynamic Time Warping (DTW) Alignment.

Attributes

n

The length of series1

m

The length of series2

series1

A TimeSeries to align with series2.

series2

A TimeSeries to align with series1.

cost

The CostMatrix for DTW.

Signature parameters
ParameterTypeDefault
series1TimeSeriesrequired
series2TimeSeriesrequired
costCostMatrixrequired

Members

pathmethod
path() -> np.ndarray

Gives the index paths from series1 to series2.

Returns

np.ndarray of shape (len(path), 2)

An array of indices [[i0,j0], [i1,j1], [i2,j2], ...], where i indexes into series1 and j indexes into series2. Indices are in monotonic order, path[n] >= path[n-1]

Returns np.ndarray

distancemethod
distance() -> float

Gives the total distance between pair-wise elements in the two series after warping.

Returns

float

The total distance between pair-wise elements in the two series after warping.

Returns float

mean_distancemethod
mean_distance() -> float

Gives the mean distance between pair-wise elements in the two series after warping.

Returns

float

The mean distance between pair-wise elements in the two series after warping.

Returns float

warpedmethod
warped() -> (TimeSeries, TimeSeries)

Warps the two time series according to the warp path returned by DTWAlignment.path(), which minimizes the pair-wise distance. This will bring two time series that are out-of-phase back into phase.

Returns

(TimeSeries, TimeSeries)

Two new TimeSeries instances of the same length, indexed by pd.RangeIndex.

Returns (TimeSeries, TimeSeries)

dtw

dtw(series1: TimeSeries, series2: TimeSeries, window: Window | None = None, distance: DistanceFunc | None = None, multi_grid_radius: int = -1) -> DTWAlignment

Determines the optimal alignment between two time series series1 and series2, according to the Dynamic Time Warping algorithm. The alignment minimizes the distance between pair-wise elements after warping. All elements in the two series are matched and are in strictly monotonically increasing order. Considers only the values in the series, ignoring the time axis.

Dynamic Time Warping can be applied to determine how closely two time series correspond, irrespective of phase, length or speed differences.

Parameters

series1

A TimeSeries to align with series2.

series2

A TimeSeries to align with series1.

window

Optionally, a Window used to constrain the search for the optimal alignment: see SakoeChiba and Itakura. Default considers all possible alignments (NoWindow).

distance

Function taking as input either two floats for univariate series or two np.ndarray, and returning the distance between them.

Defaults to the abs difference for univariate-data and the sum of the abs difference for multi-variate series.

multi_grid_radius

Default radius of -1 results in an exact evaluation of the dynamic time warping algorithm. Without constraints DTW runs in O(nxm) time where n,m are the size of the series. Exact evaluation with no constraints, will result in a performance warning on large datasets.

Setting multi_grid_radius to a value other than -1, will enable the approximate multi-grid solver, which executes in linear time, vs quadratic time for exact evaluation. Increasing radius trades solution accuracy for performance.

Returns

DTWAlignment

Helper object for getting warp path, mean_distance, distance and warped time series

Signature parameters
ParameterTypeDefault
series1TimeSeriesrequired
series2TimeSeriesrequired
windowWindow | NoneNone
distanceDistanceFunc | NoneNone
multi_grid_radiusint-1

Returns DTWAlignment