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
     expr = condition.lhs - condition.rhs
     rvs = random_symbols(expr)
     z = Dummy('z', real=True, Finite=True)
     dens = self.compute_density(expr)
     if any([pspace(rv).is_Continuous for rv in rvs]):
         from sympy.stats.crv import (ContinuousDistributionHandmade,
             SingleContinuousPSpace)
         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)
         space = SingleContinuousPSpace(z, dens)
         result = space.probability(condition.__class__(space.value, 0))
     else:
         from sympy.stats.drv import (DiscreteDistributionHandmade,
             SingleDiscretePSpace)
         dens = DiscreteDistributionHandmade(dens)
         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 __new__(cls, sym, dist):
     sym = sympify(sym)
     if isinstance(dist, SingleContinuousDistribution):
         return SingleContinuousPSpace(sym, dist)
     if isinstance(dist, SingleDiscreteDistribution):
         return SingleDiscretePSpace(sym, dist)
     return Basic.__new__(cls, sym, dist)
 def __new__(cls, sym, dist):
     if isinstance(dist, SingleContinuousDistribution):
         return SingleContinuousPSpace(sym, dist)
     if isinstance(dist, SingleDiscreteDistribution):
         return SingleDiscretePSpace(sym, dist)
     sym = _symbol_converter(sym)
     return Basic.__new__(cls, sym, dist)
Beispiel #4
0
 def pdf(self, *x):
     dist = self.args[0]
     z = Dummy('z')
     if isinstance(dist, ContinuousDistribution):
         rv = SingleContinuousPSpace(z, dist).value
     elif isinstance(dist, DiscreteDistribution):
         rv = SingleDiscretePSpace(z, dist).value
     return MarginalDistribution(self, (rv,)).pdf(*x)
Beispiel #5
0
 def __new__(cls, sym, dist):
     if isinstance(dist, SingleContinuousDistribution):
         return SingleContinuousPSpace(sym, dist)
     if isinstance(dist, SingleDiscreteDistribution):
         return SingleDiscretePSpace(sym, dist)
     if isinstance(sym, str):
         sym = Symbol(sym)
     if not isinstance(sym, Symbol):
         raise TypeError("s should have been string or Symbol")
     return Basic.__new__(cls, sym, dist)
Beispiel #6
0
 def __new__(cls, s, distribution):
     s = _symbol_converter(s)
     if isinstance(distribution, ContinuousDistribution):
         return SingleContinuousPSpace(s, distribution)
     if isinstance(distribution, DiscreteDistribution):
         return SingleDiscretePSpace(s, distribution)
     if isinstance(distribution, SingleFiniteDistribution):
         return SingleFinitePSpace(s, distribution)
     if not isinstance(distribution, CompoundDistribution):
         raise ValueError("%s should be an isinstance of "
                     "CompoundDistribution"%(distribution))
     return Basic.__new__(cls, s, distribution)
Beispiel #7
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 #8
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))