Commit | Line | Data |
---|---|---|
5c49f6ce BA |
1 | #' @include Forecaster.R |
2 | #' | |
3 | #' Neighbors2 Forecaster | |
4 | #' | |
5 | #' Predict tomorrow as a weighted combination of "futures of the past" days. | |
6 | #' Inherits \code{\link{Forecaster}} | |
7 | #' | |
8 | Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster", | |
9 | inherit = Forecaster, | |
10 | ||
11 | public = list( | |
12 | predictShape = function(data, today, memory, horizon, ...) | |
13 | { | |
14 | # (re)initialize computed parameters | |
15 | private$.params <- list("weights"=NA, "indices"=NA, "window"=NA) | |
16 | ||
17 | # Do not forecast on days with NAs (TODO: softer condition...) | |
18 | if (any(is.na(data$getCenteredSerie(today)))) | |
19 | return (NA) | |
20 | ||
21 | # Determine indices of no-NAs days followed by no-NAs tomorrows | |
9db234c5 | 22 | fdays = getNoNA2(data, max(today-memory,1), today-1) |
5c49f6ce BA |
23 | |
24 | # Get optional args | |
5e838b3e | 25 | simtype = ifelse(hasArg("simtype"), list(...)$simtype, "mix") #or "endo", or "exo" |
5c49f6ce BA |
26 | kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan" |
27 | if (hasArg(h_window)) | |
28 | { | |
29 | return ( private$.predictShapeAux(data, | |
5e838b3e | 30 | fdays, today, horizon, list(...)$h_window, kernel, simtype, TRUE) ) |
5c49f6ce BA |
31 | } |
32 | ||
6774e53d BA |
33 | # Indices of similar days for cross-validation; TODO: 20 = magic number |
34 | cv_days = getSimilarDaysIndices(today, data, limit=20, same_season=FALSE, days_in=fdays) | |
5e838b3e | 35 | |
5c49f6ce | 36 | # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days |
5e838b3e | 37 | errorOnLastNdays = function(h, kernel, simtype) |
5c49f6ce BA |
38 | { |
39 | error = 0 | |
40 | nb_jours = 0 | |
5e838b3e | 41 | for (i in seq_along(cv_days)) |
5c49f6ce BA |
42 | { |
43 | # mix_strategy is never used here (simtype != "mix"), therefore left blank | |
5e838b3e BA |
44 | prediction = private$.predictShapeAux(data, |
45 | fdays, cv_days[i], horizon, h, kernel, simtype, FALSE) | |
5c49f6ce BA |
46 | if (!is.na(prediction[1])) |
47 | { | |
48 | nb_jours = nb_jours + 1 | |
49 | error = error + | |
6774e53d | 50 | mean((data$getCenteredSerie(cv_days[i]+1)[1:horizon] - prediction)^2) |
5c49f6ce BA |
51 | } |
52 | } | |
53 | return (error / nb_jours) | |
54 | } | |
55 | ||
5e838b3e BA |
56 | if (simtype != "endo") |
57 | { | |
58 | h_best_exo = optimize( | |
ea5c7e56 | 59 | errorOnLastNdays, c(0,7), kernel=kernel, simtype="exo")$minimum |
5e838b3e BA |
60 | } |
61 | if (simtype != "exo") | |
62 | { | |
63 | h_best_endo = optimize( | |
ea5c7e56 | 64 | errorOnLastNdays, c(0,7), kernel=kernel, simtype="endo")$minimum |
5e838b3e BA |
65 | } |
66 | ||
67 | if (simtype == "endo") | |
68 | { | |
69 | return (private$.predictShapeAux(data, | |
70 | fdays, today, horizon, h_best_endo, kernel, "endo", TRUE)) | |
71 | } | |
72 | if (simtype == "exo") | |
73 | { | |
74 | return (private$.predictShapeAux(data, | |
75 | fdays, today, horizon, h_best_exo, kernel, "exo", TRUE)) | |
76 | } | |
77 | if (simtype == "mix") | |
78 | { | |
79 | h_best_mix = c(h_best_endo,h_best_exo) | |
80 | return(private$.predictShapeAux(data, | |
81 | fdays, today, horizon, h_best_mix, kernel, "mix", TRUE)) | |
82 | } | |
5c49f6ce BA |
83 | } |
84 | ), | |
85 | private = list( | |
86 | # Precondition: "today" is full (no NAs) | |
5e838b3e | 87 | .predictShapeAux = function(data, fdays, today, horizon, h, kernel, simtype, final_call) |
5c49f6ce | 88 | { |
6774e53d | 89 | fdays_cut = fdays[ fdays < today ] |
5c49f6ce | 90 | # TODO: 3 = magic number |
6774e53d | 91 | if (length(fdays_cut) < 3) |
5c49f6ce BA |
92 | return (NA) |
93 | ||
6774e53d BA |
94 | # Neighbors: days in "same season"; TODO: 60 == magic number... |
95 | fdays = getSimilarDaysIndices(today, data, limit=60, same_season=TRUE, days_in=fdays_cut) | |
96 | if (length(fdays) <= 1) | |
ee8b1b4e | 97 | return (NA) |
9db234c5 | 98 | levelToday = data$getLevel(today) |
6774e53d | 99 | distances = sapply(fdays, function(i) abs(data$getLevel(i)-levelToday)) |
ea5c7e56 BA |
100 | #TODO: 2, 3, 5, 10 magic numbers here... |
101 | dist_thresh = 2 | |
102 | min_neighbs = min(3,length(fdays)) | |
6774e53d | 103 | repeat |
5c49f6ce | 104 | { |
6774e53d | 105 | same_pollution = (distances <= dist_thresh) |
ea5c7e56 BA |
106 | nb_neighbs = sum(same_pollution) |
107 | if (nb_neighbs >= min_neighbs) #will eventually happen | |
6774e53d | 108 | break |
ea5c7e56 | 109 | dist_thresh = dist_thresh + 3 |
5c49f6ce | 110 | } |
6774e53d | 111 | fdays = fdays[same_pollution] |
ea5c7e56 BA |
112 | max_neighbs = 10 |
113 | if (nb_neighbs > max_neighbs) | |
114 | { | |
115 | # Keep only max_neighbs closest neighbors | |
116 | fdays = fdays[ sort(distances[same_pollution],index.return=TRUE)$ix[1:max_neighbs] ] | |
117 | } | |
118 | if (length(fdays) == 1) #the other extreme... | |
a866acb3 BA |
119 | { |
120 | if (final_call) | |
121 | { | |
122 | private$.params$weights <- 1 | |
6774e53d | 123 | private$.params$indices <- fdays |
a866acb3 BA |
124 | private$.params$window <- 1 |
125 | } | |
6774e53d | 126 | return ( data$getSerie(fdays[1])[1:horizon] ) #what else?! |
a866acb3 BA |
127 | } |
128 | ||
5e838b3e BA |
129 | if (simtype != "exo") |
130 | { | |
131 | h_endo = ifelse(simtype=="mix", h[1], h) | |
9db234c5 | 132 | |
5e838b3e BA |
133 | # Distances from last observed day to days in the past |
134 | serieToday = data$getSerie(today) | |
6774e53d | 135 | distances2 = sapply(fdays, function(i) { |
5e838b3e BA |
136 | delta = serieToday - data$getSerie(i) |
137 | mean(delta^2) | |
138 | }) | |
5c49f6ce | 139 | |
5e838b3e BA |
140 | sd_dist = sd(distances2) |
141 | if (sd_dist < .Machine$double.eps) | |
142 | { | |
5c49f6ce | 143 | # warning("All computed distances are very close: stdev too small") |
5e838b3e BA |
144 | sd_dist = 1 #mostly for tests... FIXME: |
145 | } | |
146 | simils_endo = | |
147 | if (kernel=="Gauss") | |
148 | exp(-distances2/(sd_dist*h_endo^2)) | |
149 | else | |
150 | { | |
151 | # Epanechnikov | |
152 | u = 1 - distances2/(sd_dist*h_endo^2) | |
153 | u[abs(u)>1] = 0. | |
154 | u | |
155 | } | |
5c49f6ce | 156 | } |
5e838b3e BA |
157 | |
158 | if (simtype != "endo") | |
159 | { | |
160 | h_exo = ifelse(simtype=="mix", h[2], h) | |
161 | ||
6774e53d | 162 | M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) ) |
5e838b3e | 163 | M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) ) |
6774e53d BA |
164 | for (i in seq_along(fdays)) |
165 | M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) ) | |
5e838b3e BA |
166 | |
167 | sigma = cov(M) #NOTE: robust covariance is way too slow | |
ee8b1b4e BA |
168 | # TODO: 10 == magic number; more robust way == det, or always ginv() |
169 | sigma_inv = | |
6774e53d | 170 | if (length(fdays) > 10) |
ee8b1b4e BA |
171 | solve(sigma) |
172 | else | |
173 | MASS::ginv(sigma) | |
a866acb3 | 174 | |
5e838b3e | 175 | # Distances from last observed day to days in the past |
6774e53d | 176 | distances2 = sapply(seq_along(fdays), function(i) { |
5e838b3e BA |
177 | delta = M[1,] - M[i+1,] |
178 | delta %*% sigma_inv %*% delta | |
179 | }) | |
180 | ||
181 | sd_dist = sd(distances2) | |
182 | if (sd_dist < .25 * sqrt(.Machine$double.eps)) | |
5c49f6ce | 183 | { |
5e838b3e BA |
184 | # warning("All computed distances are very close: stdev too small") |
185 | sd_dist = 1 #mostly for tests... FIXME: | |
5c49f6ce | 186 | } |
5e838b3e BA |
187 | simils_exo = |
188 | if (kernel=="Gauss") | |
189 | exp(-distances2/(sd_dist*h_exo^2)) | |
190 | else | |
191 | { | |
192 | # Epanechnikov | |
193 | u = 1 - distances2/(sd_dist*h_exo^2) | |
194 | u[abs(u)>1] = 0. | |
195 | u | |
196 | } | |
197 | } | |
5c49f6ce | 198 | |
5e838b3e BA |
199 | similarities = |
200 | if (simtype == "exo") | |
201 | simils_exo | |
202 | else if (simtype == "endo") | |
203 | simils_endo | |
204 | else #mix | |
205 | simils_endo * simils_exo | |
ea5c7e56 | 206 | similarities = similarities / sum(similarities) |
5c49f6ce BA |
207 | |
208 | prediction = rep(0, horizon) | |
6774e53d BA |
209 | for (i in seq_along(fdays)) |
210 | prediction = prediction + similarities[i] * data$getCenteredSerie(fdays[i]+1)[1:horizon] | |
5c49f6ce BA |
211 | |
212 | if (final_call) | |
213 | { | |
214 | private$.params$weights <- similarities | |
6774e53d | 215 | private$.params$indices <- fdays |
5e838b3e BA |
216 | private$.params$window <- |
217 | if (simtype=="endo") | |
218 | h_endo | |
219 | else if (simtype=="exo") | |
220 | h_exo | |
221 | else #mix | |
222 | c(h_endo,h_exo) | |
5c49f6ce BA |
223 | } |
224 | ||
225 | return (prediction) | |
226 | } | |
227 | ) | |
228 | ) |