Skip to content

ForecastingHorizon

ForecastingHorizon

class ForecastingHorizon(values=None, is_relative=None, freq=None)[source]

Forecasting horizon.

Parameters:
valuespd.Index, pd.TimedeltaIndex, np.array, list, pd.Timedelta, or int

Values of forecasting horizon

  • int, positive: interpreted as forecasting horizon at period offsets 1, 2, …, int.

  • int, non-positive: interpreted as forecasting horizon at number of periods in the past or present relative to the cutoff, with a single period offset, the integer. At the default is_relative=True, zero is the cutoff itself, i.e., nowcasting the last observation. Negative integers are interpreted as in-sample forecasting horizon values.

  • range: interpreted as forecasting horizon with values in the range

  • pd.Index of supported type: interpreted as forecasting horizon with values as in the index.

  • iterable of int or pd.Timedelta or date offset: interpreted as forecasting horizon with values in the iterable. Whether relative or absolute forecasting horizon is determined by the type of the values.

is_relativebool, optional (default=None)
  • If True, a relative ForecastingHorizon is created: values are relative to end of training series.

  • If False, an absolute ForecastingHorizon is created: values are absolute.

  • if None, the flag is determined automatically: relative, if values are of supported relative index type: integer-like, timedelta-like, or date offset-like types. absolute, if not relative and values of supported absolute index type: time index types, e.g., DatetimeIndex or PeriodIndex.

freqstr, pd.Index, pandas offset, or sktime forecaster, optional (default=None)

object carrying frequency information on values ignored unless values is without inferable freq

Attributes:
freq

Frequency attribute.

is_relative

Whether forecasting horizon is relative to the end of the training series.

Examples

>>> from sktime.forecasting.base import ForecastingHorizon
>>> from sktime.forecasting.naive import NaiveForecaster
>>> from sktime.datasets import load_airline
>>> from sktime.split import temporal_train_test_split
>>> import numpy as np
>>> y = load_airline()
>>> y_train, y_test = temporal_train_test_split(y, test_size=6)

List as ForecastingHorizon

>>> ForecastingHorizon([1, 2, 3])
>>> # ForecastingHorizon([1, 2, 3], is_relative=True)

Numpy as ForecastingHorizon

>>> ForecastingHorizon(np.arange(1, 7))
>>> # ForecastingHorizon([1, 2, 3, 4, 5, 6], is_relative=True)

Absolute ForecastingHorizon with a pandas Index

>>> ForecastingHorizon(y_test.index, is_relative=False)
>>> # ForecastingHorizon(['1960-07', ..., '1960-12'], is_relative=False)

Converting

>>> # set cutoff (last time point of training data)
>>> cutoff = y_train.index[-1]
>>> cutoff
Period('1960-06', 'M')
>>> # to_relative
>>> fh = ForecastingHorizon(y_test.index, is_relative=False)
>>> fh.to_relative(cutoff=cutoff)
>>> # ForecastingHorizon([1, 2, 3, 4, 5, 6], is_relative=True)
>>> # to_absolute
>>> fh = ForecastingHorizon([1, 2, 3, 4, 5, 6], is_relative=True)
>>> fh = fh.to_absolute(cutoff=cutoff)
>>> # ForecastingHorizon(['1960-07', ..., '1960-12'], is_relative=False)

Automatically casted ForecastingHorizon from list when calling predict()

>>> forecaster = NaiveForecaster(strategy="drift")
>>> forecaster.fit(y_train)
NaiveForecaster(...)
>>> y_pred = forecaster.predict(fh=[1,2,3])
>>> forecaster.fh
>>> # ForecastingHorizon([1, 2, 3], dtype='int64', is_relative=True)

This is identical to give an object of ForecastingHorizon

>>> y_pred = forecaster.predict(fh=ForecastingHorizon([1,2,3]))
>>> forecaster.fh
>>> # ForecastingHorizon([1, 2, 3], dtype='int64', is_relative=True)

Methods

get_expected_pred_idx([y, cutoff, sort_by_time])

Construct DataFrame Index expected in y_pred, return of _predict.

is_all_in_sample([cutoff])

Whether the forecasting horizon is purely in-sample for given cutoff.

is_all_out_of_sample([cutoff])

Whether the forecasting horizon is purely out-of-sample for given cutoff.

to_absolute(cutoff)

Return absolute version of forecasting horizon values.

to_absolute_index([cutoff])

Return absolute values of the horizon as a pandas.Index.

to_absolute_int(start[, cutoff])

Return absolute values as zero-based integer index starting from start.

to_in_sample([cutoff])

Return in-sample index values of fh.

to_indexer([cutoff, from_cutoff])

Return zero-based indexer values for easy indexing into arrays.

to_numpy(**kwargs)

Return forecasting horizon's underlying values as np.array.

to_out_of_sample([cutoff])

Return out-of-sample values of fh.

to_pandas()

Return forecasting horizon's underlying values as pd.Index.

to_relative([cutoff])

Return forecasting horizon values relative to a cutoff.