コード例 #1
0
    def get_precise(self, gamble, event=True, algorithm='linear'):
        r"""Calculate the conditional expectation,

        .. math::

           E(f|A)=
           \frac{
           \sum_{\omega\in A}p(\omega)f(\omega)
           }{
           \sum_{\omega\in A}p(\omega)
           }

        where :math:`p(\omega)` is simply the probability of the
        singleton :math:`\omega`::

            self[{omega: 1}, True][0]
        """
        # default algorithm
        if algorithm is None:
            algorithm = 'linear'
        # other algorithms?
        if algorithm != 'linear':
            return LinVac.get_lower(self, gamble, event, algorithm)
        self.is_valid(raise_error=True)
        gamble = self.make_gamble(gamble)
        if event is True or (isinstance(event, Event) and event.is_true()):
            # self[{omega: 1}, True][0] is the lower probability ([0])
            # of the indicator gamble of omega ({omega: 1}),
            # unconditionally (True).
            return sum(self[{
                omega: 1
            }, True][0] * gamble[omega] for omega in self.pspace)
        else:
            event = self.pspace.make_event(event)
            event_prob = self.get_precise(event.indicator(self.number_type))
            if event_prob == 0:
                raise ZeroDivisionError(
                    "cannot condition on event with zero probability")
            return self.get_precise(gamble * event) / event_prob
コード例 #2
0
ファイル: prob.py プロジェクト: gnustats/improb
    def get_precise(self, gamble, event=True, algorithm='linear'):
        r"""Calculate the conditional expectation,

        .. math::

           E(f|A)=
           \frac{
           \sum_{\omega\in A}p(\omega)f(\omega)
           }{
           \sum_{\omega\in A}p(\omega)
           }

        where :math:`p(\omega)` is simply the probability of the
        singleton :math:`\omega`::

            self[{omega: 1}, True][0]
        """
        # default algorithm
        if algorithm is None:
            algorithm = 'linear'
        # other algorithms?
        if algorithm != 'linear':
            return LinVac.get_lower(self, gamble, event, algorithm)
        self.is_valid(raise_error=True)
        gamble = self.make_gamble(gamble)
        if event is True or (isinstance(event, Event) and event.is_true()):
            # self[{omega: 1}, True][0] is the lower probability ([0])
            # of the indicator gamble of omega ({omega: 1}),
            # unconditionally (True).
            return sum(self[{omega: 1}, True][0] * gamble[omega]
                       for omega in self.pspace)
        else:
            event = self.pspace.make_event(event)
            event_prob = self.get_precise(event.indicator(self.number_type))
            if event_prob == 0:
                raise ZeroDivisionError(
                    "cannot condition on event with zero probability")
            return self.get_precise(gamble * event) / event_prob