def fit(self, y_train, fh=None, X_train=None): """Fit to training data. Parameters ---------- y_train : pd.Series Target time series to which to fit the forecaster. fh : int, list or np.array, optional (default=None) The forecasters horizon with the steps ahead to to predict. X_train : pd.DataFrame, optional (default=None) Exogenous variables are ignored Returns ------- self : returns an instance of self. """ sp = check_sp(self.sp) if sp > 1 and not self.deseasonalise: warn("`sp` is ignored when `deseasonalise`=False") if self.deseasonalise: self.deseasonaliser_ = Deseasonalizer(sp=self.sp, model="multiplicative") y_train = self.deseasonaliser_.fit_transform(y_train) # fit exponential smoothing forecaster # find theta lines: Theta lines are just SES + drift super(ThetaForecaster, self).fit(y_train, fh=fh) self.smoothing_level_ = self._fitted_forecaster.params[ "smoothing_level"] # compute trend self.trend_ = self._compute_trend(y_train) self._is_fitted = True return self
def test_pipeline(): y = load_airline() y_train, y_test = temporal_train_test_split(y) f = TransformedTargetForecaster([ ("t1", Deseasonalizer(sp=12, model="multiplicative")), ("t2", Detrender(PolynomialTrendForecaster(degree=1))), ("f", NaiveForecaster()) ]) fh = np.arange(len(y_test)) + 1 f.fit(y_train, fh) actual = f.predict() def compute_expected_y_pred(y_train, fh): # fitting yt = y_train.copy() t1 = Deseasonalizer(sp=12, model="multiplicative") yt = t1.fit_transform(yt) t2 = Detrender(PolynomialTrendForecaster(degree=1)) yt = t2.fit_transform(yt) f = NaiveForecaster() f.fit(yt, fh) # predicting y_pred = f.predict() y_pred = t2.inverse_transform(y_pred) y_pred = t1.inverse_transform(y_pred) return y_pred expected = compute_expected_y_pred(y_train, fh) np.testing.assert_array_equal(actual, expected)
def test_deseasonalised_values(sp): t = Deseasonalizer(sp=sp) t.fit(y_train) a = t.transform(y_train) r = seasonal_decompose(y_train, period=sp) b = y_train - r.seasonal np.testing.assert_array_equal(a.values, b.values)
def learn(series_data): model = TransformedTargetForecaster([ ("deseasonalise", Deseasonalizer(model="multiplicative", sp=7)), ("detrend", Detrender(forecaster=PolynomialTrendForecaster(degree=4))), ("forecast", PolynomialTrendForecaster(degree=4)) ]) model.fit(series_data[:-2]) return model
def test_deseasonalised_values(sp): transformer = Deseasonalizer(sp=sp) transformer.fit(y_train) actual = transformer.transform(y_train) r = seasonal_decompose(y_train, period=sp) expected = y_train - r.seasonal np.testing.assert_array_equal(actual, expected)
def compute_expected_y_pred(y_train, fh): # fitting yt = y_train.copy() t1 = Deseasonalizer(sp=12, model="multiplicative") yt = t1.fit_transform(yt) t2 = Detrender(PolynomialTrendForecaster(degree=1)) yt = t2.fit_transform(yt) f = NaiveForecaster() f.fit(yt, fh) # predicting y_pred = f.predict() y_pred = t2.inverse_transform(y_pred) y_pred = t1.inverse_transform(y_pred) return y_pred
# In[126]: model = PolynomialTrendForecaster(degree=1) transformer = Detrender(model) yt = transformer.fit_transform(train) trendline = model.fit(train).predict(fh=-np.arange(len(train))) plot_ys(train, trendline, yt, labels=['series', 'trend', 'detrended']) # ### Pipelining # In[130]: forecaster = TransformedTargetForecaster([ ("deseasonalise", Deseasonalizer(model="multiplicative", sp=12)), ("detrend", Detrender(forecaster=PolynomialTrendForecaster(degree=1))), ("forecast", ReducedRegressionForecaster(regressor=regressor, window_length=12, strategy="recursive")) ]) forecaster.fit(train) y_pred = forecaster.predict(fh) plot_ys(train, test, y_pred, labels=["y_train", "y_test", "y_pred"]) smape_loss(test, y_pred) # ## WORK IN PROGRESS # ## Pipeline Tuning
def test_transform_inverse_transform_equivalence(sp, model): t = Deseasonalizer(sp=sp, model=model) t.fit(y_train) yit = t.inverse_transform(t.transform(y_train)) np.testing.assert_array_equal(y_train.index, yit.index) np.testing.assert_almost_equal(y_train.values, yit.values)
def test_inverse_transform_time_index(sp, model): t = Deseasonalizer(sp=sp, model=model) t.fit(y_train) yit = t.inverse_transform(y_test) np.testing.assert_array_equal(yit.index, y_test.index)
def test_transform_time_index(sp, model): transformer = Deseasonalizer(sp=sp, model=model) transformer.fit(y_train) yt = transformer.transform(y_test) np.testing.assert_array_equal(yt.index, y_test.index)