class_docstr: Empirical and conformal prediction intervals.
  
  Implements empirical and conformal prediction intervals, on absolute residuals.
  Empirical prediction intervals are based on sliding window empirical quantiles.
  Conformal prediction intervals are implemented as described in [1]_.
  
  All intervals wrap an arbitrary forecaster, i.e., add probabilistic prediction
  capability to a given point prediction forecaster (first argument).
  
  method="conformal_bonferroni" is the method described in [1]_,
      where an arbitrary forecaster is used instead of the RNN.
  method="conformal" is the method in [1]_, but without Bonferroni correction.
      i.e., separate forecasts are made which results in H=1 (at all horizons).
  method="empirical" uses quantiles of relative signed residuals on training set,
      i.e., y_t+h^(i) - y-hat_t+h^(i), ranging over i, in the notation of [1]_,
      at quantiles 0.5-0.5*coverage (lower) and 0.5+0.5*coverage (upper),
      as offsets to the point prediction at forecast horizon h
  method="empirical_residual" uses empirical quantiles of absolute residuals
      on the training set, i.e., quantiles of epsilon-h (in notation [1]_),
      at quantile point (1-coverage)/2 quantiles, as offsets to point prediction
  
  Parameters
  ----------
  forecaster : estimator
      Estimator to which probabilistic forecasts are being added
  method : str, optional, default="empirical"
      "empirical": predictive interval bounds are empirical quantiles from training
      "empirical_residual": upper/lower are plusminus (1-coverage)/2 quantiles
          of the absolute residuals at horizon, i.e., of epsilon-h
      "conformal_bonferroni": Bonferroni, as in Stankeviciute et al
          Caveat: this does not give frequentist but conformal predictive intervals
      "conformal": as in Stankeviciute et al, but with H=1,
          i.e., no Bonferroni correction under number of indices in the horizon
  initial_window : float, int or None, optional (default=max(10, 0.1*len(y)))
      Defines the size of the initial training window
      If float, should be between 0.0 and 1.0 and represent the proportion
      of the dataset to include for the initial window for the train split.
      If int, represents the relative number of train samples in the initial window.
      If None, the value is set to the larger of 0.1*len(y) and 10
  sample_frac : float, optional, default=None
      value in range (0,1) corresponding to fraction of y index to calculate
      residuals matrix values for (for speeding up calculation)
  verbose : bool, optional, default=False
      whether to print warnings if windows with too few data points occur
  n_jobs : int or None, optional, default=1
      The number of jobs to run in parallel for fit.
      -1 means using all processors.
  
  References
  ----------
  .. [1] Kamile Stankeviciute, Ahmed M Alaa and Mihaela van der Schaar.
      Conformal Time Series Forecasting. NeurIPS 2021.
  
  Examples
  --------
  >>> from sktime.datasets import load_airline  # doctest: +SKIP
  >>> from sktime.forecasting.conformal import ConformalIntervals  # doctest: +SKIP
  >>> from sktime.forecasting.naive import NaiveForecaster  # doctest: +SKIP
  >>> y = load_airline()  # doctest: +SKIP
  >>> forecaster = NaiveForecaster(strategy="drift")  # doctest: +SKIP
  >>> conformal_forecaster = ConformalIntervals(forecaster)  # doctest: +SKIP
  >>> conformal_forecaster.fit(y, fh=[1, 2, 3])  # doctest: +SKIP
  ConformalIntervals(...)
  >>> pred_int = conformal_forecaster.predict_interval()  # doctest: +SKIP
  
  recommended use of ConformalIntervals together with ForecastingGridSearch
  is by 1. first running grid search, 2. then ConformalIntervals on the tuned params
  otherwise, nested sliding windows will cause high compute requirement
  
  >>> from sktime.datasets import load_airline
  >>> from sktime.forecasting.conformal import ConformalIntervals
  >>> from sktime.forecasting.naive import NaiveForecaster
  >>> from sktime.forecasting.model_selection import ForecastingGridSearchCV
  >>> from sktime.split import ExpandingWindowSplitter
  >>> from sktime.param_est.plugin import PluginParamsForecaster
  >>> # part 1 = grid search
  >>> cv = ExpandingWindowSplitter(fh=[1, 2, 3])  # doctest: +SKIP
  >>> forecaster = NaiveForecaster()  # doctest: +SKIP
  >>> param_grid = {"strategy" : ["last", "mean", "drift"]}  # doctest: +SKIP
  >>> gscv = ForecastingGridSearchCV(
  ...     forecaster=forecaster,
  ...     param_grid=param_grid,
  ...     cv=cv,
  ... )  # doctest: +SKIP
  >>> # part 2 = plug in results of grid search into conformal intervals estimator
  >>> conformal_with_fallback = ConformalIntervals(NaiveForecaster())
  >>> gscv_with_conformal = PluginParamsForecaster(
  ...     gscv,
  ...     conformal_with_fallback,
  ...     params={"forecaster": "best_forecaster"},
  ... )  # doctest: +SKIP
  >>> y = load_airline()  # doctest: +SKIP
  >>> gscv_with_conformal.fit(y, fh=[1, 2, 3])  # doctest: +SKIP
  PluginParamsForecaster(...)
  >>> y_pred_quantiles = gscv_with_conformal.predict_quantiles()  # doctest: +SKIP
class_docstr_tok: ["Empirical","and","conformal","prediction","intervals",".","Implements","empirical","and","conformal","prediction","intervals",",","on","absolute","residuals",".","Empirical","prediction","intervals","are","based","on","sliding","window","empirical","quantiles",".","Conformal","prediction","intervals","are","implemented","as","described","in","[","1","]","_",".","All","intervals","wrap","an","arbitrary","forecaster",",","i.e.",",","add","probabilistic","prediction","capability","to","a","given","point","prediction","forecaster","(","first","argument",")",".","method=","''","conformal_bonferroni","''","is","the","method","described","in","[","1","]","_",",","where","an","arbitrary","forecaster","is","used","instead","of","the","RNN",".","method=","''","conformal","''","is","the","method","in","[","1","]","_",",","but","without","Bonferroni","correction",".","i.e.",",","separate","forecasts","are","made","which","results","in","H=1","(","at","all","horizons",")",".","method=","''","empirical","''","uses","quantiles","of","relative","signed","residuals","on","training","set",",","i.e.",",","y_t+h^","(","i",")","-","y-hat_t+h^","(","i",")",",","ranging","over","i",",","in","the","notation","of","[","1","]","_",",","at","quantiles","0.5-0.5","*","coverage","(","lower",")","and","0.5+0.5","*","coverage","(","upper",")",",","as","offsets","to","the","point","prediction","at","forecast","horizon","h","method=","''","empirical_residual","''","uses","empirical","quantiles","of","absolute","residuals","on","the","training","set",",","i.e.",",","quantiles","of","epsilon-h","(","in","notation","[","1","]","_",")",",","at","quantile","point","(","1-coverage",")","\/2","quantiles",",","as","offsets","to","point","prediction","Parameters","--","--","--","--","--","forecaster",":","estimator","Estimator","to","which","probabilistic","forecasts","are","being","added","method",":","str",",","optional",",","default=","''","empirical","''","``","empirical","''",":","predictive","interval","bounds","are","empirical","quantiles","from","training","``","empirical_residual","''",":","upper\/lower","are","plusminus","(","1-coverage",")","\/2","quantiles","of","the","absolute","residuals","at","horizon",",","i.e.",",","of","epsilon-h","``","conformal_bonferroni","''",":","Bonferroni",",","as","in","Stankeviciute","et","al","Caveat",":","this","does","not","give","frequentist","but","conformal","predictive","intervals","``","conformal","''",":","as","in","Stankeviciute","et","al",",","but","with","H=1",",","i.e.",",","no","Bonferroni","correction","under","number","of","indices","in","the","horizon","initial_window",":","float",",","int","or","None",",","optional","(","default=max","(","10",",","0.1","*","len","(","y",")",")",")","Defines","the","size","of","the","initial","training","window","If","float",",","should","be","between","0.0","and","1.0","and","represent","the","proportion","of","the","dataset","to","include","for","the","initial","window","for","the","train","split",".","If","int",",","represents","the","relative","number","of","train","samples","in","the","initial","window",".","If","None",",","the","value","is","set","to","the","larger","of","0.1","*","len","(","y",")","and","10","sample_frac",":","float",",","optional",",","default=None","value","in","range","(","0,1",")","corresponding","to","fraction","of","y","index","to","calculate","residuals","matrix","values","for","(","for","speeding","up","calculation",")","verbose",":","bool",",","optional",",","default=False","whether","to","print","warnings","if","windows","with","too","few","data","points","occur","n_jobs",":","int","or","None",",","optional",",","default=1","The","number","of","jobs","to","run","in","parallel","for","fit",".","-1","means","using","all","processors",".","References","--","--","--","--","--","..","[","1","]","Kamile","Stankeviciute",",","Ahmed","M","Alaa","and","Mihaela","van","der","Schaar",".","Conformal","Time","Series","Forecasting",".","NeurIPS","2021",".","Examples","--","--","--","--",">",">",">","from","sktime.datasets","import","load_airline","#","doctest",":","+SKIP",">",">",">","from","sktime.forecasting.conformal","import","ConformalIntervals","#","doctest",":","+SKIP",">",">",">","from","sktime.forecasting.naive","import","NaiveForecaster","#","doctest",":","+SKIP",">",">",">","y","=","load_airline","(",")","#","doctest",":","+SKIP",">",">",">","forecaster","=","NaiveForecaster","(","strategy=","''","drift","''",")","#","doctest",":","+SKIP",">",">",">","conformal_forecaster","=","ConformalIntervals","(","forecaster",")","#","doctest",":","+SKIP",">",">",">","conformal_forecaster.fit","(","y",",","fh=","[","1",",","2",",","3","]",")","#","doctest",":","+SKIP","ConformalIntervals","(","...",")",">",">",">","pred_int","=","conformal_forecaster.predict_interval","(",")","#","doctest",":","+SKIP","recommended","use","of","ConformalIntervals","together","with","ForecastingGridSearch","is","by","1.","first","running","grid","search",",","2.","then","ConformalIntervals","on","the","tuned","params","otherwise",",","nested","sliding","windows","will","cause","high","compute","requirement",">",">",">","from","sktime.datasets","import","load_airline",">",">",">","from","sktime.forecasting.conformal","import","ConformalIntervals",">",">",">","from","sktime.forecasting.naive","import","NaiveForecaster",">",">",">","from","sktime.forecasting.model_selection","import","ForecastingGridSearchCV",">",">",">","from","sktime.split","import","ExpandingWindowSplitter",">",">",">","from","sktime.param_est.plugin","import","PluginParamsForecaster",">",">",">","#","part","1","=","grid","search",">",">",">","cv","=","ExpandingWindowSplitter","(","fh=","[","1",",","2",",","3","]",")","#","doctest",":","+SKIP",">",">",">","forecaster","=","NaiveForecaster","(",")","#","doctest",":","+SKIP",">",">",">","param_grid","=","{","``","strategy","''",":","[","``","last","''",",","``","mean","''",",","``","drift","''","]","}","#","doctest",":","+SKIP",">",">",">","gscv","=","ForecastingGridSearchCV","(","...","forecaster=forecaster",",","...","param_grid=param_grid",",","...","cv=cv",",","...",")","#","doctest",":","+SKIP",">",">",">","#","part","2","=","plug","in","results","of","grid","search","into","conformal","intervals","estimator",">",">",">","conformal_with_fallback","=","ConformalIntervals","(","NaiveForecaster","(",")",")",">",">",">","gscv_with_conformal","=","PluginParamsForecaster","(","...","gscv",",","...","conformal_with_fallback",",","...","params=","{","``","forecaster","''",":","``","best_forecaster","''","}",",","...",")","#","doctest",":","+SKIP",">",">",">","y","=","load_airline","(",")","#","doctest",":","+SKIP",">",">",">","gscv_with_conformal.fit","(","y",",","fh=","[","1",",","2",",","3","]",")","#","doctest",":","+SKIP","PluginParamsForecaster","(","...",")",">",">",">","y_pred_quantiles","=","gscv_with_conformal.predict_quantiles","(",")","#","doctest",":","+SKIP"]
docstr: Compute sliding residuals used in uncertainty estimates.
  
  Parameters
  ----------
  y : pd.Series or pd.DataFrame
      sktime compatible time series to use in computing residuals matrix
  X : pd.DataFrame
      sktime compatible exogeneous time series to use in forecasts
  forecaster : sktime compatible forecaster
      forecaster to use in computing the sliding residuals
  initial_window : float, int or None, optional (default=max(10, 0.1*len(y)))
      Defines the size of the initial training window
      If float, should be between 0.0 and 1.0 and represent the proportion
      of the dataset to include for the initial window for the train split.
      If int, represents the relative number of train samples in the
      initial window.
      If None, the value is set to the larger of 0.1*len(y) and 10
  sample_frac : float
      for speeding up computing of residuals matrix.
      sample value in range (0, 1) to obtain a fraction of y indices to
      compute residuals matrix for
  update : bool
      Whether residuals_matrix has been calculated previously and just
      needs extending. Default = False
  Returns
  -------
  residuals_matrix : pd.DataFrame, row and column index = y.index[initial_window:]
      [i,j]-th entry is signed residual of forecasting y.loc[j] from y.loc[:i],
      using a clone of the forecaster passed through the forecaster arg.
      if sample_frac is passed this will have NaN values for 1 - sample_frac
      fraction of the matrix
docstr_tok: ["Compute","sliding","residuals","used","in","uncertainty","estimates",".","Parameters","--","--","--","--","--","y",":","pd.Series","or","pd.DataFrame","sktime","compatible","time","series","to","use","in","computing","residuals","matrix","X",":","pd.DataFrame","sktime","compatible","exogeneous","time","series","to","use","in","forecasts","forecaster",":","sktime","compatible","forecaster","forecaster","to","use","in","computing","the","sliding","residuals","initial_window",":","float",",","int","or","None",",","optional","(","default=max","(","10",",","0.1","*","len","(","y",")",")",")","Defines","the","size","of","the","initial","training","window","If","float",",","should","be","between","0.0","and","1.0","and","represent","the","proportion","of","the","dataset","to","include","for","the","initial","window","for","the","train","split",".","If","int",",","represents","the","relative","number","of","train","samples","in","the","initial","window",".","If","None",",","the","value","is","set","to","the","larger","of","0.1","*","len","(","y",")","and","10","sample_frac",":","float","for","speeding","up","computing","of","residuals","matrix",".","sample","value","in","range","(","0",",","1",")","to","obtain","a","fraction","of","y","indices","to","compute","residuals","matrix","for","update",":","bool","Whether","residuals_matrix","has","been","calculated","previously","and","just","needs","extending",".","Default","=","False","Returns","--","--","--","-","residuals_matrix",":","pd.DataFrame",",","row","and","column","index","=","y.index","[","initial_window",":","]","[","i",",","j","]","-th","entry","is","signed","residual","of","forecasting","y.loc","[","j","]","from","y.loc","[",":","i","]",",","using","a","clone","of","the","forecaster","passed","through","the","forecaster","arg",".","if","sample_frac","is","passed","this","will","have","NaN","values","for","1","-","sample_frac","fraction","of","the","matrix"]
code: def _compute_sliding_residuals(
      self, y, X, forecaster, initial_window, sample_frac, update=False
  ):
      """Compute sliding residuals used in uncertainty estimates.
  
      Parameters
      ----------
      y : pd.Series or pd.DataFrame
          sktime compatible time series to use in computing residuals matrix
      X : pd.DataFrame
          sktime compatible exogeneous time series to use in forecasts
      forecaster : sktime compatible forecaster
          forecaster to use in computing the sliding residuals
      initial_window : float, int or None, optional (default=max(10, 0.1*len(y)))
          Defines the size of the initial training window
          If float, should be between 0.0 and 1.0 and represent the proportion
          of the dataset to include for the initial window for the train split.
          If int, represents the relative number of train samples in the
          initial window.
          If None, the value is set to the larger of 0.1*len(y) and 10
      sample_frac : float
          for speeding up computing of residuals matrix.
          sample value in range (0, 1) to obtain a fraction of y indices to
          compute residuals matrix for
      update : bool
          Whether residuals_matrix has been calculated previously and just
          needs extending. Default = False
      Returns
      -------
      residuals_matrix : pd.DataFrame, row and column index = y.index[initial_window:]
          [i,j]-th entry is signed residual of forecasting y.loc[j] from y.loc[:i],
          using a clone of the forecaster passed through the forecaster arg.
          if sample_frac is passed this will have NaN values for 1 - sample_frac
          fraction of the matrix
      """
      y = convert_to(
          y, ["pd.Series", "pd-multiindex", "pd_multiindex_hier"]
      )
  
      # vectorize over multiindex if y is hierarchical
      if isinstance(y.index, pd.MultiIndex):
          y_index = y.index.droplevel(-1)
  
          residuals = {}
          for ix in y_index:
              if X is not None:
                  X_ix = X.loc[ix]
              else:
                  X_ix = None
              y_ix = y.loc[ix]
              residuals[ix] = self._compute_sliding_residuals(
                  y_ix,
                  X_ix,
                  forecaster,
                  initial_window,
                  sample_frac,
                  update,
              )
          return residuals
  
      n_initial_window = self._parse_initial_window(
          y, initial_window=initial_window
      )
  
      full_y_index = y.iloc[n_initial_window:].index
  
      residuals_matrix = pd.DataFrame(
          columns=full_y_index, index=full_y_index, dtype="float"
      )
  
      if (
          update
          and hasattr(self, "residuals_matrix_")
          and not sample_frac
      ):
          remaining_y_index = full_y_index.difference(
              self.residuals_matrix_.index
          )
          if len(remaining_y_index) != len(full_y_index):
              overlapping_index = pd.Index(
                  self.residuals_matrix_.index.intersection(
                      full_y_index
                  )
              ).sort_values()
              residuals_matrix.loc[
                  overlapping_index, overlapping_index
              ] = self.residuals_matrix_.loc[
                  overlapping_index, overlapping_index
              ]
          else:
              overlapping_index = None
          y_index = remaining_y_index
      else:
          y_index = full_y_index
          overlapping_index = None
  
      if sample_frac:
          y_sample = y_index.to_series().sample(frac=sample_frac)
          if len(y_sample) > 2:
              y_index = y_sample
  
      def _get_residuals_matrix_row(forecaster, y, X, id):
          y_train = get_slice(
              y, start=None, end=id
          )  # subset on which we fit
          y_test = get_slice(
              y, start=id, end=None
          )  # subset on which we predict
  
          X_train = get_slice(X, start=None, end=id)
          X_test = get_slice(X, start=id, end=None)
          forecaster.fit(y_train, X=X_train, fh=y_test.index)
          # Append fitted forecaster to list for extending for update
          self.forecasters_.append(
              {"id": str(id), "forecaster": forecaster}
          )
  
          try:
              residuals = forecaster.predict_residuals(y_test, X_test)
          except IndexError:
              warn(
                  "Couldn't predict after fitting on time series of length"
                  f"{len(y_train)}.",
                  obj=forecaster,
              )
          return residuals
  
      all_residuals = Parallel(n_jobs=self.n_jobs)(
          delayed(_get_residuals_matrix_row)(
              forecaster.clone(), y, X, id
          )
          for id in y_index
      )
      for idx, id in enumerate(y_index):
          residuals_matrix.loc[id] = all_residuals[idx]
  
      if overlapping_index is not None:
  
          def _extend_residuals_matrix_row(y, X, id):
              forecasters_df = pd.DataFrame(self.forecasters_)
              forecaster_to_extend = forecasters_df.loc[
                  forecasters_df["id"] == str(id)
              ]["forecaster"].values[0]
  
              y_test = get_slice(y, start=id, end=None)
              X_test = get_slice(X, start=id, end=None)
  
              try:
                  residuals = forecaster_to_extend.predict_residuals(
                      y_test, X_test
                  )
              except IndexError:
                  warn(
                      f"Couldn't predict with existing forecaster for cutoff {id}"
                      " with existing forecaster.",
                      obj=forecaster,
                  )
              return residuals
  
          extend_residuals = Parallel(n_jobs=self.n_jobs)(
              delayed(_extend_residuals_matrix_row)(y, X, id)
              for id in overlapping_index
          )
  
          for idx, id in enumerate(overlapping_index):
              residuals_matrix.loc[id] = extend_residuals[idx]
  
      return residuals_matrix
  
file_path: sktime/sktime/forecasting/conformal.py
imports: from math import floor
  import numpy
  import pandas
  from joblib import Parallel, delayed
  from sklearn.base import clone
  from sktime.datatypes import MTYPE_LIST_SERIES, convert, convert_to
  from sktime.datatypes._utilities import get_slice
  from sktime.forecasting.base import BaseForecaster
  from sktime.utils.warnings import warn
label_desc: Use image node_id 8 for calling the ConformalIntervals obj's underlying member method code with example usage: obj._compute_sliding_residuals(y, X, forecaster, initial_window, sample_frac, update) and returns: residuals_matrix, residuals, residuals, residuals