def test_likelihood_free_mixture(): p1 = Normal(random_state=1) p2 = Normal(mu=2.0, random_state=1) h1 = Histogram(bins=50).fit(p1.rvs(10000)) h2 = Histogram(bins=50).fit(p2.rvs(10000)) m1 = Mixture(components=[p1, p2]) m2 = Mixture(components=[h1, h2]) # Check whether pdf, nnlf and cdf have been overriden assert isinstance(m1.pdf, theano.compile.function_module.Function) assert isinstance(m1.nnlf, theano.compile.function_module.Function) assert isinstance(m1.cdf, theano.compile.function_module.Function) assert isinstance(m2.pdf, types.MethodType) assert isinstance(m2.nnlf, types.MethodType) assert isinstance(m2.cdf, types.MethodType) # Compare pdfs rng = check_random_state(1) X = rng.rand(100, 1) * 10 - 5 assert np.mean(np.abs(m1.pdf(X) - m2.pdf(X))) < 0.05 # Test sampling X = m2.rvs(10) assert X.shape == (10, 1) # Check errors assert_raises(NotImplementedError, m2.fit, X)
def test_mv_mixture(): p1 = MultivariateNormal(mu=np.array([0.0, 0.0]), sigma=np.eye(2)) p2 = MultivariateNormal(mu=np.array([2.0, 2.0]), sigma=0.5 * np.eye(2)) m = Mixture(components=[p1, p2]) assert m.ndim == 2 X = m.rvs(100) assert X.shape == (100, 2) assert_raises(ValueError, Mixture, components=[p1, Normal()])
def test_rvs(): p1 = Normal(mu=0.0, sigma=T.constant(1.0), random_state=0) p2 = Normal(mu=2.0, sigma=2.0, random_state=0) m = Mixture(components=[p1, p2], weights=[0.25], random_state=0) X = m.rvs(2000) assert (np.mean(X) - (0.25 * p1.mu.eval() + 0.75 * p2.mu.eval())) < 0.1