Ejemplo n.º 1
0
def test_DiscreteRV():
    p = S(1) / 2
    x = Symbol('x', integer=True, positive=True)
    pdf = p * (1 - p)**(x - 1)  # pdf of Geometric Distribution
    D = DiscreteRV(x, pdf, set=S.Naturals)
    assert E(D) == E(Geometric('G', S(1) / 2)) == 2
    assert P(D > 3) == S(1) / 8
    assert D.pspace.domain.set == S.Naturals
    raises(ValueError, lambda: DiscreteRV(x, x, FiniteSet(*range(4))))
Ejemplo n.º 2
0
def test_DiscreteRV():
    p = S(1)/2
    x = Symbol('x', integer=True, positive=True)
    pdf = p*(1 - p)**(x - 1) # pdf of Geometric Distribution
    D = DiscreteRV(x, pdf, set=S.Naturals, check=True)
    assert E(D) == E(Geometric('G', S(1)/2)) == 2
    assert P(D > 3) == S(1)/8
    assert D.pspace.domain.set == S.Naturals
    raises(ValueError, lambda: DiscreteRV(x, x, FiniteSet(*range(4)), check=True))

    # purposeful invalid pmf but it should not raise since check=False
    # see test_drv_types.test_ContinuousRV for explanation
    X = DiscreteRV(x, 1/x, S.Naturals)
    assert P(X < 2) == 1
    assert E(X) == oo
Ejemplo n.º 3
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
    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