Beispiel #1
0
def test_unevaluated_CompoundDist():
    # these tests need to be removed once they work with evaluation as they are currently not
    # evaluated completely in sympy.
    R = Rayleigh('R', 4)
    X = Normal('X', 3, R)
    _k = Dummy('k')
    exprd = Piecewise((exp(S(3)/4 - x/4)/8, 2*Abs(arg(x - 3)) <= pi/2),
    (sqrt(2)*Integral(exp(-(_k**4 + 16*(x - 3)**2)/(32*_k**2)),
    (_k, 0, oo))/(32*sqrt(pi)), True))
    assert (density(X)(x).simplify()).dummy_eq(exprd.simplify())

    expre = Integral(_k*Integral(sqrt(2)*exp(-_k**2/32)*exp(-(_k - 3)**2/(2*_k**2)
    )/(32*sqrt(pi)), (_k, 0, oo)), (_k, -oo, oo))
    with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed
        assert E(X, evaluate=False).rewrite(Integral).dummy_eq(expre)

    X = Poisson('X', 1)
    Y = Poisson('Y', X)
    Z = Poisson('Z', Y)
    exprd = exp(-1)*Sum(exp(-Y)*Y**x*Sum(exp(-X)*X**Y/(factorial(X)*factorial(Y)
                ), (X, 0, oo)), (Y, 0, oo))/factorial(x)
    assert density(Z)(x).simplify() == exprd

    N = Normal('N', 1, 2)
    M = Normal('M', 3, 4)
    D = Normal('D', M, N)
    exprd = Integral(sqrt(2)*exp(-(_k - 1)**2/8)*Integral(exp(-(-_k + x
    )**2/(2*_k**2))*exp(-(_k - 3)**2/32)/(8*pi*_k)
    , (_k, -oo, oo))/(4*sqrt(pi)), (_k, -oo, oo))
    assert density(D, evaluate=False)(x).dummy_eq(exprd)
Beispiel #2
0
def test_literal_probability():
    X = Normal('X', 2, 3)
    Y = Normal('Y', 3, 4)
    Z = Poisson('Z', 4)
    W = Poisson('W', 3)
    x, y, w, z = symbols('x, y, w, z')

    assert Probability(X > 0).evaluate_integral() == probability(X > 0)
    assert Probability(X > x).evaluate_integral() == probability(X > x)
    assert Probability(X > 0).rewrite(Integral).doit() == probability(X > 0)
    assert Probability(X > x).rewrite(Integral).doit() == probability(X > x)

    assert Expectation(X).evaluate_integral() == expectation(X)
    assert Expectation(X).rewrite(Integral).doit() == expectation(X)
    assert Expectation(X**2).evaluate_integral() == expectation(X**2)
    assert Expectation(x*X).args == (x*X,)
    assert Expectation(x*X).doit() == x*Expectation(X)
    assert Expectation(2*X + 3*Y + z*X*Y).doit() == 2*Expectation(X) + 3*Expectation(Y) + z*Expectation(X*Y)
    assert Expectation(2*X + 3*Y + z*X*Y).args == (2*X + 3*Y + z*X*Y,)
    assert Expectation(sin(X)) == Expectation(sin(X)).doit()
    assert Expectation(2*x*sin(X)*Y + y*X**2 + z*X*Y).doit() == 2*x*Expectation(sin(X)*Y) + y*Expectation(X**2) + z*Expectation(X*Y)

    assert Variance(w).args == (w,)
    assert Variance(w).doit() == 0
    assert Variance(X).evaluate_integral() == Variance(X).rewrite(Integral).doit() == variance(X)
    assert Variance(X + z).args == (X + z,)
    assert Variance(X + z).doit() == Variance(X)
    assert Variance(X*Y).args == (Mul(X, Y),)
    assert type(Variance(X*Y)) == Variance
    assert Variance(z*X).doit() == z**2*Variance(X)
    assert Variance(X + Y).doit() == Variance(X) + Variance(Y) + 2*Covariance(X, Y)
    assert Variance(X + Y + Z + W).doit() == (Variance(X) + Variance(Y) + Variance(Z) + Variance(W) +
                                       2 * Covariance(X, Y) + 2 * Covariance(X, Z) + 2 * Covariance(X, W) +
                                       2 * Covariance(Y, Z) + 2 * Covariance(Y, W) + 2 * Covariance(W, Z))
    assert Variance(X**2).evaluate_integral() == variance(X**2)
    assert Variance(X**2) == Variance(X**2)
    assert Variance(x*X**2).doit() == x**2*Variance(X**2)
    assert Variance(sin(X)).args == (sin(X),)
    assert Variance(sin(X)).doit() == Variance(sin(X))
    assert Variance(x*sin(X)).doit() == x**2*Variance(sin(X))

    assert Covariance(w, z).args == (w, z)
    assert Covariance(w, z).doit() == 0
    assert Covariance(X, w).doit() == 0
    assert Covariance(w, X).doit() == 0
    assert Covariance(X, Y).args == (X, Y)
    assert type(Covariance(X, Y)) == Covariance
    assert Covariance(z*X + 3, Y).doit() == z*Covariance(X, Y)
    assert Covariance(X, X).args == (X, X)
    assert Covariance(X, X).doit() == Variance(X)
    assert Covariance(z*X + 3, w*Y + 4).doit() == w*z*Covariance(X,Y)
    assert Covariance(X, Y) == Covariance(Y, X)
    assert Covariance(X + Y, Z + W).doit() == Covariance(W, X) + Covariance(W, Y) + Covariance(X, Z) + Covariance(Y, Z)
    assert Covariance(x*X + y*Y, z*Z + w*W).doit() == (x*w*Covariance(W, X) + w*y*Covariance(W, Y) +
                                                x*z*Covariance(X, Z) + y*z*Covariance(Y, Z))
    assert Covariance(x*X**2 + y*sin(Y), z*Y*Z**2 + w*W).doit() == (w*x*Covariance(W, X**2) + w*y*Covariance(sin(Y), W) +
                                                        x*z*Covariance(Y*Z**2, X**2) + y*z*Covariance(Y*Z**2, sin(Y)))
    assert Covariance(X, X**2).doit() == Covariance(X, X**2)
    assert Covariance(X, sin(X)).doit() == Covariance(sin(X), X)
    assert Covariance(X**2, sin(X)*Y).doit() == Covariance(sin(X)*Y, X**2)
Beispiel #3
0
def test_unevaluated_CompoundDist():
    # these tests need to be removed once they work with evaluation as they are currently not
    # evaluated completely in sympy.
    R = Rayleigh('R', 4)
    X = Normal('X', 3, R)
    ans = '''
        Piecewise(((-sqrt(pi)*sinh(x/4 - 3/4) + sqrt(pi)*cosh(x/4 - 3/4))/(
        8*sqrt(pi)), Abs(arg(x - 3)) <= pi/4), (Integral(sqrt(2)*exp(-(x - 3)
        **2/(2*R**2))*exp(-R**2/32)/(32*sqrt(pi)), (R, 0, oo)), True))'''
    assert streq(density(X)(x), ans)

    expre = '''
        Integral(X*Integral(sqrt(2)*exp(-(X-3)**2/(2*R**2))*exp(-R**2/32)/(32*
        sqrt(pi)),(R,0,oo)),(X,-oo,oo))'''
    with ignore_warnings(
            UserWarning):  ### TODO: Restore tests once warnings are removed
        assert streq(E(X, evaluate=False).rewrite(Integral), expre)

    X = Poisson('X', 1)
    Y = Poisson('Y', X)
    Z = Poisson('Z', Y)
    exprd = Sum(
        exp(-Y) * Y**x * Sum(
            exp(-1) * exp(-X) * X**Y / (factorial(X) * factorial(Y)),
            (X, 0, oo)) / factorial(x), (Y, 0, oo))
    assert density(Z)(x) == exprd

    N = Normal('N', 1, 2)
    M = Normal('M', 3, 4)
    D = Normal('D', M, N)
    exprd = '''
        Integral(sqrt(2)*exp(-(N-1)**2/8)*Integral(exp(-(x-M)**2/(2*N**2))*exp
        (-(M-3)**2/32)/(8*pi*N),(M,-oo,oo))/(4*sqrt(pi)),(N,-oo,oo))'''
    assert streq(density(D, evaluate=False)(x), exprd)
Beispiel #4
0
def test_probability_rewrite():
    X = Normal('X', 2, 3)
    Y = Normal('Y', 3, 4)
    Z = Poisson('Z', 4)
    W = Poisson('W', 3)
    x, y, w, z = symbols('x, y, w, z')

    assert Variance(w).rewrite(Expectation) == 0
    assert Variance(X).rewrite(Expectation) == Expectation(X ** 2) - Expectation(X) ** 2
    assert Variance(X, condition=Y).rewrite(Expectation) == Expectation(X ** 2, Y) - Expectation(X, Y) ** 2
    assert Variance(X, Y) != Expectation(X**2) - Expectation(X)**2
    assert Variance(X + z).rewrite(Expectation) == Expectation((X + z) ** 2) - Expectation(X + z) ** 2
    assert Variance(X * Y).rewrite(Expectation) == Expectation(X ** 2 * Y ** 2) - Expectation(X * Y) ** 2

    assert Covariance(w, X).rewrite(Expectation) == -w*Expectation(X) + Expectation(w*X)
    assert Covariance(X, Y).rewrite(Expectation) == Expectation(X*Y) - Expectation(X)*Expectation(Y)
    assert Covariance(X, Y, condition=W).rewrite(Expectation) == Expectation(X * Y, W) - Expectation(X, W) * Expectation(Y, W)

    w, x, z = symbols("W, x, z")
    px = Probability(Eq(X, x))
    pz = Probability(Eq(Z, z))

    assert Expectation(X).rewrite(Probability) == Integral(x*px, (x, -oo, oo))
    assert Expectation(Z).rewrite(Probability) == Sum(z*pz, (z, 0, oo))
    assert Variance(X).rewrite(Probability) == Integral(x**2*px, (x, -oo, oo)) - Integral(x*px, (x, -oo, oo))**2
    assert Variance(Z).rewrite(Probability) == Sum(z**2*pz, (z, 0, oo)) - Sum(z*pz, (z, 0, oo))**2

    assert Variance(X, condition=Y).rewrite(Probability) == Integral(x**2*Probability(Eq(X, x), Y), (x, -oo, oo)) - \
                                                            Integral(x*Probability(Eq(X, x), Y), (x, -oo, oo))**2
Beispiel #5
0
def test_probability_rewrite():
    X = Normal("X", 2, 3)
    Y = Normal("Y", 3, 4)
    Z = Poisson("Z", 4)
    W = Poisson("W", 3)
    x, y, w, z = symbols("x, y, w, z")

    assert Variance(w).rewrite(Expectation) == 0
    assert Variance(X).rewrite(Expectation) == Expectation(
        X**2) - Expectation(X)**2
    assert (Variance(
        X, condition=Y).rewrite(Expectation) == Expectation(X**2, Y) -
            Expectation(X, Y)**2)
    assert Variance(X, Y) != Expectation(X**2) - Expectation(X)**2
    assert (Variance(X + z).rewrite(Expectation) == Expectation(
        (X + z)**2) - Expectation(X + z)**2)
    assert (Variance(X * Y).rewrite(Expectation) == Expectation(X**2 * Y**2) -
            Expectation(X * Y)**2)

    assert Covariance(
        w, X).rewrite(Expectation) == -w * Expectation(X) + Expectation(w * X)
    assert Covariance(X, Y).rewrite(Expectation) == Expectation(
        X * Y) - Expectation(X) * Expectation(Y)
    assert Covariance(X, Y, condition=W).rewrite(Expectation) == Expectation(
        X * Y, W) - Expectation(X, W) * Expectation(Y, W)

    w, x, z = symbols("W, x, z")
    px = Probability(Eq(X, x))
    pz = Probability(Eq(Z, z))

    assert Expectation(X).rewrite(Probability) == Integral(
        x * px, (x, -oo, oo))
    assert Expectation(Z).rewrite(Probability) == Sum(z * pz, (z, 0, oo))
    assert (
        Variance(X).rewrite(Probability) == Integral(x**2 * px, (x, -oo, oo)) -
        Integral(x * px, (x, -oo, oo))**2)
    assert (Variance(Z).rewrite(Probability) == Sum(z**2 * pz, (z, 0, oo)) -
            Sum(z * pz, (z, 0, oo))**2)
    assert Covariance(w, X).rewrite(Probability) == -w * Integral(
        x * Probability(Eq(X, x)),
        (x, -oo, oo)) + Integral(w * x * Probability(Eq(X, x)), (x, -oo, oo))

    # To test rewrite as sum function
    assert Variance(X).rewrite(Sum) == Variance(X).rewrite(Integral)
    assert Expectation(X).rewrite(Sum) == Expectation(X).rewrite(Integral)

    assert Covariance(w, X).rewrite(Sum) == 0

    assert Covariance(w, X).rewrite(Integral) == 0

    assert (Variance(X, condition=Y).rewrite(Probability) == Integral(
        x**2 * Probability(Eq(X, x), Y),
        (x, -oo, oo)) - Integral(x * Probability(Eq(X, x), Y),
                                 (x, -oo, oo))**2)
Beispiel #6
0
def test_sample_scipy():
    p = S(2)/3
    x = Symbol('x', integer=True, positive=True)
    pdf = p*(1 - p)**(x - 1) # pdf of Geometric Distribution
    distribs_scipy = [
        DiscreteRV(x, pdf, set=S.Naturals),
        Geometric('G', 0.5),
        Logarithmic('L', 0.5),
        NegativeBinomial('N', 5, 0.4),
        Poisson('P', 1),
        Skellam('S', 1, 1),
        YuleSimon('Y', 1),
        Zeta('Z', 2)
    ]
    size = 3
    scipy = import_module('scipy')
    if not scipy:
        skip('Scipy is not installed. Abort tests for _sample_scipy.')
    else:
        for X in distribs_scipy:
            samps = sample(X, size=size, library='scipy')
            samps2 = sample(X, size=(2, 2), library='scipy')
            for sam in samps:
                assert sam in X.pspace.domain.set
            for i in range(2):
                for j in range(2):
                    assert samps2[i][j] in X.pspace.domain.set
Beispiel #7
0
def test_density():
    x = Symbol('x')
    l = Symbol('l', positive=True)
    rate = Beta(l, 2, 3)
    X = Poisson(x, rate)
    assert isinstance(pspace(X), ProductPSpace)
    assert density(X, Eq(rate, rate.symbol)) == PoissonDistribution(l)
Beispiel #8
0
def test_poisson_CompoundDist():
    k, t, y = symbols('k t y', positive=True, real=True)
    G = Gamma('G', k, t)
    D = Poisson('P', G)
    assert density(D)(y).simplify() == t**y*(t + 1)**(-k - y)*gamma(k + y)/(gamma(k)*gamma(y + 1))
    # https://en.wikipedia.org/wiki/Negative_binomial_distribution#Gamma%E2%80%93Poisson_mixture
    assert E(D).simplify() == k*t # mean of NegativeBinomialDistribution
Beispiel #9
0
def test_sample_iter():

    X = Normal("X", 0, 1)
    Y = DiscreteUniform("Y", [1, 2, 7])
    Z = Poisson("Z", 2)

    expr = X ** 2 + 3
    iterator = sample_iter(expr)

    expr2 = Y ** 2 + 5 * Y + 4
    iterator2 = sample_iter(expr2)

    expr3 = Z ** 3 + 4
    iterator3 = sample_iter(expr3)

    def is_iterator(obj):
        if (
            hasattr(obj, "__iter__")
            and (hasattr(obj, "next") or hasattr(obj, "__next__"))
            and callable(obj.__iter__)
            and obj.__iter__() is obj
        ):
            return True
        else:
            return False

    assert is_iterator(iterator)
    assert is_iterator(iterator2)
    assert is_iterator(iterator3)
Beispiel #10
0
def test_sample_iter():

    X = Normal('X', 0, 1)
    Y = DiscreteUniform('Y', [1, 2, 7])
    Z = Poisson('Z', 2)

    scipy = import_module('scipy')
    if not scipy:
        skip('Scipy is not installed. Abort tests')
    expr = X**2 + 3
    iterator = sample_iter(expr)

    expr2 = Y**2 + 5 * Y + 4
    iterator2 = sample_iter(expr2)

    expr3 = Z**3 + 4
    iterator3 = sample_iter(expr3)

    def is_iterator(obj):
        if (hasattr(obj, '__iter__')
                and (hasattr(obj, 'next') or hasattr(obj, '__next__'))
                and callable(obj.__iter__) and obj.__iter__() is obj):
            return True
        else:
            return False

    assert is_iterator(iterator)
    assert is_iterator(iterator2)
    assert is_iterator(iterator3)
def test_sample_scipy():
    p = S(2) / 3
    x = Symbol('x', integer=True, positive=True)
    pdf = p * (1 - p)**(x - 1)  # pdf of Geometric Distribution
    distribs_scipy = [
        DiscreteRV(x, pdf, set=S.Naturals),
        Geometric('G', 0.5),
        Logarithmic('L', 0.5),
        NegativeBinomial('N', 5, 0.4),
        Poisson('P', 1),
        Skellam('S', 1, 1),
        YuleSimon('Y', 1),
        Zeta('Z', 2)
    ]
    size = 3
    numsamples = 5
    scipy = import_module('scipy')
    if not scipy:
        skip('Scipy is not installed. Abort tests for _sample_scipy.')
    else:
        with ignore_warnings(
                UserWarning
        ):  ### TODO: Restore tests once warnings are removed
            z_sample = list(
                sample(Zeta("G", 7), size=size, numsamples=numsamples))
            assert len(z_sample) == numsamples
            for X in distribs_scipy:
                samps = next(sample(X, size=size, library='scipy'))
                samps2 = next(sample(X, size=(2, 2), library='scipy'))
                for sam in samps:
                    assert sam in X.pspace.domain.set
                for i in range(2):
                    for j in range(2):
                        assert samps2[i][j] in X.pspace.domain.set
Beispiel #12
0
def test_sample_iter():

    X = Normal('X', 0, 1)
    Y = DiscreteUniform('Y', [1, 2, 7])
    Z = Poisson('Z', 2)

    expr = X**2 + 3
    iterator = sample_iter(expr)

    expr2 = Y**2 + 5 * Y + 4
    iterator2 = sample_iter(expr2)

    expr3 = Z**3 + 4
    iterator3 = sample_iter(expr3)

    def is_iterator(obj):
        if (hasattr(obj, '__iter__')
                and (hasattr(obj, 'next') or hasattr(obj, '__next__'))
                and callable(obj.__iter__) and obj.__iter__() is obj):
            return True
        else:
            return False

    assert is_iterator(iterator)
    assert is_iterator(iterator2)
    assert is_iterator(iterator3)
Beispiel #13
0
def test_probability_rewrite():
    X = Normal('X', 2, 3)
    Y = Normal('Y', 3, 4)
    Z = Poisson('Z', 4)
    W = Poisson('W', 3)
    x, y, w, z = symbols('x, y, w, z')

    assert Variance(w).rewrite(Expectation) == 0
    assert Variance(X).rewrite(Expectation) == Expectation(X ** 2) - Expectation(X) ** 2
    assert Variance(X, condition=Y).rewrite(Expectation) == Expectation(X ** 2, Y) - Expectation(X, Y) ** 2
    assert Variance(X, Y) != Expectation(X**2) - Expectation(X)**2
    assert Variance(X + z).rewrite(Expectation) == Expectation(X ** 2) - Expectation(X) ** 2
    assert Variance(X * Y).rewrite(Expectation) == Expectation(X ** 2 * Y ** 2) - Expectation(X * Y) ** 2

    assert Covariance(w, X).rewrite(Expectation) == 0
    assert Covariance(X, Y).rewrite(Expectation) == Expectation(X*Y) - Expectation(X)*Expectation(Y)
    assert Covariance(X, Y, condition=W).rewrite(Expectation) == Expectation(X * Y, W) - Expectation(X, W) * Expectation(Y, W)
Beispiel #14
0
def test_mix_expression():
    Y, E = Poisson('Y', 1), Exponential('E', 1)
    assert P(Eq(Y + E, 1)) == 0
    assert P(Ne(Y + E, 2)) == 1
    assert str(P(E + Y < 2, evaluate=False)) == """Integral(Sum(exp(-1)*Integral"""\
+"""(exp(-E)*DiracDelta(-_z + E + Y - 2), (E, 0, oo))/factorial(Y), (Y, 0, oo)), (_z, -oo, 0))"""
    assert str(P(E + Y > 2, evaluate=False)) == """Integral(Sum(exp(-1)*Integral"""\
+"""(exp(-E)*DiracDelta(-_z + E + Y - 2), (E, 0, oo))/factorial(Y), (Y, 0, oo)), (_z, 0, oo))"""
Beispiel #15
0
def test_mix_expression():
    Y, E = Poisson('Y', 1), Exponential('E', 1)
    k = Dummy('k')
    expr1 = Integral(Sum(exp(-1)*Integral(exp(-k)*DiracDelta(k - 2), (k, 0, oo)
    )/factorial(k), (k, 0, oo)), (k, -oo, 0))
    expr2 = Integral(Sum(exp(-1)*Integral(exp(-k)*DiracDelta(k - 2), (k, 0, oo)
    )/factorial(k), (k, 0, oo)), (k, 0, oo))
    assert P(Eq(Y + E, 1)) == 0
    assert P(Ne(Y + E, 2)) == 1
    with ignore_warnings(UserWarning): ### TODO: Restore tests once warnings are removed
        assert P(E + Y < 2, evaluate=False).rewrite(Integral).dummy_eq(expr1)
        assert P(E + Y > 2, evaluate=False).rewrite(Integral).dummy_eq(expr2)
Beispiel #16
0
def test_density():
    x = Symbol('x')
    l = Symbol('l', positive=True)
    rate = Beta(l, 2, 3)
    X = Poisson(x, rate)
    assert isinstance(pspace(X), JointPSpace)
    assert density(X, Eq(rate, rate.symbol)) == PoissonDistribution(l)
    N1 = Normal('N1', 0, 1)
    N2 = Normal('N2', N1, 2)
    assert density(N2)(0).doit() == sqrt(10) / (10 * sqrt(pi))
    assert simplify(density(N2, Eq(N1, 1))(x)) == \
        sqrt(2)*exp(-(x - 1)**2/8)/(4*sqrt(pi))
Beispiel #17
0
def test_moment_generating_function():

    X = Normal('X', 0, 1)
    Y = DiscreteUniform('Y', [1, 2, 7])
    Z = Poisson('Z', 2)
    t = symbols('_t')
    P = Lambda(t, exp(t**2 / 2))
    Q = Lambda(t, (exp(7 * t) / 3 + exp(2 * t) / 3 + exp(t) / 3))
    R = Lambda(t, exp(2 * exp(t) - 2))

    assert moment_generating_function(X).dummy_eq(P)
    assert moment_generating_function(Y).dummy_eq(Q)
    assert moment_generating_function(Z).dummy_eq(R)
Beispiel #18
0
def test_moment_generating_function():

    X = Normal("X", 0, 1)
    Y = DiscreteUniform("Y", [1, 2, 7])
    Z = Poisson("Z", 2)
    t = symbols("_t")
    P = Lambda(t, exp(t ** 2 / 2))
    Q = Lambda(t, (exp(7 * t) / 3 + exp(2 * t) / 3 + exp(t) / 3))
    R = Lambda(t, exp(2 * exp(t) - 2))

    assert moment_generating_function(X) == P
    assert moment_generating_function(Y) == Q
    assert moment_generating_function(Z) == R
Beispiel #19
0
def test_characteristic_function():
    #  Imports I from sympy
    from sympy.core.numbers import I
    X = Normal('X', 0, 1)
    Y = DiscreteUniform('Y', [1, 2, 7])
    Z = Poisson('Z', 2)
    t = symbols('_t')
    P = Lambda(t, exp(-t**2 / 2))
    Q = Lambda(t, exp(7 * t * I) / 3 + exp(2 * t * I) / 3 + exp(t * I) / 3)
    R = Lambda(t, exp(2 * exp(t * I) - 2))

    assert characteristic_function(X).dummy_eq(P)
    assert characteristic_function(Y).dummy_eq(Q)
    assert characteristic_function(Z).dummy_eq(R)
Beispiel #20
0
def test_characteristic_function():
    #  Imports I from sympy
    from sympy import I

    X = Normal("X", 0, 1)
    Y = DiscreteUniform("Y", [1, 2, 7])
    Z = Poisson("Z", 2)
    t = symbols("_t")
    P = Lambda(t, exp(-(t ** 2) / 2))
    Q = Lambda(t, exp(7 * t * I) / 3 + exp(2 * t * I) / 3 + exp(t * I) / 3)
    R = Lambda(t, exp(2 * exp(t * I) - 2))

    assert characteristic_function(X) == P
    assert characteristic_function(Y) == Q
    assert characteristic_function(Z) == R
Beispiel #21
0
def test_factorial_moment():
    X = Poisson('X', 2)
    Y = Binomial('Y', 2, S.Half)
    Z = Hypergeometric('Z', 4, 2, 2)
    assert factorial_moment(X, 2) == 4
    assert factorial_moment(Y, 2) == S.Half
    assert factorial_moment(Z, 2) == Rational(1, 3)

    x, y, z, l = symbols('x y z l')
    Y = Binomial('Y', 2, y)
    Z = Hypergeometric('Z', 10, 2, 3)
    assert factorial_moment(Y, l) == y**2*FallingFactorial(
        2, l) + 2*y*(1 - y)*FallingFactorial(1, l) + (1 - y)**2*\
            FallingFactorial(0, l)
    assert factorial_moment(Z, l) == 7*FallingFactorial(0, l)/\
        15 + 7*FallingFactorial(1, l)/15 + FallingFactorial(2, l)/15
Beispiel #22
0
def test_sample_pymc3():
    distribs_pymc3 = [
        Geometric('G', 0.5),
        Poisson('P', 1),
        NegativeBinomial('N', 5, 0.4)
    ]
    size = 3
    pymc3 = import_module('pymc3')
    if not pymc3:
        skip('PyMC3 is not installed. Abort tests for _sample_pymc3.')
    else:
        for X in distribs_pymc3:
            samps = sample(X, size=size, library='pymc3')
            for sam in samps:
                assert sam in X.pspace.domain.set
        raises(NotImplementedError,
               lambda: sample(Skellam('S', 1, 1), library='pymc3'))
def test_sample_numpy():
    distribs_numpy = [Geometric('G', 0.5), Poisson('P', 1), Zeta('Z', 2)]
    size = 3
    numpy = import_module('numpy')
    if not numpy:
        skip('Numpy is not installed. Abort tests for _sample_numpy.')
    else:
        with ignore_warnings(
                UserWarning
        ):  ### TODO: Restore tests once warnings are removed
            for X in distribs_numpy:
                samps = next(sample(X, size=size, library='numpy'))
                for sam in samps:
                    assert sam in X.pspace.domain.set
            raises(NotImplementedError,
                   lambda: next(sample(Skellam('S', 1, 1), library='numpy')))
    raises(
        NotImplementedError, lambda: Skellam('S', 1, 1).pspace.distribution.
        sample(library='tensorflow'))
Beispiel #24
0
def test_sample_numpy():
    distribs_numpy = [
        Geometric('G', 0.5),
        Poisson('P', 1),
        Zeta('Z', 2)
    ]
    size = 3
    numpy = import_module('numpy')
    if not numpy:
        skip('Numpy is not installed. Abort tests for _sample_numpy.')
    else:
        for X in distribs_numpy:
            samps = sample(X, size=size, library='numpy')
            for sam in samps:
                assert sam in X.pspace.domain.set
        raises(NotImplementedError,
               lambda: sample(Skellam('S', 1, 1), library='numpy'))
    raises(NotImplementedError,
           lambda: Skellam('S', 1, 1).pspace.distribution.sample(library='tensorflow'))
def test_sample_pymc3():
    distribs_pymc3 = [
        Geometric('G', 0.5),
        Poisson('P', 1),
        NegativeBinomial('N', 5, 0.4)
    ]
    size = 3
    pymc3 = import_module('pymc3')
    if not pymc3:
        skip('PyMC3 is not installed. Abort tests for _sample_pymc3.')
    else:
        with ignore_warnings(
                UserWarning
        ):  ### TODO: Restore tests once warnings are removed
            for X in distribs_pymc3:
                samps = next(sample(X, size=size, library='pymc3'))
                for sam in samps:
                    assert sam in X.pspace.domain.set
            raises(NotImplementedError,
                   lambda: next(sample(Skellam('S', 1, 1), library='pymc3')))
Beispiel #26
0
def test_factorial_moment():
    X = Poisson("X", 2)
    Y = Binomial("Y", 2, S.Half)
    Z = Hypergeometric("Z", 4, 2, 2)
    assert factorial_moment(X, 2) == 4
    assert factorial_moment(Y, 2) == S.Half
    assert factorial_moment(Z, 2) == Rational(1, 3)

    x, y, z, l = symbols("x y z l")
    Y = Binomial("Y", 2, y)
    Z = Hypergeometric("Z", 10, 2, 3)
    assert factorial_moment(Y, l) == y ** 2 * FallingFactorial(2, l) + 2 * y * (
        1 - y
    ) * FallingFactorial(1, l) + (1 - y) ** 2 * FallingFactorial(0, l)
    assert (
        factorial_moment(Z, l)
        == 7 * FallingFactorial(0, l) / 15
        + 7 * FallingFactorial(1, l) / 15
        + FallingFactorial(2, l) / 15
    )
Beispiel #27
0
import sympy as sp
from sympy.stats import Poisson, density

lamb = sp.symbols('lambda')
n = sp.symbols('n')
distro = Poisson('N', lamb)
dens = density(distro)(n)

Pd = sp.symbols('P_d')
Pd = 0.234
r_k_prev = 0.5  # previous existance probability
# l^ak probability that the meesurement k os associated to the current state
sum_l = 0.1  # probability that any of the measurement comes from current state

L_k = sp.simplify((1 - Pd) + (Pd / lamb) * sum_l)

# IPDA existence probability
r_k = sp.simplify((L_k * r_k_prev) / (1 - (1 - L_k) * r_k_prev), rational=1)
Beispiel #28
0
def test_compound_distribution():
    Y = Poisson('Y', 1)
    Z = Poisson('Z', Y)
    assert isinstance(pspace(Z), JointPSpace)
    assert isinstance(pspace(Z).distribution, CompoundDistribution)
def test_literal_probability():
    X = Normal('X', 2, 3)
    Y = Normal('Y', 3, 4)
    Z = Poisson('Z', 4)
    W = Poisson('W', 3)
    x = symbols('x', real=True)
    y, w, z = symbols('y, w, z')

    assert Probability(X > 0).evaluate_integral() == probability(X > 0)
    assert Probability(X > x).evaluate_integral() == probability(X > x)
    assert Probability(X > 0).rewrite(Integral).doit() == probability(X > 0)
    assert Probability(X > x).rewrite(Integral).doit() == probability(X > x)

    assert Expectation(X).evaluate_integral() == expectation(X)
    assert Expectation(X).rewrite(Integral).doit() == expectation(X)
    assert Expectation(X**2).evaluate_integral() == expectation(X**2)
    assert Expectation(x * X).args == (x * X, )
    assert Expectation(x * X).expand() == x * Expectation(X)
    assert Expectation(2 * X + 3 * Y + z * X * Y).expand(
    ) == 2 * Expectation(X) + 3 * Expectation(Y) + z * Expectation(X * Y)
    assert Expectation(2 * X + 3 * Y + z * X * Y).args == (2 * X + 3 * Y +
                                                           z * X * Y, )
    assert Expectation(sin(X)) == Expectation(sin(X)).expand()
    assert Expectation(2*x*sin(X)*Y + y*X**2 + z*X*Y).expand() == 2*x*Expectation(sin(X)*Y) \
                            + y*Expectation(X**2) + z*Expectation(X*Y)
    assert Expectation(X + Y).expand() == Expectation(X) + Expectation(Y)
    assert Expectation(
        (X + Y) * (X - Y)).expand() == Expectation(X**2) - Expectation(Y**2)
    assert Expectation((X + Y) * (X - Y)).expand().doit() == -12
    assert Expectation(X + Y, evaluate=True).doit() == 5
    assert Expectation(X + Expectation(Y)).doit() == 5
    assert Expectation(X + Expectation(Y)).doit(
        deep=False) == 2 + Expectation(Expectation(Y))
    assert Expectation(X + Expectation(Y + Expectation(2*X))).doit(deep=False) == 2 \
                                + Expectation(Expectation(Y + Expectation(2*X)))
    assert Expectation(X + Expectation(Y + Expectation(2 * X))).doit() == 9
    assert Expectation(Expectation(2 * X)).doit() == 4
    assert Expectation(Expectation(2 * X)).doit(deep=False) == Expectation(2 *
                                                                           X)
    assert Expectation(
        4 * Expectation(2 * X)).doit(deep=False) == 4 * Expectation(2 * X)
    assert Expectation((X + Y)**3).expand() == 3*Expectation(X*Y**2) +\
                3*Expectation(X**2*Y) + Expectation(X**3) + Expectation(Y**3)
    assert Expectation((X - Y)**3).expand() == 3*Expectation(X*Y**2) -\
                3*Expectation(X**2*Y) + Expectation(X**3) - Expectation(Y**3)
    assert Expectation((X - Y)**2).expand() == -2*Expectation(X*Y) +\
                Expectation(X**2) + Expectation(Y**2)

    assert Variance(w).args == (w, )
    assert Variance(w).expand() == 0
    assert Variance(X).evaluate_integral() == Variance(X).rewrite(
        Integral).doit() == variance(X)
    assert Variance(X + z).args == (X + z, )
    assert Variance(X + z).expand() == Variance(X)
    assert Variance(X * Y).args == (Mul(X, Y), )
    assert type(Variance(X * Y)) == Variance
    assert Variance(z * X).expand() == z**2 * Variance(X)
    assert Variance(
        X + Y).expand() == Variance(X) + Variance(Y) + 2 * Covariance(X, Y)
    assert Variance(X + Y + Z + W).expand() == (
        Variance(X) + Variance(Y) + Variance(Z) + Variance(W) +
        2 * Covariance(X, Y) + 2 * Covariance(X, Z) + 2 * Covariance(X, W) +
        2 * Covariance(Y, Z) + 2 * Covariance(Y, W) + 2 * Covariance(W, Z))
    assert Variance(X**2).evaluate_integral() == variance(X**2)
    assert unchanged(Variance, X**2)
    assert Variance(x * X**2).expand() == x**2 * Variance(X**2)
    assert Variance(sin(X)).args == (sin(X), )
    assert Variance(sin(X)).expand() == Variance(sin(X))
    assert Variance(x * sin(X)).expand() == x**2 * Variance(sin(X))

    assert Covariance(w, z).args == (w, z)
    assert Covariance(w, z).expand() == 0
    assert Covariance(X, w).expand() == 0
    assert Covariance(w, X).expand() == 0
    assert Covariance(X, Y).args == (X, Y)
    assert type(Covariance(X, Y)) == Covariance
    assert Covariance(z * X + 3, Y).expand() == z * Covariance(X, Y)
    assert Covariance(X, X).args == (X, X)
    assert Covariance(X, X).expand() == Variance(X)
    assert Covariance(z * X + 3,
                      w * Y + 4).expand() == w * z * Covariance(X, Y)
    assert Covariance(X, Y) == Covariance(Y, X)
    assert Covariance(X + Y, Z + W).expand() == Covariance(W, X) + Covariance(
        W, Y) + Covariance(X, Z) + Covariance(Y, Z)
    assert Covariance(x * X + y * Y,
                      z * Z + w * W).expand() == (x * w * Covariance(W, X) +
                                                  w * y * Covariance(W, Y) +
                                                  x * z * Covariance(X, Z) +
                                                  y * z * Covariance(Y, Z))
    assert Covariance(x * X**2 + y * sin(Y), z * Y * Z**2 +
                      w * W).expand() == (w * x * Covariance(W, X**2) +
                                          w * y * Covariance(sin(Y), W) +
                                          x * z * Covariance(Y * Z**2, X**2) +
                                          y * z * Covariance(Y * Z**2, sin(Y)))
    assert Covariance(X, X**2).expand() == Covariance(X, X**2)
    assert Covariance(X, sin(X)).expand() == Covariance(sin(X), X)
    assert Covariance(X**2,
                      sin(X) * Y).expand() == Covariance(sin(X) * Y, X**2)
    assert Covariance(w, X).evaluate_integral() == 0
Beispiel #30
0
def test_compound_distribution():
    Y = Poisson('Y', 1)
    Z = Poisson('Z', Y)
    assert isinstance(pspace(Z), JointPSpace)
    assert isinstance(pspace(Z).distribution, CompoundDistribution)
    assert Z.pspace.distribution.pdf(1).doit() == exp(-2)*exp(exp(-1))