investigations on Bruno vs. talweg: a shift overved, and neighbors differ (a bit...
[talweg.git] / pkg / R / F_Neighbors.R
CommitLineData
25b75559 1#' Neighbors Forecaster
3d69ff21 2#'
c4c329f6
BA
3#' Predict next serie as a weighted combination of "futures of the past" days,
4#' where days in the past are chosen and weighted according to some similarity measures.
c4c329f6 5#'
102bcfda 6#' The main method is \code{predictShape()}, taking arguments data, today, memory,
d2ab47a7
BA
7#' predict_from, horizon respectively for the dataset (object output of
8#' \code{getData()}), the current index, the data depth (in days), the first predicted
9#' hour and the last predicted hour.
102bcfda
BA
10#' In addition, optional arguments can be passed:
11#' \itemize{
12#' \item local : TRUE (default) to constrain neighbors to be "same days within same
13#' season"
14#' \item simtype : 'endo' for a similarity based on the series only,<cr>
d2ab47a7 15#' 'exo' for a similarity based on exogenous variables only,<cr>
102bcfda
BA
16#' 'mix' for the product of 'endo' and 'exo',<cr>
17#' 'none' (default) to apply a simple average: no computed weights
18#' \item window : A window for similarities computations; override cross-validation
19#' window estimation.
20#' }
21#' The method is summarized as follows:
22#' \enumerate{
23#' \item Determine N (=20) recent days without missing values, and followed by a
24#' tomorrow also without missing values.
25#' \item Optimize the window parameters (if relevant) on the N chosen days.
26#' \item Considering the optimized window, compute the neighbors (with locality
27#' constraint or not), compute their similarities -- using a gaussian kernel if
28#' simtype != "none" -- and average accordingly the "tomorrows of neigbors" to
29#' obtain the final prediction.
30#' }
c4c329f6 31#'
4e821712 32#' @usage # NeighborsForecaster$new(pjump)
689aa1d3 33#'
102bcfda 34#' @docType class
c4c329f6 35#' @format R6 class, inherits Forecaster
3ddf1c12 36#' @aliases F_Neighbors
546b0cb6 37#'
25b75559 38NeighborsForecaster = R6::R6Class("NeighborsForecaster",
a66a84b5 39 inherit = Forecaster,
25b75559
BA
40
41 public = list(
d2ab47a7 42 predictShape = function(data, today, memory, predict_from, horizon, ...)
3d69ff21
BA
43 {
44 # (re)initialize computed parameters
a66a84b5 45 private$.params <- list("weights"=NA, "indices"=NA, "window"=NA)
3d69ff21 46
a5a3a294 47 # Do not forecast on days with NAs (TODO: softer condition...)
cf3bb001
BA
48 if (any(is.na(data$getSerie(today-1))) ||
49 (predict_from>=2 && any(is.na(data$getSerie(today)[1:(predict_from-1)]))))
d2ab47a7 50 {
a5a3a294 51 return (NA)
d2ab47a7 52 }
a5a3a294 53
f17665c7 54 # Get optional args
445e7bbc
BA
55 local = ifelse(hasArg("local"), list(...)$local, TRUE) #same level + season?
56 simtype = ifelse(hasArg("simtype"), list(...)$simtype, "none") #or "endo", or "exo"
638f27f4
BA
57 opera = ifelse(hasArg("opera"), list(...)$opera, FALSE) #operational mode?
58
59 # Determine indices of no-NAs days preceded by no-NAs yerstedays
60 tdays = .getNoNA2(data, max(today-memory,2), ifelse(opera,today-1,data$getSize()))
61 if (!opera)
62 tdays = setdiff(tdays, today) #always exclude current day
63
dca259e4
BA
64 # Shortcut if window is known or local==TRUE && simtype==none
65 if (hasArg("window") || (local && simtype=="none"))
a66a84b5 66 {
638f27f4
BA
67 return ( private$.predictShapeAux(data, tdays, today, predict_from, horizon,
68 local, list(...)$window, simtype, opera, TRUE) )
a66a84b5 69 }
3d69ff21 70
6774e53d 71 # Indices of similar days for cross-validation; TODO: 20 = magic number
aa059de7 72 cv_days = getSimilarDaysIndices(today, data, limit=20, same_season=FALSE,
638f27f4 73 days_in=tdays, operational=opera)
5e838b3e 74
445e7bbc 75 # Optimize h : h |--> sum of prediction errors on last N "similar" days
aa059de7 76 errorOnLastNdays = function(window, simtype)
3d69ff21
BA
77 {
78 error = 0
79 nb_jours = 0
5e838b3e 80 for (i in seq_along(cv_days))
3d69ff21 81 {
f17665c7 82 # mix_strategy is never used here (simtype != "mix"), therefore left blank
cf3bb001 83 prediction = private$.predictShapeAux(data, tdays, cv_days[i], predict_from,
638f27f4 84 horizon, local, window, simtype, opera, FALSE)
f17665c7 85 if (!is.na(prediction[1]))
3d69ff21
BA
86 {
87 nb_jours = nb_jours + 1
af3b84f4 88 error = error +
cf3bb001 89 mean((data$getSerie(cv_days[i])[predict_from:horizon] - prediction)^2)
3d69ff21
BA
90 }
91 }
92 return (error / nb_jours)
93 }
94
445e7bbc 95 # TODO: 7 == magic number
eef54517 96 if (simtype=="endo" || simtype=="mix")
af3b84f4 97 {
aa059de7
BA
98 best_window_endo = optimize(
99 errorOnLastNdays, c(0,7), simtype="endo")$minimum
af3b84f4 100 }
eef54517 101 if (simtype=="exo" || simtype=="mix")
af3b84f4 102 {
eef54517
BA
103 best_window_exo = optimize(
104 errorOnLastNdays, c(0,7), simtype="exo")$minimum
3d69ff21 105 }
eef54517
BA
106
107 best_window =
108 if (simtype == "endo")
109 best_window_endo
110 else if (simtype == "exo")
111 best_window_exo
112 else if (simtype == "mix")
113 c(best_window_endo,best_window_exo)
114 else #none: value doesn't matter
115 1
116
cf3bb001 117 return( private$.predictShapeAux(data, tdays, today, predict_from, horizon, local,
638f27f4 118 best_window, simtype, opera, TRUE) )
25b75559
BA
119 }
120 ),
121 private = list(
638f27f4 122 # Precondition: "yersteday until predict_from-1" is full (no NAs)
cf3bb001 123 .predictShapeAux = function(data, tdays, today, predict_from, horizon, local, window,
638f27f4 124 simtype, opera, final_call)
3d69ff21 125 {
638f27f4
BA
126 tdays_cut = tdays[ tdays != today ]
127 if (length(tdays_cut) == 0)
3d69ff21
BA
128 return (NA)
129
aa059de7
BA
130 if (local)
131 {
dca259e4
BA
132 # limit=Inf to not censor any day (TODO: finite limit? 60?)
133 tdays = getSimilarDaysIndices(today, data, limit=Inf, same_season=TRUE,
638f27f4
BA
134 days_in=tdays_cut, operational=opera)
135# if (length(tdays) <= 1)
136# return (NA)
137 # TODO: 10 == magic number
138 tdays = .getConstrainedNeighbs(today, data, tdays, min_neighbs=10)
cf3bb001 139 if (length(tdays) == 1)
aa059de7
BA
140 {
141 if (final_call)
142 {
143 private$.params$weights <- 1
cf3bb001 144 private$.params$indices <- tdays
aa059de7
BA
145 private$.params$window <- 1
146 }
cf3bb001 147 return ( data$getSerie(tdays[1])[predict_from:horizon] )
aa059de7 148 }
5037d6d0 149 max_neighbs = 10 #TODO: 12 = arbitrary number
dca259e4
BA
150 if (length(tdays) > max_neighbs)
151 {
152 distances2 <- .computeDistsEndo(data, today, tdays, predict_from)
153 ordering <- order(distances2)
154 tdays <- tdays[ ordering[1:max_neighbs] ]
5037d6d0
BA
155
156 print("VVVVV")
157 print(sort(distances2)[1:max_neighbs])
158 print(integerIndexToDate(today,data))
159 print(lapply(tdays,function(i) integerIndexToDate(i,data)))
160 print(rbind(data$getSeries(tdays-1), data$getSeries(tdays)))
dca259e4 161 }
aa059de7
BA
162 }
163 else
cf3bb001 164 tdays = tdays_cut #no conditioning
aa059de7 165
445e7bbc 166 if (simtype == "endo" || simtype == "mix")
3d69ff21 167 {
aa059de7
BA
168 # Compute endogen similarities using given window
169 window_endo = ifelse(simtype=="mix", window[1], window)
3d69ff21 170
638f27f4 171 # Distances from last observed day to selected days in the past
dca259e4 172 # TODO: redundant computation if local==TRUE
638f27f4
BA
173 distances2 <- .computeDistsEndo(data, today, tdays, predict_from)
174
3ddf1c12 175 simils_endo <- .computeSimils(distances2, window_endo)
3d69ff21
BA
176 }
177
445e7bbc 178 if (simtype == "exo" || simtype == "mix")
3d69ff21 179 {
aa059de7 180 # Compute exogen similarities using given window
445e7bbc 181 window_exo = ifelse(simtype=="mix", window[2], window)
3d69ff21 182
638f27f4 183 distances2 <- .computeDistsExo(data, today, tdays)
3d69ff21 184
3ddf1c12 185 simils_exo <- .computeSimils(distances2, window_exo)
3d69ff21
BA
186 }
187
3d69ff21 188 similarities =
f17665c7 189 if (simtype == "exo")
3d69ff21 190 simils_exo
f17665c7
BA
191 else if (simtype == "endo")
192 simils_endo
445e7bbc 193 else if (simtype == "mix")
f17665c7 194 simils_endo * simils_exo
445e7bbc 195 else #none
cf3bb001 196 rep(1, length(tdays))
ea5c7e56 197 similarities = similarities / sum(similarities)
3d69ff21 198
d2ab47a7 199 prediction = rep(0, horizon-predict_from+1)
cf3bb001 200 for (i in seq_along(tdays))
d2ab47a7
BA
201 {
202 prediction = prediction +
cf3bb001 203 similarities[i] * data$getSerie(tdays[i])[predict_from:horizon]
d2ab47a7 204 }
99f83c9a 205
3d69ff21
BA
206 if (final_call)
207 {
a66a84b5 208 private$.params$weights <- similarities
cf3bb001 209 private$.params$indices <- tdays
a66a84b5 210 private$.params$window <-
546b0cb6 211 if (simtype=="endo")
aa059de7 212 window_endo
546b0cb6 213 else if (simtype=="exo")
aa059de7 214 window_exo
eef54517 215 else if (simtype=="mix")
aa059de7 216 c(window_endo,window_exo)
eef54517
BA
217 else #none
218 1
3d69ff21 219 }
99f83c9a 220
3d69ff21
BA
221 return (prediction)
222 }
223 )
224)
3ddf1c12 225
689aa1d3
BA
226# getConstrainedNeighbs
227#
228# Get indices of neighbors of similar pollution level (among same season + day type).
229#
230# @param today Index of current day
231# @param data Object of class Data
cf3bb001 232# @param tdays Current set of "second days" (no-NA pairs)
689aa1d3
BA
233# @param min_neighbs Minimum number of points in a neighborhood
234# @param max_neighbs Maximum number of points in a neighborhood
235#
638f27f4 236.getConstrainedNeighbs = function(today, data, tdays, min_neighbs=10)
3ddf1c12 237{
d2ab47a7 238 levelToday = data$getLevelHat(today)
638f27f4 239# levelYersteday = data$getLevel(today-1)
cf3bb001 240 distances = sapply(tdays, function(i) {
638f27f4
BA
241# sqrt((data$getLevel(i-1)-levelYersteday)^2 + (data$getLevel(i)-levelToday)^2)
242 abs(data$getLevel(i)-levelToday)
d2ab47a7
BA
243 })
244 #TODO: 1, +1, +3 : magic numbers
245 dist_thresh = 1
cf3bb001 246 min_neighbs = min(min_neighbs,length(tdays))
3ddf1c12
BA
247 repeat
248 {
249 same_pollution = (distances <= dist_thresh)
250 nb_neighbs = sum(same_pollution)
251 if (nb_neighbs >= min_neighbs) #will eventually happen
252 break
d2ab47a7 253 dist_thresh = dist_thresh + ifelse(dist_thresh>1,3,1)
3ddf1c12 254 }
cf3bb001 255 tdays = tdays[same_pollution]
638f27f4
BA
256# max_neighbs = 12
257# if (nb_neighbs > max_neighbs)
258# {
259# # Keep only max_neighbs closest neighbors
260# tdays = tdays[ order(distances[same_pollution])[1:max_neighbs] ]
261# }
cf3bb001 262 tdays
3ddf1c12
BA
263}
264
689aa1d3
BA
265# compute similarities
266#
267# Apply the gaussian kernel on computed squared distances.
268#
269# @param distances2 Squared distances
270# @param window Window parameter for the kernel
271#
3ddf1c12
BA
272.computeSimils <- function(distances2, window)
273{
274 sd_dist = sd(distances2)
275 if (sd_dist < .25 * sqrt(.Machine$double.eps))
276 {
277# warning("All computed distances are very close: stdev too small")
278 sd_dist = 1 #mostly for tests... FIXME:
279 }
280 exp(-distances2/(sd_dist*window^2))
281}
638f27f4
BA
282
283.computeDistsEndo <- function(data, today, tdays, predict_from)
284{
285 lastSerie = c( data$getSerie(today-1),
286 data$getSerie(today)[if (predict_from>=2) 1:(predict_from-1) else c()] )
287 sapply(tdays, function(i) {
288 delta = lastSerie - c(data$getSerie(i-1),
289 data$getSerie(i)[if (predict_from>=2) 1:(predict_from-1) else c()])
5037d6d0
BA
290# sqrt(mean(delta^2))
291 sqrt(sum(delta^2))
638f27f4
BA
292 })
293}
294
295.computeDistsExo <- function(data, today, tdays)
296{
297 M = matrix( ncol=1+length(tdays), nrow=1+length(data$getExo(1)) )
298 M[,1] = c( data$getLevelHat(today), as.double(data$getExoHat(today)) )
299 for (i in seq_along(tdays))
300 M[,i+1] = c( data$getLevel(tdays[i]), as.double(data$getExo(tdays[i])) )
301
302 sigma = cov(t(M)) #NOTE: robust covariance is way too slow
303 # TODO: 10 == magic number; more robust way == det, or always ginv()
304 sigma_inv =
305 if (length(tdays) > 10)
306 solve(sigma)
307 else
308 MASS::ginv(sigma)
309
310 # Distances from last observed day to days in the past
311 sapply(seq_along(tdays), function(i) {
312 delta = M[,1] - M[,i+1]
313 delta %*% sigma_inv %*% delta
314 })
315}