class_docstr: Compute the prediction variance based on a naive strategy.
  
  NaiveVariance adds to a `forecaster` the ability to compute the
  prediction variance based on naive assumptions about the time series.
  The simple strategy is as follows:
  - Let :math:`y_1,\dots,y_T` be the time series we fit the estimator :math:`f` to.
  - Let :math:`\widehat{y}_{ij}` be the forecast for time point :math:`j`, obtained
  from fitting the forecaster to the partial time series :math:`y_1,\dots,y_i`.
  - We compute the residuals matrix :math:`R=(r_{ij})=(y_j-\widehat{y}_{ij})`.
  - The variance prediction :math:`v_k` for :math:`y_{T+k}` is
  :math:`\frac{1}{T-k}\sum_{i=1}^{T-k} a_{i,i+k}^2`
  because we are averaging squared residuals of all forecasts that are :math:`k`
  time points ahead.
  - And for the covariance matrix prediction, the formula becomes
  :math:`Cov(y_k, y_l)=\frac{\sum_{i=1}^N \hat{r}_{k,k+i}*\hat{r}_{l,l+i}}{N}`.
  
  The resulting forecaster will implement
      `predict_interval`, `predict_quantiles`, `predict_var`, and `predict_proba`,
      even if the wrapped forecaster `forecaster` did not have this capability;
      for point forecasts (`predict`), behaves like the wrapped forecaster.
  
  Parameters
  ----------
  forecaster : estimator
      Estimator to which probabilistic forecasts are being added
  initial_window : int, optional, default=1
      number of minimum initial indices to use for fitting when computing residuals
  verbose : bool, optional, default=False
      whether to print warnings if windows with too few data points occur
  
  Examples
  --------
  >>> from sktime.datasets import load_airline
  >>> from sktime.forecasting.naive import NaiveForecaster, NaiveVariance
  >>> y = load_airline()
  >>> forecaster = NaiveForecaster(strategy="drift")
  >>> variance_forecaster = NaiveVariance(forecaster)
  >>> variance_forecaster.fit(y)
  NaiveVariance(...)
  >>> var_pred = variance_forecaster.predict_var(fh=[1,2,3])
class_docstr_tok: ["Compute","the","prediction","variance","based","on","a","naive","strategy",".","NaiveVariance","adds","to","a","`","forecaster","`","the","ability","to","compute","the","prediction","variance","based","on","naive","assumptions","about","the","time","series",".","The","simple","strategy","is","as","follows",":","-","Let",":","math",":","`","y_1",",","\\dots",",","y_T","`","be","the","time","series","we","fit","the","estimator",":","math",":","`","f","`","to",".","-","Let",":","math",":","`","\\widehat","{","y","}","_","{","ij","}","`","be","the","forecast","for","time","point",":","math",":","`","j","`",",","obtained","from","fitting","the","forecaster","to","the","partial","time","series",":","math",":","`","y_1",",","\\dots",",","y_i","`",".","-","We","compute","the","residuals","matrix",":","math",":","`","R=","(","r_","{","ij","}",")","=","(","y_j-\\widehat","{","y","}","_","{","ij","}",")","`",".","-","The","variance","prediction",":","math",":","`","v_k","`","for",":","math",":","`","y_","{","T+k","}","`","is",":","math",":","`","\\frac","{","1","}","{","T-k","}","\\sum_","{","i=1","}","^","{","T-k","}","a_","{","i",",","i+k","}","^2","`","because","we","are","averaging","squared","residuals","of","all","forecasts","that","are",":","math",":","`","k","`","time","points","ahead",".","-","And","for","the","covariance","matrix","prediction",",","the","formula","becomes",":","math",":","`","Cov","(","y_k",",","y_l",")","=\\frac","{","\\sum_","{","i=1","}","^N","\\hat","{","r","}","_","{","k",",","k+i","}","*","\\hat","{","r","}","_","{","l",",","l+i","}","}","{","N","}","`",".","The","resulting","forecaster","will","implement","`","predict_interval","`",",","`","predict_quantiles","`",",","`","predict_var","`",",","and","`","predict_proba","`",",","even","if","the","wrapped","forecaster","`","forecaster","`","did","not","have","this","capability",";","for","point","forecasts","(","`","predict","`",")",",","behaves","like","the","wrapped","forecaster",".","Parameters","--","--","--","--","--","forecaster",":","estimator","Estimator","to","which","probabilistic","forecasts","are","being","added","initial_window",":","int",",","optional",",","default=1","number","of","minimum","initial","indices","to","use","for","fitting","when","computing","residuals","verbose",":","bool",",","optional",",","default=False","whether","to","print","warnings","if","windows","with","too","few","data","points","occur","Examples","--","--","--","--",">",">",">","from","sktime.datasets","import","load_airline",">",">",">","from","sktime.forecasting.naive","import","NaiveForecaster",",","NaiveVariance",">",">",">","y","=","load_airline","(",")",">",">",">","forecaster","=","NaiveForecaster","(","strategy=","''","drift","''",")",">",">",">","variance_forecaster","=","NaiveVariance","(","forecaster",")",">",">",">","variance_forecaster.fit","(","y",")","NaiveVariance","(","...",")",">",">",">","var_pred","=","variance_forecaster.predict_var","(","fh=","[","1,2,3","]",")"]
docstr: Compute/return prediction variance for a forecast.
  
  Must be run *after* the forecaster has been fitted.
  
  Parameters
  ----------
  fh : int, list, np.array or ForecastingHorizon
      Forecasting horizon
  X : pd.DataFrame, optional (default=None)
      Exogenous time series
  cov : bool, optional (default=False)
      If True, return the covariance matrix.
      If False, return the marginal variance.
  
  Returns
  -------
  pred_var :
      if cov=False, pd.DataFrame with index fh.
          a vector of same length as fh with predictive marginal variances;
      if cov=True, pd.DataFrame with index fh and columns fh.
          a square matrix of size len(fh) with predictive covariance matrix.
docstr_tok: ["Compute\/return","prediction","variance","for","a","forecast",".","Must","be","run","*","after","*","the","forecaster","has","been","fitted",".","Parameters","--","--","--","--","--","fh",":","int",",","list",",","np.array","or","ForecastingHorizon","Forecasting","horizon","X",":","pd.DataFrame",",","optional","(","default=None",")","Exogenous","time","series","cov",":","bool",",","optional","(","default=False",")","If","True",",","return","the","covariance","matrix",".","If","False",",","return","the","marginal","variance",".","Returns","--","--","--","-","pred_var",":","if","cov=False",",","pd.DataFrame","with","index","fh",".","a","vector","of","same","length","as","fh","with","predictive","marginal","variances",";","if","cov=True",",","pd.DataFrame","with","index","fh","and","columns","fh",".","a","square","matrix","of","size","len","(","fh",")","with","predictive","covariance","matrix","."]
code: def _predict_var(self, fh, X=None, cov=False):
      """Compute/return prediction variance for a forecast.
  
      Must be run *after* the forecaster has been fitted.
  
      Parameters
      ----------
      fh : int, list, np.array or ForecastingHorizon
          Forecasting horizon
      X : pd.DataFrame, optional (default=None)
          Exogenous time series
      cov : bool, optional (default=False)
          If True, return the covariance matrix.
          If False, return the marginal variance.
  
      Returns
      -------
      pred_var :
          if cov=False, pd.DataFrame with index fh.
              a vector of same length as fh with predictive marginal variances;
          if cov=True, pd.DataFrame with index fh and columns fh.
              a square matrix of size len(fh) with predictive covariance matrix.
      """
      if self.fh_early_:
          residuals_matrix = self.residuals_matrix_
      else:
          residuals_matrix = self._compute_sliding_residuals(
              y=self._y,
              X=self._X,
              forecaster=self.forecaster,
              initial_window=self.initial_window,
          )
  
      fh_relative = fh.to_relative(self.cutoff)
      fh_absolute = fh.to_absolute(self.cutoff)
      fh_absolute_ix = fh_absolute.to_pandas()
  
      if cov:
          fh_size = len(fh)
          covariance = np.zeros(shape=(len(fh), fh_size))
          for i in range(fh_size):
              i_residuals = np.diagonal(
                  residuals_matrix, offset=fh_relative[i]
              )
              for j in range(
                  i, fh_size
              ):  # since the matrix is symmetric
                  j_residuals = np.diagonal(
                      residuals_matrix, offset=fh_relative[j]
                  )
                  max_residuals = min(
                      len(j_residuals), len(i_residuals)
                  )
                  covariance[i, j] = covariance[j, i] = np.nanmean(
                      i_residuals[:max_residuals]
                      * j_residuals[:max_residuals]
                  )
          pred_var = pd.DataFrame(
              covariance,
              index=fh_absolute_ix,
              columns=fh_absolute_ix,
          )
      else:
          variance = [
              np.nanmean(
                  np.diagonal(residuals_matrix, offset=offset) ** 2
              )
              for offset in fh_relative
          ]
          if hasattr(self._y, "columns"):
              columns = self._y.columns
              pred_var = pd.DataFrame(
                  variance, columns=columns, index=fh_absolute_ix
              )
          else:
              pred_var = pd.DataFrame(variance, index=fh_absolute_ix)
  
      return pred_var
  
imports: import math
  import numpy
  import pandas
  from scipy.stats import norm
  from sktime.datatypes._convert import convert, convert_to
  from sktime.datatypes._utilities import get_slice
  from sktime.forecasting.base import ForecastingHorizon
  from sktime.forecasting.base._base import DEFAULT_ALPHA, BaseForecaster
  from sktime.forecasting.base._sktime import _BaseWindowForecaster
  from sktime.utils.seasonality import _pivot_sp, _unpivot_sp
  from sktime.utils.validation import check_window_length
  from sktime.utils.validation.forecasting import check_sp
  from sktime.utils.warnings import warn
label_desc: Use image node_id 6 for calling the NaiveVariance obj's underlying member method code with example usage: obj._predict_var(fh, X, cov) and returns: pred_var