robjhyndman / forecast

Forecasting Functions for Time Series and Linear Models
http://pkg.robjhyndman.com/forecast
1.13k stars 342 forks source link

Error in optim - forecast::tbats() #806

Closed mcsage closed 5 years ago

mcsage commented 5 years ago

Hi, We are facing an issue with forecast::tbats() throwing an error under Linux but not under Windows.

forecast pkg version: 8.7

Linux System:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS/LAPACK: /usr/lib/libopenblasp-r0.2.19.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1        magrittr_1.5      tidyselect_0.2.5  forecast_8.7     
 [5] munsell_0.5.0     colorspace_1.4-1  lattice_0.20-38   R6_2.4.0         
 [9] quadprog_1.5-7    rlang_0.4.0       TTR_0.23-4        dplyr_0.8.3      
[13] xts_0.11-2        quantmod_0.4-15   nnet_7.3-12       parallel_3.6.1   
[17] grid_3.6.1        nlme_3.1-140      timeDate_3043.102 gtable_0.3.0     
[21] urca_1.3-0        tseries_0.10-47   lazyeval_0.2.2    assertthat_0.2.1 
[25] lmtest_0.9-37     tibble_2.1.3      crayon_1.3.4      purrr_0.3.2      
[29] ggplot2_3.2.0     curl_3.3          glue_1.3.1        fracdiff_1.4-2   
[33] compiler_3.6.1    pillar_1.4.2      scales_1.0.0      pkgconfig_2.0.2  
[37] zoo_1.8-6        
> forecast::tbats(y=c(1:10))
Error in optim(par = param.vector$vect, fn = calcLikelihoodNOTransformed,  : 
  function cannot be evaluated at initial parameters

Windows:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1        magrittr_1.5      forecast_8.7      munsell_0.5.0     colorspace_1.4-1  lattice_0.20-38   rlang_0.4.0       quadprog_1.5-7    TTR_0.23-4        tools_3.6.1       xts_0.11-2        nnet_7.3-12      
[13] parallel_3.6.1    quantmod_0.4-15   grid_3.6.1        nlme_3.1-140      timeDate_3043.102 gtable_0.3.0      urca_1.3-0        tseries_0.10-47   lazyeval_0.2.2    lmtest_0.9-37     tibble_2.1.3      crayon_1.3.4     
[25] ggplot2_3.2.0     curl_3.3          fracdiff_1.4-2    compiler_3.6.1    pillar_1.4.2      scales_1.0.0      pkgconfig_2.0.2   zoo_1.8-6        
> forecast::tbats(y=c(1:10))
BATS(1, {0,0}, 1, -)

Call: forecast::tbats(y = c(1:10))

Parameters
  Alpha: 0.19
  Beta: 0.05
  Damping Parameter: 1

Seed States:
              [,1]
[1,] -1.025773e-15
[2,]  1.000000e+00

Sigma: 1.647101e-15
AIC: -649.7693

Adding a rnorm runs without error under Linux:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS/LAPACK: /usr/lib/libopenblasp-r0.2.19.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C             
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1        magrittr_1.5      tidyselect_0.2.5  forecast_8.7     
 [5] munsell_0.5.0     colorspace_1.4-1  lattice_0.20-38   R6_2.4.0         
 [9] quadprog_1.5-7    rlang_0.4.0       TTR_0.23-4        dplyr_0.8.3      
[13] xts_0.11-2        quantmod_0.4-15   nnet_7.3-12       parallel_3.6.1   
[17] grid_3.6.1        nlme_3.1-140      timeDate_3043.102 gtable_0.3.0     
[21] urca_1.3-0        tseries_0.10-47   lazyeval_0.2.2    assertthat_0.2.1 
[25] lmtest_0.9-37     tibble_2.1.3      crayon_1.3.4      purrr_0.3.2      
[29] ggplot2_3.2.0     curl_3.3          glue_1.3.1        fracdiff_1.4-2   
[33] compiler_3.6.1    pillar_1.4.2      scales_1.0.0      pkgconfig_2.0.2  
[37] zoo_1.8-6        
> forecast::tbats(y=c(1:10)+rnorm(10))
BATS(0.474, {0,0}, 1, -)

Call: forecast::tbats(y = c(1:10) + rnorm(10))

Parameters
  Lambda: 0.474093
  Alpha: -0.01865639
  Beta: 8.572875e-05
  Damping Parameter: 1

Seed States:
           [,1]
[1,] -0.3812095
[2,]  0.4929143
attr(,"lambda")
[1] 0.4740934

Sigma: 0.3244676
AIC: 25.82143

Is this a known bug or is this an intended behavior?

robjhyndman commented 5 years ago

The issue is probably the use of OpenBLAS on Linux which causes an error due to the perfect linear fit. Windows will be using a different library for the regression which handles the degenerate model better in this case.

If you see this sort of data, I would suggest testing for perfect linearity before running tbats. For example, you could use ets() which would give sigma=0 and AIC=-Inf in this case, indicating the model is degenerate. In such cases, there is no need to try to fit a tbats() model.

mcsage commented 5 years ago

Thanks for the explanation. I was just wondering why there is a difference at the outcome doing the same thing on both platforms.