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 by demand probability which is updated every period. Compared to Croston's method, the forecast of TSB method is not biased 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 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 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.

When it is set to 1, the leading zeros are ignored for calculating measure.

Defaults to False.

massivebool, optional

Specifies whether or not to use massive mode of croston TSB.

  • True : massive mode.

  • False : single mode.

For parameter setting in massive mode, you could use both group_params (please see the example below) or the original parameters. Using original parameters will apply for all groups. However, if you define some parameters of a group, the value of all original parameter setting will be not applicable to such 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 shall be divided into different groups with different parameters applied.

An example is as follows:

Valid only when massive is True and defaults to None.

Examples

Input dataframe df:

>>> df.collect()
ID    Y
 0  0.0
 1  0.0
 2  4.0
 3  0.0
 4  0.0
 5  0.0
 6  5.0
 7  3.0
 8  0.0
 9  0.0
10  0.0

Create an instance:

>>> 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,
                    massive=True)

Perform fit on the given data:

>>> forecast = cr.fit_predict(df, key='ID', endog='Y', group_key='GROUP_ID')

Output:

>>> forecast.collect()
ID  OUTPUT_VALUE
 1      1.440000
 2      1.296000
 3      1.566400
 4      1.409760
...
14      1.225253
15      1.225253
16      1.225253
17      1.225253
18      1.225253
19      1.225253
20      1.225253
>>> cr.stats_.collect()
STAT_NAME  STAT_VALUE
     MAPE    0.895982
>>> cr.metrics_.collect()
     DEMAND_FORECAST  3.990000
PROBABILITY_FORECAST  0.307081
Attributes
forecast_DateFrame

Forecast values.

stats_DataFrame

Statistics analysis.

metrics_DataFrame

Metrics Value.

error_msg_DataFrame

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

Methods

build_report()

Generates time series report.

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

Fit and predict based on the given time series.

generate_html_report([filename])

Display function.

generate_notebook_iframe_report()

Display function.

property fit_hdbprocedure

Returns the generated hdbprocedure for fit.

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. If data type is INT, only parameters set in the group_params are valid.

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.

property predict_hdbprocedure

Returns the generated hdbprocedure for predict.

build_report()

Generates time series report.

generate_html_report(filename=None)

Display function.

generate_notebook_iframe_report()

Display function.

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.