forked from pymc-devs/pymc
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_data_container.py
112 lines (94 loc) · 4.37 KB
/
test_data_container.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import pymc3 as pm
from .helpers import SeededTest
import numpy as np
import pytest
class TestData(SeededTest):
def test_deterministic(self):
data_values = np.array([.5, .4, 5, 2])
with pm.Model() as model:
X = pm.Data('X', data_values)
pm.Normal('y', 0, 1, observed=X)
model.logp(model.test_point)
def test_sample(self):
x = np.random.normal(size=100)
y = x + np.random.normal(scale=1e-2, size=100)
x_pred = np.linspace(-3, 3, 200, dtype='float32')
with pm.Model():
x_shared = pm.Data('x_shared', x)
b = pm.Normal('b', 0., 10.)
pm.Normal('obs', b * x_shared, np.sqrt(1e-2), observed=y)
prior_trace0 = pm.sample_prior_predictive(1000)
trace = pm.sample(1000, init=None, progressbar=False)
pp_trace0 = pm.sample_posterior_predictive(trace, 1000)
x_shared.set_value(x_pred)
pp_trace1 = pm.sample_posterior_predictive(trace, samples=1000)
prior_trace1 = pm.sample_prior_predictive(1000)
assert prior_trace0['b'].shape == (1000,)
assert prior_trace0['obs'].shape == (1000, 100)
np.testing.assert_allclose(x, pp_trace0['obs'].mean(axis=0), atol=1e-1)
assert prior_trace1['b'].shape == (1000,)
assert prior_trace1['obs'].shape == (1000, 200)
np.testing.assert_allclose(x_pred, pp_trace1['obs'].mean(axis=0),
atol=1e-1)
def test_sample_posterior_predictive_after_set_data(self):
with pm.Model() as model:
x = pm.Data('x', [1., 2., 3.])
y = pm.Data('y', [1., 2., 3.])
beta = pm.Normal('beta', 0, 10.)
pm.Normal('obs', beta * x, np.sqrt(1e-2), observed=y)
trace = pm.sample(1000, tune=1000, chains=1)
# Predict on new data.
with model:
x_test = [5., 6., 9.]
pm.set_data(new_data={'x': x_test})
y_test = pm.sample_posterior_predictive(trace)
assert y_test['obs'].shape == (1000, 3)
np.testing.assert_allclose(x_test, y_test['obs'].mean(axis=0),
atol=1e-1)
def test_sample_after_set_data(self):
with pm.Model() as model:
x = pm.Data('x', [1., 2., 3.])
y = pm.Data('y', [1., 2., 3.])
beta = pm.Normal('beta', 0, 10.)
pm.Normal('obs', beta * x, np.sqrt(1e-2), observed=y)
pm.sample(1000, init=None, tune=1000, chains=1)
# Predict on new data.
new_x = [5., 6., 9.]
new_y = [5., 6., 9.]
with model:
pm.set_data(new_data={'x': new_x, 'y': new_y})
new_trace = pm.sample()
pp_trace = pm.sample_posterior_predictive(new_trace, 1000)
assert pp_trace['obs'].shape == (1000, 3)
np.testing.assert_allclose(new_y, pp_trace['obs'].mean(axis=0),
atol=1e-1)
def test_creation_of_data_outside_model_context(self):
with pytest.raises(TypeError) as error:
pm.Data('data', [1.1, 2.2, 3.3])
error.match('No model on context stack')
def test_set_data_to_non_data_container_variables(self):
with pm.Model() as model:
x = np.array([1., 2., 3.])
y = np.array([1., 2., 3.])
beta = pm.Normal('beta', 0, 10.)
pm.Normal('obs', beta * x, np.sqrt(1e-2), observed=y)
pm.sample(1000, init=None, tune=1000, chains=1)
with pytest.raises(TypeError) as error:
pm.set_data({'beta': [1.1, 2.2, 3.3]}, model=model)
error.match('defined as `pymc3.Data` inside the model')
def test_model_to_graphviz_for_model_with_data_container(self):
with pm.Model() as model:
x = pm.Data('x', [1., 2., 3.])
y = pm.Data('y', [1., 2., 3.])
beta = pm.Normal('beta', 0, 10.)
pm.Normal('obs', beta * x, np.sqrt(1e-2), observed=y)
pm.sample(1000, init=None, tune=1000, chains=1)
g = pm.model_to_graphviz(model)
# Data node rendered correctly?
text = 'x [label="x ~ Data" shape=box style="rounded, filled"]'
assert text in g.source
# Didn't break ordinary variables?
text = 'beta [label="beta ~ Normal"]'
assert text in g.source
text = 'obs [label="obs ~ Normal" style=filled]'
assert text in g.source