Beispiel #1
0
    def calc(self, priceable: Priceable, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]])\
            -> Union[list, DataFrameWithInfo, ErrorValue, FloatWithInfo, Future, MultipleRiskMeasureFuture,
                     SeriesWithInfo]:
        """
        Calculate the risk measure for the priceable instrument. Do not use directly, use via instruments

        :param priceable: The priceable (e.g. instrument)
        :param risk_measure: The measure we wish to calculate
        :return: A float, Dataframe, Series or Future (depending on is_async or whether the context is entered)

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>> from gs_quant.risk import IRDelta
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD', fixed_rate=0.01)
        >>> delta = swap.calc(IRDelta)
        """
        position = RiskPosition(priceable, priceable.get_quantity())
        multiple_measures = not isinstance(risk_measure, RiskMeasure)
        futures = {}
        active_context_lock = self.__active_context.__lock if self.__active_context != self else nullcontext(
        )

        with self.__lock, active_context_lock:
            for measure in risk_measure if multiple_measures else (
                    risk_measure, ):
                scenario = self.__scenario
                measure_future = self.__active_context.__futures.get(
                    (scenario, measure), {}).get(position)

                if measure_future is None:
                    measure_future = PricingFuture(self.__active_context)
                    if self.__use_cache:
                        cached_result = PricingCache.get(
                            priceable, risk_measure)
                        if cached_result:
                            measure_future.set_result(cached_result)

                    if not measure_future.done():
                        self.__risk_measures_in_scenario_by_provider_and_position.setdefault(
                            priceable.provider(),
                            {}).setdefault(position,
                                           {}).setdefault(scenario,
                                                          set()).add(measure)
                        self.__active_context.__futures.setdefault(
                            (scenario, measure), {})[position] = measure_future

                futures[measure] = measure_future

        future = MultipleRiskMeasureFuture(futures, result_future=PricingFuture(self.__active_context))\
            if multiple_measures else futures[risk_measure]

        if not (self.is_entered or self.__is_async):
            if not future.done():
                self._calc()

            return future.result()
        else:
            return future
Beispiel #2
0
    def calc(self, priceable: Priceable, risk_measure: Union[RiskMeasure, Iterable[RiskMeasure]])\
            -> Union[dict, float, str, pd.DataFrame, pd.Series, Future]:
        """
        Calculate the risk measure for the priceable instrument. Do not use directly, use via instruments

        :param priceable: The priceable (e.g. instrument)
        :param risk_measure: The measure we wish to calculate
        :return: A float, Dataframe, Series or Future (depending on is_async or whether the context is entered)

        **Examples**

        >>> from gs_quant.instrument import IRSwap
        >>> from gs_quant.risk import IRDelta
        >>>
        >>> swap = IRSwap('Pay', '10y', 'USD', fixed_rate=0.01)
        >>> delta = swap.calc(IRDelta)
        """
        from gs_quant.risk.results import MultipleRiskMeasureFuture

        position = RiskPosition(priceable, priceable.get_quantity())
        multiple_measures = not isinstance(risk_measure, RiskMeasure)
        futures = {}

        for measure in risk_measure if multiple_measures else (risk_measure, ):
            measure_future = self.__futures.get(measure, {}).get(position)

            if measure_future is None:
                measure_future = Future()
                if self.__use_cache:
                    cached_result = PricingCache.get(priceable,
                                                     self.market_data_location,
                                                     risk_measure,
                                                     self.pricing_date)
                    if cached_result:
                        measure_future.set_result(cached_result)

                if not measure_future.done():
                    self.__risk_measures_by_provider_and_position.setdefault(
                        priceable.provider(),
                        {}).setdefault(position, set()).add(measure)
                    self.__futures.setdefault(measure,
                                              {})[position] = measure_future

            futures[measure] = measure_future

        future = MultipleRiskMeasureFuture(
            futures) if multiple_measures else futures[risk_measure]

        if not (self._is_entered or self.__is_async):
            if not future.done():
                self._calc()

            return future.result()
        else:
            return future