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