|
1 |
| -from pymc3 import Model, Normal |
2 |
| -from numpy import ma |
| 1 | +import pytest |
| 2 | +from numpy import ma, array |
3 | 3 | import numpy
|
4 | 4 | import pandas as pd
|
5 |
| - |
| 5 | +from pymc3 import Model, Normal, sample_prior_predictive, sample, ImputationWarning |
6 | 6 |
|
7 | 7 | def test_missing():
|
8 | 8 | data = ma.masked_values([1, 2, -1, 4, -1], value=-1)
|
9 | 9 | with Model() as model:
|
10 | 10 | x = Normal('x', 1, 1)
|
11 |
| - Normal('y', x, 1, observed=data) |
| 11 | + with pytest.warns(ImputationWarning): |
| 12 | + Normal('y', x, 1, observed=data) |
12 | 13 |
|
13 | 14 | y_missing, = model.missing_values
|
14 | 15 | assert y_missing.tag.test_value.shape == (2,)
|
15 | 16 |
|
16 | 17 | model.logp(model.test_point)
|
17 | 18 |
|
| 19 | + with model: |
| 20 | + prior_trace = sample_prior_predictive() |
| 21 | + assert set(['x', 'y']) <= set(prior_trace.keys()) |
| 22 | + |
18 | 23 |
|
19 | 24 | def test_missing_pandas():
|
20 | 25 | data = pd.DataFrame([1, 2, numpy.nan, 4, numpy.nan])
|
21 | 26 | with Model() as model:
|
22 | 27 | x = Normal('x', 1, 1)
|
23 |
| - Normal('y', x, 1, observed=data) |
| 28 | + with pytest.warns(ImputationWarning): |
| 29 | + Normal('y', x, 1, observed=data) |
| 30 | + |
| 31 | + y_missing, = model.missing_values |
| 32 | + assert y_missing.tag.test_value.shape == (2,) |
| 33 | + |
| 34 | + model.logp(model.test_point) |
| 35 | + |
| 36 | + with model: |
| 37 | + prior_trace = sample_prior_predictive() |
| 38 | + assert set(['x', 'y']) <= set(prior_trace.keys()) |
| 39 | + |
| 40 | +def test_missing_with_predictors(): |
| 41 | + predictors = array([0.5, 1, 0.5, 2, 0.3]) |
| 42 | + data = ma.masked_values([1, 2, -1, 4, -1], value=-1) |
| 43 | + with Model() as model: |
| 44 | + x = Normal('x', 1, 1) |
| 45 | + with pytest.warns(ImputationWarning): |
| 46 | + Normal('y', x * predictors, 1, observed=data) |
24 | 47 |
|
25 | 48 | y_missing, = model.missing_values
|
26 | 49 | assert y_missing.tag.test_value.shape == (2,)
|
27 | 50 |
|
28 | 51 | model.logp(model.test_point)
|
| 52 | + |
| 53 | + with model: |
| 54 | + prior_trace = sample_prior_predictive() |
| 55 | + assert set(['x', 'y']) <= set(prior_trace.keys()) |
| 56 | + |
| 57 | + |
| 58 | +def test_missing_dual_observations(): |
| 59 | + with Model() as model: |
| 60 | + obs1 = ma.masked_values([1, 2, -1, 4, -1], value=-1) |
| 61 | + obs2 = ma.masked_values([-1, -1, 6, -1, 8], value=-1) |
| 62 | + beta1 = Normal('beta1', 1, 1) |
| 63 | + beta2 = Normal('beta2', 2, 1) |
| 64 | + latent = Normal('theta', shape=5) |
| 65 | + with pytest.warns(ImputationWarning): |
| 66 | + ovar1 = Normal('o1', mu=beta1 * latent, observed=obs1) |
| 67 | + with pytest.warns(ImputationWarning): |
| 68 | + ovar2 = Normal('o2', mu=beta2 * latent, observed=obs2) |
| 69 | + |
| 70 | + prior_trace = sample_prior_predictive() |
| 71 | + assert set(['beta1', 'beta2', 'theta', 'o1', 'o2']) <= set(prior_trace.keys()) |
| 72 | + sample() |
| 73 | + |
| 74 | +def test_internal_missing_observations(): |
| 75 | + with Model() as model: |
| 76 | + obs1 = ma.masked_values([1, 2, -1, 4, -1], value=-1) |
| 77 | + obs2 = ma.masked_values([-1, -1, 6, -1, 8], value=-1) |
| 78 | + with pytest.warns(ImputationWarning): |
| 79 | + theta1 = Normal('theta1', mu=2, observed=obs1) |
| 80 | + with pytest.warns(ImputationWarning): |
| 81 | + theta2 = Normal('theta2', mu=theta1, observed=obs2) |
| 82 | + |
| 83 | + prior_trace = sample_prior_predictive() |
| 84 | + assert set(['theta1', 'theta2']) <= set(prior_trace.keys()) |
| 85 | + sample() |
0 commit comments