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",
8 inherit = "Forecaster",
11 predictShape = function(today, memory, horizon, ...)
13 # (re)initialize computed parameters
14 params <<- list("weights"=NA, "indices"=NA, "window"=NA)
17 simtype = ifelse(hasArg("simtype"), list(...)$simtype, "mix") #or "endo", or "exo"
18 kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
20 return (.predictShapeAux(fdays,today,horizon,list(...)$h_window,kernel,simtype,TRUE))
22 # Determine indices of no-NAs days followed by no-NAs tomorrows
23 first_day = max(today - memory, 1)
24 fdays = (first_day:(today-1))[ sapply(first_day:(today-1), function(i) {
25 !any(is.na(data$getSerie(i)) | is.na(data$getSerie(i+1)))
28 # Indices of similar days for cross-validation; TODO: 45 = magic number
29 sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE)
31 # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days
32 errorOnLastNdays = function(h, kernel, simtype)
36 for (i in intersect(fdays,sdays))
38 # mix_strategy is never used here (simtype != "mix"), therefore left blank
39 prediction = .predictShapeAux(fdays, i, horizon, h, kernel, simtype, FALSE)
40 if (!is.na(prediction[1]))
42 nb_jours = nb_jours + 1
43 error = error + mean((data$getCenteredSerie(i+1)[1:horizon] - prediction)^2)
46 return (error / nb_jours)
49 if (simtype != "endo")
50 h_best_exo = optimize(errorOnLastNdays, c(0,10), kernel=kernel, simtype="exo")$minimum
52 h_best_endo = optimize(errorOnLastNdays, c(0,10), kernel=kernel, simtype="endo")$minimum
54 if (simtype == "endo")
55 return (.predictShapeAux(fdays, today, horizon, h_best_endo, kernel, "endo", TRUE))
57 return (.predictShapeAux(fdays, today, horizon, h_best_exo, kernel, "exo", TRUE))
60 h_best_mix = c(h_best_endo,h_best_exo)
61 return (.predictShapeAux(fdays, today, horizon, h_best_mix, kernel, "mix", TRUE))
66 # Precondition: "today" is full (no NAs)
67 .predictShapeAux = function(fdays, today, horizon, h, kernel, simtype, final_call)
69 fdays = fdays[ fdays < today ]
70 # TODO: 3 = magic number
71 if (length(fdays) < 3)
76 h_endo = ifelse(simtype=="mix", h[1], h)
78 # Distances from last observed day to days in the past
79 distances2 = rep(NA, length(fdays))
80 for (i in seq_along(fdays))
82 delta = data$getCenteredSerie(today) - data$getCenteredSerie(fdays[i])
83 # Require at least half of non-NA common values to compute the distance
84 if (sum(is.na(delta)) <= 0) #length(delta)/2)
85 distances2[i] = mean(delta^2) #, na.rm=TRUE)
88 sd_dist = sd(distances2)
89 if (sd_dist < .Machine$double.eps)
90 sd_dist = 1 #mostly for tests... FIXME:
93 exp(-distances2/(sd_dist*h_endo^2))
95 u = 1 - distances2/(sd_dist*h_endo^2)
101 if (simtype != "endo")
103 h_exo = ifelse(simtype=="mix", h[2], h)
105 M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
106 M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) )
107 for (i in seq_along(fdays))
108 M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
110 sigma = cov(M) #NOTE: robust covariance is way too slow
111 sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed?
113 # Distances from last observed day to days in the past
114 distances2 = rep(NA, nrow(M)-1)
117 delta = M[1,] - M[i,]
118 distances2[i-1] = delta %*% sigma_inv %*% delta
121 sd_dist = sd(distances2)
124 exp(-distances2/(sd_dist*h_exo^2))
126 u = 1 - distances2/(sd_dist*h_exo^2)
133 if (simtype == "exo")
135 else if (simtype == "endo")
138 simils_endo * simils_exo
140 prediction = rep(0, horizon)
141 for (i in seq_along(fdays_indices))
142 prediction = prediction + similarities[i] * data$getSerie(fdays_indices[i]+1)[1:horizon]
143 prediction = prediction / sum(similarities, na.rm=TRUE)
147 params$weights <<- similarities
148 params$indices <<- fdays_indices
150 if (simtype=="endo") {
152 } else if (simtype=="exo") {