Exemplo n.º 1
0
def fallback(mase_all, smape_all, train, test, m):
    naive = NaiveSeasonal(K=m)
    naive.fit(train)
    forecast = naive.predict(len(test))
    mase_all.append(np.vstack([
        mase_m4(train, test, forecast, m=m),
    ]))
    smape_all.append(np.vstack([
        smape_m4(test, forecast),
    ]))
Exemplo n.º 2
0
    def test_predict_ensemble_local_models(self):
        naive = NaiveSeasonal(K=5)
        theta = Theta()
        naive_ensemble = NaiveEnsembleModel([naive, theta])
        naive_ensemble.fit(self.series1 + self.series2)
        forecast_naive_ensemble = naive_ensemble.predict(5)
        naive.fit(self.series1 + self.series2)
        theta.fit(self.series1 + self.series2)
        forecast_mean = 0.5 * naive.predict(5) + 0.5 * theta.predict(5)

        self.assertTrue(
            np.array_equal(forecast_naive_ensemble.values(),
                           forecast_mean.values()))
Exemplo n.º 3
0
def naive2_groe(ts: TimeSeries, n: int, m: int):
    """
    Return the prediction of the naive2 baseline
    """
    # It will be better to use R functions
    ts_des = ts
    seasonOut = 1
    if m > 1:
        if check_seasonality(ts, m=int(m), max_lag=2 * m):
            _, season = extract_trend_and_seasonality(ts, m, model=ModelMode.MULTIPLICATIVE)
            ts_des = remove_from_series(ts, season, model=ModelMode.MULTIPLICATIVE)
            seasonOut = season[-m:].shift(m)
            seasonOut = seasonOut.append_values(seasonOut.values())[:n]
    naive2 = NaiveSeasonal(K=1)

    naive2.fit(ts_des)
    return naive2.predict(n) * seasonOut
Exemplo n.º 4
0
 naive = NaiveDrift()
 naive2 = NaiveSeasonal(K=1)
 naiveSeason = NaiveSeasonal(K=m)
 ses = ExponentialSmoothing(trend=None,
                            seasonal=None,
                            seasonal_periods=m)
 holt = ExponentialSmoothing(seasonal=None,
                             damped=False,
                             trend='additive',
                             seasonal_periods=m)
 damp = ExponentialSmoothing(seasonal=None,
                             damped=True,
                             trend='additive',
                             seasonal_periods=m)
 naive.fit(train)
 naive2.fit(train_des)
 naiveSeason.fit(train)
 try:
     ses.fit(train_des)
 except ValueError:
     # not useful anymore
     train_des = train_des.shift(-len(train_des)).append(train_des)
     #                 train_des = TimeSeries.from_times_and_values(train_des.shift(-11).time_index[:11],
     #                                                              2*train_des.values()[0]-train_des.values()[10::-1]) \
     #                                                             .append(train_des)
     ses.fit(train_des)
 holt.fit(train_des)
 damp.fit(train_des)
 forecast_naiveSeason = naiveSeason.predict(len(test))
 forecast_naiveDrift = naive.predict(len(test))
 forecast_naive = forecast_naiveSeason + forecast_naiveDrift - train.last_value(
Exemplo n.º 5
0
                    train_des = remove_from_series(
                        train, season, model=ModelMode.MULTIPLICATIVE)
                    seasonOut = season[-m:].shift(m)
                    seasonOut = seasonOut.append_values(seasonOut.values())
                    seasonOut = seasonOut[:len(test)]

            season_diff.append(
                np.abs(train_des.values() - r_train_des) / np.abs(r_train_des))

            train_des = TimeSeries.from_times_and_values(
                train.time_index(), r_train_des)
            seasonOut = TimeSeries.from_times_and_values(
                test.time_index(), r_seasonOut)

            naive2 = NaiveSeasonal(K=1)
            naive2.fit(train_des)

            forecast_naive2 = naive2.predict(len(test)) * seasonOut

            mase_all.append(
                np.vstack([
                    mase_m4(train, test, forecast_naive2, m=m),
                ]))
            smape_all.append(np.vstack([
                smape_m4(test, forecast_naive2),
            ]))
        rel_error = np.mean(np.hstack(season_diff))
        pkl.dump([mase_all, smape_all, season_diff],
                 open("rnaive2_" + cat + ".pkl", "wb"))
        print(
            np.round(np.nanmean(np.stack(mase_all), axis=(