Пример #1
0
def test_prefab_sampling():
    N = Normal(0, 1)
    L = LogNormal(0, 1)
    E = Exponential(1)
    P = Pareto(1, 3)
    W = Weibull(1, 1)
    U = Uniform(0, 1)
    B = Beta(2, 5)
    G = Gamma(1, 3)

    variables = [N, L, E, P, W, U, B, G]
    niter = 10
    for var in variables:
        for i in xrange(niter):
            assert sample(var) in var.pspace.domain.set
def test_exponential():
    rate = Symbol('lambda', positive=True, real=True, finite=True)
    X = Exponential('x', rate)

    assert E(X) == 1 / rate
    assert variance(X) == 1 / rate**2
    assert skewness(X) == 2
    assert skewness(X) == smoment(X, 3)
    assert smoment(2 * X, 4) == smoment(X, 4)
    assert moment(X, 3) == 3 * 2 * 1 / rate**3
    assert P(X > 0) == S(1)
    assert P(X > 1) == exp(-rate)
    assert P(X > 10) == exp(-10 * rate)

    assert where(X <= 1).set == Interval(0, 1)
def test_characteristic_function():
    X = Uniform('x', 0, 1)

    cf = characteristic_function(X)
    assert cf(1) == -I * (-1 + exp(I))

    Y = Normal('y', 1, 1)
    cf = characteristic_function(Y)
    assert cf(0) == 1
    assert simplify(cf(1)) == exp(I - S(1) / 2)

    Z = Exponential('z', 5)
    cf = characteristic_function(Z)
    assert cf(0) == 1
    assert simplify(cf(1)) == S(25) / 26 + 5 * I / 26
Пример #4
0
def test_prefab_sampling():
    N = Normal('X', 0, 1)
    L = LogNormal('L', 0, 1)
    E = Exponential('Ex', 1)
    P = Pareto('P', 1, 3)
    W = Weibull('W', 1, 1)
    U = Uniform('U', 0, 1)
    B = Beta('B', 2, 5)
    G = Gamma('G', 1, 3)

    variables = [N, L, E, P, W, U, B, G]
    niter = 10
    for var in variables:
        for i in range(niter):
            assert sample(var) in var.pspace.domain.set
Пример #5
0
def test_precomputed_cdf():
    x = symbols("x", real=True, finite=True)
    mu = symbols("mu", real=True, finite=True)
    sigma, xm, alpha = symbols("sigma xm alpha", positive=True, finite=True)
    n = symbols("n", integer=True, positive=True, finite=True)
    distribs = [
            Normal("X", mu, sigma),
            Pareto("P", xm, alpha),
            ChiSquared("C", n),
            Exponential("E", sigma),
            # LogNormal("L", mu, sigma),
    ]
    for X in distribs:
        compdiff = cdf(X)(x) - simplify(X.pspace.density.compute_cdf()(x))
        compdiff = simplify(compdiff.rewrite(erfc))
        assert compdiff == 0
Пример #6
0
def test_symbolic():
    mu1, mu2 = symbols('mu1 mu2', real=True, finite=True)
    s1, s2 = symbols('sigma1 sigma2', real=True, finite=True, positive=True)
    rate = Symbol('lambda', real=True, positive=True, finite=True)
    X = Normal('x', mu1, s1)
    Y = Normal('y', mu2, s2)
    Z = Exponential('z', rate)
    a, b, c = symbols('a b c', real=True, finite=True)

    assert E(X) == mu1
    assert E(X + Y) == mu1 + mu2
    assert E(a*X + b) == a*E(X) + b
    assert variance(X) == s1**2
    assert simplify(variance(X + a*Y + b)) == variance(X) + a**2*variance(Y)

    assert E(Z) == 1/rate
    assert E(a*Z + b) == a*E(Z) + b
    assert E(X + a*Z + b) == mu1 + a/rate + b
Пример #7
0
def test_symbolic():
    mu1, mu2 = symbols('mu1 mu2', real=True, bounded=True)
    s1, s2 = symbols('sigma1 sigma2', real=True, bounded=True, positive=True)
    rate = Symbol('lambda', real=True, positive=True, bounded=True)
    X = Normal(mu1, s1)
    Y = Normal(mu2, s2)
    Z = Exponential(rate)
    a, b, c = symbols('a b c', real=True, bounded=True)

    assert E(X) == mu1
    assert E(X+Y) == mu1+mu2
    assert E(a*X+b) == a*E(X)+b
    assert Var(X) == s1**2
    assert simplify(Var(X+a*Y+b)) == Var(X) + a**2*Var(Y)

    assert E(Z) == 1/rate
    assert E(a*Z+b) == a*E(Z)+b
    assert E(X+a*Z+b) == mu1 + a/rate + b
Пример #8
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)
Пример #9
0
def test_exponential():
    rate = Symbol('lambda', positive=True)
    X = Exponential('x', rate)
    p = Symbol("p", positive=True, real=True, finite=True)

    assert E(X) == 1 / rate
    assert variance(X) == 1 / rate**2
    assert skewness(X) == 2
    assert skewness(X) == smoment(X, 3)
    assert kurtosis(X) == 9
    assert kurtosis(X) == smoment(X, 4)
    assert smoment(2 * X, 4) == smoment(X, 4)
    assert moment(X, 3) == 3 * 2 * 1 / rate**3
    assert P(X > 0) is S.One
    assert P(X > 1) == exp(-rate)
    assert P(X > 10) == exp(-10 * rate)
    assert quantile(X)(p) == -log(1 - p) / rate

    assert where(X <= 1).set == Interval(0, 1)
Пример #10
0
def test_characteristic_function():
    X = Uniform('x', 0, 1)

    cf = characteristic_function(X)
    assert cf(1) == -I * (-1 + exp(I))

    Y = Normal('y', 1, 1)
    cf = characteristic_function(Y)
    assert cf(0) == 1
    assert cf(1) == exp(I - S(1) / 2)

    Z = Exponential('z', 5)
    cf = characteristic_function(Z)
    assert cf(0) == 1
    assert cf(1).expand() == S(25) / 26 + 5 * I / 26

    X = GaussianInverse('x', 1, 1)
    cf = characteristic_function(X)
    assert cf(0) == 1
    assert cf(1) == exp(1 - sqrt(1 - 2 * I))
def test_prefab_sampling():
    scipy = import_module('scipy')
    if not scipy:
        skip('Scipy is not installed. Abort tests')
    N = Normal('X', 0, 1)
    L = LogNormal('L', 0, 1)
    E = Exponential('Ex', 1)
    P = Pareto('P', 1, 3)
    W = Weibull('W', 1, 1)
    U = Uniform('U', 0, 1)
    B = Beta('B', 2, 5)
    G = Gamma('G', 1, 3)

    variables = [N, L, E, P, W, U, B, G]
    niter = 10
    size = 5
    for var in variables:
        for _ in range(niter):
            assert sample(var) in var.pspace.domain.set
            samps = sample(var, size=size)
            for samp in samps:
                assert samp in var.pspace.domain.set
Пример #12
0
def test_sample_scipy():
    distribs_scipy = [
        Beta("B", 1, 1),
        BetaPrime("BP", 1, 1),
        Cauchy("C", 1, 1),
        Chi("C", 1),
        Normal("N", 0, 1),
        Gamma("G", 2, 7),
        GammaInverse("GI", 1, 1),
        GaussianInverse("GUI", 1, 1),
        Exponential("E", 2),
        LogNormal("LN", 0, 1),
        Pareto("P", 1, 1),
        StudentT("S", 2),
        ChiSquared("CS", 2),
        Uniform("U", 0, 1)
    ]
    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
            g_sample = list(
                sample(Gamma("G", 2, 7), size=size, numsamples=numsamples))
            assert len(g_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
def test_sample_numpy():
    distribs_numpy = [
        Beta("B", 1, 1),
        Normal("N", 0, 1),
        Gamma("G", 2, 7),
        Exponential("E", 2),
        LogNormal("LN", 0, 1),
        Pareto("P", 1, 1),
        ChiSquared("CS", 2),
        Uniform("U", 0, 1)
    ]
    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(Chi("C", 1), library='numpy'))
    raises(NotImplementedError,
           lambda: Chi("C", 1).pspace.distribution.sample(library='tensorflow'))
def test_sample_pymc3():
    distribs_pymc3 = [
        Beta("B", 1, 1),
        Cauchy("C", 1, 1),
        Normal("N", 0, 1),
        Gamma("G", 2, 7),
        GaussianInverse("GI", 1, 1),
        Exponential("E", 2),
        LogNormal("LN", 0, 1),
        Pareto("P", 1, 1),
        ChiSquared("CS", 2),
        Uniform("U", 0, 1)
    ]
    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(Chi("C", 1), library='pymc3'))
Пример #15
0
def test_conditional_eq():
    E = Exponential('E', 1)
    assert P(Eq(E, 1), Eq(E, 1)) == 1
    assert P(Eq(E, 1), Eq(E, 2)) == 0
    assert P(E > 1, Eq(E, 2)) == 1
    assert P(E < 1, Eq(E, 2)) == 0
Пример #16
0
def test_FiniteSet_prob():
    E = Exponential('E', 3)
    N = Normal('N', 5, 7)
    assert P(Eq(E, 1)) is S.Zero
    assert P(Eq(N, 2)) is S.Zero
    assert P(Eq(N, x)) is S.Zero
Пример #17
0
def test_moment_generating_function():
    t = symbols('t', positive=True)

    # Symbolic tests
    a, b, c = symbols('a b c')

    mgf = moment_generating_function(Beta('x', a, b))(t)
    assert mgf == hyper((a, ), (a + b, ), t)

    mgf = moment_generating_function(Chi('x', a))(t)
    assert mgf == sqrt(2)*t*gamma(a/2 + S.Half)*\
        hyper((a/2 + S.Half,), (Rational(3, 2),), t**2/2)/gamma(a/2) +\
        hyper((a/2,), (S.Half,), t**2/2)

    mgf = moment_generating_function(ChiSquared('x', a))(t)
    assert mgf == (1 - 2 * t)**(-a / 2)

    mgf = moment_generating_function(Erlang('x', a, b))(t)
    assert mgf == (1 - t / b)**(-a)

    mgf = moment_generating_function(ExGaussian("x", a, b, c))(t)
    assert mgf == exp(a * t + b**2 * t**2 / 2) / (1 - t / c)

    mgf = moment_generating_function(Exponential('x', a))(t)
    assert mgf == a / (a - t)

    mgf = moment_generating_function(Gamma('x', a, b))(t)
    assert mgf == (-b * t + 1)**(-a)

    mgf = moment_generating_function(Gumbel('x', a, b))(t)
    assert mgf == exp(b * t) * gamma(-a * t + 1)

    mgf = moment_generating_function(Gompertz('x', a, b))(t)
    assert mgf == b * exp(b) * expint(t / a, b)

    mgf = moment_generating_function(Laplace('x', a, b))(t)
    assert mgf == exp(a * t) / (-b**2 * t**2 + 1)

    mgf = moment_generating_function(Logistic('x', a, b))(t)
    assert mgf == exp(a * t) * beta(-b * t + 1, b * t + 1)

    mgf = moment_generating_function(Normal('x', a, b))(t)
    assert mgf == exp(a * t + b**2 * t**2 / 2)

    mgf = moment_generating_function(Pareto('x', a, b))(t)
    assert mgf == b * (-a * t)**b * uppergamma(-b, -a * t)

    mgf = moment_generating_function(QuadraticU('x', a, b))(t)
    assert str(mgf) == (
        "(3*(t*(-4*b + (a + b)**2) + 4)*exp(b*t) - "
        "3*(t*(a**2 + 2*a*(b - 2) + b**2) + 4)*exp(a*t))/(t**2*(a - b)**3)")

    mgf = moment_generating_function(RaisedCosine('x', a, b))(t)
    assert mgf == pi**2 * exp(a * t) * sinh(b * t) / (b * t *
                                                      (b**2 * t**2 + pi**2))

    mgf = moment_generating_function(Rayleigh('x', a))(t)
    assert mgf == sqrt(2)*sqrt(pi)*a*t*(erf(sqrt(2)*a*t/2) + 1)\
        *exp(a**2*t**2/2)/2 + 1

    mgf = moment_generating_function(Triangular('x', a, b, c))(t)
    assert str(mgf) == ("(-2*(-a + b)*exp(c*t) + 2*(-a + c)*exp(b*t) + "
                        "2*(b - c)*exp(a*t))/(t**2*(-a + b)*(-a + c)*(b - c))")

    mgf = moment_generating_function(Uniform('x', a, b))(t)
    assert mgf == (-exp(a * t) + exp(b * t)) / (t * (-a + b))

    mgf = moment_generating_function(UniformSum('x', a))(t)
    assert mgf == ((exp(t) - 1) / t)**a

    mgf = moment_generating_function(WignerSemicircle('x', a))(t)
    assert mgf == 2 * besseli(1, a * t) / (a * t)

    # Numeric tests

    mgf = moment_generating_function(Beta('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 1) == hyper((2, ), (3, ), 1) / 2

    mgf = moment_generating_function(Chi('x', 1))(t)
    assert mgf.diff(t).subs(t, 1) == sqrt(2) * hyper(
        (1, ), (Rational(3, 2), ), S.Half) / sqrt(pi) + hyper(
            (Rational(3, 2), ),
            (Rational(3, 2), ), S.Half) + 2 * sqrt(2) * hyper(
                (2, ), (Rational(5, 2), ), S.Half) / (3 * sqrt(pi))

    mgf = moment_generating_function(ChiSquared('x', 1))(t)
    assert mgf.diff(t).subs(t, 1) == I

    mgf = moment_generating_function(Erlang('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == 1

    mgf = moment_generating_function(ExGaussian("x", 0, 1, 1))(t)
    assert mgf.diff(t).subs(t, 2) == -exp(2)

    mgf = moment_generating_function(Exponential('x', 1))(t)
    assert mgf.diff(t).subs(t, 0) == 1

    mgf = moment_generating_function(Gamma('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == 1

    mgf = moment_generating_function(Gumbel('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == EulerGamma + 1

    mgf = moment_generating_function(Gompertz('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 1) == -e * meijerg(((), (1, 1)),
                                                  ((0, 0, 0), ()), 1)

    mgf = moment_generating_function(Laplace('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == 1

    mgf = moment_generating_function(Logistic('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == beta(1, 1)

    mgf = moment_generating_function(Normal('x', 0, 1))(t)
    assert mgf.diff(t).subs(t, 1) == exp(S.Half)

    mgf = moment_generating_function(Pareto('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 0) == expint(1, 0)

    mgf = moment_generating_function(QuadraticU('x', 1, 2))(t)
    assert mgf.diff(t).subs(t, 1) == -12 * e - 3 * exp(2)

    mgf = moment_generating_function(RaisedCosine('x', 1, 1))(t)
    assert mgf.diff(t).subs(t, 1) == -2*e*pi**2*sinh(1)/\
    (1 + pi**2)**2 + e*pi**2*cosh(1)/(1 + pi**2)

    mgf = moment_generating_function(Rayleigh('x', 1))(t)
    assert mgf.diff(t).subs(t, 0) == sqrt(2) * sqrt(pi) / 2

    mgf = moment_generating_function(Triangular('x', 1, 3, 2))(t)
    assert mgf.diff(t).subs(t, 1) == -e + exp(3)

    mgf = moment_generating_function(Uniform('x', 0, 1))(t)
    assert mgf.diff(t).subs(t, 1) == 1

    mgf = moment_generating_function(UniformSum('x', 1))(t)
    assert mgf.diff(t).subs(t, 1) == 1

    mgf = moment_generating_function(WignerSemicircle('x', 1))(t)
    assert mgf.diff(t).subs(t, 1) == -2*besseli(1, 1) + besseli(2, 1) +\
        besseli(0, 1)
Пример #18
0
def test_issue_10003():
    X = Exponential('x', 3)
    G = Gamma('g', 1, 2)
    assert P(X < -1) == S.Zero
    assert P(G < -1) == S.Zero
Пример #19
0
def test_issue_8129():
    X = Exponential('X', 4)
    assert P(X >= X) == 1
    assert P(X > X) == 0
    assert P(X > X+1) == 0