intive-DataScience / tbats

BATS and TBATS forecasting methods
MIT License
178 stars 19 forks source link

Bug: Box-cox transformation of model parameters when they can be negative #12

Closed lukedyer-peak closed 1 year ago

lukedyer-peak commented 4 years ago

It seems that sometimes the model parameters can be negative but we are trying to apply a box-cox transformation to them (c.f. https://github.com/intive-DataScience/tbats/blob/ebb914d80c1b472f0756478073fd68ccbbcd3eae/tbats/abstract/ModelParams.py#L213-L216).

When I find what the min value that has been passed to the box-cox transformation it is min(y) = -21.....

The min value of my timeseries is 1 and has length 104.

The model is: TBATS(seasonal_periods=[52], n_jobs=1, box_cox_bounds=(-2, 2))

See the following traceback:

Traceback (most recent call last):
....
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/Estimator.py", line 98, in fit
    best_model = self._do_fit(y)
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/tbats/TBATS.py", line 76, in _do_fit
    seasonal_model = self._choose_model_from_possible_component_settings(y, components_grid=components_grid)
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/Estimator.py", line 144, in _choose_model_from_possible_component_settings
    models = pool.map(self._case_fit, components_grid)
  File "/opt/conda/lib/python3.6/multiprocessing/pool.py", line 266, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File "/opt/conda/lib/python3.6/multiprocessing/pool.py", line 644, in get
    raise self._value
  File "/opt/conda/lib/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/opt/conda/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/Estimator.py", line 131, in _case_fit
    return case.fit(self._y)
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/Case.py", line 47, in fit
    best_model = self.fit_initial_model(y)
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/tbats/Case.py", line 22, in fit_initial_model
    return self.fit_case(y, self.components.without_arma())
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/Case.py", line 84, in fit_case
    return self.fit_with_starting_params(y, starting_params)
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/Case.py", line 102, in fit_with_starting_params
    return optimization.optimize(y, model_params).optimal_model()
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/ParamsOptimizer.py", line 105, in optimize
    'fatol': 1e-8,
  File "/home/{user}/python-dist/lib/python3.6/site-packages/scipy/optimize/_minimize.py", line 598, in minimize
    return _minimize_neldermead(fun, x0, args, callback, **options)
  File "/home/{user}/python-dist/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 602, in _minimize_neldermead
    fxr = func(xr)
  File "/home/{user}/python-dist/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 327, in function_wrapper
    return function(*(wrapper_args + args))
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/ParamsOptimizer.py", line 120, in _scale_and_calculate_likelihood
    return self._calculate_likelihood(optimization_vector)
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/ParamsOptimizer.py", line 126, in _calculate_likelihood
    params = self._starting_params.with_vector_values(optimization_vector)
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/abstract/ModelParams.py", line 215, in with_vector_values
    boxcox
  File "/home/{user}/python-dist/lib/python3.6/site-packages/tbats/transformation/BoxCox.py", line 22, in boxcox
    raise error.InputArgsException('y must have only positive values for box-cox transformation.')
tbats.error.InputArgsException.InputArgsException: y must have only positive values for box-cox transformation.

Package versions:

tbats==1.0.9
statsmodels==0.11.0
pmdarima==1.5.2

Let me know if you want to know anything else.

cotterpl commented 4 years ago

I am writing just to notify you that I see the issue. It lies not in input data not being positive but starting state used for calculations (and being calculated inside TBATS) being negative. I am considering on how to approach it.

popcornn1 commented 2 years ago

Hello, anything changed with this issue? I have the same problem

cotterpl commented 2 years ago

Unfortunately it is still not fixed.

cotterpl commented 1 year ago

I have just released a new version with a fix.