investigations on Bruno vs. talweg: a shift overved, and neighbors differ (a bit...
[talweg.git] / pkg / R / F_Neighbors.R
index 02536eb..d63c177 100644 (file)
@@ -61,8 +61,8 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
                        if (!opera)
                                tdays = setdiff(tdays, today) #always exclude current day
 
-                       # Shortcut if window is known
-                       if (hasArg("window"))
+                       # Shortcut if window is known or local==TRUE && simtype==none
+                       if (hasArg("window") || (local && simtype=="none"))
                        {
                                return ( private$.predictShapeAux(data, tdays, today, predict_from, horizon,
                                        local, list(...)$window, simtype, opera, TRUE) )
@@ -129,8 +129,8 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
 
                        if (local)
                        {
-                               # TODO: 60 == magic number
-                               tdays = getSimilarDaysIndices(today, data, limit=60, same_season=TRUE,
+                               # limit=Inf to not censor any day (TODO: finite limit? 60?)
+                               tdays = getSimilarDaysIndices(today, data, limit=Inf, same_season=TRUE,
                                        days_in=tdays_cut, operational=opera)
 #                              if (length(tdays) <= 1)
 #                                      return (NA)
@@ -146,6 +146,19 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
                                        }
                                        return ( data$getSerie(tdays[1])[predict_from:horizon] )
                                }
+                               max_neighbs = 10 #TODO: 12 = arbitrary number
+                               if (length(tdays) > max_neighbs)
+                               {
+                                       distances2 <- .computeDistsEndo(data, today, tdays, predict_from)
+                                       ordering <- order(distances2)
+                                       tdays <- tdays[ ordering[1:max_neighbs] ]
+
+                                       print("VVVVV")
+                                       print(sort(distances2)[1:max_neighbs])
+                                       print(integerIndexToDate(today,data))
+                                       print(lapply(tdays,function(i) integerIndexToDate(i,data)))
+                                       print(rbind(data$getSeries(tdays-1), data$getSeries(tdays)))
+                               }
                        }
                        else
                                tdays = tdays_cut #no conditioning
@@ -156,19 +169,9 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
                                window_endo = ifelse(simtype=="mix", window[1], window)
 
                                # Distances from last observed day to selected days in the past
+                               # TODO: redundant computation if local==TRUE
                                distances2 <- .computeDistsEndo(data, today, tdays, predict_from)
 
-                               if (local)
-                               {
-                                       max_neighbs = 12 #TODO: 12 = arbitrary number
-                                       if (length(distances2) > max_neighbs)
-                                       {
-                                               ordering <- order(distances2)
-                                               tdays <- tdays[ ordering[1:max_neighbs] ]
-                                               distances2 <- distances2[ ordering[1:max_neighbs] ]
-                                       }
-                               }
-
                                simils_endo <- .computeSimils(distances2, window_endo)
                        }
 
@@ -284,7 +287,8 @@ NeighborsForecaster = R6::R6Class("NeighborsForecaster",
        sapply(tdays, function(i) {
                delta = lastSerie - c(data$getSerie(i-1),
                        data$getSerie(i)[if (predict_from>=2) 1:(predict_from-1) else c()])
-               sqrt(mean(delta^2))
+#              sqrt(mean(delta^2))
+               sqrt(sum(delta^2))
        })
 }