Ejemplo n.º 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
Ejemplo n.º 2
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))
Ejemplo n.º 3
0
 def probability(self, condition):
     complement = isinstance(condition, Ne)
     if complement:
         condition = Eq(condition.args[0], condition.args[1])
     try:
         _domain = self.where(condition).set
         if condition == False or _domain is S.EmptySet:
             return S.Zero
         if condition == True or _domain == self.domain.set:
             return S.One
         prob = self.eval_prob(_domain)
     except NotImplementedError:
         from sympy.stats.rv import density
         expr = condition.lhs - condition.rhs
         dens = density(expr)
         if not isinstance(dens, DiscreteDistribution):
             from sympy.stats.drv_types import DiscreteDistributionHandmade
             dens = DiscreteDistributionHandmade(dens)
         z = Dummy('z', real=True)
         space = SingleDiscretePSpace(z, dens)
         prob = space.probability(condition.__class__(space.value, 0))
     if prob is None:
         prob = Probability(condition)
     return prob if not complement else S.One - prob