danigiro / FoReco

Forecast Reconciliation - Classical (bottom-up), optimal and heuristic combination forecast reconciliation procedures for cross-sectional, temporal, and cross-temporal linearly constrained time series.
https://danigiro.github.io/FoReco/
GNU General Public License v3.0
30 stars 5 forks source link

`htsrec` function doesn't work as intended when `nn = TRUE` and `nn_type = "osqp"` #5

Closed ShanikaLW closed 3 months ago

ShanikaLW commented 4 months ago

Please see below.

library(hts)
#> Loading required package: forecast
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
library(FoReco)
#> Warning: package 'FoReco' was built under R version 4.3.3
#> Loading required package: Matrix
#> Loading required package: osqp
#> Warning: package 'osqp' was built under R version 4.3.3
#> ──────────────────────────────────────────────────────────────── FoReco 0.2.6 ──
set.seed(123)
abc <- ts(0.5 + matrix(sort(rnorm(200)), ncol = 4, nrow = 50))
nodes <- list(2, c(2, 2))
y <- hts(abc, nodes = nodes)
#> Since argument characters are not specified, the default labelling system is used.
h <- 12
ally <- aggts(y)
n <- nrow(ally)
p <- ncol(ally)
allf <- matrix(NA,nrow = h,ncol = ncol(ally))
for(i in 1:p)
{
  fit <- auto.arima(ally[, i])
  allf[, i] <- forecast(fit, h = h)$mean
}
htsrec(allf, comb = "ols", C = smatrix(y)[1:3,], keep = "recf", nn = FALSE)
#>       serie1     serie2   serie3     serie4    serie5    serie6   serie7
#> h1  5.294030 0.28844210 5.005588 -0.1386732 0.4271153 1.0532083 3.952380
#> h2  5.473805 0.26662846 5.207177 -0.1490871 0.4157156 1.0434417 4.163735
#> h3  5.653610 0.24486500 5.408745 -0.1597520 0.4046170 1.0336652 4.375080
#> h4  5.833394 0.22306733 5.610327 -0.1702386 0.3933059 1.0238954 4.586432
#> h5  6.013193 0.20129296 5.811900 -0.1808519 0.3821448 1.0141210 4.797779
#> h6  6.192981 0.17950274 6.013479 -0.1913751 0.3708778 1.0043498 5.009129
#> h7  6.372777 0.15772329 6.215053 -0.2019623 0.3596856 0.9945764 5.220477
#> h8  6.552567 0.13593653 6.416631 -0.2125041 0.3484406 0.9848045 5.431826
#> h9  6.732361 0.11415473 6.618206 -0.2230781 0.3372329 0.9750316 5.643175
#> h10 6.912153 0.09236957 6.819783 -0.2336292 0.3259988 0.9652594 5.854524
#> h11 7.091946 0.07058668 7.021359 -0.2441967 0.3147833 0.9554867 6.065873
#> h12 7.271738 0.04880226 7.222936 -0.2547525 0.3035547 0.9457143 6.277222
combinef(allf, nodes = nodes, nonnegative = FALSE, keep = "all")
#>           [,1]       [,2]     [,3]       [,4]      [,5]      [,6]     [,7]
#>  [1,] 5.294030 0.28844210 5.005588 -0.1386732 0.4271153 1.0532083 3.952380
#>  [2,] 5.473805 0.26662846 5.207177 -0.1490871 0.4157156 1.0434417 4.163735
#>  [3,] 5.653610 0.24486500 5.408745 -0.1597520 0.4046170 1.0336652 4.375080
#>  [4,] 5.833394 0.22306733 5.610327 -0.1702386 0.3933059 1.0238954 4.586432
#>  [5,] 6.013193 0.20129296 5.811900 -0.1808519 0.3821448 1.0141210 4.797779
#>  [6,] 6.192981 0.17950274 6.013479 -0.1913751 0.3708778 1.0043498 5.009129
#>  [7,] 6.372777 0.15772329 6.215053 -0.2019623 0.3596856 0.9945764 5.220477
#>  [8,] 6.552567 0.13593653 6.416631 -0.2125041 0.3484406 0.9848045 5.431826
#>  [9,] 6.732361 0.11415473 6.618206 -0.2230781 0.3372329 0.9750316 5.643175
#> [10,] 6.912153 0.09236957 6.819783 -0.2336292 0.3259988 0.9652594 5.854524
#> [11,] 7.091946 0.07058668 7.021359 -0.2441967 0.3147833 0.9554867 6.065873
#> [12,] 7.271738 0.04880226 7.222936 -0.2547525 0.3035547 0.9457143 6.277222
htsrec(allf, comb = "ols", C = smatrix(y)[1:3,], keep = "recf", nn = TRUE)
#> Warning: OSQP flag -4 OSQP pri_res 1.89503316505579e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89476372725039e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89535603567492e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89519846571784e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89522438631684e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89534762284893e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89524621418968e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.8955886389449e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89518414117629e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89527509064646e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89532261174463e-06
#> Warning: OSQP flag -4 OSQP pri_res 1.89510910786339e-06
#> $recf
#>         serie1     serie2     serie3     serie4     serie5     serie6
#> h1  2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h2  2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h3  2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h4  2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h5  2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h6  2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h7  2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h8  2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h9  2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h10 2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h11 2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#> h12 2143289344 2143289344 2143289344 2143289344 2143289344 2143289344
#>         serie7
#> h1  2143289344
#> h2  2143289344
#> h3  2143289344
#> h4  2143289344
#> h5  2143289344
#> h6  2143289344
#> h7  2143289344
#> h8  2143289344
#> h9  2143289344
#> h10 2143289344
#> h11 2143289344
#> h12 2143289344
#> 
#> $info
#>    obj_val  run_time iter      pri_res status status_polish
#> 1   -1e+30 0.0000747   25 1.895033e-06     -4             0
#> 2   -1e+30 0.0000504   25 1.894764e-06     -4             0
#> 3   -1e+30 0.0000567   25 1.895356e-06     -4             0
#> 4   -1e+30 0.0000459   25 1.895198e-06     -4             0
#> 5   -1e+30 0.0000467   25 1.895224e-06     -4             0
#> 6   -1e+30 0.0000534   25 1.895348e-06     -4             0
#> 7   -1e+30 0.0000527   25 1.895246e-06     -4             0
#> 8   -1e+30 0.0000487   25 1.895589e-06     -4             0
#> 9   -1e+30 0.0000731   25 1.895184e-06     -4             0
#> 10  -1e+30 0.0000542   25 1.895275e-06     -4             0
#> 11  -1e+30 0.0000339   25 1.895323e-06     -4             0
#> 12  -1e+30 0.0001343   25 1.895109e-06     -4             0
combinef(allf, nodes = nodes, nonnegative = TRUE, keep = "all")
#> Warning in combinef(allf, nodes = nodes, nonnegative = TRUE, keep = "all"):
#> Negative base forecasts are truncated to zero.
#> Time Series:
#> Start = 1 
#> End = 12 
#> Frequency = 1 
#>       Total         A        B AA        AB        BA       BB
#>  1 5.326032 0.3417779 4.984254  0 0.3417779 1.0425411 3.941713
#>  2 5.508210 0.3239697 5.184240  0 0.3239697 1.0319735 4.152267
#>  3 5.690476 0.3063081 5.384168  0 0.3063081 1.0213765 4.362791
#>  4 5.872680 0.2885437 5.584136  0 0.2885437 1.0108002 4.573336
#>  5 6.054928 0.2708514 5.784076  0 0.2708514 1.0002093 4.783867
#>  6 6.237145 0.2531085 5.984036  0 0.2531085 0.9896286 4.994408
#>  7 6.419383 0.2354011 6.183982  0 0.2354011 0.9790409 5.204941
#>  8 6.601607 0.2176689 6.383938  0 0.2176689 0.9684580 5.415480
#>  9 6.783841 0.1999540 6.583887  0 0.1999540 0.9578717 5.626015
#> 10 6.966067 0.1822270 6.783840  0 0.1822270 0.9472879 5.836553
#> 11 7.148299 0.1645085 6.983791  0 0.1645085 0.9367023 6.047088
#> 12 7.330527 0.1467840 7.183743  0 0.1467840 0.9261179 6.257625

Created on 2024-05-23 with reprex v2.1.0

danigiro commented 4 months ago

Hi, sorry for the late response. This problem seems to be due to how OSQP translates the dtCMatrix-class internally. The problem does not seem to occur when we use type = “S”. I have updated the current version here on GitHub (0.2.6.9000) and now it works. However, a new version of FoReco (1.0) will be released soon.

htsrec(allf, comb = "ols", C = smatrix(y)[1:3,], keep = "recf", nn = TRUE)
#> $recf
#>       serie1    serie2   serie3       serie4    serie5    serie6   serie7
#> h1  5.326032 0.3417779 4.984254 0.000000e+00 0.3417779 1.0425411 3.941713
#> h2  5.508210 0.3239697 5.184240 0.000000e+00 0.3239697 1.0319735 4.152267
#> h3  5.690476 0.3063081 5.384168 8.422724e-26 0.3063081 1.0213765 4.362791
#> h4  5.872680 0.2885437 5.584136 0.000000e+00 0.2885437 1.0108002 4.573336
#> h5  6.054928 0.2708514 5.784076 1.750346e-23 0.2708514 1.0002093 4.783867
#> h6  6.237145 0.2531085 5.984036 0.000000e+00 0.2531085 0.9896286 4.994408
#> h7  6.419383 0.2354011 6.183982 2.455300e-23 0.2354011 0.9790409 5.204941
#> h8  6.601607 0.2176689 6.383938 0.000000e+00 0.2176689 0.9684580 5.415480
#> h9  6.783841 0.1999540 6.583887 3.736331e-24 0.1999540 0.9578717 5.626015
#> h10 6.966067 0.1822270 6.783840 0.000000e+00 0.1822270 0.9472879 5.836553
#> h11 7.148299 0.1645085 6.983791 0.000000e+00 0.1645085 0.9367023 6.047088
#> h12 7.330527 0.1467840 7.183743 0.000000e+00 0.1467840 0.9261179 6.257625

#> $info
#>      obj_val   run_time iter      pri_res status status_polish
#> 1  -35.03351 6.3541e-05   25 1.317723e-24      1             1
#> 2  -37.86646 5.1332e-05   25 2.668806e-25      1             1
#> 3  -40.81779 4.7416e-05   25 8.881784e-16      1             1
#> 4  -43.88729 3.9958e-05   25 2.668805e-25      1             1
#> 5  -47.07510 3.8832e-05   25 8.881784e-16      1             1
#> 6  -50.38113 3.8249e-05   25 8.006418e-25      1             1
#> 7  -53.80544 3.8375e-05   25 8.881784e-16      1             1
#> 8  -57.34798 3.7999e-05   25 2.562053e-23      1             1
#> 9  -61.00879 3.8624e-05   25 8.881784e-16      1             1
#> 10 -64.78785 3.7291e-05   25 0.000000e+00      1             1
#> 11 -68.68516 3.6582e-05   25 3.736326e-24      1             1
#> 12 -72.70073 3.6249e-05   25 8.881784e-16      1             1