1 #' @include Forecaster.R
3 #' Neighbors2 Forecaster
5 #' Predict tomorrow as a weighted combination of "futures of the past" days.
6 #' Inherits \code{\link{Forecaster}}
8 Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
12 predictShape = function(data, today, memory, horizon, ...)
14 # (re)initialize computed parameters
15 private$.params <- list("weights"=NA, "indices"=NA, "window"=NA)
17 # Do not forecast on days with NAs (TODO: softer condition...)
18 if (any(is.na(data$getCenteredSerie(today))))
21 # Determine indices of no-NAs days followed by no-NAs tomorrows
22 fdays = getNoNA2(data, max(today-memory,1), today-1)
25 kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
28 return ( private$.predictShapeAux(data,
29 fdays, today, horizon, list(...)$h_window, kernel, TRUE) )
33 # Indices of similar days for cross-validation; TODO: 45 = magic number
34 # TODO: ici faut une sorte de "same_season==TRUE" --> mois similaires epandage
35 sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE)
38 # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days
39 errorOnLastNdays = function(h, kernel)
43 for (i in intersect(fdays,sdays))
45 # mix_strategy is never used here (simtype != "mix"), therefore left blank
46 prediction = private$.predictShapeAux(data, fdays, i, horizon, h, kernel, FALSE)
47 if (!is.na(prediction[1]))
49 nb_jours = nb_jours + 1
51 mean((data$getSerie(i+1)[1:horizon] - prediction)^2)
54 return (error / nb_jours)
57 # h :: only for endo in this variation
58 h_best_endo = optimize(errorOnLastNdays, c(0,10), kernel=kernel)$minimum
60 return (private$.predictShapeAux(data, fdays, today, horizon, h_best, kernel, TRUE))
64 # Precondition: "today" is full (no NAs)
65 .predictShapeAux = function(data, fdays, today, horizon, h, kernel, final_call)
67 fdays = fdays[ fdays < today ]
68 # TODO: 3 = magic number
69 if (length(fdays) < 3)
72 # ENDO:: Distances from last observed day to days in the past
73 distances2 = rep(NA, length(fdays))
74 for (i in seq_along(fdays))
76 delta = data$getSerie(today) - data$getSerie(fdays[i])
77 # Require at least half of non-NA common values to compute the distance
78 if ( !any( is.na(delta) ) )
79 distances2[i] = mean(delta^2)
82 sd_dist = sd(distances2)
83 if (sd_dist < .Machine$double.eps)
85 # warning("All computed distances are very close: stdev too small")
86 sd_dist = 1 #mostly for tests... FIXME:
90 exp(-distances2/(sd_dist*h_endo^2))
94 u = 1 - distances2/(sd_dist*h_endo^2)
99 # EXOGENS: distances computations are enough
100 # TODO: search among similar concentrations....... at this stage ?!
101 M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
102 M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) )
103 for (i in seq_along(fdays))
104 M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
106 sigma = cov(M) #NOTE: robust covariance is way too slow
107 sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed?
109 # Distances from last observed day to days in the past
110 distances2 = rep(NA, nrow(M)-1)
113 delta = M[1,] - M[i,]
114 distances2[i-1] = delta %*% sigma_inv %*% delta
117 ppv <- sort(distances2, index.return=TRUE)$ix[1:10] #..............
121 if (simtype == "exo")
123 else if (simtype == "endo")
126 simils_endo * simils_exo
128 prediction = rep(0, horizon)
129 for (i in seq_along(fdays))
130 prediction = prediction + similarities[i] * data$getSerie(fdays[i]+1)[1:horizon]
131 prediction = prediction / sum(similarities, na.rm=TRUE)
135 private$.params$weights <- similarities
136 private$.params$indices <- fdays
137 private$.params$window <-
140 else if (simtype=="exo")