class_docstr: Flexible splitter for time-series cross validation and rolling window evaluation.
  Suitable for use in GridSearchCV.
  
  Attributes
  ----------
  min_splits : int
      Guaranteed min number of splits. This is always set to 1. If provided configuration results in 0 splits,
      the cross validator will yield a default split.
  
  __starting_test_index : int
      Test end index of the first CV split. Actual offset = __starting_test_index + _get_offset(X), for a particular
      dataset X.
      Cross validator ensures the last test split contains the last observation in X.
  
  Examples
  --------
  >>> from greykite.sklearn.cross_validation import RollingTimeSeriesSplit
  >>> X = np.random.rand(20, 4)
  >>> tscv = RollingTimeSeriesSplit(forecast_horizon=3, max_splits=4)
  >>> tscv.get_n_splits(X=X)
  4
  >>> for train, test in tscv.split(X=X):
  ...     print(train, test)
  [2 3 4 5 6 7] [ 8  9 10]
  [ 5  6  7  8  9 10] [11 12 13]
  [ 8  9 10 11 12 13] [14 15 16]
  [11 12 13 14 15 16] [17 18 19]
  >>> X = np.random.rand(20, 4)
  >>> tscv = RollingTimeSeriesSplit(forecast_horizon=2,
  ...                               min_train_periods=4,
  ...                               expanding_window=True,
  ...                               periods_between_splits=4,
  ...                               periods_between_train_test=2,
  ...                               max_splits=None)
  >>> tscv.get_n_splits(X=X)
  4
  >>> for train, test in tscv.split(X=X):
  ...     print(train, test)
  [0 1 2 3] [6 7]
  [0 1 2 3 4 5 6 7] [10 11]
  [ 0  1  2  3  4  5  6  7  8  9 10 11] [14 15]
  [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15] [18 19]
  >>> X = np.random.rand(5, 4)  # default split if there is not enough data
  >>> for train, test in tscv.split(X=X):
  ...     print(train, test)
  [0 1 2 3] [4]
class_docstr_tok: ["Flexible","splitter","for","time-series","cross","validation","and","rolling","window","evaluation",".","Suitable","for","use","in","GridSearchCV",".","Attributes","--","--","--","--","--","min_splits",":","int","Guaranteed","min","number","of","splits",".","This","is","always","set","to","1",".","If","provided","configuration","results","in","0","splits",",","the","cross","validator","will","yield","a","default","split",".","__starting_test_index",":","int","Test","end","index","of","the","first","CV","split",".","Actual","offset","=","__starting_test_index","+","_get_offset","(","X",")",",","for","a","particular","dataset","X",".","Cross","validator","ensures","the","last","test","split","contains","the","last","observation","in","X",".","Examples","--","--","--","--",">",">",">","from","greykite.sklearn.cross_validation","import","RollingTimeSeriesSplit",">",">",">","X","=","np.random.rand","(","20",",","4",")",">",">",">","tscv","=","RollingTimeSeriesSplit","(","forecast_horizon=3",",","max_splits=4",")",">",">",">","tscv.get_n_splits","(","X=X",")","4",">",">",">","for","train",",","test","in","tscv.split","(","X=X",")",":","...","print","(","train",",","test",")","[","2","3","4","5","6","7","]","[","8","9","10","]","[","5","6","7","8","9","10","]","[","11","12","13","]","[","8","9","10","11","12","13","]","[","14","15","16","]","[","11","12","13","14","15","16","]","[","17","18","19","]",">",">",">","X","=","np.random.rand","(","20",",","4",")",">",">",">","tscv","=","RollingTimeSeriesSplit","(","forecast_horizon=2",",","...","min_train_periods=4",",","...","expanding_window=True",",","...","periods_between_splits=4",",","...","periods_between_train_test=2",",","...","max_splits=None",")",">",">",">","tscv.get_n_splits","(","X=X",")","4",">",">",">","for","train",",","test","in","tscv.split","(","X=X",")",":","...","print","(","train",",","test",")","[","0","1","2","3","]","[","6","7","]","[","0","1","2","3","4","5","6","7","]","[","10","11","]","[","0","1","2","3","4","5","6","7","8","9","10","11","]","[","14","15","]","[","0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","]","[","18","19","]",">",">",">","X","=","np.random.rand","(","5",",","4",")","#","default","split","if","there","is","not","enough","data",">",">",">","for","train",",","test","in","tscv.split","(","X=X",")",":","...","print","(","train",",","test",")","[","0","1","2","3","]","[","4","]"]
docstr: Initializes attributes of RollingTimeSeriesSplit
  
  Parameters
  ----------
  forecast_horizon : `int`
      How many periods in each CV test set
  
  min_train_periods : `int` or None, optional
      Minimum number of periods for training.
      If ``expanding_window`` is False, every training period has this size.
  
  expanding_window : `bool`, default False
      If True, training window for each CV split is fixed to the first available date.
      Otherwise, train start date is sliding, determined by ``min_train_periods``.
  
  use_most_recent_splits: `bool`, default False
      If True, splits from the end of the dataset are used.
      Else a sampling strategy is applied. Check
      `~greykite.sklearn.cross_validation.RollingTimeSeriesSplit._sample_splits`
      for details.
  
  periods_between_splits : `int` or None
      Number of periods to slide the test window
  
  periods_between_train_test : `int`
      Number of periods gap between train and test within a CV split
  
  max_splits : `int` or None
      Maximum number of CV splits. Given the above configuration, samples up to max_splits train/test splits,
      preferring splits toward the end of available data. If None, uses all splits.
docstr_tok: ["Initializes","attributes","of","RollingTimeSeriesSplit","Parameters","--","--","--","--","--","forecast_horizon",":","`","int","`","How","many","periods","in","each","CV","test","set","min_train_periods",":","`","int","`","or","None",",","optional","Minimum","number","of","periods","for","training",".","If","``","expanding_window","``","is","False",",","every","training","period","has","this","size",".","expanding_window",":","`","bool","`",",","default","False","If","True",",","training","window","for","each","CV","split","is","fixed","to","the","first","available","date",".","Otherwise",",","train","start","date","is","sliding",",","determined","by","``","min_train_periods","``",".","use_most_recent_splits",":","`","bool","`",",","default","False","If","True",",","splits","from","the","end","of","the","dataset","are","used",".","Else","a","sampling","strategy","is","applied",".","Check","`","~greykite.sklearn.cross_validation.RollingTimeSeriesSplit._sample_splits","`","for","details",".","periods_between_splits",":","`","int","`","or","None","Number","of","periods","to","slide","the","test","window","periods_between_train_test",":","`","int","`","Number","of","periods","gap","between","train","and","test","within","a","CV","split","max_splits",":","`","int","`","or","None","Maximum","number","of","CV","splits",".","Given","the","above","configuration",",","samples","up","to","max_splits","train\/test","splits",",","preferring","splits","toward","the","end","of","available","data",".","If","None",",","uses","all","splits","."]
code: def __init__(
      self,
      forecast_horizon,
      min_train_periods=None,
      expanding_window=False,
      use_most_recent_splits=False,
      periods_between_splits=None,
      periods_between_train_test=0,
      max_splits=3,
  ):
      """Initializes attributes of RollingTimeSeriesSplit
  
      Parameters
      ----------
      forecast_horizon : `int`
          How many periods in each CV test set
  
      min_train_periods : `int` or None, optional
          Minimum number of periods for training.
          If ``expanding_window`` is False, every training period has this size.
  
      expanding_window : `bool`, default False
          If True, training window for each CV split is fixed to the first available date.
          Otherwise, train start date is sliding, determined by ``min_train_periods``.
  
      use_most_recent_splits: `bool`, default False
          If True, splits from the end of the dataset are used.
          Else a sampling strategy is applied. Check
          `~greykite.sklearn.cross_validation.RollingTimeSeriesSplit._sample_splits`
          for details.
  
      periods_between_splits : `int` or None
          Number of periods to slide the test window
  
      periods_between_train_test : `int`
          Number of periods gap between train and test within a CV split
  
      max_splits : `int` or None
          Maximum number of CV splits. Given the above configuration, samples up to max_splits train/test splits,
          preferring splits toward the end of available data. If None, uses all splits.
      """
      super().__init__()
      self.forecast_horizon = get_integer(
          forecast_horizon, name="forecast_horizon", min_value=1
      )
  
      # by default, use at least twice the forecast horizon for training
      self.min_train_periods = get_integer(
          min_train_periods,
          name="min_train_periods",
          min_value=1,
          default_value=2 * self.forecast_horizon,
      )
  
      # by default, use fixed size training window
      self.expanding_window = expanding_window
  
      # by default, does not force most recent splits
      self.use_most_recent_splits = use_most_recent_splits
  
      # by default, use non-overlapping test sets
      self.periods_between_splits = get_integer(
          periods_between_splits,
          name="periods_between_splits",
          min_value=1,
          default_value=self.forecast_horizon,
      )
  
      # by default, use test set immediately following train set
      self.periods_between_train_test = get_integer(
          periods_between_train_test,
          name="periods_between_train_test",
          min_value=0,
          default_value=0,
      )
  
      if self.min_train_periods < 2 * self.forecast_horizon:
          warnings.warn(
              f"`min_train_periods` is too small for your `forecast_horizon`. Should be at least"
              f" {forecast_horizon*2}=2*`forecast_horizon`."
          )
  
      self.max_splits = max_splits
      self.min_splits = (
          1  # CV ensures there is always at least one split
      )
      # test end index for the first CV split, before applying offset to ensure last data point in X is used
      self.__starting_test_index = (
          self.forecast_horizon
          + self.min_train_periods
          + self.periods_between_train_test
          - 1
      )
  
file_path: greykite/greykite/sklearn/cross_validation.py
imports: import math
  import random
  import warnings
  import numpy
  from sklearn.model_selection import BaseCrossValidator
  from greykite.common.logging import LoggingLevelEnum
  from greykite.common.logging import log_message
  from greykite.common.python_utils import get_integer
label_desc: Use image node_id 1 to create a new RollingTimeSeriesSplit object from inherited base classes: BaseCrossValidator with example: obj = RollingTimeSeriesSplit(forecast_horizon, min_train_periods, expanding_window, use_most_recent_splits, periods_between_splits, periods_between_train_test, max_splits)