Ejemplo n.º 1
0
def test_seeded_one_N_one_p_with_out():
    K = 5
    N = 10
    p_K = np.ones(K) / K

    n_iter = 10000
    z_K = np.zeros(K)
    for _ in xrange(n_iter):
        n_K = np.zeros(K, dtype=np.float)
        seeded_multinomial(N, p_K, out=n_K)
        assert n_K.sum() == N
        z_K += n_K
    assert np.allclose(z_K / z_K.sum(), p_K, atol=1e-2)
Ejemplo n.º 2
0
def test_seeded_one_N_one_p_with_out():
    K = 5
    N = 10
    p_K = np.ones(K) / K

    n_iter = 10000
    z_K = np.zeros(K)
    for _ in xrange(n_iter):
        n_K = np.zeros(K, dtype=np.float)
        seeded_multinomial(N, p_K, out=n_K)
        assert n_K.sum() == N
        z_K += n_K
    assert np.allclose(z_K / z_K.sum(), p_K, atol=1e-2)
Ejemplo n.º 3
0
def test_seeded_one_N_multi_p_with_out():
    K = 5
    I = 3
    N = 10
    p_IK = np.ones((I, K)) / K
    p_IK[0, :] = [0.5, 0.25, 0.05, 0.1, 0.1]  # make one non-uniform

    n_iter = 10000
    z_IK = np.zeros((I, K))
    for _ in xrange(n_iter):
        n_IK = np.zeros((I, K))
        seeded_multinomial(N, p_IK, out=n_IK)
        assert (n_IK.sum(axis=1) == N).all()
        z_IK += n_IK
    norm_z_IK = z_IK.astype(float) / np.sum(z_IK, axis=1, keepdims=True)
    assert np.allclose(norm_z_IK, p_IK, atol=1e-2)
Ejemplo n.º 4
0
def test_seeded_multi_N_one_p_with_out():
    K = 5
    I = 10
    N_I = np.ones(I) * 10
    p_K = np.ones(K) / K

    n_iter = 10000
    z_IK = np.zeros((I, K))
    for _ in xrange(n_iter):
        n_IK = np.zeros((I, K))
        seeded_multinomial(N_I, p_K, out=n_IK)
        assert np.allclose(n_IK.sum(axis=1), N_I)
        z_IK += n_IK
    norm_z_IK = z_IK.astype(float) / np.sum(z_IK, axis=1, keepdims=True)
    p_IK = np.ones((I, K)) * p_K
    assert np.allclose(norm_z_IK, p_IK, atol=1e-2)
Ejemplo n.º 5
0
def test_seeded_one_N_multi_p_with_out():
    K = 5
    I = 3
    N = 10
    p_IK = np.ones((I, K)) / K
    p_IK[0, :] = [0.5, 0.25, 0.05, 0.1, 0.1]  # make one non-uniform

    n_iter = 10000
    z_IK = np.zeros((I, K))
    for _ in xrange(n_iter):
        n_IK = np.zeros((I, K))
        seeded_multinomial(N, p_IK, out=n_IK)
        assert (n_IK.sum(axis=1) == N).all()
        z_IK += n_IK
    norm_z_IK = z_IK.astype(float) / np.sum(z_IK, axis=1, keepdims=True)
    assert np.allclose(norm_z_IK, p_IK, atol=1e-2)
Ejemplo n.º 6
0
def test_seeded_multi_N_one_p_with_out():
    K = 5
    I = 10
    N_I = np.ones(I) * 10
    p_K = np.ones(K) / K

    n_iter = 10000
    z_IK = np.zeros((I, K))
    for _ in xrange(n_iter):
        n_IK = np.zeros((I, K))
        seeded_multinomial(N_I, p_K, out=n_IK)
        assert np.allclose(n_IK.sum(axis=1), N_I)
        z_IK += n_IK
    norm_z_IK = z_IK.astype(float) / np.sum(z_IK, axis=1, keepdims=True)
    p_IK = np.ones((I, K)) * p_K
    assert np.allclose(norm_z_IK, p_IK, atol=1e-2)
Ejemplo n.º 7
0
def test_seeded_multi_N_multi_p_with_out():
    K = 5
    A = 3
    B = 2
    N_AB = (np.arange(1, A*B+1) * 10).reshape((A, B))
    p_ABK = np.ones((A, B, K)) / K
    p_ABK[0, 1, :] = [0.5, 0.25, 0.05, 0.1, 0.1]  # make one non-uniform
    p_ABK[1, 0, :] = [0.9, 0.05, 0.03, 0.01, 0.01]  # make one really non-uniform

    n_iter = 10000
    z_ABK = np.zeros((A, B, K))
    for _ in xrange(n_iter):
        n_ABK = np.zeros((A, B, K), dtype=np.uint32)
        seeded_multinomial(N_AB, p_ABK, out=n_ABK)
        np.allclose(n_ABK.sum(axis=-1), N_AB)
        z_ABK += n_ABK
    norm_z_ABK = z_ABK.astype(float) / np.sum(z_ABK, axis=-1, keepdims=True)
    assert np.allclose(norm_z_ABK, p_ABK, atol=1e-2)
Ejemplo n.º 8
0
def test_seeded_multi_N_multi_p_with_out():
    K = 5
    A = 3
    B = 2
    N_AB = (np.arange(1, A * B + 1) * 10).reshape((A, B))
    p_ABK = np.ones((A, B, K)) / K
    p_ABK[0, 1, :] = [0.5, 0.25, 0.05, 0.1, 0.1]  # make one non-uniform
    p_ABK[1, 0, :] = [0.9, 0.05, 0.03, 0.01,
                      0.01]  # make one really non-uniform

    n_iter = 10000
    z_ABK = np.zeros((A, B, K))
    for _ in xrange(n_iter):
        n_ABK = np.zeros((A, B, K), dtype=np.uint32)
        seeded_multinomial(N_AB, p_ABK, out=n_ABK)
        np.allclose(n_ABK.sum(axis=-1), N_AB)
        z_ABK += n_ABK
    norm_z_ABK = z_ABK.astype(float) / np.sum(z_ABK, axis=-1, keepdims=True)
    assert np.allclose(norm_z_ABK, p_ABK, atol=1e-2)
Ejemplo n.º 9
0
from gslrandom import PyRNG, multinomial, seeded_multinomial, get_omp_num_threads
import numpy as np
import numpy.random as rn
import time

I = 100000
K = 1000

N_I = rn.poisson(rn.gamma(2, 500, size=I)).astype(np.uint32)
P_IK = 1. / K * np.ones((I, K), dtype=np.float)

N_IK = np.ones((I, K), dtype=np.uint32)
s = time.time()
seeded_multinomial(N_I, P_IK, N_IK)
print '%fs: No PyRNG parallel version with %d cores' % (time.time() - s,
                                                        get_omp_num_threads())
assert (N_IK.sum(axis=1) == N_I).all()

rngs = [PyRNG(rn.randint(2**16)) for _ in xrange(get_omp_num_threads())]
N_IK = np.ones((I, K), dtype=np.uint32)
s = time.time()
multinomial(rngs, N_I, P_IK, N_IK)
print '%fs: PyRNG parallel version with %d cores' % (time.time() - s,
                                                     len(rngs))
assert (N_IK.sum(axis=1) == N_I).all()

rng = PyRNG(rn.randint(2**16))
N_IK = np.ones((I, K), dtype=np.uint32)
s = time.time()
multinomial(rng, N_I, P_IK, N_IK)
print '%fs: PyRNG version with 1 core' % (time.time() - s)
Ejemplo n.º 10
0
from gslrandom import PyRNG, multinomial, seeded_multinomial, get_omp_num_threads
import numpy as np
import numpy.random as rn
import time

I = 100000
K = 1000

N_I = rn.poisson(rn.gamma(2, 500, size=I)).astype(np.uint32)
P_IK = 1./K * np.ones((I, K), dtype=np.float)

N_IK = np.ones((I, K), dtype=np.uint32)
s = time.time()
seeded_multinomial(N_I, P_IK, N_IK)
print '%fs: No PyRNG parallel version with %d cores' % (time.time() - s, get_omp_num_threads())
assert (N_IK.sum(axis=1) == N_I).all()

rngs = [PyRNG(rn.randint(2**16)) for _ in xrange(get_omp_num_threads())]
N_IK = np.ones((I, K), dtype=np.uint32)
s = time.time()
multinomial(rngs, N_I, P_IK, N_IK)
print '%fs: PyRNG parallel version with %d cores' % (time.time() - s, len(rngs))
assert (N_IK.sum(axis=1) == N_I).all()

rng = PyRNG(rn.randint(2**16))
N_IK = np.ones((I, K), dtype=np.uint32)
s = time.time()
multinomial(rng, N_I, P_IK, N_IK)
print '%fs: PyRNG version with 1 core' % (time.time() - s)
assert (N_IK.sum(axis=1) == N_I).all()