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_known_density(): components = [Normal(mu=0.0), Normal(mu=0.25), Normal(mu=0.5)] p0 = Mixture(components=components, weights=[0.45, 0.1, 0.45]) p1 = Mixture(components=[components[0]] + [components[2]]) ratio = KnownDensityRatio(numerator=p0, denominator=p1) reals = np.linspace(-0.5, 1.0, num=100).reshape(-1, 1) assert ratio.score(reals, p0.pdf(reals) / p1.pdf(reals)) > -0.01 assert np.mean(np.abs(np.log(ratio.predict(reals)) - ratio.predict(reals, log=True))) < 0.01 assert ratio.nllr(reals) == -ratio.predict(reals, log=True).sum()
def test_decomposed_ratio_identity(): components = [Normal(mu=0.0), Normal(mu=0.25), Normal(mu=0.5)] p = Mixture(components=components, weights=[0.45, 0.1, 0.45]) ratio = DecomposedRatio( ClassifierRatio(CalibratedClassifierCV(base_estimator=ElasticNetCV()))) ratio.fit(numerator=p, denominator=p, n_samples=10000) reals = np.linspace(-0.5, 1.0, num=100).reshape(-1, 1) assert ratio.score(reals, p.pdf(reals) / p.pdf(reals)) == 0.0 assert_array_almost_equal(ratio.predict(reals), np.ones(len(reals))) assert_array_almost_equal(ratio.predict(reals, log=True), np.zeros(len(reals)))
def test_decomposed_ratio(): components = [Normal(mu=0.0), Normal(mu=0.25), Normal(mu=0.5)] p0 = Mixture(components=components, weights=[0.45, 0.1, 0.45]) p1 = Mixture(components=[components[0]] + [components[2]]) ratio = DecomposedRatio( ClassifierRatio(CalibratedClassifierCV(base_estimator=ElasticNetCV()))) ratio.fit(numerator=p0, denominator=p1, n_samples=10000) reals = np.linspace(-0.5, 1.0, num=100).reshape(-1, 1) assert ratio.score(reals, p0.pdf(reals) / p1.pdf(reals)) > -0.1 assert np.mean(np.abs(np.log(ratio.predict(reals)) - ratio.predict(reals, log=True))) < 0.01
def check_mixture_pdf(w0, w1, mu1, sigma1, mu2, sigma2): rng = check_random_state(1) p1 = Normal(mu=mu1, sigma=sigma1) p2 = Normal(mu=mu2, sigma=sigma2) m = Mixture(components=[p1, p2], weights=[w0, w1]) q1 = st.norm(loc=mu1, scale=sigma1) q2 = st.norm(loc=mu2, scale=sigma2) X = rng.rand(50, 1) assert_array_almost_equal(m.pdf(X).ravel(), w0 * q1.pdf(X).ravel() + (w1 if w1 is not None else (1 - w0)) * q2.pdf(X).ravel())
def check_mixture_pdf(w0, w1, mu1, sigma1, mu2, sigma2): rng = check_random_state(1) p1 = Normal(mu=mu1, sigma=sigma1) p2 = Normal(mu=mu2, sigma=sigma2) m = Mixture(components=[p1, p2], weights=[w0, w1]) q1 = st.norm(loc=mu1, scale=sigma1) q2 = st.norm(loc=mu2, scale=sigma2) X = rng.rand(50, 1) assert_array_almost_equal( m.pdf(X).ravel(), w0 * q1.pdf(X).ravel() + (w1 if w1 is not None else (1 - w0)) * q2.pdf(X).ravel())