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