コード例 #1
0
ファイル: factor.py プロジェクト: rjwinchester/gs-quant
    def covariance(self,
                   factor,
                   start_date: date = DataContext.current.start_date,
                   end_date: date = DataContext.current.end_date) -> Dict:
        """ Retrieve a Dictionary of date->covariance values between this factor and another for a date range """

        covariance_data_raw = GsDataApi.execute_query(
            'RISK_MODEL_COVARIANCE_MATRIX',
            DataQuery(
                where={"riskModel": self.risk_model_id, "factorId": self.id},
                start_date=start_date,
                end_date=end_date
            )
        ).get('data', [])

        date_to_matrix_order = factor.__matrix_order(start_date, end_date)

        covariance_data = {}
        for data in covariance_data_raw:
            date = data['date']
            if date_to_matrix_order.get(date):
                matrix_order_on_date = date_to_matrix_order[date]
                covariance_data[date] = data[matrix_order_on_date]

        return covariance_data
コード例 #2
0
    def returns(
        self,
        start_date: date = DataContext.current.start_date,
        end_date: date = DataContext.current.end_date,
        format: ReturnFormat = ReturnFormat.DATA_FRAME
    ) -> Union[Dict, pd.DataFrame]:
        """ Retrieve a Dataframe or Dictionary of date->factor return values for a date range """
        appendage = self.__get_dataset_trial_appendage()
        data_query_results = GsDataApi.execute_query(
            f'RISK_MODEL_FACTOR{appendage}',
            DataQuery(where={
                "riskModel": self.risk_model_id,
                "factorId": self.id
            },
                      fields=['return'],
                      start_date=start_date,
                      end_date=end_date)).get('data', [])

        return_data = {
            factor_data['date']: factor_data['return']
            for factor_data in data_query_results if factor_data.get('return')
        }

        if format == ReturnFormat.DATA_FRAME:
            return pd.DataFrame.from_dict(return_data,
                                          orient='index',
                                          columns=['return'])
        return return_data
コード例 #3
0
ファイル: factor.py プロジェクト: zhuangchaoxi/gs-quant
    def covariance(
        self,
        factor,
        start_date: date = DataContext.current.start_date,
        end_date: date = DataContext.current.end_date,
        format: ReturnFormat = ReturnFormat.DATA_FRAME
    ) -> Union[Dict, pd.DataFrame]:
        """ Retrieve a Dataframe or Dictionary of date->covariance values between this factor and another for a date
        range """

        covariance_data_raw = GsDataApi.execute_query(
            'RISK_MODEL_COVARIANCE_MATRIX',
            DataQuery(where={
                "riskModel": self.risk_model_id,
                "factorId": self.id
            },
                      start_date=start_date,
                      end_date=end_date)).get('data', [])

        date_to_matrix_order = factor.__matrix_order(start_date, end_date)

        covariance_data = {}
        for data in covariance_data_raw:
            date = data['date']
            if date_to_matrix_order.get(date):
                matrix_order_on_date = date_to_matrix_order[date]
                covariance_data[date] = data[matrix_order_on_date]

        if format == ReturnFormat.DATA_FRAME:
            return pd.DataFrame.from_dict(covariance_data,
                                          orient='index',
                                          columns=['covariance'])
        return covariance_data
コード例 #4
0
ファイル: factor.py プロジェクト: rjwinchester/gs-quant
 def __matrix_order(self, start_date: date, end_date: date) -> Dict:
     """ Retrieve Dictionary of date->matrix_order for the factor in the covariance matrix """
     query_results = GsDataApi.execute_query(
         'RISK_MODEL_COVARIANCE_MATRIX',
         DataQuery(
             where={"riskModel": self.risk_model_id, "factorId": self.id},
             fields=['matrixOrder'],
             start_date=start_date,
             end_date=end_date
         )
     ).get('data', [])
     return {data['date']: str(data['matrixOrder']) for data in query_results}
コード例 #5
0
ファイル: factor.py プロジェクト: pxlogpx/gs-quant
    def get_covariance(self, factor, start_date: date, end_date: date) -> List:
        """ Retrieve a list of covariances between this factor and another for a range of dates """
        covariance_data = []

        # Collect matrix order for requested factor on all available dates
        query = DataQuery(where={
            "riskModel": self.risk_model_id,
            "factorId": factor.factor.identifier
        },
                          start_date=start_date,
                          end_date=end_date)
        factor_covariances = GsDataApi.execute_query(
            'RISK_MODEL_COVARIANCE_MATRIX', query).get('data', [])
        matrix_order_map = {
            data['date']: data['matrixOrder']
            for data in factor_covariances
        }

        # Collect covariances at relevant matrix order on all available dates
        query = DataQuery(where={
            "riskModel": self.risk_model_id,
            "factorId": self.factor.identifier
        },
                          start_date=start_date,
                          end_date=end_date)
        factor_covariances = GsDataApi.execute_query(
            'RISK_MODEL_COVARIANCE_MATRIX', query).get('data', [])
        for data in factor_covariances:
            date = data['date']
            if matrix_order_map.get(date):
                covariance_data.append({
                    'date':
                    date,
                    'covariance':
                    data[str(matrix_order_map[date])]
                })
        return covariance_data