cleaning - fix getSimilarDaysIndices ; to finish
[talweg.git] / pkg / R / F_Neighbors2.R
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 # 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 # },
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
31 fdays = getNoNA2(data, max(today-memory,1), today-1)
32
33 # Get optional args
34 simtype = ifelse(hasArg("simtype"), list(...)$simtype, "mix") #or "endo", or "exo"
35 kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
36 if (hasArg(h_window))
37 {
38 return ( private$.predictShapeAux(data,
39 fdays, today, horizon, list(...)$h_window, kernel, simtype, TRUE) )
40 }
41
42 # Indices of similar days for cross-validation; TODO: 45 = magic number
43 sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE)
44
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
49 # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days
50 errorOnLastNdays = function(h, kernel, simtype)
51 {
52 error = 0
53 nb_jours = 0
54 for (i in seq_along(cv_days))
55 {
56 # mix_strategy is never used here (simtype != "mix"), therefore left blank
57 prediction = private$.predictShapeAux(data,
58 fdays, cv_days[i], horizon, h, kernel, simtype, FALSE)
59 if (!is.na(prediction[1]))
60 {
61 nb_jours = nb_jours + 1
62 error = error +
63 mean((data$getSerie(cv_days[i]+1)[1:horizon] - prediction)^2)
64 }
65 }
66 return (error / nb_jours)
67 }
68
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 }
96 }
97 ),
98 private = list(
99 # Precondition: "today" is full (no NAs)
100 .predictShapeAux = function(data, fdays, today, horizon, h, kernel, simtype, final_call)
101 {
102 fdays = fdays[ fdays < today ]
103 # TODO: 3 = magic number
104 if (length(fdays) < 1)
105 return (NA)
106
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)
111 distances = sapply(indices, function(i) abs(data$getLevel(i)-levelToday))
112 same_pollution = (distances <= 2)
113 if (sum(same_pollution) < 1) #TODO: 3 == magic number
114 {
115 same_pollution = (distances <= 5)
116 if (sum(same_pollution) < 1)
117 return (NA)
118 }
119 indices = indices[same_pollution]
120
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
133 if (simtype != "exo")
134 {
135 h_endo = ifelse(simtype=="mix", h[1], h)
136
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 })
143
144 sd_dist = sd(distances2)
145 if (sd_dist < .Machine$double.eps)
146 {
147 # warning("All computed distances are very close: stdev too small")
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 }
160 }
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)
174
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))
183 {
184 # warning("All computed distances are very close: stdev too small")
185 sd_dist = 1 #mostly for tests... FIXME:
186 }
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 }
198
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
206
207 prediction = rep(0, horizon)
208 for (i in seq_along(indices))
209 prediction = prediction + similarities[i] * data$getSerie(indices[i]+1)[1:horizon]
210 prediction = prediction / sum(similarities, na.rm=TRUE)
211
212 if (final_call)
213 {
214 private$.params$weights <- similarities
215 private$.params$indices <- indices
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)
223 }
224
225 return (prediction)
226 }
227 )
228 )