idtw2vec: Incremental vector-based DTW

View source: R/idtw.R

idtw2vecR Documentation

Incremental vector-based DTW

Description

Update the DTW distance for new observations of two time series.

Usage

idtw2vec(Q, newObs, dist_method = c("norm1", "norm2", "norm2_square"),
         step_pattern = c("symmetric2", "symmetric1"),
         gcm_lc = NULL, gcm_lr = NULL, nC = NULL, ws = NULL)

Arguments

Q

Either Q is (a) a time series (vector or matrix for multivariate time series) or (b) Q is a cost matrix, so a matrix storing the local distances of the time series Q and newObs. If Q and newObs are matrices, they need to have the same number of columns. If Q is a cost matrix, see details...

newObs

time series as vector or matrix, or if Q is a cost matrix newObs must equals "cm". If newObs is a time series, see details...

dist_method

character, describes the method of distance measure. See also dtw.

step_pattern

character, describes the step pattern. See also dtw.

gcm_lc

vector, last column of global cost matrix of previous calculation. If NULL (necessary for the initial calculation), then DTW is calculated and the last column and last row are returned to start upcoming incremental calculations. (default = NULL)

gcm_lr

vector, last row of global cost matrix of previous calculation (default = NULL).

nC

integer, is the length of the original time series C, of which newObs are the new observations. Length of time series C exclusive new observations, such that length(c(C, newObs)) = nC + length(newObs). Necessary if ws is not NULL. (default = NULL)

ws

integer, describes the window size for the sakoe chiba window. If NULL, then no window is applied. (default = NULL)

Details

If new observations are recorded only for C and the only interest is a fast update of the DTW distance, the last row is not required, neither for the current nor for future incremental calculations.

If Q is a cost matrix, it needs to store either the distances of Q and new observations of C (running calculations, in that case gcm_lc != NULL), or it stores the distances of Q and the entire time series C (initial calculation, in that case gcm_lc = NULL).

If newObs is a time series, it stores either new Observations of C (running calculations) or the complete time series C (initial calculation).

no matrices are allocated, no matrices are returned

Value

distance

the DTW distance

gcm_lc_new

the last column of the new global cost matrix

gcm_lr_new

the last row of the new global cost matrix. Only if the input vector gcm_lr is not NUll and represents the last row of the previous global cost matrix, gcm_lr_new actually is the last row of the updated global cost matrix. Otherwise, if gcm_lr is NULL then gcm_lr_new is only the last row of the new part (concerning the new observations) of the global cost matrix.

normalized_distance

the normalized DTW distance, see also dtw

References

  • Leodolter, M.; Pland, C.; Brändle, N; IncDTW: An R Package for Incremental Calculation of Dynamic Time Warping. Journal of Statistical Software, 99(9), 1-23. doi: 10.18637/jss.v099.i09

  • Sakoe, H.; Chiba, S., Dynamic programming algorithm optimization for spoken word recognition, Acoustics, Speech, and Signal Processing [see also IEEE Transactions on Signal Processing], IEEE Transactions on , vol.26, no.1, pp. 43-49, Feb 1978. http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=1163055

Examples


#--- Do the vector-based incremental DTW 
# calculation and compare it with the basic
Q <- cumsum(rnorm(100))
C <- Q[11:100] + rnorm(90, 0, 0.5)

# initial calculation
res0 <- idtw2vec(Q = Q, newObs = C, gcm_lc = NULL)

# incremental calculation for new observations
nobs <- rnorm(10)
res1 <- idtw2vec(Q, newObs = nobs, gcm_lc = res0$gcm_lc_new)

# compare with result from scratch
res2 <- dtw2vec(Q, c(C, nobs))
res1$distance - res2$distance



#--- Perform an incremental DTW calculation with a 
#  customized distance function. 
d_cos <- function(x, y){
   1 - sum(x * y)/(sqrt(sum(x^2)) * sqrt(sum(y^2))) 
}

x <- matrix(rnorm(100), ncol = 5, nrow = 20)
y <- matrix(rnorm(150), ncol = 5, nrow = 30)
cm1 <- cm(x, y, dist_method = d_cos)

# initial calculation
res0 <- idtw2vec(Q = cm(x, y[1:20,], dist_method = d_cos), 
                 newObs =  "cm")

# incremental calculation for new observations
res1 <- idtw2vec(Q = cm(x, y[21:30,], d_cos), newObs =  "cm", 
         gcm_lc = res0$gcm_lc_new)$distance

# compare with result from scratch
res2 <- dtw2vec(Q = cm1, C = "cm")$distance
res1 - res2


IncDTW documentation built on March 18, 2022, 6:43 p.m.