forked from pymc-devs/pymc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_data_container.py
374 lines (324 loc) · 14.1 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Copyright 2020 The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import pandas as pd
import pytest
from aesara import shared
from aesara.tensor.sharedvar import ScalarSharedVariable
from aesara.tensor.var import TensorVariable
import pymc3 as pm
from pymc3.aesaraf import floatX
from pymc3.distributions import logpt
from pymc3.exceptions import ShapeError
from pymc3.tests.helpers import SeededTest
class TestData(SeededTest):
def test_deterministic(self):
data_values = np.array([0.5, 0.4, 5, 2])
with pm.Model() as model:
X = pm.Data("X", data_values)
pm.Normal("y", 0, 1, observed=X)
model.logp(model.initial_point)
@pytest.mark.xfail(reason="Competence hasn't been updated")
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.0, 10.0)
pm.Normal("obs", b * x_shared, np.sqrt(1e-2), observed=y)
prior_trace0 = pm.sample_prior_predictive(1000)
idata = pm.sample(1000, init=None, tune=1000, chains=1)
pp_trace0 = pm.sample_posterior_predictive(idata, 1000)
x_shared.set_value(x_pred)
prior_trace1 = pm.sample_prior_predictive(1000)
pp_trace1 = pm.sample_posterior_predictive(idata, samples=1000)
assert prior_trace0["b"].shape == (1000,)
assert prior_trace0["obs"].shape == (1000, 100)
assert prior_trace1["obs"].shape == (1000, 200)
assert pp_trace0["obs"].shape == (1000, 100)
np.testing.assert_allclose(x, pp_trace0["obs"].mean(axis=0), atol=1e-1)
assert pp_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.0, 2.0, 3.0])
y = pm.Data("y", [1.0, 2.0, 3.0])
beta = pm.Normal("beta", 0, 10.0)
pm.Normal("obs", beta * x, np.sqrt(1e-2), observed=y)
trace = pm.sample(
1000,
tune=1000,
chains=1,
return_inferencedata=False,
compute_convergence_checks=False,
)
# 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.0, 2.0, 3.0])
y = pm.Data("y", [1.0, 2.0, 3.0])
beta = pm.Normal("beta", 0, 10.0)
pm.Normal("obs", beta * x, np.sqrt(1e-2), observed=y)
pm.sample(
1000,
init=None,
tune=1000,
chains=1,
compute_convergence_checks=False,
)
# Predict on new data.
new_x = [5.0, 6.0, 9.0]
new_y = [5.0, 6.0, 9.0]
with model:
pm.set_data(new_data={"x": new_x, "y": new_y})
new_idata = pm.sample(
1000,
init=None,
tune=1000,
chains=1,
compute_convergence_checks=False,
)
pp_trace = pm.sample_posterior_predictive(new_idata, 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_shared_data_as_index(self):
"""
Allow pm.Data to be used for index variables, i.e with integers as well as floats.
See https://github.com/pymc-devs/pymc3/issues/3813
"""
with pm.Model() as model:
index = pm.Data("index", [2, 0, 1, 0, 2])
y = pm.Data("y", [1.0, 2.0, 3.0, 2.0, 1.0])
alpha = pm.Normal("alpha", 0, 1.5, size=3)
pm.Normal("obs", alpha[index], np.sqrt(1e-2), observed=y)
prior_trace = pm.sample_prior_predictive(1000, var_names=["alpha"])
idata = pm.sample(
1000,
init=None,
tune=1000,
chains=1,
compute_convergence_checks=False,
)
# Predict on new data
new_index = np.array([0, 1, 2])
new_y = [5.0, 6.0, 9.0]
with model:
pm.set_data(new_data={"index": new_index, "y": new_y})
pp_trace = pm.sample_posterior_predictive(idata, 1000, var_names=["alpha", "obs"])
assert prior_trace["alpha"].shape == (1000, 3)
assert idata.posterior["alpha"].shape == (1, 1000, 3)
assert pp_trace["alpha"].shape == (1000, 3)
assert pp_trace["obs"].shape == (1000, 3)
def test_shared_data_as_rv_input(self):
"""
Allow pm.Data to be used as input for other RVs.
See https://github.com/pymc-devs/pymc3/issues/3842
"""
with pm.Model() as m:
x = pm.Data("x", [1.0, 2.0, 3.0])
y = pm.Normal("y", mu=x, size=(2, 3))
assert y.eval().shape == (2, 3)
idata = pm.sample(
chains=1,
tune=500,
draws=550,
return_inferencedata=True,
compute_convergence_checks=False,
)
samples = idata.posterior["y"]
assert samples.shape == (1, 550, 2, 3)
np.testing.assert_allclose(np.array([1.0, 2.0, 3.0]), x.get_value(), atol=1e-1)
np.testing.assert_allclose(
np.array([1.0, 2.0, 3.0]), samples.mean(("chain", "draw", "y_dim_0")), atol=1e-1
)
with m:
pm.set_data({"x": np.array([2.0, 4.0, 6.0])})
assert y.eval().shape == (2, 3)
idata = pm.sample(
chains=1,
tune=500,
draws=620,
return_inferencedata=True,
compute_convergence_checks=False,
)
samples = idata.posterior["y"]
assert samples.shape == (1, 620, 2, 3)
np.testing.assert_allclose(np.array([2.0, 4.0, 6.0]), x.get_value(), atol=1e-1)
np.testing.assert_allclose(
np.array([2.0, 4.0, 6.0]), samples.mean(("chain", "draw", "y_dim_0")), atol=1e-1
)
def test_shared_scalar_as_rv_input(self):
# See https://github.com/pymc-devs/pymc3/issues/3139
with pm.Model() as m:
shared_var = shared(5.0)
v = pm.Normal("v", mu=shared_var, size=1)
np.testing.assert_allclose(
logpt(v, np.r_[5.0]).eval(),
-0.91893853,
rtol=1e-5,
)
shared_var.set_value(10.0)
np.testing.assert_allclose(
logpt(v, np.r_[10.0]).eval(),
-0.91893853,
rtol=1e-5,
)
def test_creation_of_data_outside_model_context(self):
with pytest.raises((IndexError, 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.0, 2.0, 3.0])
y = np.array([1.0, 2.0, 3.0])
beta = pm.Normal("beta", 0, 10.0)
pm.Normal("obs", beta * x, np.sqrt(1e-2), observed=y)
pm.sample(
1000,
init=None,
tune=1000,
chains=1,
compute_convergence_checks=False,
)
with pytest.raises(TypeError) as error:
pm.set_data({"beta": [1.1, 2.2, 3.3]}, model=model)
error.match("The variable `beta` must be a `SharedVariable`")
@pytest.mark.xfail(reason="Depends on ModelGraph")
def test_model_to_graphviz_for_model_with_data_container(self):
with pm.Model() as model:
x = pm.Data("x", [1.0, 2.0, 3.0])
y = pm.Data("y", [1.0, 2.0, 3.0])
beta = pm.Normal("beta", 0, 10.0)
obs_sigma = floatX(np.sqrt(1e-2))
pm.Normal("obs", beta * x, obs_sigma, observed=y)
pm.sample(
1000,
init=None,
tune=1000,
chains=1,
compute_convergence_checks=False,
)
for formatting in {"latex", "latex_with_params"}:
with pytest.raises(ValueError, match="Unsupported formatting"):
pm.model_to_graphviz(model, formatting=formatting)
exp_without = [
'x [label="x\n~\nData" shape=box style="rounded, filled"]',
'beta [label="beta\n~\nNormal"]',
'obs [label="obs\n~\nNormal" style=filled]',
]
exp_with = [
'x [label="x\n~\nData" shape=box style="rounded, filled"]',
'beta [label="beta\n~\nNormal(mu=0.0, sigma=10.0)"]',
f'obs [label="obs\n~\nNormal(mu=f(f(beta), x), sigma={obs_sigma})" style=filled]',
]
for formatting, expected_substrings in [
("plain", exp_without),
("plain_with_params", exp_with),
]:
g = pm.model_to_graphviz(model, formatting=formatting)
# check formatting of RV nodes
for expected in expected_substrings:
assert expected in g.source
def test_explicit_coords(self):
N_rows = 5
N_cols = 7
data = np.random.uniform(size=(N_rows, N_cols))
coords = {
"rows": [f"R{r+1}" for r in range(N_rows)],
"columns": [f"C{c+1}" for c in range(N_cols)],
}
# pass coordinates explicitly, use numpy array in Data container
with pm.Model(coords=coords) as pmodel:
pm.Data("observations", data, dims=("rows", "columns"))
assert "rows" in pmodel.coords
assert pmodel.coords["rows"] == ["R1", "R2", "R3", "R4", "R5"]
assert "rows" in pmodel.dim_lengths
assert isinstance(pmodel.dim_lengths["rows"], ScalarSharedVariable)
assert pmodel.dim_lengths["rows"].eval() == 5
assert "columns" in pmodel.coords
assert pmodel.coords["columns"] == ["C1", "C2", "C3", "C4", "C5", "C6", "C7"]
assert pmodel.RV_dims == {"observations": ("rows", "columns")}
assert "columns" in pmodel.dim_lengths
assert isinstance(pmodel.dim_lengths["columns"], ScalarSharedVariable)
assert pmodel.dim_lengths["columns"].eval() == 7
def test_symbolic_coords(self):
"""
In v4 dimensions can be created without passing coordinate values.
Their lengths are then automatically linked to the corresponding Tensor dimension.
"""
with pm.Model() as pmodel:
intensity = pm.Data("intensity", np.ones((2, 3)), dims=("row", "column"))
assert "row" in pmodel.dim_lengths
assert "column" in pmodel.dim_lengths
assert isinstance(pmodel.dim_lengths["row"], TensorVariable)
assert isinstance(pmodel.dim_lengths["column"], TensorVariable)
assert pmodel.dim_lengths["row"].eval() == 2
assert pmodel.dim_lengths["column"].eval() == 3
intensity.set_value(floatX(np.ones((4, 5))))
assert pmodel.dim_lengths["row"].eval() == 4
assert pmodel.dim_lengths["column"].eval() == 5
def test_no_resize_of_implied_dimensions(self):
with pm.Model() as pmodel:
# Imply a dimension through RV params
pm.Normal("n", mu=[1, 2, 3], dims="city")
# _Use_ the dimension for a data variable
inhabitants = pm.Data("inhabitants", [100, 200, 300], dims="city")
# Attempting to re-size the dimension through the data variable would
# cause shape problems in InferenceData conversion, because the RV remains (3,).
with pytest.raises(
ShapeError, match="was initialized from 'n' which is not a shared variable"
):
pmodel.set_data("inhabitants", [1, 2, 3, 4])
def test_implicit_coords_series(self):
ser_sales = pd.Series(
data=np.random.randint(low=0, high=30, size=22),
index=pd.date_range(start="2020-05-01", periods=22, freq="24H", name="date"),
name="sales",
)
with pm.Model() as pmodel:
pm.Data("sales", ser_sales, dims="date", export_index_as_coords=True)
assert "date" in pmodel.coords
assert len(pmodel.coords["date"]) == 22
assert pmodel.RV_dims == {"sales": ("date",)}
def test_implicit_coords_dataframe(self):
N_rows = 5
N_cols = 7
df_data = pd.DataFrame()
for c in range(N_cols):
df_data[f"Column {c+1}"] = np.random.normal(size=(N_rows,))
df_data.index.name = "rows"
df_data.columns.name = "columns"
# infer coordinates from index and columns of the DataFrame
with pm.Model() as pmodel:
pm.Data("observations", df_data, dims=("rows", "columns"), export_index_as_coords=True)
assert "rows" in pmodel.coords
assert "columns" in pmodel.coords
assert pmodel.RV_dims == {"observations": ("rows", "columns")}
def test_data_naming():
"""
This is a test for issue #3793 -- `Data` objects in named models are
not given model-relative names.
"""
with pm.Model("named_model") as model:
x = pm.Data("x", [1.0, 2.0, 3.0])
y = pm.Normal("y")
assert y.name == "named_model_y"
assert x.name == "named_model_x"