Skip to content

Commit

Permalink
Add Croston Method #97
Browse files Browse the repository at this point in the history
Added two more tests
  • Loading branch information
antoinecarme committed Oct 31, 2018
1 parent 6db1a9c commit f4455eb
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/croston/test_croston_fpp2_counts_example_optimize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pandas as pd

def create_dataset():
lCounts = "0 2 0 1 0 11 0 0 0 0 2 0 6 3 0 0 0 0 0 7 0 0 0 0 0 0 0 3 1 0 0 1 0 1 0 0".split()
lCounts = [float(c) for c in lCounts]
N = len(lCounts)
lDates = pd.date_range(start="2000-01-01", periods=N, freq='m')

df = pd.DataFrame({"Date" : lDates, "Signal" : lCounts})
return df

def create_model(croston_type):
df_train = create_dataset()
# print(df_train.head(N))

import pyaf.ForecastEngine as autof
lEngine = autof.cForecastEngine()

lEngine.mOptions.set_active_trends(['ConstantTrend', 'LinearTrend'])
lEngine.mOptions.set_active_periodics(['None'])
lEngine.mOptions.set_active_transformations(['None'])
lEngine.mOptions.set_active_autoregressions(['CROSTON'])
lEngine.mOptions.mModelSelection_Criterion = "L2";
lEngine.mOptions.mCrostonOptions.mMethod = croston_type
lEngine.mOptions.mCrostonOptions.mZeroRate = 0.0
lEngine.mOptions.mCrostonOptions.mAlpha = None

# get the best time series model for predicting one week
lEngine.train(iInputDS = df_train, iTime = 'Date', iSignal = 'Signal', iHorizon = 7);

lEngine.getModelInfo()

lName = "outputs/fpp2_croston_optimize_" + str(croston_type) + "_"
lEngine.standardPlots(lName);

# predict one week
df_forecast = lEngine.forecast(iInputDS = df_train, iHorizon = 7)
# list the columns of the forecast dataset
print(df_forecast.columns) #

cols = ['Date', 'Signal', '_Signal',
'_Signal_TransformedForecast', 'Signal_Forecast']
# print the real forecasts
print(df_forecast[cols].tail(12))

print(df_forecast['Signal'].describe())
print(df_forecast['Signal_Forecast'].describe())


create_model(None)
52 changes: 52 additions & 0 deletions tests/croston/test_croston_fpp2_counts_example_slow_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pandas as pd

def create_dataset():
lCounts = "0 2 0 1 0 11 0 0 0 0 2 0 6 3 0 0 0 0 0 7 0 0 0 0 0 0 0 3 1 0 0 1 0 1 0 0".split()
lCounts = [float(c) for c in lCounts]
N = len(lCounts)
lDates = pd.date_range(start="2000-01-01", periods=N, freq='m')

df = pd.DataFrame({"Date" : lDates, "Signal" : lCounts})
return df

def create_model(croston_type):
df_train = create_dataset()
# print(df_train.head(N))

import pyaf.ForecastEngine as autof
lEngine = autof.cForecastEngine()

lSlowMode = False
if(lSlowMode):
lEngine.mOptions.set_active_trends(['ConstantTrend', 'LinearTrend'])
lEngine.mOptions.set_active_periodics(['None'])
lEngine.mOptions.set_active_transformations(['None'])
lEngine.mOptions.set_active_autoregressions(['CROSTON'])

lEngine.mOptions.mModelSelection_Criterion = "L2";
lEngine.mOptions.mCrostonOptions.mMethod = croston_type
lEngine.mOptions.mCrostonOptions.mZeroRate = 0.0

# get the best time series model for predicting one week
lEngine.train(iInputDS = df_train, iTime = 'Date', iSignal = 'Signal', iHorizon = 7);

lEngine.getModelInfo()

lName = "outputs/fpp2_slow_mode_" + str(croston_type) + "_"
lEngine.standardPlots(lName);

# predict one week
df_forecast = lEngine.forecast(iInputDS = df_train, iHorizon = 7)
# list the columns of the forecast dataset
print(df_forecast.columns) #

cols = ['Date', 'Signal', '_Signal',
'_Signal_TransformedForecast', 'Signal_Forecast']
# print the real forecasts
print(df_forecast[cols].tail(12))

print(df_forecast['Signal'].describe())
print(df_forecast['Signal_Forecast'].describe())


create_model(None)

0 comments on commit f4455eb

Please sign in to comment.