Example #1
0
    def get_risk_data(self, date):

        if date in self._riskdata_cache:
            return self._riskdata_cache[date]

        root = self.riskmodel_root + "/" + date.strftime("%Y%m%d")
        if not os.path.exists(root):
            return None

        factor_exp = load_dataset(root + "/" + self.factor_exp_path,
                                  index_col=[0])
        factor_cov = load_dataset(root + "/" + self.factor_cov_path,
                                  index_col=[0])
        specific_risk = load_dataset(root + "/" + self.specific_risk_path,
                                     index_col=[0])

        if not factor_exp.index.equals(specific_risk.index):
            # NOTE: for stocks missing specific_risk, we always assume it have the highest volatility
            specific_risk = specific_risk.reindex(
                factor_exp.index, fill_value=specific_risk.max())

        universe = factor_exp.index.tolist()

        blacklist = []
        if os.path.exists(root + "/" + self.blacklist_path):
            blacklist = load_dataset(root + "/" +
                                     self.blacklist_path).index.tolist()

        self._riskdata_cache[
            date] = factor_exp.values, factor_cov.values, specific_risk.values, universe, blacklist

        return self._riskdata_cache[date]
Example #2
0
 def _maybe_load_raw_data(self):
     if self._data is not None:
         return
     self._data = pd.concat(
         {fields_group: load_dataset(path_or_obj) for fields_group, path_or_obj in self.config.items()},
         axis=1,
         join=self.join,
     )
     self._data.sort_index(inplace=True)
Example #3
0
 def _maybe_load_raw_data(self):
     if self._data is not None:
         return
     if isinstance(self._config, dict):
         self._data = pd.concat(
             {
                 fields_group: load_dataset(path_or_obj)
                 for fields_group, path_or_obj in self._config.items()
             },
             axis=1,
             join=self.join,
         )
         self._data.sort_index(inplace=True)
     elif isinstance(self._config, (str, Path)):
         with Path(self._config).open("rb") as f:
             self._data = pickle.load(f)
     elif isinstance(self._config, pd.DataFrame):
         self._data = self._config