rtime-seriesdata-sciencedata-analysisdtw

Dynamic Time Warping (DTW) monotonicity constraint


How to specify a monotonicity constraint (that one time series should not come before the other) when using dynamic time warping?

For example, I have cost and revenue data; one should impact the other but not vice versa. I am using the basic dtw package but I know that there are many others that could be better. Below is my current alignment.

(I would like to save the corresponding revenue point into a separate column, would that be possible?)

library(dtw)

asy<-dtw(df$cost,
         df$revenue,
         keep=TRUE,       
         window.size = 7, # max 7 days shift
         step=asymmetric  # gives best results for this problem (other: symmetric1 & symmetric2)
         );

plot(asy, type="two", off=1);

enter image description here

Thank you for your help!


Solution

  • I think you can enforce this by defining your own window function. For example, take these series:

    library(dtw)
    
    set.seed(310L)
    
    idx <- seq(0, 6.28, len = 100L)
    reference <- sin(idx)
    query <- cos(idx) + runif(100L) / 10
    
    foo <- dtw(query, reference, keep = TRUE, step.pattern = symmetric2, window.type = sakoeChibaWindow, window.size = 30L)
    plot(foo, type = "two", off = 2)
    

    before

    The red line is the reference, and you want the query's values to only match values from the past or the same day.

    win_fun <- function(i, j, ...) { i >= j }
    
    bar <- dtw(query, reference, keep = TRUE, step.pattern = symmetric2, window.type = win_fun)
    plot(bar, type = "two", off = 2)
    

    after

    If you want to match past values strictly excluding values from the same time, change the condition to i > j.

    Check the documentation of dtwWindowingFunctions for more options. You might want to add a window size constraint.