Exemplo n.º 1
0
def test_rayleigh():
    sigma = Symbol("sigma", positive=True)

    X = Rayleigh('x', sigma)
    assert density(X)(x) == x * exp(-x**2 / (2 * sigma**2)) / sigma**2
    assert E(X) == sqrt(2) * sqrt(pi) * sigma / 2
    assert variance(X) == -pi * sigma**2 / 2 + 2 * sigma**2
Exemplo n.º 2
0
def test_precomputed_characteristic_functions():
    import mpmath

    def test_cf(dist, support_lower_limit, support_upper_limit):
        pdf = density(dist)
        t = Symbol('t')
        x = Symbol('x')

        # first function is the hardcoded CF of the distribution
        cf1 = lambdify([t], characteristic_function(dist)(t), 'mpmath')

        # second function is the Fourier transform of the density function
        f = lambdify([x, t], pdf(x) * exp(I * x * t), 'mpmath')
        cf2 = lambda t: mpmath.quad(lambda x: f(x, t),
                                    [support_lower_limit, support_upper_limit],
                                    maxdegree=10)

        # compare the two functions at various points
        for test_point in [2, 5, 8, 11]:
            n1 = cf1(test_point)
            n2 = cf2(test_point)

            assert abs(re(n1) - re(n2)) < 1e-12
            assert abs(im(n1) - im(n2)) < 1e-12

    test_cf(Beta('b', 1, 2), 0, 1)
    test_cf(Chi('c', 3), 0, mpmath.inf)
    test_cf(ChiSquared('c', 2), 0, mpmath.inf)
    test_cf(Exponential('e', 6), 0, mpmath.inf)
    test_cf(Logistic('l', 1, 2), -mpmath.inf, mpmath.inf)
    test_cf(Normal('n', -1, 5), -mpmath.inf, mpmath.inf)
    test_cf(RaisedCosine('r', 3, 1), 2, 4)
    test_cf(Rayleigh('r', 0.5), 0, mpmath.inf)
    test_cf(Uniform('u', -1, 1), -1, 1)
    test_cf(WignerSemicircle('w', 3), -3, 3)
Exemplo n.º 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)
Exemplo n.º 4
0
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),
        FDistribution("FD", 1, 2),
        Gumbel("GB", 1, 2),
        Laplace("L", 1, 2),
        Logistic("LO", 1, 2),
        Rayleigh("R", 1),
        Triangular("T", 1, 2, 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(Chi("C", 1), library='numpy'))
    raises(NotImplementedError,
           lambda: Chi("C", 1).pspace.distribution.sample(library='tensorflow'))
Exemplo n.º 5
0
def test_rayleigh():
    sigma = Symbol("sigma", positive=True)
    x = Symbol("x")

    X = Rayleigh(sigma, symbol=x)
    assert density(X) == Lambda(_x,
                                _x * exp(-_x**2 / (2 * sigma**2)) / sigma**2)
    assert E(X) == sqrt(2) * sqrt(pi) * sigma / 2
    assert variance(X) == -pi * sigma**2 / 2 + 2 * sigma**2
Exemplo n.º 6
0
def test_rayleigh():
    sigma = Symbol("sigma", positive=True)

    X = Rayleigh('x', sigma)

    #Tests characteristic_function
    assert characteristic_function(X)(x) == (-sqrt(2)*sqrt(pi)*sigma*x*(erfi(sqrt(2)*sigma*x/2) - I)*exp(-sigma**2*x**2/2)/2 + 1)

    assert density(X)(x) ==  x*exp(-x**2/(2*sigma**2))/sigma**2
    assert E(X) == sqrt(2)*sqrt(pi)*sigma/2
    assert variance(X) == -pi*sigma**2/2 + 2*sigma**2
    assert cdf(X)(x) == 1 - exp(-x**2/(2*sigma**2))
    assert diff(cdf(X)(x), x) == density(X)(x)
Exemplo n.º 7
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(
        Piecewise(
            (_k * exp(S(3) / 4 - _k / 4) / 8, 2 * Abs(arg(_k - 3)) <= pi / 2),
            (sqrt(2) * _k *
             Integral(exp(-(_k**4 + 16 * (_k - 3)**2) / (32 * _k**2)),
                      (_k, 0, oo)) / (32 * sqrt(pi)), True)), (_k, -oo, oo))
    assert (E(X, evaluate=False).simplify()).dummy_eq(expre.simplify())
Exemplo n.º 8
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)
Exemplo n.º 9
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)