Introduction

The dtt provides discrete trigonometric transforms, but unfortunately the implementation is rather slow.

Following the suggestion of Stackoverflow tsrecipes includes a faster discrete cosine transform based off of stats::fft and stats::mvfft that are included in R.

Single time series vector

library(dtt)
library(tsrecipes)
library(tidyverse)
#> ── Attaching packages ─────────────────────────────────────────────────── tidyverse 1.2.1 ──
#> ✓ ggplot2 3.3.2     ✓ purrr   0.3.4
#> ✓ tibble  3.0.3     ✓ dplyr   1.0.0
#> ✓ tidyr   1.1.0     ✓ stringr 1.4.0
#> ✓ readr   1.3.1     ✓ forcats 0.4.0
#> ── Conflicts ────────────────────────────────────────────────────── tidyverse_conflicts() ──
#> x dplyr::filter() masks stats::filter()
#> x dplyr::lag()    masks stats::lag()
ts <- prices$ts[[1]]

dct_dtt <- dtt::dct
bench_ts <- bench::mark(
  fdct(ts),
  dct_dtt(ts)
)

bench_ts %>%
  select(expression, min, median, mem_alloc)
#> # A tibble: 2 x 4
#>   expression       min   median mem_alloc
#>   <bch:expr>  <bch:tm> <bch:tm> <bch:byt>
#> 1 fdct(ts)      21.8µs     28µs     162KB
#> 2 dct_dtt(ts)  130.5µs    157µs     149KB

List of time series

dct_so_apply <- function(l) {
  l %>%
    simplify2array() %>%
    t() %>%
    apply(1, fdct) %>%
    t()
}

dct_so_mv <- function(l) {
   l %>%
    simplify2array() %>%
    mvfdct() %>%
    t()
}

dct_dtt_apply <- function(l) {
  l %>%
    simplify2array() %>%
    t() %>%
    apply(1, dtt::dct) %>%
    t()
}

dct_dtt_mv <- function(l) {
  l %>%
    simplify2array() %>%
    t() %>%
    dct()
}

ts_list <- prices$ts

bench_tsdb <- bench::mark(
  dct_dtt_apply(ts_list),
  dct_dtt_mv(ts_list),
  dct_so_apply(ts_list),
  dct_so_mv(ts_list)
)
#> Warning: Some expressions had a GC in every iteration; so filtering is disabled.

bench_tsdb %>%
  select(expression, min, median, mem_alloc)
#> # A tibble: 4 x 4
#>   expression                  min   median mem_alloc
#>   <bch:expr>             <bch:tm> <bch:tm> <bch:byt>
#> 1 dct_dtt_apply(ts_list) 175.36ms 176.31ms    49.4MB
#> 2 dct_dtt_mv(ts_list)    183.21ms 286.65ms    49.4MB
#> 3 dct_so_apply(ts_list)   23.57ms  26.93ms    14.3MB
#> 4 dct_so_mv(ts_list)       6.07ms   8.55ms     5.8MB