Beispiel #1
0
    def fit(self, series: TimeSeries):
        super().fit(series)
        series = self.training_series

        # if the model was initially created with `self.seasonal_periods=None`, make sure that
        # the model will try to automatically infer the index, otherwise it should use the
        # provided `seasonal_periods` value
        seasonal_periods_param = (
            None if self.infer_seasonal_periods else self.seasonal_periods
        )

        # set the seasonal periods paramter to a default value if it was not provided explicitly
        # and if it cannot be inferred due to the lack of a datetime index
        if self.seasonal_periods is None and series.has_range_index:
            seasonal_periods_param = 12

        hw_model = hw.ExponentialSmoothing(
            series.values(),
            trend=self.trend if self.trend is None else self.trend.value,
            damped_trend=self.damped,
            seasonal=self.seasonal if self.seasonal is None else self.seasonal.value,
            seasonal_periods=seasonal_periods_param,
            freq=series.freq if series.has_datetime_index else None,
            dates=series.time_index if series.has_datetime_index else None,
        )
        hw_results = hw_model.fit(**self.fit_kwargs)
        self.model = hw_results

        if self.infer_seasonal_periods:
            self.seasonal_periods = hw_model.seasonal_periods

        return self
Beispiel #2
0
 def fit(self, series: TimeSeries):
     super().fit(series)
     self.model = hw.ExponentialSmoothing(
         series.values(),
         trend=self.trend,
         seasonal=self.seasonal,
         seasonal_periods=self.seasonal_periods).fit()
def test_convergence_simple():
    # issue 6883
    gen = np.random.RandomState(0)
    e = gen.standard_normal(12000)
    y = e.copy()
    for i in range(1, e.shape[0]):
        y[i] = y[i - 1] - 0.2 * e[i - 1] + e[i]
    y = y[200:]
    mod = holtwinters.ExponentialSmoothing(y,
                                           initialization_method="estimated")
    res = mod.fit()
    ets_res = ETSModel(y).fit()

    # the smoothing level should be very similar, the initial state might be
    # different as it doesn't influence the final result too much
    assert_allclose(
        res.params["smoothing_level"],
        ets_res.smoothing_level,
        rtol=1e-4,
        atol=1e-4,
    )

    # the first few values are influenced by differences in initial state, so
    # we don't test them here
    assert_allclose(res.fittedvalues[10:],
                    ets_res.fittedvalues[10:],
                    rtol=1e-4,
                    atol=1e-4)
Beispiel #4
0
def holt_winters(ts, back_steps=10, degree=1, steps=1):
    """
    Predicts next value using triple exponential smoothing
    (holt-winters method).
    Parameters:
    -----------
    ts: Array of floats
        An array of times series data to be used for the polyfit regression
    Returns:
    --------
    x : The predicted value from the holt-winters method.
    """
    timeseries = np.array(list(ts.values()))
    timeseries = timeseries[-back_steps:]
    # exponential smoothing errors when there is only one datapoint
    if len(timeseries) == 1:
        timeseries = [np.inf, 0]
    # exponential smoothing errors when there are five datapoints
    # average is appended to the beginning of the timeseries for minimal impact
    # https://github.com/statsmodels/statsmodels/issues/4878
    elif len(timeseries) == 5:
        timeseries = np.append(np.mean(timeseries), timeseries)
    model = hw.ExponentialSmoothing(timeseries)
    model_fit = model.fit()
    x = model_fit.predict(len(timeseries), len(timeseries) + steps - 1)
    return x[-1]
Beispiel #5
0
def holt_linear(train, l):
    model = ets.ExponentialSmoothing(
        train, trend='mul', damped=True
    )  # Use the multiplicative version, unless the data has been logged before.

    fit1 = model.fit()
    print('Holt Linear method summary\n', fit1.summary())
    return fit1.predict(start=l.index[0], end=l.index[-1])
Beispiel #6
0
def holt_winter_seasonal(train, l):
    model = ets.ExponentialSmoothing(
        train, trend='mul', seasonal='mul'
    )  # Use the multiplicative version, unless the data has been logged before.
    fit1 = model.fit()
    # print('Holt Winter method summary\n',fit1.summary())
    # return fit1.forecast(l)
    return fit1.predict(start=l.index[0], end=l.index[-1])
def ses(y_train, y_test):
    alpha = .5
    model = ets.ExponentialSmoothing(y_train,
                                     trend=None,
                                     damped=False,
                                     seasonal=None).fit(smoothing_level=alpha,
                                                        optimized=False)
    preds = model.forecast(steps=len(y_test))

    return preds
 def get_params_model_from_pmml(ts_data, ts_model_obj):
     params = dict()
     exp_smooth_obj = ts_model_obj.get_ExponentialSmoothing()
     level_obj = exp_smooth_obj.get_Level()
     trend_obj = exp_smooth_obj.get_Trend_ExpoSmooth()
     season_obj = exp_smooth_obj.get_Seasonality_ExpoSmooth()
     params['smoothing_level'] = level_obj.get_alpha()
     params['initial_level'] = level_obj.get_initialLevelValue()
     if trend_obj:
         params['smoothing_slope'] = trend_obj.get_gamma()
         params['damping_slope'] = trend_obj.get_phi()
         params['initial_slope'] = trend_obj.get_initialTrendValue()
         trend_type = trend_obj.get_trend()
         if trend_type == 'additive':
             trend = 'add'
             damped = False
         elif trend_type == 'multiplicative':
             trend = 'mul'
             damped = False
         elif trend_type == 'damped_additive':
             trend = 'add'
             damped = True
         elif trend_type == 'damped_multiplicative':
             trend = 'mul'
             damped = True
         elif trend_type == 'polynomial_exponential':
             pass
     else:
         trend = None
         damped = False
     if season_obj:
         params['smoothing_seasonal'] = season_obj.get_delta()
         seasonal_periods = season_obj.get_Array().get_n()
         params['initial_seasons'] = np.array(
             season_obj.get_Array().get_valueOf_().strip().split(' '))
         season_type = season_obj.get_type()
         if season_type == 'additive':
             seasonal = 'add'
         elif season_type == 'multiplicative':
             seasonal = 'mul'
     else:
         seasonal = None
         seasonal_periods = None
     # if ts_model_obj.get_Extension():  # if Extension elements exist in pmml file
     #     for extension in ts_model_obj.get_Extension():
     #        if extension.get_name() == 'initialLevel':
     #            params['initial_level'] = extension.get_value()
     #        if extension.get_name() == 'initialTrend':
     #            params['initial_slope'] = extension.get_value()
     stsmdl = hw.ExponentialSmoothing(ts_data,
                                      trend=trend,
                                      seasonal=seasonal,
                                      seasonal_periods=seasonal_periods,
                                      damped=damped)
     return params, stsmdl
Beispiel #9
0
    def fit(self, series: TimeSeries):
        super().fit(series)
        hw_model = hw.ExponentialSmoothing(
            series.values(),
            trend=self.trend,
            damped=self.damped,
            seasonal=self.seasonal,
            seasonal_periods=self.seasonal_periods)

        hw_results = hw_model.fit(**self.fit_kwargs)
        self.model = hw_results
    def test_exponentialSmoothing_02(self):
        ts_data = self.getData1()
        f_name = 'exponential_smoothing2.pmml'
        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=False,
                                            seasonal='add',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        statsmodels_to_pmml(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
    def test_exponentialSmoothing_17(self):
        ts_data = self.getData3()
        f_name = 'exponential_smoothing17.pmml'
        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend=None,
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=None)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
    def fit(self, series: TimeSeries, component_index: Optional[int] = None):
        super().fit(series, component_index)
        series = self.training_series
        hw_model = hw.ExponentialSmoothing(
            series.values(),
            trend=self.trend,
            damped=self.damped,
            seasonal=self.seasonal,
            seasonal_periods=self.seasonal_periods)

        hw_results = hw_model.fit(**self.fit_kwargs)
        self.model = hw_results
    def test_exponentialSmoothing_05(self):
        ts_data = self.getData1()
        f_name = 'exponential_smoothing5.pmml'
        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=True,
                                            seasonal='add',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
    def test_exponentialSmoothing_03(self):

        # data with no trend and no seasonality

        # Oil production in Saudi Arabia
        data = [
            446.6565, 454.4733, 455.663, 423.6322, 456.2713, 440.5881,
            425.3325, 485.1494, 506.0482, 526.792, 514.2689, 494.211
        ]
        index = pd.DatetimeIndex(start='1996', end='2008', freq='A')
        ts_data = pd.Series(data, index)
        ts_data.index.name = 'datetime_index'
        ts_data.name = 'oil_production'

        f_name = 'exponential_smoothing.pmml'

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend=None,
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=None)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend=None,
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)
Beispiel #15
0
def call_holt_s(train_s,test_s,lag, a='',b='',c=''):
    prediction = ets.ExponentialSmoothing(train_s[a], trend='additive', seasonal='additive', seasonal_periods=1440,damped_trend=True).fit()
    forecast = prediction.forecast(steps=len(test_s[a]))
    yt = prediction.fittedvalues
    final_predn = dataframe_create(yt,train_s,b,a)
    final_forec = dataframe_create(forecast,test_s,b,a)
    plot_func(train_s[b],test_s[b],final_forec[b],train_s[a],test_s[a],final_forec[a],c)
    residuals = residual_avg(train_s[a], final_predn[a])
    forec_error = forecast_err(test_s[a], final_forec[a])
    mse_p, mse_f = mse(residuals, forec_error)
    var_p, var_f = var(residuals, forec_error)
    residual_acf = acf_values(residuals, lag)
    Q = Q_val(train_s, residual_acf)
    acf_plot(residual_acf, c)
    return prediction, final_forec, residuals, forec_error, mse_p, mse_f, var_p, var_f, residual_acf, Q
Beispiel #16
0
def holt_winters_trainer_full(df,
                              seasonal='additive',
                              trend='additive',
                              seasonal_periods=None):

    # optimized ExponentialSmoothing on y_data
    print('WARNING: This takes a long time. Recommend a coffee break.')
    model = ets.ExponentialSmoothing(df,
                                     seasonal=seasonal,
                                     trend=trend,
                                     seasonal_periods=seasonal_periods).fit()

    def holt_winters_predict(h):
        return model.forecast(h)

    return holt_winters_predict
Beispiel #17
0
def holt_forecast(history):
    # convert history into a univariate series
    series = to_series(history)
    # define the model
    # model = ARIMA(series, order=(6,0,0))

    model = ets.ExponentialSmoothing(series,
                                     trend='add',
                                     damped=True,
                                     seasonal='add',
                                     seasonal_periods=7)

    # fit the model
    model_fit = model.fit()
    # make forecast
    yhat = model_fit.predict(len(series), len(series) + 6)
    return yhat
 def decompose(self):
     '''
        Croston's decomposition 
     '''
     z = ts.SimpleExpSmoothing(self.demandValues.to_numpy())
     p = ts.ExponentialSmoothing(self.intervals.to_numpy())
     
     # Demand level
     z = z.fit(smoothing_level=self.alpha,
               initial_level = self.demandValues[0],
               optimized=False)
     
     # Intervals
     p = p.fit(smoothing_level=self.alpha,
               initial_level = self.intervals[0],
               optimized=False )
     
     self.fittedvalues = z.fittedfcast
     self.fittedIntervals = p.fittedfcast
     self.fittedForecasts = self.fittedvalues/self.fittedIntervals
     self.fcast = z.forecast(1)/p.forecast(1)
Beispiel #19
0
    def transform(self, seriess, debug):
        series = seriess[0]
        pdseries = series.pdseries

        damped_trend, seasonal_length, output = self.get_params()
        if seasonal_length is not None and seasonal_length != '':
            calc_seasonal_length = timedelta_to_period(seasonal_length,
                                                       series.step())
            seasonal = 'add'
        else:
            calc_seasonal_length = None
            seasonal = None

        model = tsa_hw.ExponentialSmoothing(
            pdseries,
            trend='add',
            damped_trend=damped_trend,
            seasonal=seasonal,
            seasonal_periods=calc_seasonal_length,
            initialization_method='estimated',
            use_boxcox=False)
        model_fit = model.fit()

        # Debug info
        if debug:
            debug_info = {
                "summary": str(model_fit.summary()),
            }
        else:
            debug_info = {}

        result = None
        if output == 'predicted':
            result = model_fit.fittedvalues
        elif output == 'resid':
            result = model_fit.resid
        else:
            raise ValueError('Invalid output: ' + output)

        return (result, debug_info)
def holt_winters(y_train, y_test):
    holtt = ets.ExponentialSmoothing(y_train,
                                     seasonal_periods=365,
                                     trend=None,
                                     damped=False,
                                     seasonal='mul').fit()
    holtf = holtt.forecast(steps=len(y_test))
    error = y_test - holtf
    holtf = pd.DataFrame(holtf).set_index(y_test.index)

    fig, ax = plt.subplots()
    #fig.set_figheight(20)
    #fig.set_figwidth(50)
    ax.plot(y_train, label="Train Data")
    ax.plot(y_test, label="Test Data")
    ax.plot(holtf, label="Holt-Winters")

    plt.legend(loc='upper left')
    plt.title('Holt-Winters Method - Flow')
    plt.xlabel('Time (weekly)')
    plt.ylabel('Weekly Mean Flow')
    plt.show()

    return error
def expo_smooth(timeseries):
    ts = timeseries.copy()
    ts.set_index('Date', inplace=True)
    import statsmodels.tsa.holtwinters as ets
    #Brown Simple Exponential Smoothing
    model_es = ets.ExponentialSmoothing(ts.values,
                                        trend='additive',
                                        damped=False,
                                        seasonal=None)
    model_es_fit = model_es.fit()
    ES_predictions = model_es_fit.forecast(steps=12)
    ES_error = round(
        sqrt(mean_squared_error(ts.INV_AMOUNT_USD[-12:], ES_predictions)))
    predictions['Double Exponential Smoothing Model' + ', ' + 'Error(RMSE):' +
                str(ES_error)] = ES_predictions


#MA model
#def MA(timeseries):
#global MA_predictions
#timeseries.set_index('Date', inplace=True)
#timeseries_diff = timeseries.diff(periods=1)
#integrated od order 1 (d =1)
#timeseries_diff = timeseries_diff[1:]
#from statsmodels.graphics.tsaplots import plot_acf
#plot_acf(timeseries)
#plot_acf(timeseries_diff)
#timeseries_diff.plot()
#from statsmodels.tsa.arima_model import ARMA
#model_ma = ARMA(timeseries.values, order=(0,1))
#model_ma_fit = model_ma.fit()
#MA_predictions = model_ma_fit.predict(start=24,end=36)
#MA_predictions = MA_predictions[1:]
#MA_predictions = np.array(MA_predictions)
#error(timeseries.INV_AMOUNT_USD[-12:],AR_predictions)
#create_df(AR_predictions)
import matplotlib.pyplot as plt
import statsmodels.tsa.holtwinters as ets

df = pd.read_csv('ExponentialSmoothing.csv',
                 index_col='Date',
                 parse_dates=True)
#print(df)
#Defining the range of the training and testing
spyt = df[:'2020-05-01']
spyf = df['2020-05-02':]

#print(spyt)
#print(spyf)

brownt = ets.ExponentialSmoothing(spyt,
                                  trend=None,
                                  damped=False,
                                  seasonal=None).fit()
brownf = brownt.forecast(steps=len(spyf))
brownf = pd.DataFrame(brownf).set_index(spyf.index)

fig1, ax = plt.subplots()
ax.plot(spyt, label='spyt')
ax.plot(spyf, label='spyf')
ax.plot(brownf, label='brownf')
plt.ylabel('Comments')
plt.xlabel('Timestamp')
plt.show()

# %%
#ExponentialSmoothing
import pandas as pd
Beispiel #23
0
def forecast(ts,
             alpha=None,
             beta=None,
             gamma=None,
             initial_level=None,
             initial_slope=None,
             initial_seasons=None,
             trend=None,
             seasonal=None,
             seasonal_periods=None,
             debug=False,
             use_boxcox=False):
    """Forecasting using exponential smoothing

    Usage:
        forecast(ts=ts)
        """
    smoothing_level = None
    smoothing_slope = None
    smoothing_seasonal = None
    try:
        if alpha and (0 <= alpha <= 1):
            smoothing_level = alpha
        if beta and (0 <= beta <= 1):
            smoothing_slope = beta
            # if beta is set but trend is None, trend is set equal to add
            if smoothing_slope and not trend:
                trend = 'add'
        if gamma and (0 <= gamma <= 1):
            smoothing_seasonal = gamma
            if smoothing_seasonal and not seasonal:
                seasonal = 'mul'

    except TypeError:
        print('ERRORE: alpha must be between 0 and 1')
        return np.nan

    # if trend:
    #     trend = 'add'
    # else:
    #     trend = None

    # if seasonal:
    #     seasonal = 'add'
    # else:
    #     seasonal = None

    if seasonal and not seasonal_periods:
        print(
            'ERROR: Please, provide the number of periods in a complete seasonal cycle'
        )
        return np.nan

    model = holtwinters.ExponentialSmoothing(
        endog=pd.DataFrame(data=ts),
        trend=trend,
        seasonal=seasonal,
        seasonal_periods=seasonal_periods).fit(
            smoothing_level=smoothing_level,
            smoothing_slope=smoothing_slope,
            smoothing_seasonal=smoothing_seasonal,
            initial_level=initial_level,
            initial_slope=initial_slope,
            use_boxcox=use_boxcox)

    model.predict(start=1, end=len(ts))

    if debug:
        print(model.model.params)
    # print('TS')
    # print(ts)
    # print('FORECAST')
    # print(model.fittedfcast)
    return model.fittedfcast
Beispiel #24
0
def simple_forecast_ts(train, test, method, period):
    """
    :param ytrain: training data
    :param ytest:  testing data
    :param method: simple forecast method
    :param period: only use for Holt-Winter Additive method to define the seasonal_periods
    :return: dataframes containing prediction/forecast, errors and square errors
    """
    T = len(train)  # number of observations in the training set
    h = len(
        test)  # number of observations to be forecast4ed in the testing set

    ytrain = train.values
    ytest = test.values

    xtrain = train.index
    xtest = test.index

    ytrain_hat = []  # prediction
    ytest_hat = []  # forecast
    etrain = []  # prediction error
    etest = []  # forecast error
    setrain = []  # square of prediction error
    setest = []  # square of forecast error

    if method == 'Average':  # using average method
        for i in range(0, 1):
            ytrain_hat.append(np.nan)
            etrain.append(np.nan)
            setrain.append(np.nan)
        for i in range(1, T):
            prediction = np.mean(ytrain[0:i])
            ytrain_hat.append(prediction)
            error = ytrain[i] - prediction
            etrain.append(error)
            setrain.append(error**2)

        for i in range(0, h):
            forecast = np.mean(ytrain)
            ytest_hat.append(forecast)
            error = ytest[i] - forecast
            etest.append(error)
            setest.append(error**2)

    elif method == 'Naive':  # naive method
        for i in range(0, 1):
            ytrain_hat.append(np.nan)
            etrain.append(np.nan)
            setrain.append(np.nan)
        for i in range(1, T):
            prediction = ytrain[i - 1]
            ytrain_hat.append(prediction)
            error = ytrain[i] - prediction
            etrain.append(error)
            setrain.append(error**2)

        for i in range(0, h):
            forecast = ytrain[T - 1]
            ytest_hat.append(forecast)
            error = ytest[i] - forecast
            etest.append(error)
            setest.append(error**2)

    elif method == 'Drift':  # drift method
        for i in range(0, 2):
            ytrain_hat.append(np.nan)
            etrain.append(np.nan)
            setrain.append(np.nan)
        for i in range(2, T):
            prediction = ytrain[i - 1] + (ytrain[i - 1] - ytrain[0]) / (i - 1)
            ytrain_hat.append(prediction)
            error = ytrain[i] - prediction
            etrain.append(error)
            setrain.append(error**2)

        for i in range(0, h):
            forecast = ytrain[T - 1] + (ytrain[T - 1] -
                                        ytrain[0]) * (i + 1) / (T - 1)
            ytest_hat.append(forecast)
            error = ytest[i] - forecast
            etest.append(error)
            setest.append(error**2)

    elif method == "Simple Exponential Smoothing":  # simple exponential smoothing method
        alpha = 0.5
        ytrain_hat.append(
            ytrain[0])  # the first observation is the initial condition
        etrain.append(np.nan)  # for the initial condition, there is no error
        setrain.append(np.nan)

        for i in range(1, T):
            prediction = alpha * ytrain[i - 1] + (1 - alpha) * ytrain_hat[i -
                                                                          1]
            ytrain_hat.append(prediction)
            error = ytrain[i] - prediction
            etrain.append(error)
            setrain.append(error**2)

        for i in range(0, h):
            forecast = alpha * ytrain[T - 1] + (1 - alpha) * ytrain_hat[T - 1]
            ytest_hat.append(forecast)
            error = ytest[i] - forecast
            etest.append(error)
            setest.append(error**2)

    elif method == "Holt's Linear Multiplicative":  # Holt linear using multiplicative
        holt = ets.ExponentialSmoothing(ytrain,
                                        trend='multiplicative',
                                        damped=True,
                                        seasonal=None).fit()
        ytrain_hat = holt.fittedvalues
        ytest_hat = holt.forecast(steps=h)

        for i in range(0, T):
            error = ytrain[i] - ytrain_hat[i]
            etrain.append(error)
            setrain.append(error**2)
        for i in range(0, h):
            error = ytest[i] - ytest_hat[i]
            etest.append(error)
            setest.append(error**2)

    elif method == "Holt's Linear":
        holt = ets.ExponentialSmoothing(ytrain,
                                        trend=None,
                                        damped=False,
                                        seasonal=None).fit(smoothing_level=0.1)
        ytrain_hat = holt.fittedvalues
        ytest_hat = holt.forecast(steps=h)

        for i in range(0, T):
            error = ytrain[i] - ytrain_hat[i]
            etrain.append(error)
            setrain.append(error**2)
        for i in range(0, h):
            error = ytest[i] - ytest_hat[i]
            etest.append(error)
            setest.append(error**2)

    elif method == "Holt-Winter":
        holt = ets.ExponentialSmoothing(train,
                                        seasonal_periods=period,
                                        trend='add',
                                        damped_trend=True,
                                        seasonal='additive')
        holt = holt.fit(smoothing_level=0.1,
                        smoothing_seasonal=0.2,
                        smoothing_trend=None)
        ytrain_hat = holt.fittedvalues
        ytrain_hat = ytrain_hat.values
        ytest_hat = holt.forecast(steps=h)
        ytest_hat = ytest_hat.values

        for i in range(0, T):
            error = ytrain[i] - ytrain_hat[i]
            etrain.append(error)
            setrain.append(error**2)
        for i in range(0, h):
            error = ytest[i] - ytest_hat[i]
            etest.append(error)
            setest.append(error**2)

    else:
        print("Method is not applicable")

    # create dataframes containing results after predicting the training data and forecasting the testing data
    df_train = pd.DataFrame({
        "time": xtrain,
        "y_t": ytrain,
        "hat_y_t": ytrain_hat,
        "error": etrain,
        "square_error": setrain
    })

    df_test = pd.DataFrame({
        "time": xtest,
        "y_t": ytest,
        "hat_y_t": ytest_hat,
        "error": etest,
        "square_error": setest
    })

    return df_train, df_test
    def test_keras_01(self):
        def import_data(trend=False, seasonality=False):
            """
            Returns a dataframe with time series values.
            :param trend: boolean
                If True, returns data with trend
            :param seasonality: boolean
                If True, returns data with seasonality
            :return: ts_data: DataFrame
                Index of the data frame is either a time-index or an integer index. First column has time series values
            """
            if trend and seasonality:
                # no of international visitors in Australia
                data = [
                    41.7275, 24.0418, 32.3281, 37.3287, 46.2132, 29.3463,
                    36.4829, 42.9777, 48.9015, 31.1802, 37.7179, 40.4202,
                    51.2069, 31.8872, 40.9783, 43.7725, 55.5586, 33.8509,
                    42.0764, 45.6423, 59.7668, 35.1919, 44.3197, 47.9137
                ]
                index = pd.DatetimeIndex(start='2005',
                                         end='2010-Q4',
                                         freq='QS')
                ts_data = pd.Series(data, index)
                ts_data.index.name = 'datetime_index'
                ts_data.name = 'n_visitors'
                return ts_data
            elif trend:
                # no. of annual passengers of air carriers registered in Australia
                data = [
                    17.5534, 21.86, 23.8866, 26.9293, 26.8885, 28.8314,
                    30.0751, 30.9535, 30.1857, 31.5797, 32.5776, 33.4774,
                    39.0216, 41.3864, 41.5966
                ]
                index = pd.DatetimeIndex(start='1990', end='2005', freq='A')
                ts_data = pd.Series(data, index)
                ts_data.index.name = 'datetime_index'
                ts_data.name = 'n_passengers'
                return ts_data
            elif seasonality:
                pass
            else:
                # Oil production in Saudi Arabia
                data = [
                    446.6565, 454.4733, 455.663, 423.6322, 456.2713, 440.5881,
                    425.3325, 485.1494, 506.0482, 526.792, 514.2689, 494.211
                ]
                index = pd.DatetimeIndex(start='1996', end='2008', freq='A')
                ts_data = pd.Series(data, index)
                ts_data.index.name = 'datetime_index'
                ts_data.name = 'oil_production'
                return ts_data

        ts_data = import_data(trend=False, seasonality=False)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend=None,
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=None)

        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj,
                                   'exponential_smoothing.pmml')

        self.assertEqual(os.path.isfile("exponential_smoothing.pmml"), True)

        ts_data = import_data(trend=True, seasonality=False)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=None)

        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj,
                                   'exponential_smoothing.pmml')

        self.assertEqual(os.path.isfile("exponential_smoothing.pmml"), True)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=None)

        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj,
                                   'exponential_smoothing.pmml')

        self.assertEqual(os.path.isfile("exponential_smoothing.pmml"), True)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=True,
                                            seasonal=None,
                                            seasonal_periods=None)

        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj,
                                   'exponential_smoothing.pmml')

        self.assertEqual(os.path.isfile("exponential_smoothing.pmml"), True)

        ts_data = import_data(trend=True, seasonality=True)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=False,
                                            seasonal='add',
                                            seasonal_periods=4)

        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj,
                                   'exponential_smoothing.pmml')

        self.assertEqual(os.path.isfile("exponential_smoothing.pmml"), True)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=True,
                                            seasonal='add',
                                            seasonal_periods=4)

        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj,
                                   'exponential_smoothing.pmml')

        self.assertEqual(os.path.isfile("exponential_smoothing.pmml"), True)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=False,
                                            seasonal='mul',
                                            seasonal_periods=4)

        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj,
                                   'exponential_smoothing.pmml')

        self.assertEqual(os.path.isfile("exponential_smoothing.pmml"), True)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=False,
                                            seasonal='mul',
                                            seasonal_periods=4)

        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj,
                                   'exponential_smoothing.pmml')

        self.assertEqual(os.path.isfile("exponential_smoothing.pmml"), True)
print(df.shape)

# Fill in nan with forward fill (replace nan with preceding value)
df = df.fillna(method='ffill')

time = pd.date_range('2004-03-10 18:00:00', periods=len(df.Time), freq='h')

lags = 50
# Split the data into 80% train and 20% test
y = df['CO(GT)']
y_train, y_test = train_test_split(y, shuffle=False, test_size=0.2)
x_train, x_test = train_test_split(time, shuffle=False, test_size=0.2)

# ******************************Holt-Winter method*****************************

Holt_Winter = ets.ExponentialSmoothing(y_train, trend='add', seasonal='add', seasonal_periods=24).fit()
Holt_Winter_pr = Holt_Winter.fittedvalues
Holt_Winter_forecast = Holt_Winter.forecast(len(y_test))

# Plot the Holt-Winter Seasonal Method and Forecast
plt.figure()
plt.plot(x_train, y_train, label = 'Training set')
plt.plot(x_test, y_test, label = 'Test set')
plt.plot(x_test, Holt_Winter_forecast, label = 'h-step Forecast')
plt.legend()
plt.xlabel('Time (hourly)')
plt.ylabel('Concentration')
plt.title('CO level - Holt-Winter Seasonal Method and Forecast')
plt.show()

# Residual errors
    def test_exponentialSmoothing_01(self):

        # data with trend and seasonality present

        # no of international visitors in Australia
        data = [
            41.7275, 24.0418, 32.3281, 37.3287, 46.2132, 29.3463, 36.4829,
            42.9777, 48.9015, 31.1802, 37.7179, 40.4202, 51.2069, 31.8872,
            40.9783, 43.7725, 55.5586, 33.8509, 42.0764, 45.6423, 59.7668,
            35.1919, 44.3197, 47.9137
        ]
        index = pd.DatetimeIndex(start='2005', end='2010-Q4', freq='QS')
        ts_data = pd.Series(data, index)
        ts_data.index.name = 'datetime_index'
        ts_data.name = 'n_visitors'

        f_name = 'exponential_smoothing.pmml'

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=True,
                                            seasonal='add',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=False,
                                            seasonal='add',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=True,
                                            seasonal='mul',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=False,
                                            seasonal='mul',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=True,
                                            seasonal='add',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=False,
                                            seasonal='add',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=True,
                                            seasonal='mul',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=False,
                                            seasonal='mul',
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)
Beispiel #28
0
    def exponentialFit(self, name):
        ''' Parameters
            ----------
            
            name: name of model
        '''
        modelName = name
        errorObjs = []
        runTimeObj = obj.ModelsRunTime(name)
        startTime = None
        totalTime = None

        # Step 1: fit selected model
        if name == 'NAIVE':
            # for evaluation
            startTime = dt.datetime.now()
            self.fittedModel = ts.ExponentialSmoothing(self.trainData)
            self.fittedModel = self.fittedModel.fit(smoothing_level=1)
            runTimeObj.setTrainingTime(dt.datetime.now() - startTime)

            # for real forecasts
            self.fittedModelFinal = ts.ExponentialSmoothing(self.data)
            self.fittedModelFinal = self.fittedModelFinal.fit(
                smoothing_level=1)

        elif name == 'SES':
            # for evaluation
            startTime = dt.datetime.now()
            self.fittedModel = ts.SimpleExpSmoothing(self.trainData)
            self.fittedModel = self.fittedModel.fit(
                optimized=True, use_brute=True)  #grid search
            runTimeObj.setTrainingTime(dt.datetime.now() - startTime)

            # for real forecasts
            self.fittedModelFinal = ts.SimpleExpSmoothing(self.data)
            self.fittedModelFinal = self.fittedModelFinal.fit(
                optimized=True, use_brute=True)  #grid search
        elif name == 'HOLT':
            # Holt's linear trend method
            # for evaluation
            startTime = dt.datetime.now()
            self.fittedModel = ts.Holt(self.trainData)
            self.fittedModel = self.fittedModel.fit(
                optimized=True, use_brute=True)  #grid search
            runTimeObj.setTrainingTime(dt.datetime.now() - startTime)
            # for real forecasts
            self.fittedModelFinal = ts.Holt(self.data)
            self.fittedModelFinal = self.fittedModelFinal.fit(
                optimized=True, use_brute=True)  #grid search

        # Step 2: get fitted values for training, test and forecasts
        trainingFit = pd.Series(np.ceil(self.fittedModel.fittedvalues))
        startTime = dt.datetime.now()
        testPredictions = pd.Series(
            np.ceil(self.fittedModel.forecast(len(self.testData))))
        totalTime = dt.datetime.now() - startTime

        forecasts = pd.Series(
            np.ceil(self.fittedModelFinal.forecast(self.horizon)))

        # Step 3: set error
        errorObjs = self.setErrorData(trainingFit, testPredictions, runTimeObj)

        runTimeObj.setTestTime(runTimeObj.getTestTime() + totalTime)
        self.runTimeList.append(runTimeObj)
        # Add to ModelsResult list
        self.setModelResults(modelName, errorObjs, trainingFit,
                             testPredictions, forecasts)
Beispiel #29
0
    def exponentialFit(self, name):
        ''' Parameters
            ----------
            
            name: name of model
        '''
        modelName = name
        errorObjs = []

        # Step 1: fit selected model
        if name == 'NAIVE':
            # for evaluation
            self.fittedModel = ts.ExponentialSmoothing(self.trainData)
            self.fittedModel = self.fittedModel.fit(smoothing_level=1)
            # for real forecasts
            self.fittedModelFinal = ts.ExponentialSmoothing(self.data)
            self.fittedModelFinal = self.fittedModelFinal.fit(
                smoothing_level=1)

        elif name == 'SES':
            # for evaluation
            self.fittedModel = ts.SimpleExpSmoothing(self.trainData)
            self.fittedModel = self.fittedModel.fit(
                optimized=True, use_brute=True)  #grid search
            # for real forecasts
            self.fittedModelFinal = ts.SimpleExpSmoothing(self.data)
            self.fittedModelFinal = self.fittedModelFinal.fit(
                optimized=True, use_brute=True)  #grid search
        elif name == 'HOLT':
            # Holt-Winters
            # for evaluation
            self.fittedModel = ts.Holt(self.trainData)
            self.fittedModel = self.fittedModel.fit(
                optimized=True, use_brute=True)  #grid search
            # for real forecasts
            self.fittedModelFinal = ts.Holt(self.data)
            self.fittedModelFinal = self.fittedModelFinal.fit(
                optimized=True, use_brute=True)  #grid search

        # Step 2: get fitted values for training, test and forecasts
        trainingFit = pd.Series(self.fittedModel.fittedvalues)
        forecasts = pd.Series(self.fittedModelFinal.forecast(self.horizon))

        if self.stepType == 'multi':

            testPredictions = pd.Series(
                self.fittedModel.forecast(len(self.testData)))

        else:
            # Compute one-step-ahead forecast over the test data
            SESParams = self.fittedModel.params
            self.fittedModel = ts.SimpleExpSmoothing(self.data)

            if modelName == 'HOLT':
                self.fittedModel = ts.Holt(self.data)
                self.fittedModel = self.fittedModel.fit(
                    smoothing_level=SESParams['smoothing_level'],
                    optimized=False,
                    smoothing_slope=SESParams['smoothing_slope'],
                    initial_slope=SESParams['initial_slope'],
                    initial_level=SESParams['initial_level'],
                    use_brute=False)

            else:

                self.fittedModel = self.fittedModel.fit(
                    smoothing_level=SESParams['smoothing_level'],
                    optimized=False,
                    initial_level=SESParams['initial_level'],
                    use_brute=False)

            testPredictions = self.fittedModel.fittedvalues[self.start:]

        # Step 3: set error
        errorObjs = self.setErrorData(trainingFit, testPredictions)

        # Add to ModelsResult list
        self.setModelResults(modelName, errorObjs, trainingFit,
                             testPredictions, forecasts)
    def test_exponentialSmoothing_02(self):

        # data with trend but no seasonality

        # no. of annual passengers of air carriers registered in Australia
        data = [
            17.5534, 21.86, 23.8866, 26.9293, 26.8885, 28.8314, 30.0751,
            30.9535, 30.1857, 31.5797, 32.5776, 33.4774, 39.0216, 41.3864,
            41.5966
        ]
        index = pd.DatetimeIndex(start='1990', end='2005', freq='A')
        ts_data = pd.Series(data, index)
        ts_data.index.name = 'datetime_index'
        ts_data.name = 'n_passengers'

        f_name = 'exponential_smoothing.pmml'

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=True,
                                            seasonal=None,
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=True,
                                            seasonal=None,
                                            seasonal_periods=None)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='add',
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=None)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=True,
                                            seasonal=None,
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=True,
                                            seasonal=None,
                                            seasonal_periods=None)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=2)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)

        model_obj = hw.ExponentialSmoothing(ts_data,
                                            trend='mul',
                                            damped=False,
                                            seasonal=None,
                                            seasonal_periods=None)
        results_obj = model_obj.fit(optimized=True)

        ExponentialSmoothingToPMML(ts_data, model_obj, results_obj, f_name)
        self.assertEqual(os.path.isfile(f_name), True)
        os.remove(f_name)