antoinecarme / pyaf

PyAF is an Open Source Python library for Automatic Time Series Forecasting built on top of popular pydata modules.
BSD 3-Clause "New" or "Revised" License
458 stars 73 forks source link

Add Croston Method #97

Closed antoinecarme closed 5 years ago

antoinecarme commented 6 years ago

This is a quite known method for forecasting intermittent demand (time series with a small number of non-zero observations).

R implementation :

https://www.rdocumentation.org/packages/forecast/versions/8.4/topics/croston

See if Croston method can be implemented efficiently as the last component of a PyAF signal decompostion (any other option ?).

antoinecarme commented 6 years ago

some references :

  1. The FPP2 book by Rob J Hyndman and George Athanasopoulos : https://otexts.org/fpp2/counts.html With a very nice description of the time series of counts models and an example time series dataset image

    1. More sophisticated/specialized tsintermittent package for R by Nikolaos Kourentzes . https://cran.r-project.org/package=tsintermittent http://kourentzes.com/forecasting/2014/06/23/intermittent-demand-forecasting-package-for-r/ https://github.com/trnnick/tsintermittent

      => Two datasets

  2. Good presentation : https://www.lancaster.ac.uk/media/lancaster-university/content-assets/documents/lums/forecasting/presentations/Petropoulosetal.-ISIR2014-Anotherlookatestimatorsforintermittentdemand.pdf image

        = > RAF dataset + Automotive dataset
antoinecarme commented 5 years ago

Croston options (with default values) :

class cCrostonOptions:
    def __init__(self):
        # can be : "CROSTON" , "SBJ" , "SBA", everything else is equivalent to "CROSTON"
        self.mMethod = None;
        self.mAlpha = 0.1
        # minimum amount of zeros for a series to be intermittent
        self.mZeroRate = 0.1
antoinecarme commented 5 years ago

Sample script with croston model :

https://github.com/antoinecarme/pyaf/blob/croston/tests/croston/croston_test_1_SBJ.py

and log :

INFO:pyaf.std:START_TRAINING 'Signal'
INFO:pyaf.std:END_TRAINING_TIME_IN_SECONDS 'Signal' 5.448007345199585
INFO:pyaf.std:TIME_DETAIL TimeVariable='Date' TimeMin=2016-01-25T00:00:00.000000 TimeMax=2016-11-05T00:00:00.000000 TimeDelta=<DateOffset: days=1> Horizon=7
INFO:pyaf.std:SIGNAL_DETAIL_ORIG SignalVariable='Signal' Min=0.0 Max=97.0  Mean=1.0520547945205478 StdDev=8.398631812727873
INFO:pyaf.std:SIGNAL_DETAIL_TRANSFORMED TransformedSignalVariable='_Signal' Min=0.0 Max=97.0  Mean=1.0520547945205478 StdDev=8.398631812727873
INFO:pyaf.std:BEST_TRANSOFORMATION_TYPE '_'
INFO:pyaf.std:BEST_DECOMPOSITION  '_Signal_ConstantTrend_residue_zeroCycle_residue_CROSTON(0.1)' [ConstantTrend + NoCycle + CROSTON]
INFO:pyaf.std:TREND_DETAIL '_Signal_ConstantTrend' [ConstantTrend]
INFO:pyaf.std:CYCLE_DETAIL '_Signal_ConstantTrend_residue_zeroCycle' [NoCycle]
INFO:pyaf.std:AUTOREG_DETAIL '_Signal_ConstantTrend_residue_zeroCycle_residue_CROSTON(0.1)' [CROSTON]
INFO:pyaf.std:MODEL_MAPE MAPE_Fit=166570291253.2988 MAPE_Forecast=23597014621.9707 MAPE_Test=20278312516.2075
INFO:pyaf.std:MODEL_SMAPE SMAPE_Fit=1.9795 SMAPE_Forecast=1.9986 SMAPE_Test=2.0
INFO:pyaf.std:MODEL_MASE MASE_Fit=8.7383 MASE_Forecast=1.5821 MASE_Test=20278312516.2075
INFO:pyaf.std:MODEL_L1 L1_Fit=17.292699561061582 L1_Forecast=3.3870926948121 L1_Test=2.0278312516207504
INFO:pyaf.std:MODEL_L2 L2_Fit=24.449633384717657 L2_Forecast=9.04022326571336 L2_Test=2.0278312516207504
INFO:pyaf.std:MODEL_COMPLEXITY 2
INFO:pyaf.std:AR_MODEL_DETAIL_START
INFO:pyaf.std:AR_MODEL_DETAIL_END
antoinecarme commented 5 years ago

image

antoinecarme commented 5 years ago

fpp2 counts example :

https://github.com/antoinecarme/pyaf/blob/croston/tests/croston/test_croston_fpp2_counts_example.py

image

antoinecarme commented 5 years ago

Fixed.

Croston method is now supported but not used by default.