knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

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)
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)

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)
)

bench_tsdb %>%
  select(expression, min, median, mem_alloc)


tmastny/tsrecipes documentation built on Aug. 28, 2020, 11:38 a.m.