intermediate: R6, too slow
[talweg.git] / pkg / R / F_Neighbors.R
1 #' @include Forecaster.R
2 #'
3 #' Neighbors Forecaster
4 #'
5 #' Predict tomorrow as a weighted combination of "futures of the past" days.
6 #' Inherits \code{\link{Forecaster}}
7 NeighborsForecaster = R6::R6Class("NeighborsForecaster",
8 inherit = "Forecaster",
9
10 public = list(
11 predictShape = function(today, memory, horizon, ...)
12 {
13 # (re)initialize computed parameters
14 params <<- list("weights"=NA, "indices"=NA, "window"=NA)
15
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))
21
22 # Determine indices of no-NAs days followed by no-NAs tomorrows
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 }) ]
27
28 # Indices of similar days for cross-validation; TODO: 45 = magic number
29 sdays = getSimilarDaysIndices(today, limit=45, same_season=FALSE)
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
36 for (i in intersect(fdays,sdays))
37 {
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]))
41 {
42 nb_jours = nb_jours + 1
43 error = error + mean((data$getCenteredSerie(i+1)[1:horizon] - prediction)^2)
44 }
45 }
46 return (error / nb_jours)
47 }
48
49 if (simtype != "endo")
50 h_best_exo = optimize(errorOnLastNdays, c(0,10), kernel=kernel, simtype="exo")$minimum
51 if (simtype != "exo")
52 h_best_endo = optimize(errorOnLastNdays, c(0,10), kernel=kernel, simtype="endo")$minimum
53
54 if (simtype == "endo")
55 return (.predictShapeAux(fdays, today, horizon, h_best_endo, kernel, "endo", TRUE))
56 if (simtype == "exo")
57 return (.predictShapeAux(fdays, today, horizon, h_best_exo, kernel, "exo", TRUE))
58 if (simtype == "mix")
59 {
60 h_best_mix = c(h_best_endo,h_best_exo)
61 return (.predictShapeAux(fdays, today, horizon, h_best_mix, kernel, "mix", TRUE))
62 }
63 }
64 ),
65 private = list(
66 # Precondition: "today" is full (no NAs)
67 .predictShapeAux = function(fdays, today, horizon, h, kernel, simtype, final_call)
68 {
69 fdays = fdays[ fdays < today ]
70 # TODO: 3 = magic number
71 if (length(fdays) < 3)
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
79 distances2 = rep(NA, length(fdays))
80 for (i in seq_along(fdays))
81 {
82 delta = data$getCenteredSerie(today) - data$getCenteredSerie(fdays[i])
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)
89 if (sd_dist < .Machine$double.eps)
90 sd_dist = 1 #mostly for tests... FIXME:
91 simils_endo =
92 if (kernel=="Gauss")
93 exp(-distances2/(sd_dist*h_endo^2))
94 else { #Epanechnikov
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
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)) )
107 for (i in seq_along(fdays))
108 M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
109
110 sigma = cov(M) #NOTE: robust covariance is way too slow
111 sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed?
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 =
123 if (kernel=="Gauss")
124 exp(-distances2/(sd_dist*h_exo^2))
125 else { #Epanechnikov
126 u = 1 - distances2/(sd_dist*h_exo^2)
127 u[abs(u)>1] = 0.
128 u
129 }
130 }
131
132 similarities =
133 if (simtype == "exo")
134 simils_exo
135 else if (simtype == "endo")
136 simils_endo
137 else #mix
138 simils_endo * simils_exo
139
140 prediction = rep(0, horizon)
141 for (i in seq_along(fdays_indices))
142 prediction = prediction + similarities[i] * data$getSerie(fdays_indices[i]+1)[1:horizon]
143 prediction = prediction / sum(similarities, na.rm=TRUE)
144
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
154 } else { #mix
155 c(h_endo,h_exo)
156 }
157 }
158
159 return (prediction)
160 }
161 )
162 )