def _resample_Z_gsl(self, data=[]): """ Resample the parents given the bias_model, weight_model, and impulse_model. :param bias_model: :param weight_model: :param impulse_model: :return: """ from gslrandom import multinomial bias_model, weight_model, impulse_model = \ self.model.bias_model, self.model.weight_model, self.model.impulse_model K, B = self.K, self.B # Make a big matrix of size T x (K*B + 1) for k2, (Sk, Fk, Tk, Zk) in enumerate(zip(self.Ss, self.Fs, self.Ts, self.Z)): P = np.zeros((Tk, 1 + self.K * self.B)) P[:, 0] = bias_model.lambda0[k2] Wk2 = np.repeat(weight_model.W_effective[:, k2], B) Gk2 = impulse_model.g[:, k2, :].reshape((K * B, ), order="C") P[:, 1:] = Wk2 * Gk2 * Fk.reshape((Tk, K * B)) # Normalize the rows P = P / P.sum(1)[:, None] # Sample parents from P with counts S[:,k2] multinomial(self.pyrngs, Sk, P, out=Zk)
def _resample_Z_gsl(self, data=[]): """ Resample the parents given the bias_model, weight_model, and impulse_model. :param bias_model: :param weight_model: :param impulse_model: :return: """ from gslrandom import multinomial bias_model, weight_model, impulse_model = \ self.model.bias_model, self.model.weight_model, self.model.impulse_model K, B = self.K, self.B # Make a big matrix of size T x (K*B + 1) for k2, (Sk, Fk, Tk, Zk) in enumerate(zip(self.Ss, self.Fs, self.Ts, self.Z)): P = np.zeros((Tk, 1+self.K * self.B)) P[:,0] = bias_model.lambda0[k2] Wk2 = np.repeat(weight_model.W_effective[:,k2], B) Gk2 = impulse_model.g[:,k2,:].reshape((K*B,), order="C") P[:,1:] = Wk2 * Gk2 * Fk.reshape((Tk, K*B)) # Normalize the rows P = P / P.sum(1)[:,None] # Sample parents from P with counts S[:,k2] multinomial(self.pyrngs, Sk, P, out=Zk)
def test_one_rng_one_N_one_p_with_out(): K = 5 N = 10 p_K = np.ones(K) / K rng = PyRNG(rn.randint(2**16)) n_iter = 10000 z_K = np.zeros(K) for _ in xrange(n_iter): n_K = np.zeros(K, dtype=np.float) multinomial(rng, 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)
def test_simple_with_out(): # One N count, one p array, out structure provided N = 10 K = 5 p = 1./K * np.ones(K) rng = PyRNG() n_iter = 10000 z = np.zeros(K) for _ in xrange(n_iter): out = np.zeros(K, dtype=np.float) multinomial(rng, N, p, out) assert out.sum() == N z += out assert (np.abs(z/z.sum() - p) < 1e-2).all()
def test_multi_N_single_p_with_out(): # Multiple N counts, one p array, out structure provided L = 10 N = np.arange(L) + 10 K = 5 p = 1./K * np.ones(K) rng = PyRNG() n_iter = 10000 z = np.zeros((L,K)) for _ in xrange(n_iter): out = np.zeros((L,K), dtype=np.uint32) multinomial(rng, N, p, out) assert (out.sum(axis=1) == N).all() z += out assert (np.abs(z/z.sum(axis=1)[:,np.newaxis] - p) < 1e-2).all()
def test_one_rng_multi_N_one_p_with_out(): K = 5 I = 10 N_I = np.ones(I) * 10 p_K = np.ones(K) / K rng = PyRNG(rn.randint(2**16)) n_iter = 10000 z_IK = np.zeros((I, K)) for _ in xrange(n_iter): n_IK = np.zeros((I, K)) multinomial(rng, 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)
def test_one_rng_multi_N_multi_p_with_out(): K = 5 I = 3 N_I = np.arange(1, I+1) * 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 rng = PyRNG(rn.randint(2**16)) n_iter = 10000 z_IK = np.zeros((I, K)) for _ in xrange(n_iter): n_IK = np.zeros((I, K)) multinomial(rng, N_I, p_IK, out=n_IK) 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) assert np.allclose(norm_z_IK, p_IK, atol=1e-2)
def test_one_rng_multi_N_multi_p_with_out(): K = 5 I = 3 N_I = np.arange(1, I + 1) * 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 rng = PyRNG(rn.randint(2**16)) n_iter = 10000 z_IK = np.zeros((I, K)) for _ in xrange(n_iter): n_IK = np.zeros((I, K)) multinomial(rng, N_I, p_IK, out=n_IK) 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) assert np.allclose(norm_z_IK, p_IK, atol=1e-2)
def test_single_N_multi_p_with_out(): # One N count, multiple p arrays, out structure provided N = 10 K = 5 p = np.zeros((2, K)) p[0,:] = 1./K * np.ones(K) p[1,:] = np.asarray([0.5, 0.25, 0.05, 0.1, 0.1]) rng = PyRNG() n_iter = 10000 z = np.zeros((2,K)) for _ in xrange(n_iter): out = np.zeros((2,K), dtype=np.uint32) multinomial(rng, N, p, out) assert out.shape == (2,K) assert (out.sum(axis=1) == N).all() z += out assert (np.abs(z/z.sum(axis=1)[:,np.newaxis] - p) < 1e-2).all()
def test_multi_rng_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 rngs = [PyRNG(rn.randint(2**16)) for _ in xrange(get_omp_num_threads())] 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) multinomial(rngs, 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)
def test_multi_rng_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 rngs = [PyRNG(rn.randint(2**16)) for _ in xrange(get_omp_num_threads())] 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) multinomial(rngs, 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)
def test_multi_N_single_p(): # Multiple N counts, one p array, out structure NOT provided L = 10 N = np.arange(L) + 10 K = 5 p = 1./K * np.ones(K) rng = PyRNG() n_iter = 10000 z = np.zeros((L,K)) for _ in xrange(n_iter): n = multinomial(rng, N, p) assert n.shape == (L,K) assert (n.sum(axis=1) == N).all() z += n assert (np.abs(z/z.sum(axis=1)[:,np.newaxis] - p) < 1e-2).all()
def test_one_rng_one_N_multi_p_no_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 rng = PyRNG(rn.randint(2**16)) n_iter = 10000 z_IK = np.zeros((I, K)) for _ in xrange(n_iter): n_IK = multinomial(rng, N, p_IK) assert n_IK.shape == (I, K) 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)
def test_simple(): # One N count, one p array, out structure NOT provided N = 10 K = 5 p = 1./K * np.ones(K) rng = PyRNG(np.random.randint(2**16)) # rng = PyRNG(0) n_iter = 10000 z = np.zeros(K) for _ in xrange(n_iter): n = multinomial(rng, N, p) assert n.sum() == N z += n print n assert (np.abs(z/z.sum() - p) < 1e-2).all()
def test_single_N_multi_p(): # One N count, multiple p arrays, out structure NOT provided N = 10 K = 5 p = np.zeros((2, K)) p[0,:] = 1./K * np.ones(K) p[1,:] = np.asarray([0.5, 0.25, 0.05, 0.1, 0.1]) rng = PyRNG() n_iter = 10000 z = np.zeros((2,K)) for _ in xrange(n_iter): n = multinomial(rng, N, p) assert n.shape == (2,K) assert (n.sum(axis=1) == N).all() z += n assert (np.abs(z/z.sum(axis=1)[:,np.newaxis] - p) < 1e-2).all()
def resample_z(self): topicprobs = self._get_topicprobs() multinomial(self.pyrngs, self.data.data, topicprobs, self.z) self._update_counts()
def coupling_random_sample(n, l, s, threads): P = np.empty((s, len(l)), dtype=np.uint32) l_tile = np.tile(l, (s, 1)) rngs = [PyRNG(np.random.randint(2**16)) for _ in range(threads)] return multinomial(rngs, n, l_tile, P)
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() rng = PyRNG() N_IK = np.ones((I, K), dtype=np.uint32) s = time.time() for i in xrange(I):
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() rng = PyRNG() N_IK = np.ones((I, K), dtype=np.uint32) s = time.time() for i in xrange(I): N_IK[i, :] = rn.multinomial(N_I[i], P_IK[i, :])