adapt Bruno method into package, add 'operational' mode
[talweg.git] / pkg / R / Forecaster.R
CommitLineData
25b75559 1#' Forecaster
e030a6e3 2#'
102bcfda 3#' Forecaster (abstract class, implemented by all forecasters).
e030a6e3 4#'
102bcfda
BA
5#' A Forecaster object encapsulates parameters (which can be of various kinds, for
6#' example "Neighbors" method stores informations about the considered neighborhood for
7#' the current prediction task) and one main function: \code{predictSerie()}. This last
8#' function (by default) calls \code{predictShape()} to get a forecast of a centered
9#' serie, and then calls the "jump prediction" function -- see "field" section -- to
10#' adjust it based on the last observed values.
a66a84b5 11#'
4e821712 12#' @usage # Forecaster$new(pjump) #warning: predictShape() is unimplemented
689aa1d3 13#'
102bcfda
BA
14#' @field .params List of computed parameters (if applicable).
15#' @field .pjump Function: how to predict the jump at day interface? The arguments of
16#' this function are -- in this order:
17#' \itemize{
18#' \item data : object output of \code{getData()},
19#' \item today : index (integer or date) of the last known day in data,
20#' \item memory : number of days to use in the past (including today),
21#' \item horizon : number of time steps to predict,
22#' \item params : optimized parameters in the main method \code{predictShape()},
23#' \item ... : additional arguments.
24#' }
25#' .pjump returns an estimation of the jump after the last observed value.
25b75559 26#'
98e958ca
BA
27#' @section Methods:
28#' \describe{
29#' \item{\code{initialize(data, pjump)}}{
30#' Initialize a Forecaster object with a Data object and a jump prediction function.}
31#' \item{\code{predictSerie(today,memory,horizon,...)}}{
102bcfda
BA
32#' Predict a new serie of \code{horizon} values at day index \code{today} using
33#' \code{memory} days in the past.}
98e958ca 34#' \item{\code{predictShape(today,memory,horizon,...)}}{
102bcfda
BA
35#' Predict a new shape of \code{horizon} values at day index \code{today} using
36#' \code{memory} days in the past.}
98e958ca 37#' \item{\code{getParameters()}}{
546b0cb6
BA
38#' Return (internal) parameters.}
39#' }
40#'
102bcfda
BA
41#' @docType class
42#' @format R6 class
43#'
25b75559
BA
44Forecaster = R6::R6Class("Forecaster",
45 private = list(
a66a84b5 46 .params = list(),
a66a84b5 47 .pjump = NULL
e030a6e3 48 ),
25b75559 49 public = list(
98e958ca 50 initialize = function(pjump)
a66a84b5 51 {
a66a84b5
BA
52 private$.pjump <- pjump
53 invisible(self)
54 },
d2ab47a7 55 predictSerie = function(data, today, memory, predict_from, horizon, ...)
a66a84b5
BA
56 {
57 # Parameters (potentially) computed during shape prediction stage
d2ab47a7
BA
58 predicted_shape = self$predictShape(data,today,memory,predict_from,horizon,...)
59 predicted_delta = private$.pjump(data, today, memory, predict_from, horizon,
60 private$.params, ...)
61
62 # Predicted shape is aligned on the end of current day + jump
63 c( data$getSerie(today)[if (predict_from>=2) 1:(predict_from-1) else c()],
64 predicted_shape - predicted_shape[1] + predicted_delta +
65 ifelse(predict_from>=2,
66 data$getSerie(today)[predict_from-1], tail(data$getSerie(today-1),1)) )
a66a84b5 67 },
d2ab47a7 68 predictShape = function(data, today, memory, predict_from, horizon, ...)
5d83d815 69 NULL #empty default implementation: to implement in inherited classes
25b75559 70 ,
e030a6e3 71 getParameters = function()
a66a84b5 72 private$.params
e030a6e3
BA
73 )
74)