1 #' @include Forecaster.R
3 #' Neighbors Forecaster
5 #' Predict tomorrow as a weighted combination of "futures of the past" days.
6 #' Inherits \code{\link{Forecaster}}
8 NeighborsForecaster = R6::R6Class("NeighborsForecaster",
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 simtype = ifelse(hasArg("simtype"), list(...)$simtype, "mix") #or "endo", or "exo"
26 kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
29 return ( private$.predictShapeAux(data,
30 fdays, today, horizon, list(...)$h_window, kernel, simtype, TRUE) )
33 # Indices of similar days for cross-validation; TODO: 45 = magic number
34 sdays = getSimilarDaysIndices(today, data, limit=45, same_season=FALSE)
36 cv_days = intersect(fdays,sdays)
37 # Limit to 20 most recent matching days (TODO: 20 == magic number)
38 cv_days = sort(cv_days,decreasing=TRUE)[1:min(20,length(cv_days))]
40 # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days
41 errorOnLastNdays = function(h, kernel, simtype)
45 for (i in seq_along(cv_days))
47 # mix_strategy is never used here (simtype != "mix"), therefore left blank
48 prediction = private$.predictShapeAux(data,
49 fdays, cv_days[i], horizon, h, kernel, simtype, FALSE)
50 if (!is.na(prediction[1]))
52 nb_jours = nb_jours + 1
54 mean((data$getCenteredSerie(cv_days[i]+1)[1:horizon] - prediction)^2)
57 return (error / nb_jours)
60 if (simtype != "endo")
62 h_best_exo = optimize(
63 errorOnLastNdays, c(0,10), kernel=kernel, simtype="exo")$minimum
67 h_best_endo = optimize(
68 errorOnLastNdays, c(0,10), kernel=kernel, simtype="endo")$minimum
71 if (simtype == "endo")
73 return (private$.predictShapeAux(data,
74 fdays, today, horizon, h_best_endo, kernel, "endo", TRUE))
78 return (private$.predictShapeAux(data,
79 fdays, today, horizon, h_best_exo, kernel, "exo", TRUE))
83 h_best_mix = c(h_best_endo,h_best_exo)
84 return(private$.predictShapeAux(data,
85 fdays, today, horizon, h_best_mix, kernel, "mix", TRUE))
90 # Precondition: "today" is full (no NAs)
91 .predictShapeAux = function(data, fdays, today, horizon, h, kernel, simtype, final_call)
93 fdays = fdays[ fdays < today ]
94 # TODO: 3 = magic number
95 if (length(fdays) < 3)
100 h_endo = ifelse(simtype=="mix", h[1], h)
102 # Distances from last observed day to days in the past
103 serieToday = data$getSerie(today)
104 distances2 = sapply(fdays, function(i) {
105 delta = serieToday - data$getSerie(i)
109 sd_dist = sd(distances2)
110 if (sd_dist < .Machine$double.eps)
112 # warning("All computed distances are very close: stdev too small")
113 sd_dist = 1 #mostly for tests... FIXME:
117 exp(-distances2/(sd_dist*h_endo^2))
121 u = 1 - distances2/(sd_dist*h_endo^2)
127 if (simtype != "endo")
129 h_exo = ifelse(simtype=="mix", h[2], h)
131 M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
132 M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) )
133 for (i in seq_along(fdays))
134 M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
136 sigma = cov(M) #NOTE: robust covariance is way too slow
137 # TODO: 10 == magic number; more robust way == det, or always ginv()
139 if (length(fdays) > 10)
144 # Distances from last observed day to days in the past
145 distances2 = sapply(seq_along(fdays), function(i) {
146 delta = M[1,] - M[i+1,]
147 delta %*% sigma_inv %*% delta
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:
158 exp(-distances2/(sd_dist*h_exo^2))
162 u = 1 - distances2/(sd_dist*h_exo^2)
169 if (simtype == "exo")
171 else if (simtype == "endo")
174 simils_endo * simils_exo
176 prediction = rep(0, horizon)
177 for (i in seq_along(fdays))
178 prediction = prediction + similarities[i] * data$getCenteredSerie(fdays[i]+1)[1:horizon]
179 prediction = prediction / sum(similarities, na.rm=TRUE)
183 private$.params$weights <- similarities
184 private$.params$indices <- fdays
185 private$.params$window <-
188 else if (simtype=="exo")