예제 #1
0
def test_observed_in_model():
    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)
예제 #2
0
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)
예제 #3
0
def test_initialise_with_list():
    with tp.Model() as model:
        mu = tp.Parameter()
        sigma = tp.Parameter(lower=0)
        X = tp.Normal(mu, sigma)
    model.observed(X)
    model.initialize([5, 2])
예제 #4
0
def test_assign_empty_dict():
    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.assign({})
예제 #5
0
def test_assign_wrong_container():
    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.assign([1, 2, 3])
예제 #6
0
def test_assign_in_before_observed():
    model = tp.Model()
    with model:
        mu = tp.Parameter(lower=-5, upper=5)
        sigma = tp.Parameter(lower=0)
        tp.Normal(mu, sigma)

    model.assign({mu: 1, sigma: 0})
예제 #7
0
def test_initalize_in_model_block():
    with tp.Model() as model:
        mu = tp.Parameter()
        sigma = tp.Parameter(lower=0)
        tp.Normal(mu, sigma)
        model.initialize({
            mu: 5,
            sigma: 2
        })
예제 #8
0
def test_initialise_before_observed():
    with tp.Model() as model:
        mu = tp.Parameter()
        sigma = tp.Parameter(lower=0)
        tp.Normal(mu, sigma)
    model.initialize({
        mu: 5,
        sigma: 2
    })
예제 #9
0
def test_initialise_with_too_few_variables():
    with tp.Model() as model:
        mu = tp.Parameter()
        sigma = tp.Parameter(lower=0)
        X = tp.Normal(mu, sigma)
    model.observed(X)
    model.initialize({
        mu: 5
    })
예제 #10
0
def test_assign_in_model_block():
    model = tp.Model()
    with model:
        mu = tp.Parameter(lower=-5, upper=5)
        sigma = tp.Parameter(lower=0)
        X = tp.Normal(mu, sigma)
        model.assign({mu: 1, sigma: 0})

    model.observed(X)
    model.initialize({mu: 42, sigma: 1})
예제 #11
0
def test_initialise_with_invalid_keys():
    with tp.Model() as model:
        mu = tp.Parameter()
        sigma = tp.Parameter(lower=0)
        X = tp.Normal(mu, sigma)
    model.observed(X)
    model.initialize({
        'Parameter_1': 5,
        'Parameter_2': 2
    })
예제 #12
0
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
예제 #13
0
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)
예제 #14
0
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
예제 #15
0
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
예제 #16
0
def make_normal():
    mu = tp.Parameter(name='mu')
    sigma = tp.Parameter(name='sigma', lower=0)
    X = tp.Normal(mu, sigma)
    return mu, sigma, X
예제 #17
0
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)
예제 #18
0
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))
예제 #19
0
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)