e29f19655f3f8f95dac46ae046789acff28f75a5
[talweg.git] / pkg / tests / testthat / test.Forecaster.R
1 context("Check that forecasters behave as expected")
2
3 ts_data = system.file("testdata","ts_test.csv",package="talweg")
4 exo_data = system.file("testdata","exo_test.csv",package="talweg")
5 data00 <<- getData(ts_data, exo_data, input_tz="GMT", date_format="%Y-%m-%d %H:%M",
6 working_tz="GMT", predict_at=0, limit=Inf)
7 data13 <<- getData(ts_data, exo_data, input_tz="GMT", date_format="%Y-%m-%d %H:%M",
8 working_tz="GMT", predict_at=13, limit=Inf)
9 #Forecast at sunday to saturday (series 7 to 1), for monday to sunday (series 1 to 7)
10 indices <<- seq(as.Date("2007-04-01"),as.Date("2007-04-07"),"days")
11 pred_order = c(7,1:6) #will facilitate tests
12
13 test_that("Average method behave as expected",
14 {
15 pred00_z = computeForecast(data00, indices, "Average", "Zero", Inf, 24)
16 pred00_p = computeForecast(data00, indices, "Average", "Persistence", Inf, 24)
17 for (i in 1:7)
18 {
19 #zero jump: should predict true values minus 1
20 expect_equal( pred00_z$getSerie(i), rep(pred_order[i],24) )
21 #persistence jump == 1: should predict true values
22 expect_equal( pred00_p$getSerie(i), rep(i,24) )
23 }
24
25 #NOTE: days become
26 #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)
27 #No jump between days, thus zero and persistence are equivalent (and correct)
28 pred13_z = computeForecast(data13, indices, "Average", "Zero", Inf, 24)
29 pred13_p = computeForecast(data13, indices, "Average", "Persistence", Inf, 24)
30 for (i in 1:7)
31 {
32 expect_equal( pred13_z$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
33 expect_equal( pred13_p$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
34 }
35
36 #A few extra checks
37 expect_equal( pred00_p$getIndexInData(2), dateIndexToInteger("2007-04-02",data00) )
38 expect_equal( pred00_z$getIndexInData(2), dateIndexToInteger("2007-04-02",data00) )
39 expect_equal( pred13_p$getIndexInData(5), dateIndexToInteger("2007-04-05",data13) )
40 expect_equal( pred13_z$getIndexInData(5), dateIndexToInteger("2007-04-05",data13) )
41 })
42
43 test_that("Persistence method behave as expected",
44 {
45 #Situation A: +Zero; (generally) correct if jump, wrong otherwise
46 pred00_sd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24, same_day=TRUE)
47 pred00_dd = computeForecast(data00, indices, "Persistence", "Zero", Inf, 24, same_day=FALSE)
48 for (i in 1:7)
49 {
50 expect_equal(pred00_sd$getSerie(i), rep(pred_order[i],24))
51 expect_equal(pred00_dd$getSerie(i), rep(pred_order[i],24))
52 }
53
54 pred13_sd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, same_day=TRUE)
55 pred13_dd = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, same_day=FALSE)
56 for (i in 2:6)
57 {
58 expect_equal(pred13_sd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
59 expect_equal(pred13_dd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
60 }
61 #boundaries are special cases: OK if same day, quite wrong otherwise
62 expect_equal(pred13_sd$getSerie(1), c( rep(1,11), rep(2,13) ) )
63 expect_equal(pred13_dd$getSerie(1), c( rep(1,11), rep(-5,13) ) )
64 expect_equal(pred13_sd$getSerie(7), c( rep(7,11), rep(1,13) ) )
65 expect_equal(pred13_dd$getSerie(7), c( rep(7,11), rep(8,13) ) )
66
67 #Situation B: +Persistence, (generally) correct
68 pred00_sd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24, same_day=TRUE)
69 pred00_dd = computeForecast(data00, indices, "Persistence", "Persistence", Inf, 24, same_day=FALSE)
70 for (i in 3:7)
71 {
72 expect_equal(pred00_sd$getSerie(i), rep(i,24))
73 expect_equal(pred00_dd$getSerie(i), rep(i,24))
74 }
75 #boundaries are special cases: OK if same day, quite wrong otherwise
76 expect_equal(pred00_sd$getSerie(1), rep(1,24) )
77 expect_equal(pred00_dd$getSerie(1), rep(8,24) )
78 expect_equal(pred00_sd$getSerie(2), rep(2,24) )
79 expect_equal(pred00_dd$getSerie(2), rep(-5,24) )
80
81 pred13_sd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24, same_day=TRUE)
82 pred13_dd = computeForecast(data13, indices, "Persistence", "Persistence", Inf, 24, same_day=FALSE)
83 for (i in 2:6)
84 {
85 expect_equal(pred13_sd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
86 expect_equal(pred13_dd$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
87 }
88 #boundaries are special cases: OK if same day, quite wrong otherwise
89 expect_equal(pred13_sd$getSerie(1), c( rep(1,11), rep(2,13) ) )
90 expect_equal(pred13_dd$getSerie(1), c( rep(1,11), rep(-5,13) ) )
91 expect_equal(pred13_sd$getSerie(7), c( rep(7,11), rep(1,13) ) )
92 expect_equal(pred13_dd$getSerie(7), c( rep(7,11), rep(8,13) ) )
93
94 #A few extra checks
95 expect_equal( pred00_sd$getIndexInData(3), dateIndexToInteger("2007-04-03",data00) )
96 expect_equal( pred00_dd$getIndexInData(6), dateIndexToInteger("2007-04-06",data00) )
97 expect_equal( pred13_sd$getIndexInData(3), dateIndexToInteger("2007-04-03",data13) )
98 expect_equal( pred13_dd$getIndexInData(6), dateIndexToInteger("2007-04-06",data13) )
99 })
100
101 test_that("Neighbors method behave as expected",
102 {
103 #Situation A: +Zero; correct if jump, wrong otherwise
104 pred00 = computeForecast(data00, indices, "Neighbors", "Zero", Inf, 24, simtype="mix")
105 for (i in 1:7)
106 expect_equal(pred00$getSerie(i), rep(pred_order[i],24))
107
108 pred13 = computeForecast(data13, indices, "Persistence", "Zero", Inf, 24, simtype="mix")
109 for (i in 1:7)
110 expect_equal(pred13$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
111
112 #Situation B: +Neighbors, always predict bad (small, averaged) jump
113 pred00 = computeForecast(data00, indices, "Neighbors", "Neighbors", Inf, 24, simtype="endo")
114 #Concerning weights, there are 12+(1 if i>=2) gaps at -6 and 90-12+(i-2 if i>=3) gaps at 1
115 #Thus, predicted jump is respectively
116 # (12*-6+78)/90 = 0.06666667
117 # (13*-6+78)/91 = 0
118 # (13*-6+79)/92 = 0.01086957
119 # (13*-6+80)/93 = 0.02150538
120 # (13*-6+81)/94 = 0.03191489
121 # (13*-6+82)/95 = 0.04210526
122 # (13*-6+83)/96 = 0.05208333
123 jumps = c(0.06666667, 0, 0.01086957, 0.02150538, 0.03191489, 0.04210526, 0.05208333)
124 for (i in 1:7)
125 expect_equal(pred00$getSerie(i), rep(pred_order[i]+jumps[i],24))
126
127 #Next lines commented out because too unpredictable results (tendency to flatten everything...)
128 # pred13 = computeForecast(data13, indices, "Neighbors", "Neighbors", Inf, 24, simtype="endo")
129 # for (i in 1:7)
130 # expect_equal(pred13$getSerie(i), c( rep(i,11), rep(i%%7+1,13) ) )
131
132 #A few extra checks
133 expect_equal( pred00$getIndexInData(1), dateIndexToInteger("2007-04-01",data00) )
134 expect_equal( pred00$getIndexInData(4), dateIndexToInteger("2007-04-04",data00) )
135 expect_equal( pred13$getIndexInData(1), dateIndexToInteger("2007-04-01",data13) )
136 expect_equal( pred13$getIndexInData(4), dateIndexToInteger("2007-04-04",data13) )
137 })