'update'
[talweg.git] / pkg / R / computeForecast.R
index 0585e1b..e1b29b6 100644 (file)
-#' @title get Forecast
+#' Compute forecast
 #'
-#' @description Predict time-series curves for the selected days indices (lines in data).
+#' Predict time-series curves ("today" from predict_from to horizon) at the selected days
+#' indices ("today" from 1am to predict_from-1). This function just runs a loop over all
+#' requested indices, and stores the individual forecasts into a Forecast object.
 #'
-#' @param data Dataset, object of type \code{Data} output of \code{getData}
-#' @param indices Days indices where to forecast (the day after)
-#' @param forecaster Name of the main forcaster
-#' \itemize{
-#'   \item Persistence : use values of last (similar, next) day
-#'   \item Neighbors : use values from the k closest neighbors' tomorrows
-#'   \item Average : global average of all the (similar) "tomorrow of past"
-#'   \item Zero : just output 0 (benchmarking purpose)
-#' }
-#' @param pjump How to predict the jump at the interface between two days ?
-#' \itemize{
-#'   \item Persistence : use last (similar) day values
-#'   \item Neighbors: re-use the weights optimized in corresponding forecaster
-#'   \item Zero: just output 0 (no adjustment)
-#' }
-#' @param memory Data depth (in days) to be used for prediction
-#' @param horizon Number of time steps to predict
-#' @param ... Additional parameters for the forecasting models
+#' @param data Object of class Data, output of \code{getData()}.
+#' @param indices Indices where to forecast (the day after); integers relative to the
+#'   beginning of data, or (convertible to) Date objects.
+#' @param forecaster Name of the main forecaster; more details: ?F_<forecastername>
+#'   \itemize{
+#'     \item Persistence : use last (similar) day
+#'     \item Neighbors : weighted similar days
+#'     \item Average : average curve of all same day-in-week
+#'     \item Zero : just output 0 (benchmarking purpose)
+#'   }
+#' @param pjump Function to predict the jump at the interface between two days;
+#'   more details: ?J_<functionname>
+#'   \itemize{
+#'     \item Persistence : use last (similar) day
+#'     \item Neighbors: re-use the weights from F_Neighbors
+#'     \item Zero: just output 0 (no adjustment)
+#'   }
+#'   If pjump=NULL, then no adjustment is performed (output of \code{predictShape()} is
+#'   used directly).
+#' @param predict_from First time step to predict.
+#' @param memory Data depth (in days) to be used for prediction.
+#' @param horizon Last time step to predict.
+#' @param ncores Number of cores for parallel execution (1 to disable).
+#' @param verbose TRUE to print basic traces (runs beginnings)
+#' @param ... Additional parameters for the forecasting models.
 #'
-#' @return A list with the following items
-#' \itemize{
-#'   \item serie: forecasted serie
-#'   \item params: corresponding list of parameters (weights, neighbors...)
-#'   \item index: corresponding index in data object
-#' }
+#' @return An object of class Forecast
 #'
 #' @examples
-#' ts_data = system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
-#' exo_data = system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
-#' data = getData(ts_data, exo_data, input_tz = "Europe/Paris",
-#'   working_tz="Europe/Paris", predict_at=7)
-#' pred = computeForecast(data, 2200:2230, "Persistence", "Persistence", 500, 12)
-#' \dontrun{#Sketch for real-time mode:
-#' data = new("Data", ...)
-#' forecaster = new(..., data=data)
+#' ts_data <- system.file("extdata","pm10_mesures_H_loc.csv",package="talweg")
+#' exo_data <- system.file("extdata","meteo_extra_noNAs.csv",package="talweg")
+#' data <- getData(ts_data, exo_data, limit=200)
+#' pred <- computeForecast(data, 100:130, "Persistence", "Zero",
+#'   predict_from=8, memory=50, horizon=12, ncores=1)
+#' \dontrun{
+#' #Sketch for real-time mode:
+#' data <- Data$new()
+#' forecaster <- MyForecaster$new(myJumpPredictFunc)
 #' repeat {
-#'   data$append(some_new_data)
-#'   pred = forecaster$predict(data$getSize(), ...)
+#'   # As soon as daily predictions are available:
+#'   data$append(
+#'     level_hat=predicted_level,
+#'     exo_hat=predicted_exogenous)
+#'   # When a day ends:
+#'   data$append(
+#'     level=observed_level,
+#'     exo=observed_exogenous)
+#'   # And, at every hour:
+#'   data$append(
+#'     time=current_hour,
+#'     value=current_PM10)
+#'   # Finally, a bit before predict_from hour:
+#'   pred <- forecaster$predictSerie(data, data$getSize(), ...)
 #'   #do_something_with_pred
-#' }}
+#' } }
 #' @export
-computeForecast = function(data, indices, forecaster, pjump,
-       memory=Inf, horizon=data$getStdHorizon(), ...)
+computeForecast = function(data, indices, forecaster, pjump, predict_from,
+       memory=Inf, horizon=length(data$getSerie(1)), ncores=3, verbose=FALSE, ...)
 {
        # (basic) Arguments sanity checks
+       predict_from = as.integer(predict_from)[1]
+       if (! predict_from %in% 1:length(data$getSerie(1)))
+               stop("predict_from in [1,24] (hours)")
+       if (hasArg("opera") && !list(...)$opera && memory < Inf)
+               memory <- Inf #finite memory in training mode makes no sense
        horizon = as.integer(horizon)[1]
-       if (horizon<=0 || horizon>length(data$getCenteredSerie(2)))
-               stop("Horizon too short or too long")
-       indices = sapply( seq_along(indices), function(i) dateIndexToInteger(indices[i], data) )
-       if (any(indices<=0 | indices>data$getSize()))
+       if (horizon<=predict_from || horizon>length(data$getSerie(1)))
+               stop("Horizon in [predict_from+1,24] (hours)")
+       integer_indices = sapply(indices, function(i) dateIndexToInteger(i,data))
+       if (any(integer_indices<=0 | integer_indices>data$getSize()))
                stop("Indices out of range")
-       indices = sapply(indices, dateIndexToInteger, data)
        if (!is.character(forecaster))
-               stop("forecaster (name) should be of class character") #pjump could be NULL
+               stop("forecaster (name): character")
+       if (!is.null(pjump) && !is.character(pjump))
+               stop("pjump (function): character or NULL")
 
-       pred = Forecast$new()
-       forecaster_class_name = getFromNamespace(paste(forecaster,"Forecaster",sep=""), "talweg")
-       forecaster = forecaster_class_name$new(data=data,
-               pjump = getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg"))
-       for (today in indices)
+       pred = Forecast$new( sapply(indices, function(i) integerIndexToDate(i,data)) )
+       forecaster_class_name = getFromNamespace(
+               paste(forecaster,"Forecaster",sep=""), "talweg")
+
+       if (!is.null(pjump))
+               pjump <- getFromNamespace(paste("get",pjump,"JumpPredict",sep=""), "talweg")
+       forecaster = forecaster_class_name$new(pjump)
+
+       computeOneForecast <- function(i)
+       {
+               if (verbose)
+                       print(paste("Index",i))
+               list(
+                       "forecast" = forecaster$predictSerie(data,i,memory,predict_from,horizon,...),
+                       "params" = forecaster$getParameters(),
+                       "index" = i )
+       }
+
+       p <-
+               if (ncores > 1 && requireNamespace("parallel",quietly=TRUE))
+                       parallel::mclapply(integer_indices, computeOneForecast, mc.cores=ncores)
+               else
+                       lapply(integer_indices, computeOneForecast)
+
+       # TODO: find a way to fill pred in //...
+       for (i in seq_along(integer_indices))
        {
                pred$append(
-                       new_serie = forecaster$predictSerie(today, memory, horizon, ...),
-                       new_params = forecaster$getParameters(),
-                       new_index = today
+                       forecast = p[[i]]$forecast,
+                       params = p[[i]]$params,
+                       index_in_data = p[[i]]$index
                )
        }
        pred