fix window bounds
[talweg.git] / pkg / R / F_Neighbors2.R
index 69e69dc..60916b4 100644 (file)
@@ -56,12 +56,12 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                        if (simtype != "endo")
                        {
                                h_best_exo = optimize(
-                                       errorOnLastNdays, c(0,10), kernel=kernel, simtype="exo")$minimum
+                                       errorOnLastNdays, c(0,7), kernel=kernel, simtype="exo")$minimum
                        }
                        if (simtype != "exo")
                        {
                                h_best_endo = optimize(
-                                       errorOnLastNdays, c(0,10), kernel=kernel, simtype="endo")$minimum
+                                       errorOnLastNdays, c(0,7), kernel=kernel, simtype="endo")$minimum
                        }
 
                        if (simtype == "endo")
@@ -97,16 +97,25 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                                return (NA)
                        levelToday = data$getLevel(today)
                        distances = sapply(fdays, function(i) abs(data$getLevel(i)-levelToday))
-                       dist_thresh = 1
+                       #TODO: 2, 3, 5, 10 magic numbers here...
+                       dist_thresh = 2
+                       min_neighbs = min(3,length(fdays))
                        repeat
                        {
                                same_pollution = (distances <= dist_thresh)
-                               if (sum(same_pollution) >= 2) #will eventually happen
+                               nb_neighbs = sum(same_pollution)
+                               if (nb_neighbs >= min_neighbs) #will eventually happen
                                        break
-                               dist_thresh = dist_thresh + 1
+                               dist_thresh = dist_thresh + 3
                        }
                        fdays = fdays[same_pollution]
-                       if (length(fdays) == 1)
+                       max_neighbs = 10
+                       if (nb_neighbs > max_neighbs)
+                       {
+                               # Keep only max_neighbs closest neighbors
+                               fdays = fdays[ sort(distances[same_pollution],index.return=TRUE)$ix[1:max_neighbs] ]
+                       }
+                       if (length(fdays) == 1) #the other extreme...
                        {
                                if (final_call)
                                {
@@ -194,11 +203,11 @@ Neighbors2Forecaster = R6::R6Class("Neighbors2Forecaster",
                                        simils_endo
                                else #mix
                                        simils_endo * simils_exo
+                       similarities = similarities / sum(similarities)
 
                        prediction = rep(0, horizon)
                        for (i in seq_along(fdays))
                                prediction = prediction + similarities[i] * data$getCenteredSerie(fdays[i]+1)[1:horizon]
-                       prediction = prediction / sum(similarities, na.rm=TRUE)
 
                        if (final_call)
                        {