1 #' Neighbors Forecaster
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 #' See 'details' section.
9 #' @format R6 class, inherits Forecaster
12 NeighborsForecaster = R6::R6Class("NeighborsForecaster",
16 predictShape = function(data, today, memory, horizon, ...)
18 # (re)initialize computed parameters
19 private$.params <- list("weights"=NA, "indices"=NA, "window"=NA)
21 # Do not forecast on days with NAs (TODO: softer condition...)
22 if (any(is.na(data$getCenteredSerie(today))))
25 # Determine indices of no-NAs days followed by no-NAs tomorrows
26 fdays = getNoNA2(data, max(today-memory,1), today-1)
29 local = ifelse(hasArg("local"), list(...)$local, TRUE) #same level + season?
30 simtype = ifelse(hasArg("simtype"), list(...)$simtype, "none") #or "endo", or "exo"
33 return ( private$.predictShapeAux(data,
34 fdays, today, horizon, local, list(...)$window, simtype, TRUE) )
37 # Indices of similar days for cross-validation; TODO: 20 = magic number
38 cv_days = getSimilarDaysIndices(today, data, limit=20, same_season=FALSE,
41 # Optimize h : h |--> sum of prediction errors on last N "similar" days
42 errorOnLastNdays = function(window, simtype)
46 for (i in seq_along(cv_days))
48 # mix_strategy is never used here (simtype != "mix"), therefore left blank
49 prediction = private$.predictShapeAux(data, fdays, cv_days[i], horizon, local,
50 window, simtype, FALSE)
51 if (!is.na(prediction[1]))
53 nb_jours = nb_jours + 1
55 mean((data$getSerie(cv_days[i]+1)[1:horizon] - prediction)^2)
58 return (error / nb_jours)
61 # TODO: 7 == magic number
62 if (simtype=="endo" || simtype=="mix")
64 best_window_endo = optimize(
65 errorOnLastNdays, c(0,7), simtype="endo")$minimum
67 if (simtype=="exo" || simtype=="mix")
69 best_window_exo = optimize(
70 errorOnLastNdays, c(0,7), simtype="exo")$minimum
74 if (simtype == "endo")
76 else if (simtype == "exo")
78 else if (simtype == "mix")
79 c(best_window_endo,best_window_exo)
80 else #none: value doesn't matter
83 return(private$.predictShapeAux(data, fdays, today, horizon, local,
84 best_window, simtype, TRUE))
88 # Precondition: "today" is full (no NAs)
89 .predictShapeAux = function(data, fdays, today, horizon, local, window, simtype,
92 fdays_cut = fdays[ fdays < today ]
93 if (length(fdays_cut) <= 1)
98 # Neighbors: days in "same season"; TODO: 60 == magic number...
99 fdays = getSimilarDaysIndices(today, data, limit=60, same_season=TRUE,
101 if (length(fdays) <= 1)
103 levelToday = data$getLevel(today)
104 distances = sapply(fdays, function(i) abs(data$getLevel(i)-levelToday))
105 #TODO: 2, 10, 3, 12 magic numbers here...
107 min_neighbs = min(10,length(fdays))
110 same_pollution = (distances <= dist_thresh)
111 nb_neighbs = sum(same_pollution)
112 if (nb_neighbs >= min_neighbs) #will eventually happen
114 dist_thresh = dist_thresh + 3
116 fdays = fdays[same_pollution]
118 if (nb_neighbs > max_neighbs)
120 # Keep only max_neighbs closest neighbors
122 sort(distances[same_pollution],index.return=TRUE)$ix[1:max_neighbs] ]
124 if (length(fdays) == 1) #the other extreme...
128 private$.params$weights <- 1
129 private$.params$indices <- fdays
130 private$.params$window <- 1
132 return ( data$getSerie(fdays[1])[1:horizon] )
136 fdays = fdays_cut #no conditioning
138 if (simtype == "endo" || simtype == "mix")
140 # Compute endogen similarities using given window
141 window_endo = ifelse(simtype=="mix", window[1], window)
143 # Distances from last observed day to days in the past
144 serieToday = data$getSerie(today)
145 distances2 = sapply(fdays, function(i) {
146 delta = serieToday - data$getSerie(i)
150 sd_dist = sd(distances2)
151 if (sd_dist < .25 * sqrt(.Machine$double.eps))
153 # warning("All computed distances are very close: stdev too small")
154 sd_dist = 1 #mostly for tests... FIXME:
156 simils_endo = exp(-distances2/(sd_dist*window_endo^2))
159 if (simtype == "exo" || simtype == "mix")
161 # Compute exogen similarities using given window
162 window_exo = ifelse(simtype=="mix", window[2], window)
164 M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
165 M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) )
166 for (i in seq_along(fdays))
167 M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
169 sigma = cov(M) #NOTE: robust covariance is way too slow
170 # TODO: 10 == magic number; more robust way == det, or always ginv()
172 if (length(fdays) > 10)
177 # Distances from last observed day to days in the past
178 distances2 = sapply(seq_along(fdays), function(i) {
179 delta = M[1,] - M[i+1,]
180 delta %*% sigma_inv %*% delta
183 sd_dist = sd(distances2)
184 if (sd_dist < .25 * sqrt(.Machine$double.eps))
186 # warning("All computed distances are very close: stdev too small")
187 sd_dist = 1 #mostly for tests... FIXME:
189 simils_exo = exp(-distances2/(sd_dist*window_exo^2))
193 if (simtype == "exo")
195 else if (simtype == "endo")
197 else if (simtype == "mix")
198 simils_endo * simils_exo
200 rep(1, length(fdays))
201 similarities = similarities / sum(similarities)
203 prediction = rep(0, horizon)
204 for (i in seq_along(fdays))
205 prediction = prediction + similarities[i] * data$getSerie(fdays[i]+1)[1:horizon]
209 private$.params$weights <- similarities
210 private$.params$indices <- fdays
211 private$.params$window <-
214 else if (simtype=="exo")
216 else if (simtype=="mix")
217 c(window_endo,window_exo)