def fit(self, series: TimeSeries): super().fit(series) series._assert_univariate() series = self.training_series if self.version == "tsb": self.forecast_val = self.method( series.values(copy=False), h=1, future_xreg=None, alpha_d=self.alpha_d, alpha_p=self.alpha_p, ) elif self.version == "sba": try: self.forecast_val = self.method(series.values(copy=False), h=1, future_xreg=None) except errors.TypingError: raise_if( True, '"sba" version is not supported with this version of statsforecast.', ) else: self.forecast_val = self.method(series.values(copy=False), h=1, future_xreg=None) return self
def residuals(self, series: TimeSeries, forecast_horizon: int = 1, verbose: bool = False) -> TimeSeries: """ A function for computing the residuals produced by the current model on a univariate time series. This function computes the difference between the actual observations from `series` and the fitted values vector `p` obtained by training the model on `series`. For every index `i` in `series`, `p[i]` is computed by training the model on `series[:(i - forecast_horizon)]` and forecasting `forecast_horizon` into the future. (`p[i]` will be set to the last value of the predicted series.) The vector of residuals will be shorter than `series` due to the minimum training series length required by the model and the gap introduced by `forecast_horizon`. Most commonly, unless otherwise specified, the term "residuals" implies a value for `forecast_horizon` of 1. This method works only on univariate series and does not currently support covariates. Parameters ---------- series The univariate TimeSeries instance which the residuals will be computed for. forecast_horizon The forecasting horizon used to predict each fitted value. verbose Whether to print progress. Returns ------- TimeSeries The vector of residuals. """ series._assert_univariate() # get first index not contained in the first training set first_index = series.time_index()[self.min_train_series_length] # compute fitted values p = self.historical_forecasts(series=series, start=first_index, forecast_horizon=forecast_horizon, stride=1, retrain=True, last_points_only=True, verbose=verbose) # compute residuals series_trimmed = series.slice_intersect(p) residuals = series_trimmed - p return residuals