Exemple #1
0
def test_MarginalDistribution():
    a1, p1, p2 = symbols('a1 p1 p2', positive=True)
    C = Multinomial('C', 2, p1, p2)
    B = MultivariateBeta('B', a1, C[0])
    MGR = MarginalDistribution(B, (C[0], ))
    mgrc = Mul(
        Symbol('B'),
        Piecewise(
            ExprCondPair(
                Mul(
                    Integer(2),
                    Pow(Symbol('p1', positive=True),
                        Indexed(IndexedBase(Symbol('C')), Integer(0))),
                    Pow(Symbol('p2', positive=True),
                        Indexed(IndexedBase(Symbol('C')), Integer(1))),
                    Pow(
                        factorial(Indexed(IndexedBase(Symbol('C')),
                                          Integer(0))), Integer(-1)),
                    Pow(
                        factorial(Indexed(IndexedBase(Symbol('C')),
                                          Integer(1))), Integer(-1))),
                Eq(
                    Add(Indexed(IndexedBase(Symbol('C')), Integer(0)),
                        Indexed(IndexedBase(Symbol('C')), Integer(1))),
                    Integer(2))), ExprCondPair(Integer(0), True)),
        Pow(gamma(Symbol('a1', positive=True)), Integer(-1)),
        gamma(
            Add(Symbol('a1', positive=True),
                Indexed(IndexedBase(Symbol('C')), Integer(0)))),
        Pow(gamma(Indexed(IndexedBase(Symbol('C')), Integer(0))), Integer(-1)),
        Pow(Indexed(IndexedBase(Symbol('B')), Integer(0)),
            Add(Symbol('a1', positive=True), Integer(-1))),
        Pow(Indexed(IndexedBase(Symbol('B')), Integer(1)),
            Add(Indexed(IndexedBase(Symbol('C')), Integer(0)), Integer(-1))))
    assert MGR(C) == mgrc
Exemple #2
0
def JointRV(symbol, pdf, _set=None):
    """
    Create a Joint Random Variable where each of its component is continuous,
    given the following:

    Parameters
    ==========

    symbol : Symbol
        Represents name of the random variable.
    pdf : A PDF in terms of indexed symbols of the symbol given
        as the first argument

    NOTE
    ====

    As of now, the set for each component for a ``JointRV`` is
    equal to the set of all integers, which cannot be changed.

    Examples
    ========

    >>> from sympy import exp, pi, Indexed, S
    >>> from sympy.stats import density, JointRV
    >>> x1, x2 = (Indexed('x', i) for i in (1, 2))
    >>> pdf = exp(-x1**2/2 + x1 - x2**2/2 - S(1)/2)/(2*pi)
    >>> N1 = JointRV('x', pdf) #Multivariate Normal distribution
    >>> density(N1)(1, 2)
    exp(-2)/(2*pi)

    Returns
    =======

    RandomSymbol

    """
    #TODO: Add support for sets provided by the user
    symbol = sympify(symbol)
    syms = list(i for i in pdf.free_symbols if isinstance(i, Indexed)
        and i.base == IndexedBase(symbol))
    syms = tuple(sorted(syms, key = lambda index: index.args[1]))
    _set = S.Reals**len(syms)
    pdf = Lambda(syms, pdf)
    dist = JointDistributionHandmade(pdf, _set)
    jrv = JointPSpace(symbol, dist).value
    rvs = random_symbols(pdf)
    if len(rvs) != 0:
        dist = MarginalDistribution(dist, (jrv,))
        return JointPSpace(symbol, dist).value
    return jrv