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(today, memory, horizon, ...)
13 # (re)initialize computed parameters
14 private$.params <- list("weights"=NA, "indices"=NA, "window"=NA)
16 # Determine indices of no-NAs days followed by no-NAs tomorrows
17 fdays = private$.data$getCoupleDays(max(today-memory,1), today-1)
20 simtype = ifelse(hasArg("simtype"), list(...)$simtype, "mix") #or "endo", or "exo"
21 kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
24 return ( private$.predictShapeAux(
25 fdays, today, horizon, list(...)$h_window, kernel, simtype, TRUE) )
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 = private$.predictShapeAux(fdays, i, horizon, h, kernel, simtype, FALSE)
40 if (!is.na(prediction[1]))
42 nb_jours = nb_jours + 1
44 mean((private$.data$getCenteredSerie(i+1)[1:horizon] - prediction)^2)
47 return (error / nb_jours)
50 if (simtype != "endo")
52 h_best_exo = optimize(
53 errorOnLastNdays, c(0,10), kernel=kernel, simtype="exo")$minimum
57 h_best_endo = optimize(
58 errorOnLastNdays, c(0,10), kernel=kernel, simtype="endo")$minimum
61 if (simtype == "endo")
63 return (private$.predictShapeAux(
64 fdays, today, horizon, h_best_endo, kernel, "endo", TRUE))
68 return (private$.predictShapeAux(
69 fdays, today, horizon, h_best_exo, kernel, "exo", TRUE))
73 h_best_mix = c(h_best_endo,h_best_exo)
74 return(private$.predictShapeAux(
75 fdays, today, horizon, h_best_mix, kernel, "mix", TRUE))
80 # Precondition: "today" is full (no NAs)
81 .predictShapeAux = function(fdays, today, horizon, h, kernel, simtype, final_call)
83 fdays = fdays[ fdays < today ]
84 # TODO: 3 = magic number
85 if (length(fdays) < 3)
88 data = private$.data #shorthand
92 h_endo = ifelse(simtype=="mix", h[1], h)
94 # Distances from last observed day to days in the past
95 distances2 = rep(NA, length(fdays))
96 for (i in seq_along(fdays))
98 delta = data$getCenteredSerie(today) - data$getCenteredSerie(fdays[i])
99 # Require at least half of non-NA common values to compute the distance
100 if (sum(is.na(delta)) <= 0) #length(delta)/2)
101 distances2[i] = mean(delta^2) #, na.rm=TRUE)
104 sd_dist = sd(distances2)
105 if (sd_dist < .Machine$double.eps)
106 sd_dist = 1 #mostly for tests... FIXME:
109 exp(-distances2/(sd_dist*h_endo^2))
111 u = 1 - distances2/(sd_dist*h_endo^2)
117 if (simtype != "endo")
119 h_exo = ifelse(simtype=="mix", h[2], h)
121 M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
122 M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) )
123 for (i in seq_along(fdays))
124 M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
126 sigma = cov(M) #NOTE: robust covariance is way too slow
127 sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed?
129 # Distances from last observed day to days in the past
130 distances2 = rep(NA, nrow(M)-1)
133 delta = M[1,] - M[i,]
134 distances2[i-1] = delta %*% sigma_inv %*% delta
137 sd_dist = sd(distances2)
140 exp(-distances2/(sd_dist*h_exo^2))
142 u = 1 - distances2/(sd_dist*h_exo^2)
149 if (simtype == "exo")
151 else if (simtype == "endo")
154 simils_endo * simils_exo
156 prediction = rep(0, horizon)
157 for (i in seq_along(fdays))
158 prediction = prediction + similarities[i] * data$getSerie(fdays[i]+1)[1:horizon]
159 prediction = prediction / sum(similarities, na.rm=TRUE)
163 private$.params$weights <- similarities
164 private$.params$indices <- fdays
165 private$.params$window <-
166 if (simtype=="endo") {
168 } else if (simtype=="exo") {