CrostonTSB

class hana_ml.algorithms.pal.tsa.exponential_smoothing.CrostonTSB(alpha=None, beta=None, forecast_num=None, method=None, accuracy_measure=None, ignore_zero=None, expost_flag=None, remove_leading_zeros=None, massive=False, group_params=None)

Croston TSB method (for Teunter, Syntetos & Babai) is a forecast strategy for products with intermittent demand. It is a modification of Croston's method.

It replaces the demand interval in Croston's method with demand probability, which is updated every period. Compared to Croston's method, the forecast of the TSB method is unbiased, and its probability forecast can be used to estimate the risk of obsolescence.

Parameters:
alphafloat, optional

Smoothing parameter for demand.

Defaults to 0.1.

betafloat, optional

Smoothing parameter for probability.

Defaults to 0.1.

forecast_numint, optional

Number of values to be forecast.

When it is set to 1, the algorithm only forecasts one value.

Defaults to 0.

methodstr, optional
  • 'sporadic': Use the sporadic method.

  • 'constant': Use the constant method.

Defaults to 'sporadic'.

accuracy_measurestr or a list of str, optional

The metric to quantify how well a model fits input data. Options: "mpe", "mse", "rmse", "et", "mad", "mase", "wmape", "smape", "mape".

No default value.

Note

Specify a measure name if you want the corresponding measure value to be reflected in the output statistics self.stats_.

expost_flagbool, optional
  • False: Does not output the expost forecast, and just outputs the forecast values.

  • True: Outputs both the expost forecast and the forecast values.

Defaults to True.

ignore_zerobool, optional
  • False: Uses zero values in the input dataset when calculating "mpe" or "mape".

  • True: Ignores zero values in the input dataset when calculating "mpe" or "mape".

Only valid when accuracy_measure is "mpe" or "mape".

Defaults to False.

remove_leading_zerosbool, optional
  • False: Uses leading zero values in the input dataset when smoothing the probability.

  • True: Ignores leading zero values in the input dataset when smoothing the probability.

Defaults to False.

massivebool, optional

Specifies whether or not to use the massive mode of Croston TSB.

  • True: Massive mode.

  • False: Single mode.

For parameter settings in massive mode, you can use both group_params (see the example below) or the original parameters. Using original parameters will apply them to all groups. However, if you define some parameters for a group, the value of all original parameter settings will not be applicable to that group.

An example is as follows:

In this example, as 'accuracy_measure' is set in group_params for Group_1, parameter setting of 'expost_flag' is not applicable to Group_1.

Defaults to False.

group_paramsdict, optional

If massive mode is activated (massive is True), input data for Croston TSB will be divided into different groups with different parameters applied.

An example is as follows:

Valid only when massive is True and defaults to None.

Attributes:
forecast_DataFrame

Forecast values.

stats_DataFrame

Statistics analysis.

metrics_DataFrame

Metrics values.

error_msg_DataFrame

Error messages. Only valid if massive is True when initializing a CrostonTSB instance.

Methods

fit_predict(data[, key, endog, group_key])

Fit and predict based on the given time series.

Examples

Single mode example:

>>> cr = CrostonTSB(alpha=0.3, beta=0.1, forecast_num=10,
                    method='constant', accuracy_measure=['mape'],
                    expost_flag=True, ignore_zero=False, remove_leading_zeros=False)

Perform fit_predict():

>>> forecast = cr.fit_predict(data=df, key='ID')

Output:

>>> forecast.collect()
>>> cr.stats_.collect()
>>> cr.metrics_.collect()

Massive mode example:

>>> cr_massive = CrostonTSB(massive=True, group_params={
                            'Group_1': {forecase_num=5, 'accuracy_measure': ['mape']},
                            'Group_2': {'alpha': 0.3, 'beta': 0.15, forecase_num=5, 'accuracy_measure': ['mse']}})

Perform fit_predict():

>>> forecast_massive = cr_massive.fit_predict(data=df_massive, group_key='GROUP_ID', key='ID', endog='y')

Output:

>>> forecast_massive.collect()
>>> cr_massive.stats_.collect()
>>> cr_massive.metrics_.collect()
>>> cr_massive.error_msg_.collect()
fit_predict(data, key=None, endog=None, group_key=None)

Fit and predict based on the given time series.

Parameters:
dataDataFrame

Input data. At least two columns, one is ID column, the other is raw data.

keystr, optional

The ID column.

In single mode, defaults to the first column of data if the index column of data is not provided. Otherwise, defaults to the index column of data.

In massive mode, defaults to the first-non group key column of data if the index columns of data is not provided. Otherwise, defaults to the second of index columns of data and the first column of index columns is group_key.

endogstr, optional

The column of series to be fitted and predicted.

In single mode, defaults to the first non-ID column. In massive mode, defaults to the first non group_key, non key column.

group_keystr, optional

The column of group_key. The data type can be INT or NVARCHAR/VARCHAR. This parameter is only valid when massive is True.

Defaults to the first column of data if the index columns of data is not provided. Otherwise, defaults to the first column of index columns.

Returns:
DataFrame

Forecast values.

Inherited Methods from PALBase

Besides those methods mentioned above, the CrostonTSB class also inherits methods from PALBase class, please refer to PAL Base for more details.