1 #' @include Forecaster.R
3 #' Neighbors2 Forecaster
5 #' Predict tomorrow as a weighted combination of "futures of the past" days.
6 #' Inherits \code{\link{Forecaster}}
8 Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
12 predictShape = function(data, today, memory, horizon, ...)
14 # (re)initialize computed parameters
15 private$.params <- list("weights"=NA, "indices"=NA, "window"=NA)
17 # Do not forecast on days with NAs (TODO: softer condition...)
18 if (any(is.na(data$getCenteredSerie(today))))
21 # Determine indices of no-NAs days followed by no-NAs tomorrows
22 # Indices of similar days for cross-validation; TODO: 45 = magic number
24 getNoNA2(data, max(today-memory,1), today-1)
25 getSimilarDaysIndices(today, limit=45, same_season=TRUE) )
28 kernel = ifelse(hasArg("kernel"), list(...)$kernel, "Gauss") #or "Epan"
31 return ( private$.predictShapeAux(data,
32 fdays, today, horizon, list(...)$h_window, kernel, TRUE) )
35 # Function to optimize h : h |--> sum of prediction errors on last 45 "similar" days
36 errorOnLastNdays = function(h, kernel)
42 # mix_strategy is never used here (simtype != "mix"), therefore left blank
43 prediction = private$.predictShapeAux(data,fdays,day,horizon,h,kernel,FALSE)
44 if (!is.na(prediction[1]))
46 nb_jours = nb_jours + 1
48 mean((data$getSerie(i+1)[1:horizon] - prediction)^2)
51 return (error / nb_jours)
54 # h :: only for endo in this variation
55 h_best = optimize(errorOnLastNdays, c(0,10), kernel=kernel)$minimum
56 return (private$.predictShapeAux(data,fdays,today,horizon,h_best,kernel,TRUE))
60 # Precondition: "today" is full (no NAs)
61 .predictShapeAux = function(data, fdays, today, horizon, h, kernel, final_call)
63 fdays = fdays[ fdays < today ]
64 # TODO: 3 = magic number
65 if (length(fdays) < 3)
68 # ENDO:: Distances from last observed day to days in the past
69 distances2 = rep(NA, length(fdays))
70 for (i in seq_along(fdays))
72 delta = data$getSerie(today) - data$getSerie(fdays[i])
73 # Require at least half of non-NA common values to compute the distance
74 if ( !any( is.na(delta) ) )
75 distances2[i] = mean(delta^2)
78 sd_dist = sd(distances2)
79 if (sd_dist < .Machine$double.eps)
81 # warning("All computed distances are very close: stdev too small")
82 sd_dist = 1 #mostly for tests... FIXME:
86 exp(-distances2/(sd_dist*h_endo^2))
90 u = 1 - distances2/(sd_dist*h_endo^2)
96 # EXOGENS: distances computations are enough
97 # TODO: search among similar concentrations....... at this stage ?!
98 M = matrix( nrow=1+length(fdays), ncol=1+length(data$getExo(today)) )
99 M[1,] = c( data$getLevel(today), as.double(data$getExo(today)) )
100 for (i in seq_along(fdays))
101 M[i+1,] = c( data$getLevel(fdays[i]), as.double(data$getExo(fdays[i])) )
103 sigma = cov(M) #NOTE: robust covariance is way too slow
104 sigma_inv = solve(sigma) #TODO: use pseudo-inverse if needed?
106 # Distances from last observed day to days in the past
107 distances2 = rep(NA, nrow(M)-1)
110 delta = M[1,] - M[i,]
111 distances2[i-1] = delta %*% sigma_inv %*% delta
114 ppv <- sort(distances2, index.return=TRUE)$ix[1:10] #..............
119 if (simtype == "exo")
121 else if (simtype == "endo")
124 simils_endo * simils_exo
126 prediction = rep(0, horizon)
127 for (i in seq_along(fdays))
128 prediction = prediction + similarities[i] * data$getSerie(fdays[i]+1)[1:horizon]
129 prediction = prediction / sum(similarities, na.rm=TRUE)
133 private$.params$weights <- similarities
134 private$.params$indices <- fdays
135 private$.params$window <- h