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}}
7 NeighborsForecaster = R6::R6Class("NeighborsForecaster",
11 predictShape = function(data, today, memory, horizon, ...)
13 # (re)initialize computed parameters
14 private$.params <- list("weights"=NA, "indices"=NA, "window"=NA)
16 # Do not forecast on days with NAs (TODO: softer condition...)
17 if (any(is.na(data$getCenteredSerie(today))))
20 # Determine indices of no-NAs days followed by no-NAs tomorrows
21 fdays = getNoNA2(data, max(today-memory,1), today-1)
24 simtype = ifelse(hasArg("simtype"), list(...)$simtype, "mix") #or "endo", or "exo"
25 kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
28 return ( private$.predictShapeAux(data,
29 fdays, today, horizon, list(...)$h_window, kernel, simtype, TRUE) )
32 # Indices of similar days for cross-validation; TODO: 45 = magic number
33 sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE)
35 # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days
36 errorOnLastNdays = function(h, kernel, simtype)
40 for (i in intersect(fdays,sdays))
42 # mix_strategy is never used here (simtype != "mix"), therefore left blank
43 prediction = private$.predictShapeAux(data,
44 fdays, i, horizon, h, kernel, simtype, FALSE)
45 if (!is.na(prediction[1]))
47 nb_jours = nb_jours + 1
49 mean((data$getCenteredSerie(i+1)[1:horizon] - prediction)^2)
52 return (error / nb_jours)
55 if (simtype != "endo")
57 h_best_exo = optimize(
58 errorOnLastNdays, c(0,10), kernel=kernel, simtype="exo")$minimum
62 h_best_endo = optimize(
63 errorOnLastNdays, c(0,10), kernel=kernel, simtype="endo")$minimum
66 if (simtype == "endo")
68 return (private$.predictShapeAux(data,
69 fdays, today, horizon, h_best_endo, kernel, "endo", TRUE))
73 return (private$.predictShapeAux(data,
74 fdays, today, horizon, h_best_exo, kernel, "exo", TRUE))
78 h_best_mix = c(h_best_endo,h_best_exo)
79 return(private$.predictShapeAux(data,
80 fdays, today, horizon, h_best_mix, kernel, "mix", TRUE))
85 # Precondition: "today" is full (no NAs)
86 .predictShapeAux = function(data, fdays, today, horizon, h, kernel, simtype, final_call)
88 fdays = fdays[ fdays < today ]
89 # TODO: 3 = magic number
90 if (length(fdays) < 3)
95 h_endo = ifelse(simtype=="mix", h[1], h)
97 # Distances from last observed day to days in the past
98 distances2 = rep(NA, length(fdays))
99 for (i in seq_along(fdays))
101 delta = data$getCenteredSerie(today) - data$getCenteredSerie(fdays[i])
102 # Require at least half of non-NA common values to compute the distance
103 if (sum(is.na(delta)) <= 0) #length(delta)/2)
104 distances2[i] = mean(delta^2) #, na.rm=TRUE)
107 sd_dist = sd(distances2)
108 if (sd_dist < .Machine$double.eps)
109 sd_dist = 1 #mostly for tests... FIXME:
112 exp(-distances2/(sd_dist*h_endo^2))
114 u = 1 - distances2/(sd_dist*h_endo^2)
120 if (simtype != "endo")
122 h_exo = ifelse(simtype=="mix", h[2], h)
124 M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
125 M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) )
126 for (i in seq_along(fdays))
127 M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
129 sigma = cov(M) #NOTE: robust covariance is way too slow
130 sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed?
132 # Distances from last observed day to days in the past
133 distances2 = rep(NA, nrow(M)-1)
136 delta = M[1,] - M[i,]
137 distances2[i-1] = delta %*% sigma_inv %*% delta
140 sd_dist = sd(distances2)
143 exp(-distances2/(sd_dist*h_exo^2))
145 u = 1 - distances2/(sd_dist*h_exo^2)
152 if (simtype == "exo")
154 else if (simtype == "endo")
157 simils_endo * simils_exo
159 prediction = rep(0, horizon)
160 for (i in seq_along(fdays))
161 prediction = prediction + similarities[i] * data$getSerie(fdays[i]+1)[1:horizon]
162 prediction = prediction / sum(similarities, na.rm=TRUE)
166 private$.params$weights <- similarities
167 private$.params$indices <- fdays
168 private$.params$window <-
169 if (simtype=="endo") {
171 } else if (simtype=="exo") {