backshift 0.3.0

Time-series forecasting checked against R and statsmodels: ARIMA, ETS, Theta, VAR, Croston/TSB, GARCH and changepoint detection, with streaming filters that run without a heap
Documentation
# Generate R reference vectors for the accuracy and cross-validation conformance tests.
#
# Writes `tests/it/r_accuracy_data.rs`. R's packages are pinned by the Docker image in
# `scripts/r/`; run from the repository root:
#
#     docker build -t backshift-r-reference scripts/r
#     docker run --rm -v "$PWD":/work backshift-r-reference Rscript scripts/generate_r_accuracy_reference.R
#
# The series are simulated here with fixed seeds and stored in the output, so the tests do
# not depend on R's random number generator.

suppressPackageStartupMessages(library(forecast))

out <- "tests/it/r_accuracy_data.rs"

source("scripts/r/rust_literals.R")

lines <- c(
  "//! R reference vectors for the accuracy and cross-validation conformance tests.",
  "//!",
  sprintf("//! Generated by `scripts/generate_r_accuracy_reference.R` with %s and forecast %s.",
          R.version.string, as.character(packageVersion("forecast"))),
  "//! Do not edit by hand; rerun the script instead.",
  ""
)
emit <- function(...) lines <<- c(lines, ...)

set.seed(81)
t <- 1:96
y <- 100 + 0.3 * t + 8 * sin(2 * pi * (t - 1) / 12) + cumsum(rnorm(96, sd = 1.5))
train <- ts(y[1:84], frequency = 12)
test <- y[85:96]
fc <- snaive(train, h = 12)$mean + 0.5
a <- accuracy(fc, test)
scale <- mean(abs(diff(as.numeric(train), lag = 12)))
mase <- mean(abs(test - as.numeric(fc))) / scale

emit(
  "/// A training series (monthly), the values after it, a forecast of them, and",
  "/// `accuracy(forecast, test)`: ME, RMSE, MAE, MPE, MAPE, and MASE scaled by the training",
  "/// series' seasonal naive error, as `accuracy(forecast_object, test)` scales it.",
  "pub struct AccuracyCase {",
  "    /// The training values.",
  "    pub training: &'static [f64],",
  "    /// The season length.",
  "    pub period: usize,",
  "    /// The values the forecast is scored against.",
  "    pub test: &'static [f64],",
  "    /// The forecast.",
  "    pub forecast: &'static [f64],",
  "    /// ME, RMSE, MAE, MPE, MAPE and MASE.",
  "    pub metrics: [f64; 6],",
  "}",
  "",
  "/// The accuracy case.",
  "pub const ACCURACY: AccuracyCase = AccuracyCase {",
  sprintf("    training: %s,", rust_array(train)),
  "    period: 12,",
  sprintf("    test: %s,", rust_array(test)),
  sprintf("    forecast: %s,", rust_array(fc)),
  sprintf("    metrics: [%s],", paste(vapply(c(a[1, "ME"], a[1, "RMSE"], a[1, "MAE"], a[1, "MPE"], a[1, "MAPE"], mase), rust_float, ""), collapse = ", ")),
  "};",
  ""
)

# Rolling-origin errors of the naive forecast, as `tsCV`.
z <- as.numeric(y[1:40])
e <- tsCV(ts(z), rwf, h = 3, initial = 10)
emit(
  "/// `tsCV(values, rwf, h = 3, initial = 10)`: row `t` holds the errors of the naive",
  "/// forecasts from the first `t + 1` values, at horizons 1 to 3 (`NaN` where none exists).",
  "pub struct CrossValidationCase {",
  "    /// The series.",
  "    pub values: &'static [f64],",
  "    /// The errors, row-major: one row of 3 per value.",
  "    pub errors: &'static [f64],",
  "}",
  "",
  "/// The cross-validation case.",
  "pub const CROSS_VALIDATION: CrossValidationCase = CrossValidationCase {",
  sprintf("    values: %s,", rust_array(z)),
  sprintf("    errors: %s,", rust_array(t(e))),
  "};"
)

writeLines(lines, out)