## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5)

## ----setup--------------------------------------------------------------------
library(edetect)

## ----arl----------------------------------------------------------------------
design <- edetect_design(arl = 100, class = "subgaussian", center = 0, sigma = 1, direction = "up")
set.seed(1)
no_change <- edetect_arl(design, pre = function(n) rnorm(n), n_rep = 100, max_t = 3000)
no_change$summary

## ----arl-bounded--------------------------------------------------------------
design_b <- edetect_design(arl = 100, class = "bounded", center = 0.5, bounds = c(0, 1), direction = "up")
set.seed(2)
generators <- list(
  uniform = function(n) runif(n),
  beta = function(n) rbeta(n, 4, 4),
  two_point = function(n) rbinom(n, 1, 0.5))
sapply(generators, function(g) edetect_arl(design_b, pre = g, n_rep = 60, max_t = 3000)$summary$arl_estimate)

## ----delay--------------------------------------------------------------------
set.seed(3)
shift <- edetect_arl(design, pre = function(n) rnorm(n), post = function(n) rnorm(n, 1),
                     change_at = 50, n_rep = 100, max_t = 500)
shift$summary

## ----delay-grid---------------------------------------------------------------
set.seed(4)
delays <- sapply(c(0.5, 1, 2), function(mu) {
  edetect_arl(design, pre = function(n) rnorm(n), post = function(n) rnorm(n, mu),
              change_at = 50, n_rep = 60, max_t = 800)$summary$delay_mean
})
data.frame(shift = c(0.5, 1, 2), delay = round(delays, 1))

## ----types--------------------------------------------------------------------
set.seed(5)
types <- sapply(c("sr", "cusum"), function(type) {
  d <- edetect_design(arl = 100, class = "subgaussian", center = 0, sigma = 1, direction = "up", type = type)
  edetect_arl(d, pre = function(n) rnorm(n), post = function(n) rnorm(n, 1),
              change_at = 50, n_rep = 60, max_t = 500)$summary$delay_mean
})
round(types, 1)

## ----families-----------------------------------------------------------------
set.seed(6)
families <- sapply(c("betting", "hoeffding"), function(ev) {
  d <- edetect_design(arl = 100, class = "bounded", center = 0.5, bounds = c(0, 1), direction = "up", evalue = ev)
  edetect_arl(d, pre = function(n) runif(n, 0.3, 0.7), post = function(n) runif(n, 0.45, 0.85),
              change_at = 50, n_rep = 60, max_t = 800)$summary$delay_mean
})
round(families, 1)

