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