TODO: unit tests for simil days
[talweg.git] / pkg / R / J_Neighbors.R
... / ...
CommitLineData
1#' getNeighborsJumpPredict
2#'
3#' Apply optimized weights on gaps observed on selected neighbors.
4#' This jump prediction method can only be used in conjunction with the Neighbors
5#' Forecaster, because it makes use of the optimized parameters to re-apply the weights
6#' on the jumps observed at days interfaces of the past neighbors.
7#'
8#' @inheritParams computeForecast
9#' @inheritParams getZeroJumpPredict
10#'
11#' @aliases J_Neighbors
12#'
13getNeighborsJumpPredict = function(data, today, memory, horizon, params, ...)
14{
15 first_day = max(1, today-memory)
16 filter = (params$indices >= first_day)
17 indices = params$indices[filter]
18 weights = params$weights[filter]
19
20 if (any(is.na(weights) | is.na(indices)))
21 return (NA)
22
23 gaps = sapply(indices, function(i) {
24 head( data$getSerie(i+1), 1) - tail( data$getSerie(i), 1)
25 })
26 scal_product = weights * gaps
27 norm_fact = sum( weights[!is.na(scal_product)] )
28 sum(scal_product, na.rm=TRUE) / norm_fact
29}