Beispiel #1
0
 def probability(self, condition, **kwargs):
     cond_inv = False
     if isinstance(condition, Ne):
         condition = Eq(condition.args[0], condition.args[1])
         cond_inv = True
     elif isinstance(condition, And):  # they are independent
         return Mul(*[self.probability(arg) for arg in condition.args])
     elif isinstance(condition, Or):  # they are independent
         return Add(*[self.probability(arg) for arg in condition.args])
     expr = condition.lhs - condition.rhs
     rvs = random_symbols(expr)
     dens = self.compute_density(expr)
     if any([pspace(rv).is_Continuous for rv in rvs]):
         from sympy.stats.crv import SingleContinuousPSpace
         from sympy.stats.crv_types import ContinuousDistributionHandmade
         if expr in self.values:
             # Marginalize all other random symbols out of the density
             randomsymbols = tuple(set(self.values) - frozenset([expr]))
             symbols = tuple(rs.symbol for rs in randomsymbols)
             pdf = self.domain.integrate(self.pdf, symbols, **kwargs)
             return Lambda(expr.symbol, pdf)
         dens = ContinuousDistributionHandmade(dens)
         z = Dummy('z', real=True)
         space = SingleContinuousPSpace(z, dens)
         result = space.probability(condition.__class__(space.value, 0))
     else:
         from sympy.stats.drv import SingleDiscretePSpace
         from sympy.stats.drv_types import DiscreteDistributionHandmade
         dens = DiscreteDistributionHandmade(dens)
         z = Dummy('z', integer=True)
         space = SingleDiscretePSpace(z, dens)
         result = space.probability(condition.__class__(space.value, 0))
     return result if not cond_inv else S.One - result
Beispiel #2
0
def test_ContinuousDistributionHandmade():
    x = Symbol('x')
    z = Dummy('z')
    dens = Lambda(x, Piecewise((S.Half, (0<=x)&(x<1)), (0, (x>=1)&(x<2)),
        (S.Half, (x>=2)&(x<3)), (0, True)))
    dens = ContinuousDistributionHandmade(dens, set=Interval(0, 3))
    space = SingleContinuousPSpace(z, dens)
    assert dens.pdf == Lambda(x, Piecewise((1/2, (x >= 0) & (x < 1)),
        (0, (x >= 1) & (x < 2)), (1/2, (x >= 2) & (x < 3)), (0, True)))
    assert median(space.value) == Interval(1, 2)
    assert E(space.value) == Rational(3, 2)
    assert variance(space.value) == Rational(13, 12)
Beispiel #3
0
 def _transform_pspace(self, sym, dist, pdf):
     """
     This function returns the new pspace of the distribution using handmade
     Distributions and their corresponding pspace.
     """
     pdf = Lambda(sym, pdf(sym))
     _set = dist.set
     if isinstance(dist, ContinuousDistribution):
         return SingleContinuousPSpace(sym, ContinuousDistributionHandmade(pdf, _set))
     elif isinstance(dist, DiscreteDistribution):
         return SingleDiscretePSpace(sym, DiscreteDistributionHandmade(pdf, _set))
     elif isinstance(dist, SingleFiniteDistribution):
         dens = dict((k, pdf(k)) for k in _set)
         return SingleFinitePSpace(sym, FiniteDistributionHandmade(dens))
Beispiel #4
0
    def probability(self, condition, **kwargs):
        z = Dummy('z', real=True)
        cond_inv = False
        if isinstance(condition, Ne):
            condition = Eq(condition.args[0], condition.args[1])
            cond_inv = True
        # Univariate case can be handled by where
        try:
            domain = self.where(condition)
            rv = [rv for rv in self.values if rv.symbol == domain.symbol][0]
            # Integrate out all other random variables
            pdf = self.compute_density(rv, **kwargs)
            # return S.Zero if `domain` is empty set
            if domain.set is S.EmptySet or isinstance(domain.set, FiniteSet):
                return S.Zero if not cond_inv else S.One
            if isinstance(domain.set, Union):
                return sum(
                    Integral(pdf(z), (z, subset), **kwargs)
                    for subset in domain.set.args
                    if isinstance(subset, Interval))
            # Integrate out the last variable over the special domain
            return Integral(pdf(z), (z, domain.set), **kwargs)

        # Other cases can be turned into univariate case
        # by computing a density handled by density computation
        except NotImplementedError:
            from sympy.stats.rv import density
            expr = condition.lhs - condition.rhs
            if not is_random(expr):
                dens = self.density
                comp = condition.rhs
            else:
                dens = density(expr, **kwargs)
                comp = 0
            if not isinstance(dens, ContinuousDistribution):
                from sympy.stats.crv_types import ContinuousDistributionHandmade
                dens = ContinuousDistributionHandmade(dens,
                                                      set=self.domain.set)
            # Turn problem into univariate case
            space = SingleContinuousPSpace(z, dens)
            result = space.probability(condition.__class__(space.value, comp))
            return result if not cond_inv else S.One - result