### R code from vignette source 'flowz-introduction.Rnw'

###################################################
### code chunk number 1: setup
###################################################
flowz_old_options <- options(width = 72)
library(flowz)


###################################################
### code chunk number 2: data
###################################################
t <- seq_len(260)
x <- cumsum(sin(t / 4) + cos(t / 11))


###################################################
### code chunk number 3: fit
###################################################
fit <- flowz(
  x, horizon = 3, transform = "none",
  baseline = "ewma", baseline_control = list(), autotune = TRUE,
  drift_lambda = 0.90, vol_lambda = 0.94,
  min_train = NULL, innovation_lags = 8,
  n_hidden = 10, lambda = 1e-3, flow_aug = 1,
  n_steps = 3, n_sims = 30,
  cal_fraction = 0.20, test_fraction = 0.20,
  cal_sims = 30, test_sims = 30,
  spectral_modes = NULL, spectral_spar = 0.65,
  spectral_shrink_to_one = 0.10,
  spectral_tau_bounds = c(0.15, 2.50),
  distribution_bw_factor = 0.50,
  seed = 123, n_origins = NULL,
  parallel = FALSE, n_cores = NULL,
  plot = FALSE, verbose = FALSE
)
fit$forecast$calibrated$summary
dim(fit$forecast$calibrated$samples)


###################################################
### code chunk number 4: output-structure
###################################################
fc <- fit$forecast
names(fc)
fc$calibrated$pred_fun$qfun$h3(c(.05, .5, .95))
fc$baseline$pred_fun$qfun$h3(c(.05, .5, .95))
fc$uncalibrated$pred_fun$qfun$h3(c(.05, .5, .95))
fit$evaluation$metadata$scale


###################################################
### code chunk number 5: baseline-options (eval = FALSE)
###################################################
## fit_arima <- flowz(x, baseline = "arima", autotune = TRUE)
## fit_arfima <- flowz(x, baseline = "arfima", autotune = TRUE)
## fit_structural <- flowz(x, baseline = "structural",
##   baseline_control = list(type = "trend"), autotune = FALSE)
## fit_arima$metadata$baseline_label


###################################################
### code chunk number 6: evaluation
###################################################
o <- fit$metadata$diagnostics$origin_selection
c(available = o$available_origins,
  selected = o$selected_origins,
  median_spacing = o$median_spacing)
sc <- fit$calibration
c(training = sc$train_origins,
  calibration = sc$calibration_origins,
  test = sc$test_origins)
fit$evaluation$metrics$aggregate


###################################################
### code chunk number 7: horizon-scores
###################################################
head(fit$evaluation$metrics$per_horizon)


###################################################
### code chunk number 8: marginals
###################################################
fun <- fit$forecast$calibrated$pred_fun
fun$qfun$h3(c(0.05, 0.50, 0.95))
threshold <- fun$qfun$h3(0.5)
fun$pfun$h3(threshold)
fun$dfun$h3(threshold)
set.seed(42)
head(fun$rfun$h3(10))


###################################################
### code chunk number 9: joint-event
###################################################
paths <- fit$forecast$calibrated$samples
mean(rowSums(paths > tail(x, 1)) == ncol(paths))


###################################################
### code chunk number 10: prediction
###################################################
t_new <- seq_len(265)
x_new <- cumsum(sin(t_new / 4) + cos(t_new / 11))
pr <- predict(fit, x_new, n_sims = 50, seed = 42)
pr$forecast$calibrated$summary
pr$forecast$calibrated$pred_fun$qfun$h3(c(0.05, 0.95))


###################################################
### code chunk number 11: prices
###################################################
prices <- 100 * exp(x / 20)
price_fit <- flowz(prices, horizon = 3, transform = "log",
  n_hidden = 10, flow_aug = 1, n_steps = 3,
  n_sims = 30, cal_sims = 30, test_sims = 30, parallel = FALSE)
price_fit$forecast$calibrated$summary[, c("horizon", "mean", "q05", "q95")]
price_fit$forecast$calibrated$pred_fun$qfun$h3(c(0.05, 0.50, 0.95))
stopifnot(all(price_fit$forecast$calibrated$samples > 0))


###################################################
### code chunk number 12: diagnostics
###################################################
plot(price_fit)


###################################################
### code chunk number 13: cleanup
###################################################
options(flowz_old_options)


