def test_mixin_composition(): # Check composed expressions as parameters a = theano.shared(0.0) b = theano.shared(-1.0) mu = a + b - 1.0 sigma = T.abs_(a * b) p = Normal(mu=mu, sigma=sigma) assert a in p.parameters_ assert b in p.parameters_ # Compose parameters with observed variables a = theano.shared(1.0) b = theano.shared(0.0) y = T.dmatrix(name="y") p = Normal(mu=a * y + b) assert len(p.parameters_) == 3 assert a in p.parameters_ assert b in p.parameters_ assert p.sigma in p.parameters_ assert p.mu not in p.parameters_ assert len(p.observeds_) == 1 assert y in p.observeds_ # Check signatures data_X = np.random.rand(10, 1) data_y = np.random.rand(10, 1) p.pdf(X=data_X, y=data_y) p.cdf(X=data_X, y=data_y) p.rvs(10, y=data_y) # Check error a = theano.shared(1.0) b = theano.shared(0.0) y = T.dmatrix() # y must be named assert_raises(ValueError, Normal, mu=a * y + b)
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 check_classifier_ratio(clf, method, cv): # Passing distributions directly p0 = Normal(mu=0.0) p1 = Normal(mu=0.1) ratio = ClassifierRatio(CalibratedClassifierCV(base_estimator=clf, method=method, cv=cv)) ratio.fit(numerator=p0, denominator=p1, n_samples=10000) reals = np.linspace(-1, 1, 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 # Passing X, y only X = np.vstack((p0.rvs(5000), p1.rvs(5000))) y = np.zeros(10000, dtype=np.int) y[5000:] = 1 ratio = ClassifierRatio(CalibratedClassifierCV(base_estimator=clf, method=method, cv=cv)) ratio.fit(X=X, y=y) reals = np.linspace(-1, 1, 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 test_mixin_composition(): # Check composed expressions as parameters a = theano.shared(0.0) b = theano.shared(-1.0) mu = a + b - 1.0 sigma = T.abs_(a * b) p = Normal(mu=mu, sigma=sigma) assert a in p.parameters_ assert b in p.parameters_ # Compose parameters with observed variables a = theano.shared(1.0) b = theano.shared(0.0) y = T.dmatrix(name="y") p = Normal(mu=a * y + b) assert len(p.parameters_) == 3 assert a in p.parameters_ assert b in p.parameters_ assert p.sigma in p.parameters_ assert p.mu not in p.parameters_ assert len(p.observeds_) == 1 assert y in p.observeds_ # Check signatures data_X = np.random.rand(10, 1) data_y = np.random.rand(10, 1) p.pdf(X=data_X, y=data_y) p.cdf(X=data_X, y=data_y) p.rvs(10, y=data_y) # Check error a = theano.shared(1.0) b = theano.shared(0.0) y = T.dmatrix() # y must be named assert_raises(ValueError, Normal, mu=a * y + b)
def check_classifier_ratio(clf, method, cv): # Passing distributions directly p0 = Normal(mu=0.0) p1 = Normal(mu=0.1) ratio = ClassifierRatio( CalibratedClassifierCV(base_estimator=clf, method=method, cv=cv)) ratio.fit(numerator=p0, denominator=p1, n_samples=10000) reals = np.linspace(-1, 1, 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 # Passing X, y only X = np.vstack((p0.rvs(5000), p1.rvs(5000))) y = np.zeros(10000, dtype=np.int) y[5000:] = 1 ratio = ClassifierRatio( CalibratedClassifierCV(base_estimator=clf, method=method, cv=cv)) ratio.fit(X=X, y=y) reals = np.linspace(-1, 1, 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 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_linear_transform_1d(): p0 = Normal() pt = LinearTransform(p0, A=np.array([[0.5]])) X0 = p0.rvs(10, random_state=0) Xt = pt.rvs(10, random_state=0) assert X0.shape == Xt.shape assert_array_equal(X0 * 0.5, Xt) assert_array_equal(p0.pdf(X0), pt.pdf(Xt)) assert_array_equal(p0.nll(X0), pt.nll(Xt))
def test_linear_transform_1d(): p0 = Normal() pt = LinearTransform(p0, A=np.array([[0.5]])) X0 = p0.rvs(10, random_state=0) Xt = pt.rvs(10, random_state=0) assert X0.shape == Xt.shape assert_array_equal(X0 * 0.5, Xt) assert_array_equal(p0.pdf(X0), pt.pdf(Xt)) assert_array_equal(p0.nll(X0), pt.nll(Xt))
def test_parameterized_regressor(): mu = theano.shared(0) p = Normal(mu=mu) X = p.rvs(100) y = p.pdf(X).astype(np.float32) tf = ParameterStacker(params=[mu]) clf = ParameterizedRegressor(DecisionTreeRegressor(), params=[mu]) clf.fit(tf.transform(X), y) assert clf.n_features_ == 1 assert_array_almost_equal(y, clf.predict(tf.transform(X)), decimal=3)
def test_kde(): # Test API p = Normal(random_state=1) X = p.rvs(10000) k = KernelDensity() k.fit(X) reals = np.linspace(-3, 3).reshape(-1, 1) assert np.mean(np.abs(p.pdf(reals) - k.pdf(reals))) < 0.05 assert np.mean(np.abs(p.nnlf(reals) - k.nnlf(reals))) < 0.05 # Test sampling X = k.rvs(10000) assert np.abs(np.mean(X)) < 0.05
def test_parameter_stacker(): mu = theano.shared(0) sigma = theano.shared(1) p = Normal(mu=mu, sigma=sigma) X = p.rvs(10) tf = ParameterStacker(params=[mu, sigma]) Xt = tf.transform(X) assert Xt.shape == (10, 1+2) assert_array_almost_equal(Xt[:, 1], np.zeros(10)) assert_array_almost_equal(Xt[:, 2], np.ones(10)) mu.set_value(1) Xt = tf.transform(X) assert_array_almost_equal(Xt[:, 1], np.ones(10))
def check_rvs(mu, sigma, random_state): p = Normal(mu=mu, sigma=sigma) samples = p.rvs(10000, random_state=random_state) assert np.abs(np.mean(samples) - mu) <= 0.05 assert np.abs(np.std(samples) - sigma) <= 0.05
def check_rvs(mu, sigma, random_state): p = Normal(mu=mu, sigma=sigma) samples = p.rvs(10000, random_state=random_state) assert np.abs(np.mean(samples) - mu) <= 0.05 assert np.abs(np.std(samples) - sigma) <= 0.05