ea18bb6bbda5220ce975ba7c06682006a6a6d3cb
[talweg.git] / pkg / R / F_Neighbors.R
1 #' Neighbors Forecaster
2 #'
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.
5 #'
6 #' The main method is \code{predictShape()}, taking arguments data, today, memory,
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.
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>
15 #' 'exo' for a similarity based on exogenous variables only,<cr>
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 #' }
31 #'
32 #' @usage # NeighborsForecaster$new(pjump)
33 #'
34 #' @docType class
35 #' @format R6 class, inherits Forecaster
36 #' @aliases F_Neighbors
37 #'
38 NeighborsForecaster = R6::R6Class("NeighborsForecaster",
39 inherit = Forecaster,
40
41 public = list(
42 predictShape = function(data, today, memory, predict_from, horizon, ...)
43 {
44 # (re)initialize computed parameters
45 private$.params <- list("weights"=NA, "indices"=NA, "window"=NA)
46
47 # Do not forecast on days with NAs (TODO: softer condition...)
48 if (any(is.na(data$getSerie(today-1)))
49 || any(is.na(data$getSerie(today)[1:(predict_from-1)])))
50 {
51 return (NA)
52 }
53
54 # Determine indices of no-NAs days followed by no-NAs tomorrows
55 fdays = .getNoNA2(data, max(today-memory,1), today-2)
56
57 # Get optional args
58 local = ifelse(hasArg("local"), list(...)$local, TRUE) #same level + season?
59 simtype = ifelse(hasArg("simtype"), list(...)$simtype, "none") #or "endo", or "exo"
60 if (hasArg("window"))
61 {
62 return ( private$.predictShapeAux(data,
63 fdays, today, predict_from, horizon, local, list(...)$window, simtype, TRUE) )
64 }
65
66 # Indices of similar days for cross-validation; TODO: 20 = magic number
67 cv_days = getSimilarDaysIndices(today, data, limit=20, same_season=FALSE,
68 days_in=fdays)
69
70 # Optimize h : h |--> sum of prediction errors on last N "similar" days
71 errorOnLastNdays = function(window, simtype)
72 {
73 error = 0
74 nb_jours = 0
75 for (i in seq_along(cv_days))
76 {
77 # mix_strategy is never used here (simtype != "mix"), therefore left blank
78 prediction = private$.predictShapeAux(data, fdays, cv_days[i], predict_from,
79 horizon, local, window, simtype, FALSE)
80 if (!is.na(prediction[1]))
81 {
82 nb_jours = nb_jours + 1
83 error = error +
84 mean((data$getSerie(cv_days[i]+1)[predict_from:horizon] - prediction)^2)
85 }
86 }
87 return (error / nb_jours)
88 }
89
90 # TODO: 7 == magic number
91 if (simtype=="endo" || simtype=="mix")
92 {
93 best_window_endo = optimize(
94 errorOnLastNdays, c(0,7), simtype="endo")$minimum
95 }
96 if (simtype=="exo" || simtype=="mix")
97 {
98 best_window_exo = optimize(
99 errorOnLastNdays, c(0,7), simtype="exo")$minimum
100 }
101
102 best_window =
103 if (simtype == "endo")
104 best_window_endo
105 else if (simtype == "exo")
106 best_window_exo
107 else if (simtype == "mix")
108 c(best_window_endo,best_window_exo)
109 else #none: value doesn't matter
110 1
111
112 return( private$.predictShapeAux(data, fdays, today, predict_from, horizon, local,
113 best_window, simtype, TRUE) )
114 }
115 ),
116 private = list(
117 # Precondition: "today" is full (no NAs)
118 .predictShapeAux = function(data, fdays, today, predict_from, horizon, local, window,
119 simtype, final_call)
120 {
121 fdays_cut = fdays[ fdays < today ]
122 if (length(fdays_cut) <= 1)
123 return (NA)
124
125 if (local)
126 {
127 # TODO: 60 == magic number
128 fdays = getSimilarDaysIndices(today, data, limit=60, same_season=TRUE,
129 days_in=fdays_cut)
130 if (length(fdays) <= 1)
131 return (NA)
132 # TODO: 10, 12 == magic numbers
133 fdays = .getConstrainedNeighbs(today,data,fdays,min_neighbs=10,max_neighbs=12)
134 if (length(fdays) == 1)
135 {
136 if (final_call)
137 {
138 private$.params$weights <- 1
139 private$.params$indices <- fdays
140 private$.params$window <- 1
141 }
142 return ( data$getSerie(fdays[1]+1)[predict_from:horizon] )
143 }
144 }
145 else
146 fdays = fdays_cut #no conditioning
147
148 if (simtype == "endo" || simtype == "mix")
149 {
150 # Compute endogen similarities using given window
151 window_endo = ifelse(simtype=="mix", window[1], window)
152
153 # Distances from last observed day to days in the past
154 lastSerie = c( data$getSerie(today-1), data$getSerie(today)[1:(predict_from-1)] )
155 distances2 = sapply(fdays, function(i) {
156 delta = lastSerie - c(data$getSerie(i),data$getSerie(i+1)[1:(predict_from-1)])
157 sqrt(mean(delta^2))
158 })
159
160 simils_endo <- .computeSimils(distances2, window_endo)
161 }
162
163 if (simtype == "exo" || simtype == "mix")
164 {
165 # Compute exogen similarities using given window
166 window_exo = ifelse(simtype=="mix", window[2], window)
167
168 M = matrix( ncol=1+length(fdays), nrow=1+length(data$getExo(1)) )
169 M[,1] = c( data$getLevelHat(today), as.double(data$getExoHat(today)) )
170 for (i in seq_along(fdays))
171 M[,i+1] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
172
173 sigma = cov(t(M)) #NOTE: robust covariance is way too slow
174 # TODO: 10 == magic number; more robust way == det, or always ginv()
175 sigma_inv =
176 if (length(fdays) > 10)
177 solve(sigma)
178 else
179 MASS::ginv(sigma)
180
181 # Distances from last observed day to days in the past
182 distances2 = sapply(seq_along(fdays), function(i) {
183 delta = M[,1] - M[,i+1]
184 delta %*% sigma_inv %*% delta
185 })
186
187 simils_exo <- .computeSimils(distances2, window_exo)
188 }
189
190 similarities =
191 if (simtype == "exo")
192 simils_exo
193 else if (simtype == "endo")
194 simils_endo
195 else if (simtype == "mix")
196 simils_endo * simils_exo
197 else #none
198 rep(1, length(fdays))
199 similarities = similarities / sum(similarities)
200
201 prediction = rep(0, horizon-predict_from+1)
202 for (i in seq_along(fdays))
203 {
204 prediction = prediction +
205 similarities[i] * data$getSerie(fdays[i]+1)[predict_from:horizon]
206 }
207
208 if (final_call)
209 {
210 private$.params$weights <- similarities
211 private$.params$indices <- fdays
212 private$.params$window <-
213 if (simtype=="endo")
214 window_endo
215 else if (simtype=="exo")
216 window_exo
217 else if (simtype=="mix")
218 c(window_endo,window_exo)
219 else #none
220 1
221 }
222
223 return (prediction)
224 }
225 )
226 )
227
228 # getConstrainedNeighbs
229 #
230 # Get indices of neighbors of similar pollution level (among same season + day type).
231 #
232 # @param today Index of current day
233 # @param data Object of class Data
234 # @param fdays Current set of "first days" (no-NA pairs)
235 # @param min_neighbs Minimum number of points in a neighborhood
236 # @param max_neighbs Maximum number of points in a neighborhood
237 #
238 .getConstrainedNeighbs = function(today, data, fdays, min_neighbs=10, max_neighbs=12)
239 {
240 levelToday = data$getLevelHat(today)
241 levelYersteday = data$getLevel(today-1)
242 distances = sapply(fdays, function(i) {
243 sqrt((data$getLevel(i)-levelYersteday)^2 + (data$getLevel(i+1)-levelToday)^2)
244 })
245 #TODO: 1, +1, +3 : magic numbers
246 dist_thresh = 1
247 min_neighbs = min(min_neighbs,length(fdays))
248 repeat
249 {
250 same_pollution = (distances <= dist_thresh)
251 nb_neighbs = sum(same_pollution)
252 if (nb_neighbs >= min_neighbs) #will eventually happen
253 break
254 dist_thresh = dist_thresh + ifelse(dist_thresh>1,3,1)
255 }
256 fdays = fdays[same_pollution]
257 max_neighbs = 12
258 if (nb_neighbs > max_neighbs)
259 {
260 # Keep only max_neighbs closest neighbors
261 fdays = fdays[ order(distances[same_pollution])[1:max_neighbs] ]
262 }
263 fdays
264 }
265
266 # compute similarities
267 #
268 # Apply the gaussian kernel on computed squared distances.
269 #
270 # @param distances2 Squared distances
271 # @param window Window parameter for the kernel
272 #
273 .computeSimils <- function(distances2, window)
274 {
275 sd_dist = sd(distances2)
276 if (sd_dist < .25 * sqrt(.Machine$double.eps))
277 {
278 # warning("All computed distances are very close: stdev too small")
279 sd_dist = 1 #mostly for tests... FIXME:
280 }
281 exp(-distances2/(sd_dist*window^2))
282 }