def test_observed(): with tp.Model() as model: mu = tp.Parameter() sigma = tp.Parameter() X = tp.Normal(mu, sigma) model.observed(X) assert(X in model._observed)
def test_distribution_creation_global_graph(): # Distribution creation doesn't modify the global graph before = tf.get_default_graph().as_graph_def() with tp.Model(): tp.Parameter() after = tf.get_default_graph().as_graph_def() assert before == after
def test_init(): with tp.Model(): X1 = tp.Uniform(lower=-1, upper=1) X2 = tp.Uniform(lower=-1) X3 = tp.Uniform(upper=1) X4 = tp.Uniform() X7 = tp.Uniform(lower=X1, upper=X2)
def test_pdf(): with tp.Model() as m: mu, sigma, X = make_normal() m.observed(X) xs = np.linspace(-5, 5, 100) out1 = st.norm.pdf(xs, 0, 1) m.initialize({mu: 0, sigma: 1}) out2 = m.pdf(xs) assert_array_almost_equal(out1, out2, 16)
def test_pdf(): with tp.Model() as m: dummy = tp.Parameter() X = tp.Uniform(lower=dummy, upper=1) m.observed(X) m.initialize({dummy: -2}) xs = np.linspace(-1, 1, 1) out1 = st.uniform.pdf(xs, -2, 3) out2 = m.pdf(xs) assert_array_almost_equal(out1, out2, 16)
def test_fit(): model = tp.Model() with model: mu = tp.Parameter(lower=-5, upper=5) sigma = tp.Parameter(lower=0) X = tp.Normal(mu, sigma) model.observed(X) model.initialize({mu: 2, sigma: 2}) np.random.seed(0) data = np.random.normal(0, 1, 100) results = model.fit(data) assert results.success
def test_assign(): model = tp.Model() with model: mu = tp.Parameter(lower=-5, upper=5) sigma = tp.Parameter(lower=0) X = tp.Normal(mu, sigma) model.observed(X) feed = {mu: 42, sigma: 1} model.initialize(feed) model.assign({mu: 1, sigma: 0}) model.assign({mu: -100, sigma: 0}) assert model.state == {mu: -100, sigma: 0} model.assign(feed) assert model.state == feed
def test_nll(): with tp.Model() as model: mu = tp.Parameter() sigma = tp.Parameter() X = tp.Normal(mu, sigma) model.observed(X) xs = np.linspace(-5, 5, 100) model.initialize({ mu: 0, sigma: 1 }) out1 = model.nll(xs) out2 = -sum(st.norm.logpdf(xs, 0, 1)) assert_almost_equal(out1, out2, 10)
def test_internal_graph_no_growth(): # Calling assign or fit doesn't grow the execution graph with tp.Model() as model: mu = tp.Parameter() sigma = tp.Parameter(lower=0) X = tp.Normal(mu, sigma) model.observed(X) model.initialize({ mu: 1, sigma: 1, }) before = model.session.graph_def model.assign({ mu: 0, sigma: 2, }) model.fit([1, 2, 3]) after = model.session.graph_def assert before == after
def test_observed_in_model_block(): with tp.Model(): mu = tp.Parameter() sigma = tp.Parameter(lower=0) X = tp.Normal(mu, sigma) tp.observed(X)
def test_prepare_without_observed(): with tp.Model() as model: mu = tp.Parameter() model.initialize({mu: 42})
def test_observed_erorr_on_non_distribution(): with tp.Model() as model: mu = tp.Parameter() sigma = tp.Parameter() X = tp.Normal(mu, sigma) model.observed(X, int(42))
def test_nesting_models(): with tp.Model(): with tp.Model(): # Make coveralls ignore this line raise NotImplementedError
def test_distribution_creation_outside_with(): with tp.Model(): mu = tp.Parameter(name='mu') sigma = tp.Parameter(name='sigma', lower=0) tp.Normal(mu, sigma)