class_docstr: Risk Model
  
  A risk model is used to estimate the covariance matrix of stock returns.
class_docstr_tok: ["Risk","Model","A","risk","model","is","used","to","estimate","the","covariance","matrix","of","stock","returns","."]
docstr: Args:
      X (pd.Series, pd.DataFrame or np.ndarray): data from which to estimate the covariance,
          with variables as columns and observations as rows.
      return_corr (bool): whether return the correlation matrix.
      is_price (bool): whether `X` contains price (if not assume stock returns).
      return_decomposed_components (bool): whether return decomposed components of the covariance matrix.
  
  Returns:
      pd.DataFrame or np.ndarray: estimated covariance (or correlation).
docstr_tok: ["Args",":","X","(","pd.Series",",","pd.DataFrame","or","np.ndarray",")",":","data","from","which","to","estimate","the","covariance",",","with","variables","as","columns","and","observations","as","rows",".","return_corr","(","bool",")",":","whether","return","the","correlation","matrix",".","is_price","(","bool",")",":","whether","`","X","`","contains","price","(","if","not","assume","stock","returns",")",".","return_decomposed_components","(","bool",")",":","whether","return","decomposed","components","of","the","covariance","matrix",".","Returns",":","pd.DataFrame","or","np.ndarray",":","estimated","covariance","(","or","correlation",")","."]
code: def predict(
      self,
      X: Union[pd.Series, pd.DataFrame, np.ndarray],
      return_corr: bool = False,
      is_price: bool = True,
      return_decomposed_components=False,
  ) -> Union[pd.DataFrame, np.ndarray, tuple]:
      """
      Args:
          X (pd.Series, pd.DataFrame or np.ndarray): data from which to estimate the covariance,
              with variables as columns and observations as rows.
          return_corr (bool): whether return the correlation matrix.
          is_price (bool): whether `X` contains price (if not assume stock returns).
          return_decomposed_components (bool): whether return decomposed components of the covariance matrix.
  
      Returns:
          pd.DataFrame or np.ndarray: estimated covariance (or correlation).
      """
      assert (
          not return_corr or not return_decomposed_components
      ), "Can only return either correlation matrix or decomposed components."
  
      # transform input into 2D array
      if not isinstance(X, (pd.Series, pd.DataFrame)):
          columns = None
      else:
          if isinstance(X.index, pd.MultiIndex):
              if isinstance(X, pd.DataFrame):
                  X = X.iloc[:, 0].unstack(
                      level="instrument"
                  )  # always use the first column
              else:
                  X = X.unstack(level="instrument")
          else:
              # X is 2D DataFrame
              pass
          columns = X.columns  # will be used to restore dataframe
          X = X.values
  
      # calculate pct_change
      if is_price:
          X = X[1:] / X[:-1] - 1  # NOTE: resulting `n - 1` rows
  
      # scale return
      if self.scale_return:
          X *= 100
  
      # handle nan and centered
      X = self._preprocess(X)
  
      # return decomposed components if needed
      if return_decomposed_components:
          assert (
              "return_decomposed_components"
              in inspect.getfullargspec(self._predict).args
          ), "This risk model does not support return decomposed components of the covariance matrix "
  
          F, cov_b, var_u = self._predict(
              X, return_decomposed_components=True
          )  # pylint: disable=E1123
          return F, cov_b, var_u
  
      # estimate covariance
      S = self._predict(X)
  
      # return correlation if needed
      if return_corr:
          vola = np.sqrt(np.diag(S))
          corr = S / np.outer(vola, vola)
          if columns is None:
              return corr
          return pd.DataFrame(corr, index=columns, columns=columns)
  
      # return covariance
      if columns is None:
          return S
      return pd.DataFrame(S, index=columns, columns=columns)
  
file_path: qlib/qlib/model/riskmodel/base.py
imports: import inspect
  import numpy
  import pandas
  from typing import Union
  from qlib.model.base import BaseModel
label_desc: Use image node_id 2 for calling the RiskModel obj's underlying member method code with example usage: obj.predict(X, return_corr, is_price, return_decomposed_components) and returns: pd, F,  cov_b,  var_u, pd, S, corr