Example #1
0
class USR:
    '''
    Uniform Set of References, aka RT_LMAD (Real Time Linear Memory Access
    Descriptor) both to be found in Rus et al. 2003 and Rus et al. 2008. Simple
    implementation for the needs of TVB. No strides, branching, conditionals
    etc., only union, intersection, complement and subtration; uses sympy
    finite sets as underlying implementation of dimensions of known sizes as a
    quick fix and ignores symbolic dimensions altogether. MD-LMAD
    (Multi-Dimensional Linear Memory Access Descriptor (Paek et al. 2002))
    leaves represented by ProductSet of Range instances (seems to be exact). To
    be rewritten and/or replaced with full RT_LMAD implementation if needed in
    future ;)
    '''

    def __init__(self, subscripts=None):
        '''
        Creates an empty USR or an USR containing single MD-LMAD leaf
        

        subscripts: 
            list of subscripts defining the access to particular dimension, for
            every dimension we use a triplet notation: lower bound, upper
            bound, stride. Each subscript is therefore a tuple of following
            possible formats: n - linear range from 0 to n-1; (low, up) -
            linear range from low to up-1; (low, up,step) same, but with a
            step.
        '''
        if subscripts is None:
            self._symbolic_set = EmptySet()
        else:
            ranges = tuple(map(lambda x: Range(x) if isinstance(x,int) else Range(*x), subscripts))
            self._symbolic_set = ProductSet(*ranges)
    def __repr__(self):
        return "<USR: "******">"
    
    def intersect(self, other):
        usr = USR()
        usr._symbolic_set = self._symbolic_set.intersect(other._symbolic_set)
        return usr

    def union(self, other):
        usr = USR()
        usr._symbolic_set = self._symbolic_set.union(other._symbolic_set)
        return usr

    def is_empty(self):
        return self._symbolic_set is EmptySet()

    def complement(self, other):
        usr = USR()
        usr._symbolic_set = Complement(FiniteSet(*self._symbolic_set), FiniteSet(*other._symbolic_set))
        return usr

    def __eq__(self, other):
        return FiniteSet(*self._symbolic_set) == FiniteSet(*other._symbolic_set)

    def __str__(self):
        return str(self._symbolic_set)
def union_sets(a, b):
    if b.is_subset(a):
        return a
    if len(b.args) != len(a.args):
        return None
    if a.args[0] == b.args[0]:
        return a.args[0] * Union(ProductSet(a.args[1:]),
                                    ProductSet(b.args[1:]))
    if a.args[-1] == b.args[-1]:
        return Union(ProductSet(a.args[:-1]),
                     ProductSet(b.args[:-1])) * a.args[-1]
    return None
Example #3
0
def test_hyper():
    for x in sorted(exparg):
        test("erf", x, N(sp.erf(x)))
    for x in sorted(exparg):
        test("erfc", x, N(sp.erfc(x)))

    gamarg = FiniteSet(*(x + S(1) / 12 for x in exparg))
    betarg = ProductSet(gamarg, gamarg)
    for x in sorted(gamarg):
        test("lgamma", x, N(sp.log(abs(sp.gamma(x)))))
    for x in sorted(gamarg):
        test("gamma", x, N(sp.gamma(x)))
    for x, y in sorted(betarg, key=lambda (x, y): (y, x)):
        test("beta", x, y, N(sp.beta(x, y)))

    pgamarg = FiniteSet(S(1) / 12, S(1) / 3, S(3) / 2, 5)
    pgamargp = ProductSet(gamarg & Interval(0, oo, True), pgamarg)
    for a, x in sorted(pgamargp):
        test("pgamma", a, x, N(sp.lowergamma(a, x)))
    for a, x in sorted(pgamargp):
        test("pgammac", a, x, N(sp.uppergamma(a, x)))
    for a, x in sorted(pgamargp):
        test("pgammar", a, x, N(sp.lowergamma(a, x) / sp.gamma(a)))
    for a, x in sorted(pgamargp):
        test("pgammarc", a, x, N(sp.uppergamma(a, x) / sp.gamma(a)))
    for a, x in sorted(pgamargp):
        test("ipgammarc", a, N(sp.uppergamma(a, x) / sp.gamma(a)), x)

    pbetargp = [(a, b, x) for a, b, x in ProductSet(betarg, pgamarg)
                if a > 0 and b > 0 and x < 1]
    pbetargp.sort(key=lambda (a, b, x): (b, a, x))
    for a, b, x in pbetargp:
        test("pbeta", a, b, x, mp.betainc(mpf(a), mpf(b), x2=mpf(x)))
    for a, b, x in pbetargp:
        test("pbetar", a, b, x,
             mp.betainc(mpf(a), mpf(b), x2=mpf(x), regularized=True))
    for a, b, x in pbetargp:
        test("ipbetar", a, b,
             mp.betainc(mpf(a), mpf(b), x2=mpf(x), regularized=True), x)

    for x in sorted(posarg):
        test("j0", x, N(sp.besselj(0, x)))
    for x in sorted(posarg):
        test("j1", x, N(sp.besselj(1, x)))
    for x in sorted(posarg - FiniteSet(0)):
        test("y0", x, N(sp.bessely(0, x)))
    for x in sorted(posarg - FiniteSet(0)):
        test("y1", x, N(sp.bessely(1, x)))
Example #4
0
def test_MultivariateEwens():
    n, theta, i = symbols('n theta i', positive=True)

    # tests for integer dimensions
    theta_f = symbols('t_f', negative=True)
    a = symbols('a_1:4', positive=True, integer=True)
    ed = MultivariateEwens('E', 3, theta)
    assert density(ed)(a[0], a[1], a[2]) == Piecewise(
        (6 * 2**(-a[1]) * 3**(-a[2]) * theta**a[0] * theta**a[1] *
         theta**a[2] /
         (theta * (theta + 1) *
          (theta + 2) * factorial(a[0]) * factorial(a[1]) * factorial(a[2])),
         Eq(a[0] + 2 * a[1] + 3 * a[2], 3)), (0, True))
    assert marginal_distribution(ed, ed[1])(a[1]) == Piecewise(
        (6 * 2**(-a[1]) * theta**a[1] /
         ((theta + 1) * (theta + 2) * factorial(a[1])), Eq(2 * a[1] + 1, 3)),
        (0, True))
    raises(ValueError, lambda: MultivariateEwens('e1', 5, theta_f))
    assert ed.pspace.distribution.set == ProductSet(Range(0, 4, 1),
                                                    Range(0, 2, 1),
                                                    Range(0, 2, 1))

    # tests for symbolic dimensions
    eds = MultivariateEwens('E', n, theta)
    a = IndexedBase('a')
    j, k = symbols('j, k')
    den = Piecewise((factorial(n) *
                     Product(theta**a[j] * (j + 1)**(-a[j]) / factorial(a[j]),
                             (j, 0, n - 1)) / RisingFactorial(theta, n),
                     Eq(n, Sum((k + 1) * a[k], (k, 0, n - 1)))), (0, True))
    assert density(eds)(a).dummy_eq(den)
Example #5
0
def test_Normal():
    m = Normal('A', [1, 2], [[1, 0], [0, 1]])
    A = MultivariateNormal('A', [1, 2], [[1, 0], [0, 1]])
    assert m == A
    assert density(m)(1, 2) == 1 / (2 * pi)
    assert m.pspace.distribution.set == ProductSet(S.Reals, S.Reals)
    raises(ValueError, lambda: m[2])
    raises (ValueError,\
        lambda: Normal('M',[1, 2], [[0, 0], [0, 1]]))
    n = Normal('B', [1, 2, 3], [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    p = Normal('C', Matrix([1, 2]), Matrix([[1, 0], [0, 1]]))
    assert density(m)(x, y) == density(p)(x, y)
    assert marginal_distribution(n, 0, 1)(1, 2) == 1 / (2 * pi)
    raises(ValueError, lambda: marginal_distribution(m))
    assert integrate(density(m)(x, y), (x, -oo, oo), (y, -oo, oo)).evalf() == 1
    N = Normal('N', [1, 2], [[x, 0], [0, y]])
    assert density(N)(0,
                      0) == exp(-2 / y - 1 / (2 * x)) / (2 * pi * sqrt(x * y))

    raises(ValueError, lambda: Normal('M', [1, 2], [[1, 1], [1, -1]]))
    # symbolic
    n = symbols('n', natural=True)
    mu = MatrixSymbol('mu', n, 1)
    sigma = MatrixSymbol('sigma', n, n)
    X = Normal('X', mu, sigma)
    assert density(X) == MultivariateNormalDistribution(mu, sigma)
    raises(NotImplementedError, lambda: median(m))
Example #6
0
def test_MatrixGamma():
    M = MatrixGamma('M', 1, 2, [[1, 0], [0, 1]])
    assert M.pspace.distribution.set == ProductSet(S.Reals, S.Reals)
    assert isinstance(density(M), MatrixGammaDistribution)
    X = MatrixSymbol('X', 2, 2)
    num = exp(Trace(Matrix([[-S(1) / 2, 0], [0, -S(1) / 2]]) * X))
    assert density(M)(X).doit() == num / (4 * pi * sqrt(Determinant(X)))
    assert density(M)([[2, 1], [1, 2]]).doit() == sqrt(3) * exp(-2) / (12 * pi)
    X = MatrixSymbol('X', 1, 2)
    Y = MatrixSymbol('Y', 1, 2)
    assert density(M)([X, Y]).doit() == exp(-X[0, 0] / 2 - Y[0, 1] / 2) / (
        4 * pi * sqrt(X[0, 0] * Y[0, 1] - X[0, 1] * Y[0, 0]))
    # symbolic
    a, b = symbols('a b', positive=True)
    d = symbols('d', positive=True, integer=True)
    Y = MatrixSymbol('Y', d, d)
    Z = MatrixSymbol('Z', 2, 2)
    SM = MatrixSymbol('SM', d, d)
    M2 = MatrixGamma('M2', a, b, SM)
    M3 = MatrixGamma('M3', 2, 3, [[2, 1], [1, 2]])
    k = Dummy('k')
    exprd = pi**(-d * (d - 1) / 4) * b**(-a * d) * exp(
        Trace((-1 / b) * SM**(-1) * Y)) * Determinant(SM)**(-a) * Determinant(
            Y)**(a - d / 2 - S(1) / 2) / Product(gamma(-k / 2 + a + S(1) / 2),
                                                 (k, 1, d))
    assert density(M2)(Y).dummy_eq(exprd)
    raises(NotImplementedError, lambda: density(M3 + M)(Z))
    raises(ValueError, lambda: density(M)(1))
    raises(ValueError, lambda: MatrixGamma('M', -1, 2, [[1, 0], [0, 1]]))
    raises(ValueError, lambda: MatrixGamma('M', -1, -2, [[1, 0], [0, 1]]))
    raises(ValueError, lambda: MatrixGamma('M', -1, 2, [[1, 0], [2, 1]]))
    raises(ValueError, lambda: MatrixGamma('M', -1, 2, [[1, 0], [0]]))
Example #7
0
 def set(self):
     rvs = [i for i in random_symbols(self.args[1])]
     marginalise_out = [i for i in random_symbols(self.args[1]) \
      if i not in self.args[1]]
     for i in rvs:
         if i in marginalise_out:
             rvs.remove(i)
     return ProductSet((i.pspace.set for i in rvs))
Example #8
0
    def __init__(self, subscripts=None):
        '''
        Creates an empty USR or an USR containing single MD-LMAD leaf
        

        subscripts: 
            list of subscripts defining the access to particular dimension, for
            every dimension we use a triplet notation: lower bound, upper
            bound, stride. Each subscript is therefore a tuple of following
            possible formats: n - linear range from 0 to n-1; (low, up) -
            linear range from low to up-1; (low, up,step) same, but with a
            step.
        '''
        if subscripts is None:
            self._symbolic_set = EmptySet()
        else:
            ranges = tuple(map(lambda x: Range(x) if isinstance(x,int) else Range(*x), subscripts))
            self._symbolic_set = ProductSet(*ranges)
Example #9
0
def test_NormalGamma():
    ng = NormalGamma('G', 1, 2, 3, 4)
    assert density(ng)(1, 1) == 32 * exp(-4) / sqrt(pi)
    assert ng.pspace.distribution.set == ProductSet(S.Reals, Interval(0, oo))
    raises(ValueError, lambda: NormalGamma('G', 1, 2, 3, -1))
    assert marginal_distribution(ng, 0)(1) == \
        3*sqrt(10)*gamma(Rational(7, 4))/(10*sqrt(pi)*gamma(Rational(5, 4)))
    assert marginal_distribution(ng, y)(1) == exp(Rational(-1, 4)) / 128
    assert marginal_distribution(ng, [0, 1])(x) == x**2 * exp(-x / 4) / 128
Example #10
0
def test_MultivariateTDist():
    t1 = MultivariateT('T', [0, 0], [[1, 0], [0, 1]], 2)
    assert(density(t1))(1, 1) == 1/(8*pi)
    assert t1.pspace.distribution.set == ProductSet(S.Reals, S.Reals)
    assert integrate(density(t1)(x, y), (x, -oo, oo), \
        (y, -oo, oo)).evalf() == 1
    raises(ValueError, lambda: MultivariateT('T', [1, 2], [[1, 1], [1, -1]], 1))
    t2 = MultivariateT('t2', [1, 2], [[x, 0], [0, y]], 1)
    assert density(t2)(1, 2) == 1/(2*pi*sqrt(x*y))
Example #11
0
def test_multivariate_laplace():
    raises(ValueError, lambda: Laplace('T', [1, 2], [[1, 2], [2, 1]]))
    L = Laplace('L', [1, 0], [[1, 0], [0, 1]])
    L2 = MultivariateLaplace('L2', [1, 0], [[1, 0], [0, 1]])
    assert density(L)(2, 3) == exp(2) * besselk(0, sqrt(39)) / pi
    L1 = Laplace('L1', [1, 2], [[x, 0], [0, y]])
    assert density(L1)(0, 1) == \
        exp(2/y)*besselk(0, sqrt((2 + 4/y + 1/x)/y))/(pi*sqrt(x*y))
    assert L.pspace.distribution.set == ProductSet(S.Reals, S.Reals)
    assert L.pspace.distribution == L2.pspace.distribution
Example #12
0
def test_MultivariateBeta():
    a1, a2 = symbols('a1, a2', positive=True)
    a1_f, a2_f = symbols('a1, a2', positive=False, real=True)
    mb = MultivariateBeta('B', [a1, a2])
    mb_c = MultivariateBeta('C', a1, a2)
    assert density(mb)(1, 2) == S(2)**(a2 - 1)*gamma(a1 + a2)/\
                                (gamma(a1)*gamma(a2))
    assert marginal_distribution(mb_c, 0)(3) == S(3)**(a1 - 1)*gamma(a1 + a2)/\
                                                (a2*gamma(a1)*gamma(a2))
    raises(ValueError, lambda: MultivariateBeta('b1', [a1_f, a2]))
    raises(ValueError, lambda: MultivariateBeta('b2', [a1, a2_f]))
    raises(ValueError, lambda: MultivariateBeta('b3', [0, 0]))
    raises(ValueError, lambda: MultivariateBeta('b4', [a1_f, a2_f]))
    assert mb.pspace.distribution.set == ProductSet(Interval(0, 1), Interval(0, 1))
Example #13
0
    def __init__(self, _Symbols, _Domains):
        if type(_Symbols) is not list:
            raise TypeError("Symbols must be a list of sympy symbols")
        if type(_Domains) is not list:
            raise TypeError("Domains must be a list of sympy domains")
        if len(_Symbols) != len(_Domains):
            raise TypeError("Symbols and Domains must have same length")

        #specify the type of the symbols
        self.is_continuous = False
        self.is_mixed = False
        self.is_discrete = False

        #specify different stages of model building (compiling)
        self.is_built = False
        self.is_AVS = None
        self.is_Coherent = None

        self.Symbols = _Symbols
        self._Domains = _Domains
        self._TypeSymbols = []  #list that includes symbol type (c,d)
        for el in _Domains:
            if el.is_FiniteSet == True:
                self._TypeSymbols.append('d')  #discrete
            if el.is_Interval == True:
                self._TypeSymbols.append('c')  #continuous

        ind = np.argsort(self._TypeSymbols)
        self._TypeSymbols = list(np.array(self._TypeSymbols)[ind])
        self.Symbols = list(np.array(self.Symbols)[ind])
        self._Domains = list(np.array(self._Domains)[ind])
        self.Domain = ProductSet(self._Domains)  #cartesian product
        if ('c' in self._TypeSymbols and 'd' not in self._TypeSymbols):
            self.is_continuous = True
        elif ('c' not in self._TypeSymbols and 'd' in self._TypeSymbols):
            self.is_discrete = True
        else:
            self.is_mixed = True
            ind, = np.where(np.array(self._TypeSymbols) == 'c')
            self.last_continuous = ind[-1]  #index of last continuous symbol

        self.Gambles = []  #list of gambles
        self.SupportingPoints = []  #Supporting points for smart discretisation
        self.DiscretePowerSet = []  #power set of domain of discrete variables
        self.lambda_bounds = [
        ]  #bounds for lambda_i in LP, this is used to make bounds in the LP problem
        self.type_relation = [
        ]  #type of relation: '>=' inequality or '==' equality
        self.Lambda = []  #vector of the lambdas (solutions of the LP problem)
Example #14
0
def test_NegativeMultinomial():
    k0, x1, x2, x3, x4 = symbols('k0, x1, x2, x3, x4', nonnegative=True, integer=True)
    p1, p2, p3, p4 = symbols('p1, p2, p3, p4', positive=True)
    p1_f = symbols('p1_f', negative=True)
    N = NegativeMultinomial('N', 4, [p1, p2, p3, p4])
    C = NegativeMultinomial('C', 4, 0.1, 0.2, 0.3)
    g = gamma
    f = factorial
    assert simplify(density(N)(x1, x2, x3, x4) -
            p1**x1*p2**x2*p3**x3*p4**x4*(-p1 - p2 - p3 - p4 + 1)**4*g(x1 + x2 +
            x3 + x4 + 4)/(6*f(x1)*f(x2)*f(x3)*f(x4))) is S.Zero
    assert comp(marginal_distribution(C, C[0])(1).evalf(), 0.33, .01)
    raises(ValueError, lambda: NegativeMultinomial('b1', 5, [p1, p2, p3, p1_f]))
    raises(ValueError, lambda: NegativeMultinomial('b2', k0, 0.5, 0.4, 0.3, 0.4))
    assert N.pspace.distribution.set == ProductSet(Range(0, oo, 1),
                    Range(0, oo, 1), Range(0, oo, 1), Range(0, oo, 1))
Example #15
0
 def make_SupportingPoints(self, n, criterion='centermaximin'):
     '''
     makes discretisation points for continuous and discrete vars using design of experiment strategy + exahustive on discrete vars
     '''
     if self.is_discrete == True:
         T = self.__make_DiscretePoints(self.Domain)
     elif self.is_continuous == True:
         T = self.__make_ContinuousPoints(len(self.Symbols), n, criterion)
     elif self.is_mixed == True:
         Tc = self.__make_ContinuousPoints(self.last_continuous + 1, n,
                                           criterion)
         discretedomain = ProductSet(self._Domains[self.last_continuous +
                                                   1:])
         Td = self.__make_DiscretePoints(discretedomain)
         self.DiscretePowerSet = Td
         T = []
         for i in range(0, len(Tc)):
             for j in range(0, len(Td)):
                 T.append(Tc[i] + Td[j])
     #T = np.unique(T,axis=0) #initial discretisation
     return np.array(T)
Example #16
0
def test_ProductSet_of_single_arg_is_arg():
    assert ProductSet(Interval(0, 1)) == Interval(0, 1)
Example #17
0
 def set(self):
     rvs = [i for i in self.args[1] if isinstance(i, RandomSymbol)]
     return ProductSet(*[rv.pspace.set for rv in rvs])
Example #18
0
def intersection_sets(a, b):
    if len(b.args) != len(a.args):
        return S.EmptySet
    return ProductSet(*(i.intersect(j) for i, j in zip(a.sets, b.sets)))
Run this cell before any other
Declare any other sets here if you'd like
"""
from sympy import FiniteSet, ProductSet
from sympy.sets.powerset import PowerSet

"""Finite Sets: represents a finite set of discrete numbers"""

A = FiniteSet(1, 2, 3, 4, 5)
B = FiniteSet(5, 6, 7, 8, 9)
C = FiniteSet(9, 10, 11, 12)

#%%
"""Powersets: all possible sets in a set"""

E = A.powerset() # Returns a new finite set with a finite set
F = PowerSet(A).rewrite(FiniteSet) # Returns a setwith many finite sets

print(F)

#%%
"""Cartesian product: """

cp_m1 = ProductSet(A, B) # Uses sympy function 
cp_m2 = A*B # Sympy syntax for finite sets

print()



Example #20
0
def test_Normal():
    m = Normal('A', [1, 2], [[1, 0], [0, 1]])
    A = MultivariateNormal('A', [1, 2], [[1, 0], [0, 1]])
    assert m == A
    assert density(m)(1, 2) == 1 / (2 * pi)
    assert m.pspace.distribution.set == ProductSet(S.Reals, S.Reals)
    raises(ValueError, lambda: m[2])
    n = Normal('B', [1, 2, 3], [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
    p = Normal('C', Matrix([1, 2]), Matrix([[1, 0], [0, 1]]))
    assert density(m)(x, y) == density(p)(x, y)
    assert marginal_distribution(n, 0, 1)(1, 2) == 1 / (2 * pi)
    raises(ValueError, lambda: marginal_distribution(m))
    assert integrate(density(m)(x, y), (x, -oo, oo), (y, -oo, oo)).evalf() == 1
    N = Normal('N', [1, 2], [[x, 0], [0, y]])
    assert density(N)(0, 0) == exp(-((4 * x + y) /
                                     (2 * x * y))) / (2 * pi * sqrt(x * y))

    raises(ValueError, lambda: Normal('M', [1, 2], [[1, 1], [1, -1]]))
    # symbolic
    n = symbols('n', natural=True)
    mu = MatrixSymbol('mu', n, 1)
    sigma = MatrixSymbol('sigma', n, n)
    X = Normal('X', mu, sigma)
    assert density(X) == MultivariateNormalDistribution(mu, sigma)
    raises(NotImplementedError, lambda: median(m))
    # Below tests should work after issue #17267 is resolved
    # assert E(X) == mu
    # assert variance(X) == sigma

    # test symbolic multivariate normal densities
    n = 3

    Sg = MatrixSymbol('Sg', n, n)
    mu = MatrixSymbol('mu', n, 1)
    obs = MatrixSymbol('obs', n, 1)

    X = MultivariateNormal('X', mu, Sg)
    density_X = density(X)

    eval_a = density_X(obs).subs({
        Sg: eye(3),
        mu: Matrix([0, 0, 0]),
        obs: Matrix([0, 0, 0])
    }).doit()
    eval_b = density_X(0, 0, 0).subs({
        Sg: eye(3),
        mu: Matrix([0, 0, 0])
    }).doit()

    assert eval_a == sqrt(2) / (4 * pi**Rational(3 / 2))
    assert eval_b == sqrt(2) / (4 * pi**Rational(3 / 2))

    n = symbols('n', natural=True)

    Sg = MatrixSymbol('Sg', n, n)
    mu = MatrixSymbol('mu', n, 1)
    obs = MatrixSymbol('obs', n, 1)

    X = MultivariateNormal('X', mu, Sg)
    density_X_at_obs = density(X)(obs)

    expected_density = MatrixElement(
        exp((S(1)/2) * (mu.T - obs.T) * Sg**(-1) * (-mu + obs)) / \
        sqrt((2*pi)**n * Determinant(Sg)), 0, 0)

    assert density_X_at_obs == expected_density
Example #21
0
def test_prob():
    def emit(name, iname, cdf, args, no_small=False):
        V = []
        for arg in sorted(args):
            y = cdf(*arg)
            if isinstance(y, mpf):
                e = sp.nsimplify(y, rational=True)
                if e.is_Rational and e.q <= 1000 and \
                        mp.almosteq(mp.mpf(e), y, 1e-25):
                    y = e
            else:
                y = N(y)
            V.append(arg + (y, ))
        for v in V:
            if name:
                test(name, *v)
        for v in V:
            if iname and (not no_small or 1 / 1000 <= v[-1] <= 999 / 1000):
                test(iname, *(v[:-2] + v[:-3:-1]))

    x = sp.Symbol("x")
    emit("ncdf", "nicdf", sp.Lambda(x,
                                    st.cdf(st.Normal("X", 0, 1))(x)),
         zip(exparg))
    # using cdf() for anything more complex is too slow

    df = FiniteSet(1, S(3) / 2, 2, S(5) / 2, 5, 25)
    emit("c2cdf",
         "c2icdf",
         lambda k, x: sp.lowergamma(k / 2, x / 2) / sp.gamma(k / 2),
         ProductSet(df, posarg),
         no_small=True)

    dfint = df & sp.fancysets.Naturals()

    def cdf(k, x):
        k, x = map(mpf, (k, x))
        return .5 + .5 * mp.sign(x) * mp.betainc(
            k / 2, .5, x1=1 / (1 + x**2 / k), regularized=True)

    emit("stcdf", "sticdf", cdf, ProductSet(dfint, exparg))

    def cdf(d1, d2, x):
        d1, d2, x = map(mpf, (d1, d2, x))
        return mp.betainc(d1 / 2,
                          d2 / 2,
                          x2=x / (x + d2 / d1),
                          regularized=True)

    emit("fcdf", "ficdf", cdf, ProductSet(dfint, dfint, posarg))

    kth = ProductSet(sp.ImageSet(lambda x: x / 5, df), posarg - FiniteSet(0))
    emit("gcdf",
         "gicdf",
         lambda k, th, x: sp.lowergamma(k, x / th) / sp.gamma(k),
         ProductSet(kth, posarg),
         no_small=True)

    karg = FiniteSet(0, 1, 2, 5, 10, 15, 40)
    knparg = [(k, n, p)
              for k, n, p in ProductSet(karg, karg, posarg
                                        & Interval(0, 1, True, True))
              if k <= n and n > 0]

    def cdf(k, n, p):
        return st.P(st.Binomial("X", n, p) <= k)

    emit("bncdf", "bnicdf", cdf, knparg, no_small=True)

    def cdf(k, lamda):
        return sp.uppergamma(k + 1, lamda) / sp.gamma(k + 1)

    emit("pscdf",
         "psicdf",
         cdf,
         ProductSet(karg, posarg + karg - FiniteSet(0)),
         no_small=True)

    x, i = sp.symbols("x i")

    def smcdf(n, e):
        return 1 - sp.Sum(
            sp.binomial(n, i) * e * (e + i / n)**(i - 1) *
            (1 - e - i / n)**(n - i), (i, 0, sp.floor(n * (1 - e)))).doit()

    kcdf = sp.Lambda(
        x,
        sp.sqrt(2 * pi) / x *
        sp.Sum(sp.exp(-pi**2 / 8 * (2 * i - 1)**2 / x**2), (i, 1, oo)))
    smarg = ProductSet(karg - FiniteSet(0),
                       posarg & Interval(0, 1, True, True))
    karg = FiniteSet(S(1) / 100,
                     S(1) / 10) + (posarg & Interval(S(1) / 4, oo, True))

    for n, e in sorted(smarg):
        test("smcdf", n, e, N(smcdf(n, e)))
    prec("1e-10")
    for x in sorted(karg):
        test("kcdf", x, N(kcdf(x)))
    prec("1e-9")
    for n, e in sorted(smarg):
        p = smcdf(n, e)
        if p < S(9) / 10:
            test("smicdf", n, N(p), e)
    prec("1e-6")
    for x in sorted(karg):
        p = kcdf(x)
        if N(p) > S(10)**-8:
            test("kicdf", N(p), x)
Example #22
0
def test_ProductSet_is_empty():
    assert ProductSet(S.Integers, S.Reals).is_empty == False
    assert ProductSet(Interval(x, 1), S.Reals).is_empty == None
Example #23
0
from sympy import FiniteSet, Intersection, Interval, ProductSet, Union
init_printing(use_unicode=False, wrap_line=False, no_global=True)

Union(Interval(1, 10), Interval(10, 30))
Union(Interval(5, 15), Interval(15, 25))
Union(FiniteSet(1, 2, 3, 4), FiniteSet(10, 15, 30, 7))

Intersection(Interval(1, 3), Interval(2, 4))
Interval(1, 3).intersect(Interval(2, 4))
Intersection(FiniteSet(1, 2, 3, 4), FiniteSet(1, 3, 4, 7))
FiniteSet(1, 2, 3, 4).intersect(FiniteSet(1, 3, 4, 7))

I = Interval(0, 5)
S = FiniteSet(1, 2, 3)
ProductSet(I, S)
(2, 2) in ProductSet(I, S)

Interval(0, 1) * Interval(0, 1)
coin = FiniteSet('H', 'T')
set(coin**2)

Complement(FiniteSet(0, 1, 2, 3, 4, 5), FiniteSet(1, 2))