facebookresearch / Kats

Kats, a kit to analyze time series data, a lightweight, easy-to-use, generalizable, and extendable framework to perform time series analysis, from understanding the key statistics and characteristics, detecting change points and anomalies, to forecasting future trends.
MIT License
4.89k stars 536 forks source link

LinAlgError: Incompatible dimensions #86

Open VedAustin opened 3 years ago

VedAustin commented 3 years ago
from kats.models.theta import ThetaModel, ThetaParams
params = ThetaParams(m=7) # Weekly seasonality 
m = ThetaModel(data_ts, params) # data_ts -> daily data points
m.fit()

gives this error:

Optimization failed to converge. Check mle_retvals.

---------------------------------------------------------------------------
LinAlgError                               Traceback (most recent call last)
/var/folders/25/t45ty5ps70l5y9gww_k11zy40000gq/T/ipykernel_44596/687703454.py in <module>
----> 1 m.fit()

~/kats/lib/python3.9/site-packages/kats/models/theta.py in fit(self, **kwargs)
    119         # creating x and intercept variables to fit a straight line
    120         regr = np.vstack([np.arange(self.n), np.ones(self.n)]).T
--> 121         slope, _ = np.linalg.lstsq(regr, deseas_data.value.values)[0]
    122         # pyre-fixme[16]: `ThetaModel` has no attribute `drift`.
    123         self.drift = slope / 2

<__array_function__ internals> in lstsq(*args, **kwargs)

~/kats/lib/python3.9/site-packages/numpy/linalg/linalg.py in lstsq(a, b, rcond)
   2273     m2, n_rhs = b.shape[-2:]
   2274     if m != m2:
-> 2275         raise LinAlgError('Incompatible dimensions')
   2276 
   2277     t, result_t = _commonType(a, b)

LinAlgError: Incompatible dimensions

Ran the same data under Prophet model and it fits and forecasts w/o errors. Any pointers as to the source of the problem?

iamxiaodong commented 3 years ago

looks like the convergence is failed, do you mind sharing your time series data here so that we can take a look at? thanks.

VedAustin commented 3 years ago

sure .. here you go: data_gh.csv

VedAustin commented 3 years ago
model_params = EnsembleParams(
[
#     BaseModelParams("arima", arima.ARIMAParams(p=1, d=1, q=1)),
#     BaseModelParams("sarima", sarima.SARIMAParams(p=2, d=1, q=1,trend='ct',seasonal_order=(1, 0, 1, 12), enforce_invertibility=False, enforce_stationarity=False)),
    BaseModelParams('prophet', prophet.ProphetParams()),
    BaseModelParams('linear', linear_model.LinearModelParams()),
    BaseModelParams('quadratic', quadratic_model.QuadraticModelParams()),
    BaseModelParams('theta', theta.ThetaParams(m=7))
])

KatsEnsembleParam = {
    'models': model_params,
    'aggregation': 'median',
    'seasonality_length': 7,
    'decomposition_method': 'multiplicative'
}

Errors out at 'arima', 'sarima', 'linear' and 'quadratic' but funnily enough not at 'theta'. Example of error message for 'linear': ValueError: endog and exog matrices are different sizes

Data set same as above.