intermediate: R6, too slow
[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}}
7NeighborsForecaster = R6::R6Class("NeighborsForecaster",
8 inherit = "Forecaster",
9
10 public = list(
e030a6e3 11 predictShape = function(today, memory, horizon, ...)
3d69ff21
BA
12 {
13 # (re)initialize computed parameters
14 params <<- list("weights"=NA, "indices"=NA, "window"=NA)
15
f17665c7
BA
16 # Get optional args
17 simtype = ifelse(hasArg("simtype"), list(...)$simtype, "mix") #or "endo", or "exo"
18 kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
19 if (hasArg(h_window))
20 return (.predictShapeAux(fdays,today,horizon,list(...)$h_window,kernel,simtype,TRUE))
3d69ff21 21
3d69ff21 22 # Determine indices of no-NAs days followed by no-NAs tomorrows
f17665c7
BA
23 first_day = max(today - memory, 1)
24 fdays = (first_day:(today-1))[ sapply(first_day:(today-1), function(i) {
25 !any(is.na(data$getSerie(i)) | is.na(data$getSerie(i+1)))
26 }) ]
3d69ff21 27
f17665c7
BA
28 # Indices of similar days for cross-validation; TODO: 45 = magic number
29 sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE)
3d69ff21
BA
30
31 # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days
32 errorOnLastNdays = function(h, kernel, simtype)
33 {
34 error = 0
35 nb_jours = 0
f17665c7 36 for (i in intersect(fdays,sdays))
3d69ff21 37 {
f17665c7
BA
38 # mix_strategy is never used here (simtype != "mix"), therefore left blank
39 prediction = .predictShapeAux(fdays, i, horizon, h, kernel, simtype, FALSE)
40 if (!is.na(prediction[1]))
3d69ff21
BA
41 {
42 nb_jours = nb_jours + 1
f17665c7 43 error = error + mean((data$getCenteredSerie(i+1)[1:horizon] - prediction)^2)
3d69ff21
BA
44 }
45 }
46 return (error / nb_jours)
47 }
48
f17665c7
BA
49 if (simtype != "endo")
50 h_best_exo = optimize(errorOnLastNdays, c(0,10), kernel=kernel, simtype="exo")$minimum
3d69ff21 51 if (simtype != "exo")
f17665c7 52 h_best_endo = optimize(errorOnLastNdays, c(0,10), kernel=kernel, simtype="endo")$minimum
3d69ff21
BA
53
54 if (simtype == "endo")
f17665c7 55 return (.predictShapeAux(fdays, today, horizon, h_best_endo, kernel, "endo", TRUE))
3d69ff21 56 if (simtype == "exo")
f17665c7 57 return (.predictShapeAux(fdays, today, horizon, h_best_exo, kernel, "exo", TRUE))
3d69ff21
BA
58 if (simtype == "mix")
59 {
f17665c7
BA
60 h_best_mix = c(h_best_endo,h_best_exo)
61 return (.predictShapeAux(fdays, today, horizon, h_best_mix, kernel, "mix", TRUE))
3d69ff21 62 }
25b75559
BA
63 }
64 ),
65 private = list(
3d69ff21 66 # Precondition: "today" is full (no NAs)
f17665c7 67 .predictShapeAux = function(fdays, today, horizon, h, kernel, simtype, final_call)
3d69ff21 68 {
f17665c7 69 fdays = fdays[ fdays < today ]
3d69ff21 70 # TODO: 3 = magic number
f17665c7 71 if (length(fdays) < 3)
3d69ff21
BA
72 return (NA)
73
74 if (simtype != "exo")
75 {
76 h_endo = ifelse(simtype=="mix", h[1], h)
77
78 # Distances from last observed day to days in the past
f17665c7
BA
79 distances2 = rep(NA, length(fdays))
80 for (i in seq_along(fdays))
3d69ff21 81 {
25b75559 82 delta = data$getCenteredSerie(today) - data$getCenteredSerie(fdays[i])
3d69ff21
BA
83 # Require at least half of non-NA common values to compute the distance
84 if (sum(is.na(delta)) <= 0) #length(delta)/2)
85 distances2[i] = mean(delta^2) #, na.rm=TRUE)
86 }
87
88 sd_dist = sd(distances2)
99f83c9a
BA
89 if (sd_dist < .Machine$double.eps)
90 sd_dist = 1 #mostly for tests... FIXME:
3d69ff21 91 simils_endo =
99f83c9a 92 if (kernel=="Gauss")
3d69ff21 93 exp(-distances2/(sd_dist*h_endo^2))
99f83c9a 94 else { #Epanechnikov
3d69ff21
BA
95 u = 1 - distances2/(sd_dist*h_endo^2)
96 u[abs(u)>1] = 0.
97 u
98 }
99 }
100
101 if (simtype != "endo")
102 {
103 h_exo = ifelse(simtype=="mix", h[2], h)
104
25b75559
BA
105 M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
106 M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) )
f17665c7 107 for (i in seq_along(fdays))
25b75559 108 M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
3d69ff21
BA
109
110 sigma = cov(M) #NOTE: robust covariance is way too slow
613a986f 111 sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed?
3d69ff21
BA
112
113 # Distances from last observed day to days in the past
114 distances2 = rep(NA, nrow(M)-1)
115 for (i in 2:nrow(M))
116 {
117 delta = M[1,] - M[i,]
118 distances2[i-1] = delta %*% sigma_inv %*% delta
119 }
120
121 sd_dist = sd(distances2)
122 simils_exo =
f17665c7 123 if (kernel=="Gauss")
3d69ff21 124 exp(-distances2/(sd_dist*h_exo^2))
f17665c7 125 else { #Epanechnikov
3d69ff21
BA
126 u = 1 - distances2/(sd_dist*h_exo^2)
127 u[abs(u)>1] = 0.
128 u
129 }
130 }
131
3d69ff21 132 similarities =
f17665c7 133 if (simtype == "exo")
3d69ff21 134 simils_exo
f17665c7
BA
135 else if (simtype == "endo")
136 simils_endo
137 else #mix
138 simils_endo * simils_exo
3d69ff21
BA
139
140 prediction = rep(0, horizon)
141 for (i in seq_along(fdays_indices))
25b75559 142 prediction = prediction + similarities[i] * data$getSerie(fdays_indices[i]+1)[1:horizon]
3d69ff21 143 prediction = prediction / sum(similarities, na.rm=TRUE)
99f83c9a 144
3d69ff21
BA
145 if (final_call)
146 {
147 params$weights <<- similarities
148 params$indices <<- fdays_indices
149 params$window <<-
150 if (simtype=="endo") {
151 h_endo
152 } else if (simtype=="exo") {
153 h_exo
f17665c7 154 } else { #mix
3d69ff21
BA
155 c(h_endo,h_exo)
156 }
157 }
99f83c9a 158
3d69ff21
BA
159 return (prediction)
160 }
161 )
162)