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