Skip to contents

Distance-based anomaly and discord detection using Dynamic Time Warping (DTW) with DTW Barycenter Averaging (DBA) centroids (Sakoe & Chiba 1978; Berndt & Clifford 1994; Petitjean et al. 2011). When seq == 1, isolated observations are labeled as anomalies; when seq > 1, subsequences are labeled as discords. Self-contained base-R implementation shared across the DAL stack.

Usage

hanct_dtw(seq = 1, centers = NA, window = NULL, max_iter = 20)

Arguments

seq

Integer. Subsequence (sliding-window) size (default 1).

centers

Integer or NA. Number of centroids (NA defaults to ceil(log10(n))).

window

Integer or NULL. Optional Sakoe-Chiba warping band width.

max_iter

Integer. Maximum DTW-DBA clustering iterations (default 20).

Value

hanct_dtw object.

References

  • Sakoe H, Chiba S (1978). Dynamic programming algorithm optimization for spoken word recognition. IEEE Trans. ASSP, 26(1):43-49.

  • Berndt DJ, Clifford J (1994). Using Dynamic Time Warping to Find Patterns in Time Series. KDD-94 Workshop, pp. 359-370.

  • Petitjean F, Ketterlin A, Gancarski P (2011). A global averaging method for dynamic time warping, with applications to clustering. Pattern Recognition, 44(3):678-693.

  • Ogasawara E, Salles R, Porto F, Pacitti E (2025). Event Detection in Time Series. 1st ed. Cham: Springer Nature Switzerland. doi:10.1007/978-3-031-75941-3

Examples

library(daltoolbox)

# Load anomaly example data
data(examples_anomalies)

# Use a simple example
dataset <- examples_anomalies$simple
head(dataset)
#>       serie event
#> 1 1.0000000 FALSE
#> 2 0.9689124 FALSE
#> 3 0.8775826 FALSE
#> 4 0.7316889 FALSE
#> 5 0.5403023 FALSE
#> 6 0.3153224 FALSE

# Configure DTW-based detector
model <- hanct_dtw()

# Fit the model
model <- fit(model, dataset$serie)

# Run detection
detection <- detect(model, dataset$serie)

# Show detected events
print(detection[(detection$event),])
#>    idx event    type
#> 50  50  TRUE anomaly