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( | |
a866acb3 BA |
12 | # predictSerie = function(data, today, memory, horizon, ...) |
13 | # { | |
14 | # # Parameters (potentially) computed during shape prediction stage | |
15 | # predicted_shape = self$predictShape(data, today, memory, horizon, ...) | |
16 | ## predicted_delta = private$.pjump(data,today,memory,horizon,private$.params,...) | |
17 | # # Predicted shape is aligned it on the end of current day + jump | |
18 | ## predicted_shape+tail(data$getSerie(today),1)-predicted_shape[1]+predicted_delta | |
19 | # predicted_shape | |
20 | # }, | |
5c49f6ce BA |
21 | predictShape = function(data, today, memory, horizon, ...) |
22 | { | |
23 | # (re)initialize computed parameters | |
24 | private$.params <- list("weights"=NA, "indices"=NA, "window"=NA) | |
25 | ||
26 | # Do not forecast on days with NAs (TODO: softer condition...) | |
27 | if (any(is.na(data$getCenteredSerie(today)))) | |
28 | return (NA) | |
29 | ||
30 | # Determine indices of no-NAs days followed by no-NAs tomorrows | |
9db234c5 | 31 | fdays = getNoNA2(data, max(today-memory,1), today-1) |
5c49f6ce BA |
32 | |
33 | # Get optional args | |
5e838b3e | 34 | simtype = ifelse(hasArg("simtype"), list(...)$simtype, "mix") #or "endo", or "exo" |
5c49f6ce BA |
35 | kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan" |
36 | if (hasArg(h_window)) | |
37 | { | |
38 | return ( private$.predictShapeAux(data, | |
5e838b3e | 39 | fdays, today, horizon, list(...)$h_window, kernel, simtype, TRUE) ) |
5c49f6ce BA |
40 | } |
41 | ||
9db234c5 BA |
42 | # Indices of similar days for cross-validation; TODO: 45 = magic number |
43 | sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE) | |
44 | ||
5e838b3e BA |
45 | cv_days = intersect(fdays,sdays) |
46 | # Limit to 20 most recent matching days (TODO: 20 == magic number) | |
47 | cv_days = sort(cv_days,decreasing=TRUE)[1:min(20,length(cv_days))] | |
48 | ||
5c49f6ce | 49 | # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days |
5e838b3e | 50 | errorOnLastNdays = function(h, kernel, simtype) |
5c49f6ce BA |
51 | { |
52 | error = 0 | |
53 | nb_jours = 0 | |
5e838b3e | 54 | for (i in seq_along(cv_days)) |
5c49f6ce BA |
55 | { |
56 | # mix_strategy is never used here (simtype != "mix"), therefore left blank | |
5e838b3e BA |
57 | prediction = private$.predictShapeAux(data, |
58 | fdays, cv_days[i], horizon, h, kernel, simtype, FALSE) | |
5c49f6ce BA |
59 | if (!is.na(prediction[1])) |
60 | { | |
61 | nb_jours = nb_jours + 1 | |
62 | error = error + | |
5e838b3e | 63 | mean((data$getSerie(cv_days[i]+1)[1:horizon] - prediction)^2) |
5c49f6ce BA |
64 | } |
65 | } | |
66 | return (error / nb_jours) | |
67 | } | |
68 | ||
5e838b3e BA |
69 | if (simtype != "endo") |
70 | { | |
71 | h_best_exo = optimize( | |
72 | errorOnLastNdays, c(0,10), kernel=kernel, simtype="exo")$minimum | |
73 | } | |
74 | if (simtype != "exo") | |
75 | { | |
76 | h_best_endo = optimize( | |
77 | errorOnLastNdays, c(0,10), kernel=kernel, simtype="endo")$minimum | |
78 | } | |
79 | ||
80 | if (simtype == "endo") | |
81 | { | |
82 | return (private$.predictShapeAux(data, | |
83 | fdays, today, horizon, h_best_endo, kernel, "endo", TRUE)) | |
84 | } | |
85 | if (simtype == "exo") | |
86 | { | |
87 | return (private$.predictShapeAux(data, | |
88 | fdays, today, horizon, h_best_exo, kernel, "exo", TRUE)) | |
89 | } | |
90 | if (simtype == "mix") | |
91 | { | |
92 | h_best_mix = c(h_best_endo,h_best_exo) | |
93 | return(private$.predictShapeAux(data, | |
94 | fdays, today, horizon, h_best_mix, kernel, "mix", TRUE)) | |
95 | } | |
5c49f6ce BA |
96 | } |
97 | ), | |
98 | private = list( | |
99 | # Precondition: "today" is full (no NAs) | |
5e838b3e | 100 | .predictShapeAux = function(data, fdays, today, horizon, h, kernel, simtype, final_call) |
5c49f6ce BA |
101 | { |
102 | fdays = fdays[ fdays < today ] | |
103 | # TODO: 3 = magic number | |
a866acb3 | 104 | if (length(fdays) < 1) |
5c49f6ce BA |
105 | return (NA) |
106 | ||
9db234c5 BA |
107 | # Neighbors: days in "same season" |
108 | sdays = getSimilarDaysIndices(today, limit=45, same_season=TRUE, data) | |
109 | indices = intersect(fdays,sdays) | |
110 | levelToday = data$getLevel(today) | |
5e838b3e | 111 | distances = sapply(indices, function(i) abs(data$getLevel(i)-levelToday)) |
9db234c5 | 112 | same_pollution = (distances <= 2) |
a866acb3 | 113 | if (sum(same_pollution) < 1) #TODO: 3 == magic number |
5c49f6ce | 114 | { |
9db234c5 | 115 | same_pollution = (distances <= 5) |
a866acb3 | 116 | if (sum(same_pollution) < 1) |
9db234c5 | 117 | return (NA) |
5c49f6ce | 118 | } |
9db234c5 BA |
119 | indices = indices[same_pollution] |
120 | ||
a866acb3 BA |
121 | #TODO: we shouldn't need that block |
122 | if (length(indices) == 1) | |
123 | { | |
124 | if (final_call) | |
125 | { | |
126 | private$.params$weights <- 1 | |
127 | private$.params$indices <- indices | |
128 | private$.params$window <- 1 | |
129 | } | |
130 | return ( data$getSerie(indices[1])[1:horizon] ) #what else?! | |
131 | } | |
132 | ||
5e838b3e BA |
133 | if (simtype != "exo") |
134 | { | |
135 | h_endo = ifelse(simtype=="mix", h[1], h) | |
9db234c5 | 136 | |
5e838b3e BA |
137 | # Distances from last observed day to days in the past |
138 | serieToday = data$getSerie(today) | |
139 | distances2 = sapply(indices, function(i) { | |
140 | delta = serieToday - data$getSerie(i) | |
141 | mean(delta^2) | |
142 | }) | |
5c49f6ce | 143 | |
5e838b3e BA |
144 | sd_dist = sd(distances2) |
145 | if (sd_dist < .Machine$double.eps) | |
146 | { | |
5c49f6ce | 147 | # warning("All computed distances are very close: stdev too small") |
5e838b3e BA |
148 | sd_dist = 1 #mostly for tests... FIXME: |
149 | } | |
150 | simils_endo = | |
151 | if (kernel=="Gauss") | |
152 | exp(-distances2/(sd_dist*h_endo^2)) | |
153 | else | |
154 | { | |
155 | # Epanechnikov | |
156 | u = 1 - distances2/(sd_dist*h_endo^2) | |
157 | u[abs(u)>1] = 0. | |
158 | u | |
159 | } | |
5c49f6ce | 160 | } |
5e838b3e BA |
161 | |
162 | if (simtype != "endo") | |
163 | { | |
164 | h_exo = ifelse(simtype=="mix", h[2], h) | |
165 | ||
166 | M = matrix( nrow=1+length(indices), ncol=1+length(data$getExo(today)) ) | |
167 | M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) ) | |
168 | for (i in seq_along(indices)) | |
169 | M[i+1,] = c( data$getLevel(indices[i]), as.double(data$getExo(indices[i])) ) | |
170 | ||
171 | sigma = cov(M) #NOTE: robust covariance is way too slow | |
172 | # sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed? | |
173 | sigma_inv = MASS::ginv(sigma) | |
a866acb3 | 174 | |
5e838b3e BA |
175 | # Distances from last observed day to days in the past |
176 | distances2 = sapply(seq_along(indices), function(i) { | |
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 | |
5c49f6ce BA |
206 | |
207 | prediction = rep(0, horizon) | |
9db234c5 BA |
208 | for (i in seq_along(indices)) |
209 | prediction = prediction + similarities[i] * data$getSerie(indices[i]+1)[1:horizon] | |
5c49f6ce BA |
210 | prediction = prediction / sum(similarities, na.rm=TRUE) |
211 | ||
212 | if (final_call) | |
213 | { | |
214 | private$.params$weights <- similarities | |
a866acb3 | 215 | private$.params$indices <- indices |
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 | ) |