X-Git-Url: https://git.auder.net/?p=talweg.git;a=blobdiff_plain;f=pkg%2Ftests%2Ftestthat%2Ftest-Forecaster.R;h=09b6f0ae2c62aa66b0ffd43c74ebbe0f9e874d80;hp=9b3eaa0e66c5f849cf8fa26815774d4b8cf51558;hb=c36568faefc97bb417d355e2084398c1ad1acf92;hpb=3ddf1c12af0c167fe7d3bb59e63258550270cfc5 diff --git a/pkg/tests/testthat/test-Forecaster.R b/pkg/tests/testthat/test-Forecaster.R index 9b3eaa0..09b6f0a 100644 --- a/pkg/tests/testthat/test-Forecaster.R +++ b/pkg/tests/testthat/test-Forecaster.R @@ -12,25 +12,25 @@ pred_order = c(7,1:6) #will facilitate tests test_that("Average method behave as expected", { - pred00_z = computeForecast(data00, indices, "Average", "Zero", Inf, 24) + pred00_z = computeForecast(data00, indices, "Average", "Zero", Inf, 24) pred00_p = computeForecast(data00, indices, "Average", "Persistence", Inf, 24) for (i in 1:7) { #zero jump: should predict true values minus 1 - expect_equal( pred00_z$getSerie(i), rep(pred_order[i],24) ) + expect_equal( pred00_z$getForecast(i), rep(pred_order[i],24) ) #persistence jump == 1: should predict true values - expect_equal( pred00_p$getSerie(i), rep(i,24) ) + expect_equal( pred00_p$getForecast(i), rep(i,24) ) } #NOTE: days become #1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 (14h-->0h then 1h-->13h) #No jump between days, thus zero and persistence are equivalent (and correct) - pred13_z = computeForecast(data13, indices, "Average", "Zero", Inf, 24) + pred13_z = computeForecast(data13, indices, "Average", "Zero", Inf, 24) pred13_p = computeForecast(data13, indices, "Average", "Persistence", Inf, 24) for (i in 1:7) { - expect_equal( pred13_z$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) - expect_equal( pred13_p$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) + expect_equal( pred13_z$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) ) + expect_equal( pred13_p$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) ) } #A few extra checks @@ -44,60 +44,60 @@ test_that("Persistence method behave as expected", { #Situation A: +Zero; (generally) correct if jump, wrong otherwise pred00_sd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24, - same_day=TRUE) + ncores=1, same_day=TRUE) pred00_dd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24, - same_day=FALSE) + ncores=1, same_day=FALSE) for (i in 1:7) { - expect_equal(pred00_sd$getSerie(i), rep(pred_order[i],24)) - expect_equal(pred00_dd$getSerie(i), rep(pred_order[i],24)) + expect_equal(pred00_sd$getForecast(i), rep(pred_order[i],24)) + expect_equal(pred00_dd$getForecast(i), rep(pred_order[i],24)) } pred13_sd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, - same_day=TRUE) + ncores=1, same_day=TRUE) pred13_dd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, - same_day=FALSE) + ncores=1, same_day=FALSE) for (i in 2:6) { - expect_equal(pred13_sd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) - expect_equal(pred13_dd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) + expect_equal(pred13_sd$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) ) + expect_equal(pred13_dd$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) ) } #boundaries are special cases: OK if same day, quite wrong otherwise - expect_equal(pred13_sd$getSerie(1), c( rep(1,11), rep(2,13) ) ) - expect_equal(pred13_dd$getSerie(1), c( rep(1,11), rep(-5,13) ) ) - expect_equal(pred13_sd$getSerie(7), c( rep(7,11), rep(1,13) ) ) - expect_equal(pred13_dd$getSerie(7), c( rep(7,11), rep(8,13) ) ) + expect_equal(pred13_sd$getForecast(1), c( rep(1,11), rep(2,13) ) ) + expect_equal(pred13_dd$getForecast(1), c( rep(1,11), rep(-5,13) ) ) + expect_equal(pred13_sd$getForecast(7), c( rep(7,11), rep(1,13) ) ) + expect_equal(pred13_dd$getForecast(7), c( rep(7,11), rep(8,13) ) ) #Situation B: +Persistence, (generally) correct pred00_sd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24, - same_day=TRUE) + ncores=1, same_day=TRUE) pred00_dd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24, - same_day=FALSE) + ncores=1, same_day=FALSE) for (i in 3:7) { - expect_equal(pred00_sd$getSerie(i), rep(i,24)) - expect_equal(pred00_dd$getSerie(i), rep(i,24)) + expect_equal(pred00_sd$getForecast(i), rep(i,24)) + expect_equal(pred00_dd$getForecast(i), rep(i,24)) } #boundaries are special cases: OK if same day, quite wrong otherwise - expect_equal(pred00_sd$getSerie(1), rep(1,24) ) - expect_equal(pred00_dd$getSerie(1), rep(8,24) ) - expect_equal(pred00_sd$getSerie(2), rep(2,24) ) - expect_equal(pred00_dd$getSerie(2), rep(-5,24) ) + expect_equal(pred00_sd$getForecast(1), rep(1,24) ) + expect_equal(pred00_dd$getForecast(1), rep(8,24) ) + expect_equal(pred00_sd$getForecast(2), rep(2,24) ) + expect_equal(pred00_dd$getForecast(2), rep(-5,24) ) pred13_sd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24, - same_day=TRUE) + ncores=1, same_day=TRUE) pred13_dd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24, - same_day=FALSE) + ncores=1, same_day=FALSE) for (i in 2:6) { - expect_equal(pred13_sd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) - expect_equal(pred13_dd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) + expect_equal(pred13_sd$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) ) + expect_equal(pred13_dd$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) ) } #boundaries are special cases: OK if same day, quite wrong otherwise - expect_equal(pred13_sd$getSerie(1), c( rep(1,11), rep(2,13) ) ) - expect_equal(pred13_dd$getSerie(1), c( rep(1,11), rep(-5,13) ) ) - expect_equal(pred13_sd$getSerie(7), c( rep(7,11), rep(1,13) ) ) - expect_equal(pred13_dd$getSerie(7), c( rep(7,11), rep(8,13) ) ) + expect_equal(pred13_sd$getForecast(1), c( rep(1,11), rep(2,13) ) ) + expect_equal(pred13_dd$getForecast(1), c( rep(1,11), rep(-5,13) ) ) + expect_equal(pred13_sd$getForecast(7), c( rep(7,11), rep(1,13) ) ) + expect_equal(pred13_dd$getForecast(7), c( rep(7,11), rep(8,13) ) ) #A few extra checks expect_equal( pred00_sd$getIndexInData(3), dateIndexToInteger("2007-04-03",data00) ) @@ -110,37 +110,25 @@ test_that("Neighbors method behave as expected", { #Situation A: +Zero; correct if jump, wrong otherwise pred00 = computeForecast(data00, indices, "Neighbors", "Zero", Inf, 24, - simtype="mix") + simtype="mix", local=FALSE) for (i in 1:7) - expect_equal(pred00$getSerie(i), rep(pred_order[i],24)) + expect_equal(pred00$getForecast(i), rep(pred_order[i],24)) pred13 = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, - simtype="mix") + simtype="mix", local=FALSE) for (i in 1:7) - expect_equal(pred13$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) + expect_equal(pred13$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) ) - #Situation B: +Neighbors, always predict bad (small, averaged) jump - pred00 = computeForecast(data00, indices, "Neighbors", "Neighbors", Inf, 24, - simtype="endo") - #Concerning weights, there are 12+(1 if i>=2) gaps at -6 and 90-12+(i-2 if i>=3) gaps - #at 1. Thus, predicted jump is respectively - # (12*-6+78)/90 = 0.06666667 - # (13*-6+78)/91 = 0 - # (13*-6+79)/92 = 0.01086957 - # (13*-6+80)/93 = 0.02150538 - # (13*-6+81)/94 = 0.03191489 - # (13*-6+82)/95 = 0.04210526 - # (13*-6+83)/96 = 0.05208333 - jumps = c(0.06666667, 0, 0.01086957, 0.02150538, 0.03191489, 0.04210526, 0.05208333) - for (i in 1:7) - expect_equal(pred00$getSerie(i), rep(pred_order[i]+jumps[i],24)) - - #Next lines commented out because too unpredictable results - #(tendency to flatten everything...) + #Situation B: +Neighbors == too difficult to eval in a unit test +# pred00 = computeForecast(data00, indices, "Neighbors", "Neighbors", Inf, 24, +# simtype="endo", local=FALSE) +# jumps = ... +# for (i in 1:7) +# expect_equal(pred00$getForecast(i), rep(pred_order[i]+jumps[i],24)) # pred13 = computeForecast(data13, indices, "Neighbors", "Neighbors", Inf, 24, -# simtype="endo") +# simtype="endo", local=FALSE) # for (i in 1:7) -# expect_equal(pred13$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) ) +# expect_equal(pred13$getForecast(i), c( rep(i,11), rep(i%%7+1,13) ) ) #A few extra checks expect_equal( pred00$getIndexInData(1), dateIndexToInteger("2007-04-01",data00) )